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

systemd / systemd / 14630481637

23 Apr 2025 07:04PM UTC coverage: 72.178% (-0.002%) from 72.18%
14630481637

push

github

DaanDeMeyer
mkosi: Run clangd within the tools tree instead of the build container

Running within the build sandbox has a number of disadvantages:
- We have a separate clangd cache for each distribution/release combo
- It requires to build the full image before clangd can be used
- It breaks every time the image becomes out of date and requires a
  rebuild
- We can't look at system headers as we don't have the knowledge to map
  them from inside the build sandbox to the corresponding path on the host

Instead, let's have mkosi.clangd run clangd within the tools tree. We
already require building systemd for both the host and the target anyway,
and all the dependencies to build systemd are installed in the tools tree
already for that, as well as clangd since it's installed together with the
other clang tooling we install in the tools tree. Unlike the previous approach,
this approach only requires the mkosi tools tree to be built upfront, which has
a much higher chance of not invalidating its cache. We can also trivially map
system header lookups from within the sandbox to the path within mkosi.tools
on the host so that starts working as well.

297054 of 411557 relevant lines covered (72.18%)

686269.58 hits per line

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

80.83
/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 "core-varlink.h"
38
#include "creds-util.h"
39
#include "daemon-util.h"
40
#include "dbus-job.h"
41
#include "dbus-manager.h"
42
#include "dbus-unit.h"
43
#include "dbus.h"
44
#include "dirent-util.h"
45
#include "dynamic-user.h"
46
#include "env-util.h"
47
#include "escape.h"
48
#include "event-util.h"
49
#include "exec-util.h"
50
#include "execute.h"
51
#include "exit-status.h"
52
#include "fd-util.h"
53
#include "fileio.h"
54
#include "generator-setup.h"
55
#include "hashmap.h"
56
#include "initrd-util.h"
57
#include "inotify-util.h"
58
#include "install.h"
59
#include "io-util.h"
60
#include "iovec-util.h"
61
#include "label-util.h"
62
#include "load-fragment.h"
63
#include "locale-setup.h"
64
#include "log.h"
65
#include "macro.h"
66
#include "manager.h"
67
#include "manager-dump.h"
68
#include "manager-serialize.h"
69
#include "memory-util.h"
70
#include "mkdir-label.h"
71
#include "mount-util.h"
72
#include "notify-recv.h"
73
#include "os-util.h"
74
#include "parse-util.h"
75
#include "path-lookup.h"
76
#include "path-util.h"
77
#include "plymouth-util.h"
78
#include "pretty-print.h"
79
#include "process-util.h"
80
#include "psi-util.h"
81
#include "ratelimit.h"
82
#include "rlimit-util.h"
83
#include "rm-rf.h"
84
#include "selinux-util.h"
85
#include "serialize.h"
86
#include "signal-util.h"
87
#include "socket-util.h"
88
#include "special.h"
89
#include "stat-util.h"
90
#include "string-table.h"
91
#include "string-util.h"
92
#include "strv.h"
93
#include "strxcpyx.h"
94
#include "sysctl-util.h"
95
#include "syslog-util.h"
96
#include "taint.h"
97
#include "terminal-util.h"
98
#include "time-util.h"
99
#include "transaction.h"
100
#include "uid-range.h"
101
#include "umask-util.h"
102
#include "unit-name.h"
103
#include "user-util.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,420✔
141
        usec_t timeout;
4,420✔
142

143
        if (MANAGER_IS_USER(m))
4,420✔
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,607✔
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,420✔
154
}
155

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

159
        if (!m->confirm_spawn)
3,171✔
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,171✔
166
        usec_t next;
3,171✔
167
        int r;
3,171✔
168

169
        assert(m);
3,171✔
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,171✔
175
                return;
176

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

180
        next = manager_watch_jobs_next_time(m);
754✔
181
        r = sd_event_add_time(
754✔
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)
754✔
188
                return;
189

190
        (void) sd_event_source_set_description(m->jobs_in_progress_event_source, "manager-jobs-in-progress");
754✔
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)
403✔
198
                        manager_set_show_status(m, SHOW_STATUS_TEMPORARY, reason);
×
199
        } else {
200
                if (m->show_status == SHOW_STATUS_TEMPORARY)
179✔
201
                        manager_set_show_status(m, SHOW_STATUS_AUTO, reason);
×
202
        }
203
}
582✔
204

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

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

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

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

219
        HASHMAP_FOREACH(j, m->jobs)
403✔
220
                if (j->state == JOB_RUNNING && counter++ == print_nr)
403✔
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);
403✔
226
        assert(j);
403✔
227

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

233
        m->jobs_in_progress_iteration++;
403✔
234

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

239
        (void) job_get_timeout(j, &timeout);
403✔
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);
403✔
247

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

251
        if (m->status_unit_format == STATUS_UNIT_FORMAT_DESCRIPTION)
403✔
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);
403✔
262

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

273
        (void) sd_notifyf(/* unset_environment= */ false,
403✔
274
                          "STATUS=%sUser job %s/%s running (%s / %s)...",
275
                          job_of_n,
276
                          ident, job_type_to_string(j->type),
403✔
277
                          time, limit);
278
        m->status_ready = false;
403✔
279
}
403✔
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) {
876✔
318
        assert(m);
876✔
319

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

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

327
        assert(m);
6,527✔
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,527✔
332
                return 0;
333

334
        if (!m->ask_password_event_source) {
6,527✔
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,527✔
369
}
370

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

374
        assert(m);
427✔
375

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

379
        if (m->idle_pipe[2] < 0)
43✔
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);
43✔
383
        if (r < 0)
43✔
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");
43✔
387

388
        return 0;
43✔
389
}
390

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

394
        m->idle_pipe_event_source = sd_event_source_disable_unref(m->idle_pipe_event_source);
2,706✔
395

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

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

403
        assert(m);
249✔
404

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

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

410
        r = event_add_time_change(m->event, &m->time_change_event_source, manager_dispatch_time_change_fd, m);
237✔
411
        if (r < 0)
237✔
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);
237✔
416
        if (r < 0)
237✔
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.");
237✔
420

421
        return 0;
422
}
423

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

428
        assert(m);
264✔
429

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

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

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

445
        return changed;
264✔
446
}
447

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

452
        assert(m);
257✔
453

454
        if (MANAGER_IS_TEST_RUN(m))
257✔
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",
245✔
469
                                 IN_ATTRIB|IN_MOVE_SELF|IN_CLOSE_WRITE|IN_DONT_FOLLOW, manager_dispatch_timezone_change, m);
470
        if (r == -ENOENT) {
245✔
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)
245✔
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);
245✔
483
        if (r < 0)
245✔
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);
245✔
487
        m->timezone_change_event_source = TAKE_PTR(new_event);
245✔
488

489
        return 0;
245✔
490
}
491

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

495
        assert(m);
246✔
496

497
        if (!MANAGER_IS_SYSTEM(m) || MANAGER_IS_TEST_RUN(m))
246✔
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");
246✔
513
        }
514

515
        return 0;
516
}
517

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

526
        assert(m);
246✔
527

528
        assert_se(sigaction(SIGCHLD, &sa, NULL) == 0);
246✔
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);
246✔
534
        sigset_add_many(&mask,
246✔
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);
246✔
579

580
        m->signal_fd = signalfd(-1, &mask, SFD_NONBLOCK|SFD_CLOEXEC);
246✔
581
        if (m->signal_fd < 0)
246✔
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);
246✔
585
        if (r < 0)
246✔
586
                return r;
587

588
        (void) sd_event_source_set_description(m->signal_event_source, "manager-signal");
246✔
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);
246✔
595
        if (r < 0)
246✔
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,
246✔
607
                         "X_SYSTEMD_SIGNALS_LEVEL=2");
608

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

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

614
        /* Let's remove some environment variables that we need ourselves to communicate with our clients */
615
        strv_env_unset_many(
1,215✔
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,215✔
655
}
656

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

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

662
        if (MANAGER_IS_SYSTEM(m)) {
957✔
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());
588✔
669
                if (!path)
588✔
670
                        return log_oom();
×
671

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

675
                /* Import locale variables LC_*= from configuration */
676
                (void) locale_setup(&m->transient_environment);
588✔
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);
369✔
682
                if (!m->transient_environment)
369✔
683
                        return log_oom();
×
684

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

689
                if (strv_env_replace_consume(&m->transient_environment, path) < 0)
369✔
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,
369✔
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);
957✔
704
        return 0;
957✔
705
}
706

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

713
        static const struct table_entry paths_system[_EXEC_DIRECTORY_TYPE_MAX] = {
697✔
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] = {
697✔
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);
697✔
730

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

734
        for (ExecDirectoryType i = 0; i < _EXEC_DIRECTORY_TYPE_MAX; i++) {
4,182✔
735
                r = sd_path_lookup(p[i].type, p[i].suffix, &m->prefix[i]);
3,485✔
736
                if (r < 0)
3,485✔
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) {
723✔
745
        m->unit_id_map = hashmap_free(m->unit_id_map);
723✔
746
        m->unit_name_map = hashmap_free(m->unit_name_map);
723✔
747
        m->unit_path_cache = set_free(m->unit_path_cache);
723✔
748
        m->unit_cache_timestamp_hash = 0;
723✔
749
}
723✔
750

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

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

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

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

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

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

771
        return 0;
697✔
772
}
773

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

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

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

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

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

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

794
        return 0;
246✔
795
}
796

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

800
        assert(m);
509✔
801

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

804
        r = sd_event_add_memory_pressure(m->event, &m->memory_pressure_event_source, NULL, NULL);
509✔
805
        if (r < 0)
509✔
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) {
509✔
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(
509✔
812
                                m->memory_pressure_event_source,
813
                                m->defaults.memory_pressure_threshold_usec,
814
                                MEMORY_PRESSURE_DEFAULT_WINDOW_USEC);
815
                if (r < 0)
509✔
816
                        log_warning_errno(r, "Failed to adjust memory pressure threshold, ignoring: %m");
12✔
817
        }
818

819
        return 0;
509✔
820
}
821

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

826
        assert(m);
697✔
827

828
        r = get_credentials_dir(&e);
697✔
829
        if (r < 0) {
697✔
830
                if (r != -ENXIO)
637✔
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;
697✔
836
        }
837

838
        r = get_encrypted_credentials_dir(&e);
697✔
839
        if (r < 0) {
697✔
840
                if (r != -ENXIO)
697✔
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) {
938✔
852
        assert(m);
938✔
853

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

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

860
        if (MANAGER_IS_FINISHED(m) || m->n_installed_jobs == 0)
34✔
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) {
302,974✔
867
        const Job *x = a, *y = b;
302,974✔
868

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

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

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

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

883
        *m = (Manager) {
697✔
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);
697✔
924

925
#if ENABLE_EFI
926
        if (MANAGER_IS_SYSTEM(m) && detect_container() <= 0)
697✔
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 };
697✔
934

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1002
#if HAVE_LIBBPF
1003
                if (MANAGER_IS_SYSTEM(m) && bpf_restrict_fs_supported(/* initialize = */ true)) {
246✔
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) {
697✔
1012
                if (MANAGER_IS_SYSTEM(m))
234✔
1013
                        r = mkdir_label("/run/systemd/units", 0755);
60✔
1014
                else {
1015
                        _cleanup_free_ char *units_path = NULL;
174✔
1016
                        r = xdg_user_runtime_dir("/systemd/units", &units_path);
174✔
1017
                        if (r < 0)
174✔
1018
                                return r;
×
1019

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

1026
        if (!FLAGS_SET(test_run_flags, MANAGER_TEST_DONT_OPEN_EXECUTOR)) {
697✔
1027
                m->executor_fd = pin_callout_binary(SYSTEMD_EXECUTOR_BINARY_PATH, &m->executor_path);
253✔
1028
                if (m->executor_fd < 0)
253✔
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);
253✔
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);
697✔
1038

1039
        return 0;
697✔
1040
}
1041

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

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

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

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

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

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

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

1067
                r = sockaddr_un_set_path(&sa.un, m->notify_socket);
203✔
1068
                if (r < 0)
203✔
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;
203✔
1072

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

1075
                r = mac_selinux_bind(fd, &sa.sa, sa_len);
203✔
1076
                if (r < 0)
203✔
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);
203✔
1080
                if (r < 0)
203✔
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);
203✔
1085
                if (r < 0 && r != -ENOPROTOOPT)
203✔
1086
                        log_warning_errno(r, "Failed to enable SO_PASSPIDFD for notify socket, ignoring: %m");
×
1087

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

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

1093
        if (!m->notify_event_source) {
260✔
1094
                r = sd_event_add_io(m->event, &m->notify_event_source, m->notify_fd, EPOLLIN, manager_dispatch_notify_fd, m);
260✔
1095
                if (r < 0)
260✔
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);
260✔
1101
                if (r < 0)
260✔
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");
260✔
1105
        }
