]> git.ipfire.org Git - thirdparty/dhcpcd.git/commitdiff
Remove custom set_cloexec and set_nonblock functions.
authorRoy Marples <roy@marples.name>
Fri, 7 Feb 2014 17:32:08 +0000 (17:32 +0000)
committerRoy Marples <roy@marples.name>
Fri, 7 Feb 2014 17:32:08 +0000 (17:32 +0000)
Instead pass O_CLOEXEC or SOCK_CLOEXEC to open, socket, etc.
This requires a fairly modern system.

bpf.c
common.c
common.h
control.c
dhcp.c
dhcp6.c
dhcpcd.c
if-bsd.c
if-linux.c
ipv6nd.c
lpf.c

diff --git a/bpf.c b/bpf.c
index 59d17d92a973624a422504f512ceef8a73e410f3..96e07ce68dc5d48c19287b5f1c054372e7e5c65d 100644 (file)
--- a/bpf.c
+++ b/bpf.c
@@ -62,7 +62,7 @@ ipv4_opensocket(struct interface *ifp, int protocol)
        int flags;
 #endif
 #ifdef _PATH_BPF
-       fd = open(_PATH_BPF, O_RDWR | O_NONBLOCK);
+       fd = open(_PATH_BPF, O_RDWR | O_CLOEXEC | O_NONBLOCK);
 #else
        char *device;
        int n = 0;
@@ -72,7 +72,7 @@ ipv4_opensocket(struct interface *ifp, int protocol)
                return -1;
        do {
                snprintf(device, PATH_MAX, "/dev/bpf%d", n++);
-               fd = open(device, O_RDWR | O_NONBLOCK);
+               fd = open(device, O_RDWR | O_CLOEXEC | O_NONBLOCK);
        } while (fd == -1 && errno == EBUSY);
        free(device);
 #endif
@@ -123,8 +123,6 @@ ipv4_opensocket(struct interface *ifp, int protocol)
        }
        if (ioctl(fd, BIOCSETF, &pf) == -1)
                goto eexit;
-       if (set_cloexec(fd) == -1)
-               goto eexit;
        return fd;
 
 eexit:
index b0d03678ac185333922152f2fef92c3b0a304471..2710020557dcc7551e4cd8f9236420033b118582 100644 (file)
--- a/common.c
+++ b/common.c
@@ -96,34 +96,6 @@ get_line_free(void)
        lbuf_len = 0;
 }
 
