• 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

63.54
/src/notify/notify.c
1
/* SPDX-License-Identifier: LGPL-2.1-or-later */
2

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

8
#include "sd-daemon.h"
9
#include "sd-event.h"
10

11
#include "alloc-util.h"
12
#include "build.h"
13
#include "env-util.h"
14
#include "escape.h"
15
#include "event-util.h"
16
#include "exit-status.h"
17
#include "fd-util.h"
18
#include "fdset.h"
19
#include "format-util.h"
20
#include "log.h"
21
#include "main-func.h"
22
#include "notify-recv.h"
23
#include "parse-util.h"
24
#include "pidref.h"
25
#include "pretty-print.h"
26
#include "process-util.h"
27
#include "signal-util.h"
28
#include "string-util.h"
29
#include "strv.h"
30
#include "time-util.h"
31
#include "user-util.h"
32

33
static enum {
34
        ACTION_NOTIFY,
35
        ACTION_BOOTED,
36
        ACTION_FORK,
37
} arg_action = ACTION_NOTIFY;
38
static bool arg_ready = false;
39
static bool arg_reloading = false;
40
static bool arg_stopping = false;
41
static PidRef arg_pid = PIDREF_NULL;
42
static const char *arg_status = NULL;
43
static uid_t arg_uid = UID_INVALID;
44
static gid_t arg_gid = GID_INVALID;
45
static bool arg_no_block = false;
46
static char **arg_env = NULL;
47
static char **arg_exec = NULL;
48
static FDSet *arg_fds = NULL;
49
static char *arg_fdname = NULL;
50
static bool arg_quiet = false;
51

52
STATIC_DESTRUCTOR_REGISTER(arg_pid, pidref_done);
117✔
53
STATIC_DESTRUCTOR_REGISTER(arg_env, strv_freep);
117✔
54
STATIC_DESTRUCTOR_REGISTER(arg_exec, strv_freep);
117✔
55
STATIC_DESTRUCTOR_REGISTER(arg_fds, fdset_freep);
117✔
56
STATIC_DESTRUCTOR_REGISTER(arg_fdname, freep);
117✔
57

58
static int help(void) {
×
59
        _cleanup_free_ char *link = NULL;
×
60
        int r;
×
61

62
        r = terminal_urlify_man("systemd-notify", "1", &link);
×
63
        if (r < 0)
×
64
                return log_oom();
×
65

66
        printf("%s [OPTIONS...] [VARIABLE=VALUE...]\n"
×
67
               "%s [OPTIONS...] --exec [VARIABLE=VALUE...] ; -- CMDLINE...\n"
68
               "%s [OPTIONS...] --fork -- CMDLINE...\n"
69
               "\n%sNotify the init system about service status updates.%s\n\n"
70
               "  -h --help            Show this help\n"
71
               "     --version         Show package version\n"
72
               "     --ready           Inform the service manager about service start-up/reload\n"
73
               "                       completion\n"
74
               "     --reloading       Inform the service manager about configuration reloading\n"
75
               "     --stopping        Inform the service manager about service shutdown\n"
76
               "     --pid[=PID]       Set main PID of daemon\n"
77
               "     --uid=USER        Set user to send from\n"
78
               "     --status=TEXT     Set status text\n"
79
               "     --booted          Check if the system was booted up with systemd\n"
80
               "     --no-block        Do not wait until operation finished\n"
81
               "     --exec            Execute command line separated by ';' once done\n"
82
               "     --fd=FD           Pass specified file descriptor with along with message\n"
83
               "     --fdname=NAME     Name to assign to passed file descriptor(s)\n"
84
               "     --fork            Receive notifications from child rather than sending them\n"
85
               "  -q --quiet           Do not show PID of child when forking\n"
86
               "\nSee the %s for details.\n",
87
               program_invocation_short_name,
88
               program_invocation_short_name,
89
               program_invocation_short_name,
90
               ansi_highlight(),
91
               ansi_normal(),
92
               link);
93

94
        return 0;
95
}
96

