• 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

80.7
/src/core/manager.c
1
/* SPDX-License-Identifier: LGPL-2.1-or-later */
2

3
#include <errno.h>
4
#include <fcntl.h>
5
#include <linux/kd.h>
6
#include <sys/epoll.h>
7
#include <sys/inotify.h>
8
#include <sys/ioctl.h>
9
#include <sys/mount.h>
10
#include <sys/reboot.h>
11
#include <sys/timerfd.h>
12
#include <sys/wait.h>
13
#include <unistd.h>
14

15
#if HAVE_AUDIT
16
#include <libaudit.h>
17
#endif
18

19
#include "sd-daemon.h"
20
#include "sd-messages.h"
21
#include "sd-path.h"
22

23
#include "all-units.h"
24
#include "alloc-util.h"
25
#include "audit-fd.h"
26
#include "boot-timestamps.h"
27
#include "build-path.h"
28
#include "bus-common-errors.h"
29
#include "bus-error.h"
30
#include "bus-kernel.h"
31
#include "bus-util.h"
32
#include "clean-ipc.h"
33
#include "clock-util.h"
34
#include "common-signal.h"
35
#include "confidential-virt.h"
36
#include "constants.h"
37
#include "creds-util.h"
38
#include "daemon-util.h"
39
#include "dbus.h"
40
#include "dbus-job.h"
41
#include "dbus-manager.h"
42
#include "dbus-unit.h"
43
#include "dirent-util.h"
44
#include "dynamic-user.h"
45
#include "env-util.h"
46
#include "escape.h"
47
#include "event-util.h"
48
#include "exec-util.h"
49
#include "execute.h"
50
#include "exit-status.h"
51
#include "fd-util.h"
52
#include "fileio.h"
53
#include "generator-setup.h"
54
#include "hashmap.h"
55
#include "initrd-util.h"
56
#include "inotify-util.h"
57
#include "install.h"
58
#include "io-util.h"
59
#include "iovec-util.h"
60
#include "label-util.h"
61
#include "load-fragment.h"
62
#include "locale-setup.h"
63
#include "log.h"
64
#include "macro.h"
65
#include "manager.h"
66
#include "manager-dump.h"
67
#include "manager-serialize.h"
68
#include "memory-util.h"
69
#include "mkdir-label.h"
70
#include "mount-util.h"
71
#include "notify-recv.h"
72
#include "os-util.h"
73
#include "parse-util.h"
74
#include "path-lookup.h"
75
#include "path-util.h"
76
#include "plymouth-util.h"
77
#include "pretty-print.h"
78
#include "process-util.h"
79
#include "psi-util.h"
80
#include "ratelimit.h"
81
#include "rlimit-util.h"
82
#include "rm-rf.h"
83
#include "selinux-util.h"
84
#include "serialize.h"
85
#include "signal-util.h"
86
#include "socket-util.h"
87
#include "special.h"
88
#include "stat-util.h"
89
#include "string-table.h"
90
#include "string-util.h"
91
#include "strv.h"
92
#include "strxcpyx.h"
93
#include "sysctl-util.h"
94
#include "syslog-util.h"
95
#include "taint.h"
96
#include "terminal-util.h"
97
#include "time-util.h"
98
#include "transaction.h"
99
#include "uid-range.h"
100
#include "umask-util.h"
101
#include "unit-name.h"
102
#include "user-util.h"
103
#include "varlink.h"
104
#include "virt.h"
105
#include "watchdog.h"
106

107
/* Make sure clients notifying us don't block */
108
#define MANAGER_SOCKET_RCVBUF_SIZE (8*U64_MB)
109

110
/* Initial delay and the interval for printing status messages about running jobs */
111
#define JOBS_IN_PROGRESS_WAIT_USEC (2*USEC_PER_SEC)
112
#define JOBS_IN_PROGRESS_QUIET_WAIT_USEC (25*USEC_PER_SEC)
113
#define JOBS_IN_PROGRESS_PERIOD_USEC (USEC_PER_SEC / 3)
114
#define JOBS_IN_PROGRESS_PERIOD_DIVISOR 3
115

116
/* If there are more than 1K bus messages queue across our API and direct buses, then let's not add more on top until
117
 * the queue gets more empty. */
118
#define MANAGER_BUS_BUSY_THRESHOLD 1024LU
119

120
/* How many units and jobs to process of the bus queue before returning to the event loop. */
121
#define MANAGER_BUS_MESSAGE_BUDGET 100U
122

123
#define DEFAULT_TASKS_MAX ((CGroupTasksMax) { 15U, 100U }) /* 15% */
124

125
static int manager_dispatch_notify_fd(sd_event_source *source, int fd, uint32_t revents, void *userdata);
126
static int manager_dispatch_signal_fd(sd_event_source *source, int fd, uint32_t revents, void *userdata);
127
static int manager_dispatch_time_change_fd(sd_event_source *source, int fd, uint32_t revents, void *userdata);
128
static int manager_dispatch_idle_pipe_fd(sd_event_source *source, int fd, uint32_t revents, void *userdata);
129
static int manager_dispatch_user_lookup_fd(sd_event_source *source, int fd, uint32_t revents, void *userdata);
130
static int manager_dispatch_handoff_timestamp_fd(sd_event_source *source, int fd, uint32_t revents, void *userdata);
131
static int manager_dispatch_pidref_transport_fd(sd_event_source *source, int fd, uint32_t revents, void *userdata);
132
static int manager_dispatch_jobs_in_progress(sd_event_source *source, usec_t usec, void *userdata);
133
static int manager_dispatch_run_queue(sd_event_source *source, void *userdata);
134
static int manager_dispatch_sigchld(sd_event_source *source, void *userdata);
135
static int manager_dispatch_timezone_change(sd_event_source *source, const struct inotify_event *event, void *userdata);
136
static int manager_run_environment_generators(Manager *m);
137
static int manager_run_generators(Manager *m);
138
static void manager_vacuum(Manager *m);
139

140
static usec_t manager_watch_jobs_next_time(Manager *m) {
4,406✔
141
        usec_t timeout;
4,406✔
142

143
        if (MANAGER_IS_USER(m))
4,406✔
144
                /* Let the user manager without a timeout show status quickly, so the system manager can make
145
                 * use of it, if it wants to. */
146
                timeout = JOBS_IN_PROGRESS_WAIT_USEC * 2 / 3;
147
        else if (show_status_on(m->show_status))
3,576✔
148
                /* When status is on, just use the usual timeout. */
149
                timeout = JOBS_IN_PROGRESS_WAIT_USEC;
150
        else
151
                timeout = JOBS_IN_PROGRESS_QUIET_WAIT_USEC;
152

153
        return usec_add(now(CLOCK_MONOTONIC), timeout);
4,406✔
154
}
155

156
static bool manager_is_confirm_spawn_disabled(Manager *m) {
3,170✔
157
        assert(m);
3,170✔
158

159
        if (!m->confirm_spawn)
3,170✔
160
                return true;
161

162
        return access("/run/systemd/confirm_spawn_disabled", F_OK) >= 0;
×
163
}
164

165
static void manager_watch_jobs_in_progress(Manager *m) {
3,170✔
166
        usec_t next;
3,170✔
167
        int r;
3,170✔
168

169
        assert(m);
3,170✔
170

171
        /* We do not want to show the cylon animation if the user
172
         * needs to confirm service executions otherwise confirmation
173
         * messages will be screwed by the cylon animation. */
174
        if (!manager_is_confirm_spawn_disabled(m))
3,170✔
175
                return;
176

177
        if (m->jobs_in_progress_event_source)
3,170✔
178
                return;
179

180
        next = manager_watch_jobs_next_time(m);
762✔
181
        r = sd_event_add_time(
762✔
182
                        m->event,
183
                        &m->jobs_in_progress_event_source,
184
                        CLOCK_MONOTONIC,
185
                        next, 0,
186
                        manager_dispatch_jobs_in_progress, m);
187
        if (r < 0)
762✔
188
                return;
189

190
        (void) sd_event_source_set_description(m->jobs_in_progress_event_source, "manager-jobs-in-progress");
762✔
191
}
192

193
static void manager_flip_auto_status(Manager *m, bool enable, const char *reason) {
582✔
194
        assert(m);
582✔
195

196
        if (enable) {
582✔
197
                if (m->show_status == SHOW_STATUS_AUTO)
402✔
198
                        manager_set_show_status(m, SHOW_STATUS_TEMPORARY, reason);
×
199
        } else {
200
                if (m->show_status == SHOW_STATUS_TEMPORARY)
180✔
201
                        manager_set_show_status(m, SHOW_STATUS_AUTO, reason);
×
202
        }
203
}
582✔
204

205
static void manager_print_jobs_in_progress(Manager *m) {
402✔
206
        Job *j;
402✔
207
        unsigned counter = 0, print_nr;
402✔
208
        char cylon[6 + CYLON_BUFFER_EXTRA + 1];
402✔
209
        unsigned cylon_pos;
402✔
210
        uint64_t timeout = 0;
402✔
211

212
        assert(m);
402✔
213
        assert(m->n_running_jobs > 0);
402✔
214

215
        manager_flip_auto_status(m, true, "delay");
402✔
216

217
        print_nr = (m->jobs_in_progress_iteration / JOBS_IN_PROGRESS_PERIOD_DIVISOR) % m->n_running_jobs;
402✔
218

219
        HASHMAP_FOREACH(j, m->jobs)
404✔
220
                if (j->state == JOB_RUNNING && counter++ == print_nr)
404✔
221
                        break;
222

223
        /* m->n_running_jobs must be consistent with the contents of m->jobs,
224
         * so the above loop must have succeeded in finding j. */
225
        assert(counter == print_nr + 1);
402✔
226
        assert(j);
402✔
227

228
        cylon_pos = m->jobs_in_progress_iteration % 14;
402✔
229
        if (cylon_pos >= 8)
402✔
230
                cylon_pos = 14 - cylon_pos;
170✔
231
        draw_cylon(cylon, sizeof(cylon), 6, cylon_pos);
402✔
232

233
        m->jobs_in_progress_iteration++;
402✔
234

235
        char job_of_n[STRLEN("( of ) ") + DECIMAL_STR_MAX(unsigned)*2] = "";
402✔
236
        if (m->n_running_jobs > 1)
402✔
237
                xsprintf(job_of_n, "(%u of %u) ", counter, m->n_running_jobs);
×
238

239
        (void) job_get_timeout(j, &timeout);
402✔
240

241
        /* We want to use enough information for the user to identify previous lines talking about the same
242
         * unit, but keep the message as short as possible. So if 'Starting foo.service' or 'Starting
243
         * foo.service - Description' were used, 'foo.service' is enough here. On the other hand, if we used
244
         * 'Starting Description' before, then we shall also use 'Description' here. So we pass NULL as the
245
         * second argument to unit_status_string(). */
246
        const char *ident = unit_status_string(j->unit, NULL);
402✔
247

248
        const char *time = FORMAT_TIMESPAN(now(CLOCK_MONOTONIC) - j->begin_usec, 1*USEC_PER_SEC);
402✔
249
        const char *limit = timeout > 0 ? FORMAT_TIMESPAN(timeout - j->begin_usec, 1*USEC_PER_SEC) : "no limit";
402✔
250

251
        if (m->status_unit_format == STATUS_UNIT_FORMAT_DESCRIPTION)
402✔
252
                /* When using 'Description', we effectively don't have enough space to show the nested status
253
                 * without ellipsization, so let's not even try. */
254
                manager_status_printf(m, STATUS_TYPE_EPHEMERAL, cylon,
×
255
                                      "%sA %s job is running for %s (%s / %s)",
256
                                      job_of_n,
257
                                      job_type_to_string(j->type),
×
258
                                      ident,
259
                                      time, limit);
260
        else {
261
                const char *status_text = unit_status_text(j->unit);
402✔
262

263
                manager_status_printf(m, STATUS_TYPE_EPHEMERAL, cylon,
804✔
264
                                      "%sJob %s/%s running (%s / %s)%s%s",
265
                                      job_of_n,
266
                                      ident,
267
                                      job_type_to_string(j->type),
402✔
268
                                      time, limit,
269
                                      status_text ? ": " : "",
270
                                      strempty(status_text));
271
        }
272

273
        (void) sd_notifyf(/* unset_environment= */ false,
402✔
274
                          "STATUS=%sUser job %s/%s running (%s / %s)...",
275
                          job_of_n,
276
                          ident, job_type_to_string(j->type),
402✔
277
                          time, limit);
278
        m->status_ready = false;
402✔
279
}
402✔
280

281
static int have_ask_password(void) {
44✔
282
        _cleanup_closedir_ DIR *dir = NULL;
44✔
283

284
        dir = opendir("/run/systemd/ask-password");
44✔
285
        if (!dir) {
44✔
286
                if (errno == ENOENT)
×
287
                        return false;
288

289
                return -errno;
×
290
        }
291

292
        FOREACH_DIRENT_ALL(de, dir, return -errno) {
132✔
293
                if (!IN_SET(de->d_type, DT_REG, DT_UNKNOWN))
88✔
294
                        continue;
88✔
295

296
                if (startswith(de->d_name, "ask."))
×
297
                        return true;
298
        }
299

300
        return false;
301
}
302

303
static int manager_dispatch_ask_password_fd(sd_event_source *source,
44✔
304
                                            int fd, uint32_t revents, void *userdata) {
305
        Manager *m = ASSERT_PTR(userdata);
44✔
306

307
        (void) flush_fd(fd);
44✔
308

309
        m->have_ask_password = have_ask_password();
44✔
310
        if (m->have_ask_password < 0)
44✔
311
                /* Log error but continue. Negative have_ask_password is treated as unknown status. */
312
                log_warning_errno(m->have_ask_password, "Failed to list /run/systemd/ask-password/, ignoring: %m");
×
313

314
        return 0;
44✔
315
}
316

317
static void manager_close_ask_password(Manager *m) {
882✔
318
        assert(m);
882✔
319

320
        m->ask_password_event_source = sd_event_source_disable_unref(m->ask_password_event_source);
882✔
321
        m->have_ask_password = -EINVAL;
882✔
322
}
882✔
323

324
static int manager_check_ask_password(Manager *m) {
6,658✔
325
        int r;
6,658✔
326

327
        assert(m);
6,658✔
328

329
        /* We only care about passwords prompts when running in system mode (because that's the only time we
330
         * manage a console) */
331
        if (!MANAGER_IS_SYSTEM(m))
6,658✔
332
                return 0;
333

334
        if (!m->ask_password_event_source) {
6,658✔
335
                _cleanup_close_ int inotify_fd = inotify_init1(IN_NONBLOCK|IN_CLOEXEC);
88✔
336
                if (inotify_fd < 0)
44✔
337
                        return log_error_errno(errno, "Failed to create inotify object: %m");
×
338

339
                (void) mkdir_label("/run/systemd/ask-password", 0755);
44✔
340
                r = inotify_add_watch_and_warn(inotify_fd, "/run/systemd/ask-password", IN_CLOSE_WRITE|IN_DELETE|IN_MOVED_TO|IN_ONLYDIR);
44✔
341
                if (r < 0)
44✔
342
                        return r;
343

344
                _cleanup_(sd_event_source_disable_unrefp) sd_event_source *event_source = NULL;
44✔
345
                r = sd_event_add_io(
44✔
346
                                m->event,
347
                                &event_source,
348
                                inotify_fd,
349
                                EPOLLIN,
350
                                manager_dispatch_ask_password_fd,
351
                                m);
352
                if (r < 0)
44✔
353
                        return log_error_errno(r, "Failed to add event source for /run/systemd/ask-password/: %m");
×
354

355
                r = sd_event_source_set_io_fd_own(event_source, true);
44✔
356
                if (r < 0)
44✔
357
                        return log_error_errno(r, "Failed to pass ownership of /run/systemd/ask-password/ inotify fd to event source: %m");
×
358
                TAKE_FD(inotify_fd);
44✔
359

360
                (void) sd_event_source_set_description(event_source, "manager-ask-password");
44✔
361

362
                m->ask_password_event_source = TAKE_PTR(event_source);
44✔
363

364
                /* Queries might have been added meanwhile... */
365
                (void) manager_dispatch_ask_password_fd(m->ask_password_event_source, sd_event_source_get_io_fd(m->ask_password_event_source), EPOLLIN, m);
44✔
366
        }
367

368
        return m->have_ask_password;
6,658✔
369
}
370

371
static int manager_watch_idle_pipe(Manager *m) {
440✔
372
        int r;
440✔
373

374
        assert(m);
440✔
375

376
        if (m->idle_pipe_event_source)
440✔
377
                return 0;
378

379
        if (m->idle_pipe[2] < 0)
46✔
380
                return 0;
381

382
        r = sd_event_add_io(m->event, &m->idle_pipe_event_source, m->idle_pipe[2], EPOLLIN, manager_dispatch_idle_pipe_fd, m);
46✔
383
        if (r < 0)
46✔
384
                return log_error_errno(r, "Failed to watch idle pipe: %m");
×
385

386
        (void) sd_event_source_set_description(m->idle_pipe_event_source, "manager-idle-pipe");
46✔
387

388
        return 0;
46✔
389
}
390

391
static void manager_close_idle_pipe(Manager *m) {
3,360✔
392
        assert(m);
3,360✔
393

394
        m->idle_pipe_event_source = sd_event_source_disable_unref(m->idle_pipe_event_source);
3,360✔
395

396
        safe_close_pair(m->idle_pipe);
3,360✔
397
        safe_close_pair(m->idle_pipe + 2);
3,360✔
398
}
3,360✔
399

400
static int manager_setup_time_change(Manager *m) {
251✔
401
        int r;
251✔
402

403
        assert(m);
251✔
404

405
        if (MANAGER_IS_TEST_RUN(m))
251✔
406
                return 0;
407

408
        m->time_change_event_source = sd_event_source_disable_unref(m->time_change_event_source);
239✔
409

410
        r = event_add_time_change(m->event, &m->time_change_event_source, manager_dispatch_time_change_fd, m);
239✔
411
        if (r < 0)
239✔
412
                return log_error_errno(r, "Failed to create time change event source: %m");
×
413

414
        /* Schedule this slightly earlier than the .timer event sources */
415
        r = sd_event_source_set_priority(m->time_change_event_source, EVENT_PRIORITY_TIME_CHANGE);
239✔
416
        if (r < 0)
239✔
417
                return log_error_errno(r, "Failed to set priority of time change event sources: %m");
×
418

419
        log_debug("Set up TFD_TIMER_CANCEL_ON_SET timerfd.");
239✔
420

421
        return 0;
422
}
423

424
static int manager_read_timezone_stat(Manager *m) {
267✔
425
        struct stat st;
267✔
426
        bool changed;
267✔
427

428
        assert(m);
267✔
429

430
        /* Read the current stat() data of /etc/localtime so that we detect changes */
431
        if (lstat("/etc/localtime", &st) < 0) {
267✔
432
                log_debug_errno(errno, "Failed to stat /etc/localtime, ignoring: %m");
20✔
433
                changed = m->etc_localtime_accessible;
20✔
434
                m->etc_localtime_accessible = false;
20✔
435
        } else {
436
                usec_t k;
247✔
437

438
                k = timespec_load(&st.st_mtim);
247✔
439
                changed = !m->etc_localtime_accessible || k != m->etc_localtime_mtime;
247✔
440

441
                m->etc_localtime_mtime = k;
247✔
442
                m->etc_localtime_accessible = true;
247✔
443
        }
444

445
        return changed;
267✔
446
}
447

448
static int manager_setup_timezone_change(Manager *m) {
259✔
449
        _cleanup_(sd_event_source_unrefp) sd_event_source *new_event = NULL;
259✔
450
        int r;
259✔
451

452
        assert(m);
259✔
453

454
        if (MANAGER_IS_TEST_RUN(m))
259✔
455
                return 0;
456

457
        /* We watch /etc/localtime for three events: change of the link count (which might mean removal from /etc even
458
         * though another link might be kept), renames, and file close operations after writing. Note we don't bother
459
         * with IN_DELETE_SELF, as that would just report when the inode is removed entirely, i.e. after the link count
460
         * went to zero and all fds to it are closed.
461
         *
462
         * Note that we never follow symlinks here. This is a simplification, but should cover almost all cases
463
         * correctly.
464
         *
465
         * Note that we create the new event source first here, before releasing the old one. This should optimize
466
         * behaviour as this way sd-event can reuse the old watch in case the inode didn't change. */
467

468
        r = sd_event_add_inotify(m->event, &new_event, "/etc/localtime",
247✔
469
                                 IN_ATTRIB|IN_MOVE_SELF|IN_CLOSE_WRITE|IN_DONT_FOLLOW, manager_dispatch_timezone_change, m);
470
        if (r == -ENOENT) {
247✔
471
                /* If the file doesn't exist yet, subscribe to /etc instead, and wait until it is created either by
472
                 * O_CREATE or by rename() */
473

474
                log_debug_errno(r, "/etc/localtime doesn't exist yet, watching /etc instead.");
14✔
475
                r = sd_event_add_inotify(m->event, &new_event, "/etc",
14✔
476
                                         IN_CREATE|IN_MOVED_TO|IN_ONLYDIR, manager_dispatch_timezone_change, m);
477
        }
478
        if (r < 0)
247✔
479
                return log_error_errno(r, "Failed to create timezone change event source: %m");
×
480

481
        /* Schedule this slightly earlier than the .timer event sources */
482
        r = sd_event_source_set_priority(new_event, EVENT_PRIORITY_TIME_ZONE);
247✔
483
        if (r < 0)
247✔
484
                return log_error_errno(r, "Failed to set priority of timezone change event sources: %m");
×
485

486
        sd_event_source_unref(m->timezone_change_event_source);
247✔
487
        m->timezone_change_event_source = TAKE_PTR(new_event);
247✔
488

489
        return 0;
247✔
490
}
491

492
static int manager_enable_special_signals(Manager *m) {
248✔
493
        _cleanup_close_ int fd = -EBADF;
248✔
494

495
        assert(m);
248✔
496

497
        if (!MANAGER_IS_SYSTEM(m) || MANAGER_IS_TEST_RUN(m))
248✔
498
                return 0;
499

500
        /* Enable that we get SIGINT on control-alt-del. In containers this will fail with EPERM (older) or
501
         * EINVAL (newer), so ignore that. */
502
        if (reboot(RB_DISABLE_CAD) < 0 && !IN_SET(errno, EPERM, EINVAL))
60✔
503
                log_warning_errno(errno, "Failed to enable ctrl-alt-del handling, ignoring: %m");
×
504

505
        fd = open_terminal("/dev/tty0", O_RDWR|O_NOCTTY|O_CLOEXEC);
60✔
506
        if (fd < 0)
60✔
507
                /* Support systems without virtual console (ENOENT) gracefully */
508
                log_full_errno(fd == -ENOENT ? LOG_DEBUG : LOG_WARNING, fd, "Failed to open /dev/tty0, ignoring: %m");
40✔
509
        else {
510
                /* Enable that we get SIGWINCH on kbrequest */
511
                if (ioctl(fd, KDSIGACCEPT, SIGWINCH) < 0)
20✔
512
                        log_warning_errno(errno, "Failed to enable kbrequest handling, ignoring: %m");
248✔
513
        }
514

515
        return 0;
516
}
517

518
static int manager_setup_signals(Manager *m) {
248✔
519
        static const struct sigaction sa = {
248✔
520
                .sa_handler = SIG_DFL,
521
                .sa_flags = SA_NOCLDSTOP|SA_RESTART,
522
        };
523
        sigset_t mask;
248✔
524
        int r;
248✔
525

526
        assert(m);
248✔
527

528
        assert_se(sigaction(SIGCHLD, &sa, NULL) == 0);
248✔
529

530
        /* We make liberal use of realtime signals here. On Linux/glibc we have 30 of them, between
531
         * SIGRTMIN+0 ... SIGRTMIN+30 (aka SIGRTMAX). */
532

533
        assert_se(sigemptyset(&mask) == 0);
248✔
534
        sigset_add_many(&mask,
248✔
535
                        SIGCHLD,     /* Child died */
536
                        SIGTERM,     /* Reexecute daemon */
537
                        SIGHUP,      /* Reload configuration */
538
                        SIGUSR1,     /* systemd: reconnect to D-Bus */
539
                        SIGUSR2,     /* systemd: dump status */
540
                        SIGINT,      /* Kernel sends us this on control-alt-del */
541
                        SIGWINCH,    /* Kernel sends us this on kbrequest (alt-arrowup) */
542
                        SIGPWR,      /* Some kernel drivers and upsd send us this on power failure */
543

544
                        SIGRTMIN+0,  /* systemd: start default.target */
545
                        SIGRTMIN+1,  /* systemd: isolate rescue.target */
546
                        SIGRTMIN+2,  /* systemd: isolate emergency.target */
547
                        SIGRTMIN+3,  /* systemd: start halt.target */
548
                        SIGRTMIN+4,  /* systemd: start poweroff.target */
549
                        SIGRTMIN+5,  /* systemd: start reboot.target */
550
                        SIGRTMIN+6,  /* systemd: start kexec.target */
551
                        SIGRTMIN+7,  /* systemd: start soft-reboot.target */
552

553
                        /* ... space for more special targets ... */
554

555
                        SIGRTMIN+13, /* systemd: Immediate halt */
556
                        SIGRTMIN+14, /* systemd: Immediate poweroff */
557
                        SIGRTMIN+15, /* systemd: Immediate reboot */
558
                        SIGRTMIN+16, /* systemd: Immediate kexec */
559
                        SIGRTMIN+17, /* systemd: Immediate soft-reboot */
560
                        SIGRTMIN+18, /* systemd: control command */
561

562
                        /* ... space ... */
563

564
                        SIGRTMIN+20, /* systemd: enable status messages */
565
                        SIGRTMIN+21, /* systemd: disable status messages */
566
                        SIGRTMIN+22, /* systemd: set log level to LOG_DEBUG */
567
                        SIGRTMIN+23, /* systemd: set log level to LOG_INFO */
568
                        SIGRTMIN+24, /* systemd: Immediate exit (--user only) */
569
                        SIGRTMIN+25, /* systemd: reexecute manager */
570

571
                        SIGRTMIN+26, /* systemd: set log target to journal-or-kmsg */
572
                        SIGRTMIN+27, /* systemd: set log target to console */
573
                        SIGRTMIN+28, /* systemd: set log target to kmsg */
574
                        SIGRTMIN+29, /* systemd: set log target to syslog-or-kmsg (obsolete) */
575

576
                        /* ... one free signal here SIGRTMIN+30 ... */
577
                        -1);
578
        assert_se(sigprocmask(SIG_SETMASK, &mask, NULL) == 0);
248✔
579

580
        m->signal_fd = signalfd(-1, &mask, SFD_NONBLOCK|SFD_CLOEXEC);
248✔
581
        if (m->signal_fd < 0)
248✔
582
                return -errno;
×
583

584
        r = sd_event_add_io(m->event, &m->signal_event_source, m->signal_fd, EPOLLIN, manager_dispatch_signal_fd, m);
248✔
585
        if (r < 0)
248✔
586
                return r;
587

588
        (void) sd_event_source_set_description(m->signal_event_source, "manager-signal");
248✔
589

590
        /* Process signals a bit earlier than the rest of things, but later than notify_fd processing, so that the
591
         * notify processing can still figure out to which process/service a message belongs, before we reap the
592
         * process. Also, process this before handling cgroup notifications, so that we always collect child exit
593
         * status information before detecting that there's no process in a cgroup. */
594
        r = sd_event_source_set_priority(m->signal_event_source, EVENT_PRIORITY_SIGNALS);
248✔
595
        if (r < 0)
248✔
596
                return r;
597

598
        /* Report to supervisor that we now process the above signals. We report this as level "2", to
599
         * indicate that we support more than sysvinit's signals (of course, sysvinit never sent this
600
         * message, but conceptually it makes sense to consider level "1" to be equivalent to sysvinit's
601
         * signal handling). Also, by setting this to "2" people looking for this hopefully won't
602
         * misunderstand this as a boolean concept. Signal level 2 shall refer to the signals PID 1
603
         * understands at the time of release of systemd v256, i.e. including basic SIGRTMIN+18 handling for
604
         * memory pressure and stuff. When more signals are hooked up (or more SIGRTMIN+18 multiplex
605
         * operations added, this level should be increased). */
606
        (void) sd_notify(/* unset_environment= */ false,
248✔
607
                         "X_SYSTEMD_SIGNALS_LEVEL=2");
608

609
        return manager_enable_special_signals(m);
248✔
610
}
611

612
static char** sanitize_environment(char **l) {
1,223✔
613

614
        /* Let's remove some environment variables that we need ourselves to communicate with our clients */
615
        strv_env_unset_many(
1,223✔
616
                        l,
617
                        "CACHE_DIRECTORY",
618
                        "CONFIGURATION_DIRECTORY",
619
                        "CREDENTIALS_DIRECTORY",
620
                        "EXIT_CODE",
621
                        "EXIT_STATUS",
622
                        "INVOCATION_ID",
623
                        "JOURNAL_STREAM",
624
                        "LISTEN_FDNAMES",
625
                        "LISTEN_FDS",
626
                        "LISTEN_PID",
627
                        "LOGS_DIRECTORY",
628
                        "LOG_NAMESPACE",
629
                        "MAINPID",
630
                        "MANAGERPID",
631
                        "MEMORY_PRESSURE_WATCH",
632
                        "MEMORY_PRESSURE_WRITE",
633
                        "MONITOR_EXIT_CODE",
634
                        "MONITOR_EXIT_STATUS",
635
                        "MONITOR_INVOCATION_ID",
636
                        "MONITOR_SERVICE_RESULT",
637
                        "MONITOR_UNIT",
638
                        "NOTIFY_SOCKET",
639
                        "PIDFILE",
640
                        "REMOTE_ADDR",
641
                        "REMOTE_PORT",
642
                        "RUNTIME_DIRECTORY",
643
                        "SERVICE_RESULT",
644
                        "STATE_DIRECTORY",
645
                        "SYSTEMD_EXEC_PID",
646
                        "TRIGGER_PATH",
647
                        "TRIGGER_TIMER_MONOTONIC_USEC",
648
                        "TRIGGER_TIMER_REALTIME_USEC",
649
                        "TRIGGER_UNIT",
650
                        "WATCHDOG_PID",
651
                        "WATCHDOG_USEC");
652

653
        /* Let's order the environment alphabetically, just to make it pretty */
654
        return strv_sort(l);
1,223✔
655
}
656

