]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/manager.c
manager: fix job mode for SIGRTMIN+1, +2
[thirdparty/systemd.git] / src / 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
9 under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
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
16 General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
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
LP
43
44#include "manager.h"
45#include "hashmap.h"
46#include "macro.h"
47#include "strv.h"
16354eff 48#include "log.h"
2a987ee8 49#include "util.h"
ea430986 50#include "ratelimit.h"
8e274523
LP
51#include "cgroup.h"
52#include "mount-setup.h"
9e2f7c11 53#include "unit-name.h"
4139c1b2
LP
54#include "dbus-unit.h"
55#include "dbus-job.h"
1137a57c 56#include "missing.h"
84e3543e 57#include "path-lookup.h"
514f4ef5 58#include "special.h"
398ef8ba 59#include "bus-errors.h"
d06dacd0 60#include "exit-status.h"
530345e7 61#include "sd-daemon.h"
60918275 62
701cc384
LP
63/* As soon as 16 units are in our GC queue, make sure to run a gc sweep */
64#define GC_QUEUE_ENTRIES_MAX 16
65
66/* As soon as 5s passed since a unit was added to our GC queue, make sure to run a gc sweep */
94b6dfa2 67#define GC_QUEUE_USEC_MAX (10*USEC_PER_SEC)
701cc384 68
8c47c732 69/* Where clients shall send notification messages to */
2b583ce6 70#define NOTIFY_SOCKET_SYSTEM "/run/systemd/notify"
91b22f21 71#define NOTIFY_SOCKET_USER "@/org/freedesktop/systemd1/notify"
8c47c732
LP
72
73static int manager_setup_notify(Manager *m) {
74 union {
75 struct sockaddr sa;
76 struct sockaddr_un un;
77 } sa;
78 struct epoll_event ev;
1d6702e8
LP
79 int one = 1, r;
80 mode_t u;
8c47c732
LP
81
82 assert(m);
83
84 m->notify_watch.type = WATCH_NOTIFY;
85 if ((m->notify_watch.fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0)) < 0) {
86 log_error("Failed to allocate notification socket: %m");
87 return -errno;
88 }
89
90 zero(sa);
91 sa.sa.sa_family = AF_UNIX;
92
a821caaa 93 if (getpid() != 1)
91b22f21 94 snprintf(sa.un.sun_path, sizeof(sa.un.sun_path), NOTIFY_SOCKET_USER "/%llu", random_ull());
6f79c579
LP
95 else {
96 unlink(NOTIFY_SOCKET_SYSTEM);
91b22f21 97 strncpy(sa.un.sun_path, NOTIFY_SOCKET_SYSTEM, sizeof(sa.un.sun_path));
6f79c579 98 }
91b22f21
LP
99
100 if (sa.un.sun_path[0] == '@')
101 sa.un.sun_path[0] = 0;
8c47c732 102
1d6702e8
LP
103 u = umask(0111);
104 r = bind(m->notify_watch.fd, &sa.sa, offsetof(struct sockaddr_un, sun_path) + 1 + strlen(sa.un.sun_path+1));
105 umask(u);
106
107 if (r < 0) {
8c47c732
LP
108 log_error("bind() failed: %m");
109 return -errno;
110 }
111
112 if (setsockopt(m->notify_watch.fd, SOL_SOCKET, SO_PASSCRED, &one, sizeof(one)) < 0) {
113 log_error("SO_PASSCRED failed: %m");
114 return -errno;
115 }
116
117 zero(ev);
118 ev.events = EPOLLIN;
119 ev.data.ptr = &m->notify_watch;
120
121 if (epoll_ctl(m->epoll_fd, EPOLL_CTL_ADD, m->notify_watch.fd, &ev) < 0)
122 return -errno;
123
91b22f21
LP
124 if (sa.un.sun_path[0] == 0)
125 sa.un.sun_path[0] = '@';
126
127 if (!(m->notify_socket = strdup(sa.un.sun_path)))
8c47c732
LP
128 return -ENOMEM;
129
b2bb3dbe
LP
130 log_debug("Using notification socket %s", m->notify_socket);
131
8c47c732
LP
132 return 0;
133}
134
80876c20 135static int enable_special_signals(Manager *m) {
4466ee6a 136 int fd;
80876c20
LP
137
138 assert(m);
139
140 /* Enable that we get SIGINT on control-alt-del */
141 if (reboot(RB_DISABLE_CAD) < 0)
142 log_warning("Failed to enable ctrl-alt-del handling: %m");
143
ccaa6149 144 if ((fd = open_terminal("/dev/tty0", O_RDWR|O_NOCTTY|O_CLOEXEC)) < 0)
80876c20
LP
145 log_warning("Failed to open /dev/tty0: %m");
146 else {
147 /* Enable that we get SIGWINCH on kbrequest */
148 if (ioctl(fd, KDSIGACCEPT, SIGWINCH) < 0)
149 log_warning("Failed to enable kbrequest handling: %s", strerror(errno));
150
151 close_nointr_nofail(fd);
152 }
153
154 return 0;
155}
156
ce578209 157static int manager_setup_signals(Manager *m) {
9152c765
LP
158 sigset_t mask;
159 struct epoll_event ev;
57c0c30e 160 struct sigaction sa;
60918275 161
ce578209
LP
162 assert(m);
163
57c0c30e
LP
164 /* We are not interested in SIGSTOP and friends. */
165 zero(sa);
166 sa.sa_handler = SIG_DFL;
167 sa.sa_flags = SA_NOCLDSTOP|SA_RESTART;
168 assert_se(sigaction(SIGCHLD, &sa, NULL) == 0);
169
ce578209 170 assert_se(sigemptyset(&mask) == 0);
7d793605
LP
171
172 sigset_add_many(&mask,
173 SIGCHLD, /* Child died */
174 SIGTERM, /* Reexecute daemon */
175 SIGHUP, /* Reload configuration */
176 SIGUSR1, /* systemd/upstart: reconnect to D-Bus */
177 SIGUSR2, /* systemd: dump status */
178 SIGINT, /* Kernel sends us this on control-alt-del */
179 SIGWINCH, /* Kernel sends us this on kbrequest (alt-arrowup) */
180 SIGPWR, /* Some kernel drivers and upsd send us this on power failure */
181 SIGRTMIN+0, /* systemd: start default.target */
0003d1ab 182 SIGRTMIN+1, /* systemd: isolate rescue.target */
7d793605
LP
183 SIGRTMIN+2, /* systemd: isolate emergency.target */
184 SIGRTMIN+3, /* systemd: start halt.target */
185 SIGRTMIN+4, /* systemd: start poweroff.target */
186 SIGRTMIN+5, /* systemd: start reboot.target */
0003d1ab
LP
187 SIGRTMIN+6, /* systemd: start kexec.target */
188 SIGRTMIN+13, /* systemd: Immediate halt */
189 SIGRTMIN+14, /* systemd: Immediate poweroff */
190 SIGRTMIN+15, /* systemd: Immediate reboot */
191 SIGRTMIN+16, /* systemd: Immediate kexec */
0658666b
LP
192 SIGRTMIN+20, /* systemd: enable status messages */
193 SIGRTMIN+21, /* systemd: disable status messages */
253ee27a
LP
194 SIGRTMIN+22, /* systemd: set log level to LOG_DEBUG */
195 SIGRTMIN+23, /* systemd: set log level to LOG_INFO */
196 SIGRTMIN+27, /* systemd: set log target to console */
197 SIGRTMIN+28, /* systemd: set log target to kmsg */
198 SIGRTMIN+29, /* systemd: set log target to syslog-or-kmsg */
7d793605 199 -1);
ce578209
LP
200 assert_se(sigprocmask(SIG_SETMASK, &mask, NULL) == 0);
201
ef734fd6 202 m->signal_watch.type = WATCH_SIGNAL;
ce578209
LP
203 if ((m->signal_watch.fd = signalfd(-1, &mask, SFD_NONBLOCK|SFD_CLOEXEC)) < 0)
204 return -errno;
205
206 zero(ev);
207 ev.events = EPOLLIN;
208 ev.data.ptr = &m->signal_watch;
209
210 if (epoll_ctl(m->epoll_fd, EPOLL_CTL_ADD, m->signal_watch.fd, &ev) < 0)
211 return -errno;
212
a3d4e06d 213 if (m->running_as == MANAGER_SYSTEM)
80876c20 214 return enable_special_signals(m);
e1414003 215
ce578209
LP
216 return 0;
217}
218
9e58ff9c 219int manager_new(ManagerRunningAs running_as, Manager **_m) {
ce578209 220 Manager *m;
8e274523
LP
221 int r = -ENOMEM;
222
223 assert(_m);
a5dab5ce
LP
224 assert(running_as >= 0);
225 assert(running_as < _MANAGER_RUNNING_AS_MAX);
ce578209 226
60918275 227 if (!(m = new0(Manager, 1)))
8e274523 228 return -ENOMEM;
60918275 229
63983207 230 dual_timestamp_get(&m->startup_timestamp);
e537352b 231
a5dab5ce 232 m->running_as = running_as;
a567261a 233 m->name_data_slot = m->subscribed_data_slot = -1;
a16e1123 234 m->exit_code = _MANAGER_EXIT_CODE_INVALID;
33be102a 235 m->pin_cgroupfs_fd = -1;
80876c20 236
4927fcae
LP
237#ifdef HAVE_AUDIT
238 m->audit_fd = -1;
239#endif
240
4e434314 241 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 242 m->current_job_id = 1; /* start as id #1, so that we can leave #0 around as "null-like" value */
9152c765 243
1137a57c
LP
244 if (!(m->environment = strv_copy(environ)))
245 goto fail;
246
06d4c99a
LP
247 if (!(m->default_controllers = strv_new("cpu", NULL)))
248 goto fail;
249
87f0e418 250 if (!(m->units = hashmap_new(string_hash_func, string_compare_func)))
60918275
LP
251 goto fail;
252
253 if (!(m->jobs = hashmap_new(trivial_hash_func, trivial_compare_func)))
254 goto fail;
255
e5b5ae50 256 if (!(m->transaction_jobs = hashmap_new(trivial_hash_func, trivial_compare_func)))
60918275
LP
257 goto fail;
258
9152c765
LP
259 if (!(m->watch_pids = hashmap_new(trivial_hash_func, trivial_compare_func)))
260 goto fail;
261
8e274523
LP
262 if (!(m->cgroup_bondings = hashmap_new(string_hash_func, string_compare_func)))
263 goto fail;
264
05e343b7
LP
265 if (!(m->watch_bus = hashmap_new(string_hash_func, string_compare_func)))
266 goto fail;
267
9152c765
LP
268 if ((m->epoll_fd = epoll_create1(EPOLL_CLOEXEC)) < 0)
269 goto fail;
270
c800e483 271 if ((r = lookup_paths_init(&m->lookup_paths, m->running_as, true)) < 0)
e1414003
LP
272 goto fail;
273
8e274523
LP
274 if ((r = manager_setup_signals(m)) < 0)
275 goto fail;
276
8e274523 277 if ((r = manager_setup_cgroup(m)) < 0)
9152c765
LP
278 goto fail;
279
8c47c732
LP
280 if ((r = manager_setup_notify(m)) < 0)
281 goto fail;
282
f278026d 283 /* Try to connect to the busses, if possible. */
3996fbe2 284 if ((r = bus_init(m, running_as != MANAGER_SYSTEM)) < 0)
ea430986
LP
285 goto fail;
286
e543deae 287#ifdef HAVE_AUDIT
4927fcae
LP
288 if ((m->audit_fd = audit_open()) < 0)
289 log_error("Failed to connect to audit log: %m");
e543deae 290#endif
4927fcae 291
72bc8d00
LP
292 m->taint_usr = dir_is_empty("/usr") > 0;
293
8e274523
LP
294 *_m = m;
295 return 0;
60918275
LP
296
297fail:
298 manager_free(m);
8e274523 299 return r;
60918275
LP
300}
301
23a177ef
LP
302static unsigned manager_dispatch_cleanup_queue(Manager *m) {
303 Meta *meta;
304 unsigned n = 0;
305
306 assert(m);
307
308 while ((meta = m->cleanup_queue)) {
309 assert(meta->in_cleanup_queue);
310
399ab2b1 311 unit_free((Unit*) meta);
23a177ef
LP
312 n++;
313 }
314
315 return n;
316}
317
eced69b3 318enum {
35b8ca3a 319 GC_OFFSET_IN_PATH, /* This one is on the path we were traveling */
eced69b3
LP
320 GC_OFFSET_UNSURE, /* No clue */
321 GC_OFFSET_GOOD, /* We still need this unit */
322 GC_OFFSET_BAD, /* We don't need this unit anymore */
323 _GC_OFFSET_MAX
324};
325
326static void unit_gc_sweep(Unit *u, unsigned gc_marker) {
701cc384
LP
327 Iterator i;
328 Unit *other;
eced69b3 329 bool is_bad;
701cc384
LP
330
331 assert(u);
332
eced69b3
LP
333 if (u->meta.gc_marker == gc_marker + GC_OFFSET_GOOD ||
334 u->meta.gc_marker == gc_marker + GC_OFFSET_BAD ||
335 u->meta.gc_marker == gc_marker + GC_OFFSET_IN_PATH)
701cc384
LP
336 return;
337
c9c0cadb 338 if (u->meta.in_cleanup_queue)
701cc384
LP
339 goto bad;
340
341 if (unit_check_gc(u))
342 goto good;
343
eced69b3
LP
344 u->meta.gc_marker = gc_marker + GC_OFFSET_IN_PATH;
345
346 is_bad = true;
347
701cc384
LP
348 SET_FOREACH(other, u->meta.dependencies[UNIT_REFERENCED_BY], i) {
349 unit_gc_sweep(other, gc_marker);
350
eced69b3 351 if (other->meta.gc_marker == gc_marker + GC_OFFSET_GOOD)
701cc384 352 goto good;
eced69b3
LP
353
354 if (other->meta.gc_marker != gc_marker + GC_OFFSET_BAD)
355 is_bad = false;
701cc384
LP
356 }
357
eced69b3
LP
358 if (is_bad)
359 goto bad;
360
361 /* We were unable to find anything out about this entry, so
362 * let's investigate it later */
363 u->meta.gc_marker = gc_marker + GC_OFFSET_UNSURE;
364 unit_add_to_gc_queue(u);
365 return;
366
701cc384 367bad:
eced69b3
LP
368 /* We definitely know that this one is not useful anymore, so
369 * let's mark it for deletion */
370 u->meta.gc_marker = gc_marker + GC_OFFSET_BAD;
371 unit_add_to_cleanup_queue(u);
701cc384
LP
372 return;
373
374good:
eced69b3 375 u->meta.gc_marker = gc_marker + GC_OFFSET_GOOD;
701cc384
LP
376}
377
378static unsigned manager_dispatch_gc_queue(Manager *m) {
379 Meta *meta;
380 unsigned n = 0;
eced69b3 381 unsigned gc_marker;
701cc384
LP
382
383 assert(m);
384
385 if ((m->n_in_gc_queue < GC_QUEUE_ENTRIES_MAX) &&
386 (m->gc_queue_timestamp <= 0 ||
387 (m->gc_queue_timestamp + GC_QUEUE_USEC_MAX) > now(CLOCK_MONOTONIC)))
388 return 0;
389
390 log_debug("Running GC...");
391
eced69b3
LP
392 m->gc_marker += _GC_OFFSET_MAX;
393 if (m->gc_marker + _GC_OFFSET_MAX <= _GC_OFFSET_MAX)
c9c0cadb 394 m->gc_marker = 1;
701cc384 395
eced69b3
LP
396 gc_marker = m->gc_marker;
397
701cc384
LP
398 while ((meta = m->gc_queue)) {
399 assert(meta->in_gc_queue);
400
399ab2b1 401 unit_gc_sweep((Unit*) meta, gc_marker);
eced69b3 402
701cc384
LP
403 LIST_REMOVE(Meta, gc_queue, m->gc_queue, meta);
404 meta->in_gc_queue = false;
405
406 n++;
407
eced69b3
LP
408 if (meta->gc_marker == gc_marker + GC_OFFSET_BAD ||
409 meta->gc_marker == gc_marker + GC_OFFSET_UNSURE) {
701cc384 410 log_debug("Collecting %s", meta->id);
eced69b3 411 meta->gc_marker = gc_marker + GC_OFFSET_BAD;
399ab2b1 412 unit_add_to_cleanup_queue((Unit*) meta);
701cc384
LP
413 }
414 }
415
416 m->n_in_gc_queue = 0;
417 m->gc_queue_timestamp = 0;
418
419 return n;
420}
421
a16e1123 422static void manager_clear_jobs_and_units(Manager *m) {
e5b5ae50 423 Job *j;
a16e1123 424 Unit *u;
60918275
LP
425
426 assert(m);
427
87f0e418 428 while ((j = hashmap_first(m->transaction_jobs)))
e5b5ae50
LP
429 job_free(j);
430
87f0e418
LP
431 while ((u = hashmap_first(m->units)))
432 unit_free(u);
964e0949
LP
433
434 manager_dispatch_cleanup_queue(m);
435
436 assert(!m->load_queue);
437 assert(!m->run_queue);
438 assert(!m->dbus_unit_queue);
439 assert(!m->dbus_job_queue);
440 assert(!m->cleanup_queue);
441 assert(!m->gc_queue);
442
443 assert(hashmap_isempty(m->transaction_jobs));
444 assert(hashmap_isempty(m->jobs));
445 assert(hashmap_isempty(m->units));
a16e1123
LP
446}
447
448void manager_free(Manager *m) {
449 UnitType c;
87f0e418 450
a16e1123
LP
451 assert(m);
452
453 manager_clear_jobs_and_units(m);
23a177ef 454
7824bbeb
LP
455 for (c = 0; c < _UNIT_TYPE_MAX; c++)
456 if (unit_vtable[c]->shutdown)
457 unit_vtable[c]->shutdown(m);
458
a16e1123
LP
459 /* If we reexecute ourselves, we keep the root cgroup
460 * around */
c6c18be3 461 manager_shutdown_cgroup(m, m->exit_code != MANAGER_REEXECUTE);
8e274523 462
5a1e9937
LP
463 manager_undo_generators(m);
464
5e8d1c9a 465 bus_done(m);
ea430986 466
87f0e418 467 hashmap_free(m->units);
60918275 468 hashmap_free(m->jobs);
e5b5ae50 469 hashmap_free(m->transaction_jobs);
9152c765 470 hashmap_free(m->watch_pids);
05e343b7 471 hashmap_free(m->watch_bus);
9152c765
LP
472
473 if (m->epoll_fd >= 0)
a16e1123 474 close_nointr_nofail(m->epoll_fd);
acbb0225 475 if (m->signal_watch.fd >= 0)
a16e1123 476 close_nointr_nofail(m->signal_watch.fd);
8c47c732
LP
477 if (m->notify_watch.fd >= 0)
478 close_nointr_nofail(m->notify_watch.fd);
60918275 479
4927fcae
LP
480#ifdef HAVE_AUDIT
481 if (m->audit_fd >= 0)
482 audit_close(m->audit_fd);
483#endif
484
c952c6ec
LP
485 free(m->notify_socket);
486
84e3543e 487 lookup_paths_free(&m->lookup_paths);
1137a57c 488 strv_free(m->environment);
036643a2 489
06d4c99a
LP
490 strv_free(m->default_controllers);
491
8e274523 492 hashmap_free(m->cgroup_bondings);
c6c18be3 493 set_free_free(m->unit_path_cache);
33be102a 494
60918275
LP
495 free(m);
496}
497
a16e1123
LP
498int manager_enumerate(Manager *m) {
499 int r = 0, q;
f50e0a01 500 UnitType c;
f50e0a01
LP
501
502 assert(m);
503
a16e1123
LP
504 /* Let's ask every type to load all units from disk/kernel
505 * that it might know */
f50e0a01
LP
506 for (c = 0; c < _UNIT_TYPE_MAX; c++)
507 if (unit_vtable[c]->enumerate)
a16e1123
LP
508 if ((q = unit_vtable[c]->enumerate(m)) < 0)
509 r = q;
f50e0a01
LP
510
511 manager_dispatch_load_queue(m);
a16e1123
LP
512 return r;
513}
514
515int manager_coldplug(Manager *m) {
516 int r = 0, q;
517 Iterator i;
518 Unit *u;
519 char *k;
520
521 assert(m);
f50e0a01
LP
522
523 /* Then, let's set up their initial state. */
524 HASHMAP_FOREACH_KEY(u, k, m->units, i) {
525
526 /* ignore aliases */
9e2f7c11 527 if (u->meta.id != k)
f50e0a01
LP
528 continue;
529
cca098b0
LP
530 if ((q = unit_coldplug(u)) < 0)
531 r = q;
f50e0a01
LP
532 }
533
a16e1123
LP
534 return r;
535}
536
fe51822e
LP
537static void manager_build_unit_path_cache(Manager *m) {
538 char **i;
539 DIR *d = NULL;
540 int r;
541
542 assert(m);
543
544 set_free_free(m->unit_path_cache);
545
546 if (!(m->unit_path_cache = set_new(string_hash_func, string_compare_func))) {
547 log_error("Failed to allocate unit path cache.");
548 return;
549 }
550
551 /* This simply builds a list of files we know exist, so that
552 * we don't always have to go to disk */
553
554 STRV_FOREACH(i, m->lookup_paths.unit_path) {
555 struct dirent *de;
556
557 if (!(d = opendir(*i))) {
558 log_error("Failed to open directory: %m");
559 continue;
560 }
561
562 while ((de = readdir(d))) {
563 char *p;
564
565 if (ignore_file(de->d_name))
566 continue;
567
44d91056
LP
568 p = join(streq(*i, "/") ? "" : *i, "/", de->d_name, NULL);
569 if (!p) {
fe51822e
LP
570 r = -ENOMEM;
571 goto fail;
572 }
573
574 if ((r = set_put(m->unit_path_cache, p)) < 0) {
575 free(p);
576 goto fail;
577 }
578 }
579
580 closedir(d);
581 d = NULL;
582 }
583
584 return;
585
586fail:
587 log_error("Failed to build unit path cache: %s", strerror(-r));
588
589 set_free_free(m->unit_path_cache);
590 m->unit_path_cache = NULL;
591
592 if (d)
593 closedir(d);
594}
595
a16e1123
LP
596int manager_startup(Manager *m, FILE *serialization, FDSet *fds) {
597 int r, q;
598
599 assert(m);
600
5a1e9937
LP
601 manager_run_generators(m);
602
fe51822e
LP
603 manager_build_unit_path_cache(m);
604
9f611ad8
LP
605 /* If we will deserialize make sure that during enumeration
606 * this is already known, so we increase the counter here
607 * already */
608 if (serialization)
a7556052 609 m->n_reloading ++;
9f611ad8 610
a16e1123
LP
611 /* First, enumerate what we can from all config files */
612 r = manager_enumerate(m);
613
614 /* Second, deserialize if there is something to deserialize */
615 if (serialization)
616 if ((q = manager_deserialize(m, serialization, fds)) < 0)
617 r = q;
618
619 /* Third, fire things up! */
620 if ((q = manager_coldplug(m)) < 0)
621 r = q;
622
9f611ad8 623 if (serialization) {
a7556052
LP
624 assert(m->n_reloading > 0);
625 m->n_reloading --;
9f611ad8
LP
626 }
627
a16e1123 628 return r;
f50e0a01
LP
629}
630
23a177ef 631static void transaction_delete_job(Manager *m, Job *j, bool delete_dependencies) {
302d0040
LP
632 assert(m);
633 assert(j);
634
1ffba6fe
LP
635 /* Deletes one job from the transaction */
636
23a177ef 637 manager_transaction_unlink_job(m, j, delete_dependencies);
302d0040 638
ac1135be 639 if (!j->installed)
302d0040
LP
640 job_free(j);
641}
642
87f0e418 643static void transaction_delete_unit(Manager *m, Unit *u) {
1ffba6fe
LP
644 Job *j;
645
87f0e418 646 /* Deletes all jobs associated with a certain unit from the
1ffba6fe
LP
647 * transaction */
648
87f0e418 649 while ((j = hashmap_get(m->transaction_jobs, u)))
23a177ef 650 transaction_delete_job(m, j, true);
1ffba6fe
LP
651}
652
f04fa1d5
LP
653static void transaction_clean_dependencies(Manager *m) {
654 Iterator i;
655 Job *j;
656
657 assert(m);
658
659 /* Drops all dependencies of all installed jobs */
660
661 HASHMAP_FOREACH(j, m->jobs, i) {
662 while (j->subject_list)
663 job_dependency_free(j->subject_list);
664 while (j->object_list)
665 job_dependency_free(j->object_list);
666 }
667
668 assert(!m->transaction_anchor);
669}
670
11dd41ce
LP
671static void transaction_abort(Manager *m) {
672 Job *j;
673
674 assert(m);
11dd41ce 675
e5b5ae50 676 while ((j = hashmap_first(m->transaction_jobs)))
ac1135be 677 if (j->installed)
23a177ef 678 transaction_delete_job(m, j, true);
e5b5ae50
LP
679 else
680 job_free(j);
681
682 assert(hashmap_isempty(m->transaction_jobs));
f04fa1d5
LP
683
684 transaction_clean_dependencies(m);
e5b5ae50
LP
685}
686
687static void transaction_find_jobs_that_matter_to_anchor(Manager *m, Job *j, unsigned generation) {
688 JobDependency *l;
689
690 assert(m);
691
87f0e418 692 /* A recursive sweep through the graph that marks all units
1ffba6fe
LP
693 * that matter to the anchor job, i.e. are directly or
694 * indirectly a dependency of the anchor job via paths that
695 * are fully marked as mattering. */
696
44d8db9e
LP
697 if (j)
698 l = j->subject_list;
699 else
700 l = m->transaction_anchor;
701
702 LIST_FOREACH(subject, l, l) {
e5b5ae50
LP
703
704 /* This link does not matter */
705 if (!l->matters)
706 continue;
707
87f0e418 708 /* This unit has already been marked */
e5b5ae50
LP
709 if (l->object->generation == generation)
710 continue;
711
712 l->object->matters_to_anchor = true;
713 l->object->generation = generation;
714
715 transaction_find_jobs_that_matter_to_anchor(m, l->object, generation);
716 }
717}
718
7fad411c 719static void transaction_merge_and_delete_job(Manager *m, Job *j, Job *other, JobType t) {
e5b5ae50
LP
720 JobDependency *l, *last;
721
722 assert(j);
723 assert(other);
87f0e418 724 assert(j->unit == other->unit);
ac1135be 725 assert(!j->installed);
e5b5ae50 726
1ffba6fe
LP
727 /* Merges 'other' into 'j' and then deletes j. */
728
e5b5ae50
LP
729 j->type = t;
730 j->state = JOB_WAITING;
9e2f7c11 731 j->override = j->override || other->override;
e5b5ae50
LP
732
733 j->matters_to_anchor = j->matters_to_anchor || other->matters_to_anchor;
734
735 /* Patch us in as new owner of the JobDependency objects */
736 last = NULL;
44d8db9e 737 LIST_FOREACH(subject, l, other->subject_list) {
e5b5ae50
LP
738 assert(l->subject == other);
739 l->subject = j;
740 last = l;
741 }
742
743 /* Merge both lists */
744 if (last) {
745 last->subject_next = j->subject_list;
746 if (j->subject_list)
747 j->subject_list->subject_prev = last;
748 j->subject_list = other->subject_list;
749 }
750
751 /* Patch us in as new owner of the JobDependency objects */
752 last = NULL;
44d8db9e 753 LIST_FOREACH(object, l, other->object_list) {
e5b5ae50
LP
754 assert(l->object == other);
755 l->object = j;
756 last = l;
757 }
758
759 /* Merge both lists */
760 if (last) {
761 last->object_next = j->object_list;
762 if (j->object_list)
763 j->object_list->object_prev = last;
764 j->object_list = other->object_list;
765 }
766
e5b5ae50
LP
767 /* Kill the other job */
768 other->subject_list = NULL;
769 other->object_list = NULL;
23a177ef 770 transaction_delete_job(m, other, true);
e5b5ae50 771}
69dd2852
LP
772static bool job_is_conflicted_by(Job *j) {
773 JobDependency *l;
774
775 assert(j);
776
777 /* Returns true if this job is pulled in by a least one
778 * ConflictedBy dependency. */
779
780 LIST_FOREACH(object, l, j->object_list)
781 if (l->conflicts)
782 return true;
783
784 return false;
785}
e5b5ae50 786
5cb5a6ff 787static int delete_one_unmergeable_job(Manager *m, Job *j) {
1ffba6fe
LP
788 Job *k;
789
790 assert(j);
791
792 /* Tries to delete one item in the linked list
793 * j->transaction_next->transaction_next->... that conflicts
35b8ca3a 794 * with another one, in an attempt to make an inconsistent
1ffba6fe
LP
795 * transaction work. */
796
797 /* We rely here on the fact that if a merged with b does not
798 * merge with c, either a or b merge with c neither */
034c6ed7
LP
799 LIST_FOREACH(transaction, j, j)
800 LIST_FOREACH(transaction, k, j->transaction_next) {
1ffba6fe
LP
801 Job *d;
802
803 /* Is this one mergeable? Then skip it */
5cb5a6ff 804 if (job_type_is_mergeable(j->type, k->type))
1ffba6fe
LP
805 continue;
806
807 /* Ok, we found two that conflict, let's see if we can
808 * drop one of them */
69dd2852
LP
809 if (!j->matters_to_anchor && !k->matters_to_anchor) {
810
811 /* Both jobs don't matter, so let's
812 * find the one that is smarter to
813 * remove. Let's think positive and
814 * rather remove stops then starts --
815 * except if something is being
816 * stopped because it is conflicted by
817 * another unit in which case we
818 * rather remove the start. */
819
820 log_debug("Looking at job %s/%s conflicted_by=%s", j->unit->meta.id, job_type_to_string(j->type), yes_no(j->type == JOB_STOP && job_is_conflicted_by(j)));
821 log_debug("Looking at job %s/%s conflicted_by=%s", k->unit->meta.id, job_type_to_string(k->type), yes_no(k->type == JOB_STOP && job_is_conflicted_by(k)));
822
823 if (j->type == JOB_STOP) {
824
825 if (job_is_conflicted_by(j))
826 d = k;
827 else
828 d = j;
829
830 } else if (k->type == JOB_STOP) {
831
832 if (job_is_conflicted_by(k))
833 d = j;
834 else
835 d = k;
e364ad06
LP
836 } else
837 d = j;
69dd2852
LP
838
839 } else if (!j->matters_to_anchor)
1ffba6fe
LP
840 d = j;
841 else if (!k->matters_to_anchor)
842 d = k;
843 else
844 return -ENOEXEC;
845
846 /* Ok, we can drop one, so let's do so. */
2e81c8a5 847 log_debug("Fixing conflicting jobs by deleting job %s/%s", d->unit->meta.id, job_type_to_string(d->type));
23a177ef 848 transaction_delete_job(m, d, true);
1ffba6fe
LP
849 return 0;
850 }
851
852 return -EINVAL;
853}
854
398ef8ba 855static int transaction_merge_jobs(Manager *m, DBusError *e) {
11dd41ce 856 Job *j;
034c6ed7 857 Iterator i;
e5b5ae50
LP
858 int r;
859
860 assert(m);
861
1ffba6fe
LP
862 /* First step, check whether any of the jobs for one specific
863 * task conflict. If so, try to drop one of them. */
034c6ed7 864 HASHMAP_FOREACH(j, m->transaction_jobs, i) {
1ffba6fe
LP
865 JobType t;
866 Job *k;
867
868 t = j->type;
034c6ed7 869 LIST_FOREACH(transaction, k, j->transaction_next) {
e364ad06 870 if (job_type_merge(&t, k->type) >= 0)
1ffba6fe
LP
871 continue;
872
873 /* OK, we could not merge all jobs for this
874 * action. Let's see if we can get rid of one
875 * of them */
876
5cb5a6ff 877 if ((r = delete_one_unmergeable_job(m, j)) >= 0)
1ffba6fe
LP
878 /* Ok, we managed to drop one, now
879 * let's ask our callers to call us
880 * again after garbage collecting */
881 return -EAGAIN;
882
883 /* We couldn't merge anything. Failure */
398ef8ba
LP
884 dbus_set_error(e, BUS_ERROR_TRANSACTION_JOBS_CONFLICTING, "Transaction contains conflicting jobs '%s' and '%s' for %s. Probably contradicting requirement dependencies configured.",
885 job_type_to_string(t), job_type_to_string(k->type), k->unit->meta.id);
1ffba6fe
LP
886 return r;
887 }
888 }
889
890 /* Second step, merge the jobs. */
034c6ed7 891 HASHMAP_FOREACH(j, m->transaction_jobs, i) {
e5b5ae50
LP
892 JobType t = j->type;
893 Job *k;
894
e094e853 895 /* Merge all transactions */
034c6ed7 896 LIST_FOREACH(transaction, k, j->transaction_next)
1ffba6fe 897 assert_se(job_type_merge(&t, k->type) == 0);
e5b5ae50 898
5cb5a6ff 899 /* If an active job is mergeable, merge it too */
87f0e418
LP
900 if (j->unit->meta.job)
901 job_type_merge(&t, j->unit->meta.job->type); /* Might fail. Which is OK */
e094e853 902
e5b5ae50 903 while ((k = j->transaction_next)) {
ac1135be 904 if (j->installed) {
7fad411c 905 transaction_merge_and_delete_job(m, k, j, t);
e5b5ae50
LP
906 j = k;
907 } else
7fad411c 908 transaction_merge_and_delete_job(m, j, k, t);
e5b5ae50
LP
909 }
910
1b562e46
MS
911 if (j->unit->meta.job && !j->installed)
912 transaction_merge_and_delete_job(m, j, j->unit->meta.job, t);
913
e5b5ae50
LP
914 assert(!j->transaction_next);
915 assert(!j->transaction_prev);
916 }
917
7fad411c 918 return 0;
e5b5ae50
LP
919}
920
23a177ef
LP
921static void transaction_drop_redundant(Manager *m) {
922 bool again;
923
924 assert(m);
925
926 /* Goes through the transaction and removes all jobs that are
927 * a noop */
928
929 do {
930 Job *j;
931 Iterator i;
932
933 again = false;
934
935 HASHMAP_FOREACH(j, m->transaction_jobs, i) {
936 bool changes_something = false;
937 Job *k;
938
939 LIST_FOREACH(transaction, k, j) {
940
941 if (!job_is_anchor(k) &&
3aea3b35
LP
942 (k->installed || job_type_is_redundant(k->type, unit_active_state(k->unit))) &&
943 (!k->unit->meta.job || !job_type_is_conflicting(k->type, k->unit->meta.job->type)))
23a177ef
LP
944 continue;
945
946 changes_something = true;
947 break;
948 }
949
950 if (changes_something)
951 continue;
952
9b3d9090 953 /* log_debug("Found redundant job %s/%s, dropping.", j->unit->meta.id, job_type_to_string(j->type)); */
23a177ef
LP
954 transaction_delete_job(m, j, false);
955 again = true;
956 break;
957 }
958
959 } while (again);
960}
961
87f0e418
LP
962static bool unit_matters_to_anchor(Unit *u, Job *j) {
963 assert(u);
1ffba6fe
LP
964 assert(!j->transaction_prev);
965
87f0e418 966 /* Checks whether at least one of the jobs for this unit
1ffba6fe
LP
967 * matters to the anchor. */
968
034c6ed7 969 LIST_FOREACH(transaction, j, j)
1ffba6fe
LP
970 if (j->matters_to_anchor)
971 return true;
972
973 return false;
974}
975
398ef8ba 976static int transaction_verify_order_one(Manager *m, Job *j, Job *from, unsigned generation, DBusError *e) {
034c6ed7 977 Iterator i;
87f0e418 978 Unit *u;
11dd41ce 979 int r;
e5b5ae50
LP
980
981 assert(m);
982 assert(j);
1ffba6fe
LP
983 assert(!j->transaction_prev);
984
985 /* Does a recursive sweep through the ordering graph, looking
986 * for a cycle. If we find cycle we try to break it. */
e5b5ae50 987
23e3c588
LP
988 /* Have we seen this before? */
989 if (j->generation == generation) {
674a6e4d 990 Job *k, *delete;
e5b5ae50 991
23e3c588
LP
992 /* If the marker is NULL we have been here already and
993 * decided the job was loop-free from here. Hence
994 * shortcut things and return right-away. */
995 if (!j->marker)
996 return 0;
e5b5ae50 997
23e3c588
LP
998 /* So, the marker is not NULL and we already have been
999 * here. We have a cycle. Let's try to break it. We go
1000 * backwards in our path and try to find a suitable
1001 * job to remove. We use the marker to find our way
1002 * back, since smart how we are we stored our way back
1003 * in there. */
54165a39 1004 log_warning("Found ordering cycle on %s/%s", j->unit->meta.id, job_type_to_string(j->type));
9f04bd52 1005
674a6e4d 1006 delete = NULL;
23e3c588 1007 for (k = from; k; k = ((k->generation == generation && k->marker != k) ? k->marker : NULL)) {
1ffba6fe 1008
54165a39 1009 log_info("Walked on cycle path to %s/%s", k->unit->meta.id, job_type_to_string(k->type));
9f04bd52 1010
674a6e4d
LP
1011 if (!delete &&
1012 !k->installed &&
87f0e418 1013 !unit_matters_to_anchor(k->unit, k)) {
1ffba6fe
LP
1014 /* Ok, we can drop this one, so let's
1015 * do so. */
674a6e4d 1016 delete = k;
e5b5ae50
LP
1017 }
1018
1019 /* Check if this in fact was the beginning of
7fad411c 1020 * the cycle */
e5b5ae50
LP
1021 if (k == j)
1022 break;
1023 }
1024
674a6e4d
LP
1025
1026 if (delete) {
54ec68b6 1027 log_warning("Breaking ordering cycle by deleting job %s/%s", delete->unit->meta.id, job_type_to_string(delete->type));
674a6e4d
LP
1028 transaction_delete_unit(m, delete->unit);
1029 return -EAGAIN;
1030 }
1031
54165a39 1032 log_error("Unable to break cycle");
9f04bd52 1033
fe71c02c 1034 dbus_set_error(e, BUS_ERROR_TRANSACTION_ORDER_IS_CYCLIC, "Transaction order is cyclic. See system logs for details.");
1ffba6fe 1035 return -ENOEXEC;
e5b5ae50
LP
1036 }
1037
1ffba6fe 1038 /* Make the marker point to where we come from, so that we can
23e3c588
LP
1039 * find our way backwards if we want to break a cycle. We use
1040 * a special marker for the beginning: we point to
1041 * ourselves. */
1042 j->marker = from ? from : j;
e5b5ae50
LP
1043 j->generation = generation;
1044
1ffba6fe 1045 /* We assume that the the dependencies are bidirectional, and
87f0e418
LP
1046 * hence can ignore UNIT_AFTER */
1047 SET_FOREACH(u, j->unit->meta.dependencies[UNIT_BEFORE], i) {
e5b5ae50
LP
1048 Job *o;
1049
87f0e418
LP
1050 /* Is there a job for this unit? */
1051 if (!(o = hashmap_get(m->transaction_jobs, u)))
1ffba6fe
LP
1052
1053 /* Ok, there is no job for this in the
1054 * transaction, but maybe there is already one
1055 * running? */
87f0e418 1056 if (!(o = u->meta.job))
e5b5ae50
LP
1057 continue;
1058
398ef8ba 1059 if ((r = transaction_verify_order_one(m, o, j, generation, e)) < 0)
e5b5ae50
LP
1060 return r;
1061 }
1062
9f04bd52
LP
1063 /* Ok, let's backtrack, and remember that this entry is not on
1064 * our path anymore. */
1065 j->marker = NULL;
1066
e5b5ae50
LP
1067 return 0;
1068}
1069
398ef8ba 1070static int transaction_verify_order(Manager *m, unsigned *generation, DBusError *e) {
1ffba6fe
LP
1071 Job *j;
1072 int r;
034c6ed7 1073 Iterator i;
23e3c588 1074 unsigned g;
1ffba6fe 1075
e5b5ae50
LP
1076 assert(m);
1077 assert(generation);
1078
1ffba6fe
LP
1079 /* Check if the ordering graph is cyclic. If it is, try to fix
1080 * that up by dropping one of the jobs. */
e5b5ae50 1081
23e3c588
LP
1082 g = (*generation)++;
1083
034c6ed7 1084 HASHMAP_FOREACH(j, m->transaction_jobs, i)
398ef8ba 1085 if ((r = transaction_verify_order_one(m, j, NULL, g, e)) < 0)
1ffba6fe 1086 return r;
e5b5ae50
LP
1087
1088 return 0;
1089}
1090
1091static void transaction_collect_garbage(Manager *m) {
1092 bool again;
1093
1094 assert(m);
1095
1ffba6fe
LP
1096 /* Drop jobs that are not required by any other job */
1097
e5b5ae50 1098 do {
034c6ed7 1099 Iterator i;
e5b5ae50
LP
1100 Job *j;
1101
1102 again = false;
1103
034c6ed7 1104 HASHMAP_FOREACH(j, m->transaction_jobs, i) {
9b3d9090
LP
1105 if (j->object_list) {
1106 /* log_debug("Keeping job %s/%s because of %s/%s", */
1107 /* j->unit->meta.id, job_type_to_string(j->type), */
1108 /* j->object_list->subject ? j->object_list->subject->unit->meta.id : "root", */
1109 /* j->object_list->subject ? job_type_to_string(j->object_list->subject->type) : "root"); */
e5b5ae50 1110 continue;
9b3d9090 1111 }
e5b5ae50 1112
9b3d9090 1113 /* log_debug("Garbage collecting job %s/%s", j->unit->meta.id, job_type_to_string(j->type)); */
23a177ef 1114 transaction_delete_job(m, j, true);
e5b5ae50
LP
1115 again = true;
1116 break;
1117 }
1118
1119 } while (again);
1120}
1121
398ef8ba 1122static int transaction_is_destructive(Manager *m, DBusError *e) {
034c6ed7 1123 Iterator i;
e5b5ae50 1124 Job *j;
11dd41ce
LP
1125
1126 assert(m);
11dd41ce 1127
e5b5ae50
LP
1128 /* Checks whether applying this transaction means that
1129 * existing jobs would be replaced */
11dd41ce 1130
034c6ed7 1131 HASHMAP_FOREACH(j, m->transaction_jobs, i) {
e094e853
LP
1132
1133 /* Assume merged */
1134 assert(!j->transaction_prev);
1135 assert(!j->transaction_next);
1136
87f0e418
LP
1137 if (j->unit->meta.job &&
1138 j->unit->meta.job != j &&
398ef8ba
LP
1139 !job_type_is_superset(j->type, j->unit->meta.job->type)) {
1140
1141 dbus_set_error(e, BUS_ERROR_TRANSACTION_IS_DESTRUCTIVE, "Transaction is destructive.");
e5b5ae50 1142 return -EEXIST;
398ef8ba 1143 }
e094e853 1144 }
11dd41ce 1145
e5b5ae50
LP
1146 return 0;
1147}
1148
e094e853
LP
1149static void transaction_minimize_impact(Manager *m) {
1150 bool again;
1151 assert(m);
1152
1153 /* Drops all unnecessary jobs that reverse already active jobs
1154 * or that stop a running service. */
1155
1156 do {
1157 Job *j;
034c6ed7 1158 Iterator i;
e094e853
LP
1159
1160 again = false;
1161
034c6ed7
LP
1162 HASHMAP_FOREACH(j, m->transaction_jobs, i) {
1163 LIST_FOREACH(transaction, j, j) {
c20cae32 1164 bool stops_running_service, changes_existing_job;
e094e853
LP
1165
1166 /* If it matters, we shouldn't drop it */
1167 if (j->matters_to_anchor)
1168 continue;
1169
1170 /* Would this stop a running service?
1171 * Would this change an existing job?
1172 * If so, let's drop this entry */
c20cae32
LP
1173
1174 stops_running_service =
1175 j->type == JOB_STOP && UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(j->unit));
1176
1177 changes_existing_job =
143072ed
LP
1178 j->unit->meta.job &&
1179 job_type_is_conflicting(j->type, j->unit->meta.job->type);
c20cae32
LP
1180
1181 if (!stops_running_service && !changes_existing_job)
e094e853
LP
1182 continue;
1183
c20cae32 1184 if (stops_running_service)
d718bbb5 1185 log_debug("%s/%s would stop a running service.", j->unit->meta.id, job_type_to_string(j->type));
c20cae32
LP
1186
1187 if (changes_existing_job)
d718bbb5 1188 log_debug("%s/%s would change existing job.", j->unit->meta.id, job_type_to_string(j->type));
c20cae32 1189
e094e853 1190 /* Ok, let's get rid of this */
d718bbb5 1191 log_debug("Deleting %s/%s to minimize impact.", j->unit->meta.id, job_type_to_string(j->type));
c20cae32 1192
23a177ef 1193 transaction_delete_job(m, j, true);
e094e853
LP
1194 again = true;
1195 break;
1196 }
1197
1198 if (again)
1199 break;
1200 }
1201
1202 } while (again);
1203}
1204
4fe60156 1205static int transaction_apply(Manager *m, JobMode mode) {
034c6ed7 1206 Iterator i;
e5b5ae50
LP
1207 Job *j;
1208 int r;
1209
1ffba6fe
LP
1210 /* Moves the transaction jobs to the set of active jobs */
1211
4fe60156
LP
1212 if (mode == JOB_ISOLATE) {
1213
1214 /* When isolating first kill all installed jobs which
1215 * aren't part of the new transaction */
1216 HASHMAP_FOREACH(j, m->jobs, i) {
1217 assert(j->installed);
1218
1219 if (hashmap_get(m->transaction_jobs, j->unit))
1220 continue;
1221
1222 job_finish_and_invalidate(j, JOB_CANCELED);
1223 }
1224 }
1225
034c6ed7 1226 HASHMAP_FOREACH(j, m->transaction_jobs, i) {
e094e853
LP
1227 /* Assume merged */
1228 assert(!j->transaction_prev);
1229 assert(!j->transaction_next);
1230
ac1135be 1231 if (j->installed)
e5b5ae50
LP
1232 continue;
1233
1234 if ((r = hashmap_put(m->jobs, UINT32_TO_PTR(j->id), j)) < 0)
11dd41ce
LP
1235 goto rollback;
1236 }
1237
e5b5ae50 1238 while ((j = hashmap_steal_first(m->transaction_jobs))) {
9b3d9090
LP
1239 if (j->installed) {
1240 /* log_debug("Skipping already installed job %s/%s as %u", j->unit->meta.id, job_type_to_string(j->type), (unsigned) j->id); */
e5b5ae50 1241 continue;
9b3d9090 1242 }
e5b5ae50 1243
87f0e418
LP
1244 if (j->unit->meta.job)
1245 job_free(j->unit->meta.job);
11dd41ce 1246
87f0e418 1247 j->unit->meta.job = j;
ac1135be 1248 j->installed = true;
e409f875 1249 m->n_installed_jobs ++;
11dd41ce 1250
e5b5ae50
LP
1251 /* We're fully installed. Now let's free data we don't
1252 * need anymore. */
1253
1254 assert(!j->transaction_next);
1255 assert(!j->transaction_prev);
1256
c1e1601e
LP
1257 job_add_to_run_queue(j);
1258 job_add_to_dbus_queue(j);
312732cf 1259 job_start_timer(j);
28c3247e
LP
1260
1261 log_debug("Installed new job %s/%s as %u", j->unit->meta.id, job_type_to_string(j->type), (unsigned) j->id);
01184e04
LP
1262 }
1263
1264 /* As last step, kill all remaining job dependencies. */
f04fa1d5 1265 transaction_clean_dependencies(m);
1ffba6fe 1266
11dd41ce
LP
1267 return 0;
1268
1269rollback:
1270
034c6ed7 1271 HASHMAP_FOREACH(j, m->transaction_jobs, i) {
ac1135be 1272 if (j->installed)
e5b5ae50
LP
1273 continue;
1274
1275 hashmap_remove(m->jobs, UINT32_TO_PTR(j->id));
1276 }
1277
1278 return r;
1279}
1280
398ef8ba 1281static int transaction_activate(Manager *m, JobMode mode, DBusError *e) {
e5b5ae50
LP
1282 int r;
1283 unsigned generation = 1;
1284
1285 assert(m);
1286
1287 /* This applies the changes recorded in transaction_jobs to
1288 * the actual list of jobs, if possible. */
1289
1290 /* First step: figure out which jobs matter */
1291 transaction_find_jobs_that_matter_to_anchor(m, NULL, generation++);
1292
e094e853
LP
1293 /* Second step: Try not to stop any running services if
1294 * we don't have to. Don't try to reverse running
1295 * jobs if we don't have to. */
143072ed 1296 if (mode == JOB_FAIL)
c88e7f4e 1297 transaction_minimize_impact(m);
e094e853 1298
23a177ef
LP
1299 /* Third step: Drop redundant jobs */
1300 transaction_drop_redundant(m);
1301
1ffba6fe 1302 for (;;) {
23a177ef 1303 /* Fourth step: Let's remove unneeded jobs that might
1ffba6fe 1304 * be lurking. */
a8049b7a
LP
1305 if (mode != JOB_ISOLATE)
1306 transaction_collect_garbage(m);
e5b5ae50 1307
23a177ef 1308 /* Fifth step: verify order makes sense and correct
1ffba6fe 1309 * cycles if necessary and possible */
398ef8ba 1310 if ((r = transaction_verify_order(m, &generation, e)) >= 0)
1ffba6fe 1311 break;
e5b5ae50 1312
9f04bd52 1313 if (r != -EAGAIN) {
398ef8ba 1314 log_warning("Requested transaction contains an unfixable cyclic ordering dependency: %s", bus_error(e, r));
1ffba6fe 1315 goto rollback;
9f04bd52 1316 }
e5b5ae50 1317
1ffba6fe
LP
1318 /* Let's see if the resulting transaction ordering
1319 * graph is still cyclic... */
1320 }
1321
1322 for (;;) {
23a177ef 1323 /* Sixth step: let's drop unmergeable entries if
1ffba6fe
LP
1324 * necessary and possible, merge entries we can
1325 * merge */
398ef8ba 1326 if ((r = transaction_merge_jobs(m, e)) >= 0)
1ffba6fe
LP
1327 break;
1328
9f04bd52 1329 if (r != -EAGAIN) {
35b8ca3a 1330 log_warning("Requested transaction contains unmergeable jobs: %s", bus_error(e, r));
1ffba6fe 1331 goto rollback;
9f04bd52 1332 }
1ffba6fe 1333
23a177ef 1334 /* Seventh step: an entry got dropped, let's garbage
1ffba6fe 1335 * collect its dependencies. */
a8049b7a
LP
1336 if (mode != JOB_ISOLATE)
1337 transaction_collect_garbage(m);
1ffba6fe
LP
1338
1339 /* Let's see if the resulting transaction still has
5cb5a6ff 1340 * unmergeable entries ... */
1ffba6fe
LP
1341 }
1342
23a177ef
LP
1343 /* Eights step: Drop redundant jobs again, if the merging now allows us to drop more. */
1344 transaction_drop_redundant(m);
1345
1346 /* Ninth step: check whether we can actually apply this */
e5b5ae50 1347 if (mode == JOB_FAIL)
398ef8ba
LP
1348 if ((r = transaction_is_destructive(m, e)) < 0) {
1349 log_notice("Requested transaction contradicts existing jobs: %s", bus_error(e, r));
e5b5ae50 1350 goto rollback;
9f04bd52 1351 }
e5b5ae50 1352
23a177ef 1353 /* Tenth step: apply changes */
4fe60156 1354 if ((r = transaction_apply(m, mode)) < 0) {
54165a39 1355 log_warning("Failed to apply transaction: %s", strerror(-r));
e5b5ae50 1356 goto rollback;
9f04bd52 1357 }
e5b5ae50
LP
1358
1359 assert(hashmap_isempty(m->transaction_jobs));
1360 assert(!m->transaction_anchor);
1361
1362 return 0;
11dd41ce 1363
e5b5ae50 1364rollback:
11dd41ce
LP
1365 transaction_abort(m);
1366 return r;
1367}
1368
9e2f7c11 1369static Job* transaction_add_one_job(Manager *m, JobType type, Unit *unit, bool override, bool *is_new) {
e5b5ae50 1370 Job *j, *f;
60918275
LP
1371
1372 assert(m);
87f0e418 1373 assert(unit);
60918275 1374
35b8ca3a 1375 /* Looks for an existing prospective job and returns that. If
e5b5ae50
LP
1376 * it doesn't exist it is created and added to the prospective
1377 * jobs list. */
60918275 1378
87f0e418 1379 f = hashmap_get(m->transaction_jobs, unit);
60918275 1380
034c6ed7 1381 LIST_FOREACH(transaction, j, f) {
87f0e418 1382 assert(j->unit == unit);
60918275 1383
e5b5ae50
LP
1384 if (j->type == type) {
1385 if (is_new)
1386 *is_new = false;
1387 return j;
1388 }
1389 }
60918275 1390
87f0e418
LP
1391 if (unit->meta.job && unit->meta.job->type == type)
1392 j = unit->meta.job;
1393 else if (!(j = job_new(m, type, unit)))
e5b5ae50 1394 return NULL;
60918275 1395
e5b5ae50
LP
1396 j->generation = 0;
1397 j->marker = NULL;
1398 j->matters_to_anchor = false;
9e2f7c11 1399 j->override = override;
60918275 1400
034c6ed7
LP
1401 LIST_PREPEND(Job, transaction, f, j);
1402
e364ad06 1403 if (hashmap_replace(m->transaction_jobs, unit, f) < 0) {
034c6ed7
LP
1404 job_free(j);
1405 return NULL;
1406 }
1407
e5b5ae50
LP
1408 if (is_new)
1409 *is_new = true;
60918275 1410
9b3d9090 1411 /* log_debug("Added job %s/%s to transaction.", unit->meta.id, job_type_to_string(type)); */
23a177ef 1412
e5b5ae50
LP
1413 return j;
1414}
11dd41ce 1415
23a177ef 1416void manager_transaction_unlink_job(Manager *m, Job *j, bool delete_dependencies) {
e5b5ae50
LP
1417 assert(m);
1418 assert(j);
11dd41ce 1419
e5b5ae50
LP
1420 if (j->transaction_prev)
1421 j->transaction_prev->transaction_next = j->transaction_next;
1422 else if (j->transaction_next)
87f0e418 1423 hashmap_replace(m->transaction_jobs, j->unit, j->transaction_next);
e5b5ae50 1424 else
87f0e418 1425 hashmap_remove_value(m->transaction_jobs, j->unit, j);
e5b5ae50
LP
1426
1427 if (j->transaction_next)
1428 j->transaction_next->transaction_prev = j->transaction_prev;
1429
1430 j->transaction_prev = j->transaction_next = NULL;
1431
1432 while (j->subject_list)
1433 job_dependency_free(j->subject_list);
1e198baf
LP
1434
1435 while (j->object_list) {
1436 Job *other = j->object_list->matters ? j->object_list->subject : NULL;
1437
e5b5ae50 1438 job_dependency_free(j->object_list);
1e198baf 1439
23a177ef 1440 if (other && delete_dependencies) {
2e81c8a5 1441 log_debug("Deleting job %s/%s as dependency of job %s/%s",
9e2f7c11
LP
1442 other->unit->meta.id, job_type_to_string(other->type),
1443 j->unit->meta.id, job_type_to_string(j->type));
23a177ef 1444 transaction_delete_job(m, other, delete_dependencies);
1e198baf
LP
1445 }
1446 }
e5b5ae50
LP
1447}
1448
9e2f7c11
LP
1449static int transaction_add_job_and_dependencies(
1450 Manager *m,
1451 JobType type,
1452 Unit *unit,
1453 Job *by,
1454 bool matters,
1455 bool override,
69dd2852 1456 bool conflicts,
cebe0d41
LP
1457 bool ignore_requirements,
1458 bool ignore_order,
398ef8ba 1459 DBusError *e,
9e2f7c11 1460 Job **_ret) {
e5b5ae50 1461 Job *ret;
034c6ed7 1462 Iterator i;
87f0e418 1463 Unit *dep;
e5b5ae50
LP
1464 int r;
1465 bool is_new;
1466
1467 assert(m);
1468 assert(type < _JOB_TYPE_MAX);
87f0e418 1469 assert(unit);
e5b5ae50 1470
3aea3b35
LP
1471 /* log_debug("Pulling in %s/%s from %s/%s", */
1472 /* unit->meta.id, job_type_to_string(type), */
1473 /* by ? by->unit->meta.id : "NA", */
1474 /* by ? job_type_to_string(by->type) : "NA"); */
1475
00dc5d76
LP
1476 if (unit->meta.load_state != UNIT_LOADED &&
1477 unit->meta.load_state != UNIT_ERROR &&
6daf4f90 1478 unit->meta.load_state != UNIT_MASKED) {
8821a00f
LP
1479 dbus_set_error(e, BUS_ERROR_LOAD_FAILED, "Unit %s is not loaded properly.", unit->meta.id);
1480 return -EINVAL;
1481 }
1482
fdf20a31 1483 if (type != JOB_STOP && unit->meta.load_state == UNIT_ERROR) {
00dc5d76
LP
1484 dbus_set_error(e, BUS_ERROR_LOAD_FAILED,
1485 "Unit %s failed to load: %s. "
3661ac04 1486 "See system logs and 'systemctl status %s' for details.",
8821a00f 1487 unit->meta.id,
3661ac04
LP
1488 strerror(-unit->meta.load_error),
1489 unit->meta.id);
21b293e8 1490 return -EINVAL;
398ef8ba 1491 }
21b293e8 1492
6daf4f90
LP
1493 if (type != JOB_STOP && unit->meta.load_state == UNIT_MASKED) {
1494 dbus_set_error(e, BUS_ERROR_MASKED, "Unit %s is masked.", unit->meta.id);
00dc5d76
LP
1495 return -EINVAL;
1496 }
1497
398ef8ba
LP
1498 if (!unit_job_is_applicable(unit, type)) {
1499 dbus_set_error(e, BUS_ERROR_JOB_TYPE_NOT_APPLICABLE, "Job type %s is not applicable for unit %s.", job_type_to_string(type), unit->meta.id);
cd2dbd7d 1500 return -EBADR;
398ef8ba 1501 }
cd2dbd7d 1502
e5b5ae50 1503 /* First add the job. */
9e2f7c11 1504 if (!(ret = transaction_add_one_job(m, type, unit, override, &is_new)))
e5b5ae50
LP
1505 return -ENOMEM;
1506
cebe0d41 1507 ret->ignore_order = ret->ignore_order || ignore_order;
e67c3609 1508
e5b5ae50 1509 /* Then, add a link to the job. */
69dd2852 1510 if (!job_dependency_new(by, ret, matters, conflicts))
e5b5ae50
LP
1511 return -ENOMEM;
1512
cebe0d41 1513 if (is_new && !ignore_requirements) {
6210e7fc
LP
1514 Set *following;
1515
1516 /* If we are following some other unit, make sure we
1517 * add all dependencies of everybody following. */
1518 if (unit_following_set(ret->unit, &following) > 0) {
1519 SET_FOREACH(dep, following, i)
cebe0d41 1520 if ((r = transaction_add_job_and_dependencies(m, type, dep, ret, false, override, false, false, ignore_order, e, NULL)) < 0) {
6210e7fc
LP
1521 log_warning("Cannot add dependency job for unit %s, ignoring: %s", dep->meta.id, bus_error(e, r));
1522
1523 if (e)
1524 dbus_error_free(e);
1525 }
1526
1527 set_free(following);
1528 }
1529
e5b5ae50
LP
1530 /* Finally, recursively add in all dependencies. */
1531 if (type == JOB_START || type == JOB_RELOAD_OR_START) {
87f0e418 1532 SET_FOREACH(dep, ret->unit->meta.dependencies[UNIT_REQUIRES], i)
cebe0d41 1533 if ((r = transaction_add_job_and_dependencies(m, JOB_START, dep, ret, true, override, false, false, ignore_order, e, NULL)) < 0) {
41242c42
LP
1534 if (r != -EBADR)
1535 goto fail;
1536
1537 if (e)
1538 dbus_error_free(e);
1539 }
9e2f7c11 1540
b81884e7 1541 SET_FOREACH(dep, ret->unit->meta.dependencies[UNIT_BIND_TO], i)
cebe0d41 1542 if ((r = transaction_add_job_and_dependencies(m, JOB_START, dep, ret, true, override, false, false, ignore_order, e, NULL)) < 0) {
41242c42
LP
1543
1544 if (r != -EBADR)
1545 goto fail;
1546
1547 if (e)
1548 dbus_error_free(e);
1549 }
b81884e7 1550
9e2f7c11 1551 SET_FOREACH(dep, ret->unit->meta.dependencies[UNIT_REQUIRES_OVERRIDABLE], i)
cebe0d41 1552 if ((r = transaction_add_job_and_dependencies(m, JOB_START, dep, ret, !override, override, false, false, ignore_order, e, NULL)) < 0) {
65e92d67 1553 log_warning("Cannot add dependency job for unit %s, ignoring: %s", dep->meta.id, bus_error(e, r));
60dc72b5
LP
1554
1555 if (e)
1556 dbus_error_free(e);
65e92d67 1557 }
9e2f7c11 1558
87f0e418 1559 SET_FOREACH(dep, ret->unit->meta.dependencies[UNIT_WANTS], i)
cebe0d41 1560 if ((r = transaction_add_job_and_dependencies(m, JOB_START, dep, ret, false, false, false, false, ignore_order, e, NULL)) < 0) {
65e92d67 1561 log_warning("Cannot add dependency job for unit %s, ignoring: %s", dep->meta.id, bus_error(e, r));
60dc72b5
LP
1562
1563 if (e)
1564 dbus_error_free(e);
65e92d67 1565 }
9e2f7c11 1566
87f0e418 1567 SET_FOREACH(dep, ret->unit->meta.dependencies[UNIT_REQUISITE], i)
cebe0d41 1568 if ((r = transaction_add_job_and_dependencies(m, JOB_VERIFY_ACTIVE, dep, ret, true, override, false, false, ignore_order, e, NULL)) < 0) {
41242c42
LP
1569
1570 if (r != -EBADR)
1571 goto fail;
1572
1573 if (e)
1574 dbus_error_free(e);
1575 }
9e2f7c11
LP
1576
1577 SET_FOREACH(dep, ret->unit->meta.dependencies[UNIT_REQUISITE_OVERRIDABLE], i)
cebe0d41 1578 if ((r = transaction_add_job_and_dependencies(m, JOB_VERIFY_ACTIVE, dep, ret, !override, override, false, false, ignore_order, e, NULL)) < 0) {
65e92d67 1579 log_warning("Cannot add dependency job for unit %s, ignoring: %s", dep->meta.id, bus_error(e, r));
60dc72b5
LP
1580
1581 if (e)
1582 dbus_error_free(e);
65e92d67 1583 }
9e2f7c11 1584
87f0e418 1585 SET_FOREACH(dep, ret->unit->meta.dependencies[UNIT_CONFLICTS], i)
cebe0d41 1586 if ((r = transaction_add_job_and_dependencies(m, JOB_STOP, dep, ret, true, override, true, false, ignore_order, e, NULL)) < 0) {
41242c42
LP
1587
1588 if (r != -EBADR)
1589 goto fail;
1590
1591 if (e)
1592 dbus_error_free(e);
1593 }
69dd2852
LP
1594
1595 SET_FOREACH(dep, ret->unit->meta.dependencies[UNIT_CONFLICTED_BY], i)
cebe0d41 1596 if ((r = transaction_add_job_and_dependencies(m, JOB_STOP, dep, ret, false, override, false, false, ignore_order, e, NULL)) < 0) {
41242c42
LP
1597 log_warning("Cannot add dependency job for unit %s, ignoring: %s", dep->meta.id, bus_error(e, r));
1598
1599 if (e)
1600 dbus_error_free(e);
1601 }
e5b5ae50
LP
1602
1603 } else if (type == JOB_STOP || type == JOB_RESTART || type == JOB_TRY_RESTART) {
1604
87f0e418 1605 SET_FOREACH(dep, ret->unit->meta.dependencies[UNIT_REQUIRED_BY], i)
cebe0d41 1606 if ((r = transaction_add_job_and_dependencies(m, type, dep, ret, true, override, false, false, ignore_order, e, NULL)) < 0) {
41242c42
LP
1607
1608 if (r != -EBADR)
1609 goto fail;
1610
1611 if (e)
1612 dbus_error_free(e);
1613 }
b81884e7
LP
1614
1615 SET_FOREACH(dep, ret->unit->meta.dependencies[UNIT_BOUND_BY], i)
cebe0d41 1616 if ((r = transaction_add_job_and_dependencies(m, type, dep, ret, true, override, false, false, ignore_order, e, NULL)) < 0) {
41242c42
LP
1617
1618 if (r != -EBADR)
1619 goto fail;
1620
1621 if (e)
1622 dbus_error_free(e);
1623 }
e5b5ae50
LP
1624 }
1625
1626 /* JOB_VERIFY_STARTED, JOB_RELOAD require no dependency handling */
1627 }
60918275 1628
c0dafa48
LP
1629 if (_ret)
1630 *_ret = ret;
1631
60918275
LP
1632 return 0;
1633
1634fail:
e5b5ae50
LP
1635 return r;
1636}
1637
c497c7a9
LP
1638static int transaction_add_isolate_jobs(Manager *m) {
1639 Iterator i;
1640 Unit *u;
1641 char *k;
1642 int r;
1643
1644 assert(m);
1645
1646 HASHMAP_FOREACH_KEY(u, k, m->units, i) {
1647
1648 /* ignore aliases */
1649 if (u->meta.id != k)
1650 continue;
1651
c8f4d764 1652 if (u->meta.ignore_on_isolate)
c497c7a9
LP
1653 continue;
1654
1655 /* No need to stop inactive jobs */
2ce5c8f9 1656 if (UNIT_IS_INACTIVE_OR_FAILED(unit_active_state(u)) && !u->meta.job)
c497c7a9
LP
1657 continue;
1658
1659 /* Is there already something listed for this? */
1660 if (hashmap_get(m->transaction_jobs, u))
1661 continue;
1662
cebe0d41 1663 if ((r = transaction_add_job_and_dependencies(m, JOB_STOP, u, NULL, true, false, false, false, false, NULL, NULL)) < 0)
c497c7a9
LP
1664 log_warning("Cannot add isolate job for unit %s, ignoring: %s", u->meta.id, strerror(-r));
1665 }
1666
1667 return 0;
1668}
1669
398ef8ba 1670int manager_add_job(Manager *m, JobType type, Unit *unit, JobMode mode, bool override, DBusError *e, Job **_ret) {
e5b5ae50
LP
1671 int r;
1672 Job *ret;
1673
1674 assert(m);
1675 assert(type < _JOB_TYPE_MAX);
87f0e418 1676 assert(unit);
e5b5ae50 1677 assert(mode < _JOB_MODE_MAX);
60918275 1678
398ef8ba
LP
1679 if (mode == JOB_ISOLATE && type != JOB_START) {
1680 dbus_set_error(e, BUS_ERROR_INVALID_JOB_MODE, "Isolate is only valid for start.");
c497c7a9 1681 return -EINVAL;
398ef8ba 1682 }
c497c7a9 1683
2528a7a6
LP
1684 if (mode == JOB_ISOLATE && !unit->meta.allow_isolate) {
1685 dbus_set_error(e, BUS_ERROR_NO_ISOLATION, "Operation refused, unit may not be isolated.");
1686 return -EPERM;
1687 }
1688
e43ac878 1689 log_debug("Trying to enqueue job %s/%s/%s", unit->meta.id, job_type_to_string(type), job_mode_to_string(mode));
9f04bd52 1690
cebe0d41
LP
1691 if ((r = transaction_add_job_and_dependencies(m, type, unit, NULL, true, override, false,
1692 mode == JOB_IGNORE_DEPENDENCIES || mode == JOB_IGNORE_REQUIREMENTS,
1693 mode == JOB_IGNORE_DEPENDENCIES, e, &ret)) < 0) {
11dd41ce 1694 transaction_abort(m);
e5b5ae50
LP
1695 return r;
1696 }
11dd41ce 1697
c497c7a9
LP
1698 if (mode == JOB_ISOLATE)
1699 if ((r = transaction_add_isolate_jobs(m)) < 0) {
1700 transaction_abort(m);
1701 return r;
1702 }
1703
398ef8ba 1704 if ((r = transaction_activate(m, mode, e)) < 0)
e5b5ae50
LP
1705 return r;
1706
9e2f7c11 1707 log_debug("Enqueued job %s/%s as %u", unit->meta.id, job_type_to_string(type), (unsigned) ret->id);
f50e0a01 1708
e5b5ae50
LP
1709 if (_ret)
1710 *_ret = ret;
60918275 1711
e5b5ae50
LP
1712 return 0;
1713}
60918275 1714
398ef8ba 1715int manager_add_job_by_name(Manager *m, JobType type, const char *name, JobMode mode, bool override, DBusError *e, Job **_ret) {
28247076
LP
1716 Unit *unit;
1717 int r;
1718
1719 assert(m);
1720 assert(type < _JOB_TYPE_MAX);
1721 assert(name);
1722 assert(mode < _JOB_MODE_MAX);
1723
398ef8ba 1724 if ((r = manager_load_unit(m, name, NULL, NULL, &unit)) < 0)
28247076
LP
1725 return r;
1726
398ef8ba 1727 return manager_add_job(m, type, unit, mode, override, e, _ret);
28247076
LP
1728}
1729
60918275
LP
1730Job *manager_get_job(Manager *m, uint32_t id) {
1731 assert(m);
1732
1733 return hashmap_get(m->jobs, UINT32_TO_PTR(id));
1734}
1735
87f0e418 1736Unit *manager_get_unit(Manager *m, const char *name) {
60918275
LP
1737 assert(m);
1738 assert(name);
1739
87f0e418 1740 return hashmap_get(m->units, name);
60918275
LP
1741}
1742
c1e1601e 1743unsigned manager_dispatch_load_queue(Manager *m) {
60918275 1744 Meta *meta;
c1e1601e 1745 unsigned n = 0;
60918275
LP
1746
1747 assert(m);
1748
223dabab
LP
1749 /* Make sure we are not run recursively */
1750 if (m->dispatching_load_queue)
c1e1601e 1751 return 0;
223dabab
LP
1752
1753 m->dispatching_load_queue = true;
1754
87f0e418 1755 /* Dispatches the load queue. Takes a unit from the queue and
60918275
LP
1756 * tries to load its data until the queue is empty */
1757
1758 while ((meta = m->load_queue)) {
034c6ed7
LP
1759 assert(meta->in_load_queue);
1760
399ab2b1 1761 unit_load((Unit*) meta);
c1e1601e 1762 n++;
60918275
LP
1763 }
1764
223dabab 1765 m->dispatching_load_queue = false;
c1e1601e 1766 return n;
60918275
LP
1767}
1768
398ef8ba 1769int manager_load_unit_prepare(Manager *m, const char *name, const char *path, DBusError *e, Unit **_ret) {
87f0e418 1770 Unit *ret;
60918275
LP
1771 int r;
1772
1773 assert(m);
9e2f7c11 1774 assert(name || path);
60918275 1775
db06e3b6
LP
1776 /* This will prepare the unit for loading, but not actually
1777 * load anything from disk. */
0301abf4 1778
398ef8ba
LP
1779 if (path && !is_path(path)) {
1780 dbus_set_error(e, BUS_ERROR_INVALID_PATH, "Path %s is not absolute.", path);
9e2f7c11 1781 return -EINVAL;
398ef8ba 1782 }
9e2f7c11
LP
1783
1784 if (!name)
1785 name = file_name_from_path(path);
1786
b9c0d441 1787 if (!unit_name_is_valid(name, false)) {
398ef8ba 1788 dbus_set_error(e, BUS_ERROR_INVALID_NAME, "Unit name %s is not valid.", name);
9e2f7c11 1789 return -EINVAL;
398ef8ba 1790 }
60918275 1791
87f0e418 1792 if ((ret = manager_get_unit(m, name))) {
034c6ed7 1793 *_ret = ret;
413d6313 1794 return 1;
034c6ed7 1795 }
60918275 1796
87f0e418 1797 if (!(ret = unit_new(m)))
60918275
LP
1798 return -ENOMEM;
1799
9e2f7c11 1800 if (path)
6be1e7d5 1801 if (!(ret->meta.fragment_path = strdup(path))) {
0301abf4
LP
1802 unit_free(ret);
1803 return -ENOMEM;
1804 }
0301abf4 1805
87f0e418
LP
1806 if ((r = unit_add_name(ret, name)) < 0) {
1807 unit_free(ret);
1ffba6fe 1808 return r;
60918275
LP
1809 }
1810
87f0e418 1811 unit_add_to_load_queue(ret);
c1e1601e 1812 unit_add_to_dbus_queue(ret);
949061f0 1813 unit_add_to_gc_queue(ret);
c1e1601e 1814
db06e3b6
LP
1815 if (_ret)
1816 *_ret = ret;
1817
1818 return 0;
1819}
1820
398ef8ba 1821int manager_load_unit(Manager *m, const char *name, const char *path, DBusError *e, Unit **_ret) {
db06e3b6
LP
1822 int r;
1823
1824 assert(m);
1825
1826 /* This will load the service information files, but not actually
1827 * start any services or anything. */
1828
398ef8ba 1829 if ((r = manager_load_unit_prepare(m, name, path, e, _ret)) != 0)
db06e3b6
LP
1830 return r;
1831
f50e0a01 1832 manager_dispatch_load_queue(m);
60918275 1833
9e2f7c11 1834 if (_ret)
413d6313 1835 *_ret = unit_follow_merge(*_ret);
9e2f7c11 1836
60918275
LP
1837 return 0;
1838}
a66d02c3 1839
cea8e32e 1840void manager_dump_jobs(Manager *s, FILE *f, const char *prefix) {
034c6ed7 1841 Iterator i;
a66d02c3
LP
1842 Job *j;
1843
1844 assert(s);
1845 assert(f);
1846
034c6ed7 1847 HASHMAP_FOREACH(j, s->jobs, i)
cea8e32e 1848 job_dump(j, f, prefix);
a66d02c3
LP
1849}
1850
87f0e418 1851void manager_dump_units(Manager *s, FILE *f, const char *prefix) {
034c6ed7 1852 Iterator i;
87f0e418 1853 Unit *u;
11dd41ce 1854 const char *t;
a66d02c3
LP
1855
1856 assert(s);
1857 assert(f);
1858
87f0e418 1859 HASHMAP_FOREACH_KEY(u, t, s->units, i)
9e2f7c11 1860 if (u->meta.id == t)
87f0e418 1861 unit_dump(u, f, prefix);
a66d02c3 1862}
7fad411c
LP
1863
1864void manager_clear_jobs(Manager *m) {
1865 Job *j;
1866
1867 assert(m);
1868
1869 transaction_abort(m);
1870
1871 while ((j = hashmap_first(m->jobs)))
c77bc38d 1872 job_finish_and_invalidate(j, JOB_CANCELED);
7fad411c 1873}
83c60c9f 1874
c1e1601e 1875unsigned manager_dispatch_run_queue(Manager *m) {
83c60c9f 1876 Job *j;
c1e1601e 1877 unsigned n = 0;
83c60c9f 1878
034c6ed7 1879 if (m->dispatching_run_queue)
c1e1601e 1880 return 0;
034c6ed7
LP
1881
1882 m->dispatching_run_queue = true;
9152c765 1883
034c6ed7 1884 while ((j = m->run_queue)) {
ac1135be 1885 assert(j->installed);
034c6ed7
LP
1886 assert(j->in_run_queue);
1887
1888 job_run_and_invalidate(j);
c1e1601e 1889 n++;
9152c765 1890 }
034c6ed7
LP
1891
1892 m->dispatching_run_queue = false;
c1e1601e
LP
1893 return n;
1894}
1895
1896unsigned manager_dispatch_dbus_queue(Manager *m) {
1897 Job *j;
1898 Meta *meta;
1899 unsigned n = 0;
1900
1901 assert(m);
1902
1903 if (m->dispatching_dbus_queue)
1904 return 0;
1905
1906 m->dispatching_dbus_queue = true;
1907
1908 while ((meta = m->dbus_unit_queue)) {
23a177ef 1909 assert(meta->in_dbus_queue);
c1e1601e 1910
399ab2b1 1911 bus_unit_send_change_signal((Unit*) meta);
c1e1601e
LP
1912 n++;
1913 }
1914
1915 while ((j = m->dbus_job_queue)) {
1916 assert(j->in_dbus_queue);
1917
1918 bus_job_send_change_signal(j);
1919 n++;
1920 }
1921
1922 m->dispatching_dbus_queue = false;
1923 return n;
9152c765
LP
1924}
1925
8c47c732
LP
1926static int manager_process_notify_fd(Manager *m) {
1927 ssize_t n;
1928
1929 assert(m);
1930
1931 for (;;) {
1932 char buf[4096];
1933 struct msghdr msghdr;
1934 struct iovec iovec;
1935 struct ucred *ucred;
1936 union {
1937 struct cmsghdr cmsghdr;
1938 uint8_t buf[CMSG_SPACE(sizeof(struct ucred))];
1939 } control;
1940 Unit *u;
1941 char **tags;
1942
1943 zero(iovec);
1944 iovec.iov_base = buf;
1945 iovec.iov_len = sizeof(buf)-1;
1946
1947 zero(control);
1948 zero(msghdr);
1949 msghdr.msg_iov = &iovec;
1950 msghdr.msg_iovlen = 1;
1951 msghdr.msg_control = &control;
1952 msghdr.msg_controllen = sizeof(control);
1953
1954 if ((n = recvmsg(m->notify_watch.fd, &msghdr, MSG_DONTWAIT)) <= 0) {
1955 if (n >= 0)
1956 return -EIO;
1957
f6144808 1958 if (errno == EAGAIN || errno == EINTR)
8c47c732
LP
1959 break;
1960
1961 return -errno;
1962 }
1963
1964 if (msghdr.msg_controllen < CMSG_LEN(sizeof(struct ucred)) ||
1965 control.cmsghdr.cmsg_level != SOL_SOCKET ||
1966 control.cmsghdr.cmsg_type != SCM_CREDENTIALS ||
1967 control.cmsghdr.cmsg_len != CMSG_LEN(sizeof(struct ucred))) {
1968 log_warning("Received notify message without credentials. Ignoring.");
1969 continue;
1970 }
1971
1972 ucred = (struct ucred*) CMSG_DATA(&control.cmsghdr);
1973
c6c18be3 1974 if (!(u = hashmap_get(m->watch_pids, LONG_TO_PTR(ucred->pid))))
8c47c732
LP
1975 if (!(u = cgroup_unit_by_pid(m, ucred->pid))) {
1976 log_warning("Cannot find unit for notify message of PID %lu.", (unsigned long) ucred->pid);
1977 continue;
1978 }
1979
8c40acf7
LP
1980 assert((size_t) n < sizeof(buf));
1981 buf[n] = 0;
8c47c732
LP
1982 if (!(tags = strv_split(buf, "\n\r")))
1983 return -ENOMEM;
1984
1985 log_debug("Got notification message for unit %s", u->meta.id);
1986
1987 if (UNIT_VTABLE(u)->notify_message)
c952c6ec 1988 UNIT_VTABLE(u)->notify_message(u, ucred->pid, tags);
8c47c732
LP
1989
1990 strv_free(tags);
1991 }
1992
1993 return 0;
1994}
1995
034c6ed7 1996static int manager_dispatch_sigchld(Manager *m) {
9152c765
LP
1997 assert(m);
1998
1999 for (;;) {
2000 siginfo_t si;
87f0e418 2001 Unit *u;
8c47c732 2002 int r;
9152c765
LP
2003
2004 zero(si);
4112df16
LP
2005
2006 /* First we call waitd() for a PID and do not reap the
2007 * zombie. That way we can still access /proc/$PID for
2008 * it while it is a zombie. */
2009 if (waitid(P_ALL, 0, &si, WEXITED|WNOHANG|WNOWAIT) < 0) {
acbb0225
LP
2010
2011 if (errno == ECHILD)
2012 break;
2013
4112df16
LP
2014 if (errno == EINTR)
2015 continue;
2016
9152c765 2017 return -errno;
acbb0225 2018 }
9152c765 2019
4112df16 2020 if (si.si_pid <= 0)
9152c765
LP
2021 break;
2022
15d5d9d9 2023 if (si.si_code == CLD_EXITED || si.si_code == CLD_KILLED || si.si_code == CLD_DUMPED) {
4112df16
LP
2024 char *name = NULL;
2025
2026 get_process_name(si.si_pid, &name);
bb00e604 2027 log_debug("Got SIGCHLD for process %lu (%s)", (unsigned long) si.si_pid, strna(name));
4112df16
LP
2028 free(name);
2029 }
2030
8c47c732
LP
2031 /* Let's flush any message the dying child might still
2032 * have queued for us. This ensures that the process
2033 * still exists in /proc so that we can figure out
2034 * which cgroup and hence unit it belongs to. */
2035 if ((r = manager_process_notify_fd(m)) < 0)
2036 return r;
2037
2038 /* And now figure out the unit this belongs to */
c6c18be3 2039 if (!(u = hashmap_get(m->watch_pids, LONG_TO_PTR(si.si_pid))))
8c47c732
LP
2040 u = cgroup_unit_by_pid(m, si.si_pid);
2041
4112df16
LP
2042 /* And now, we actually reap the zombie. */
2043 if (waitid(P_PID, si.si_pid, &si, WEXITED) < 0) {
2044 if (errno == EINTR)
2045 continue;
2046
2047 return -errno;
2048 }
2049
034c6ed7
LP
2050 if (si.si_code != CLD_EXITED && si.si_code != CLD_KILLED && si.si_code != CLD_DUMPED)
2051 continue;
2052
bb00e604
LP
2053 log_debug("Child %lu died (code=%s, status=%i/%s)",
2054 (long unsigned) si.si_pid,
4112df16
LP
2055 sigchld_code_to_string(si.si_code),
2056 si.si_status,
d06dacd0
LP
2057 strna(si.si_code == CLD_EXITED
2058 ? exit_status_to_string(si.si_status, EXIT_STATUS_FULL)
2059 : signal_to_string(si.si_status)));
acbb0225 2060
8c47c732 2061 if (!u)
9152c765
LP
2062 continue;
2063
c6c18be3 2064 log_debug("Child %lu belongs to %s", (long unsigned) si.si_pid, u->meta.id);
6c1a0478 2065
c6c18be3 2066 hashmap_remove(m->watch_pids, LONG_TO_PTR(si.si_pid));
87f0e418 2067 UNIT_VTABLE(u)->sigchld_event(u, si.si_pid, si.si_code, si.si_status);
9152c765
LP
2068 }
2069
2070 return 0;
2071}
2072
7d793605 2073static int manager_start_target(Manager *m, const char *name, JobMode mode) {
28247076 2074 int r;
398ef8ba
LP
2075 DBusError error;
2076
2077 dbus_error_init(&error);
2078
af2d49f7 2079 log_debug("Activating special unit %s", name);
1e001f52 2080
398ef8ba
LP
2081 if ((r = manager_add_job_by_name(m, JOB_START, name, mode, true, &error, NULL)) < 0)
2082 log_error("Failed to enqueue %s job: %s", name, bus_error(&error, r));
28247076 2083
398ef8ba 2084 dbus_error_free(&error);
a1b256b0
LP
2085
2086 return r;
28247076
LP
2087}
2088
a16e1123 2089static int manager_process_signal_fd(Manager *m) {
9152c765
LP
2090 ssize_t n;
2091 struct signalfd_siginfo sfsi;
2092 bool sigchld = false;
2093
2094 assert(m);
2095
2096 for (;;) {
acbb0225 2097 if ((n = read(m->signal_watch.fd, &sfsi, sizeof(sfsi))) != sizeof(sfsi)) {
9152c765
LP
2098
2099 if (n >= 0)
2100 return -EIO;
2101
63090775 2102 if (errno == EINTR || errno == EAGAIN)
acbb0225 2103 break;
9152c765
LP
2104
2105 return -errno;
2106 }
2107
67370238
LP
2108 if (sfsi.ssi_pid > 0) {
2109 char *p = NULL;
2110
dfa7f7e1
LP
2111 get_process_name(sfsi.ssi_pid, &p);
2112
67370238
LP
2113 log_debug("Received SIG%s from PID %lu (%s).",
2114 strna(signal_to_string(sfsi.ssi_signo)),
2115 (unsigned long) sfsi.ssi_pid, strna(p));
2116 free(p);
2117 } else
2118 log_debug("Received SIG%s.", strna(signal_to_string(sfsi.ssi_signo)));
1e001f52 2119
b9cd2ec1
LP
2120 switch (sfsi.ssi_signo) {
2121
4112df16 2122 case SIGCHLD:
9152c765 2123 sigchld = true;
b9cd2ec1
LP
2124 break;
2125
6632c602 2126 case SIGTERM:
a3d4e06d 2127 if (m->running_as == MANAGER_SYSTEM) {
db06e3b6
LP
2128 /* This is for compatibility with the
2129 * original sysvinit */
e11dc4a2 2130 m->exit_code = MANAGER_REEXECUTE;
a1b256b0
LP
2131 break;
2132 }
84e9af1e 2133
a1b256b0 2134 /* Fall through */
e11dc4a2
LP
2135
2136 case SIGINT:
a3d4e06d 2137 if (m->running_as == MANAGER_SYSTEM) {
7d793605 2138 manager_start_target(m, SPECIAL_CTRL_ALT_DEL_TARGET, JOB_REPLACE);
84e9af1e
LP
2139 break;
2140 }
2141
a1b256b0 2142 /* Run the exit target if there is one, if not, just exit. */
0003d1ab 2143 if (manager_start_target(m, SPECIAL_EXIT_TARGET, JOB_REPLACE) < 0) {
a1b256b0
LP
2144 m->exit_code = MANAGER_EXIT;
2145 return 0;
2146 }
2147
2148 break;
84e9af1e 2149
28247076 2150 case SIGWINCH:
a3d4e06d 2151 if (m->running_as == MANAGER_SYSTEM)
7d793605 2152 manager_start_target(m, SPECIAL_KBREQUEST_TARGET, JOB_REPLACE);
84e9af1e 2153
28247076
LP
2154 /* This is a nop on non-init */
2155 break;
84e9af1e 2156
28247076 2157 case SIGPWR:
a3d4e06d 2158 if (m->running_as == MANAGER_SYSTEM)
7d793605 2159 manager_start_target(m, SPECIAL_SIGPWR_TARGET, JOB_REPLACE);
84e9af1e 2160
28247076 2161 /* This is a nop on non-init */
84e9af1e 2162 break;
6632c602 2163
1005d14f 2164 case SIGUSR1: {
57ee42ce
LP
2165 Unit *u;
2166
2167 u = manager_get_unit(m, SPECIAL_DBUS_SERVICE);
2168
2169 if (!u || UNIT_IS_ACTIVE_OR_RELOADING(unit_active_state(u))) {
2170 log_info("Trying to reconnect to bus...");
3996fbe2 2171 bus_init(m, true);
57ee42ce
LP
2172 }
2173
2174 if (!u || !UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(u))) {
2175 log_info("Loading D-Bus service...");
7d793605 2176 manager_start_target(m, SPECIAL_DBUS_SERVICE, JOB_REPLACE);
57ee42ce
LP
2177 }
2178
2179 break;
2180 }
2181
2149e37c
LP
2182 case SIGUSR2: {
2183 FILE *f;
2184 char *dump = NULL;
2185 size_t size;
2186
2187 if (!(f = open_memstream(&dump, &size))) {
2188 log_warning("Failed to allocate memory stream.");
2189 break;
2190 }
2191
2192 manager_dump_units(m, f, "\t");
2193 manager_dump_jobs(m, f, "\t");
2194
2195 if (ferror(f)) {
2196 fclose(f);
2197 free(dump);
2198 log_warning("Failed to write status stream");
2199 break;
2200 }
2201
2202 fclose(f);
2203 log_dump(LOG_INFO, dump);
2204 free(dump);
2205
1005d14f 2206 break;
2149e37c 2207 }
1005d14f 2208
a16e1123
LP
2209 case SIGHUP:
2210 m->exit_code = MANAGER_RELOAD;
2211 break;
2212
7d793605 2213 default: {
253ee27a 2214
0003d1ab
LP
2215 /* Starting SIGRTMIN+0 */
2216 static const char * const target_table[] = {
7d793605
LP
2217 [0] = SPECIAL_DEFAULT_TARGET,
2218 [1] = SPECIAL_RESCUE_TARGET,
f057408c 2219 [2] = SPECIAL_EMERGENCY_TARGET,
7d793605
LP
2220 [3] = SPECIAL_HALT_TARGET,
2221 [4] = SPECIAL_POWEROFF_TARGET,
0003d1ab
LP
2222 [5] = SPECIAL_REBOOT_TARGET,
2223 [6] = SPECIAL_KEXEC_TARGET
2224 };
2225
2226 /* Starting SIGRTMIN+13, so that target halt and system halt are 10 apart */
2227 static const ManagerExitCode code_table[] = {
2228 [0] = MANAGER_HALT,
2229 [1] = MANAGER_POWEROFF,
2230 [2] = MANAGER_REBOOT,
2231 [3] = MANAGER_KEXEC
7d793605
LP
2232 };
2233
2234 if ((int) sfsi.ssi_signo >= SIGRTMIN+0 &&
0003d1ab 2235 (int) sfsi.ssi_signo < SIGRTMIN+(int) ELEMENTSOF(target_table)) {
764e9b5f
MS
2236 int idx = (int) sfsi.ssi_signo - SIGRTMIN;
2237 manager_start_target(m, target_table[idx],
2238 (idx == 1 || idx == 2) ? JOB_ISOLATE : JOB_REPLACE);
7d793605
LP
2239 break;
2240 }
2241
0003d1ab
LP
2242 if ((int) sfsi.ssi_signo >= SIGRTMIN+13 &&
2243 (int) sfsi.ssi_signo < SIGRTMIN+13+(int) ELEMENTSOF(code_table)) {
2244 m->exit_code = code_table[sfsi.ssi_signo - SIGRTMIN - 13];
2245 break;
2246 }
2247
0658666b
LP
2248 switch (sfsi.ssi_signo - SIGRTMIN) {
2249
2250 case 20:
2251 log_debug("Enabling showing of status.");
27d340c7 2252 manager_set_show_status(m, true);
0658666b
LP
2253 break;
2254
2255 case 21:
2256 log_debug("Disabling showing of status.");
27d340c7 2257 manager_set_show_status(m, false);
0658666b
LP
2258 break;
2259
253ee27a
LP
2260 case 22:
2261 log_set_max_level(LOG_DEBUG);
2262 log_notice("Setting log level to debug.");
2263 break;
2264
2265 case 23:
2266 log_set_max_level(LOG_INFO);
2267 log_notice("Setting log level to info.");
2268 break;
2269
2270 case 27:
2271 log_set_target(LOG_TARGET_CONSOLE);
2272 log_notice("Setting log target to console.");
2273 break;
2274
2275 case 28:
2276 log_set_target(LOG_TARGET_KMSG);
2277 log_notice("Setting log target to kmsg.");
2278 break;
2279
2280 case 29:
2281 log_set_target(LOG_TARGET_SYSLOG_OR_KMSG);
2282 log_notice("Setting log target to syslog-or-kmsg.");
2283 break;
2284
0658666b
LP
2285 default:
2286 log_warning("Got unhandled signal <%s>.", strna(signal_to_string(sfsi.ssi_signo)));
2287 }
b9cd2ec1 2288 }
7d793605 2289 }
9152c765
LP
2290 }
2291
2292 if (sigchld)
034c6ed7
LP
2293 return manager_dispatch_sigchld(m);
2294
2295 return 0;
2296}
2297
a16e1123 2298static int process_event(Manager *m, struct epoll_event *ev) {
034c6ed7 2299 int r;
acbb0225 2300 Watch *w;
034c6ed7
LP
2301
2302 assert(m);
2303 assert(ev);
2304
3df5bf61 2305 assert_se(w = ev->data.ptr);
034c6ed7 2306
40dde66f
LP
2307 if (w->type == WATCH_INVALID)
2308 return 0;
2309
acbb0225 2310 switch (w->type) {
034c6ed7 2311
ef734fd6 2312 case WATCH_SIGNAL:
034c6ed7 2313
acbb0225 2314 /* An incoming signal? */
f94ea366 2315 if (ev->events != EPOLLIN)
acbb0225 2316 return -EINVAL;
034c6ed7 2317
a16e1123 2318 if ((r = manager_process_signal_fd(m)) < 0)
acbb0225 2319 return r;
034c6ed7 2320
acbb0225 2321 break;
034c6ed7 2322
8c47c732
LP
2323 case WATCH_NOTIFY:
2324
2325 /* An incoming daemon notification event? */
2326 if (ev->events != EPOLLIN)
2327 return -EINVAL;
2328
2329 if ((r = manager_process_notify_fd(m)) < 0)
2330 return r;
2331
2332 break;
2333
acbb0225 2334 case WATCH_FD:
034c6ed7 2335
acbb0225 2336 /* Some fd event, to be dispatched to the units */
ea430986 2337 UNIT_VTABLE(w->data.unit)->fd_event(w->data.unit, w->fd, ev->events, w);
acbb0225 2338 break;
034c6ed7 2339
faf919f1
LP
2340 case WATCH_UNIT_TIMER:
2341 case WATCH_JOB_TIMER: {
acbb0225
LP
2342 uint64_t v;
2343 ssize_t k;
034c6ed7 2344
acbb0225 2345 /* Some timer event, to be dispatched to the units */
be888ebb 2346 if ((k = read(w->fd, &v, sizeof(v))) != sizeof(v)) {
034c6ed7 2347
acbb0225
LP
2348 if (k < 0 && (errno == EINTR || errno == EAGAIN))
2349 break;
034c6ed7 2350
acbb0225 2351 return k < 0 ? -errno : -EIO;
034c6ed7
LP
2352 }
2353
faf919f1
LP
2354 if (w->type == WATCH_UNIT_TIMER)
2355 UNIT_VTABLE(w->data.unit)->timer_event(w->data.unit, v, w);
2356 else
2357 job_timer_event(w->data.job, v, w);
acbb0225
LP
2358 break;
2359 }
2360
ef734fd6
LP
2361 case WATCH_MOUNT:
2362 /* Some mount table change, intended for the mount subsystem */
2363 mount_fd_event(m, ev->events);
2364 break;
2365
4e434314
LP
2366 case WATCH_SWAP:
2367 /* Some swap table change, intended for the swap subsystem */
2368 swap_fd_event(m, ev->events);
2369 break;
2370
f94ea366
LP
2371 case WATCH_UDEV:
2372 /* Some notification from udev, intended for the device subsystem */
2373 device_fd_event(m, ev->events);
2374 break;
2375
ea430986
LP
2376 case WATCH_DBUS_WATCH:
2377 bus_watch_event(m, w, ev->events);
2378 break;
2379
2380 case WATCH_DBUS_TIMEOUT:
2381 bus_timeout_event(m, w, ev->events);
2382 break;
2383
acbb0225 2384 default:
c5fd1e57 2385 log_error("event type=%i", w->type);
acbb0225 2386 assert_not_reached("Unknown epoll event type.");
034c6ed7 2387 }
9152c765
LP
2388
2389 return 0;
2390}
2391
2392int manager_loop(Manager *m) {
2393 int r;
9152c765 2394
fac9f8df 2395 RATELIMIT_DEFINE(rl, 1*USEC_PER_SEC, 50000);
ea430986 2396
9152c765 2397 assert(m);
a16e1123 2398 m->exit_code = MANAGER_RUNNING;
9152c765 2399
fe51822e
LP
2400 /* Release the path cache */
2401 set_free_free(m->unit_path_cache);
2402 m->unit_path_cache = NULL;
2403
b0c918b9
LP
2404 manager_check_finished(m);
2405
a4312405
LP
2406 /* There might still be some zombies hanging around from
2407 * before we were exec()'ed. Leat's reap them */
2408 if ((r = manager_dispatch_sigchld(m)) < 0)
2409 return r;
2410
a16e1123 2411 while (m->exit_code == MANAGER_RUNNING) {
957ca890
LP
2412 struct epoll_event event;
2413 int n;
9152c765 2414
ea430986
LP
2415 if (!ratelimit_test(&rl)) {
2416 /* Yay, something is going seriously wrong, pause a little */
2417 log_warning("Looping too fast. Throttling execution a little.");
2418 sleep(1);
2419 }
2420
37a8e683 2421 if (manager_dispatch_load_queue(m) > 0)
23a177ef
LP
2422 continue;
2423
37a8e683 2424 if (manager_dispatch_run_queue(m) > 0)
701cc384
LP
2425 continue;
2426
37a8e683 2427 if (bus_dispatch(m) > 0)
c1e1601e 2428 continue;
034c6ed7 2429
37a8e683 2430 if (manager_dispatch_cleanup_queue(m) > 0)
c1e1601e
LP
2431 continue;
2432
37a8e683 2433 if (manager_dispatch_gc_queue(m) > 0)
c1e1601e
LP
2434 continue;
2435
2436 if (manager_dispatch_dbus_queue(m) > 0)
ea430986 2437 continue;
ea430986 2438
e04aad61
LP
2439 if (swap_dispatch_reload(m) > 0)
2440 continue;
2441
957ca890 2442 if ((n = epoll_wait(m->epoll_fd, &event, 1, -1)) < 0) {
9152c765 2443
6089f4a9 2444 if (errno == EINTR)
9152c765
LP
2445 continue;
2446
2447 return -errno;
2448 }
2449
957ca890 2450 assert(n == 1);
b9cd2ec1 2451
a16e1123 2452 if ((r = process_event(m, &event)) < 0)
957ca890 2453 return r;
a16e1123 2454 }
957ca890 2455
a16e1123 2456 return m->exit_code;
83c60c9f 2457}
ea430986
LP
2458
2459int manager_get_unit_from_dbus_path(Manager *m, const char *s, Unit **_u) {
2460 char *n;
2461 Unit *u;
2462
2463 assert(m);
2464 assert(s);
2465 assert(_u);
2466
2467 if (!startswith(s, "/org/freedesktop/systemd1/unit/"))
2468 return -EINVAL;
2469
2470 if (!(n = bus_path_unescape(s+31)))
2471 return -ENOMEM;
2472
2473 u = manager_get_unit(m, n);
2474 free(n);
2475
2476 if (!u)
2477 return -ENOENT;
2478
2479 *_u = u;
2480
2481 return 0;
2482}
86fbf370
LP
2483
2484int manager_get_job_from_dbus_path(Manager *m, const char *s, Job **_j) {
2485 Job *j;
2486 unsigned id;
2487 int r;
2488
2489 assert(m);
2490 assert(s);
2491 assert(_j);
2492
2493 if (!startswith(s, "/org/freedesktop/systemd1/job/"))
2494 return -EINVAL;
2495
2496 if ((r = safe_atou(s + 30, &id)) < 0)
2497 return r;
2498
2499 if (!(j = manager_get_job(m, id)))
2500 return -ENOENT;
2501
2502 *_j = j;
2503
2504 return 0;
2505}
dfcd764e 2506
4927fcae 2507void manager_send_unit_audit(Manager *m, Unit *u, int type, bool success) {
e537352b 2508
4927fcae
LP
2509#ifdef HAVE_AUDIT
2510 char *p;
e537352b 2511
4927fcae 2512 if (m->audit_fd < 0)
e537352b
LP
2513 return;
2514
bbd3a7ba
LP
2515 /* Don't generate audit events if the service was already
2516 * started and we're just deserializing */
a7556052 2517 if (m->n_reloading > 0)
bbd3a7ba
LP
2518 return;
2519
f1dd0c3f
LP
2520 if (m->running_as != MANAGER_SYSTEM)
2521 return;
2522
2523 if (u->meta.type != UNIT_SERVICE)
2524 return;
2525
4927fcae
LP
2526 if (!(p = unit_name_to_prefix_and_instance(u->meta.id))) {
2527 log_error("Failed to allocate unit name for audit message: %s", strerror(ENOMEM));
e537352b
LP
2528 return;
2529 }
2530
391ade86
LP
2531 if (audit_log_user_comm_message(m->audit_fd, type, "", p, NULL, NULL, NULL, success) < 0) {
2532 log_warning("Failed to send audit message: %m");
2533
2534 if (errno == EPERM) {
2535 /* We aren't allowed to send audit messages?
2536 * Then let's not retry again, to avoid
2537 * spamming the user with the same and same
2538 * messages over and over. */
2539
2540 audit_close(m->audit_fd);
2541 m->audit_fd = -1;
2542 }
2543 }
e537352b 2544
4927fcae
LP
2545 free(p);
2546#endif
e537352b 2547
e537352b
LP
2548}
2549
e983b760
LP
2550void manager_send_unit_plymouth(Manager *m, Unit *u) {
2551 int fd = -1;
2552 union sockaddr_union sa;
2553 int n = 0;
2554 char *message = NULL;
e983b760
LP
2555
2556 /* Don't generate plymouth events if the service was already
2557 * started and we're just deserializing */
a7556052 2558 if (m->n_reloading > 0)
e983b760
LP
2559 return;
2560
2561 if (m->running_as != MANAGER_SYSTEM)
2562 return;
2563
2564 if (u->meta.type != UNIT_SERVICE &&
2565 u->meta.type != UNIT_MOUNT &&
2566 u->meta.type != UNIT_SWAP)
2567 return;
2568
2569 /* We set SOCK_NONBLOCK here so that we rather drop the
2570 * message then wait for plymouth */
2571 if ((fd = socket(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0)) < 0) {
2572 log_error("socket() failed: %m");
2573 return;
2574 }
2575
2576 zero(sa);
2577 sa.sa.sa_family = AF_UNIX;
96707269
LP
2578 strncpy(sa.un.sun_path+1, "/org/freedesktop/plymouthd", sizeof(sa.un.sun_path)-1);
2579 if (connect(fd, &sa.sa, offsetof(struct sockaddr_un, sun_path) + 1 + strlen(sa.un.sun_path+1)) < 0) {
e983b760
LP
2580
2581 if (errno != EPIPE &&
2582 errno != EAGAIN &&
2583 errno != ENOENT &&
2584 errno != ECONNREFUSED &&
2585 errno != ECONNRESET &&
2586 errno != ECONNABORTED)
2587 log_error("connect() failed: %m");
2588
2589 goto finish;
2590 }
2591
2592 if (asprintf(&message, "U\002%c%s%n", (int) (strlen(u->meta.id) + 1), u->meta.id, &n) < 0) {
2593 log_error("Out of memory");
2594 goto finish;
2595 }
2596
2597 errno = 0;
bd40a2d8 2598 if (write(fd, message, n + 1) != n + 1) {
e983b760
LP
2599
2600 if (errno != EPIPE &&
2601 errno != EAGAIN &&
2602 errno != ENOENT &&
2603 errno != ECONNREFUSED &&
2604 errno != ECONNRESET &&
2605 errno != ECONNABORTED)
2606 log_error("Failed to write Plymouth message: %m");
2607
2608 goto finish;
2609 }
2610
2611finish:
2612 if (fd >= 0)
2613 close_nointr_nofail(fd);
2614
2615 free(message);
2616}
2617
05e343b7
LP
2618void manager_dispatch_bus_name_owner_changed(
2619 Manager *m,
2620 const char *name,
2621 const char* old_owner,
2622 const char *new_owner) {
2623
2624 Unit *u;
2625
2626 assert(m);
2627 assert(name);
2628
2629 if (!(u = hashmap_get(m->watch_bus, name)))
2630 return;
2631
2632 UNIT_VTABLE(u)->bus_name_owner_change(u, name, old_owner, new_owner);
2633}
2634
2635void manager_dispatch_bus_query_pid_done(
2636 Manager *m,
2637 const char *name,
2638 pid_t pid) {
2639
2640 Unit *u;
2641
2642 assert(m);
2643 assert(name);
2644 assert(pid >= 1);
2645
2646 if (!(u = hashmap_get(m->watch_bus, name)))
2647 return;
2648
2649 UNIT_VTABLE(u)->bus_query_pid_done(u, name, pid);
2650}
2651
d8d5ab98 2652int manager_open_serialization(Manager *m, FILE **_f) {
b925e726 2653 char *path = NULL;
a16e1123
LP
2654 mode_t saved_umask;
2655 int fd;
2656 FILE *f;
2657
2658 assert(_f);
2659
b925e726 2660 if (m->running_as == MANAGER_SYSTEM)
2b583ce6 2661 asprintf(&path, "/run/systemd/dump-%lu-XXXXXX", (unsigned long) getpid());
b925e726
LP
2662 else
2663 asprintf(&path, "/tmp/systemd-dump-%lu-XXXXXX", (unsigned long) getpid());
d8d5ab98 2664
b925e726
LP
2665 if (!path)
2666 return -ENOMEM;
a16e1123
LP
2667
2668 saved_umask = umask(0077);
2669 fd = mkostemp(path, O_RDWR|O_CLOEXEC);
2670 umask(saved_umask);
2671
2672 if (fd < 0) {
2673 free(path);
2674 return -errno;
2675 }
2676
2677 unlink(path);
2678
2679 log_debug("Serializing state to %s", path);
2680 free(path);
2681
da19d5c1 2682 if (!(f = fdopen(fd, "w+")))
a16e1123
LP
2683 return -errno;
2684
2685 *_f = f;
2686
2687 return 0;
2688}
2689
2690int manager_serialize(Manager *m, FILE *f, FDSet *fds) {
2691 Iterator i;
2692 Unit *u;
2693 const char *t;
2694 int r;
2695
2696 assert(m);
2697 assert(f);
2698 assert(fds);
2699
a7556052 2700 m->n_reloading ++;
38c52d46 2701
01d67b43
LP
2702 fprintf(f, "current-job-id=%i\n", m->current_job_id);
2703 fprintf(f, "taint-usr=%s\n", yes_no(m->taint_usr));
2704
e9ddabc2 2705 dual_timestamp_serialize(f, "initrd-timestamp", &m->initrd_timestamp);
10717a1a
LP
2706 dual_timestamp_serialize(f, "startup-timestamp", &m->startup_timestamp);
2707 dual_timestamp_serialize(f, "finish-timestamp", &m->finish_timestamp);
47a483a1 2708
f2382a94
LP
2709 fputc('\n', f);
2710
a16e1123
LP
2711 HASHMAP_FOREACH_KEY(u, t, m->units, i) {
2712 if (u->meta.id != t)
2713 continue;
2714
2715 if (!unit_can_serialize(u))
2716 continue;
2717
2718 /* Start marker */
2719 fputs(u->meta.id, f);
2720 fputc('\n', f);
2721
38c52d46 2722 if ((r = unit_serialize(u, f, fds)) < 0) {
a7556052 2723 m->n_reloading --;
a16e1123 2724 return r;
38c52d46 2725 }
a16e1123
LP
2726 }
2727
a7556052
LP
2728 assert(m->n_reloading > 0);
2729 m->n_reloading --;
38c52d46 2730
a16e1123
LP
2731 if (ferror(f))
2732 return -EIO;
2733
b23de6af
LP
2734 r = bus_fdset_add_all(m, fds);
2735 if (r < 0)
2736 return r;
2737
a16e1123
LP
2738 return 0;
2739}
2740
2741int manager_deserialize(Manager *m, FILE *f, FDSet *fds) {
2742 int r = 0;
2743
2744 assert(m);
2745 assert(f);
2746
2747 log_debug("Deserializing state...");
2748
a7556052 2749 m->n_reloading ++;
82c64bf5 2750
10f8e83c 2751 for (;;) {
20c03b7b 2752 char line[LINE_MAX], *l;
10f8e83c
LP
2753
2754 if (!fgets(line, sizeof(line), f)) {
2755 if (feof(f))
2756 r = 0;
2757 else
2758 r = -errno;
2759
2760 goto finish;
2761 }
2762
2763 char_array_0(line);
2764 l = strstrip(line);
2765
2766 if (l[0] == 0)
2767 break;
2768
01d67b43
LP
2769 if (startswith(l, "current-job-id=")) {
2770 uint32_t id;
2771
2772 if (safe_atou32(l+15, &id) < 0)
2773 log_debug("Failed to parse current job id value %s", l+15);
2774 else
2775 m->current_job_id = MAX(m->current_job_id, id);
2776 } else if (startswith(l, "taint-usr=")) {
2777 int b;
2778
2779 if ((b = parse_boolean(l+10)) < 0)
2780 log_debug("Failed to parse taint /usr flag %s", l+10);
2781 else
2782 m->taint_usr = m->taint_usr || b;
2783 } else if (startswith(l, "initrd-timestamp="))
e9ddabc2
LP
2784 dual_timestamp_deserialize(l+17, &m->initrd_timestamp);
2785 else if (startswith(l, "startup-timestamp="))
799fd0fd 2786 dual_timestamp_deserialize(l+18, &m->startup_timestamp);
10717a1a 2787 else if (startswith(l, "finish-timestamp="))
799fd0fd 2788 dual_timestamp_deserialize(l+17, &m->finish_timestamp);
10717a1a 2789 else
10f8e83c
LP
2790 log_debug("Unknown serialization item '%s'", l);
2791 }
2792
a16e1123
LP
2793 for (;;) {
2794 Unit *u;
2795 char name[UNIT_NAME_MAX+2];
2796
2797 /* Start marker */
2798 if (!fgets(name, sizeof(name), f)) {
2799 if (feof(f))
10f8e83c
LP
2800 r = 0;
2801 else
2802 r = -errno;
a16e1123 2803
82c64bf5 2804 goto finish;
a16e1123
LP
2805 }
2806
2807 char_array_0(name);
2808
398ef8ba 2809 if ((r = manager_load_unit(m, strstrip(name), NULL, NULL, &u)) < 0)
82c64bf5 2810 goto finish;
a16e1123
LP
2811
2812 if ((r = unit_deserialize(u, f, fds)) < 0)
82c64bf5 2813 goto finish;
a16e1123
LP
2814 }
2815
10f8e83c 2816finish:
82c64bf5
LP
2817 if (ferror(f)) {
2818 r = -EIO;
2819 goto finish;
2820 }
a16e1123 2821
a7556052
LP
2822 assert(m->n_reloading > 0);
2823 m->n_reloading --;
82c64bf5
LP
2824
2825 return r;
a16e1123
LP
2826}
2827
2828int manager_reload(Manager *m) {
2829 int r, q;
2830 FILE *f;
2831 FDSet *fds;
2832
2833 assert(m);
2834
d8d5ab98 2835 if ((r = manager_open_serialization(m, &f)) < 0)
a16e1123
LP
2836 return r;
2837
a7556052 2838 m->n_reloading ++;
38c52d46 2839
a16e1123 2840 if (!(fds = fdset_new())) {
a7556052 2841 m->n_reloading --;
a16e1123
LP
2842 r = -ENOMEM;
2843 goto finish;
2844 }
2845
38c52d46 2846 if ((r = manager_serialize(m, f, fds)) < 0) {
a7556052 2847 m->n_reloading --;
a16e1123 2848 goto finish;
38c52d46 2849 }
a16e1123
LP
2850
2851 if (fseeko(f, 0, SEEK_SET) < 0) {
a7556052 2852 m->n_reloading --;
a16e1123
LP
2853 r = -errno;
2854 goto finish;
2855 }
2856
2857 /* From here on there is no way back. */
2858 manager_clear_jobs_and_units(m);
5a1e9937 2859 manager_undo_generators(m);
a16e1123 2860
2ded0c04 2861 /* Find new unit paths */
84e3543e 2862 lookup_paths_free(&m->lookup_paths);
c800e483 2863 if ((q = lookup_paths_init(&m->lookup_paths, m->running_as, true)) < 0)
2ded0c04
LP
2864 r = q;
2865
5a1e9937
LP
2866 manager_run_generators(m);
2867
2868 manager_build_unit_path_cache(m);
2869
a16e1123
LP
2870 /* First, enumerate what we can from all config files */
2871 if ((q = manager_enumerate(m)) < 0)
2872 r = q;
2873
2874 /* Second, deserialize our stored data */
2875 if ((q = manager_deserialize(m, f, fds)) < 0)
2876 r = q;
2877
2878 fclose(f);
2879 f = NULL;
2880
2881 /* Third, fire things up! */
2882 if ((q = manager_coldplug(m)) < 0)
2883 r = q;
2884
a7556052
LP
2885 assert(m->n_reloading > 0);
2886 m->n_reloading--;
9f611ad8 2887
a16e1123
LP
2888finish:
2889 if (f)
2890 fclose(f);
2891
2892 if (fds)
2893 fdset_free(fds);
2894
2895 return r;
2896}
2897
9e58ff9c
LP
2898bool manager_is_booting_or_shutting_down(Manager *m) {
2899 Unit *u;
2900
2901 assert(m);
2902
2903 /* Is the initial job still around? */
2904 if (manager_get_job(m, 1))
2905 return true;
2906
2907 /* Is there a job for the shutdown target? */
27d340c7
LP
2908 u = manager_get_unit(m, SPECIAL_SHUTDOWN_TARGET);
2909 if (u)
9e58ff9c
LP
2910 return !!u->meta.job;
2911
2912 return false;
2913}
2914
fdf20a31 2915void manager_reset_failed(Manager *m) {
5632e374
LP
2916 Unit *u;
2917 Iterator i;
2918
2919 assert(m);
2920
2921 HASHMAP_FOREACH(u, m->units, i)
fdf20a31 2922 unit_reset_failed(u);
5632e374
LP
2923}
2924
8f6df3fa
LP
2925bool manager_unit_pending_inactive(Manager *m, const char *name) {
2926 Unit *u;
2927
2928 assert(m);
2929 assert(name);
2930
2931 /* Returns true if the unit is inactive or going down */
8f6df3fa
LP
2932 if (!(u = manager_get_unit(m, name)))
2933 return true;
2934
18ffdfda 2935 return unit_pending_inactive(u);
8f6df3fa
LP
2936}
2937
b0c918b9 2938void manager_check_finished(Manager *m) {
e9ddabc2 2939 char userspace[FORMAT_TIMESPAN_MAX], initrd[FORMAT_TIMESPAN_MAX], kernel[FORMAT_TIMESPAN_MAX], sum[FORMAT_TIMESPAN_MAX];
18fa6b27 2940 usec_t kernel_usec = 0, initrd_usec = 0, userspace_usec = 0, total_usec = 0;
b0c918b9
LP
2941
2942 assert(m);
2943
2944 if (dual_timestamp_is_set(&m->finish_timestamp))
2945 return;
2946
2947 if (hashmap_size(m->jobs) > 0)
2948 return;
2949
2950 dual_timestamp_get(&m->finish_timestamp);
2951
e03ae661
LP
2952 if (m->running_as == MANAGER_SYSTEM && detect_container(NULL) <= 0) {
2953
18fa6b27
LP
2954 userspace_usec = m->finish_timestamp.monotonic - m->startup_timestamp.monotonic;
2955 total_usec = m->finish_timestamp.monotonic;
2956
e9ddabc2 2957 if (dual_timestamp_is_set(&m->initrd_timestamp)) {
18fa6b27
LP
2958
2959 kernel_usec = m->initrd_timestamp.monotonic;
2960 initrd_usec = m->startup_timestamp.monotonic - m->initrd_timestamp.monotonic;
2961
e9ddabc2 2962 log_info("Startup finished in %s (kernel) + %s (initrd) + %s (userspace) = %s.",
18fa6b27
LP
2963 format_timespan(kernel, sizeof(kernel), kernel_usec),
2964 format_timespan(initrd, sizeof(initrd), initrd_usec),
2965 format_timespan(userspace, sizeof(userspace), userspace_usec),
2966 format_timespan(sum, sizeof(sum), total_usec));
2967 } else {
2968 kernel_usec = m->startup_timestamp.monotonic;
2969 initrd_usec = 0;
2970
e9ddabc2 2971 log_info("Startup finished in %s (kernel) + %s (userspace) = %s.",
18fa6b27
LP
2972 format_timespan(kernel, sizeof(kernel), kernel_usec),
2973 format_timespan(userspace, sizeof(userspace), userspace_usec),
2974 format_timespan(sum, sizeof(sum), total_usec));
2975 }
2976 } else {
2977 userspace_usec = initrd_usec = kernel_usec = 0;
2978 total_usec = m->finish_timestamp.monotonic - m->startup_timestamp.monotonic;
2979
b0c918b9 2980 log_debug("Startup finished in %s.",
18fa6b27
LP
2981 format_timespan(sum, sizeof(sum), total_usec));
2982 }
b0c918b9 2983
18fa6b27 2984 bus_broadcast_finished(m, kernel_usec, initrd_usec, userspace_usec, total_usec);
530345e7
LP
2985
2986 sd_notifyf(false,
2987 "READY=1\nSTATUS=Startup finished in %s.",
2988 format_timespan(sum, sizeof(sum), total_usec));
b0c918b9
LP
2989}
2990
5a1e9937
LP
2991void manager_run_generators(Manager *m) {
2992 DIR *d = NULL;
5a1e9937 2993 const char *generator_path;
83cc030f 2994 const char *argv[3];
07f8a4aa 2995 mode_t u;
5a1e9937
LP
2996
2997 assert(m);
2998
af2d49f7 2999 generator_path = m->running_as == MANAGER_SYSTEM ? SYSTEM_GENERATOR_PATH : USER_GENERATOR_PATH;
5a1e9937
LP
3000 if (!(d = opendir(generator_path))) {
3001
3002 if (errno == ENOENT)
3003 return;
3004
3005 log_error("Failed to enumerate generator directory: %m");
3006 return;
3007 }
3008
3009 if (!m->generator_unit_path) {
f1d19aa4
LP
3010 const char *p;
3011 char user_path[] = "/tmp/systemd-generator-XXXXXX";
5a1e9937 3012
a08dab55 3013 if (m->running_as == MANAGER_SYSTEM && getpid() == 1) {
f1d19aa4
LP
3014 p = "/run/systemd/generator";
3015
3016 if (mkdir_p(p, 0755) < 0) {
3017 log_error("Failed to create generator directory: %m");
3018 goto finish;
3019 }
3020
3021 } else {
3022 if (!(p = mkdtemp(user_path))) {
3023 log_error("Failed to create generator directory: %m");
3024 goto finish;
3025 }
5a1e9937
LP
3026 }
3027
3028 if (!(m->generator_unit_path = strdup(p))) {
3029 log_error("Failed to allocate generator unit path.");
3030 goto finish;
3031 }
3032 }
3033
83cc030f
LP
3034 argv[0] = NULL; /* Leave this empty, execute_directory() will fill something in */
3035 argv[1] = m->generator_unit_path;
3036 argv[2] = NULL;
5a1e9937 3037
07f8a4aa 3038 u = umask(0022);
83cc030f 3039 execute_directory(generator_path, d, (char**) argv);
07f8a4aa 3040 umask(u);
5a1e9937
LP
3041
3042 if (rmdir(m->generator_unit_path) >= 0) {
3043 /* Uh? we were able to remove this dir? I guess that
3044 * means the directory was empty, hence let's shortcut
3045 * this */
3046
3047 free(m->generator_unit_path);
3048 m->generator_unit_path = NULL;
3049 goto finish;
3050 }
3051
3052 if (!strv_find(m->lookup_paths.unit_path, m->generator_unit_path)) {
3053 char **l;
3054
3055 if (!(l = strv_append(m->lookup_paths.unit_path, m->generator_unit_path))) {
3056 log_error("Failed to add generator directory to unit search path: %m");
3057 goto finish;
3058 }
3059
3060 strv_free(m->lookup_paths.unit_path);
3061 m->lookup_paths.unit_path = l;
7f4e0805
LP
3062
3063 log_debug("Added generator unit path %s to search path.", m->generator_unit_path);
5a1e9937
LP
3064 }
3065
3066finish:
3067 if (d)
3068 closedir(d);
5a1e9937
LP
3069}
3070
3071void manager_undo_generators(Manager *m) {
3072 assert(m);
3073
3074 if (!m->generator_unit_path)
3075 return;
3076
3077 strv_remove(m->lookup_paths.unit_path, m->generator_unit_path);
ad293f5a 3078 rm_rf(m->generator_unit_path, false, true, false);
5a1e9937
LP
3079
3080 free(m->generator_unit_path);
3081 m->generator_unit_path = NULL;
3082}
3083
06d4c99a
LP
3084int manager_set_default_controllers(Manager *m, char **controllers) {
3085 char **l;
3086
3087 assert(m);
3088
3089 if (!(l = strv_copy(controllers)))
3090 return -ENOMEM;
3091
3092 strv_free(m->default_controllers);
3093 m->default_controllers = l;
3094
3095 return 0;
3096}
3097
f1dd0c3f
LP
3098void manager_recheck_syslog(Manager *m) {
3099 Unit *u;
3100
3101 assert(m);
3102
3103 if (m->running_as != MANAGER_SYSTEM)
3104 return;
3105
3106 if ((u = manager_get_unit(m, SPECIAL_SYSLOG_SOCKET))) {
3107 SocketState state;
3108
3109 state = SOCKET(u)->state;
3110
3111 if (state != SOCKET_DEAD &&
3112 state != SOCKET_FAILED &&
3113 state != SOCKET_RUNNING) {
3114
3115 /* Hmm, the socket is not set up, or is still
3116 * listening, let's better not try to use
3117 * it. Note that we have no problem if the
3118 * socket is completely down, since there
3119 * might be a foreign /dev/log socket around
3120 * and we want to make use of that.
3121 */
3122
3123 log_close_syslog();
3124 return;
3125 }
3126 }
3127
3128 if ((u = manager_get_unit(m, SPECIAL_SYSLOG_TARGET)))
3129 if (TARGET(u)->state != TARGET_ACTIVE) {
3130 log_close_syslog();
3131 return;
3132 }
3133
3134 /* Hmm, OK, so the socket is either fully up, or fully down,
3135 * and the target is up, then let's make use of the socket */
3136 log_open();
3137}
3138
27d340c7
LP
3139void manager_set_show_status(Manager *m, bool b) {
3140 assert(m);
3141
3142 if (m->running_as != MANAGER_SYSTEM)
3143 return;
3144
3145 m->show_status = b;
3146
3147 if (b)
3148 touch("/run/systemd/show-status");
3149 else
3150 unlink("/run/systemd/show-status");
3151}
3152
3153bool manager_get_show_status(Manager *m) {
3154 assert(m);
3155
3156 if (m->running_as != MANAGER_SYSTEM)
3157 return false;
3158
3159 if (m->show_status)
3160 return true;
3161
3162 /* If Plymouth is running make sure we show the status, so
3163 * that there's something nice to see when people press Esc */
3164
3165 return plymouth_running();
3166}
3167
dfcd764e 3168static const char* const manager_running_as_table[_MANAGER_RUNNING_AS_MAX] = {
dfcd764e 3169 [MANAGER_SYSTEM] = "system",
af2d49f7 3170 [MANAGER_USER] = "user"
dfcd764e
LP
3171};
3172
3173DEFINE_STRING_TABLE_LOOKUP(manager_running_as, ManagerRunningAs);