1106

1107
        return 0;
1108
}
1109

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

1113
        assert(m);
721✔
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) {
721✔
1135

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

1140
                if (socketpair(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC, 0, m->user_lookup_fds) < 0)
664✔
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);
664✔
1144
        }
1145

1146
        if (!m->user_lookup_event_source) {
721✔
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);
721✔
1148
                if (r < 0)
721✔
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);
721✔
1154
                if (r < 0)
721✔
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");
721✔
1158
        }
1159

1160
        return 0;
1161
}
1162

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

1166
        assert(m);
721✔
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) {
721✔
1172
                m->handoff_timestamp_event_source = sd_event_source_disable_unref(m->handoff_timestamp_event_source);
664✔
1173
                safe_close_pair(m->handoff_timestamp_fds);
664✔
1174

1175
                if (socketpair(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC, 0, m->handoff_timestamp_fds) < 0)
664✔
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);
664✔
1180

1181
                r = setsockopt_int(m->handoff_timestamp_fds[0], SOL_SOCKET, SO_PASSCRED, true);
664✔
1182
                if (r < 0)
664✔
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);
664✔
1187
                if (r < 0)
664✔
1188
                        return log_error_errno(r, "Failed to make handoff timestamp socket O_NONBLOCK: %m");
×
1189
        }
1190

1191
        if (!m->handoff_timestamp_event_source) {
721✔
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);
721✔
1193
                if (r < 0)
721✔
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);
721✔
1197
                if (r < 0)
721✔
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");
721✔
1201
        }
1202

1203
        return 0;
1204
}
1205

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

1209
        assert(m);
721✔
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) {
721✔
1215
                m->pidref_event_source = sd_event_source_disable_unref(m->pidref_event_source);
695✔
1216
                safe_close_pair(m->pidref_transport_fds);
695✔
1217

1218
                if (socketpair(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC, 0, m->pidref_transport_fds) < 0)
695✔
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);
695✔
1223

1224
                r = setsockopt_int(m->pidref_transport_fds[0], SOL_SOCKET, SO_PASSCRED, true);
695✔
1225
                if (r < 0)
695✔
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);
695✔
1229
                if (ERRNO_IS_NEG_NOT_SUPPORTED(r))
695✔
1230
                        log_debug("SO_PASSPIDFD is not supported for pidref socket, ignoring.");
×
1231
                else if (r < 0)
695✔
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);
695✔
1236
                if (r < 0)
695✔
1237
                        return log_error_errno(r, "Failed to make pidref socket O_NONBLOCK: %m");
×
1238
        }
1239

1240
        if (!m->pidref_event_source) {
721✔
1241
                r = sd_event_add_io(m->event, &m->pidref_event_source, m->pidref_transport_fds[0], EPOLLIN, manager_dispatch_pidref_transport_fd, m);
695✔
1242
                if (r < 0)
695✔
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);
695✔
1246
                if (r < 0)
695✔
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");
695✔
1250
        }
1251

1252
        return 0;
1253
}
1254

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

1259
        assert(m);
183,240✔
1260

1261
        while ((u = m->cleanup_queue)) {
216,746✔
1262
                assert(u->in_cleanup_queue);
33,506✔
1263

1264
                unit_free(u);
33,506✔
1265
                n++;
33,506✔
1266
        }
1267

1268
        return n;
183,240✔
1269
}
1270

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

1275
        assert(m);
170,765✔
1276

1277
        while ((u = LIST_POP(release_resources_queue, m->release_resources_queue))) {
171,934✔
1278
                assert(u->in_release_resources_queue);
1,169✔
1279
                u->in_release_resources_queue = false;
1,169✔
1280

1281
                n++;
1,169✔
1282

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

1286
        return n;
170,765✔
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) {
67,358✔
1298
        Unit *other;
67,358✔
1299

1300
        u->gc_marker = gc_marker + GC_OFFSET_GOOD;
67,358✔
1301

1302
        /* Recursively mark referenced units as GOOD as well */
1303
        UNIT_FOREACH_DEPENDENCY(other, u, UNIT_ATOM_REFERENCES)
487,748✔
1304
                if (other->gc_marker == gc_marker + GC_OFFSET_UNSURE)
270,226✔
1305
                        unit_gc_mark_good(other, gc_marker);
1,638✔
1306
}
67,358✔
1307

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

1312
        assert(u);
117,906✔
1313

1314
        if (IN_SET(u->gc_marker - gc_marker,
117,906✔
1315
                   GC_OFFSET_GOOD, GC_OFFSET_BAD, GC_OFFSET_UNSURE, GC_OFFSET_IN_PATH))
1316
                return;
52,186✔
1317

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

1321
        if (!unit_may_gc(u))
100,863✔
1322
                goto good;
44,942✔
1323

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

1326
        is_bad = true;
55,921✔
1327

1328
        UNIT_FOREACH_DEPENDENCY(other, u, UNIT_ATOM_REFERENCED_BY) {
119,329✔
1329
                unit_gc_sweep(other, gc_marker);
25,663✔
1330

1331
                if (other->gc_marker == gc_marker + GC_OFFSET_GOOD)
25,663✔
1332
                        goto good;
20,778✔
1333

1334
                if (other->gc_marker != gc_marker + GC_OFFSET_BAD)
4,885✔
1335
                        is_bad = false;
4,294✔
1336
        }
1337

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

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

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

1348
        if (is_bad)
35,143✔
1349
                goto bad;
33,499✔
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,644✔
1354
        unit_add_to_gc_queue(u);
1,644✔
1355
        return;
1356

1357
bad:
33,499✔
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;
33,499✔
1361
        unit_add_to_cleanup_queue(u);
33,499✔
1362
        return;
1363

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

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

1372
        assert(m);
196,577✔
1373

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

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

1380
        gc_marker = m->gc_marker;
196,577✔
1381

1382
        while ((u = LIST_POP(gc_queue, m->gc_unit_queue))) {
288,422✔
1383
                assert(u->in_gc_queue);
91,845✔
1384

1385
                unit_gc_sweep(u, gc_marker);
91,845✔
1386

1387
                u->in_gc_queue = false;
91,845✔
1388

1389
                n++;
91,845✔
1390

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

1400
        return n;
196,577✔
1401
}
1402

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

1407
        assert(m);
196,584✔
1408

1409
        while ((j = LIST_POP(gc_queue, m->gc_job_queue))) {
196,591✔
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;
196,584✔
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) {
13✔
1442
        int r;
13✔
1443

1444
        assert(u);
13✔
1445

1446
        if (ratelimit_below(&u->auto_start_stop_ratelimit))
13✔
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) {
170,896✔
1468
        unsigned n = 0;
170,896✔
1469
        Unit *u;
170,896✔
1470
        int r;
170,896✔
1471

1472
        assert(m);
170,896✔
1473

1474
        while ((u = LIST_POP(stop_when_unneeded_queue, m->stop_when_unneeded_queue))) {
171,080✔
1475
                _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
184✔
1476

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

1480
                n++;
184✔
1481

1482
                if (!unit_is_unneeded(u))
184✔
1483
                        continue;
174✔
1484

1485
                log_unit_debug(u, "Unit is not needed anymore.");
10✔
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);
10✔
1491
                if (r <= 0) {
10✔
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);
10✔
1500
                if (r < 0)
10✔
1501
                        log_unit_warning_errno(u, r, "Failed to enqueue stop job, ignoring: %s", bus_error_message(&error, r));
×
1502
        }
1503

1504
        return n;
170,896✔
1505
}
1506

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

1512
        assert(m);
171,039✔
1513

1514
        while ((u = LIST_POP(start_when_upheld_queue, m->start_when_upheld_queue))) {
171,039✔
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;
171,039✔
1546
}
1547

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

1553
        assert(m);
171,039✔
1554

1555
        while ((u = LIST_POP(stop_when_bound_queue, m->stop_when_bound_queue))) {
171,304✔
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;
171,039✔
1587
}
1588

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

1592
        assert(m);
723✔
1593

1594
        while ((u = hashmap_first(m->units)))
45,586✔
1595
                unit_free(u);
44,863✔
1596

1597
        manager_dispatch_cleanup_queue(m);
723✔
1598

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

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

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

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

1629
        manager_clear_jobs_and_units(m);
697✔
1630

1631
        for (UnitType c = 0; c < _UNIT_TYPE_MAX; c++)
8,364✔
1632
                if (unit_vtable[c]->shutdown)
7,667✔
1633
                        unit_vtable[c]->shutdown(m);
2,788✔
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));
697✔
1637

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

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

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

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

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

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

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

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

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

1679
        manager_close_ask_password(m);
697✔
1680

1681
        manager_close_idle_pipe(m);
697✔
1682

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1734
        if (FLAGS_SET(m->test_run_flags, MANAGER_TEST_RUN_MINIMAL))
721✔
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,264✔
1739
                if (!unit_type_supported(c)) {
2,992✔
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,509✔
1745
                        unit_vtable[c]->enumerate_perpetual(m);
816✔
1746
        }
1747
}
1748

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

1752
        if (FLAGS_SET(m->test_run_flags, MANAGER_TEST_RUN_MINIMAL))
721✔
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,264✔
1757
                if (!unit_type_supported(c)) {
2,992✔
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,509✔
1763
                        unit_vtable[c]->enumerate(m);
494✔
1764
        }
1765

1766
        manager_dispatch_load_queue(m);
272✔
1767
}
1768

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

1774
        assert(m);
721✔
1775

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

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

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

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

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

1795
        assert(m);
721✔
1796

1797
        log_debug("Invoking unit catchup() handlers%s", glyph(GLYPH_ELLIPSIS));
1,040✔
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) {
40,798✔
1801

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

1806
                unit_catchup(u);
39,213✔
1807
        }
1808
}
721✔
1809

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

1813
        assert(m);
695✔
1814

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

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

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

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

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

1830
        assert(m);
60,236✔
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))
60,236✔
1837
                return false;
1838

1839
        u = manager_get_unit(m, SPECIAL_DBUS_SOCKET);
59,386✔
1840
        if (!u)
59,386✔
1841
                return false;
1842
        if ((deserialized ? SOCKET(u)->deserialized_state : SOCKET(u)->state) != SOCKET_RUNNING)
99,438✔
1843
                return false;
1844

1845
        u = manager_get_unit(m, SPECIAL_DBUS_SERVICE);
3,974✔
1846
        if (!u)
3,974✔
1847
                return false;
1848
        if (!IN_SET((deserialized ? SERVICE(u)->deserialized_state : SERVICE(u)->state),
7,948✔
1849
                    SERVICE_RUNNING,
1850
                    SERVICE_MOUNTING,
1851
                    SERVICE_RELOAD,
1852
                    SERVICE_RELOAD_NOTIFY,
1853
                    SERVICE_RELOAD_SIGNAL))
1854
                return false;
262✔
1855

1856
        return true;
1857
}
1858

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

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

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

1868
        /* If we are in --user mode also connect to the system bus now */
1869
        if (MANAGER_IS_USER(m))
234✔
1870
                (void) bus_init_system(m);
174✔
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))) {
234✔
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) {
695✔
1882
        int r;
695✔
1883

1884
        assert(m);
695✔
1885

1886
        if (m->first_boot <= 0)
695✔
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) {
721✔
1908
        assert(m);
721✔
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 */
721✔
1913

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

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

1921
        /* Create a file which will indicate when the manager started loading units the last time. */
1922
        if (MANAGER_IS_SYSTEM(m))
721✔
1923
                (void) touch_file("/run/systemd/systemd-units-load", false,
528✔
1924
                        m->timestamps[MANAGER_TIMESTAMP_UNITS_LOAD].realtime ?: now(CLOCK_REALTIME),
528✔
1925
                        UID_INVALID, GID_INVALID, 0444);
1926
}
721✔
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) {
851✔
1935
        if (*m) {
851✔
1936
                assert((*m)->n_reloading > 0);
161✔
1937
                (*m)->n_reloading--;
161✔
1938
        }
1939
}
851✔
1940

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

1944
        assert(m);
695✔
1945

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

1950
        r = mkdir_label(d, 0755);
695✔
1951
        if (r < 0 && r != -EEXIST)
695✔
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) {
695✔
1958
        int r;
695✔
1959

1960
        assert(m);
695✔
1961

1962
        r = manager_make_runtime_dir(m);
695✔
1963
        if (r < 0)
695✔
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,
695✔
1969
                                      MANAGER_IS_TEST_RUN(m) ? LOOKUP_PATHS_TEMPORARY_GENERATED : 0,
695✔
1970
                                      root);
1971
        if (r < 0)
695✔
1972
                return r;
1973

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

1982
        manager_preset_all(m);
695✔
1983

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

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

1990
                /* Make sure we don't have a left-over from a previous run */
1991
                if (!serialization)
695✔
1992
                        (void) rm_rf(m->lookup_paths.transient, 0);
664✔
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));
695✔
2001
                manager_enumerate_perpetual(m);