657
int manager_default_environment(Manager *m) {
964✔
658
        assert(m);
964✔
659

660
        m->transient_environment = strv_free(m->transient_environment);
964✔
661

662
        if (MANAGER_IS_SYSTEM(m)) {
964✔
663
                /* The system manager always starts with a clean environment for its children. It does not
664
                 * import the kernel's or the parents' exported variables.
665
                 *
666
                 * The initial passed environment is untouched to keep /proc/self/environ valid; it is used
667
                 * for tagging the init process inside containers. */
668
                char *path = strjoin("PATH=", default_PATH());
591✔
669
                if (!path)
591✔
670
                        return log_oom();
×
671

672
                if (strv_consume(&m->transient_environment, path) < 0)
591✔
673
                        return log_oom();
×
674

675
                /* Import locale variables LC_*= from configuration */
676
                (void) locale_setup(&m->transient_environment);
591✔
677
        } else {
678
                /* The user manager passes its own environment along to its children, except for $PATH and
679
                 * session envs. */
680

681
                m->transient_environment = strv_copy(environ);
373✔
682
                if (!m->transient_environment)
373✔
683
                        return log_oom();
×
684

685
                char *path = strjoin("PATH=", default_user_PATH());
373✔
686
                if (!path)
373✔
687
                        return log_oom();
×
688

689
                if (strv_env_replace_consume(&m->transient_environment, path) < 0)
373✔
690
                        return log_oom();
×
691

692
                /* Envvars set for our 'manager' class session are private and should not be propagated
693
                 * to children. Also it's likely that the graphical session will set these on their own. */
694
                strv_env_unset_many(m->transient_environment,
373✔
695
                                    "XDG_SESSION_ID",
696
                                    "XDG_SESSION_CLASS",
697
                                    "XDG_SESSION_TYPE",
698
                                    "XDG_SESSION_DESKTOP",
699
                                    "XDG_SEAT",
700
                                    "XDG_VTNR");
701
        }
702

703
        sanitize_environment(m->transient_environment);
964✔
704
        return 0;
964✔
705
}
706

707
static int manager_setup_prefix(Manager *m) {
702✔
708
        struct table_entry {
702✔
709
                uint64_t type;
710
                const char *suffix;
711
        };
712

713
        static const struct table_entry paths_system[_EXEC_DIRECTORY_TYPE_MAX] = {
702✔
714
                [EXEC_DIRECTORY_RUNTIME]       = { SD_PATH_SYSTEM_RUNTIME,       NULL },
715
                [EXEC_DIRECTORY_STATE]         = { SD_PATH_SYSTEM_STATE_PRIVATE, NULL },
716
                [EXEC_DIRECTORY_CACHE]         = { SD_PATH_SYSTEM_STATE_CACHE,   NULL },
717
                [EXEC_DIRECTORY_LOGS]          = { SD_PATH_SYSTEM_STATE_LOGS,    NULL },
718
                [EXEC_DIRECTORY_CONFIGURATION] = { SD_PATH_SYSTEM_CONFIGURATION, NULL },
719
        };
720

721
        static const struct table_entry paths_user[_EXEC_DIRECTORY_TYPE_MAX] = {
702✔
722
                [EXEC_DIRECTORY_RUNTIME]       = { SD_PATH_USER_RUNTIME,       NULL  },
723
                [EXEC_DIRECTORY_STATE]         = { SD_PATH_USER_STATE_PRIVATE, NULL  },
724
                [EXEC_DIRECTORY_CACHE]         = { SD_PATH_USER_STATE_CACHE,   NULL  },
725
                [EXEC_DIRECTORY_LOGS]          = { SD_PATH_USER_STATE_PRIVATE, "log" },
726
                [EXEC_DIRECTORY_CONFIGURATION] = { SD_PATH_USER_CONFIGURATION, NULL  },
727
        };
728

729
        assert(m);
702✔
730

731
        const struct table_entry *p = MANAGER_IS_SYSTEM(m) ? paths_system : paths_user;
702✔
732
        int r;
702✔
733

734
        for (ExecDirectoryType i = 0; i < _EXEC_DIRECTORY_TYPE_MAX; i++) {
4,212✔
735
                r = sd_path_lookup(p[i].type, p[i].suffix, &m->prefix[i]);
3,510✔
736
                if (r < 0)
3,510✔
737
                        return log_warning_errno(r, "Failed to lookup %s path: %m",
×
738
                                                 exec_directory_type_to_string(i));
739
        }
740

741
        return 0;
742
}
743

744
static void manager_free_unit_name_maps(Manager *m) {
728✔
745
        m->unit_id_map = hashmap_free(m->unit_id_map);
728✔
746
        m->unit_name_map = hashmap_free(m->unit_name_map);
728✔
747
        m->unit_path_cache = set_free(m->unit_path_cache);
728✔
748
        m->unit_cache_timestamp_hash = 0;
728✔
749
}
728✔
750

751
static int manager_setup_run_queue(Manager *m) {
702✔
752
        int r;
702✔
753

754
        assert(m);
702✔
755
        assert(!m->run_queue_event_source);
702✔
756

757
        r = sd_event_add_defer(m->event, &m->run_queue_event_source, manager_dispatch_run_queue, m);
702✔
758
        if (r < 0)
702✔
759
                return r;
760

761
        r = sd_event_source_set_priority(m->run_queue_event_source, EVENT_PRIORITY_RUN_QUEUE);
702✔
762
        if (r < 0)
702✔
763
                return r;
764

765
        r = sd_event_source_set_enabled(m->run_queue_event_source, SD_EVENT_OFF);
702✔
766
        if (r < 0)
702✔
767
                return r;
768

769
        (void) sd_event_source_set_description(m->run_queue_event_source, "manager-run-queue");
702✔
770

771
        return 0;
702✔
772
}
773

774
static int manager_setup_sigchld_event_source(Manager *m) {
248✔
775
        int r;
248✔
776

777
        assert(m);
248✔
778
        assert(!m->sigchld_event_source);
248✔
779

780
        r = sd_event_add_defer(m->event, &m->sigchld_event_source, manager_dispatch_sigchld, m);
248✔
781
        if (r < 0)
248✔
782
                return r;
783

784
        r = sd_event_source_set_priority(m->sigchld_event_source, EVENT_PRIORITY_SIGCHLD);
248✔
785
        if (r < 0)
248✔
786
                return r;
787

788
        r = sd_event_source_set_enabled(m->sigchld_event_source, SD_EVENT_OFF);
248✔
789
        if (r < 0)
248✔
790
                return r;
791

792
        (void) sd_event_source_set_description(m->sigchld_event_source, "manager-sigchld");
248✔
793

794
        return 0;
248✔
795
}
796

797
int manager_setup_memory_pressure_event_source(Manager *m) {
513✔
798
        int r;
513✔
799

800
        assert(m);
513✔
801

802
        m->memory_pressure_event_source = sd_event_source_disable_unref(m->memory_pressure_event_source);
513✔
803

804
        r = sd_event_add_memory_pressure(m->event, &m->memory_pressure_event_source, NULL, NULL);
513✔
805
        if (r < 0)
513✔
806
                log_full_errno(ERRNO_IS_NOT_SUPPORTED(r) || ERRNO_IS_PRIVILEGE(r) || (r == -EHOSTDOWN) ? LOG_DEBUG : LOG_NOTICE, r,
×
807
                               "Failed to establish memory pressure event source, ignoring: %m");
808
        else if (m->defaults.memory_pressure_threshold_usec != USEC_INFINITY) {
513✔
809

810
                /* If there's a default memory pressure threshold set, also apply it to the service manager itself */
811
                r = sd_event_source_set_memory_pressure_period(
513✔
812
                                m->memory_pressure_event_source,
813
                                m->defaults.memory_pressure_threshold_usec,
814
                                MEMORY_PRESSURE_DEFAULT_WINDOW_USEC);
815
                if (r < 0)
513✔
816
                        log_warning_errno(r, "Failed to adjust memory pressure threshold, ignoring: %m");
12✔
817
        }
818

819
        return 0;
513✔
820
}
821

822
static int manager_find_credentials_dirs(Manager *m) {
702✔
823
        const char *e;
702✔
824
        int r;
702✔
825

826
        assert(m);
702✔
827

828
        r = get_credentials_dir(&e);
702✔
829
        if (r < 0) {
702✔
830
                if (r != -ENXIO)
642✔
831
                        log_debug_errno(r, "Failed to determine credentials directory, ignoring: %m");
×
832
        } else {
833
                m->received_credentials_directory = strdup(e);
60✔
834
                if (!m->received_credentials_directory)
60✔
835
                        return -ENOMEM;
702✔
836
        }
837

838
        r = get_encrypted_credentials_dir(&e);
702✔
839
        if (r < 0) {
702✔
840
                if (r != -ENXIO)
702✔
841
                        log_debug_errno(r, "Failed to determine encrypted credentials directory, ignoring: %m");
×
842
        } else {
843
                m->received_encrypted_credentials_directory = strdup(e);
×
844
                if (!m->received_encrypted_credentials_directory)
×
845
                        return -ENOMEM;
×
846
        }
847

848
        return 0;
849
}
850

851
void manager_set_switching_root(Manager *m, bool switching_root) {
945✔
852
        assert(m);
945✔
853

854
        m->switching_root = MANAGER_IS_SYSTEM(m) && switching_root;
945✔
855
}
945✔
856

857
double manager_get_progress(Manager *m) {
18✔
858
        assert(m);
18✔
859

860
        if (MANAGER_IS_FINISHED(m) || m->n_installed_jobs == 0)
35✔
861
                return 1.0;
862

863
        return 1.0 - ((double) hashmap_size(m->jobs) / (double) m->n_installed_jobs);
17✔
864
}
865

866
static int compare_job_priority(const void *a, const void *b) {
323,923✔
867
        const Job *x = a, *y = b;
323,923✔
868

869
        return unit_compare_priority(x->unit, y->unit);
323,923✔
870
}
871

872
int manager_new(RuntimeScope runtime_scope, ManagerTestRunFlags test_run_flags, Manager **ret) {
702✔
873
        _cleanup_(manager_freep) Manager *m = NULL;
702✔
874
        int r;
702✔
875

876
        assert(IN_SET(runtime_scope, RUNTIME_SCOPE_SYSTEM, RUNTIME_SCOPE_USER));
702✔
877
        assert(ret);
702✔
878

879
        m = new(Manager, 1);
702✔
880
        if (!m)
702✔
881
                return -ENOMEM;
882

883
        *m = (Manager) {
702✔
884
                .runtime_scope = runtime_scope,
885
                .objective = _MANAGER_OBJECTIVE_INVALID,
886
                .previous_objective = _MANAGER_OBJECTIVE_INVALID,
887

888
                .status_unit_format = STATUS_UNIT_FORMAT_DEFAULT,
889

890
                .original_log_level = -1,
891
                .original_log_target = _LOG_TARGET_INVALID,
892

893
                .watchdog_overridden[WATCHDOG_RUNTIME] = USEC_INFINITY,
894
                .watchdog_overridden[WATCHDOG_REBOOT] = USEC_INFINITY,
895
                .watchdog_overridden[WATCHDOG_KEXEC] = USEC_INFINITY,
896
                .watchdog_overridden[WATCHDOG_PRETIMEOUT] = USEC_INFINITY,
897

898
                .show_status_overridden = _SHOW_STATUS_INVALID,
899

900
                .notify_fd = -EBADF,
901
                .signal_fd = -EBADF,
902
                .user_lookup_fds = EBADF_PAIR,
903
                .handoff_timestamp_fds = EBADF_PAIR,
904
                .pidref_transport_fds = EBADF_PAIR,
905
                .private_listen_fd = -EBADF,
906
                .dev_autofs_fd = -EBADF,
907
                .cgroup_inotify_fd = -EBADF,
908
                .pin_cgroupfs_fd = -EBADF,
909
                .idle_pipe = { -EBADF, -EBADF, -EBADF, -EBADF},
910

911
                 /* start as id #1, so that we can leave #0 around as "null-like" value */
912
                .current_job_id = 1,
913

914
                .have_ask_password = -EINVAL, /* we don't know */
915
                .first_boot = -1,
916
                .test_run_flags = test_run_flags,
917

918
                .dump_ratelimit = (const RateLimit) { .interval = 10 * USEC_PER_MINUTE, .burst = 10 },
919

920
                .executor_fd = -EBADF,
921
        };
922

923
        unit_defaults_init(&m->defaults, runtime_scope);
702✔
924

925
#if ENABLE_EFI
926
        if (MANAGER_IS_SYSTEM(m) && detect_container() <= 0)
702✔
927
                boot_timestamps(m->timestamps + MANAGER_TIMESTAMP_USERSPACE,
22✔
928
                                m->timestamps + MANAGER_TIMESTAMP_FIRMWARE,
22✔
929
                                m->timestamps + MANAGER_TIMESTAMP_LOADER);
22✔
930
#endif
931

932
        /* Reboot immediately if the user hits C-A-D more often than 7x per 2s */
933
        m->ctrl_alt_del_ratelimit = (const RateLimit) { .interval = 2 * USEC_PER_SEC, .burst = 7 };
702✔
934

935
        r = manager_default_environment(m);
702✔
936
        if (r < 0)
702✔
937
                return r;
938

939
        r = hashmap_ensure_allocated(&m->units, &string_hash_ops);
702✔
940
        if (r < 0)
702✔
941
                return r;
942

943
        r = hashmap_ensure_allocated(&m->cgroup_unit, &path_hash_ops);
702✔
944
        if (r < 0)
702✔
945
                return r;
946

947
        r = hashmap_ensure_allocated(&m->watch_bus, &string_hash_ops);
702✔
948
        if (r < 0)
702✔
949
                return r;
950

951
        r = prioq_ensure_allocated(&m->run_queue, compare_job_priority);
702✔
952
        if (r < 0)
702✔
953
                return r;
954

955
        r = manager_setup_prefix(m);
702✔
956
        if (r < 0)
702✔
957
                return r;
958

959
        r = manager_find_credentials_dirs(m);
702✔
960
        if (r < 0)
702✔
961
                return r;
962

963
        r = sd_event_default(&m->event);
702✔
964
        if (r < 0)
702✔
965
                return r;
966

967
        r = manager_setup_run_queue(m);
702✔
968
        if (r < 0)
702✔
969
                return r;
970

971
        if (FLAGS_SET(test_run_flags, MANAGER_TEST_RUN_MINIMAL)) {
702✔
972
                m->cgroup_root = strdup("");
454✔
973
                if (!m->cgroup_root)
454✔
974
                        return -ENOMEM;
975
        } else {
976
                r = manager_setup_signals(m);
248✔
977
                if (r < 0)
248✔
978
                        return r;
979

980
                r = manager_setup_cgroup(m);
248✔
981
                if (r < 0)
248✔
982
                        return r;
983

984
                r = manager_setup_time_change(m);
248✔
985
                if (r < 0)
248✔
986
                        return r;
987

988
                r = manager_read_timezone_stat(m);
248✔
989
                if (r < 0)
248✔
990
                        return r;
991

992
                (void) manager_setup_timezone_change(m);
248✔
993

994
                r = manager_setup_sigchld_event_source(m);
248✔
995
                if (r < 0)
248✔
996
                        return r;
997

998
                r = manager_setup_memory_pressure_event_source(m);
248✔
999
                if (r < 0)
248✔
1000
                        return r;
1001

1002
#if HAVE_LIBBPF
1003
                if (MANAGER_IS_SYSTEM(m) && bpf_restrict_fs_supported(/* initialize = */ true)) {
248✔
1004
                        r = bpf_restrict_fs_setup(m);
11✔
1005
                        if (r < 0)
11✔
1006
                                log_warning_errno(r, "Failed to setup LSM BPF, ignoring: %m");
×
1007
                }
1008
#endif
1009
        }
1010

1011
        if (test_run_flags == 0) {
702✔
1012
                if (MANAGER_IS_SYSTEM(m))
236✔
1013
                        r = mkdir_label("/run/systemd/units", 0755);
60✔
1014
                else {
1015
                        _cleanup_free_ char *units_path = NULL;
176✔
1016
                        r = xdg_user_runtime_dir("/systemd/units", &units_path);
176✔
1017
                        if (r < 0)
176✔
1018
                                return r;
×
1019

1020
                        r = mkdir_label(units_path, 0755);
176✔
1021
                }
1022
                if (r < 0 && r != -EEXIST)
236✔
1023
                        return r;
1024
        }
1025

1026
        if (!FLAGS_SET(test_run_flags, MANAGER_TEST_DONT_OPEN_EXECUTOR)) {
702✔
1027
                m->executor_fd = pin_callout_binary(SYSTEMD_EXECUTOR_BINARY_PATH, &m->executor_path);
255✔
1028
                if (m->executor_fd < 0)
255✔
1029
                        return log_debug_errno(m->executor_fd, "Failed to pin executor binary: %m");
×
1030

1031
                log_debug("Using systemd-executor binary from '%s'.", m->executor_path);
255✔
1032
        }
1033

1034
        /* Note that we do not set up the notify fd here. We do that after deserialization,
1035
         * since they might have gotten serialized across the reexec. */
1036

1037
        *ret = TAKE_PTR(m);
702✔
1038

1039
        return 0;
702✔
1040
}
1041

1042
static int manager_setup_notify(Manager *m) {
726✔
1043
        int r;
726✔
1044

1045
        if (MANAGER_IS_TEST_RUN(m))
726✔
1046
                return 0;
1047

1048
        if (m->notify_fd < 0) {
262✔
1049
                _cleanup_close_ int fd = -EBADF;
205✔
1050
                union sockaddr_union sa;
205✔
1051
                socklen_t sa_len;
205✔
1052

1053
                /* First free all secondary fields */
1054
                m->notify_socket = mfree(m->notify_socket);
205✔
1055
                m->notify_event_source = sd_event_source_disable_unref(m->notify_event_source);
205✔
1056

1057
                fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0);
205✔
1058
                if (fd < 0)
205✔
1059
                        return log_error_errno(errno, "Failed to allocate notification socket: %m");
×
1060

1061
                (void) fd_increase_rxbuf(fd, MANAGER_SOCKET_RCVBUF_SIZE);
205✔
1062

1063
                m->notify_socket = path_join(m->prefix[EXEC_DIRECTORY_RUNTIME], "systemd/notify");
205✔
1064
                if (!m->notify_socket)
205✔
1065
                        return log_oom();
×
1066

1067
                r = sockaddr_un_set_path(&sa.un, m->notify_socket);
205✔
1068
                if (r < 0)
205✔
1069
                        return log_error_errno(r, "Notify socket '%s' not valid for AF_UNIX socket address, refusing.",
×
1070
                                               m->notify_socket);
1071
                sa_len = r;
205✔
1072

1073
                (void) sockaddr_un_unlink(&sa.un);
205✔
1074

1075
                r = mac_selinux_bind(fd, &sa.sa, sa_len);
205✔
1076
                if (r < 0)
205✔
1077
                        return log_error_errno(r, "Failed to bind notify fd to '%s': %m", m->notify_socket);
×
1078

1079
                r = setsockopt_int(fd, SOL_SOCKET, SO_PASSCRED, true);
205✔
1080
                if (r < 0)
205✔
1081
                        return log_error_errno(r, "Failed to enable SO_PASSCRED for notify socket: %m");
×
1082

1083
                // TODO: enforce SO_PASSPIDFD when our baseline of the kernel version is bumped to >= 6.5.
1084
                r = setsockopt_int(fd, SOL_SOCKET, SO_PASSPIDFD, true);
205✔
1085
                if (r < 0 && r != -ENOPROTOOPT)
205✔
1086
                        log_warning_errno(r, "Failed to enable SO_PASSPIDFD for notify socket, ignoring: %m");
×
1087

1088
                m->notify_fd = TAKE_FD(fd);
205✔
1089

1090
                log_debug("Using notification socket %s", m->notify_socket);
205✔
1091
        }
1092

1093
        if (!m->notify_event_source) {
262✔
1094
                r = sd_event_add_io(m->event, &m->notify_event_source, m->notify_fd, EPOLLIN, manager_dispatch_notify_fd, m);
262✔
1095
                if (r < 0)
262✔
1096
                        return log_error_errno(r, "Failed to allocate notify event source: %m");
×
1097

1098
                /* Process notification messages a bit earlier than SIGCHLD, so that we can still identify to which
1099
                 * service an exit message belongs. */
1100
                r = sd_event_source_set_priority(m->notify_event_source, EVENT_PRIORITY_NOTIFY);
262✔
1101
                if (r < 0)
262✔
1102
                        return log_error_errno(r, "Failed to set priority of notify event source: %m");
×
1103

1104
                (void) sd_event_source_set_description(m->notify_event_source, "manager-notify");
262✔
1105
        }
1106

1107
        return 0;
1108
}
1109

1110
static int manager_setup_user_lookup_fd(Manager *m) {
726✔
1111
        int r;
726✔
1112

1113
        assert(m);
726✔
1114

1115
        /* Set up the socket pair used for passing UID/GID resolution results from forked off processes to PID
1116
         * 1. Background: we can't do name lookups (NSS) from PID 1, since it might involve IPC and thus activation,
1117
         * and we might hence deadlock on ourselves. Hence we do all user/group lookups asynchronously from the forked
1118
         * off processes right before executing the binaries to start. In order to be able to clean up any IPC objects
1119
         * created by a unit (see RemoveIPC=) we need to know in PID 1 the used UID/GID of the executed processes,
1120
         * hence we establish this communication channel so that forked off processes can pass their UID/GID
1121
         * information back to PID 1. The forked off processes send their resolved UID/GID to PID 1 in a simple
1122
         * datagram, along with their unit name, so that we can share one communication socket pair among all units for
1123
         * this purpose.
1124
         *
1125
         * You might wonder why we need a communication channel for this that is independent of the usual notification
1126
         * socket scheme (i.e. $NOTIFY_SOCKET). The primary difference is about trust: data sent via the $NOTIFY_SOCKET
1127
         * channel is only accepted if it originates from the right unit and if reception was enabled for it. The user
1128
         * lookup socket OTOH is only accessible by PID 1 and its children until they exec(), and always available.
1129
         *
1130
         * Note that this function is called under two circumstances: when we first initialize (in which case we
1131
         * allocate both the socket pair and the event source to listen on it), and when we deserialize after a reload
1132
         * (in which case the socket pair already exists but we still need to allocate the event source for it). */
1133

1134
        if (m->user_lookup_fds[0] < 0) {
726✔
1135

1136
                /* Free all secondary fields */
1137
                safe_close_pair(m->user_lookup_fds);
669✔
1138
                m->user_lookup_event_source = sd_event_source_disable_unref(m->user_lookup_event_source);
669✔
1139

1140
                if (socketpair(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC, 0, m->user_lookup_fds) < 0)
669✔
1141
                        return log_error_errno(errno, "Failed to allocate user lookup socket: %m");
×
1142

1143
                (void) fd_increase_rxbuf(m->user_lookup_fds[0], MANAGER_SOCKET_RCVBUF_SIZE);
669✔
1144
        }
1145

1146
        if (!m->user_lookup_event_source) {
726✔
1147
                r = sd_event_add_io(m->event, &m->user_lookup_event_source, m->user_lookup_fds[0], EPOLLIN, manager_dispatch_user_lookup_fd, m);
726✔
1148
                if (r < 0)
726✔
1149
                        return log_error_errno(r, "Failed to allocate user lookup event source: %m");
×
1150

1151
                /* Process even earlier than the notify event source, so that we always know first about valid UID/GID
1152
                 * resolutions */
1153
                r = sd_event_source_set_priority(m->user_lookup_event_source, EVENT_PRIORITY_USER_LOOKUP);
726✔
1154
                if (r < 0)
726✔
1155
                        return log_error_errno(r, "Failed to set priority of user lookup event source: %m");
×
1156

1157
                (void) sd_event_source_set_description(m->user_lookup_event_source, "user-lookup");
726✔
1158
        }
1159

1160
        return 0;
1161
}
1162

1163
static int manager_setup_handoff_timestamp_fd(Manager *m) {
726✔
1164
        int r;
726✔
1165

1166
        assert(m);
726✔
1167

1168
        /* Set up the socket pair used for passing timestamps back when the executor processes we fork
1169
         * off invokes execve(), i.e. when we hand off control to our payload processes. */
1170

1171
        if (m->handoff_timestamp_fds[0] < 0) {
726✔
1172
                m->handoff_timestamp_event_source = sd_event_source_disable_unref(m->handoff_timestamp_event_source);
669✔
1173
                safe_close_pair(m->handoff_timestamp_fds);
669✔
1174

1175
                if (socketpair(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC, 0, m->handoff_timestamp_fds) < 0)
669✔
1176
                        return log_error_errno(errno, "Failed to allocate handoff timestamp socket: %m");
×
1177

1178
                /* Make sure children never have to block */
1179
                (void) fd_increase_rxbuf(m->handoff_timestamp_fds[0], MANAGER_SOCKET_RCVBUF_SIZE);
669✔
1180

1181
                r = setsockopt_int(m->handoff_timestamp_fds[0], SOL_SOCKET, SO_PASSCRED, true);
669✔
1182
                if (r < 0)
669✔
1183
                        return log_error_errno(r, "SO_PASSCRED failed: %m");
×
1184

1185
                /* Mark the receiving socket as O_NONBLOCK (but leave sending side as-is) */
1186
                r = fd_nonblock(m->handoff_timestamp_fds[0], true);
669✔
1187
                if (r < 0)
669✔
1188
                        return log_error_errno(r, "Failed to make handoff timestamp socket O_NONBLOCK: %m");
×
1189
        }
1190

1191
        if (!m->handoff_timestamp_event_source) {
726✔
1192
                r = sd_event_add_io(m->event, &m->handoff_timestamp_event_source, m->handoff_timestamp_fds[0], EPOLLIN, manager_dispatch_handoff_timestamp_fd, m);
726✔
1193
                if (r < 0)
726✔
1194
                        return log_error_errno(r, "Failed to allocate handoff timestamp event source: %m");
×
1195

1196
                r = sd_event_source_set_priority(m->handoff_timestamp_event_source, EVENT_PRIORITY_HANDOFF_TIMESTAMP);
726✔
1197
                if (r < 0)
726✔
1198
                        return log_error_errno(r, "Failed to set priority of handoff timestamp event source: %m");
×
1199

1200
                (void) sd_event_source_set_description(m->handoff_timestamp_event_source, "handoff-timestamp");
726✔
1201
        }
1202

1203
        return 0;
1204
}
1205

1206
static int manager_setup_pidref_transport_fd(Manager *m) {
726✔
1207
        int r;
726✔
1208

1209
        assert(m);
726✔
1210

1211
        /* Set up the socket pair used for passing parent and child pidrefs back when the executor unshares
1212
         * a PID namespace and forks again when using PrivatePIDs=yes. */
1213

1214
        if (m->pidref_transport_fds[0] < 0) {
726✔
1215
                m->pidref_event_source = sd_event_source_disable_unref(m->pidref_event_source);
700✔
1216
                safe_close_pair(m->pidref_transport_fds);
700✔
1217

1218
                if (socketpair(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC, 0, m->pidref_transport_fds) < 0)
700✔
1219
                        return log_error_errno(errno, "Failed to allocate pidref socket: %m");
×
1220

1221
                /* Make sure children never have to block */
1222
                (void) fd_increase_rxbuf(m->pidref_transport_fds[0], MANAGER_SOCKET_RCVBUF_SIZE);
700✔
1223

1224
                r = setsockopt_int(m->pidref_transport_fds[0], SOL_SOCKET, SO_PASSCRED, true);
700✔
1225
                if (r < 0)
700✔
1226
                        return log_error_errno(r, "Failed to enable SO_PASSCRED for pidref socket: %m");
×
1227

1228
                r = setsockopt_int(m->pidref_transport_fds[0], SOL_SOCKET, SO_PASSPIDFD, true);
700✔
1229
                if (ERRNO_IS_NEG_NOT_SUPPORTED(r))
700✔
1230
                        log_debug("SO_PASSPIDFD is not supported for pidref socket, ignoring.");
×
1231
                else if (r < 0)
700✔
1232
                        log_warning_errno(r, "Failed to enable SO_PASSPIDFD for pidref socket, ignoring: %m");
×
1233

1234
                /* Mark the receiving socket as O_NONBLOCK (but leave sending side as-is) */
1235
                r = fd_nonblock(m->pidref_transport_fds[0], true);
700✔
1236
                if (r < 0)
700✔
1237
                        return log_error_errno(r, "Failed to make pidref socket O_NONBLOCK: %m");
×
1238
        }
1239

1240
        if (!m->pidref_event_source) {
726✔
1241
                r = sd_event_add_io(m->event, &m->pidref_event_source, m->pidref_transport_fds[0], EPOLLIN, manager_dispatch_pidref_transport_fd, m);
700✔
1242
                if (r < 0)
700✔
1243
                        return log_error_errno(r, "Failed to allocate pidref event source: %m");
×
1244

1245
                r = sd_event_source_set_priority(m->pidref_event_source, EVENT_PRIORITY_PIDREF);
700✔
1246
                if (r < 0)
700✔
1247
                        return log_error_errno(r, "Failed to set priority of pidref event source: %m");
×
1248

1249
                (void) sd_event_source_set_description(m->pidref_event_source, "pidref");
700✔
1250
        }
1251

1252
        return 0;
1253
}
1254

1255
static unsigned manager_dispatch_cleanup_queue(Manager *m) {
187,820✔
1256
        Unit *u;
187,820✔
1257
        unsigned n = 0;
187,820✔
1258

1259
        assert(m);
187,820✔
1260

1261
        while ((u = m->cleanup_queue)) {
221,877✔
1262
                assert(u->in_cleanup_queue);
34,057✔
1263

1264
                unit_free(u);
34,057✔
1265
                n++;
34,057✔
1266
        }
1267

1268
        return n;
187,820✔
1269
}
1270