97
static int get_manager_pid(PidRef *ret) {
111✔
98
        int r;
111✔
99

100
        assert(ret);
111✔
101

102
        /* If we run as a service managed by systemd --user the $MANAGERPID environment variable points to
103
         * the service manager's PID. */
104
        const char *e = getenv("MANAGERPID");
111✔
105
        if (!e) {
111✔
106
                *ret = PIDREF_NULL;
111✔
107
                return 0;
111✔
108
        }
109

110
        _cleanup_(pidref_done) PidRef manager = PIDREF_NULL;
×
111
        r = pidref_set_pidstr(&manager, e);
×
112
        if (r < 0)
×
113
                return log_warning_errno(r, "$MANAGERPID is set to an invalid PID, ignoring: %s", e);
×
114

115
        e = getenv("MANAGERPIDFDID");
×
116
        if (e) {
×
117
                uint64_t manager_pidfd_id;
×
118

119
                r = safe_atou64(e, &manager_pidfd_id);
×
120
                if (r < 0)
×
121
                        return log_warning_errno(r, "$MANAGERPIDFDID is not set to a valid inode number, ignoring: %s", e);
×
122

123
                r = pidref_acquire_pidfd_id(&manager);
×
124
                if (r < 0)
×
125
                        return log_warning_errno(r, "Unable to acquire pidfd ID for manager: %m");
×
126

127
                if (manager_pidfd_id != manager.fd_id) {
×
128
                        log_debug("$MANAGERPIDFDID doesn't match process currently referenced by $MANAGERPID, suppressing.");
×
129
                        *ret = PIDREF_NULL;
×
130
                        return 0;
×
131
                }
132
        }
133

134
        *ret = TAKE_PIDREF(manager);
×
135
        return 1;
×
136
}
137

138
static int pidref_parent_if_applicable(PidRef *ret) {
113✔
139
        _cleanup_(pidref_done) PidRef pidref = PIDREF_NULL, manager = PIDREF_NULL;
113✔
140
        int r;
113✔
141

142
        assert(ret);
113✔
143

144
        r = pidref_set_parent(&pidref);
113✔
145
        if (r < 0)
113✔
146
                return log_debug_errno(r, "Failed to create reference to our parent process: %m");
×
147

148
        /* Don't send from PID 1 or the service manager's PID (which might be distinct from 1, if we are a
149
         * --user service). That'd just be confusing for the service manager. */
150
        if (pidref.pid == 1)
113✔
151
                goto from_self;
2✔
152

153
        r = get_manager_pid(&manager);
111✔
154
        if (r > 0 && pidref_equal(&pidref, &manager))
111✔
155
                goto from_self;
×
156

157
        *ret = TAKE_PIDREF(pidref);
111✔
158
        return 0;
111✔
159

160
from_self:
2✔
161
        return pidref_set_self(ret);
113✔
162
}
163

