• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

systemd / systemd / 25238955322

01 May 2026 10:09AM UTC coverage: 71.943% (-0.2%) from 72.134%
25238955322

push

github

bluca
po: Translated using Weblate (Greek)

Currently translated at 100.0% (266 of 266 strings)

Co-authored-by: Jim Spentzos <jimspentzos2000@gmail.com>
Translate-URL: https://translate.fedoraproject.org/projects/systemd/main/el/
Translation: systemd/main

324741 of 451384 relevant lines covered (71.94%)

1387736.3 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

82.82
/src/basic/process-util.c
1
/* SPDX-License-Identifier: LGPL-2.1-or-later */
2

3
#include <linux/oom.h>
4
#include <pthread.h>
5
#include <spawn.h>
6
#include <stdio.h>
7
#include <sys/mman.h>
8
#include <sys/mount.h>
9
#include <sys/personality.h>
10
#include <sys/prctl.h>
11
#include <sys/wait.h>
12
#include <syslog.h>
13
#include <threads.h>
14
#include <unistd.h>
15
#if HAVE_VALGRIND_VALGRIND_H
16
#include <valgrind/valgrind.h>
17
#endif
18

19
#include "sd-messages.h"
20

21
#include "alloc-util.h"
22
#include "architecture.h"
23
#include "argv-util.h"
24
#include "capability-util.h"
25
#include "cgroup-util.h"
26
#include "dirent-util.h"
27
#include "dlfcn-util.h"
28
#include "errno-util.h"
29
#include "escape.h"
30
#include "fd-util.h"
31
#include "fileio.h"
32
#include "fs-util.h"
33
#include "io-util.h"
34
#include "iovec-util.h"
35
#include "locale-util.h"
36
#include "log.h"
37
#include "memory-util.h"
38
#include "mountpoint-util.h"
39
#include "namespace-util.h"
40
#include "nulstr-util.h"
41
#include "parse-util.h"
42
#include "path-util.h"
43
#include "pidfd-util.h"
44
#include "pidref.h"
45
#include "process-util.h"
46
#include "raw-clone.h"
47
#include "rlimit-util.h"
48
#include "signal-util.h"
49
#include "socket-util.h"
50
#include "stat-util.h"
51
#include "stdio-util.h"
52
#include "string-table.h"
53
#include "string-util.h"
54
#include "strv.h"
55
#include "time-util.h"
56
#include "user-util.h"
57

58
/* The kernel limits userspace processes to TASK_COMM_LEN (16 bytes), but allows higher values for its own
59
 * workers, e.g. "kworker/u9:3-kcryptd/253:0". Let's pick a fixed smallish limit that will work for the kernel.
60
 */
61
#define COMM_MAX_LEN 128
62

63
static int get_process_state(pid_t pid) {
16,627✔
64
        _cleanup_free_ char *line = NULL;
16,627✔
65
        const char *p;
16,627✔
66
        char state;
16,627✔
67
        int r;
16,627✔
68

69
        assert(pid >= 0);
16,627✔
70

71
        /* Shortcut: if we are enquired about our own state, we are obviously running */
72
        if (pid == 0 || pid == getpid_cached())
16,627✔
73
                return (unsigned char) 'R';
×
74

75
        p = procfs_file_alloca(pid, "stat");
16,627✔
76

77
        r = read_one_line_file(p, &line);
16,627✔
78
        if (r == -ENOENT)
16,627✔
79
                return -ESRCH;
80
        if (r < 0)
13,472✔
81
                return r;
82

83
        p = strrchr(line, ')');
13,470✔
84
        if (!p)
13,470✔
85
                return -EIO;
86

87
        p++;
13,470✔
88

89
        if (sscanf(p, " %c", &state) != 1)
13,470✔
90
                return -EIO;
91

92
        return (unsigned char) state;
13,470✔
93
}
94

95
int pid_get_comm(pid_t pid, char **ret) {
51,195✔
96
        _cleanup_free_ char *escaped = NULL, *comm = NULL;
51,195✔
97
        int r;
51,195✔
98

99
        assert(pid >= 0);
51,195✔
100
        assert(ret);
51,195✔
101

102
        if (pid == 0 || pid == getpid_cached()) {
51,195✔
103
                comm = new0(char, TASK_COMM_LEN + 1); /* Must fit in 16 byte according to prctl(2) */
24,196✔
104
                if (!comm)
24,196✔
105
                        return -ENOMEM;
106

107
                if (prctl(PR_GET_NAME, comm) < 0)
24,196✔
108
                        return -errno;
×
109
        } else {
110
                const char *p;
26,999✔
111

112
                p = procfs_file_alloca(pid, "comm");
26,999✔
113

114
                /* Note that process names of kernel threads can be much longer than TASK_COMM_LEN */
115
                r = read_one_line_file(p, &comm);
26,999✔
116
                if (r == -ENOENT)
26,999✔
117
                        return -ESRCH;
118
                if (r < 0)
22,327✔
119
                        return r;
120
        }
121

122
        escaped = new(char, COMM_MAX_LEN);
46,521✔
123
        if (!escaped)
46,521✔
124
                return -ENOMEM;
125

126
        /* Escape unprintable characters, just in case, but don't grow the string beyond the underlying size */
127
        cellescape(escaped, COMM_MAX_LEN, comm);
46,521✔
128

129
        *ret = TAKE_PTR(escaped);
46,521✔
130
        return 0;
46,521✔
131
}
132

133
int pidref_get_comm(const PidRef *pid, char **ret) {
231✔
134
        _cleanup_free_ char *comm = NULL;
231✔
135
        int r;
231✔
136

137
        if (!pidref_is_set(pid))
231✔
138
                return -ESRCH;
139

140
        if (pidref_is_remote(pid))
462✔
141
                return -EREMOTE;
142

143
        r = pid_get_comm(pid->pid, &comm);
231✔
144
        if (r < 0)
231✔
145
                return r;
146

147
        r = pidref_verify(pid);
231✔
148
        if (r < 0)
231✔
149
                return r;
150

151
        if (ret)
231✔
152
                *ret = TAKE_PTR(comm);
231✔
153
        return 0;
154
}
155

156
static int pid_get_cmdline_nulstr(
23,804✔
157
                pid_t pid,
158
                size_t max_size,
159
                ProcessCmdlineFlags flags,
160
                char **ret,
161
                size_t *ret_size) {
162

163
        _cleanup_free_ char *t = NULL;
23,804✔
164
        const char *p;
23,804✔
165
        size_t k;
23,804✔
166
        int r;
23,804✔
167

168
        /* Retrieves a process' command line as a "sized nulstr", i.e. possibly without the last NUL, but
169
         * with a specified size.
170
         *
171
         * If PROCESS_CMDLINE_COMM_FALLBACK is specified in flags and the process has no command line set
172
         * (the case for kernel threads), or has a command line that resolves to the empty string, will
173
         * return the "comm" name of the process instead. This will use at most _SC_ARG_MAX bytes of input
174
         * data.
175
         *
176
         * Returns an error, 0 if output was read but is truncated, 1 otherwise.
177
         */
178

179
        p = procfs_file_alloca(pid, "cmdline");
24,020✔
180
        r = read_virtual_file(p, max_size, &t, &k); /* Let's assume that each input byte results in >= 1
23,804✔
181
                                                     * columns of output. We ignore zero-width codepoints. */
182
        if (r == -ENOENT)
23,804✔
183
                return -ESRCH;
184
        if (r < 0)
19,047✔
185
                return r;
186

187
        if (k == 0) {
19,047✔
188
                if (!(flags & PROCESS_CMDLINE_COMM_FALLBACK))
572✔
189
                        return -ENOENT;
547✔
190

191
                /* Kernel threads have no argv[] */
192
                _cleanup_free_ char *comm = NULL;
25✔
193

194
                r = pid_get_comm(pid, &comm);
25✔
195
                if (r < 0)
25✔
196
                        return r;
197

198
                free(t);
25✔
199
                t = strjoin("[", comm, "]");
25✔
200
                if (!t)
25✔
201
                        return -ENOMEM;
202

203
                k = strlen(t);
25✔
204
                r = k <= max_size;
25✔
205
                if (r == 0) /* truncation */
25✔
206
                        t[max_size] = '\0';
12✔
207
        }
208

209
        if (ret)
18,500✔
210
                *ret = TAKE_PTR(t);
18,500✔
211
        if (ret_size)
18,500✔
212
                *ret_size = k;
18,500✔
213

214
        return r;
215
}
216

217
int pid_get_cmdline(pid_t pid, size_t max_columns, ProcessCmdlineFlags flags, char **ret) {
18,896✔
218
        _cleanup_free_ char *t = NULL;
18,896✔
219
        size_t k;
18,896✔
220
        char *ans;
18,896✔
221

222
        assert(pid >= 0);
18,896✔
223
        assert(ret);
18,896✔
224

225
        /* Retrieve and format a command line. See above for discussion of retrieval options.
226
         *
227
         * There are two main formatting modes:
228
         *
229
         * - when PROCESS_CMDLINE_QUOTE is specified, output is quoted in C/Python style. If no shell special
230
         *   characters are present, this output can be copy-pasted into the terminal to execute. UTF-8
231
         *   output is assumed.
232
         *
233
         * - otherwise, a compact non-roundtrippable form is returned. Non-UTF8 bytes are replaced by �. The
234
         *   returned string is of the specified console width at most, abbreviated with an ellipsis.
235
         *
236
         * Returns -ESRCH if the process doesn't exist, and -ENOENT if the process has no command line (and
237
         * PROCESS_CMDLINE_COMM_FALLBACK is not specified). Returns 0 and sets *line otherwise. */
238

239
        int full = pid_get_cmdline_nulstr(pid, max_columns, flags, &t, &k);
18,896✔
240
        if (full < 0)
18,896✔
241
                return full;
242

243
        if (flags & (PROCESS_CMDLINE_QUOTE | PROCESS_CMDLINE_QUOTE_POSIX)) {
13,667✔
244
                ShellEscapeFlags shflags = SHELL_ESCAPE_EMPTY |
13,310✔
245
                        FLAGS_SET(flags, PROCESS_CMDLINE_QUOTE_POSIX) * SHELL_ESCAPE_POSIX;
13,310✔
246

247
                assert(!(flags & PROCESS_CMDLINE_USE_LOCALE));
13,310✔
248

249
                _cleanup_strv_free_ char **args = NULL;
13,310✔
250

251
                /* Drop trailing NULs, otherwise strv_parse_nulstr() adds additional empty strings at the end.
252
                 * See also issue #21186. */
253
                args = strv_parse_nulstr_full(t, k, /* drop_trailing_nuls= */ true);
13,310✔
254
                if (!args)
13,310✔
255
                        return -ENOMEM;
256

257
                ans = quote_command_line(args, shflags);
13,310✔
258
                if (!ans)
13,310✔
259
                        return -ENOMEM;
260
        } else {
261
                /* Arguments are separated by NULs. Let's replace those with spaces. */
262
                for (size_t i = 0; i < k - 1; i++)
17,397✔
263
                        if (t[i] == '\0')
17,040✔
264
                                t[i] = ' ';
627✔
265

266
                delete_trailing_chars(t, WHITESPACE);
357✔
267

268
                bool eight_bit = (flags & PROCESS_CMDLINE_USE_LOCALE) && !is_locale_utf8();
357✔
269

270
                ans = escape_non_printable_full(t, max_columns,
1,071✔
271
                                                eight_bit * XESCAPE_8_BIT | !full * XESCAPE_FORCE_ELLIPSIS);
663✔
272
                if (!ans)
357✔
273
                        return -ENOMEM;
274

275
                ans = str_realloc(ans);
357✔
276
        }
277

278
        *ret = ans;
13,667✔
279
        return 0;
13,667✔
280
}
281

282
int pidref_get_cmdline(const PidRef *pid, size_t max_columns, ProcessCmdlineFlags flags, char **ret) {
59✔
283
        _cleanup_free_ char *s = NULL;
59✔
284
        int r;
59✔
285

286
        if (!pidref_is_set(pid))
59✔
287
                return -ESRCH;
288

289
        if (pidref_is_remote(pid))
118✔
290
                return -EREMOTE;
291

292
        r = pid_get_cmdline(pid->pid, max_columns, flags, &s);
59✔
293
        if (r < 0)
59✔
294
                return r;
295

296
        r = pidref_verify(pid);
59✔
297
        if (r < 0)
59✔
298
                return r;
299

300
        if (ret)
59✔
301
                *ret = TAKE_PTR(s);
59✔
302
        return 0;
303
}
304

