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

systemd / systemd / 14766779411

30 Apr 2025 04:55PM UTC coverage: 72.225% (-0.06%) from 72.282%
14766779411

push

github

web-flow
wait-online: handle varlink connection errors while waiting for DNS (#37283)

Currently, if systemd-networkd-wait-online is started with --dns, and
systemd-resolved is not running, it will exit with an error right away.
Similarly, if systemd-resolved is restarted while waiting for DNS
configuration, systemd-networkd-wait-online will not attempt to
re-connect, and will potentially never see subsequent DNS
configurations.

Improve this by adding socket units for the systemd-resolved varlink
servers, and re-establish the connection in systemd-networkd-wait-online
when we receive `SD_VARLINK_ERROR_DISCONNECTED`.

8 of 16 new or added lines in 2 files covered. (50.0%)

5825 existing lines in 217 files now uncovered.

297168 of 411450 relevant lines covered (72.22%)

695892.62 hits per line

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

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

3
#include <ctype.h>
4
#include <errno.h>
5
#include <limits.h>
6
#include <linux/oom.h>
7
#include <pthread.h>
8
#include <spawn.h>
9
#include <stdbool.h>
10
#include <stdio.h>
11
#include <stdlib.h>
12
#include <sys/mount.h>
13
#include <sys/personality.h>
14
#include <sys/prctl.h>
15
#include <sys/types.h>
16
#include <sys/wait.h>
17
#include <syslog.h>
18
#include <threads.h>
19
#include <unistd.h>
20
#if HAVE_VALGRIND_VALGRIND_H
21
#include <valgrind/valgrind.h>
22
#endif
23

24
#include "sd-messages.h"
25

26
#include "alloc-util.h"
27
#include "architecture.h"
28
#include "argv-util.h"
29
#include "cgroup-util.h"
30
#include "dirent-util.h"
31
#include "env-file.h"
32
#include "env-util.h"
33
#include "errno-util.h"
34
#include "escape.h"
35
#include "fd-util.h"
36
#include "fileio.h"
37
#include "fs-util.h"
38
#include "hostname-util.h"
39
#include "io-util.h"
40
#include "iovec-util.h"
41
#include "locale-util.h"
42
#include "log.h"
43
#include "macro.h"
44
#include "memory-util.h"
45
#include "missing_sched.h"
46
#include "missing_syscall.h"
47
#include "mountpoint-util.h"
48
#include "namespace-util.h"
49
#include "nulstr-util.h"
50
#include "parse-util.h"
51
#include "path-util.h"
52
#include "pidfd-util.h"
53
#include "process-util.h"
54
#include "raw-clone.h"
55
#include "rlimit-util.h"
56
#include "signal-util.h"
57
#include "socket-util.h"
58
#include "stat-util.h"
59
#include "stdio-util.h"
60
#include "string-table.h"
61
#include "string-util.h"
62
#include "terminal-util.h"
63
#include "time-util.h"
64
#include "user-util.h"
65
#include "utf8.h"
66

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

72
static int get_process_state(pid_t pid) {
9,983✔
73
        _cleanup_free_ char *line = NULL;
9,983✔
74
        const char *p;
9,983✔
75
        char state;
9,983✔
76
        int r;
9,983✔
77

78
        assert(pid >= 0);
9,983✔
79

80
        /* Shortcut: if we are enquired about our own state, we are obviously running */
81
        if (pid == 0 || pid == getpid_cached())
9,983✔
82
                return (unsigned char) 'R';
×
83

84
        p = procfs_file_alloca(pid, "stat");
9,983✔
85

86
        r = read_one_line_file(p, &line);
9,983✔
87
        if (r == -ENOENT)
9,983✔
88
                return -ESRCH;
89
        if (r < 0)
8,019✔
90
                return r;
91

92
        p = strrchr(line, ')');
8,019✔
93
        if (!p)
8,019✔
94
                return -EIO;
95

96
        p++;
8,019✔
97

98
        if (sscanf(p, " %c", &state) != 1)
8,019✔
99
                return -EIO;
100

101
        return (unsigned char) state;
8,019✔
102
}
103

104
int pid_get_comm(pid_t pid, char **ret) {
105
        _cleanup_free_ char *escaped = NULL, *comm = NULL;
46,377✔
106
        int r;
46,377✔
107

108
        assert(pid >= 0);
46,377✔
109
        assert(ret);
46,377✔
110

111
        if (pid == 0 || pid == getpid_cached()) {
46,377✔
112
                comm = new0(char, TASK_COMM_LEN + 1); /* Must fit in 16 byte according to prctl(2) */
25,553✔
113
                if (!comm)
25,553✔
114
                        return -ENOMEM;
115

116
                if (prctl(PR_GET_NAME, comm) < 0)
25,553✔
117
                        return -errno;
×
118
        } else {
119
                const char *p;
20,824✔
120

121
                p = procfs_file_alloca(pid, "comm");
20,824✔
122

123
                /* Note that process names of kernel threads can be much longer than TASK_COMM_LEN */
124
                r = read_one_line_file(p, &comm);
20,824✔
125
                if (r == -ENOENT)
20,824✔
126
                        return -ESRCH;
127
                if (r < 0)
17,210✔
128
                        return r;
129
        }
130

131
        escaped = new(char, COMM_MAX_LEN);
42,762✔
132
        if (!escaped)
42,762✔
133
                return -ENOMEM;
134

135
        /* Escape unprintable characters, just in case, but don't grow the string beyond the underlying size */
136
        cellescape(escaped, COMM_MAX_LEN, comm);
42,762✔
137

138
        *ret = TAKE_PTR(escaped);
42,762✔
139
        return 0;
42,762✔
140
}
141

142
int pidref_get_comm(const PidRef *pid, char **ret) {
143
        _cleanup_free_ char *comm = NULL;
30✔
144
        int r;
30✔
145

146
        if (!pidref_is_set(pid))
30✔
147
                return -ESRCH;
148

149
        if (pidref_is_remote(pid))
60✔
150
                return -EREMOTE;
151

152
        r = pid_get_comm(pid->pid, &comm);
30✔
153
        if (r < 0)
30✔
154
                return r;
155

156
        r = pidref_verify(pid);
30✔
157
        if (r < 0)
30✔
158
                return r;
159

160
        if (ret)
30✔
161
                *ret = TAKE_PTR(comm);
30✔
162
        return 0;
163
}
164

165
static int pid_get_cmdline_nulstr(
17,427✔
166
                pid_t pid,
167
                size_t max_size,
168
                ProcessCmdlineFlags flags,
169
                char **ret,
170
                size_t *ret_size) {
171

172
        _cleanup_free_ char *t = NULL;
17,427✔
173
        const char *p;
17,427✔
174
        size_t k;
17,427✔
175
        int r;
17,427✔
176

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

188
        p = procfs_file_alloca(pid, "cmdline");
17,427✔
189
        r = read_virtual_file(p, max_size, &t, &k); /* Let's assume that each input byte results in >= 1
17,427✔
190
                                                     * columns of output. We ignore zero-width codepoints. */
191
        if (r == -ENOENT)
17,427✔
192
                return -ESRCH;
193
        if (r < 0)
14,463✔
194
                return r;
195

196
        if (k == 0) {
14,462✔
197
                if (!(flags & PROCESS_CMDLINE_COMM_FALLBACK))
644✔
198
                        return -ENOENT;
623✔
199

200
                /* Kernel threads have no argv[] */
201
                _cleanup_free_ char *comm = NULL;
21✔
202

203
                r = pid_get_comm(pid, &comm);
21✔
204
                if (r < 0)
21✔
205
                        return r;
206

207
                free(t);
21✔
208
                t = strjoin("[", comm, "]");
21✔
209
                if (!t)
21✔
210
                        return -ENOMEM;
211

212
                k = strlen(t);
21✔
213
                r = k <= max_size;
21✔
214
                if (r == 0) /* truncation */
21✔
215
                        t[max_size] = '\0';
12✔
216
        }
217

218
        if (ret)
13,839✔
219
                *ret = TAKE_PTR(t);
13,839✔
220
        if (ret_size)
13,839✔
221
                *ret_size = k;
13,839✔
222

223
        return r;
224
}
225

226
int pid_get_cmdline(pid_t pid, size_t max_columns, ProcessCmdlineFlags flags, char **ret) {
227
        _cleanup_free_ char *t = NULL;
12,584✔
228
        size_t k;
12,584✔
229
        char *ans;
12,584✔
230

231
        assert(pid >= 0);
12,584✔
232
        assert(ret);
12,584✔
233

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

248
        int full = pid_get_cmdline_nulstr(pid, max_columns, flags, &t, &k);
12,584✔
249
        if (full < 0)
12,584✔
250
                return full;
251

252
        if (flags & (PROCESS_CMDLINE_QUOTE | PROCESS_CMDLINE_QUOTE_POSIX)) {
9,068✔
253
                ShellEscapeFlags shflags = SHELL_ESCAPE_EMPTY |
8,669✔
254
                        FLAGS_SET(flags, PROCESS_CMDLINE_QUOTE_POSIX) * SHELL_ESCAPE_POSIX;
8,669✔
255

256
                assert(!(flags & PROCESS_CMDLINE_USE_LOCALE));
8,669✔
257

258
                _cleanup_strv_free_ char **args = NULL;
8,669✔
259

260
                /* Drop trailing NULs, otherwise strv_parse_nulstr() adds additional empty strings at the end.
261
                 * See also issue #21186. */
262
                args = strv_parse_nulstr_full(t, k, /* drop_trailing_nuls = */ true);
8,669✔
263
                if (!args)
8,669✔
264
                        return -ENOMEM;
265

266
                ans = quote_command_line(args, shflags);
8,669✔
267
                if (!ans)
8,669✔
268
                        return -ENOMEM;
269
        } else {
270
                /* Arguments are separated by NULs. Let's replace those with spaces. */
271
                for (size_t i = 0; i < k - 1; i++)
19,388✔
272
                        if (t[i] == '\0')
18,989✔
273
                                t[i] = ' ';
705✔
274

275
                delete_trailing_chars(t, WHITESPACE);
399✔
276

277
                bool eight_bit = (flags & PROCESS_CMDLINE_USE_LOCALE) && !is_locale_utf8();
399✔
278

279
                ans = escape_non_printable_full(t, max_columns,
1,197✔
280
                                                eight_bit * XESCAPE_8_BIT | !full * XESCAPE_FORCE_ELLIPSIS);
744✔
281
                if (!ans)
399✔
282
                        return -ENOMEM;
283

284
                ans = str_realloc(ans);
399✔
285
        }
286

287
        *ret = ans;
9,068✔
288
        return 0;
9,068✔
289
}
290

291
int pidref_get_cmdline(const PidRef *pid, size_t max_columns, ProcessCmdlineFlags flags, char **ret) {
292
        _cleanup_free_ char *s = NULL;
99✔
293
        int r;
99✔
294

295
        if (!pidref_is_set(pid))
99✔
296
                return -ESRCH;
297

298
        if (pidref_is_remote(pid))
198✔
299
                return -EREMOTE;
300

301
        r = pid_get_cmdline(pid->pid, max_columns, flags, &s);
99✔
302
        if (r < 0)
99✔
303
                return r;
304

305
        r = pidref_verify(pid);
99✔
306
        if (r < 0)
99✔
307
                return r;
308

309
        if (ret)
99✔
310
                *ret = TAKE_PTR(s);
99✔
311
        return 0;
312
}
313

314
int pid_get_cmdline_strv(pid_t pid, ProcessCmdlineFlags flags, char ***ret) {
315
        _cleanup_free_ char *t = NULL;
4,843✔
316
        char **args;
4,843✔
317
        size_t k;
4,843✔
318
        int r;
4,843✔
319

320
        assert(pid >= 0);
4,843✔
321
        assert((flags & ~PROCESS_CMDLINE_COMM_FALLBACK) == 0);
4,843✔
322
        assert(ret);
4,843✔
323

324
        r = pid_get_cmdline_nulstr(pid, SIZE_MAX, flags, &t, &k);
4,843✔
325
        if (r < 0)
4,843✔
326
                return r;
327

328
        args = strv_parse_nulstr_full(t, k, /* drop_trailing_nuls = */ true);
4,771✔
329
        if (!args)
4,771✔
330
                return -ENOMEM;
331

332
        *ret = args;
4,771✔
333
        return 0;
4,771✔
334
}
335

336
int pidref_get_cmdline_strv(const PidRef *pid, ProcessCmdlineFlags flags, char ***ret) {
337
        _cleanup_strv_free_ char **args = NULL;
×
338
        int r;
×
339

340
        if (!pidref_is_set(pid))
×
341
                return -ESRCH;
342

343
        if (pidref_is_remote(pid))
×
344
                return -EREMOTE;
345

346
        r = pid_get_cmdline_strv(pid->pid, flags, &args);
×
347
        if (r < 0)
×
348
                return r;
349

350
        r = pidref_verify(pid);
×
351
        if (r < 0)
×
352
                return r;
353

354
        if (ret)
×
355
                *ret = TAKE_PTR(args);
×
356

357
        return 0;
358
}
359

360
int container_get_leader(const char *machine, pid_t *pid) {
361
        _cleanup_free_ char *s = NULL, *class = NULL;
10✔
362
        const char *p;
10✔
363
        pid_t leader;
10✔
364
        int r;
10✔
365

366
        assert(machine);
10✔
367
        assert(pid);
10✔
368

369
        if (streq(machine, ".host")) {
10✔
370
                *pid = 1;
1✔
371
                return 0;
1✔
372
        }
373

374
        if (!hostname_is_valid(machine, 0))
9✔
375
                return -EINVAL;
376

377
        p = strjoina("/run/systemd/machines/", machine);
45✔
378
        r = parse_env_file(NULL, p,
9✔
379
                           "LEADER", &s,
380
                           "CLASS", &class);
381
        if (r == -ENOENT)
9✔
382
                return -EHOSTDOWN;
383
        if (r < 0)
9✔
384
                return r;
385
        if (!s)
9✔
386
                return -EIO;
387

388
        if (!streq_ptr(class, "container"))
9✔
389
                return -EIO;
390

391
        r = parse_pid(s, &leader);
9✔
392
        if (r < 0)
9✔
393
                return r;
394
        if (leader <= 1)
9✔
395
                return -EIO;
396

397
        *pid = leader;
9✔
398
        return 0;
9✔
399
}
400

401
int pid_is_kernel_thread(pid_t pid) {
402
        _cleanup_free_ char *line = NULL;
3,509✔
403
        unsigned long long flags;
3,509✔
404
        size_t l, i;
3,509✔
405
        const char *p;
3,509✔
406
        char *q;
3,509✔
407
        int r;
3,509✔
408

409
        if (IN_SET(pid, 0, 1) || pid == getpid_cached()) /* pid 1, and we ourselves certainly aren't a kernel thread */
3,509✔
410
                return 0;
25✔
411
        if (!pid_is_valid(pid))
3,484✔
412
                return -EINVAL;
413

414
        p = procfs_file_alloca(pid, "stat");
3,484✔
415
        r = read_one_line_file(p, &line);
3,484✔
416
        if (r == -ENOENT)
3,484✔
417
                return -ESRCH;
418
        if (r < 0)
3,484✔
419
                return r;
420

421
        /* Skip past the comm field */
422
        q = strrchr(line, ')');
3,484✔
423
        if (!q)
3,484✔
424
                return -EINVAL;
425
        q++;
3,484✔
426

427
        /* Skip 6 fields to reach the flags field */
428
        for (i = 0; i < 6; i++) {
24,388✔
429
                l = strspn(q, WHITESPACE);
20,904✔
430
                if (l < 1)
20,904✔
431
                        return -EINVAL;
432
                q += l;
20,904✔
433

434
                l = strcspn(q, WHITESPACE);
20,904✔
435
                if (l < 1)
20,904✔
436
                        return -EINVAL;
437
                q += l;
20,904✔
438
        }
439

440
        /* Skip preceding whitespace */
441
        l = strspn(q, WHITESPACE);
3,484✔
442
        if (l < 1)
3,484✔
443
                return -EINVAL;
444
        q += l;
3,484✔
445

446
        /* Truncate the rest */
447
        l = strcspn(q, WHITESPACE);
3,484✔
448
        if (l < 1)
3,484✔
449
                return -EINVAL;
450
        q[l] = 0;
3,484✔
451

452
        r = safe_atollu(q, &flags);
3,484✔
453
        if (r < 0)
3,484✔
454
                return r;
455

456
        return !!(flags & PF_KTHREAD);
3,484✔
457
}
458

459
int pidref_is_kernel_thread(const PidRef *pid) {
460
        int result, r;
1,437✔
461

462
        if (!pidref_is_set(pid))
1,437✔
463
                return -ESRCH;
464

465
        if (pidref_is_remote(pid))
1,437✔
466
                return -EREMOTE;
467

468
        result = pid_is_kernel_thread(pid->pid);
1,437✔
469
        if (result < 0)
1,437✔
470
                return result;
471

472
        r = pidref_verify(pid); /* Verify that the PID wasn't reused since */
1,437✔
473
        if (r < 0)
1,437✔
474
                return r;
9✔
475

476
        return result;
477
}
478

479
static int get_process_link_contents(pid_t pid, const char *proc_file, char **ret) {
11,955✔
480
        const char *p;
11,955✔
481
        int r;
11,955✔
482

483
        assert(proc_file);
11,955✔
484

485
        p = procfs_file_alloca(pid, proc_file);
11,955✔
486

487
        r = readlink_malloc(p, ret);
11,955✔
488
        return (r == -ENOENT && proc_mounted() > 0) ? -ESRCH : r;
11,955✔
489
}
490

491
int get_process_exe(pid_t pid, char **ret) {
492
        char *d;
11,929✔
493
        int r;
11,929✔
494

495
        assert(pid >= 0);
11,929✔
496

497
        r = get_process_link_contents(pid, "exe", ret);
11,929✔
498
        if (r < 0)
11,929✔
499
                return r;
500

501
        if (ret) {
8,856✔
502
                d = endswith(*ret, " (deleted)");
8,856✔
503
                if (d)
8,856✔
504
                        *d = '\0';
×
505
        }
506

507
        return 0;
508
}
509

510
static int get_process_id(pid_t pid, const char *field, uid_t *ret) {
5,342✔
511
        _cleanup_fclose_ FILE *f = NULL;
5,342✔
512
        const char *p;
5,342✔
513
        int r;
5,342✔
514

515
        assert(field);
5,342✔
516
        assert(ret);
5,342✔
517

518
        if (pid < 0)
5,342✔
519
                return -EINVAL;
520

521
        p = procfs_file_alloca(pid, "status");
5,342✔
522
        r = fopen_unlocked(p, "re", &f);
5,342✔
523
        if (r == -ENOENT)
5,342✔
524
                return -ESRCH;
525
        if (r < 0)
262✔
526
                return r;
527

528
        for (;;) {
4,698✔
529
                _cleanup_free_ char *line = NULL;
2,480✔
530
                char *l;
2,480✔
531

532
                r = read_stripped_line(f, LONG_LINE_MAX, &line);
2,480✔
533
                if (r < 0)
2,480✔
534
                        return r;
535
                if (r == 0)
2,480✔
536
                        break;
537

538
                l = startswith(line, field);
2,480✔
539
                if (l) {
2,480✔
540
                        l += strspn(l, WHITESPACE);
262✔
541

542
                        l[strcspn(l, WHITESPACE)] = 0;
262✔
543

544
                        return parse_uid(l, ret);
262✔
545
                }
546
        }
547

548
        return -EIO;
×
549
}
550

551
int pid_get_uid(pid_t pid, uid_t *ret) {
552
        assert(ret);
2,678✔
553

554
        if (pid == 0 || pid == getpid_cached()) {
2,678✔
555
                *ret = getuid();
4✔
556
                return 0;
4✔
557
        }
558

559
        return get_process_id(pid, "Uid:", ret);
2,674✔
560
}
561

562
int pidref_get_uid(const PidRef *pid, uid_t *ret) {
563
        int r;
54✔
564

565
        if (!pidref_is_set(pid))
54✔
566
                return -ESRCH;
54✔
567

568
        if (pidref_is_remote(pid))
54✔
569
                return -EREMOTE;
570

571
        if (pid->fd >= 0) {
54✔
572
                r = pidfd_get_uid(pid->fd, ret);
54✔
573
                if (!ERRNO_IS_NEG_NOT_SUPPORTED(r))
54✔
574
                        return r;
575
        }
576

577
        uid_t uid;
9✔
578
        r = pid_get_uid(pid->pid, &uid);
9✔
579
        if (r < 0)
9✔
580
                return r;
581

582
        r = pidref_verify(pid);
9✔
583
        if (r < 0)
9✔
584
                return r;
585

586
        if (ret)
9✔
587
                *ret = uid;
9✔
588
        return 0;
589
}
590

591
int get_process_gid(pid_t pid, gid_t *ret) {
592

593
        if (pid == 0 || pid == getpid_cached()) {
2,669✔
594
                *ret = getgid();
1✔
595
                return 0;
1✔
596
        }
597

598
        assert_cc(sizeof(uid_t) == sizeof(gid_t));
2,668✔
599
        return get_process_id(pid, "Gid:", ret);
2,668✔
600
}
601

602
int get_process_cwd(pid_t pid, char **ret) {
603
        assert(pid >= 0);
13✔
604

605
        if (pid == 0 || pid == getpid_cached())
13✔
606
                return safe_getcwd(ret);
×
607

608
        return get_process_link_contents(pid, "cwd", ret);
13✔
609
}
610

611
int get_process_root(pid_t pid, char **ret) {
612
        assert(pid >= 0);
13✔
613
        return get_process_link_contents(pid, "root", ret);
13✔
614
}
615

616
#define ENVIRONMENT_BLOCK_MAX (5U*1024U*1024U)
617

618
int get_process_environ(pid_t pid, char **ret) {
619
        _cleanup_fclose_ FILE *f = NULL;
15✔
620
        _cleanup_free_ char *outcome = NULL;
15✔
621
        size_t sz = 0;
15✔
622
        const char *p;
15✔
623
        int r;
15✔
624

625
        assert(pid >= 0);
15✔
626
        assert(ret);
15✔
627

628
        p = procfs_file_alloca(pid, "environ");
15✔
629

630
        r = fopen_unlocked(p, "re", &f);
15✔
631
        if (r == -ENOENT)
15✔
632
                return -ESRCH;
633
        if (r < 0)
15✔
634
                return r;
635

636
        for (;;) {
6,448✔
637
                char c;
6,463✔
638

639
                if (sz >= ENVIRONMENT_BLOCK_MAX)
6,463✔
640
                        return -ENOBUFS;
×
641

642
                if (!GREEDY_REALLOC(outcome, sz + 5))
6,463✔
643
                        return -ENOMEM;
644

645
                r = safe_fgetc(f, &c);
6,463✔
646
                if (r < 0)
6,463✔
647
                        return r;
648
                if (r == 0)
6,463✔
649
                        break;
650

651
                if (c == '\0')
6,448✔
652
                        outcome[sz++] = '\n';
227✔
653
                else
654
                        sz += cescape_char(c, outcome + sz);
6,221✔
655
        }
656

657
        outcome[sz] = '\0';
15✔
658
        *ret = TAKE_PTR(outcome);
15✔
659

660
        return 0;
15✔
661
}
662

663
int pid_get_ppid(pid_t pid, pid_t *ret) {
664
        _cleanup_free_ char *line = NULL;
1,391✔
665
        unsigned long ppid;
1,391✔
666
        const char *p;
1,391✔
667
        int r;
1,391✔
668

669
        assert(pid >= 0);
1,391✔
670

671
        if (pid == 0)
1,391✔
672
                pid = getpid_cached();
1✔
673
        if (pid == 1) /* PID 1 has no parent, shortcut this case */
1,391✔
674
                return -EADDRNOTAVAIL;
675

676
        if (pid == getpid_cached()) {
1,387✔
677
                if (ret)
6✔
678
                        *ret = getppid();
6✔
679
                return 0;
6✔
680
        }
681

682
        p = procfs_file_alloca(pid, "stat");
1,381✔
683
        r = read_one_line_file(p, &line);
1,381✔
684
        if (r == -ENOENT)
1,381✔
685
                return -ESRCH;
686
        if (r < 0)
1,380✔
687
                return r;
688

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

692
        p = strrchr(line, ')');
1,380✔
693
        if (!p)
1,380✔
694
                return -EIO;
695
        p++;
1,380✔
696

697
        if (sscanf(p, " "
1,380✔
698
                   "%*c "  /* state */
699
                   "%lu ", /* ppid */
700
                   &ppid) != 1)
701
                return -EIO;
702

703
        /* If ppid is zero the process has no parent. Which might be the case for PID 1 (caught above)
704
         * but also for processes originating in other namespaces that are inserted into a pidns.
705
         * Return a recognizable error in this case. */
706
        if (ppid == 0)
1,380✔
707
                return -EADDRNOTAVAIL;
708

709
        if ((pid_t) ppid < 0 || (unsigned long) (pid_t) ppid != ppid)
1,380✔
710
                return -ERANGE;
711

712
        if (ret)
1,380✔
713
                *ret = (pid_t) ppid;
1,380✔
714

715
        return 0;
716
}
717

718
int pidref_get_ppid(const PidRef *pidref, pid_t *ret) {
719
        int r;
2,399✔
720

721
        if (!pidref_is_set(pidref))
2,399✔
722
                return -ESRCH;
2,399✔
723

724
        if (pidref_is_remote(pidref))
2,399✔
725
                return -EREMOTE;
726

727
        if (pidref->fd >= 0) {
2,399✔
728
                r = pidfd_get_ppid(pidref->fd, ret);
2,399✔
729
                if (!ERRNO_IS_NEG_NOT_SUPPORTED(r))
2,399✔
730
                        return r;
731
        }
732

733
        pid_t ppid;
1,385✔
734
        r = pid_get_ppid(pidref->pid, ret ? &ppid : NULL);
1,385✔
735
        if (r < 0)
1,385✔
736
                return r;
737

738
        r = pidref_verify(pidref);
1,384✔
739
        if (r < 0)
1,384✔
740
                return r;
741

742
        if (ret)
1,384✔
743
                *ret = ppid;
1,384✔
744
        return 0;
745
}
746

747
int pidref_get_ppid_as_pidref(const PidRef *pidref, PidRef *ret) {
748
        pid_t ppid;
17✔
749
        int r;
17✔
750

751
        assert(ret);
17✔
752

753
        r = pidref_get_ppid(pidref, &ppid);
17✔
754
        if (r < 0)
17✔
755
                return r;
17✔
756

757
        for (unsigned attempt = 0; attempt < 16; attempt++) {
16✔
758
                _cleanup_(pidref_done) PidRef parent = PIDREF_NULL;
16✔
759

760
                r = pidref_set_pid(&parent, ppid);
16✔
761
                if (r < 0)
16✔
762
                        return r;
763

764
                /* If we have a pidfd of the original PID, let's verify that the process we acquired really
765
                 * is the parent still */
766
                if (pidref->fd >= 0) {
16✔
767
                        r = pidref_get_ppid(pidref, &ppid);
16✔
768
                        if (r < 0)
16✔
769
                                return r;
770

771
                        /* Did the PPID change since we queried it? if so we might have pinned the wrong
772
                         * process, if its PID got reused by now. Let's try again */
773
                        if (parent.pid != ppid)
16✔
774
                                continue;
×
775
                }
776

777
                *ret = TAKE_PIDREF(parent);
16✔
778
                return 0;
16✔
779
        }
780

781
        /* Give up after 16 tries */
782
        return -ENOTRECOVERABLE;
783
}
784

785
int pid_get_start_time(pid_t pid, usec_t *ret) {
786
        _cleanup_free_ char *line = NULL;
658✔
787
        const char *p;
658✔
788
        int r;
658✔
789

790
        assert(pid >= 0);
658✔
791

792
        p = procfs_file_alloca(pid, "stat");
658✔
793
        r = read_one_line_file(p, &line);
658✔
794
        if (r == -ENOENT)
658✔
795
                return -ESRCH;
796
        if (r < 0)
658✔
797
                return r;
798

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

802
        p = strrchr(line, ')');
658✔
803
        if (!p)
658✔
804
                return -EIO;
805
        p++;
658✔
806

807
        unsigned long llu;
658✔
808

809
        if (sscanf(p, " "
658✔
810
                   "%*c " /* state */
811
                   "%*u " /* ppid */
812
                   "%*u " /* pgrp */
813
                   "%*u " /* session */
814
                   "%*u " /* tty_nr */
815
                   "%*u " /* tpgid */
816
                   "%*u " /* flags */
817
                   "%*u " /* minflt */
818
                   "%*u " /* cminflt */
819
                   "%*u " /* majflt */
820
                   "%*u " /* cmajflt */
821
                   "%*u " /* utime */
822
                   "%*u " /* stime */
823
                   "%*u " /* cutime */
824
                   "%*u " /* cstime */
825
                   "%*i " /* priority */
826
                   "%*i " /* nice */
827
                   "%*u " /* num_threads */
828
                   "%*u " /* itrealvalue */
829
                   "%lu ", /* starttime */
830
                   &llu) != 1)
831
                return -EIO;
832

833
        if (ret)
658✔
834
                *ret = jiffies_to_usec(llu); /* CLOCK_BOOTTIME */
658✔
835

836
        return 0;
837
}
838

839
int pidref_get_start_time(const PidRef *pid, usec_t *ret) {
840
        usec_t t;
658✔
841
        int r;
658✔
842

843
        if (!pidref_is_set(pid))
658✔
844
                return -ESRCH;
658✔
845

846
        if (pidref_is_remote(pid))
658✔
847
                return -EREMOTE;
848

849
        r = pid_get_start_time(pid->pid, ret ? &t : NULL);
658✔
850
        if (r < 0)
658✔
851
                return r;
852

853
        r = pidref_verify(pid);
658✔
854
        if (r < 0)
658✔
855
                return r;
856

857
        if (ret)
658✔
858
                *ret = t;
658✔
859

860
        return 0;
861
}
862

863
int get_process_umask(pid_t pid, mode_t *ret) {
864
        _cleanup_free_ char *m = NULL;
18,693✔
865
        const char *p;
18,693✔
866
        int r;
18,693✔
867

868
        assert(pid >= 0);
18,693✔
869
        assert(ret);
18,693✔
870

871
        p = procfs_file_alloca(pid, "status");
18,693✔
872

873
        r = get_proc_field(p, "Umask", WHITESPACE, &m);
18,693✔
874
        if (r == -ENOENT)
18,693✔
875
                return -ESRCH;
876
        if (r < 0)
18,693✔
877
                return r;
878

879
        return parse_mode(m, ret);
18,693✔
880
}
881

882
int wait_for_terminate(pid_t pid, siginfo_t *ret) {
883
        return pidref_wait_for_terminate(&PIDREF_MAKE_FROM_PID(pid), ret);
821✔
884
}
885

886
/*
887
 * Return values:
888
 * < 0 : wait_for_terminate() failed to get the state of the
889
 *       process, the process was terminated by a signal, or
890
 *       failed for an unknown reason.
891
 * >=0 : The process terminated normally, and its exit code is
892
 *       returned.
893
 *
894
 * That is, success is indicated by a return value of zero, and an
895
 * error is indicated by a non-zero value.
896
 *
897
 * A warning is emitted if the process terminates abnormally,
898
 * and also if it returns non-zero unless check_exit_code is true.
899
 */
900
int pidref_wait_for_terminate_and_check(const char *name, PidRef *pidref, WaitFlags flags) {
901
        int r;
9,089✔
902

903
        if (!pidref_is_set(pidref))
9,089✔
904
                return -ESRCH;
9,089✔
905
        if (pidref_is_remote(pidref))
18,178✔
906
                return -EREMOTE;
907
        if (pidref->pid == 1 || pidref_is_self(pidref))
9,089✔
908
                return -ECHILD;
×
909

910
        _cleanup_free_ char *buffer = NULL;
9,089✔
911
        if (!name) {
9,089✔
912
                r = pidref_get_comm(pidref, &buffer);
×
913
                if (r < 0)
×
914
                        log_debug_errno(r, "Failed to acquire process name of " PID_FMT ", ignoring: %m", pidref->pid);
×
915
                else
916
                        name = buffer;
×
917
        }
918

919
        int prio = flags & WAIT_LOG_ABNORMAL ? LOG_ERR : LOG_DEBUG;
9,089✔
920

921
        siginfo_t status;
9,089✔
922
        r = pidref_wait_for_terminate(pidref, &status);
9,089✔
923
        if (r < 0)
9,089✔
924
                return log_full_errno(prio, r, "Failed to wait for %s: %m", strna(name));
×
925

926
        if (status.si_code == CLD_EXITED) {
9,089✔
927
                if (status.si_status != EXIT_SUCCESS)
9,089✔
928
                        log_full(flags & WAIT_LOG_NON_ZERO_EXIT_STATUS ? LOG_ERR : LOG_DEBUG,
48✔
929
                                 "%s failed with exit status %i.", strna(name), status.si_status);
930
                else
931
                        log_debug("%s succeeded.", name);
9,041✔
932

933
                return status.si_status;
9,089✔
934

935
        } else if (IN_SET(status.si_code, CLD_KILLED, CLD_DUMPED)) {
×
936

937
                log_full(prio, "%s terminated by signal %s.", strna(name), signal_to_string(status.si_status));
×
938
                return -EPROTO;
×
939
        }
940

UNCOV
941
        log_full(prio, "%s failed due to unknown reason.", strna(name));
×
942
        return -EPROTO;
943
}
944

945
int wait_for_terminate_and_check(const char *name, pid_t pid, WaitFlags flags) {
946
        return pidref_wait_for_terminate_and_check(name, &PIDREF_MAKE_FROM_PID(pid), flags);
7,970✔
947
}
948

949
/*
950
 * Return values:
951
 *
952
 * < 0 : wait_for_terminate_with_timeout() failed to get the state of the process, the process timed out, the process
953
 *       was terminated by a signal, or failed for an unknown reason.
954
 *
955
 * >=0 : The process terminated normally with no failures.
956
 *
957
 * Success is indicated by a return value of zero, a timeout is indicated by ETIMEDOUT, and all other child failure
958
 * states are indicated by error is indicated by a non-zero value.
959
 *
960
 * This call assumes SIGCHLD has been blocked already, in particular before the child to wait for has been forked off
961
 * to remain entirely race-free.
962
 */
963
int wait_for_terminate_with_timeout(pid_t pid, usec_t timeout) {
964
        sigset_t mask;
×
965
        int r;
×
966
        usec_t until;
×
967

968
        assert_se(sigemptyset(&mask) == 0);
×
969
        assert_se(sigaddset(&mask, SIGCHLD) == 0);
×
970

971
        /* Drop into a sigtimewait-based timeout. Waiting for the
972
         * pid to exit. */
973
        until = usec_add(now(CLOCK_MONOTONIC), timeout);
×
974
        for (;;) {
×
975
                usec_t n;
×
976
                siginfo_t status = {};
×
977

978
                n = now(CLOCK_MONOTONIC);
×
979
                if (n >= until)
×
980
                        break;
981

982
                r = RET_NERRNO(sigtimedwait(&mask, NULL, TIMESPEC_STORE(until - n)));
×
983
                /* Assuming we woke due to the child exiting. */
984
                if (waitid(P_PID, pid, &status, WEXITED|WNOHANG) == 0) {
×
985
                        if (status.si_pid == pid) {
×
986
                                /* This is the correct child. */
987
                                if (status.si_code == CLD_EXITED)
×
988
                                        return status.si_status == 0 ? 0 : -EPROTO;
×
989
                                else
990
                                        return -EPROTO;
991
                        }
992
                }
993
                /* Not the child, check for errors and proceed appropriately */
994
                if (r < 0) {
×
995
                        switch (r) {
×
996
                        case -EAGAIN:
997
                                /* Timed out, child is likely hung. */
998
                                return -ETIMEDOUT;
999
                        case -EINTR:
×
1000
                                /* Received a different signal and should retry */
1001
                                continue;
×
1002
                        default:
×
1003
                                /* Return any unexpected errors */
1004
                                return r;
×
1005
                        }
1006
                }
1007
        }
1008

1009
        return -EPROTO;
×
1010
}
1011

1012
void sigkill_wait(pid_t pid) {
1013
        assert(pid > 1);
37✔
1014

1015
        (void) kill(pid, SIGKILL);
37✔
1016
        (void) wait_for_terminate(pid, NULL);
37✔
1017
}
37✔
1018

1019
void sigkill_waitp(pid_t *pid) {
1020
        PROTECT_ERRNO;
11,749✔
1021

1022
        if (!pid)
11,749✔
1023
                return;
1024
        if (*pid <= 1)
11,749✔
1025
                return;
1026

1027
        sigkill_wait(*pid);
36✔
1028
}
1029

1030
void sigterm_wait(pid_t pid) {
1031
        assert(pid > 1);
65✔
1032

1033
        (void) kill_and_sigcont(pid, SIGTERM);
65✔
1034
        (void) wait_for_terminate(pid, NULL);
65✔
1035
}
65✔
1036

1037
void sigkill_nowait(pid_t pid) {
1038
        assert(pid > 1);
×
1039

1040
        (void) kill(pid, SIGKILL);
×
1041
}
×
1042

1043
void sigkill_nowaitp(pid_t *pid) {
1044
        PROTECT_ERRNO;
×
1045

1046
        if (!pid)
×
1047
                return;
1048
        if (*pid <= 1)
×
1049
                return;
1050

1051
        sigkill_nowait(*pid);
×
1052
}
1053

1054
int kill_and_sigcont(pid_t pid, int sig) {
1055
        int r;
65✔
1056

1057
        r = RET_NERRNO(kill(pid, sig));
65✔
1058

1059
        /* If this worked, also send SIGCONT, unless we already just sent a SIGCONT, or SIGKILL was sent which isn't
1060
         * affected by a process being suspended anyway. */
1061
        if (r >= 0 && !IN_SET(sig, SIGCONT, SIGKILL))
65✔
1062
                (void) kill(pid, SIGCONT);
65✔
1063

1064
        return r;
65✔
1065
}
1066

1067
int getenv_for_pid(pid_t pid, const char *field, char **ret) {
1068
        _cleanup_fclose_ FILE *f = NULL;
4,707✔
1069
        const char *path;
4,707✔
1070
        size_t sum = 0;
4,707✔
1071
        int r;
4,707✔
1072

1073
        assert(pid >= 0);
4,707✔
1074
        assert(field);
4,707✔
1075
        assert(ret);
4,707✔
1076

1077
        if (pid == 0 || pid == getpid_cached())
4,707✔
1078
                return strdup_to_full(ret, getenv(field));
13✔
1079

1080
        if (!pid_is_valid(pid))
4,694✔
1081
                return -EINVAL;
1082

1083
        path = procfs_file_alloca(pid, "environ");
4,694✔
1084

1085
        r = fopen_unlocked(path, "re", &f);
4,694✔
1086
        if (r == -ENOENT)
4,694✔
1087
                return -ESRCH;
1088
        if (r < 0)
4,229✔
1089
                return r;
1090

1091
        for (;;) {
46,469✔
1092
                _cleanup_free_ char *line = NULL;
21,813✔
1093
                const char *match;
24,658✔
1094

1095
                if (sum > ENVIRONMENT_BLOCK_MAX) /* Give up searching eventually */
24,658✔
1096
                        return -ENOBUFS;
1097

1098
                r = read_nul_string(f, LONG_LINE_MAX, &line);
24,658✔
1099
                if (r < 0)
24,658✔
1100
                        return r;
1101
                if (r == 0)  /* EOF */
24,658✔
1102
                        break;
1103

1104
                sum += r;
21,813✔
1105

1106
                match = startswith(line, field);
21,813✔
1107
                if (match && *match == '=')
21,813✔
1108
                        return strdup_to_full(ret, match + 1);
2✔
1109
        }
1110

1111
        *ret = NULL;
2,845✔
1112
        return 0;
2,845✔
1113
}
1114

1115
int pidref_is_my_child(PidRef *pid) {
1116
        int r;
2,365✔
1117

1118
        if (!pidref_is_set(pid))
2,365✔
1119
                return -ESRCH;
2,365✔
1120

1121
        if (pidref_is_remote(pid))
2,365✔
1122
                return -EREMOTE;
1123

1124
        if (pid->pid == 1 || pidref_is_self(pid))
2,365✔
1125
                return false;
×
1126

1127
        pid_t ppid;
2,365✔
1128
        r = pidref_get_ppid(pid, &ppid);
2,365✔
1129
        if (r == -EADDRNOTAVAIL) /* if this process is outside of our pidns, it is definitely not our child */
2,365✔
1130
                return false;
1131
        if (r < 0)
2,365✔
1132
                return r;
1133

1134
        return ppid == getpid_cached();
2,365✔
1135
}
1136

1137
int pid_is_my_child(pid_t pid) {
1138

1139
        if (pid == 0)
×
1140
                return false;
×
1141

1142
        return pidref_is_my_child(&PIDREF_MAKE_FROM_PID(pid));
×
1143
}
1144

1145
int pidref_is_unwaited(PidRef *pid) {
1146
        int r;
8,050✔
1147

1148
        /* Checks whether a PID is still valid at all, including a zombie */
1149

1150
        if (!pidref_is_set(pid))
8,050✔
1151
                return -ESRCH;
1152

1153
        if (pidref_is_remote(pid))
8,049✔
1154
                return -EREMOTE;
1155

1156
        if (pid->pid == 1 || pidref_is_self(pid))
8,049✔
1157
                return true;
5✔
1158

1159
        r = pidref_kill(pid, 0);
8,044✔
1160
        if (r == -ESRCH)
8,044✔
1161
                return false;
1162
        if (r < 0)
1,572✔
1163
                return r;
70✔
1164

1165
        return true;
1166
}
1167

1168
int pid_is_unwaited(pid_t pid) {
1169

1170
        if (pid == 0)
7,394✔
1171
                return true;
7,394✔
1172

1173
        return pidref_is_unwaited(&PIDREF_MAKE_FROM_PID(pid));
7,394✔
1174
}
1175

1176
int pid_is_alive(pid_t pid) {
1177
        int r;
9,985✔
1178

1179
        /* Checks whether a PID is still valid and not a zombie */
1180

1181
        if (pid < 0)
9,985✔
1182
                return -ESRCH;
1183

1184
        if (pid <= 1) /* If we or PID 1 would be a zombie, this code would not be running */
9,984✔
1185
                return true;
1186

1187
        if (pid == getpid_cached())
9,984✔
1188
                return true;
1189

1190
        r = get_process_state(pid);
9,983✔
1191
        if (r == -ESRCH)
9,983✔
1192
                return false;
1193
        if (r < 0)
8,019✔
1194
                return r;
1195

1196
        return r != 'Z';
8,019✔
1197
}
1198

1199
int pidref_is_alive(const PidRef *pidref) {
1200
        int r, result;
9,980✔
1201

1202
        if (!pidref_is_set(pidref))
9,980✔
1203
                return -ESRCH;
1204

1205
        if (pidref_is_remote(pidref))
9,980✔
1206
                return -EREMOTE;
1207

1208
        result = pid_is_alive(pidref->pid);
9,980✔
1209
        if (result < 0) {
9,980✔
1210
                assert(result != -ESRCH);
×
1211
                return result;
1212
        }
1213

1214
        r = pidref_verify(pidref);
9,980✔
1215
        if (r == -ESRCH)
9,980✔
1216
                return false;
1217
        if (r < 0)
8,017✔
1218
                return r;
×
1219

1220
        return result;
1221
}
1222

1223
int pidref_from_same_root_fs(PidRef *a, PidRef *b) {
1224
        _cleanup_(pidref_done) PidRef self = PIDREF_NULL;
×
1225
        int r;
13,569✔
1226

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

1230
        if (!a || !b) {
13,569✔
1231
                r = pidref_set_self(&self);
13,549✔
1232
                if (r < 0)
13,549✔
1233
                        return r;
1234
                if (!a)
13,549✔
1235
                        a = &self;
×
1236
                if (!b)
13,549✔
1237
                        b = &self;
13,549✔
1238
        }
1239

1240
        if (!pidref_is_set(a) || !pidref_is_set(b))
13,569✔
1241
                return -ESRCH;
×
1242

1243
        /* If one of the two processes have the same root they cannot have the same root fs, but if both of
1244
         * them do we don't know */
1245
        if (pidref_is_remote(a) && pidref_is_remote(b))
13,569✔
1246
                return -EREMOTE;
1247
        if (pidref_is_remote(a) || pidref_is_remote(b))
40,707✔
1248
                return false;
1249

1250
        if (pidref_equal(a, b))
13,569✔
1251
                return true;
1252

1253
        const char *roota = procfs_file_alloca(a->pid, "root");
13,455✔
1254
        const char *rootb = procfs_file_alloca(b->pid, "root");
13,455✔
1255

1256
        int result = inode_same(roota, rootb, 0);
13,455✔
1257
        if (result == -ENOENT)
13,455✔
1258
                return proc_mounted() == 0 ? -ENOSYS : -ESRCH;
×
1259
        if (result < 0)
13,455✔
1260
                return result;
1261

1262
        r = pidref_verify(a);
13,338✔
1263
        if (r < 0)
13,338✔
1264
                return r;
1265
        r = pidref_verify(b);
13,338✔
1266
        if (r < 0)
13,338✔
1267
                return r;
×
1268

1269
        return result;
1270
}
1271

1272
bool is_main_thread(void) {
1273
        static thread_local int cached = -1;
6,771,370✔
1274

1275
        if (cached < 0)
6,771,370✔
1276
                cached = getpid_cached() == gettid();
70,101✔
1277

1278
        return cached;
6,771,370✔
1279
}
1280

1281
bool oom_score_adjust_is_valid(int oa) {
1282
        return oa >= OOM_SCORE_ADJ_MIN && oa <= OOM_SCORE_ADJ_MAX;
5,781✔
1283
}
1284

1285
unsigned long personality_from_string(const char *p) {
1286
        Architecture architecture;
9✔
1287

1288
        if (!p)
9✔
1289
                return PERSONALITY_INVALID;
1290

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

1295
        architecture = architecture_from_string(p);
8✔
1296
        if (architecture < 0)
8✔
1297
                return PERSONALITY_INVALID;
1298

1299
        if (architecture == native_architecture())
6✔
1300
                return PER_LINUX;
1301
#ifdef ARCHITECTURE_SECONDARY
1302
        if (architecture == ARCHITECTURE_SECONDARY)
3✔
1303
                return PER_LINUX32;
2✔
1304
#endif
1305

1306
        return PERSONALITY_INVALID;
1307
}
1308

1309
const char* personality_to_string(unsigned long p) {
1310
        Architecture architecture = _ARCHITECTURE_INVALID;
1,311✔
1311

1312
        if (p == PER_LINUX)
1,311✔
1313
                architecture = native_architecture();
1314
#ifdef ARCHITECTURE_SECONDARY
1315
        else if (p == PER_LINUX32)
1,306✔
1316
                architecture = ARCHITECTURE_SECONDARY;
1317
#endif
1318

1319
        if (architecture < 0)
1320
                return NULL;
1321

1322
        return architecture_to_string(architecture);
7✔
1323
}
1324

1325
int safe_personality(unsigned long p) {
1326
        int ret;
1,506✔
1327

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

1335
        errno = 0;
1,506✔
1336
        ret = personality(p);
1,506✔
1337
        if (ret < 0) {
1,506✔
1338
                if (errno != 0)
12✔
1339
                        return -errno;
12✔
1340

1341
                errno = -ret;
×
1342
        }
1343

1344
        return ret;
1345
}
1346

1347
int opinionated_personality(unsigned long *ret) {
1348
        int current;
1,491✔
1349

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

1354
        current = safe_personality(PERSONALITY_INVALID);
1,491✔
1355
        if (current < 0)
1,491✔
1356
                return current;
1357

1358
        if (((unsigned long) current & OPINIONATED_PERSONALITY_MASK) == PER_LINUX32)
1,491✔
1359
                *ret = PER_LINUX32;
×
1360
        else
1361
                *ret = PER_LINUX;
1,491✔
1362

1363
        return 0;
1364
}
1365

1366
void valgrind_summary_hack(void) {
1367
#if HAVE_VALGRIND_VALGRIND_H
1368
        if (getpid_cached() == 1 && RUNNING_ON_VALGRIND) {
1369
                pid_t pid;
1370
                pid = raw_clone(SIGCHLD);
1371
                if (pid < 0)
1372
                        log_struct_errno(
1373
                                LOG_EMERG, errno,
1374
                                LOG_MESSAGE_ID(SD_MESSAGE_VALGRIND_HELPER_FORK_STR),
1375
                                LOG_MESSAGE("Failed to fork off valgrind helper: %m"));
1376
                else if (pid == 0)
1377
                        exit(EXIT_SUCCESS);
1378
                else {
1379
                        log_info("Spawned valgrind helper as PID "PID_FMT".", pid);
1380
                        (void) wait_for_terminate(pid, NULL);
1381
                }
1382
        }
1383
#endif
1384
}
47✔
1385

1386
int pid_compare_func(const pid_t *a, const pid_t *b) {
1387
        /* Suitable for usage in qsort() */
1388
        return CMP(*a, *b);
1,406✔
1389
}
1390

1391
/* The cached PID, possible values:
1392
 *
1393
 *     == UNSET [0]  → cache not initialized yet
1394
 *     == BUSY [-1]  → some thread is initializing it at the moment
1395
 *     any other     → the cached PID
1396
 */
1397

1398
#define CACHED_PID_UNSET ((pid_t) 0)
1399
#define CACHED_PID_BUSY ((pid_t) -1)
1400

1401
static pid_t cached_pid = CACHED_PID_UNSET;
1402

1403
void reset_cached_pid(void) {
1404
        /* Invoked in the child after a fork(), i.e. at the first moment the PID changed */
1405
        cached_pid = CACHED_PID_UNSET;
3,166✔
1406
}
3,166✔
1407

1408
pid_t getpid_cached(void) {
1409
        static bool installed = false;
78,433,356✔
1410
        pid_t current_value = CACHED_PID_UNSET;
78,433,356✔
1411

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

1421
        (void) __atomic_compare_exchange_n(
78,433,356✔
1422
                        &cached_pid,
1423
                        &current_value,
1424
                        CACHED_PID_BUSY,
1425
                        false,
1426
                        __ATOMIC_SEQ_CST,
1427
                        __ATOMIC_SEQ_CST);
1428

1429
        switch (current_value) {
78,433,356✔
1430

1431
        case CACHED_PID_UNSET: { /* Not initialized yet, then do so now */
121,500✔
1432
                pid_t new_pid;
121,500✔
1433

1434
                new_pid = getpid();
121,500✔
1435

1436
                if (!installed) {
121,500✔
1437
                        /* __register_atfork() either returns 0 or -ENOMEM, in its glibc implementation. Since it's
1438
                         * only half-documented (glibc doesn't document it but LSB does — though only superficially)
1439
                         * we'll check for errors only in the most generic fashion possible. */
1440

1441
                        if (pthread_atfork(NULL, NULL, reset_cached_pid) != 0) {
91,852✔
1442
                                /* OOM? Let's try again later */
1443
                                cached_pid = CACHED_PID_UNSET;
×
1444
                                return new_pid;
×
1445
                        }
1446

1447
                        installed = true;
91,852✔
1448
                }
1449

1450
                cached_pid = new_pid;
121,500✔
1451
                return new_pid;
121,500✔
1452
        }
1453

1454
        case CACHED_PID_BUSY: /* Somebody else is currently initializing */
×
1455
                return getpid();
×
1456

1457
        default: /* Properly initialized */
1458
                return current_value;
1459
        }
1460
}
1461

1462
int must_be_root(void) {
1463

1464
        if (geteuid() == 0)
59✔
1465
                return 0;
1466

1467
        return log_error_errno(SYNTHETIC_ERRNO(EPERM), "Need to be root.");
×
1468
}
1469

1470
pid_t clone_with_nested_stack(int (*fn)(void *), int flags, void *userdata) {
1471
        size_t ps;
2,935✔
1472
        pid_t pid;
2,935✔
1473
        void *mystack;
2,935✔
1474

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

1483
        assert((flags & (CLONE_VM|CLONE_PARENT_SETTID|CLONE_CHILD_SETTID|
2,935✔
1484
                         CLONE_CHILD_CLEARTID|CLONE_SETTLS)) == 0);
1485

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

1493
        ps = page_size();
2,935✔
1494
        mystack = alloca(ps*3);
2,935✔
1495
        mystack = (uint8_t*) mystack + ps; /* move pointer one page ahead since stacks usually grow backwards */
2,935✔
1496
        mystack = (void*) ALIGN_TO((uintptr_t) mystack, ps); /* align to page size (moving things further ahead) */
2,935✔
1497

1498
#if HAVE_CLONE
1499
        pid = clone(fn, mystack, flags, userdata);
2,935✔
1500
#else
1501
        pid = __clone2(fn, mystack, ps, flags, userdata);
1502
#endif
1503
        if (pid < 0)
2,935✔
1504
                return -errno;
×
1505

1506
        return pid;
1507
}
1508

1509
static void restore_sigsetp(sigset_t **ssp) {
59,358✔
1510
        if (*ssp)
59,358✔
1511
                (void) sigprocmask(SIG_SETMASK, *ssp, NULL);
24,813✔
1512
}
59,358✔
1513

1514
static int fork_flags_to_signal(ForkFlags flags) {
28,912✔
1515
        return (flags & FORK_DEATHSIG_SIGTERM) ? SIGTERM :
28,912✔
1516
                (flags & FORK_DEATHSIG_SIGINT) ? SIGINT :
1,440✔
1517
                                                 SIGKILL;
1518
}
1519

1520
int pidref_safe_fork_full(
1521
                const char *name,
1522
                const int stdio_fds[3],
1523
                int except_fds[],
1524
                size_t n_except_fds,
1525
                ForkFlags flags,
1526
                PidRef *ret_pid) {
1527

1528
        pid_t original_pid, pid;
31,430✔
1529
        sigset_t saved_ss, ss;
31,430✔
1530
        _unused_ _cleanup_(restore_sigsetp) sigset_t *saved_ssp = NULL;
×
1531
        bool block_signals = false, block_all = false, intermediary = false;
31,430✔
1532
        _cleanup_close_pair_ int pidref_transport_fds[2] = EBADF_PAIR;
59,358✔
1533
        int prio, r;
31,430✔
1534

1535
        assert(!FLAGS_SET(flags, FORK_WAIT|FORK_FREEZE));
31,430✔
1536
        assert(!FLAGS_SET(flags, FORK_DETACH) ||
31,430✔
1537
               (flags & (FORK_WAIT|FORK_DEATHSIG_SIGTERM|FORK_DEATHSIG_SIGINT|FORK_DEATHSIG_SIGKILL)) == 0);
1538

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

1543
        prio = flags & FORK_LOG ? LOG_ERR : LOG_DEBUG;
31,430✔
1544

1545
        original_pid = getpid_cached();
31,430✔
1546

1547
        if (flags & FORK_FLUSH_STDIO) {
31,430✔
1548
                fflush(stdout);
5✔
1549
                fflush(stderr); /* This one shouldn't be necessary, stderr should be unbuffered anyway, but let's better be safe than sorry */
5✔
1550
        }
1551

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

1557
                assert_se(sigfillset(&ss) >= 0);
26,909✔
1558
                block_signals = block_all = true;
1559

1560
        } else if (flags & FORK_WAIT) {
4,521✔
1561
                /* Let's block SIGCHLD at least, so that we can safely watch for the child process */
1562

1563
                assert_se(sigemptyset(&ss) >= 0);
106✔
1564
                assert_se(sigaddset(&ss, SIGCHLD) >= 0);
106✔
1565
                block_signals = true;
1566
        }
1567

1568
        if (block_signals) {
1569
                if (sigprocmask(SIG_BLOCK, &ss, &saved_ss) < 0)
27,015✔
1570
                        return log_full_errno(prio, errno, "Failed to block signal mask: %m");
×
1571
                saved_ssp = &saved_ss;
27,015✔
1572
        }
1573

1574
        if (FLAGS_SET(flags, FORK_DETACH)) {
31,430✔
1575
                /* Fork off intermediary child if needed */
1576

1577
                r = is_reaper_process();
98✔
1578
                if (r < 0)
98✔
1579
                        return log_full_errno(prio, r, "Failed to determine if we are a reaper process: %m");
×
1580

1581
                if (!r) {
98✔
1582
                        /* Not a reaper process, hence do a double fork() so we are reparented to one */
1583

1584
                        if (ret_pid && socketpair(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC, 0, pidref_transport_fds) < 0)
10✔
1585
                                return log_full_errno(prio, errno, "Failed to allocate pidref socket: %m");
×
1586

1587
                        pid = fork();
10✔
1588
                        if (pid < 0)
26✔
1589
                                return log_full_errno(prio, errno, "Failed to fork off '%s': %m", strna(name));
×
1590
                        if (pid > 0) {
26✔
1591
                                log_debug("Successfully forked off intermediary '%s' as PID " PID_FMT ".", strna(name), pid);
10✔
1592

1593
                                pidref_transport_fds[1] = safe_close(pidref_transport_fds[1]);
10✔
1594

1595
                                if (pidref_transport_fds[0] >= 0) {
10✔
1596
                                        /* Wait for the intermediary child to exit so the caller can be certain the actual child
1597
                                         * process has been reparented by the time this function returns. */
1598
                                        r = wait_for_terminate_and_check(name, pid, FLAGS_SET(flags, FORK_LOG) ? WAIT_LOG : 0);
18✔
1599
                                        if (r < 0)
9✔
1600
                                                return log_full_errno(prio, r, "Failed to wait for intermediary process: %m");
×
1601
                                        if (r != EXIT_SUCCESS) /* exit status > 0 should be treated as failure, too */
9✔
1602
                                                return -EPROTO;
1603

1604
                                        int pidfd;
9✔
1605
                                        ssize_t n = receive_one_fd_iov(
18✔
1606
                                                        pidref_transport_fds[0],
1607
                                                        &IOVEC_MAKE(&pid, sizeof(pid)),
9✔
1608
                                                        /* iovlen= */ 1,
1609
                                                        /* flags= */ 0,
1610
                                                        &pidfd);
1611
                                        if (n < 0)
9✔
1612
                                                return log_full_errno(prio, n, "Failed to receive child pidref: %m");
×
1613

1614
                                        *ret_pid = (PidRef) { .pid = pid, .fd = pidfd };
9✔
1615
                                }
1616

1617
                                return 1; /* return in the parent */
10✔
1618
                        }
1619

1620
                        pidref_transport_fds[0] = safe_close(pidref_transport_fds[0]);
16✔
1621
                        intermediary = true;
16✔
1622
                }
1623
        }
1624

1625
        if ((flags & (FORK_NEW_MOUNTNS|FORK_NEW_USERNS|FORK_NEW_NETNS|FORK_NEW_PIDNS)) != 0)
31,436✔
1626
                pid = raw_clone(SIGCHLD|
9,882✔
1627
                                (FLAGS_SET(flags, FORK_NEW_MOUNTNS) ? CLONE_NEWNS : 0) |
9,882✔
1628
                                (FLAGS_SET(flags, FORK_NEW_USERNS) ? CLONE_NEWUSER : 0) |
9,882✔
1629
                                (FLAGS_SET(flags, FORK_NEW_NETNS) ? CLONE_NEWNET : 0) |
9,882✔
1630
                                (FLAGS_SET(flags, FORK_NEW_PIDNS) ? CLONE_NEWPID : 0));
9,882✔
1631
        else
1632
                pid = fork();
21,554✔
1633
        if (pid < 0)
59,358✔
1634
                return log_full_errno(prio, errno, "Failed to fork off '%s': %m", strna(name));
2✔
1635
        if (pid > 0) {
59,357✔
1636

1637
                /* If we are in the intermediary process, exit now */
1638
                if (intermediary) {
29,227✔
1639
                        if (pidref_transport_fds[1] >= 0) {
10✔
1640
                                _cleanup_(pidref_done) PidRef pidref = PIDREF_NULL;
9✔
1641

1642
                                r = pidref_set_pid(&pidref, pid);
9✔
1643
                                if (r < 0) {
9✔
1644
                                        log_full_errno(prio, r, "Failed to open reference to PID "PID_FMT": %m", pid);
×
1645
                                        _exit(EXIT_FAILURE);
×
1646
                                }
1647

1648
                                r = send_one_fd_iov(
9✔
1649
                                                pidref_transport_fds[1],
1650
                                                pidref.fd,
1651
                                                &IOVEC_MAKE(&pidref.pid, sizeof(pidref.pid)),
1652
                                                /* iovlen= */ 1,
1653
                                                /* flags= */ 0);
1654
                                if (r < 0) {
9✔
1655
                                        log_full_errno(prio, r, "Failed to send child pidref: %m");
×
1656
                                        _exit(EXIT_FAILURE);
×
1657
                                }
1658
                        }
1659

1660
                        _exit(EXIT_SUCCESS);
10✔
1661
                }
1662

1663
                /* We are in the parent process */
1664
                log_debug("Successfully forked off '%s' as PID " PID_FMT ".", strna(name), pid);
29,217✔
1665

1666
                if (flags & FORK_WAIT) {
29,217✔
1667
                        if (block_all) {
3,208✔
1668
                                /* undo everything except SIGCHLD */
1669
                                ss = saved_ss;
3,102✔
1670
                                assert_se(sigaddset(&ss, SIGCHLD) >= 0);
3,102✔
1671
                                (void) sigprocmask(SIG_SETMASK, &ss, NULL);
3,102✔
1672
                        }
1673

1674
                        r = wait_for_terminate_and_check(name, pid, (flags & FORK_LOG ? WAIT_LOG : 0));
5,962✔
1675
                        if (r < 0)
3,208✔
1676
                                return r;
1677
                        if (r != EXIT_SUCCESS) /* exit status > 0 should be treated as failure, too */
3,208✔
1678
                                return -EPROTO;
1679

1680
                        /* If we are in the parent and successfully waited, then the process doesn't exist anymore. */
1681
                        if (ret_pid)
3,208✔
1682
                                *ret_pid = PIDREF_NULL;
5✔
1683

1684
                        return 1;
3,208✔
1685
                }
1686

1687
                if (ret_pid) {
26,009✔
1688
                        if (FLAGS_SET(flags, FORK_PID_ONLY))
25,534✔
1689
                                *ret_pid = PIDREF_MAKE_FROM_PID(pid);
7,166✔
1690
                        else {
1691
                                r = pidref_set_pid(ret_pid, pid);
18,368✔
1692
                                if (r < 0) /* Let's not fail for this, no matter what, the process exists after all, and that's key */
18,368✔
1693
                                        *ret_pid = PIDREF_MAKE_FROM_PID(pid);
×
1694
                        }
1695
                }
1696

1697
                return 1;
26,009✔
1698
        }
1699

1700
        /* We are in the child process */
1701

1702
        pidref_transport_fds[1] = safe_close(pidref_transport_fds[1]);
30,130✔
1703

1704
        /* Restore signal mask manually */
1705
        saved_ssp = NULL;
30,130✔
1706

1707
        if (flags & FORK_REOPEN_LOG) {
30,130✔
1708
                /* Close the logs if requested, before we log anything. And make sure we reopen it if needed. */
1709
                log_close();
2,578✔
1710
                log_set_open_when_needed(true);
2,578✔
1711
                log_settle_target();
2,578✔
1712
        }
1713

1714
        if (name) {
30,130✔
1715
                r = rename_process(name);
30,130✔
1716
                if (r < 0)
30,130✔
1717
                        log_full_errno(flags & FORK_LOG ? LOG_WARNING : LOG_DEBUG,
×
1718
                                       r, "Failed to rename process, ignoring: %m");
1719
        }
1720

1721
        if (flags & (FORK_DEATHSIG_SIGTERM|FORK_DEATHSIG_SIGINT|FORK_DEATHSIG_SIGKILL))
30,130✔
1722
                if (prctl(PR_SET_PDEATHSIG, fork_flags_to_signal(flags)) < 0) {
28,912✔
1723
                        log_full_errno(prio, errno, "Failed to set death signal: %m");
×
1724
                        _exit(EXIT_FAILURE);
×
1725
                }
1726

1727
        if (flags & FORK_RESET_SIGNALS) {
30,130✔
1728
                r = reset_all_signal_handlers();
23,352✔
1729
                if (r < 0) {
23,352✔
1730
                        log_full_errno(prio, r, "Failed to reset signal handlers: %m");
×
1731
                        _exit(EXIT_FAILURE);
×
1732
                }
1733

1734
                /* This implicitly undoes the signal mask stuff we did before the fork()ing above */
1735
                r = reset_signal_mask();
23,352✔
1736
                if (r < 0) {
23,352✔
1737
                        log_full_errno(prio, r, "Failed to reset signal mask: %m");
×
1738
                        _exit(EXIT_FAILURE);
×
1739
                }
1740
        } else if (block_signals) { /* undo what we did above */
6,778✔
1741
                if (sigprocmask(SIG_SETMASK, &saved_ss, NULL) < 0) {
6,419✔
1742
                        log_full_errno(prio, errno, "Failed to restore signal mask: %m");
×
1743
                        _exit(EXIT_FAILURE);
×
1744
                }
1745
        }
1746

1747
        if (flags & (FORK_DEATHSIG_SIGTERM|FORK_DEATHSIG_SIGKILL|FORK_DEATHSIG_SIGINT)) {
30,130✔
1748
                pid_t ppid;
28,912✔
1749
                /* Let's see if the parent PID is still the one we started from? If not, then the parent
1750
                 * already died by the time we set PR_SET_PDEATHSIG, hence let's emulate the effect */
1751

1752
                ppid = getppid();
28,912✔
1753
                if (ppid == 0)
28,912✔
1754
                        /* Parent is in a different PID namespace. */;
1755
                else if (ppid != original_pid) {
28,875✔
1756
                        int sig = fork_flags_to_signal(flags);
×
1757
                        log_debug("Parent died early, raising %s.", signal_to_string(sig));
×
1758
                        (void) raise(sig);
×
1759
                        _exit(EXIT_FAILURE);
×
1760
                }
1761
        }
1762

1763
        if (FLAGS_SET(flags, FORK_NEW_MOUNTNS | FORK_MOUNTNS_SLAVE)) {
30,130✔
1764
                /* Optionally, make sure we never propagate mounts to the host. */
1765
                if (mount(NULL, "/", NULL, MS_SLAVE | MS_REC, NULL) < 0) {
108✔
1766
                        log_full_errno(prio, errno, "Failed to remount root directory as MS_SLAVE: %m");
×
1767
                        _exit(EXIT_FAILURE);
×
1768
                }
1769
        }
1770

1771
        if (FLAGS_SET(flags, FORK_PRIVATE_TMP)) {
30,130✔
1772
                assert(FLAGS_SET(flags, FORK_NEW_MOUNTNS));
×
1773

1774
                /* Optionally, overmount new tmpfs instance on /tmp/. */
1775
                r = mount_nofollow("tmpfs", "/tmp", "tmpfs",
×
1776
                                   MS_NOSUID|MS_NODEV,
1777
                                   "mode=01777" TMPFS_LIMITS_RUN);
1778
                if (r < 0) {
×
1779
                        log_full_errno(prio, r, "Failed to overmount /tmp/: %m");
×
1780
                        _exit(EXIT_FAILURE);
×
1781
                }
1782
        }
1783

1784
        if (flags & FORK_REARRANGE_STDIO) {
30,130✔
1785
                if (stdio_fds) {
14,380✔
1786
                        r = rearrange_stdio(stdio_fds[0], stdio_fds[1], stdio_fds[2]);
14,369✔
1787
                        if (r < 0) {
14,369✔
1788
                                log_full_errno(prio, r, "Failed to rearrange stdio fds: %m");
×
1789
                                _exit(EXIT_FAILURE);
×
1790
                        }
1791

1792
                        /* Turn off O_NONBLOCK on the fdio fds, in case it was left on */
1793
                        stdio_disable_nonblock();
14,369✔
1794
                } else {
1795
                        r = make_null_stdio();
11✔
1796
                        if (r < 0) {
11✔
1797
                                log_full_errno(prio, r, "Failed to connect stdin/stdout to /dev/null: %m");
×
1798
                                _exit(EXIT_FAILURE);
×
1799
                        }
1800
                }
1801
        } else if (flags & FORK_STDOUT_TO_STDERR) {
15,750✔
1802
                if (dup2(STDERR_FILENO, STDOUT_FILENO) < 0) {
2✔
1803
                        log_full_errno(prio, errno, "Failed to connect stdout to stderr: %m");
×
1804
                        _exit(EXIT_FAILURE);
×
1805
                }
1806
        }
1807

1808
        if (flags & FORK_CLOSE_ALL_FDS) {
30,130✔
1809
                /* Close the logs here in case it got reopened above, as close_all_fds() would close them for us */
1810
                log_close();
22,356✔
1811

1812
                r = close_all_fds(except_fds, n_except_fds);
22,356✔
1813
                if (r < 0) {
22,356✔
1814
                        log_full_errno(prio, r, "Failed to close all file descriptors: %m");
×
1815
                        _exit(EXIT_FAILURE);
×
1816
                }
1817
        }
1818

1819
        if (flags & FORK_PACK_FDS) {
30,130✔
1820
                /* FORK_CLOSE_ALL_FDS ensures that except_fds are the only FDs >= 3 that are
1821
                 * open, this is including the log. This is required by pack_fds, which will
1822
                 * get stuck in an infinite loop of any FDs other than except_fds are open. */
1823
                assert(FLAGS_SET(flags, FORK_CLOSE_ALL_FDS));
87✔
1824

1825
                r = pack_fds(except_fds, n_except_fds);
87✔
1826
                if (r < 0) {
87✔
1827
                        log_full_errno(prio, r, "Failed to pack file descriptors: %m");
×
1828
                        _exit(EXIT_FAILURE);
×
1829
                }
1830
        }
1831

1832
        if (flags & FORK_CLOEXEC_OFF) {
30,130✔
1833
                r = fd_cloexec_many(except_fds, n_except_fds, false);
97✔
1834
                if (r < 0) {
97✔
1835
                        log_full_errno(prio, r, "Failed to turn off O_CLOEXEC on file descriptors: %m");
×
1836
                        _exit(EXIT_FAILURE);
×
1837
                }
1838
        }
1839

1840
        /* When we were asked to reopen the logs, do so again now */
1841
        if (flags & FORK_REOPEN_LOG) {
30,130✔
1842
                log_open();
2,578✔
1843
                log_set_open_when_needed(false);
2,578✔
1844
        }
1845

1846
        if (flags & FORK_RLIMIT_NOFILE_SAFE) {
30,130✔
1847
                r = rlimit_nofile_safe();
18,562✔
1848
                if (r < 0) {
18,562✔
1849
                        log_full_errno(prio, r, "Failed to lower RLIMIT_NOFILE's soft limit to 1K: %m");
×
1850
                        _exit(EXIT_FAILURE);
×
1851
                }
1852
        }
1853

1854
        if (!FLAGS_SET(flags, FORK_KEEP_NOTIFY_SOCKET)) {
30,130✔
1855
                r = RET_NERRNO(unsetenv("NOTIFY_SOCKET"));
30,130✔
1856
                if (r < 0) {
×
1857
                        log_full_errno(prio, r, "Failed to unset $NOTIFY_SOCKET: %m");
×
1858
                        _exit(EXIT_FAILURE);
×
1859
                }
1860
        }
1861

1862
        if (FLAGS_SET(flags, FORK_FREEZE))
30,130✔
1863
                freeze();
×
1864

1865
        if (ret_pid) {
30,130✔
1866
                if (FLAGS_SET(flags, FORK_PID_ONLY))
26,192✔
1867
                        *ret_pid = PIDREF_MAKE_FROM_PID(getpid_cached());
7,104✔
1868
                else {
1869
                        r = pidref_set_self(ret_pid);
19,088✔
1870
                        if (r < 0) {
19,088✔
1871
                                log_full_errno(prio, r, "Failed to acquire PID reference on ourselves: %m");
×
1872
                                _exit(EXIT_FAILURE);
×
1873
                        }
1874
                }
1875
        }
1876

1877
        return 0;
1878
}
1879