695✔
2002
                manager_enumerate(m);
695✔
2003
                dual_timestamp_now(m->timestamps + manager_timestamp_initrd_mangle(MANAGER_TIMESTAMP_UNITS_LOAD_FINISH));
695✔
2004

2005
                /* Second, deserialize if there is something to deserialize */
2006
                if (serialization) {
695✔
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) {
695✔
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)
695✔
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);
695✔
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);
695✔
2033
                if (r < 0)
695✔
2034
                        /* No sense to continue without notifications, our children would fail anyway. */
2035
                        return r;
2036

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

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

2047
                r = manager_setup_pidref_transport_fd(m);
695✔
2048
                if (r < 0)
695✔
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);
695✔
2054

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

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

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

2065
                if (serialization)
695✔
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);
695✔
2072

2073
        manager_set_switching_root(m, false);
695✔
2074

2075
        return 0;
695✔
2076
}
2077

2078
int manager_add_job_full(
1,307✔
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,307✔
2089
        int r;
1,307✔
2090

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

2097
        if (mode == JOB_ISOLATE && type != JOB_START)
1,307✔
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,307✔
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,283✔
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,283✔
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,283✔
2110

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

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

2117
        r = transaction_add_job_and_dependencies(
3,849✔
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,283✔
2124
                        (mode == JOB_IGNORE_DEPENDENCIES ? TRANSACTION_IGNORE_ORDER : 0) |
1,283✔
2125
                        (mode == JOB_RESTART_DEPENDENCIES ? TRANSACTION_PROPAGATE_START_AS_RESTART : 0) |
2,561✔
2126
                        extra_flags,
2127
                        error);
2128
        if (r < 0)
1,283✔
2129
                return r;
2130

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

2137
        if (mode == JOB_TRIGGERING) {
1,275✔
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,275✔
2144
        if (r < 0)
1,275✔
2145
                return r;
2146

2147
        log_unit_debug(unit,
1,271✔
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,271✔
2152
                *ret = tr->anchor_job;
692✔
2153

2154
        tr = transaction_free(tr);
1,271✔
2155
        return 0;
1,271✔
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) {
189✔
2159
        Unit *unit = NULL;  /* just to appease gcc, initialization is not really necessary */
189✔
2160
        int r;
189✔
2161

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

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

2172
        return manager_add_job_full(m, type, unit, mode, /* extra_flags = */ 0, affected_jobs, e, ret);
189✔
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) {
174✔
2176
        _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
174✔
2177
        int r;
174✔
2178

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

2184
        r = manager_add_job_by_name(m, type, name, mode, affected_jobs, &error, ret);
174✔
2185
        if (r < 0)
174✔
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) {
12,112✔
2192
        int r;
12,112✔
2193
        _cleanup_(transaction_abort_and_freep) Transaction *tr = NULL;
12,112✔
2194

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

2200
        tr = transaction_new(mode == JOB_REPLACE_IRREVERSIBLY);
12,112✔
2201
        if (!tr)
12,112✔
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);
12,112✔
2206
        if (r < 0)
12,112✔
2207
                return r;
2208

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

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

2220
        tr = transaction_free(tr);
12,112✔
2221
        return 0;
12,112✔
2222
}
2223

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

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

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

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

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

2241
        assert(m);
209,661✔
2242

2243
        while ((u = LIST_POP(target_deps_queue, m->target_deps_queue))) {
266,177✔
2244
                _cleanup_free_ Unit **targets = NULL;
56,516✔
2245
                int n_targets;
56,516✔
2246

2247
                assert(u->in_target_deps_queue);
56,516✔
2248

2249
                u->in_target_deps_queue = false;
56,516✔
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);
56,516✔
2255
                if (n_targets < 0)
56,516✔
2256
                        return n_targets;
2257

2258
                FOREACH_ARRAY(i, targets, n_targets) {
111,876✔
2259
                        r = unit_add_default_target_dependency(u, *i);
55,360✔
2260
                        if (r < 0)
55,360✔
2261
                                return r;
2262
                }
2263
        }
2264

2265
        return r;
2266
}
2267

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

2272
        assert(m);
238,357✔
2273

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

2278
        m->dispatching_load_queue = true;
209,661✔
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)) {
288,155✔
2284
                assert(u->in_load_queue);
78,494✔
2285

2286
                unit_load(u);
78,494✔
2287
                n++;
78,494✔
2288
        }
2289

2290
        m->dispatching_load_queue = false;
209,661✔
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);
209,661✔
2295

2296
        return n;
209,661✔
2297
}
2298

2299
bool manager_unit_cache_should_retry_load(Unit *u) {
366,246✔
2300
        assert(u);
366,246✔
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)
366,246✔
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,774✔
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,774✔
2315
}
2316

2317
int manager_load_unit_prepare(
416,919✔
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;
416,919✔
2326
        int r;
416,919✔
2327

2328
        assert(m);
416,919✔
2329
        assert(ret);
416,919✔
2330
        assert(name || path);
416,919✔
2331

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

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

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

2344
                name = nbuf;
439✔
2345
        }
2346

2347
        UnitType t = unit_name_to_type(name);
416,919✔
2348

2349
        if (t == _UNIT_TYPE_INVALID || !unit_name_is_valid(name, UNIT_NAME_PLAIN|UNIT_NAME_INSTANCE)) {
416,919✔
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);
416,919✔
2357
        if (unit) {
416,919✔
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))
365,423✔
2368
                        unit->load_state = UNIT_STUB;
×
2369
                else {
2370
                        *ret = unit;
365,423✔
2371
                        return 0;  /* The unit was already loaded */
365,423✔
2372
                }
2373
        } else {
2374
                unit = cleanup_unit = unit_new(m, unit_vtable[t]->object_size);
51,496✔
2375
                if (!unit)
51,496✔
2376
                        return -ENOMEM;
2377
        }
2378

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

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

2389
        unit_add_to_load_queue(unit);
51,496✔
2390
        unit_add_to_dbus_queue(unit);
51,496✔
2391
        unit_add_to_gc_queue(unit);
51,496✔
2392

2393
        *ret = unit;
51,496✔
2394
        TAKE_PTR(cleanup_unit);
51,496✔
2395

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

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

2407
        assert(m);
396,693✔
2408
        assert(ret);
396,693✔
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);
396,693✔
2413
        if (r <= 0)
396,693✔
2414
                return r;
2415

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

2422
int manager_load_startable_unit_or_warn(
683✔
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;
683✔
2431
        Unit *unit;
683✔
2432
        int r;
683✔
2433

2434
        r = manager_load_unit(m, name, path, &error, &unit);
683✔
2435
        if (r < 0)
683✔
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);
683✔
2441
        if (r < 0)
683✔
2442
                return log_error_errno(r, "%s", bus_error_message(&error, r));
9✔
2443

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

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

2451
        assert(m);
429✔
2452

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

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

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

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

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

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

2476
        assert(source);
5,487✔
2477

2478
        while ((j = prioq_peek(m->run_queue))) {
52,496✔
2479
                assert(j->installed);
47,009✔
2480
                assert(j->in_run_queue);
47,009✔
2481

2482
                (void) job_run_and_invalidate(j);
47,009✔
2483
        }
2484

2485
        if (m->n_running_jobs > 0)
5,487✔
2486
                manager_watch_jobs_in_progress(m);
3,171✔
2487

2488
        if (m->n_on_console > 0)
5,487✔
2489
                manager_watch_idle_pipe(m);
427✔
2490

2491
        return 1;
5,487✔
2492
}
2493

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

2497
        assert(m);
49,195✔
2498

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

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

2511
        assert(m);
169,741✔
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)
169,741✔
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)
169,684✔
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)
6,940✔
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)) {
30,477✔
2542

2543
                assert(u->in_dbus_queue);
23,480✔
2544

2545
                bus_unit_send_change_signal(u);
23,480✔
2546
                n++;
23,480✔
2547

2548
                if (budget != UINT_MAX)
23,480✔
2549
                        budget--;
11,269✔
2550
        }
2551

2552
        while (budget != 0 && (j = m->dbus_job_queue)) {
35,312✔
2553
                assert(j->in_dbus_queue);
28,315✔
2554

2555
                bus_job_send_change_signal(j);
28,315✔
2556
                n++;
28,315✔
2557

2558
                if (budget != UINT_MAX)
28,315✔
2559
                        budget--;
27,934✔
2560
        }
2561

2562
        if (m->send_reloading_done) {
6,997✔
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) {
6,997✔
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,477✔
2577

2578
        /* nothing else must be sent when using BARRIER=1 */
2579
        if (strv_contains(tags, "BARRIER=1")) {
3,477✔
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,794✔
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,794✔
2601
        assert(u);
6,794✔
2602
        assert(pidref_is_set(pidref));
6,794✔
2603
        assert(ucred);
6,794✔
2604
        assert(pidref->pid == ucred->pid);
6,794✔
2605
        assert(tags);
6,794✔
2606

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

2611
        if (UNIT_VTABLE(u)->notify_message)
3,463✔
2612
                UNIT_VTABLE(u)->notify_message(u, pidref, ucred, tags, fds);
3,463✔
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,633✔
2624
        /* Determine array of every unit that is interested in the specified process */
2625

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

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

2634
        size_t n = 0;
10,633✔
2635
        if (u1)
10,633✔
2636
                n++;
10,631✔
2637
        if (u2)
10,633✔
2638
                n++;
7,543✔
2639
        if (array)
10,633✔
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" */
10,633✔
2644

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

2648
                if (n > 0) {
10,633✔
2649
                        units = new(Unit*, n + 1);
10,631✔
2650
                        if (!units)
10,631✔
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,631✔
2658
                        if (u1)
10,631✔
2659
                                units[i++] = u1;
10,631✔
2660
                        if (u2)
10,631✔
2661
                                units[i++] = u2;
7,543✔
2662
                        if (array)
10,631✔
2663
                                for (size_t j = 0; array[j]; j++)
×
2664
                                        units[i++] = array[j];
×
2665
                        assert(i == n);
10,631✔
2666

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

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

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

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

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

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

2690
        _cleanup_strv_free_ char **tags = NULL;
3,477✔
2691
        r = notify_recv_with_fds_strv(m->notify_fd, &tags, &ucred, &pidref, &fds);
3,477✔
2692
        if (r == -EAGAIN)
3,477✔
2693
                return 0;
2694
        if (r < 0)
3,477✔
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,477✔
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,465✔
2709

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

2713
        int n_array = manager_get_units_for_pidref(m, &pidref, &array);
3,465✔
2714
        if (n_array < 0) {
3,465✔
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,465✔
2719
                log_debug("Cannot find unit for notify message of PID "PID_FMT", ignoring.", pidref.pid);
2✔
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,257✔
2724
                        manager_invoke_notify_message(m, *u, &pidref, &ucred, tags, fds);
6,794✔
2725

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

2729
        return 0;
2730
}
2731

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

2737
        assert(m);
7,018✔
2738
        assert(u);
7,018✔
2739
        assert(si);
7,018✔
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,018✔
2743
                return;
2,026✔
2744
        u->sigchldgen = m->sigchldgen;
4,992✔
2745

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

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

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

2758
        assert(source);
8,623✔
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,623✔
2764

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

2768
                goto turn_off;
80✔
2769
        }
2770

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

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

2778
                log_debug("Child "PID_FMT" (%s) died (code=%s, status=%i/%s)",
9,841✔
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,987✔
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,987✔
2792

2793
                /* And now figure out the units this belongs to, there might be multiple... */
2794
                _cleanup_free_ Unit **array = NULL;
4,987✔
2795
                int n_array = manager_get_units_for_pidref(m, &pidref, &array);
4,987✔
2796
                if (n_array < 0)
4,987✔
2797
                        log_warning_errno(n_array, "Failed to get units for process " PID_FMT ", ignoring: %m", si.si_pid);
×
2798
                else if (n_array == 0)
4,987✔
2799
                        log_debug("Got SIGCHLD for process " PID_FMT " we weren't interested in, ignoring.", si.si_pid);
4,987✔
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,987✔
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,987✔
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)
12,005✔
2812
                                manager_invoke_sigchld_event(m, *u, &si);
7,018✔
2813
                }
2814
        }
2815

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

2822
        return 0;
2823

2824
turn_off:
3,636✔
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,636✔
2828
        if (r < 0)