164
static int parse_argv(int argc, char *argv[]) {
118✔
165

166
        enum {
118✔
167
                ARG_READY = 0x100,
168
                ARG_RELOADING,
169
                ARG_STOPPING,
170
                ARG_VERSION,
171
                ARG_PID,
172
                ARG_STATUS,
173
                ARG_BOOTED,
174
                ARG_UID,
175
                ARG_NO_BLOCK,
176
                ARG_EXEC,
177
                ARG_FD,
178
                ARG_FDNAME,
179
                ARG_FORK,
180
        };
181

182
        static const struct option options[] = {
118✔
183
                { "help",      no_argument,       NULL, 'h'           },
184
                { "version",   no_argument,       NULL, ARG_VERSION   },
185
                { "ready",     no_argument,       NULL, ARG_READY     },
186
                { "reloading", no_argument,       NULL, ARG_RELOADING },
187
                { "stopping",  no_argument,       NULL, ARG_STOPPING  },
188
                { "pid",       optional_argument, NULL, ARG_PID       },
189
                { "status",    required_argument, NULL, ARG_STATUS    },
190
                { "booted",    no_argument,       NULL, ARG_BOOTED    },
191
                { "uid",       required_argument, NULL, ARG_UID       },
192
                { "no-block",  no_argument,       NULL, ARG_NO_BLOCK  },
193
                { "exec",      no_argument,       NULL, ARG_EXEC      },
194
                { "fd",        required_argument, NULL, ARG_FD        },
195
                { "fdname",    required_argument, NULL, ARG_FDNAME    },
196
                { "fork",      no_argument,       NULL, ARG_FORK      },
197
                { "quiet",     no_argument,       NULL, 'q'           },
198
                {}
199
        };
200

201
        _cleanup_fdset_free_ FDSet *passed = NULL;
×
202
        bool do_exec = false;
118✔
203
        int c, r;
118✔
204

205
        assert(argc >= 0);
118✔
206
        assert(argv);
118✔
207

208
        while ((c = getopt_long(argc, argv, "hq", options, NULL)) >= 0) {
204✔
209

210
                switch (c) {
86✔
211

212
                case 'h':
×
213
                        return help();
×
214

215
                case ARG_VERSION:
×
216
                        return version();
×
217

218
                case ARG_READY:
46✔
219
                        arg_ready = true;
46✔
220
                        break;
46✔
221

222
                case ARG_RELOADING:
1✔
223
                        arg_reloading = true;
1✔
224
                        break;
1✔
225

226
                case ARG_STOPPING:
1✔
227
                        arg_stopping = true;
1✔
228
                        break;
1✔
229

230
                case ARG_PID:
4✔
231
                        pidref_done(&arg_pid);
4✔
232

233
                        if (isempty(optarg) || streq(optarg, "auto"))
8✔
234
                                r = pidref_parent_if_applicable(&arg_pid);
×
235
                        else if (streq(optarg, "parent"))
4✔
236
                                r = pidref_set_parent(&arg_pid);
2✔
237
                        else if (streq(optarg, "self"))
2✔
238
                                r = pidref_set_self(&arg_pid);
1✔
239
                        else
240
                                r = pidref_set_pidstr(&arg_pid, optarg);
1✔
241
                        if (r < 0)
4✔
242
                                return log_error_errno(r, "Failed to refer to --pid='%s': %m", optarg);
×
243

244
                        break;
245

246
                case ARG_STATUS:
23✔
247
                        arg_status = optarg;
23✔
248
                        break;
23✔
249

250
                case ARG_BOOTED:
×
251
                        arg_action = ACTION_BOOTED;
×
252
                        break;
×
253

254
                case ARG_UID: {
3✔
255
                        const char *u = optarg;
3✔
256

257
                        r = get_user_creds(&u, &arg_uid, &arg_gid, NULL, NULL, 0);
3✔
258
                        if (r == -ESRCH) /* If the user doesn't exist, then accept it anyway as numeric */
3✔
259
                                r = parse_uid(u, &arg_uid);
3✔
260
                        if (r < 0)
3✔
261
                                return log_error_errno(r, "Can't resolve user %s: %m", optarg);
×
262

263
                        break;
3✔
264
                }
265

266
                case ARG_NO_BLOCK:
×
267
                        arg_no_block = true;
×
268
                        break;
×
269

270
                case ARG_EXEC:
271
                        do_exec = true;
272
                        break;
273

274
                case ARG_FD: {
3✔
275
                        _cleanup_close_ int owned_fd = -EBADF;
322✔
276
                        int fdnr;
3✔
277

278
                        fdnr = parse_fd(optarg);
3✔
279
                        if (fdnr < 0)
3✔
280
                                return log_error_errno(fdnr, "Failed to parse file descriptor: %s", optarg);
×
281

282
                        if (!passed) {
3✔
283
                                /* Take possession of all passed fds */
284
                                r = fdset_new_fill(/* filter_cloexec= */ 0, &passed);
3✔
285
                                if (r < 0)
3✔
286
                                        return log_error_errno(r, "Failed to take possession of passed file descriptors: %m");
×
287
                        }
288

289
                        if (fdnr < 3) {
3✔
290
                                /* For stdin/stdout/stderr we want to keep the fd, too, hence make a copy */
291
                                owned_fd = fcntl(fdnr, F_DUPFD_CLOEXEC, 3);
×
292
                                if (owned_fd < 0)
×
293
                                        return log_error_errno(errno, "Failed to duplicate file descriptor: %m");
×
294
                        } else {
295
                                /* Otherwise, move the fd over */
296
                                owned_fd = fdset_remove(passed, fdnr);
3✔
297
                                if (owned_fd < 0)
3✔
298
                                        return log_error_errno(owned_fd, "Specified file descriptor '%i' not passed or specified more than once: %m", fdnr);
×
299
                        }
300

301
                        if (!arg_fds) {
3✔
302
                                arg_fds = fdset_new();
3✔
303
                                if (!arg_fds)
3✔
304
                                        return log_oom();
×
305
                        }
306

307
                        r = fdset_consume(arg_fds, TAKE_FD(owned_fd));
3✔
308
                        if (r < 0)
3✔
309
                                return log_error_errno(r, "Failed to add file descriptor to set: %m");
×
310
                        break;
3✔
311
                }
312

313
                case ARG_FDNAME:
3✔
314
                        if (!fdname_is_valid(optarg))
3✔
315
                                return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "File descriptor name invalid: %s", optarg);
×
316

317
                        if (free_and_strdup(&arg_fdname, optarg) < 0)
3✔
318
                                return log_oom();
×
319

320
                        break;
321

322
                case ARG_FORK:
1✔
323
                        arg_action = ACTION_FORK;
1✔
324
                        break;
1✔
325

326
                case 'q':
×
327
                        arg_quiet = true;
×
328
                        break;
×
329

330
                case '?':
331
                        return -EINVAL;
332

333
                default:
×
334
                        assert_not_reached();
×
335
                }
336
        }
