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

systemd / systemd / 21192089104

20 Jan 2026 11:35PM UTC coverage: 72.524% (-0.3%) from 72.818%
21192089104

push

github

yuwata
mkdir: reset mtime *after* fchown()

Follow-up for 34c3d5747

Also, drop pointless shortcut.

1 of 2 new or added lines in 1 file covered. (50.0%)

2960 existing lines in 48 files now uncovered.

309808 of 427181 relevant lines covered (72.52%)

1236537.64 hits per line

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

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

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

12
#include "sd-bus.h"
13
#include "sd-daemon.h"
14
#include "sd-messages.h"
15
#include "sd-netlink.h"
16
#include "sd-path.h"
17

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

100
/* Make sure clients notifying us don't block */
101
#define MANAGER_SOCKET_RCVBUF_SIZE (8*U64_MB)
102

103
/* Initial delay and the interval for printing status messages about running jobs */
104
#define JOBS_IN_PROGRESS_WAIT_USEC (2*USEC_PER_SEC)
105
#define JOBS_IN_PROGRESS_QUIET_WAIT_USEC (25*USEC_PER_SEC)
106
#define JOBS_IN_PROGRESS_PERIOD_USEC (USEC_PER_SEC / 3)
107
#define JOBS_IN_PROGRESS_PERIOD_DIVISOR 3
108

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

113
/* How many units and jobs to process of the bus queue before returning to the event loop. */
114
#define MANAGER_BUS_MESSAGE_BUDGET 100U
115

116
#define DEFAULT_TASKS_MAX ((const CGroupTasksMax) { 15U, 100U }) /* 15% */
117

118
static int manager_dispatch_notify_fd(sd_event_source *source, int fd, uint32_t revents, void *userdata);
119
static int manager_dispatch_signal_fd(sd_event_source *source, int fd, uint32_t revents, void *userdata);
120
static int manager_dispatch_time_change_fd(sd_event_source *source, int fd, uint32_t revents, void *userdata);
121
static int manager_dispatch_idle_pipe_fd(sd_event_source *source, int fd, uint32_t revents, void *userdata);
122
static int manager_dispatch_user_lookup_fd(sd_event_source *source, int fd, uint32_t revents, void *userdata);
123
static int manager_dispatch_handoff_timestamp_fd(sd_event_source *source, int fd, uint32_t revents, void *userdata);
124
static int manager_dispatch_pidref_transport_fd(sd_event_source *source, int fd, uint32_t revents, void *userdata);
125
static int manager_dispatch_jobs_in_progress(sd_event_source *source, usec_t usec, void *userdata);
126
static int manager_dispatch_run_queue(sd_event_source *source, void *userdata);
127
static int manager_dispatch_sigchld(sd_event_source *source, void *userdata);
128
static int manager_dispatch_timezone_change(sd_event_source *source, const struct inotify_event *event, void *userdata);
129
static int manager_run_environment_generators(Manager *m);
130
static int manager_run_generators(Manager *m);
131
static void manager_vacuum(Manager *m);
132

133
static usec_t manager_watch_jobs_next_time(Manager *m) {
5,018✔
134
        usec_t timeout;
5,018✔
135

136
        if (MANAGER_IS_USER(m))
5,018✔
137
                /* Let the user manager without a timeout show status quickly, so the system manager can make
138
                 * use of it, if it wants to. */
139
                timeout = JOBS_IN_PROGRESS_WAIT_USEC * 2 / 3;
140
        else if (manager_get_show_status_on(m))
4,306✔
141
                /* When status is on, just use the usual timeout. */
142
                timeout = JOBS_IN_PROGRESS_WAIT_USEC;
143
        else
144
                timeout = JOBS_IN_PROGRESS_QUIET_WAIT_USEC;
4,306✔
145

146
        return usec_add(now(CLOCK_MONOTONIC), timeout);
5,018✔
147
}
148

149
static bool manager_is_confirm_spawn_disabled(Manager *m) {
3,596✔
150
        assert(m);
3,596✔
151

152
        if (!m->confirm_spawn)
3,596✔
153
                return true;
154

155
        return access("/run/systemd/confirm_spawn_disabled", F_OK) >= 0;
×
156
}
157

158
static void manager_watch_jobs_in_progress(Manager *m) {
3,596✔
159
        usec_t next;
3,596✔
160
        int r;
3,596✔
161

162
        assert(m);
3,596✔
163

164
        /* We do not want to show the cylon animation if the user
165
         * needs to confirm service executions otherwise confirmation
166
         * messages will be screwed by the cylon animation. */
167
        if (!manager_is_confirm_spawn_disabled(m))
3,596✔
168
                return;
169

170
        if (m->jobs_in_progress_event_source)
3,596✔
171
                return;
172

173
        next = manager_watch_jobs_next_time(m);
798✔
174
        r = sd_event_add_time(
798✔
175
                        m->event,
176
                        &m->jobs_in_progress_event_source,
177
                        CLOCK_MONOTONIC,
178
                        next, 0,
179
                        manager_dispatch_jobs_in_progress, m);
180
        if (r < 0)
798✔
181
                return;
182

183
        (void) sd_event_source_set_description(m->jobs_in_progress_event_source, "manager-jobs-in-progress");
798✔
184
}
185

186
static void manager_flip_auto_status(Manager *m, bool enable, const char *reason) {
197✔
187
        assert(m);
197✔
188

189
        if (enable) {
197✔
190
                if (m->show_status == SHOW_STATUS_AUTO)
×
191
                        manager_set_show_status(m, SHOW_STATUS_TEMPORARY, reason);
×
192
        } else {
193
                if (m->show_status == SHOW_STATUS_TEMPORARY)
197✔
194
                        manager_set_show_status(m, SHOW_STATUS_AUTO, reason);
×
195
        }
196
}
197✔
197

198
static void manager_print_jobs_in_progress(Manager *m) {
×
199
        Job *j;
×
200
        unsigned counter = 0, print_nr;
×
201
        char cylon[6 + CYLON_BUFFER_EXTRA + 1];
×
202
        unsigned cylon_pos;
×
203
        uint64_t timeout = 0;
×
204

205
        assert(m);
×
206
        assert(m->n_running_jobs > 0);
×
207

208
        manager_flip_auto_status(m, true, "delay");
×
209

210
        print_nr = (m->jobs_in_progress_iteration / JOBS_IN_PROGRESS_PERIOD_DIVISOR) % m->n_running_jobs;
×
211

212
        HASHMAP_FOREACH(j, m->jobs)
×
213
                if (j->state == JOB_RUNNING && counter++ == print_nr)
×
214
                        break;
215

216
        /* m->n_running_jobs must be consistent with the contents of m->jobs,
217
         * so the above loop must have succeeded in finding j. */
218
        assert(counter == print_nr + 1);
×
219
        assert(j);
×
220

221
        cylon_pos = m->jobs_in_progress_iteration % 14;
×
222
        if (cylon_pos >= 8)
×
223
                cylon_pos = 14 - cylon_pos;
×
224
        draw_cylon(cylon, sizeof(cylon), 6, cylon_pos);
×
225

226
        m->jobs_in_progress_iteration++;
×
227

228
        char job_of_n[STRLEN("( of ) ") + DECIMAL_STR_MAX(unsigned)*2] = "";
×
229
        if (m->n_running_jobs > 1)
×
230
                xsprintf(job_of_n, "(%u of %u) ", counter, m->n_running_jobs);
×
231

232
        (void) job_get_timeout(j, &timeout);
×
233

234
        /* We want to use enough information for the user to identify previous lines talking about the same
235
         * unit, but keep the message as short as possible. So if 'Starting foo.service' or 'Starting
236
         * foo.service - Description' were used, 'foo.service' is enough here. On the other hand, if we used
237
         * 'Starting Description' before, then we shall also use 'Description' here. So we pass NULL as the
238
         * second argument to unit_status_string(). */
239
        const char *ident = unit_status_string(j->unit, NULL);
×
240

241
        const char *time = FORMAT_TIMESPAN(now(CLOCK_MONOTONIC) - j->begin_usec, 1*USEC_PER_SEC);
×
242
        const char *limit = timeout > 0 ? FORMAT_TIMESPAN(timeout - j->begin_usec, 1*USEC_PER_SEC) : "no limit";
×
243

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

256
                manager_status_printf(m, STATUS_TYPE_EPHEMERAL, cylon,
×
257
                                      "%sJob %s/%s running (%s / %s)%s%s",
258
                                      job_of_n,
259
                                      ident,
260
                                      job_type_to_string(j->type),
×
261
                                      time, limit,
262
                                      status_text ? ": " : "",
263
                                      strempty(status_text));
264
        }
265

266
        (void) sd_notifyf(/* unset_environment= */ false,
×
267
                          "STATUS=%sUser job %s/%s running (%s / %s)...",
268
                          job_of_n,
269
                          ident, job_type_to_string(j->type),
×
270
                          time, limit);
271
        m->status_ready = false;
×
272
}
×
273

274
static int have_ask_password(void) {
54✔
275
        _cleanup_closedir_ DIR *dir = NULL;
54✔
276

277
        dir = opendir("/run/systemd/ask-password");
54✔
278
        if (!dir) {
54✔
279
                if (errno == ENOENT)
×
280
                        return false;
281

282
                return -errno;
×
283
        }
284

285
        FOREACH_DIRENT_ALL(de, dir, return -errno) {
162✔
286
                if (!IN_SET(de->d_type, DT_REG, DT_UNKNOWN))
108✔
287
                        continue;
108✔
288

289
                if (startswith(de->d_name, "ask."))
×
290
                        return true;
291
        }
292

293
        return false;
294
}
295

296
static int manager_dispatch_ask_password_fd(sd_event_source *source,
54✔
297
                                            int fd, uint32_t revents, void *userdata) {
298
        Manager *m = ASSERT_PTR(userdata);
54✔
299

300
        (void) flush_fd(fd);
54✔
301

302
        m->have_ask_password = have_ask_password();
54✔
303
        if (m->have_ask_password < 0)
54✔
304
                /* Log error but continue. Negative have_ask_password is treated as unknown status. */
305
                log_warning_errno(m->have_ask_password, "Failed to list /run/systemd/ask-password/, ignoring: %m");
×
306

307
        return 0;
54✔
308
}
309

310
static void manager_close_ask_password(Manager *m) {
935✔
311
        assert(m);
935✔
312

313
        m->ask_password_event_source = sd_event_source_disable_unref(m->ask_password_event_source);
935✔
314
        m->have_ask_password = -EINVAL;
935✔
315
}
935✔
316

317
static int manager_check_ask_password(Manager *m) {
8,406✔
318
        int r;
8,406✔
319

320
        assert(m);
8,406✔
321

322
        /* We only care about passwords prompts when running in system mode (because that's the only time we
323
         * manage a console) */
324
        if (!MANAGER_IS_SYSTEM(m))
8,406✔
325
                return 0;
326

327
        if (!m->ask_password_event_source) {
8,406✔
328
                _cleanup_close_ int inotify_fd = inotify_init1(IN_NONBLOCK|IN_CLOEXEC);
104✔
329
                if (inotify_fd < 0)
52✔
330
                        return log_error_errno(errno, "Failed to create inotify object: %m");
×
331

332
                (void) mkdir_label("/run/systemd/ask-password", 0755);
52✔
333
                r = inotify_add_watch_and_warn(inotify_fd, "/run/systemd/ask-password", IN_CLOSE_WRITE|IN_DELETE|IN_MOVED_TO|IN_ONLYDIR);
52✔
334
                if (r < 0)
52✔
335
                        return r;
336

337
                _cleanup_(sd_event_source_disable_unrefp) sd_event_source *event_source = NULL;
52✔
338
                r = sd_event_add_io(
52✔
339
                                m->event,
340
                                &event_source,
341
                                inotify_fd,
342
                                EPOLLIN,
343
                                manager_dispatch_ask_password_fd,
344
                                m);
345
                if (r < 0)
52✔
346
                        return log_error_errno(r, "Failed to add event source for /run/systemd/ask-password/: %m");
×
347

348
                r = sd_event_source_set_io_fd_own(event_source, true);
52✔
349
                if (r < 0)
52✔
350
                        return log_error_errno(r, "Failed to pass ownership of /run/systemd/ask-password/ inotify fd to event source: %m");
×
351
                TAKE_FD(inotify_fd);
52✔
352

353
                (void) sd_event_source_set_description(event_source, "manager-ask-password");
52✔
354

355
                m->ask_password_event_source = TAKE_PTR(event_source);
52✔
356

357
                /* Queries might have been added meanwhile... */
358
                (void) manager_dispatch_ask_password_fd(m->ask_password_event_source, sd_event_source_get_io_fd(m->ask_password_event_source), EPOLLIN, m);
52✔
359
        }
360

361
        return m->have_ask_password;
8,406✔
362
}
363

364
static int manager_watch_idle_pipe(Manager *m) {
755✔
365
        int r;
755✔
366

367
        assert(m);
755✔
368

369
        if (m->idle_pipe_event_source)
755✔
370
                return 0;
371

372
        if (m->idle_pipe[2] < 0)
42✔
373
                return 0;
374

375
        r = sd_event_add_io(m->event, &m->idle_pipe_event_source, m->idle_pipe[2], EPOLLIN, manager_dispatch_idle_pipe_fd, m);
42✔
376
        if (r < 0)
42✔
377
                return log_error_errno(r, "Failed to watch idle pipe: %m");
×
378

379
        (void) sd_event_source_set_description(m->idle_pipe_event_source, "manager-idle-pipe");
42✔
380

381
        return 0;
42✔
382
}
383

384
static void manager_close_idle_pipe(Manager *m) {
1,088✔
385
        assert(m);
1,088✔
386

387
        m->idle_pipe_event_source = sd_event_source_disable_unref(m->idle_pipe_event_source);
1,088✔
388

389
        safe_close_pair(m->idle_pipe);
1,088✔
390
        safe_close_pair(m->idle_pipe + 2);
1,088✔
391
}
1,088✔
392

393
static int manager_setup_time_change(Manager *m) {
264✔
394
        int r;
264✔
395

396
        assert(m);
264✔
397

398
        if (MANAGER_IS_TEST_RUN(m))
264✔
399
                return 0;
400

401
        m->time_change_event_source = sd_event_source_disable_unref(m->time_change_event_source);
253✔
402

403
        r = event_add_time_change(m->event, &m->time_change_event_source, manager_dispatch_time_change_fd, m);
253✔
404
        if (r < 0)
253✔
405
                return log_error_errno(r, "Failed to create time change event source: %m");
×
406

407
        /* Schedule this slightly earlier than the .timer event sources */
408
        r = sd_event_source_set_priority(m->time_change_event_source, EVENT_PRIORITY_TIME_CHANGE);
253✔
409
        if (r < 0)
253✔
410
                return log_error_errno(r, "Failed to set priority of time change event sources: %m");
×
411

412
        log_debug("Set up TFD_TIMER_CANCEL_ON_SET timerfd.");
253✔
413

414
        return 0;
415
}
416

417
static int manager_read_timezone_stat(Manager *m) {
319✔
418
        struct stat st;
319✔
419
        bool changed;
319✔
420

421
        assert(m);
319✔
422

423
        /* Read the current stat() data of /etc/localtime so that we detect changes */
424
        if (lstat(etc_localtime(), &st) < 0) {
319✔
425
                log_debug_errno(errno, "Failed to stat /etc/localtime, ignoring: %m");
74✔
426
                changed = m->etc_localtime_accessible;
74✔
427
                m->etc_localtime_accessible = false;
74✔
428
        } else {
429
                usec_t k;
245✔
430

431
                k = timespec_load(&st.st_mtim);
245✔
432
                changed = !m->etc_localtime_accessible || k != m->etc_localtime_mtime;
245✔
433

434
                m->etc_localtime_mtime = k;
245✔
435
                m->etc_localtime_accessible = true;
245✔
436
        }
437

438
        return changed;
319✔
439
}
440

441
static int manager_setup_timezone_change(Manager *m) {
287✔
442
        _cleanup_(sd_event_source_unrefp) sd_event_source *new_event = NULL;
287✔
443
        int r;
287✔
444

445
        assert(m);
287✔
446

447
        if (MANAGER_IS_TEST_RUN(m))
287✔
448
                return 0;
449

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

461
        r = sd_event_add_inotify(m->event, &new_event, etc_localtime(),
276✔
462
                                 IN_ATTRIB|IN_MOVE_SELF|IN_CLOSE_WRITE|IN_DONT_FOLLOW, manager_dispatch_timezone_change, m);
463
        if (r == -ENOENT) {
276✔
464
                /* If the file doesn't exist yet, subscribe to /etc instead, and wait until it is created either by
465
                 * O_CREATE or by rename() */
466
                _cleanup_free_ char *localtime_dir = NULL;
49✔
467

468
                int dir_r = path_extract_directory(etc_localtime(), &localtime_dir);
49✔
469
                if (dir_r < 0)
49✔
470
                        return log_error_errno(dir_r, "Failed to extract directory from path '%s': %m", etc_localtime());
×
471

472
                log_debug_errno(r, "%s doesn't exist yet, watching %s instead.", etc_localtime(), localtime_dir);
49✔
473

474
                r = sd_event_add_inotify(m->event, &new_event, localtime_dir,
49✔
475
                                         IN_CREATE|IN_MOVED_TO|IN_ONLYDIR, manager_dispatch_timezone_change, m);
476
        }
477
        if (r < 0)
276✔
478
                return log_error_errno(r, "Failed to create timezone change event source: %m");
×
479

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

485
        sd_event_source_unref(m->timezone_change_event_source);
276✔
486
        m->timezone_change_event_source = TAKE_PTR(new_event);
276✔
487

488
        return 0;
276✔
489
}
490

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

494
        assert(m);
254✔
495

496
        if (!MANAGER_IS_SYSTEM(m) || MANAGER_IS_TEST_RUN(m))
254✔
497
                return 0;
498

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

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

514
        return 0;
515
}
516

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

525
        assert(m);
254✔
526

527
        assert_se(sigaction(SIGCHLD, &sa, NULL) == 0);
254✔
528

529
        /* We make liberal use of realtime signals here. On Linux we have 29 of them, between
530
         * SIGRTMIN+0 ... SIGRTMIN+29. The glibc has one more (SIGRTMAX is SIGRTMIN+30),
531
         * but musl does not (SIGRTMAX is SIGRTMIN+29). */
532

533
        assert_se(sigemptyset(&mask) == 0);
254✔
534
        sigset_add_many(&mask,
254✔
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 (glibc only) ... */
577
                        -1);
578
        assert_se(sigprocmask(SIG_SETMASK, &mask, NULL) == 0);
254✔
579

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

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

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

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

614
        /* Let's remove some environment variables that we need ourselves to communicate with our clients */
615
        strv_env_unset_many(
1,285✔
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
                        "LISTEN_PIDFDID",
628
                        "LOGS_DIRECTORY",
629
                        "LOG_NAMESPACE",
630
                        "MAINPID",
631
                        "MANAGERPID",
632
                        "MEMORY_PRESSURE_WATCH",
633
                        "MEMORY_PRESSURE_WRITE",
634
                        "MONITOR_EXIT_CODE",
635
                        "MONITOR_EXIT_STATUS",
636
                        "MONITOR_INVOCATION_ID",
637
                        "MONITOR_SERVICE_RESULT",
638
                        "MONITOR_UNIT",
639
                        "NOTIFY_SOCKET",
640
                        "PIDFILE",
641
                        "REMOTE_ADDR",
642
                        "REMOTE_PORT",
643
                        "RUNTIME_DIRECTORY",
644
                        "SERVICE_RESULT",
645
                        "STATE_DIRECTORY",
646
                        "SYSTEMD_EXEC_PID",
647
                        "TRIGGER_PATH",
648
                        "TRIGGER_TIMER_MONOTONIC_USEC",
649
                        "TRIGGER_TIMER_REALTIME_USEC",
650
                        "TRIGGER_UNIT",
651
                        "WATCHDOG_PID",
652
                        "WATCHDOG_USEC");
653

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

658
int manager_default_environment(Manager *m) {
1,012✔
659
        assert(m);
1,012✔
660

661
        m->transient_environment = strv_free(m->transient_environment);
1,012✔
662

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

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

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

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

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

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

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

704
        sanitize_environment(m->transient_environment);
1,012✔
705
        return 0;
1,012✔
706
}
707

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

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

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

730
        assert(m);
738✔
731

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

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

742
        return 0;
743
}
744

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

752
static int manager_setup_run_queue(Manager *m) {
738✔
753
        int r;
738✔
754

755
        assert(m);
738✔
756
        assert(!m->run_queue_event_source);
738✔
757

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

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

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

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

772
        return 0;
738✔
773
}
774

775
static int manager_setup_sigchld_event_source(Manager *m) {
254✔
776
        int r;
254✔
777

778
        assert(m);
254✔
779
        assert(!m->sigchld_event_source);
254✔
780

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

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

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

793
        (void) sd_event_source_set_description(m->sigchld_event_source, "manager-sigchld");
254✔
794

795
        return 0;
254✔
796
}
797

798
int manager_setup_memory_pressure_event_source(Manager *m) {
531✔
799
        int r;
531✔
800

801
        assert(m);
531✔
802

803
        m->memory_pressure_event_source = sd_event_source_disable_unref(m->memory_pressure_event_source);
531✔
804

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

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

820
        return 0;
531✔
821
}
822

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

827
        assert(m);
738✔
828

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

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

849
        return 0;
850
}
851

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

855
        m->switching_root = MANAGER_IS_SYSTEM(m) && switching_root;
990✔
856
}
990✔
857

858
double manager_get_progress(Manager *m) {
21✔
859
        assert(m);
21✔
860

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

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

867
static int compare_job_priority(const void *a, const void *b) {
315,179✔
868
        const Job *x = a, *y = b;
315,179✔
869

870
        return unit_compare_priority(x->unit, y->unit);
315,179✔
871
}
872

873
usec_t manager_default_timeout(RuntimeScope scope) {
4,796✔
874
        return scope == RUNTIME_SCOPE_SYSTEM ? DEFAULT_TIMEOUT_USEC : DEFAULT_USER_TIMEOUT_USEC;
4,796✔
875
}
876

877
int manager_new(RuntimeScope runtime_scope, ManagerTestRunFlags test_run_flags, Manager **ret) {
738✔
878
        _cleanup_(manager_freep) Manager *m = NULL;
738✔
879
        int r;
738✔
880

881
        assert(IN_SET(runtime_scope, RUNTIME_SCOPE_SYSTEM, RUNTIME_SCOPE_USER));
738✔
882
        assert(ret);
738✔
883

884
        m = new(Manager, 1);
738✔
885
        if (!m)
738✔
886
                return -ENOMEM;
887

888
        *m = (Manager) {
738✔
889
                .runtime_scope = runtime_scope,
890
                .objective = _MANAGER_OBJECTIVE_INVALID,
891
                .previous_objective = _MANAGER_OBJECTIVE_INVALID,
892

893
                .status_unit_format = STATUS_UNIT_FORMAT_DEFAULT,
894

895
                .original_log_level = -1,
896
                .original_log_target = _LOG_TARGET_INVALID,
897

898
                .watchdog_overridden[WATCHDOG_RUNTIME] = USEC_INFINITY,
899
                .watchdog_overridden[WATCHDOG_REBOOT] = USEC_INFINITY,
900
                .watchdog_overridden[WATCHDOG_KEXEC] = USEC_INFINITY,
901
                .watchdog_overridden[WATCHDOG_PRETIMEOUT] = USEC_INFINITY,
902

903
                .show_status_overridden = _SHOW_STATUS_INVALID,
904

905
                .notify_fd = -EBADF,
906
                .signal_fd = -EBADF,
907
                .user_lookup_fds = EBADF_PAIR,
908
                .handoff_timestamp_fds = EBADF_PAIR,
909
                .pidref_transport_fds = EBADF_PAIR,
910
                .private_listen_fd = -EBADF,
911
                .dev_autofs_fd = -EBADF,
912
                .cgroup_inotify_fd = -EBADF,
913
                .pin_cgroupfs_fd = -EBADF,
914
                .idle_pipe = { -EBADF, -EBADF, -EBADF, -EBADF},
915

916
                 /* start as id #1, so that we can leave #0 around as "null-like" value */
917
                .current_job_id = 1,
918

919
                .have_ask_password = -EINVAL, /* we don't know */
920
                .first_boot = -1,
921
                .test_run_flags = test_run_flags,
922

923
                .dump_ratelimit = (const RateLimit) { .interval = 10 * USEC_PER_MINUTE, .burst = 10 },
924

925
                .executor_fd = -EBADF,
926
        };
927

928
        unit_defaults_init(&m->defaults, runtime_scope);
738✔
929

930
#if ENABLE_EFI
931
        if (MANAGER_IS_SYSTEM(m) && detect_container() <= 0)
738✔
932
                boot_timestamps(m->timestamps + MANAGER_TIMESTAMP_USERSPACE,
27✔
933
                                m->timestamps + MANAGER_TIMESTAMP_FIRMWARE,
27✔
934
                                m->timestamps + MANAGER_TIMESTAMP_LOADER);
27✔
935
#endif
936

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

940
        r = manager_default_environment(m);
738✔
941
        if (r < 0)
738✔
942
                return r;
943

944
        r = hashmap_ensure_allocated(&m->units, &string_hash_ops);
738✔
945
        if (r < 0)
738✔
946
                return r;
947

948
        r = hashmap_ensure_allocated(&m->cgroup_unit, &path_hash_ops);
738✔
949
        if (r < 0)
738✔
950
                return r;
951

952
        r = hashmap_ensure_allocated(&m->watch_bus, &string_hash_ops);
738✔
953
        if (r < 0)
738✔
954
                return r;
955

956
        r = prioq_ensure_allocated(&m->run_queue, compare_job_priority);
738✔
957
        if (r < 0)
738✔
958
                return r;
959

960
        r = manager_setup_prefix(m);
738✔
961
        if (r < 0)
738✔
962
                return r;
963

964
        r = manager_find_credentials_dirs(m);
738✔
965
        if (r < 0)
738✔
966
                return r;
967

968
        r = sd_event_default(&m->event);
738✔
969
        if (r < 0)
738✔
970
                return r;
971

972
        r = manager_setup_run_queue(m);
738✔
973
        if (r < 0)
738✔
974
                return r;
975

976
        if (FLAGS_SET(test_run_flags, MANAGER_TEST_RUN_MINIMAL)) {
738✔
977
                m->cgroup_root = strdup("");
484✔
978
                if (!m->cgroup_root)
484✔
979
                        return -ENOMEM;
980
        } else {
981
                r = manager_setup_signals(m);
254✔
982
                if (r < 0)
254✔
983
                        return r;
984

985
                r = manager_setup_cgroup(m);
254✔
986
                if (r < 0)
254✔
987
                        return r;
988

989
                r = manager_setup_time_change(m);
254✔
990
                if (r < 0)
254✔
991
                        return r;
992

993
                r = manager_read_timezone_stat(m);
254✔
994
                if (r < 0)
254✔
995
                        return r;
996

997
                (void) manager_setup_timezone_change(m);
254✔
998

999
                r = manager_setup_sigchld_event_source(m);
254✔
1000
                if (r < 0)
254✔
1001
                        return r;
1002

1003
                r = manager_setup_memory_pressure_event_source(m);
254✔
1004
                if (r < 0)
254✔
1005
                        return r;
1006

1007
#if HAVE_LIBBPF
1008
                if (MANAGER_IS_SYSTEM(m) && bpf_restrict_fs_supported(/* initialize= */ true)) {
254✔
1009
                        r = bpf_restrict_fs_setup(m);
14✔
1010
                        if (r < 0)
14✔
1011
                                log_warning_errno(r, "Failed to setup LSM BPF, ignoring: %m");
×
1012
                }
1013
#endif
1014
        }
1015

1016
        if (test_run_flags == 0) {
738✔
1017
                if (MANAGER_IS_SYSTEM(m))
243✔
1018
                        r = mkdir_label("/run/systemd/units", 0755);
55✔
1019
                else {
1020
                        _cleanup_free_ char *units_path = NULL;
188✔
1021
                        r = xdg_user_runtime_dir("/systemd/units", &units_path);
188✔
1022
                        if (r < 0)
188✔
1023
                                return r;
×
1024

1025
                        r = mkdir_label(units_path, 0755);
188✔
1026
                }
1027
                if (r < 0 && r != -EEXIST)
243✔
1028
                        return r;
1029
        }
1030

1031
        if (!FLAGS_SET(test_run_flags, MANAGER_TEST_DONT_OPEN_EXECUTOR)) {
738✔
1032
                m->executor_fd = pin_callout_binary(SYSTEMD_EXECUTOR_BINARY_PATH, &m->executor_path);
261✔
1033
                if (m->executor_fd < 0)
261✔
1034
                        return log_debug_errno(m->executor_fd, "Failed to pin executor binary: %m");
×
1035

1036
                log_debug("Using systemd-executor binary from '%s'.", m->executor_path);
261✔
1037
        }
1038

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

1042
        *ret = TAKE_PTR(m);
738✔
1043

1044
        return 0;
738✔
1045
}
1046

1047
static int manager_setup_notify(Manager *m) {
767✔
1048
        int r;
767✔
1049

1050
        if (MANAGER_IS_TEST_RUN(m))
767✔
1051
                return 0;
1052

1053
        if (m->notify_fd < 0) {
274✔
1054
                _cleanup_close_ int fd = -EBADF;
223✔
1055
                union sockaddr_union sa;
223✔
1056
                socklen_t sa_len;
223✔
1057

1058
                /* First free all secondary fields */
1059
                m->notify_socket = mfree(m->notify_socket);
223✔
1060
                m->notify_event_source = sd_event_source_disable_unref(m->notify_event_source);
223✔
1061

1062
                fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0);
223✔
1063
                if (fd < 0)
223✔
1064
                        return log_error_errno(errno, "Failed to allocate notification socket: %m");
×
1065

1066
                (void) fd_increase_rxbuf(fd, MANAGER_SOCKET_RCVBUF_SIZE);
223✔
1067

1068
                m->notify_socket = path_join(m->prefix[EXEC_DIRECTORY_RUNTIME], "systemd/notify");
223✔
1069
                if (!m->notify_socket)
223✔
1070
                        return log_oom();
×
1071

1072
                r = sockaddr_un_set_path(&sa.un, m->notify_socket);
223✔
1073
                if (r < 0)
223✔
1074
                        return log_error_errno(r, "Notify socket '%s' not valid for AF_UNIX socket address, refusing.",
×
1075
                                               m->notify_socket);
1076
                sa_len = r;
223✔
1077

1078
                (void) sockaddr_un_unlink(&sa.un);
223✔
1079

1080
                r = mac_selinux_bind(fd, &sa.sa, sa_len);
223✔
1081
                if (r < 0)
223✔
1082
                        return log_error_errno(r, "Failed to bind notify fd to '%s': %m", m->notify_socket);
×
1083

1084
                r = setsockopt_int(fd, SOL_SOCKET, SO_PASSCRED, true);
223✔
1085
                if (r < 0)
223✔
1086
                        return log_error_errno(r, "Failed to enable SO_PASSCRED for notify socket: %m");
×
1087

1088
                // TODO: enforce SO_PASSPIDFD when our baseline of the kernel version is bumped to >= 6.5.
1089
                r = setsockopt_int(fd, SOL_SOCKET, SO_PASSPIDFD, true);
223✔
1090
                if (r < 0 && r != -ENOPROTOOPT)
223✔
1091
                        log_warning_errno(r, "Failed to enable SO_PASSPIDFD for notify socket, ignoring: %m");
×
1092

1093
                m->notify_fd = TAKE_FD(fd);
223✔
1094

1095
                log_debug("Using notification socket %s", m->notify_socket);
223✔
1096
        }
1097

1098
        if (!m->notify_event_source) {
274✔
1099
                r = sd_event_add_io(m->event, &m->notify_event_source, m->notify_fd, EPOLLIN, manager_dispatch_notify_fd, m);
274✔
1100
                if (r < 0)
274✔
1101
                        return log_error_errno(r, "Failed to allocate notify event source: %m");
×
1102

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

1109
                (void) sd_event_source_set_description(m->notify_event_source, "manager-notify");
274✔
1110
        }
1111

1112
        return 0;
1113
}
1114

