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