337

338
        bool have_env = arg_ready || arg_stopping || arg_reloading || arg_status || pidref_is_set(&arg_pid) || !fdset_isempty(arg_fds);
179✔
339

340
        switch (arg_action) {
118✔
341

342
        case ACTION_NOTIFY: {
117✔
343
                if (arg_fdname && fdset_isempty(arg_fds))
117✔
344
                        return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "No file descriptors passed, but --fdname= set, refusing.");
×
345

346
                size_t n_arg_env;
117✔
347

348
                if (do_exec) {
117✔
349
                        int i;
1✔
350

351
                        for (i = optind; i < argc; i++)
1✔
352
                                if (streq(argv[i], ";"))
1✔
353
                                        break;
354

355
                        if (i >= argc)
1✔
356
                                return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "If --exec is used argument list must contain ';' separator, refusing.");
×
357
                        if (i+1 == argc)
1✔
358
                                return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Empty command line specified after ';' separator, refusing.");
×
359

360
                        arg_exec = strv_copy_n(argv + i + 1, argc - i - 1);
1✔
361
                        if (!arg_exec)
1✔
362
                                return log_oom();
×
363

364
                        n_arg_env = i - optind;
1✔
365
                } else
366
                        n_arg_env = argc - optind;
116✔
367

368
                have_env = have_env || n_arg_env > 0;
117✔
369
                if (!have_env) {
117✔
370
                        if (do_exec)
×
371
                                return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "No notify message specified while --exec, refusing.");
×
372

373
                        /* No argument at all? */
374
                        help();
×
375
                        return -EINVAL;
376
                }
377

378
                if (n_arg_env > 0) {
117✔
379
                        arg_env = strv_copy_n(argv + optind, n_arg_env);
58✔
380
                        if (!arg_env)
58✔
381
                                return log_oom();
×
382
                }
383