1115
static int manager_setup_user_lookup_fd(Manager *m) {
767✔
1116
        int r;
767✔
1117

1118
        assert(m);
767✔
1119

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

1139
        if (m->user_lookup_fds[0] < 0) {
767✔
1140

1141
                /* Free all secondary fields */
1142
                safe_close_pair(m->user_lookup_fds);
716✔
1143
                m->user_lookup_event_source = sd_event_source_disable_unref(m->user_lookup_event_source);
716✔
1144

1145
                if (socketpair(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC, 0, m->user_lookup_fds) < 0)
716✔
1146
                        return log_error_errno(errno, "Failed to allocate user lookup socket: %m");
×
1147

1148
                r = setsockopt_int(m->user_lookup_fds[0], SOL_SOCKET, SO_PASSRIGHTS, false);
716✔
1149
                if (r < 0 && !ERRNO_IS_NEG_NOT_SUPPORTED(r))
716✔
1150
                        log_warning_errno(r, "Failed to turn off SO_PASSRIGHTS on user lookup socket, ignoring: %m");
×
1151

1152
                (void) fd_increase_rxbuf(m->user_lookup_fds[0], MANAGER_SOCKET_RCVBUF_SIZE);
716✔
1153
        }
1154

1155
        if (!m->user_lookup_event_source) {
767✔
1156
                r = sd_event_add_io(m->event, &m->user_lookup_event_source, m->user_lookup_fds[0], EPOLLIN, manager_dispatch_user_lookup_fd, m);
767✔
1157
                if (r < 0)
767✔
1158
                        return log_error_errno(r, "Failed to allocate user lookup event source: %m");
×
1159

1160
                /* Process even earlier than the notify event source, so that we always know first about valid UID/GID
1161
                 * resolutions */
1162
                r = sd_event_source_set_priority(m->user_lookup_event_source, EVENT_PRIORITY_USER_LOOKUP);
767✔
1163
                if (r < 0)
767✔
1164
                        return log_error_errno(r, "Failed to set priority of user lookup event source: %m");
×
1165

1166
                (void) sd_event_source_set_description(m->user_lookup_event_source, "user-lookup");
767✔
1167
        }
1168

1169
        return 0;
1170
}
1171

1172
static int manager_setup_handoff_timestamp_fd(Manager *m) {
767✔
1173
        int r;
767✔
1174

1175
        assert(m);
767✔
1176

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

1180
        if (m->handoff_timestamp_fds[0] < 0) {
767✔
1181
                m->handoff_timestamp_event_source = sd_event_source_disable_unref(m->handoff_timestamp_event_source);
716✔
1182
                safe_close_pair(m->handoff_timestamp_fds);
716✔
1183

1184
                if (socketpair(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC, 0, m->handoff_timestamp_fds) < 0)
716✔
1185
                        return log_error_errno(errno, "Failed to allocate handoff timestamp socket: %m");
×
1186

1187
                /* Make sure children never have to block */
1188
                (void) fd_increase_rxbuf(m->handoff_timestamp_fds[0], MANAGER_SOCKET_RCVBUF_SIZE);
716✔
1189

1190
                r = setsockopt_int(m->handoff_timestamp_fds[0], SOL_SOCKET, SO_PASSCRED, true);
716✔
1191
                if (r < 0)
716✔
1192
                        return log_error_errno(r, "Failed to enable SO_PASSCRED on handoff timestamp socket: %m");
×
1193

1194
                r = setsockopt_int(m->handoff_timestamp_fds[0], SOL_SOCKET, SO_PASSRIGHTS, false);
716✔
1195
                if (r < 0 && !ERRNO_IS_NEG_NOT_SUPPORTED(r))
716✔
1196
                        log_warning_errno(r, "Failed to turn off SO_PASSRIGHTS on handoff timestamp socket, ignoring: %m");
×
1197

1198
                /* Mark the receiving socket as O_NONBLOCK (but leave sending side as-is) */
1199
                r = fd_nonblock(m->handoff_timestamp_fds[0], true);
716✔
1200
                if (r < 0)
716✔
1201
                        return log_error_errno(r, "Failed to make handoff timestamp socket O_NONBLOCK: %m");
×
1202
        }
1203

1204
        if (!m->handoff_timestamp_event_source) {
767✔
1205
                r = sd_event_add_io(m->event, &m->handoff_timestamp_event_source, m->handoff_timestamp_fds[0], EPOLLIN, manager_dispatch_handoff_timestamp_fd, m);
767✔
1206
                if (r < 0)
767✔
1207
                        return log_error_errno(r, "Failed to allocate handoff timestamp event source: %m");
×
1208

1209
                r = sd_event_source_set_priority(m->handoff_timestamp_event_source, EVENT_PRIORITY_HANDOFF_TIMESTAMP);
767✔
1210
                if (r < 0)
767✔
1211
                        return log_error_errno(r, "Failed to set priority of handoff timestamp event source: %m");
×
1212

1213
                (void) sd_event_source_set_description(m->handoff_timestamp_event_source, "handoff-timestamp");
767✔
1214
        }
1215

1216
        return 0;
1217
}
1218

1219
static int manager_setup_pidref_transport_fd(Manager *m) {
767✔
1220
        int r;
767✔
1221

1222
        assert(m);
767✔
1223

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

1227
        if (m->pidref_transport_fds[0] < 0) {
767✔
1228
                m->pidref_event_source = sd_event_source_disable_unref(m->pidref_event_source);
736✔
1229
                safe_close_pair(m->pidref_transport_fds);
736✔
1230

1231
                if (socketpair(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC, 0, m->pidref_transport_fds) < 0)
736✔
1232
                        return log_error_errno(errno, "Failed to allocate pidref socket: %m");
×
1233

1234
                /* Make sure children never have to block */
1235
                (void) fd_increase_rxbuf(m->pidref_transport_fds[0], MANAGER_SOCKET_RCVBUF_SIZE);
736✔
1236

1237
                r = setsockopt_int(m->pidref_transport_fds[0], SOL_SOCKET, SO_PASSCRED, true);
736✔
1238
                if (r < 0)
736✔
1239
                        return log_error_errno(r, "Failed to enable SO_PASSCRED for pidref socket: %m");
×
1240

1241
                r = setsockopt_int(m->pidref_transport_fds[0], SOL_SOCKET, SO_PASSPIDFD, true);
736✔
1242
                if (ERRNO_IS_NEG_NOT_SUPPORTED(r))
736✔
1243
                        log_debug_errno(r, "SO_PASSPIDFD is not supported for pidref socket, ignoring.");
×
1244
                else if (r < 0)
736✔
1245
                        log_warning_errno(r, "Failed to enable SO_PASSPIDFD for pidref socket, ignoring: %m");
×
1246

1247
                /* Mark the receiving socket as O_NONBLOCK (but leave sending side as-is) */
1248
                r = fd_nonblock(m->pidref_transport_fds[0], true);
736✔
1249
                if (r < 0)
736✔
1250
                        return log_error_errno(r, "Failed to make pidref socket O_NONBLOCK: %m");
×
1251
        }
1252

1253
        if (!m->pidref_event_source) {
767✔
1254
                r = sd_event_add_io(m->event, &m->pidref_event_source, m->pidref_transport_fds[0], EPOLLIN, manager_dispatch_pidref_transport_fd, m);
736✔
1255
                if (r < 0)
736✔
1256
                        return log_error_errno(r, "Failed to allocate pidref event source: %m");
×
1257

1258
                r = sd_event_source_set_priority(m->pidref_event_source, EVENT_PRIORITY_PIDREF);
736✔
1259
                if (r < 0)
736✔
1260
                        return log_error_errno(r, "Failed to set priority of pidref event source: %m");
×
1261

1262
                (void) sd_event_source_set_description(m->pidref_event_source, "pidref");
736✔
1263
        }
1264

1265
        return 0;
1266
}
1267

1268
static unsigned manager_dispatch_cleanup_queue(Manager *m) {
219,473✔
1269
        Unit *u;
219,473✔
1270
        unsigned n = 0;
219,473✔
1271

1272
        assert(m);
219,473✔
1273

1274
        while ((u = m->cleanup_queue)) {
258,874✔
1275
                assert(u->in_cleanup_queue);
39,401✔
1276

1277
                unit_free(u);
39,401✔
1278
                n++;
39,401✔
1279
        }
1280

1281
        return n;
219,473✔
1282
}
1283

1284
static unsigned manager_dispatch_release_resources_queue(Manager *m) {
204,154✔
1285
        unsigned n = 0;
204,154✔
1286
        Unit *u;
204,154✔
1287

1288
        assert(m);
204,154✔
1289

1290
        while ((u = LIST_POP(release_resources_queue, m->release_resources_queue))) {
205,513✔
1291
                assert(u->in_release_resources_queue);
1,359✔
1292
                u->in_release_resources_queue = false;
1,359✔
1293

1294
                n++;
1,359✔
1295

1296
                unit_release_resources(u);
1,359✔
1297
        }
1298

1299
        return n;
204,154✔
1300
}
1301

1302
enum {
1303
        GC_OFFSET_IN_PATH,  /* This one is on the path we were traveling */
1304
        GC_OFFSET_UNSURE,   /* No clue */
1305
        GC_OFFSET_GOOD,     /* We still need this unit */
1306
        GC_OFFSET_BAD,      /* We don't need this unit anymore */
1307
        _GC_OFFSET_MAX
1308
};
1309

1310
static void unit_gc_mark_good(Unit *u, unsigned gc_marker) {
76,619✔
1311
        Unit *other;
76,619✔
1312

1313
        u->gc_marker = gc_marker + GC_OFFSET_GOOD;
76,619✔
1314

1315
        /* Recursively mark referenced units as GOOD as well */
1316
        UNIT_FOREACH_DEPENDENCY(other, u, UNIT_ATOM_REFERENCES)
551,877✔
1317
                if (other->gc_marker == gc_marker + GC_OFFSET_UNSURE)
308,105✔
1318
                        unit_gc_mark_good(other, gc_marker);
1,130✔
1319
}
76,619✔
1320

1321
static void unit_gc_sweep(Unit *u, unsigned gc_marker) {
132,350✔
1322
        Unit *other;
132,350✔
1323
        bool is_bad;
132,350✔
1324

1325
        assert(u);
132,350✔
1326

1327
        if (IN_SET(u->gc_marker - gc_marker,
132,350✔
1328
                   GC_OFFSET_GOOD, GC_OFFSET_BAD, GC_OFFSET_UNSURE, GC_OFFSET_IN_PATH))
1329
                return;
56,861✔
1330

1331
        if (u->in_cleanup_queue)
116,019✔
1332
                goto bad;
×
1333

1334
        if (!unit_may_gc(u))
116,019✔
1335
                goto good;
53,664✔
1336

1337
        u->gc_marker = gc_marker + GC_OFFSET_IN_PATH;
62,355✔
1338

1339
        is_bad = true;
62,355✔
1340

1341
        UNIT_FOREACH_DEPENDENCY(other, u, UNIT_ATOM_REFERENCED_BY) {
131,149✔
1342
                unit_gc_sweep(other, gc_marker);
25,828✔
1343

1344
                if (other->gc_marker == gc_marker + GC_OFFSET_GOOD)
25,828✔
1345
                        goto good;
21,825✔
1346

1347
                if (other->gc_marker != gc_marker + GC_OFFSET_BAD)
4,003✔
1348
                        is_bad = false;
3,326✔
1349
        }
1350

1351
        LIST_FOREACH(refs_by_target, ref, u->refs_by_target) {
40,742✔
1352
                unit_gc_sweep(ref->source, gc_marker);
212✔
1353

1354
                if (ref->source->gc_marker == gc_marker + GC_OFFSET_GOOD)
212✔
1355
                        goto good;
×
1356

1357
                if (ref->source->gc_marker != gc_marker + GC_OFFSET_BAD)
212✔
1358
                        is_bad = false;
212✔
1359
        }
1360

1361
        if (is_bad)
40,530✔
1362
                goto bad;
39,024✔
1363

1364
        /* We were unable to find anything out about this entry, so
1365
         * let's investigate it later */
1366
        u->gc_marker = gc_marker + GC_OFFSET_UNSURE;
1,506✔
1367
        unit_add_to_gc_queue(u);
1,506✔
1368
        return;
1369

1370
bad:
39,024✔
1371
        /* We definitely know that this one is not useful anymore, so
1372
         * let's mark it for deletion */
1373
        u->gc_marker = gc_marker + GC_OFFSET_BAD;
39,024✔
1374
        unit_add_to_cleanup_queue(u);
39,024✔
1375
        return;
1376

1377
good:
75,489✔
1378
        unit_gc_mark_good(u, gc_marker);
75,489✔
1379
}
1380

1381
static unsigned manager_dispatch_gc_unit_queue(Manager *m) {
236,607✔
1382
        unsigned n = 0, gc_marker;
236,607✔
1383

1384
        assert(m);
236,607✔
1385

1386
        /* log_debug("Running GC..."); */
1387

1388
        m->gc_marker += _GC_OFFSET_MAX;
236,607✔
1389
        if (m->gc_marker + _GC_OFFSET_MAX <= _GC_OFFSET_MAX)
236,607✔
1390
                m->gc_marker = 1;
×
1391

1392
        gc_marker = m->gc_marker;
236,607✔
1393

1394
        Unit *u;
236,607✔
1395
        while ((u = m->gc_unit_queue)) {
342,917✔
1396
                assert(u->in_gc_queue);
106,310✔
1397

1398
                unit_gc_sweep(u, gc_marker);
106,310✔
1399

1400
                LIST_REMOVE(gc_queue, m->gc_unit_queue, u);
106,310✔
1401
                u->in_gc_queue = false;
106,310✔
1402

1403
                n++;
106,310✔
1404

1405
                if (IN_SET(u->gc_marker - gc_marker,
106,310✔
1406
                           GC_OFFSET_BAD, GC_OFFSET_UNSURE)) {
1407
                        if (u->id)
39,400✔
1408
                                log_unit_debug(u, "Collecting.");
39,400✔
1409
                        u->gc_marker = gc_marker + GC_OFFSET_BAD;
39,400✔
1410
                        unit_add_to_cleanup_queue(u);
39,400✔
1411
                }
1412
        }
1413

1414
        return n;
236,607✔
1415
}
1416

1417
static unsigned manager_dispatch_gc_job_queue(Manager *m) {
236,616✔
1418
        unsigned n = 0;
236,616✔
1419
        Job *j;
236,616✔
1420

1421
        assert(m);
236,616✔
1422

1423
        while ((j = LIST_POP(gc_queue, m->gc_job_queue))) {
236,625✔
1424
                assert(j->in_gc_queue);
9✔
1425
                j->in_gc_queue = false;
9✔
1426

1427
                n++;
9✔
1428

1429
                if (!job_may_gc(j))
9✔
1430
                        continue;
9✔
1431

1432
                log_unit_debug(j->unit, "Collecting job.");
×
1433
                (void) job_finish_and_invalidate(j, JOB_COLLECTED, false, false);
×
1434
        }
1435

1436
        return n;
236,616✔
1437
}
1438

1439
static int manager_ratelimit_requeue(sd_event_source *s, uint64_t usec, void *userdata) {
×
1440
        Unit *u = userdata;
×
1441

1442
        assert(u);
×
1443
        assert(s == u->auto_start_stop_event_source);
×
1444

1445
        u->auto_start_stop_event_source = sd_event_source_unref(u->auto_start_stop_event_source);
×
1446

1447
        /* Re-queue to all queues, if the rate limit hit we might have been throttled on any of them. */
1448
        unit_submit_to_stop_when_unneeded_queue(u);
×
1449
        unit_submit_to_start_when_upheld_queue(u);
×
1450
        unit_submit_to_stop_when_bound_queue(u);
×
1451

1452
        return 0;
×
1453
}
1454

1455
static int manager_ratelimit_check_and_queue(Unit *u) {
15✔
1456
        int r;
15✔
1457

1458
        assert(u);
15✔
1459

1460
        if (ratelimit_below(&u->auto_start_stop_ratelimit))
15✔
1461
                return 1;
1462

1463
        /* Already queued, no need to requeue */
1464
        if (u->auto_start_stop_event_source)
×
1465
                return 0;
1466

1467
        r = sd_event_add_time(
×
1468
                        u->manager->event,
×
1469
                        &u->auto_start_stop_event_source,
1470
                        CLOCK_MONOTONIC,
1471
                        ratelimit_end(&u->auto_start_stop_ratelimit),
×
1472
                        0,
1473
                        manager_ratelimit_requeue,
1474
                        u);
1475
        if (r < 0)
×
1476
                return log_unit_error_errno(u, r, "Failed to queue timer on event loop: %m");
×
1477

1478
        return 0;
1479
}
1480

1481
static unsigned manager_dispatch_stop_when_unneeded_queue(Manager *m) {
204,409✔
1482
        unsigned n = 0;
204,409✔
1483
        Unit *u;
204,409✔
1484
        int r;
204,409✔
1485

1486
        assert(m);
204,409✔
1487

1488
        while ((u = LIST_POP(stop_when_unneeded_queue, m->stop_when_unneeded_queue))) {
204,755✔
1489
                _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
346✔
1490

1491
                assert(u->in_stop_when_unneeded_queue);
346✔
1492
                u->in_stop_when_unneeded_queue = false;
346✔
1493

1494
                n++;
346✔
1495

1496
                if (!unit_is_unneeded(u))
346✔
1497
                        continue;
334✔
1498

1499
                log_unit_debug(u, "Unit is not needed anymore.");
12✔
1500

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

1504
                r = manager_ratelimit_check_and_queue(u);
12✔
1505
                if (r <= 0) {
12✔
1506
                        log_unit_warning(u,
×
1507
                                         "Unit not needed anymore, but not stopping since we tried this too often recently.%s",
1508
                                         r == 0 ? " Will retry later." : "");
1509
                        continue;
×
1510
                }
1511

1512
                /* Ok, nobody needs us anymore. Sniff. Then let's commit suicide */
1513
                r = manager_add_job(u->manager, JOB_STOP, u, JOB_FAIL, &error, /* ret= */ NULL);
12✔
1514
                if (r < 0)
12✔
1515
                        log_unit_warning_errno(u, r, "Failed to enqueue stop job, ignoring: %s", bus_error_message(&error, r));
×
1516
        }
1517

1518
        return n;
204,409✔
1519
}
1520

1521
static unsigned manager_dispatch_start_when_upheld_queue(Manager *m) {
204,561✔
1522
        unsigned n = 0;
204,561✔
1523
        Unit *u;
204,561✔
1524
        int r;
204,561✔
1525

1526
        assert(m);
204,561✔
1527

1528
        while ((u = LIST_POP(start_when_upheld_queue, m->start_when_upheld_queue))) {
204,561✔
1529
                _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
×
1530
                Unit *culprit = NULL;
×
1531

1532
                assert(u->in_start_when_upheld_queue);
×
1533
                u->in_start_when_upheld_queue = false;
×
1534

1535
                n++;
×
1536

1537
                if (!unit_is_upheld_by_active(u, &culprit))
×
1538
                        continue;
×
1539

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

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

1545
                r = manager_ratelimit_check_and_queue(u);
×
1546
                if (r <= 0) {
×
1547
                        log_unit_warning(u,
×
1548
                                         "Unit needs to be started because active unit %s upholds it, but not starting since we tried this too often recently.%s",
1549
                                         culprit->id,
1550
                                         r == 0 ? " Will retry later." : "");
1551
                        continue;
×
1552
                }
1553

1554
                r = manager_add_job(u->manager, JOB_START, u, JOB_FAIL, &error, /* ret= */ NULL);
×
1555
                if (r < 0)
×
1556
                        log_unit_warning_errno(u, r, "Failed to enqueue start job, ignoring: %s", bus_error_message(&error, r));
×
1557
        }
1558

1559
        return n;
204,561✔
1560
}
1561

1562
static unsigned manager_dispatch_stop_when_bound_queue(Manager *m) {
204,561✔
1563
        unsigned n = 0;
204,561✔
1564
        Unit *u;
204,561✔
1565
        int r;
204,561✔
1566

1567
        assert(m);
204,561✔
1568

1569
        while ((u = LIST_POP(stop_when_bound_queue, m->stop_when_bound_queue))) {
204,823✔
1570
                _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
262✔
1571
                Unit *culprit = NULL;
262✔
1572

1573
                assert(u->in_stop_when_bound_queue);
262✔
1574
                u->in_stop_when_bound_queue = false;
262✔
1575

1576
                n++;
262✔
1577

1578
                if (!unit_is_bound_by_inactive(u, &culprit))
262✔
1579
                        continue;
259✔
1580

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

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

1586
                r = manager_ratelimit_check_and_queue(u);
3✔
1587
                if (r <= 0) {
3✔
1588
                        log_unit_warning(u,
×
1589
                                         "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",
1590
                                         culprit->id,
1591
                                         r == 0 ? " Will retry later." : "");
1592
                        continue;
×
1593
                }
1594

1595
                r = manager_add_job(u->manager, JOB_STOP, u, JOB_REPLACE, &error, /* ret= */ NULL);
3✔
1596
                if (r < 0)
3✔
1597
                        log_unit_warning_errno(u, r, "Failed to enqueue stop job, ignoring: %s", bus_error_message(&error, r));
×
1598
        }
1599

1600
        return n;
204,561✔
1601
}
1602

1603
static unsigned manager_dispatch_stop_notify_queue(Manager *m) {
202,974✔
1604
        unsigned n = 0;
202,974✔
1605

1606
        assert(m);
202,974✔
1607

1608
        if (m->may_dispatch_stop_notify_queue < 0)
202,974✔
1609
                m->may_dispatch_stop_notify_queue = hashmap_isempty(m->jobs);
3,270✔
1610

1611
        if (!m->may_dispatch_stop_notify_queue)
202,974✔
1612
                return 0;
1613

1614
        m->may_dispatch_stop_notify_queue = false;
7,846✔
1615

1616
        LIST_FOREACH(stop_notify_queue, u, m->stop_notify_queue) {
7,846✔
1617
                assert(u->in_stop_notify_queue);
×
1618

1619
                assert(UNIT_VTABLE(u)->stop_notify);
×
1620
                if (UNIT_VTABLE(u)->stop_notify(u)) {
×
1621
                        assert(!u->in_stop_notify_queue);
×
1622
                        n++;
×
1623
                }
1624
        }
1625

1626
        return n;
1627
}
1628