1271
static unsigned manager_dispatch_release_resources_queue(Manager *m) {
175,097✔
1272
        unsigned n = 0;
175,097✔
1273
        Unit *u;
175,097✔
1274

1275
        assert(m);
175,097✔
1276

1277
        while ((u = LIST_POP(release_resources_queue, m->release_resources_queue))) {
176,271✔
1278
                assert(u->in_release_resources_queue);
1,174✔
1279
                u->in_release_resources_queue = false;
1,174✔
1280

1281
                n++;
1,174✔
1282

1283
                unit_release_resources(u);
1,174✔
1284
        }
1285

1286
        return n;
175,097✔
1287
}
1288

1289
enum {
1290
        GC_OFFSET_IN_PATH,  /* This one is on the path we were traveling */
1291
        GC_OFFSET_UNSURE,   /* No clue */
1292
        GC_OFFSET_GOOD,     /* We still need this unit */
1293
        GC_OFFSET_BAD,      /* We don't need this unit anymore */
1294
        _GC_OFFSET_MAX
1295
};
1296

1297
static void unit_gc_mark_good(Unit *u, unsigned gc_marker) {
66,697✔
1298
        Unit *other;
66,697✔
1299

1300
        u->gc_marker = gc_marker + GC_OFFSET_GOOD;
66,697✔
1301

1302
        /* Recursively mark referenced units as GOOD as well */
1303
        UNIT_FOREACH_DEPENDENCY(other, u, UNIT_ATOM_REFERENCES)
476,237✔
1304
                if (other->gc_marker == gc_marker + GC_OFFSET_UNSURE)
261,593✔
1305
                        unit_gc_mark_good(other, gc_marker);
1,251✔
1306
}
66,697✔
1307

1308
static void unit_gc_sweep(Unit *u, unsigned gc_marker) {
116,911✔
1309
        Unit *other;
116,911✔
1310
        bool is_bad;
116,911✔
1311

1312
        assert(u);
116,911✔
1313

1314
        if (IN_SET(u->gc_marker - gc_marker,
116,911✔
1315
                   GC_OFFSET_GOOD, GC_OFFSET_BAD, GC_OFFSET_UNSURE, GC_OFFSET_IN_PATH))
1316
                return;
51,465✔
1317

1318
        if (u->in_cleanup_queue)
100,753✔
1319
                goto bad;
×
1320

1321
        if (!unit_may_gc(u))
100,753✔
1322
                goto good;
45,034✔
1323

1324
        u->gc_marker = gc_marker + GC_OFFSET_IN_PATH;
55,719✔
1325

1326
        is_bad = true;
55,719✔
1327

1328
        UNIT_FOREACH_DEPENDENCY(other, u, UNIT_ATOM_REFERENCED_BY) {
117,528✔
1329
                unit_gc_sweep(other, gc_marker);
24,277✔
1330

1331
                if (other->gc_marker == gc_marker + GC_OFFSET_GOOD)
24,277✔
1332
                        goto good;
20,412✔
1333

1334
                if (other->gc_marker != gc_marker + GC_OFFSET_BAD)
3,865✔
1335
                        is_bad = false;
3,261✔
1336
        }
1337

1338
        LIST_FOREACH(refs_by_target, ref, u->refs_by_target) {
35,524✔
1339
                unit_gc_sweep(ref->source, gc_marker);
217✔
1340

1341
                if (ref->source->gc_marker == gc_marker + GC_OFFSET_GOOD)
217✔
1342
                        goto good;
×
1343

1344
                if (ref->source->gc_marker != gc_marker + GC_OFFSET_BAD)
217✔
1345
                        is_bad = false;
217✔
1346
        }
1347

1348
        if (is_bad)
35,307✔
1349
                goto bad;
34,050✔
1350

1351
        /* We were unable to find anything out about this entry, so
1352
         * let's investigate it later */
1353
        u->gc_marker = gc_marker + GC_OFFSET_UNSURE;
1,257✔
1354
        unit_add_to_gc_queue(u);
1,257✔
1355
        return;
1356

1357
bad:
34,050✔
1358
        /* We definitely know that this one is not useful anymore, so
1359
         * let's mark it for deletion */
1360
        u->gc_marker = gc_marker + GC_OFFSET_BAD;
34,050✔
1361
        unit_add_to_cleanup_queue(u);
34,050✔
1362
        return;
1363

1364
good:
65,446✔
1365
        unit_gc_mark_good(u, gc_marker);
65,446✔
1366
}
1367

1368
static unsigned manager_dispatch_gc_unit_queue(Manager *m) {
201,535✔
1369
        unsigned n = 0, gc_marker;
201,535✔
1370
        Unit *u;
201,535✔
1371

1372
        assert(m);
201,535✔
1373

1374
        /* log_debug("Running GC..."); */
1375

1376
        m->gc_marker += _GC_OFFSET_MAX;
201,535✔
1377
        if (m->gc_marker + _GC_OFFSET_MAX <= _GC_OFFSET_MAX)
201,535✔
1378
                m->gc_marker = 1;
×
1379

1380
        gc_marker = m->gc_marker;
201,535✔
1381

1382
        while ((u = LIST_POP(gc_queue, m->gc_unit_queue))) {
293,952✔
1383
                assert(u->in_gc_queue);
92,417✔
1384

1385
                unit_gc_sweep(u, gc_marker);
92,417✔
1386

1387
                u->in_gc_queue = false;
92,417✔
1388

1389
                n++;
92,417✔
1390

1391
                if (IN_SET(u->gc_marker - gc_marker,
92,417✔
1392
                           GC_OFFSET_BAD, GC_OFFSET_UNSURE)) {
1393
                        if (u->id)
34,056✔
1394
                                log_unit_debug(u, "Collecting.");
34,056✔
1395
                        u->gc_marker = gc_marker + GC_OFFSET_BAD;
34,056✔
1396
                        unit_add_to_cleanup_queue(u);
34,056✔
1397
                }
1398
        }
1399

1400
        return n;
201,535✔
1401
}
1402

1403
static unsigned manager_dispatch_gc_job_queue(Manager *m) {
201,542✔
1404
        unsigned n = 0;
201,542✔
1405
        Job *j;
201,542✔
1406

1407
        assert(m);
201,542✔
1408

1409
        while ((j = LIST_POP(gc_queue, m->gc_job_queue))) {
201,549✔
1410
                assert(j->in_gc_queue);
7✔
1411
                j->in_gc_queue = false;
7✔
1412

1413
                n++;
7✔
1414

1415
                if (!job_may_gc(j))
7✔
1416
                        continue;
7✔
1417

1418
                log_unit_debug(j->unit, "Collecting job.");
×
1419
                (void) job_finish_and_invalidate(j, JOB_COLLECTED, false, false);
×
1420
        }
1421

1422
        return n;
201,542✔
1423
}
1424

1425
static int manager_ratelimit_requeue(sd_event_source *s, uint64_t usec, void *userdata) {
×
1426
        Unit *u = userdata;
×
1427

1428
        assert(u);
×
1429
        assert(s == u->auto_start_stop_event_source);
×
1430

1431
        u->auto_start_stop_event_source = sd_event_source_unref(u->auto_start_stop_event_source);
×
1432

1433
        /* Re-queue to all queues, if the rate limit hit we might have been throttled on any of them. */
1434
        unit_submit_to_stop_when_unneeded_queue(u);
×
1435
        unit_submit_to_start_when_upheld_queue(u);
×
1436
        unit_submit_to_stop_when_bound_queue(u);
×
1437

1438
        return 0;
×
1439
}
1440

1441
static int manager_ratelimit_check_and_queue(Unit *u) {
14✔
1442
        int r;
14✔
1443

1444
        assert(u);
14✔
1445

1446
        if (ratelimit_below(&u->auto_start_stop_ratelimit))
14✔
1447
                return 1;
1448

1449
        /* Already queued, no need to requeue */
1450
        if (u->auto_start_stop_event_source)
×
1451
                return 0;
1452

1453
        r = sd_event_add_time(
×
1454
                        u->manager->event,
×
1455
                        &u->auto_start_stop_event_source,
1456
                        CLOCK_MONOTONIC,
1457
                        ratelimit_end(&u->auto_start_stop_ratelimit),
×
1458
                        0,
1459
                        manager_ratelimit_requeue,
1460
                        u);
1461
        if (r < 0)
×
1462
                return log_unit_error_errno(u, r, "Failed to queue timer on event loop: %m");
×
1463

1464
        return 0;
1465
}
1466

1467
static unsigned manager_dispatch_stop_when_unneeded_queue(Manager *m) {
175,235✔
1468
        unsigned n = 0;
175,235✔
1469
        Unit *u;
175,235✔
1470
        int r;
175,235✔
1471

1472
        assert(m);
175,235✔
1473

1474
        while ((u = LIST_POP(stop_when_unneeded_queue, m->stop_when_unneeded_queue))) {
175,426✔
1475
                _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
191✔
1476

1477
                assert(u->in_stop_when_unneeded_queue);
191✔
1478
                u->in_stop_when_unneeded_queue = false;
191✔
1479

1480
                n++;
191✔
1481

1482
                if (!unit_is_unneeded(u))
191✔
1483
                        continue;
180✔
1484

1485
                log_unit_debug(u, "Unit is not needed anymore.");
11✔
1486

1487
                /* If stopping a unit fails continuously we might enter a stop loop here, hence stop acting on the
1488
                 * service being unnecessary after a while. */
1489

1490
                r = manager_ratelimit_check_and_queue(u);
11✔
1491
                if (r <= 0) {
11✔
1492
                        log_unit_warning(u,
×
1493
                                         "Unit not needed anymore, but not stopping since we tried this too often recently.%s",
1494
                                         r == 0 ? " Will retry later." : "");
1495
                        continue;
×
1496
                }
1497

1498
                /* Ok, nobody needs us anymore. Sniff. Then let's commit suicide */
1499
                r = manager_add_job(u->manager, JOB_STOP, u, JOB_FAIL, &error, /* ret = */ NULL);
11✔
1500
                if (r < 0)
11✔
1501
                        log_unit_warning_errno(u, r, "Failed to enqueue stop job, ignoring: %s", bus_error_message(&error, r));
×
1502
        }
1503

1504
        return n;
175,235✔
1505
}
1506

1507
static unsigned manager_dispatch_start_when_upheld_queue(Manager *m) {
175,377✔
1508
        unsigned n = 0;
175,377✔
1509
        Unit *u;
175,377✔
1510
        int r;
175,377✔
1511

1512
        assert(m);
175,377✔
1513

1514
        while ((u = LIST_POP(start_when_upheld_queue, m->start_when_upheld_queue))) {
175,377✔
1515
                _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
×
1516
                Unit *culprit = NULL;
×
1517

1518
                assert(u->in_start_when_upheld_queue);
×
1519
                u->in_start_when_upheld_queue = false;
×
1520

1521
                n++;
×
1522

1523
                if (!unit_is_upheld_by_active(u, &culprit))
×
1524
                        continue;
×
1525

1526
                log_unit_debug(u, "Unit is started because upheld by active unit %s.", culprit->id);
×
1527

1528
                /* If stopping a unit fails continuously we might enter a stop loop here, hence stop acting on the
1529
                 * service being unnecessary after a while. */
1530

1531
                r = manager_ratelimit_check_and_queue(u);
×
1532
                if (r <= 0) {
×
1533
                        log_unit_warning(u,
×
1534
                                         "Unit needs to be started because active unit %s upholds it, but not starting since we tried this too often recently.%s",
1535
                                         culprit->id,
1536
                                         r == 0 ? " Will retry later." : "");
1537
                        continue;
×
1538
                }
1539

1540
                r = manager_add_job(u->manager, JOB_START, u, JOB_FAIL, &error, /* ret = */ NULL);
×
1541
                if (r < 0)
×
1542
                        log_unit_warning_errno(u, r, "Failed to enqueue start job, ignoring: %s", bus_error_message(&error, r));
×
1543
        }
1544

1545
        return n;
175,377✔
1546
}
1547

1548
static unsigned manager_dispatch_stop_when_bound_queue(Manager *m) {
175,377✔
1549
        unsigned n = 0;
175,377✔
1550
        Unit *u;
175,377✔
1551
        int r;
175,377✔
1552

1553
        assert(m);
175,377✔
1554

1555
        while ((u = LIST_POP(stop_when_bound_queue, m->stop_when_bound_queue))) {
175,642✔
1556
                _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
265✔
1557
                Unit *culprit = NULL;
265✔
1558

1559
                assert(u->in_stop_when_bound_queue);
265✔
1560
                u->in_stop_when_bound_queue = false;
265✔
1561

1562
                n++;
265✔
1563

1564
                if (!unit_is_bound_by_inactive(u, &culprit))
265✔
1565
                        continue;
262✔
1566

1567
                log_unit_debug(u, "Unit is stopped because bound to inactive unit %s.", culprit->id);
3✔
1568

1569
                /* If stopping a unit fails continuously we might enter a stop loop here, hence stop acting on the
1570
                 * service being unnecessary after a while. */
1571

1572
                r = manager_ratelimit_check_and_queue(u);
3✔
1573
                if (r <= 0) {
3✔
1574
                        log_unit_warning(u,
×
1575
                                         "Unit needs to be stopped because it is bound to inactive unit %s it, but not stopping since we tried this too often recently.%s",
1576
                                         culprit->id,
1577
                                         r == 0 ? " Will retry later." : "");
1578
                        continue;
×
1579
                }
1580

1581
                r = manager_add_job(u->manager, JOB_STOP, u, JOB_REPLACE, &error, /* ret = */ NULL);
3✔
1582
                if (r < 0)
3✔
1583
                        log_unit_warning_errno(u, r, "Failed to enqueue stop job, ignoring: %s", bus_error_message(&error, r));
×
1584
        }
1585

1586
        return n;
175,377✔
1587
}
1588

1589
static void manager_clear_jobs_and_units(Manager *m) {
728✔
1590
        Unit *u;
728✔
1591

1592
        assert(m);
728✔
1593

1594
        while ((u = hashmap_first(m->units)))
46,204✔
1595
                unit_free(u);
45,476✔
1596

1597
        manager_dispatch_cleanup_queue(m);
728✔
1598

1599
        assert(!m->load_queue);
728✔
1600
        assert(prioq_isempty(m->run_queue));
728✔
1601
        assert(!m->dbus_unit_queue);
728✔
1602
        assert(!m->dbus_job_queue);
728✔
1603
        assert(!m->cleanup_queue);
728✔
1604
        assert(!m->gc_unit_queue);
728✔
1605
        assert(!m->gc_job_queue);
728✔
1606
        assert(!m->cgroup_realize_queue);
728✔
1607
        assert(!m->cgroup_empty_queue);
728✔
1608
        assert(!m->cgroup_oom_queue);
728✔
1609
        assert(!m->target_deps_queue);
728✔
1610
        assert(!m->stop_when_unneeded_queue);
728✔
1611
        assert(!m->start_when_upheld_queue);
728✔
1612
        assert(!m->stop_when_bound_queue);
728✔
1613
        assert(!m->release_resources_queue);
728✔
1614

1615
        assert(hashmap_isempty(m->jobs));
728✔
1616
        assert(hashmap_isempty(m->units));
728✔
1617
        assert(hashmap_isempty(m->units_by_invocation_id));
728✔
1618

1619
        m->n_on_console = 0;
728✔
1620
        m->n_running_jobs = 0;
728✔
1621
        m->n_installed_jobs = 0;
728✔
1622
        m->n_failed_jobs = 0;
728✔
1623
}
728✔
1624

1625
Manager* manager_free(Manager *m) {
702✔
1626
        if (!m)
702✔
1627
                return NULL;
1628

1629
        manager_clear_jobs_and_units(m);
702✔
1630

1631
        for (UnitType c = 0; c < _UNIT_TYPE_MAX; c++)
8,424✔
1632
                if (unit_vtable[c]->shutdown)
7,722✔
1633
                        unit_vtable[c]->shutdown(m);
2,808✔
1634

1635
        /* Keep the cgroup hierarchy in place except when we know we are going down for good */
1636
        manager_shutdown_cgroup(m, /* delete= */ IN_SET(m->objective, MANAGER_EXIT, MANAGER_REBOOT, MANAGER_POWEROFF, MANAGER_HALT, MANAGER_KEXEC));
702✔
1637

1638
        lookup_paths_flush_generator(&m->lookup_paths);
702✔
1639

1640
        bus_done(m);
702✔
1641
        manager_varlink_done(m);
702✔
1642

1643
        exec_shared_runtime_vacuum(m);
702✔
1644
        hashmap_free(m->exec_shared_runtime_by_id);
702✔
1645

1646
        dynamic_user_vacuum(m, false);
702✔
1647
        hashmap_free(m->dynamic_users);
702✔
1648

1649
        hashmap_free(m->units);
702✔
1650
        hashmap_free(m->units_by_invocation_id);
702✔
1651
        hashmap_free(m->jobs);
702✔
1652
        hashmap_free(m->watch_pids);
702✔
1653
        hashmap_free(m->watch_pids_more);
702✔
1654
        hashmap_free(m->watch_bus);
702✔
1655

1656
        prioq_free(m->run_queue);
702✔
1657

1658
        set_free(m->startup_units);
702✔
1659
        set_free(m->failed_units);
702✔
1660

1661
        sd_event_source_unref(m->signal_event_source);
702✔
1662
        sd_event_source_unref(m->sigchld_event_source);
702✔
1663
        sd_event_source_unref(m->notify_event_source);
702✔
1664
        sd_event_source_unref(m->time_change_event_source);
702✔
1665
        sd_event_source_unref(m->timezone_change_event_source);
702✔
1666
        sd_event_source_unref(m->jobs_in_progress_event_source);
702✔
1667
        sd_event_source_unref(m->run_queue_event_source);
702✔
1668
        sd_event_source_unref(m->user_lookup_event_source);
702✔
1669
        sd_event_source_unref(m->handoff_timestamp_event_source);
702✔
1670
        sd_event_source_unref(m->pidref_event_source);
702✔
1671
        sd_event_source_unref(m->memory_pressure_event_source);
702✔
1672

1673
        safe_close(m->signal_fd);
702✔
1674
        safe_close(m->notify_fd);
702✔
1675
        safe_close_pair(m->user_lookup_fds);
702✔
1676
        safe_close_pair(m->handoff_timestamp_fds);
702✔
1677
        safe_close_pair(m->pidref_transport_fds);
702✔
1678

1679
        manager_close_ask_password(m);
702✔
1680

1681
        manager_close_idle_pipe(m);
702✔
1682

1683
        sd_event_unref(m->event);
702✔
1684

1685
        free(m->notify_socket);
702✔
1686

1687
        lookup_paths_done(&m->lookup_paths);
702✔
1688
        strv_free(m->transient_environment);
702✔
1689
        strv_free(m->client_environment);
702✔
1690

1691
        hashmap_free(m->cgroup_unit);
702✔
1692
        manager_free_unit_name_maps(m);
702✔
1693

1694
        free(m->switch_root);
702✔
1695
        free(m->switch_root_init);
702✔
1696

1697
        sd_bus_track_unref(m->subscribed);
702✔
1698
        strv_free(m->subscribed_as_strv);
702✔
1699

1700
        unit_defaults_done(&m->defaults);
702✔
1701

1702
        FOREACH_ARRAY(map, m->units_needing_mounts_for, _UNIT_MOUNT_DEPENDENCY_TYPE_MAX) {
2,106✔
1703
                assert(hashmap_isempty(*map));
1,404✔
1704
                hashmap_free(*map);
1,404✔
1705
        }
1706

1707
        hashmap_free(m->uid_refs);
702✔
1708
        hashmap_free(m->gid_refs);
702✔
1709

1710
        FOREACH_ARRAY(i, m->prefix, _EXEC_DIRECTORY_TYPE_MAX)
4,212✔
1711
                free(*i);
3,510✔
1712

1713
        free(m->received_credentials_directory);
702✔
1714
        free(m->received_encrypted_credentials_directory);
702✔
1715

1716
        free(m->watchdog_pretimeout_governor);
702✔
1717
        free(m->watchdog_pretimeout_governor_overridden);
702✔
1718

1719
        fw_ctx_free(m->fw_ctx);
702✔
1720

1721
#if BPF_FRAMEWORK
1722
        bpf_restrict_fs_destroy(m->restrict_fs);
702✔
1723
#endif
1724

1725
        safe_close(m->executor_fd);
702✔
1726
        free(m->executor_path);
702✔
1727

1728
        return mfree(m);
702✔
1729
}
1730

1731
static void manager_enumerate_perpetual(Manager *m) {
726✔
1732
        assert(m);
726✔
1733

1734
        if (FLAGS_SET(m->test_run_flags, MANAGER_TEST_RUN_MINIMAL))
726✔
1735
                return;
1736

1737
        /* Let's ask every type to load all units from disk/kernel that it might know */
1738
        for (UnitType c = 0; c < _UNIT_TYPE_MAX; c++) {
3,288✔
1739
                if (!unit_type_supported(c)) {
3,014✔
1740
                        log_debug("Unit type .%s is not supported on this system.", unit_type_to_string(c));
483✔
1741
                        continue;
483✔
1742
                }
1743

1744
                if (unit_vtable[c]->enumerate_perpetual)
2,531✔
1745
                        unit_vtable[c]->enumerate_perpetual(m);
822✔
1746
        }
1747
}
1748

1749
static void manager_enumerate(Manager *m) {
726✔
1750
        assert(m);
726✔
1751

1752
        if (FLAGS_SET(m->test_run_flags, MANAGER_TEST_RUN_MINIMAL))
726✔
1753
                return;
1754

1755
        /* Let's ask every type to load all units from disk/kernel that it might know */
1756
        for (UnitType c = 0; c < _UNIT_TYPE_MAX; c++) {
3,288✔
1757
                if (!unit_type_supported(c)) {
3,014✔
1758
                        log_debug("Unit type .%s is not supported on this system.", unit_type_to_string(c));
483✔
1759
                        continue;
483✔
1760
                }
1761

1762
                if (unit_vtable[c]->enumerate)
2,531✔
1763
                        unit_vtable[c]->enumerate(m);
500✔
1764
        }
1765

1766
        manager_dispatch_load_queue(m);
274✔
1767
}
1768

1769
static void manager_coldplug(Manager *m) {
726✔
1770
        Unit *u;
726✔
1771
        char *k;
726✔
1772
        int r;
726✔
1773

1774
        assert(m);
726✔
1775

1776
        log_debug("Invoking unit coldplug() handlers%s", glyph(GLYPH_ELLIPSIS));
1,047✔
1777

1778
        /* Let's place the units back into their deserialized state */
1779
        HASHMAP_FOREACH_KEY(u, k, m->units) {
42,075✔
1780

1781
                /* ignore aliases */
1782
                if (u->id != k)
40,623✔
1783
                        continue;
864✔
1784

1785
                r = unit_coldplug(u);
39,759✔
1786
                if (r < 0)
39,759✔
1787
                        log_warning_errno(r, "We couldn't coldplug %s, proceeding anyway: %m", u->id);
41,349✔
1788
        }
1789
}
726✔
1790

1791
static void manager_catchup(Manager *m) {
726✔
1792
        Unit *u;
726✔
1793
        char *k;
726✔
1794

1795
        assert(m);
726✔
1796

1797
        log_debug("Invoking unit catchup() handlers%s", glyph(GLYPH_ELLIPSIS));
1,047✔
1798

1799
        /* Let's catch up on any state changes that happened while we were reloading/reexecing */
1800
        HASHMAP_FOREACH_KEY(u, k, m->units) {
41,349✔
1801

1802
                /* ignore aliases */
1803
                if (u->id != k)
40,623✔
1804
                        continue;
864✔
1805

1806
                unit_catchup(u);
39,759✔
1807
        }
1808
}
726✔
1809

1810
static void manager_distribute_fds(Manager *m, FDSet *fds) {
700✔
1811
        Unit *u;
700✔
1812

1813
        assert(m);
700✔
1814

1815
        HASHMAP_FOREACH(u, m->units) {
11,470✔
1816

1817
                if (fdset_isempty(fds))
10,987✔
1818
                        break;
1819

1820
                if (!UNIT_VTABLE(u)->distribute_fds)
10,770✔
1821
                        continue;
9,679✔
1822

1823
                UNIT_VTABLE(u)->distribute_fds(u, fds);
1,091✔
1824
        }
1825
}
700✔
1826

1827
static bool manager_dbus_is_running(Manager *m, bool deserialized) {
61,386✔
1828
        Unit *u;
61,386✔
1829

1830
        assert(m);
61,386✔
1831

1832
        /* This checks whether the dbus instance we are supposed to expose our APIs on is up. We check both the socket
1833
         * and the service unit. If the 'deserialized' parameter is true we'll check the deserialized state of the unit
1834
         * rather than the current one. */
1835

1836
        if (MANAGER_IS_TEST_RUN(m))
61,386✔
1837
                return false;
1838

1839
        u = manager_get_unit(m, SPECIAL_DBUS_SOCKET);
60,535✔
1840
        if (!u)
60,535✔
1841
                return false;
1842
        if ((deserialized ? SOCKET(u)->deserialized_state : SOCKET(u)->state) != SOCKET_RUNNING)
101,506✔
1843
                return false;
1844

1845
        u = manager_get_unit(m, SPECIAL_DBUS_SERVICE);
4,250✔
1846
        if (!u)
4,250✔
1847
                return false;
1848
        if (!IN_SET((deserialized ? SERVICE(u)->deserialized_state : SERVICE(u)->state),
8,500✔
1849
                    SERVICE_RUNNING,
1850
                    SERVICE_MOUNTING,
1851
                    SERVICE_RELOAD,
1852
                    SERVICE_RELOAD_NOTIFY,
1853
                    SERVICE_RELOAD_SIGNAL))
1854
                return false;
232✔
1855

1856
        return true;
1857
}
1858

1859
static void manager_setup_bus(Manager *m) {
700✔
1860
        assert(m);
700✔
1861

1862
        if (MANAGER_IS_TEST_RUN(m))
700✔
1863
                return;
1864

1865
        /* Let's set up our private bus connection now, unconditionally */
1866
        (void) bus_init_private(m);
236✔
1867

1868
        /* If we are in --user mode also connect to the system bus now */
1869
        if (MANAGER_IS_USER(m))
236✔
1870
                (void) bus_init_system(m);
176✔
1871

1872
        /* Let's connect to the bus now, but only if the unit is supposed to be up */
1873
        if (manager_dbus_is_running(m, MANAGER_IS_RELOADING(m))) {
236✔
1874
                (void) bus_init_api(m);
28✔
1875

1876
                if (MANAGER_IS_SYSTEM(m))
28✔
1877
                        (void) bus_init_system(m);
27✔
1878
        }
1879
}
1880

1881
static void manager_preset_all(Manager *m) {
700✔
1882
        int r;
700✔
1883

1884
        assert(m);
700✔
1885

1886
        if (m->first_boot <= 0)
700✔
1887
                return;
1888

1889
        if (!MANAGER_IS_SYSTEM(m))
17✔
1890
                return;
1891

1892
        if (MANAGER_IS_TEST_RUN(m))
17✔
1893
                return;
1894

1895
        /* If this is the first boot, and we are in the host system, then preset everything */
1896
        UnitFilePresetMode mode =
17✔
1897
                ENABLE_FIRST_BOOT_FULL_PRESET ? UNIT_FILE_PRESET_FULL : UNIT_FILE_PRESET_ENABLE_ONLY;
1898

1899
        r = unit_file_preset_all(RUNTIME_SCOPE_SYSTEM, 0, NULL, mode, NULL, NULL);
17✔
1900
        if (r < 0)
17✔
1901
                log_full_errno(r == -EEXIST ? LOG_NOTICE : LOG_WARNING, r,
×
1902
                               "Failed to populate /etc with preset unit settings, ignoring: %m");
1903
        else
1904
                log_info("Populated /etc with preset unit settings.");
17✔
1905
}
1906

1907
static void manager_ready(Manager *m) {
726✔
1908
        assert(m);
726✔
1909

1910
        /* After having loaded everything, do the final round of catching up with what might have changed */
1911

1912
        m->objective = MANAGER_OK; /* Tell everyone we are up now */
726✔
1913

1914
        /* It might be safe to log to the journal now and connect to dbus */
1915
        manager_recheck_journal(m);
726✔
1916
        manager_recheck_dbus(m);
726✔
1917

1918
        /* Let's finally catch up with any changes that took place while we were reloading/reexecing */
1919
        manager_catchup(m);
726✔
1920

1921
        /* Create a file which will indicate when the manager started loading units the last time. */
1922
        if (MANAGER_IS_SYSTEM(m))
726✔
1923
                (void) touch_file("/run/systemd/systemd-units-load", false,
531✔
1924
                        m->timestamps[MANAGER_TIMESTAMP_UNITS_LOAD].realtime ?: now(CLOCK_REALTIME),
531✔
1925
                        UID_INVALID, GID_INVALID, 0444);
1926
}
726✔
1927

1928
Manager* manager_reloading_start(Manager *m) {
187✔
1929
        m->n_reloading++;
187✔
1930
        dual_timestamp_now(m->timestamps + MANAGER_TIMESTAMP_UNITS_LOAD);
187✔
1931
        return m;
187✔
1932
}
1933

1934
void manager_reloading_stopp(Manager **m) {
856✔
1935
        if (*m) {
856✔
1936
                assert((*m)->n_reloading > 0);
161✔
1937
                (*m)->n_reloading--;
161✔
1938
        }
1939
}
856✔
1940

1941
static int manager_make_runtime_dir(Manager *m) {
700✔
1942
        int r;
700✔
1943

1944
        assert(m);
700✔
1945

1946
        _cleanup_free_ char *d = path_join(m->prefix[EXEC_DIRECTORY_RUNTIME], "systemd");
1,400✔
1947
        if (!d)
700✔
1948
                return log_oom();
×
1949

1950
        r = mkdir_label(d, 0755);
700✔
1951
        if (r < 0 && r != -EEXIST)
700✔
1952
                return log_error_errno(r, "Failed to create directory '%s/': %m", d);
×
1953

1954
        return 0;
1955
}
1956