1880
int safe_fork_full(
1881
                const char *name,
1882
                const int stdio_fds[3],
1883
                int except_fds[],
1884
                size_t n_except_fds,
1885
                ForkFlags flags,
1886
                pid_t *ret_pid) {
1887

1888
        _cleanup_(pidref_done) PidRef pidref = PIDREF_NULL;
21,828✔
1889
        int r;
13,047✔
1890

1891
        /* Getting the detached child process pid without pidfd is racy, so don't allow it if not returning
1892
         * a pidref to the caller. */
1893
        assert(!FLAGS_SET(flags, FORK_DETACH) || !ret_pid);
13,047✔
1894

1895
        r = pidref_safe_fork_full(name, stdio_fds, except_fds, n_except_fds, flags|FORK_PID_ONLY, ret_pid ? &pidref : NULL);
18,702✔
1896
        if (r < 0 || !ret_pid)
21,828✔
1897
                return r;
1898

1899
        *ret_pid = pidref.pid;
14,273✔
1900

1901
        return r;
14,273✔
1902
}
1903

1904
int namespace_fork(
1905
                const char *outer_name,
1906
                const char *inner_name,
1907
                int except_fds[],
1908
                size_t n_except_fds,
1909
                ForkFlags flags,
1910
                int pidns_fd,
1911
                int mntns_fd,
1912
                int netns_fd,
1913
                int userns_fd,
1914
                int root_fd,
1915
                pid_t *ret_pid) {
1916

1917
        int r;
163✔
1918

1919
        /* This is much like safe_fork(), but forks twice, and joins the specified namespaces in the middle
1920
         * process. This ensures that we are fully a member of the destination namespace, with pidns an all, so that
1921
         * /proc/self/fd works correctly. */
1922

1923
        r = safe_fork_full(outer_name,
477✔
1924
                           NULL,
1925
                           except_fds, n_except_fds,
1926
                           (flags|FORK_DEATHSIG_SIGINT|FORK_DEATHSIG_SIGTERM|FORK_DEATHSIG_SIGKILL) & ~(FORK_REOPEN_LOG|FORK_NEW_MOUNTNS|FORK_MOUNTNS_SLAVE), ret_pid);
163✔
1927
        if (r < 0)
314✔
1928
                return r;
1929
        if (r == 0) {
314✔
1930
                pid_t pid;
151✔
1931

1932
                /* Child */
1933

1934
                r = namespace_enter(pidns_fd, mntns_fd, netns_fd, userns_fd, root_fd);
151✔
1935
                if (r < 0) {
151✔
1936
                        log_full_errno(FLAGS_SET(flags, FORK_LOG) ? LOG_ERR : LOG_DEBUG, r, "Failed to join namespace: %m");
×
1937
                        _exit(EXIT_FAILURE);
×
1938
                }
1939

1940
                /* We mask a few flags here that either make no sense for the grandchild, or that we don't have to do again */
1941
                r = safe_fork_full(inner_name,
454✔
1942
                                   NULL,
1943
                                   except_fds, n_except_fds,
1944
                                   flags & ~(FORK_WAIT|FORK_RESET_SIGNALS|FORK_CLOSE_ALL_FDS|FORK_REARRANGE_STDIO), &pid);
151✔
1945
                if (r < 0)
303✔
1946
                        _exit(EXIT_FAILURE);
×
1947
                if (r == 0) {
303✔
1948
                        /* Child */
1949
                        if (ret_pid)
152✔
1950
                                *ret_pid = pid;
152✔
1951
                        return 0;
152✔
1952
                }
1953

1954
                r = wait_for_terminate_and_check(inner_name, pid, FLAGS_SET(flags, FORK_LOG) ? WAIT_LOG : 0);
302✔
1955
                if (r < 0)
151✔
1956
                        _exit(EXIT_FAILURE);
×
1957

1958
                _exit(r);
151✔
1959
        }
1960

1961
        return 1;
1962
}
1963

