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

systemd / systemd / 15057632786

15 May 2025 09:01PM UTC coverage: 72.267% (+0.02%) from 72.244%
15057632786

push

github

bluca
man: document how to hook stuff into system wakeup

Fixes: #6364

298523 of 413084 relevant lines covered (72.27%)

738132.88 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 <dirent.h>
4
#include <errno.h>
5
#include <stdio.h>
6
#include <sys/prctl.h>
7
#include <sys/types.h>
8
#include <unistd.h>
9

10
#include "alloc-util.h"
11
#include "bitfield.h"
12
#include "conf-files.h"
13
#include "env-file.h"
14
#include "env-util.h"
15
#include "errno-util.h"
16
#include "escape.h"
17
#include "exec-util.h"
18
#include "fd-util.h"
19
#include "fileio.h"
20
#include "hashmap.h"
21
#include "macro.h"
22
#include "missing_syscall.h"
23
#include "path-util.h"
24
#include "process-util.h"
25
#include "serialize.h"
26
#include "set.h"
27
#include "signal-util.h"
28
#include "stat-util.h"
29
#include "string-table.h"
30
#include "string-util.h"
31
#include "strv.h"
32
#include "terminal-util.h"
33

34
#define EXIT_SKIP_REMAINING 77
35

36
/* Put this test here for a lack of better place */
37
assert_cc(EAGAIN == EWOULDBLOCK);
38

39
static int do_spawn(
1,440✔
40
                const char *path,
41
                char *argv[],
42
                int stdout_fd,
43
                bool set_systemd_exec_pid,
44
                pid_t *ret_pid) {
45

46
        int r;
1,440✔
47

48
        assert(path);
1,440✔
49
        assert(ret_pid);
1,440✔
50

51
        if (null_or_empty_path(path) > 0) {
1,440✔
52
                log_debug("%s is masked, skipping.", path);
×
53
                return 0;
×
54
        }
55

56
        pid_t pid;
1,440✔
57
        r = safe_fork_full(
5,760✔
58
                        "(exec-inner)",
59
                        (const int[]) { STDIN_FILENO, stdout_fd < 0 ? STDOUT_FILENO : stdout_fd, STDERR_FILENO },
1,634✔
60
                        /* except_fds= */ NULL, /* n_except_fds= */ 0,
61
                        FORK_DEATHSIG_SIGTERM|FORK_LOG|FORK_RLIMIT_NOFILE_SAFE|FORK_REARRANGE_STDIO|FORK_CLOSE_ALL_FDS,
62
                        &pid);
63
        if (r < 0)
2,880✔
64
                return r;
65
        if (r == 0) {
2,880✔
66
                char *_argv[2];
1,440✔
67

68
                if (set_systemd_exec_pid) {
1,440✔
69
                        r = setenv_systemd_exec_pid(false);
1,408✔
70
                        if (r < 0)
1,408✔
71
                                log_warning_errno(r, "Failed to set $SYSTEMD_EXEC_PID, ignoring: %m");
×
72
                }
73

74
                if (!argv) {
1,440✔
75
                        _argv[0] = (char*) path;
1,251✔
76
                        _argv[1] = NULL;
1,251✔
77
                        argv = _argv;
1,251✔
78
                } else
79
                        argv[0] = (char*) path;
189✔
80

81
                execv(path, argv);
1,440✔
82
                log_error_errno(errno, "Failed to execute %s: %m", path);
1,440✔
83
                _exit(EXIT_FAILURE);
×
84
        }
85

86
        *ret_pid = pid;
1,440✔
87
        return 1;
1,440✔
88
}
89