1957
int manager_startup(Manager *m, FILE *serialization, FDSet *fds, const char *root) {
700✔
1958
        int r;
700✔
1959

1960
        assert(m);
700✔
1961

1962
        r = manager_make_runtime_dir(m);
700✔
1963
        if (r < 0)
700✔
1964
                return r;
1965

1966
        /* If we are running in test mode, we still want to run the generators,
1967
         * but we should not touch the real generator directories. */
1968
        r = lookup_paths_init_or_warn(&m->lookup_paths, m->runtime_scope,
700✔
1969
                                      MANAGER_IS_TEST_RUN(m) ? LOOKUP_PATHS_TEMPORARY_GENERATED : 0,
700✔
1970
                                      root);
1971
        if (r < 0)
700✔
1972
                return r;
1973

1974
        dual_timestamp_now(m->timestamps + manager_timestamp_initrd_mangle(MANAGER_TIMESTAMP_GENERATORS_START));
700✔
1975
        r = manager_run_environment_generators(m);
700✔
1976
        if (r >= 0)
700✔
1977
                r = manager_run_generators(m);
700✔
1978
        dual_timestamp_now(m->timestamps + manager_timestamp_initrd_mangle(MANAGER_TIMESTAMP_GENERATORS_FINISH));
700✔
1979
        if (r < 0)
700✔
1980
                return r;
1981

1982
        manager_preset_all(m);
700✔
1983

1984
        lookup_paths_log(&m->lookup_paths);
700✔
1985

1986
        {
1987
                /* This block is (optionally) done with the reloading counter bumped */
1988
                _unused_ _cleanup_(manager_reloading_stopp) Manager *reloading = NULL;
700✔
1989

1990
                /* Make sure we don't have a left-over from a previous run */
1991
                if (!serialization)
700✔
1992
                        (void) rm_rf(m->lookup_paths.transient, 0);
669✔
1993

1994
                /* If we will deserialize make sure that during enumeration this is already known, so we increase the
1995
                 * counter here already */
1996
                if (serialization)
31✔
1997
                        reloading = manager_reloading_start(m);
31✔
1998

1999
                /* First, enumerate what we can from all config files */
2000
                dual_timestamp_now(m->timestamps + manager_timestamp_initrd_mangle(MANAGER_TIMESTAMP_UNITS_LOAD_START));
700✔
2001
                manager_enumerate_perpetual(m);
700✔
2002
                manager_enumerate(m);
700✔
2003
                dual_timestamp_now(m->timestamps + manager_timestamp_initrd_mangle(MANAGER_TIMESTAMP_UNITS_LOAD_FINISH));
700✔
2004

2005
                /* Second, deserialize if there is something to deserialize */
2006
                if (serialization) {
700✔
2007
                        r = manager_deserialize(m, serialization, fds);
31✔
2008
                        if (r < 0)
31✔
2009
                                return log_error_errno(r, "Deserialization failed: %m");
×
2010
                }
2011

2012
                if (m->previous_objective >= 0) {
700✔
2013
                        if (IN_SET(m->previous_objective, MANAGER_REEXECUTE, MANAGER_SOFT_REBOOT, MANAGER_SWITCH_ROOT))
31✔
2014
                                log_debug("Launching as effect of a '%s' operation.",
31✔
2015
                                          manager_objective_to_string(m->previous_objective));
2016
                        else
2017
                                log_warning("Got unexpected previous objective '%s', ignoring.",
×
2018
                                            manager_objective_to_string(m->previous_objective));
2019
                }
2020

2021
                /* If we are in a new soft-reboot iteration bump the counter now before starting units, so
2022
                 * that they can reliably read it. We get the previous objective from serialized state. */
2023
                if (m->previous_objective == MANAGER_SOFT_REBOOT)
700✔
2024
                        m->soft_reboots_count++;
×
2025

2026
                /* Any fds left? Find some unit which wants them. This is useful to allow container managers to pass
2027
                 * some file descriptors to us pre-initialized. This enables socket-based activation of entire
2028
                 * containers. */
2029
                manager_distribute_fds(m, fds);
700✔
2030

2031
                /* We might have deserialized the notify fd, but if we didn't then let's create it now */
2032
                r = manager_setup_notify(m);
700✔
2033
                if (r < 0)
700✔
2034
                        /* No sense to continue without notifications, our children would fail anyway. */
2035
                        return r;
2036

2037
                r = manager_setup_user_lookup_fd(m);
700✔
2038
                if (r < 0)
700✔
2039
                        /* This shouldn't fail, except if things are really broken. */
2040
                        return r;
2041

2042
                r = manager_setup_handoff_timestamp_fd(m);
700✔
2043
                if (r < 0)
700✔
2044
                        /* This shouldn't fail, except if things are really broken. */
2045
                        return r;
2046

2047
                r = manager_setup_pidref_transport_fd(m);
700✔
2048
                if (r < 0)
700✔
2049
                        /* This shouldn't fail, except if things are really broken. */
2050
                        return r;
2051

2052
                /* Connect to the bus if we are good for it */
2053
                manager_setup_bus(m);
700✔
2054

2055
                r = manager_varlink_init(m);
700✔
2056
                if (r < 0)
700✔
2057
                        log_warning_errno(r, "Failed to set up Varlink, ignoring: %m");
×
2058

2059
                /* Third, fire things up! */
2060
                manager_coldplug(m);
700✔
2061

2062
                /* Clean up runtime objects */
2063
                manager_vacuum(m);
700✔
2064

2065
                if (serialization)
700✔
2066
                        /* Let's wait for the UnitNew/JobNew messages being sent, before we notify that the
2067
                         * reload is finished */
2068
                        m->send_reloading_done = true;
31✔
2069
        }
2070

2071
        manager_ready(m);
700✔
2072

2073
        manager_set_switching_root(m, false);
700✔
2074

2075
        return 0;
700✔
2076
}
2077

2078
int manager_add_job_full(
1,327✔
2079
                Manager *m,
2080
                JobType type,
2081
                Unit *unit,
2082
                JobMode mode,
2083
                TransactionAddFlags extra_flags,
2084
                Set *affected_jobs,
2085
                sd_bus_error *error,
2086
                Job **ret) {
2087

2088
        _cleanup_(transaction_abort_and_freep) Transaction *tr = NULL;
1,327✔
2089
        int r;
1,327✔
2090

2091
        assert(m);
1,327✔
2092
        assert(type >= 0 && type < _JOB_TYPE_MAX);
1,327✔
2093
        assert(unit);
1,327✔
2094
        assert(mode >= 0 && mode < _JOB_MODE_MAX);
1,327✔
2095
        assert((extra_flags & ~_TRANSACTION_FLAGS_MASK_PUBLIC) == 0);
1,327✔
2096

2097
        if (mode == JOB_ISOLATE && type != JOB_START)
1,327✔
2098
                return sd_bus_error_set(error, SD_BUS_ERROR_INVALID_ARGS, "Isolate is only valid for start.");
×
2099

2100
        if (mode == JOB_ISOLATE && !unit->allow_isolate)
1,327✔
2101
                return sd_bus_error_set(error, BUS_ERROR_NO_ISOLATION, "Operation refused, unit may not be isolated.");
24✔
2102

2103
        if (mode == JOB_TRIGGERING && type != JOB_STOP)
1,303✔
2104
                return sd_bus_error_set(error, SD_BUS_ERROR_INVALID_ARGS, "--job-mode=triggering is only valid for stop.");
×
2105

2106
        if (mode == JOB_RESTART_DEPENDENCIES && type != JOB_START)
1,303✔
2107
                return sd_bus_error_set(error, SD_BUS_ERROR_INVALID_ARGS, "--job-mode=restart-dependencies is only valid for start.");
×
2108

2109
        log_unit_debug(unit, "Trying to enqueue job %s/%s/%s", unit->id, job_type_to_string(type), job_mode_to_string(mode));
1,303✔
2110

2111
        type = job_type_collapse(type, unit);
1,303✔
2112

2113
        tr = transaction_new(mode == JOB_REPLACE_IRREVERSIBLY);
1,303✔
2114
        if (!tr)
1,303✔
2115
                return -ENOMEM;
2116

2117
        r = transaction_add_job_and_dependencies(
3,909✔
2118
                        tr,
2119
                        type,
2120
                        unit,
2121
                        /* by= */ NULL,
2122
                        TRANSACTION_MATTERS |
2123
                        (IN_SET(mode, JOB_IGNORE_DEPENDENCIES, JOB_IGNORE_REQUIREMENTS) ? TRANSACTION_IGNORE_REQUIREMENTS : 0) |
1,303✔
2124
                        (mode == JOB_IGNORE_DEPENDENCIES ? TRANSACTION_IGNORE_ORDER : 0) |
1,303✔
2125
                        (mode == JOB_RESTART_DEPENDENCIES ? TRANSACTION_PROPAGATE_START_AS_RESTART : 0) |
2,601✔
2126
                        extra_flags,
2127
                        error);
2128
        if (r < 0)
1,303✔
2129
                return r;
2130

2131
        if (mode == JOB_ISOLATE) {
1,295✔
2132
                r = transaction_add_isolate_jobs(tr, m);
193✔
2133
                if (r < 0)
193✔
2134
                        return r;
2135
        }
2136

2137
        if (mode == JOB_TRIGGERING) {
1,295✔
2138
                r = transaction_add_triggering_jobs(tr, unit);
×
2139
                if (r < 0)
×
2140
                        return r;
2141
        }
2142

2143
        r = transaction_activate(tr, m, mode, affected_jobs, error);
1,295✔
2144
        if (r < 0)
1,295✔
2145
                return r;
2146

2147
        log_unit_debug(unit,
1,288✔
2148
                       "Enqueued job %s/%s as %u", unit->id,
2149
                       job_type_to_string(type), (unsigned) tr->anchor_job->id);
2150

2151
        if (ret)
1,288✔
2152
                *ret = tr->anchor_job;
705✔
2153

2154
        tr = transaction_free(tr);
1,288✔
2155
        return 0;
1,288✔
2156
}
2157

2158
int manager_add_job_by_name(Manager *m, JobType type, const char *name, JobMode mode, Set *affected_jobs, sd_bus_error *e, Job **ret) {
194✔
2159
        Unit *unit = NULL;  /* just to appease gcc, initialization is not really necessary */
194✔
2160
        int r;
194✔
2161

2162
        assert(m);
194✔
2163
        assert(type < _JOB_TYPE_MAX);
194✔
2164
        assert(name);
194✔
2165
        assert(mode < _JOB_MODE_MAX);
194✔
2166

2167
        r = manager_load_unit(m, name, NULL, NULL, &unit);
194✔
2168
        if (r < 0)
194✔
2169
                return r;
194✔
2170
        assert(unit);
194✔
2171

2172
        return manager_add_job_full(m, type, unit, mode, /* extra_flags = */ 0, affected_jobs, e, ret);
194✔
2173
}
2174

2175
int manager_add_job_by_name_and_warn(Manager *m, JobType type, const char *name, JobMode mode, Set *affected_jobs, Job **ret) {
176✔
2176
        _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
176✔
2177
        int r;
176✔
2178

2179
        assert(m);
176✔
2180
        assert(type < _JOB_TYPE_MAX);
176✔
2181
        assert(name);
176✔
2182
        assert(mode < _JOB_MODE_MAX);
176✔
2183

2184
        r = manager_add_job_by_name(m, type, name, mode, affected_jobs, &error, ret);
176✔
2185
        if (r < 0)
176✔
2186
                return log_warning_errno(r, "Failed to enqueue %s job for %s: %s", job_mode_to_string(mode), name, bus_error_message(&error, r));
×
2187

2188
        return r;
2189
}
2190

2191
int manager_propagate_reload(Manager *m, Unit *unit, JobMode mode, sd_bus_error *e) {
15,962✔
2192
        int r;
15,962✔
2193
        _cleanup_(transaction_abort_and_freep) Transaction *tr = NULL;
15,962✔
2194

2195
        assert(m);
15,962✔
2196
        assert(unit);
15,962✔
2197
        assert(mode < _JOB_MODE_MAX);
15,962✔
2198
        assert(mode != JOB_ISOLATE); /* Isolate is only valid for start */
15,962✔
2199

2200
        tr = transaction_new(mode == JOB_REPLACE_IRREVERSIBLY);
15,962✔
2201
        if (!tr)
15,962✔
2202
                return -ENOMEM;
2203

2204
        /* We need an anchor job */
2205
        r = transaction_add_job_and_dependencies(tr, JOB_NOP, unit, NULL, TRANSACTION_IGNORE_REQUIREMENTS|TRANSACTION_IGNORE_ORDER, e);
15,962✔
2206
        if (r < 0)
15,962✔
2207
                return r;
2208

2209
        /* Failure in adding individual dependencies is ignored, so this always succeeds. */
2210
        transaction_add_propagate_reload_jobs(
15,962✔
2211
                        tr,
2212
                        unit,
2213
                        tr->anchor_job,
15,962✔
2214
                        mode == JOB_IGNORE_DEPENDENCIES ? TRANSACTION_IGNORE_ORDER : 0);
2215

2216
        r = transaction_activate(tr, m, mode, NULL, e);
15,962✔
2217
        if (r < 0)
15,962✔
2218
                return r;
2219

2220
        tr = transaction_free(tr);
15,962✔
2221
        return 0;
15,962✔
2222
}
2223

2224
Job *manager_get_job(Manager *m, uint32_t id) {
15,064✔
2225
        assert(m);
15,064✔
2226

2227
        return hashmap_get(m->jobs, UINT32_TO_PTR(id));
15,064✔
2228
}
2229

2230
Unit *manager_get_unit(Manager *m, const char *name) {
1,585,735✔
2231
        assert(m);
1,585,735✔
2232
        assert(name);
1,585,735✔
2233

2234
        return hashmap_get(m->units, name);
1,585,735✔
2235
}
2236

2237
static int manager_dispatch_target_deps_queue(Manager *m) {
215,488✔
2238
        Unit *u;
215,488✔
2239
        int r = 0;
215,488✔
2240

2241
        assert(m);
215,488✔
2242

2243
        while ((u = LIST_POP(target_deps_queue, m->target_deps_queue))) {
272,829✔
2244
                _cleanup_free_ Unit **targets = NULL;
57,341✔
2245
                int n_targets;
57,341✔
2246

2247
                assert(u->in_target_deps_queue);
57,341✔
2248

2249
                u->in_target_deps_queue = false;
57,341✔
2250

2251
                /* Take an "atomic" snapshot of dependencies here, as the call below will likely modify the
2252
                 * dependencies, and we can't have it that hash tables we iterate through are modified while
2253
                 * we are iterating through them. */
2254
                n_targets = unit_get_dependency_array(u, UNIT_ATOM_DEFAULT_TARGET_DEPENDENCIES, &targets);
57,341✔
2255
                if (n_targets < 0)
57,341✔
2256
                        return n_targets;
2257

2258
                FOREACH_ARRAY(i, targets, n_targets) {
114,730✔
2259
                        r = unit_add_default_target_dependency(u, *i);
57,389✔
2260
                        if (r < 0)
57,389✔
2261
                                return r;
2262
                }
2263
        }
2264

2265
        return r;
2266
}
2267

2268
unsigned manager_dispatch_load_queue(Manager *m) {
244,668✔
2269
        Unit *u;
244,668✔
2270
        unsigned n = 0;
244,668✔
2271

2272
        assert(m);
244,668✔
2273

2274
        /* Make sure we are not run recursively */
2275
        if (m->dispatching_load_queue)
244,668✔
2276
                return 0;
2277

2278
        m->dispatching_load_queue = true;
215,488✔
2279

2280
        /* Dispatches the load queue. Takes a unit from the queue and
2281
         * tries to load its data until the queue is empty */
2282

2283
        while ((u = m->load_queue)) {
295,149✔
2284
                assert(u->in_load_queue);
79,661✔
2285

2286
                unit_load(u);
79,661✔
2287
                n++;
79,661✔
2288
        }
2289

2290
        m->dispatching_load_queue = false;
215,488✔
2291

2292
        /* Dispatch the units waiting for their target dependencies to be added now, as all targets that we know about
2293
         * should be loaded and have aliases resolved */
2294
        (void) manager_dispatch_target_deps_queue(m);
215,488✔
2295

2296
        return n;
215,488✔
2297
}
2298

2299
bool manager_unit_cache_should_retry_load(Unit *u) {
372,419✔
2300
        assert(u);
372,419✔
2301

2302
        /* Automatic reloading from disk only applies to units which were not found sometime in the past, and
2303
         * the not-found stub is kept pinned in the unit graph by dependencies. For units that were
2304
         * previously loaded, we don't do automatic reloading, and daemon-reload is necessary to update. */
2305
        if (u->load_state != UNIT_NOT_FOUND)
372,419✔
2306
                return false;
2307

2308
        /* The cache has been updated since the last time we tried to load the unit. There might be new
2309
         * fragment paths to read. */
2310
        if (u->manager->unit_cache_timestamp_hash != u->fragment_not_found_timestamp_hash)
2,615✔
2311
                return true;
2312

2313
        /* The cache needs to be updated because there are modifications on disk. */
2314
        return !lookup_paths_timestamp_hash_same(&u->manager->lookup_paths, u->manager->unit_cache_timestamp_hash, NULL);
2,615✔
2315
}
2316

2317
int manager_load_unit_prepare(
423,637✔
2318
                Manager *m,
2319
                const char *name,
2320
                const char *path,
2321
                sd_bus_error *e,
2322
                Unit **ret) {
2323

2324
        _cleanup_(unit_freep) Unit *cleanup_unit = NULL;
×
2325
        _cleanup_free_ char *nbuf = NULL;
423,637✔
2326
        int r;
423,637✔
2327

2328
        assert(m);
423,637✔
2329
        assert(ret);
423,637✔
2330
        assert(name || path);
423,637✔
2331

2332
        /* This will prepare the unit for loading, but not actually load anything from disk. */
2333

2334
        if (path && !path_is_absolute(path))
423,637✔
2335
                return sd_bus_error_setf(e, SD_BUS_ERROR_INVALID_ARGS, "Path %s is not absolute.", path);
×
2336

2337
        if (!name) {
423,637✔
2338
                r = path_extract_filename(path, &nbuf);
442✔
2339
                if (r < 0)
442✔
2340
                        return r;
2341
                if (r == O_DIRECTORY)
442✔
2342
                        return sd_bus_error_setf(e, SD_BUS_ERROR_INVALID_ARGS, "Path '%s' refers to directory, refusing.", path);
×
2343

2344
                name = nbuf;
442✔
2345
        }
2346

2347
        UnitType t = unit_name_to_type(name);
423,637✔
2348

2349
        if (t == _UNIT_TYPE_INVALID || !unit_name_is_valid(name, UNIT_NAME_PLAIN|UNIT_NAME_INSTANCE)) {
423,637✔
2350
                if (unit_name_is_valid(name, UNIT_NAME_TEMPLATE))
×
2351
                        return sd_bus_error_setf(e, SD_BUS_ERROR_INVALID_ARGS, "Unit name %s is missing the instance name.", name);
×
2352

2353
                return sd_bus_error_setf(e, SD_BUS_ERROR_INVALID_ARGS, "Unit name %s is not valid.", name);
×
2354
        }
2355

2356
        Unit *unit = manager_get_unit(m, name);
423,637✔
2357
        if (unit) {
423,637✔
2358
                /* The time-based cache allows new units to be started without daemon-reload,
2359
                 * but if they are already referenced (because of dependencies or ordering)
2360
                 * then we have to force a load of the fragment. As an optimization, check
2361
                 * first if anything in the usual paths was modified since the last time
2362
                 * the cache was loaded. Also check if the last time an attempt to load the
2363
                 * unit was made was before the most recent cache refresh, so that we know
2364
                 * we need to try again — even if the cache is current, it might have been
2365
                 * updated in a different context before we had a chance to retry loading
2366
                 * this particular unit. */
2367
                if (manager_unit_cache_should_retry_load(unit))
371,590✔
2368
                        unit->load_state = UNIT_STUB;
×
2369
                else {
2370
                        *ret = unit;
371,590✔
2371
                        return 0;  /* The unit was already loaded */
371,590✔
2372
                }
2373
        } else {
2374
                unit = cleanup_unit = unit_new(m, unit_vtable[t]->object_size);
52,047✔
2375
                if (!unit)
52,047✔
2376
                        return -ENOMEM;
2377
        }
2378

2379
        if (path) {
52,047✔
2380
                r = free_and_strdup(&unit->fragment_path, path);
442✔
2381
                if (r < 0)
442✔
2382
                        return r;
2383
        }
2384

2385
        r = unit_add_name(unit, name);
52,047✔
2386
        if (r < 0)
52,047✔
2387
                return r;
2388

2389
        unit_add_to_load_queue(unit);
52,047✔
2390
        unit_add_to_dbus_queue(unit);
52,047✔
2391
        unit_add_to_gc_queue(unit);
52,047✔
2392

2393
        *ret = unit;
52,047✔
2394
        TAKE_PTR(cleanup_unit);
52,047✔
2395

2396
        return 1;  /* The unit was added the load queue */
52,047✔
2397
}
2398

2399
int manager_load_unit(
403,083✔
2400
                Manager *m,
2401
                const char *name,
2402
                const char *path,
2403
                sd_bus_error *e,
2404
                Unit **ret) {
2405
        int r;
403,083✔
2406

2407
        assert(m);
403,083✔
2408
        assert(ret);
403,083✔
2409

2410
        /* This will load the unit config, but not actually start any services or anything. */
2411

2412
        r = manager_load_unit_prepare(m, name, path, e, ret);
403,083✔
2413
        if (r <= 0)
403,083✔
2414
                return r;
2415

2416
        /* Unit was newly loaded */
2417
        manager_dispatch_load_queue(m);
31,528✔
2418
        *ret = unit_follow_merge(*ret);
31,528✔
2419
        return 0;
31,528✔
2420
}
2421

2422
int manager_load_startable_unit_or_warn(
688✔
2423
                Manager *m,
2424
                const char *name,
2425
                const char *path,
2426
                Unit **ret) {
2427

2428
        /* Load a unit, make sure it loaded fully and is not masked. */
2429

2430
        _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
688✔
2431
        Unit *unit;
688✔
2432
        int r;
688✔
2433

2434
        r = manager_load_unit(m, name, path, &error, &unit);
688✔
2435
        if (r < 0)
688✔
2436
                return log_error_errno(r, "Failed to load %s %s: %s",
×
2437
                                       name ? "unit" : "unit file", name ?: path,
2438
                                       bus_error_message(&error, r));
2439

2440
        r = bus_unit_validate_load_state(unit, &error);
688✔
2441
        if (r < 0)
688✔
2442
                return log_error_errno(r, "%s", bus_error_message(&error, r));
9✔
2443

2444
        *ret = unit;
679✔
2445
        return 0;
679✔
2446
}
2447

2448
void manager_clear_jobs(Manager *m) {
432✔
2449
        Job *j;
432✔
2450

2451
        assert(m);
432✔
2452

2453
        while ((j = hashmap_first(m->jobs)))
459✔
2454
                /* No need to recurse. We're cancelling all jobs. */
2455
                job_finish_and_invalidate(j, JOB_CANCELED, false, false);
27✔
2456
}
432✔
2457

2458
void manager_unwatch_pidref(Manager *m, const PidRef *pid) {
2,211✔
2459
        assert(m);
2,211✔
2460

2461
        for (;;) {
×
2462
                Unit *u;
2,211✔
2463

2464
                u = manager_get_unit_by_pidref_watching(m, pid);
2,211✔
2465
                if (!u)
2,211✔
2466
                        break;
2467

2468
                unit_unwatch_pidref(u, pid);
×
2469
        }
2470
}
2,211✔
2471

2472
static int manager_dispatch_run_queue(sd_event_source *source, void *userdata) {
6,135✔
2473
        Manager *m = ASSERT_PTR(userdata);
6,135✔
2474
        Job *j;
6,135✔
2475

2476
        assert(source);
6,135✔
2477

2478
        while ((j = prioq_peek(m->run_queue))) {
57,392✔
2479
                assert(j->installed);
51,257✔
2480
                assert(j->in_run_queue);
51,257✔
2481

2482
                (void) job_run_and_invalidate(j);
51,257✔
2483
        }
2484

2485
        if (m->n_running_jobs > 0)
6,135✔
2486
                manager_watch_jobs_in_progress(m);
3,170✔
2487

2488
        if (m->n_on_console > 0)
6,135✔
2489
                manager_watch_idle_pipe(m);
440✔
2490

2491
        return 1;
6,135✔
2492
}
2493

2494
void manager_trigger_run_queue(Manager *m) {
53,529✔
2495
        int r;
53,529✔
2496

2497
        assert(m);
53,529✔
2498

2499
        r = sd_event_source_set_enabled(
106,893✔
2500
                        m->run_queue_event_source,
2501
                        prioq_isempty(m->run_queue) ? SD_EVENT_OFF : SD_EVENT_ONESHOT);
53,529✔
2502
        if (r < 0)
53,529✔
2503
                log_warning_errno(r, "Failed to enable job run queue event source, ignoring: %m");
×
2504
}
53,529✔
2505

2506
static unsigned manager_dispatch_dbus_queue(Manager *m) {
174,077✔
2507
        unsigned n = 0, budget;
174,077✔
2508
        Unit *u;
174,077✔
2509
        Job *j;
174,077✔
2510

2511
        assert(m);
174,077✔
2512

2513
        /* When we are reloading, let's not wait with generating signals, since we need to exit the manager as quickly
2514
         * as we can. There's no point in throttling generation of signals in that case. */
2515
        if (MANAGER_IS_RELOADING(m) || m->send_reloading_done || m->pending_reload_message)
174,077✔
2516
                budget = UINT_MAX; /* infinite budget in this case */
2517
        else {
2518
                /* Anything to do at all? */
2519
                if (!m->dbus_unit_queue && !m->dbus_job_queue)
174,020✔
2520
                        return 0;
2521

2522
                /* Do we have overly many messages queued at the moment? If so, let's not enqueue more on top, let's
2523
                 * sit this cycle out, and process things in a later cycle when the queues got a bit emptier. */
2524
                if (manager_bus_n_queued_write(m) > MANAGER_BUS_BUSY_THRESHOLD)
8,070✔
2525
                        return 0;
2526

2527
                /* Only process a certain number of units/jobs per event loop iteration. Even if the bus queue wasn't
2528
                 * overly full before this call we shouldn't increase it in size too wildly in one step, and we
2529
                 * shouldn't monopolize CPU time with generating these messages. Note the difference in counting of
2530
                 * this "budget" and the "threshold" above: the "budget" is decreased only once per generated message,
2531
                 * regardless how many buses/direct connections it is enqueued on, while the "threshold" is applied to
2532
                 * each queued instance of bus message, i.e. if the same message is enqueued to five buses/direct
2533
                 * connections it will be counted five times. This difference in counting ("references"
2534
                 * vs. "instances") is primarily a result of the fact that it's easier to implement it this way,
2535
                 * however it also reflects the thinking that the "threshold" should put a limit on used queue memory,
2536
                 * i.e. space, while the "budget" should put a limit on time. Also note that the "threshold" is
2537
                 * currently chosen much higher than the "budget". */
2538
                budget = MANAGER_BUS_MESSAGE_BUDGET;
2539
        }
2540

2541
        while (budget != 0 && (u = m->dbus_unit_queue)) {
32,312✔
2542

2543
                assert(u->in_dbus_queue);
24,185✔
2544

2545
                bus_unit_send_change_signal(u);
24,185✔
2546
                n++;
24,185✔
2547

2548
                if (budget != UINT_MAX)
24,185✔
2549
                        budget--;
11,799✔
2550
        }
2551

2552
        while (budget != 0 && (j = m->dbus_job_queue)) {
40,555✔
2553
                assert(j->in_dbus_queue);
32,428✔
2554

2555
                bus_job_send_change_signal(j);
32,428✔
2556
                n++;
32,428✔
2557

2558
                if (budget != UINT_MAX)
32,428✔
2559
                        budget--;
32,038✔
2560
        }
2561

2562
        if (m->send_reloading_done) {
8,127✔
2563
                m->send_reloading_done = false;
57✔
2564
                bus_manager_send_reloading(m, false);
57✔
2565
                n++;
57✔
2566
        }
2567

2568
        if (m->pending_reload_message) {
8,127✔
2569
                bus_send_pending_reload_message(m);
26✔
2570
                n++;
26✔
2571
        }
2572

2573
        return n;
2574
}
2575

2576
static bool manager_process_barrier_fd(char * const *tags, FDSet *fds) {
3,488✔
2577

2578
        /* nothing else must be sent when using BARRIER=1 */
2579
        if (strv_contains(tags, "BARRIER=1")) {
3,488✔
2580
                if (strv_length(tags) != 1)
12✔
2581
                        log_warning("Extra notification messages sent with BARRIER=1, ignoring everything.");
×
2582
                else if (fdset_size(fds) != 1)
12✔
2583
                        log_warning("Got incorrect number of fds with BARRIER=1, closing them.");
×
2584

2585
                /* Drop the message if BARRIER=1 was found */
2586
                return true;
12✔
2587
        }
2588

2589
        return false;
2590
}
2591

2592
static void manager_invoke_notify_message(
6,817✔
2593
                Manager *m,
2594
                Unit *u,
2595
                PidRef *pidref,
2596
                const struct ucred *ucred,
2597
                char * const *tags,
2598
                FDSet *fds) {
2599

2600
        assert(m);
6,817✔
2601
        assert(u);
6,817✔
2602
        assert(pidref_is_set(pidref));
6,817✔
2603
        assert(ucred);
6,817✔
2604
        assert(pidref->pid == ucred->pid);
6,817✔
2605
        assert(tags);
6,817✔
2606

2607
        if (u->notifygen == m->notifygen) /* Already invoked on this same unit in this same iteration? */
6,817✔
2608
                return;
2609
        u->notifygen = m->notifygen;
3,475✔
2610

2611
        if (UNIT_VTABLE(u)->notify_message)
3,475✔
2612
                UNIT_VTABLE(u)->notify_message(u, pidref, ucred, tags, fds);
3,475✔
2613

2614
        else if (DEBUG_LOGGING) {
×
2615
                _cleanup_free_ char *joined = strv_join(tags, ", ");
×
2616
                char buf[CELLESCAPE_DEFAULT_LENGTH];
×
2617

2618
                log_unit_debug(u, "Got notification message from unexpected unit type, ignoring: %s",
×
2619
                               joined ? cellescape(buf, sizeof(buf), joined) : "(null)");
2620
        }
2621
}
2622