305
int pid_get_cmdline_strv(pid_t pid, ProcessCmdlineFlags flags, char ***ret) {
4,908✔
306
        _cleanup_free_ char *t = NULL;
4,908✔
307
        char **args;
4,908✔
308
        size_t k;
4,908✔
309
        int r;
4,908✔
310

311
        assert(pid >= 0);
4,908✔
312
        assert((flags & ~PROCESS_CMDLINE_COMM_FALLBACK) == 0);
4,908✔
313
        assert(ret);
4,908✔
314

315
        r = pid_get_cmdline_nulstr(pid, SIZE_MAX, flags, &t, &k);
4,908✔
316
        if (r < 0)
4,908✔
317
                return r;
318

319
        args = strv_parse_nulstr_full(t, k, /* drop_trailing_nuls= */ true);
4,833✔
320
        if (!args)
4,833✔
321
                return -ENOMEM;
322

323
        *ret = args;
4,833✔
324
        return 0;
4,833✔
325
}
326

327
int pidref_get_cmdline_strv(const PidRef *pid, ProcessCmdlineFlags flags, char ***ret) {
×
328
        _cleanup_strv_free_ char **args = NULL;
×
329
        int r;
×
330

331
        if (!pidref_is_set(pid))
×
332
                return -ESRCH;
333

334
        if (pidref_is_remote(pid))
×
335
                return -EREMOTE;
336

337
        r = pid_get_cmdline_strv(pid->pid, flags, &args);
×
338
        if (r < 0)
×
339
                return r;
340

341
        r = pidref_verify(pid);
×
342
        if (r < 0)
×
343
                return r;
344

345
        if (ret)
×
346
                *ret = TAKE_PTR(args);
×
347

348
        return 0;
349
}
350

351
int pid_is_kernel_thread(pid_t pid) {
5,011✔
352
        int r;
5,011✔
353

354
        if (IN_SET(pid, 0, 1) || pid == getpid_cached()) /* pid 1, and we ourselves certainly aren't a kernel thread */
5,011✔
355
                return 0;
5,011✔
356
        if (!pid_is_valid(pid))
4,986✔
357
                return -EINVAL;
358

359
        const char *p = procfs_file_alloca(pid, "stat");
4,986✔
360
        _cleanup_free_ char *line = NULL;
4,986✔
361
        r = read_one_line_file(p, &line);
4,986✔
362
        if (r == -ENOENT)
4,986✔
363
                return -ESRCH;
364
        if (r < 0)
4,986✔
365
                return r;
366

367
        /* Skip past the comm field */
368
        char *q = strrchr(line, ')');
4,986✔
369
        if (!q)
4,986✔
370
                return -EINVAL;
371
        q++;
4,986✔
372

373
        /* Skip 6 fields to reach the flags field */
374
        for (size_t i = 0; i < 6; i++) {
34,902✔
375
                size_t l = strspn(q, WHITESPACE);
29,916✔
376
                if (l < 1)
29,916✔
377
                        return -EINVAL;
378
                q += l;
29,916✔
379

380
                l = strcspn(q, WHITESPACE);
29,916✔
381
                if (l < 1)
29,916✔
382
                        return -EINVAL;
383
                q += l;
29,916✔
384
        }
385

386
        /* Skip preceding whitespace */
387
        size_t l = strspn(q, WHITESPACE);
4,986✔
388
        if (l < 1)
4,986✔
389
                return -EINVAL;
390
        q += l;
4,986✔
391

392
        /* Truncate the rest */
393
        l = strcspn(q, WHITESPACE);
4,986✔
394
        if (l < 1)
4,986✔
395
                return -EINVAL;
396
        q[l] = 0;
4,986✔
397

398
        unsigned long long flags;
4,986✔
399
        r = safe_atollu(q, &flags);
4,986✔
400
        if (r < 0)
4,986✔
401
                return r;
402

403
        return !!(flags & PF_KTHREAD);
4,986✔
404
}
405

406
int pidref_is_kernel_thread(const PidRef *pid) {
1,869✔
407
        int result, r;
1,869✔
408

409
        if (!pidref_is_set(pid))
1,869✔
410
                return -ESRCH;
411

412
        if (pidref_is_remote(pid))
1,869✔
413
                return -EREMOTE;
414

415
        result = pid_is_kernel_thread(pid->pid);
1,869✔
416
        if (result < 0)
1,869✔
417
                return result;
418

419
        r = pidref_verify(pid); /* Verify that the PID wasn't reused since */
1,869✔
420
        if (r < 0)
1,869✔
421
                return r;
×
422

423
        return result;
424
}
425

426
static int get_process_link_contents(pid_t pid, const char *proc_file, char **ret) {
18,359✔
427
        const char *p;
18,359✔
428
        int r;
18,359✔
429

430
        assert(proc_file);
18,359✔
431

432
        p = procfs_file_alloca(pid, proc_file);
18,363✔
433

434
        r = readlink_malloc(p, ret);
18,359✔
435
        return (r == -ENOENT && proc_mounted() > 0) ? -ESRCH : r;
18,359✔
436
}
437

438
int get_process_exe(pid_t pid, char **ret) {
18,333✔
439
        char *d;
18,333✔
440
        int r;
18,333✔
441

442
        assert(pid >= 0);
18,333✔
443

444
        r = get_process_link_contents(pid, "exe", ret);
18,333✔
445
        if (r < 0)
18,333✔
446
                return r;
447

448
        if (ret) {
13,367✔
449
                d = endswith(*ret, " (deleted)");
13,367✔
450
                if (d)
13,367✔
451
                        *d = '\0';
×
452
        }
453

454
        return 0;
455
}
456

457
int pid_get_uid(pid_t pid, uid_t *ret) {
4,227✔
458
        int r;
4,227✔
459

460
        assert(pid >= 0);
4,227✔
461
        assert(ret);
4,227✔
462

463
        if (pid == 0 || pid == getpid_cached()) {
4,227✔
464
                *ret = getuid();
1✔
465
                return 0;
4,227✔
466
        }
467

468
        _cleanup_free_ char *v = NULL;
4,226✔
469
        r = procfs_file_get_field(pid, "status", "Uid", &v);
4,226✔
470
        if (r == -ENOENT)
4,226✔
471
                return -ESRCH;
472
        if (r < 0)
220✔
473
                return r;
474

475
        return parse_uid(v, ret);
220✔
476
}
477

478
int pidref_get_uid(const PidRef *pid, uid_t *ret) {
68✔
479
        int r;
68✔
480

481
        if (!pidref_is_set(pid))
68✔
482
                return -ESRCH;
68✔
483

484
        if (pidref_is_remote(pid))
68✔
485
                return -EREMOTE;
486

487
        if (pid->fd >= 0) {
68✔
488
                r = pidfd_get_uid(pid->fd, ret);
68✔
489
                if (!ERRNO_IS_NEG_NOT_SUPPORTED(r))
68✔
490
                        return r;
491
        }
492

493
        uid_t uid;
×
494
        r = pid_get_uid(pid->pid, &uid);
×
495
        if (r < 0)
×
496
                return r;
497

498
        r = pidref_verify(pid);
×
499
        if (r < 0)
×
500
                return r;
501

502
        if (ret)
×
503
                *ret = uid;
×
504
        return 0;
505
}
506

507
int get_process_gid(pid_t pid, gid_t *ret) {
4,227✔
508
        int r;
4,227✔
509

510
        assert(pid >= 0);
4,227✔
511
        assert(ret);
4,227✔
512

513
        if (pid == 0 || pid == getpid_cached()) {
4,227✔
514
                *ret = getgid();
1✔
515
                return 0;
4,227✔
516
        }
517

518
        _cleanup_free_ char *v = NULL;
4,226✔
519
        r = procfs_file_get_field(pid, "status", "Gid", &v);
4,226✔
520
        if (r == -ENOENT)
4,226✔
521
                return -ESRCH;
522
        if (r < 0)
220✔
523
                return r;
524

525
        return parse_gid(v, ret);
4,226✔
526
}
527

528
int get_process_cwd(pid_t pid, char **ret) {
13✔
529
        assert(pid >= 0);
13✔
530

531
        if (pid == 0 || pid == getpid_cached())
13✔
532
                return safe_getcwd(ret);
×
533

534
        return get_process_link_contents(pid, "cwd", ret);
13✔
535
}
536

537
int get_process_root(pid_t pid, char **ret) {
13✔
538
        assert(pid >= 0);
13✔
539
        return get_process_link_contents(pid, "root", ret);
13✔
540
}
541

542
#define ENVIRONMENT_BLOCK_MAX (5U*1024U*1024U)
543

544
int get_process_environ(pid_t pid, char **ret) {
15✔
545
        _cleanup_fclose_ FILE *f = NULL;
15✔
546
        _cleanup_free_ char *outcome = NULL;
15✔
547
        size_t sz = 0;
15✔
548
        const char *p;
15✔
549
        int r;
15✔
550

551
        assert(pid >= 0);
15✔
552
        assert(ret);
15✔
553

554
        p = procfs_file_alloca(pid, "environ");
15✔
555

556
        r = fopen_unlocked(p, "re", &f);
15✔
557
        if (r == -ENOENT)
15✔
558
                return -ESRCH;
559
        if (r < 0)
15✔
560
                return r;
561

562
        for (;;) {
6,611✔
563
                char c;
6,626✔
564

565
                if (sz >= ENVIRONMENT_BLOCK_MAX)
6,626✔
566
                        return -ENOBUFS;
×
567

568
                if (!GREEDY_REALLOC(outcome, sz + 5))
6,626✔
569
                        return -ENOMEM;
570

571
                r = safe_fgetc(f, &c);
6,626✔
572
                if (r < 0)
6,626✔
573
                        return r;
574
                if (r == 0)
6,626✔
575
                        break;
576

577
                if (c == '\0')
6,611✔
578
                        outcome[sz++] = '\n';
235✔
579
                else
580
                        sz += cescape_char(c, outcome + sz);
6,376✔
581
        }
582

583
        outcome[sz] = '\0';
15✔
584
        *ret = TAKE_PTR(outcome);
15✔
585

586
        return 0;
15✔
587
}
588

589
int pid_get_ppid(pid_t pid, pid_t *ret) {
6✔
590
        _cleanup_free_ char *line = NULL;
6✔
591
        unsigned long ppid;
6✔
592
        const char *p;
6✔
593
        int r;
6✔
594

595
        assert(pid >= 0);
6✔
596

597
        if (pid == 0)
6✔
598
                pid = getpid_cached();
1✔
599
        if (pid == 1) /* PID 1 has no parent, shortcut this case */
6✔
600
                return -EADDRNOTAVAIL;
601

602
        if (pid == getpid_cached()) {
3✔
603
                if (ret)
2✔
604
                        *ret = getppid();
2✔
605
                return 0;
2✔
606
        }
607

608
        p = procfs_file_alloca(pid, "stat");
1✔
609
        r = read_one_line_file(p, &line);
1✔
610
        if (r == -ENOENT)
1✔
611
                return -ESRCH;
612
        if (r < 0)
×
613
                return r;
614

615
        /* Let's skip the pid and comm fields. The latter is enclosed in () but does not escape any () in its
616
         * value, so let's skip over it manually */
617

618
        p = strrchr(line, ')');
×
619
        if (!p)
×
620
                return -EIO;
621
        p++;
×
622

623
        if (sscanf(p, " "
×
624
                   "%*c "  /* state */
625
                   "%lu ", /* ppid */
626
                   &ppid) != 1)
627
                return -EIO;
628

629
        /* If ppid is zero the process has no parent. Which might be the case for PID 1 (caught above)
630
         * but also for processes originating in other namespaces that are inserted into a pidns.
631
         * Return a recognizable error in this case. */
632
        if (ppid == 0)
×
633
                return -EADDRNOTAVAIL;
634

635
        if ((pid_t) ppid < 0 || (unsigned long) (pid_t) ppid != ppid)
×
636
                return -ERANGE;
637

638
        if (ret)
×
639
                *ret = (pid_t) ppid;
×
640

641
        return 0;
642
}
643