1964
int set_oom_score_adjust(int value) {
1965
        char t[DECIMAL_STR_MAX(int)];
4,002✔
1966

1967
        if (!oom_score_adjust_is_valid(value))
4,002✔
1968
                return -EINVAL;
4,002✔
1969

1970
        xsprintf(t, "%i", value);
4,002✔
1971

1972
        return write_string_file("/proc/self/oom_score_adj", t,
4,002✔
1973
                                 WRITE_STRING_FILE_VERIFY_ON_FAILURE|WRITE_STRING_FILE_DISABLE_BUFFER);
1974
}
1975

1976
int get_oom_score_adjust(int *ret) {
1977
        _cleanup_free_ char *t = NULL;
1,073✔
1978
        int r, a;
1,073✔
1979

1980
        r = read_virtual_file("/proc/self/oom_score_adj", SIZE_MAX, &t, NULL);
1,073✔
1981
        if (r < 0)
1,073✔
1982
                return r;
1983

1984
        delete_trailing_chars(t, WHITESPACE);
1,073✔
1985

1986
        r = safe_atoi(t, &a);
1,073✔
1987
        if (r < 0)
1,073✔
1988
                return r;
1989

1990
        if (!oom_score_adjust_is_valid(a))
1,073✔
1991
                return -ENODATA;
1992

1993
        if (ret)
1,073✔
1994
                *ret = a;
1,073✔
1995

1996
        return 0;
1997
}
1998