384
                if (!fdset_isempty(passed))
117✔
385
                        log_warning("Warning: %u more file descriptors passed than referenced with --fd=.", fdset_size(passed));
×
386

387
                break;
388
        }
389

390
        case ACTION_BOOTED:
×
391
                if (argc > optind)
×
392
                        return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "--booted takes no parameters, refusing.");
×
393

394
                break;
395

396
        case ACTION_FORK:
1✔
397
                if (optind >= argc)
1✔
398
                        return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "--fork requires a command to be specified, refusing.");
×
399

400
                break;
401

402
        default:
×
403
                assert_not_reached();
×
404
        }
405

406
        if (have_env && arg_action != ACTION_NOTIFY)
118✔
407
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "--ready, --reloading, --stopping, --pid=, --status=, --fd= may not be combined with --fork or --booted, refusing.");
×
408

409
        return 1;
410
}
411

412
static int on_notify_socket(sd_event_source *s, int fd, unsigned event, void *userdata) {
1✔
413
        PidRef *child = ASSERT_PTR(userdata);
1✔
414
        int r;
1✔
415

416
        assert(s);
1✔
417
        assert(fd >= 0);
1✔
418

419
        _cleanup_free_ char *text = NULL;
1✔
420
        _cleanup_(pidref_done) PidRef pidref = PIDREF_NULL;
×
421
        r = notify_recv(fd, &text, /* ret_ucred= */ NULL, &pidref);
1✔
422
        if (r == -EAGAIN)
1✔
423
                return 0;
424
        if (r < 0)
1✔
425
                return r;
426

427
        if (!pidref_equal(child, &pidref)) {
1✔
428
                log_warning("Received notification message from unexpected process " PID_FMT " (expected " PID_FMT "), ignoring.",
×
429
                            pidref.pid, child->pid);
430
                return 0;
×
431
        }
432

433
        const char *p = find_line_startswith(text, "READY=1");
1✔
434
        if (!p || !IN_SET(*p, '\n', 0)) {
1✔
435
                if (!DEBUG_LOGGING)
×
436
                        return 0;
1✔
437

438
                _cleanup_free_ char *escaped = cescape(text);
×
439
                log_debug("Received notification message without READY=1, ignoring: %s", strna(escaped));
×
440
                return 0;
×
441
        }
442

443
        log_debug("Received READY=1, exiting.");
1✔
444
        return sd_event_exit(sd_event_source_get_event(s), EXIT_SUCCESS);
1✔
445
}
446

447
static int on_child(sd_event_source *s, const siginfo_t *si, void *userdata) {
×
448
        assert(s);
×
449
        assert(si);
×
450

451
        int ret;
×
452
        if (si->si_code == CLD_EXITED) {
×
453
                if (si->si_status != EXIT_SUCCESS)
×
454
                        log_debug("Child failed with exit status %i.", si->si_status);
×
455
                else
456
                        log_debug("Child exited successfully. (But no READY=1 message was sent!)");
×
457

458
                /* NB: we propagate success here if the child exited cleanly but never sent us READY=1. We
459
                 * are not a service manager after all, where this would be a protocol violation. We are just
460
                 * a shell tool to fork off stuff in the background, where I think it makes sense to allow
461
                 * clean early exit of forked off processes. */
462
                ret = si->si_status;
×
463

464
        } else if (IN_SET(si->si_code, CLD_KILLED, CLD_DUMPED))
×
465
                ret = log_debug_errno(SYNTHETIC_ERRNO(EPROTO),
×
466
                                      "Child terminated by signal %s.", signal_to_string(si->si_status));
467
        else
468
                ret = log_debug_errno(SYNTHETIC_ERRNO(EPROTO),
×
469
                                      "Child terminated due to unknown reason.");
470

471
        return sd_event_exit(sd_event_source_get_event(s), ret);
×
472
}
473