644
int pidref_get_ppid(const PidRef *pidref, pid_t *ret) {
3,430✔
645
        int r;
3,430✔
646

647
        if (!pidref_is_set(pidref))
3,430✔
648
                return -ESRCH;
3,430✔
649

650
        if (pidref_is_remote(pidref))
3,430✔
651
                return -EREMOTE;
652

653
        if (pidref->fd >= 0) {
3,430✔
654
                r = pidfd_get_ppid(pidref->fd, ret);
3,430✔
655
                if (!ERRNO_IS_NEG_NOT_SUPPORTED(r))
3,430✔
656
                        return r;
657
        }
658

659
        pid_t ppid;
×
660
        r = pid_get_ppid(pidref->pid, ret ? &ppid : NULL);
×
661
        if (r < 0)
×
662
                return r;
663

664
        r = pidref_verify(pidref);
×
665
        if (r < 0)
×
666
                return r;
667

668
        if (ret)
×
669
                *ret = ppid;
×
670
        return 0;
671
}
672

673
int pidref_get_ppid_as_pidref(const PidRef *pidref, PidRef *ret) {
11✔
674
        pid_t ppid;
11✔
675
        int r;
11✔
676

677
        assert(ret);
11✔
678

679
        r = pidref_get_ppid(pidref, &ppid);
11✔
680
        if (r < 0)
11✔
681
                return r;
11✔
682

683
        for (unsigned attempt = 0; attempt < 16; attempt++) {
10✔
684
                _cleanup_(pidref_done) PidRef parent = PIDREF_NULL;
10✔
685

686
                r = pidref_set_pid(&parent, ppid);
10✔
687
                if (r < 0)
10✔
688
                        return r;
689

690
                /* If we have a pidfd of the original PID, let's verify that the process we acquired really
691
                 * is the parent still */
692
                if (pidref->fd >= 0) {
10✔
693
                        r = pidref_get_ppid(pidref, &ppid);
10✔
694
                        if (r < 0)
10✔
695
                                return r;
696

697
                        /* Did the PPID change since we queried it? if so we might have pinned the wrong
698
                         * process, if its PID got reused by now. Let's try again */
699
                        if (parent.pid != ppid)
10✔
700
                                continue;
×
701
                }
702

703
                *ret = TAKE_PIDREF(parent);
10✔
704
                return 0;
10✔
705
        }
706

707
        /* Give up after 16 tries */
708
        return -ENOTRECOVERABLE;
709
}
710

711
int pid_get_start_time(pid_t pid, usec_t *ret) {
993✔
712
        _cleanup_free_ char *line = NULL;
993✔
713
        const char *p;
993✔
714
        int r;
993✔
715

716
        assert(pid >= 0);
993✔
717

718
        p = procfs_file_alloca(pid, "stat");
993✔
719
        r = read_one_line_file(p, &line);
993✔
720
        if (r == -ENOENT)
993✔
721
                return -ESRCH;
722
        if (r < 0)
993✔
723
                return r;
724

725
        /* Let's skip the pid and comm fields. The latter is enclosed in () but does not escape any () in its
726
         * value, so let's skip over it manually */
727

728
        p = strrchr(line, ')');
993✔
729
        if (!p)
993✔
730
                return -EIO;
731
        p++;
993✔
732

733
        unsigned long llu;
993✔
734

735
        if (sscanf(p, " "
993✔
736
                   "%*c " /* state */
737
                   "%*u " /* ppid */
738
                   "%*u " /* pgrp */
739
                   "%*u " /* session */
740
                   "%*u " /* tty_nr */
741
                   "%*u " /* tpgid */
742
                   "%*u " /* flags */
743
                   "%*u " /* minflt */
744
                   "%*u " /* cminflt */
745
                   "%*u " /* majflt */
746
                   "%*u " /* cmajflt */
747
                   "%*u " /* utime */
748
                   "%*u " /* stime */
749
                   "%*u " /* cutime */
750
                   "%*u " /* cstime */
751
                   "%*i " /* priority */
752
                   "%*i " /* nice */
753
                   "%*u " /* num_threads */
754
                   "%*u " /* itrealvalue */
755
                   "%lu ", /* starttime */
756
                   &llu) != 1)
757
                return -EIO;
758

759
        if (ret)
993✔
760
                *ret = jiffies_to_usec(llu); /* CLOCK_BOOTTIME */
993✔
761

762
        return 0;
763
}
764

765
int pidref_get_start_time(const PidRef *pid, usec_t *ret) {
993✔
766
        usec_t t;
993✔
767
        int r;
993✔
768

769
        if (!pidref_is_set(pid))
993✔
770
                return -ESRCH;
993✔
771

772
        if (pidref_is_remote(pid))
993✔
773
                return -EREMOTE;
774

775
        r = pid_get_start_time(pid->pid, ret ? &t : NULL);
993✔
776
        if (r < 0)
993✔
777
                return r;
778

779
        r = pidref_verify(pid);
993✔
780
        if (r < 0)
993✔
781
                return r;
782

783
        if (ret)
993✔
784
                *ret = t;
993✔
785

786
        return 0;
787
}
788

789
int get_process_umask(pid_t pid, mode_t *ret) {
27,610✔
790
        _cleanup_free_ char *m = NULL;
27,610✔
791
        int r;
27,610✔
792

793
        assert(pid >= 0);
27,610✔
794
        assert(ret);
27,610✔
795

796
        r = procfs_file_get_field(pid, "status", "Umask", &m);
27,610✔
797
        if (r == -ENOENT)
27,610✔
798
                return -ESRCH;
799
        if (r < 0)
27,610✔
800
                return r;
801

802
        return parse_mode(m, ret);
27,610✔
803
}
804

805
/*
806
 * Return values:
807
 * < 0 : pidref_wait_for_terminate() failed to get the state of the
808
 *       process, the process was terminated by a signal, or
809
 *       failed for an unknown reason.
810
 * >=0 : The process terminated normally, and its exit code is
811
 *       returned.
812
 *
813
 * That is, success is indicated by a return value of zero, and an
814
 * error is indicated by a non-zero value.
815
 *
816
 * A warning is emitted if the process terminates abnormally,
817
 * and also if it returns non-zero unless check_exit_code is true.
818
 */
819
int pidref_wait_for_terminate_and_check(const char *name, PidRef *pidref, WaitFlags flags) {
8,352✔
820
        int r;
8,352✔
821

822
        if (!pidref_is_set(pidref))
8,352✔
823
                return -ESRCH;
8,352✔
824
        if (pidref_is_remote(pidref))
16,704✔
825
                return -EREMOTE;
826
        if (pidref->pid == 1 || pidref_is_self(pidref))
8,352✔
827
                return -ECHILD;
×
828

829
        _cleanup_free_ char *buffer = NULL;
8,352✔
830
        if (!name) {
8,352✔
831
                r = pidref_get_comm(pidref, &buffer);
2✔
832
                if (r < 0)
2✔
833
                        log_debug_errno(r, "Failed to acquire process name of " PID_FMT ", ignoring: %m", pidref->pid);
×
834
                else
835
                        name = buffer;
2✔
836
        }
837

838
        int prio = flags & WAIT_LOG_ABNORMAL ? LOG_ERR : LOG_DEBUG;
8,352✔
839

840
        siginfo_t status;
8,352✔
841
        r = pidref_wait_for_terminate(pidref, &status);
8,352✔
842
        if (r < 0)
8,352✔
843
                return log_full_errno(prio, r, "Failed to wait for '%s': %m", strna(name));
×
844

845
        if (status.si_code == CLD_EXITED) {
8,352✔
846
                if (status.si_status != EXIT_SUCCESS)
8,352✔
847
                        log_full(flags & WAIT_LOG_NON_ZERO_EXIT_STATUS ? LOG_ERR : LOG_DEBUG,
72✔
848
                                 "'%s' failed with exit status %i.", strna(name), status.si_status);
849
                else
850
                        log_debug("'%s' succeeded.", name);
8,280✔
851

852
                return status.si_status;
8,352✔
853

854
        } else if (IN_SET(status.si_code, CLD_KILLED, CLD_DUMPED))
×
855
                return log_full_errno(prio, SYNTHETIC_ERRNO(EPROTO),
×
856
                                      "'%s' terminated by signal %s.", strna(name), signal_to_string(status.si_status));
857

858
        return log_full_errno(prio, SYNTHETIC_ERRNO(EPROTO),
×
859
                              "'%s' failed due to unknown reason.", strna(name));
860
}
861

862
int kill_and_sigcont(pid_t pid, int sig) {
×
863
        int r;
×
864

865
        r = RET_NERRNO(kill(pid, sig));
×
866

867
        /* If this worked, also send SIGCONT, unless we already just sent a SIGCONT, or SIGKILL was sent which isn't
868
         * affected by a process being suspended anyway. */
869
        if (r >= 0 && !IN_SET(sig, SIGCONT, SIGKILL))
×
870
                (void) kill(pid, SIGCONT);
×
871

872
        return r;
×
873
}
874

875
int getenv_for_pid(pid_t pid, const char *field, char **ret) {
6,434✔
876
        _cleanup_fclose_ FILE *f = NULL;
6,434✔
877
        const char *path;
6,434✔
878
        size_t sum = 0;
6,434✔
879
        int r;
6,434✔
880

881
        assert(pid >= 0);
6,434✔
882
        assert(field);
6,434✔
883
        assert(ret);
6,434✔
884

885
        if (pid == 0 || pid == getpid_cached())
6,434✔
886
                return strdup_to_full(ret, getenv(field));
14✔
887

888
        if (!pid_is_valid(pid))
6,420✔
889
                return -EINVAL;
890

891
        path = procfs_file_alloca(pid, "environ");
6,420✔
892

893
        r = fopen_unlocked(path, "re", &f);
6,420✔
894
        if (r == -ENOENT)
6,420✔
895
                return -ESRCH;
896
        if (r < 0)
6,007✔
897
                return r;
898

899
        for (;;) {
77,538✔
900
                _cleanup_free_ char *line = NULL;
36,520✔
901
                const char *match;
41,036✔
902

903
                if (sum > ENVIRONMENT_BLOCK_MAX) /* Give up searching eventually */
41,036✔
904
                        return -ENOBUFS;
905

906
                r = read_nul_string(f, LONG_LINE_MAX, &line);
41,036✔
907
                if (r < 0)
41,036✔
908
                        return r;
909
                if (r == 0)  /* EOF */
41,036✔
910
                        break;
911

912
                sum += r;
36,520✔
913

914
                match = startswith(line, field);
36,520✔
915
                if (match && *match == '=')
36,520✔
916
                        return strdup_to_full(ret, match + 1);
18✔
917
        }
918

919
        *ret = NULL;
4,516✔
920
        return 0;
4,516✔
921
}
922

923
int pidref_is_my_child(PidRef *pid) {
3,408✔
924
        int r;
3,408✔
925

926
        if (!pidref_is_set(pid))
3,408✔
927
                return -ESRCH;
3,408✔
928

929
        if (pidref_is_remote(pid))
3,408✔
930
                return -EREMOTE;
931

932
        if (pid->pid == 1 || pidref_is_self(pid))
3,408✔
933
                return false;
×
934

935
        pid_t ppid;
3,408✔
936
        r = pidref_get_ppid(pid, &ppid);
3,408✔
937
        if (r == -EADDRNOTAVAIL) /* if this process is outside of our pidns, it is definitely not our child */
3,408✔
938
                return false;
939
        if (r < 0)
3,408✔
940
                return r;
941

942
        return ppid == getpid_cached();
3,408✔
943
}
944

945
int pid_is_my_child(pid_t pid) {
×
946

947
        if (pid == 0)
×
948
                return false;
×
949

950
        return pidref_is_my_child(&PIDREF_MAKE_FROM_PID(pid));
×
951
}
952

953
int pidref_is_unwaited(PidRef *pid) {
11,738✔
954
        int r;
11,738✔
955

956
        /* Checks whether a PID is still valid at all, including a zombie */
957

958
        if (!pidref_is_set(pid))
11,738✔
959
                return -ESRCH;
960

961
        if (pidref_is_remote(pid))
11,737✔
962
                return -EREMOTE;
963

964
        if (pid->pid == 1 || pidref_is_self(pid))
11,737✔
965
                return true;
7✔
966

967
        r = pidref_kill(pid, 0);
11,730✔
968
        if (r == -ESRCH)
11,730✔
969
                return false;
970
        if (r < 0)
3,505✔
971
                return r;
266✔
972

973
        return true;
974
}
975

