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

systemd / systemd / 23172097450

16 Mar 2026 07:45PM UTC coverage: 72.568% (+0.06%) from 72.513%
23172097450

push

github

keszybz
add-ug-bo-translation

316055 of 435531 relevant lines covered (72.57%)

1182407.5 hits per line

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

82.28
/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,129✔
134
        usec_t timeout;
5,129✔
135

136
        if (MANAGER_IS_USER(m))
5,129✔
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,356✔
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,356✔
145

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

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

152
        if (!m->confirm_spawn)
3,674✔
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,674✔
159
        usec_t next;
3,674✔
160
        int r;
3,674✔
161

162
        assert(m);
3,674✔
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,674✔
168
                return;
169

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

173
        next = manager_watch_jobs_next_time(m);
853✔
174
        r = sd_event_add_time(
853✔
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)
853✔
181
                return;
182

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

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

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

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

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

208
        manager_flip_auto_status(m, true, "delay");
394✔
209

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

212
        HASHMAP_FOREACH(j, m->jobs)
18,036✔
213
                if (j->state == JOB_RUNNING && counter++ == print_nr)
18,036✔
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);
394✔
219
        assert(j);
394✔
220

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

226
        m->jobs_in_progress_iteration++;
394✔
227

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

232
        (void) job_get_timeout(j, &timeout);
394✔
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);
394✔
240

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

244
        if (m->status_unit_format == STATUS_UNIT_FORMAT_DESCRIPTION)
394✔
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);
394✔
255

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

266
        (void) sd_notifyf(/* unset_environment= */ false,
394✔
267
                          "STATUS=%sUser job %s/%s running (%s / %s)...",
268
                          job_of_n,
269
                          ident, job_type_to_string(j->type),
394✔
270
                          time, limit);
271
        m->status_ready = false;
394✔
272
}
394✔
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) {
954✔
311
        assert(m);
954✔
312

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

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

320
        assert(m);
9,044✔
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))
9,044✔
325
                return 0;
326

327
        if (!m->ask_password_event_source) {
9,044✔
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;
9,044✔
362
}
363

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

367
        assert(m);
774✔
368

369
        if (m->idle_pipe_event_source)
774✔
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,176✔
385
        assert(m);
1,176✔
386

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

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

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

396
        assert(m);
271✔
397

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

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

403
        r = event_add_time_change(m->event, &m->time_change_event_source, manager_dispatch_time_change_fd, m);
260✔
404
        if (r < 0)
260✔
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);
260✔
409
        if (r < 0)
260✔
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.");
260✔
413

414
        return 0;
415
}
416

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

421
        assert(m);
322✔
422

423
        /* Read the current stat() data of /etc/localtime so that we detect changes */
424
        if (lstat(etc_localtime(), &st) < 0) {
322✔
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;
248✔
430

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

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

438
        return changed;
322✔
439
}
440

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

445
        assert(m);
291✔
446

447
        if (MANAGER_IS_TEST_RUN(m))
291✔
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(),
280✔
462
                                 IN_ATTRIB|IN_MOVE_SELF|IN_CLOSE_WRITE|IN_DONT_FOLLOW, manager_dispatch_timezone_change, m);
463
        if (r == -ENOENT) {
280✔
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)
280✔
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);
280✔
482
        if (r < 0)
280✔
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);
280✔
486
        m->timezone_change_event_source = TAKE_PTR(new_event);
280✔
487

488
        return 0;
280✔
489
}
490

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

494
        assert(m);
257✔
495

496
        if (!MANAGER_IS_SYSTEM(m) || MANAGER_IS_TEST_RUN(m))
257✔
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");
257✔
512
        }
513

514
        return 0;
515
}
516

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

525
        assert(m);
257✔
526

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

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

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

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

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

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

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

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

663
        if (MANAGER_IS_SYSTEM(m)) {
1,065✔
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());
632✔
670
                if (!path)
632✔
671
                        return log_oom();
×
672

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

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

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

690
                if (strv_env_replace_consume(&m->transient_environment, path) < 0)
433✔
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,
433✔
696
                                    "XDG_SESSION_ID",
697
                                    "XDG_SESSION_CLASS",
698
                                    "XDG_SESSION_TYPE",
699
                                    "XDG_SESSION_DESKTOP",
700
                                    "XDG_SESSION_EXTRA_DEVICE_ACCESS",
701
                                    "XDG_SEAT",
702
                                    "XDG_VTNR");
703
        }
704

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

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

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

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

731
        assert(m);
756✔
732

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

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

743
        return 0;
744
}
745

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

753
static int manager_setup_run_queue(Manager *m) {
756✔
754
        int r;
756✔
755

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

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

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

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

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

773
        return 0;
756✔
774
}
775

776
static int manager_setup_sigchld_event_source(Manager *m) {
257✔
777
        int r;
257✔
778

779
        assert(m);
257✔
780
        assert(!m->sigchld_event_source);
257✔
781

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

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

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

794
        (void) sd_event_source_set_description(m->sigchld_event_source, "manager-sigchld");
257✔
795

796
        return 0;
257✔
797
}
798

799
int manager_setup_memory_pressure_event_source(Manager *m) {
566✔
800
        int r;
566✔
801

802
        assert(m);
566✔
803

804
        m->memory_pressure_event_source = sd_event_source_disable_unref(m->memory_pressure_event_source);
566✔
805

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

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

821
        return 0;
566✔
822
}
823

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

828
        assert(m);
756✔
829

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

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

850
        return 0;
851
}
852

853
void manager_set_switching_root(Manager *m, bool switching_root) {
1,011✔
854
        assert(m);
1,011✔
855

856
        m->switching_root = MANAGER_IS_SYSTEM(m) && switching_root;
1,011✔
857
}
1,011✔
858

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

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

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

868
static int compare_job_priority(const void *a, const void *b) {
332,023✔
869
        const Job *x = a, *y = b;
332,023✔
870

871
        return unit_compare_priority(x->unit, y->unit);
332,023✔
872
}
873

874
usec_t manager_default_timeout(RuntimeScope scope) {
5,020✔
875
        return scope == RUNTIME_SCOPE_SYSTEM ? DEFAULT_TIMEOUT_USEC : DEFAULT_USER_TIMEOUT_USEC;
5,020✔
876
}
877

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

882
        assert(IN_SET(runtime_scope, RUNTIME_SCOPE_SYSTEM, RUNTIME_SCOPE_USER));
756✔
883
        assert(ret);
756✔
884

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

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

894
                .status_unit_format = STATUS_UNIT_FORMAT_DEFAULT,
895

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

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

904
                .show_status_overridden = _SHOW_STATUS_INVALID,
905

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

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

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

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

926
                .executor_fd = -EBADF,
927
        };
928

929
        unit_defaults_init(&m->defaults, runtime_scope);
756✔
930

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

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

941
        r = manager_default_environment(m);
756✔
942
        if (r < 0)
756✔
943
                return r;
944

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

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

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

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

961
        r = manager_setup_prefix(m);
756✔
962
        if (r < 0)
756✔
963
                return r;
964

965
        r = manager_find_credentials_dirs(m);
756✔
966
        if (r < 0)
756✔
967
                return r;
968

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

973
        r = manager_setup_run_queue(m);
756✔
974
        if (r < 0)
756✔
975
                return r;
976

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

986
                r = manager_setup_cgroup(m);
257✔
987
                if (r < 0)
257✔
988
                        return r;
989

990
                r = manager_setup_time_change(m);
257✔
991
                if (r < 0)
257✔
992
                        return r;
993

994
                r = manager_read_timezone_stat(m);
257✔
995
                if (r < 0)
257✔
996
                        return r;
997

998
                (void) manager_setup_timezone_change(m);
257✔
999

1000
                r = manager_setup_sigchld_event_source(m);
257✔
1001
                if (r < 0)
257✔
1002
                        return r;
1003

1004
                r = manager_setup_memory_pressure_event_source(m);
257✔
1005
                if (r < 0)
257✔
1006
                        return r;
1007

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

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

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

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

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

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

1043
        *ret = TAKE_PTR(m);
756✔
1044

1045
        return 0;
756✔
1046
}
1047

1048
static int manager_setup_notify(Manager *m) {
817✔
1049
        int r;
817✔
1050

1051
        if (MANAGER_IS_TEST_RUN(m))
817✔
1052
                return 0;
1053

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

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

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

1067
                (void) fd_increase_rxbuf(fd, MANAGER_SOCKET_RCVBUF_SIZE);
226✔
1068

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

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

1079
                (void) sockaddr_un_unlink(&sa.un);
226✔
1080

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

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

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

1094
                m->notify_fd = TAKE_FD(fd);
226✔
1095

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

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

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

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

1113
        return 0;
1114
}
1115

