X-Git-Url: http://git.madism.org/?a=blobdiff_plain;f=daemon.c;h=64f9c68936aca297cc5dd29b969fe6a9a7734874;hb=6a966ebc32006b64216715314ce21a506ef1f7c4;hp=ec8be6590a36f25062ad655a57a175a385065e71;hpb=c0cfcd4c414bd423e4766ff6c90850ee25c9b449;p=apps%2Fpfixtools.git diff --git a/daemon.c b/daemon.c index ec8be65..64f9c68 100644 --- a/daemon.c +++ b/daemon.c @@ -1,5 +1,5 @@ /******************************************************************************/ -/* postlicyd: a postfix policy daemon with a lot of features */ +/* pfixtools: a collection of postfix related tools */ /* ~~~~~~~~~ */ /* ________________________________________________________________________ */ /* */ @@ -34,10 +34,29 @@ */ #include +#include -#include "postlicyd.h" +#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; +}