]>
Commit | Line | Data |
---|---|---|
db9ecf05 | 1 | /* SPDX-License-Identifier: LGPL-2.1-or-later */ |
a7334b09 | 2 | |
400f1a33 LP |
3 | #include <fcntl.h> |
4 | #include <linux/kd.h> | |
400f1a33 | 5 | #include <sys/inotify.h> |
e1414003 | 6 | #include <sys/ioctl.h> |
ca6ce62d | 7 | #include <sys/mount.h> |
400f1a33 | 8 | #include <sys/reboot.h> |
400f1a33 LP |
9 | #include <sys/wait.h> |
10 | #include <unistd.h> | |
830f6caa | 11 | |
6ae1ba8a | 12 | #include "sd-bus.h" |
718db961 | 13 | #include "sd-daemon.h" |
718db961 | 14 | #include "sd-messages.h" |
3536f49e | 15 | #include "sd-path.h" |
81527be1 | 16 | |
57b7a260 | 17 | #include "all-units.h" |
d68c645b | 18 | #include "alloc-util.h" |
836e4e7e | 19 | #include "architecture.h" |
400f1a33 LP |
20 | #include "audit-fd.h" |
21 | #include "boot-timestamps.h" | |
836e4e7e | 22 | #include "bpf-restrict-fs.h" |
2287565d | 23 | #include "build-path.h" |
400f1a33 LP |
24 | #include "bus-common-errors.h" |
25 | #include "bus-error.h" | |
00d9ef85 | 26 | #include "clean-ipc.h" |
29e6b0c1 | 27 | #include "common-signal.h" |
08951245 | 28 | #include "confidential-virt.h" |
28db6fbf | 29 | #include "constants.h" |
786d19fd | 30 | #include "creds-util.h" |
b377a7cc | 31 | #include "daemon-util.h" |
400f1a33 LP |
32 | #include "dbus-job.h" |
33 | #include "dbus-manager.h" | |
34 | #include "dbus-unit.h" | |
836e4e7e | 35 | #include "dbus.h" |
d063a527 | 36 | #include "dirent-util.h" |
4ea4abb6 | 37 | #include "dynamic-user.h" |
400f1a33 | 38 | #include "env-util.h" |
4f5dd394 | 39 | #include "escape.h" |
ec75e8e0 | 40 | #include "event-util.h" |
89711996 | 41 | #include "exec-util.h" |
d3070fbd | 42 | #include "execute.h" |
400f1a33 | 43 | #include "exit-status.h" |
3ffd4af2 | 44 | #include "fd-util.h" |
836e4e7e DDM |
45 | #include "fdset.h" |
46 | #include "format-util.h" | |
47 | #include "fs-util.h" | |
385093b7 | 48 | #include "generator-setup.h" |
60918275 | 49 | #include "hashmap.h" |
baa6a42d | 50 | #include "initrd-util.h" |
9e5fd717 | 51 | #include "inotify-util.h" |
5cfa33e0 | 52 | #include "install.h" |
19d22d43 | 53 | #include "io-util.h" |
817062e6 | 54 | #include "iovec-util.h" |
cdd5fac0 | 55 | #include "libaudit-util.h" |
786d19fd | 56 | #include "locale-setup.h" |
16354eff | 57 | #include "log.h" |
2a341bb9 | 58 | #include "manager-dump.h" |
a01ba4b2 | 59 | #include "manager-serialize.h" |
836e4e7e | 60 | #include "manager.h" |
35cd0ba5 | 61 | #include "mkdir-label.h" |
ca6ce62d | 62 | #include "mount-util.h" |
5d09689b | 63 | #include "notify-recv.h" |
6bedfcbb | 64 | #include "parse-util.h" |
400f1a33 LP |
65 | #include "path-lookup.h" |
66 | #include "path-util.h" | |
aa25e19b | 67 | #include "plymouth-util.h" |
d61a4dbb | 68 | #include "pretty-print.h" |
836e4e7e | 69 | #include "prioq.h" |
400f1a33 | 70 | #include "process-util.h" |
6bb00842 | 71 | #include "psi-util.h" |
ea430986 | 72 | #include "ratelimit.h" |
31ce987c | 73 | #include "rlimit-util.h" |
c6878637 | 74 | #include "rm-rf.h" |
45ae2f72 | 75 | #include "selinux-util.h" |
5d1e57b8 | 76 | #include "serialize.h" |
836e4e7e | 77 | #include "set.h" |
400f1a33 | 78 | #include "signal-util.h" |
57b7a260 | 79 | #include "socket-util.h" |
514f4ef5 | 80 | #include "special.h" |
8fcde012 | 81 | #include "stat-util.h" |
8b43440b | 82 | #include "string-table.h" |
07630cea | 83 | #include "string-util.h" |
400f1a33 | 84 | #include "strv.h" |
dd1db3c2 | 85 | #include "strxcpyx.h" |
6123dfaa | 86 | #include "sysctl-util.h" |
a6ecbf83 | 87 | #include "syslog-util.h" |
d851637c | 88 | #include "taint.h" |
400f1a33 LP |
89 | #include "terminal-util.h" |
90 | #include "time-util.h" | |
91 | #include "transaction.h" | |
affb60b1 | 92 | #include "umask-util.h" |
400f1a33 | 93 | #include "unit-name.h" |
00d9ef85 | 94 | #include "user-util.h" |
bad578b1 | 95 | #include "varlink.h" |
5dc4c17f | 96 | #include "virt.h" |
e96d6be7 | 97 | #include "watchdog.h" |
60918275 | 98 | |
e634df9f MY |
99 | /* Make sure clients notifying us don't block */ |
100 | #define MANAGER_SOCKET_RCVBUF_SIZE (8*U64_MB) | |
a47806fa | 101 | |
03b717a3 | 102 | /* Initial delay and the interval for printing status messages about running jobs */ |
1b4154a8 ZJS |
103 | #define JOBS_IN_PROGRESS_WAIT_USEC (2*USEC_PER_SEC) |
104 | #define JOBS_IN_PROGRESS_QUIET_WAIT_USEC (25*USEC_PER_SEC) | |
fd08a840 | 105 | #define JOBS_IN_PROGRESS_PERIOD_USEC (USEC_PER_SEC / 3) |
03b717a3 MS |
106 | #define JOBS_IN_PROGRESS_PERIOD_DIVISOR 3 |
107 | ||
5238e957 | 108 | /* If there are more than 1K bus messages queue across our API and direct buses, then let's not add more on top until |
e0a08581 LP |
109 | * the queue gets more empty. */ |
110 | #define MANAGER_BUS_BUSY_THRESHOLD 1024LU | |
111 | ||
112 | /* How many units and jobs to process of the bus queue before returning to the event loop. */ | |
113 | #define MANAGER_BUS_MESSAGE_BUDGET 100U | |
114 | ||
94f0b13b | 115 | #define DEFAULT_TASKS_MAX ((CGroupTasksMax) { 15U, 100U }) /* 15% */ |
ea09a416 | 116 | |
718db961 LP |
117 | static int manager_dispatch_notify_fd(sd_event_source *source, int fd, uint32_t revents, void *userdata); |
118 | static int manager_dispatch_signal_fd(sd_event_source *source, int fd, uint32_t revents, void *userdata); | |
119 | static int manager_dispatch_time_change_fd(sd_event_source *source, int fd, uint32_t revents, void *userdata); | |
120 | static int manager_dispatch_idle_pipe_fd(sd_event_source *source, int fd, uint32_t revents, void *userdata); | |
00d9ef85 | 121 | static int manager_dispatch_user_lookup_fd(sd_event_source *source, int fd, uint32_t revents, void *userdata); |
817062e6 | 122 | static int manager_dispatch_handoff_timestamp_fd(sd_event_source *source, int fd, uint32_t revents, void *userdata); |
406f1775 | 123 | static int manager_dispatch_pidref_transport_fd(sd_event_source *source, int fd, uint32_t revents, void *userdata); |
718db961 | 124 | static int manager_dispatch_jobs_in_progress(sd_event_source *source, usec_t usec, void *userdata); |
752b5905 | 125 | static int manager_dispatch_run_queue(sd_event_source *source, void *userdata); |
575b300b | 126 | static int manager_dispatch_sigchld(sd_event_source *source, void *userdata); |
bbf5fd8e | 127 | static int manager_dispatch_timezone_change(sd_event_source *source, const struct inotify_event *event, void *userdata); |
64691d20 | 128 | static int manager_run_environment_generators(Manager *m); |
e801700e | 129 | static int manager_run_generators(Manager *m); |
06a4eb07 | 130 | static void manager_vacuum(Manager *m); |
718db961 | 131 | |
1b4154a8 | 132 | static usec_t manager_watch_jobs_next_time(Manager *m) { |
3889fc6f ZJS |
133 | usec_t timeout; |
134 | ||
135 | if (MANAGER_IS_USER(m)) | |
136 | /* Let the user manager without a timeout show status quickly, so the system manager can make | |
137 | * use of it, if it wants to. */ | |
138 | timeout = JOBS_IN_PROGRESS_WAIT_USEC * 2 / 3; | |
139 | else if (show_status_on(m->show_status)) | |
140 | /* When status is on, just use the usual timeout. */ | |
141 | timeout = JOBS_IN_PROGRESS_WAIT_USEC; | |
142 | else | |
143 | timeout = JOBS_IN_PROGRESS_QUIET_WAIT_USEC; | |
144 | ||
145 | return usec_add(now(CLOCK_MONOTONIC), timeout); | |
1b4154a8 ZJS |
146 | } |
147 | ||
b646fc32 LB |
148 | static bool manager_is_confirm_spawn_disabled(Manager *m) { |
149 | assert(m); | |
150 | ||
151 | if (!m->confirm_spawn) | |
152 | return true; | |
153 | ||
154 | return access("/run/systemd/confirm_spawn_disabled", F_OK) >= 0; | |
155 | } | |
156 | ||
2ae56591 | 157 | static void manager_watch_jobs_in_progress(Manager *m) { |
e5723c89 | 158 | usec_t next; |
cfa9677b | 159 | int r; |
e5723c89 | 160 | |
718db961 | 161 | assert(m); |
03b717a3 | 162 | |
42bf1ae1 FB |
163 | /* We do not want to show the cylon animation if the user |
164 | * needs to confirm service executions otherwise confirmation | |
165 | * messages will be screwed by the cylon animation. */ | |
b0eb2944 | 166 | if (!manager_is_confirm_spawn_disabled(m)) |
42bf1ae1 FB |
167 | return; |
168 | ||
718db961 | 169 | if (m->jobs_in_progress_event_source) |
2ae56591 | 170 | return; |
03b717a3 | 171 | |
1b4154a8 | 172 | next = manager_watch_jobs_next_time(m); |
cfa9677b | 173 | r = sd_event_add_time( |
6a0f1f6d LP |
174 | m->event, |
175 | &m->jobs_in_progress_event_source, | |
176 | CLOCK_MONOTONIC, | |
177 | next, 0, | |
178 | manager_dispatch_jobs_in_progress, m); | |
cfa9677b MM |
179 | if (r < 0) |
180 | return; | |
7dfbe2e3 TG |
181 | |
182 | (void) sd_event_source_set_description(m->jobs_in_progress_event_source, "manager-jobs-in-progress"); | |
03b717a3 MS |
183 | } |
184 | ||
1addc46c | 185 | static void manager_flip_auto_status(Manager *m, bool enable, const char *reason) { |
f755e3b7 LP |
186 | assert(m); |
187 | ||
cb8ccb22 ZJS |
188 | if (enable) { |
189 | if (m->show_status == SHOW_STATUS_AUTO) | |
7365a296 | 190 | manager_set_show_status(m, SHOW_STATUS_TEMPORARY, reason); |
cb8ccb22 ZJS |
191 | } else { |
192 | if (m->show_status == SHOW_STATUS_TEMPORARY) | |
7365a296 | 193 | manager_set_show_status(m, SHOW_STATUS_AUTO, reason); |
cb8ccb22 ZJS |
194 | } |
195 | } | |
196 | ||
03b717a3 | 197 | static void manager_print_jobs_in_progress(Manager *m) { |
03b717a3 | 198 | Job *j; |
03b717a3 MS |
199 | unsigned counter = 0, print_nr; |
200 | char cylon[6 + CYLON_BUFFER_EXTRA + 1]; | |
201 | unsigned cylon_pos; | |
4c989f89 | 202 | uint64_t timeout = 0; |
03b717a3 | 203 | |
718db961 | 204 | assert(m); |
9c3349e2 | 205 | assert(m->n_running_jobs > 0); |
718db961 | 206 | |
7365a296 | 207 | manager_flip_auto_status(m, true, "delay"); |
d450b6f2 | 208 | |
03b717a3 MS |
209 | print_nr = (m->jobs_in_progress_iteration / JOBS_IN_PROGRESS_PERIOD_DIVISOR) % m->n_running_jobs; |
210 | ||
90e74a66 | 211 | HASHMAP_FOREACH(j, m->jobs) |
03b717a3 MS |
212 | if (j->state == JOB_RUNNING && counter++ == print_nr) |
213 | break; | |
214 | ||
e970a72e MS |
215 | /* m->n_running_jobs must be consistent with the contents of m->jobs, |
216 | * so the above loop must have succeeded in finding j. */ | |
217 | assert(counter == print_nr + 1); | |
51d122af | 218 | assert(j); |
5a82a91a | 219 | |
03b717a3 MS |
220 | cylon_pos = m->jobs_in_progress_iteration % 14; |
221 | if (cylon_pos >= 8) | |
222 | cylon_pos = 14 - cylon_pos; | |
223 | draw_cylon(cylon, sizeof(cylon), 6, cylon_pos); | |
224 | ||
8bb310c3 ZJS |
225 | m->jobs_in_progress_iteration++; |
226 | ||
3889fc6f | 227 | char job_of_n[STRLEN("( of ) ") + DECIMAL_STR_MAX(unsigned)*2] = ""; |
5291f26d | 228 | if (m->n_running_jobs > 1) |
3889fc6f | 229 | xsprintf(job_of_n, "(%u of %u) ", counter, m->n_running_jobs); |
03b717a3 | 230 | |
4c989f89 | 231 | (void) job_get_timeout(j, &timeout); |
8bb310c3 | 232 | |
04d232d8 ZJS |
233 | /* We want to use enough information for the user to identify previous lines talking about the same |
234 | * unit, but keep the message as short as possible. So if 'Starting foo.service' or 'Starting | |
3889fc6f | 235 | * foo.service - Description' were used, 'foo.service' is enough here. On the other hand, if we used |
04d232d8 ZJS |
236 | * 'Starting Description' before, then we shall also use 'Description' here. So we pass NULL as the |
237 | * second argument to unit_status_string(). */ | |
238 | const char *ident = unit_status_string(j->unit, NULL); | |
239 | ||
3889fc6f | 240 | const char *time = FORMAT_TIMESPAN(now(CLOCK_MONOTONIC) - j->begin_usec, 1*USEC_PER_SEC); |
4c989f89 | 241 | const char *limit = timeout > 0 ? FORMAT_TIMESPAN(timeout - j->begin_usec, 1*USEC_PER_SEC) : "no limit"; |
3889fc6f ZJS |
242 | |
243 | if (m->status_unit_format == STATUS_UNIT_FORMAT_DESCRIPTION) | |
244 | /* When using 'Description', we effectively don't have enough space to show the nested status | |
245 | * without ellipsization, so let's not even try. */ | |
246 | manager_status_printf(m, STATUS_TYPE_EPHEMERAL, cylon, | |
247 | "%sA %s job is running for %s (%s / %s)", | |
248 | job_of_n, | |
249 | job_type_to_string(j->type), | |
250 | ident, | |
251 | time, limit); | |
252 | else { | |
253 | const char *status_text = unit_status_text(j->unit); | |
254 | ||
255 | manager_status_printf(m, STATUS_TYPE_EPHEMERAL, cylon, | |
256 | "%sJob %s/%s running (%s / %s)%s%s", | |
257 | job_of_n, | |
258 | ident, | |
259 | job_type_to_string(j->type), | |
260 | time, limit, | |
261 | status_text ? ": " : "", | |
262 | strempty(status_text)); | |
263 | } | |
264 | ||
fb44dc64 LP |
265 | (void) sd_notifyf(/* unset_environment= */ false, |
266 | "STATUS=%sUser job %s/%s running (%s / %s)...", | |
267 | job_of_n, | |
268 | ident, job_type_to_string(j->type), | |
269 | time, limit); | |
3889fc6f | 270 | m->status_ready = false; |
03b717a3 MS |
271 | } |
272 | ||
e46b13c8 | 273 | static int have_ask_password(void) { |
c2b2df60 | 274 | _cleanup_closedir_ DIR *dir = NULL; |
e46b13c8 ZJS |
275 | |
276 | dir = opendir("/run/systemd/ask-password"); | |
277 | if (!dir) { | |
278 | if (errno == ENOENT) | |
279 | return false; | |
298507b2 LP |
280 | |
281 | return -errno; | |
e46b13c8 ZJS |
282 | } |
283 | ||
298507b2 LP |
284 | FOREACH_DIRENT_ALL(de, dir, return -errno) { |
285 | if (!IN_SET(de->d_type, DT_REG, DT_UNKNOWN)) | |
286 | continue; | |
287 | ||
e46b13c8 ZJS |
288 | if (startswith(de->d_name, "ask.")) |
289 | return true; | |
298507b2 LP |
290 | } |
291 | ||
8fb3f009 | 292 | return false; |
e46b13c8 ZJS |
293 | } |
294 | ||
295 | static int manager_dispatch_ask_password_fd(sd_event_source *source, | |
296 | int fd, uint32_t revents, void *userdata) { | |
99534007 | 297 | Manager *m = ASSERT_PTR(userdata); |
e46b13c8 | 298 | |
665dfe93 | 299 | (void) flush_fd(fd); |
e46b13c8 ZJS |
300 | |
301 | m->have_ask_password = have_ask_password(); | |
302 | if (m->have_ask_password < 0) | |
298507b2 LP |
303 | /* Log error but continue. Negative have_ask_password is treated as unknown status. */ |
304 | log_warning_errno(m->have_ask_password, "Failed to list /run/systemd/ask-password/, ignoring: %m"); | |
e46b13c8 ZJS |
305 | |
306 | return 0; | |
307 | } | |
308 | ||
309 | static void manager_close_ask_password(Manager *m) { | |
310 | assert(m); | |
311 | ||
5dcadb4c | 312 | m->ask_password_event_source = sd_event_source_disable_unref(m->ask_password_event_source); |
e46b13c8 ZJS |
313 | m->have_ask_password = -EINVAL; |
314 | } | |
315 | ||
316 | static int manager_check_ask_password(Manager *m) { | |
317 | int r; | |
318 | ||
319 | assert(m); | |
320 | ||
298507b2 LP |
321 | /* We only care about passwords prompts when running in system mode (because that's the only time we |
322 | * manage a console) */ | |
323 | if (!MANAGER_IS_SYSTEM(m)) | |
324 | return 0; | |
325 | ||
e46b13c8 | 326 | if (!m->ask_password_event_source) { |
298507b2 LP |
327 | _cleanup_close_ int inotify_fd = inotify_init1(IN_NONBLOCK|IN_CLOEXEC); |
328 | if (inotify_fd < 0) | |
329 | return log_error_errno(errno, "Failed to create inotify object: %m"); | |
e46b13c8 | 330 | |
e75fbee6 | 331 | (void) mkdir_label("/run/systemd/ask-password", 0755); |
298507b2 LP |
332 | r = inotify_add_watch_and_warn(inotify_fd, "/run/systemd/ask-password", IN_CLOSE_WRITE|IN_DELETE|IN_MOVED_TO|IN_ONLYDIR); |
333 | if (r < 0) | |
334 | return r; | |
e46b13c8 | 335 | |
298507b2 LP |
336 | _cleanup_(sd_event_source_disable_unrefp) sd_event_source *event_source = NULL; |
337 | r = sd_event_add_io( | |
338 | m->event, | |
339 | &event_source, | |
340 | inotify_fd, | |
341 | EPOLLIN, | |
342 | manager_dispatch_ask_password_fd, | |
343 | m); | |
344 | if (r < 0) | |
345 | return log_error_errno(r, "Failed to add event source for /run/systemd/ask-password/: %m"); | |
e46b13c8 | 346 | |
298507b2 LP |
347 | r = sd_event_source_set_io_fd_own(event_source, true); |
348 | if (r < 0) | |
349 | return log_error_errno(r, "Failed to pass ownership of /run/systemd/ask-password/ inotify fd to event source: %m"); | |
350 | TAKE_FD(inotify_fd); | |
351 | ||
352 | (void) sd_event_source_set_description(event_source, "manager-ask-password"); | |
e46b13c8 | 353 | |
298507b2 | 354 | m->ask_password_event_source = TAKE_PTR(event_source); |
7dfbe2e3 | 355 | |
e46b13c8 | 356 | /* Queries might have been added meanwhile... */ |
298507b2 | 357 | (void) manager_dispatch_ask_password_fd(m->ask_password_event_source, sd_event_source_get_io_fd(m->ask_password_event_source), EPOLLIN, m); |
e46b13c8 ZJS |
358 | } |
359 | ||
360 | return m->have_ask_password; | |
361 | } | |
362 | ||
31a7eb86 | 363 | static int manager_watch_idle_pipe(Manager *m) { |
31a7eb86 ZJS |
364 | int r; |
365 | ||
718db961 LP |
366 | assert(m); |
367 | ||
368 | if (m->idle_pipe_event_source) | |
31a7eb86 ZJS |
369 | return 0; |
370 | ||
371 | if (m->idle_pipe[2] < 0) | |
372 | return 0; | |
373 | ||
151b9b96 | 374 | r = sd_event_add_io(m->event, &m->idle_pipe_event_source, m->idle_pipe[2], EPOLLIN, manager_dispatch_idle_pipe_fd, m); |
23bbb0de MS |
375 | if (r < 0) |
376 | return log_error_errno(r, "Failed to watch idle pipe: %m"); | |
31a7eb86 | 377 | |
7dfbe2e3 TG |
378 | (void) sd_event_source_set_description(m->idle_pipe_event_source, "manager-idle-pipe"); |
379 | ||
31a7eb86 | 380 | return 0; |
31a7eb86 ZJS |
381 | } |
382 | ||
718db961 LP |
383 | static void manager_close_idle_pipe(Manager *m) { |
384 | assert(m); | |
31a7eb86 | 385 | |
5dcadb4c | 386 | m->idle_pipe_event_source = sd_event_source_disable_unref(m->idle_pipe_event_source); |
cd72bd8a | 387 | |
3d94f76c LP |
388 | safe_close_pair(m->idle_pipe); |
389 | safe_close_pair(m->idle_pipe + 2); | |
31a7eb86 ZJS |
390 | } |
391 | ||
8742514c | 392 | static int manager_setup_time_change(Manager *m) { |
718db961 | 393 | int r; |
b92bea5d | 394 | |
718db961 | 395 | assert(m); |
8742514c | 396 | |
638cece4 | 397 | if (MANAGER_IS_TEST_RUN(m)) |
0d8c31ff ZJS |
398 | return 0; |
399 | ||
5dcadb4c | 400 | m->time_change_event_source = sd_event_source_disable_unref(m->time_change_event_source); |
7feedd18 | 401 | |
ec75e8e0 | 402 | r = event_add_time_change(m->event, &m->time_change_event_source, manager_dispatch_time_change_fd, m); |
23bbb0de MS |
403 | if (r < 0) |
404 | return log_error_errno(r, "Failed to create time change event source: %m"); | |
8742514c | 405 | |
a5cc7e5a | 406 | /* Schedule this slightly earlier than the .timer event sources */ |
d42b61d2 | 407 | r = sd_event_source_set_priority(m->time_change_event_source, EVENT_PRIORITY_TIME_CHANGE); |
a5cc7e5a LP |
408 | if (r < 0) |
409 | return log_error_errno(r, "Failed to set priority of time change event sources: %m"); | |
410 | ||
8742514c LP |
411 | log_debug("Set up TFD_TIMER_CANCEL_ON_SET timerfd."); |
412 | ||
413 | return 0; | |
414 | } | |
415 | ||
bbf5fd8e LP |
416 | static int manager_read_timezone_stat(Manager *m) { |
417 | struct stat st; | |
418 | bool changed; | |
419 | ||
420 | assert(m); | |
421 | ||
422 | /* Read the current stat() data of /etc/localtime so that we detect changes */ | |
0dc39dff | 423 | if (lstat(etc_localtime(), &st) < 0) { |
bbf5fd8e LP |
424 | log_debug_errno(errno, "Failed to stat /etc/localtime, ignoring: %m"); |
425 | changed = m->etc_localtime_accessible; | |
426 | m->etc_localtime_accessible = false; | |
427 | } else { | |
428 | usec_t k; | |
429 | ||
430 | k = timespec_load(&st.st_mtim); | |
431 | changed = !m->etc_localtime_accessible || k != m->etc_localtime_mtime; | |
432 | ||
433 | m->etc_localtime_mtime = k; | |
434 | m->etc_localtime_accessible = true; | |
435 | } | |
436 | ||
437 | return changed; | |
438 | } | |
439 | ||
440 | static int manager_setup_timezone_change(Manager *m) { | |
a5cc7e5a | 441 | _cleanup_(sd_event_source_unrefp) sd_event_source *new_event = NULL; |
bbf5fd8e LP |
442 | int r; |
443 | ||
444 | assert(m); | |
445 | ||
638cece4 | 446 | if (MANAGER_IS_TEST_RUN(m)) |
bbf5fd8e LP |
447 | return 0; |
448 | ||
449 | /* We watch /etc/localtime for three events: change of the link count (which might mean removal from /etc even | |
450 | * though another link might be kept), renames, and file close operations after writing. Note we don't bother | |
451 | * with IN_DELETE_SELF, as that would just report when the inode is removed entirely, i.e. after the link count | |
452 | * went to zero and all fds to it are closed. | |
453 | * | |
454 | * Note that we never follow symlinks here. This is a simplification, but should cover almost all cases | |
455 | * correctly. | |
456 | * | |
457 | * Note that we create the new event source first here, before releasing the old one. This should optimize | |
458 | * behaviour as this way sd-event can reuse the old watch in case the inode didn't change. */ | |
459 | ||
0dc39dff | 460 | r = sd_event_add_inotify(m->event, &new_event, etc_localtime(), |
bbf5fd8e | 461 | IN_ATTRIB|IN_MOVE_SELF|IN_CLOSE_WRITE|IN_DONT_FOLLOW, manager_dispatch_timezone_change, m); |
0cb21d8c LP |
462 | if (r == -ENOENT) { |
463 | /* If the file doesn't exist yet, subscribe to /etc instead, and wait until it is created either by | |
464 | * O_CREATE or by rename() */ | |
0dc39dff | 465 | _cleanup_free_ char *localtime_dir = NULL; |
0cb21d8c | 466 | |
0dc39dff VD |
467 | int dir_r = path_extract_directory(etc_localtime(), &localtime_dir); |
468 | if (dir_r < 0) | |
469 | return log_error_errno(dir_r, "Failed to extract directory from path '%s': %m", etc_localtime()); | |
470 | ||
471 | log_debug_errno(r, "%s doesn't exist yet, watching %s instead.", etc_localtime(), localtime_dir); | |
472 | ||
473 | r = sd_event_add_inotify(m->event, &new_event, localtime_dir, | |
bbf5fd8e | 474 | IN_CREATE|IN_MOVED_TO|IN_ONLYDIR, manager_dispatch_timezone_change, m); |
0cb21d8c | 475 | } |
bbf5fd8e LP |
476 | if (r < 0) |
477 | return log_error_errno(r, "Failed to create timezone change event source: %m"); | |
478 | ||
a5cc7e5a | 479 | /* Schedule this slightly earlier than the .timer event sources */ |
d42b61d2 | 480 | r = sd_event_source_set_priority(new_event, EVENT_PRIORITY_TIME_ZONE); |
a5cc7e5a LP |
481 | if (r < 0) |
482 | return log_error_errno(r, "Failed to set priority of timezone change event sources: %m"); | |
483 | ||
bbf5fd8e | 484 | sd_event_source_unref(m->timezone_change_event_source); |
a5cc7e5a | 485 | m->timezone_change_event_source = TAKE_PTR(new_event); |
bbf5fd8e LP |
486 | |
487 | return 0; | |
488 | } | |
489 | ||
7eedec73 | 490 | static int manager_enable_special_signals(Manager *m) { |
254d1313 | 491 | _cleanup_close_ int fd = -EBADF; |
80876c20 LP |
492 | |
493 | assert(m); | |
494 | ||
7eedec73 | 495 | if (!MANAGER_IS_SYSTEM(m) || MANAGER_IS_TEST_RUN(m)) |
37453b3a EV |
496 | return 0; |
497 | ||
e62731f9 LP |
498 | /* Enable that we get SIGINT on control-alt-del. In containers this will fail with EPERM (older) or |
499 | * EINVAL (newer), so ignore that. */ | |
4c701096 | 500 | if (reboot(RB_DISABLE_CAD) < 0 && !IN_SET(errno, EPERM, EINVAL)) |
e62731f9 | 501 | log_warning_errno(errno, "Failed to enable ctrl-alt-del handling, ignoring: %m"); |
80876c20 | 502 | |
a41b539e | 503 | fd = open_terminal("/dev/tty0", O_RDWR|O_NOCTTY|O_CLOEXEC); |
e62731f9 LP |
504 | if (fd < 0) |
505 | /* Support systems without virtual console (ENOENT) gracefully */ | |
42ba9974 | 506 | log_full_errno(fd == -ENOENT ? LOG_DEBUG : LOG_WARNING, fd, "Failed to open %s, ignoring: %m", "/dev/tty0"); |
e62731f9 | 507 | else { |
80876c20 LP |
508 | /* Enable that we get SIGWINCH on kbrequest */ |
509 | if (ioctl(fd, KDSIGACCEPT, SIGWINCH) < 0) | |
e62731f9 | 510 | log_warning_errno(errno, "Failed to enable kbrequest handling, ignoring: %m"); |
80876c20 LP |
511 | } |
512 | ||
513 | return 0; | |
514 | } | |
515 | ||
ce578209 | 516 | static int manager_setup_signals(Manager *m) { |
5734893f | 517 | static const struct sigaction sa = { |
b92bea5d ZJS |
518 | .sa_handler = SIG_DFL, |
519 | .sa_flags = SA_NOCLDSTOP|SA_RESTART, | |
520 | }; | |
718db961 LP |
521 | sigset_t mask; |
522 | int r; | |
60918275 | 523 | |
ce578209 LP |
524 | assert(m); |
525 | ||
57c0c30e LP |
526 | assert_se(sigaction(SIGCHLD, &sa, NULL) == 0); |
527 | ||
9af70339 MY |
528 | /* We make liberal use of realtime signals here. On Linux/glibc we have 30 of them, between |
529 | * SIGRTMIN+0 ... SIGRTMIN+30 (aka SIGRTMAX). */ | |
7d793605 | 530 | |
4dffec14 | 531 | assert_se(sigemptyset(&mask) == 0); |
7d793605 LP |
532 | sigset_add_many(&mask, |
533 | SIGCHLD, /* Child died */ | |
534 | SIGTERM, /* Reexecute daemon */ | |
535 | SIGHUP, /* Reload configuration */ | |
54d04cd1 | 536 | SIGUSR1, /* systemd: reconnect to D-Bus */ |
7d793605 LP |
537 | SIGUSR2, /* systemd: dump status */ |
538 | SIGINT, /* Kernel sends us this on control-alt-del */ | |
539 | SIGWINCH, /* Kernel sends us this on kbrequest (alt-arrowup) */ | |
540 | SIGPWR, /* Some kernel drivers and upsd send us this on power failure */ | |
4dffec14 | 541 | |
7d793605 | 542 | SIGRTMIN+0, /* systemd: start default.target */ |
0003d1ab | 543 | SIGRTMIN+1, /* systemd: isolate rescue.target */ |
7d793605 LP |
544 | SIGRTMIN+2, /* systemd: isolate emergency.target */ |
545 | SIGRTMIN+3, /* systemd: start halt.target */ | |
546 | SIGRTMIN+4, /* systemd: start poweroff.target */ | |
547 | SIGRTMIN+5, /* systemd: start reboot.target */ | |
0003d1ab | 548 | SIGRTMIN+6, /* systemd: start kexec.target */ |
13ffc607 | 549 | SIGRTMIN+7, /* systemd: start soft-reboot.target */ |
4dffec14 LP |
550 | |
551 | /* ... space for more special targets ... */ | |
552 | ||
0003d1ab LP |
553 | SIGRTMIN+13, /* systemd: Immediate halt */ |
554 | SIGRTMIN+14, /* systemd: Immediate poweroff */ | |
555 | SIGRTMIN+15, /* systemd: Immediate reboot */ | |
556 | SIGRTMIN+16, /* systemd: Immediate kexec */ | |
13ffc607 | 557 | SIGRTMIN+17, /* systemd: Immediate soft-reboot */ |
29e6b0c1 LP |
558 | SIGRTMIN+18, /* systemd: control command */ |
559 | ||
560 | /* ... space ... */ | |
4dffec14 | 561 | |
0658666b LP |
562 | SIGRTMIN+20, /* systemd: enable status messages */ |
563 | SIGRTMIN+21, /* systemd: disable status messages */ | |
253ee27a LP |
564 | SIGRTMIN+22, /* systemd: set log level to LOG_DEBUG */ |
565 | SIGRTMIN+23, /* systemd: set log level to LOG_INFO */ | |
600b704e | 566 | SIGRTMIN+24, /* systemd: Immediate exit (--user only) */ |
463aef23 | 567 | SIGRTMIN+25, /* systemd: reexecute manager */ |
4dffec14 | 568 | |
9af70339 MY |
569 | SIGRTMIN+26, /* systemd: set log target to journal-or-kmsg */ |
570 | SIGRTMIN+27, /* systemd: set log target to console */ | |
571 | SIGRTMIN+28, /* systemd: set log target to kmsg */ | |
572 | SIGRTMIN+29, /* systemd: set log target to syslog-or-kmsg (obsolete) */ | |
4dffec14 LP |
573 | |
574 | /* ... one free signal here SIGRTMIN+30 ... */ | |
7d793605 | 575 | -1); |
ce578209 LP |
576 | assert_se(sigprocmask(SIG_SETMASK, &mask, NULL) == 0); |
577 | ||
718db961 LP |
578 | m->signal_fd = signalfd(-1, &mask, SFD_NONBLOCK|SFD_CLOEXEC); |
579 | if (m->signal_fd < 0) | |
ce578209 LP |
580 | return -errno; |
581 | ||
151b9b96 | 582 | r = sd_event_add_io(m->event, &m->signal_event_source, m->signal_fd, EPOLLIN, manager_dispatch_signal_fd, m); |
718db961 LP |
583 | if (r < 0) |
584 | return r; | |
ce578209 | 585 | |
7dfbe2e3 TG |
586 | (void) sd_event_source_set_description(m->signal_event_source, "manager-signal"); |
587 | ||
d8fdc620 LP |
588 | /* Process signals a bit earlier than the rest of things, but later than notify_fd processing, so that the |
589 | * notify processing can still figure out to which process/service a message belongs, before we reap the | |
590 | * process. Also, process this before handling cgroup notifications, so that we always collect child exit | |
591 | * status information before detecting that there's no process in a cgroup. */ | |
d42b61d2 | 592 | r = sd_event_source_set_priority(m->signal_event_source, EVENT_PRIORITY_SIGNALS); |
29083707 LP |
593 | if (r < 0) |
594 | return r; | |
595 | ||
6dfa0a9d LP |
596 | /* Report to supervisor that we now process the above signals. We report this as level "2", to |
597 | * indicate that we support more than sysvinit's signals (of course, sysvinit never sent this | |
598 | * message, but conceptually it makes sense to consider level "1" to be equivalent to sysvinit's | |
599 | * signal handling). Also, by setting this to "2" people looking for this hopefully won't | |
600 | * misunderstand this as a boolean concept. Signal level 2 shall refer to the signals PID 1 | |
601 | * understands at the time of release of systemd v256, i.e. including basic SIGRTMIN+18 handling for | |
602 | * memory pressure and stuff. When more signals are hooked up (or more SIGRTMIN+18 multiplex | |
bd6e5b4d | 603 | * operations added, this level should be increased). */ |
6dfa0a9d LP |
604 | (void) sd_notify(/* unset_environment= */ false, |
605 | "X_SYSTEMD_SIGNALS_LEVEL=2"); | |
606 | ||
7eedec73 | 607 | return manager_enable_special_signals(m); |
ce578209 LP |
608 | } |
609 | ||
1ad6e8b3 | 610 | static char** sanitize_environment(char **l) { |
f069efb4 | 611 | |
47cf8ff2 | 612 | /* Let's remove some environment variables that we need ourselves to communicate with our clients */ |
f069efb4 | 613 | strv_env_unset_many( |
1ad6e8b3 | 614 | l, |
8047ac8f LP |
615 | "CACHE_DIRECTORY", |
616 | "CONFIGURATION_DIRECTORY", | |
bb0c0d6f | 617 | "CREDENTIALS_DIRECTORY", |
47cf8ff2 LP |
618 | "EXIT_CODE", |
619 | "EXIT_STATUS", | |
620 | "INVOCATION_ID", | |
621 | "JOURNAL_STREAM", | |
622 | "LISTEN_FDNAMES", | |
623 | "LISTEN_FDS", | |
624 | "LISTEN_PID", | |
8047ac8f | 625 | "LOGS_DIRECTORY", |
c792a2e5 | 626 | "LOG_NAMESPACE", |
f069efb4 LP |
627 | "MAINPID", |
628 | "MANAGERPID", | |
6bb00842 LP |
629 | "MEMORY_PRESSURE_WATCH", |
630 | "MEMORY_PRESSURE_WRITE", | |
c792a2e5 LP |
631 | "MONITOR_EXIT_CODE", |
632 | "MONITOR_EXIT_STATUS", | |
633 | "MONITOR_INVOCATION_ID", | |
634 | "MONITOR_SERVICE_RESULT", | |
635 | "MONITOR_UNIT", | |
47cf8ff2 | 636 | "NOTIFY_SOCKET", |
dcf3c3c3 | 637 | "PIDFILE", |
47cf8ff2 LP |
638 | "REMOTE_ADDR", |
639 | "REMOTE_PORT", | |
8047ac8f | 640 | "RUNTIME_DIRECTORY", |
47cf8ff2 | 641 | "SERVICE_RESULT", |
8047ac8f | 642 | "STATE_DIRECTORY", |
c792a2e5 LP |
643 | "SYSTEMD_EXEC_PID", |
644 | "TRIGGER_PATH", | |
645 | "TRIGGER_TIMER_MONOTONIC_USEC", | |
646 | "TRIGGER_TIMER_REALTIME_USEC", | |
647 | "TRIGGER_UNIT", | |
f069efb4 | 648 | "WATCHDOG_PID", |
9128fd55 | 649 | "WATCHDOG_USEC"); |
47cf8ff2 LP |
650 | |
651 | /* Let's order the environment alphabetically, just to make it pretty */ | |
305757d8 | 652 | return strv_sort(l); |
f069efb4 LP |
653 | } |
654 | ||
79a224c4 | 655 | int manager_default_environment(Manager *m) { |
71ecc858 LP |
656 | assert(m); |
657 | ||
1ad6e8b3 LP |
658 | m->transient_environment = strv_free(m->transient_environment); |
659 | ||
463d0d15 | 660 | if (MANAGER_IS_SYSTEM(m)) { |
c792a2e5 LP |
661 | /* The system manager always starts with a clean environment for its children. It does not |
662 | * import the kernel's or the parents' exported variables. | |
e21fea24 | 663 | * |
c792a2e5 LP |
664 | * The initial passed environment is untouched to keep /proc/self/environ valid; it is used |
665 | * for tagging the init process inside containers. */ | |
0f36a4c8 ZJS |
666 | char *path = strjoin("PATH=", default_PATH()); |
667 | if (!path) | |
668 | return log_oom(); | |
669 | ||
670 | if (strv_consume(&m->transient_environment, path) < 0) | |
db11487d | 671 | return log_oom(); |
e21fea24 KS |
672 | |
673 | /* Import locale variables LC_*= from configuration */ | |
1ad6e8b3 | 674 | (void) locale_setup(&m->transient_environment); |
db11487d | 675 | } else { |
0ba24952 MY |
676 | /* The user manager passes its own environment along to its children, except for $PATH and |
677 | * session envs. */ | |
678 | ||
1ad6e8b3 | 679 | m->transient_environment = strv_copy(environ); |
db11487d ZJS |
680 | if (!m->transient_environment) |
681 | return log_oom(); | |
682 | ||
0f36a4c8 ZJS |
683 | char *path = strjoin("PATH=", default_user_PATH()); |
684 | if (!path) | |
685 | return log_oom(); | |
686 | ||
687 | if (strv_env_replace_consume(&m->transient_environment, path) < 0) | |
db11487d | 688 | return log_oom(); |
0ba24952 MY |
689 | |
690 | /* Envvars set for our 'manager' class session are private and should not be propagated | |
691 | * to children. Also it's likely that the graphical session will set these on their own. */ | |
692 | strv_env_unset_many(m->transient_environment, | |
693 | "XDG_SESSION_ID", | |
694 | "XDG_SESSION_CLASS", | |
695 | "XDG_SESSION_TYPE", | |
696 | "XDG_SESSION_DESKTOP", | |
697 | "XDG_SEAT", | |
698 | "XDG_VTNR"); | |
db11487d | 699 | } |
8b55b8c4 | 700 | |
1ad6e8b3 | 701 | sanitize_environment(m->transient_environment); |
e21fea24 | 702 | return 0; |
71ecc858 LP |
703 | } |
704 | ||
3536f49e YW |
705 | static int manager_setup_prefix(Manager *m) { |
706 | struct table_entry { | |
707 | uint64_t type; | |
708 | const char *suffix; | |
709 | }; | |
710 | ||
72fd1768 | 711 | static const struct table_entry paths_system[_EXEC_DIRECTORY_TYPE_MAX] = { |
17f6b640 YW |
712 | [EXEC_DIRECTORY_RUNTIME] = { SD_PATH_SYSTEM_RUNTIME, NULL }, |
713 | [EXEC_DIRECTORY_STATE] = { SD_PATH_SYSTEM_STATE_PRIVATE, NULL }, | |
714 | [EXEC_DIRECTORY_CACHE] = { SD_PATH_SYSTEM_STATE_CACHE, NULL }, | |
715 | [EXEC_DIRECTORY_LOGS] = { SD_PATH_SYSTEM_STATE_LOGS, NULL }, | |
3536f49e YW |
716 | [EXEC_DIRECTORY_CONFIGURATION] = { SD_PATH_SYSTEM_CONFIGURATION, NULL }, |
717 | }; | |
718 | ||
72fd1768 | 719 | static const struct table_entry paths_user[_EXEC_DIRECTORY_TYPE_MAX] = { |
17f6b640 YW |
720 | [EXEC_DIRECTORY_RUNTIME] = { SD_PATH_USER_RUNTIME, NULL }, |
721 | [EXEC_DIRECTORY_STATE] = { SD_PATH_USER_STATE_PRIVATE, NULL }, | |
722 | [EXEC_DIRECTORY_CACHE] = { SD_PATH_USER_STATE_CACHE, NULL }, | |
723 | [EXEC_DIRECTORY_LOGS] = { SD_PATH_USER_STATE_PRIVATE, "log" }, | |
9978e631 | 724 | [EXEC_DIRECTORY_CONFIGURATION] = { SD_PATH_USER_CONFIGURATION, NULL }, |
3536f49e YW |
725 | }; |
726 | ||
3536f49e YW |
727 | assert(m); |
728 | ||
24b45970 ZJS |
729 | const struct table_entry *p = MANAGER_IS_SYSTEM(m) ? paths_system : paths_user; |
730 | int r; | |
3536f49e | 731 | |
24b45970 | 732 | for (ExecDirectoryType i = 0; i < _EXEC_DIRECTORY_TYPE_MAX; i++) { |
51327bcc | 733 | r = sd_path_lookup(p[i].type, p[i].suffix, &m->prefix[i]); |
3536f49e | 734 | if (r < 0) |
998df7ce ZJS |
735 | return log_warning_errno(r, "Failed to lookup %s path: %m", |
736 | exec_directory_type_to_string(i)); | |
3536f49e YW |
737 | } |
738 | ||
739 | return 0; | |
740 | } | |
741 | ||
e8630e69 ZJS |
742 | static void manager_free_unit_name_maps(Manager *m) { |
743 | m->unit_id_map = hashmap_free(m->unit_id_map); | |
744 | m->unit_name_map = hashmap_free(m->unit_name_map); | |
3fb2326f | 745 | m->unit_path_cache = set_free(m->unit_path_cache); |
c2911d48 | 746 | m->unit_cache_timestamp_hash = 0; |
e8630e69 ZJS |
747 | } |
748 | ||
279d81dd LP |
749 | static int manager_setup_run_queue(Manager *m) { |
750 | int r; | |
751 | ||
752 | assert(m); | |
753 | assert(!m->run_queue_event_source); | |
754 | ||
755 | r = sd_event_add_defer(m->event, &m->run_queue_event_source, manager_dispatch_run_queue, m); | |
756 | if (r < 0) | |
757 | return r; | |
758 | ||
d42b61d2 | 759 | r = sd_event_source_set_priority(m->run_queue_event_source, EVENT_PRIORITY_RUN_QUEUE); |
279d81dd LP |
760 | if (r < 0) |
761 | return r; | |
762 | ||
763 | r = sd_event_source_set_enabled(m->run_queue_event_source, SD_EVENT_OFF); | |
764 | if (r < 0) | |
765 | return r; | |
766 | ||
767 | (void) sd_event_source_set_description(m->run_queue_event_source, "manager-run-queue"); | |
768 | ||
769 | return 0; | |
770 | } | |
771 | ||
575b300b LP |
772 | static int manager_setup_sigchld_event_source(Manager *m) { |
773 | int r; | |
774 | ||
775 | assert(m); | |
776 | assert(!m->sigchld_event_source); | |
777 | ||
778 | r = sd_event_add_defer(m->event, &m->sigchld_event_source, manager_dispatch_sigchld, m); | |
779 | if (r < 0) | |
780 | return r; | |
781 | ||
d42b61d2 | 782 | r = sd_event_source_set_priority(m->sigchld_event_source, EVENT_PRIORITY_SIGCHLD); |
575b300b LP |
783 | if (r < 0) |
784 | return r; | |
785 | ||
786 | r = sd_event_source_set_enabled(m->sigchld_event_source, SD_EVENT_OFF); | |
787 | if (r < 0) | |
788 | return r; | |
789 | ||
790 | (void) sd_event_source_set_description(m->sigchld_event_source, "manager-sigchld"); | |
791 | ||
792 | return 0; | |
793 | } | |
794 | ||
29e6b0c1 LP |
795 | int manager_setup_memory_pressure_event_source(Manager *m) { |
796 | int r; | |
797 | ||
798 | assert(m); | |
799 | ||
800 | m->memory_pressure_event_source = sd_event_source_disable_unref(m->memory_pressure_event_source); | |
801 | ||
802 | r = sd_event_add_memory_pressure(m->event, &m->memory_pressure_event_source, NULL, NULL); | |
803 | if (r < 0) | |
804 | log_full_errno(ERRNO_IS_NOT_SUPPORTED(r) || ERRNO_IS_PRIVILEGE(r) || (r == -EHOSTDOWN) ? LOG_DEBUG : LOG_NOTICE, r, | |
805 | "Failed to establish memory pressure event source, ignoring: %m"); | |
c9e120e0 | 806 | else if (m->defaults.memory_pressure_threshold_usec != USEC_INFINITY) { |
6bb00842 LP |
807 | |
808 | /* If there's a default memory pressure threshold set, also apply it to the service manager itself */ | |
809 | r = sd_event_source_set_memory_pressure_period( | |
810 | m->memory_pressure_event_source, | |
c9e120e0 | 811 | m->defaults.memory_pressure_threshold_usec, |
6bb00842 LP |
812 | MEMORY_PRESSURE_DEFAULT_WINDOW_USEC); |
813 | if (r < 0) | |
814 | log_warning_errno(r, "Failed to adjust memory pressure threshold, ignoring: %m"); | |
815 | } | |
29e6b0c1 LP |
816 | |
817 | return 0; | |
818 | } | |
819 | ||
2ad591a3 LP |
820 | static int manager_find_credentials_dirs(Manager *m) { |
821 | const char *e; | |
822 | int r; | |
823 | ||
824 | assert(m); | |
825 | ||
826 | r = get_credentials_dir(&e); | |
827 | if (r < 0) { | |
828 | if (r != -ENXIO) | |
829 | log_debug_errno(r, "Failed to determine credentials directory, ignoring: %m"); | |
830 | } else { | |
831 | m->received_credentials_directory = strdup(e); | |
832 | if (!m->received_credentials_directory) | |
833 | return -ENOMEM; | |
834 | } | |
835 | ||
836 | r = get_encrypted_credentials_dir(&e); | |
837 | if (r < 0) { | |
838 | if (r != -ENXIO) | |
839 | log_debug_errno(r, "Failed to determine encrypted credentials directory, ignoring: %m"); | |
840 | } else { | |
841 | m->received_encrypted_credentials_directory = strdup(e); | |
842 | if (!m->received_encrypted_credentials_directory) | |
843 | return -ENOMEM; | |
844 | } | |
845 | ||
846 | return 0; | |
847 | } | |
848 | ||
d35fe8c0 | 849 | void manager_set_switching_root(Manager *m, bool switching_root) { |
7fa49280 LP |
850 | assert(m); |
851 | ||
d35fe8c0 FB |
852 | m->switching_root = MANAGER_IS_SYSTEM(m) && switching_root; |
853 | } | |
854 | ||
cc156539 DDM |
855 | double manager_get_progress(Manager *m) { |
856 | assert(m); | |
857 | ||
858 | if (MANAGER_IS_FINISHED(m) || m->n_installed_jobs == 0) | |
859 | return 1.0; | |
860 | ||
861 | return 1.0 - ((double) hashmap_size(m->jobs) / (double) m->n_installed_jobs); | |
862 | } | |
863 | ||
a8157796 LP |
864 | static int compare_job_priority(const void *a, const void *b) { |
865 | const Job *x = a, *y = b; | |
866 | ||
867 | return unit_compare_priority(x->unit, y->unit); | |
868 | } | |
869 | ||
836e4e7e DDM |
870 | usec_t manager_default_timeout(RuntimeScope scope) { |
871 | return scope == RUNTIME_SCOPE_SYSTEM ? DEFAULT_TIMEOUT_USEC : DEFAULT_USER_TIMEOUT_USEC; | |
872 | } | |
873 | ||
5cacac04 | 874 | int manager_new(RuntimeScope runtime_scope, ManagerTestRunFlags test_run_flags, Manager **ret) { |
c70cac54 | 875 | _cleanup_(manager_freep) Manager *m = NULL; |
e3dd987c | 876 | int r; |
8e274523 | 877 | |
4870133b | 878 | assert(IN_SET(runtime_scope, RUNTIME_SCOPE_SYSTEM, RUNTIME_SCOPE_USER)); |
5cacac04 | 879 | assert(ret); |
ce578209 | 880 | |
3ad228ce | 881 | m = new(Manager, 1); |
915b3753 | 882 | if (!m) |
8e274523 | 883 | return -ENOMEM; |
60918275 | 884 | |
3ad228ce | 885 | *m = (Manager) { |
4870133b | 886 | .runtime_scope = runtime_scope, |
3ad228ce | 887 | .objective = _MANAGER_OBJECTIVE_INVALID, |
8c15bf36 | 888 | .previous_objective = _MANAGER_OBJECTIVE_INVALID, |
3ad228ce | 889 | |
36cf4507 ZJS |
890 | .status_unit_format = STATUS_UNIT_FORMAT_DEFAULT, |
891 | ||
3ad228ce LP |
892 | .original_log_level = -1, |
893 | .original_log_target = _LOG_TARGET_INVALID, | |
894 | ||
986935cf FB |
895 | .watchdog_overridden[WATCHDOG_RUNTIME] = USEC_INFINITY, |
896 | .watchdog_overridden[WATCHDOG_REBOOT] = USEC_INFINITY, | |
897 | .watchdog_overridden[WATCHDOG_KEXEC] = USEC_INFINITY, | |
5717062e | 898 | .watchdog_overridden[WATCHDOG_PRETIMEOUT] = USEC_INFINITY, |
986935cf | 899 | |
44a41954 FB |
900 | .show_status_overridden = _SHOW_STATUS_INVALID, |
901 | ||
254d1313 | 902 | .notify_fd = -EBADF, |
254d1313 | 903 | .signal_fd = -EBADF, |
71136404 | 904 | .user_lookup_fds = EBADF_PAIR, |
817062e6 | 905 | .handoff_timestamp_fds = EBADF_PAIR, |
406f1775 | 906 | .pidref_transport_fds = EBADF_PAIR, |
254d1313 ZJS |
907 | .private_listen_fd = -EBADF, |
908 | .dev_autofs_fd = -EBADF, | |
909 | .cgroup_inotify_fd = -EBADF, | |
910 | .pin_cgroupfs_fd = -EBADF, | |
254d1313 | 911 | .idle_pipe = { -EBADF, -EBADF, -EBADF, -EBADF}, |
3ad228ce LP |
912 | |
913 | /* start as id #1, so that we can leave #0 around as "null-like" value */ | |
914 | .current_job_id = 1, | |
915 | ||
916 | .have_ask_password = -EINVAL, /* we don't know */ | |
917 | .first_boot = -1, | |
918 | .test_run_flags = test_run_flags, | |
afcfaa69 | 919 | |
fed25720 | 920 | .dump_ratelimit = (const RateLimit) { .interval = 10 * USEC_PER_MINUTE, .burst = 10 }, |
bb5232b6 LB |
921 | |
922 | .executor_fd = -EBADF, | |
3ad228ce | 923 | }; |
80876c20 | 924 | |
ea09a416 LP |
925 | unit_defaults_init(&m->defaults, runtime_scope); |
926 | ||
349cc4a5 | 927 | #if ENABLE_EFI |
463d0d15 | 928 | if (MANAGER_IS_SYSTEM(m) && detect_container() <= 0) |
9f9f0342 LP |
929 | boot_timestamps(m->timestamps + MANAGER_TIMESTAMP_USERSPACE, |
930 | m->timestamps + MANAGER_TIMESTAMP_FIRMWARE, | |
931 | m->timestamps + MANAGER_TIMESTAMP_LOADER); | |
463d0d15 LP |
932 | #endif |
933 | ||
2e5c94b9 | 934 | /* Reboot immediately if the user hits C-A-D more often than 7x per 2s */ |
7d1e61ca | 935 | m->ctrl_alt_del_ratelimit = (const RateLimit) { .interval = 2 * USEC_PER_SEC, .burst = 7 }; |
2e5c94b9 | 936 | |
e21fea24 KS |
937 | r = manager_default_environment(m); |
938 | if (r < 0) | |
c70cac54 | 939 | return r; |
1137a57c | 940 | |
d5099efc | 941 | r = hashmap_ensure_allocated(&m->units, &string_hash_ops); |
718db961 | 942 | if (r < 0) |
c70cac54 | 943 | return r; |
60918275 | 944 | |
548f6937 | 945 | r = hashmap_ensure_allocated(&m->cgroup_unit, &path_hash_ops); |
718db961 | 946 | if (r < 0) |
c70cac54 | 947 | return r; |
9152c765 | 948 | |
d5099efc | 949 | r = hashmap_ensure_allocated(&m->watch_bus, &string_hash_ops); |
718db961 | 950 | if (r < 0) |
c70cac54 | 951 | return r; |
05e343b7 | 952 | |
da8e1782 MO |
953 | r = prioq_ensure_allocated(&m->run_queue, compare_job_priority); |
954 | if (r < 0) | |
955 | return r; | |
956 | ||
e8112e67 | 957 | r = manager_setup_prefix(m); |
718db961 | 958 | if (r < 0) |
c70cac54 | 959 | return r; |
8742514c | 960 | |
2ad591a3 LP |
961 | r = manager_find_credentials_dirs(m); |
962 | if (r < 0) | |
963 | return r; | |
bb0c0d6f | 964 | |
e8112e67 | 965 | r = sd_event_default(&m->event); |
8742514c | 966 | if (r < 0) |
c70cac54 | 967 | return r; |
9152c765 | 968 | |
e8112e67 | 969 | r = manager_setup_run_queue(m); |
a1d32bac | 970 | if (r < 0) |
c70cac54 | 971 | return r; |
8e274523 | 972 | |
5dd2f5ff | 973 | if (FLAGS_SET(test_run_flags, MANAGER_TEST_RUN_MINIMAL)) { |
e8112e67 ZJS |
974 | m->cgroup_root = strdup(""); |
975 | if (!m->cgroup_root) | |
976 | return -ENOMEM; | |
977 | } else { | |
978 | r = manager_setup_signals(m); | |
979 | if (r < 0) | |
980 | return r; | |
8c47c732 | 981 | |
e8112e67 ZJS |
982 | r = manager_setup_cgroup(m); |
983 | if (r < 0) | |
984 | return r; | |
575b300b | 985 | |
e8112e67 ZJS |
986 | r = manager_setup_time_change(m); |
987 | if (r < 0) | |
988 | return r; | |
9670d583 | 989 | |
bbf5fd8e LP |
990 | r = manager_read_timezone_stat(m); |
991 | if (r < 0) | |
992 | return r; | |
993 | ||
ea5c5f68 | 994 | (void) manager_setup_timezone_change(m); |
bbf5fd8e | 995 | |
e8112e67 ZJS |
996 | r = manager_setup_sigchld_event_source(m); |
997 | if (r < 0) | |
998 | return r; | |
b1994387 | 999 | |
29e6b0c1 LP |
1000 | r = manager_setup_memory_pressure_event_source(m); |
1001 | if (r < 0) | |
1002 | return r; | |
1003 | ||
b1994387 | 1004 | #if HAVE_LIBBPF |
352ec23c LP |
1005 | if (MANAGER_IS_SYSTEM(m) && bpf_restrict_fs_supported(/* initialize = */ true)) { |
1006 | r = bpf_restrict_fs_setup(m); | |
b1994387 | 1007 | if (r < 0) |
299d9417 | 1008 | log_warning_errno(r, "Failed to setup LSM BPF, ignoring: %m"); |
b1994387 ILG |
1009 | } |
1010 | #endif | |
e8112e67 | 1011 | } |
e27fe688 | 1012 | |
2f8c48b6 AZ |
1013 | if (test_run_flags == 0) { |
1014 | if (MANAGER_IS_SYSTEM(m)) | |
1015 | r = mkdir_label("/run/systemd/units", 0755); | |
1016 | else { | |
1017 | _cleanup_free_ char *units_path = NULL; | |
60cd6deb | 1018 | r = xdg_user_runtime_dir("/systemd/units", &units_path); |
2f8c48b6 AZ |
1019 | if (r < 0) |
1020 | return r; | |
bd6e5b4d | 1021 | |
e75fbee6 | 1022 | r = mkdir_label(units_path, 0755); |
2f8c48b6 | 1023 | } |
d3070fbd | 1024 | if (r < 0 && r != -EEXIST) |
c70cac54 | 1025 | return r; |
2287565d | 1026 | } |
bb5232b6 | 1027 | |
2287565d | 1028 | if (!FLAGS_SET(test_run_flags, MANAGER_TEST_DONT_OPEN_EXECUTOR)) { |
b157a7e6 | 1029 | m->executor_fd = pin_callout_binary(SYSTEMD_EXECUTOR_BINARY_PATH, &m->executor_path); |
bb5232b6 | 1030 | if (m->executor_fd < 0) |
2287565d | 1031 | return log_debug_errno(m->executor_fd, "Failed to pin executor binary: %m"); |
bb5232b6 | 1032 | |
b157a7e6 | 1033 | log_debug("Using systemd-executor binary from '%s'.", m->executor_path); |
d3070fbd LP |
1034 | } |
1035 | ||
232f6754 ZJS |
1036 | /* Note that we do not set up the notify fd here. We do that after deserialization, |
1037 | * since they might have gotten serialized across the reexec. */ | |
3536f49e | 1038 | |
5cacac04 | 1039 | *ret = TAKE_PTR(m); |
1cc6c93a | 1040 | |
8e274523 | 1041 | return 0; |
60918275 LP |
1042 | } |
1043 | ||
d86f9d52 | 1044 | static int manager_setup_notify(Manager *m) { |
7181dbdb | 1045 | int r; |
d86f9d52 | 1046 | |
638cece4 | 1047 | if (MANAGER_IS_TEST_RUN(m)) |
0d8c31ff ZJS |
1048 | return 0; |
1049 | ||
d86f9d52 | 1050 | if (m->notify_fd < 0) { |
254d1313 | 1051 | _cleanup_close_ int fd = -EBADF; |
f36a9d59 ZJS |
1052 | union sockaddr_union sa; |
1053 | socklen_t sa_len; | |
d86f9d52 LP |
1054 | |
1055 | /* First free all secondary fields */ | |
a1e58e8e | 1056 | m->notify_socket = mfree(m->notify_socket); |
5dcadb4c | 1057 | m->notify_event_source = sd_event_source_disable_unref(m->notify_event_source); |
d86f9d52 LP |
1058 | |
1059 | fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0); | |
4a62c710 MS |
1060 | if (fd < 0) |
1061 | return log_error_errno(errno, "Failed to allocate notification socket: %m"); | |
d86f9d52 | 1062 | |
e634df9f | 1063 | (void) fd_increase_rxbuf(fd, MANAGER_SOCKET_RCVBUF_SIZE); |
a47806fa | 1064 | |
b910cc72 | 1065 | m->notify_socket = path_join(m->prefix[EXEC_DIRECTORY_RUNTIME], "systemd/notify"); |
498e87d6 LP |
1066 | if (!m->notify_socket) |
1067 | return log_oom(); | |
1068 | ||
f36a9d59 ZJS |
1069 | r = sockaddr_un_set_path(&sa.un, m->notify_socket); |
1070 | if (r < 0) | |
1071 | return log_error_errno(r, "Notify socket '%s' not valid for AF_UNIX socket address, refusing.", | |
1072 | m->notify_socket); | |
1073 | sa_len = r; | |
15a3e96f | 1074 | |
fbda85b0 | 1075 | (void) sockaddr_un_unlink(&sa.un); |
7181dbdb | 1076 | |
45ae2f72 | 1077 | r = mac_selinux_bind(fd, &sa.sa, sa_len); |
4a62c710 | 1078 | if (r < 0) |
678f271c | 1079 | return log_error_errno(r, "Failed to bind notify fd to '%s': %m", m->notify_socket); |
d86f9d52 | 1080 | |
2ff48e98 | 1081 | r = setsockopt_int(fd, SOL_SOCKET, SO_PASSCRED, true); |
4a62c710 | 1082 | if (r < 0) |
678f271c | 1083 | return log_error_errno(r, "Failed to enable SO_PASSCRED for notify socket: %m"); |
d86f9d52 | 1084 | |
566b8f4d | 1085 | // TODO: enforce SO_PASSPIDFD when our baseline of the kernel version is bumped to >= 6.5. |
19eccb76 MY |
1086 | r = setsockopt_int(fd, SOL_SOCKET, SO_PASSPIDFD, true); |
1087 | if (r < 0 && r != -ENOPROTOOPT) | |
1088 | log_warning_errno(r, "Failed to enable SO_PASSPIDFD for notify socket, ignoring: %m"); | |
19eccb76 | 1089 | |
c10d6bdb | 1090 | m->notify_fd = TAKE_FD(fd); |
d86f9d52 LP |
1091 | |
1092 | log_debug("Using notification socket %s", m->notify_socket); | |
1093 | } | |
1094 | ||
1095 | if (!m->notify_event_source) { | |
151b9b96 | 1096 | r = sd_event_add_io(m->event, &m->notify_event_source, m->notify_fd, EPOLLIN, manager_dispatch_notify_fd, m); |
895b3a7b MS |
1097 | if (r < 0) |
1098 | return log_error_errno(r, "Failed to allocate notify event source: %m"); | |
d86f9d52 | 1099 | |
d8fdc620 LP |
1100 | /* Process notification messages a bit earlier than SIGCHLD, so that we can still identify to which |
1101 | * service an exit message belongs. */ | |
d42b61d2 | 1102 | r = sd_event_source_set_priority(m->notify_event_source, EVENT_PRIORITY_NOTIFY); |
23bbb0de MS |
1103 | if (r < 0) |
1104 | return log_error_errno(r, "Failed to set priority of notify event source: %m"); | |
7dfbe2e3 TG |
1105 | |
1106 | (void) sd_event_source_set_description(m->notify_event_source, "manager-notify"); | |
d86f9d52 LP |
1107 | } |
1108 | ||
1109 | return 0; | |
1110 | } | |
1111 | ||
00d9ef85 LP |
1112 | static int manager_setup_user_lookup_fd(Manager *m) { |
1113 | int r; | |
1114 | ||
1115 | assert(m); | |
1116 | ||
1117 | /* Set up the socket pair used for passing UID/GID resolution results from forked off processes to PID | |
1118 | * 1. Background: we can't do name lookups (NSS) from PID 1, since it might involve IPC and thus activation, | |
1119 | * and we might hence deadlock on ourselves. Hence we do all user/group lookups asynchronously from the forked | |
1120 | * off processes right before executing the binaries to start. In order to be able to clean up any IPC objects | |
1121 | * created by a unit (see RemoveIPC=) we need to know in PID 1 the used UID/GID of the executed processes, | |
1122 | * hence we establish this communication channel so that forked off processes can pass their UID/GID | |
1123 | * information back to PID 1. The forked off processes send their resolved UID/GID to PID 1 in a simple | |
1124 | * datagram, along with their unit name, so that we can share one communication socket pair among all units for | |
1125 | * this purpose. | |
1126 | * | |
1127 | * You might wonder why we need a communication channel for this that is independent of the usual notification | |
1128 | * socket scheme (i.e. $NOTIFY_SOCKET). The primary difference is about trust: data sent via the $NOTIFY_SOCKET | |
1129 | * channel is only accepted if it originates from the right unit and if reception was enabled for it. The user | |
1130 | * lookup socket OTOH is only accessible by PID 1 and its children until they exec(), and always available. | |
1131 | * | |
1132 | * Note that this function is called under two circumstances: when we first initialize (in which case we | |
1133 | * allocate both the socket pair and the event source to listen on it), and when we deserialize after a reload | |
1134 | * (in which case the socket pair already exists but we still need to allocate the event source for it). */ | |
1135 | ||
1136 | if (m->user_lookup_fds[0] < 0) { | |
1137 | ||
1138 | /* Free all secondary fields */ | |
1139 | safe_close_pair(m->user_lookup_fds); | |
5dcadb4c | 1140 | m->user_lookup_event_source = sd_event_source_disable_unref(m->user_lookup_event_source); |
00d9ef85 LP |
1141 | |
1142 | if (socketpair(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC, 0, m->user_lookup_fds) < 0) | |
1143 | return log_error_errno(errno, "Failed to allocate user lookup socket: %m"); | |
1144 | ||
85352c09 MY |
1145 | r = setsockopt_int(m->user_lookup_fds[0], SOL_SOCKET, SO_PASSRIGHTS, false); |
1146 | if (r < 0 && !ERRNO_IS_NEG_NOT_SUPPORTED(r)) | |
1147 | log_warning_errno(r, "Failed to turn off SO_PASSRIGHTS on user lookup socket, ignoring: %m"); | |
1148 | ||
e634df9f | 1149 | (void) fd_increase_rxbuf(m->user_lookup_fds[0], MANAGER_SOCKET_RCVBUF_SIZE); |
00d9ef85 LP |
1150 | } |
1151 | ||
1152 | if (!m->user_lookup_event_source) { | |
1153 | r = sd_event_add_io(m->event, &m->user_lookup_event_source, m->user_lookup_fds[0], EPOLLIN, manager_dispatch_user_lookup_fd, m); | |
1154 | if (r < 0) | |
af1690cf | 1155 | return log_error_errno(r, "Failed to allocate user lookup event source: %m"); |
00d9ef85 LP |
1156 | |
1157 | /* Process even earlier than the notify event source, so that we always know first about valid UID/GID | |
1158 | * resolutions */ | |
d42b61d2 | 1159 | r = sd_event_source_set_priority(m->user_lookup_event_source, EVENT_PRIORITY_USER_LOOKUP); |
00d9ef85 | 1160 | if (r < 0) |
af1690cf | 1161 | return log_error_errno(r, "Failed to set priority of user lookup event source: %m"); |
00d9ef85 LP |
1162 | |
1163 | (void) sd_event_source_set_description(m->user_lookup_event_source, "user-lookup"); | |
1164 | } | |
1165 | ||
1166 | return 0; | |
1167 | } | |
1168 | ||
817062e6 LP |
1169 | static int manager_setup_handoff_timestamp_fd(Manager *m) { |
1170 | int r; | |
1171 | ||
1172 | assert(m); | |
1173 | ||
c309b9e9 | 1174 | /* Set up the socket pair used for passing timestamps back when the executor processes we fork |
817062e6 LP |
1175 | * off invokes execve(), i.e. when we hand off control to our payload processes. */ |
1176 | ||
1177 | if (m->handoff_timestamp_fds[0] < 0) { | |
1178 | m->handoff_timestamp_event_source = sd_event_source_disable_unref(m->handoff_timestamp_event_source); | |
1179 | safe_close_pair(m->handoff_timestamp_fds); | |
1180 | ||
1181 | if (socketpair(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC, 0, m->handoff_timestamp_fds) < 0) | |
1182 | return log_error_errno(errno, "Failed to allocate handoff timestamp socket: %m"); | |
1183 | ||
1184 | /* Make sure children never have to block */ | |
e634df9f | 1185 | (void) fd_increase_rxbuf(m->handoff_timestamp_fds[0], MANAGER_SOCKET_RCVBUF_SIZE); |
817062e6 LP |
1186 | |
1187 | r = setsockopt_int(m->handoff_timestamp_fds[0], SOL_SOCKET, SO_PASSCRED, true); | |
1188 | if (r < 0) | |
85352c09 MY |
1189 | return log_error_errno(r, "Failed to enable SO_PASSCRED on handoff timestamp socket: %m"); |
1190 | ||
1191 | r = setsockopt_int(m->handoff_timestamp_fds[0], SOL_SOCKET, SO_PASSRIGHTS, false); | |
1192 | if (r < 0 && !ERRNO_IS_NEG_NOT_SUPPORTED(r)) | |
1193 | log_warning_errno(r, "Failed to turn off SO_PASSRIGHTS on handoff timestamp socket, ignoring: %m"); | |
817062e6 LP |
1194 | |
1195 | /* Mark the receiving socket as O_NONBLOCK (but leave sending side as-is) */ | |
1196 | r = fd_nonblock(m->handoff_timestamp_fds[0], true); | |
1197 | if (r < 0) | |
1198 | return log_error_errno(r, "Failed to make handoff timestamp socket O_NONBLOCK: %m"); | |
1199 | } | |
1200 | ||
1201 | if (!m->handoff_timestamp_event_source) { | |
1202 | r = sd_event_add_io(m->event, &m->handoff_timestamp_event_source, m->handoff_timestamp_fds[0], EPOLLIN, manager_dispatch_handoff_timestamp_fd, m); | |
1203 | if (r < 0) | |
af1690cf | 1204 | return log_error_errno(r, "Failed to allocate handoff timestamp event source: %m"); |
817062e6 LP |
1205 | |
1206 | r = sd_event_source_set_priority(m->handoff_timestamp_event_source, EVENT_PRIORITY_HANDOFF_TIMESTAMP); | |
1207 | if (r < 0) | |
af1690cf | 1208 | return log_error_errno(r, "Failed to set priority of handoff timestamp event source: %m"); |
817062e6 LP |
1209 | |
1210 | (void) sd_event_source_set_description(m->handoff_timestamp_event_source, "handoff-timestamp"); | |
1211 | } | |
1212 | ||
1213 | return 0; | |
1214 | } | |
1215 | ||
406f1775 DDM |
1216 | static int manager_setup_pidref_transport_fd(Manager *m) { |
1217 | int r; | |
1218 | ||
1219 | assert(m); | |
1220 | ||
1221 | /* Set up the socket pair used for passing parent and child pidrefs back when the executor unshares | |
1222 | * a PID namespace and forks again when using PrivatePIDs=yes. */ | |
1223 | ||
1224 | if (m->pidref_transport_fds[0] < 0) { | |
1225 | m->pidref_event_source = sd_event_source_disable_unref(m->pidref_event_source); | |
1226 | safe_close_pair(m->pidref_transport_fds); | |
1227 | ||
1228 | if (socketpair(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC, 0, m->pidref_transport_fds) < 0) | |
1229 | return log_error_errno(errno, "Failed to allocate pidref socket: %m"); | |
1230 | ||
1231 | /* Make sure children never have to block */ | |
1232 | (void) fd_increase_rxbuf(m->pidref_transport_fds[0], MANAGER_SOCKET_RCVBUF_SIZE); | |
1233 | ||
1234 | r = setsockopt_int(m->pidref_transport_fds[0], SOL_SOCKET, SO_PASSCRED, true); | |
1235 | if (r < 0) | |
1236 | return log_error_errno(r, "Failed to enable SO_PASSCRED for pidref socket: %m"); | |
1237 | ||
1238 | r = setsockopt_int(m->pidref_transport_fds[0], SOL_SOCKET, SO_PASSPIDFD, true); | |
1239 | if (ERRNO_IS_NEG_NOT_SUPPORTED(r)) | |
85352c09 | 1240 | log_debug_errno(r, "SO_PASSPIDFD is not supported for pidref socket, ignoring."); |
406f1775 DDM |
1241 | else if (r < 0) |
1242 | log_warning_errno(r, "Failed to enable SO_PASSPIDFD for pidref socket, ignoring: %m"); | |
1243 | ||
1244 | /* Mark the receiving socket as O_NONBLOCK (but leave sending side as-is) */ | |
1245 | r = fd_nonblock(m->pidref_transport_fds[0], true); | |
1246 | if (r < 0) | |
1247 | return log_error_errno(r, "Failed to make pidref socket O_NONBLOCK: %m"); | |
1248 | } | |
1249 | ||
1250 | if (!m->pidref_event_source) { | |
1251 | r = sd_event_add_io(m->event, &m->pidref_event_source, m->pidref_transport_fds[0], EPOLLIN, manager_dispatch_pidref_transport_fd, m); | |
1252 | if (r < 0) | |
1253 | return log_error_errno(r, "Failed to allocate pidref event source: %m"); | |
1254 | ||
1255 | r = sd_event_source_set_priority(m->pidref_event_source, EVENT_PRIORITY_PIDREF); | |
1256 | if (r < 0) | |
1257 | return log_error_errno(r, "Failed to set priority of pidref event source: %m"); | |
1258 | ||
1259 | (void) sd_event_source_set_description(m->pidref_event_source, "pidref"); | |
1260 | } | |
1261 | ||
1262 | return 0; | |
1263 | } | |
1264 | ||
23a177ef | 1265 | static unsigned manager_dispatch_cleanup_queue(Manager *m) { |
595ed347 | 1266 | Unit *u; |
23a177ef LP |
1267 | unsigned n = 0; |
1268 | ||
1269 | assert(m); | |
1270 | ||
595ed347 MS |
1271 | while ((u = m->cleanup_queue)) { |
1272 | assert(u->in_cleanup_queue); | |
23a177ef | 1273 | |
595ed347 | 1274 | unit_free(u); |
23a177ef LP |
1275 | n++; |
1276 | } | |
1277 | ||
1278 | return n; | |
1279 | } | |
1280 | ||
6ac62d61 LP |
1281 | static unsigned manager_dispatch_release_resources_queue(Manager *m) { |
1282 | unsigned n = 0; | |
1283 | Unit *u; | |
1284 | ||
1285 | assert(m); | |
1286 | ||
52e3671b | 1287 | while ((u = LIST_POP(release_resources_queue, m->release_resources_queue))) { |
6ac62d61 | 1288 | assert(u->in_release_resources_queue); |
6ac62d61 LP |
1289 | u->in_release_resources_queue = false; |
1290 | ||
1291 | n++; | |
1292 | ||
1293 | unit_release_resources(u); | |
1294 | } | |
1295 | ||
1296 | return n; | |
1297 | } | |
1298 | ||
eced69b3 | 1299 | enum { |
35b8ca3a | 1300 | GC_OFFSET_IN_PATH, /* This one is on the path we were traveling */ |
eced69b3 LP |
1301 | GC_OFFSET_UNSURE, /* No clue */ |
1302 | GC_OFFSET_GOOD, /* We still need this unit */ | |
1303 | GC_OFFSET_BAD, /* We don't need this unit anymore */ | |
1304 | _GC_OFFSET_MAX | |
1305 | }; | |
1306 | ||
00d9ef85 | 1307 | static void unit_gc_mark_good(Unit *u, unsigned gc_marker) { |
4892084f LN |
1308 | Unit *other; |
1309 | ||
1310 | u->gc_marker = gc_marker + GC_OFFSET_GOOD; | |
1311 | ||
1312 | /* Recursively mark referenced units as GOOD as well */ | |
15ed3c3a | 1313 | UNIT_FOREACH_DEPENDENCY(other, u, UNIT_ATOM_REFERENCES) |
4892084f LN |
1314 | if (other->gc_marker == gc_marker + GC_OFFSET_UNSURE) |
1315 | unit_gc_mark_good(other, gc_marker); | |
1316 | } | |
1317 | ||
eced69b3 | 1318 | static void unit_gc_sweep(Unit *u, unsigned gc_marker) { |
701cc384 | 1319 | Unit *other; |
eced69b3 | 1320 | bool is_bad; |
701cc384 LP |
1321 | |
1322 | assert(u); | |
1323 | ||
4c701096 YW |
1324 | if (IN_SET(u->gc_marker - gc_marker, |
1325 | GC_OFFSET_GOOD, GC_OFFSET_BAD, GC_OFFSET_UNSURE, GC_OFFSET_IN_PATH)) | |
701cc384 LP |
1326 | return; |
1327 | ||
ac155bb8 | 1328 | if (u->in_cleanup_queue) |
701cc384 LP |
1329 | goto bad; |
1330 | ||
f2f725e5 | 1331 | if (!unit_may_gc(u)) |
701cc384 LP |
1332 | goto good; |
1333 | ||
ac155bb8 | 1334 | u->gc_marker = gc_marker + GC_OFFSET_IN_PATH; |
eced69b3 LP |
1335 | |
1336 | is_bad = true; | |
1337 | ||
15ed3c3a | 1338 | UNIT_FOREACH_DEPENDENCY(other, u, UNIT_ATOM_REFERENCED_BY) { |
701cc384 LP |
1339 | unit_gc_sweep(other, gc_marker); |
1340 | ||
ac155bb8 | 1341 | if (other->gc_marker == gc_marker + GC_OFFSET_GOOD) |
701cc384 | 1342 | goto good; |
eced69b3 | 1343 | |
ac155bb8 | 1344 | if (other->gc_marker != gc_marker + GC_OFFSET_BAD) |
eced69b3 | 1345 | is_bad = false; |
701cc384 LP |
1346 | } |
1347 | ||
ef3fc326 ZJS |
1348 | LIST_FOREACH(refs_by_target, ref, u->refs_by_target) { |
1349 | unit_gc_sweep(ref->source, gc_marker); | |
2641f02e | 1350 | |
ef3fc326 ZJS |
1351 | if (ref->source->gc_marker == gc_marker + GC_OFFSET_GOOD) |
1352 | goto good; | |
2641f02e | 1353 | |
ef3fc326 ZJS |
1354 | if (ref->source->gc_marker != gc_marker + GC_OFFSET_BAD) |
1355 | is_bad = false; | |
2641f02e | 1356 | } |
701cc384 | 1357 | |
eced69b3 LP |
1358 | if (is_bad) |
1359 | goto bad; | |
1360 | ||
1361 | /* We were unable to find anything out about this entry, so | |
1362 | * let's investigate it later */ | |
ac155bb8 | 1363 | u->gc_marker = gc_marker + GC_OFFSET_UNSURE; |
eced69b3 LP |
1364 | unit_add_to_gc_queue(u); |
1365 | return; | |
1366 | ||
701cc384 | 1367 | bad: |
eced69b3 LP |
1368 | /* We definitely know that this one is not useful anymore, so |
1369 | * let's mark it for deletion */ | |
ac155bb8 | 1370 | u->gc_marker = gc_marker + GC_OFFSET_BAD; |
eced69b3 | 1371 | unit_add_to_cleanup_queue(u); |
701cc384 LP |
1372 | return; |
1373 | ||
1374 | good: | |
4892084f | 1375 | unit_gc_mark_good(u, gc_marker); |
701cc384 LP |
1376 | } |
1377 | ||
c5a97ed1 LP |
1378 | static unsigned manager_dispatch_gc_unit_queue(Manager *m) { |
1379 | unsigned n = 0, gc_marker; | |
701cc384 LP |
1380 | |
1381 | assert(m); | |
1382 | ||
cf1265e1 | 1383 | /* log_debug("Running GC..."); */ |
701cc384 | 1384 | |
eced69b3 LP |
1385 | m->gc_marker += _GC_OFFSET_MAX; |
1386 | if (m->gc_marker + _GC_OFFSET_MAX <= _GC_OFFSET_MAX) | |
c9c0cadb | 1387 | m->gc_marker = 1; |
701cc384 | 1388 | |
eced69b3 LP |
1389 | gc_marker = m->gc_marker; |
1390 | ||
741a184a MY |
1391 | Unit *u; |
1392 | while ((u = m->gc_unit_queue)) { | |
595ed347 | 1393 | assert(u->in_gc_queue); |
701cc384 | 1394 | |
595ed347 | 1395 | unit_gc_sweep(u, gc_marker); |
eced69b3 | 1396 | |
741a184a | 1397 | LIST_REMOVE(gc_queue, m->gc_unit_queue, u); |
595ed347 | 1398 | u->in_gc_queue = false; |
701cc384 LP |
1399 | |
1400 | n++; | |
1401 | ||
4c701096 YW |
1402 | if (IN_SET(u->gc_marker - gc_marker, |
1403 | GC_OFFSET_BAD, GC_OFFSET_UNSURE)) { | |
cc3bc3e6 | 1404 | if (u->id) |
f2341e0a | 1405 | log_unit_debug(u, "Collecting."); |
595ed347 MS |
1406 | u->gc_marker = gc_marker + GC_OFFSET_BAD; |
1407 | unit_add_to_cleanup_queue(u); | |
701cc384 LP |
1408 | } |
1409 | } | |
1410 | ||
701cc384 LP |
1411 | return n; |
1412 | } | |
1413 | ||
c5a97ed1 LP |
1414 | static unsigned manager_dispatch_gc_job_queue(Manager *m) { |
1415 | unsigned n = 0; | |
1416 | Job *j; | |
1417 | ||
1418 | assert(m); | |
1419 | ||
52e3671b | 1420 | while ((j = LIST_POP(gc_queue, m->gc_job_queue))) { |
c5a97ed1 | 1421 | assert(j->in_gc_queue); |
c5a97ed1 LP |
1422 | j->in_gc_queue = false; |
1423 | ||
1424 | n++; | |
1425 | ||
2ab3050f | 1426 | if (!job_may_gc(j)) |
c5a97ed1 LP |
1427 | continue; |
1428 | ||
1429 | log_unit_debug(j->unit, "Collecting job."); | |
1430 | (void) job_finish_and_invalidate(j, JOB_COLLECTED, false, false); | |
1431 | } | |
1432 | ||
1433 | return n; | |
1434 | } | |
1435 | ||
7223d500 LB |
1436 | static int manager_ratelimit_requeue(sd_event_source *s, uint64_t usec, void *userdata) { |
1437 | Unit *u = userdata; | |
1438 | ||
1439 | assert(u); | |
1440 | assert(s == u->auto_start_stop_event_source); | |
1441 | ||
1442 | u->auto_start_stop_event_source = sd_event_source_unref(u->auto_start_stop_event_source); | |
1443 | ||
1444 | /* Re-queue to all queues, if the rate limit hit we might have been throttled on any of them. */ | |
1445 | unit_submit_to_stop_when_unneeded_queue(u); | |
1446 | unit_submit_to_start_when_upheld_queue(u); | |
1447 | unit_submit_to_stop_when_bound_queue(u); | |
1448 | ||
1449 | return 0; | |
1450 | } | |
1451 | ||
1452 | static int manager_ratelimit_check_and_queue(Unit *u) { | |
1453 | int r; | |
1454 | ||
1455 | assert(u); | |
1456 | ||
1457 | if (ratelimit_below(&u->auto_start_stop_ratelimit)) | |
1458 | return 1; | |
1459 | ||
1460 | /* Already queued, no need to requeue */ | |
1461 | if (u->auto_start_stop_event_source) | |
1462 | return 0; | |
1463 | ||
1464 | r = sd_event_add_time( | |
1465 | u->manager->event, | |
1466 | &u->auto_start_stop_event_source, | |
1467 | CLOCK_MONOTONIC, | |
1468 | ratelimit_end(&u->auto_start_stop_ratelimit), | |
1469 | 0, | |
1470 | manager_ratelimit_requeue, | |
1471 | u); | |
1472 | if (r < 0) | |
1473 | return log_unit_error_errno(u, r, "Failed to queue timer on event loop: %m"); | |
1474 | ||
1475 | return 0; | |
1476 | } | |
1477 | ||
a3c1168a LP |
1478 | static unsigned manager_dispatch_stop_when_unneeded_queue(Manager *m) { |
1479 | unsigned n = 0; | |
1480 | Unit *u; | |
1481 | int r; | |
1482 | ||
1483 | assert(m); | |
1484 | ||
52e3671b | 1485 | while ((u = LIST_POP(stop_when_unneeded_queue, m->stop_when_unneeded_queue))) { |
a3c1168a | 1486 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
a3c1168a LP |
1487 | |
1488 | assert(u->in_stop_when_unneeded_queue); | |
a3c1168a LP |
1489 | u->in_stop_when_unneeded_queue = false; |
1490 | ||
1491 | n++; | |
1492 | ||
1493 | if (!unit_is_unneeded(u)) | |
1494 | continue; | |
1495 | ||
1496 | log_unit_debug(u, "Unit is not needed anymore."); | |
1497 | ||
1498 | /* If stopping a unit fails continuously we might enter a stop loop here, hence stop acting on the | |
1499 | * service being unnecessary after a while. */ | |
1500 | ||
7223d500 LB |
1501 | r = manager_ratelimit_check_and_queue(u); |
1502 | if (r <= 0) { | |
1503 | log_unit_warning(u, | |
1504 | "Unit not needed anymore, but not stopping since we tried this too often recently.%s", | |
1505 | r == 0 ? " Will retry later." : ""); | |
a3c1168a LP |
1506 | continue; |
1507 | } | |
1508 | ||
1509 | /* Ok, nobody needs us anymore. Sniff. Then let's commit suicide */ | |
d993ad6c | 1510 | r = manager_add_job(u->manager, JOB_STOP, u, JOB_FAIL, &error, /* ret = */ NULL); |
a3c1168a LP |
1511 | if (r < 0) |
1512 | log_unit_warning_errno(u, r, "Failed to enqueue stop job, ignoring: %s", bus_error_message(&error, r)); | |
1513 | } | |
1514 | ||
1515 | return n; | |
1516 | } | |
1517 | ||
0bc488c9 LP |
1518 | static unsigned manager_dispatch_start_when_upheld_queue(Manager *m) { |
1519 | unsigned n = 0; | |
1520 | Unit *u; | |
1521 | int r; | |
1522 | ||
1523 | assert(m); | |
1524 | ||
52e3671b | 1525 | while ((u = LIST_POP(start_when_upheld_queue, m->start_when_upheld_queue))) { |
0bc488c9 LP |
1526 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
1527 | Unit *culprit = NULL; | |
1528 | ||
1529 | assert(u->in_start_when_upheld_queue); | |
0bc488c9 LP |
1530 | u->in_start_when_upheld_queue = false; |
1531 | ||
1532 | n++; | |
1533 | ||
1534 | if (!unit_is_upheld_by_active(u, &culprit)) | |
1535 | continue; | |
1536 | ||
1537 | log_unit_debug(u, "Unit is started because upheld by active unit %s.", culprit->id); | |
1538 | ||
1539 | /* If stopping a unit fails continuously we might enter a stop loop here, hence stop acting on the | |
1540 | * service being unnecessary after a while. */ | |
1541 | ||
7223d500 LB |
1542 | r = manager_ratelimit_check_and_queue(u); |
1543 | if (r <= 0) { | |
1544 | log_unit_warning(u, | |
1545 | "Unit needs to be started because active unit %s upholds it, but not starting since we tried this too often recently.%s", | |
1546 | culprit->id, | |
1547 | r == 0 ? " Will retry later." : ""); | |
0bc488c9 LP |
1548 | continue; |
1549 | } | |
1550 | ||
d993ad6c | 1551 | r = manager_add_job(u->manager, JOB_START, u, JOB_FAIL, &error, /* ret = */ NULL); |
0bc488c9 LP |
1552 | if (r < 0) |
1553 | log_unit_warning_errno(u, r, "Failed to enqueue start job, ignoring: %s", bus_error_message(&error, r)); | |
1554 | } | |
1555 | ||
1556 | return n; | |
1557 | } | |
1558 | ||
56c59592 LP |
1559 | static unsigned manager_dispatch_stop_when_bound_queue(Manager *m) { |
1560 | unsigned n = 0; | |
1561 | Unit *u; | |
1562 | int r; | |
1563 | ||
1564 | assert(m); | |
1565 | ||
52e3671b | 1566 | while ((u = LIST_POP(stop_when_bound_queue, m->stop_when_bound_queue))) { |
56c59592 LP |
1567 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
1568 | Unit *culprit = NULL; | |
1569 | ||
1570 | assert(u->in_stop_when_bound_queue); | |
56c59592 LP |
1571 | u->in_stop_when_bound_queue = false; |
1572 | ||
1573 | n++; | |
1574 | ||
1575 | if (!unit_is_bound_by_inactive(u, &culprit)) | |
1576 | continue; | |
1577 | ||
1578 | log_unit_debug(u, "Unit is stopped because bound to inactive unit %s.", culprit->id); | |
1579 | ||
1580 | /* If stopping a unit fails continuously we might enter a stop loop here, hence stop acting on the | |
1581 | * service being unnecessary after a while. */ | |
1582 | ||
7223d500 LB |
1583 | r = manager_ratelimit_check_and_queue(u); |
1584 | if (r <= 0) { | |
1585 | log_unit_warning(u, | |
1586 | "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", | |
1587 | culprit->id, | |
1588 | r == 0 ? " Will retry later." : ""); | |
56c59592 LP |
1589 | continue; |
1590 | } | |
1591 | ||
d993ad6c | 1592 | r = manager_add_job(u->manager, JOB_STOP, u, JOB_REPLACE, &error, /* ret = */ NULL); |
56c59592 LP |
1593 | if (r < 0) |
1594 | log_unit_warning_errno(u, r, "Failed to enqueue stop job, ignoring: %s", bus_error_message(&error, r)); | |
1595 | } | |
1596 | ||
1597 | return n; | |
1598 | } | |
1599 | ||
a16e1123 | 1600 | static void manager_clear_jobs_and_units(Manager *m) { |
a16e1123 | 1601 | Unit *u; |
60918275 LP |
1602 | |
1603 | assert(m); | |
1604 | ||
87f0e418 LP |
1605 | while ((u = hashmap_first(m->units))) |
1606 | unit_free(u); | |
964e0949 LP |
1607 | |
1608 | manager_dispatch_cleanup_queue(m); | |
1609 | ||
1610 | assert(!m->load_queue); | |
da8e1782 | 1611 | assert(prioq_isempty(m->run_queue)); |
964e0949 LP |
1612 | assert(!m->dbus_unit_queue); |
1613 | assert(!m->dbus_job_queue); | |
1614 | assert(!m->cleanup_queue); | |
c5a97ed1 LP |
1615 | assert(!m->gc_unit_queue); |
1616 | assert(!m->gc_job_queue); | |
13e72103 MK |
1617 | assert(!m->cgroup_realize_queue); |
1618 | assert(!m->cgroup_empty_queue); | |
1619 | assert(!m->cgroup_oom_queue); | |
1620 | assert(!m->target_deps_queue); | |
a3c1168a | 1621 | assert(!m->stop_when_unneeded_queue); |
0bc488c9 | 1622 | assert(!m->start_when_upheld_queue); |
56c59592 | 1623 | assert(!m->stop_when_bound_queue); |
cb3c6aec | 1624 | assert(!m->release_resources_queue); |
964e0949 | 1625 | |
964e0949 LP |
1626 | assert(hashmap_isempty(m->jobs)); |
1627 | assert(hashmap_isempty(m->units)); | |
ead510fe | 1628 | assert(hashmap_isempty(m->units_by_invocation_id)); |
9e9e2b72 MS |
1629 | |
1630 | m->n_on_console = 0; | |
1631 | m->n_running_jobs = 0; | |
1e75824c MK |
1632 | m->n_installed_jobs = 0; |
1633 | m->n_failed_jobs = 0; | |
a16e1123 LP |
1634 | } |
1635 | ||
06d8d842 | 1636 | Manager* manager_free(Manager *m) { |
06d8d842 ZJS |
1637 | if (!m) |
1638 | return NULL; | |
a16e1123 LP |
1639 | |
1640 | manager_clear_jobs_and_units(m); | |
23a177ef | 1641 | |
24b45970 | 1642 | for (UnitType c = 0; c < _UNIT_TYPE_MAX; c++) |
7824bbeb LP |
1643 | if (unit_vtable[c]->shutdown) |
1644 | unit_vtable[c]->shutdown(m); | |
1645 | ||
86036b26 | 1646 | /* Keep the cgroup hierarchy in place except when we know we are going down for good */ |
13ffc607 | 1647 | manager_shutdown_cgroup(m, /* delete= */ IN_SET(m->objective, MANAGER_EXIT, MANAGER_REBOOT, MANAGER_POWEROFF, MANAGER_HALT, MANAGER_KEXEC)); |
8e274523 | 1648 | |
07a78643 | 1649 | lookup_paths_flush_generator(&m->lookup_paths); |
5a1e9937 | 1650 | |
5e8d1c9a | 1651 | bus_done(m); |
19d22d43 | 1652 | manager_varlink_done(m); |
ea430986 | 1653 | |
e76506b7 DDM |
1654 | exec_shared_runtime_vacuum(m); |
1655 | hashmap_free(m->exec_shared_runtime_by_id); | |
e8a565cb | 1656 | |
29206d46 LP |
1657 | dynamic_user_vacuum(m, false); |
1658 | hashmap_free(m->dynamic_users); | |
1659 | ||
87f0e418 | 1660 | hashmap_free(m->units); |
4b58153d | 1661 | hashmap_free(m->units_by_invocation_id); |
60918275 | 1662 | hashmap_free(m->jobs); |
62a76913 | 1663 | hashmap_free(m->watch_pids); |
495e75ed | 1664 | hashmap_free(m->watch_pids_more); |
05e343b7 | 1665 | hashmap_free(m->watch_bus); |
9152c765 | 1666 | |
da8e1782 MO |
1667 | prioq_free(m->run_queue); |
1668 | ||
95ae05c0 | 1669 | set_free(m->startup_units); |
f755e3b7 LP |
1670 | set_free(m->failed_units); |
1671 | ||
718db961 | 1672 | sd_event_source_unref(m->signal_event_source); |
575b300b | 1673 | sd_event_source_unref(m->sigchld_event_source); |
718db961 LP |
1674 | sd_event_source_unref(m->notify_event_source); |
1675 | sd_event_source_unref(m->time_change_event_source); | |
bbf5fd8e | 1676 | sd_event_source_unref(m->timezone_change_event_source); |
718db961 | 1677 | sd_event_source_unref(m->jobs_in_progress_event_source); |
752b5905 | 1678 | sd_event_source_unref(m->run_queue_event_source); |
00d9ef85 | 1679 | sd_event_source_unref(m->user_lookup_event_source); |
817062e6 | 1680 | sd_event_source_unref(m->handoff_timestamp_event_source); |
406f1775 | 1681 | sd_event_source_unref(m->pidref_event_source); |
29e6b0c1 | 1682 | sd_event_source_unref(m->memory_pressure_event_source); |
718db961 | 1683 | |
03e334a1 LP |
1684 | safe_close(m->signal_fd); |
1685 | safe_close(m->notify_fd); | |
00d9ef85 | 1686 | safe_close_pair(m->user_lookup_fds); |
817062e6 | 1687 | safe_close_pair(m->handoff_timestamp_fds); |
406f1775 | 1688 | safe_close_pair(m->pidref_transport_fds); |
718db961 | 1689 | |
e46b13c8 ZJS |
1690 | manager_close_ask_password(m); |
1691 | ||
718db961 LP |
1692 | manager_close_idle_pipe(m); |
1693 | ||
1694 | sd_event_unref(m->event); | |
60918275 | 1695 | |
c952c6ec LP |
1696 | free(m->notify_socket); |
1697 | ||
7dfc7139 | 1698 | lookup_paths_done(&m->lookup_paths); |
1ad6e8b3 LP |
1699 | strv_free(m->transient_environment); |
1700 | strv_free(m->client_environment); | |
036643a2 | 1701 | |
4ad49000 | 1702 | hashmap_free(m->cgroup_unit); |
e8630e69 | 1703 | manager_free_unit_name_maps(m); |
33be102a | 1704 | |
664f88a7 LP |
1705 | free(m->switch_root); |
1706 | free(m->switch_root_init); | |
1707 | ||
8402ca04 | 1708 | sd_bus_track_unref(m->subscribed); |
e1315a62 | 1709 | strv_free(m->subscribed_as_strv); |
8402ca04 | 1710 | |
c9e120e0 | 1711 | unit_defaults_done(&m->defaults); |
c93ff2e9 | 1712 | |
9e615fa3 LB |
1713 | FOREACH_ARRAY(map, m->units_needing_mounts_for, _UNIT_MOUNT_DEPENDENCY_TYPE_MAX) { |
1714 | assert(hashmap_isempty(*map)); | |
1715 | hashmap_free(*map); | |
1716 | } | |
a57f7e2c | 1717 | |
00d9ef85 LP |
1718 | hashmap_free(m->uid_refs); |
1719 | hashmap_free(m->gid_refs); | |
1720 | ||
a7516260 MY |
1721 | FOREACH_ARRAY(i, m->prefix, _EXEC_DIRECTORY_TYPE_MAX) |
1722 | free(*i); | |
1723 | ||
2ad591a3 LP |
1724 | free(m->received_credentials_directory); |
1725 | free(m->received_encrypted_credentials_directory); | |
35aba85a | 1726 | |
aff3a9e1 LB |
1727 | free(m->watchdog_pretimeout_governor); |
1728 | free(m->watchdog_pretimeout_governor_overridden); | |
1729 | ||
a7516260 | 1730 | fw_ctx_free(m->fw_ctx); |
dc7d69b3 | 1731 | |
b1994387 | 1732 | #if BPF_FRAMEWORK |
352ec23c | 1733 | bpf_restrict_fs_destroy(m->restrict_fs); |
b1994387 ILG |
1734 | #endif |
1735 | ||
bb5232b6 | 1736 | safe_close(m->executor_fd); |
b157a7e6 | 1737 | free(m->executor_path); |
bb5232b6 | 1738 | |
6b430fdb | 1739 | return mfree(m); |
60918275 LP |
1740 | } |
1741 | ||
04eb582a | 1742 | static void manager_enumerate_perpetual(Manager *m) { |
04eb582a LP |
1743 | assert(m); |
1744 | ||
1545051c | 1745 | if (FLAGS_SET(m->test_run_flags, MANAGER_TEST_RUN_MINIMAL)) |
b1d5246d ZJS |
1746 | return; |
1747 | ||
04eb582a | 1748 | /* Let's ask every type to load all units from disk/kernel that it might know */ |
24b45970 | 1749 | for (UnitType c = 0; c < _UNIT_TYPE_MAX; c++) { |
04eb582a LP |
1750 | if (!unit_type_supported(c)) { |
1751 | log_debug("Unit type .%s is not supported on this system.", unit_type_to_string(c)); | |
1752 | continue; | |
1753 | } | |
1754 | ||
1755 | if (unit_vtable[c]->enumerate_perpetual) | |
1756 | unit_vtable[c]->enumerate_perpetual(m); | |
1757 | } | |
1758 | } | |
1759 | ||
a1113e08 | 1760 | static void manager_enumerate(Manager *m) { |
f50e0a01 LP |
1761 | assert(m); |
1762 | ||
1545051c | 1763 | if (FLAGS_SET(m->test_run_flags, MANAGER_TEST_RUN_MINIMAL)) |
b1d5246d ZJS |
1764 | return; |
1765 | ||
04eb582a | 1766 | /* Let's ask every type to load all units from disk/kernel that it might know */ |
24b45970 | 1767 | for (UnitType c = 0; c < _UNIT_TYPE_MAX; c++) { |
1c2e9646 | 1768 | if (!unit_type_supported(c)) { |
03afec3c | 1769 | log_debug("Unit type .%s is not supported on this system.", unit_type_to_string(c)); |
0faacd47 | 1770 | continue; |
a57f7e2c | 1771 | } |
f50e0a01 | 1772 | |
94b01dae ZJS |
1773 | if (unit_vtable[c]->enumerate) |
1774 | unit_vtable[c]->enumerate(m); | |
0faacd47 LP |
1775 | } |
1776 | ||
f50e0a01 | 1777 | manager_dispatch_load_queue(m); |
a16e1123 LP |
1778 | } |
1779 | ||
007c6337 | 1780 | static void manager_coldplug(Manager *m) { |
a16e1123 LP |
1781 | Unit *u; |
1782 | char *k; | |
007c6337 | 1783 | int r; |
a16e1123 LP |
1784 | |
1785 | assert(m); | |
f50e0a01 | 1786 | |
1ae9b0cf | 1787 | log_debug("Invoking unit coldplug() handlers%s", glyph(GLYPH_ELLIPSIS)); |
f0831ed2 LP |
1788 | |
1789 | /* Let's place the units back into their deserialized state */ | |
90e74a66 | 1790 | HASHMAP_FOREACH_KEY(u, k, m->units) { |
f50e0a01 LP |
1791 | |
1792 | /* ignore aliases */ | |
ac155bb8 | 1793 | if (u->id != k) |
f50e0a01 LP |
1794 | continue; |
1795 | ||
007c6337 LP |
1796 | r = unit_coldplug(u); |
1797 | if (r < 0) | |
1798 | log_warning_errno(r, "We couldn't coldplug %s, proceeding anyway: %m", u->id); | |
f50e0a01 | 1799 | } |
a16e1123 LP |
1800 | } |
1801 | ||
f0831ed2 | 1802 | static void manager_catchup(Manager *m) { |
f0831ed2 LP |
1803 | Unit *u; |
1804 | char *k; | |
1805 | ||
1806 | assert(m); | |
1807 | ||
1ae9b0cf | 1808 | log_debug("Invoking unit catchup() handlers%s", glyph(GLYPH_ELLIPSIS)); |
f0831ed2 LP |
1809 | |
1810 | /* Let's catch up on any state changes that happened while we were reloading/reexecing */ | |
90e74a66 | 1811 | HASHMAP_FOREACH_KEY(u, k, m->units) { |
f0831ed2 LP |
1812 | |
1813 | /* ignore aliases */ | |
1814 | if (u->id != k) | |
1815 | continue; | |
1816 | ||
1817 | unit_catchup(u); | |
1818 | } | |
1819 | } | |
1820 | ||
9ff1a6f1 | 1821 | static void manager_distribute_fds(Manager *m, FDSet *fds) { |
9ff1a6f1 | 1822 | Unit *u; |
9588bc32 LP |
1823 | |
1824 | assert(m); | |
1825 | ||
90e74a66 | 1826 | HASHMAP_FOREACH(u, m->units) { |
9588bc32 | 1827 | |
43127aeb | 1828 | if (fdset_isempty(fds)) |
9588bc32 LP |
1829 | break; |
1830 | ||
9ff1a6f1 LP |
1831 | if (!UNIT_VTABLE(u)->distribute_fds) |
1832 | continue; | |
9588bc32 | 1833 | |
9ff1a6f1 LP |
1834 | UNIT_VTABLE(u)->distribute_fds(u, fds); |
1835 | } | |
9588bc32 LP |
1836 | } |
1837 | ||
2b680534 | 1838 | static bool manager_dbus_is_running(Manager *m, bool deserialized) { |
8559b3b7 LP |
1839 | Unit *u; |
1840 | ||
1841 | assert(m); | |
1842 | ||
1843 | /* This checks whether the dbus instance we are supposed to expose our APIs on is up. We check both the socket | |
1844 | * and the service unit. If the 'deserialized' parameter is true we'll check the deserialized state of the unit | |
1845 | * rather than the current one. */ | |
1846 | ||
638cece4 | 1847 | if (MANAGER_IS_TEST_RUN(m)) |
8559b3b7 LP |
1848 | return false; |
1849 | ||
8559b3b7 LP |
1850 | u = manager_get_unit(m, SPECIAL_DBUS_SOCKET); |
1851 | if (!u) | |
1852 | return false; | |
1853 | if ((deserialized ? SOCKET(u)->deserialized_state : SOCKET(u)->state) != SOCKET_RUNNING) | |
1854 | return false; | |
1855 | ||
1856 | u = manager_get_unit(m, SPECIAL_DBUS_SERVICE); | |
1857 | if (!u) | |
1858 | return false; | |
845824ac | 1859 | if (!IN_SET((deserialized ? SERVICE(u)->deserialized_state : SERVICE(u)->state), |
1860 | SERVICE_RUNNING, | |
5162829e | 1861 | SERVICE_MOUNTING, |
845824ac | 1862 | SERVICE_RELOAD, |
1863 | SERVICE_RELOAD_NOTIFY, | |
dfdeb0b1 | 1864 | SERVICE_REFRESH_EXTENSIONS, |
845824ac | 1865 | SERVICE_RELOAD_SIGNAL)) |
8559b3b7 LP |
1866 | return false; |
1867 | ||
1868 | return true; | |
1869 | } | |
1870 | ||
9d4c195c LP |
1871 | static void manager_setup_bus(Manager *m) { |
1872 | assert(m); | |
1873 | ||
71a737d6 LP |
1874 | if (MANAGER_IS_TEST_RUN(m)) |
1875 | return; | |
1876 | ||
9d4c195c LP |
1877 | /* Let's set up our private bus connection now, unconditionally */ |
1878 | (void) bus_init_private(m); | |
1879 | ||
1880 | /* If we are in --user mode also connect to the system bus now */ | |
1881 | if (MANAGER_IS_USER(m)) | |
1882 | (void) bus_init_system(m); | |
1883 | ||
1884 | /* Let's connect to the bus now, but only if the unit is supposed to be up */ | |
2b680534 | 1885 | if (manager_dbus_is_running(m, MANAGER_IS_RELOADING(m))) { |
9d4c195c LP |
1886 | (void) bus_init_api(m); |
1887 | ||
1888 | if (MANAGER_IS_SYSTEM(m)) | |
1889 | (void) bus_init_system(m); | |
1890 | } | |
1891 | } | |
1892 | ||
159f1e76 LP |
1893 | static void manager_preset_all(Manager *m) { |
1894 | int r; | |
1895 | ||
1896 | assert(m); | |
1897 | ||
1898 | if (m->first_boot <= 0) | |
1899 | return; | |
1900 | ||
1901 | if (!MANAGER_IS_SYSTEM(m)) | |
1902 | return; | |
1903 | ||
638cece4 | 1904 | if (MANAGER_IS_TEST_RUN(m)) |
159f1e76 LP |
1905 | return; |
1906 | ||
1907 | /* If this is the first boot, and we are in the host system, then preset everything */ | |
22330352 ZJS |
1908 | UnitFilePresetMode mode = |
1909 | ENABLE_FIRST_BOOT_FULL_PRESET ? UNIT_FILE_PRESET_FULL : UNIT_FILE_PRESET_ENABLE_ONLY; | |
bdd852a1 DM |
1910 | InstallChange *changes = NULL; |
1911 | size_t n_changes = 0; | |
93651582 | 1912 | |
bdd852a1 DM |
1913 | CLEANUP_ARRAY(changes, n_changes, install_changes_free); |
1914 | ||
1915 | log_info("Applying preset policy."); | |
1916 | r = unit_file_preset_all(RUNTIME_SCOPE_SYSTEM, /* file_flags = */ 0, | |
1917 | /* root_dir = */ NULL, mode, &changes, &n_changes); | |
1918 | install_changes_dump(r, "preset", changes, n_changes, /* quiet = */ false); | |
5b80cef6 LP |
1919 | if (r < 0) |
1920 | log_full_errno(r == -EEXIST ? LOG_NOTICE : LOG_WARNING, r, | |
1921 | "Failed to populate /etc with preset unit settings, ignoring: %m"); | |
1922 | else | |
1923 | log_info("Populated /etc with preset unit settings."); | |
159f1e76 LP |
1924 | } |
1925 | ||
5ce5e1ad LP |
1926 | static void manager_ready(Manager *m) { |
1927 | assert(m); | |
1928 | ||
1929 | /* After having loaded everything, do the final round of catching up with what might have changed */ | |
1930 | ||
1931 | m->objective = MANAGER_OK; /* Tell everyone we are up now */ | |
1932 | ||
1933 | /* It might be safe to log to the journal now and connect to dbus */ | |
1934 | manager_recheck_journal(m); | |
1935 | manager_recheck_dbus(m); | |
1936 | ||
5ce5e1ad LP |
1937 | /* Let's finally catch up with any changes that took place while we were reloading/reexecing */ |
1938 | manager_catchup(m); | |
c6e892bc | 1939 | |
15b9243c | 1940 | /* Create a file which will indicate when the manager started loading units the last time. */ |
4b3ad81b LB |
1941 | if (MANAGER_IS_SYSTEM(m)) |
1942 | (void) touch_file("/run/systemd/systemd-units-load", false, | |
1943 | m->timestamps[MANAGER_TIMESTAMP_UNITS_LOAD].realtime ?: now(CLOCK_REALTIME), | |
1944 | UID_INVALID, GID_INVALID, 0444); | |
5ce5e1ad LP |
1945 | } |
1946 | ||
a01ba4b2 | 1947 | Manager* manager_reloading_start(Manager *m) { |
d147e2b6 | 1948 | m->n_reloading++; |
fa5a0251 | 1949 | dual_timestamp_now(m->timestamps + MANAGER_TIMESTAMP_UNITS_LOAD); |
d147e2b6 ZJS |
1950 | return m; |
1951 | } | |
49fbe940 | 1952 | |
a01ba4b2 | 1953 | void manager_reloading_stopp(Manager **m) { |
d147e2b6 ZJS |
1954 | if (*m) { |
1955 | assert((*m)->n_reloading > 0); | |
1956 | (*m)->n_reloading--; | |
1957 | } | |
1958 | } | |
1959 | ||
e75fbee6 LP |
1960 | static int manager_make_runtime_dir(Manager *m) { |
1961 | int r; | |
1962 | ||
1963 | assert(m); | |
1964 | ||
1965 | _cleanup_free_ char *d = path_join(m->prefix[EXEC_DIRECTORY_RUNTIME], "systemd"); | |
1966 | if (!d) | |
1967 | return log_oom(); | |
1968 | ||
1969 | r = mkdir_label(d, 0755); | |
1970 | if (r < 0 && r != -EEXIST) | |
1971 | return log_error_errno(r, "Failed to create directory '%s/': %m", d); | |
1972 | ||
1973 | return 0; | |
1974 | } | |
1975 | ||
2a7cf953 | 1976 | int manager_startup(Manager *m, FILE *serialization, FDSet *fds, const char *root) { |
17f01ace | 1977 | int r; |
a16e1123 LP |
1978 | |
1979 | assert(m); | |
1980 | ||
e75fbee6 LP |
1981 | r = manager_make_runtime_dir(m); |
1982 | if (r < 0) | |
1983 | return r; | |
1984 | ||
a1f31f47 ZJS |
1985 | /* If we are running in test mode, we still want to run the generators, |
1986 | * but we should not touch the real generator directories. */ | |
4870133b | 1987 | r = lookup_paths_init_or_warn(&m->lookup_paths, m->runtime_scope, |
99aad9a2 ZJS |
1988 | MANAGER_IS_TEST_RUN(m) ? LOOKUP_PATHS_TEMPORARY_GENERATED : 0, |
1989 | root); | |
e801700e | 1990 | if (r < 0) |
99aad9a2 | 1991 | return r; |
5a1e9937 | 1992 | |
fa5a0251 | 1993 | dual_timestamp_now(m->timestamps + manager_timestamp_initrd_mangle(MANAGER_TIMESTAMP_GENERATORS_START)); |
eb523bfb LP |
1994 | r = manager_run_environment_generators(m); |
1995 | if (r >= 0) | |
1996 | r = manager_run_generators(m); | |
fa5a0251 | 1997 | dual_timestamp_now(m->timestamps + manager_timestamp_initrd_mangle(MANAGER_TIMESTAMP_GENERATORS_FINISH)); |
07719a21 LP |
1998 | if (r < 0) |
1999 | return r; | |
2000 | ||
159f1e76 | 2001 | manager_preset_all(m); |
572986ca | 2002 | |
581fef8d | 2003 | lookup_paths_log(&m->lookup_paths); |
572986ca | 2004 | |
4df7d537 ZJS |
2005 | { |
2006 | /* This block is (optionally) done with the reloading counter bumped */ | |
d7ac0952 | 2007 | _unused_ _cleanup_(manager_reloading_stopp) Manager *reloading = NULL; |
4df7d537 | 2008 | |
45f540a2 ZJS |
2009 | /* Make sure we don't have a left-over from a previous run */ |
2010 | if (!serialization) | |
2011 | (void) rm_rf(m->lookup_paths.transient, 0); | |
2012 | ||
4df7d537 ZJS |
2013 | /* If we will deserialize make sure that during enumeration this is already known, so we increase the |
2014 | * counter here already */ | |
2015 | if (serialization) | |
2016 | reloading = manager_reloading_start(m); | |
2017 | ||
2018 | /* First, enumerate what we can from all config files */ | |
fa5a0251 | 2019 | dual_timestamp_now(m->timestamps + manager_timestamp_initrd_mangle(MANAGER_TIMESTAMP_UNITS_LOAD_START)); |
4df7d537 ZJS |
2020 | manager_enumerate_perpetual(m); |
2021 | manager_enumerate(m); | |
fa5a0251 | 2022 | dual_timestamp_now(m->timestamps + manager_timestamp_initrd_mangle(MANAGER_TIMESTAMP_UNITS_LOAD_FINISH)); |
4df7d537 ZJS |
2023 | |
2024 | /* Second, deserialize if there is something to deserialize */ | |
2025 | if (serialization) { | |
2026 | r = manager_deserialize(m, serialization, fds); | |
2027 | if (r < 0) | |
2028 | return log_error_errno(r, "Deserialization failed: %m"); | |
2029 | } | |
01e10de3 | 2030 | |
d336b8ee MY |
2031 | if (m->previous_objective >= 0) { |
2032 | if (IN_SET(m->previous_objective, MANAGER_REEXECUTE, MANAGER_SOFT_REBOOT, MANAGER_SWITCH_ROOT)) | |
2033 | log_debug("Launching as effect of a '%s' operation.", | |
2034 | manager_objective_to_string(m->previous_objective)); | |
2035 | else | |
2036 | log_warning("Got unexpected previous objective '%s', ignoring.", | |
2037 | manager_objective_to_string(m->previous_objective)); | |
2038 | } | |
2039 | ||
8c15bf36 LB |
2040 | /* If we are in a new soft-reboot iteration bump the counter now before starting units, so |
2041 | * that they can reliably read it. We get the previous objective from serialized state. */ | |
2042 | if (m->previous_objective == MANAGER_SOFT_REBOOT) | |
2043 | m->soft_reboots_count++; | |
2044 | ||
4df7d537 ZJS |
2045 | /* Any fds left? Find some unit which wants them. This is useful to allow container managers to pass |
2046 | * some file descriptors to us pre-initialized. This enables socket-based activation of entire | |
2047 | * containers. */ | |
2048 | manager_distribute_fds(m, fds); | |
d86f9d52 | 2049 | |
678f271c | 2050 | /* We might have deserialized the notify fd, but if we didn't then let's create it now */ |
4df7d537 ZJS |
2051 | r = manager_setup_notify(m); |
2052 | if (r < 0) | |
2053 | /* No sense to continue without notifications, our children would fail anyway. */ | |
2054 | return r; | |
d8fdc620 | 2055 | |
4df7d537 ZJS |
2056 | r = manager_setup_user_lookup_fd(m); |
2057 | if (r < 0) | |
2058 | /* This shouldn't fail, except if things are really broken. */ | |
2059 | return r; | |
8559b3b7 | 2060 | |
817062e6 LP |
2061 | r = manager_setup_handoff_timestamp_fd(m); |
2062 | if (r < 0) | |
2063 | /* This shouldn't fail, except if things are really broken. */ | |
2064 | return r; | |
2065 | ||
406f1775 DDM |
2066 | r = manager_setup_pidref_transport_fd(m); |
2067 | if (r < 0) | |
2068 | /* This shouldn't fail, except if things are really broken. */ | |
2069 | return r; | |
2070 | ||
4df7d537 ZJS |
2071 | /* Connect to the bus if we are good for it */ |
2072 | manager_setup_bus(m); | |
e3dd987c | 2073 | |
19d22d43 LP |
2074 | r = manager_varlink_init(m); |
2075 | if (r < 0) | |
064a5c14 | 2076 | log_warning_errno(r, "Failed to set up Varlink, ignoring: %m"); |
19d22d43 | 2077 | |
4df7d537 ZJS |
2078 | /* Third, fire things up! */ |
2079 | manager_coldplug(m); | |
3ad2afb6 | 2080 | |
4df7d537 ZJS |
2081 | /* Clean up runtime objects */ |
2082 | manager_vacuum(m); | |
71445ae7 | 2083 | |
4df7d537 ZJS |
2084 | if (serialization) |
2085 | /* Let's wait for the UnitNew/JobNew messages being sent, before we notify that the | |
2086 | * reload is finished */ | |
2087 | m->send_reloading_done = true; | |
9f611ad8 LP |
2088 | } |
2089 | ||
5ce5e1ad | 2090 | manager_ready(m); |
f0831ed2 | 2091 | |
d35fe8c0 FB |
2092 | manager_set_switching_root(m, false); |
2093 | ||
17f01ace | 2094 | return 0; |
f50e0a01 LP |
2095 | } |
2096 | ||
d993ad6c | 2097 | int manager_add_job_full( |
50cbaba4 LP |
2098 | Manager *m, |
2099 | JobType type, | |
2100 | Unit *unit, | |
2101 | JobMode mode, | |
d993ad6c | 2102 | TransactionAddFlags extra_flags, |
50cbaba4 LP |
2103 | Set *affected_jobs, |
2104 | sd_bus_error *error, | |
2105 | Job **ret) { | |
2106 | ||
c7e3eb5d | 2107 | _cleanup_(transaction_abort_and_freep) Transaction *tr = NULL; |
50cbaba4 | 2108 | int r; |
e5b5ae50 LP |
2109 | |
2110 | assert(m); | |
d993ad6c | 2111 | assert(type >= 0 && type < _JOB_TYPE_MAX); |
87f0e418 | 2112 | assert(unit); |
d993ad6c MY |
2113 | assert(mode >= 0 && mode < _JOB_MODE_MAX); |
2114 | assert((extra_flags & ~_TRANSACTION_FLAGS_MASK_PUBLIC) == 0); | |
60918275 | 2115 | |
7358dc02 | 2116 | if (mode == JOB_ISOLATE && type != JOB_START) |
1b09b81c | 2117 | return sd_bus_error_set(error, SD_BUS_ERROR_INVALID_ARGS, "Isolate is only valid for start."); |
c497c7a9 | 2118 | |
7358dc02 | 2119 | if (mode == JOB_ISOLATE && !unit->allow_isolate) |
1b09b81c | 2120 | return sd_bus_error_set(error, BUS_ERROR_NO_ISOLATION, "Operation refused, unit may not be isolated."); |
2528a7a6 | 2121 | |
1f0f9f21 | 2122 | if (mode == JOB_TRIGGERING && type != JOB_STOP) |
1b09b81c | 2123 | return sd_bus_error_set(error, SD_BUS_ERROR_INVALID_ARGS, "--job-mode=triggering is only valid for stop."); |
1f0f9f21 | 2124 | |
09d04ad3 LP |
2125 | if (mode == JOB_RESTART_DEPENDENCIES && type != JOB_START) |
2126 | return sd_bus_error_set(error, SD_BUS_ERROR_INVALID_ARGS, "--job-mode=restart-dependencies is only valid for start."); | |
2127 | ||
f2341e0a | 2128 | log_unit_debug(unit, "Trying to enqueue job %s/%s/%s", unit->id, job_type_to_string(type), job_mode_to_string(mode)); |
9f04bd52 | 2129 | |
c6497ccb | 2130 | type = job_type_collapse(type, unit); |
e0209d83 | 2131 | |
23ade460 | 2132 | tr = transaction_new(mode == JOB_REPLACE_IRREVERSIBLY); |
7527cb52 MS |
2133 | if (!tr) |
2134 | return -ENOMEM; | |
11dd41ce | 2135 | |
b0904249 LP |
2136 | r = transaction_add_job_and_dependencies( |
2137 | tr, | |
2138 | type, | |
2139 | unit, | |
2140 | /* by= */ NULL, | |
2141 | TRANSACTION_MATTERS | | |
2142 | (IN_SET(mode, JOB_IGNORE_DEPENDENCIES, JOB_IGNORE_REQUIREMENTS) ? TRANSACTION_IGNORE_REQUIREMENTS : 0) | | |
09d04ad3 | 2143 | (mode == JOB_IGNORE_DEPENDENCIES ? TRANSACTION_IGNORE_ORDER : 0) | |
d993ad6c MY |
2144 | (mode == JOB_RESTART_DEPENDENCIES ? TRANSACTION_PROPAGATE_START_AS_RESTART : 0) | |
2145 | extra_flags, | |
b0904249 | 2146 | error); |
7527cb52 | 2147 | if (r < 0) |
c7e3eb5d | 2148 | return r; |
c497c7a9 | 2149 | |
7527cb52 MS |
2150 | if (mode == JOB_ISOLATE) { |
2151 | r = transaction_add_isolate_jobs(tr, m); | |
2152 | if (r < 0) | |
c7e3eb5d | 2153 | return r; |
7527cb52 MS |
2154 | } |
2155 | ||
1f0f9f21 KK |
2156 | if (mode == JOB_TRIGGERING) { |
2157 | r = transaction_add_triggering_jobs(tr, unit); | |
2158 | if (r < 0) | |
c7e3eb5d | 2159 | return r; |
1f0f9f21 KK |
2160 | } |
2161 | ||
50cbaba4 | 2162 | r = transaction_activate(tr, m, mode, affected_jobs, error); |
7527cb52 | 2163 | if (r < 0) |
c7e3eb5d | 2164 | return r; |
e5b5ae50 | 2165 | |
f2341e0a | 2166 | log_unit_debug(unit, |
66870f90 ZJS |
2167 | "Enqueued job %s/%s as %u", unit->id, |
2168 | job_type_to_string(type), (unsigned) tr->anchor_job->id); | |
f50e0a01 | 2169 | |
50cbaba4 LP |
2170 | if (ret) |
2171 | *ret = tr->anchor_job; | |
60918275 | 2172 | |
c7e3eb5d | 2173 | tr = transaction_free(tr); |
e5b5ae50 LP |
2174 | return 0; |
2175 | } | |
60918275 | 2176 | |
836e4e7e DDM |
2177 | int manager_add_job( |
2178 | Manager *m, | |
2179 | JobType type, | |
2180 | Unit *unit, | |
2181 | JobMode mode, | |
2182 | sd_bus_error *error, | |
2183 | Job **ret) { | |
2184 | ||
2185 | return manager_add_job_full(m, type, unit, mode, 0, NULL, error, ret); | |
2186 | } | |
2187 | ||
50cbaba4 | 2188 | int manager_add_job_by_name(Manager *m, JobType type, const char *name, JobMode mode, Set *affected_jobs, sd_bus_error *e, Job **ret) { |
4440b27d | 2189 | Unit *unit = NULL; /* just to appease gcc, initialization is not really necessary */ |
28247076 LP |
2190 | int r; |
2191 | ||
2192 | assert(m); | |
2193 | assert(type < _JOB_TYPE_MAX); | |
2194 | assert(name); | |
2195 | assert(mode < _JOB_MODE_MAX); | |
2196 | ||
c3090674 LP |
2197 | r = manager_load_unit(m, name, NULL, NULL, &unit); |
2198 | if (r < 0) | |
28247076 | 2199 | return r; |
4440b27d | 2200 | assert(unit); |
28247076 | 2201 | |
d993ad6c | 2202 | return manager_add_job_full(m, type, unit, mode, /* extra_flags = */ 0, affected_jobs, e, ret); |
53f18416 LP |
2203 | } |
2204 | ||
50cbaba4 | 2205 | int manager_add_job_by_name_and_warn(Manager *m, JobType type, const char *name, JobMode mode, Set *affected_jobs, Job **ret) { |
4afd3348 | 2206 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
53f18416 LP |
2207 | int r; |
2208 | ||
2209 | assert(m); | |
2210 | assert(type < _JOB_TYPE_MAX); | |
2211 | assert(name); | |
2212 | assert(mode < _JOB_MODE_MAX); | |
2213 | ||
50cbaba4 | 2214 | r = manager_add_job_by_name(m, type, name, mode, affected_jobs, &error, ret); |
53f18416 LP |
2215 | if (r < 0) |
2216 | return log_warning_errno(r, "Failed to enqueue %s job for %s: %s", job_mode_to_string(mode), name, bus_error_message(&error, r)); | |
2217 | ||
2218 | return r; | |
28247076 LP |
2219 | } |
2220 | ||
15d167f8 JW |
2221 | int manager_propagate_reload(Manager *m, Unit *unit, JobMode mode, sd_bus_error *e) { |
2222 | int r; | |
32620826 | 2223 | _cleanup_(transaction_abort_and_freep) Transaction *tr = NULL; |
15d167f8 JW |
2224 | |
2225 | assert(m); | |
2226 | assert(unit); | |
2227 | assert(mode < _JOB_MODE_MAX); | |
2228 | assert(mode != JOB_ISOLATE); /* Isolate is only valid for start */ | |
2229 | ||
2230 | tr = transaction_new(mode == JOB_REPLACE_IRREVERSIBLY); | |
2231 | if (!tr) | |
2232 | return -ENOMEM; | |
2233 | ||
2234 | /* We need an anchor job */ | |
b0904249 | 2235 | r = transaction_add_job_and_dependencies(tr, JOB_NOP, unit, NULL, TRANSACTION_IGNORE_REQUIREMENTS|TRANSACTION_IGNORE_ORDER, e); |
15d167f8 | 2236 | if (r < 0) |
32620826 | 2237 | return r; |
15d167f8 JW |
2238 | |
2239 | /* Failure in adding individual dependencies is ignored, so this always succeeds. */ | |
b0904249 LP |
2240 | transaction_add_propagate_reload_jobs( |
2241 | tr, | |
2242 | unit, | |
2243 | tr->anchor_job, | |
3044510d | 2244 | mode == JOB_IGNORE_DEPENDENCIES ? TRANSACTION_IGNORE_ORDER : 0); |
15d167f8 | 2245 | |
50cbaba4 | 2246 | r = transaction_activate(tr, m, mode, NULL, e); |
15d167f8 | 2247 | if (r < 0) |
32620826 | 2248 | return r; |
15d167f8 | 2249 | |
32620826 | 2250 | tr = transaction_free(tr); |
15d167f8 | 2251 | return 0; |
15d167f8 JW |
2252 | } |
2253 | ||
60918275 LP |
2254 | Job *manager_get_job(Manager *m, uint32_t id) { |
2255 | assert(m); | |
2256 | ||
2257 | return hashmap_get(m->jobs, UINT32_TO_PTR(id)); | |
2258 | } | |
2259 | ||
87f0e418 | 2260 | Unit *manager_get_unit(Manager *m, const char *name) { |
60918275 LP |
2261 | assert(m); |
2262 | assert(name); | |
2263 | ||
87f0e418 | 2264 | return hashmap_get(m->units, name); |
60918275 LP |
2265 | } |
2266 | ||
19496554 MS |
2267 | static int manager_dispatch_target_deps_queue(Manager *m) { |
2268 | Unit *u; | |
19496554 MS |
2269 | int r = 0; |
2270 | ||
19496554 MS |
2271 | assert(m); |
2272 | ||
52e3671b | 2273 | while ((u = LIST_POP(target_deps_queue, m->target_deps_queue))) { |
15ed3c3a LP |
2274 | _cleanup_free_ Unit **targets = NULL; |
2275 | int n_targets; | |
2276 | ||
19496554 MS |
2277 | assert(u->in_target_deps_queue); |
2278 | ||
19496554 MS |
2279 | u->in_target_deps_queue = false; |
2280 | ||
15ed3c3a LP |
2281 | /* Take an "atomic" snapshot of dependencies here, as the call below will likely modify the |
2282 | * dependencies, and we can't have it that hash tables we iterate through are modified while | |
2283 | * we are iterating through them. */ | |
2284 | n_targets = unit_get_dependency_array(u, UNIT_ATOM_DEFAULT_TARGET_DEPENDENCIES, &targets); | |
2285 | if (n_targets < 0) | |
2286 | return n_targets; | |
19496554 | 2287 | |
8b317c34 MY |
2288 | FOREACH_ARRAY(i, targets, n_targets) { |
2289 | r = unit_add_default_target_dependency(u, *i); | |
15ed3c3a LP |
2290 | if (r < 0) |
2291 | return r; | |
19496554 MS |
2292 | } |
2293 | } | |
2294 | ||
2295 | return r; | |
2296 | } | |
2297 | ||
c1e1601e | 2298 | unsigned manager_dispatch_load_queue(Manager *m) { |
595ed347 | 2299 | Unit *u; |
c1e1601e | 2300 | unsigned n = 0; |
60918275 LP |
2301 | |
2302 | assert(m); | |
2303 | ||
223dabab LP |
2304 | /* Make sure we are not run recursively */ |
2305 | if (m->dispatching_load_queue) | |
c1e1601e | 2306 | return 0; |
223dabab LP |
2307 | |
2308 | m->dispatching_load_queue = true; | |
2309 | ||
87f0e418 | 2310 | /* Dispatches the load queue. Takes a unit from the queue and |
60918275 LP |
2311 | * tries to load its data until the queue is empty */ |
2312 | ||
595ed347 MS |
2313 | while ((u = m->load_queue)) { |
2314 | assert(u->in_load_queue); | |
034c6ed7 | 2315 | |
595ed347 | 2316 | unit_load(u); |
c1e1601e | 2317 | n++; |
60918275 LP |
2318 | } |
2319 | ||
223dabab | 2320 | m->dispatching_load_queue = false; |
19496554 MS |
2321 | |
2322 | /* Dispatch the units waiting for their target dependencies to be added now, as all targets that we know about | |
2323 | * should be loaded and have aliases resolved */ | |
2324 | (void) manager_dispatch_target_deps_queue(m); | |
2325 | ||
c1e1601e | 2326 | return n; |
60918275 LP |
2327 | } |
2328 | ||
81be2388 | 2329 | bool manager_unit_cache_should_retry_load(Unit *u) { |
cda66772 LB |
2330 | assert(u); |
2331 | ||
81be2388 ZJS |
2332 | /* Automatic reloading from disk only applies to units which were not found sometime in the past, and |
2333 | * the not-found stub is kept pinned in the unit graph by dependencies. For units that were | |
2334 | * previously loaded, we don't do automatic reloading, and daemon-reload is necessary to update. */ | |
cda66772 LB |
2335 | if (u->load_state != UNIT_NOT_FOUND) |
2336 | return false; | |
2337 | ||
81be2388 ZJS |
2338 | /* The cache has been updated since the last time we tried to load the unit. There might be new |
2339 | * fragment paths to read. */ | |
c2911d48 | 2340 | if (u->manager->unit_cache_timestamp_hash != u->fragment_not_found_timestamp_hash) |
cda66772 | 2341 | return true; |
d904afc7 | 2342 | |
81be2388 | 2343 | /* The cache needs to be updated because there are modifications on disk. */ |
c2911d48 | 2344 | return !lookup_paths_timestamp_hash_same(&u->manager->lookup_paths, u->manager->unit_cache_timestamp_hash, NULL); |
d904afc7 LB |
2345 | } |
2346 | ||
c2756a68 LP |
2347 | int manager_load_unit_prepare( |
2348 | Manager *m, | |
2349 | const char *name, | |
2350 | const char *path, | |
718db961 | 2351 | sd_bus_error *e, |
4b6a2b3f | 2352 | Unit **ret) { |
c2756a68 | 2353 | |
4b6a2b3f | 2354 | _cleanup_(unit_freep) Unit *cleanup_unit = NULL; |
a99626c1 | 2355 | _cleanup_free_ char *nbuf = NULL; |
60918275 LP |
2356 | int r; |
2357 | ||
2358 | assert(m); | |
4b6a2b3f | 2359 | assert(ret); |
a99626c1 | 2360 | assert(name || path); |
60918275 | 2361 | |
526e3cbb | 2362 | /* This will prepare the unit for loading, but not actually load anything from disk. */ |
0301abf4 | 2363 | |
526e3cbb | 2364 | if (path && !path_is_absolute(path)) |
718db961 | 2365 | return sd_bus_error_setf(e, SD_BUS_ERROR_INVALID_ARGS, "Path %s is not absolute.", path); |
9e2f7c11 | 2366 | |
08f46856 | 2367 | if (!name) { |
a99626c1 LP |
2368 | r = path_extract_filename(path, &nbuf); |
2369 | if (r < 0) | |
2370 | return r; | |
2371 | if (r == O_DIRECTORY) | |
2372 | return sd_bus_error_setf(e, SD_BUS_ERROR_INVALID_ARGS, "Path '%s' refers to directory, refusing.", path); | |
2373 | ||
2374 | name = nbuf; | |
08f46856 | 2375 | } |
9e2f7c11 | 2376 | |
4b6a2b3f | 2377 | UnitType t = unit_name_to_type(name); |
7d17cfbc | 2378 | |
5d512d54 LN |
2379 | if (t == _UNIT_TYPE_INVALID || !unit_name_is_valid(name, UNIT_NAME_PLAIN|UNIT_NAME_INSTANCE)) { |
2380 | if (unit_name_is_valid(name, UNIT_NAME_TEMPLATE)) | |
2381 | return sd_bus_error_setf(e, SD_BUS_ERROR_INVALID_ARGS, "Unit name %s is missing the instance name.", name); | |
2382 | ||
718db961 | 2383 | return sd_bus_error_setf(e, SD_BUS_ERROR_INVALID_ARGS, "Unit name %s is not valid.", name); |
5d512d54 | 2384 | } |
60918275 | 2385 | |
4b6a2b3f ZJS |
2386 | Unit *unit = manager_get_unit(m, name); |
2387 | if (unit) { | |
93df5217 | 2388 | /* The time-based cache allows new units to be started without daemon-reload, |
d904afc7 LB |
2389 | * but if they are already referenced (because of dependencies or ordering) |
2390 | * then we have to force a load of the fragment. As an optimization, check | |
2391 | * first if anything in the usual paths was modified since the last time | |
7233e91a LB |
2392 | * the cache was loaded. Also check if the last time an attempt to load the |
2393 | * unit was made was before the most recent cache refresh, so that we know | |
81be2388 | 2394 | * we need to try again — even if the cache is current, it might have been |
7233e91a LB |
2395 | * updated in a different context before we had a chance to retry loading |
2396 | * this particular unit. */ | |
4b6a2b3f ZJS |
2397 | if (manager_unit_cache_should_retry_load(unit)) |
2398 | unit->load_state = UNIT_STUB; | |
d904afc7 | 2399 | else { |
4b6a2b3f | 2400 | *ret = unit; |
535b7fcb | 2401 | return 0; /* The unit was already loaded */ |
d904afc7 LB |
2402 | } |
2403 | } else { | |
4b6a2b3f ZJS |
2404 | unit = cleanup_unit = unit_new(m, unit_vtable[t]->object_size); |
2405 | if (!unit) | |
d904afc7 | 2406 | return -ENOMEM; |
034c6ed7 | 2407 | } |
60918275 | 2408 | |
7d17cfbc | 2409 | if (path) { |
4b6a2b3f | 2410 | r = free_and_strdup(&unit->fragment_path, path); |
d904afc7 LB |
2411 | if (r < 0) |
2412 | return r; | |
7d17cfbc | 2413 | } |
0301abf4 | 2414 | |
4b6a2b3f | 2415 | r = unit_add_name(unit, name); |
dc409696 | 2416 | if (r < 0) |
1ffba6fe | 2417 | return r; |
60918275 | 2418 | |
4b6a2b3f ZJS |
2419 | unit_add_to_load_queue(unit); |
2420 | unit_add_to_dbus_queue(unit); | |
2421 | unit_add_to_gc_queue(unit); | |
c1e1601e | 2422 | |
4b6a2b3f ZJS |
2423 | *ret = unit; |
2424 | TAKE_PTR(cleanup_unit); | |
db06e3b6 | 2425 | |
535b7fcb | 2426 | return 1; /* The unit was added the load queue */ |
db06e3b6 LP |
2427 | } |
2428 | ||
c2756a68 LP |
2429 | int manager_load_unit( |
2430 | Manager *m, | |
2431 | const char *name, | |
2432 | const char *path, | |
718db961 | 2433 | sd_bus_error *e, |
4b6a2b3f | 2434 | Unit **ret) { |
db06e3b6 LP |
2435 | int r; |
2436 | ||
2437 | assert(m); | |
4b6a2b3f | 2438 | assert(ret); |
db06e3b6 | 2439 | |
4b6a2b3f | 2440 | /* This will load the unit config, but not actually start any services or anything. */ |
db06e3b6 | 2441 | |
4b6a2b3f | 2442 | r = manager_load_unit_prepare(m, name, path, e, ret); |
535b7fcb | 2443 | if (r <= 0) |
db06e3b6 LP |
2444 | return r; |
2445 | ||
535b7fcb | 2446 | /* Unit was newly loaded */ |
f50e0a01 | 2447 | manager_dispatch_load_queue(m); |
4b6a2b3f | 2448 | *ret = unit_follow_merge(*ret); |
4109ede7 ZJS |
2449 | return 0; |
2450 | } | |
2451 | ||
2452 | int manager_load_startable_unit_or_warn( | |
2453 | Manager *m, | |
2454 | const char *name, | |
2455 | const char *path, | |
2456 | Unit **ret) { | |
2457 | ||
2458 | /* Load a unit, make sure it loaded fully and is not masked. */ | |
2459 | ||
2460 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; | |
2461 | Unit *unit; | |
2462 | int r; | |
2463 | ||
2464 | r = manager_load_unit(m, name, path, &error, &unit); | |
2465 | if (r < 0) | |
2466 | return log_error_errno(r, "Failed to load %s %s: %s", | |
8ace1db7 | 2467 | name ? "unit" : "unit file", name ?: path, |
4109ede7 | 2468 | bus_error_message(&error, r)); |
8ace1db7 LP |
2469 | |
2470 | r = bus_unit_validate_load_state(unit, &error); | |
2471 | if (r < 0) | |
2472 | return log_error_errno(r, "%s", bus_error_message(&error, r)); | |
9e2f7c11 | 2473 | |
4109ede7 | 2474 | *ret = unit; |
60918275 LP |
2475 | return 0; |
2476 | } | |
a66d02c3 | 2477 | |
7fad411c LP |
2478 | void manager_clear_jobs(Manager *m) { |
2479 | Job *j; | |
2480 | ||
2481 | assert(m); | |
2482 | ||
7fad411c | 2483 | while ((j = hashmap_first(m->jobs))) |
5273510e | 2484 | /* No need to recurse. We're cancelling all jobs. */ |
833f92ad | 2485 | job_finish_and_invalidate(j, JOB_CANCELED, false, false); |
7fad411c | 2486 | } |
83c60c9f | 2487 | |
d70dfe1b | 2488 | void manager_unwatch_pidref(Manager *m, const PidRef *pid) { |
f75f613d FB |
2489 | assert(m); |
2490 | ||
495e75ed LP |
2491 | for (;;) { |
2492 | Unit *u; | |
f75f613d | 2493 | |
495e75ed LP |
2494 | u = manager_get_unit_by_pidref_watching(m, pid); |
2495 | if (!u) | |
2496 | break; | |
2497 | ||
2498 | unit_unwatch_pidref(u, pid); | |
2499 | } | |
f75f613d FB |
2500 | } |
2501 | ||
752b5905 | 2502 | static int manager_dispatch_run_queue(sd_event_source *source, void *userdata) { |
99534007 | 2503 | Manager *m = ASSERT_PTR(userdata); |
83c60c9f | 2504 | Job *j; |
034c6ed7 | 2505 | |
752b5905 | 2506 | assert(source); |
9152c765 | 2507 | |
da8e1782 | 2508 | while ((j = prioq_peek(m->run_queue))) { |
ac1135be | 2509 | assert(j->installed); |
034c6ed7 LP |
2510 | assert(j->in_run_queue); |
2511 | ||
6e64994d | 2512 | (void) job_run_and_invalidate(j); |
9152c765 | 2513 | } |
034c6ed7 | 2514 | |
a0b64226 | 2515 | if (m->n_running_jobs > 0) |
03b717a3 MS |
2516 | manager_watch_jobs_in_progress(m); |
2517 | ||
31a7eb86 ZJS |
2518 | if (m->n_on_console > 0) |
2519 | manager_watch_idle_pipe(m); | |
2520 | ||
752b5905 | 2521 | return 1; |
c1e1601e LP |
2522 | } |
2523 | ||
b0c4b282 LP |
2524 | void manager_trigger_run_queue(Manager *m) { |
2525 | int r; | |
2526 | ||
2527 | assert(m); | |
2528 | ||
2529 | r = sd_event_source_set_enabled( | |
2530 | m->run_queue_event_source, | |
2531 | prioq_isempty(m->run_queue) ? SD_EVENT_OFF : SD_EVENT_ONESHOT); | |
2532 | if (r < 0) | |
2533 | log_warning_errno(r, "Failed to enable job run queue event source, ignoring: %m"); | |
2534 | } | |
2535 | ||
9588bc32 | 2536 | static unsigned manager_dispatch_dbus_queue(Manager *m) { |
e0a08581 | 2537 | unsigned n = 0, budget; |
595ed347 | 2538 | Unit *u; |
e0a08581 | 2539 | Job *j; |
c1e1601e LP |
2540 | |
2541 | assert(m); | |
2542 | ||
b8d381c4 LP |
2543 | /* When we are reloading, let's not wait with generating signals, since we need to exit the manager as quickly |
2544 | * as we can. There's no point in throttling generation of signals in that case. */ | |
2545 | if (MANAGER_IS_RELOADING(m) || m->send_reloading_done || m->pending_reload_message) | |
f5fbe71d | 2546 | budget = UINT_MAX; /* infinite budget in this case */ |
b8d381c4 LP |
2547 | else { |
2548 | /* Anything to do at all? */ | |
2549 | if (!m->dbus_unit_queue && !m->dbus_job_queue) | |
2550 | return 0; | |
2551 | ||
2552 | /* Do we have overly many messages queued at the moment? If so, let's not enqueue more on top, let's | |
2553 | * sit this cycle out, and process things in a later cycle when the queues got a bit emptier. */ | |
2554 | if (manager_bus_n_queued_write(m) > MANAGER_BUS_BUSY_THRESHOLD) | |
2555 | return 0; | |
2556 | ||
2557 | /* Only process a certain number of units/jobs per event loop iteration. Even if the bus queue wasn't | |
2558 | * overly full before this call we shouldn't increase it in size too wildly in one step, and we | |
2559 | * shouldn't monopolize CPU time with generating these messages. Note the difference in counting of | |
2560 | * this "budget" and the "threshold" above: the "budget" is decreased only once per generated message, | |
5238e957 BB |
2561 | * regardless how many buses/direct connections it is enqueued on, while the "threshold" is applied to |
2562 | * each queued instance of bus message, i.e. if the same message is enqueued to five buses/direct | |
b8d381c4 LP |
2563 | * connections it will be counted five times. This difference in counting ("references" |
2564 | * vs. "instances") is primarily a result of the fact that it's easier to implement it this way, | |
2565 | * however it also reflects the thinking that the "threshold" should put a limit on used queue memory, | |
2566 | * i.e. space, while the "budget" should put a limit on time. Also note that the "threshold" is | |
2567 | * currently chosen much higher than the "budget". */ | |
2568 | budget = MANAGER_BUS_MESSAGE_BUDGET; | |
2569 | } | |
e0a08581 | 2570 | |
b8d381c4 | 2571 | while (budget != 0 && (u = m->dbus_unit_queue)) { |
e0a08581 | 2572 | |
595ed347 | 2573 | assert(u->in_dbus_queue); |
c1e1601e | 2574 | |
595ed347 | 2575 | bus_unit_send_change_signal(u); |
b8d381c4 LP |
2576 | n++; |
2577 | ||
f5fbe71d | 2578 | if (budget != UINT_MAX) |
b8d381c4 | 2579 | budget--; |
c1e1601e LP |
2580 | } |
2581 | ||
b8d381c4 | 2582 | while (budget != 0 && (j = m->dbus_job_queue)) { |
c1e1601e LP |
2583 | assert(j->in_dbus_queue); |
2584 | ||
2585 | bus_job_send_change_signal(j); | |
b8d381c4 LP |
2586 | n++; |
2587 | ||
f5fbe71d | 2588 | if (budget != UINT_MAX) |
b8d381c4 | 2589 | budget--; |
c1e1601e LP |
2590 | } |
2591 | ||
b8d381c4 | 2592 | if (m->send_reloading_done) { |
71445ae7 | 2593 | m->send_reloading_done = false; |
718db961 | 2594 | bus_manager_send_reloading(m, false); |
b8d381c4 | 2595 | n++; |
71445ae7 LP |
2596 | } |
2597 | ||
b8d381c4 | 2598 | if (m->pending_reload_message) { |
209de525 | 2599 | bus_send_pending_reload_message(m); |
e0a08581 LP |
2600 | n++; |
2601 | } | |
718db961 | 2602 | |
c1e1601e | 2603 | return n; |
9152c765 LP |
2604 | } |
2605 | ||
5151b4cc | 2606 | static bool manager_process_barrier_fd(char * const *tags, FDSet *fds) { |
4f07ddfa KKD |
2607 | |
2608 | /* nothing else must be sent when using BARRIER=1 */ | |
5151b4cc | 2609 | if (strv_contains(tags, "BARRIER=1")) { |
46276454 | 2610 | if (strv_length(tags) != 1) |
5151b4cc | 2611 | log_warning("Extra notification messages sent with BARRIER=1, ignoring everything."); |
46276454 LP |
2612 | else if (fdset_size(fds) != 1) |
2613 | log_warning("Got incorrect number of fds with BARRIER=1, closing them."); | |
5151b4cc BR |
2614 | |
2615 | /* Drop the message if BARRIER=1 was found */ | |
4f07ddfa | 2616 | return true; |
5151b4cc | 2617 | } |
4f07ddfa KKD |
2618 | |
2619 | return false; | |
2620 | } | |
2621 | ||
db256aab LP |
2622 | static void manager_invoke_notify_message( |
2623 | Manager *m, | |
2624 | Unit *u, | |
19eccb76 | 2625 | PidRef *pidref, |
db256aab | 2626 | const struct ucred *ucred, |
5151b4cc | 2627 | char * const *tags, |
db256aab LP |
2628 | FDSet *fds) { |
2629 | ||
5ba6985b LP |
2630 | assert(m); |
2631 | assert(u); | |
19eccb76 | 2632 | assert(pidref_is_set(pidref)); |
db256aab | 2633 | assert(ucred); |
19eccb76 | 2634 | assert(pidref->pid == ucred->pid); |
5151b4cc | 2635 | assert(tags); |
5ba6985b | 2636 | |
62a76913 | 2637 | if (u->notifygen == m->notifygen) /* Already invoked on this same unit in this same iteration? */ |
5ba6985b | 2638 | return; |
62a76913 LP |
2639 | u->notifygen = m->notifygen; |
2640 | ||
5151b4cc | 2641 | if (UNIT_VTABLE(u)->notify_message) |
19eccb76 | 2642 | UNIT_VTABLE(u)->notify_message(u, pidref, ucred, tags, fds); |
62a76913 | 2643 | |
5151b4cc | 2644 | else if (DEBUG_LOGGING) { |
a2098d8b MY |
2645 | _cleanup_free_ char *joined = strv_join(tags, ", "); |
2646 | char buf[CELLESCAPE_DEFAULT_LENGTH]; | |
a86b7675 | 2647 | |
a2098d8b MY |
2648 | log_unit_debug(u, "Got notification message from unexpected unit type, ignoring: %s", |
2649 | joined ? cellescape(buf, sizeof(buf), joined) : "(null)"); | |
a86b7675 | 2650 | } |
5ba6985b LP |
2651 | } |
2652 | ||
4414bd59 LP |
2653 | static int manager_get_units_for_pidref(Manager *m, const PidRef *pidref, Unit ***ret_units) { |
2654 | /* Determine array of every unit that is interested in the specified process */ | |
2655 | ||
2656 | assert(m); | |
2657 | assert(pidref_is_set(pidref)); | |
2658 | ||
2659 | Unit *u1, *u2, **array; | |
2660 | u1 = manager_get_unit_by_pidref_cgroup(m, pidref); | |
2661 | u2 = hashmap_get(m->watch_pids, pidref); | |
2662 | array = hashmap_get(m->watch_pids_more, pidref); | |
2663 | ||
2664 | size_t n = 0; | |
2665 | if (u1) | |
2666 | n++; | |
2667 | if (u2) | |
2668 | n++; | |
2669 | if (array) | |
2670 | for (size_t j = 0; array[j]; j++) | |
2671 | n++; | |
2672 | ||
2673 | assert(n <= INT_MAX); /* Make sure we can reasonably return the counter as "int" */ | |
2674 | ||
2675 | if (ret_units) { | |
2676 | _cleanup_free_ Unit **units = NULL; | |
2677 | ||
2678 | if (n > 0) { | |
2679 | units = new(Unit*, n + 1); | |
2680 | if (!units) | |
2681 | return -ENOMEM; | |
2682 | ||
2683 | /* We return a dense array, and put the "main" unit first, i.e. unit in whose cgroup | |
2684 | * the process currently is. Note that we do not bother with filtering duplicates | |
2685 | * here. */ | |
2686 | ||
2687 | size_t i = 0; | |
2688 | if (u1) | |
2689 | units[i++] = u1; | |
2690 | if (u2) | |
2691 | units[i++] = u2; | |
2692 | if (array) | |
2693 | for (size_t j = 0; array[j]; j++) | |
2694 | units[i++] = array[j]; | |
2695 | assert(i == n); | |
2696 | ||
2697 | units[i] = NULL; /* end array in an extra NULL */ | |
2698 | } | |
2699 | ||
2700 | *ret_units = TAKE_PTR(units); | |
2701 | } | |
2702 | ||
2703 | return (int) n; | |
2704 | } | |
2705 | ||
718db961 | 2706 | static int manager_dispatch_notify_fd(sd_event_source *source, int fd, uint32_t revents, void *userdata) { |
99534007 | 2707 | Manager *m = ASSERT_PTR(userdata); |
5d09689b MY |
2708 | _cleanup_(pidref_done) PidRef pidref = PIDREF_NULL; |
2709 | struct ucred ucred; | |
5d09689b | 2710 | _cleanup_(fdset_free_asyncp) FDSet *fds = NULL; |
7d6e1293 | 2711 | int r; |
8c47c732 | 2712 | |
718db961 LP |
2713 | assert(m->notify_fd == fd); |
2714 | ||
2715 | if (revents != EPOLLIN) { | |
2716 | log_warning("Got unexpected poll event for notify fd."); | |
2717 | return 0; | |
2718 | } | |
8c47c732 | 2719 | |
19ade244 LP |
2720 | _cleanup_strv_free_ char **tags = NULL; |
2721 | r = notify_recv_with_fds_strv(m->notify_fd, &tags, &ucred, &pidref, &fds); | |
5d09689b | 2722 | if (r == -EAGAIN) |
882321a1 | 2723 | return 0; |
5d09689b | 2724 | if (r < 0) |
882321a1 ZJS |
2725 | /* If this is any other, real error, then stop processing this socket. This of course means |
2726 | * we won't take notification messages anymore, but that's still better than busy looping: | |
2727 | * being woken up over and over again, but being unable to actually read the message from the | |
2728 | * socket. */ | |
5d09689b | 2729 | return r; |
7d6e1293 | 2730 | |
8f41e6b6 | 2731 | /* Possibly a barrier fd, let's see. */ |
7b0a1267 | 2732 | if (manager_process_barrier_fd(tags, fds)) { |
5d09689b | 2733 | log_debug("Received barrier notification message from PID " PID_FMT ".", pidref.pid); |
4f07ddfa | 2734 | return 0; |
7b0a1267 | 2735 | } |
4f07ddfa | 2736 | |
62a76913 LP |
2737 | /* Increase the generation counter used for filtering out duplicate unit invocations. */ |
2738 | m->notifygen++; | |
2739 | ||
2740 | /* Notify every unit that might be interested, which might be multiple. */ | |
4414bd59 | 2741 | _cleanup_free_ Unit **array = NULL; |
5ba6985b | 2742 | |
4414bd59 LP |
2743 | int n_array = manager_get_units_for_pidref(m, &pidref, &array); |
2744 | if (n_array < 0) { | |
5d09689b | 2745 | log_warning_errno(n_array, "Failed to determine units for PID " PID_FMT ", ignoring: %m", pidref.pid); |
4414bd59 | 2746 | return 0; |
62a76913 | 2747 | } |
4414bd59 | 2748 | if (n_array == 0) |
5d09689b | 2749 | log_debug("Cannot find unit for notify message of PID "PID_FMT", ignoring.", pidref.pid); |
4414bd59 LP |
2750 | else |
2751 | /* And now invoke the per-unit callbacks. Note that manager_invoke_notify_message() will handle | |
2752 | * duplicate units – making sure we only invoke each unit's handler once. */ | |
2753 | FOREACH_ARRAY(u, array, n_array) | |
5d09689b | 2754 | manager_invoke_notify_message(m, *u, &pidref, &ucred, tags, fds); |
a354329f | 2755 | |
43127aeb | 2756 | if (!fdset_isempty(fds)) |
5fd2c135 | 2757 | log_warning("Got extra auxiliary fds with notification message, closing them."); |
8c47c732 LP |
2758 | |
2759 | return 0; | |
2760 | } | |
2761 | ||
62a76913 LP |
2762 | static void manager_invoke_sigchld_event( |
2763 | Manager *m, | |
2764 | Unit *u, | |
2765 | const siginfo_t *si) { | |
36f20ae3 | 2766 | |
5ba6985b LP |
2767 | assert(m); |
2768 | assert(u); | |
2769 | assert(si); | |
2770 | ||
62a76913 LP |
2771 | /* Already invoked the handler of this unit in this iteration? Then don't process this again */ |
2772 | if (u->sigchldgen == m->sigchldgen) | |
2773 | return; | |
2774 | u->sigchldgen = m->sigchldgen; | |
5ba6985b | 2775 | |
62a76913 | 2776 | log_unit_debug(u, "Child "PID_FMT" belongs to %s.", si->si_pid, u->id); |
eb355466 | 2777 | unit_unwatch_pidref(u, &PIDREF_MAKE_FROM_PID(si->si_pid)); |
e57051f5 | 2778 | |
62a76913 LP |
2779 | if (UNIT_VTABLE(u)->sigchld_event) |
2780 | UNIT_VTABLE(u)->sigchld_event(u, si->si_pid, si->si_code, si->si_status); | |
5ba6985b LP |
2781 | } |
2782 | ||
575b300b | 2783 | static int manager_dispatch_sigchld(sd_event_source *source, void *userdata) { |
99534007 | 2784 | Manager *m = ASSERT_PTR(userdata); |
575b300b LP |
2785 | siginfo_t si = {}; |
2786 | int r; | |
2787 | ||
2788 | assert(source); | |
9152c765 | 2789 | |
8f41e6b6 ZJS |
2790 | /* First we call waitid() for a PID and do not reap the zombie. That way we can still access |
2791 | * /proc/$PID for it while it is a zombie. */ | |
9152c765 | 2792 | |
575b300b | 2793 | if (waitid(P_ALL, 0, &si, WEXITED|WNOHANG|WNOWAIT) < 0) { |
acbb0225 | 2794 | |
8afabc50 AJ |
2795 | if (errno != ECHILD) |
2796 | log_error_errno(errno, "Failed to peek for child with waitid(), ignoring: %m"); | |
acbb0225 | 2797 | |
8afabc50 | 2798 | goto turn_off; |
575b300b | 2799 | } |
4112df16 | 2800 | |
575b300b LP |
2801 | if (si.si_pid <= 0) |
2802 | goto turn_off; | |
2803 | ||
37149e69 | 2804 | if (SIGINFO_CODE_IS_DEAD(si.si_code)) { |
575b300b | 2805 | _cleanup_free_ char *name = NULL; |
d7d74854 | 2806 | (void) pid_get_comm(si.si_pid, &name); |
575b300b LP |
2807 | |
2808 | log_debug("Child "PID_FMT" (%s) died (code=%s, status=%i/%s)", | |
2809 | si.si_pid, strna(name), | |
2810 | sigchld_code_to_string(si.si_code), | |
2811 | si.si_status, | |
2812 | strna(si.si_code == CLD_EXITED | |
2813 | ? exit_status_to_string(si.si_status, EXIT_STATUS_FULL) | |
2814 | : signal_to_string(si.si_status))); | |
2815 | ||
62a76913 LP |
2816 | /* Increase the generation counter used for filtering out duplicate unit invocations */ |
2817 | m->sigchldgen++; | |
2818 | ||
495e75ed LP |
2819 | /* We look this up by a PidRef that only consists of the PID. After all we couldn't create a |
2820 | * pidfd here any more even if we wanted (since the process just exited). */ | |
2821 | PidRef pidref = PIDREF_MAKE_FROM_PID(si.si_pid); | |
2822 | ||
58290af4 LP |
2823 | /* And now figure out the units this belongs to, there might be multiple... */ |
2824 | _cleanup_free_ Unit **array = NULL; | |
2825 | int n_array = manager_get_units_for_pidref(m, &pidref, &array); | |
2826 | if (n_array < 0) | |
2827 | log_warning_errno(n_array, "Failed to get units for process " PID_FMT ", ignoring: %m", si.si_pid); | |
2828 | else if (n_array == 0) | |
2829 | log_debug("Got SIGCHLD for process " PID_FMT " we weren't interested in, ignoring.", si.si_pid); | |
2830 | else { | |
2831 | /* We check for an OOM condition, in case we got SIGCHLD before the OOM notification. | |
2832 | * We only do this for the cgroup the PID belonged to, which is the f */ | |
2833 | (void) unit_check_oom(array[0]); | |
2ba6ae6b | 2834 | |
288bd406 | 2835 | /* We check if systemd-oomd performed a kill so that we log and notify appropriately */ |
58290af4 | 2836 | (void) unit_check_oomd_kill(array[0]); |
fe8d22fb | 2837 | |
58290af4 LP |
2838 | /* Finally, execute them all. Note that the array might contain duplicates, but that's fine, |
2839 | * manager_invoke_sigchld_event() will ensure we only invoke the handlers once for each | |
2840 | * iteration. */ | |
2841 | FOREACH_ARRAY(u, array, n_array) | |
2842 | manager_invoke_sigchld_event(m, *u, &si); | |
2ba6ae6b | 2843 | } |
575b300b | 2844 | } |
9152c765 | 2845 | |
575b300b LP |
2846 | /* And now, we actually reap the zombie. */ |
2847 | if (waitid(P_PID, si.si_pid, &si, WEXITED) < 0) { | |
2848 | log_error_errno(errno, "Failed to dequeue child, ignoring: %m"); | |
2849 | return 0; | |
2850 | } | |
9152c765 | 2851 | |
575b300b | 2852 | return 0; |
8c47c732 | 2853 | |
575b300b LP |
2854 | turn_off: |
2855 | /* All children processed for now, turn off event source */ | |
4112df16 | 2856 | |
575b300b LP |
2857 | r = sd_event_source_set_enabled(m->sigchld_event_source, SD_EVENT_OFF); |
2858 | if (r < 0) | |
2859 | return log_error_errno(r, "Failed to disable SIGCHLD event source: %m"); | |
9152c765 LP |
2860 | |
2861 | return 0; | |
2862 | } | |
2863 | ||
5f968096 ZJS |
2864 | static void manager_start_special(Manager *m, const char *name, JobMode mode) { |
2865 | Job *job; | |
398ef8ba | 2866 | |
5f968096 ZJS |
2867 | if (manager_add_job_by_name_and_warn(m, JOB_START, name, mode, NULL, &job) < 0) |
2868 | return; | |
1e001f52 | 2869 | |
5f968096 ZJS |
2870 | const char *s = unit_status_string(job->unit, NULL); |
2871 | ||
2872 | log_info("Activating special unit %s...", s); | |
3889fc6f | 2873 | |
fb44dc64 LP |
2874 | (void) sd_notifyf(/* unset_environment= */ false, |
2875 | "STATUS=Activating special unit %s...", s); | |
3889fc6f | 2876 | m->status_ready = false; |
28247076 LP |
2877 | } |
2878 | ||
24dd31c1 | 2879 | static void manager_handle_ctrl_alt_del(Manager *m) { |
58aec56d MY |
2880 | assert(m); |
2881 | ||
2882 | /* If the user presses C-A-D more than 7 times within 2s, we reboot/shutdown immediately, | |
2883 | * unless it was disabled in system.conf. */ | |
24dd31c1 | 2884 | |
7994ac1d | 2885 | if (ratelimit_below(&m->ctrl_alt_del_ratelimit) || m->cad_burst_action == EMERGENCY_ACTION_NONE) |
5f968096 | 2886 | manager_start_special(m, SPECIAL_CTRL_ALT_DEL_TARGET, JOB_REPLACE_IRREVERSIBLY); |
ae8c7939 | 2887 | else |
96a0cfbf LP |
2888 | emergency_action( |
2889 | m, | |
2890 | m->cad_burst_action, | |
2891 | EMERGENCY_ACTION_WARN, | |
2892 | /* reboot_arg= */ NULL, | |
2893 | /* exit_status= */ -1, | |
2894 | "Ctrl-Alt-Del was pressed more than 7 times within 2s"); | |
24dd31c1 LN |
2895 | } |
2896 | ||
718db961 | 2897 | static int manager_dispatch_signal_fd(sd_event_source *source, int fd, uint32_t revents, void *userdata) { |
99534007 | 2898 | Manager *m = ASSERT_PTR(userdata); |
9152c765 LP |
2899 | ssize_t n; |
2900 | struct signalfd_siginfo sfsi; | |
dacd6cee | 2901 | int r; |
9152c765 | 2902 | |
718db961 LP |
2903 | assert(m->signal_fd == fd); |
2904 | ||
2905 | if (revents != EPOLLIN) { | |
2906 | log_warning("Got unexpected events from signal file descriptor."); | |
2907 | return 0; | |
2908 | } | |
9152c765 | 2909 | |
575b300b | 2910 | n = read(m->signal_fd, &sfsi, sizeof(sfsi)); |
8add30a0 YW |
2911 | if (n < 0) { |
2912 | if (ERRNO_IS_TRANSIENT(errno)) | |
575b300b | 2913 | return 0; |
9152c765 | 2914 | |
575b300b LP |
2915 | /* We return an error here, which will kill this handler, |
2916 | * to avoid a busy loop on read error. */ | |
2917 | return log_error_errno(errno, "Reading from signal fd failed: %m"); | |
2918 | } | |
8add30a0 | 2919 | if (n != sizeof(sfsi)) { |
c0f86d66 | 2920 | log_warning("Truncated read from signal fd (%zi bytes), ignoring!", n); |
8add30a0 YW |
2921 | return 0; |
2922 | } | |
9152c765 | 2923 | |
575b300b LP |
2924 | log_received_signal(sfsi.ssi_signo == SIGCHLD || |
2925 | (sfsi.ssi_signo == SIGTERM && MANAGER_IS_USER(m)) | |
2926 | ? LOG_DEBUG : LOG_INFO, | |
2927 | &sfsi); | |
1e001f52 | 2928 | |
575b300b | 2929 | switch (sfsi.ssi_signo) { |
b9cd2ec1 | 2930 | |
575b300b LP |
2931 | case SIGCHLD: |
2932 | r = sd_event_source_set_enabled(m->sigchld_event_source, SD_EVENT_ON); | |
2933 | if (r < 0) | |
8afabc50 | 2934 | log_warning_errno(r, "Failed to enable SIGCHLD event source, ignoring: %m"); |
b9cd2ec1 | 2935 | |
575b300b | 2936 | break; |
84e9af1e | 2937 | |
575b300b LP |
2938 | case SIGTERM: |
2939 | if (MANAGER_IS_SYSTEM(m)) { | |
ba0c7754 | 2940 | /* This is for compatibility with the original sysvinit */ |
c52b19d6 | 2941 | m->objective = MANAGER_REEXECUTE; |
a1b256b0 | 2942 | break; |
575b300b | 2943 | } |
84e9af1e | 2944 | |
575b300b LP |
2945 | _fallthrough_; |
2946 | case SIGINT: | |
2947 | if (MANAGER_IS_SYSTEM(m)) | |
2948 | manager_handle_ctrl_alt_del(m); | |
2949 | else | |
5f968096 | 2950 | manager_start_special(m, SPECIAL_EXIT_TARGET, JOB_REPLACE_IRREVERSIBLY); |
575b300b | 2951 | break; |
84e9af1e | 2952 | |
575b300b | 2953 | case SIGWINCH: |
ba0c7754 | 2954 | /* This is a nop on non-init */ |
575b300b | 2955 | if (MANAGER_IS_SYSTEM(m)) |
5f968096 | 2956 | manager_start_special(m, SPECIAL_KBREQUEST_TARGET, JOB_REPLACE); |
84e9af1e | 2957 | |
575b300b | 2958 | break; |
84e9af1e | 2959 | |
575b300b | 2960 | case SIGPWR: |
ba0c7754 | 2961 | /* This is a nop on non-init */ |
575b300b | 2962 | if (MANAGER_IS_SYSTEM(m)) |
5f968096 | 2963 | manager_start_special(m, SPECIAL_SIGPWR_TARGET, JOB_REPLACE); |
6632c602 | 2964 | |
575b300b | 2965 | break; |
57ee42ce | 2966 | |
8559b3b7 | 2967 | case SIGUSR1: |
2b680534 | 2968 | if (manager_dbus_is_running(m, false)) { |
575b300b | 2969 | log_info("Trying to reconnect to bus..."); |
575b300b | 2970 | |
8559b3b7 LP |
2971 | (void) bus_init_api(m); |
2972 | ||
2973 | if (MANAGER_IS_SYSTEM(m)) | |
2974 | (void) bus_init_system(m); | |
5f968096 ZJS |
2975 | } else |
2976 | manager_start_special(m, SPECIAL_DBUS_SERVICE, JOB_REPLACE); | |
57ee42ce | 2977 | |
575b300b | 2978 | break; |
575b300b LP |
2979 | |
2980 | case SIGUSR2: { | |
2981 | _cleanup_free_ char *dump = NULL; | |
2982 | ||
d1d8786c | 2983 | r = manager_get_dump_string(m, /* patterns= */ NULL, &dump); |
575b300b | 2984 | if (r < 0) { |
af1690cf | 2985 | log_warning_errno(r, "Failed to acquire manager dump: %m"); |
57ee42ce LP |
2986 | break; |
2987 | } | |
2988 | ||
575b300b LP |
2989 | log_dump(LOG_INFO, dump); |
2990 | break; | |
2991 | } | |
2149e37c | 2992 | |
575b300b | 2993 | case SIGHUP: |
c52b19d6 | 2994 | m->objective = MANAGER_RELOAD; |
575b300b LP |
2995 | break; |
2996 | ||
2997 | default: { | |
2998 | ||
6851abe4 MY |
2999 | if (MANAGER_IS_SYSTEM(m)) { |
3000 | /* Starting SIGRTMIN+0 */ | |
3001 | static const struct { | |
3002 | const char *target; | |
3003 | JobMode mode; | |
3004 | } target_table[] = { | |
3005 | [0] = { SPECIAL_DEFAULT_TARGET, JOB_ISOLATE }, | |
3006 | [1] = { SPECIAL_RESCUE_TARGET, JOB_ISOLATE }, | |
3007 | [2] = { SPECIAL_EMERGENCY_TARGET, JOB_ISOLATE }, | |
3008 | [3] = { SPECIAL_HALT_TARGET, JOB_REPLACE_IRREVERSIBLY }, | |
3009 | [4] = { SPECIAL_POWEROFF_TARGET, JOB_REPLACE_IRREVERSIBLY }, | |
3010 | [5] = { SPECIAL_REBOOT_TARGET, JOB_REPLACE_IRREVERSIBLY }, | |
3011 | [6] = { SPECIAL_KEXEC_TARGET, JOB_REPLACE_IRREVERSIBLY }, | |
3012 | [7] = { SPECIAL_SOFT_REBOOT_TARGET, JOB_REPLACE_IRREVERSIBLY }, | |
3013 | }; | |
3014 | ||
3015 | /* Starting SIGRTMIN+13, so that target halt and system halt are 10 apart */ | |
3016 | static const ManagerObjective objective_table[] = { | |
3017 | [0] = MANAGER_HALT, | |
3018 | [1] = MANAGER_POWEROFF, | |
3019 | [2] = MANAGER_REBOOT, | |
3020 | [3] = MANAGER_KEXEC, | |
3021 | [4] = MANAGER_SOFT_REBOOT, | |
3022 | }; | |
3023 | ||
3024 | if ((int) sfsi.ssi_signo >= SIGRTMIN+0 && | |
3025 | (int) sfsi.ssi_signo < SIGRTMIN+(int) ELEMENTSOF(target_table)) { | |
3026 | int idx = (int) sfsi.ssi_signo - SIGRTMIN; | |
3027 | manager_start_special(m, target_table[idx].target, target_table[idx].mode); | |
3028 | break; | |
3029 | } | |
1005d14f | 3030 | |
6851abe4 MY |
3031 | if ((int) sfsi.ssi_signo >= SIGRTMIN+13 && |
3032 | (int) sfsi.ssi_signo < SIGRTMIN+13+(int) ELEMENTSOF(objective_table)) { | |
3033 | m->objective = objective_table[sfsi.ssi_signo - SIGRTMIN - 13]; | |
3034 | break; | |
3035 | } | |
575b300b LP |
3036 | } |
3037 | ||
3038 | switch (sfsi.ssi_signo - SIGRTMIN) { | |
3039 | ||
29e6b0c1 LP |
3040 | case 18: { |
3041 | bool generic = false; | |
3042 | ||
3043 | if (sfsi.ssi_code != SI_QUEUE) | |
3044 | generic = true; | |
3045 | else { | |
3046 | /* Override a few select commands by our own PID1-specific logic */ | |
3047 | ||
3048 | switch (sfsi.ssi_int) { | |
3049 | ||
3050 | case _COMMON_SIGNAL_COMMAND_LOG_LEVEL_BASE..._COMMON_SIGNAL_COMMAND_LOG_LEVEL_END: | |
3051 | manager_override_log_level(m, sfsi.ssi_int - _COMMON_SIGNAL_COMMAND_LOG_LEVEL_BASE); | |
3052 | break; | |
3053 | ||
3054 | case COMMON_SIGNAL_COMMAND_CONSOLE: | |
3055 | manager_override_log_target(m, LOG_TARGET_CONSOLE); | |
3056 | break; | |
3057 | ||
3058 | case COMMON_SIGNAL_COMMAND_JOURNAL: | |
3059 | manager_override_log_target(m, LOG_TARGET_JOURNAL); | |
3060 | break; | |
3061 | ||
3062 | case COMMON_SIGNAL_COMMAND_KMSG: | |
3063 | manager_override_log_target(m, LOG_TARGET_KMSG); | |
3064 | break; | |
3065 | ||
3066 | case COMMON_SIGNAL_COMMAND_NULL: | |
3067 | manager_override_log_target(m, LOG_TARGET_NULL); | |
3068 | break; | |
3069 | ||
0112c37c LB |
3070 | case MANAGER_SIGNAL_COMMAND_DUMP_JOBS: { |
3071 | _cleanup_free_ char *dump_jobs = NULL; | |
3072 | ||
3073 | r = manager_get_dump_jobs_string(m, /* patterns= */ NULL, " ", &dump_jobs); | |
3074 | if (r < 0) { | |
af1690cf | 3075 | log_warning_errno(r, "Failed to acquire manager jobs dump: %m"); |
0112c37c LB |
3076 | break; |
3077 | } | |
3078 | ||
3079 | log_dump(LOG_INFO, dump_jobs); | |
3080 | break; | |
3081 | } | |
3082 | ||
29e6b0c1 LP |
3083 | default: |
3084 | generic = true; | |
3085 | } | |
3086 | } | |
3087 | ||
3088 | if (generic) | |
3089 | return sigrtmin18_handler(source, &sfsi, NULL); | |
3090 | ||
3091 | break; | |
3092 | } | |
3093 | ||
575b300b | 3094 | case 20: |
43bba15a | 3095 | manager_override_show_status(m, SHOW_STATUS_YES, "signal"); |
a16e1123 LP |
3096 | break; |
3097 | ||
575b300b | 3098 | case 21: |
43bba15a | 3099 | manager_override_show_status(m, SHOW_STATUS_NO, "signal"); |
575b300b | 3100 | break; |
7d793605 | 3101 | |
575b300b | 3102 | case 22: |
a6ecbf83 | 3103 | manager_override_log_level(m, LOG_DEBUG); |
575b300b LP |
3104 | break; |
3105 | ||
3106 | case 23: | |
a6ecbf83 | 3107 | manager_restore_original_log_level(m); |
575b300b | 3108 | break; |
0003d1ab | 3109 | |
575b300b LP |
3110 | case 24: |
3111 | if (MANAGER_IS_USER(m)) { | |
af41e508 | 3112 | m->objective = MANAGER_EXIT; |
575b300b | 3113 | return 0; |
0658666b | 3114 | } |
9152c765 | 3115 | |
575b300b LP |
3116 | /* This is a nop on init */ |
3117 | break; | |
3118 | ||
463aef23 FB |
3119 | case 25: |
3120 | m->objective = MANAGER_REEXECUTE; | |
3121 | break; | |
3122 | ||
575b300b LP |
3123 | case 26: |
3124 | case 29: /* compatibility: used to be mapped to LOG_TARGET_SYSLOG_OR_KMSG */ | |
bda7d78b | 3125 | manager_restore_original_log_target(m); |
575b300b LP |
3126 | break; |
3127 | ||
3128 | case 27: | |
bda7d78b | 3129 | manager_override_log_target(m, LOG_TARGET_CONSOLE); |
575b300b LP |
3130 | break; |
3131 | ||
3132 | case 28: | |
bda7d78b | 3133 | manager_override_log_target(m, LOG_TARGET_KMSG); |
575b300b LP |
3134 | break; |
3135 | ||
3136 | default: | |
3137 | log_warning("Got unhandled signal <%s>.", signal_to_string(sfsi.ssi_signo)); | |
3138 | } | |
3139 | }} | |
034c6ed7 LP |
3140 | |
3141 | return 0; | |
3142 | } | |
3143 | ||
718db961 | 3144 | static int manager_dispatch_time_change_fd(sd_event_source *source, int fd, uint32_t revents, void *userdata) { |
99534007 | 3145 | Manager *m = ASSERT_PTR(userdata); |
718db961 | 3146 | Unit *u; |
034c6ed7 | 3147 | |
a80c1575 | 3148 | log_struct(LOG_DEBUG, |
3cf6a3a3 | 3149 | LOG_MESSAGE_ID(SD_MESSAGE_TIME_CHANGE_STR), |
a1230ff9 | 3150 | LOG_MESSAGE("Time has been changed")); |
034c6ed7 | 3151 | |
718db961 | 3152 | /* Restart the watch */ |
7feedd18 | 3153 | (void) manager_setup_time_change(m); |
4e434314 | 3154 | |
90e74a66 | 3155 | HASHMAP_FOREACH(u, m->units) |
718db961 LP |
3156 | if (UNIT_VTABLE(u)->time_change) |
3157 | UNIT_VTABLE(u)->time_change(u); | |
ea430986 | 3158 | |
718db961 LP |
3159 | return 0; |
3160 | } | |
ea430986 | 3161 | |
bbf5fd8e LP |
3162 | static int manager_dispatch_timezone_change( |
3163 | sd_event_source *source, | |
3164 | const struct inotify_event *e, | |
3165 | void *userdata) { | |
3166 | ||
99534007 | 3167 | Manager *m = ASSERT_PTR(userdata); |
bbf5fd8e | 3168 | int changed; |
bbf5fd8e LP |
3169 | Unit *u; |
3170 | ||
bbf5fd8e LP |
3171 | log_debug("inotify event for /etc/localtime"); |
3172 | ||
3173 | changed = manager_read_timezone_stat(m); | |
f20db199 | 3174 | if (changed <= 0) |
bbf5fd8e | 3175 | return changed; |
bbf5fd8e LP |
3176 | |
3177 | /* Something changed, restart the watch, to ensure we watch the new /etc/localtime if it changed */ | |
3178 | (void) manager_setup_timezone_change(m); | |
3179 | ||
3180 | /* Read the new timezone */ | |
3181 | tzset(); | |
3182 | ||
3183 | log_debug("Timezone has been changed (now: %s).", tzname[daylight]); | |
3184 | ||
90e74a66 | 3185 | HASHMAP_FOREACH(u, m->units) |
bbf5fd8e LP |
3186 | if (UNIT_VTABLE(u)->timezone_change) |
3187 | UNIT_VTABLE(u)->timezone_change(u); | |
3188 | ||
3189 | return 0; | |
3190 | } | |
3191 | ||
718db961 | 3192 | static int manager_dispatch_idle_pipe_fd(sd_event_source *source, int fd, uint32_t revents, void *userdata) { |
99534007 | 3193 | Manager *m = ASSERT_PTR(userdata); |
8742514c | 3194 | |
718db961 | 3195 | assert(m->idle_pipe[2] == fd); |
8742514c | 3196 | |
8f41e6b6 ZJS |
3197 | /* There's at least one Type=idle child that just gave up on us waiting for the boot process to |
3198 | * complete. Let's now turn off any further console output if there's at least one service that needs | |
3199 | * console access, so that from now on our own output should not spill into that service's output | |
3200 | * anymore. After all, we support Type=idle only to beautify console output and it generally is set | |
3201 | * on services that want to own the console exclusively without our interference. */ | |
718db961 | 3202 | m->no_console_output = m->n_on_console > 0; |
03b717a3 | 3203 | |
30fd9a2d | 3204 | /* Acknowledge the child's request, and let all other children know too that they shouldn't wait |
8f41e6b6 | 3205 | * any longer by closing the pipes towards them, which is what they are waiting for. */ |
718db961 | 3206 | manager_close_idle_pipe(m); |
03b717a3 | 3207 | |
718db961 LP |
3208 | return 0; |
3209 | } | |
31a7eb86 | 3210 | |
718db961 | 3211 | static int manager_dispatch_jobs_in_progress(sd_event_source *source, usec_t usec, void *userdata) { |
99534007 | 3212 | Manager *m = ASSERT_PTR(userdata); |
fd08a840 | 3213 | int r; |
31a7eb86 | 3214 | |
fd08a840 | 3215 | assert(source); |
9152c765 | 3216 | |
718db961 | 3217 | manager_print_jobs_in_progress(m); |
fd08a840 | 3218 | |
39cf0351 | 3219 | r = sd_event_source_set_time_relative(source, JOBS_IN_PROGRESS_PERIOD_USEC); |
fd08a840 ZJS |
3220 | if (r < 0) |
3221 | return r; | |
3222 | ||
3223 | return sd_event_source_set_enabled(source, SD_EVENT_ONESHOT); | |
9152c765 LP |
3224 | } |
3225 | ||
3226 | int manager_loop(Manager *m) { | |
8c227e7f | 3227 | RateLimit rl = { .interval = 1*USEC_PER_SEC, .burst = 50000 }; |
9152c765 | 3228 | int r; |
9152c765 LP |
3229 | |
3230 | assert(m); | |
1fb70e66 | 3231 | assert(m->objective == MANAGER_OK); /* Ensure manager_startup() has been called */ |
9152c765 | 3232 | |
b0c918b9 LP |
3233 | manager_check_finished(m); |
3234 | ||
575b300b LP |
3235 | /* There might still be some zombies hanging around from before we were exec()'ed. Let's reap them. */ |
3236 | r = sd_event_source_set_enabled(m->sigchld_event_source, SD_EVENT_ON); | |
e96d6be7 | 3237 | if (r < 0) |
575b300b | 3238 | return log_error_errno(r, "Failed to enable SIGCHLD event source: %m"); |
a4312405 | 3239 | |
af41e508 | 3240 | while (m->objective == MANAGER_OK) { |
9152c765 | 3241 | |
7994ac1d | 3242 | if (!ratelimit_below(&rl)) { |
ea430986 LP |
3243 | /* Yay, something is going seriously wrong, pause a little */ |
3244 | log_warning("Looping too fast. Throttling execution a little."); | |
3245 | sleep(1); | |
3246 | } | |
3247 | ||
a9cee8f4 ZJS |
3248 | (void) watchdog_ping(); |
3249 | ||
37a8e683 | 3250 | if (manager_dispatch_load_queue(m) > 0) |
23a177ef LP |
3251 | continue; |
3252 | ||
c5a97ed1 LP |
3253 | if (manager_dispatch_gc_job_queue(m) > 0) |
3254 | continue; | |
3255 | ||
3256 | if (manager_dispatch_gc_unit_queue(m) > 0) | |
701cc384 LP |
3257 | continue; |
3258 | ||
cf1265e1 | 3259 | if (manager_dispatch_cleanup_queue(m) > 0) |
c1e1601e | 3260 | continue; |
034c6ed7 | 3261 | |
91a6073e | 3262 | if (manager_dispatch_cgroup_realize_queue(m) > 0) |
c1e1601e LP |
3263 | continue; |
3264 | ||
0bc488c9 LP |
3265 | if (manager_dispatch_start_when_upheld_queue(m) > 0) |
3266 | continue; | |
3267 | ||
56c59592 LP |
3268 | if (manager_dispatch_stop_when_bound_queue(m) > 0) |
3269 | continue; | |
3270 | ||
a3c1168a LP |
3271 | if (manager_dispatch_stop_when_unneeded_queue(m) > 0) |
3272 | continue; | |
3273 | ||
6ac62d61 LP |
3274 | if (manager_dispatch_release_resources_queue(m) > 0) |
3275 | continue; | |
3276 | ||
c1e1601e | 3277 | if (manager_dispatch_dbus_queue(m) > 0) |
ea430986 | 3278 | continue; |
ea430986 | 3279 | |
c5f8a179 | 3280 | /* Sleep for watchdog runtime wait time */ |
ab596e4c | 3281 | r = sd_event_run(m->event, watchdog_runtime_wait(/* divisor= */ 2)); |
23bbb0de MS |
3282 | if (r < 0) |
3283 | return log_error_errno(r, "Failed to run event loop: %m"); | |
a16e1123 | 3284 | } |
957ca890 | 3285 | |
af41e508 | 3286 | return m->objective; |
83c60c9f | 3287 | } |
ea430986 | 3288 | |
718db961 | 3289 | int manager_load_unit_from_dbus_path(Manager *m, const char *s, sd_bus_error *e, Unit **_u) { |
ede3a796 | 3290 | _cleanup_free_ char *n = NULL; |
4b58153d | 3291 | sd_id128_t invocation_id; |
ea430986 | 3292 | Unit *u; |
80fbf05e | 3293 | int r; |
ea430986 LP |
3294 | |
3295 | assert(m); | |
3296 | assert(s); | |
3297 | assert(_u); | |
3298 | ||
ede3a796 LP |
3299 | r = unit_name_from_dbus_path(s, &n); |
3300 | if (r < 0) | |
3301 | return r; | |
ea430986 | 3302 | |
da890466 | 3303 | /* Permit addressing units by invocation ID: if the passed bus path is suffixed by a 128-bit ID then |
8f41e6b6 | 3304 | * we use it as invocation ID. */ |
4b58153d LP |
3305 | r = sd_id128_from_string(n, &invocation_id); |
3306 | if (r >= 0) { | |
3307 | u = hashmap_get(m->units_by_invocation_id, &invocation_id); | |
3308 | if (u) { | |
3309 | *_u = u; | |
3310 | return 0; | |
3311 | } | |
3312 | ||
930c124c ZJS |
3313 | return sd_bus_error_setf(e, BUS_ERROR_NO_UNIT_FOR_INVOCATION_ID, |
3314 | "No unit with the specified invocation ID " SD_ID128_FORMAT_STR " known.", | |
3315 | SD_ID128_FORMAT_VAL(invocation_id)); | |
4b58153d LP |
3316 | } |
3317 | ||
00c83b43 | 3318 | /* If this didn't work, we check if this is a unit name */ |
930c124c ZJS |
3319 | if (!unit_name_is_valid(n, UNIT_NAME_PLAIN|UNIT_NAME_INSTANCE)) { |
3320 | _cleanup_free_ char *nn = NULL; | |
3321 | ||
3322 | nn = cescape(n); | |
3323 | return sd_bus_error_setf(e, SD_BUS_ERROR_INVALID_ARGS, | |
3324 | "Unit name %s is neither a valid invocation ID nor unit name.", strnull(nn)); | |
3325 | } | |
00c83b43 | 3326 | |
80fbf05e | 3327 | r = manager_load_unit(m, n, NULL, e, &u); |
80fbf05e MS |
3328 | if (r < 0) |
3329 | return r; | |
ea430986 LP |
3330 | |
3331 | *_u = u; | |
ea430986 LP |
3332 | return 0; |
3333 | } | |
86fbf370 LP |
3334 | |
3335 | int manager_get_job_from_dbus_path(Manager *m, const char *s, Job **_j) { | |
718db961 | 3336 | const char *p; |
86fbf370 | 3337 | unsigned id; |
718db961 | 3338 | Job *j; |
86fbf370 LP |
3339 | int r; |
3340 | ||
3341 | assert(m); | |
3342 | assert(s); | |
3343 | assert(_j); | |
3344 | ||
718db961 LP |
3345 | p = startswith(s, "/org/freedesktop/systemd1/job/"); |
3346 | if (!p) | |
86fbf370 LP |
3347 | return -EINVAL; |
3348 | ||
718db961 | 3349 | r = safe_atou(p, &id); |
8742514c | 3350 | if (r < 0) |
86fbf370 LP |
3351 | return r; |
3352 | ||
8742514c LP |
3353 | j = manager_get_job(m, id); |
3354 | if (!j) | |
86fbf370 LP |
3355 | return -ENOENT; |
3356 | ||
3357 | *_j = j; | |
3358 | ||
3359 | return 0; | |
3360 | } | |
dfcd764e | 3361 | |
4927fcae | 3362 | void manager_send_unit_audit(Manager *m, Unit *u, int type, bool success) { |
e537352b | 3363 | |
349cc4a5 | 3364 | #if HAVE_AUDIT |
2ba11090 | 3365 | _cleanup_free_ char *p = NULL; |
0aa281df | 3366 | const char *msg; |
7410616c | 3367 | int audit_fd, r; |
e537352b | 3368 | |
ad60cdd0 LP |
3369 | assert(m); |
3370 | assert(u); | |
3371 | ||
463d0d15 | 3372 | if (!MANAGER_IS_SYSTEM(m)) |
a1a078ee LP |
3373 | return; |
3374 | ||
ad60cdd0 LP |
3375 | /* Don't generate audit events if the service was already started and we're just deserializing */ |
3376 | if (MANAGER_IS_RELOADING(m)) | |
e537352b LP |
3377 | return; |
3378 | ||
0dba7b5c | 3379 | audit_fd = get_core_audit_fd(); |
ad60cdd0 | 3380 | if (audit_fd < 0) |
bbd3a7ba LP |
3381 | return; |
3382 | ||
7410616c LP |
3383 | r = unit_name_to_prefix_and_instance(u->id, &p); |
3384 | if (r < 0) { | |
d52b8493 | 3385 | log_warning_errno(r, "Failed to extract prefix and instance of unit name, ignoring: %m"); |
e537352b LP |
3386 | return; |
3387 | } | |
3388 | ||
63c372cb | 3389 | msg = strjoina("unit=", p); |
0aa281df | 3390 | if (audit_log_user_comm_message(audit_fd, type, msg, "systemd", NULL, NULL, NULL, success) < 0) { |
d52b8493 LP |
3391 | if (ERRNO_IS_PRIVILEGE(errno)) { |
3392 | /* We aren't allowed to send audit messages? Then let's not retry again. */ | |
3393 | log_debug_errno(errno, "Failed to send audit message, closing audit socket: %m"); | |
0dba7b5c | 3394 | close_core_audit_fd(); |
d52b8493 LP |
3395 | } else |
3396 | log_warning_errno(errno, "Failed to send audit message, ignoring: %m"); | |
391ade86 | 3397 | } |
4927fcae | 3398 | #endif |
e537352b LP |
3399 | } |
3400 | ||
e983b760 | 3401 | void manager_send_unit_plymouth(Manager *m, Unit *u) { |
2ba11090 | 3402 | _cleanup_free_ char *message = NULL; |
aa25e19b | 3403 | int c, r; |
e983b760 | 3404 | |
ad60cdd0 LP |
3405 | assert(m); |
3406 | assert(u); | |
e983b760 | 3407 | |
463d0d15 | 3408 | if (!MANAGER_IS_SYSTEM(m)) |
e983b760 LP |
3409 | return; |
3410 | ||
ad60cdd0 LP |
3411 | /* Don't generate plymouth events if the service was already started and we're just deserializing */ |
3412 | if (MANAGER_IS_RELOADING(m)) | |
3413 | return; | |
3414 | ||
75f86906 | 3415 | if (detect_container() > 0) |
3772995a LP |
3416 | return; |
3417 | ||
b2bfd121 | 3418 | if (!UNIT_VTABLE(u)->notify_plymouth) |
e983b760 LP |
3419 | return; |
3420 | ||
aa25e19b LP |
3421 | c = asprintf(&message, "U\x02%c%s%c", (int) (strlen(u->id) + 1), u->id, '\x00'); |
3422 | if (c < 0) | |
305757d8 | 3423 | return (void) log_oom(); |
e983b760 | 3424 | |
aa25e19b LP |
3425 | /* We set SOCK_NONBLOCK here so that we rather drop the message then wait for plymouth */ |
3426 | r = plymouth_send_raw(message, c, SOCK_NONBLOCK); | |
3427 | if (r < 0) | |
3428 | log_full_errno(ERRNO_IS_NO_PLYMOUTH(r) ? LOG_DEBUG : LOG_WARNING, r, | |
3429 | "Failed to communicate with plymouth: %m"); | |
e983b760 LP |
3430 | } |
3431 | ||
b2d6bb5b LP |
3432 | void manager_send_unit_supervisor(Manager *m, Unit *u, bool active) { |
3433 | assert(m); | |
3434 | assert(u); | |
3435 | ||
3436 | /* Notify a "supervisor" process about our progress, i.e. a container manager, hypervisor, or | |
3437 | * surrounding service manager. */ | |
3438 | ||
3439 | if (MANAGER_IS_RELOADING(m)) | |
3440 | return; | |
3441 | ||
3442 | if (!UNIT_VTABLE(u)->notify_supervisor) | |
3443 | return; | |
3444 | ||
3445 | if (in_initrd()) /* Only send these once we left the initrd */ | |
3446 | return; | |
3447 | ||
3448 | (void) sd_notifyf(/* unset_environment= */ false, | |
3449 | active ? "X_SYSTEMD_UNIT_ACTIVE=%s" : "X_SYSTEMD_UNIT_INACTIVE=%s", | |
3450 | u->id); | |
3451 | } | |
3452 | ||
986935cf FB |
3453 | usec_t manager_get_watchdog(Manager *m, WatchdogType t) { |
3454 | assert(m); | |
3455 | ||
3456 | if (MANAGER_IS_USER(m)) | |
3457 | return USEC_INFINITY; | |
3458 | ||
902ea119 | 3459 | if (m->watchdog_overridden[t] != USEC_INFINITY) |
986935cf FB |
3460 | return m->watchdog_overridden[t]; |
3461 | ||
3462 | return m->watchdog[t]; | |
3463 | } | |
3464 | ||
3465 | void manager_set_watchdog(Manager *m, WatchdogType t, usec_t timeout) { | |
986935cf FB |
3466 | |
3467 | assert(m); | |
3468 | ||
3469 | if (MANAGER_IS_USER(m)) | |
3470 | return; | |
3471 | ||
902ea119 CK |
3472 | if (m->watchdog_overridden[t] == USEC_INFINITY) { |
3473 | if (t == WATCHDOG_RUNTIME) | |
f16890f8 | 3474 | (void) watchdog_setup(timeout); |
902ea119 | 3475 | else if (t == WATCHDOG_PRETIMEOUT) |
5717062e | 3476 | (void) watchdog_setup_pretimeout(timeout); |
902ea119 | 3477 | } |
986935cf | 3478 | |
61927b9f | 3479 | m->watchdog[t] = timeout; |
986935cf FB |
3480 | } |
3481 | ||
aac47032 | 3482 | void manager_override_watchdog(Manager *m, WatchdogType t, usec_t timeout) { |
902ea119 | 3483 | usec_t usec; |
986935cf FB |
3484 | |
3485 | assert(m); | |
3486 | ||
3487 | if (MANAGER_IS_USER(m)) | |
aac47032 | 3488 | return; |
986935cf | 3489 | |
9b5560f3 | 3490 | usec = timeout == USEC_INFINITY ? m->watchdog[t] : timeout; |
902ea119 | 3491 | if (t == WATCHDOG_RUNTIME) |
f16890f8 | 3492 | (void) watchdog_setup(usec); |
902ea119 CK |
3493 | else if (t == WATCHDOG_PRETIMEOUT) |
3494 | (void) watchdog_setup_pretimeout(usec); | |
986935cf | 3495 | |
61927b9f | 3496 | m->watchdog_overridden[t] = timeout; |
986935cf FB |
3497 | } |
3498 | ||
aff3a9e1 LB |
3499 | int manager_set_watchdog_pretimeout_governor(Manager *m, const char *governor) { |
3500 | _cleanup_free_ char *p = NULL; | |
3501 | int r; | |
3502 | ||
3503 | assert(m); | |
3504 | ||
3505 | if (MANAGER_IS_USER(m)) | |
3506 | return 0; | |
3507 | ||
3508 | if (streq_ptr(m->watchdog_pretimeout_governor, governor)) | |
3509 | return 0; | |
3510 | ||
3511 | p = strdup(governor); | |
3512 | if (!p) | |
3513 | return -ENOMEM; | |
3514 | ||
3515 | r = watchdog_setup_pretimeout_governor(governor); | |
3516 | if (r < 0) | |
3517 | return r; | |
3518 | ||
3519 | return free_and_replace(m->watchdog_pretimeout_governor, p); | |
3520 | } | |
3521 | ||
3522 | int manager_override_watchdog_pretimeout_governor(Manager *m, const char *governor) { | |
3523 | _cleanup_free_ char *p = NULL; | |
3524 | int r; | |
3525 | ||
3526 | assert(m); | |
3527 | ||
3528 | if (MANAGER_IS_USER(m)) | |
3529 | return 0; | |
3530 | ||
3531 | if (streq_ptr(m->watchdog_pretimeout_governor_overridden, governor)) | |
3532 | return 0; | |
3533 | ||
3534 | p = strdup(governor); | |
3535 | if (!p) | |
3536 | return -ENOMEM; | |
3537 | ||
3538 | r = watchdog_setup_pretimeout_governor(governor); | |
3539 | if (r < 0) | |
3540 | return r; | |
3541 | ||
3542 | return free_and_replace(m->watchdog_pretimeout_governor_overridden, p); | |
3543 | } | |
3544 | ||
a16e1123 | 3545 | int manager_reload(Manager *m) { |
d7ac0952 | 3546 | _unused_ _cleanup_(manager_reloading_stopp) Manager *reloading = NULL; |
51d122af | 3547 | _cleanup_fdset_free_ FDSet *fds = NULL; |
6a33af40 LP |
3548 | _cleanup_fclose_ FILE *f = NULL; |
3549 | int r; | |
a16e1123 LP |
3550 | |
3551 | assert(m); | |
3552 | ||
07719a21 LP |
3553 | r = manager_open_serialization(m, &f); |
3554 | if (r < 0) | |
6a33af40 | 3555 | return log_error_errno(r, "Failed to create serialization file: %m"); |
38c52d46 | 3556 | |
07719a21 | 3557 | fds = fdset_new(); |
6a33af40 LP |
3558 | if (!fds) |
3559 | return log_oom(); | |
3560 | ||
d147e2b6 ZJS |
3561 | /* We are officially in reload mode from here on. */ |
3562 | reloading = manager_reloading_start(m); | |
a16e1123 | 3563 | |
b3680f49 | 3564 | r = manager_serialize(m, f, fds, false); |
d147e2b6 | 3565 | if (r < 0) |
d68c645b | 3566 | return r; |
a16e1123 | 3567 | |
5d1e57b8 LP |
3568 | r = finish_serialization_file(f); |
3569 | if (r < 0) | |
3570 | return log_error_errno(r, "Failed to finish serialization: %m"); | |
a16e1123 | 3571 | |
6a33af40 | 3572 | /* 💀 This is the point of no return, from here on there is no way back. 💀 */ |
d147e2b6 | 3573 | reloading = NULL; |
6a33af40 LP |
3574 | |
3575 | bus_manager_send_reloading(m, true); | |
3576 | ||
3577 | /* Start by flushing out all jobs and units, all generated units, all runtime environments, all dynamic users | |
3578 | * and everything else that is worth flushing out. We'll get it all back from the serialization — if we need | |
7802194a | 3579 | * it. */ |
6a33af40 | 3580 | |
a16e1123 | 3581 | manager_clear_jobs_and_units(m); |
07a78643 | 3582 | lookup_paths_flush_generator(&m->lookup_paths); |
e76506b7 | 3583 | exec_shared_runtime_vacuum(m); |
29206d46 | 3584 | dynamic_user_vacuum(m, false); |
00d9ef85 LP |
3585 | m->uid_refs = hashmap_free(m->uid_refs); |
3586 | m->gid_refs = hashmap_free(m->gid_refs); | |
2ded0c04 | 3587 | |
6a33af40 LP |
3588 | (void) manager_run_environment_generators(m); |
3589 | (void) manager_run_generators(m); | |
64691d20 | 3590 | |
91e0ee5f | 3591 | /* We flushed out generated files, for which we don't watch mtime, so we should flush the old map. */ |
e8630e69 | 3592 | manager_free_unit_name_maps(m); |
a82b8b3d | 3593 | m->unit_file_state_outdated = false; |
5a1e9937 | 3594 | |
6a33af40 | 3595 | /* First, enumerate what we can from kernel and suchlike */ |
3ad2afb6 | 3596 | manager_enumerate_perpetual(m); |
ba64af90 | 3597 | manager_enumerate(m); |
a16e1123 LP |
3598 | |
3599 | /* Second, deserialize our stored data */ | |
6a33af40 LP |
3600 | r = manager_deserialize(m, f, fds); |
3601 | if (r < 0) | |
3602 | log_warning_errno(r, "Deserialization failed, proceeding anyway: %m"); | |
a16e1123 | 3603 | |
6a33af40 | 3604 | /* We don't need the serialization anymore */ |
62b0cbb3 | 3605 | f = safe_fclose(f); |
a16e1123 | 3606 | |
6a33af40 LP |
3607 | /* Re-register notify_fd as event source, and set up other sockets/communication channels we might need */ |
3608 | (void) manager_setup_notify(m); | |
6a33af40 | 3609 | (void) manager_setup_user_lookup_fd(m); |
817062e6 | 3610 | (void) manager_setup_handoff_timestamp_fd(m); |
406f1775 | 3611 | (void) manager_setup_pidref_transport_fd(m); |
00d9ef85 | 3612 | |
34f4b817 MY |
3613 | /* Clean up deserialized bus track information. They're never consumed during reload (as opposed to |
3614 | * reexec) since we do not disconnect from the bus. */ | |
3615 | m->subscribed_as_strv = strv_free(m->subscribed_as_strv); | |
3616 | m->deserialized_bus_id = SD_ID128_NULL; | |
3617 | ||
a16e1123 | 3618 | /* Third, fire things up! */ |
007c6337 | 3619 | manager_coldplug(m); |
a16e1123 | 3620 | |
5197be06 LP |
3621 | /* Clean up runtime objects no longer referenced */ |
3622 | manager_vacuum(m); | |
e8a565cb | 3623 | |
6a33af40 | 3624 | /* Consider the reload process complete now. */ |
31dc1ca3 LP |
3625 | assert(m->n_reloading > 0); |
3626 | m->n_reloading--; | |
3627 | ||
5ce5e1ad | 3628 | manager_ready(m); |
8936a5e3 | 3629 | |
71445ae7 | 3630 | m->send_reloading_done = true; |
6a33af40 | 3631 | return 0; |
a16e1123 LP |
3632 | } |
3633 | ||
fdf20a31 | 3634 | void manager_reset_failed(Manager *m) { |
5632e374 | 3635 | Unit *u; |
5632e374 LP |
3636 | |
3637 | assert(m); | |
3638 | ||
90e74a66 | 3639 | HASHMAP_FOREACH(u, m->units) |
fdf20a31 | 3640 | unit_reset_failed(u); |
5632e374 LP |
3641 | } |
3642 | ||
31afa0a4 | 3643 | bool manager_unit_inactive_or_pending(Manager *m, const char *name) { |
8f6df3fa LP |
3644 | Unit *u; |
3645 | ||
3646 | assert(m); | |
3647 | assert(name); | |
3648 | ||
3649 | /* Returns true if the unit is inactive or going down */ | |
bd0af849 ZJS |
3650 | u = manager_get_unit(m, name); |
3651 | if (!u) | |
8f6df3fa LP |
3652 | return true; |
3653 | ||
31afa0a4 | 3654 | return unit_inactive_or_pending(u); |
8f6df3fa LP |
3655 | } |
3656 | ||
d8eb10d6 | 3657 | static void log_taint_string(Manager *m) { |
d8eb10d6 ZJS |
3658 | assert(m); |
3659 | ||
3660 | if (MANAGER_IS_USER(m) || m->taint_logged) | |
3661 | return; | |
3662 | ||
3663 | m->taint_logged = true; /* only check for taint once */ | |
3664 | ||
d851637c | 3665 | _cleanup_free_ char *taint = taint_string(); |
d8eb10d6 ZJS |
3666 | if (isempty(taint)) |
3667 | return; | |
3668 | ||
3669 | log_struct(LOG_NOTICE, | |
3670 | LOG_MESSAGE("System is tainted: %s", taint), | |
3cf6a3a3 YW |
3671 | LOG_ITEM("TAINT=%s", taint), |
3672 | LOG_MESSAGE_ID(SD_MESSAGE_TAINTED_STR)); | |
d8eb10d6 ZJS |
3673 | } |
3674 | ||
56dacdbc | 3675 | static void manager_notify_finished(Manager *m) { |
915b3753 | 3676 | usec_t firmware_usec, loader_usec, kernel_usec, initrd_usec, userspace_usec, total_usec; |
b0c918b9 | 3677 | |
638cece4 | 3678 | if (MANAGER_IS_TEST_RUN(m)) |
b0c918b9 LP |
3679 | return; |
3680 | ||
b3f54861 | 3681 | if (MANAGER_IS_SYSTEM(m) && m->soft_reboots_count > 0) { |
54f86b86 LB |
3682 | /* The soft-reboot case, where we only report data for the last reboot */ |
3683 | firmware_usec = loader_usec = initrd_usec = kernel_usec = 0; | |
a48ad66f MY |
3684 | total_usec = userspace_usec = usec_sub_unsigned(m->timestamps[MANAGER_TIMESTAMP_FINISH].monotonic, |
3685 | m->timestamps[MANAGER_TIMESTAMP_SHUTDOWN_START].monotonic); | |
54f86b86 LB |
3686 | |
3687 | log_struct(LOG_INFO, | |
3cf6a3a3 YW |
3688 | LOG_MESSAGE_ID(SD_MESSAGE_STARTUP_FINISHED_STR), |
3689 | LOG_ITEM("USERSPACE_USEC="USEC_FMT, userspace_usec), | |
a48ad66f MY |
3690 | LOG_MESSAGE("Soft-reboot finished in %s, counter is now at %u.", |
3691 | FORMAT_TIMESPAN(total_usec, USEC_PER_MSEC), | |
3692 | m->soft_reboots_count)); | |
54f86b86 | 3693 | } else if (MANAGER_IS_SYSTEM(m) && detect_container() <= 0) { |
dc3c9f5e ZJS |
3694 | char buf[FORMAT_TIMESPAN_MAX + STRLEN(" (firmware) + ") + FORMAT_TIMESPAN_MAX + STRLEN(" (loader) + ")] |
3695 | = {}; | |
3696 | char *p = buf; | |
3697 | size_t size = sizeof buf; | |
e03ae661 | 3698 | |
9f9f0342 LP |
3699 | /* Note that MANAGER_TIMESTAMP_KERNEL's monotonic value is always at 0, and |
3700 | * MANAGER_TIMESTAMP_FIRMWARE's and MANAGER_TIMESTAMP_LOADER's monotonic value should be considered | |
915b3753 LP |
3701 | * negative values. */ |
3702 | ||
9f9f0342 LP |
3703 | firmware_usec = m->timestamps[MANAGER_TIMESTAMP_FIRMWARE].monotonic - m->timestamps[MANAGER_TIMESTAMP_LOADER].monotonic; |
3704 | loader_usec = m->timestamps[MANAGER_TIMESTAMP_LOADER].monotonic - m->timestamps[MANAGER_TIMESTAMP_KERNEL].monotonic; | |
3705 | userspace_usec = m->timestamps[MANAGER_TIMESTAMP_FINISH].monotonic - m->timestamps[MANAGER_TIMESTAMP_USERSPACE].monotonic; | |
3706 | total_usec = m->timestamps[MANAGER_TIMESTAMP_FIRMWARE].monotonic + m->timestamps[MANAGER_TIMESTAMP_FINISH].monotonic; | |
18fa6b27 | 3707 | |
dd1db3c2 | 3708 | if (firmware_usec > 0) |
5291f26d | 3709 | size = strpcpyf(&p, size, "%s (firmware) + ", FORMAT_TIMESPAN(firmware_usec, USEC_PER_MSEC)); |
dd1db3c2 | 3710 | if (loader_usec > 0) |
5291f26d | 3711 | size = strpcpyf(&p, size, "%s (loader) + ", FORMAT_TIMESPAN(loader_usec, USEC_PER_MSEC)); |
dd1db3c2 | 3712 | |
9f9f0342 | 3713 | if (dual_timestamp_is_set(&m->timestamps[MANAGER_TIMESTAMP_INITRD])) { |
18fa6b27 | 3714 | |
7802194a | 3715 | /* The initrd case on bare-metal */ |
9f9f0342 LP |
3716 | kernel_usec = m->timestamps[MANAGER_TIMESTAMP_INITRD].monotonic - m->timestamps[MANAGER_TIMESTAMP_KERNEL].monotonic; |
3717 | initrd_usec = m->timestamps[MANAGER_TIMESTAMP_USERSPACE].monotonic - m->timestamps[MANAGER_TIMESTAMP_INITRD].monotonic; | |
18fa6b27 | 3718 | |
e12919e8 | 3719 | log_struct(LOG_INFO, |
3cf6a3a3 YW |
3720 | LOG_MESSAGE_ID(SD_MESSAGE_STARTUP_FINISHED_STR), |
3721 | LOG_ITEM("KERNEL_USEC="USEC_FMT, kernel_usec), | |
3722 | LOG_ITEM("INITRD_USEC="USEC_FMT, initrd_usec), | |
3723 | LOG_ITEM("USERSPACE_USEC="USEC_FMT, userspace_usec), | |
dd1db3c2 YW |
3724 | LOG_MESSAGE("Startup finished in %s%s (kernel) + %s (initrd) + %s (userspace) = %s.", |
3725 | buf, | |
5291f26d ZJS |
3726 | FORMAT_TIMESPAN(kernel_usec, USEC_PER_MSEC), |
3727 | FORMAT_TIMESPAN(initrd_usec, USEC_PER_MSEC), | |
3728 | FORMAT_TIMESPAN(userspace_usec, USEC_PER_MSEC), | |
3729 | FORMAT_TIMESPAN(total_usec, USEC_PER_MSEC))); | |
18fa6b27 | 3730 | } else { |
7802194a | 3731 | /* The initrd-less case on bare-metal */ |
9f9f0342 LP |
3732 | |
3733 | kernel_usec = m->timestamps[MANAGER_TIMESTAMP_USERSPACE].monotonic - m->timestamps[MANAGER_TIMESTAMP_KERNEL].monotonic; | |
18fa6b27 LP |
3734 | initrd_usec = 0; |
3735 | ||
81270860 | 3736 | log_struct(LOG_INFO, |
3cf6a3a3 YW |
3737 | LOG_MESSAGE_ID(SD_MESSAGE_STARTUP_FINISHED_STR), |
3738 | LOG_ITEM("KERNEL_USEC="USEC_FMT, kernel_usec), | |
3739 | LOG_ITEM("USERSPACE_USEC="USEC_FMT, userspace_usec), | |
dd1db3c2 YW |
3740 | LOG_MESSAGE("Startup finished in %s%s (kernel) + %s (userspace) = %s.", |
3741 | buf, | |
5291f26d ZJS |
3742 | FORMAT_TIMESPAN(kernel_usec, USEC_PER_MSEC), |
3743 | FORMAT_TIMESPAN(userspace_usec, USEC_PER_MSEC), | |
3744 | FORMAT_TIMESPAN(total_usec, USEC_PER_MSEC))); | |
e12919e8 LP |
3745 | } |
3746 | } else { | |
4adf314b | 3747 | /* The container and --user case */ |
e12919e8 | 3748 | firmware_usec = loader_usec = initrd_usec = kernel_usec = 0; |
9f9f0342 | 3749 | total_usec = userspace_usec = m->timestamps[MANAGER_TIMESTAMP_FINISH].monotonic - m->timestamps[MANAGER_TIMESTAMP_USERSPACE].monotonic; |
e12919e8 LP |
3750 | |
3751 | log_struct(LOG_INFO, | |
3cf6a3a3 YW |
3752 | LOG_MESSAGE_ID(SD_MESSAGE_USER_STARTUP_FINISHED_STR), |
3753 | LOG_ITEM("USERSPACE_USEC="USEC_FMT, userspace_usec), | |
e2cc6eca | 3754 | LOG_MESSAGE("Startup finished in %s.", |
5291f26d | 3755 | FORMAT_TIMESPAN(total_usec, USEC_PER_MSEC))); |
18fa6b27 | 3756 | } |
b0c918b9 | 3757 | |
718db961 | 3758 | bus_manager_send_finished(m, firmware_usec, loader_usec, kernel_usec, initrd_usec, userspace_usec, total_usec); |
530345e7 | 3759 | |
2c6397d1 ZJS |
3760 | if (MANAGER_IS_SYSTEM(m) && detect_container() <= 0) |
3761 | watchdog_report_if_missing(); | |
3762 | ||
d8eb10d6 | 3763 | log_taint_string(m); |
b0c918b9 LP |
3764 | } |
3765 | ||
da81a108 | 3766 | static void manager_send_ready_on_basic_target(Manager *m) { |
4bf4f50f ZJS |
3767 | int r; |
3768 | ||
4adf314b LP |
3769 | assert(m); |
3770 | ||
3771 | /* We send READY=1 on reaching basic.target only when running in --user mode. */ | |
3772 | if (!MANAGER_IS_USER(m) || m->ready_sent) | |
3773 | return; | |
3774 | ||
fb44dc64 | 3775 | r = sd_notify(/* unset_environment= */ false, |
d4341b76 ZJS |
3776 | "READY=1\n" |
3777 | "STATUS=Reached " SPECIAL_BASIC_TARGET "."); | |
4bf4f50f ZJS |
3778 | if (r < 0) |
3779 | log_warning_errno(r, "Failed to send readiness notification, ignoring: %m"); | |
3780 | ||
6d932659 ZJS |
3781 | m->ready_sent = true; |
3782 | m->status_ready = false; | |
3783 | } | |
3784 | ||
da81a108 | 3785 | static void manager_send_ready_on_idle(Manager *m) { |
4bf4f50f ZJS |
3786 | int r; |
3787 | ||
37d15cd1 LP |
3788 | assert(m); |
3789 | ||
37d15cd1 | 3790 | /* Skip the notification if nothing changed. */ |
6d932659 | 3791 | if (m->ready_sent && m->status_ready) |
6d932659 ZJS |
3792 | return; |
3793 | ||
da81a108 MY |
3794 | /* Note that for user managers, we might have already sent READY=1 in manager_send_ready_user_scope(). |
3795 | * But we still need to flush STATUS=. The second READY=1 will be treated as a noop so it doesn't | |
3796 | * hurt to send it twice. */ | |
fb44dc64 | 3797 | r = sd_notify(/* unset_environment= */ false, |
028f7d3a ZJS |
3798 | "READY=1\n" |
3799 | "STATUS=Ready."); | |
4bf4f50f ZJS |
3800 | if (r < 0) |
3801 | log_full_errno(m->ready_sent ? LOG_DEBUG : LOG_WARNING, r, | |
3802 | "Failed to send readiness notification, ignoring: %m"); | |
3803 | ||
6d932659 | 3804 | m->ready_sent = m->status_ready = true; |
4adf314b LP |
3805 | } |
3806 | ||
3807 | static void manager_check_basic_target(Manager *m) { | |
3808 | Unit *u; | |
3809 | ||
3810 | assert(m); | |
3811 | ||
3812 | /* Small shortcut */ | |
3813 | if (m->ready_sent && m->taint_logged) | |
3814 | return; | |
3815 | ||
3816 | u = manager_get_unit(m, SPECIAL_BASIC_TARGET); | |
3817 | if (!u || !UNIT_IS_ACTIVE_OR_RELOADING(unit_active_state(u))) | |
3818 | return; | |
3819 | ||
3820 | /* For user managers, send out READY=1 as soon as we reach basic.target */ | |
da81a108 | 3821 | manager_send_ready_on_basic_target(m); |
4adf314b LP |
3822 | |
3823 | /* Log the taint string as soon as we reach basic.target */ | |
3824 | log_taint_string(m); | |
3825 | } | |
3826 | ||
56dacdbc | 3827 | void manager_check_finished(Manager *m) { |
56dacdbc ZJS |
3828 | assert(m); |
3829 | ||
2c289ea8 | 3830 | if (MANAGER_IS_RELOADING(m)) |
aad1976f LP |
3831 | return; |
3832 | ||
4259d202 LP |
3833 | /* Verify that we have entered the event loop already, and not left it again. */ |
3834 | if (!MANAGER_IS_RUNNING(m)) | |
9771b62d LP |
3835 | return; |
3836 | ||
4adf314b | 3837 | manager_check_basic_target(m); |
0c2826c6 | 3838 | |
43127aeb | 3839 | if (!hashmap_isempty(m->jobs)) { |
56dacdbc | 3840 | if (m->jobs_in_progress_event_source) |
2ae56591 | 3841 | /* Ignore any failure, this is only for feedback */ |
1b4154a8 ZJS |
3842 | (void) sd_event_source_set_time(m->jobs_in_progress_event_source, |
3843 | manager_watch_jobs_next_time(m)); | |
56dacdbc ZJS |
3844 | return; |
3845 | } | |
3846 | ||
a4ac27c1 ZJS |
3847 | /* The jobs hashmap tends to grow a lot during boot, and then it's not reused until shutdown. Let's |
3848 | kill the hashmap if it is relatively large. */ | |
3849 | if (hashmap_buckets(m->jobs) > hashmap_size(m->units) / 10) | |
3850 | m->jobs = hashmap_free(m->jobs); | |
3851 | ||
da81a108 | 3852 | manager_send_ready_on_idle(m); |
6d932659 | 3853 | |
9c1b17c3 YW |
3854 | /* Notify Type=idle units that we are done now */ |
3855 | manager_close_idle_pipe(m); | |
3856 | ||
6d932659 ZJS |
3857 | if (MANAGER_IS_FINISHED(m)) |
3858 | return; | |
3859 | ||
7365a296 | 3860 | manager_flip_auto_status(m, false, "boot finished"); |
56dacdbc | 3861 | |
56dacdbc | 3862 | /* Turn off confirm spawn now */ |
7d5ceb64 | 3863 | m->confirm_spawn = NULL; |
56dacdbc ZJS |
3864 | |
3865 | /* No need to update ask password status when we're going non-interactive */ | |
3866 | manager_close_ask_password(m); | |
3867 | ||
3868 | /* This is no longer the first boot */ | |
3869 | manager_set_first_boot(m, false); | |
3870 | ||
fa5a0251 | 3871 | dual_timestamp_now(m->timestamps + MANAGER_TIMESTAMP_FINISH); |
56dacdbc ZJS |
3872 | |
3873 | manager_notify_finished(m); | |
3874 | ||
e7ab4d1a | 3875 | manager_invalidate_startup_units(m); |
56dacdbc ZJS |
3876 | } |
3877 | ||
dd0ab174 LP |
3878 | void manager_send_reloading(Manager *m) { |
3879 | assert(m); | |
3880 | ||
3881 | /* Let whoever invoked us know that we are now reloading */ | |
b377a7cc | 3882 | (void) notify_reloading_full(/* status = */ NULL); |
dd0ab174 LP |
3883 | |
3884 | /* And ensure that we'll send READY=1 again as soon as we are ready again */ | |
3885 | m->ready_sent = false; | |
3886 | } | |
3887 | ||
de41622b MY |
3888 | static bool generator_path_any(char * const *paths) { |
3889 | ||
3890 | /* Optimize by skipping the whole process by not creating output directories if no generators are found. */ | |
3891 | ||
3892 | STRV_FOREACH(i, paths) { | |
3893 | if (access(*i, F_OK) >= 0) | |
3894 | return true; | |
3895 | if (errno != ENOENT) | |
3896 | log_warning_errno(errno, "Failed to check if generator dir '%s' exists, assuming not: %m", *i); | |
3897 | } | |
3898 | ||
3899 | return false; | |
64691d20 ZJS |
3900 | } |
3901 | ||
64691d20 | 3902 | static int manager_run_environment_generators(Manager *m) { |
cccf5703 | 3903 | _cleanup_strv_free_ char **paths = NULL; |
e3b8d063 | 3904 | int r; |
64691d20 | 3905 | |
de41622b MY |
3906 | assert(m); |
3907 | ||
638cece4 | 3908 | if (MANAGER_IS_TEST_RUN(m) && !(m->test_run_flags & MANAGER_TEST_RUN_ENV_GENERATORS)) |
e0a3da1f ZJS |
3909 | return 0; |
3910 | ||
361cacf4 | 3911 | paths = env_generator_binary_paths(m->runtime_scope); |
cccf5703 BB |
3912 | if (!paths) |
3913 | return log_oom(); | |
64691d20 | 3914 | |
de41622b | 3915 | if (!generator_path_any(paths)) |
64691d20 ZJS |
3916 | return 0; |
3917 | ||
de41622b MY |
3918 | char **tmp = NULL; /* this is only used in the forked process, no cleanup here */ |
3919 | void *args[_STDOUT_CONSUME_MAX] = { | |
3920 | [STDOUT_GENERATE] = &tmp, | |
3921 | [STDOUT_COLLECT] = &tmp, | |
3922 | [STDOUT_CONSUME] = &m->transient_environment, | |
3923 | }; | |
3924 | ||
2053593f | 3925 | WITH_UMASK(0022) |
d65dc4c5 LP |
3926 | r = execute_directories( |
3927 | (const char* const*) paths, | |
3928 | DEFAULT_TIMEOUT_USEC, | |
3929 | gather_environment, | |
3930 | args, | |
3931 | /* argv[]= */ NULL, | |
3932 | m->transient_environment, | |
3933 | EXEC_DIR_PARALLEL | EXEC_DIR_IGNORE_ERRORS | EXEC_DIR_SET_SYSTEMD_EXEC_PID); | |
e3b8d063 | 3934 | return r; |
64691d20 ZJS |
3935 | } |
3936 | ||
82c5db16 LP |
3937 | static int build_generator_environment(Manager *m, char ***ret) { |
3938 | _cleanup_strv_free_ char **nl = NULL; | |
3939 | Virtualization v; | |
08951245 | 3940 | ConfidentialVirtualization cv; |
82c5db16 LP |
3941 | int r; |
3942 | ||
3943 | assert(m); | |
3944 | assert(ret); | |
3945 | ||
3946 | /* Generators oftentimes want to know some basic facts about the environment they run in, in order to | |
3947 | * adjust generated units to that. Let's pass down some bits of information that are easy for us to | |
3948 | * determine (but a bit harder for generator scripts to determine), as environment variables. */ | |
3949 | ||
3950 | nl = strv_copy(m->transient_environment); | |
3951 | if (!nl) | |
3952 | return -ENOMEM; | |
3953 | ||
4870133b | 3954 | r = strv_env_assign(&nl, "SYSTEMD_SCOPE", runtime_scope_to_string(m->runtime_scope)); |
82c5db16 LP |
3955 | if (r < 0) |
3956 | return r; | |
3957 | ||
3958 | if (MANAGER_IS_SYSTEM(m)) { | |
3959 | /* Note that $SYSTEMD_IN_INITRD may be used to override the initrd detection in much of our | |
3960 | * codebase. This is hence more than purely informational. It will shortcut detection of the | |
3961 | * initrd state if generators invoke our own tools. But that's OK, as it would come to the | |
3962 | * same results (hopefully). */ | |
3963 | r = strv_env_assign(&nl, "SYSTEMD_IN_INITRD", one_zero(in_initrd())); | |
3964 | if (r < 0) | |
3965 | return r; | |
3966 | ||
86eb3a8f MY |
3967 | if (m->soft_reboots_count > 0) { |
3968 | r = strv_env_assignf(&nl, "SYSTEMD_SOFT_REBOOTS_COUNT", "%u", m->soft_reboots_count); | |
3969 | if (r < 0) | |
3970 | return r; | |
3971 | } | |
3972 | ||
82c5db16 LP |
3973 | if (m->first_boot >= 0) { |
3974 | r = strv_env_assign(&nl, "SYSTEMD_FIRST_BOOT", one_zero(m->first_boot)); | |
3975 | if (r < 0) | |
3976 | return r; | |
3977 | } | |
3978 | } | |
3979 | ||
3980 | v = detect_virtualization(); | |
3981 | if (v < 0) | |
3982 | log_debug_errno(v, "Failed to detect virtualization, ignoring: %m"); | |
3983 | else if (v > 0) { | |
3984 | const char *s; | |
3985 | ||
3986 | s = strjoina(VIRTUALIZATION_IS_VM(v) ? "vm:" : | |
3987 | VIRTUALIZATION_IS_CONTAINER(v) ? "container:" : ":", | |
3988 | virtualization_to_string(v)); | |
3989 | ||
3990 | r = strv_env_assign(&nl, "SYSTEMD_VIRTUALIZATION", s); | |
3991 | if (r < 0) | |
3992 | return r; | |
3993 | } | |
3994 | ||
08951245 DB |
3995 | cv = detect_confidential_virtualization(); |
3996 | if (cv < 0) | |
3997 | log_debug_errno(cv, "Failed to detect confidential virtualization, ignoring: %m"); | |
3998 | else if (cv > 0) { | |
3999 | r = strv_env_assign(&nl, "SYSTEMD_CONFIDENTIAL_VIRTUALIZATION", confidential_virtualization_to_string(cv)); | |
4000 | if (r < 0) | |
4001 | return r; | |
4002 | } | |
4003 | ||
82c5db16 LP |
4004 | r = strv_env_assign(&nl, "SYSTEMD_ARCHITECTURE", architecture_to_string(uname_architecture())); |
4005 | if (r < 0) | |
4006 | return r; | |
4007 | ||
4008 | *ret = TAKE_PTR(nl); | |
4009 | return 0; | |
4010 | } | |
4011 | ||
de41622b | 4012 | static int manager_execute_generators(Manager *m, char * const *paths, bool remount_ro) { |
ca6ce62d | 4013 | _cleanup_strv_free_ char **ge = NULL; |
ca6ce62d ZJS |
4014 | int r; |
4015 | ||
de41622b MY |
4016 | assert(m); |
4017 | ||
ca6ce62d ZJS |
4018 | r = build_generator_environment(m, &ge); |
4019 | if (r < 0) | |
4020 | return log_error_errno(r, "Failed to build generator environment: %m"); | |
4021 | ||
4022 | if (remount_ro) { | |
4023 | /* Remount most of the filesystem tree read-only. We leave /sys/ as-is, because our code | |
4024 | * checks whether it is read-only to detect containerized execution environments. We leave | |
4025 | * /run/ as-is too, because that's where our output goes. We also leave /proc/ and /dev/shm/ | |
4026 | * because they're API, and /tmp/ that safe_fork() mounted for us. | |
4027 | */ | |
4028 | r = bind_remount_recursive("/", MS_RDONLY, MS_RDONLY, | |
4029 | STRV_MAKE("/sys", "/run", "/proc", "/dev/shm", "/tmp")); | |
4030 | if (r < 0) | |
4031 | log_warning_errno(r, "Read-only bind remount failed, ignoring: %m"); | |
4032 | } | |
4033 | ||
de41622b MY |
4034 | const char *argv[] = { |
4035 | NULL, /* Leave this empty, execute_directory() will fill something in */ | |
4036 | m->lookup_paths.generator, | |
4037 | m->lookup_paths.generator_early, | |
4038 | m->lookup_paths.generator_late, | |
4039 | NULL, | |
4040 | }; | |
4041 | ||
ca6ce62d ZJS |
4042 | BLOCK_WITH_UMASK(0022); |
4043 | return execute_directories( | |
4044 | (const char* const*) paths, | |
4045 | DEFAULT_TIMEOUT_USEC, | |
4046 | /* callbacks= */ NULL, /* callback_args= */ NULL, | |
4047 | (char**) argv, | |
4048 | ge, | |
da32cac8 | 4049 | EXEC_DIR_PARALLEL | EXEC_DIR_IGNORE_ERRORS | EXEC_DIR_SET_SYSTEMD_EXEC_PID | EXEC_DIR_WARN_WORLD_WRITABLE); |
ca6ce62d ZJS |
4050 | } |
4051 | ||
e801700e | 4052 | static int manager_run_generators(Manager *m) { |
b8fba0cd | 4053 | ForkFlags flags = FORK_RESET_SIGNALS | FORK_WAIT | FORK_NEW_MOUNTNS | FORK_MOUNTNS_SLAVE; |
ca6ce62d | 4054 | _cleanup_strv_free_ char **paths = NULL; |
07719a21 | 4055 | int r; |
5a1e9937 LP |
4056 | |
4057 | assert(m); | |
4058 | ||
638cece4 | 4059 | if (MANAGER_IS_TEST_RUN(m) && !(m->test_run_flags & MANAGER_TEST_RUN_GENERATORS)) |
e0a3da1f ZJS |
4060 | return 0; |
4061 | ||
4870133b | 4062 | paths = generator_binary_paths(m->runtime_scope); |
e801700e ZJS |
4063 | if (!paths) |
4064 | return log_oom(); | |
5a1e9937 | 4065 | |
de41622b | 4066 | if (!generator_path_any(paths)) |
64691d20 | 4067 | return 0; |
5a1e9937 | 4068 | |
cd64fd56 | 4069 | r = lookup_paths_mkdir_generator(&m->lookup_paths); |
7eb4f326 LP |
4070 | if (r < 0) { |
4071 | log_error_errno(r, "Failed to create generator directories: %m"); | |
07719a21 | 4072 | goto finish; |
7eb4f326 | 4073 | } |
5a1e9937 | 4074 | |
ca6ce62d ZJS |
4075 | /* If we are the system manager, we fork and invoke the generators in a sanitized mount namespace. If |
4076 | * we are the user manager, let's just execute the generators directly. We might not have the | |
4077 | * necessary privileges, and the system manager has already mounted /tmp/ and everything else for us. | |
4078 | */ | |
4079 | if (MANAGER_IS_USER(m)) { | |
4080 | r = manager_execute_generators(m, paths, /* remount_ro= */ false); | |
82c5db16 LP |
4081 | goto finish; |
4082 | } | |
5a1e9937 | 4083 | |
b8fba0cd LB |
4084 | /* On some systems /tmp/ doesn't exist, and on some other systems we cannot create it at all. Avoid |
4085 | * trying to mount a private tmpfs on it as there's no one size fits all. */ | |
452f91d8 | 4086 | if (is_dir("/tmp", /* follow= */ false) > 0 && !MANAGER_IS_TEST_RUN(m)) |
b8fba0cd LB |
4087 | flags |= FORK_PRIVATE_TMP; |
4088 | ||
4089 | r = safe_fork("(sd-gens)", flags, NULL); | |
ca6ce62d ZJS |
4090 | if (r == 0) { |
4091 | r = manager_execute_generators(m, paths, /* remount_ro= */ true); | |
4092 | _exit(r >= 0 ? EXIT_SUCCESS : EXIT_FAILURE); | |
4093 | } | |
a2275dcb | 4094 | if (r < 0) { |
46801870 | 4095 | if (!ERRNO_IS_PRIVILEGE(r) && r != -EINVAL) { |
a2275dcb YW |
4096 | log_error_errno(r, "Failed to fork off sandboxing environment for executing generators: %m"); |
4097 | goto finish; | |
4098 | } | |
4099 | ||
4100 | /* Failed to fork with new mount namespace? Maybe, running in a container environment with | |
46801870 YW |
4101 | * seccomp or without capability. |
4102 | * | |
4103 | * We also allow -EINVAL to allow running without CLONE_NEWNS. | |
4104 | * | |
4105 | * Also, when running on non-native userland architecture via systemd-nspawn and | |
4106 | * qemu-user-static QEMU-emulator, clone() with CLONE_NEWNS fails with EINVAL, see | |
4107 | * https://github.com/systemd/systemd/issues/28901. | |
4108 | */ | |
a2275dcb YW |
4109 | log_debug_errno(r, |
4110 | "Failed to fork off sandboxing environment for executing generators. " | |
4111 | "Falling back to execute generators without sandboxing: %m"); | |
4112 | r = manager_execute_generators(m, paths, /* remount_ro= */ false); | |
4113 | } | |
5a1e9937 | 4114 | |
718db961 | 4115 | finish: |
cd64fd56 | 4116 | lookup_paths_trim_generator(&m->lookup_paths); |
e801700e | 4117 | return r; |
5a1e9937 LP |
4118 | } |
4119 | ||
1ad6e8b3 LP |
4120 | int manager_transient_environment_add(Manager *m, char **plus) { |
4121 | char **a; | |
4122 | ||
4123 | assert(m); | |
4124 | ||
4125 | if (strv_isempty(plus)) | |
4126 | return 0; | |
4127 | ||
4ab3d29f | 4128 | a = strv_env_merge(m->transient_environment, plus); |
1ad6e8b3 | 4129 | if (!a) |
2fbbbf9a | 4130 | return log_oom(); |
1ad6e8b3 LP |
4131 | |
4132 | sanitize_environment(a); | |
4133 | ||
4134 | return strv_free_and_replace(m->transient_environment, a); | |
4135 | } | |
4136 | ||
4137 | int manager_client_environment_modify( | |
4138 | Manager *m, | |
4139 | char **minus, | |
4140 | char **plus) { | |
4141 | ||
718db961 | 4142 | char **a = NULL, **b = NULL, **l; |
1ad6e8b3 | 4143 | |
97d0e5f8 | 4144 | assert(m); |
bcd8e6d1 | 4145 | |
1ad6e8b3 LP |
4146 | if (strv_isempty(minus) && strv_isempty(plus)) |
4147 | return 0; | |
4148 | ||
4149 | l = m->client_environment; | |
bcd8e6d1 | 4150 | |
718db961 LP |
4151 | if (!strv_isempty(minus)) { |
4152 | a = strv_env_delete(l, 1, minus); | |
4153 | if (!a) | |
4154 | return -ENOMEM; | |
4155 | ||
4156 | l = a; | |
4157 | } | |
4158 | ||
4159 | if (!strv_isempty(plus)) { | |
4ab3d29f | 4160 | b = strv_env_merge(l, plus); |
aa9f8a30 AH |
4161 | if (!b) { |
4162 | strv_free(a); | |
718db961 | 4163 | return -ENOMEM; |
aa9f8a30 | 4164 | } |
bcd8e6d1 | 4165 | |
718db961 LP |
4166 | l = b; |
4167 | } | |
4168 | ||
1ad6e8b3 LP |
4169 | if (m->client_environment != l) |
4170 | strv_free(m->client_environment); | |
4171 | ||
718db961 LP |
4172 | if (a != l) |
4173 | strv_free(a); | |
4174 | if (b != l) | |
4175 | strv_free(b); | |
4176 | ||
1ad6e8b3 LP |
4177 | m->client_environment = sanitize_environment(l); |
4178 | return 0; | |
4179 | } | |
4180 | ||
4181 | int manager_get_effective_environment(Manager *m, char ***ret) { | |
4182 | char **l; | |
4183 | ||
4184 | assert(m); | |
4185 | assert(ret); | |
4186 | ||
4ab3d29f | 4187 | l = strv_env_merge(m->transient_environment, m->client_environment); |
1ad6e8b3 LP |
4188 | if (!l) |
4189 | return -ENOMEM; | |
f069efb4 | 4190 | |
1ad6e8b3 | 4191 | *ret = l; |
97d0e5f8 UTL |
4192 | return 0; |
4193 | } | |
4194 | ||
bfb27b06 LP |
4195 | int manager_set_unit_defaults(Manager *m, const UnitDefaults *defaults) { |
4196 | _cleanup_free_ char *label = NULL; | |
4197 | struct rlimit *rlimit[_RLIMIT_MAX]; | |
4198 | int r; | |
4199 | ||
aa5ae971 | 4200 | assert(m); |
bfb27b06 | 4201 | assert(defaults); |
aa5ae971 | 4202 | |
bfb27b06 LP |
4203 | if (streq_ptr(defaults->smack_process_label, "/")) |
4204 | label = NULL; | |
4205 | else { | |
4206 | const char *l = defaults->smack_process_label; | |
aa5ae971 | 4207 | #ifdef SMACK_DEFAULT_PROCESS_LABEL |
bfb27b06 LP |
4208 | if (!l) |
4209 | l = SMACK_DEFAULT_PROCESS_LABEL; | |
aa5ae971 | 4210 | #endif |
bfb27b06 LP |
4211 | if (l) { |
4212 | label = strdup(l); | |
4213 | if (!label) | |
4214 | return -ENOMEM; | |
4215 | } else | |
4216 | label = NULL; | |
4217 | } | |
aa5ae971 | 4218 | |
bfb27b06 LP |
4219 | r = rlimit_copy_all(rlimit, defaults->rlimit); |
4220 | if (r < 0) | |
4221 | return r; | |
aa5ae971 | 4222 | |
bfb27b06 LP |
4223 | m->defaults.std_output = defaults->std_output; |
4224 | m->defaults.std_error = defaults->std_error; | |
c93ff2e9 | 4225 | |
bfb27b06 LP |
4226 | m->defaults.restart_usec = defaults->restart_usec; |
4227 | m->defaults.timeout_start_usec = defaults->timeout_start_usec; | |
4228 | m->defaults.timeout_stop_usec = defaults->timeout_stop_usec; | |
4229 | m->defaults.timeout_abort_usec = defaults->timeout_abort_usec; | |
4230 | m->defaults.timeout_abort_set = defaults->timeout_abort_set; | |
4231 | m->defaults.device_timeout_usec = defaults->device_timeout_usec; | |
d9814c76 | 4232 | |
14702b9c | 4233 | m->defaults.start_limit = defaults->start_limit; |
c93ff2e9 | 4234 | |
bfb27b06 LP |
4235 | m->defaults.memory_accounting = defaults->memory_accounting; |
4236 | m->defaults.io_accounting = defaults->io_accounting; | |
bfb27b06 LP |
4237 | m->defaults.tasks_accounting = defaults->tasks_accounting; |
4238 | m->defaults.ip_accounting = defaults->ip_accounting; | |
4239 | ||
4240 | m->defaults.tasks_max = defaults->tasks_max; | |
4241 | m->defaults.timer_accuracy_usec = defaults->timer_accuracy_usec; | |
4242 | ||
4243 | m->defaults.oom_policy = defaults->oom_policy; | |
4244 | m->defaults.oom_score_adjust = defaults->oom_score_adjust; | |
4245 | m->defaults.oom_score_adjust_set = defaults->oom_score_adjust_set; | |
4246 | ||
4247 | m->defaults.memory_pressure_watch = defaults->memory_pressure_watch; | |
4248 | m->defaults.memory_pressure_threshold_usec = defaults->memory_pressure_threshold_usec; | |
4249 | ||
4250 | free_and_replace(m->defaults.smack_process_label, label); | |
4251 | rlimit_free_all(m->defaults.rlimit); | |
4252 | memcpy(m->defaults.rlimit, rlimit, sizeof(struct rlimit*) * _RLIMIT_MAX); | |
c93ff2e9 FC |
4253 | |
4254 | return 0; | |
4255 | } | |
4256 | ||
8559b3b7 LP |
4257 | void manager_recheck_dbus(Manager *m) { |
4258 | assert(m); | |
4259 | ||
8f41e6b6 ZJS |
4260 | /* Connects to the bus if the dbus service and socket are running. If we are running in user mode |
4261 | * this is all it does. In system mode we'll also connect to the system bus (which will most likely | |
4262 | * just reuse the connection of the API bus). That's because the system bus after all runs as service | |
4263 | * of the system instance, while in the user instance we can assume it's already there. */ | |
8559b3b7 | 4264 | |
31dc1ca3 LP |
4265 | if (MANAGER_IS_RELOADING(m)) |
4266 | return; /* don't check while we are reloading… */ | |
4267 | ||
2b680534 | 4268 | if (manager_dbus_is_running(m, false)) { |
8559b3b7 LP |
4269 | (void) bus_init_api(m); |
4270 | ||
4271 | if (MANAGER_IS_SYSTEM(m)) | |
4272 | (void) bus_init_system(m); | |
4273 | } else { | |
4274 | (void) bus_done_api(m); | |
4275 | ||
4276 | if (MANAGER_IS_SYSTEM(m)) | |
4277 | (void) bus_done_system(m); | |
4278 | } | |
4279 | } | |
4280 | ||
d075092f | 4281 | static bool manager_journal_is_running(Manager *m) { |
f1dd0c3f LP |
4282 | Unit *u; |
4283 | ||
4284 | assert(m); | |
4285 | ||
638cece4 | 4286 | if (MANAGER_IS_TEST_RUN(m)) |
7d814a19 LP |
4287 | return false; |
4288 | ||
d075092f | 4289 | /* If we are the user manager we can safely assume that the journal is up */ |
463d0d15 | 4290 | if (!MANAGER_IS_SYSTEM(m)) |
d075092f | 4291 | return true; |
f1dd0c3f | 4292 | |
d075092f | 4293 | /* Check that the socket is not only up, but in RUNNING state */ |
731a676c | 4294 | u = manager_get_unit(m, SPECIAL_JOURNALD_SOCKET); |
d075092f LP |
4295 | if (!u) |
4296 | return false; | |
4297 | if (SOCKET(u)->state != SOCKET_RUNNING) | |
4298 | return false; | |
f1dd0c3f | 4299 | |
d075092f | 4300 | /* Similar, check if the daemon itself is fully up, too */ |
731a676c | 4301 | u = manager_get_unit(m, SPECIAL_JOURNALD_SERVICE); |
d075092f LP |
4302 | if (!u) |
4303 | return false; | |
217677ab | 4304 | if (!IN_SET(SERVICE(u)->state, SERVICE_RELOAD, SERVICE_RUNNING)) |
d075092f LP |
4305 | return false; |
4306 | ||
4307 | return true; | |
4308 | } | |
4309 | ||
6123dfaa ZJS |
4310 | void disable_printk_ratelimit(void) { |
4311 | /* Disable kernel's printk ratelimit. | |
4312 | * | |
4313 | * Logging to /dev/kmsg is most useful during early boot and shutdown, where normal logging | |
4314 | * mechanisms are not available. The semantics of this sysctl are such that any kernel command-line | |
4315 | * setting takes precedence. */ | |
4316 | int r; | |
4317 | ||
4318 | r = sysctl_write("kernel/printk_devkmsg", "on"); | |
4319 | if (r < 0) | |
4320 | log_debug_errno(r, "Failed to set sysctl kernel.printk_devkmsg=on: %m"); | |
4321 | } | |
4322 | ||
d075092f LP |
4323 | void manager_recheck_journal(Manager *m) { |
4324 | ||
4325 | assert(m); | |
4326 | ||
4327 | /* Don't bother with this unless we are in the special situation of being PID 1 */ | |
4328 | if (getpid_cached() != 1) | |
731a676c | 4329 | return; |
f1dd0c3f | 4330 | |
31dc1ca3 LP |
4331 | /* Don't check this while we are reloading, things might still change */ |
4332 | if (MANAGER_IS_RELOADING(m)) | |
4333 | return; | |
4334 | ||
8f41e6b6 ZJS |
4335 | /* The journal is fully and entirely up? If so, let's permit logging to it, if that's configured. If |
4336 | * the journal is down, don't ever log to it, otherwise we might end up deadlocking ourselves as we | |
4337 | * might trigger an activation ourselves we can't fulfill. */ | |
cedf5088 | 4338 | log_set_prohibit_ipc(!manager_journal_is_running(m)); |
cc2b9e6b | 4339 | log_open(); |
f1dd0c3f LP |
4340 | } |
4341 | ||
44a41954 FB |
4342 | static ShowStatus manager_get_show_status(Manager *m) { |
4343 | assert(m); | |
4344 | ||
4345 | if (MANAGER_IS_USER(m)) | |
4346 | return _SHOW_STATUS_INVALID; | |
4347 | ||
4348 | if (m->show_status_overridden != _SHOW_STATUS_INVALID) | |
4349 | return m->show_status_overridden; | |
4350 | ||
4351 | return m->show_status; | |
4352 | } | |
4353 | ||
4354 | bool manager_get_show_status_on(Manager *m) { | |
4355 | assert(m); | |
4356 | ||
4357 | return show_status_on(manager_get_show_status(m)); | |
4358 | } | |
b309078a | 4359 | |
3ceb3471 FB |
4360 | static void set_show_status_marker(bool b) { |
4361 | if (b) | |
4362 | (void) touch("/run/systemd/show-status"); | |
4363 | else | |
4364 | (void) unlink("/run/systemd/show-status"); | |
4365 | } | |
4366 | ||
44a41954 | 4367 | void manager_set_show_status(Manager *m, ShowStatus mode, const char *reason) { |
27d340c7 | 4368 | assert(m); |
44a41954 | 4369 | assert(reason); |
0d066dd1 | 4370 | assert(mode >= 0 && mode < _SHOW_STATUS_MAX); |
27d340c7 | 4371 | |
44a41954 | 4372 | if (MANAGER_IS_USER(m)) |
27d340c7 LP |
4373 | return; |
4374 | ||
ef15d3e1 ZJS |
4375 | if (mode == m->show_status) |
4376 | return; | |
4377 | ||
44a41954 FB |
4378 | if (m->show_status_overridden == _SHOW_STATUS_INVALID) { |
4379 | bool enabled; | |
4380 | ||
4381 | enabled = show_status_on(mode); | |
4382 | log_debug("%s (%s) showing of status (%s).", | |
4383 | enabled ? "Enabling" : "Disabling", | |
4384 | strna(show_status_to_string(mode)), | |
4385 | reason); | |
4386 | ||
3ceb3471 | 4387 | set_show_status_marker(enabled); |
44a41954 FB |
4388 | } |
4389 | ||
4390 | m->show_status = mode; | |
4391 | } | |
4392 | ||
43bba15a | 4393 | void manager_override_show_status(Manager *m, ShowStatus mode, const char *reason) { |
44a41954 FB |
4394 | assert(m); |
4395 | assert(mode < _SHOW_STATUS_MAX); | |
4396 | ||
4397 | if (MANAGER_IS_USER(m)) | |
4398 | return; | |
4399 | ||
4400 | if (mode == m->show_status_overridden) | |
4401 | return; | |
4402 | ||
4403 | m->show_status_overridden = mode; | |
4404 | ||
4405 | if (mode == _SHOW_STATUS_INVALID) | |
4406 | mode = m->show_status; | |
4407 | ||
ef15d3e1 | 4408 | log_debug("%s (%s) showing of status (%s).", |
44a41954 | 4409 | m->show_status_overridden != _SHOW_STATUS_INVALID ? "Overriding" : "Restoring", |
ef15d3e1 ZJS |
4410 | strna(show_status_to_string(mode)), |
4411 | reason); | |
b309078a | 4412 | |
3ceb3471 | 4413 | set_show_status_marker(show_status_on(mode)); |
27d340c7 LP |
4414 | } |
4415 | ||
37ca2ccf | 4416 | const char* manager_get_confirm_spawn(Manager *m) { |
7d5ceb64 | 4417 | static int last_errno = 0; |
7d5ceb64 FB |
4418 | struct stat st; |
4419 | int r; | |
4420 | ||
ea758432 LP |
4421 | assert(m); |
4422 | ||
7d5ceb64 FB |
4423 | /* Here's the deal: we want to test the validity of the console but don't want |
4424 | * PID1 to go through the whole console process which might block. But we also | |
4425 | * want to warn the user only once if something is wrong with the console so we | |
4426 | * cannot do the sanity checks after spawning our children. So here we simply do | |
4427 | * really basic tests to hopefully trap common errors. | |
4428 | * | |
4429 | * If the console suddenly disappear at the time our children will really it | |
4430 | * then they will simply fail to acquire it and a positive answer will be | |
2aed63f4 | 4431 | * assumed. New children will fall back to /dev/console though. |
7d5ceb64 FB |
4432 | * |
4433 | * Note: TTYs are devices that can come and go any time, and frequently aren't | |
4434 | * available yet during early boot (consider a USB rs232 dongle...). If for any | |
2aed63f4 | 4435 | * reason the configured console is not ready, we fall back to the default |
7d5ceb64 FB |
4436 | * console. */ |
4437 | ||
ea758432 LP |
4438 | if (!m->confirm_spawn || path_equal(m->confirm_spawn, "/dev/console")) |
4439 | return m->confirm_spawn; | |
7d5ceb64 | 4440 | |
ea758432 LP |
4441 | if (stat(m->confirm_spawn, &st) < 0) { |
4442 | r = -errno; | |
7d5ceb64 | 4443 | goto fail; |
ea758432 | 4444 | } |
7d5ceb64 FB |
4445 | |
4446 | if (!S_ISCHR(st.st_mode)) { | |
ea758432 | 4447 | r = -ENOTTY; |
7d5ceb64 FB |
4448 | goto fail; |
4449 | } | |
4450 | ||
4451 | last_errno = 0; | |
ea758432 LP |
4452 | return m->confirm_spawn; |
4453 | ||
7d5ceb64 | 4454 | fail: |
ea758432 LP |
4455 | if (last_errno != r) |
4456 | last_errno = log_warning_errno(r, "Failed to open %s, using default console: %m", m->confirm_spawn); | |
4457 | ||
7d5ceb64 FB |
4458 | return "/dev/console"; |
4459 | } | |
4460 | ||
e2680723 LP |
4461 | void manager_set_first_boot(Manager *m, bool b) { |
4462 | assert(m); | |
4463 | ||
463d0d15 | 4464 | if (!MANAGER_IS_SYSTEM(m)) |
e2680723 LP |
4465 | return; |
4466 | ||
ae2a2c53 LP |
4467 | if (m->first_boot != (int) b) { |
4468 | if (b) | |
4469 | (void) touch("/run/systemd/first-boot"); | |
4470 | else | |
4471 | (void) unlink("/run/systemd/first-boot"); | |
4472 | } | |
e2680723 | 4473 | |
ae2a2c53 | 4474 | m->first_boot = b; |
e2680723 LP |
4475 | } |
4476 | ||
b0eb2944 FB |
4477 | void manager_disable_confirm_spawn(void) { |
4478 | (void) touch("/run/systemd/confirm_spawn_disabled"); | |
4479 | } | |
4480 | ||
0d6d3cf0 FB |
4481 | static bool manager_should_show_status(Manager *m, StatusType type) { |
4482 | assert(m); | |
4483 | ||
4484 | if (!MANAGER_IS_SYSTEM(m)) | |
4485 | return false; | |
4486 | ||
4487 | if (m->no_console_output) | |
4488 | return false; | |
4489 | ||
4490 | if (!IN_SET(manager_state(m), MANAGER_INITIALIZING, MANAGER_STARTING, MANAGER_STOPPING)) | |
4491 | return false; | |
4492 | ||
4493 | /* If we cannot find out the status properly, just proceed. */ | |
4494 | if (type != STATUS_TYPE_EMERGENCY && manager_check_ask_password(m) > 0) | |
4495 | return false; | |
4496 | ||
4497 | if (type == STATUS_TYPE_NOTICE && m->show_status != SHOW_STATUS_NO) | |
4498 | return true; | |
4499 | ||
44a41954 | 4500 | return manager_get_show_status_on(m); |
0d6d3cf0 FB |
4501 | } |
4502 | ||
127d5fd1 | 4503 | void manager_status_printf(Manager *m, StatusType type, const char *status, const char *format, ...) { |
25cee550 MS |
4504 | va_list ap; |
4505 | ||
cb6531be ZJS |
4506 | /* If m is NULL, assume we're after shutdown and let the messages through. */ |
4507 | ||
0d6d3cf0 | 4508 | if (m && !manager_should_show_status(m, type)) |
25cee550 MS |
4509 | return; |
4510 | ||
03b717a3 MS |
4511 | /* XXX We should totally drop the check for ephemeral here |
4512 | * and thus effectively make 'Type=idle' pointless. */ | |
cb6531be | 4513 | if (type == STATUS_TYPE_EPHEMERAL && m && m->n_on_console > 0) |
03b717a3 MS |
4514 | return; |
4515 | ||
25cee550 | 4516 | va_start(ap, format); |
a885727a | 4517 | status_vprintf(status, SHOW_STATUS_ELLIPSIZE|(type == STATUS_TYPE_EPHEMERAL ? SHOW_STATUS_EPHEMERAL : 0), format, ap); |
25cee550 MS |
4518 | va_end(ap); |
4519 | } | |
4520 | ||
9e615fa3 | 4521 | Set* manager_get_units_needing_mounts_for(Manager *m, const char *path, UnitMountDependencyType t) { |
a57f7e2c LP |
4522 | assert(m); |
4523 | assert(path); | |
9e615fa3 | 4524 | assert(t >= 0 && t < _UNIT_MOUNT_DEPENDENCY_TYPE_MAX); |
a57f7e2c | 4525 | |
ac19bdd0 ZJS |
4526 | if (path_equal(path, "/")) |
4527 | path = ""; | |
a57f7e2c | 4528 | |
9e615fa3 | 4529 | return hashmap_get(m->units_needing_mounts_for[t], path); |
a57f7e2c | 4530 | } |
e66cf1a3 | 4531 | |
5269eb6b | 4532 | int manager_update_failed_units(Manager *m, Unit *u, bool failed) { |
03455c28 | 4533 | unsigned size; |
5269eb6b | 4534 | int r; |
03455c28 LDM |
4535 | |
4536 | assert(m); | |
4537 | assert(u->manager == m); | |
4538 | ||
4539 | size = set_size(m->failed_units); | |
4540 | ||
9fff8981 | 4541 | if (failed) { |
de7fef4b | 4542 | r = set_ensure_put(&m->failed_units, NULL, u); |
5269eb6b LP |
4543 | if (r < 0) |
4544 | return log_oom(); | |
9fff8981 | 4545 | } else |
5269eb6b | 4546 | (void) set_remove(m->failed_units, u); |
03455c28 LDM |
4547 | |
4548 | if (set_size(m->failed_units) != size) | |
4549 | bus_manager_send_change_signal(m); | |
5269eb6b LP |
4550 | |
4551 | return 0; | |
03455c28 LDM |
4552 | } |
4553 | ||
f755e3b7 LP |
4554 | ManagerState manager_state(Manager *m) { |
4555 | Unit *u; | |
4556 | ||
4557 | assert(m); | |
4558 | ||
f9d29f6d JB |
4559 | /* Is the special shutdown target active or queued? If so, we are in shutdown state */ |
4560 | u = manager_get_unit(m, SPECIAL_SHUTDOWN_TARGET); | |
4561 | if (u && unit_active_or_pending(u)) | |
4562 | return MANAGER_STOPPING; | |
4563 | ||
f755e3b7 | 4564 | /* Did we ever finish booting? If not then we are still starting up */ |
49d5666c | 4565 | if (!MANAGER_IS_FINISHED(m)) { |
d81afec1 LP |
4566 | |
4567 | u = manager_get_unit(m, SPECIAL_BASIC_TARGET); | |
4568 | if (!u || !UNIT_IS_ACTIVE_OR_RELOADING(unit_active_state(u))) | |
4569 | return MANAGER_INITIALIZING; | |
4570 | ||
f755e3b7 | 4571 | return MANAGER_STARTING; |
d81afec1 | 4572 | } |
f755e3b7 | 4573 | |
45a7b16b LP |
4574 | if (MANAGER_IS_SYSTEM(m)) { |
4575 | /* Are the rescue or emergency targets active or queued? If so we are in maintenance state */ | |
4576 | u = manager_get_unit(m, SPECIAL_RESCUE_TARGET); | |
4577 | if (u && unit_active_or_pending(u)) | |
4578 | return MANAGER_MAINTENANCE; | |
f755e3b7 | 4579 | |
45a7b16b LP |
4580 | u = manager_get_unit(m, SPECIAL_EMERGENCY_TARGET); |
4581 | if (u && unit_active_or_pending(u)) | |
4582 | return MANAGER_MAINTENANCE; | |
4583 | } | |
f755e3b7 LP |
4584 | |
4585 | /* Are there any failed units? If so, we are in degraded mode */ | |
43127aeb | 4586 | if (!set_isempty(m->failed_units)) |
f755e3b7 LP |
4587 | return MANAGER_DEGRADED; |
4588 | ||
4589 | return MANAGER_RUNNING; | |
4590 | } | |
4591 | ||
00d9ef85 | 4592 | static void manager_unref_uid_internal( |
010becd9 | 4593 | Hashmap *uid_refs, |
00d9ef85 LP |
4594 | uid_t uid, |
4595 | bool destroy_now, | |
4596 | int (*_clean_ipc)(uid_t uid)) { | |
4597 | ||
4598 | uint32_t c, n; | |
4599 | ||
00d9ef85 LP |
4600 | assert(uid_is_valid(uid)); |
4601 | assert(_clean_ipc); | |
4602 | ||
8f41e6b6 ZJS |
4603 | /* A generic implementation, covering both manager_unref_uid() and manager_unref_gid(), under the |
4604 | * assumption that uid_t and gid_t are actually defined the same way, with the same validity rules. | |
00d9ef85 | 4605 | * |
da890466 | 4606 | * We store a hashmap where the key is the UID/GID and the value is a 32-bit reference counter, whose |
8f41e6b6 ZJS |
4607 | * highest bit is used as flag for marking UIDs/GIDs whose IPC objects to remove when the last |
4608 | * reference to the UID/GID is dropped. The flag is set to on, once at least one reference from a | |
4609 | * unit where RemoveIPC= is set is added on a UID/GID. It is reset when the UID's/GID's reference | |
4610 | * counter drops to 0 again. */ | |
00d9ef85 LP |
4611 | |
4612 | assert_cc(sizeof(uid_t) == sizeof(gid_t)); | |
4613 | assert_cc(UID_INVALID == (uid_t) GID_INVALID); | |
4614 | ||
4615 | if (uid == 0) /* We don't keep track of root, and will never destroy it */ | |
4616 | return; | |
4617 | ||
010becd9 | 4618 | c = PTR_TO_UINT32(hashmap_get(uid_refs, UID_TO_PTR(uid))); |
00d9ef85 LP |
4619 | |
4620 | n = c & ~DESTROY_IPC_FLAG; | |
4621 | assert(n > 0); | |
4622 | n--; | |
4623 | ||
4624 | if (destroy_now && n == 0) { | |
010becd9 | 4625 | hashmap_remove(uid_refs, UID_TO_PTR(uid)); |
00d9ef85 LP |
4626 | |
4627 | if (c & DESTROY_IPC_FLAG) { | |
4628 | log_debug("%s " UID_FMT " is no longer referenced, cleaning up its IPC.", | |
4629 | _clean_ipc == clean_ipc_by_uid ? "UID" : "GID", | |
4630 | uid); | |
4631 | (void) _clean_ipc(uid); | |
4632 | } | |
4633 | } else { | |
4634 | c = n | (c & DESTROY_IPC_FLAG); | |
010becd9 | 4635 | assert_se(hashmap_update(uid_refs, UID_TO_PTR(uid), UINT32_TO_PTR(c)) >= 0); |
00d9ef85 LP |
4636 | } |
4637 | } | |
4638 | ||
4639 | void manager_unref_uid(Manager *m, uid_t uid, bool destroy_now) { | |
010becd9 | 4640 | manager_unref_uid_internal(m->uid_refs, uid, destroy_now, clean_ipc_by_uid); |
00d9ef85 LP |
4641 | } |
4642 | ||
4643 | void manager_unref_gid(Manager *m, gid_t gid, bool destroy_now) { | |
010becd9 | 4644 | manager_unref_uid_internal(m->gid_refs, (uid_t) gid, destroy_now, clean_ipc_by_gid); |
00d9ef85 LP |
4645 | } |
4646 | ||
4647 | static int manager_ref_uid_internal( | |
00d9ef85 LP |
4648 | Hashmap **uid_refs, |
4649 | uid_t uid, | |
4650 | bool clean_ipc) { | |
4651 | ||
4652 | uint32_t c, n; | |
4653 | int r; | |
4654 | ||
00d9ef85 LP |
4655 | assert(uid_refs); |
4656 | assert(uid_is_valid(uid)); | |
4657 | ||
8f41e6b6 ZJS |
4658 | /* A generic implementation, covering both manager_ref_uid() and manager_ref_gid(), under the |
4659 | * assumption that uid_t and gid_t are actually defined the same way, with the same validity | |
4660 | * rules. */ | |
00d9ef85 LP |
4661 | |
4662 | assert_cc(sizeof(uid_t) == sizeof(gid_t)); | |
4663 | assert_cc(UID_INVALID == (uid_t) GID_INVALID); | |
4664 | ||
4665 | if (uid == 0) /* We don't keep track of root, and will never destroy it */ | |
4666 | return 0; | |
4667 | ||
4668 | r = hashmap_ensure_allocated(uid_refs, &trivial_hash_ops); | |
4669 | if (r < 0) | |
4670 | return r; | |
4671 | ||
4672 | c = PTR_TO_UINT32(hashmap_get(*uid_refs, UID_TO_PTR(uid))); | |
4673 | ||
4674 | n = c & ~DESTROY_IPC_FLAG; | |
4675 | n++; | |
4676 | ||
4677 | if (n & DESTROY_IPC_FLAG) /* check for overflow */ | |
4678 | return -EOVERFLOW; | |
4679 | ||
4680 | c = n | (c & DESTROY_IPC_FLAG) | (clean_ipc ? DESTROY_IPC_FLAG : 0); | |
4681 | ||
4682 | return hashmap_replace(*uid_refs, UID_TO_PTR(uid), UINT32_TO_PTR(c)); | |
4683 | } | |
4684 | ||
4685 | int manager_ref_uid(Manager *m, uid_t uid, bool clean_ipc) { | |
010becd9 | 4686 | return manager_ref_uid_internal(&m->uid_refs, uid, clean_ipc); |
00d9ef85 LP |
4687 | } |
4688 | ||
4689 | int manager_ref_gid(Manager *m, gid_t gid, bool clean_ipc) { | |
010becd9 | 4690 | return manager_ref_uid_internal(&m->gid_refs, (uid_t) gid, clean_ipc); |
00d9ef85 LP |
4691 | } |
4692 | ||
4693 | static void manager_vacuum_uid_refs_internal( | |
010becd9 | 4694 | Hashmap *uid_refs, |
00d9ef85 LP |
4695 | int (*_clean_ipc)(uid_t uid)) { |
4696 | ||
00d9ef85 LP |
4697 | void *p, *k; |
4698 | ||
00d9ef85 LP |
4699 | assert(_clean_ipc); |
4700 | ||
010becd9 | 4701 | HASHMAP_FOREACH_KEY(p, k, uid_refs) { |
00d9ef85 LP |
4702 | uint32_t c, n; |
4703 | uid_t uid; | |
4704 | ||
4705 | uid = PTR_TO_UID(k); | |
4706 | c = PTR_TO_UINT32(p); | |
4707 | ||
4708 | n = c & ~DESTROY_IPC_FLAG; | |
4709 | if (n > 0) | |
4710 | continue; | |
4711 | ||
4712 | if (c & DESTROY_IPC_FLAG) { | |
4713 | log_debug("Found unreferenced %s " UID_FMT " after reload/reexec. Cleaning up.", | |
4714 | _clean_ipc == clean_ipc_by_uid ? "UID" : "GID", | |
4715 | uid); | |
4716 | (void) _clean_ipc(uid); | |
4717 | } | |
4718 | ||
010becd9 | 4719 | assert_se(hashmap_remove(uid_refs, k) == p); |
00d9ef85 LP |
4720 | } |
4721 | } | |
4722 | ||
06a4eb07 | 4723 | static void manager_vacuum_uid_refs(Manager *m) { |
010becd9 | 4724 | manager_vacuum_uid_refs_internal(m->uid_refs, clean_ipc_by_uid); |
00d9ef85 LP |
4725 | } |
4726 | ||
06a4eb07 | 4727 | static void manager_vacuum_gid_refs(Manager *m) { |
010becd9 | 4728 | manager_vacuum_uid_refs_internal(m->gid_refs, clean_ipc_by_gid); |
00d9ef85 LP |
4729 | } |
4730 | ||
06a4eb07 FB |
4731 | static void manager_vacuum(Manager *m) { |
4732 | assert(m); | |
4733 | ||
4734 | /* Release any dynamic users no longer referenced */ | |
4735 | dynamic_user_vacuum(m, true); | |
4736 | ||
4737 | /* Release any references to UIDs/GIDs no longer referenced, and destroy any IPC owned by them */ | |
4738 | manager_vacuum_uid_refs(m); | |
4739 | manager_vacuum_gid_refs(m); | |
4740 | ||
4741 | /* Release any runtimes no longer referenced */ | |
e76506b7 | 4742 | exec_shared_runtime_vacuum(m); |
06a4eb07 FB |
4743 | } |
4744 | ||
c3ab362d | 4745 | static int manager_dispatch_user_lookup_fd(sd_event_source *source, int fd, uint32_t revents, void *userdata) { |
00d9ef85 LP |
4746 | struct buffer { |
4747 | uid_t uid; | |
4748 | gid_t gid; | |
4749 | char unit_name[UNIT_NAME_MAX+1]; | |
4750 | } _packed_ buffer; | |
4751 | ||
33fc8b0d | 4752 | Manager *m = ASSERT_PTR(userdata); |
00d9ef85 LP |
4753 | ssize_t l; |
4754 | size_t n; | |
4755 | Unit *u; | |
4756 | ||
33fc8b0d | 4757 | assert(source); |
00d9ef85 | 4758 | |
8f41e6b6 ZJS |
4759 | /* Invoked whenever a child process succeeded resolving its user/group to use and sent us the |
4760 | * resulting UID/GID in a datagram. We parse the datagram here and pass it off to the unit, so that | |
4761 | * it can add a reference to the UID/GID so that it can destroy the UID/GID's IPC objects when the | |
4762 | * reference counter drops to 0. */ | |
00d9ef85 LP |
4763 | |
4764 | l = recv(fd, &buffer, sizeof(buffer), MSG_DONTWAIT); | |
4765 | if (l < 0) { | |
8add30a0 | 4766 | if (ERRNO_IS_TRANSIENT(errno)) |
00d9ef85 LP |
4767 | return 0; |
4768 | ||
4769 | return log_error_errno(errno, "Failed to read from user lookup fd: %m"); | |
4770 | } | |
4771 | ||
4772 | if ((size_t) l <= offsetof(struct buffer, unit_name)) { | |
4773 | log_warning("Received too short user lookup message, ignoring."); | |
4774 | return 0; | |
4775 | } | |
4776 | ||
4777 | if ((size_t) l > offsetof(struct buffer, unit_name) + UNIT_NAME_MAX) { | |
4778 | log_warning("Received too long user lookup message, ignoring."); | |
4779 | return 0; | |
4780 | } | |
4781 | ||
4782 | if (!uid_is_valid(buffer.uid) && !gid_is_valid(buffer.gid)) { | |
4783 | log_warning("Got user lookup message with invalid UID/GID pair, ignoring."); | |
4784 | return 0; | |
4785 | } | |
4786 | ||
4787 | n = (size_t) l - offsetof(struct buffer, unit_name); | |
4788 | if (memchr(buffer.unit_name, 0, n)) { | |
4789 | log_warning("Received lookup message with embedded NUL character, ignoring."); | |
4790 | return 0; | |
4791 | } | |
4792 | ||
4793 | buffer.unit_name[n] = 0; | |
4794 | u = manager_get_unit(m, buffer.unit_name); | |
4795 | if (!u) { | |
4796 | log_debug("Got user lookup message but unit doesn't exist, ignoring."); | |
4797 | return 0; | |
4798 | } | |
4799 | ||
4800 | log_unit_debug(u, "User lookup succeeded: uid=" UID_FMT " gid=" GID_FMT, buffer.uid, buffer.gid); | |
4801 | ||
4802 | unit_notify_user_lookup(u, buffer.uid, buffer.gid); | |
4803 | return 0; | |
4804 | } | |
4805 | ||
817062e6 LP |
4806 | static int manager_dispatch_handoff_timestamp_fd(sd_event_source *source, int fd, uint32_t revents, void *userdata) { |
4807 | Manager *m = ASSERT_PTR(userdata); | |
4808 | usec_t ts[2] = {}; | |
4809 | CMSG_BUFFER_TYPE(CMSG_SPACE(sizeof(struct ucred))) control; | |
4810 | struct msghdr msghdr = { | |
4811 | .msg_iov = &IOVEC_MAKE(ts, sizeof(ts)), | |
4812 | .msg_iovlen = 1, | |
4813 | .msg_control = &control, | |
4814 | .msg_controllen = sizeof(control), | |
4815 | }; | |
4816 | ssize_t n; | |
4817 | ||
4818 | assert(source); | |
4819 | ||
ad501930 | 4820 | n = recvmsg_safe(m->handoff_timestamp_fds[0], &msghdr, MSG_DONTWAIT|MSG_CMSG_CLOEXEC); |
817062e6 LP |
4821 | if (ERRNO_IS_NEG_TRANSIENT(n)) |
4822 | return 0; /* Spurious wakeup, try again */ | |
ad501930 MY |
4823 | if (n == -ECHRNG) { |
4824 | log_warning_errno(n, "Got message with truncated control data (unexpected fds sent?), ignoring."); | |
4825 | return 0; | |
4826 | } | |
817062e6 | 4827 | if (n == -EXFULL) { |
ad501930 | 4828 | log_warning_errno(n, "Got message with truncated payload data, ignoring."); |
817062e6 LP |
4829 | return 0; |
4830 | } | |
4831 | if (n < 0) | |
4832 | return log_error_errno(n, "Failed to receive handoff timestamp message: %m"); | |
4833 | ||
e8b9767f MY |
4834 | cmsg_close_all(&msghdr); |
4835 | ||
817062e6 LP |
4836 | if (n != sizeof(ts)) { |
4837 | log_warning("Got handoff timestamp message of unexpected size %zi (expected %zu), ignoring.", n, sizeof(ts)); | |
4838 | return 0; | |
4839 | } | |
4840 | ||
4841 | struct ucred *ucred = CMSG_FIND_DATA(&msghdr, SOL_SOCKET, SCM_CREDENTIALS, struct ucred); | |
4842 | if (!ucred || !pid_is_valid(ucred->pid)) { | |
43960ff0 | 4843 | log_warning("Received handoff timestamp message without valid credentials. Ignoring."); |
817062e6 LP |
4844 | return 0; |
4845 | } | |
4846 | ||
4847 | log_debug("Got handoff timestamp event for PID " PID_FMT ".", ucred->pid); | |
4848 | ||
4849 | _cleanup_free_ Unit **units = NULL; | |
4850 | int n_units = manager_get_units_for_pidref(m, &PIDREF_MAKE_FROM_PID(ucred->pid), &units); | |
4851 | if (n_units < 0) { | |
4852 | log_warning_errno(n_units, "Unable to determine units for PID " PID_FMT ", ignoring: %m", ucred->pid); | |
4853 | return 0; | |
4854 | } | |
4855 | if (n_units == 0) { | |
4856 | log_debug("Got handoff timestamp for process " PID_FMT " we are not interested in, ignoring.", ucred->pid); | |
4857 | return 0; | |
4858 | } | |
4859 | ||
4860 | dual_timestamp dt = { | |
4861 | .realtime = ts[0], | |
4862 | .monotonic = ts[1], | |
4863 | }; | |
4864 | ||
4865 | FOREACH_ARRAY(u, units, n_units) { | |
4866 | if (!UNIT_VTABLE(*u)->notify_handoff_timestamp) | |
4867 | continue; | |
4868 | ||
4869 | UNIT_VTABLE(*u)->notify_handoff_timestamp(*u, ucred, &dt); | |
4870 | } | |
4871 | ||
4872 | return 0; | |
4873 | } | |
4874 | ||
406f1775 DDM |
4875 | static int manager_dispatch_pidref_transport_fd(sd_event_source *source, int fd, uint32_t revents, void *userdata) { |
4876 | Manager *m = ASSERT_PTR(userdata); | |
4877 | _cleanup_(pidref_done) PidRef child_pidref = PIDREF_NULL, parent_pidref = PIDREF_NULL; | |
4878 | _cleanup_close_ int child_pidfd = -EBADF, parent_pidfd = -EBADF; | |
4879 | struct ucred *ucred = NULL; | |
4880 | CMSG_BUFFER_TYPE(CMSG_SPACE(sizeof(struct ucred)) + CMSG_SPACE(sizeof(int)) * 2) control; | |
b66948bb | 4881 | pid_t child_pid = 0; /* silence false-positive warning by coverity */ |
406f1775 DDM |
4882 | struct msghdr msghdr = { |
4883 | .msg_iov = &IOVEC_MAKE(&child_pid, sizeof(child_pid)), | |
4884 | .msg_iovlen = 1, | |
4885 | .msg_control = &control, | |
4886 | .msg_controllen = sizeof(control), | |
4887 | }; | |
4888 | struct cmsghdr *cmsg; | |
4889 | ssize_t n; | |
4890 | int r; | |
4891 | ||
4892 | assert(source); | |
4893 | ||
4894 | /* Server expects: | |
4895 | * - Parent PID in ucreds enabled via SO_PASSCRED | |
4896 | * - Parent PIDFD in SCM_PIDFD message enabled via SO_PASSPIDFD | |
4897 | * - Child PIDFD in SCM_RIGHTS in message body | |
4898 | * - Child PID in message IOV | |
4899 | * | |
566b8f4d YW |
4900 | * SO_PASSPIDFD may not be supported by the kernel (it is supported since v6.5) so we fall back to |
4901 | * using parent PID from ucreds and accept some raciness. */ | |
406f1775 DDM |
4902 | n = recvmsg_safe(m->pidref_transport_fds[0], &msghdr, MSG_DONTWAIT|MSG_CMSG_CLOEXEC|MSG_TRUNC); |
4903 | if (ERRNO_IS_NEG_TRANSIENT(n)) | |
4904 | return 0; /* Spurious wakeup, try again */ | |
4905 | if (n == -ECHRNG) { | |
4906 | log_warning_errno(n, "Got message with truncated control data (unexpected fds sent?), ignoring."); | |
4907 | return 0; | |
4908 | } | |
4909 | if (n == -EXFULL) { | |
4910 | log_warning_errno(n, "Got message with truncated payload data, ignoring."); | |
4911 | return 0; | |
4912 | } | |
4913 | if (n < 0) | |
4914 | return log_error_errno(n, "Failed to receive pidref message: %m"); | |
4915 | ||
4916 | if (n != sizeof(child_pid)) { | |
4917 | log_warning("Got pidref message of unexpected size %zi (expected %zu), ignoring.", n, sizeof(child_pid)); | |
4918 | return 0; | |
4919 | } | |
4920 | ||
4921 | CMSG_FOREACH(cmsg, &msghdr) { | |
4922 | if (cmsg->cmsg_level != SOL_SOCKET) | |
4923 | continue; | |
4924 | ||
4925 | if (cmsg->cmsg_type == SCM_CREDENTIALS && cmsg->cmsg_len == CMSG_LEN(sizeof(struct ucred))) { | |
4926 | assert(!ucred); | |
4927 | ucred = CMSG_TYPED_DATA(cmsg, struct ucred); | |
4928 | } else if (cmsg->cmsg_type == SCM_PIDFD) { | |
4929 | assert(parent_pidfd < 0); | |
4930 | parent_pidfd = *CMSG_TYPED_DATA(cmsg, int); | |
4931 | } else if (cmsg->cmsg_type == SCM_RIGHTS) { | |
4932 | assert(child_pidfd < 0); | |
4933 | child_pidfd = *CMSG_TYPED_DATA(cmsg, int); | |
4934 | } | |
4935 | } | |
4936 | ||
4937 | /* Verify and set parent pidref. */ | |
4938 | if (!ucred || !pid_is_valid(ucred->pid)) { | |
4939 | log_warning("Received pidref message without valid credentials. Ignoring."); | |
4940 | return 0; | |
4941 | } | |
4942 | ||
4943 | /* Need to handle kernels without SO_PASSPIDFD where SCM_PIDFD will not be set. */ | |
4944 | if (parent_pidfd >= 0) | |
4945 | r = pidref_set_pidfd_consume(&parent_pidref, TAKE_FD(parent_pidfd)); | |
4946 | else | |
4947 | r = pidref_set_pid(&parent_pidref, ucred->pid); | |
4948 | if (r < 0) { | |
4949 | if (r == -ESRCH) | |
4950 | log_debug_errno(r, "PidRef child process died before message is processed. Ignoring."); | |
4951 | else | |
4952 | log_warning_errno(r, "Failed to pin pidref child process, ignoring message: %m"); | |
4953 | return 0; | |
4954 | } | |
4955 | ||
4956 | if (parent_pidref.pid != ucred->pid) { | |
4957 | assert(parent_pidref.fd >= 0); | |
4958 | log_warning("Got SCM_PIDFD for parent process " PID_FMT " but got SCM_CREDENTIALS for parent process " PID_FMT ". Ignoring.", | |
4959 | parent_pidref.pid, ucred->pid); | |
4960 | return 0; | |
4961 | } | |
4962 | ||
4963 | /* Verify and set child pidref. */ | |
4964 | if (!pid_is_valid(child_pid)) { | |
4965 | log_warning("Received pidref message without valid child PID. Ignoring."); | |
4966 | return 0; | |
4967 | } | |
4968 | ||
4969 | /* Need to handle kernels without PIDFD support. */ | |
4970 | if (child_pidfd >= 0) | |
4971 | r = pidref_set_pidfd_consume(&child_pidref, TAKE_FD(child_pidfd)); | |
4972 | else | |
4973 | r = pidref_set_pid(&child_pidref, child_pid); | |
4974 | if (r < 0) { | |
4975 | if (r == -ESRCH) | |
4976 | log_debug_errno(r, "PidRef child process died before message is processed. Ignoring."); | |
4977 | else | |
4978 | log_warning_errno(r, "Failed to pin pidref child process, ignoring message: %m"); | |
4979 | return 0; | |
4980 | } | |
4981 | ||
4982 | if (child_pidref.pid != child_pid) { | |
4983 | assert(child_pidref.fd >= 0); | |
4984 | log_warning("Got SCM_RIGHTS for child process " PID_FMT " but PID in IOV message is " PID_FMT ". Ignoring.", | |
4985 | child_pidref.pid, child_pid); | |
4986 | return 0; | |
4987 | } | |
4988 | ||
4989 | log_debug("Got pidref event with parent PID " PID_FMT " and child PID " PID_FMT ".", parent_pidref.pid, child_pidref.pid); | |
4990 | ||
4991 | /* Try finding cgroup of parent process. But if parent process exited and we're not using PIDFD, this could return NULL. | |
4992 | * Then fall back to finding cgroup of the child process. */ | |
4993 | Unit *u = manager_get_unit_by_pidref_cgroup(m, &parent_pidref); | |
4994 | if (!u) | |
4995 | u = manager_get_unit_by_pidref_cgroup(m, &child_pidref); | |
4996 | if (!u) { | |
4997 | 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); | |
4998 | return 0; | |
4999 | } | |
5000 | ||
5001 | if (!UNIT_VTABLE(u)->notify_pidref) { | |
5002 | log_unit_warning(u, "Received pidref event from unexpected unit type '%s'.", unit_type_to_string(u->type)); | |
5003 | return 0; | |
5004 | } | |
5005 | ||
5006 | UNIT_VTABLE(u)->notify_pidref(u, &parent_pidref, &child_pidref); | |
5007 | ||
5008 | return 0; | |
5009 | } | |
5010 | ||
adefcf28 LP |
5011 | void manager_ref_console(Manager *m) { |
5012 | assert(m); | |
5013 | ||
5014 | m->n_on_console++; | |
5015 | } | |
5016 | ||
5017 | void manager_unref_console(Manager *m) { | |
5018 | ||
5019 | assert(m->n_on_console > 0); | |
5020 | m->n_on_console--; | |
5021 | ||
5022 | if (m->n_on_console == 0) | |
5023 | m->no_console_output = false; /* unset no_console_output flag, since the console is definitely free now */ | |
5024 | } | |
5025 | ||
a6ecbf83 FB |
5026 | void manager_override_log_level(Manager *m, int level) { |
5027 | _cleanup_free_ char *s = NULL; | |
5028 | assert(m); | |
5029 | ||
5030 | if (!m->log_level_overridden) { | |
5031 | m->original_log_level = log_get_max_level(); | |
5032 | m->log_level_overridden = true; | |
5033 | } | |
5034 | ||
5035 | (void) log_level_to_string_alloc(level, &s); | |
5036 | log_info("Setting log level to %s.", strna(s)); | |
5037 | ||
5038 | log_set_max_level(level); | |
5039 | } | |
5040 | ||
5041 | void manager_restore_original_log_level(Manager *m) { | |
5042 | _cleanup_free_ char *s = NULL; | |
5043 | assert(m); | |
5044 | ||
5045 | if (!m->log_level_overridden) | |
5046 | return; | |
5047 | ||
5048 | (void) log_level_to_string_alloc(m->original_log_level, &s); | |
5049 | log_info("Restoring log level to original (%s).", strna(s)); | |
5050 | ||
5051 | log_set_max_level(m->original_log_level); | |
5052 | m->log_level_overridden = false; | |
5053 | } | |
5054 | ||
bda7d78b FB |
5055 | void manager_override_log_target(Manager *m, LogTarget target) { |
5056 | assert(m); | |
5057 | ||
5058 | if (!m->log_target_overridden) { | |
5059 | m->original_log_target = log_get_target(); | |
5060 | m->log_target_overridden = true; | |
5061 | } | |
5062 | ||
5063 | log_info("Setting log target to %s.", log_target_to_string(target)); | |
5064 | log_set_target(target); | |
5065 | } | |
5066 | ||
5067 | void manager_restore_original_log_target(Manager *m) { | |
5068 | assert(m); | |
5069 | ||
5070 | if (!m->log_target_overridden) | |
5071 | return; | |
5072 | ||
5073 | log_info("Restoring log target to original %s.", log_target_to_string(m->original_log_target)); | |
5074 | ||
5075 | log_set_target(m->original_log_target); | |
5076 | m->log_target_overridden = false; | |
5077 | } | |
5078 | ||
d4ee7bd8 YW |
5079 | ManagerTimestamp manager_timestamp_initrd_mangle(ManagerTimestamp s) { |
5080 | if (in_initrd() && | |
5081 | s >= MANAGER_TIMESTAMP_SECURITY_START && | |
5082 | s <= MANAGER_TIMESTAMP_UNITS_LOAD_FINISH) | |
5083 | return s - MANAGER_TIMESTAMP_SECURITY_START + MANAGER_TIMESTAMP_INITRD_SECURITY_START; | |
5084 | return s; | |
5085 | } | |
5086 | ||
06044356 LP |
5087 | int manager_allocate_idle_pipe(Manager *m) { |
5088 | int r; | |
5089 | ||
5090 | assert(m); | |
5091 | ||
5092 | if (m->idle_pipe[0] >= 0) { | |
5093 | assert(m->idle_pipe[1] >= 0); | |
5094 | assert(m->idle_pipe[2] >= 0); | |
5095 | assert(m->idle_pipe[3] >= 0); | |
5096 | return 0; | |
5097 | } | |
5098 | ||
5099 | assert(m->idle_pipe[1] < 0); | |
5100 | assert(m->idle_pipe[2] < 0); | |
5101 | assert(m->idle_pipe[3] < 0); | |
5102 | ||
5103 | r = RET_NERRNO(pipe2(m->idle_pipe + 0, O_NONBLOCK|O_CLOEXEC)); | |
5104 | if (r < 0) | |
5105 | return r; | |
5106 | ||
5107 | r = RET_NERRNO(pipe2(m->idle_pipe + 2, O_NONBLOCK|O_CLOEXEC)); | |
5108 | if (r < 0) { | |
5109 | safe_close_pair(m->idle_pipe + 0); | |
5110 | return r; | |
5111 | } | |
5112 | ||
5113 | return 1; | |
5114 | } | |
5115 | ||
ea09a416 LP |
5116 | void unit_defaults_init(UnitDefaults *defaults, RuntimeScope scope) { |
5117 | assert(defaults); | |
5118 | assert(scope >= 0); | |
5119 | assert(scope < _RUNTIME_SCOPE_MAX); | |
5120 | ||
5121 | *defaults = (UnitDefaults) { | |
5122 | .std_output = EXEC_OUTPUT_JOURNAL, | |
5123 | .std_error = EXEC_OUTPUT_INHERIT, | |
5124 | .restart_usec = DEFAULT_RESTART_USEC, | |
5125 | .timeout_start_usec = manager_default_timeout(scope), | |
5126 | .timeout_stop_usec = manager_default_timeout(scope), | |
5127 | .timeout_abort_usec = manager_default_timeout(scope), | |
5128 | .timeout_abort_set = false, | |
5129 | .device_timeout_usec = manager_default_timeout(scope), | |
14702b9c | 5130 | .start_limit = { DEFAULT_START_LIMIT_INTERVAL, DEFAULT_START_LIMIT_BURST }, |
ea09a416 | 5131 | |
ea09a416 LP |
5132 | .memory_accounting = MEMORY_ACCOUNTING_DEFAULT, |
5133 | .io_accounting = false, | |
ea09a416 LP |
5134 | .tasks_accounting = true, |
5135 | .ip_accounting = false, | |
5136 | ||
5137 | .tasks_max = DEFAULT_TASKS_MAX, | |
5138 | .timer_accuracy_usec = 1 * USEC_PER_MINUTE, | |
5139 | ||
5140 | .memory_pressure_watch = CGROUP_PRESSURE_WATCH_AUTO, | |
5141 | .memory_pressure_threshold_usec = MEMORY_PRESSURE_DEFAULT_THRESHOLD_USEC, | |
5142 | ||
5143 | .oom_policy = OOM_STOP, | |
5144 | .oom_score_adjust_set = false, | |
5145 | }; | |
5146 | } | |
5147 | ||
c9e120e0 LP |
5148 | void unit_defaults_done(UnitDefaults *defaults) { |
5149 | assert(defaults); | |
5150 | ||
5151 | defaults->smack_process_label = mfree(defaults->smack_process_label); | |
5152 | rlimit_free_all(defaults->rlimit); | |
5153 | } | |
5154 | ||
bb5232b6 LB |
5155 | LogTarget manager_get_executor_log_target(Manager *m) { |
5156 | assert(m); | |
5157 | ||
5158 | /* If journald is not available tell sd-executor to go to kmsg, as it might be starting journald */ | |
90265bcf DDM |
5159 | if (!MANAGER_IS_TEST_RUN(m) && !manager_journal_is_running(m)) |
5160 | return LOG_TARGET_KMSG; | |
bb5232b6 | 5161 | |
90265bcf | 5162 | return log_get_target(); |
bb5232b6 LB |
5163 | } |
5164 | ||
37ca2ccf | 5165 | static const char* const manager_state_table[_MANAGER_STATE_MAX] = { |
d81afec1 | 5166 | [MANAGER_INITIALIZING] = "initializing", |
48d83e33 ZJS |
5167 | [MANAGER_STARTING] = "starting", |
5168 | [MANAGER_RUNNING] = "running", | |
5169 | [MANAGER_DEGRADED] = "degraded", | |
5170 | [MANAGER_MAINTENANCE] = "maintenance", | |
5171 | [MANAGER_STOPPING] = "stopping", | |
f755e3b7 LP |
5172 | }; |
5173 | ||
5174 | DEFINE_STRING_TABLE_LOOKUP(manager_state, ManagerState); | |
9f9f0342 | 5175 | |
37ca2ccf MY |
5176 | static const char* const manager_objective_table[_MANAGER_OBJECTIVE_MAX] = { |
5177 | [MANAGER_OK] = "ok", | |
5178 | [MANAGER_EXIT] = "exit", | |
5179 | [MANAGER_RELOAD] = "reload", | |
5180 | [MANAGER_REEXECUTE] = "reexecute", | |
5181 | [MANAGER_REBOOT] = "reboot", | |
5182 | [MANAGER_SOFT_REBOOT] = "soft-reboot", | |
5183 | [MANAGER_POWEROFF] = "poweroff", | |
5184 | [MANAGER_HALT] = "halt", | |
5185 | [MANAGER_KEXEC] = "kexec", | |
5186 | [MANAGER_SWITCH_ROOT] = "switch-root", | |
5187 | }; | |
5188 | ||
5189 | DEFINE_STRING_TABLE_LOOKUP(manager_objective, ManagerObjective); | |
5190 | ||
5191 | static const char* const manager_timestamp_table[_MANAGER_TIMESTAMP_MAX] = { | |
48d83e33 ZJS |
5192 | [MANAGER_TIMESTAMP_FIRMWARE] = "firmware", |
5193 | [MANAGER_TIMESTAMP_LOADER] = "loader", | |
5194 | [MANAGER_TIMESTAMP_KERNEL] = "kernel", | |
5195 | [MANAGER_TIMESTAMP_INITRD] = "initrd", | |
5196 | [MANAGER_TIMESTAMP_USERSPACE] = "userspace", | |
5197 | [MANAGER_TIMESTAMP_FINISH] = "finish", | |
5198 | [MANAGER_TIMESTAMP_SECURITY_START] = "security-start", | |
5199 | [MANAGER_TIMESTAMP_SECURITY_FINISH] = "security-finish", | |
5200 | [MANAGER_TIMESTAMP_GENERATORS_START] = "generators-start", | |
5201 | [MANAGER_TIMESTAMP_GENERATORS_FINISH] = "generators-finish", | |
5202 | [MANAGER_TIMESTAMP_UNITS_LOAD_START] = "units-load-start", | |
5203 | [MANAGER_TIMESTAMP_UNITS_LOAD_FINISH] = "units-load-finish", | |
49fbe940 | 5204 | [MANAGER_TIMESTAMP_UNITS_LOAD] = "units-load", |
48d83e33 ZJS |
5205 | [MANAGER_TIMESTAMP_INITRD_SECURITY_START] = "initrd-security-start", |
5206 | [MANAGER_TIMESTAMP_INITRD_SECURITY_FINISH] = "initrd-security-finish", | |
5207 | [MANAGER_TIMESTAMP_INITRD_GENERATORS_START] = "initrd-generators-start", | |
d4ee7bd8 | 5208 | [MANAGER_TIMESTAMP_INITRD_GENERATORS_FINISH] = "initrd-generators-finish", |
48d83e33 | 5209 | [MANAGER_TIMESTAMP_INITRD_UNITS_LOAD_START] = "initrd-units-load-start", |
d4ee7bd8 | 5210 | [MANAGER_TIMESTAMP_INITRD_UNITS_LOAD_FINISH] = "initrd-units-load-finish", |
b3f54861 | 5211 | [MANAGER_TIMESTAMP_SHUTDOWN_START] = "shutdown-start", |
9f9f0342 LP |
5212 | }; |
5213 | ||
5214 | DEFINE_STRING_TABLE_LOOKUP(manager_timestamp, ManagerTimestamp); |