1629
static void manager_clear_jobs_and_units(Manager *m) {
769✔
1630
        Unit *u;
769✔
1631

1632
        assert(m);
769✔
1633

1634
        while ((u = hashmap_first(m->units)))
52,220✔
1635
                unit_free(u);
51,451✔
1636

1637
        manager_dispatch_cleanup_queue(m);
769✔
1638

1639
        assert(!m->load_queue);
769✔
1640
        assert(prioq_isempty(m->run_queue));
769✔
1641
        assert(!m->dbus_unit_queue);
769✔
1642
        assert(!m->dbus_job_queue);
769✔
1643
        assert(!m->cleanup_queue);
769✔
1644
        assert(!m->gc_unit_queue);
769✔
1645
        assert(!m->gc_job_queue);
769✔
1646
        assert(!m->cgroup_realize_queue);
769✔
1647
        assert(!m->cgroup_empty_queue);
769✔
1648
        assert(!m->cgroup_oom_queue);
769✔
1649
        assert(!m->target_deps_queue);
769✔
1650
        assert(!m->stop_when_unneeded_queue);
769✔
1651
        assert(!m->start_when_upheld_queue);
769✔
1652
        assert(!m->stop_when_bound_queue);
769✔
1653
        assert(!m->release_resources_queue);
769✔
1654

1655
        assert(hashmap_isempty(m->jobs));
769✔
1656
        assert(hashmap_isempty(m->units));
769✔
1657
        assert(hashmap_isempty(m->units_by_invocation_id));
769✔
1658

1659
        m->n_on_console = 0;
769✔
1660
        m->n_running_jobs = 0;
769✔
1661
        m->n_installed_jobs = 0;
769✔
1662
        m->n_failed_jobs = 0;
769✔
1663

1664
        m->transactions_with_cycle = set_free(m->transactions_with_cycle);
769✔
1665
}
769✔
1666

1667
Manager* manager_free(Manager *m) {
738✔
1668
        if (!m)
738✔
1669
                return NULL;
1670

1671
        manager_clear_jobs_and_units(m);
738✔
1672

1673
        for (UnitType c = 0; c < _UNIT_TYPE_MAX; c++)
8,856✔
1674
                if (unit_vtable[c]->shutdown)
8,118✔
1675
                        unit_vtable[c]->shutdown(m);
2,952✔
1676

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

1680
        lookup_paths_flush_generator(&m->lookup_paths);
738✔
1681

1682
        bus_done(m);
738✔
1683
        manager_varlink_done(m);
738✔
1684

1685
        exec_shared_runtime_vacuum(m);
738✔
1686
        hashmap_free(m->exec_shared_runtime_by_id);
738✔
1687

1688
        dynamic_user_vacuum(m, false);
738✔
1689
        hashmap_free(m->dynamic_users);
738✔
1690

1691
        hashmap_free(m->units);
738✔
1692
        hashmap_free(m->units_by_invocation_id);
738✔
1693
        hashmap_free(m->jobs);
738✔
1694
        hashmap_free(m->watch_pids);
738✔
1695
        hashmap_free(m->watch_pids_more);
738✔
1696
        hashmap_free(m->watch_bus);
738✔
1697

1698
        prioq_free(m->run_queue);
738✔
1699

1700
        set_free(m->startup_units);
738✔
1701
        set_free(m->failed_units);
738✔
1702

1703
        sd_event_source_unref(m->signal_event_source);
738✔
1704
        sd_event_source_unref(m->sigchld_event_source);
738✔
1705
        sd_event_source_unref(m->notify_event_source);
738✔
1706
        sd_event_source_unref(m->time_change_event_source);
738✔
1707
        sd_event_source_unref(m->timezone_change_event_source);
738✔
1708
        sd_event_source_unref(m->jobs_in_progress_event_source);
738✔
1709
        sd_event_source_unref(m->run_queue_event_source);
738✔
1710
        sd_event_source_unref(m->user_lookup_event_source);
738✔
1711
        sd_event_source_unref(m->handoff_timestamp_event_source);
738✔
1712
        sd_event_source_unref(m->pidref_event_source);
738✔
1713
        sd_event_source_unref(m->memory_pressure_event_source);
738✔
1714

1715
        safe_close(m->signal_fd);
738✔
1716
        safe_close(m->notify_fd);
738✔
1717
        safe_close_pair(m->user_lookup_fds);
738✔
1718
        safe_close_pair(m->handoff_timestamp_fds);
738✔
1719
        safe_close_pair(m->pidref_transport_fds);
738✔
1720

1721
        manager_close_ask_password(m);
738✔
1722

1723
        manager_close_idle_pipe(m);
738✔
1724

1725
        sd_event_unref(m->event);
738✔
1726

1727
        free(m->notify_socket);
738✔
1728

1729
        lookup_paths_done(&m->lookup_paths);
738✔
1730
        strv_free(m->transient_environment);
738✔
1731
        strv_free(m->client_environment);
738✔
1732

1733
        hashmap_free(m->cgroup_unit);
738✔
1734
        manager_free_unit_name_maps(m);
738✔
1735

1736
        free(m->switch_root);
738✔
1737
        free(m->switch_root_init);
738✔
1738

1739
        sd_bus_track_unref(m->subscribed);
738✔
1740
        strv_free(m->subscribed_as_strv);
738✔
1741

1742
        unit_defaults_done(&m->defaults);
738✔
1743

1744
        FOREACH_ARRAY(map, m->units_needing_mounts_for, _UNIT_MOUNT_DEPENDENCY_TYPE_MAX) {
2,214✔
1745
                assert(hashmap_isempty(*map));
1,476✔
1746
                hashmap_free(*map);
1,476✔
1747
        }
1748

1749
        hashmap_free(m->uid_refs);
738✔
1750
        hashmap_free(m->gid_refs);
738✔
1751

1752
        FOREACH_ARRAY(i, m->prefix, _EXEC_DIRECTORY_TYPE_MAX)
4,428✔
1753
                free(*i);
3,690✔
1754

1755
        free(m->received_credentials_directory);
738✔
1756
        free(m->received_encrypted_credentials_directory);
738✔
1757

1758
        free(m->watchdog_pretimeout_governor);
738✔
1759
        free(m->watchdog_pretimeout_governor_overridden);
738✔
1760

1761
        sd_netlink_unref(m->nfnl);
738✔
1762

1763
#if BPF_FRAMEWORK
1764
        bpf_restrict_fs_destroy(m->restrict_fs);
738✔
1765
#endif
1766

1767
        safe_close(m->executor_fd);
738✔
1768
        free(m->executor_path);
738✔
1769

1770
        return mfree(m);
738✔
1771
}
1772

1773
static void manager_enumerate_perpetual(Manager *m) {
767✔
1774
        assert(m);
767✔
1775

1776
        if (FLAGS_SET(m->test_run_flags, MANAGER_TEST_RUN_MINIMAL))
767✔
1777
                return;
1778

1779
        /* Let's ask every type to load all units from disk/kernel that it might know */
1780
        for (UnitType c = 0; c < _UNIT_TYPE_MAX; c++) {
3,420✔
1781
                if (!unit_type_supported(c)) {
3,135✔
1782
                        log_debug("Unit type .%s is not supported on this system.", unit_type_to_string(c));
477✔
1783
                        continue;
477✔
1784
                }
1785

1786
                if (unit_vtable[c]->enumerate_perpetual)
2,658✔
1787
                        unit_vtable[c]->enumerate_perpetual(m);
855✔
1788
        }
1789
}
1790

1791
static void manager_enumerate(Manager *m) {
767✔
1792
        assert(m);
767✔
1793

1794
        if (FLAGS_SET(m->test_run_flags, MANAGER_TEST_RUN_MINIMAL))
767✔
1795
                return;
1796

1797
        /* Let's ask every type to load all units from disk/kernel that it might know */
1798
        for (UnitType c = 0; c < _UNIT_TYPE_MAX; c++) {
3,420✔
1799
                if (!unit_type_supported(c)) {
3,135✔
1800
                        log_debug("Unit type .%s is not supported on this system.", unit_type_to_string(c));
477✔
1801
                        continue;
477✔
1802
                }
1803

1804
                if (unit_vtable[c]->enumerate)
2,658✔
1805
                        unit_vtable[c]->enumerate(m);
537✔
1806
        }
1807

1808
        manager_dispatch_load_queue(m);
285✔
1809
}
1810

1811
static void manager_coldplug(Manager *m) {
767✔
1812
        Unit *u;
767✔
1813
        char *k;
767✔
1814
        int r;
767✔
1815

1816
        assert(m);
767✔
1817

1818
        log_debug("Invoking unit coldplug() handlers%s", glyph(GLYPH_ELLIPSIS));
1,111✔
1819

1820
        /* Let's place the units back into their deserialized state */
1821
        HASHMAP_FOREACH_KEY(u, k, m->units) {
45,245✔
1822

1823
                /* ignore aliases */
1824
                if (u->id != k)
43,711✔
1825
                        continue;
767✔
1826

1827
                r = unit_coldplug(u);
42,944✔
1828
                if (r < 0)
42,944✔
1829
                        log_warning_errno(r, "We couldn't coldplug %s, proceeding anyway: %m", u->id);
44,478✔
1830
        }
1831
}
767✔
1832

1833
static void manager_catchup(Manager *m) {
767✔
1834
        Unit *u;
767✔
1835
        char *k;
767✔
1836

1837
        assert(m);
767✔
1838

1839
        log_debug("Invoking unit catchup() handlers%s", glyph(GLYPH_ELLIPSIS));
1,111✔
1840

1841
        /* Let's catch up on any state changes that happened while we were reloading/reexecing */
1842
        HASHMAP_FOREACH_KEY(u, k, m->units) {
44,478✔
1843

1844
                /* ignore aliases */
1845
                if (u->id != k)
43,711✔
1846
                        continue;
767✔
1847

1848
                unit_catchup(u);
42,944✔
1849
        }
1850
}
767✔
1851

1852
static void manager_distribute_fds(Manager *m, FDSet *fds) {
736✔
1853
        Unit *u;
736✔
1854

1855
        assert(m);
736✔
1856

1857
        HASHMAP_FOREACH(u, m->units) {
8,183✔
1858

1859
                if (fdset_isempty(fds))
7,681✔
1860
                        break;
1861

1862
                if (!UNIT_VTABLE(u)->distribute_fds)
7,447✔
1863
                        continue;
6,635✔
1864

1865
                UNIT_VTABLE(u)->distribute_fds(u, fds);
812✔
1866
        }
1867
}
736✔
1868

1869
static bool manager_dbus_is_running(Manager *m, bool deserialized) {
74,524✔
1870
        Unit *u;
74,524✔
1871

1872
        assert(m);
74,524✔
1873

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

1878
        if (MANAGER_IS_TEST_RUN(m))
74,524✔
1879
                return false;
1880

1881
        u = manager_get_unit(m, SPECIAL_DBUS_SOCKET);
73,677✔
1882
        if (!u)
73,677✔
1883
                return false;
1884
        if ((deserialized ? SOCKET(u)->deserialized_state : SOCKET(u)->state) != SOCKET_RUNNING)
123,516✔
1885
                return false;
1886

1887
        u = manager_get_unit(m, SPECIAL_DBUS_SERVICE);
7,480✔
1888
        if (!u)
7,480✔
1889
                return false;
1890
        if (!IN_SET((deserialized ? SERVICE(u)->deserialized_state : SERVICE(u)->state),
14,960✔
1891
                    SERVICE_RUNNING,
1892
                    SERVICE_MOUNTING,
1893
                    SERVICE_RELOAD,
1894
                    SERVICE_RELOAD_NOTIFY,
1895
                    SERVICE_REFRESH_EXTENSIONS,
1896
                    SERVICE_RELOAD_SIGNAL))
1897
                return false;
327✔
1898

1899
        return true;
1900
}
1901

1902
static void manager_setup_bus(Manager *m) {
736✔
1903
        assert(m);
736✔
1904

1905
        if (MANAGER_IS_TEST_RUN(m))
736✔
1906
                return;
1907

1908
        /* Let's set up our private bus connection now, unconditionally */
1909
        (void) bus_init_private(m);
243✔
1910

1911
        /* If we are in --user mode also connect to the system bus now */
1912
        if (MANAGER_IS_USER(m))
243✔
1913
                (void) bus_init_system(m);
188✔
1914

1915
        /* Let's connect to the bus now, but only if the unit is supposed to be up */
1916
        if (manager_dbus_is_running(m, MANAGER_IS_RELOADING(m))) {
243✔
1917
                (void) bus_init_api(m);
17✔
1918

1919
                if (MANAGER_IS_SYSTEM(m))
17✔
1920
                        (void) bus_init_system(m);
16✔
1921
        }
1922
}
1923

1924
static void manager_preset_all(Manager *m) {
736✔
1925
        int r;
736✔
1926

1927
        assert(m);
736✔
1928

1929
        if (m->first_boot <= 0)
736✔
1930
                return;
716✔
1931

1932
        if (!MANAGER_IS_SYSTEM(m))
20✔
1933
                return;
1934

1935
        if (MANAGER_IS_TEST_RUN(m))
20✔
1936
                return;
1937

1938
        /* If this is the first boot, and we are in the host system, then preset everything */
1939
        UnitFilePresetMode mode =
20✔
1940
                ENABLE_FIRST_BOOT_FULL_PRESET ? UNIT_FILE_PRESET_FULL : UNIT_FILE_PRESET_ENABLE_ONLY;
1941
        InstallChange *changes = NULL;
20✔
1942
        size_t n_changes = 0;
20✔
1943

1944
        CLEANUP_ARRAY(changes, n_changes, install_changes_free);
×
1945

1946
        log_info("Applying preset policy.");
20✔
1947
        r = unit_file_preset_all(RUNTIME_SCOPE_SYSTEM, /* file_flags= */ 0,
20✔
1948
                                 /* root_dir= */ NULL, mode, &changes, &n_changes);
1949
        install_changes_dump(r, "preset", changes, n_changes, /* quiet= */ false);
20✔
1950
        if (r < 0)
20✔
1951
                log_full_errno(r == -EEXIST ? LOG_NOTICE : LOG_WARNING, r,
20✔
1952
                               "Failed to populate /etc with preset unit settings, ignoring: %m");
1953
        else
1954
                log_info("Populated /etc with preset unit settings.");
20✔
1955
}
1956

1957
static void manager_ready(Manager *m) {
767✔
1958
        assert(m);
767✔
1959

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

1962
        m->objective = MANAGER_OK; /* Tell everyone we are up now */
767✔
1963

1964
        /* It might be safe to log to the journal now and connect to dbus */
1965
        manager_recheck_journal(m);
767✔
1966
        manager_recheck_dbus(m);
767✔
1967

1968
        /* Let's finally catch up with any changes that took place while we were reloading/reexecing */
1969
        manager_catchup(m);
767✔
1970

1971
        /* Create a file which will indicate when the manager started loading units the last time. */
1972
        if (MANAGER_IS_SYSTEM(m))
767✔
1973
                (void) touch_file("/run/systemd/systemd-units-load", false,
561✔
1974
                        m->timestamps[MANAGER_TIMESTAMP_UNITS_LOAD].realtime ?: now(CLOCK_REALTIME),
561✔
1975
                        UID_INVALID, GID_INVALID, 0444);
1976
}
767✔
1977

1978
Manager* manager_reloading_start(Manager *m) {
172✔
1979
        m->n_reloading++;
172✔
1980
        dual_timestamp_now(m->timestamps + MANAGER_TIMESTAMP_UNITS_LOAD);
172✔
1981
        return m;
172✔
1982
}
1983

1984
void manager_reloading_stopp(Manager **m) {
888✔
1985
        if (*m) {
888✔
1986
                assert((*m)->n_reloading > 0);
141✔
1987
                (*m)->n_reloading--;
141✔
1988
        }
1989
}
888✔
1990

1991
static int manager_make_runtime_dir(Manager *m) {
736✔
1992
        int r;
736✔
1993

1994
        assert(m);
736✔
1995

1996
        _cleanup_free_ char *d = path_join(m->prefix[EXEC_DIRECTORY_RUNTIME], "systemd");
1,472✔
1997
        if (!d)
736✔
1998
                return log_oom();
×
1999

2000
        r = mkdir_label(d, 0755);
736✔
2001
        if (r < 0 && r != -EEXIST)
736✔
2002
                return log_error_errno(r, "Failed to create directory '%s/': %m", d);
×
2003

2004
        return 0;
2005
}
2006

2007
int manager_startup(Manager *m, FILE *serialization, FDSet *fds, const char *root) {
736✔
2008
        int r;
736✔
2009

2010
        assert(m);
736✔
2011

2012
        r = manager_make_runtime_dir(m);
736✔
2013
        if (r < 0)
736✔
2014
                return r;
2015

2016
        /* If we are running in test mode, we still want to run the generators,
2017
         * but we should not touch the real generator directories. */
2018
        r = lookup_paths_init_or_warn(&m->lookup_paths, m->runtime_scope,
736✔
2019
                                      MANAGER_IS_TEST_RUN(m) ? LOOKUP_PATHS_TEMPORARY_GENERATED : 0,
736✔
2020
                                      root);
2021
        if (r < 0)
736✔
2022
                return r;
2023

2024
        dual_timestamp_now(m->timestamps + manager_timestamp_initrd_mangle(MANAGER_TIMESTAMP_GENERATORS_START));
736✔
2025
        r = manager_run_environment_generators(m);
736✔
2026
        if (r >= 0)
736✔
2027
                r = manager_run_generators(m);
736✔
2028
        dual_timestamp_now(m->timestamps + manager_timestamp_initrd_mangle(MANAGER_TIMESTAMP_GENERATORS_FINISH));
736✔
2029
        if (r < 0)
736✔
2030
                return r;
2031

2032
        manager_preset_all(m);
736✔
2033

2034
        lookup_paths_log(&m->lookup_paths);
736✔
2035

2036
        {
2037
                /* This block is (optionally) done with the reloading counter bumped */
2038
                _unused_ _cleanup_(manager_reloading_stopp) Manager *reloading = NULL;
736✔
2039

2040
                /* Make sure we don't have a left-over from a previous run */
2041
                if (!serialization)
736✔
2042
                        (void) rm_rf(m->lookup_paths.transient, 0);
716✔
2043

2044
                /* If we will deserialize make sure that during enumeration this is already known, so we increase the
2045
                 * counter here already */
2046
                if (serialization)
20✔
2047
                        reloading = manager_reloading_start(m);
20✔
2048

2049
                /* First, enumerate what we can from all config files */
2050
                dual_timestamp_now(m->timestamps + manager_timestamp_initrd_mangle(MANAGER_TIMESTAMP_UNITS_LOAD_START));
736✔
2051
                manager_enumerate_perpetual(m);
736✔
2052
                manager_enumerate(m);
736✔
2053
                dual_timestamp_now(m->timestamps + manager_timestamp_initrd_mangle(MANAGER_TIMESTAMP_UNITS_LOAD_FINISH));
736✔
2054

2055
                /* Second, deserialize if there is something to deserialize */
2056
                if (serialization) {
736✔
2057
                        r = manager_deserialize(m, serialization, fds);
20✔
2058
                        if (r < 0)
20✔
2059
                                return log_error_errno(r, "Deserialization failed: %m");
×
2060
                }
2061

2062
                if (m->previous_objective >= 0) {
736✔
2063
                        if (IN_SET(m->previous_objective, MANAGER_REEXECUTE, MANAGER_SOFT_REBOOT, MANAGER_SWITCH_ROOT))
20✔
2064
                                log_debug("Launching as effect of a '%s' operation.",
20✔
2065
                                          manager_objective_to_string(m->previous_objective));
2066
                        else
2067
                                log_warning("Got unexpected previous objective '%s', ignoring.",
×
2068
                                            manager_objective_to_string(m->previous_objective));
2069
                }
2070

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

2076
                /* Any fds left? Find some unit which wants them. This is useful to allow container managers to pass
2077
                 * some file descriptors to us pre-initialized. This enables socket-based activation of entire
2078
                 * containers. */
2079
                manager_distribute_fds(m, fds);
736✔
2080

2081
                /* We might have deserialized the notify fd, but if we didn't then let's create it now */
2082
                r = manager_setup_notify(m);
736✔
2083
                if (r < 0)
736✔
2084
                        /* No sense to continue without notifications, our children would fail anyway. */
2085
                        return r;
2086

2087
                r = manager_setup_user_lookup_fd(m);
736✔
2088
                if (r < 0)
736✔
2089
                        /* This shouldn't fail, except if things are really broken. */
2090
                        return r;
2091

2092
                r = manager_setup_handoff_timestamp_fd(m);
736✔
2093
                if (r < 0)
736✔
2094
                        /* This shouldn't fail, except if things are really broken. */
2095
                        return r;
2096

2097
                r = manager_setup_pidref_transport_fd(m);
736✔
2098
                if (r < 0)
736✔
2099
                        /* This shouldn't fail, except if things are really broken. */
2100
                        return r;
2101

2102
                /* Connect to the bus if we are good for it */
2103
                manager_setup_bus(m);
736✔
2104

2105
                r = manager_varlink_init(m);
736✔
2106
                if (r < 0)
736✔
2107
                        log_warning_errno(r, "Failed to set up Varlink, ignoring: %m");
×
2108

2109
                /* Third, fire things up! */
2110
                manager_coldplug(m);
736✔
2111

2112
                /* Clean up runtime objects */
2113
                manager_vacuum(m);
736✔
2114

2115
                if (serialization)
736✔
2116
                        /* Let's wait for the UnitNew/JobNew messages being sent, before we notify that the
2117
                         * reload is finished */
2118
                        m->send_reloading_done = true;
20✔
2119
        }
2120

2121
        manager_ready(m);
736✔
2122

2123
        manager_set_switching_root(m, false);
736✔
2124

2125
        return 0;
736✔
2126
}
2127

2128
int manager_add_job_full(
1,824✔
2129
                Manager *m,
2130
                JobType type,
2131
                Unit *unit,
2132
                JobMode mode,
2133
                TransactionAddFlags extra_flags,
2134
                Set *affected_jobs,
2135
                sd_bus_error *reterr_error,
2136
                Job **ret) {
2137

2138
        _cleanup_(transaction_abort_and_freep) Transaction *tr = NULL;
1,824✔
2139
        int r;
1,824✔
2140

2141
        assert(m);
1,824✔
2142
        assert(type >= 0 && type < _JOB_TYPE_MAX);
1,824✔
2143
        assert(unit);
1,824✔
2144
        assert(mode >= 0 && mode < _JOB_MODE_MAX);
1,824✔
2145
        assert((extra_flags & ~_TRANSACTION_FLAGS_MASK_PUBLIC) == 0);
1,824✔
2146

2147
        if (mode == JOB_ISOLATE && type != JOB_START)
1,824✔
2148
                return sd_bus_error_set(reterr_error, SD_BUS_ERROR_INVALID_ARGS, "Isolate is only valid for start.");
×
2149

2150
        if (mode == JOB_ISOLATE && !unit->allow_isolate)
1,824✔
2151
                return sd_bus_error_set(reterr_error, BUS_ERROR_NO_ISOLATION, "Operation refused, unit may not be isolated.");
28✔
2152

2153
        if (mode == JOB_TRIGGERING && type != JOB_STOP)
1,796✔
2154
                return sd_bus_error_set(reterr_error, SD_BUS_ERROR_INVALID_ARGS, "--job-mode=triggering is only valid for stop.");
×
2155

2156
        if (mode == JOB_RESTART_DEPENDENCIES && type != JOB_START)
1,796✔
2157
                return sd_bus_error_set(reterr_error, SD_BUS_ERROR_INVALID_ARGS, "--job-mode=restart-dependencies is only valid for start.");
×
2158

2159
        tr = transaction_new(mode == JOB_REPLACE_IRREVERSIBLY, ++m->last_transaction_id);
1,796✔
2160
        if (!tr)
1,796✔
2161
                return -ENOMEM;
2162

2163
        LOG_CONTEXT_PUSHF("TRANSACTION_ID=%" PRIu64, tr->id);
3,592✔
2164

2165
        log_unit_debug(unit, "Trying to enqueue job %s/%s/%s", unit->id, job_type_to_string(type), job_mode_to_string(mode));
1,796✔
2166

2167
        type = job_type_collapse(type, unit);
1,796✔
2168

2169
        r = transaction_add_job_and_dependencies(
5,388✔
2170
                        tr,
2171
                        type,
2172
                        unit,
2173
                        /* by= */ NULL,
2174
                        TRANSACTION_MATTERS |
2175
                        (IN_SET(mode, JOB_IGNORE_DEPENDENCIES, JOB_IGNORE_REQUIREMENTS) ? TRANSACTION_IGNORE_REQUIREMENTS : 0) |
1,796✔
2176
                        (mode == JOB_IGNORE_DEPENDENCIES ? TRANSACTION_IGNORE_ORDER : 0) |
1,796✔
2177
                        (mode == JOB_RESTART_DEPENDENCIES ? TRANSACTION_PROPAGATE_START_AS_RESTART : 0) |
3,587✔
2178
                        extra_flags,
2179
                        reterr_error);
2180
        if (r < 0)
1,796✔
2181
                return r;
2182

2183
        if (mode == JOB_ISOLATE) {
1,783✔
2184
                r = transaction_add_isolate_jobs(tr, m);
209✔
2185
                if (r < 0)
209✔
2186
                        return r;
2187
        }
2188

2189
        if (mode == JOB_TRIGGERING) {
1,783✔
2190
                r = transaction_add_triggering_jobs(tr, unit);
×
2191
                if (r < 0)
×
2192
                        return r;
2193
        }
2194

2195
        r = transaction_activate(tr, m, mode, affected_jobs, reterr_error);
1,783✔
2196
        if (r < 0)
1,783✔
2197
                return r;
2198

2199
        log_unit_debug(unit,
1,777✔
2200
                       "Enqueued job %s/%s as %u", unit->id,
2201
                       job_type_to_string(type), (unsigned) tr->anchor_job->id);
2202

2203
        if (ret)
1,777✔
2204
                *ret = tr->anchor_job;
1,059✔
2205

2206
        tr = transaction_free(tr);
1,777✔
2207
        return 0;
1,777✔
2208
}
2209

2210
int manager_add_job(
986✔
2211
        Manager *m,
2212
        JobType type,
2213
        Unit *unit,
2214
        JobMode mode,
2215
        sd_bus_error *reterr_error,
2216
        Job **ret) {
2217

2218
        return manager_add_job_full(m, type, unit, mode, 0, NULL, reterr_error, ret);
986✔
2219
}
2220

2221
int manager_add_job_by_name(Manager *m, JobType type, const char *name, JobMode mode, Set *affected_jobs, sd_bus_error *e, Job **ret) {
206✔
2222
        Unit *unit = NULL;  /* just to appease gcc, initialization is not really necessary */
206✔
2223
        int r;
206✔
2224

2225
        assert(m);
206✔
2226
        assert(type < _JOB_TYPE_MAX);
206✔
2227
        assert(name);
206✔
2228
        assert(mode < _JOB_MODE_MAX);
206✔
2229

2230
        r = manager_load_unit(m, name, NULL, NULL, &unit);
206✔
2231
        if (r < 0)
206✔
2232
                return r;
206✔
2233
        assert(unit);
206✔
2234

2235
        return manager_add_job_full(m, type, unit, mode, /* extra_flags= */ 0, affected_jobs, e, ret);
206✔
2236
}
2237