976
int pid_is_unwaited(pid_t pid) {
10,745✔
977

978
        if (pid == 0)
10,745✔
979
                return true;
10,745✔
980

981
        return pidref_is_unwaited(&PIDREF_MAKE_FROM_PID(pid));
10,745✔
982
}
983

984
int pid_is_alive(pid_t pid) {
16,629✔
985
        int r;
16,629✔
986

987
        /* Checks whether a PID is still valid and not a zombie */
988

989
        if (pid < 0)
16,629✔
990
                return -ESRCH;
991

992
        if (pid <= 1) /* If we or PID 1 would be a zombie, this code would not be running */
16,628✔
993
                return true;
994

995
        if (pid == getpid_cached())
16,628✔
996
                return true;
997

998
        r = get_process_state(pid);
16,627✔
999
        if (r == -ESRCH)
16,627✔
1000
                return false;
1001
        if (r < 0)
13,470✔
1002
                return r;
1003

1004
        return r != 'Z';
13,470✔
1005
}
1006

1007
int pidref_is_alive(const PidRef *pidref) {
16,624✔
1008
        int r, result;
16,624✔
1009

1010
        if (!pidref_is_set(pidref))
16,624✔
1011
                return -ESRCH;
1012

1013
        if (pidref_is_remote(pidref))
16,624✔
1014
                return -EREMOTE;
1015

1016
        result = pid_is_alive(pidref->pid);
16,624✔
1017
        if (result < 0) {
16,624✔
1018
                assert(result != -ESRCH);
×
1019
                return result;
1020
        }
1021

1022
        r = pidref_verify(pidref);
16,624✔
1023
        if (r == -ESRCH)
16,624✔
1024
                return false;
1025
        if (r < 0)
13,467✔
1026
                return r;
×
1027

1028
        return result;
1029
}
1030

1031
int pidref_from_same_root_fs(PidRef *a, PidRef *b) {
20✔
1032
        _cleanup_(pidref_done) PidRef self = PIDREF_NULL;
×
1033
        int r;
20✔
1034

1035
        /* Checks if the two specified processes have the same root fs. Either can be specified as NULL in
1036
         * which case we'll check against ourselves. */
1037

1038
        if (!a || !b) {
20✔
1039
                r = pidref_set_self(&self);
×
1040
                if (r < 0)
×
1041
                        return r;
1042
                if (!a)
×
1043
                        a = &self;
×
1044
                if (!b)
×
1045
                        b = &self;
×
1046
        }
1047

1048
        if (!pidref_is_set(a) || !pidref_is_set(b))
20✔
1049
                return -ESRCH;
×
1050

1051
        /* If one of the two processes have the same root they cannot have the same root fs, but if both of
1052
         * them do we don't know */
1053
        if (pidref_is_remote(a) && pidref_is_remote(b))
20✔
1054
                return -EREMOTE;
1055
        if (pidref_is_remote(a) || pidref_is_remote(b))
60✔
1056
                return false;
1057

1058
        if (pidref_equal(a, b))
20✔
1059
                return true;
1060

1061
        const char *roota = procfs_file_alloca(a->pid, "root");
18✔
1062
        const char *rootb = procfs_file_alloca(b->pid, "root");
18✔
1063

1064
        int result = inode_same(roota, rootb, 0);
18✔
1065
        if (result == -ENOENT)
18✔
1066
                return proc_mounted() == 0 ? -ENOSYS : -ESRCH;
×
1067
        if (result < 0)
18✔
1068
                return result;
1069

1070
        r = pidref_verify(a);
18✔
1071
        if (r < 0)
18✔
1072
                return r;
1073
        r = pidref_verify(b);
18✔
1074
        if (r < 0)
18✔
1075
                return r;
×
1076

1077
        return result;
1078
}
1079

1080
bool is_main_thread(void) {
8,398,009✔
1081
        static thread_local int cached = -1;
8,398,009✔
1082

1083
        if (cached < 0)
8,398,009✔
1084
                cached = getpid_cached() == gettid();
65,848✔
1085

1086
        return cached;
8,398,009✔
1087
}
1088

1089
unsigned long personality_from_string(const char *s) {
9✔
1090
        Architecture architecture;
9✔
1091

1092
        if (!s)
9✔
1093
                return PERSONALITY_INVALID;
1094

1095
        /* Parse a personality specifier. We use our own identifiers that indicate specific ABIs, rather than just
1096
         * hints regarding the register size, since we want to keep things open for multiple locally supported ABIs for
1097
         * the same register size. */
1098

1099
        architecture = architecture_from_string(s);
8✔
1100
        if (architecture < 0)
8✔
1101
                return PERSONALITY_INVALID;
1102

1103
        if (architecture == native_architecture())
6✔
1104
                return PER_LINUX;
1105
#ifdef ARCHITECTURE_SECONDARY
1106
        if (architecture == ARCHITECTURE_SECONDARY)
3✔
1107
                return PER_LINUX32;
2✔
1108
#endif
1109

1110
        return PERSONALITY_INVALID;
1111
}
1112

1113
const char* personality_to_string(unsigned long p) {
3,287✔
1114
        Architecture architecture = _ARCHITECTURE_INVALID;
3,287✔
1115

1116
        if (p == PER_LINUX)
3,287✔
1117
                architecture = native_architecture();
1118
#ifdef ARCHITECTURE_SECONDARY
1119
        else if (p == PER_LINUX32)
3,282✔
1120
                architecture = ARCHITECTURE_SECONDARY;
1121
#endif
1122

1123
        if (architecture < 0)
1124
                return NULL;
1125

1126
        return architecture_to_string(architecture);
7✔
1127
}
1128

1129
int safe_personality(unsigned long p) {
1,582✔
1130
        int ret;
1,582✔
1131

1132
        /* So here's the deal, personality() is weirdly defined by glibc. In some cases it returns a failure via errno,
1133
         * and in others as negative return value containing an errno-like value. Let's work around this: this is a
1134
         * wrapper that uses errno if it is set, and uses the return value otherwise. And then it sets both errno and
1135
         * the return value indicating the same issue, so that we are definitely on the safe side.
1136
         *
1137
         * See https://github.com/systemd/systemd/issues/6737 */
1138

1139
        errno = 0;
1,582✔
1140
        ret = personality(p);
1,582✔
1141
        if (ret < 0) {
1,582✔
1142
                if (errno != 0)
12✔
1143
                        return -errno;
12✔
1144

1145
                errno = -ret;
×
1146
        }
1147

1148
        return ret;
1149
}
1150

1151
int opinionated_personality(unsigned long *ret) {
1,567✔
1152
        int current;
1,567✔
1153

1154
        assert(ret);
1,567✔
1155

1156
        /* Returns the current personality, or PERSONALITY_INVALID if we can't determine it. This function is a bit
1157
         * opinionated though, and ignores all the finer-grained bits and exotic personalities, only distinguishing the
1158
         * two most relevant personalities: PER_LINUX and PER_LINUX32. */
1159

1160
        current = safe_personality(PERSONALITY_INVALID);
1,567✔
1161
        if (current < 0)
1,567✔
1162
                return current;
1163

1164
        if (((unsigned long) current & OPINIONATED_PERSONALITY_MASK) == PER_LINUX32)
1,567✔
1165
                *ret = PER_LINUX32;
×
1166
        else
1167
                *ret = PER_LINUX;
1,567✔
1168

1169
        return 0;
1170
}
1171

1172
void valgrind_summary_hack(void) {
45✔
1173
#if HAVE_VALGRIND_VALGRIND_H
1174
        if (getpid_cached() == 1 && RUNNING_ON_VALGRIND) {
1175
                pid_t pid;
1176
                pid = raw_clone(SIGCHLD);
1177
                if (pid < 0)
1178
                        log_struct_errno(
1179
                                LOG_EMERG, errno,
1180
                                LOG_MESSAGE_ID(SD_MESSAGE_VALGRIND_HELPER_FORK_STR),
1181
                                LOG_MESSAGE("Failed to fork off valgrind helper: %m"));
1182
                else if (pid == 0)
1183
                        exit(EXIT_SUCCESS);
1184
                else {
1185
                        log_info("Spawned valgrind helper as PID "PID_FMT".", pid);
1186
                        _cleanup_(pidref_done) PidRef pidref = PIDREF_MAKE_FROM_PID(pid);
1187
                        (void) pidref_set_pid(&pidref, pid);
1188
                        (void) pidref_wait_for_terminate(&pidref, NULL);
1189
                }
1190
        }
1191
#endif
1192
}
45✔
1193

1194
int pid_compare_func(const pid_t *a, const pid_t *b) {
1,641✔
1195
        /* Suitable for usage in qsort() */
1196
        assert(a);
1,641✔
1197
        assert(b);
1,641✔
1198

1199
        return CMP(*a, *b);
1,641✔
1200
}
1201

1202
bool nice_is_valid(int n) {
997✔
1203
        return n >= PRIO_MIN && n < PRIO_MAX;
997✔
1204
}
1205

1206
bool sched_policy_is_valid(int policy) {
×
1207
        return IN_SET(policy, SCHED_OTHER, SCHED_BATCH, SCHED_IDLE, SCHED_FIFO, SCHED_RR, SCHED_EXT);
×
1208
}
1209

1210
bool sched_policy_supported(int policy) {
4✔
1211
        return sched_get_priority_min(policy) >= 0;
4✔
1212
}
1213

1214
/* Wrappers around sched_get_priority_{min,max}() that gracefully handles missing SCHED_EXT support in the kernel */
1215
int sched_get_priority_min_safe(int policy) {
4✔
1216
        int r;
4✔
1217

1218
        r = sched_get_priority_min(policy);
4✔
1219
        if (r >= 0)
4✔
1220
                return r;
4✔
1221

1222
        /* Fallback priority */
1223
        return 0;
1224
}
1225

1226
int sched_get_priority_max_safe(int policy) {
4✔
1227
        int r;
4✔
1228

1229
        r = sched_get_priority_max(policy);
4✔
1230
        if (r >= 0)
4✔
1231
                return r;
4✔
1232

1233
        return 0;
1234
}
1235

1236
/* The cached PID, possible values:
1237
 *
1238
 *     == UNSET [0]  → cache not initialized yet
1239
 *     == BUSY [-1]  → some thread is initializing it at the moment
1240
 *     any other     → the cached PID
1241
 */
1242

1243
#define CACHED_PID_UNSET ((pid_t) 0)
1244
#define CACHED_PID_BUSY ((pid_t) -1)
1245

1246
static pid_t cached_pid = CACHED_PID_UNSET;
1247

1248
void reset_cached_pid(void) {
1,830✔
1249
        /* Invoked in the child after a fork(), i.e. at the first moment the PID changed */
1250
        cached_pid = CACHED_PID_UNSET;
1,830✔
1251
}
1,830✔
1252

1253
pid_t getpid_cached(void) {
176,296,255✔
1254
        static bool installed = false;
176,296,255✔
1255
        pid_t current_value = CACHED_PID_UNSET;
176,296,255✔
1256

1257
        /* getpid_cached() is much like getpid(), but caches the value in local memory, to avoid having to invoke a
1258
         * system call each time. This restores glibc behaviour from before 2.24, when getpid() was unconditionally
1259
         * cached. Starting with 2.24 getpid() started to become prohibitively expensive when used for detecting when
1260
         * objects were used across fork()s. With this caching the old behaviour is somewhat restored.
1261
         *
1262
         * https://bugzilla.redhat.com/show_bug.cgi?id=1443976
1263
         * https://sourceware.org/git/gitweb.cgi?p=glibc.git;h=c579f48edba88380635ab98cb612030e3ed8691e
1264
         */
1265

1266
        (void) __atomic_compare_exchange_n(
176,296,255✔
1267
                        &cached_pid,
1268
                        &current_value,
1269
                        CACHED_PID_BUSY,
1270
                        false,
1271
                        __ATOMIC_SEQ_CST,
1272
                        __ATOMIC_SEQ_CST);
1273

1274
        switch (current_value) {
176,296,255✔
1275

1276
        case CACHED_PID_UNSET: { /* Not initialized yet, then do so now */
117,247✔
1277
                pid_t new_pid;
117,247✔
1278

1279
                new_pid = getpid();
117,247✔
1280

1281
                if (!installed) {
117,247✔
1282
                        /* __register_atfork() either returns 0 or -ENOMEM, in its glibc implementation. Since it's
1283
                         * only half-documented (glibc doesn't document it but LSB does — though only superficially)
1284
                         * we'll check for errors only in the most generic fashion possible. */
1285

1286
                        if (pthread_atfork(NULL, NULL, reset_cached_pid) != 0) {
82,856✔
1287
                                /* OOM? Let's try again later */
1288
                                cached_pid = CACHED_PID_UNSET;
×
1289
                                return new_pid;
×
1290
                        }
1291

1292
                        installed = true;
82,856✔
1293
                }
1294

1295
                cached_pid = new_pid;
117,247✔
1296
                return new_pid;
117,247✔
1297
        }
1298

1299
        case CACHED_PID_BUSY: /* Somebody else is currently initializing */
×
1300
                return getpid();
×
1301

1302
        default: /* Properly initialized */
1303
                return current_value;
1304
        }
1305
}
1306

