1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
12 #include <sys/mount.h>
13 #include <sys/personality.h>
14 #include <sys/prctl.h>
15 #include <sys/types.h>
19 #if HAVE_VALGRIND_VALGRIND_H
20 #include <valgrind/valgrind.h>
23 #include "alloc-util.h"
24 #include "architecture.h"
26 #include "errno-util.h"
31 #include "locale-util.h"
34 #include "memory-util.h"
35 #include "missing_sched.h"
36 #include "missing_syscall.h"
37 #include "namespace-util.h"
38 #include "path-util.h"
39 #include "process-util.h"
40 #include "raw-clone.h"
41 #include "rlimit-util.h"
42 #include "signal-util.h"
43 #include "stat-util.h"
44 #include "stdio-util.h"
45 #include "string-table.h"
46 #include "string-util.h"
47 #include "terminal-util.h"
48 #include "user-util.h"
51 /* The kernel limits userspace processes to TASK_COMM_LEN (16 bytes), but allows higher values for its own
52 * workers, e.g. "kworker/u9:3-kcryptd/253:0". Let's pick a fixed smallish limit that will work for the kernel.
54 #define COMM_MAX_LEN 128
56 static int get_process_state(pid_t pid
) {
57 _cleanup_free_
char *line
= NULL
;
64 /* Shortcut: if we are enquired about our own state, we are obviously running */
65 if (pid
== 0 || pid
== getpid_cached())
66 return (unsigned char) 'R';
68 p
= procfs_file_alloca(pid
, "stat");
70 r
= read_one_line_file(p
, &line
);
76 p
= strrchr(line
, ')');
82 if (sscanf(p
, " %c", &state
) != 1)
85 return (unsigned char) state
;
88 int get_process_comm(pid_t pid
, char **ret
) {
89 _cleanup_free_
char *escaped
= NULL
, *comm
= NULL
;
95 if (pid
== 0 || pid
== getpid_cached()) {
96 comm
= new0(char, TASK_COMM_LEN
+ 1); /* Must fit in 16 byte according to prctl(2) */
100 if (prctl(PR_GET_NAME
, comm
) < 0)
105 p
= procfs_file_alloca(pid
, "comm");
107 /* Note that process names of kernel threads can be much longer than TASK_COMM_LEN */
108 r
= read_one_line_file(p
, &comm
);
115 escaped
= new(char, COMM_MAX_LEN
);
119 /* Escape unprintable characters, just in case, but don't grow the string beyond the underlying size */
120 cellescape(escaped
, COMM_MAX_LEN
, comm
);
122 *ret
= TAKE_PTR(escaped
);
126 static int get_process_cmdline_nulstr(
129 ProcessCmdlineFlags flags
,
138 /* Retrieves a process' command line as a "sized nulstr", i.e. possibly without the last NUL, but
139 * with a specified size.
141 * If PROCESS_CMDLINE_COMM_FALLBACK is specified in flags and the process has no command line set
142 * (the case for kernel threads), or has a command line that resolves to the empty string, will
143 * return the "comm" name of the process instead. This will use at most _SC_ARG_MAX bytes of input
146 * Returns an error, 0 if output was read but is truncated, 1 otherwise.
149 p
= procfs_file_alloca(pid
, "cmdline");
150 r
= read_virtual_file(p
, max_size
, &t
, &k
); /* Let's assume that each input byte results in >= 1
151 * columns of output. We ignore zero-width codepoints. */
160 if (!(flags
& PROCESS_CMDLINE_COMM_FALLBACK
))
163 /* Kernel threads have no argv[] */
164 _cleanup_free_
char *comm
= NULL
;
166 r
= get_process_comm(pid
, &comm
);
170 t
= strjoin("[", comm
, "]");
176 if (r
== 0) /* truncation */
185 int get_process_cmdline(pid_t pid
, size_t max_columns
, ProcessCmdlineFlags flags
, char **ret
) {
186 _cleanup_free_
char *t
= NULL
;
193 /* Retrieve and format a commandline. See above for discussion of retrieval options.
195 * There are two main formatting modes:
197 * - when PROCESS_CMDLINE_QUOTE is specified, output is quoted in C/Python style. If no shell special
198 * characters are present, this output can be copy-pasted into the terminal to execute. UTF-8
201 * - otherwise, a compact non-roundtrippable form is returned. Non-UTF8 bytes are replaced by �. The
202 * returned string is of the specified console width at most, abbreviated with an ellipsis.
204 * Returns -ESRCH if the process doesn't exist, and -ENOENT if the process has no command line (and
205 * PROCESS_CMDLINE_COMM_FALLBACK is not specified). Returns 0 and sets *line otherwise. */
207 int full
= get_process_cmdline_nulstr(pid
, max_columns
, flags
, &t
, &k
);
211 if (flags
& (PROCESS_CMDLINE_QUOTE
| PROCESS_CMDLINE_QUOTE_POSIX
)) {
212 ShellEscapeFlags shflags
= SHELL_ESCAPE_EMPTY
|
213 FLAGS_SET(flags
, PROCESS_CMDLINE_QUOTE_POSIX
) * SHELL_ESCAPE_POSIX
;
215 assert(!(flags
& PROCESS_CMDLINE_USE_LOCALE
));
217 _cleanup_strv_free_
char **args
= NULL
;
220 args
= strv_parse_nulstr(t
, k
);
224 /* Drop trailing empty strings. See issue #21186. */
225 STRV_FOREACH_BACKWARDS(p
, args
) {
232 ans
= quote_command_line(args
, shflags
);
236 /* Arguments are separated by NULs. Let's replace those with spaces. */
237 for (size_t i
= 0; i
< k
- 1; i
++)
241 delete_trailing_chars(t
, WHITESPACE
);
243 bool eight_bit
= (flags
& PROCESS_CMDLINE_USE_LOCALE
) && !is_locale_utf8();
245 ans
= escape_non_printable_full(t
, max_columns
,
246 eight_bit
* XESCAPE_8_BIT
| !full
* XESCAPE_FORCE_ELLIPSIS
);
250 ans
= str_realloc(ans
);
257 static int update_argv(const char name
[], size_t l
) {
258 static int can_do
= -1;
262 can_do
= false; /* We'll set it to true only if the whole process works */
264 /* Let's not bother with this if we don't have euid == 0. Strictly speaking we should check for the
265 * CAP_SYS_RESOURCE capability which is independent of the euid. In our own code the capability generally is
266 * present only for euid == 0, hence let's use this as quick bypass check, to avoid calling mmap() if
267 * PR_SET_MM_ARG_{START,END} fails with EPERM later on anyway. After all geteuid() is dead cheap to call, but
270 return log_debug_errno(SYNTHETIC_ERRNO(EPERM
),
271 "Skipping PR_SET_MM, as we don't have privileges.");
273 static size_t mm_size
= 0;
274 static char *mm
= NULL
;
281 nn_size
= PAGE_ALIGN(l
+1);
282 nn
= mmap(NULL
, nn_size
, PROT_READ
|PROT_WRITE
, MAP_PRIVATE
|MAP_ANONYMOUS
, -1, 0);
283 if (nn
== MAP_FAILED
)
284 return log_debug_errno(errno
, "mmap() failed: %m");
286 strncpy(nn
, name
, nn_size
);
288 /* Now, let's tell the kernel about this new memory */
289 if (prctl(PR_SET_MM
, PR_SET_MM_ARG_START
, (unsigned long) nn
, 0, 0) < 0) {
290 if (ERRNO_IS_PRIVILEGE(errno
))
291 return log_debug_errno(errno
, "PR_SET_MM_ARG_START failed: %m");
293 /* HACK: prctl() API is kind of dumb on this point. The existing end address may already be
294 * below the desired start address, in which case the kernel may have kicked this back due
295 * to a range-check failure (see linux/kernel/sys.c:validate_prctl_map() to see this in
296 * action). The proper solution would be to have a prctl() API that could set both start+end
297 * simultaneously, or at least let us query the existing address to anticipate this condition
298 * and respond accordingly. For now, we can only guess at the cause of this failure and try
299 * a workaround--which will briefly expand the arg space to something potentially huge before
300 * resizing it to what we want. */
301 log_debug_errno(errno
, "PR_SET_MM_ARG_START failed, attempting PR_SET_MM_ARG_END hack: %m");
303 if (prctl(PR_SET_MM
, PR_SET_MM_ARG_END
, (unsigned long) nn
+ l
+ 1, 0, 0) < 0) {
304 r
= log_debug_errno(errno
, "PR_SET_MM_ARG_END hack failed, proceeding without: %m");
305 (void) munmap(nn
, nn_size
);
309 if (prctl(PR_SET_MM
, PR_SET_MM_ARG_START
, (unsigned long) nn
, 0, 0) < 0)
310 return log_debug_errno(errno
, "PR_SET_MM_ARG_START still failed, proceeding without: %m");
312 /* And update the end pointer to the new end, too. If this fails, we don't really know what
313 * to do, it's pretty unlikely that we can rollback, hence we'll just accept the failure,
315 if (prctl(PR_SET_MM
, PR_SET_MM_ARG_END
, (unsigned long) nn
+ l
+ 1, 0, 0) < 0)
316 log_debug_errno(errno
, "PR_SET_MM_ARG_END failed, proceeding without: %m");
320 (void) munmap(mm
, mm_size
);
325 strncpy(mm
, name
, mm_size
);
327 /* Update the end pointer, continuing regardless of any failure. */
328 if (prctl(PR_SET_MM
, PR_SET_MM_ARG_END
, (unsigned long) mm
+ l
+ 1, 0, 0) < 0)
329 log_debug_errno(errno
, "PR_SET_MM_ARG_END failed, proceeding without: %m");
336 int rename_process(const char name
[]) {
337 bool truncated
= false;
339 /* This is a like a poor man's setproctitle(). It changes the comm field, argv[0], and also the glibc's
340 * internally used name of the process. For the first one a limit of 16 chars applies; to the second one in
341 * many cases one of 10 (i.e. length of "/sbin/init") — however if we have CAP_SYS_RESOURCES it is unbounded;
342 * to the third one 7 (i.e. the length of "systemd". If you pass a longer string it will likely be
345 * Returns 0 if a name was set but truncated, > 0 if it was set but not truncated. */
348 return -EINVAL
; /* let's not confuse users unnecessarily with an empty name */
350 if (!is_main_thread())
351 return -EPERM
; /* Let's not allow setting the process name from other threads than the main one, as we
352 * cache things without locking, and we make assumptions that PR_SET_NAME sets the
353 * process name that isn't correct on any other threads */
355 size_t l
= strlen(name
);
357 /* First step, change the comm field. The main thread's comm is identical to the process comm. This means we
358 * can use PR_SET_NAME, which sets the thread name for the calling thread. */
359 if (prctl(PR_SET_NAME
, name
) < 0)
360 log_debug_errno(errno
, "PR_SET_NAME failed: %m");
361 if (l
>= TASK_COMM_LEN
) /* Linux userspace process names can be 15 chars at max */
364 /* Second step, change glibc's ID of the process name. */
365 if (program_invocation_name
) {
368 k
= strlen(program_invocation_name
);
369 strncpy(program_invocation_name
, name
, k
);
374 /* Third step, completely replace the argv[] array the kernel maintains for us. This requires privileges, but
375 * has the advantage that the argv[] array is exactly what we want it to be, and not filled up with zeros at
376 * the end. This is the best option for changing /proc/self/cmdline. */
377 (void) update_argv(name
, l
);
379 /* Fourth step: in all cases we'll also update the original argv[], so that our own code gets it right too if
380 * it still looks here */
381 if (saved_argc
> 0) {
385 k
= strlen(saved_argv
[0]);
386 strncpy(saved_argv
[0], name
, k
);
391 for (int i
= 1; i
< saved_argc
; i
++) {
395 memzero(saved_argv
[i
], strlen(saved_argv
[i
]));
402 int is_kernel_thread(pid_t pid
) {
403 _cleanup_free_
char *line
= NULL
;
404 unsigned long long flags
;
410 if (IN_SET(pid
, 0, 1) || pid
== getpid_cached()) /* pid 1, and we ourselves certainly aren't a kernel thread */
412 if (!pid_is_valid(pid
))
415 p
= procfs_file_alloca(pid
, "stat");
416 r
= read_one_line_file(p
, &line
);
422 /* Skip past the comm field */
423 q
= strrchr(line
, ')');
428 /* Skip 6 fields to reach the flags field */
429 for (i
= 0; i
< 6; i
++) {
430 l
= strspn(q
, WHITESPACE
);
435 l
= strcspn(q
, WHITESPACE
);
441 /* Skip preceding whitespace */
442 l
= strspn(q
, WHITESPACE
);
447 /* Truncate the rest */
448 l
= strcspn(q
, WHITESPACE
);
453 r
= safe_atollu(q
, &flags
);
457 return !!(flags
& PF_KTHREAD
);
460 int get_process_capeff(pid_t pid
, char **ret
) {
467 p
= procfs_file_alloca(pid
, "status");
469 r
= get_proc_field(p
, "CapEff", WHITESPACE
, ret
);
476 static int get_process_link_contents(const char *proc_file
, char **ret
) {
482 r
= readlink_malloc(proc_file
, ret
);
491 int get_process_exe(pid_t pid
, char **ret
) {
499 p
= procfs_file_alloca(pid
, "exe");
500 r
= get_process_link_contents(p
, ret
);
504 d
= endswith(*ret
, " (deleted)");
511 static int get_process_id(pid_t pid
, const char *field
, uid_t
*ret
) {
512 _cleanup_fclose_
FILE *f
= NULL
;
522 p
= procfs_file_alloca(pid
, "status");
523 r
= fopen_unlocked(p
, "re", &f
);
530 _cleanup_free_
char *line
= NULL
;
533 r
= read_line(f
, LONG_LINE_MAX
, &line
);
541 if (startswith(l
, field
)) {
543 l
+= strspn(l
, WHITESPACE
);
545 l
[strcspn(l
, WHITESPACE
)] = 0;
547 return parse_uid(l
, ret
);
554 int get_process_uid(pid_t pid
, uid_t
*ret
) {
556 if (pid
== 0 || pid
== getpid_cached()) {
561 return get_process_id(pid
, "Uid:", ret
);
564 int get_process_gid(pid_t pid
, gid_t
*ret
) {
566 if (pid
== 0 || pid
== getpid_cached()) {
571 assert_cc(sizeof(uid_t
) == sizeof(gid_t
));
572 return get_process_id(pid
, "Gid:", ret
);
575 int get_process_cwd(pid_t pid
, char **ret
) {
581 if (pid
== 0 || pid
== getpid_cached())
582 return safe_getcwd(ret
);
584 p
= procfs_file_alloca(pid
, "cwd");
586 return get_process_link_contents(p
, ret
);
589 int get_process_root(pid_t pid
, char **ret
) {
595 p
= procfs_file_alloca(pid
, "root");
597 return get_process_link_contents(p
, ret
);
600 #define ENVIRONMENT_BLOCK_MAX (5U*1024U*1024U)
602 int get_process_environ(pid_t pid
, char **ret
) {
603 _cleanup_fclose_
FILE *f
= NULL
;
604 _cleanup_free_
char *outcome
= NULL
;
612 p
= procfs_file_alloca(pid
, "environ");
614 r
= fopen_unlocked(p
, "re", &f
);
623 if (sz
>= ENVIRONMENT_BLOCK_MAX
)
626 if (!GREEDY_REALLOC(outcome
, sz
+ 5))
629 r
= safe_fgetc(f
, &c
);
636 outcome
[sz
++] = '\n';
638 sz
+= cescape_char(c
, outcome
+ sz
);
642 *ret
= TAKE_PTR(outcome
);
647 int get_process_ppid(pid_t pid
, pid_t
*ret
) {
648 _cleanup_free_
char *line
= NULL
;
655 if (pid
== 0 || pid
== getpid_cached()) {
661 if (pid
== 1) /* PID 1 has no parent, shortcut this case */
662 return -EADDRNOTAVAIL
;
664 p
= procfs_file_alloca(pid
, "stat");
665 r
= read_one_line_file(p
, &line
);
671 /* Let's skip the pid and comm fields. The latter is enclosed in () but does not escape any () in its
672 * value, so let's skip over it manually */
674 p
= strrchr(line
, ')');
686 /* If ppid is zero the process has no parent. Which might be the case for PID 1 but also for
687 * processes originating in other namespaces that are inserted into a pidns. Return a recognizable
688 * error in this case. */
690 return -EADDRNOTAVAIL
;
692 if ((pid_t
) ppid
< 0 || (unsigned long) (pid_t
) ppid
!= ppid
)
701 int get_process_umask(pid_t pid
, mode_t
*ret
) {
702 _cleanup_free_
char *m
= NULL
;
709 p
= procfs_file_alloca(pid
, "status");
711 r
= get_proc_field(p
, "Umask", WHITESPACE
, &m
);
715 return parse_mode(m
, ret
);
718 int wait_for_terminate(pid_t pid
, siginfo_t
*status
) {
729 if (waitid(P_PID
, pid
, status
, WEXITED
) < 0) {
734 return negative_errno();
743 * < 0 : wait_for_terminate() failed to get the state of the
744 * process, the process was terminated by a signal, or
745 * failed for an unknown reason.
746 * >=0 : The process terminated normally, and its exit code is
749 * That is, success is indicated by a return value of zero, and an
750 * error is indicated by a non-zero value.
752 * A warning is emitted if the process terminates abnormally,
753 * and also if it returns non-zero unless check_exit_code is true.
755 int wait_for_terminate_and_check(const char *name
, pid_t pid
, WaitFlags flags
) {
756 _cleanup_free_
char *buffer
= NULL
;
763 r
= get_process_comm(pid
, &buffer
);
765 log_debug_errno(r
, "Failed to acquire process name of " PID_FMT
", ignoring: %m", pid
);
770 prio
= flags
& WAIT_LOG_ABNORMAL
? LOG_ERR
: LOG_DEBUG
;
772 r
= wait_for_terminate(pid
, &status
);
774 return log_full_errno(prio
, r
, "Failed to wait for %s: %m", strna(name
));
776 if (status
.si_code
== CLD_EXITED
) {
777 if (status
.si_status
!= EXIT_SUCCESS
)
778 log_full(flags
& WAIT_LOG_NON_ZERO_EXIT_STATUS
? LOG_ERR
: LOG_DEBUG
,
779 "%s failed with exit status %i.", strna(name
), status
.si_status
);
781 log_debug("%s succeeded.", name
);
783 return status
.si_status
;
785 } else if (IN_SET(status
.si_code
, CLD_KILLED
, CLD_DUMPED
)) {
787 log_full(prio
, "%s terminated by signal %s.", strna(name
), signal_to_string(status
.si_status
));
791 log_full(prio
, "%s failed due to unknown reason.", strna(name
));
798 * < 0 : wait_for_terminate_with_timeout() failed to get the state of the process, the process timed out, the process
799 * was terminated by a signal, or failed for an unknown reason.
801 * >=0 : The process terminated normally with no failures.
803 * Success is indicated by a return value of zero, a timeout is indicated by ETIMEDOUT, and all other child failure
804 * states are indicated by error is indicated by a non-zero value.
806 * This call assumes SIGCHLD has been blocked already, in particular before the child to wait for has been forked off
807 * to remain entirely race-free.
809 int wait_for_terminate_with_timeout(pid_t pid
, usec_t timeout
) {
814 assert_se(sigemptyset(&mask
) == 0);
815 assert_se(sigaddset(&mask
, SIGCHLD
) == 0);
817 /* Drop into a sigtimewait-based timeout. Waiting for the
819 until
= usec_add(now(CLOCK_MONOTONIC
), timeout
);
822 siginfo_t status
= {};
825 n
= now(CLOCK_MONOTONIC
);
829 r
= RET_NERRNO(sigtimedwait(&mask
, NULL
, timespec_store(&ts
, until
- n
)));
830 /* Assuming we woke due to the child exiting. */
831 if (waitid(P_PID
, pid
, &status
, WEXITED
|WNOHANG
) == 0) {
832 if (status
.si_pid
== pid
) {
833 /* This is the correct child. */
834 if (status
.si_code
== CLD_EXITED
)
835 return (status
.si_status
== 0) ? 0 : -EPROTO
;
840 /* Not the child, check for errors and proceed appropriately */
844 /* Timed out, child is likely hung. */
847 /* Received a different signal and should retry */
850 /* Return any unexpected errors */
859 void sigkill_wait(pid_t pid
) {
862 (void) kill(pid
, SIGKILL
);
863 (void) wait_for_terminate(pid
, NULL
);
866 void sigkill_waitp(pid_t
*pid
) {
877 void sigterm_wait(pid_t pid
) {
880 (void) kill_and_sigcont(pid
, SIGTERM
);
881 (void) wait_for_terminate(pid
, NULL
);
884 int kill_and_sigcont(pid_t pid
, int sig
) {
887 r
= RET_NERRNO(kill(pid
, sig
));
889 /* If this worked, also send SIGCONT, unless we already just sent a SIGCONT, or SIGKILL was sent which isn't
890 * affected by a process being suspended anyway. */
891 if (r
>= 0 && !IN_SET(sig
, SIGCONT
, SIGKILL
))
892 (void) kill(pid
, SIGCONT
);
897 int getenv_for_pid(pid_t pid
, const char *field
, char **ret
) {
898 _cleanup_fclose_
FILE *f
= NULL
;
908 if (pid
== 0 || pid
== getpid_cached()) {
925 if (!pid_is_valid(pid
))
928 path
= procfs_file_alloca(pid
, "environ");
930 r
= fopen_unlocked(path
, "re", &f
);
938 _cleanup_free_
char *line
= NULL
;
940 if (sum
> ENVIRONMENT_BLOCK_MAX
) /* Give up searching eventually */
943 r
= read_nul_string(f
, LONG_LINE_MAX
, &line
);
946 if (r
== 0) /* EOF */
951 if (strneq(line
, field
, l
) && line
[l
] == '=') {
952 value
= strdup(line
+ l
+ 1);
965 int pid_is_my_child(pid_t pid
) {
972 r
= get_process_ppid(pid
, &ppid
);
976 return ppid
== getpid_cached();
979 bool pid_is_unwaited(pid_t pid
) {
980 /* Checks whether a PID is still valid at all, including a zombie */
985 if (pid
<= 1) /* If we or PID 1 would be dead and have been waited for, this code would not be running */
988 if (pid
== getpid_cached())
991 if (kill(pid
, 0) >= 0)
994 return errno
!= ESRCH
;
997 bool pid_is_alive(pid_t pid
) {
1000 /* Checks whether a PID is still valid and not a zombie */
1005 if (pid
<= 1) /* If we or PID 1 would be a zombie, this code would not be running */
1008 if (pid
== getpid_cached())
1011 r
= get_process_state(pid
);
1012 if (IN_SET(r
, -ESRCH
, 'Z'))
1018 int pid_from_same_root_fs(pid_t pid
) {
1024 if (pid
== 0 || pid
== getpid_cached())
1027 root
= procfs_file_alloca(pid
, "root");
1029 return files_same(root
, "/proc/1/root", 0);
1032 bool is_main_thread(void) {
1033 static thread_local
int cached
= 0;
1035 if (_unlikely_(cached
== 0))
1036 cached
= getpid_cached() == gettid() ? 1 : -1;
1041 bool oom_score_adjust_is_valid(int oa
) {
1042 return oa
>= OOM_SCORE_ADJ_MIN
&& oa
<= OOM_SCORE_ADJ_MAX
;
1045 unsigned long personality_from_string(const char *p
) {
1049 return PERSONALITY_INVALID
;
1051 /* Parse a personality specifier. We use our own identifiers that indicate specific ABIs, rather than just
1052 * hints regarding the register size, since we want to keep things open for multiple locally supported ABIs for
1053 * the same register size. */
1055 architecture
= architecture_from_string(p
);
1056 if (architecture
< 0)
1057 return PERSONALITY_INVALID
;
1059 if (architecture
== native_architecture())
1061 #ifdef ARCHITECTURE_SECONDARY
1062 if (architecture
== ARCHITECTURE_SECONDARY
)
1066 return PERSONALITY_INVALID
;
1069 const char* personality_to_string(unsigned long p
) {
1070 int architecture
= _ARCHITECTURE_INVALID
;
1073 architecture
= native_architecture();
1074 #ifdef ARCHITECTURE_SECONDARY
1075 else if (p
== PER_LINUX32
)
1076 architecture
= ARCHITECTURE_SECONDARY
;
1079 if (architecture
< 0)
1082 return architecture_to_string(architecture
);
1085 int safe_personality(unsigned long p
) {
1088 /* So here's the deal, personality() is weirdly defined by glibc. In some cases it returns a failure via errno,
1089 * and in others as negative return value containing an errno-like value. Let's work around this: this is a
1090 * wrapper that uses errno if it is set, and uses the return value otherwise. And then it sets both errno and
1091 * the return value indicating the same issue, so that we are definitely on the safe side.
1093 * See https://github.com/systemd/systemd/issues/6737 */
1096 ret
= personality(p
);
1107 int opinionated_personality(unsigned long *ret
) {
1110 /* Returns the current personality, or PERSONALITY_INVALID if we can't determine it. This function is a bit
1111 * opinionated though, and ignores all the finer-grained bits and exotic personalities, only distinguishing the
1112 * two most relevant personalities: PER_LINUX and PER_LINUX32. */
1114 current
= safe_personality(PERSONALITY_INVALID
);
1118 if (((unsigned long) current
& 0xffff) == PER_LINUX32
)
1126 void valgrind_summary_hack(void) {
1127 #if HAVE_VALGRIND_VALGRIND_H
1128 if (getpid_cached() == 1 && RUNNING_ON_VALGRIND
) {
1130 pid
= raw_clone(SIGCHLD
);
1132 log_emergency_errno(errno
, "Failed to fork off valgrind helper: %m");
1136 log_info("Spawned valgrind helper as PID "PID_FMT
".", pid
);
1137 (void) wait_for_terminate(pid
, NULL
);
1143 int pid_compare_func(const pid_t
*a
, const pid_t
*b
) {
1144 /* Suitable for usage in qsort() */
1148 /* The cached PID, possible values:
1150 * == UNSET [0] → cache not initialized yet
1151 * == BUSY [-1] → some thread is initializing it at the moment
1152 * any other → the cached PID
1155 #define CACHED_PID_UNSET ((pid_t) 0)
1156 #define CACHED_PID_BUSY ((pid_t) -1)
1158 static pid_t cached_pid
= CACHED_PID_UNSET
;
1160 void reset_cached_pid(void) {
1161 /* Invoked in the child after a fork(), i.e. at the first moment the PID changed */
1162 cached_pid
= CACHED_PID_UNSET
;
1165 pid_t
getpid_cached(void) {
1166 static bool installed
= false;
1167 pid_t current_value
;
1169 /* getpid_cached() is much like getpid(), but caches the value in local memory, to avoid having to invoke a
1170 * system call each time. This restores glibc behaviour from before 2.24, when getpid() was unconditionally
1171 * cached. Starting with 2.24 getpid() started to become prohibitively expensive when used for detecting when
1172 * objects were used across fork()s. With this caching the old behaviour is somewhat restored.
1174 * https://bugzilla.redhat.com/show_bug.cgi?id=1443976
1175 * https://sourceware.org/git/gitweb.cgi?p=glibc.git;h=c579f48edba88380635ab98cb612030e3ed8691e
1178 current_value
= __sync_val_compare_and_swap(&cached_pid
, CACHED_PID_UNSET
, CACHED_PID_BUSY
);
1180 switch (current_value
) {
1182 case CACHED_PID_UNSET
: { /* Not initialized yet, then do so now */
1185 new_pid
= raw_getpid();
1188 /* __register_atfork() either returns 0 or -ENOMEM, in its glibc implementation. Since it's
1189 * only half-documented (glibc doesn't document it but LSB does — though only superficially)
1190 * we'll check for errors only in the most generic fashion possible. */
1192 if (pthread_atfork(NULL
, NULL
, reset_cached_pid
) != 0) {
1193 /* OOM? Let's try again later */
1194 cached_pid
= CACHED_PID_UNSET
;
1201 cached_pid
= new_pid
;
1205 case CACHED_PID_BUSY
: /* Somebody else is currently initializing */
1206 return raw_getpid();
1208 default: /* Properly initialized */
1209 return current_value
;
1213 int must_be_root(void) {
1218 return log_error_errno(SYNTHETIC_ERRNO(EPERM
), "Need to be root.");
1221 static void restore_sigsetp(sigset_t
**ssp
) {
1223 (void) sigprocmask(SIG_SETMASK
, *ssp
, NULL
);
1228 const int except_fds
[],
1229 size_t n_except_fds
,
1233 pid_t original_pid
, pid
;
1234 sigset_t saved_ss
, ss
;
1235 _unused_
_cleanup_(restore_sigsetp
) sigset_t
*saved_ssp
= NULL
;
1236 bool block_signals
= false, block_all
= false;
1239 /* A wrapper around fork(), that does a couple of important initializations in addition to mere forking. Always
1240 * returns the child's PID in *ret_pid. Returns == 0 in the child, and > 0 in the parent. */
1242 prio
= flags
& FORK_LOG
? LOG_ERR
: LOG_DEBUG
;
1244 original_pid
= getpid_cached();
1246 if (flags
& FORK_FLUSH_STDIO
) {
1248 fflush(stderr
); /* This one shouldn't be necessary, stderr should be unbuffered anyway, but let's better be safe than sorry */
1251 if (flags
& (FORK_RESET_SIGNALS
|FORK_DEATHSIG
)) {
1252 /* We temporarily block all signals, so that the new child has them blocked initially. This way, we can
1253 * be sure that SIGTERMs are not lost we might send to the child. */
1255 assert_se(sigfillset(&ss
) >= 0);
1256 block_signals
= block_all
= true;
1258 } else if (flags
& FORK_WAIT
) {
1259 /* Let's block SIGCHLD at least, so that we can safely watch for the child process */
1261 assert_se(sigemptyset(&ss
) >= 0);
1262 assert_se(sigaddset(&ss
, SIGCHLD
) >= 0);
1263 block_signals
= true;
1266 if (block_signals
) {
1267 if (sigprocmask(SIG_SETMASK
, &ss
, &saved_ss
) < 0)
1268 return log_full_errno(prio
, errno
, "Failed to set signal mask: %m");
1269 saved_ssp
= &saved_ss
;
1272 if ((flags
& (FORK_NEW_MOUNTNS
|FORK_NEW_USERNS
)) != 0)
1273 pid
= raw_clone(SIGCHLD
|
1274 (FLAGS_SET(flags
, FORK_NEW_MOUNTNS
) ? CLONE_NEWNS
: 0) |
1275 (FLAGS_SET(flags
, FORK_NEW_USERNS
) ? CLONE_NEWUSER
: 0));
1279 return log_full_errno(prio
, errno
, "Failed to fork: %m");
1281 /* We are in the parent process */
1283 log_debug("Successfully forked off '%s' as PID " PID_FMT
".", strna(name
), pid
);
1285 if (flags
& FORK_WAIT
) {
1287 /* undo everything except SIGCHLD */
1289 assert_se(sigaddset(&ss
, SIGCHLD
) >= 0);
1290 (void) sigprocmask(SIG_SETMASK
, &ss
, NULL
);
1293 r
= wait_for_terminate_and_check(name
, pid
, (flags
& FORK_LOG
? WAIT_LOG
: 0));
1296 if (r
!= EXIT_SUCCESS
) /* exit status > 0 should be treated as failure, too */
1306 /* We are in the child process */
1308 /* Restore signal mask manually */
1311 if (flags
& FORK_REOPEN_LOG
) {
1312 /* Close the logs if requested, before we log anything. And make sure we reopen it if needed. */
1314 log_set_open_when_needed(true);
1318 r
= rename_process(name
);
1320 log_full_errno(flags
& FORK_LOG
? LOG_WARNING
: LOG_DEBUG
,
1321 r
, "Failed to rename process, ignoring: %m");
1324 if (flags
& (FORK_DEATHSIG
|FORK_DEATHSIG_SIGINT
))
1325 if (prctl(PR_SET_PDEATHSIG
, (flags
& FORK_DEATHSIG_SIGINT
) ? SIGINT
: SIGTERM
) < 0) {
1326 log_full_errno(prio
, errno
, "Failed to set death signal: %m");
1327 _exit(EXIT_FAILURE
);
1330 if (flags
& FORK_RESET_SIGNALS
) {
1331 r
= reset_all_signal_handlers();
1333 log_full_errno(prio
, r
, "Failed to reset signal handlers: %m");
1334 _exit(EXIT_FAILURE
);
1337 /* This implicitly undoes the signal mask stuff we did before the fork()ing above */
1338 r
= reset_signal_mask();
1340 log_full_errno(prio
, r
, "Failed to reset signal mask: %m");
1341 _exit(EXIT_FAILURE
);
1343 } else if (block_signals
) { /* undo what we did above */
1344 if (sigprocmask(SIG_SETMASK
, &saved_ss
, NULL
) < 0) {
1345 log_full_errno(prio
, errno
, "Failed to restore signal mask: %m");
1346 _exit(EXIT_FAILURE
);
1350 if (flags
& FORK_DEATHSIG
) {
1352 /* Let's see if the parent PID is still the one we started from? If not, then the parent
1353 * already died by the time we set PR_SET_PDEATHSIG, hence let's emulate the effect */
1357 /* Parent is in a different PID namespace. */;
1358 else if (ppid
!= original_pid
) {
1359 log_debug("Parent died early, raising SIGTERM.");
1360 (void) raise(SIGTERM
);
1361 _exit(EXIT_FAILURE
);
1365 if (FLAGS_SET(flags
, FORK_NEW_MOUNTNS
| FORK_MOUNTNS_SLAVE
)) {
1367 /* Optionally, make sure we never propagate mounts to the host. */
1369 if (mount(NULL
, "/", NULL
, MS_SLAVE
| MS_REC
, NULL
) < 0) {
1370 log_full_errno(prio
, errno
, "Failed to remount root directory as MS_SLAVE: %m");
1371 _exit(EXIT_FAILURE
);
1375 if (flags
& FORK_CLOSE_ALL_FDS
) {
1376 /* Close the logs here in case it got reopened above, as close_all_fds() would close them for us */
1379 r
= close_all_fds(except_fds
, n_except_fds
);
1381 log_full_errno(prio
, r
, "Failed to close all file descriptors: %m");
1382 _exit(EXIT_FAILURE
);
1386 /* When we were asked to reopen the logs, do so again now */
1387 if (flags
& FORK_REOPEN_LOG
) {
1389 log_set_open_when_needed(false);
1392 if (flags
& FORK_NULL_STDIO
) {
1393 r
= make_null_stdio();
1395 log_full_errno(prio
, r
, "Failed to connect stdin/stdout to /dev/null: %m");
1396 _exit(EXIT_FAILURE
);
1399 } else if (flags
& FORK_STDOUT_TO_STDERR
) {
1400 if (dup2(STDERR_FILENO
, STDOUT_FILENO
) < 0) {
1401 log_full_errno(prio
, errno
, "Failed to connect stdout to stderr: %m");
1402 _exit(EXIT_FAILURE
);
1406 if (flags
& FORK_RLIMIT_NOFILE_SAFE
) {
1407 r
= rlimit_nofile_safe();
1409 log_full_errno(prio
, r
, "Failed to lower RLIMIT_NOFILE's soft limit to 1K: %m");
1410 _exit(EXIT_FAILURE
);
1415 *ret_pid
= getpid_cached();
1421 const char *outer_name
,
1422 const char *inner_name
,
1423 const int except_fds
[],
1424 size_t n_except_fds
,
1435 /* This is much like safe_fork(), but forks twice, and joins the specified namespaces in the middle
1436 * process. This ensures that we are fully a member of the destination namespace, with pidns an all, so that
1437 * /proc/self/fd works correctly. */
1439 r
= safe_fork_full(outer_name
, except_fds
, n_except_fds
, (flags
|FORK_DEATHSIG
) & ~(FORK_REOPEN_LOG
|FORK_NEW_MOUNTNS
|FORK_MOUNTNS_SLAVE
), ret_pid
);
1447 r
= namespace_enter(pidns_fd
, mntns_fd
, netns_fd
, userns_fd
, root_fd
);
1449 log_full_errno(FLAGS_SET(flags
, FORK_LOG
) ? LOG_ERR
: LOG_DEBUG
, r
, "Failed to join namespace: %m");
1450 _exit(EXIT_FAILURE
);
1453 /* We mask a few flags here that either make no sense for the grandchild, or that we don't have to do again */
1454 r
= safe_fork_full(inner_name
, except_fds
, n_except_fds
, flags
& ~(FORK_WAIT
|FORK_RESET_SIGNALS
|FORK_CLOSE_ALL_FDS
|FORK_NULL_STDIO
), &pid
);
1456 _exit(EXIT_FAILURE
);
1464 r
= wait_for_terminate_and_check(inner_name
, pid
, FLAGS_SET(flags
, FORK_LOG
) ? WAIT_LOG
: 0);
1466 _exit(EXIT_FAILURE
);
1474 int set_oom_score_adjust(int value
) {
1475 char t
[DECIMAL_STR_MAX(int)];
1477 xsprintf(t
, "%i", value
);
1479 return write_string_file("/proc/self/oom_score_adj", t
,
1480 WRITE_STRING_FILE_VERIFY_ON_FAILURE
|WRITE_STRING_FILE_DISABLE_BUFFER
);
1483 int get_oom_score_adjust(int *ret
) {
1484 _cleanup_free_
char *t
= NULL
;
1487 r
= read_virtual_file("/proc/self/oom_score_adj", SIZE_MAX
, &t
, NULL
);
1491 delete_trailing_chars(t
, WHITESPACE
);
1493 assert_se(safe_atoi(t
, &a
) >= 0);
1494 assert_se(oom_score_adjust_is_valid(a
));
1501 int pidfd_get_pid(int fd
, pid_t
*ret
) {
1502 char path
[STRLEN("/proc/self/fdinfo/") + DECIMAL_STR_MAX(int)];
1503 _cleanup_free_
char *fdinfo
= NULL
;
1510 xsprintf(path
, "/proc/self/fdinfo/%i", fd
);
1512 r
= read_full_virtual_file(path
, &fdinfo
, NULL
);
1513 if (r
== -ENOENT
) /* if fdinfo doesn't exist we assume the process does not exist */
1518 p
= startswith(fdinfo
, "Pid:");
1520 p
= strstr(fdinfo
, "\nPid:");
1522 return -ENOTTY
; /* not a pidfd? */
1527 p
+= strspn(p
, WHITESPACE
);
1528 p
[strcspn(p
, WHITESPACE
)] = 0;
1530 return parse_pid(p
, ret
);
1533 static int rlimit_to_nice(rlim_t limit
) {
1535 return PRIO_MAX
-1; /* i.e. 19 */
1537 if (limit
>= -PRIO_MIN
+ PRIO_MAX
)
1538 return PRIO_MIN
; /* i.e. -20 */
1540 return PRIO_MAX
- (int) limit
;
1543 int setpriority_closest(int priority
) {
1544 int current
, limit
, saved_errno
;
1545 struct rlimit highest
;
1547 /* Try to set requested nice level */
1548 if (setpriority(PRIO_PROCESS
, 0, priority
) >= 0)
1551 /* Permission failed */
1552 saved_errno
= -errno
;
1553 if (!ERRNO_IS_PRIVILEGE(saved_errno
))
1557 current
= getpriority(PRIO_PROCESS
, 0);
1561 if (priority
== current
)
1564 /* Hmm, we'd expect that raising the nice level from our status quo would always work. If it doesn't,
1565 * then the whole setpriority() system call is blocked to us, hence let's propagate the error
1567 if (priority
> current
)
1570 if (getrlimit(RLIMIT_NICE
, &highest
) < 0)
1573 limit
= rlimit_to_nice(highest
.rlim_cur
);
1575 /* We are already less nice than limit allows us */
1576 if (current
< limit
) {
1577 log_debug("Cannot raise nice level, permissions and the resource limit do not allow it.");
1581 /* Push to the allowed limit */
1582 if (setpriority(PRIO_PROCESS
, 0, limit
) < 0)
1585 log_debug("Cannot set requested nice level (%i), used next best (%i).", priority
, limit
);
1589 bool invoked_as(char *argv
[], const char *token
) {
1590 if (!argv
|| isempty(argv
[0]))
1596 return strstr(last_path_component(argv
[0]), token
);
1599 _noreturn_
void freeze(void) {
1602 /* Make sure nobody waits for us (i.e. on one of our sockets) anymore. Note that we use
1603 * close_all_fds_without_malloc() instead of plain close_all_fds() here, since we want this function
1604 * to be compatible with being called from signal handlers. */
1605 (void) close_all_fds_without_malloc(NULL
, 0);
1607 /* Let's not freeze right away, but keep reaping zombies. */
1611 if (waitid(P_ALL
, 0, &si
, WEXITED
) < 0 && errno
!= EINTR
)
1615 /* waitid() failed with an unexpected error, things are really borked. Freeze now! */
1621 static const char *const sigchld_code_table
[] = {
1622 [CLD_EXITED
] = "exited",
1623 [CLD_KILLED
] = "killed",
1624 [CLD_DUMPED
] = "dumped",
1625 [CLD_TRAPPED
] = "trapped",
1626 [CLD_STOPPED
] = "stopped",
1627 [CLD_CONTINUED
] = "continued",
1630 DEFINE_STRING_TABLE_LOOKUP(sigchld_code
, int);
1632 static const char* const sched_policy_table
[] = {
1633 [SCHED_OTHER
] = "other",
1634 [SCHED_BATCH
] = "batch",
1635 [SCHED_IDLE
] = "idle",
1636 [SCHED_FIFO
] = "fifo",
1640 DEFINE_STRING_TABLE_LOOKUP_WITH_FALLBACK(sched_policy
, int, INT_MAX
);