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