-int
-set_cloexec(int fd)
-{
-       int flags;
-
-       if ((flags = fcntl(fd, F_GETFD, 0)) == -1 ||
-           fcntl(fd, F_SETFD, flags | FD_CLOEXEC) == -1)
-       {
-               syslog(LOG_ERR, "fcntl: %m");
-               return -1;
-       }
-       return 0;
-}
-
-int
-set_nonblock(int fd)
-{
-       int flags;
-
-       if ((flags = fcntl(fd, F_GETFL, 0)) == -1 ||
-           fcntl(fd, F_SETFL, flags | O_NONBLOCK) == -1)
-       {
-               syslog(LOG_ERR, "fcntl: %m");
-               return -1;
-       }
-       return 0;
-}
-
 const char *
 get_hostname(int short_hostname)
 {
index 398d873390d8689f0a42fdfae2093639840685c6..2cea3151da06f323f73c19a57cd7a6dc4326d871 100644 (file)
--- a/common.h
+++ b/common.h
@@ -99,8 +99,6 @@
 # endif
 #endif
 
-int set_cloexec(int);
-int set_nonblock(int);
 char *get_line(FILE * __restrict);
 void get_line_free(void);
 const char *get_hostname(int);
index dffcf8716e771a88d2df31d06b15a743dde94490..645e54fb9007329ef38a14eb9d67dd50126296fc 100644 (file)
--- a/control.c
+++ b/control.c
@@ -29,6 +29,7 @@
 #include <sys/un.h>
 
 #include <errno.h>
+#include <fcntl.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
@@ -106,28 +107,35 @@ control_handle(void *arg)
        struct sockaddr_un run;
        socklen_t len;
        struct fd_list *l;
-       int f;
+       int fd, flags;
 
        ctx = arg;
        len = sizeof(run);
-       if ((f = accept(ctx->fd, (struct sockaddr *)&run, &len)) == -1)
+       if ((fd = accept(ctx->fd, (struct sockaddr *)&run, &len)) == -1)
                return;
-       set_cloexec(f);
+       if ((flags = fcntl(fd, F_GETFD, 0)) == -1 ||
+           fcntl(fd, F_SETFD, flags | FD_CLOEXEC) == -1)
+       {
+               close(fd);
+               return;
+       }
        l = malloc(sizeof(*l));
        if (l) {
-               l->fd = f;
+               l->fd = fd;
                l->listener = 0;
                l->next = control_fds;
                control_fds = l;
                eloop_event_add(l->fd, control_handle_data, l);
-       }
+       } else
+               close(fd);
 }
 
 static int
 make_sock(struct control_ctx *ctx, struct sockaddr_un *sun)
 {
 
-       if ((ctx->fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
+       if ((ctx->fd = socket(AF_UNIX,
+           SOCK_STREAM | SOCK_CLOEXEC | SOCK_NONBLOCK, 0)) == -1)
                return -1;
        memset(sun, 0, sizeof(*sun));
        sun->sun_family = AF_UNIX;
@@ -147,8 +155,6 @@ control_start(struct control_ctx *ctx)
        if (bind(ctx->fd, (struct sockaddr *)&sun, len) == -1 ||
            chmod(CONTROLSOCKET,
                S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP) == -1 ||
-           set_cloexec(ctx->fd) == -1 ||
-           set_nonblock(ctx->fd) == -1 ||
            listen(ctx->fd, sizeof(control_fds)) == -1)
        {
                close(ctx->fd);
diff --git a/dhcp.c b/dhcp.c
index 0c3cc0541851f48e49e035e251409beaf879a9c2..cb8e2f7472dc5ea3a935bef85ebad26aa996eaab 100644 (file)
--- a/dhcp.c
+++ b/dhcp.c
@@ -1358,7 +1358,7 @@ dhcp_openudp(struct interface *ifp)
        char *p;
 #endif
 
-       if ((s = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1)
+       if ((s = socket(PF_INET, SOCK_DGRAM | SOCK_CLOEXEC, IPPROTO_UDP)) == -1)
                return -1;
 
        n = 1;
@@ -1388,7 +1388,6 @@ dhcp_openudp(struct interface *ifp)
        if (bind(s, (struct sockaddr *)&sin, sizeof(sin)) == -1)
                goto eexit;
 
-       set_cloexec(s);
        if (ifp)
                state->udp_fd = s;
        else
diff --git a/dhcp6.c b/dhcp6.c
index bf00919336c84f9ada1b1e42c8cf873c10201bad..623e2db86c3e7785af0fc8ce33d3e549b569e14f 100644 (file)
--- a/dhcp6.c
+++ b/dhcp6.c
@@ -2486,7 +2486,8 @@ dhcp6_open(void)
        sa.sin6_len = sizeof(sa);
 #endif
 
-       sock = socket(PF_INET6, SOCK_DGRAM, IPPROTO_UDP);
+       sock = socket(PF_INET6, SOCK_DGRAM | SOCK_CLOEXEC | SOCK_NONBLOCK,
+           IPPROTO_UDP);
        if (sock == -1)
                return -1;
 
@@ -2515,9 +2516,6 @@ dhcp6_open(void)
            &n, sizeof(n)) == -1)
                goto errexit;
 
-       if (set_cloexec(sock) == -1 || set_nonblock(sock) == -1)
-               goto errexit;
-
        eloop_event_add(sock, dhcp6_handledata, NULL);
 
        return 0;
index 288831bc24f40af933fffa18865c89b50e137e64..669f359f31b324b2b9f37f53f42dc6b5a9052b1a 100644 (file)
--- a/dhcpcd.c
+++ b/dhcpcd.c
@@ -1284,7 +1284,9 @@ main(int argc, char **argv)
                if (mkdir(DBDIR, 0755) == -1 && errno != EEXIST)
                        syslog(LOG_ERR, "mkdir `%s': %m", DBDIR);
 
-               pidfd = open(pidfile, O_WRONLY | O_CREAT | O_NONBLOCK, 0664);
+               pidfd = open(pidfile,
+                   O_WRONLY | O_CREAT | O_NONBLOCK | O_CLOEXEC,
+                   0664);
                if (pidfd == -1)
                        syslog(LOG_ERR, "open `%s': %m", pidfile);
                else {
@@ -1294,8 +1296,6 @@ main(int argc, char **argv)
                                syslog(LOG_ERR, "flock `%s': %m", pidfile);
                                goto exit_failure;
                        }
-                       if (set_cloexec(pidfd) == -1)
-                               goto exit_failure;
                        writepid(pidfd, getpid());
                }
        }
index 320e94efb824308b1e8ba059f64873e8ec11885f..adc7bf5922f0f0867176c5767ea8d87122fb4efc 100644 (file)
--- a/if-bsd.c
+++ b/if-bsd.c
@@ -101,14 +101,8 @@ if_conf(__unused struct interface *iface)
 int
 open_link_socket(void)
 {
-       int fd;
 
-       fd = socket(PF_ROUTE, SOCK_RAW, 0);
-       if (fd != -1) {
-               set_cloexec(fd);
-               set_nonblock(fd);
-       }
-       return fd;
+       return socket(PF_ROUTE, SOCK_RAW | SOCK_CLOEXEC | SOCK_NONBLOCK, 0);
 }
 
 int
index fe69fc956acd07551f57102cb2676e62cc4d8ce2..632e89d9daf252a4be4525ec271c5867980f9b8e 100644 (file)
@@ -122,13 +122,12 @@ _open_link_socket(struct sockaddr_nl *nl)
 {
        int fd;
 
-       if ((fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE)) == -1)
+       fd = socket(AF_NETLINK, SOCK_RAW | SOCK_CLOEXEC, NETLINK_ROUTE);
+       if (fd == -1)
                return -1;
        nl->nl_family = AF_NETLINK;
        if (bind(fd, (struct sockaddr *)nl, sizeof(*nl)) == -1)
                return -1;
-       set_cloexec(fd);
-
        return fd;
 }
 
