]> git.ipfire.org Git - thirdparty/ulogd2.git/commitdiff
ulogd: use daemon() function
authorEric Leblond <eric@regit.org>
Sat, 20 Jul 2013 10:08:33 +0000 (12:08 +0200)
committerEric Leblond <eric@regit.org>
Thu, 10 Oct 2013 00:31:42 +0000 (02:31 +0200)
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 <eric@regit.org>
src/ulogd.c

index c1aba7721eb74ad4830942939b0da2d2f2fb7cda..2c0fbd1f53ac75f30ea4a95c50e2582c9719c679 100644 (file)
@@ -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);