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
+15 -27
View File
@@ -3539,10 +3539,10 @@ static int load_kexec_kernel(void) {
if (arg_dry_run)
return 0;
pid = fork();
if (pid < 0)
return log_error_errno(errno, "Failed to fork: %m");
else if (pid == 0) {
r = safe_fork("(kexec)", FORK_RESET_SIGNALS|FORK_DEATHSIG, &pid);
if (r < 0)
return log_error_errno(r, "Failed to fork: %m");
if (r == 0) {
const char* const args[] = {
KEXEC,
@@ -3552,15 +3552,11 @@ static int load_kexec_kernel(void) {
NULL };
/* Child */
(void) reset_all_signal_handlers();
(void) reset_signal_mask();
assert_se(prctl(PR_SET_PDEATHSIG, SIGTERM) == 0);
execv(args[0], (char * const *) args);
_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) {
@@ -6123,15 +6119,11 @@ static int enable_sysv_units(const char *verb, char **args) {
if (!arg_quiet)
log_info("Executing: %s", l);
pid = fork();
if (pid < 0)
return log_error_errno(errno, "Failed to fork: %m");
else if (pid == 0) {
j = safe_fork("(sysv)", FORK_RESET_SIGNALS|FORK_DEATHSIG, &pid);
if (j < 0)
return log_error_errno(j, "Failed to fork: %m");
if (j == 0) {
/* Child */
(void) reset_all_signal_handlers();
(void) reset_signal_mask();
execv(argv[0], (char**) argv);
log_error_errno(errno, "Failed to execute %s: %m", argv[0]);
_exit(EXIT_FAILURE);
@@ -7000,20 +6992,16 @@ static int run_editor(char **paths) {
assert(paths);
pid = fork();
if (pid < 0)
return log_error_errno(errno, "Failed to fork: %m");
if (pid == 0) {
r = safe_fork("(editor)", FORK_RESET_SIGNALS|FORK_DEATHSIG, &pid);
if (r < 0)
return log_error_errno(r, "Failed to fork: %m");
if (r == 0) {
const char **args;
char *editor, **editor_args = NULL;
char **tmp_path, **original_path, *p;
unsigned n_editor_args = 0, i = 1;
size_t argc;
(void) reset_all_signal_handlers();
(void) reset_signal_mask();
argc = strv_length(paths)/2 + 1;
/* SYSTEMD_EDITOR takes precedence over EDITOR which takes precedence over VISUAL