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