1999
static int rlimit_to_nice(rlim_t limit) {
2✔
2000
        if (limit <= 1)
2✔
2001
                return PRIO_MAX-1; /* i.e. 19 */
2002

2003
        if (limit >= -PRIO_MIN + PRIO_MAX)
2✔
2004
                return PRIO_MIN; /* i.e. -20 */
2005

2006
        return PRIO_MAX - (int) limit;
2✔
2007
}
2008

2009
int setpriority_closest(int priority) {
2010
        struct rlimit highest;
23✔
2011
        int r, current, limit;
23✔
2012

2013
        /* Try to set requested nice level */
2014
        r = RET_NERRNO(setpriority(PRIO_PROCESS, 0, priority));
23✔
2015
        if (r >= 0)
2✔
2016
                return 1;
21✔
2017
        if (!ERRNO_IS_NEG_PRIVILEGE(r))
2✔
2018
                return r;
2019

2020
        errno = 0;
2✔
2021
        current = getpriority(PRIO_PROCESS, 0);
2✔
2022
        if (errno != 0)
2✔
2023
                return -errno;
×
2024

2025
        if (priority == current)
2✔
2026
                return 1;
2027

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

2034
        if (getrlimit(RLIMIT_NICE, &highest) < 0)
2✔
2035
                return -errno;
×
2036

2037
        limit = rlimit_to_nice(highest.rlim_cur);
2✔
2038

2039
        /* Push to the allowed limit if we're higher than that. Note that we could also be less nice than
2040
         * limit allows us, but still higher than what's requested. In that case our current value is
2041
         * the best choice. */
2042
        if (current > limit)
2✔
2043
                if (setpriority(PRIO_PROCESS, 0, limit) < 0)
2✔
2044
                        return -errno;
×
2045

2046
        log_debug("Cannot set requested nice level (%i), using next best (%i).", priority, MIN(current, limit));
2✔
2047
        return 0;
2048
}
2049

