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

systemd / systemd / 20417900562

21 Dec 2025 07:31PM UTC coverage: 72.5% (-0.2%) from 72.701%
20417900562

push

github

DaanDeMeyer
mkosi: Use initrd as exitrd

Let's speed up image builds by avoiding building
an exitrd and instead reusing the initrd image for
the same purpose.

309142 of 426400 relevant lines covered (72.5%)

1141502.27 hits per line

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

84.55
/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/mount.h>
8
#include <sys/personality.h>
9
#include <sys/prctl.h>
10
#include <sys/wait.h>
11
#include <syslog.h>
12
#include <threads.h>
13
#include <unistd.h>
14
#if HAVE_VALGRIND_VALGRIND_H
15
#include <valgrind/valgrind.h>
16
#endif
17

18
#include "sd-messages.h"
19

20
#include "alloc-util.h"
21
#include "architecture.h"
22
#include "argv-util.h"
23
#include "cgroup-util.h"
24
#include "dirent-util.h"
25
#include "dlfcn-util.h"
26
#include "env-file.h"
27
#include "errno-util.h"
28
#include "escape.h"
29
#include "fd-util.h"
30
#include "fileio.h"
31
#include "fs-util.h"
32
#include "hostname-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 "time-util.h"
55
#include "user-util.h"
56

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

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

68
        assert(pid >= 0);
13,067✔
69

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

74
        p = procfs_file_alloca(pid, "stat");
13,067✔
75

76
        r = read_one_line_file(p, &line);
13,067✔
77
        if (r == -ENOENT)
13,067✔
78
                return -ESRCH;
79
        if (r < 0)
10,243✔
80
                return r;
81

82
        p = strrchr(line, ')');
10,242✔
83
        if (!p)
10,242✔
84
                return -EIO;
85

86
        p++;
10,242✔
87

88
        if (sscanf(p, " %c", &state) != 1)
10,242✔
89
                return -EIO;
90

91
        return (unsigned char) state;
10,242✔
92
}
93

94
int pid_get_comm(pid_t pid, char **ret) {
43,098✔
95
        _cleanup_free_ char *escaped = NULL, *comm = NULL;
43,098✔
96
        int r;
43,098✔
97

98
        assert(pid >= 0);
43,098✔
99
        assert(ret);
43,098✔
100

101
        if (pid == 0 || pid == getpid_cached()) {
43,098✔
102
                comm = new0(char, TASK_COMM_LEN + 1); /* Must fit in 16 byte according to prctl(2) */
23,836✔
103
                if (!comm)
23,836✔
104
                        return -ENOMEM;
105

106
                if (prctl(PR_GET_NAME, comm) < 0)
23,836✔
107
                        return -errno;
×
108
        } else {
109
                const char *p;
19,262✔
110

111
                p = procfs_file_alloca(pid, "comm");
19,262✔
112

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

121
        escaped = new(char, COMM_MAX_LEN);
39,741✔
122
        if (!escaped)
39,741✔
123
                return -ENOMEM;
124

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

128
        *ret = TAKE_PTR(escaped);
39,741✔
129
        return 0;
39,741✔
130
}
131

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

136
        if (!pidref_is_set(pid))
156✔
137
                return -ESRCH;
138

139
        if (pidref_is_remote(pid))
312✔
140
                return -EREMOTE;
141

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

146
        r = pidref_verify(pid);
156✔
147
        if (r < 0)
156✔
148
                return r;
149

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

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

162
        _cleanup_free_ char *t = NULL;
18,170✔
163
        const char *p;
18,170✔
164
        size_t k;
18,170✔
165
        int r;
18,170✔
166

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

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

186
        if (k == 0) {
14,733✔
187
                if (!(flags & PROCESS_CMDLINE_COMM_FALLBACK))
326✔
188
                        return -ENOENT;
307✔
189

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

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

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

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

208
        if (ret)
14,426✔
209
                *ret = TAKE_PTR(t);
14,426✔
210
        if (ret_size)
14,426✔
211
                *ret_size = k;
14,426✔
212

213
        return r;
214
}
215

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

221
        assert(pid >= 0);
13,506✔
222
        assert(ret);
13,506✔
223

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

238
        int full = pid_get_cmdline_nulstr(pid, max_columns, flags, &t, &k);
13,506✔
239
        if (full < 0)
13,506✔
240
                return full;
241

242
        if (flags & (PROCESS_CMDLINE_QUOTE | PROCESS_CMDLINE_QUOTE_POSIX)) {
9,838✔
243
                ShellEscapeFlags shflags = SHELL_ESCAPE_EMPTY |
9,469✔
244
                        FLAGS_SET(flags, PROCESS_CMDLINE_QUOTE_POSIX) * SHELL_ESCAPE_POSIX;
9,469✔
245

246
                assert(!(flags & PROCESS_CMDLINE_USE_LOCALE));
9,469✔
247

248
                _cleanup_strv_free_ char **args = NULL;
9,469✔
249

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

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

265
                delete_trailing_chars(t, WHITESPACE);
369✔
266

267
                bool eight_bit = (flags & PROCESS_CMDLINE_USE_LOCALE) && !is_locale_utf8();
369✔
268

269
                ans = escape_non_printable_full(t, max_columns,
1,107✔
270
                                                eight_bit * XESCAPE_8_BIT | !full * XESCAPE_FORCE_ELLIPSIS);
684✔
271
                if (!ans)
369✔
272
                        return -ENOMEM;
273

274
                ans = str_realloc(ans);
369✔
275
        }
276

277
        *ret = ans;
9,838✔
278
        return 0;
9,838✔
279
}
280

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

285
        if (!pidref_is_set(pid))
46✔
286
                return -ESRCH;
287

288
        if (pidref_is_remote(pid))
92✔
289
                return -EREMOTE;
290

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

295
        r = pidref_verify(pid);
46✔
296
        if (r < 0)
46✔
297
                return r;
298

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

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

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

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

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

322
        *ret = args;
4,588✔
323
        return 0;
4,588✔
324
}
325

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

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

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

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

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

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

347
        return 0;
348
}
349

350
int container_get_leader(const char *machine, pid_t *pid) {
35✔
351
        _cleanup_free_ char *s = NULL, *class = NULL;
35✔
352
        const char *p;
35✔
353
        pid_t leader;
35✔
354
        int r;
35✔
355

356
        assert(machine);
35✔
357
        assert(pid);
35✔
358

359
        if (streq(machine, ".host")) {
35✔
360
                *pid = 1;
1✔
361
                return 0;
1✔
362
        }
363

364
        if (!hostname_is_valid(machine, 0))
34✔
365
                return -EINVAL;
366

367
        p = strjoina("/run/systemd/machines/", machine);
170✔
368
        r = parse_env_file(NULL, p,
34✔
369
                           "LEADER", &s,
370
                           "CLASS", &class);
371
        if (r == -ENOENT)
34✔
372
                return -EHOSTDOWN;
373
        if (r < 0)
34✔
374
                return r;
375
        if (!s)
34✔
376
                return -EIO;
377

378
        if (!streq_ptr(class, "container"))
34✔
379
                return -EIO;
380

381
        r = parse_pid(s, &leader);
34✔
382
        if (r < 0)
34✔
383
                return r;
384
        if (leader <= 1)
34✔
385
                return -EIO;
386

387
        *pid = leader;
34✔
388
        return 0;
34✔
389
}
390

391
int pid_is_kernel_thread(pid_t pid) {
3,869✔
392
        _cleanup_free_ char *line = NULL;
3,869✔
393
        unsigned long long flags;
3,869✔
394
        size_t l, i;
3,869✔
395
        const char *p;
3,869✔
396
        char *q;
3,869✔
397
        int r;
3,869✔
398

399
        if (IN_SET(pid, 0, 1) || pid == getpid_cached()) /* pid 1, and we ourselves certainly aren't a kernel thread */
3,869✔
400
                return 0;
25✔
401
        if (!pid_is_valid(pid))
3,844✔
402
                return -EINVAL;
403

404
        p = procfs_file_alloca(pid, "stat");
3,844✔
405
        r = read_one_line_file(p, &line);
3,844✔
406
        if (r == -ENOENT)
3,844✔
407
                return -ESRCH;
408
        if (r < 0)
3,844✔
409
                return r;
410

411
        /* Skip past the comm field */
412
        q = strrchr(line, ')');
3,844✔
413
        if (!q)
3,844✔
414
                return -EINVAL;
415
        q++;
3,844✔
416

417
        /* Skip 6 fields to reach the flags field */
418
        for (i = 0; i < 6; i++) {
26,908✔
419
                l = strspn(q, WHITESPACE);
23,064✔
420
                if (l < 1)
23,064✔
421
                        return -EINVAL;
422
                q += l;
23,064✔
423

424
                l = strcspn(q, WHITESPACE);
23,064✔
425
                if (l < 1)
23,064✔
426
                        return -EINVAL;
427
                q += l;
23,064✔
428
        }
429

430
        /* Skip preceding whitespace */
431
        l = strspn(q, WHITESPACE);
3,844✔
432
        if (l < 1)
3,844✔
433
                return -EINVAL;
434
        q += l;
3,844✔
435

436
        /* Truncate the rest */
437
        l = strcspn(q, WHITESPACE);
3,844✔
438
        if (l < 1)
3,844✔
439
                return -EINVAL;
440
        q[l] = 0;
3,844✔
441

442
        r = safe_atollu(q, &flags);
3,844✔
443
        if (r < 0)
3,844✔
444
                return r;
445

446
        return !!(flags & PF_KTHREAD);
3,844✔
447
}
448

449
int pidref_is_kernel_thread(const PidRef *pid) {
1,618✔
450
        int result, r;
1,618✔
451

452
        if (!pidref_is_set(pid))
1,618✔
453
                return -ESRCH;
454

455
        if (pidref_is_remote(pid))
1,618✔
456
                return -EREMOTE;
457

458
        result = pid_is_kernel_thread(pid->pid);
1,618✔
459
        if (result < 0)
1,618✔
460
                return result;
461

462
        r = pidref_verify(pid); /* Verify that the PID wasn't reused since */
1,618✔
463
        if (r < 0)
1,618✔
464
                return r;
×
465

466
        return result;
467
}
468

469
static int get_process_link_contents(pid_t pid, const char *proc_file, char **ret) {
12,978✔
470
        const char *p;
12,978✔
471
        int r;
12,978✔
472

473
        assert(proc_file);
12,978✔
474

475
        p = procfs_file_alloca(pid, proc_file);
12,982✔
476

477
        r = readlink_malloc(p, ret);
12,978✔
478
        return (r == -ENOENT && proc_mounted() > 0) ? -ESRCH : r;
12,978✔
479
}
480

