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:
Lennart Poettering
2017-12-25 11:48:21 +01:00
parent d8caff6db6
commit 4c253ed1ca
42 changed files with 448 additions and 507 deletions
+8 -23
View File
@@ -267,38 +267,23 @@ static int exec_process(const char* name, char **argv, char **env, int start_fd,
static int fork_and_exec_process(const char* child, char** argv, char **env, int fd) { static int fork_and_exec_process(const char* child, char** argv, char **env, int fd) {
_cleanup_free_ char *joined = NULL; _cleanup_free_ char *joined = NULL;
pid_t parent_pid, child_pid; pid_t child_pid;
int r;
joined = strv_join(argv, " "); joined = strv_join(argv, " ");
if (!joined) if (!joined)
return log_oom(); return log_oom();
parent_pid = getpid_cached(); r = safe_fork("(activate)", FORK_RESET_SIGNALS|FORK_DEATHSIG, &child_pid);
if (r < 0)
child_pid = fork(); return log_error_errno(r, "Failed to fork: %m");
if (child_pid < 0) if (r == 0) {
return log_error_errno(errno, "Failed to fork: %m"); /* In the child */
/* In the child */
if (child_pid == 0) {
(void) reset_all_signal_handlers();
(void) reset_signal_mask();
/* Make sure the child goes away when the parent dies */
if (prctl(PR_SET_PDEATHSIG, SIGTERM) < 0)
_exit(EXIT_FAILURE);
/* Check whether our parent died before we were able
* to set the death signal */
if (getppid() != parent_pid)
_exit(EXIT_SUCCESS);
exec_process(child, argv, env, fd, 1); exec_process(child, argv, env, fd, 1);
_exit(EXIT_FAILURE); _exit(EXIT_FAILURE);
} }
log_info("Spawned %s (%s) as PID %d", child, joined, child_pid); log_info("Spawned %s (%s) as PID " PID_FMT ".", child, joined, child_pid);
return 0; return 0;
} }
+5 -16
View File
@@ -61,29 +61,18 @@ finish:
} }
int asynchronous_sync(void) { int asynchronous_sync(void) {
pid_t pid; int r;
/* This forks off an invocation of fork() as a child process, in order to initiate synchronization to /* This forks off an invocation of fork() as a child process, in order to initiate synchronization to
* disk. Note that we implement this as helper process rather than thread as we don't want the sync() to hang our * disk. Note that we implement this as helper process rather than thread as we don't want the sync() to hang our
* original process ever, and a thread would do that as the process can't exit with threads hanging in blocking * original process ever, and a thread would do that as the process can't exit with threads hanging in blocking
* syscalls. */ * syscalls. */
log_debug("Spawning new process for sync"); r = safe_fork("(sd-sync)", FORK_RESET_SIGNALS|FORK_CLOSE_ALL_FDS, NULL);
if (r < 0)
pid = fork(); return r;
if (pid < 0) if (r == 0) {
return -errno;
if (pid == 0) {
/* Child process */ /* Child process */
(void) rename_process("(sd-sync)");
(void) reset_all_signal_handlers();
(void) reset_signal_mask();
(void) close_all_fds(NULL, 0);
(void) sync(); (void) sync();
_exit(EXIT_SUCCESS); _exit(EXIT_SUCCESS);
} }
+5 -9
View File
@@ -1348,9 +1348,8 @@ typedef struct SpecNextResult {
} SpecNextResult; } SpecNextResult;
int calendar_spec_next_usec(const CalendarSpec *spec, usec_t usec, usec_t *next) { int calendar_spec_next_usec(const CalendarSpec *spec, usec_t usec, usec_t *next) {
SpecNextResult *shared, tmp;
pid_t pid; pid_t pid;
SpecNextResult *shared;
SpecNextResult tmp;
int r; int r;
if (isempty(spec->timezone)) if (isempty(spec->timezone))
@@ -1360,15 +1359,12 @@ int calendar_spec_next_usec(const CalendarSpec *spec, usec_t usec, usec_t *next)
if (shared == MAP_FAILED) if (shared == MAP_FAILED)
return negative_errno(); return negative_errno();
pid = fork(); r = safe_fork("(sd-calendar)", FORK_RESET_SIGNALS|FORK_CLOSE_ALL_FDS|FORK_DEATHSIG, &pid);
if (r < 0) {
if (pid == -1) {
int fork_errno = errno;
(void) munmap(shared, sizeof *shared); (void) munmap(shared, sizeof *shared);
return -fork_errno; return r;
} }
if (r == 0) {
if (pid == 0) {
if (setenv("TZ", spec->timezone, 1) != 0) { if (setenv("TZ", spec->timezone, 1) != 0) {
shared->return_value = negative_errno(); shared->return_value = negative_errno();
_exit(EXIT_FAILURE); _exit(EXIT_FAILURE);
+9 -16
View File
@@ -48,20 +48,19 @@ assert_cc(EAGAIN == EWOULDBLOCK);
static int do_spawn(const char *path, char *argv[], int stdout_fd, pid_t *pid) { static int do_spawn(const char *path, char *argv[], int stdout_fd, pid_t *pid) {
pid_t _pid; pid_t _pid;
int r;
if (null_or_empty_path(path)) { if (null_or_empty_path(path)) {
log_debug("%s is empty (a mask).", path); log_debug("%s is empty (a mask).", path);
return 0; return 0;
} }
_pid = fork(); r = safe_fork("(direxec)", FORK_DEATHSIG, &_pid);
if (_pid < 0) if (r < 0)
return log_error_errno(errno, "Failed to fork: %m"); return log_error_errno(r, "Failed to fork: %m");
if (_pid == 0) { if (r == 0) {
char *_argv[2]; char *_argv[2];
assert_se(prctl(PR_SET_PDEATHSIG, SIGTERM) == 0);
if (stdout_fd >= 0) { if (stdout_fd >= 0) {
/* If the fd happens to be in the right place, go along with that */ /* If the fd happens to be in the right place, go along with that */
if (stdout_fd != STDOUT_FILENO && if (stdout_fd != STDOUT_FILENO &&
@@ -107,11 +106,6 @@ static int do_execute(
* If callbacks is nonnull, execution is serial. Otherwise, we default to parallel. * If callbacks is nonnull, execution is serial. Otherwise, we default to parallel.
*/ */
(void) reset_all_signal_handlers();
(void) reset_signal_mask();
assert_se(prctl(PR_SET_PDEATHSIG, SIGTERM) == 0);
r = conf_files_list_strv(&paths, NULL, NULL, CONF_FILES_EXECUTABLE, (const char* const*) directories); r = conf_files_list_strv(&paths, NULL, NULL, CONF_FILES_EXECUTABLE, (const char* const*) directories);
if (r < 0) if (r < 0)
return r; return r;
@@ -222,11 +216,10 @@ int execute_directories(
* them to finish. Optionally a timeout is applied. If a file with the same name * them to finish. Optionally a timeout is applied. If a file with the same name
* exists in more than one directory, the earliest one wins. */ * exists in more than one directory, the earliest one wins. */
executor_pid = fork(); r = safe_fork("(sd-executor)", FORK_RESET_SIGNALS|FORK_DEATHSIG, &executor_pid);
if (executor_pid < 0) if (r < 0)
return log_error_errno(errno, "Failed to fork: %m"); return log_error_errno(r, "Failed to fork: %m");
if (r == 0) {
if (executor_pid == 0) {
r = do_execute(dirs, timeout, callbacks, callback_args, fd, argv); r = do_execute(dirs, timeout, callbacks, callback_args, fd, argv);
_exit(r < 0 ? EXIT_FAILURE : EXIT_SUCCESS); _exit(r < 0 ? EXIT_FAILURE : EXIT_SUCCESS);
} }
+137
View File
@@ -56,6 +56,7 @@
#include "stat-util.h" #include "stat-util.h"
#include "string-table.h" #include "string-table.h"
#include "string-util.h" #include "string-util.h"
#include "terminal-util.h"
#include "user-util.h" #include "user-util.h"
#include "util.h" #include "util.h"
@@ -1134,6 +1135,142 @@ int must_be_root(void) {
return -EPERM; return -EPERM;
} }
int safe_fork_full(
const char *name,
const int except_fds[],
size_t n_except_fds,
ForkFlags flags,
pid_t *ret_pid) {
pid_t original_pid, pid;
sigset_t saved_ss;
bool block_signals;
int r;
/* A wrapper around fork(), that does a couple of important initializations in addition to mere forking. Always
* returns the child's PID in *ret_pid. Returns == 0 in the child, and > 0 in the parent. */
original_pid = getpid_cached();
block_signals = flags & (FORK_RESET_SIGNALS|FORK_DEATHSIG);
if (block_signals) {
sigset_t ss;
/* We temporarily block all signals, so that the new child has them blocked initially. This way, we can be sure
* that SIGTERMs are not lost we might send to the child. */
if (sigfillset(&ss) < 0)
return log_debug_errno(errno, "Failed to reset signal set: %m");
if (sigprocmask(SIG_SETMASK, &ss, &saved_ss) < 0)
return log_debug_errno(errno, "Failed to reset signal mask: %m");
}
pid = fork();
if (pid < 0) {
r = -errno;
if (block_signals) /* undo what we did above */
(void) sigprocmask(SIG_SETMASK, &saved_ss, NULL);
return log_debug_errno(r, "Failed to fork: %m");
}
if (pid > 0) {
/* We are in the parent process */
if (block_signals) /* undo what we did above */
(void) sigprocmask(SIG_SETMASK, &saved_ss, NULL);
log_debug("Sucessfully forked off '%s' as PID " PID_FMT ".", strna(name), pid);
if (ret_pid)
*ret_pid = pid;
return 1;
}
/* We are in the child process */
if (flags & FORK_REOPEN_LOG) {
/* Close the logs if requested, before we log anything. And make sure we reopen it if needed. */
log_close();
log_set_open_when_needed(true);
}
if (name) {
r = rename_process(name);
if (r < 0)
log_debug_errno(r, "Failed to rename process, ignoring: %m");
}
if (flags & FORK_DEATHSIG)
if (prctl(PR_SET_PDEATHSIG, SIGTERM) < 0) {
log_debug_errno(errno, "Failed to set death signal: %m");
_exit(EXIT_FAILURE);
}
if (flags & FORK_RESET_SIGNALS) {
r = reset_all_signal_handlers();
if (r < 0) {
log_debug_errno(r, "Failed to reset signal handlers: %m");
_exit(EXIT_FAILURE);
}
/* This implicitly undoes the signal mask stuff we did before the fork()ing above */
r = reset_signal_mask();
if (r < 0) {
log_debug_errno(r, "Failed to reset signal mask: %m");
_exit(EXIT_FAILURE);
}
} else if (block_signals) { /* undo what we did above */
if (sigprocmask(SIG_SETMASK, &saved_ss, NULL) < 0) {
log_debug_errno(errno, "Failed to restore signal mask: %m");
_exit(EXIT_FAILURE);
}
}
if (flags & FORK_DEATHSIG) {
/* Let's see if the parent PID is still the one we started from? If not, then the parent
* already died by the time we set PR_SET_PDEATHSIG, hence let's emulate the effect */
if (getppid() != original_pid) {
log_debug("Parent died early, raising SIGTERM.");
(void) raise(SIGTERM);
_exit(EXIT_FAILURE);
}
}
if (flags & FORK_CLOSE_ALL_FDS) {
/* Close the logs here in case it got reopened above, as close_all_fds() would close them for us */
log_close();
r = close_all_fds(except_fds, n_except_fds);
if (r < 0) {
log_debug_errno(r, "Failed to close all file descriptors: %m");
_exit(EXIT_FAILURE);
}
}
/* When we were asked to reopen the logs, do so again now */
if (flags & FORK_REOPEN_LOG) {
log_open();
log_set_open_when_needed(false);
}
if (flags & FORK_NULL_STDIO) {
r = make_null_stdio();
if (r < 0) {
log_debug_errno(r, "Failed to connect stdin/stdout to /dev/null: %m");
_exit(EXIT_FAILURE);
}
}
if (ret_pid)
*ret_pid = getpid_cached();
return 0;
}
static const char *const ioprio_class_table[] = { static const char *const ioprio_class_table[] = {
[IOPRIO_CLASS_NONE] = "none", [IOPRIO_CLASS_NONE] = "none",
[IOPRIO_CLASS_RT] = "realtime", [IOPRIO_CLASS_RT] = "realtime",
+14
View File
@@ -142,3 +142,17 @@ int ioprio_parse_priority(const char *s, int *ret);
pid_t getpid_cached(void); pid_t getpid_cached(void);
int must_be_root(void); int must_be_root(void);
typedef enum ForkFlags {
FORK_RESET_SIGNALS = 1U << 0,
FORK_CLOSE_ALL_FDS = 1U << 1,
FORK_DEATHSIG = 1U << 2,
FORK_NULL_STDIO = 1U << 3,
FORK_REOPEN_LOG = 1U << 4,
} ForkFlags;
int safe_fork_full(const char *name, const int except_fds[], size_t n_except_fds, ForkFlags flags, pid_t *ret_pid);
static inline int safe_fork(const char *name, ForkFlags flags, pid_t *ret_pid) {
return safe_fork_full(name, NULL, 0, flags, ret_pid);
}
+8 -10
View File
@@ -1106,11 +1106,10 @@ int openpt_in_namespace(pid_t pid, int flags) {
if (socketpair(AF_UNIX, SOCK_DGRAM, 0, pair) < 0) if (socketpair(AF_UNIX, SOCK_DGRAM, 0, pair) < 0)
return -errno; return -errno;
child = fork(); r = safe_fork("(sd-openpt)", FORK_RESET_SIGNALS|FORK_DEATHSIG, &child);
if (child < 0) if (r < 0)
return -errno; return r;
if (r == 0) {
if (child == 0) {
int master; int master;
pair[0] = safe_close(pair[0]); pair[0] = safe_close(pair[0]);
@@ -1157,11 +1156,10 @@ int open_terminal_in_namespace(pid_t pid, const char *name, int mode) {
if (socketpair(AF_UNIX, SOCK_DGRAM, 0, pair) < 0) if (socketpair(AF_UNIX, SOCK_DGRAM, 0, pair) < 0)
return -errno; return -errno;
child = fork(); r = safe_fork("(sd-terminal)", FORK_RESET_SIGNALS|FORK_DEATHSIG, &child);
if (child < 0) if (r < 0)
return -errno; return r;
if (r == 0) {
if (child == 0) {
int master; int master;
pair[0] = safe_close(pair[0]); pair[0] = safe_close(pair[0]);
+5 -8
View File
@@ -886,8 +886,8 @@ typedef struct ParseTimestampResult {
int parse_timestamp(const char *t, usec_t *usec) { int parse_timestamp(const char *t, usec_t *usec) {
char *last_space, *tz = NULL; char *last_space, *tz = NULL;
ParseTimestampResult *shared, tmp; ParseTimestampResult *shared, tmp;
int r;
pid_t pid; pid_t pid;
int r;
last_space = strrchr(t, ' '); last_space = strrchr(t, ' ');
if (last_space != NULL && timezone_is_valid(last_space + 1)) if (last_space != NULL && timezone_is_valid(last_space + 1))
@@ -900,15 +900,12 @@ int parse_timestamp(const char *t, usec_t *usec) {
if (shared == MAP_FAILED) if (shared == MAP_FAILED)
return negative_errno(); return negative_errno();
pid = fork(); r = safe_fork("(sd-timestamp)", FORK_RESET_SIGNALS|FORK_CLOSE_ALL_FDS|FORK_DEATHSIG, &pid);
if (r < 0) {
if (pid == -1) {
int fork_errno = errno;
(void) munmap(shared, sizeof *shared); (void) munmap(shared, sizeof *shared);
return -fork_errno; return r;
} }
if (r == 0) {
if (pid == 0) {
bool with_tz = true; bool with_tz = true;
if (setenv("TZ", tz, 1) != 0) { if (setenv("TZ", tz, 1) != 0) {
+7 -39
View File
@@ -186,11 +186,11 @@ int prot_from_flags(int flags) {
int fork_agent(pid_t *pid, const int except[], unsigned n_except, const char *path, ...) { int fork_agent(pid_t *pid, const int except[], unsigned n_except, const char *path, ...) {
bool stdout_is_tty, stderr_is_tty; bool stdout_is_tty, stderr_is_tty;
pid_t parent_pid, agent_pid; pid_t agent_pid;
sigset_t ss, saved_ss;
unsigned n, i; unsigned n, i;
va_list ap; va_list ap;
char **l; char **l;
int r;
assert(pid); assert(pid);
assert(path); assert(path);
@@ -198,45 +198,13 @@ int fork_agent(pid_t *pid, const int except[], unsigned n_except, const char *pa
/* Spawns a temporary TTY agent, making sure it goes away when /* Spawns a temporary TTY agent, making sure it goes away when
* we go away */ * we go away */
parent_pid = getpid_cached(); r = safe_fork_full("(sd-agent)", except, n_except, FORK_RESET_SIGNALS|FORK_DEATHSIG|FORK_CLOSE_ALL_FDS, &agent_pid);
if (r < 0)
/* First we temporarily block all signals, so that the new return r;
* child has them blocked initially. This way, we can be sure if (r > 0)
* that SIGTERMs are not lost we might send to the agent. */
assert_se(sigfillset(&ss) >= 0);
assert_se(sigprocmask(SIG_SETMASK, &ss, &saved_ss) >= 0);
agent_pid = fork();
if (agent_pid < 0) {
assert_se(sigprocmask(SIG_SETMASK, &saved_ss, NULL) >= 0);
return -errno;
}
if (agent_pid != 0) {
assert_se(sigprocmask(SIG_SETMASK, &saved_ss, NULL) >= 0);
*pid = agent_pid;
return 0; return 0;
}
/* In the child: /* In the child: */
*
* Make sure the agent goes away when the parent dies */
if (prctl(PR_SET_PDEATHSIG, SIGTERM) < 0)
_exit(EXIT_FAILURE);
/* Make sure we actually can kill the agent, if we need to, in
* case somebody invoked us from a shell script that trapped
* SIGTERM or so... */
(void) reset_all_signal_handlers();
(void) reset_signal_mask();
/* Check whether our parent died before we were able
* to set the death signal and unblock the signals */
if (getppid() != parent_pid)
_exit(EXIT_SUCCESS);
/* Don't leak fds to the agent */
close_all_fds(except, n_except);
stdout_is_tty = isatty(STDOUT_FILENO); stdout_is_tty = isatty(STDOUT_FILENO);
stderr_is_tty = isatty(STDERR_FILENO); stderr_is_tty = isatty(STDERR_FILENO);
+10 -19
View File
@@ -1207,27 +1207,19 @@ static int setup_pam(
parent_pid = getpid_cached(); parent_pid = getpid_cached();
pam_pid = fork(); r = safe_fork("(sd-pam)", 0, &pam_pid);
if (pam_pid < 0) { if (r < 0)
r = -errno;
goto fail; goto fail;
} if (r == 0) {
if (pam_pid == 0) {
int sig, ret = EXIT_PAM; int sig, ret = EXIT_PAM;
/* The child's job is to reset the PAM session on /* The child's job is to reset the PAM session on
* termination */ * termination */
barrier_set_role(&barrier, BARRIER_CHILD); barrier_set_role(&barrier, BARRIER_CHILD);
/* This string must fit in 10 chars (i.e. the length /* Make sure we don't keep open the passed fds in this child. We assume that otherwise only those fds
* of "/sbin/init"), to look pretty in /bin/ps */ * are open here that have been opened by PAM. */
rename_process("(sd-pam)"); (void) close_many(fds, n_fds);
/* Make sure we don't keep open the passed fds in this
child. We assume that otherwise only those fds are
open here that have been opened by PAM. */
close_many(fds, n_fds);
/* Drop privileges - we don't need any to pam_close_session /* Drop privileges - we don't need any to pam_close_session
* and this will make PR_SET_PDEATHSIG work in most cases. * and this will make PR_SET_PDEATHSIG work in most cases.
@@ -1879,11 +1871,10 @@ static int setup_private_users(uid_t uid, gid_t gid) {
if (pipe2(errno_pipe, O_CLOEXEC) < 0) if (pipe2(errno_pipe, O_CLOEXEC) < 0)
return -errno; return -errno;
pid = fork(); r = safe_fork("(sd-userns)", FORK_RESET_SIGNALS|FORK_DEATHSIG, &pid);
if (pid < 0) if (r < 0)
return -errno; return r;
if (r == 0) {
if (pid == 0) {
_cleanup_close_ int fd = -1; _cleanup_close_ int fd = -1;
const char *a; const char *a;
pid_t ppid; pid_t ppid;
+11 -11
View File
@@ -221,13 +221,12 @@ static void sync_with_progress(void) {
/* Due to the possiblity of the sync operation hanging, we fork /* Due to the possiblity of the sync operation hanging, we fork
* a child process and monitor the progress. If the timeout * a child process and monitor the progress. If the timeout
* lapses, the assumption is that that particular sync stalled. */ * lapses, the assumption is that that particular sync stalled. */
pid = fork(); r = safe_fork("(sd-sync)", FORK_RESET_SIGNALS|FORK_CLOSE_ALL_FDS, &pid);
if (pid < 0) { if (r < 0) {
log_error_errno(errno, "Failed to fork: %m"); log_error_errno(r, "Failed to fork: %m");
return; return;
} }
if (r == 0) {
if (pid == 0) {
/* Start the sync operation here in the child */ /* Start the sync operation here in the child */
sync(); sync();
_exit(EXIT_SUCCESS); _exit(EXIT_SUCCESS);
@@ -492,10 +491,10 @@ int main(int argc, char *argv[]) {
log_info("Rebooting with kexec."); log_info("Rebooting with kexec.");
pid = fork(); r = safe_fork("(sd-kexec)", FORK_RESET_SIGNALS|FORK_CLOSE_ALL_FDS, &pid);
if (pid < 0) if (r < 0)
log_error_errno(errno, "Failed to fork: %m"); log_error_errno(r, "Failed to fork: %m");
else if (pid == 0) { if (r == 0) {
const char * const args[] = { const char * const args[] = {
KEXEC, "-e", NULL KEXEC, "-e", NULL
@@ -505,8 +504,9 @@ int main(int argc, char *argv[]) {
execv(args[0], (char * const *) args); execv(args[0], (char * const *) args);
_exit(EXIT_FAILURE); _exit(EXIT_FAILURE);
} else }
wait_for_terminate_and_warn("kexec", pid, true);
(void) wait_for_terminate_and_warn("kexec", pid, true);
} }
cmd = RB_AUTOBOOT; cmd = RB_AUTOBOOT;
+3 -3
View File
@@ -1527,7 +1527,7 @@ static int socket_address_listen_in_cgroup(
if (socketpair(AF_UNIX, SOCK_SEQPACKET|SOCK_CLOEXEC, 0, pair) < 0) if (socketpair(AF_UNIX, SOCK_SEQPACKET|SOCK_CLOEXEC, 0, pair) < 0)
return log_unit_error_errno(UNIT(s), errno, "Failed to create communication channel: %m"); return log_unit_error_errno(UNIT(s), errno, "Failed to create communication channel: %m");
r = unit_fork_helper_process(UNIT(s), &pid); r = unit_fork_helper_process(UNIT(s), "(sd-listen)", &pid);
if (r < 0) if (r < 0)
return log_unit_error_errno(UNIT(s), r, "Failed to fork off listener stub process: %m"); return log_unit_error_errno(UNIT(s), r, "Failed to fork off listener stub process: %m");
if (r == 0) { if (r == 0) {
@@ -1944,7 +1944,7 @@ static int socket_chown(Socket *s, pid_t *_pid) {
/* We have to resolve the user names out-of-process, hence /* We have to resolve the user names out-of-process, hence
* let's fork here. It's messy, but well, what can we do? */ * let's fork here. It's messy, but well, what can we do? */
r = unit_fork_helper_process(UNIT(s), &pid); r = unit_fork_helper_process(UNIT(s), "(sd-chown)", &pid);
if (r < 0) if (r < 0)
return r; return r;
if (r == 0) { if (r == 0) {
@@ -2871,7 +2871,7 @@ static int socket_accept_in_cgroup(Socket *s, SocketPort *p, int fd) {
if (socketpair(AF_UNIX, SOCK_SEQPACKET|SOCK_CLOEXEC, 0, pair) < 0) if (socketpair(AF_UNIX, SOCK_SEQPACKET|SOCK_CLOEXEC, 0, pair) < 0)
return log_unit_error_errno(UNIT(s), errno, "Failed to create communication channel: %m"); return log_unit_error_errno(UNIT(s), errno, "Failed to create communication channel: %m");
r = unit_fork_helper_process(UNIT(s), &pid); r = unit_fork_helper_process(UNIT(s), "(sd-accept)", &pid);
if (r < 0) if (r < 0)
return log_unit_error_errno(UNIT(s), r, "Failed to fork off accept stub process: %m"); return log_unit_error_errno(UNIT(s), r, "Failed to fork off accept stub process: %m");
if (r == 0) { if (r == 0) {
+8 -10
View File
@@ -388,11 +388,10 @@ static int remount_with_timeout(MountPoint *m, char *options, int *n_failed) {
* fork a child process and set a timeout. If the timeout * fork a child process and set a timeout. If the timeout
* lapses, the assumption is that that particular remount * lapses, the assumption is that that particular remount
* failed. */ * failed. */
pid = fork(); r = safe_fork("(sd-remount)", FORK_RESET_SIGNALS|FORK_CLOSE_ALL_FDS, &pid);
if (pid < 0) if (r < 0)
return log_error_errno(errno, "Failed to fork: %m"); return log_error_errno(r, "Failed to fork: %m");
if (r == 0) {
if (pid == 0) {
log_info("Remounting '%s' read-only in with options '%s'.", m->path, options); log_info("Remounting '%s' read-only in with options '%s'.", m->path, options);
/* Start the mount operation here in the child */ /* Start the mount operation here in the child */
@@ -423,11 +422,10 @@ static int umount_with_timeout(MountPoint *m, bool *changed) {
* fork a child process and set a timeout. If the timeout * fork a child process and set a timeout. If the timeout
* lapses, the assumption is that that particular umount * lapses, the assumption is that that particular umount
* failed. */ * failed. */
pid = fork(); r = safe_fork("(sd-umount)", FORK_RESET_SIGNALS|FORK_CLOSE_ALL_FDS, &pid);
if (pid < 0) if (r < 0)
return log_error_errno(errno, "Failed to fork: %m"); return log_error_errno(r, "Failed to fork: %m");
if (r == 0) {
if (pid == 0) {
log_info("Unmounting '%s'.", m->path); log_info("Unmounting '%s'.", m->path);
/* Start the mount operation here in the child Using MNT_FORCE /* Start the mount operation here in the child Using MNT_FORCE
+14 -22
View File
@@ -21,6 +21,7 @@
#include <errno.h> #include <errno.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <sys/prctl.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <unistd.h> #include <unistd.h>
@@ -4968,8 +4969,7 @@ void unit_set_exec_params(Unit *u, ExecParameters *p) {
SET_FLAG(p->flags, EXEC_CGROUP_DELEGATE, UNIT_CGROUP_BOOL(u, delegate)); SET_FLAG(p->flags, EXEC_CGROUP_DELEGATE, UNIT_CGROUP_BOOL(u, delegate));
} }
int unit_fork_helper_process(Unit *u, pid_t *ret) { int unit_fork_helper_process(Unit *u, const char *name, pid_t *ret) {
pid_t pid;
int r; int r;
assert(u); assert(u);
@@ -4980,32 +4980,24 @@ int unit_fork_helper_process(Unit *u, pid_t *ret) {
(void) unit_realize_cgroup(u); (void) unit_realize_cgroup(u);
pid = fork(); r = safe_fork(name, FORK_REOPEN_LOG, ret);
if (pid < 0) if (r != 0)
return -errno; return r;
if (pid == 0) { (void) default_signals(SIGNALS_CRASH_HANDLER, SIGNALS_IGNORE, -1);
(void) ignore_signals(SIGPIPE, -1);
(void) default_signals(SIGNALS_CRASH_HANDLER, SIGNALS_IGNORE, -1); (void) prctl(PR_SET_PDEATHSIG, SIGTERM);
(void) ignore_signals(SIGPIPE, -1);
log_close(); if (u->cgroup_path) {
log_open(); r = cg_attach_everywhere(u->manager->cgroup_supported, u->cgroup_path, 0, NULL, NULL);
if (r < 0) {
if (u->cgroup_path) { log_unit_error_errno(u, r, "Failed to join unit cgroup %s: %m", u->cgroup_path);
r = cg_attach_everywhere(u->manager->cgroup_supported, u->cgroup_path, 0, NULL, NULL); _exit(EXIT_CGROUP);
if (r < 0) {
log_unit_error_errno(u, r, "Failed to join unit cgroup %s: %m", u->cgroup_path);
_exit(EXIT_CGROUP);
}
} }
*ret = getpid_cached();
return 0;
} }
*ret = pid; return 0;
return 1;
} }
static void unit_update_dependency_mask(Unit *u, UnitDependency d, Unit *other, UnitDependencyInfo di) { static void unit_update_dependency_mask(Unit *u, UnitDependency d, Unit *other, UnitDependencyInfo di) {
+1 -1
View File
@@ -782,7 +782,7 @@ bool unit_shall_confirm_spawn(Unit *u);
void unit_set_exec_params(Unit *s, ExecParameters *p); void unit_set_exec_params(Unit *s, ExecParameters *p);
int unit_fork_helper_process(Unit *u, pid_t *ret); int unit_fork_helper_process(Unit *u, const char *name, pid_t *ret);
void unit_remove_dependencies(Unit *u, UnitDependencyMask mask); void unit_remove_dependencies(Unit *u, UnitDependencyMask mask);
+4 -8
View File
@@ -928,17 +928,13 @@ static int run_gdb(sd_journal *j) {
/* Don't interfere with gdb and its handling of SIGINT. */ /* Don't interfere with gdb and its handling of SIGINT. */
(void) ignore_signals(SIGINT, -1); (void) ignore_signals(SIGINT, -1);
pid = fork(); r = safe_fork("(gdb)", FORK_RESET_SIGNALS|FORK_DEATHSIG|FORK_CLOSE_ALL_FDS, &pid);
if (pid < 0) { if (r < 0) {
r = log_error_errno(errno, "Failed to fork(): %m"); log_error_errno(r, "Failed to fork(): %m");
goto finish; goto finish;
} }
if (pid == 0) { if (r == 0) {
(void) reset_all_signal_handlers();
(void) reset_signal_mask();
execlp("gdb", "gdb", exe, path, NULL); execlp("gdb", "gdb", exe, path, NULL);
log_error_errno(errno, "Failed to invoke gdb: %m"); log_error_errno(errno, "Failed to invoke gdb: %m");
_exit(1); _exit(1);
} }
+10 -15
View File
@@ -161,8 +161,8 @@ static int notify_override_unchanged(const char *f) {
static int found_override(const char *top, const char *bottom) { static int found_override(const char *top, const char *bottom) {
_cleanup_free_ char *dest = NULL; _cleanup_free_ char *dest = NULL;
int k;
pid_t pid; pid_t pid;
int r;
assert(top); assert(top);
assert(bottom); assert(bottom);
@@ -170,31 +170,26 @@ static int found_override(const char *top, const char *bottom) {
if (null_or_empty_path(top) > 0) if (null_or_empty_path(top) > 0)
return notify_override_masked(top, bottom); return notify_override_masked(top, bottom);
k = readlink_malloc(top, &dest); r = readlink_malloc(top, &dest);
if (k >= 0) { if (r >= 0) {
if (equivalent(dest, bottom) > 0) if (equivalent(dest, bottom) > 0)
return notify_override_equivalent(top, bottom); return notify_override_equivalent(top, bottom);
else else
return notify_override_redirected(top, bottom); return notify_override_redirected(top, bottom);
} }
k = notify_override_overridden(top, bottom); r = notify_override_overridden(top, bottom);
if (!arg_diff) if (!arg_diff)
return k; return r;
putchar('\n'); putchar('\n');
fflush(stdout); fflush(stdout);
pid = fork(); r = safe_fork("(diff)", FORK_RESET_SIGNALS|FORK_DEATHSIG|FORK_CLOSE_ALL_FDS, &pid);
if (pid < 0) if (r < 0)
return log_error_errno(errno, "Failed to fork off diff: %m"); return log_error_errno(pid, "Failed to fork off diff: %m");
else if (pid == 0) { if (r == 0) {
(void) reset_all_signal_handlers();
(void) reset_signal_mask();
assert_se(prctl(PR_SET_PDEATHSIG, SIGTERM) == 0);
execlp("diff", "diff", "-us", "--", bottom, top, NULL); execlp("diff", "diff", "-us", "--", bottom, top, NULL);
log_error_errno(errno, "Failed to execute diff: %m"); log_error_errno(errno, "Failed to execute diff: %m");
_exit(EXIT_FAILURE); _exit(EXIT_FAILURE);
@@ -203,7 +198,7 @@ static int found_override(const char *top, const char *bottom) {
wait_for_terminate_and_warn("diff", pid, false); wait_for_terminate_and_warn("diff", pid, false);
putchar('\n'); putchar('\n');
return k; return r;
} }
static int enumerate_dir_d( static int enumerate_dir_d(
+4 -8
View File
@@ -392,12 +392,12 @@ int main(int argc, char *argv[]) {
} }
} }
pid = fork(); r = safe_fork("(fsck)", FORK_RESET_SIGNALS|FORK_DEATHSIG, &pid);
if (pid < 0) { if (r < 0) {
r = log_error_errno(errno, "fork(): %m"); log_error_errno(r, "fork(): %m");
goto finish; goto finish;
} }
if (pid == 0) { if (r == 0) {
char dash_c[STRLEN("-C") + DECIMAL_STR_MAX(int) + 1]; char dash_c[STRLEN("-C") + DECIMAL_STR_MAX(int) + 1];
int progress_socket = -1; int progress_socket = -1;
const char *cmdline[9]; const char *cmdline[9];
@@ -405,10 +405,6 @@ int main(int argc, char *argv[]) {
/* Child */ /* Child */
(void) reset_all_signal_handlers();
(void) reset_signal_mask();
assert_se(prctl(PR_SET_PDEATHSIG, SIGTERM) == 0);
/* Close the reading side of the progress pipe */ /* Close the reading side of the progress pipe */
progress_pipe[0] = safe_close(progress_pipe[0]); progress_pipe[0] = safe_close(progress_pipe[0]);
+8 -18
View File
@@ -82,11 +82,10 @@ int import_fork_tar_x(const char *path, pid_t *ret) {
if (pipe2(pipefd, O_CLOEXEC) < 0) if (pipe2(pipefd, O_CLOEXEC) < 0)
return log_error_errno(errno, "Failed to create pipe for tar: %m"); return log_error_errno(errno, "Failed to create pipe for tar: %m");
pid = fork(); r = safe_fork("(tar)", FORK_RESET_SIGNALS|FORK_DEATHSIG, &pid);
if (pid < 0) if (r < 0)
return log_error_errno(errno, "Failed to fork off tar: %m"); return log_error_errno(r, "Failed to fork off tar: %m");
if (r == 0) {
if (pid == 0) {
int null_fd; int null_fd;
uint64_t retain = uint64_t retain =
(1ULL << CAP_CHOWN) | (1ULL << CAP_CHOWN) |
@@ -98,10 +97,6 @@ int import_fork_tar_x(const char *path, pid_t *ret) {
/* Child */ /* Child */
(void) reset_all_signal_handlers();
(void) reset_signal_mask();
assert_se(prctl(PR_SET_PDEATHSIG, SIGTERM) == 0);
pipefd[1] = safe_close(pipefd[1]); pipefd[1] = safe_close(pipefd[1]);
r = move_fd(pipefd[0], STDIN_FILENO, false); r = move_fd(pipefd[0], STDIN_FILENO, false);
@@ -156,20 +151,15 @@ int import_fork_tar_c(const char *path, pid_t *ret) {
if (pipe2(pipefd, O_CLOEXEC) < 0) if (pipe2(pipefd, O_CLOEXEC) < 0)
return log_error_errno(errno, "Failed to create pipe for tar: %m"); return log_error_errno(errno, "Failed to create pipe for tar: %m");
pid = fork(); r = safe_fork("(tar)", FORK_RESET_SIGNALS|FORK_DEATHSIG, &pid);
if (pid < 0) if (r < 0)
return log_error_errno(errno, "Failed to fork off tar: %m"); return log_error_errno(r, "Failed to fork off tar: %m");
if (r == 0) {
if (pid == 0) {
int null_fd; int null_fd;
uint64_t retain = (1ULL << CAP_DAC_OVERRIDE); uint64_t retain = (1ULL << CAP_DAC_OVERRIDE);
/* Child */ /* Child */
(void) reset_all_signal_handlers();
(void) reset_signal_mask();
assert_se(prctl(PR_SET_PDEATHSIG, SIGTERM) == 0);
pipefd[0] = safe_close(pipefd[0]); pipefd[0] = safe_close(pipefd[0]);
r = move_fd(pipefd[1], STDOUT_FILENO, false); r = move_fd(pipefd[1], STDOUT_FILENO, false);
+4 -8
View File
@@ -371,10 +371,10 @@ static int transfer_start(Transfer *t) {
if (pipe2(pipefd, O_CLOEXEC) < 0) if (pipe2(pipefd, O_CLOEXEC) < 0)
return -errno; return -errno;
t->pid = fork(); r = safe_fork("(sd-transfer)", FORK_RESET_SIGNALS|FORK_DEATHSIG, &t->pid);
if (t->pid < 0) if (r < 0)
return -errno; return r;
if (t->pid == 0) { if (r == 0) {
const char *cmd[] = { const char *cmd[] = {
NULL, /* systemd-import, systemd-export or systemd-pull */ NULL, /* systemd-import, systemd-export or systemd-pull */
NULL, /* tar, raw */ NULL, /* tar, raw */
@@ -393,10 +393,6 @@ static int transfer_start(Transfer *t) {
/* Child */ /* Child */
(void) reset_all_signal_handlers();
(void) reset_signal_mask();
assert_se(prctl(PR_SET_PDEATHSIG, SIGTERM) == 0);
pipefd[0] = safe_close(pipefd[0]); pipefd[0] = safe_close(pipefd[0]);
if (dup2(pipefd[1], STDERR_FILENO) != STDERR_FILENO) { if (dup2(pipefd[1], STDERR_FILENO) != STDERR_FILENO) {
+4 -8
View File
@@ -463,10 +463,10 @@ int pull_verify(PullJob *main_job,
gpg_home_created = true; gpg_home_created = true;
pid = fork(); r = safe_fork("(gpg)", FORK_RESET_SIGNALS|FORK_DEATHSIG, &pid);
if (pid < 0) if (r < 0)
return log_error_errno(errno, "Failed to fork off gpg: %m"); return log_error_errno(r, "Failed to fork off gpg: %m");
if (pid == 0) { if (r == 0) {
const char *cmd[] = { const char *cmd[] = {
"gpg", "gpg",
"--no-options", "--no-options",
@@ -487,10 +487,6 @@ int pull_verify(PullJob *main_job,
/* Child */ /* Child */
(void) reset_all_signal_handlers();
(void) reset_signal_mask();
assert_se(prctl(PR_SET_PDEATHSIG, SIGTERM) == 0);
gpg_pipe[1] = safe_close(gpg_pipe[1]); gpg_pipe[1] = safe_close(gpg_pipe[1]);
r = move_fd(gpg_pipe[0], STDIN_FILENO, false); r = move_fd(gpg_pipe[0], STDIN_FILENO, false);
+6 -22
View File
@@ -81,27 +81,20 @@ static bool arg_trust_all = false;
**********************************************************************/ **********************************************************************/
static int spawn_child(const char* child, char** argv) { static int spawn_child(const char* child, char** argv) {
int fd[2]; pid_t child_pid;
pid_t parent_pid, child_pid; int fd[2], r;
int r;
if (pipe(fd) < 0) if (pipe(fd) < 0)
return log_error_errno(errno, "Failed to create pager pipe: %m"); return log_error_errno(errno, "Failed to create pager pipe: %m");
parent_pid = getpid_cached(); r = safe_fork("(remote)", FORK_RESET_SIGNALS|FORK_DEATHSIG, &child_pid);
if (r < 0) {
child_pid = fork();
if (child_pid < 0) {
r = log_error_errno(errno, "Failed to fork: %m");
safe_close_pair(fd); safe_close_pair(fd);
return r; return log_error_errno(r, "Failed to fork: %m");
} }
/* In the child */ /* In the child */
if (child_pid == 0) { if (r == 0) {
(void) reset_all_signal_handlers();
(void) reset_signal_mask();
r = dup2(fd[1], STDOUT_FILENO); r = dup2(fd[1], STDOUT_FILENO);
if (r < 0) { if (r < 0) {
@@ -111,15 +104,6 @@ static int spawn_child(const char* child, char** argv) {
safe_close_pair(fd); safe_close_pair(fd);
/* Make sure the child goes away when the parent dies */
if (prctl(PR_SET_PDEATHSIG, SIGTERM) < 0)
_exit(EXIT_FAILURE);
/* Check whether our parent died before we were able
* to set the death signal */
if (getppid() != parent_pid)
_exit(EXIT_SUCCESS);
execvp(child, argv); execvp(child, argv);
log_error_errno(errno, "Failed to exec child %s: %m", child); log_error_errno(errno, "Failed to exec child %s: %m", child);
_exit(EXIT_FAILURE); _exit(EXIT_FAILURE);
+7 -9
View File
@@ -62,11 +62,10 @@ int bus_container_connect_socket(sd_bus *b) {
if (socketpair(AF_UNIX, SOCK_SEQPACKET, 0, pair) < 0) if (socketpair(AF_UNIX, SOCK_SEQPACKET, 0, pair) < 0)
return -errno; return -errno;
child = fork(); r = safe_fork("(sd-buscntr)", FORK_RESET_SIGNALS|FORK_DEATHSIG, &child);
if (child < 0) if (r < 0)
return -errno; return r;
if (r == 0) {
if (child == 0) {
pid_t grandchild; pid_t grandchild;
pair[0] = safe_close(pair[0]); pair[0] = safe_close(pair[0]);
@@ -82,11 +81,10 @@ int bus_container_connect_socket(sd_bus *b) {
* comes from a process from within the container, and * comes from a process from within the container, and
* not outside of it */ * not outside of it */
grandchild = fork(); r = safe_fork("(sd-buscntr2)", FORK_RESET_SIGNALS|FORK_DEATHSIG, &grandchild);
if (grandchild < 0) if (r < 0)
_exit(EXIT_FAILURE); _exit(EXIT_FAILURE);
if (r == 0) {
if (grandchild == 0) {
r = connect(b->input_fd, &b->sockaddr.sa, b->sockaddr_size); r = connect(b->input_fd, &b->sockaddr.sa, b->sockaddr_size);
if (r < 0) { if (r < 0) {
+4 -9
View File
@@ -715,19 +715,14 @@ int bus_socket_exec(sd_bus *b) {
if (r < 0) if (r < 0)
return -errno; return -errno;
pid = fork(); r = safe_fork_full("(sd-busexec)", s+1, 1, FORK_RESET_SIGNALS|FORK_CLOSE_ALL_FDS, &pid);
if (pid < 0) { if (r < 0) {
safe_close_pair(s); safe_close_pair(s);
return -errno; return r;
} }
if (pid == 0) { if (r == 0) {
/* Child */ /* Child */
(void) reset_all_signal_handlers();
(void) reset_signal_mask();
close_all_fds(s+1, 1);
assert_se(dup3(s[1], STDIN_FILENO, 0) == STDIN_FILENO); assert_se(dup3(s[1], STDIN_FILENO, 0) == STDIN_FILENO);
assert_se(dup3(s[1], STDOUT_FILENO, 0) == STDOUT_FILENO); assert_se(dup3(s[1], STDOUT_FILENO, 0) == STDOUT_FILENO);
+4 -11
View File
@@ -266,20 +266,13 @@ int main(int argc, char *argv[]) {
return EXIT_FAILURE; return EXIT_FAILURE;
} }
pid = fork(); r = safe_fork("(inhibit)", FORK_RESET_SIGNALS|FORK_DEATHSIG|FORK_CLOSE_ALL_FDS, &pid);
if (pid < 0) { if (r < 0) {
log_error_errno(errno, "Failed to fork: %m"); log_error_errno(r, "Failed to fork: %m");
return EXIT_FAILURE; return EXIT_FAILURE;
} }
if (r == 0) {
if (pid == 0) {
/* Child */ /* Child */
(void) reset_all_signal_handlers();
(void) reset_signal_mask();
close_all_fds(NULL, 0);
execvp(argv[optind], argv + optind); execvp(argv[optind], argv + optind);
log_error_errno(errno, "Failed to execute %s: %m", argv[optind]); log_error_errno(errno, "Failed to execute %s: %m", argv[optind]);
_exit(EXIT_FAILURE); _exit(EXIT_FAILURE);
+8 -8
View File
@@ -75,10 +75,10 @@ int bus_image_method_remove(
if (pipe2(errno_pipe_fd, O_CLOEXEC|O_NONBLOCK) < 0) if (pipe2(errno_pipe_fd, O_CLOEXEC|O_NONBLOCK) < 0)
return sd_bus_error_set_errnof(error, errno, "Failed to create pipe: %m"); return sd_bus_error_set_errnof(error, errno, "Failed to create pipe: %m");
child = fork(); r = safe_fork("(sd-imgrm)", FORK_RESET_SIGNALS, &child);
if (child < 0) if (r < 0)
return sd_bus_error_set_errnof(error, errno, "Failed to fork(): %m"); return sd_bus_error_set_errnof(error, r, "Failed to fork(): %m");
if (child == 0) { if (r == 0) {
errno_pipe_fd[0] = safe_close(errno_pipe_fd[0]); errno_pipe_fd[0] = safe_close(errno_pipe_fd[0]);
r = image_remove(image); r = image_remove(image);
@@ -187,10 +187,10 @@ int bus_image_method_clone(
if (pipe2(errno_pipe_fd, O_CLOEXEC|O_NONBLOCK) < 0) if (pipe2(errno_pipe_fd, O_CLOEXEC|O_NONBLOCK) < 0)
return sd_bus_error_set_errnof(error, errno, "Failed to create pipe: %m"); return sd_bus_error_set_errnof(error, errno, "Failed to create pipe: %m");
child = fork(); r = safe_fork("(imgclone)", FORK_RESET_SIGNALS, &child);
if (child < 0) if (r < 0)
return sd_bus_error_set_errnof(error, errno, "Failed to fork(): %m"); return sd_bus_error_set_errnof(error, r, "Failed to fork(): %m");
if (child == 0) { if (r == 0) {
errno_pipe_fd[0] = safe_close(errno_pipe_fd[0]); errno_pipe_fd[0] = safe_close(errno_pipe_fd[0]);
r = image_clone(image, new_name, read_only); r = image_clone(image, new_name, read_only);
+20 -25
View File
@@ -250,11 +250,10 @@ int bus_machine_method_get_addresses(sd_bus_message *message, void *userdata, sd
if (socketpair(AF_UNIX, SOCK_SEQPACKET, 0, pair) < 0) if (socketpair(AF_UNIX, SOCK_SEQPACKET, 0, pair) < 0)
return -errno; return -errno;
child = fork(); r = safe_fork("(sd-addr)", FORK_RESET_SIGNALS|FORK_DEATHSIG, &child);
if (child < 0) if (r < 0)
return sd_bus_error_set_errnof(error, errno, "Failed to fork(): %m"); return sd_bus_error_set_errnof(error, r, "Failed to fork(): %m");
if (r == 0) {
if (child == 0) {
_cleanup_free_ struct local_address *addresses = NULL; _cleanup_free_ struct local_address *addresses = NULL;
struct local_address *a; struct local_address *a;
int i, n; int i, n;
@@ -390,11 +389,10 @@ int bus_machine_method_get_os_release(sd_bus_message *message, void *userdata, s
if (socketpair(AF_UNIX, SOCK_SEQPACKET, 0, pair) < 0) if (socketpair(AF_UNIX, SOCK_SEQPACKET, 0, pair) < 0)
return -errno; return -errno;
child = fork(); r = safe_fork("(sd-osrel)", FORK_RESET_SIGNALS|FORK_DEATHSIG, &child);
if (child < 0) if (r < 0)
return sd_bus_error_set_errnof(error, errno, "Failed to fork(): %m"); return sd_bus_error_set_errnof(error, r, "Failed to fork(): %m");
if (r == 0) {
if (child == 0) {
int fd = -1; int fd = -1;
pair[0] = safe_close(pair[0]); pair[0] = safe_close(pair[0]);
@@ -997,13 +995,12 @@ int bus_machine_method_bind_mount(sd_bus_message *message, void *userdata, sd_bu
goto finish; goto finish;
} }
child = fork(); r = safe_fork("(sd-bindmnt)", FORK_RESET_SIGNALS, &child);
if (child < 0) { if (r < 0) {
r = sd_bus_error_set_errnof(error, errno, "Failed to fork(): %m"); sd_bus_error_set_errnof(error, r, "Failed to fork(): %m");
goto finish; goto finish;
} }
if (r == 0) {
if (child == 0) {
const char *mount_inside; const char *mount_inside;
int mntfd; int mntfd;
const char *q; const char *q;
@@ -1172,11 +1169,10 @@ int bus_machine_method_copy(sd_bus_message *message, void *userdata, sd_bus_erro
if (pipe2(errno_pipe_fd, O_CLOEXEC|O_NONBLOCK) < 0) if (pipe2(errno_pipe_fd, O_CLOEXEC|O_NONBLOCK) < 0)
return sd_bus_error_set_errnof(error, errno, "Failed to create pipe: %m"); return sd_bus_error_set_errnof(error, errno, "Failed to create pipe: %m");
child = fork(); r = safe_fork("(sd-copy)", FORK_RESET_SIGNALS, &child);
if (child < 0) if (r < 0)
return sd_bus_error_set_errnof(error, errno, "Failed to fork(): %m"); return sd_bus_error_set_errnof(error, r, "Failed to fork(): %m");
if (r == 0) {
if (child == 0) {
int containerfd; int containerfd;
const char *q; const char *q;
int mntfd; int mntfd;
@@ -1282,11 +1278,10 @@ int bus_machine_method_open_root_directory(sd_bus_message *message, void *userda
if (socketpair(AF_UNIX, SOCK_DGRAM, 0, pair) < 0) if (socketpair(AF_UNIX, SOCK_DGRAM, 0, pair) < 0)
return -errno; return -errno;
child = fork(); r = safe_fork("(sd-openroot)", FORK_RESET_SIGNALS|FORK_DEATHSIG, &child);
if (child < 0) if (r < 0)
return sd_bus_error_set_errnof(error, errno, "Failed to fork(): %m"); return sd_bus_error_set_errnof(error, r, "Failed to fork(): %m");
if (r == 0) {
if (child == 0) {
_cleanup_close_ int dfd = -1; _cleanup_close_ int dfd = -1;
pair[0] = safe_close(pair[0]); pair[0] = safe_close(pair[0]);
+4 -5
View File
@@ -1078,11 +1078,10 @@ static int method_clean_pool(sd_bus_message *message, void *userdata, sd_bus_err
return -errno; return -errno;
/* This might be a slow operation, run it asynchronously in a background process */ /* This might be a slow operation, run it asynchronously in a background process */
child = fork(); r = safe_fork("(sd-clean)", FORK_RESET_SIGNALS, &child);
if (child < 0) if (r < 0)
return sd_bus_error_set_errnof(error, errno, "Failed to fork(): %m"); return sd_bus_error_set_errnof(error, r, "Failed to fork(): %m");
if (r == 0) {
if (child == 0) {
_cleanup_(image_hashmap_freep) Hashmap *images = NULL; _cleanup_(image_hashmap_freep) Hashmap *images = NULL;
bool success = true; bool success = true;
Image *image; Image *image;
+5 -7
View File
@@ -33,7 +33,7 @@
#include "util.h" #include "util.h"
static int spawn_getent(const char *database, const char *key, pid_t *rpid) { 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; pid_t pid;
assert(database); 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) if (pipe2(pipe_fds, O_CLOEXEC) < 0)
return log_error_errno(errno, "Failed to allocate pipe: %m"); return log_error_errno(errno, "Failed to allocate pipe: %m");
pid = fork(); r = safe_fork("(getent)", FORK_RESET_SIGNALS|FORK_DEATHSIG, &pid);
if (pid < 0) if (r < 0)
return log_error_errno(errno, "Failed to fork getent child: %m"); return log_error_errno(r, "Failed to fork getent child: %m");
else if (pid == 0) { if (r == 0) {
int nullfd; int nullfd;
char *empty_env = NULL; char *empty_env = NULL;
@@ -71,8 +71,6 @@ static int spawn_getent(const char *database, const char *key, pid_t *rpid) {
if (nullfd > 2) if (nullfd > 2)
safe_close(nullfd); safe_close(nullfd);
(void) reset_all_signal_handlers();
(void) reset_signal_mask();
close_all_fds(NULL, 0); close_all_fds(NULL, 0);
execle("/usr/bin/getent", "getent", database, key, NULL, &empty_env); execle("/usr/bin/getent", "getent", database, key, NULL, &empty_env);
+1 -1
View File
@@ -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); sd_id128_to_string(uuid, new_environment + sizeof(new_environment) - SD_ID128_STRING_MAX);
reset_environ(new_environment, sizeof(new_environment)); reset_environ(new_environment, sizeof(new_environment));
rename_process("STUBINIT"); (void) rename_process("(sd-stubinit)");
assert_se(sigemptyset(&waitmask) >= 0); assert_se(sigemptyset(&waitmask) >= 0);
assert_se(sigset_add_many(&waitmask, assert_se(sigset_add_many(&waitmask,
+5 -9
View File
@@ -34,6 +34,7 @@
static int makefs(const char *type, const char *device) { static int makefs(const char *type, const char *device) {
const char *mkfs; const char *mkfs;
pid_t pid; pid_t pid;
int r;
if (streq(type, "swap")) if (streq(type, "swap"))
mkfs = "/sbin/mkswap"; mkfs = "/sbin/mkswap";
@@ -42,19 +43,14 @@ static int makefs(const char *type, const char *device) {
if (access(mkfs, X_OK) != 0) if (access(mkfs, X_OK) != 0)
return log_error_errno(errno, "%s is not executable: %m", mkfs); return log_error_errno(errno, "%s is not executable: %m", mkfs);
pid = fork(); r = safe_fork("(fsck)", FORK_RESET_SIGNALS|FORK_DEATHSIG, &pid);
if (pid < 0) if (r < 0)
return log_error_errno(errno, "fork(): %m"); return log_error_errno(r, "fork(): %m");
if (r == 0) {
if (pid == 0) {
const char *cmdline[3] = { mkfs, device, NULL }; const char *cmdline[3] = { mkfs, device, NULL };
/* Child */ /* Child */
(void) reset_all_signal_handlers();
(void) reset_signal_mask();
assert_se(prctl(PR_SET_PDEATHSIG, SIGTERM) == 0);
execv(cmdline[0], (char**) cmdline); execv(cmdline[0], (char**) cmdline);
_exit(EXIT_FAILURE); _exit(EXIT_FAILURE);
} }
+4 -8
View File
@@ -106,19 +106,15 @@ int main(int argc, char *argv[]) {
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }
pid = fork(); r = safe_fork("(quotacheck)", FORK_RESET_SIGNALS|FORK_DEATHSIG, &pid);
if (pid < 0) { if (r < 0) {
r = log_error_errno(errno, "fork(): %m"); log_error_errno(r, "fork(): %m");
goto finish; goto finish;
} }
if (pid == 0) { if (r == 0) {
/* Child */ /* Child */
(void) reset_all_signal_handlers();
(void) reset_signal_mask();
assert_se(prctl(PR_SET_PDEATHSIG, SIGTERM) == 0);
execv(cmdline[0], (char**) cmdline); execv(cmdline[0], (char**) cmdline);
_exit(1); /* Operational error */ _exit(1); /* Operational error */
} }
+4 -9
View File
@@ -87,19 +87,14 @@ int main(int argc, char *argv[]) {
log_debug("Remounting %s", me->mnt_dir); log_debug("Remounting %s", me->mnt_dir);
pid = fork(); r = safe_fork("(remount)", FORK_RESET_SIGNALS|FORK_DEATHSIG, &pid);
if (pid < 0) { if (r < 0) {
r = log_error_errno(errno, "Failed to fork: %m"); log_error_errno(r, "Failed to fork: %m");
goto finish; goto finish;
} }
if (r == 0) {
if (pid == 0) {
/* Child */ /* Child */
(void) reset_all_signal_handlers();
(void) reset_signal_mask();
(void) prctl(PR_SET_PDEATHSIG, SIGTERM);
execv(MOUNT_PATH, STRV_MAKE(MOUNT_PATH, me->mnt_dir, "-o", "remount")); execv(MOUNT_PATH, STRV_MAKE(MOUNT_PATH, me->mnt_dir, "-o", "remount"));
log_error_errno(errno, "Failed to execute " MOUNT_PATH ": %m"); log_error_errno(errno, "Failed to execute " MOUNT_PATH ": %m");
+4 -5
View File
@@ -1278,11 +1278,10 @@ static int get_boot_id_for_machine(const char *machine, sd_id128_t *boot_id) {
if (socketpair(AF_UNIX, SOCK_DGRAM, 0, pair) < 0) if (socketpair(AF_UNIX, SOCK_DGRAM, 0, pair) < 0)
return -errno; return -errno;
child = fork(); r = safe_fork("(sd-bootid)", FORK_RESET_SIGNALS|FORK_DEATHSIG, &child);
if (child < 0) if (r < 0)
return -errno; return r;
if (r == 0) {
if (child == 0) {
int fd; int fd;
pair[0] = safe_close(pair[0]); pair[0] = safe_close(pair[0]);
+4 -9
View File
@@ -123,20 +123,15 @@ static int setup_machine_raw(uint64_t size, sd_bus_error *error) {
goto fail; goto fail;
} }
pid = fork(); r = safe_fork("(mkfs)", FORK_RESET_SIGNALS|FORK_DEATHSIG, &pid);
if (pid < 0) { if (r < 0) {
r = sd_bus_error_set_errnof(error, errno, "Failed to fork mkfs.btrfs: %m"); sd_bus_error_set_errnof(error, r, "Failed to fork mkfs.btrfs: %m");
goto fail; goto fail;
} }
if (r == 0) {
if (pid == 0) {
/* Child */ /* Child */
(void) reset_all_signal_handlers();
(void) reset_signal_mask();
assert_se(prctl(PR_SET_PDEATHSIG, SIGTERM) == 0);
fd = safe_close(fd); fd = safe_close(fd);
execlp("mkfs.btrfs", "-Lvar-lib-machines", tmp, NULL); execlp("mkfs.btrfs", "-Lvar-lib-machines", tmp, NULL);
+10 -37
View File
@@ -62,7 +62,7 @@ static bool stderr_redirected = false;
int pager_open(bool no_pager, bool jump_to_end) { int pager_open(bool no_pager, bool jump_to_end) {
_cleanup_close_pair_ int fd[2] = { -1, -1 }; _cleanup_close_pair_ int fd[2] = { -1, -1 };
const char *pager; const char *pager;
pid_t parent_pid; int r;
if (no_pager) if (no_pager)
return 0; return 0;
@@ -89,18 +89,13 @@ int pager_open(bool no_pager, bool jump_to_end) {
if (pipe2(fd, O_CLOEXEC) < 0) if (pipe2(fd, O_CLOEXEC) < 0)
return log_error_errno(errno, "Failed to create pager pipe: %m"); return log_error_errno(errno, "Failed to create pager pipe: %m");
parent_pid = getpid_cached(); r = safe_fork("(pager)", FORK_RESET_SIGNALS|FORK_DEATHSIG, &pager_pid);
if (r < 0)
pager_pid = fork(); return log_error_errno(r, "Failed to fork pager: %m");
if (pager_pid < 0) if (r == 0) {
return log_error_errno(errno, "Failed to fork pager: %m");
/* In the child start the pager */
if (pager_pid == 0) {
const char* less_opts, *less_charset; const char* less_opts, *less_charset;
(void) reset_all_signal_handlers(); /* In the child start the pager */
(void) reset_signal_mask();
(void) dup2(fd[0], STDIN_FILENO); (void) dup2(fd[0], STDIN_FILENO);
safe_close_pair(fd); safe_close_pair(fd);
@@ -124,15 +119,6 @@ int pager_open(bool no_pager, bool jump_to_end) {
setenv("LESSCHARSET", less_charset, 1) < 0) setenv("LESSCHARSET", less_charset, 1) < 0)
_exit(EXIT_FAILURE); _exit(EXIT_FAILURE);
/* Make sure the pager goes away when the parent dies */
if (prctl(PR_SET_PDEATHSIG, SIGTERM) < 0)
_exit(EXIT_FAILURE);
/* Check whether our parent died before we were able
* to set the death signal */
if (getppid() != parent_pid)
_exit(EXIT_SUCCESS);
if (pager) { if (pager) {
execlp(pager, pager, NULL); execlp(pager, pager, NULL);
execl("/bin/sh", "sh", "-c", pager, NULL); execl("/bin/sh", "sh", "-c", pager, NULL);
@@ -222,24 +208,11 @@ int show_man_page(const char *desc, bool null_stdio) {
} else } else
args[1] = desc; args[1] = desc;
pid = fork(); r = safe_fork("(man)", FORK_RESET_SIGNALS|FORK_DEATHSIG|(null_stdio ? FORK_NULL_STDIO : 0), &pid);
if (pid < 0) if (r < 0)
return log_error_errno(errno, "Failed to fork: %m"); return log_error_errno(r, "Failed to fork: %m");
if (r == 0) {
if (pid == 0) {
/* Child */ /* Child */
(void) reset_all_signal_handlers();
(void) reset_signal_mask();
if (null_stdio) {
r = make_null_stdio();
if (r < 0) {
log_error_errno(r, "Failed to kill stdio: %m");
_exit(EXIT_FAILURE);
}
}
execvp(args[0], (char**) args); execvp(args[0], (char**) args);
log_error_errno(errno, "Failed to execute man: %m"); log_error_errno(errno, "Failed to execute man: %m");
_exit(EXIT_FAILURE); _exit(EXIT_FAILURE);
+5 -10
View File
@@ -81,18 +81,13 @@ static int start_default_target(sd_bus *bus) {
static int fork_wait(const char* const cmdline[]) { static int fork_wait(const char* const cmdline[]) {
pid_t pid; pid_t pid;
int r;
pid = fork(); r = safe_fork("(sulogin)", FORK_RESET_SIGNALS|FORK_DEATHSIG, &pid);
if (pid < 0) if (r < 0)
return log_error_errno(errno, "fork(): %m"); return log_error_errno(r, "fork(): %m");
if (pid == 0) { if (r == 0) {
/* Child */ /* Child */
(void) reset_all_signal_handlers();
(void) reset_signal_mask();
assert_se(prctl(PR_SET_PDEATHSIG, SIGTERM) == 0);
execv(cmdline[0], (char**) cmdline); execv(cmdline[0], (char**) cmdline);
log_error_errno(errno, "Failed to execute %s: %m", cmdline[0]); log_error_errno(errno, "Failed to execute %s: %m", cmdline[0]);
_exit(EXIT_FAILURE); /* Operational error */ _exit(EXIT_FAILURE); /* Operational error */
+15 -27
View File
@@ -3539,10 +3539,10 @@ static int load_kexec_kernel(void) {
if (arg_dry_run) if (arg_dry_run)
return 0; return 0;
pid = fork(); r = safe_fork("(kexec)", FORK_RESET_SIGNALS|FORK_DEATHSIG, &pid);
if (pid < 0) if (r < 0)
return log_error_errno(errno, "Failed to fork: %m"); return log_error_errno(r, "Failed to fork: %m");
else if (pid == 0) { if (r == 0) {
const char* const args[] = { const char* const args[] = {
KEXEC, KEXEC,
@@ -3552,15 +3552,11 @@ static int load_kexec_kernel(void) {
NULL }; NULL };
/* Child */ /* Child */
(void) reset_all_signal_handlers();
(void) reset_signal_mask();
assert_se(prctl(PR_SET_PDEATHSIG, SIGTERM) == 0);
execv(args[0], (char * const *) args); execv(args[0], (char * const *) args);
_exit(EXIT_FAILURE); _exit(EXIT_FAILURE);
} else }
return wait_for_terminate_and_warn("kexec", pid, true);
return wait_for_terminate_and_warn("kexec", pid, true);
} }
static int set_exit_code(uint8_t code) { static int set_exit_code(uint8_t code) {
@@ -6123,15 +6119,11 @@ static int enable_sysv_units(const char *verb, char **args) {
if (!arg_quiet) if (!arg_quiet)
log_info("Executing: %s", l); log_info("Executing: %s", l);
pid = fork(); j = safe_fork("(sysv)", FORK_RESET_SIGNALS|FORK_DEATHSIG, &pid);
if (pid < 0) if (j < 0)
return log_error_errno(errno, "Failed to fork: %m"); return log_error_errno(j, "Failed to fork: %m");
else if (pid == 0) { if (j == 0) {
/* Child */ /* Child */
(void) reset_all_signal_handlers();
(void) reset_signal_mask();
execv(argv[0], (char**) argv); execv(argv[0], (char**) argv);
log_error_errno(errno, "Failed to execute %s: %m", argv[0]); log_error_errno(errno, "Failed to execute %s: %m", argv[0]);
_exit(EXIT_FAILURE); _exit(EXIT_FAILURE);
@@ -7000,20 +6992,16 @@ static int run_editor(char **paths) {
assert(paths); assert(paths);
pid = fork(); r = safe_fork("(editor)", FORK_RESET_SIGNALS|FORK_DEATHSIG, &pid);
if (pid < 0) if (r < 0)
return log_error_errno(errno, "Failed to fork: %m"); return log_error_errno(r, "Failed to fork: %m");
if (r == 0) {
if (pid == 0) {
const char **args; const char **args;
char *editor, **editor_args = NULL; char *editor, **editor_args = NULL;
char **tmp_path, **original_path, *p; char **tmp_path, **original_path, *p;
unsigned n_editor_args = 0, i = 1; unsigned n_editor_args = 0, i = 1;
size_t argc; size_t argc;
(void) reset_all_signal_handlers();
(void) reset_signal_mask();
argc = strv_length(paths)/2 + 1; argc = strv_length(paths)/2 + 1;
/* SYSTEMD_EDITOR takes precedence over EDITOR which takes precedence over VISUAL /* SYSTEMD_EDITOR takes precedence over EDITOR which takes precedence over VISUAL
+24
View File
@@ -38,6 +38,7 @@
#include "macro.h" #include "macro.h"
#include "parse-util.h" #include "parse-util.h"
#include "process-util.h" #include "process-util.h"
#include "signal-util.h"
#include "stdio-util.h" #include "stdio-util.h"
#include "string-util.h" #include "string-util.h"
#include "terminal-util.h" #include "terminal-util.h"
@@ -497,6 +498,28 @@ static void test_getpid_measure(void) {
log_info("getpid_cached(): %llu/s\n", (unsigned long long) (MEASURE_ITERATIONS*USEC_PER_SEC/q)); log_info("getpid_cached(): %llu/s\n", (unsigned long long) (MEASURE_ITERATIONS*USEC_PER_SEC/q));
} }
static void test_safe_fork(void) {
siginfo_t status;
pid_t pid;
int r;
BLOCK_SIGNALS(SIGCHLD);
r = safe_fork("(test-child)", FORK_RESET_SIGNALS|FORK_CLOSE_ALL_FDS|FORK_DEATHSIG|FORK_NULL_STDIO|FORK_REOPEN_LOG, &pid);
assert_se(r >= 0);
if (r == 0) {
/* child */
usleep(100 * USEC_PER_MSEC);
_exit(88);
}
assert_se(wait_for_terminate(pid, &status) >= 0);
assert_se(status.si_code == CLD_EXITED);
assert_se(status.si_status == 88);
}
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
log_set_max_level(LOG_DEBUG); log_set_max_level(LOG_DEBUG);
@@ -523,6 +546,7 @@ int main(int argc, char *argv[]) {
test_rename_process(); test_rename_process();
test_getpid_cached(); test_getpid_cached();
test_getpid_measure(); test_getpid_measure();
test_safe_fork();
return 0; return 0;
} }
@@ -693,11 +693,13 @@ static int parse_argv(int argc, char *argv[]) {
* If one of the tasks does handle a password, the remaining tasks * If one of the tasks does handle a password, the remaining tasks
* will be terminated. * will be terminated.
*/ */
static int ask_on_this_console(const char *tty, pid_t *pid, int argc, char *argv[]) { static int ask_on_this_console(const char *tty, pid_t *ret_pid, int argc, char *argv[]) {
struct sigaction sig = { struct sigaction sig = {
.sa_handler = nop_signal_handler, .sa_handler = nop_signal_handler,
.sa_flags = SA_NOCLDSTOP | SA_RESTART, .sa_flags = SA_NOCLDSTOP | SA_RESTART,
}; };
pid_t pid;
int r;
assert_se(sigprocmask_many(SIG_UNBLOCK, NULL, SIGHUP, SIGCHLD, -1) >= 0); assert_se(sigprocmask_many(SIG_UNBLOCK, NULL, SIGHUP, SIGCHLD, -1) >= 0);
@@ -707,18 +709,14 @@ static int ask_on_this_console(const char *tty, pid_t *pid, int argc, char *argv
sig.sa_handler = SIG_DFL; sig.sa_handler = SIG_DFL;
assert_se(sigaction(SIGHUP, &sig, NULL) >= 0); assert_se(sigaction(SIGHUP, &sig, NULL) >= 0);
*pid = fork(); r = safe_fork("(sd-passwd)", FORK_RESET_SIGNALS, &pid);
if (*pid < 0) if (r < 0)
return log_error_errno(errno, "Failed to fork process: %m"); return log_error_errno(r, "Failed to fork process: %m");
if (r == 0) {
if (*pid == 0) {
int ac; int ac;
assert_se(prctl(PR_SET_PDEATHSIG, SIGHUP) >= 0); assert_se(prctl(PR_SET_PDEATHSIG, SIGHUP) >= 0);
reset_signal_mask();
reset_all_signal_handlers();
for (ac = 0; ac < argc; ac++) { for (ac = 0; ac < argc; ac++) {
if (streq(argv[ac], "--console")) { if (streq(argv[ac], "--console")) {
argv[ac] = strjoina("--console=", tty); argv[ac] = strjoina("--console=", tty);
@@ -731,6 +729,8 @@ static int ask_on_this_console(const char *tty, pid_t *pid, int argc, char *argv
execv(SYSTEMD_TTY_ASK_PASSWORD_AGENT_BINARY_PATH, argv); execv(SYSTEMD_TTY_ASK_PASSWORD_AGENT_BINARY_PATH, argv);
_exit(EXIT_FAILURE); _exit(EXIT_FAILURE);
} }
*ret_pid = pid;
return 0; return 0;
} }
+16 -19
View File
@@ -774,10 +774,12 @@ int udev_event_spawn(struct udev_event *event,
} }
} }
pid = fork(); err = safe_fork("(spawn)", FORK_RESET_SIGNALS, &pid);
switch(pid) { if (err < 0) {
case 0: log_error_errno(err, "fork of '%s' failed: %m", cmd);
{ goto out;
}
if (err == 0) {
char arg[UTIL_PATH_SIZE]; char arg[UTIL_PATH_SIZE];
char *argv[128]; char *argv[128];
char program[UTIL_PATH_SIZE]; char program[UTIL_PATH_SIZE];
@@ -802,23 +804,18 @@ int udev_event_spawn(struct udev_event *event,
_exit(2); _exit(2);
} }
case -1:
log_error_errno(errno, "fork of '%s' failed: %m", cmd);
err = -1;
goto out;
default:
/* parent closed child's ends of pipes */
outpipe[WRITE_END] = safe_close(outpipe[WRITE_END]);
errpipe[WRITE_END] = safe_close(errpipe[WRITE_END]);
spawn_read(event, /* parent closed child's ends of pipes */
timeout_usec, outpipe[WRITE_END] = safe_close(outpipe[WRITE_END]);
cmd, errpipe[WRITE_END] = safe_close(errpipe[WRITE_END]);
outpipe[READ_END], errpipe[READ_END],
result, ressize);
err = spawn_wait(event, timeout_usec, timeout_warn_usec, cmd, pid, accept_failure); spawn_read(event,
} timeout_usec,
cmd,
outpipe[READ_END], errpipe[READ_END],
result, ressize);
err = spawn_wait(event, timeout_usec, timeout_warn_usec, cmd, pid, accept_failure);
out: out:
if (outpipe[READ_END] >= 0) if (outpipe[READ_END] >= 0)
+10 -16
View File
@@ -133,6 +133,7 @@ static int keyboard_load_and_wait(const char *vc, const char *map, const char *m
const char *args[8]; const char *args[8];
unsigned i = 0; unsigned i = 0;
pid_t pid; pid_t pid;
int r;
/* An empty map means kernel map */ /* An empty map means kernel map */
if (isempty(map)) if (isempty(map))
@@ -152,14 +153,10 @@ static int keyboard_load_and_wait(const char *vc, const char *map, const char *m
log_debug("Executing \"%s\"...", log_debug("Executing \"%s\"...",
strnull((cmd = strv_join((char**) args, " ")))); strnull((cmd = strv_join((char**) args, " "))));
pid = fork(); r = safe_fork("(loadkeys)", FORK_RESET_SIGNALS|FORK_CLOSE_ALL_FDS, &pid);
if (pid < 0) if (r < 0)
return log_error_errno(errno, "Failed to fork: %m"); return log_error_errno(r, "Failed to fork: %m");
else if (pid == 0) { if (r == 0) {
(void) reset_all_signal_handlers();
(void) reset_signal_mask();
execv(args[0], (char **) args); execv(args[0], (char **) args);
_exit(EXIT_FAILURE); _exit(EXIT_FAILURE);
} }
@@ -172,6 +169,7 @@ static int font_load_and_wait(const char *vc, const char *font, const char *map,
const char *args[9]; const char *args[9];
unsigned i = 0; unsigned i = 0;
pid_t pid; pid_t pid;
int r;
/* Any part can be set independently */ /* Any part can be set independently */
if (isempty(font) && isempty(map) && isempty(unimap)) if (isempty(font) && isempty(map) && isempty(unimap))
@@ -195,14 +193,10 @@ static int font_load_and_wait(const char *vc, const char *font, const char *map,
log_debug("Executing \"%s\"...", log_debug("Executing \"%s\"...",
strnull((cmd = strv_join((char**) args, " ")))); strnull((cmd = strv_join((char**) args, " "))));
pid = fork(); r = safe_fork("(setfont)", FORK_RESET_SIGNALS|FORK_CLOSE_ALL_FDS, &pid);
if (pid < 0) if (r < 0)
return log_error_errno(errno, "Failed to fork: %m"); return log_error_errno(r, "Failed to fork: %m");
else if (pid == 0) { if (r == 0) {
(void) reset_all_signal_handlers();
(void) reset_signal_mask();
execv(args[0], (char **) args); execv(args[0], (char **) args);
_exit(EXIT_FAILURE); _exit(EXIT_FAILURE);
} }