90
static int do_execute(
1,426✔
91
                char * const *paths,
92
                const char *root,
93
                usec_t timeout,
94
                gather_stdout_callback_t const callbacks[_STDOUT_CONSUME_MAX],
95
                void * const callback_args[_STDOUT_CONSUME_MAX],
96
                int output_fd,
97
                char *argv[],
98
                char *envp[],
99
                ExecDirFlags flags) {
100

101
        _cleanup_hashmap_free_ Hashmap *pids = NULL;
1,426✔
102
        bool parallel_execution;
1,426✔
103
        int r;
1,426✔
104

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

112
        assert(!strv_isempty(paths));
1,426✔
113

114
        parallel_execution = FLAGS_SET(flags, EXEC_DIR_PARALLEL) && !callbacks;
1,426✔
115

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

119
        if (timeout != USEC_INFINITY)
1,426✔
120
                alarm(DIV_ROUND_UP(timeout, USEC_PER_SEC));
1,426✔
121

122
        STRV_FOREACH(e, envp)
8,672✔
123
                if (putenv(*e) != 0)
7,246✔
124
                        return log_error_errno(errno, "Failed to set environment variable: %m");
×
125

126
        STRV_FOREACH(path, paths) {
2,865✔
127
                _cleanup_free_ char *t = NULL;
1,440✔
128
                _cleanup_close_ int fd = -EBADF;
1,440✔
129
                pid_t pid;
1,440✔
130

131
                t = path_join(root, *path);
1,440✔
132
                if (!t)
1,440✔
133
                        return log_oom();
×
134

135
                if (callbacks) {
1,440✔
136
                        _cleanup_free_ char *bn = NULL;
1,246✔
137

138
                        r = path_extract_filename(*path, &bn);
1,246✔
139
                        if (r < 0)
1,246✔
140
                                return log_error_errno(r, "Failed to extract filename from path '%s': %m", *path);
×
141

142
                        fd = open_serialization_fd(bn);
1,246✔
143
                        if (fd < 0)
1,246✔
144
                                return log_error_errno(fd, "Failed to open serialization file: %m");
×
145
                }
146

147
                if (DEBUG_LOGGING) {
1,440✔
148
                        _cleanup_free_ char *s = NULL;
1,013✔
149

150
                        char **args = strv_skip(argv, 1);
1,013✔
151
                        if (args)
1,013✔
152
                                s = quote_command_line(args, SHELL_ESCAPE_EMPTY);
×
153

154
                        log_debug("About to execute %s%s%s", t, args ? " " : "", args ? strnull(s) : "");
1,013✔
155
                }
156

157
                if (FLAGS_SET(flags, EXEC_DIR_WARN_WORLD_WRITABLE)) {
1,440✔
158
                        struct stat st;
178✔
159

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

169
                r = do_spawn(t, argv, fd, FLAGS_SET(flags, EXEC_DIR_SET_SYSTEMD_EXEC_PID), &pid);
1,440✔
170
                if (r <= 0)
1,440✔
171
                        continue;
×
172

173
                if (parallel_execution) {
1,440✔
174
                        r = hashmap_ensure_put(&pids, &trivial_hash_ops_value_free, PID_TO_PTR(pid), t);
192✔
175
                        if (r < 0)
192✔
176
                                return log_oom();
×
177
                        t = NULL;
178
                } else {
179
                        bool skip_remaining = false;
1,248✔
180

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

196
                        if (callbacks) {
1,247✔
197
                                r = finish_serialization_fd(fd);
1,246✔
198
                                if (r < 0)
1,246✔
199
                                        return log_error_errno(r, "Failed to finish serialization fd: %m");
×
200

201
                                r = callbacks[STDOUT_GENERATE](TAKE_FD(fd), callback_args[STDOUT_GENERATE]);
1,246✔
202
                                if (r < 0)
1,246✔
203
                                        return log_error_errno(r, "Failed to process output from %s: %m", *path);
×
204
                        }
205

206
                        if (skip_remaining)
1,247✔
207
                                break;
208
                }
209
        }
210

211
        if (callbacks) {
1,425✔
212
                r = callbacks[STDOUT_COLLECT](output_fd, callback_args[STDOUT_COLLECT]);
1,235✔
213
                if (r < 0)
1,235✔
214
                        return log_error_errno(r, "Callback two failed: %m");
×
215
        }
216

217
        while (!hashmap_isempty(pids)) {
1,617✔
218
                _cleanup_free_ char *t = NULL;
192✔
219
                pid_t pid;
192✔
220
                void *p;
192✔
221

222
                t = ASSERT_PTR(hashmap_steal_first_key_and_value(pids, &p));
192✔
223
                pid = PTR_TO_PID(p);
192✔
224
                assert(pid > 0);
192✔
225

226
                r = wait_for_terminate_and_check(t, pid, WAIT_LOG);
192✔
227
                if (r < 0)
192✔
228
                        return r;
229
                if (!FLAGS_SET(flags, EXEC_DIR_IGNORE_ERRORS) && r > 0)
192✔
230
                        return r;
231
        }
232

233
        return 0;
234
}
235

236
int execute_strv(
888✔
237
                const char *name,
238
                char * const *paths,
239
                const char *root,
240
                usec_t timeout,
241
                gather_stdout_callback_t const callbacks[_STDOUT_CONSUME_MAX],
242
                void * const callback_args[_STDOUT_CONSUME_MAX],
243
                char *argv[],
244
                char *envp[],
245
                ExecDirFlags flags) {
246

247
        _cleanup_close_ int fd = -EBADF;
888✔
248
        pid_t executor_pid;
888✔
249
        int r;
888✔
250

251
        assert(!FLAGS_SET(flags, EXEC_DIR_PARALLEL | EXEC_DIR_SKIP_REMAINING));
888✔
252

253
        if (strv_isempty(paths))
1,776✔
254
                return 0;
255

256
        if (callbacks) {
888✔
257
                assert(name);
708✔
258
                assert(callbacks[STDOUT_GENERATE]);
708✔
259
                assert(callbacks[STDOUT_COLLECT]);
708✔
260
                assert(callbacks[STDOUT_CONSUME]);
708✔
261
                assert(callback_args);
708✔
262

263
                fd = open_serialization_fd(name);
708✔
264
                if (fd < 0)
708✔
265
                        return log_error_errno(fd, "Failed to open serialization file: %m");
×
266
        }
267

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

272
        r = safe_fork("(sd-exec-strv)", FORK_RESET_SIGNALS|FORK_DEATHSIG_SIGTERM|FORK_LOG, &executor_pid);
888✔
273
        if (r < 0)
2,314✔
274
                return r;
275
        if (r == 0) {
2,314✔
276
                r = do_execute(paths, root, timeout, callbacks, callback_args, fd, argv, envp, flags);
1,426✔
277
                _exit(r < 0 ? EXIT_FAILURE : r);
1,426✔
278
        }
279

280
        r = wait_for_terminate_and_check("(sd-exec-strv)", executor_pid, 0);
888✔
281
        if (r < 0)
888✔
282
                return r;
283
        if (!FLAGS_SET(flags, EXEC_DIR_IGNORE_ERRORS) && r > 0)
888✔
284
                return r;
285

286
        if (!callbacks)
887✔
287
                return 0;
288

289
        r = finish_serialization_fd(fd);
708✔
290
        if (r < 0)
708✔
291
                return log_error_errno(r, "Failed to finish serialization fd: %m");
×
292

293
        r = callbacks[STDOUT_CONSUME](TAKE_FD(fd), callback_args[STDOUT_CONSUME]);
708✔
294
        if (r < 0)
708✔
295
                return log_error_errno(r, "Failed to parse returned data: %m");
×
296

297
        return 0;
298
}
299

300
int execute_directories(
888✔
301
                const char * const *directories,
302
                usec_t timeout,
303
                gather_stdout_callback_t const callbacks[_STDOUT_CONSUME_MAX],
304
                void * const callback_args[_STDOUT_CONSUME_MAX],
305
                char *argv[],
306
                char *envp[],
307
                ExecDirFlags flags) {
308

309
        _cleanup_strv_free_ char **paths = NULL;
×
310
        _cleanup_free_ char *name = NULL;
888✔
311
        int r;
888✔
312

313
        assert(!strv_isempty((char* const*) directories));
888✔
314

315
        r = conf_files_list_strv(&paths, NULL, NULL, CONF_FILES_EXECUTABLE|CONF_FILES_REGULAR|CONF_FILES_FILTER_MASKED, directories);
888✔
316
        if (r < 0)
888✔
317
                return log_error_errno(r, "Failed to enumerate executables: %m");
×
318

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

324
        if (callbacks) {
888✔
325
                r = path_extract_filename(directories[0], &name);
708✔
326
                if (r < 0)
708✔
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);
888✔
331
}
332

333
static int gather_environment_generate(int fd, void *arg) {
1,236✔
334
        char ***env = ASSERT_PTR(arg);
1,236✔
335
        _cleanup_fclose_ FILE *f = NULL;
1,236✔
336
        _cleanup_strv_free_ char **new = NULL;
1,236✔
337
        int r;
1,236✔
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,236✔
346

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

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

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

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

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

371
        return 0;
372
}
373

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

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

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

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

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

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

397
        return 0;
398
}
399

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

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

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

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

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

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