1307
int must_be_root(void) {
79✔
1308

1309
        if (geteuid() == 0)
79✔
1310
                return 0;
1311

1312
        return log_error_errno(SYNTHETIC_ERRNO(EPERM), "Need to be root.");
×
1313
}
1314

1315
pid_t clone_with_nested_stack(int (*fn)(void *), int flags, void *userdata) {
4,904✔
1316
        size_t ps;
4,904✔
1317
        pid_t pid;
4,904✔
1318
        void *mystack;
4,904✔
1319

1320
        /* A wrapper around glibc's clone() call that automatically sets up a "nested" stack. Only supports
1321
         * invocations without CLONE_VM, so that we can continue to use the parent's stack mapping.
1322
         *
1323
         * Note: glibc's clone() wrapper does not synchronize malloc() locks. This means that if the parent
1324
         * is threaded these locks will be in an undefined state in the child, and hence memory allocations
1325
         * are likely going to run into deadlocks. Hence: if you use this function make sure your parent is
1326
         * strictly single-threaded or your child never calls malloc(). */
1327

1328
        assert((flags & (CLONE_VM|CLONE_PARENT_SETTID|CLONE_CHILD_SETTID|
4,904✔
1329
                         CLONE_CHILD_CLEARTID|CLONE_SETTLS)) == 0);
1330

1331
        /* We allocate some space on the stack to use as the stack for the child (hence "nested"). Note that
1332
         * the net effect is that the child will have the start of its stack inside the stack of the parent,
1333
         * but since they are a CoW copy of each other that's fine. We allocate one page-aligned page. But
1334
         * since we don't want to deal with differences between systems where the stack grows backwards or
1335
         * forwards we'll allocate one more and place the stack address in the middle. Except that we also
1336
         * want it page aligned, hence we'll allocate one page more. Makes 3. */
1337

1338
        ps = page_size();
4,904✔
1339
        mystack = alloca(ps*3);
4,904✔
1340
        mystack = (uint8_t*) mystack + ps; /* move pointer one page ahead since stacks usually grow backwards */
4,904✔
1341
        mystack = (void*) ALIGN_TO((uintptr_t) mystack, ps); /* align to page size (moving things further ahead) */
4,904✔
1342

1343
#if HAVE_CLONE
1344
        pid = clone(fn, mystack, flags, userdata);
4,904✔
1345
#else
1346
        pid = __clone2(fn, mystack, ps, flags, userdata);
1347
#endif
1348
        if (pid < 0)
4,904✔
1349
                return -errno;
×
1350

1351
        return pid;
1352
}
1353

1354
static int fork_flags_to_signal(ForkFlags flags) {
32,567✔
1355
        return (flags & FORK_DEATHSIG_SIGTERM) ? SIGTERM :
32,567✔
1356
                (flags & FORK_DEATHSIG_SIGINT) ? SIGINT :
1,066✔
1357
                                                 SIGKILL;
1358
}
1359

