X-Git-Url: http://git.madism.org/?a=blobdiff_plain;f=postlicyd.c;h=bd887e45a1ba9e6888545de8859f87e62d59c0d5;hb=79ff3ad0aaa667dc9ce658460632177c8ffcf39e;hp=d980d20f97e07a011665c08db2c640535e1a1d9b;hpb=9a4efa4f0dc893f243ee69d1b20f024666ca943d;p=apps%2Fpfixtools.git diff --git a/postlicyd.c b/postlicyd.c index d980d20..bd887e4 100644 --- a/postlicyd.c +++ b/postlicyd.c @@ -30,10 +30,99 @@ /******************************************************************************/ /* - * Copyright © 2006 Pierre Habouzit + * Copyright © 2006-2007 Pierre Habouzit */ +#include +#include +#include + +#include "postlicyd.h" + +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); + 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) +{ + while (!sigint) { + int fd = accept(-1, 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); + } +} + +static void main_shutdown(void) +{ + syslog(LOG_INFO, cleanexit ? "Stopping..." : "Unclean exit..."); + closelog(); +} + int main(void) { - return 0; + if (atexit(main_shutdown)) { + fputs("Cannot hook my atexit function, quitting !\n", stderr); + return EXIT_FAILURE; + } + + main_initialize(); + main_loop(); + cleanexit = true; + main_shutdown(); + return EXIT_SUCCESS; }