Instead pass O_CLOEXEC or SOCK_CLOEXEC to open, socket, etc.
This requires a fairly modern system.
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;
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
}
if (ioctl(fd, BIOCSETF, &pf) == -1)
goto eexit;
- if (set_cloexec(fd) == -1)
- goto eexit;
return fd;
eexit:
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)
{
# 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);
#include <sys/un.h>
#include <errno.h>
+#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
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;
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);
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;
if (bind(s, (struct sockaddr *)&sin, sizeof(sin)) == -1)
goto eexit;
- set_cloexec(s);
if (ifp)
state->udp_fd = s;
else
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;
&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;
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 {
syslog(LOG_ERR, "flock `%s': %m", pidfile);
goto exit_failure;
}
- if (set_cloexec(pidfd) == -1)
- goto exit_failure;
writepid(pidfd, getpid());
}
}
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
{
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;
}
} 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;
&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)
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));
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;