From 471a5502bf16dab3a13c8c14469582897f43ee7e Mon Sep 17 00:00:00 2001 From: Eric Leblond Date: Sat, 20 Jul 2013 12:08:33 +0200 Subject: ulogd: use daemon() function MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This patches update the daemonization code. It is done earlier and it uses the daemon(à function which is used for daemonization by most projects. Signed-off-by: Eric Leblond --- src/ulogd.c | 30 +++++++++++++----------------- 1 file changed, 13 insertions(+), 17 deletions(-) diff --git a/src/ulogd.c b/src/ulogd.c index c1aba77..2c0fbd1 100644 --- a/src/ulogd.c +++ b/src/ulogd.c @@ -1399,6 +1399,19 @@ int main(int argc, char* argv[]) " with daemon mode).\n"); } + if (daemonize){ + if (daemon(0, 0) < 0) { + ulogd_log(ULOGD_FATAL, "can't daemonize: %s (%d)", + errno, strerror(errno)); + warn_and_exit(daemonize); + } + } + + if (ulogd_pidfile) { + if (write_pidfile(daemonize) < 0) + warn_and_exit(0); + } + if (config_register_file(ulogd_configfile)) { ulogd_log(ULOGD_FATAL, "error registering configfile \"%s\"\n", ulogd_configfile); @@ -1450,23 +1463,6 @@ int main(int argc, char* argv[]) } } - - if (daemonize){ - if (fork()) { - exit(0); - } - if (logfile != stdout) - fclose(stdout); - fclose(stderr); - fclose(stdin); - setsid(); - } - - if (ulogd_pidfile) { - if (write_pidfile(daemonize) < 0) - warn_and_exit(0); - } - signal(SIGTERM, &sigterm_handler); signal(SIGINT, &sigterm_handler); signal(SIGHUP, &signal_handler); -- cgit v1.2.3