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