1360
int pidref_safe_fork_full(
31,569✔
1361
                const char *name,
1362
                const int stdio_fds[3],
1363
                int except_fds[],
1364
                size_t n_except_fds,
1365
                ForkFlags flags,
1366
                PidRef *ret) {
1367

1368
        pid_t original_pid, pid;
31,569✔
1369
        sigset_t saved_ss, ss;
31,569✔
1370
        _unused_ _cleanup_(block_signals_reset) sigset_t *saved_ssp = NULL;
×
1371
        bool block_signals = false, block_all = false, intermediary = false;
31,569✔
1372
        _cleanup_close_pair_ int pidref_transport_fds[2] = EBADF_PAIR;
65,123✔
1373
        int prio, r;
31,569✔
1374

1375
        assert(!FLAGS_SET(flags, FORK_WAIT|FORK_FREEZE));
31,569✔
1376
        assert(!FLAGS_SET(flags, FORK_DETACH) ||
31,569✔
1377
               (flags & (FORK_WAIT|FORK_DEATHSIG_SIGTERM|FORK_DEATHSIG_SIGINT|FORK_DEATHSIG_SIGKILL)) == 0);
1378

1379
        /* A wrapper around fork(), that does a couple of important initializations in addition to mere
1380
         * forking. If provided, ret is initialized in both the parent and the child process, both times
1381
         * referencing the child process. Returns == 0 in the child and > 0 in the parent. */
1382

1383
        prio = flags & FORK_LOG ? LOG_ERR : LOG_DEBUG;
31,569✔
1384

1385
        original_pid = getpid_cached();
31,569✔
1386

1387
        if (flags & FORK_FLUSH_STDIO) {
31,569✔
1388
                fflush(stdout);
5✔
1389
                fflush(stderr); /* This one shouldn't be necessary, stderr should be unbuffered anyway, but let's better be safe than sorry */
5✔
1390
        }
1391

1392
        if (flags & (FORK_RESET_SIGNALS|FORK_DEATHSIG_SIGTERM|FORK_DEATHSIG_SIGINT)) {
31,569✔
1393
                /* We temporarily block all signals, so that the new child has them blocked initially. This
1394
                 * way, we can be sure that SIGTERMs are not lost we might send to the child. (Note that for
1395
                 * FORK_DEATHSIG_SIGKILL we don't bother, since it cannot be blocked anyway.) */
1396

1397
                assert_se(sigfillset(&ss) >= 0);
26,993✔
1398
                block_signals = block_all = true;
1399

1400
        } else if (flags & FORK_WAIT) {
4,576✔
1401
                /* Let's block SIGCHLD at least, so that we can safely watch for the child process */
1402

1403
                assert_se(sigemptyset(&ss) >= 0);
164✔
1404
                assert_se(sigaddset(&ss, SIGCHLD) >= 0);
164✔
1405
                block_signals = true;
1406
        }
1407

1408
        if (block_signals) {
1409
                if (sigprocmask(SIG_BLOCK, &ss, &saved_ss) < 0)
27,157✔
1410
                        return log_full_errno(prio, errno, "Failed to block signal mask: %m");
×
1411
                saved_ssp = &saved_ss;
27,157✔
1412
        }
1413

1414
        if (FLAGS_SET(flags, FORK_DETACH)) {
31,569✔
1415
                /* Fork off intermediary child if needed */
1416

1417
                r = is_reaper_process();
100✔
1418
                if (r < 0)
100✔
1419
                        return log_full_errno(prio, r, "Failed to determine if we are a reaper process: %m");
×
1420

1421
                if (!r) {
100✔
1422
                        /* Not a reaper process, hence do a double fork() so we are reparented to one */
1423

1424
                        if (ret && socketpair(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC, 0, pidref_transport_fds) < 0)
11✔
1425
                                return log_full_errno(prio, errno, "Failed to allocate pidref socket: %m");
×
1426

1427
                        pid = fork();
11✔
1428
                        if (pid < 0)
30✔
1429
                                return log_full_errno(prio, errno, "Failed to fork off '%s': %m", strna(name));
×
1430
                        if (pid > 0) {
30✔
1431
                                log_debug("Successfully forked off intermediary '%s' as PID " PID_FMT ".", strna(name), pid);
11✔
1432

1433
                                pidref_transport_fds[1] = safe_close(pidref_transport_fds[1]);
11✔
1434

1435
                                if (pidref_transport_fds[0] >= 0) {
11✔
1436
                                        /* Wait for the intermediary child to exit so the caller can be
1437
                                         * certain the actual child process has been reparented by the time
1438
                                         * this function returns. */
1439
                                        r = pidref_wait_for_terminate_and_check(
10✔
1440
                                                        name,
1441
                                                        &PIDREF_MAKE_FROM_PID(pid),
10✔
1442
                                                        FLAGS_SET(flags, FORK_LOG) ? WAIT_LOG : 0);
1443
                                        if (r < 0)
10✔
1444
                                                return log_full_errno(prio, r, "Failed to wait for intermediary process: %m");
×
1445
                                        if (r != EXIT_SUCCESS) /* exit status > 0 should be treated as failure, too */
10✔
1446
                                                return -EPROTO;
1447

1448
                                        int pidfd;
10✔
1449
                                        ssize_t n = receive_one_fd_iov(
20✔
1450
                                                        pidref_transport_fds[0],
1451
                                                        &IOVEC_MAKE(&pid, sizeof(pid)),
10✔
1452
                                                        /* iovlen= */ 1,
1453
                                                        /* flags= */ 0,
1454
                                                        &pidfd);
1455
                                        if (n < 0)
10✔
1456
                                                return log_full_errno(prio, n, "Failed to receive child pidref: %m");
×
1457

1458
                                        *ret = (PidRef) { .pid = pid, .fd = pidfd };
10✔
1459
                                }
1460

1461
                                return 1; /* return in the parent */
11✔
1462
                        }
1463

1464
                        pidref_transport_fds[0] = safe_close(pidref_transport_fds[0]);
19✔
1465
                        intermediary = true;
19✔
1466
                }
1467
        }
1468

1469
        if ((flags & (FORK_NEW_MOUNTNS|FORK_NEW_USERNS|FORK_NEW_NETNS|FORK_NEW_PIDNS)) != 0)
31,577✔
1470
                pid = raw_clone(SIGCHLD|
6,485✔
1471
                                (FLAGS_SET(flags, FORK_NEW_MOUNTNS) ? CLONE_NEWNS : 0) |
6,485✔
1472
                                (FLAGS_SET(flags, FORK_NEW_USERNS) ? CLONE_NEWUSER : 0) |
6,485✔
1473
                                (FLAGS_SET(flags, FORK_NEW_NETNS) ? CLONE_NEWNET : 0) |
6,485✔
1474
                                (FLAGS_SET(flags, FORK_NEW_PIDNS) ? CLONE_NEWPID : 0));
6,485✔
1475
        else
1476
                pid = fork();
25,092✔
1477
        if (pid < 0)
65,125✔
1478
                return log_full_errno(prio, errno, "Failed to fork off '%s': %m", strna(name));
2✔
1479
        if (pid > 0) {
65,124✔
1480

1481
                /* If we are in the intermediary process, exit now */
1482
                if (intermediary) {
31,117✔
1483
                        if (pidref_transport_fds[1] >= 0) {
13✔
1484
                                _cleanup_(pidref_done) PidRef pidref = PIDREF_NULL;
10✔
1485

1486
                                r = pidref_set_pid(&pidref, pid);
10✔
1487
                                if (r < 0) {
10✔
1488
                                        log_full_errno(prio, r, "Failed to open reference to PID "PID_FMT": %m", pid);
×
1489
                                        _exit(EXIT_FAILURE);
×
1490
                                }
1491

1492
                                r = send_one_fd_iov(
10✔
1493
                                                pidref_transport_fds[1],
1494
                                                pidref.fd,
1495
                                                &IOVEC_MAKE(&pidref.pid, sizeof(pidref.pid)),
1496
                                                /* iovlen= */ 1,
1497
                                                /* flags= */ 0);
1498
                                if (r < 0) {
10✔
1499
                                        log_full_errno(prio, r, "Failed to send child pidref: %m");
×
1500
                                        _exit(EXIT_FAILURE);
×
1501
                                }
1502
                        }
1503

1504
                        _exit(EXIT_SUCCESS);
13✔
1505
                }
1506

1507
                /* We are in the parent process */
1508
                log_debug("Successfully forked off '%s' as PID " PID_FMT ".", strna(name), pid);
31,104✔
1509

1510
                if (flags & FORK_WAIT) {
31,104✔
1511
                        if (block_all) {
1,012✔
1512
                                /* undo everything except SIGCHLD */
1513
                                ss = saved_ss;
848✔
1514
                                assert_se(sigaddset(&ss, SIGCHLD) >= 0);
848✔
1515
                                (void) sigprocmask(SIG_SETMASK, &ss, NULL);
848✔
1516
                        }
1517

1518
                        r = pidref_wait_for_terminate_and_check(
1,012✔
1519
                                        name,
1520
                                        &PIDREF_MAKE_FROM_PID(pid),
1,012✔
1521
                                        FLAGS_SET(flags, FORK_LOG) ? WAIT_LOG : 0);
1522
                        if (r < 0)
1,012✔
1523
                                return r;
1,012✔
1524
                        if (r != EXIT_SUCCESS) /* exit status > 0 should be treated as failure, too */
1,012✔
1525
                                return -EPROTO;
1526

1527
                        /* If we are in the parent and successfully waited, then the process doesn't exist anymore. */
1528
                        if (ret)
1,004✔
1529
                                *ret = PIDREF_NULL;
28✔
1530

1531
                        return 1;
1,004✔
1532
                }
1533

1534
                if (ret) {
30,092✔
1535
                        r = pidref_set_pid(ret, pid);
28,807✔
1536
                        if (r < 0) /* Let's not fail for this, no matter what, the process exists after all, and that's key */
28,807✔
1537
                                *ret = PIDREF_MAKE_FROM_PID(pid);
×
1538
                }
1539

1540
                return 1;
30,092✔
1541
        }
1542

1543
        /* We are in the child process */
1544

1545
        pidref_transport_fds[1] = safe_close(pidref_transport_fds[1]);
34,007✔
1546

1547
        /* Restore signal mask manually */
1548
        saved_ssp = NULL;
34,007✔
1549

1550
        if (flags & FORK_REOPEN_LOG) {
34,007✔
1551
                /* Close the logs if requested, before we log anything. And make sure we reopen it if needed. */
1552
                log_close();
8,493✔
1553
                log_set_open_when_needed(true);
8,493✔
1554
                log_settle_target();
8,493✔
1555
        }
1556

1557
        if (name) {
34,007✔
1558
                r = rename_process(name);
34,007✔
1559
                if (r < 0)
34,007✔
1560
                        log_full_errno(flags & FORK_LOG ? LOG_WARNING : LOG_DEBUG,
×
1561
                                       r, "Failed to rename process, ignoring: %m");
1562
        }
1563

1564
        /* let's disable dlopen() in the child, as a paranoia safety precaution: children should not live for
1565
         * long and only do minimal work before exiting or exec()ing. Doing dlopen() is not either. If people
1566
         * want dlopen() they should do it before forking. This is a safety precaution in particular for
1567
         * cases where the child does namespace shenanigans: we should never end up loading a module from a
1568
         * foreign environment. Note that this has no effect on NSS! (i.e. it only has effect on uses of our
1569
         * dlopen_safe(), which we use comprehensively in our codebase, but glibc NSS doesn't bother, of
1570
         * course.) */
1571
        if (!FLAGS_SET(flags, FORK_ALLOW_DLOPEN))
34,007✔
1572
                block_dlopen();
33,968✔
1573

1574
        if (flags & (FORK_DEATHSIG_SIGTERM|FORK_DEATHSIG_SIGINT|FORK_DEATHSIG_SIGKILL))
34,007✔
1575
                if (prctl(PR_SET_PDEATHSIG, fork_flags_to_signal(flags)) < 0) {
32,567✔
1576
                        log_full_errno(prio, errno, "Failed to set death signal: %m");
×
1577
                        _exit(EXIT_FAILURE);
×
1578
                }
1579

1580
        if (flags & FORK_RESET_SIGNALS) {
34,007✔
1581
                r = reset_all_signal_handlers();
27,759✔
1582
                if (r < 0) {
27,759✔
1583
                        log_full_errno(prio, r, "Failed to reset signal handlers: %m");
×
1584
                        _exit(EXIT_FAILURE);
×
1585
                }
1586

1587
                /* This implicitly undoes the signal mask stuff we did before the fork()ing above */
1588
                r = reset_signal_mask();
27,759✔
1589
                if (r < 0) {
27,759✔
1590
                        log_full_errno(prio, r, "Failed to reset signal mask: %m");
×
1591
                        _exit(EXIT_FAILURE);
×
1592
                }
1593
        } else if (block_signals) { /* undo what we did above */
6,248✔
1594
                if (sigprocmask(SIG_SETMASK, &saved_ss, NULL) < 0) {
5,749✔
1595
                        log_full_errno(prio, errno, "Failed to restore signal mask: %m");
×
1596
                        _exit(EXIT_FAILURE);
×
1597
                }
1598
        }
1599

1600
        if (flags & (FORK_DEATHSIG_SIGTERM|FORK_DEATHSIG_SIGKILL|FORK_DEATHSIG_SIGINT)) {
34,007✔
1601
                pid_t ppid;
32,567✔
1602
                /* Let's see if the parent PID is still the one we started from? If not, then the parent
1603
                 * already died by the time we set PR_SET_PDEATHSIG, hence let's emulate the effect */
1604

1605
                ppid = getppid();
32,567✔
1606
                if (ppid == 0)
32,567✔
1607
                        /* Parent is in a different PID namespace. */;
1608
                else if (ppid != original_pid) {
32,530✔
1609
                        int sig = fork_flags_to_signal(flags);
×
1610
                        log_debug("Parent died early, raising %s.", signal_to_string(sig));
×
1611
                        (void) raise(sig);
×
1612
                        _exit(EXIT_FAILURE);
×
1613
                }
1614
        }
1615

1616
        if (FLAGS_SET(flags, FORK_NEW_MOUNTNS | FORK_MOUNTNS_SLAVE)) {
34,007✔
1617
                /* Optionally, make sure we never propagate mounts to the host. */
1618
                if (mount(NULL, "/", NULL, MS_SLAVE | MS_REC, NULL) < 0) {
197✔
1619
                        log_full_errno(prio, errno, "Failed to remount root directory as MS_SLAVE: %m");
×
1620
                        _exit(EXIT_FAILURE);
×
1621
                }
1622
        }
1623

1624
        if (FLAGS_SET(flags, FORK_PRIVATE_TMP)) {
34,007✔
1625
                assert(FLAGS_SET(flags, FORK_NEW_MOUNTNS));
×
1626

1627
                /* Optionally, overmount new tmpfs instance on /tmp/. */
1628
                r = mount_nofollow("tmpfs", "/tmp", "tmpfs",
×
1629
                                   MS_NOSUID|MS_NODEV,
1630
                                   "mode=01777" TMPFS_LIMITS_RUN);
1631
                if (r < 0) {
×
1632
                        log_full_errno(prio, r, "Failed to overmount /tmp/: %m");
×
1633
                        _exit(EXIT_FAILURE);
×
1634
                }
1635
        }
1636

1637
        if (flags & FORK_REARRANGE_STDIO) {
34,007✔
1638
                if (stdio_fds) {
16,743✔
1639
                        r = rearrange_stdio(stdio_fds[0], stdio_fds[1], stdio_fds[2]);
16,727✔
1640
                        if (r < 0) {
16,727✔
1641
                                log_full_errno(prio, r, "Failed to rearrange stdio fds: %m");
×
1642
                                _exit(EXIT_FAILURE);
×
1643
                        }
1644

1645
                        /* Turn off O_NONBLOCK on the fdio fds, in case it was left on */
1646
                        stdio_disable_nonblock();
16,727✔
1647
                } else {
1648
                        r = make_null_stdio();
16✔
1649
                        if (r < 0) {
16✔
1650
                                log_full_errno(prio, r, "Failed to connect stdin/stdout to /dev/null: %m");
×
1651
                                _exit(EXIT_FAILURE);
×
1652
                        }
1653
                }
1654
        } else if (flags & FORK_STDOUT_TO_STDERR) {
17,264✔
1655
                if (dup2(STDERR_FILENO, STDOUT_FILENO) < 0) {
2✔
1656
                        log_full_errno(prio, errno, "Failed to connect stdout to stderr: %m");
×
1657
                        _exit(EXIT_FAILURE);
×
1658
                }
1659
        }
1660

1661
        if (flags & FORK_CLOSE_ALL_FDS) {
34,007✔
1662
                /* Close the logs here in case it got reopened above, as close_all_fds() would close them for us */
1663
                log_close();
26,345✔
1664

1665
                r = close_all_fds(except_fds, n_except_fds);
26,345✔
1666
                if (r < 0) {
26,345✔
1667
                        log_full_errno(prio, r, "Failed to close all file descriptors: %m");
×
1668
                        _exit(EXIT_FAILURE);
×
1669
                }
1670
        }
1671

1672
        if (flags & FORK_PACK_FDS) {
34,007✔
1673
                /* FORK_CLOSE_ALL_FDS ensures that except_fds are the only FDs >= 3 that are
1674
                 * open, this is including the log. This is required by pack_fds, which will
1675
                 * get stuck in an infinite loop of any FDs other than except_fds are open. */
1676
                assert(FLAGS_SET(flags, FORK_CLOSE_ALL_FDS));
142✔
1677

1678
                r = pack_fds(except_fds, n_except_fds);
142✔
1679
                if (r < 0) {
142✔
1680
                        log_full_errno(prio, r, "Failed to pack file descriptors: %m");
×
1681
                        _exit(EXIT_FAILURE);
×
1682
                }
1683
        }
1684

1685
        if (flags & FORK_CLOEXEC_OFF) {
34,007✔
1686
                r = fd_cloexec_many(except_fds, n_except_fds, false);
157✔
1687
                if (r < 0) {
157✔
1688
                        log_full_errno(prio, r, "Failed to turn off O_CLOEXEC on file descriptors: %m");
×
1689
                        _exit(EXIT_FAILURE);
×
1690
                }
1691
        }
1692

1693
        /* When we were asked to reopen the logs, do so again now */
1694
        if (flags & FORK_REOPEN_LOG) {
34,007✔
1695
                log_open();
8,493✔
1696
                log_set_open_when_needed(false);
8,493✔
1697
        }
1698

1699
        if (flags & FORK_RLIMIT_NOFILE_SAFE) {
34,007✔
1700
                r = rlimit_nofile_safe();
17,639✔
1701
                if (r < 0) {
17,639✔
1702
                        log_full_errno(prio, r, "Failed to lower RLIMIT_NOFILE's soft limit to 1K: %m");
×
1703
                        _exit(EXIT_FAILURE);
×
1704
                }
1705
        }
1706

1707
        if (!FLAGS_SET(flags, FORK_KEEP_NOTIFY_SOCKET)) {
34,007✔
1708
                r = RET_NERRNO(unsetenv("NOTIFY_SOCKET"));
34,007✔
1709
                if (r < 0) {
×
1710
                        log_full_errno(prio, r, "Failed to unset $NOTIFY_SOCKET: %m");
×
1711
                        _exit(EXIT_FAILURE);
×
1712
                }
1713
        }
1714

1715
        if (FLAGS_SET(flags, FORK_FREEZE))
34,007✔
1716
                freeze();
×
1717

1718
        if (ret) {
34,007✔
1719
                r = pidref_set_self(ret);
31,757✔
1720
                if (r < 0) {
31,757✔
1721
                        log_full_errno(prio, r, "Failed to acquire PID reference on ourselves: %m");
×
1722
                        _exit(EXIT_FAILURE);
×
1723
                }
1724
        }
1725

1726
        return 0;
1727
}
1728

