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

systemd / systemd / 15512929258

06 Jun 2025 10:28PM UTC coverage: 72.058% (-0.03%) from 72.092%
15512929258

push

github

poettering
nspawn: do basic port to PidRef

THis is sometimes a bit superficial, but in many cases allows us to use
pidfd for various of our operations.

75 of 86 new or added lines in 4 files covered. (87.21%)

3361 existing lines in 68 files now uncovered.

300145 of 416533 relevant lines covered (72.06%)

703207.48 hits per line

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

75.9
/src/shared/exec-util.c
1
/* SPDX-License-Identifier: LGPL-2.1-or-later */
2

3
#include <stdio.h>
4
#include <stdlib.h>
5
#include <unistd.h>
6

7
#include "alloc-util.h"
8
#include "bitfield.h"
9
#include "conf-files.h"
10
#include "env-file.h"
11
#include "env-util.h"
12
#include "errno-util.h"
13
#include "escape.h"
14
#include "exec-util.h"
15
#include "fd-util.h"
16
#include "fileio.h"
17
#include "hashmap.h"
18
#include "log.h"
19
#include "path-util.h"
20
#include "process-util.h"
21
#include "serialize.h"
22
#include "stat-util.h"
23
#include "string-table.h"
24
#include "string-util.h"
25
#include "strv.h"
26
#include "terminal-util.h"
27
#include "time-util.h"
28

29
#define EXIT_SKIP_REMAINING 77
30

31
/* Put this test here for a lack of better place */
32
assert_cc(EAGAIN == EWOULDBLOCK);
33

34
static int do_spawn(
1,439✔
35
                const char *path,
36
                char *argv[],
37
                int stdout_fd,
38
                bool set_systemd_exec_pid,
39
                pid_t *ret_pid) {
40

41
        int r;
1,439✔
42

43
        assert(path);
1,439✔
44
        assert(ret_pid);
1,439✔
45

46
        if (null_or_empty_path(path) > 0) {
1,439✔
47
                log_debug("%s is masked, skipping.", path);
×
48
                return 0;
×
49
        }
50

51
        pid_t pid;
1,439✔
52
        r = safe_fork_full(
5,756✔
53
                        "(exec-inner)",
54
                        (const int[]) { STDIN_FILENO, stdout_fd < 0 ? STDOUT_FILENO : stdout_fd, STDERR_FILENO },
1,631✔
55
                        /* except_fds= */ NULL, /* n_except_fds= */ 0,
56
                        FORK_DEATHSIG_SIGTERM|FORK_LOG|FORK_RLIMIT_NOFILE_SAFE|FORK_REARRANGE_STDIO|FORK_CLOSE_ALL_FDS,
57
                        &pid);
58
        if (r < 0)
2,878✔
59
                return r;
60
        if (r == 0) {
2,878✔
61
                char *_argv[2];
1,439✔
62

63
                if (set_systemd_exec_pid) {
1,439✔
64
                        r = setenv_systemd_exec_pid(false);
1,407✔
65
                        if (r < 0)
1,407✔
66
                                log_warning_errno(r, "Failed to set $SYSTEMD_EXEC_PID, ignoring: %m");
×
67
                }
68

69
                if (!argv) {
1,439✔
70
                        _argv[0] = (char*) path;
1,252✔
71
                        _argv[1] = NULL;
1,252✔
72
                        argv = _argv;
1,252✔
73
                } else
74
                        argv[0] = (char*) path;
187✔
75

76
                execv(path, argv);
1,439✔
77
                log_error_errno(errno, "Failed to execute %s: %m", path);
1,439✔
78
                _exit(EXIT_FAILURE);
×
79
        }
80

81
        *ret_pid = pid;
1,439✔
82
        return 1;
1,439✔
83
}
84