474
static int action_fork(char *const *_command) {
1✔
475

476
        static const int forward_signals[] = {
1✔
477
                SIGHUP,
478
                SIGTERM,
479
                SIGINT,
480
                SIGQUIT,
481
                SIGTSTP,
482
                SIGCONT,
483
                SIGUSR1,
484
                SIGUSR2,
485
        };
486

487
        int r;
1✔
488

489
        assert(!strv_isempty(_command));
1✔
490

491
        /* Make a copy, since pidref_safe_fork_full() will change argv[] further down. */
492
        _cleanup_strv_free_ char **command = strv_copy(_command);
2✔
493
        if (!command)
1✔
494
                return log_oom();
×
495

496
        _cleanup_free_ char *c = strv_join(command, " ");
2✔
497
        if (!c)
1✔
498
                return log_oom();
×
499

500
        _cleanup_(sd_event_unrefp) sd_event *event = NULL;
1✔
501
        r = sd_event_new(&event);
1✔
502
        if (r < 0)
1✔
503
                return log_error_errno(r, "Failed to allocate event loop: %m");
×
504

505
        _cleanup_(pidref_done) PidRef child = PIDREF_NULL;
×
506
        _cleanup_free_ char *addr_string = NULL;
1✔
507
        r = notify_socket_prepare(
1✔
508
                        event,
509
                        SD_EVENT_PRIORITY_NORMAL - 10, /* If we receive both the sd_notify() message and a
510
                                                        * SIGCHLD always process sd_notify() first, it's the
511
                                                        * more interesting, "positive" information. */
512
                        on_notify_socket,
513
                        &child,
514
                        &addr_string,
515
                        /* ret_event_source= */ NULL);
516
        if (r < 0)
1✔
517
                return log_error_errno(r, "Failed to prepare notify socket: %m");
×
518

519
        r = pidref_safe_fork_full(
3✔
520
                        "(notify)",
521
                        /* stdio_fds= */ (const int[]) { -EBADF, -EBADF, STDERR_FILENO },
1✔
522
                        /* except_fds= */ NULL,
523
                        /* n_except_fds= */ 0,
524
                        /* flags= */ FORK_REARRANGE_STDIO,
525
                        &child);
526
        if (r < 0)
2✔
527
                return log_error_errno(r, "Failed to fork child in order to execute '%s': %m", c);
×
528
        if (r == 0) {
2✔
529
                if (setenv("NOTIFY_SOCKET", addr_string, /* overwrite= */ true) < 0) {
1✔
530
                        log_error_errno(errno, "Failed to set $NOTIFY_SOCKET: %m");
×
531
                        _exit(EXIT_MEMORY);
×
532
                }
533

534
                log_debug("Executing: %s", c);
1✔
535
                execvp(command[0], command);
×
536
                log_error_errno(errno, "Failed to execute '%s': %m", c);
×
537
                _exit(EXIT_EXEC);
×
538
        }
539

540
        if (!arg_quiet) {
1✔
541
                printf(PID_FMT "\n", child.pid);
1✔
542
                fflush(stdout);
1✔
543
        }
544

545
        BLOCK_SIGNALS(SIGCHLD);
1✔
546

547
        _cleanup_(sd_event_source_disable_unrefp) sd_event_source *child_event_source = NULL;
1✔
548
        r = event_add_child_pidref(event, &child_event_source, &child, WEXITED, on_child, /* userdata= */ NULL);
1✔
549
        if (r < 0)
1✔
550
                return log_error_errno(r, "Failed to allocate child source: %m");
×
551

552
        /* Handle SIGCHLD before propagating the other signals below */
553
        r = sd_event_source_set_priority(child_event_source, SD_EVENT_PRIORITY_NORMAL - 5);
1✔
554
        if (r < 0)
1✔
555
                return log_error_errno(r, "Failed to change child event source priority: %m");
×
556

557
        sd_event_source **forward_signal_sources = NULL;
1✔
558
        size_t n_forward_signal_sources = 0;
1✔
559
        CLEANUP_ARRAY(forward_signal_sources, n_forward_signal_sources, event_source_unref_many);
1✔
560

561
        r = event_forward_signals(
1✔
562
                        event,
563
                        child_event_source,
564
                        forward_signals, ELEMENTSOF(forward_signals),
565
                        &forward_signal_sources, &n_forward_signal_sources);
566
        if (r < 0)
1✔
567
                return log_error_errno(r, "Failed to set up signal forwarding: %m");
×
568

569
        r = sd_event_loop(event);
1✔
570
        if (r < 0)
1✔
571
                return log_error_errno(r, "Failed to run event loop: %m");
×
572

573
        return r;
574
}
575

