gmid/test.c

156 lines
3.0 KiB
C
Raw Normal View History

2024-03-10 13:24:50 +01:00
#include <sys/stat.h>
2024-03-10 13:15:42 +01:00
#include <sys/socket.h>
2024-03-10 13:24:50 +01:00
#include <sys/wait.h>
2024-03-10 13:15:42 +01:00
#include <err.h>
2024-03-10 13:29:56 +01:00
#include <errno.h>
2024-03-10 13:15:42 +01:00
#include <fcntl.h>
2024-03-10 13:29:56 +01:00
#include <poll.h>
2024-03-10 13:15:42 +01:00
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
2024-03-10 13:40:00 +01:00
char buf[1024];
2024-03-10 13:37:27 +01:00
2024-03-10 13:15:42 +01:00
int
parent(int sock)
{
int fd;
struct msghdr msg;
struct cmsghdr *cmsg;
union {
struct cmsghdr hdr;
unsigned char buf[CMSG_SPACE(sizeof(int))];
} cmsgbuf;
struct iovec iov[1];
int i;
2024-03-10 13:49:58 +01:00
if ((fd = open("/dev/null", O_RDONLY)) == -1)
err(1, "parent: open /dev/null");
2024-03-10 13:15:42 +01:00
2024-03-10 13:49:58 +01:00
for (i = 0; i < 500000; ++i) {
2024-03-10 13:37:27 +01:00
iov[0].iov_base = buf;
iov[0].iov_len = sizeof(buf);
2024-03-10 13:15:42 +01:00
memset(&msg, 0, sizeof(msg));
msg.msg_control = &cmsgbuf.buf;
msg.msg_controllen = sizeof(cmsgbuf.buf);
msg.msg_iov = iov;
msg.msg_iovlen = 1;
cmsg = CMSG_FIRSTHDR(&msg);
cmsg->cmsg_len = CMSG_LEN(sizeof(int));
cmsg->cmsg_level = SOL_SOCKET;
cmsg->cmsg_type = SCM_RIGHTS;
*(int *)CMSG_DATA(cmsg) = fd;
2024-03-10 13:29:56 +01:00
again:
if (sendmsg(sock, &msg, 0) == -1) {
if (errno == EAGAIN) {
struct pollfd pfd = {.fd = sock, .events = POLLOUT};
poll(&pfd, 1, -1);
goto again;
}
2024-03-10 13:15:42 +01:00
err(1, "parent:sendmsg");
2024-03-10 13:29:56 +01:00
}
2024-03-10 13:15:42 +01:00
}
return 0;
}
int
child(int sock)
{
int fd;
struct msghdr msg;
struct cmsghdr *cmsg;
union {
struct cmsghdr hdr;
unsigned char buf[CMSG_SPACE(sizeof(int))];
} cmsgbuf;
struct iovec iov[1];
ssize_t n;
2024-03-10 13:24:50 +01:00
struct stat sb;
2024-03-10 13:15:42 +01:00
for (;;) {
2024-03-10 13:37:27 +01:00
iov[0].iov_base = buf;
iov[0].iov_len = sizeof(buf);
2024-03-10 13:15:42 +01:00
memset(&msg, 0, sizeof(msg));
msg.msg_control = &cmsgbuf.buf;
msg.msg_controllen = sizeof(cmsgbuf.buf);
msg.msg_iov = iov;
msg.msg_iovlen = 1;
2024-03-10 13:37:27 +01:00
again:
2024-03-10 13:29:56 +01:00
if ((n = recvmsg(sock, &msg, 0)) == -1) {
if (errno == EAGAIN) {
struct pollfd pfd = {.fd = sock, .events = POLLIN};
poll(&pfd, 1, -1);
2024-03-10 13:37:27 +01:00
goto again;
2024-03-10 13:29:56 +01:00
}
2024-03-10 13:15:42 +01:00
err(1, "child: recvmsg");
2024-03-10 13:29:56 +01:00
}
2024-03-10 13:15:42 +01:00
if (n == 0)
errx(0, "child: done!");
if ((msg.msg_flags & MSG_TRUNC) ||
(msg.msg_flags & MSG_CTRUNC))
errx(1, "child: control message truncated");
for (cmsg = CMSG_FIRSTHDR(&msg); cmsg;
cmsg = CMSG_NXTHDR(&msg, cmsg)) {
if (cmsg->cmsg_len == CMSG_LEN(sizeof(int)) &&
cmsg->cmsg_level == SOL_SOCKET &&
cmsg->cmsg_type == SCM_RIGHTS) {
fd = *(int *)CMSG_DATA(cmsg);
2024-03-10 13:24:50 +01:00
if (fstat(fd, &sb) == -1)
err(1, "child: fstat %d", fd);
if (close(fd) == -1)
err(1, "child: close %d", fd);
2024-03-10 13:15:42 +01:00
}
}
}
}
2024-03-10 13:29:56 +01:00
void
mark_nonblock(int fd)
{
int flags;
if ((flags = fcntl(fd, F_GETFL)) == -1)
err(1, "fcntl(F_GETFL)");
if (fcntl(fd, F_SETFL, flags | O_NONBLOCK) == -1)
err(1, "fcntl(F_SETFL)");
}
2024-03-10 13:15:42 +01:00
int
main(void)
{
2024-03-10 13:37:27 +01:00
int p[2], status, i;
2024-03-10 13:15:42 +01:00
pid_t pid;
if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, p) == -1)
err(1, "socketpair");
2024-03-10 13:29:56 +01:00
mark_nonblock(p[0]);
mark_nonblock(p[1]);
2024-03-10 13:15:42 +01:00
if ((pid = fork()) == -1)
err(1, "fork");
if (pid == 0) {
close(p[0]);
return child(p[1]);
}
2024-03-10 13:37:27 +01:00
/* prepare the buffer */
for (i = 0; i < sizeof(buf); ++i)
buf[i] = i % 16;
2024-03-10 13:15:42 +01:00
close(p[1]);
2024-03-10 13:24:50 +01:00
parent(p[0]);
warnx("parent: done");
close(p[0]);
waitpid(pid, &status, 0);
return (0);
2024-03-10 13:15:42 +01:00
}