]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/process-util.c
sd-boot+bootctl: invert order of entries w/o sort-key
[thirdparty/systemd.git] / src / basic / process-util.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <ctype.h>
4 #include <errno.h>
5 #include <limits.h>
6 #include <linux/oom.h>
7 #include <pthread.h>
8 #include <stdbool.h>
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <sys/mman.h>
12 #include <sys/mount.h>
13 #include <sys/personality.h>
14 #include <sys/prctl.h>
15 #include <sys/types.h>
16 #include <sys/wait.h>
17 #include <syslog.h>
18 #include <unistd.h>
19 #if HAVE_VALGRIND_VALGRIND_H
20 #include <valgrind/valgrind.h>
21 #endif
22
23 #include "alloc-util.h"
24 #include "architecture.h"
25 #include "env-util.h"
26 #include "errno-util.h"
27 #include "escape.h"
28 #include "fd-util.h"
29 #include "fileio.h"
30 #include "fs-util.h"
31 #include "locale-util.h"
32 #include "log.h"
33 #include "macro.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"
49 #include "utf8.h"
50
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.
53 */
54 #define COMM_MAX_LEN 128
55
56 static int get_process_state(pid_t pid) {
57 _cleanup_free_ char *line = NULL;
58 const char *p;
59 char state;
60 int r;
61
62 assert(pid >= 0);
63
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';
67
68 p = procfs_file_alloca(pid, "stat");
69
70 r = read_one_line_file(p, &line);
71 if (r == -ENOENT)
72 return -ESRCH;
73 if (r < 0)
74 return r;
75
76 p = strrchr(line, ')');
77 if (!p)
78 return -EIO;
79
80 p++;
81
82 if (sscanf(p, " %c", &state) != 1)
83 return -EIO;
84
85 return (unsigned char) state;
86 }
87
88 int get_process_comm(pid_t pid, char **ret) {
89 _cleanup_free_ char *escaped = NULL, *comm = NULL;
90 int r;
91
92 assert(ret);
93 assert(pid >= 0);
94
95 if (pid == 0 || pid == getpid_cached()) {
96 comm = new0(char, TASK_COMM_LEN + 1); /* Must fit in 16 byte according to prctl(2) */
97 if (!comm)
98 return -ENOMEM;
99
100 if (prctl(PR_GET_NAME, comm) < 0)
101 return -errno;
102 } else {
103 const char *p;
104
105 p = procfs_file_alloca(pid, "comm");
106
107 /* Note that process names of kernel threads can be much longer than TASK_COMM_LEN */
108 r = read_one_line_file(p, &comm);
109 if (r == -ENOENT)
110 return -ESRCH;
111 if (r < 0)
112 return r;
113 }
114
115 escaped = new(char, COMM_MAX_LEN);
116 if (!escaped)
117 return -ENOMEM;
118
119 /* Escape unprintable characters, just in case, but don't grow the string beyond the underlying size */
120 cellescape(escaped, COMM_MAX_LEN, comm);
121
122 *ret = TAKE_PTR(escaped);
123 return 0;
124 }
125
126 static int get_process_cmdline_nulstr(
127 pid_t pid,
128 size_t max_size,
129 ProcessCmdlineFlags flags,
130 char **ret,
131 size_t *ret_size) {
132
133 const char *p;
134 char *t;
135 size_t k;
136 int r;
137
138 /* Retrieves a process' command line as a "sized nulstr", i.e. possibly without the last NUL, but
139 * with a specified size.
140 *
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
144 * data.
145 *
146 * Returns an error, 0 if output was read but is truncated, 1 otherwise.
147 */
148
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. */
152 if (r == -ENOENT)
153 return -ESRCH;
154 if (r < 0)
155 return r;
156
157 if (k == 0) {
158 t = mfree(t);
159
160 if (!(flags & PROCESS_CMDLINE_COMM_FALLBACK))
161 return -ENOENT;
162
163 /* Kernel threads have no argv[] */
164 _cleanup_free_ char *comm = NULL;
165
166 r = get_process_comm(pid, &comm);
167 if (r < 0)
168 return r;
169
170 t = strjoin("[", comm, "]");
171 if (!t)
172 return -ENOMEM;
173
174 k = strlen(t);
175 r = k <= max_size;
176 if (r == 0) /* truncation */
177 t[max_size] = '\0';
178 }
179
180 *ret = t;
181 *ret_size = k;
182 return r;
183 }
184
185 int get_process_cmdline(pid_t pid, size_t max_columns, ProcessCmdlineFlags flags, char **ret) {
186 _cleanup_free_ char *t = NULL;
187 size_t k;
188 char *ans;
189
190 assert(pid >= 0);
191 assert(ret);
192
193 /* Retrieve and format a commandline. See above for discussion of retrieval options.
194 *
195 * There are two main formatting modes:
196 *
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
199 * output is assumed.
200 *
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.
203 *
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. */
206
207 int full = get_process_cmdline_nulstr(pid, max_columns, flags, &t, &k);
208 if (full < 0)
209 return full;
210
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;
214
215 assert(!(flags & PROCESS_CMDLINE_USE_LOCALE));
216
217 _cleanup_strv_free_ char **args = NULL;
218 char **p;
219
220 args = strv_parse_nulstr(t, k);
221 if (!args)
222 return -ENOMEM;
223
224 /* Drop trailing empty strings. See issue #21186. */
225 STRV_FOREACH_BACKWARDS(p, args) {
226 if (!isempty(*p))
227 break;
228
229 *p = mfree(*p);
230 }
231
232 ans = quote_command_line(args, shflags);
233 if (!ans)
234 return -ENOMEM;
235 } else {
236 /* Arguments are separated by NULs. Let's replace those with spaces. */
237 for (size_t i = 0; i < k - 1; i++)
238 if (t[i] == '\0')
239 t[i] = ' ';
240
241 delete_trailing_chars(t, WHITESPACE);
242
243 bool eight_bit = (flags & PROCESS_CMDLINE_USE_LOCALE) && !is_locale_utf8();
244
245 ans = escape_non_printable_full(t, max_columns,
246 eight_bit * XESCAPE_8_BIT | !full * XESCAPE_FORCE_ELLIPSIS);
247 if (!ans)
248 return -ENOMEM;
249
250 ans = str_realloc(ans);
251 }
252
253 *ret = ans;
254 return 0;
255 }
256
257 static int update_argv(const char name[], size_t l) {
258 static int can_do = -1;
259
260 if (can_do == 0)
261 return 0;
262 can_do = false; /* We'll set it to true only if the whole process works */
263
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
268 * mmap() is not. */
269 if (geteuid() != 0)
270 return log_debug_errno(SYNTHETIC_ERRNO(EPERM),
271 "Skipping PR_SET_MM, as we don't have privileges.");
272
273 static size_t mm_size = 0;
274 static char *mm = NULL;
275 int r;
276
277 if (mm_size < l+1) {
278 size_t nn_size;
279 char *nn;
280
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");
285
286 strncpy(nn, name, nn_size);
287
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");
292
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");
302
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);
306 return r;
307 }
308
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");
311 } else {
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,
314 * and continue. */
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");
317 }
318
319 if (mm)
320 (void) munmap(mm, mm_size);
321
322 mm = nn;
323 mm_size = nn_size;
324 } else {
325 strncpy(mm, name, mm_size);
326
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");
330 }
331
332 can_do = true;
333 return 0;
334 }
335
336 int rename_process(const char name[]) {
337 bool truncated = false;
338
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
343 * truncated.
344 *
345 * Returns 0 if a name was set but truncated, > 0 if it was set but not truncated. */
346
347 if (isempty(name))
348 return -EINVAL; /* let's not confuse users unnecessarily with an empty name */
349
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 */
354
355 size_t l = strlen(name);
356
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 */
362 truncated = true;
363
364 /* Second step, change glibc's ID of the process name. */
365 if (program_invocation_name) {
366 size_t k;
367
368 k = strlen(program_invocation_name);
369 strncpy(program_invocation_name, name, k);
370 if (l > k)
371 truncated = true;
372 }
373
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);
378
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) {
382 if (saved_argv[0]) {
383 size_t k;
384
385 k = strlen(saved_argv[0]);
386 strncpy(saved_argv[0], name, k);
387 if (l > k)
388 truncated = true;
389 }
390
391 for (int i = 1; i < saved_argc; i++) {
392 if (!saved_argv[i])
393 break;
394
395 memzero(saved_argv[i], strlen(saved_argv[i]));
396 }
397 }
398
399 return !truncated;
400 }
401
402 int is_kernel_thread(pid_t pid) {
403 _cleanup_free_ char *line = NULL;
404 unsigned long long flags;
405 size_t l, i;
406 const char *p;
407 char *q;
408 int r;
409
410 if (IN_SET(pid, 0, 1) || pid == getpid_cached()) /* pid 1, and we ourselves certainly aren't a kernel thread */
411 return 0;
412 if (!pid_is_valid(pid))
413 return -EINVAL;
414
415 p = procfs_file_alloca(pid, "stat");
416 r = read_one_line_file(p, &line);
417 if (r == -ENOENT)
418 return -ESRCH;
419 if (r < 0)
420 return r;
421
422 /* Skip past the comm field */
423 q = strrchr(line, ')');
424 if (!q)
425 return -EINVAL;
426 q++;
427
428 /* Skip 6 fields to reach the flags field */
429 for (i = 0; i < 6; i++) {
430 l = strspn(q, WHITESPACE);
431 if (l < 1)
432 return -EINVAL;
433 q += l;
434
435 l = strcspn(q, WHITESPACE);
436 if (l < 1)
437 return -EINVAL;
438 q += l;
439 }
440
441 /* Skip preceding whitespace */
442 l = strspn(q, WHITESPACE);
443 if (l < 1)
444 return -EINVAL;
445 q += l;
446
447 /* Truncate the rest */
448 l = strcspn(q, WHITESPACE);
449 if (l < 1)
450 return -EINVAL;
451 q[l] = 0;
452
453 r = safe_atollu(q, &flags);
454 if (r < 0)
455 return r;
456
457 return !!(flags & PF_KTHREAD);
458 }
459
460 int get_process_capeff(pid_t pid, char **ret) {
461 const char *p;
462 int r;
463
464 assert(pid >= 0);
465 assert(ret);
466
467 p = procfs_file_alloca(pid, "status");
468
469 r = get_proc_field(p, "CapEff", WHITESPACE, ret);
470 if (r == -ENOENT)
471 return -ESRCH;
472
473 return r;
474 }
475
476 static int get_process_link_contents(const char *proc_file, char **ret) {
477 int r;
478
479 assert(proc_file);
480 assert(ret);
481
482 r = readlink_malloc(proc_file, ret);
483 if (r == -ENOENT)
484 return -ESRCH;
485 if (r < 0)
486 return r;
487
488 return 0;
489 }
490
491 int get_process_exe(pid_t pid, char **ret) {
492 const char *p;
493 char *d;
494 int r;
495
496 assert(pid >= 0);
497 assert(ret);
498
499 p = procfs_file_alloca(pid, "exe");
500 r = get_process_link_contents(p, ret);
501 if (r < 0)
502 return r;
503
504 d = endswith(*ret, " (deleted)");
505 if (d)
506 *d = '\0';
507
508 return 0;
509 }
510
511 static int get_process_id(pid_t pid, const char *field, uid_t *ret) {
512 _cleanup_fclose_ FILE *f = NULL;
513 const char *p;
514 int r;
515
516 assert(field);
517 assert(ret);
518
519 if (pid < 0)
520 return -EINVAL;
521
522 p = procfs_file_alloca(pid, "status");
523 r = fopen_unlocked(p, "re", &f);
524 if (r == -ENOENT)
525 return -ESRCH;
526 if (r < 0)
527 return r;
528
529 for (;;) {
530 _cleanup_free_ char *line = NULL;
531 char *l;
532
533 r = read_line(f, LONG_LINE_MAX, &line);
534 if (r < 0)
535 return r;
536 if (r == 0)
537 break;
538
539 l = strstrip(line);
540
541 if (startswith(l, field)) {
542 l += strlen(field);
543 l += strspn(l, WHITESPACE);
544
545 l[strcspn(l, WHITESPACE)] = 0;
546
547 return parse_uid(l, ret);
548 }
549 }
550
551 return -EIO;
552 }
553
554 int get_process_uid(pid_t pid, uid_t *ret) {
555
556 if (pid == 0 || pid == getpid_cached()) {
557 *ret = getuid();
558 return 0;
559 }
560
561 return get_process_id(pid, "Uid:", ret);
562 }
563
564 int get_process_gid(pid_t pid, gid_t *ret) {
565
566 if (pid == 0 || pid == getpid_cached()) {
567 *ret = getgid();
568 return 0;
569 }
570
571 assert_cc(sizeof(uid_t) == sizeof(gid_t));
572 return get_process_id(pid, "Gid:", ret);
573 }
574
575 int get_process_cwd(pid_t pid, char **ret) {
576 const char *p;
577
578 assert(pid >= 0);
579 assert(ret);
580
581 if (pid == 0 || pid == getpid_cached())
582 return safe_getcwd(ret);
583
584 p = procfs_file_alloca(pid, "cwd");
585
586 return get_process_link_contents(p, ret);
587 }
588
589 int get_process_root(pid_t pid, char **ret) {
590 const char *p;
591
592 assert(pid >= 0);
593 assert(ret);
594
595 p = procfs_file_alloca(pid, "root");
596
597 return get_process_link_contents(p, ret);
598 }
599
600 #define ENVIRONMENT_BLOCK_MAX (5U*1024U*1024U)
601
602 int get_process_environ(pid_t pid, char **ret) {
603 _cleanup_fclose_ FILE *f = NULL;
604 _cleanup_free_ char *outcome = NULL;
605 size_t sz = 0;
606 const char *p;
607 int r;
608
609 assert(pid >= 0);
610 assert(ret);
611
612 p = procfs_file_alloca(pid, "environ");
613
614 r = fopen_unlocked(p, "re", &f);
615 if (r == -ENOENT)
616 return -ESRCH;
617 if (r < 0)
618 return r;
619
620 for (;;) {
621 char c;
622
623 if (sz >= ENVIRONMENT_BLOCK_MAX)
624 return -ENOBUFS;
625
626 if (!GREEDY_REALLOC(outcome, sz + 5))
627 return -ENOMEM;
628
629 r = safe_fgetc(f, &c);
630 if (r < 0)
631 return r;
632 if (r == 0)
633 break;
634
635 if (c == '\0')
636 outcome[sz++] = '\n';
637 else
638 sz += cescape_char(c, outcome + sz);
639 }
640
641 outcome[sz] = '\0';
642 *ret = TAKE_PTR(outcome);
643
644 return 0;
645 }
646
647 int get_process_ppid(pid_t pid, pid_t *ret) {
648 _cleanup_free_ char *line = NULL;
649 unsigned long ppid;
650 const char *p;
651 int r;
652
653 assert(pid >= 0);
654
655 if (pid == 0 || pid == getpid_cached()) {
656 if (ret)
657 *ret = getppid();
658 return 0;
659 }
660
661 if (pid == 1) /* PID 1 has no parent, shortcut this case */
662 return -EADDRNOTAVAIL;
663
664 p = procfs_file_alloca(pid, "stat");
665 r = read_one_line_file(p, &line);
666 if (r == -ENOENT)
667 return -ESRCH;
668 if (r < 0)
669 return r;
670
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 */
673
674 p = strrchr(line, ')');
675 if (!p)
676 return -EIO;
677
678 p++;
679
680 if (sscanf(p, " "
681 "%*c " /* state */
682 "%lu ", /* ppid */
683 &ppid) != 1)
684 return -EIO;
685
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. */
689 if (ppid == 0)
690 return -EADDRNOTAVAIL;
691
692 if ((pid_t) ppid < 0 || (unsigned long) (pid_t) ppid != ppid)
693 return -ERANGE;
694
695 if (ret)
696 *ret = (pid_t) ppid;
697
698 return 0;
699 }
700
701 int get_process_umask(pid_t pid, mode_t *ret) {
702 _cleanup_free_ char *m = NULL;
703 const char *p;
704 int r;
705
706 assert(pid >= 0);
707 assert(ret);
708
709 p = procfs_file_alloca(pid, "status");
710
711 r = get_proc_field(p, "Umask", WHITESPACE, &m);
712 if (r == -ENOENT)
713 return -ESRCH;
714
715 return parse_mode(m, ret);
716 }
717
718 int wait_for_terminate(pid_t pid, siginfo_t *status) {
719 siginfo_t dummy;
720
721 assert(pid >= 1);
722
723 if (!status)
724 status = &dummy;
725
726 for (;;) {
727 zero(*status);
728
729 if (waitid(P_PID, pid, status, WEXITED) < 0) {
730
731 if (errno == EINTR)
732 continue;
733
734 return negative_errno();
735 }
736
737 return 0;
738 }
739 }
740
741 /*
742 * Return values:
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
747 * returned.
748 *
749 * That is, success is indicated by a return value of zero, and an
750 * error is indicated by a non-zero value.
751 *
752 * A warning is emitted if the process terminates abnormally,
753 * and also if it returns non-zero unless check_exit_code is true.
754 */
755 int wait_for_terminate_and_check(const char *name, pid_t pid, WaitFlags flags) {
756 _cleanup_free_ char *buffer = NULL;
757 siginfo_t status;
758 int r, prio;
759
760 assert(pid > 1);
761
762 if (!name) {
763 r = get_process_comm(pid, &buffer);
764 if (r < 0)
765 log_debug_errno(r, "Failed to acquire process name of " PID_FMT ", ignoring: %m", pid);
766 else
767 name = buffer;
768 }
769
770 prio = flags & WAIT_LOG_ABNORMAL ? LOG_ERR : LOG_DEBUG;
771
772 r = wait_for_terminate(pid, &status);
773 if (r < 0)
774 return log_full_errno(prio, r, "Failed to wait for %s: %m", strna(name));
775
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);
780 else
781 log_debug("%s succeeded.", name);
782
783 return status.si_status;
784
785 } else if (IN_SET(status.si_code, CLD_KILLED, CLD_DUMPED)) {
786
787 log_full(prio, "%s terminated by signal %s.", strna(name), signal_to_string(status.si_status));
788 return -EPROTO;
789 }
790
791 log_full(prio, "%s failed due to unknown reason.", strna(name));
792 return -EPROTO;
793 }
794
795 /*
796 * Return values:
797 *
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.
800 *
801 * >=0 : The process terminated normally with no failures.
802 *
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.
805 *
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.
808 */
809 int wait_for_terminate_with_timeout(pid_t pid, usec_t timeout) {
810 sigset_t mask;
811 int r;
812 usec_t until;
813
814 assert_se(sigemptyset(&mask) == 0);
815 assert_se(sigaddset(&mask, SIGCHLD) == 0);
816
817 /* Drop into a sigtimewait-based timeout. Waiting for the
818 * pid to exit. */
819 until = usec_add(now(CLOCK_MONOTONIC), timeout);
820 for (;;) {
821 usec_t n;
822 siginfo_t status = {};
823 struct timespec ts;
824
825 n = now(CLOCK_MONOTONIC);
826 if (n >= until)
827 break;
828
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;
836 else
837 return -EPROTO;
838 }
839 }
840 /* Not the child, check for errors and proceed appropriately */
841 if (r < 0) {
842 switch (r) {
843 case -EAGAIN:
844 /* Timed out, child is likely hung. */
845 return -ETIMEDOUT;
846 case -EINTR:
847 /* Received a different signal and should retry */
848 continue;
849 default:
850 /* Return any unexpected errors */
851 return r;
852 }
853 }
854 }
855
856 return -EPROTO;
857 }
858
859 void sigkill_wait(pid_t pid) {
860 assert(pid > 1);
861
862 (void) kill(pid, SIGKILL);
863 (void) wait_for_terminate(pid, NULL);
864 }
865
866 void sigkill_waitp(pid_t *pid) {
867 PROTECT_ERRNO;
868
869 if (!pid)
870 return;
871 if (*pid <= 1)
872 return;
873
874 sigkill_wait(*pid);
875 }
876
877 void sigterm_wait(pid_t pid) {
878 assert(pid > 1);
879
880 (void) kill_and_sigcont(pid, SIGTERM);
881 (void) wait_for_terminate(pid, NULL);
882 }
883
884 int kill_and_sigcont(pid_t pid, int sig) {
885 int r;
886
887 r = RET_NERRNO(kill(pid, sig));
888
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);
893
894 return r;
895 }
896
897 int getenv_for_pid(pid_t pid, const char *field, char **ret) {
898 _cleanup_fclose_ FILE *f = NULL;
899 char *value = NULL;
900 const char *path;
901 size_t l, sum = 0;
902 int r;
903
904 assert(pid >= 0);
905 assert(field);
906 assert(ret);
907
908 if (pid == 0 || pid == getpid_cached()) {
909 const char *e;
910
911 e = getenv(field);
912 if (!e) {
913 *ret = NULL;
914 return 0;
915 }
916
917 value = strdup(e);
918 if (!value)
919 return -ENOMEM;
920
921 *ret = value;
922 return 1;
923 }
924
925 if (!pid_is_valid(pid))
926 return -EINVAL;
927
928 path = procfs_file_alloca(pid, "environ");
929
930 r = fopen_unlocked(path, "re", &f);
931 if (r == -ENOENT)
932 return -ESRCH;
933 if (r < 0)
934 return r;
935
936 l = strlen(field);
937 for (;;) {
938 _cleanup_free_ char *line = NULL;
939
940 if (sum > ENVIRONMENT_BLOCK_MAX) /* Give up searching eventually */
941 return -ENOBUFS;
942
943 r = read_nul_string(f, LONG_LINE_MAX, &line);
944 if (r < 0)
945 return r;
946 if (r == 0) /* EOF */
947 break;
948
949 sum += r;
950
951 if (strneq(line, field, l) && line[l] == '=') {
952 value = strdup(line + l + 1);
953 if (!value)
954 return -ENOMEM;
955
956 *ret = value;
957 return 1;
958 }
959 }
960
961 *ret = NULL;
962 return 0;
963 }
964
965 int pid_is_my_child(pid_t pid) {
966 pid_t ppid;
967 int r;
968
969 if (pid <= 1)
970 return false;
971
972 r = get_process_ppid(pid, &ppid);
973 if (r < 0)
974 return r;
975
976 return ppid == getpid_cached();
977 }
978
979 bool pid_is_unwaited(pid_t pid) {
980 /* Checks whether a PID is still valid at all, including a zombie */
981
982 if (pid < 0)
983 return false;
984
985 if (pid <= 1) /* If we or PID 1 would be dead and have been waited for, this code would not be running */
986 return true;
987
988 if (pid == getpid_cached())
989 return true;
990
991 if (kill(pid, 0) >= 0)
992 return true;
993
994 return errno != ESRCH;
995 }
996
997 bool pid_is_alive(pid_t pid) {
998 int r;
999
1000 /* Checks whether a PID is still valid and not a zombie */
1001
1002 if (pid < 0)
1003 return false;
1004
1005 if (pid <= 1) /* If we or PID 1 would be a zombie, this code would not be running */
1006 return true;
1007
1008 if (pid == getpid_cached())
1009 return true;
1010
1011 r = get_process_state(pid);
1012 if (IN_SET(r, -ESRCH, 'Z'))
1013 return false;
1014
1015 return true;
1016 }
1017
1018 int pid_from_same_root_fs(pid_t pid) {
1019 const char *root;
1020
1021 if (pid < 0)
1022 return false;
1023
1024 if (pid == 0 || pid == getpid_cached())
1025 return true;
1026
1027 root = procfs_file_alloca(pid, "root");
1028
1029 return files_same(root, "/proc/1/root", 0);
1030 }
1031
1032 bool is_main_thread(void) {
1033 static thread_local int cached = 0;
1034
1035 if (_unlikely_(cached == 0))
1036 cached = getpid_cached() == gettid() ? 1 : -1;
1037
1038 return cached > 0;
1039 }
1040
1041 bool oom_score_adjust_is_valid(int oa) {
1042 return oa >= OOM_SCORE_ADJ_MIN && oa <= OOM_SCORE_ADJ_MAX;
1043 }
1044
1045 unsigned long personality_from_string(const char *p) {
1046 int architecture;
1047
1048 if (!p)
1049 return PERSONALITY_INVALID;
1050
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. */
1054
1055 architecture = architecture_from_string(p);
1056 if (architecture < 0)
1057 return PERSONALITY_INVALID;
1058
1059 if (architecture == native_architecture())
1060 return PER_LINUX;
1061 #ifdef ARCHITECTURE_SECONDARY
1062 if (architecture == ARCHITECTURE_SECONDARY)
1063 return PER_LINUX32;
1064 #endif
1065
1066 return PERSONALITY_INVALID;
1067 }
1068
1069 const char* personality_to_string(unsigned long p) {
1070 int architecture = _ARCHITECTURE_INVALID;
1071
1072 if (p == PER_LINUX)
1073 architecture = native_architecture();
1074 #ifdef ARCHITECTURE_SECONDARY
1075 else if (p == PER_LINUX32)
1076 architecture = ARCHITECTURE_SECONDARY;
1077 #endif
1078
1079 if (architecture < 0)
1080 return NULL;
1081
1082 return architecture_to_string(architecture);
1083 }
1084
1085 int safe_personality(unsigned long p) {
1086 int ret;
1087
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.
1092 *
1093 * See https://github.com/systemd/systemd/issues/6737 */
1094
1095 errno = 0;
1096 ret = personality(p);
1097 if (ret < 0) {
1098 if (errno != 0)
1099 return -errno;
1100
1101 errno = -ret;
1102 }
1103
1104 return ret;
1105 }
1106
1107 int opinionated_personality(unsigned long *ret) {
1108 int current;
1109
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. */
1113
1114 current = safe_personality(PERSONALITY_INVALID);
1115 if (current < 0)
1116 return current;
1117
1118 if (((unsigned long) current & 0xffff) == PER_LINUX32)
1119 *ret = PER_LINUX32;
1120 else
1121 *ret = PER_LINUX;
1122
1123 return 0;
1124 }
1125
1126 void valgrind_summary_hack(void) {
1127 #if HAVE_VALGRIND_VALGRIND_H
1128 if (getpid_cached() == 1 && RUNNING_ON_VALGRIND) {
1129 pid_t pid;
1130 pid = raw_clone(SIGCHLD);
1131 if (pid < 0)
1132 log_emergency_errno(errno, "Failed to fork off valgrind helper: %m");
1133 else if (pid == 0)
1134 exit(EXIT_SUCCESS);
1135 else {
1136 log_info("Spawned valgrind helper as PID "PID_FMT".", pid);
1137 (void) wait_for_terminate(pid, NULL);
1138 }
1139 }
1140 #endif
1141 }
1142
1143 int pid_compare_func(const pid_t *a, const pid_t *b) {
1144 /* Suitable for usage in qsort() */
1145 return CMP(*a, *b);
1146 }
1147
1148 /* The cached PID, possible values:
1149 *
1150 * == UNSET [0] → cache not initialized yet
1151 * == BUSY [-1] → some thread is initializing it at the moment
1152 * any other → the cached PID
1153 */
1154
1155 #define CACHED_PID_UNSET ((pid_t) 0)
1156 #define CACHED_PID_BUSY ((pid_t) -1)
1157
1158 static pid_t cached_pid = CACHED_PID_UNSET;
1159
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;
1163 }
1164
1165 pid_t getpid_cached(void) {
1166 static bool installed = false;
1167 pid_t current_value;
1168
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.
1173 *
1174 * https://bugzilla.redhat.com/show_bug.cgi?id=1443976
1175 * https://sourceware.org/git/gitweb.cgi?p=glibc.git;h=c579f48edba88380635ab98cb612030e3ed8691e
1176 */
1177
1178 current_value = __sync_val_compare_and_swap(&cached_pid, CACHED_PID_UNSET, CACHED_PID_BUSY);
1179
1180 switch (current_value) {
1181
1182 case CACHED_PID_UNSET: { /* Not initialized yet, then do so now */
1183 pid_t new_pid;
1184
1185 new_pid = raw_getpid();
1186
1187 if (!installed) {
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. */
1191
1192 if (pthread_atfork(NULL, NULL, reset_cached_pid) != 0) {
1193 /* OOM? Let's try again later */
1194 cached_pid = CACHED_PID_UNSET;
1195 return new_pid;
1196 }
1197
1198 installed = true;
1199 }
1200
1201 cached_pid = new_pid;
1202 return new_pid;
1203 }
1204
1205 case CACHED_PID_BUSY: /* Somebody else is currently initializing */
1206 return raw_getpid();
1207
1208 default: /* Properly initialized */
1209 return current_value;
1210 }
1211 }
1212
1213 int must_be_root(void) {
1214
1215 if (geteuid() == 0)
1216 return 0;
1217
1218 return log_error_errno(SYNTHETIC_ERRNO(EPERM), "Need to be root.");
1219 }
1220
1221 static void restore_sigsetp(sigset_t **ssp) {
1222 if (*ssp)
1223 (void) sigprocmask(SIG_SETMASK, *ssp, NULL);
1224 }
1225
1226 int safe_fork_full(
1227 const char *name,
1228 const int except_fds[],
1229 size_t n_except_fds,
1230 ForkFlags flags,
1231 pid_t *ret_pid) {
1232
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;
1237 int prio, r;
1238
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. */
1241
1242 prio = flags & FORK_LOG ? LOG_ERR : LOG_DEBUG;
1243
1244 original_pid = getpid_cached();
1245
1246 if (flags & FORK_FLUSH_STDIO) {
1247 fflush(stdout);
1248 fflush(stderr); /* This one shouldn't be necessary, stderr should be unbuffered anyway, but let's better be safe than sorry */
1249 }
1250
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. */
1254
1255 assert_se(sigfillset(&ss) >= 0);
1256 block_signals = block_all = true;
1257
1258 } else if (flags & FORK_WAIT) {
1259 /* Let's block SIGCHLD at least, so that we can safely watch for the child process */
1260
1261 assert_se(sigemptyset(&ss) >= 0);
1262 assert_se(sigaddset(&ss, SIGCHLD) >= 0);
1263 block_signals = true;
1264 }
1265
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;
1270 }
1271
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));
1276 else
1277 pid = fork();
1278 if (pid < 0)
1279 return log_full_errno(prio, errno, "Failed to fork: %m");
1280 if (pid > 0) {
1281 /* We are in the parent process */
1282
1283 log_debug("Successfully forked off '%s' as PID " PID_FMT ".", strna(name), pid);
1284
1285 if (flags & FORK_WAIT) {
1286 if (block_all) {
1287 /* undo everything except SIGCHLD */
1288 ss = saved_ss;
1289 assert_se(sigaddset(&ss, SIGCHLD) >= 0);
1290 (void) sigprocmask(SIG_SETMASK, &ss, NULL);
1291 }
1292
1293 r = wait_for_terminate_and_check(name, pid, (flags & FORK_LOG ? WAIT_LOG : 0));
1294 if (r < 0)
1295 return r;
1296 if (r != EXIT_SUCCESS) /* exit status > 0 should be treated as failure, too */
1297 return -EPROTO;
1298 }
1299
1300 if (ret_pid)
1301 *ret_pid = pid;
1302
1303 return 1;
1304 }
1305
1306 /* We are in the child process */
1307
1308 /* Restore signal mask manually */
1309 saved_ssp = NULL;
1310
1311 if (flags & FORK_REOPEN_LOG) {
1312 /* Close the logs if requested, before we log anything. And make sure we reopen it if needed. */
1313 log_close();
1314 log_set_open_when_needed(true);
1315 }
1316
1317 if (name) {
1318 r = rename_process(name);
1319 if (r < 0)
1320 log_full_errno(flags & FORK_LOG ? LOG_WARNING : LOG_DEBUG,
1321 r, "Failed to rename process, ignoring: %m");
1322 }
1323
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);
1328 }
1329
1330 if (flags & FORK_RESET_SIGNALS) {
1331 r = reset_all_signal_handlers();
1332 if (r < 0) {
1333 log_full_errno(prio, r, "Failed to reset signal handlers: %m");
1334 _exit(EXIT_FAILURE);
1335 }
1336
1337 /* This implicitly undoes the signal mask stuff we did before the fork()ing above */
1338 r = reset_signal_mask();
1339 if (r < 0) {
1340 log_full_errno(prio, r, "Failed to reset signal mask: %m");
1341 _exit(EXIT_FAILURE);
1342 }
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);
1347 }
1348 }
1349
1350 if (flags & FORK_DEATHSIG) {
1351 pid_t ppid;
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 */
1354
1355 ppid = getppid();
1356 if (ppid == 0)
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);
1362 }
1363 }
1364
1365 if (FLAGS_SET(flags, FORK_NEW_MOUNTNS | FORK_MOUNTNS_SLAVE)) {
1366
1367 /* Optionally, make sure we never propagate mounts to the host. */
1368
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);
1372 }
1373 }
1374
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 */
1377 log_close();
1378
1379 r = close_all_fds(except_fds, n_except_fds);
1380 if (r < 0) {
1381 log_full_errno(prio, r, "Failed to close all file descriptors: %m");
1382 _exit(EXIT_FAILURE);
1383 }
1384 }
1385
1386 /* When we were asked to reopen the logs, do so again now */
1387 if (flags & FORK_REOPEN_LOG) {
1388 log_open();
1389 log_set_open_when_needed(false);
1390 }
1391
1392 if (flags & FORK_NULL_STDIO) {
1393 r = make_null_stdio();
1394 if (r < 0) {
1395 log_full_errno(prio, r, "Failed to connect stdin/stdout to /dev/null: %m");
1396 _exit(EXIT_FAILURE);
1397 }
1398
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);
1403 }
1404 }
1405
1406 if (flags & FORK_RLIMIT_NOFILE_SAFE) {
1407 r = rlimit_nofile_safe();
1408 if (r < 0) {
1409 log_full_errno(prio, r, "Failed to lower RLIMIT_NOFILE's soft limit to 1K: %m");
1410 _exit(EXIT_FAILURE);
1411 }
1412 }
1413
1414 if (ret_pid)
1415 *ret_pid = getpid_cached();
1416
1417 return 0;
1418 }
1419
1420 int namespace_fork(
1421 const char *outer_name,
1422 const char *inner_name,
1423 const int except_fds[],
1424 size_t n_except_fds,
1425 ForkFlags flags,
1426 int pidns_fd,
1427 int mntns_fd,
1428 int netns_fd,
1429 int userns_fd,
1430 int root_fd,
1431 pid_t *ret_pid) {
1432
1433 int r;
1434
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. */
1438
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);
1440 if (r < 0)
1441 return r;
1442 if (r == 0) {
1443 pid_t pid;
1444
1445 /* Child */
1446
1447 r = namespace_enter(pidns_fd, mntns_fd, netns_fd, userns_fd, root_fd);
1448 if (r < 0) {
1449 log_full_errno(FLAGS_SET(flags, FORK_LOG) ? LOG_ERR : LOG_DEBUG, r, "Failed to join namespace: %m");
1450 _exit(EXIT_FAILURE);
1451 }
1452
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);
1455 if (r < 0)
1456 _exit(EXIT_FAILURE);
1457 if (r == 0) {
1458 /* Child */
1459 if (ret_pid)
1460 *ret_pid = pid;
1461 return 0;
1462 }
1463
1464 r = wait_for_terminate_and_check(inner_name, pid, FLAGS_SET(flags, FORK_LOG) ? WAIT_LOG : 0);
1465 if (r < 0)
1466 _exit(EXIT_FAILURE);
1467
1468 _exit(r);
1469 }
1470
1471 return 1;
1472 }
1473
1474 int set_oom_score_adjust(int value) {
1475 char t[DECIMAL_STR_MAX(int)];
1476
1477 xsprintf(t, "%i", value);
1478
1479 return write_string_file("/proc/self/oom_score_adj", t,
1480 WRITE_STRING_FILE_VERIFY_ON_FAILURE|WRITE_STRING_FILE_DISABLE_BUFFER);
1481 }
1482
1483 int get_oom_score_adjust(int *ret) {
1484 _cleanup_free_ char *t = NULL;
1485 int r, a;
1486
1487 r = read_virtual_file("/proc/self/oom_score_adj", SIZE_MAX, &t, NULL);
1488 if (r < 0)
1489 return r;
1490
1491 delete_trailing_chars(t, WHITESPACE);
1492
1493 assert_se(safe_atoi(t, &a) >= 0);
1494 assert_se(oom_score_adjust_is_valid(a));
1495
1496 if (ret)
1497 *ret = a;
1498 return 0;
1499 }
1500
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;
1504 char *p;
1505 int r;
1506
1507 if (fd < 0)
1508 return -EBADF;
1509
1510 xsprintf(path, "/proc/self/fdinfo/%i", fd);
1511
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 */
1514 return -ESRCH;
1515 if (r < 0)
1516 return r;
1517
1518 p = startswith(fdinfo, "Pid:");
1519 if (!p) {
1520 p = strstr(fdinfo, "\nPid:");
1521 if (!p)
1522 return -ENOTTY; /* not a pidfd? */
1523
1524 p += 5;
1525 }
1526
1527 p += strspn(p, WHITESPACE);
1528 p[strcspn(p, WHITESPACE)] = 0;
1529
1530 return parse_pid(p, ret);
1531 }
1532
1533 static int rlimit_to_nice(rlim_t limit) {
1534 if (limit <= 1)
1535 return PRIO_MAX-1; /* i.e. 19 */
1536
1537 if (limit >= -PRIO_MIN + PRIO_MAX)
1538 return PRIO_MIN; /* i.e. -20 */
1539
1540 return PRIO_MAX - (int) limit;
1541 }
1542
1543 int setpriority_closest(int priority) {
1544 int current, limit, saved_errno;
1545 struct rlimit highest;
1546
1547 /* Try to set requested nice level */
1548 if (setpriority(PRIO_PROCESS, 0, priority) >= 0)
1549 return 1;
1550
1551 /* Permission failed */
1552 saved_errno = -errno;
1553 if (!ERRNO_IS_PRIVILEGE(saved_errno))
1554 return saved_errno;
1555
1556 errno = 0;
1557 current = getpriority(PRIO_PROCESS, 0);
1558 if (errno != 0)
1559 return -errno;
1560
1561 if (priority == current)
1562 return 1;
1563
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
1566 * right-away */
1567 if (priority > current)
1568 return saved_errno;
1569
1570 if (getrlimit(RLIMIT_NICE, &highest) < 0)
1571 return -errno;
1572
1573 limit = rlimit_to_nice(highest.rlim_cur);
1574
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.");
1578 return 0;
1579 }
1580
1581 /* Push to the allowed limit */
1582 if (setpriority(PRIO_PROCESS, 0, limit) < 0)
1583 return -errno;
1584
1585 log_debug("Cannot set requested nice level (%i), used next best (%i).", priority, limit);
1586 return 0;
1587 }
1588
1589 bool invoked_as(char *argv[], const char *token) {
1590 if (!argv || isempty(argv[0]))
1591 return false;
1592
1593 if (isempty(token))
1594 return false;
1595
1596 return strstr(last_path_component(argv[0]), token);
1597 }
1598
1599 _noreturn_ void freeze(void) {
1600 log_close();
1601
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);
1606
1607 /* Let's not freeze right away, but keep reaping zombies. */
1608 for (;;) {
1609 siginfo_t si = {};
1610
1611 if (waitid(P_ALL, 0, &si, WEXITED) < 0 && errno != EINTR)
1612 break;
1613 }
1614
1615 /* waitid() failed with an unexpected error, things are really borked. Freeze now! */
1616 for (;;)
1617 pause();
1618 }
1619
1620
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",
1628 };
1629
1630 DEFINE_STRING_TABLE_LOOKUP(sigchld_code, int);
1631
1632 static const char* const sched_policy_table[] = {
1633 [SCHED_OTHER] = "other",
1634 [SCHED_BATCH] = "batch",
1635 [SCHED_IDLE] = "idle",
1636 [SCHED_FIFO] = "fifo",
1637 [SCHED_RR] = "rr",
1638 };
1639
1640 DEFINE_STRING_TABLE_LOOKUP_WITH_FALLBACK(sched_policy, int, INT_MAX);