From 0d0a857c696b653a65c709bbfb11bdaff67369b8 Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Wed, 30 Apr 2014 03:32:36 +0200 Subject: Use SOCK_NONBLOCK where available --- src/async.c | 7 +++++-- src/compat.h | 7 +++++++ src/fastd.h | 4 ++-- src/socket.c | 8 +++++--- 4 files changed, 19 insertions(+), 7 deletions(-) diff --git a/src/async.c b/src/async.c index 28e0b03..21a6930 100644 --- a/src/async.c +++ b/src/async.c @@ -38,10 +38,13 @@ void fastd_async_init(void) { int fds[2]; /* use socketpair with SOCK_DGRAM instead of pipe2 with O_DIRECT to keep this portable */ - if (socketpair(AF_UNIX, SOCK_DGRAM, 0, fds)) + if (socketpair(AF_UNIX, SOCK_DGRAM|SOCK_NONBLOCK, 0, fds)) exit_errno("socketpair"); - fastd_setfl(fds[1], O_NONBLOCK); +#ifdef NO_HAVE_SOCK_NONBLOCK + fastd_setnonblock(fds[0]); + fastd_setnonblock(fds[1]); +#endif ctx.async_rfd = fds[0]; ctx.async_wfd = fds[1]; diff --git a/src/compat.h b/src/compat.h index ea3f109..e3d9e49 100644 --- a/src/compat.h +++ b/src/compat.h @@ -60,6 +60,13 @@ struct ethhdr { #define IP_FREEBIND 15 #endif + +#ifndef SOCK_NONBLOCK +#define NO_HAVE_SOCK_NONBLOCK +#define SOCK_NONBLOCK 0 +#endif + + #ifndef HAVE_GET_CURRENT_DIR_NAME #if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) diff --git a/src/fastd.h b/src/fastd.h index b5dbe0c..3bb4689 100644 --- a/src/fastd.h +++ b/src/fastd.h @@ -331,12 +331,12 @@ static inline int fastd_rand(int min, int max) { } -static inline void fastd_setfl(const int fd, int set) { +static inline void fastd_setnonblock(int fd) { int flags = fcntl(fd, F_GETFL); if (flags < 0) exit_errno("Getting file status flags failed: fcntl"); - if (fcntl(fd, F_SETFL, flags|set) < 0) + if (fcntl(fd, F_SETFL, flags|O_NONBLOCK) < 0) exit_errno("Setting file status flags failed: fcntl"); } diff --git a/src/socket.c b/src/socket.c index d49888c..340967c 100644 --- a/src/socket.c +++ b/src/socket.c @@ -33,7 +33,7 @@ static int bind_socket(const fastd_bind_address_t *addr, bool warn) { int af = AF_UNSPEC; if (addr->addr.sa.sa_family != AF_INET) { - fd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_UDP); + fd = socket(PF_INET6, SOCK_DGRAM|SOCK_NONBLOCK, IPPROTO_UDP); if (fd >= 0) { af = AF_INET6; @@ -46,7 +46,7 @@ static int bind_socket(const fastd_bind_address_t *addr, bool warn) { } } if (fd < 0 && addr->addr.sa.sa_family != AF_INET6) { - fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP); + fd = socket(PF_INET, SOCK_DGRAM|SOCK_NONBLOCK, IPPROTO_UDP); if (fd < 0) exit_errno("unable to create socket"); else @@ -56,7 +56,9 @@ static int bind_socket(const fastd_bind_address_t *addr, bool warn) { if (fd < 0) goto error; - fastd_setfl(fd, O_NONBLOCK); +#ifdef NO_HAVE_SOCK_NONBLOCK + fastd_setnonblock(fd); +#endif int one = 1; -- cgit v1.2.3