X-Git-Url: http://git.madism.org/?a=blobdiff_plain;f=common%2Fcommon.c;h=e9e4f2e6b54d67e87d7bb87fdf62b561114a7bd8;hb=bcdb5e34f3c251407304e01eea26607e77dd8609;hp=1abb148c6f6ff3b3c9034f184c277a30a872f90a;hpb=146b8bb68c6280fbf701337f689f8d84b7ff1ff6;p=apps%2Fpfixtools.git diff --git a/common/common.c b/common/common.c index 1abb148..e9e4f2e 100644 --- a/common/common.c +++ b/common/common.c @@ -46,13 +46,14 @@ sig_atomic_t sighup = false; bool daemon_process = true; int log_level = LOG_INFO; +bool log_syslog = false; static FILE *pidfile = NULL; void common_sighandler(int sig) { switch (sig) { - case SIGTERM: + case SIGTERM: case SIGINT: sigint = true; return; @@ -62,7 +63,7 @@ void common_sighandler(int sig) return; default: - err("Killed (got signal %d)...", sig); + err("Killed (got signal %d)...", sig); exit(-1); } } @@ -199,11 +200,11 @@ int daemon_detach(void) pid = fork(); if (pid < 0) { return -1; - } + } if (pid) { - daemon_process = false; + daemon_process = false; exit(0); - } + } setsid(); return 0; @@ -240,7 +241,7 @@ int pidfile_open(const char *name) pidfile = fopen(name, "w"); if (!pidfile) return -1; - fprintf(pidfile, "%d\n", getpid()); + fprintf(pidfile, "%d\n", getpid()); return fflush(pidfile); } return 0; @@ -260,9 +261,11 @@ int pidfile_refresh(void) static void pidfile_close(void) { if (pidfile) { - rewind(pidfile); - ftruncate(fileno(pidfile), 0); - fclose(pidfile); + if (daemon_process) { + rewind(pidfile); + ftruncate(fileno(pidfile), 0); + } + fclose(pidfile); pidfile = NULL; } } @@ -270,6 +273,11 @@ 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) { + crit("unable to write pidfile %s", pidfilename); + return EXIT_FAILURE; + } + if (!unsafe && drop_privileges(runas_user, runas_group) < 0) { crit("unable to drop privileges"); return EXIT_FAILURE; @@ -280,11 +288,6 @@ int common_setup(const char* pidfilename, bool unsafe, const char* runas_user, return EXIT_FAILURE; } - if (pidfile_open(pidfilename) < 0) { - crit("unable to write pidfile %s", pidfilename); - return EXIT_FAILURE; - } - pidfile_refresh(); return EXIT_SUCCESS; } @@ -294,10 +297,10 @@ extern exitcall_t __madexit[]; static void common_shutdown(void) { - if (daemon_process) { - info("stopping..."); - pidfile_close(); - } + if (daemon_process) { + info("stopping..."); + } + pidfile_close(); for (int i = -1; __madexit[i]; i--) { (*__madexit[i])(); }