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