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

systemd / systemd / 19720950421

26 Nov 2025 06:54PM UTC coverage: 72.89% (+0.4%) from 72.517%
19720950421

push

github

web-flow
Symlink for the /dev/ptp0 in vmware (#39917)

There are rules to create a symlink for the /dev/ptp0 device in KVM and
Hyper-V virtualization infrastructure but not for vmware.

310103 of 425438 relevant lines covered (72.89%)

1128896.62 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(
3,028✔
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;
3,028✔
42

43
        assert(path);
3,028✔
44
        assert(ret_pid);
3,028✔
45

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

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

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

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

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

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

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

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

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

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

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

130
                if (callbacks) {
3,028✔
131
                        _cleanup_free_ char *bn = NULL;
1,335✔
132

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

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

142
                if (DEBUG_LOGGING) {
3,028✔
143
                        _cleanup_free_ char *s = NULL;
2,577✔
144

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

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

152
                if (FLAGS_SET(flags, EXEC_DIR_WARN_WORLD_WRITABLE)) {
3,028✔
153
                        struct stat st;
1,677✔
154

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

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

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

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

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

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

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

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

228
        return 0;
229
}
230

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

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

249
        if (strv_isempty(paths))
2,240✔
250
                return 0;
251

252
        if (callbacks) {
1,120✔
253
                assert(callbacks[STDOUT_GENERATE]);
836✔
254
                assert(callbacks[STDOUT_COLLECT]);
836✔
255
                assert(callbacks[STDOUT_CONSUME]);
836✔
256
                assert(callback_args);
836✔
257

258
                fd = open_serialization_fd(name);
836✔
259
                if (fd < 0)
836✔
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, ")");
7,840✔
268

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

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

283
        if (!callbacks)
1,119✔
284
                return 0;
285

286
        r = finish_serialization_fd(fd);
836✔
287
        if (r < 0)
836✔
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]);
836✔
291
        if (r < 0)
836✔
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(
1,120✔
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;
1,120✔
308
        int r;
1,120✔
309

310
        assert(name);
1,120✔
311
        assert(!strv_isempty((char* const*) directories));
1,120✔
312

313
        r = conf_files_list_strv(
1,120✔
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)
1,120✔
320
                return log_error_errno(r, "%s: failed to enumerate executables: %m", name);
×
321

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

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

330
static int gather_environment_generate(int fd, void *arg) {
1,325✔
331
        char ***env = ASSERT_PTR(arg);
1,325✔
332
        _cleanup_fclose_ FILE *f = NULL;
1,325✔
333
        _cleanup_strv_free_ char **new = NULL;
1,325✔
334
        int r;
1,325✔
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,325✔
343

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

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

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

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

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

368
        return 0;
369
}
370

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

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

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

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

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

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

394
        return 0;
395
}
396

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

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

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

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

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

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

422
                v = startswith(line, "env=");
658✔
423
                if (!v) {
658✔
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);
658✔
431
                if (r < 0)
658✔
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) {
490✔
443
        ExecCommandFlags flags = 0;
490✔
444

445
        assert(ret);
490✔
446

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

452
                flags |= fl;
215✔
453
        }
454

455
        *ret = flags;
489✔
456

457
        return 0;
489✔
458
}
459

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

464
        assert(flags >= 0);
3,088✔
465
        assert(ret);
3,088✔
466

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

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

477
        *ret = TAKE_PTR(opts);
3,087✔
478

479
        return 0;
3,087✔
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) {
1,386✔
493
        for (size_t idx = 0; idx < ELEMENTSOF(exec_command_strings); idx++)
5,525✔
494
                if (i == (1 << idx))
5,524✔
495
                        return exec_command_strings[idx];
1,385✔
496

497
        return NULL;
498
}
499

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

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

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

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

513
int fexecve_or_execve(int executable_fd, const char *executable, char *const argv[], char *const envp[]) {
23,220✔
514
        /* Refuse invalid fds, regardless if fexecve() use is enabled or not */
515
        if (executable_fd < 0)
23,220✔
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))
46,440✔
520
                return -EINVAL;
521

522
#if ENABLE_FEXECVE
523

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

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

546
int shall_fork_agent(void) {
4,331✔
547
        int r;
4,331✔
548

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

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

560
        return true;
561
}
562

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

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

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

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

581
        /* In the child: */
582

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

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

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

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

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

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

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

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

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