From a1d2685b08312addc2c6bf772c6f4dd801629a4a Mon Sep 17 00:00:00 2001 From: Wayne Davison Date: Sun, 30 Sep 2007 06:57:07 +0000 Subject: [PATCH] - Moved become_daemon() here from socket.c and made it static. - Call lp_load() before we call become_daemon(). - Output an error to stderr if we fail to open/parse the config file. - Added a create_pid_file() static function and call it earlier in the startup sequence. --- clientserver.c | 80 +++++++++++++++++++++++++++++++++++--------------- 1 file changed, 57 insertions(+), 23 deletions(-) diff --git a/clientserver.c b/clientserver.c index bef2e2ff..4c636664 100644 --- a/clientserver.c +++ b/clientserver.c @@ -839,10 +839,57 @@ int start_daemon(int f_in, int f_out) return rsync_module(f_in, f_out, i, addr, host); } -int daemon_main(void) +static void create_pid_file(pid_t pid) +{ + char *pid_file = lp_pid_file(); + char pidbuf[16]; + int fd; + + if (!pid_file || !*pid_file) + return; + + cleanup_set_pid(pid); + if ((fd = do_open(pid_file, O_WRONLY|O_CREAT|O_TRUNC, 0666 & ~orig_umask)) == -1) { + cleanup_set_pid(0); + rsyserr(FLOG, errno, "failed to create pid file %s", pid_file); + exit_cleanup(RERR_FILEIO); + } + snprintf(pidbuf, sizeof pidbuf, "%ld\n", (long)pid); + write(fd, pidbuf, strlen(pidbuf)); + close(fd); +} + +/* Become a daemon, discarding the controlling terminal. */ +static void become_daemon(void) { - char *pid_file; + int i; + pid_t pid = fork(); + + if (pid) { + create_pid_file(pid); + _exit(0); + } + /* detach from the terminal */ +#ifdef HAVE_SETSID + setsid(); +#elif defined TIOCNOTTY + i = open("/dev/tty", O_RDWR); + if (i >= 0) { + ioctl(i, (int)TIOCNOTTY, (char *)0); + close(i); + } +#endif + /* make sure that stdin, stdout an stderr don't stuff things + * up (library functions, for example) */ + for (i = 0; i < 3; i++) { + close(i); + open("/dev/null", O_RDWR); + } +} + +int daemon_main(void) +{ if (is_a_socket(STDIN_FILENO)) { int i; @@ -857,11 +904,15 @@ int daemon_main(void) return start_daemon(STDIN_FILENO, STDIN_FILENO); } - if (!no_detach) - become_daemon(); - - if (!lp_load(config_file, 1)) + if (!lp_load(config_file, 1)) { + fprintf(stderr, "Failed to parse config file: %s\n", config_file); exit_cleanup(RERR_SYNTAX); + } + + if (no_detach) + create_pid_file(getpid()); + else + become_daemon(); if (rsync_port == 0 && (rsync_port = lp_rsync_port()) == 0) rsync_port = RSYNC_PORT; @@ -876,23 +927,6 @@ int daemon_main(void) * address too. In fact, why not just do inet_ntop on the * local address??? */ - if (((pid_file = lp_pid_file()) != NULL) && (*pid_file != '\0')) { - char pidbuf[16]; - int fd; - pid_t pid = getpid(); - cleanup_set_pid(pid); - if ((fd = do_open(lp_pid_file(), O_WRONLY|O_CREAT|O_TRUNC, - 0666 & ~orig_umask)) == -1) { - cleanup_set_pid(0); - rsyserr(FLOG, errno, "failed to create pid file %s", - pid_file); - exit_cleanup(RERR_FILEIO); - } - snprintf(pidbuf, sizeof pidbuf, "%ld\n", (long)pid); - write(fd, pidbuf, strlen(pidbuf)); - close(fd); - } - start_accept_loop(rsync_port, start_daemon); return -1; } -- 2.34.1