85
static int do_execute(
1,425✔
86
                char * const *paths,
87
                const char *root,
88
                usec_t timeout,
89
                gather_stdout_callback_t const callbacks[_STDOUT_CONSUME_MAX],
90
                void * const callback_args[_STDOUT_CONSUME_MAX],
91
                int output_fd,
92
                char *argv[],
93
                char *envp[],
94
                ExecDirFlags flags) {
95

96
        _cleanup_hashmap_free_ Hashmap *pids = NULL;
1,425✔
97
        bool parallel_execution;
1,425✔
98
        int r;
1,425✔
99

100
        /* We fork this all off from a child process so that we can somewhat cleanly make use of SIGALRM
101
         * to set a time limit.
102
         *
103
         * We attempt to perform parallel execution if configured by the user, however if `callbacks` is nonnull,
104
         * execution must be serial.
105
         */
106

107
        assert(!strv_isempty(paths));
1,425✔
108

109
        parallel_execution = FLAGS_SET(flags, EXEC_DIR_PARALLEL) && !callbacks;
1,425✔
110

111
        /* Abort execution of this process after the timeout. We simply rely on SIGALRM as
112
         * default action terminating the process, and turn on alarm(). */
113

114
        if (timeout != USEC_INFINITY)
1,425✔
115
                alarm(DIV_ROUND_UP(timeout, USEC_PER_SEC));
1,425✔
116

117
        STRV_FOREACH(e, envp)
8,630✔
118
                if (putenv(*e) != 0)
7,205✔
119
                        return log_error_errno(errno, "Failed to set environment variable: %m");
×
120

121
        STRV_FOREACH(path, paths) {
2,863✔
122
                _cleanup_free_ char *t = NULL;
1,439✔
123
                _cleanup_close_ int fd = -EBADF;
1,439✔
124
                pid_t pid;
1,439✔
125

126
                t = path_join(root, *path);
1,439✔
127
                if (!t)
1,439✔
128
                        return log_oom();
×
129

130
                if (callbacks) {
1,439✔
131
                        _cleanup_free_ char *bn = NULL;
1,247✔
132

133
                        r = path_extract_filename(*path, &bn);
1,247✔
134
                        if (r < 0)
1,247✔
135
                                return log_error_errno(r, "Failed to extract filename from path '%s': %m", *path);
×
136

137
                        fd = open_serialization_fd(bn);
1,247✔
138
                        if (fd < 0)
1,247✔
139
                                return log_error_errno(fd, "Failed to open serialization file: %m");
×
140
                }
141

142
                if (DEBUG_LOGGING) {
1,439✔
143
                        _cleanup_free_ char *s = NULL;
1,010✔
144

145
                        char **args = strv_skip(argv, 1);
1,010✔
146
                        if (args)
1,010✔
147
                                s = quote_command_line(args, SHELL_ESCAPE_EMPTY);
×
148

149
                        log_debug("About to execute %s%s%s", t, args ? " " : "", args ? strnull(s) : "");
1,010✔
150
                }
151

152
                if (FLAGS_SET(flags, EXEC_DIR_WARN_WORLD_WRITABLE)) {
1,439✔
153
                        struct stat st;
176✔
154

155
                        r = stat(t, &st);
176✔
156
                        if (r < 0)
176✔
157
                                log_warning_errno(errno, "Failed to stat '%s', ignoring: %m", t);
176✔
158
                        else if (S_ISREG(st.st_mode) && (st.st_mode & 0002))
176✔
159
                                log_warning("'%s' is marked world-writable, which is a security risk as it "
×
160
                                            "is executed with privileges. Please remove world writability "
161
                                            "permission bits. Proceeding anyway.", t);
162
                }
163

164
                r = do_spawn(t, argv, fd, FLAGS_SET(flags, EXEC_DIR_SET_SYSTEMD_EXEC_PID), &pid);
1,439✔
165
                if (r <= 0)
1,439✔
166
                        continue;
×
167

168
                if (parallel_execution) {
1,439✔
169
                        r = hashmap_ensure_put(&pids, &trivial_hash_ops_value_free, PID_TO_PTR(pid), t);
190✔
170
                        if (r < 0)
190✔
171
                                return log_oom();
×
172
                        t = NULL;
173
                } else {
174
                        bool skip_remaining = false;
1,249✔
175

176
                        r = wait_for_terminate_and_check(t, pid, WAIT_LOG_ABNORMAL);
1,249✔
177
                        if (r < 0)
1,249✔
178
                                return r;
179
                        if (r > 0) {
1,249✔
180
                                if (FLAGS_SET(flags, EXEC_DIR_SKIP_REMAINING) && r == EXIT_SKIP_REMAINING) {
1✔
181
                                        log_info("%s succeeded with exit status %i, not executing remaining executables.", *path, r);
×
182
                                        skip_remaining = true;
183
                                } else if (FLAGS_SET(flags, EXEC_DIR_IGNORE_ERRORS))
1✔
184
                                        log_warning("%s failed with exit status %i, ignoring.", *path, r);
×
185
                                else {
186
                                        log_error("%s failed with exit status %i.", *path, r);
1✔
187
                                        return r;
1✔
188
                                }
189
                        }
190

191
                        if (callbacks) {
1,248✔
192
                                r = finish_serialization_fd(fd);
1,247✔
193
                                if (r < 0)
1,247✔
194
                                        return log_error_errno(r, "Failed to finish serialization fd: %m");
×
195

196
                                r = callbacks[STDOUT_GENERATE](TAKE_FD(fd), callback_args[STDOUT_GENERATE]);
1,247✔
197
                                if (r < 0)
1,247✔
198
                                        return log_error_errno(r, "Failed to process output from %s: %m", *path);
×
199
                        }
200

201
                        if (skip_remaining)
1,248✔
202
                                break;
203
                }
204
        }
205

206
        if (callbacks) {
1,424✔
207
                r = callbacks[STDOUT_COLLECT](output_fd, callback_args[STDOUT_COLLECT]);
1,236✔
208
                if (r < 0)
1,236✔
209
                        return log_error_errno(r, "Callback two failed: %m");
×
210
        }
211

212
        while (!hashmap_isempty(pids)) {
1,614✔
213
                _cleanup_free_ char *t = NULL;
190✔
214
                pid_t pid;
190✔
215
                void *p;
190✔
216

217
                t = ASSERT_PTR(hashmap_steal_first_key_and_value(pids, &p));
190✔
218
                pid = PTR_TO_PID(p);
190✔
219
                assert(pid > 0);
190✔
220

221
                r = wait_for_terminate_and_check(t, pid, WAIT_LOG);
190✔
222
                if (r < 0)
190✔
223
                        return r;
224
                if (!FLAGS_SET(flags, EXEC_DIR_IGNORE_ERRORS) && r > 0)
190✔
225
                        return r;
226
        }
227

228
        return 0;
229
}
230