2238
int manager_add_job_by_name_and_warn(Manager *m, JobType type, const char *name, JobMode mode, Set *affected_jobs, Job **ret) {
191✔
2239
        _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
191✔
2240
        int r;
191✔
2241

2242
        assert(m);
191✔
2243
        assert(type < _JOB_TYPE_MAX);
191✔
2244
        assert(name);
191✔
2245
        assert(mode < _JOB_MODE_MAX);
191✔
2246

2247
        r = manager_add_job_by_name(m, type, name, mode, affected_jobs, &error, ret);
191✔
2248
        if (r < 0)
191✔
2249
                return log_warning_errno(r, "Failed to enqueue %s job for %s: %s", job_mode_to_string(mode), name, bus_error_message(&error, r));
×
2250

2251
        return r;
2252
}
2253

2254
int manager_propagate_reload(Manager *m, Unit *unit, JobMode mode, sd_bus_error *e) {
18,665✔
2255
        _cleanup_(transaction_abort_and_freep) Transaction *tr = NULL;
18,665✔
2256
        int r;
18,665✔
2257

2258
        assert(m);
18,665✔
2259
        assert(unit);
18,665✔
2260
        assert(mode < _JOB_MODE_MAX);
18,665✔
2261
        assert(mode != JOB_ISOLATE); /* Isolate is only valid for start */
18,665✔
2262

2263
        tr = transaction_new(mode == JOB_REPLACE_IRREVERSIBLY, ++m->last_transaction_id);
18,665✔
2264
        if (!tr)
18,665✔
2265
                return -ENOMEM;
2266

2267
        LOG_CONTEXT_PUSHF("TRANSACTION_ID=%" PRIu64, tr->id);
37,330✔
2268

2269
        /* We need an anchor job */
2270
        r = transaction_add_job_and_dependencies(tr, JOB_NOP, unit, NULL, TRANSACTION_IGNORE_REQUIREMENTS|TRANSACTION_IGNORE_ORDER, e);
18,665✔
2271
        if (r < 0)
18,665✔
2272
                return r;
2273

2274
        /* Failure in adding individual dependencies is ignored, so this always succeeds. */
2275
        transaction_add_propagate_reload_jobs(
18,665✔
2276
                        tr,
2277
                        unit,
2278
                        tr->anchor_job,
18,665✔
2279
                        mode == JOB_IGNORE_DEPENDENCIES ? TRANSACTION_IGNORE_ORDER : 0);
2280

2281
        /* Only activate the transaction if it contains jobs other than NOP anchor.
2282
         * Short-circuiting here avoids unnecessary processing, such as emitting D-Bus signals. */
2283
        if (hashmap_size(tr->jobs) <= 1)
18,665✔
2284
                return 0;
2285

2286
        r = transaction_activate(tr, m, mode, NULL, e);
×
2287
        if (r < 0)
×
2288
                return r;
2289

2290
        tr = transaction_free(tr);
×
2291
        return 0;
×
2292
}
2293

2294
Job *manager_get_job(Manager *m, uint32_t id) {
18,383✔
2295
        assert(m);
18,383✔
2296

2297
        return hashmap_get(m->jobs, UINT32_TO_PTR(id));
18,383✔
2298
}
2299

2300
Unit *manager_get_unit(Manager *m, const char *name) {
1,990,298✔
2301
        assert(m);
1,990,298✔
2302
        assert(name);
1,990,298✔
2303

2304
        return hashmap_get(m->units, name);
1,990,298✔
2305
}
2306

2307
static int manager_dispatch_target_deps_queue(Manager *m) {
253,342✔
2308
        Unit *u;
253,342✔
2309
        int r = 0;
253,342✔
2310

2311
        assert(m);
253,342✔
2312

2313
        while ((u = LIST_POP(target_deps_queue, m->target_deps_queue))) {
320,049✔
2314
                _cleanup_free_ Unit **targets = NULL;
66,707✔
2315
                int n_targets;
66,707✔
2316

2317
                assert(u->in_target_deps_queue);
66,707✔
2318

2319
                u->in_target_deps_queue = false;
66,707✔
2320

2321
                /* Take an "atomic" snapshot of dependencies here, as the call below will likely modify the
2322
                 * dependencies, and we can't have it that hash tables we iterate through are modified while
2323
                 * we are iterating through them. */
2324
                n_targets = unit_get_dependency_array(u, UNIT_ATOM_DEFAULT_TARGET_DEPENDENCIES, &targets);
66,707✔
2325
                if (n_targets < 0)
66,707✔
2326
                        return n_targets;
2327

2328
                FOREACH_ARRAY(i, targets, n_targets) {
130,226✔
2329
                        r = unit_add_default_target_dependency(u, *i);
63,519✔
2330
                        if (r < 0)
63,519✔
2331
                                return r;
2332
                }
2333
        }
2334

2335
        return r;
2336
}
2337

2338
unsigned manager_dispatch_load_queue(Manager *m) {
284,728✔
2339
        Unit *u;
284,728✔
2340
        unsigned n = 0;
284,728✔
2341

2342
        assert(m);
284,728✔
2343

2344
        /* Make sure we are not run recursively */
2345
        if (m->dispatching_load_queue)
284,728✔
2346
                return 0;
2347

2348
        m->dispatching_load_queue = true;
253,342✔
2349

2350
        /* Dispatches the load queue. Takes a unit from the queue and
2351
         * tries to load its data until the queue is empty */
2352

2353
        while ((u = m->load_queue)) {
344,602✔
2354
                assert(u->in_load_queue);
91,260✔
2355

2356
                unit_load(u);
91,260✔
2357
                n++;
91,260✔
2358
        }
2359

2360
        m->dispatching_load_queue = false;
253,342✔
2361

2362
        /* Dispatch the units waiting for their target dependencies to be added now, as all targets that we know about
2363
         * should be loaded and have aliases resolved */
2364
        (void) manager_dispatch_target_deps_queue(m);
253,342✔
2365

2366
        return n;
253,342✔
2367
}
2368

2369
bool manager_unit_cache_should_retry_load(Unit *u) {
637,895✔
2370
        assert(u);
637,895✔
2371

2372
        /* Automatic reloading from disk only applies to units which were not found sometime in the past, and
2373
         * the not-found stub is kept pinned in the unit graph by dependencies. For units that were
2374
         * previously loaded, we don't do automatic reloading, and daemon-reload is necessary to update. */
2375
        if (u->load_state != UNIT_NOT_FOUND)
637,895✔
2376
                return false;
2377

2378
        /* The cache has been updated since the last time we tried to load the unit. There might be new
2379
         * fragment paths to read. */
2380
        if (u->manager->unit_cache_timestamp_hash != u->fragment_not_found_timestamp_hash)
2,006✔
2381
                return true;
2382

2383
        /* The cache needs to be updated because there are modifications on disk. */
2384
        return !lookup_paths_timestamp_hash_same(&u->manager->lookup_paths, u->manager->unit_cache_timestamp_hash, NULL);
2,006✔
2385
}
2386

2387
int manager_load_unit_prepare(
585,227✔
2388
                Manager *m,
2389
                const char *name,
2390
                const char *path,
2391
                sd_bus_error *e,
2392
                Unit **ret) {
2393

2394
        _cleanup_(unit_freep) Unit *cleanup_unit = NULL;
×
2395
        _cleanup_free_ char *nbuf = NULL;
585,227✔
2396
        int r;
585,227✔
2397

2398
        assert(m);
585,227✔
2399
        assert(ret);
585,227✔
2400
        assert(name || path);
585,227✔
2401

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

2404
        if (path && !path_is_absolute(path))
585,227✔
2405
                return sd_bus_error_setf(e, SD_BUS_ERROR_INVALID_ARGS, "Path %s is not absolute.", path);
×
2406

2407
        if (!name) {
585,227✔
2408
                r = path_extract_filename(path, &nbuf);
469✔
2409
                if (r < 0)
469✔
2410
                        return r;
2411
                if (r == O_DIRECTORY)
469✔
2412
                        return sd_bus_error_setf(e, SD_BUS_ERROR_INVALID_ARGS, "Path '%s' refers to directory, refusing.", path);
×
2413

2414
                name = nbuf;
469✔
2415
        }
2416

2417
        UnitType t = unit_name_to_type(name);
585,227✔
2418

2419
        if (t == _UNIT_TYPE_INVALID || !unit_name_is_valid(name, UNIT_NAME_PLAIN|UNIT_NAME_INSTANCE)) {
585,227✔
2420
                if (unit_name_is_valid(name, UNIT_NAME_TEMPLATE))
×
2421
                        return sd_bus_error_setf(e, SD_BUS_ERROR_INVALID_ARGS, "Unit name %s is missing the instance name.", name);
×
2422

2423
                return sd_bus_error_setf(e, SD_BUS_ERROR_INVALID_ARGS, "Unit name %s is not valid.", name);
×
2424
        }
2425

2426
        Unit *unit = manager_get_unit(m, name);
585,227✔
2427
        if (unit) {
585,227✔
2428
                /* The time-based cache allows new units to be started without daemon-reload,
2429
                 * but if they are already referenced (because of dependencies or ordering)
2430
                 * then we have to force a load of the fragment. As an optimization, check
2431
                 * first if anything in the usual paths was modified since the last time
2432
                 * the cache was loaded. Also check if the last time an attempt to load the
2433
                 * unit was made was before the most recent cache refresh, so that we know
2434
                 * we need to try again — even if the cache is current, it might have been
2435
                 * updated in a different context before we had a chance to retry loading
2436
                 * this particular unit. */
2437
                if (manager_unit_cache_should_retry_load(unit))
527,517✔
2438
                        unit->load_state = UNIT_STUB;
×
2439
                else {
2440
                        *ret = unit;
527,517✔
2441
                        return 0;  /* The unit was already loaded */
527,517✔
2442
                }
2443
        } else {
2444
                unit = cleanup_unit = unit_new(m, unit_vtable[t]->object_size);
57,710✔
2445
                if (!unit)
57,710✔
2446
                        return -ENOMEM;
2447
        }
2448

2449
        if (path) {
57,710✔
2450
                r = free_and_strdup(&unit->fragment_path, path);
469✔
2451
                if (r < 0)
469✔
2452
                        return r;
2453
        }
2454

2455
        r = unit_add_name(unit, name);
57,710✔
2456
        if (r < 0)
57,710✔
2457
                return r;
2458

2459
        unit_add_to_load_queue(unit);
57,710✔
2460
        unit_add_to_dbus_queue(unit);
57,710✔
2461
        unit_add_to_gc_queue(unit);
57,710✔
2462

2463
        *ret = unit;
57,710✔
2464
        TAKE_PTR(cleanup_unit);
57,710✔
2465

2466
        return 1;  /* The unit was added the load queue */
57,710✔
2467
}
2468

2469
int manager_load_unit(
561,606✔
2470
                Manager *m,
2471
                const char *name,
2472
                const char *path,
2473
                sd_bus_error *e,
2474
                Unit **ret) {
2475
        int r;
561,606✔
2476

2477
        assert(m);
561,606✔
2478
        assert(ret);
561,606✔
2479

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

2482
        r = manager_load_unit_prepare(m, name, path, e, ret);
561,606✔
2483
        if (r <= 0)
561,606✔
2484
                return r;
2485

2486
        /* Unit was newly loaded */
2487
        manager_dispatch_load_queue(m);
34,479✔
2488
        *ret = unit_follow_merge(*ret);
34,479✔
2489
        return 0;
34,479✔
2490
}
2491

2492
int manager_load_startable_unit_or_warn(
724✔
2493
                Manager *m,
2494
                const char *name,
2495
                const char *path,
2496
                Unit **ret) {
2497

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

2500
        _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
724✔
2501
        Unit *unit;
724✔
2502
        int r;
724✔
2503

2504
        r = manager_load_unit(m, name, path, &error, &unit);
724✔
2505
        if (r < 0)
724✔
2506
                return log_error_errno(r, "Failed to load %s %s: %s",
×
2507
                                       name ? "unit" : "unit file", name ?: path,
2508
                                       bus_error_message(&error, r));
2509

2510
        r = bus_unit_validate_load_state(unit, &error);
724✔
2511
        if (r < 0)
724✔
2512
                return log_error_errno(r, "%s", bus_error_message(&error, r));
9✔
2513

2514
        *ret = unit;
715✔
2515
        return 0;
715✔
2516
}
2517

2518
void manager_clear_jobs(Manager *m) {
459✔
2519
        Job *j;
459✔
2520

2521
        assert(m);
459✔
2522

2523
        while ((j = hashmap_first(m->jobs)))
480✔
2524
                /* No need to recurse. We're cancelling all jobs. */
2525
                job_finish_and_invalidate(j, JOB_CANCELED, false, false);
21✔
2526
}
459✔
2527

2528
void manager_unwatch_pidref(Manager *m, const PidRef *pid) {
2,567✔
2529
        assert(m);
2,567✔
2530

2531
        for (;;) {
×
2532
                Unit *u;
2,567✔
2533

2534
                u = manager_get_unit_by_pidref_watching(m, pid);
2,567✔
2535
                if (!u)
2,567✔
2536
                        break;
2537

2538
                unit_unwatch_pidref(u, pid);
×
2539
        }
2540
}
2,567✔
2541

2542
static int manager_dispatch_run_queue(sd_event_source *source, void *userdata) {
4,270✔
2543
        Manager *m = ASSERT_PTR(userdata);
4,270✔
2544
        Job *j;
4,270✔
2545

2546
        assert(source);
4,270✔
2547

2548
        while ((j = prioq_peek(m->run_queue))) {
46,956✔
2549
                assert(j->installed);
42,686✔
2550
                assert(j->in_run_queue);
42,686✔
2551

2552
                (void) job_run_and_invalidate(j);
42,686✔
2553
        }
2554

2555
        if (m->n_running_jobs > 0)
4,270✔
2556
                manager_watch_jobs_in_progress(m);
3,596✔
2557

2558
        if (m->n_on_console > 0)
4,270✔
2559
                manager_watch_idle_pipe(m);
755✔
2560

2561
        return 1;
4,270✔
2562
}
2563

2564
void manager_trigger_run_queue(Manager *m) {
45,228✔
2565
        int r;
45,228✔
2566

2567
        assert(m);
45,228✔
2568

2569
        r = sd_event_source_set_enabled(
90,271✔
2570
                        m->run_queue_event_source,
2571
                        prioq_isempty(m->run_queue) ? SD_EVENT_OFF : SD_EVENT_ONESHOT);
45,228✔
2572
        if (r < 0)
45,228✔
2573
                log_warning_errno(r, "Failed to enable job run queue event source, ignoring: %m");
×
2574
}
45,228✔
2575