2050
_noreturn_ void freeze(void) {
2051
        log_close();
×
2052

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

2058
        /* Let's not freeze right away, but keep reaping zombies. */
2059
        for (;;) {
×
2060
                siginfo_t si = {};
×
2061

2062
                if (waitid(P_ALL, 0, &si, WEXITED) < 0 && errno != EINTR)
×
2063
                        break;
2064
        }
2065

2066
        /* waitid() failed with an ECHLD error (because there are no left-over child processes) or any other
2067
         * (unexpected) error. Freeze for good now! */
2068
        for (;;)
×
2069
                pause();
×
2070
}
2071

2072
int get_process_threads(pid_t pid) {
2073
        _cleanup_free_ char *t = NULL;
7✔
2074
        const char *p;
7✔
2075
        int n, r;
7✔
2076

2077
        if (pid < 0)
7✔
2078
                return -EINVAL;
2079

2080
        p = procfs_file_alloca(pid, "status");
7✔
2081

2082
        r = get_proc_field(p, "Threads", WHITESPACE, &t);
7✔
2083
        if (r == -ENOENT)
7✔
2084
                return proc_mounted() == 0 ? -ENOSYS : -ESRCH;
×
2085
        if (r < 0)
7✔
2086
                return r;
2087

2088
        r = safe_atoi(t, &n);
7✔
2089
        if (r < 0)
7✔
2090
                return r;
2091
        if (n < 0)
7✔
2092
                return -EINVAL;
×
2093

2094
        return n;
2095
}
2096

