Mercurial > hg > toybox
comparison toys/pending/syslogd.c @ 995:893c86bbe452
Add daemonize function to lib for klogd and syslogd
author | Felix Janda <felix.janda@posteo.de> |
---|---|
date | Sat, 10 Aug 2013 20:18:18 +0200 |
parents | c0627c163893 |
children | 8b1814e4c987 |
comparison
equal
deleted
inserted
replaced
994:00c7b6f4104a | 995:893c86bbe452 |
---|---|
630 signal(SIGTERM, signal_handler); | 630 signal(SIGTERM, signal_handler); |
631 signal(SIGINT, signal_handler); | 631 signal(SIGINT, signal_handler); |
632 signal(SIGQUIT, signal_handler); | 632 signal(SIGQUIT, signal_handler); |
633 } | 633 } |
634 | 634 |
635 static void syslog_daemon(void) | |
636 { | |
637 int fd = open("/dev/null", O_RDWR); | |
638 if (fd < 0) fd = xcreate("/", O_RDONLY, 0666); | |
639 | |
640 pid_t pid = fork(); | |
641 if (pid < 0) perror_exit("DAEMON: failed to fork"); | |
642 if (pid) exit(EXIT_SUCCESS); | |
643 | |
644 setsid(); | |
645 dup2(fd, 0); | |
646 dup2(fd, 1); | |
647 dup2(fd, 2); | |
648 if (fd > 2) close(fd); | |
649 | |
650 //don't daemonize again if SIGHUP received. | |
651 toys.optflags |= FLAG_n; | |
652 } | |
653 | |
654 void syslogd_main(void) | 635 void syslogd_main(void) |
655 { | 636 { |
656 unsocks_t *tsd; | 637 unsocks_t *tsd; |
657 int maxfd, retval, last_len=0; | 638 int maxfd, retval, last_len=0; |
658 struct timeval tv; | 639 struct timeval tv; |
686 goto clean_and_exit; | 667 goto clean_and_exit; |
687 } | 668 } |
688 setup_signal(); | 669 setup_signal(); |
689 if (parse_config_file() == -1) goto clean_and_exit; | 670 if (parse_config_file() == -1) goto clean_and_exit; |
690 open_logfiles(); | 671 open_logfiles(); |
691 if (!flag_chk(FLAG_n)) syslog_daemon(); | 672 if (!flag_chk(FLAG_n)) { |
673 //don't daemonize again if SIGHUP received. | |
674 toys.optflags |= FLAG_n; | |
675 } | |
692 { | 676 { |
693 int pid_fd = open("/var/run/syslogd.pid", O_CREAT | O_WRONLY | O_TRUNC, 0666); | 677 int pid_fd = open("/var/run/syslogd.pid", O_CREAT | O_WRONLY | O_TRUNC, 0666); |
694 if (pid_fd > 0) { | 678 if (pid_fd > 0) { |
695 unsigned pid = getpid(); | 679 unsigned pid = getpid(); |
696 int len = sprintf(toybuf, "%u\n", pid); | 680 int len = sprintf(toybuf, "%u\n", pid); |