From: Pierre Habouzit Date: Wed, 29 Aug 2007 17:33:44 +0000 (+0200) Subject: Ahem, use non blocking IO. silly me. X-Git-Url: http://git.madism.org/?p=apps%2Fpfixtools.git;a=commitdiff_plain;h=90f03cb500b9d269c49291a257eb9472fd6c8fad Ahem, use non blocking IO. silly me. Signed-off-by: Pierre Habouzit --- diff --git a/daemon.c b/daemon.c index d3dc00d..1d40424 100644 --- a/daemon.c +++ b/daemon.c @@ -34,10 +34,29 @@ */ #include +#include #include "common.h" #include "daemon.h" +static int setnonblock(int sock) +{ + int res = fcntl(sock, F_GETFL); + + if (res < 0) { + UNIXERR("fcntl"); + return -1; + } + + if (fcntl(sock, F_SETFL, res | O_NONBLOCK) < 0) { + UNIXERR("fcntl"); + return -1; + } + + return 0; +} + + int tcp_listen(const struct sockaddr *addr, socklen_t len) { int sock; @@ -78,6 +97,11 @@ int tcp_listen(const struct sockaddr *addr, socklen_t len) return -1; } + if (setnonblock(sock)) { + close(sock); + return -1; + } + if (listen(sock, 0) < 0) { UNIXERR("bind"); close(sock); @@ -87,3 +111,19 @@ int tcp_listen(const struct sockaddr *addr, socklen_t len) return sock; } +int accept_nonblock(int fd) +{ + int sock = accept(fd, NULL, 0); + + if (sock < 0) { + UNIXERR("accept"); + return -1; + } + + if (setnonblock(sock)) { + close(sock); + return -1; + } + + return sock; +} diff --git a/daemon.h b/daemon.h index 5e3b30f..155ee06 100644 --- a/daemon.h +++ b/daemon.h @@ -37,5 +37,6 @@ #define POSTLICYD_DAEMON_H int tcp_listen(const struct sockaddr *addr, socklen_t len); +int accept_nonblock(int fd); #endif diff --git a/srsd.c b/srsd.c index b79be36..26280de 100644 --- a/srsd.c +++ b/srsd.c @@ -262,7 +262,7 @@ int main_loop(srs_t *srs, const char *domain, int port_enc, int port_dec) srsd_t *tmp; int sock; - sock = accept(srsd->fd, NULL, NULL); + sock = accept_nonblock(srsd->fd); if (sock < 0) { UNIXERR("accept"); continue;