576
static int run(int argc, char* argv[]) {
118✔
577
        _cleanup_free_ char *status = NULL, *main_pid = NULL, *main_pidfd_id = NULL, *msg = NULL,
×
578
                       *monotonic_usec = NULL, *fdn = NULL;
117✔
579
        _cleanup_strv_free_ char **final_env = NULL;
117✔
580
        const char *our_env[10];
118✔
581
        size_t i = 0;
118✔
582
        int r;
118✔
583

584
        log_setup();
118✔
585

586
        r = parse_argv(argc, argv);
118✔
587
        if (r <= 0)
118✔
588
                return r;
589

590
        if (arg_action == ACTION_FORK)
118✔
591
                return action_fork(argv + optind);
1✔
592

593
        if (arg_action == ACTION_BOOTED) {
117✔
594
                r = sd_booted();
×
595
                if (r < 0)
×
596
                        log_debug_errno(r, "Failed to determine whether we are booted with systemd, assuming we aren't: %m");
×
597
                else
598
                        log_debug("The system %s booted with systemd.", r ? "was" : "was not");
×
599

600
                return r <= 0;
×
601
        }
602

603
        if (arg_reloading) {
117✔
604
                our_env[i++] = "RELOADING=1";
1✔
605

606
                if (asprintf(&monotonic_usec, "MONOTONIC_USEC=" USEC_FMT, now(CLOCK_MONOTONIC)) < 0)
1✔
607
                        return log_oom();
×
608

609
                our_env[i++] = monotonic_usec;
1✔
610
        }
611

612
        if (arg_ready)
117✔
613
                our_env[i++] = "READY=1";
46✔
614

615
        if (arg_stopping)
117✔
616
                our_env[i++] = "STOPPING=1";
1✔
617

618
        if (arg_status) {
117✔
619
                status = strjoin("STATUS=", arg_status);
23✔
620
                if (!status)
23✔
621
                        return log_oom();
×
622

623
                our_env[i++] = status;
23✔
624
        }
625

626
        if (pidref_is_set(&arg_pid)) {
117✔
627
                if (asprintf(&main_pid, "MAINPID="PID_FMT, arg_pid.pid) < 0)
4✔
628
                        return log_oom();
×
629

630
                our_env[i++] = main_pid;
4✔
631

632
                r = pidref_acquire_pidfd_id(&arg_pid);
4✔
633
                if (r < 0)
4✔
634
                        log_debug_errno(r, "Unable to acquire pidfd id of new main pid " PID_FMT ", ignoring: %m",
×
635
                                        arg_pid.pid);
636
                else {
637
                        if (asprintf(&main_pidfd_id, "MAINPIDFDID=%" PRIu64, arg_pid.fd_id) < 0)
4✔
638
                                return log_oom();
×
639

640
                        our_env[i++] = main_pidfd_id;
4✔
641
                }
642
        }
643

644
        if (!fdset_isempty(arg_fds)) {
117✔
645
                our_env[i++] = "FDSTORE=1";
3✔
646

647
                if (arg_fdname) {
3✔
648
                        fdn = strjoin("FDNAME=", arg_fdname);
3✔
649
                        if (!fdn)
3✔
650
                                return log_oom();
×
651

652
                        our_env[i++] = fdn;
3✔
653
                }
654
        }
655

656
        our_env[i++] = NULL;
117✔
657

658
        final_env = strv_env_merge((char**) our_env, arg_env);
117✔
659
        if (!final_env)
117✔
660
                return log_oom();
×
661
        assert(!strv_isempty(final_env));
117✔
662

663
        msg = strv_join(final_env, "\n");
117✔
664
        if (!msg)
117✔
665
                return log_oom();
×
666

667
        /* If this is requested change to the requested UID/GID. Note that we only change the real UID here, and leave
668
           the effective UID in effect (which is 0 for this to work). That's because we want the privileges to fake the
669
           ucred data, and sd_pid_notify() uses the real UID for filling in ucred. */
670

671
        if (arg_gid != GID_INVALID &&
117✔
672
            setregid(arg_gid, GID_INVALID) < 0)
×
673
                return log_error_errno(errno, "Failed to change GID: %m");
×
674

675
        if (arg_uid != UID_INVALID &&
120✔
676
            setreuid(arg_uid, UID_INVALID) < 0)
3✔
677
                return log_error_errno(errno, "Failed to change UID: %m");
×
678

679
        /* If --pid= is explicitly specified, use it as source pid. Otherwise, pretend the message originates
680
         * from our parent, i.e. --pid=auto */
681
        if (!pidref_is_set(&arg_pid))
117✔
682
                (void) pidref_parent_if_applicable(&arg_pid);
113✔
683

684
        if (fdset_isempty(arg_fds))
117✔
685
                r = sd_pid_notify(arg_pid.pid, /* unset_environment= */ false, msg);
114✔
686
        else {
687
                _cleanup_free_ int *a = NULL;
3✔
688
                int k;
3✔
689

690
                k = fdset_to_array(arg_fds, &a);
3✔
691
                if (k < 0)
3✔
692
                        return log_error_errno(k, "Failed to convert file descriptor set to array: %m");
×
693

694
                r = sd_pid_notify_with_fds(arg_pid.pid, /* unset_environment= */ false, msg, a, k);
3✔
695

696
        }
697
        if (r == -E2BIG)
117✔
698
                return log_error_errno(r, "Too many file descriptors passed.");
×
699
        if (r < 0)
117✔
700
                return log_error_errno(r, "Failed to notify init system: %m");
×
701
        if (r == 0)
117✔
702
                return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP),
