| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
#pragma once |
| 3 |
|
| 4 |
#include <signal.h> |
| 5 |
|
| 6 |
#include "fileio.h" |
| 7 |
#include "format-util.h" |
| 8 |
#include "basic-forward.h" |
| 9 |
#include "string-util.h" |
| 10 |
|
| 11 |
#define procfs_file_alloca(pid, field) \ |
| 12 |
({ \ |
| 13 |
pid_t _pid_ = (pid); \ |
| 14 |
const char *_field_ = (field); \ |
| 15 |
char *_r_; \ |
| 16 |
if (_pid_ == 0) \ |
| 17 |
_r_ = strjoina("/proc/self/", _field_); \ |
| 18 |
else { \ |
| 19 |
assert(_pid_ > 0); \ |
| 20 |
_r_ = newa(char, STRLEN("/proc/") + DECIMAL_STR_MAX(pid_t) + 1 + strlen(_field_) + 1); \ |
| 21 |
sprintf(_r_, "/proc/" PID_FMT "/%s", _pid_, _field_); \ |
| 22 |
} \ |
| 23 |
(const char*) _r_; \ |
| 24 |
}) |
| 25 |
|
| 26 |
static inline int procfs_file_get_field(pid_t pid, const char *name, const char *key, char **ret) { |
| 27 |
return get_proc_field(procfs_file_alloca(pid, name), key, ret); |
| 28 |
} |
| 29 |
|
| 30 |
typedef enum ProcessCmdlineFlags { |
| 31 |
PROCESS_CMDLINE_COMM_FALLBACK = 1 << 0, |
| 32 |
PROCESS_CMDLINE_USE_LOCALE = 1 << 1, |
| 33 |
PROCESS_CMDLINE_QUOTE = 1 << 2, |
| 34 |
PROCESS_CMDLINE_QUOTE_POSIX = 1 << 3, |
| 35 |
} ProcessCmdlineFlags; |
| 36 |
|
| 37 |
int pid_get_comm(pid_t pid, char **ret); |
| 38 |
int pidref_get_comm(const PidRef *pid, char **ret); |
| 39 |
int pid_get_cmdline(pid_t pid, size_t max_columns, ProcessCmdlineFlags flags, char **ret); |
| 40 |
int pidref_get_cmdline(const PidRef *pid, size_t max_columns, ProcessCmdlineFlags flags, char **ret); |
| 41 |
int pid_get_cmdline_strv(pid_t pid, ProcessCmdlineFlags flags, char ***ret); |
| 42 |
int pidref_get_cmdline_strv(const PidRef *pid, ProcessCmdlineFlags flags, char ***ret); |
| 43 |
int get_process_exe(pid_t pid, char **ret); |
| 44 |
int pid_get_uid(pid_t pid, uid_t *ret); |
| 45 |
int pidref_get_uid(const PidRef *pid, uid_t *ret); |
| 46 |
int get_process_gid(pid_t pid, gid_t *ret); |
| 47 |
int get_process_cwd(pid_t pid, char **ret); |
| 48 |
int get_process_root(pid_t pid, char **ret); |
| 49 |
int get_process_environ(pid_t pid, char **ret); |
| 50 |
int pid_get_ppid(pid_t pid, pid_t *ret); |
| 51 |
int pidref_get_ppid(const PidRef *pidref, pid_t *ret); |
| 52 |
int pidref_get_ppid_as_pidref(const PidRef *pidref, PidRef *ret); |
| 53 |
int pid_get_start_time(pid_t pid, usec_t *ret); |
| 54 |
int pidref_get_start_time(const PidRef *pid, usec_t *ret); |
| 55 |
int get_process_umask(pid_t pid, mode_t *ret); |
| 56 |
|
| 57 |
static inline bool SIGINFO_CODE_IS_DEAD(int code) { |
| 58 |
return IN_SET(code, CLD_EXITED, CLD_KILLED, CLD_DUMPED); |
| 59 |
} |
| 60 |
|
| 61 |
typedef enum WaitFlags { |
| 62 |
WAIT_LOG_ABNORMAL = 1 << 0, |
| 63 |
WAIT_LOG_NON_ZERO_EXIT_STATUS = 1 << 1, |
| 64 |
|
| 65 |
/* A shortcut for requesting the most complete logging */ |
| 66 |
WAIT_LOG = WAIT_LOG_ABNORMAL|WAIT_LOG_NON_ZERO_EXIT_STATUS, |
| 67 |
} WaitFlags; |
| 68 |
|
| 69 |
int pidref_wait_for_terminate_and_check(const char *name, PidRef *pidref, WaitFlags flags); |
| 70 |
|
| 71 |
int kill_and_sigcont(pid_t pid, int sig); |
| 72 |
|
| 73 |
int pid_is_kernel_thread(pid_t pid); |
| 74 |
int pidref_is_kernel_thread(const PidRef *pid); |
| 75 |
|
| 76 |
int getenv_for_pid(pid_t pid, const char *field, char **ret); |
| 77 |
|
| 78 |
int pid_is_alive(pid_t pid); |
| 79 |
int pidref_is_alive(const PidRef *pidref); |
| 80 |
int pid_is_unwaited(pid_t pid); |
| 81 |
int pidref_is_unwaited(PidRef *pidref); |
| 82 |
int pid_is_my_child(pid_t pid); |
| 83 |
int pidref_is_my_child(PidRef *pidref); |
| 84 |
int pidref_from_same_root_fs(PidRef *a, PidRef *b); |
| 85 |
|
| 86 |
bool is_main_thread(void); |
| 87 |
|
| 88 |
#ifndef PERSONALITY_INVALID |
| 89 |
/* personality(2) documents that 0xFFFFFFFFUL is used for querying the |
| 90 |
* current personality, hence let's use that here as error |
| 91 |
* indicator. */ |
| 92 |
#define PERSONALITY_INVALID 0xFFFFFFFFUL |
| 93 |
#endif |
| 94 |
|
| 95 |
/* The personality() syscall returns a 32-bit value where the top three bytes are reserved for flags that |
| 96 |
* emulate historical or architectural quirks, and only the least significant byte reflects the actual |
| 97 |
* personality we're interested in. */ |
| 98 |
#define OPINIONATED_PERSONALITY_MASK 0xFFUL |
| 99 |
|
| 100 |
unsigned long personality_from_string(const char *s); |
| 101 |
const char* personality_to_string(unsigned long p); |
| 102 |
|
| 103 |
int safe_personality(unsigned long p); |
| 104 |
int opinionated_personality(unsigned long *ret); |
| 105 |
|
| 106 |
DECLARE_STRING_TABLE_LOOKUP(sigchld_code, int); |
| 107 |
DECLARE_STRING_TABLE_LOOKUP_WITH_FALLBACK(sched_policy, int); |
| 108 |
|
| 109 |
static inline pid_t PTR_TO_PID(const void *p) { |
| 110 |
return (pid_t) ((uintptr_t) p); |
| 111 |
} |
| 112 |
|
| 113 |
static inline void* PID_TO_PTR(pid_t pid) { |
| 114 |
return (void*) ((uintptr_t) pid); |
| 115 |
} |
| 116 |
|
| 117 |
void valgrind_summary_hack(void); |
| 118 |
|
| 119 |
int pid_compare_func(const pid_t *a, const pid_t *b); |
| 120 |
|
| 121 |
bool nice_is_valid(int n) _const_; |
| 122 |
|
| 123 |
bool sched_policy_is_valid(int policy) _const_; |
| 124 |
bool sched_policy_supported(int policy); |
| 125 |
int sched_get_priority_min_safe(int policy); |
| 126 |
int sched_get_priority_max_safe(int policy); |
| 127 |
|
| 128 |
#define PID_AUTOMATIC ((pid_t) INT_MIN) /* special value indicating "acquire pid from connection peer" */ |
| 129 |
|
| 130 |
static inline bool pid_is_valid(pid_t p) { |
| 131 |
return p > 0; |
| 132 |
} |
| 133 |
|
| 134 |
static inline bool pid_is_automatic(pid_t p) { |
| 135 |
return p == PID_AUTOMATIC; |
| 136 |
} |
| 137 |
|
| 138 |
pid_t getpid_cached(void); |
| 139 |
void reset_cached_pid(void); |
| 140 |
|
| 141 |
int must_be_root(void); |
| 142 |
|
| 143 |
pid_t clone_with_nested_stack(int (*fn)(void *), int flags, void *userdata); |
| 144 |
|
| 145 |
/* 💣 Note that FORK_NEW_USERNS, FORK_NEW_MOUNTNS, FORK_NEW_NETNS or FORK_NEW_PIDNS should not be called in threaded |
| 146 |
* programs, because they cause us to use raw_clone() which does not synchronize the glibc malloc() locks, |
| 147 |
* and thus will cause deadlocks if the parent uses threads and the child does memory allocations. Hence: if |
| 148 |
* the parent is threaded these flags may not be used. These flags cannot be used if the parent uses threads |
| 149 |
* or the child uses malloc(). 💣 */ |
| 150 |
typedef enum ForkFlags { |
| 151 |
FORK_RESET_SIGNALS = 1 << 0, /* Reset all signal handlers and signal mask */ |
| 152 |
FORK_CLOSE_ALL_FDS = 1 << 1, /* Close all open file descriptors in the child, except for 0,1,2 */ |
| 153 |
FORK_DEATHSIG_SIGTERM = 1 << 2, /* Set PR_DEATHSIG in the child to SIGTERM */ |
| 154 |
FORK_DEATHSIG_SIGINT = 1 << 3, /* Set PR_DEATHSIG in the child to SIGINT */ |
| 155 |
FORK_DEATHSIG_SIGKILL = 1 << 4, /* Set PR_DEATHSIG in the child to SIGKILL */ |
| 156 |
FORK_REARRANGE_STDIO = 1 << 5, /* Connect 0,1,2 to specified fds or /dev/null */ |
| 157 |
FORK_REOPEN_LOG = 1 << 6, /* Reopen log connection */ |
| 158 |
FORK_LOG = 1 << 7, /* Log above LOG_DEBUG log level about failures */ |
| 159 |
FORK_WAIT = 1 << 8, /* Wait until child exited */ |
| 160 |
FORK_MOUNTNS_SLAVE = 1 << 9, /* Make child's mount namespace MS_SLAVE */ |
| 161 |
FORK_PRIVATE_TMP = 1 << 10, /* Mount new /tmp/ in the child (combine with FORK_NEW_MOUNTNS!) */ |
| 162 |
FORK_RLIMIT_NOFILE_SAFE = 1 << 11, /* Set RLIMIT_NOFILE soft limit to 1K for select() compat */ |
| 163 |
FORK_STDOUT_TO_STDERR = 1 << 12, /* Make stdout a copy of stderr */ |
| 164 |
FORK_FLUSH_STDIO = 1 << 13, /* fflush() stdout (and stderr) before forking */ |
| 165 |
FORK_CLOEXEC_OFF = 1 << 14, /* In the child: turn off O_CLOEXEC on all fds in except_fds[] */ |
| 166 |
FORK_KEEP_NOTIFY_SOCKET = 1 << 15, /* Unless this specified, $NOTIFY_SOCKET will be unset. */ |
| 167 |
FORK_DETACH = 1 << 16, /* Double fork if needed to ensure PID1/subreaper is parent */ |
| 168 |
FORK_PACK_FDS = 1 << 17, /* Rearrange the passed FDs to be FD 3,4,5,etc. Updates the array in place (combine with FORK_CLOSE_ALL_FDS!) */ |
| 169 |
FORK_NEW_MOUNTNS = 1 << 18, /* Run child in its own mount namespace 💣 DO NOT USE IN THREADED PROGRAMS! 💣 */ |
| 170 |
FORK_NEW_USERNS = 1 << 19, /* Run child in its own user namespace 💣 DO NOT USE IN THREADED PROGRAMS! 💣 */ |
| 171 |
FORK_NEW_NETNS = 1 << 20, /* Run child in its own network namespace 💣 DO NOT USE IN THREADED PROGRAMS! 💣 */ |
| 172 |
FORK_NEW_PIDNS = 1 << 21, /* Run child in its own PID namespace 💣 DO NOT USE IN THREADED PROGRAMS! 💣 */ |
| 173 |
FORK_FREEZE = 1 << 22, /* Don't return in child, just call freeze() instead */ |
| 174 |
FORK_ALLOW_DLOPEN = 1 << 23, /* Do not block dlopen() in child */ |
| 175 |
} ForkFlags; |
| 176 |
|
| 177 |
int pidref_safe_fork_full( |
| 178 |
const char *name, |
| 179 |
const int stdio_fds[3], |
| 180 |
int except_fds[], |
| 181 |
size_t n_except_fds, |
| 182 |
ForkFlags flags, |
| 183 |
PidRef *ret); |
| 184 |
|
| 185 |
static inline int pidref_safe_fork(const char *name, ForkFlags flags, PidRef *ret) { |
| 186 |
return pidref_safe_fork_full(name, NULL, NULL, 0, flags, ret); |
| 187 |
} |
| 188 |
|
| 189 |
int namespace_fork_full( |
| 190 |
const char *outer_name, |
| 191 |
const char *inner_name, |
| 192 |
int except_fds[], |
| 193 |
size_t n_except_fds, |
| 194 |
ForkFlags flags, |
| 195 |
int pidns_fd, |
| 196 |
int mntns_fd, |
| 197 |
int netns_fd, |
| 198 |
int userns_fd, |
| 199 |
int root_fd, |
| 200 |
PidRef *ret); |
| 201 |
|
| 202 |
static inline int namespace_fork( |
| 203 |
const char *outer_name, |
| 204 |
const char *inner_name, |
| 205 |
ForkFlags flags, |
| 206 |
int pidns_fd, |
| 207 |
int mntns_fd, |
| 208 |
int netns_fd, |
| 209 |
int userns_fd, |
| 210 |
int root_fd, |
| 211 |
PidRef *ret) { |
| 212 |
|
| 213 |
return namespace_fork_full(outer_name, inner_name, NULL, 0, flags, |
| 214 |
pidns_fd, mntns_fd, netns_fd, userns_fd, root_fd, |
| 215 |
ret); |
| 216 |
} |
| 217 |
|
| 218 |
bool oom_score_adjust_is_valid(int oa); |
| 219 |
int set_oom_score_adjust(int value); |
| 220 |
int get_oom_score_adjust(int *ret); |
| 221 |
|
| 222 |
/* The highest possibly (theoretic) pid_t value on this architecture. */ |
| 223 |
#define PID_T_MAX ((pid_t) INT32_MAX) |
| 224 |
/* The maximum number of concurrent processes Linux allows on this architecture, as well as the highest valid PID value |
| 225 |
* the kernel will potentially assign. This reflects a value compiled into the kernel (PID_MAX_LIMIT), and sets the |
| 226 |
* upper boundary on what may be written to the /proc/sys/kernel/pid_max sysctl (but do note that the sysctl is off by |
| 227 |
* 1, since PID 0 can never exist and there can hence only be one process less than the limit would suggest). Since |
| 228 |
* these values are documented in proc(5) we feel quite confident that they are stable enough for the near future at |
| 229 |
* least to define them here too. */ |
| 230 |
#define TASKS_MAX 4194303U |
| 231 |
|
| 232 |
assert_cc(TASKS_MAX <= (unsigned long) PID_T_MAX); |
| 233 |
|
| 234 |
int setpriority_closest(int priority); |
| 235 |
|
| 236 |
_noreturn_ void freeze(void); |
| 237 |
|
| 238 |
int get_process_threads(pid_t pid); |
| 239 |
|
| 240 |
int is_reaper_process(void); |
| 241 |
int make_reaper_process(bool b); |
| 242 |
|
| 243 |
int posix_spawn_wrapper( |
| 244 |
const char *path, |
| 245 |
char * const *argv, |
| 246 |
char * const *envp, |
| 247 |
const char *cgroup, |
| 248 |
PidRef *ret_pidref); |
| 249 |
|
| 250 |
int proc_dir_open(DIR **ret); |
| 251 |
int proc_dir_read(DIR *d, pid_t *ret); |
| 252 |
int proc_dir_read_pidref(DIR *d, PidRef *ret); |
| 253 |
|
| 254 |
int safe_mlockall(int flags); |
| 255 |
|
| 256 |
_noreturn_ void report_errno_and_exit(int errno_fd, int error); |
| 257 |
int read_errno(int errno_fd); |