2576
static unsigned manager_dispatch_dbus_queue(Manager *m) {
202,974✔
2577
        unsigned n = 0, budget;
202,974✔
2578
        Unit *u;
202,974✔
2579
        Job *j;
202,974✔
2580

2581
        assert(m);
202,974✔
2582

2583
        /* When we are reloading, let's not wait with generating signals, since we need to exit the manager as quickly
2584
         * as we can. There's no point in throttling generation of signals in that case. */
2585
        if (MANAGER_IS_RELOADING(m) || m->send_reloading_done || m->pending_reload_message_dbus || m->pending_reload_message_vl)
202,974✔
2586
                budget = UINT_MAX; /* infinite budget in this case */
2587
        else {
2588
                /* Anything to do at all? */
2589
                if (!m->dbus_unit_queue && !m->dbus_job_queue)
202,923✔
2590
                        return 0;
2591

2592
                /* Do we have overly many messages queued at the moment? If so, let's not enqueue more on top, let's
2593
                 * sit this cycle out, and process things in a later cycle when the queues got a bit emptier. */
2594
                if (manager_bus_n_queued_write(m) > MANAGER_BUS_BUSY_THRESHOLD)
6,927✔
2595
                        return 0;
2596

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

2611
        while (budget != 0 && (u = m->dbus_unit_queue)) {
37,546✔
2612

2613
                assert(u->in_dbus_queue);
30,568✔
2614

2615
                bus_unit_send_change_signal(u);
30,568✔
2616
                n++;
30,568✔
2617

2618
                if (budget != UINT_MAX)
30,568✔
2619
                        budget--;
17,113✔
2620
        }
2621

2622
        while (budget != 0 && (j = m->dbus_job_queue)) {
26,284✔
2623
                assert(j->in_dbus_queue);
19,306✔
2624

2625
                bus_job_send_change_signal(j);
19,306✔
2626
                n++;
19,306✔
2627

2628
                if (budget != UINT_MAX)
19,306✔
2629
                        budget--;
18,904✔
2630
        }
2631

2632
        if (m->send_reloading_done) {
6,978✔
2633
                m->send_reloading_done = false;
51✔
2634
                bus_manager_send_reloading(m, false);
51✔
2635
                n++;
51✔
2636
        }
2637

2638
        if (m->pending_reload_message_dbus) {
6,978✔
2639
                bus_send_pending_reload_message(m);
30✔
2640
                n++;
30✔
2641
        }
2642

2643
        if (m->pending_reload_message_vl) {
6,978✔
2644
                manager_varlink_send_pending_reload_message(m);
1✔
2645
                n++;
1✔
2646
        }
2647

2648
        return n;
2649
}
2650

2651
static bool manager_process_barrier_fd(char * const *tags, FDSet *fds) {
3,848✔
2652

2653
        /* nothing else must be sent when using BARRIER=1 */
2654
        if (strv_contains(tags, "BARRIER=1")) {
3,848✔
2655
                if (strv_length(tags) != 1)
12✔
2656
                        log_warning("Extra notification messages sent with BARRIER=1, ignoring everything.");
×
2657
                else if (fdset_size(fds) != 1)
12✔
2658
                        log_warning("Got incorrect number of fds with BARRIER=1, closing them.");
×
2659

2660
                /* Drop the message if BARRIER=1 was found */
2661
                return true;
12✔
2662
        }
2663

2664
        return false;
2665
}
2666

2667
static void manager_invoke_notify_message(
7,535✔
2668
                Manager *m,
2669
                Unit *u,
2670
                PidRef *pidref,
2671
                const struct ucred *ucred,
2672
                char * const *tags,
2673
                FDSet *fds) {
2674

2675
        assert(m);
7,535✔
2676
        assert(u);
7,535✔
2677
        assert(pidref_is_set(pidref));
7,535✔
2678
        assert(ucred);
7,535✔
2679
        assert(pidref->pid == ucred->pid);
7,535✔
2680
        assert(tags);
7,535✔
2681

2682
        if (u->notifygen == m->notifygen) /* Already invoked on this same unit in this same iteration? */
7,535✔
2683
                return;
2684
        u->notifygen = m->notifygen;
3,836✔
2685

2686
        if (UNIT_VTABLE(u)->notify_message)
3,836✔
2687
                UNIT_VTABLE(u)->notify_message(u, pidref, ucred, tags, fds);
3,836✔
2688

2689
        else if (DEBUG_LOGGING) {
×
2690
                _cleanup_free_ char *joined = strv_join(tags, ", ");
×
2691
                char buf[CELLESCAPE_DEFAULT_LENGTH];
×
2692

2693
                log_unit_debug(u, "Got notification message from unexpected unit type, ignoring: %s",
×
2694
                               joined ? cellescape(buf, sizeof(buf), joined) : "(null)");
2695
        }
2696
}
2697

2698
static int manager_get_units_for_pidref(Manager *m, const PidRef *pidref, Unit ***ret_units) {
12,414✔
2699
        /* Determine array of every unit that is interested in the specified process */
2700

2701
        assert(m);
12,414✔
2702
        assert(pidref_is_set(pidref));
12,414✔
2703

2704
        Unit *u1, *u2, **array;
12,414✔
2705
        u1 = manager_get_unit_by_pidref_cgroup(m, pidref);
12,414✔
2706
        u2 = hashmap_get(m->watch_pids, pidref);
12,414✔
2707
        array = hashmap_get(m->watch_pids_more, pidref);
12,414✔
2708

2709
        size_t n = 0;
12,414✔
2710
        if (u1)
12,414✔
2711
                n++;
12,414✔
2712
        if (u2)
12,414✔
2713
                n++;
8,607✔
2714
        if (array)
12,414✔
2715
                for (size_t j = 0; array[j]; j++)
×
2716
                        n++;
×
2717

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

2720
        if (ret_units) {
12,414✔
2721
                _cleanup_free_ Unit **units = NULL;
×
2722

2723
                if (n > 0) {
12,414✔
2724
                        units = new(Unit*, n + 1);
12,414✔
2725
                        if (!units)
12,414✔
2726
                                return -ENOMEM;
×
2727

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

2732
                        size_t i = 0;
12,414✔
2733
                        if (u1)
12,414✔
2734
                                units[i++] = u1;
12,414✔
2735
                        if (u2)
12,414✔
2736
                                units[i++] = u2;
8,607✔
2737
                        if (array)
12,414✔
2738
                                for (size_t j = 0; array[j]; j++)
×
2739
                                        units[i++] = array[j];
×
2740
                        assert(i == n);
12,414✔
2741

2742
                        units[i] = NULL; /* end array in an extra NULL */
12,414✔
2743
                }
2744

2745
                *ret_units = TAKE_PTR(units);
12,414✔
2746
        }
2747

2748
        return (int) n;
12,414✔
2749
}
2750

2751
static int manager_dispatch_notify_fd(sd_event_source *source, int fd, uint32_t revents, void *userdata) {
3,848✔
2752
        Manager *m = ASSERT_PTR(userdata);
3,848✔
2753
        _cleanup_(pidref_done) PidRef pidref = PIDREF_NULL;
3,848✔
2754
        struct ucred ucred;
3,848✔
2755
        _cleanup_(fdset_free_asyncp) FDSet *fds = NULL;
3,848✔
2756
        int r;
3,848✔
2757

2758
        assert(m->notify_fd == fd);
3,848✔
2759

2760
        if (revents != EPOLLIN) {
3,848✔
2761
                log_warning("Got unexpected poll event for notify fd.");
×
2762
                return 0;
×
2763
        }
2764

2765
        _cleanup_strv_free_ char **tags = NULL;
3,848✔
2766
        r = notify_recv_with_fds_strv(m->notify_fd, &tags, &ucred, &pidref, &fds);
3,848✔
2767
        if (r == -EAGAIN)
3,848✔
2768
                return 0;
2769
        if (r < 0)
3,848✔
2770
                /* If this is any other, real error, then stop processing this socket. This of course means
2771
                 * we won't take notification messages anymore, but that's still better than busy looping:
2772
                 * being woken up over and over again, but being unable to actually read the message from the
2773
                 * socket. */
2774
                return r;
2775

2776
        /* Possibly a barrier fd, let's see. */
2777
        if (manager_process_barrier_fd(tags, fds)) {
3,848✔
2778
                log_debug("Received barrier notification message from PID " PID_FMT ".", pidref.pid);
12✔
2779
                return 0;
12✔
2780
        }
2781

2782
        /* Increase the generation counter used for filtering out duplicate unit invocations. */
2783
        m->notifygen++;
3,836✔
2784

2785
        /* Notify every unit that might be interested, which might be multiple. */
2786
        _cleanup_free_ Unit **array = NULL;
3,836✔
2787

2788
        int n_array = manager_get_units_for_pidref(m, &pidref, &array);
3,836✔
2789
        if (n_array < 0) {
3,836✔
2790
                log_warning_errno(n_array, "Failed to determine units for PID " PID_FMT ", ignoring: %m", pidref.pid);
×
2791
                return 0;
×
2792
        }
2793
        if (n_array == 0)
3,836✔
UNCOV
2794
                log_debug("Cannot find unit for notify message of PID "PID_FMT", ignoring.", pidref.pid);
×
2795
        else
2796
                /* And now invoke the per-unit callbacks. Note that manager_invoke_notify_message() will handle
2797
                 * duplicate units – making sure we only invoke each unit's handler once. */
2798
                FOREACH_ARRAY(u, array, n_array)
11,371✔
2799
                        manager_invoke_notify_message(m, *u, &pidref, &ucred, tags, fds);
7,535✔
2800

2801
        if (!fdset_isempty(fds))
3,836✔
2802
                log_warning("Got extra auxiliary fds with notification message, closing them.");
×
2803

2804
        return 0;
2805
}
2806

2807
static void manager_invoke_sigchld_event(
8,412✔
2808
                Manager *m,
2809
                Unit *u,
2810
                const siginfo_t *si) {
2811

2812
        assert(m);
8,412✔
2813
        assert(u);
8,412✔
2814
        assert(si);
8,412✔
2815

2816
        /* Already invoked the handler of this unit in this iteration? Then don't process this again */
2817
        if (u->sigchldgen == m->sigchldgen)
8,412✔
2818
                return;
2,273✔
2819
        u->sigchldgen = m->sigchldgen;
6,139✔
2820

2821
        log_unit_debug(u, "Child "PID_FMT" belongs to %s.", si->si_pid, u->id);
6,139✔
2822
        unit_unwatch_pidref(u, &PIDREF_MAKE_FROM_PID(si->si_pid));
6,139✔
2823

2824
        if (UNIT_VTABLE(u)->sigchld_event)
6,139✔
2825
                UNIT_VTABLE(u)->sigchld_event(u, si->si_pid, si->si_code, si->si_status);
5,974✔
2826
}
2827

2828
static int manager_dispatch_sigchld(sd_event_source *source, void *userdata) {
10,696✔
2829
        Manager *m = ASSERT_PTR(userdata);
10,696✔
2830
        siginfo_t si = {};
10,696✔
2831
        int r;
10,696✔
2832

2833
        assert(source);
10,696✔
2834

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

2838
        if (waitid(P_ALL, 0, &si, WEXITED|WNOHANG|WNOWAIT) < 0) {
10,696✔
2839

2840
                if (errno != ECHILD)
93✔
2841
                        log_error_errno(errno, "Failed to peek for child with waitid(), ignoring: %m");
×
2842

2843
                goto turn_off;
93✔
2844
        }
2845

2846
        if (si.si_pid <= 0)
10,603✔
2847
                goto turn_off;
4,562✔
2848

2849
        if (SIGINFO_CODE_IS_DEAD(si.si_code)) {
6,041✔
2850
                _cleanup_free_ char *name = NULL;
12,082✔
2851
                (void) pid_get_comm(si.si_pid, &name);
6,041✔
2852

2853
                log_debug("Child "PID_FMT" (%s) died (code=%s, status=%i/%s)",
12,082✔
2854
                          si.si_pid, strna(name),
2855
                          sigchld_code_to_string(si.si_code),
2856
                          si.si_status,
2857
                          strna(si.si_code == CLD_EXITED
2858
                                ? exit_status_to_string(si.si_status, EXIT_STATUS_FULL)
2859
                                : signal_to_string(si.si_status)));
2860

2861
                /* Increase the generation counter used for filtering out duplicate unit invocations */
2862
                m->sigchldgen++;
6,041✔
2863

2864
                /* We look this up by a PidRef that only consists of the PID. After all we couldn't create a
2865
                 * pidfd here any more even if we wanted (since the process just exited). */
2866
                PidRef pidref = PIDREF_MAKE_FROM_PID(si.si_pid);
6,041✔
2867

2868
                /* And now figure out the units this belongs to, there might be multiple... */
2869
                _cleanup_free_ Unit **array = NULL;
6,041✔
2870
                int n_array = manager_get_units_for_pidref(m, &pidref, &array);
6,041✔
2871
                if (n_array < 0)
6,041✔
2872
                        log_warning_errno(n_array, "Failed to get units for process " PID_FMT ", ignoring: %m", si.si_pid);
6,041✔
2873
                else if (n_array == 0)
6,041✔
2874
                        log_debug("Got SIGCHLD for process " PID_FMT " we weren't interested in, ignoring.", si.si_pid);
×
2875
                else {
2876
                        /* We check for an OOM condition, in case we got SIGCHLD before the OOM notification.
2877
                         * We only do this for the cgroup the PID belonged to, which is the f */
2878
                        (void) unit_check_oom(array[0]);
6,041✔
2879

2880
                        /* We check if systemd-oomd performed a kill so that we log and notify appropriately */
2881
                        (void) unit_check_oomd_kill(array[0]);
6,041✔
2882

2883
                        /* Finally, execute them all. Note that the array might contain duplicates, but that's fine,
2884
                         * manager_invoke_sigchld_event() will ensure we only invoke the handlers once for each
2885
                         * iteration. */
2886
                        FOREACH_ARRAY(u, array, n_array)
14,453✔
2887
                                manager_invoke_sigchld_event(m, *u, &si);
8,412✔
2888
                }
2889
        }
2890

2891
        /* And now, we actually reap the zombie. */
2892
        if (waitid(P_PID, si.si_pid, &si, WEXITED) < 0) {
6,041✔
2893
                log_error_errno(errno, "Failed to dequeue child, ignoring: %m");
×
2894
                return 0;
10,696✔
2895
        }
2896

2897
        return 0;
2898

2899
turn_off:
4,655✔
2900
        /* All children processed for now, turn off event source */
2901

2902
        r = sd_event_source_set_enabled(m->sigchld_event_source, SD_EVENT_OFF);
4,655✔
2903
        if (r < 0)
4,655✔
2904
                return log_error_errno(r, "Failed to disable SIGCHLD event source: %m");
×
2905

2906
        return 0;
2907
}
2908

2909
static void manager_start_special(Manager *m, const char *name, JobMode mode) {
187✔
2910
        Job *job;
187✔
2911

2912
        if (manager_add_job_by_name_and_warn(m, JOB_START, name, mode, NULL, &job) < 0)
187✔
2913
                return;
×
2914

2915
        const char *s = unit_status_string(job->unit, NULL);
187✔
2916

2917
        log_info("Activating special unit %s...", s);
187✔
2918

2919
        (void) sd_notifyf(/* unset_environment= */ false,
187✔
2920
                          "STATUS=Activating special unit %s...", s);
2921
        m->status_ready = false;
187✔
2922
}
2923

2924
static void manager_handle_ctrl_alt_del(Manager *m) {
×
2925
        assert(m);
×
2926

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

2930
        if (ratelimit_below(&m->ctrl_alt_del_ratelimit) || m->cad_burst_action == EMERGENCY_ACTION_NONE)
×
2931
                manager_start_special(m, SPECIAL_CTRL_ALT_DEL_TARGET, JOB_REPLACE_IRREVERSIBLY);
×
2932
        else
2933
                emergency_action(
×
2934
                                m,
2935
                                m->cad_burst_action,
2936
                                EMERGENCY_ACTION_WARN,
2937
                                /* reboot_arg= */ NULL,
2938
                                /* exit_status= */ -1,
2939
                                "Ctrl-Alt-Del was pressed more than 7 times within 2s");
2940
}
×
2941

2942
static int manager_dispatch_signal_fd(sd_event_source *source, int fd, uint32_t revents, void *userdata) {
4,571✔
2943
        Manager *m = ASSERT_PTR(userdata);
4,571✔
2944
        ssize_t n;
4,571✔
2945
        struct signalfd_siginfo sfsi;
4,571✔
2946
        int r;
4,571✔
2947

2948
        assert(m->signal_fd == fd);
4,571✔
2949

2950
        if (revents != EPOLLIN) {
4,571✔
2951
                log_warning("Got unexpected events from signal file descriptor.");
×
2952
                return 0;
×
2953
        }
2954

2955
        n = read(m->signal_fd, &sfsi, sizeof(sfsi));
4,571✔
2956
        if (n < 0) {
4,571✔
2957
                if (ERRNO_IS_TRANSIENT(errno))
×
2958
                        return 0;
2959

2960
                /* We return an error here, which will kill this handler,
2961
                 * to avoid a busy loop on read error. */
2962
                return log_error_errno(errno, "Reading from signal fd failed: %m");
×
2963
        }
2964
        if (n != sizeof(sfsi)) {
4,571✔
2965
                log_warning("Truncated read from signal fd (%zi bytes), ignoring!", n);
×
2966
                return 0;
×
2967
        }
2968

2969
        log_received_signal(sfsi.ssi_signo == SIGCHLD ||
4,758✔
2970
                            (sfsi.ssi_signo == SIGTERM && MANAGER_IS_USER(m))
187✔
2971
                            ? LOG_DEBUG : LOG_INFO,
2972
                            &sfsi);
2973

2974
        switch (sfsi.ssi_signo) {
4,571✔
2975

2976
        case SIGCHLD:
4,384✔
2977
                r = sd_event_source_set_enabled(m->sigchld_event_source, SD_EVENT_ON);
4,384✔
2978
                if (r < 0)
4,384✔
2979
                        log_warning_errno(r, "Failed to enable SIGCHLD event source, ignoring: %m");
4,571✔
2980

2981
                break;
2982

2983
        case SIGTERM:
187✔
2984
                if (MANAGER_IS_SYSTEM(m)) {
187✔
2985
                        /* This is for compatibility with the original sysvinit */
2986
                        m->objective = MANAGER_REEXECUTE;
×
2987
                        break;
×
2988
                }
2989

2990
                _fallthrough_;
187✔
2991
        case SIGINT:
2992
                if (MANAGER_IS_SYSTEM(m))
187✔
2993
                        manager_handle_ctrl_alt_del(m);
×
2994
                else
2995
                        manager_start_special(m, SPECIAL_EXIT_TARGET, JOB_REPLACE_IRREVERSIBLY);
187✔
2996
                break;
2997

2998
        case SIGWINCH:
×
2999
                /* This is a nop on non-init */
3000
                if (MANAGER_IS_SYSTEM(m))
×
3001
                        manager_start_special(m, SPECIAL_KBREQUEST_TARGET, JOB_REPLACE);
×
3002

3003
                break;
3004

3005
        case SIGPWR:
×
3006
                /* This is a nop on non-init */
3007
                if (MANAGER_IS_SYSTEM(m))
×
3008
                        manager_start_special(m, SPECIAL_SIGPWR_TARGET, JOB_REPLACE);
×
3009

3010
                break;
3011

3012
        case SIGUSR1:
×
3013
                if (manager_dbus_is_running(m, false)) {
×
3014
                        log_info("Trying to reconnect to bus...");
×
3015

3016
                        (void) bus_init_api(m);
×
3017

3018
                        if (MANAGER_IS_SYSTEM(m))
×
3019
                                (void) bus_init_system(m);
×
3020
                } else
3021
                        manager_start_special(m, SPECIAL_DBUS_SERVICE, JOB_REPLACE);
×
3022

3023
                break;
3024

3025
        case SIGUSR2: {
×
3026
                _cleanup_free_ char *dump = NULL;
×
3027

3028
                r = manager_get_dump_string(m, /* patterns= */ NULL, &dump);
×
3029
                if (r < 0) {
×
3030
                        log_warning_errno(r, "Failed to acquire manager dump: %m");
×
3031
                        break;
3032
                }
3033

3034
                log_dump(LOG_INFO, dump);
×
3035
                break;
3036
        }
3037

3038
        case SIGHUP:
×
3039
                m->objective = MANAGER_RELOAD;
×
3040
                break;
×
3041

3042
        default: {
×
3043

3044
                if (MANAGER_IS_SYSTEM(m)) {
×
3045
                        /* Starting SIGRTMIN+0 */
3046
                        static const struct {
×
3047
                                const char *target;
3048
                                JobMode mode;
3049
                        } target_table[] = {
3050
                                [0] = { SPECIAL_DEFAULT_TARGET,     JOB_ISOLATE              },
3051
                                [1] = { SPECIAL_RESCUE_TARGET,      JOB_ISOLATE              },
3052
                                [2] = { SPECIAL_EMERGENCY_TARGET,   JOB_ISOLATE              },
3053
                                [3] = { SPECIAL_HALT_TARGET,        JOB_REPLACE_IRREVERSIBLY },
3054
                                [4] = { SPECIAL_POWEROFF_TARGET,    JOB_REPLACE_IRREVERSIBLY },
3055
                                [5] = { SPECIAL_REBOOT_TARGET,      JOB_REPLACE_IRREVERSIBLY },
3056
                                [6] = { SPECIAL_KEXEC_TARGET,       JOB_REPLACE_IRREVERSIBLY },
3057
                                [7] = { SPECIAL_SOFT_REBOOT_TARGET, JOB_REPLACE_IRREVERSIBLY },
3058
                        };
3059

3060
                        /* Starting SIGRTMIN+13, so that target halt and system halt are 10 apart */
3061
                        static const ManagerObjective objective_table[] = {
×
3062
                                [0] = MANAGER_HALT,
3063
                                [1] = MANAGER_POWEROFF,
3064
                                [2] = MANAGER_REBOOT,
3065
                                [3] = MANAGER_KEXEC,
3066
                                [4] = MANAGER_SOFT_REBOOT,
3067
                        };
3068

3069
                        if ((int) sfsi.ssi_signo >= SIGRTMIN+0 &&
×
3070
                            (int) sfsi.ssi_signo < SIGRTMIN+(int) ELEMENTSOF(target_table)) {
×
3071
                                int idx = (int) sfsi.ssi_signo - SIGRTMIN;
×
3072
                                manager_start_special(m, target_table[idx].target, target_table[idx].mode);
×
3073
                                break;
3074
                        }
3075

3076
                        if ((int) sfsi.ssi_signo >= SIGRTMIN+13 &&
×
3077
                            (int) sfsi.ssi_signo < SIGRTMIN+13+(int) ELEMENTSOF(objective_table)) {
×
3078
                                m->objective = objective_table[sfsi.ssi_signo - SIGRTMIN - 13];
×
3079
                                break;
×
3080
                        }
3081
                }
3082

3083
                switch (sfsi.ssi_signo - SIGRTMIN) {
×
3084

3085
                case 18: {
×
3086
                        bool generic = false;
×
3087

3088
                        if (sfsi.ssi_code != SI_QUEUE)
×
3089
                                generic = true;
3090
                        else {
3091
                                /* Override a few select commands by our own PID1-specific logic */
3092

3093
                                switch (sfsi.ssi_int) {
×
3094

3095
                                case _COMMON_SIGNAL_COMMAND_LOG_LEVEL_BASE..._COMMON_SIGNAL_COMMAND_LOG_LEVEL_END:
×
3096
                                        manager_override_log_level(m, sfsi.ssi_int - _COMMON_SIGNAL_COMMAND_LOG_LEVEL_BASE);
×
3097
                                        break;
3098

3099
                                case COMMON_SIGNAL_COMMAND_CONSOLE:
×
3100
                                        manager_override_log_target(m, LOG_TARGET_CONSOLE);
×
3101
                                        break;
3102

3103
                                case COMMON_SIGNAL_COMMAND_JOURNAL:
×
3104
                                        manager_override_log_target(m, LOG_TARGET_JOURNAL);
×
3105
                                        break;
3106

3107
                                case COMMON_SIGNAL_COMMAND_KMSG:
×
3108
                                        manager_override_log_target(m, LOG_TARGET_KMSG);
×
3109
                                        break;
3110

3111
                                case COMMON_SIGNAL_COMMAND_NULL:
×
3112
                                        manager_override_log_target(m, LOG_TARGET_NULL);
×
3113
                                        break;
3114

3115
                                case MANAGER_SIGNAL_COMMAND_DUMP_JOBS: {
×
3116
                                        _cleanup_free_ char *dump_jobs = NULL;
×
3117

3118
                                        r = manager_get_dump_jobs_string(m, /* patterns= */ NULL, "  ", &dump_jobs);
×
3119
                                        if (r < 0) {
×
3120
                                                log_warning_errno(r, "Failed to acquire manager jobs dump: %m");
×
3121
                                                break;
3122
                                        }
3123

3124
                                        log_dump(LOG_INFO, dump_jobs);
×
3125
                                        break;
3126
                                }
3127

3128
                                default:
3129
                                        generic = true;
3130
                                }
3131
                        }
3132

3133
                        if (generic)
×
3134
                                return sigrtmin18_handler(source, &sfsi, NULL);
×
3135

3136
                        break;
3137
                }
3138

3139
                case 20:
×
3140
                        manager_override_show_status(m, SHOW_STATUS_YES, "signal");
×
3141
                        break;
3142

3143
                case 21:
×
3144
                        manager_override_show_status(m, SHOW_STATUS_NO, "signal");
×
3145
                        break;
3146

3147
                case 22:
×
3148
                        manager_override_log_level(m, LOG_DEBUG);
×
3149
                        break;
3150

3151
                case 23:
×
3152
                        manager_restore_original_log_level(m);
×
3153
                        break;
3154

3155
                case 24:
×
3156
                        if (MANAGER_IS_USER(m)) {
×
3157
                                m->objective = MANAGER_EXIT;
×
3158
                                return 0;
×
3159
                        }
3160

3161
                        /* This is a nop on init */
3162
                        break;
3163

3164
                case 25:
×
3165
                        m->objective = MANAGER_REEXECUTE;
×
3166
                        break;
×
3167

3168
                case 26:
×
3169
                case 29: /* compatibility: used to be mapped to LOG_TARGET_SYSLOG_OR_KMSG */
3170
                        manager_restore_original_log_target(m);
×
3171
                        break;
3172

3173
                case 27:
×
3174
                        manager_override_log_target(m, LOG_TARGET_CONSOLE);
×
3175
                        break;
3176

3177
                case 28:
×
3178
                        manager_override_log_target(m, LOG_TARGET_KMSG);
×
3179
                        break;
3180

3181
                default:
3182
                        log_warning("Got unhandled signal <%s>.", signal_to_string(sfsi.ssi_signo));
×
3183
                }
3184
        }}
3185

3186
        return 0;
3187
}
3188

3189
static int manager_dispatch_time_change_fd(sd_event_source *source, int fd, uint32_t revents, void *userdata) {
10✔
3190
        Manager *m = ASSERT_PTR(userdata);
10✔
3191
        Unit *u;
10✔
3192

3193
        log_struct(LOG_DEBUG,
10✔
3194
                   LOG_MESSAGE_ID(SD_MESSAGE_TIME_CHANGE_STR),
3195
                   LOG_MESSAGE("Time has been changed"));
3196

3197
        /* Restart the watch */
3198
        (void) manager_setup_time_change(m);
10✔
3199

3200
        HASHMAP_FOREACH(u, m->units)
1,366✔
3201
                if (UNIT_VTABLE(u)->time_change)
1,346✔
3202
                        UNIT_VTABLE(u)->time_change(u);
10✔
3203

3204
        return 0;
10✔
3205
}
3206

3207
static int manager_dispatch_timezone_change(
65✔
3208
                sd_event_source *source,
3209
                const struct inotify_event *e,
3210
                void *userdata) {
3211

3212
        Manager *m = ASSERT_PTR(userdata);
65✔
3213
        int changed;
65✔
3214
        Unit *u;
65✔
3215

3216
        log_debug("inotify event for /etc/localtime");
65✔
3217

3218
        changed = manager_read_timezone_stat(m);
65✔
3219
        if (changed <= 0)
65✔
3220
                return changed;
65✔
3221

3222
        /* Something changed, restart the watch, to ensure we watch the new /etc/localtime if it changed */
3223
        (void) manager_setup_timezone_change(m);
33✔
3224

3225
        /* Read the new timezone */
3226
        tzset();
33✔
3227

3228
        log_debug("Timezone has been changed (now: %s).", get_tzname(daylight));
33✔
3229

3230
        HASHMAP_FOREACH(u, m->units)
6,807✔
3231
                if (UNIT_VTABLE(u)->timezone_change)
6,741✔
3232
                        UNIT_VTABLE(u)->timezone_change(u);
73✔
3233

3234
        return 0;
33✔
3235
}
3236

3237
static int manager_dispatch_idle_pipe_fd(sd_event_source *source, int fd, uint32_t revents, void *userdata) {
9✔
3238
        Manager *m = ASSERT_PTR(userdata);
9✔
3239

3240
        assert(m->idle_pipe[2] == fd);
9✔
3241

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

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

3253
        return 0;
9✔
3254
}
3255

3256
static int manager_dispatch_jobs_in_progress(sd_event_source *source, usec_t usec, void *userdata) {
×
3257
        Manager *m = ASSERT_PTR(userdata);
×
3258
        int r;
×
3259

3260
        assert(source);
×
3261

3262
        manager_print_jobs_in_progress(m);
×
3263

3264
        r = sd_event_source_set_time_relative(source, JOBS_IN_PROGRESS_PERIOD_USEC);
×
3265
        if (r < 0)
×
3266
                return r;
3267

3268
        return sd_event_source_set_enabled(source, SD_EVENT_ONESHOT);
×
3269
}
3270

3271
int manager_loop(Manager *m) {
274✔
3272
        RateLimit rl = { .interval = 1*USEC_PER_SEC, .burst = 50000 };
274✔
3273
        int r;
274✔
3274

3275
        assert(m);
274✔
3276
        assert(m->objective == MANAGER_OK); /* Ensure manager_startup() has been called */
274✔
3277

3278
        manager_check_finished(m);
274✔
3279

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

3285
        while (m->objective == MANAGER_OK) {
236,891✔
3286

3287
                if (!ratelimit_below(&rl)) {
236,617✔
3288
                        /* Yay, something is going seriously wrong, pause a little */
3289
                        log_warning("Looping too fast. Throttling execution a little.");
×
3290
                        sleep(1);
×
3291
                }
3292

3293
                (void) watchdog_ping();
236,617✔
3294

3295
                if (manager_dispatch_load_queue(m) > 0)
236,617✔
3296
                        continue;
1✔
3297

3298
                if (manager_dispatch_gc_job_queue(m) > 0)
236,616✔
3299
                        continue;
9✔
3300

3301
                if (manager_dispatch_gc_unit_queue(m) > 0)
236,607✔
3302
                        continue;
17,903✔
3303

3304
                if (manager_dispatch_cleanup_queue(m) > 0)
218,704✔
3305
                        continue;
9,761✔
3306

3307
                if (manager_dispatch_cgroup_realize_queue(m) > 0)
208,943✔
3308
                        continue;
4,382✔
3309

3310
                if (manager_dispatch_start_when_upheld_queue(m) > 0)
204,561✔
3311
                        continue;
×
3312

3313
                if (manager_dispatch_stop_when_bound_queue(m) > 0)
204,561✔
3314
                        continue;
152✔
3315

3316
                if (manager_dispatch_stop_when_unneeded_queue(m) > 0)
204,409✔
3317
                        continue;
255✔
3318

3319
                if (manager_dispatch_release_resources_queue(m) > 0)
204,154✔
3320
                        continue;
1,180✔
3321

3322
                if (manager_dispatch_stop_notify_queue(m) > 0)
202,974✔
3323
                        continue;
×
3324

3325
                if (manager_dispatch_dbus_queue(m) > 0)
202,974✔
3326
                        continue;
6,978✔
3327

3328
                /* Sleep for watchdog runtime wait time */
3329
                r = sd_event_run(m->event, watchdog_runtime_wait(/* divisor= */ 2));
195,996✔
3330
                if (r < 0)
195,996✔
3331
                        return log_error_errno(r, "Failed to run event loop: %m");
×
3332
        }
3333

3334
        return m->objective;
3335
}
3336

3337
int manager_load_unit_from_dbus_path(Manager *m, const char *s, sd_bus_error *e, Unit **_u) {
359,440✔
3338
        _cleanup_free_ char *n = NULL;
359,440✔
3339
        sd_id128_t invocation_id;
359,440✔
3340
        Unit *u;
359,440✔
3341
        int r;
359,440✔
3342

3343
        assert(m);
359,440✔
3344
        assert(s);
359,440✔
3345
        assert(_u);
359,440✔
3346

3347
        r = unit_name_from_dbus_path(s, &n);
359,440✔
3348
        if (r < 0)
359,440✔
3349
                return r;
3350

3351
        /* Permit addressing units by invocation ID: if the passed bus path is suffixed by a 128-bit ID then
3352
         * we use it as invocation ID. */
3353
        r = sd_id128_from_string(n, &invocation_id);
359,403✔
3354
        if (r >= 0) {
359,403✔
3355
                u = hashmap_get(m->units_by_invocation_id, &invocation_id);
×
3356
                if (u) {
×
3357
                        *_u = u;
×
3358
                        return 0;
×
3359
                }
3360

3361
                return sd_bus_error_setf(e, BUS_ERROR_NO_UNIT_FOR_INVOCATION_ID,
×
3362
                                         "No unit with the specified invocation ID " SD_ID128_FORMAT_STR " known.",
3363
                                         SD_ID128_FORMAT_VAL(invocation_id));
×
3364
        }
3365

3366
        /* If this didn't work, we check if this is a unit name */
3367
        if (!unit_name_is_valid(n, UNIT_NAME_PLAIN|UNIT_NAME_INSTANCE)) {
359,403✔
3368
                _cleanup_free_ char *nn = NULL;
1✔
3369

3370
                nn = cescape(n);
1✔
3371
                return sd_bus_error_setf(e, SD_BUS_ERROR_INVALID_ARGS,
1✔
3372
                                         "Unit name %s is neither a valid invocation ID nor unit name.", strnull(nn));
3373
        }
3374

3375
        r = manager_load_unit(m, n, NULL, e, &u);
359,402✔
3376
        if (r < 0)
359,402✔
3377
                return r;
3378

3379
        *_u = u;
359,402✔
3380
        return 0;
359,402✔
3381
}
3382

3383
int manager_get_job_from_dbus_path(Manager *m, const char *s, Job **_j) {
916✔
3384
        const char *p;
916✔
3385
        unsigned id;
916✔
3386
        Job *j;
916✔
3387
        int r;
916✔
3388

3389
        assert(m);
916✔
3390
        assert(s);
916✔
3391
        assert(_j);
916✔
3392

3393
        p = startswith(s, "/org/freedesktop/systemd1/job/");
916✔
3394
        if (!p)
916✔
3395
                return -EINVAL;
916✔
3396

3397
        r = safe_atou(p, &id);
915✔
3398
        if (r < 0)
915✔
3399
                return r;
3400

3401
        j = manager_get_job(m, id);
915✔
3402
        if (!j)
915✔
3403
                return -ENOENT;
3404

3405
        *_j = j;
907✔
3406

3407
        return 0;
907✔
3408
}
3409

3410
void manager_send_unit_audit(Manager *m, Unit *u, int type, bool success) {
3,868✔
3411

3412
#if HAVE_AUDIT
3413
        _cleanup_free_ char *p = NULL;
3,868✔
3414
        const char *msg;
3,868✔
3415
        int audit_fd, r;
3,868✔
3416

3417
        assert(m);
3,868✔
3418
        assert(u);
3,868✔
3419

3420
        if (!MANAGER_IS_SYSTEM(m))
3,868✔
3421
                return;
3422

3423
        /* Don't generate audit events if the service was already started and we're just deserializing */
3424
        if (MANAGER_IS_RELOADING(m))
2,482✔
3425
                return;
3426

3427
        audit_fd = get_core_audit_fd();
2,482✔
3428
        if (audit_fd < 0)
2,482✔
3429
                return;
3430

3431
        r = unit_name_to_prefix_and_instance(u->id, &p);
1,003✔
3432
        if (r < 0) {
1,003✔
3433
                log_warning_errno(r, "Failed to extract prefix and instance of unit name, ignoring: %m");
×
3434
                return;
×
3435
        }
3436

3437
        msg = strjoina("unit=", p);
5,015✔
3438
        if (sym_audit_log_user_comm_message(audit_fd, type, msg, "systemd", NULL, NULL, NULL, success) < 0) {
1,003✔
3439
                if (ERRNO_IS_PRIVILEGE(errno)) {
×
3440
                        /* We aren't allowed to send audit messages?  Then let's not retry again. */
3441
                        log_debug_errno(errno, "Failed to send audit message, closing audit socket: %m");
×
3442
                        close_core_audit_fd();
×
3443
                } else
3444
                        log_warning_errno(errno, "Failed to send audit message, ignoring: %m");
1,003✔
3445
        }
3446
#endif
3447
}
3448

3449
void manager_send_unit_plymouth(Manager *m, Unit *u) {
40,241✔
3450
        _cleanup_free_ char *message = NULL;
40,241✔
3451
        int c, r;
40,241✔
3452

3453
        assert(m);
40,241✔
3454
        assert(u);
40,241✔
3455

3456
        if (!MANAGER_IS_SYSTEM(m))
40,241✔
3457
                return;
3458

3459
        /* Don't generate plymouth events if the service was already started and we're just deserializing */
3460
        if (MANAGER_IS_RELOADING(m))
6,954✔
3461
                return;
3462

3463
        if (detect_container() > 0)
6,954✔
3464
                return;
3465

3466
        if (!UNIT_VTABLE(u)->notify_plymouth)
4,191✔
3467
                return;
3468

3469
        c = asprintf(&message, "U\x02%c%s%c", (int) (strlen(u->id) + 1), u->id, '\x00');
709✔
3470
        if (c < 0)
709✔
3471
                return (void) log_oom();
×
3472

3473
        /* We set SOCK_NONBLOCK here so that we rather drop the message then wait for plymouth */
3474
        r = plymouth_send_raw(message, c, SOCK_NONBLOCK);
709✔
3475
        if (r < 0)
709✔
3476
                log_full_errno(ERRNO_IS_NO_PLYMOUTH(r) ? LOG_DEBUG : LOG_WARNING, r,
709✔
3477
                               "Failed to communicate with plymouth: %m");
3478
}
3479

3480
void manager_send_unit_supervisor(Manager *m, Unit *u, bool active) {
64,119✔
3481
        assert(m);
64,119✔
3482
        assert(u);
64,119✔
3483

3484
        /* Notify a "supervisor" process about our progress, i.e. a container manager, hypervisor, or
3485
         * surrounding service manager. */
3486

3487
        if (MANAGER_IS_RELOADING(m))
64,119✔
3488
                return;
3489

3490
        if (!UNIT_VTABLE(u)->notify_supervisor)
64,119✔
3491
                return;
3492

3493
        if (in_initrd()) /* Only send these once we left the initrd */
4,317✔
3494
                return;
3495

3496
        (void) sd_notifyf(/* unset_environment= */ false,
5,313✔
3497
                          active ? "X_SYSTEMD_UNIT_ACTIVE=%s" : "X_SYSTEMD_UNIT_INACTIVE=%s",
3498
                          u->id);
3499
}
3500

3501
usec_t manager_get_watchdog(Manager *m, WatchdogType t) {
571✔
3502
        assert(m);
571✔
3503

3504
        if (MANAGER_IS_USER(m))
571✔
3505
                return USEC_INFINITY;
3506

3507
        if (m->watchdog_overridden[t] != USEC_INFINITY)
191✔
3508
                return m->watchdog_overridden[t];
3509

3510
        return m->watchdog[t];
188✔
3511
}
3512

3513
void manager_set_watchdog(Manager *m, WatchdogType t, usec_t timeout) {
1,096✔
3514

3515
        assert(m);
1,096✔
3516

3517
        if (MANAGER_IS_USER(m))
1,096✔
3518
                return;
3519

3520
        if (m->watchdog_overridden[t] == USEC_INFINITY) {
336✔
3521
                if (t == WATCHDOG_RUNTIME)
330✔
3522
                        (void) watchdog_setup(timeout);
84✔
3523
                else if (t == WATCHDOG_PRETIMEOUT)
246✔
3524
                        (void) watchdog_setup_pretimeout(timeout);
84✔
3525
        }
3526

3527
        m->watchdog[t] = timeout;
336✔
3528
}
3529

3530
void manager_override_watchdog(Manager *m, WatchdogType t, usec_t timeout) {
7✔
3531
        usec_t usec;
7✔
3532

3533
        assert(m);
7✔
3534

3535
        if (MANAGER_IS_USER(m))
7✔
3536
                return;
3537

3538
        usec = timeout == USEC_INFINITY ? m->watchdog[t] : timeout;
7✔
3539
        if (t == WATCHDOG_RUNTIME)
7✔
3540
                (void) watchdog_setup(usec);
×
3541
        else if (t == WATCHDOG_PRETIMEOUT)
7✔
3542
                (void) watchdog_setup_pretimeout(usec);
×
3543

3544
        m->watchdog_overridden[t] = timeout;
7✔
3545
}
3546

3547
int manager_set_watchdog_pretimeout_governor(Manager *m, const char *governor) {
274✔
3548
        _cleanup_free_ char *p = NULL;
274✔
3549
        int r;
274✔
3550

3551
        assert(m);
274✔
3552

3553
        if (MANAGER_IS_USER(m))
274✔
3554
                return 0;
3555

3556
        if (streq_ptr(m->watchdog_pretimeout_governor, governor))
84✔
3557
                return 0;
3558

3559
        p = strdup(governor);
×
3560
        if (!p)
×
3561
                return -ENOMEM;
3562

3563
        r = watchdog_setup_pretimeout_governor(governor);
×
3564
        if (r < 0)
×
3565
                return r;
3566

3567
        return free_and_replace(m->watchdog_pretimeout_governor, p);
×
3568
}
3569

3570
int manager_override_watchdog_pretimeout_governor(Manager *m, const char *governor) {
×
3571
        _cleanup_free_ char *p = NULL;
×
3572
        int r;
×
3573

3574
        assert(m);
×
3575

3576
        if (MANAGER_IS_USER(m))
×
3577
                return 0;
3578

3579
        if (streq_ptr(m->watchdog_pretimeout_governor_overridden, governor))
×
3580
                return 0;
3581

3582
        p = strdup(governor);
×
3583
        if (!p)
×
3584
                return -ENOMEM;
3585

3586
        r = watchdog_setup_pretimeout_governor(governor);
×
3587
        if (r < 0)
×
3588
                return r;
3589

3590
        return free_and_replace(m->watchdog_pretimeout_governor_overridden, p);
×
3591
}
3592

3593
int manager_reload(Manager *m) {
31✔
3594
        _unused_ _cleanup_(manager_reloading_stopp) Manager *reloading = NULL;
31✔
3595
        _cleanup_fdset_free_ FDSet *fds = NULL;
×
3596
        _cleanup_fclose_ FILE *f = NULL;
31✔
3597
        int r;
31✔
3598

3599
        assert(m);
31✔
3600

3601
        r = manager_open_serialization(m, &f);
31✔
3602
        if (r < 0)
31✔
3603
                return log_error_errno(r, "Failed to create serialization file: %m");
×
3604

3605
        fds = fdset_new();
31✔
3606
        if (!fds)
31✔
3607
                return log_oom();
×
3608

3609
        /* We are officially in reload mode from here on. */
3610
        reloading = manager_reloading_start(m);
31✔
3611

3612
        r = manager_serialize(m, f, fds, false);
31✔
3613
        if (r < 0)
31✔
3614
                return r;
3615

3616
        r = finish_serialization_file(f);
31✔
3617
        if (r < 0)
31✔
3618
                return log_error_errno(r, "Failed to finish serialization: %m");
×
3619

3620
        /* 💀 This is the point of no return, from here on there is no way back. 💀 */
3621
        reloading = NULL;
31✔
3622

3623
        bus_manager_send_reloading(m, true);
31✔
3624

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

3629
        manager_clear_jobs_and_units(m);
31✔
3630
        lookup_paths_flush_generator(&m->lookup_paths);
31✔
3631
        exec_shared_runtime_vacuum(m);
31✔
3632
        dynamic_user_vacuum(m, false);
31✔
3633
        m->uid_refs = hashmap_free(m->uid_refs);
31✔
3634
        m->gid_refs = hashmap_free(m->gid_refs);
31✔
3635

3636
        (void) manager_run_environment_generators(m);
31✔
3637
        (void) manager_run_generators(m);
31✔
3638

3639
        /* We flushed out generated files, for which we don't watch mtime, so we should flush the old map. */
3640
        manager_free_unit_name_maps(m);
31✔
3641
        m->unit_file_state_outdated = false;
31✔
3642

3643
        /* First, enumerate what we can from kernel and suchlike */
3644
        manager_enumerate_perpetual(m);
31✔
3645
        manager_enumerate(m);
31✔
3646

3647
        /* Second, deserialize our stored data */
3648
        r = manager_deserialize(m, f, fds);
31✔
3649
        if (r < 0)
31✔
3650
                log_warning_errno(r, "Deserialization failed, proceeding anyway: %m");
×
3651

3652
        /* We don't need the serialization anymore */
3653
        f = safe_fclose(f);
31✔
3654

3655
        /* Re-register notify_fd as event source, and set up other sockets/communication channels we might need */
3656
        (void) manager_setup_notify(m);
31✔
3657
        (void) manager_setup_user_lookup_fd(m);
31✔
3658
        (void) manager_setup_handoff_timestamp_fd(m);
31✔
3659
        (void) manager_setup_pidref_transport_fd(m);
31✔
3660

3661
        /* Clean up deserialized bus track information. They're never consumed during reload (as opposed to
3662
         * reexec) since we do not disconnect from the bus. */
3663
        m->subscribed_as_strv = strv_free(m->subscribed_as_strv);
31✔
3664
        m->deserialized_bus_id = SD_ID128_NULL;
31✔
3665

3666
        /* Third, fire things up! */
3667
        manager_coldplug(m);
31✔
3668

3669
        /* Clean up runtime objects no longer referenced */
3670
        manager_vacuum(m);
31✔
3671

3672
        /* Consider the reload process complete now. */
3673
        assert(m->n_reloading > 0);
31✔
3674
        m->n_reloading--;
31✔
3675

3676
        manager_ready(m);
31✔
3677

3678
        m->send_reloading_done = true;
31✔
3679
        return 0;
31✔
3680
}
3681

3682
void manager_reset_failed(Manager *m) {
1✔
3683
        Unit *u;
1✔
3684

3685
        assert(m);
1✔
3686

3687
        HASHMAP_FOREACH(u, m->units)
255✔
3688
                unit_reset_failed(u);
254✔
3689

3690
        m->transactions_with_cycle = set_free(m->transactions_with_cycle);
1✔
3691
}
1✔
3692

3693
bool manager_unit_inactive_or_pending(Manager *m, const char *name) {
×
3694
        Unit *u;
×
3695

3696
        assert(m);
×
3697
        assert(name);
×
3698

3699
        /* Returns true if the unit is inactive or going down */
3700
        u = manager_get_unit(m, name);
×
3701
        if (!u)
×
3702
                return true;
3703

3704
        return unit_inactive_or_pending(u);
×
3705
}
3706

3707
static void log_taint_string(Manager *m) {
3,291✔
3708
        assert(m);
3,291✔
3709

3710
        if (MANAGER_IS_USER(m) || m->taint_logged)
3,291✔
3711
                return;
3,291✔
3712

3713
        m->taint_logged = true; /* only check for taint once */
36✔
3714

3715
        _cleanup_free_ char *taint = taint_string();
72✔
3716
        if (isempty(taint))
36✔
3717
                return;
36✔
3718

3719
        log_struct(LOG_NOTICE,
×
3720
                   LOG_MESSAGE("System is tainted: %s", taint),
3721
                   LOG_ITEM("TAINT=%s", taint),
3722
                   LOG_MESSAGE_ID(SD_MESSAGE_TAINTED_STR));
3723
}
3724

3725
static void manager_notify_finished(Manager *m) {
197✔
3726
        usec_t firmware_usec, loader_usec, kernel_usec, initrd_usec, userspace_usec, total_usec;
197✔
3727

3728
        if (MANAGER_IS_TEST_RUN(m))
197✔
3729
                return;
3730

3731
        if (MANAGER_IS_SYSTEM(m) && m->soft_reboots_count > 0) {
190✔
3732
                /* The soft-reboot case, where we only report data for the last reboot */
3733
                firmware_usec = loader_usec = initrd_usec = kernel_usec = 0;
×
3734
                total_usec = userspace_usec = usec_sub_unsigned(m->timestamps[MANAGER_TIMESTAMP_FINISH].monotonic,
×
3735
                                                                m->timestamps[MANAGER_TIMESTAMP_SHUTDOWN_START].monotonic);
3736

3737
                log_struct(LOG_INFO,
×
3738
                           LOG_MESSAGE_ID(SD_MESSAGE_STARTUP_FINISHED_STR),
3739
                           LOG_ITEM("USERSPACE_USEC="USEC_FMT, userspace_usec),
3740
                           LOG_MESSAGE("Soft-reboot finished in %s, counter is now at %u.",
3741
                                       FORMAT_TIMESPAN(total_usec, USEC_PER_MSEC),
3742
                                       m->soft_reboots_count));
3743
        } else if (MANAGER_IS_SYSTEM(m) && detect_container() <= 0) {
193✔
3744
                char buf[FORMAT_TIMESPAN_MAX + STRLEN(" (firmware) + ") + FORMAT_TIMESPAN_MAX + STRLEN(" (loader) + ")]
3✔
3745
                        = {};
3746
                char *p = buf;
3✔
3747
                size_t size = sizeof buf;
3✔
3748

3749
                /* Note that MANAGER_TIMESTAMP_KERNEL's monotonic value is always at 0, and
3750
                 * MANAGER_TIMESTAMP_FIRMWARE's and MANAGER_TIMESTAMP_LOADER's monotonic value should be considered
3751
                 * negative values. */
3752

3753
                firmware_usec = m->timestamps[MANAGER_TIMESTAMP_FIRMWARE].monotonic - m->timestamps[MANAGER_TIMESTAMP_LOADER].monotonic;
3✔
3754
                loader_usec = m->timestamps[MANAGER_TIMESTAMP_LOADER].monotonic - m->timestamps[MANAGER_TIMESTAMP_KERNEL].monotonic;
3✔
3755
                userspace_usec = m->timestamps[MANAGER_TIMESTAMP_FINISH].monotonic - m->timestamps[MANAGER_TIMESTAMP_USERSPACE].monotonic;
3✔
3756
                total_usec = m->timestamps[MANAGER_TIMESTAMP_FIRMWARE].monotonic + m->timestamps[MANAGER_TIMESTAMP_FINISH].monotonic;
3✔
3757

3758
                if (firmware_usec > 0)
3✔
3759
                        size = strpcpyf(&p, size, "%s (firmware) + ", FORMAT_TIMESPAN(firmware_usec, USEC_PER_MSEC));
×
3760
                if (loader_usec > 0)
3✔
3761
                        size = strpcpyf(&p, size, "%s (loader) + ", FORMAT_TIMESPAN(loader_usec, USEC_PER_MSEC));
×
3762

3763
                if (dual_timestamp_is_set(&m->timestamps[MANAGER_TIMESTAMP_INITRD])) {
3✔
3764

3765
                        /* The initrd case on bare-metal */
3766
                        kernel_usec = m->timestamps[MANAGER_TIMESTAMP_INITRD].monotonic - m->timestamps[MANAGER_TIMESTAMP_KERNEL].monotonic;
×
3767
                        initrd_usec = m->timestamps[MANAGER_TIMESTAMP_USERSPACE].monotonic - m->timestamps[MANAGER_TIMESTAMP_INITRD].monotonic;
×
3768

3769
                        log_struct(LOG_INFO,
×
3770
                                   LOG_MESSAGE_ID(SD_MESSAGE_STARTUP_FINISHED_STR),
3771
                                   LOG_ITEM("KERNEL_USEC="USEC_FMT, kernel_usec),
3772
                                   LOG_ITEM("INITRD_USEC="USEC_FMT, initrd_usec),
3773
                                   LOG_ITEM("USERSPACE_USEC="USEC_FMT, userspace_usec),
3774
                                   LOG_MESSAGE("Startup finished in %s%s (kernel) + %s (initrd) + %s (userspace) = %s.",
3775
                                               buf,
3776
                                               FORMAT_TIMESPAN(kernel_usec, USEC_PER_MSEC),
3777
                                               FORMAT_TIMESPAN(initrd_usec, USEC_PER_MSEC),
3778
                                               FORMAT_TIMESPAN(userspace_usec, USEC_PER_MSEC),
3779
                                               FORMAT_TIMESPAN(total_usec, USEC_PER_MSEC)));
3780
                } else {
3781
                        /* The initrd-less case on bare-metal */
3782

3783
                        kernel_usec = m->timestamps[MANAGER_TIMESTAMP_USERSPACE].monotonic - m->timestamps[MANAGER_TIMESTAMP_KERNEL].monotonic;
3✔
3784
                        initrd_usec = 0;
3✔
3785

3786
                        log_struct(LOG_INFO,
3✔
3787
                                   LOG_MESSAGE_ID(SD_MESSAGE_STARTUP_FINISHED_STR),
3788
                                   LOG_ITEM("KERNEL_USEC="USEC_FMT, kernel_usec),
3789
                                   LOG_ITEM("USERSPACE_USEC="USEC_FMT, userspace_usec),
3790
                                   LOG_MESSAGE("Startup finished in %s%s (kernel) + %s (userspace) = %s.",
3791
                                               buf,
3792
                                               FORMAT_TIMESPAN(kernel_usec, USEC_PER_MSEC),
3793
                                               FORMAT_TIMESPAN(userspace_usec, USEC_PER_MSEC),
3794
                                               FORMAT_TIMESPAN(total_usec, USEC_PER_MSEC)));
3795
                }
3796
        } else {
3797
                /* The container and --user case */
3798
                firmware_usec = loader_usec = initrd_usec = kernel_usec = 0;
187✔
3799
                total_usec = userspace_usec = m->timestamps[MANAGER_TIMESTAMP_FINISH].monotonic - m->timestamps[MANAGER_TIMESTAMP_USERSPACE].monotonic;
187✔
3800

3801
                log_struct(LOG_INFO,
187✔
3802
                           LOG_MESSAGE_ID(SD_MESSAGE_USER_STARTUP_FINISHED_STR),
3803
                           LOG_ITEM("USERSPACE_USEC="USEC_FMT, userspace_usec),
3804
                           LOG_MESSAGE("Startup finished in %s.",
3805
                                       FORMAT_TIMESPAN(total_usec, USEC_PER_MSEC)));
3806
        }
3807

3808
        bus_manager_send_finished(m, firmware_usec, loader_usec, kernel_usec, initrd_usec, userspace_usec, total_usec);
190✔
3809

3810
        if (MANAGER_IS_SYSTEM(m) && detect_container() <= 0)
190✔
3811
                watchdog_report_if_missing();
3✔
3812

3813
        log_taint_string(m);
190✔
3814
}
3815

3816
static void manager_send_ready_on_basic_target(Manager *m) {
3,101✔
3817
        int r;
3,101✔
3818

3819
        assert(m);
3,101✔
3820

3821
        /* We send READY=1 on reaching basic.target only when running in --user mode. */
3822
        if (!MANAGER_IS_USER(m) || m->ready_sent)
3,101✔
3823
                return;
3824

3825
        r = sd_notify(/* unset_environment= */ false,
196✔
3826
                      "READY=1\n"
3827
                      "STATUS=Reached " SPECIAL_BASIC_TARGET ".");
3828
        if (r < 0)
196✔
3829
                log_warning_errno(r, "Failed to send readiness notification, ignoring: %m");
×
3830

3831
        m->ready_sent = true;
196✔
3832
        m->status_ready = false;
196✔
3833
}
3834

3835
static void manager_send_ready_on_idle(Manager *m) {
341✔
3836
        int r;
341✔
3837

3838
        assert(m);
341✔
3839

3840
        /* Skip the notification if nothing changed. */
3841
        if (m->ready_sent && m->status_ready)
341✔
3842
                return;
3843

3844
        /* Note that for user managers, we might have already sent READY=1 in manager_send_ready_user_scope().
3845
         * But we still need to flush STATUS=. The second READY=1 will be treated as a noop so it doesn't
3846
         * hurt to send it twice. */
3847
        r = sd_notify(/* unset_environment= */ false,
200✔
3848
                      "READY=1\n"
3849
                      "STATUS=Ready.");
3850
        if (r < 0)
200✔
3851
                log_full_errno(m->ready_sent ? LOG_DEBUG : LOG_WARNING, r,
×
3852
                               "Failed to send readiness notification, ignoring: %m");
3853

3854
        m->ready_sent = m->status_ready = true;
200✔
3855
}
3856

3857
static void manager_check_basic_target(Manager *m) {
17,539✔
3858
        Unit *u;
17,539✔
3859

3860
        assert(m);
17,539✔
3861

3862
        /* Small shortcut */
3863
        if (m->ready_sent && m->taint_logged)
17,539✔
3864
                return;
3865

3866
        u = manager_get_unit(m, SPECIAL_BASIC_TARGET);
17,311✔
3867
        if (!u || !UNIT_IS_ACTIVE_OR_RELOADING(unit_active_state(u)))
17,311✔
3868
                return;
14,210✔
3869

3870
        /* For user managers, send out READY=1 as soon as we reach basic.target */
3871
        manager_send_ready_on_basic_target(m);
3,101✔
3872

3873
        /* Log the taint string as soon as we reach basic.target */
3874
        log_taint_string(m);
3,101✔
3875
}
3876

3877
void manager_check_finished(Manager *m) {
17,746✔
3878
        assert(m);
17,746✔
3879

3880
        if (MANAGER_IS_RELOADING(m))
17,746✔
3881
                return;
3882

3883
        /* Verify that we have entered the event loop already, and not left it again. */
3884
        if (!MANAGER_IS_RUNNING(m))
17,746✔
3885
                return;
3886

3887
        manager_check_basic_target(m);
17,539✔
3888

3889
        if (!hashmap_isempty(m->jobs)) {
17,539✔
3890
                if (m->jobs_in_progress_event_source)
17,198✔
3891
                        /* Ignore any failure, this is only for feedback */
3892
                        (void) sd_event_source_set_time(m->jobs_in_progress_event_source,
4,220✔
3893
                                                        manager_watch_jobs_next_time(m));
4,220✔
3894
                return;
17,198✔
3895
        }
3896

3897
        /* The jobs hashmap tends to grow a lot during boot, and then it's not reused until shutdown. Let's
3898
           kill the hashmap if it is relatively large. */
3899
        if (hashmap_buckets(m->jobs) > hashmap_size(m->units) / 10)
341✔
3900
                m->jobs = hashmap_free(m->jobs);
199✔
3901

3902
        manager_send_ready_on_idle(m);
341✔
3903

3904
        /* Notify Type=idle units that we are done now */
3905
        manager_close_idle_pipe(m);
341✔
3906

3907
        if (MANAGER_IS_FINISHED(m))
341✔
3908
                return;
3909

3910
        manager_flip_auto_status(m, false, "boot finished");
197✔
3911

3912
        /* Turn off confirm spawn now */
3913
        m->confirm_spawn = NULL;
197✔
3914

3915
        /* No need to update ask password status when we're going non-interactive */
3916
        manager_close_ask_password(m);
197✔
3917

3918
        /* This is no longer the first boot */
3919
        manager_set_first_boot(m, false);
197✔
3920

3921
        dual_timestamp_now(m->timestamps + MANAGER_TIMESTAMP_FINISH);
197✔
3922

3923
        manager_notify_finished(m);
197✔
3924

3925
        manager_invalidate_startup_units(m);
197✔
3926
}
3927

3928
void manager_send_reloading(Manager *m) {
70✔
3929
        assert(m);
70✔
3930

3931
        /* Let whoever invoked us know that we are now reloading */
3932
        (void) notify_reloading_full(/* status= */ NULL);
70✔
3933

3934
        /* And ensure that we'll send READY=1 again as soon as we are ready again */
3935
        m->ready_sent = false;
70✔
3936
}
70✔
3937

3938
static bool generator_path_any(char * const *paths) {
1,016✔
3939

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

3942
        STRV_FOREACH(i, paths) {
4,064✔
3943
                if (access(*i, F_OK) >= 0)
4,064✔
3944
                        return true;
3945
                if (errno != ENOENT)
3,048✔
3946
                        log_warning_errno(errno, "Failed to check if generator dir '%s' exists, assuming not: %m", *i);
3,048✔
3947
        }
3948

3949
        return false;
3950
}
3951

3952
static int manager_run_environment_generators(Manager *m) {
767✔
3953
        _cleanup_strv_free_ char **paths = NULL;
767✔
3954
        int r;
767✔
3955

3956
        assert(m);
767✔
3957

3958
        if (MANAGER_IS_TEST_RUN(m) && !(m->test_run_flags & MANAGER_TEST_RUN_ENV_GENERATORS))
767✔
3959
                return 0;
3960

3961
        paths = env_generator_binary_paths(m->runtime_scope);
742✔
3962
        if (!paths)
742✔
3963
                return log_oom();
×
3964

3965
        if (!generator_path_any(paths))
742✔
3966
                return 0;
3967

3968
        char **tmp = NULL; /* this is only used in the forked process, no cleanup here */
742✔
3969
        void *args[_STDOUT_CONSUME_MAX] = {
742✔
3970
                [STDOUT_GENERATE] = &tmp,
3971
                [STDOUT_COLLECT]  = &tmp,
3972
                [STDOUT_CONSUME]  = &m->transient_environment,
742✔
3973
        };
3974

3975
        WITH_UMASK(0022)
1,484✔
3976
                r = execute_directories(
742✔
3977
                                "environment-generators",
3978
                                (const char* const*) paths,
3979
                                DEFAULT_TIMEOUT_USEC,
3980
                                gather_environment,
3981
                                args,
3982
                                /* argv[]= */ NULL,
3983
                                m->transient_environment,
3984
                                EXEC_DIR_PARALLEL | EXEC_DIR_IGNORE_ERRORS | EXEC_DIR_SET_SYSTEMD_EXEC_PID);
3985
        return r;
742✔
3986
}
3987

3988
static int build_generator_environment(Manager *m, char ***ret) {
190✔
3989
        _cleanup_strv_free_ char **nl = NULL;
190✔
3990
        Virtualization v;
190✔
3991
        ConfidentialVirtualization cv;
190✔
3992
        int r;
190✔
3993

3994
        assert(m);
190✔
3995
        assert(ret);
190✔
3996

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

4001
        nl = strv_copy(m->transient_environment);
190✔
4002
        if (!nl)
190✔
4003
                return -ENOMEM;
4004

4005
        r = strv_env_assign(&nl, "SYSTEMD_SCOPE", runtime_scope_to_string(m->runtime_scope));
190✔
4006
        if (r < 0)
190✔
4007
                return r;
4008

4009
        if (MANAGER_IS_SYSTEM(m)) {
190✔
4010
                /* Note that $SYSTEMD_IN_INITRD may be used to override the initrd detection in much of our
4011
                 * codebase. This is hence more than purely informational. It will shortcut detection of the
4012
                 * initrd state if generators invoke our own tools. But that's OK, as it would come to the
4013
                 * same results (hopefully). */
4014
                r = strv_env_assign(&nl, "SYSTEMD_IN_INITRD", one_zero(in_initrd()));
×
4015
                if (r < 0)
×
4016
                        return r;
4017

4018
                if (m->soft_reboots_count > 0) {
×
4019
                        r = strv_env_assignf(&nl, "SYSTEMD_SOFT_REBOOTS_COUNT", "%u", m->soft_reboots_count);
×
4020
                        if (r < 0)
×
4021
                                return r;
4022
                }
4023

4024
                if (m->first_boot >= 0) {
×
4025
                        r = strv_env_assign(&nl, "SYSTEMD_FIRST_BOOT", one_zero(m->first_boot));
×
4026
                        if (r < 0)
×
4027
                                return r;
4028
                }
4029
        }
4030

4031
        v = detect_virtualization();
190✔
4032
        if (v < 0)
190✔
4033
                log_debug_errno(v, "Failed to detect virtualization, ignoring: %m");
×
4034
        else if (v > 0) {
190✔
4035
                const char *s;
190✔
4036

4037
                s = strjoina(VIRTUALIZATION_IS_VM(v) ? "vm:" :
950✔
4038
                             VIRTUALIZATION_IS_CONTAINER(v) ? "container:" : ":",
4039
                             virtualization_to_string(v));
4040

4041
                r = strv_env_assign(&nl, "SYSTEMD_VIRTUALIZATION", s);
190✔
4042
                if (r < 0)
190✔
4043
                        return r;
4044
        }
4045

4046
        cv = detect_confidential_virtualization();
190✔
4047
        if (cv < 0)
190✔
4048
                log_debug_errno(cv, "Failed to detect confidential virtualization, ignoring: %m");
×
4049
        else if (cv > 0) {
190✔
4050
                r = strv_env_assign(&nl, "SYSTEMD_CONFIDENTIAL_VIRTUALIZATION", confidential_virtualization_to_string(cv));
×
4051
                if (r < 0)
×
4052
                        return r;
4053
        }
4054

4055
        r = strv_env_assign(&nl, "SYSTEMD_ARCHITECTURE", architecture_to_string(uname_architecture()));
190✔
4056
        if (r < 0)
190✔
4057
                return r;
4058

4059
        *ret = TAKE_PTR(nl);
190✔
4060
        return 0;
190✔
4061
}
4062

4063
static int manager_execute_generators(Manager *m, char * const *paths, bool remount_ro) {
190✔
4064
        _cleanup_strv_free_ char **ge = NULL;
190✔
4065
        int r;
190✔
4066

4067
        assert(m);
190✔
4068

4069
        r = build_generator_environment(m, &ge);
190✔
4070
        if (r < 0)
190✔
4071
                return log_error_errno(r, "Failed to build generator environment: %m");
×
4072

4073
        if (remount_ro) {
190✔
4074
                /* Remount most of the filesystem tree read-only. We leave /sys/ as-is, because our code
4075
                 * checks whether it is read-only to detect containerized execution environments. We leave
4076
                 * /run/ as-is too, because that's where our output goes. We also leave /proc/ and /dev/shm/
4077
                 * because they're API, and /tmp/ that safe_fork() mounted for us.
4078
                 */
4079
                r = bind_remount_recursive("/", MS_RDONLY, MS_RDONLY,
×
4080
                                           STRV_MAKE("/sys", "/run", "/proc", "/dev/shm", "/tmp"));
×
4081
                if (r < 0)
×
4082
                        log_warning_errno(r, "Read-only bind remount failed, ignoring: %m");
×
4083
        }
4084

4085
        const char *argv[] = {
190✔
4086
                NULL, /* Leave this empty, execute_directory() will fill something in */
4087
                m->lookup_paths.generator,
190✔
4088
                m->lookup_paths.generator_early,
190✔
4089
                m->lookup_paths.generator_late,
190✔
4090
                NULL,
4091
        };
4092

4093
        BLOCK_WITH_UMASK(0022);
380✔
4094
        return execute_directories(
190✔
4095
                        "generators",
4096
                        (const char* const*) paths,
4097
                        DEFAULT_TIMEOUT_USEC,
4098
                        /* callbacks= */ NULL, /* callback_args= */ NULL,
4099
                        (char**) argv,
4100
                        ge,
4101
                        EXEC_DIR_PARALLEL | EXEC_DIR_IGNORE_ERRORS | EXEC_DIR_SET_SYSTEMD_EXEC_PID | EXEC_DIR_WARN_WORLD_WRITABLE);
4102
}
4103

4104
static int manager_run_generators(Manager *m) {
767✔
4105
        ForkFlags flags = FORK_RESET_SIGNALS | FORK_WAIT | FORK_NEW_MOUNTNS | FORK_MOUNTNS_SLAVE;
767✔
4106
        _cleanup_strv_free_ char **paths = NULL;
767✔
4107
        int r;
767✔
4108

4109
        assert(m);
767✔
4110

4111
        if (MANAGER_IS_TEST_RUN(m) && !(m->test_run_flags & MANAGER_TEST_RUN_GENERATORS))
767✔
4112
                return 0;
4113

4114
        paths = generator_binary_paths(m->runtime_scope);
274✔
4115
        if (!paths)
274✔
4116
                return log_oom();
×
4117

4118
        if (!generator_path_any(paths))
274✔
4119
                return 0;
4120

4121
        r = lookup_paths_mkdir_generator(&m->lookup_paths);
274✔
4122
        if (r < 0) {
274✔
4123
                log_error_errno(r, "Failed to create generator directories: %m");
×
4124
                goto finish;
×
4125
        }
4126

4127
        /* If we are the system manager, we fork and invoke the generators in a sanitized mount namespace. If
4128
         * we are the user manager, let's just execute the generators directly. We might not have the
4129
         * necessary privileges, and the system manager has already mounted /tmp/ and everything else for us.
4130
         */
4131
        if (MANAGER_IS_USER(m)) {
274✔
4132
                r = manager_execute_generators(m, paths, /* remount_ro= */ false);
190✔
4133
                goto finish;
190✔
4134
        }
4135

4136
        /* On some systems /tmp/ doesn't exist, and on some other systems we cannot create it at all. Avoid
4137
         * trying to mount a private tmpfs on it as there's no one size fits all. */
4138
        if (is_dir("/tmp", /* follow= */ false) > 0 && !MANAGER_IS_TEST_RUN(m))
84✔
4139
                flags |= FORK_PRIVATE_TMP;
84✔
4140

4141
        r = pidref_safe_fork("(sd-gens)", flags, /* ret= */ NULL);
84✔
4142
        if (r == 0) {
84✔
4143
                r = manager_execute_generators(m, paths, /* remount_ro= */ true);
×
4144
                _exit(r >= 0 ? EXIT_SUCCESS : EXIT_FAILURE);
×
4145
        }
4146
        if (r < 0) {
84✔
4147
                if (!ERRNO_IS_PRIVILEGE(r) && r != -EINVAL) {
×
4148
                        log_error_errno(r, "Failed to fork off sandboxing environment for executing generators: %m");
×
4149
                        goto finish;
×
4150
                }
4151

4152
                /* Failed to fork with new mount namespace? Maybe, running in a container environment with
4153
                 * seccomp or without capability.
4154
                 *
4155
                 * We also allow -EINVAL to allow running without CLONE_NEWNS.
4156
                 *
4157
                 * Also, when running on non-native userland architecture via systemd-nspawn and
4158
                 * qemu-user-static QEMU-emulator, clone() with CLONE_NEWNS fails with EINVAL, see
4159
                 * https://github.com/systemd/systemd/issues/28901.
4160
                 */
4161
                log_debug_errno(r,
×
4162
                                "Failed to fork off sandboxing environment for executing generators. "
4163
                                "Falling back to execute generators without sandboxing: %m");
4164
                r = manager_execute_generators(m, paths, /* remount_ro= */ false);
×
4165
        }
4166

4167
finish:
84✔
4168
        lookup_paths_trim_generator(&m->lookup_paths);
274✔
4169
        return r;
4170
}
4171

4172
int manager_transient_environment_add(Manager *m, char **plus) {
274✔
4173
        char **a;
274✔
4174

4175
        assert(m);
274✔
4176

4177
        if (strv_isempty(plus))
274✔
4178
                return 0;
274✔
4179

4180
        a = strv_env_merge(m->transient_environment, plus);
84✔
4181
        if (!a)
84✔
4182
                return log_oom();
×
4183

4184
        sanitize_environment(a);
84✔
4185

4186
        return strv_free_and_replace(m->transient_environment, a);
84✔
4187
}
4188

4189
int manager_client_environment_modify(
189✔
4190
                Manager *m,
4191
                char **minus,
4192
                char **plus) {
4193

4194
        char **a = NULL, **b = NULL, **l;
189✔
4195

4196
        assert(m);
189✔
4197

4198
        if (strv_isempty(minus) && strv_isempty(plus))
189✔
4199
                return 0;
4200

4201
        l = m->client_environment;
189✔
4202

4203
        if (!strv_isempty(minus)) {
189✔
4204
                a = strv_env_delete(l, 1, minus);
1✔
4205
                if (!a)
1✔
4206
                        return -ENOMEM;
4207

4208
                l = a;
4209
        }
4210

4211
        if (!strv_isempty(plus)) {
189✔
4212
                b = strv_env_merge(l, plus);
188✔
4213
                if (!b) {
188✔
4214
                        strv_free(a);
×
4215
                        return -ENOMEM;
×
4216
                }
4217

4218
                l = b;
4219
        }
4220

4221
        if (m->client_environment != l)
189✔
4222
                strv_free(m->client_environment);
189✔
4223

4224
        if (a != l)
189✔
4225
                strv_free(a);
188✔
4226
        if (b != l)
189✔
4227
                strv_free(b);
1✔
4228

4229
        m->client_environment = sanitize_environment(l);
189✔
4230
        return 0;
189✔
4231
}
4232

4233
int manager_get_effective_environment(Manager *m, char ***ret) {
22,932✔
4234
        char **l;
22,932✔
4235

4236
        assert(m);
22,932✔
4237
        assert(ret);
22,932✔
4238

4239
        l = strv_env_merge(m->transient_environment, m->client_environment);
22,932✔
4240
        if (!l)
22,932✔
4241
                return -ENOMEM;
4242

4243
        *ret = l;
22,932✔
4244
        return 0;
22,932✔
4245
}
4246

4247
int manager_set_unit_defaults(Manager *m, const UnitDefaults *defaults) {
274✔
4248
        _cleanup_free_ char *label = NULL;
274✔
4249
        struct rlimit *rlimit[_RLIMIT_MAX];
274✔
4250
        int r;
274✔
4251

4252
        assert(m);
274✔
4253
        assert(defaults);
274✔
4254

4255
        if (streq_ptr(defaults->smack_process_label, "/"))
274✔
4256
                label = NULL;
4257
        else  {
4258
                const char *l = defaults->smack_process_label;
274✔
4259
#ifdef SMACK_DEFAULT_PROCESS_LABEL
4260
                if (!l)
4261
                        l = SMACK_DEFAULT_PROCESS_LABEL;
4262
#endif
4263
                if (l) {
274✔
4264
                        label = strdup(l);
×
4265
                        if (!label)
×
4266
                                return -ENOMEM;
4267
                } else
4268
                        label = NULL;
4269
        }
4270

4271
        r = rlimit_copy_all(rlimit, defaults->rlimit);
274✔
4272
        if (r < 0)
274✔
4273
                return r;
4274

4275
        m->defaults.std_output = defaults->std_output;
274✔
4276
        m->defaults.std_error = defaults->std_error;
274✔
4277

4278
        m->defaults.restart_usec = defaults->restart_usec;
274✔
4279
        m->defaults.timeout_start_usec = defaults->timeout_start_usec;
274✔
4280
        m->defaults.timeout_stop_usec = defaults->timeout_stop_usec;
274✔
4281
        m->defaults.timeout_abort_usec = defaults->timeout_abort_usec;
274✔
4282
        m->defaults.timeout_abort_set = defaults->timeout_abort_set;
274✔
4283
        m->defaults.device_timeout_usec = defaults->device_timeout_usec;
274✔
4284

4285
        m->defaults.restrict_suid_sgid = defaults->restrict_suid_sgid;
274✔
4286

4287
        m->defaults.start_limit = defaults->start_limit;
274✔
4288

4289
        m->defaults.memory_accounting = defaults->memory_accounting;
274✔
4290
        m->defaults.io_accounting = defaults->io_accounting;
274✔
4291
        m->defaults.tasks_accounting = defaults->tasks_accounting;
274✔
4292
        m->defaults.ip_accounting = defaults->ip_accounting;
274✔
4293

4294
        m->defaults.tasks_max = defaults->tasks_max;
274✔
4295
        m->defaults.timer_accuracy_usec = defaults->timer_accuracy_usec;
274✔
4296

4297
        m->defaults.oom_policy = defaults->oom_policy;
274✔
4298
        m->defaults.oom_score_adjust = defaults->oom_score_adjust;
274✔
4299
        m->defaults.oom_score_adjust_set = defaults->oom_score_adjust_set;
274✔
4300

4301
        m->defaults.memory_pressure_watch = defaults->memory_pressure_watch;
274✔
4302
        m->defaults.memory_pressure_threshold_usec = defaults->memory_pressure_threshold_usec;
274✔
4303

4304
        free_and_replace(m->defaults.smack_process_label, label);
274✔
4305
        rlimit_free_all(m->defaults.rlimit);
274✔
4306
        memcpy(m->defaults.rlimit, rlimit, sizeof(struct rlimit*) * _RLIMIT_MAX);
274✔
4307

4308
        return 0;
274✔
4309
}
4310

4311
void manager_recheck_dbus(Manager *m) {
81,934✔
4312
        assert(m);
81,934✔
4313

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

4319
        if (MANAGER_IS_RELOADING(m))
81,934✔
4320
                return; /* don't check while we are reloading… */
4321

4322
        if (manager_dbus_is_running(m, false)) {
74,281✔
4323
                (void) bus_init_api(m);
7,136✔
4324

4325
                if (MANAGER_IS_SYSTEM(m))
7,136✔
4326
                        (void) bus_init_system(m);
4,940✔
4327
        } else {
4328
                bus_done_api(m);
67,145✔
4329

4330
                if (MANAGER_IS_SYSTEM(m))
67,145✔
4331
                        bus_done_system(m);
10,487✔
4332
        }
4333
}
4334

4335
static bool manager_journal_is_running(Manager *m) {
17,502✔
4336
        Unit *u;
17,502✔
4337

4338
        assert(m);
17,502✔
4339

4340
        if (MANAGER_IS_TEST_RUN(m))
17,502✔
4341
                return false;
4342

4343
        /* If we are the user manager we can safely assume that the journal is up */
4344
        if (!MANAGER_IS_SYSTEM(m))
17,502✔
4345
                return true;
4346

4347
        /* Check that the socket is not only up, but in RUNNING state */
4348
        u = manager_get_unit(m, SPECIAL_JOURNALD_SOCKET);
16,815✔
4349
        if (!u)
16,815✔
4350
                return false;
4351
        if (SOCKET(u)->state != SOCKET_RUNNING)
16,815✔
4352
                return false;
4353

4354
        /* Similar, check if the daemon itself is fully up, too */
4355
        u = manager_get_unit(m, SPECIAL_JOURNALD_SERVICE);
13,189✔
4356
        if (!u)
13,189✔
4357
                return false;
4358
        if (!UNIT_IS_ACTIVE_OR_RELOADING(unit_active_state(u)) || SERVICE(u)->state == SERVICE_EXITED)
13,189✔
4359
                return false;
480✔
4360

4361
        return true;
4362
}
4363

4364
void disable_printk_ratelimit(void) {
20✔
4365
        /* Disable kernel's printk ratelimit.
4366
         *
4367
         * Logging to /dev/kmsg is most useful during early boot and shutdown, where normal logging
4368
         * mechanisms are not available. The semantics of this sysctl are such that any kernel command-line
4369
         * setting takes precedence. */
4370
        int r;
20✔
4371

4372
        r = sysctl_write("kernel/printk_devkmsg", "on");
20✔
4373
        if (r < 0)
20✔
4374
                log_debug_errno(r, "Failed to set sysctl kernel.printk_devkmsg=on: %m");
×
4375
}
20✔
4376

4377
void manager_recheck_journal(Manager *m) {
81,934✔
4378

4379
        assert(m);
81,934✔
4380

4381
        /* Don't bother with this unless we are in the special situation of being PID 1 */
4382
        if (getpid_cached() != 1)
81,934✔
4383
                return;
4384

4385
        /* Don't check this while we are reloading, things might still change */
4386
        if (MANAGER_IS_RELOADING(m))
22,453✔
4387
                return;
4388

4389
        /* The journal is fully and entirely up? If so, let's permit logging to it, if that's configured. If
4390
         * the journal is down, don't ever log to it, otherwise we might end up deadlocking ourselves as we
4391
         * might trigger an activation ourselves we can't fulfill. */
4392
        log_set_prohibit_ipc(!manager_journal_is_running(m));
14,950✔
4393
        log_open();
14,950✔
4394
}
4395

4396
static ShowStatus manager_get_show_status(Manager *m) {
12,733✔
4397
        assert(m);
12,733✔
4398

4399
        if (MANAGER_IS_USER(m))
12,733✔
4400
                return _SHOW_STATUS_INVALID;
4401

4402
        if (m->show_status_overridden != _SHOW_STATUS_INVALID)
12,732✔
4403
                return m->show_status_overridden;
4404

4405
        return m->show_status;
12,728✔
4406
}
4407

4408
bool manager_get_show_status_on(Manager *m) {
12,731✔
4409
        assert(m);
12,731✔
4410

4411
        return show_status_on(manager_get_show_status(m));
12,731✔
4412
}
4413

4414
static void set_show_status_marker(bool b) {
65✔
4415
        if (b)
65✔
4416
                (void) touch("/run/systemd/show-status");
×
4417
        else
4418
                (void) unlink("/run/systemd/show-status");
65✔
4419
}
65✔
4420

4421
void manager_set_show_status(Manager *m, ShowStatus mode, const char *reason) {
274✔
4422
        assert(m);
274✔
4423
        assert(reason);
274✔
4424
        assert(mode >= 0 && mode < _SHOW_STATUS_MAX);
274✔
4425

4426
        if (MANAGER_IS_USER(m))
274✔
4427
                return;
4428

4429
        if (mode == m->show_status)
84✔
4430
                return;
4431

4432
        if (m->show_status_overridden == _SHOW_STATUS_INVALID) {
55✔
4433
                bool enabled;
55✔
4434

4435
                enabled = show_status_on(mode);
55✔
4436
                log_debug("%s (%s) showing of status (%s).",
110✔
4437
                          enabled ? "Enabling" : "Disabling",
4438
                          strna(show_status_to_string(mode)),
4439
                          reason);
4440

4441
                set_show_status_marker(enabled);
55✔
4442
        }
4443

4444
        m->show_status = mode;
55✔
4445
}
4446

4447
void manager_override_show_status(Manager *m, ShowStatus mode, const char *reason) {
10✔
4448
        assert(m);
10✔
4449
        assert(mode < _SHOW_STATUS_MAX);
10✔
4450

4451
        if (MANAGER_IS_USER(m))
10✔
4452
                return;
4453

4454
        if (mode == m->show_status_overridden)
10✔
4455
                return;
4456

4457
        m->show_status_overridden = mode;
10✔
4458

4459
        if (mode == _SHOW_STATUS_INVALID)
10✔
4460
                mode = m->show_status;
5✔
4461

4462
        log_debug("%s (%s) showing of status (%s).",
15✔
4463
                  m->show_status_overridden != _SHOW_STATUS_INVALID ? "Overriding" : "Restoring",
4464
                  strna(show_status_to_string(mode)),
4465
                  reason);
4466

4467
        set_show_status_marker(show_status_on(mode));
10✔
4468
}
4469

4470
const char* manager_get_confirm_spawn(Manager *m) {
2,565✔
4471
        static int last_errno = 0;
2,565✔
4472
        struct stat st;
2,565✔
4473
        int r;
2,565✔
4474

4475
        assert(m);
2,565✔
4476

4477
        /* Here's the deal: we want to test the validity of the console but don't want
4478
         * PID1 to go through the whole console process which might block. But we also
4479
         * want to warn the user only once if something is wrong with the console so we
4480
         * cannot do the sanity checks after spawning our children. So here we simply do
4481
         * really basic tests to hopefully trap common errors.
4482
         *
4483
         * If the console suddenly disappear at the time our children will really it
4484
         * then they will simply fail to acquire it and a positive answer will be
4485
         * assumed. New children will fall back to /dev/console though.
4486
         *
4487
         * Note: TTYs are devices that can come and go any time, and frequently aren't
4488
         * available yet during early boot (consider a USB rs232 dongle...). If for any
4489
         * reason the configured console is not ready, we fall back to the default
4490
         * console. */
4491

4492
        if (!m->confirm_spawn || path_equal(m->confirm_spawn, "/dev/console"))
2,565✔
4493
                return m->confirm_spawn;
2,565✔
4494

4495
        if (stat(m->confirm_spawn, &st) < 0) {
×
4496
                r = -errno;
×
4497
                goto fail;
×
4498
        }
4499

4500
        if (!S_ISCHR(st.st_mode)) {
×
4501
                r = -ENOTTY;
×
4502
                goto fail;
×
4503
        }
4504

4505
        last_errno = 0;
×
4506
        return m->confirm_spawn;
×
4507

4508
fail:
×
4509
        if (last_errno != r)
×
4510
                last_errno = log_warning_errno(r, "Failed to open %s, using default console: %m", m->confirm_spawn);
×
4511

4512
        return "/dev/console";
4513
}
4514

4515
void manager_set_first_boot(Manager *m, bool b) {
440✔
4516
        assert(m);
440✔
4517

4518
        if (!MANAGER_IS_SYSTEM(m))
440✔
4519
                return;
4520

4521
        if (m->first_boot != (int) b) {
58✔
4522
                if (b)
57✔
4523
                        (void) touch("/run/systemd/first-boot");
20✔
4524
                else
4525
                        (void) unlink("/run/systemd/first-boot");
37✔
4526
        }
4527

4528
        m->first_boot = b;
58✔
4529
}
4530

4531
void manager_disable_confirm_spawn(void) {
×
4532
        (void) touch("/run/systemd/confirm_spawn_disabled");
×
4533
}
×
4534

4535
static bool manager_should_show_status(Manager *m, StatusType type) {
18,141✔
4536
        assert(m);
18,141✔
4537

4538
        if (!MANAGER_IS_SYSTEM(m))
18,141✔
4539
                return false;
4540

4541
        if (m->no_console_output)
8,470✔
4542
                return false;
4543

4544
        if (!IN_SET(manager_state(m), MANAGER_INITIALIZING, MANAGER_STARTING, MANAGER_STOPPING))
8,406✔
4545
                return false;
4546

4547
        /* If we cannot find out the status properly, just proceed. */
4548
        if (type < STATUS_TYPE_EMERGENCY && manager_check_ask_password(m) > 0)
8,406✔
4549
                return false;
4550

4551
        if (type >= STATUS_TYPE_NOTICE && manager_get_show_status(m) != SHOW_STATUS_NO)
8,406✔
4552
                return true;
4553

4554
        return manager_get_show_status_on(m);
8,404✔
4555
}
4556

4557
void manager_status_printf(Manager *m, StatusType type, const char *status, const char *format, ...) {
18,141✔
4558
        va_list ap;
18,141✔
4559

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

4562
        if (m && !manager_should_show_status(m, type))
18,141✔
4563
                return;
18,139✔
4564

4565
        /* XXX We should totally drop the check for ephemeral here
4566
         * and thus effectively make 'Type=idle' pointless. */
4567
        if (type == STATUS_TYPE_EPHEMERAL && m && m->n_on_console > 0)
2✔
4568
                return;
4569

4570
        va_start(ap, format);
2✔
4571
        status_vprintf(status, SHOW_STATUS_ELLIPSIZE|(type == STATUS_TYPE_EPHEMERAL ? SHOW_STATUS_EPHEMERAL : 0), format, ap);
4✔
4572
        va_end(ap);
2✔
4573
}
4574

4575
Set* manager_get_units_needing_mounts_for(Manager *m, const char *path, UnitMountDependencyType t) {
19,650✔
4576
        assert(m);
19,650✔
4577
        assert(path);
19,650✔
4578
        assert(t >= 0 && t < _UNIT_MOUNT_DEPENDENCY_TYPE_MAX);
19,650✔
4579

4580
        if (path_equal(path, "/"))
19,650✔
4581
                path = "";
878✔
4582

4583
        return hashmap_get(m->units_needing_mounts_for[t], path);
19,650✔
4584
}
4585

4586
int manager_update_failed_units(Manager *m, Unit *u, bool failed) {
172,032✔
4587
        unsigned size;
172,032✔
4588
        int r;
172,032✔
4589

4590
        assert(m);
172,032✔
4591
        assert(u->manager == m);
172,032✔
4592

4593
        size = set_size(m->failed_units);
172,032✔
4594

4595
        if (failed) {
172,032✔
4596
                r = set_ensure_put(&m->failed_units, NULL, u);
278✔
4597
                if (r < 0)
278✔
4598
                        return log_oom();
×
4599
        } else
4600
                (void) set_remove(m->failed_units, u);
171,754✔
4601

4602
        if (set_size(m->failed_units) != size)
172,032✔
4603
                bus_manager_send_change_signal(m);
556✔
4604

4605
        return 0;
4606
}
4607

4608
ManagerState manager_state(Manager *m) {
515,831✔
4609
        Unit *u;
515,831✔
4610

4611
        assert(m);
515,831✔
4612

4613
        /* Is the special shutdown target active or queued? If so, we are in shutdown state */
4614
        u = manager_get_unit(m, SPECIAL_SHUTDOWN_TARGET);
515,831✔
4615
        if (u && unit_active_or_pending(u))
515,831✔
4616
                return MANAGER_STOPPING;
4617

4618
        /* Did we ever finish booting? If not then we are still starting up */
4619
        if (!MANAGER_IS_FINISHED(m)) {
400,696✔
4620

4621
                u = manager_get_unit(m, SPECIAL_BASIC_TARGET);
370,795✔
4622
                if (!u || !UNIT_IS_ACTIVE_OR_RELOADING(unit_active_state(u)))
370,795✔
4623
                        return MANAGER_INITIALIZING;
300,536✔
4624

4625
                return MANAGER_STARTING;
4626
        }
4627

4628
        if (MANAGER_IS_SYSTEM(m)) {
29,901✔
4629
                /* Are the rescue or emergency targets active or queued? If so we are in maintenance state */
4630
                u = manager_get_unit(m, SPECIAL_RESCUE_TARGET);
110✔
4631
                if (u && unit_active_or_pending(u))
110✔
4632
                        return MANAGER_MAINTENANCE;
4633

4634
                u = manager_get_unit(m, SPECIAL_EMERGENCY_TARGET);
110✔
4635
                if (u && unit_active_or_pending(u))
110✔
4636
                        return MANAGER_MAINTENANCE;
4637
        }
4638

4639
        /* Are there any failed units or ordering cycles? If so, we are in degraded mode */
4640
        if (!set_isempty(m->failed_units) || !set_isempty(m->transactions_with_cycle))
29,901✔
4641
                return MANAGER_DEGRADED;
2,761✔
4642

4643
        return MANAGER_RUNNING;
4644
}
4645

4646
static void manager_unref_uid_internal(
908✔
4647
                Hashmap *uid_refs,
4648
                uid_t uid,
4649
                bool destroy_now,
4650
                int (*_clean_ipc)(uid_t uid)) {
4651

4652
        uint32_t c, n;
908✔
4653

4654
        assert(uid_is_valid(uid));
908✔
4655
        assert(_clean_ipc);
908✔
4656

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

4666
        assert_cc(sizeof(uid_t) == sizeof(gid_t));
908✔
4667
        assert_cc(UID_INVALID == (uid_t) GID_INVALID);
908✔
4668

4669
        if (uid == 0) /* We don't keep track of root, and will never destroy it */
908✔
4670
                return;
4671

4672
        c = PTR_TO_UINT32(hashmap_get(uid_refs, UID_TO_PTR(uid)));
681✔
4673

4674
        n = c & ~DESTROY_IPC_FLAG;
681✔
4675
        assert(n > 0);
681✔
4676
        n--;
681✔
4677

4678
        if (destroy_now && n == 0) {
681✔
4679
                hashmap_remove(uid_refs, UID_TO_PTR(uid));
135✔
4680

4681
                if (c & DESTROY_IPC_FLAG) {
135✔
4682
                        log_debug("%s " UID_FMT " is no longer referenced, cleaning up its IPC.",
9✔
4683
                                  _clean_ipc == clean_ipc_by_uid ? "UID" : "GID",
4684
                                  uid);
4685
                        (void) _clean_ipc(uid);
6✔
4686
                }
4687
        } else {
4688
                c = n | (c & DESTROY_IPC_FLAG);
546✔
4689
                assert_se(hashmap_update(uid_refs, UID_TO_PTR(uid), UINT32_TO_PTR(c)) >= 0);
546✔
4690
        }
4691
}
4692

4693
void manager_unref_uid(Manager *m, uid_t uid, bool destroy_now) {
452✔
4694
        manager_unref_uid_internal(m->uid_refs, uid, destroy_now, clean_ipc_by_uid);
452✔
4695
}
452✔
4696

4697
void manager_unref_gid(Manager *m, gid_t gid, bool destroy_now) {
456✔
4698
        manager_unref_uid_internal(m->gid_refs, (uid_t) gid, destroy_now, clean_ipc_by_gid);
456✔
4699
}
456✔
4700

4701
static int manager_ref_uid_internal(
908✔
4702
                Hashmap **uid_refs,
4703
                uid_t uid,
4704
                bool clean_ipc) {
4705

4706
        uint32_t c, n;
908✔
4707
        int r;
908✔
4708

4709
        assert(uid_refs);
908✔
4710
        assert(uid_is_valid(uid));
908✔
4711

4712
        /* A generic implementation, covering both manager_ref_uid() and manager_ref_gid(), under the
4713
         * assumption that uid_t and gid_t are actually defined the same way, with the same validity
4714
         * rules. */
4715

4716
        assert_cc(sizeof(uid_t) == sizeof(gid_t));
908✔
4717
        assert_cc(UID_INVALID == (uid_t) GID_INVALID);
908✔
4718

4719
        if (uid == 0) /* We don't keep track of root, and will never destroy it */
908✔
4720
                return 0;
4721

4722
        r = hashmap_ensure_allocated(uid_refs, &trivial_hash_ops);
681✔
4723
        if (r < 0)
681✔
4724
                return r;
4725

4726
        c = PTR_TO_UINT32(hashmap_get(*uid_refs, UID_TO_PTR(uid)));
681✔
4727

4728
        n = c & ~DESTROY_IPC_FLAG;
681✔
4729
        n++;
681✔
4730

4731
        if (n & DESTROY_IPC_FLAG) /* check for overflow */
681✔
4732
                return -EOVERFLOW;
4733

4734
        c = n | (c & DESTROY_IPC_FLAG) | (clean_ipc ? DESTROY_IPC_FLAG : 0);
681✔
4735

4736
        return hashmap_replace(*uid_refs, UID_TO_PTR(uid), UINT32_TO_PTR(c));
681✔
4737
}
4738

4739
int manager_ref_uid(Manager *m, uid_t uid, bool clean_ipc) {
452✔
4740
        return manager_ref_uid_internal(&m->uid_refs, uid, clean_ipc);
452✔
4741
}
4742

4743
int manager_ref_gid(Manager *m, gid_t gid, bool clean_ipc) {
456✔
4744
        return manager_ref_uid_internal(&m->gid_refs, (uid_t) gid, clean_ipc);
456✔
4745
}
4746

4747
static void manager_vacuum_uid_refs_internal(
1,534✔
4748
                Hashmap *uid_refs,
4749
                int (*_clean_ipc)(uid_t uid)) {
4750

4751
        void *p, *k;
1,534✔
4752

4753
        assert(_clean_ipc);
1,534✔
4754

4755
        HASHMAP_FOREACH_KEY(p, k, uid_refs) {
3,334✔
4756
                uint32_t c, n;
266✔
4757
                uid_t uid;
266✔
4758

4759
                uid = PTR_TO_UID(k);
266✔
4760
                c = PTR_TO_UINT32(p);
266✔
4761

4762
                n = c & ~DESTROY_IPC_FLAG;
266✔
4763
                if (n > 0)
266✔
4764
                        continue;
266✔
4765

4766
                if (c & DESTROY_IPC_FLAG) {
×
4767
                        log_debug("Found unreferenced %s " UID_FMT " after reload/reexec. Cleaning up.",
×
4768
                                  _clean_ipc == clean_ipc_by_uid ? "UID" : "GID",
4769
                                  uid);
4770
                        (void) _clean_ipc(uid);
×
4771
                }
4772

4773
                assert_se(hashmap_remove(uid_refs, k) == p);
1,800✔
4774
        }
4775
}
1,534✔
4776

4777
static void manager_vacuum_uid_refs(Manager *m) {
767✔
4778
        manager_vacuum_uid_refs_internal(m->uid_refs, clean_ipc_by_uid);
767✔
4779
}
767✔
4780

4781
static void manager_vacuum_gid_refs(Manager *m) {
767✔
4782
        manager_vacuum_uid_refs_internal(m->gid_refs, clean_ipc_by_gid);
767✔
4783
}
767✔
4784

4785
static void manager_vacuum(Manager *m) {
767✔
4786
        assert(m);
767✔
4787

4788
        /* Release any dynamic users no longer referenced */
4789
        dynamic_user_vacuum(m, true);
767✔
4790

4791
        /* Release any references to UIDs/GIDs no longer referenced, and destroy any IPC owned by them */
4792
        manager_vacuum_uid_refs(m);
767✔
4793
        manager_vacuum_gid_refs(m);
767✔
4794

4795
        /* Release any runtimes no longer referenced */
4796
        exec_shared_runtime_vacuum(m);
767✔
4797
}
767✔
4798

4799
static int manager_dispatch_user_lookup_fd(sd_event_source *source, int fd, uint32_t revents, void *userdata) {
257✔
4800
        struct buffer {
257✔
4801
                uid_t uid;
4802
                gid_t gid;
4803
                char unit_name[UNIT_NAME_MAX+1];
4804
        } _packed_ buffer;
4805

4806
        Manager *m = ASSERT_PTR(userdata);
257✔
4807
        ssize_t l;
257✔
4808
        size_t n;
257✔
4809
        Unit *u;
257✔
4810

4811
        assert(source);
257✔
4812

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

4818
        l = recv(fd, &buffer, sizeof(buffer), MSG_DONTWAIT);
257✔
4819
        if (l < 0) {
257✔
4820
                if (ERRNO_IS_TRANSIENT(errno))
×
4821
                        return 0;
257✔
4822

4823
                return log_error_errno(errno, "Failed to read from user lookup fd: %m");
×
4824
        }
4825

4826
        if ((size_t) l <= offsetof(struct buffer, unit_name)) {
257✔
4827
                log_warning("Received too short user lookup message, ignoring.");
×
4828
                return 0;
×
4829
        }
4830

4831
        if ((size_t) l > offsetof(struct buffer, unit_name) + UNIT_NAME_MAX) {
257✔
4832
                log_warning("Received too long user lookup message, ignoring.");
×
4833
                return 0;
×
4834
        }
4835

4836
        if (!uid_is_valid(buffer.uid) && !gid_is_valid(buffer.gid)) {
257✔
4837
                log_warning("Got user lookup message with invalid UID/GID pair, ignoring.");
×
4838
                return 0;
×
4839
        }
4840

4841
        n = (size_t) l - offsetof(struct buffer, unit_name);
257✔
4842
        if (memchr(buffer.unit_name, 0, n)) {
257✔
4843
                log_warning("Received lookup message with embedded NUL character, ignoring.");
×
4844
                return 0;
×
4845
        }
4846

4847
        buffer.unit_name[n] = 0;
257✔
4848
        u = manager_get_unit(m, buffer.unit_name);
257✔
4849
        if (!u) {
257✔
4850
                log_debug("Got user lookup message but unit doesn't exist, ignoring.");
×
4851
                return 0;
×
4852
        }
4853

4854
        log_unit_debug(u, "User lookup succeeded: uid=" UID_FMT " gid=" GID_FMT, buffer.uid, buffer.gid);
257✔
4855

4856
        unit_notify_user_lookup(u, buffer.uid, buffer.gid);
257✔
4857
        return 0;
4858
}
4859

4860
static int manager_dispatch_handoff_timestamp_fd(sd_event_source *source, int fd, uint32_t revents, void *userdata) {
2,537✔
4861
        Manager *m = ASSERT_PTR(userdata);
2,537✔
4862
        usec_t ts[2] = {};
2,537✔
4863
        CMSG_BUFFER_TYPE(CMSG_SPACE(sizeof(struct ucred))) control;
2,537✔
4864
        struct msghdr msghdr = {
2,537✔
4865
                .msg_iov = &IOVEC_MAKE(ts, sizeof(ts)),
2,537✔
4866
                .msg_iovlen = 1,
4867
                .msg_control = &control,
4868
                .msg_controllen = sizeof(control),
4869
        };
4870
        ssize_t n;
2,537✔
4871

4872
        assert(source);
2,537✔
4873

4874
        n = recvmsg_safe(m->handoff_timestamp_fds[0], &msghdr, MSG_DONTWAIT|MSG_CMSG_CLOEXEC);
2,537✔
4875
        if (ERRNO_IS_NEG_TRANSIENT(n))
2,537✔
4876
                return 0; /* Spurious wakeup, try again */
2,537✔
4877
        if (n == -ECHRNG) {
2,537✔
4878
                log_warning_errno(n, "Got message with truncated control data (unexpected fds sent?), ignoring.");
×
4879
                return 0;
×
4880
        }
4881
        if (n == -EXFULL) {
2,537✔
4882
                log_warning_errno(n, "Got message with truncated payload data, ignoring.");
×
4883
                return 0;
×
4884
        }
4885
        if (n < 0)
2,537✔
4886
                return log_error_errno(n, "Failed to receive handoff timestamp message: %m");
×
4887

4888
        cmsg_close_all(&msghdr);
2,537✔
4889

4890
        if (n != sizeof(ts)) {
2,537✔
4891
                log_warning("Got handoff timestamp message of unexpected size %zi (expected %zu), ignoring.", n, sizeof(ts));
×
4892
                return 0;
×
4893
        }
4894

4895
        struct ucred *ucred = CMSG_FIND_DATA(&msghdr, SOL_SOCKET, SCM_CREDENTIALS, struct ucred);
2,537✔
4896
        if (!ucred || !pid_is_valid(ucred->pid)) {
2,537✔
4897
                log_warning("Received handoff timestamp message without valid credentials. Ignoring.");
×
4898
                return 0;
×
4899
        }
4900

4901
        log_debug("Got handoff timestamp event for PID " PID_FMT ".", ucred->pid);
2,537✔
4902

4903
        _cleanup_free_ Unit **units = NULL;
2,537✔
4904
        int n_units = manager_get_units_for_pidref(m, &PIDREF_MAKE_FROM_PID(ucred->pid), &units);
2,537✔
4905
        if (n_units < 0) {
2,537✔
4906
                log_warning_errno(n_units, "Unable to determine units for PID " PID_FMT ", ignoring: %m", ucred->pid);
×
4907
                return 0;
×
4908
        }
4909
        if (n_units == 0) {
2,537✔
4910
                log_debug("Got handoff timestamp for process " PID_FMT " we are not interested in, ignoring.", ucred->pid);
×
4911
                return 0;
×
4912
        }
4913

4914
        dual_timestamp dt = {
2,537✔
4915
                .realtime = ts[0],
2,537✔
4916
                .monotonic = ts[1],
2,537✔
4917
        };
4918

4919
        FOREACH_ARRAY(u, units, n_units) {
7,611✔
4920
                if (!UNIT_VTABLE(*u)->notify_handoff_timestamp)
5,074✔
4921
                        continue;
95✔
4922

4923
                UNIT_VTABLE(*u)->notify_handoff_timestamp(*u, ucred, &dt);
4,979✔
4924
        }
4925

4926
        return 0;
4927
}
4928

4929
static int manager_dispatch_pidref_transport_fd(sd_event_source *source, int fd, uint32_t revents, void *userdata) {
3✔
4930
        Manager *m = ASSERT_PTR(userdata);
3✔
4931
        _cleanup_(pidref_done) PidRef child_pidref = PIDREF_NULL, parent_pidref = PIDREF_NULL;
3✔
4932
        _cleanup_close_ int child_pidfd = -EBADF, parent_pidfd = -EBADF;
6✔
4933
        struct ucred *ucred = NULL;
3✔
4934
        CMSG_BUFFER_TYPE(CMSG_SPACE(sizeof(struct ucred)) + CMSG_SPACE(sizeof(int)) * 2) control;
3✔
4935
        pid_t child_pid = 0; /* silence false-positive warning by coverity */
3✔
4936
        struct msghdr msghdr = {
3✔
4937
                .msg_iov = &IOVEC_MAKE(&child_pid, sizeof(child_pid)),
3✔
4938
                .msg_iovlen = 1,
4939
                .msg_control = &control,
4940
                .msg_controllen = sizeof(control),
4941
        };
4942
        struct cmsghdr *cmsg;
3✔
4943
        ssize_t n;
3✔
4944
        int r;
3✔
4945

4946
        assert(source);
3✔
4947

4948
        /* Server expects:
4949
         * - Parent PID in ucreds enabled via SO_PASSCRED
4950
         * - Parent PIDFD in SCM_PIDFD message enabled via SO_PASSPIDFD
4951
         * - Child PIDFD in SCM_RIGHTS in message body
4952
         * - Child PID in message IOV
4953
         *
4954
         * SO_PASSPIDFD may not be supported by the kernel (it is supported since v6.5) so we fall back to
4955
         * using parent PID from ucreds and accept some raciness. */
4956
        n = recvmsg_safe(m->pidref_transport_fds[0], &msghdr, MSG_DONTWAIT|MSG_CMSG_CLOEXEC|MSG_TRUNC);
3✔
4957
        if (ERRNO_IS_NEG_TRANSIENT(n))
6✔
4958
                return 0; /* Spurious wakeup, try again */
4959
        if (n == -ECHRNG) {
3✔
4960
                log_warning_errno(n, "Got message with truncated control data (unexpected fds sent?), ignoring.");
×
4961
                return 0;
×
4962
        }
4963
        if (n == -EXFULL) {
3✔
4964
                log_warning_errno(n, "Got message with truncated payload data, ignoring.");
×
4965
                return 0;
×
4966
        }
4967
        if (n < 0)
3✔
4968
                return log_error_errno(n, "Failed to receive pidref message: %m");
×
4969

4970
        if (n != sizeof(child_pid)) {
3✔
4971
                log_warning("Got pidref message of unexpected size %zi (expected %zu), ignoring.", n, sizeof(child_pid));
×
4972
                return 0;
×
4973
        }
4974

4975
        CMSG_FOREACH(cmsg, &msghdr) {
24✔
4976
                if (cmsg->cmsg_level != SOL_SOCKET)
9✔
4977
                        continue;
×
4978

4979
                if (cmsg->cmsg_type == SCM_CREDENTIALS && cmsg->cmsg_len == CMSG_LEN(sizeof(struct ucred))) {
9✔
4980
                        assert(!ucred);
3✔
4981
                        ucred = CMSG_TYPED_DATA(cmsg, struct ucred);
3✔
4982
                } else if (cmsg->cmsg_type == SCM_PIDFD) {
6✔
4983
                        assert(parent_pidfd < 0);
3✔
4984
                        parent_pidfd = *CMSG_TYPED_DATA(cmsg, int);
3✔
4985
                } else if (cmsg->cmsg_type == SCM_RIGHTS) {
3✔
4986
                        assert(child_pidfd < 0);
3✔
4987
                        child_pidfd = *CMSG_TYPED_DATA(cmsg, int);
3✔
4988
                }
4989
        }
4990

4991
        /* Verify and set parent pidref. */
4992
        if (!ucred || !pid_is_valid(ucred->pid)) {
3✔
4993
                log_warning("Received pidref message without valid credentials. Ignoring.");
×
4994
                return 0;
×
4995
        }
4996

4997
        /* Need to handle kernels without SO_PASSPIDFD where SCM_PIDFD will not be set. */
4998
        if (parent_pidfd >= 0)
3✔
4999
                r = pidref_set_pidfd_consume(&parent_pidref, TAKE_FD(parent_pidfd));
3✔
5000
        else
5001
                r = pidref_set_pid(&parent_pidref, ucred->pid);
×
5002
        if (r < 0) {
3✔
5003
                if (r == -ESRCH)
×
5004
                        log_debug_errno(r, "PidRef child process died before message is processed. Ignoring.");
×
5005
                else
5006
                        log_warning_errno(r, "Failed to pin pidref child process, ignoring message: %m");
×
5007
                return 0;
×
5008
        }
5009

5010
        if (parent_pidref.pid != ucred->pid) {
3✔
5011
                assert(parent_pidref.fd >= 0);
×
5012
                log_warning("Got SCM_PIDFD for parent process " PID_FMT " but got SCM_CREDENTIALS for parent process " PID_FMT ". Ignoring.",
×
5013
                            parent_pidref.pid, ucred->pid);
5014
                return 0;
×
5015
        }
5016

5017
        /* Verify and set child pidref. */
5018
        if (!pid_is_valid(child_pid)) {
3✔
5019
                log_warning("Received pidref message without valid child PID. Ignoring.");
×
5020
                return 0;
×
5021
        }
5022

5023
        /* Need to handle kernels without PIDFD support. */
5024
        if (child_pidfd >= 0)
3✔
5025
                r = pidref_set_pidfd_consume(&child_pidref, TAKE_FD(child_pidfd));
3✔
5026
        else
5027
                r = pidref_set_pid(&child_pidref, child_pid);
×
5028
        if (r < 0) {
3✔
5029
                if (r == -ESRCH)
×
5030
                        log_debug_errno(r, "PidRef child process died before message is processed. Ignoring.");
×
5031
                else
5032
                        log_warning_errno(r, "Failed to pin pidref child process, ignoring message: %m");
×
5033
                return 0;
×
5034
        }
5035

5036
        if (child_pidref.pid != child_pid) {
3✔
5037
                assert(child_pidref.fd >= 0);
×
5038
                log_warning("Got SCM_RIGHTS for child process " PID_FMT " but PID in IOV message is " PID_FMT ". Ignoring.",
×
5039
                            child_pidref.pid, child_pid);
5040
                return 0;
×
5041
        }
5042

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

5045
        /* Try finding cgroup of parent process. But if parent process exited and we're not using PIDFD, this could return NULL.
5046
         * Then fall back to finding cgroup of the child process. */
5047
        Unit *u = manager_get_unit_by_pidref_cgroup(m, &parent_pidref);
3✔
5048
        if (!u)
3✔
5049
                u = manager_get_unit_by_pidref_cgroup(m, &child_pidref);
×
5050
        if (!u) {
×
5051
                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);
×
5052
                return 0;
×
5053
        }
5054

5055
        if (!UNIT_VTABLE(u)->notify_pidref) {
3✔
5056
                log_unit_warning(u, "Received pidref event from unexpected unit type '%s'.", unit_type_to_string(u->type));
×
5057
                return 0;
×
5058
        }
5059

5060
        UNIT_VTABLE(u)->notify_pidref(u, &parent_pidref, &child_pidref);
3✔
5061

5062
        return 0;
5063
}
5064