231
int execute_strv(
884✔
232
                const char *name,
233
                char * const *paths,
234
                const char *root,
235
                usec_t timeout,
236
                gather_stdout_callback_t const callbacks[_STDOUT_CONSUME_MAX],
237
                void * const callback_args[_STDOUT_CONSUME_MAX],
238
                char *argv[],
239
                char *envp[],
240
                ExecDirFlags flags) {
241

242
        _cleanup_close_ int fd = -EBADF;
884✔
243
        pid_t executor_pid;
884✔
244
        int r;
884✔
245

246
        assert(!FLAGS_SET(flags, EXEC_DIR_PARALLEL | EXEC_DIR_SKIP_REMAINING));
884✔
247

248
        if (strv_isempty(paths))
1,768✔
249
                return 0;
250

251
        if (callbacks) {
884✔
252
                assert(name);
706✔
253
                assert(callbacks[STDOUT_GENERATE]);
706✔
254
                assert(callbacks[STDOUT_COLLECT]);
706✔
255
                assert(callbacks[STDOUT_CONSUME]);
706✔
256
                assert(callback_args);
706✔
257

258
                fd = open_serialization_fd(name);
706✔
259
                if (fd < 0)
706✔
260
                        return log_error_errno(fd, "Failed to open serialization file: %m");
×
261
        }
262

263
        /* Executes all binaries in the directories serially or in parallel and waits for
264
         * them to finish. Optionally a timeout is applied. If a file with the same name
265
         * exists in more than one directory, the earliest one wins. */
266

267
        r = safe_fork("(sd-exec-strv)", FORK_RESET_SIGNALS|FORK_DEATHSIG_SIGTERM|FORK_LOG, &executor_pid);
884✔
268
        if (r < 0)
2,309✔
269
                return r;
270
        if (r == 0) {
2,309✔
271
                r = do_execute(paths, root, timeout, callbacks, callback_args, fd, argv, envp, flags);
1,425✔
272
                _exit(r < 0 ? EXIT_FAILURE : r);
1,425✔
273
        }
274

275
        r = wait_for_terminate_and_check("(sd-exec-strv)", executor_pid, 0);
884✔
276
        if (r < 0)
884✔
277
                return r;
278
        if (!FLAGS_SET(flags, EXEC_DIR_IGNORE_ERRORS) && r > 0)
884✔
279
                return r;
280

281
        if (!callbacks)
883✔
282
                return 0;
283

284
        r = finish_serialization_fd(fd);
706✔
285
        if (r < 0)
706✔
286
                return log_error_errno(r, "Failed to finish serialization fd: %m");
×
287

288
        r = callbacks[STDOUT_CONSUME](TAKE_FD(fd), callback_args[STDOUT_CONSUME]);
706✔
289
        if (r < 0)
706✔
290
                return log_error_errno(r, "Failed to parse returned data: %m");
×
291

292
        return 0;
293
}
294

