diff options
author | Lennart Poettering <lennart@poettering.net> | 2018-11-26 15:59:17 +0100 |
---|---|---|
committer | Lennart Poettering <lennart@poettering.net> | 2018-12-01 12:50:45 +0100 |
commit | 909106ebdf9a128627cd5974d4d388c71d694464 (patch) | |
tree | 1e5be2cfc3d33a05acc3db704eba38199a880662 /src | |
parent | 3c069cdac439c21b8df12caf0dd7bd6e6a502141 (diff) | |
download | systemd-909106ebdf9a128627cd5974d4d388c71d694464.tar.gz |
process-util: add new FORK_RLIMIT_NOFILE_SAFE flag for safe_fork()
The new flag simply means rlimit_nofile_safe() is called in the child
after all fds are rearranged.
Diffstat (limited to 'src')
-rw-r--r-- | src/basic/process-util.c | 9 | ||||
-rw-r--r-- | src/basic/process-util.h | 19 |
2 files changed, 19 insertions, 9 deletions
diff --git a/src/basic/process-util.c b/src/basic/process-util.c index d1a34338f6..5cf4e37f24 100644 --- a/src/basic/process-util.c +++ b/src/basic/process-util.c @@ -35,6 +35,7 @@ #include "missing.h" #include "process-util.h" #include "raw-clone.h" +#include "rlimit-util.h" #include "signal-util.h" #include "stat-util.h" #include "string-table.h" @@ -1401,6 +1402,14 @@ int safe_fork_full( } } + 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 (ret_pid) *ret_pid = getpid_cached(); diff --git a/src/basic/process-util.h b/src/basic/process-util.h index af47513fab..496e14d3de 100644 --- a/src/basic/process-util.h +++ b/src/basic/process-util.h @@ -142,15 +142,16 @@ void reset_cached_pid(void); int must_be_root(void); typedef enum ForkFlags { - FORK_RESET_SIGNALS = 1 << 0, - FORK_CLOSE_ALL_FDS = 1 << 1, - FORK_DEATHSIG = 1 << 2, - FORK_NULL_STDIO = 1 << 3, - FORK_REOPEN_LOG = 1 << 4, - FORK_LOG = 1 << 5, - FORK_WAIT = 1 << 6, - FORK_NEW_MOUNTNS = 1 << 7, - FORK_MOUNTNS_SLAVE = 1 << 8, + FORK_RESET_SIGNALS = 1 << 0, /* Reset all signal handlers and signal mask */ + FORK_CLOSE_ALL_FDS = 1 << 1, /* Close all open file descriptors in the child, except for 0,1,2 */ + FORK_DEATHSIG = 1 << 2, /* Set PR_DEATHSIG in the child */ + FORK_NULL_STDIO = 1 << 3, /* Connect 0,1,2 to /dev/null */ + FORK_REOPEN_LOG = 1 << 4, /* Reopen log connection */ + FORK_LOG = 1 << 5, /* Log above LOG_DEBUG log level about failures */ + FORK_WAIT = 1 << 6, /* Wait until child exited */ + FORK_NEW_MOUNTNS = 1 << 7, /* Run child in its own mount namespace */ + FORK_MOUNTNS_SLAVE = 1 << 8, /* Make child's mount namespace MS_SLAVE */ + FORK_RLIMIT_NOFILE_SAFE = 1 << 9, /* Set RLIMIT_NOFILE soft limit to 1K for select() compat */ } ForkFlags; int safe_fork_full(const char *name, const int except_fds[], size_t n_except_fds, ForkFlags flags, pid_t *ret_pid); |