X-Git-Url: http://git.madism.org/?a=blobdiff_plain;f=common.c;h=3688c9d9e758422864c2fcd4f0387e5faf53dde9;hb=6d692d51a497e480ac4b0f4f63e6ce2539419e36;hp=3addd0e3d0e6cec7f8656aec9cbbccb1f3f220cb;hpb=93ab2b3dc7658be50317765792a8720c54b29807;p=apps%2Fpfixtools.git diff --git a/common.c b/common.c index 3addd0e..3688c9d 100644 --- a/common.c +++ b/common.c @@ -31,6 +31,7 @@ /* * Copyright © 2007 Pierre Habouzit + * Copyright © 2008 Florent Bruneau */ #include @@ -47,18 +48,9 @@ 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: @@ -268,10 +260,34 @@ static void pidfile_close(void) } } -extern initcall_t __madinit[], __madexit[]; +int common_setup(const char* pidfilename, bool unsafe, const char* runas_user, + const char* runas_group, bool daemonize) +{ + if (pidfile_open(pidfilename) < 0) { + syslog(LOG_CRIT, "unable to write pidfile %s", pidfilename); + return EXIT_FAILURE; + } + + 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--) {