481
int get_process_exe(pid_t pid, char **ret) {
12,952✔
482
        char *d;
12,952✔
483
        int r;
12,952✔
484

485
        assert(pid >= 0);
12,952✔
486

487
        r = get_process_link_contents(pid, "exe", ret);
12,952✔
488
        if (r < 0)
12,952✔
489
                return r;
490

491
        if (ret) {
9,420✔
492
                d = endswith(*ret, " (deleted)");
9,420✔
493
                if (d)
9,420✔
494
                        *d = '\0';
×
495
        }
496

497
        return 0;
498
}
499

500
int pid_get_uid(pid_t pid, uid_t *ret) {
3,001✔
501
        int r;
3,001✔
502

503
        assert(pid >= 0);
3,001✔
504
        assert(ret);
3,001✔
505

506
        if (pid == 0 || pid == getpid_cached()) {
3,001✔
507
                *ret = getuid();
2✔
508
                return 0;
3,001✔
509
        }
510

511
        _cleanup_free_ char *v = NULL;
2,999✔
512
        r = procfs_file_get_field(pid, "status", "Uid", &v);
2,999✔
513
        if (r == -ENOENT)
2,999✔
514
                return -ESRCH;
515
        if (r < 0)
171✔
516
                return r;
517

518
        return parse_uid(v, ret);
171✔
519
}
520

521
int pidref_get_uid(const PidRef *pid, uid_t *ret) {
69✔
522
        int r;
69✔
523

524
        if (!pidref_is_set(pid))
69✔
525
                return -ESRCH;
69✔
526

527
        if (pidref_is_remote(pid))
69✔
528
                return -EREMOTE;
529

530
        if (pid->fd >= 0) {
69✔
531
                r = pidfd_get_uid(pid->fd, ret);
69✔
532
                if (!ERRNO_IS_NEG_NOT_SUPPORTED(r))
69✔
533
                        return r;
534
        }
535

536
        uid_t uid;
3✔
537
        r = pid_get_uid(pid->pid, &uid);
3✔
538
        if (r < 0)
3✔
539
                return r;
540

541
        r = pidref_verify(pid);
3✔
542
        if (r < 0)
3✔
543
                return r;
544

545
        if (ret)
3✔
546
                *ret = uid;
3✔
547
        return 0;
548
}
549

550
int get_process_gid(pid_t pid, gid_t *ret) {
2,998✔
551
        int r;
2,998✔
552

553
        assert(pid >= 0);
2,998✔
554
        assert(ret);
2,998✔
555

556
        if (pid == 0 || pid == getpid_cached()) {
2,998✔
557
                *ret = getgid();
1✔
558
                return 0;
2,998✔
559
        }
560

561
        _cleanup_free_ char *v = NULL;
2,997✔
562
        r = procfs_file_get_field(pid, "status", "Gid", &v);
2,997✔
563
        if (r == -ENOENT)
2,997✔
564
                return -ESRCH;
565
        if (r < 0)
169✔
566
                return r;
567

568
        return parse_gid(v, ret);
2,997✔
569
}
570

571
int get_process_cwd(pid_t pid, char **ret) {
13✔
572
        assert(pid >= 0);
13✔
573

574
        if (pid == 0 || pid == getpid_cached())
13✔
575
                return safe_getcwd(ret);
×
576

577
        return get_process_link_contents(pid, "cwd", ret);
13✔
578
}
579

580
int get_process_root(pid_t pid, char **ret) {
13✔
581
        assert(pid >= 0);
13✔
582
        return get_process_link_contents(pid, "root", ret);
13✔
583
}
584

585
#define ENVIRONMENT_BLOCK_MAX (5U*1024U*1024U)
586

587
int get_process_environ(pid_t pid, char **ret) {
15✔
588
        _cleanup_fclose_ FILE *f = NULL;
15✔
589
        _cleanup_free_ char *outcome = NULL;
15✔
590
        size_t sz = 0;
15✔
591
        const char *p;
15✔
592
        int r;
15✔
593

594
        assert(pid >= 0);
15✔
595
        assert(ret);
15✔
596

597
        p = procfs_file_alloca(pid, "environ");
15✔
598

599
        r = fopen_unlocked(p, "re", &f);
15✔
600
        if (r == -ENOENT)
15✔
601
                return -ESRCH;
602
        if (r < 0)
15✔
603
                return r;
604

605
        for (;;) {
6,419✔
606
                char c;
6,434✔
607

608
                if (sz >= ENVIRONMENT_BLOCK_MAX)
6,434✔
609
                        return -ENOBUFS;
×
610

611
                if (!GREEDY_REALLOC(outcome, sz + 5))
6,434✔
612
                        return -ENOMEM;
613

614
                r = safe_fgetc(f, &c);
6,434✔
615
                if (r < 0)
6,434✔
616
                        return r;
617
                if (r == 0)
6,434✔
618
                        break;
619

620
                if (c == '\0')
6,419✔
621
                        outcome[sz++] = '\n';
228✔
622
                else
623
                        sz += cescape_char(c, outcome + sz);
6,191✔
624
        }
625

626
        outcome[sz] = '\0';
15✔
627
        *ret = TAKE_PTR(outcome);
15✔
628

629
        return 0;
15✔
630
}
631

632
int pid_get_ppid(pid_t pid, pid_t *ret) {
1,526✔
633
        _cleanup_free_ char *line = NULL;
1,526✔
634
        unsigned long ppid;
1,526✔
635
        const char *p;
1,526✔
636
        int r;
1,526✔
637

638
        assert(pid >= 0);
1,526✔
639

640
        if (pid == 0)
1,526✔
641
                pid = getpid_cached();
1✔
642
        if (pid == 1) /* PID 1 has no parent, shortcut this case */
1,526✔
643
                return -EADDRNOTAVAIL;
644

645
        if (pid == getpid_cached()) {
1,522✔
646
                if (ret)
6✔
647
                        *ret = getppid();
6✔
648
                return 0;
6✔
649
        }
650

651
        p = procfs_file_alloca(pid, "stat");
1,516✔
652
        r = read_one_line_file(p, &line);
1,516✔
653
        if (r == -ENOENT)
1,516✔
654
                return -ESRCH;
655
        if (r < 0)
1,515✔
656
                return r;
657

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

661
        p = strrchr(line, ')');
1,515✔
662
        if (!p)
1,515✔
663
                return -EIO;
664
        p++;
1,515✔
665

666
        if (sscanf(p, " "
1,515✔
667
                   "%*c "  /* state */
668
                   "%lu ", /* ppid */
669
                   &ppid) != 1)
670
                return -EIO;
671

672
        /* If ppid is zero the process has no parent. Which might be the case for PID 1 (caught above)
673
         * but also for processes originating in other namespaces that are inserted into a pidns.
674
         * Return a recognizable error in this case. */
675
        if (ppid == 0)
1,515✔
676
                return -EADDRNOTAVAIL;
677

678
        if ((pid_t) ppid < 0 || (unsigned long) (pid_t) ppid != ppid)
1,515✔
679
                return -ERANGE;
680

681
        if (ret)
1,515✔
682
                *ret = (pid_t) ppid;
1,515✔
683

684
        return 0;
685
}
686

687
int pidref_get_ppid(const PidRef *pidref, pid_t *ret) {
2,468✔
688
        int r;
2,468✔
689

690
        if (!pidref_is_set(pidref))
2,468✔
691
                return -ESRCH;
2,468✔
692

693
        if (pidref_is_remote(pidref))
2,468✔
694
                return -EREMOTE;
695

696
        if (pidref->fd >= 0) {
2,468✔
697
                r = pidfd_get_ppid(pidref->fd, ret);
2,468✔
698
                if (!ERRNO_IS_NEG_NOT_SUPPORTED(r))
2,468✔
699
                        return r;
700
        }
701

702
        pid_t ppid;
1,520✔
703
        r = pid_get_ppid(pidref->pid, ret ? &ppid : NULL);
1,520✔
704
        if (r < 0)
1,520✔
705
                return r;
706

707
        r = pidref_verify(pidref);
1,519✔
708
        if (r < 0)
1,519✔
709
                return r;
710

711
        if (ret)
1,519✔
712
                *ret = ppid;
1,519✔
713
        return 0;
714
}
715

716
int pidref_get_ppid_as_pidref(const PidRef *pidref, PidRef *ret) {
11✔
717
        pid_t ppid;
11✔
718
        int r;
11✔
719

720
        assert(ret);
11✔
721

722
        r = pidref_get_ppid(pidref, &ppid);
11✔
723
        if (r < 0)
11✔
724
                return r;
11✔
725

726
        for (unsigned attempt = 0; attempt < 16; attempt++) {
10✔
727
                _cleanup_(pidref_done) PidRef parent = PIDREF_NULL;
10✔
728

729
                r = pidref_set_pid(&parent, ppid);
10✔
730
                if (r < 0)
10✔
731
                        return r;
732

733
                /* If we have a pidfd of the original PID, let's verify that the process we acquired really
734
                 * is the parent still */
735
                if (pidref->fd >= 0) {
10✔
736
                        r = pidref_get_ppid(pidref, &ppid);
10✔
737
                        if (r < 0)
10✔
738
                                return r;
739

740
                        /* Did the PPID change since we queried it? if so we might have pinned the wrong
741
                         * process, if its PID got reused by now. Let's try again */
742
                        if (parent.pid != ppid)
10✔
743
                                continue;
×
744
                }
745

746
                *ret = TAKE_PIDREF(parent);
10✔
747
                return 0;
10✔
748
        }
749

750
        /* Give up after 16 tries */
751
        return -ENOTRECOVERABLE;
752
}
753

754
int pid_get_start_time(pid_t pid, usec_t *ret) {
579✔
755
        _cleanup_free_ char *line = NULL;
579✔
756
        const char *p;
579✔
757
        int r;
579✔
758

759
        assert(pid >= 0);
579✔
760

761
        p = procfs_file_alloca(pid, "stat");
579✔
762
        r = read_one_line_file(p, &line);
579✔
763
        if (r == -ENOENT)
579✔
764
                return -ESRCH;
765
        if (r < 0)
579✔
766
                return r;
767

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

771
        p = strrchr(line, ')');
579✔
772
        if (!p)
579✔
773
                return -EIO;
774
        p++;
579✔
775

776
        unsigned long llu;
579✔
777

778
        if (sscanf(p, " "
579✔
779
                   "%*c " /* state */
780
                   "%*u " /* ppid */
781
                   "%*u " /* pgrp */
782
                   "%*u " /* session */
783
                   "%*u " /* tty_nr */
784
                   "%*u " /* tpgid */
785
                   "%*u " /* flags */
786
                   "%*u " /* minflt */
787
                   "%*u " /* cminflt */
788
                   "%*u " /* majflt */
789
                   "%*u " /* cmajflt */
790
                   "%*u " /* utime */
791
                   "%*u " /* stime */
792
                   "%*u " /* cutime */
793
                   "%*u " /* cstime */
794
                   "%*i " /* priority */
795
                   "%*i " /* nice */
796
                   "%*u " /* num_threads */
797
                   "%*u " /* itrealvalue */
798
                   "%lu ", /* starttime */
799
                   &llu) != 1)
800
                return -EIO;
801

802
        if (ret)
579✔
803
                *ret = jiffies_to_usec(llu); /* CLOCK_BOOTTIME */
579✔
804

805
        return 0;
806
}
807

