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