]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
a7334b09 LP |
2 | /*** |
3 | This file is part of systemd. | |
4 | ||
5 | Copyright 2010 Lennart Poettering | |
a7334b09 LP |
6 | ***/ |
7 | ||
60918275 | 8 | #include <errno.h> |
400f1a33 LP |
9 | #include <fcntl.h> |
10 | #include <linux/kd.h> | |
9152c765 | 11 | #include <signal.h> |
713f6f90 | 12 | #include <stdio_ext.h> |
400f1a33 | 13 | #include <string.h> |
e46b13c8 | 14 | #include <sys/epoll.h> |
400f1a33 | 15 | #include <sys/inotify.h> |
e1414003 | 16 | #include <sys/ioctl.h> |
400f1a33 | 17 | #include <sys/reboot.h> |
8742514c | 18 | #include <sys/timerfd.h> |
400f1a33 LP |
19 | #include <sys/wait.h> |
20 | #include <unistd.h> | |
830f6caa | 21 | |
349cc4a5 | 22 | #if HAVE_AUDIT |
4927fcae | 23 | #include <libaudit.h> |
830f6caa | 24 | #endif |
60918275 | 25 | |
718db961 | 26 | #include "sd-daemon.h" |
718db961 | 27 | #include "sd-messages.h" |
3536f49e | 28 | #include "sd-path.h" |
81527be1 | 29 | |
b5efdb8a | 30 | #include "alloc-util.h" |
57b7a260 | 31 | #include "all-units.h" |
400f1a33 LP |
32 | #include "audit-fd.h" |
33 | #include "boot-timestamps.h" | |
34 | #include "bus-common-errors.h" | |
35 | #include "bus-error.h" | |
36 | #include "bus-kernel.h" | |
37 | #include "bus-util.h" | |
00d9ef85 | 38 | #include "clean-ipc.h" |
af6b0ecc | 39 | #include "clock-util.h" |
400f1a33 LP |
40 | #include "dbus-job.h" |
41 | #include "dbus-manager.h" | |
42 | #include "dbus-unit.h" | |
43 | #include "dbus.h" | |
d063a527 | 44 | #include "dirent-util.h" |
400f1a33 | 45 | #include "env-util.h" |
4f5dd394 | 46 | #include "escape.h" |
89711996 | 47 | #include "exec-util.h" |
d3070fbd | 48 | #include "execute.h" |
400f1a33 | 49 | #include "exit-status.h" |
3ffd4af2 | 50 | #include "fd-util.h" |
0d39fa9c | 51 | #include "fileio.h" |
f4f15635 | 52 | #include "fs-util.h" |
60918275 | 53 | #include "hashmap.h" |
c004493c | 54 | #include "io-util.h" |
d3070fbd | 55 | #include "label.h" |
400f1a33 | 56 | #include "locale-setup.h" |
16354eff | 57 | #include "log.h" |
400f1a33 | 58 | #include "macro.h" |
3ffd4af2 | 59 | #include "manager.h" |
400f1a33 | 60 | #include "missing.h" |
49e942b2 | 61 | #include "mkdir.h" |
6bedfcbb | 62 | #include "parse-util.h" |
400f1a33 LP |
63 | #include "path-lookup.h" |
64 | #include "path-util.h" | |
65 | #include "process-util.h" | |
ea430986 | 66 | #include "ratelimit.h" |
31ce987c | 67 | #include "rlimit-util.h" |
c6878637 | 68 | #include "rm-rf.h" |
400f1a33 | 69 | #include "signal-util.h" |
57b7a260 | 70 | #include "socket-util.h" |
514f4ef5 | 71 | #include "special.h" |
8fcde012 | 72 | #include "stat-util.h" |
8b43440b | 73 | #include "string-table.h" |
07630cea | 74 | #include "string-util.h" |
400f1a33 | 75 | #include "strv.h" |
dd1db3c2 | 76 | #include "strxcpyx.h" |
400f1a33 LP |
77 | #include "terminal-util.h" |
78 | #include "time-util.h" | |
79 | #include "transaction.h" | |
affb60b1 | 80 | #include "umask-util.h" |
400f1a33 | 81 | #include "unit-name.h" |
00d9ef85 | 82 | #include "user-util.h" |
400f1a33 | 83 | #include "util.h" |
5dc4c17f | 84 | #include "virt.h" |
e96d6be7 | 85 | #include "watchdog.h" |
60918275 | 86 | |
a47806fa | 87 | #define NOTIFY_RCVBUF_SIZE (8*1024*1024) |
d8fdc620 | 88 | #define CGROUPS_AGENT_RCVBUF_SIZE (8*1024*1024) |
a47806fa | 89 | |
03b717a3 | 90 | /* Initial delay and the interval for printing status messages about running jobs */ |
fd08a840 ZJS |
91 | #define JOBS_IN_PROGRESS_WAIT_USEC (5*USEC_PER_SEC) |
92 | #define JOBS_IN_PROGRESS_PERIOD_USEC (USEC_PER_SEC / 3) | |
03b717a3 MS |
93 | #define JOBS_IN_PROGRESS_PERIOD_DIVISOR 3 |
94 | ||
e0a08581 LP |
95 | /* If there are more than 1K bus messages queue across our API and direct busses, then let's not add more on top until |
96 | * the queue gets more empty. */ | |
97 | #define MANAGER_BUS_BUSY_THRESHOLD 1024LU | |
98 | ||
99 | /* How many units and jobs to process of the bus queue before returning to the event loop. */ | |
100 | #define MANAGER_BUS_MESSAGE_BUDGET 100U | |
101 | ||
718db961 | 102 | static int manager_dispatch_notify_fd(sd_event_source *source, int fd, uint32_t revents, void *userdata); |
d8fdc620 | 103 | static int manager_dispatch_cgroups_agent_fd(sd_event_source *source, int fd, uint32_t revents, void *userdata); |
718db961 LP |
104 | static int manager_dispatch_signal_fd(sd_event_source *source, int fd, uint32_t revents, void *userdata); |
105 | static int manager_dispatch_time_change_fd(sd_event_source *source, int fd, uint32_t revents, void *userdata); | |
106 | static int manager_dispatch_idle_pipe_fd(sd_event_source *source, int fd, uint32_t revents, void *userdata); | |
00d9ef85 | 107 | static int manager_dispatch_user_lookup_fd(sd_event_source *source, int fd, uint32_t revents, void *userdata); |
718db961 | 108 | static int manager_dispatch_jobs_in_progress(sd_event_source *source, usec_t usec, void *userdata); |
752b5905 | 109 | static int manager_dispatch_run_queue(sd_event_source *source, void *userdata); |
575b300b | 110 | static int manager_dispatch_sigchld(sd_event_source *source, void *userdata); |
64691d20 | 111 | static int manager_run_environment_generators(Manager *m); |
e801700e | 112 | static int manager_run_generators(Manager *m); |
718db961 | 113 | |
2ae56591 | 114 | static void manager_watch_jobs_in_progress(Manager *m) { |
e5723c89 | 115 | usec_t next; |
cfa9677b | 116 | int r; |
e5723c89 | 117 | |
718db961 | 118 | assert(m); |
03b717a3 | 119 | |
42bf1ae1 FB |
120 | /* We do not want to show the cylon animation if the user |
121 | * needs to confirm service executions otherwise confirmation | |
122 | * messages will be screwed by the cylon animation. */ | |
b0eb2944 | 123 | if (!manager_is_confirm_spawn_disabled(m)) |
42bf1ae1 FB |
124 | return; |
125 | ||
718db961 | 126 | if (m->jobs_in_progress_event_source) |
2ae56591 | 127 | return; |
03b717a3 | 128 | |
e5723c89 | 129 | next = now(CLOCK_MONOTONIC) + JOBS_IN_PROGRESS_WAIT_USEC; |
cfa9677b | 130 | r = sd_event_add_time( |
6a0f1f6d LP |
131 | m->event, |
132 | &m->jobs_in_progress_event_source, | |
133 | CLOCK_MONOTONIC, | |
134 | next, 0, | |
135 | manager_dispatch_jobs_in_progress, m); | |
cfa9677b MM |
136 | if (r < 0) |
137 | return; | |
7dfbe2e3 TG |
138 | |
139 | (void) sd_event_source_set_description(m->jobs_in_progress_event_source, "manager-jobs-in-progress"); | |
03b717a3 MS |
140 | } |
141 | ||
fbd0b64f | 142 | #define CYLON_BUFFER_EXTRA (2*STRLEN(ANSI_RED) + STRLEN(ANSI_HIGHLIGHT_RED) + 2*STRLEN(ANSI_NORMAL)) |
03b717a3 | 143 | |
03b717a3 MS |
144 | static void draw_cylon(char buffer[], size_t buflen, unsigned width, unsigned pos) { |
145 | char *p = buffer; | |
146 | ||
147 | assert(buflen >= CYLON_BUFFER_EXTRA + width + 1); | |
148 | assert(pos <= width+1); /* 0 or width+1 mean that the center light is behind the corner */ | |
149 | ||
150 | if (pos > 1) { | |
6282c859 MS |
151 | if (pos > 2) |
152 | p = mempset(p, ' ', pos-2); | |
64c3610b FB |
153 | if (log_get_show_color()) |
154 | p = stpcpy(p, ANSI_RED); | |
03b717a3 MS |
155 | *p++ = '*'; |
156 | } | |
157 | ||
158 | if (pos > 0 && pos <= width) { | |
64c3610b FB |
159 | if (log_get_show_color()) |
160 | p = stpcpy(p, ANSI_HIGHLIGHT_RED); | |
03b717a3 MS |
161 | *p++ = '*'; |
162 | } | |
163 | ||
64c3610b FB |
164 | if (log_get_show_color()) |
165 | p = stpcpy(p, ANSI_NORMAL); | |
03b717a3 MS |
166 | |
167 | if (pos < width) { | |
64c3610b FB |
168 | if (log_get_show_color()) |
169 | p = stpcpy(p, ANSI_RED); | |
03b717a3 | 170 | *p++ = '*'; |
6282c859 MS |
171 | if (pos < width-1) |
172 | p = mempset(p, ' ', width-1-pos); | |
64c3610b FB |
173 | if (log_get_show_color()) |
174 | strcpy(p, ANSI_NORMAL); | |
03b717a3 | 175 | } |
03b717a3 MS |
176 | } |
177 | ||
cb8ccb22 | 178 | void manager_flip_auto_status(Manager *m, bool enable) { |
f755e3b7 LP |
179 | assert(m); |
180 | ||
cb8ccb22 ZJS |
181 | if (enable) { |
182 | if (m->show_status == SHOW_STATUS_AUTO) | |
183 | manager_set_show_status(m, SHOW_STATUS_TEMPORARY); | |
184 | } else { | |
185 | if (m->show_status == SHOW_STATUS_TEMPORARY) | |
186 | manager_set_show_status(m, SHOW_STATUS_AUTO); | |
187 | } | |
188 | } | |
189 | ||
03b717a3 | 190 | static void manager_print_jobs_in_progress(Manager *m) { |
718db961 | 191 | _cleanup_free_ char *job_of_n = NULL; |
03b717a3 MS |
192 | Iterator i; |
193 | Job *j; | |
03b717a3 MS |
194 | unsigned counter = 0, print_nr; |
195 | char cylon[6 + CYLON_BUFFER_EXTRA + 1]; | |
196 | unsigned cylon_pos; | |
8bb310c3 ZJS |
197 | char time[FORMAT_TIMESPAN_MAX], limit[FORMAT_TIMESPAN_MAX] = "no limit"; |
198 | uint64_t x; | |
03b717a3 | 199 | |
718db961 | 200 | assert(m); |
9c3349e2 | 201 | assert(m->n_running_jobs > 0); |
718db961 | 202 | |
cb8ccb22 | 203 | manager_flip_auto_status(m, true); |
d450b6f2 | 204 | |
03b717a3 MS |
205 | print_nr = (m->jobs_in_progress_iteration / JOBS_IN_PROGRESS_PERIOD_DIVISOR) % m->n_running_jobs; |
206 | ||
207 | HASHMAP_FOREACH(j, m->jobs, i) | |
208 | if (j->state == JOB_RUNNING && counter++ == print_nr) | |
209 | break; | |
210 | ||
e970a72e MS |
211 | /* m->n_running_jobs must be consistent with the contents of m->jobs, |
212 | * so the above loop must have succeeded in finding j. */ | |
213 | assert(counter == print_nr + 1); | |
51d122af | 214 | assert(j); |
5a82a91a | 215 | |
03b717a3 MS |
216 | cylon_pos = m->jobs_in_progress_iteration % 14; |
217 | if (cylon_pos >= 8) | |
218 | cylon_pos = 14 - cylon_pos; | |
219 | draw_cylon(cylon, sizeof(cylon), 6, cylon_pos); | |
220 | ||
8bb310c3 ZJS |
221 | m->jobs_in_progress_iteration++; |
222 | ||
d6483ba7 ZJS |
223 | if (m->n_running_jobs > 1) { |
224 | if (asprintf(&job_of_n, "(%u of %u) ", counter, m->n_running_jobs) < 0) | |
225 | job_of_n = NULL; | |
226 | } | |
03b717a3 | 227 | |
8bb310c3 ZJS |
228 | format_timespan(time, sizeof(time), now(CLOCK_MONOTONIC) - j->begin_usec, 1*USEC_PER_SEC); |
229 | if (job_get_timeout(j, &x) > 0) | |
230 | format_timespan(limit, sizeof(limit), x - j->begin_usec, 1*USEC_PER_SEC); | |
231 | ||
127d5fd1 | 232 | manager_status_printf(m, STATUS_TYPE_EPHEMERAL, cylon, |
8bb310c3 ZJS |
233 | "%sA %s job is running for %s (%s / %s)", |
234 | strempty(job_of_n), | |
235 | job_type_to_string(j->type), | |
236 | unit_description(j->unit), | |
237 | time, limit); | |
03b717a3 MS |
238 | } |
239 | ||
e46b13c8 ZJS |
240 | static int have_ask_password(void) { |
241 | _cleanup_closedir_ DIR *dir; | |
8fb3f009 | 242 | struct dirent *de; |
e46b13c8 ZJS |
243 | |
244 | dir = opendir("/run/systemd/ask-password"); | |
245 | if (!dir) { | |
246 | if (errno == ENOENT) | |
247 | return false; | |
248 | else | |
249 | return -errno; | |
250 | } | |
251 | ||
8fb3f009 | 252 | FOREACH_DIRENT_ALL(de, dir, return -errno) { |
e46b13c8 ZJS |
253 | if (startswith(de->d_name, "ask.")) |
254 | return true; | |
255 | } | |
8fb3f009 | 256 | return false; |
e46b13c8 ZJS |
257 | } |
258 | ||
259 | static int manager_dispatch_ask_password_fd(sd_event_source *source, | |
260 | int fd, uint32_t revents, void *userdata) { | |
261 | Manager *m = userdata; | |
262 | ||
263 | assert(m); | |
264 | ||
665dfe93 | 265 | (void) flush_fd(fd); |
e46b13c8 ZJS |
266 | |
267 | m->have_ask_password = have_ask_password(); | |
268 | if (m->have_ask_password < 0) | |
269 | /* Log error but continue. Negative have_ask_password | |
270 | * is treated as unknown status. */ | |
c33b3297 | 271 | log_error_errno(m->have_ask_password, "Failed to list /run/systemd/ask-password: %m"); |
e46b13c8 ZJS |
272 | |
273 | return 0; | |
274 | } | |
275 | ||
276 | static void manager_close_ask_password(Manager *m) { | |
277 | assert(m); | |
278 | ||
e46b13c8 | 279 | m->ask_password_event_source = sd_event_source_unref(m->ask_password_event_source); |
90990e28 | 280 | m->ask_password_inotify_fd = safe_close(m->ask_password_inotify_fd); |
e46b13c8 ZJS |
281 | m->have_ask_password = -EINVAL; |
282 | } | |
283 | ||
284 | static int manager_check_ask_password(Manager *m) { | |
285 | int r; | |
286 | ||
287 | assert(m); | |
288 | ||
289 | if (!m->ask_password_event_source) { | |
290 | assert(m->ask_password_inotify_fd < 0); | |
291 | ||
292 | mkdir_p_label("/run/systemd/ask-password", 0755); | |
293 | ||
294 | m->ask_password_inotify_fd = inotify_init1(IN_NONBLOCK|IN_CLOEXEC); | |
4a62c710 MS |
295 | if (m->ask_password_inotify_fd < 0) |
296 | return log_error_errno(errno, "inotify_init1() failed: %m"); | |
e46b13c8 ZJS |
297 | |
298 | if (inotify_add_watch(m->ask_password_inotify_fd, "/run/systemd/ask-password", IN_CREATE|IN_DELETE|IN_MOVE) < 0) { | |
56f64d95 | 299 | log_error_errno(errno, "Failed to add watch on /run/systemd/ask-password: %m"); |
e46b13c8 ZJS |
300 | manager_close_ask_password(m); |
301 | return -errno; | |
302 | } | |
303 | ||
304 | r = sd_event_add_io(m->event, &m->ask_password_event_source, | |
305 | m->ask_password_inotify_fd, EPOLLIN, | |
306 | manager_dispatch_ask_password_fd, m); | |
307 | if (r < 0) { | |
56f64d95 | 308 | log_error_errno(errno, "Failed to add event source for /run/systemd/ask-password: %m"); |
e46b13c8 ZJS |
309 | manager_close_ask_password(m); |
310 | return -errno; | |
311 | } | |
312 | ||
7dfbe2e3 TG |
313 | (void) sd_event_source_set_description(m->ask_password_event_source, "manager-ask-password"); |
314 | ||
e46b13c8 ZJS |
315 | /* Queries might have been added meanwhile... */ |
316 | manager_dispatch_ask_password_fd(m->ask_password_event_source, | |
317 | m->ask_password_inotify_fd, EPOLLIN, m); | |
318 | } | |
319 | ||
320 | return m->have_ask_password; | |
321 | } | |
322 | ||
31a7eb86 | 323 | static int manager_watch_idle_pipe(Manager *m) { |
31a7eb86 ZJS |
324 | int r; |
325 | ||
718db961 LP |
326 | assert(m); |
327 | ||
328 | if (m->idle_pipe_event_source) | |
31a7eb86 ZJS |
329 | return 0; |
330 | ||
331 | if (m->idle_pipe[2] < 0) | |
332 | return 0; | |
333 | ||
151b9b96 | 334 | 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 |
335 | if (r < 0) |
336 | return log_error_errno(r, "Failed to watch idle pipe: %m"); | |
31a7eb86 | 337 | |
7dfbe2e3 TG |
338 | (void) sd_event_source_set_description(m->idle_pipe_event_source, "manager-idle-pipe"); |
339 | ||
31a7eb86 | 340 | return 0; |
31a7eb86 ZJS |
341 | } |
342 | ||
718db961 LP |
343 | static void manager_close_idle_pipe(Manager *m) { |
344 | assert(m); | |
31a7eb86 | 345 | |
cd72bd8a LP |
346 | m->idle_pipe_event_source = sd_event_source_unref(m->idle_pipe_event_source); |
347 | ||
3d94f76c LP |
348 | safe_close_pair(m->idle_pipe); |
349 | safe_close_pair(m->idle_pipe + 2); | |
31a7eb86 ZJS |
350 | } |
351 | ||
8742514c | 352 | static int manager_setup_time_change(Manager *m) { |
718db961 | 353 | int r; |
b92bea5d ZJS |
354 | |
355 | /* We only care for the cancellation event, hence we set the | |
356 | * timeout to the latest possible value. */ | |
357 | struct itimerspec its = { | |
358 | .it_value.tv_sec = TIME_T_MAX, | |
359 | }; | |
8742514c | 360 | |
718db961 LP |
361 | assert(m); |
362 | assert_cc(sizeof(time_t) == sizeof(TIME_T_MAX)); | |
8742514c | 363 | |
e0a3da1f | 364 | if (m->test_run_flags) |
0d8c31ff ZJS |
365 | return 0; |
366 | ||
8742514c LP |
367 | /* Uses TFD_TIMER_CANCEL_ON_SET to get notifications whenever |
368 | * CLOCK_REALTIME makes a jump relative to CLOCK_MONOTONIC */ | |
369 | ||
718db961 | 370 | m->time_change_fd = timerfd_create(CLOCK_REALTIME, TFD_NONBLOCK|TFD_CLOEXEC); |
4a62c710 MS |
371 | if (m->time_change_fd < 0) |
372 | return log_error_errno(errno, "Failed to create timerfd: %m"); | |
8742514c | 373 | |
718db961 | 374 | if (timerfd_settime(m->time_change_fd, TFD_TIMER_ABSTIME|TFD_TIMER_CANCEL_ON_SET, &its, NULL) < 0) { |
56f64d95 | 375 | log_debug_errno(errno, "Failed to set up TFD_TIMER_CANCEL_ON_SET, ignoring: %m"); |
03e334a1 | 376 | m->time_change_fd = safe_close(m->time_change_fd); |
8742514c LP |
377 | return 0; |
378 | } | |
379 | ||
151b9b96 | 380 | r = sd_event_add_io(m->event, &m->time_change_event_source, m->time_change_fd, EPOLLIN, manager_dispatch_time_change_fd, m); |
23bbb0de MS |
381 | if (r < 0) |
382 | return log_error_errno(r, "Failed to create time change event source: %m"); | |
8742514c | 383 | |
7dfbe2e3 TG |
384 | (void) sd_event_source_set_description(m->time_change_event_source, "manager-time-change"); |
385 | ||
8742514c LP |
386 | log_debug("Set up TFD_TIMER_CANCEL_ON_SET timerfd."); |
387 | ||
388 | return 0; | |
389 | } | |
390 | ||
80876c20 | 391 | static int enable_special_signals(Manager *m) { |
718db961 | 392 | _cleanup_close_ int fd = -1; |
80876c20 LP |
393 | |
394 | assert(m); | |
395 | ||
e0a3da1f | 396 | if (m->test_run_flags) |
37453b3a EV |
397 | return 0; |
398 | ||
a41b539e | 399 | /* Enable that we get SIGINT on control-alt-del. In containers |
c9999773 LP |
400 | * this will fail with EPERM (older) or EINVAL (newer), so |
401 | * ignore that. */ | |
4c701096 | 402 | if (reboot(RB_DISABLE_CAD) < 0 && !IN_SET(errno, EPERM, EINVAL)) |
56f64d95 | 403 | log_warning_errno(errno, "Failed to enable ctrl-alt-del handling: %m"); |
80876c20 | 404 | |
a41b539e LP |
405 | fd = open_terminal("/dev/tty0", O_RDWR|O_NOCTTY|O_CLOEXEC); |
406 | if (fd < 0) { | |
407 | /* Support systems without virtual console */ | |
408 | if (fd != -ENOENT) | |
56f64d95 | 409 | log_warning_errno(errno, "Failed to open /dev/tty0: %m"); |
a41b539e | 410 | } else { |
80876c20 LP |
411 | /* Enable that we get SIGWINCH on kbrequest */ |
412 | if (ioctl(fd, KDSIGACCEPT, SIGWINCH) < 0) | |
56f64d95 | 413 | log_warning_errno(errno, "Failed to enable kbrequest handling: %m"); |
80876c20 LP |
414 | } |
415 | ||
416 | return 0; | |
417 | } | |
418 | ||
8750ac02 ZJS |
419 | #define RTSIG_IF_AVAILABLE(signum) (signum <= SIGRTMAX ? signum : -1) |
420 | ||
ce578209 | 421 | static int manager_setup_signals(Manager *m) { |
b92bea5d ZJS |
422 | struct sigaction sa = { |
423 | .sa_handler = SIG_DFL, | |
424 | .sa_flags = SA_NOCLDSTOP|SA_RESTART, | |
425 | }; | |
718db961 LP |
426 | sigset_t mask; |
427 | int r; | |
60918275 | 428 | |
ce578209 LP |
429 | assert(m); |
430 | ||
57c0c30e LP |
431 | assert_se(sigaction(SIGCHLD, &sa, NULL) == 0); |
432 | ||
4dffec14 LP |
433 | /* We make liberal use of realtime signals here. On |
434 | * Linux/glibc we have 30 of them (with the exception of Linux | |
435 | * on hppa, see below), between SIGRTMIN+0 ... SIGRTMIN+30 | |
436 | * (aka SIGRTMAX). */ | |
7d793605 | 437 | |
4dffec14 | 438 | assert_se(sigemptyset(&mask) == 0); |
7d793605 LP |
439 | sigset_add_many(&mask, |
440 | SIGCHLD, /* Child died */ | |
441 | SIGTERM, /* Reexecute daemon */ | |
442 | SIGHUP, /* Reload configuration */ | |
443 | SIGUSR1, /* systemd/upstart: reconnect to D-Bus */ | |
444 | SIGUSR2, /* systemd: dump status */ | |
445 | SIGINT, /* Kernel sends us this on control-alt-del */ | |
446 | SIGWINCH, /* Kernel sends us this on kbrequest (alt-arrowup) */ | |
447 | SIGPWR, /* Some kernel drivers and upsd send us this on power failure */ | |
4dffec14 | 448 | |
7d793605 | 449 | SIGRTMIN+0, /* systemd: start default.target */ |
0003d1ab | 450 | SIGRTMIN+1, /* systemd: isolate rescue.target */ |
7d793605 LP |
451 | SIGRTMIN+2, /* systemd: isolate emergency.target */ |
452 | SIGRTMIN+3, /* systemd: start halt.target */ | |
453 | SIGRTMIN+4, /* systemd: start poweroff.target */ | |
454 | SIGRTMIN+5, /* systemd: start reboot.target */ | |
0003d1ab | 455 | SIGRTMIN+6, /* systemd: start kexec.target */ |
4dffec14 LP |
456 | |
457 | /* ... space for more special targets ... */ | |
458 | ||
0003d1ab LP |
459 | SIGRTMIN+13, /* systemd: Immediate halt */ |
460 | SIGRTMIN+14, /* systemd: Immediate poweroff */ | |
461 | SIGRTMIN+15, /* systemd: Immediate reboot */ | |
462 | SIGRTMIN+16, /* systemd: Immediate kexec */ | |
4dffec14 LP |
463 | |
464 | /* ... space for more immediate system state changes ... */ | |
465 | ||
0658666b LP |
466 | SIGRTMIN+20, /* systemd: enable status messages */ |
467 | SIGRTMIN+21, /* systemd: disable status messages */ | |
253ee27a LP |
468 | SIGRTMIN+22, /* systemd: set log level to LOG_DEBUG */ |
469 | SIGRTMIN+23, /* systemd: set log level to LOG_INFO */ | |
600b704e | 470 | SIGRTMIN+24, /* systemd: Immediate exit (--user only) */ |
4dffec14 LP |
471 | |
472 | /* .. one free signal here ... */ | |
473 | ||
8750ac02 ZJS |
474 | /* Apparently Linux on hppa had fewer RT signals until v3.18, |
475 | * SIGRTMAX was SIGRTMIN+25, and then SIGRTMIN was lowered, | |
476 | * see commit v3.17-7614-g1f25df2eff. | |
477 | * | |
478 | * We cannot unconditionally make use of those signals here, | |
479 | * so let's use a runtime check. Since these commands are | |
480 | * accessible by different means and only really a safety | |
481 | * net, the missing functionality on hppa shouldn't matter. | |
482 | */ | |
483 | ||
484 | RTSIG_IF_AVAILABLE(SIGRTMIN+26), /* systemd: set log target to journal-or-kmsg */ | |
485 | RTSIG_IF_AVAILABLE(SIGRTMIN+27), /* systemd: set log target to console */ | |
486 | RTSIG_IF_AVAILABLE(SIGRTMIN+28), /* systemd: set log target to kmsg */ | |
487 | RTSIG_IF_AVAILABLE(SIGRTMIN+29), /* systemd: set log target to syslog-or-kmsg (obsolete) */ | |
4dffec14 LP |
488 | |
489 | /* ... one free signal here SIGRTMIN+30 ... */ | |
7d793605 | 490 | -1); |
ce578209 LP |
491 | assert_se(sigprocmask(SIG_SETMASK, &mask, NULL) == 0); |
492 | ||
718db961 LP |
493 | m->signal_fd = signalfd(-1, &mask, SFD_NONBLOCK|SFD_CLOEXEC); |
494 | if (m->signal_fd < 0) | |
ce578209 LP |
495 | return -errno; |
496 | ||
151b9b96 | 497 | r = sd_event_add_io(m->event, &m->signal_event_source, m->signal_fd, EPOLLIN, manager_dispatch_signal_fd, m); |
718db961 LP |
498 | if (r < 0) |
499 | return r; | |
ce578209 | 500 | |
7dfbe2e3 TG |
501 | (void) sd_event_source_set_description(m->signal_event_source, "manager-signal"); |
502 | ||
d8fdc620 LP |
503 | /* Process signals a bit earlier than the rest of things, but later than notify_fd processing, so that the |
504 | * notify processing can still figure out to which process/service a message belongs, before we reap the | |
505 | * process. Also, process this before handling cgroup notifications, so that we always collect child exit | |
506 | * status information before detecting that there's no process in a cgroup. */ | |
507 | r = sd_event_source_set_priority(m->signal_event_source, SD_EVENT_PRIORITY_NORMAL-6); | |
29083707 LP |
508 | if (r < 0) |
509 | return r; | |
510 | ||
463d0d15 | 511 | if (MANAGER_IS_SYSTEM(m)) |
80876c20 | 512 | return enable_special_signals(m); |
e1414003 | 513 | |
ce578209 LP |
514 | return 0; |
515 | } | |
516 | ||
47cf8ff2 | 517 | static void manager_sanitize_environment(Manager *m) { |
f069efb4 LP |
518 | assert(m); |
519 | ||
47cf8ff2 | 520 | /* Let's remove some environment variables that we need ourselves to communicate with our clients */ |
f069efb4 LP |
521 | strv_env_unset_many( |
522 | m->environment, | |
47cf8ff2 LP |
523 | "EXIT_CODE", |
524 | "EXIT_STATUS", | |
525 | "INVOCATION_ID", | |
526 | "JOURNAL_STREAM", | |
527 | "LISTEN_FDNAMES", | |
528 | "LISTEN_FDS", | |
529 | "LISTEN_PID", | |
f069efb4 LP |
530 | "MAINPID", |
531 | "MANAGERPID", | |
47cf8ff2 LP |
532 | "NOTIFY_SOCKET", |
533 | "REMOTE_ADDR", | |
534 | "REMOTE_PORT", | |
535 | "SERVICE_RESULT", | |
f069efb4 LP |
536 | "WATCHDOG_PID", |
537 | "WATCHDOG_USEC", | |
538 | NULL); | |
47cf8ff2 LP |
539 | |
540 | /* Let's order the environment alphabetically, just to make it pretty */ | |
541 | strv_sort(m->environment); | |
f069efb4 LP |
542 | } |
543 | ||
e21fea24 | 544 | static int manager_default_environment(Manager *m) { |
71ecc858 LP |
545 | assert(m); |
546 | ||
463d0d15 | 547 | if (MANAGER_IS_SYSTEM(m)) { |
e21fea24 KS |
548 | /* The system manager always starts with a clean |
549 | * environment for its children. It does not import | |
71cb7d30 | 550 | * the kernel's or the parents' exported variables. |
e21fea24 | 551 | * |
71cb7d30 | 552 | * The initial passed environment is untouched to keep |
e21fea24 KS |
553 | * /proc/self/environ valid; it is used for tagging |
554 | * the init process inside containers. */ | |
43638332 ZJS |
555 | m->environment = strv_new("PATH=" DEFAULT_PATH, |
556 | NULL); | |
e21fea24 KS |
557 | |
558 | /* Import locale variables LC_*= from configuration */ | |
559 | locale_setup(&m->environment); | |
71cb7d30 | 560 | } else |
e21fea24 KS |
561 | /* The user manager passes its own environment |
562 | * along to its children. */ | |
563 | m->environment = strv_copy(environ); | |
43d03a83 | 564 | |
e21fea24 KS |
565 | if (!m->environment) |
566 | return -ENOMEM; | |
8b55b8c4 | 567 | |
47cf8ff2 | 568 | manager_sanitize_environment(m); |
9d5a3757 | 569 | |
e21fea24 | 570 | return 0; |
71ecc858 LP |
571 | } |
572 | ||
3536f49e YW |
573 | static int manager_setup_prefix(Manager *m) { |
574 | struct table_entry { | |
575 | uint64_t type; | |
576 | const char *suffix; | |
577 | }; | |
578 | ||
72fd1768 | 579 | static const struct table_entry paths_system[_EXEC_DIRECTORY_TYPE_MAX] = { |
3536f49e YW |
580 | [EXEC_DIRECTORY_RUNTIME] = { SD_PATH_SYSTEM_RUNTIME, NULL }, |
581 | [EXEC_DIRECTORY_STATE] = { SD_PATH_SYSTEM_STATE_PRIVATE, NULL }, | |
582 | [EXEC_DIRECTORY_CACHE] = { SD_PATH_SYSTEM_STATE_CACHE, NULL }, | |
583 | [EXEC_DIRECTORY_LOGS] = { SD_PATH_SYSTEM_STATE_LOGS, NULL }, | |
584 | [EXEC_DIRECTORY_CONFIGURATION] = { SD_PATH_SYSTEM_CONFIGURATION, NULL }, | |
585 | }; | |
586 | ||
72fd1768 | 587 | static const struct table_entry paths_user[_EXEC_DIRECTORY_TYPE_MAX] = { |
3536f49e YW |
588 | [EXEC_DIRECTORY_RUNTIME] = { SD_PATH_USER_RUNTIME, NULL }, |
589 | [EXEC_DIRECTORY_STATE] = { SD_PATH_USER_CONFIGURATION, NULL }, | |
c6218495 LP |
590 | [EXEC_DIRECTORY_CACHE] = { SD_PATH_USER_STATE_CACHE, NULL }, |
591 | [EXEC_DIRECTORY_LOGS] = { SD_PATH_USER_CONFIGURATION, "log" }, | |
592 | [EXEC_DIRECTORY_CONFIGURATION] = { SD_PATH_USER_CONFIGURATION, NULL }, | |
3536f49e YW |
593 | }; |
594 | ||
595 | const struct table_entry *p; | |
596 | ExecDirectoryType i; | |
597 | int r; | |
598 | ||
599 | assert(m); | |
600 | ||
601 | if (MANAGER_IS_SYSTEM(m)) | |
602 | p = paths_system; | |
603 | else | |
604 | p = paths_user; | |
605 | ||
72fd1768 | 606 | for (i = 0; i < _EXEC_DIRECTORY_TYPE_MAX; i++) { |
3536f49e YW |
607 | r = sd_path_home(p[i].type, p[i].suffix, &m->prefix[i]); |
608 | if (r < 0) | |
609 | return r; | |
610 | } | |
611 | ||
612 | return 0; | |
613 | } | |
614 | ||
279d81dd LP |
615 | static int manager_setup_run_queue(Manager *m) { |
616 | int r; | |
617 | ||
618 | assert(m); | |
619 | assert(!m->run_queue_event_source); | |
620 | ||
621 | r = sd_event_add_defer(m->event, &m->run_queue_event_source, manager_dispatch_run_queue, m); | |
622 | if (r < 0) | |
623 | return r; | |
624 | ||
625 | r = sd_event_source_set_priority(m->run_queue_event_source, SD_EVENT_PRIORITY_IDLE); | |
626 | if (r < 0) | |
627 | return r; | |
628 | ||
629 | r = sd_event_source_set_enabled(m->run_queue_event_source, SD_EVENT_OFF); | |
630 | if (r < 0) | |
631 | return r; | |
632 | ||
633 | (void) sd_event_source_set_description(m->run_queue_event_source, "manager-run-queue"); | |
634 | ||
635 | return 0; | |
636 | } | |
637 | ||
575b300b LP |
638 | static int manager_setup_sigchld_event_source(Manager *m) { |
639 | int r; | |
640 | ||
641 | assert(m); | |
642 | assert(!m->sigchld_event_source); | |
643 | ||
644 | r = sd_event_add_defer(m->event, &m->sigchld_event_source, manager_dispatch_sigchld, m); | |
645 | if (r < 0) | |
646 | return r; | |
647 | ||
648 | r = sd_event_source_set_priority(m->sigchld_event_source, SD_EVENT_PRIORITY_NORMAL-7); | |
649 | if (r < 0) | |
650 | return r; | |
651 | ||
652 | r = sd_event_source_set_enabled(m->sigchld_event_source, SD_EVENT_OFF); | |
653 | if (r < 0) | |
654 | return r; | |
655 | ||
656 | (void) sd_event_source_set_description(m->sigchld_event_source, "manager-sigchld"); | |
657 | ||
658 | return 0; | |
659 | } | |
660 | ||
e0a3da1f | 661 | int manager_new(UnitFileScope scope, unsigned test_run_flags, Manager **_m) { |
c70cac54 | 662 | _cleanup_(manager_freep) Manager *m = NULL; |
e3dd987c | 663 | int r; |
8e274523 LP |
664 | |
665 | assert(_m); | |
463d0d15 | 666 | assert(IN_SET(scope, UNIT_FILE_SYSTEM, UNIT_FILE_USER)); |
ce578209 | 667 | |
915b3753 LP |
668 | m = new0(Manager, 1); |
669 | if (!m) | |
8e274523 | 670 | return -ENOMEM; |
60918275 | 671 | |
463d0d15 | 672 | m->unit_file_scope = scope; |
a16e1123 | 673 | m->exit_code = _MANAGER_EXIT_CODE_INVALID; |
bd8f585b | 674 | m->default_timer_accuracy_usec = USEC_PER_MINUTE; |
444d5863 | 675 | m->default_memory_accounting = MEMORY_ACCOUNTING_DEFAULT; |
9ded9cd1 | 676 | m->default_tasks_accounting = true; |
79baeeb9 | 677 | m->default_tasks_max = UINT64_MAX; |
bd389aa7 LP |
678 | m->default_timeout_start_usec = DEFAULT_TIMEOUT_USEC; |
679 | m->default_timeout_stop_usec = DEFAULT_TIMEOUT_USEC; | |
680 | m->default_restart_usec = DEFAULT_RESTART_USEC; | |
80876c20 | 681 | |
349cc4a5 | 682 | #if ENABLE_EFI |
463d0d15 | 683 | if (MANAGER_IS_SYSTEM(m) && detect_container() <= 0) |
9f9f0342 LP |
684 | boot_timestamps(m->timestamps + MANAGER_TIMESTAMP_USERSPACE, |
685 | m->timestamps + MANAGER_TIMESTAMP_FIRMWARE, | |
686 | m->timestamps + MANAGER_TIMESTAMP_LOADER); | |
463d0d15 LP |
687 | #endif |
688 | ||
f2341e0a | 689 | /* Prepare log fields we can use for structured logging */ |
463d0d15 LP |
690 | if (MANAGER_IS_SYSTEM(m)) { |
691 | m->unit_log_field = "UNIT="; | |
692 | m->unit_log_format_string = "UNIT=%s"; | |
4b58153d LP |
693 | |
694 | m->invocation_log_field = "INVOCATION_ID="; | |
f1c50bec | 695 | m->invocation_log_format_string = "INVOCATION_ID=%s"; |
463d0d15 LP |
696 | } else { |
697 | m->unit_log_field = "USER_UNIT="; | |
698 | m->unit_log_format_string = "USER_UNIT=%s"; | |
4b58153d LP |
699 | |
700 | m->invocation_log_field = "USER_INVOCATION_ID="; | |
f1c50bec | 701 | m->invocation_log_format_string = "USER_INVOCATION_ID=%s"; |
463d0d15 | 702 | } |
f2341e0a | 703 | |
718db961 | 704 | m->idle_pipe[0] = m->idle_pipe[1] = m->idle_pipe[2] = m->idle_pipe[3] = -1; |
8742514c | 705 | |
d8fdc620 | 706 | m->pin_cgroupfs_fd = m->notify_fd = m->cgroups_agent_fd = m->signal_fd = m->time_change_fd = |
232f6754 | 707 | m->dev_autofs_fd = m->private_listen_fd = m->cgroup_inotify_fd = |
d8fdc620 | 708 | m->ask_password_inotify_fd = -1; |
d379d442 | 709 | |
00d9ef85 LP |
710 | m->user_lookup_fds[0] = m->user_lookup_fds[1] = -1; |
711 | ||
ea430986 | 712 | m->current_job_id = 1; /* start as id #1, so that we can leave #0 around as "null-like" value */ |
9152c765 | 713 | |
e46b13c8 | 714 | m->have_ask_password = -EINVAL; /* we don't know */ |
ae2a2c53 | 715 | m->first_boot = -1; |
e46b13c8 | 716 | |
e0a3da1f | 717 | m->test_run_flags = test_run_flags; |
0d8c31ff | 718 | |
2e5c94b9 LP |
719 | /* Reboot immediately if the user hits C-A-D more often than 7x per 2s */ |
720 | RATELIMIT_INIT(m->ctrl_alt_del_ratelimit, 2 * USEC_PER_SEC, 7); | |
721 | ||
e21fea24 KS |
722 | r = manager_default_environment(m); |
723 | if (r < 0) | |
c70cac54 | 724 | return r; |
1137a57c | 725 | |
d5099efc | 726 | r = hashmap_ensure_allocated(&m->units, &string_hash_ops); |
718db961 | 727 | if (r < 0) |
c70cac54 | 728 | return r; |
60918275 | 729 | |
d5099efc | 730 | r = hashmap_ensure_allocated(&m->jobs, NULL); |
718db961 | 731 | if (r < 0) |
c70cac54 | 732 | return r; |
60918275 | 733 | |
548f6937 | 734 | r = hashmap_ensure_allocated(&m->cgroup_unit, &path_hash_ops); |
718db961 | 735 | if (r < 0) |
c70cac54 | 736 | return r; |
9152c765 | 737 | |
d5099efc | 738 | r = hashmap_ensure_allocated(&m->watch_bus, &string_hash_ops); |
718db961 | 739 | if (r < 0) |
c70cac54 | 740 | return r; |
05e343b7 | 741 | |
e8112e67 | 742 | r = manager_setup_prefix(m); |
718db961 | 743 | if (r < 0) |
c70cac54 | 744 | return r; |
8742514c | 745 | |
e8112e67 ZJS |
746 | m->udev = udev_new(); |
747 | if (!m->udev) | |
748 | return -ENOMEM; | |
752b5905 | 749 | |
e8112e67 | 750 | r = sd_event_default(&m->event); |
8742514c | 751 | if (r < 0) |
c70cac54 | 752 | return r; |
9152c765 | 753 | |
e8112e67 | 754 | r = manager_setup_run_queue(m); |
a1d32bac | 755 | if (r < 0) |
c70cac54 | 756 | return r; |
8e274523 | 757 | |
e8112e67 ZJS |
758 | if (test_run_flags == MANAGER_TEST_RUN_MINIMAL) { |
759 | m->cgroup_root = strdup(""); | |
760 | if (!m->cgroup_root) | |
761 | return -ENOMEM; | |
762 | } else { | |
763 | r = manager_setup_signals(m); | |
764 | if (r < 0) | |
765 | return r; | |
8c47c732 | 766 | |
e8112e67 ZJS |
767 | r = manager_setup_cgroup(m); |
768 | if (r < 0) | |
769 | return r; | |
575b300b | 770 | |
e8112e67 ZJS |
771 | r = manager_setup_time_change(m); |
772 | if (r < 0) | |
773 | return r; | |
9670d583 | 774 | |
e8112e67 ZJS |
775 | r = manager_setup_sigchld_event_source(m); |
776 | if (r < 0) | |
777 | return r; | |
778 | } | |
e27fe688 | 779 | |
5eb397cf | 780 | if (MANAGER_IS_SYSTEM(m) && test_run_flags == 0) { |
d3070fbd LP |
781 | r = mkdir_label("/run/systemd/units", 0755); |
782 | if (r < 0 && r != -EEXIST) | |
c70cac54 | 783 | return r; |
d3070fbd LP |
784 | } |
785 | ||
e27fe688 LP |
786 | m->taint_usr = |
787 | !in_initrd() && | |
788 | dir_is_empty("/usr") > 0; | |
789 | ||
232f6754 ZJS |
790 | /* Note that we do not set up the notify fd here. We do that after deserialization, |
791 | * since they might have gotten serialized across the reexec. */ | |
3536f49e | 792 | |
1cc6c93a YW |
793 | *_m = TAKE_PTR(m); |
794 | ||
8e274523 | 795 | return 0; |
60918275 LP |
796 | } |
797 | ||
d86f9d52 | 798 | static int manager_setup_notify(Manager *m) { |
7181dbdb | 799 | int r; |
d86f9d52 | 800 | |
e0a3da1f | 801 | if (m->test_run_flags) |
0d8c31ff ZJS |
802 | return 0; |
803 | ||
d86f9d52 LP |
804 | if (m->notify_fd < 0) { |
805 | _cleanup_close_ int fd = -1; | |
920b52e4 | 806 | union sockaddr_union sa = { |
7181dbdb LP |
807 | .sa.sa_family = AF_UNIX, |
808 | }; | |
55836941 | 809 | static const int one = 1; |
d86f9d52 LP |
810 | |
811 | /* First free all secondary fields */ | |
a1e58e8e | 812 | m->notify_socket = mfree(m->notify_socket); |
d86f9d52 LP |
813 | m->notify_event_source = sd_event_source_unref(m->notify_event_source); |
814 | ||
815 | fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0); | |
4a62c710 MS |
816 | if (fd < 0) |
817 | return log_error_errno(errno, "Failed to allocate notification socket: %m"); | |
d86f9d52 | 818 | |
a47806fa LP |
819 | fd_inc_rcvbuf(fd, NOTIFY_RCVBUF_SIZE); |
820 | ||
3536f49e | 821 | m->notify_socket = strappend(m->prefix[EXEC_DIRECTORY_RUNTIME], "/systemd/notify"); |
498e87d6 LP |
822 | if (!m->notify_socket) |
823 | return log_oom(); | |
824 | ||
825 | (void) mkdir_parents_label(m->notify_socket, 0755); | |
f0e62e89 | 826 | (void) unlink(m->notify_socket); |
7181dbdb LP |
827 | |
828 | strncpy(sa.un.sun_path, m->notify_socket, sizeof(sa.un.sun_path)-1); | |
fc2fffe7 | 829 | r = bind(fd, &sa.sa, SOCKADDR_UN_LEN(sa.un)); |
4a62c710 MS |
830 | if (r < 0) |
831 | return log_error_errno(errno, "bind(%s) failed: %m", sa.un.sun_path); | |
d86f9d52 LP |
832 | |
833 | r = setsockopt(fd, SOL_SOCKET, SO_PASSCRED, &one, sizeof(one)); | |
4a62c710 MS |
834 | if (r < 0) |
835 | return log_error_errno(errno, "SO_PASSCRED failed: %m"); | |
d86f9d52 | 836 | |
c10d6bdb | 837 | m->notify_fd = TAKE_FD(fd); |
d86f9d52 LP |
838 | |
839 | log_debug("Using notification socket %s", m->notify_socket); | |
840 | } | |
841 | ||
842 | if (!m->notify_event_source) { | |
151b9b96 | 843 | r = sd_event_add_io(m->event, &m->notify_event_source, m->notify_fd, EPOLLIN, manager_dispatch_notify_fd, m); |
895b3a7b MS |
844 | if (r < 0) |
845 | return log_error_errno(r, "Failed to allocate notify event source: %m"); | |
d86f9d52 | 846 | |
d8fdc620 LP |
847 | /* Process notification messages a bit earlier than SIGCHLD, so that we can still identify to which |
848 | * service an exit message belongs. */ | |
575b300b | 849 | r = sd_event_source_set_priority(m->notify_event_source, SD_EVENT_PRIORITY_NORMAL-8); |
23bbb0de MS |
850 | if (r < 0) |
851 | return log_error_errno(r, "Failed to set priority of notify event source: %m"); | |
7dfbe2e3 TG |
852 | |
853 | (void) sd_event_source_set_description(m->notify_event_source, "manager-notify"); | |
d86f9d52 LP |
854 | } |
855 | ||
856 | return 0; | |
857 | } | |
858 | ||
d8fdc620 LP |
859 | static int manager_setup_cgroups_agent(Manager *m) { |
860 | ||
861 | static const union sockaddr_union sa = { | |
862 | .un.sun_family = AF_UNIX, | |
863 | .un.sun_path = "/run/systemd/cgroups-agent", | |
864 | }; | |
865 | int r; | |
866 | ||
867 | /* This creates a listening socket we receive cgroups agent messages on. We do not use D-Bus for delivering | |
868 | * these messages from the cgroups agent binary to PID 1, as the cgroups agent binary is very short-living, and | |
869 | * each instance of it needs a new D-Bus connection. Since D-Bus connections are SOCK_STREAM/AF_UNIX, on | |
870 | * overloaded systems the backlog of the D-Bus socket becomes relevant, as not more than the configured number | |
871 | * of D-Bus connections may be queued until the kernel will start dropping further incoming connections, | |
872 | * possibly resulting in lost cgroups agent messages. To avoid this, we'll use a private SOCK_DGRAM/AF_UNIX | |
873 | * socket, where no backlog is relevant as communication may take place without an actual connect() cycle, and | |
874 | * we thus won't lose messages. | |
875 | * | |
876 | * Note that PID 1 will forward the agent message to system bus, so that the user systemd instance may listen | |
877 | * to it. The system instance hence listens on this special socket, but the user instances listen on the system | |
878 | * bus for these messages. */ | |
879 | ||
e0a3da1f | 880 | if (m->test_run_flags) |
d8fdc620 LP |
881 | return 0; |
882 | ||
883 | if (!MANAGER_IS_SYSTEM(m)) | |
884 | return 0; | |
885 | ||
c22800e4 | 886 | r = cg_unified_controller(SYSTEMD_CGROUP_CONTROLLER); |
b4cccbc1 LP |
887 | if (r < 0) |
888 | return log_error_errno(r, "Failed to determine whether unified cgroups hierarchy is used: %m"); | |
889 | if (r > 0) /* We don't need this anymore on the unified hierarchy */ | |
d8fdc620 LP |
890 | return 0; |
891 | ||
892 | if (m->cgroups_agent_fd < 0) { | |
893 | _cleanup_close_ int fd = -1; | |
894 | ||
895 | /* First free all secondary fields */ | |
896 | m->cgroups_agent_event_source = sd_event_source_unref(m->cgroups_agent_event_source); | |
897 | ||
898 | fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0); | |
899 | if (fd < 0) | |
900 | return log_error_errno(errno, "Failed to allocate cgroups agent socket: %m"); | |
901 | ||
902 | fd_inc_rcvbuf(fd, CGROUPS_AGENT_RCVBUF_SIZE); | |
903 | ||
904 | (void) unlink(sa.un.sun_path); | |
905 | ||
906 | /* Only allow root to connect to this socket */ | |
907 | RUN_WITH_UMASK(0077) | |
fc2fffe7 | 908 | r = bind(fd, &sa.sa, SOCKADDR_UN_LEN(sa.un)); |
d8fdc620 LP |
909 | if (r < 0) |
910 | return log_error_errno(errno, "bind(%s) failed: %m", sa.un.sun_path); | |
911 | ||
912 | m->cgroups_agent_fd = fd; | |
913 | fd = -1; | |
914 | } | |
915 | ||
916 | if (!m->cgroups_agent_event_source) { | |
917 | r = sd_event_add_io(m->event, &m->cgroups_agent_event_source, m->cgroups_agent_fd, EPOLLIN, manager_dispatch_cgroups_agent_fd, m); | |
918 | if (r < 0) | |
919 | return log_error_errno(r, "Failed to allocate cgroups agent event source: %m"); | |
920 | ||
921 | /* Process cgroups notifications early, but after having processed service notification messages or | |
922 | * SIGCHLD signals, so that a cgroup running empty is always just the last safety net of notification, | |
923 | * and we collected the metadata the notification and SIGCHLD stuff offers first. Also see handling of | |
924 | * cgroup inotify for the unified cgroup stuff. */ | |
09e24654 | 925 | r = sd_event_source_set_priority(m->cgroups_agent_event_source, SD_EVENT_PRIORITY_NORMAL-4); |
d8fdc620 LP |
926 | if (r < 0) |
927 | return log_error_errno(r, "Failed to set priority of cgroups agent event source: %m"); | |
928 | ||
929 | (void) sd_event_source_set_description(m->cgroups_agent_event_source, "manager-cgroups-agent"); | |
930 | } | |
931 | ||
932 | return 0; | |
933 | } | |
934 | ||
00d9ef85 LP |
935 | static int manager_setup_user_lookup_fd(Manager *m) { |
936 | int r; | |
937 | ||
938 | assert(m); | |
939 | ||
940 | /* Set up the socket pair used for passing UID/GID resolution results from forked off processes to PID | |
941 | * 1. Background: we can't do name lookups (NSS) from PID 1, since it might involve IPC and thus activation, | |
942 | * and we might hence deadlock on ourselves. Hence we do all user/group lookups asynchronously from the forked | |
943 | * off processes right before executing the binaries to start. In order to be able to clean up any IPC objects | |
944 | * created by a unit (see RemoveIPC=) we need to know in PID 1 the used UID/GID of the executed processes, | |
945 | * hence we establish this communication channel so that forked off processes can pass their UID/GID | |
946 | * information back to PID 1. The forked off processes send their resolved UID/GID to PID 1 in a simple | |
947 | * datagram, along with their unit name, so that we can share one communication socket pair among all units for | |
948 | * this purpose. | |
949 | * | |
950 | * You might wonder why we need a communication channel for this that is independent of the usual notification | |
951 | * socket scheme (i.e. $NOTIFY_SOCKET). The primary difference is about trust: data sent via the $NOTIFY_SOCKET | |
952 | * channel is only accepted if it originates from the right unit and if reception was enabled for it. The user | |
953 | * lookup socket OTOH is only accessible by PID 1 and its children until they exec(), and always available. | |
954 | * | |
955 | * Note that this function is called under two circumstances: when we first initialize (in which case we | |
956 | * allocate both the socket pair and the event source to listen on it), and when we deserialize after a reload | |
957 | * (in which case the socket pair already exists but we still need to allocate the event source for it). */ | |
958 | ||
959 | if (m->user_lookup_fds[0] < 0) { | |
960 | ||
961 | /* Free all secondary fields */ | |
962 | safe_close_pair(m->user_lookup_fds); | |
963 | m->user_lookup_event_source = sd_event_source_unref(m->user_lookup_event_source); | |
964 | ||
965 | if (socketpair(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC, 0, m->user_lookup_fds) < 0) | |
966 | return log_error_errno(errno, "Failed to allocate user lookup socket: %m"); | |
967 | ||
968 | (void) fd_inc_rcvbuf(m->user_lookup_fds[0], NOTIFY_RCVBUF_SIZE); | |
969 | } | |
970 | ||
971 | if (!m->user_lookup_event_source) { | |
972 | r = sd_event_add_io(m->event, &m->user_lookup_event_source, m->user_lookup_fds[0], EPOLLIN, manager_dispatch_user_lookup_fd, m); | |
973 | if (r < 0) | |
974 | return log_error_errno(errno, "Failed to allocate user lookup event source: %m"); | |
975 | ||
976 | /* Process even earlier than the notify event source, so that we always know first about valid UID/GID | |
977 | * resolutions */ | |
67ae4e8d | 978 | r = sd_event_source_set_priority(m->user_lookup_event_source, SD_EVENT_PRIORITY_NORMAL-11); |
00d9ef85 LP |
979 | if (r < 0) |
980 | return log_error_errno(errno, "Failed to set priority ot user lookup event source: %m"); | |
981 | ||
982 | (void) sd_event_source_set_description(m->user_lookup_event_source, "user-lookup"); | |
983 | } | |
984 | ||
985 | return 0; | |
986 | } | |
987 | ||
23a177ef | 988 | static unsigned manager_dispatch_cleanup_queue(Manager *m) { |
595ed347 | 989 | Unit *u; |
23a177ef LP |
990 | unsigned n = 0; |
991 | ||
992 | assert(m); | |
993 | ||
595ed347 MS |
994 | while ((u = m->cleanup_queue)) { |
995 | assert(u->in_cleanup_queue); | |
23a177ef | 996 | |
595ed347 | 997 | unit_free(u); |
23a177ef LP |
998 | n++; |
999 | } | |
1000 | ||
1001 | return n; | |
1002 | } | |
1003 | ||
eced69b3 | 1004 | enum { |
35b8ca3a | 1005 | GC_OFFSET_IN_PATH, /* This one is on the path we were traveling */ |
eced69b3 LP |
1006 | GC_OFFSET_UNSURE, /* No clue */ |
1007 | GC_OFFSET_GOOD, /* We still need this unit */ | |
1008 | GC_OFFSET_BAD, /* We don't need this unit anymore */ | |
1009 | _GC_OFFSET_MAX | |
1010 | }; | |
1011 | ||
00d9ef85 | 1012 | static void unit_gc_mark_good(Unit *u, unsigned gc_marker) { |
4892084f | 1013 | Unit *other; |
eef85c4a LP |
1014 | Iterator i; |
1015 | void *v; | |
4892084f LN |
1016 | |
1017 | u->gc_marker = gc_marker + GC_OFFSET_GOOD; | |
1018 | ||
1019 | /* Recursively mark referenced units as GOOD as well */ | |
eef85c4a | 1020 | HASHMAP_FOREACH_KEY(v, other, u->dependencies[UNIT_REFERENCES], i) |
4892084f LN |
1021 | if (other->gc_marker == gc_marker + GC_OFFSET_UNSURE) |
1022 | unit_gc_mark_good(other, gc_marker); | |
1023 | } | |
1024 | ||
eced69b3 | 1025 | static void unit_gc_sweep(Unit *u, unsigned gc_marker) { |
701cc384 | 1026 | Unit *other; |
eced69b3 | 1027 | bool is_bad; |
eef85c4a LP |
1028 | Iterator i; |
1029 | void *v; | |
701cc384 LP |
1030 | |
1031 | assert(u); | |
1032 | ||
4c701096 YW |
1033 | if (IN_SET(u->gc_marker - gc_marker, |
1034 | GC_OFFSET_GOOD, GC_OFFSET_BAD, GC_OFFSET_UNSURE, GC_OFFSET_IN_PATH)) | |
701cc384 LP |
1035 | return; |
1036 | ||
ac155bb8 | 1037 | if (u->in_cleanup_queue) |
701cc384 LP |
1038 | goto bad; |
1039 | ||
f2f725e5 | 1040 | if (!unit_may_gc(u)) |
701cc384 LP |
1041 | goto good; |
1042 | ||
ac155bb8 | 1043 | u->gc_marker = gc_marker + GC_OFFSET_IN_PATH; |
eced69b3 LP |
1044 | |
1045 | is_bad = true; | |
1046 | ||
eef85c4a | 1047 | HASHMAP_FOREACH_KEY(v, other, u->dependencies[UNIT_REFERENCED_BY], i) { |
701cc384 LP |
1048 | unit_gc_sweep(other, gc_marker); |
1049 | ||
ac155bb8 | 1050 | if (other->gc_marker == gc_marker + GC_OFFSET_GOOD) |
701cc384 | 1051 | goto good; |
eced69b3 | 1052 | |
ac155bb8 | 1053 | if (other->gc_marker != gc_marker + GC_OFFSET_BAD) |
eced69b3 | 1054 | is_bad = false; |
701cc384 LP |
1055 | } |
1056 | ||
2641f02e ZJS |
1057 | if (u->refs_by_target) { |
1058 | const UnitRef *ref; | |
1059 | ||
1060 | LIST_FOREACH(refs_by_target, ref, u->refs_by_target) { | |
1061 | unit_gc_sweep(ref->source, gc_marker); | |
1062 | ||
1063 | if (ref->source->gc_marker == gc_marker + GC_OFFSET_GOOD) | |
1064 | goto good; | |
1065 | ||
1066 | if (ref->source->gc_marker != gc_marker + GC_OFFSET_BAD) | |
1067 | is_bad = false; | |
1068 | } | |
1069 | } | |
701cc384 | 1070 | |
eced69b3 LP |
1071 | if (is_bad) |
1072 | goto bad; | |
1073 | ||
1074 | /* We were unable to find anything out about this entry, so | |
1075 | * let's investigate it later */ | |
ac155bb8 | 1076 | u->gc_marker = gc_marker + GC_OFFSET_UNSURE; |
eced69b3 LP |
1077 | unit_add_to_gc_queue(u); |
1078 | return; | |
1079 | ||
701cc384 | 1080 | bad: |
eced69b3 LP |
1081 | /* We definitely know that this one is not useful anymore, so |
1082 | * let's mark it for deletion */ | |
ac155bb8 | 1083 | u->gc_marker = gc_marker + GC_OFFSET_BAD; |
eced69b3 | 1084 | unit_add_to_cleanup_queue(u); |
701cc384 LP |
1085 | return; |
1086 | ||
1087 | good: | |
4892084f | 1088 | unit_gc_mark_good(u, gc_marker); |
701cc384 LP |
1089 | } |
1090 | ||
c5a97ed1 LP |
1091 | static unsigned manager_dispatch_gc_unit_queue(Manager *m) { |
1092 | unsigned n = 0, gc_marker; | |
595ed347 | 1093 | Unit *u; |
701cc384 LP |
1094 | |
1095 | assert(m); | |
1096 | ||
cf1265e1 | 1097 | /* log_debug("Running GC..."); */ |
701cc384 | 1098 | |
eced69b3 LP |
1099 | m->gc_marker += _GC_OFFSET_MAX; |
1100 | if (m->gc_marker + _GC_OFFSET_MAX <= _GC_OFFSET_MAX) | |
c9c0cadb | 1101 | m->gc_marker = 1; |
701cc384 | 1102 | |
eced69b3 LP |
1103 | gc_marker = m->gc_marker; |
1104 | ||
c5a97ed1 | 1105 | while ((u = m->gc_unit_queue)) { |
595ed347 | 1106 | assert(u->in_gc_queue); |
701cc384 | 1107 | |
595ed347 | 1108 | unit_gc_sweep(u, gc_marker); |
eced69b3 | 1109 | |
c5a97ed1 | 1110 | LIST_REMOVE(gc_queue, m->gc_unit_queue, u); |
595ed347 | 1111 | u->in_gc_queue = false; |
701cc384 LP |
1112 | |
1113 | n++; | |
1114 | ||
4c701096 YW |
1115 | if (IN_SET(u->gc_marker - gc_marker, |
1116 | GC_OFFSET_BAD, GC_OFFSET_UNSURE)) { | |
cc3bc3e6 | 1117 | if (u->id) |
f2341e0a | 1118 | log_unit_debug(u, "Collecting."); |
595ed347 MS |
1119 | u->gc_marker = gc_marker + GC_OFFSET_BAD; |
1120 | unit_add_to_cleanup_queue(u); | |
701cc384 LP |
1121 | } |
1122 | } | |
1123 | ||
701cc384 LP |
1124 | return n; |
1125 | } | |
1126 | ||
c5a97ed1 LP |
1127 | static unsigned manager_dispatch_gc_job_queue(Manager *m) { |
1128 | unsigned n = 0; | |
1129 | Job *j; | |
1130 | ||
1131 | assert(m); | |
1132 | ||
1133 | while ((j = m->gc_job_queue)) { | |
1134 | assert(j->in_gc_queue); | |
1135 | ||
1136 | LIST_REMOVE(gc_queue, m->gc_job_queue, j); | |
1137 | j->in_gc_queue = false; | |
1138 | ||
1139 | n++; | |
1140 | ||
2ab3050f | 1141 | if (!job_may_gc(j)) |
c5a97ed1 LP |
1142 | continue; |
1143 | ||
1144 | log_unit_debug(j->unit, "Collecting job."); | |
1145 | (void) job_finish_and_invalidate(j, JOB_COLLECTED, false, false); | |
1146 | } | |
1147 | ||
1148 | return n; | |
1149 | } | |
1150 | ||
a16e1123 | 1151 | static void manager_clear_jobs_and_units(Manager *m) { |
a16e1123 | 1152 | Unit *u; |
60918275 LP |
1153 | |
1154 | assert(m); | |
1155 | ||
87f0e418 LP |
1156 | while ((u = hashmap_first(m->units))) |
1157 | unit_free(u); | |
964e0949 LP |
1158 | |
1159 | manager_dispatch_cleanup_queue(m); | |
1160 | ||
1161 | assert(!m->load_queue); | |
1162 | assert(!m->run_queue); | |
1163 | assert(!m->dbus_unit_queue); | |
1164 | assert(!m->dbus_job_queue); | |
1165 | assert(!m->cleanup_queue); | |
c5a97ed1 LP |
1166 | assert(!m->gc_unit_queue); |
1167 | assert(!m->gc_job_queue); | |
964e0949 | 1168 | |
964e0949 LP |
1169 | assert(hashmap_isempty(m->jobs)); |
1170 | assert(hashmap_isempty(m->units)); | |
9e9e2b72 MS |
1171 | |
1172 | m->n_on_console = 0; | |
1173 | m->n_running_jobs = 0; | |
a16e1123 LP |
1174 | } |
1175 | ||
06d8d842 | 1176 | Manager* manager_free(Manager *m) { |
a16e1123 | 1177 | UnitType c; |
35aba85a | 1178 | ExecDirectoryType dt; |
87f0e418 | 1179 | |
06d8d842 ZJS |
1180 | if (!m) |
1181 | return NULL; | |
a16e1123 LP |
1182 | |
1183 | manager_clear_jobs_and_units(m); | |
23a177ef | 1184 | |
7824bbeb LP |
1185 | for (c = 0; c < _UNIT_TYPE_MAX; c++) |
1186 | if (unit_vtable[c]->shutdown) | |
1187 | unit_vtable[c]->shutdown(m); | |
1188 | ||
a1f31f47 | 1189 | /* If we reexecute ourselves, we keep the root cgroup around */ |
c6c18be3 | 1190 | manager_shutdown_cgroup(m, m->exit_code != MANAGER_REEXECUTE); |
8e274523 | 1191 | |
07a78643 | 1192 | lookup_paths_flush_generator(&m->lookup_paths); |
5a1e9937 | 1193 | |
5e8d1c9a | 1194 | bus_done(m); |
ea430986 | 1195 | |
e8a565cb YW |
1196 | exec_runtime_vacuum(m); |
1197 | hashmap_free(m->exec_runtime_by_id); | |
1198 | ||
29206d46 LP |
1199 | dynamic_user_vacuum(m, false); |
1200 | hashmap_free(m->dynamic_users); | |
1201 | ||
87f0e418 | 1202 | hashmap_free(m->units); |
4b58153d | 1203 | hashmap_free(m->units_by_invocation_id); |
60918275 | 1204 | hashmap_free(m->jobs); |
62a76913 | 1205 | hashmap_free(m->watch_pids); |
05e343b7 | 1206 | hashmap_free(m->watch_bus); |
9152c765 | 1207 | |
95ae05c0 | 1208 | set_free(m->startup_units); |
f755e3b7 LP |
1209 | set_free(m->failed_units); |
1210 | ||
718db961 | 1211 | sd_event_source_unref(m->signal_event_source); |
575b300b | 1212 | sd_event_source_unref(m->sigchld_event_source); |
718db961 | 1213 | sd_event_source_unref(m->notify_event_source); |
d8fdc620 | 1214 | sd_event_source_unref(m->cgroups_agent_event_source); |
718db961 LP |
1215 | sd_event_source_unref(m->time_change_event_source); |
1216 | sd_event_source_unref(m->jobs_in_progress_event_source); | |
752b5905 | 1217 | sd_event_source_unref(m->run_queue_event_source); |
00d9ef85 | 1218 | sd_event_source_unref(m->user_lookup_event_source); |
5f109056 | 1219 | sd_event_source_unref(m->sync_bus_names_event_source); |
718db961 | 1220 | |
03e334a1 LP |
1221 | safe_close(m->signal_fd); |
1222 | safe_close(m->notify_fd); | |
d8fdc620 | 1223 | safe_close(m->cgroups_agent_fd); |
03e334a1 | 1224 | safe_close(m->time_change_fd); |
00d9ef85 | 1225 | safe_close_pair(m->user_lookup_fds); |
718db961 | 1226 | |
e46b13c8 ZJS |
1227 | manager_close_ask_password(m); |
1228 | ||
718db961 LP |
1229 | manager_close_idle_pipe(m); |
1230 | ||
9670d583 | 1231 | udev_unref(m->udev); |
718db961 | 1232 | sd_event_unref(m->event); |
60918275 | 1233 | |
c952c6ec LP |
1234 | free(m->notify_socket); |
1235 | ||
84e3543e | 1236 | lookup_paths_free(&m->lookup_paths); |
1137a57c | 1237 | strv_free(m->environment); |
036643a2 | 1238 | |
4ad49000 | 1239 | hashmap_free(m->cgroup_unit); |
c6c18be3 | 1240 | set_free_free(m->unit_path_cache); |
33be102a | 1241 | |
664f88a7 LP |
1242 | free(m->switch_root); |
1243 | free(m->switch_root_init); | |
1244 | ||
31ce987c | 1245 | rlimit_free_all(m->rlimit); |
c93ff2e9 | 1246 | |
a57f7e2c LP |
1247 | assert(hashmap_isempty(m->units_requiring_mounts_for)); |
1248 | hashmap_free(m->units_requiring_mounts_for); | |
1249 | ||
00d9ef85 LP |
1250 | hashmap_free(m->uid_refs); |
1251 | hashmap_free(m->gid_refs); | |
1252 | ||
72fd1768 | 1253 | for (dt = 0; dt < _EXEC_DIRECTORY_TYPE_MAX; dt++) |
35aba85a YW |
1254 | m->prefix[dt] = mfree(m->prefix[dt]); |
1255 | ||
6b430fdb | 1256 | return mfree(m); |
60918275 LP |
1257 | } |
1258 | ||
a1113e08 | 1259 | static void manager_enumerate(Manager *m) { |
f50e0a01 | 1260 | UnitType c; |
f50e0a01 LP |
1261 | |
1262 | assert(m); | |
1263 | ||
a16e1123 LP |
1264 | /* Let's ask every type to load all units from disk/kernel |
1265 | * that it might know */ | |
0faacd47 | 1266 | for (c = 0; c < _UNIT_TYPE_MAX; c++) { |
1c2e9646 | 1267 | if (!unit_type_supported(c)) { |
03afec3c | 1268 | log_debug("Unit type .%s is not supported on this system.", unit_type_to_string(c)); |
0faacd47 | 1269 | continue; |
a57f7e2c | 1270 | } |
f50e0a01 | 1271 | |
94b01dae ZJS |
1272 | if (unit_vtable[c]->enumerate) |
1273 | unit_vtable[c]->enumerate(m); | |
0faacd47 LP |
1274 | } |
1275 | ||
f50e0a01 | 1276 | manager_dispatch_load_queue(m); |
a16e1123 LP |
1277 | } |
1278 | ||
007c6337 | 1279 | static void manager_coldplug(Manager *m) { |
a16e1123 LP |
1280 | Iterator i; |
1281 | Unit *u; | |
1282 | char *k; | |
007c6337 | 1283 | int r; |
a16e1123 LP |
1284 | |
1285 | assert(m); | |
f50e0a01 LP |
1286 | |
1287 | /* Then, let's set up their initial state. */ | |
1288 | HASHMAP_FOREACH_KEY(u, k, m->units, i) { | |
1289 | ||
1290 | /* ignore aliases */ | |
ac155bb8 | 1291 | if (u->id != k) |
f50e0a01 LP |
1292 | continue; |
1293 | ||
007c6337 LP |
1294 | r = unit_coldplug(u); |
1295 | if (r < 0) | |
1296 | log_warning_errno(r, "We couldn't coldplug %s, proceeding anyway: %m", u->id); | |
f50e0a01 | 1297 | } |
a16e1123 LP |
1298 | } |
1299 | ||
fe51822e LP |
1300 | static void manager_build_unit_path_cache(Manager *m) { |
1301 | char **i; | |
fe51822e LP |
1302 | int r; |
1303 | ||
1304 | assert(m); | |
1305 | ||
1306 | set_free_free(m->unit_path_cache); | |
1307 | ||
548f6937 | 1308 | m->unit_path_cache = set_new(&path_hash_ops); |
874310b7 | 1309 | if (!m->unit_path_cache) { |
d063a527 LP |
1310 | r = -ENOMEM; |
1311 | goto fail; | |
fe51822e LP |
1312 | } |
1313 | ||
1314 | /* This simply builds a list of files we know exist, so that | |
1315 | * we don't always have to go to disk */ | |
1316 | ||
a3c4eb07 | 1317 | STRV_FOREACH(i, m->lookup_paths.search_path) { |
d063a527 | 1318 | _cleanup_closedir_ DIR *d = NULL; |
fe51822e LP |
1319 | struct dirent *de; |
1320 | ||
bd0af849 ZJS |
1321 | d = opendir(*i); |
1322 | if (!d) { | |
874310b7 | 1323 | if (errno != ENOENT) |
d063a527 | 1324 | log_warning_errno(errno, "Failed to open directory %s, ignoring: %m", *i); |
fe51822e LP |
1325 | continue; |
1326 | } | |
1327 | ||
d063a527 | 1328 | FOREACH_DIRENT(de, d, r = -errno; goto fail) { |
fe51822e LP |
1329 | char *p; |
1330 | ||
605405c6 | 1331 | p = strjoin(streq(*i, "/") ? "" : *i, "/", de->d_name); |
44d91056 | 1332 | if (!p) { |
fe51822e LP |
1333 | r = -ENOMEM; |
1334 | goto fail; | |
1335 | } | |
1336 | ||
ef42202a ZJS |
1337 | r = set_consume(m->unit_path_cache, p); |
1338 | if (r < 0) | |
fe51822e | 1339 | goto fail; |
fe51822e | 1340 | } |
fe51822e LP |
1341 | } |
1342 | ||
1343 | return; | |
1344 | ||
1345 | fail: | |
d063a527 LP |
1346 | log_warning_errno(r, "Failed to build unit path cache, proceeding without: %m"); |
1347 | m->unit_path_cache = set_free_free(m->unit_path_cache); | |
fe51822e LP |
1348 | } |
1349 | ||
9ff1a6f1 | 1350 | static void manager_distribute_fds(Manager *m, FDSet *fds) { |
9588bc32 | 1351 | Iterator i; |
9ff1a6f1 | 1352 | Unit *u; |
9588bc32 LP |
1353 | |
1354 | assert(m); | |
1355 | ||
1356 | HASHMAP_FOREACH(u, m->units, i) { | |
1357 | ||
1358 | if (fdset_size(fds) <= 0) | |
1359 | break; | |
1360 | ||
9ff1a6f1 LP |
1361 | if (!UNIT_VTABLE(u)->distribute_fds) |
1362 | continue; | |
9588bc32 | 1363 | |
9ff1a6f1 LP |
1364 | UNIT_VTABLE(u)->distribute_fds(u, fds); |
1365 | } | |
9588bc32 LP |
1366 | } |
1367 | ||
8559b3b7 LP |
1368 | static bool manager_dbus_is_running(Manager *m, bool deserialized) { |
1369 | Unit *u; | |
1370 | ||
1371 | assert(m); | |
1372 | ||
1373 | /* This checks whether the dbus instance we are supposed to expose our APIs on is up. We check both the socket | |
1374 | * and the service unit. If the 'deserialized' parameter is true we'll check the deserialized state of the unit | |
1375 | * rather than the current one. */ | |
1376 | ||
1377 | if (m->test_run_flags != 0) | |
1378 | return false; | |
1379 | ||
1380 | /* If we are in the user instance, and the env var is already set for us, then this means D-Bus is ran | |
1381 | * somewhere outside of our own logic. Let's use it */ | |
1382 | if (MANAGER_IS_USER(m) && getenv("DBUS_SESSION_BUS_ADDRESS")) | |
1383 | return true; | |
1384 | ||
1385 | u = manager_get_unit(m, SPECIAL_DBUS_SOCKET); | |
1386 | if (!u) | |
1387 | return false; | |
1388 | if ((deserialized ? SOCKET(u)->deserialized_state : SOCKET(u)->state) != SOCKET_RUNNING) | |
1389 | return false; | |
1390 | ||
1391 | u = manager_get_unit(m, SPECIAL_DBUS_SERVICE); | |
1392 | if (!u) | |
1393 | return false; | |
1394 | if (!IN_SET((deserialized ? SERVICE(u)->deserialized_state : SERVICE(u)->state), SERVICE_RUNNING, SERVICE_RELOAD)) | |
1395 | return false; | |
1396 | ||
1397 | return true; | |
1398 | } | |
1399 | ||
a16e1123 | 1400 | int manager_startup(Manager *m, FILE *serialization, FDSet *fds) { |
17f01ace | 1401 | int r; |
a16e1123 LP |
1402 | |
1403 | assert(m); | |
1404 | ||
a1f31f47 ZJS |
1405 | /* If we are running in test mode, we still want to run the generators, |
1406 | * but we should not touch the real generator directories. */ | |
1407 | r = lookup_paths_init(&m->lookup_paths, m->unit_file_scope, | |
e0a3da1f | 1408 | m->test_run_flags ? LOOKUP_PATHS_TEMPORARY_GENERATED : 0, |
a1f31f47 | 1409 | NULL); |
e801700e ZJS |
1410 | if (r < 0) |
1411 | return r; | |
5a1e9937 | 1412 | |
64691d20 ZJS |
1413 | r = manager_run_environment_generators(m); |
1414 | if (r < 0) | |
1415 | return r; | |
1416 | ||
9f9f0342 | 1417 | dual_timestamp_get(m->timestamps + MANAGER_TIMESTAMP_GENERATORS_START); |
a3c4eb07 | 1418 | r = manager_run_generators(m); |
9f9f0342 | 1419 | dual_timestamp_get(m->timestamps + MANAGER_TIMESTAMP_GENERATORS_FINISH); |
07719a21 LP |
1420 | if (r < 0) |
1421 | return r; | |
1422 | ||
fd130612 | 1423 | /* If this is the first boot, and we are in the host system, then preset everything */ |
81fe6cde | 1424 | if (m->first_boot > 0 && |
fd130612 | 1425 | MANAGER_IS_SYSTEM(m) && |
e0a3da1f | 1426 | !m->test_run_flags) { |
81fe6cde | 1427 | |
17f01ace ZJS |
1428 | r = unit_file_preset_all(UNIT_FILE_SYSTEM, 0, NULL, UNIT_FILE_PRESET_ENABLE_ONLY, NULL, 0); |
1429 | if (r < 0) | |
1430 | log_full_errno(r == -EEXIST ? LOG_NOTICE : LOG_WARNING, r, | |
1431 | "Failed to populate /etc with preset unit settings, ignoring: %m"); | |
28dd66ec LB |
1432 | else |
1433 | log_info("Populated /etc with preset unit settings."); | |
1434 | } | |
1435 | ||
a1453343 | 1436 | lookup_paths_reduce(&m->lookup_paths); |
fe51822e LP |
1437 | manager_build_unit_path_cache(m); |
1438 | ||
9f611ad8 LP |
1439 | /* If we will deserialize make sure that during enumeration |
1440 | * this is already known, so we increase the counter here | |
1441 | * already */ | |
1442 | if (serialization) | |
313cefa1 | 1443 | m->n_reloading++; |
9f611ad8 | 1444 | |
a16e1123 | 1445 | /* First, enumerate what we can from all config files */ |
9f9f0342 | 1446 | dual_timestamp_get(m->timestamps + MANAGER_TIMESTAMP_UNITS_LOAD_START); |
ba64af90 | 1447 | manager_enumerate(m); |
9f9f0342 | 1448 | dual_timestamp_get(m->timestamps + MANAGER_TIMESTAMP_UNITS_LOAD_FINISH); |
a16e1123 LP |
1449 | |
1450 | /* Second, deserialize if there is something to deserialize */ | |
07429866 | 1451 | if (serialization) { |
1cd974ed | 1452 | r = manager_deserialize(m, serialization, fds); |
07429866 | 1453 | if (r < 0) |
17f01ace | 1454 | return log_error_errno(r, "Deserialization failed: %m"); |
07429866 | 1455 | } |
a16e1123 | 1456 | |
01e10de3 LP |
1457 | /* Any fds left? Find some unit which wants them. This is |
1458 | * useful to allow container managers to pass some file | |
1459 | * descriptors to us pre-initialized. This enables | |
1460 | * socket-based activation of entire containers. */ | |
9ff1a6f1 | 1461 | manager_distribute_fds(m, fds); |
01e10de3 | 1462 | |
d86f9d52 LP |
1463 | /* We might have deserialized the notify fd, but if we didn't |
1464 | * then let's create the bus now */ | |
17f01ace ZJS |
1465 | r = manager_setup_notify(m); |
1466 | if (r < 0) | |
1467 | /* No sense to continue without notifications, our children would fail anyway. */ | |
1468 | return r; | |
d86f9d52 | 1469 | |
17f01ace ZJS |
1470 | r = manager_setup_cgroups_agent(m); |
1471 | if (r < 0) | |
1472 | /* Likewise, no sense to continue without empty cgroup notifications. */ | |
1473 | return r; | |
d8fdc620 | 1474 | |
17f01ace ZJS |
1475 | r = manager_setup_user_lookup_fd(m); |
1476 | if (r < 0) | |
1477 | /* This shouldn't fail, except if things are really broken. */ | |
1478 | return r; | |
00d9ef85 | 1479 | |
8559b3b7 LP |
1480 | /* Let's set up our private bus connection now, unconditionally */ |
1481 | (void) bus_init_private(m); | |
05a98afd | 1482 | |
8559b3b7 LP |
1483 | /* If we are in --user mode also connect to the system bus now */ |
1484 | if (MANAGER_IS_USER(m)) | |
1485 | (void) bus_init_system(m); | |
1486 | ||
1487 | /* Let's connect to the bus now, but only if the unit is supposed to be up */ | |
1488 | if (manager_dbus_is_running(m, !!serialization)) { | |
1489 | (void) bus_init_api(m); | |
05a98afd | 1490 | |
8559b3b7 LP |
1491 | if (MANAGER_IS_SYSTEM(m)) |
1492 | (void) bus_init_system(m); | |
1493 | } | |
1494 | ||
1495 | /* Now that we are connected to all possible busses, let's deserialize who is tracking us. */ | |
05a98afd LP |
1496 | (void) bus_track_coldplug(m, &m->subscribed, false, m->deserialized_subscribed); |
1497 | m->deserialized_subscribed = strv_free(m->deserialized_subscribed); | |
e3dd987c | 1498 | |
a16e1123 | 1499 | /* Third, fire things up! */ |
007c6337 | 1500 | manager_coldplug(m); |
a16e1123 | 1501 | |
29206d46 LP |
1502 | /* Release any dynamic users no longer referenced */ |
1503 | dynamic_user_vacuum(m, true); | |
1504 | ||
e8a565cb YW |
1505 | exec_runtime_vacuum(m); |
1506 | ||
00d9ef85 LP |
1507 | /* Release any references to UIDs/GIDs no longer referenced, and destroy any IPC owned by them */ |
1508 | manager_vacuum_uid_refs(m); | |
1509 | manager_vacuum_gid_refs(m); | |
1510 | ||
9f611ad8 | 1511 | if (serialization) { |
a7556052 | 1512 | assert(m->n_reloading > 0); |
313cefa1 | 1513 | m->n_reloading--; |
71445ae7 LP |
1514 | |
1515 | /* Let's wait for the UnitNew/JobNew messages being | |
1516 | * sent, before we notify that the reload is | |
1517 | * finished */ | |
1518 | m->send_reloading_done = true; | |
9f611ad8 LP |
1519 | } |
1520 | ||
17f01ace | 1521 | return 0; |
f50e0a01 LP |
1522 | } |
1523 | ||
4bd29fe5 | 1524 | int manager_add_job(Manager *m, JobType type, Unit *unit, JobMode mode, sd_bus_error *e, Job **_ret) { |
e5b5ae50 | 1525 | int r; |
7527cb52 | 1526 | Transaction *tr; |
e5b5ae50 LP |
1527 | |
1528 | assert(m); | |
1529 | assert(type < _JOB_TYPE_MAX); | |
87f0e418 | 1530 | assert(unit); |
e5b5ae50 | 1531 | assert(mode < _JOB_MODE_MAX); |
60918275 | 1532 | |
7358dc02 ZJS |
1533 | if (mode == JOB_ISOLATE && type != JOB_START) |
1534 | return sd_bus_error_setf(e, SD_BUS_ERROR_INVALID_ARGS, "Isolate is only valid for start."); | |
c497c7a9 | 1535 | |
7358dc02 ZJS |
1536 | if (mode == JOB_ISOLATE && !unit->allow_isolate) |
1537 | return sd_bus_error_setf(e, BUS_ERROR_NO_ISOLATION, "Operation refused, unit may not be isolated."); | |
2528a7a6 | 1538 | |
f2341e0a | 1539 | log_unit_debug(unit, "Trying to enqueue job %s/%s/%s", unit->id, job_type_to_string(type), job_mode_to_string(mode)); |
9f04bd52 | 1540 | |
c6497ccb | 1541 | type = job_type_collapse(type, unit); |
e0209d83 | 1542 | |
23ade460 | 1543 | tr = transaction_new(mode == JOB_REPLACE_IRREVERSIBLY); |
7527cb52 MS |
1544 | if (!tr) |
1545 | return -ENOMEM; | |
11dd41ce | 1546 | |
4bd29fe5 | 1547 | r = transaction_add_job_and_dependencies(tr, type, unit, NULL, true, false, |
3742095b | 1548 | IN_SET(mode, JOB_IGNORE_DEPENDENCIES, JOB_IGNORE_REQUIREMENTS), |
b94fbd30 | 1549 | mode == JOB_IGNORE_DEPENDENCIES, e); |
7527cb52 MS |
1550 | if (r < 0) |
1551 | goto tr_abort; | |
c497c7a9 | 1552 | |
7527cb52 MS |
1553 | if (mode == JOB_ISOLATE) { |
1554 | r = transaction_add_isolate_jobs(tr, m); | |
1555 | if (r < 0) | |
1556 | goto tr_abort; | |
1557 | } | |
1558 | ||
1559 | r = transaction_activate(tr, m, mode, e); | |
1560 | if (r < 0) | |
1561 | goto tr_abort; | |
e5b5ae50 | 1562 | |
f2341e0a | 1563 | log_unit_debug(unit, |
66870f90 ZJS |
1564 | "Enqueued job %s/%s as %u", unit->id, |
1565 | job_type_to_string(type), (unsigned) tr->anchor_job->id); | |
f50e0a01 | 1566 | |
e5b5ae50 | 1567 | if (_ret) |
b94fbd30 | 1568 | *_ret = tr->anchor_job; |
60918275 | 1569 | |
7527cb52 | 1570 | transaction_free(tr); |
e5b5ae50 | 1571 | return 0; |
7527cb52 MS |
1572 | |
1573 | tr_abort: | |
1574 | transaction_abort(tr); | |
1575 | transaction_free(tr); | |
1576 | return r; | |
e5b5ae50 | 1577 | } |
60918275 | 1578 | |
53f18416 | 1579 | int manager_add_job_by_name(Manager *m, JobType type, const char *name, JobMode mode, sd_bus_error *e, Job **ret) { |
4440b27d | 1580 | Unit *unit = NULL; /* just to appease gcc, initialization is not really necessary */ |
28247076 LP |
1581 | int r; |
1582 | ||
1583 | assert(m); | |
1584 | assert(type < _JOB_TYPE_MAX); | |
1585 | assert(name); | |
1586 | assert(mode < _JOB_MODE_MAX); | |
1587 | ||
c3090674 LP |
1588 | r = manager_load_unit(m, name, NULL, NULL, &unit); |
1589 | if (r < 0) | |
28247076 | 1590 | return r; |
4440b27d | 1591 | assert(unit); |
28247076 | 1592 | |
53f18416 LP |
1593 | return manager_add_job(m, type, unit, mode, e, ret); |
1594 | } | |
1595 | ||
1596 | int manager_add_job_by_name_and_warn(Manager *m, JobType type, const char *name, JobMode mode, Job **ret) { | |
4afd3348 | 1597 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
53f18416 LP |
1598 | int r; |
1599 | ||
1600 | assert(m); | |
1601 | assert(type < _JOB_TYPE_MAX); | |
1602 | assert(name); | |
1603 | assert(mode < _JOB_MODE_MAX); | |
1604 | ||
1605 | r = manager_add_job_by_name(m, type, name, mode, &error, ret); | |
1606 | if (r < 0) | |
1607 | return log_warning_errno(r, "Failed to enqueue %s job for %s: %s", job_mode_to_string(mode), name, bus_error_message(&error, r)); | |
1608 | ||
1609 | return r; | |
28247076 LP |
1610 | } |
1611 | ||
15d167f8 JW |
1612 | int manager_propagate_reload(Manager *m, Unit *unit, JobMode mode, sd_bus_error *e) { |
1613 | int r; | |
1614 | Transaction *tr; | |
1615 | ||
1616 | assert(m); | |
1617 | assert(unit); | |
1618 | assert(mode < _JOB_MODE_MAX); | |
1619 | assert(mode != JOB_ISOLATE); /* Isolate is only valid for start */ | |
1620 | ||
1621 | tr = transaction_new(mode == JOB_REPLACE_IRREVERSIBLY); | |
1622 | if (!tr) | |
1623 | return -ENOMEM; | |
1624 | ||
1625 | /* We need an anchor job */ | |
1626 | r = transaction_add_job_and_dependencies(tr, JOB_NOP, unit, NULL, false, false, true, true, e); | |
1627 | if (r < 0) | |
1628 | goto tr_abort; | |
1629 | ||
1630 | /* Failure in adding individual dependencies is ignored, so this always succeeds. */ | |
1631 | transaction_add_propagate_reload_jobs(tr, unit, tr->anchor_job, mode == JOB_IGNORE_DEPENDENCIES, e); | |
1632 | ||
1633 | r = transaction_activate(tr, m, mode, e); | |
1634 | if (r < 0) | |
1635 | goto tr_abort; | |
1636 | ||
1637 | transaction_free(tr); | |
1638 | return 0; | |
1639 | ||
1640 | tr_abort: | |
1641 | transaction_abort(tr); | |
1642 | transaction_free(tr); | |
1643 | return r; | |
1644 | } | |
1645 | ||
60918275 LP |
1646 | Job *manager_get_job(Manager *m, uint32_t id) { |
1647 | assert(m); | |
1648 | ||
1649 | return hashmap_get(m->jobs, UINT32_TO_PTR(id)); | |
1650 | } | |
1651 | ||
87f0e418 | 1652 | Unit *manager_get_unit(Manager *m, const char *name) { |
60918275 LP |
1653 | assert(m); |
1654 | assert(name); | |
1655 | ||
87f0e418 | 1656 | return hashmap_get(m->units, name); |
60918275 LP |
1657 | } |
1658 | ||
19496554 MS |
1659 | static int manager_dispatch_target_deps_queue(Manager *m) { |
1660 | Unit *u; | |
1661 | unsigned k; | |
1662 | int r = 0; | |
1663 | ||
1664 | static const UnitDependency deps[] = { | |
1665 | UNIT_REQUIRED_BY, | |
1666 | UNIT_REQUISITE_OF, | |
1667 | UNIT_WANTED_BY, | |
1668 | UNIT_BOUND_BY | |
1669 | }; | |
1670 | ||
1671 | assert(m); | |
1672 | ||
1673 | while ((u = m->target_deps_queue)) { | |
1674 | assert(u->in_target_deps_queue); | |
1675 | ||
1676 | LIST_REMOVE(target_deps_queue, u->manager->target_deps_queue, u); | |
1677 | u->in_target_deps_queue = false; | |
1678 | ||
1679 | for (k = 0; k < ELEMENTSOF(deps); k++) { | |
1680 | Unit *target; | |
1681 | Iterator i; | |
1682 | void *v; | |
1683 | ||
1684 | HASHMAP_FOREACH_KEY(v, target, u->dependencies[deps[k]], i) { | |
1685 | r = unit_add_default_target_dependency(u, target); | |
1686 | if (r < 0) | |
1687 | return r; | |
1688 | } | |
1689 | } | |
1690 | } | |
1691 | ||
1692 | return r; | |
1693 | } | |
1694 | ||
c1e1601e | 1695 | unsigned manager_dispatch_load_queue(Manager *m) { |
595ed347 | 1696 | Unit *u; |
c1e1601e | 1697 | unsigned n = 0; |
60918275 LP |
1698 | |
1699 | assert(m); | |
1700 | ||
223dabab LP |
1701 | /* Make sure we are not run recursively */ |
1702 | if (m->dispatching_load_queue) | |
c1e1601e | 1703 | return 0; |
223dabab LP |
1704 | |
1705 | m->dispatching_load_queue = true; | |
1706 | ||
87f0e418 | 1707 | /* Dispatches the load queue. Takes a unit from the queue and |
60918275 LP |
1708 | * tries to load its data until the queue is empty */ |
1709 | ||
595ed347 MS |
1710 | while ((u = m->load_queue)) { |
1711 | assert(u->in_load_queue); | |
034c6ed7 | 1712 | |
595ed347 | 1713 | unit_load(u); |
c1e1601e | 1714 | n++; |
60918275 LP |
1715 | } |
1716 | ||
223dabab | 1717 | m->dispatching_load_queue = false; |
19496554 MS |
1718 | |
1719 | /* Dispatch the units waiting for their target dependencies to be added now, as all targets that we know about | |
1720 | * should be loaded and have aliases resolved */ | |
1721 | (void) manager_dispatch_target_deps_queue(m); | |
1722 | ||
c1e1601e | 1723 | return n; |
60918275 LP |
1724 | } |
1725 | ||
c2756a68 LP |
1726 | int manager_load_unit_prepare( |
1727 | Manager *m, | |
1728 | const char *name, | |
1729 | const char *path, | |
718db961 | 1730 | sd_bus_error *e, |
c2756a68 LP |
1731 | Unit **_ret) { |
1732 | ||
dc409696 | 1733 | _cleanup_(unit_freep) Unit *cleanup_ret = NULL; |
87f0e418 | 1734 | Unit *ret; |
7d17cfbc | 1735 | UnitType t; |
60918275 LP |
1736 | int r; |
1737 | ||
1738 | assert(m); | |
9e2f7c11 | 1739 | assert(name || path); |
7a6a095a | 1740 | assert(_ret); |
60918275 | 1741 | |
db06e3b6 LP |
1742 | /* This will prepare the unit for loading, but not actually |
1743 | * load anything from disk. */ | |
0301abf4 | 1744 | |
718db961 LP |
1745 | if (path && !is_path(path)) |
1746 | return sd_bus_error_setf(e, SD_BUS_ERROR_INVALID_ARGS, "Path %s is not absolute.", path); | |
9e2f7c11 LP |
1747 | |
1748 | if (!name) | |
2b6bf07d | 1749 | name = basename(path); |
9e2f7c11 | 1750 | |
7d17cfbc MS |
1751 | t = unit_name_to_type(name); |
1752 | ||
5d512d54 LN |
1753 | if (t == _UNIT_TYPE_INVALID || !unit_name_is_valid(name, UNIT_NAME_PLAIN|UNIT_NAME_INSTANCE)) { |
1754 | if (unit_name_is_valid(name, UNIT_NAME_TEMPLATE)) | |
1755 | return sd_bus_error_setf(e, SD_BUS_ERROR_INVALID_ARGS, "Unit name %s is missing the instance name.", name); | |
1756 | ||
718db961 | 1757 | return sd_bus_error_setf(e, SD_BUS_ERROR_INVALID_ARGS, "Unit name %s is not valid.", name); |
5d512d54 | 1758 | } |
60918275 | 1759 | |
7d17cfbc MS |
1760 | ret = manager_get_unit(m, name); |
1761 | if (ret) { | |
034c6ed7 | 1762 | *_ret = ret; |
413d6313 | 1763 | return 1; |
034c6ed7 | 1764 | } |
60918275 | 1765 | |
dc409696 | 1766 | ret = cleanup_ret = unit_new(m, unit_vtable[t]->object_size); |
7d17cfbc | 1767 | if (!ret) |
60918275 LP |
1768 | return -ENOMEM; |
1769 | ||
7d17cfbc | 1770 | if (path) { |
ac155bb8 | 1771 | ret->fragment_path = strdup(path); |
dc409696 | 1772 | if (!ret->fragment_path) |
0301abf4 | 1773 | return -ENOMEM; |
7d17cfbc | 1774 | } |
0301abf4 | 1775 | |
1058cbf2 | 1776 | r = unit_add_name(ret, name); |
dc409696 | 1777 | if (r < 0) |
1ffba6fe | 1778 | return r; |
60918275 | 1779 | |
87f0e418 | 1780 | unit_add_to_load_queue(ret); |
c1e1601e | 1781 | unit_add_to_dbus_queue(ret); |
949061f0 | 1782 | unit_add_to_gc_queue(ret); |
c1e1601e | 1783 | |
7a6a095a | 1784 | *_ret = ret; |
dc409696 | 1785 | cleanup_ret = NULL; |
db06e3b6 LP |
1786 | |
1787 | return 0; | |
1788 | } | |
1789 | ||
c2756a68 LP |
1790 | int manager_load_unit( |
1791 | Manager *m, | |
1792 | const char *name, | |
1793 | const char *path, | |
718db961 | 1794 | sd_bus_error *e, |
c2756a68 LP |
1795 | Unit **_ret) { |
1796 | ||
db06e3b6 LP |
1797 | int r; |
1798 | ||
1799 | assert(m); | |
7a6a095a | 1800 | assert(_ret); |
db06e3b6 LP |
1801 | |
1802 | /* This will load the service information files, but not actually | |
1803 | * start any services or anything. */ | |
1804 | ||
c3090674 LP |
1805 | r = manager_load_unit_prepare(m, name, path, e, _ret); |
1806 | if (r != 0) | |
db06e3b6 LP |
1807 | return r; |
1808 | ||
f50e0a01 | 1809 | manager_dispatch_load_queue(m); |
60918275 | 1810 | |
7a6a095a | 1811 | *_ret = unit_follow_merge(*_ret); |
4109ede7 ZJS |
1812 | return 0; |
1813 | } | |
1814 | ||
1815 | int manager_load_startable_unit_or_warn( | |
1816 | Manager *m, | |
1817 | const char *name, | |
1818 | const char *path, | |
1819 | Unit **ret) { | |
1820 | ||
1821 | /* Load a unit, make sure it loaded fully and is not masked. */ | |
1822 | ||
1823 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; | |
1824 | Unit *unit; | |
1825 | int r; | |
1826 | ||
1827 | r = manager_load_unit(m, name, path, &error, &unit); | |
1828 | if (r < 0) | |
1829 | return log_error_errno(r, "Failed to load %s %s: %s", | |
1830 | name ? "unit" : "file", name ?: path, | |
1831 | bus_error_message(&error, r)); | |
1832 | else if (IN_SET(unit->load_state, UNIT_ERROR, UNIT_NOT_FOUND)) | |
1833 | return log_error_errno(unit->load_error, "Failed to load %s %s: %m", | |
1834 | name ? "unit" : "file", name ?: path); | |
1835 | else if (unit->load_state == UNIT_MASKED) { | |
1836 | log_error("%s %s is masked.", | |
1837 | name ? "Unit" : "File", name ?: path); | |
1838 | return -ERFKILL; | |
1839 | } | |
9e2f7c11 | 1840 | |
4109ede7 | 1841 | *ret = unit; |
60918275 LP |
1842 | return 0; |
1843 | } | |
a66d02c3 | 1844 | |
cea8e32e | 1845 | void manager_dump_jobs(Manager *s, FILE *f, const char *prefix) { |
034c6ed7 | 1846 | Iterator i; |
a66d02c3 LP |
1847 | Job *j; |
1848 | ||
1849 | assert(s); | |
1850 | assert(f); | |
1851 | ||
034c6ed7 | 1852 | HASHMAP_FOREACH(j, s->jobs, i) |
cea8e32e | 1853 | job_dump(j, f, prefix); |
a66d02c3 LP |
1854 | } |
1855 | ||
87f0e418 | 1856 | void manager_dump_units(Manager *s, FILE *f, const char *prefix) { |
034c6ed7 | 1857 | Iterator i; |
87f0e418 | 1858 | Unit *u; |
11dd41ce | 1859 | const char *t; |
a66d02c3 LP |
1860 | |
1861 | assert(s); | |
1862 | assert(f); | |
1863 | ||
87f0e418 | 1864 | HASHMAP_FOREACH_KEY(u, t, s->units, i) |
ac155bb8 | 1865 | if (u->id == t) |
87f0e418 | 1866 | unit_dump(u, f, prefix); |
a66d02c3 | 1867 | } |
7fad411c | 1868 | |
ad75b9e7 LP |
1869 | void manager_dump(Manager *m, FILE *f, const char *prefix) { |
1870 | ManagerTimestamp q; | |
1871 | ||
1872 | assert(m); | |
1873 | assert(f); | |
1874 | ||
1875 | for (q = 0; q < _MANAGER_TIMESTAMP_MAX; q++) { | |
1876 | char buf[FORMAT_TIMESTAMP_MAX]; | |
1877 | ||
1878 | if (dual_timestamp_is_set(m->timestamps + q)) | |
1879 | fprintf(f, "%sTimestamp %s: %s\n", | |
1880 | strempty(prefix), | |
1881 | manager_timestamp_to_string(q), | |
1882 | format_timestamp(buf, sizeof(buf), m->timestamps[q].realtime)); | |
1883 | } | |
1884 | ||
1885 | manager_dump_units(m, f, prefix); | |
1886 | manager_dump_jobs(m, f, prefix); | |
1887 | } | |
1888 | ||
713f6f90 LP |
1889 | int manager_get_dump_string(Manager *m, char **ret) { |
1890 | _cleanup_free_ char *dump = NULL; | |
1891 | _cleanup_fclose_ FILE *f = NULL; | |
1892 | size_t size; | |
1893 | int r; | |
1894 | ||
1895 | assert(m); | |
1896 | assert(ret); | |
1897 | ||
1898 | f = open_memstream(&dump, &size); | |
1899 | if (!f) | |
1900 | return -errno; | |
1901 | ||
0d536673 | 1902 | (void) __fsetlocking(f, FSETLOCKING_BYCALLER); |
713f6f90 LP |
1903 | |
1904 | manager_dump(m, f, NULL); | |
1905 | ||
1906 | r = fflush_and_check(f); | |
1907 | if (r < 0) | |
1908 | return r; | |
1909 | ||
1910 | f = safe_fclose(f); | |
1911 | ||
1cc6c93a | 1912 | *ret = TAKE_PTR(dump); |
713f6f90 LP |
1913 | |
1914 | return 0; | |
1915 | } | |
1916 | ||
7fad411c LP |
1917 | void manager_clear_jobs(Manager *m) { |
1918 | Job *j; | |
1919 | ||
1920 | assert(m); | |
1921 | ||
7fad411c | 1922 | while ((j = hashmap_first(m->jobs))) |
5273510e | 1923 | /* No need to recurse. We're cancelling all jobs. */ |
833f92ad | 1924 | job_finish_and_invalidate(j, JOB_CANCELED, false, false); |
7fad411c | 1925 | } |
83c60c9f | 1926 | |
752b5905 LP |
1927 | static int manager_dispatch_run_queue(sd_event_source *source, void *userdata) { |
1928 | Manager *m = userdata; | |
83c60c9f | 1929 | Job *j; |
034c6ed7 | 1930 | |
752b5905 LP |
1931 | assert(source); |
1932 | assert(m); | |
9152c765 | 1933 | |
034c6ed7 | 1934 | while ((j = m->run_queue)) { |
ac1135be | 1935 | assert(j->installed); |
034c6ed7 LP |
1936 | assert(j->in_run_queue); |
1937 | ||
1938 | job_run_and_invalidate(j); | |
9152c765 | 1939 | } |
034c6ed7 | 1940 | |
a0b64226 | 1941 | if (m->n_running_jobs > 0) |
03b717a3 MS |
1942 | manager_watch_jobs_in_progress(m); |
1943 | ||
31a7eb86 ZJS |
1944 | if (m->n_on_console > 0) |
1945 | manager_watch_idle_pipe(m); | |
1946 | ||
752b5905 | 1947 | return 1; |
c1e1601e LP |
1948 | } |
1949 | ||
9588bc32 | 1950 | static unsigned manager_dispatch_dbus_queue(Manager *m) { |
e0a08581 | 1951 | unsigned n = 0, budget; |
595ed347 | 1952 | Unit *u; |
e0a08581 | 1953 | Job *j; |
c1e1601e LP |
1954 | |
1955 | assert(m); | |
1956 | ||
1957 | if (m->dispatching_dbus_queue) | |
1958 | return 0; | |
1959 | ||
e0a08581 LP |
1960 | /* Anything to do at all? */ |
1961 | if (!m->dbus_unit_queue && !m->dbus_job_queue && !m->send_reloading_done && !m->queued_message) | |
1962 | return 0; | |
1963 | ||
1964 | /* Do we have overly many messages queued at the moment? If so, let's not enqueue more on top, let's sit this | |
1965 | * cycle out, and process things in a later cycle when the queues got a bit emptier. */ | |
1966 | if (manager_bus_n_queued_write(m) > MANAGER_BUS_BUSY_THRESHOLD) | |
1967 | return 0; | |
1968 | ||
1969 | /* Only process a certain number of units/jobs per event loop iteration. Even if the bus queue wasn't overly | |
1970 | * full before this call we shouldn't increase it in size too wildly in one step, and we shouldn't monopolize | |
1971 | * CPU time with generating these messages. Note the difference in counting of this "budget" and the | |
1972 | * "threshold" above: the "budget" is decreased only once per generated message, regardless how many | |
1973 | * busses/direct connections it is enqueued on, while the "threshold" is applied to each queued instance of bus | |
1974 | * message, i.e. if the same message is enqueued to five busses/direct connections it will be counted five | |
1975 | * times. This difference in counting ("references" vs. "instances") is primarily a result of the fact that | |
1976 | * it's easier to implement it this way, however it also reflects the thinking that the "threshold" should put | |
1977 | * a limit on used queue memory, i.e. space, while the "budget" should put a limit on time. Also note that | |
1978 | * the "threshold" is currently chosen much higher than the "budget". */ | |
1979 | budget = MANAGER_BUS_MESSAGE_BUDGET; | |
1980 | ||
c1e1601e LP |
1981 | m->dispatching_dbus_queue = true; |
1982 | ||
e0a08581 LP |
1983 | while (budget > 0 && (u = m->dbus_unit_queue)) { |
1984 | ||
595ed347 | 1985 | assert(u->in_dbus_queue); |
c1e1601e | 1986 | |
595ed347 | 1987 | bus_unit_send_change_signal(u); |
e0a08581 | 1988 | n++, budget--; |
c1e1601e LP |
1989 | } |
1990 | ||
e0a08581 | 1991 | while (budget > 0 && (j = m->dbus_job_queue)) { |
c1e1601e LP |
1992 | assert(j->in_dbus_queue); |
1993 | ||
1994 | bus_job_send_change_signal(j); | |
e0a08581 | 1995 | n++, budget--; |
c1e1601e LP |
1996 | } |
1997 | ||
1998 | m->dispatching_dbus_queue = false; | |
71445ae7 | 1999 | |
e0a08581 | 2000 | if (budget > 0 && m->send_reloading_done) { |
71445ae7 | 2001 | m->send_reloading_done = false; |
718db961 | 2002 | bus_manager_send_reloading(m, false); |
e0a08581 | 2003 | n++, budget--; |
71445ae7 LP |
2004 | } |
2005 | ||
e0a08581 | 2006 | if (budget > 0 && m->queued_message) { |
718db961 | 2007 | bus_send_queued_message(m); |
e0a08581 LP |
2008 | n++; |
2009 | } | |
718db961 | 2010 | |
c1e1601e | 2011 | return n; |
9152c765 LP |
2012 | } |
2013 | ||
d8fdc620 LP |
2014 | static int manager_dispatch_cgroups_agent_fd(sd_event_source *source, int fd, uint32_t revents, void *userdata) { |
2015 | Manager *m = userdata; | |
2016 | char buf[PATH_MAX+1]; | |
2017 | ssize_t n; | |
2018 | ||
2019 | n = recv(fd, buf, sizeof(buf), 0); | |
2020 | if (n < 0) | |
2021 | return log_error_errno(errno, "Failed to read cgroups agent message: %m"); | |
2022 | if (n == 0) { | |
2023 | log_error("Got zero-length cgroups agent message, ignoring."); | |
2024 | return 0; | |
2025 | } | |
2026 | if ((size_t) n >= sizeof(buf)) { | |
2027 | log_error("Got overly long cgroups agent message, ignoring."); | |
2028 | return 0; | |
2029 | } | |
2030 | ||
2031 | if (memchr(buf, 0, n)) { | |
2032 | log_error("Got cgroups agent message with embedded NUL byte, ignoring."); | |
2033 | return 0; | |
2034 | } | |
2035 | buf[n] = 0; | |
2036 | ||
2037 | manager_notify_cgroup_empty(m, buf); | |
d5f15326 | 2038 | (void) bus_forward_agent_released(m, buf); |
d8fdc620 LP |
2039 | |
2040 | return 0; | |
2041 | } | |
2042 | ||
db256aab LP |
2043 | static void manager_invoke_notify_message( |
2044 | Manager *m, | |
2045 | Unit *u, | |
2046 | const struct ucred *ucred, | |
2047 | const char *buf, | |
2048 | FDSet *fds) { | |
2049 | ||
5ba6985b LP |
2050 | assert(m); |
2051 | assert(u); | |
db256aab | 2052 | assert(ucred); |
5ba6985b | 2053 | assert(buf); |
5ba6985b | 2054 | |
62a76913 | 2055 | if (u->notifygen == m->notifygen) /* Already invoked on this same unit in this same iteration? */ |
5ba6985b | 2056 | return; |
62a76913 LP |
2057 | u->notifygen = m->notifygen; |
2058 | ||
2059 | if (UNIT_VTABLE(u)->notify_message) { | |
2060 | _cleanup_strv_free_ char **tags = NULL; | |
2061 | ||
2062 | tags = strv_split(buf, NEWLINE); | |
2063 | if (!tags) { | |
2064 | log_oom(); | |
2065 | return; | |
2066 | } | |
5ba6985b | 2067 | |
db256aab | 2068 | UNIT_VTABLE(u)->notify_message(u, ucred, tags, fds); |
62a76913 LP |
2069 | |
2070 | } else if (DEBUG_LOGGING) { | |
a86b7675 ZJS |
2071 | _cleanup_free_ char *x = NULL, *y = NULL; |
2072 | ||
da5fb861 | 2073 | x = ellipsize(buf, 20, 90); |
a86b7675 | 2074 | if (x) |
da5fb861 LP |
2075 | y = cescape(x); |
2076 | ||
a86b7675 ZJS |
2077 | log_unit_debug(u, "Got notification message \"%s\", ignoring.", strnull(y)); |
2078 | } | |
5ba6985b LP |
2079 | } |
2080 | ||
718db961 | 2081 | static int manager_dispatch_notify_fd(sd_event_source *source, int fd, uint32_t revents, void *userdata) { |
3d0b8a55 | 2082 | |
b215b0ed | 2083 | _cleanup_fdset_free_ FDSet *fds = NULL; |
718db961 | 2084 | Manager *m = userdata; |
b215b0ed DH |
2085 | char buf[NOTIFY_BUFFER_MAX+1]; |
2086 | struct iovec iovec = { | |
2087 | .iov_base = buf, | |
2088 | .iov_len = sizeof(buf)-1, | |
2089 | }; | |
2090 | union { | |
2091 | struct cmsghdr cmsghdr; | |
2092 | uint8_t buf[CMSG_SPACE(sizeof(struct ucred)) + | |
2093 | CMSG_SPACE(sizeof(int) * NOTIFY_FD_MAX)]; | |
2094 | } control = {}; | |
2095 | struct msghdr msghdr = { | |
2096 | .msg_iov = &iovec, | |
2097 | .msg_iovlen = 1, | |
2098 | .msg_control = &control, | |
2099 | .msg_controllen = sizeof(control), | |
2100 | }; | |
2101 | ||
2102 | struct cmsghdr *cmsg; | |
2103 | struct ucred *ucred = NULL; | |
62a76913 LP |
2104 | _cleanup_free_ Unit **array_copy = NULL; |
2105 | Unit *u1, *u2, **array; | |
b215b0ed | 2106 | int r, *fd_array = NULL; |
da6053d0 | 2107 | size_t n_fds = 0; |
62a76913 | 2108 | bool found = false; |
8c47c732 LP |
2109 | ssize_t n; |
2110 | ||
2111 | assert(m); | |
718db961 LP |
2112 | assert(m->notify_fd == fd); |
2113 | ||
2114 | if (revents != EPOLLIN) { | |
2115 | log_warning("Got unexpected poll event for notify fd."); | |
2116 | return 0; | |
2117 | } | |
8c47c732 | 2118 | |
045a3d59 | 2119 | n = recvmsg(m->notify_fd, &msghdr, MSG_DONTWAIT|MSG_CMSG_CLOEXEC|MSG_TRUNC); |
b215b0ed | 2120 | if (n < 0) { |
c55ae51e LP |
2121 | if (IN_SET(errno, EAGAIN, EINTR)) |
2122 | return 0; /* Spurious wakeup, try again */ | |
8c47c732 | 2123 | |
c55ae51e LP |
2124 | /* If this is any other, real error, then let's stop processing this socket. This of course means we |
2125 | * won't take notification messages anymore, but that's still better than busy looping around this: | |
2126 | * being woken up over and over again but being unable to actually read the message off the socket. */ | |
2127 | return log_error_errno(errno, "Failed to receive notification message: %m"); | |
b215b0ed | 2128 | } |
a354329f | 2129 | |
b215b0ed DH |
2130 | CMSG_FOREACH(cmsg, &msghdr) { |
2131 | if (cmsg->cmsg_level == SOL_SOCKET && cmsg->cmsg_type == SCM_RIGHTS) { | |
a354329f | 2132 | |
b215b0ed DH |
2133 | fd_array = (int*) CMSG_DATA(cmsg); |
2134 | n_fds = (cmsg->cmsg_len - CMSG_LEN(0)) / sizeof(int); | |
a354329f | 2135 | |
b215b0ed DH |
2136 | } else if (cmsg->cmsg_level == SOL_SOCKET && |
2137 | cmsg->cmsg_type == SCM_CREDENTIALS && | |
2138 | cmsg->cmsg_len == CMSG_LEN(sizeof(struct ucred))) { | |
a354329f | 2139 | |
b215b0ed | 2140 | ucred = (struct ucred*) CMSG_DATA(cmsg); |
a354329f | 2141 | } |
b215b0ed | 2142 | } |
a354329f | 2143 | |
b215b0ed DH |
2144 | if (n_fds > 0) { |
2145 | assert(fd_array); | |
a354329f | 2146 | |
b215b0ed DH |
2147 | r = fdset_new_array(&fds, fd_array, n_fds); |
2148 | if (r < 0) { | |
2149 | close_many(fd_array, n_fds); | |
9987750e FB |
2150 | log_oom(); |
2151 | return 0; | |
a354329f | 2152 | } |
b215b0ed | 2153 | } |
8c47c732 | 2154 | |
15e23e8c | 2155 | if (!ucred || !pid_is_valid(ucred->pid)) { |
b215b0ed DH |
2156 | log_warning("Received notify message without valid credentials. Ignoring."); |
2157 | return 0; | |
2158 | } | |
8c47c732 | 2159 | |
045a3d59 | 2160 | if ((size_t) n >= sizeof(buf) || (msghdr.msg_flags & MSG_TRUNC)) { |
b215b0ed DH |
2161 | log_warning("Received notify message exceeded maximum size. Ignoring."); |
2162 | return 0; | |
2163 | } | |
8c47c732 | 2164 | |
875ca88d LP |
2165 | /* As extra safety check, let's make sure the string we get doesn't contain embedded NUL bytes. We permit one |
2166 | * trailing NUL byte in the message, but don't expect it. */ | |
2167 | if (n > 1 && memchr(buf, 0, n-1)) { | |
2168 | log_warning("Received notify message with embedded NUL bytes. Ignoring."); | |
2169 | return 0; | |
2170 | } | |
2171 | ||
2172 | /* Make sure it's NUL-terminated. */ | |
b215b0ed | 2173 | buf[n] = 0; |
8c47c732 | 2174 | |
62a76913 LP |
2175 | /* Increase the generation counter used for filtering out duplicate unit invocations. */ |
2176 | m->notifygen++; | |
2177 | ||
2178 | /* Notify every unit that might be interested, which might be multiple. */ | |
b215b0ed | 2179 | u1 = manager_get_unit_by_pid_cgroup(m, ucred->pid); |
62a76913 LP |
2180 | u2 = hashmap_get(m->watch_pids, PID_TO_PTR(ucred->pid)); |
2181 | array = hashmap_get(m->watch_pids, PID_TO_PTR(-ucred->pid)); | |
2182 | if (array) { | |
2183 | size_t k = 0; | |
5ba6985b | 2184 | |
62a76913 LP |
2185 | while (array[k]) |
2186 | k++; | |
5ba6985b | 2187 | |
62a76913 LP |
2188 | array_copy = newdup(Unit*, array, k+1); |
2189 | if (!array_copy) | |
2190 | log_oom(); | |
2191 | } | |
2192 | /* And now invoke the per-unit callbacks. Note that manager_invoke_notify_message() will handle duplicate units | |
2193 | * make sure we only invoke each unit's handler once. */ | |
2194 | if (u1) { | |
2195 | manager_invoke_notify_message(m, u1, ucred, buf, fds); | |
2196 | found = true; | |
2197 | } | |
2198 | if (u2) { | |
2199 | manager_invoke_notify_message(m, u2, ucred, buf, fds); | |
2200 | found = true; | |
2201 | } | |
2202 | if (array_copy) | |
2203 | for (size_t i = 0; array_copy[i]; i++) { | |
2204 | manager_invoke_notify_message(m, array_copy[i], ucred, buf, fds); | |
2205 | found = true; | |
2206 | } | |
8c47c732 | 2207 | |
62a76913 LP |
2208 | if (!found) |
2209 | log_warning("Cannot find unit for notify message of PID "PID_FMT", ignoring.", ucred->pid); | |
a354329f | 2210 | |
b215b0ed | 2211 | if (fdset_size(fds) > 0) |
5fd2c135 | 2212 | log_warning("Got extra auxiliary fds with notification message, closing them."); |
8c47c732 LP |
2213 | |
2214 | return 0; | |
2215 | } | |
2216 | ||
62a76913 LP |
2217 | static void manager_invoke_sigchld_event( |
2218 | Manager *m, | |
2219 | Unit *u, | |
2220 | const siginfo_t *si) { | |
36f20ae3 | 2221 | |
5ba6985b LP |
2222 | assert(m); |
2223 | assert(u); | |
2224 | assert(si); | |
2225 | ||
62a76913 LP |
2226 | /* Already invoked the handler of this unit in this iteration? Then don't process this again */ |
2227 | if (u->sigchldgen == m->sigchldgen) | |
2228 | return; | |
2229 | u->sigchldgen = m->sigchldgen; | |
5ba6985b | 2230 | |
62a76913 | 2231 | log_unit_debug(u, "Child "PID_FMT" belongs to %s.", si->si_pid, u->id); |
5ba6985b | 2232 | unit_unwatch_pid(u, si->si_pid); |
e57051f5 | 2233 | |
62a76913 LP |
2234 | if (UNIT_VTABLE(u)->sigchld_event) |
2235 | UNIT_VTABLE(u)->sigchld_event(u, si->si_pid, si->si_code, si->si_status); | |
5ba6985b LP |
2236 | } |
2237 | ||
575b300b LP |
2238 | static int manager_dispatch_sigchld(sd_event_source *source, void *userdata) { |
2239 | Manager *m = userdata; | |
2240 | siginfo_t si = {}; | |
2241 | int r; | |
2242 | ||
2243 | assert(source); | |
9152c765 LP |
2244 | assert(m); |
2245 | ||
575b300b LP |
2246 | /* First we call waitd() for a PID and do not reap the zombie. That way we can still access /proc/$PID for it |
2247 | * while it is a zombie. */ | |
9152c765 | 2248 | |
575b300b | 2249 | if (waitid(P_ALL, 0, &si, WEXITED|WNOHANG|WNOWAIT) < 0) { |
acbb0225 | 2250 | |
8afabc50 AJ |
2251 | if (errno != ECHILD) |
2252 | log_error_errno(errno, "Failed to peek for child with waitid(), ignoring: %m"); | |
acbb0225 | 2253 | |
8afabc50 | 2254 | goto turn_off; |
575b300b | 2255 | } |
4112df16 | 2256 | |
575b300b LP |
2257 | if (si.si_pid <= 0) |
2258 | goto turn_off; | |
2259 | ||
2260 | if (IN_SET(si.si_code, CLD_EXITED, CLD_KILLED, CLD_DUMPED)) { | |
62a76913 | 2261 | _cleanup_free_ Unit **array_copy = NULL; |
575b300b | 2262 | _cleanup_free_ char *name = NULL; |
62a76913 | 2263 | Unit *u1, *u2, **array; |
575b300b LP |
2264 | |
2265 | (void) get_process_comm(si.si_pid, &name); | |
2266 | ||
2267 | log_debug("Child "PID_FMT" (%s) died (code=%s, status=%i/%s)", | |
2268 | si.si_pid, strna(name), | |
2269 | sigchld_code_to_string(si.si_code), | |
2270 | si.si_status, | |
2271 | strna(si.si_code == CLD_EXITED | |
2272 | ? exit_status_to_string(si.si_status, EXIT_STATUS_FULL) | |
2273 | : signal_to_string(si.si_status))); | |
2274 | ||
62a76913 LP |
2275 | /* Increase the generation counter used for filtering out duplicate unit invocations */ |
2276 | m->sigchldgen++; | |
2277 | ||
2278 | /* And now figure out the unit this belongs to, it might be multiple... */ | |
575b300b | 2279 | u1 = manager_get_unit_by_pid_cgroup(m, si.si_pid); |
62a76913 LP |
2280 | u2 = hashmap_get(m->watch_pids, PID_TO_PTR(si.si_pid)); |
2281 | array = hashmap_get(m->watch_pids, PID_TO_PTR(-si.si_pid)); | |
2282 | if (array) { | |
2283 | size_t n = 0; | |
2284 | ||
2285 | /* Cound how many entries the array has */ | |
2286 | while (array[n]) | |
2287 | n++; | |
2288 | ||
2289 | /* Make a copy of the array so that we don't trip up on the array changing beneath us */ | |
2290 | array_copy = newdup(Unit*, array, n+1); | |
2291 | if (!array_copy) | |
2292 | log_oom(); | |
2293 | } | |
2294 | ||
2295 | /* Finally, execute them all. Note that u1, u2 and the array might contain duplicates, but | |
2296 | * that's fine, manager_invoke_sigchld_event() will ensure we only invoke the handlers once for | |
2297 | * each iteration. */ | |
575b300b | 2298 | if (u1) |
62a76913 LP |
2299 | manager_invoke_sigchld_event(m, u1, &si); |
2300 | if (u2) | |
2301 | manager_invoke_sigchld_event(m, u2, &si); | |
2302 | if (array_copy) | |
2303 | for (size_t i = 0; array_copy[i]; i++) | |
2304 | manager_invoke_sigchld_event(m, array_copy[i], &si); | |
575b300b | 2305 | } |
9152c765 | 2306 | |
575b300b LP |
2307 | /* And now, we actually reap the zombie. */ |
2308 | if (waitid(P_PID, si.si_pid, &si, WEXITED) < 0) { | |
2309 | log_error_errno(errno, "Failed to dequeue child, ignoring: %m"); | |
2310 | return 0; | |
2311 | } | |
9152c765 | 2312 | |
575b300b | 2313 | return 0; |
8c47c732 | 2314 | |
575b300b LP |
2315 | turn_off: |
2316 | /* All children processed for now, turn off event source */ | |
4112df16 | 2317 | |
575b300b LP |
2318 | r = sd_event_source_set_enabled(m->sigchld_event_source, SD_EVENT_OFF); |
2319 | if (r < 0) | |
2320 | return log_error_errno(r, "Failed to disable SIGCHLD event source: %m"); | |
9152c765 LP |
2321 | |
2322 | return 0; | |
2323 | } | |
2324 | ||
c75fbada | 2325 | static void manager_start_target(Manager *m, const char *name, JobMode mode) { |
4afd3348 | 2326 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
28247076 | 2327 | int r; |
398ef8ba | 2328 | |
f2341e0a | 2329 | log_debug("Activating special unit %s", name); |
1e001f52 | 2330 | |
4bd29fe5 | 2331 | r = manager_add_job_by_name(m, JOB_START, name, mode, &error, NULL); |
bd0af849 | 2332 | if (r < 0) |
f2341e0a | 2333 | log_error("Failed to enqueue %s job: %s", name, bus_error_message(&error, r)); |
28247076 LP |
2334 | } |
2335 | ||
24dd31c1 LN |
2336 | static void manager_handle_ctrl_alt_del(Manager *m) { |
2337 | /* If the user presses C-A-D more than | |
2338 | * 7 times within 2s, we reboot/shutdown immediately, | |
2339 | * unless it was disabled in system.conf */ | |
2340 | ||
7994ac1d | 2341 | if (ratelimit_below(&m->ctrl_alt_del_ratelimit) || m->cad_burst_action == EMERGENCY_ACTION_NONE) |
24dd31c1 | 2342 | manager_start_target(m, SPECIAL_CTRL_ALT_DEL_TARGET, JOB_REPLACE_IRREVERSIBLY); |
ae8c7939 LN |
2343 | else |
2344 | emergency_action(m, m->cad_burst_action, NULL, | |
2345 | "Ctrl-Alt-Del was pressed more than 7 times within 2s"); | |
24dd31c1 LN |
2346 | } |
2347 | ||
718db961 LP |
2348 | static int manager_dispatch_signal_fd(sd_event_source *source, int fd, uint32_t revents, void *userdata) { |
2349 | Manager *m = userdata; | |
9152c765 LP |
2350 | ssize_t n; |
2351 | struct signalfd_siginfo sfsi; | |
dacd6cee | 2352 | int r; |
9152c765 LP |
2353 | |
2354 | assert(m); | |
718db961 LP |
2355 | assert(m->signal_fd == fd); |
2356 | ||
2357 | if (revents != EPOLLIN) { | |
2358 | log_warning("Got unexpected events from signal file descriptor."); | |
2359 | return 0; | |
2360 | } | |
9152c765 | 2361 | |
575b300b LP |
2362 | n = read(m->signal_fd, &sfsi, sizeof(sfsi)); |
2363 | if (n != sizeof(sfsi)) { | |
2364 | if (n >= 0) { | |
2365 | log_warning("Truncated read from signal fd (%zu bytes), ignoring!", n); | |
2366 | return 0; | |
2367 | } | |
9152c765 | 2368 | |
575b300b LP |
2369 | if (IN_SET(errno, EINTR, EAGAIN)) |
2370 | return 0; | |
9152c765 | 2371 | |
575b300b LP |
2372 | /* We return an error here, which will kill this handler, |
2373 | * to avoid a busy loop on read error. */ | |
2374 | return log_error_errno(errno, "Reading from signal fd failed: %m"); | |
2375 | } | |
9152c765 | 2376 | |
575b300b LP |
2377 | log_received_signal(sfsi.ssi_signo == SIGCHLD || |
2378 | (sfsi.ssi_signo == SIGTERM && MANAGER_IS_USER(m)) | |
2379 | ? LOG_DEBUG : LOG_INFO, | |
2380 | &sfsi); | |
1e001f52 | 2381 | |
575b300b | 2382 | switch (sfsi.ssi_signo) { |
b9cd2ec1 | 2383 | |
575b300b LP |
2384 | case SIGCHLD: |
2385 | r = sd_event_source_set_enabled(m->sigchld_event_source, SD_EVENT_ON); | |
2386 | if (r < 0) | |
8afabc50 | 2387 | log_warning_errno(r, "Failed to enable SIGCHLD event source, ignoring: %m"); |
b9cd2ec1 | 2388 | |
575b300b | 2389 | break; |
84e9af1e | 2390 | |
575b300b LP |
2391 | case SIGTERM: |
2392 | if (MANAGER_IS_SYSTEM(m)) { | |
ba0c7754 | 2393 | /* This is for compatibility with the original sysvinit */ |
575b300b LP |
2394 | r = verify_run_space_and_log("Refusing to reexecute"); |
2395 | if (r >= 0) | |
2396 | m->exit_code = MANAGER_REEXECUTE; | |
a1b256b0 | 2397 | break; |
575b300b | 2398 | } |
84e9af1e | 2399 | |
575b300b LP |
2400 | _fallthrough_; |
2401 | case SIGINT: | |
2402 | if (MANAGER_IS_SYSTEM(m)) | |
2403 | manager_handle_ctrl_alt_del(m); | |
2404 | else | |
2405 | manager_start_target(m, SPECIAL_EXIT_TARGET, | |
2406 | JOB_REPLACE_IRREVERSIBLY); | |
2407 | break; | |
84e9af1e | 2408 | |
575b300b | 2409 | case SIGWINCH: |
ba0c7754 | 2410 | /* This is a nop on non-init */ |
575b300b LP |
2411 | if (MANAGER_IS_SYSTEM(m)) |
2412 | manager_start_target(m, SPECIAL_KBREQUEST_TARGET, JOB_REPLACE); | |
84e9af1e | 2413 | |
575b300b | 2414 | break; |
84e9af1e | 2415 | |
575b300b | 2416 | case SIGPWR: |
ba0c7754 | 2417 | /* This is a nop on non-init */ |
575b300b LP |
2418 | if (MANAGER_IS_SYSTEM(m)) |
2419 | manager_start_target(m, SPECIAL_SIGPWR_TARGET, JOB_REPLACE); | |
6632c602 | 2420 | |
575b300b | 2421 | break; |
57ee42ce | 2422 | |
8559b3b7 | 2423 | case SIGUSR1: |
8559b3b7 | 2424 | if (manager_dbus_is_running(m, false)) { |
575b300b | 2425 | log_info("Trying to reconnect to bus..."); |
575b300b | 2426 | |
8559b3b7 LP |
2427 | (void) bus_init_api(m); |
2428 | ||
2429 | if (MANAGER_IS_SYSTEM(m)) | |
2430 | (void) bus_init_system(m); | |
2431 | } else { | |
2432 | log_info("Starting D-Bus service..."); | |
575b300b LP |
2433 | manager_start_target(m, SPECIAL_DBUS_SERVICE, JOB_REPLACE); |
2434 | } | |
57ee42ce | 2435 | |
575b300b | 2436 | break; |
575b300b LP |
2437 | |
2438 | case SIGUSR2: { | |
2439 | _cleanup_free_ char *dump = NULL; | |
2440 | ||
2441 | r = manager_get_dump_string(m, &dump); | |
2442 | if (r < 0) { | |
2443 | log_warning_errno(errno, "Failed to acquire manager dump: %m"); | |
57ee42ce LP |
2444 | break; |
2445 | } | |
2446 | ||
575b300b LP |
2447 | log_dump(LOG_INFO, dump); |
2448 | break; | |
2449 | } | |
2149e37c | 2450 | |
575b300b LP |
2451 | case SIGHUP: |
2452 | r = verify_run_space_and_log("Refusing to reload"); | |
2453 | if (r >= 0) | |
2454 | m->exit_code = MANAGER_RELOAD; | |
2455 | break; | |
2456 | ||
2457 | default: { | |
2458 | ||
2459 | /* Starting SIGRTMIN+0 */ | |
2460 | static const struct { | |
2461 | const char *target; | |
2462 | JobMode mode; | |
2463 | } target_table[] = { | |
2464 | [0] = { SPECIAL_DEFAULT_TARGET, JOB_ISOLATE }, | |
2465 | [1] = { SPECIAL_RESCUE_TARGET, JOB_ISOLATE }, | |
2466 | [2] = { SPECIAL_EMERGENCY_TARGET, JOB_ISOLATE }, | |
2467 | [3] = { SPECIAL_HALT_TARGET, JOB_REPLACE_IRREVERSIBLY }, | |
2468 | [4] = { SPECIAL_POWEROFF_TARGET, JOB_REPLACE_IRREVERSIBLY }, | |
2469 | [5] = { SPECIAL_REBOOT_TARGET, JOB_REPLACE_IRREVERSIBLY }, | |
2470 | [6] = { SPECIAL_KEXEC_TARGET, JOB_REPLACE_IRREVERSIBLY }, | |
2471 | }; | |
2472 | ||
2473 | /* Starting SIGRTMIN+13, so that target halt and system halt are 10 apart */ | |
2474 | static const ManagerExitCode code_table[] = { | |
2475 | [0] = MANAGER_HALT, | |
2476 | [1] = MANAGER_POWEROFF, | |
2477 | [2] = MANAGER_REBOOT, | |
2478 | [3] = MANAGER_KEXEC, | |
2479 | }; | |
b2cdc666 | 2480 | |
575b300b LP |
2481 | if ((int) sfsi.ssi_signo >= SIGRTMIN+0 && |
2482 | (int) sfsi.ssi_signo < SIGRTMIN+(int) ELEMENTSOF(target_table)) { | |
2483 | int idx = (int) sfsi.ssi_signo - SIGRTMIN; | |
2484 | manager_start_target(m, target_table[idx].target, | |
2485 | target_table[idx].mode); | |
1005d14f | 2486 | break; |
2149e37c | 2487 | } |
1005d14f | 2488 | |
575b300b LP |
2489 | if ((int) sfsi.ssi_signo >= SIGRTMIN+13 && |
2490 | (int) sfsi.ssi_signo < SIGRTMIN+13+(int) ELEMENTSOF(code_table)) { | |
2491 | m->exit_code = code_table[sfsi.ssi_signo - SIGRTMIN - 13]; | |
2492 | break; | |
2493 | } | |
2494 | ||
2495 | switch (sfsi.ssi_signo - SIGRTMIN) { | |
2496 | ||
2497 | case 20: | |
2498 | manager_set_show_status(m, SHOW_STATUS_YES); | |
a16e1123 LP |
2499 | break; |
2500 | ||
575b300b LP |
2501 | case 21: |
2502 | manager_set_show_status(m, SHOW_STATUS_NO); | |
2503 | break; | |
7d793605 | 2504 | |
575b300b LP |
2505 | case 22: |
2506 | log_set_max_level(LOG_DEBUG); | |
2507 | log_info("Setting log level to debug."); | |
2508 | break; | |
2509 | ||
2510 | case 23: | |
2511 | log_set_max_level(LOG_INFO); | |
2512 | log_info("Setting log level to info."); | |
2513 | break; | |
0003d1ab | 2514 | |
575b300b LP |
2515 | case 24: |
2516 | if (MANAGER_IS_USER(m)) { | |
2517 | m->exit_code = MANAGER_EXIT; | |
2518 | return 0; | |
0658666b | 2519 | } |
9152c765 | 2520 | |
575b300b LP |
2521 | /* This is a nop on init */ |
2522 | break; | |
2523 | ||
2524 | case 26: | |
2525 | case 29: /* compatibility: used to be mapped to LOG_TARGET_SYSLOG_OR_KMSG */ | |
2526 | log_set_target(LOG_TARGET_JOURNAL_OR_KMSG); | |
2527 | log_notice("Setting log target to journal-or-kmsg."); | |
2528 | break; | |
2529 | ||
2530 | case 27: | |
2531 | log_set_target(LOG_TARGET_CONSOLE); | |
2532 | log_notice("Setting log target to console."); | |
2533 | break; | |
2534 | ||
2535 | case 28: | |
2536 | log_set_target(LOG_TARGET_KMSG); | |
2537 | log_notice("Setting log target to kmsg."); | |
2538 | break; | |
2539 | ||
2540 | default: | |
2541 | log_warning("Got unhandled signal <%s>.", signal_to_string(sfsi.ssi_signo)); | |
2542 | } | |
2543 | }} | |
034c6ed7 LP |
2544 | |
2545 | return 0; | |
2546 | } | |
2547 | ||
718db961 LP |
2548 | static int manager_dispatch_time_change_fd(sd_event_source *source, int fd, uint32_t revents, void *userdata) { |
2549 | Manager *m = userdata; | |
2550 | Iterator i; | |
2551 | Unit *u; | |
034c6ed7 LP |
2552 | |
2553 | assert(m); | |
718db961 | 2554 | assert(m->time_change_fd == fd); |
034c6ed7 | 2555 | |
a80c1575 | 2556 | log_struct(LOG_DEBUG, |
2b044526 | 2557 | "MESSAGE_ID=" SD_MESSAGE_TIME_CHANGE_STR, |
e2cc6eca | 2558 | LOG_MESSAGE("Time has been changed"), |
718db961 | 2559 | NULL); |
034c6ed7 | 2560 | |
718db961 LP |
2561 | /* Restart the watch */ |
2562 | m->time_change_event_source = sd_event_source_unref(m->time_change_event_source); | |
03e334a1 | 2563 | m->time_change_fd = safe_close(m->time_change_fd); |
ef734fd6 | 2564 | |
718db961 | 2565 | manager_setup_time_change(m); |
4e434314 | 2566 | |
718db961 LP |
2567 | HASHMAP_FOREACH(u, m->units, i) |
2568 | if (UNIT_VTABLE(u)->time_change) | |
2569 | UNIT_VTABLE(u)->time_change(u); | |
ea430986 | 2570 | |
718db961 LP |
2571 | return 0; |
2572 | } | |
ea430986 | 2573 | |
718db961 LP |
2574 | static int manager_dispatch_idle_pipe_fd(sd_event_source *source, int fd, uint32_t revents, void *userdata) { |
2575 | Manager *m = userdata; | |
8742514c | 2576 | |
718db961 LP |
2577 | assert(m); |
2578 | assert(m->idle_pipe[2] == fd); | |
8742514c | 2579 | |
5a69973f LP |
2580 | /* There's at least one Type=idle child that just gave up on us waiting for the boot process to complete. Let's |
2581 | * now turn off any further console output if there's at least one service that needs console access, so that | |
2582 | * from now on our own output should not spill into that service's output anymore. After all, we support | |
2583 | * Type=idle only to beautify console output and it generally is set on services that want to own the console | |
2584 | * exclusively without our interference. */ | |
718db961 | 2585 | m->no_console_output = m->n_on_console > 0; |
03b717a3 | 2586 | |
5a69973f LP |
2587 | /* Acknowledge the child's request, and let all all other children know too that they shouldn't wait any longer |
2588 | * by closing the pipes towards them, which is what they are waiting for. */ | |
718db961 | 2589 | manager_close_idle_pipe(m); |
03b717a3 | 2590 | |
718db961 LP |
2591 | return 0; |
2592 | } | |
31a7eb86 | 2593 | |
718db961 LP |
2594 | static int manager_dispatch_jobs_in_progress(sd_event_source *source, usec_t usec, void *userdata) { |
2595 | Manager *m = userdata; | |
fd08a840 ZJS |
2596 | int r; |
2597 | uint64_t next; | |
31a7eb86 | 2598 | |
718db961 | 2599 | assert(m); |
fd08a840 | 2600 | assert(source); |
9152c765 | 2601 | |
718db961 | 2602 | manager_print_jobs_in_progress(m); |
fd08a840 ZJS |
2603 | |
2604 | next = now(CLOCK_MONOTONIC) + JOBS_IN_PROGRESS_PERIOD_USEC; | |
2605 | r = sd_event_source_set_time(source, next); | |
2606 | if (r < 0) | |
2607 | return r; | |
2608 | ||
2609 | return sd_event_source_set_enabled(source, SD_EVENT_ONESHOT); | |
9152c765 LP |
2610 | } |
2611 | ||
2612 | int manager_loop(Manager *m) { | |
2613 | int r; | |
9152c765 | 2614 | |
fac9f8df | 2615 | RATELIMIT_DEFINE(rl, 1*USEC_PER_SEC, 50000); |
ea430986 | 2616 | |
9152c765 | 2617 | assert(m); |
f755e3b7 | 2618 | m->exit_code = MANAGER_OK; |
9152c765 | 2619 | |
fe51822e | 2620 | /* Release the path cache */ |
97044145 | 2621 | m->unit_path_cache = set_free_free(m->unit_path_cache); |
fe51822e | 2622 | |
b0c918b9 LP |
2623 | manager_check_finished(m); |
2624 | ||
575b300b LP |
2625 | /* There might still be some zombies hanging around from before we were exec()'ed. Let's reap them. */ |
2626 | r = sd_event_source_set_enabled(m->sigchld_event_source, SD_EVENT_ON); | |
e96d6be7 | 2627 | if (r < 0) |
575b300b | 2628 | return log_error_errno(r, "Failed to enable SIGCHLD event source: %m"); |
a4312405 | 2629 | |
f755e3b7 | 2630 | while (m->exit_code == MANAGER_OK) { |
718db961 | 2631 | usec_t wait_usec; |
9152c765 | 2632 | |
463d0d15 | 2633 | if (m->runtime_watchdog > 0 && m->runtime_watchdog != USEC_INFINITY && MANAGER_IS_SYSTEM(m)) |
e96d6be7 LP |
2634 | watchdog_ping(); |
2635 | ||
7994ac1d | 2636 | if (!ratelimit_below(&rl)) { |
ea430986 LP |
2637 | /* Yay, something is going seriously wrong, pause a little */ |
2638 | log_warning("Looping too fast. Throttling execution a little."); | |
2639 | sleep(1); | |
2640 | } | |
2641 | ||
37a8e683 | 2642 | if (manager_dispatch_load_queue(m) > 0) |
23a177ef LP |
2643 | continue; |
2644 | ||
c5a97ed1 LP |
2645 | if (manager_dispatch_gc_job_queue(m) > 0) |
2646 | continue; | |
2647 | ||
2648 | if (manager_dispatch_gc_unit_queue(m) > 0) | |
701cc384 LP |
2649 | continue; |
2650 | ||
cf1265e1 | 2651 | if (manager_dispatch_cleanup_queue(m) > 0) |
c1e1601e | 2652 | continue; |
034c6ed7 | 2653 | |
91a6073e | 2654 | if (manager_dispatch_cgroup_realize_queue(m) > 0) |
c1e1601e LP |
2655 | continue; |
2656 | ||
c1e1601e | 2657 | if (manager_dispatch_dbus_queue(m) > 0) |
ea430986 | 2658 | continue; |
ea430986 | 2659 | |
c757a65b | 2660 | /* Sleep for half the watchdog time */ |
463d0d15 | 2661 | if (m->runtime_watchdog > 0 && m->runtime_watchdog != USEC_INFINITY && MANAGER_IS_SYSTEM(m)) { |
718db961 LP |
2662 | wait_usec = m->runtime_watchdog / 2; |
2663 | if (wait_usec <= 0) | |
2664 | wait_usec = 1; | |
c757a65b | 2665 | } else |
3a43da28 | 2666 | wait_usec = USEC_INFINITY; |
9152c765 | 2667 | |
718db961 | 2668 | r = sd_event_run(m->event, wait_usec); |
23bbb0de MS |
2669 | if (r < 0) |
2670 | return log_error_errno(r, "Failed to run event loop: %m"); | |
a16e1123 | 2671 | } |
957ca890 | 2672 | |
a16e1123 | 2673 | return m->exit_code; |
83c60c9f | 2674 | } |
ea430986 | 2675 | |
718db961 | 2676 | int manager_load_unit_from_dbus_path(Manager *m, const char *s, sd_bus_error *e, Unit **_u) { |
ede3a796 | 2677 | _cleanup_free_ char *n = NULL; |
4b58153d | 2678 | sd_id128_t invocation_id; |
ea430986 | 2679 | Unit *u; |
80fbf05e | 2680 | int r; |
ea430986 LP |
2681 | |
2682 | assert(m); | |
2683 | assert(s); | |
2684 | assert(_u); | |
2685 | ||
ede3a796 LP |
2686 | r = unit_name_from_dbus_path(s, &n); |
2687 | if (r < 0) | |
2688 | return r; | |
ea430986 | 2689 | |
4b58153d LP |
2690 | /* Permit addressing units by invocation ID: if the passed bus path is suffixed by a 128bit ID then we use it |
2691 | * as invocation ID. */ | |
2692 | r = sd_id128_from_string(n, &invocation_id); | |
2693 | if (r >= 0) { | |
2694 | u = hashmap_get(m->units_by_invocation_id, &invocation_id); | |
2695 | if (u) { | |
2696 | *_u = u; | |
2697 | return 0; | |
2698 | } | |
2699 | ||
930c124c ZJS |
2700 | return sd_bus_error_setf(e, BUS_ERROR_NO_UNIT_FOR_INVOCATION_ID, |
2701 | "No unit with the specified invocation ID " SD_ID128_FORMAT_STR " known.", | |
2702 | SD_ID128_FORMAT_VAL(invocation_id)); | |
4b58153d LP |
2703 | } |
2704 | ||
00c83b43 | 2705 | /* If this didn't work, we check if this is a unit name */ |
930c124c ZJS |
2706 | if (!unit_name_is_valid(n, UNIT_NAME_PLAIN|UNIT_NAME_INSTANCE)) { |
2707 | _cleanup_free_ char *nn = NULL; | |
2708 | ||
2709 | nn = cescape(n); | |
2710 | return sd_bus_error_setf(e, SD_BUS_ERROR_INVALID_ARGS, | |
2711 | "Unit name %s is neither a valid invocation ID nor unit name.", strnull(nn)); | |
2712 | } | |
00c83b43 | 2713 | |
80fbf05e | 2714 | r = manager_load_unit(m, n, NULL, e, &u); |
80fbf05e MS |
2715 | if (r < 0) |
2716 | return r; | |
ea430986 LP |
2717 | |
2718 | *_u = u; | |
ea430986 LP |
2719 | return 0; |
2720 | } | |
86fbf370 LP |
2721 | |
2722 | int manager_get_job_from_dbus_path(Manager *m, const char *s, Job **_j) { | |
718db961 | 2723 | const char *p; |
86fbf370 | 2724 | unsigned id; |
718db961 | 2725 | Job *j; |
86fbf370 LP |
2726 | int r; |
2727 | ||
2728 | assert(m); | |
2729 | assert(s); | |
2730 | assert(_j); | |
2731 | ||
718db961 LP |
2732 | p = startswith(s, "/org/freedesktop/systemd1/job/"); |
2733 | if (!p) | |
86fbf370 LP |
2734 | return -EINVAL; |
2735 | ||
718db961 | 2736 | r = safe_atou(p, &id); |
8742514c | 2737 | if (r < 0) |
86fbf370 LP |
2738 | return r; |
2739 | ||
8742514c LP |
2740 | j = manager_get_job(m, id); |
2741 | if (!j) | |
86fbf370 LP |
2742 | return -ENOENT; |
2743 | ||
2744 | *_j = j; | |
2745 | ||
2746 | return 0; | |
2747 | } | |
dfcd764e | 2748 | |
4927fcae | 2749 | void manager_send_unit_audit(Manager *m, Unit *u, int type, bool success) { |
e537352b | 2750 | |
349cc4a5 | 2751 | #if HAVE_AUDIT |
2ba11090 | 2752 | _cleanup_free_ char *p = NULL; |
0aa281df | 2753 | const char *msg; |
7410616c | 2754 | int audit_fd, r; |
e537352b | 2755 | |
463d0d15 | 2756 | if (!MANAGER_IS_SYSTEM(m)) |
a1a078ee LP |
2757 | return; |
2758 | ||
c1165f82 LP |
2759 | audit_fd = get_audit_fd(); |
2760 | if (audit_fd < 0) | |
e537352b LP |
2761 | return; |
2762 | ||
bbd3a7ba LP |
2763 | /* Don't generate audit events if the service was already |
2764 | * started and we're just deserializing */ | |
2c289ea8 | 2765 | if (MANAGER_IS_RELOADING(m)) |
bbd3a7ba LP |
2766 | return; |
2767 | ||
ac155bb8 | 2768 | if (u->type != UNIT_SERVICE) |
f1dd0c3f LP |
2769 | return; |
2770 | ||
7410616c LP |
2771 | r = unit_name_to_prefix_and_instance(u->id, &p); |
2772 | if (r < 0) { | |
2773 | log_error_errno(r, "Failed to extract prefix and instance of unit name: %m"); | |
e537352b LP |
2774 | return; |
2775 | } | |
2776 | ||
63c372cb | 2777 | msg = strjoina("unit=", p); |
0aa281df LP |
2778 | if (audit_log_user_comm_message(audit_fd, type, msg, "systemd", NULL, NULL, NULL, success) < 0) { |
2779 | if (errno == EPERM) | |
391ade86 | 2780 | /* We aren't allowed to send audit messages? |
44785992 | 2781 | * Then let's not retry again. */ |
c1165f82 | 2782 | close_audit_fd(); |
0aa281df | 2783 | else |
56f64d95 | 2784 | log_warning_errno(errno, "Failed to send audit message: %m"); |
391ade86 | 2785 | } |
4927fcae | 2786 | #endif |
e537352b | 2787 | |
e537352b LP |
2788 | } |
2789 | ||
e983b760 | 2790 | void manager_send_unit_plymouth(Manager *m, Unit *u) { |
fc2fffe7 | 2791 | static const union sockaddr_union sa = PLYMOUTH_SOCKET; |
2ba11090 ZJS |
2792 | _cleanup_free_ char *message = NULL; |
2793 | _cleanup_close_ int fd = -1; | |
fc2fffe7 | 2794 | int n = 0; |
e983b760 LP |
2795 | |
2796 | /* Don't generate plymouth events if the service was already | |
2797 | * started and we're just deserializing */ | |
2c289ea8 | 2798 | if (MANAGER_IS_RELOADING(m)) |
e983b760 LP |
2799 | return; |
2800 | ||
463d0d15 | 2801 | if (!MANAGER_IS_SYSTEM(m)) |
e983b760 LP |
2802 | return; |
2803 | ||
75f86906 | 2804 | if (detect_container() > 0) |
3772995a LP |
2805 | return; |
2806 | ||
ec2ce0c5 | 2807 | if (!IN_SET(u->type, UNIT_SERVICE, UNIT_MOUNT, UNIT_SWAP)) |
e983b760 LP |
2808 | return; |
2809 | ||
2810 | /* We set SOCK_NONBLOCK here so that we rather drop the | |
2811 | * message then wait for plymouth */ | |
e62d8c39 ZJS |
2812 | fd = socket(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0); |
2813 | if (fd < 0) { | |
56f64d95 | 2814 | log_error_errno(errno, "socket() failed: %m"); |
e983b760 LP |
2815 | return; |
2816 | } | |
2817 | ||
fc2fffe7 | 2818 | if (connect(fd, &sa.sa, SOCKADDR_UN_LEN(sa.un)) < 0) { |
2ba11090 | 2819 | if (!IN_SET(errno, EPIPE, EAGAIN, ENOENT, ECONNREFUSED, ECONNRESET, ECONNABORTED)) |
56f64d95 | 2820 | log_error_errno(errno, "connect() failed: %m"); |
2ba11090 | 2821 | return; |
e983b760 LP |
2822 | } |
2823 | ||
ac155bb8 | 2824 | if (asprintf(&message, "U\002%c%s%n", (int) (strlen(u->id) + 1), u->id, &n) < 0) { |
0d0f0c50 | 2825 | log_oom(); |
2ba11090 | 2826 | return; |
e983b760 LP |
2827 | } |
2828 | ||
2829 | errno = 0; | |
2ba11090 ZJS |
2830 | if (write(fd, message, n + 1) != n + 1) |
2831 | if (!IN_SET(errno, EPIPE, EAGAIN, ENOENT, ECONNREFUSED, ECONNRESET, ECONNABORTED)) | |
56f64d95 | 2832 | log_error_errno(errno, "Failed to write Plymouth message: %m"); |
e983b760 LP |
2833 | } |
2834 | ||
d8d5ab98 | 2835 | int manager_open_serialization(Manager *m, FILE **_f) { |
504afd7c | 2836 | int fd; |
a16e1123 LP |
2837 | FILE *f; |
2838 | ||
2839 | assert(_f); | |
2840 | ||
504afd7c ZJS |
2841 | fd = open_serialization_fd("systemd-state"); |
2842 | if (fd < 0) | |
2843 | return fd; | |
a16e1123 | 2844 | |
01e10de3 | 2845 | f = fdopen(fd, "w+"); |
d86f9d52 | 2846 | if (!f) { |
03e334a1 | 2847 | safe_close(fd); |
a16e1123 | 2848 | return -errno; |
d86f9d52 | 2849 | } |
a16e1123 LP |
2850 | |
2851 | *_f = f; | |
a16e1123 LP |
2852 | return 0; |
2853 | } | |
2854 | ||
b3680f49 | 2855 | int manager_serialize(Manager *m, FILE *f, FDSet *fds, bool switching_root) { |
9f9f0342 LP |
2856 | ManagerTimestamp q; |
2857 | const char *t; | |
a16e1123 LP |
2858 | Iterator i; |
2859 | Unit *u; | |
a16e1123 LP |
2860 | int r; |
2861 | ||
2862 | assert(m); | |
2863 | assert(f); | |
2864 | assert(fds); | |
2865 | ||
313cefa1 | 2866 | m->n_reloading++; |
38c52d46 | 2867 | |
1fa2f38f | 2868 | fprintf(f, "current-job-id=%"PRIu32"\n", m->current_job_id); |
33c5fae9 LP |
2869 | fprintf(f, "n-installed-jobs=%u\n", m->n_installed_jobs); |
2870 | fprintf(f, "n-failed-jobs=%u\n", m->n_failed_jobs); | |
0c2826c6 ZJS |
2871 | fprintf(f, "taint-usr=%s\n", yes_no(m->taint_usr)); |
2872 | fprintf(f, "ready-sent=%s\n", yes_no(m->ready_sent)); | |
d8eb10d6 | 2873 | fprintf(f, "taint-logged=%s\n", yes_no(m->taint_logged)); |
2a12e32e | 2874 | fprintf(f, "service-watchdogs=%s\n", yes_no(m->service_watchdogs)); |
01d67b43 | 2875 | |
9f9f0342 LP |
2876 | for (q = 0; q < _MANAGER_TIMESTAMP_MAX; q++) { |
2877 | /* The userspace and finish timestamps only apply to the host system, hence only serialize them there */ | |
2878 | if (in_initrd() && IN_SET(q, MANAGER_TIMESTAMP_USERSPACE, MANAGER_TIMESTAMP_FINISH)) | |
2879 | continue; | |
f38ed060 | 2880 | |
9f9f0342 LP |
2881 | t = manager_timestamp_to_string(q); |
2882 | { | |
fbd0b64f | 2883 | char field[strlen(t) + STRLEN("-timestamp") + 1]; |
9f9f0342 LP |
2884 | strcpy(stpcpy(field, t), "-timestamp"); |
2885 | dual_timestamp_serialize(f, field, m->timestamps + q); | |
2886 | } | |
f38ed060 | 2887 | } |
47a483a1 | 2888 | |
fe902fa4 ZJS |
2889 | if (!switching_root) |
2890 | (void) serialize_environment(f, m->environment); | |
4a9fd066 | 2891 | |
d86f9d52 LP |
2892 | if (m->notify_fd >= 0) { |
2893 | int copy; | |
2894 | ||
2895 | copy = fdset_put_dup(fds, m->notify_fd); | |
2896 | if (copy < 0) | |
2897 | return copy; | |
2898 | ||
2899 | fprintf(f, "notify-fd=%i\n", copy); | |
2900 | fprintf(f, "notify-socket=%s\n", m->notify_socket); | |
2901 | } | |
2902 | ||
d8fdc620 LP |
2903 | if (m->cgroups_agent_fd >= 0) { |
2904 | int copy; | |
2905 | ||
2906 | copy = fdset_put_dup(fds, m->cgroups_agent_fd); | |
2907 | if (copy < 0) | |
2908 | return copy; | |
2909 | ||
2910 | fprintf(f, "cgroups-agent-fd=%i\n", copy); | |
2911 | } | |
2912 | ||
00d9ef85 LP |
2913 | if (m->user_lookup_fds[0] >= 0) { |
2914 | int copy0, copy1; | |
2915 | ||
2916 | copy0 = fdset_put_dup(fds, m->user_lookup_fds[0]); | |
2917 | if (copy0 < 0) | |
2918 | return copy0; | |
2919 | ||
2920 | copy1 = fdset_put_dup(fds, m->user_lookup_fds[1]); | |
2921 | if (copy1 < 0) | |
2922 | return copy1; | |
2923 | ||
2924 | fprintf(f, "user-lookup=%i %i\n", copy0, copy1); | |
2925 | } | |
2926 | ||
05a98afd | 2927 | bus_track_serialize(m->subscribed, f, "subscribed"); |
6fa48533 | 2928 | |
29206d46 LP |
2929 | r = dynamic_user_serialize(m, f, fds); |
2930 | if (r < 0) | |
2931 | return r; | |
2932 | ||
00d9ef85 LP |
2933 | manager_serialize_uid_refs(m, f); |
2934 | manager_serialize_gid_refs(m, f); | |
2935 | ||
e8a565cb YW |
2936 | r = exec_runtime_serialize(m, f, fds); |
2937 | if (r < 0) | |
2938 | return r; | |
2939 | ||
0d536673 | 2940 | (void) fputc('\n', f); |
f2382a94 | 2941 | |
a16e1123 | 2942 | HASHMAP_FOREACH_KEY(u, t, m->units, i) { |
ac155bb8 | 2943 | if (u->id != t) |
a16e1123 LP |
2944 | continue; |
2945 | ||
a16e1123 | 2946 | /* Start marker */ |
0d536673 LP |
2947 | fputs(u->id, f); |
2948 | fputc('\n', f); | |
a16e1123 | 2949 | |
6fa48533 LP |
2950 | r = unit_serialize(u, f, fds, !switching_root); |
2951 | if (r < 0) { | |
313cefa1 | 2952 | m->n_reloading--; |
a16e1123 | 2953 | return r; |
38c52d46 | 2954 | } |
a16e1123 LP |
2955 | } |
2956 | ||
a7556052 | 2957 | assert(m->n_reloading > 0); |
313cefa1 | 2958 | m->n_reloading--; |
38c52d46 | 2959 | |
a16e1123 LP |
2960 | if (ferror(f)) |
2961 | return -EIO; | |
2962 | ||
b23de6af LP |
2963 | r = bus_fdset_add_all(m, fds); |
2964 | if (r < 0) | |
2965 | return r; | |
2966 | ||
a16e1123 LP |
2967 | return 0; |
2968 | } | |
2969 | ||
2970 | int manager_deserialize(Manager *m, FILE *f, FDSet *fds) { | |
2971 | int r = 0; | |
2972 | ||
2973 | assert(m); | |
2974 | assert(f); | |
2975 | ||
2976 | log_debug("Deserializing state..."); | |
2977 | ||
313cefa1 | 2978 | m->n_reloading++; |
82c64bf5 | 2979 | |
10f8e83c | 2980 | for (;;) { |
d233c99a ZJS |
2981 | char line[LINE_MAX]; |
2982 | const char *val, *l; | |
10f8e83c LP |
2983 | |
2984 | if (!fgets(line, sizeof(line), f)) { | |
2985 | if (feof(f)) | |
2986 | r = 0; | |
2987 | else | |
2988 | r = -errno; | |
2989 | ||
2990 | goto finish; | |
2991 | } | |
2992 | ||
2993 | char_array_0(line); | |
2994 | l = strstrip(line); | |
2995 | ||
2996 | if (l[0] == 0) | |
2997 | break; | |
2998 | ||
fb4650aa | 2999 | if ((val = startswith(l, "current-job-id="))) { |
01d67b43 LP |
3000 | uint32_t id; |
3001 | ||
fb4650aa | 3002 | if (safe_atou32(val, &id) < 0) |
62c460c6 | 3003 | log_notice("Failed to parse current job id value %s", val); |
01d67b43 LP |
3004 | else |
3005 | m->current_job_id = MAX(m->current_job_id, id); | |
718db961 | 3006 | |
fb4650aa | 3007 | } else if ((val = startswith(l, "n-installed-jobs="))) { |
33c5fae9 LP |
3008 | uint32_t n; |
3009 | ||
fb4650aa | 3010 | if (safe_atou32(val, &n) < 0) |
62c460c6 | 3011 | log_notice("Failed to parse installed jobs counter %s", val); |
33c5fae9 LP |
3012 | else |
3013 | m->n_installed_jobs += n; | |
718db961 | 3014 | |
fb4650aa | 3015 | } else if ((val = startswith(l, "n-failed-jobs="))) { |
33c5fae9 LP |
3016 | uint32_t n; |
3017 | ||
fb4650aa | 3018 | if (safe_atou32(val, &n) < 0) |
62c460c6 | 3019 | log_notice("Failed to parse failed jobs counter %s", val); |
33c5fae9 LP |
3020 | else |
3021 | m->n_failed_jobs += n; | |
718db961 | 3022 | |
fb4650aa | 3023 | } else if ((val = startswith(l, "taint-usr="))) { |
01d67b43 LP |
3024 | int b; |
3025 | ||
fb4650aa | 3026 | b = parse_boolean(val); |
e3dd987c | 3027 | if (b < 0) |
62c460c6 | 3028 | log_notice("Failed to parse taint /usr flag %s", val); |
01d67b43 LP |
3029 | else |
3030 | m->taint_usr = m->taint_usr || b; | |
718db961 | 3031 | |
0c2826c6 ZJS |
3032 | } else if ((val = startswith(l, "ready-sent="))) { |
3033 | int b; | |
3034 | ||
3035 | b = parse_boolean(val); | |
3036 | if (b < 0) | |
3037 | log_notice("Failed to parse ready-sent flag %s", val); | |
3038 | else | |
3039 | m->ready_sent = m->ready_sent || b; | |
3040 | ||
d8eb10d6 ZJS |
3041 | } else if ((val = startswith(l, "taint-logged="))) { |
3042 | int b; | |
3043 | ||
3044 | b = parse_boolean(val); | |
3045 | if (b < 0) | |
3046 | log_notice("Failed to parse taint-logged flag %s", val); | |
3047 | else | |
3048 | m->taint_logged = m->taint_logged || b; | |
3049 | ||
2a12e32e JK |
3050 | } else if ((val = startswith(l, "service-watchdogs="))) { |
3051 | int b; | |
3052 | ||
3053 | b = parse_boolean(val); | |
3054 | if (b < 0) | |
3055 | log_notice("Failed to parse service-watchdogs flag %s", val); | |
3056 | else | |
3057 | m->service_watchdogs = b; | |
3058 | ||
9f9f0342 | 3059 | } else if (startswith(l, "env=")) { |
fe902fa4 | 3060 | r = deserialize_environment(&m->environment, l); |
d233c99a ZJS |
3061 | if (r == -ENOMEM) |
3062 | goto finish; | |
527b7a42 | 3063 | if (r < 0) |
d233c99a | 3064 | log_notice_errno(r, "Failed to parse environment entry: \"%s\": %m", l); |
e3dd987c | 3065 | |
fb4650aa | 3066 | } else if ((val = startswith(l, "notify-fd="))) { |
d86f9d52 LP |
3067 | int fd; |
3068 | ||
fb4650aa | 3069 | if (safe_atoi(val, &fd) < 0 || fd < 0 || !fdset_contains(fds, fd)) |
62c460c6 | 3070 | log_notice("Failed to parse notify fd: \"%s\"", val); |
d86f9d52 | 3071 | else { |
03e334a1 LP |
3072 | m->notify_event_source = sd_event_source_unref(m->notify_event_source); |
3073 | safe_close(m->notify_fd); | |
d86f9d52 LP |
3074 | m->notify_fd = fdset_remove(fds, fd); |
3075 | } | |
3076 | ||
fb4650aa | 3077 | } else if ((val = startswith(l, "notify-socket="))) { |
d86f9d52 LP |
3078 | char *n; |
3079 | ||
fb4650aa | 3080 | n = strdup(val); |
d86f9d52 LP |
3081 | if (!n) { |
3082 | r = -ENOMEM; | |
3083 | goto finish; | |
3084 | } | |
3085 | ||
3086 | free(m->notify_socket); | |
3087 | m->notify_socket = n; | |
3088 | ||
fb4650aa | 3089 | } else if ((val = startswith(l, "cgroups-agent-fd="))) { |
d8fdc620 LP |
3090 | int fd; |
3091 | ||
fb4650aa | 3092 | if (safe_atoi(val, &fd) < 0 || fd < 0 || !fdset_contains(fds, fd)) |
62c460c6 | 3093 | log_notice("Failed to parse cgroups agent fd: %s", val); |
d8fdc620 LP |
3094 | else { |
3095 | m->cgroups_agent_event_source = sd_event_source_unref(m->cgroups_agent_event_source); | |
3096 | safe_close(m->cgroups_agent_fd); | |
3097 | m->cgroups_agent_fd = fdset_remove(fds, fd); | |
3098 | } | |
3099 | ||
fb4650aa | 3100 | } else if ((val = startswith(l, "user-lookup="))) { |
00d9ef85 LP |
3101 | int fd0, fd1; |
3102 | ||
fb4650aa | 3103 | if (sscanf(val, "%i %i", &fd0, &fd1) != 2 || fd0 < 0 || fd1 < 0 || fd0 == fd1 || !fdset_contains(fds, fd0) || !fdset_contains(fds, fd1)) |
62c460c6 | 3104 | log_notice("Failed to parse user lookup fd: %s", val); |
00d9ef85 LP |
3105 | else { |
3106 | m->user_lookup_event_source = sd_event_source_unref(m->user_lookup_event_source); | |
3107 | safe_close_pair(m->user_lookup_fds); | |
3108 | m->user_lookup_fds[0] = fdset_remove(fds, fd0); | |
3109 | m->user_lookup_fds[1] = fdset_remove(fds, fd1); | |
3110 | } | |
3111 | ||
fb4650aa ZJS |
3112 | } else if ((val = startswith(l, "dynamic-user="))) |
3113 | dynamic_user_deserialize_one(m, val, fds); | |
3114 | else if ((val = startswith(l, "destroy-ipc-uid="))) | |
3115 | manager_deserialize_uid_refs_one(m, val); | |
3116 | else if ((val = startswith(l, "destroy-ipc-gid="))) | |
3117 | manager_deserialize_gid_refs_one(m, val); | |
e8a565cb YW |
3118 | else if ((val = startswith(l, "exec-runtime="))) |
3119 | exec_runtime_deserialize_one(m, val, fds); | |
fb4650aa | 3120 | else if ((val = startswith(l, "subscribed="))) { |
05a98afd | 3121 | |
fb4650aa | 3122 | if (strv_extend(&m->deserialized_subscribed, val) < 0) |
05a98afd | 3123 | log_oom(); |
9f9f0342 LP |
3124 | } else { |
3125 | ManagerTimestamp q; | |
3126 | ||
3127 | for (q = 0; q < _MANAGER_TIMESTAMP_MAX; q++) { | |
3128 | val = startswith(l, manager_timestamp_to_string(q)); | |
3129 | if (!val) | |
3130 | continue; | |
05a98afd | 3131 | |
9f9f0342 LP |
3132 | val = startswith(val, "-timestamp="); |
3133 | if (val) | |
3134 | break; | |
3135 | } | |
05a98afd | 3136 | |
9f9f0342 LP |
3137 | if (q < _MANAGER_TIMESTAMP_MAX) /* found it */ |
3138 | dual_timestamp_deserialize(val, m->timestamps + q); | |
3139 | else if (!startswith(l, "kdbus-fd=")) /* ignore kdbus */ | |
3140 | log_notice("Unknown serialization item '%s'", l); | |
3141 | } | |
10f8e83c LP |
3142 | } |
3143 | ||
a16e1123 LP |
3144 | for (;;) { |
3145 | Unit *u; | |
3146 | char name[UNIT_NAME_MAX+2]; | |
07429866 | 3147 | const char* unit_name; |
a16e1123 LP |
3148 | |
3149 | /* Start marker */ | |
3150 | if (!fgets(name, sizeof(name), f)) { | |
3151 | if (feof(f)) | |
10f8e83c LP |
3152 | r = 0; |
3153 | else | |
3154 | r = -errno; | |
a16e1123 | 3155 | |
82c64bf5 | 3156 | goto finish; |
a16e1123 LP |
3157 | } |
3158 | ||
3159 | char_array_0(name); | |
07429866 | 3160 | unit_name = strstrip(name); |
a16e1123 | 3161 | |
07429866 ZJS |
3162 | r = manager_load_unit(m, unit_name, NULL, NULL, &u); |
3163 | if (r < 0) { | |
3164 | log_notice_errno(r, "Failed to load unit \"%s\", skipping deserialization: %m", unit_name); | |
3165 | if (r == -ENOMEM) | |
3166 | goto finish; | |
3167 | unit_deserialize_skip(f); | |
3168 | continue; | |
3169 | } | |
a16e1123 | 3170 | |
01e10de3 | 3171 | r = unit_deserialize(u, f, fds); |
07429866 ZJS |
3172 | if (r < 0) { |
3173 | log_notice_errno(r, "Failed to deserialize unit \"%s\": %m", unit_name); | |
3174 | if (r == -ENOMEM) | |
3175 | goto finish; | |
3176 | } | |
a16e1123 LP |
3177 | } |
3178 | ||
10f8e83c | 3179 | finish: |
145b1f79 | 3180 | if (ferror(f)) |
82c64bf5 | 3181 | r = -EIO; |
a16e1123 | 3182 | |
a7556052 | 3183 | assert(m->n_reloading > 0); |
313cefa1 | 3184 | m->n_reloading--; |
82c64bf5 LP |
3185 | |
3186 | return r; | |
a16e1123 LP |
3187 | } |
3188 | ||
a7a7163d DT |
3189 | static void manager_flush_finished_jobs(Manager *m) { |
3190 | Job *j; | |
3191 | ||
3192 | while ((j = set_steal_first(m->pending_finished_jobs))) { | |
3193 | bus_job_send_removed_signal(j); | |
3194 | job_free(j); | |
3195 | } | |
3196 | ||
3197 | m->pending_finished_jobs = set_free(m->pending_finished_jobs); | |
3198 | } | |
3199 | ||
a16e1123 LP |
3200 | int manager_reload(Manager *m) { |
3201 | int r, q; | |
51d122af ZJS |
3202 | _cleanup_fclose_ FILE *f = NULL; |
3203 | _cleanup_fdset_free_ FDSet *fds = NULL; | |
a16e1123 LP |
3204 | |
3205 | assert(m); | |
3206 | ||
07719a21 LP |
3207 | r = manager_open_serialization(m, &f); |
3208 | if (r < 0) | |
a16e1123 LP |
3209 | return r; |
3210 | ||
313cefa1 | 3211 | m->n_reloading++; |
718db961 | 3212 | bus_manager_send_reloading(m, true); |
38c52d46 | 3213 | |
07719a21 LP |
3214 | fds = fdset_new(); |
3215 | if (!fds) { | |
313cefa1 | 3216 | m->n_reloading--; |
51d122af | 3217 | return -ENOMEM; |
a16e1123 LP |
3218 | } |
3219 | ||
b3680f49 | 3220 | r = manager_serialize(m, f, fds, false); |
07719a21 | 3221 | if (r < 0) { |
313cefa1 | 3222 | m->n_reloading--; |
51d122af | 3223 | return r; |
38c52d46 | 3224 | } |
a16e1123 LP |
3225 | |
3226 | if (fseeko(f, 0, SEEK_SET) < 0) { | |
313cefa1 | 3227 | m->n_reloading--; |
51d122af | 3228 | return -errno; |
a16e1123 LP |
3229 | } |
3230 | ||
3231 | /* From here on there is no way back. */ | |
3232 | manager_clear_jobs_and_units(m); | |
07a78643 | 3233 | lookup_paths_flush_generator(&m->lookup_paths); |
84e3543e | 3234 | lookup_paths_free(&m->lookup_paths); |
e8a565cb | 3235 | exec_runtime_vacuum(m); |
29206d46 | 3236 | dynamic_user_vacuum(m, false); |
00d9ef85 LP |
3237 | m->uid_refs = hashmap_free(m->uid_refs); |
3238 | m->gid_refs = hashmap_free(m->gid_refs); | |
2ded0c04 | 3239 | |
4943d143 | 3240 | q = lookup_paths_init(&m->lookup_paths, m->unit_file_scope, 0, NULL); |
e801700e ZJS |
3241 | if (q < 0 && r >= 0) |
3242 | r = q; | |
5a1e9937 | 3243 | |
64691d20 ZJS |
3244 | q = manager_run_environment_generators(m); |
3245 | if (q < 0 && r >= 0) | |
3246 | r = q; | |
3247 | ||
a3c4eb07 LP |
3248 | /* Find new unit paths */ |
3249 | q = manager_run_generators(m); | |
e801700e | 3250 | if (q < 0 && r >= 0) |
07719a21 LP |
3251 | r = q; |
3252 | ||
a1453343 | 3253 | lookup_paths_reduce(&m->lookup_paths); |
5a1e9937 LP |
3254 | manager_build_unit_path_cache(m); |
3255 | ||
a16e1123 | 3256 | /* First, enumerate what we can from all config files */ |
ba64af90 | 3257 | manager_enumerate(m); |
a16e1123 LP |
3258 | |
3259 | /* Second, deserialize our stored data */ | |
07719a21 | 3260 | q = manager_deserialize(m, f, fds); |
07429866 ZJS |
3261 | if (q < 0) { |
3262 | log_error_errno(q, "Deserialization failed: %m"); | |
3263 | ||
3264 | if (r >= 0) | |
3265 | r = q; | |
3266 | } | |
a16e1123 LP |
3267 | |
3268 | fclose(f); | |
3269 | f = NULL; | |
3270 | ||
a2cc4a6c ZJS |
3271 | /* Re-register notify_fd as event source */ |
3272 | q = manager_setup_notify(m); | |
e801700e | 3273 | if (q < 0 && r >= 0) |
a2cc4a6c ZJS |
3274 | r = q; |
3275 | ||
d8fdc620 LP |
3276 | q = manager_setup_cgroups_agent(m); |
3277 | if (q < 0 && r >= 0) | |
3278 | r = q; | |
3279 | ||
00d9ef85 LP |
3280 | q = manager_setup_user_lookup_fd(m); |
3281 | if (q < 0 && r >= 0) | |
3282 | r = q; | |
3283 | ||
a16e1123 | 3284 | /* Third, fire things up! */ |
007c6337 | 3285 | manager_coldplug(m); |
a16e1123 | 3286 | |
29206d46 LP |
3287 | /* Release any dynamic users no longer referenced */ |
3288 | dynamic_user_vacuum(m, true); | |
3289 | ||
00d9ef85 LP |
3290 | /* Release any references to UIDs/GIDs no longer referenced, and destroy any IPC owned by them */ |
3291 | manager_vacuum_uid_refs(m); | |
3292 | manager_vacuum_gid_refs(m); | |
3293 | ||
e8a565cb YW |
3294 | exec_runtime_vacuum(m); |
3295 | ||
31dc1ca3 LP |
3296 | assert(m->n_reloading > 0); |
3297 | m->n_reloading--; | |
3298 | ||
8559b3b7 | 3299 | /* It might be safe to log to the journal now and connect to dbus */ |
d075092f | 3300 | manager_recheck_journal(m); |
8559b3b7 | 3301 | manager_recheck_dbus(m); |
d075092f | 3302 | |
8936a5e3 | 3303 | /* Sync current state of bus names with our set of listening units */ |
5f109056 LP |
3304 | q = manager_enqueue_sync_bus_names(m); |
3305 | if (q < 0 && r >= 0) | |
3306 | r = q; | |
8936a5e3 | 3307 | |
a7a7163d DT |
3308 | if (!MANAGER_IS_RELOADING(m)) |
3309 | manager_flush_finished_jobs(m); | |
3310 | ||
71445ae7 LP |
3311 | m->send_reloading_done = true; |
3312 | ||
a16e1123 LP |
3313 | return r; |
3314 | } | |
3315 | ||
fdf20a31 | 3316 | void manager_reset_failed(Manager *m) { |
5632e374 LP |
3317 | Unit *u; |
3318 | Iterator i; | |
3319 | ||
3320 | assert(m); | |
3321 | ||
3322 | HASHMAP_FOREACH(u, m->units, i) | |
fdf20a31 | 3323 | unit_reset_failed(u); |
5632e374 LP |
3324 | } |
3325 | ||
31afa0a4 | 3326 | bool manager_unit_inactive_or_pending(Manager *m, const char *name) { |
8f6df3fa LP |
3327 | Unit *u; |
3328 | ||
3329 | assert(m); | |
3330 | assert(name); | |
3331 | ||
3332 | /* Returns true if the unit is inactive or going down */ | |
bd0af849 ZJS |
3333 | u = manager_get_unit(m, name); |
3334 | if (!u) | |
8f6df3fa LP |
3335 | return true; |
3336 | ||
31afa0a4 | 3337 | return unit_inactive_or_pending(u); |
8f6df3fa LP |
3338 | } |
3339 | ||
d8eb10d6 ZJS |
3340 | static void log_taint_string(Manager *m) { |
3341 | _cleanup_free_ char *taint = NULL; | |
3342 | ||
3343 | assert(m); | |
3344 | ||
3345 | if (MANAGER_IS_USER(m) || m->taint_logged) | |
3346 | return; | |
3347 | ||
3348 | m->taint_logged = true; /* only check for taint once */ | |
3349 | ||
3350 | taint = manager_taint_string(m); | |
3351 | if (isempty(taint)) | |
3352 | return; | |
3353 | ||
3354 | log_struct(LOG_NOTICE, | |
3355 | LOG_MESSAGE("System is tainted: %s", taint), | |
3356 | "TAINT=%s", taint, | |
3357 | "MESSAGE_ID=" SD_MESSAGE_TAINTED_STR, | |
3358 | NULL); | |
3359 | } | |
3360 | ||
56dacdbc | 3361 | static void manager_notify_finished(Manager *m) { |
7ceba241 | 3362 | char userspace[FORMAT_TIMESPAN_MAX], initrd[FORMAT_TIMESPAN_MAX], kernel[FORMAT_TIMESPAN_MAX], sum[FORMAT_TIMESPAN_MAX]; |
915b3753 | 3363 | usec_t firmware_usec, loader_usec, kernel_usec, initrd_usec, userspace_usec, total_usec; |
b0c918b9 | 3364 | |
e0a3da1f | 3365 | if (m->test_run_flags) |
b0c918b9 LP |
3366 | return; |
3367 | ||
463d0d15 | 3368 | if (MANAGER_IS_SYSTEM(m) && detect_container() <= 0) { |
dd1db3c2 | 3369 | char ts[FORMAT_TIMESPAN_MAX]; |
dc3c9f5e ZJS |
3370 | char buf[FORMAT_TIMESPAN_MAX + STRLEN(" (firmware) + ") + FORMAT_TIMESPAN_MAX + STRLEN(" (loader) + ")] |
3371 | = {}; | |
3372 | char *p = buf; | |
3373 | size_t size = sizeof buf; | |
e03ae661 | 3374 | |
9f9f0342 LP |
3375 | /* Note that MANAGER_TIMESTAMP_KERNEL's monotonic value is always at 0, and |
3376 | * MANAGER_TIMESTAMP_FIRMWARE's and MANAGER_TIMESTAMP_LOADER's monotonic value should be considered | |
915b3753 LP |
3377 | * negative values. */ |
3378 | ||
9f9f0342 LP |
3379 | firmware_usec = m->timestamps[MANAGER_TIMESTAMP_FIRMWARE].monotonic - m->timestamps[MANAGER_TIMESTAMP_LOADER].monotonic; |
3380 | loader_usec = m->timestamps[MANAGER_TIMESTAMP_LOADER].monotonic - m->timestamps[MANAGER_TIMESTAMP_KERNEL].monotonic; | |
3381 | userspace_usec = m->timestamps[MANAGER_TIMESTAMP_FINISH].monotonic - m->timestamps[MANAGER_TIMESTAMP_USERSPACE].monotonic; | |
3382 | total_usec = m->timestamps[MANAGER_TIMESTAMP_FIRMWARE].monotonic + m->timestamps[MANAGER_TIMESTAMP_FINISH].monotonic; | |
18fa6b27 | 3383 | |
dd1db3c2 YW |
3384 | if (firmware_usec > 0) |
3385 | size = strpcpyf(&p, size, "%s (firmware) + ", format_timespan(ts, sizeof(ts), firmware_usec, USEC_PER_MSEC)); | |
3386 | if (loader_usec > 0) | |
3387 | size = strpcpyf(&p, size, "%s (loader) + ", format_timespan(ts, sizeof(ts), loader_usec, USEC_PER_MSEC)); | |
3388 | ||
9f9f0342 | 3389 | if (dual_timestamp_is_set(&m->timestamps[MANAGER_TIMESTAMP_INITRD])) { |
18fa6b27 | 3390 | |
9f9f0342 LP |
3391 | /* The initrd case on bare-metal*/ |
3392 | kernel_usec = m->timestamps[MANAGER_TIMESTAMP_INITRD].monotonic - m->timestamps[MANAGER_TIMESTAMP_KERNEL].monotonic; | |
3393 | initrd_usec = m->timestamps[MANAGER_TIMESTAMP_USERSPACE].monotonic - m->timestamps[MANAGER_TIMESTAMP_INITRD].monotonic; | |
18fa6b27 | 3394 | |
e12919e8 | 3395 | log_struct(LOG_INFO, |
2b044526 | 3396 | "MESSAGE_ID=" SD_MESSAGE_STARTUP_FINISHED_STR, |
e12919e8 LP |
3397 | "KERNEL_USEC="USEC_FMT, kernel_usec, |
3398 | "INITRD_USEC="USEC_FMT, initrd_usec, | |
3399 | "USERSPACE_USEC="USEC_FMT, userspace_usec, | |
dd1db3c2 YW |
3400 | LOG_MESSAGE("Startup finished in %s%s (kernel) + %s (initrd) + %s (userspace) = %s.", |
3401 | buf, | |
e2cc6eca LP |
3402 | format_timespan(kernel, sizeof(kernel), kernel_usec, USEC_PER_MSEC), |
3403 | format_timespan(initrd, sizeof(initrd), initrd_usec, USEC_PER_MSEC), | |
3404 | format_timespan(userspace, sizeof(userspace), userspace_usec, USEC_PER_MSEC), | |
3405 | format_timespan(sum, sizeof(sum), total_usec, USEC_PER_MSEC)), | |
e12919e8 | 3406 | NULL); |
18fa6b27 | 3407 | } else { |
9f9f0342 LP |
3408 | /* The initrd-less case on bare-metal*/ |
3409 | ||
3410 | kernel_usec = m->timestamps[MANAGER_TIMESTAMP_USERSPACE].monotonic - m->timestamps[MANAGER_TIMESTAMP_KERNEL].monotonic; | |
18fa6b27 LP |
3411 | initrd_usec = 0; |
3412 | ||
81270860 | 3413 | log_struct(LOG_INFO, |
2b044526 | 3414 | "MESSAGE_ID=" SD_MESSAGE_STARTUP_FINISHED_STR, |
e12919e8 | 3415 | "KERNEL_USEC="USEC_FMT, kernel_usec, |
ccd06097 | 3416 | "USERSPACE_USEC="USEC_FMT, userspace_usec, |
dd1db3c2 YW |
3417 | LOG_MESSAGE("Startup finished in %s%s (kernel) + %s (userspace) = %s.", |
3418 | buf, | |
e2cc6eca LP |
3419 | format_timespan(kernel, sizeof(kernel), kernel_usec, USEC_PER_MSEC), |
3420 | format_timespan(userspace, sizeof(userspace), userspace_usec, USEC_PER_MSEC), | |
3421 | format_timespan(sum, sizeof(sum), total_usec, USEC_PER_MSEC)), | |
81270860 | 3422 | NULL); |
e12919e8 LP |
3423 | } |
3424 | } else { | |
4adf314b | 3425 | /* The container and --user case */ |
e12919e8 | 3426 | firmware_usec = loader_usec = initrd_usec = kernel_usec = 0; |
9f9f0342 | 3427 | total_usec = userspace_usec = m->timestamps[MANAGER_TIMESTAMP_FINISH].monotonic - m->timestamps[MANAGER_TIMESTAMP_USERSPACE].monotonic; |
e12919e8 LP |
3428 | |
3429 | log_struct(LOG_INFO, | |
2b044526 | 3430 | "MESSAGE_ID=" SD_MESSAGE_USER_STARTUP_FINISHED_STR, |
e12919e8 | 3431 | "USERSPACE_USEC="USEC_FMT, userspace_usec, |
e2cc6eca LP |
3432 | LOG_MESSAGE("Startup finished in %s.", |
3433 | format_timespan(sum, sizeof(sum), total_usec, USEC_PER_MSEC)), | |
e12919e8 | 3434 | NULL); |
18fa6b27 | 3435 | } |
b0c918b9 | 3436 | |
718db961 | 3437 | bus_manager_send_finished(m, firmware_usec, loader_usec, kernel_usec, initrd_usec, userspace_usec, total_usec); |
530345e7 LP |
3438 | |
3439 | sd_notifyf(false, | |
0c2826c6 ZJS |
3440 | m->ready_sent ? "STATUS=Startup finished in %s." |
3441 | : "READY=1\n" | |
3442 | "STATUS=Startup finished in %s.", | |
2fa4092c | 3443 | format_timespan(sum, sizeof(sum), total_usec, USEC_PER_MSEC)); |
0c2826c6 | 3444 | m->ready_sent = true; |
d8eb10d6 ZJS |
3445 | |
3446 | log_taint_string(m); | |
b0c918b9 LP |
3447 | } |
3448 | ||
4adf314b LP |
3449 | static void manager_send_ready(Manager *m) { |
3450 | assert(m); | |
3451 | ||
3452 | /* We send READY=1 on reaching basic.target only when running in --user mode. */ | |
3453 | if (!MANAGER_IS_USER(m) || m->ready_sent) | |
3454 | return; | |
3455 | ||
3456 | m->ready_sent = true; | |
3457 | ||
3458 | sd_notifyf(false, | |
3459 | "READY=1\n" | |
3460 | "STATUS=Reached " SPECIAL_BASIC_TARGET "."); | |
3461 | } | |
3462 | ||
3463 | static void manager_check_basic_target(Manager *m) { | |
3464 | Unit *u; | |
3465 | ||
3466 | assert(m); | |
3467 | ||
3468 | /* Small shortcut */ | |
3469 | if (m->ready_sent && m->taint_logged) | |
3470 | return; | |
3471 | ||
3472 | u = manager_get_unit(m, SPECIAL_BASIC_TARGET); | |
3473 | if (!u || !UNIT_IS_ACTIVE_OR_RELOADING(unit_active_state(u))) | |
3474 | return; | |
3475 | ||
3476 | /* For user managers, send out READY=1 as soon as we reach basic.target */ | |
3477 | manager_send_ready(m); | |
3478 | ||
3479 | /* Log the taint string as soon as we reach basic.target */ | |
3480 | log_taint_string(m); | |
3481 | } | |
3482 | ||
56dacdbc | 3483 | void manager_check_finished(Manager *m) { |
56dacdbc ZJS |
3484 | assert(m); |
3485 | ||
2c289ea8 | 3486 | if (MANAGER_IS_RELOADING(m)) |
aad1976f LP |
3487 | return; |
3488 | ||
4259d202 LP |
3489 | /* Verify that we have entered the event loop already, and not left it again. */ |
3490 | if (!MANAGER_IS_RUNNING(m)) | |
9771b62d LP |
3491 | return; |
3492 | ||
4adf314b | 3493 | manager_check_basic_target(m); |
0c2826c6 | 3494 | |
56dacdbc | 3495 | if (hashmap_size(m->jobs) > 0) { |
56dacdbc | 3496 | if (m->jobs_in_progress_event_source) |
2ae56591 | 3497 | /* Ignore any failure, this is only for feedback */ |
e7ab4d1a | 3498 | (void) sd_event_source_set_time(m->jobs_in_progress_event_source, now(CLOCK_MONOTONIC) + JOBS_IN_PROGRESS_WAIT_USEC); |
56dacdbc ZJS |
3499 | |
3500 | return; | |
3501 | } | |
3502 | ||
3503 | manager_flip_auto_status(m, false); | |
3504 | ||
3505 | /* Notify Type=idle units that we are done now */ | |
56dacdbc ZJS |
3506 | manager_close_idle_pipe(m); |
3507 | ||
3508 | /* Turn off confirm spawn now */ | |
7d5ceb64 | 3509 | m->confirm_spawn = NULL; |
56dacdbc ZJS |
3510 | |
3511 | /* No need to update ask password status when we're going non-interactive */ | |
3512 | manager_close_ask_password(m); | |
3513 | ||
3514 | /* This is no longer the first boot */ | |
3515 | manager_set_first_boot(m, false); | |
3516 | ||
49d5666c | 3517 | if (MANAGER_IS_FINISHED(m)) |
56dacdbc ZJS |
3518 | return; |
3519 | ||
9f9f0342 | 3520 | dual_timestamp_get(m->timestamps + MANAGER_TIMESTAMP_FINISH); |
56dacdbc ZJS |
3521 | |
3522 | manager_notify_finished(m); | |
3523 | ||
e7ab4d1a | 3524 | manager_invalidate_startup_units(m); |
56dacdbc ZJS |
3525 | } |
3526 | ||
64691d20 ZJS |
3527 | static bool generator_path_any(const char* const* paths) { |
3528 | char **path; | |
3529 | bool found = false; | |
3530 | ||
3531 | /* Optimize by skipping the whole process by not creating output directories | |
3532 | * if no generators are found. */ | |
3533 | STRV_FOREACH(path, (char**) paths) | |
3534 | if (access(*path, F_OK) == 0) | |
3535 | found = true; | |
3536 | else if (errno != ENOENT) | |
3537 | log_warning_errno(errno, "Failed to open generator directory %s: %m", *path); | |
3538 | ||
3539 | return found; | |
3540 | } | |
3541 | ||
3542 | static const char* system_env_generator_binary_paths[] = { | |
3543 | "/run/systemd/system-environment-generators", | |
3544 | "/etc/systemd/system-environment-generators", | |
3545 | "/usr/local/lib/systemd/system-environment-generators", | |
3546 | SYSTEM_ENV_GENERATOR_PATH, | |
3547 | NULL | |
3548 | }; | |
3549 | ||
3550 | static const char* user_env_generator_binary_paths[] = { | |
3551 | "/run/systemd/user-environment-generators", | |
3552 | "/etc/systemd/user-environment-generators", | |
3553 | "/usr/local/lib/systemd/user-environment-generators", | |
3554 | USER_ENV_GENERATOR_PATH, | |
3555 | NULL | |
3556 | }; | |
3557 | ||
3558 | static int manager_run_environment_generators(Manager *m) { | |
3559 | char **tmp = NULL; /* this is only used in the forked process, no cleanup here */ | |
3560 | const char **paths; | |
3561 | void* args[] = {&tmp, &tmp, &m->environment}; | |
3562 | ||
e0a3da1f ZJS |
3563 | if (m->test_run_flags && !(m->test_run_flags & MANAGER_TEST_RUN_ENV_GENERATORS)) |
3564 | return 0; | |
3565 | ||
64691d20 ZJS |
3566 | paths = MANAGER_IS_SYSTEM(m) ? system_env_generator_binary_paths : user_env_generator_binary_paths; |
3567 | ||
3568 | if (!generator_path_any(paths)) | |
3569 | return 0; | |
3570 | ||
3571 | return execute_directories(paths, DEFAULT_TIMEOUT_USEC, gather_environment, args, NULL); | |
3572 | } | |
3573 | ||
e801700e | 3574 | static int manager_run_generators(Manager *m) { |
f42348ac | 3575 | _cleanup_strv_free_ char **paths = NULL; |
07719a21 | 3576 | const char *argv[5]; |
07719a21 | 3577 | int r; |
5a1e9937 LP |
3578 | |
3579 | assert(m); | |
3580 | ||
e0a3da1f ZJS |
3581 | if (m->test_run_flags && !(m->test_run_flags & MANAGER_TEST_RUN_GENERATORS)) |
3582 | return 0; | |
3583 | ||
9183df70 | 3584 | paths = generator_binary_paths(m->unit_file_scope); |
e801700e ZJS |
3585 | if (!paths) |
3586 | return log_oom(); | |
5a1e9937 | 3587 | |
64691d20 ZJS |
3588 | if (!generator_path_any((const char* const*) paths)) |
3589 | return 0; | |
5a1e9937 | 3590 | |
cd64fd56 | 3591 | r = lookup_paths_mkdir_generator(&m->lookup_paths); |
07719a21 LP |
3592 | if (r < 0) |
3593 | goto finish; | |
5a1e9937 | 3594 | |
83cc030f | 3595 | argv[0] = NULL; /* Leave this empty, execute_directory() will fill something in */ |
a3c4eb07 LP |
3596 | argv[1] = m->lookup_paths.generator; |
3597 | argv[2] = m->lookup_paths.generator_early; | |
3598 | argv[3] = m->lookup_paths.generator_late; | |
07719a21 | 3599 | argv[4] = NULL; |
5a1e9937 | 3600 | |
718db961 | 3601 | RUN_WITH_UMASK(0022) |
c6e47247 ZJS |
3602 | execute_directories((const char* const*) paths, DEFAULT_TIMEOUT_USEC, |
3603 | NULL, NULL, (char**) argv); | |
5a1e9937 | 3604 | |
718db961 | 3605 | finish: |
cd64fd56 | 3606 | lookup_paths_trim_generator(&m->lookup_paths); |
e801700e | 3607 | return r; |
5a1e9937 LP |
3608 | } |
3609 | ||
718db961 LP |
3610 | int manager_environment_add(Manager *m, char **minus, char **plus) { |
3611 | char **a = NULL, **b = NULL, **l; | |
97d0e5f8 | 3612 | assert(m); |
bcd8e6d1 | 3613 | |
718db961 | 3614 | l = m->environment; |
bcd8e6d1 | 3615 | |
718db961 LP |
3616 | if (!strv_isempty(minus)) { |
3617 | a = strv_env_delete(l, 1, minus); | |
3618 | if (!a) | |
3619 | return -ENOMEM; | |
3620 | ||
3621 | l = a; | |
3622 | } | |
3623 | ||
3624 | if (!strv_isempty(plus)) { | |
3625 | b = strv_env_merge(2, l, plus); | |
aa9f8a30 AH |
3626 | if (!b) { |
3627 | strv_free(a); | |
718db961 | 3628 | return -ENOMEM; |
aa9f8a30 | 3629 | } |
bcd8e6d1 | 3630 | |
718db961 LP |
3631 | l = b; |
3632 | } | |
3633 | ||
3634 | if (m->environment != l) | |
3635 | strv_free(m->environment); | |
3636 | if (a != l) | |
3637 | strv_free(a); | |
3638 | if (b != l) | |
3639 | strv_free(b); | |
3640 | ||
f069efb4 | 3641 | m->environment = l; |
47cf8ff2 | 3642 | manager_sanitize_environment(m); |
f069efb4 | 3643 | |
97d0e5f8 UTL |
3644 | return 0; |
3645 | } | |
3646 | ||
c93ff2e9 FC |
3647 | int manager_set_default_rlimits(Manager *m, struct rlimit **default_rlimit) { |
3648 | int i; | |
3649 | ||
3650 | assert(m); | |
3651 | ||
517d56b1 | 3652 | for (i = 0; i < _RLIMIT_MAX; i++) { |
d9814c76 EV |
3653 | m->rlimit[i] = mfree(m->rlimit[i]); |
3654 | ||
07719a21 LP |
3655 | if (!default_rlimit[i]) |
3656 | continue; | |
c93ff2e9 | 3657 | |
07719a21 LP |
3658 | m->rlimit[i] = newdup(struct rlimit, default_rlimit[i], 1); |
3659 | if (!m->rlimit[i]) | |
3ce40911 | 3660 | return log_oom(); |
c93ff2e9 FC |
3661 | } |
3662 | ||
3663 | return 0; | |
3664 | } | |
3665 | ||
8559b3b7 LP |
3666 | void manager_recheck_dbus(Manager *m) { |
3667 | assert(m); | |
3668 | ||
3669 | /* Connects to the bus if the dbus service and socket are running. If we are running in user mode this is all | |
3670 | * it does. In system mode we'll also connect to the system bus (which will most likely just reuse the | |
3671 | * connection of the API bus). That's because the system bus after all runs as service of the system instance, | |
3672 | * while in the user instance we can assume it's already there. */ | |
3673 | ||
31dc1ca3 LP |
3674 | if (MANAGER_IS_RELOADING(m)) |
3675 | return; /* don't check while we are reloading… */ | |
3676 | ||
8559b3b7 LP |
3677 | if (manager_dbus_is_running(m, false)) { |
3678 | (void) bus_init_api(m); | |
3679 | ||
3680 | if (MANAGER_IS_SYSTEM(m)) | |
3681 | (void) bus_init_system(m); | |
3682 | } else { | |
3683 | (void) bus_done_api(m); | |
3684 | ||
3685 | if (MANAGER_IS_SYSTEM(m)) | |
3686 | (void) bus_done_system(m); | |
3687 | } | |
3688 | } | |
3689 | ||
d075092f | 3690 | static bool manager_journal_is_running(Manager *m) { |
f1dd0c3f LP |
3691 | Unit *u; |
3692 | ||
3693 | assert(m); | |
3694 | ||
7d814a19 LP |
3695 | if (m->test_run_flags != 0) |
3696 | return false; | |
3697 | ||
d075092f | 3698 | /* If we are the user manager we can safely assume that the journal is up */ |
463d0d15 | 3699 | if (!MANAGER_IS_SYSTEM(m)) |
d075092f | 3700 | return true; |
f1dd0c3f | 3701 | |
d075092f | 3702 | /* Check that the socket is not only up, but in RUNNING state */ |
731a676c | 3703 | u = manager_get_unit(m, SPECIAL_JOURNALD_SOCKET); |
d075092f LP |
3704 | if (!u) |
3705 | return false; | |
3706 | if (SOCKET(u)->state != SOCKET_RUNNING) | |
3707 | return false; | |
f1dd0c3f | 3708 | |
d075092f | 3709 | /* Similar, check if the daemon itself is fully up, too */ |
731a676c | 3710 | u = manager_get_unit(m, SPECIAL_JOURNALD_SERVICE); |
d075092f LP |
3711 | if (!u) |
3712 | return false; | |
217677ab | 3713 | if (!IN_SET(SERVICE(u)->state, SERVICE_RELOAD, SERVICE_RUNNING)) |
d075092f LP |
3714 | return false; |
3715 | ||
3716 | return true; | |
3717 | } | |
3718 | ||
3719 | void manager_recheck_journal(Manager *m) { | |
3720 | ||
3721 | assert(m); | |
3722 | ||
3723 | /* Don't bother with this unless we are in the special situation of being PID 1 */ | |
3724 | if (getpid_cached() != 1) | |
731a676c | 3725 | return; |
f1dd0c3f | 3726 | |
31dc1ca3 LP |
3727 | /* Don't check this while we are reloading, things might still change */ |
3728 | if (MANAGER_IS_RELOADING(m)) | |
3729 | return; | |
3730 | ||
cedf5088 LP |
3731 | /* The journal is fully and entirely up? If so, let's permit logging to it, if that's configured. If the |
3732 | * journal is down, don't ever log to it, otherwise we might end up deadlocking ourselves as we might trigger | |
3733 | * an activation ourselves we can't fulfill. */ | |
3734 | log_set_prohibit_ipc(!manager_journal_is_running(m)); | |
cc2b9e6b | 3735 | log_open(); |
f1dd0c3f LP |
3736 | } |
3737 | ||
d450b6f2 | 3738 | void manager_set_show_status(Manager *m, ShowStatus mode) { |
27d340c7 | 3739 | assert(m); |
d450b6f2 | 3740 | assert(IN_SET(mode, SHOW_STATUS_AUTO, SHOW_STATUS_NO, SHOW_STATUS_YES, SHOW_STATUS_TEMPORARY)); |
27d340c7 | 3741 | |
463d0d15 | 3742 | if (!MANAGER_IS_SYSTEM(m)) |
27d340c7 LP |
3743 | return; |
3744 | ||
76b6f3f6 ZJS |
3745 | if (m->show_status != mode) |
3746 | log_debug("%s showing of status.", | |
3747 | mode == SHOW_STATUS_NO ? "Disabling" : "Enabling"); | |
d450b6f2 | 3748 | m->show_status = mode; |
27d340c7 | 3749 | |
d450b6f2 | 3750 | if (mode > 0) |
ac5b0c13 | 3751 | (void) touch("/run/systemd/show-status"); |
27d340c7 | 3752 | else |
ac5b0c13 | 3753 | (void) unlink("/run/systemd/show-status"); |
27d340c7 LP |
3754 | } |
3755 | ||
127d5fd1 | 3756 | static bool manager_get_show_status(Manager *m, StatusType type) { |
27d340c7 LP |
3757 | assert(m); |
3758 | ||
463d0d15 | 3759 | if (!MANAGER_IS_SYSTEM(m)) |
27d340c7 LP |
3760 | return false; |
3761 | ||
31a7eb86 ZJS |
3762 | if (m->no_console_output) |
3763 | return false; | |
3764 | ||
d81afec1 | 3765 | if (!IN_SET(manager_state(m), MANAGER_INITIALIZING, MANAGER_STARTING, MANAGER_STOPPING)) |
08510627 LP |
3766 | return false; |
3767 | ||
e46b13c8 | 3768 | /* If we cannot find out the status properly, just proceed. */ |
ebc5788e | 3769 | if (type != STATUS_TYPE_EMERGENCY && manager_check_ask_password(m) > 0) |
e46b13c8 ZJS |
3770 | return false; |
3771 | ||
46fb617b | 3772 | return m->show_status > 0; |
27d340c7 | 3773 | } |
68b29a9f | 3774 | |
7d5ceb64 FB |
3775 | const char *manager_get_confirm_spawn(Manager *m) { |
3776 | static int last_errno = 0; | |
3777 | const char *vc = m->confirm_spawn; | |
3778 | struct stat st; | |
3779 | int r; | |
3780 | ||
3781 | /* Here's the deal: we want to test the validity of the console but don't want | |
3782 | * PID1 to go through the whole console process which might block. But we also | |
3783 | * want to warn the user only once if something is wrong with the console so we | |
3784 | * cannot do the sanity checks after spawning our children. So here we simply do | |
3785 | * really basic tests to hopefully trap common errors. | |
3786 | * | |
3787 | * If the console suddenly disappear at the time our children will really it | |
3788 | * then they will simply fail to acquire it and a positive answer will be | |
3789 | * assumed. New children will fallback to /dev/console though. | |
3790 | * | |
3791 | * Note: TTYs are devices that can come and go any time, and frequently aren't | |
3792 | * available yet during early boot (consider a USB rs232 dongle...). If for any | |
3793 | * reason the configured console is not ready, we fallback to the default | |
3794 | * console. */ | |
3795 | ||
3796 | if (!vc || path_equal(vc, "/dev/console")) | |
3797 | return vc; | |
3798 | ||
3799 | r = stat(vc, &st); | |
3800 | if (r < 0) | |
3801 | goto fail; | |
3802 | ||
3803 | if (!S_ISCHR(st.st_mode)) { | |
3804 | errno = ENOTTY; | |
3805 | goto fail; | |
3806 | } | |
3807 | ||
3808 | last_errno = 0; | |
3809 | return vc; | |
3810 | fail: | |
3811 | if (last_errno != errno) { | |
3812 | last_errno = errno; | |
3813 | log_warning_errno(errno, "Failed to open %s: %m, using default console", vc); | |
3814 | } | |
3815 | return "/dev/console"; | |
3816 | } | |
3817 | ||
e2680723 LP |
3818 | void manager_set_first_boot(Manager *m, bool b) { |
3819 | assert(m); | |
3820 | ||
463d0d15 | 3821 | if (!MANAGER_IS_SYSTEM(m)) |
e2680723 LP |
3822 | return; |
3823 | ||
ae2a2c53 LP |
3824 | if (m->first_boot != (int) b) { |
3825 | if (b) | |
3826 | (void) touch("/run/systemd/first-boot"); | |
3827 | else | |
3828 | (void) unlink("/run/systemd/first-boot"); | |
3829 | } | |
e2680723 | 3830 | |
ae2a2c53 | 3831 | m->first_boot = b; |
e2680723 LP |
3832 | } |
3833 | ||
b0eb2944 FB |
3834 | void manager_disable_confirm_spawn(void) { |
3835 | (void) touch("/run/systemd/confirm_spawn_disabled"); | |
3836 | } | |
3837 | ||
3838 | bool manager_is_confirm_spawn_disabled(Manager *m) { | |
3839 | if (!m->confirm_spawn) | |
3840 | return true; | |
3841 | ||
3842 | return access("/run/systemd/confirm_spawn_disabled", F_OK) >= 0; | |
3843 | } | |
3844 | ||
127d5fd1 | 3845 | void manager_status_printf(Manager *m, StatusType type, const char *status, const char *format, ...) { |
25cee550 MS |
3846 | va_list ap; |
3847 | ||
cb6531be ZJS |
3848 | /* If m is NULL, assume we're after shutdown and let the messages through. */ |
3849 | ||
3850 | if (m && !manager_get_show_status(m, type)) | |
25cee550 MS |
3851 | return; |
3852 | ||
03b717a3 MS |
3853 | /* XXX We should totally drop the check for ephemeral here |
3854 | * and thus effectively make 'Type=idle' pointless. */ | |
cb6531be | 3855 | if (type == STATUS_TYPE_EPHEMERAL && m && m->n_on_console > 0) |
03b717a3 MS |
3856 | return; |
3857 | ||
25cee550 | 3858 | va_start(ap, format); |
127d5fd1 | 3859 | status_vprintf(status, true, type == STATUS_TYPE_EPHEMERAL, format, ap); |
25cee550 MS |
3860 | va_end(ap); |
3861 | } | |
3862 | ||
a57f7e2c LP |
3863 | Set *manager_get_units_requiring_mounts_for(Manager *m, const char *path) { |
3864 | char p[strlen(path)+1]; | |
3865 | ||
3866 | assert(m); | |
3867 | assert(path); | |
3868 | ||
3869 | strcpy(p, path); | |
858d36c1 | 3870 | path_simplify(p, false); |
a57f7e2c LP |
3871 | |
3872 | return hashmap_get(m->units_requiring_mounts_for, streq(p, "/") ? "" : p); | |
3873 | } | |
e66cf1a3 | 3874 | |
5269eb6b | 3875 | int manager_update_failed_units(Manager *m, Unit *u, bool failed) { |
03455c28 | 3876 | unsigned size; |
5269eb6b | 3877 | int r; |
03455c28 LDM |
3878 | |
3879 | assert(m); | |
3880 | assert(u->manager == m); | |
3881 | ||
3882 | size = set_size(m->failed_units); | |
3883 | ||
9fff8981 | 3884 | if (failed) { |
5269eb6b LP |
3885 | r = set_ensure_allocated(&m->failed_units, NULL); |
3886 | if (r < 0) | |
3887 | return log_oom(); | |
3888 | ||
9fff8981 | 3889 | if (set_put(m->failed_units, u) < 0) |
5269eb6b | 3890 | return log_oom(); |
9fff8981 | 3891 | } else |
5269eb6b | 3892 | (void) set_remove(m->failed_units, u); |
03455c28 LDM |
3893 | |
3894 | if (set_size(m->failed_units) != size) | |
3895 | bus_manager_send_change_signal(m); | |
5269eb6b LP |
3896 | |
3897 | return 0; | |
03455c28 LDM |
3898 | } |
3899 | ||
f755e3b7 LP |
3900 | ManagerState manager_state(Manager *m) { |
3901 | Unit *u; | |
3902 | ||
3903 | assert(m); | |
3904 | ||
3905 | /* Did we ever finish booting? If not then we are still starting up */ | |
49d5666c | 3906 | if (!MANAGER_IS_FINISHED(m)) { |
d81afec1 LP |
3907 | |
3908 | u = manager_get_unit(m, SPECIAL_BASIC_TARGET); | |
3909 | if (!u || !UNIT_IS_ACTIVE_OR_RELOADING(unit_active_state(u))) | |
3910 | return MANAGER_INITIALIZING; | |
3911 | ||
f755e3b7 | 3912 | return MANAGER_STARTING; |
d81afec1 | 3913 | } |
f755e3b7 | 3914 | |
e68537f0 | 3915 | /* Is the special shutdown target active or queued? If so, we are in shutdown state */ |
f755e3b7 | 3916 | u = manager_get_unit(m, SPECIAL_SHUTDOWN_TARGET); |
706424c2 | 3917 | if (u && unit_active_or_pending(u)) |
f755e3b7 LP |
3918 | return MANAGER_STOPPING; |
3919 | ||
45a7b16b LP |
3920 | if (MANAGER_IS_SYSTEM(m)) { |
3921 | /* Are the rescue or emergency targets active or queued? If so we are in maintenance state */ | |
3922 | u = manager_get_unit(m, SPECIAL_RESCUE_TARGET); | |
3923 | if (u && unit_active_or_pending(u)) | |
3924 | return MANAGER_MAINTENANCE; | |
f755e3b7 | 3925 | |
45a7b16b LP |
3926 | u = manager_get_unit(m, SPECIAL_EMERGENCY_TARGET); |
3927 | if (u && unit_active_or_pending(u)) | |
3928 | return MANAGER_MAINTENANCE; | |
3929 | } | |
f755e3b7 LP |
3930 | |
3931 | /* Are there any failed units? If so, we are in degraded mode */ | |
3932 | if (set_size(m->failed_units) > 0) | |
3933 | return MANAGER_DEGRADED; | |
3934 | ||
3935 | return MANAGER_RUNNING; | |
3936 | } | |
3937 | ||
00d9ef85 LP |
3938 | #define DESTROY_IPC_FLAG (UINT32_C(1) << 31) |
3939 | ||
3940 | static void manager_unref_uid_internal( | |
3941 | Manager *m, | |
3942 | Hashmap **uid_refs, | |
3943 | uid_t uid, | |
3944 | bool destroy_now, | |
3945 | int (*_clean_ipc)(uid_t uid)) { | |
3946 | ||
3947 | uint32_t c, n; | |
3948 | ||
3949 | assert(m); | |
3950 | assert(uid_refs); | |
3951 | assert(uid_is_valid(uid)); | |
3952 | assert(_clean_ipc); | |
3953 | ||
3954 | /* A generic implementation, covering both manager_unref_uid() and manager_unref_gid(), under the assumption | |
3955 | * that uid_t and gid_t are actually defined the same way, with the same validity rules. | |
3956 | * | |
3957 | * We store a hashmap where the UID/GID is they key and the value is a 32bit reference counter, whose highest | |
3958 | * bit is used as flag for marking UIDs/GIDs whose IPC objects to remove when the last reference to the UID/GID | |
3959 | * is dropped. The flag is set to on, once at least one reference from a unit where RemoveIPC= is set is added | |
3960 | * on a UID/GID. It is reset when the UID's/GID's reference counter drops to 0 again. */ | |
3961 | ||
3962 | assert_cc(sizeof(uid_t) == sizeof(gid_t)); | |
3963 | assert_cc(UID_INVALID == (uid_t) GID_INVALID); | |
3964 | ||
3965 | if (uid == 0) /* We don't keep track of root, and will never destroy it */ | |
3966 | return; | |
3967 | ||
3968 | c = PTR_TO_UINT32(hashmap_get(*uid_refs, UID_TO_PTR(uid))); | |
3969 | ||
3970 | n = c & ~DESTROY_IPC_FLAG; | |
3971 | assert(n > 0); | |
3972 | n--; | |
3973 | ||
3974 | if (destroy_now && n == 0) { | |
3975 | hashmap_remove(*uid_refs, UID_TO_PTR(uid)); | |
3976 | ||
3977 | if (c & DESTROY_IPC_FLAG) { | |
3978 | log_debug("%s " UID_FMT " is no longer referenced, cleaning up its IPC.", | |
3979 | _clean_ipc == clean_ipc_by_uid ? "UID" : "GID", | |
3980 | uid); | |
3981 | (void) _clean_ipc(uid); | |
3982 | } | |
3983 | } else { | |
3984 | c = n | (c & DESTROY_IPC_FLAG); | |
3985 | assert_se(hashmap_update(*uid_refs, UID_TO_PTR(uid), UINT32_TO_PTR(c)) >= 0); | |
3986 | } | |
3987 | } | |
3988 | ||
3989 | void manager_unref_uid(Manager *m, uid_t uid, bool destroy_now) { | |
3990 | manager_unref_uid_internal(m, &m->uid_refs, uid, destroy_now, clean_ipc_by_uid); | |
3991 | } | |
3992 | ||
3993 | void manager_unref_gid(Manager *m, gid_t gid, bool destroy_now) { | |
3994 | manager_unref_uid_internal(m, &m->gid_refs, (uid_t) gid, destroy_now, clean_ipc_by_gid); | |
3995 | } | |
3996 | ||
3997 | static int manager_ref_uid_internal( | |
3998 | Manager *m, | |
3999 | Hashmap **uid_refs, | |
4000 | uid_t uid, | |
4001 | bool clean_ipc) { | |
4002 | ||
4003 | uint32_t c, n; | |
4004 | int r; | |
4005 | ||
4006 | assert(m); | |
4007 | assert(uid_refs); | |
4008 | assert(uid_is_valid(uid)); | |
4009 | ||
4010 | /* A generic implementation, covering both manager_ref_uid() and manager_ref_gid(), under the assumption | |
4011 | * that uid_t and gid_t are actually defined the same way, with the same validity rules. */ | |
4012 | ||
4013 | assert_cc(sizeof(uid_t) == sizeof(gid_t)); | |
4014 | assert_cc(UID_INVALID == (uid_t) GID_INVALID); | |
4015 | ||
4016 | if (uid == 0) /* We don't keep track of root, and will never destroy it */ | |
4017 | return 0; | |
4018 | ||
4019 | r = hashmap_ensure_allocated(uid_refs, &trivial_hash_ops); | |
4020 | if (r < 0) | |
4021 | return r; | |
4022 | ||
4023 | c = PTR_TO_UINT32(hashmap_get(*uid_refs, UID_TO_PTR(uid))); | |
4024 | ||
4025 | n = c & ~DESTROY_IPC_FLAG; | |
4026 | n++; | |
4027 | ||
4028 | if (n & DESTROY_IPC_FLAG) /* check for overflow */ | |
4029 | return -EOVERFLOW; | |
4030 | ||
4031 | c = n | (c & DESTROY_IPC_FLAG) | (clean_ipc ? DESTROY_IPC_FLAG : 0); | |
4032 | ||
4033 | return hashmap_replace(*uid_refs, UID_TO_PTR(uid), UINT32_TO_PTR(c)); | |
4034 | } | |
4035 | ||
4036 | int manager_ref_uid(Manager *m, uid_t uid, bool clean_ipc) { | |
4037 | return manager_ref_uid_internal(m, &m->uid_refs, uid, clean_ipc); | |
4038 | } | |
4039 | ||
4040 | int manager_ref_gid(Manager *m, gid_t gid, bool clean_ipc) { | |
4041 | return manager_ref_uid_internal(m, &m->gid_refs, (uid_t) gid, clean_ipc); | |
4042 | } | |
4043 | ||
4044 | static void manager_vacuum_uid_refs_internal( | |
4045 | Manager *m, | |
4046 | Hashmap **uid_refs, | |
4047 | int (*_clean_ipc)(uid_t uid)) { | |
4048 | ||
4049 | Iterator i; | |
4050 | void *p, *k; | |
4051 | ||
4052 | assert(m); | |
4053 | assert(uid_refs); | |
4054 | assert(_clean_ipc); | |
4055 | ||
4056 | HASHMAP_FOREACH_KEY(p, k, *uid_refs, i) { | |
4057 | uint32_t c, n; | |
4058 | uid_t uid; | |
4059 | ||
4060 | uid = PTR_TO_UID(k); | |
4061 | c = PTR_TO_UINT32(p); | |
4062 | ||
4063 | n = c & ~DESTROY_IPC_FLAG; | |
4064 | if (n > 0) | |
4065 | continue; | |
4066 | ||
4067 | if (c & DESTROY_IPC_FLAG) { | |
4068 | log_debug("Found unreferenced %s " UID_FMT " after reload/reexec. Cleaning up.", | |
4069 | _clean_ipc == clean_ipc_by_uid ? "UID" : "GID", | |
4070 | uid); | |
4071 | (void) _clean_ipc(uid); | |
4072 | } | |
4073 | ||
4074 | assert_se(hashmap_remove(*uid_refs, k) == p); | |
4075 | } | |
4076 | } | |
4077 | ||
4078 | void manager_vacuum_uid_refs(Manager *m) { | |
4079 | manager_vacuum_uid_refs_internal(m, &m->uid_refs, clean_ipc_by_uid); | |
4080 | } | |
4081 | ||
4082 | void manager_vacuum_gid_refs(Manager *m) { | |
4083 | manager_vacuum_uid_refs_internal(m, &m->gid_refs, clean_ipc_by_gid); | |
4084 | } | |
4085 | ||
4086 | static void manager_serialize_uid_refs_internal( | |
4087 | Manager *m, | |
4088 | FILE *f, | |
4089 | Hashmap **uid_refs, | |
4090 | const char *field_name) { | |
4091 | ||
4092 | Iterator i; | |
4093 | void *p, *k; | |
4094 | ||
4095 | assert(m); | |
4096 | assert(f); | |
4097 | assert(uid_refs); | |
4098 | assert(field_name); | |
4099 | ||
4100 | /* Serialize the UID reference table. Or actually, just the IPC destruction flag of it, as the actual counter | |
4101 | * of it is better rebuild after a reload/reexec. */ | |
4102 | ||
4103 | HASHMAP_FOREACH_KEY(p, k, *uid_refs, i) { | |
4104 | uint32_t c; | |
4105 | uid_t uid; | |
4106 | ||
4107 | uid = PTR_TO_UID(k); | |
4108 | c = PTR_TO_UINT32(p); | |
4109 | ||
4110 | if (!(c & DESTROY_IPC_FLAG)) | |
4111 | continue; | |
4112 | ||
4113 | fprintf(f, "%s=" UID_FMT "\n", field_name, uid); | |
4114 | } | |
4115 | } | |
4116 | ||
4117 | void manager_serialize_uid_refs(Manager *m, FILE *f) { | |
4118 | manager_serialize_uid_refs_internal(m, f, &m->uid_refs, "destroy-ipc-uid"); | |
4119 | } | |
4120 | ||
4121 | void manager_serialize_gid_refs(Manager *m, FILE *f) { | |
4122 | manager_serialize_uid_refs_internal(m, f, &m->gid_refs, "destroy-ipc-gid"); | |
4123 | } | |
4124 | ||
4125 | static void manager_deserialize_uid_refs_one_internal( | |
4126 | Manager *m, | |
4127 | Hashmap** uid_refs, | |
4128 | const char *value) { | |
4129 | ||
4130 | uid_t uid; | |
4131 | uint32_t c; | |
4132 | int r; | |
4133 | ||
4134 | assert(m); | |
4135 | assert(uid_refs); | |
4136 | assert(value); | |
4137 | ||
4138 | r = parse_uid(value, &uid); | |
4139 | if (r < 0 || uid == 0) { | |
4140 | log_debug("Unable to parse UID reference serialization"); | |
4141 | return; | |
4142 | } | |
4143 | ||
4144 | r = hashmap_ensure_allocated(uid_refs, &trivial_hash_ops); | |
4145 | if (r < 0) { | |
4146 | log_oom(); | |
4147 | return; | |
4148 | } | |
4149 | ||
4150 | c = PTR_TO_UINT32(hashmap_get(*uid_refs, UID_TO_PTR(uid))); | |
4151 | if (c & DESTROY_IPC_FLAG) | |
4152 | return; | |
4153 | ||
4154 | c |= DESTROY_IPC_FLAG; | |
4155 | ||
4156 | r = hashmap_replace(*uid_refs, UID_TO_PTR(uid), UINT32_TO_PTR(c)); | |
4157 | if (r < 0) { | |
4158 | log_debug("Failed to add UID reference entry"); | |
4159 | return; | |
4160 | } | |
4161 | } | |
4162 | ||
4163 | void manager_deserialize_uid_refs_one(Manager *m, const char *value) { | |
4164 | manager_deserialize_uid_refs_one_internal(m, &m->uid_refs, value); | |
4165 | } | |
4166 | ||
4167 | void manager_deserialize_gid_refs_one(Manager *m, const char *value) { | |
4168 | manager_deserialize_uid_refs_one_internal(m, &m->gid_refs, value); | |
4169 | } | |
4170 | ||
4171 | int manager_dispatch_user_lookup_fd(sd_event_source *source, int fd, uint32_t revents, void *userdata) { | |
4172 | struct buffer { | |
4173 | uid_t uid; | |
4174 | gid_t gid; | |
4175 | char unit_name[UNIT_NAME_MAX+1]; | |
4176 | } _packed_ buffer; | |
4177 | ||
4178 | Manager *m = userdata; | |
4179 | ssize_t l; | |
4180 | size_t n; | |
4181 | Unit *u; | |
4182 | ||
4183 | assert_se(source); | |
4184 | assert_se(m); | |
4185 | ||
4186 | /* Invoked whenever a child process succeeded resolving its user/group to use and sent us the resulting UID/GID | |
4187 | * in a datagram. We parse the datagram here and pass it off to the unit, so that it can add a reference to the | |
4188 | * UID/GID so that it can destroy the UID/GID's IPC objects when the reference counter drops to 0. */ | |
4189 | ||
4190 | l = recv(fd, &buffer, sizeof(buffer), MSG_DONTWAIT); | |
4191 | if (l < 0) { | |
4c701096 | 4192 | if (IN_SET(errno, EINTR, EAGAIN)) |
00d9ef85 LP |
4193 | return 0; |
4194 | ||
4195 | return log_error_errno(errno, "Failed to read from user lookup fd: %m"); | |
4196 | } | |
4197 | ||
4198 | if ((size_t) l <= offsetof(struct buffer, unit_name)) { | |
4199 | log_warning("Received too short user lookup message, ignoring."); | |
4200 | return 0; | |
4201 | } | |
4202 | ||
4203 | if ((size_t) l > offsetof(struct buffer, unit_name) + UNIT_NAME_MAX) { | |
4204 | log_warning("Received too long user lookup message, ignoring."); | |
4205 | return 0; | |
4206 | } | |
4207 | ||
4208 | if (!uid_is_valid(buffer.uid) && !gid_is_valid(buffer.gid)) { | |
4209 | log_warning("Got user lookup message with invalid UID/GID pair, ignoring."); | |
4210 | return 0; | |
4211 | } | |
4212 | ||
4213 | n = (size_t) l - offsetof(struct buffer, unit_name); | |
4214 | if (memchr(buffer.unit_name, 0, n)) { | |
4215 | log_warning("Received lookup message with embedded NUL character, ignoring."); | |
4216 | return 0; | |
4217 | } | |
4218 | ||
4219 | buffer.unit_name[n] = 0; | |
4220 | u = manager_get_unit(m, buffer.unit_name); | |
4221 | if (!u) { | |
4222 | log_debug("Got user lookup message but unit doesn't exist, ignoring."); | |
4223 | return 0; | |
4224 | } | |
4225 | ||
4226 | log_unit_debug(u, "User lookup succeeded: uid=" UID_FMT " gid=" GID_FMT, buffer.uid, buffer.gid); | |
4227 | ||
4228 | unit_notify_user_lookup(u, buffer.uid, buffer.gid); | |
4229 | return 0; | |
4230 | } | |
4231 | ||
af6b0ecc | 4232 | char *manager_taint_string(Manager *m) { |
90d7464d | 4233 | _cleanup_free_ char *destination = NULL, *overflowuid = NULL, *overflowgid = NULL; |
af6b0ecc LP |
4234 | char *buf, *e; |
4235 | int r; | |
4236 | ||
198ce932 ZJS |
4237 | /* Returns a "taint string", e.g. "local-hwclock:var-run-bad". |
4238 | * Only things that are detected at runtime should be tagged | |
4239 | * here. For stuff that is set during compilation, emit a warning | |
4240 | * in the configuration phase. */ | |
4241 | ||
af6b0ecc LP |
4242 | assert(m); |
4243 | ||
4244 | buf = new(char, sizeof("split-usr:" | |
4245 | "cgroups-missing:" | |
4246 | "local-hwclock:" | |
4247 | "var-run-bad:" | |
90d7464d LP |
4248 | "overflowuid-not-65534:" |
4249 | "overflowgid-not-65534:")); | |
af6b0ecc LP |
4250 | if (!buf) |
4251 | return NULL; | |
4252 | ||
4253 | e = buf; | |
0fd402b0 | 4254 | buf[0] = 0; |
af6b0ecc LP |
4255 | |
4256 | if (m->taint_usr) | |
4257 | e = stpcpy(e, "split-usr:"); | |
4258 | ||
4259 | if (access("/proc/cgroups", F_OK) < 0) | |
4260 | e = stpcpy(e, "cgroups-missing:"); | |
4261 | ||
4262 | if (clock_is_localtime(NULL) > 0) | |
4263 | e = stpcpy(e, "local-hwclock:"); | |
4264 | ||
4265 | r = readlink_malloc("/var/run", &destination); | |
4266 | if (r < 0 || !PATH_IN_SET(destination, "../run", "/run")) | |
4267 | e = stpcpy(e, "var-run-bad:"); | |
4268 | ||
90d7464d LP |
4269 | r = read_one_line_file("/proc/sys/kernel/overflowuid", &overflowuid); |
4270 | if (r >= 0 && !streq(overflowuid, "65534")) | |
4271 | e = stpcpy(e, "overflowuid-not-65534:"); | |
4272 | ||
4273 | r = read_one_line_file("/proc/sys/kernel/overflowgid", &overflowgid); | |
4274 | if (r >= 0 && !streq(overflowgid, "65534")) | |
4275 | e = stpcpy(e, "overflowgid-not-65534:"); | |
4276 | ||
af6b0ecc LP |
4277 | /* remove the last ':' */ |
4278 | if (e != buf) | |
4279 | e[-1] = 0; | |
4280 | ||
4281 | return buf; | |
4282 | } | |
4283 | ||
adefcf28 LP |
4284 | void manager_ref_console(Manager *m) { |
4285 | assert(m); | |
4286 | ||
4287 | m->n_on_console++; | |
4288 | } | |
4289 | ||
4290 | void manager_unref_console(Manager *m) { | |
4291 | ||
4292 | assert(m->n_on_console > 0); | |
4293 | m->n_on_console--; | |
4294 | ||
4295 | if (m->n_on_console == 0) | |
4296 | m->no_console_output = false; /* unset no_console_output flag, since the console is definitely free now */ | |
4297 | } | |
4298 | ||
f755e3b7 | 4299 | static const char *const manager_state_table[_MANAGER_STATE_MAX] = { |
d81afec1 | 4300 | [MANAGER_INITIALIZING] = "initializing", |
f755e3b7 LP |
4301 | [MANAGER_STARTING] = "starting", |
4302 | [MANAGER_RUNNING] = "running", | |
4303 | [MANAGER_DEGRADED] = "degraded", | |
4304 | [MANAGER_MAINTENANCE] = "maintenance", | |
4305 | [MANAGER_STOPPING] = "stopping", | |
4306 | }; | |
4307 | ||
4308 | DEFINE_STRING_TABLE_LOOKUP(manager_state, ManagerState); | |
9f9f0342 LP |
4309 | |
4310 | static const char *const manager_timestamp_table[_MANAGER_TIMESTAMP_MAX] = { | |
4311 | [MANAGER_TIMESTAMP_FIRMWARE] = "firmware", | |
4312 | [MANAGER_TIMESTAMP_LOADER] = "loader", | |
4313 | [MANAGER_TIMESTAMP_KERNEL] = "kernel", | |
4314 | [MANAGER_TIMESTAMP_INITRD] = "initrd", | |
4315 | [MANAGER_TIMESTAMP_USERSPACE] = "userspace", | |
4316 | [MANAGER_TIMESTAMP_FINISH] = "finish", | |
4317 | [MANAGER_TIMESTAMP_SECURITY_START] = "security-start", | |
4318 | [MANAGER_TIMESTAMP_SECURITY_FINISH] = "security-finish", | |
4319 | [MANAGER_TIMESTAMP_GENERATORS_START] = "generators-start", | |
4320 | [MANAGER_TIMESTAMP_GENERATORS_FINISH] = "generators-finish", | |
4321 | [MANAGER_TIMESTAMP_UNITS_LOAD_START] = "units-load-start", | |
4322 | [MANAGER_TIMESTAMP_UNITS_LOAD_FINISH] = "units-load-finish", | |
4323 | }; | |
4324 | ||
4325 | DEFINE_STRING_TABLE_LOOKUP(manager_timestamp, ManagerTimestamp); |