]> andersk Git - openssh.git/blobdiff - monitor_fdpass.c
- djm@cvs.openbsd.org 2010/01/30 02:54:53
[openssh.git] / monitor_fdpass.c
index a572302e89b1f48c44c461fd19b1d618cce1cfb2..7eb6f5c6e1f3ffe7a561a0bd48ef65d9deccadb0 100644 (file)
@@ -1,4 +1,4 @@
-/* $OpenBSD: monitor_fdpass.c,v 1.13 2007/09/04 03:21:03 djm Exp $ */
+/* $OpenBSD: monitor_fdpass.c,v 1.19 2010/01/12 00:58:25 djm Exp $ */
 /*
  * Copyright 2001 Niels Provos <provos@citi.umich.edu>
  * All rights reserved.
@@ -34,6 +34,9 @@
 #endif
 
 #include <errno.h>
+#ifdef HAVE_POLL_H
+#include <poll.h>
+#endif
 #include <string.h>
 #include <stdarg.h>
 
@@ -45,21 +48,25 @@ mm_send_fd(int sock, int fd)
 {
 #if defined(HAVE_SENDMSG) && (defined(HAVE_ACCRIGHTS_IN_MSGHDR) || defined(HAVE_CONTROL_IN_MSGHDR))
        struct msghdr msg;
-       struct iovec vec;
-       char ch = '\0';
-       ssize_t n;
 #ifndef HAVE_ACCRIGHTS_IN_MSGHDR
-       char tmp[CMSG_SPACE(sizeof(int))];
+       union {
+               struct cmsghdr hdr;
+               char buf[CMSG_SPACE(sizeof(int))];
+       } cmsgbuf;
        struct cmsghdr *cmsg;
 #endif
+       struct iovec vec;
+       char ch = '\0';
+       ssize_t n;
+       struct pollfd pfd;
 
        memset(&msg, 0, sizeof(msg));
 #ifdef HAVE_ACCRIGHTS_IN_MSGHDR
        msg.msg_accrights = (caddr_t)&fd;
        msg.msg_accrightslen = sizeof(fd);
 #else
-       msg.msg_control = (caddr_t)tmp;
-       msg.msg_controllen = CMSG_LEN(sizeof(int));
+       msg.msg_control = (caddr_t)&cmsgbuf.buf;
+       msg.msg_controllen = sizeof(cmsgbuf.buf);
        cmsg = CMSG_FIRSTHDR(&msg);
        cmsg->cmsg_len = CMSG_LEN(sizeof(int));
        cmsg->cmsg_level = SOL_SOCKET;
@@ -72,7 +79,14 @@ mm_send_fd(int sock, int fd)
        msg.msg_iov = &vec;
        msg.msg_iovlen = 1;
 
-       if ((n = sendmsg(sock, &msg, 0)) == -1) {
+       pfd.fd = sock;
+       pfd.events = POLLOUT;
+       while ((n = sendmsg(sock, &msg, 0)) == -1 &&
+           (errno == EAGAIN || errno == EINTR)) {
+               debug3("%s: sendmsg(%d): %s", __func__, fd, strerror(errno));
+               (void)poll(&pfd, 1, -1);
+       }
+       if (n == -1) {
                error("%s: sendmsg(%d): %s", __func__, fd,
                    strerror(errno));
                return -1;
@@ -95,14 +109,18 @@ mm_receive_fd(int sock)
 {
 #if defined(HAVE_RECVMSG) && (defined(HAVE_ACCRIGHTS_IN_MSGHDR) || defined(HAVE_CONTROL_IN_MSGHDR))
        struct msghdr msg;
+#ifndef HAVE_ACCRIGHTS_IN_MSGHDR
+       union {
+               struct cmsghdr hdr;
+               char buf[CMSG_SPACE(sizeof(int))];
+       } cmsgbuf;
+       struct cmsghdr *cmsg;
+#endif
        struct iovec vec;
        ssize_t n;
        char ch;
        int fd;
-#ifndef HAVE_ACCRIGHTS_IN_MSGHDR
-       char tmp[CMSG_SPACE(sizeof(int))];
-       struct cmsghdr *cmsg;
-#endif
+       struct pollfd pfd;
 
        memset(&msg, 0, sizeof(msg));
        vec.iov_base = &ch;
@@ -113,14 +131,22 @@ mm_receive_fd(int sock)
        msg.msg_accrights = (caddr_t)&fd;
        msg.msg_accrightslen = sizeof(fd);
 #else
-       msg.msg_control = tmp;
-       msg.msg_controllen = sizeof(tmp);
+       msg.msg_control = &cmsgbuf.buf;
+       msg.msg_controllen = sizeof(cmsgbuf.buf);
 #endif
 
-       if ((n = recvmsg(sock, &msg, 0)) == -1) {
+       pfd.fd = sock;
+       pfd.events = POLLIN;
+       while ((n = recvmsg(sock, &msg, 0)) == -1 &&
+           (errno == EAGAIN || errno == EINTR)) {
+               debug3("%s: recvmsg: %s", __func__, strerror(errno));
+               (void)poll(&pfd, 1, -1);
+       }
+       if (n == -1) {
                error("%s: recvmsg: %s", __func__, strerror(errno));
                return -1;
        }
+
        if (n != 1) {
                error("%s: recvmsg: expected received 1 got %ld",
                    __func__, (long)n);
@@ -138,6 +164,7 @@ mm_receive_fd(int sock)
                error("%s: no message header", __func__);
                return -1;
        }
+
 #ifndef BROKEN_CMSG_TYPE
        if (cmsg->cmsg_type != SCM_RIGHTS) {
                error("%s: expected type %d got %d", __func__,
This page took 0.04033 seconds and 4 git commands to generate.