make_sock(struct dhcpcd_ctx *ctx, struct sockaddr_un *sun)
{
+#ifdef SOCK_CLOEXEC
if ((ctx->control_fd = socket(AF_UNIX,
SOCK_STREAM | SOCK_CLOEXEC | SOCK_NONBLOCK, 0)) == -1)
return -1;
+#else
+ int flags;
+
+ if ((ctx->control_fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
+ return -1;
+ if ((flags = fcntl(ctx->control_fd, F_GETFD, 0)) == -1 ||
+ fcntl(ctx->control_fd, F_SETFD, flags | FD_CLOEXEC) == -1)
+ {
+ close(ctx->control_fd);
+ ctx->control_fd = -1;
+ return -1;
+ }
+ if ((flags = fcntl(ctx->control_fd, F_GETFL, 0)) == -1 ||
+ fcntl(ctx->control_fd, F_SETFL, flags | O_NONBLOCK) == -1)
+ {
+ close(ctx->control_fd);
+ ctx->control_fd = -1;
+ return -1;
+ }
+#endif
memset(sun, 0, sizeof(*sun));
sun->sun_family = AF_UNIX;
strlcpy(sun->sun_path, CONTROLSOCKET, sizeof(sun->sun_path));
char *p;
#endif
+#ifdef SOCK_CLOEXEC
if ((s = socket(PF_INET, SOCK_DGRAM | SOCK_CLOEXEC, IPPROTO_UDP)) == -1)
return -1;
+#else
+ if ((s = socket(PF_INET6, SOCK_DGRAM, IPPROTO_UDP)) == -1)
+ return -1;
+ if ((n = fcntl(s, F_GETFD, 0)) == -1 ||
+ fcntl(s, F_SETFD, n | FD_CLOEXEC) == -1)
+ {
+ close(s);
+ return -1;
+ }
+#endif
n = 1;
if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &n, sizeof(n)) == -1)
#endif
ctx = dctx->ipv6;
+#ifdef SOCK_CLOEXEC
ctx->dhcp_fd = socket(PF_INET6,
SOCK_DGRAM | SOCK_CLOEXEC | SOCK_NONBLOCK,
IPPROTO_UDP);
if (ctx->dhcp_fd == -1)
return -1;
+#else
+ if ((ctx->dhcp_fd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_UDP)) == -1)
+ return -1;
+ if ((n = fcntl(ctx->dhcp_fd, F_GETFD, 0)) == -1 ||
+ fcntl(ctx->dhcp_fd, F_SETFD, n | FD_CLOEXEC) == -1)
+ {
+ close(ctx->dhcp_fd);
+ ctx->dhcp_fd = -1;
+ return -1;
+ }
+ if ((n = fcntl(ctx->dhcp_fd, F_GETFL, 0)) == -1 ||
+ fcntl(ctx->dhcp_fd, F_SETFL, n | O_NONBLOCK) == -1)
+ {
+ close(ctx->dhcp_fd);
+ ctx->dhcp_fd = -1;
+ return -1;
+ }
+#endif
n = 1;
if (setsockopt(ctx->dhcp_fd, SOL_SOCKET, SO_REUSEADDR,
#endif
#include <errno.h>
+#include <fcntl.h>
#include <fnmatch.h>
#include <stddef.h>
#include <stdio.h>
open_link_socket(void)
{
+#ifdef SOCK_CLOEXEC
return socket(PF_ROUTE, SOCK_RAW | SOCK_CLOEXEC | SOCK_NONBLOCK, 0);
+#else
+ int s, flags;
+
+ if ((s = socket(PF_ROUTE, SOCK_RAW, 0)) == -1)
+ return -1;
+ if ((flags = fcntl(s, F_GETFD, 0)) == -1 ||
+ fcntl(s, F_SETFD, flags | FD_CLOEXEC) == -1)
+ {
+ close(s);
+ return -1;
+ }
+ if ((flags = fcntl(s, F_GETFL, 0)) == -1 ||
+ fcntl(s, F_SETFL, flags | O_NONBLOCK) == -1)
+ {
+ close(s);
+ return -1;
+ }
+#endif
}
int
#endif
#include <errno.h>
+#include <fcntl.h>
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
ctx = dctx->ipv6;
if (ctx->nd_fd != -1)
goto unspec;
+#ifdef SOCK_CLOEXEC
ctx->nd_fd = socket(AF_INET6, SOCK_RAW | SOCK_CLOEXEC | SOCK_NONBLOCK,
IPPROTO_ICMPV6);
if (ctx->nd_fd == -1)
return -1;
+#else
+ if ((ctx->nd_fd = socket(AF_INET6, SOCK_RAW, IPPROTO_ICMPV6)) == -1)
+ return -1;
+ if ((on = fcntl(ctx->nd_fd, F_GETFD, 0)) == -1 ||
+ fcntl(ctx->nd_fd, F_SETFD, on | FD_CLOEXEC) == -1)
+ {
+ close(ctx->nd_fd);
+ ctx->nd_fd = -1;
+ return -1;
+ }
+ if ((on = fcntl(ctx->nd_fd, F_GETFL, 0)) == -1 ||
+ fcntl(ctx->nd_fd, F_SETFL, on | O_NONBLOCK) == -1)
+ {
+ close(ctx->nd_fd);
+ ctx->nd_fd = -1;
+ return -1;
+ }
+#endif
on = 1;
if (setsockopt(ctx->nd_fd, IPPROTO_IPV6, IPV6_RECVPKTINFO,
ICMP6_FILTER_SETBLOCKALL(&filt);
/* We send DAD requests from the unspecified address. */
- ctx->unspec_fd = socket(AF_INET6, SOCK_RAW, IPPROTO_ICMPV6);
+#ifdef SOCK_CLOEXEC
+ ctx->unspec_fd = socket(AF_INET6,
+ SOCK_RAW | SOCK_CLOEXEC | SOCK_NONBLOCK,
+ IPPROTO_ICMPV6);
if (ctx->unspec_fd == -1)
- goto eexit;
+ return -1;
+#else
+ if ((ctx->unspec_fd = socket(AF_INET6, SOCK_RAW, IPPROTO_ICMPV6)) == -1)
+ return -1;
+ if ((on = fcntl(ctx->unspec_fd, F_GETFD, 0)) == -1 ||
+ fcntl(ctx->unspec_fd, F_SETFD, on | FD_CLOEXEC) == -1)
+ {
+ close(ctx->unspec_fd);
+ ctx->unspec_fd = -1;
+ return -1;
+ }
+ if ((on = fcntl(ctx->unspec_fd, F_GETFL, 0)) == -1 ||
+ fcntl(ctx->unspec_fd, F_SETFL, on | O_NONBLOCK) == -1)
+ {
+ close(ctx->unspec_fd);
+ ctx->unspec_fd = -1;
+ return -1;
+ }
+#endif
+
if (setsockopt(ctx->unspec_fd, IPPROTO_ICMPV6, ICMP6_FILTER,
&filt, sizeof(filt)) == -1)
goto eexit;