tree-wide: introduce new safe_fork() helper and port everything over
This adds a new safe_fork() wrapper around fork() and makes use of it everywhere. The new wrapper does a couple of things we previously did manually and separately in a safer, more correct and automatic way: 1. Optionally resets signal handlers/mask in the child 2. Sets a name on all processes we fork off right after forking off (and the patch assigns useful names for all processes we fork off now, following a systematic naming scheme: always enclosed in () – in order to indicate that these are not proper, exec()ed processes, but only forked off children, and if the process is long-running with only our own code, without execve()'ing something else, it gets am "sd-" prefix.) 3. Optionally closes all file descriptors in the child 4. Optionally sets a PR_SET_DEATHSIG to SIGTERM in the child, in a safe way so that the parent dying before this happens being handled safely. 5. Optionally reopens the logs 6. Optionally connects stdin/stdout/stderr to /dev/null 7. Debug logs about the forked off processes.
This commit is contained in:
@@ -33,7 +33,7 @@
|
||||
#include "util.h"
|
||||
|
||||
static int spawn_getent(const char *database, const char *key, pid_t *rpid) {
|
||||
int pipe_fds[2];
|
||||
int pipe_fds[2], r;
|
||||
pid_t pid;
|
||||
|
||||
assert(database);
|
||||
@@ -43,10 +43,10 @@ static int spawn_getent(const char *database, const char *key, pid_t *rpid) {
|
||||
if (pipe2(pipe_fds, O_CLOEXEC) < 0)
|
||||
return log_error_errno(errno, "Failed to allocate pipe: %m");
|
||||
|
||||
pid = fork();
|
||||
if (pid < 0)
|
||||
return log_error_errno(errno, "Failed to fork getent child: %m");
|
||||
else if (pid == 0) {
|
||||
r = safe_fork("(getent)", FORK_RESET_SIGNALS|FORK_DEATHSIG, &pid);
|
||||
if (r < 0)
|
||||
return log_error_errno(r, "Failed to fork getent child: %m");
|
||||
if (r == 0) {
|
||||
int nullfd;
|
||||
char *empty_env = NULL;
|
||||
|
||||
@@ -71,8 +71,6 @@ static int spawn_getent(const char *database, const char *key, pid_t *rpid) {
|
||||
if (nullfd > 2)
|
||||
safe_close(nullfd);
|
||||
|
||||
(void) reset_all_signal_handlers();
|
||||
(void) reset_signal_mask();
|
||||
close_all_fds(NULL, 0);
|
||||
|
||||
execle("/usr/bin/getent", "getent", database, key, NULL, &empty_env);
|
||||
|
||||
@@ -93,7 +93,7 @@ int stub_pid1(sd_id128_t uuid) {
|
||||
sd_id128_to_string(uuid, new_environment + sizeof(new_environment) - SD_ID128_STRING_MAX);
|
||||
reset_environ(new_environment, sizeof(new_environment));
|
||||
|
||||
rename_process("STUBINIT");
|
||||
(void) rename_process("(sd-stubinit)");
|
||||
|
||||
assert_se(sigemptyset(&waitmask) >= 0);
|
||||
assert_se(sigset_add_many(&waitmask,
|
||||
|
||||
Reference in New Issue
Block a user