1116
static int manager_setup_user_lookup_fd(Manager *m) {
817✔
1117
        int r;
817✔
1118

1119
        assert(m);
817✔
1120

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

1140
        if (m->user_lookup_fds[0] < 0) {
817✔
1141

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

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

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

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

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

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

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

1170
        return 0;
1171
}
1172

1173
static int manager_setup_handoff_timestamp_fd(Manager *m) {
817✔
1174
        int r;
817✔
1175

1176
        assert(m);
817✔
1177

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

1181
        if (m->handoff_timestamp_fds[0] < 0) {
817✔
1182
                m->handoff_timestamp_event_source = sd_event_source_disable_unref(m->handoff_timestamp_event_source);
734✔
1183
                safe_close_pair(m->handoff_timestamp_fds);
734✔
1184

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

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

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

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

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

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

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

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

1217
        return 0;
1218
}
1219

1220
static int manager_setup_pidref_transport_fd(Manager *m) {
817✔
1221
        int r;
817✔
1222

1223
        assert(m);
817✔
1224

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

1228
        if (m->pidref_transport_fds[0] < 0) {
817✔
1229
                m->pidref_event_source = sd_event_source_disable_unref(m->pidref_event_source);
754✔
1230
                safe_close_pair(m->pidref_transport_fds);
754✔
1231

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

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

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

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

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

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

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

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

1266
        return 0;
1267
}
1268

1269
static unsigned manager_dispatch_cleanup_queue(Manager *m) {
227,169✔
1270
        Unit *u;
227,169✔
1271
        unsigned n = 0;
227,169✔
1272

1273
        assert(m);
227,169✔
1274

1275
        while ((u = m->cleanup_queue)) {
273,920✔
1276
                assert(u->in_cleanup_queue);
46,751✔
1277

1278
                unit_free(u);
46,751✔
1279
                n++;
46,751✔
1280
        }
1281

1282
        return n;
227,169✔
1283
}
1284

1285
static unsigned manager_dispatch_release_resources_queue(Manager *m) {
210,074✔
1286
        unsigned n = 0;
210,074✔
1287
        Unit *u;
210,074✔
1288

1289
        assert(m);
210,074✔
1290

1291
        while ((u = LIST_POP(release_resources_queue, m->release_resources_queue))) {
211,478✔
1292
                assert(u->in_release_resources_queue);
1,404✔
1293
                u->in_release_resources_queue = false;
1,404✔
1294

1295
                n++;
1,404✔
1296

1297
                unit_release_resources(u);
1,404✔
1298
        }
1299

1300
        return n;
210,074✔
1301
}
1302

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

1311
static void unit_gc_mark_good(Unit *u, unsigned gc_marker) {
84,560✔
1312
        Unit *other;
84,560✔
1313

1314
        u->gc_marker = gc_marker + GC_OFFSET_GOOD;
84,560✔
1315

1316
        /* Recursively mark referenced units as GOOD as well */
1317
        UNIT_FOREACH_DEPENDENCY(other, u, UNIT_ATOM_REFERENCES)
579,516✔
1318
                if (other->gc_marker == gc_marker + GC_OFFSET_UNSURE)
314,152✔
1319
                        unit_gc_mark_good(other, gc_marker);
1,422✔
1320
}
84,560✔
1321

1322
static void unit_gc_sweep(Unit *u, unsigned gc_marker) {
149,099✔
1323
        Unit *other;
149,099✔
1324
        bool is_bad;
149,099✔
1325

1326
        assert(u);
149,099✔
1327

1328
        if (IN_SET(u->gc_marker - gc_marker,
149,099✔
1329
                   GC_OFFSET_GOOD, GC_OFFSET_BAD, GC_OFFSET_UNSURE, GC_OFFSET_IN_PATH))
1330
                return;
65,961✔
1331

1332
        if (u->in_cleanup_queue)
131,310✔
1333
                goto bad;
×
1334

1335
        if (!unit_may_gc(u))
131,310✔
1336
                goto good;
61,179✔
1337

1338
        u->gc_marker = gc_marker + GC_OFFSET_IN_PATH;
70,131✔
1339

1340
        is_bad = true;
70,131✔
1341

1342
        UNIT_FOREACH_DEPENDENCY(other, u, UNIT_ATOM_REFERENCED_BY) {
148,368✔
1343
                unit_gc_sweep(other, gc_marker);
26,950✔
1344

1345
                if (other->gc_marker == gc_marker + GC_OFFSET_GOOD)
26,950✔
1346
                        goto good;
21,959✔
1347

1348
                if (other->gc_marker != gc_marker + GC_OFFSET_BAD)
4,991✔
1349
                        is_bad = false;
3,970✔
1350
        }
1351

1352
        LIST_FOREACH(refs_by_target, ref, u->refs_by_target) {
48,536✔
1353
                unit_gc_sweep(ref->source, gc_marker);
364✔
1354

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

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

1362
        if (is_bad)
48,172✔
1363
                goto bad;
46,378✔
1364

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

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

1378
good:
83,138✔
1379
        unit_gc_mark_good(u, gc_marker);
83,138✔
1380
}
1381

1382
static unsigned manager_dispatch_gc_unit_queue(Manager *m) {
246,713✔
1383
        unsigned n = 0, gc_marker;
246,713✔
1384

1385
        assert(m);
246,713✔
1386

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

1389
        m->gc_marker += _GC_OFFSET_MAX;
246,713✔
1390
        if (m->gc_marker + _GC_OFFSET_MAX <= _GC_OFFSET_MAX)
246,713✔
1391
                m->gc_marker = 1;
×
1392

1393
        gc_marker = m->gc_marker;
246,713✔
1394

1395
        Unit *u;
246,713✔
1396
        while ((u = m->gc_unit_queue)) {
368,498✔
1397
                assert(u->in_gc_queue);
121,785✔
1398

1399
                unit_gc_sweep(u, gc_marker);
121,785✔
1400

1401
                LIST_REMOVE(gc_queue, m->gc_unit_queue, u);
121,785✔
1402
                u->in_gc_queue = false;
121,785✔
1403

1404
                n++;
121,785✔
1405

1406
                if (IN_SET(u->gc_marker - gc_marker,
121,785✔
1407
                           GC_OFFSET_BAD, GC_OFFSET_UNSURE)) {
1408
                        if (u->id)
46,750✔
1409
                                log_unit_debug(u, "Collecting.");
46,750✔
1410
                        u->gc_marker = gc_marker + GC_OFFSET_BAD;
46,750✔
1411
                        unit_add_to_cleanup_queue(u);
46,750✔
1412
                }
1413
        }
1414

1415
        return n;
246,713✔
1416
}
1417

1418
static unsigned manager_dispatch_gc_job_queue(Manager *m) {
246,722✔
1419
        unsigned n = 0;
246,722✔
1420
        Job *j;
246,722✔
1421

1422
        assert(m);
246,722✔
1423

1424
        while ((j = LIST_POP(gc_queue, m->gc_job_queue))) {
246,732✔
1425
                assert(j->in_gc_queue);
10✔
1426
                j->in_gc_queue = false;
10✔
1427

1428
                n++;
10✔
1429

1430
                if (!job_may_gc(j))
10✔
1431
                        continue;
10✔
1432

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

1437
        return n;
246,722✔
1438
}
1439

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

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

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

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

1453
        return 0;
×
1454
}
1455

1456
static int manager_ratelimit_check_and_queue(Unit *u) {
16✔
1457
        int r;
16✔
1458

1459
        assert(u);
16✔
1460

1461
        if (ratelimit_below(&u->auto_start_stop_ratelimit))
16✔
1462
                return 1;
1463

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

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

1479
        return 0;
1480
}
1481

1482
static unsigned manager_dispatch_stop_when_unneeded_queue(Manager *m) {
210,337✔
1483
        unsigned n = 0;
210,337✔
1484
        Unit *u;
210,337✔
1485
        int r;
210,337✔
1486

1487
        assert(m);
210,337✔
1488

1489
        while ((u = LIST_POP(stop_when_unneeded_queue, m->stop_when_unneeded_queue))) {
210,695✔
1490
                _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
358✔
1491

1492
                assert(u->in_stop_when_unneeded_queue);
358✔
1493
                u->in_stop_when_unneeded_queue = false;
358✔
1494

1495
                n++;
358✔
1496

1497
                if (!unit_is_unneeded(u))
358✔
1498
                        continue;
345✔
1499

1500
                log_unit_debug(u, "Unit is not needed anymore.");
13✔
1501

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

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

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

1519
        return n;
210,337✔
1520
}
1521

1522
static unsigned manager_dispatch_start_when_upheld_queue(Manager *m) {
210,492✔
1523
        unsigned n = 0;
210,492✔
1524
        Unit *u;
210,492✔
1525
        int r;
210,492✔
1526

1527
        assert(m);
210,492✔
1528

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

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

1536
                n++;
×
1537

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

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

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

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

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

1560
        return n;
210,492✔
1561
}
1562

1563
static unsigned manager_dispatch_stop_when_bound_queue(Manager *m) {
210,492✔
1564
        unsigned n = 0;
210,492✔
1565
        Unit *u;
210,492✔
1566
        int r;
210,492✔
1567

1568
        assert(m);
210,492✔
1569

1570
        while ((u = LIST_POP(stop_when_bound_queue, m->stop_when_bound_queue))) {
210,762✔
1571
                _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
270✔
1572
                Unit *culprit = NULL;
270✔
1573

1574
                assert(u->in_stop_when_bound_queue);
270✔
1575
                u->in_stop_when_bound_queue = false;
270✔
1576

1577
                n++;
270✔
1578

1579
                if (!unit_is_bound_by_inactive(u, &culprit))
270✔
1580
                        continue;
267✔
1581

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

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

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

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

1601
        return n;
210,492✔
1602
}
1603

1604
static unsigned manager_dispatch_stop_notify_queue(Manager *m) {
208,848✔
1605
        unsigned n = 0;
208,848✔
1606

1607
        assert(m);
208,848✔
1608

1609
        if (m->may_dispatch_stop_notify_queue < 0)
208,848✔
1610
                m->may_dispatch_stop_notify_queue = hashmap_isempty(m->jobs);
3,420✔
1611

1612
        if (!m->may_dispatch_stop_notify_queue)
208,848✔
1613
                return 0;
1614

1615
        m->may_dispatch_stop_notify_queue = false;
9,142✔
1616

1617
        LIST_FOREACH(stop_notify_queue, u, m->stop_notify_queue) {
9,144✔
1618
                assert(u->in_stop_notify_queue);
2✔
1619

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

1627
        return n;
1628
}
1629

1630
static void manager_clear_jobs_and_units(Manager *m) {
819✔
1631
        Unit *u;
819✔
1632

1633
        assert(m);
819✔
1634

1635
        while ((u = hashmap_first(m->units)))
59,336✔
1636
                unit_free(u);
58,517✔
1637

1638
        manager_dispatch_cleanup_queue(m);
819✔
1639

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

1656
        assert(hashmap_isempty(m->jobs));
819✔
1657
        assert(hashmap_isempty(m->units));
819✔
1658
        assert(hashmap_isempty(m->units_by_invocation_id));
819✔
1659

1660
        m->n_on_console = 0;
819✔
1661
        m->n_running_jobs = 0;
819✔
1662
        m->n_installed_jobs = 0;
819✔
1663
        m->n_failed_jobs = 0;
819✔
1664

1665
        m->transactions_with_cycle = set_free(m->transactions_with_cycle);
819✔
1666
}
819✔
1667

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

1672
        manager_clear_jobs_and_units(m);
756✔
1673

1674
        for (UnitType c = 0; c < _UNIT_TYPE_MAX; c++)
9,072✔
1675
                if (unit_vtable[c]->shutdown)
8,316✔
1676
                        unit_vtable[c]->shutdown(m);
3,024✔
1677

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

1681
        lookup_paths_flush_generator(&m->lookup_paths);
756✔
1682

1683
        bus_done(m);
756✔
1684
        manager_varlink_done(m);
756✔
1685

1686
        exec_shared_runtime_vacuum(m);
756✔
1687
        hashmap_free(m->exec_shared_runtime_by_id);
756✔
1688

1689
        dynamic_user_vacuum(m, false);
756✔
1690
        hashmap_free(m->dynamic_users);
756✔
1691

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

1699
        prioq_free(m->run_queue);
756✔
1700

1701
        set_free(m->startup_units);
756✔
1702
        set_free(m->failed_units);
756✔
1703

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

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

1722
        manager_close_ask_password(m);
756✔
1723

1724
        manager_close_idle_pipe(m);
756✔
1725

1726
        sd_event_unref(m->event);
756✔
1727

1728
        free(m->notify_socket);
756✔
1729

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

1734
        hashmap_free(m->cgroup_unit);
756✔
1735
        manager_free_unit_name_maps(m);
756✔
1736

1737
        free(m->switch_root);
756✔
1738
        free(m->switch_root_init);
756✔
1739

1740
        sd_bus_track_unref(m->subscribed);
756✔
1741
        strv_free(m->subscribed_as_strv);
756✔
1742

1743
        unit_defaults_done(&m->defaults);
756✔
1744

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

1750
        hashmap_free(m->uid_refs);
756✔
1751
        hashmap_free(m->gid_refs);
756✔
1752

1753
        FOREACH_ARRAY(i, m->prefix, _EXEC_DIRECTORY_TYPE_MAX)
4,536✔
1754
                free(*i);
3,780✔
1755

1756
        free(m->received_credentials_directory);
756✔
1757
        free(m->received_encrypted_credentials_directory);
756✔
1758

1759
        free(m->watchdog_pretimeout_governor);
756✔
1760
        free(m->watchdog_pretimeout_governor_overridden);
756✔
1761

1762
        sd_netlink_unref(m->nfnl);
756✔
1763

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

1768
        safe_close(m->executor_fd);
756✔
1769
        free(m->executor_path);
756✔
1770

1771
        return mfree(m);
756✔
1772
}
1773

1774
static void manager_enumerate_perpetual(Manager *m) {
817✔
1775
        assert(m);
817✔
1776

1777
        if (FLAGS_SET(m->test_run_flags, MANAGER_TEST_RUN_MINIMAL))
817✔
1778
                return;
1779

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

1787
                if (unit_vtable[c]->enumerate_perpetual)
3,034✔
1788
                        unit_vtable[c]->enumerate_perpetual(m);
960✔
1789
        }
1790
}
1791

1792
static void manager_enumerate(Manager *m) {
817✔
1793
        assert(m);
817✔
1794

1795
        if (FLAGS_SET(m->test_run_flags, MANAGER_TEST_RUN_MINIMAL))
817✔
1796
                return;
1797

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

1805
                if (unit_vtable[c]->enumerate)
3,034✔
1806
                        unit_vtable[c]->enumerate(m);
636✔
1807
        }
1808

1809
        manager_dispatch_load_queue(m);
320✔
1810
}
1811

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

1817
        assert(m);
817✔
1818

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

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

1824
                /* ignore aliases */
1825
                if (u->id != k)
52,494✔
1826
                        continue;
1,297✔
1827

1828
                r = unit_coldplug(u);
51,197✔
1829
                if (r < 0)
51,197✔
1830
                        log_warning_errno(r, "We couldn't coldplug %s, proceeding anyway: %m", u->id);
53,311✔
1831
        }
1832
}
817✔
1833

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

1838
        assert(m);
817✔
1839

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

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

1845
                /* ignore aliases */
1846
                if (u->id != k)
52,494✔
1847
                        continue;
1,297✔
1848

1849
                unit_catchup(u);
51,197✔
1850
        }
1851
}
817✔
1852

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

