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