295
int execute_directories(
884✔
296
                const char * const *directories,
297
                usec_t timeout,
298
                gather_stdout_callback_t const callbacks[_STDOUT_CONSUME_MAX],
299
                void * const callback_args[_STDOUT_CONSUME_MAX],
300
                char *argv[],
301
                char *envp[],
302
                ExecDirFlags flags) {
303

304
        _cleanup_strv_free_ char **paths = NULL;
×
305
        _cleanup_free_ char *name = NULL;
884✔
306
        int r;
884✔
307

308
        assert(!strv_isempty((char* const*) directories));
884✔
309

310
        r = conf_files_list_strv(
884✔
311
                        &paths,
312
                        /* suffix= */ NULL,
313
                        /* root= */ NULL,
314
                        CONF_FILES_EXECUTABLE|CONF_FILES_REGULAR|CONF_FILES_FILTER_MASKED,
315
                        directories);
316
        if (r < 0)
884✔
UNCOV
317
                return log_error_errno(r, "Failed to enumerate executables: %m");
×
318

319
        if (strv_isempty(paths)) {
884✔
UNCOV
320
                log_debug("No executables found.");
×
UNCOV
321
                return 0;
×
322
        }
323

324
        if (callbacks) {
884✔
325
                r = path_extract_filename(directories[0], &name);
706✔
326
                if (r < 0)
706✔
UNCOV
327
                        return log_error_errno(r, "Failed to extract file name from '%s': %m", directories[0]);
×
328
        }
329

330
        return execute_strv(name, paths, /* root = */ NULL, timeout, callbacks, callback_args, argv, envp, flags);
884✔
331
}
332

333
static int gather_environment_generate(int fd, void *arg) {
1,237✔
334
        char ***env = ASSERT_PTR(arg);
1,237✔
335
        _cleanup_fclose_ FILE *f = NULL;
1,237✔
336
        _cleanup_strv_free_ char **new = NULL;
1,237✔
337
        int r;
1,237✔
338

339
        /* Read a series of VAR=value assignments from fd, use them to update the list of variables in env.
340
         * Also update the exported environment.
341
         *
342
         * fd is always consumed, even on error.
343
         */
344

345
        assert(fd >= 0);
1,237✔
346

347
        f = fdopen(fd, "r");
1,237✔
348
        if (!f) {
1,237✔
UNCOV
349
                safe_close(fd);
×
UNCOV
350
                return -errno;
×
351
        }
352

353
        r = load_env_file_pairs(f, NULL, &new);
1,237✔
354
        if (r < 0)
1,237✔
355
                return r;
356

357
        STRV_FOREACH_PAIR(x, y, new) {
2,316✔
358
                if (!env_name_is_valid(*x)) {
1,079✔
359
                        log_warning("Invalid variable assignment \"%s=...\", ignoring.", *x);
4✔
360
                        continue;
4✔
361
                }
362

363
                r = strv_env_assign(env, *x, *y);
1,075✔
364
                if (r < 0)
1,075✔
365
                        return r;
366

367
                if (setenv(*x, *y, /* overwrite = */ true) < 0)
1,075✔
UNCOV
368
                        return -errno;
×
369
        }
370

371
        return 0;
372
}
373

