X-Git-Url: http://git.madism.org/?a=blobdiff_plain;f=postlicyd.c;h=f6f3ccc830b861205d8ffbb4c9a5ceaec63a21b7;hb=628163e5113bd3bcce92dc51395998a2fdd8ad9d;hp=c20973e547574395217f652101bb28c70d25830b;hpb=59c1f51ef38081d87885a37873545154c7a23e2d;p=apps%2Fpfixtools.git diff --git a/postlicyd.c b/postlicyd.c index c20973e..f6f3ccc 100644 --- a/postlicyd.c +++ b/postlicyd.c @@ -30,32 +30,91 @@ /******************************************************************************/ /* - * Copyright © 2006 Pierre Habouzit + * Copyright © 2006-2007 Pierre Habouzit */ -#include -#include -#include -#include +#include +#include +#include -#include "job.h" -#include "gai.h" +#include "postlicyd.h" -bool cleanexit = false; +static sig_atomic_t cleanexit = false; +static sig_atomic_t sigint = false; +static volatile int nbthreads = 0; + +static void main_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; + } + return; + + case SIGTERM: + break; + + default: + return; + } + + syslog(LOG_ERR, "Killed..."); + exit(-1); +} static void main_initialize(void) { openlog("postlicyd", LOG_PID, LOG_MAIL); - gai_initialize(); - job_initialize(); + signal(SIGPIPE, SIG_IGN); + signal(SIGINT, &main_sighandler); + signal(SIGTERM, &main_sighandler); syslog(LOG_INFO, "Starting..."); } +void *job_run(void *_fd) +{ + int fd = (intptr_t)_fd; + + close(fd); + return NULL; +} + +static void main_loop(void) +{ + int sock = -1; + + while (!sigint) { + int fd = accept(sock, NULL, 0); + pthread_attr_t attr; + pthread_t dummy; + + if (fd < 0) { + if (errno != EINTR || errno != EAGAIN) + UNIXERR("accept"); + continue; + } + + pthread_attr_init(&attr); + pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); + pthread_create(&dummy, &attr, job_run, (void *)(intptr_t)fd); + pthread_attr_destroy(&attr); + } + + cleanexit = true; + close(sock); +} + static void main_shutdown(void) { syslog(LOG_INFO, cleanexit ? "Stopping..." : "Unclean exit..."); - job_shutdown(); - gai_shutdown(); closelog(); } @@ -63,14 +122,11 @@ int main(void) { if (atexit(main_shutdown)) { fputs("Cannot hook my atexit function, quitting !\n", stderr); - return EX_CONFIG; + return EXIT_FAILURE; } main_initialize(); - - job_loop(); - - cleanexit = true; + main_loop(); main_shutdown(); - return 0; + return EXIT_SUCCESS; }