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