5065
void manager_ref_console(Manager *m) {
83✔
5066
        assert(m);
83✔
5067

5068
        m->n_on_console++;
83✔
5069
}
83✔
5070

5071
void manager_unref_console(Manager *m) {
83✔
5072

5073
        assert(m->n_on_console > 0);
83✔
5074
        m->n_on_console--;
83✔
5075

5076
        if (m->n_on_console == 0)
83✔
5077
                m->no_console_output = false; /* unset no_console_output flag, since the console is definitely free now */
69✔
5078
}
83✔
5079

5080
void manager_override_log_level(Manager *m, int level) {
4✔
5081
        _cleanup_free_ char *s = NULL;
8✔
5082
        assert(m);
4✔
5083

5084
        if (!m->log_level_overridden) {
4✔
5085
                m->original_log_level = log_get_max_level();
1✔
5086
                m->log_level_overridden = true;
1✔
5087
        }
5088

5089
        (void) log_level_to_string_alloc(level, &s);
4✔
5090
        log_info("Setting log level to %s.", strna(s));
4✔
5091

5092
        log_set_max_level(level);
4✔
5093
}
4✔
5094

5095
void manager_restore_original_log_level(Manager *m) {
×
5096
        _cleanup_free_ char *s = NULL;
×
5097
        assert(m);
×
5098

5099
        if (!m->log_level_overridden)
×
5100
                return;
×
5101

5102
        (void) log_level_to_string_alloc(m->original_log_level, &s);
×
5103
        log_info("Restoring log level to original (%s).", strna(s));
×
5104

5105
        log_set_max_level(m->original_log_level);
×
5106
        m->log_level_overridden = false;
×
5107
}
5108

