diff options
author | Felix Janda <felix.janda@posteo.de> | 2013-08-10 20:18:18 +0200 |
---|---|---|
committer | Felix Janda <felix.janda@posteo.de> | 2013-08-10 20:18:18 +0200 |
commit | e49fe14705f78ba5a865ca4efd6ee53c78eeb253 (patch) | |
tree | 26b08f12af55b4ea4d08b30a2088e90ba04c6852 | |
parent | 35104f47f9d36a9afb43de8c863137fe76c9ff3f (diff) | |
download | toybox-e49fe14705f78ba5a865ca4efd6ee53c78eeb253.tar.gz |
Add daemonize function to lib for klogd and syslogd
-rw-r--r-- | lib/lib.h | 2 | ||||
-rw-r--r-- | lib/pending.c | 16 | ||||
-rw-r--r-- | toys/pending/klogd.c | 14 | ||||
-rw-r--r-- | toys/pending/syslogd.c | 24 |
4 files changed, 23 insertions, 33 deletions
@@ -202,3 +202,5 @@ unsigned long get_int_value(const char *numstr, unsigned lowrange, unsigned high // grep helper functions char *astrcat (char *, char *); char *xastrcat (char *, char *); + +void daemonize(void); diff --git a/lib/pending.c b/lib/pending.c index fad1c656..79b9c91c 100644 --- a/lib/pending.c +++ b/lib/pending.c @@ -102,3 +102,19 @@ char *xastrcat (char *x, char *y) { if (!x) error_exit ("xastrcat"); return x; } + +void daemonize(void) +{ + int fd = open("/dev/null", O_RDWR); + if (fd < 0) fd = xcreate("/", O_RDONLY, 0666); + + pid_t pid = fork(); + if (pid < 0) perror_exit("DAEMON: failed to fork"); + if (pid) exit(EXIT_SUCCESS); + + setsid(); + dup2(fd, 0); + dup2(fd, 1); + dup2(fd, 2); + if (fd > 2) close(fd); +} diff --git a/toys/pending/klogd.c b/toys/pending/klogd.c index 0b638294..da0e4de2 100644 --- a/toys/pending/klogd.c +++ b/toys/pending/klogd.c @@ -67,19 +67,7 @@ void klogd_main(void) sigatexit(handle_signal); if (toys.optflags & FLAG_c) set_log_level(TT.level); //set log level - if (!(toys.optflags & FLAG_n)) { //Make it daemon - pid_t pid; - int fd = open("/dev/null", O_RDWR); - if (fd < 0) fd = open("/", O_RDONLY, 0666); - if((pid = fork()) < 0) perror_exit("DAEMON: fail to fork"); - if (pid) exit(EXIT_SUCCESS); - - setsid(); - dup2(fd, 0); - dup2(fd, 1); - dup2(fd, 2); - if (fd > 2) close(fd); - } + if (!(toys.optflags & FLAG_n)) daemonize(); //Make it daemon if (CFG_KLOGD_SOURCE_RING_BUFFER) { syslog(LOG_NOTICE, "KLOGD: started with Kernel ring buffer as log source\n"); diff --git a/toys/pending/syslogd.c b/toys/pending/syslogd.c index 5ac96c6f..00f12773 100644 --- a/toys/pending/syslogd.c +++ b/toys/pending/syslogd.c @@ -632,25 +632,6 @@ static void setup_signal() signal(SIGQUIT, signal_handler); } -static void syslog_daemon(void) -{ - int fd = open("/dev/null", O_RDWR); - if (fd < 0) fd = xcreate("/", O_RDONLY, 0666); - - pid_t pid = fork(); - if (pid < 0) perror_exit("DAEMON: failed to fork"); - if (pid) exit(EXIT_SUCCESS); - - setsid(); - dup2(fd, 0); - dup2(fd, 1); - dup2(fd, 2); - if (fd > 2) close(fd); - - //don't daemonize again if SIGHUP received. - toys.optflags |= FLAG_n; -} - void syslogd_main(void) { unsocks_t *tsd; @@ -688,7 +669,10 @@ init_jumpin: setup_signal(); if (parse_config_file() == -1) goto clean_and_exit; open_logfiles(); - if (!flag_chk(FLAG_n)) syslog_daemon(); + if (!flag_chk(FLAG_n)) { + //don't daemonize again if SIGHUP received. + toys.optflags |= FLAG_n; + } { int pid_fd = open("/var/run/syslogd.pid", O_CREAT | O_WRONLY | O_TRUNC, 0666); if (pid_fd > 0) { |