]>
Commit | Line | Data |
---|---|---|
663996b3 MS |
1 | /*** |
2 | This file is part of systemd. | |
3 | ||
4 | Copyright 2010 Lennart Poettering | |
5 | ||
6 | systemd is free software; you can redistribute it and/or modify it | |
7 | under the terms of the GNU Lesser General Public License as published by | |
8 | the Free Software Foundation; either version 2.1 of the License, or | |
9 | (at your option) any later version. | |
10 | ||
11 | systemd is distributed in the hope that it will be useful, but | |
12 | WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 | Lesser General Public License for more details. | |
15 | ||
16 | You should have received a copy of the GNU Lesser General Public License | |
17 | along with systemd; If not, see <http://www.gnu.org/licenses/>. | |
18 | ***/ | |
19 | ||
6300502b | 20 | #include <dirent.h> |
663996b3 | 21 | #include <errno.h> |
6300502b MP |
22 | #include <fcntl.h> |
23 | #include <linux/kd.h> | |
663996b3 | 24 | #include <signal.h> |
6300502b | 25 | #include <string.h> |
5eef597e | 26 | #include <sys/epoll.h> |
6300502b | 27 | #include <sys/inotify.h> |
663996b3 | 28 | #include <sys/ioctl.h> |
6300502b | 29 | #include <sys/reboot.h> |
663996b3 | 30 | #include <sys/timerfd.h> |
6300502b MP |
31 | #include <sys/wait.h> |
32 | #include <unistd.h> | |
663996b3 MS |
33 | |
34 | #ifdef HAVE_AUDIT | |
35 | #include <libaudit.h> | |
36 | #endif | |
37 | ||
60f067b4 | 38 | #include "sd-daemon.h" |
60f067b4 | 39 | #include "sd-messages.h" |
663996b3 | 40 | |
db2df898 | 41 | #include "alloc-util.h" |
6300502b MP |
42 | #include "audit-fd.h" |
43 | #include "boot-timestamps.h" | |
44 | #include "bus-common-errors.h" | |
45 | #include "bus-error.h" | |
46 | #include "bus-kernel.h" | |
47 | #include "bus-util.h" | |
48 | #include "dbus-job.h" | |
49 | #include "dbus-manager.h" | |
50 | #include "dbus-unit.h" | |
51 | #include "dbus.h" | |
52 | #include "env-util.h" | |
db2df898 | 53 | #include "escape.h" |
6300502b | 54 | #include "exit-status.h" |
db2df898 MP |
55 | #include "fd-util.h" |
56 | #include "fileio.h" | |
57 | #include "fs-util.h" | |
663996b3 | 58 | #include "hashmap.h" |
db2df898 | 59 | #include "io-util.h" |
6300502b | 60 | #include "locale-setup.h" |
663996b3 | 61 | #include "log.h" |
6300502b | 62 | #include "macro.h" |
db2df898 | 63 | #include "manager.h" |
6300502b | 64 | #include "missing.h" |
663996b3 | 65 | #include "mkdir.h" |
db2df898 | 66 | #include "parse-util.h" |
6300502b MP |
67 | #include "path-lookup.h" |
68 | #include "path-util.h" | |
69 | #include "process-util.h" | |
663996b3 | 70 | #include "ratelimit.h" |
e3bff60a | 71 | #include "rm-rf.h" |
6300502b | 72 | #include "signal-util.h" |
663996b3 | 73 | #include "special.h" |
db2df898 MP |
74 | #include "stat-util.h" |
75 | #include "string-table.h" | |
76 | #include "string-util.h" | |
6300502b MP |
77 | #include "strv.h" |
78 | #include "terminal-util.h" | |
79 | #include "time-util.h" | |
80 | #include "transaction.h" | |
db2df898 | 81 | #include "umask-util.h" |
6300502b MP |
82 | #include "unit-name.h" |
83 | #include "util.h" | |
663996b3 MS |
84 | #include "virt.h" |
85 | #include "watchdog.h" | |
db2df898 MP |
86 | |
87 | #define NOTIFY_RCVBUF_SIZE (8*1024*1024) | |
663996b3 | 88 | |
663996b3 | 89 | /* Initial delay and the interval for printing status messages about running jobs */ |
60f067b4 JS |
90 | #define JOBS_IN_PROGRESS_WAIT_USEC (5*USEC_PER_SEC) |
91 | #define JOBS_IN_PROGRESS_PERIOD_USEC (USEC_PER_SEC / 3) | |
663996b3 MS |
92 | #define JOBS_IN_PROGRESS_PERIOD_DIVISOR 3 |
93 | ||
60f067b4 JS |
94 | static int manager_dispatch_notify_fd(sd_event_source *source, int fd, uint32_t revents, void *userdata); |
95 | static int manager_dispatch_signal_fd(sd_event_source *source, int fd, uint32_t revents, void *userdata); | |
96 | static int manager_dispatch_time_change_fd(sd_event_source *source, int fd, uint32_t revents, void *userdata); | |
97 | static int manager_dispatch_idle_pipe_fd(sd_event_source *source, int fd, uint32_t revents, void *userdata); | |
98 | static int manager_dispatch_jobs_in_progress(sd_event_source *source, usec_t usec, void *userdata); | |
99 | static int manager_dispatch_run_queue(sd_event_source *source, void *userdata); | |
e735f4d4 MP |
100 | static int manager_run_generators(Manager *m); |
101 | static void manager_undo_generators(Manager *m); | |
663996b3 | 102 | |
e3bff60a | 103 | static void manager_watch_jobs_in_progress(Manager *m) { |
60f067b4 | 104 | usec_t next; |
e3bff60a | 105 | int r; |
663996b3 | 106 | |
60f067b4 | 107 | assert(m); |
663996b3 | 108 | |
60f067b4 | 109 | if (m->jobs_in_progress_event_source) |
e3bff60a | 110 | return; |
663996b3 | 111 | |
60f067b4 | 112 | next = now(CLOCK_MONOTONIC) + JOBS_IN_PROGRESS_WAIT_USEC; |
e3bff60a | 113 | r = sd_event_add_time( |
60f067b4 JS |
114 | m->event, |
115 | &m->jobs_in_progress_event_source, | |
116 | CLOCK_MONOTONIC, | |
117 | next, 0, | |
118 | manager_dispatch_jobs_in_progress, m); | |
e3bff60a MP |
119 | if (r < 0) |
120 | return; | |
121 | ||
122 | (void) sd_event_source_set_description(m->jobs_in_progress_event_source, "manager-jobs-in-progress"); | |
663996b3 MS |
123 | } |
124 | ||
6300502b | 125 | #define CYLON_BUFFER_EXTRA (2*(sizeof(ANSI_RED)-1) + sizeof(ANSI_HIGHLIGHT_RED)-1 + 2*(sizeof(ANSI_NORMAL)-1)) |
663996b3 | 126 | |
663996b3 MS |
127 | static void draw_cylon(char buffer[], size_t buflen, unsigned width, unsigned pos) { |
128 | char *p = buffer; | |
129 | ||
130 | assert(buflen >= CYLON_BUFFER_EXTRA + width + 1); | |
131 | assert(pos <= width+1); /* 0 or width+1 mean that the center light is behind the corner */ | |
132 | ||
133 | if (pos > 1) { | |
134 | if (pos > 2) | |
135 | p = mempset(p, ' ', pos-2); | |
6300502b | 136 | p = stpcpy(p, ANSI_RED); |
663996b3 MS |
137 | *p++ = '*'; |
138 | } | |
139 | ||
140 | if (pos > 0 && pos <= width) { | |
6300502b | 141 | p = stpcpy(p, ANSI_HIGHLIGHT_RED); |
663996b3 MS |
142 | *p++ = '*'; |
143 | } | |
144 | ||
6300502b | 145 | p = stpcpy(p, ANSI_NORMAL); |
663996b3 MS |
146 | |
147 | if (pos < width) { | |
6300502b | 148 | p = stpcpy(p, ANSI_RED); |
663996b3 MS |
149 | *p++ = '*'; |
150 | if (pos < width-1) | |
151 | p = mempset(p, ' ', width-1-pos); | |
6300502b | 152 | strcpy(p, ANSI_NORMAL); |
60f067b4 JS |
153 | } |
154 | } | |
155 | ||
156 | void manager_flip_auto_status(Manager *m, bool enable) { | |
157 | assert(m); | |
158 | ||
159 | if (enable) { | |
160 | if (m->show_status == SHOW_STATUS_AUTO) | |
161 | manager_set_show_status(m, SHOW_STATUS_TEMPORARY); | |
162 | } else { | |
163 | if (m->show_status == SHOW_STATUS_TEMPORARY) | |
164 | manager_set_show_status(m, SHOW_STATUS_AUTO); | |
663996b3 MS |
165 | } |
166 | } | |
167 | ||
168 | static void manager_print_jobs_in_progress(Manager *m) { | |
60f067b4 | 169 | _cleanup_free_ char *job_of_n = NULL; |
663996b3 MS |
170 | Iterator i; |
171 | Job *j; | |
663996b3 MS |
172 | unsigned counter = 0, print_nr; |
173 | char cylon[6 + CYLON_BUFFER_EXTRA + 1]; | |
174 | unsigned cylon_pos; | |
60f067b4 JS |
175 | char time[FORMAT_TIMESPAN_MAX], limit[FORMAT_TIMESPAN_MAX] = "no limit"; |
176 | uint64_t x; | |
177 | ||
178 | assert(m); | |
e735f4d4 | 179 | assert(m->n_running_jobs > 0); |
60f067b4 JS |
180 | |
181 | manager_flip_auto_status(m, true); | |
663996b3 MS |
182 | |
183 | print_nr = (m->jobs_in_progress_iteration / JOBS_IN_PROGRESS_PERIOD_DIVISOR) % m->n_running_jobs; | |
184 | ||
185 | HASHMAP_FOREACH(j, m->jobs, i) | |
186 | if (j->state == JOB_RUNNING && counter++ == print_nr) | |
187 | break; | |
188 | ||
189 | /* m->n_running_jobs must be consistent with the contents of m->jobs, | |
190 | * so the above loop must have succeeded in finding j. */ | |
191 | assert(counter == print_nr + 1); | |
60f067b4 | 192 | assert(j); |
663996b3 MS |
193 | |
194 | cylon_pos = m->jobs_in_progress_iteration % 14; | |
195 | if (cylon_pos >= 8) | |
196 | cylon_pos = 14 - cylon_pos; | |
197 | draw_cylon(cylon, sizeof(cylon), 6, cylon_pos); | |
198 | ||
60f067b4 JS |
199 | m->jobs_in_progress_iteration++; |
200 | ||
e3bff60a MP |
201 | if (m->n_running_jobs > 1) { |
202 | if (asprintf(&job_of_n, "(%u of %u) ", counter, m->n_running_jobs) < 0) | |
203 | job_of_n = NULL; | |
204 | } | |
663996b3 | 205 | |
60f067b4 JS |
206 | format_timespan(time, sizeof(time), now(CLOCK_MONOTONIC) - j->begin_usec, 1*USEC_PER_SEC); |
207 | if (job_get_timeout(j, &x) > 0) | |
208 | format_timespan(limit, sizeof(limit), x - j->begin_usec, 1*USEC_PER_SEC); | |
209 | ||
5eef597e | 210 | manager_status_printf(m, STATUS_TYPE_EPHEMERAL, cylon, |
60f067b4 JS |
211 | "%sA %s job is running for %s (%s / %s)", |
212 | strempty(job_of_n), | |
213 | job_type_to_string(j->type), | |
214 | unit_description(j->unit), | |
215 | time, limit); | |
663996b3 MS |
216 | } |
217 | ||
5eef597e MP |
218 | static int have_ask_password(void) { |
219 | _cleanup_closedir_ DIR *dir; | |
220 | ||
221 | dir = opendir("/run/systemd/ask-password"); | |
222 | if (!dir) { | |
223 | if (errno == ENOENT) | |
224 | return false; | |
225 | else | |
226 | return -errno; | |
227 | } | |
228 | ||
229 | for (;;) { | |
230 | struct dirent *de; | |
231 | ||
232 | errno = 0; | |
233 | de = readdir(dir); | |
4c89c718 | 234 | if (!de && errno > 0) |
5eef597e MP |
235 | return -errno; |
236 | if (!de) | |
237 | return false; | |
238 | ||
239 | if (startswith(de->d_name, "ask.")) | |
240 | return true; | |
241 | } | |
242 | } | |
243 | ||
244 | static int manager_dispatch_ask_password_fd(sd_event_source *source, | |
245 | int fd, uint32_t revents, void *userdata) { | |
246 | Manager *m = userdata; | |
247 | ||
248 | assert(m); | |
249 | ||
250 | flush_fd(fd); | |
251 | ||
252 | m->have_ask_password = have_ask_password(); | |
253 | if (m->have_ask_password < 0) | |
254 | /* Log error but continue. Negative have_ask_password | |
255 | * is treated as unknown status. */ | |
f47781d8 | 256 | log_error_errno(m->have_ask_password, "Failed to list /run/systemd/ask-password: %m"); |
5eef597e MP |
257 | |
258 | return 0; | |
259 | } | |
260 | ||
261 | static void manager_close_ask_password(Manager *m) { | |
262 | assert(m); | |
263 | ||
5eef597e | 264 | m->ask_password_event_source = sd_event_source_unref(m->ask_password_event_source); |
d9dfd233 | 265 | m->ask_password_inotify_fd = safe_close(m->ask_password_inotify_fd); |
5eef597e MP |
266 | m->have_ask_password = -EINVAL; |
267 | } | |
268 | ||
269 | static int manager_check_ask_password(Manager *m) { | |
270 | int r; | |
271 | ||
272 | assert(m); | |
273 | ||
274 | if (!m->ask_password_event_source) { | |
275 | assert(m->ask_password_inotify_fd < 0); | |
276 | ||
277 | mkdir_p_label("/run/systemd/ask-password", 0755); | |
278 | ||
279 | m->ask_password_inotify_fd = inotify_init1(IN_NONBLOCK|IN_CLOEXEC); | |
f47781d8 MP |
280 | if (m->ask_password_inotify_fd < 0) |
281 | return log_error_errno(errno, "inotify_init1() failed: %m"); | |
5eef597e MP |
282 | |
283 | if (inotify_add_watch(m->ask_password_inotify_fd, "/run/systemd/ask-password", IN_CREATE|IN_DELETE|IN_MOVE) < 0) { | |
f47781d8 | 284 | log_error_errno(errno, "Failed to add watch on /run/systemd/ask-password: %m"); |
5eef597e MP |
285 | manager_close_ask_password(m); |
286 | return -errno; | |
287 | } | |
288 | ||
289 | r = sd_event_add_io(m->event, &m->ask_password_event_source, | |
290 | m->ask_password_inotify_fd, EPOLLIN, | |
291 | manager_dispatch_ask_password_fd, m); | |
292 | if (r < 0) { | |
f47781d8 | 293 | log_error_errno(errno, "Failed to add event source for /run/systemd/ask-password: %m"); |
5eef597e MP |
294 | manager_close_ask_password(m); |
295 | return -errno; | |
296 | } | |
297 | ||
e3bff60a MP |
298 | (void) sd_event_source_set_description(m->ask_password_event_source, "manager-ask-password"); |
299 | ||
5eef597e MP |
300 | /* Queries might have been added meanwhile... */ |
301 | manager_dispatch_ask_password_fd(m->ask_password_event_source, | |
302 | m->ask_password_inotify_fd, EPOLLIN, m); | |
303 | } | |
304 | ||
305 | return m->have_ask_password; | |
306 | } | |
307 | ||
14228c0d | 308 | static int manager_watch_idle_pipe(Manager *m) { |
14228c0d MB |
309 | int r; |
310 | ||
60f067b4 JS |
311 | assert(m); |
312 | ||
313 | if (m->idle_pipe_event_source) | |
14228c0d MB |
314 | return 0; |
315 | ||
316 | if (m->idle_pipe[2] < 0) | |
317 | return 0; | |
318 | ||
60f067b4 | 319 | r = sd_event_add_io(m->event, &m->idle_pipe_event_source, m->idle_pipe[2], EPOLLIN, manager_dispatch_idle_pipe_fd, m); |
f47781d8 MP |
320 | if (r < 0) |
321 | return log_error_errno(r, "Failed to watch idle pipe: %m"); | |
14228c0d | 322 | |
e3bff60a MP |
323 | (void) sd_event_source_set_description(m->idle_pipe_event_source, "manager-idle-pipe"); |
324 | ||
14228c0d | 325 | return 0; |
14228c0d MB |
326 | } |
327 | ||
60f067b4 JS |
328 | static void manager_close_idle_pipe(Manager *m) { |
329 | assert(m); | |
14228c0d | 330 | |
6300502b MP |
331 | m->idle_pipe_event_source = sd_event_source_unref(m->idle_pipe_event_source); |
332 | ||
60f067b4 JS |
333 | safe_close_pair(m->idle_pipe); |
334 | safe_close_pair(m->idle_pipe + 2); | |
14228c0d MB |
335 | } |
336 | ||
663996b3 | 337 | static int manager_setup_time_change(Manager *m) { |
60f067b4 | 338 | int r; |
663996b3 MS |
339 | |
340 | /* We only care for the cancellation event, hence we set the | |
341 | * timeout to the latest possible value. */ | |
342 | struct itimerspec its = { | |
343 | .it_value.tv_sec = TIME_T_MAX, | |
344 | }; | |
663996b3 | 345 | |
60f067b4 JS |
346 | assert(m); |
347 | assert_cc(sizeof(time_t) == sizeof(TIME_T_MAX)); | |
663996b3 | 348 | |
5eef597e MP |
349 | if (m->test_run) |
350 | return 0; | |
351 | ||
663996b3 MS |
352 | /* Uses TFD_TIMER_CANCEL_ON_SET to get notifications whenever |
353 | * CLOCK_REALTIME makes a jump relative to CLOCK_MONOTONIC */ | |
354 | ||
60f067b4 | 355 | m->time_change_fd = timerfd_create(CLOCK_REALTIME, TFD_NONBLOCK|TFD_CLOEXEC); |
f47781d8 MP |
356 | if (m->time_change_fd < 0) |
357 | return log_error_errno(errno, "Failed to create timerfd: %m"); | |
663996b3 | 358 | |
60f067b4 | 359 | if (timerfd_settime(m->time_change_fd, TFD_TIMER_ABSTIME|TFD_TIMER_CANCEL_ON_SET, &its, NULL) < 0) { |
f47781d8 | 360 | log_debug_errno(errno, "Failed to set up TFD_TIMER_CANCEL_ON_SET, ignoring: %m"); |
60f067b4 | 361 | m->time_change_fd = safe_close(m->time_change_fd); |
663996b3 MS |
362 | return 0; |
363 | } | |
364 | ||
60f067b4 | 365 | r = sd_event_add_io(m->event, &m->time_change_event_source, m->time_change_fd, EPOLLIN, manager_dispatch_time_change_fd, m); |
f47781d8 MP |
366 | if (r < 0) |
367 | return log_error_errno(r, "Failed to create time change event source: %m"); | |
663996b3 | 368 | |
e3bff60a MP |
369 | (void) sd_event_source_set_description(m->time_change_event_source, "manager-time-change"); |
370 | ||
663996b3 MS |
371 | log_debug("Set up TFD_TIMER_CANCEL_ON_SET timerfd."); |
372 | ||
373 | return 0; | |
374 | } | |
375 | ||
376 | static int enable_special_signals(Manager *m) { | |
60f067b4 | 377 | _cleanup_close_ int fd = -1; |
663996b3 MS |
378 | |
379 | assert(m); | |
380 | ||
4c89c718 MP |
381 | if (m->test_run) |
382 | return 0; | |
383 | ||
663996b3 MS |
384 | /* Enable that we get SIGINT on control-alt-del. In containers |
385 | * this will fail with EPERM (older) or EINVAL (newer), so | |
386 | * ignore that. */ | |
387 | if (reboot(RB_DISABLE_CAD) < 0 && errno != EPERM && errno != EINVAL) | |
f47781d8 | 388 | log_warning_errno(errno, "Failed to enable ctrl-alt-del handling: %m"); |
663996b3 MS |
389 | |
390 | fd = open_terminal("/dev/tty0", O_RDWR|O_NOCTTY|O_CLOEXEC); | |
391 | if (fd < 0) { | |
392 | /* Support systems without virtual console */ | |
393 | if (fd != -ENOENT) | |
f47781d8 | 394 | log_warning_errno(errno, "Failed to open /dev/tty0: %m"); |
663996b3 MS |
395 | } else { |
396 | /* Enable that we get SIGWINCH on kbrequest */ | |
397 | if (ioctl(fd, KDSIGACCEPT, SIGWINCH) < 0) | |
f47781d8 | 398 | log_warning_errno(errno, "Failed to enable kbrequest handling: %m"); |
663996b3 MS |
399 | } |
400 | ||
401 | return 0; | |
402 | } | |
403 | ||
404 | static int manager_setup_signals(Manager *m) { | |
663996b3 MS |
405 | struct sigaction sa = { |
406 | .sa_handler = SIG_DFL, | |
407 | .sa_flags = SA_NOCLDSTOP|SA_RESTART, | |
408 | }; | |
60f067b4 JS |
409 | sigset_t mask; |
410 | int r; | |
663996b3 MS |
411 | |
412 | assert(m); | |
413 | ||
663996b3 MS |
414 | assert_se(sigaction(SIGCHLD, &sa, NULL) == 0); |
415 | ||
5eef597e MP |
416 | /* We make liberal use of realtime signals here. On |
417 | * Linux/glibc we have 30 of them (with the exception of Linux | |
418 | * on hppa, see below), between SIGRTMIN+0 ... SIGRTMIN+30 | |
419 | * (aka SIGRTMAX). */ | |
663996b3 | 420 | |
5eef597e | 421 | assert_se(sigemptyset(&mask) == 0); |
663996b3 MS |
422 | sigset_add_many(&mask, |
423 | SIGCHLD, /* Child died */ | |
424 | SIGTERM, /* Reexecute daemon */ | |
425 | SIGHUP, /* Reload configuration */ | |
426 | SIGUSR1, /* systemd/upstart: reconnect to D-Bus */ | |
427 | SIGUSR2, /* systemd: dump status */ | |
428 | SIGINT, /* Kernel sends us this on control-alt-del */ | |
429 | SIGWINCH, /* Kernel sends us this on kbrequest (alt-arrowup) */ | |
430 | SIGPWR, /* Some kernel drivers and upsd send us this on power failure */ | |
5eef597e | 431 | |
663996b3 MS |
432 | SIGRTMIN+0, /* systemd: start default.target */ |
433 | SIGRTMIN+1, /* systemd: isolate rescue.target */ | |
434 | SIGRTMIN+2, /* systemd: isolate emergency.target */ | |
435 | SIGRTMIN+3, /* systemd: start halt.target */ | |
436 | SIGRTMIN+4, /* systemd: start poweroff.target */ | |
437 | SIGRTMIN+5, /* systemd: start reboot.target */ | |
438 | SIGRTMIN+6, /* systemd: start kexec.target */ | |
5eef597e MP |
439 | |
440 | /* ... space for more special targets ... */ | |
441 | ||
663996b3 MS |
442 | SIGRTMIN+13, /* systemd: Immediate halt */ |
443 | SIGRTMIN+14, /* systemd: Immediate poweroff */ | |
444 | SIGRTMIN+15, /* systemd: Immediate reboot */ | |
445 | SIGRTMIN+16, /* systemd: Immediate kexec */ | |
5eef597e MP |
446 | |
447 | /* ... space for more immediate system state changes ... */ | |
448 | ||
663996b3 MS |
449 | SIGRTMIN+20, /* systemd: enable status messages */ |
450 | SIGRTMIN+21, /* systemd: disable status messages */ | |
451 | SIGRTMIN+22, /* systemd: set log level to LOG_DEBUG */ | |
452 | SIGRTMIN+23, /* systemd: set log level to LOG_INFO */ | |
453 | SIGRTMIN+24, /* systemd: Immediate exit (--user only) */ | |
5eef597e MP |
454 | |
455 | /* .. one free signal here ... */ | |
456 | ||
457 | #if !defined(__hppa64__) && !defined(__hppa__) | |
458 | /* Apparently Linux on hppa has fewer RT | |
459 | * signals (SIGRTMAX is SIGRTMIN+25 there), | |
460 | * hence let's not try to make use of them | |
461 | * here. Since these commands are accessible | |
462 | * by different means and only really a safety | |
463 | * net, the missing functionality on hppa | |
464 | * shouldn't matter. */ | |
465 | ||
663996b3 MS |
466 | SIGRTMIN+26, /* systemd: set log target to journal-or-kmsg */ |
467 | SIGRTMIN+27, /* systemd: set log target to console */ | |
468 | SIGRTMIN+28, /* systemd: set log target to kmsg */ | |
e735f4d4 | 469 | SIGRTMIN+29, /* systemd: set log target to syslog-or-kmsg (obsolete) */ |
5eef597e MP |
470 | |
471 | /* ... one free signal here SIGRTMIN+30 ... */ | |
472 | #endif | |
663996b3 MS |
473 | -1); |
474 | assert_se(sigprocmask(SIG_SETMASK, &mask, NULL) == 0); | |
475 | ||
60f067b4 JS |
476 | m->signal_fd = signalfd(-1, &mask, SFD_NONBLOCK|SFD_CLOEXEC); |
477 | if (m->signal_fd < 0) | |
663996b3 MS |
478 | return -errno; |
479 | ||
60f067b4 JS |
480 | r = sd_event_add_io(m->event, &m->signal_event_source, m->signal_fd, EPOLLIN, manager_dispatch_signal_fd, m); |
481 | if (r < 0) | |
482 | return r; | |
483 | ||
e3bff60a MP |
484 | (void) sd_event_source_set_description(m->signal_event_source, "manager-signal"); |
485 | ||
60f067b4 | 486 | /* Process signals a bit earlier than the rest of things, but |
5eef597e | 487 | * later than notify_fd processing, so that the notify |
60f067b4 JS |
488 | * processing can still figure out to which process/service a |
489 | * message belongs, before we reap the process. */ | |
db2df898 | 490 | r = sd_event_source_set_priority(m->signal_event_source, SD_EVENT_PRIORITY_NORMAL-5); |
60f067b4 JS |
491 | if (r < 0) |
492 | return r; | |
663996b3 | 493 | |
e3bff60a | 494 | if (m->running_as == MANAGER_SYSTEM) |
663996b3 MS |
495 | return enable_special_signals(m); |
496 | ||
497 | return 0; | |
498 | } | |
499 | ||
60f067b4 JS |
500 | static void manager_clean_environment(Manager *m) { |
501 | assert(m); | |
502 | ||
503 | /* Let's remove some environment variables that we | |
504 | * need ourselves to communicate with our clients */ | |
505 | strv_env_unset_many( | |
506 | m->environment, | |
507 | "NOTIFY_SOCKET", | |
508 | "MAINPID", | |
509 | "MANAGERPID", | |
510 | "LISTEN_PID", | |
511 | "LISTEN_FDS", | |
6300502b | 512 | "LISTEN_FDNAMES", |
60f067b4 JS |
513 | "WATCHDOG_PID", |
514 | "WATCHDOG_USEC", | |
515 | NULL); | |
516 | } | |
517 | ||
14228c0d | 518 | static int manager_default_environment(Manager *m) { |
663996b3 MS |
519 | assert(m); |
520 | ||
e3bff60a | 521 | if (m->running_as == MANAGER_SYSTEM) { |
14228c0d MB |
522 | /* The system manager always starts with a clean |
523 | * environment for its children. It does not import | |
524 | * the kernel or the parents exported variables. | |
525 | * | |
526 | * The initial passed environ is untouched to keep | |
527 | * /proc/self/environ valid; it is used for tagging | |
528 | * the init process inside containers. */ | |
529 | m->environment = strv_new("PATH=" DEFAULT_PATH, | |
530 | NULL); | |
531 | ||
532 | /* Import locale variables LC_*= from configuration */ | |
533 | locale_setup(&m->environment); | |
60f067b4 | 534 | } else { |
14228c0d MB |
535 | /* The user manager passes its own environment |
536 | * along to its children. */ | |
537 | m->environment = strv_copy(environ); | |
60f067b4 | 538 | } |
663996b3 | 539 | |
14228c0d MB |
540 | if (!m->environment) |
541 | return -ENOMEM; | |
663996b3 | 542 | |
60f067b4 JS |
543 | manager_clean_environment(m); |
544 | strv_sort(m->environment); | |
545 | ||
14228c0d | 546 | return 0; |
663996b3 MS |
547 | } |
548 | ||
e3bff60a MP |
549 | |
550 | int manager_new(ManagerRunningAs running_as, bool test_run, Manager **_m) { | |
551 | ||
552 | static const char * const unit_log_fields[_MANAGER_RUNNING_AS_MAX] = { | |
553 | [MANAGER_SYSTEM] = "UNIT=", | |
554 | [MANAGER_USER] = "USER_UNIT=", | |
555 | }; | |
556 | ||
557 | static const char * const unit_log_format_strings[_MANAGER_RUNNING_AS_MAX] = { | |
558 | [MANAGER_SYSTEM] = "UNIT=%s", | |
559 | [MANAGER_USER] = "USER_UNIT=%s", | |
560 | }; | |
561 | ||
663996b3 | 562 | Manager *m; |
60f067b4 | 563 | int r; |
663996b3 MS |
564 | |
565 | assert(_m); | |
566 | assert(running_as >= 0); | |
e3bff60a | 567 | assert(running_as < _MANAGER_RUNNING_AS_MAX); |
663996b3 MS |
568 | |
569 | m = new0(Manager, 1); | |
570 | if (!m) | |
571 | return -ENOMEM; | |
572 | ||
14228c0d | 573 | #ifdef ENABLE_EFI |
6300502b | 574 | if (running_as == MANAGER_SYSTEM && detect_container() <= 0) |
14228c0d MB |
575 | boot_timestamps(&m->userspace_timestamp, &m->firmware_timestamp, &m->loader_timestamp); |
576 | #endif | |
577 | ||
663996b3 | 578 | m->running_as = running_as; |
663996b3 | 579 | m->exit_code = _MANAGER_EXIT_CODE_INVALID; |
60f067b4 | 580 | m->default_timer_accuracy_usec = USEC_PER_MINUTE; |
db2df898 MP |
581 | m->default_tasks_accounting = true; |
582 | m->default_tasks_max = UINT64_C(512); | |
663996b3 | 583 | |
e3bff60a MP |
584 | /* Prepare log fields we can use for structured logging */ |
585 | m->unit_log_field = unit_log_fields[running_as]; | |
586 | m->unit_log_format_string = unit_log_format_strings[running_as]; | |
587 | ||
60f067b4 | 588 | m->idle_pipe[0] = m->idle_pipe[1] = m->idle_pipe[2] = m->idle_pipe[3] = -1; |
663996b3 | 589 | |
d9dfd233 | 590 | m->pin_cgroupfs_fd = m->notify_fd = m->signal_fd = m->time_change_fd = |
6300502b MP |
591 | m->dev_autofs_fd = m->private_listen_fd = m->kdbus_fd = m->cgroup_inotify_fd = -1; |
592 | ||
663996b3 MS |
593 | m->current_job_id = 1; /* start as id #1, so that we can leave #0 around as "null-like" value */ |
594 | ||
5eef597e MP |
595 | m->ask_password_inotify_fd = -1; |
596 | m->have_ask_password = -EINVAL; /* we don't know */ | |
d9dfd233 | 597 | m->first_boot = -1; |
5eef597e MP |
598 | |
599 | m->test_run = test_run; | |
600 | ||
e735f4d4 MP |
601 | /* Reboot immediately if the user hits C-A-D more often than 7x per 2s */ |
602 | RATELIMIT_INIT(m->ctrl_alt_del_ratelimit, 2 * USEC_PER_SEC, 7); | |
603 | ||
14228c0d MB |
604 | r = manager_default_environment(m); |
605 | if (r < 0) | |
663996b3 MS |
606 | goto fail; |
607 | ||
5eef597e | 608 | r = hashmap_ensure_allocated(&m->units, &string_hash_ops); |
60f067b4 | 609 | if (r < 0) |
663996b3 MS |
610 | goto fail; |
611 | ||
5eef597e | 612 | r = hashmap_ensure_allocated(&m->jobs, NULL); |
60f067b4 | 613 | if (r < 0) |
663996b3 MS |
614 | goto fail; |
615 | ||
5eef597e | 616 | r = hashmap_ensure_allocated(&m->cgroup_unit, &string_hash_ops); |
60f067b4 | 617 | if (r < 0) |
663996b3 MS |
618 | goto fail; |
619 | ||
5eef597e | 620 | r = hashmap_ensure_allocated(&m->watch_bus, &string_hash_ops); |
60f067b4 | 621 | if (r < 0) |
663996b3 MS |
622 | goto fail; |
623 | ||
60f067b4 | 624 | r = sd_event_default(&m->event); |
663996b3 MS |
625 | if (r < 0) |
626 | goto fail; | |
627 | ||
60f067b4 | 628 | r = sd_event_add_defer(m->event, &m->run_queue_event_source, manager_dispatch_run_queue, m); |
663996b3 MS |
629 | if (r < 0) |
630 | goto fail; | |
631 | ||
60f067b4 JS |
632 | r = sd_event_source_set_priority(m->run_queue_event_source, SD_EVENT_PRIORITY_IDLE); |
633 | if (r < 0) | |
634 | goto fail; | |
635 | ||
636 | r = sd_event_source_set_enabled(m->run_queue_event_source, SD_EVENT_OFF); | |
637 | if (r < 0) | |
638 | goto fail; | |
639 | ||
e3bff60a MP |
640 | (void) sd_event_source_set_description(m->run_queue_event_source, "manager-run-queue"); |
641 | ||
60f067b4 JS |
642 | r = manager_setup_signals(m); |
643 | if (r < 0) | |
644 | goto fail; | |
645 | ||
646 | r = manager_setup_cgroup(m); | |
663996b3 MS |
647 | if (r < 0) |
648 | goto fail; | |
649 | ||
650 | r = manager_setup_time_change(m); | |
651 | if (r < 0) | |
652 | goto fail; | |
653 | ||
60f067b4 JS |
654 | m->udev = udev_new(); |
655 | if (!m->udev) { | |
656 | r = -ENOMEM; | |
657 | goto fail; | |
658 | } | |
659 | ||
660 | /* Note that we set up neither kdbus, nor the notify fd | |
661 | * here. We do that after deserialization, since they might | |
662 | * have gotten serialized across the reexec. */ | |
663996b3 MS |
663 | |
664 | m->taint_usr = dir_is_empty("/usr") > 0; | |
665 | ||
666 | *_m = m; | |
667 | return 0; | |
668 | ||
669 | fail: | |
670 | manager_free(m); | |
671 | return r; | |
672 | } | |
673 | ||
60f067b4 JS |
674 | static int manager_setup_notify(Manager *m) { |
675 | int r; | |
676 | ||
5eef597e MP |
677 | if (m->test_run) |
678 | return 0; | |
679 | ||
60f067b4 JS |
680 | if (m->notify_fd < 0) { |
681 | _cleanup_close_ int fd = -1; | |
86f210e9 | 682 | union sockaddr_union sa = { |
60f067b4 JS |
683 | .sa.sa_family = AF_UNIX, |
684 | }; | |
5eef597e | 685 | static const int one = 1; |
60f067b4 JS |
686 | |
687 | /* First free all secondary fields */ | |
6300502b | 688 | m->notify_socket = mfree(m->notify_socket); |
60f067b4 JS |
689 | m->notify_event_source = sd_event_source_unref(m->notify_event_source); |
690 | ||
691 | fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0); | |
f47781d8 MP |
692 | if (fd < 0) |
693 | return log_error_errno(errno, "Failed to allocate notification socket: %m"); | |
60f067b4 | 694 | |
db2df898 MP |
695 | fd_inc_rcvbuf(fd, NOTIFY_RCVBUF_SIZE); |
696 | ||
e3bff60a | 697 | if (m->running_as == MANAGER_SYSTEM) |
60f067b4 JS |
698 | m->notify_socket = strdup("/run/systemd/notify"); |
699 | else { | |
700 | const char *e; | |
701 | ||
702 | e = getenv("XDG_RUNTIME_DIR"); | |
703 | if (!e) { | |
f47781d8 | 704 | log_error_errno(errno, "XDG_RUNTIME_DIR is not set: %m"); |
60f067b4 JS |
705 | return -EINVAL; |
706 | } | |
707 | ||
708 | m->notify_socket = strappend(e, "/systemd/notify"); | |
709 | } | |
710 | if (!m->notify_socket) | |
711 | return log_oom(); | |
712 | ||
f47781d8 MP |
713 | (void) mkdir_parents_label(m->notify_socket, 0755); |
714 | (void) unlink(m->notify_socket); | |
715 | ||
60f067b4 JS |
716 | strncpy(sa.un.sun_path, m->notify_socket, sizeof(sa.un.sun_path)-1); |
717 | r = bind(fd, &sa.sa, offsetof(struct sockaddr_un, sun_path) + strlen(sa.un.sun_path)); | |
f47781d8 MP |
718 | if (r < 0) |
719 | return log_error_errno(errno, "bind(%s) failed: %m", sa.un.sun_path); | |
60f067b4 JS |
720 | |
721 | r = setsockopt(fd, SOL_SOCKET, SO_PASSCRED, &one, sizeof(one)); | |
f47781d8 MP |
722 | if (r < 0) |
723 | return log_error_errno(errno, "SO_PASSCRED failed: %m"); | |
60f067b4 JS |
724 | |
725 | m->notify_fd = fd; | |
726 | fd = -1; | |
727 | ||
728 | log_debug("Using notification socket %s", m->notify_socket); | |
729 | } | |
730 | ||
731 | if (!m->notify_event_source) { | |
732 | r = sd_event_add_io(m->event, &m->notify_event_source, m->notify_fd, EPOLLIN, manager_dispatch_notify_fd, m); | |
f47781d8 MP |
733 | if (r < 0) |
734 | return log_error_errno(r, "Failed to allocate notify event source: %m"); | |
60f067b4 JS |
735 | |
736 | /* Process signals a bit earlier than SIGCHLD, so that we can | |
737 | * still identify to which service an exit message belongs */ | |
db2df898 | 738 | r = sd_event_source_set_priority(m->notify_event_source, SD_EVENT_PRIORITY_NORMAL-7); |
f47781d8 MP |
739 | if (r < 0) |
740 | return log_error_errno(r, "Failed to set priority of notify event source: %m"); | |
e3bff60a MP |
741 | |
742 | (void) sd_event_source_set_description(m->notify_event_source, "manager-notify"); | |
60f067b4 JS |
743 | } |
744 | ||
745 | return 0; | |
746 | } | |
747 | ||
748 | static int manager_setup_kdbus(Manager *m) { | |
60f067b4 | 749 | _cleanup_free_ char *p = NULL; |
60f067b4 | 750 | |
60f067b4 JS |
751 | assert(m); |
752 | ||
5eef597e | 753 | if (m->test_run || m->kdbus_fd >= 0) |
60f067b4 | 754 | return 0; |
e3bff60a MP |
755 | if (!is_kdbus_available()) |
756 | return -ESOCKTNOSUPPORT; | |
60f067b4 | 757 | |
f47781d8 | 758 | m->kdbus_fd = bus_kernel_create_bus( |
e3bff60a MP |
759 | m->running_as == MANAGER_SYSTEM ? "system" : "user", |
760 | m->running_as == MANAGER_SYSTEM, &p); | |
60f067b4 | 761 | |
f47781d8 MP |
762 | if (m->kdbus_fd < 0) |
763 | return log_debug_errno(m->kdbus_fd, "Failed to set up kdbus: %m"); | |
764 | ||
765 | log_debug("Successfully set up kdbus on %s", p); | |
60f067b4 JS |
766 | |
767 | return 0; | |
768 | } | |
769 | ||
770 | static int manager_connect_bus(Manager *m, bool reexecuting) { | |
771 | bool try_bus_connect; | |
772 | ||
773 | assert(m); | |
774 | ||
5eef597e MP |
775 | if (m->test_run) |
776 | return 0; | |
777 | ||
60f067b4 JS |
778 | try_bus_connect = |
779 | m->kdbus_fd >= 0 || | |
780 | reexecuting || | |
e3bff60a | 781 | (m->running_as == MANAGER_USER && getenv("DBUS_SESSION_BUS_ADDRESS")); |
60f067b4 | 782 | |
e3bff60a | 783 | /* Try to connect to the buses, if possible. */ |
60f067b4 JS |
784 | return bus_init(m, try_bus_connect); |
785 | } | |
786 | ||
663996b3 MS |
787 | static unsigned manager_dispatch_cleanup_queue(Manager *m) { |
788 | Unit *u; | |
789 | unsigned n = 0; | |
790 | ||
791 | assert(m); | |
792 | ||
793 | while ((u = m->cleanup_queue)) { | |
794 | assert(u->in_cleanup_queue); | |
795 | ||
796 | unit_free(u); | |
797 | n++; | |
798 | } | |
799 | ||
800 | return n; | |
801 | } | |
802 | ||
803 | enum { | |
804 | GC_OFFSET_IN_PATH, /* This one is on the path we were traveling */ | |
805 | GC_OFFSET_UNSURE, /* No clue */ | |
806 | GC_OFFSET_GOOD, /* We still need this unit */ | |
807 | GC_OFFSET_BAD, /* We don't need this unit anymore */ | |
808 | _GC_OFFSET_MAX | |
809 | }; | |
810 | ||
811 | static void unit_gc_sweep(Unit *u, unsigned gc_marker) { | |
812 | Iterator i; | |
813 | Unit *other; | |
814 | bool is_bad; | |
815 | ||
816 | assert(u); | |
817 | ||
818 | if (u->gc_marker == gc_marker + GC_OFFSET_GOOD || | |
819 | u->gc_marker == gc_marker + GC_OFFSET_BAD || | |
820 | u->gc_marker == gc_marker + GC_OFFSET_IN_PATH) | |
821 | return; | |
822 | ||
823 | if (u->in_cleanup_queue) | |
824 | goto bad; | |
825 | ||
826 | if (unit_check_gc(u)) | |
827 | goto good; | |
828 | ||
829 | u->gc_marker = gc_marker + GC_OFFSET_IN_PATH; | |
830 | ||
831 | is_bad = true; | |
832 | ||
833 | SET_FOREACH(other, u->dependencies[UNIT_REFERENCED_BY], i) { | |
834 | unit_gc_sweep(other, gc_marker); | |
835 | ||
836 | if (other->gc_marker == gc_marker + GC_OFFSET_GOOD) | |
837 | goto good; | |
838 | ||
839 | if (other->gc_marker != gc_marker + GC_OFFSET_BAD) | |
840 | is_bad = false; | |
841 | } | |
842 | ||
843 | if (is_bad) | |
844 | goto bad; | |
845 | ||
846 | /* We were unable to find anything out about this entry, so | |
847 | * let's investigate it later */ | |
848 | u->gc_marker = gc_marker + GC_OFFSET_UNSURE; | |
849 | unit_add_to_gc_queue(u); | |
850 | return; | |
851 | ||
852 | bad: | |
853 | /* We definitely know that this one is not useful anymore, so | |
854 | * let's mark it for deletion */ | |
855 | u->gc_marker = gc_marker + GC_OFFSET_BAD; | |
856 | unit_add_to_cleanup_queue(u); | |
857 | return; | |
858 | ||
859 | good: | |
860 | u->gc_marker = gc_marker + GC_OFFSET_GOOD; | |
861 | } | |
862 | ||
863 | static unsigned manager_dispatch_gc_queue(Manager *m) { | |
864 | Unit *u; | |
865 | unsigned n = 0; | |
866 | unsigned gc_marker; | |
867 | ||
868 | assert(m); | |
869 | ||
14228c0d | 870 | /* log_debug("Running GC..."); */ |
663996b3 MS |
871 | |
872 | m->gc_marker += _GC_OFFSET_MAX; | |
873 | if (m->gc_marker + _GC_OFFSET_MAX <= _GC_OFFSET_MAX) | |
874 | m->gc_marker = 1; | |
875 | ||
876 | gc_marker = m->gc_marker; | |
877 | ||
878 | while ((u = m->gc_queue)) { | |
879 | assert(u->in_gc_queue); | |
880 | ||
881 | unit_gc_sweep(u, gc_marker); | |
882 | ||
60f067b4 | 883 | LIST_REMOVE(gc_queue, m->gc_queue, u); |
663996b3 MS |
884 | u->in_gc_queue = false; |
885 | ||
886 | n++; | |
887 | ||
888 | if (u->gc_marker == gc_marker + GC_OFFSET_BAD || | |
889 | u->gc_marker == gc_marker + GC_OFFSET_UNSURE) { | |
e3bff60a MP |
890 | if (u->id) |
891 | log_unit_debug(u, "Collecting."); | |
663996b3 MS |
892 | u->gc_marker = gc_marker + GC_OFFSET_BAD; |
893 | unit_add_to_cleanup_queue(u); | |
894 | } | |
895 | } | |
896 | ||
897 | m->n_in_gc_queue = 0; | |
663996b3 MS |
898 | |
899 | return n; | |
900 | } | |
901 | ||
902 | static void manager_clear_jobs_and_units(Manager *m) { | |
903 | Unit *u; | |
904 | ||
905 | assert(m); | |
906 | ||
907 | while ((u = hashmap_first(m->units))) | |
908 | unit_free(u); | |
909 | ||
910 | manager_dispatch_cleanup_queue(m); | |
911 | ||
912 | assert(!m->load_queue); | |
913 | assert(!m->run_queue); | |
914 | assert(!m->dbus_unit_queue); | |
915 | assert(!m->dbus_job_queue); | |
916 | assert(!m->cleanup_queue); | |
917 | assert(!m->gc_queue); | |
918 | ||
919 | assert(hashmap_isempty(m->jobs)); | |
920 | assert(hashmap_isempty(m->units)); | |
921 | ||
922 | m->n_on_console = 0; | |
923 | m->n_running_jobs = 0; | |
924 | } | |
925 | ||
f47781d8 | 926 | Manager* manager_free(Manager *m) { |
663996b3 MS |
927 | UnitType c; |
928 | int i; | |
929 | ||
f47781d8 MP |
930 | if (!m) |
931 | return NULL; | |
663996b3 MS |
932 | |
933 | manager_clear_jobs_and_units(m); | |
934 | ||
935 | for (c = 0; c < _UNIT_TYPE_MAX; c++) | |
936 | if (unit_vtable[c]->shutdown) | |
937 | unit_vtable[c]->shutdown(m); | |
938 | ||
939 | /* If we reexecute ourselves, we keep the root cgroup | |
940 | * around */ | |
941 | manager_shutdown_cgroup(m, m->exit_code != MANAGER_REEXECUTE); | |
942 | ||
943 | manager_undo_generators(m); | |
944 | ||
945 | bus_done(m); | |
946 | ||
947 | hashmap_free(m->units); | |
948 | hashmap_free(m->jobs); | |
60f067b4 JS |
949 | hashmap_free(m->watch_pids1); |
950 | hashmap_free(m->watch_pids2); | |
663996b3 MS |
951 | hashmap_free(m->watch_bus); |
952 | ||
60f067b4 JS |
953 | set_free(m->startup_units); |
954 | set_free(m->failed_units); | |
955 | ||
956 | sd_event_source_unref(m->signal_event_source); | |
957 | sd_event_source_unref(m->notify_event_source); | |
958 | sd_event_source_unref(m->time_change_event_source); | |
959 | sd_event_source_unref(m->jobs_in_progress_event_source); | |
60f067b4 JS |
960 | sd_event_source_unref(m->run_queue_event_source); |
961 | ||
962 | safe_close(m->signal_fd); | |
963 | safe_close(m->notify_fd); | |
964 | safe_close(m->time_change_fd); | |
965 | safe_close(m->kdbus_fd); | |
966 | ||
5eef597e MP |
967 | manager_close_ask_password(m); |
968 | ||
60f067b4 JS |
969 | manager_close_idle_pipe(m); |
970 | ||
971 | udev_unref(m->udev); | |
972 | sd_event_unref(m->event); | |
663996b3 MS |
973 | |
974 | free(m->notify_socket); | |
975 | ||
976 | lookup_paths_free(&m->lookup_paths); | |
977 | strv_free(m->environment); | |
978 | ||
14228c0d | 979 | hashmap_free(m->cgroup_unit); |
663996b3 MS |
980 | set_free_free(m->unit_path_cache); |
981 | ||
663996b3 MS |
982 | free(m->switch_root); |
983 | free(m->switch_root_init); | |
984 | ||
60f067b4 | 985 | for (i = 0; i < _RLIMIT_MAX; i++) |
4c89c718 | 986 | m->rlimit[i] = mfree(m->rlimit[i]); |
663996b3 | 987 | |
14228c0d MB |
988 | assert(hashmap_isempty(m->units_requiring_mounts_for)); |
989 | hashmap_free(m->units_requiring_mounts_for); | |
990 | ||
663996b3 | 991 | free(m); |
f47781d8 | 992 | return NULL; |
663996b3 MS |
993 | } |
994 | ||
db2df898 | 995 | void manager_enumerate(Manager *m) { |
663996b3 MS |
996 | UnitType c; |
997 | ||
998 | assert(m); | |
999 | ||
1000 | /* Let's ask every type to load all units from disk/kernel | |
1001 | * that it might know */ | |
e735f4d4 | 1002 | for (c = 0; c < _UNIT_TYPE_MAX; c++) { |
e3bff60a MP |
1003 | if (!unit_type_supported(c)) { |
1004 | log_debug("Unit type .%s is not supported on this system.", unit_type_to_string(c)); | |
e735f4d4 | 1005 | continue; |
14228c0d | 1006 | } |
663996b3 | 1007 | |
e735f4d4 MP |
1008 | if (!unit_vtable[c]->enumerate) |
1009 | continue; | |
1010 | ||
db2df898 | 1011 | unit_vtable[c]->enumerate(m); |
e735f4d4 MP |
1012 | } |
1013 | ||
663996b3 | 1014 | manager_dispatch_load_queue(m); |
663996b3 MS |
1015 | } |
1016 | ||
e3bff60a | 1017 | static void manager_coldplug(Manager *m) { |
663996b3 MS |
1018 | Iterator i; |
1019 | Unit *u; | |
1020 | char *k; | |
e3bff60a | 1021 | int r; |
663996b3 MS |
1022 | |
1023 | assert(m); | |
1024 | ||
1025 | /* Then, let's set up their initial state. */ | |
1026 | HASHMAP_FOREACH_KEY(u, k, m->units, i) { | |
1027 | ||
1028 | /* ignore aliases */ | |
1029 | if (u->id != k) | |
1030 | continue; | |
1031 | ||
e3bff60a MP |
1032 | r = unit_coldplug(u); |
1033 | if (r < 0) | |
1034 | log_warning_errno(r, "We couldn't coldplug %s, proceeding anyway: %m", u->id); | |
663996b3 | 1035 | } |
663996b3 MS |
1036 | } |
1037 | ||
1038 | static void manager_build_unit_path_cache(Manager *m) { | |
1039 | char **i; | |
5eef597e | 1040 | _cleanup_closedir_ DIR *d = NULL; |
663996b3 MS |
1041 | int r; |
1042 | ||
1043 | assert(m); | |
1044 | ||
1045 | set_free_free(m->unit_path_cache); | |
1046 | ||
5eef597e | 1047 | m->unit_path_cache = set_new(&string_hash_ops); |
663996b3 MS |
1048 | if (!m->unit_path_cache) { |
1049 | log_error("Failed to allocate unit path cache."); | |
1050 | return; | |
1051 | } | |
1052 | ||
1053 | /* This simply builds a list of files we know exist, so that | |
1054 | * we don't always have to go to disk */ | |
1055 | ||
1056 | STRV_FOREACH(i, m->lookup_paths.unit_path) { | |
1057 | struct dirent *de; | |
1058 | ||
1059 | d = opendir(*i); | |
1060 | if (!d) { | |
1061 | if (errno != ENOENT) | |
f47781d8 | 1062 | log_error_errno(errno, "Failed to open directory %s: %m", *i); |
663996b3 MS |
1063 | continue; |
1064 | } | |
1065 | ||
1066 | while ((de = readdir(d))) { | |
1067 | char *p; | |
1068 | ||
e735f4d4 | 1069 | if (hidden_file(de->d_name)) |
663996b3 MS |
1070 | continue; |
1071 | ||
1072 | p = strjoin(streq(*i, "/") ? "" : *i, "/", de->d_name, NULL); | |
1073 | if (!p) { | |
1074 | r = -ENOMEM; | |
1075 | goto fail; | |
1076 | } | |
1077 | ||
1078 | r = set_consume(m->unit_path_cache, p); | |
1079 | if (r < 0) | |
1080 | goto fail; | |
1081 | } | |
1082 | ||
6300502b | 1083 | d = safe_closedir(d); |
663996b3 MS |
1084 | } |
1085 | ||
1086 | return; | |
1087 | ||
1088 | fail: | |
f47781d8 | 1089 | log_error_errno(r, "Failed to build unit path cache: %m"); |
663996b3 MS |
1090 | |
1091 | set_free_free(m->unit_path_cache); | |
1092 | m->unit_path_cache = NULL; | |
1093 | } | |
1094 | ||
60f067b4 | 1095 | |
db2df898 | 1096 | static void manager_distribute_fds(Manager *m, FDSet *fds) { |
60f067b4 | 1097 | Iterator i; |
db2df898 | 1098 | Unit *u; |
60f067b4 JS |
1099 | |
1100 | assert(m); | |
1101 | ||
1102 | HASHMAP_FOREACH(u, m->units, i) { | |
1103 | ||
1104 | if (fdset_size(fds) <= 0) | |
1105 | break; | |
1106 | ||
db2df898 MP |
1107 | if (!UNIT_VTABLE(u)->distribute_fds) |
1108 | continue; | |
60f067b4 | 1109 | |
db2df898 MP |
1110 | UNIT_VTABLE(u)->distribute_fds(u, fds); |
1111 | } | |
60f067b4 JS |
1112 | } |
1113 | ||
663996b3 MS |
1114 | int manager_startup(Manager *m, FILE *serialization, FDSet *fds) { |
1115 | int r, q; | |
1116 | ||
1117 | assert(m); | |
1118 | ||
14228c0d | 1119 | dual_timestamp_get(&m->generators_start_timestamp); |
e735f4d4 | 1120 | r = manager_run_generators(m); |
14228c0d | 1121 | dual_timestamp_get(&m->generators_finish_timestamp); |
e735f4d4 MP |
1122 | if (r < 0) |
1123 | return r; | |
663996b3 MS |
1124 | |
1125 | r = lookup_paths_init( | |
1126 | &m->lookup_paths, m->running_as, true, | |
60f067b4 | 1127 | NULL, |
663996b3 MS |
1128 | m->generator_unit_path, |
1129 | m->generator_unit_path_early, | |
1130 | m->generator_unit_path_late); | |
1131 | if (r < 0) | |
1132 | return r; | |
1133 | ||
1134 | manager_build_unit_path_cache(m); | |
1135 | ||
1136 | /* If we will deserialize make sure that during enumeration | |
1137 | * this is already known, so we increase the counter here | |
1138 | * already */ | |
1139 | if (serialization) | |
1140 | m->n_reloading ++; | |
1141 | ||
1142 | /* First, enumerate what we can from all config files */ | |
60f067b4 | 1143 | dual_timestamp_get(&m->units_load_start_timestamp); |
db2df898 | 1144 | manager_enumerate(m); |
60f067b4 | 1145 | dual_timestamp_get(&m->units_load_finish_timestamp); |
663996b3 MS |
1146 | |
1147 | /* Second, deserialize if there is something to deserialize */ | |
5eef597e MP |
1148 | if (serialization) |
1149 | r = manager_deserialize(m, serialization, fds); | |
663996b3 MS |
1150 | |
1151 | /* Any fds left? Find some unit which wants them. This is | |
1152 | * useful to allow container managers to pass some file | |
1153 | * descriptors to us pre-initialized. This enables | |
1154 | * socket-based activation of entire containers. */ | |
db2df898 | 1155 | manager_distribute_fds(m, fds); |
663996b3 | 1156 | |
60f067b4 JS |
1157 | /* We might have deserialized the notify fd, but if we didn't |
1158 | * then let's create the bus now */ | |
5eef597e MP |
1159 | q = manager_setup_notify(m); |
1160 | if (q < 0 && r == 0) | |
1161 | r = q; | |
60f067b4 JS |
1162 | |
1163 | /* We might have deserialized the kdbus control fd, but if we | |
1164 | * didn't, then let's create the bus now. */ | |
1165 | manager_setup_kdbus(m); | |
1166 | manager_connect_bus(m, !!serialization); | |
1167 | bus_track_coldplug(m, &m->subscribed, &m->deserialized_subscribed); | |
1168 | ||
663996b3 | 1169 | /* Third, fire things up! */ |
e3bff60a | 1170 | manager_coldplug(m); |
663996b3 MS |
1171 | |
1172 | if (serialization) { | |
1173 | assert(m->n_reloading > 0); | |
1174 | m->n_reloading --; | |
14228c0d MB |
1175 | |
1176 | /* Let's wait for the UnitNew/JobNew messages being | |
1177 | * sent, before we notify that the reload is | |
1178 | * finished */ | |
1179 | m->send_reloading_done = true; | |
663996b3 MS |
1180 | } |
1181 | ||
1182 | return r; | |
1183 | } | |
1184 | ||
db2df898 | 1185 | int manager_add_job(Manager *m, JobType type, Unit *unit, JobMode mode, sd_bus_error *e, Job **_ret) { |
663996b3 MS |
1186 | int r; |
1187 | Transaction *tr; | |
1188 | ||
1189 | assert(m); | |
1190 | assert(type < _JOB_TYPE_MAX); | |
1191 | assert(unit); | |
1192 | assert(mode < _JOB_MODE_MAX); | |
1193 | ||
f47781d8 MP |
1194 | if (mode == JOB_ISOLATE && type != JOB_START) |
1195 | return sd_bus_error_setf(e, SD_BUS_ERROR_INVALID_ARGS, "Isolate is only valid for start."); | |
663996b3 | 1196 | |
f47781d8 MP |
1197 | if (mode == JOB_ISOLATE && !unit->allow_isolate) |
1198 | return sd_bus_error_setf(e, BUS_ERROR_NO_ISOLATION, "Operation refused, unit may not be isolated."); | |
663996b3 | 1199 | |
e3bff60a | 1200 | log_unit_debug(unit, "Trying to enqueue job %s/%s/%s", unit->id, job_type_to_string(type), job_mode_to_string(mode)); |
663996b3 | 1201 | |
e3bff60a | 1202 | type = job_type_collapse(type, unit); |
663996b3 MS |
1203 | |
1204 | tr = transaction_new(mode == JOB_REPLACE_IRREVERSIBLY); | |
1205 | if (!tr) | |
1206 | return -ENOMEM; | |
1207 | ||
db2df898 | 1208 | r = transaction_add_job_and_dependencies(tr, type, unit, NULL, true, false, |
663996b3 MS |
1209 | mode == JOB_IGNORE_DEPENDENCIES || mode == JOB_IGNORE_REQUIREMENTS, |
1210 | mode == JOB_IGNORE_DEPENDENCIES, e); | |
1211 | if (r < 0) | |
1212 | goto tr_abort; | |
1213 | ||
1214 | if (mode == JOB_ISOLATE) { | |
1215 | r = transaction_add_isolate_jobs(tr, m); | |
1216 | if (r < 0) | |
1217 | goto tr_abort; | |
1218 | } | |
1219 | ||
1220 | r = transaction_activate(tr, m, mode, e); | |
1221 | if (r < 0) | |
1222 | goto tr_abort; | |
1223 | ||
e3bff60a | 1224 | log_unit_debug(unit, |
663996b3 MS |
1225 | "Enqueued job %s/%s as %u", unit->id, |
1226 | job_type_to_string(type), (unsigned) tr->anchor_job->id); | |
1227 | ||
1228 | if (_ret) | |
1229 | *_ret = tr->anchor_job; | |
1230 | ||
1231 | transaction_free(tr); | |
1232 | return 0; | |
1233 | ||
1234 | tr_abort: | |
1235 | transaction_abort(tr); | |
1236 | transaction_free(tr); | |
1237 | return r; | |
1238 | } | |
1239 | ||
db2df898 | 1240 | int manager_add_job_by_name(Manager *m, JobType type, const char *name, JobMode mode, sd_bus_error *e, Job **ret) { |
663996b3 MS |
1241 | Unit *unit; |
1242 | int r; | |
1243 | ||
1244 | assert(m); | |
1245 | assert(type < _JOB_TYPE_MAX); | |
1246 | assert(name); | |
1247 | assert(mode < _JOB_MODE_MAX); | |
1248 | ||
1249 | r = manager_load_unit(m, name, NULL, NULL, &unit); | |
1250 | if (r < 0) | |
1251 | return r; | |
1252 | ||
db2df898 MP |
1253 | return manager_add_job(m, type, unit, mode, e, ret); |
1254 | } | |
1255 | ||
1256 | int manager_add_job_by_name_and_warn(Manager *m, JobType type, const char *name, JobMode mode, Job **ret) { | |
4c89c718 | 1257 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
db2df898 MP |
1258 | int r; |
1259 | ||
1260 | assert(m); | |
1261 | assert(type < _JOB_TYPE_MAX); | |
1262 | assert(name); | |
1263 | assert(mode < _JOB_MODE_MAX); | |
1264 | ||
1265 | r = manager_add_job_by_name(m, type, name, mode, &error, ret); | |
1266 | if (r < 0) | |
1267 | return log_warning_errno(r, "Failed to enqueue %s job for %s: %s", job_mode_to_string(mode), name, bus_error_message(&error, r)); | |
1268 | ||
1269 | return r; | |
663996b3 MS |
1270 | } |
1271 | ||
1272 | Job *manager_get_job(Manager *m, uint32_t id) { | |
1273 | assert(m); | |
1274 | ||
1275 | return hashmap_get(m->jobs, UINT32_TO_PTR(id)); | |
1276 | } | |
1277 | ||
1278 | Unit *manager_get_unit(Manager *m, const char *name) { | |
1279 | assert(m); | |
1280 | assert(name); | |
1281 | ||
1282 | return hashmap_get(m->units, name); | |
1283 | } | |
1284 | ||
1285 | unsigned manager_dispatch_load_queue(Manager *m) { | |
1286 | Unit *u; | |
1287 | unsigned n = 0; | |
1288 | ||
1289 | assert(m); | |
1290 | ||
1291 | /* Make sure we are not run recursively */ | |
1292 | if (m->dispatching_load_queue) | |
1293 | return 0; | |
1294 | ||
1295 | m->dispatching_load_queue = true; | |
1296 | ||
1297 | /* Dispatches the load queue. Takes a unit from the queue and | |
1298 | * tries to load its data until the queue is empty */ | |
1299 | ||
1300 | while ((u = m->load_queue)) { | |
1301 | assert(u->in_load_queue); | |
1302 | ||
1303 | unit_load(u); | |
1304 | n++; | |
1305 | } | |
1306 | ||
1307 | m->dispatching_load_queue = false; | |
1308 | return n; | |
1309 | } | |
1310 | ||
14228c0d MB |
1311 | int manager_load_unit_prepare( |
1312 | Manager *m, | |
1313 | const char *name, | |
1314 | const char *path, | |
60f067b4 | 1315 | sd_bus_error *e, |
14228c0d MB |
1316 | Unit **_ret) { |
1317 | ||
663996b3 MS |
1318 | Unit *ret; |
1319 | UnitType t; | |
1320 | int r; | |
1321 | ||
1322 | assert(m); | |
1323 | assert(name || path); | |
1324 | ||
1325 | /* This will prepare the unit for loading, but not actually | |
1326 | * load anything from disk. */ | |
1327 | ||
60f067b4 JS |
1328 | if (path && !is_path(path)) |
1329 | return sd_bus_error_setf(e, SD_BUS_ERROR_INVALID_ARGS, "Path %s is not absolute.", path); | |
663996b3 MS |
1330 | |
1331 | if (!name) | |
60f067b4 | 1332 | name = basename(path); |
663996b3 MS |
1333 | |
1334 | t = unit_name_to_type(name); | |
1335 | ||
e3bff60a | 1336 | if (t == _UNIT_TYPE_INVALID || !unit_name_is_valid(name, UNIT_NAME_PLAIN|UNIT_NAME_INSTANCE)) |
60f067b4 | 1337 | return sd_bus_error_setf(e, SD_BUS_ERROR_INVALID_ARGS, "Unit name %s is not valid.", name); |
663996b3 MS |
1338 | |
1339 | ret = manager_get_unit(m, name); | |
1340 | if (ret) { | |
1341 | *_ret = ret; | |
1342 | return 1; | |
1343 | } | |
1344 | ||
1345 | ret = unit_new(m, unit_vtable[t]->object_size); | |
1346 | if (!ret) | |
1347 | return -ENOMEM; | |
1348 | ||
1349 | if (path) { | |
1350 | ret->fragment_path = strdup(path); | |
1351 | if (!ret->fragment_path) { | |
1352 | unit_free(ret); | |
1353 | return -ENOMEM; | |
1354 | } | |
1355 | } | |
1356 | ||
14228c0d MB |
1357 | r = unit_add_name(ret, name); |
1358 | if (r < 0) { | |
663996b3 MS |
1359 | unit_free(ret); |
1360 | return r; | |
1361 | } | |
1362 | ||
1363 | unit_add_to_load_queue(ret); | |
1364 | unit_add_to_dbus_queue(ret); | |
1365 | unit_add_to_gc_queue(ret); | |
1366 | ||
1367 | if (_ret) | |
1368 | *_ret = ret; | |
1369 | ||
1370 | return 0; | |
1371 | } | |
1372 | ||
14228c0d MB |
1373 | int manager_load_unit( |
1374 | Manager *m, | |
1375 | const char *name, | |
1376 | const char *path, | |
60f067b4 | 1377 | sd_bus_error *e, |
14228c0d MB |
1378 | Unit **_ret) { |
1379 | ||
663996b3 MS |
1380 | int r; |
1381 | ||
1382 | assert(m); | |
1383 | ||
1384 | /* This will load the service information files, but not actually | |
1385 | * start any services or anything. */ | |
1386 | ||
1387 | r = manager_load_unit_prepare(m, name, path, e, _ret); | |
1388 | if (r != 0) | |
1389 | return r; | |
1390 | ||
1391 | manager_dispatch_load_queue(m); | |
1392 | ||
1393 | if (_ret) | |
1394 | *_ret = unit_follow_merge(*_ret); | |
1395 | ||
1396 | return 0; | |
1397 | } | |
1398 | ||
1399 | void manager_dump_jobs(Manager *s, FILE *f, const char *prefix) { | |
1400 | Iterator i; | |
1401 | Job *j; | |
1402 | ||
1403 | assert(s); | |
1404 | assert(f); | |
1405 | ||
1406 | HASHMAP_FOREACH(j, s->jobs, i) | |
1407 | job_dump(j, f, prefix); | |
1408 | } | |
1409 | ||
1410 | void manager_dump_units(Manager *s, FILE *f, const char *prefix) { | |
1411 | Iterator i; | |
1412 | Unit *u; | |
1413 | const char *t; | |
1414 | ||
1415 | assert(s); | |
1416 | assert(f); | |
1417 | ||
1418 | HASHMAP_FOREACH_KEY(u, t, s->units, i) | |
1419 | if (u->id == t) | |
1420 | unit_dump(u, f, prefix); | |
1421 | } | |
1422 | ||
1423 | void manager_clear_jobs(Manager *m) { | |
1424 | Job *j; | |
1425 | ||
1426 | assert(m); | |
1427 | ||
1428 | while ((j = hashmap_first(m->jobs))) | |
1429 | /* No need to recurse. We're cancelling all jobs. */ | |
1430 | job_finish_and_invalidate(j, JOB_CANCELED, false); | |
1431 | } | |
1432 | ||
60f067b4 JS |
1433 | static int manager_dispatch_run_queue(sd_event_source *source, void *userdata) { |
1434 | Manager *m = userdata; | |
663996b3 | 1435 | Job *j; |
663996b3 | 1436 | |
60f067b4 JS |
1437 | assert(source); |
1438 | assert(m); | |
663996b3 MS |
1439 | |
1440 | while ((j = m->run_queue)) { | |
1441 | assert(j->installed); | |
1442 | assert(j->in_run_queue); | |
1443 | ||
1444 | job_run_and_invalidate(j); | |
663996b3 MS |
1445 | } |
1446 | ||
663996b3 MS |
1447 | if (m->n_running_jobs > 0) |
1448 | manager_watch_jobs_in_progress(m); | |
1449 | ||
14228c0d MB |
1450 | if (m->n_on_console > 0) |
1451 | manager_watch_idle_pipe(m); | |
1452 | ||
60f067b4 | 1453 | return 1; |
663996b3 MS |
1454 | } |
1455 | ||
60f067b4 | 1456 | static unsigned manager_dispatch_dbus_queue(Manager *m) { |
663996b3 MS |
1457 | Job *j; |
1458 | Unit *u; | |
1459 | unsigned n = 0; | |
1460 | ||
1461 | assert(m); | |
1462 | ||
1463 | if (m->dispatching_dbus_queue) | |
1464 | return 0; | |
1465 | ||
1466 | m->dispatching_dbus_queue = true; | |
1467 | ||
1468 | while ((u = m->dbus_unit_queue)) { | |
1469 | assert(u->in_dbus_queue); | |
1470 | ||
1471 | bus_unit_send_change_signal(u); | |
1472 | n++; | |
1473 | } | |
1474 | ||
1475 | while ((j = m->dbus_job_queue)) { | |
1476 | assert(j->in_dbus_queue); | |
1477 | ||
1478 | bus_job_send_change_signal(j); | |
1479 | n++; | |
1480 | } | |
1481 | ||
1482 | m->dispatching_dbus_queue = false; | |
14228c0d MB |
1483 | |
1484 | if (m->send_reloading_done) { | |
1485 | m->send_reloading_done = false; | |
1486 | ||
60f067b4 | 1487 | bus_manager_send_reloading(m, false); |
14228c0d MB |
1488 | } |
1489 | ||
60f067b4 JS |
1490 | if (m->queued_message) |
1491 | bus_send_queued_message(m); | |
1492 | ||
663996b3 MS |
1493 | return n; |
1494 | } | |
1495 | ||
db2df898 | 1496 | static void manager_invoke_notify_message(Manager *m, Unit *u, pid_t pid, const char *buf, size_t n, FDSet *fds) { |
60f067b4 JS |
1497 | _cleanup_strv_free_ char **tags = NULL; |
1498 | ||
1499 | assert(m); | |
1500 | assert(u); | |
1501 | assert(buf); | |
1502 | assert(n > 0); | |
1503 | ||
1504 | tags = strv_split(buf, "\n\r"); | |
1505 | if (!tags) { | |
1506 | log_oom(); | |
1507 | return; | |
1508 | } | |
1509 | ||
60f067b4 | 1510 | if (UNIT_VTABLE(u)->notify_message) |
e735f4d4 | 1511 | UNIT_VTABLE(u)->notify_message(u, pid, tags, fds); |
e3bff60a MP |
1512 | else |
1513 | log_unit_debug(u, "Got notification message for unit. Ignoring."); | |
60f067b4 JS |
1514 | } |
1515 | ||
1516 | static int manager_dispatch_notify_fd(sd_event_source *source, int fd, uint32_t revents, void *userdata) { | |
db2df898 | 1517 | _cleanup_fdset_free_ FDSet *fds = NULL; |
60f067b4 | 1518 | Manager *m = userdata; |
db2df898 MP |
1519 | |
1520 | char buf[NOTIFY_BUFFER_MAX+1]; | |
1521 | struct iovec iovec = { | |
1522 | .iov_base = buf, | |
1523 | .iov_len = sizeof(buf)-1, | |
1524 | }; | |
1525 | union { | |
1526 | struct cmsghdr cmsghdr; | |
1527 | uint8_t buf[CMSG_SPACE(sizeof(struct ucred)) + | |
1528 | CMSG_SPACE(sizeof(int) * NOTIFY_FD_MAX)]; | |
1529 | } control = {}; | |
1530 | struct msghdr msghdr = { | |
1531 | .msg_iov = &iovec, | |
1532 | .msg_iovlen = 1, | |
1533 | .msg_control = &control, | |
1534 | .msg_controllen = sizeof(control), | |
1535 | }; | |
1536 | ||
1537 | struct cmsghdr *cmsg; | |
1538 | struct ucred *ucred = NULL; | |
1539 | bool found = false; | |
1540 | Unit *u1, *u2, *u3; | |
1541 | int r, *fd_array = NULL; | |
1542 | unsigned n_fds = 0; | |
663996b3 MS |
1543 | ssize_t n; |
1544 | ||
1545 | assert(m); | |
60f067b4 JS |
1546 | assert(m->notify_fd == fd); |
1547 | ||
1548 | if (revents != EPOLLIN) { | |
1549 | log_warning("Got unexpected poll event for notify fd."); | |
1550 | return 0; | |
1551 | } | |
663996b3 | 1552 | |
db2df898 MP |
1553 | n = recvmsg(m->notify_fd, &msghdr, MSG_DONTWAIT|MSG_CMSG_CLOEXEC); |
1554 | if (n < 0) { | |
1555 | if (errno == EAGAIN || errno == EINTR) | |
1556 | return 0; | |
663996b3 | 1557 | |
db2df898 MP |
1558 | return -errno; |
1559 | } | |
663996b3 | 1560 | |
db2df898 MP |
1561 | CMSG_FOREACH(cmsg, &msghdr) { |
1562 | if (cmsg->cmsg_level == SOL_SOCKET && cmsg->cmsg_type == SCM_RIGHTS) { | |
e735f4d4 | 1563 | |
db2df898 MP |
1564 | fd_array = (int*) CMSG_DATA(cmsg); |
1565 | n_fds = (cmsg->cmsg_len - CMSG_LEN(0)) / sizeof(int); | |
e735f4d4 | 1566 | |
db2df898 MP |
1567 | } else if (cmsg->cmsg_level == SOL_SOCKET && |
1568 | cmsg->cmsg_type == SCM_CREDENTIALS && | |
1569 | cmsg->cmsg_len == CMSG_LEN(sizeof(struct ucred))) { | |
e735f4d4 | 1570 | |
db2df898 | 1571 | ucred = (struct ucred*) CMSG_DATA(cmsg); |
e735f4d4 | 1572 | } |
db2df898 | 1573 | } |
e735f4d4 | 1574 | |
db2df898 MP |
1575 | if (n_fds > 0) { |
1576 | assert(fd_array); | |
e735f4d4 | 1577 | |
db2df898 MP |
1578 | r = fdset_new_array(&fds, fd_array, n_fds); |
1579 | if (r < 0) { | |
1580 | close_many(fd_array, n_fds); | |
1581 | return log_oom(); | |
e735f4d4 | 1582 | } |
db2df898 | 1583 | } |
e735f4d4 | 1584 | |
db2df898 MP |
1585 | if (!ucred || ucred->pid <= 0) { |
1586 | log_warning("Received notify message without valid credentials. Ignoring."); | |
1587 | return 0; | |
1588 | } | |
663996b3 | 1589 | |
db2df898 MP |
1590 | if ((size_t) n >= sizeof(buf)) { |
1591 | log_warning("Received notify message exceeded maximum size. Ignoring."); | |
1592 | return 0; | |
1593 | } | |
663996b3 | 1594 | |
db2df898 | 1595 | buf[n] = 0; |
663996b3 | 1596 | |
db2df898 MP |
1597 | /* Notify every unit that might be interested, but try |
1598 | * to avoid notifying the same one multiple times. */ | |
1599 | u1 = manager_get_unit_by_pid_cgroup(m, ucred->pid); | |
1600 | if (u1) { | |
1601 | manager_invoke_notify_message(m, u1, ucred->pid, buf, n, fds); | |
1602 | found = true; | |
1603 | } | |
60f067b4 | 1604 | |
db2df898 MP |
1605 | u2 = hashmap_get(m->watch_pids1, PID_TO_PTR(ucred->pid)); |
1606 | if (u2 && u2 != u1) { | |
1607 | manager_invoke_notify_message(m, u2, ucred->pid, buf, n, fds); | |
1608 | found = true; | |
1609 | } | |
663996b3 | 1610 | |
db2df898 MP |
1611 | u3 = hashmap_get(m->watch_pids2, PID_TO_PTR(ucred->pid)); |
1612 | if (u3 && u3 != u2 && u3 != u1) { | |
1613 | manager_invoke_notify_message(m, u3, ucred->pid, buf, n, fds); | |
1614 | found = true; | |
1615 | } | |
60f067b4 | 1616 | |
db2df898 MP |
1617 | if (!found) |
1618 | log_warning("Cannot find unit for notify message of PID "PID_FMT".", ucred->pid); | |
e735f4d4 | 1619 | |
db2df898 MP |
1620 | if (fdset_size(fds) > 0) |
1621 | log_warning("Got auxiliary fds with notification message, closing all."); | |
663996b3 MS |
1622 | |
1623 | return 0; | |
1624 | } | |
1625 | ||
db2df898 | 1626 | static void invoke_sigchld_event(Manager *m, Unit *u, const siginfo_t *si) { |
60f067b4 JS |
1627 | assert(m); |
1628 | assert(u); | |
1629 | assert(si); | |
1630 | ||
e3bff60a | 1631 | log_unit_debug(u, "Child "PID_FMT" belongs to %s", si->si_pid, u->id); |
60f067b4 JS |
1632 | |
1633 | unit_unwatch_pid(u, si->si_pid); | |
1634 | UNIT_VTABLE(u)->sigchld_event(u, si->si_pid, si->si_code, si->si_status); | |
1635 | } | |
1636 | ||
663996b3 MS |
1637 | static int manager_dispatch_sigchld(Manager *m) { |
1638 | assert(m); | |
1639 | ||
1640 | for (;;) { | |
1641 | siginfo_t si = {}; | |
663996b3 MS |
1642 | |
1643 | /* First we call waitd() for a PID and do not reap the | |
1644 | * zombie. That way we can still access /proc/$PID for | |
1645 | * it while it is a zombie. */ | |
1646 | if (waitid(P_ALL, 0, &si, WEXITED|WNOHANG|WNOWAIT) < 0) { | |
1647 | ||
1648 | if (errno == ECHILD) | |
1649 | break; | |
1650 | ||
1651 | if (errno == EINTR) | |
1652 | continue; | |
1653 | ||
1654 | return -errno; | |
1655 | } | |
1656 | ||
1657 | if (si.si_pid <= 0) | |
1658 | break; | |
1659 | ||
1660 | if (si.si_code == CLD_EXITED || si.si_code == CLD_KILLED || si.si_code == CLD_DUMPED) { | |
1661 | _cleanup_free_ char *name = NULL; | |
5eef597e | 1662 | Unit *u1, *u2, *u3; |
663996b3 MS |
1663 | |
1664 | get_process_comm(si.si_pid, &name); | |
663996b3 | 1665 | |
60f067b4 JS |
1666 | log_debug("Child "PID_FMT" (%s) died (code=%s, status=%i/%s)", |
1667 | si.si_pid, strna(name), | |
1668 | sigchld_code_to_string(si.si_code), | |
1669 | si.si_status, | |
1670 | strna(si.si_code == CLD_EXITED | |
1671 | ? exit_status_to_string(si.si_status, EXIT_STATUS_FULL) | |
1672 | : signal_to_string(si.si_status))); | |
663996b3 | 1673 | |
60f067b4 JS |
1674 | /* And now figure out the unit this belongs |
1675 | * to, it might be multiple... */ | |
d9dfd233 | 1676 | u1 = manager_get_unit_by_pid_cgroup(m, si.si_pid); |
5eef597e MP |
1677 | if (u1) |
1678 | invoke_sigchld_event(m, u1, &si); | |
d9dfd233 | 1679 | u2 = hashmap_get(m->watch_pids1, PID_TO_PTR(si.si_pid)); |
5eef597e MP |
1680 | if (u2 && u2 != u1) |
1681 | invoke_sigchld_event(m, u2, &si); | |
d9dfd233 | 1682 | u3 = hashmap_get(m->watch_pids2, PID_TO_PTR(si.si_pid)); |
5eef597e MP |
1683 | if (u3 && u3 != u2 && u3 != u1) |
1684 | invoke_sigchld_event(m, u3, &si); | |
60f067b4 | 1685 | } |
663996b3 MS |
1686 | |
1687 | /* And now, we actually reap the zombie. */ | |
1688 | if (waitid(P_PID, si.si_pid, &si, WEXITED) < 0) { | |
1689 | if (errno == EINTR) | |
1690 | continue; | |
1691 | ||
1692 | return -errno; | |
60f067b4 | 1693 | } |
663996b3 MS |
1694 | } |
1695 | ||
1696 | return 0; | |
1697 | } | |
1698 | ||
1699 | static int manager_start_target(Manager *m, const char *name, JobMode mode) { | |
4c89c718 | 1700 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
663996b3 | 1701 | int r; |
663996b3 | 1702 | |
e3bff60a | 1703 | log_debug("Activating special unit %s", name); |
663996b3 | 1704 | |
db2df898 | 1705 | r = manager_add_job_by_name(m, JOB_START, name, mode, &error, NULL); |
663996b3 | 1706 | if (r < 0) |
e3bff60a | 1707 | log_error("Failed to enqueue %s job: %s", name, bus_error_message(&error, r)); |
663996b3 MS |
1708 | |
1709 | return r; | |
1710 | } | |
1711 | ||
60f067b4 JS |
1712 | static int manager_dispatch_signal_fd(sd_event_source *source, int fd, uint32_t revents, void *userdata) { |
1713 | Manager *m = userdata; | |
663996b3 MS |
1714 | ssize_t n; |
1715 | struct signalfd_siginfo sfsi; | |
1716 | bool sigchld = false; | |
5fd56512 | 1717 | int r; |
663996b3 MS |
1718 | |
1719 | assert(m); | |
60f067b4 JS |
1720 | assert(m->signal_fd == fd); |
1721 | ||
1722 | if (revents != EPOLLIN) { | |
1723 | log_warning("Got unexpected events from signal file descriptor."); | |
1724 | return 0; | |
1725 | } | |
663996b3 MS |
1726 | |
1727 | for (;;) { | |
60f067b4 | 1728 | n = read(m->signal_fd, &sfsi, sizeof(sfsi)); |
663996b3 MS |
1729 | if (n != sizeof(sfsi)) { |
1730 | ||
1731 | if (n >= 0) | |
1732 | return -EIO; | |
1733 | ||
1734 | if (errno == EINTR || errno == EAGAIN) | |
1735 | break; | |
1736 | ||
1737 | return -errno; | |
1738 | } | |
1739 | ||
60f067b4 | 1740 | log_received_signal(sfsi.ssi_signo == SIGCHLD || |
e3bff60a | 1741 | (sfsi.ssi_signo == SIGTERM && m->running_as == MANAGER_USER) |
60f067b4 JS |
1742 | ? LOG_DEBUG : LOG_INFO, |
1743 | &sfsi); | |
663996b3 MS |
1744 | |
1745 | switch (sfsi.ssi_signo) { | |
1746 | ||
1747 | case SIGCHLD: | |
1748 | sigchld = true; | |
1749 | break; | |
1750 | ||
1751 | case SIGTERM: | |
e3bff60a | 1752 | if (m->running_as == MANAGER_SYSTEM) { |
663996b3 MS |
1753 | /* This is for compatibility with the |
1754 | * original sysvinit */ | |
1755 | m->exit_code = MANAGER_REEXECUTE; | |
1756 | break; | |
1757 | } | |
1758 | ||
1759 | /* Fall through */ | |
1760 | ||
1761 | case SIGINT: | |
e3bff60a | 1762 | if (m->running_as == MANAGER_SYSTEM) { |
e735f4d4 MP |
1763 | |
1764 | /* If the user presses C-A-D more than | |
1765 | * 7 times within 2s, we reboot | |
1766 | * immediately. */ | |
1767 | ||
1768 | if (ratelimit_test(&m->ctrl_alt_del_ratelimit)) | |
1769 | manager_start_target(m, SPECIAL_CTRL_ALT_DEL_TARGET, JOB_REPLACE_IRREVERSIBLY); | |
1770 | else { | |
1771 | log_notice("Ctrl-Alt-Del was pressed more than 7 times within 2s, rebooting immediately."); | |
1772 | status_printf(NULL, true, false, "Ctrl-Alt-Del was pressed more than 7 times within 2s, rebooting immediately."); | |
1773 | m->exit_code = MANAGER_REBOOT; | |
1774 | } | |
1775 | ||
663996b3 MS |
1776 | break; |
1777 | } | |
1778 | ||
1779 | /* Run the exit target if there is one, if not, just exit. */ | |
1780 | if (manager_start_target(m, SPECIAL_EXIT_TARGET, JOB_REPLACE) < 0) { | |
1781 | m->exit_code = MANAGER_EXIT; | |
1782 | return 0; | |
1783 | } | |
1784 | ||
1785 | break; | |
1786 | ||
1787 | case SIGWINCH: | |
e3bff60a | 1788 | if (m->running_as == MANAGER_SYSTEM) |
663996b3 MS |
1789 | manager_start_target(m, SPECIAL_KBREQUEST_TARGET, JOB_REPLACE); |
1790 | ||
1791 | /* This is a nop on non-init */ | |
1792 | break; | |
1793 | ||
1794 | case SIGPWR: | |
e3bff60a | 1795 | if (m->running_as == MANAGER_SYSTEM) |
663996b3 MS |
1796 | manager_start_target(m, SPECIAL_SIGPWR_TARGET, JOB_REPLACE); |
1797 | ||
1798 | /* This is a nop on non-init */ | |
1799 | break; | |
1800 | ||
1801 | case SIGUSR1: { | |
1802 | Unit *u; | |
1803 | ||
1804 | u = manager_get_unit(m, SPECIAL_DBUS_SERVICE); | |
1805 | ||
1806 | if (!u || UNIT_IS_ACTIVE_OR_RELOADING(unit_active_state(u))) { | |
1807 | log_info("Trying to reconnect to bus..."); | |
1808 | bus_init(m, true); | |
1809 | } | |
1810 | ||
1811 | if (!u || !UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(u))) { | |
1812 | log_info("Loading D-Bus service..."); | |
1813 | manager_start_target(m, SPECIAL_DBUS_SERVICE, JOB_REPLACE); | |
1814 | } | |
1815 | ||
1816 | break; | |
1817 | } | |
1818 | ||
1819 | case SIGUSR2: { | |
60f067b4 JS |
1820 | _cleanup_free_ char *dump = NULL; |
1821 | _cleanup_fclose_ FILE *f = NULL; | |
663996b3 MS |
1822 | size_t size; |
1823 | ||
60f067b4 JS |
1824 | f = open_memstream(&dump, &size); |
1825 | if (!f) { | |
5fd56512 | 1826 | log_warning_errno(errno, "Failed to allocate memory stream: %m"); |
663996b3 MS |
1827 | break; |
1828 | } | |
1829 | ||
1830 | manager_dump_units(m, f, "\t"); | |
1831 | manager_dump_jobs(m, f, "\t"); | |
1832 | ||
5fd56512 MP |
1833 | r = fflush_and_check(f); |
1834 | if (r < 0) { | |
1835 | log_warning_errno(r, "Failed to write status stream: %m"); | |
60f067b4 JS |
1836 | break; |
1837 | } | |
663996b3 | 1838 | |
60f067b4 | 1839 | log_dump(LOG_INFO, dump); |
663996b3 MS |
1840 | break; |
1841 | } | |
1842 | ||
1843 | case SIGHUP: | |
1844 | m->exit_code = MANAGER_RELOAD; | |
1845 | break; | |
1846 | ||
1847 | default: { | |
1848 | ||
1849 | /* Starting SIGRTMIN+0 */ | |
1850 | static const char * const target_table[] = { | |
1851 | [0] = SPECIAL_DEFAULT_TARGET, | |
1852 | [1] = SPECIAL_RESCUE_TARGET, | |
1853 | [2] = SPECIAL_EMERGENCY_TARGET, | |
1854 | [3] = SPECIAL_HALT_TARGET, | |
1855 | [4] = SPECIAL_POWEROFF_TARGET, | |
1856 | [5] = SPECIAL_REBOOT_TARGET, | |
1857 | [6] = SPECIAL_KEXEC_TARGET | |
1858 | }; | |
1859 | ||
1860 | /* Starting SIGRTMIN+13, so that target halt and system halt are 10 apart */ | |
1861 | static const ManagerExitCode code_table[] = { | |
1862 | [0] = MANAGER_HALT, | |
1863 | [1] = MANAGER_POWEROFF, | |
1864 | [2] = MANAGER_REBOOT, | |
1865 | [3] = MANAGER_KEXEC | |
1866 | }; | |
1867 | ||
1868 | if ((int) sfsi.ssi_signo >= SIGRTMIN+0 && | |
1869 | (int) sfsi.ssi_signo < SIGRTMIN+(int) ELEMENTSOF(target_table)) { | |
1870 | int idx = (int) sfsi.ssi_signo - SIGRTMIN; | |
1871 | manager_start_target(m, target_table[idx], | |
1872 | (idx == 1 || idx == 2) ? JOB_ISOLATE : JOB_REPLACE); | |
1873 | break; | |
1874 | } | |
1875 | ||
1876 | if ((int) sfsi.ssi_signo >= SIGRTMIN+13 && | |
1877 | (int) sfsi.ssi_signo < SIGRTMIN+13+(int) ELEMENTSOF(code_table)) { | |
1878 | m->exit_code = code_table[sfsi.ssi_signo - SIGRTMIN - 13]; | |
1879 | break; | |
1880 | } | |
1881 | ||
1882 | switch (sfsi.ssi_signo - SIGRTMIN) { | |
1883 | ||
1884 | case 20: | |
60f067b4 | 1885 | manager_set_show_status(m, SHOW_STATUS_YES); |
663996b3 MS |
1886 | break; |
1887 | ||
1888 | case 21: | |
60f067b4 | 1889 | manager_set_show_status(m, SHOW_STATUS_NO); |
663996b3 MS |
1890 | break; |
1891 | ||
1892 | case 22: | |
1893 | log_set_max_level(LOG_DEBUG); | |
4c89c718 | 1894 | log_info("Setting log level to debug."); |
663996b3 MS |
1895 | break; |
1896 | ||
1897 | case 23: | |
1898 | log_set_max_level(LOG_INFO); | |
4c89c718 | 1899 | log_info("Setting log level to info."); |
663996b3 MS |
1900 | break; |
1901 | ||
1902 | case 24: | |
e3bff60a | 1903 | if (m->running_as == MANAGER_USER) { |
663996b3 MS |
1904 | m->exit_code = MANAGER_EXIT; |
1905 | return 0; | |
1906 | } | |
1907 | ||
1908 | /* This is a nop on init */ | |
1909 | break; | |
1910 | ||
1911 | case 26: | |
5eef597e | 1912 | case 29: /* compatibility: used to be mapped to LOG_TARGET_SYSLOG_OR_KMSG */ |
663996b3 MS |
1913 | log_set_target(LOG_TARGET_JOURNAL_OR_KMSG); |
1914 | log_notice("Setting log target to journal-or-kmsg."); | |
1915 | break; | |
1916 | ||
1917 | case 27: | |
1918 | log_set_target(LOG_TARGET_CONSOLE); | |
1919 | log_notice("Setting log target to console."); | |
1920 | break; | |
1921 | ||
1922 | case 28: | |
1923 | log_set_target(LOG_TARGET_KMSG); | |
1924 | log_notice("Setting log target to kmsg."); | |
1925 | break; | |
1926 | ||
663996b3 MS |
1927 | default: |
1928 | log_warning("Got unhandled signal <%s>.", signal_to_string(sfsi.ssi_signo)); | |
1929 | } | |
1930 | } | |
1931 | } | |
1932 | } | |
1933 | ||
1934 | if (sigchld) | |
60f067b4 | 1935 | manager_dispatch_sigchld(m); |
663996b3 MS |
1936 | |
1937 | return 0; | |
1938 | } | |
1939 | ||
60f067b4 JS |
1940 | static int manager_dispatch_time_change_fd(sd_event_source *source, int fd, uint32_t revents, void *userdata) { |
1941 | Manager *m = userdata; | |
1942 | Iterator i; | |
1943 | Unit *u; | |
663996b3 MS |
1944 | |
1945 | assert(m); | |
60f067b4 | 1946 | assert(m->time_change_fd == fd); |
663996b3 | 1947 | |
60f067b4 | 1948 | log_struct(LOG_INFO, |
f47781d8 MP |
1949 | LOG_MESSAGE_ID(SD_MESSAGE_TIME_CHANGE), |
1950 | LOG_MESSAGE("Time has been changed"), | |
60f067b4 | 1951 | NULL); |
663996b3 | 1952 | |
60f067b4 JS |
1953 | /* Restart the watch */ |
1954 | m->time_change_event_source = sd_event_source_unref(m->time_change_event_source); | |
1955 | m->time_change_fd = safe_close(m->time_change_fd); | |
663996b3 | 1956 | |
60f067b4 | 1957 | manager_setup_time_change(m); |
663996b3 | 1958 | |
60f067b4 JS |
1959 | HASHMAP_FOREACH(u, m->units, i) |
1960 | if (UNIT_VTABLE(u)->time_change) | |
1961 | UNIT_VTABLE(u)->time_change(u); | |
663996b3 | 1962 | |
60f067b4 JS |
1963 | return 0; |
1964 | } | |
663996b3 | 1965 | |
60f067b4 JS |
1966 | static int manager_dispatch_idle_pipe_fd(sd_event_source *source, int fd, uint32_t revents, void *userdata) { |
1967 | Manager *m = userdata; | |
663996b3 | 1968 | |
60f067b4 JS |
1969 | assert(m); |
1970 | assert(m->idle_pipe[2] == fd); | |
663996b3 | 1971 | |
60f067b4 | 1972 | m->no_console_output = m->n_on_console > 0; |
663996b3 | 1973 | |
60f067b4 | 1974 | manager_close_idle_pipe(m); |
663996b3 | 1975 | |
60f067b4 JS |
1976 | return 0; |
1977 | } | |
663996b3 | 1978 | |
60f067b4 JS |
1979 | static int manager_dispatch_jobs_in_progress(sd_event_source *source, usec_t usec, void *userdata) { |
1980 | Manager *m = userdata; | |
1981 | int r; | |
1982 | uint64_t next; | |
663996b3 | 1983 | |
60f067b4 JS |
1984 | assert(m); |
1985 | assert(source); | |
14228c0d | 1986 | |
60f067b4 | 1987 | manager_print_jobs_in_progress(m); |
14228c0d | 1988 | |
60f067b4 JS |
1989 | next = now(CLOCK_MONOTONIC) + JOBS_IN_PROGRESS_PERIOD_USEC; |
1990 | r = sd_event_source_set_time(source, next); | |
1991 | if (r < 0) | |
1992 | return r; | |
663996b3 | 1993 | |
60f067b4 | 1994 | return sd_event_source_set_enabled(source, SD_EVENT_ONESHOT); |
663996b3 MS |
1995 | } |
1996 | ||
1997 | int manager_loop(Manager *m) { | |
1998 | int r; | |
1999 | ||
2000 | RATELIMIT_DEFINE(rl, 1*USEC_PER_SEC, 50000); | |
2001 | ||
2002 | assert(m); | |
60f067b4 | 2003 | m->exit_code = MANAGER_OK; |
663996b3 MS |
2004 | |
2005 | /* Release the path cache */ | |
db2df898 | 2006 | m->unit_path_cache = set_free_free(m->unit_path_cache); |
663996b3 MS |
2007 | |
2008 | manager_check_finished(m); | |
2009 | ||
2010 | /* There might still be some zombies hanging around from | |
60f067b4 | 2011 | * before we were exec()'ed. Let's reap them. */ |
663996b3 MS |
2012 | r = manager_dispatch_sigchld(m); |
2013 | if (r < 0) | |
2014 | return r; | |
2015 | ||
60f067b4 JS |
2016 | while (m->exit_code == MANAGER_OK) { |
2017 | usec_t wait_usec; | |
663996b3 | 2018 | |
e3bff60a | 2019 | if (m->runtime_watchdog > 0 && m->running_as == MANAGER_SYSTEM) |
663996b3 MS |
2020 | watchdog_ping(); |
2021 | ||
2022 | if (!ratelimit_test(&rl)) { | |
2023 | /* Yay, something is going seriously wrong, pause a little */ | |
2024 | log_warning("Looping too fast. Throttling execution a little."); | |
2025 | sleep(1); | |
663996b3 MS |
2026 | } |
2027 | ||
2028 | if (manager_dispatch_load_queue(m) > 0) | |
2029 | continue; | |
2030 | ||
14228c0d | 2031 | if (manager_dispatch_gc_queue(m) > 0) |
663996b3 MS |
2032 | continue; |
2033 | ||
14228c0d | 2034 | if (manager_dispatch_cleanup_queue(m) > 0) |
663996b3 MS |
2035 | continue; |
2036 | ||
14228c0d | 2037 | if (manager_dispatch_cgroup_queue(m) > 0) |
663996b3 MS |
2038 | continue; |
2039 | ||
663996b3 MS |
2040 | if (manager_dispatch_dbus_queue(m) > 0) |
2041 | continue; | |
2042 | ||
663996b3 | 2043 | /* Sleep for half the watchdog time */ |
e3bff60a | 2044 | if (m->runtime_watchdog > 0 && m->running_as == MANAGER_SYSTEM) { |
60f067b4 JS |
2045 | wait_usec = m->runtime_watchdog / 2; |
2046 | if (wait_usec <= 0) | |
2047 | wait_usec = 1; | |
663996b3 | 2048 | } else |
5eef597e | 2049 | wait_usec = USEC_INFINITY; |
663996b3 | 2050 | |
60f067b4 | 2051 | r = sd_event_run(m->event, wait_usec); |
f47781d8 MP |
2052 | if (r < 0) |
2053 | return log_error_errno(r, "Failed to run event loop: %m"); | |
663996b3 MS |
2054 | } |
2055 | ||
2056 | return m->exit_code; | |
2057 | } | |
2058 | ||
60f067b4 | 2059 | int manager_load_unit_from_dbus_path(Manager *m, const char *s, sd_bus_error *e, Unit **_u) { |
14228c0d | 2060 | _cleanup_free_ char *n = NULL; |
663996b3 MS |
2061 | Unit *u; |
2062 | int r; | |
2063 | ||
2064 | assert(m); | |
2065 | assert(s); | |
2066 | assert(_u); | |
2067 | ||
14228c0d MB |
2068 | r = unit_name_from_dbus_path(s, &n); |
2069 | if (r < 0) | |
2070 | return r; | |
663996b3 MS |
2071 | |
2072 | r = manager_load_unit(m, n, NULL, e, &u); | |
663996b3 MS |
2073 | if (r < 0) |
2074 | return r; | |
2075 | ||
2076 | *_u = u; | |
2077 | ||
2078 | return 0; | |
2079 | } | |
2080 | ||
2081 | int manager_get_job_from_dbus_path(Manager *m, const char *s, Job **_j) { | |
60f067b4 | 2082 | const char *p; |
663996b3 | 2083 | unsigned id; |
60f067b4 | 2084 | Job *j; |
663996b3 MS |
2085 | int r; |
2086 | ||
2087 | assert(m); | |
2088 | assert(s); | |
2089 | assert(_j); | |
2090 | ||
60f067b4 JS |
2091 | p = startswith(s, "/org/freedesktop/systemd1/job/"); |
2092 | if (!p) | |
663996b3 MS |
2093 | return -EINVAL; |
2094 | ||
60f067b4 | 2095 | r = safe_atou(p, &id); |
663996b3 MS |
2096 | if (r < 0) |
2097 | return r; | |
2098 | ||
2099 | j = manager_get_job(m, id); | |
2100 | if (!j) | |
2101 | return -ENOENT; | |
2102 | ||
2103 | *_j = j; | |
2104 | ||
2105 | return 0; | |
2106 | } | |
2107 | ||
2108 | void manager_send_unit_audit(Manager *m, Unit *u, int type, bool success) { | |
2109 | ||
2110 | #ifdef HAVE_AUDIT | |
60f067b4 | 2111 | _cleanup_free_ char *p = NULL; |
f47781d8 | 2112 | const char *msg; |
e3bff60a | 2113 | int audit_fd, r; |
663996b3 | 2114 | |
db2df898 MP |
2115 | if (m->running_as != MANAGER_SYSTEM) |
2116 | return; | |
2117 | ||
663996b3 MS |
2118 | audit_fd = get_audit_fd(); |
2119 | if (audit_fd < 0) | |
2120 | return; | |
2121 | ||
2122 | /* Don't generate audit events if the service was already | |
2123 | * started and we're just deserializing */ | |
2124 | if (m->n_reloading > 0) | |
2125 | return; | |
2126 | ||
663996b3 MS |
2127 | if (u->type != UNIT_SERVICE) |
2128 | return; | |
2129 | ||
e3bff60a MP |
2130 | r = unit_name_to_prefix_and_instance(u->id, &p); |
2131 | if (r < 0) { | |
2132 | log_error_errno(r, "Failed to extract prefix and instance of unit name: %m"); | |
663996b3 MS |
2133 | return; |
2134 | } | |
2135 | ||
e735f4d4 | 2136 | msg = strjoina("unit=", p); |
f47781d8 MP |
2137 | if (audit_log_user_comm_message(audit_fd, type, msg, "systemd", NULL, NULL, NULL, success) < 0) { |
2138 | if (errno == EPERM) | |
663996b3 MS |
2139 | /* We aren't allowed to send audit messages? |
2140 | * Then let's not retry again. */ | |
2141 | close_audit_fd(); | |
f47781d8 MP |
2142 | else |
2143 | log_warning_errno(errno, "Failed to send audit message: %m"); | |
663996b3 | 2144 | } |
663996b3 MS |
2145 | #endif |
2146 | ||
2147 | } | |
2148 | ||
2149 | void manager_send_unit_plymouth(Manager *m, Unit *u) { | |
5eef597e | 2150 | union sockaddr_union sa = PLYMOUTH_SOCKET; |
60f067b4 | 2151 | |
663996b3 | 2152 | int n = 0; |
60f067b4 JS |
2153 | _cleanup_free_ char *message = NULL; |
2154 | _cleanup_close_ int fd = -1; | |
663996b3 MS |
2155 | |
2156 | /* Don't generate plymouth events if the service was already | |
2157 | * started and we're just deserializing */ | |
2158 | if (m->n_reloading > 0) | |
2159 | return; | |
2160 | ||
e3bff60a | 2161 | if (m->running_as != MANAGER_SYSTEM) |
663996b3 MS |
2162 | return; |
2163 | ||
6300502b | 2164 | if (detect_container() > 0) |
60f067b4 JS |
2165 | return; |
2166 | ||
663996b3 MS |
2167 | if (u->type != UNIT_SERVICE && |
2168 | u->type != UNIT_MOUNT && | |
2169 | u->type != UNIT_SWAP) | |
2170 | return; | |
2171 | ||
2172 | /* We set SOCK_NONBLOCK here so that we rather drop the | |
2173 | * message then wait for plymouth */ | |
2174 | fd = socket(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0); | |
2175 | if (fd < 0) { | |
f47781d8 | 2176 | log_error_errno(errno, "socket() failed: %m"); |
663996b3 MS |
2177 | return; |
2178 | } | |
2179 | ||
663996b3 MS |
2180 | if (connect(fd, &sa.sa, offsetof(struct sockaddr_un, sun_path) + 1 + strlen(sa.un.sun_path+1)) < 0) { |
2181 | ||
60f067b4 | 2182 | if (!IN_SET(errno, EPIPE, EAGAIN, ENOENT, ECONNREFUSED, ECONNRESET, ECONNABORTED)) |
f47781d8 | 2183 | log_error_errno(errno, "connect() failed: %m"); |
60f067b4 | 2184 | return; |
663996b3 MS |
2185 | } |
2186 | ||
2187 | if (asprintf(&message, "U\002%c%s%n", (int) (strlen(u->id) + 1), u->id, &n) < 0) { | |
2188 | log_oom(); | |
60f067b4 | 2189 | return; |
663996b3 MS |
2190 | } |
2191 | ||
2192 | errno = 0; | |
60f067b4 JS |
2193 | if (write(fd, message, n + 1) != n + 1) |
2194 | if (!IN_SET(errno, EPIPE, EAGAIN, ENOENT, ECONNREFUSED, ECONNRESET, ECONNABORTED)) | |
f47781d8 | 2195 | log_error_errno(errno, "Failed to write Plymouth message: %m"); |
663996b3 MS |
2196 | } |
2197 | ||
663996b3 | 2198 | int manager_open_serialization(Manager *m, FILE **_f) { |
60f067b4 JS |
2199 | const char *path; |
2200 | int fd = -1; | |
663996b3 MS |
2201 | FILE *f; |
2202 | ||
2203 | assert(_f); | |
2204 | ||
e3bff60a | 2205 | path = m->running_as == MANAGER_SYSTEM ? "/run/systemd" : "/tmp"; |
60f067b4 JS |
2206 | fd = open_tmpfile(path, O_RDWR|O_CLOEXEC); |
2207 | if (fd < 0) | |
663996b3 | 2208 | return -errno; |
663996b3 MS |
2209 | |
2210 | log_debug("Serializing state to %s", path); | |
663996b3 MS |
2211 | |
2212 | f = fdopen(fd, "w+"); | |
60f067b4 JS |
2213 | if (!f) { |
2214 | safe_close(fd); | |
663996b3 | 2215 | return -errno; |
60f067b4 | 2216 | } |
663996b3 MS |
2217 | |
2218 | *_f = f; | |
2219 | ||
2220 | return 0; | |
2221 | } | |
2222 | ||
2223 | int manager_serialize(Manager *m, FILE *f, FDSet *fds, bool switching_root) { | |
2224 | Iterator i; | |
2225 | Unit *u; | |
2226 | const char *t; | |
2227 | char **e; | |
2228 | int r; | |
2229 | ||
2230 | assert(m); | |
2231 | assert(f); | |
2232 | assert(fds); | |
2233 | ||
2234 | m->n_reloading ++; | |
2235 | ||
e735f4d4 | 2236 | fprintf(f, "current-job-id=%"PRIu32"\n", m->current_job_id); |
663996b3 MS |
2237 | fprintf(f, "taint-usr=%s\n", yes_no(m->taint_usr)); |
2238 | fprintf(f, "n-installed-jobs=%u\n", m->n_installed_jobs); | |
2239 | fprintf(f, "n-failed-jobs=%u\n", m->n_failed_jobs); | |
2240 | ||
2241 | dual_timestamp_serialize(f, "firmware-timestamp", &m->firmware_timestamp); | |
663996b3 | 2242 | dual_timestamp_serialize(f, "loader-timestamp", &m->loader_timestamp); |
60f067b4 | 2243 | dual_timestamp_serialize(f, "kernel-timestamp", &m->kernel_timestamp); |
663996b3 MS |
2244 | dual_timestamp_serialize(f, "initrd-timestamp", &m->initrd_timestamp); |
2245 | ||
2246 | if (!in_initrd()) { | |
2247 | dual_timestamp_serialize(f, "userspace-timestamp", &m->userspace_timestamp); | |
2248 | dual_timestamp_serialize(f, "finish-timestamp", &m->finish_timestamp); | |
60f067b4 JS |
2249 | dual_timestamp_serialize(f, "security-start-timestamp", &m->security_start_timestamp); |
2250 | dual_timestamp_serialize(f, "security-finish-timestamp", &m->security_finish_timestamp); | |
2251 | dual_timestamp_serialize(f, "generators-start-timestamp", &m->generators_start_timestamp); | |
2252 | dual_timestamp_serialize(f, "generators-finish-timestamp", &m->generators_finish_timestamp); | |
2253 | dual_timestamp_serialize(f, "units-load-start-timestamp", &m->units_load_start_timestamp); | |
2254 | dual_timestamp_serialize(f, "units-load-finish-timestamp", &m->units_load_finish_timestamp); | |
663996b3 MS |
2255 | } |
2256 | ||
2257 | if (!switching_root) { | |
2258 | STRV_FOREACH(e, m->environment) { | |
2259 | _cleanup_free_ char *ce; | |
2260 | ||
2261 | ce = cescape(*e); | |
60f067b4 JS |
2262 | if (!ce) |
2263 | return -ENOMEM; | |
2264 | ||
2265 | fprintf(f, "env=%s\n", *e); | |
663996b3 MS |
2266 | } |
2267 | } | |
2268 | ||
60f067b4 JS |
2269 | if (m->notify_fd >= 0) { |
2270 | int copy; | |
2271 | ||
2272 | copy = fdset_put_dup(fds, m->notify_fd); | |
2273 | if (copy < 0) | |
2274 | return copy; | |
2275 | ||
2276 | fprintf(f, "notify-fd=%i\n", copy); | |
2277 | fprintf(f, "notify-socket=%s\n", m->notify_socket); | |
2278 | } | |
2279 | ||
2280 | if (m->kdbus_fd >= 0) { | |
2281 | int copy; | |
2282 | ||
2283 | copy = fdset_put_dup(fds, m->kdbus_fd); | |
2284 | if (copy < 0) | |
2285 | return copy; | |
2286 | ||
2287 | fprintf(f, "kdbus-fd=%i\n", copy); | |
2288 | } | |
2289 | ||
2290 | bus_track_serialize(m->subscribed, f); | |
14228c0d | 2291 | |
663996b3 MS |
2292 | fputc('\n', f); |
2293 | ||
2294 | HASHMAP_FOREACH_KEY(u, t, m->units, i) { | |
2295 | if (u->id != t) | |
2296 | continue; | |
2297 | ||
663996b3 MS |
2298 | /* Start marker */ |
2299 | fputs(u->id, f); | |
2300 | fputc('\n', f); | |
2301 | ||
14228c0d MB |
2302 | r = unit_serialize(u, f, fds, !switching_root); |
2303 | if (r < 0) { | |
663996b3 MS |
2304 | m->n_reloading --; |
2305 | return r; | |
2306 | } | |
2307 | } | |
2308 | ||
2309 | assert(m->n_reloading > 0); | |
2310 | m->n_reloading --; | |
2311 | ||
2312 | if (ferror(f)) | |
2313 | return -EIO; | |
2314 | ||
2315 | r = bus_fdset_add_all(m, fds); | |
2316 | if (r < 0) | |
2317 | return r; | |
2318 | ||
2319 | return 0; | |
2320 | } | |
2321 | ||
2322 | int manager_deserialize(Manager *m, FILE *f, FDSet *fds) { | |
2323 | int r = 0; | |
2324 | ||
2325 | assert(m); | |
2326 | assert(f); | |
2327 | ||
2328 | log_debug("Deserializing state..."); | |
2329 | ||
2330 | m->n_reloading ++; | |
2331 | ||
2332 | for (;;) { | |
2333 | char line[LINE_MAX], *l; | |
2334 | ||
2335 | if (!fgets(line, sizeof(line), f)) { | |
2336 | if (feof(f)) | |
2337 | r = 0; | |
2338 | else | |
2339 | r = -errno; | |
2340 | ||
2341 | goto finish; | |
2342 | } | |
2343 | ||
2344 | char_array_0(line); | |
2345 | l = strstrip(line); | |
2346 | ||
2347 | if (l[0] == 0) | |
2348 | break; | |
2349 | ||
2350 | if (startswith(l, "current-job-id=")) { | |
2351 | uint32_t id; | |
2352 | ||
2353 | if (safe_atou32(l+15, &id) < 0) | |
f47781d8 | 2354 | log_debug("Failed to parse current job id value %s", l+15); |
663996b3 MS |
2355 | else |
2356 | m->current_job_id = MAX(m->current_job_id, id); | |
60f067b4 | 2357 | |
663996b3 MS |
2358 | } else if (startswith(l, "n-installed-jobs=")) { |
2359 | uint32_t n; | |
2360 | ||
2361 | if (safe_atou32(l+17, &n) < 0) | |
f47781d8 | 2362 | log_debug("Failed to parse installed jobs counter %s", l+17); |
663996b3 MS |
2363 | else |
2364 | m->n_installed_jobs += n; | |
60f067b4 | 2365 | |
663996b3 MS |
2366 | } else if (startswith(l, "n-failed-jobs=")) { |
2367 | uint32_t n; | |
2368 | ||
2369 | if (safe_atou32(l+14, &n) < 0) | |
f47781d8 | 2370 | log_debug("Failed to parse failed jobs counter %s", l+14); |
663996b3 MS |
2371 | else |
2372 | m->n_failed_jobs += n; | |
60f067b4 | 2373 | |
663996b3 MS |
2374 | } else if (startswith(l, "taint-usr=")) { |
2375 | int b; | |
2376 | ||
60f067b4 JS |
2377 | b = parse_boolean(l+10); |
2378 | if (b < 0) | |
f47781d8 | 2379 | log_debug("Failed to parse taint /usr flag %s", l+10); |
663996b3 MS |
2380 | else |
2381 | m->taint_usr = m->taint_usr || b; | |
60f067b4 | 2382 | |
663996b3 MS |
2383 | } else if (startswith(l, "firmware-timestamp=")) |
2384 | dual_timestamp_deserialize(l+19, &m->firmware_timestamp); | |
2385 | else if (startswith(l, "loader-timestamp=")) | |
2386 | dual_timestamp_deserialize(l+17, &m->loader_timestamp); | |
2387 | else if (startswith(l, "kernel-timestamp=")) | |
2388 | dual_timestamp_deserialize(l+17, &m->kernel_timestamp); | |
2389 | else if (startswith(l, "initrd-timestamp=")) | |
2390 | dual_timestamp_deserialize(l+17, &m->initrd_timestamp); | |
2391 | else if (startswith(l, "userspace-timestamp=")) | |
2392 | dual_timestamp_deserialize(l+20, &m->userspace_timestamp); | |
2393 | else if (startswith(l, "finish-timestamp=")) | |
2394 | dual_timestamp_deserialize(l+17, &m->finish_timestamp); | |
60f067b4 JS |
2395 | else if (startswith(l, "security-start-timestamp=")) |
2396 | dual_timestamp_deserialize(l+25, &m->security_start_timestamp); | |
2397 | else if (startswith(l, "security-finish-timestamp=")) | |
2398 | dual_timestamp_deserialize(l+26, &m->security_finish_timestamp); | |
2399 | else if (startswith(l, "generators-start-timestamp=")) | |
2400 | dual_timestamp_deserialize(l+27, &m->generators_start_timestamp); | |
2401 | else if (startswith(l, "generators-finish-timestamp=")) | |
2402 | dual_timestamp_deserialize(l+28, &m->generators_finish_timestamp); | |
2403 | else if (startswith(l, "units-load-start-timestamp=")) | |
2404 | dual_timestamp_deserialize(l+27, &m->units_load_start_timestamp); | |
2405 | else if (startswith(l, "units-load-finish-timestamp=")) | |
2406 | dual_timestamp_deserialize(l+28, &m->units_load_finish_timestamp); | |
663996b3 MS |
2407 | else if (startswith(l, "env=")) { |
2408 | _cleanup_free_ char *uce = NULL; | |
2409 | char **e; | |
2410 | ||
e3bff60a MP |
2411 | r = cunescape(l + 4, UNESCAPE_RELAX, &uce); |
2412 | if (r < 0) | |
663996b3 | 2413 | goto finish; |
663996b3 MS |
2414 | |
2415 | e = strv_env_set(m->environment, uce); | |
2416 | if (!e) { | |
2417 | r = -ENOMEM; | |
2418 | goto finish; | |
2419 | } | |
2420 | ||
2421 | strv_free(m->environment); | |
2422 | m->environment = e; | |
60f067b4 JS |
2423 | |
2424 | } else if (startswith(l, "notify-fd=")) { | |
2425 | int fd; | |
2426 | ||
2427 | if (safe_atoi(l + 10, &fd) < 0 || fd < 0 || !fdset_contains(fds, fd)) | |
f47781d8 | 2428 | log_debug("Failed to parse notify fd: %s", l + 10); |
60f067b4 JS |
2429 | else { |
2430 | m->notify_event_source = sd_event_source_unref(m->notify_event_source); | |
2431 | safe_close(m->notify_fd); | |
2432 | m->notify_fd = fdset_remove(fds, fd); | |
2433 | } | |
2434 | ||
2435 | } else if (startswith(l, "notify-socket=")) { | |
2436 | char *n; | |
2437 | ||
2438 | n = strdup(l+14); | |
2439 | if (!n) { | |
2440 | r = -ENOMEM; | |
2441 | goto finish; | |
2442 | } | |
2443 | ||
2444 | free(m->notify_socket); | |
2445 | m->notify_socket = n; | |
2446 | ||
2447 | } else if (startswith(l, "kdbus-fd=")) { | |
2448 | int fd; | |
2449 | ||
2450 | if (safe_atoi(l + 9, &fd) < 0 || fd < 0 || !fdset_contains(fds, fd)) | |
f47781d8 | 2451 | log_debug("Failed to parse kdbus fd: %s", l + 9); |
60f067b4 JS |
2452 | else { |
2453 | safe_close(m->kdbus_fd); | |
2454 | m->kdbus_fd = fdset_remove(fds, fd); | |
2455 | } | |
2456 | ||
f47781d8 MP |
2457 | } else { |
2458 | int k; | |
2459 | ||
2460 | k = bus_track_deserialize_item(&m->deserialized_subscribed, l); | |
2461 | if (k < 0) | |
2462 | log_debug_errno(k, "Failed to deserialize bus tracker object: %m"); | |
2463 | else if (k == 0) | |
2464 | log_debug("Unknown serialization item '%s'", l); | |
2465 | } | |
663996b3 MS |
2466 | } |
2467 | ||
2468 | for (;;) { | |
2469 | Unit *u; | |
2470 | char name[UNIT_NAME_MAX+2]; | |
2471 | ||
2472 | /* Start marker */ | |
2473 | if (!fgets(name, sizeof(name), f)) { | |
2474 | if (feof(f)) | |
2475 | r = 0; | |
2476 | else | |
2477 | r = -errno; | |
2478 | ||
2479 | goto finish; | |
2480 | } | |
2481 | ||
2482 | char_array_0(name); | |
2483 | ||
2484 | r = manager_load_unit(m, strstrip(name), NULL, NULL, &u); | |
2485 | if (r < 0) | |
2486 | goto finish; | |
2487 | ||
2488 | r = unit_deserialize(u, f, fds); | |
2489 | if (r < 0) | |
2490 | goto finish; | |
2491 | } | |
2492 | ||
2493 | finish: | |
60f067b4 | 2494 | if (ferror(f)) |
663996b3 | 2495 | r = -EIO; |
663996b3 MS |
2496 | |
2497 | assert(m->n_reloading > 0); | |
2498 | m->n_reloading --; | |
2499 | ||
2500 | return r; | |
2501 | } | |
2502 | ||
663996b3 MS |
2503 | int manager_reload(Manager *m) { |
2504 | int r, q; | |
60f067b4 JS |
2505 | _cleanup_fclose_ FILE *f = NULL; |
2506 | _cleanup_fdset_free_ FDSet *fds = NULL; | |
663996b3 MS |
2507 | |
2508 | assert(m); | |
2509 | ||
2510 | r = manager_open_serialization(m, &f); | |
2511 | if (r < 0) | |
2512 | return r; | |
2513 | ||
2514 | m->n_reloading ++; | |
60f067b4 | 2515 | bus_manager_send_reloading(m, true); |
663996b3 MS |
2516 | |
2517 | fds = fdset_new(); | |
2518 | if (!fds) { | |
2519 | m->n_reloading --; | |
60f067b4 | 2520 | return -ENOMEM; |
663996b3 MS |
2521 | } |
2522 | ||
2523 | r = manager_serialize(m, f, fds, false); | |
2524 | if (r < 0) { | |
2525 | m->n_reloading --; | |
60f067b4 | 2526 | return r; |
663996b3 MS |
2527 | } |
2528 | ||
2529 | if (fseeko(f, 0, SEEK_SET) < 0) { | |
2530 | m->n_reloading --; | |
60f067b4 | 2531 | return -errno; |
663996b3 MS |
2532 | } |
2533 | ||
2534 | /* From here on there is no way back. */ | |
2535 | manager_clear_jobs_and_units(m); | |
2536 | manager_undo_generators(m); | |
2537 | lookup_paths_free(&m->lookup_paths); | |
2538 | ||
2539 | /* Find new unit paths */ | |
e735f4d4 MP |
2540 | q = manager_run_generators(m); |
2541 | if (q < 0 && r >= 0) | |
2542 | r = q; | |
663996b3 MS |
2543 | |
2544 | q = lookup_paths_init( | |
2545 | &m->lookup_paths, m->running_as, true, | |
60f067b4 | 2546 | NULL, |
663996b3 MS |
2547 | m->generator_unit_path, |
2548 | m->generator_unit_path_early, | |
2549 | m->generator_unit_path_late); | |
e735f4d4 | 2550 | if (q < 0 && r >= 0) |
663996b3 MS |
2551 | r = q; |
2552 | ||
2553 | manager_build_unit_path_cache(m); | |
2554 | ||
2555 | /* First, enumerate what we can from all config files */ | |
db2df898 | 2556 | manager_enumerate(m); |
663996b3 MS |
2557 | |
2558 | /* Second, deserialize our stored data */ | |
2559 | q = manager_deserialize(m, f, fds); | |
e735f4d4 | 2560 | if (q < 0 && r >= 0) |
663996b3 MS |
2561 | r = q; |
2562 | ||
2563 | fclose(f); | |
2564 | f = NULL; | |
2565 | ||
60f067b4 JS |
2566 | /* Re-register notify_fd as event source */ |
2567 | q = manager_setup_notify(m); | |
e735f4d4 | 2568 | if (q < 0 && r >= 0) |
60f067b4 JS |
2569 | r = q; |
2570 | ||
663996b3 | 2571 | /* Third, fire things up! */ |
e3bff60a | 2572 | manager_coldplug(m); |
663996b3 | 2573 | |
4c89c718 MP |
2574 | /* Sync current state of bus names with our set of listening units */ |
2575 | if (m->api_bus) | |
2576 | manager_sync_bus_names(m, m->api_bus); | |
2577 | ||
663996b3 MS |
2578 | assert(m->n_reloading > 0); |
2579 | m->n_reloading--; | |
2580 | ||
14228c0d MB |
2581 | m->send_reloading_done = true; |
2582 | ||
663996b3 MS |
2583 | return r; |
2584 | } | |
2585 | ||
663996b3 MS |
2586 | bool manager_is_reloading_or_reexecuting(Manager *m) { |
2587 | assert(m); | |
2588 | ||
2589 | return m->n_reloading != 0; | |
2590 | } | |
2591 | ||
2592 | void manager_reset_failed(Manager *m) { | |
2593 | Unit *u; | |
2594 | Iterator i; | |
2595 | ||
2596 | assert(m); | |
2597 | ||
2598 | HASHMAP_FOREACH(u, m->units, i) | |
2599 | unit_reset_failed(u); | |
2600 | } | |
2601 | ||
2602 | bool manager_unit_inactive_or_pending(Manager *m, const char *name) { | |
2603 | Unit *u; | |
2604 | ||
2605 | assert(m); | |
2606 | assert(name); | |
2607 | ||
2608 | /* Returns true if the unit is inactive or going down */ | |
2609 | u = manager_get_unit(m, name); | |
2610 | if (!u) | |
2611 | return true; | |
2612 | ||
2613 | return unit_inactive_or_pending(u); | |
2614 | } | |
2615 | ||
f47781d8 | 2616 | static void manager_notify_finished(Manager *m) { |
663996b3 MS |
2617 | char userspace[FORMAT_TIMESPAN_MAX], initrd[FORMAT_TIMESPAN_MAX], kernel[FORMAT_TIMESPAN_MAX], sum[FORMAT_TIMESPAN_MAX]; |
2618 | usec_t firmware_usec, loader_usec, kernel_usec, initrd_usec, userspace_usec, total_usec; | |
60f067b4 | 2619 | |
f47781d8 | 2620 | if (m->test_run) |
663996b3 MS |
2621 | return; |
2622 | ||
6300502b | 2623 | if (m->running_as == MANAGER_SYSTEM && detect_container() <= 0) { |
663996b3 MS |
2624 | |
2625 | /* Note that m->kernel_usec.monotonic is always at 0, | |
2626 | * and m->firmware_usec.monotonic and | |
2627 | * m->loader_usec.monotonic should be considered | |
2628 | * negative values. */ | |
2629 | ||
2630 | firmware_usec = m->firmware_timestamp.monotonic - m->loader_timestamp.monotonic; | |
2631 | loader_usec = m->loader_timestamp.monotonic - m->kernel_timestamp.monotonic; | |
2632 | userspace_usec = m->finish_timestamp.monotonic - m->userspace_timestamp.monotonic; | |
2633 | total_usec = m->firmware_timestamp.monotonic + m->finish_timestamp.monotonic; | |
2634 | ||
2635 | if (dual_timestamp_is_set(&m->initrd_timestamp)) { | |
2636 | ||
2637 | kernel_usec = m->initrd_timestamp.monotonic - m->kernel_timestamp.monotonic; | |
2638 | initrd_usec = m->userspace_timestamp.monotonic - m->initrd_timestamp.monotonic; | |
2639 | ||
5eef597e | 2640 | log_struct(LOG_INFO, |
f47781d8 | 2641 | LOG_MESSAGE_ID(SD_MESSAGE_STARTUP_FINISHED), |
5eef597e MP |
2642 | "KERNEL_USEC="USEC_FMT, kernel_usec, |
2643 | "INITRD_USEC="USEC_FMT, initrd_usec, | |
2644 | "USERSPACE_USEC="USEC_FMT, userspace_usec, | |
f47781d8 MP |
2645 | LOG_MESSAGE("Startup finished in %s (kernel) + %s (initrd) + %s (userspace) = %s.", |
2646 | format_timespan(kernel, sizeof(kernel), kernel_usec, USEC_PER_MSEC), | |
2647 | format_timespan(initrd, sizeof(initrd), initrd_usec, USEC_PER_MSEC), | |
2648 | format_timespan(userspace, sizeof(userspace), userspace_usec, USEC_PER_MSEC), | |
2649 | format_timespan(sum, sizeof(sum), total_usec, USEC_PER_MSEC)), | |
5eef597e | 2650 | NULL); |
663996b3 MS |
2651 | } else { |
2652 | kernel_usec = m->userspace_timestamp.monotonic - m->kernel_timestamp.monotonic; | |
2653 | initrd_usec = 0; | |
2654 | ||
663996b3 | 2655 | log_struct(LOG_INFO, |
f47781d8 | 2656 | LOG_MESSAGE_ID(SD_MESSAGE_STARTUP_FINISHED), |
5eef597e | 2657 | "KERNEL_USEC="USEC_FMT, kernel_usec, |
60f067b4 | 2658 | "USERSPACE_USEC="USEC_FMT, userspace_usec, |
f47781d8 MP |
2659 | LOG_MESSAGE("Startup finished in %s (kernel) + %s (userspace) = %s.", |
2660 | format_timespan(kernel, sizeof(kernel), kernel_usec, USEC_PER_MSEC), | |
2661 | format_timespan(userspace, sizeof(userspace), userspace_usec, USEC_PER_MSEC), | |
2662 | format_timespan(sum, sizeof(sum), total_usec, USEC_PER_MSEC)), | |
663996b3 | 2663 | NULL); |
5eef597e MP |
2664 | } |
2665 | } else { | |
2666 | firmware_usec = loader_usec = initrd_usec = kernel_usec = 0; | |
2667 | total_usec = userspace_usec = m->finish_timestamp.monotonic - m->userspace_timestamp.monotonic; | |
2668 | ||
2669 | log_struct(LOG_INFO, | |
f47781d8 | 2670 | LOG_MESSAGE_ID(SD_MESSAGE_STARTUP_FINISHED), |
5eef597e | 2671 | "USERSPACE_USEC="USEC_FMT, userspace_usec, |
f47781d8 MP |
2672 | LOG_MESSAGE("Startup finished in %s.", |
2673 | format_timespan(sum, sizeof(sum), total_usec, USEC_PER_MSEC)), | |
5eef597e | 2674 | NULL); |
663996b3 MS |
2675 | } |
2676 | ||
60f067b4 | 2677 | bus_manager_send_finished(m, firmware_usec, loader_usec, kernel_usec, initrd_usec, userspace_usec, total_usec); |
663996b3 MS |
2678 | |
2679 | sd_notifyf(false, | |
5eef597e MP |
2680 | "READY=1\n" |
2681 | "STATUS=Startup finished in %s.", | |
663996b3 MS |
2682 | format_timespan(sum, sizeof(sum), total_usec, USEC_PER_MSEC)); |
2683 | } | |
2684 | ||
f47781d8 | 2685 | void manager_check_finished(Manager *m) { |
f47781d8 MP |
2686 | assert(m); |
2687 | ||
e3bff60a MP |
2688 | if (m->n_reloading > 0) |
2689 | return; | |
2690 | ||
2691 | /* Verify that we are actually running currently. Initially | |
2692 | * the exit code is set to invalid, and during operation it is | |
2693 | * then set to MANAGER_OK */ | |
2694 | if (m->exit_code != MANAGER_OK) | |
2695 | return; | |
2696 | ||
f47781d8 | 2697 | if (hashmap_size(m->jobs) > 0) { |
f47781d8 | 2698 | if (m->jobs_in_progress_event_source) |
e3bff60a | 2699 | /* Ignore any failure, this is only for feedback */ |
6300502b | 2700 | (void) sd_event_source_set_time(m->jobs_in_progress_event_source, now(CLOCK_MONOTONIC) + JOBS_IN_PROGRESS_WAIT_USEC); |
f47781d8 MP |
2701 | |
2702 | return; | |
2703 | } | |
2704 | ||
2705 | manager_flip_auto_status(m, false); | |
2706 | ||
2707 | /* Notify Type=idle units that we are done now */ | |
f47781d8 MP |
2708 | manager_close_idle_pipe(m); |
2709 | ||
2710 | /* Turn off confirm spawn now */ | |
2711 | m->confirm_spawn = false; | |
2712 | ||
2713 | /* No need to update ask password status when we're going non-interactive */ | |
2714 | manager_close_ask_password(m); | |
2715 | ||
2716 | /* This is no longer the first boot */ | |
2717 | manager_set_first_boot(m, false); | |
2718 | ||
2719 | if (dual_timestamp_is_set(&m->finish_timestamp)) | |
2720 | return; | |
2721 | ||
2722 | dual_timestamp_get(&m->finish_timestamp); | |
2723 | ||
2724 | manager_notify_finished(m); | |
2725 | ||
6300502b | 2726 | manager_invalidate_startup_units(m); |
f47781d8 MP |
2727 | } |
2728 | ||
663996b3 MS |
2729 | static int create_generator_dir(Manager *m, char **generator, const char *name) { |
2730 | char *p; | |
2731 | int r; | |
2732 | ||
2733 | assert(m); | |
2734 | assert(generator); | |
2735 | assert(name); | |
2736 | ||
2737 | if (*generator) | |
2738 | return 0; | |
2739 | ||
e3bff60a | 2740 | if (m->running_as == MANAGER_SYSTEM && getpid() == 1) { |
60f067b4 | 2741 | /* systemd --system, not running --test */ |
663996b3 MS |
2742 | |
2743 | p = strappend("/run/systemd/", name); | |
2744 | if (!p) | |
2745 | return log_oom(); | |
2746 | ||
60f067b4 JS |
2747 | r = mkdir_p_label(p, 0755); |
2748 | if (r < 0) { | |
f47781d8 | 2749 | log_error_errno(r, "Failed to create generator directory %s: %m", p); |
60f067b4 JS |
2750 | free(p); |
2751 | return r; | |
2752 | } | |
e3bff60a | 2753 | } else if (m->running_as == MANAGER_USER) { |
60f067b4 JS |
2754 | const char *s = NULL; |
2755 | ||
2756 | s = getenv("XDG_RUNTIME_DIR"); | |
2757 | if (!s) | |
2758 | return -EINVAL; | |
2759 | p = strjoin(s, "/systemd/", name, NULL); | |
2760 | if (!p) | |
2761 | return log_oom(); | |
2762 | ||
663996b3 MS |
2763 | r = mkdir_p_label(p, 0755); |
2764 | if (r < 0) { | |
f47781d8 | 2765 | log_error_errno(r, "Failed to create generator directory %s: %m", p); |
663996b3 MS |
2766 | free(p); |
2767 | return r; | |
2768 | } | |
2769 | } else { | |
60f067b4 JS |
2770 | /* systemd --system --test */ |
2771 | ||
663996b3 MS |
2772 | p = strjoin("/tmp/systemd-", name, ".XXXXXX", NULL); |
2773 | if (!p) | |
2774 | return log_oom(); | |
2775 | ||
2776 | if (!mkdtemp(p)) { | |
db2df898 | 2777 | log_error_errno(errno, "Failed to create generator directory %s: %m", p); |
663996b3 MS |
2778 | free(p); |
2779 | return -errno; | |
2780 | } | |
2781 | } | |
2782 | ||
2783 | *generator = p; | |
2784 | return 0; | |
2785 | } | |
2786 | ||
2787 | static void trim_generator_dir(Manager *m, char **generator) { | |
2788 | assert(m); | |
2789 | assert(generator); | |
2790 | ||
2791 | if (!*generator) | |
2792 | return; | |
2793 | ||
6300502b MP |
2794 | if (rmdir(*generator) >= 0) |
2795 | *generator = mfree(*generator); | |
663996b3 MS |
2796 | |
2797 | return; | |
2798 | } | |
2799 | ||
e735f4d4 | 2800 | static int manager_run_generators(Manager *m) { |
e3bff60a | 2801 | _cleanup_strv_free_ char **paths = NULL; |
663996b3 | 2802 | const char *argv[5]; |
e735f4d4 | 2803 | char **path; |
663996b3 MS |
2804 | int r; |
2805 | ||
2806 | assert(m); | |
2807 | ||
5eef597e | 2808 | if (m->test_run) |
e735f4d4 | 2809 | return 0; |
663996b3 | 2810 | |
e735f4d4 MP |
2811 | paths = generator_paths(m->running_as); |
2812 | if (!paths) | |
2813 | return log_oom(); | |
2814 | ||
2815 | /* Optimize by skipping the whole process by not creating output directories | |
2816 | * if no generators are found. */ | |
2817 | STRV_FOREACH(path, paths) { | |
2818 | r = access(*path, F_OK); | |
2819 | if (r == 0) | |
2820 | goto found; | |
2821 | if (errno != ENOENT) | |
2822 | log_warning_errno(errno, "Failed to open generator directory %s: %m", *path); | |
663996b3 | 2823 | } |
e735f4d4 | 2824 | return 0; |
663996b3 | 2825 | |
e735f4d4 | 2826 | found: |
663996b3 MS |
2827 | r = create_generator_dir(m, &m->generator_unit_path, "generator"); |
2828 | if (r < 0) | |
2829 | goto finish; | |
2830 | ||
2831 | r = create_generator_dir(m, &m->generator_unit_path_early, "generator.early"); | |
2832 | if (r < 0) | |
2833 | goto finish; | |
2834 | ||
2835 | r = create_generator_dir(m, &m->generator_unit_path_late, "generator.late"); | |
2836 | if (r < 0) | |
2837 | goto finish; | |
2838 | ||
2839 | argv[0] = NULL; /* Leave this empty, execute_directory() will fill something in */ | |
2840 | argv[1] = m->generator_unit_path; | |
2841 | argv[2] = m->generator_unit_path_early; | |
2842 | argv[3] = m->generator_unit_path_late; | |
2843 | argv[4] = NULL; | |
2844 | ||
60f067b4 | 2845 | RUN_WITH_UMASK(0022) |
e735f4d4 | 2846 | execute_directories((const char* const*) paths, DEFAULT_TIMEOUT_USEC, (char**) argv); |
663996b3 | 2847 | |
60f067b4 | 2848 | finish: |
663996b3 MS |
2849 | trim_generator_dir(m, &m->generator_unit_path); |
2850 | trim_generator_dir(m, &m->generator_unit_path_early); | |
2851 | trim_generator_dir(m, &m->generator_unit_path_late); | |
e735f4d4 | 2852 | return r; |
663996b3 MS |
2853 | } |
2854 | ||
2855 | static void remove_generator_dir(Manager *m, char **generator) { | |
2856 | assert(m); | |
2857 | assert(generator); | |
2858 | ||
2859 | if (!*generator) | |
2860 | return; | |
2861 | ||
2862 | strv_remove(m->lookup_paths.unit_path, *generator); | |
e3bff60a | 2863 | (void) rm_rf(*generator, REMOVE_ROOT); |
663996b3 | 2864 | |
6300502b | 2865 | *generator = mfree(*generator); |
663996b3 MS |
2866 | } |
2867 | ||
e735f4d4 | 2868 | static void manager_undo_generators(Manager *m) { |
663996b3 MS |
2869 | assert(m); |
2870 | ||
2871 | remove_generator_dir(m, &m->generator_unit_path); | |
2872 | remove_generator_dir(m, &m->generator_unit_path_early); | |
2873 | remove_generator_dir(m, &m->generator_unit_path_late); | |
2874 | } | |
2875 | ||
60f067b4 JS |
2876 | int manager_environment_add(Manager *m, char **minus, char **plus) { |
2877 | char **a = NULL, **b = NULL, **l; | |
663996b3 MS |
2878 | assert(m); |
2879 | ||
60f067b4 | 2880 | l = m->environment; |
663996b3 | 2881 | |
60f067b4 JS |
2882 | if (!strv_isempty(minus)) { |
2883 | a = strv_env_delete(l, 1, minus); | |
2884 | if (!a) | |
2885 | return -ENOMEM; | |
2886 | ||
2887 | l = a; | |
2888 | } | |
2889 | ||
2890 | if (!strv_isempty(plus)) { | |
2891 | b = strv_env_merge(2, l, plus); | |
5eef597e MP |
2892 | if (!b) { |
2893 | strv_free(a); | |
60f067b4 | 2894 | return -ENOMEM; |
5eef597e | 2895 | } |
60f067b4 JS |
2896 | |
2897 | l = b; | |
2898 | } | |
2899 | ||
2900 | if (m->environment != l) | |
2901 | strv_free(m->environment); | |
2902 | if (a != l) | |
2903 | strv_free(a); | |
2904 | if (b != l) | |
2905 | strv_free(b); | |
2906 | ||
2907 | m->environment = l; | |
2908 | manager_clean_environment(m); | |
2909 | strv_sort(m->environment); | |
663996b3 MS |
2910 | |
2911 | return 0; | |
2912 | } | |
2913 | ||
2914 | int manager_set_default_rlimits(Manager *m, struct rlimit **default_rlimit) { | |
2915 | int i; | |
2916 | ||
2917 | assert(m); | |
2918 | ||
60f067b4 | 2919 | for (i = 0; i < _RLIMIT_MAX; i++) { |
4c89c718 MP |
2920 | m->rlimit[i] = mfree(m->rlimit[i]); |
2921 | ||
663996b3 MS |
2922 | if (!default_rlimit[i]) |
2923 | continue; | |
2924 | ||
2925 | m->rlimit[i] = newdup(struct rlimit, default_rlimit[i], 1); | |
2926 | if (!m->rlimit[i]) | |
2927 | return -ENOMEM; | |
2928 | } | |
2929 | ||
2930 | return 0; | |
2931 | } | |
2932 | ||
2933 | void manager_recheck_journal(Manager *m) { | |
2934 | Unit *u; | |
2935 | ||
2936 | assert(m); | |
2937 | ||
e3bff60a | 2938 | if (m->running_as != MANAGER_SYSTEM) |
663996b3 MS |
2939 | return; |
2940 | ||
2941 | u = manager_get_unit(m, SPECIAL_JOURNALD_SOCKET); | |
2942 | if (u && SOCKET(u)->state != SOCKET_RUNNING) { | |
2943 | log_close_journal(); | |
2944 | return; | |
2945 | } | |
2946 | ||
2947 | u = manager_get_unit(m, SPECIAL_JOURNALD_SERVICE); | |
2948 | if (u && SERVICE(u)->state != SERVICE_RUNNING) { | |
2949 | log_close_journal(); | |
2950 | return; | |
2951 | } | |
2952 | ||
2953 | /* Hmm, OK, so the socket is fully up and the service is up | |
2954 | * too, then let's make use of the thing. */ | |
2955 | log_open(); | |
2956 | } | |
2957 | ||
60f067b4 | 2958 | void manager_set_show_status(Manager *m, ShowStatus mode) { |
663996b3 | 2959 | assert(m); |
60f067b4 | 2960 | assert(IN_SET(mode, SHOW_STATUS_AUTO, SHOW_STATUS_NO, SHOW_STATUS_YES, SHOW_STATUS_TEMPORARY)); |
663996b3 | 2961 | |
e3bff60a | 2962 | if (m->running_as != MANAGER_SYSTEM) |
663996b3 MS |
2963 | return; |
2964 | ||
4c89c718 MP |
2965 | if (m->show_status != mode) |
2966 | log_debug("%s showing of status.", | |
2967 | mode == SHOW_STATUS_NO ? "Disabling" : "Enabling"); | |
60f067b4 | 2968 | m->show_status = mode; |
663996b3 | 2969 | |
60f067b4 | 2970 | if (mode > 0) |
db2df898 | 2971 | (void) touch("/run/systemd/show-status"); |
663996b3 | 2972 | else |
db2df898 | 2973 | (void) unlink("/run/systemd/show-status"); |
663996b3 MS |
2974 | } |
2975 | ||
5eef597e | 2976 | static bool manager_get_show_status(Manager *m, StatusType type) { |
663996b3 MS |
2977 | assert(m); |
2978 | ||
e3bff60a | 2979 | if (m->running_as != MANAGER_SYSTEM) |
663996b3 MS |
2980 | return false; |
2981 | ||
14228c0d MB |
2982 | if (m->no_console_output) |
2983 | return false; | |
2984 | ||
5eef597e MP |
2985 | if (!IN_SET(manager_state(m), MANAGER_INITIALIZING, MANAGER_STARTING, MANAGER_STOPPING)) |
2986 | return false; | |
2987 | ||
2988 | /* If we cannot find out the status properly, just proceed. */ | |
2989 | if (type != STATUS_TYPE_EMERGENCY && manager_check_ask_password(m) > 0) | |
60f067b4 JS |
2990 | return false; |
2991 | ||
2992 | if (m->show_status > 0) | |
663996b3 MS |
2993 | return true; |
2994 | ||
e3bff60a | 2995 | return false; |
663996b3 MS |
2996 | } |
2997 | ||
5eef597e MP |
2998 | void manager_set_first_boot(Manager *m, bool b) { |
2999 | assert(m); | |
3000 | ||
e3bff60a | 3001 | if (m->running_as != MANAGER_SYSTEM) |
5eef597e MP |
3002 | return; |
3003 | ||
d9dfd233 MP |
3004 | if (m->first_boot != (int) b) { |
3005 | if (b) | |
3006 | (void) touch("/run/systemd/first-boot"); | |
3007 | else | |
3008 | (void) unlink("/run/systemd/first-boot"); | |
3009 | } | |
5eef597e | 3010 | |
d9dfd233 | 3011 | m->first_boot = b; |
5eef597e MP |
3012 | } |
3013 | ||
3014 | void manager_status_printf(Manager *m, StatusType type, const char *status, const char *format, ...) { | |
663996b3 MS |
3015 | va_list ap; |
3016 | ||
f47781d8 MP |
3017 | /* If m is NULL, assume we're after shutdown and let the messages through. */ |
3018 | ||
3019 | if (m && !manager_get_show_status(m, type)) | |
663996b3 MS |
3020 | return; |
3021 | ||
3022 | /* XXX We should totally drop the check for ephemeral here | |
3023 | * and thus effectively make 'Type=idle' pointless. */ | |
f47781d8 | 3024 | if (type == STATUS_TYPE_EPHEMERAL && m && m->n_on_console > 0) |
663996b3 MS |
3025 | return; |
3026 | ||
663996b3 | 3027 | va_start(ap, format); |
5eef597e | 3028 | status_vprintf(status, true, type == STATUS_TYPE_EPHEMERAL, format, ap); |
663996b3 MS |
3029 | va_end(ap); |
3030 | } | |
3031 | ||
14228c0d MB |
3032 | Set *manager_get_units_requiring_mounts_for(Manager *m, const char *path) { |
3033 | char p[strlen(path)+1]; | |
3034 | ||
3035 | assert(m); | |
3036 | assert(path); | |
3037 | ||
3038 | strcpy(p, path); | |
3039 | path_kill_slashes(p); | |
3040 | ||
3041 | return hashmap_get(m->units_requiring_mounts_for, streq(p, "/") ? "" : p); | |
3042 | } | |
3043 | ||
60f067b4 JS |
3044 | const char *manager_get_runtime_prefix(Manager *m) { |
3045 | assert(m); | |
3046 | ||
e3bff60a | 3047 | return m->running_as == MANAGER_SYSTEM ? |
60f067b4 JS |
3048 | "/run" : |
3049 | getenv("XDG_RUNTIME_DIR"); | |
3050 | } | |
3051 | ||
6300502b | 3052 | int manager_update_failed_units(Manager *m, Unit *u, bool failed) { |
e3bff60a | 3053 | unsigned size; |
6300502b | 3054 | int r; |
e3bff60a MP |
3055 | |
3056 | assert(m); | |
3057 | assert(u->manager == m); | |
3058 | ||
3059 | size = set_size(m->failed_units); | |
3060 | ||
3061 | if (failed) { | |
6300502b MP |
3062 | r = set_ensure_allocated(&m->failed_units, NULL); |
3063 | if (r < 0) | |
3064 | return log_oom(); | |
3065 | ||
e3bff60a | 3066 | if (set_put(m->failed_units, u) < 0) |
6300502b | 3067 | return log_oom(); |
e3bff60a | 3068 | } else |
6300502b | 3069 | (void) set_remove(m->failed_units, u); |
e3bff60a MP |
3070 | |
3071 | if (set_size(m->failed_units) != size) | |
3072 | bus_manager_send_change_signal(m); | |
6300502b MP |
3073 | |
3074 | return 0; | |
e3bff60a MP |
3075 | } |
3076 | ||
60f067b4 JS |
3077 | ManagerState manager_state(Manager *m) { |
3078 | Unit *u; | |
3079 | ||
3080 | assert(m); | |
3081 | ||
3082 | /* Did we ever finish booting? If not then we are still starting up */ | |
5eef597e MP |
3083 | if (!dual_timestamp_is_set(&m->finish_timestamp)) { |
3084 | ||
3085 | u = manager_get_unit(m, SPECIAL_BASIC_TARGET); | |
3086 | if (!u || !UNIT_IS_ACTIVE_OR_RELOADING(unit_active_state(u))) | |
3087 | return MANAGER_INITIALIZING; | |
3088 | ||
60f067b4 | 3089 | return MANAGER_STARTING; |
5eef597e | 3090 | } |
663996b3 | 3091 | |
60f067b4 JS |
3092 | /* Is the special shutdown target queued? If so, we are in shutdown state */ |
3093 | u = manager_get_unit(m, SPECIAL_SHUTDOWN_TARGET); | |
4c89c718 | 3094 | if (u && u->job && IN_SET(u->job->type, JOB_START, JOB_RESTART, JOB_RELOAD_OR_START)) |
60f067b4 JS |
3095 | return MANAGER_STOPPING; |
3096 | ||
3097 | /* Are the rescue or emergency targets active or queued? If so we are in maintenance state */ | |
3098 | u = manager_get_unit(m, SPECIAL_RESCUE_TARGET); | |
3099 | if (u && (UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(u)) || | |
4c89c718 | 3100 | (u->job && IN_SET(u->job->type, JOB_START, JOB_RESTART, JOB_RELOAD_OR_START)))) |
60f067b4 JS |
3101 | return MANAGER_MAINTENANCE; |
3102 | ||
3103 | u = manager_get_unit(m, SPECIAL_EMERGENCY_TARGET); | |
3104 | if (u && (UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(u)) || | |
4c89c718 | 3105 | (u->job && IN_SET(u->job->type, JOB_START, JOB_RESTART, JOB_RELOAD_OR_START)))) |
60f067b4 JS |
3106 | return MANAGER_MAINTENANCE; |
3107 | ||
3108 | /* Are there any failed units? If so, we are in degraded mode */ | |
3109 | if (set_size(m->failed_units) > 0) | |
3110 | return MANAGER_DEGRADED; | |
3111 | ||
3112 | return MANAGER_RUNNING; | |
663996b3 | 3113 | } |
60f067b4 JS |
3114 | |
3115 | static const char *const manager_state_table[_MANAGER_STATE_MAX] = { | |
5eef597e | 3116 | [MANAGER_INITIALIZING] = "initializing", |
60f067b4 JS |
3117 | [MANAGER_STARTING] = "starting", |
3118 | [MANAGER_RUNNING] = "running", | |
3119 | [MANAGER_DEGRADED] = "degraded", | |
3120 | [MANAGER_MAINTENANCE] = "maintenance", | |
3121 | [MANAGER_STOPPING] = "stopping", | |
3122 | }; | |
3123 | ||
3124 | DEFINE_STRING_TABLE_LOOKUP(manager_state, ManagerState); |