425
                v = startswith(line, "env=");
541✔
426
                if (!v) {
541✔
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✔
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) {
323✔
446
        ExecCommandFlags flags = 0;
323✔
447

448
        assert(ret);
323✔
449

450
        STRV_FOREACH(opt, ex_opts) {
361✔
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;
322✔
459

460
        return 0;
322✔
461
}
462

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

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

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

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

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

482
        return 0;
1,433✔
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) {
253✔
496
        for (size_t idx = 0; idx < ELEMENTSOF(exec_command_strings); idx++)
858✔
497
                if (i == (1 << idx))
857✔
498
                        return exec_command_strings[idx];
252✔
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,557✔
517
        /* Refuse invalid fds, regardless if fexecve() use is enabled or not */
518
        if (executable_fd < 0)
21,557✔
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,114✔
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,557✔
545
        return -errno;
21,557✔
546
}
547

548
int shall_fork_agent(void) {
3,052✔
549
        int r;
3,052✔
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,052✔
554
        if (r == -ENXIO)
3,052✔
555
                return false;
556
        if (r < 0)
×
557
                return r;
558

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

562
        return true;
563
}
564

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

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

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

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);
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),
×
586
                stdout_is_tty = isatty_safe(STDOUT_FILENO),
×
587
                stderr_is_tty = isatty_safe(STDERR_FILENO);
×
588

589
        if (!stdin_is_tty || !stdout_is_tty || !stderr_is_tty) {
×
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. */
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 /dev/tty: %m");
×
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) {
×
613
                        log_error_errno(errno, "Failed to dup2 /dev/tty to STDERR: %m");
×
614
                        _exit(EXIT_FAILURE);
×
615
                }
616

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

620
        /* Count arguments */
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. */
625
        log_full_errno(errno == ENOENT ? LOG_DEBUG : LOG_ERR, errno,
×
626
                       "Failed to execute %s: %m", argv[0]);
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