1729
int namespace_fork_full(
139✔
1730
                const char *outer_name,
1731
                const char *inner_name,
1732
                int except_fds[],
1733
                size_t n_except_fds,
1734
                ForkFlags flags,
1735
                int pidns_fd,
1736
                int mntns_fd,
1737
                int netns_fd,
1738
                int userns_fd,
1739
                int root_fd,
1740
                PidRef *ret) {
1741

1742
        _cleanup_(pidref_done_sigkill_wait) PidRef pidref_outer = PIDREF_NULL;
×
1743
        _cleanup_close_pair_ int errno_pipe_fd[2] = EBADF_PAIR;
235✔
1744
        int r, prio = FLAGS_SET(flags, FORK_LOG) ? LOG_ERR : LOG_DEBUG;
139✔
1745

1746
        /* This is much like safe_fork(), but forks twice, and joins the specified namespaces in the middle
1747
         * process. This ensures that we are fully a member of the destination namespace, with pidns an all, so that
1748
         * /proc/self/fd works correctly.
1749
         *
1750
         * TODO: once we can rely on PIDFD_INFO_EXIT, do not keep the middle process around and instead
1751
         * return the pidfd of the inner process for direct tracking. */
1752

1753
        /* Insist on PDEATHSIG being enabled, as the pid returned is the one of the middle man, and otherwise
1754
         * killing of it won't be propagated to the inner child. */
1755
        assert((flags & (FORK_DEATHSIG_SIGKILL|FORK_DEATHSIG_SIGTERM|FORK_DEATHSIG_SIGINT)) != 0);
139✔
1756
        assert((flags & (FORK_DETACH|FORK_FREEZE)) == 0);
139✔
1757
        assert(!FLAGS_SET(flags, FORK_ALLOW_DLOPEN)); /* never allow loading shared library from another ns */
139✔
1758

1759
        /* We want read() to block as a synchronization point */
1760
        assert_cc(sizeof(int) <= PIPE_BUF);
139✔
1761
        if (pipe2(errno_pipe_fd, O_CLOEXEC) < 0)
139✔
1762
                return log_full_errno(prio, errno, "Failed to create pipe: %m");
×
1763

1764
        r = pidref_safe_fork_full(
373✔
1765
                        outer_name,
1766
                        /* stdio_fds= */ NULL, /* except_fds= */ NULL, /* n_except_fds= */ 0,
1767
                        (flags|FORK_DEATHSIG_SIGKILL) & ~(FORK_DEATHSIG_SIGTERM|FORK_DEATHSIG_SIGINT|FORK_REOPEN_LOG|FORK_NEW_MOUNTNS|FORK_MOUNTNS_SLAVE|FORK_NEW_USERNS|FORK_NEW_NETNS|FORK_NEW_PIDNS|FORK_CLOSE_ALL_FDS|FORK_PACK_FDS|FORK_CLOEXEC_OFF|FORK_RLIMIT_NOFILE_SAFE),
139✔
1768
                        &pidref_outer);
1769
        if (r == -EPROTO && FLAGS_SET(flags, FORK_WAIT)) {
234✔
1770
                errno_pipe_fd[1] = safe_close(errno_pipe_fd[1]);
8✔
1771

1772
                int k = read_errno(errno_pipe_fd[0]);
8✔
1773
                if (k < 0 && k != -EIO)
8✔
1774
                        return k;
1775
        }
1776
        if (r < 0)
234✔
1777
                return r;
1778
        if (r == 0) {
226✔
1779
                _cleanup_(pidref_done) PidRef pidref_inner = PIDREF_NULL;
×
1780

1781
                /* Child */
1782

1783
                errno_pipe_fd[0] = safe_close(errno_pipe_fd[0]);
95✔
1784

1785
                r = namespace_enter(pidns_fd, mntns_fd, netns_fd, userns_fd, root_fd);
95✔
1786
                if (r < 0) {
95✔
1787
                        log_full_errno(prio, r, "Failed to join namespace: %m");
×
1788
                        report_errno_and_exit(errno_pipe_fd[1], r);
×
1789
                }
1790

1791
                /* We mask a few flags here that either make no sense for the grandchild, or that we don't have to do again */
1792
                r = pidref_safe_fork_full(
286✔
1793
                                inner_name,
1794
                                NULL,
1795
                                except_fds, n_except_fds,
1796
                                flags & ~(FORK_WAIT|FORK_RESET_SIGNALS|FORK_REARRANGE_STDIO|FORK_FLUSH_STDIO|FORK_STDOUT_TO_STDERR),
95✔
1797
                                &pidref_inner);
1798
                if (r < 0)
191✔
1799
                        report_errno_and_exit(errno_pipe_fd[1], r);
×
1800
                if (r == 0) {
191✔
1801
                        /* Child */
1802

1803
                        if (!FLAGS_SET(flags, FORK_CLOSE_ALL_FDS)) {
96✔
1804
                                errno_pipe_fd[1] = safe_close(errno_pipe_fd[1]);
93✔
1805
                                pidref_done(&pidref_outer);
93✔
1806
                        } else {
1807
                                errno_pipe_fd[1] = -EBADF;
3✔
1808
                                pidref_outer = PIDREF_NULL;
3✔
1809
                        }
1810

1811
                        if (ret)
96✔
1812
                                *ret = TAKE_PIDREF(pidref_inner);
96✔
1813
                        return 0;
96✔
1814
                }
1815

1816
                log_forget_fds();
95✔
1817
                log_set_open_when_needed(true);
95✔
1818

1819
                (void) close_all_fds(&pidref_inner.fd, 1);
95✔
1820

1821
                r = pidref_wait_for_terminate_and_check(
190✔
1822
                                inner_name,
1823
                                &pidref_inner,
1824
                                FLAGS_SET(flags, FORK_LOG) ? WAIT_LOG : 0);
1825
                if (r < 0)
95✔
1826
                        _exit(EXIT_FAILURE);
×
1827

1828
                _exit(r);
95✔
1829
        }
1830

1831
        errno_pipe_fd[1] = safe_close(errno_pipe_fd[1]);
131✔
1832

1833
        r = read_errno(errno_pipe_fd[0]);
131✔
1834
        if (r < 0)
131✔
1835
                return r; /* the child logs about failures on its own, no need to duplicate here */
1836

1837
        if (ret)
131✔
1838
                *ret = TAKE_PIDREF(pidref_outer);
105✔
1839
        else
1840
                pidref_done(&pidref_outer); /* disarm sigkill_wait */
26✔
1841

1842
        return 1;
1843
}
1844

1845
bool oom_score_adjust_is_valid(int oa) {
7,875✔
1846
        return oa >= OOM_SCORE_ADJ_MIN && oa <= OOM_SCORE_ADJ_MAX;
7,875✔
1847
}
1848

1849
int set_oom_score_adjust(int value) {
4,192✔
1850
        char t[DECIMAL_STR_MAX(int)];
4,192✔
1851

1852
        if (!oom_score_adjust_is_valid(value))
4,192✔
1853
                return -EINVAL;
4,192✔
1854

1855
        xsprintf(t, "%i", value);
4,192✔
1856

1857
        return write_string_file("/proc/self/oom_score_adj", t,
4,192✔
1858
                                 WRITE_STRING_FILE_VERIFY_ON_FAILURE|WRITE_STRING_FILE_DISABLE_BUFFER);
1859
}
1860

1861
int get_oom_score_adjust(int *ret) {
2,767✔
1862
        _cleanup_free_ char *t = NULL;
2,767✔
1863
        int r, a;
2,767✔
1864

1865
        r = read_virtual_file("/proc/self/oom_score_adj", SIZE_MAX, &t, NULL);
2,767✔
1866
        if (r < 0)
2,767✔
1867
                return r;
1868

1869
        delete_trailing_chars(t, WHITESPACE);
2,767✔
1870

1871
        r = safe_atoi(t, &a);
2,767✔
1872
        if (r < 0)
2,767✔
1873
                return r;
1874

1875
        if (!oom_score_adjust_is_valid(a))
2,767✔
1876
                return -ENODATA;
1877

1878
        if (ret)
2,767✔
1879
                *ret = a;
2,767✔
1880

1881
        return 0;
1882
}
1883

1884
static int rlimit_to_nice(rlim_t limit) {
2✔
1885
        if (limit <= 1)
2✔
1886
                return PRIO_MAX-1; /* i.e. 19 */
1887

1888
        if (limit >= -PRIO_MIN + PRIO_MAX)
2✔
1889
                return PRIO_MIN; /* i.e. -20 */
1890

1891
        return PRIO_MAX - (int) limit;
2✔
1892
}
1893

1894
int setpriority_closest(int priority) {
27✔
1895
        struct rlimit highest;
27✔
1896
        int r, current, limit;
27✔
1897

1898
        /* Try to set requested nice level */
1899
        r = RET_NERRNO(setpriority(PRIO_PROCESS, 0, priority));
27✔
1900
        if (r >= 0)
2✔
1901
                return 1;
25✔
1902
        if (!ERRNO_IS_NEG_PRIVILEGE(r))
2✔
1903
                return r;
1904

1905
        errno = 0;
2✔
1906
        current = getpriority(PRIO_PROCESS, 0);
2✔
1907
        if (errno != 0)
2✔
1908
                return -errno;
×
1909

1910
        if (priority == current)
2✔
1911
                return 1;
1912

1913
       /* Hmm, we'd expect that raising the nice level from our status quo would always work. If it doesn't,
1914
        * then the whole setpriority() system call is blocked to us, hence let's propagate the error
1915
        * right-away */
1916
        if (priority > current)
2✔
1917
                return r;
1918

1919
        if (getrlimit(RLIMIT_NICE, &highest) < 0)
2✔
1920
                return -errno;
×
1921

1922
        limit = rlimit_to_nice(highest.rlim_cur);
2✔
1923

1924
        /* Push to the allowed limit if we're higher than that. Note that we could also be less nice than
1925
         * limit allows us, but still higher than what's requested. In that case our current value is
1926
         * the best choice. */
1927
        if (current > limit)
2✔
1928
                if (setpriority(PRIO_PROCESS, 0, limit) < 0)
2✔
1929
                        return -errno;
×
1930

1931
        log_debug("Cannot set requested nice level (%i), using next best (%i).", priority, MIN(current, limit));
2✔
1932
        return 0;
1933
}
1934

1935
_noreturn_ void freeze(void) {
×
1936
        log_close();
×
1937

1938
        /* Make sure nobody waits for us (i.e. on one of our sockets) anymore. Note that we use
1939
         * close_all_fds_without_malloc() instead of plain close_all_fds() here, since we want this function
1940
         * to be compatible with being called from signal handlers. */
1941
        (void) close_all_fds_without_malloc(NULL, 0);
×
1942

1943
        /* Let's not freeze right away, but keep reaping zombies. */
1944
        for (;;) {
×
1945
                siginfo_t si = {};
×
1946

1947
                if (waitid(P_ALL, 0, &si, WEXITED) < 0 && errno != EINTR)
×
1948
                        break;
1949
        }
1950

1951
        /* waitid() failed with an ECHLD error (because there are no left-over child processes) or any other
1952
         * (unexpected) error. Freeze for good now! */
1953
        for (;;)
×
1954
                pause();
×
1955
}
1956

1957
int get_process_threads(pid_t pid) {
7✔
1958
        _cleanup_free_ char *t = NULL;
7✔
1959
        int n, r;
7✔
1960

1961
        if (pid < 0)
7✔
1962
                return -EINVAL;
1963

1964
        r = procfs_file_get_field(pid, "status", "Threads", &t);
7✔
1965
        if (r == -ENOENT)
7✔
1966
                return -ESRCH;
1967
        if (r < 0)
7✔
1968
                return r;
1969

1970
        r = safe_atoi(t, &n);
7✔
1971
        if (r < 0)
7✔
1972
                return r;
1973
        if (n < 0)
7✔
1974
                return -EINVAL;
×
1975

1976
        return n;
1977
}
1978

1979
int is_reaper_process(void) {
5,008✔
1980
        int b = 0;
5,008✔
1981

1982
        /* Checks if we are running in a reaper process, i.e. if we are expected to deal with processes
1983
         * reparented to us. This simply checks if we are PID 1 or if PR_SET_CHILD_SUBREAPER was called. */
1984

1985
        if (getpid_cached() == 1)
5,008✔
1986
                return true;
5,008✔
1987

1988
        if (prctl(PR_GET_CHILD_SUBREAPER, (unsigned long) &b, 0UL, 0UL, 0UL) < 0)
372✔
1989
                return -errno;
×
1990

1991
        return b != 0;
372✔
1992
}
1993

1994
int make_reaper_process(bool b) {
710✔
1995

1996
        if (getpid_cached() == 1) {
710✔
1997

1998
                if (!b)
68✔
1999
                        return -EINVAL;
2000

2001
                return 0;
68✔
2002
        }
2003

2004
        /* Some prctl()s insist that all 5 arguments are specified, others do not. Let's always specify all,
2005
         * to avoid any ambiguities */
2006
        if (prctl(PR_SET_CHILD_SUBREAPER, (unsigned long) b, 0UL, 0UL, 0UL) < 0)
642✔
2007
                return -errno;
×
2008

2009
        return 0;
2010
}
2011