808
int pidref_get_start_time(const PidRef *pid, usec_t *ret) {
579✔
809
        usec_t t;
579✔
810
        int r;
579✔
811

812
        if (!pidref_is_set(pid))
579✔
813
                return -ESRCH;
579✔
814

815
        if (pidref_is_remote(pid))
579✔
816
                return -EREMOTE;
817

818
        r = pid_get_start_time(pid->pid, ret ? &t : NULL);
579✔
819
        if (r < 0)
579✔
820
                return r;
821

822
        r = pidref_verify(pid);
579✔
823
        if (r < 0)
579✔
824
                return r;
825

826
        if (ret)
579✔
827
                *ret = t;
579✔
828

829
        return 0;
830
}
831

832
int get_process_umask(pid_t pid, mode_t *ret) {
21,985✔
833
        _cleanup_free_ char *m = NULL;
21,985✔
834
        int r;
21,985✔
835

836
        assert(pid >= 0);
21,985✔
837
        assert(ret);
21,985✔
838

839
        r = procfs_file_get_field(pid, "status", "Umask", &m);
21,985✔
840
        if (r == -ENOENT)
21,985✔
841
                return -ESRCH;
842
        if (r < 0)
21,985✔
843
                return r;
844

845
        return parse_mode(m, ret);
21,985✔
846
}
847

848
/*
849
 * Return values:
850
 * < 0 : pidref_wait_for_terminate() failed to get the state of the
851
 *       process, the process was terminated by a signal, or
852
 *       failed for an unknown reason.
853
 * >=0 : The process terminated normally, and its exit code is
854
 *       returned.
855
 *
856
 * That is, success is indicated by a return value of zero, and an
857
 * error is indicated by a non-zero value.
858
 *
859
 * A warning is emitted if the process terminates abnormally,
860
 * and also if it returns non-zero unless check_exit_code is true.
861
 */
862
int pidref_wait_for_terminate_and_check(const char *name, PidRef *pidref, WaitFlags flags) {
6,852✔
863
        int r;
6,852✔
864

865
        if (!pidref_is_set(pidref))
6,852✔
866
                return -ESRCH;
6,852✔
867
        if (pidref_is_remote(pidref))
13,704✔
868
                return -EREMOTE;
869
        if (pidref->pid == 1 || pidref_is_self(pidref))
6,852✔
870
                return -ECHILD;
×
871

872
        _cleanup_free_ char *buffer = NULL;
6,852✔
873
        if (!name) {
6,852✔
874
                r = pidref_get_comm(pidref, &buffer);
2✔
875
                if (r < 0)
2✔
876
                        log_debug_errno(r, "Failed to acquire process name of " PID_FMT ", ignoring: %m", pidref->pid);
×
877
                else
878
                        name = buffer;
2✔
879
        }
880

881
        int prio = flags & WAIT_LOG_ABNORMAL ? LOG_ERR : LOG_DEBUG;
6,852✔
882

883
        siginfo_t status;
6,852✔
884
        r = pidref_wait_for_terminate(pidref, &status);
6,852✔
885
        if (r < 0)
6,852✔
886
                return log_full_errno(prio, r, "Failed to wait for %s: %m", strna(name));
×
887

888
        if (status.si_code == CLD_EXITED) {
6,852✔
889
                if (status.si_status != EXIT_SUCCESS)
6,852✔
890
                        log_full(flags & WAIT_LOG_NON_ZERO_EXIT_STATUS ? LOG_ERR : LOG_DEBUG,
64✔
891
                                 "%s failed with exit status %i.", strna(name), status.si_status);
892
                else
893
                        log_debug("%s succeeded.", name);
6,788✔
894

895
                return status.si_status;
6,852✔
896

897
        } else if (IN_SET(status.si_code, CLD_KILLED, CLD_DUMPED)) {
×
898

899
                log_full(prio, "%s terminated by signal %s.", strna(name), signal_to_string(status.si_status));
×
900
                return -EPROTO;
×
901
        }
902

903
        log_full(prio, "%s failed due to unknown reason.", strna(name));
×
904
        return -EPROTO;
905
}
906

907
int kill_and_sigcont(pid_t pid, int sig) {
×
908
        int r;
×
909

910
        r = RET_NERRNO(kill(pid, sig));
×
911

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

917
        return r;
×
918
}
919

920
int getenv_for_pid(pid_t pid, const char *field, char **ret) {
4,779✔
921
        _cleanup_fclose_ FILE *f = NULL;
4,779✔
922
        const char *path;
4,779✔
923
        size_t sum = 0;
4,779✔
924
        int r;
4,779✔
925

926
        assert(pid >= 0);
4,779✔
927
        assert(field);
4,779✔
928
        assert(ret);
4,779✔
929

930
        if (pid == 0 || pid == getpid_cached())
4,779✔
931
                return strdup_to_full(ret, getenv(field));
14✔
932

933
        if (!pid_is_valid(pid))
4,765✔
934
                return -EINVAL;
935

936
        path = procfs_file_alloca(pid, "environ");
4,765✔
937

938
        r = fopen_unlocked(path, "re", &f);
4,765✔
939
        if (r == -ENOENT)
4,765✔
940
                return -ESRCH;
941
        if (r < 0)
4,358✔
942
                return r;
943

944
        for (;;) {
51,033✔
945
                _cleanup_free_ char *line = NULL;
24,017✔
946
                const char *match;
27,034✔
947

948
                if (sum > ENVIRONMENT_BLOCK_MAX) /* Give up searching eventually */
27,034✔
949
                        return -ENOBUFS;
950

951
                r = read_nul_string(f, LONG_LINE_MAX, &line);
27,034✔
952
                if (r < 0)
27,034✔
953
                        return r;
954
                if (r == 0)  /* EOF */
27,034✔
955
                        break;
956

957
                sum += r;
24,017✔
958

959
                match = startswith(line, field);
24,017✔
960
                if (match && *match == '=')
24,017✔
961
                        return strdup_to_full(ret, match + 1);
18✔
962
        }
963

964
        *ret = NULL;
3,017✔
965
        return 0;
3,017✔
966
}
967

968
int pidref_is_my_child(PidRef *pid) {
2,446✔
969
        int r;
2,446✔
970

971
        if (!pidref_is_set(pid))
2,446✔
972
                return -ESRCH;
2,446✔
973

974
        if (pidref_is_remote(pid))
2,446✔
975
                return -EREMOTE;
976

977
        if (pid->pid == 1 || pidref_is_self(pid))
2,446✔
978
                return false;
×
979

980
        pid_t ppid;
2,446✔
981
        r = pidref_get_ppid(pid, &ppid);
2,446✔
982
        if (r == -EADDRNOTAVAIL) /* if this process is outside of our pidns, it is definitely not our child */
2,446✔
983
                return false;
984
        if (r < 0)
2,446✔
985
                return r;
986

987
        return ppid == getpid_cached();
2,446✔
988
}
989

990
int pid_is_my_child(pid_t pid) {
×
991

992
        if (pid == 0)
×
993
                return false;
×
994

995
        return pidref_is_my_child(&PIDREF_MAKE_FROM_PID(pid));
×
996
}
997

998
int pidref_is_unwaited(PidRef *pid) {
8,634✔
999
        int r;
8,634✔
1000

1001
        /* Checks whether a PID is still valid at all, including a zombie */
1002

1003
        if (!pidref_is_set(pid))
8,634✔
1004
                return -ESRCH;
1005

1006
        if (pidref_is_remote(pid))
8,633✔
1007
                return -EREMOTE;
1008

1009
        if (pid->pid == 1 || pidref_is_self(pid))
8,633✔
1010
                return true;
3✔
1011

1012
        r = pidref_kill(pid, 0);
8,630✔
1013
        if (r == -ESRCH)
8,630✔
1014
                return false;
1015
        if (r < 0)
1,858✔
1016
                return r;
149✔
1017

1018
        return true;
1019
}
1020

1021
int pid_is_unwaited(pid_t pid) {
8,058✔
1022

1023
        if (pid == 0)
8,058✔
1024
                return true;
8,058✔
1025

1026
        return pidref_is_unwaited(&PIDREF_MAKE_FROM_PID(pid));
8,058✔
1027
}
1028

1029
int pid_is_alive(pid_t pid) {
13,069✔
1030
        int r;
13,069✔
1031

1032
        /* Checks whether a PID is still valid and not a zombie */
1033

1034
        if (pid < 0)
13,069✔
1035
                return -ESRCH;
1036

1037
        if (pid <= 1) /* If we or PID 1 would be a zombie, this code would not be running */
13,068✔
1038
                return true;
1039

1040
        if (pid == getpid_cached())
13,068✔
1041
                return true;
1042

1043
        r = get_process_state(pid);
13,067✔
1044
        if (r == -ESRCH)
13,067✔
1045
                return false;
1046
        if (r < 0)
10,242✔
1047
                return r;
1048

1049
        return r != 'Z';
10,242✔
1050
}
1051

1052
int pidref_is_alive(const PidRef *pidref) {
13,066✔
1053
        int r, result;
13,066✔
1054

1055
        if (!pidref_is_set(pidref))
13,066✔
1056
                return -ESRCH;
1057

1058
        if (pidref_is_remote(pidref))
13,064✔
1059
                return -EREMOTE;
1060

1061
        result = pid_is_alive(pidref->pid);
13,064✔
1062
        if (result < 0) {
13,064✔
1063
                assert(result != -ESRCH);
×
1064
                return result;
1065
        }
1066

1067
        r = pidref_verify(pidref);
13,064✔
1068
        if (r == -ESRCH)
13,064✔
1069
                return false;
1070
        if (r < 0)
10,230✔
1071
                return r;
×
1072

1073
        return result;
1074
}
1075