1856
        assert(m);
754✔
1857

1858
        HASHMAP_FOREACH(u, m->units) {
8,431✔
1859

1860
                if (fdset_isempty(fds))
7,914✔
1861
                        break;
1862

1863
                if (!UNIT_VTABLE(u)->distribute_fds)
7,677✔
1864
                        continue;
6,826✔
1865

1866
                UNIT_VTABLE(u)->distribute_fds(u, fds);
851✔
1867
        }
1868
}
754✔
1869

1870
static bool manager_dbus_is_running(Manager *m, bool deserialized) {
85,190✔
1871
        Unit *u;
85,190✔
1872

1873
        assert(m);
85,190✔
1874

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

1879
        if (MANAGER_IS_TEST_RUN(m))
85,190✔
1880
                return false;
1881

1882
        u = manager_get_unit(m, SPECIAL_DBUS_SOCKET);
84,273✔
1883
        if (!u)
84,273✔
1884
                return false;
1885
        if ((deserialized ? SOCKET(u)->deserialized_state : SOCKET(u)->state) != SOCKET_RUNNING)
143,106✔
1886
                return false;
1887

1888
        u = manager_get_unit(m, SPECIAL_DBUS_SERVICE);
11,411✔
1889
        if (!u)
11,411✔
1890
                return false;
1891
        if (!IN_SET(deserialized ? SERVICE(u)->deserialized_state : SERVICE(u)->state,
22,822✔
1892
                    SERVICE_RUNNING,
1893
                    SERVICE_REFRESH_EXTENSIONS,
1894
                    SERVICE_REFRESH_CREDENTIALS,
1895
                    SERVICE_RELOAD,
1896
                    SERVICE_RELOAD_SIGNAL,
1897
                    SERVICE_RELOAD_NOTIFY,
1898
                    SERVICE_RELOAD_POST,
1899
                    SERVICE_MOUNTING))
1900
                return false;
272✔
1901

1902
        return true;
1903
}
1904

1905
static void manager_setup_bus(Manager *m) {
754✔
1906
        assert(m);
754✔
1907

1908
        if (MANAGER_IS_TEST_RUN(m))
754✔
1909
                return;
1910

1911
        /* Let's set up our private bus connection now, unconditionally */
1912
        (void) bus_init_private(m);
246✔
1913

1914
        /* If we are in --user mode also connect to the system bus now */
1915
        if (MANAGER_IS_USER(m))
246✔
1916
                (void) bus_init_system(m);
191✔
1917

1918
        /* Let's connect to the bus now, but only if the unit is supposed to be up */
1919
        if (manager_dbus_is_running(m, MANAGER_IS_RELOADING(m))) {
246✔
1920
                (void) bus_init_api(m);
17✔
1921

1922
                if (MANAGER_IS_SYSTEM(m))
17✔
1923
                        (void) bus_init_system(m);
16✔
1924
        }
1925
}
1926

1927
static void manager_preset_all(Manager *m) {
754✔
1928
        int r;
754✔
1929

1930
        assert(m);
754✔
1931

1932
        if (m->first_boot <= 0)
754✔
1933
                return;
734✔
1934

1935
        if (!MANAGER_IS_SYSTEM(m))
20✔
1936
                return;
1937

1938
        if (MANAGER_IS_TEST_RUN(m))
20✔
1939
                return;
1940

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

1947
        CLEANUP_ARRAY(changes, n_changes, install_changes_free);
20✔
1948

1949
        log_info("Applying preset policy.");
20✔
1950
        r = unit_file_preset_all(RUNTIME_SCOPE_SYSTEM, /* file_flags= */ 0,
20✔
1951
                                 /* root_dir= */ NULL, mode, &changes, &n_changes);
1952
        r = install_changes_dump(r, "preset all", changes, n_changes, /* quiet= */ false);
20✔
1953
        if (r >= 0)
20✔
1954
                log_info("Populated /etc with preset unit settings.");
×
1955
}
1956

1957
static void manager_ready(Manager *m) {
817✔
1958
        assert(m);
817✔
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 */
817✔
1963

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

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

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

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

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

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

1994
        assert(m);
754✔
1995

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

2000
        r = mkdir_label(d, 0755);
754✔
2001
        if (r < 0 && r != -EEXIST)
754✔
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) {
754✔
2008
        int r;
754✔
2009

2010
        assert(m);
754✔
2011

2012
        r = manager_make_runtime_dir(m);
754✔
2013
        if (r < 0)
754✔
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,
754✔
2019
                                      MANAGER_IS_TEST_RUN(m) ? LOOKUP_PATHS_TEMPORARY_GENERATED : 0,
754✔
2020
                                      root);
2021
        if (r < 0)
754✔
2022
                return r;
2023

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

2032
        manager_preset_all(m);
754✔
2033

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

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

2040
                /* Make sure we don't have a left-over from a previous run */
2041
                if (!serialization)
754✔
2042
                        (void) rm_rf(m->lookup_paths.transient, 0);
734✔
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));
754✔
2051
                manager_enumerate_perpetual(m);
754✔
2052
                manager_enumerate(m);
754✔
2053
                dual_timestamp_now(m->timestamps + manager_timestamp_initrd_mangle(MANAGER_TIMESTAMP_UNITS_LOAD_FINISH));
754✔
2054

2055
                /* Second, deserialize if there is something to deserialize */
2056
                if (serialization) {
754✔
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) {
754✔
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)
754✔
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);
754✔
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);
754✔
2083
                if (r < 0)
754✔
2084
                        /* No sense to continue without notifications, our children would fail anyway. */
2085
                        return r;
2086

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

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

2097
                r = manager_setup_pidref_transport_fd(m);
754✔
2098
                if (r < 0)
754✔
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);
754✔
2104

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

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

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

2115
                if (serialization)
754✔
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);
754✔
2122

2123
        manager_set_switching_root(m, false);
754✔
2124

2125
        return 0;
754✔
2126
}
2127

2128
int manager_add_job_full(
1,940✔
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,940✔
2139
        int r;
1,940✔
2140

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

2147
        if (mode == JOB_ISOLATE && type != JOB_START)
1,940✔
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,940✔
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,912✔
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,912✔
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,912✔
2160
        if (!tr)
1,912✔
2161
                return -ENOMEM;
2162

2163
        LOG_CONTEXT_PUSHF("TRANSACTION_ID=%" PRIu64, tr->id);
3,824✔
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,912✔
2166

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

2169
        r = transaction_add_job_and_dependencies(
5,736✔
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,912✔
2176
                        (mode == JOB_IGNORE_DEPENDENCIES ? TRANSACTION_IGNORE_ORDER : 0) |
1,912✔
2177
                        (mode == JOB_RESTART_DEPENDENCIES ? TRANSACTION_PROPAGATE_START_AS_RESTART : 0) |
3,819✔
2178
                        extra_flags,
2179
                        reterr_error);
2180
        if (r < 0)
1,912✔
2181
                return r;
2182

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

2189
        if (mode == JOB_TRIGGERING) {
1,899✔
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,899✔
2196
        if (r < 0)
1,899✔
2197
                return r;
2198

2199
        log_unit_debug(unit,
1,890✔
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,890✔
2204
                *ret = tr->anchor_job;
1,124✔
2205

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

2210
int manager_add_job(
1,044✔
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);
1,044✔
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) {
209✔
2222
        Unit *unit = NULL;  /* just to appease gcc, initialization is not really necessary */
209✔
2223
        int r;
209✔
2224

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

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

2235
        return manager_add_job_full(m, type, unit, mode, /* extra_flags= */ 0, affected_jobs, e, ret);
209✔
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) {
194✔
2239
        _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
194✔
2240
        int r;
194✔
2241

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

2247
        r = manager_add_job_by_name(m, type, name, mode, affected_jobs, &error, ret);
194✔
2248
        if (r < 0)
194✔
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) {
30,399✔
2255
        _cleanup_(transaction_abort_and_freep) Transaction *tr = NULL;
30,399✔
2256
        int r;
30,399✔
2257

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

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

2267
        LOG_CONTEXT_PUSHF("TRANSACTION_ID=%" PRIu64, tr->id);
60,798✔
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);
30,399✔
2271
        if (r < 0)
30,399✔
2272
                return r;
2273

2274
        /* Failure in adding individual dependencies is ignored, so this always succeeds. */
2275
        transaction_add_propagate_reload_jobs(
30,399✔
2276
                        tr,
2277
                        unit,
2278
                        tr->anchor_job,
30,399✔
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)
30,399✔
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) {
19,227✔
2295
        assert(m);
19,227✔
2296

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

2300
Unit *manager_get_unit(Manager *m, const char *name) {
2,183,245✔
2301
        assert(m);
2,183,245✔
2302
        assert(name);
2,183,245✔
2303

2304
        return hashmap_get(m->units, name);
2,183,245✔
2305
}
2306

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

2311
        assert(m);
268,184✔
2312

2313
        while ((u = LIST_POP(target_deps_queue, m->target_deps_queue))) {
346,473✔
2314
                _cleanup_free_ Unit **targets = NULL;
78,289✔
2315
                int n_targets;
78,289✔
2316

2317
                assert(u->in_target_deps_queue);
78,289✔
2318

2319
                u->in_target_deps_queue = false;
78,289✔
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);
78,289✔
2325
                if (n_targets < 0)
78,289✔
2326
                        return n_targets;
2327

2328
                FOREACH_ARRAY(i, targets, n_targets) {
147,152✔
2329
                        r = unit_add_default_target_dependency(u, *i);
68,863✔
2330
                        if (r < 0)
68,863✔
2331
                                return r;
2332
                }
2333
        }
2334

2335
        return r;
2336
}
2337

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