2097
int is_reaper_process(void) {
2098
        int b = 0;
3,037✔
2099

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

2103
        if (getpid_cached() == 1)
3,037✔
2104
                return true;
3,037✔
2105

2106
        if (prctl(PR_GET_CHILD_SUBREAPER, (unsigned long) &b, 0UL, 0UL, 0UL) < 0)
299✔
2107
                return -errno;
×
2108

2109
        return b != 0;
299✔
2110
}
2111

2112
int make_reaper_process(bool b) {
2113

2114
        if (getpid_cached() == 1) {
598✔
2115

2116
                if (!b)
60✔
2117
                        return -EINVAL;
2118

2119
                return 0;
60✔
2120
        }
2121

2122
        /* Some prctl()s insist that all 5 arguments are specified, others do not. Let's always specify all,
2123
         * to avoid any ambiguities */
2124
        if (prctl(PR_SET_CHILD_SUBREAPER, (unsigned long) b, 0UL, 0UL, 0UL) < 0)
538✔
2125
                return -errno;
×
2126

2127
        return 0;
2128
}
2129

2130
DEFINE_TRIVIAL_CLEANUP_FUNC_FULL(posix_spawnattr_t*, posix_spawnattr_destroy, NULL);
×
2131

2132
int posix_spawn_wrapper(
2133
                const char *path,
2134
                char * const *argv,
2135
                char * const *envp,
2136
                const char *cgroup,
2137
                PidRef *ret_pidref) {
2138

2139
        short flags = POSIX_SPAWN_SETSIGMASK;
2,199✔
2140
        posix_spawnattr_t attr;
2,199✔
2141
        sigset_t mask;
2,199✔
2142
        int r;
2,199✔
2143

2144
        /* Forks and invokes 'path' with 'argv' and 'envp' using CLONE_VM and CLONE_VFORK, which means the
2145
         * caller will be blocked until the child either exits or exec's. The memory of the child will be
2146
         * fully shared with the memory of the parent, so that there are no copy-on-write or memory.max
2147
         * issues.
2148
         *
2149
         * Also, move the newly-created process into 'cgroup' through POSIX_SPAWN_SETCGROUP (clone3())
2150
         * if available.
2151
         * returns 1: We're already in the right cgroup
2152
         *         0: 'cgroup' not specified or POSIX_SPAWN_SETCGROUP is not supported. The caller
2153
         *            needs to call 'cg_attach' on their own */
2154

2155
        assert(path);
2,199✔
2156
        assert(argv);
2,199✔
2157
        assert(ret_pidref);
2,199✔
2158

2159
        assert_se(sigfillset(&mask) >= 0);
2,199✔
2160

2161
        r = posix_spawnattr_init(&attr);
2,199✔
2162
        if (r != 0)
2,199✔
2163
                return -r; /* These functions return a positive errno on failure */
2,199✔
2164

2165
        /* Initialization needs to succeed before we can set up a destructor. */
2166
        _unused_ _cleanup_(posix_spawnattr_destroyp) posix_spawnattr_t *attr_destructor = &attr;
4,398✔
2167

2168
#if HAVE_PIDFD_SPAWN
2169
        static bool have_clone_into_cgroup = true; /* kernel 5.7+ */
2,199✔
2170
        _cleanup_close_ int cgroup_fd = -EBADF;
2,199✔
2171

2172
        if (cgroup && have_clone_into_cgroup) {
2,199✔
2173
                _cleanup_free_ char *resolved_cgroup = NULL;
2,199✔
2174

2175
                r = cg_get_path_and_check(
2,199✔
2176
                                SYSTEMD_CGROUP_CONTROLLER,
2177
                                cgroup,
2178
                                /* suffix= */ NULL,
2179
                                &resolved_cgroup);
2180
                if (r < 0)
2,199✔
2181
                        return r;
2182

2183
                cgroup_fd = open(resolved_cgroup, O_PATH|O_DIRECTORY|O_CLOEXEC);
2,199✔
2184
                if (cgroup_fd < 0)
2,199✔
2185
                        return -errno;
×
2186

2187
                r = posix_spawnattr_setcgroup_np(&attr, cgroup_fd);
2,199✔
2188
                if (r != 0)
2,199✔
2189
                        return -r;
×
2190

2191
                flags |= POSIX_SPAWN_SETCGROUP;
2,199✔
2192
        }
2193
#endif
2194

2195
        r = posix_spawnattr_setflags(&attr, flags);
2,199✔
2196
        if (r != 0)
2,199✔
2197
                return -r;
×
2198
        r = posix_spawnattr_setsigmask(&attr, &mask);
2,199✔
2199
        if (r != 0)
2,199✔
2200
                return -r;
×
2201

2202
#if HAVE_PIDFD_SPAWN
2203
        _cleanup_close_ int pidfd = -EBADF;
2,199✔
2204

2205
        r = pidfd_spawn(&pidfd, path, NULL, &attr, argv, envp);
2,199✔
2206
        if (ERRNO_IS_NOT_SUPPORTED(r) && FLAGS_SET(flags, POSIX_SPAWN_SETCGROUP) && cg_is_threaded(cgroup) > 0)
2,199✔
2207
                return -EUCLEAN; /* clone3() could also return EOPNOTSUPP if the target cgroup is in threaded mode,
2208
                                    turn that into something recognizable */
2209
        if ((ERRNO_IS_NOT_SUPPORTED(r) || ERRNO_IS_PRIVILEGE(r) || r == E2BIG) &&
2,199✔
2210
            FLAGS_SET(flags, POSIX_SPAWN_SETCGROUP)) {
2211
                /* Compiled on a newer host, or seccomp&friends blocking clone3()? Fallback, but
2212
                 * need to disable POSIX_SPAWN_SETCGROUP, which is what redirects to clone3().
2213
                 * Note that we might get E2BIG here since some kernels (e.g. 5.4) support clone3()
2214
                 * but not CLONE_INTO_CGROUP. */
2215

2216
                /* CLONE_INTO_CGROUP definitely won't work, hence remember the fact so that we don't
2217
                 * retry every time. */
2218
                have_clone_into_cgroup = false;
×
2219

2220
                flags &= ~POSIX_SPAWN_SETCGROUP;
×
2221
                r = posix_spawnattr_setflags(&attr, flags);
×
2222
                if (r != 0)
×
2223
                        return -r;
×
2224

2225
                r = pidfd_spawn(&pidfd, path, NULL, &attr, argv, envp);
×
2226
        }
2227
        if (r != 0)
2,199✔
2228
                return -r;
×
2229

2230
        r = pidref_set_pidfd_consume(ret_pidref, TAKE_FD(pidfd));
2,199✔
2231
        if (r < 0)
2,199✔
2232
                return r;
2233

2234
        return FLAGS_SET(flags, POSIX_SPAWN_SETCGROUP);
2,199✔
2235
#else
2236
        pid_t pid;
2237

2238
        r = posix_spawn(&pid, path, NULL, &attr, argv, envp);
2239
        if (r != 0)
2240
                return -r;
2241

2242
        r = pidref_set_pid(ret_pidref, pid);
2243
        if (r < 0)
2244
                return r;
2245

2246
        return 0; /* We did not use CLONE_INTO_CGROUP so return 0, the caller will have to move the child */
2247
#endif
2248
}
2249

