HALF ECHO LEARN / REFERENCE COLLECTION
v262.2src/basic/process-util.c
Git blob: bcee9c39d1c691fd8f4fc26f16e925d3ccd6fa54
Lines 1001–2000 of 2,419
return false;
if (r < 0)
return r;
return ppid == getpid_cached();
}
int pid_is_my_child(pid_t pid) {
if (pid == 0)
return false;
return pidref_is_my_child(&PIDREF_MAKE_FROM_PID(pid));
}
int pidref_is_unwaited(PidRef *pid) {
int r;
/* Checks whether a PID is still valid at all, including a zombie */
if (!pidref_is_set(pid))
return -ESRCH;
if (pidref_is_remote(pid))
return -EREMOTE;
if (pid->pid == 1 || pidref_is_self(pid))
return true;
r = pidref_kill(pid, 0);
if (r == -ESRCH)
return false;
if (r < 0)
return r;
return true;
}
int pid_is_unwaited(pid_t pid) {
if (pid == 0)
return true;
return pidref_is_unwaited(&PIDREF_MAKE_FROM_PID(pid));
}
int pid_is_alive(pid_t pid) {
int r;
/* Checks whether a PID is still valid and not a zombie */
if (pid < 0)
return -ESRCH;
if (pid <= 1) /* If we or PID 1 would be a zombie, this code would not be running */
return true;
if (pid == getpid_cached())
return true;
r = get_process_state(pid);
if (r == -ESRCH)
return false;
if (r < 0)
return r;
return r != 'Z';
}
int pidref_is_alive(const PidRef *pidref) {
int r, result;
if (!pidref_is_set(pidref))
return -ESRCH;
if (pidref_is_remote(pidref))
return -EREMOTE;
result = pid_is_alive(pidref->pid);
if (result < 0) {
assert(result != -ESRCH);
return result;
}
r = pidref_verify(pidref);
if (r == -ESRCH)
return false;
if (r < 0)
return r;
return result;
}
int pidref_from_same_root_fs(PidRef *a, PidRef *b) {
_cleanup_(pidref_done) PidRef self = PIDREF_NULL;
int r;
/* Checks if the two specified processes have the same root fs. Either can be specified as NULL in
* which case we'll check against ourselves. */
if (!a || !b) {
r = pidref_set_self(&self);
if (r < 0)
return r;
if (!a)
a = &self;
if (!b)
b = &self;
}
if (!pidref_is_set(a) || !pidref_is_set(b))
return -ESRCH;
/* If one of the two processes have the same root they cannot have the same root fs, but if both of
* them do we don't know */
if (pidref_is_remote(a) && pidref_is_remote(b))
return -EREMOTE;
if (pidref_is_remote(a) || pidref_is_remote(b))
return false;
if (pidref_equal(a, b))
return true;
const char *roota = procfs_file_alloca(a->pid, "root");
const char *rootb = procfs_file_alloca(b->pid, "root");
int result = inode_same(roota, rootb, 0);
if (result == -ENOENT)
return proc_mounted() == 0 ? -ENOSYS : -ESRCH;
if (result < 0)
return result;
r = pidref_verify(a);
if (r < 0)
return r;
r = pidref_verify(b);
if (r < 0)
return r;
return result;
}
bool is_main_thread(void) {
static thread_local int cached = -1;
if (cached < 0)
cached = getpid_cached() == gettid();
return cached;
}
unsigned long personality_from_string(const char *s) {
Architecture architecture;
if (!s)
return PERSONALITY_INVALID;
/* Parse a personality specifier. We use our own identifiers that indicate specific ABIs, rather than just
* hints regarding the register size, since we want to keep things open for multiple locally supported ABIs for
* the same register size. */
architecture = architecture_from_string(s);
if (architecture < 0)
return PERSONALITY_INVALID;
if (architecture == native_architecture())
return PER_LINUX;
#ifdef ARCHITECTURE_SECONDARY
if (architecture == ARCHITECTURE_SECONDARY)
return PER_LINUX32;
#endif
return PERSONALITY_INVALID;
}
const char* personality_to_string(unsigned long p) {
Architecture architecture = _ARCHITECTURE_INVALID;
if (p == PER_LINUX)
architecture = native_architecture();
#ifdef ARCHITECTURE_SECONDARY
else if (p == PER_LINUX32)
architecture = ARCHITECTURE_SECONDARY;
#endif
if (architecture < 0)
return NULL;
return architecture_to_string(architecture);
}
int safe_personality(unsigned long p) {
int ret;
/* So here's the deal, personality() is weirdly defined by glibc. In some cases it returns a failure via errno,
* and in others as negative return value containing an errno-like value. Let's work around this: this is a
* wrapper that uses errno if it is set, and uses the return value otherwise. And then it sets both errno and
* the return value indicating the same issue, so that we are definitely on the safe side.
*
* See https://github.com/systemd/systemd/issues/6737 */
errno = 0;
ret = personality(p);
if (ret < 0) {
if (errno != 0)
return -errno;
errno = -ret;
}
return ret;
}
int opinionated_personality(unsigned long *ret) {
int current;
assert(ret);
/* Returns the current personality, or PERSONALITY_INVALID if we can't determine it. This function is a bit
* opinionated though, and ignores all the finer-grained bits and exotic personalities, only distinguishing the
* two most relevant personalities: PER_LINUX and PER_LINUX32. */
current = safe_personality(PERSONALITY_INVALID);
if (current < 0)
return current;
if (((unsigned long) current & OPINIONATED_PERSONALITY_MASK) == PER_LINUX32)
*ret = PER_LINUX32;
else
*ret = PER_LINUX;
return 0;
}
void valgrind_summary_hack(void) {
#if HAVE_VALGRIND_VALGRIND_H
if (getpid_cached() == 1 && RUNNING_ON_VALGRIND) {
pid_t pid;
pid = raw_clone(SIGCHLD);
if (pid < 0)
log_struct_errno(
LOG_EMERG, errno,
LOG_MESSAGE_ID(SD_MESSAGE_VALGRIND_HELPER_FORK_STR),
LOG_MESSAGE("Failed to fork off valgrind helper: %m"));
else if (pid == 0)
exit(EXIT_SUCCESS);
else {
log_info("Spawned valgrind helper as PID "PID_FMT".", pid);
_cleanup_(pidref_done) PidRef pidref = PIDREF_MAKE_FROM_PID(pid);
(void) pidref_set_pid(&pidref, pid);
(void) pidref_wait_for_terminate(&pidref, NULL);
}
}
#endif
}
int pid_compare_func(const pid_t *a, const pid_t *b) {
/* Suitable for usage in qsort() */
assert(a);
assert(b);
return CMP(*a, *b);
}
bool nice_is_valid(int n) {
return n >= PRIO_MIN && n < PRIO_MAX;
}
bool sched_policy_is_valid(int policy) {
return IN_SET(policy, SCHED_OTHER, SCHED_BATCH, SCHED_IDLE, SCHED_FIFO, SCHED_RR, SCHED_EXT);
}
bool sched_policy_supported(int policy) {
return sched_get_priority_min(policy) >= 0;
}
/* Wrappers around sched_get_priority_{min,max}() that gracefully handles missing SCHED_EXT support in the kernel */
int sched_get_priority_min_safe(int policy) {
int r;
r = sched_get_priority_min(policy);
if (r >= 0)
return r;
/* Fallback priority */
return 0;
}
int sched_get_priority_max_safe(int policy) {
int r;
r = sched_get_priority_max(policy);
if (r >= 0)
return r;
return 0;
}
/* The cached PID, possible values:
*
* == UNSET [0] → cache not initialized yet
* == BUSY [-1] → some thread is initializing it at the moment
* any other → the cached PID
*/
#define CACHED_PID_UNSET ((pid_t) 0)
#define CACHED_PID_BUSY ((pid_t) -1)
static pid_t cached_pid = CACHED_PID_UNSET;
void reset_cached_pid(void) {
/* Invoked in the child after a fork(), i.e. at the first moment the PID changed */
cached_pid = CACHED_PID_UNSET;
/* The pidfd ID cache is keyed by PID, which is not enough to detect the PID 1 → PID 1 transition
* when forking into a new PID namespace, hence flush it explicitly too. */
reset_cached_pidfd_id();
}
pid_t getpid_cached(void) {
static bool installed = false;
pid_t current_value = CACHED_PID_UNSET;
/* getpid_cached() is much like getpid(), but caches the value in local memory, to avoid having to invoke a
* system call each time. This restores glibc behaviour from before 2.24, when getpid() was unconditionally
* cached. Starting with 2.24 getpid() started to become prohibitively expensive when used for detecting when
* objects were used across fork()s. With this caching the old behaviour is somewhat restored.
*
* https://bugzilla.redhat.com/show_bug.cgi?id=1443976
* https://sourceware.org/git/gitweb.cgi?p=glibc.git;h=c579f48edba88380635ab98cb612030e3ed8691e
*/
(void) __atomic_compare_exchange_n(
&cached_pid,
¤t_value,
CACHED_PID_BUSY,
false,
__ATOMIC_SEQ_CST,
__ATOMIC_SEQ_CST);
switch (current_value) {
case CACHED_PID_UNSET: { /* Not initialized yet, then do so now */
pid_t new_pid;
new_pid = getpid();
if (!installed) {
/* __register_atfork() either returns 0 or -ENOMEM, in its glibc implementation. Since it's
* only half-documented (glibc doesn't document it but LSB does — though only superficially)
* we'll check for errors only in the most generic fashion possible. */
if (pthread_atfork(NULL, NULL, reset_cached_pid) != 0) {
/* OOM? Let's try again later */
cached_pid = CACHED_PID_UNSET;
return new_pid;
}
installed = true;
}
cached_pid = new_pid;
return new_pid;
}
case CACHED_PID_BUSY: /* Somebody else is currently initializing */
return getpid();
default: /* Properly initialized */
return current_value;
}
}
int must_be_root(void) {
if (geteuid() == 0)
return 0;
return log_error_errno(SYNTHETIC_ERRNO(EPERM), "Need to be root.");
}
pid_t clone_with_nested_stack(int (*fn)(void *), int flags, void *userdata) {
size_t ps;
pid_t pid;
void *mystack;
/* A wrapper around glibc's clone() call that automatically sets up a "nested" stack. Only supports
* invocations without CLONE_VM, so that we can continue to use the parent's stack mapping.
*
* Note: glibc's clone() wrapper does not synchronize malloc() locks. This means that if the parent
* is threaded these locks will be in an undefined state in the child, and hence memory allocations
* are likely going to run into deadlocks. Hence: if you use this function make sure your parent is
* strictly single-threaded or your child never calls malloc(). */
assert((flags & (CLONE_VM|CLONE_PARENT_SETTID|CLONE_CHILD_SETTID|
CLONE_CHILD_CLEARTID|CLONE_SETTLS)) == 0);
/* We allocate some space on the stack to use as the stack for the child (hence "nested"). Note that
* the net effect is that the child will have the start of its stack inside the stack of the parent,
* but since they are a CoW copy of each other that's fine. We allocate one page-aligned page. But
* since we don't want to deal with differences between systems where the stack grows backwards or
* forwards we'll allocate one more and place the stack address in the middle. Except that we also
* want it page aligned, hence we'll allocate one page more. Makes 3. */
ps = page_size();
mystack = alloca(ps*3);
mystack = (uint8_t*) mystack + ps; /* move pointer one page ahead since stacks usually grow backwards */
mystack = (void*) ALIGN_TO((uintptr_t) mystack, ps); /* align to page size (moving things further ahead) */
#if HAVE_CLONE
pid = clone(fn, mystack, flags, userdata);
#else
pid = __clone2(fn, mystack, ps, flags, userdata);
#endif
if (pid < 0)
return -errno;
return pid;
}
static int fork_flags_to_signal(ForkFlags flags) {
return (flags & FORK_DEATHSIG_SIGTERM) ? SIGTERM :
(flags & FORK_DEATHSIG_SIGINT) ? SIGINT :
SIGKILL;
}
int pidref_safe_fork_full(
const char *name,
const int stdio_fds[3],
int except_fds[],
size_t n_except_fds,
ForkFlags flags,
PidRef *ret) {
pid_t original_pid, pid;
sigset_t saved_ss, ss;
_unused_ _cleanup_(block_signals_reset) sigset_t *saved_ssp = NULL;
bool block_signals = false, block_all = false, intermediary = false;
_cleanup_close_pair_ int pidref_transport_fds[2] = EBADF_PAIR;
int prio, r;
assert(!FLAGS_SET(flags, FORK_WAIT|FORK_FREEZE));
assert(!FLAGS_SET(flags, FORK_DETACH) ||
(flags & (FORK_WAIT|FORK_DEATHSIG_SIGTERM|FORK_DEATHSIG_SIGINT|FORK_DEATHSIG_SIGKILL)) == 0);
/* A wrapper around fork(), that does a couple of important initializations in addition to mere
* forking. If provided, ret is initialized in both the parent and the child process, both times
* referencing the child process. Returns == 0 in the child and > 0 in the parent. */
prio = flags & FORK_LOG ? LOG_ERR : LOG_DEBUG;
original_pid = getpid_cached();
if (flags & FORK_FLUSH_STDIO) {
fflush(stdout);
fflush(stderr); /* This one shouldn't be necessary, stderr should be unbuffered anyway, but let's better be safe than sorry */
}
if (flags & (FORK_RESET_SIGNALS|FORK_DEATHSIG_SIGTERM|FORK_DEATHSIG_SIGINT)) {
/* 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. (Note that for
* FORK_DEATHSIG_SIGKILL we don't bother, since it cannot be blocked anyway.) */
assert_se(sigfillset(&ss) >= 0);
block_signals = block_all = true;
} else if (flags & FORK_WAIT) {
/* Let's block SIGCHLD at least, so that we can safely watch for the child process */
assert_se(sigemptyset(&ss) >= 0);
assert_se(sigaddset(&ss, SIGCHLD) >= 0);
block_signals = true;
}
if (block_signals) {
if (sigprocmask(SIG_BLOCK, &ss, &saved_ss) < 0)
return log_full_errno(prio, errno, "Failed to block signal mask: %m");
saved_ssp = &saved_ss;
}
if (FLAGS_SET(flags, FORK_DETACH)) {
/* Fork off intermediary child if needed */
r = is_reaper_process();
if (r < 0)
return log_full_errno(prio, r, "Failed to determine if we are a reaper process: %m");
if (!r) {
/* Not a reaper process, hence do a double fork() so we are reparented to one */
if (ret && socketpair(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC, 0, pidref_transport_fds) < 0)
return log_full_errno(prio, errno, "Failed to allocate pidref socket: %m");
pid = fork();
if (pid < 0)
return log_full_errno(prio, errno, "Failed to fork off '%s': %m", strna(name));
if (pid > 0) {
log_debug("Successfully forked off intermediary '%s' as PID " PID_FMT ".", strna(name), pid);
pidref_transport_fds[1] = safe_close(pidref_transport_fds[1]);
if (pidref_transport_fds[0] >= 0) {
/* Wait for the intermediary child to exit so the caller can be
* certain the actual child process has been reparented by the time
* this function returns. */
r = pidref_wait_for_terminate_and_check(
name,
&PIDREF_MAKE_FROM_PID(pid),
FLAGS_SET(flags, FORK_LOG) ? WAIT_LOG : 0);
if (r < 0)
return log_full_errno(prio, r, "Failed to wait for intermediary process: %m");
if (r != EXIT_SUCCESS) /* exit status > 0 should be treated as failure, too */
return -EPROTO;
int pidfd;
ssize_t n = receive_one_fd_iov(
pidref_transport_fds[0],
&IOVEC_MAKE(&pid, sizeof(pid)),
/* iovlen= */ 1,
/* flags= */ 0,
&pidfd);
if (n < 0)
return log_full_errno(prio, n, "Failed to receive child pidref: %m");
*ret = (PidRef) { .pid = pid, .fd = pidfd };
}
return 1; /* return in the parent */
}
pidref_transport_fds[0] = safe_close(pidref_transport_fds[0]);
intermediary = true;
}
}
if ((flags & (FORK_NEW_MOUNTNS|FORK_NEW_USERNS|FORK_NEW_NETNS|FORK_NEW_PIDNS)) != 0)
pid = raw_clone(SIGCHLD|
(FLAGS_SET(flags, FORK_NEW_MOUNTNS) ? CLONE_NEWNS : 0) |
(FLAGS_SET(flags, FORK_NEW_USERNS) ? CLONE_NEWUSER : 0) |
(FLAGS_SET(flags, FORK_NEW_NETNS) ? CLONE_NEWNET : 0) |
(FLAGS_SET(flags, FORK_NEW_PIDNS) ? CLONE_NEWPID : 0));
else
pid = fork();
if (pid < 0)
return log_full_errno(prio, errno, "Failed to fork off '%s': %m", strna(name));
if (pid > 0) {
/* If we are in the intermediary process, exit now */
if (intermediary) {
if (pidref_transport_fds[1] >= 0) {
_cleanup_(pidref_done) PidRef pidref = PIDREF_NULL;
r = pidref_set_pid(&pidref, pid);
if (r < 0) {
log_full_errno(prio, r, "Failed to open reference to PID "PID_FMT": %m", pid);
_exit(EXIT_FAILURE);
}
r = send_one_fd_iov(
pidref_transport_fds[1],
pidref.fd,
&IOVEC_MAKE(&pidref.pid, sizeof(pidref.pid)),
/* iovlen= */ 1,
/* flags= */ 0);
if (r < 0) {
log_full_errno(prio, r, "Failed to send child pidref: %m");
_exit(EXIT_FAILURE);
}
}
_exit(EXIT_SUCCESS);
}
/* We are in the parent process */
log_debug("Successfully forked off '%s' as PID " PID_FMT ".", strna(name), pid);
if (flags & FORK_WAIT) {
if (block_all) {
/* undo everything except SIGCHLD */
ss = saved_ss;
assert_se(sigaddset(&ss, SIGCHLD) >= 0);
(void) sigprocmask(SIG_SETMASK, &ss, NULL);
}
r = pidref_wait_for_terminate_and_check(
name,
&PIDREF_MAKE_FROM_PID(pid),
FLAGS_SET(flags, FORK_LOG) ? WAIT_LOG : 0);
if (r < 0)
return r;
if (r != EXIT_SUCCESS) /* exit status > 0 should be treated as failure, too */
return -EPROTO;
/* If we are in the parent and successfully waited, then the process doesn't exist anymore. */
if (ret)
*ret = PIDREF_NULL;
return 1;
}
if (ret) {
r = pidref_set_pid(ret, pid);
if (r < 0) /* Let's not fail for this, no matter what, the process exists after all, and that's key */
*ret = PIDREF_MAKE_FROM_PID(pid);
}
return 1;
}
/* We are in the child process */
pidref_transport_fds[1] = safe_close(pidref_transport_fds[1]);
/* Restore signal mask manually */
saved_ssp = NULL;
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);
log_settle_target();
}
if (name) {
r = rename_process(name);
if (r < 0)
log_full_errno(flags & FORK_LOG ? LOG_WARNING : LOG_DEBUG,
r, "Failed to rename process, ignoring: %m");
}
/* let's disable dlopen() in the child, as a paranoia safety precaution: children should not live for
* long and only do minimal work before exiting or exec()ing. Doing dlopen() is not either. If people
* want dlopen() they should do it before forking. This is a safety precaution in particular for
* cases where the child does namespace shenanigans: we should never end up loading a module from a
* foreign environment. Note that this has no effect on NSS! (i.e. it only has effect on uses of our
* dlopen_safe(), which we use comprehensively in our codebase, but glibc NSS doesn't bother, of
* course.) */
if (!FLAGS_SET(flags, FORK_ALLOW_DLOPEN))
block_dlopen();
if (flags & (FORK_DEATHSIG_SIGTERM|FORK_DEATHSIG_SIGINT|FORK_DEATHSIG_SIGKILL)) {
r = prctl_safe(PR_SET_PDEATHSIG, fork_flags_to_signal(flags), 0, 0, 0);
if (r < 0) {
log_full_errno(prio, r, "Failed to set death signal: %m");
_exit(EXIT_FAILURE);
}
}
if (flags & FORK_RESET_SIGNALS) {
r = reset_all_signal_handlers();
if (r < 0) {
log_full_errno(prio, 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_full_errno(prio, 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_full_errno(prio, errno, "Failed to restore signal mask: %m");
_exit(EXIT_FAILURE);
}
}
if (flags & (FORK_DEATHSIG_SIGTERM|FORK_DEATHSIG_SIGKILL|FORK_DEATHSIG_SIGINT)) {
pid_t ppid;
/* 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 */
ppid = getppid();
if (ppid == 0)
/* Parent is in a different PID namespace. */;
else if (ppid != original_pid) {
int sig = fork_flags_to_signal(flags);
log_debug("Parent died early, raising %s.", signal_to_string(sig));
(void) raise(sig);
_exit(EXIT_FAILURE);
}
}
if (FLAGS_SET(flags, FORK_NEW_MOUNTNS | FORK_MOUNTNS_SLAVE)) {
/* Optionally, make sure we never propagate mounts to the host. */
if (mount(NULL, "/", NULL, MS_SLAVE | MS_REC, NULL) < 0) {
log_full_errno(prio, errno, "Failed to remount root directory as MS_SLAVE: %m");
_exit(EXIT_FAILURE);
}
}
if (FLAGS_SET(flags, FORK_PRIVATE_TMP)) {
assert(FLAGS_SET(flags, FORK_NEW_MOUNTNS));
/* Optionally, overmount new tmpfs instance on /tmp/. */
r = mount_nofollow("tmpfs", "/tmp", "tmpfs",
MS_NOSUID|MS_NODEV,
"mode=01777" TMPFS_LIMITS_RUN);
if (r < 0) {
log_full_errno(prio, r, "Failed to overmount /tmp/: %m");
_exit(EXIT_FAILURE);
}
}
if (flags & FORK_REARRANGE_STDIO) {
if (stdio_fds) {
r = rearrange_stdio(stdio_fds[0], stdio_fds[1], stdio_fds[2]);
if (r < 0) {
log_full_errno(prio, r, "Failed to rearrange stdio fds: %m");
_exit(EXIT_FAILURE);
}
/* Turn off O_NONBLOCK on the fdio fds, in case it was left on */
stdio_disable_nonblock();
} else {
r = make_null_stdio();
if (r < 0) {
log_full_errno(prio, r, "Failed to connect stdin/stdout to /dev/null: %m");
_exit(EXIT_FAILURE);
}
}
} else if (flags & FORK_STDOUT_TO_STDERR) {
if (dup2(STDERR_FILENO, STDOUT_FILENO) < 0) {
log_full_errno(prio, errno, "Failed to connect stdout to stderr: %m");
_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_full_errno(prio, r, "Failed to close all file descriptors: %m");
_exit(EXIT_FAILURE);
}
}
if (flags & FORK_PACK_FDS) {
/* FORK_CLOSE_ALL_FDS ensures that except_fds are the only FDs >= 3 that are
* open, this is including the log. This is required by pack_fds, which will
* get stuck in an infinite loop of any FDs other than except_fds are open. */
assert(FLAGS_SET(flags, FORK_CLOSE_ALL_FDS));
r = pack_fds(except_fds, n_except_fds);
if (r < 0) {
log_full_errno(prio, r, "Failed to pack file descriptors: %m");
_exit(EXIT_FAILURE);
}
}
if (flags & FORK_CLOEXEC_OFF) {
r = fd_cloexec_many(except_fds, n_except_fds, false);
if (r < 0) {
log_full_errno(prio, r, "Failed to turn off O_CLOEXEC on 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_RLIMIT_NOFILE_SAFE) {
r = rlimit_nofile_safe();
if (r < 0) {
log_full_errno(prio, r, "Failed to lower RLIMIT_NOFILE's soft limit to 1K: %m");
_exit(EXIT_FAILURE);
}
}
if (!FLAGS_SET(flags, FORK_KEEP_NOTIFY_SOCKET)) {
r = RET_NERRNO(unsetenv("NOTIFY_SOCKET"));
if (r < 0) {
log_full_errno(prio, r, "Failed to unset $NOTIFY_SOCKET: %m");
_exit(EXIT_FAILURE);
}
}
if (FLAGS_SET(flags, FORK_FREEZE))
freeze();
if (ret) {
r = pidref_set_self(ret);
if (r < 0) {
log_full_errno(prio, r, "Failed to acquire PID reference on ourselves: %m");
_exit(EXIT_FAILURE);
}
}
return 0;
}
int namespace_fork_full(
const char *outer_name,
const char *inner_name,
int except_fds[],
size_t n_except_fds,
ForkFlags flags,
int pidns_fd,
int mntns_fd,
int netns_fd,
int userns_fd,
int root_fd,
NamespaceEnterFlags namespace_flags,
PidRef *ret) {
_cleanup_(pidref_done_sigkill_wait) PidRef pidref_outer = PIDREF_NULL;
_cleanup_close_pair_ int errno_pipe_fd[2] = EBADF_PAIR;
int r, prio = FLAGS_SET(flags, FORK_LOG) ? LOG_ERR : LOG_DEBUG;
/* This is much like safe_fork(), but forks twice, and joins the specified namespaces in the middle
* process. This ensures that we are fully a member of the destination namespace, with pidns an all, so that
* /proc/self/fd works correctly.
*
* TODO: once we can rely on PIDFD_INFO_EXIT, do not keep the middle process around and instead
* return the pidfd of the inner process for direct tracking. */
/* Insist on PDEATHSIG being enabled, as the pid returned is the one of the middle man, and otherwise
* killing of it won't be propagated to the inner child. */
assert((flags & (FORK_DEATHSIG_SIGKILL|FORK_DEATHSIG_SIGTERM|FORK_DEATHSIG_SIGINT)) != 0);
assert((flags & (FORK_DETACH|FORK_FREEZE)) == 0);
assert(!FLAGS_SET(flags, FORK_ALLOW_DLOPEN)); /* never allow loading shared library from another ns */
/* We want read() to block as a synchronization point */
assert_cc(sizeof(int) <= PIPE_BUF);
if (pipe2(errno_pipe_fd, O_CLOEXEC) < 0)
return log_full_errno(prio, errno, "Failed to create pipe: %m");
r = pidref_safe_fork_full(
outer_name,
/* stdio_fds= */ NULL, /* except_fds= */ NULL, /* n_except_fds= */ 0,
(flags|FORK_DEATHSIG_SIGKILL) & ~(FORK_DEATHSIG_SIGTERM|FORK_DEATHSIG_SIGINT|FORK_REOPEN_LOG|FORK_NEW_MOUNTNS|FORK_MOUNTNS_SLAVE|FORK_NEW_USERNS|FORK_NEW_NETNS|FORK_NEW_PIDNS|FORK_CLOSE_ALL_FDS|FORK_PACK_FDS|FORK_CLOEXEC_OFF|FORK_RLIMIT_NOFILE_SAFE),
&pidref_outer);
if (r == -EPROTO && FLAGS_SET(flags, FORK_WAIT)) {
errno_pipe_fd[1] = safe_close(errno_pipe_fd[1]);
int k = read_errno(errno_pipe_fd[0]);
if (k < 0 && k != -EIO)
return k;
}
if (r < 0)
return r;
if (r == 0) {
_cleanup_(pidref_done) PidRef pidref_inner = PIDREF_NULL;
/* Child */
errno_pipe_fd[0] = safe_close(errno_pipe_fd[0]);
r = namespace_enter_full(pidns_fd, mntns_fd, netns_fd, userns_fd, root_fd, namespace_flags);
if (r < 0) {
log_full_errno(prio, r, "Failed to join namespace: %m");
report_errno_and_exit(errno_pipe_fd[1], r);
}
/* We mask a few flags here that either make no sense for the grandchild, or that we don't have to do again */
r = pidref_safe_fork_full(
inner_name,
NULL,
except_fds, n_except_fds,
flags & ~(FORK_WAIT|FORK_RESET_SIGNALS|FORK_REARRANGE_STDIO|FORK_FLUSH_STDIO|FORK_STDOUT_TO_STDERR),
&pidref_inner);
if (r < 0)
report_errno_and_exit(errno_pipe_fd[1], r);
if (r == 0) {
/* Child */
if (!FLAGS_SET(flags, FORK_CLOSE_ALL_FDS)) {
errno_pipe_fd[1] = safe_close(errno_pipe_fd[1]);
pidref_done(&pidref_outer);
} else {
errno_pipe_fd[1] = -EBADF;
pidref_outer = PIDREF_NULL;
}
if (ret)
*ret = TAKE_PIDREF(pidref_inner);
return 0;
}
log_forget_fds();
log_set_open_when_needed(true);
(void) close_all_fds(&pidref_inner.fd, 1);
r = pidref_wait_for_terminate_and_check(
inner_name,
&pidref_inner,
FLAGS_SET(flags, FORK_LOG) ? WAIT_LOG : 0);
if (r < 0)
_exit(EXIT_FAILURE);
_exit(r);
}
errno_pipe_fd[1] = safe_close(errno_pipe_fd[1]);
r = read_errno(errno_pipe_fd[0]);
if (r < 0)
return r; /* the child logs about failures on its own, no need to duplicate here */
if (ret)
*ret = TAKE_PIDREF(pidref_outer);
else
pidref_done(&pidref_outer); /* disarm sigkill_wait */
return 1;
}
int namespace_fork(
const char *outer_name,
const char *inner_name,
ForkFlags flags,
int pidns_fd,
int mntns_fd,
int netns_fd,
int userns_fd,
int root_fd,
PidRef *ret) {
return namespace_fork_full(outer_name, inner_name, /* except_fds= */ NULL, /* n_except_fds= */ 0, flags,
pidns_fd, mntns_fd, netns_fd, userns_fd, root_fd,
/* namespace_flags= */ 0, ret);
}
bool oom_score_adjust_is_valid(int oa) {
return oa >= OOM_SCORE_ADJ_MIN && oa <= OOM_SCORE_ADJ_MAX;
}
int set_oom_score_adjust(int value) {
char t[DECIMAL_STR_MAX(int)];
if (!oom_score_adjust_is_valid(value))
return -EINVAL;
xsprintf(t, "%i", value);
return write_string_file("/proc/self/oom_score_adj", t,
WRITE_STRING_FILE_VERIFY_ON_FAILURE|WRITE_STRING_FILE_DISABLE_BUFFER);
}
int get_oom_score_adjust(int *ret) {
_cleanup_free_ char *t = NULL;
int r, a;
r = read_virtual_file("/proc/self/oom_score_adj", SIZE_MAX, &t, NULL);
if (r < 0)
return r;
delete_trailing_chars(t, WHITESPACE);
r = safe_atoi(t, &a);
if (r < 0)
return r;
if (!oom_score_adjust_is_valid(a))
return -ENODATA;
if (ret)
*ret = a;
return 0;
}
static int rlimit_to_nice(rlim_t limit) {
if (limit <= 1)
return PRIO_MAX-1; /* i.e. 19 */
if (limit >= -PRIO_MIN + PRIO_MAX)
return PRIO_MIN; /* i.e. -20 */
return PRIO_MAX - (int) limit;
}
int setpriority_closest(int priority) {
struct rlimit highest;
int r, current, limit;
/* Try to set requested nice level */
r = RET_NERRNO(setpriority(PRIO_PROCESS, 0, priority));
if (r >= 0)
return 1;
if (!ERRNO_IS_NEG_PRIVILEGE(r))
return r;
errno = 0;
current = getpriority(PRIO_PROCESS, 0);
if (errno != 0)
return -errno;
if (priority == current)
return 1;
/* Hmm, we'd expect that raising the nice level from our status quo would always work. If it doesn't,
* then the whole setpriority() system call is blocked to us, hence let's propagate the error