From e5c24dfd010732ab6b80a96ad2a5414499c496c0 Mon Sep 17 00:00:00 2001 From: Eric Andersen Date: Thu, 29 Mar 2001 21:58:33 +0000 Subject: Use daemon() to spawn syslogd and klogd daemons. -Erik --- klogd.c | 21 ++++----------------- sysklogd/klogd.c | 21 ++++----------------- sysklogd/syslogd.c | 22 ++++------------------ syslogd.c | 22 ++++------------------ 4 files changed, 16 insertions(+), 70 deletions(-) diff --git a/klogd.c b/klogd.c index d5e330b..95d4eea 100644 --- a/klogd.c +++ b/klogd.c @@ -124,18 +124,10 @@ static void doKlogd (void) } } -static void daemon_init (char **argv, char *dz, void fn (void)) -{ - setsid(); /* start a new session? */ - strncpy(argv[0], dz, strlen(argv[0])); - fn(); - exit(0); -} - extern int klogd_main(int argc, char **argv) { /* no options, no getopt */ - int opt, pid; + int opt; int doFork = TRUE; /* do normal option parsing */ @@ -150,15 +142,10 @@ extern int klogd_main(int argc, char **argv) } if (doFork == TRUE) { - pid = fork(); - if (pid < 0) - exit(pid); - else if (pid == 0) { - daemon_init (argv, "klogd", doKlogd); - } - } else { - doKlogd(); + if (daemon(0, 1) < 0) + perror_msg_and_die("daemon"); } + doKlogd(); return EXIT_SUCCESS; } diff --git a/sysklogd/klogd.c b/sysklogd/klogd.c index d5e330b..95d4eea 100644 --- a/sysklogd/klogd.c +++ b/sysklogd/klogd.c @@ -124,18 +124,10 @@ static void doKlogd (void) } } -static void daemon_init (char **argv, char *dz, void fn (void)) -{ - setsid(); /* start a new session? */ - strncpy(argv[0], dz, strlen(argv[0])); - fn(); - exit(0); -} - extern int klogd_main(int argc, char **argv) { /* no options, no getopt */ - int opt, pid; + int opt; int doFork = TRUE; /* do normal option parsing */ @@ -150,15 +142,10 @@ extern int klogd_main(int argc, char **argv) } if (doFork == TRUE) { - pid = fork(); - if (pid < 0) - exit(pid); - else if (pid == 0) { - daemon_init (argv, "klogd", doKlogd); - } - } else { - doKlogd(); + if (daemon(0, 1) < 0) + perror_msg_and_die("daemon"); } + doKlogd(); return EXIT_SUCCESS; } diff --git a/sysklogd/syslogd.c b/sysklogd/syslogd.c index cc42c29..e83ce48 100644 --- a/sysklogd/syslogd.c +++ b/sysklogd/syslogd.c @@ -562,18 +562,9 @@ static void doSyslogd (void) } /* for main loop */ } -static void daemon_init (char **argv, char *dz, void fn (void)) -{ - setsid(); - chdir ("/"); - strncpy(argv[0], dz, strlen(argv[0])); - fn(); - exit(0); -} - extern int syslogd_main(int argc, char **argv) { - int opt, pid; + int opt; int doFork = TRUE; char *p; @@ -635,15 +626,10 @@ extern int syslogd_main(int argc, char **argv) #endif if (doFork == TRUE) { - pid = fork(); - if (pid < 0) - exit(pid); - else if (pid == 0) { - daemon_init (argv, "syslogd", doSyslogd); - } - } else { - doSyslogd(); + if (daemon(0, 1) < 0) + perror_msg_and_die("daemon"); } + doSyslogd(); return EXIT_SUCCESS; } diff --git a/syslogd.c b/syslogd.c index cc42c29..e83ce48 100644 --- a/syslogd.c +++ b/syslogd.c @@ -562,18 +562,9 @@ static void doSyslogd (void) } /* for main loop */ } -static void daemon_init (char **argv, char *dz, void fn (void)) -{ - setsid(); - chdir ("/"); - strncpy(argv[0], dz, strlen(argv[0])); - fn(); - exit(0); -} - extern int syslogd_main(int argc, char **argv) { - int opt, pid; + int opt; int doFork = TRUE; char *p; @@ -635,15 +626,10 @@ extern int syslogd_main(int argc, char **argv) #endif if (doFork == TRUE) { - pid = fork(); - if (pid < 0) - exit(pid); - else if (pid == 0) { - daemon_init (argv, "syslogd", doSyslogd); - } - } else { - doSyslogd(); + if (daemon(0, 1) < 0) + perror_msg_and_die("daemon"); } + doSyslogd(); return EXIT_SUCCESS; } -- cgit v1.1