index d801357d4c48c6e8fed349793f511a2be8e19711..43f7645cd66833ab3b637c74ea79c53e2ba536c9 100644 (file)
--- a/ipv6nd.c
+++ b/ipv6nd.c
@@ -192,7 +192,8 @@ ipv6nd_open(void)
        } su;
 #endif
 
-       sock = socket(AF_INET6, SOCK_RAW, IPPROTO_ICMPV6);
+       sock = socket(AF_INET6, SOCK_RAW | SOCK_CLOEXEC | SOCK_NONBLOCK,
+           IPPROTO_ICMPV6);
        if (sock == -1)
                return -1;
 
@@ -219,8 +220,6 @@ ipv6nd_open(void)
                &filt, sizeof(filt)) == -1)
                goto eexit;
 
-       set_cloexec(sock);
-
        len = CMSG_SPACE(sizeof(struct in6_pktinfo)) + CMSG_SPACE(sizeof(int));
        sndbuf = calloc(1, len);
        if (sndbuf == NULL)
diff --git a/lpf.c b/lpf.c
index de5cc957c7622e2f7ac0957995efc404138e1e19..4f4a27e7da522a5abe4e70e2e9aac2b5452e9ed9 100644 (file)
--- a/lpf.c
+++ b/lpf.c
@@ -80,7 +80,8 @@ ipv4_opensocket(struct interface *ifp, int protocol)
        int n;
 #endif
 
-       if ((s = socket(PF_PACKET, SOCK_DGRAM, htons(protocol))) == -1)
+       if ((s = socket(PF_PACKET, SOCK_DGRAM | SOCK_CLOEXEC | SOCK_NONBLOCK,
+           htons(protocol))) == -1)
                return -1;
 
        memset(&su, 0, sizeof(su));
@@ -105,10 +106,6 @@ ipv4_opensocket(struct interface *ifp, int protocol)
                        goto eexit;
        }
 #endif
-       if (set_cloexec(s) == -1)
-               goto eexit;
-       if (set_nonblock(s) == -1)
-               goto eexit;
        if (bind(s, &su.sa, sizeof(su)) == -1)
                goto eexit;
        return s;