1076
int pidref_from_same_root_fs(PidRef *a, PidRef *b) {
20✔
1077
        _cleanup_(pidref_done) PidRef self = PIDREF_NULL;
×
1078
        int r;
20✔
1079

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

1083
        if (!a || !b) {
20✔
1084
                r = pidref_set_self(&self);
×
1085
                if (r < 0)
×
1086
                        return r;
1087
                if (!a)
×
1088
                        a = &self;
×
1089
                if (!b)
×
1090
                        b = &self;
×
1091
        }
1092

1093
        if (!pidref_is_set(a) || !pidref_is_set(b))
20✔
1094
                return -ESRCH;
×
1095

1096
        /* If one of the two processes have the same root they cannot have the same root fs, but if both of
1097
         * them do we don't know */
1098
        if (pidref_is_remote(a) && pidref_is_remote(b))
20✔
1099
                return -EREMOTE;
1100
        if (pidref_is_remote(a) || pidref_is_remote(b))
60✔
1101
                return false;
1102

1103
        if (pidref_equal(a, b))
20✔
1104
                return true;
1105

1106
        const char *roota = procfs_file_alloca(a->pid, "root");
18✔
1107
        const char *rootb = procfs_file_alloca(b->pid, "root");
18✔
1108

1109
        int result = inode_same(roota, rootb, 0);
18✔
1110
        if (result == -ENOENT)
18✔
1111
                return proc_mounted() == 0 ? -ENOSYS : -ESRCH;
×
1112
        if (result < 0)
18✔
1113
                return result;
1114

1115
        r = pidref_verify(a);
18✔
1116
        if (r < 0)
18✔
1117
                return r;
1118
        r = pidref_verify(b);
18✔
1119
        if (r < 0)
18✔
1120
                return r;
×
1121

1122
        return result;
1123
}
1124

1125
bool is_main_thread(void) {
7,287,960✔
1126
        static thread_local int cached = -1;
7,287,960✔
1127

1128
        if (cached < 0)
7,287,960✔
1129
                cached = getpid_cached() == gettid();
55,952✔
1130

1131
        return cached;
7,287,960✔
1132
}
1133

1134
bool oom_score_adjust_is_valid(int oa) {
6,907✔
1135
        return oa >= OOM_SCORE_ADJ_MIN && oa <= OOM_SCORE_ADJ_MAX;
6,907✔
1136
}
1137

1138
unsigned long personality_from_string(const char *p) {
9✔
1139
        Architecture architecture;
9✔
1140

1141
        if (!p)
9✔
1142
                return PERSONALITY_INVALID;
1143

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

1148
        architecture = architecture_from_string(p);
8✔
1149
        if (architecture < 0)
8✔
1150
                return PERSONALITY_INVALID;
1151

1152
        if (architecture == native_architecture())
6✔
1153
                return PER_LINUX;
1154
#ifdef ARCHITECTURE_SECONDARY
1155
        if (architecture == ARCHITECTURE_SECONDARY)
3✔
1156
                return PER_LINUX32;
2✔
1157
#endif
1158

1159
        return PERSONALITY_INVALID;
1160
}
1161

1162
const char* personality_to_string(unsigned long p) {
2,782✔
1163
        Architecture architecture = _ARCHITECTURE_INVALID;
2,782✔
1164

1165
        if (p == PER_LINUX)
2,782✔
1166
                architecture = native_architecture();
1167
#ifdef ARCHITECTURE_SECONDARY
1168
        else if (p == PER_LINUX32)
2,777✔
1169
                architecture = ARCHITECTURE_SECONDARY;
1170
#endif
1171

1172
        if (architecture < 0)
1173
                return NULL;
1174

1175
        return architecture_to_string(architecture);
7✔
1176
}
1177

1178
int safe_personality(unsigned long p) {
1,484✔
1179
        int ret;
1,484✔
1180

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

1188
        errno = 0;
1,484✔
1189
        ret = personality(p);
1,484✔
1190
        if (ret < 0) {
1,484✔
1191
                if (errno != 0)
12✔
1192
                        return -errno;
12✔
1193

1194
                errno = -ret;
×
1195
        }
1196

1197
        return ret;
1198
}
1199

1200
int opinionated_personality(unsigned long *ret) {
1,469✔
1201
        int current;
1,469✔
1202

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

1207
        current = safe_personality(PERSONALITY_INVALID);
1,469✔
1208
        if (current < 0)
1,469✔
1209
                return current;
1210

1211
        if (((unsigned long) current & OPINIONATED_PERSONALITY_MASK) == PER_LINUX32)
1,469✔
1212
                *ret = PER_LINUX32;
×
1213
        else
1214
                *ret = PER_LINUX;
1,469✔
1215

1216
        return 0;
1217
}
1218

1219
void valgrind_summary_hack(void) {
39✔
1220
#if HAVE_VALGRIND_VALGRIND_H
1221
        if (getpid_cached() == 1 && RUNNING_ON_VALGRIND) {
1222
                pid_t pid;
1223
                pid = raw_clone(SIGCHLD);
1224
                if (pid < 0)
1225
                        log_struct_errno(
1226
                                LOG_EMERG, errno,
1227
                                LOG_MESSAGE_ID(SD_MESSAGE_VALGRIND_HELPER_FORK_STR),
1228
                                LOG_MESSAGE("Failed to fork off valgrind helper: %m"));
1229
                else if (pid == 0)
1230
                        exit(EXIT_SUCCESS);
1231
                else {
1232
                        log_info("Spawned valgrind helper as PID "PID_FMT".", pid);
1233
                        _cleanup_(pidref_done) PidRef pidref = PIDREF_MAKE_FROM_PID(pid);
1234
                        (void) pidref_set_pid(&pidref, pid);
1235
                        (void) pidref_wait_for_terminate(&pidref, NULL);
1236
                }
1237
        }
1238
#endif
1239
}
39✔
1240

1241
int pid_compare_func(const pid_t *a, const pid_t *b) {
1,070✔
1242
        /* Suitable for usage in qsort() */
1243
        return CMP(*a, *b);
1,070✔
1244
}
1245

1246
bool nice_is_valid(int n) {
871✔
1247
        return n >= PRIO_MIN && n < PRIO_MAX;
871✔
1248
}
1249

1250
bool sched_policy_is_valid(int i) {
×
1251
        return IN_SET(i, SCHED_OTHER, SCHED_BATCH, SCHED_IDLE, SCHED_FIFO, SCHED_RR, SCHED_EXT);
×
1252
}
1253

1254
bool sched_policy_supported(int policy) {
4✔
1255
        return sched_get_priority_min(policy) >= 0;
4✔
1256
}
1257

1258
/* Wrappers around sched_get_priority_{min,max}() that gracefully handles missing SCHED_EXT support in the kernel */
1259
int sched_get_priority_min_safe(int policy) {
4✔
1260
        int r;
4✔
1261

1262
        r = sched_get_priority_min(policy);
4✔
1263
        if (r >= 0)
4✔
1264
                return r;
3✔
1265

1266
        /* Fallback priority */
1267
        return 0;
1268
}
1269

1270
int sched_get_priority_max_safe(int policy) {
4✔
1271
        int r;
4✔
1272

1273
        r = sched_get_priority_max(policy);
4✔
1274
        if (r >= 0)
4✔
1275
                return r;
3✔
1276

1277
        return 0;
1278
}
1279

1280
/* The cached PID, possible values:
1281
 *
1282
 *     == UNSET [0]  → cache not initialized yet
1283
 *     == BUSY [-1]  → some thread is initializing it at the moment
1284
 *     any other     → the cached PID
1285
 */
1286

1287
#define CACHED_PID_UNSET ((pid_t) 0)
1288
#define CACHED_PID_BUSY ((pid_t) -1)
1289

1290
static pid_t cached_pid = CACHED_PID_UNSET;
1291

1292
void reset_cached_pid(void) {
1,592✔
1293
        /* Invoked in the child after a fork(), i.e. at the first moment the PID changed */
1294
        cached_pid = CACHED_PID_UNSET;
1,592✔
1295
}
1,592✔
1296

1297
pid_t getpid_cached(void) {
152,972,607✔
1298
        static bool installed = false;
152,972,607✔
1299
        pid_t current_value = CACHED_PID_UNSET;
152,972,607✔
1300

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

1310
        (void) __atomic_compare_exchange_n(
152,972,607✔
1311
                        &cached_pid,
1312
                        &current_value,
1313
                        CACHED_PID_BUSY,
1314
                        false,
1315
                        __ATOMIC_SEQ_CST,
1316
                        __ATOMIC_SEQ_CST);
1317

1318
        switch (current_value) {
152,972,607✔
1319

1320
        case CACHED_PID_UNSET: { /* Not initialized yet, then do so now */
102,422✔
1321
                pid_t new_pid;
102,422✔
1322

1323
                new_pid = getpid();
102,422✔
1324

1325
                if (!installed) {
102,422✔
1326
                        /* __register_atfork() either returns 0 or -ENOMEM, in its glibc implementation. Since it's
1327
                         * only half-documented (glibc doesn't document it but LSB does — though only superficially)
1328
                         * we'll check for errors only in the most generic fashion possible. */
1329

1330
                        if (pthread_atfork(NULL, NULL, reset_cached_pid) != 0) {
71,908✔
1331
                                /* OOM? Let's try again later */
1332
                                cached_pid = CACHED_PID_UNSET;
×
1333
                                return new_pid;
×
1334
                        }
1335

1336
                        installed = true;
71,908✔
1337
                }
1338

1339
                cached_pid = new_pid;
102,422✔
1340
                return new_pid;
102,422✔
1341
        }
1342

1343
        case CACHED_PID_BUSY: /* Somebody else is currently initializing */
×
1344
                return getpid();
×
1345

1346
        default: /* Properly initialized */
1347
                return current_value;
1348
        }
1349
}
1350

1351
int must_be_root(void) {
56✔
1352

1353
        if (geteuid() == 0)
56✔
1354
                return 0;
1355

1356
        return log_error_errno(SYNTHETIC_ERRNO(EPERM), "Need to be root.");
×
1357
}
1358

1359
pid_t clone_with_nested_stack(int (*fn)(void *), int flags, void *userdata) {
3,516✔
1360
        size_t ps;
3,516✔
1361
        pid_t pid;
3,516✔
1362
        void *mystack;
3,516✔
1363

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

1372
        assert((flags & (CLONE_VM|CLONE_PARENT_SETTID|CLONE_CHILD_SETTID|
3,516✔
1373
                         CLONE_CHILD_CLEARTID|CLONE_SETTLS)) == 0);
1374

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

1382
        ps = page_size();
3,516✔
1383
        mystack = alloca(ps*3);
3,516✔
1384
        mystack = (uint8_t*) mystack + ps; /* move pointer one page ahead since stacks usually grow backwards */
3,516✔
1385
        mystack = (void*) ALIGN_TO((uintptr_t) mystack, ps); /* align to page size (moving things further ahead) */
3,516✔
1386

1387
#if HAVE_CLONE
1388
        pid = clone(fn, mystack, flags, userdata);
3,516✔
1389
#else
1390
        pid = __clone2(fn, mystack, ps, flags, userdata);
1391
#endif
1392
        if (pid < 0)
3,516✔
1393
                return -errno;
×
1394

1395
        return pid;
1396
}
1397

1398
static int fork_flags_to_signal(ForkFlags flags) {
28,692✔
1399
        return (flags & FORK_DEATHSIG_SIGTERM) ? SIGTERM :
28,692✔
1400
                (flags & FORK_DEATHSIG_SIGINT) ? SIGINT :
561✔
1401
                                                 SIGKILL;
1402
}
1403

