X-Git-Url: http://git.madism.org/?a=blobdiff_plain;f=common.c;h=d00461b827761c2d526fb9f4cd075b1b6a2319eb;hb=75e665838fab6bd7f579d8f2802b60435af1c783;hp=80746ae554b80faef68821938315044211b022a0;hpb=dbc6818c72dfe618b164db970773490b19b808c5;p=apps%2Fpfixtools.git diff --git a/common.c b/common.c index 80746ae..d00461b 100644 --- a/common.c +++ b/common.c @@ -40,23 +40,16 @@ #include "common.h" -sig_atomic_t sigint = false; -sig_atomic_t sighup = false; +sig_atomic_t sigint = false; +sig_atomic_t sighup = false; + +static FILE *pidfile = NULL; void common_sighandler(int sig) { - static time_t lastintr = 0; - time_t now = time(NULL); - switch (sig) { case SIGINT: - if (sigint) { - if (now - lastintr >= 1) - break; - } else { - lastintr = now; - sigint = true; - } + sigint = true; return; case SIGHUP: @@ -86,7 +79,7 @@ static int setnonblock(int sock) return 0; } -int tcp_listen_nonblock(const struct sockaddr *addr, socklen_t len) +int tcp_bind(const struct sockaddr *addr, socklen_t len) { int sock; @@ -126,17 +119,32 @@ int tcp_listen_nonblock(const struct sockaddr *addr, socklen_t len) return -1; } - if (setnonblock(sock)) { + return sock; +} + +int tcp_listen(const struct sockaddr *addr, socklen_t len) +{ + int sock = tcp_bind(addr, len); + if (listen(sock, 0) < 0) { + UNIXERR("bind"); close(sock); return -1; } + return sock; +} +int tcp_listen_nonblock(const struct sockaddr *addr, socklen_t len) +{ + int sock = tcp_bind(addr, len); + if (setnonblock(sock)) { + close(sock); + return -1; + } if (listen(sock, 0) < 0) { UNIXERR("bind"); close(sock); return -1; } - return sock; } @@ -157,6 +165,20 @@ int accept_nonblock(int fd) return sock; } +int xwrite(int fd, const char *s, size_t l) +{ + while (l > 0) { + int nb = write(fd, s, l); + if (nb < 0) { + if (errno == EINTR || errno == EAGAIN) + continue; + return -1; + } + l -= nb; + } + return 0; +} + int daemon_detach(void) { pid_t pid; @@ -204,9 +226,52 @@ int drop_privileges(const char *user, const char *group) return 0; } +int pidfile_open(const char *name) +{ + if (name) { + pidfile = fopen(name, "w"); + if (!pidfile) + return -1; + fprintf(pidfile, "%d\n", getpid()); + return fflush(pidfile); + } + return 0; +} + +int pidfile_refresh(void) +{ + if (pidfile) { + rewind(pidfile); + ftruncate(fileno(pidfile), 0); + fprintf(pidfile, "%d\n", getpid()); + return fflush(pidfile); + } + return 0; +} + +static void pidfile_close(void) +{ + if (pidfile) { + rewind(pidfile); + ftruncate(fileno(pidfile), 0); + fclose(pidfile); + pidfile = NULL; + } +} + extern initcall_t __madinit[], __madexit[]; -void common_initialize(void) +static void common_shutdown(void) +{ + pidfile_close(); + + for (int i = -1; __madexit[i]; i--) { + (*__madexit[i])(); + } +} + +static void __attribute__((__constructor__,__used__)) +common_initialize(void) { if (atexit(common_shutdown)) { fputs("Cannot hook my atexit function, quitting !\n", stderr); @@ -220,9 +285,3 @@ void common_initialize(void) } } -void common_shutdown(void) -{ - for (int i = -1; __madexit[i]; i--) { - (*__madexit[i])(); - } -}