1
0
mirror of git://sourceware.org/git/lvm2.git synced 2024-12-21 13:34:40 +03:00

deamonize: restore detection of errors

Keep forked environment for daemon more strick and check even
for nearly impossible to happen errors.
This commit is contained in:
Zdenek Kabelac 2017-09-06 11:47:53 +02:00
parent 3071837e21
commit 03efec2712
2 changed files with 24 additions and 11 deletions

View File

@ -1111,12 +1111,18 @@ static void be_daemon(int timeout)
} }
/* Detach ourself from the calling environment */ /* Detach ourself from the calling environment */
(void) dup2(devnull, STDIN_FILENO); if ((dup2(devnull, STDIN_FILENO) == -1) ||
(void) dup2(devnull, STDOUT_FILENO); (dup2(devnull, STDOUT_FILENO) == -1) ||
(void) dup2(devnull, STDERR_FILENO); (dup2(devnull, STDERR_FILENO) == -1)) {
perror("Error setting terminal FDs to /dev/null");
log_error("Error setting terminal FDs to /dev/null: %m");
exit(5);
}
if (devnull > STDERR_FILENO) if ((devnull > STDERR_FILENO) && close(devnull)) {
(void) 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");

View File

@ -377,15 +377,22 @@ static void _daemonise(daemon_state s)
exit(WEXITSTATUS(child_status)); exit(WEXITSTATUS(child_status));
} }
if (chdir("/")) if (chdir("/")) {
perror("Cannot chdir to /");
exit(1); exit(1);
}
(void) dup2(fd, STDIN_FILENO); if ((dup2(fd, STDIN_FILENO) == -1) ||
(void) dup2(fd, STDOUT_FILENO); (dup2(fd, STDOUT_FILENO) == -1) ||
(void) dup2(fd, STDERR_FILENO); (dup2(fd, STDERR_FILENO) == -1)) {
perror("Error setting terminal FDs to /dev/null");
exit(2);
}
if (fd > STDERR_FILENO) if ((fd > STDERR_FILENO) && close(fd)) {
(void) close(fd); perror("Failed to close /dev/null descriptor");
exit(3);
}
/* Switch to sysconf(_SC_OPEN_MAX) ?? */ /* Switch to sysconf(_SC_OPEN_MAX) ?? */
if (getrlimit(RLIMIT_NOFILE, &rlim) < 0) if (getrlimit(RLIMIT_NOFILE, &rlim) < 0)