mirror of https://github.com/omar-polo/gmid.git
156 lines
3.0 KiB
C
Executable File
156 lines
3.0 KiB
C
Executable File
#include <sys/stat.h>
|
|
#include <sys/socket.h>
|
|
#include <sys/wait.h>
|
|
|
|
#include <err.h>
|
|
#include <errno.h>
|
|
#include <fcntl.h>
|
|
#include <poll.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <unistd.h>
|
|
|
|
char buf[1024];
|
|
|
|
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;
|
|
|
|
if ((fd = open("/dev/null", O_RDONLY)) == -1)
|
|
err(1, "parent: open /dev/null");
|
|
|
|
for (i = 0; i < 500000; ++i) {
|
|
iov[0].iov_base = buf;
|
|
iov[0].iov_len = sizeof(buf);
|
|
|
|
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;
|
|
|
|
again:
|
|
if (sendmsg(sock, &msg, 0) == -1) {
|
|
if (errno == EAGAIN) {
|
|
struct pollfd pfd = {.fd = sock, .events = POLLOUT};
|
|
poll(&pfd, 1, -1);
|
|
goto again;
|
|
}
|
|
err(1, "parent:sendmsg");
|
|
}
|
|
}
|
|
|
|
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;
|
|
struct stat sb;
|
|
|
|
for (;;) {
|
|
iov[0].iov_base = buf;
|
|
iov[0].iov_len = sizeof(buf);
|
|
|
|
memset(&msg, 0, sizeof(msg));
|
|
msg.msg_control = &cmsgbuf.buf;
|
|
msg.msg_controllen = sizeof(cmsgbuf.buf);
|
|
msg.msg_iov = iov;
|
|
msg.msg_iovlen = 1;
|
|
|
|
again:
|
|
if ((n = recvmsg(sock, &msg, 0)) == -1) {
|
|
if (errno == EAGAIN) {
|
|
struct pollfd pfd = {.fd = sock, .events = POLLIN};
|
|
poll(&pfd, 1, -1);
|
|
goto again;
|
|
}
|
|
err(1, "child: recvmsg");
|
|
}
|
|
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);
|
|
if (fstat(fd, &sb) == -1)
|
|
err(1, "child: fstat %d", fd);
|
|
if (close(fd) == -1)
|
|
err(1, "child: close %d", fd);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
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)");
|
|
}
|
|
|
|
int
|
|
main(void)
|
|
{
|
|
int p[2], status, i;
|
|
pid_t pid;
|
|
|
|
if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, p) == -1)
|
|
err(1, "socketpair");
|
|
|
|
mark_nonblock(p[0]);
|
|
mark_nonblock(p[1]);
|
|
|
|
if ((pid = fork()) == -1)
|
|
err(1, "fork");
|
|
if (pid == 0) {
|
|
close(p[0]);
|
|
return child(p[1]);
|
|
}
|
|
|
|
/* prepare the buffer */
|
|
for (i = 0; i < sizeof(buf); ++i)
|
|
buf[i] = i % 16;
|
|
|
|
close(p[1]);
|
|
parent(p[0]);
|
|
warnx("parent: done");
|
|
close(p[0]);
|
|
waitpid(pid, &status, 0);
|
|
return (0);
|
|
}
|