2250
int proc_dir_open(DIR **ret) {
2251
        DIR *d;
11✔
2252

2253
        assert(ret);
11✔
2254

2255
        d = opendir("/proc");
11✔
2256
        if (!d)
11✔
2257
                return -errno;
×
2258

2259
        *ret = d;
11✔
2260
        return 0;
11✔
2261
}
2262

2263
int proc_dir_read(DIR *d, pid_t *ret) {
2264
        assert(d);
978✔
2265

2266
        for (;;) {
1,642✔
2267
                struct dirent *de;
1,642✔
2268

2269
                errno = 0;
1,642✔
2270
                de = readdir_no_dot(d);
1,642✔
2271
                if (!de) {
1,642✔
2272
                        if (errno != 0)
11✔
2273
                                return -errno;
×
2274

2275
                        break;
11✔
2276
                }
2277

2278
                if (!IN_SET(de->d_type, DT_DIR, DT_UNKNOWN))
1,631✔
2279
                        continue;
543✔
2280

2281
                if (parse_pid(de->d_name, ret) >= 0)
1,088✔
2282
                        return 1;
2283
        }
2284

2285
        if (ret)
11✔
2286
                *ret = 0;
11✔
2287
        return 0;
2288
}
2289

2290
int proc_dir_read_pidref(DIR *d, PidRef *ret) {
2291
        int r;
929✔
2292

2293
        assert(d);
929✔
2294

2295
        for (;;) {
939✔
2296
                pid_t pid;
934✔
2297

2298
                r = proc_dir_read(d, &pid);
934✔
2299
                if (r < 0)
934✔
2300
                        return r;
919✔
2301
                if (r == 0)
934✔
2302
                        break;
2303

2304
                r = pidref_set_pid(ret, pid);
924✔
2305
                if (r == -ESRCH) /* gone by now? skip it */
924✔
2306
                        continue;
5✔
2307
                if (r < 0)
919✔
2308
                        return r;
×
2309

2310
                return 1;
2311
        }
2312

2313
        if (ret)
10✔
2314
                *ret = PIDREF_NULL;
10✔
2315
        return 0;
2316
}
2317

