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