From 9a86ce6ea990ac72337bffb78f663f8a904a408f Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Tue, 29 Apr 2014 16:17:58 +0200 Subject: Fold fastd_open_pipe into fastd_async_init, simpify fastd_setfl and fastd_setfd and move to fastd.h --- src/async.c | 16 ++++++++++++---- src/fastd.c | 36 +----------------------------------- src/fastd.h | 24 ++++++++++++++++++++---- src/random.c | 1 - src/socket.c | 6 ++---- src/tuntap.c | 1 - 6 files changed, 35 insertions(+), 49 deletions(-) (limited to 'src') diff --git a/src/async.c b/src/async.c index ed8370f..2343cbb 100644 --- a/src/async.c +++ b/src/async.c @@ -27,8 +27,6 @@ #include "async.h" #include "fastd.h" -#include - typedef struct fastd_async_hdr { fastd_async_type_t type; @@ -37,8 +35,18 @@ typedef struct fastd_async_hdr { void fastd_async_init(void) { - fastd_open_pipe(&ctx.async_rfd, &ctx.async_wfd); - fastd_setfl(ctx.async_wfd, O_NONBLOCK, 0); + 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)) + exit_errno("socketpair"); + + fastd_setfd(fds[0], FD_CLOEXEC); + fastd_setfd(fds[1], FD_CLOEXEC); + fastd_setfl(fds[1], O_NONBLOCK); + + ctx.async_rfd = fds[0]; + ctx.async_wfd = fds[1]; } static void handle_resolve_return(const fastd_async_resolve_return_t *resolve_return) { diff --git a/src/fastd.c b/src/fastd.c index 9f1f60b..523889a 100644 --- a/src/fastd.c +++ b/src/fastd.c @@ -34,7 +34,6 @@ #include "poll.h" #include -#include #include #include #include @@ -121,20 +120,6 @@ static void init_signals(void) { } -void fastd_open_pipe(int *readfd, int *writefd) { - int pipefd[2]; - - /* use socketpair with SOCK_DGRAM instead of pipe2 with O_DIRECT to keep this portable */ - if (socketpair(AF_UNIX, SOCK_DGRAM, 0, pipefd)) - exit_errno("socketpair"); - - fastd_setfd(pipefd[0], FD_CLOEXEC, 0); - fastd_setfd(pipefd[1], FD_CLOEXEC, 0); - - *readfd = pipefd[0]; - *writefd = pipefd[1]; -} - static void init_log(void) { uid_t uid = geteuid(); gid_t gid = getegid(); @@ -202,25 +187,6 @@ static void init_sockets(void) { ctx.n_socks = conf.n_bind_addrs; } - -void fastd_setfd(const int fd, int set, int unset) { - int flags = fcntl(fd, F_GETFD); - if (flags < 0) - exit_errno("Getting file descriptor flags failed: fcntl"); - - if (fcntl(fd, F_SETFD, (flags|set) & (~unset)) < 0) - exit_errno("Setting file descriptor flags failed: fcntl"); -} - -void fastd_setfl(const int fd, int set, int unset) { - int flags = fcntl(fd, F_GETFL); - if (flags < 0) - exit_errno("Getting file status flags failed: fcntl"); - - if (fcntl(fd, F_SETFL, (flags|set) & (~unset)) < 0) - exit_errno("Setting file status flags failed: fcntl"); -} - static void close_sockets(void) { size_t i; for (i = 0; i < ctx.n_socks; i++) @@ -544,7 +510,7 @@ static int daemonize(void) { } else { /* child 2 */ - fastd_setfd(pipefd[1], FD_CLOEXEC, 0); + fastd_setfd(pipefd[1], FD_CLOEXEC); return pipefd[1]; } } diff --git a/src/fastd.h b/src/fastd.h index 6666d70..73c7229 100644 --- a/src/fastd.h +++ b/src/fastd.h @@ -34,6 +34,7 @@ #include "vector.h" #include +#include #include #include #include @@ -310,10 +311,6 @@ fastd_socket_t* fastd_socket_open(fastd_peer_t *peer, int af); void fastd_socket_close(fastd_socket_t *sock); void fastd_socket_error(fastd_socket_t *sock); -void fastd_open_pipe(int *readfd, int *writefd); -void fastd_setfd(const int fd, int set, int unset); -void fastd_setfl(const int fd, int set, int unset); - void fastd_resolve_peer(fastd_peer_t *peer, fastd_remote_t *remote); void fastd_tuntap_open(void); @@ -332,6 +329,25 @@ static inline int fastd_rand(int min, int max) { } +static inline void fastd_setfd(const int fd, int set) { + int flags = fcntl(fd, F_GETFD); + if (flags < 0) + exit_errno("Getting file descriptor flags failed: fcntl"); + + if (fcntl(fd, F_SETFD, flags|set) < 0) + exit_errno("Setting file descriptor flags failed: fcntl"); +} + +static inline void fastd_setfl(const int fd, int set) { + 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) + exit_errno("Setting file status flags failed: fcntl"); +} + + #define container_of(ptr, type, member) ({ \ const __typeof__(((type *)0)->member) *_mptr = (ptr); \ (type*)((char*)_mptr - offsetof(type, member)); \ diff --git a/src/random.c b/src/random.c index ce7183b..d8c06d2 100644 --- a/src/random.c +++ b/src/random.c @@ -26,7 +26,6 @@ #include "fastd.h" -#include #include diff --git a/src/socket.c b/src/socket.c index 2a8611d..0751a3c 100644 --- a/src/socket.c +++ b/src/socket.c @@ -27,8 +27,6 @@ #include "fastd.h" #include "poll.h" -#include - static int bind_socket(const fastd_bind_address_t *addr, bool warn) { int fd = -1; @@ -58,8 +56,8 @@ static int bind_socket(const fastd_bind_address_t *addr, bool warn) { if (fd < 0) goto error; - fastd_setfd(fd, FD_CLOEXEC, 0); - fastd_setfl(fd, O_NONBLOCK, 0); + fastd_setfd(fd, FD_CLOEXEC); + fastd_setfl(fd, O_NONBLOCK); int one = 1; diff --git a/src/tuntap.c b/src/tuntap.c index e5c0471..d9fd353 100644 --- a/src/tuntap.c +++ b/src/tuntap.c @@ -27,7 +27,6 @@ #include "fastd.h" #include "poll.h" -#include #include #include -- cgit v1.2.3