2342
        assert(m);
301,894✔
2343

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

2348
        m->dispatching_load_queue = true;
268,184✔
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)) {
373,869✔
2354
                assert(u->in_load_queue);
105,685✔
2355

2356
                unit_load(u);
105,685✔
2357
                n++;
105,685✔
2358
        }
2359

2360
        m->dispatching_load_queue = false;
268,184✔
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);
268,184✔
2365

2366
        return n;
268,184✔
2367
}
2368

2369
bool manager_unit_cache_should_retry_load(Unit *u) {
705,055✔
2370
        assert(u);
705,055✔
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)
705,055✔
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,447✔
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,447✔
2385
}
2386

2387
int manager_load_unit_prepare(
642,870✔
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;
642,870✔
2396
        int r;
642,870✔
2397

2398
        assert(m);
642,870✔
2399
        assert(ret);
642,870✔
2400
        assert(name || path);
642,870✔
2401

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

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

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

2414
                name = nbuf;
482✔
2415
        }
2416

2417
        UnitType t = unit_name_to_type(name);
642,870✔
2418

2419
        if (t == _UNIT_TYPE_INVALID || !unit_name_is_valid(name, UNIT_NAME_PLAIN|UNIT_NAME_INSTANCE)) {
642,870✔
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);
642,870✔
2427
        if (unit) {
642,870✔
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))
579,497✔
2438
                        unit->load_state = UNIT_STUB;
×
2439
                else {
2440
                        *ret = unit;
579,497✔
2441
                        return 0;  /* The unit was already loaded */
579,497✔
2442
                }
2443
        } else {
2444
                unit = cleanup_unit = unit_new(m, unit_vtable[t]->object_size);
63,373✔
2445
                if (!unit)
63,373✔
2446
                        return -ENOMEM;
2447
        }
2448

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

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

2459
        unit_add_to_load_queue(unit);
63,373✔
2460
        unit_add_to_dbus_queue(unit);
63,373✔
2461
        unit_add_to_gc_queue(unit);
63,373✔
2462

2463
        *ret = unit;
63,373✔
2464
        TAKE_PTR(cleanup_unit);
63,373✔
2465

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

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

2477
        assert(m);
616,636✔
2478
        assert(ret);
616,636✔
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);
616,636✔
2483
        if (r <= 0)
616,636✔
2484
                return r;
2485

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

2492
int manager_load_startable_unit_or_warn(
740✔
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;
740✔
2501
        Unit *unit;
740✔
2502
        int r;
740✔
2503

2504
        r = manager_load_unit(m, name, path, &error, &unit);
740✔
2505
        if (r < 0)
740✔
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);
740✔
2511
        if (r < 0)
740✔
2512
                return log_error_errno(r, "%s", bus_error_message(&error, r));
9✔
2513

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

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

2521
        assert(m);
472✔
2522

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

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

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

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

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

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

2546
        assert(source);
4,365✔
2547

2548
        while ((j = prioq_peek(m->run_queue))) {
48,884✔
2549
                assert(j->installed);
44,519✔
2550
                assert(j->in_run_queue);
44,519✔
2551

2552
                (void) job_run_and_invalidate(j);
44,519✔
2553
        }
2554

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

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

2561
        return 1;
4,365✔
2562
}
2563

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

2567
        assert(m);
47,162✔
2568

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

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

2581
        assert(m);
208,848✔
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)
208,848✔
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)
208,765✔
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)
8,370✔
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)) {
51,657✔
2612

2613
                assert(u->in_dbus_queue);
43,204✔
2614

2615
                bus_unit_send_change_signal(u);
43,204✔
2616
                n++;
43,204✔
2617

2618
                if (budget != UINT_MAX)
43,204✔
2619
                        budget--;
22,924✔
2620
        }
2621

2622
        while (budget != 0 && (j = m->dbus_job_queue)) {
28,605✔
2623
                assert(j->in_dbus_queue);
20,152✔
2624

2625
                bus_job_send_change_signal(j);
20,152✔
2626
                n++;
20,152✔
2627

2628
                if (budget != UINT_MAX)
20,152✔
2629
                        budget--;
19,744✔
2630
        }
2631

2632
        if (m->send_reloading_done) {
8,453✔
2633
                m->send_reloading_done = false;
83✔
2634
                bus_manager_send_reloading(m, false);
83✔
2635
                n++;
83✔
2636
        }
2637

2638
        if (m->pending_reload_message_dbus) {
8,453✔
2639
                bus_send_pending_reload_message(m);
62✔
2640
                n++;
62✔
2641
        }
2642

2643
        if (m->pending_reload_message_vl) {
8,453✔
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,974✔
2652

2653
        /* nothing else must be sent when using BARRIER=1 */
2654
        if (strv_contains(tags, "BARRIER=1")) {
3,974✔
2655
                if (strv_length(tags) != 1)
15✔
2656
                        log_warning("Extra notification messages sent with BARRIER=1, ignoring everything.");
×
2657
                else if (fdset_size(fds) != 1)
15✔
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;
15✔
2662
        }
2663

2664
        return false;
2665
}
2666

2667
static void manager_invoke_notify_message(
7,768✔
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,768✔
2676
        assert(u);
7,768✔
2677
        assert(pidref_is_set(pidref));
7,768✔
2678
        assert(ucred);
7,768✔
2679
        assert(pidref->pid == ucred->pid);
7,768✔
2680
        assert(tags);
7,768✔
2681

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

2686
        if (UNIT_VTABLE(u)->notify_message)
3,955✔
2687
                UNIT_VTABLE(u)->notify_message(u, pidref, ucred, tags, fds);
3,955✔
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,831✔
2699
        /* Determine array of every unit that is interested in the specified process */
2700

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

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

2709
        size_t n = 0;
12,831✔
2710
        if (u1)
12,831✔
2711
                n++;
12,827✔
2712
        if (u2)
12,831✔
2713
                n++;
8,943✔
2714
        if (array)
12,831✔
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,831✔
2721
                _cleanup_free_ Unit **units = NULL;
×
2722

2723
                if (n > 0) {
12,831✔
2724
                        units = new(Unit*, n + 1);
12,827✔
2725
                        if (!units)
12,827✔
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,827✔
2733
                        if (u1)
12,827✔
2734
                                units[i++] = u1;
12,827✔
2735
                        if (u2)
12,827✔
2736
                                units[i++] = u2;
8,943✔
2737
                        if (array)
12,827✔
2738
                                for (size_t j = 0; array[j]; j++)
×
2739
                                        units[i++] = array[j];
×
2740
                        assert(i == n);
12,827✔
2741

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

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

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

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

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

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

2765
        _cleanup_strv_free_ char **tags = NULL;
3,974✔
2766
        r = notify_recv_with_fds_strv(m->notify_fd, &tags, &ucred, &pidref, &fds);
3,974✔
2767
        if (r == -EAGAIN)
3,974✔
2768
                return 0;
2769
        if (r < 0)
3,974✔
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,974✔
2778
                log_debug("Received barrier notification message from PID " PID_FMT ".", pidref.pid);
15✔
2779
                return 0;
15✔
2780
        }
2781

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

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

2788
        int n_array = manager_get_units_for_pidref(m, &pidref, &array);
3,959✔
2789
        if (n_array < 0) {
3,959✔
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,959✔
2794
                log_debug("Cannot find unit for notify message of PID "PID_FMT", ignoring.", pidref.pid);
4✔
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,723✔
2799
                        manager_invoke_notify_message(m, *u, &pidref, &ucred, tags, fds);
7,768✔
2800

2801
        if (!fdset_isempty(fds))
3,959✔
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,746✔
2808
                Manager *m,
2809
                Unit *u,
2810
                const siginfo_t *si) {
2811

2812
        assert(m);
8,746✔
2813
        assert(u);
8,746✔
2814
        assert(si);
8,746✔
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,746✔
2818
                return;
2,400✔
2819
        u->sigchldgen = m->sigchldgen;
6,346✔
2820

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

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

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

2833
        assert(source);
10,997✔
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,997✔
2839

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

2843
                goto turn_off;
94✔
2844
        }
2845

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

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

2853
                log_debug("Child "PID_FMT" (%s) died (code=%s, status=%i/%s)",
12,488✔
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,244✔
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,244✔
2867

2868
                /* And now figure out the units this belongs to, there might be multiple... */
2869
                _cleanup_free_ Unit **array = NULL;
6,244✔
2870
                int n_array = manager_get_units_for_pidref(m, &pidref, &array);
6,244✔
2871
                if (n_array < 0)
6,244✔
2872
                        log_warning_errno(n_array, "Failed to get units for process " PID_FMT ", ignoring: %m", si.si_pid);
6,244✔
2873
                else if (n_array == 0)
6,244✔
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,244✔
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,244✔
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,990✔
2887
                                manager_invoke_sigchld_event(m, *u, &si);
8,746✔
2888
                }
2889
        }
2890

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

2897
        return 0;
2898

2899
turn_off:
4,753✔
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,753✔
2903
        if (r < 0)