2623
static int manager_get_units_for_pidref(Manager *m, const PidRef *pidref, Unit ***ret_units) {
10,639✔
2624
        /* Determine array of every unit that is interested in the specified process */
2625

2626
        assert(m);
10,639✔
2627
        assert(pidref_is_set(pidref));
10,639✔
2628

2629
        Unit *u1, *u2, **array;
10,639✔
2630
        u1 = manager_get_unit_by_pidref_cgroup(m, pidref);
10,639✔
2631
        u2 = hashmap_get(m->watch_pids, pidref);
10,639✔
2632
        array = hashmap_get(m->watch_pids_more, pidref);
10,639✔
2633

2634
        size_t n = 0;
10,639✔
2635
        if (u1)
10,639✔
2636
                n++;
10,638✔
2637
        if (u2)
10,639✔
2638
                n++;
7,575✔
2639
        if (array)
10,639✔
2640
                for (size_t j = 0; array[j]; j++)
×
2641
                        n++;
×
2642

2643
        assert(n <= INT_MAX); /* Make sure we can reasonably return the counter as "int" */
×
2644

2645
        if (ret_units) {
10,639✔
2646
                _cleanup_free_ Unit **units = NULL;
×
2647

2648
                if (n > 0) {
10,639✔
2649
                        units = new(Unit*, n + 1);
10,638✔
2650
                        if (!units)
10,638✔
2651
                                return -ENOMEM;
×
2652

2653
                        /* We return a dense array, and put the "main" unit first, i.e. unit in whose cgroup
2654
                         * the process currently is. Note that we do not bother with filtering duplicates
2655
                         * here. */
2656

2657
                        size_t i = 0;
10,638✔
2658
                        if (u1)
10,638✔
2659
                                units[i++] = u1;
10,638✔
2660
                        if (u2)
10,638✔
2661
                                units[i++] = u2;
7,575✔
2662
                        if (array)
10,638✔
2663
                                for (size_t j = 0; array[j]; j++)
×
2664
                                        units[i++] = array[j];
×
2665
                        assert(i == n);
10,638✔
2666

2667
                        units[i] = NULL; /* end array in an extra NULL */
10,638✔
2668
                }
2669

2670
                *ret_units = TAKE_PTR(units);
10,639✔
2671
        }
2672

2673
        return (int) n;
10,639✔
2674
}
2675

2676
static int manager_dispatch_notify_fd(sd_event_source *source, int fd, uint32_t revents, void *userdata) {
3,488✔
2677
        Manager *m = ASSERT_PTR(userdata);
3,488✔
2678
        _cleanup_(pidref_done) PidRef pidref = PIDREF_NULL;
3,488✔
2679
        struct ucred ucred;
3,488✔
2680
        _cleanup_(fdset_free_asyncp) FDSet *fds = NULL;
3,488✔
2681
        int r;
3,488✔
2682

2683
        assert(m->notify_fd == fd);
3,488✔
2684

2685
        if (revents != EPOLLIN) {
3,488✔
2686
                log_warning("Got unexpected poll event for notify fd.");
×
2687
                return 0;
×
2688
        }
2689

2690
        _cleanup_strv_free_ char **tags = NULL;
3,488✔
2691
        r = notify_recv_with_fds_strv(m->notify_fd, &tags, &ucred, &pidref, &fds);
3,488✔
2692
        if (r == -EAGAIN)
3,488✔
2693
                return 0;
2694
        if (r < 0)
3,488✔
2695
                /* If this is any other, real error, then stop processing this socket. This of course means
2696
                 * we won't take notification messages anymore, but that's still better than busy looping:
2697
                 * being woken up over and over again, but being unable to actually read the message from the
2698
                 * socket. */
2699
                return r;
2700

2701
        /* Possibly a barrier fd, let's see. */
2702
        if (manager_process_barrier_fd(tags, fds)) {
3,488✔
2703
                log_debug("Received barrier notification message from PID " PID_FMT ".", pidref.pid);
12✔
2704
                return 0;
12✔
2705
        }
2706

2707
        /* Increase the generation counter used for filtering out duplicate unit invocations. */
2708
        m->notifygen++;
3,476✔
2709

2710
        /* Notify every unit that might be interested, which might be multiple. */
2711
        _cleanup_free_ Unit **array = NULL;
3,476✔
2712

2713
        int n_array = manager_get_units_for_pidref(m, &pidref, &array);
3,476✔
2714
        if (n_array < 0) {
3,476✔
2715
                log_warning_errno(n_array, "Failed to determine units for PID " PID_FMT ", ignoring: %m", pidref.pid);
×
2716
                return 0;
×
2717
        }
2718
        if (n_array == 0)
3,476✔
2719
                log_debug("Cannot find unit for notify message of PID "PID_FMT", ignoring.", pidref.pid);
1✔
2720
        else
2721
                /* And now invoke the per-unit callbacks. Note that manager_invoke_notify_message() will handle
2722
                 * duplicate units – making sure we only invoke each unit's handler once. */
2723
                FOREACH_ARRAY(u, array, n_array)
10,292✔
2724
                        manager_invoke_notify_message(m, *u, &pidref, &ucred, tags, fds);
6,817✔
2725

2726
        if (!fdset_isempty(fds))
3,476✔
2727
                log_warning("Got extra auxiliary fds with notification message, closing them.");
×
2728

2729
        return 0;
2730
}
2731

2732
static void manager_invoke_sigchld_event(
7,010✔
2733
                Manager *m,
2734
                Unit *u,
2735
                const siginfo_t *si) {
2736

2737
        assert(m);
7,010✔
2738
        assert(u);
7,010✔
2739
        assert(si);
7,010✔
2740

2741
        /* Already invoked the handler of this unit in this iteration? Then don't process this again */
2742
        if (u->sigchldgen == m->sigchldgen)
7,010✔
2743
                return;
2,035✔
2744
        u->sigchldgen = m->sigchldgen;
4,975✔
2745

2746
        log_unit_debug(u, "Child "PID_FMT" belongs to %s.", si->si_pid, u->id);
4,975✔
2747
        unit_unwatch_pidref(u, &PIDREF_MAKE_FROM_PID(si->si_pid));
4,975✔
2748

2749
        if (UNIT_VTABLE(u)->sigchld_event)
4,975✔
2750
                UNIT_VTABLE(u)->sigchld_event(u, si->si_pid, si->si_code, si->si_status);
4,729✔
2751
}
2752

2753
static int manager_dispatch_sigchld(sd_event_source *source, void *userdata) {
8,634✔
2754
        Manager *m = ASSERT_PTR(userdata);
8,634✔
2755
        siginfo_t si = {};
8,634✔
2756
        int r;
8,634✔
2757

2758
        assert(source);
8,634✔
2759

2760
        /* First we call waitid() for a PID and do not reap the zombie. That way we can still access
2761
         * /proc/$PID for it while it is a zombie. */
2762

2763
        if (waitid(P_ALL, 0, &si, WEXITED|WNOHANG|WNOWAIT) < 0) {
8,634✔
2764

2765
                if (errno != ECHILD)
79✔
2766
                        log_error_errno(errno, "Failed to peek for child with waitid(), ignoring: %m");
×
2767

2768
                goto turn_off;
79✔
2769
        }
2770

2771
        if (si.si_pid <= 0)
8,555✔
2772
                goto turn_off;
3,585✔
2773

2774
        if (SIGINFO_CODE_IS_DEAD(si.si_code)) {
4,970✔
2775
                _cleanup_free_ char *name = NULL;
9,940✔
2776
                (void) pid_get_comm(si.si_pid, &name);
4,970✔
2777

2778
                log_debug("Child "PID_FMT" (%s) died (code=%s, status=%i/%s)",
9,807✔
2779
                          si.si_pid, strna(name),
2780
                          sigchld_code_to_string(si.si_code),
2781
                          si.si_status,
2782
                          strna(si.si_code == CLD_EXITED
2783
                                ? exit_status_to_string(si.si_status, EXIT_STATUS_FULL)
2784
                                : signal_to_string(si.si_status)));
2785

2786
                /* Increase the generation counter used for filtering out duplicate unit invocations */
2787
                m->sigchldgen++;
4,970✔
2788

2789
                /* We look this up by a PidRef that only consists of the PID. After all we couldn't create a
2790
                 * pidfd here any more even if we wanted (since the process just exited). */
2791
                PidRef pidref = PIDREF_MAKE_FROM_PID(si.si_pid);
4,970✔
2792

2793
                /* And now figure out the units this belongs to, there might be multiple... */
2794
                _cleanup_free_ Unit **array = NULL;
4,970✔
2795
                int n_array = manager_get_units_for_pidref(m, &pidref, &array);
4,970✔
2796
                if (n_array < 0)
4,970✔
2797
                        log_warning_errno(n_array, "Failed to get units for process " PID_FMT ", ignoring: %m", si.si_pid);
4,970✔
2798
                else if (n_array == 0)
4,970✔
2799
                        log_debug("Got SIGCHLD for process " PID_FMT " we weren't interested in, ignoring.", si.si_pid);
×
2800
                else {
2801
                        /* We check for an OOM condition, in case we got SIGCHLD before the OOM notification.
2802
                         * We only do this for the cgroup the PID belonged to, which is the f */
2803
                        (void) unit_check_oom(array[0]);
4,970✔
2804

2805
                        /* We check if systemd-oomd performed a kill so that we log and notify appropriately */
2806
                        (void) unit_check_oomd_kill(array[0]);
4,970✔
2807

2808
                        /* Finally, execute them all. Note that the array might contain duplicates, but that's fine,
2809
                         * manager_invoke_sigchld_event() will ensure we only invoke the handlers once for each
2810
                         * iteration. */
2811
                        FOREACH_ARRAY(u, array, n_array)
11,980✔
2812
                                manager_invoke_sigchld_event(m, *u, &si);
7,010✔
2813
                }
2814
        }
2815

2816
        /* And now, we actually reap the zombie. */
2817
        if (waitid(P_PID, si.si_pid, &si, WEXITED) < 0) {
4,970✔
2818
                log_error_errno(errno, "Failed to dequeue child, ignoring: %m");
×
2819
                return 0;
8,634✔
2820
        }
2821

2822
        return 0;
2823

2824
turn_off:
3,664✔
2825
        /* All children processed for now, turn off event source */
2826

2827
        r = sd_event_source_set_enabled(m->sigchld_event_source, SD_EVENT_OFF);
3,664✔
2828
        if (r < 0)
3,664✔
2829
                return log_error_errno(r, "Failed to disable SIGCHLD event source: %m");
×
2830

2831
        return 0;
2832
}
2833

2834
static void manager_start_special(Manager *m, const char *name, JobMode mode) {
175✔
2835
        Job *job;
175✔
2836

2837
        if (manager_add_job_by_name_and_warn(m, JOB_START, name, mode, NULL, &job) < 0)
175✔
2838
                return;
×
2839

2840
        const char *s = unit_status_string(job->unit, NULL);
175✔
2841

2842
        log_info("Activating special unit %s...", s);
175✔
2843

2844
        (void) sd_notifyf(/* unset_environment= */ false,
175✔
2845
                          "STATUS=Activating special unit %s...", s);
2846
        m->status_ready = false;
175✔
2847
}
2848

2849
static void manager_handle_ctrl_alt_del(Manager *m) {
×
2850
        assert(m);
×
2851

2852
        /* If the user presses C-A-D more than 7 times within 2s, we reboot/shutdown immediately,
2853
         * unless it was disabled in system.conf. */
2854

2855
        if (ratelimit_below(&m->ctrl_alt_del_ratelimit) || m->cad_burst_action == EMERGENCY_ACTION_NONE)
×
2856
                manager_start_special(m, SPECIAL_CTRL_ALT_DEL_TARGET, JOB_REPLACE_IRREVERSIBLY);
×
2857
        else
2858
                emergency_action(
×
2859
                                m,
2860
                                m->cad_burst_action,
2861
                                EMERGENCY_ACTION_WARN,
2862
                                /* reboot_arg= */ NULL,
2863
                                /* exit_status= */ -1,
2864
                                "Ctrl-Alt-Del was pressed more than 7 times within 2s");
2865
}
×
2866

2867
static int manager_dispatch_signal_fd(sd_event_source *source, int fd, uint32_t revents, void *userdata) {
3,582✔
2868
        Manager *m = ASSERT_PTR(userdata);
3,582✔
2869
        ssize_t n;
3,582✔
2870
        struct signalfd_siginfo sfsi;
3,582✔
2871
        int r;
3,582✔
2872

2873
        assert(m->signal_fd == fd);
3,582✔
2874

2875
        if (revents != EPOLLIN) {
3,582✔
2876
                log_warning("Got unexpected events from signal file descriptor.");
×
2877
                return 0;
×
2878
        }
2879

2880
        n = read(m->signal_fd, &sfsi, sizeof(sfsi));
3,582✔
2881
        if (n < 0) {
3,582✔
2882
                if (ERRNO_IS_TRANSIENT(errno))
×
2883
                        return 0;
2884

2885
                /* We return an error here, which will kill this handler,
2886
                 * to avoid a busy loop on read error. */
2887
                return log_error_errno(errno, "Reading from signal fd failed: %m");
×
2888
        }
2889
        if (n != sizeof(sfsi)) {
3,582✔
2890
                log_warning("Truncated read from signal fd (%zi bytes), ignoring!", n);
×
2891
                return 0;
×
2892
        }
2893

2894
        log_received_signal(sfsi.ssi_signo == SIGCHLD ||
3,757✔
2895
                            (sfsi.ssi_signo == SIGTERM && MANAGER_IS_USER(m))
175✔
2896
                            ? LOG_DEBUG : LOG_INFO,
2897
                            &sfsi);
2898

2899
        switch (sfsi.ssi_signo) {
3,582✔
2900

2901
        case SIGCHLD:
3,407✔
2902
                r = sd_event_source_set_enabled(m->sigchld_event_source, SD_EVENT_ON);
3,407✔
2903
                if (r < 0)
3,407✔
2904
                        log_warning_errno(r, "Failed to enable SIGCHLD event source, ignoring: %m");
3,582✔
2905

2906
                break;
2907

2908
        case SIGTERM:
175✔
2909
                if (MANAGER_IS_SYSTEM(m)) {
175✔
2910
                        /* This is for compatibility with the original sysvinit */
2911
                        m->objective = MANAGER_REEXECUTE;
×
2912
                        break;
×
2913
                }
2914

2915
                _fallthrough_;
175✔
2916
        case SIGINT:
2917
                if (MANAGER_IS_SYSTEM(m))
175✔
2918
                        manager_handle_ctrl_alt_del(m);
×
2919
                else
2920
                        manager_start_special(m, SPECIAL_EXIT_TARGET, JOB_REPLACE_IRREVERSIBLY);
175✔
2921
                break;
2922

2923
        case SIGWINCH:
×
2924
                /* This is a nop on non-init */
2925
                if (MANAGER_IS_SYSTEM(m))
×
2926
                        manager_start_special(m, SPECIAL_KBREQUEST_TARGET, JOB_REPLACE);
×
2927

2928
                break;
2929

2930
        case SIGPWR:
×
2931
                /* This is a nop on non-init */
2932
                if (MANAGER_IS_SYSTEM(m))
×
2933
                        manager_start_special(m, SPECIAL_SIGPWR_TARGET, JOB_REPLACE);
×
2934

2935
                break;
2936

2937
        case SIGUSR1:
×
2938
                if (manager_dbus_is_running(m, false)) {
×
2939
                        log_info("Trying to reconnect to bus...");
×
2940

2941
                        (void) bus_init_api(m);
×
2942

2943
                        if (MANAGER_IS_SYSTEM(m))
×
2944
                                (void) bus_init_system(m);
×
2945
                } else
2946
                        manager_start_special(m, SPECIAL_DBUS_SERVICE, JOB_REPLACE);
×
2947

2948
                break;
2949

2950
        case SIGUSR2: {
×
2951
                _cleanup_free_ char *dump = NULL;
×
2952

2953
                r = manager_get_dump_string(m, /* patterns= */ NULL, &dump);
×
2954
                if (r < 0) {
×
2955
                        log_warning_errno(r, "Failed to acquire manager dump: %m");
×
2956
                        break;
2957
                }
2958

2959
                log_dump(LOG_INFO, dump);
×
2960
                break;
2961
        }
2962

2963
        case SIGHUP:
×
2964
                m->objective = MANAGER_RELOAD;
×
2965
                break;
×
2966

2967
        default: {
×
2968

2969
                if (MANAGER_IS_SYSTEM(m)) {
×
2970
                        /* Starting SIGRTMIN+0 */
2971
                        static const struct {
×
2972
                                const char *target;
2973
                                JobMode mode;
2974
                        } target_table[] = {
2975
                                [0] = { SPECIAL_DEFAULT_TARGET,     JOB_ISOLATE              },
2976
                                [1] = { SPECIAL_RESCUE_TARGET,      JOB_ISOLATE              },
2977
                                [2] = { SPECIAL_EMERGENCY_TARGET,   JOB_ISOLATE              },
2978
                                [3] = { SPECIAL_HALT_TARGET,        JOB_REPLACE_IRREVERSIBLY },
2979
                                [4] = { SPECIAL_POWEROFF_TARGET,    JOB_REPLACE_IRREVERSIBLY },
2980
                                [5] = { SPECIAL_REBOOT_TARGET,      JOB_REPLACE_IRREVERSIBLY },
2981
                                [6] = { SPECIAL_KEXEC_TARGET,       JOB_REPLACE_IRREVERSIBLY },
2982
                                [7] = { SPECIAL_SOFT_REBOOT_TARGET, JOB_REPLACE_IRREVERSIBLY },
2983
                        };
2984

2985
                        /* Starting SIGRTMIN+13, so that target halt and system halt are 10 apart */
2986
                        static const ManagerObjective objective_table[] = {
×
2987
                                [0] = MANAGER_HALT,
2988
                                [1] = MANAGER_POWEROFF,
2989
                                [2] = MANAGER_REBOOT,
2990
                                [3] = MANAGER_KEXEC,
2991
                                [4] = MANAGER_SOFT_REBOOT,
2992
                        };
2993

2994
                        if ((int) sfsi.ssi_signo >= SIGRTMIN+0 &&
×
2995
                            (int) sfsi.ssi_signo < SIGRTMIN+(int) ELEMENTSOF(target_table)) {
×
2996
                                int idx = (int) sfsi.ssi_signo - SIGRTMIN;
×
2997
                                manager_start_special(m, target_table[idx].target, target_table[idx].mode);
×
2998
                                break;
2999
                        }
3000

3001
                        if ((int) sfsi.ssi_signo >= SIGRTMIN+13 &&
×
3002
                            (int) sfsi.ssi_signo < SIGRTMIN+13+(int) ELEMENTSOF(objective_table)) {
×
3003
                                m->objective = objective_table[sfsi.ssi_signo - SIGRTMIN - 13];
×
3004
                                break;
×
3005
                        }
3006
                }
3007

3008
                switch (sfsi.ssi_signo - SIGRTMIN) {
×
3009

3010
                case 18: {
×
3011
                        bool generic = false;
×
3012

3013
                        if (sfsi.ssi_code != SI_QUEUE)
×
3014
                                generic = true;
3015
                        else {
3016
                                /* Override a few select commands by our own PID1-specific logic */
3017

3018
                                switch (sfsi.ssi_int) {
×
3019

3020
                                case _COMMON_SIGNAL_COMMAND_LOG_LEVEL_BASE..._COMMON_SIGNAL_COMMAND_LOG_LEVEL_END:
×
3021
                                        manager_override_log_level(m, sfsi.ssi_int - _COMMON_SIGNAL_COMMAND_LOG_LEVEL_BASE);
×
3022
                                        break;
3023

3024
                                case COMMON_SIGNAL_COMMAND_CONSOLE:
×
3025
                                        manager_override_log_target(m, LOG_TARGET_CONSOLE);
×
3026
                                        break;
3027

3028
                                case COMMON_SIGNAL_COMMAND_JOURNAL:
×
3029
                                        manager_override_log_target(m, LOG_TARGET_JOURNAL);
×
3030
                                        break;
3031

3032
                                case COMMON_SIGNAL_COMMAND_KMSG:
×
3033
                                        manager_override_log_target(m, LOG_TARGET_KMSG);
×
3034
                                        break;
3035

3036
                                case COMMON_SIGNAL_COMMAND_NULL:
×
3037
                                        manager_override_log_target(m, LOG_TARGET_NULL);
×
3038
                                        break;
3039

3040
                                case MANAGER_SIGNAL_COMMAND_DUMP_JOBS: {
×
3041
                                        _cleanup_free_ char *dump_jobs = NULL;
×
3042

3043
                                        r = manager_get_dump_jobs_string(m, /* patterns= */ NULL, "  ", &dump_jobs);
×
3044
                                        if (r < 0) {
×
3045
                                                log_warning_errno(r, "Failed to acquire manager jobs dump: %m");
×
3046
                                                break;
3047
                                        }
3048

3049
                                        log_dump(LOG_INFO, dump_jobs);
×
3050
                                        break;
3051
                                }
3052

3053
                                default:
3054
                                        generic = true;
3055
                                }
3056
                        }
3057

3058
                        if (generic)
×
3059
                                return sigrtmin18_handler(source, &sfsi, NULL);
×
3060

3061
                        break;
3062
                }
3063

3064
                case 20:
×
3065
                        manager_override_show_status(m, SHOW_STATUS_YES, "signal");
×
3066
                        break;
3067

3068
                case 21:
×
3069
                        manager_override_show_status(m, SHOW_STATUS_NO, "signal");
×
3070
                        break;
3071

3072
                case 22:
×
3073
                        manager_override_log_level(m, LOG_DEBUG);
×
3074
                        break;
3075

3076
                case 23:
×
3077
                        manager_restore_original_log_level(m);
×
3078
                        break;
3079

3080
                case 24:
×
3081
                        if (MANAGER_IS_USER(m)) {
×
3082
                                m->objective = MANAGER_EXIT;
×
3083
                                return 0;
×
3084
                        }
3085

3086
                        /* This is a nop on init */
3087
                        break;
3088

3089
                case 25:
×
3090
                        m->objective = MANAGER_REEXECUTE;
×
3091
                        break;
×
3092

3093
                case 26:
×
3094
                case 29: /* compatibility: used to be mapped to LOG_TARGET_SYSLOG_OR_KMSG */
3095
                        manager_restore_original_log_target(m);
×
3096
                        break;
3097

3098
                case 27:
×
3099
                        manager_override_log_target(m, LOG_TARGET_CONSOLE);
×
3100
                        break;
3101

3102
                case 28:
×
3103
                        manager_override_log_target(m, LOG_TARGET_KMSG);
×
3104
                        break;
3105

3106
                default:
3107
                        log_warning("Got unhandled signal <%s>.", signal_to_string(sfsi.ssi_signo));
×
3108
                }
3109
        }}
3110

3111
        return 0;
3112
}
3113

3114
static int manager_dispatch_time_change_fd(sd_event_source *source, int fd, uint32_t revents, void *userdata) {
3✔
3115
        Manager *m = ASSERT_PTR(userdata);
3✔
3116
        Unit *u;
3✔
3117

3118
        log_struct(LOG_DEBUG,
3✔
3119
                   LOG_MESSAGE_ID(SD_MESSAGE_TIME_CHANGE_STR),
3120
                   LOG_MESSAGE("Time has been changed"));
3121

3122
        /* Restart the watch */
3123
        (void) manager_setup_time_change(m);
3✔
3124

3125
        HASHMAP_FOREACH(u, m->units)
357✔
3126
                if (UNIT_VTABLE(u)->time_change)
351✔
3127
                        UNIT_VTABLE(u)->time_change(u);
3✔
3128

3129
        return 0;
3✔
3130
}
3131

3132
static int manager_dispatch_timezone_change(
19✔
3133
                sd_event_source *source,
3134
                const struct inotify_event *e,
3135
                void *userdata) {
3136

3137
        Manager *m = ASSERT_PTR(userdata);
19✔
3138
        int changed;
19✔
3139
        Unit *u;
19✔
3140

3141
        log_debug("inotify event for /etc/localtime");
19✔
3142

3143
        changed = manager_read_timezone_stat(m);
19✔
3144
        if (changed <= 0)
19✔
3145
                return changed;
19✔
3146

3147
        /* Something changed, restart the watch, to ensure we watch the new /etc/localtime if it changed */
3148
        (void) manager_setup_timezone_change(m);
11✔
3149

3150
        /* Read the new timezone */
3151
        tzset();
11✔
3152

3153
        log_debug("Timezone has been changed (now: %s).", tzname[daylight]);
11✔
3154

3155
        HASHMAP_FOREACH(u, m->units)
1,874✔
3156
                if (UNIT_VTABLE(u)->timezone_change)
1,852✔
3157
                        UNIT_VTABLE(u)->timezone_change(u);
21✔
3158

3159
        return 0;
11✔
3160
}
3161

3162
static int manager_dispatch_idle_pipe_fd(sd_event_source *source, int fd, uint32_t revents, void *userdata) {
11✔
3163
        Manager *m = ASSERT_PTR(userdata);
11✔
3164

3165
        assert(m->idle_pipe[2] == fd);
11✔
3166

3167
        /* There's at least one Type=idle child that just gave up on us waiting for the boot process to
3168
         * complete. Let's now turn off any further console output if there's at least one service that needs
3169
         * console access, so that from now on our own output should not spill into that service's output
3170
         * anymore. After all, we support Type=idle only to beautify console output and it generally is set
3171
         * on services that want to own the console exclusively without our interference. */
3172
        m->no_console_output = m->n_on_console > 0;
11✔
3173

3174
        /* Acknowledge the child's request, and let all other children know too that they shouldn't wait
3175
         * any longer by closing the pipes towards them, which is what they are waiting for. */
3176
        manager_close_idle_pipe(m);
11✔
3177

3178
        return 0;
11✔
3179
}
3180

3181
static int manager_dispatch_jobs_in_progress(sd_event_source *source, usec_t usec, void *userdata) {
402✔
3182
        Manager *m = ASSERT_PTR(userdata);
402✔
3183
        int r;
402✔
3184

3185
        assert(source);
402✔
3186

3187
        manager_print_jobs_in_progress(m);
402✔
3188

3189
        r = sd_event_source_set_time_relative(source, JOBS_IN_PROGRESS_PERIOD_USEC);
402✔
3190
        if (r < 0)
402✔
3191
                return r;
3192

3193
        return sd_event_source_set_enabled(source, SD_EVENT_ONESHOT);
402✔
3194
}
3195

3196
int manager_loop(Manager *m) {
262✔
3197
        RateLimit rl = { .interval = 1*USEC_PER_SEC, .burst = 50000 };
262✔
3198
        int r;
262✔
3199

3200
        assert(m);
262✔
3201
        assert(m->objective == MANAGER_OK); /* Ensure manager_startup() has been called */
262✔
3202

3203
        manager_check_finished(m);
262✔
3204

3205
        /* There might still be some zombies hanging around from before we were exec()'ed. Let's reap them. */
3206
        r = sd_event_source_set_enabled(m->sigchld_event_source, SD_EVENT_ON);
262✔
3207
        if (r < 0)
262✔
3208
                return log_error_errno(r, "Failed to enable SIGCHLD event source: %m");
×
3209

3210
        while (m->objective == MANAGER_OK) {
201,804✔
3211

3212
                if (!ratelimit_below(&rl)) {
201,542✔
3213
                        /* Yay, something is going seriously wrong, pause a little */
3214
                        log_warning("Looping too fast. Throttling execution a little.");
×
3215
                        sleep(1);
×
3216
                }
3217

3218
                (void) watchdog_ping();
201,542✔
3219

3220
                if (manager_dispatch_load_queue(m) > 0)
201,542✔
3221
                        continue;
×
3222

3223
                if (manager_dispatch_gc_job_queue(m) > 0)
201,542✔
3224
                        continue;
7✔
3225

3226
                if (manager_dispatch_gc_unit_queue(m) > 0)
201,535✔
3227
                        continue;
14,443✔
3228

3229
                if (manager_dispatch_cleanup_queue(m) > 0)
187,092✔
3230
                        continue;
8,193✔
3231

3232
                if (manager_dispatch_cgroup_realize_queue(m) > 0)
178,899✔
3233
                        continue;
3,522✔
3234

3235
                if (manager_dispatch_start_when_upheld_queue(m) > 0)
175,377✔
3236
                        continue;
×
3237

3238
                if (manager_dispatch_stop_when_bound_queue(m) > 0)
175,377✔
3239
                        continue;
142✔
3240

3241
                if (manager_dispatch_stop_when_unneeded_queue(m) > 0)
175,235✔
3242
                        continue;
138✔
3243

3244
                if (manager_dispatch_release_resources_queue(m) > 0)
175,097✔
3245
                        continue;
1,020✔
3246

3247
                if (manager_dispatch_dbus_queue(m) > 0)
174,077✔
3248
                        continue;
8,127✔
3249

3250
                /* Sleep for watchdog runtime wait time */
3251
                r = sd_event_run(m->event, watchdog_runtime_wait(/* divisor= */ 2));
165,950✔
3252
                if (r < 0)
165,950✔
3253
                        return log_error_errno(r, "Failed to run event loop: %m");
×
3254
        }
3255

3256
        return m->objective;
3257
}
3258