2012
DEFINE_TRIVIAL_CLEANUP_FUNC_FULL(posix_spawnattr_t*, posix_spawnattr_destroy, NULL);
×
2013

2014
int posix_spawn_wrapper(
3,187✔
2015
                const char *path,
2016
                char * const *argv,
2017
                char * const *envp,
2018
                const char *cgroup,
2019
                PidRef *ret_pidref) {
2020

2021
        short flags = POSIX_SPAWN_SETSIGMASK;
3,187✔
2022
        posix_spawnattr_t attr;
3,187✔
2023
        sigset_t mask;
3,187✔
2024
        int r;
3,187✔
2025

2026
        /* Forks and invokes 'path' with 'argv' and 'envp' using CLONE_VM and CLONE_VFORK, which means the
2027
         * caller will be blocked until the child either exits or exec's. The memory of the child will be
2028
         * fully shared with the memory of the parent, so that there are no copy-on-write or memory.max
2029
         * issues.
2030
         *
2031
         * Also, move the newly-created process into 'cgroup' through POSIX_SPAWN_SETCGROUP (clone3())
2032
         * if available.
2033
         * returns 1: We're already in the right cgroup
2034
         *         0: 'cgroup' not specified or POSIX_SPAWN_SETCGROUP is not supported. The caller
2035
         *            needs to call 'cg_attach' on their own */
2036

2037
        assert(path);
3,187✔
2038
        assert(argv);
3,187✔
2039
        assert(ret_pidref);
3,187✔
2040

2041
        assert_se(sigfillset(&mask) >= 0);
3,187✔
2042

2043
        r = posix_spawnattr_init(&attr);
3,187✔
2044
        if (r != 0)
3,187✔
2045
                return -r; /* These functions return a positive errno on failure */
3,187✔
2046

2047
        /* Initialization needs to succeed before we can set up a destructor. */
2048
        _unused_ _cleanup_(posix_spawnattr_destroyp) posix_spawnattr_t *attr_destructor = &attr;
6,374✔
2049

2050
#if HAVE_PIDFD_SPAWN
2051
        static bool have_clone_into_cgroup = true; /* kernel 5.7+ */
3,187✔
2052
        _cleanup_close_ int cgroup_fd = -EBADF;
3,187✔
2053

2054
        if (cgroup && have_clone_into_cgroup) {
3,187✔
2055
                _cleanup_free_ char *resolved_cgroup = NULL;
3,187✔
2056

2057
                r = cg_get_path(cgroup, /* suffix= */ NULL, &resolved_cgroup);
3,187✔
2058
                if (r < 0)
3,187✔
2059
                        return r;
2060

2061
                cgroup_fd = open(resolved_cgroup, O_PATH|O_DIRECTORY|O_CLOEXEC);
3,187✔
2062
                if (cgroup_fd < 0)
3,187✔
2063
                        return -errno;
×
2064

2065
                r = posix_spawnattr_setcgroup_np(&attr, cgroup_fd);
3,187✔
2066
                if (r != 0)
3,187✔
2067
                        return -r;
×
2068

2069
                flags |= POSIX_SPAWN_SETCGROUP;
3,187✔
2070
        }
2071
#endif
2072

2073
        r = posix_spawnattr_setflags(&attr, flags);
3,187✔
2074
        if (r != 0)
3,187✔
2075
                return -r;
×
2076
        r = posix_spawnattr_setsigmask(&attr, &mask);
3,187✔
2077
        if (r != 0)
3,187✔
2078
                return -r;
×
2079

2080
#if HAVE_PIDFD_SPAWN
2081
        _cleanup_close_ int pidfd = -EBADF;
3,187✔
2082

2083
        r = pidfd_spawn(&pidfd, path, NULL, &attr, argv, envp);
3,187✔
2084
        if (ERRNO_IS_NOT_SUPPORTED(r) && FLAGS_SET(flags, POSIX_SPAWN_SETCGROUP) && cg_is_threaded(cgroup) > 0)
3,187✔
2085
                return -EUCLEAN; /* clone3() could also return EOPNOTSUPP if the target cgroup is in threaded mode,
2086
                                    turn that into something recognizable */
2087
        if ((ERRNO_IS_NOT_SUPPORTED(r) || ERRNO_IS_PRIVILEGE(r)) &&
3,187✔
2088
            FLAGS_SET(flags, POSIX_SPAWN_SETCGROUP)) {
2089
                /* Compiled on a newer host, or seccomp&friends blocking clone3()? Fallback, but
2090
                 * need to disable POSIX_SPAWN_SETCGROUP, which is what redirects to clone3().
2091
                 * CLONE_INTO_CGROUP definitely won't work, hence remember the fact so that we don't
2092
                 * retry every time.
2093
                 * Note, CLONE_INTO_CGROUP is supported since kernel v5.7, but some architectures still
2094
                 * do not support clone3(). Hence, we need to keep the fallback logic for a while. */
2095
                have_clone_into_cgroup = false;
×
2096

2097
                flags &= ~POSIX_SPAWN_SETCGROUP;
×
2098
                r = posix_spawnattr_setflags(&attr, flags);
×
2099
                if (r != 0)
×
2100
                        return -r;
×
2101

2102
                r = pidfd_spawn(&pidfd, path, NULL, &attr, argv, envp);
×
2103
        }
2104
        if (r != 0)
3,187✔
2105
                return -r;
×
2106

2107
        r = pidref_set_pidfd_consume(ret_pidref, TAKE_FD(pidfd));
3,187✔
2108
        if (r < 0)
3,187✔
2109
                return r;
2110

2111
        return FLAGS_SET(flags, POSIX_SPAWN_SETCGROUP);
3,187✔
2112
#else
2113
        pid_t pid;
2114

2115
        r = posix_spawn(&pid, path, NULL, &attr, argv, envp);
2116
        if (r != 0)
2117
                return -r;
2118

2119
        r = pidref_set_pid(ret_pidref, pid);
2120
        if (r < 0)
2121
                return r;
2122

2123
        return 0; /* We did not use CLONE_INTO_CGROUP so return 0, the caller will have to move the child */
2124
#endif
2125
}
2126

2127
int proc_dir_open(DIR **ret) {
13✔
2128
        DIR *d;
13✔
2129

2130
        assert(ret);
13✔
2131

2132
        d = opendir("/proc");
13✔
2133
        if (!d)
13✔
2134
                return -errno;
×
2135

2136
        *ret = d;
13✔
2137
        return 0;
13✔
2138
}
2139

2140
int proc_dir_read(DIR *d, pid_t *ret) {
1,207✔
2141
        assert(d);
1,207✔
2142

2143
        for (;;) {
1,991✔
2144
                struct dirent *de;
1,991✔
2145

2146
                errno = 0;
1,991✔
2147
                de = readdir_no_dot(d);
1,991✔
2148
                if (!de) {
1,991✔
2149
                        if (errno != 0)
13✔
2150
                                return -errno;
×
2151

2152
                        break;
13✔
2153
                }
2154

2155
                if (!IN_SET(de->d_type, DT_DIR, DT_UNKNOWN))
1,978✔
2156
                        continue;
641✔
2157

2158
                if (parse_pid(de->d_name, ret) >= 0)
1,337✔
2159
                        return 1;
2160
        }
2161

2162
        if (ret)
13✔
2163
                *ret = 0;
13✔
2164
        return 0;
2165
}
2166

2167
int proc_dir_read_pidref(DIR *d, PidRef *ret) {
1,165✔
2168
        int r;
1,165✔
2169

2170
        assert(d);
1,165✔
2171

2172
        for (;;) {
1,165✔
2173
                pid_t pid;
1,165✔
2174

2175
                r = proc_dir_read(d, &pid);
1,165✔
2176
                if (r < 0)
1,165✔
2177
                        return r;
1,153✔
2178
                if (r == 0)
1,165✔
2179
                        break;
2180

2181
                r = pidref_set_pid(ret, pid);
1,153✔
2182
                if (r == -ESRCH) /* gone by now? skip it */
1,153✔
2183
                        continue;
×
2184
                if (r < 0)
1,153✔
2185
                        return r;
×
2186

2187
                return 1;
2188
        }
2189

2190
        if (ret)
12✔
2191
                *ret = PIDREF_NULL;
12✔
2192
        return 0;
2193
}
2194

2195
int safe_mlockall(int flags) {
162✔
2196
        int r;
162✔
2197

2198
        /* When dealing with sensitive data, let's lock ourselves into memory. We do this only when
2199
         * privileged however, as otherwise the amount of lockable memory that RLIMIT_MEMLOCK grants us is
2200
         * frequently too low to make this work. The resource limit has no effect on CAP_IPC_LOCK processes,
2201
         * hence that's the capability we check for. */
2202
        r = have_effective_cap(CAP_IPC_LOCK);
162✔
2203
        if (r < 0)
162✔
2204
                return log_debug_errno(r, "Failed to determine if we have CAP_IPC_LOCK: %m");
×
2205
        if (r == 0)
162✔
2206
                return log_debug_errno(SYNTHETIC_ERRNO(EPERM), "Lacking CAP_IPC_LOCK, skipping mlockall().");
×
2207

2208
        if (mlockall(flags) < 0)
162✔
2209
                return log_debug_errno(errno, "Failed to call mlockall(): %m");
×
2210

2211
        log_debug("Successfully called mlockall().");
162✔
2212
        return 0;
2213
}
2214

2215
static const char *const sigchld_code_table[] = {
2216
        [CLD_EXITED] = "exited",
2217
        [CLD_KILLED] = "killed",
2218
        [CLD_DUMPED] = "dumped",
2219
        [CLD_TRAPPED] = "trapped",
2220
        [CLD_STOPPED] = "stopped",
2221
        [CLD_CONTINUED] = "continued",
2222
};
2223

2224
DEFINE_STRING_TABLE_LOOKUP(sigchld_code, int);
11,732✔
2225

2226
static const char* const sched_policy_table[] = {
2227
        [SCHED_OTHER] = "other",
2228
        [SCHED_BATCH] = "batch",
2229
        [SCHED_IDLE]  = "idle",
2230
        [SCHED_FIFO]  = "fifo",
2231
        [SCHED_EXT]   = "ext",
2232
        [SCHED_RR]    = "rr",
2233
};
2234

2235
DEFINE_STRING_TABLE_LOOKUP_WITH_FALLBACK(sched_policy, int, INT_MAX);
66✔
2236

2237
_noreturn_ void report_errno_and_exit(int errno_fd, int error) {
216✔
2238
        int r;
216✔
2239

2240
        if (error >= 0)
216✔
2241
                _exit(EXIT_SUCCESS);
215✔
2242

2243
        assert(errno_fd >= 0);
1✔
2244

2245
        r = loop_write(errno_fd, &error, sizeof(error));
1✔
2246
        if (r < 0)
1✔
2247
                log_debug_errno(r, "Failed to write errno to errno_fd=%d: %m", errno_fd);
×
2248

2249
        _exit(EXIT_FAILURE);
1✔
2250
}
2251

2252
int read_errno(int errno_fd) {
150✔
2253
        int r;
150✔
2254

2255
        assert(errno_fd >= 0);
150✔
2256

2257
        /* The issue here is that it's impossible to distinguish between an error code returned by child and
2258
         * IO error arose when reading it. So, the function logs errors and return EIO for the later case. */
2259

2260
        ssize_t n = loop_read(errno_fd, &r, sizeof(r), /* do_poll= */ false);
150✔
2261
        if (n < 0) {
150✔
2262
                log_debug_errno(n, "Failed to read errno: %m");
×
2263
                return -EIO;
×
2264
        }
2265
        if (n == 0) /* the process exited without reporting an error, assuming success */
150✔
2266
                return 0;
2267
        if (n != sizeof(r))
8✔
2268
                return log_debug_errno(SYNTHETIC_ERRNO(EIO), "Received unexpected amount of bytes (%zi) while reading errno.", n);
×
2269

2270
        if (r == 0)
8✔
2271
                return 0;
2272
        if (r < 0) /* child process reported an error, return it */
8✔
2273
                return log_debug_errno(r, "Child process failed with errno: %m");
8✔
2274

2275
        return log_debug_errno(SYNTHETIC_ERRNO(EIO), "Received positive errno from child, refusing: %d", r);
×
2276
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc