X-Git-Url: http://git.madism.org/?a=blobdiff_plain;f=common.c;h=3688c9d9e758422864c2fcd4f0387e5faf53dde9;hb=8edd7234e1c30a16d914292dc652046ee581fa5b;hp=0d7671d9640dc692e719b76dcbb383de6c13323b;hpb=c693c0b0d26eac89aef4e4c6740feca3627e2d26;p=apps%2Fpfixtools.git diff --git a/common.c b/common.c index 0d7671d..3688c9d 100644 --- a/common.c +++ b/common.c @@ -31,6 +31,7 @@ /* * Copyright © 2007 Pierre Habouzit + * Copyright © 2008 Florent Bruneau */ #include @@ -40,23 +41,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 +80,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 +120,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 +166,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,22 +227,86 @@ int drop_privileges(const char *user, const char *group) return 0; } -void common_initialize(void) +int pidfile_open(const char *name) { - extern initcall_t __madinit_start, __madinit_end; + 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; +} - initcall_t *call_p = &__madinit_start; - while (call_p < &__madinit_end) { - (*call_p++)(); +static void pidfile_close(void) +{ + if (pidfile) { + rewind(pidfile); + ftruncate(fileno(pidfile), 0); + fclose(pidfile); + pidfile = NULL; } } -void common_shutdown(void) +int common_setup(const char* pidfilename, bool unsafe, const char* runas_user, + const char* runas_group, bool daemonize) { - extern exitcall_t __madexit_start, __madexit_end; + if (pidfile_open(pidfilename) < 0) { + syslog(LOG_CRIT, "unable to write pidfile %s", pidfilename); + return EXIT_FAILURE; + } - exitcall_t *call_p = &__madexit_end; - while (call_p > &__madexit_start) { - (*--call_p)(); + if (!unsafe && drop_privileges(runas_user, runas_group) < 0) { + syslog(LOG_CRIT, "unable to drop privileges"); + return EXIT_FAILURE; } + + if (daemonize && daemon_detach() < 0) { + syslog(LOG_CRIT, "unable to fork"); + return EXIT_FAILURE; + } + + pidfile_refresh(); + return EXIT_SUCCESS; } + +extern initcall_t __madinit[]; +extern exitcall_t __madexit[]; + +static void common_shutdown(void) +{ + syslog(LOG_INFO, "Stopping..."); + 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); + abort(); + } + + for (int i = 0; __madinit[i]; i++) { + if ((*__madinit[i])()) { + exit(EXIT_FAILURE); + } + } +} +