From: Roy Marples Date: Fri, 7 Feb 2014 17:32:08 +0000 (+0000) Subject: Remove custom set_cloexec and set_nonblock functions. X-Git-Tag: v6.3.0~34 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=cc0502023ee717d78b78e76a3d8ad41657c9b362;p=thirdparty%2Fdhcpcd.git Remove custom set_cloexec and set_nonblock functions. Instead pass O_CLOEXEC or SOCK_CLOEXEC to open, socket, etc. This requires a fairly modern system. --- diff --git a/bpf.c b/bpf.c index 59d17d92..96e07ce6 100644 --- 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: diff --git a/common.c b/common.c index b0d03678..27100205 100644 --- 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) { diff --git a/common.h b/common.h index 398d8733..2cea3151 100644 --- 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); diff --git a/control.c b/control.c index dffcf871..645e54fb 100644 --- a/control.c +++ b/control.c @@ -29,6 +29,7 @@ #include #include +#include #include #include #include @@ -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 0c3cc054..cb8e2f74 100644 --- 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 bf009193..623e2db8 100644 --- 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; diff --git a/dhcpcd.c b/dhcpcd.c index 288831bc..669f359f 100644 --- 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()); } } diff --git a/if-bsd.c b/if-bsd.c index 320e94ef..adc7bf59 100644 --- 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 diff --git a/if-linux.c b/if-linux.c index fe69fc95..632e89d9 100644 --- a/if-linux.c +++ b/if-linux.c @@ -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; } diff --git a/ipv6nd.c b/ipv6nd.c index d801357d..43f7645c 100644 --- 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 de5cc957..4f4a27e7 100644 --- 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;