3,636✔
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) {
173✔
2835
        Job *job;
173✔
2836

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

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

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

2844
        (void) sd_notifyf(/* unset_environment= */ false,
173✔
2845
                          "STATUS=Activating special unit %s...", s);
2846
        m->status_ready = false;
173✔
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,553✔
2868
        Manager *m = ASSERT_PTR(userdata);
3,553✔
2869
        ssize_t n;
3,553✔
2870
        struct signalfd_siginfo sfsi;
3,553✔
2871
        int r;
3,553✔
2872

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

2875
        if (revents != EPOLLIN) {
3,553✔
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,553✔
2881
        if (n < 0) {
3,553✔
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,553✔
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,726✔
2895
                            (sfsi.ssi_signo == SIGTERM && MANAGER_IS_USER(m))
173✔
2896
                            ? LOG_DEBUG : LOG_INFO,
2897
                            &sfsi);
2898

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

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

2906
                break;
2907

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

2915
                _fallthrough_;
173✔
2916
        case SIGINT:
2917
                if (MANAGER_IS_SYSTEM(m))
173✔
2918
                        manager_handle_ctrl_alt_del(m);
×
2919
                else
2920
                        manager_start_special(m, SPECIAL_EXIT_TARGET, JOB_REPLACE_IRREVERSIBLY);
173✔
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));
3,553✔
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(
18✔
3133
                sd_event_source *source,
3134
                const struct inotify_event *e,
3135
                void *userdata) {
3136

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

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

3143
        changed = manager_read_timezone_stat(m);
18✔
3144
        if (changed <= 0)
18✔
3145
                return changed;
18✔
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)
2,018✔
3156
                if (UNIT_VTABLE(u)->timezone_change)
1,996✔
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) {
8✔
3163
        Manager *m = ASSERT_PTR(userdata);
8✔
3164

3165
        assert(m->idle_pipe[2] == fd);
8✔
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;
8✔
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);
8✔
3177

3178
        return 0;
8✔
3179
}
3180

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

3185
        assert(source);
403✔
3186

3187
        manager_print_jobs_in_progress(m);
403✔
3188

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

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

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

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

3203
        manager_check_finished(m);
260✔
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);
260✔
3207
        if (r < 0)
260✔
3208
                return log_error_errno(r, "Failed to enable SIGCHLD event source: %m");
×
3209

3210
        while (m->objective == MANAGER_OK) {
196,844✔
3211

3212
                if (!ratelimit_below(&rl)) {
196,584✔
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();
196,584✔
3219

3220
                if (manager_dispatch_load_queue(m) > 0)
196,584✔
3221
                        continue;
×
3222

3223
                if (manager_dispatch_gc_job_queue(m) > 0)
196,584✔
3224
                        continue;
7✔
3225

3226
                if (manager_dispatch_gc_unit_queue(m) > 0)
196,577✔
3227
                        continue;
14,060✔
3228

3229
                if (manager_dispatch_cleanup_queue(m) > 0)
182,517✔
3230
                        continue;
7,993✔
3231

3232
                if (manager_dispatch_cgroup_realize_queue(m) > 0)
174,524✔
3233
                        continue;
3,485✔
3234

3235
                if (manager_dispatch_start_when_upheld_queue(m) > 0)
171,039✔
3236
                        continue;
×
3237

3238
                if (manager_dispatch_stop_when_bound_queue(m) > 0)
171,039✔
3239
                        continue;
143✔
3240

3241
                if (manager_dispatch_stop_when_unneeded_queue(m) > 0)
170,896✔
3242
                        continue;
131✔
3243

3244
                if (manager_dispatch_release_resources_queue(m) > 0)
170,765✔
3245
                        continue;
1,024✔
3246

3247
                if (manager_dispatch_dbus_queue(m) > 0)
169,741✔
3248
                        continue;
6,997✔
3249

3250
                /* Sleep for watchdog runtime wait time */
3251
                r = sd_event_run(m->event, watchdog_runtime_wait(/* divisor= */ 2));
162,744✔
3252
                if (r < 0)
162,744✔
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) {
211,594✔
3260
        _cleanup_free_ char *n = NULL;
211,594✔
3261
        sd_id128_t invocation_id;
211,594✔
3262
        Unit *u;
211,594✔
3263
        int r;
211,594✔
3264

3265
        assert(m);
211,594✔
3266
        assert(s);
211,594✔
3267
        assert(_u);
211,594✔
3268

3269
        r = unit_name_from_dbus_path(s, &n);
211,594✔
3270
        if (r < 0)
211,594✔
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);
211,594✔
3276
        if (r >= 0) {
211,594✔
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)) {
211,594✔
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);
211,593✔
3298
        if (r < 0)
211,593✔
3299
                return r;
3300

3301
        *_u = u;
211,593✔
3302
        return 0;
211,593✔
3303
}
3304

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

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

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

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

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

3327
        *_j = j;
584✔
3328

3329
        return 0;
584✔
3330
}
3331

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

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

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

3342
        if (!MANAGER_IS_SYSTEM(m))
3,399✔
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,822✔
3347
                return;
3348

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

3353
        r = unit_name_to_prefix_and_instance(u->id, &p);
849✔
3354
        if (r < 0) {
849✔
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,245✔
3360
        if (audit_log_user_comm_message(audit_fd, type, msg, "systemd", NULL, NULL, NULL, success) < 0) {
849✔
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");
849✔
3367
        }
3368
#endif
3369
}
3370

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

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

3378
        if (!MANAGER_IS_SYSTEM(m))
32,415✔
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,781✔
3383
                return;
3384

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

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

3391
        c = asprintf(&message, "U\x02%c%s%c", (int) (strlen(u->id) + 1), u->id, '\x00');
581✔
3392
        if (c < 0)
581✔
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);
581✔
3397
        if (r < 0)
581✔
3398
                log_full_errno(ERRNO_IS_NO_PLYMOUTH(r) ? LOG_DEBUG : LOG_WARNING, r,
581✔
3399
                               "Failed to communicate with plymouth: %m");
3400
}
3401

3402
void manager_send_unit_supervisor(Manager *m, Unit *u, bool active) {
52,152✔
3403
        assert(m);
52,152✔
3404
        assert(u);
52,152✔
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,152✔
3410
                return;
3411

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

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

3418
        (void) sd_notifyf(/* unset_environment= */ false,
4,880✔
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) {
536✔
3424
        assert(m);
536✔
3425

3426
        if (MANAGER_IS_USER(m))
536✔
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,040✔
3436

3437
        assert(m);
1,040✔
3438

3439
        if (MANAGER_IS_USER(m))
1,040✔
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) {
260✔
3470
        _cleanup_free_ char *p = NULL;
260✔
3471
        int r;
260✔
3472

3473
        assert(m);
260✔
3474

3475
        if (MANAGER_IS_USER(m))
260✔
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)
245✔
3610
                unit_reset_failed(u);
244✔
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) {
14,844✔
3628
        assert(m);
14,844✔
3629

3630
        if (MANAGER_IS_USER(m) || m->taint_logged)
14,844✔
3631
                return;
14,844✔
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) {
179✔
3646
        usec_t firmware_usec, loader_usec, kernel_usec, initrd_usec, userspace_usec, total_usec;
179✔
3647

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

3651
        if (MANAGER_IS_SYSTEM(m) && m->soft_reboots_count > 0) {
172✔
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) {
172✔
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;
172✔
3719
                total_usec = userspace_usec = m->timestamps[MANAGER_TIMESTAMP_FINISH].monotonic - m->timestamps[MANAGER_TIMESTAMP_USERSPACE].monotonic;
172✔
3720

3721
                log_struct(LOG_INFO,
172✔
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);
172✔
3729

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

3733
        log_taint_string(m);
172✔
3734
}
3735

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

3739
        assert(m);
14,672✔
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)
14,672✔
3743
                return;
3744

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

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

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

3758
        assert(m);
2,001✔
3759

3760
        /* Skip the notification if nothing changed. */
3761
        if (m->ready_sent && m->status_ready)
2,001✔
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,
182✔
3768
                      "READY=1\n"
3769
                      "STATUS=Ready.");
3770
        if (r < 0)
182✔
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;
182✔
3775
}
3776

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

3780
        assert(m);
26,157✔
3781

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

3786
        u = manager_get_unit(m, SPECIAL_BASIC_TARGET);
26,157✔
3787
        if (!u || !UNIT_IS_ACTIVE_OR_RELOADING(unit_active_state(u)))
26,157✔
3788
                return;
11,485✔
3789

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

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

3797
void manager_check_finished(Manager *m) {
26,344✔
3798
        assert(m);
26,344✔
3799

3800
        if (MANAGER_IS_RELOADING(m))
26,344✔
3801
                return;
3802

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

3807
        manager_check_basic_target(m);
26,157✔
3808

3809
        if (!hashmap_isempty(m->jobs)) {
26,157✔
3810
                if (m->jobs_in_progress_event_source)
24,156✔
3811
                        /* Ignore any failure, this is only for feedback */
3812
                        (void) sd_event_source_set_time(m->jobs_in_progress_event_source,
3,666✔
3813
                                                        manager_watch_jobs_next_time(m));
3814
                return;
24,156✔
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,001✔
3820
                m->jobs = hashmap_free(m->jobs);
457✔
3821

3822
        manager_send_ready_on_idle(m);
2,001✔
3823

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

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

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

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

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

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

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

3843
        manager_notify_finished(m);
179✔
3844

3845
        manager_invalidate_startup_units(m);
179✔
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) {
958✔
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,832✔
3863
                if (access(*i, F_OK) >= 0)
3,832✔
3864
                        return true;
3865
                if (errno != ENOENT)
2,874✔
3866
                        log_warning_errno(errno, "Failed to check if generator dir '%s' exists, assuming not: %m", *i);
2,874✔
3867
        }
3868

3869
        return false;
3870
}
3871

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

3876
        assert(m);
721✔
3877

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

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

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

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

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

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

3908
        assert(m);
176✔
3909
        assert(ret);
176✔
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);
176✔
3916
        if (!nl)
176✔
3917
                return -ENOMEM;
3918

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

3923
        if (MANAGER_IS_SYSTEM(m)) {
176✔
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();
176✔
3946
        if (v < 0)
176✔
3947
                log_debug_errno(v, "Failed to detect virtualization, ignoring: %m");
×
3948
        else if (v > 0) {
176✔
3949
                const char *s;
176✔
3950

3951
                s = strjoina(VIRTUALIZATION_IS_VM(v) ? "vm:" :
880✔
3952
                             VIRTUALIZATION_IS_CONTAINER(v) ? "container:" : ":",
3953
                             virtualization_to_string(v));
3954

3955
                r = strv_env_assign(&nl, "SYSTEMD_VIRTUALIZATION", s);
176✔
3956
                if (r < 0)
176✔
3957
                        return r;
3958
        }
3959

3960
        cv = detect_confidential_virtualization();
176✔
3961
        if (cv < 0)
176✔
3962
                log_debug_errno(cv, "Failed to detect confidential virtualization, ignoring: %m");
×
3963
        else if (cv > 0) {
176✔
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()));
176✔
3970
        if (r < 0)
176✔
3971
                return r;
3972

3973
        *ret = TAKE_PTR(nl);
176✔
3974
        return 0;
176✔
3975
}
3976