3259
int manager_load_unit_from_dbus_path(Manager *m, const char *s, sd_bus_error *e, Unit **_u) {
216,601✔
3260
        _cleanup_free_ char *n = NULL;
216,601✔
3261
        sd_id128_t invocation_id;
216,601✔
3262
        Unit *u;
216,601✔
3263
        int r;
216,601✔
3264

3265
        assert(m);
216,601✔
3266
        assert(s);
216,601✔
3267
        assert(_u);
216,601✔
3268

3269
        r = unit_name_from_dbus_path(s, &n);
216,601✔
3270
        if (r < 0)
216,601✔
3271
                return r;
3272

3273
        /* Permit addressing units by invocation ID: if the passed bus path is suffixed by a 128-bit ID then
3274
         * we use it as invocation ID. */
3275
        r = sd_id128_from_string(n, &invocation_id);
216,601✔
3276
        if (r >= 0) {
216,601✔
3277
                u = hashmap_get(m->units_by_invocation_id, &invocation_id);
×
3278
                if (u) {
×
3279
                        *_u = u;
×
3280
                        return 0;
×
3281
                }
3282

3283
                return sd_bus_error_setf(e, BUS_ERROR_NO_UNIT_FOR_INVOCATION_ID,
×
3284
                                         "No unit with the specified invocation ID " SD_ID128_FORMAT_STR " known.",
3285
                                         SD_ID128_FORMAT_VAL(invocation_id));
×
3286
        }
3287

3288
        /* If this didn't work, we check if this is a unit name */
3289
        if (!unit_name_is_valid(n, UNIT_NAME_PLAIN|UNIT_NAME_INSTANCE)) {
216,601✔
3290
                _cleanup_free_ char *nn = NULL;
1✔
3291

3292
                nn = cescape(n);
1✔
3293
                return sd_bus_error_setf(e, SD_BUS_ERROR_INVALID_ARGS,
1✔
3294
                                         "Unit name %s is neither a valid invocation ID nor unit name.", strnull(nn));
3295
        }
3296

3297
        r = manager_load_unit(m, n, NULL, e, &u);
216,600✔
3298
        if (r < 0)
216,600✔
3299
                return r;
3300

3301
        *_u = u;
216,600✔
3302
        return 0;
216,600✔
3303
}
3304

3305
int manager_get_job_from_dbus_path(Manager *m, const char *s, Job **_j) {
617✔
3306
        const char *p;
617✔
3307
        unsigned id;
617✔
3308
        Job *j;
617✔
3309
        int r;
617✔
3310

3311
        assert(m);
617✔
3312
        assert(s);
617✔
3313
        assert(_j);
617✔
3314

3315
        p = startswith(s, "/org/freedesktop/systemd1/job/");
617✔
3316
        if (!p)
617✔
3317
                return -EINVAL;
617✔
3318

3319
        r = safe_atou(p, &id);
617✔
3320
        if (r < 0)
617✔
3321
                return r;
3322

3323
        j = manager_get_job(m, id);
617✔
3324
        if (!j)
617✔
3325
                return -ENOENT;
3326

3327
        *_j = j;
617✔
3328

3329
        return 0;
617✔
3330
}
3331

3332
void manager_send_unit_audit(Manager *m, Unit *u, int type, bool success) {
3,414✔
3333

3334
#if HAVE_AUDIT
3335
        _cleanup_free_ char *p = NULL;
3,414✔
3336
        const char *msg;
3,414✔
3337
        int audit_fd, r;
3,414✔
3338

3339
        assert(m);
3,414✔
3340
        assert(u);
3,414✔
3341

3342
        if (!MANAGER_IS_SYSTEM(m))
3,414✔
3343
                return;
3344

3345
        /* Don't generate audit events if the service was already started and we're just deserializing */
3346
        if (MANAGER_IS_RELOADING(m))
1,828✔
3347
                return;
3348

3349
        audit_fd = get_core_audit_fd();
1,828✔
3350
        if (audit_fd < 0)
1,828✔
3351
                return;
3352

3353
        r = unit_name_to_prefix_and_instance(u->id, &p);
856✔
3354
        if (r < 0) {
856✔
3355
                log_warning_errno(r, "Failed to extract prefix and instance of unit name, ignoring: %m");
×
3356
                return;
×
3357
        }
3358

3359
        msg = strjoina("unit=", p);
4,280✔
3360
        if (audit_log_user_comm_message(audit_fd, type, msg, "systemd", NULL, NULL, NULL, success) < 0) {
856✔
3361
                if (ERRNO_IS_PRIVILEGE(errno)) {
×
3362
                        /* We aren't allowed to send audit messages?  Then let's not retry again. */
3363
                        log_debug_errno(errno, "Failed to send audit message, closing audit socket: %m");
×
3364
                        close_core_audit_fd();
×
3365
                } else
3366
                        log_warning_errno(errno, "Failed to send audit message, ignoring: %m");
856✔
3367
        }
3368
#endif
3369
}
3370

3371
void manager_send_unit_plymouth(Manager *m, Unit *u) {
32,925✔
3372
        _cleanup_free_ char *message = NULL;
32,925✔
3373
        int c, r;
32,925✔
3374

3375
        assert(m);
32,925✔
3376
        assert(u);
32,925✔
3377

3378
        if (!MANAGER_IS_SYSTEM(m))
32,925✔
3379
                return;
3380

3381
        /* Don't generate plymouth events if the service was already started and we're just deserializing */
3382
        if (MANAGER_IS_RELOADING(m))
4,856✔
3383
                return;
3384

3385
        if (detect_container() > 0)
4,856✔
3386
                return;
3387

3388
        if (!UNIT_VTABLE(u)->notify_plymouth)
2,673✔
3389
                return;
3390

3391
        c = asprintf(&message, "U\x02%c%s%c", (int) (strlen(u->id) + 1), u->id, '\x00');
590✔
3392
        if (c < 0)
590✔
3393
                return (void) log_oom();
×
3394

3395
        /* We set SOCK_NONBLOCK here so that we rather drop the message then wait for plymouth */
3396
        r = plymouth_send_raw(message, c, SOCK_NONBLOCK);
590✔
3397
        if (r < 0)
590✔
3398
                log_full_errno(ERRNO_IS_NO_PLYMOUTH(r) ? LOG_DEBUG : LOG_WARNING, r,
590✔
3399
                               "Failed to communicate with plymouth: %m");
3400
}
3401

3402
void manager_send_unit_supervisor(Manager *m, Unit *u, bool active) {
52,992✔
3403
        assert(m);
52,992✔
3404
        assert(u);
52,992✔
3405

3406
        /* Notify a "supervisor" process about our progress, i.e. a container manager, hypervisor, or
3407
         * surrounding service manager. */
3408

3409
        if (MANAGER_IS_RELOADING(m))
52,992✔
3410
                return;
3411

3412
        if (!UNIT_VTABLE(u)->notify_supervisor)
52,992✔
3413
                return;
3414

3415
        if (in_initrd()) /* Only send these once we left the initrd */
3,942✔
3416
                return;
3417

3418
        (void) sd_notifyf(/* unset_environment= */ false,
4,893✔
3419
                          active ? "X_SYSTEMD_UNIT_ACTIVE=%s" : "X_SYSTEMD_UNIT_INACTIVE=%s",
3420
                          u->id);
3421
}
3422

3423
usec_t manager_get_watchdog(Manager *m, WatchdogType t) {
544✔
3424
        assert(m);
544✔
3425

3426
        if (MANAGER_IS_USER(m))
544✔
3427
                return USEC_INFINITY;
3428

3429
        if (m->watchdog_overridden[t] != USEC_INFINITY)
188✔
3430
                return m->watchdog_overridden[t];
3431

3432
        return m->watchdog[t];
188✔
3433
}
3434

3435
void manager_set_watchdog(Manager *m, WatchdogType t, usec_t timeout) {
1,048✔
3436

3437
        assert(m);
1,048✔
3438

3439
        if (MANAGER_IS_USER(m))
1,048✔
3440
                return;
3441

3442
        if (m->watchdog_overridden[t] == USEC_INFINITY) {
336✔
3443
                if (t == WATCHDOG_RUNTIME)
336✔
3444
                        (void) watchdog_setup(timeout);
84✔
3445
                else if (t == WATCHDOG_PRETIMEOUT)
252✔
3446
                        (void) watchdog_setup_pretimeout(timeout);
84✔
3447
        }
3448

3449
        m->watchdog[t] = timeout;
336✔
3450
}
3451

3452
void manager_override_watchdog(Manager *m, WatchdogType t, usec_t timeout) {
×
3453
        usec_t usec;
×
3454

3455
        assert(m);
×
3456

3457
        if (MANAGER_IS_USER(m))
×
3458
                return;
3459

3460
        usec = timeout == USEC_INFINITY ? m->watchdog[t] : timeout;
×
3461
        if (t == WATCHDOG_RUNTIME)
×
3462
                (void) watchdog_setup(usec);
×
3463
        else if (t == WATCHDOG_PRETIMEOUT)
×
3464
                (void) watchdog_setup_pretimeout(usec);
×
3465

3466
        m->watchdog_overridden[t] = timeout;
×
3467
}
3468

3469
int manager_set_watchdog_pretimeout_governor(Manager *m, const char *governor) {
262✔
3470
        _cleanup_free_ char *p = NULL;
262✔
3471
        int r;
262✔
3472

3473
        assert(m);
262✔
3474

3475
        if (MANAGER_IS_USER(m))
262✔
3476
                return 0;
3477

3478
        if (streq_ptr(m->watchdog_pretimeout_governor, governor))
84✔
3479
                return 0;
3480

3481
        p = strdup(governor);
×
3482
        if (!p)
×
3483
                return -ENOMEM;
3484

3485
        r = watchdog_setup_pretimeout_governor(governor);
×
3486
        if (r < 0)
×
3487
                return r;
3488

3489
        return free_and_replace(m->watchdog_pretimeout_governor, p);
×
3490
}
3491

3492
int manager_override_watchdog_pretimeout_governor(Manager *m, const char *governor) {
×
3493
        _cleanup_free_ char *p = NULL;
×
3494
        int r;
×
3495

3496
        assert(m);
×
3497

3498
        if (MANAGER_IS_USER(m))
×
3499
                return 0;
3500

3501
        if (streq_ptr(m->watchdog_pretimeout_governor_overridden, governor))
×
3502
                return 0;
3503

3504
        p = strdup(governor);
×
3505
        if (!p)
×
3506
                return -ENOMEM;
3507

3508
        r = watchdog_setup_pretimeout_governor(governor);
×
3509
        if (r < 0)
×
3510
                return r;
3511

3512
        return free_and_replace(m->watchdog_pretimeout_governor_overridden, p);
×
3513
}
3514

3515
int manager_reload(Manager *m) {
26✔
3516
        _unused_ _cleanup_(manager_reloading_stopp) Manager *reloading = NULL;
26✔
3517
        _cleanup_fdset_free_ FDSet *fds = NULL;
×
3518
        _cleanup_fclose_ FILE *f = NULL;
26✔
3519
        int r;
26✔
3520

3521
        assert(m);
26✔
3522

3523
        r = manager_open_serialization(m, &f);
26✔
3524
        if (r < 0)
26✔
3525
                return log_error_errno(r, "Failed to create serialization file: %m");
×
3526

3527
        fds = fdset_new();
26✔
3528
        if (!fds)
26✔
3529
                return log_oom();
×
3530

3531
        /* We are officially in reload mode from here on. */
3532
        reloading = manager_reloading_start(m);
26✔
3533

3534
        r = manager_serialize(m, f, fds, false);
26✔
3535
        if (r < 0)
26✔
3536
                return r;
3537

3538
        r = finish_serialization_file(f);
26✔
3539
        if (r < 0)
26✔
3540
                return log_error_errno(r, "Failed to finish serialization: %m");
×
3541

3542
        /* 💀 This is the point of no return, from here on there is no way back. 💀 */
3543
        reloading = NULL;
26✔
3544

3545
        bus_manager_send_reloading(m, true);
26✔
3546

3547
        /* Start by flushing out all jobs and units, all generated units, all runtime environments, all dynamic users
3548
         * and everything else that is worth flushing out. We'll get it all back from the serialization — if we need
3549
         * it. */
3550

3551
        manager_clear_jobs_and_units(m);
26✔
3552
        lookup_paths_flush_generator(&m->lookup_paths);
26✔
3553
        exec_shared_runtime_vacuum(m);
26✔
3554
        dynamic_user_vacuum(m, false);
26✔
3555
        m->uid_refs = hashmap_free(m->uid_refs);
26✔
3556
        m->gid_refs = hashmap_free(m->gid_refs);
26✔
3557

3558
        (void) manager_run_environment_generators(m);
26✔
3559
        (void) manager_run_generators(m);
26✔
3560

3561
        /* We flushed out generated files, for which we don't watch mtime, so we should flush the old map. */
3562
        manager_free_unit_name_maps(m);
26✔
3563
        m->unit_file_state_outdated = false;
26✔
3564

3565
        /* First, enumerate what we can from kernel and suchlike */
3566
        manager_enumerate_perpetual(m);
26✔
3567
        manager_enumerate(m);
26✔
3568

3569
        /* Second, deserialize our stored data */
3570
        r = manager_deserialize(m, f, fds);
26✔
3571
        if (r < 0)
26✔
3572
                log_warning_errno(r, "Deserialization failed, proceeding anyway: %m");
×
3573

3574
        /* We don't need the serialization anymore */
3575
        f = safe_fclose(f);
26✔
3576

3577
        /* Re-register notify_fd as event source, and set up other sockets/communication channels we might need */
3578
        (void) manager_setup_notify(m);
26✔
3579
        (void) manager_setup_user_lookup_fd(m);
26✔
3580
        (void) manager_setup_handoff_timestamp_fd(m);
26✔
3581
        (void) manager_setup_pidref_transport_fd(m);
26✔
3582

3583
        /* Clean up deserialized bus track information. They're never consumed during reload (as opposed to
3584
         * reexec) since we do not disconnect from the bus. */
3585
        m->subscribed_as_strv = strv_free(m->subscribed_as_strv);
26✔
3586
        m->deserialized_bus_id = SD_ID128_NULL;
26✔
3587

3588
        /* Third, fire things up! */
3589
        manager_coldplug(m);
26✔
3590

3591
        /* Clean up runtime objects no longer referenced */
3592
        manager_vacuum(m);
26✔
3593

3594
        /* Consider the reload process complete now. */
3595
        assert(m->n_reloading > 0);
26✔
3596
        m->n_reloading--;
26✔
3597

3598
        manager_ready(m);
26✔
3599

3600
        m->send_reloading_done = true;
26✔
3601
        return 0;
26✔
3602
}
3603

3604
void manager_reset_failed(Manager *m) {
1✔
3605
        Unit *u;
1✔
3606

3607
        assert(m);
1✔
3608

3609
        HASHMAP_FOREACH(u, m->units)
248✔
3610
                unit_reset_failed(u);
247✔
3611
}
1✔
3612

3613
bool manager_unit_inactive_or_pending(Manager *m, const char *name) {
×
3614
        Unit *u;
×
3615

3616
        assert(m);
×
3617
        assert(name);
×
3618

3619
        /* Returns true if the unit is inactive or going down */
3620
        u = manager_get_unit(m, name);
×
3621
        if (!u)
×
3622
                return true;
3623

3624
        return unit_inactive_or_pending(u);
×
3625
}
3626

3627
static void log_taint_string(Manager *m) {
18,552✔
3628
        assert(m);
18,552✔
3629

3630
        if (MANAGER_IS_USER(m) || m->taint_logged)
18,552✔
3631
                return;
18,552✔
3632

3633
        m->taint_logged = true; /* only check for taint once */
30✔
3634

3635
        _cleanup_free_ char *taint = taint_string();
60✔
3636
        if (isempty(taint))
30✔
3637
                return;
30✔
3638

3639
        log_struct(LOG_NOTICE,
×
3640
                   LOG_MESSAGE("System is tainted: %s", taint),
3641
                   LOG_ITEM("TAINT=%s", taint),
3642
                   LOG_MESSAGE_ID(SD_MESSAGE_TAINTED_STR));
3643
}
3644

3645
static void manager_notify_finished(Manager *m) {
180✔
3646
        usec_t firmware_usec, loader_usec, kernel_usec, initrd_usec, userspace_usec, total_usec;
180✔
3647

3648
        if (MANAGER_IS_TEST_RUN(m))
180✔
3649
                return;
3650

3651
        if (MANAGER_IS_SYSTEM(m) && m->soft_reboots_count > 0) {
173✔
3652
                /* The soft-reboot case, where we only report data for the last reboot */
3653
                firmware_usec = loader_usec = initrd_usec = kernel_usec = 0;
×
3654
                total_usec = userspace_usec = usec_sub_unsigned(m->timestamps[MANAGER_TIMESTAMP_FINISH].monotonic,
×
3655
                                                                m->timestamps[MANAGER_TIMESTAMP_SHUTDOWN_START].monotonic);
3656

3657
                log_struct(LOG_INFO,
×
3658
                           LOG_MESSAGE_ID(SD_MESSAGE_STARTUP_FINISHED_STR),
3659
                           LOG_ITEM("USERSPACE_USEC="USEC_FMT, userspace_usec),
3660
                           LOG_MESSAGE("Soft-reboot finished in %s, counter is now at %u.",
3661
                                       FORMAT_TIMESPAN(total_usec, USEC_PER_MSEC),
3662
                                       m->soft_reboots_count));
3663
        } else if (MANAGER_IS_SYSTEM(m) && detect_container() <= 0) {
173✔
3664
                char buf[FORMAT_TIMESPAN_MAX + STRLEN(" (firmware) + ") + FORMAT_TIMESPAN_MAX + STRLEN(" (loader) + ")]
×
3665
                        = {};
3666
                char *p = buf;
×
3667
                size_t size = sizeof buf;
×
3668

3669
                /* Note that MANAGER_TIMESTAMP_KERNEL's monotonic value is always at 0, and
3670
                 * MANAGER_TIMESTAMP_FIRMWARE's and MANAGER_TIMESTAMP_LOADER's monotonic value should be considered
3671
                 * negative values. */
3672

3673
                firmware_usec = m->timestamps[MANAGER_TIMESTAMP_FIRMWARE].monotonic - m->timestamps[MANAGER_TIMESTAMP_LOADER].monotonic;
×
3674
                loader_usec = m->timestamps[MANAGER_TIMESTAMP_LOADER].monotonic - m->timestamps[MANAGER_TIMESTAMP_KERNEL].monotonic;
×
3675
                userspace_usec = m->timestamps[MANAGER_TIMESTAMP_FINISH].monotonic - m->timestamps[MANAGER_TIMESTAMP_USERSPACE].monotonic;
×
3676
                total_usec = m->timestamps[MANAGER_TIMESTAMP_FIRMWARE].monotonic + m->timestamps[MANAGER_TIMESTAMP_FINISH].monotonic;
×
3677

3678
                if (firmware_usec > 0)
×
3679
                        size = strpcpyf(&p, size, "%s (firmware) + ", FORMAT_TIMESPAN(firmware_usec, USEC_PER_MSEC));
×
3680
                if (loader_usec > 0)
×
3681
                        size = strpcpyf(&p, size, "%s (loader) + ", FORMAT_TIMESPAN(loader_usec, USEC_PER_MSEC));
×
3682

3683
                if (dual_timestamp_is_set(&m->timestamps[MANAGER_TIMESTAMP_INITRD])) {
×
3684

3685
                        /* The initrd case on bare-metal */
3686
                        kernel_usec = m->timestamps[MANAGER_TIMESTAMP_INITRD].monotonic - m->timestamps[MANAGER_TIMESTAMP_KERNEL].monotonic;
×
3687
                        initrd_usec = m->timestamps[MANAGER_TIMESTAMP_USERSPACE].monotonic - m->timestamps[MANAGER_TIMESTAMP_INITRD].monotonic;
×
3688

3689
                        log_struct(LOG_INFO,
×
3690
                                   LOG_MESSAGE_ID(SD_MESSAGE_STARTUP_FINISHED_STR),
3691
                                   LOG_ITEM("KERNEL_USEC="USEC_FMT, kernel_usec),
3692
                                   LOG_ITEM("INITRD_USEC="USEC_FMT, initrd_usec),
3693
                                   LOG_ITEM("USERSPACE_USEC="USEC_FMT, userspace_usec),
3694
                                   LOG_MESSAGE("Startup finished in %s%s (kernel) + %s (initrd) + %s (userspace) = %s.",
3695
                                               buf,
3696
                                               FORMAT_TIMESPAN(kernel_usec, USEC_PER_MSEC),
3697
                                               FORMAT_TIMESPAN(initrd_usec, USEC_PER_MSEC),
3698
                                               FORMAT_TIMESPAN(userspace_usec, USEC_PER_MSEC),
3699
                                               FORMAT_TIMESPAN(total_usec, USEC_PER_MSEC)));
3700
                } else {
3701
                        /* The initrd-less case on bare-metal */
3702

3703
                        kernel_usec = m->timestamps[MANAGER_TIMESTAMP_USERSPACE].monotonic - m->timestamps[MANAGER_TIMESTAMP_KERNEL].monotonic;
×
3704
                        initrd_usec = 0;
×
3705

3706
                        log_struct(LOG_INFO,
×
3707
                                   LOG_MESSAGE_ID(SD_MESSAGE_STARTUP_FINISHED_STR),
3708
                                   LOG_ITEM("KERNEL_USEC="USEC_FMT, kernel_usec),
3709
                                   LOG_ITEM("USERSPACE_USEC="USEC_FMT, userspace_usec),
3710
                                   LOG_MESSAGE("Startup finished in %s%s (kernel) + %s (userspace) = %s.",
3711
                                               buf,
3712
                                               FORMAT_TIMESPAN(kernel_usec, USEC_PER_MSEC),
3713
                                               FORMAT_TIMESPAN(userspace_usec, USEC_PER_MSEC),
3714
                                               FORMAT_TIMESPAN(total_usec, USEC_PER_MSEC)));
3715
                }
3716
        } else {
3717
                /* The container and --user case */
3718
                firmware_usec = loader_usec = initrd_usec = kernel_usec = 0;
173✔
3719
                total_usec = userspace_usec = m->timestamps[MANAGER_TIMESTAMP_FINISH].monotonic - m->timestamps[MANAGER_TIMESTAMP_USERSPACE].monotonic;
173✔
3720

3721
                log_struct(LOG_INFO,
173✔
3722
                           LOG_MESSAGE_ID(SD_MESSAGE_USER_STARTUP_FINISHED_STR),
3723
                           LOG_ITEM("USERSPACE_USEC="USEC_FMT, userspace_usec),
3724
                           LOG_MESSAGE("Startup finished in %s.",
3725
                                       FORMAT_TIMESPAN(total_usec, USEC_PER_MSEC)));
3726
        }
3727

3728
        bus_manager_send_finished(m, firmware_usec, loader_usec, kernel_usec, initrd_usec, userspace_usec, total_usec);
173✔
3729

3730
        if (MANAGER_IS_SYSTEM(m) && detect_container() <= 0)
173✔
3731
                watchdog_report_if_missing();
×
3732

3733
        log_taint_string(m);
173✔
3734
}
3735

3736
static void manager_send_ready_on_basic_target(Manager *m) {
18,379✔
3737
        int r;
18,379✔
3738

3739
        assert(m);
18,379✔
3740

3741
        /* We send READY=1 on reaching basic.target only when running in --user mode. */
3742
        if (!MANAGER_IS_USER(m) || m->ready_sent)
18,379✔
3743
                return;
3744

3745
        r = sd_notify(/* unset_environment= */ false,
182✔
3746
                      "READY=1\n"
3747
                      "STATUS=Reached " SPECIAL_BASIC_TARGET ".");
3748
        if (r < 0)
182✔
3749
                log_warning_errno(r, "Failed to send readiness notification, ignoring: %m");
×
3750

3751
        m->ready_sent = true;
182✔
3752
        m->status_ready = false;
182✔
3753
}
3754

3755
static void manager_send_ready_on_idle(Manager *m) {
2,647✔
3756
        int r;
2,647✔
3757

3758
        assert(m);
2,647✔
3759

3760
        /* Skip the notification if nothing changed. */
3761
        if (m->ready_sent && m->status_ready)
2,647✔
3762
                return;
3763

3764
        /* Note that for user managers, we might have already sent READY=1 in manager_send_ready_user_scope().
3765
         * But we still need to flush STATUS=. The second READY=1 will be treated as a noop so it doesn't
3766
         * hurt to send it twice. */
3767
        r = sd_notify(/* unset_environment= */ false,
183✔
3768
                      "READY=1\n"
3769
                      "STATUS=Ready.");
3770
        if (r < 0)
183✔
3771
                log_full_errno(m->ready_sent ? LOG_DEBUG : LOG_WARNING, r,
×
3772
                               "Failed to send readiness notification, ignoring: %m");
3773

3774
        m->ready_sent = m->status_ready = true;
183✔
3775
}
3776

3777
static void manager_check_basic_target(Manager *m) {
30,095✔
3778
        Unit *u;
30,095✔
3779

3780
        assert(m);
30,095✔
3781

3782
        /* Small shortcut */
3783
        if (m->ready_sent && m->taint_logged)
30,095✔
3784
                return;
3785

3786
        u = manager_get_unit(m, SPECIAL_BASIC_TARGET);
30,095✔
3787
        if (!u || !UNIT_IS_ACTIVE_OR_RELOADING(unit_active_state(u)))
30,095✔
3788
                return;
11,716✔
3789

3790
        /* For user managers, send out READY=1 as soon as we reach basic.target */
3791
        manager_send_ready_on_basic_target(m);
18,379✔
3792

3793
        /* Log the taint string as soon as we reach basic.target */
3794
        log_taint_string(m);
18,379✔
3795
}
3796

3797
void manager_check_finished(Manager *m) {
30,284✔
3798
        assert(m);
30,284✔
3799

3800
        if (MANAGER_IS_RELOADING(m))
30,284✔
3801
                return;
3802

3803
        /* Verify that we have entered the event loop already, and not left it again. */
3804
        if (!MANAGER_IS_RUNNING(m))
30,284✔
3805
                return;
3806

3807
        manager_check_basic_target(m);
30,095✔
3808

3809
        if (!hashmap_isempty(m->jobs)) {
30,095✔
3810
                if (m->jobs_in_progress_event_source)
27,448✔
3811
                        /* Ignore any failure, this is only for feedback */
3812
                        (void) sd_event_source_set_time(m->jobs_in_progress_event_source,
3,644✔
3813
                                                        manager_watch_jobs_next_time(m));
3,644✔
3814
                return;
27,448✔
3815
        }
3816

3817
        /* The jobs hashmap tends to grow a lot during boot, and then it's not reused until shutdown. Let's
3818
           kill the hashmap if it is relatively large. */
3819
        if (hashmap_buckets(m->jobs) > hashmap_size(m->units) / 10)
2,647✔
3820
                m->jobs = hashmap_free(m->jobs);
555✔
3821

3822
        manager_send_ready_on_idle(m);
2,647✔
3823

3824
        /* Notify Type=idle units that we are done now */
3825
        manager_close_idle_pipe(m);
2,647✔
3826

3827
        if (MANAGER_IS_FINISHED(m))
2,647✔
3828
                return;
3829

3830
        manager_flip_auto_status(m, false, "boot finished");
180✔
3831

3832
        /* Turn off confirm spawn now */
3833
        m->confirm_spawn = NULL;
180✔
3834

3835
        /* No need to update ask password status when we're going non-interactive */
3836
        manager_close_ask_password(m);
180✔
3837

3838
        /* This is no longer the first boot */
3839
        manager_set_first_boot(m, false);
180✔
3840

3841
        dual_timestamp_now(m->timestamps + MANAGER_TIMESTAMP_FINISH);
180✔
3842

3843
        manager_notify_finished(m);
180✔
3844

3845
        manager_invalidate_startup_units(m);
180✔
3846
}
3847

3848
void manager_send_reloading(Manager *m) {
73✔
3849
        assert(m);
73✔
3850

3851
        /* Let whoever invoked us know that we are now reloading */
3852
        (void) notify_reloading_full(/* status = */ NULL);
73✔
3853

3854
        /* And ensure that we'll send READY=1 again as soon as we are ready again */
3855
        m->ready_sent = false;
73✔
3856
}
73✔
3857

3858
static bool generator_path_any(char * const *paths) {
965✔
3859

3860
        /* Optimize by skipping the whole process by not creating output directories if no generators are found. */
3861

3862
        STRV_FOREACH(i, paths) {
3,860✔
3863
                if (access(*i, F_OK) >= 0)
3,860✔
3864
                        return true;
3865
                if (errno != ENOENT)
2,895✔
3866
                        log_warning_errno(errno, "Failed to check if generator dir '%s' exists, assuming not: %m", *i);
2,895✔
3867
        }
3868

3869
        return false;
3870
}
3871

3872
static int manager_run_environment_generators(Manager *m) {
726✔
3873
        _cleanup_strv_free_ char **paths = NULL;
726✔
3874
        int r;
726✔
3875

3876
        assert(m);
726✔
3877

3878
        if (MANAGER_IS_TEST_RUN(m) && !(m->test_run_flags & MANAGER_TEST_RUN_ENV_GENERATORS))
726✔
3879
                return 0;
3880

3881
        paths = env_generator_binary_paths(m->runtime_scope);
703✔
3882
        if (!paths)
703✔
3883
                return log_oom();
×
3884

3885
        if (!generator_path_any(paths))
703✔
3886
                return 0;
3887

3888
        char **tmp = NULL; /* this is only used in the forked process, no cleanup here */
703✔
3889
        void *args[_STDOUT_CONSUME_MAX] = {
703✔
3890
                [STDOUT_GENERATE] = &tmp,
3891
                [STDOUT_COLLECT]  = &tmp,
3892
                [STDOUT_CONSUME]  = &m->transient_environment,
703✔
3893
        };
3894

3895
        WITH_UMASK(0022)
1,406✔
3896
                r = execute_directories((const char* const*) paths, DEFAULT_TIMEOUT_USEC, gather_environment,
703✔
3897
                                        args, NULL, m->transient_environment,
3898
                                        EXEC_DIR_PARALLEL | EXEC_DIR_IGNORE_ERRORS | EXEC_DIR_SET_SYSTEMD_EXEC_PID);
3899
        return r;
703✔
3900
}
3901