4,753✔
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) {
190✔
2910
        Job *job;
190✔
2911

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

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

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

2919
        (void) sd_notifyf(/* unset_environment= */ false,
190✔
2920
                          "STATUS=Activating special unit %s...", s);
2921
        m->status_ready = false;
190✔
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,636✔
2943
        Manager *m = ASSERT_PTR(userdata);
4,636✔
2944
        ssize_t n;
4,636✔
2945
        struct signalfd_siginfo sfsi;
4,636✔
2946
        int r;
4,636✔
2947

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

2950
        if (revents != EPOLLIN) {
4,636✔
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,636✔
2956
        if (n < 0) {
4,636✔
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,636✔
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,826✔
2970
                            (sfsi.ssi_signo == SIGTERM && MANAGER_IS_USER(m))
190✔
2971
                            ? LOG_DEBUG : LOG_INFO,
2972
                            &sfsi);
2973

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

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

2981
                break;
2982

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

2990
                _fallthrough_;
190✔
2991
        case SIGINT:
2992
                if (MANAGER_IS_SYSTEM(m))
190✔
2993
                        manager_handle_ctrl_alt_del(m);
×
2994
                else
2995
                        manager_start_special(m, SPECIAL_EXIT_TARGET, JOB_REPLACE_IRREVERSIBLY);
190✔
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) {
14✔
3190
        Manager *m = ASSERT_PTR(userdata);
14✔
3191
        Unit *u;
14✔
3192

3193
        log_struct(LOG_DEBUG,
14✔
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);
14✔
3199

3200
        HASHMAP_FOREACH(u, m->units)
1,955✔
3201
                if (UNIT_VTABLE(u)->time_change)
1,927✔
3202
                        UNIT_VTABLE(u)->time_change(u);
14✔
3203

3204
        return 0;
14✔
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);
34✔
3224

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

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

3230
        HASHMAP_FOREACH(u, m->units)
7,451✔
3231
                if (UNIT_VTABLE(u)->timezone_change)
7,383✔
3232
                        UNIT_VTABLE(u)->timezone_change(u);
74✔
3233

3234
        return 0;
34✔
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) {
394✔
3257
        Manager *m = ASSERT_PTR(userdata);
394✔
3258
        int r;
394✔
3259

3260
        assert(source);
394✔
3261

3262
        manager_print_jobs_in_progress(m);
394✔
3263

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

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

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

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

3278
        manager_check_finished(m);
309✔
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);
309✔
3282
        if (r < 0)
309✔
3283
                return log_error_errno(r, "Failed to enable SIGCHLD event source: %m");
×
3284

3285
        while (m->objective == MANAGER_OK) {
247,032✔
3286

3287
                if (!ratelimit_below(&rl)) {
246,723✔
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();
246,723✔
3294

3295
                if (manager_dispatch_load_queue(m) > 0)
246,723✔
3296
                        continue;
1✔
3297

3298
                if (manager_dispatch_gc_job_queue(m) > 0)
246,722✔
3299
                        continue;
9✔
3300

3301
                if (manager_dispatch_gc_unit_queue(m) > 0)
246,713✔
3302
                        continue;
20,363✔
3303

3304
                if (manager_dispatch_cleanup_queue(m) > 0)
226,350✔
3305
                        continue;
11,237✔
3306

3307
                if (manager_dispatch_cgroup_realize_queue(m) > 0)
215,113✔
3308
                        continue;
4,621✔
3309

3310
                if (manager_dispatch_start_when_upheld_queue(m) > 0)
210,492✔
3311
                        continue;
×
3312

3313
                if (manager_dispatch_stop_when_bound_queue(m) > 0)
210,492✔
3314
                        continue;
155✔
3315

3316
                if (manager_dispatch_stop_when_unneeded_queue(m) > 0)
210,337✔
3317
                        continue;
263✔
3318

3319
                if (manager_dispatch_release_resources_queue(m) > 0)
210,074✔
3320
                        continue;
1,226✔
3321

3322
                if (manager_dispatch_stop_notify_queue(m) > 0)
208,848✔
3323
                        continue;
×
3324

3325
                if (manager_dispatch_dbus_queue(m) > 0)
208,848✔
3326
                        continue;
8,453✔
3327

3328
                /* Sleep for watchdog runtime wait time */
3329
                r = sd_event_run(m->event, watchdog_runtime_wait(/* divisor= */ 2));
200,395✔
3330
                if (r < 0)
200,395✔
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) {
394,906✔
3338
        _cleanup_free_ char *n = NULL;
394,906✔
3339
        sd_id128_t invocation_id;
394,906✔
3340
        Unit *u;
394,906✔
3341
        int r;
394,906✔
3342

3343
        assert(m);
394,906✔
3344
        assert(s);
394,906✔
3345
        assert(_u);
394,906✔
3346

3347
        r = unit_name_from_dbus_path(s, &n);
394,906✔
3348
        if (r < 0)
394,906✔
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);
394,869✔
3354
        if (r >= 0) {
394,869✔
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)) {
394,869✔
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);
394,868✔
3376
        if (r < 0)
394,868✔
3377
                return r;
3378

3379
        *_u = u;
394,868✔
3380
        return 0;
394,868✔
3381
}
3382

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

3389
        assert(m);
1,025✔
3390
        assert(s);
1,025✔
3391
        assert(_j);
1,025✔
3392

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

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

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

3405
        *_j = j;
1,008✔
3406

3407
        return 0;
1,008✔
3408
}
3409

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

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

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

3420
        if (!MANAGER_IS_SYSTEM(m))
3,981✔
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,527✔
3425
                return;
3426

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

3431
        r = unit_name_to_prefix_and_instance(u->id, &p);
1,010✔
3432
        if (r < 0) {
1,010✔
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,050✔
3438
        if (sym_audit_log_user_comm_message(audit_fd, type, msg, "systemd", NULL, NULL, NULL, success) < 0) {
1,010✔
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,010✔
3445
        }
3446
#endif
3447
}
3448

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

3453
        assert(m);
45,681✔
3454
        assert(u);
45,681✔
3455

3456
        if (!MANAGER_IS_SYSTEM(m))
45,681✔
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))
7,182✔
3461
                return;
3462

3463
        if (detect_container() > 0)
7,182✔
3464
                return;
3465

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

3469
        c = asprintf(&message, "U\x02%c%s%c", (int) (strlen(u->id) + 1), u->id, '\x00');
738✔
3470
        if (c < 0)
738✔
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);
738✔
3475
        if (r < 0)
738✔
3476
                log_full_errno(ERRNO_IS_NO_PLYMOUTH(r) ? LOG_DEBUG : LOG_WARNING, r,
738✔
3477
                               "Failed to communicate with plymouth: %m");
3478
}
3479

3480
void manager_send_unit_supervisor(Manager *m, Unit *u, bool active) {
74,028✔
3481
        assert(m);
74,028✔
3482
        assert(u);
74,028✔
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))
74,028✔
3488
                return;
3489

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

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

3496
        (void) sd_notifyf(/* unset_environment= */ false,
5,337✔
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) {
577✔
3502
        assert(m);
577✔
3503

3504
        if (MANAGER_IS_USER(m))
577✔
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,236✔
3514

3515
        assert(m);
1,236✔
3516

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

3520
        if (m->watchdog_overridden[t] == USEC_INFINITY) {
344✔
3521
                if (t == WATCHDOG_RUNTIME)
338✔
3522
                        (void) watchdog_setup(timeout);
86✔
3523
                else if (t == WATCHDOG_PRETIMEOUT)
252✔
3524
                        (void) watchdog_setup_pretimeout(timeout);
86✔
3525
        }
3526

3527
        m->watchdog[t] = timeout;
344✔
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) {
309✔
3548
        _cleanup_free_ char *p = NULL;
309✔
3549
        int r;
309✔
3550

3551
        assert(m);
309✔
3552

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

3556
        if (streq_ptr(m->watchdog_pretimeout_governor, governor))
86✔
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) {
63✔
3594
        _unused_ _cleanup_(manager_reloading_stopp) Manager *reloading = NULL;
63✔
3595
        _cleanup_fdset_free_ FDSet *fds = NULL;
×
3596
        _cleanup_fclose_ FILE *f = NULL;
63✔
3597
        int r;
63✔
3598

3599
        assert(m);
63✔
3600

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

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

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

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

3616
        r = finish_serialization_file(f);
63✔
3617
        if (r < 0)
63✔
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;
63✔
3622

3623
        bus_manager_send_reloading(m, true);
63✔
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);
63✔
3630
        lookup_paths_flush_generator(&m->lookup_paths);
63✔
3631
        exec_shared_runtime_vacuum(m);
63✔
3632
        dynamic_user_vacuum(m, false);
63✔
3633
        m->uid_refs = hashmap_free(m->uid_refs);
63✔
3634
        m->gid_refs = hashmap_free(m->gid_refs);
63✔
3635

3636
        (void) manager_run_environment_generators(m);
63✔
3637
        (void) manager_run_generators(m);
63✔
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);
63✔
3641
        m->unit_file_state_outdated = false;
63✔
3642

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

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

3652
        /* We don't need the serialization anymore */
3653
        f = safe_fclose(f);
63✔
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);
63✔
3657
        (void) manager_setup_user_lookup_fd(m);
63✔
3658
        (void) manager_setup_handoff_timestamp_fd(m);
63✔
3659
        (void) manager_setup_pidref_transport_fd(m);
63✔
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);
63✔
3664
        m->deserialized_bus_id = SD_ID128_NULL;
63✔
3665

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

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

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

3676
        manager_ready(m);
63✔
3677

3678
        m->send_reloading_done = true;
63✔
3679
        return 0;
63✔
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)
266✔
3688
                unit_reset_failed(u);
265✔
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,424✔
3708
        assert(m);
3,424✔
3709

3710
        if (MANAGER_IS_USER(m) || m->taint_logged)
3,424✔
3711
                return;
3,424✔
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) {
198✔
3726
        usec_t firmware_usec, loader_usec, kernel_usec, initrd_usec, userspace_usec, total_usec;
198✔
3727

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

3731
        if (MANAGER_IS_SYSTEM(m) && m->soft_reboots_count > 0) {
191✔
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) {
194✔
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;
188✔
3799
                total_usec = userspace_usec = m->timestamps[MANAGER_TIMESTAMP_FINISH].monotonic - m->timestamps[MANAGER_TIMESTAMP_USERSPACE].monotonic;
188✔
3800

3801
                log_struct(LOG_INFO,
188✔
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);
191✔
3809

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

3813
        log_taint_string(m);
191✔
3814
}
3815

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

3819
        assert(m);
3,233✔
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,233✔
3823
                return;
3824

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

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

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

3838
        assert(m);
411✔
3839

3840
        /* Skip the notification if nothing changed. */
3841
        if (m->ready_sent && m->status_ready)
411✔
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,
233✔
3848
                      "READY=1\n"
3849
                      "STATUS=Ready.");
3850
        if (r < 0)
233✔
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;
233✔
3855
}
3856

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