374
static int gather_environment_collect(int fd, void *arg) {
1,233✔
375
        char ***env = ASSERT_PTR(arg);
1,233✔
376
        _cleanup_fclose_ FILE *f = NULL;
1,233✔
377
        int r;
1,233✔
378

379
        /* Write out a series of env=cescape(VAR=value) assignments to fd. */
380

381
        assert(fd >= 0);
1,233✔
382

383
        f = fdopen(fd, "w");
1,233✔
384
        if (!f) {
1,233✔
UNCOV
385
                safe_close(fd);
×
UNCOV
386
                return -errno;
×
387
        }
388

389
        r = serialize_strv(f, "env", *env);
1,233✔
390
        if (r < 0)
1,233✔
391
                return r;
392

393
        r = fflush_and_check(f);
1,233✔
394
        if (r < 0)
1,233✔
UNCOV
395
                return r;
×
396

397
        return 0;
398
}
399

400
static int gather_environment_consume(int fd, void *arg) {
703✔
401
        char ***env = ASSERT_PTR(arg);
703✔
402
        _cleanup_fclose_ FILE *f = NULL;
703✔
403
        int r, ret = 0;
703✔
404

405
        /* Read a series of env=cescape(VAR=value) assignments from fd into env. */
406

407
        assert(fd >= 0);
703✔
408

409
        f = fdopen(fd, "r");
703✔
410
        if (!f) {
703✔
UNCOV
411
                safe_close(fd);
×
UNCOV
412
                return -errno;
×
413
        }
414

415
        for (;;) {
1,244✔
416
                _cleanup_free_ char *line = NULL;
1,244✔
417
                const char *v;
1,244✔
418

419
                r = read_line(f, LONG_LINE_MAX, &line);
1,244✔
420
                if (r < 0)
1,244✔
421
                        return r;
422
                if (r == 0)
1,244✔
423
                        return ret;
424

425
                v = startswith(line, "env=");
541✔
426
                if (!v) {
541✔
UNCOV
427
                        RET_GATHER(ret, log_debug_errno(SYNTHETIC_ERRNO(EINVAL),
×
428
                                                        "Serialization line unexpectedly didn't start with \"env=\", ignoring: %s",
429
                                                        line));
430
                        continue;
×
431
                }
432

433
                r = deserialize_environment(v, env);
541✔
434
                if (r < 0)
541✔
UNCOV
435
                        RET_GATHER(ret, log_debug_errno(r, "Failed to deserialize line \"%s\": %m", line));
×
436
        }
437
}
438

439
const gather_stdout_callback_t gather_environment[_STDOUT_CONSUME_MAX] = {
440
        gather_environment_generate,
441
        gather_environment_collect,
442
        gather_environment_consume,
443
};
444

445
int exec_command_flags_from_strv(char * const *ex_opts, ExecCommandFlags *ret) {
326✔
446
        ExecCommandFlags flags = 0;
326✔
447

448
        assert(ret);
326✔
449

450
        STRV_FOREACH(opt, ex_opts) {
364✔
451
                ExecCommandFlags fl = exec_command_flags_from_string(*opt);
39✔
452
                if (fl < 0)
39✔
453
                        return fl;
454

455
                flags |= fl;
38✔
456
        }
457

458
        *ret = flags;
325✔
459

460
        return 0;
325✔
461
}
462

