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