2318
static const char *const sigchld_code_table[] = {
2319
        [CLD_EXITED] = "exited",
2320
        [CLD_KILLED] = "killed",
2321
        [CLD_DUMPED] = "dumped",
2322
        [CLD_TRAPPED] = "trapped",
2323
        [CLD_STOPPED] = "stopped",
2324
        [CLD_CONTINUED] = "continued",
2325
};
2326

2327
DEFINE_STRING_TABLE_LOOKUP(sigchld_code, int);
7,639✔
2328

2329
static const char* const sched_policy_table[] = {
2330
        [SCHED_OTHER] = "other",
2331
        [SCHED_BATCH] = "batch",
2332
        [SCHED_IDLE] = "idle",
2333
        [SCHED_FIFO] = "fifo",
2334
        [SCHED_RR] = "rr",
2335
};
2336

2337
DEFINE_STRING_TABLE_LOOKUP_WITH_FALLBACK(sched_policy, int, INT_MAX);
3✔
2338

2339
_noreturn_ void report_errno_and_exit(int errno_fd, int error) {
2340
        int r;
48✔
2341

2342
        if (error >= 0)
48✔
2343
                _exit(EXIT_SUCCESS);
47✔
2344

2345
        assert(errno_fd >= 0);
1✔
2346

2347
        r = loop_write(errno_fd, &error, sizeof(error));
1✔
2348
        if (r < 0)
1✔
2349
                log_debug_errno(r, "Failed to write errno to errno_fd=%d: %m", errno_fd);
×
2350

2351
        _exit(EXIT_FAILURE);
1✔
2352
}
2353

2354
int read_errno(int errno_fd) {
2355
        int r;
1✔
2356

2357
        assert(errno_fd >= 0);
1✔
2358

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

2362
        ssize_t n = loop_read(errno_fd, &r, sizeof(r), /* do_poll = */ false);
1✔
2363
        if (n < 0) {
1✔
2364
                log_debug_errno(n, "Failed to read errno: %m");
×
2365
                return -EIO;
×
2366
        }
2367
        if (n == sizeof(r)) {
1✔
2368
                if (r == 0)
×
2369
                        return 0;
2370
                if (r < 0) /* child process reported an error, return it */
×
2371
                        return log_debug_errno(r, "Child process failed with errno: %m");
×
2372
                return log_debug_errno(SYNTHETIC_ERRNO(EIO), "Received an errno, but it's a positive value.");
×
2373
        }
2374
        if (n != 0)
1✔
2375
                return log_debug_errno(SYNTHETIC_ERRNO(EIO), "Received unexpected amount of bytes while reading errno.");
×
2376

2377
        /* the process exited without reporting an error, assuming success */
2378
        return 0;
2379
}
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