5109
void manager_override_log_target(Manager *m, LogTarget target) {
4✔
5110
        assert(m);
4✔
5111

5112
        if (!m->log_target_overridden) {
4✔
5113
                m->original_log_target = log_get_target();
1✔
5114
                m->log_target_overridden = true;
1✔
5115
        }
5116

5117
        log_info("Setting log target to %s.", log_target_to_string(target));
4✔
5118
        log_set_target(target);
4✔
5119
}
4✔
5120

5121
void manager_restore_original_log_target(Manager *m) {
×
5122
        assert(m);
×
5123

5124
        if (!m->log_target_overridden)
×
5125
                return;
5126

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

5129
        log_set_target(m->original_log_target);
×
5130
        m->log_target_overridden = false;
×
5131
}
5132

5133
ManagerTimestamp manager_timestamp_initrd_mangle(ManagerTimestamp s) {
3,430✔
5134
        if (in_initrd() &&
3,430✔
5135
            s >= MANAGER_TIMESTAMP_SECURITY_START &&
66✔
5136
            s <= MANAGER_TIMESTAMP_UNITS_LOAD_FINISH)
5137
                return s - MANAGER_TIMESTAMP_SECURITY_START + MANAGER_TIMESTAMP_INITRD_SECURITY_START;
66✔
5138
        return s;
5139
}
5140

5141
int manager_allocate_idle_pipe(Manager *m) {
1,777✔
5142
        int r;
1,777✔
5143

5144
        assert(m);
1,777✔
5145

5146
        if (m->idle_pipe[0] >= 0) {
1,777✔
5147
                assert(m->idle_pipe[1] >= 0);
751✔
5148
                assert(m->idle_pipe[2] >= 0);
751✔
5149
                assert(m->idle_pipe[3] >= 0);
751✔
5150
                return 0;
5151
        }
5152

5153
        assert(m->idle_pipe[1] < 0);
1,026✔
5154
        assert(m->idle_pipe[2] < 0);
1,026✔
5155
        assert(m->idle_pipe[3] < 0);
1,026✔
5156

5157
        r = RET_NERRNO(pipe2(m->idle_pipe + 0, O_NONBLOCK|O_CLOEXEC));
1,026✔
5158
        if (r < 0)
×
5159
                return r;
5160

5161
        r = RET_NERRNO(pipe2(m->idle_pipe + 2, O_NONBLOCK|O_CLOEXEC));
1,026✔
5162
        if (r < 0) {
×
5163
                safe_close_pair(m->idle_pipe + 0);
×
5164
                return r;
×
5165
        }
5166

5167
        return 1;
5168
}
5169

5170
void unit_defaults_init(UnitDefaults *defaults, RuntimeScope scope) {
1,199✔
5171
        assert(defaults);
1,199✔
5172
        assert(scope >= 0);
1,199✔
5173
        assert(scope < _RUNTIME_SCOPE_MAX);
1,199✔
5174

5175
        *defaults = (UnitDefaults) {
2,398✔
5176
                .std_output = EXEC_OUTPUT_JOURNAL,
5177
                .std_error = EXEC_OUTPUT_INHERIT,
5178
                .restart_usec = DEFAULT_RESTART_USEC,
5179
                .timeout_start_usec = manager_default_timeout(scope),
1,199✔
5180
                .timeout_stop_usec = manager_default_timeout(scope),
1,199✔
5181
                .timeout_abort_usec = manager_default_timeout(scope),
1,199✔
5182
                .timeout_abort_set = false,
5183
                .device_timeout_usec = manager_default_timeout(scope),
1,199✔
5184
                .start_limit = { DEFAULT_START_LIMIT_INTERVAL, DEFAULT_START_LIMIT_BURST },
5185

5186
                .memory_accounting = MEMORY_ACCOUNTING_DEFAULT,
5187
                .io_accounting = false,
5188
                .tasks_accounting = true,
5189
                .ip_accounting = false,
5190

5191
                .tasks_max = DEFAULT_TASKS_MAX,
5192
                .timer_accuracy_usec = 1 * USEC_PER_MINUTE,
5193

5194
                .memory_pressure_watch = CGROUP_PRESSURE_WATCH_AUTO,
5195
                .memory_pressure_threshold_usec = MEMORY_PRESSURE_DEFAULT_THRESHOLD_USEC,
5196

5197
                .oom_policy = OOM_STOP,
5198
                .oom_score_adjust_set = false,
5199
        };
5200
}
1,199✔
5201

5202
void unit_defaults_done(UnitDefaults *defaults) {
1,199✔
5203
        assert(defaults);
1,199✔
5204

5205
        defaults->smack_process_label = mfree(defaults->smack_process_label);
1,199✔
5206
        rlimit_free_all(defaults->rlimit);
1,199✔
5207
}
1,199✔
5208

5209
LogTarget manager_get_executor_log_target(Manager *m) {
2,563✔
5210
        assert(m);
2,563✔
5211

5212
        /* If journald is not available tell sd-executor to go to kmsg, as it might be starting journald */
5213
        if (!MANAGER_IS_TEST_RUN(m) && !manager_journal_is_running(m))
2,563✔
5214
                return LOG_TARGET_KMSG;
5215

5216
        return log_get_target();
2,093✔
5217
}
5218

5219
void manager_log_caller(Manager *manager, PidRef *caller, const char *method) {
63✔
5220
        _cleanup_free_ char *comm = NULL;
126✔
5221

5222
        assert(manager);
63✔
5223
        assert(pidref_is_set(caller));
63✔
5224
        assert(method);
63✔
5225

5226
        (void) pidref_get_comm(caller, &comm);
63✔
5227
        Unit *caller_unit = manager_get_unit_by_pidref(manager, caller);
63✔
5228

5229
        log_notice("%s requested from client PID " PID_FMT "%s%s%s%s%s%s...",
63✔
5230
                   method, caller->pid,
5231
                   comm ? " ('" : "", strempty(comm), comm ? "')" : "",
5232
                   caller_unit ? " (unit " : "", caller_unit ? caller_unit->id : "", caller_unit ? ")" : "");
5233
}
63✔
5234

5235
static const char* const manager_state_table[_MANAGER_STATE_MAX] = {
5236
        [MANAGER_INITIALIZING] = "initializing",
5237
        [MANAGER_STARTING]     = "starting",
5238
        [MANAGER_RUNNING]      = "running",
5239
        [MANAGER_DEGRADED]     = "degraded",
5240
        [MANAGER_MAINTENANCE]  = "maintenance",
5241
        [MANAGER_STOPPING]     = "stopping",
5242
};
5243

5244
DEFINE_STRING_TABLE_LOOKUP(manager_state, ManagerState);
82✔
5245

5246
static const char* const manager_objective_table[_MANAGER_OBJECTIVE_MAX] = {
5247
        [MANAGER_OK]          = "ok",
5248
        [MANAGER_EXIT]        = "exit",
5249
        [MANAGER_RELOAD]      = "reload",
5250
        [MANAGER_REEXECUTE]   = "reexecute",
5251
        [MANAGER_REBOOT]      = "reboot",
5252
        [MANAGER_SOFT_REBOOT] = "soft-reboot",
5253
        [MANAGER_POWEROFF]    = "poweroff",
5254
        [MANAGER_HALT]        = "halt",
5255
        [MANAGER_KEXEC]       = "kexec",
5256
        [MANAGER_SWITCH_ROOT] = "switch-root",
5257
};
5258

5259
DEFINE_STRING_TABLE_LOOKUP(manager_objective, ManagerObjective);
141✔
5260

5261
static const char* const manager_timestamp_table[_MANAGER_TIMESTAMP_MAX] = {
5262
        [MANAGER_TIMESTAMP_FIRMWARE]                 = "firmware",
5263
        [MANAGER_TIMESTAMP_LOADER]                   = "loader",
5264
        [MANAGER_TIMESTAMP_KERNEL]                   = "kernel",
5265
        [MANAGER_TIMESTAMP_INITRD]                   = "initrd",
5266
        [MANAGER_TIMESTAMP_USERSPACE]                = "userspace",
5267
        [MANAGER_TIMESTAMP_FINISH]                   = "finish",
5268
        [MANAGER_TIMESTAMP_SECURITY_START]           = "security-start",
5269
        [MANAGER_TIMESTAMP_SECURITY_FINISH]          = "security-finish",
5270
        [MANAGER_TIMESTAMP_GENERATORS_START]         = "generators-start",
5271
        [MANAGER_TIMESTAMP_GENERATORS_FINISH]        = "generators-finish",
5272
        [MANAGER_TIMESTAMP_UNITS_LOAD_START]         = "units-load-start",
5273
        [MANAGER_TIMESTAMP_UNITS_LOAD_FINISH]        = "units-load-finish",
5274
        [MANAGER_TIMESTAMP_UNITS_LOAD]               = "units-load",
5275
        [MANAGER_TIMESTAMP_INITRD_SECURITY_START]    = "initrd-security-start",
5276
        [MANAGER_TIMESTAMP_INITRD_SECURITY_FINISH]   = "initrd-security-finish",
5277
        [MANAGER_TIMESTAMP_INITRD_GENERATORS_START]  = "initrd-generators-start",
5278
        [MANAGER_TIMESTAMP_INITRD_GENERATORS_FINISH] = "initrd-generators-finish",
5279
        [MANAGER_TIMESTAMP_INITRD_UNITS_LOAD_START]  = "initrd-units-load-start",
5280
        [MANAGER_TIMESTAMP_INITRD_UNITS_LOAD_FINISH] = "initrd-units-load-finish",
5281
        [MANAGER_TIMESTAMP_SHUTDOWN_START]           = "shutdown-start",
5282
};
5283

5284
DEFINE_STRING_TABLE_LOOKUP(manager_timestamp, ManagerTimestamp);
4,928✔
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