Start using SOCK_NONBLOCK and SOCK_CLOEXEC in ntpd as well.
Make sure we handle EAGAIN and EINTR on the recvmsg and sendto calls.

OK?
-- 
:wq Claudio

Index: client.c
===================================================================
RCS file: /cvs/src/usr.sbin/ntpd/client.c,v
retrieving revision 1.98
diff -u -p -r1.98 client.c
--- client.c    19 Jan 2015 20:47:03 -0000      1.98
+++ client.c    10 Feb 2015 03:55:54 -0000
@@ -138,7 +138,8 @@ client_query(struct ntp_peer *p)
        if (p->query->fd == -1) {
                struct sockaddr *sa = (struct sockaddr *)&p->addr->ss;
 
-               if ((p->query->fd = socket(p->addr->ss.ss_family, SOCK_DGRAM,
+               if ((p->query->fd = socket(p->addr->ss.ss_family,
+                   SOCK_DGRAM | SOCK_NONBLOCK | SOCK_CLOEXEC,
                    0)) == -1)
                        fatal("client_query socket");
 
@@ -225,7 +226,9 @@ client_dispatch(struct ntp_peer *p, u_in
 
        T4 = getoffset();
        if ((size = recvmsg(p->query->fd, &somsg, 0)) == -1) {
-               if (errno == EHOSTUNREACH || errno == EHOSTDOWN ||
+               if (errno == EAGAIN || errno == EINTR)
+                       return (0);
+               else if (errno == EHOSTUNREACH || errno == EHOSTDOWN ||
                    errno == ENETUNREACH || errno == ENETDOWN ||
                    errno == ECONNREFUSED || errno == EADDRNOTAVAIL ||
                    errno == ENOPROTOOPT || errno == ENOENT) {
Index: control.c
===================================================================
RCS file: /cvs/src/usr.sbin/ntpd/control.c,v
retrieving revision 1.4
diff -u -p -r1.4 control.c
--- control.c   9 Jan 2015 07:35:37 -0000       1.4
+++ control.c   10 Feb 2015 03:17:52 -0000
@@ -40,7 +40,8 @@ control_init(char *path)
        int                      fd;
        mode_t                   old_umask;
 
-       if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
+       if ((fd = socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC | SOCK_NONBLOCK,
+           0)) == -1) {
                log_warn("control_init: socket");
                return (-1);
        }
@@ -74,8 +75,6 @@ control_init(char *path)
                return (-1);
        }
 
-       session_socket_blockmode(fd, BM_NONBLOCK);
-
        return (fd);
 }
 
@@ -112,15 +111,14 @@ control_accept(int listenfd)
        struct ctl_conn         *ctl_conn;
 
        len = sizeof(sa);
-       if ((connfd = accept(listenfd,
-           (struct sockaddr *)&sa, &len)) == -1) {
+       if ((connfd = accept4(listenfd,
+           (struct sockaddr *)&sa, &len,
+           SOCK_CLOEXEC | SOCK_NONBLOCK)) == -1) {
                if (errno != EWOULDBLOCK && errno != EINTR)
                        log_warn("control_accept: accept");
                return (0);
        }
 
-       session_socket_blockmode(connfd, BM_NONBLOCK);
-
        if ((ctl_conn = calloc(1, sizeof(struct ctl_conn))) == NULL) {
                log_warn("control_accept");
                close(connfd);
@@ -267,23 +265,6 @@ control_dispatch_msg(struct pollfd *pfd,
                imsg_free(&imsg);
        }
        return (0);
-}
-
-void
-session_socket_blockmode(int fd, enum blockmodes bm)
-{
-       int     flags;
-
-       if ((flags = fcntl(fd, F_GETFL, 0)) == -1)
-               fatal("fcntl F_GETFL");
-
-       if (bm == BM_NONBLOCK)
-               flags |= O_NONBLOCK;
-       else
-               flags &= ~O_NONBLOCK;
-
-       if ((flags = fcntl(fd, F_SETFL, flags)) == -1)
-               fatal("fcntl F_SETFL");
 }
 
 void
Index: ntp_msg.c
===================================================================
RCS file: /cvs/src/usr.sbin/ntpd/ntp_msg.c,v
retrieving revision 1.20
diff -u -p -r1.20 ntp_msg.c
--- ntp_msg.c   9 Jan 2015 07:35:37 -0000       1.20
+++ ntp_msg.c   10 Feb 2015 03:55:08 -0000
@@ -53,7 +53,8 @@ ntp_sendmsg(int fd, struct sockaddr *sa,
        n = sendto(fd, msg, sizeof(*msg), 0, sa, sa_len);
        if (n == -1) {
                if (errno == ENOBUFS || errno == EHOSTUNREACH ||
-                   errno == ENETDOWN || errno == EHOSTDOWN) {
+                   errno == ENETDOWN || errno == EHOSTDOWN ||
+                   errno == EAGAIN || errno == EINTR) {
                        /* logging is futile */
                        return (-1);
                }
Index: ntpd.h
===================================================================
RCS file: /cvs/src/usr.sbin/ntpd/ntpd.h,v
retrieving revision 1.117
diff -u -p -r1.117 ntpd.h
--- ntpd.h      13 Jan 2015 02:28:56 -0000      1.117
+++ ntpd.h      10 Feb 2015 03:15:56 -0000
@@ -227,11 +227,6 @@ struct ctl_show_sensor {
        double           correction;
 };
 
-enum blockmodes {
-       BM_NORMAL,
-       BM_NONBLOCK
-};
-
 struct ctl_conn {
        TAILQ_ENTRY(ctl_conn)   entry;
        struct imsgbuf          ibuf;
@@ -334,7 +329,6 @@ int                  control_accept(int);
 struct ctl_conn                *control_connbyfd(int);
 int                     control_close(int);
 int                     control_dispatch_msg(struct pollfd *, u_int *);
-void                    session_socket_blockmode(int, enum blockmodes);
 void                    build_show_status(struct ctl_show_status *);
 void                    build_show_peer(struct ctl_show_peer *,
                             struct ntp_peer *);
Index: server.c
===================================================================
RCS file: /cvs/src/usr.sbin/ntpd/server.c,v
retrieving revision 1.40
diff -u -p -r1.40 server.c
--- server.c    9 Jan 2015 07:35:37 -0000       1.40
+++ server.c    10 Feb 2015 03:50:46 -0000
@@ -59,7 +59,8 @@ setup_listeners(struct servent *se, stru
                                strlcpy(ifr.ifr_name, ifap->ifa_name,
                                    sizeof(ifr.ifr_name));
 
-                               fd = socket(AF_INET, SOCK_DGRAM, 0);
+                               fd = socket(AF_INET, SOCK_DGRAM |
+                                   SOCK_CLOEXEC | SOCK_NONBLOCK, 0);
                                if (ioctl(fd, SIOCGIFRDOMAIN,
                                    (caddr_t)&ifr) == -1)
                                        rdomain = 0;
@@ -170,7 +171,9 @@ server_dispatch(int fd, struct ntpd_conf
        fsa_len = sizeof(fsa);
        if ((size = recvfrom(fd, &buf, sizeof(buf), 0,
            (struct sockaddr *)&fsa, &fsa_len)) == -1) {
-               if (errno == EHOSTUNREACH || errno == EHOSTDOWN ||
+               if (errno == EAGAIN || errno == EINTR)
+                       return (0);
+               else if (errno == EHOSTUNREACH || errno == EHOSTDOWN ||
                    errno == ENETUNREACH || errno == ENETDOWN) {
                        log_warn("recvfrom %s",
                            log_sockaddr((struct sockaddr *)&fsa));

Reply via email to