463
int exec_command_flags_to_strv(ExecCommandFlags flags, char ***ret) {
1,568✔
464
        _cleanup_strv_free_ char **opts = NULL;
1,568✔
465
        int r;
1,568✔
466

467
        assert(flags >= 0);
1,568✔
468
        assert(ret);
1,568✔
469

470
        BIT_FOREACH(i, flags) {
1,821✔
471
                const char *s = exec_command_flags_to_string(1 << i);
254✔
472
                if (!s)
254✔
473
                        return -EINVAL;
474

475
                r = strv_extend(&opts, s);
253✔
476
                if (r < 0)
253✔
477
                        return r;
478
        }
479

480
        *ret = TAKE_PTR(opts);
1,567✔
481

482
        return 0;
1,567✔
483
}
484

485
static const char* const exec_command_strings[] = {
486
        "ignore-failure", /* EXEC_COMMAND_IGNORE_FAILURE */
487
        "privileged",     /* EXEC_COMMAND_FULLY_PRIVILEGED */
488
        "no-setuid",      /* EXEC_COMMAND_NO_SETUID */
489
        "no-env-expand",  /* EXEC_COMMAND_NO_ENV_EXPAND */
490
        "via-shell",      /* EXEC_COMMAND_VIA_SHELL */
491
};
492

493
assert_cc((1 << ELEMENTSOF(exec_command_strings)) - 1 == _EXEC_COMMAND_FLAGS_ALL);
494

495
const char* exec_command_flags_to_string(ExecCommandFlags i) {
254✔
496
        for (size_t idx = 0; idx < ELEMENTSOF(exec_command_strings); idx++)
862✔
497
                if (i == (1 << idx))
861✔
498
                        return exec_command_strings[idx];
253✔
499

500
        return NULL;
501
}
502

503
ExecCommandFlags exec_command_flags_from_string(const char *s) {
39✔
504
        ssize_t idx;
39✔
505

506
        if (streq(s, "ambient")) /* Compatibility with ambient hack, removed in v258, map to no bits set */
39✔
507
                return 0;
508

509
        idx = string_table_lookup_from_string(exec_command_strings, ELEMENTSOF(exec_command_strings), s);
39✔
510
        if (idx < 0)
39✔
511
                return _EXEC_COMMAND_FLAGS_INVALID;
512

513
        return 1 << idx;
38✔
514
}
515

516
int fexecve_or_execve(int executable_fd, const char *executable, char *const argv[], char *const envp[]) {
21,620✔
517
        /* Refuse invalid fds, regardless if fexecve() use is enabled or not */
518
        if (executable_fd < 0)
21,620✔
519
                return -EBADF;
520

521
        /* Block any attempts on exploiting Linux' liberal argv[] handling, i.e. CVE-2021-4034 and suchlike */
522
        if (isempty(executable) || strv_isempty(argv))
43,240✔
523
                return -EINVAL;
524

525
#if ENABLE_FEXECVE
526

527
        execveat(executable_fd, "", argv, envp, AT_EMPTY_PATH);
528

529
        if (IN_SET(errno, ENOSYS, ENOENT) || ERRNO_IS_PRIVILEGE(errno))
530
                /* Old kernel or a script or an overzealous seccomp filter? Let's fall back to execve().
531
                 *
532
                 * fexecve(3): "If fd refers to a script (i.e., it is an executable text file that names a
533
                 * script interpreter with a first line that begins with the characters #!) and the
534
                 * close-on-exec flag has been set for fd, then fexecve() fails with the error ENOENT. This
535
                 * error occurs because, by the time the script interpreter is executed, fd has already been
536
                 * closed because of the close-on-exec flag. Thus, the close-on-exec flag can't be set on fd
537
                 * if it refers to a script."
538
                 *
539
                 * Unfortunately, if we unset close-on-exec, the script will be executed just fine, but (at
540
                 * least in case of bash) the script name, $0, will be shown as /dev/fd/nnn, which breaks
541
                 * scripts which make use of $0. Thus, let's fall back to execve() in this case.
542
                 */
543
#endif
544
                execve(executable, argv, envp);
21,620✔
545
        return -errno;
21,620✔
546
}
547