3860
        assert(m);
18,330✔
3861

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

3866
        u = manager_get_unit(m, SPECIAL_BASIC_TARGET);
18,096✔
3867
        if (!u || !UNIT_IS_ACTIVE_OR_RELOADING(unit_active_state(u)))
18,096✔
3868
                return;
14,863✔
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,233✔
3872

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

3877
void manager_check_finished(Manager *m) {
18,544✔
3878
        assert(m);
18,544✔
3879

3880
        if (MANAGER_IS_RELOADING(m))
18,544✔
3881
                return;
3882

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

3887
        manager_check_basic_target(m);
18,330✔
3888

3889
        if (!hashmap_isempty(m->jobs)) {
18,330✔
3890
                if (m->jobs_in_progress_event_source)
17,919✔
3891
                        /* Ignore any failure, this is only for feedback */
3892
                        (void) sd_event_source_set_time(m->jobs_in_progress_event_source,
4,276✔
3893
                                                        manager_watch_jobs_next_time(m));
4,276✔
3894
                return;
17,919✔
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)
411✔
3900
                m->jobs = hashmap_free(m->jobs);
200✔
3901

3902
        manager_send_ready_on_idle(m);
411✔
3903

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

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

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

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

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

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

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

3923
        manager_notify_finished(m);
198✔
3924

3925
        manager_invalidate_startup_units(m);
198✔
3926
}
3927

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

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

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

3938
static bool generator_path_any(char * const *paths) {
1,099✔
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,396✔
3943
                if (access(*i, F_OK) >= 0)
4,396✔
3944
                        return true;
3945
                if (errno != ENOENT)
3,297✔
3946
                        log_warning_errno(errno, "Failed to check if generator dir '%s' exists, assuming not: %m", *i);
3,297✔
3947
        }
3948

3949
        return false;
3950
}
3951

3952
static int manager_run_environment_generators(Manager *m) {
817✔
3953
        _cleanup_strv_free_ char **paths = NULL;
817✔
3954
        int r;
817✔
3955

3956
        assert(m);
817✔
3957

3958
        if (MANAGER_IS_TEST_RUN(m) && !(m->test_run_flags & MANAGER_TEST_RUN_ENV_GENERATORS))
817✔
3959
                return 0;
3960

3961
        paths = env_generator_binary_paths(m->runtime_scope);
790✔
3962
        if (!paths)
790✔
3963
                return log_oom();
×
3964

3965
        if (!generator_path_any(paths))
790✔
3966
                return 0;
3967

3968
        char **tmp = NULL; /* this is only used in the forked process, no cleanup here */
790✔
3969
        void *args[_STDOUT_CONSUME_MAX] = {
790✔
3970
                [STDOUT_GENERATE] = &tmp,
3971
                [STDOUT_COLLECT]  = &tmp,
3972
                [STDOUT_CONSUME]  = &m->transient_environment,
790✔
3973
        };
3974

3975
        WITH_UMASK(0022)
1,580✔
3976
                r = execute_directories(
790✔
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;
790✔
3986
}
3987

3988
static int build_generator_environment(Manager *m, char ***ret) {
223✔
3989
        _cleanup_strv_free_ char **nl = NULL;
223✔
3990
        Virtualization v;
223✔
3991
        ConfidentialVirtualization cv;
223✔
3992
        int r;
223✔
3993

3994
        assert(m);
223✔
3995
        assert(ret);
223✔
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);
223✔
4002
        if (!nl)
223✔
4003
                return -ENOMEM;
4004

4005
        r = strv_env_assign(&nl, "SYSTEMD_SCOPE", runtime_scope_to_string(m->runtime_scope));
223✔
4006
        if (r < 0)
223✔
4007
                return r;
4008

4009
        if (MANAGER_IS_SYSTEM(m)) {
223✔
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();
223✔
4032
        if (v < 0)
223✔
4033
                log_debug_errno(v, "Failed to detect virtualization, ignoring: %m");
×
4034
        else if (v > 0) {
223✔
4035
                const char *s;
223✔
4036

4037
                s = strjoina(VIRTUALIZATION_IS_VM(v) ? "vm:" :
1,115✔
4038
                             VIRTUALIZATION_IS_CONTAINER(v) ? "container:" : ":",
4039
                             virtualization_to_string(v));
4040

4041
                r = strv_env_assign(&nl, "SYSTEMD_VIRTUALIZATION", s);
223✔
4042
                if (r < 0)
223✔
4043
                        return r;
4044
        }
4045

4046
        cv = detect_confidential_virtualization();
223✔
4047
        if (cv < 0)
223✔
4048
                log_debug_errno(cv, "Failed to detect confidential virtualization, ignoring: %m");
×
4049
        else if (cv > 0) {
223✔
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()));
223✔
4056
        if (r < 0)
223✔
4057
                return r;
4058

4059
        *ret = TAKE_PTR(nl);
223✔
4060
        return 0;
223✔
4061
}
4062

4063
static int manager_execute_generators(Manager *m, char * const *paths, bool remount_ro) {
223✔
4064
        _cleanup_strv_free_ char **ge = NULL;
223✔
4065
        int r;
223✔
4066

4067
        assert(m);
223✔
4068

4069
        r = build_generator_environment(m, &ge);
223✔
4070
        if (r < 0)
223✔
4071
                return log_error_errno(r, "Failed to build generator environment: %m");
×
4072

4073
        if (remount_ro) {
223✔
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[] = {
223✔
4086
                NULL, /* Leave this empty, execute_directory() will fill something in */
4087
                m->lookup_paths.generator,
223✔
4088
                m->lookup_paths.generator_early,
223✔
4089
                m->lookup_paths.generator_late,
223✔
4090
                NULL,
4091
        };
4092

4093
        BLOCK_WITH_UMASK(0022);
446✔
4094
        return execute_directories(
223✔
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) {
817✔
4105
        ForkFlags flags = FORK_RESET_SIGNALS | FORK_WAIT | FORK_NEW_MOUNTNS | FORK_MOUNTNS_SLAVE;
817✔
4106
        _cleanup_strv_free_ char **paths = NULL;
817✔
4107
        int r;
817✔
4108

4109
        assert(m);
817✔
4110

4111
        if (MANAGER_IS_TEST_RUN(m) && !(m->test_run_flags & MANAGER_TEST_RUN_GENERATORS))
817✔
4112
                return 0;
4113

4114
        paths = generator_binary_paths(m->runtime_scope);
309✔
4115
        if (!paths)
309✔
4116
                return log_oom();
×
4117

4118
        if (!generator_path_any(paths))
309✔
4119
                return 0;
4120

4121
        r = lookup_paths_mkdir_generator(&m->lookup_paths);
309✔
4122
        if (r < 0) {
309✔
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)) {
309✔
4132
                r = manager_execute_generators(m, paths, /* remount_ro= */ false);
223✔
4133
                goto finish;
223✔
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))
86✔
4139
                flags |= FORK_PRIVATE_TMP;
86✔
4140

4141
        r = pidref_safe_fork("(sd-gens)", flags, /* ret= */ NULL);
86✔
4142
        if (r == 0) {
86✔
4143
                r = manager_execute_generators(m, paths, /* remount_ro= */ true);
×
4144
                _exit(r >= 0 ? EXIT_SUCCESS : EXIT_FAILURE);
×
4145
        }
4146
        if (r < 0) {
86✔
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:
86✔
4168
        lookup_paths_trim_generator(&m->lookup_paths);
309✔
4169
        return r;
4170
}
4171

4172
int manager_transient_environment_add(Manager *m, char **plus) {
309✔
4173
        char **a;
309✔
4174

4175
        assert(m);
309✔
4176

4177
        if (strv_isempty(plus))
309✔
4178
                return 0;
309✔
4179

4180
        a = strv_env_merge(m->transient_environment, plus);
86✔
4181
        if (!a)
86✔
4182
                return log_oom();
×
4183

4184
        sanitize_environment(a);
86✔
4185

4186
        return strv_free_and_replace(m->transient_environment, a);
86✔
4187
}
4188

4189
int manager_client_environment_modify(
190✔
4190
                Manager *m,
4191
                char **minus,
4192
                char **plus) {
4193

4194
        char **a = NULL, **b = NULL, **l;
190✔
4195

4196
        assert(m);
190✔
4197

4198
        if (strv_isempty(minus) && strv_isempty(plus))
190✔
4199
                return 0;
4200

4201
        l = m->client_environment;
190✔
4202

4203
        if (!strv_isempty(minus)) {
190✔
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)) {
190✔
4212
                b = strv_env_merge(l, plus);
189✔
4213
                if (!b) {
189✔
4214
                        strv_free(a);
×
4215
                        return -ENOMEM;
×
4216
                }
4217

4218
                l = b;
4219
        }
4220

4221
        if (m->client_environment != l)
190✔
4222
                strv_free(m->client_environment);
190✔
4223

4224
        if (a != l)
190✔
4225
                strv_free(a);
189✔
4226
        if (b != l)
190✔
4227
                strv_free(b);
1✔
4228

4229
        m->client_environment = sanitize_environment(l);
190✔
4230
        return 0;
190✔
4231
}
4232

4233
int manager_get_effective_environment(Manager *m, char ***ret) {
23,785✔
4234
        char **l;
23,785✔
4235

4236
        assert(m);
23,785✔
4237
        assert(ret);
23,785✔
4238

4239
        l = strv_env_merge(m->transient_environment, m->client_environment);
23,785✔
4240
        if (!l)
23,785✔
4241
                return -ENOMEM;
4242

4243
        *ret = l;
23,785✔
4244
        return 0;
23,785✔
4245
}
4246

