1
0
mirror of git://sourceware.org/git/lvm2.git synced 2025-10-05 07:33:15 +03:00

daemonize: more unified code

ATM we have several instances of daemonizing code.
Each has its 'special' logic so not completely easy
to unify them all into a single routine.

Start to unify them and use one strategy for rediricting
all input/outpus to /dev/null - use 'dup2' function for this
and open /dev/null before fork to make sure it's available.
This commit is contained in:
Zdenek Kabelac
2017-08-25 11:55:38 +02:00
parent 5de9444202
commit b3b1e788e1
2 changed files with 25 additions and 22 deletions

View File

@@ -1105,31 +1105,25 @@ static void be_daemon(int timeout)
break; break;
default: /* Parent */ default: /* Parent */
(void) close(devnull);
(void) close(child_pipe[1]); (void) close(child_pipe[1]);
wait_for_child(child_pipe[0], timeout); wait_for_child(child_pipe[0], timeout); /* noreturn */
} }
/* Detach ourself from the calling environment */ /* Detach ourself from the calling environment */
if (close(0) || close(1) || close(2)) { (void) dup2(devnull, STDIN_FILENO);
perror("Error closing terminal FDs"); (void) dup2(devnull, STDOUT_FILENO);
exit(4); (void) dup2(devnull, STDERR_FILENO);
}
setsid(); if (devnull > STDERR_FILENO)
(void) close(devnull);
if (dup2(devnull, 0) < 0 || dup2(devnull, 1) < 0
|| dup2(devnull, 2) < 0) {
perror("Error setting terminal FDs to /dev/null");
log_error("Error setting terminal FDs to /dev/null: %m");
exit(5);
}
if ((devnull > STDERR_FILENO) && close(devnull)) {
log_sys_error("close", "/dev/null");
exit(7);
}
if (chdir("/")) { if (chdir("/")) {
log_error("Error setting current directory to /: %m"); log_error("Error setting current directory to /: %m");
exit(6); exit(6);
} }
setsid();
} }
static int verify_message(char *buf, int len) static int verify_message(char *buf, int len)

View File

@@ -334,6 +334,11 @@ static void _daemonise(daemon_state s)
struct timeval tval; struct timeval tval;
sigset_t my_sigset; sigset_t my_sigset;
if ((fd = open("/dev/null", O_RDWR)) == -1) {
fprintf(stderr, "Unable to open /dev/null.\n");
exit(EXIT_FAILURE);
}
sigemptyset(&my_sigset); sigemptyset(&my_sigset);
if (sigprocmask(SIG_SETMASK, &my_sigset, NULL) < 0) { if (sigprocmask(SIG_SETMASK, &my_sigset, NULL) < 0) {
fprintf(stderr, "Unable to restore signals.\n"); fprintf(stderr, "Unable to restore signals.\n");
@@ -350,6 +355,7 @@ static void _daemonise(daemon_state s)
break; break;
default: default:
(void) close(fd);
/* Wait for response from child */ /* Wait for response from child */
while (!waitpid(pid, &child_status, WNOHANG) && !_shutdown_requested) { while (!waitpid(pid, &child_status, WNOHANG) && !_shutdown_requested) {
tval.tv_sec = 0; tval.tv_sec = 0;
@@ -374,12 +380,20 @@ static void _daemonise(daemon_state s)
if (chdir("/")) if (chdir("/"))
exit(1); exit(1);
(void) dup2(fd, STDIN_FILENO);
(void) dup2(fd, STDOUT_FILENO);
(void) dup2(fd, STDERR_FILENO);
if (fd > STDERR_FILENO)
(void) close(fd);
/* Switch to sysconf(_SC_OPEN_MAX) ?? */
if (getrlimit(RLIMIT_NOFILE, &rlim) < 0) if (getrlimit(RLIMIT_NOFILE, &rlim) < 0)
fd = 256; /* just have to guess */ fd = 256; /* just have to guess */
else else
fd = rlim.rlim_cur; fd = rlim.rlim_cur;
for (--fd; fd >= 0; fd--) { for (--fd; fd > STDERR_FILENO; fd--) {
#ifdef __linux__ #ifdef __linux__
/* Do not close fds preloaded by systemd! */ /* Do not close fds preloaded by systemd! */
if (_systemd_activation && fd == SD_FD_SOCKET_SERVER) if (_systemd_activation && fd == SD_FD_SOCKET_SERVER)
@@ -388,11 +402,6 @@ static void _daemonise(daemon_state s)
(void) close(fd); (void) close(fd);
} }
if ((open("/dev/null", O_RDONLY) < 0) ||
(open("/dev/null", O_WRONLY) < 0) ||
(open("/dev/null", O_WRONLY) < 0))
exit(1);
setsid(); setsid();
} }