1404
int pidref_safe_fork_full(
28,239✔
1405
                const char *name,
1406
                const int stdio_fds[3],
1407
                int except_fds[],
1408
                size_t n_except_fds,
1409
                ForkFlags flags,
1410
                PidRef *ret_pid) {
1411

1412
        pid_t original_pid, pid;
28,239✔
1413
        sigset_t saved_ss, ss;
28,239✔
1414
        _unused_ _cleanup_(block_signals_reset) sigset_t *saved_ssp = NULL;
×
1415
        bool block_signals = false, block_all = false, intermediary = false;
28,239✔
1416
        _cleanup_close_pair_ int pidref_transport_fds[2] = EBADF_PAIR;
58,043✔
1417
        int prio, r;
28,239✔
1418

1419
        assert(!FLAGS_SET(flags, FORK_WAIT|FORK_FREEZE));
28,239✔
1420
        assert(!FLAGS_SET(flags, FORK_DETACH) ||
28,239✔
1421
               (flags & (FORK_WAIT|FORK_DEATHSIG_SIGTERM|FORK_DEATHSIG_SIGINT|FORK_DEATHSIG_SIGKILL)) == 0);
1422

1423
        /* A wrapper around fork(), that does a couple of important initializations in addition to mere
1424
         * forking. If provided, ret_pid is initialized in both the parent and the child process, both times
1425
         * referencing the child process. Returns == 0 in the child and > 0 in the parent. */
1426

1427
        prio = flags & FORK_LOG ? LOG_ERR : LOG_DEBUG;
28,239✔
1428

1429
        original_pid = getpid_cached();
28,239✔
1430

1431
        if (flags & FORK_FLUSH_STDIO) {
28,239✔
1432
                fflush(stdout);
5✔
1433
                fflush(stderr); /* This one shouldn't be necessary, stderr should be unbuffered anyway, but let's better be safe than sorry */
5✔
1434
        }
1435

1436
        if (flags & (FORK_RESET_SIGNALS|FORK_DEATHSIG_SIGTERM|FORK_DEATHSIG_SIGINT)) {
28,239✔
1437
                /* We temporarily block all signals, so that the new child has them blocked initially. This
1438
                 * way, we can be sure that SIGTERMs are not lost we might send to the child. (Note that for
1439
                 * FORK_DEATHSIG_SIGKILL we don't bother, since it cannot be blocked anyway.) */
1440

1441
                assert_se(sigfillset(&ss) >= 0);
24,174✔
1442
                block_signals = block_all = true;
1443

1444
        } else if (flags & FORK_WAIT) {
4,065✔
1445
                /* Let's block SIGCHLD at least, so that we can safely watch for the child process */
1446

1447
                assert_se(sigemptyset(&ss) >= 0);
157✔
1448
                assert_se(sigaddset(&ss, SIGCHLD) >= 0);
157✔
1449
                block_signals = true;
1450
        }
1451

1452
        if (block_signals) {
1453
                if (sigprocmask(SIG_BLOCK, &ss, &saved_ss) < 0)
24,331✔
1454
                        return log_full_errno(prio, errno, "Failed to block signal mask: %m");
×
1455
                saved_ssp = &saved_ss;
24,331✔
1456
        }
1457

1458
        if (FLAGS_SET(flags, FORK_DETACH)) {
28,239✔
1459
                /* Fork off intermediary child if needed */
1460

1461
                r = is_reaper_process();
105✔
1462
                if (r < 0)
105✔
1463
                        return log_full_errno(prio, r, "Failed to determine if we are a reaper process: %m");
×
1464

1465
                if (!r) {
105✔
1466
                        /* Not a reaper process, hence do a double fork() so we are reparented to one */
1467

1468
                        if (ret_pid && socketpair(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC, 0, pidref_transport_fds) < 0)
11✔
1469
                                return log_full_errno(prio, errno, "Failed to allocate pidref socket: %m");
×
1470

1471
                        pid = fork();
11✔
1472
                        if (pid < 0)
28✔
1473
                                return log_full_errno(prio, errno, "Failed to fork off '%s': %m", strna(name));
×
1474
                        if (pid > 0) {
28✔
1475
                                log_debug("Successfully forked off intermediary '%s' as PID " PID_FMT ".", strna(name), pid);
11✔
1476

1477
                                pidref_transport_fds[1] = safe_close(pidref_transport_fds[1]);
11✔
1478

1479
                                if (pidref_transport_fds[0] >= 0) {
11✔
1480
                                        /* Wait for the intermediary child to exit so the caller can be
1481
                                         * certain the actual child process has been reparented by the time
1482
                                         * this function returns. */
1483
                                        r = pidref_wait_for_terminate_and_check(
10✔
1484
                                                        name,
1485
                                                        &PIDREF_MAKE_FROM_PID(pid),
10✔
1486
                                                        FLAGS_SET(flags, FORK_LOG) ? WAIT_LOG : 0);
1487
                                        if (r < 0)
10✔
1488
                                                return log_full_errno(prio, r, "Failed to wait for intermediary process: %m");
×
1489
                                        if (r != EXIT_SUCCESS) /* exit status > 0 should be treated as failure, too */
10✔
1490
                                                return -EPROTO;
1491

1492
                                        int pidfd;
10✔
1493
                                        ssize_t n = receive_one_fd_iov(
20✔
1494
                                                        pidref_transport_fds[0],
1495
                                                        &IOVEC_MAKE(&pid, sizeof(pid)),
10✔
1496
                                                        /* iovlen= */ 1,
1497
                                                        /* flags= */ 0,
1498
                                                        &pidfd);
1499
                                        if (n < 0)
10✔
1500
                                                return log_full_errno(prio, n, "Failed to receive child pidref: %m");
×
1501

1502
                                        *ret_pid = (PidRef) { .pid = pid, .fd = pidfd };
10✔
1503
                                }
1504

1505
                                return 1; /* return in the parent */
11✔
1506
                        }
1507

1508
                        pidref_transport_fds[0] = safe_close(pidref_transport_fds[0]);
17✔
1509
                        intermediary = true;
17✔
1510
                }
1511
        }
1512

1513
        if ((flags & (FORK_NEW_MOUNTNS|FORK_NEW_USERNS|FORK_NEW_NETNS|FORK_NEW_PIDNS)) != 0)
28,245✔
1514
                pid = raw_clone(SIGCHLD|
5,589✔
1515
                                (FLAGS_SET(flags, FORK_NEW_MOUNTNS) ? CLONE_NEWNS : 0) |
5,589✔
1516
                                (FLAGS_SET(flags, FORK_NEW_USERNS) ? CLONE_NEWUSER : 0) |
5,589✔
1517
                                (FLAGS_SET(flags, FORK_NEW_NETNS) ? CLONE_NEWNET : 0) |
5,589✔
1518
                                (FLAGS_SET(flags, FORK_NEW_PIDNS) ? CLONE_NEWPID : 0));
5,589✔
1519
        else
1520
                pid = fork();
22,656✔
1521
        if (pid < 0)
58,043✔
1522
                return log_full_errno(prio, errno, "Failed to fork off '%s': %m", strna(name));
×
1523
        if (pid > 0) {
58,043✔
1524

1525
                /* If we are in the intermediary process, exit now */
1526
                if (intermediary) {
27,907✔
1527
                        if (pidref_transport_fds[1] >= 0) {
11✔
1528
                                _cleanup_(pidref_done) PidRef pidref = PIDREF_NULL;
10✔
1529

1530
                                r = pidref_set_pid(&pidref, pid);
10✔
1531
                                if (r < 0) {
10✔
1532
                                        log_full_errno(prio, r, "Failed to open reference to PID "PID_FMT": %m", pid);
×
1533
                                        _exit(EXIT_FAILURE);
×
1534
                                }
1535

1536
                                r = send_one_fd_iov(
10✔
1537
                                                pidref_transport_fds[1],
1538
                                                pidref.fd,
1539
                                                &IOVEC_MAKE(&pidref.pid, sizeof(pidref.pid)),
1540
                                                /* iovlen= */ 1,
1541
                                                /* flags= */ 0);
1542
                                if (r < 0) {
10✔
1543
                                        log_full_errno(prio, r, "Failed to send child pidref: %m");
×
1544
                                        _exit(EXIT_FAILURE);
×
1545
                                }
1546
                        }
1547

1548
                        _exit(EXIT_SUCCESS);
11✔
1549
                }
1550

1551
                /* We are in the parent process */
1552
                log_debug("Successfully forked off '%s' as PID " PID_FMT ".", strna(name), pid);
27,896✔
1553

1554
                if (flags & FORK_WAIT) {
27,896✔
1555
                        if (block_all) {
836✔
1556
                                /* undo everything except SIGCHLD */
1557
                                ss = saved_ss;
679✔
1558
                                assert_se(sigaddset(&ss, SIGCHLD) >= 0);
679✔
1559
                                (void) sigprocmask(SIG_SETMASK, &ss, NULL);
679✔
1560
                        }
1561

1562
                        r = pidref_wait_for_terminate_and_check(
836✔
1563
                                        name,
1564
                                        &PIDREF_MAKE_FROM_PID(pid),
836✔
1565
                                        FLAGS_SET(flags, FORK_LOG) ? WAIT_LOG : 0);
1566
                        if (r < 0)
836✔
1567
                                return r;
836✔
1568
                        if (r != EXIT_SUCCESS) /* exit status > 0 should be treated as failure, too */
836✔
1569
                                return -EPROTO;
1570

1571
                        /* If we are in the parent and successfully waited, then the process doesn't exist anymore. */
1572
                        if (ret_pid)
836✔
1573
                                *ret_pid = PIDREF_NULL;
14✔
1574

1575
                        return 1;
836✔
1576
                }
1577

1578
                if (ret_pid) {
27,060✔
1579
                        if (FLAGS_SET(flags, _FORK_PID_ONLY))
26,457✔
1580
                                *ret_pid = PIDREF_MAKE_FROM_PID(pid);
1,977✔
1581
                        else {
1582
                                r = pidref_set_pid(ret_pid, pid);
24,480✔
1583
                                if (r < 0) /* Let's not fail for this, no matter what, the process exists after all, and that's key */
24,480✔
1584
                                        *ret_pid = PIDREF_MAKE_FROM_PID(pid);
×
1585
                        }
1586
                }
1587

1588
                return 1;
27,060✔
1589
        }
1590

1591
        /* We are in the child process */
1592

1593
        pidref_transport_fds[1] = safe_close(pidref_transport_fds[1]);
30,136✔
1594

1595
        /* Restore signal mask manually */
1596
        saved_ssp = NULL;
30,136✔
1597

1598
        if (flags & FORK_REOPEN_LOG) {
30,136✔
1599
                /* Close the logs if requested, before we log anything. And make sure we reopen it if needed. */
1600
                log_close();
6,727✔
1601
                log_set_open_when_needed(true);
6,727✔
1602
                log_settle_target();
6,727✔
1603
        }
1604

1605
        if (name) {
30,136✔
1606
                r = rename_process(name);
30,136✔
1607
                if (r < 0)
30,136✔
1608
                        log_full_errno(flags & FORK_LOG ? LOG_WARNING : LOG_DEBUG,
×
1609
                                       r, "Failed to rename process, ignoring: %m");
1610
        }
1611

1612
        /* let's disable dlopen() in the child, as a paranoia safety precaution: children should not live for
1613
         * long and only do minimal work before exiting or exec()ing. Doing dlopen() is not either. If people
1614
         * want dlopen() they should do it before forking. This is a safety precaution in particular for
1615
         * cases where the child does namespace shenanigans: we should never end up loading a module from a
1616
         * foreign environment. Note that this has no effect on NSS! (i.e. it only has effect on uses of our
1617
         * dlopen_safe(), which we use comprehensively in our codebase, but glibc NSS doesn't bother, of
1618
         * course.) */
1619
        if (!FLAGS_SET(flags, FORK_ALLOW_DLOPEN))
30,136✔
1620
                block_dlopen();
30,102✔
1621

1622
        if (flags & (FORK_DEATHSIG_SIGTERM|FORK_DEATHSIG_SIGINT|FORK_DEATHSIG_SIGKILL))
30,136✔
1623
                if (prctl(PR_SET_PDEATHSIG, fork_flags_to_signal(flags)) < 0) {
28,692✔
1624
                        log_full_errno(prio, errno, "Failed to set death signal: %m");
×
1625
                        _exit(EXIT_FAILURE);
×
1626
                }
1627

1628
        if (flags & FORK_RESET_SIGNALS) {
30,136✔
1629
                r = reset_all_signal_handlers();
24,938✔
1630
                if (r < 0) {
24,938✔
1631
                        log_full_errno(prio, r, "Failed to reset signal handlers: %m");
×
1632
                        _exit(EXIT_FAILURE);
×
1633
                }
1634

1635
                /* This implicitly undoes the signal mask stuff we did before the fork()ing above */
1636
                r = reset_signal_mask();
24,938✔
1637
                if (r < 0) {
24,938✔
1638
                        log_full_errno(prio, r, "Failed to reset signal mask: %m");
×
1639
                        _exit(EXIT_FAILURE);
×
1640
                }
1641
        } else if (block_signals) { /* undo what we did above */
5,198✔
1642
                if (sigprocmask(SIG_SETMASK, &saved_ss, NULL) < 0) {
4,760✔
1643
                        log_full_errno(prio, errno, "Failed to restore signal mask: %m");
×
1644
                        _exit(EXIT_FAILURE);
×
1645
                }
1646
        }
1647

1648
        if (flags & (FORK_DEATHSIG_SIGTERM|FORK_DEATHSIG_SIGKILL|FORK_DEATHSIG_SIGINT)) {
30,136✔
1649
                pid_t ppid;
28,692✔
1650
                /* Let's see if the parent PID is still the one we started from? If not, then the parent
1651
                 * already died by the time we set PR_SET_PDEATHSIG, hence let's emulate the effect */
1652

1653
                ppid = getppid();
28,692✔
1654
                if (ppid == 0)
28,692✔
1655
                        /* Parent is in a different PID namespace. */;
1656
                else if (ppid != original_pid) {
28,654✔
1657
                        int sig = fork_flags_to_signal(flags);
×
1658
                        log_debug("Parent died early, raising %s.", signal_to_string(sig));
×
1659
                        (void) raise(sig);
×
1660
                        _exit(EXIT_FAILURE);
×
1661
                }
1662
        }
1663

1664
        if (FLAGS_SET(flags, FORK_NEW_MOUNTNS | FORK_MOUNTNS_SLAVE)) {
30,136✔
1665
                /* Optionally, make sure we never propagate mounts to the host. */
1666
                if (mount(NULL, "/", NULL, MS_SLAVE | MS_REC, NULL) < 0) {
152✔
1667
                        log_full_errno(prio, errno, "Failed to remount root directory as MS_SLAVE: %m");
×
1668
                        _exit(EXIT_FAILURE);
×
1669
                }
1670
        }
1671

1672
        if (FLAGS_SET(flags, FORK_PRIVATE_TMP)) {
30,136✔
1673
                assert(FLAGS_SET(flags, FORK_NEW_MOUNTNS));
×
1674

1675
                /* Optionally, overmount new tmpfs instance on /tmp/. */
1676
                r = mount_nofollow("tmpfs", "/tmp", "tmpfs",
×
1677
                                   MS_NOSUID|MS_NODEV,
1678
                                   "mode=01777" TMPFS_LIMITS_RUN);
1679
                if (r < 0) {
×
1680
                        log_full_errno(prio, r, "Failed to overmount /tmp/: %m");
×
1681
                        _exit(EXIT_FAILURE);
×
1682
                }
1683
        }
1684

1685
        if (flags & FORK_REARRANGE_STDIO) {
30,136✔
1686
                if (stdio_fds) {
15,929✔
1687
                        r = rearrange_stdio(stdio_fds[0], stdio_fds[1], stdio_fds[2]);
15,913✔
1688
                        if (r < 0) {
15,913✔
1689
                                log_full_errno(prio, r, "Failed to rearrange stdio fds: %m");
×
1690
                                _exit(EXIT_FAILURE);
×
1691
                        }
1692

1693
                        /* Turn off O_NONBLOCK on the fdio fds, in case it was left on */
1694
                        stdio_disable_nonblock();
15,913✔
1695
                } else {
1696
                        r = make_null_stdio();
16✔
1697
                        if (r < 0) {
16✔
1698
                                log_full_errno(prio, r, "Failed to connect stdin/stdout to /dev/null: %m");
×
1699
                                _exit(EXIT_FAILURE);
×
1700
                        }
1701
                }
1702
        } else if (flags & FORK_STDOUT_TO_STDERR) {
14,207✔
1703
                if (dup2(STDERR_FILENO, STDOUT_FILENO) < 0) {
2✔
1704
                        log_full_errno(prio, errno, "Failed to connect stdout to stderr: %m");
×
1705
                        _exit(EXIT_FAILURE);
×
1706
                }
1707
        }
1708

1709
        if (flags & FORK_CLOSE_ALL_FDS) {
30,136✔
1710
                /* Close the logs here in case it got reopened above, as close_all_fds() would close them for us */
1711
                log_close();
24,223✔
1712

1713
                r = close_all_fds(except_fds, n_except_fds);
24,223✔
1714
                if (r < 0) {
24,223✔
1715
                        log_full_errno(prio, r, "Failed to close all file descriptors: %m");
×
1716
                        _exit(EXIT_FAILURE);
×
1717
                }
1718
        }
1719

1720
        if (flags & FORK_PACK_FDS) {
30,136✔
1721
                /* FORK_CLOSE_ALL_FDS ensures that except_fds are the only FDs >= 3 that are
1722
                 * open, this is including the log. This is required by pack_fds, which will
1723
                 * get stuck in an infinite loop of any FDs other than except_fds are open. */
1724
                assert(FLAGS_SET(flags, FORK_CLOSE_ALL_FDS));
92✔
1725

1726
                r = pack_fds(except_fds, n_except_fds);
92✔
1727
                if (r < 0) {
92✔
1728
                        log_full_errno(prio, r, "Failed to pack file descriptors: %m");
×
1729
                        _exit(EXIT_FAILURE);
×
1730
                }
1731
        }
1732

1733
        if (flags & FORK_CLOEXEC_OFF) {
30,136✔
1734
                r = fd_cloexec_many(except_fds, n_except_fds, false);
107✔
1735
                if (r < 0) {
107✔
1736
                        log_full_errno(prio, r, "Failed to turn off O_CLOEXEC on file descriptors: %m");
×
1737
                        _exit(EXIT_FAILURE);
×
1738
                }
1739
        }
1740

1741
        /* When we were asked to reopen the logs, do so again now */
1742
        if (flags & FORK_REOPEN_LOG) {
30,136✔
1743
                log_open();
6,727✔
1744
                log_set_open_when_needed(false);
6,727✔
1745
        }
1746

1747
        if (flags & FORK_RLIMIT_NOFILE_SAFE) {
30,136✔
1748
                r = rlimit_nofile_safe();
17,053✔
1749
                if (r < 0) {
17,053✔
1750
                        log_full_errno(prio, r, "Failed to lower RLIMIT_NOFILE's soft limit to 1K: %m");
×
1751
                        _exit(EXIT_FAILURE);
×
1752
                }
1753
        }
1754

1755
        if (!FLAGS_SET(flags, FORK_KEEP_NOTIFY_SOCKET)) {
30,136✔
1756
                r = RET_NERRNO(unsetenv("NOTIFY_SOCKET"));
30,136✔
1757
                if (r < 0) {
×
1758
                        log_full_errno(prio, r, "Failed to unset $NOTIFY_SOCKET: %m");
×
1759
                        _exit(EXIT_FAILURE);
×
1760
                }
1761
        }
1762

1763
        if (FLAGS_SET(flags, FORK_FREEZE))
30,136✔
1764
                freeze();
×
1765

1766
        if (ret_pid) {
30,136✔
1767
                if (FLAGS_SET(flags, _FORK_PID_ONLY))
28,573✔
1768
                        *ret_pid = PIDREF_MAKE_FROM_PID(getpid_cached());
2,954✔
1769
                else {
1770
                        r = pidref_set_self(ret_pid);
25,619✔
1771
                        if (r < 0) {
25,619✔
1772
                                log_full_errno(prio, r, "Failed to acquire PID reference on ourselves: %m");
×
1773
                                _exit(EXIT_FAILURE);
×
1774
                        }
1775
                }
1776
        }
1777

1778
        return 0;
1779
}
1780

