]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/manager.c
service: rework killing logic so that we always kill the main process, even if it...
[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"
60918275 61
701cc384
LP
62/* As soon as 16 units are in our GC queue, make sure to run a gc sweep */
63#define GC_QUEUE_ENTRIES_MAX 16
64
65/* As soon as 5s passed since a unit was added to our GC queue, make sure to run a gc sweep */
94b6dfa2 66#define GC_QUEUE_USEC_MAX (10*USEC_PER_SEC)
701cc384 67
8c47c732
LP
68/* Where clients shall send notification messages to */
69#define NOTIFY_SOCKET "/org/freedesktop/systemd1/notify"
70
71static int manager_setup_notify(Manager *m) {
72 union {
73 struct sockaddr sa;
74 struct sockaddr_un un;
75 } sa;
76 struct epoll_event ev;
8c47c732
LP
77 int one = 1;
78
79 assert(m);
80
81 m->notify_watch.type = WATCH_NOTIFY;
82 if ((m->notify_watch.fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0)) < 0) {
83 log_error("Failed to allocate notification socket: %m");
84 return -errno;
85 }
86
87 zero(sa);
88 sa.sa.sa_family = AF_UNIX;
89
a821caaa 90 if (getpid() != 1)
8c47c732
LP
91 snprintf(sa.un.sun_path+1, sizeof(sa.un.sun_path)-1, NOTIFY_SOCKET "/%llu", random_ull());
92 else
93 strncpy(sa.un.sun_path+1, NOTIFY_SOCKET, sizeof(sa.un.sun_path)-1);
94
b12c1e7c 95 if (bind(m->notify_watch.fd, &sa.sa, sizeof(sa_family_t) + 1 + strlen(sa.un.sun_path+1)) < 0) {
8c47c732
LP
96 log_error("bind() failed: %m");
97 return -errno;
98 }
99
100 if (setsockopt(m->notify_watch.fd, SOL_SOCKET, SO_PASSCRED, &one, sizeof(one)) < 0) {
101 log_error("SO_PASSCRED failed: %m");
102 return -errno;
103 }
104
105 zero(ev);
106 ev.events = EPOLLIN;
107 ev.data.ptr = &m->notify_watch;
108
109 if (epoll_ctl(m->epoll_fd, EPOLL_CTL_ADD, m->notify_watch.fd, &ev) < 0)
110 return -errno;
111
c952c6ec 112 if (!(m->notify_socket = strdup(sa.un.sun_path+1)))
8c47c732
LP
113 return -ENOMEM;
114
b2bb3dbe
LP
115 log_debug("Using notification socket %s", m->notify_socket);
116
8c47c732
LP
117 return 0;
118}
119
80876c20
LP
120static int enable_special_signals(Manager *m) {
121 char fd;
122
123 assert(m);
124
125 /* Enable that we get SIGINT on control-alt-del */
126 if (reboot(RB_DISABLE_CAD) < 0)
127 log_warning("Failed to enable ctrl-alt-del handling: %m");
128
affda787 129 if ((fd = open_terminal("/dev/tty0", O_RDWR|O_NOCTTY)) < 0)
80876c20
LP
130 log_warning("Failed to open /dev/tty0: %m");
131 else {
132 /* Enable that we get SIGWINCH on kbrequest */
133 if (ioctl(fd, KDSIGACCEPT, SIGWINCH) < 0)
134 log_warning("Failed to enable kbrequest handling: %s", strerror(errno));
135
136 close_nointr_nofail(fd);
137 }
138
139 return 0;
140}
141
ce578209 142static int manager_setup_signals(Manager *m) {
9152c765
LP
143 sigset_t mask;
144 struct epoll_event ev;
57c0c30e 145 struct sigaction sa;
60918275 146
ce578209
LP
147 assert(m);
148
57c0c30e
LP
149 /* We are not interested in SIGSTOP and friends. */
150 zero(sa);
151 sa.sa_handler = SIG_DFL;
152 sa.sa_flags = SA_NOCLDSTOP|SA_RESTART;
153 assert_se(sigaction(SIGCHLD, &sa, NULL) == 0);
154
ce578209 155 assert_se(sigemptyset(&mask) == 0);
7d793605
LP
156
157 sigset_add_many(&mask,
158 SIGCHLD, /* Child died */
159 SIGTERM, /* Reexecute daemon */
160 SIGHUP, /* Reload configuration */
161 SIGUSR1, /* systemd/upstart: reconnect to D-Bus */
162 SIGUSR2, /* systemd: dump status */
163 SIGINT, /* Kernel sends us this on control-alt-del */
164 SIGWINCH, /* Kernel sends us this on kbrequest (alt-arrowup) */
165 SIGPWR, /* Some kernel drivers and upsd send us this on power failure */
166 SIGRTMIN+0, /* systemd: start default.target */
167 SIGRTMIN+1, /* systemd: start rescue.target */
168 SIGRTMIN+2, /* systemd: isolate emergency.target */
169 SIGRTMIN+3, /* systemd: start halt.target */
170 SIGRTMIN+4, /* systemd: start poweroff.target */
171 SIGRTMIN+5, /* systemd: start reboot.target */
172 -1);
ce578209
LP
173 assert_se(sigprocmask(SIG_SETMASK, &mask, NULL) == 0);
174
ef734fd6 175 m->signal_watch.type = WATCH_SIGNAL;
ce578209
LP
176 if ((m->signal_watch.fd = signalfd(-1, &mask, SFD_NONBLOCK|SFD_CLOEXEC)) < 0)
177 return -errno;
178
179 zero(ev);
180 ev.events = EPOLLIN;
181 ev.data.ptr = &m->signal_watch;
182
183 if (epoll_ctl(m->epoll_fd, EPOLL_CTL_ADD, m->signal_watch.fd, &ev) < 0)
184 return -errno;
185
a3d4e06d 186 if (m->running_as == MANAGER_SYSTEM)
80876c20 187 return enable_special_signals(m);
e1414003 188
ce578209
LP
189 return 0;
190}
191
9e58ff9c 192int manager_new(ManagerRunningAs running_as, Manager **_m) {
ce578209 193 Manager *m;
8e274523
LP
194 int r = -ENOMEM;
195
196 assert(_m);
a5dab5ce
LP
197 assert(running_as >= 0);
198 assert(running_as < _MANAGER_RUNNING_AS_MAX);
ce578209 199
60918275 200 if (!(m = new0(Manager, 1)))
8e274523 201 return -ENOMEM;
60918275 202
63983207 203 dual_timestamp_get(&m->startup_timestamp);
e537352b 204
a5dab5ce 205 m->running_as = running_as;
a567261a 206 m->name_data_slot = m->subscribed_data_slot = -1;
a16e1123 207 m->exit_code = _MANAGER_EXIT_CODE_INVALID;
33be102a 208 m->pin_cgroupfs_fd = -1;
80876c20 209
4927fcae
LP
210#ifdef HAVE_AUDIT
211 m->audit_fd = -1;
212#endif
213
8d567588 214 m->signal_watch.fd = m->mount_watch.fd = m->udev_watch.fd = m->epoll_fd = m->dev_autofs_fd = -1;
ea430986 215 m->current_job_id = 1; /* start as id #1, so that we can leave #0 around as "null-like" value */
9152c765 216
1137a57c
LP
217 if (!(m->environment = strv_copy(environ)))
218 goto fail;
219
87f0e418 220 if (!(m->units = hashmap_new(string_hash_func, string_compare_func)))
60918275
LP
221 goto fail;
222
223 if (!(m->jobs = hashmap_new(trivial_hash_func, trivial_compare_func)))
224 goto fail;
225
e5b5ae50 226 if (!(m->transaction_jobs = hashmap_new(trivial_hash_func, trivial_compare_func)))
60918275
LP
227 goto fail;
228
9152c765
LP
229 if (!(m->watch_pids = hashmap_new(trivial_hash_func, trivial_compare_func)))
230 goto fail;
231
8e274523
LP
232 if (!(m->cgroup_bondings = hashmap_new(string_hash_func, string_compare_func)))
233 goto fail;
234
05e343b7
LP
235 if (!(m->watch_bus = hashmap_new(string_hash_func, string_compare_func)))
236 goto fail;
237
9152c765
LP
238 if ((m->epoll_fd = epoll_create1(EPOLL_CLOEXEC)) < 0)
239 goto fail;
240
84e3543e 241 if ((r = lookup_paths_init(&m->lookup_paths, m->running_as)) < 0)
e1414003
LP
242 goto fail;
243
8e274523
LP
244 if ((r = manager_setup_signals(m)) < 0)
245 goto fail;
246
8e274523 247 if ((r = manager_setup_cgroup(m)) < 0)
9152c765
LP
248 goto fail;
249
8c47c732
LP
250 if ((r = manager_setup_notify(m)) < 0)
251 goto fail;
252
f278026d 253 /* Try to connect to the busses, if possible. */
5e8d1c9a 254 if ((r = bus_init(m)) < 0)
ea430986
LP
255 goto fail;
256
e543deae 257#ifdef HAVE_AUDIT
4927fcae
LP
258 if ((m->audit_fd = audit_open()) < 0)
259 log_error("Failed to connect to audit log: %m");
e543deae 260#endif
4927fcae 261
8e274523
LP
262 *_m = m;
263 return 0;
60918275
LP
264
265fail:
266 manager_free(m);
8e274523 267 return r;
60918275
LP
268}
269
23a177ef
LP
270static unsigned manager_dispatch_cleanup_queue(Manager *m) {
271 Meta *meta;
272 unsigned n = 0;
273
274 assert(m);
275
276 while ((meta = m->cleanup_queue)) {
277 assert(meta->in_cleanup_queue);
278
399ab2b1 279 unit_free((Unit*) meta);
23a177ef
LP
280 n++;
281 }
282
283 return n;
284}
285
eced69b3
LP
286enum {
287 GC_OFFSET_IN_PATH, /* This one is on the path we were travelling */
288 GC_OFFSET_UNSURE, /* No clue */
289 GC_OFFSET_GOOD, /* We still need this unit */
290 GC_OFFSET_BAD, /* We don't need this unit anymore */
291 _GC_OFFSET_MAX
292};
293
294static void unit_gc_sweep(Unit *u, unsigned gc_marker) {
701cc384
LP
295 Iterator i;
296 Unit *other;
eced69b3 297 bool is_bad;
701cc384
LP
298
299 assert(u);
300
eced69b3
LP
301 if (u->meta.gc_marker == gc_marker + GC_OFFSET_GOOD ||
302 u->meta.gc_marker == gc_marker + GC_OFFSET_BAD ||
303 u->meta.gc_marker == gc_marker + GC_OFFSET_IN_PATH)
701cc384
LP
304 return;
305
c9c0cadb 306 if (u->meta.in_cleanup_queue)
701cc384
LP
307 goto bad;
308
309 if (unit_check_gc(u))
310 goto good;
311
eced69b3
LP
312 u->meta.gc_marker = gc_marker + GC_OFFSET_IN_PATH;
313
314 is_bad = true;
315
701cc384
LP
316 SET_FOREACH(other, u->meta.dependencies[UNIT_REFERENCED_BY], i) {
317 unit_gc_sweep(other, gc_marker);
318
eced69b3 319 if (other->meta.gc_marker == gc_marker + GC_OFFSET_GOOD)
701cc384 320 goto good;
eced69b3
LP
321
322 if (other->meta.gc_marker != gc_marker + GC_OFFSET_BAD)
323 is_bad = false;
701cc384
LP
324 }
325
eced69b3
LP
326 if (is_bad)
327 goto bad;
328
329 /* We were unable to find anything out about this entry, so
330 * let's investigate it later */
331 u->meta.gc_marker = gc_marker + GC_OFFSET_UNSURE;
332 unit_add_to_gc_queue(u);
333 return;
334
701cc384 335bad:
eced69b3
LP
336 /* We definitely know that this one is not useful anymore, so
337 * let's mark it for deletion */
338 u->meta.gc_marker = gc_marker + GC_OFFSET_BAD;
339 unit_add_to_cleanup_queue(u);
701cc384
LP
340 return;
341
342good:
eced69b3 343 u->meta.gc_marker = gc_marker + GC_OFFSET_GOOD;
701cc384
LP
344}
345
346static unsigned manager_dispatch_gc_queue(Manager *m) {
347 Meta *meta;
348 unsigned n = 0;
eced69b3 349 unsigned gc_marker;
701cc384
LP
350
351 assert(m);
352
353 if ((m->n_in_gc_queue < GC_QUEUE_ENTRIES_MAX) &&
354 (m->gc_queue_timestamp <= 0 ||
355 (m->gc_queue_timestamp + GC_QUEUE_USEC_MAX) > now(CLOCK_MONOTONIC)))
356 return 0;
357
358 log_debug("Running GC...");
359
eced69b3
LP
360 m->gc_marker += _GC_OFFSET_MAX;
361 if (m->gc_marker + _GC_OFFSET_MAX <= _GC_OFFSET_MAX)
c9c0cadb 362 m->gc_marker = 1;
701cc384 363
eced69b3
LP
364 gc_marker = m->gc_marker;
365
701cc384
LP
366 while ((meta = m->gc_queue)) {
367 assert(meta->in_gc_queue);
368
399ab2b1 369 unit_gc_sweep((Unit*) meta, gc_marker);
eced69b3 370
701cc384
LP
371 LIST_REMOVE(Meta, gc_queue, m->gc_queue, meta);
372 meta->in_gc_queue = false;
373
374 n++;
375
eced69b3
LP
376 if (meta->gc_marker == gc_marker + GC_OFFSET_BAD ||
377 meta->gc_marker == gc_marker + GC_OFFSET_UNSURE) {
701cc384 378 log_debug("Collecting %s", meta->id);
eced69b3 379 meta->gc_marker = gc_marker + GC_OFFSET_BAD;
399ab2b1 380 unit_add_to_cleanup_queue((Unit*) meta);
701cc384
LP
381 }
382 }
383
384 m->n_in_gc_queue = 0;
385 m->gc_queue_timestamp = 0;
386
387 return n;
388}
389
a16e1123 390static void manager_clear_jobs_and_units(Manager *m) {
e5b5ae50 391 Job *j;
a16e1123 392 Unit *u;
60918275
LP
393
394 assert(m);
395
87f0e418 396 while ((j = hashmap_first(m->transaction_jobs)))
e5b5ae50
LP
397 job_free(j);
398
87f0e418
LP
399 while ((u = hashmap_first(m->units)))
400 unit_free(u);
964e0949
LP
401
402 manager_dispatch_cleanup_queue(m);
403
404 assert(!m->load_queue);
405 assert(!m->run_queue);
406 assert(!m->dbus_unit_queue);
407 assert(!m->dbus_job_queue);
408 assert(!m->cleanup_queue);
409 assert(!m->gc_queue);
410
411 assert(hashmap_isempty(m->transaction_jobs));
412 assert(hashmap_isempty(m->jobs));
413 assert(hashmap_isempty(m->units));
a16e1123
LP
414}
415
416void manager_free(Manager *m) {
417 UnitType c;
87f0e418 418
a16e1123
LP
419 assert(m);
420
421 manager_clear_jobs_and_units(m);
23a177ef 422
7824bbeb
LP
423 for (c = 0; c < _UNIT_TYPE_MAX; c++)
424 if (unit_vtable[c]->shutdown)
425 unit_vtable[c]->shutdown(m);
426
a16e1123
LP
427 /* If we reexecute ourselves, we keep the root cgroup
428 * around */
c6c18be3 429 manager_shutdown_cgroup(m, m->exit_code != MANAGER_REEXECUTE);
8e274523 430
5e8d1c9a 431 bus_done(m);
ea430986 432
87f0e418 433 hashmap_free(m->units);
60918275 434 hashmap_free(m->jobs);
e5b5ae50 435 hashmap_free(m->transaction_jobs);
9152c765 436 hashmap_free(m->watch_pids);
05e343b7 437 hashmap_free(m->watch_bus);
9152c765
LP
438
439 if (m->epoll_fd >= 0)
a16e1123 440 close_nointr_nofail(m->epoll_fd);
acbb0225 441 if (m->signal_watch.fd >= 0)
a16e1123 442 close_nointr_nofail(m->signal_watch.fd);
8c47c732
LP
443 if (m->notify_watch.fd >= 0)
444 close_nointr_nofail(m->notify_watch.fd);
60918275 445
4927fcae
LP
446#ifdef HAVE_AUDIT
447 if (m->audit_fd >= 0)
448 audit_close(m->audit_fd);
449#endif
450
c952c6ec 451 free(m->notify_socket);
b2bb3dbe 452 free(m->console);
c952c6ec 453
84e3543e 454 lookup_paths_free(&m->lookup_paths);
1137a57c 455 strv_free(m->environment);
036643a2 456
8e274523 457 hashmap_free(m->cgroup_bondings);
c6c18be3 458 set_free_free(m->unit_path_cache);
33be102a 459
60918275
LP
460 free(m);
461}
462
a16e1123
LP
463int manager_enumerate(Manager *m) {
464 int r = 0, q;
f50e0a01 465 UnitType c;
f50e0a01
LP
466
467 assert(m);
468
a16e1123
LP
469 /* Let's ask every type to load all units from disk/kernel
470 * that it might know */
f50e0a01
LP
471 for (c = 0; c < _UNIT_TYPE_MAX; c++)
472 if (unit_vtable[c]->enumerate)
a16e1123
LP
473 if ((q = unit_vtable[c]->enumerate(m)) < 0)
474 r = q;
f50e0a01
LP
475
476 manager_dispatch_load_queue(m);
a16e1123
LP
477 return r;
478}
479
480int manager_coldplug(Manager *m) {
481 int r = 0, q;
482 Iterator i;
483 Unit *u;
484 char *k;
485
486 assert(m);
f50e0a01
LP
487
488 /* Then, let's set up their initial state. */
489 HASHMAP_FOREACH_KEY(u, k, m->units, i) {
490
491 /* ignore aliases */
9e2f7c11 492 if (u->meta.id != k)
f50e0a01
LP
493 continue;
494
cca098b0
LP
495 if ((q = unit_coldplug(u)) < 0)
496 r = q;
f50e0a01
LP
497 }
498
a16e1123
LP
499 return r;
500}
501
fe51822e
LP
502static void manager_build_unit_path_cache(Manager *m) {
503 char **i;
504 DIR *d = NULL;
505 int r;
506
507 assert(m);
508
509 set_free_free(m->unit_path_cache);
510
511 if (!(m->unit_path_cache = set_new(string_hash_func, string_compare_func))) {
512 log_error("Failed to allocate unit path cache.");
513 return;
514 }
515
516 /* This simply builds a list of files we know exist, so that
517 * we don't always have to go to disk */
518
519 STRV_FOREACH(i, m->lookup_paths.unit_path) {
520 struct dirent *de;
521
522 if (!(d = opendir(*i))) {
523 log_error("Failed to open directory: %m");
524 continue;
525 }
526
527 while ((de = readdir(d))) {
528 char *p;
529
530 if (ignore_file(de->d_name))
531 continue;
532
533 if (asprintf(&p, "%s/%s", streq(*i, "/") ? "" : *i, de->d_name) < 0) {
534 r = -ENOMEM;
535 goto fail;
536 }
537
538 if ((r = set_put(m->unit_path_cache, p)) < 0) {
539 free(p);
540 goto fail;
541 }
542 }
543
544 closedir(d);
545 d = NULL;
546 }
547
548 return;
549
550fail:
551 log_error("Failed to build unit path cache: %s", strerror(-r));
552
553 set_free_free(m->unit_path_cache);
554 m->unit_path_cache = NULL;
555
556 if (d)
557 closedir(d);
558}
559
a16e1123
LP
560int manager_startup(Manager *m, FILE *serialization, FDSet *fds) {
561 int r, q;
562
563 assert(m);
564
fe51822e
LP
565 manager_build_unit_path_cache(m);
566
9f611ad8
LP
567 /* If we will deserialize make sure that during enumeration
568 * this is already known, so we increase the counter here
569 * already */
570 if (serialization)
571 m->n_deserializing ++;
572
a16e1123
LP
573 /* First, enumerate what we can from all config files */
574 r = manager_enumerate(m);
575
576 /* Second, deserialize if there is something to deserialize */
577 if (serialization)
578 if ((q = manager_deserialize(m, serialization, fds)) < 0)
579 r = q;
580
581 /* Third, fire things up! */
582 if ((q = manager_coldplug(m)) < 0)
583 r = q;
584
9f611ad8
LP
585 if (serialization) {
586 assert(m->n_deserializing > 0);
587 m->n_deserializing --;
588 }
589
a16e1123 590 return r;
f50e0a01
LP
591}
592
23a177ef 593static void transaction_delete_job(Manager *m, Job *j, bool delete_dependencies) {
302d0040
LP
594 assert(m);
595 assert(j);
596
1ffba6fe
LP
597 /* Deletes one job from the transaction */
598
23a177ef 599 manager_transaction_unlink_job(m, j, delete_dependencies);
302d0040 600
ac1135be 601 if (!j->installed)
302d0040
LP
602 job_free(j);
603}
604
87f0e418 605static void transaction_delete_unit(Manager *m, Unit *u) {
1ffba6fe
LP
606 Job *j;
607
87f0e418 608 /* Deletes all jobs associated with a certain unit from the
1ffba6fe
LP
609 * transaction */
610
87f0e418 611 while ((j = hashmap_get(m->transaction_jobs, u)))
23a177ef 612 transaction_delete_job(m, j, true);
1ffba6fe
LP
613}
614
f04fa1d5
LP
615static void transaction_clean_dependencies(Manager *m) {
616 Iterator i;
617 Job *j;
618
619 assert(m);
620
621 /* Drops all dependencies of all installed jobs */
622
623 HASHMAP_FOREACH(j, m->jobs, i) {
624 while (j->subject_list)
625 job_dependency_free(j->subject_list);
626 while (j->object_list)
627 job_dependency_free(j->object_list);
628 }
629
630 assert(!m->transaction_anchor);
631}
632
11dd41ce
LP
633static void transaction_abort(Manager *m) {
634 Job *j;
635
636 assert(m);
11dd41ce 637
e5b5ae50 638 while ((j = hashmap_first(m->transaction_jobs)))
ac1135be 639 if (j->installed)
23a177ef 640 transaction_delete_job(m, j, true);
e5b5ae50
LP
641 else
642 job_free(j);
643
644 assert(hashmap_isempty(m->transaction_jobs));
f04fa1d5
LP
645
646 transaction_clean_dependencies(m);
e5b5ae50
LP
647}
648
649static void transaction_find_jobs_that_matter_to_anchor(Manager *m, Job *j, unsigned generation) {
650 JobDependency *l;
651
652 assert(m);
653
87f0e418 654 /* A recursive sweep through the graph that marks all units
1ffba6fe
LP
655 * that matter to the anchor job, i.e. are directly or
656 * indirectly a dependency of the anchor job via paths that
657 * are fully marked as mattering. */
658
44d8db9e
LP
659 if (j)
660 l = j->subject_list;
661 else
662 l = m->transaction_anchor;
663
664 LIST_FOREACH(subject, l, l) {
e5b5ae50
LP
665
666 /* This link does not matter */
667 if (!l->matters)
668 continue;
669
87f0e418 670 /* This unit has already been marked */
e5b5ae50
LP
671 if (l->object->generation == generation)
672 continue;
673
674 l->object->matters_to_anchor = true;
675 l->object->generation = generation;
676
677 transaction_find_jobs_that_matter_to_anchor(m, l->object, generation);
678 }
679}
680
7fad411c 681static void transaction_merge_and_delete_job(Manager *m, Job *j, Job *other, JobType t) {
e5b5ae50
LP
682 JobDependency *l, *last;
683
684 assert(j);
685 assert(other);
87f0e418 686 assert(j->unit == other->unit);
ac1135be 687 assert(!j->installed);
e5b5ae50 688
1ffba6fe
LP
689 /* Merges 'other' into 'j' and then deletes j. */
690
e5b5ae50
LP
691 j->type = t;
692 j->state = JOB_WAITING;
9e2f7c11 693 j->override = j->override || other->override;
e5b5ae50
LP
694
695 j->matters_to_anchor = j->matters_to_anchor || other->matters_to_anchor;
696
697 /* Patch us in as new owner of the JobDependency objects */
698 last = NULL;
44d8db9e 699 LIST_FOREACH(subject, l, other->subject_list) {
e5b5ae50
LP
700 assert(l->subject == other);
701 l->subject = j;
702 last = l;
703 }
704
705 /* Merge both lists */
706 if (last) {
707 last->subject_next = j->subject_list;
708 if (j->subject_list)
709 j->subject_list->subject_prev = last;
710 j->subject_list = other->subject_list;
711 }
712
713 /* Patch us in as new owner of the JobDependency objects */
714 last = NULL;
44d8db9e 715 LIST_FOREACH(object, l, other->object_list) {
e5b5ae50
LP
716 assert(l->object == other);
717 l->object = j;
718 last = l;
719 }
720
721 /* Merge both lists */
722 if (last) {
723 last->object_next = j->object_list;
724 if (j->object_list)
725 j->object_list->object_prev = last;
726 j->object_list = other->object_list;
727 }
728
e5b5ae50
LP
729 /* Kill the other job */
730 other->subject_list = NULL;
731 other->object_list = NULL;
23a177ef 732 transaction_delete_job(m, other, true);
e5b5ae50 733}
69dd2852
LP
734static bool job_is_conflicted_by(Job *j) {
735 JobDependency *l;
736
737 assert(j);
738
739 /* Returns true if this job is pulled in by a least one
740 * ConflictedBy dependency. */
741
742 LIST_FOREACH(object, l, j->object_list)
743 if (l->conflicts)
744 return true;
745
746 return false;
747}
e5b5ae50 748
5cb5a6ff 749static int delete_one_unmergeable_job(Manager *m, Job *j) {
1ffba6fe
LP
750 Job *k;
751
752 assert(j);
753
754 /* Tries to delete one item in the linked list
755 * j->transaction_next->transaction_next->... that conflicts
756 * whith another one, in an attempt to make an inconsistent
757 * transaction work. */
758
759 /* We rely here on the fact that if a merged with b does not
760 * merge with c, either a or b merge with c neither */
034c6ed7
LP
761 LIST_FOREACH(transaction, j, j)
762 LIST_FOREACH(transaction, k, j->transaction_next) {
1ffba6fe
LP
763 Job *d;
764
765 /* Is this one mergeable? Then skip it */
5cb5a6ff 766 if (job_type_is_mergeable(j->type, k->type))
1ffba6fe
LP
767 continue;
768
769 /* Ok, we found two that conflict, let's see if we can
770 * drop one of them */
69dd2852
LP
771 if (!j->matters_to_anchor && !k->matters_to_anchor) {
772
773 /* Both jobs don't matter, so let's
774 * find the one that is smarter to
775 * remove. Let's think positive and
776 * rather remove stops then starts --
777 * except if something is being
778 * stopped because it is conflicted by
779 * another unit in which case we
780 * rather remove the start. */
781
782 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)));
783 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)));
784
785 if (j->type == JOB_STOP) {
786
787 if (job_is_conflicted_by(j))
788 d = k;
789 else
790 d = j;
791
792 } else if (k->type == JOB_STOP) {
793
794 if (job_is_conflicted_by(k))
795 d = j;
796 else
797 d = k;
e364ad06
LP
798 } else
799 d = j;
69dd2852
LP
800
801 } else if (!j->matters_to_anchor)
1ffba6fe
LP
802 d = j;
803 else if (!k->matters_to_anchor)
804 d = k;
805 else
806 return -ENOEXEC;
807
808 /* Ok, we can drop one, so let's do so. */
2e81c8a5 809 log_debug("Fixing conflicting jobs by deleting job %s/%s", d->unit->meta.id, job_type_to_string(d->type));
23a177ef 810 transaction_delete_job(m, d, true);
1ffba6fe
LP
811 return 0;
812 }
813
814 return -EINVAL;
815}
816
398ef8ba 817static int transaction_merge_jobs(Manager *m, DBusError *e) {
11dd41ce 818 Job *j;
034c6ed7 819 Iterator i;
e5b5ae50
LP
820 int r;
821
822 assert(m);
823
1ffba6fe
LP
824 /* First step, check whether any of the jobs for one specific
825 * task conflict. If so, try to drop one of them. */
034c6ed7 826 HASHMAP_FOREACH(j, m->transaction_jobs, i) {
1ffba6fe
LP
827 JobType t;
828 Job *k;
829
830 t = j->type;
034c6ed7 831 LIST_FOREACH(transaction, k, j->transaction_next) {
e364ad06 832 if (job_type_merge(&t, k->type) >= 0)
1ffba6fe
LP
833 continue;
834
835 /* OK, we could not merge all jobs for this
836 * action. Let's see if we can get rid of one
837 * of them */
838
5cb5a6ff 839 if ((r = delete_one_unmergeable_job(m, j)) >= 0)
1ffba6fe
LP
840 /* Ok, we managed to drop one, now
841 * let's ask our callers to call us
842 * again after garbage collecting */
843 return -EAGAIN;
844
845 /* We couldn't merge anything. Failure */
398ef8ba
LP
846 dbus_set_error(e, BUS_ERROR_TRANSACTION_JOBS_CONFLICTING, "Transaction contains conflicting jobs '%s' and '%s' for %s. Probably contradicting requirement dependencies configured.",
847 job_type_to_string(t), job_type_to_string(k->type), k->unit->meta.id);
1ffba6fe
LP
848 return r;
849 }
850 }
851
852 /* Second step, merge the jobs. */
034c6ed7 853 HASHMAP_FOREACH(j, m->transaction_jobs, i) {
e5b5ae50
LP
854 JobType t = j->type;
855 Job *k;
856
e094e853 857 /* Merge all transactions */
034c6ed7 858 LIST_FOREACH(transaction, k, j->transaction_next)
1ffba6fe 859 assert_se(job_type_merge(&t, k->type) == 0);
e5b5ae50 860
5cb5a6ff 861 /* If an active job is mergeable, merge it too */
87f0e418
LP
862 if (j->unit->meta.job)
863 job_type_merge(&t, j->unit->meta.job->type); /* Might fail. Which is OK */
e094e853 864
e5b5ae50 865 while ((k = j->transaction_next)) {
ac1135be 866 if (j->installed) {
7fad411c 867 transaction_merge_and_delete_job(m, k, j, t);
e5b5ae50
LP
868 j = k;
869 } else
7fad411c 870 transaction_merge_and_delete_job(m, j, k, t);
e5b5ae50
LP
871 }
872
873 assert(!j->transaction_next);
874 assert(!j->transaction_prev);
875 }
876
7fad411c 877 return 0;
e5b5ae50
LP
878}
879
23a177ef
LP
880static void transaction_drop_redundant(Manager *m) {
881 bool again;
882
883 assert(m);
884
885 /* Goes through the transaction and removes all jobs that are
886 * a noop */
887
888 do {
889 Job *j;
890 Iterator i;
891
892 again = false;
893
894 HASHMAP_FOREACH(j, m->transaction_jobs, i) {
895 bool changes_something = false;
896 Job *k;
897
898 LIST_FOREACH(transaction, k, j) {
899
900 if (!job_is_anchor(k) &&
901 job_type_is_redundant(k->type, unit_active_state(k->unit)))
902 continue;
903
904 changes_something = true;
905 break;
906 }
907
908 if (changes_something)
909 continue;
910
9e2f7c11 911 log_debug("Found redundant job %s/%s, dropping.", j->unit->meta.id, job_type_to_string(j->type));
23a177ef
LP
912 transaction_delete_job(m, j, false);
913 again = true;
914 break;
915 }
916
917 } while (again);
918}
919
87f0e418
LP
920static bool unit_matters_to_anchor(Unit *u, Job *j) {
921 assert(u);
1ffba6fe
LP
922 assert(!j->transaction_prev);
923
87f0e418 924 /* Checks whether at least one of the jobs for this unit
1ffba6fe
LP
925 * matters to the anchor. */
926
034c6ed7 927 LIST_FOREACH(transaction, j, j)
1ffba6fe
LP
928 if (j->matters_to_anchor)
929 return true;
930
931 return false;
932}
933
398ef8ba 934static int transaction_verify_order_one(Manager *m, Job *j, Job *from, unsigned generation, DBusError *e) {
034c6ed7 935 Iterator i;
87f0e418 936 Unit *u;
11dd41ce 937 int r;
e5b5ae50
LP
938
939 assert(m);
940 assert(j);
1ffba6fe
LP
941 assert(!j->transaction_prev);
942
943 /* Does a recursive sweep through the ordering graph, looking
944 * for a cycle. If we find cycle we try to break it. */
e5b5ae50 945
23e3c588
LP
946 /* Have we seen this before? */
947 if (j->generation == generation) {
674a6e4d 948 Job *k, *delete;
e5b5ae50 949
23e3c588
LP
950 /* If the marker is NULL we have been here already and
951 * decided the job was loop-free from here. Hence
952 * shortcut things and return right-away. */
953 if (!j->marker)
954 return 0;
e5b5ae50 955
23e3c588
LP
956 /* So, the marker is not NULL and we already have been
957 * here. We have a cycle. Let's try to break it. We go
958 * backwards in our path and try to find a suitable
959 * job to remove. We use the marker to find our way
960 * back, since smart how we are we stored our way back
961 * in there. */
54165a39 962 log_warning("Found ordering cycle on %s/%s", j->unit->meta.id, job_type_to_string(j->type));
9f04bd52 963
674a6e4d 964 delete = NULL;
23e3c588 965 for (k = from; k; k = ((k->generation == generation && k->marker != k) ? k->marker : NULL)) {
1ffba6fe 966
54165a39 967 log_info("Walked on cycle path to %s/%s", k->unit->meta.id, job_type_to_string(k->type));
9f04bd52 968
674a6e4d
LP
969 if (!delete &&
970 !k->installed &&
87f0e418 971 !unit_matters_to_anchor(k->unit, k)) {
1ffba6fe
LP
972 /* Ok, we can drop this one, so let's
973 * do so. */
674a6e4d 974 delete = k;
e5b5ae50
LP
975 }
976
977 /* Check if this in fact was the beginning of
7fad411c 978 * the cycle */
e5b5ae50
LP
979 if (k == j)
980 break;
981 }
982
674a6e4d
LP
983
984 if (delete) {
985 log_warning("Breaking ordering cycle by deleting job %s/%s", k->unit->meta.id, job_type_to_string(k->type));
986 transaction_delete_unit(m, delete->unit);
987 return -EAGAIN;
988 }
989
54165a39 990 log_error("Unable to break cycle");
9f04bd52 991
398ef8ba 992 dbus_set_error(e, BUS_ERROR_TRANSACTION_ORDER_IS_CYCLIC, "Transaction order is cyclic. See logs for details.");
1ffba6fe 993 return -ENOEXEC;
e5b5ae50
LP
994 }
995
1ffba6fe 996 /* Make the marker point to where we come from, so that we can
23e3c588
LP
997 * find our way backwards if we want to break a cycle. We use
998 * a special marker for the beginning: we point to
999 * ourselves. */
1000 j->marker = from ? from : j;
e5b5ae50
LP
1001 j->generation = generation;
1002
1ffba6fe 1003 /* We assume that the the dependencies are bidirectional, and
87f0e418
LP
1004 * hence can ignore UNIT_AFTER */
1005 SET_FOREACH(u, j->unit->meta.dependencies[UNIT_BEFORE], i) {
e5b5ae50
LP
1006 Job *o;
1007
87f0e418
LP
1008 /* Is there a job for this unit? */
1009 if (!(o = hashmap_get(m->transaction_jobs, u)))
1ffba6fe
LP
1010
1011 /* Ok, there is no job for this in the
1012 * transaction, but maybe there is already one
1013 * running? */
87f0e418 1014 if (!(o = u->meta.job))
e5b5ae50
LP
1015 continue;
1016
398ef8ba 1017 if ((r = transaction_verify_order_one(m, o, j, generation, e)) < 0)
e5b5ae50
LP
1018 return r;
1019 }
1020
9f04bd52
LP
1021 /* Ok, let's backtrack, and remember that this entry is not on
1022 * our path anymore. */
1023 j->marker = NULL;
1024
e5b5ae50
LP
1025 return 0;
1026}
1027
398ef8ba 1028static int transaction_verify_order(Manager *m, unsigned *generation, DBusError *e) {
1ffba6fe
LP
1029 Job *j;
1030 int r;
034c6ed7 1031 Iterator i;
23e3c588 1032 unsigned g;
1ffba6fe 1033
e5b5ae50
LP
1034 assert(m);
1035 assert(generation);
1036
1ffba6fe
LP
1037 /* Check if the ordering graph is cyclic. If it is, try to fix
1038 * that up by dropping one of the jobs. */
e5b5ae50 1039
23e3c588
LP
1040 g = (*generation)++;
1041
034c6ed7 1042 HASHMAP_FOREACH(j, m->transaction_jobs, i)
398ef8ba 1043 if ((r = transaction_verify_order_one(m, j, NULL, g, e)) < 0)
1ffba6fe 1044 return r;
e5b5ae50
LP
1045
1046 return 0;
1047}
1048
1049static void transaction_collect_garbage(Manager *m) {
1050 bool again;
1051
1052 assert(m);
1053
1ffba6fe
LP
1054 /* Drop jobs that are not required by any other job */
1055
e5b5ae50 1056 do {
034c6ed7 1057 Iterator i;
e5b5ae50
LP
1058 Job *j;
1059
1060 again = false;
1061
034c6ed7 1062 HASHMAP_FOREACH(j, m->transaction_jobs, i) {
e5b5ae50
LP
1063 if (j->object_list)
1064 continue;
1065
9e2f7c11 1066 log_debug("Garbage collecting job %s/%s", j->unit->meta.id, job_type_to_string(j->type));
23a177ef 1067 transaction_delete_job(m, j, true);
e5b5ae50
LP
1068 again = true;
1069 break;
1070 }
1071
1072 } while (again);
1073}
1074
398ef8ba 1075static int transaction_is_destructive(Manager *m, DBusError *e) {
034c6ed7 1076 Iterator i;
e5b5ae50 1077 Job *j;
11dd41ce
LP
1078
1079 assert(m);
11dd41ce 1080
e5b5ae50
LP
1081 /* Checks whether applying this transaction means that
1082 * existing jobs would be replaced */
11dd41ce 1083
034c6ed7 1084 HASHMAP_FOREACH(j, m->transaction_jobs, i) {
e094e853
LP
1085
1086 /* Assume merged */
1087 assert(!j->transaction_prev);
1088 assert(!j->transaction_next);
1089
87f0e418
LP
1090 if (j->unit->meta.job &&
1091 j->unit->meta.job != j &&
398ef8ba
LP
1092 !job_type_is_superset(j->type, j->unit->meta.job->type)) {
1093
1094 dbus_set_error(e, BUS_ERROR_TRANSACTION_IS_DESTRUCTIVE, "Transaction is destructive.");
e5b5ae50 1095 return -EEXIST;
398ef8ba 1096 }
e094e853 1097 }
11dd41ce 1098
e5b5ae50
LP
1099 return 0;
1100}
1101
e094e853
LP
1102static void transaction_minimize_impact(Manager *m) {
1103 bool again;
1104 assert(m);
1105
1106 /* Drops all unnecessary jobs that reverse already active jobs
1107 * or that stop a running service. */
1108
1109 do {
1110 Job *j;
034c6ed7 1111 Iterator i;
e094e853
LP
1112
1113 again = false;
1114
034c6ed7
LP
1115 HASHMAP_FOREACH(j, m->transaction_jobs, i) {
1116 LIST_FOREACH(transaction, j, j) {
c20cae32 1117 bool stops_running_service, changes_existing_job;
e094e853
LP
1118
1119 /* If it matters, we shouldn't drop it */
1120 if (j->matters_to_anchor)
1121 continue;
1122
1123 /* Would this stop a running service?
1124 * Would this change an existing job?
1125 * If so, let's drop this entry */
c20cae32
LP
1126
1127 stops_running_service =
1128 j->type == JOB_STOP && UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(j->unit));
1129
1130 changes_existing_job =
48a21c9b 1131 j->unit->meta.job && job_type_is_conflicting(j->type, j->unit->meta.job->type);
c20cae32
LP
1132
1133 if (!stops_running_service && !changes_existing_job)
e094e853
LP
1134 continue;
1135
c20cae32 1136 if (stops_running_service)
54165a39 1137 log_info("%s/%s would stop a running service.", j->unit->meta.id, job_type_to_string(j->type));
c20cae32
LP
1138
1139 if (changes_existing_job)
54165a39 1140 log_info("%s/%s would change existing job.", j->unit->meta.id, job_type_to_string(j->type));
c20cae32 1141
e094e853 1142 /* Ok, let's get rid of this */
54165a39 1143 log_info("Deleting %s/%s to minimize impact.", j->unit->meta.id, job_type_to_string(j->type));
c20cae32 1144
23a177ef 1145 transaction_delete_job(m, j, true);
e094e853
LP
1146 again = true;
1147 break;
1148 }
1149
1150 if (again)
1151 break;
1152 }
1153
1154 } while (again);
1155}
1156
c497c7a9 1157static int transaction_apply(Manager *m) {
034c6ed7 1158 Iterator i;
e5b5ae50
LP
1159 Job *j;
1160 int r;
1161
1ffba6fe
LP
1162 /* Moves the transaction jobs to the set of active jobs */
1163
034c6ed7 1164 HASHMAP_FOREACH(j, m->transaction_jobs, i) {
e094e853
LP
1165 /* Assume merged */
1166 assert(!j->transaction_prev);
1167 assert(!j->transaction_next);
1168
ac1135be 1169 if (j->installed)
e5b5ae50
LP
1170 continue;
1171
1172 if ((r = hashmap_put(m->jobs, UINT32_TO_PTR(j->id), j)) < 0)
11dd41ce
LP
1173 goto rollback;
1174 }
1175
e5b5ae50 1176 while ((j = hashmap_steal_first(m->transaction_jobs))) {
ac1135be 1177 if (j->installed)
e5b5ae50
LP
1178 continue;
1179
87f0e418
LP
1180 if (j->unit->meta.job)
1181 job_free(j->unit->meta.job);
11dd41ce 1182
87f0e418 1183 j->unit->meta.job = j;
ac1135be 1184 j->installed = true;
11dd41ce 1185
e5b5ae50
LP
1186 /* We're fully installed. Now let's free data we don't
1187 * need anymore. */
1188
1189 assert(!j->transaction_next);
1190 assert(!j->transaction_prev);
1191
c1e1601e
LP
1192 job_add_to_run_queue(j);
1193 job_add_to_dbus_queue(j);
faf919f1 1194 job_start_timer(j);
01184e04
LP
1195 }
1196
1197 /* As last step, kill all remaining job dependencies. */
f04fa1d5 1198 transaction_clean_dependencies(m);
1ffba6fe 1199
11dd41ce
LP
1200 return 0;
1201
1202rollback:
1203
034c6ed7 1204 HASHMAP_FOREACH(j, m->transaction_jobs, i) {
ac1135be 1205 if (j->installed)
e5b5ae50
LP
1206 continue;
1207
1208 hashmap_remove(m->jobs, UINT32_TO_PTR(j->id));
1209 }
1210
1211 return r;
1212}
1213
398ef8ba 1214static int transaction_activate(Manager *m, JobMode mode, DBusError *e) {
e5b5ae50
LP
1215 int r;
1216 unsigned generation = 1;
1217
1218 assert(m);
1219
1220 /* This applies the changes recorded in transaction_jobs to
1221 * the actual list of jobs, if possible. */
1222
1223 /* First step: figure out which jobs matter */
1224 transaction_find_jobs_that_matter_to_anchor(m, NULL, generation++);
1225
e094e853
LP
1226 /* Second step: Try not to stop any running services if
1227 * we don't have to. Don't try to reverse running
1228 * jobs if we don't have to. */
c88e7f4e
LP
1229 if (mode != JOB_ISOLATE)
1230 transaction_minimize_impact(m);
e094e853 1231
23a177ef
LP
1232 /* Third step: Drop redundant jobs */
1233 transaction_drop_redundant(m);
1234
1ffba6fe 1235 for (;;) {
23a177ef 1236 /* Fourth step: Let's remove unneeded jobs that might
1ffba6fe
LP
1237 * be lurking. */
1238 transaction_collect_garbage(m);
e5b5ae50 1239
23a177ef 1240 /* Fifth step: verify order makes sense and correct
1ffba6fe 1241 * cycles if necessary and possible */
398ef8ba 1242 if ((r = transaction_verify_order(m, &generation, e)) >= 0)
1ffba6fe 1243 break;
e5b5ae50 1244
9f04bd52 1245 if (r != -EAGAIN) {
398ef8ba 1246 log_warning("Requested transaction contains an unfixable cyclic ordering dependency: %s", bus_error(e, r));
1ffba6fe 1247 goto rollback;
9f04bd52 1248 }
e5b5ae50 1249
1ffba6fe
LP
1250 /* Let's see if the resulting transaction ordering
1251 * graph is still cyclic... */
1252 }
1253
1254 for (;;) {
23a177ef 1255 /* Sixth step: let's drop unmergeable entries if
1ffba6fe
LP
1256 * necessary and possible, merge entries we can
1257 * merge */
398ef8ba 1258 if ((r = transaction_merge_jobs(m, e)) >= 0)
1ffba6fe
LP
1259 break;
1260
9f04bd52 1261 if (r != -EAGAIN) {
398ef8ba 1262 log_warning("Requested transaction contains unmergable jobs: %s", bus_error(e, r));
1ffba6fe 1263 goto rollback;
9f04bd52 1264 }
1ffba6fe 1265
23a177ef 1266 /* Seventh step: an entry got dropped, let's garbage
1ffba6fe
LP
1267 * collect its dependencies. */
1268 transaction_collect_garbage(m);
1269
1270 /* Let's see if the resulting transaction still has
5cb5a6ff 1271 * unmergeable entries ... */
1ffba6fe
LP
1272 }
1273
23a177ef
LP
1274 /* Eights step: Drop redundant jobs again, if the merging now allows us to drop more. */
1275 transaction_drop_redundant(m);
1276
1277 /* Ninth step: check whether we can actually apply this */
e5b5ae50 1278 if (mode == JOB_FAIL)
398ef8ba
LP
1279 if ((r = transaction_is_destructive(m, e)) < 0) {
1280 log_notice("Requested transaction contradicts existing jobs: %s", bus_error(e, r));
e5b5ae50 1281 goto rollback;
9f04bd52 1282 }
e5b5ae50 1283
23a177ef 1284 /* Tenth step: apply changes */
c497c7a9 1285 if ((r = transaction_apply(m)) < 0) {
54165a39 1286 log_warning("Failed to apply transaction: %s", strerror(-r));
e5b5ae50 1287 goto rollback;
9f04bd52 1288 }
e5b5ae50
LP
1289
1290 assert(hashmap_isempty(m->transaction_jobs));
1291 assert(!m->transaction_anchor);
1292
1293 return 0;
11dd41ce 1294
e5b5ae50 1295rollback:
11dd41ce
LP
1296 transaction_abort(m);
1297 return r;
1298}
1299
9e2f7c11 1300static Job* transaction_add_one_job(Manager *m, JobType type, Unit *unit, bool override, bool *is_new) {
e5b5ae50 1301 Job *j, *f;
60918275
LP
1302
1303 assert(m);
87f0e418 1304 assert(unit);
60918275 1305
e5b5ae50
LP
1306 /* Looks for an axisting prospective job and returns that. If
1307 * it doesn't exist it is created and added to the prospective
1308 * jobs list. */
60918275 1309
87f0e418 1310 f = hashmap_get(m->transaction_jobs, unit);
60918275 1311
034c6ed7 1312 LIST_FOREACH(transaction, j, f) {
87f0e418 1313 assert(j->unit == unit);
60918275 1314
e5b5ae50
LP
1315 if (j->type == type) {
1316 if (is_new)
1317 *is_new = false;
1318 return j;
1319 }
1320 }
60918275 1321
87f0e418
LP
1322 if (unit->meta.job && unit->meta.job->type == type)
1323 j = unit->meta.job;
1324 else if (!(j = job_new(m, type, unit)))
e5b5ae50 1325 return NULL;
60918275 1326
e5b5ae50
LP
1327 j->generation = 0;
1328 j->marker = NULL;
1329 j->matters_to_anchor = false;
9e2f7c11 1330 j->override = override;
60918275 1331
034c6ed7
LP
1332 LIST_PREPEND(Job, transaction, f, j);
1333
e364ad06 1334 if (hashmap_replace(m->transaction_jobs, unit, f) < 0) {
034c6ed7
LP
1335 job_free(j);
1336 return NULL;
1337 }
1338
e5b5ae50
LP
1339 if (is_new)
1340 *is_new = true;
60918275 1341
9e2f7c11 1342 log_debug("Added job %s/%s to transaction.", unit->meta.id, job_type_to_string(type));
23a177ef 1343
e5b5ae50
LP
1344 return j;
1345}
11dd41ce 1346
23a177ef 1347void manager_transaction_unlink_job(Manager *m, Job *j, bool delete_dependencies) {
e5b5ae50
LP
1348 assert(m);
1349 assert(j);
11dd41ce 1350
e5b5ae50
LP
1351 if (j->transaction_prev)
1352 j->transaction_prev->transaction_next = j->transaction_next;
1353 else if (j->transaction_next)
87f0e418 1354 hashmap_replace(m->transaction_jobs, j->unit, j->transaction_next);
e5b5ae50 1355 else
87f0e418 1356 hashmap_remove_value(m->transaction_jobs, j->unit, j);
e5b5ae50
LP
1357
1358 if (j->transaction_next)
1359 j->transaction_next->transaction_prev = j->transaction_prev;
1360
1361 j->transaction_prev = j->transaction_next = NULL;
1362
1363 while (j->subject_list)
1364 job_dependency_free(j->subject_list);
1e198baf
LP
1365
1366 while (j->object_list) {
1367 Job *other = j->object_list->matters ? j->object_list->subject : NULL;
1368
e5b5ae50 1369 job_dependency_free(j->object_list);
1e198baf 1370
23a177ef 1371 if (other && delete_dependencies) {
2e81c8a5 1372 log_debug("Deleting job %s/%s as dependency of job %s/%s",
9e2f7c11
LP
1373 other->unit->meta.id, job_type_to_string(other->type),
1374 j->unit->meta.id, job_type_to_string(j->type));
23a177ef 1375 transaction_delete_job(m, other, delete_dependencies);
1e198baf
LP
1376 }
1377 }
e5b5ae50
LP
1378}
1379
9e2f7c11
LP
1380static int transaction_add_job_and_dependencies(
1381 Manager *m,
1382 JobType type,
1383 Unit *unit,
1384 Job *by,
1385 bool matters,
1386 bool override,
69dd2852 1387 bool conflicts,
398ef8ba 1388 DBusError *e,
9e2f7c11 1389 Job **_ret) {
e5b5ae50 1390 Job *ret;
034c6ed7 1391 Iterator i;
87f0e418 1392 Unit *dep;
e5b5ae50
LP
1393 int r;
1394 bool is_new;
1395
1396 assert(m);
1397 assert(type < _JOB_TYPE_MAX);
87f0e418 1398 assert(unit);
e5b5ae50 1399
fdf20a31 1400 if (unit->meta.load_state != UNIT_LOADED && unit->meta.load_state != UNIT_ERROR) {
8821a00f
LP
1401 dbus_set_error(e, BUS_ERROR_LOAD_FAILED, "Unit %s is not loaded properly.", unit->meta.id);
1402 return -EINVAL;
1403 }
1404
fdf20a31 1405 if (type != JOB_STOP && unit->meta.load_state == UNIT_ERROR) {
8821a00f
LP
1406 dbus_set_error(e, BUS_ERROR_LOAD_FAILED, "Unit %s failed to load: %s. You might find more information in the logs.",
1407 unit->meta.id,
1408 strerror(-unit->meta.load_error));
21b293e8 1409 return -EINVAL;
398ef8ba 1410 }
21b293e8 1411
398ef8ba
LP
1412 if (!unit_job_is_applicable(unit, type)) {
1413 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 1414 return -EBADR;
398ef8ba 1415 }
cd2dbd7d 1416
e5b5ae50 1417 /* First add the job. */
9e2f7c11 1418 if (!(ret = transaction_add_one_job(m, type, unit, override, &is_new)))
e5b5ae50
LP
1419 return -ENOMEM;
1420
1421 /* Then, add a link to the job. */
69dd2852 1422 if (!job_dependency_new(by, ret, matters, conflicts))
e5b5ae50
LP
1423 return -ENOMEM;
1424
1425 if (is_new) {
1426 /* Finally, recursively add in all dependencies. */
1427 if (type == JOB_START || type == JOB_RELOAD_OR_START) {
87f0e418 1428 SET_FOREACH(dep, ret->unit->meta.dependencies[UNIT_REQUIRES], i)
69dd2852 1429 if ((r = transaction_add_job_and_dependencies(m, JOB_START, dep, ret, true, override, false, e, NULL)) < 0 && r != -EBADR)
e5b5ae50 1430 goto fail;
9e2f7c11
LP
1431
1432 SET_FOREACH(dep, ret->unit->meta.dependencies[UNIT_REQUIRES_OVERRIDABLE], i)
69dd2852 1433 if ((r = transaction_add_job_and_dependencies(m, JOB_START, dep, ret, !override, override, false, e, NULL)) < 0 && r != -EBADR) {
65e92d67
LP
1434 log_warning("Cannot add dependency job for unit %s, ignoring: %s", dep->meta.id, bus_error(e, r));
1435 dbus_error_free(e);
1436 }
9e2f7c11 1437
87f0e418 1438 SET_FOREACH(dep, ret->unit->meta.dependencies[UNIT_WANTS], i)
69dd2852 1439 if ((r = transaction_add_job_and_dependencies(m, JOB_START, dep, ret, false, false, false, e, NULL)) < 0) {
65e92d67
LP
1440 log_warning("Cannot add dependency job for unit %s, ignoring: %s", dep->meta.id, bus_error(e, r));
1441 dbus_error_free(e);
1442 }
9e2f7c11 1443
87f0e418 1444 SET_FOREACH(dep, ret->unit->meta.dependencies[UNIT_REQUISITE], i)
69dd2852 1445 if ((r = transaction_add_job_and_dependencies(m, JOB_VERIFY_ACTIVE, dep, ret, true, override, false, e, NULL)) < 0 && r != -EBADR)
e5b5ae50 1446 goto fail;
9e2f7c11
LP
1447
1448 SET_FOREACH(dep, ret->unit->meta.dependencies[UNIT_REQUISITE_OVERRIDABLE], i)
69dd2852 1449 if ((r = transaction_add_job_and_dependencies(m, JOB_VERIFY_ACTIVE, dep, ret, !override, override, false, e, NULL)) < 0 && r != -EBADR) {
65e92d67
LP
1450 log_warning("Cannot add dependency job for unit %s, ignoring: %s", dep->meta.id, bus_error(e, r));
1451 dbus_error_free(e);
1452 }
9e2f7c11 1453
87f0e418 1454 SET_FOREACH(dep, ret->unit->meta.dependencies[UNIT_CONFLICTS], i)
69dd2852
LP
1455 if ((r = transaction_add_job_and_dependencies(m, JOB_STOP, dep, ret, true, override, true, e, NULL)) < 0 && r != -EBADR)
1456 goto fail;
1457
1458 SET_FOREACH(dep, ret->unit->meta.dependencies[UNIT_CONFLICTED_BY], i)
1459 if ((r = transaction_add_job_and_dependencies(m, JOB_STOP, dep, ret, true, override, false, e, NULL)) < 0 && r != -EBADR)
e5b5ae50
LP
1460 goto fail;
1461
1462 } else if (type == JOB_STOP || type == JOB_RESTART || type == JOB_TRY_RESTART) {
1463
87f0e418 1464 SET_FOREACH(dep, ret->unit->meta.dependencies[UNIT_REQUIRED_BY], i)
69dd2852 1465 if ((r = transaction_add_job_and_dependencies(m, type, dep, ret, true, override, false, e, NULL)) < 0 && r != -EBADR)
e5b5ae50
LP
1466 goto fail;
1467 }
1468
1469 /* JOB_VERIFY_STARTED, JOB_RELOAD require no dependency handling */
1470 }
60918275 1471
c0dafa48
LP
1472 if (_ret)
1473 *_ret = ret;
1474
60918275
LP
1475 return 0;
1476
1477fail:
e5b5ae50
LP
1478 return r;
1479}
1480
c497c7a9
LP
1481static int transaction_add_isolate_jobs(Manager *m) {
1482 Iterator i;
1483 Unit *u;
1484 char *k;
1485 int r;
1486
1487 assert(m);
1488
1489 HASHMAP_FOREACH_KEY(u, k, m->units, i) {
1490
1491 /* ignore aliases */
1492 if (u->meta.id != k)
1493 continue;
1494
1495 if (UNIT_VTABLE(u)->no_isolate)
1496 continue;
1497
1498 /* No need to stop inactive jobs */
fdf20a31 1499 if (UNIT_IS_INACTIVE_OR_FAILED(unit_active_state(u)))
c497c7a9
LP
1500 continue;
1501
1502 /* Is there already something listed for this? */
1503 if (hashmap_get(m->transaction_jobs, u))
1504 continue;
1505
69dd2852 1506 if ((r = transaction_add_job_and_dependencies(m, JOB_STOP, u, NULL, true, false, false, NULL, NULL)) < 0)
c497c7a9
LP
1507 log_warning("Cannot add isolate job for unit %s, ignoring: %s", u->meta.id, strerror(-r));
1508 }
1509
1510 return 0;
1511}
1512
398ef8ba 1513int manager_add_job(Manager *m, JobType type, Unit *unit, JobMode mode, bool override, DBusError *e, Job **_ret) {
e5b5ae50
LP
1514 int r;
1515 Job *ret;
1516
1517 assert(m);
1518 assert(type < _JOB_TYPE_MAX);
87f0e418 1519 assert(unit);
e5b5ae50 1520 assert(mode < _JOB_MODE_MAX);
60918275 1521
398ef8ba
LP
1522 if (mode == JOB_ISOLATE && type != JOB_START) {
1523 dbus_set_error(e, BUS_ERROR_INVALID_JOB_MODE, "Isolate is only valid for start.");
c497c7a9 1524 return -EINVAL;
398ef8ba 1525 }
c497c7a9 1526
2528a7a6
LP
1527 if (mode == JOB_ISOLATE && !unit->meta.allow_isolate) {
1528 dbus_set_error(e, BUS_ERROR_NO_ISOLATION, "Operation refused, unit may not be isolated.");
1529 return -EPERM;
1530 }
1531
9e2f7c11 1532 log_debug("Trying to enqueue job %s/%s", unit->meta.id, job_type_to_string(type));
9f04bd52 1533
69dd2852 1534 if ((r = transaction_add_job_and_dependencies(m, type, unit, NULL, true, override, false, e, &ret)) < 0) {
11dd41ce 1535 transaction_abort(m);
e5b5ae50
LP
1536 return r;
1537 }
11dd41ce 1538
c497c7a9
LP
1539 if (mode == JOB_ISOLATE)
1540 if ((r = transaction_add_isolate_jobs(m)) < 0) {
1541 transaction_abort(m);
1542 return r;
1543 }
1544
398ef8ba 1545 if ((r = transaction_activate(m, mode, e)) < 0)
e5b5ae50
LP
1546 return r;
1547
9e2f7c11 1548 log_debug("Enqueued job %s/%s as %u", unit->meta.id, job_type_to_string(type), (unsigned) ret->id);
f50e0a01 1549
e5b5ae50
LP
1550 if (_ret)
1551 *_ret = ret;
60918275 1552
e5b5ae50
LP
1553 return 0;
1554}
60918275 1555
398ef8ba 1556int manager_add_job_by_name(Manager *m, JobType type, const char *name, JobMode mode, bool override, DBusError *e, Job **_ret) {
28247076
LP
1557 Unit *unit;
1558 int r;
1559
1560 assert(m);
1561 assert(type < _JOB_TYPE_MAX);
1562 assert(name);
1563 assert(mode < _JOB_MODE_MAX);
1564
398ef8ba 1565 if ((r = manager_load_unit(m, name, NULL, NULL, &unit)) < 0)
28247076
LP
1566 return r;
1567
398ef8ba 1568 return manager_add_job(m, type, unit, mode, override, e, _ret);
28247076
LP
1569}
1570
60918275
LP
1571Job *manager_get_job(Manager *m, uint32_t id) {
1572 assert(m);
1573
1574 return hashmap_get(m->jobs, UINT32_TO_PTR(id));
1575}
1576
87f0e418 1577Unit *manager_get_unit(Manager *m, const char *name) {
60918275
LP
1578 assert(m);
1579 assert(name);
1580
87f0e418 1581 return hashmap_get(m->units, name);
60918275
LP
1582}
1583
c1e1601e 1584unsigned manager_dispatch_load_queue(Manager *m) {
60918275 1585 Meta *meta;
c1e1601e 1586 unsigned n = 0;
60918275
LP
1587
1588 assert(m);
1589
223dabab
LP
1590 /* Make sure we are not run recursively */
1591 if (m->dispatching_load_queue)
c1e1601e 1592 return 0;
223dabab
LP
1593
1594 m->dispatching_load_queue = true;
1595
87f0e418 1596 /* Dispatches the load queue. Takes a unit from the queue and
60918275
LP
1597 * tries to load its data until the queue is empty */
1598
1599 while ((meta = m->load_queue)) {
034c6ed7
LP
1600 assert(meta->in_load_queue);
1601
399ab2b1 1602 unit_load((Unit*) meta);
c1e1601e 1603 n++;
60918275
LP
1604 }
1605
223dabab 1606 m->dispatching_load_queue = false;
c1e1601e 1607 return n;
60918275
LP
1608}
1609
398ef8ba 1610int manager_load_unit_prepare(Manager *m, const char *name, const char *path, DBusError *e, Unit **_ret) {
87f0e418 1611 Unit *ret;
60918275
LP
1612 int r;
1613
1614 assert(m);
9e2f7c11 1615 assert(name || path);
60918275 1616
db06e3b6
LP
1617 /* This will prepare the unit for loading, but not actually
1618 * load anything from disk. */
0301abf4 1619
398ef8ba
LP
1620 if (path && !is_path(path)) {
1621 dbus_set_error(e, BUS_ERROR_INVALID_PATH, "Path %s is not absolute.", path);
9e2f7c11 1622 return -EINVAL;
398ef8ba 1623 }
9e2f7c11
LP
1624
1625 if (!name)
1626 name = file_name_from_path(path);
1627
398ef8ba
LP
1628 if (!unit_name_is_valid(name)) {
1629 dbus_set_error(e, BUS_ERROR_INVALID_NAME, "Unit name %s is not valid.", name);
9e2f7c11 1630 return -EINVAL;
398ef8ba 1631 }
60918275 1632
87f0e418 1633 if ((ret = manager_get_unit(m, name))) {
034c6ed7 1634 *_ret = ret;
413d6313 1635 return 1;
034c6ed7 1636 }
60918275 1637
87f0e418 1638 if (!(ret = unit_new(m)))
60918275
LP
1639 return -ENOMEM;
1640
9e2f7c11 1641 if (path)
6be1e7d5 1642 if (!(ret->meta.fragment_path = strdup(path))) {
0301abf4
LP
1643 unit_free(ret);
1644 return -ENOMEM;
1645 }
0301abf4 1646
87f0e418
LP
1647 if ((r = unit_add_name(ret, name)) < 0) {
1648 unit_free(ret);
1ffba6fe 1649 return r;
60918275
LP
1650 }
1651
87f0e418 1652 unit_add_to_load_queue(ret);
c1e1601e 1653 unit_add_to_dbus_queue(ret);
949061f0 1654 unit_add_to_gc_queue(ret);
c1e1601e 1655
db06e3b6
LP
1656 if (_ret)
1657 *_ret = ret;
1658
1659 return 0;
1660}
1661
398ef8ba 1662int manager_load_unit(Manager *m, const char *name, const char *path, DBusError *e, Unit **_ret) {
db06e3b6
LP
1663 int r;
1664
1665 assert(m);
1666
1667 /* This will load the service information files, but not actually
1668 * start any services or anything. */
1669
398ef8ba 1670 if ((r = manager_load_unit_prepare(m, name, path, e, _ret)) != 0)
db06e3b6
LP
1671 return r;
1672
f50e0a01 1673 manager_dispatch_load_queue(m);
60918275 1674
9e2f7c11 1675 if (_ret)
413d6313 1676 *_ret = unit_follow_merge(*_ret);
9e2f7c11 1677
60918275
LP
1678 return 0;
1679}
a66d02c3 1680
cea8e32e 1681void manager_dump_jobs(Manager *s, FILE *f, const char *prefix) {
034c6ed7 1682 Iterator i;
a66d02c3
LP
1683 Job *j;
1684
1685 assert(s);
1686 assert(f);
1687
034c6ed7 1688 HASHMAP_FOREACH(j, s->jobs, i)
cea8e32e 1689 job_dump(j, f, prefix);
a66d02c3
LP
1690}
1691
87f0e418 1692void manager_dump_units(Manager *s, FILE *f, const char *prefix) {
034c6ed7 1693 Iterator i;
87f0e418 1694 Unit *u;
11dd41ce 1695 const char *t;
a66d02c3
LP
1696
1697 assert(s);
1698 assert(f);
1699
87f0e418 1700 HASHMAP_FOREACH_KEY(u, t, s->units, i)
9e2f7c11 1701 if (u->meta.id == t)
87f0e418 1702 unit_dump(u, f, prefix);
a66d02c3 1703}
7fad411c
LP
1704
1705void manager_clear_jobs(Manager *m) {
1706 Job *j;
1707
1708 assert(m);
1709
1710 transaction_abort(m);
1711
1712 while ((j = hashmap_first(m->jobs)))
1713 job_free(j);
1714}
83c60c9f 1715
c1e1601e 1716unsigned manager_dispatch_run_queue(Manager *m) {
83c60c9f 1717 Job *j;
c1e1601e 1718 unsigned n = 0;
83c60c9f 1719
034c6ed7 1720 if (m->dispatching_run_queue)
c1e1601e 1721 return 0;
034c6ed7
LP
1722
1723 m->dispatching_run_queue = true;
9152c765 1724
034c6ed7 1725 while ((j = m->run_queue)) {
ac1135be 1726 assert(j->installed);
034c6ed7
LP
1727 assert(j->in_run_queue);
1728
1729 job_run_and_invalidate(j);
c1e1601e 1730 n++;
9152c765 1731 }
034c6ed7
LP
1732
1733 m->dispatching_run_queue = false;
c1e1601e
LP
1734 return n;
1735}
1736
1737unsigned manager_dispatch_dbus_queue(Manager *m) {
1738 Job *j;
1739 Meta *meta;
1740 unsigned n = 0;
1741
1742 assert(m);
1743
1744 if (m->dispatching_dbus_queue)
1745 return 0;
1746
1747 m->dispatching_dbus_queue = true;
1748
1749 while ((meta = m->dbus_unit_queue)) {
23a177ef 1750 assert(meta->in_dbus_queue);
c1e1601e 1751
399ab2b1 1752 bus_unit_send_change_signal((Unit*) meta);
c1e1601e
LP
1753 n++;
1754 }
1755
1756 while ((j = m->dbus_job_queue)) {
1757 assert(j->in_dbus_queue);
1758
1759 bus_job_send_change_signal(j);
1760 n++;
1761 }
1762
1763 m->dispatching_dbus_queue = false;
1764 return n;
9152c765
LP
1765}
1766
8c47c732
LP
1767static int manager_process_notify_fd(Manager *m) {
1768 ssize_t n;
1769
1770 assert(m);
1771
1772 for (;;) {
1773 char buf[4096];
1774 struct msghdr msghdr;
1775 struct iovec iovec;
1776 struct ucred *ucred;
1777 union {
1778 struct cmsghdr cmsghdr;
1779 uint8_t buf[CMSG_SPACE(sizeof(struct ucred))];
1780 } control;
1781 Unit *u;
1782 char **tags;
1783
1784 zero(iovec);
1785 iovec.iov_base = buf;
1786 iovec.iov_len = sizeof(buf)-1;
1787
1788 zero(control);
1789 zero(msghdr);
1790 msghdr.msg_iov = &iovec;
1791 msghdr.msg_iovlen = 1;
1792 msghdr.msg_control = &control;
1793 msghdr.msg_controllen = sizeof(control);
1794
1795 if ((n = recvmsg(m->notify_watch.fd, &msghdr, MSG_DONTWAIT)) <= 0) {
1796 if (n >= 0)
1797 return -EIO;
1798
f6144808 1799 if (errno == EAGAIN || errno == EINTR)
8c47c732
LP
1800 break;
1801
1802 return -errno;
1803 }
1804
1805 if (msghdr.msg_controllen < CMSG_LEN(sizeof(struct ucred)) ||
1806 control.cmsghdr.cmsg_level != SOL_SOCKET ||
1807 control.cmsghdr.cmsg_type != SCM_CREDENTIALS ||
1808 control.cmsghdr.cmsg_len != CMSG_LEN(sizeof(struct ucred))) {
1809 log_warning("Received notify message without credentials. Ignoring.");
1810 continue;
1811 }
1812
1813 ucred = (struct ucred*) CMSG_DATA(&control.cmsghdr);
1814
c6c18be3 1815 if (!(u = hashmap_get(m->watch_pids, LONG_TO_PTR(ucred->pid))))
8c47c732
LP
1816 if (!(u = cgroup_unit_by_pid(m, ucred->pid))) {
1817 log_warning("Cannot find unit for notify message of PID %lu.", (unsigned long) ucred->pid);
1818 continue;
1819 }
1820
8c40acf7
LP
1821 assert((size_t) n < sizeof(buf));
1822 buf[n] = 0;
8c47c732
LP
1823 if (!(tags = strv_split(buf, "\n\r")))
1824 return -ENOMEM;
1825
1826 log_debug("Got notification message for unit %s", u->meta.id);
1827
1828 if (UNIT_VTABLE(u)->notify_message)
c952c6ec 1829 UNIT_VTABLE(u)->notify_message(u, ucred->pid, tags);
8c47c732
LP
1830
1831 strv_free(tags);
1832 }
1833
1834 return 0;
1835}
1836
034c6ed7 1837static int manager_dispatch_sigchld(Manager *m) {
9152c765
LP
1838 assert(m);
1839
1840 for (;;) {
1841 siginfo_t si;
87f0e418 1842 Unit *u;
8c47c732 1843 int r;
9152c765
LP
1844
1845 zero(si);
4112df16
LP
1846
1847 /* First we call waitd() for a PID and do not reap the
1848 * zombie. That way we can still access /proc/$PID for
1849 * it while it is a zombie. */
1850 if (waitid(P_ALL, 0, &si, WEXITED|WNOHANG|WNOWAIT) < 0) {
acbb0225
LP
1851
1852 if (errno == ECHILD)
1853 break;
1854
4112df16
LP
1855 if (errno == EINTR)
1856 continue;
1857
9152c765 1858 return -errno;
acbb0225 1859 }
9152c765 1860
4112df16 1861 if (si.si_pid <= 0)
9152c765
LP
1862 break;
1863
15d5d9d9 1864 if (si.si_code == CLD_EXITED || si.si_code == CLD_KILLED || si.si_code == CLD_DUMPED) {
4112df16
LP
1865 char *name = NULL;
1866
1867 get_process_name(si.si_pid, &name);
bb00e604 1868 log_debug("Got SIGCHLD for process %lu (%s)", (unsigned long) si.si_pid, strna(name));
4112df16
LP
1869 free(name);
1870 }
1871
8c47c732
LP
1872 /* Let's flush any message the dying child might still
1873 * have queued for us. This ensures that the process
1874 * still exists in /proc so that we can figure out
1875 * which cgroup and hence unit it belongs to. */
1876 if ((r = manager_process_notify_fd(m)) < 0)
1877 return r;
1878
1879 /* And now figure out the unit this belongs to */
c6c18be3 1880 if (!(u = hashmap_get(m->watch_pids, LONG_TO_PTR(si.si_pid))))
8c47c732
LP
1881 u = cgroup_unit_by_pid(m, si.si_pid);
1882
4112df16
LP
1883 /* And now, we actually reap the zombie. */
1884 if (waitid(P_PID, si.si_pid, &si, WEXITED) < 0) {
1885 if (errno == EINTR)
1886 continue;
1887
1888 return -errno;
1889 }
1890
034c6ed7
LP
1891 if (si.si_code != CLD_EXITED && si.si_code != CLD_KILLED && si.si_code != CLD_DUMPED)
1892 continue;
1893
bb00e604
LP
1894 log_debug("Child %lu died (code=%s, status=%i/%s)",
1895 (long unsigned) si.si_pid,
4112df16
LP
1896 sigchld_code_to_string(si.si_code),
1897 si.si_status,
d06dacd0
LP
1898 strna(si.si_code == CLD_EXITED
1899 ? exit_status_to_string(si.si_status, EXIT_STATUS_FULL)
1900 : signal_to_string(si.si_status)));
acbb0225 1901
8c47c732 1902 if (!u)
9152c765
LP
1903 continue;
1904
c6c18be3 1905 log_debug("Child %lu belongs to %s", (long unsigned) si.si_pid, u->meta.id);
6c1a0478 1906
c6c18be3 1907 hashmap_remove(m->watch_pids, LONG_TO_PTR(si.si_pid));
87f0e418 1908 UNIT_VTABLE(u)->sigchld_event(u, si.si_pid, si.si_code, si.si_status);
9152c765
LP
1909 }
1910
1911 return 0;
1912}
1913
7d793605 1914static int manager_start_target(Manager *m, const char *name, JobMode mode) {
28247076 1915 int r;
398ef8ba
LP
1916 DBusError error;
1917
1918 dbus_error_init(&error);
1919
1e001f52
LP
1920 log_info("Activating special unit %s", name);
1921
398ef8ba
LP
1922 if ((r = manager_add_job_by_name(m, JOB_START, name, mode, true, &error, NULL)) < 0)
1923 log_error("Failed to enqueue %s job: %s", name, bus_error(&error, r));
28247076 1924
398ef8ba 1925 dbus_error_free(&error);
a1b256b0
LP
1926
1927 return r;
28247076
LP
1928}
1929
a16e1123 1930static int manager_process_signal_fd(Manager *m) {
9152c765
LP
1931 ssize_t n;
1932 struct signalfd_siginfo sfsi;
1933 bool sigchld = false;
1934
1935 assert(m);
1936
1937 for (;;) {
acbb0225 1938 if ((n = read(m->signal_watch.fd, &sfsi, sizeof(sfsi))) != sizeof(sfsi)) {
9152c765
LP
1939
1940 if (n >= 0)
1941 return -EIO;
1942
63090775 1943 if (errno == EINTR || errno == EAGAIN)
acbb0225 1944 break;
9152c765
LP
1945
1946 return -errno;
1947 }
1948
1e001f52
LP
1949 log_debug("Received SIG%s", strna(signal_to_string(sfsi.ssi_signo)));
1950
b9cd2ec1
LP
1951 switch (sfsi.ssi_signo) {
1952
4112df16 1953 case SIGCHLD:
9152c765 1954 sigchld = true;
b9cd2ec1
LP
1955 break;
1956
6632c602 1957 case SIGTERM:
a3d4e06d 1958 if (m->running_as == MANAGER_SYSTEM) {
db06e3b6
LP
1959 /* This is for compatibility with the
1960 * original sysvinit */
e11dc4a2 1961 m->exit_code = MANAGER_REEXECUTE;
a1b256b0
LP
1962 break;
1963 }
84e9af1e 1964
a1b256b0 1965 /* Fall through */
e11dc4a2
LP
1966
1967 case SIGINT:
a3d4e06d 1968 if (m->running_as == MANAGER_SYSTEM) {
7d793605 1969 manager_start_target(m, SPECIAL_CTRL_ALT_DEL_TARGET, JOB_REPLACE);
84e9af1e
LP
1970 break;
1971 }
1972
a1b256b0 1973 /* Run the exit target if there is one, if not, just exit. */
7d793605 1974 if (manager_start_target(m, SPECIAL_EXIT_SERVICE, JOB_REPLACE) < 0) {
a1b256b0
LP
1975 m->exit_code = MANAGER_EXIT;
1976 return 0;
1977 }
1978
1979 break;
84e9af1e 1980
28247076 1981 case SIGWINCH:
a3d4e06d 1982 if (m->running_as == MANAGER_SYSTEM)
7d793605 1983 manager_start_target(m, SPECIAL_KBREQUEST_TARGET, JOB_REPLACE);
84e9af1e 1984
28247076
LP
1985 /* This is a nop on non-init */
1986 break;
84e9af1e 1987
28247076 1988 case SIGPWR:
a3d4e06d 1989 if (m->running_as == MANAGER_SYSTEM)
7d793605 1990 manager_start_target(m, SPECIAL_SIGPWR_TARGET, JOB_REPLACE);
84e9af1e 1991
28247076 1992 /* This is a nop on non-init */
84e9af1e 1993 break;
6632c602 1994
1005d14f 1995 case SIGUSR1: {
57ee42ce
LP
1996 Unit *u;
1997
1998 u = manager_get_unit(m, SPECIAL_DBUS_SERVICE);
1999
2000 if (!u || UNIT_IS_ACTIVE_OR_RELOADING(unit_active_state(u))) {
2001 log_info("Trying to reconnect to bus...");
5e8d1c9a 2002 bus_init(m);
57ee42ce
LP
2003 }
2004
2005 if (!u || !UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(u))) {
2006 log_info("Loading D-Bus service...");
7d793605 2007 manager_start_target(m, SPECIAL_DBUS_SERVICE, JOB_REPLACE);
57ee42ce
LP
2008 }
2009
2010 break;
2011 }
2012
2149e37c
LP
2013 case SIGUSR2: {
2014 FILE *f;
2015 char *dump = NULL;
2016 size_t size;
2017
2018 if (!(f = open_memstream(&dump, &size))) {
2019 log_warning("Failed to allocate memory stream.");
2020 break;
2021 }
2022
2023 manager_dump_units(m, f, "\t");
2024 manager_dump_jobs(m, f, "\t");
2025
2026 if (ferror(f)) {
2027 fclose(f);
2028 free(dump);
2029 log_warning("Failed to write status stream");
2030 break;
2031 }
2032
2033 fclose(f);
2034 log_dump(LOG_INFO, dump);
2035 free(dump);
2036
1005d14f 2037 break;
2149e37c 2038 }
1005d14f 2039
a16e1123
LP
2040 case SIGHUP:
2041 m->exit_code = MANAGER_RELOAD;
2042 break;
2043
7d793605
LP
2044 default: {
2045 static const char * const table[] = {
2046 [0] = SPECIAL_DEFAULT_TARGET,
2047 [1] = SPECIAL_RESCUE_TARGET,
f057408c 2048 [2] = SPECIAL_EMERGENCY_TARGET,
7d793605
LP
2049 [3] = SPECIAL_HALT_TARGET,
2050 [4] = SPECIAL_POWEROFF_TARGET,
2051 [5] = SPECIAL_REBOOT_TARGET
2052 };
2053
2054 if ((int) sfsi.ssi_signo >= SIGRTMIN+0 &&
2055 (int) sfsi.ssi_signo < SIGRTMIN+(int) ELEMENTSOF(table)) {
2056 manager_start_target(m, table[sfsi.ssi_signo - SIGRTMIN],
e6b3f00f 2057 (sfsi.ssi_signo == 1 || sfsi.ssi_signo == 2) ? JOB_ISOLATE : JOB_REPLACE);
7d793605
LP
2058 break;
2059 }
2060
1e001f52 2061 log_warning("Got unhandled signal <%s>.", strna(signal_to_string(sfsi.ssi_signo)));
b9cd2ec1 2062 }
7d793605 2063 }
9152c765
LP
2064 }
2065
2066 if (sigchld)
034c6ed7
LP
2067 return manager_dispatch_sigchld(m);
2068
2069 return 0;
2070}
2071
a16e1123 2072static int process_event(Manager *m, struct epoll_event *ev) {
034c6ed7 2073 int r;
acbb0225 2074 Watch *w;
034c6ed7
LP
2075
2076 assert(m);
2077 assert(ev);
2078
acbb0225 2079 assert(w = ev->data.ptr);
034c6ed7 2080
acbb0225 2081 switch (w->type) {
034c6ed7 2082
ef734fd6 2083 case WATCH_SIGNAL:
034c6ed7 2084
acbb0225 2085 /* An incoming signal? */
f94ea366 2086 if (ev->events != EPOLLIN)
acbb0225 2087 return -EINVAL;
034c6ed7 2088
a16e1123 2089 if ((r = manager_process_signal_fd(m)) < 0)
acbb0225 2090 return r;
034c6ed7 2091
acbb0225 2092 break;
034c6ed7 2093
8c47c732
LP
2094 case WATCH_NOTIFY:
2095
2096 /* An incoming daemon notification event? */
2097 if (ev->events != EPOLLIN)
2098 return -EINVAL;
2099
2100 if ((r = manager_process_notify_fd(m)) < 0)
2101 return r;
2102
2103 break;
2104
acbb0225 2105 case WATCH_FD:
034c6ed7 2106
acbb0225 2107 /* Some fd event, to be dispatched to the units */
ea430986 2108 UNIT_VTABLE(w->data.unit)->fd_event(w->data.unit, w->fd, ev->events, w);
acbb0225 2109 break;
034c6ed7 2110
faf919f1
LP
2111 case WATCH_UNIT_TIMER:
2112 case WATCH_JOB_TIMER: {
acbb0225
LP
2113 uint64_t v;
2114 ssize_t k;
034c6ed7 2115
acbb0225 2116 /* Some timer event, to be dispatched to the units */
be888ebb 2117 if ((k = read(w->fd, &v, sizeof(v))) != sizeof(v)) {
034c6ed7 2118
acbb0225
LP
2119 if (k < 0 && (errno == EINTR || errno == EAGAIN))
2120 break;
034c6ed7 2121
acbb0225 2122 return k < 0 ? -errno : -EIO;
034c6ed7
LP
2123 }
2124
faf919f1
LP
2125 if (w->type == WATCH_UNIT_TIMER)
2126 UNIT_VTABLE(w->data.unit)->timer_event(w->data.unit, v, w);
2127 else
2128 job_timer_event(w->data.job, v, w);
acbb0225
LP
2129 break;
2130 }
2131
ef734fd6
LP
2132 case WATCH_MOUNT:
2133 /* Some mount table change, intended for the mount subsystem */
2134 mount_fd_event(m, ev->events);
2135 break;
2136
f94ea366
LP
2137 case WATCH_UDEV:
2138 /* Some notification from udev, intended for the device subsystem */
2139 device_fd_event(m, ev->events);
2140 break;
2141
ea430986
LP
2142 case WATCH_DBUS_WATCH:
2143 bus_watch_event(m, w, ev->events);
2144 break;
2145
2146 case WATCH_DBUS_TIMEOUT:
2147 bus_timeout_event(m, w, ev->events);
2148 break;
2149
acbb0225
LP
2150 default:
2151 assert_not_reached("Unknown epoll event type.");
034c6ed7 2152 }
9152c765
LP
2153
2154 return 0;
2155}
2156
2157int manager_loop(Manager *m) {
2158 int r;
9152c765 2159
ea430986
LP
2160 RATELIMIT_DEFINE(rl, 1*USEC_PER_SEC, 1000);
2161
9152c765 2162 assert(m);
a16e1123 2163 m->exit_code = MANAGER_RUNNING;
9152c765 2164
fe51822e
LP
2165 /* Release the path cache */
2166 set_free_free(m->unit_path_cache);
2167 m->unit_path_cache = NULL;
2168
a4312405
LP
2169 /* There might still be some zombies hanging around from
2170 * before we were exec()'ed. Leat's reap them */
2171 if ((r = manager_dispatch_sigchld(m)) < 0)
2172 return r;
2173
a16e1123 2174 while (m->exit_code == MANAGER_RUNNING) {
957ca890
LP
2175 struct epoll_event event;
2176 int n;
9152c765 2177
ea430986
LP
2178 if (!ratelimit_test(&rl)) {
2179 /* Yay, something is going seriously wrong, pause a little */
2180 log_warning("Looping too fast. Throttling execution a little.");
2181 sleep(1);
2182 }
2183
37a8e683 2184 if (manager_dispatch_load_queue(m) > 0)
23a177ef
LP
2185 continue;
2186
37a8e683 2187 if (manager_dispatch_run_queue(m) > 0)
701cc384
LP
2188 continue;
2189
37a8e683 2190 if (bus_dispatch(m) > 0)
c1e1601e 2191 continue;
034c6ed7 2192
37a8e683 2193 if (manager_dispatch_cleanup_queue(m) > 0)
c1e1601e
LP
2194 continue;
2195
37a8e683 2196 if (manager_dispatch_gc_queue(m) > 0)
c1e1601e
LP
2197 continue;
2198
2199 if (manager_dispatch_dbus_queue(m) > 0)
ea430986 2200 continue;
ea430986 2201
957ca890 2202 if ((n = epoll_wait(m->epoll_fd, &event, 1, -1)) < 0) {
9152c765 2203
6089f4a9 2204 if (errno == EINTR)
9152c765
LP
2205 continue;
2206
2207 return -errno;
2208 }
2209
957ca890 2210 assert(n == 1);
b9cd2ec1 2211
a16e1123 2212 if ((r = process_event(m, &event)) < 0)
957ca890 2213 return r;
a16e1123 2214 }
957ca890 2215
a16e1123 2216 return m->exit_code;
83c60c9f 2217}
ea430986
LP
2218
2219int manager_get_unit_from_dbus_path(Manager *m, const char *s, Unit **_u) {
2220 char *n;
2221 Unit *u;
2222
2223 assert(m);
2224 assert(s);
2225 assert(_u);
2226
2227 if (!startswith(s, "/org/freedesktop/systemd1/unit/"))
2228 return -EINVAL;
2229
2230 if (!(n = bus_path_unescape(s+31)))
2231 return -ENOMEM;
2232
2233 u = manager_get_unit(m, n);
2234 free(n);
2235
2236 if (!u)
2237 return -ENOENT;
2238
2239 *_u = u;
2240
2241 return 0;
2242}
86fbf370
LP
2243
2244int manager_get_job_from_dbus_path(Manager *m, const char *s, Job **_j) {
2245 Job *j;
2246 unsigned id;
2247 int r;
2248
2249 assert(m);
2250 assert(s);
2251 assert(_j);
2252
2253 if (!startswith(s, "/org/freedesktop/systemd1/job/"))
2254 return -EINVAL;
2255
2256 if ((r = safe_atou(s + 30, &id)) < 0)
2257 return r;
2258
2259 if (!(j = manager_get_job(m, id)))
2260 return -ENOENT;
2261
2262 *_j = j;
2263
2264 return 0;
2265}
dfcd764e 2266
4927fcae 2267void manager_send_unit_audit(Manager *m, Unit *u, int type, bool success) {
e537352b 2268
4927fcae
LP
2269#ifdef HAVE_AUDIT
2270 char *p;
e537352b 2271
4927fcae 2272 if (m->audit_fd < 0)
e537352b
LP
2273 return;
2274
bbd3a7ba
LP
2275 /* Don't generate audit events if the service was already
2276 * started and we're just deserializing */
2277 if (m->n_deserializing > 0)
2278 return;
2279
4927fcae
LP
2280 if (!(p = unit_name_to_prefix_and_instance(u->meta.id))) {
2281 log_error("Failed to allocate unit name for audit message: %s", strerror(ENOMEM));
e537352b
LP
2282 return;
2283 }
2284
4927fcae
LP
2285 if (audit_log_user_comm_message(m->audit_fd, type, "", p, NULL, NULL, NULL, success) < 0)
2286 log_error("Failed to send audit message: %m");
e537352b 2287
4927fcae
LP
2288 free(p);
2289#endif
e537352b 2290
e537352b
LP
2291}
2292
05e343b7
LP
2293void manager_dispatch_bus_name_owner_changed(
2294 Manager *m,
2295 const char *name,
2296 const char* old_owner,
2297 const char *new_owner) {
2298
2299 Unit *u;
2300
2301 assert(m);
2302 assert(name);
2303
2304 if (!(u = hashmap_get(m->watch_bus, name)))
2305 return;
2306
2307 UNIT_VTABLE(u)->bus_name_owner_change(u, name, old_owner, new_owner);
2308}
2309
2310void manager_dispatch_bus_query_pid_done(
2311 Manager *m,
2312 const char *name,
2313 pid_t pid) {
2314
2315 Unit *u;
2316
2317 assert(m);
2318 assert(name);
2319 assert(pid >= 1);
2320
2321 if (!(u = hashmap_get(m->watch_bus, name)))
2322 return;
2323
2324 UNIT_VTABLE(u)->bus_query_pid_done(u, name, pid);
2325}
2326
d8d5ab98 2327int manager_open_serialization(Manager *m, FILE **_f) {
a16e1123
LP
2328 char *path;
2329 mode_t saved_umask;
2330 int fd;
2331 FILE *f;
2332
2333 assert(_f);
2334
d8d5ab98
LP
2335 if (m->running_as == MANAGER_SYSTEM) {
2336 mkdir_p("/dev/.systemd", 0755);
2337
2338 if (asprintf(&path, "/dev/.systemd/dump-%lu-XXXXXX", (unsigned long) getpid()) < 0)
2339 return -ENOMEM;
2340 } else {
2341 if (asprintf(&path, "/tmp/systemd-dump-%lu-XXXXXX", (unsigned long) getpid()) < 0)
2342 return -ENOMEM;
2343 }
a16e1123
LP
2344
2345 saved_umask = umask(0077);
2346 fd = mkostemp(path, O_RDWR|O_CLOEXEC);
2347 umask(saved_umask);
2348
2349 if (fd < 0) {
2350 free(path);
2351 return -errno;
2352 }
2353
2354 unlink(path);
2355
2356 log_debug("Serializing state to %s", path);
2357 free(path);
2358
2359 if (!(f = fdopen(fd, "w+")) < 0)
2360 return -errno;
2361
2362 *_f = f;
2363
2364 return 0;
2365}
2366
2367int manager_serialize(Manager *m, FILE *f, FDSet *fds) {
2368 Iterator i;
2369 Unit *u;
2370 const char *t;
2371 int r;
2372
2373 assert(m);
2374 assert(f);
2375 assert(fds);
2376
10f8e83c
LP
2377 fprintf(f, "startup-timestamp=%llu %llu\n\n",
2378 (unsigned long long) m->startup_timestamp.realtime,
2379 (unsigned long long) m->startup_timestamp.monotonic);
2380
a16e1123
LP
2381 HASHMAP_FOREACH_KEY(u, t, m->units, i) {
2382 if (u->meta.id != t)
2383 continue;
2384
2385 if (!unit_can_serialize(u))
2386 continue;
2387
2388 /* Start marker */
2389 fputs(u->meta.id, f);
2390 fputc('\n', f);
2391
2392 if ((r = unit_serialize(u, f, fds)) < 0)
2393 return r;
2394 }
2395
2396 if (ferror(f))
2397 return -EIO;
2398
2399 return 0;
2400}
2401
2402int manager_deserialize(Manager *m, FILE *f, FDSet *fds) {
2403 int r = 0;
2404
2405 assert(m);
2406 assert(f);
2407
2408 log_debug("Deserializing state...");
2409
9f611ad8 2410 m->n_deserializing ++;
82c64bf5 2411
10f8e83c
LP
2412 for (;;) {
2413 char line[1024], *l;
2414
2415 if (!fgets(line, sizeof(line), f)) {
2416 if (feof(f))
2417 r = 0;
2418 else
2419 r = -errno;
2420
2421 goto finish;
2422 }
2423
2424 char_array_0(line);
2425 l = strstrip(line);
2426
2427 if (l[0] == 0)
2428 break;
2429
2430 if (startswith(l, "startup-timestamp=")) {
2431 unsigned long long a, b;
2432
2433 if (sscanf(l+18, "%lli %llu", &a, &b) != 2)
2434 log_debug("Failed to parse startup timestamp value %s", l+18);
2435 else {
2436 m->startup_timestamp.realtime = a;
2437 m->startup_timestamp.monotonic = b;
2438 }
2439 } else
2440 log_debug("Unknown serialization item '%s'", l);
2441 }
2442
a16e1123
LP
2443 for (;;) {
2444 Unit *u;
2445 char name[UNIT_NAME_MAX+2];
2446
2447 /* Start marker */
2448 if (!fgets(name, sizeof(name), f)) {
2449 if (feof(f))
10f8e83c
LP
2450 r = 0;
2451 else
2452 r = -errno;
a16e1123 2453
82c64bf5 2454 goto finish;
a16e1123
LP
2455 }
2456
2457 char_array_0(name);
2458
398ef8ba 2459 if ((r = manager_load_unit(m, strstrip(name), NULL, NULL, &u)) < 0)
82c64bf5 2460 goto finish;
a16e1123
LP
2461
2462 if ((r = unit_deserialize(u, f, fds)) < 0)
82c64bf5 2463 goto finish;
a16e1123
LP
2464 }
2465
10f8e83c 2466finish:
82c64bf5
LP
2467 if (ferror(f)) {
2468 r = -EIO;
2469 goto finish;
2470 }
a16e1123 2471
9f611ad8
LP
2472 assert(m->n_deserializing > 0);
2473 m->n_deserializing --;
82c64bf5
LP
2474
2475 return r;
a16e1123
LP
2476}
2477
2478int manager_reload(Manager *m) {
2479 int r, q;
2480 FILE *f;
2481 FDSet *fds;
2482
2483 assert(m);
2484
d8d5ab98 2485 if ((r = manager_open_serialization(m, &f)) < 0)
a16e1123
LP
2486 return r;
2487
2488 if (!(fds = fdset_new())) {
2489 r = -ENOMEM;
2490 goto finish;
2491 }
2492
2493 if ((r = manager_serialize(m, f, fds)) < 0)
2494 goto finish;
2495
2496 if (fseeko(f, 0, SEEK_SET) < 0) {
2497 r = -errno;
2498 goto finish;
2499 }
2500
2501 /* From here on there is no way back. */
2502 manager_clear_jobs_and_units(m);
2503
2ded0c04 2504 /* Find new unit paths */
84e3543e
LP
2505 lookup_paths_free(&m->lookup_paths);
2506 if ((q = lookup_paths_init(&m->lookup_paths, m->running_as)) < 0)
2ded0c04
LP
2507 r = q;
2508
9f611ad8
LP
2509 m->n_deserializing ++;
2510
a16e1123
LP
2511 /* First, enumerate what we can from all config files */
2512 if ((q = manager_enumerate(m)) < 0)
2513 r = q;
2514
2515 /* Second, deserialize our stored data */
2516 if ((q = manager_deserialize(m, f, fds)) < 0)
2517 r = q;
2518
2519 fclose(f);
2520 f = NULL;
2521
2522 /* Third, fire things up! */
2523 if ((q = manager_coldplug(m)) < 0)
2524 r = q;
2525
9f611ad8
LP
2526 assert(m->n_deserializing > 0);
2527 m->n_deserializing ++;
2528
a16e1123
LP
2529finish:
2530 if (f)
2531 fclose(f);
2532
2533 if (fds)
2534 fdset_free(fds);
2535
2536 return r;
2537}
2538
9e58ff9c
LP
2539bool manager_is_booting_or_shutting_down(Manager *m) {
2540 Unit *u;
2541
2542 assert(m);
2543
2544 /* Is the initial job still around? */
2545 if (manager_get_job(m, 1))
2546 return true;
2547
2548 /* Is there a job for the shutdown target? */
2549 if (((u = manager_get_unit(m, SPECIAL_SHUTDOWN_TARGET))))
2550 return !!u->meta.job;
2551
2552 return false;
2553}
2554
fdf20a31 2555void manager_reset_failed(Manager *m) {
5632e374
LP
2556 Unit *u;
2557 Iterator i;
2558
2559 assert(m);
2560
2561 HASHMAP_FOREACH(u, m->units, i)
fdf20a31 2562 unit_reset_failed(u);
5632e374
LP
2563}
2564
b2bb3dbe
LP
2565int manager_set_console(Manager *m, const char *console) {
2566 char *c;
2567
2568 assert(m);
2569
2570 if (!(c = strdup(console)))
2571 return -ENOMEM;
2572
2573 free(m->console);
2574 m->console = c;
2575
2576 log_debug("Using kernel console %s", c);
2577
2578 return 0;
2579}
2580
dfcd764e 2581static const char* const manager_running_as_table[_MANAGER_RUNNING_AS_MAX] = {
dfcd764e 2582 [MANAGER_SYSTEM] = "system",
036643a2 2583 [MANAGER_SESSION] = "session"
dfcd764e
LP
2584};
2585
2586DEFINE_STRING_TABLE_LOOKUP(manager_running_as, ManagerRunningAs);