make regress/netcat.c fd passing (more) portable

This commit is contained in:
Damien Miller 2015-02-26 20:33:22 -08:00
parent 9e1cfca7e1
commit 1ad3a77cc9
1 changed files with 25 additions and 21 deletions

View File

@ -1014,43 +1014,44 @@ fillbuf(int fd, unsigned char *buf, size_t *bufpos)
void void
fdpass(int nfd) fdpass(int nfd)
{ {
struct msghdr mh; #if defined(HAVE_SENDMSG) && (defined(HAVE_ACCRIGHTS_IN_MSGHDR) || defined(HAVE_CONTROL_IN_MSGHDR))
struct msghdr msg;
#ifndef HAVE_ACCRIGHTS_IN_MSGHDR
union { union {
struct cmsghdr hdr; struct cmsghdr hdr;
char buf[CMSG_SPACE(sizeof(int))]; char buf[CMSG_SPACE(sizeof(int))];
} cmsgbuf; } cmsgbuf;
struct cmsghdr *cmsg; struct cmsghdr *cmsg;
struct iovec iov; #endif
char c = '\0'; struct iovec vec;
ssize_t r; char ch = '\0';
struct pollfd pfd; struct pollfd pfd;
ssize_t r;
/* Avoid obvious stupidity */ memset(&msg, 0, sizeof(msg));
if (isatty(STDOUT_FILENO)) #ifdef HAVE_ACCRIGHTS_IN_MSGHDR
errx(1, "Cannot pass file descriptor to tty"); msg.msg_accrights = (caddr_t)&nfd;
msg.msg_accrightslen = sizeof(nfd);
bzero(&mh, sizeof(mh)); #else
bzero(&cmsgbuf, sizeof(cmsgbuf)); memset(&cmsgbuf, 0, sizeof(cmsgbuf));
bzero(&iov, sizeof(iov)); msg.msg_control = (caddr_t)&cmsgbuf.buf;
bzero(&pfd, sizeof(pfd)); msg.msg_controllen = sizeof(cmsgbuf.buf);
cmsg = CMSG_FIRSTHDR(&msg);
mh.msg_control = (caddr_t)&cmsgbuf.buf;
mh.msg_controllen = sizeof(cmsgbuf.buf);
cmsg = CMSG_FIRSTHDR(&mh);
cmsg->cmsg_len = CMSG_LEN(sizeof(int)); cmsg->cmsg_len = CMSG_LEN(sizeof(int));
cmsg->cmsg_level = SOL_SOCKET; cmsg->cmsg_level = SOL_SOCKET;
cmsg->cmsg_type = SCM_RIGHTS; cmsg->cmsg_type = SCM_RIGHTS;
*(int *)CMSG_DATA(cmsg) = nfd; *(int *)CMSG_DATA(cmsg) = nfd;
#endif
iov.iov_base = &c; vec.iov_base = &ch;
iov.iov_len = 1; vec.iov_len = 1;
mh.msg_iov = &iov; msg.msg_iov = &vec;
mh.msg_iovlen = 1; msg.msg_iovlen = 1;
bzero(&pfd, sizeof(pfd)); bzero(&pfd, sizeof(pfd));
pfd.fd = STDOUT_FILENO; pfd.fd = STDOUT_FILENO;
for (;;) { for (;;) {
r = sendmsg(STDOUT_FILENO, &mh, 0); r = sendmsg(STDOUT_FILENO, &msg, 0);
if (r == -1) { if (r == -1) {
if (errno == EAGAIN || errno == EINTR) { if (errno == EAGAIN || errno == EINTR) {
pfd.events = POLLOUT; pfd.events = POLLOUT;
@ -1065,6 +1066,9 @@ fdpass(int nfd)
break; break;
} }
exit(0); exit(0);
#else
errx(1, "%s: file descriptor passing not supported", __func__);
#endif
} }
/* Deal with RFC 854 WILL/WONT DO/DONT negotiation. */ /* Deal with RFC 854 WILL/WONT DO/DONT negotiation. */