1781
int safe_fork_full(
3,586✔
1782
                const char *name,
1783
                const int stdio_fds[3],
1784
                int except_fds[],
1785
                size_t n_except_fds,
1786
                ForkFlags flags,
1787
                pid_t *ret_pid) {
1788

1789
        _cleanup_(pidref_done) PidRef pidref = PIDREF_NULL;
7,864✔
1790
        int r;
3,586✔
1791

1792
        /* Getting the detached child process pid without pidfd is racy, so don't allow it if not returning
1793
         * a pidref to the caller. */
1794
        assert(!FLAGS_SET(flags, FORK_DETACH) || !ret_pid);
3,586✔
1795

1796
        r = pidref_safe_fork_full(name, stdio_fds, except_fds, n_except_fds, flags|_FORK_PID_ONLY, ret_pid ? &pidref : NULL);
5,082✔
1797
        if (r < 0 || !ret_pid)
7,864✔
1798
                return r;
1799

1800
        *ret_pid = pidref.pid;
4,943✔
1801

1802
        return r;
4,943✔
1803
}
1804

1805
int namespace_fork_full(
129✔
1806
                const char *outer_name,
1807
                const char *inner_name,
1808
                int except_fds[],
1809
                size_t n_except_fds,
1810
                ForkFlags flags,
1811
                int pidns_fd,
1812
                int mntns_fd,
1813
                int netns_fd,
1814
                int userns_fd,
1815
                int root_fd,
1816
                PidRef *ret) {
1817

1818
        _cleanup_(pidref_done_sigkill_wait) PidRef pidref_outer = PIDREF_NULL;
×
1819
        _cleanup_close_pair_ int errno_pipe_fd[2] = EBADF_PAIR;
216✔
1820
        int r, prio = FLAGS_SET(flags, FORK_LOG) ? LOG_ERR : LOG_DEBUG;
129✔
1821

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

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

1835
        /* We want read() to block as a synchronization point */
1836
        assert_cc(sizeof(int) <= PIPE_BUF);
129✔
1837
        if (pipe2(errno_pipe_fd, O_CLOEXEC) < 0)
129✔
1838
                return log_full_errno(prio, errno, "Failed to create pipe: %m");
×
1839

1840
        r = pidref_safe_fork_full(
344✔
1841
                        outer_name,
1842
                        /* stdio_fds = */ NULL, /* except_fds = */ NULL, /* n_except_fds = */ 0,
1843
                        (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),
129✔
1844
                        &pidref_outer);
1845
        if (r == -EPROTO && FLAGS_SET(flags, FORK_WAIT)) {
215✔
1846
                errno_pipe_fd[1] = safe_close(errno_pipe_fd[1]);
×
1847

1848
                int k = read_errno(errno_pipe_fd[0]);
×
1849
                if (k < 0 && k != -EIO)
×
1850
                        return k;
1851
        }
1852
        if (r < 0)
215✔
1853
                return r;
1854
        if (r == 0) {
215✔
1855
                _cleanup_(pidref_done) PidRef pidref_inner = PIDREF_NULL;
×
1856

1857
                /* Child */
1858

1859
                errno_pipe_fd[0] = safe_close(errno_pipe_fd[0]);
86✔
1860

1861
                r = namespace_enter(pidns_fd, mntns_fd, netns_fd, userns_fd, root_fd);
86✔
1862
                if (r < 0) {
86✔
1863
                        log_full_errno(prio, r, "Failed to join namespace: %m");
×
1864
                        report_errno_and_exit(errno_pipe_fd[1], r);
×
1865
                }
1866

1867
                /* We mask a few flags here that either make no sense for the grandchild, or that we don't have to do again */
1868
                r = pidref_safe_fork_full(
259✔
1869
                                inner_name,
1870
                                NULL,
1871
                                except_fds, n_except_fds,
1872
                                flags & ~(FORK_WAIT|FORK_RESET_SIGNALS|FORK_REARRANGE_STDIO|FORK_FLUSH_STDIO|FORK_STDOUT_TO_STDERR),
86✔
1873
                                &pidref_inner);
1874
                if (r < 0)
173✔
1875
                        report_errno_and_exit(errno_pipe_fd[1], r);
×
1876
                if (r == 0) {
173✔
1877
                        /* Child */
1878

1879
                        if (!FLAGS_SET(flags, FORK_CLOSE_ALL_FDS)) {
87✔
1880
                                errno_pipe_fd[1] = safe_close(errno_pipe_fd[1]);
87✔
1881
                                pidref_done(&pidref_outer);
87✔
1882
                        } else {
1883
                                errno_pipe_fd[1] = -EBADF;
×
1884
                                pidref_outer = PIDREF_NULL;
×
1885
                        }
1886

1887
                        if (ret)
87✔
1888
                                *ret = TAKE_PIDREF(pidref_inner);
87✔
1889
                        return 0;
87✔
1890
                }
1891

1892
                log_close();
86✔
1893
                log_set_open_when_needed(true);
86✔
1894

1895
                (void) close_all_fds(&pidref_inner.fd, 1);
86✔
1896

1897
                r = pidref_wait_for_terminate_and_check(
172✔
1898
                                inner_name,
1899
                                &pidref_inner,
1900
                                FLAGS_SET(flags, FORK_LOG) ? WAIT_LOG : 0);
1901
                if (r < 0)
86✔
1902
                        _exit(EXIT_FAILURE);
×
1903

1904
                _exit(r);
86✔
1905
        }
1906

1907
        errno_pipe_fd[1] = safe_close(errno_pipe_fd[1]);
129✔
1908

1909
        r = read_errno(errno_pipe_fd[0]);
129✔
1910
        if (r < 0)
129✔
1911
                return r; /* the child logs about failures on its own, no need to duplicate here */
1912

1913
        if (ret)
129✔
1914
                *ret = TAKE_PIDREF(pidref_outer);
129✔
1915
        else
1916
                pidref_done(&pidref_outer); /* disarm sigkill_wait */
×
1917

1918
        return 1;
1919
}
1920

