X-Git-Url: http://git.madism.org/?a=blobdiff_plain;f=common.c;h=3688c9d9e758422864c2fcd4f0387e5faf53dde9;hb=881732b600190bc02b9dde47980b14382851c266;hp=59ac027654b396a8461e2dbd9ef028d1243e6a4c;hpb=9444dcb2ea8e2b24883d3f5740e28ca20d467ad2;p=apps%2Fpfixtools.git diff --git a/common.c b/common.c index 59ac027..3688c9d 100644 --- a/common.c +++ b/common.c @@ -31,6 +31,7 @@ /* * Copyright © 2007 Pierre Habouzit + * Copyright © 2008 Florent Bruneau */ #include @@ -259,11 +260,34 @@ static void pidfile_close(void) } } +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--) {