4247
int manager_set_unit_defaults(Manager *m, const UnitDefaults *defaults) {
309✔
4248
        _cleanup_free_ char *label = NULL;
309✔
4249
        struct rlimit *rlimit[_RLIMIT_MAX];
309✔
4250
        int r;
309✔
4251

4252
        assert(m);
309✔
4253
        assert(defaults);
309✔
4254

4255
        if (streq_ptr(defaults->smack_process_label, "/"))
309✔
4256
                label = NULL;
4257
        else  {
4258
                const char *l = defaults->smack_process_label;
309✔
4259
#ifdef SMACK_DEFAULT_PROCESS_LABEL
4260
                if (!l)
4261
                        l = SMACK_DEFAULT_PROCESS_LABEL;
4262
#endif
4263
                if (l) {
309✔
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);
309✔
4272
        if (r < 0)
309✔
4273
                return r;
4274

4275
        m->defaults.std_output = defaults->std_output;
309✔
4276
        m->defaults.std_error = defaults->std_error;
309✔
4277

4278
        m->defaults.restart_usec = defaults->restart_usec;
309✔
4279
        m->defaults.timeout_start_usec = defaults->timeout_start_usec;
309✔
4280
        m->defaults.timeout_stop_usec = defaults->timeout_stop_usec;
309✔
4281
        m->defaults.timeout_abort_usec = defaults->timeout_abort_usec;
309✔
4282
        m->defaults.timeout_abort_set = defaults->timeout_abort_set;
309✔
4283
        m->defaults.device_timeout_usec = defaults->device_timeout_usec;
309✔
4284

4285
        m->defaults.restrict_suid_sgid = defaults->restrict_suid_sgid;
309✔
4286

4287
        m->defaults.start_limit = defaults->start_limit;
309✔
4288

4289
        m->defaults.memory_accounting = defaults->memory_accounting;
309✔
4290
        m->defaults.io_accounting = defaults->io_accounting;
309✔
4291
        m->defaults.tasks_accounting = defaults->tasks_accounting;
309✔
4292
        m->defaults.ip_accounting = defaults->ip_accounting;
309✔
4293

4294
        m->defaults.tasks_max = defaults->tasks_max;
309✔
4295
        m->defaults.timer_accuracy_usec = defaults->timer_accuracy_usec;
309✔
4296

4297
        m->defaults.oom_policy = defaults->oom_policy;
309✔
4298
        m->defaults.oom_score_adjust = defaults->oom_score_adjust;
309✔
4299
        m->defaults.oom_score_adjust_set = defaults->oom_score_adjust_set;
309✔
4300

4301
        m->defaults.memory_pressure_watch = defaults->memory_pressure_watch;
309✔
4302
        m->defaults.memory_pressure_threshold_usec = defaults->memory_pressure_threshold_usec;
309✔
4303

4304
        free_and_replace(m->defaults.smack_process_label, label);
309✔
4305
        rlimit_free_all(m->defaults.rlimit);
309✔
4306
        memcpy(m->defaults.rlimit, rlimit, sizeof(struct rlimit*) * _RLIMIT_MAX);
309✔
4307

4308
        return 0;
309✔
4309
}
4310

4311
void manager_recheck_dbus(Manager *m) {
97,771✔
4312
        assert(m);
97,771✔
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))
97,771✔
4320
                return; /* don't check while we are reloading… */
4321

4322
        if (manager_dbus_is_running(m, false)) {
84,944✔
4323
                (void) bus_init_api(m);
11,122✔
4324

4325
                if (MANAGER_IS_SYSTEM(m))
11,122✔
4326
                        (void) bus_init_system(m);
5,113✔
4327
        } else {
4328
                bus_done_api(m);
73,822✔
4329

4330
                if (MANAGER_IS_SYSTEM(m))
73,822✔
4331
                        bus_done_system(m);
10,983✔
4332
        }
4333
}
4334

4335
static bool manager_journal_is_running(Manager *m) {
18,249✔
4336
        Unit *u;
18,249✔
4337

4338
        assert(m);
18,249✔
4339

4340
        if (MANAGER_IS_TEST_RUN(m))
18,249✔
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))
18,249✔
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);
17,522✔
4349
        if (!u)
17,522✔
4350
                return false;
4351
        if (SOCKET(u)->state != SOCKET_RUNNING)
17,522✔
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,666✔
4356
        if (!u)
13,666✔
4357
                return false;
4358
        if (!UNIT_IS_ACTIVE_OR_RELOADING(unit_active_state(u)) || SERVICE(u)->state == SERVICE_EXITED)
13,666✔
4359
                return false;
483✔
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) {
97,771✔
4378

4379
        assert(m);
97,771✔
4380

4381
        /* Don't bother with this unless we are in the special situation of being PID 1 */
4382
        if (getpid_cached() != 1)
97,771✔
4383
                return;
4384

4385
        /* Don't check this while we are reloading, things might still change */
4386
        if (MANAGER_IS_RELOADING(m))
23,743✔
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));
15,605✔
4393
        log_open();
15,605✔
4394
}
4395

4396
static ShowStatus manager_get_show_status(Manager *m) {
13,421✔
4397
        assert(m);
13,421✔
4398

4399
        if (MANAGER_IS_USER(m))
13,421✔
4400
                return _SHOW_STATUS_INVALID;
4401

4402
        if (m->show_status_overridden != _SHOW_STATUS_INVALID)
13,420✔
4403
                return m->show_status_overridden;
4404

4405
        return m->show_status;
13,420✔
4406
}
4407

4408
bool manager_get_show_status_on(Manager *m) {
13,419✔
4409
        assert(m);
13,419✔
4410

4411
        return show_status_on(manager_get_show_status(m));
13,419✔
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) {
309✔
4422
        assert(m);
309✔
4423
        assert(reason);
309✔
4424
        assert(mode >= 0 && mode < _SHOW_STATUS_MAX);
309✔
4425

4426
        if (MANAGER_IS_USER(m))
309✔
4427
                return;
4428

4429
        if (mode == m->show_status)
86✔
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,657✔
4471
        static int last_errno = 0;
2,657✔
4472
        struct stat st;
2,657✔
4473
        int r;
2,657✔
4474

4475
        assert(m);
2,657✔
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,657✔
4493
                return m->confirm_spawn;
2,657✔
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) {
444✔
4516
        assert(m);
444✔
4517

4518
        if (!MANAGER_IS_SYSTEM(m))
444✔
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) {
19,326✔
4536
        assert(m);
19,326✔
4537

4538
        if (!MANAGER_IS_SYSTEM(m))
19,326✔
4539
                return false;
4540

4541
        if (m->no_console_output)
9,111✔
4542
                return false;
4543

4544
        if (!IN_SET(manager_state(m), MANAGER_INITIALIZING, MANAGER_STARTING, MANAGER_STOPPING))
9,044✔
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)
9,044✔
4549
                return false;
4550

4551
        if (type >= STATUS_TYPE_NOTICE && manager_get_show_status(m) != SHOW_STATUS_NO)
9,044✔
4552
                return true;
4553

4554
        return manager_get_show_status_on(m);
9,042✔
4555
}
4556

4557
void manager_status_printf(Manager *m, StatusType type, const char *status, const char *format, ...) {
19,326✔
4558
        va_list ap;
19,326✔
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))
19,326✔
4563
                return;
19,324✔
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) {
23,232✔
4576
        assert(m);
23,232✔
4577
        assert(path);
23,232✔
4578
        assert(t >= 0 && t < _UNIT_MOUNT_DEPENDENCY_TYPE_MAX);
23,232✔
4579

4580
        if (path_equal(path, "/"))
23,232✔
4581
                path = "";
956✔
4582

4583
        return hashmap_get(m->units_needing_mounts_for[t], path);
23,232✔
4584
}
4585

4586
int manager_update_failed_units(Manager *m, Unit *u, bool failed) {
202,236✔
4587
        unsigned size;
202,236✔
4588
        int r;
202,236✔
4589

4590
        assert(m);
202,236✔
4591
        assert(u->manager == m);
202,236✔
4592

4593
        size = set_size(m->failed_units);
202,236✔
4594

4595
        if (failed) {
202,236✔
4596
                r = set_ensure_put(&m->failed_units, NULL, u);
286✔
4597
                if (r < 0)
286✔
4598
                        return log_oom();
×
4599
        } else
4600
                (void) set_remove(m->failed_units, u);
201,950✔
4601

4602
        if (set_size(m->failed_units) != size)
202,236✔
4603
                bus_manager_send_change_signal(m);
572✔
4604

4605
        return 0;
4606
}
4607

4608
ManagerState manager_state(Manager *m) {
542,165✔
4609
        Unit *u;
542,165✔
4610

4611
        assert(m);
542,165✔
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);
542,165✔
4615
        if (u && unit_active_or_pending(u))
542,165✔
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)) {
415,140✔
4620

4621
                u = manager_get_unit(m, SPECIAL_BASIC_TARGET);
376,229✔
4622
                if (!u || !UNIT_IS_ACTIVE_OR_RELOADING(unit_active_state(u)))
376,229✔
4623
                        return MANAGER_INITIALIZING;
305,894✔
4624

4625
                return MANAGER_STARTING;
4626
        }
4627

4628
        if (MANAGER_IS_SYSTEM(m)) {
38,911✔
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);
2✔
4631
                if (u && unit_active_or_pending(u))
2✔
4632
                        return MANAGER_MAINTENANCE;
4633

4634
                u = manager_get_unit(m, SPECIAL_EMERGENCY_TARGET);
2✔
4635
                if (u && unit_active_or_pending(u))
2✔
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))
38,911✔
4641
                return MANAGER_DEGRADED;
3,631✔
4642

4643
        return MANAGER_RUNNING;
4644
}
4645

4646
static void manager_unref_uid_internal(
962✔
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;
962✔
4653

4654
        assert(uid_is_valid(uid));
962✔
4655
        assert(_clean_ipc);
962✔
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));
962✔
4667
        assert_cc(UID_INVALID == (uid_t) GID_INVALID);
962✔
4668

4669
        if (uid == 0) /* We don't keep track of root, and will never destroy it */
962✔
4670
                return;
4671

4672
        c = PTR_TO_UINT32(hashmap_get(uid_refs, UID_TO_PTR(uid)));
731✔
4673

4674
        n = c & ~DESTROY_IPC_FLAG;
731✔
4675
        assert(n > 0);
731✔
4676
        n--;
731✔
4677

4678
        if (destroy_now && n == 0) {
731✔
4679
                hashmap_remove(uid_refs, UID_TO_PTR(uid));
163✔
4680

4681
                if (c & DESTROY_IPC_FLAG) {
163✔
4682
                        log_debug("%s " UID_FMT " is no longer referenced, cleaning up its IPC.",
45✔
4683
                                  _clean_ipc == clean_ipc_by_uid ? "UID" : "GID",
4684
                                  uid);
4685
                        (void) _clean_ipc(uid);
30✔
4686
                }
4687
        } else {
4688
                c = n | (c & DESTROY_IPC_FLAG);
568✔
4689
                assert_se(hashmap_update(uid_refs, UID_TO_PTR(uid), UINT32_TO_PTR(c)) >= 0);
568✔
4690
        }
4691
}
4692

4693
void manager_unref_uid(Manager *m, uid_t uid, bool destroy_now) {
479✔
4694
        manager_unref_uid_internal(m->uid_refs, uid, destroy_now, clean_ipc_by_uid);
479✔
4695
}
479✔
4696

4697
void manager_unref_gid(Manager *m, gid_t gid, bool destroy_now) {
483✔
4698
        manager_unref_uid_internal(m->gid_refs, (uid_t) gid, destroy_now, clean_ipc_by_gid);
483✔
4699
}
483✔
4700

4701
static int manager_ref_uid_internal(
962✔
4702
                Hashmap **uid_refs,
4703
                uid_t uid,
4704
                bool clean_ipc) {
4705

4706
        uint32_t c, n;
962✔
4707
        int r;
962✔
4708

4709
        assert(uid_refs);
962✔
4710
        assert(uid_is_valid(uid));
962✔
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));
962✔
4717
        assert_cc(UID_INVALID == (uid_t) GID_INVALID);
