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

systemd / systemd / 17813902210

17 Sep 2025 11:56PM UTC coverage: 72.24% (-0.04%) from 72.281%
17813902210

push

github

web-flow
sysupdate: use conf_files_list_strv_full() where possible (#38198)

37 of 44 new or added lines in 1 file covered. (84.09%)

6236 existing lines in 79 files now uncovered.

302695 of 419015 relevant lines covered (72.24%)

1046897.1 hits per line

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

76.36
/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,508✔
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,508✔
42

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

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

51
        pid_t pid;
1,508✔
52
        r = safe_fork_full(
6,032✔
53
                        "(exec-inner)",
54
                        (const int[]) { STDIN_FILENO, stdout_fd < 0 ? STDOUT_FILENO : stdout_fd, STDERR_FILENO },
1,712✔
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)
3,016✔
59
                return r;
60
        if (r == 0) {
3,016✔
61
                char *_argv[2];
1,508✔
62

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

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

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

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

85
static int do_execute(
1,494✔
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,494✔
97
        bool parallel_execution;
1,494✔
98
        int r;
1,494✔
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,494✔
108

109
        parallel_execution = FLAGS_SET(flags, EXEC_DIR_PARALLEL) && !callbacks;
1,494✔
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,494✔
115
                alarm(DIV_ROUND_UP(timeout, USEC_PER_SEC));
1,494✔
116

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

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

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

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

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

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

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

145
                        char **args = strv_skip(argv, 1);
1,049✔
146
                        if (args)
1,049✔
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,049✔
150
                }
151

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

155
                        r = stat(t, &st);
188✔
156
                        if (r < 0)
188✔
157
                                log_warning_errno(errno, "Failed to stat '%s', ignoring: %m", t);
188✔
158
                        else if (S_ISREG(st.st_mode) && (st.st_mode & 0002))
188✔
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,508✔
165
                if (r <= 0)
1,508✔
166
                        continue;
×
167

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

176
                        r = wait_for_terminate_and_check(t, pid, WAIT_LOG_ABNORMAL);
1,306✔
177
                        if (r < 0)
1,306✔
178
                                return r;
179
                        if (r > 0) {
1,306✔
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,305✔
192
                                r = finish_serialization_fd(fd);
1,304✔
193
                                if (r < 0)
1,304✔
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,304✔
197
                                if (r < 0)
1,304✔
198
                                        return log_error_errno(r, "Failed to process output from %s: %m", *path);
×
199
                        }
200

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

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

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

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

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

228
        return 0;
229
}
230

231
int execute_strv(
909✔
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;
909✔
243
        pid_t executor_pid;
909✔
244
        int r;
909✔
245

246
        assert(name);
909✔
247
        assert(!FLAGS_SET(flags, EXEC_DIR_PARALLEL | EXEC_DIR_SKIP_REMAINING));
909✔
248

249
        if (strv_isempty(paths))
1,818✔
250
                return 0;
251

252
        if (callbacks) {
909✔
253
                assert(callbacks[STDOUT_GENERATE]);
719✔
254
                assert(callbacks[STDOUT_COLLECT]);
719✔
255
                assert(callbacks[STDOUT_CONSUME]);
719✔
256
                assert(callback_args);
719✔
257

258
                fd = open_serialization_fd(name);
719✔
259
                if (fd < 0)
719✔
260
                        return log_error_errno(fd, "Failed to open serialization file for %s: %m", name);
×
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
        const char *process_name = strjoina("(", name, ")");
6,363✔
268

269
        r = safe_fork(process_name, FORK_RESET_SIGNALS|FORK_DEATHSIG_SIGTERM|FORK_LOG, &executor_pid);
909✔
270
        if (r < 0)
2,403✔
271
                return r;
272
        if (r == 0) {
2,403✔
273
                r = do_execute(paths, root, timeout, callbacks, callback_args, fd, argv, envp, flags);
1,494✔
274
                _exit(r < 0 ? EXIT_FAILURE : r);
1,494✔
275
        }
276

277
        r = wait_for_terminate_and_check(process_name, executor_pid, 0);
909✔
278
        if (r < 0)
909✔
279
                return r;
280
        if (!FLAGS_SET(flags, EXEC_DIR_IGNORE_ERRORS) && r > 0)
909✔
281
                return r;
282

283
        if (!callbacks)
908✔
284
                return 0;
285

286
        r = finish_serialization_fd(fd);
719✔
287
        if (r < 0)
719✔
UNCOV
288
                return log_error_errno(r, "Failed to finish serialization fd for %s: %m", name);
×
289

290
        r = callbacks[STDOUT_CONSUME](TAKE_FD(fd), callback_args[STDOUT_CONSUME]);
719✔
291
        if (r < 0)
719✔
UNCOV
292
                return log_error_errno(r, "Failed to parse returned data for %s: %m", name);
×
293

294
        return 0;
295
}
296

297
int execute_directories(
909✔
298
                const char *name,
299
                const char * const *directories,
300
                usec_t timeout,
301
                gather_stdout_callback_t const callbacks[_STDOUT_CONSUME_MAX],
302
                void * const callback_args[_STDOUT_CONSUME_MAX],
303
                char *argv[],
304
                char *envp[],
305
                ExecDirFlags flags) {
306

307
        _cleanup_strv_free_ char **paths = NULL;
909✔
308
        int r;
909✔
309

310
        assert(name);
909✔
311
        assert(!strv_isempty((char* const*) directories));
909✔
312

313
        r = conf_files_list_strv(
909✔
314
                        &paths,
315
                        /* suffix= */ NULL,
316
                        /* root= */ NULL,
317
                        CONF_FILES_EXECUTABLE|CONF_FILES_REGULAR|CONF_FILES_FILTER_MASKED,
318
                        directories);
319
        if (r < 0)
909✔
320
                return log_error_errno(r, "%s: failed to enumerate executables: %m", name);
×
321

322
        if (strv_isempty(paths)) {
909✔
UNCOV
323
                log_debug("%s: no executables found.", name);
×
UNCOV
324
                return 0;
×
325
        }
326

327
        return execute_strv(name, paths, /* root = */ NULL, timeout, callbacks, callback_args, argv, envp, flags);
909✔
328
}
329

330
static int gather_environment_generate(int fd, void *arg) {
1,294✔
331
        char ***env = ASSERT_PTR(arg);
1,294✔
332
        _cleanup_fclose_ FILE *f = NULL;
1,294✔
333
        _cleanup_strv_free_ char **new = NULL;
1,294✔
334
        int r;
1,294✔
335

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

342
        assert(fd >= 0);
1,294✔
343

344
        f = fdopen(fd, "r");
1,294✔
345
        if (!f) {
1,294✔
UNCOV
346
                safe_close(fd);
×
UNCOV
347
                return -errno;
×
348
        }
349

350
        r = load_env_file_pairs(f, NULL, &new);
1,294✔
351
        if (r < 0)
1,294✔
352
                return r;
353

354
        STRV_FOREACH_PAIR(x, y, new) {
2,418✔
355
                if (!env_name_is_valid(*x)) {
1,124✔
356
                        log_warning("Invalid variable assignment \"%s=...\", ignoring.", *x);
4✔
357
                        continue;
4✔
358
                }
359

360
                r = strv_env_assign(env, *x, *y);
1,120✔
361
                if (r < 0)
1,120✔
362
                        return r;
363

364
                if (setenv(*x, *y, /* overwrite = */ true) < 0)
1,120✔
UNCOV
365
                        return -errno;
×
366
        }
367

368
        return 0;
369
}
370

371
static int gather_environment_collect(int fd, void *arg) {
1,290✔
372
        char ***env = ASSERT_PTR(arg);
1,290✔
373
        _cleanup_fclose_ FILE *f = NULL;
1,290✔
374
        int r;
1,290✔
375

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

378
        assert(fd >= 0);
1,290✔
379

380
        f = fdopen(fd, "w");
1,290✔
381
        if (!f) {
1,290✔
UNCOV
382
                safe_close(fd);
×
UNCOV
383
                return -errno;
×
384
        }
385

386
        r = serialize_strv(f, "env", *env);
1,290✔
387
        if (r < 0)
1,290✔
388
                return r;
389

390
        r = fflush_and_check(f);
1,290✔
391
        if (r < 0)
1,290✔
UNCOV
392
                return r;
×
393

394
        return 0;
395
}
396

397
static int gather_environment_consume(int fd, void *arg) {
716✔
398
        char ***env = ASSERT_PTR(arg);
716✔
399
        _cleanup_fclose_ FILE *f = NULL;
716✔
400
        int r, ret = 0;
716✔
401

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

404
        assert(fd >= 0);
716✔
405

406
        f = fdopen(fd, "r");
716✔
407
        if (!f) {
716✔
UNCOV
408
                safe_close(fd);
×
UNCOV
409
                return -errno;
×
410
        }
411

412
        for (;;) {
1,258✔
413
                _cleanup_free_ char *line = NULL;
1,258✔
414
                const char *v;
1,258✔
415

416
                r = read_line(f, LONG_LINE_MAX, &line);
1,258✔
417
                if (r < 0)
1,258✔
418
                        return r;
419
                if (r == 0)
1,258✔
420
                        return ret;
421

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

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

436
const gather_stdout_callback_t gather_environment[_STDOUT_CONSUME_MAX] = {
437
        gather_environment_generate,
438
        gather_environment_collect,
439
        gather_environment_consume,
440
};
441

442
int exec_command_flags_from_strv(char * const *ex_opts, ExecCommandFlags *ret) {
323✔
443
        ExecCommandFlags flags = 0;
323✔
444

445
        assert(ret);
323✔
446

447
        STRV_FOREACH(opt, ex_opts) {
362✔
448
                ExecCommandFlags fl = exec_command_flags_from_string(*opt);
40✔
449
                if (fl < 0)
40✔
450
                        return fl;
451

452
                flags |= fl;
39✔
453
        }
454

455
        *ret = flags;
322✔
456

457
        return 0;
322✔
458
}
459

460
int exec_command_flags_to_strv(ExecCommandFlags flags, char ***ret) {
1,408✔
461
        _cleanup_strv_free_ char **opts = NULL;
1,408✔
462
        int r;
1,408✔
463

464
        assert(flags >= 0);
1,408✔
465
        assert(ret);
1,408✔
466

467
        BIT_FOREACH(i, flags) {
1,671✔
468
                const char *s = exec_command_flags_to_string(1 << i);
264✔
469
                if (!s)
264✔
470
                        return -EINVAL;
471

472
                r = strv_extend(&opts, s);
263✔
473
                if (r < 0)
263✔
474
                        return r;
475
        }
476

477
        *ret = TAKE_PTR(opts);
1,407✔
478

479
        return 0;
1,407✔
480
}
481

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

490
assert_cc((1 << ELEMENTSOF(exec_command_strings)) - 1 == _EXEC_COMMAND_FLAGS_ALL);
491

492
const char* exec_command_flags_to_string(ExecCommandFlags i) {
264✔
493
        for (size_t idx = 0; idx < ELEMENTSOF(exec_command_strings); idx++)
935✔
494
                if (i == (1 << idx))
934✔
495
                        return exec_command_strings[idx];
263✔
496

497
        return NULL;
498
}
499

500
ExecCommandFlags exec_command_flags_from_string(const char *s) {
40✔
501
        ssize_t idx;
40✔
502

503
        if (streq(s, "ambient")) /* Compatibility with ambient hack, removed in v258, map to no bits set */
40✔
504
                return 0;
505

506
        idx = string_table_lookup_from_string(exec_command_strings, ELEMENTSOF(exec_command_strings), s);
40✔
507
        if (idx < 0)
40✔
508
                return _EXEC_COMMAND_FLAGS_INVALID;
509

510
        return 1 << idx;
39✔
511
}
512

513
int fexecve_or_execve(int executable_fd, const char *executable, char *const argv[], char *const envp[]) {
22,311✔
514
        /* Refuse invalid fds, regardless if fexecve() use is enabled or not */
515
        if (executable_fd < 0)
22,311✔
516
                return -EBADF;
517

518
        /* Block any attempts on exploiting Linux' liberal argv[] handling, i.e. CVE-2021-4034 and suchlike */
519
        if (isempty(executable) || strv_isempty(argv))
44,622✔
520
                return -EINVAL;
521

522
#if ENABLE_FEXECVE
523

524
        execveat(executable_fd, "", argv, envp, AT_EMPTY_PATH);
525

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

545
int shall_fork_agent(void) {
3,831✔
546
        int r;
3,831✔
547

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

556
        if (!is_main_thread())
×
UNCOV
557
                return -EPERM;
×
558

559
        return true;
560
}
561

UNCOV
562
int _fork_agent(const char *name, char * const *argv, const int except[], size_t n_except, pid_t *ret_pid) {
×
UNCOV
563
        int r;
×
564

565
        assert(!strv_isempty(argv));
×
566

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

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

580
        /* In the child: */
581

UNCOV
582
        bool stdin_is_tty = isatty_safe(STDIN_FILENO),
×
UNCOV
583
                stdout_is_tty = isatty_safe(STDOUT_FILENO),
×
UNCOV
584
                stderr_is_tty = isatty_safe(STDERR_FILENO);
×
585

586
        if (!stdin_is_tty || !stdout_is_tty || !stderr_is_tty) {
×
587
                int fd;
×
588

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

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

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

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

614
                fd = safe_close_above_stdio(fd);
×
615
        }
616

617
        /* Count arguments */
UNCOV
618
        execv(argv[0], argv);
×
619

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