548
int shall_fork_agent(void) {
3,110✔
549
        int r;
3,110✔
550

551
        /* Check if we have a controlling terminal. If not (ENXIO here), we aren't actually invoked
552
         * interactively on a terminal, hence fail. */
553
        r = get_ctty_devnr(0, NULL);
3,110✔
554
        if (r == -ENXIO)
3,110✔
555
                return false;
UNCOV
556
        if (r < 0)
×
557
                return r;
558

UNCOV
559
        if (!is_main_thread())
×
560
                return -EPERM;
×
561

562
        return true;
563
}
564

UNCOV
565
int _fork_agent(const char *name, char * const *argv, const int except[], size_t n_except, pid_t *ret_pid) {
×
UNCOV
566
        int r;
×
567

UNCOV
568
        assert(!strv_isempty(argv));
×
569

570
        /* Spawns a temporary TTY agent, making sure it goes away when we go away */
571

UNCOV
572
        r = safe_fork_full(name,
×
573
                           NULL,
574
                           (int*) except, /* safe_fork_full only changes except if you pass in FORK_PACK_FDS, which we don't */
575
                           n_except,
576
                           FORK_RESET_SIGNALS|FORK_DEATHSIG_SIGTERM|FORK_CLOSE_ALL_FDS|FORK_REOPEN_LOG|FORK_RLIMIT_NOFILE_SAFE,
577
                           ret_pid);
UNCOV
578
        if (r < 0)
×
579
                return r;
580
        if (r > 0)
×
581
                return 0;
582

583
        /* In the child: */
584

585
        bool stdin_is_tty = isatty_safe(STDIN_FILENO),
×
UNCOV
586
                stdout_is_tty = isatty_safe(STDOUT_FILENO),
×
UNCOV
587
                stderr_is_tty = isatty_safe(STDERR_FILENO);
×
588

UNCOV
589
        if (!stdin_is_tty || !stdout_is_tty || !stderr_is_tty) {
×
UNCOV
590
                int fd;
×
591

592
                /* Detach from stdin/stdout/stderr and reopen /dev/tty for them. This is important to ensure
593
                 * that when systemctl is started via popen() or a similar call that expects to read EOF we
594
                 * actually do generate EOF and not delay this indefinitely by keeping an unused copy of
595
                 * stdin around. */
UNCOV
596
                fd = open_terminal("/dev/tty", stdin_is_tty ? O_WRONLY : (stdout_is_tty && stderr_is_tty) ? O_RDONLY : O_RDWR);
×
597
                if (fd < 0) {
×
598
                        log_error_errno(fd, "Failed to open %s: %m", "/dev/tty");
×
599
                        _exit(EXIT_FAILURE);
×
600
                }
601

602
                if (!stdin_is_tty && dup2(fd, STDIN_FILENO) < 0) {
×
603
                        log_error_errno(errno, "Failed to dup2 /dev/tty to STDIN: %m");
×
604
                        _exit(EXIT_FAILURE);
×
605
                }
606

607
                if (!stdout_is_tty && dup2(fd, STDOUT_FILENO) < 0) {
×
608
                        log_error_errno(errno, "Failed to dup2 /dev/tty to STDOUT: %m");
×
609
                        _exit(EXIT_FAILURE);
×
610
                }
611

612
                if (!stderr_is_tty && dup2(fd, STDERR_FILENO) < 0) {
×
UNCOV
613
                        log_error_errno(errno, "Failed to dup2 /dev/tty to STDERR: %m");
×
UNCOV
614
                        _exit(EXIT_FAILURE);
×
615
                }
616

UNCOV
617
                fd = safe_close_above_stdio(fd);
×
618
        }
619

620
        /* Count arguments */
UNCOV
621
        execv(argv[0], argv);
×
622

623
        /* Let's treat missing agent binary as a graceful issue (in order to support splitting out the Polkit
624
         * or password agents into separate, optional distro packages), and not complain loudly. */
UNCOV
625
        log_full_errno(errno == ENOENT ? LOG_DEBUG : LOG_ERR, errno,
×
626
                       "Failed to execute %s: %m", argv[0]);
UNCOV
627
        _exit(EXIT_FAILURE);
×
628
}
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