diff options
author | David Hardeman <david@hardeman.nu> | 2015-01-21 16:16:59 -0500 |
---|---|---|
committer | Steve Dickson <steved@redhat.com> | 2015-01-23 14:19:09 -0500 |
commit | 7addf9dadb35e8f6a770b943e67ddc77e1bc97ba (patch) | |
tree | 80f3d61941272f055676823bc3a14fe8009525c2 /utils/statd/statd.c | |
parent | f980298853d9b12a222421342418732f65883c30 (diff) | |
download | nfs-utils-7addf9dadb35e8f6a770b943e67ddc77e1bc97ba.tar.gz nfs-utils-7addf9dadb35e8f6a770b943e67ddc77e1bc97ba.tar.xz nfs-utils-7addf9dadb35e8f6a770b943e67ddc77e1bc97ba.zip |
cleanup daemonization code
The daemonization init/ready functions have parameters that are never used,
require the caller to keep track of some pipefds that it has no interest in
and which might not be used in some scenarios. Cleanup both functions a bit.
The idea here is also that these two functions might be good points to
insert more systemd init code later (sd_notify()).
Also, statd had a private copy of the daemonization code for unknown
reasons...so make it use the generic version instead.
Signed-off-by: David H?rdeman <david@hardeman.nu>
Signed-off-by: Steve Dickson <steved@redhat.com>
Diffstat (limited to 'utils/statd/statd.c')
-rw-r--r-- | utils/statd/statd.c | 66 |
1 files changed, 11 insertions, 55 deletions
diff --git a/utils/statd/statd.c b/utils/statd/statd.c index 51a016e..60ce6d1 100644 --- a/utils/statd/statd.c +++ b/utils/statd/statd.c @@ -248,13 +248,12 @@ int main (int argc, char **argv) int nlm_udp = 0, nlm_tcp = 0; struct rlimit rlim; - int pipefds[2] = { -1, -1}; - char status; - /* Default: daemon mode, no other options */ run_mode = 0; - xlog_stderr(0); - xlog_syslog(1); + + /* Log to stderr if there's an error during startup */ + xlog_stderr(1); + xlog_syslog(0); /* Set the basename */ if ((name_p = strrchr(argv[0],'/')) != NULL) { @@ -394,52 +393,17 @@ int main (int argc, char **argv) simulator (--argc, ++argv); /* simulator() does exit() */ #endif - if (!(run_mode & MODE_NODAEMON)) { - int tempfd; - - if (pipe(pipefds)<0) { - perror("statd: unable to create pipe"); - exit(1); - } - if ((pid = fork ()) < 0) { - perror ("statd: Could not fork"); - exit (1); - } else if (pid != 0) { - /* Parent. - * Wait for status from child. - */ - close(pipefds[1]); - if (read(pipefds[0], &status, 1) != 1) - exit(1); - exit (0); - } - /* Child. */ - close(pipefds[0]); - setsid (); - - while (pipefds[1] <= 2) { - pipefds[1] = dup(pipefds[1]); - if (pipefds[1]<0) { - perror("statd: dup"); - exit(1); - } - } - tempfd = open("/dev/null", O_RDWR); - dup2(tempfd, 0); - dup2(tempfd, 1); - dup2(tempfd, 2); - dup2(pipefds[1], 3); - pipefds[1] = 3; - closeall(4); - } - - /* Child. */ + daemon_init(!(run_mode & MODE_NODAEMON)); if (run_mode & MODE_LOG_STDERR) { xlog_syslog(0); xlog_stderr(1); xlog_config(D_ALL, 1); + } else { + xlog_syslog(1); + xlog_stderr(0); } + xlog_open(name_p); xlog(L_NOTICE, "Version " VERSION " starting"); @@ -512,16 +476,8 @@ int main (int argc, char **argv) } atexit(statd_unregister); - /* If we got this far, we have successfully started, so notify parent */ - if (pipefds[1] > 0) { - status = 0; - if (write(pipefds[1], &status, 1) != 1) { - xlog_warn("writing to parent pipe failed: errno %d (%s)\n", - errno, strerror(errno)); - } - close(pipefds[1]); - pipefds[1] = -1; - } + /* If we got this far, we have successfully started */ + daemon_ready(); for (;;) { /* |