From: Florent Bruneau Date: Sat, 4 Oct 2008 15:59:53 +0000 (+0200) Subject: Fix startup sequence. X-Git-Url: http://git.madism.org/?a=commitdiff_plain;h=146b8bb68c6280fbf701337f689f8d84b7ff1ff6;p=apps%2Fpfixtools.git Fix startup sequence. Signed-off-by: Florent Bruneau --- diff --git a/common/common.c b/common/common.c index af95efa..1abb148 100644 --- a/common/common.c +++ b/common/common.c @@ -236,19 +236,10 @@ int drop_privileges(const char *user, const char *group) int pidfile_open(const char *name) { - struct flock lock; - p_clear(&lock, 1); - lock.l_type = F_WRLCK; if (name) { pidfile = fopen(name, "w"); if (!pidfile) return -1; - if (fcntl(fileno(pidfile), F_SETLK, &lock) == -1) { - crit("program already started"); - fclose(pidfile); - pidfile = NULL; - return -1; - } fprintf(pidfile, "%d\n", getpid()); return fflush(pidfile); } @@ -268,13 +259,9 @@ int pidfile_refresh(void) static void pidfile_close(void) { - struct flock lock; - p_clear(&lock, 1); - lock.l_type = F_UNLCK; if (pidfile) { rewind(pidfile); ftruncate(fileno(pidfile), 0); - fcntl(fileno(pidfile), F_SETLK, &lock); fclose(pidfile); pidfile = NULL; } @@ -309,8 +296,8 @@ static void common_shutdown(void) { if (daemon_process) { info("stopping..."); + pidfile_close(); } - pidfile_close(); for (int i = -1; __madexit[i]; i--) { (*__madexit[i])(); } diff --git a/postlicyd/main-postlicyd.c b/postlicyd/main-postlicyd.c index 1f286eb..9ff1bc5 100644 --- a/postlicyd/main-postlicyd.c +++ b/postlicyd/main-postlicyd.c @@ -204,6 +204,11 @@ int main(int argc, char *argv[]) return EXIT_FAILURE; } + if (pidfile_open(pidfile) < 0) { + crit("unable to write pidfile %s", pidfile); + return EXIT_FAILURE; + } + if (drop_privileges(RUNAS_USER, RUNAS_GROUP) < 0) { crit("unable to drop privileges"); return EXIT_FAILURE; @@ -217,8 +222,14 @@ int main(int argc, char *argv[]) config->port = port; } - if (common_setup(pidfile, true, NULL, NULL, daemonize) != EXIT_SUCCESS - || start_listener(config->port) < 0) { + if (daemonize && daemon_detach() < 0) { + crit("unable to fork"); + return EXIT_FAILURE; + } + + pidfile_refresh(); + + if (start_listener(config->port) < 0) { return EXIT_FAILURE; } else { return server_loop(query_starter, (delete_client_t)query_delete,