3902
static int build_generator_environment(Manager *m, char ***ret) {
178✔
3903
        _cleanup_strv_free_ char **nl = NULL;
178✔
3904
        Virtualization v;
178✔
3905
        ConfidentialVirtualization cv;
178✔
3906
        int r;
178✔
3907

3908
        assert(m);
178✔
3909
        assert(ret);
178✔
3910

3911
        /* Generators oftentimes want to know some basic facts about the environment they run in, in order to
3912
         * adjust generated units to that. Let's pass down some bits of information that are easy for us to
3913
         * determine (but a bit harder for generator scripts to determine), as environment variables. */
3914

3915
        nl = strv_copy(m->transient_environment);
178✔
3916
        if (!nl)
178✔
3917
                return -ENOMEM;
3918

3919
        r = strv_env_assign(&nl, "SYSTEMD_SCOPE", runtime_scope_to_string(m->runtime_scope));
178✔
3920
        if (r < 0)
178✔
3921
                return r;
3922

3923
        if (MANAGER_IS_SYSTEM(m)) {
178✔
3924
                /* Note that $SYSTEMD_IN_INITRD may be used to override the initrd detection in much of our
3925
                 * codebase. This is hence more than purely informational. It will shortcut detection of the
3926
                 * initrd state if generators invoke our own tools. But that's OK, as it would come to the
3927
                 * same results (hopefully). */
3928
                r = strv_env_assign(&nl, "SYSTEMD_IN_INITRD", one_zero(in_initrd()));
×
3929
                if (r < 0)
×
3930
                        return r;
3931

3932
                if (m->soft_reboots_count > 0) {
×
3933
                        r = strv_env_assignf(&nl, "SYSTEMD_SOFT_REBOOTS_COUNT", "%u", m->soft_reboots_count);
×
3934
                        if (r < 0)
×
3935
                                return r;
3936
                }
3937

3938
                if (m->first_boot >= 0) {
×
3939
                        r = strv_env_assign(&nl, "SYSTEMD_FIRST_BOOT", one_zero(m->first_boot));
×
3940
                        if (r < 0)
×
3941
                                return r;
3942
                }
3943
        }
3944

3945
        v = detect_virtualization();
178✔
3946
        if (v < 0)
178✔
3947
                log_debug_errno(v, "Failed to detect virtualization, ignoring: %m");
×
3948
        else if (v > 0) {
178✔
3949
                const char *s;
178✔
3950

3951
                s = strjoina(VIRTUALIZATION_IS_VM(v) ? "vm:" :
890✔
3952
                             VIRTUALIZATION_IS_CONTAINER(v) ? "container:" : ":",
3953
                             virtualization_to_string(v));
3954

3955
                r = strv_env_assign(&nl, "SYSTEMD_VIRTUALIZATION", s);
178✔
3956
                if (r < 0)
178✔
3957
                        return r;
3958
        }
3959

3960
        cv = detect_confidential_virtualization();
178✔
3961
        if (cv < 0)
178✔
3962
                log_debug_errno(cv, "Failed to detect confidential virtualization, ignoring: %m");
×
3963
        else if (cv > 0) {
178✔
3964
                r = strv_env_assign(&nl, "SYSTEMD_CONFIDENTIAL_VIRTUALIZATION", confidential_virtualization_to_string(cv));
×
3965
                if (r < 0)
×
3966
                        return r;
3967
        }
3968

3969
        r = strv_env_assign(&nl, "SYSTEMD_ARCHITECTURE", architecture_to_string(uname_architecture()));
178✔
3970
        if (r < 0)
178✔
3971
                return r;
3972

3973
        *ret = TAKE_PTR(nl);
178✔
3974
        return 0;
178✔
3975
}
3976

3977
static int manager_execute_generators(Manager *m, char * const *paths, bool remount_ro) {
178✔
3978
        _cleanup_strv_free_ char **ge = NULL;
178✔
3979
        int r;
178✔
3980

3981
        assert(m);
178✔
3982

3983
        r = build_generator_environment(m, &ge);
178✔
3984
        if (r < 0)
178✔
3985
                return log_error_errno(r, "Failed to build generator environment: %m");
×
3986

3987
        if (remount_ro) {
178✔
3988
                /* Remount most of the filesystem tree read-only. We leave /sys/ as-is, because our code
3989
                 * checks whether it is read-only to detect containerized execution environments. We leave
3990
                 * /run/ as-is too, because that's where our output goes. We also leave /proc/ and /dev/shm/
3991
                 * because they're API, and /tmp/ that safe_fork() mounted for us.
3992
                 */
3993
                r = bind_remount_recursive("/", MS_RDONLY, MS_RDONLY,
×
3994
                                           STRV_MAKE("/sys", "/run", "/proc", "/dev/shm", "/tmp"));
×
3995
                if (r < 0)
×
3996
                        log_warning_errno(r, "Read-only bind remount failed, ignoring: %m");
×
3997
        }
3998

3999
        const char *argv[] = {
178✔
4000
                NULL, /* Leave this empty, execute_directory() will fill something in */
4001
                m->lookup_paths.generator,
178✔
4002
                m->lookup_paths.generator_early,
178✔
4003
                m->lookup_paths.generator_late,
178✔
4004
                NULL,
4005
        };
4006

4007
        BLOCK_WITH_UMASK(0022);
356✔
4008
        return execute_directories(
178✔
4009
                        (const char* const*) paths,
4010
                        DEFAULT_TIMEOUT_USEC,
4011
                        /* callbacks= */ NULL, /* callback_args= */ NULL,
4012
                        (char**) argv,
4013
                        ge,
4014
                        EXEC_DIR_PARALLEL | EXEC_DIR_IGNORE_ERRORS | EXEC_DIR_SET_SYSTEMD_EXEC_PID | EXEC_DIR_WARN_WORLD_WRITABLE);
4015
}
4016

4017
static int manager_run_generators(Manager *m) {
726✔
4018
        ForkFlags flags = FORK_RESET_SIGNALS | FORK_WAIT | FORK_NEW_MOUNTNS | FORK_MOUNTNS_SLAVE;
726✔
4019
        _cleanup_strv_free_ char **paths = NULL;
726✔
4020
        int r;
726✔
4021

4022
        assert(m);
726✔
4023

4024
        if (MANAGER_IS_TEST_RUN(m) && !(m->test_run_flags & MANAGER_TEST_RUN_GENERATORS))
726✔
4025
                return 0;
4026

4027
        paths = generator_binary_paths(m->runtime_scope);
262✔
4028
        if (!paths)
262✔
4029
                return log_oom();
×
4030

4031
        if (!generator_path_any(paths))
262✔
4032
                return 0;
4033

4034
        r = lookup_paths_mkdir_generator(&m->lookup_paths);
262✔
4035
        if (r < 0) {
262✔
4036
                log_error_errno(r, "Failed to create generator directories: %m");
×
4037
                goto finish;
×
4038
        }
4039

4040
        /* If we are the system manager, we fork and invoke the generators in a sanitized mount namespace. If
4041
         * we are the user manager, let's just execute the generators directly. We might not have the
4042
         * necessary privileges, and the system manager has already mounted /tmp/ and everything else for us.
4043
         */
4044
        if (MANAGER_IS_USER(m)) {
262✔
4045
                r = manager_execute_generators(m, paths, /* remount_ro= */ false);
178✔
4046
                goto finish;
178✔
4047
        }
4048

4049
        /* On some systems /tmp/ doesn't exist, and on some other systems we cannot create it at all. Avoid
4050
         * trying to mount a private tmpfs on it as there's no one size fits all. */
4051
        if (is_dir("/tmp", /* follow= */ false) > 0 && !MANAGER_IS_TEST_RUN(m))
84✔
4052
                flags |= FORK_PRIVATE_TMP;
84✔
4053

4054
        r = safe_fork("(sd-gens)", flags, NULL);
84✔
4055
        if (r == 0) {
84✔
4056
                r = manager_execute_generators(m, paths, /* remount_ro= */ true);
×
4057
                _exit(r >= 0 ? EXIT_SUCCESS : EXIT_FAILURE);
×
4058
        }
4059
        if (r < 0) {
84✔
4060
                if (!ERRNO_IS_PRIVILEGE(r) && r != -EINVAL) {
×
4061
                        log_error_errno(r, "Failed to fork off sandboxing environment for executing generators: %m");
×
4062
                        goto finish;
×
4063
                }
4064

4065
                /* Failed to fork with new mount namespace? Maybe, running in a container environment with
4066
                 * seccomp or without capability.
4067
                 *
4068
                 * We also allow -EINVAL to allow running without CLONE_NEWNS.
4069
                 *
4070
                 * Also, when running on non-native userland architecture via systemd-nspawn and
4071
                 * qemu-user-static QEMU-emulator, clone() with CLONE_NEWNS fails with EINVAL, see
4072
                 * https://github.com/systemd/systemd/issues/28901.
4073
                 */
4074
                log_debug_errno(r,
×
4075
                                "Failed to fork off sandboxing environment for executing generators. "
4076
                                "Falling back to execute generators without sandboxing: %m");
4077
                r = manager_execute_generators(m, paths, /* remount_ro= */ false);
×
4078
        }
4079

4080
finish:
84✔
4081
        lookup_paths_trim_generator(&m->lookup_paths);
262✔
4082
        return r;
4083
}
4084

4085
int manager_transient_environment_add(Manager *m, char **plus) {
262✔
4086
        char **a;
262✔
4087

4088
        assert(m);
262✔
4089

4090
        if (strv_isempty(plus))
262✔
4091
                return 0;
262✔
4092

4093
        a = strv_env_merge(m->transient_environment, plus);
84✔
4094
        if (!a)
84✔
4095
                return log_oom();
×
4096

4097
        sanitize_environment(a);
84✔
4098

4099
        return strv_free_and_replace(m->transient_environment, a);
84✔
4100
}
4101

4102
int manager_client_environment_modify(
175✔
4103
                Manager *m,
4104
                char **minus,
4105
                char **plus) {
4106

4107
        char **a = NULL, **b = NULL, **l;
175✔
4108

4109
        assert(m);
175✔
4110

4111
        if (strv_isempty(minus) && strv_isempty(plus))
175✔
4112
                return 0;
4113

4114
        l = m->client_environment;
175✔
4115

4116
        if (!strv_isempty(minus)) {
175✔
4117
                a = strv_env_delete(l, 1, minus);
1✔
4118
                if (!a)
1✔
4119
                        return -ENOMEM;
4120

4121
                l = a;
4122
        }
4123

4124
        if (!strv_isempty(plus)) {
175✔
4125
                b = strv_env_merge(l, plus);
174✔
4126
                if (!b) {
174✔
4127
                        strv_free(a);
×
4128
                        return -ENOMEM;
×
4129
                }
4130

4131
                l = b;
4132
        }
4133

4134
        if (m->client_environment != l)
175✔
4135
                strv_free(m->client_environment);
175✔
4136

4137
        if (a != l)
175✔
4138
                strv_free(a);
174✔
4139
        if (b != l)
175✔
4140
                strv_free(b);
1✔
4141

4142
        m->client_environment = sanitize_environment(l);
175✔
4143
        return 0;
175✔
4144
}
4145

4146
int manager_get_effective_environment(Manager *m, char ***ret) {
18,832✔
4147
        char **l;
18,832✔
4148

4149
        assert(m);
18,832✔
4150
        assert(ret);
18,832✔
4151

4152
        l = strv_env_merge(m->transient_environment, m->client_environment);
18,832✔
4153
        if (!l)
18,832✔
4154
                return -ENOMEM;
4155

4156
        *ret = l;
18,832✔
4157
        return 0;
18,832✔
4158
}
4159

4160
int manager_set_unit_defaults(Manager *m, const UnitDefaults *defaults) {
262✔
4161
        _cleanup_free_ char *label = NULL;
262✔
4162
        struct rlimit *rlimit[_RLIMIT_MAX];
262✔
4163
        int r;
262✔
4164

4165
        assert(m);
262✔
4166
        assert(defaults);
262✔
4167

4168
        if (streq_ptr(defaults->smack_process_label, "/"))
262✔
4169
                label = NULL;
4170
        else  {
4171
                const char *l = defaults->smack_process_label;
262✔
4172
#ifdef SMACK_DEFAULT_PROCESS_LABEL
4173
                if (!l)
4174
                        l = SMACK_DEFAULT_PROCESS_LABEL;
4175
#endif
4176
                if (l) {
262✔
4177
                        label = strdup(l);
×
4178
                        if (!label)
×
4179
                                return -ENOMEM;
4180
                } else
4181
                        label = NULL;
4182
        }
4183

4184
        r = rlimit_copy_all(rlimit, defaults->rlimit);
262✔
4185
        if (r < 0)
262✔
4186
                return r;
4187

4188
        m->defaults.std_output = defaults->std_output;
262✔
4189
        m->defaults.std_error = defaults->std_error;
262✔
4190

4191
        m->defaults.restart_usec = defaults->restart_usec;
262✔
4192
        m->defaults.timeout_start_usec = defaults->timeout_start_usec;
262✔
4193
        m->defaults.timeout_stop_usec = defaults->timeout_stop_usec;
262✔
4194
        m->defaults.timeout_abort_usec = defaults->timeout_abort_usec;
262✔
4195
        m->defaults.timeout_abort_set = defaults->timeout_abort_set;
262✔
4196
        m->defaults.device_timeout_usec = defaults->device_timeout_usec;
262✔
4197

4198
        m->defaults.start_limit = defaults->start_limit;
262✔
4199

4200
        m->defaults.memory_accounting = defaults->memory_accounting;
262✔
4201
        m->defaults.io_accounting = defaults->io_accounting;
262✔
4202
        m->defaults.tasks_accounting = defaults->tasks_accounting;
262✔
4203
        m->defaults.ip_accounting = defaults->ip_accounting;
262✔
4204

4205
        m->defaults.tasks_max = defaults->tasks_max;
262✔
4206
        m->defaults.timer_accuracy_usec = defaults->timer_accuracy_usec;
262✔
4207

4208
        m->defaults.oom_policy = defaults->oom_policy;
262✔
4209
        m->defaults.oom_score_adjust = defaults->oom_score_adjust;
262✔
4210
        m->defaults.oom_score_adjust_set = defaults->oom_score_adjust_set;
262✔
4211

4212
        m->defaults.memory_pressure_watch = defaults->memory_pressure_watch;
262✔
4213
        m->defaults.memory_pressure_threshold_usec = defaults->memory_pressure_threshold_usec;
262✔
4214

4215
        free_and_replace(m->defaults.smack_process_label, label);
262✔
4216
        rlimit_free_all(m->defaults.rlimit);
262✔
4217
        memcpy(m->defaults.rlimit, rlimit, sizeof(struct rlimit*) * _RLIMIT_MAX);
262✔
4218

4219
        return 0;
262✔
4220
}
4221

4222
void manager_recheck_dbus(Manager *m) {
68,983✔
4223
        assert(m);
68,983✔
4224

4225
        /* Connects to the bus if the dbus service and socket are running. If we are running in user mode
4226
         * this is all it does. In system mode we'll also connect to the system bus (which will most likely
4227
         * just reuse the connection of the API bus). That's because the system bus after all runs as service
4228
         * of the system instance, while in the user instance we can assume it's already there. */
4229

4230
        if (MANAGER_IS_RELOADING(m))
68,983✔
4231
                return; /* don't check while we are reloading… */
4232

4233
        if (manager_dbus_is_running(m, false)) {
61,150✔
4234
                (void) bus_init_api(m);
3,990✔
4235

4236
                if (MANAGER_IS_SYSTEM(m))
3,990✔
4237
                        (void) bus_init_system(m);
3,090✔
4238
        } else {
4239
                (void) bus_done_api(m);
57,160✔
4240

4241
                if (MANAGER_IS_SYSTEM(m))
57,160✔
4242
                        (void) bus_done_system(m);
8,243✔
4243
        }
4244
}
4245

4246
static bool manager_journal_is_running(Manager *m) {
13,083✔
4247
        Unit *u;
13,083✔
4248

4249
        assert(m);
13,083✔
4250

4251
        if (MANAGER_IS_TEST_RUN(m))
13,083✔
4252
                return false;
4253

4254
        /* If we are the user manager we can safely assume that the journal is up */
4255
        if (!MANAGER_IS_SYSTEM(m))
13,083✔
4256
                return true;
4257

4258
        /* Check that the socket is not only up, but in RUNNING state */
4259
        u = manager_get_unit(m, SPECIAL_JOURNALD_SOCKET);
12,303✔
4260
        if (!u)
12,303✔
4261
                return false;
4262
        if (SOCKET(u)->state != SOCKET_RUNNING)
12,303✔
4263
                return false;
4264

4265
        /* Similar, check if the daemon itself is fully up, too */
4266
        u = manager_get_unit(m, SPECIAL_JOURNALD_SERVICE);
9,378✔
4267
        if (!u)
9,378✔
4268
                return false;
4269
        if (!IN_SET(SERVICE(u)->state, SERVICE_RELOAD, SERVICE_RUNNING))
9,378✔
4270
                return false;
546✔
4271

4272
        return true;
4273
}
4274

4275
void disable_printk_ratelimit(void) {
15✔
4276
        /* Disable kernel's printk ratelimit.
4277
         *
4278
         * Logging to /dev/kmsg is most useful during early boot and shutdown, where normal logging
4279
         * mechanisms are not available. The semantics of this sysctl are such that any kernel command-line
4280
         * setting takes precedence. */
4281
        int r;
15✔
4282

4283
        r = sysctl_write("kernel/printk_devkmsg", "on");
15✔
4284
        if (r < 0)
15✔
4285
                log_debug_errno(r, "Failed to set sysctl kernel.printk_devkmsg=on: %m");
×
4286
}
15✔
4287

4288
void manager_recheck_journal(Manager *m) {
68,983✔
4289

4290
        assert(m);
68,983✔
4291

4292
        /* Don't bother with this unless we are in the special situation of being PID 1 */
4293
        if (getpid_cached() != 1)
68,983✔
4294
                return;
4295

4296
        /* Don't check this while we are reloading, things might still change */
4297
        if (MANAGER_IS_RELOADING(m))
18,578✔
4298
                return;
4299

4300
        /* The journal is fully and entirely up? If so, let's permit logging to it, if that's configured. If
4301
         * the journal is down, don't ever log to it, otherwise we might end up deadlocking ourselves as we
4302
         * might trigger an activation ourselves we can't fulfill. */
4303
        log_set_prohibit_ipc(!manager_journal_is_running(m));
10,886✔
4304
        log_open();
10,886✔
4305
}
4306

4307
static ShowStatus manager_get_show_status(Manager *m) {
6,673✔
4308
        assert(m);
6,673✔
4309

4310
        if (MANAGER_IS_USER(m))
6,673✔
4311
                return _SHOW_STATUS_INVALID;
4312

4313
        if (m->show_status_overridden != _SHOW_STATUS_INVALID)
6,672✔
4314
                return m->show_status_overridden;
4315

4316
        return m->show_status;
6,672✔
4317
}
4318

4319
bool manager_get_show_status_on(Manager *m) {
6,673✔
4320
        assert(m);
6,673✔
4321

4322
        return show_status_on(manager_get_show_status(m));
6,673✔
4323
}
4324

4325
static void set_show_status_marker(bool b) {
60✔
4326
        if (b)
60✔
4327
                (void) touch("/run/systemd/show-status");
×
4328
        else
4329
                (void) unlink("/run/systemd/show-status");
60✔
4330
}
60✔
4331

4332
void manager_set_show_status(Manager *m, ShowStatus mode, const char *reason) {
262✔
4333
        assert(m);
262✔
4334
        assert(reason);
262✔
4335
        assert(mode >= 0 && mode < _SHOW_STATUS_MAX);
262✔
4336

4337
        if (MANAGER_IS_USER(m))
262✔
4338
                return;
4339

4340
        if (mode == m->show_status)
84✔
4341
                return;
4342

4343
        if (m->show_status_overridden == _SHOW_STATUS_INVALID) {
60✔
4344
                bool enabled;
60✔
4345

4346
                enabled = show_status_on(mode);
60✔
4347
                log_debug("%s (%s) showing of status (%s).",
120✔
4348
                          enabled ? "Enabling" : "Disabling",
4349
                          strna(show_status_to_string(mode)),
4350
                          reason);
4351

4352
                set_show_status_marker(enabled);
60✔
4353
        }
4354

4355
        m->show_status = mode;
60✔
4356
}
4357

4358
void manager_override_show_status(Manager *m, ShowStatus mode, const char *reason) {
×
4359
        assert(m);
×
4360
        assert(mode < _SHOW_STATUS_MAX);
×
4361

4362
        if (MANAGER_IS_USER(m))
×
4363
                return;
4364

4365
        if (mode == m->show_status_overridden)
×
4366
                return;
4367

4368
        m->show_status_overridden = mode;
×
4369

4370
        if (mode == _SHOW_STATUS_INVALID)
×
4371
                mode = m->show_status;
×
4372

4373
        log_debug("%s (%s) showing of status (%s).",
×
4374
                  m->show_status_overridden != _SHOW_STATUS_INVALID ? "Overriding" : "Restoring",
4375
                  strna(show_status_to_string(mode)),
4376
                  reason);
4377

4378
        set_show_status_marker(show_status_on(mode));
×
4379
}
4380

4381
const char* manager_get_confirm_spawn(Manager *m) {
2,209✔
4382
        static int last_errno = 0;
2,209✔
4383
        struct stat st;
2,209✔
4384
        int r;
2,209✔
4385

4386
        assert(m);
2,209✔
4387

4388
        /* Here's the deal: we want to test the validity of the console but don't want
4389
         * PID1 to go through the whole console process which might block. But we also
4390
         * want to warn the user only once if something is wrong with the console so we
4391
         * cannot do the sanity checks after spawning our children. So here we simply do
4392
         * really basic tests to hopefully trap common errors.
4393
         *
4394
         * If the console suddenly disappear at the time our children will really it
4395
         * then they will simply fail to acquire it and a positive answer will be
4396
         * assumed. New children will fall back to /dev/console though.
4397
         *
4398
         * Note: TTYs are devices that can come and go any time, and frequently aren't
4399
         * available yet during early boot (consider a USB rs232 dongle...). If for any
4400
         * reason the configured console is not ready, we fall back to the default
4401
         * console. */
4402

4403
        if (!m->confirm_spawn || path_equal(m->confirm_spawn, "/dev/console"))
2,209✔
4404
                return m->confirm_spawn;
2,209✔
4405

4406
        if (stat(m->confirm_spawn, &st) < 0) {
×
4407
                r = -errno;
×
4408
                goto fail;
×
4409
        }
4410

4411
        if (!S_ISCHR(st.st_mode)) {
×
4412
                r = -ENOTTY;
×
4413
                goto fail;
×
4414
        }
4415

4416
        last_errno = 0;
×
4417
        return m->confirm_spawn;
×
4418

4419
fail:
×
4420
        if (last_errno != r)
×
4421
                last_errno = log_warning_errno(r, "Failed to open %s, using default console: %m", m->confirm_spawn);
×
4422

4423
        return "/dev/console";
4424
}
4425

4426
void manager_set_first_boot(Manager *m, bool b) {
416✔
4427
        assert(m);
416✔
4428

4429
        if (!MANAGER_IS_SYSTEM(m))
416✔
4430
                return;
4431

4432
        if (m->first_boot != (int) b) {
60✔
4433
                if (b)
60✔
4434
                        (void) touch("/run/systemd/first-boot");
17✔
4435
                else
4436
                        (void) unlink("/run/systemd/first-boot");
43✔
4437
        }
4438

4439
        m->first_boot = b;
60✔
4440
}
4441

4442
void manager_disable_confirm_spawn(void) {
×
4443
        (void) touch("/run/systemd/confirm_spawn_disabled");
×
4444
}
×
4445

4446
static bool manager_should_show_status(Manager *m, StatusType type) {
15,534✔
4447
        assert(m);
15,534✔
4448

4449
        if (!MANAGER_IS_SYSTEM(m))
15,534✔
4450
                return false;
4451

4452
        if (m->no_console_output)
7,138✔
4453
                return false;
4454

4455
        if (!IN_SET(manager_state(m), MANAGER_INITIALIZING, MANAGER_STARTING, MANAGER_STOPPING))
6,658✔
4456
                return false;
4457

4458
        /* If we cannot find out the status properly, just proceed. */
4459
        if (type != STATUS_TYPE_EMERGENCY && manager_check_ask_password(m) > 0)
6,658✔
4460
                return false;
4461

4462
        if (type == STATUS_TYPE_NOTICE && m->show_status != SHOW_STATUS_NO)
6,658✔
4463
                return true;
4464

4465
        return manager_get_show_status_on(m);
6,655✔
4466
}
4467

4468
void manager_status_printf(Manager *m, StatusType type, const char *status, const char *format, ...) {
15,534✔
4469
        va_list ap;
15,534✔
4470

4471
        /* If m is NULL, assume we're after shutdown and let the messages through. */
4472

4473
        if (m && !manager_should_show_status(m, type))
15,534✔
4474
                return;
15,531✔
4475

4476
        /* XXX We should totally drop the check for ephemeral here
4477
         * and thus effectively make 'Type=idle' pointless. */
4478
        if (type == STATUS_TYPE_EPHEMERAL && m && m->n_on_console > 0)
3✔
4479
                return;
4480

4481
        va_start(ap, format);
3✔
4482
        status_vprintf(status, SHOW_STATUS_ELLIPSIZE|(type == STATUS_TYPE_EPHEMERAL ? SHOW_STATUS_EPHEMERAL : 0), format, ap);
6✔
4483
        va_end(ap);
3✔
4484
}
4485

4486
Set* manager_get_units_needing_mounts_for(Manager *m, const char *path, UnitMountDependencyType t) {
18,458✔
4487
        assert(m);
18,458✔
4488
        assert(path);
18,458✔
4489
        assert(t >= 0 && t < _UNIT_MOUNT_DEPENDENCY_TYPE_MAX);
18,458✔
4490

4491
        if (path_equal(path, "/"))
18,458✔
4492
                path = "";
828✔
4493

4494
        return hashmap_get(m->units_needing_mounts_for[t], path);
18,458✔
4495
}
4496

4497
int manager_update_failed_units(Manager *m, Unit *u, bool failed) {
147,803✔
4498
        unsigned size;
147,803✔
4499
        int r;
147,803✔
4500

4501
        assert(m);
147,803✔
4502
        assert(u->manager == m);
147,803✔
4503

4504
        size = set_size(m->failed_units);
147,803✔
4505

4506
        if (failed) {
147,803✔
4507
                r = set_ensure_put(&m->failed_units, NULL, u);
251✔
4508
                if (r < 0)
251✔
4509
                        return log_oom();
×
4510
        } else
4511
                (void) set_remove(m->failed_units, u);
147,552✔
4512

4513
        if (set_size(m->failed_units) != size)
147,803✔
4514
                bus_manager_send_change_signal(m);
502✔
4515

4516
        return 0;
4517
}
4518

4519
ManagerState manager_state(Manager *m) {
411,034✔
4520
        Unit *u;
411,034✔
4521

4522
        assert(m);
411,034✔
4523

4524
        /* Is the special shutdown target active or queued? If so, we are in shutdown state */
4525
        u = manager_get_unit(m, SPECIAL_SHUTDOWN_TARGET);
411,034✔
4526
        if (u && unit_active_or_pending(u))
411,034✔
4527
                return MANAGER_STOPPING;
4528

4529
        /* Did we ever finish booting? If not then we are still starting up */
4530
        if (!MANAGER_IS_FINISHED(m)) {
327,962✔
4531

4532
                u = manager_get_unit(m, SPECIAL_BASIC_TARGET);
296,641✔
4533
                if (!u || !UNIT_IS_ACTIVE_OR_RELOADING(unit_active_state(u)))
296,641✔
4534
                        return MANAGER_INITIALIZING;
247,293✔
4535

4536
                return MANAGER_STARTING;
4537
        }
4538

4539
        if (MANAGER_IS_SYSTEM(m)) {
31,321✔
4540
                /* Are the rescue or emergency targets active or queued? If so we are in maintenance state */
4541
                u = manager_get_unit(m, SPECIAL_RESCUE_TARGET);
×
4542
                if (u && unit_active_or_pending(u))
×
4543
                        return MANAGER_MAINTENANCE;
4544

4545
                u = manager_get_unit(m, SPECIAL_EMERGENCY_TARGET);
×
4546
                if (u && unit_active_or_pending(u))
×
4547
                        return MANAGER_MAINTENANCE;
4548
        }
4549

4550
        /* Are there any failed units? If so, we are in degraded mode */
4551
        if (!set_isempty(m->failed_units))
31,321✔
4552
                return MANAGER_DEGRADED;
1,086✔
4553

4554
        return MANAGER_RUNNING;
4555
}
4556

4557
static void manager_unref_uid_internal(
809✔
4558
                Hashmap *uid_refs,
4559
                uid_t uid,
4560
                bool destroy_now,
4561
                int (*_clean_ipc)(uid_t uid)) {
4562

4563
        uint32_t c, n;
809✔
4564

4565
        assert(uid_is_valid(uid));
809✔
4566
        assert(_clean_ipc);
809✔
4567

4568
        /* A generic implementation, covering both manager_unref_uid() and manager_unref_gid(), under the
4569
         * assumption that uid_t and gid_t are actually defined the same way, with the same validity rules.
4570
         *
4571
         * We store a hashmap where the key is the UID/GID and the value is a 32-bit reference counter, whose
4572
         * highest bit is used as flag for marking UIDs/GIDs whose IPC objects to remove when the last
4573
         * reference to the UID/GID is dropped. The flag is set to on, once at least one reference from a
4574
         * unit where RemoveIPC= is set is added on a UID/GID. It is reset when the UID's/GID's reference
4575
         * counter drops to 0 again. */
4576

4577
        assert_cc(sizeof(uid_t) == sizeof(gid_t));
809✔
4578
        assert_cc(UID_INVALID == (uid_t) GID_INVALID);
809✔
4579

4580
        if (uid == 0) /* We don't keep track of root, and will never destroy it */
809✔
4581
                return;
4582

4583
        c = PTR_TO_UINT32(hashmap_get(uid_refs, UID_TO_PTR(uid)));
707✔
4584

4585
        n = c & ~DESTROY_IPC_FLAG;
707✔
4586
        assert(n > 0);
707✔
4587
        n--;
707✔
4588

4589
        if (destroy_now && n == 0) {
707✔
4590
                hashmap_remove(uid_refs, UID_TO_PTR(uid));
129✔
4591

4592
                if (c & DESTROY_IPC_FLAG) {
129✔
4593
                        log_debug("%s " UID_FMT " is no longer referenced, cleaning up its IPC.",
3✔
4594
                                  _clean_ipc == clean_ipc_by_uid ? "UID" : "GID",
4595
                                  uid);
4596
                        (void) _clean_ipc(uid);
2✔
4597
                }
4598
        } else {
4599
                c = n | (c & DESTROY_IPC_FLAG);
578✔
4600
                assert_se(hashmap_update(uid_refs, UID_TO_PTR(uid), UINT32_TO_PTR(c)) >= 0);
578✔
4601
        }
4602
}
4603

4604
void manager_unref_uid(Manager *m, uid_t uid, bool destroy_now) {
404✔
4605
        manager_unref_uid_internal(m->uid_refs, uid, destroy_now, clean_ipc_by_uid);
404✔
4606
}
404✔
4607

4608
void manager_unref_gid(Manager *m, gid_t gid, bool destroy_now) {
405✔
4609
        manager_unref_uid_internal(m->gid_refs, (uid_t) gid, destroy_now, clean_ipc_by_gid);
405✔
4610
}
405✔
4611

4612
static int manager_ref_uid_internal(
809✔
4613
                Hashmap **uid_refs,
4614
                uid_t uid,
4615
                bool clean_ipc) {
4616

4617
        uint32_t c, n;
809✔
4618
        int r;
809✔
4619

4620
        assert(uid_refs);
809✔
4621
        assert(uid_is_valid(uid));
809✔
4622

4623
        /* A generic implementation, covering both manager_ref_uid() and manager_ref_gid(), under the
4624
         * assumption that uid_t and gid_t are actually defined the same way, with the same validity
4625
         * rules. */
4626

4627
        assert_cc(sizeof(uid_t) == sizeof(gid_t));
809✔
4628
        assert_cc(UID_INVALID == (uid_t) GID_INVALID);
809✔
4629

4630
        if (uid == 0) /* We don't keep track of root, and will never destroy it */
809✔
4631
                return 0;
4632

4633
        r = hashmap_ensure_allocated(uid_refs, &trivial_hash_ops);
707✔
4634
        if (r < 0)
707✔
4635
                return r;
4636

4637
        c = PTR_TO_UINT32(hashmap_get(*uid_refs, UID_TO_PTR(uid)));
707✔
4638

4639
        n = c & ~DESTROY_IPC_FLAG;
707✔
4640
        n++;
707✔
4641

4642
        if (n & DESTROY_IPC_FLAG) /* check for overflow */
707✔
4643
                return -EOVERFLOW;
4644

4645
        c = n | (c & DESTROY_IPC_FLAG) | (clean_ipc ? DESTROY_IPC_FLAG : 0);
707✔
4646

4647
        return hashmap_replace(*uid_refs, UID_TO_PTR(uid), UINT32_TO_PTR(c));
707✔
4648
}
4649

4650
int manager_ref_uid(Manager *m, uid_t uid, bool clean_ipc) {
404✔
4651
        return manager_ref_uid_internal(&m->uid_refs, uid, clean_ipc);
404✔
4652
}
4653

4654
int manager_ref_gid(Manager *m, gid_t gid, bool clean_ipc) {
405✔
4655
        return manager_ref_uid_internal(&m->gid_refs, (uid_t) gid, clean_ipc);
405✔
4656
}
4657

4658
static void manager_vacuum_uid_refs_internal(
1,452✔
4659
                Hashmap *uid_refs,
4660
                int (*_clean_ipc)(uid_t uid)) {
4661

4662
        void *p, *k;
1,452✔
4663

4664
        assert(_clean_ipc);
1,452✔
4665

4666
        HASHMAP_FOREACH_KEY(p, k, uid_refs) {
3,242✔
4667
                uint32_t c, n;
338✔
4668
                uid_t uid;
338✔
4669

4670
                uid = PTR_TO_UID(k);
338✔
4671
                c = PTR_TO_UINT32(p);
338✔
4672

4673
                n = c & ~DESTROY_IPC_FLAG;
338✔
4674
                if (n > 0)
338✔
4675
                        continue;
338✔
4676

4677
                if (c & DESTROY_IPC_FLAG) {
×
4678
                        log_debug("Found unreferenced %s " UID_FMT " after reload/reexec. Cleaning up.",
×
4679
                                  _clean_ipc == clean_ipc_by_uid ? "UID" : "GID",
4680
                                  uid);
4681
                        (void) _clean_ipc(uid);
×
4682
                }
4683

4684
                assert_se(hashmap_remove(uid_refs, k) == p);
1,790✔
4685
        }
4686
}
1,452✔
4687

4688
static void manager_vacuum_uid_refs(Manager *m) {
726✔
4689
        manager_vacuum_uid_refs_internal(m->uid_refs, clean_ipc_by_uid);
726✔
4690
}
726✔
4691

4692
static void manager_vacuum_gid_refs(Manager *m) {
726✔
4693
        manager_vacuum_uid_refs_internal(m->gid_refs, clean_ipc_by_gid);
726✔
4694
}
726✔
4695

4696
static void manager_vacuum(Manager *m) {
726✔
4697
        assert(m);
726✔
4698

4699
        /* Release any dynamic users no longer referenced */
4700
        dynamic_user_vacuum(m, true);
726✔
4701

4702
        /* Release any references to UIDs/GIDs no longer referenced, and destroy any IPC owned by them */
4703
        manager_vacuum_uid_refs(m);
726✔
4704
        manager_vacuum_gid_refs(m);
726✔
4705

4706
        /* Release any runtimes no longer referenced */
4707
        exec_shared_runtime_vacuum(m);
726✔
4708
}
726✔
4709

4710
static int manager_dispatch_user_lookup_fd(sd_event_source *source, int fd, uint32_t revents, void *userdata) {
159✔
4711
        struct buffer {
159✔
4712
                uid_t uid;
4713
                gid_t gid;
4714
                char unit_name[UNIT_NAME_MAX+1];
4715
        } _packed_ buffer;
4716

4717
        Manager *m = ASSERT_PTR(userdata);
159✔
4718
        ssize_t l;
159✔
4719
        size_t n;
159✔
4720
        Unit *u;
159✔
4721

4722
        assert(source);
159✔
4723

4724
        /* Invoked whenever a child process succeeded resolving its user/group to use and sent us the
4725
         * resulting UID/GID in a datagram. We parse the datagram here and pass it off to the unit, so that
4726
         * it can add a reference to the UID/GID so that it can destroy the UID/GID's IPC objects when the
4727
         * reference counter drops to 0. */
4728

4729
        l = recv(fd, &buffer, sizeof(buffer), MSG_DONTWAIT);
159✔
4730
        if (l < 0) {
159✔
4731
                if (ERRNO_IS_TRANSIENT(errno))
×
4732
                        return 0;
159✔
4733

4734
                return log_error_errno(errno, "Failed to read from user lookup fd: %m");
×
4735
        }
4736

4737
        if ((size_t) l <= offsetof(struct buffer, unit_name)) {
159✔
4738
                log_warning("Received too short user lookup message, ignoring.");
×
4739
                return 0;
×
4740
        }
4741

4742
        if ((size_t) l > offsetof(struct buffer, unit_name) + UNIT_NAME_MAX) {
159✔
4743
                log_warning("Received too long user lookup message, ignoring.");
×
4744
                return 0;
×
4745
        }
4746

4747
        if (!uid_is_valid(buffer.uid) && !gid_is_valid(buffer.gid)) {
159✔
4748
                log_warning("Got user lookup message with invalid UID/GID pair, ignoring.");
×
4749
                return 0;
×
4750
        }
4751

4752
        n = (size_t) l - offsetof(struct buffer, unit_name);
159✔
4753
        if (memchr(buffer.unit_name, 0, n)) {
159✔
4754
                log_warning("Received lookup message with embedded NUL character, ignoring.");
×
4755
                return 0;
×
4756
        }
4757

4758
        buffer.unit_name[n] = 0;
159✔
4759
        u = manager_get_unit(m, buffer.unit_name);
159✔
4760
        if (!u) {
159✔
4761
                log_debug("Got user lookup message but unit doesn't exist, ignoring.");
×
4762
                return 0;
×
4763
        }
4764

4765
        log_unit_debug(u, "User lookup succeeded: uid=" UID_FMT " gid=" GID_FMT, buffer.uid, buffer.gid);
159✔
4766

4767
        unit_notify_user_lookup(u, buffer.uid, buffer.gid);
159✔
4768
        return 0;
4769
}
4770

4771
static int manager_dispatch_handoff_timestamp_fd(sd_event_source *source, int fd, uint32_t revents, void *userdata) {
2,193✔
4772
        Manager *m = ASSERT_PTR(userdata);
2,193✔
4773
        usec_t ts[2] = {};
2,193✔
4774
        CMSG_BUFFER_TYPE(CMSG_SPACE(sizeof(struct ucred))) control;
2,193✔
4775
        struct msghdr msghdr = {
2,193✔
4776
                .msg_iov = &IOVEC_MAKE(ts, sizeof(ts)),
2,193✔
4777
                .msg_iovlen = 1,
4778
                .msg_control = &control,
4779
                .msg_controllen = sizeof(control),
4780
        };
4781
        ssize_t n;
2,193✔
4782

4783
        assert(source);
2,193✔
4784

4785
        n = recvmsg_safe(m->handoff_timestamp_fds[0], &msghdr, MSG_DONTWAIT|MSG_CMSG_CLOEXEC);
2,193✔
4786
        if (ERRNO_IS_NEG_TRANSIENT(n))
2,193✔
4787
                return 0; /* Spurious wakeup, try again */
2,193✔
4788
        if (n == -ECHRNG) {
2,193✔
4789
                log_warning_errno(n, "Got message with truncated control data (unexpected fds sent?), ignoring.");
×
4790
                return 0;
×
4791
        }
4792
        if (n == -EXFULL) {
2,193✔
4793
                log_warning_errno(n, "Got message with truncated payload data, ignoring.");
×
4794
                return 0;
×
4795
        }
4796
        if (n < 0)
2,193✔
4797
                return log_error_errno(n, "Failed to receive handoff timestamp message: %m");
×
4798

4799
        cmsg_close_all(&msghdr);
2,193✔
4800

4801
        if (n != sizeof(ts)) {
2,193✔
4802
                log_warning("Got handoff timestamp message of unexpected size %zi (expected %zu), ignoring.", n, sizeof(ts));
×
4803
                return 0;
×
4804
        }
4805

4806
        struct ucred *ucred = CMSG_FIND_DATA(&msghdr, SOL_SOCKET, SCM_CREDENTIALS, struct ucred);
2,193✔
4807
        if (!ucred || !pid_is_valid(ucred->pid)) {
2,193✔
4808
                log_warning("Received handoff timestamp message without valid credentials. Ignoring.");
×
4809
                return 0;
×
4810
        }
4811

4812
        log_debug("Got handoff timestamp event for PID " PID_FMT ".", ucred->pid);
2,193✔
4813

4814
        _cleanup_free_ Unit **units = NULL;
2,193✔
4815
        int n_units = manager_get_units_for_pidref(m, &PIDREF_MAKE_FROM_PID(ucred->pid), &units);
2,193✔
4816
        if (n_units < 0) {
2,193✔
4817
                log_warning_errno(n_units, "Unable to determine units for PID " PID_FMT ", ignoring: %m", ucred->pid);
×
4818
                return 0;
×
4819
        }
4820
        if (n_units == 0) {
2,193✔
4821
                log_debug("Got handoff timestamp for process " PID_FMT " we are not interested in, ignoring.", ucred->pid);
×
4822
                return 0;
×
4823
        }
4824

4825
        dual_timestamp dt = {
2,193✔
4826
                .realtime = ts[0],
2,193✔
4827
                .monotonic = ts[1],
2,193✔
4828
        };
4829

4830
        FOREACH_ARRAY(u, units, n_units) {
6,579✔
4831
                if (!UNIT_VTABLE(*u)->notify_handoff_timestamp)
4,386✔
4832
                        continue;
5✔
4833

4834
                UNIT_VTABLE(*u)->notify_handoff_timestamp(*u, ucred, &dt);
4,381✔
4835
        }
4836

4837
        return 0;
4838
}
4839

4840
static int manager_dispatch_pidref_transport_fd(sd_event_source *source, int fd, uint32_t revents, void *userdata) {
3✔
4841
        Manager *m = ASSERT_PTR(userdata);
3✔
4842
        _cleanup_(pidref_done) PidRef child_pidref = PIDREF_NULL, parent_pidref = PIDREF_NULL;
3✔
4843
        _cleanup_close_ int child_pidfd = -EBADF, parent_pidfd = -EBADF;
6✔
4844
        struct ucred *ucred = NULL;
3✔
4845
        CMSG_BUFFER_TYPE(CMSG_SPACE(sizeof(struct ucred)) + CMSG_SPACE(sizeof(int)) * 2) control;
3✔
4846
        pid_t child_pid = 0; /* silence false-positive warning by coverity */
3✔
4847
        struct msghdr msghdr = {
3✔
4848
                .msg_iov = &IOVEC_MAKE(&child_pid, sizeof(child_pid)),
3✔
4849
                .msg_iovlen = 1,
4850
                .msg_control = &control,
4851
                .msg_controllen = sizeof(control),
4852
        };
4853
        struct cmsghdr *cmsg;
3✔
4854
        ssize_t n;
3✔
4855
        int r;
3✔
4856

4857
        assert(source);
3✔
4858

4859
        /* Server expects:
4860
         * - Parent PID in ucreds enabled via SO_PASSCRED
4861
         * - Parent PIDFD in SCM_PIDFD message enabled via SO_PASSPIDFD
4862
         * - Child PIDFD in SCM_RIGHTS in message body
4863
         * - Child PID in message IOV
4864
         *
4865
         * SO_PASSPIDFD may not be supported by the kernel (it is supported since v6.5) so we fall back to
4866
         * using parent PID from ucreds and accept some raciness. */
4867
        n = recvmsg_safe(m->pidref_transport_fds[0], &msghdr, MSG_DONTWAIT|MSG_CMSG_CLOEXEC|MSG_TRUNC);
3✔
4868
        if (ERRNO_IS_NEG_TRANSIENT(n))
6✔
4869
                return 0; /* Spurious wakeup, try again */
4870
        if (n == -ECHRNG) {
3✔
4871
                log_warning_errno(n, "Got message with truncated control data (unexpected fds sent?), ignoring.");
×
4872
                return 0;
×
4873
        }
4874
        if (n == -EXFULL) {
3✔
4875
                log_warning_errno(n, "Got message with truncated payload data, ignoring.");
×
4876
                return 0;
×
4877
        }
4878
        if (n < 0)
3✔
4879
                return log_error_errno(n, "Failed to receive pidref message: %m");
×
4880

4881
        if (n != sizeof(child_pid)) {
3✔
4882
                log_warning("Got pidref message of unexpected size %zi (expected %zu), ignoring.", n, sizeof(child_pid));
×
4883
                return 0;
×
4884
        }
4885

4886
        CMSG_FOREACH(cmsg, &msghdr) {
24✔
4887
                if (cmsg->cmsg_level != SOL_SOCKET)
9✔
4888
                        continue;
×
4889

4890
                if (cmsg->cmsg_type == SCM_CREDENTIALS && cmsg->cmsg_len == CMSG_LEN(sizeof(struct ucred))) {
9✔
4891
                        assert(!ucred);
3✔
4892
                        ucred = CMSG_TYPED_DATA(cmsg, struct ucred);
3✔
4893
                } else if (cmsg->cmsg_type == SCM_PIDFD) {
6✔
4894
                        assert(parent_pidfd < 0);
3✔
4895
                        parent_pidfd = *CMSG_TYPED_DATA(cmsg, int);
3✔
4896
                } else if (cmsg->cmsg_type == SCM_RIGHTS) {
3✔
4897
                        assert(child_pidfd < 0);
3✔
4898
                        child_pidfd = *CMSG_TYPED_DATA(cmsg, int);
3✔
4899
                }
4900
        }
4901

4902
        /* Verify and set parent pidref. */
4903
        if (!ucred || !pid_is_valid(ucred->pid)) {
3✔
4904
                log_warning("Received pidref message without valid credentials. Ignoring.");
×
4905
                return 0;
×
4906
        }
4907

4908
        /* Need to handle kernels without SO_PASSPIDFD where SCM_PIDFD will not be set. */
4909
        if (parent_pidfd >= 0)
3✔
4910
                r = pidref_set_pidfd_consume(&parent_pidref, TAKE_FD(parent_pidfd));
3✔
4911
        else
4912
                r = pidref_set_pid(&parent_pidref, ucred->pid);
×
4913
        if (r < 0) {
3✔
4914
                if (r == -ESRCH)
×
4915
                        log_debug_errno(r, "PidRef child process died before message is processed. Ignoring.");
×
4916
                else
4917
                        log_warning_errno(r, "Failed to pin pidref child process, ignoring message: %m");
×
4918
                return 0;
×
4919
        }
4920

4921
        if (parent_pidref.pid != ucred->pid) {
3✔
4922
                assert(parent_pidref.fd >= 0);
×
4923
                log_warning("Got SCM_PIDFD for parent process " PID_FMT " but got SCM_CREDENTIALS for parent process " PID_FMT ". Ignoring.",
×
4924
                            parent_pidref.pid, ucred->pid);
4925
                return 0;
×
4926
        }
4927

4928
        /* Verify and set child pidref. */
4929
        if (!pid_is_valid(child_pid)) {
3✔
4930
                log_warning("Received pidref message without valid child PID. Ignoring.");
×
4931
                return 0;
×
4932
        }
4933

4934
        /* Need to handle kernels without PIDFD support. */
4935
        if (child_pidfd >= 0)
3✔
4936
                r = pidref_set_pidfd_consume(&child_pidref, TAKE_FD(child_pidfd));
3✔
4937
        else
4938
                r = pidref_set_pid(&child_pidref, child_pid);
×
4939
        if (r < 0) {
3✔
4940
                if (r == -ESRCH)
×
4941
                        log_debug_errno(r, "PidRef child process died before message is processed. Ignoring.");
×
4942
                else
4943
                        log_warning_errno(r, "Failed to pin pidref child process, ignoring message: %m");
×
4944
                return 0;
×
4945
        }
4946

4947
        if (child_pidref.pid != child_pid) {
3✔
4948
                assert(child_pidref.fd >= 0);
×
4949
                log_warning("Got SCM_RIGHTS for child process " PID_FMT " but PID in IOV message is " PID_FMT ". Ignoring.",
×
4950
                            child_pidref.pid, child_pid);
4951
                return 0;
×
4952
        }
4953

4954
        log_debug("Got pidref event with parent PID " PID_FMT " and child PID " PID_FMT ".", parent_pidref.pid, child_pidref.pid);
3✔
4955

4956
        /* Try finding cgroup of parent process. But if parent process exited and we're not using PIDFD, this could return NULL.
4957
         * Then fall back to finding cgroup of the child process. */
4958
        Unit *u = manager_get_unit_by_pidref_cgroup(m, &parent_pidref);
3✔
4959
        if (!u)
3✔
4960
                u = manager_get_unit_by_pidref_cgroup(m, &child_pidref);
×
4961
        if (!u) {
×
4962
                log_debug("Got pidref for parent process " PID_FMT " and child process " PID_FMT " we are not interested in, ignoring.", parent_pidref.pid, child_pidref.pid);
×
4963
                return 0;
×
4964
        }
4965

4966
        if (!UNIT_VTABLE(u)->notify_pidref) {
3✔
4967
                log_unit_warning(u, "Received pidref event from unexpected unit type '%s'.", unit_type_to_string(u->type));
×
4968
                return 0;
×
4969
        }
4970

4971
        UNIT_VTABLE(u)->notify_pidref(u, &parent_pidref, &child_pidref);
3✔
4972

4973
        return 0;
4974
}
4975

4976
void manager_ref_console(Manager *m) {
160✔
4977
        assert(m);
160✔
4978

4979
        m->n_on_console++;
160✔
4980
}
160✔
4981

4982
void manager_unref_console(Manager *m) {
160✔
4983

4984
        assert(m->n_on_console > 0);
160✔
4985
        m->n_on_console--;
160✔
4986

4987
        if (m->n_on_console == 0)
160✔
4988
                m->no_console_output = false; /* unset no_console_output flag, since the console is definitely free now */
92✔
4989
}
160✔
4990

4991
void manager_override_log_level(Manager *m, int level) {
48✔
4992
        _cleanup_free_ char *s = NULL;
96✔
4993
        assert(m);
48✔
4994

4995
        if (!m->log_level_overridden) {
48✔
4996
                m->original_log_level = log_get_max_level();
17✔
4997
                m->log_level_overridden = true;
17✔
4998
        }
4999

5000
        (void) log_level_to_string_alloc(level, &s);
48✔
5001
        log_info("Setting log level to %s.", strna(s));
48✔
5002

5003
        log_set_max_level(level);
48✔
5004
}
48✔
5005

5006
void manager_restore_original_log_level(Manager *m) {
×
5007
        _cleanup_free_ char *s = NULL;
×
5008
        assert(m);
×
5009

5010
        if (!m->log_level_overridden)
×
5011
                return;
×
5012

5013
        (void) log_level_to_string_alloc(m->original_log_level, &s);
×
5014
        log_info("Restoring log level to original (%s).", strna(s));
×
5015

5016
        log_set_max_level(m->original_log_level);
×
5017
        m->log_level_overridden = false;
×
5018
}
5019

5020
void manager_override_log_target(Manager *m, LogTarget target) {
16✔
5021
        assert(m);
16✔
5022

5023
        if (!m->log_target_overridden) {
16✔
5024
                m->original_log_target = log_get_target();
11✔
5025
                m->log_target_overridden = true;
11✔
5026
        }
5027

5028
        log_info("Setting log target to %s.", log_target_to_string(target));
16✔
5029
        log_set_target(target);
16✔
5030
}
16✔
5031

5032
void manager_restore_original_log_target(Manager *m) {
×
5033
        assert(m);
×
5034

5035
        if (!m->log_target_overridden)
×
5036
                return;
5037

5038
        log_info("Restoring log target to original %s.", log_target_to_string(m->original_log_target));
×
5039

5040
        log_set_target(m->original_log_target);
×
5041
        m->log_target_overridden = false;
×
5042
}
5043

5044
ManagerTimestamp manager_timestamp_initrd_mangle(ManagerTimestamp s) {
3,272✔
5045
        if (in_initrd() &&
3,272✔
5046
            s >= MANAGER_TIMESTAMP_SECURITY_START &&
54✔
5047
            s <= MANAGER_TIMESTAMP_UNITS_LOAD_FINISH)
5048
                return s - MANAGER_TIMESTAMP_SECURITY_START + MANAGER_TIMESTAMP_INITRD_SECURITY_START;
54✔
5049
        return s;
5050
}
5051

5052
int manager_allocate_idle_pipe(Manager *m) {
17,250✔
5053
        int r;
17,250✔
5054

5055
        assert(m);
17,250✔
5056

5057
        if (m->idle_pipe[0] >= 0) {
17,250✔
5058
                assert(m->idle_pipe[1] >= 0);
13,960✔
5059
                assert(m->idle_pipe[2] >= 0);
13,960✔
5060
                assert(m->idle_pipe[3] >= 0);
13,960✔
5061
                return 0;
5062
        }
5063

5064
        assert(m->idle_pipe[1] < 0);
3,290✔
5065
        assert(m->idle_pipe[2] < 0);
3,290✔
5066
        assert(m->idle_pipe[3] < 0);
3,290✔
5067

5068
        r = RET_NERRNO(pipe2(m->idle_pipe + 0, O_NONBLOCK|O_CLOEXEC));
3,290✔
5069
        if (r < 0)
×
5070
                return r;
5071

5072
        r = RET_NERRNO(pipe2(m->idle_pipe + 2, O_NONBLOCK|O_CLOEXEC));
3,290✔
5073
        if (r < 0) {
×
5074
                safe_close_pair(m->idle_pipe + 0);
×
5075
                return r;
×
5076
        }
5077

5078
        return 1;
5079
}
5080

5081
void unit_defaults_init(UnitDefaults *defaults, RuntimeScope scope) {
1,139✔
5082
        assert(defaults);
1,139✔
5083
        assert(scope >= 0);
1,139✔
5084
        assert(scope < _RUNTIME_SCOPE_MAX);
1,139✔
5085

5086
        *defaults = (UnitDefaults) {
1,139✔
5087
                .std_output = EXEC_OUTPUT_JOURNAL,
5088
                .std_error = EXEC_OUTPUT_INHERIT,
5089
                .restart_usec = DEFAULT_RESTART_USEC,
5090
                .timeout_start_usec = manager_default_timeout(scope),
5091
                .timeout_stop_usec = manager_default_timeout(scope),
5092
                .timeout_abort_usec = manager_default_timeout(scope),
5093
                .timeout_abort_set = false,
5094
                .device_timeout_usec = manager_default_timeout(scope),
5095
                .start_limit = { DEFAULT_START_LIMIT_INTERVAL, DEFAULT_START_LIMIT_BURST },
5096

5097
                .memory_accounting = MEMORY_ACCOUNTING_DEFAULT,
5098
                .io_accounting = false,
5099
                .tasks_accounting = true,
5100
                .ip_accounting = false,
5101

5102
                .tasks_max = DEFAULT_TASKS_MAX,
5103
                .timer_accuracy_usec = 1 * USEC_PER_MINUTE,
5104

5105
                .memory_pressure_watch = CGROUP_PRESSURE_WATCH_AUTO,
5106
                .memory_pressure_threshold_usec = MEMORY_PRESSURE_DEFAULT_THRESHOLD_USEC,
5107

5108
                .oom_policy = OOM_STOP,
5109
                .oom_score_adjust_set = false,
5110
        };
5111
}
1,139✔
5112

5113
void unit_defaults_done(UnitDefaults *defaults) {
1,139✔
5114
        assert(defaults);
1,139✔
5115

5116
        defaults->smack_process_label = mfree(defaults->smack_process_label);
1,139✔
5117
        rlimit_free_all(defaults->rlimit);
1,139✔
5118
}
1,139✔
5119

5120
LogTarget manager_get_executor_log_target(Manager *m) {
2,208✔
5121
        assert(m);
2,208✔
5122

5123
        /* If journald is not available tell sd-executor to go to kmsg, as it might be starting journald */
5124
        if (!MANAGER_IS_TEST_RUN(m) && !manager_journal_is_running(m))
2,208✔
5125
                return LOG_TARGET_KMSG;
5126

5127
        return log_get_target();
1,797✔
5128
}
5129

5130
static const char* const manager_state_table[_MANAGER_STATE_MAX] = {
5131
        [MANAGER_INITIALIZING] = "initializing",
5132
        [MANAGER_STARTING]     = "starting",
5133
        [MANAGER_RUNNING]      = "running",
5134
        [MANAGER_DEGRADED]     = "degraded",
5135
        [MANAGER_MAINTENANCE]  = "maintenance",
5136
        [MANAGER_STOPPING]     = "stopping",
5137
};
5138

5139
DEFINE_STRING_TABLE_LOOKUP(manager_state, ManagerState);
76✔
5140

5141
static const char* const manager_objective_table[_MANAGER_OBJECTIVE_MAX] = {
5142
        [MANAGER_OK]          = "ok",
5143
        [MANAGER_EXIT]        = "exit",
5144
        [MANAGER_RELOAD]      = "reload",
5145
        [MANAGER_REEXECUTE]   = "reexecute",
5146
        [MANAGER_REBOOT]      = "reboot",
5147
        [MANAGER_SOFT_REBOOT] = "soft-reboot",
5148
        [MANAGER_POWEROFF]    = "poweroff",
5149
        [MANAGER_HALT]        = "halt",
5150
        [MANAGER_KEXEC]       = "kexec",
5151
        [MANAGER_SWITCH_ROOT] = "switch-root",
5152
};
5153

5154
DEFINE_STRING_TABLE_LOOKUP(manager_objective, ManagerObjective);
159✔
5155

5156
static const char* const manager_timestamp_table[_MANAGER_TIMESTAMP_MAX] = {
5157
        [MANAGER_TIMESTAMP_FIRMWARE]                 = "firmware",
5158
        [MANAGER_TIMESTAMP_LOADER]                   = "loader",
5159
        [MANAGER_TIMESTAMP_KERNEL]                   = "kernel",
5160
        [MANAGER_TIMESTAMP_INITRD]                   = "initrd",
5161
        [MANAGER_TIMESTAMP_USERSPACE]                = "userspace",
5162
        [MANAGER_TIMESTAMP_FINISH]                   = "finish",
5163
        [MANAGER_TIMESTAMP_SECURITY_START]           = "security-start",
5164
        [MANAGER_TIMESTAMP_SECURITY_FINISH]          = "security-finish",
5165
        [MANAGER_TIMESTAMP_GENERATORS_START]         = "generators-start",
5166
        [MANAGER_TIMESTAMP_GENERATORS_FINISH]        = "generators-finish",
5167
        [MANAGER_TIMESTAMP_UNITS_LOAD_START]         = "units-load-start",
5168
        [MANAGER_TIMESTAMP_UNITS_LOAD_FINISH]        = "units-load-finish",
5169
        [MANAGER_TIMESTAMP_UNITS_LOAD]               = "units-load",
5170
        [MANAGER_TIMESTAMP_INITRD_SECURITY_START]    = "initrd-security-start",
5171
        [MANAGER_TIMESTAMP_INITRD_SECURITY_FINISH]   = "initrd-security-finish",
5172
        [MANAGER_TIMESTAMP_INITRD_GENERATORS_START]  = "initrd-generators-start",
5173
        [MANAGER_TIMESTAMP_INITRD_GENERATORS_FINISH] = "initrd-generators-finish",
5174
        [MANAGER_TIMESTAMP_INITRD_UNITS_LOAD_START]  = "initrd-units-load-start",
5175
        [MANAGER_TIMESTAMP_INITRD_UNITS_LOAD_FINISH] = "initrd-units-load-finish",
5176
        [MANAGER_TIMESTAMP_SHUTDOWN_START]           = "shutdown-start",
5177
};
5178

5179
DEFINE_STRING_TABLE_LOOKUP(manager_timestamp, ManagerTimestamp);
5,359✔
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