1921
int set_oom_score_adjust(int value) {
3,806✔
1922
        char t[DECIMAL_STR_MAX(int)];
3,806✔
1923

1924
        if (!oom_score_adjust_is_valid(value))
3,806✔
1925
                return -EINVAL;
3,806✔
1926

1927
        xsprintf(t, "%i", value);
3,806✔
1928

1929
        return write_string_file("/proc/self/oom_score_adj", t,
3,806✔
1930
                                 WRITE_STRING_FILE_VERIFY_ON_FAILURE|WRITE_STRING_FILE_DISABLE_BUFFER);
1931
}
1932

1933
int get_oom_score_adjust(int *ret) {
2,409✔
1934
        _cleanup_free_ char *t = NULL;
2,409✔
1935
        int r, a;
2,409✔
1936

1937
        r = read_virtual_file("/proc/self/oom_score_adj", SIZE_MAX, &t, NULL);
2,409✔
1938
        if (r < 0)
2,409✔
1939
                return r;
1940

1941
        delete_trailing_chars(t, WHITESPACE);
2,409✔
1942

1943
        r = safe_atoi(t, &a);
2,409✔
1944
        if (r < 0)
2,409✔
1945
                return r;
1946

1947
        if (!oom_score_adjust_is_valid(a))
2,409✔
1948
                return -ENODATA;
1949

1950
        if (ret)
2,409✔
1951
                *ret = a;
2,409✔
1952

1953
        return 0;
1954
}
1955

1956
static int rlimit_to_nice(rlim_t limit) {
2✔
1957
        if (limit <= 1)
2✔
1958
                return PRIO_MAX-1; /* i.e. 19 */
1959

1960
        if (limit >= -PRIO_MIN + PRIO_MAX)
2✔
1961
                return PRIO_MIN; /* i.e. -20 */
1962

1963
        return PRIO_MAX - (int) limit;
2✔
1964
}
1965

1966
int setpriority_closest(int priority) {
27✔
1967
        struct rlimit highest;
27✔
1968
        int r, current, limit;
27✔
1969

1970
        /* Try to set requested nice level */
1971
        r = RET_NERRNO(setpriority(PRIO_PROCESS, 0, priority));
27✔
1972
        if (r >= 0)
2✔
1973
                return 1;
25✔
1974
        if (!ERRNO_IS_NEG_PRIVILEGE(r))
2✔
1975
                return r;
1976

1977
        errno = 0;
2✔
1978
        current = getpriority(PRIO_PROCESS, 0);
2✔
1979
        if (errno != 0)
2✔
1980
                return -errno;
×
1981

1982
        if (priority == current)
2✔
1983
                return 1;
1984

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

1991
        if (getrlimit(RLIMIT_NICE, &highest) < 0)
2✔
1992
                return -errno;
×
1993

1994
        limit = rlimit_to_nice(highest.rlim_cur);
2✔
1995

1996
        /* Push to the allowed limit if we're higher than that. Note that we could also be less nice than
1997
         * limit allows us, but still higher than what's requested. In that case our current value is
1998
         * the best choice. */
1999
        if (current > limit)
2✔
2000
                if (setpriority(PRIO_PROCESS, 0, limit) < 0)
2✔
2001
                        return -errno;
×
2002

2003
        log_debug("Cannot set requested nice level (%i), using next best (%i).", priority, MIN(current, limit));
2✔
2004
        return 0;
2005
}
2006

2007
_noreturn_ void freeze(void) {
×
2008
        log_close();
×
2009

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

2015
        /* Let's not freeze right away, but keep reaping zombies. */
2016
        for (;;) {
×
2017
                siginfo_t si = {};
×
2018

2019
                if (waitid(P_ALL, 0, &si, WEXITED) < 0 && errno != EINTR)
×
2020
                        break;
2021
        }
2022

2023
        /* waitid() failed with an ECHLD error (because there are no left-over child processes) or any other
2024
         * (unexpected) error. Freeze for good now! */
2025
        for (;;)
×
2026
                pause();
×
2027
}
2028

2029
int get_process_threads(pid_t pid) {
7✔
2030
        _cleanup_free_ char *t = NULL;
7✔
2031
        int n, r;
7✔
2032

2033
        if (pid < 0)
7✔
2034
                return -EINVAL;
2035

2036
        r = procfs_file_get_field(pid, "status", "Threads", &t);
7✔
2037
        if (r == -ENOENT)
7✔
2038
                return -ESRCH;
2039
        if (r < 0)
7✔
2040
                return r;
2041

2042
        r = safe_atoi(t, &n);
7✔
2043
        if (r < 0)
7✔
2044
                return r;
2045
        if (n < 0)
7✔
2046
                return -EINVAL;
×
2047

2048
        return n;
2049
}
2050

2051
int is_reaper_process(void) {
3,625✔
2052
        int b = 0;
3,625✔
2053

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

2057
        if (getpid_cached() == 1)
3,625✔
2058
                return true;
3,625✔
2059

2060
        if (prctl(PR_GET_CHILD_SUBREAPER, (unsigned long) &b, 0UL, 0UL, 0UL) < 0)
351✔
2061
                return -errno;
×
2062

2063
        return b != 0;
351✔
2064
}
2065

2066
int make_reaper_process(bool b) {
666✔
2067

2068
        if (getpid_cached() == 1) {
666✔
2069

2070
                if (!b)
52✔
2071
                        return -EINVAL;
2072

2073
                return 0;
52✔
2074
        }
2075

2076
        /* Some prctl()s insist that all 5 arguments are specified, others do not. Let's always specify all,
2077
         * to avoid any ambiguities */
2078
        if (prctl(PR_SET_CHILD_SUBREAPER, (unsigned long) b, 0UL, 0UL, 0UL) < 0)
614✔
2079
                return -errno;
×
2080

2081
        return 0;
2082
}
2083

2084
DEFINE_TRIVIAL_CLEANUP_FUNC_FULL(posix_spawnattr_t*, posix_spawnattr_destroy, NULL);
×
2085