3977
static int manager_execute_generators(Manager *m, char * const *paths, bool remount_ro) {
176✔
3978
        _cleanup_strv_free_ char **ge = NULL;
176✔
3979
        int r;
176✔
3980

3981
        assert(m);
176✔
3982

3983
        r = build_generator_environment(m, &ge);
176✔
3984
        if (r < 0)
176✔
3985
                return log_error_errno(r, "Failed to build generator environment: %m");
×
3986

3987
        if (remount_ro) {
176✔
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[] = {
176✔
4000
                NULL, /* Leave this empty, execute_directory() will fill something in */
4001
                m->lookup_paths.generator,
176✔
4002
                m->lookup_paths.generator_early,
176✔
4003
                m->lookup_paths.generator_late,
176✔
4004
                NULL,
4005
        };
4006

4007
        BLOCK_WITH_UMASK(0022);
352✔
4008
        return execute_directories(
176✔
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) {
721✔
4018
        ForkFlags flags = FORK_RESET_SIGNALS | FORK_WAIT | FORK_NEW_MOUNTNS | FORK_MOUNTNS_SLAVE;
721✔
4019
        _cleanup_strv_free_ char **paths = NULL;
721✔
4020
        int r;
721✔
4021

4022
        assert(m);
721✔
4023

4024
        if (MANAGER_IS_TEST_RUN(m) && !(m->test_run_flags & MANAGER_TEST_RUN_GENERATORS))
721✔
4025
                return 0;
4026

4027
        paths = generator_binary_paths(m->runtime_scope);
260✔
4028
        if (!paths)
260✔
4029
                return log_oom();
×
4030

4031
        if (!generator_path_any(paths))
260✔
4032
                return 0;
4033

4034
        r = lookup_paths_mkdir_generator(&m->lookup_paths);
260✔
4035
        if (r < 0) {
260✔
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)) {
260✔
4045
                r = manager_execute_generators(m, paths, /* remount_ro= */ false);
176✔
4046
                goto finish;
176✔
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);
260✔
4082
        return r;
4083
}
4084

4085
int manager_transient_environment_add(Manager *m, char **plus) {
260✔
4086
        char **a;
260✔
4087

4088
        assert(m);
260✔
4089

4090
        if (strv_isempty(plus))
260✔
4091
                return 0;
260✔
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(
174✔
4103
                Manager *m,
4104
                char **minus,
4105
                char **plus) {
4106

4107
        char **a = NULL, **b = NULL, **l;
174✔
4108

4109
        assert(m);
174✔
4110

4111
        if (strv_isempty(minus) && strv_isempty(plus))
174✔
4112
                return 0;
4113

4114
        l = m->client_environment;
174✔
4115

4116
        if (!strv_isempty(minus)) {
174✔
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)) {
174✔
4125
                b = strv_env_merge(l, plus);
173✔
4126
                if (!b) {
173✔
4127
                        strv_free(a);
×
4128
                        return -ENOMEM;
×
4129
                }
4130

4131
                l = b;
4132
        }
4133

4134
        if (m->client_environment != l)
174✔
4135
                strv_free(m->client_environment);
174✔
4136

4137
        if (a != l)
174✔
4138
                strv_free(a);
173✔
4139
        if (b != l)
174✔
4140
                strv_free(b);
1✔
4141

4142
        m->client_environment = sanitize_environment(l);
174✔
4143
        return 0;
174✔
4144
}
4145

4146
int manager_get_effective_environment(Manager *m, char ***ret) {
18,625✔
4147
        char **l;
18,625✔
4148

4149
        assert(m);
18,625✔
4150
        assert(ret);
18,625✔
4151

4152
        l = strv_env_merge(m->transient_environment, m->client_environment);
18,625✔
4153
        if (!l)
18,625✔
4154
                return -ENOMEM;
4155

4156
        *ret = l;
18,625✔
4157
        return 0;
18,625✔
4158
}
4159

4160
int manager_set_unit_defaults(Manager *m, const UnitDefaults *defaults) {
260✔
4161
        _cleanup_free_ char *label = NULL;
260✔
4162
        struct rlimit *rlimit[_RLIMIT_MAX];
260✔
4163
        int r;
260✔
4164

4165
        assert(m);
260✔
4166
        assert(defaults);
260✔
4167

4168
        if (streq_ptr(defaults->smack_process_label, "/"))
260✔
4169
                label = NULL;
4170
        else  {
4171
                const char *l = defaults->smack_process_label;
260✔
4172
#ifdef SMACK_DEFAULT_PROCESS_LABEL
4173
                if (!l)
4174
                        l = SMACK_DEFAULT_PROCESS_LABEL;
4175
#endif
4176
                if (l) {
260✔
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);
260✔
4185
        if (r < 0)
260✔
4186
                return r;
4187

4188
        m->defaults.std_output = defaults->std_output;
260✔
4189
        m->defaults.std_error = defaults->std_error;
260✔
4190

4191
        m->defaults.restart_usec = defaults->restart_usec;
260✔
4192
        m->defaults.timeout_start_usec = defaults->timeout_start_usec;
260✔
4193
        m->defaults.timeout_stop_usec = defaults->timeout_stop_usec;
260✔
4194
        m->defaults.timeout_abort_usec = defaults->timeout_abort_usec;
260✔
4195
        m->defaults.timeout_abort_set = defaults->timeout_abort_set;
260✔
4196
        m->defaults.device_timeout_usec = defaults->device_timeout_usec;
260✔
4197

4198
        m->defaults.start_limit = defaults->start_limit;
260✔
4199

4200
        m->defaults.cpu_accounting = defaults->cpu_accounting;
260✔
4201
        m->defaults.memory_accounting = defaults->memory_accounting;
260✔
4202
        m->defaults.io_accounting = defaults->io_accounting;
260✔
4203
        m->defaults.blockio_accounting = defaults->blockio_accounting;
260✔
4204
        m->defaults.tasks_accounting = defaults->tasks_accounting;
260✔
4205
        m->defaults.ip_accounting = defaults->ip_accounting;
260✔
4206

4207
        m->defaults.tasks_max = defaults->tasks_max;
260✔
4208
        m->defaults.timer_accuracy_usec = defaults->timer_accuracy_usec;
260✔
4209

4210
        m->defaults.oom_policy = defaults->oom_policy;
260✔
4211
        m->defaults.oom_score_adjust = defaults->oom_score_adjust;
260✔
4212
        m->defaults.oom_score_adjust_set = defaults->oom_score_adjust_set;
260✔
4213

4214
        m->defaults.memory_pressure_watch = defaults->memory_pressure_watch;
260✔
4215
        m->defaults.memory_pressure_threshold_usec = defaults->memory_pressure_threshold_usec;
260✔
4216

4217
        free_and_replace(m->defaults.smack_process_label, label);
260✔
4218
        rlimit_free_all(m->defaults.rlimit);
260✔
4219
        memcpy(m->defaults.rlimit, rlimit, sizeof(struct rlimit*) * _RLIMIT_MAX);
260✔
4220

4221
        return 0;
260✔
4222
}
4223

4224
void manager_recheck_dbus(Manager *m) {
67,668✔
4225
        assert(m);
67,668✔
4226

4227
        /* Connects to the bus if the dbus service and socket are running. If we are running in user mode
4228
         * this is all it does. In system mode we'll also connect to the system bus (which will most likely
4229
         * just reuse the connection of the API bus). That's because the system bus after all runs as service
4230
         * of the system instance, while in the user instance we can assume it's already there. */
4231

4232
        if (MANAGER_IS_RELOADING(m))
67,668✔
4233
                return; /* don't check while we are reloading… */
4234

4235
        if (manager_dbus_is_running(m, false)) {
60,002✔
4236
                (void) bus_init_api(m);
3,684✔
4237

4238
                if (MANAGER_IS_SYSTEM(m))
3,684✔
4239
                        (void) bus_init_system(m);
2,747✔
4240
        } else {
4241
                (void) bus_done_api(m);
56,318✔
4242

4243
                if (MANAGER_IS_SYSTEM(m))
56,318✔
4244
                        (void) bus_done_system(m);
8,133✔
4245
        }
4246
}
4247

4248
static bool manager_journal_is_running(Manager *m) {
12,624✔
4249
        Unit *u;
12,624✔
4250

4251
        assert(m);
12,624✔
4252

4253
        if (MANAGER_IS_TEST_RUN(m))
12,624✔
4254
                return false;
4255

4256
        /* If we are the user manager we can safely assume that the journal is up */
4257
        if (!MANAGER_IS_SYSTEM(m))
12,624✔
4258
                return true;
4259

4260
        /* Check that the socket is not only up, but in RUNNING state */
4261
        u = manager_get_unit(m, SPECIAL_JOURNALD_SOCKET);
11,846✔
4262
        if (!u)
11,846✔
4263
                return false;
4264
        if (SOCKET(u)->state != SOCKET_RUNNING)
11,846✔
4265
                return false;
4266

4267
        /* Similar, check if the daemon itself is fully up, too */
4268
        u = manager_get_unit(m, SPECIAL_JOURNALD_SERVICE);
9,039✔
4269
        if (!u)
9,039✔
4270
                return false;
4271
        if (!IN_SET(SERVICE(u)->state, SERVICE_RELOAD, SERVICE_RUNNING))
9,039✔
4272
                return false;
664✔
4273

4274
        return true;
4275
}
4276

4277
void disable_printk_ratelimit(void) {
15✔
4278
        /* Disable kernel's printk ratelimit.
4279
         *
4280
         * Logging to /dev/kmsg is most useful during early boot and shutdown, where normal logging
4281
         * mechanisms are not available. The semantics of this sysctl are such that any kernel command-line
4282
         * setting takes precedence. */
4283
        int r;
15✔
4284

4285
        r = sysctl_write("kernel/printk_devkmsg", "on");
15✔
4286
        if (r < 0)
15✔
4287
                log_debug_errno(r, "Failed to set sysctl kernel.printk_devkmsg=on: %m");
×
4288
}
15✔
4289

4290
void manager_recheck_journal(Manager *m) {
67,668✔
4291

4292
        assert(m);
67,668✔
4293

4294
        /* Don't bother with this unless we are in the special situation of being PID 1 */
4295
        if (getpid_cached() != 1)
67,668✔
4296
                return;
4297

4298
        /* Don't check this while we are reloading, things might still change */
4299
        if (MANAGER_IS_RELOADING(m))
17,961✔
4300
                return;
4301

4302
        /* The journal is fully and entirely up? If so, let's permit logging to it, if that's configured. If
4303
         * the journal is down, don't ever log to it, otherwise we might end up deadlocking ourselves as we
4304
         * might trigger an activation ourselves we can't fulfill. */
4305
        log_set_prohibit_ipc(!manager_journal_is_running(m));
10,436✔
4306
        log_open();
10,436✔
4307
}
4308

4309
static ShowStatus manager_get_show_status(Manager *m) {
6,543✔
4310
        assert(m);
6,543✔
4311

4312
        if (MANAGER_IS_USER(m))
6,543✔
4313
                return _SHOW_STATUS_INVALID;
4314

4315
        if (m->show_status_overridden != _SHOW_STATUS_INVALID)
6,543✔
4316
                return m->show_status_overridden;
4317

4318
        return m->show_status;
6,543✔
4319
}
4320

4321
bool manager_get_show_status_on(Manager *m) {
6,543✔
4322
        assert(m);
6,543✔
4323

4324
        return show_status_on(manager_get_show_status(m));
6,543✔
4325
}
4326

4327
static void set_show_status_marker(bool b) {
60✔
4328
        if (b)
60✔
4329
                (void) touch("/run/systemd/show-status");
×
4330
        else
4331
                (void) unlink("/run/systemd/show-status");
60✔
4332
}
60✔
4333

4334
void manager_set_show_status(Manager *m, ShowStatus mode, const char *reason) {
260✔
4335
        assert(m);
260✔
4336
        assert(reason);
260✔
4337
        assert(mode >= 0 && mode < _SHOW_STATUS_MAX);
260✔
4338

4339
        if (MANAGER_IS_USER(m))
260✔
4340
                return;
4341

4342
        if (mode == m->show_status)
84✔
4343
                return;
4344

4345
        if (m->show_status_overridden == _SHOW_STATUS_INVALID) {
60✔
4346
                bool enabled;
60✔
4347

4348
                enabled = show_status_on(mode);
60✔
4349
                log_debug("%s (%s) showing of status (%s).",
120✔
4350
                          enabled ? "Enabling" : "Disabling",
4351
                          strna(show_status_to_string(mode)),
4352
                          reason);
4353

4354
                set_show_status_marker(enabled);
60✔
4355
        }
4356

4357
        m->show_status = mode;
60✔
4358
}
4359

4360
void manager_override_show_status(Manager *m, ShowStatus mode, const char *reason) {
×
4361
        assert(m);
×
4362
        assert(mode < _SHOW_STATUS_MAX);
×
4363

4364
        if (MANAGER_IS_USER(m))
×
4365
                return;
4366

4367
        if (mode == m->show_status_overridden)
×
4368
                return;
4369

4370
        m->show_status_overridden = mode;
×
4371

4372
        if (mode == _SHOW_STATUS_INVALID)
×
4373
                mode = m->show_status;
×
4374

4375
        log_debug("%s (%s) showing of status (%s).",
×
4376
                  m->show_status_overridden != _SHOW_STATUS_INVALID ? "Overriding" : "Restoring",
4377
                  strna(show_status_to_string(mode)),
4378
                  reason);
4379

4380
        set_show_status_marker(show_status_on(mode));
×
4381
}
4382

4383
const char* manager_get_confirm_spawn(Manager *m) {
2,199✔
4384
        static int last_errno = 0;
2,199✔
4385
        struct stat st;
2,199✔
4386
        int r;
2,199✔
4387

4388
        assert(m);
2,199✔
4389

4390
        /* Here's the deal: we want to test the validity of the console but don't want
4391
         * PID1 to go through the whole console process which might block. But we also
4392
         * want to warn the user only once if something is wrong with the console so we
4393
         * cannot do the sanity checks after spawning our children. So here we simply do
4394
         * really basic tests to hopefully trap common errors.
4395
         *
4396
         * If the console suddenly disappear at the time our children will really it
4397
         * then they will simply fail to acquire it and a positive answer will be
4398
         * assumed. New children will fall back to /dev/console though.
4399
         *
4400
         * Note: TTYs are devices that can come and go any time, and frequently aren't
4401
         * available yet during early boot (consider a USB rs232 dongle...). If for any
4402
         * reason the configured console is not ready, we fall back to the default
4403
         * console. */
4404

4405
        if (!m->confirm_spawn || path_equal(m->confirm_spawn, "/dev/console"))
2,199✔
4406
                return m->confirm_spawn;
2,199✔
4407

4408
        if (stat(m->confirm_spawn, &st) < 0) {
×
4409
                r = -errno;
×
4410
                goto fail;
×
4411
        }
4412

4413
        if (!S_ISCHR(st.st_mode)) {
×
4414
                r = -ENOTTY;
×
4415
                goto fail;
×
4416
        }
4417

4418
        last_errno = 0;
×
4419
        return m->confirm_spawn;
×
4420

4421
fail:
×
4422
        if (last_errno != r)
×
4423
                last_errno = log_warning_errno(r, "Failed to open %s, using default console: %m", m->confirm_spawn);
×
4424

4425
        return "/dev/console";
4426
}
4427

4428
void manager_set_first_boot(Manager *m, bool b) {
413✔
4429
        assert(m);
413✔
4430

4431
        if (!MANAGER_IS_SYSTEM(m))
413✔
4432
                return;
4433

4434
        if (m->first_boot != (int) b) {
60✔
4435
                if (b)
60✔
4436
                        (void) touch("/run/systemd/first-boot");
17✔
4437
                else
4438
                        (void) unlink("/run/systemd/first-boot");
43✔
4439
        }
4440

4441
        m->first_boot = b;
60✔
4442
}
4443

4444
void manager_disable_confirm_spawn(void) {
×
4445
        (void) touch("/run/systemd/confirm_spawn_disabled");
×
4446
}
×
4447

4448
static bool manager_should_show_status(Manager *m, StatusType type) {
15,377✔
4449
        assert(m);
15,377✔
4450

4451
        if (!MANAGER_IS_SYSTEM(m))
15,377✔
4452
                return false;
4453

4454
        if (m->no_console_output)
7,001✔
4455
                return false;
4456

4457
        if (!IN_SET(manager_state(m), MANAGER_INITIALIZING, MANAGER_STARTING, MANAGER_STOPPING))
6,527✔
4458
                return false;
4459

4460
        /* If we cannot find out the status properly, just proceed. */
4461
        if (type != STATUS_TYPE_EMERGENCY && manager_check_ask_password(m) > 0)
6,527✔
4462
                return false;
4463

4464
        if (type == STATUS_TYPE_NOTICE && m->show_status != SHOW_STATUS_NO)
6,527✔
4465
                return true;
4466

4467
        return manager_get_show_status_on(m);
6,526✔
4468
}
4469

4470
void manager_status_printf(Manager *m, StatusType type, const char *status, const char *format, ...) {
15,377✔
4471
        va_list ap;
15,377✔
4472

4473
        /* If m is NULL, assume we're after shutdown and let the messages through. */
4474

4475
        if (m && !manager_should_show_status(m, type))
15,377✔
4476
                return;
15,376✔
4477

4478
        /* XXX We should totally drop the check for ephemeral here
4479
         * and thus effectively make 'Type=idle' pointless. */
4480
        if (type == STATUS_TYPE_EPHEMERAL && m && m->n_on_console > 0)
1✔
4481
                return;
4482

4483
        va_start(ap, format);
1✔
4484
        status_vprintf(status, SHOW_STATUS_ELLIPSIZE|(type == STATUS_TYPE_EPHEMERAL ? SHOW_STATUS_EPHEMERAL : 0), format, ap);
2✔
4485
        va_end(ap);
1✔
4486
}
4487

4488
Set* manager_get_units_needing_mounts_for(Manager *m, const char *path, UnitMountDependencyType t) {
30,894✔
4489
        assert(m);
30,894✔
4490
        assert(path);
30,894✔
4491
        assert(t >= 0 && t < _UNIT_MOUNT_DEPENDENCY_TYPE_MAX);
30,894✔
4492

4493
        if (path_equal(path, "/"))
30,894✔
4494
                path = "";
1,358✔
4495

4496
        return hashmap_get(m->units_needing_mounts_for[t], path);
30,894✔
4497
}
4498

4499
int manager_update_failed_units(Manager *m, Unit *u, bool failed) {
145,329✔
4500
        unsigned size;
145,329✔
4501
        int r;
145,329✔
4502

4503
        assert(m);
145,329✔
4504
        assert(u->manager == m);
145,329✔
4505

4506
        size = set_size(m->failed_units);
145,329✔
4507

4508
        if (failed) {
145,329✔
4509
                r = set_ensure_put(&m->failed_units, NULL, u);
250✔
4510
                if (r < 0)
250✔
4511
                        return log_oom();
×
4512
        } else
4513
                (void) set_remove(m->failed_units, u);
145,079✔
4514

4515
        if (set_size(m->failed_units) != size)
145,329✔
4516
                bus_manager_send_change_signal(m);
500✔
4517

4518
        return 0;
4519
}
4520

4521
ManagerState manager_state(Manager *m) {
400,705✔
4522
        Unit *u;
400,705✔
4523

4524
        assert(m);
400,705✔
4525

4526
        /* Is the special shutdown target active or queued? If so, we are in shutdown state */
4527
        u = manager_get_unit(m, SPECIAL_SHUTDOWN_TARGET);
400,705✔
4528
        if (u && unit_active_or_pending(u))
400,705✔
4529
                return MANAGER_STOPPING;
4530

4531
        /* Did we ever finish booting? If not then we are still starting up */
4532
        if (!MANAGER_IS_FINISHED(m)) {
320,331✔
4533

4534
                u = manager_get_unit(m, SPECIAL_BASIC_TARGET);
291,733✔
4535
                if (!u || !UNIT_IS_ACTIVE_OR_RELOADING(unit_active_state(u)))
291,733✔
4536
                        return MANAGER_INITIALIZING;
243,888✔
4537

4538
                return MANAGER_STARTING;
4539
        }
4540

4541
        if (MANAGER_IS_SYSTEM(m)) {
28,598✔
4542
                /* Are the rescue or emergency targets active or queued? If so we are in maintenance state */
4543
                u = manager_get_unit(m, SPECIAL_RESCUE_TARGET);
×
4544
                if (u && unit_active_or_pending(u))
×
4545
                        return MANAGER_MAINTENANCE;
4546

4547
                u = manager_get_unit(m, SPECIAL_EMERGENCY_TARGET);
×
4548
                if (u && unit_active_or_pending(u))
×
4549
                        return MANAGER_MAINTENANCE;
4550
        }
4551

4552
        /* Are there any failed units? If so, we are in degraded mode */
4553
        if (!set_isempty(m->failed_units))
28,598✔
4554
                return MANAGER_DEGRADED;
1,059✔
4555

4556
        return MANAGER_RUNNING;
4557
}
4558

4559
static void manager_unref_uid_internal(
807✔
4560
                Hashmap *uid_refs,
4561
                uid_t uid,
4562
                bool destroy_now,
4563
                int (*_clean_ipc)(uid_t uid)) {
4564

4565
        uint32_t c, n;
807✔
4566

4567
        assert(uid_is_valid(uid));
807✔
4568
        assert(_clean_ipc);
807✔
4569

4570
        /* A generic implementation, covering both manager_unref_uid() and manager_unref_gid(), under the
4571
         * assumption that uid_t and gid_t are actually defined the same way, with the same validity rules.
4572
         *
4573
         * We store a hashmap where the key is the UID/GID and the value is a 32-bit reference counter, whose
4574
         * highest bit is used as flag for marking UIDs/GIDs whose IPC objects to remove when the last
4575
         * reference to the UID/GID is dropped. The flag is set to on, once at least one reference from a
4576
         * unit where RemoveIPC= is set is added on a UID/GID. It is reset when the UID's/GID's reference
4577
         * counter drops to 0 again. */
4578

4579
        assert_cc(sizeof(uid_t) == sizeof(gid_t));
807✔
4580
        assert_cc(UID_INVALID == (uid_t) GID_INVALID);
807✔
4581

4582
        if (uid == 0) /* We don't keep track of root, and will never destroy it */
807✔
4583
                return;
4584

4585
        c = PTR_TO_UINT32(hashmap_get(uid_refs, UID_TO_PTR(uid)));
707✔
4586

4587
        n = c & ~DESTROY_IPC_FLAG;
707✔
4588
        assert(n > 0);
707✔
4589
        n--;
707✔
4590

4591
        if (destroy_now && n == 0) {
707✔
4592
                hashmap_remove(uid_refs, UID_TO_PTR(uid));
129✔
4593

4594
                if (c & DESTROY_IPC_FLAG) {
129✔
4595
                        log_debug("%s " UID_FMT " is no longer referenced, cleaning up its IPC.",
3✔
4596
                                  _clean_ipc == clean_ipc_by_uid ? "UID" : "GID",
4597
                                  uid);
4598
                        (void) _clean_ipc(uid);
2✔
4599
                }
4600
        } else {
4601
                c = n | (c & DESTROY_IPC_FLAG);
578✔
4602
                assert_se(hashmap_update(uid_refs, UID_TO_PTR(uid), UINT32_TO_PTR(c)) >= 0);
578✔
4603
        }
4604
}
4605

4606
void manager_unref_uid(Manager *m, uid_t uid, bool destroy_now) {
403✔
4607
        manager_unref_uid_internal(m->uid_refs, uid, destroy_now, clean_ipc_by_uid);
403✔
4608
}
403✔
4609

4610
void manager_unref_gid(Manager *m, gid_t gid, bool destroy_now) {
404✔
4611
        manager_unref_uid_internal(m->gid_refs, (uid_t) gid, destroy_now, clean_ipc_by_gid);
404✔
4612
}
404✔
4613

4614
static int manager_ref_uid_internal(
807✔
4615
                Hashmap **uid_refs,
4616
                uid_t uid,
4617
                bool clean_ipc) {
4618

4619
        uint32_t c, n;
807✔
4620
        int r;
807✔
4621

4622
        assert(uid_refs);
807✔
4623
        assert(uid_is_valid(uid));
807✔
4624

4625
        /* A generic implementation, covering both manager_ref_uid() and manager_ref_gid(), under the
4626
         * assumption that uid_t and gid_t are actually defined the same way, with the same validity
4627
         * rules. */
4628

4629
        assert_cc(sizeof(uid_t) == sizeof(gid_t));
807✔
4630
        assert_cc(UID_INVALID == (uid_t) GID_INVALID);
807✔
4631

4632
        if (uid == 0) /* We don't keep track of root, and will never destroy it */
807✔
4633
                return 0;
4634

4635
        r = hashmap_ensure_allocated(uid_refs, &trivial_hash_ops);
707✔
4636
        if (r < 0)
707✔
4637
                return r;
4638

4639
        c = PTR_TO_UINT32(hashmap_get(*uid_refs, UID_TO_PTR(uid)));
707✔
4640

4641
        n = c & ~DESTROY_IPC_FLAG;
707✔
4642
        n++;
707✔
4643

4644
        if (n & DESTROY_IPC_FLAG) /* check for overflow */
707✔
4645
                return -EOVERFLOW;
4646

4647
        c = n | (c & DESTROY_IPC_FLAG) | (clean_ipc ? DESTROY_IPC_FLAG : 0);
707✔
4648

4649
        return hashmap_replace(*uid_refs, UID_TO_PTR(uid), UINT32_TO_PTR(c));
707✔
4650
}
4651

4652
int manager_ref_uid(Manager *m, uid_t uid, bool clean_ipc) {
403✔
4653
        return manager_ref_uid_internal(&m->uid_refs, uid, clean_ipc);
403✔
4654
}
4655

4656
int manager_ref_gid(Manager *m, gid_t gid, bool clean_ipc) {
404✔
4657
        return manager_ref_uid_internal(&m->gid_refs, (uid_t) gid, clean_ipc);
404✔
4658
}
4659

4660
static void manager_vacuum_uid_refs_internal(
1,442✔
4661
                Hashmap *uid_refs,
4662
                int (*_clean_ipc)(uid_t uid)) {
4663

4664
        void *p, *k;
1,442✔
4665

4666
        assert(_clean_ipc);
1,442✔
4667

4668
        HASHMAP_FOREACH_KEY(p, k, uid_refs) {
3,222✔
4669
                uint32_t c, n;
338✔
4670
                uid_t uid;
338✔
4671

4672
                uid = PTR_TO_UID(k);
338✔
4673
                c = PTR_TO_UINT32(p);
338✔
4674

4675
                n = c & ~DESTROY_IPC_FLAG;
338✔
4676
                if (n > 0)
338✔
4677
                        continue;
338✔
4678

4679
                if (c & DESTROY_IPC_FLAG) {
×
4680
                        log_debug("Found unreferenced %s " UID_FMT " after reload/reexec. Cleaning up.",
×
4681
                                  _clean_ipc == clean_ipc_by_uid ? "UID" : "GID",
4682
                                  uid);
4683
                        (void) _clean_ipc(uid);
×
4684
                }
4685

4686
                assert_se(hashmap_remove(uid_refs, k) == p);
1,780✔
4687
        }
4688
}
1,442✔
4689

4690
static void manager_vacuum_uid_refs(Manager *m) {
721✔
4691
        manager_vacuum_uid_refs_internal(m->uid_refs, clean_ipc_by_uid);
721✔
4692
}
721✔
4693

4694
static void manager_vacuum_gid_refs(Manager *m) {
721✔
4695
        manager_vacuum_uid_refs_internal(m->gid_refs, clean_ipc_by_gid);
721✔
4696
}
721✔
4697

4698
static void manager_vacuum(Manager *m) {
721✔
4699
        assert(m);
721✔
4700

4701
        /* Release any dynamic users no longer referenced */
4702
        dynamic_user_vacuum(m, true);
721✔
4703

4704
        /* Release any references to UIDs/GIDs no longer referenced, and destroy any IPC owned by them */
4705
        manager_vacuum_uid_refs(m);
721✔
4706
        manager_vacuum_gid_refs(m);
721✔
4707

4708
        /* Release any runtimes no longer referenced */
4709
        exec_shared_runtime_vacuum(m);
721✔
4710
}
721✔
4711

4712
static int manager_dispatch_user_lookup_fd(sd_event_source *source, int fd, uint32_t revents, void *userdata) {
158✔
4713
        struct buffer {
158✔
4714
                uid_t uid;
4715
                gid_t gid;
4716
                char unit_name[UNIT_NAME_MAX+1];
4717
        } _packed_ buffer;
4718

4719
        Manager *m = ASSERT_PTR(userdata);
158✔
4720
        ssize_t l;
158✔
4721
        size_t n;
158✔
4722
        Unit *u;
158✔
4723

4724
        assert(source);
158✔
4725

4726
        /* Invoked whenever a child process succeeded resolving its user/group to use and sent us the
4727
         * resulting UID/GID in a datagram. We parse the datagram here and pass it off to the unit, so that
4728
         * it can add a reference to the UID/GID so that it can destroy the UID/GID's IPC objects when the
4729
         * reference counter drops to 0. */
4730

4731
        l = recv(fd, &buffer, sizeof(buffer), MSG_DONTWAIT);
158✔
4732
        if (l < 0) {
158✔
4733
                if (ERRNO_IS_TRANSIENT(errno))
×
4734
                        return 0;
158✔
4735

4736
                return log_error_errno(errno, "Failed to read from user lookup fd: %m");
×
4737
        }
4738

4739
        if ((size_t) l <= offsetof(struct buffer, unit_name)) {
158✔
4740
                log_warning("Received too short user lookup message, ignoring.");
×
4741
                return 0;
×
4742
        }
4743

4744
        if ((size_t) l > offsetof(struct buffer, unit_name) + UNIT_NAME_MAX) {
158✔
4745
                log_warning("Received too long user lookup message, ignoring.");
×
4746
                return 0;
×
4747
        }
4748

4749
        if (!uid_is_valid(buffer.uid) && !gid_is_valid(buffer.gid)) {
158✔
4750
                log_warning("Got user lookup message with invalid UID/GID pair, ignoring.");
×
4751
                return 0;
×
4752
        }
4753

4754
        n = (size_t) l - offsetof(struct buffer, unit_name);
158✔
4755
        if (memchr(buffer.unit_name, 0, n)) {
158✔
4756
                log_warning("Received lookup message with embedded NUL character, ignoring.");
×
4757
                return 0;
×
4758
        }
4759

4760
        buffer.unit_name[n] = 0;
158✔
4761
        u = manager_get_unit(m, buffer.unit_name);
158✔
4762
        if (!u) {
158✔
4763
                log_debug("Got user lookup message but unit doesn't exist, ignoring.");
×
4764
                return 0;
×
4765
        }
4766

4767
        log_unit_debug(u, "User lookup succeeded: uid=" UID_FMT " gid=" GID_FMT, buffer.uid, buffer.gid);
158✔
4768

4769
        unit_notify_user_lookup(u, buffer.uid, buffer.gid);
158✔
4770
        return 0;
4771
}
4772

4773
static int manager_dispatch_handoff_timestamp_fd(sd_event_source *source, int fd, uint32_t revents, void *userdata) {
2,181✔
4774
        Manager *m = ASSERT_PTR(userdata);
2,181✔
4775
        usec_t ts[2] = {};
2,181✔
4776
        CMSG_BUFFER_TYPE(CMSG_SPACE(sizeof(struct ucred))) control;
2,181✔
4777
        struct msghdr msghdr = {
2,181✔
4778
                .msg_iov = &IOVEC_MAKE(ts, sizeof(ts)),
2,181✔
4779
                .msg_iovlen = 1,
4780
                .msg_control = &control,
4781
                .msg_controllen = sizeof(control),
4782
        };
4783
        ssize_t n;
2,181✔
4784

4785
        assert(source);
2,181✔
4786

4787
        n = recvmsg_safe(m->handoff_timestamp_fds[0], &msghdr, MSG_DONTWAIT|MSG_CMSG_CLOEXEC);
2,181✔
4788
        if (ERRNO_IS_NEG_TRANSIENT(n))
2,181✔
4789
                return 0; /* Spurious wakeup, try again */
2,181✔
4790
        if (n == -ECHRNG) {
2,181✔
4791
                log_warning_errno(n, "Got message with truncated control data (unexpected fds sent?), ignoring.");
×
4792
                return 0;
×
4793
        }
4794
        if (n == -EXFULL) {
2,181✔
4795
                log_warning_errno(n, "Got message with truncated payload data, ignoring.");
×
4796
                return 0;
×
4797
        }
4798
        if (n < 0)
2,181✔
4799
                return log_error_errno(n, "Failed to receive handoff timestamp message: %m");
×
4800

4801
        cmsg_close_all(&msghdr);
2,181✔
4802

4803
        if (n != sizeof(ts)) {
2,181✔
4804
                log_warning("Got handoff timestamp message of unexpected size %zi (expected %zu), ignoring.", n, sizeof(ts));
×
4805
                return 0;
×
4806
        }
4807

4808
        struct ucred *ucred = CMSG_FIND_DATA(&msghdr, SOL_SOCKET, SCM_CREDENTIALS, struct ucred);
2,181✔
4809
        if (!ucred || !pid_is_valid(ucred->pid)) {
2,181✔
4810
                log_warning("Received handoff timestamp message without valid credentials. Ignoring.");
×
4811
                return 0;
×
4812
        }
4813

4814
        log_debug("Got handoff timestamp event for PID " PID_FMT ".", ucred->pid);
2,181✔
4815

4816
        _cleanup_free_ Unit **units = NULL;
2,181✔
4817
        int n_units = manager_get_units_for_pidref(m, &PIDREF_MAKE_FROM_PID(ucred->pid), &units);
2,181✔
4818
        if (n_units < 0) {
2,181✔
4819
                log_warning_errno(n_units, "Unable to determine units for PID " PID_FMT ", ignoring: %m", ucred->pid);
×
4820
                return 0;
×
4821
        }
4822
        if (n_units == 0) {
2,181✔
4823
                log_debug("Got handoff timestamp for process " PID_FMT " we are not interested in, ignoring.", ucred->pid);
×
4824
                return 0;
×
4825
        }
4826

4827
        dual_timestamp dt = {
2,181✔
4828
                .realtime = ts[0],
2,181✔
4829
                .monotonic = ts[1],
2,181✔
4830
        };
4831

4832
        FOREACH_ARRAY(u, units, n_units) {
6,543✔
4833
                if (!UNIT_VTABLE(*u)->notify_handoff_timestamp)
4,362✔
4834
                        continue;
5✔
4835

4836
                UNIT_VTABLE(*u)->notify_handoff_timestamp(*u, ucred, &dt);
4,357✔
4837
        }
4838

4839
        return 0;
4840
}
4841

4842
static int manager_dispatch_pidref_transport_fd(sd_event_source *source, int fd, uint32_t revents, void *userdata) {
3✔
4843
        Manager *m = ASSERT_PTR(userdata);
3✔
4844
        _cleanup_(pidref_done) PidRef child_pidref = PIDREF_NULL, parent_pidref = PIDREF_NULL;
3✔
4845
        _cleanup_close_ int child_pidfd = -EBADF, parent_pidfd = -EBADF;
6✔
4846
        struct ucred *ucred = NULL;
3✔
4847
        CMSG_BUFFER_TYPE(CMSG_SPACE(sizeof(struct ucred)) + CMSG_SPACE(sizeof(int)) * 2) control;
3✔
4848
        pid_t child_pid = 0; /* silence false-positive warning by coverity */
3✔
4849
        struct msghdr msghdr = {
3✔
4850
                .msg_iov = &IOVEC_MAKE(&child_pid, sizeof(child_pid)),
3✔
4851
                .msg_iovlen = 1,
4852
                .msg_control = &control,
4853
                .msg_controllen = sizeof(control),
4854
        };
4855
        struct cmsghdr *cmsg;
3✔
4856
        ssize_t n;
3✔
4857
        int r;
3✔
4858

4859
        assert(source);
3✔
4860

4861
        /* Server expects:
4862
         * - Parent PID in ucreds enabled via SO_PASSCRED
4863
         * - Parent PIDFD in SCM_PIDFD message enabled via SO_PASSPIDFD
4864
         * - Child PIDFD in SCM_RIGHTS in message body
4865
         * - Child PID in message IOV
4866
         *
4867
         * SO_PASSPIDFD may not be supported by the kernel (it is supported since v6.5) so we fall back to
4868
         * using parent PID from ucreds and accept some raciness. */
4869
        n = recvmsg_safe(m->pidref_transport_fds[0], &msghdr, MSG_DONTWAIT|MSG_CMSG_CLOEXEC|MSG_TRUNC);
3✔
4870
        if (ERRNO_IS_NEG_TRANSIENT(n))
6✔
4871
                return 0; /* Spurious wakeup, try again */
4872
        if (n == -ECHRNG) {
3✔
4873
                log_warning_errno(n, "Got message with truncated control data (unexpected fds sent?), ignoring.");
×
4874
                return 0;
×
4875
        }
4876
        if (n == -EXFULL) {
3✔
4877
                log_warning_errno(n, "Got message with truncated payload data, ignoring.");
×
4878
                return 0;
×
4879
        }
4880
        if (n < 0)
3✔
4881
                return log_error_errno(n, "Failed to receive pidref message: %m");
×
4882

4883
        if (n != sizeof(child_pid)) {
3✔
4884
                log_warning("Got pidref message of unexpected size %zi (expected %zu), ignoring.", n, sizeof(child_pid));
×
4885
                return 0;
×
4886
        }
4887

4888
        CMSG_FOREACH(cmsg, &msghdr) {
24✔
4889
                if (cmsg->cmsg_level != SOL_SOCKET)
9✔
4890
                        continue;
×
4891

4892
                if (cmsg->cmsg_type == SCM_CREDENTIALS && cmsg->cmsg_len == CMSG_LEN(sizeof(struct ucred))) {
9✔
4893
                        assert(!ucred);
3✔
4894
                        ucred = CMSG_TYPED_DATA(cmsg, struct ucred);
3✔
4895
                } else if (cmsg->cmsg_type == SCM_PIDFD) {
6✔
4896
                        assert(parent_pidfd < 0);
3✔
4897
                        parent_pidfd = *CMSG_TYPED_DATA(cmsg, int);
3✔
4898
                } else if (cmsg->cmsg_type == SCM_RIGHTS) {
3✔
4899
                        assert(child_pidfd < 0);
3✔
4900
                        child_pidfd = *CMSG_TYPED_DATA(cmsg, int);
3✔
4901
                }
4902
        }
4903

4904
        /* Verify and set parent pidref. */
4905
        if (!ucred || !pid_is_valid(ucred->pid)) {
3✔
4906
                log_warning("Received pidref message without valid credentials. Ignoring.");
×
4907
                return 0;
×
4908
        }
4909

4910
        /* Need to handle kernels without SO_PASSPIDFD where SCM_PIDFD will not be set. */
4911
        if (parent_pidfd >= 0)
3✔
4912
                r = pidref_set_pidfd_consume(&parent_pidref, TAKE_FD(parent_pidfd));
3✔
4913
        else
4914
                r = pidref_set_pid(&parent_pidref, ucred->pid);
×
4915
        if (r < 0) {
3✔
4916
                if (r == -ESRCH)
×
4917
                        log_debug_errno(r, "PidRef child process died before message is processed. Ignoring.");
×
4918
                else
4919
                        log_warning_errno(r, "Failed to pin pidref child process, ignoring message: %m");
×
4920
                return 0;
×
4921
        }
4922

4923
        if (parent_pidref.pid != ucred->pid) {
3✔
4924
                assert(parent_pidref.fd >= 0);
×
4925
                log_warning("Got SCM_PIDFD for parent process " PID_FMT " but got SCM_CREDENTIALS for parent process " PID_FMT ". Ignoring.",
×
4926
                            parent_pidref.pid, ucred->pid);
4927
                return 0;
×
4928
        }
4929

4930
        /* Verify and set child pidref. */
4931
        if (!pid_is_valid(child_pid)) {
3✔
4932
                log_warning("Received pidref message without valid child PID. Ignoring.");
×
4933
                return 0;
×
4934
        }
4935

4936
        /* Need to handle kernels without PIDFD support. */
4937
        if (child_pidfd >= 0)
3✔
4938
                r = pidref_set_pidfd_consume(&child_pidref, TAKE_FD(child_pidfd));
3✔
4939
        else
4940
                r = pidref_set_pid(&child_pidref, child_pid);
×
4941
        if (r < 0) {
3✔
4942
                if (r == -ESRCH)
×
4943
                        log_debug_errno(r, "PidRef child process died before message is processed. Ignoring.");
×
4944
                else
4945
                        log_warning_errno(r, "Failed to pin pidref child process, ignoring message: %m");
×
4946
                return 0;
×
4947
        }
4948

4949
        if (child_pidref.pid != child_pid) {
3✔
4950
                assert(child_pidref.fd >= 0);
×
4951
                log_warning("Got SCM_RIGHTS for child process " PID_FMT " but PID in IOV message is " PID_FMT ". Ignoring.",
×
4952
                            child_pidref.pid, child_pid);
4953
                return 0;
×
4954
        }
4955

4956
        log_debug("Got pidref event with parent PID " PID_FMT " and child PID " PID_FMT ".", parent_pidref.pid, child_pidref.pid);
3✔
4957

4958
        /* Try finding cgroup of parent process. But if parent process exited and we're not using PIDFD, this could return NULL.
4959
         * Then fall back to finding cgroup of the child process. */
4960
        Unit *u = manager_get_unit_by_pidref_cgroup(m, &parent_pidref);
3✔
4961
        if (!u)
3✔
4962
                u = manager_get_unit_by_pidref_cgroup(m, &child_pidref);
×
4963
        if (!u) {
×
4964
                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);
×
4965
                return 0;
×
4966
        }
4967

4968
        if (!UNIT_VTABLE(u)->notify_pidref) {
3✔
4969
                log_unit_warning(u, "Received pidref event from unexpected unit type '%s'.", unit_type_to_string(u->type));
×
4970
                return 0;
×
4971
        }
4972

4973
        UNIT_VTABLE(u)->notify_pidref(u, &parent_pidref, &child_pidref);
3✔
4974

4975
        return 0;
4976
}
4977

4978
void manager_ref_console(Manager *m) {
160✔
4979
        assert(m);
160✔
4980

4981
        m->n_on_console++;
160✔
4982
}
160✔
4983

4984
void manager_unref_console(Manager *m) {
160✔
4985

4986
        assert(m->n_on_console > 0);
160✔
4987
        m->n_on_console--;
160✔
4988

4989
        if (m->n_on_console == 0)
160✔
4990
                m->no_console_output = false; /* unset no_console_output flag, since the console is definitely free now */
92✔
4991
}
160✔
4992

4993
void manager_override_log_level(Manager *m, int level) {
48✔
4994
        _cleanup_free_ char *s = NULL;
96✔
4995
        assert(m);
48✔
4996

4997
        if (!m->log_level_overridden) {
48✔
4998
                m->original_log_level = log_get_max_level();
17✔
4999
                m->log_level_overridden = true;
17✔
5000
        }
5001

5002
        (void) log_level_to_string_alloc(level, &s);
48✔
5003
        log_info("Setting log level to %s.", strna(s));
48✔
5004

5005
        log_set_max_level(level);
48✔
5006
}
48✔
5007

5008
void manager_restore_original_log_level(Manager *m) {
×
5009
        _cleanup_free_ char *s = NULL;
×
5010
        assert(m);
×
5011

5012
        if (!m->log_level_overridden)
×
5013
                return;
×
5014

5015
        (void) log_level_to_string_alloc(m->original_log_level, &s);
×
5016
        log_info("Restoring log level to original (%s).", strna(s));
×
5017

5018
        log_set_max_level(m->original_log_level);
×
5019
        m->log_level_overridden = false;
×
5020
}
5021

5022
void manager_override_log_target(Manager *m, LogTarget target) {
16✔
5023
        assert(m);
16✔
5024

5025
        if (!m->log_target_overridden) {
16✔
5026
                m->original_log_target = log_get_target();
11✔
5027
                m->log_target_overridden = true;
11✔
5028
        }
5029

5030
        log_info("Setting log target to %s.", log_target_to_string(target));
16✔
5031
        log_set_target(target);
16✔
5032
}
16✔
5033

5034
void manager_restore_original_log_target(Manager *m) {
×
5035
        assert(m);
×
5036

5037
        if (!m->log_target_overridden)
×
5038
                return;
5039

5040
        log_info("Restoring log target to original %s.", log_target_to_string(m->original_log_target));
×
5041

5042
        log_set_target(m->original_log_target);
×
5043
        m->log_target_overridden = false;
×
5044
}
5045

5046
ManagerTimestamp manager_timestamp_initrd_mangle(ManagerTimestamp s) {
3,248✔
5047
        if (in_initrd() &&
3,248✔
5048
            s >= MANAGER_TIMESTAMP_SECURITY_START &&
54✔
5049
            s <= MANAGER_TIMESTAMP_UNITS_LOAD_FINISH)
5050
                return s - MANAGER_TIMESTAMP_SECURITY_START + MANAGER_TIMESTAMP_INITRD_SECURITY_START;
54✔
5051
        return s;
5052
}
5053

5054
int manager_allocate_idle_pipe(Manager *m) {
13,383✔
5055
        int r;
13,383✔
5056

5057
        assert(m);
13,383✔
5058

5059
        if (m->idle_pipe[0] >= 0) {
13,383✔
5060
                assert(m->idle_pipe[1] >= 0);
10,747✔
5061
                assert(m->idle_pipe[2] >= 0);
10,747✔
5062
                assert(m->idle_pipe[3] >= 0);
10,747✔
5063
                return 0;
5064
        }
5065

5066
        assert(m->idle_pipe[1] < 0);
2,636✔
5067
        assert(m->idle_pipe[2] < 0);
2,636✔
5068
        assert(m->idle_pipe[3] < 0);
2,636✔
5069

5070
        r = RET_NERRNO(pipe2(m->idle_pipe + 0, O_NONBLOCK|O_CLOEXEC));
2,636✔
5071
        if (r < 0)
×
5072
                return r;
5073

5074
        r = RET_NERRNO(pipe2(m->idle_pipe + 2, O_NONBLOCK|O_CLOEXEC));
2,636✔
5075
        if (r < 0) {
×
5076
                safe_close_pair(m->idle_pipe + 0);
×
5077
                return r;
×
5078
        }
5079

5080
        return 1;
5081
}
5082

5083
void unit_defaults_init(UnitDefaults *defaults, RuntimeScope scope) {
1,130✔
5084
        assert(defaults);
1,130✔
5085
        assert(scope >= 0);
1,130✔
5086
        assert(scope < _RUNTIME_SCOPE_MAX);
1,130✔
5087

5088
        *defaults = (UnitDefaults) {
2,260✔
5089
                .std_output = EXEC_OUTPUT_JOURNAL,
5090
                .std_error = EXEC_OUTPUT_INHERIT,
5091
                .restart_usec = DEFAULT_RESTART_USEC,
5092
                .timeout_start_usec = manager_default_timeout(scope),
5093
                .timeout_stop_usec = manager_default_timeout(scope),
5094
                .timeout_abort_usec = manager_default_timeout(scope),
5095
                .timeout_abort_set = false,
5096
                .device_timeout_usec = manager_default_timeout(scope),
5097
                .start_limit = { DEFAULT_START_LIMIT_INTERVAL, DEFAULT_START_LIMIT_BURST },
5098

5099
                /* On 4.15+ with unified hierarchy, CPU accounting is essentially free as it doesn't require the CPU
5100
                 * controller to be enabled, so the default is to enable it unless we got told otherwise. */
5101
                .cpu_accounting = cpu_accounting_is_cheap(),
1,130✔
5102
                .memory_accounting = MEMORY_ACCOUNTING_DEFAULT,
5103
                .io_accounting = false,
5104
                .blockio_accounting = false,
5105
                .tasks_accounting = true,
5106
                .ip_accounting = false,
5107

5108
                .tasks_max = DEFAULT_TASKS_MAX,
5109
                .timer_accuracy_usec = 1 * USEC_PER_MINUTE,
5110

5111
                .memory_pressure_watch = CGROUP_PRESSURE_WATCH_AUTO,
5112
                .memory_pressure_threshold_usec = MEMORY_PRESSURE_DEFAULT_THRESHOLD_USEC,
5113

5114
                .oom_policy = OOM_STOP,
5115
                .oom_score_adjust_set = false,
5116
        };
5117
}
1,130✔
5118

5119
void unit_defaults_done(UnitDefaults *defaults) {
1,130✔
5120
        assert(defaults);
1,130✔
5121

5122
        defaults->smack_process_label = mfree(defaults->smack_process_label);
1,130✔
5123
        rlimit_free_all(defaults->rlimit);
1,130✔
5124
}
1,130✔
5125

5126
LogTarget manager_get_executor_log_target(Manager *m) {
2,199✔
5127
        assert(m);
2,199✔
5128

5129
        /* If journald is not available tell sd-executor to go to kmsg, as it might be starting journald */
5130
        if (!MANAGER_IS_TEST_RUN(m) && !manager_journal_is_running(m))
2,199✔
5131
                return LOG_TARGET_KMSG;
5132

5133
        return log_get_target();
1,766✔
5134
}
5135

5136
static const char* const manager_state_table[_MANAGER_STATE_MAX] = {
5137
        [MANAGER_INITIALIZING] = "initializing",
5138
        [MANAGER_STARTING]     = "starting",
5139
        [MANAGER_RUNNING]      = "running",
5140
        [MANAGER_DEGRADED]     = "degraded",
5141
        [MANAGER_MAINTENANCE]  = "maintenance",
5142
        [MANAGER_STOPPING]     = "stopping",
5143
};
5144

5145
DEFINE_STRING_TABLE_LOOKUP(manager_state, ManagerState);
75✔
5146

5147
static const char* const manager_objective_table[_MANAGER_OBJECTIVE_MAX] = {
5148
        [MANAGER_OK]          = "ok",
5149
        [MANAGER_EXIT]        = "exit",
5150
        [MANAGER_RELOAD]      = "reload",
5151
        [MANAGER_REEXECUTE]   = "reexecute",
5152
        [MANAGER_REBOOT]      = "reboot",
5153
        [MANAGER_SOFT_REBOOT] = "soft-reboot",
5154
        [MANAGER_POWEROFF]    = "poweroff",
5155
        [MANAGER_HALT]        = "halt",
5156
        [MANAGER_KEXEC]       = "kexec",
5157
        [MANAGER_SWITCH_ROOT] = "switch-root",
5158
};
5159

5160
DEFINE_STRING_TABLE_LOOKUP(manager_objective, ManagerObjective);
159✔
5161

5162
static const char* const manager_timestamp_table[_MANAGER_TIMESTAMP_MAX] = {
5163
        [MANAGER_TIMESTAMP_FIRMWARE]                 = "firmware",
5164
        [MANAGER_TIMESTAMP_LOADER]                   = "loader",
5165
        [MANAGER_TIMESTAMP_KERNEL]                   = "kernel",
5166
        [MANAGER_TIMESTAMP_INITRD]                   = "initrd",
5167
        [MANAGER_TIMESTAMP_USERSPACE]                = "userspace",
5168
        [MANAGER_TIMESTAMP_FINISH]                   = "finish",
5169
        [MANAGER_TIMESTAMP_SECURITY_START]           = "security-start",
5170
        [MANAGER_TIMESTAMP_SECURITY_FINISH]          = "security-finish",
5171
        [MANAGER_TIMESTAMP_GENERATORS_START]         = "generators-start",
5172
        [MANAGER_TIMESTAMP_GENERATORS_FINISH]        = "generators-finish",
5173
        [MANAGER_TIMESTAMP_UNITS_LOAD_START]         = "units-load-start",
5174
        [MANAGER_TIMESTAMP_UNITS_LOAD_FINISH]        = "units-load-finish",
5175
        [MANAGER_TIMESTAMP_UNITS_LOAD]               = "units-load",
5176
        [MANAGER_TIMESTAMP_INITRD_SECURITY_START]    = "initrd-security-start",
5177
        [MANAGER_TIMESTAMP_INITRD_SECURITY_FINISH]   = "initrd-security-finish",
5178
        [MANAGER_TIMESTAMP_INITRD_GENERATORS_START]  = "initrd-generators-start",
5179
        [MANAGER_TIMESTAMP_INITRD_GENERATORS_FINISH] = "initrd-generators-finish",
5180
        [MANAGER_TIMESTAMP_INITRD_UNITS_LOAD_START]  = "initrd-units-load-start",
5181
        [MANAGER_TIMESTAMP_INITRD_UNITS_LOAD_FINISH] = "initrd-units-load-finish",
5182
        [MANAGER_TIMESTAMP_SHUTDOWN_START]           = "shutdown-start",
5183
};
5184

5185
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