962✔
4718

4719
        if (uid == 0) /* We don't keep track of root, and will never destroy it */
962✔
4720
                return 0;
4721

4722
        r = hashmap_ensure_allocated(uid_refs, &trivial_hash_ops);
731✔
4723
        if (r < 0)
731✔
4724
                return r;
4725

4726
        c = PTR_TO_UINT32(hashmap_get(*uid_refs, UID_TO_PTR(uid)));
731✔
4727

4728
        n = c & ~DESTROY_IPC_FLAG;
731✔
4729
        n++;
731✔
4730

4731
        if (n & DESTROY_IPC_FLAG) /* check for overflow */
731✔
4732
                return -EOVERFLOW;
4733

4734
        c = n | (c & DESTROY_IPC_FLAG) | (clean_ipc ? DESTROY_IPC_FLAG : 0);
731✔
4735

4736
        return hashmap_replace(*uid_refs, UID_TO_PTR(uid), UINT32_TO_PTR(c));
731✔
4737
}
4738

4739
int manager_ref_uid(Manager *m, uid_t uid, bool clean_ipc) {
479✔
4740
        return manager_ref_uid_internal(&m->uid_refs, uid, clean_ipc);
479✔
4741
}
4742

4743
int manager_ref_gid(Manager *m, gid_t gid, bool clean_ipc) {
483✔
4744
        return manager_ref_uid_internal(&m->gid_refs, (uid_t) gid, clean_ipc);
483✔
4745
}
4746

4747
static void manager_vacuum_uid_refs_internal(
1,634✔
4748
                Hashmap *uid_refs,
4749
                int (*_clean_ipc)(uid_t uid)) {
4750

4751
        void *p, *k;
1,634✔
4752

4753
        assert(_clean_ipc);
1,634✔
4754

4755
        HASHMAP_FOREACH_KEY(p, k, uid_refs) {
3,546✔
4756
                uint32_t c, n;
278✔
4757
                uid_t uid;
278✔
4758

4759
                uid = PTR_TO_UID(k);
278✔
4760
                c = PTR_TO_UINT32(p);
278✔
4761

4762
                n = c & ~DESTROY_IPC_FLAG;
278✔
4763
                if (n > 0)
278✔
4764
                        continue;
278✔
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,912✔
4774
        }
4775
}
1,634✔
4776

4777
static void manager_vacuum_uid_refs(Manager *m) {
817✔
4778
        manager_vacuum_uid_refs_internal(m->uid_refs, clean_ipc_by_uid);
817✔
4779
}
817✔
4780

4781
static void manager_vacuum_gid_refs(Manager *m) {
817✔
4782
        manager_vacuum_uid_refs_internal(m->gid_refs, clean_ipc_by_gid);
817✔
4783
}
817✔
4784

4785
static void manager_vacuum(Manager *m) {
817✔
4786
        assert(m);
817✔
4787

4788
        /* Release any dynamic users no longer referenced */
4789
        dynamic_user_vacuum(m, true);
817✔
4790

4791
        /* Release any references to UIDs/GIDs no longer referenced, and destroy any IPC owned by them */
4792
        manager_vacuum_uid_refs(m);
817✔
4793
        manager_vacuum_gid_refs(m);
817✔
4794

4795
        /* Release any runtimes no longer referenced */
4796
        exec_shared_runtime_vacuum(m);
817✔
4797
}
817✔
4798

4799
static int manager_dispatch_user_lookup_fd(sd_event_source *source, int fd, uint32_t revents, void *userdata) {
274✔
4800
        struct buffer {
274✔
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);
274✔
4807
        ssize_t l;
274✔
4808
        size_t n;
274✔
4809
        Unit *u;
274✔
4810

4811
        assert(source);
274✔
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);
274✔
4819
        if (l < 0) {
274✔
4820
                if (ERRNO_IS_TRANSIENT(errno))
×
4821
                        return 0;
274✔
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)) {
274✔
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) {
274✔
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)) {
274✔
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);
274✔
4842
        if (memchr(buffer.unit_name, 0, n)) {
274✔
4843
                log_warning("Received lookup message with embedded NUL character, ignoring.");
×
4844
                return 0;
×
4845
        }
4846

4847
        buffer.unit_name[n] = 0;
274✔
4848
        u = manager_get_unit(m, buffer.unit_name);
274✔
4849
        if (!u) {
274✔
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);
274✔
4855

4856
        unit_notify_user_lookup(u, buffer.uid, buffer.gid);
274✔
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,628✔
4861
        Manager *m = ASSERT_PTR(userdata);
2,628✔
4862
        usec_t ts[2] = {};
2,628✔
4863
        CMSG_BUFFER_TYPE(CMSG_SPACE(sizeof(struct ucred))) control;
2,628✔
4864
        struct msghdr msghdr = {
2,628✔
4865
                .msg_iov = &IOVEC_MAKE(ts, sizeof(ts)),
2,628✔
4866
                .msg_iovlen = 1,
4867
                .msg_control = &control,
4868
                .msg_controllen = sizeof(control),
4869
        };
4870
        ssize_t n;
2,628✔
4871

4872
        assert(source);
2,628✔
4873

4874
        n = recvmsg_safe(m->handoff_timestamp_fds[0], &msghdr, MSG_DONTWAIT|MSG_CMSG_CLOEXEC);
2,628✔
4875
        if (ERRNO_IS_NEG_TRANSIENT(n))
2,628✔
4876
                return 0; /* Spurious wakeup, try again */
2,628✔
4877
        if (n == -ECHRNG) {
2,628✔
4878
                log_warning_errno(n, "Got message with truncated control data (unexpected fds sent?), ignoring.");
×
4879
                return 0;
×
4880
        }
4881
        if (n == -EXFULL) {
2,628✔
4882
                log_warning_errno(n, "Got message with truncated payload data, ignoring.");
×
4883
                return 0;
×
4884
        }
4885
        if (n < 0)
2,628✔
4886
                return log_error_errno(n, "Failed to receive handoff timestamp message: %m");
×
4887

4888
        cmsg_close_all(&msghdr);
2,628✔
4889

4890
        if (n != sizeof(ts)) {
2,628✔
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,628✔
4896
        if (!ucred || !pid_is_valid(ucred->pid)) {
2,628✔
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,628✔
4902

4903
        _cleanup_free_ Unit **units = NULL;
2,628✔
4904
        int n_units = manager_get_units_for_pidref(m, &PIDREF_MAKE_FROM_PID(ucred->pid), &units);
2,628✔
4905
        if (n_units < 0) {
2,628✔
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,628✔
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,628✔
4915
                .realtime = ts[0],
2,628✔
4916
                .monotonic = ts[1],
2,628✔
4917
        };
4918

4919
        FOREACH_ARRAY(u, units, n_units) {
7,884✔
4920
                if (!UNIT_VTABLE(*u)->notify_handoff_timestamp)
5,256✔
4921
                        continue;
98✔
4922

4923
                UNIT_VTABLE(*u)->notify_handoff_timestamp(*u, ucred, &dt);
5,158✔
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) {
85✔
5066
        assert(m);
85✔
5067

5068
        m->n_on_console++;
85✔
5069
}
85✔
5070

5071
void manager_unref_console(Manager *m) {
85✔
5072

5073
        assert(m->n_on_console > 0);
85✔
5074
        m->n_on_console--;
85✔
5075

5076
        if (m->n_on_console == 0)
85✔
5077
                m->no_console_output = false; /* unset no_console_output flag, since the console is definitely free now */
71✔
5078
}
85✔
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,508✔
5134
        if (in_initrd() &&
3,508✔
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,890✔
5142
        int r;
1,890✔
5143

5144
        assert(m);
1,890✔
5145

5146
        if (m->idle_pipe[0] >= 0) {
1,890✔
5147
                assert(m->idle_pipe[1] >= 0);
808✔
5148
                assert(m->idle_pipe[2] >= 0);
808✔
5149
                assert(m->idle_pipe[3] >= 0);
808✔
5150
                return 0;
5151
        }
5152

5153
        assert(m->idle_pipe[1] < 0);
1,082✔
5154
        assert(m->idle_pipe[2] < 0);
1,082✔
5155
        assert(m->idle_pipe[3] < 0);
1,082✔
5156

5157
        r = RET_NERRNO(pipe2(m->idle_pipe + 0, O_NONBLOCK|O_CLOEXEC));
1,082✔
5158
        if (r < 0)
×
5159
                return r;
5160

5161
        r = RET_NERRNO(pipe2(m->idle_pipe + 2, O_NONBLOCK|O_CLOEXEC));
1,082✔
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,255✔
5171
        assert(defaults);
1,255✔
5172
        assert(scope >= 0);
1,255✔
5173
        assert(scope < _RUNTIME_SCOPE_MAX);
1,255✔
5174

5175
        *defaults = (UnitDefaults) {
2,510✔
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,255✔
5180
                .timeout_stop_usec = manager_default_timeout(scope),
1,255✔
5181
                .timeout_abort_usec = manager_default_timeout(scope),
1,255✔
5182
                .timeout_abort_set = false,
5183
                .device_timeout_usec = manager_default_timeout(scope),
1,255✔
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,255✔
5201

5202
void unit_defaults_done(UnitDefaults *defaults) {
1,255✔
5203
        assert(defaults);
1,255✔
5204

5205
        defaults->smack_process_label = mfree(defaults->smack_process_label);
1,255✔
5206
        rlimit_free_all(defaults->rlimit);
1,255✔
5207
}
1,255✔
5208

5209
LogTarget manager_get_executor_log_target(Manager *m) {
2,655✔
5210
        assert(m);
2,655✔
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,655✔
5214
                return LOG_TARGET_KMSG;
5215

5216
        return log_get_target();
2,189✔
5217
}
5218

5219
void manager_log_caller(Manager *manager, PidRef *caller, const char *method) {
95✔
5220
        _cleanup_free_ char *comm = NULL;
190✔
5221

5222
        assert(manager);
95✔
5223
        assert(pidref_is_set(caller));
95✔
5224
        assert(method);
95✔
5225

5226
        (void) pidref_get_comm(caller, &comm);
95✔
5227
        Unit *caller_unit = manager_get_unit_by_pidref(manager, caller);
95✔
5228

5229
        log_notice("%s requested from client PID " PID_FMT "%s%s%s%s%s%s...",
95✔
5230
                   method, caller->pid,
5231
                   comm ? " ('" : "", strempty(comm), comm ? "')" : "",
5232
                   caller_unit ? " (unit " : "", caller_unit ? caller_unit->id : "", caller_unit ? ")" : "");
5233
}
95✔
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);
205✔
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);
7,668✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc