]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/core/manager.c
transaction: do not add installed jobs to the transaction
[thirdparty/systemd.git] / src / core / manager.c
CommitLineData
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>
830f6caa
LP
39
40#ifdef HAVE_AUDIT
4927fcae 41#include <libaudit.h>
830f6caa 42#endif
60918275 43
81527be1
LP
44#include <systemd/sd-daemon.h>
45
60918275 46#include "manager.h"
75778e21 47#include "transaction.h"
60918275
LP
48#include "hashmap.h"
49#include "macro.h"
50#include "strv.h"
16354eff 51#include "log.h"
2a987ee8 52#include "util.h"
49e942b2 53#include "mkdir.h"
ea430986 54#include "ratelimit.h"
8e274523
LP
55#include "cgroup.h"
56#include "mount-setup.h"
9e2f7c11 57#include "unit-name.h"
4139c1b2
LP
58#include "dbus-unit.h"
59#include "dbus-job.h"
1137a57c 60#include "missing.h"
84e3543e 61#include "path-lookup.h"
514f4ef5 62#include "special.h"
398ef8ba 63#include "bus-errors.h"
d06dacd0 64#include "exit-status.h"
5dc4c17f 65#include "virt.h"
e96d6be7 66#include "watchdog.h"
b59e2465 67#include "cgroup-util.h"
60918275 68
701cc384
LP
69/* As soon as 16 units are in our GC queue, make sure to run a gc sweep */
70#define GC_QUEUE_ENTRIES_MAX 16
71
72/* As soon as 5s passed since a unit was added to our GC queue, make sure to run a gc sweep */
94b6dfa2 73#define GC_QUEUE_USEC_MAX (10*USEC_PER_SEC)
701cc384 74
8c47c732 75/* Where clients shall send notification messages to */
2b583ce6 76#define NOTIFY_SOCKET_SYSTEM "/run/systemd/notify"
91b22f21 77#define NOTIFY_SOCKET_USER "@/org/freedesktop/systemd1/notify"
8c47c732
LP
78
79static int manager_setup_notify(Manager *m) {
80 union {
81 struct sockaddr sa;
82 struct sockaddr_un un;
83 } sa;
84 struct epoll_event ev;
1d6702e8
LP
85 int one = 1, r;
86 mode_t u;
8c47c732
LP
87
88 assert(m);
89
90 m->notify_watch.type = WATCH_NOTIFY;
91 if ((m->notify_watch.fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0)) < 0) {
92 log_error("Failed to allocate notification socket: %m");
93 return -errno;
94 }
95
96 zero(sa);
97 sa.sa.sa_family = AF_UNIX;
98
a821caaa 99 if (getpid() != 1)
91b22f21 100 snprintf(sa.un.sun_path, sizeof(sa.un.sun_path), NOTIFY_SOCKET_USER "/%llu", random_ull());
6f79c579
LP
101 else {
102 unlink(NOTIFY_SOCKET_SYSTEM);
91b22f21 103 strncpy(sa.un.sun_path, NOTIFY_SOCKET_SYSTEM, sizeof(sa.un.sun_path));
6f79c579 104 }
91b22f21
LP
105
106 if (sa.un.sun_path[0] == '@')
107 sa.un.sun_path[0] = 0;
8c47c732 108
1d6702e8
LP
109 u = umask(0111);
110 r = bind(m->notify_watch.fd, &sa.sa, offsetof(struct sockaddr_un, sun_path) + 1 + strlen(sa.un.sun_path+1));
111 umask(u);
112
113 if (r < 0) {
8c47c732
LP
114 log_error("bind() failed: %m");
115 return -errno;
116 }
117
118 if (setsockopt(m->notify_watch.fd, SOL_SOCKET, SO_PASSCRED, &one, sizeof(one)) < 0) {
119 log_error("SO_PASSCRED failed: %m");
120 return -errno;
121 }
122
123 zero(ev);
124 ev.events = EPOLLIN;
125 ev.data.ptr = &m->notify_watch;
126
127 if (epoll_ctl(m->epoll_fd, EPOLL_CTL_ADD, m->notify_watch.fd, &ev) < 0)
128 return -errno;
129
91b22f21
LP
130 if (sa.un.sun_path[0] == 0)
131 sa.un.sun_path[0] = '@';
132
133 if (!(m->notify_socket = strdup(sa.un.sun_path)))
8c47c732
LP
134 return -ENOMEM;
135
b2bb3dbe
LP
136 log_debug("Using notification socket %s", m->notify_socket);
137
8c47c732
LP
138 return 0;
139}
140
80876c20 141static int enable_special_signals(Manager *m) {
4466ee6a 142 int fd;
80876c20
LP
143
144 assert(m);
145
a41b539e
LP
146 /* Enable that we get SIGINT on control-alt-del. In containers
147 * this will fail with EPERM, so ignore that. */
148 if (reboot(RB_DISABLE_CAD) < 0 && errno != EPERM)
80876c20
LP
149 log_warning("Failed to enable ctrl-alt-del handling: %m");
150
a41b539e
LP
151 fd = open_terminal("/dev/tty0", O_RDWR|O_NOCTTY|O_CLOEXEC);
152 if (fd < 0) {
153 /* Support systems without virtual console */
154 if (fd != -ENOENT)
155 log_warning("Failed to open /dev/tty0: %m");
156 } else {
80876c20
LP
157 /* Enable that we get SIGWINCH on kbrequest */
158 if (ioctl(fd, KDSIGACCEPT, SIGWINCH) < 0)
159 log_warning("Failed to enable kbrequest handling: %s", strerror(errno));
160
161 close_nointr_nofail(fd);
162 }
163
164 return 0;
165}
166
ce578209 167static int manager_setup_signals(Manager *m) {
9152c765
LP
168 sigset_t mask;
169 struct epoll_event ev;
57c0c30e 170 struct sigaction sa;
60918275 171
ce578209
LP
172 assert(m);
173
57c0c30e
LP
174 /* We are not interested in SIGSTOP and friends. */
175 zero(sa);
176 sa.sa_handler = SIG_DFL;
177 sa.sa_flags = SA_NOCLDSTOP|SA_RESTART;
178 assert_se(sigaction(SIGCHLD, &sa, NULL) == 0);
179
ce578209 180 assert_se(sigemptyset(&mask) == 0);
7d793605
LP
181
182 sigset_add_many(&mask,
183 SIGCHLD, /* Child died */
184 SIGTERM, /* Reexecute daemon */
185 SIGHUP, /* Reload configuration */
186 SIGUSR1, /* systemd/upstart: reconnect to D-Bus */
187 SIGUSR2, /* systemd: dump status */
188 SIGINT, /* Kernel sends us this on control-alt-del */
189 SIGWINCH, /* Kernel sends us this on kbrequest (alt-arrowup) */
190 SIGPWR, /* Some kernel drivers and upsd send us this on power failure */
191 SIGRTMIN+0, /* systemd: start default.target */
0003d1ab 192 SIGRTMIN+1, /* systemd: isolate rescue.target */
7d793605
LP
193 SIGRTMIN+2, /* systemd: isolate emergency.target */
194 SIGRTMIN+3, /* systemd: start halt.target */
195 SIGRTMIN+4, /* systemd: start poweroff.target */
196 SIGRTMIN+5, /* systemd: start reboot.target */
0003d1ab
LP
197 SIGRTMIN+6, /* systemd: start kexec.target */
198 SIGRTMIN+13, /* systemd: Immediate halt */
199 SIGRTMIN+14, /* systemd: Immediate poweroff */
200 SIGRTMIN+15, /* systemd: Immediate reboot */
201 SIGRTMIN+16, /* systemd: Immediate kexec */
0658666b
LP
202 SIGRTMIN+20, /* systemd: enable status messages */
203 SIGRTMIN+21, /* systemd: disable status messages */
253ee27a
LP
204 SIGRTMIN+22, /* systemd: set log level to LOG_DEBUG */
205 SIGRTMIN+23, /* systemd: set log level to LOG_INFO */
4cfa2c99 206 SIGRTMIN+26, /* systemd: set log target to journal-or-kmsg */
253ee27a
LP
207 SIGRTMIN+27, /* systemd: set log target to console */
208 SIGRTMIN+28, /* systemd: set log target to kmsg */
209 SIGRTMIN+29, /* systemd: set log target to syslog-or-kmsg */
7d793605 210 -1);
ce578209
LP
211 assert_se(sigprocmask(SIG_SETMASK, &mask, NULL) == 0);
212
ef734fd6 213 m->signal_watch.type = WATCH_SIGNAL;
ce578209
LP
214 if ((m->signal_watch.fd = signalfd(-1, &mask, SFD_NONBLOCK|SFD_CLOEXEC)) < 0)
215 return -errno;
216
217 zero(ev);
218 ev.events = EPOLLIN;
219 ev.data.ptr = &m->signal_watch;
220
221 if (epoll_ctl(m->epoll_fd, EPOLL_CTL_ADD, m->signal_watch.fd, &ev) < 0)
222 return -errno;
223
a3d4e06d 224 if (m->running_as == MANAGER_SYSTEM)
80876c20 225 return enable_special_signals(m);
e1414003 226
ce578209
LP
227 return 0;
228}
229
71ecc858
LP
230static void manager_strip_environment(Manager *m) {
231 assert(m);
232
233 /* Remove variables from the inherited set that are part of
234 * the container interface:
235 * http://www.freedesktop.org/wiki/Software/systemd/ContainerInterface */
236 strv_remove_prefix(m->environment, "container=");
237 strv_remove_prefix(m->environment, "container_");
238
239 /* Remove variables from the inherited set that are part of
240 * the initrd interface:
241 * http://www.freedesktop.org/wiki/Software/systemd/InitrdInterface */
242 strv_remove_prefix(m->environment, "RD_");
243}
244
9e58ff9c 245int manager_new(ManagerRunningAs running_as, Manager **_m) {
ce578209 246 Manager *m;
8e274523
LP
247 int r = -ENOMEM;
248
249 assert(_m);
a5dab5ce
LP
250 assert(running_as >= 0);
251 assert(running_as < _MANAGER_RUNNING_AS_MAX);
ce578209 252
60918275 253 if (!(m = new0(Manager, 1)))
8e274523 254 return -ENOMEM;
60918275 255
63983207 256 dual_timestamp_get(&m->startup_timestamp);
e537352b 257
a5dab5ce 258 m->running_as = running_as;
cbd37330 259 m->name_data_slot = m->conn_data_slot = m->subscribed_data_slot = -1;
a16e1123 260 m->exit_code = _MANAGER_EXIT_CODE_INVALID;
33be102a 261 m->pin_cgroupfs_fd = -1;
80876c20 262
4927fcae
LP
263#ifdef HAVE_AUDIT
264 m->audit_fd = -1;
265#endif
266
4e434314 267 m->signal_watch.fd = m->mount_watch.fd = m->udev_watch.fd = m->epoll_fd = m->dev_autofs_fd = m->swap_watch.fd = -1;
ea430986 268 m->current_job_id = 1; /* start as id #1, so that we can leave #0 around as "null-like" value */
9152c765 269
71ecc858
LP
270 m->environment = strv_copy(environ);
271 if (!m->environment)
1137a57c
LP
272 goto fail;
273
71ecc858
LP
274 manager_strip_environment(m);
275
88f06645
LP
276 if (running_as == MANAGER_SYSTEM) {
277 m->default_controllers = strv_new("cpu", NULL);
278 if (!m->default_controllers)
279 goto fail;
280 }
06d4c99a 281
87f0e418 282 if (!(m->units = hashmap_new(string_hash_func, string_compare_func)))
60918275
LP
283 goto fail;
284
285 if (!(m->jobs = hashmap_new(trivial_hash_func, trivial_compare_func)))
286 goto fail;
287
9152c765
LP
288 if (!(m->watch_pids = hashmap_new(trivial_hash_func, trivial_compare_func)))
289 goto fail;
290
8e274523
LP
291 if (!(m->cgroup_bondings = hashmap_new(string_hash_func, string_compare_func)))
292 goto fail;
293
05e343b7
LP
294 if (!(m->watch_bus = hashmap_new(string_hash_func, string_compare_func)))
295 goto fail;
296
9152c765
LP
297 if ((m->epoll_fd = epoll_create1(EPOLL_CLOEXEC)) < 0)
298 goto fail;
299
c800e483 300 if ((r = lookup_paths_init(&m->lookup_paths, m->running_as, true)) < 0)
e1414003
LP
301 goto fail;
302
8e274523
LP
303 if ((r = manager_setup_signals(m)) < 0)
304 goto fail;
305
8e274523 306 if ((r = manager_setup_cgroup(m)) < 0)
9152c765
LP
307 goto fail;
308
8c47c732
LP
309 if ((r = manager_setup_notify(m)) < 0)
310 goto fail;
311
f278026d 312 /* Try to connect to the busses, if possible. */
3996fbe2 313 if ((r = bus_init(m, running_as != MANAGER_SYSTEM)) < 0)
ea430986
LP
314 goto fail;
315
e543deae 316#ifdef HAVE_AUDIT
5a8d081c
JN
317 if ((m->audit_fd = audit_open()) < 0 &&
318 /* If the kernel lacks netlink or audit support,
319 * don't worry about it. */
320 errno != EAFNOSUPPORT && errno != EPROTONOSUPPORT)
4927fcae 321 log_error("Failed to connect to audit log: %m");
e543deae 322#endif
4927fcae 323
72bc8d00
LP
324 m->taint_usr = dir_is_empty("/usr") > 0;
325
8e274523
LP
326 *_m = m;
327 return 0;
60918275
LP
328
329fail:
330 manager_free(m);
8e274523 331 return r;
60918275
LP
332}
333
23a177ef 334static unsigned manager_dispatch_cleanup_queue(Manager *m) {
595ed347 335 Unit *u;
23a177ef
LP
336 unsigned n = 0;
337
338 assert(m);
339
595ed347
MS
340 while ((u = m->cleanup_queue)) {
341 assert(u->in_cleanup_queue);
23a177ef 342
595ed347 343 unit_free(u);
23a177ef
LP
344 n++;
345 }
346
347 return n;
348}
349
eced69b3 350enum {
35b8ca3a 351 GC_OFFSET_IN_PATH, /* This one is on the path we were traveling */
eced69b3
LP
352 GC_OFFSET_UNSURE, /* No clue */
353 GC_OFFSET_GOOD, /* We still need this unit */
354 GC_OFFSET_BAD, /* We don't need this unit anymore */
355 _GC_OFFSET_MAX
356};
357
358static void unit_gc_sweep(Unit *u, unsigned gc_marker) {
701cc384
LP
359 Iterator i;
360 Unit *other;
eced69b3 361 bool is_bad;
701cc384
LP
362
363 assert(u);
364
ac155bb8
MS
365 if (u->gc_marker == gc_marker + GC_OFFSET_GOOD ||
366 u->gc_marker == gc_marker + GC_OFFSET_BAD ||
367 u->gc_marker == gc_marker + GC_OFFSET_IN_PATH)
701cc384
LP
368 return;
369
ac155bb8 370 if (u->in_cleanup_queue)
701cc384
LP
371 goto bad;
372
373 if (unit_check_gc(u))
374 goto good;
375
ac155bb8 376 u->gc_marker = gc_marker + GC_OFFSET_IN_PATH;
eced69b3
LP
377
378 is_bad = true;
379
ac155bb8 380 SET_FOREACH(other, u->dependencies[UNIT_REFERENCED_BY], i) {
701cc384
LP
381 unit_gc_sweep(other, gc_marker);
382
ac155bb8 383 if (other->gc_marker == gc_marker + GC_OFFSET_GOOD)
701cc384 384 goto good;
eced69b3 385
ac155bb8 386 if (other->gc_marker != gc_marker + GC_OFFSET_BAD)
eced69b3 387 is_bad = false;
701cc384
LP
388 }
389
eced69b3
LP
390 if (is_bad)
391 goto bad;
392
393 /* We were unable to find anything out about this entry, so
394 * let's investigate it later */
ac155bb8 395 u->gc_marker = gc_marker + GC_OFFSET_UNSURE;
eced69b3
LP
396 unit_add_to_gc_queue(u);
397 return;
398
701cc384 399bad:
eced69b3
LP
400 /* We definitely know that this one is not useful anymore, so
401 * let's mark it for deletion */
ac155bb8 402 u->gc_marker = gc_marker + GC_OFFSET_BAD;
eced69b3 403 unit_add_to_cleanup_queue(u);
701cc384
LP
404 return;
405
406good:
ac155bb8 407 u->gc_marker = gc_marker + GC_OFFSET_GOOD;
701cc384
LP
408}
409
410static unsigned manager_dispatch_gc_queue(Manager *m) {
595ed347 411 Unit *u;
701cc384 412 unsigned n = 0;
eced69b3 413 unsigned gc_marker;
701cc384
LP
414
415 assert(m);
416
417 if ((m->n_in_gc_queue < GC_QUEUE_ENTRIES_MAX) &&
418 (m->gc_queue_timestamp <= 0 ||
419 (m->gc_queue_timestamp + GC_QUEUE_USEC_MAX) > now(CLOCK_MONOTONIC)))
420 return 0;
421
422 log_debug("Running GC...");
423
eced69b3
LP
424 m->gc_marker += _GC_OFFSET_MAX;
425 if (m->gc_marker + _GC_OFFSET_MAX <= _GC_OFFSET_MAX)
c9c0cadb 426 m->gc_marker = 1;
701cc384 427
eced69b3
LP
428 gc_marker = m->gc_marker;
429
595ed347
MS
430 while ((u = m->gc_queue)) {
431 assert(u->in_gc_queue);
701cc384 432
595ed347 433 unit_gc_sweep(u, gc_marker);
eced69b3 434
595ed347
MS
435 LIST_REMOVE(Unit, gc_queue, m->gc_queue, u);
436 u->in_gc_queue = false;
701cc384
LP
437
438 n++;
439
595ed347
MS
440 if (u->gc_marker == gc_marker + GC_OFFSET_BAD ||
441 u->gc_marker == gc_marker + GC_OFFSET_UNSURE) {
442 log_debug("Collecting %s", u->id);
443 u->gc_marker = gc_marker + GC_OFFSET_BAD;
444 unit_add_to_cleanup_queue(u);
701cc384
LP
445 }
446 }
447
448 m->n_in_gc_queue = 0;
449 m->gc_queue_timestamp = 0;
450
451 return n;
452}
453
a16e1123 454static void manager_clear_jobs_and_units(Manager *m) {
a16e1123 455 Unit *u;
60918275
LP
456
457 assert(m);
458
87f0e418
LP
459 while ((u = hashmap_first(m->units)))
460 unit_free(u);
964e0949
LP
461
462 manager_dispatch_cleanup_queue(m);
463
464 assert(!m->load_queue);
465 assert(!m->run_queue);
466 assert(!m->dbus_unit_queue);
467 assert(!m->dbus_job_queue);
468 assert(!m->cleanup_queue);
469 assert(!m->gc_queue);
470
964e0949
LP
471 assert(hashmap_isempty(m->jobs));
472 assert(hashmap_isempty(m->units));
a16e1123
LP
473}
474
475void manager_free(Manager *m) {
476 UnitType c;
87f0e418 477
a16e1123
LP
478 assert(m);
479
480 manager_clear_jobs_and_units(m);
23a177ef 481
7824bbeb
LP
482 for (c = 0; c < _UNIT_TYPE_MAX; c++)
483 if (unit_vtable[c]->shutdown)
484 unit_vtable[c]->shutdown(m);
485
a16e1123
LP
486 /* If we reexecute ourselves, we keep the root cgroup
487 * around */
c6c18be3 488 manager_shutdown_cgroup(m, m->exit_code != MANAGER_REEXECUTE);
8e274523 489
5a1e9937
LP
490 manager_undo_generators(m);
491
5e8d1c9a 492 bus_done(m);
ea430986 493
87f0e418 494 hashmap_free(m->units);
60918275 495 hashmap_free(m->jobs);
9152c765 496 hashmap_free(m->watch_pids);
05e343b7 497 hashmap_free(m->watch_bus);
9152c765
LP
498
499 if (m->epoll_fd >= 0)
a16e1123 500 close_nointr_nofail(m->epoll_fd);
acbb0225 501 if (m->signal_watch.fd >= 0)
a16e1123 502 close_nointr_nofail(m->signal_watch.fd);
8c47c732
LP
503 if (m->notify_watch.fd >= 0)
504 close_nointr_nofail(m->notify_watch.fd);
60918275 505
4927fcae
LP
506#ifdef HAVE_AUDIT
507 if (m->audit_fd >= 0)
508 audit_close(m->audit_fd);
509#endif
510
c952c6ec
LP
511 free(m->notify_socket);
512
84e3543e 513 lookup_paths_free(&m->lookup_paths);
1137a57c 514 strv_free(m->environment);
036643a2 515
06d4c99a
LP
516 strv_free(m->default_controllers);
517
8e274523 518 hashmap_free(m->cgroup_bondings);
c6c18be3 519 set_free_free(m->unit_path_cache);
33be102a 520
60918275
LP
521 free(m);
522}
523
a16e1123
LP
524int manager_enumerate(Manager *m) {
525 int r = 0, q;
f50e0a01 526 UnitType c;
f50e0a01
LP
527
528 assert(m);
529
a16e1123
LP
530 /* Let's ask every type to load all units from disk/kernel
531 * that it might know */
f50e0a01
LP
532 for (c = 0; c < _UNIT_TYPE_MAX; c++)
533 if (unit_vtable[c]->enumerate)
a16e1123
LP
534 if ((q = unit_vtable[c]->enumerate(m)) < 0)
535 r = q;
f50e0a01
LP
536
537 manager_dispatch_load_queue(m);
a16e1123
LP
538 return r;
539}
540
541int manager_coldplug(Manager *m) {
542 int r = 0, q;
543 Iterator i;
544 Unit *u;
545 char *k;
546
547 assert(m);
f50e0a01
LP
548
549 /* Then, let's set up their initial state. */
550 HASHMAP_FOREACH_KEY(u, k, m->units, i) {
551
552 /* ignore aliases */
ac155bb8 553 if (u->id != k)
f50e0a01
LP
554 continue;
555
cca098b0
LP
556 if ((q = unit_coldplug(u)) < 0)
557 r = q;
f50e0a01
LP
558 }
559
a16e1123
LP
560 return r;
561}
562
fe51822e
LP
563static void manager_build_unit_path_cache(Manager *m) {
564 char **i;
565 DIR *d = NULL;
566 int r;
567
568 assert(m);
569
570 set_free_free(m->unit_path_cache);
571
572 if (!(m->unit_path_cache = set_new(string_hash_func, string_compare_func))) {
573 log_error("Failed to allocate unit path cache.");
574 return;
575 }
576
577 /* This simply builds a list of files we know exist, so that
578 * we don't always have to go to disk */
579
580 STRV_FOREACH(i, m->lookup_paths.unit_path) {
581 struct dirent *de;
582
583 if (!(d = opendir(*i))) {
584 log_error("Failed to open directory: %m");
585 continue;
586 }
587
588 while ((de = readdir(d))) {
589 char *p;
590
591 if (ignore_file(de->d_name))
592 continue;
593
44d91056
LP
594 p = join(streq(*i, "/") ? "" : *i, "/", de->d_name, NULL);
595 if (!p) {
fe51822e
LP
596 r = -ENOMEM;
597 goto fail;
598 }
599
600 if ((r = set_put(m->unit_path_cache, p)) < 0) {
601 free(p);
602 goto fail;
603 }
604 }
605
606 closedir(d);
607 d = NULL;
608 }
609
610 return;
611
612fail:
613 log_error("Failed to build unit path cache: %s", strerror(-r));
614
615 set_free_free(m->unit_path_cache);
616 m->unit_path_cache = NULL;
617
618 if (d)
619 closedir(d);
620}
621
a16e1123
LP
622int manager_startup(Manager *m, FILE *serialization, FDSet *fds) {
623 int r, q;
624
625 assert(m);
626
5a1e9937
LP
627 manager_run_generators(m);
628
fe51822e
LP
629 manager_build_unit_path_cache(m);
630
9f611ad8
LP
631 /* If we will deserialize make sure that during enumeration
632 * this is already known, so we increase the counter here
633 * already */
634 if (serialization)
a7556052 635 m->n_reloading ++;
9f611ad8 636
a16e1123
LP
637 /* First, enumerate what we can from all config files */
638 r = manager_enumerate(m);
639
640 /* Second, deserialize if there is something to deserialize */
641 if (serialization)
642 if ((q = manager_deserialize(m, serialization, fds)) < 0)
643 r = q;
644
645 /* Third, fire things up! */
646 if ((q = manager_coldplug(m)) < 0)
647 r = q;
648
9f611ad8 649 if (serialization) {
a7556052
LP
650 assert(m->n_reloading > 0);
651 m->n_reloading --;
9f611ad8
LP
652 }
653
a16e1123 654 return r;
f50e0a01
LP
655}
656
398ef8ba 657int manager_add_job(Manager *m, JobType type, Unit *unit, JobMode mode, bool override, DBusError *e, Job **_ret) {
e5b5ae50
LP
658 int r;
659 Job *ret;
7527cb52 660 Transaction *tr;
e5b5ae50
LP
661
662 assert(m);
663 assert(type < _JOB_TYPE_MAX);
87f0e418 664 assert(unit);
e5b5ae50 665 assert(mode < _JOB_MODE_MAX);
60918275 666
398ef8ba
LP
667 if (mode == JOB_ISOLATE && type != JOB_START) {
668 dbus_set_error(e, BUS_ERROR_INVALID_JOB_MODE, "Isolate is only valid for start.");
c497c7a9 669 return -EINVAL;
398ef8ba 670 }
c497c7a9 671
ac155bb8 672 if (mode == JOB_ISOLATE && !unit->allow_isolate) {
2528a7a6
LP
673 dbus_set_error(e, BUS_ERROR_NO_ISOLATION, "Operation refused, unit may not be isolated.");
674 return -EPERM;
675 }
676
ac155bb8 677 log_debug("Trying to enqueue job %s/%s/%s", unit->id, job_type_to_string(type), job_mode_to_string(mode));
9f04bd52 678
7527cb52
MS
679 tr = transaction_new();
680 if (!tr)
681 return -ENOMEM;
11dd41ce 682
7527cb52
MS
683 r = transaction_add_job_and_dependencies(tr, type, unit, NULL, true, override, false,
684 mode == JOB_IGNORE_DEPENDENCIES || mode == JOB_IGNORE_REQUIREMENTS,
685 mode == JOB_IGNORE_DEPENDENCIES, e, &ret);
686 if (r < 0)
687 goto tr_abort;
c497c7a9 688
7527cb52
MS
689 if (mode == JOB_ISOLATE) {
690 r = transaction_add_isolate_jobs(tr, m);
691 if (r < 0)
692 goto tr_abort;
693 }
694
695 r = transaction_activate(tr, m, mode, e);
696 if (r < 0)
697 goto tr_abort;
e5b5ae50 698
ac155bb8 699 log_debug("Enqueued job %s/%s as %u", unit->id, job_type_to_string(type), (unsigned) ret->id);
f50e0a01 700
e5b5ae50
LP
701 if (_ret)
702 *_ret = ret;
60918275 703
7527cb52 704 transaction_free(tr);
e5b5ae50 705 return 0;
7527cb52
MS
706
707tr_abort:
708 transaction_abort(tr);
709 transaction_free(tr);
710 return r;
e5b5ae50 711}
60918275 712
398ef8ba 713int manager_add_job_by_name(Manager *m, JobType type, const char *name, JobMode mode, bool override, DBusError *e, Job **_ret) {
28247076
LP
714 Unit *unit;
715 int r;
716
717 assert(m);
718 assert(type < _JOB_TYPE_MAX);
719 assert(name);
720 assert(mode < _JOB_MODE_MAX);
721
398ef8ba 722 if ((r = manager_load_unit(m, name, NULL, NULL, &unit)) < 0)
28247076
LP
723 return r;
724
398ef8ba 725 return manager_add_job(m, type, unit, mode, override, e, _ret);
28247076
LP
726}
727
60918275
LP
728Job *manager_get_job(Manager *m, uint32_t id) {
729 assert(m);
730
731 return hashmap_get(m->jobs, UINT32_TO_PTR(id));
732}
733
87f0e418 734Unit *manager_get_unit(Manager *m, const char *name) {
60918275
LP
735 assert(m);
736 assert(name);
737
87f0e418 738 return hashmap_get(m->units, name);
60918275
LP
739}
740
c1e1601e 741unsigned manager_dispatch_load_queue(Manager *m) {
595ed347 742 Unit *u;
c1e1601e 743 unsigned n = 0;
60918275
LP
744
745 assert(m);
746
223dabab
LP
747 /* Make sure we are not run recursively */
748 if (m->dispatching_load_queue)
c1e1601e 749 return 0;
223dabab
LP
750
751 m->dispatching_load_queue = true;
752
87f0e418 753 /* Dispatches the load queue. Takes a unit from the queue and
60918275
LP
754 * tries to load its data until the queue is empty */
755
595ed347
MS
756 while ((u = m->load_queue)) {
757 assert(u->in_load_queue);
034c6ed7 758
595ed347 759 unit_load(u);
c1e1601e 760 n++;
60918275
LP
761 }
762
223dabab 763 m->dispatching_load_queue = false;
c1e1601e 764 return n;
60918275
LP
765}
766
398ef8ba 767int manager_load_unit_prepare(Manager *m, const char *name, const char *path, DBusError *e, Unit **_ret) {
87f0e418 768 Unit *ret;
7d17cfbc 769 UnitType t;
60918275
LP
770 int r;
771
772 assert(m);
9e2f7c11 773 assert(name || path);
60918275 774
db06e3b6
LP
775 /* This will prepare the unit for loading, but not actually
776 * load anything from disk. */
0301abf4 777
398ef8ba
LP
778 if (path && !is_path(path)) {
779 dbus_set_error(e, BUS_ERROR_INVALID_PATH, "Path %s is not absolute.", path);
9e2f7c11 780 return -EINVAL;
398ef8ba 781 }
9e2f7c11
LP
782
783 if (!name)
784 name = file_name_from_path(path);
785
7d17cfbc
MS
786 t = unit_name_to_type(name);
787
788 if (t == _UNIT_TYPE_INVALID || !unit_name_is_valid_no_type(name, false)) {
398ef8ba 789 dbus_set_error(e, BUS_ERROR_INVALID_NAME, "Unit name %s is not valid.", name);
9e2f7c11 790 return -EINVAL;
398ef8ba 791 }
60918275 792
7d17cfbc
MS
793 ret = manager_get_unit(m, name);
794 if (ret) {
034c6ed7 795 *_ret = ret;
413d6313 796 return 1;
034c6ed7 797 }
60918275 798
7d17cfbc
MS
799 ret = unit_new(m, unit_vtable[t]->object_size);
800 if (!ret)
60918275
LP
801 return -ENOMEM;
802
7d17cfbc 803 if (path) {
ac155bb8
MS
804 ret->fragment_path = strdup(path);
805 if (!ret->fragment_path) {
0301abf4
LP
806 unit_free(ret);
807 return -ENOMEM;
808 }
7d17cfbc 809 }
0301abf4 810
87f0e418
LP
811 if ((r = unit_add_name(ret, name)) < 0) {
812 unit_free(ret);
1ffba6fe 813 return r;
60918275
LP
814 }
815
87f0e418 816 unit_add_to_load_queue(ret);
c1e1601e 817 unit_add_to_dbus_queue(ret);
949061f0 818 unit_add_to_gc_queue(ret);
c1e1601e 819
db06e3b6
LP
820 if (_ret)
821 *_ret = ret;
822
823 return 0;
824}
825
398ef8ba 826int manager_load_unit(Manager *m, const char *name, const char *path, DBusError *e, Unit **_ret) {
db06e3b6
LP
827 int r;
828
829 assert(m);
830
831 /* This will load the service information files, but not actually
832 * start any services or anything. */
833
398ef8ba 834 if ((r = manager_load_unit_prepare(m, name, path, e, _ret)) != 0)
db06e3b6
LP
835 return r;
836
f50e0a01 837 manager_dispatch_load_queue(m);
60918275 838
9e2f7c11 839 if (_ret)
413d6313 840 *_ret = unit_follow_merge(*_ret);
9e2f7c11 841
60918275
LP
842 return 0;
843}
a66d02c3 844
cea8e32e 845void manager_dump_jobs(Manager *s, FILE *f, const char *prefix) {
034c6ed7 846 Iterator i;
a66d02c3
LP
847 Job *j;
848
849 assert(s);
850 assert(f);
851
034c6ed7 852 HASHMAP_FOREACH(j, s->jobs, i)
cea8e32e 853 job_dump(j, f, prefix);
a66d02c3
LP
854}
855
87f0e418 856void manager_dump_units(Manager *s, FILE *f, const char *prefix) {
034c6ed7 857 Iterator i;
87f0e418 858 Unit *u;
11dd41ce 859 const char *t;
a66d02c3
LP
860
861 assert(s);
862 assert(f);
863
87f0e418 864 HASHMAP_FOREACH_KEY(u, t, s->units, i)
ac155bb8 865 if (u->id == t)
87f0e418 866 unit_dump(u, f, prefix);
a66d02c3 867}
7fad411c
LP
868
869void manager_clear_jobs(Manager *m) {
870 Job *j;
871
872 assert(m);
873
7fad411c 874 while ((j = hashmap_first(m->jobs)))
c77bc38d 875 job_finish_and_invalidate(j, JOB_CANCELED);
7fad411c 876}
83c60c9f 877
c1e1601e 878unsigned manager_dispatch_run_queue(Manager *m) {
83c60c9f 879 Job *j;
c1e1601e 880 unsigned n = 0;
83c60c9f 881
034c6ed7 882 if (m->dispatching_run_queue)
c1e1601e 883 return 0;
034c6ed7
LP
884
885 m->dispatching_run_queue = true;
9152c765 886
034c6ed7 887 while ((j = m->run_queue)) {
ac1135be 888 assert(j->installed);
034c6ed7
LP
889 assert(j->in_run_queue);
890
891 job_run_and_invalidate(j);
c1e1601e 892 n++;
9152c765 893 }
034c6ed7
LP
894
895 m->dispatching_run_queue = false;
c1e1601e
LP
896 return n;
897}
898
899unsigned manager_dispatch_dbus_queue(Manager *m) {
900 Job *j;
595ed347 901 Unit *u;
c1e1601e
LP
902 unsigned n = 0;
903
904 assert(m);
905
906 if (m->dispatching_dbus_queue)
907 return 0;
908
909 m->dispatching_dbus_queue = true;
910
595ed347
MS
911 while ((u = m->dbus_unit_queue)) {
912 assert(u->in_dbus_queue);
c1e1601e 913
595ed347 914 bus_unit_send_change_signal(u);
c1e1601e
LP
915 n++;
916 }
917
918 while ((j = m->dbus_job_queue)) {
919 assert(j->in_dbus_queue);
920
921 bus_job_send_change_signal(j);
922 n++;
923 }
924
925 m->dispatching_dbus_queue = false;
926 return n;
9152c765
LP
927}
928
8c47c732
LP
929static int manager_process_notify_fd(Manager *m) {
930 ssize_t n;
931
932 assert(m);
933
934 for (;;) {
935 char buf[4096];
936 struct msghdr msghdr;
937 struct iovec iovec;
938 struct ucred *ucred;
939 union {
940 struct cmsghdr cmsghdr;
941 uint8_t buf[CMSG_SPACE(sizeof(struct ucred))];
942 } control;
943 Unit *u;
944 char **tags;
945
946 zero(iovec);
947 iovec.iov_base = buf;
948 iovec.iov_len = sizeof(buf)-1;
949
950 zero(control);
951 zero(msghdr);
952 msghdr.msg_iov = &iovec;
953 msghdr.msg_iovlen = 1;
954 msghdr.msg_control = &control;
955 msghdr.msg_controllen = sizeof(control);
956
957 if ((n = recvmsg(m->notify_watch.fd, &msghdr, MSG_DONTWAIT)) <= 0) {
958 if (n >= 0)
959 return -EIO;
960
f6144808 961 if (errno == EAGAIN || errno == EINTR)
8c47c732
LP
962 break;
963
964 return -errno;
965 }
966
967 if (msghdr.msg_controllen < CMSG_LEN(sizeof(struct ucred)) ||
968 control.cmsghdr.cmsg_level != SOL_SOCKET ||
969 control.cmsghdr.cmsg_type != SCM_CREDENTIALS ||
970 control.cmsghdr.cmsg_len != CMSG_LEN(sizeof(struct ucred))) {
971 log_warning("Received notify message without credentials. Ignoring.");
972 continue;
973 }
974
975 ucred = (struct ucred*) CMSG_DATA(&control.cmsghdr);
976
c6c18be3 977 if (!(u = hashmap_get(m->watch_pids, LONG_TO_PTR(ucred->pid))))
8c47c732
LP
978 if (!(u = cgroup_unit_by_pid(m, ucred->pid))) {
979 log_warning("Cannot find unit for notify message of PID %lu.", (unsigned long) ucred->pid);
980 continue;
981 }
982
8c40acf7
LP
983 assert((size_t) n < sizeof(buf));
984 buf[n] = 0;
8c47c732
LP
985 if (!(tags = strv_split(buf, "\n\r")))
986 return -ENOMEM;
987
ac155bb8 988 log_debug("Got notification message for unit %s", u->id);
8c47c732
LP
989
990 if (UNIT_VTABLE(u)->notify_message)
c952c6ec 991 UNIT_VTABLE(u)->notify_message(u, ucred->pid, tags);
8c47c732
LP
992
993 strv_free(tags);
994 }
995
996 return 0;
997}
998
034c6ed7 999static int manager_dispatch_sigchld(Manager *m) {
9152c765
LP
1000 assert(m);
1001
1002 for (;;) {
1003 siginfo_t si;
87f0e418 1004 Unit *u;
8c47c732 1005 int r;
9152c765
LP
1006
1007 zero(si);
4112df16
LP
1008
1009 /* First we call waitd() for a PID and do not reap the
1010 * zombie. That way we can still access /proc/$PID for
1011 * it while it is a zombie. */
1012 if (waitid(P_ALL, 0, &si, WEXITED|WNOHANG|WNOWAIT) < 0) {
acbb0225
LP
1013
1014 if (errno == ECHILD)
1015 break;
1016
4112df16
LP
1017 if (errno == EINTR)
1018 continue;
1019
9152c765 1020 return -errno;
acbb0225 1021 }
9152c765 1022
4112df16 1023 if (si.si_pid <= 0)
9152c765
LP
1024 break;
1025
15d5d9d9 1026 if (si.si_code == CLD_EXITED || si.si_code == CLD_KILLED || si.si_code == CLD_DUMPED) {
4112df16
LP
1027 char *name = NULL;
1028
87d2c1ff 1029 get_process_comm(si.si_pid, &name);
bb00e604 1030 log_debug("Got SIGCHLD for process %lu (%s)", (unsigned long) si.si_pid, strna(name));
4112df16
LP
1031 free(name);
1032 }
1033
8c47c732
LP
1034 /* Let's flush any message the dying child might still
1035 * have queued for us. This ensures that the process
1036 * still exists in /proc so that we can figure out
1037 * which cgroup and hence unit it belongs to. */
1038 if ((r = manager_process_notify_fd(m)) < 0)
1039 return r;
1040
1041 /* And now figure out the unit this belongs to */
c6c18be3 1042 if (!(u = hashmap_get(m->watch_pids, LONG_TO_PTR(si.si_pid))))
8c47c732
LP
1043 u = cgroup_unit_by_pid(m, si.si_pid);
1044
4112df16
LP
1045 /* And now, we actually reap the zombie. */
1046 if (waitid(P_PID, si.si_pid, &si, WEXITED) < 0) {
1047 if (errno == EINTR)
1048 continue;
1049
1050 return -errno;
1051 }
1052
034c6ed7
LP
1053 if (si.si_code != CLD_EXITED && si.si_code != CLD_KILLED && si.si_code != CLD_DUMPED)
1054 continue;
1055
bb00e604
LP
1056 log_debug("Child %lu died (code=%s, status=%i/%s)",
1057 (long unsigned) si.si_pid,
4112df16
LP
1058 sigchld_code_to_string(si.si_code),
1059 si.si_status,
d06dacd0
LP
1060 strna(si.si_code == CLD_EXITED
1061 ? exit_status_to_string(si.si_status, EXIT_STATUS_FULL)
1062 : signal_to_string(si.si_status)));
acbb0225 1063
8c47c732 1064 if (!u)
9152c765
LP
1065 continue;
1066
ac155bb8 1067 log_debug("Child %lu belongs to %s", (long unsigned) si.si_pid, u->id);
6c1a0478 1068
c6c18be3 1069 hashmap_remove(m->watch_pids, LONG_TO_PTR(si.si_pid));
87f0e418 1070 UNIT_VTABLE(u)->sigchld_event(u, si.si_pid, si.si_code, si.si_status);
9152c765
LP
1071 }
1072
1073 return 0;
1074}
1075
7d793605 1076static int manager_start_target(Manager *m, const char *name, JobMode mode) {
28247076 1077 int r;
398ef8ba
LP
1078 DBusError error;
1079
1080 dbus_error_init(&error);
1081
af2d49f7 1082 log_debug("Activating special unit %s", name);
1e001f52 1083
398ef8ba
LP
1084 if ((r = manager_add_job_by_name(m, JOB_START, name, mode, true, &error, NULL)) < 0)
1085 log_error("Failed to enqueue %s job: %s", name, bus_error(&error, r));
28247076 1086
398ef8ba 1087 dbus_error_free(&error);
a1b256b0
LP
1088
1089 return r;
28247076
LP
1090}
1091
a16e1123 1092static int manager_process_signal_fd(Manager *m) {
9152c765
LP
1093 ssize_t n;
1094 struct signalfd_siginfo sfsi;
1095 bool sigchld = false;
1096
1097 assert(m);
1098
1099 for (;;) {
acbb0225 1100 if ((n = read(m->signal_watch.fd, &sfsi, sizeof(sfsi))) != sizeof(sfsi)) {
9152c765
LP
1101
1102 if (n >= 0)
1103 return -EIO;
1104
63090775 1105 if (errno == EINTR || errno == EAGAIN)
acbb0225 1106 break;
9152c765
LP
1107
1108 return -errno;
1109 }
1110
67370238
LP
1111 if (sfsi.ssi_pid > 0) {
1112 char *p = NULL;
1113
87d2c1ff 1114 get_process_comm(sfsi.ssi_pid, &p);
dfa7f7e1 1115
67370238 1116 log_debug("Received SIG%s from PID %lu (%s).",
4e240ab0 1117 signal_to_string(sfsi.ssi_signo),
67370238
LP
1118 (unsigned long) sfsi.ssi_pid, strna(p));
1119 free(p);
1120 } else
4e240ab0 1121 log_debug("Received SIG%s.", signal_to_string(sfsi.ssi_signo));
1e001f52 1122
b9cd2ec1
LP
1123 switch (sfsi.ssi_signo) {
1124
4112df16 1125 case SIGCHLD:
9152c765 1126 sigchld = true;
b9cd2ec1
LP
1127 break;
1128
6632c602 1129 case SIGTERM:
a3d4e06d 1130 if (m->running_as == MANAGER_SYSTEM) {
db06e3b6
LP
1131 /* This is for compatibility with the
1132 * original sysvinit */
e11dc4a2 1133 m->exit_code = MANAGER_REEXECUTE;
a1b256b0
LP
1134 break;
1135 }
84e9af1e 1136
a1b256b0 1137 /* Fall through */
e11dc4a2
LP
1138
1139 case SIGINT:
a3d4e06d 1140 if (m->running_as == MANAGER_SYSTEM) {
7d793605 1141 manager_start_target(m, SPECIAL_CTRL_ALT_DEL_TARGET, JOB_REPLACE);
84e9af1e
LP
1142 break;
1143 }
1144
a1b256b0 1145 /* Run the exit target if there is one, if not, just exit. */
0003d1ab 1146 if (manager_start_target(m, SPECIAL_EXIT_TARGET, JOB_REPLACE) < 0) {
a1b256b0
LP
1147 m->exit_code = MANAGER_EXIT;
1148 return 0;
1149 }
1150
1151 break;
84e9af1e 1152
28247076 1153 case SIGWINCH:
a3d4e06d 1154 if (m->running_as == MANAGER_SYSTEM)
7d793605 1155 manager_start_target(m, SPECIAL_KBREQUEST_TARGET, JOB_REPLACE);
84e9af1e 1156
28247076
LP
1157 /* This is a nop on non-init */
1158 break;
84e9af1e 1159
28247076 1160 case SIGPWR:
a3d4e06d 1161 if (m->running_as == MANAGER_SYSTEM)
7d793605 1162 manager_start_target(m, SPECIAL_SIGPWR_TARGET, JOB_REPLACE);
84e9af1e 1163
28247076 1164 /* This is a nop on non-init */
84e9af1e 1165 break;
6632c602 1166
1005d14f 1167 case SIGUSR1: {
57ee42ce
LP
1168 Unit *u;
1169
1170 u = manager_get_unit(m, SPECIAL_DBUS_SERVICE);
1171
1172 if (!u || UNIT_IS_ACTIVE_OR_RELOADING(unit_active_state(u))) {
1173 log_info("Trying to reconnect to bus...");
3996fbe2 1174 bus_init(m, true);
57ee42ce
LP
1175 }
1176
1177 if (!u || !UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(u))) {
1178 log_info("Loading D-Bus service...");
7d793605 1179 manager_start_target(m, SPECIAL_DBUS_SERVICE, JOB_REPLACE);
57ee42ce
LP
1180 }
1181
1182 break;
1183 }
1184
2149e37c
LP
1185 case SIGUSR2: {
1186 FILE *f;
1187 char *dump = NULL;
1188 size_t size;
1189
1190 if (!(f = open_memstream(&dump, &size))) {
1191 log_warning("Failed to allocate memory stream.");
1192 break;
1193 }
1194
1195 manager_dump_units(m, f, "\t");
1196 manager_dump_jobs(m, f, "\t");
1197
1198 if (ferror(f)) {
1199 fclose(f);
1200 free(dump);
1201 log_warning("Failed to write status stream");
1202 break;
1203 }
1204
1205 fclose(f);
1206 log_dump(LOG_INFO, dump);
1207 free(dump);
1208
1005d14f 1209 break;
2149e37c 1210 }
1005d14f 1211
a16e1123
LP
1212 case SIGHUP:
1213 m->exit_code = MANAGER_RELOAD;
1214 break;
1215
7d793605 1216 default: {
253ee27a 1217
0003d1ab
LP
1218 /* Starting SIGRTMIN+0 */
1219 static const char * const target_table[] = {
7d793605
LP
1220 [0] = SPECIAL_DEFAULT_TARGET,
1221 [1] = SPECIAL_RESCUE_TARGET,
f057408c 1222 [2] = SPECIAL_EMERGENCY_TARGET,
7d793605
LP
1223 [3] = SPECIAL_HALT_TARGET,
1224 [4] = SPECIAL_POWEROFF_TARGET,
0003d1ab
LP
1225 [5] = SPECIAL_REBOOT_TARGET,
1226 [6] = SPECIAL_KEXEC_TARGET
1227 };
1228
1229 /* Starting SIGRTMIN+13, so that target halt and system halt are 10 apart */
1230 static const ManagerExitCode code_table[] = {
1231 [0] = MANAGER_HALT,
1232 [1] = MANAGER_POWEROFF,
1233 [2] = MANAGER_REBOOT,
1234 [3] = MANAGER_KEXEC
7d793605
LP
1235 };
1236
1237 if ((int) sfsi.ssi_signo >= SIGRTMIN+0 &&
0003d1ab 1238 (int) sfsi.ssi_signo < SIGRTMIN+(int) ELEMENTSOF(target_table)) {
764e9b5f
MS
1239 int idx = (int) sfsi.ssi_signo - SIGRTMIN;
1240 manager_start_target(m, target_table[idx],
1241 (idx == 1 || idx == 2) ? JOB_ISOLATE : JOB_REPLACE);
7d793605
LP
1242 break;
1243 }
1244
0003d1ab
LP
1245 if ((int) sfsi.ssi_signo >= SIGRTMIN+13 &&
1246 (int) sfsi.ssi_signo < SIGRTMIN+13+(int) ELEMENTSOF(code_table)) {
1247 m->exit_code = code_table[sfsi.ssi_signo - SIGRTMIN - 13];
1248 break;
1249 }
1250
0658666b
LP
1251 switch (sfsi.ssi_signo - SIGRTMIN) {
1252
1253 case 20:
1254 log_debug("Enabling showing of status.");
27d340c7 1255 manager_set_show_status(m, true);
0658666b
LP
1256 break;
1257
1258 case 21:
1259 log_debug("Disabling showing of status.");
27d340c7 1260 manager_set_show_status(m, false);
0658666b
LP
1261 break;
1262
253ee27a
LP
1263 case 22:
1264 log_set_max_level(LOG_DEBUG);
1265 log_notice("Setting log level to debug.");
1266 break;
1267
1268 case 23:
1269 log_set_max_level(LOG_INFO);
1270 log_notice("Setting log level to info.");
1271 break;
1272
4cfa2c99
LP
1273 case 26:
1274 log_set_target(LOG_TARGET_JOURNAL_OR_KMSG);
1275 log_notice("Setting log target to journal-or-kmsg.");
1276 break;
1277
253ee27a
LP
1278 case 27:
1279 log_set_target(LOG_TARGET_CONSOLE);
1280 log_notice("Setting log target to console.");
1281 break;
1282
1283 case 28:
1284 log_set_target(LOG_TARGET_KMSG);
1285 log_notice("Setting log target to kmsg.");
1286 break;
1287
1288 case 29:
1289 log_set_target(LOG_TARGET_SYSLOG_OR_KMSG);
1290 log_notice("Setting log target to syslog-or-kmsg.");
1291 break;
1292
0658666b 1293 default:
4e240ab0 1294 log_warning("Got unhandled signal <%s>.", signal_to_string(sfsi.ssi_signo));
0658666b 1295 }
b9cd2ec1 1296 }
7d793605 1297 }
9152c765
LP
1298 }
1299
1300 if (sigchld)
034c6ed7
LP
1301 return manager_dispatch_sigchld(m);
1302
1303 return 0;
1304}
1305
a16e1123 1306static int process_event(Manager *m, struct epoll_event *ev) {
034c6ed7 1307 int r;
acbb0225 1308 Watch *w;
034c6ed7
LP
1309
1310 assert(m);
1311 assert(ev);
1312
3df5bf61 1313 assert_se(w = ev->data.ptr);
034c6ed7 1314
40dde66f
LP
1315 if (w->type == WATCH_INVALID)
1316 return 0;
1317
acbb0225 1318 switch (w->type) {
034c6ed7 1319
ef734fd6 1320 case WATCH_SIGNAL:
034c6ed7 1321
acbb0225 1322 /* An incoming signal? */
f94ea366 1323 if (ev->events != EPOLLIN)
acbb0225 1324 return -EINVAL;
034c6ed7 1325
a16e1123 1326 if ((r = manager_process_signal_fd(m)) < 0)
acbb0225 1327 return r;
034c6ed7 1328
acbb0225 1329 break;
034c6ed7 1330
8c47c732
LP
1331 case WATCH_NOTIFY:
1332
1333 /* An incoming daemon notification event? */
1334 if (ev->events != EPOLLIN)
1335 return -EINVAL;
1336
1337 if ((r = manager_process_notify_fd(m)) < 0)
1338 return r;
1339
1340 break;
1341
acbb0225 1342 case WATCH_FD:
034c6ed7 1343
acbb0225 1344 /* Some fd event, to be dispatched to the units */
ea430986 1345 UNIT_VTABLE(w->data.unit)->fd_event(w->data.unit, w->fd, ev->events, w);
acbb0225 1346 break;
034c6ed7 1347
faf919f1
LP
1348 case WATCH_UNIT_TIMER:
1349 case WATCH_JOB_TIMER: {
acbb0225
LP
1350 uint64_t v;
1351 ssize_t k;
034c6ed7 1352
acbb0225 1353 /* Some timer event, to be dispatched to the units */
be888ebb 1354 if ((k = read(w->fd, &v, sizeof(v))) != sizeof(v)) {
034c6ed7 1355
acbb0225
LP
1356 if (k < 0 && (errno == EINTR || errno == EAGAIN))
1357 break;
034c6ed7 1358
acbb0225 1359 return k < 0 ? -errno : -EIO;
034c6ed7
LP
1360 }
1361
faf919f1
LP
1362 if (w->type == WATCH_UNIT_TIMER)
1363 UNIT_VTABLE(w->data.unit)->timer_event(w->data.unit, v, w);
1364 else
1365 job_timer_event(w->data.job, v, w);
acbb0225
LP
1366 break;
1367 }
1368
ef734fd6
LP
1369 case WATCH_MOUNT:
1370 /* Some mount table change, intended for the mount subsystem */
1371 mount_fd_event(m, ev->events);
1372 break;
1373
4e434314
LP
1374 case WATCH_SWAP:
1375 /* Some swap table change, intended for the swap subsystem */
1376 swap_fd_event(m, ev->events);
1377 break;
1378
f94ea366
LP
1379 case WATCH_UDEV:
1380 /* Some notification from udev, intended for the device subsystem */
1381 device_fd_event(m, ev->events);
1382 break;
1383
ea430986
LP
1384 case WATCH_DBUS_WATCH:
1385 bus_watch_event(m, w, ev->events);
1386 break;
1387
1388 case WATCH_DBUS_TIMEOUT:
1389 bus_timeout_event(m, w, ev->events);
1390 break;
1391
acbb0225 1392 default:
c5fd1e57 1393 log_error("event type=%i", w->type);
acbb0225 1394 assert_not_reached("Unknown epoll event type.");
034c6ed7 1395 }
9152c765
LP
1396
1397 return 0;
1398}
1399
1400int manager_loop(Manager *m) {
1401 int r;
9152c765 1402
fac9f8df 1403 RATELIMIT_DEFINE(rl, 1*USEC_PER_SEC, 50000);
ea430986 1404
9152c765 1405 assert(m);
a16e1123 1406 m->exit_code = MANAGER_RUNNING;
9152c765 1407
fe51822e
LP
1408 /* Release the path cache */
1409 set_free_free(m->unit_path_cache);
1410 m->unit_path_cache = NULL;
1411
b0c918b9
LP
1412 manager_check_finished(m);
1413
a4312405
LP
1414 /* There might still be some zombies hanging around from
1415 * before we were exec()'ed. Leat's reap them */
e96d6be7
LP
1416 r = manager_dispatch_sigchld(m);
1417 if (r < 0)
a4312405
LP
1418 return r;
1419
a16e1123 1420 while (m->exit_code == MANAGER_RUNNING) {
957ca890
LP
1421 struct epoll_event event;
1422 int n;
c757a65b 1423 int wait_msec = -1;
9152c765 1424
c757a65b 1425 if (m->runtime_watchdog > 0 && m->running_as == MANAGER_SYSTEM)
e96d6be7
LP
1426 watchdog_ping();
1427
ea430986
LP
1428 if (!ratelimit_test(&rl)) {
1429 /* Yay, something is going seriously wrong, pause a little */
1430 log_warning("Looping too fast. Throttling execution a little.");
1431 sleep(1);
e96d6be7 1432 continue;
ea430986
LP
1433 }
1434
37a8e683 1435 if (manager_dispatch_load_queue(m) > 0)
23a177ef
LP
1436 continue;
1437
37a8e683 1438 if (manager_dispatch_run_queue(m) > 0)
701cc384
LP
1439 continue;
1440
37a8e683 1441 if (bus_dispatch(m) > 0)
c1e1601e 1442 continue;
034c6ed7 1443
37a8e683 1444 if (manager_dispatch_cleanup_queue(m) > 0)
c1e1601e
LP
1445 continue;
1446
37a8e683 1447 if (manager_dispatch_gc_queue(m) > 0)
c1e1601e
LP
1448 continue;
1449
1450 if (manager_dispatch_dbus_queue(m) > 0)
ea430986 1451 continue;
ea430986 1452
e04aad61
LP
1453 if (swap_dispatch_reload(m) > 0)
1454 continue;
1455
c757a65b
LP
1456 /* Sleep for half the watchdog time */
1457 if (m->runtime_watchdog > 0 && m->running_as == MANAGER_SYSTEM) {
1458 wait_msec = (int) (m->runtime_watchdog / 2 / USEC_PER_MSEC);
1459 if (wait_msec <= 0)
1460 wait_msec = 1;
1461 } else
1462 wait_msec = -1;
1463
e96d6be7
LP
1464 n = epoll_wait(m->epoll_fd, &event, 1, wait_msec);
1465 if (n < 0) {
9152c765 1466
6089f4a9 1467 if (errno == EINTR)
9152c765
LP
1468 continue;
1469
1470 return -errno;
e96d6be7
LP
1471 } else if (n == 0)
1472 continue;
9152c765 1473
957ca890 1474 assert(n == 1);
b9cd2ec1 1475
e96d6be7
LP
1476 r = process_event(m, &event);
1477 if (r < 0)
957ca890 1478 return r;
a16e1123 1479 }
957ca890 1480
a16e1123 1481 return m->exit_code;
83c60c9f 1482}
ea430986
LP
1483
1484int manager_get_unit_from_dbus_path(Manager *m, const char *s, Unit **_u) {
1485 char *n;
1486 Unit *u;
1487
1488 assert(m);
1489 assert(s);
1490 assert(_u);
1491
1492 if (!startswith(s, "/org/freedesktop/systemd1/unit/"))
1493 return -EINVAL;
1494
1495 if (!(n = bus_path_unescape(s+31)))
1496 return -ENOMEM;
1497
1498 u = manager_get_unit(m, n);
1499 free(n);
1500
1501 if (!u)
1502 return -ENOENT;
1503
1504 *_u = u;
1505
1506 return 0;
1507}
86fbf370
LP
1508
1509int manager_get_job_from_dbus_path(Manager *m, const char *s, Job **_j) {
1510 Job *j;
1511 unsigned id;
1512 int r;
1513
1514 assert(m);
1515 assert(s);
1516 assert(_j);
1517
1518 if (!startswith(s, "/org/freedesktop/systemd1/job/"))
1519 return -EINVAL;
1520
1521 if ((r = safe_atou(s + 30, &id)) < 0)
1522 return r;
1523
1524 if (!(j = manager_get_job(m, id)))
1525 return -ENOENT;
1526
1527 *_j = j;
1528
1529 return 0;
1530}
dfcd764e 1531
4927fcae 1532void manager_send_unit_audit(Manager *m, Unit *u, int type, bool success) {
e537352b 1533
4927fcae
LP
1534#ifdef HAVE_AUDIT
1535 char *p;
e537352b 1536
4927fcae 1537 if (m->audit_fd < 0)
e537352b
LP
1538 return;
1539
bbd3a7ba
LP
1540 /* Don't generate audit events if the service was already
1541 * started and we're just deserializing */
a7556052 1542 if (m->n_reloading > 0)
bbd3a7ba
LP
1543 return;
1544
f1dd0c3f
LP
1545 if (m->running_as != MANAGER_SYSTEM)
1546 return;
1547
ac155bb8 1548 if (u->type != UNIT_SERVICE)
f1dd0c3f
LP
1549 return;
1550
ac155bb8 1551 if (!(p = unit_name_to_prefix_and_instance(u->id))) {
4927fcae 1552 log_error("Failed to allocate unit name for audit message: %s", strerror(ENOMEM));
e537352b
LP
1553 return;
1554 }
1555
391ade86 1556 if (audit_log_user_comm_message(m->audit_fd, type, "", p, NULL, NULL, NULL, success) < 0) {
391ade86
LP
1557 if (errno == EPERM) {
1558 /* We aren't allowed to send audit messages?
44785992 1559 * Then let's not retry again. */
391ade86
LP
1560 audit_close(m->audit_fd);
1561 m->audit_fd = -1;
44785992
LP
1562 } else
1563 log_warning("Failed to send audit message: %m");
391ade86 1564 }
e537352b 1565
4927fcae
LP
1566 free(p);
1567#endif
e537352b 1568
e537352b
LP
1569}
1570
e983b760
LP
1571void manager_send_unit_plymouth(Manager *m, Unit *u) {
1572 int fd = -1;
1573 union sockaddr_union sa;
1574 int n = 0;
1575 char *message = NULL;
e983b760
LP
1576
1577 /* Don't generate plymouth events if the service was already
1578 * started and we're just deserializing */
a7556052 1579 if (m->n_reloading > 0)
e983b760
LP
1580 return;
1581
1582 if (m->running_as != MANAGER_SYSTEM)
1583 return;
1584
ac155bb8
MS
1585 if (u->type != UNIT_SERVICE &&
1586 u->type != UNIT_MOUNT &&
1587 u->type != UNIT_SWAP)
e983b760
LP
1588 return;
1589
1590 /* We set SOCK_NONBLOCK here so that we rather drop the
1591 * message then wait for plymouth */
1592 if ((fd = socket(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0)) < 0) {
1593 log_error("socket() failed: %m");
1594 return;
1595 }
1596
1597 zero(sa);
1598 sa.sa.sa_family = AF_UNIX;
96707269
LP
1599 strncpy(sa.un.sun_path+1, "/org/freedesktop/plymouthd", sizeof(sa.un.sun_path)-1);
1600 if (connect(fd, &sa.sa, offsetof(struct sockaddr_un, sun_path) + 1 + strlen(sa.un.sun_path+1)) < 0) {
e983b760
LP
1601
1602 if (errno != EPIPE &&
1603 errno != EAGAIN &&
1604 errno != ENOENT &&
1605 errno != ECONNREFUSED &&
1606 errno != ECONNRESET &&
1607 errno != ECONNABORTED)
1608 log_error("connect() failed: %m");
1609
1610 goto finish;
1611 }
1612
ac155bb8 1613 if (asprintf(&message, "U\002%c%s%n", (int) (strlen(u->id) + 1), u->id, &n) < 0) {
e983b760
LP
1614 log_error("Out of memory");
1615 goto finish;
1616 }
1617
1618 errno = 0;
bd40a2d8 1619 if (write(fd, message, n + 1) != n + 1) {
e983b760
LP
1620
1621 if (errno != EPIPE &&
1622 errno != EAGAIN &&
1623 errno != ENOENT &&
1624 errno != ECONNREFUSED &&
1625 errno != ECONNRESET &&
1626 errno != ECONNABORTED)
1627 log_error("Failed to write Plymouth message: %m");
1628
1629 goto finish;
1630 }
1631
1632finish:
1633 if (fd >= 0)
1634 close_nointr_nofail(fd);
1635
1636 free(message);
1637}
1638
05e343b7
LP
1639void manager_dispatch_bus_name_owner_changed(
1640 Manager *m,
1641 const char *name,
1642 const char* old_owner,
1643 const char *new_owner) {
1644
1645 Unit *u;
1646
1647 assert(m);
1648 assert(name);
1649
1650 if (!(u = hashmap_get(m->watch_bus, name)))
1651 return;
1652
1653 UNIT_VTABLE(u)->bus_name_owner_change(u, name, old_owner, new_owner);
1654}
1655
1656void manager_dispatch_bus_query_pid_done(
1657 Manager *m,
1658 const char *name,
1659 pid_t pid) {
1660
1661 Unit *u;
1662
1663 assert(m);
1664 assert(name);
1665 assert(pid >= 1);
1666
1667 if (!(u = hashmap_get(m->watch_bus, name)))
1668 return;
1669
1670 UNIT_VTABLE(u)->bus_query_pid_done(u, name, pid);
1671}
1672
d8d5ab98 1673int manager_open_serialization(Manager *m, FILE **_f) {
b925e726 1674 char *path = NULL;
a16e1123
LP
1675 mode_t saved_umask;
1676 int fd;
1677 FILE *f;
1678
1679 assert(_f);
1680
b925e726 1681 if (m->running_as == MANAGER_SYSTEM)
2b583ce6 1682 asprintf(&path, "/run/systemd/dump-%lu-XXXXXX", (unsigned long) getpid());
b925e726
LP
1683 else
1684 asprintf(&path, "/tmp/systemd-dump-%lu-XXXXXX", (unsigned long) getpid());
d8d5ab98 1685
b925e726
LP
1686 if (!path)
1687 return -ENOMEM;
a16e1123
LP
1688
1689 saved_umask = umask(0077);
1690 fd = mkostemp(path, O_RDWR|O_CLOEXEC);
1691 umask(saved_umask);
1692
1693 if (fd < 0) {
1694 free(path);
1695 return -errno;
1696 }
1697
1698 unlink(path);
1699
1700 log_debug("Serializing state to %s", path);
1701 free(path);
1702
da19d5c1 1703 if (!(f = fdopen(fd, "w+")))
a16e1123
LP
1704 return -errno;
1705
1706 *_f = f;
1707
1708 return 0;
1709}
1710
1711int manager_serialize(Manager *m, FILE *f, FDSet *fds) {
1712 Iterator i;
1713 Unit *u;
1714 const char *t;
1715 int r;
1716
1717 assert(m);
1718 assert(f);
1719 assert(fds);
1720
a7556052 1721 m->n_reloading ++;
38c52d46 1722
01d67b43
LP
1723 fprintf(f, "current-job-id=%i\n", m->current_job_id);
1724 fprintf(f, "taint-usr=%s\n", yes_no(m->taint_usr));
1725
e9ddabc2 1726 dual_timestamp_serialize(f, "initrd-timestamp", &m->initrd_timestamp);
10717a1a
LP
1727 dual_timestamp_serialize(f, "startup-timestamp", &m->startup_timestamp);
1728 dual_timestamp_serialize(f, "finish-timestamp", &m->finish_timestamp);
47a483a1 1729
f2382a94
LP
1730 fputc('\n', f);
1731
a16e1123 1732 HASHMAP_FOREACH_KEY(u, t, m->units, i) {
ac155bb8 1733 if (u->id != t)
a16e1123
LP
1734 continue;
1735
1736 if (!unit_can_serialize(u))
1737 continue;
1738
1739 /* Start marker */
ac155bb8 1740 fputs(u->id, f);
a16e1123
LP
1741 fputc('\n', f);
1742
38c52d46 1743 if ((r = unit_serialize(u, f, fds)) < 0) {
a7556052 1744 m->n_reloading --;
a16e1123 1745 return r;
38c52d46 1746 }
a16e1123
LP
1747 }
1748
a7556052
LP
1749 assert(m->n_reloading > 0);
1750 m->n_reloading --;
38c52d46 1751
a16e1123
LP
1752 if (ferror(f))
1753 return -EIO;
1754
b23de6af
LP
1755 r = bus_fdset_add_all(m, fds);
1756 if (r < 0)
1757 return r;
1758
a16e1123
LP
1759 return 0;
1760}
1761
1762int manager_deserialize(Manager *m, FILE *f, FDSet *fds) {
1763 int r = 0;
1764
1765 assert(m);
1766 assert(f);
1767
1768 log_debug("Deserializing state...");
1769
a7556052 1770 m->n_reloading ++;
82c64bf5 1771
10f8e83c 1772 for (;;) {
20c03b7b 1773 char line[LINE_MAX], *l;
10f8e83c
LP
1774
1775 if (!fgets(line, sizeof(line), f)) {
1776 if (feof(f))
1777 r = 0;
1778 else
1779 r = -errno;
1780
1781 goto finish;
1782 }
1783
1784 char_array_0(line);
1785 l = strstrip(line);
1786
1787 if (l[0] == 0)
1788 break;
1789
01d67b43
LP
1790 if (startswith(l, "current-job-id=")) {
1791 uint32_t id;
1792
1793 if (safe_atou32(l+15, &id) < 0)
1794 log_debug("Failed to parse current job id value %s", l+15);
1795 else
1796 m->current_job_id = MAX(m->current_job_id, id);
1797 } else if (startswith(l, "taint-usr=")) {
1798 int b;
1799
1800 if ((b = parse_boolean(l+10)) < 0)
1801 log_debug("Failed to parse taint /usr flag %s", l+10);
1802 else
1803 m->taint_usr = m->taint_usr || b;
1804 } else if (startswith(l, "initrd-timestamp="))
e9ddabc2
LP
1805 dual_timestamp_deserialize(l+17, &m->initrd_timestamp);
1806 else if (startswith(l, "startup-timestamp="))
799fd0fd 1807 dual_timestamp_deserialize(l+18, &m->startup_timestamp);
10717a1a 1808 else if (startswith(l, "finish-timestamp="))
799fd0fd 1809 dual_timestamp_deserialize(l+17, &m->finish_timestamp);
10717a1a 1810 else
10f8e83c
LP
1811 log_debug("Unknown serialization item '%s'", l);
1812 }
1813
a16e1123
LP
1814 for (;;) {
1815 Unit *u;
1816 char name[UNIT_NAME_MAX+2];
1817
1818 /* Start marker */
1819 if (!fgets(name, sizeof(name), f)) {
1820 if (feof(f))
10f8e83c
LP
1821 r = 0;
1822 else
1823 r = -errno;
a16e1123 1824
82c64bf5 1825 goto finish;
a16e1123
LP
1826 }
1827
1828 char_array_0(name);
1829
398ef8ba 1830 if ((r = manager_load_unit(m, strstrip(name), NULL, NULL, &u)) < 0)
82c64bf5 1831 goto finish;
a16e1123
LP
1832
1833 if ((r = unit_deserialize(u, f, fds)) < 0)
82c64bf5 1834 goto finish;
a16e1123
LP
1835 }
1836
10f8e83c 1837finish:
82c64bf5
LP
1838 if (ferror(f)) {
1839 r = -EIO;
1840 goto finish;
1841 }
a16e1123 1842
a7556052
LP
1843 assert(m->n_reloading > 0);
1844 m->n_reloading --;
82c64bf5
LP
1845
1846 return r;
a16e1123
LP
1847}
1848
1849int manager_reload(Manager *m) {
1850 int r, q;
1851 FILE *f;
1852 FDSet *fds;
1853
1854 assert(m);
1855
d8d5ab98 1856 if ((r = manager_open_serialization(m, &f)) < 0)
a16e1123
LP
1857 return r;
1858
a7556052 1859 m->n_reloading ++;
38c52d46 1860
a16e1123 1861 if (!(fds = fdset_new())) {
a7556052 1862 m->n_reloading --;
a16e1123
LP
1863 r = -ENOMEM;
1864 goto finish;
1865 }
1866
38c52d46 1867 if ((r = manager_serialize(m, f, fds)) < 0) {
a7556052 1868 m->n_reloading --;
a16e1123 1869 goto finish;
38c52d46 1870 }
a16e1123
LP
1871
1872 if (fseeko(f, 0, SEEK_SET) < 0) {
a7556052 1873 m->n_reloading --;
a16e1123
LP
1874 r = -errno;
1875 goto finish;
1876 }
1877
1878 /* From here on there is no way back. */
1879 manager_clear_jobs_and_units(m);
5a1e9937 1880 manager_undo_generators(m);
a16e1123 1881
2ded0c04 1882 /* Find new unit paths */
84e3543e 1883 lookup_paths_free(&m->lookup_paths);
c800e483 1884 if ((q = lookup_paths_init(&m->lookup_paths, m->running_as, true)) < 0)
2ded0c04
LP
1885 r = q;
1886
5a1e9937
LP
1887 manager_run_generators(m);
1888
1889 manager_build_unit_path_cache(m);
1890
a16e1123
LP
1891 /* First, enumerate what we can from all config files */
1892 if ((q = manager_enumerate(m)) < 0)
1893 r = q;
1894
1895 /* Second, deserialize our stored data */
1896 if ((q = manager_deserialize(m, f, fds)) < 0)
1897 r = q;
1898
1899 fclose(f);
1900 f = NULL;
1901
1902 /* Third, fire things up! */
1903 if ((q = manager_coldplug(m)) < 0)
1904 r = q;
1905
a7556052
LP
1906 assert(m->n_reloading > 0);
1907 m->n_reloading--;
9f611ad8 1908
a16e1123
LP
1909finish:
1910 if (f)
1911 fclose(f);
1912
1913 if (fds)
1914 fdset_free(fds);
1915
1916 return r;
1917}
1918
9e58ff9c
LP
1919bool manager_is_booting_or_shutting_down(Manager *m) {
1920 Unit *u;
1921
1922 assert(m);
1923
1924 /* Is the initial job still around? */
bacbccb7 1925 if (manager_get_job(m, m->default_unit_job_id))
9e58ff9c
LP
1926 return true;
1927
1928 /* Is there a job for the shutdown target? */
27d340c7
LP
1929 u = manager_get_unit(m, SPECIAL_SHUTDOWN_TARGET);
1930 if (u)
ac155bb8 1931 return !!u->job;
9e58ff9c
LP
1932
1933 return false;
1934}
1935
fdf20a31 1936void manager_reset_failed(Manager *m) {
5632e374
LP
1937 Unit *u;
1938 Iterator i;
1939
1940 assert(m);
1941
1942 HASHMAP_FOREACH(u, m->units, i)
fdf20a31 1943 unit_reset_failed(u);
5632e374
LP
1944}
1945
8f6df3fa
LP
1946bool manager_unit_pending_inactive(Manager *m, const char *name) {
1947 Unit *u;
1948
1949 assert(m);
1950 assert(name);
1951
1952 /* Returns true if the unit is inactive or going down */
8f6df3fa
LP
1953 if (!(u = manager_get_unit(m, name)))
1954 return true;
1955
18ffdfda 1956 return unit_pending_inactive(u);
8f6df3fa
LP
1957}
1958
b0c918b9 1959void manager_check_finished(Manager *m) {
e9ddabc2 1960 char userspace[FORMAT_TIMESPAN_MAX], initrd[FORMAT_TIMESPAN_MAX], kernel[FORMAT_TIMESPAN_MAX], sum[FORMAT_TIMESPAN_MAX];
7ea07dcd 1961 usec_t kernel_usec, initrd_usec, userspace_usec, total_usec;
b0c918b9
LP
1962
1963 assert(m);
1964
1965 if (dual_timestamp_is_set(&m->finish_timestamp))
1966 return;
1967
1968 if (hashmap_size(m->jobs) > 0)
1969 return;
1970
1971 dual_timestamp_get(&m->finish_timestamp);
1972
e03ae661
LP
1973 if (m->running_as == MANAGER_SYSTEM && detect_container(NULL) <= 0) {
1974
18fa6b27
LP
1975 userspace_usec = m->finish_timestamp.monotonic - m->startup_timestamp.monotonic;
1976 total_usec = m->finish_timestamp.monotonic;
1977
e9ddabc2 1978 if (dual_timestamp_is_set(&m->initrd_timestamp)) {
18fa6b27
LP
1979
1980 kernel_usec = m->initrd_timestamp.monotonic;
1981 initrd_usec = m->startup_timestamp.monotonic - m->initrd_timestamp.monotonic;
1982
e9ddabc2 1983 log_info("Startup finished in %s (kernel) + %s (initrd) + %s (userspace) = %s.",
18fa6b27
LP
1984 format_timespan(kernel, sizeof(kernel), kernel_usec),
1985 format_timespan(initrd, sizeof(initrd), initrd_usec),
1986 format_timespan(userspace, sizeof(userspace), userspace_usec),
1987 format_timespan(sum, sizeof(sum), total_usec));
1988 } else {
1989 kernel_usec = m->startup_timestamp.monotonic;
1990 initrd_usec = 0;
1991
e9ddabc2 1992 log_info("Startup finished in %s (kernel) + %s (userspace) = %s.",
18fa6b27
LP
1993 format_timespan(kernel, sizeof(kernel), kernel_usec),
1994 format_timespan(userspace, sizeof(userspace), userspace_usec),
1995 format_timespan(sum, sizeof(sum), total_usec));
1996 }
1997 } else {
1998 userspace_usec = initrd_usec = kernel_usec = 0;
1999 total_usec = m->finish_timestamp.monotonic - m->startup_timestamp.monotonic;
2000
b0c918b9 2001 log_debug("Startup finished in %s.",
18fa6b27
LP
2002 format_timespan(sum, sizeof(sum), total_usec));
2003 }
b0c918b9 2004
18fa6b27 2005 bus_broadcast_finished(m, kernel_usec, initrd_usec, userspace_usec, total_usec);
530345e7
LP
2006
2007 sd_notifyf(false,
2008 "READY=1\nSTATUS=Startup finished in %s.",
2009 format_timespan(sum, sizeof(sum), total_usec));
b0c918b9
LP
2010}
2011
5a1e9937
LP
2012void manager_run_generators(Manager *m) {
2013 DIR *d = NULL;
5a1e9937 2014 const char *generator_path;
83cc030f 2015 const char *argv[3];
07f8a4aa 2016 mode_t u;
5a1e9937
LP
2017
2018 assert(m);
2019
af2d49f7 2020 generator_path = m->running_as == MANAGER_SYSTEM ? SYSTEM_GENERATOR_PATH : USER_GENERATOR_PATH;
5a1e9937
LP
2021 if (!(d = opendir(generator_path))) {
2022
2023 if (errno == ENOENT)
2024 return;
2025
2026 log_error("Failed to enumerate generator directory: %m");
2027 return;
2028 }
2029
2030 if (!m->generator_unit_path) {
f1d19aa4
LP
2031 const char *p;
2032 char user_path[] = "/tmp/systemd-generator-XXXXXX";
5a1e9937 2033
a08dab55 2034 if (m->running_as == MANAGER_SYSTEM && getpid() == 1) {
f1d19aa4
LP
2035 p = "/run/systemd/generator";
2036
2037 if (mkdir_p(p, 0755) < 0) {
2038 log_error("Failed to create generator directory: %m");
2039 goto finish;
2040 }
2041
2042 } else {
2043 if (!(p = mkdtemp(user_path))) {
2044 log_error("Failed to create generator directory: %m");
2045 goto finish;
2046 }
5a1e9937
LP
2047 }
2048
2049 if (!(m->generator_unit_path = strdup(p))) {
2050 log_error("Failed to allocate generator unit path.");
2051 goto finish;
2052 }
2053 }
2054
83cc030f
LP
2055 argv[0] = NULL; /* Leave this empty, execute_directory() will fill something in */
2056 argv[1] = m->generator_unit_path;
2057 argv[2] = NULL;
5a1e9937 2058
07f8a4aa 2059 u = umask(0022);
83cc030f 2060 execute_directory(generator_path, d, (char**) argv);
07f8a4aa 2061 umask(u);
5a1e9937
LP
2062
2063 if (rmdir(m->generator_unit_path) >= 0) {
2064 /* Uh? we were able to remove this dir? I guess that
2065 * means the directory was empty, hence let's shortcut
2066 * this */
2067
2068 free(m->generator_unit_path);
2069 m->generator_unit_path = NULL;
2070 goto finish;
2071 }
2072
2073 if (!strv_find(m->lookup_paths.unit_path, m->generator_unit_path)) {
2074 char **l;
2075
2076 if (!(l = strv_append(m->lookup_paths.unit_path, m->generator_unit_path))) {
2077 log_error("Failed to add generator directory to unit search path: %m");
2078 goto finish;
2079 }
2080
2081 strv_free(m->lookup_paths.unit_path);
2082 m->lookup_paths.unit_path = l;
7f4e0805
LP
2083
2084 log_debug("Added generator unit path %s to search path.", m->generator_unit_path);
5a1e9937
LP
2085 }
2086
2087finish:
2088 if (d)
2089 closedir(d);
5a1e9937
LP
2090}
2091
2092void manager_undo_generators(Manager *m) {
2093 assert(m);
2094
2095 if (!m->generator_unit_path)
2096 return;
2097
2098 strv_remove(m->lookup_paths.unit_path, m->generator_unit_path);
ad293f5a 2099 rm_rf(m->generator_unit_path, false, true, false);
5a1e9937
LP
2100
2101 free(m->generator_unit_path);
2102 m->generator_unit_path = NULL;
2103}
2104
06d4c99a
LP
2105int manager_set_default_controllers(Manager *m, char **controllers) {
2106 char **l;
2107
2108 assert(m);
2109
9156e799
LP
2110 l = strv_copy(controllers);
2111 if (!l)
06d4c99a
LP
2112 return -ENOMEM;
2113
2114 strv_free(m->default_controllers);
2115 m->default_controllers = l;
2116
b59e2465 2117 cg_shorten_controllers(m->default_controllers);
9156e799 2118
06d4c99a
LP
2119 return 0;
2120}
2121
4cfa2c99 2122void manager_recheck_journal(Manager *m) {
f1dd0c3f
LP
2123 Unit *u;
2124
2125 assert(m);
2126
2127 if (m->running_as != MANAGER_SYSTEM)
2128 return;
2129
731a676c
LP
2130 u = manager_get_unit(m, SPECIAL_JOURNALD_SOCKET);
2131 if (u && SOCKET(u)->state != SOCKET_RUNNING) {
4cfa2c99 2132 log_close_journal();
731a676c 2133 return;
f1dd0c3f
LP
2134 }
2135
731a676c
LP
2136 u = manager_get_unit(m, SPECIAL_JOURNALD_SERVICE);
2137 if (u && SERVICE(u)->state != SERVICE_RUNNING) {
4cfa2c99 2138 log_close_journal();
731a676c
LP
2139 return;
2140 }
f1dd0c3f 2141
731a676c
LP
2142 /* Hmm, OK, so the socket is fully up and the service is up
2143 * too, then let's make use of the thing. */
f1dd0c3f
LP
2144 log_open();
2145}
2146
27d340c7
LP
2147void manager_set_show_status(Manager *m, bool b) {
2148 assert(m);
2149
2150 if (m->running_as != MANAGER_SYSTEM)
2151 return;
2152
2153 m->show_status = b;
2154
2155 if (b)
2156 touch("/run/systemd/show-status");
2157 else
2158 unlink("/run/systemd/show-status");
2159}
2160
2161bool manager_get_show_status(Manager *m) {
2162 assert(m);
2163
2164 if (m->running_as != MANAGER_SYSTEM)
2165 return false;
2166
2167 if (m->show_status)
2168 return true;
2169
2170 /* If Plymouth is running make sure we show the status, so
2171 * that there's something nice to see when people press Esc */
2172
2173 return plymouth_running();
2174}
2175
dfcd764e 2176static const char* const manager_running_as_table[_MANAGER_RUNNING_AS_MAX] = {
dfcd764e 2177 [MANAGER_SYSTEM] = "system",
af2d49f7 2178 [MANAGER_USER] = "user"
dfcd764e
LP
2179};
2180
2181DEFINE_STRING_TABLE_LOOKUP(manager_running_as, ManagerRunningAs);