From: Florent Bruneau Date: Thu, 2 Oct 2008 21:32:26 +0000 (+0200) Subject: Cleanup daemon startup. X-Git-Url: http://git.madism.org/?p=apps%2Fpfixtools.git;a=commitdiff_plain;h=44d04c83a53af19faecb1620cf0f9ed53054da5c Cleanup daemon startup. Signed-off-by: Florent Bruneau --- diff --git a/Makefile b/Makefile index e7f631d..e9e3917 100644 --- a/Makefile +++ b/Makefile @@ -66,6 +66,7 @@ $(INSTALL_TARGETS): install-%: % install-dir install-dir: install -d $(DESTDIR)$(prefix)/sbin install -d $(DESTDIR)/etc/pfixtools + install example/postlicyd.conf $(DESTDIR)/etc/pfixtools/postlicyd.example.conf .PHONY: clean distclean install install-dir $(SUBDIRS) $(CLEAN_TARGETS) \ $(DISTCLEAN_TARGETS) $(INSTALL_TARGETS) diff --git a/common/common.c b/common/common.c index 758e869..c2abeb4 100644 --- a/common/common.c +++ b/common/common.c @@ -44,6 +44,8 @@ sig_atomic_t sigint = false; sig_atomic_t sighup = false; +bool daemon_process = true; + static FILE *pidfile = NULL; void common_sighandler(int sig) @@ -193,10 +195,13 @@ int daemon_detach(void) open("/dev/null", O_RDWR); pid = fork(); - if (pid < 0) + if (pid < 0) { return -1; - if (pid) + } + if (pid) { + daemon_process = false; exit(0); + } setsid(); return 0; @@ -276,11 +281,6 @@ 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; @@ -291,6 +291,11 @@ int common_setup(const char* pidfilename, bool unsafe, const char* runas_user, return EXIT_FAILURE; } + if (pidfile_open(pidfilename) < 0) { + syslog(LOG_CRIT, "unable to write pidfile %s", pidfilename); + return EXIT_FAILURE; + } + pidfile_refresh(); return EXIT_SUCCESS; } @@ -300,9 +305,10 @@ extern exitcall_t __madexit[]; static void common_shutdown(void) { - syslog(LOG_INFO, "Stopping..."); - pidfile_close(); - + if (daemon_process) { + syslog(LOG_INFO, "Stopping..."); + } + pidfile_close(); for (int i = -1; __madexit[i]; i--) { (*__madexit[i])(); } diff --git a/postlicyd/main-postlicyd.c b/postlicyd/main-postlicyd.c index cae2d23..60ecb3a 100644 --- a/postlicyd/main-postlicyd.c +++ b/postlicyd/main-postlicyd.c @@ -183,6 +183,11 @@ int main(int argc, char *argv[]) return EXIT_FAILURE; } + if (drop_privileges(RUNAS_USER, RUNAS_GROUP) < 0) { + syslog(LOG_CRIT, "unable to drop privileges"); + return EXIT_FAILURE; + } + config_t *config = config_read(argv[optind]); if (config == NULL) { return EXIT_FAILURE; @@ -191,13 +196,11 @@ int main(int argc, char *argv[]) config->port = port; } - if (common_setup(pidfile, false, RUNAS_USER, RUNAS_GROUP, - daemonize) != EXIT_SUCCESS + if (common_setup(pidfile, true, NULL, NULL, daemonize) != EXIT_SUCCESS || start_listener(config->port) < 0) { config_delete(&config); return EXIT_FAILURE; - } - { + } else { int res = server_loop(query_starter, (delete_client_t)query_delete, policy_run, config_refresh, config); config_delete(&config);