2086
int posix_spawn_wrapper(
2,430✔
2087
                const char *path,
2088
                char * const *argv,
2089
                char * const *envp,
2090
                const char *cgroup,
2091
                PidRef *ret_pidref) {
2092

2093
        short flags = POSIX_SPAWN_SETSIGMASK;
2,430✔
2094
        posix_spawnattr_t attr;
2,430✔
2095
        sigset_t mask;
2,430✔
2096
        int r;
2,430✔
2097

2098
        /* Forks and invokes 'path' with 'argv' and 'envp' using CLONE_VM and CLONE_VFORK, which means the
2099
         * caller will be blocked until the child either exits or exec's. The memory of the child will be
2100
         * fully shared with the memory of the parent, so that there are no copy-on-write or memory.max
2101
         * issues.
2102
         *
2103
         * Also, move the newly-created process into 'cgroup' through POSIX_SPAWN_SETCGROUP (clone3())
2104
         * if available.
2105
         * returns 1: We're already in the right cgroup
2106
         *         0: 'cgroup' not specified or POSIX_SPAWN_SETCGROUP is not supported. The caller
2107
         *            needs to call 'cg_attach' on their own */
2108

2109
        assert(path);
2,430✔
2110
        assert(argv);
2,430✔
2111
        assert(ret_pidref);
2,430✔
2112

2113
        assert_se(sigfillset(&mask) >= 0);
2,430✔
2114

2115
        r = posix_spawnattr_init(&attr);
2,430✔
2116
        if (r != 0)
2,430✔
2117
                return -r; /* These functions return a positive errno on failure */
2,430✔
2118

2119
        /* Initialization needs to succeed before we can set up a destructor. */
2120
        _unused_ _cleanup_(posix_spawnattr_destroyp) posix_spawnattr_t *attr_destructor = &attr;
4,860✔
2121

2122
#if HAVE_PIDFD_SPAWN
2123
        static bool have_clone_into_cgroup = true; /* kernel 5.7+ */
2,430✔
2124
        _cleanup_close_ int cgroup_fd = -EBADF;
2,430✔
2125

2126
        if (cgroup && have_clone_into_cgroup) {
2,430✔
2127
                _cleanup_free_ char *resolved_cgroup = NULL;
2,430✔
2128

2129
                r = cg_get_path(cgroup, /* suffix= */ NULL, &resolved_cgroup);
2,430✔
2130
                if (r < 0)
2,430✔
2131
                        return r;
2132

2133
                cgroup_fd = open(resolved_cgroup, O_PATH|O_DIRECTORY|O_CLOEXEC);
2,430✔
2134
                if (cgroup_fd < 0)
2,430✔
2135
                        return -errno;
×
2136

2137
                r = posix_spawnattr_setcgroup_np(&attr, cgroup_fd);
2,430✔
2138
                if (r != 0)
2,430✔
2139
                        return -r;
×
2140

2141
                flags |= POSIX_SPAWN_SETCGROUP;
2,430✔
2142
        }
2143
#endif
2144

2145
        r = posix_spawnattr_setflags(&attr, flags);
2,430✔
2146
        if (r != 0)
2,430✔
2147
                return -r;
×
2148
        r = posix_spawnattr_setsigmask(&attr, &mask);
2,430✔
2149
        if (r != 0)
2,430✔
2150
                return -r;
×
2151

2152
#if HAVE_PIDFD_SPAWN
2153
        _cleanup_close_ int pidfd = -EBADF;
2,430✔
2154

2155
        r = pidfd_spawn(&pidfd, path, NULL, &attr, argv, envp);
2,430✔
2156
        if (ERRNO_IS_NOT_SUPPORTED(r) && FLAGS_SET(flags, POSIX_SPAWN_SETCGROUP) && cg_is_threaded(cgroup) > 0)
2,430✔
2157
                return -EUCLEAN; /* clone3() could also return EOPNOTSUPP if the target cgroup is in threaded mode,
2158
                                    turn that into something recognizable */
2159
        if ((ERRNO_IS_NOT_SUPPORTED(r) || ERRNO_IS_PRIVILEGE(r) || r == E2BIG) &&
2,430✔
2160
            FLAGS_SET(flags, POSIX_SPAWN_SETCGROUP)) {
2161
                /* Compiled on a newer host, or seccomp&friends blocking clone3()? Fallback, but
2162
                 * need to disable POSIX_SPAWN_SETCGROUP, which is what redirects to clone3().
2163
                 * Note that we might get E2BIG here since some kernels (e.g. 5.4) support clone3()
2164
                 * but not CLONE_INTO_CGROUP. */
2165

2166
                /* CLONE_INTO_CGROUP definitely won't work, hence remember the fact so that we don't
2167
                 * retry every time. */
2168
                have_clone_into_cgroup = false;
×
2169

2170
                flags &= ~POSIX_SPAWN_SETCGROUP;
×
2171
                r = posix_spawnattr_setflags(&attr, flags);
×
2172
                if (r != 0)
×
2173
                        return -r;
×
2174

2175
                r = pidfd_spawn(&pidfd, path, NULL, &attr, argv, envp);
×
2176
        }
2177
        if (r != 0)
2,430✔
2178
                return -r;
×
2179

2180
        r = pidref_set_pidfd_consume(ret_pidref, TAKE_FD(pidfd));
2,430✔
2181
        if (r < 0)
2,430✔
2182
                return r;
2183

2184
        return FLAGS_SET(flags, POSIX_SPAWN_SETCGROUP);
2,430✔
2185
#else
2186
        pid_t pid;
2187

2188
        r = posix_spawn(&pid, path, NULL, &attr, argv, envp);
2189
        if (r != 0)
2190
                return -r;
2191

2192
        r = pidref_set_pid(ret_pidref, pid);
2193
        if (r < 0)
2194
                return r;
2195

2196
        return 0; /* We did not use CLONE_INTO_CGROUP so return 0, the caller will have to move the child */
2197
#endif
2198
}
2199

2200
int proc_dir_open(DIR **ret) {
13✔
2201
        DIR *d;
13✔
2202

2203
        assert(ret);
13✔
2204

2205
        d = opendir("/proc");
13✔
2206
        if (!d)
13✔
2207
                return -errno;
×
2208

2209
        *ret = d;
13✔
2210
        return 0;
13✔
2211
}
2212

2213
int proc_dir_read(DIR *d, pid_t *ret) {
1,175✔
2214
        assert(d);
1,175✔
2215

2216
        for (;;) {
1,959✔
2217
                struct dirent *de;
1,959✔
2218

2219
                errno = 0;
1,959✔
2220
                de = readdir_no_dot(d);
1,959✔
2221
                if (!de) {
1,959✔
2222
                        if (errno != 0)
13✔
2223
                                return -errno;
×
2224

2225
                        break;
13✔
2226
                }
2227

2228
                if (!IN_SET(de->d_type, DT_DIR, DT_UNKNOWN))
1,946✔
2229
                        continue;
641✔
2230

2231
                if (parse_pid(de->d_name, ret) >= 0)
1,305✔
2232
                        return 1;
2233
        }
2234

2235
        if (ret)
13✔
2236
                *ret = 0;
13✔
2237
        return 0;
2238
}
2239

2240
int proc_dir_read_pidref(DIR *d, PidRef *ret) {
1,135✔
2241
        int r;
1,135✔
2242

2243
        assert(d);
1,135✔
2244

2245
        for (;;) {
1,135✔
2246
                pid_t pid;
1,135✔
2247

2248
                r = proc_dir_read(d, &pid);
1,135✔
2249
                if (r < 0)
1,135✔
2250
                        return r;
1,123✔
2251
                if (r == 0)
1,135✔
2252
                        break;
2253

2254
                r = pidref_set_pid(ret, pid);
1,123✔
2255
                if (r == -ESRCH) /* gone by now? skip it */
1,123✔
2256
                        continue;
×
2257
                if (r < 0)
1,123✔
2258
                        return r;
×
2259

2260
                return 1;
2261
        }
2262

2263
        if (ret)
12✔
2264
                *ret = PIDREF_NULL;
12✔
2265
        return 0;
2266
}
2267

2268
static const char *const sigchld_code_table[] = {
2269
        [CLD_EXITED] = "exited",
2270
        [CLD_KILLED] = "killed",
2271
        [CLD_DUMPED] = "dumped",
2272
        [CLD_TRAPPED] = "trapped",
2273
        [CLD_STOPPED] = "stopped",
2274
        [CLD_CONTINUED] = "continued",
2275
};
2276

2277
DEFINE_STRING_TABLE_LOOKUP(sigchld_code, int);
8,821✔
2278

2279
static const char* const sched_policy_table[] = {
2280
        [SCHED_OTHER] = "other",
2281
        [SCHED_BATCH] = "batch",
2282
        [SCHED_IDLE]  = "idle",
2283
        [SCHED_FIFO]  = "fifo",
2284
        [SCHED_EXT]   = "ext",
2285
        [SCHED_RR]    = "rr",
2286
};
2287

2288
DEFINE_STRING_TABLE_LOOKUP_WITH_FALLBACK(sched_policy, int, INT_MAX);
53✔
2289

2290
_noreturn_ void report_errno_and_exit(int errno_fd, int error) {
64✔
2291
        int r;
64✔
2292

2293
        if (error >= 0)
64✔
2294
                _exit(EXIT_SUCCESS);
63✔
2295

2296
        assert(errno_fd >= 0);
1✔
2297

2298
        r = loop_write(errno_fd, &error, sizeof(error));
1✔
2299
        if (r < 0)
1✔
2300
                log_debug_errno(r, "Failed to write errno to errno_fd=%d: %m", errno_fd);
×
2301

2302
        _exit(EXIT_FAILURE);
1✔
2303
}
2304

2305
int read_errno(int errno_fd) {
132✔
2306
        int r;
132✔
2307

2308
        assert(errno_fd >= 0);
132✔
2309

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

2313
        ssize_t n = loop_read(errno_fd, &r, sizeof(r), /* do_poll= */ false);
132✔
2314
        if (n < 0) {
132✔
2315
                log_debug_errno(n, "Failed to read errno: %m");
×
2316
                return -EIO;
×
2317
        }
2318
        if (n == 0) /* the process exited without reporting an error, assuming success */
132✔
2319
                return 0;
2320
        if (n != sizeof(r))
×
2321
                return log_debug_errno(SYNTHETIC_ERRNO(EIO), "Received unexpected amount of bytes (%zi) while reading errno.", n);
×
2322

2323
        if (r == 0)
×
2324
                return 0;
2325
        if (r < 0) /* child process reported an error, return it */
×
2326
                return log_debug_errno(r, "Child process failed with errno: %m");
×
2327

2328
        return log_debug_errno(SYNTHETIC_ERRNO(EIO), "Received positive errno from child, refusing: %d", r);
×
2329
}
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