×
703
                                       "No status data could be sent: $NOTIFY_SOCKET was not set");
704

705
        arg_fds = fdset_free(arg_fds); /* Close before we execute anything */
117✔
706

707
        if (!arg_no_block) {
117✔
708
                r = sd_pid_notify_barrier(arg_pid.pid, /* unset_environment= */ false, 5 * USEC_PER_SEC);
117✔
709
                if (r < 0)
117✔
710
                        return log_error_errno(r, "Failed to invoke barrier: %m");
×
711
                if (r == 0)
117✔
712
                        return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP),
×
713
                                               "No status data could be sent: $NOTIFY_SOCKET was not set");
714
        }
715

716
        if (arg_exec) {
117✔
717
                execvp(arg_exec[0], arg_exec);
1✔
718

719
                _cleanup_free_ char *cmdline = strv_join(arg_exec, " ");
1✔
720
                return log_error_errno(errno, "Failed to execute command line: %s", strnull(cmdline));
×
721
        }
722

723
        /* The DEFINE_MAIN_FUNCTION_WITH_POSITIVE_FAILURE() boilerplate will send the exit status via
724
         * sd_notify(). Which is normally fine, but very confusing in systemd-notify, whose purpose is to
725
         * send user-controllable notification messages, and not implicit ones. Let's turn if off, by
726
         * unsetting the $NOTIFY_SOCKET environment variable. */
727
        (void) unsetenv("NOTIFY_SOCKET");
116✔
728
        return 0;
116✔
729
}
730

731
DEFINE_MAIN_FUNCTION_WITH_POSITIVE_FAILURE(run);
118✔
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