chiark / gitweb /
62222e6c7a3654f28d6af2cdf57296902d353130
[elogind.git] / src / core / manager.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /***
4   This file is part of systemd.
5
6   Copyright 2010 Lennart Poettering
7
8   systemd is free software; you can redistribute it and/or modify it
9   under the terms of the GNU Lesser General Public License as published by
10   the Free Software Foundation; either version 2.1 of the License, or
11   (at your option) any later version.
12
13   systemd is distributed in the hope that it will be useful, but
14   WITHOUT ANY WARRANTY; without even the implied warranty of
15   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16   Lesser General Public License for more details.
17
18   You should have received a copy of the GNU Lesser General Public License
19   along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 ***/
21
22 #include <assert.h>
23 #include <errno.h>
24 #include <string.h>
25 #include <sys/epoll.h>
26 #include <signal.h>
27 #include <sys/signalfd.h>
28 #include <sys/wait.h>
29 #include <unistd.h>
30 #include <sys/poll.h>
31 #include <sys/reboot.h>
32 #include <sys/ioctl.h>
33 #include <linux/kd.h>
34 #include <termios.h>
35 #include <fcntl.h>
36 #include <sys/types.h>
37 #include <sys/stat.h>
38 #include <dirent.h>
39
40 #ifdef HAVE_AUDIT
41 #include <libaudit.h>
42 #endif
43
44 #include <systemd/sd-daemon.h>
45
46 #include "manager.h"
47 #include "transaction.h"
48 #include "hashmap.h"
49 #include "macro.h"
50 #include "strv.h"
51 #include "log.h"
52 #include "util.h"
53 #include "mkdir.h"
54 #include "ratelimit.h"
55 #include "cgroup.h"
56 #include "mount-setup.h"
57 #include "unit-name.h"
58 #include "dbus-unit.h"
59 #include "dbus-job.h"
60 #include "missing.h"
61 #include "path-lookup.h"
62 #include "special.h"
63 #include "bus-errors.h"
64 #include "exit-status.h"
65 #include "virt.h"
66 #include "watchdog.h"
67 #include "cgroup-util.h"
68
69 /* As soon as 16 units are in our GC queue, make sure to run a gc sweep */
70 #define GC_QUEUE_ENTRIES_MAX 16
71
72 /* As soon as 5s passed since a unit was added to our GC queue, make sure to run a gc sweep */
73 #define GC_QUEUE_USEC_MAX (10*USEC_PER_SEC)
74
75 /* Where clients shall send notification messages to */
76 #define NOTIFY_SOCKET_SYSTEM "/run/systemd/notify"
77 #define NOTIFY_SOCKET_USER "@/org/freedesktop/systemd1/notify"
78
79 static int manager_setup_notify(Manager *m) {
80         union {
81                 struct sockaddr sa;
82                 struct sockaddr_un un;
83         } sa;
84         struct epoll_event ev;
85         int one = 1, r;
86         mode_t u;
87
88         assert(m);
89
90         m->notify_watch.type = WATCH_NOTIFY;
91         if ((m->notify_watch.fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0)) < 0) {
92                 log_error("Failed to allocate notification socket: %m");
93                 return -errno;
94         }
95
96         zero(sa);
97         sa.sa.sa_family = AF_UNIX;
98
99         if (getpid() != 1)
100                 snprintf(sa.un.sun_path, sizeof(sa.un.sun_path), NOTIFY_SOCKET_USER "/%llu", random_ull());
101         else {
102                 unlink(NOTIFY_SOCKET_SYSTEM);
103                 strncpy(sa.un.sun_path, NOTIFY_SOCKET_SYSTEM, sizeof(sa.un.sun_path));
104         }
105
106         if (sa.un.sun_path[0] == '@')
107                 sa.un.sun_path[0] = 0;
108
109         u = umask(0111);
110         r = bind(m->notify_watch.fd, &sa.sa, offsetof(struct sockaddr_un, sun_path) + 1 + strlen(sa.un.sun_path+1));
111         umask(u);
112
113         if (r < 0) {
114                 log_error("bind() failed: %m");
115                 return -errno;
116         }
117
118         if (setsockopt(m->notify_watch.fd, SOL_SOCKET, SO_PASSCRED, &one, sizeof(one)) < 0) {
119                 log_error("SO_PASSCRED failed: %m");
120                 return -errno;
121         }
122
123         zero(ev);
124         ev.events = EPOLLIN;
125         ev.data.ptr = &m->notify_watch;
126
127         if (epoll_ctl(m->epoll_fd, EPOLL_CTL_ADD, m->notify_watch.fd, &ev) < 0)
128                 return -errno;
129
130         if (sa.un.sun_path[0] == 0)
131                 sa.un.sun_path[0] = '@';
132
133         if (!(m->notify_socket = strdup(sa.un.sun_path)))
134                 return -ENOMEM;
135
136         log_debug("Using notification socket %s", m->notify_socket);
137
138         return 0;
139 }
140
141 static int enable_special_signals(Manager *m) {
142         int fd;
143
144         assert(m);
145
146         /* Enable that we get SIGINT on control-alt-del. In containers
147          * this will fail with EPERM, so ignore that. */
148         if (reboot(RB_DISABLE_CAD) < 0 && errno != EPERM)
149                 log_warning("Failed to enable ctrl-alt-del handling: %m");
150
151         fd = open_terminal("/dev/tty0", O_RDWR|O_NOCTTY|O_CLOEXEC);
152         if (fd < 0) {
153                 /* Support systems without virtual console */
154                 if (fd != -ENOENT)
155                         log_warning("Failed to open /dev/tty0: %m");
156         } else {
157                 /* Enable that we get SIGWINCH on kbrequest */
158                 if (ioctl(fd, KDSIGACCEPT, SIGWINCH) < 0)
159                         log_warning("Failed to enable kbrequest handling: %s", strerror(errno));
160
161                 close_nointr_nofail(fd);
162         }
163
164         return 0;
165 }
166
167 static int manager_setup_signals(Manager *m) {
168         sigset_t mask;
169         struct epoll_event ev;
170         struct sigaction sa;
171
172         assert(m);
173
174         /* We are not interested in SIGSTOP and friends. */
175         zero(sa);
176         sa.sa_handler = SIG_DFL;
177         sa.sa_flags = SA_NOCLDSTOP|SA_RESTART;
178         assert_se(sigaction(SIGCHLD, &sa, NULL) == 0);
179
180         assert_se(sigemptyset(&mask) == 0);
181
182         sigset_add_many(&mask,
183                         SIGCHLD,     /* Child died */
184                         SIGTERM,     /* Reexecute daemon */
185                         SIGHUP,      /* Reload configuration */
186                         SIGUSR1,     /* systemd/upstart: reconnect to D-Bus */
187                         SIGUSR2,     /* systemd: dump status */
188                         SIGINT,      /* Kernel sends us this on control-alt-del */
189                         SIGWINCH,    /* Kernel sends us this on kbrequest (alt-arrowup) */
190                         SIGPWR,      /* Some kernel drivers and upsd send us this on power failure */
191                         SIGRTMIN+0,  /* systemd: start default.target */
192                         SIGRTMIN+1,  /* systemd: isolate rescue.target */
193                         SIGRTMIN+2,  /* systemd: isolate emergency.target */
194                         SIGRTMIN+3,  /* systemd: start halt.target */
195                         SIGRTMIN+4,  /* systemd: start poweroff.target */
196                         SIGRTMIN+5,  /* systemd: start reboot.target */
197                         SIGRTMIN+6,  /* systemd: start kexec.target */
198                         SIGRTMIN+13, /* systemd: Immediate halt */
199                         SIGRTMIN+14, /* systemd: Immediate poweroff */
200                         SIGRTMIN+15, /* systemd: Immediate reboot */
201                         SIGRTMIN+16, /* systemd: Immediate kexec */
202                         SIGRTMIN+20, /* systemd: enable status messages */
203                         SIGRTMIN+21, /* systemd: disable status messages */
204                         SIGRTMIN+22, /* systemd: set log level to LOG_DEBUG */
205                         SIGRTMIN+23, /* systemd: set log level to LOG_INFO */
206                         SIGRTMIN+26, /* systemd: set log target to journal-or-kmsg */
207                         SIGRTMIN+27, /* systemd: set log target to console */
208                         SIGRTMIN+28, /* systemd: set log target to kmsg */
209                         SIGRTMIN+29, /* systemd: set log target to syslog-or-kmsg */
210                         -1);
211         assert_se(sigprocmask(SIG_SETMASK, &mask, NULL) == 0);
212
213         m->signal_watch.type = WATCH_SIGNAL;
214         if ((m->signal_watch.fd = signalfd(-1, &mask, SFD_NONBLOCK|SFD_CLOEXEC)) < 0)
215                 return -errno;
216
217         zero(ev);
218         ev.events = EPOLLIN;
219         ev.data.ptr = &m->signal_watch;
220
221         if (epoll_ctl(m->epoll_fd, EPOLL_CTL_ADD, m->signal_watch.fd, &ev) < 0)
222                 return -errno;
223
224         if (m->running_as == MANAGER_SYSTEM)
225                 return enable_special_signals(m);
226
227         return 0;
228 }
229
230 static void manager_strip_environment(Manager *m) {
231         assert(m);
232
233         /* Remove variables from the inherited set that are part of
234          * the container interface:
235          * http://www.freedesktop.org/wiki/Software/systemd/ContainerInterface */
236         strv_remove_prefix(m->environment, "container=");
237         strv_remove_prefix(m->environment, "container_");
238
239         /* Remove variables from the inherited set that are part of
240          * the initrd interface:
241          * http://www.freedesktop.org/wiki/Software/systemd/InitrdInterface */
242         strv_remove_prefix(m->environment, "RD_");
243 }
244
245 int manager_new(ManagerRunningAs running_as, Manager **_m) {
246         Manager *m;
247         int r = -ENOMEM;
248
249         assert(_m);
250         assert(running_as >= 0);
251         assert(running_as < _MANAGER_RUNNING_AS_MAX);
252
253         if (!(m = new0(Manager, 1)))
254                 return -ENOMEM;
255
256         dual_timestamp_get(&m->startup_timestamp);
257
258         m->running_as = running_as;
259         m->name_data_slot = m->conn_data_slot = m->subscribed_data_slot = -1;
260         m->exit_code = _MANAGER_EXIT_CODE_INVALID;
261         m->pin_cgroupfs_fd = -1;
262         m->idle_pipe[0] = m->idle_pipe[1] = -1;
263
264 #ifdef HAVE_AUDIT
265         m->audit_fd = -1;
266 #endif
267
268         m->signal_watch.fd = m->mount_watch.fd = m->udev_watch.fd = m->epoll_fd = m->dev_autofs_fd = m->swap_watch.fd = -1;
269         m->current_job_id = 1; /* start as id #1, so that we can leave #0 around as "null-like" value */
270
271         m->environment = strv_copy(environ);
272         if (!m->environment)
273                 goto fail;
274
275         manager_strip_environment(m);
276
277         if (running_as == MANAGER_SYSTEM) {
278                 m->default_controllers = strv_new("cpu", NULL);
279                 if (!m->default_controllers)
280                         goto fail;
281         }
282
283         if (!(m->units = hashmap_new(string_hash_func, string_compare_func)))
284                 goto fail;
285
286         if (!(m->jobs = hashmap_new(trivial_hash_func, trivial_compare_func)))
287                 goto fail;
288
289         if (!(m->watch_pids = hashmap_new(trivial_hash_func, trivial_compare_func)))
290                 goto fail;
291
292         if (!(m->cgroup_bondings = hashmap_new(string_hash_func, string_compare_func)))
293                 goto fail;
294
295         if (!(m->watch_bus = hashmap_new(string_hash_func, string_compare_func)))
296                 goto fail;
297
298         if ((m->epoll_fd = epoll_create1(EPOLL_CLOEXEC)) < 0)
299                 goto fail;
300
301         if ((r = lookup_paths_init(&m->lookup_paths, m->running_as, true)) < 0)
302                 goto fail;
303
304         if ((r = manager_setup_signals(m)) < 0)
305                 goto fail;
306
307         if ((r = manager_setup_cgroup(m)) < 0)
308                 goto fail;
309
310         if ((r = manager_setup_notify(m)) < 0)
311                 goto fail;
312
313         /* Try to connect to the busses, if possible. */
314         if ((r = bus_init(m, running_as != MANAGER_SYSTEM)) < 0)
315                 goto fail;
316
317 #ifdef HAVE_AUDIT
318         if ((m->audit_fd = audit_open()) < 0 &&
319             /* If the kernel lacks netlink or audit support,
320              * don't worry about it. */
321             errno != EAFNOSUPPORT && errno != EPROTONOSUPPORT)
322                 log_error("Failed to connect to audit log: %m");
323 #endif
324
325         m->taint_usr = dir_is_empty("/usr") > 0;
326
327         *_m = m;
328         return 0;
329
330 fail:
331         manager_free(m);
332         return r;
333 }
334
335 static unsigned manager_dispatch_cleanup_queue(Manager *m) {
336         Unit *u;
337         unsigned n = 0;
338
339         assert(m);
340
341         while ((u = m->cleanup_queue)) {
342                 assert(u->in_cleanup_queue);
343
344                 unit_free(u);
345                 n++;
346         }
347
348         return n;
349 }
350
351 enum {
352         GC_OFFSET_IN_PATH,  /* This one is on the path we were traveling */
353         GC_OFFSET_UNSURE,   /* No clue */
354         GC_OFFSET_GOOD,     /* We still need this unit */
355         GC_OFFSET_BAD,      /* We don't need this unit anymore */
356         _GC_OFFSET_MAX
357 };
358
359 static void unit_gc_sweep(Unit *u, unsigned gc_marker) {
360         Iterator i;
361         Unit *other;
362         bool is_bad;
363
364         assert(u);
365
366         if (u->gc_marker == gc_marker + GC_OFFSET_GOOD ||
367             u->gc_marker == gc_marker + GC_OFFSET_BAD ||
368             u->gc_marker == gc_marker + GC_OFFSET_IN_PATH)
369                 return;
370
371         if (u->in_cleanup_queue)
372                 goto bad;
373
374         if (unit_check_gc(u))
375                 goto good;
376
377         u->gc_marker = gc_marker + GC_OFFSET_IN_PATH;
378
379         is_bad = true;
380
381         SET_FOREACH(other, u->dependencies[UNIT_REFERENCED_BY], i) {
382                 unit_gc_sweep(other, gc_marker);
383
384                 if (other->gc_marker == gc_marker + GC_OFFSET_GOOD)
385                         goto good;
386
387                 if (other->gc_marker != gc_marker + GC_OFFSET_BAD)
388                         is_bad = false;
389         }
390
391         if (is_bad)
392                 goto bad;
393
394         /* We were unable to find anything out about this entry, so
395          * let's investigate it later */
396         u->gc_marker = gc_marker + GC_OFFSET_UNSURE;
397         unit_add_to_gc_queue(u);
398         return;
399
400 bad:
401         /* We definitely know that this one is not useful anymore, so
402          * let's mark it for deletion */
403         u->gc_marker = gc_marker + GC_OFFSET_BAD;
404         unit_add_to_cleanup_queue(u);
405         return;
406
407 good:
408         u->gc_marker = gc_marker + GC_OFFSET_GOOD;
409 }
410
411 static unsigned manager_dispatch_gc_queue(Manager *m) {
412         Unit *u;
413         unsigned n = 0;
414         unsigned gc_marker;
415
416         assert(m);
417
418         if ((m->n_in_gc_queue < GC_QUEUE_ENTRIES_MAX) &&
419             (m->gc_queue_timestamp <= 0 ||
420              (m->gc_queue_timestamp + GC_QUEUE_USEC_MAX) > now(CLOCK_MONOTONIC)))
421                 return 0;
422
423         log_debug("Running GC...");
424
425         m->gc_marker += _GC_OFFSET_MAX;
426         if (m->gc_marker + _GC_OFFSET_MAX <= _GC_OFFSET_MAX)
427                 m->gc_marker = 1;
428
429         gc_marker = m->gc_marker;
430
431         while ((u = m->gc_queue)) {
432                 assert(u->in_gc_queue);
433
434                 unit_gc_sweep(u, gc_marker);
435
436                 LIST_REMOVE(Unit, gc_queue, m->gc_queue, u);
437                 u->in_gc_queue = false;
438
439                 n++;
440
441                 if (u->gc_marker == gc_marker + GC_OFFSET_BAD ||
442                     u->gc_marker == gc_marker + GC_OFFSET_UNSURE) {
443                         log_debug("Collecting %s", u->id);
444                         u->gc_marker = gc_marker + GC_OFFSET_BAD;
445                         unit_add_to_cleanup_queue(u);
446                 }
447         }
448
449         m->n_in_gc_queue = 0;
450         m->gc_queue_timestamp = 0;
451
452         return n;
453 }
454
455 static void manager_clear_jobs_and_units(Manager *m) {
456         Unit *u;
457
458         assert(m);
459
460         while ((u = hashmap_first(m->units)))
461                 unit_free(u);
462
463         manager_dispatch_cleanup_queue(m);
464
465         assert(!m->load_queue);
466         assert(!m->run_queue);
467         assert(!m->dbus_unit_queue);
468         assert(!m->dbus_job_queue);
469         assert(!m->cleanup_queue);
470         assert(!m->gc_queue);
471
472         assert(hashmap_isempty(m->jobs));
473         assert(hashmap_isempty(m->units));
474 }
475
476 void manager_free(Manager *m) {
477         UnitType c;
478
479         assert(m);
480
481         manager_clear_jobs_and_units(m);
482
483         for (c = 0; c < _UNIT_TYPE_MAX; c++)
484                 if (unit_vtable[c]->shutdown)
485                         unit_vtable[c]->shutdown(m);
486
487         /* If we reexecute ourselves, we keep the root cgroup
488          * around */
489         manager_shutdown_cgroup(m, m->exit_code != MANAGER_REEXECUTE);
490
491         manager_undo_generators(m);
492
493         bus_done(m);
494
495         hashmap_free(m->units);
496         hashmap_free(m->jobs);
497         hashmap_free(m->watch_pids);
498         hashmap_free(m->watch_bus);
499
500         if (m->epoll_fd >= 0)
501                 close_nointr_nofail(m->epoll_fd);
502         if (m->signal_watch.fd >= 0)
503                 close_nointr_nofail(m->signal_watch.fd);
504         if (m->notify_watch.fd >= 0)
505                 close_nointr_nofail(m->notify_watch.fd);
506
507 #ifdef HAVE_AUDIT
508         if (m->audit_fd >= 0)
509                 audit_close(m->audit_fd);
510 #endif
511
512         free(m->notify_socket);
513
514         lookup_paths_free(&m->lookup_paths);
515         strv_free(m->environment);
516
517         strv_free(m->default_controllers);
518
519         hashmap_free(m->cgroup_bondings);
520         set_free_free(m->unit_path_cache);
521
522         close_pipe(m->idle_pipe);
523
524         free(m);
525 }
526
527 int manager_enumerate(Manager *m) {
528         int r = 0, q;
529         UnitType c;
530
531         assert(m);
532
533         /* Let's ask every type to load all units from disk/kernel
534          * that it might know */
535         for (c = 0; c < _UNIT_TYPE_MAX; c++)
536                 if (unit_vtable[c]->enumerate)
537                         if ((q = unit_vtable[c]->enumerate(m)) < 0)
538                                 r = q;
539
540         manager_dispatch_load_queue(m);
541         return r;
542 }
543
544 int manager_coldplug(Manager *m) {
545         int r = 0, q;
546         Iterator i;
547         Unit *u;
548         char *k;
549
550         assert(m);
551
552         /* Then, let's set up their initial state. */
553         HASHMAP_FOREACH_KEY(u, k, m->units, i) {
554
555                 /* ignore aliases */
556                 if (u->id != k)
557                         continue;
558
559                 if ((q = unit_coldplug(u)) < 0)
560                         r = q;
561         }
562
563         return r;
564 }
565
566 static void manager_build_unit_path_cache(Manager *m) {
567         char **i;
568         DIR *d = NULL;
569         int r;
570
571         assert(m);
572
573         set_free_free(m->unit_path_cache);
574
575         if (!(m->unit_path_cache = set_new(string_hash_func, string_compare_func))) {
576                 log_error("Failed to allocate unit path cache.");
577                 return;
578         }
579
580         /* This simply builds a list of files we know exist, so that
581          * we don't always have to go to disk */
582
583         STRV_FOREACH(i, m->lookup_paths.unit_path) {
584                 struct dirent *de;
585
586                 if (!(d = opendir(*i))) {
587                         log_error("Failed to open directory: %m");
588                         continue;
589                 }
590
591                 while ((de = readdir(d))) {
592                         char *p;
593
594                         if (ignore_file(de->d_name))
595                                 continue;
596
597                         p = join(streq(*i, "/") ? "" : *i, "/", de->d_name, NULL);
598                         if (!p) {
599                                 r = -ENOMEM;
600                                 goto fail;
601                         }
602
603                         if ((r = set_put(m->unit_path_cache, p)) < 0) {
604                                 free(p);
605                                 goto fail;
606                         }
607                 }
608
609                 closedir(d);
610                 d = NULL;
611         }
612
613         return;
614
615 fail:
616         log_error("Failed to build unit path cache: %s", strerror(-r));
617
618         set_free_free(m->unit_path_cache);
619         m->unit_path_cache = NULL;
620
621         if (d)
622                 closedir(d);
623 }
624
625 int manager_startup(Manager *m, FILE *serialization, FDSet *fds) {
626         int r, q;
627
628         assert(m);
629
630         manager_run_generators(m);
631
632         manager_build_unit_path_cache(m);
633
634         /* If we will deserialize make sure that during enumeration
635          * this is already known, so we increase the counter here
636          * already */
637         if (serialization)
638                 m->n_reloading ++;
639
640         /* First, enumerate what we can from all config files */
641         r = manager_enumerate(m);
642
643         /* Second, deserialize if there is something to deserialize */
644         if (serialization)
645                 if ((q = manager_deserialize(m, serialization, fds)) < 0)
646                         r = q;
647
648         /* Third, fire things up! */
649         if ((q = manager_coldplug(m)) < 0)
650                 r = q;
651
652         if (serialization) {
653                 assert(m->n_reloading > 0);
654                 m->n_reloading --;
655         }
656
657         return r;
658 }
659
660 int manager_add_job(Manager *m, JobType type, Unit *unit, JobMode mode, bool override, DBusError *e, Job **_ret) {
661         int r;
662         Transaction *tr;
663
664         assert(m);
665         assert(type < _JOB_TYPE_MAX);
666         assert(unit);
667         assert(mode < _JOB_MODE_MAX);
668
669         if (mode == JOB_ISOLATE && type != JOB_START) {
670                 dbus_set_error(e, BUS_ERROR_INVALID_JOB_MODE, "Isolate is only valid for start.");
671                 return -EINVAL;
672         }
673
674         if (mode == JOB_ISOLATE && !unit->allow_isolate) {
675                 dbus_set_error(e, BUS_ERROR_NO_ISOLATION, "Operation refused, unit may not be isolated.");
676                 return -EPERM;
677         }
678
679         log_debug("Trying to enqueue job %s/%s/%s", unit->id, job_type_to_string(type), job_mode_to_string(mode));
680
681         job_type_collapse(&type, unit);
682
683         tr = transaction_new();
684         if (!tr)
685                 return -ENOMEM;
686
687         r = transaction_add_job_and_dependencies(tr, type, unit, NULL, true, override, false,
688                                                  mode == JOB_IGNORE_DEPENDENCIES || mode == JOB_IGNORE_REQUIREMENTS,
689                                                  mode == JOB_IGNORE_DEPENDENCIES, e);
690         if (r < 0)
691                 goto tr_abort;
692
693         if (mode == JOB_ISOLATE) {
694                 r = transaction_add_isolate_jobs(tr, m);
695                 if (r < 0)
696                         goto tr_abort;
697         }
698
699         r = transaction_activate(tr, m, mode, e);
700         if (r < 0)
701                 goto tr_abort;
702
703         log_debug("Enqueued job %s/%s as %u", unit->id, job_type_to_string(type), (unsigned) tr->anchor_job->id);
704
705         if (_ret)
706                 *_ret = tr->anchor_job;
707
708         transaction_free(tr);
709         return 0;
710
711 tr_abort:
712         transaction_abort(tr);
713         transaction_free(tr);
714         return r;
715 }
716
717 int manager_add_job_by_name(Manager *m, JobType type, const char *name, JobMode mode, bool override, DBusError *e, Job **_ret) {
718         Unit *unit;
719         int r;
720
721         assert(m);
722         assert(type < _JOB_TYPE_MAX);
723         assert(name);
724         assert(mode < _JOB_MODE_MAX);
725
726         if ((r = manager_load_unit(m, name, NULL, NULL, &unit)) < 0)
727                 return r;
728
729         return manager_add_job(m, type, unit, mode, override, e, _ret);
730 }
731
732 Job *manager_get_job(Manager *m, uint32_t id) {
733         assert(m);
734
735         return hashmap_get(m->jobs, UINT32_TO_PTR(id));
736 }
737
738 Unit *manager_get_unit(Manager *m, const char *name) {
739         assert(m);
740         assert(name);
741
742         return hashmap_get(m->units, name);
743 }
744
745 unsigned manager_dispatch_load_queue(Manager *m) {
746         Unit *u;
747         unsigned n = 0;
748
749         assert(m);
750
751         /* Make sure we are not run recursively */
752         if (m->dispatching_load_queue)
753                 return 0;
754
755         m->dispatching_load_queue = true;
756
757         /* Dispatches the load queue. Takes a unit from the queue and
758          * tries to load its data until the queue is empty */
759
760         while ((u = m->load_queue)) {
761                 assert(u->in_load_queue);
762
763                 unit_load(u);
764                 n++;
765         }
766
767         m->dispatching_load_queue = false;
768         return n;
769 }
770
771 int manager_load_unit_prepare(Manager *m, const char *name, const char *path, DBusError *e, Unit **_ret) {
772         Unit *ret;
773         UnitType t;
774         int r;
775
776         assert(m);
777         assert(name || path);
778
779         /* This will prepare the unit for loading, but not actually
780          * load anything from disk. */
781
782         if (path && !is_path(path)) {
783                 dbus_set_error(e, BUS_ERROR_INVALID_PATH, "Path %s is not absolute.", path);
784                 return -EINVAL;
785         }
786
787         if (!name)
788                 name = file_name_from_path(path);
789
790         t = unit_name_to_type(name);
791
792         if (t == _UNIT_TYPE_INVALID || !unit_name_is_valid_no_type(name, false)) {
793                 dbus_set_error(e, BUS_ERROR_INVALID_NAME, "Unit name %s is not valid.", name);
794                 return -EINVAL;
795         }
796
797         ret = manager_get_unit(m, name);
798         if (ret) {
799                 *_ret = ret;
800                 return 1;
801         }
802
803         ret = unit_new(m, unit_vtable[t]->object_size);
804         if (!ret)
805                 return -ENOMEM;
806
807         if (path) {
808                 ret->fragment_path = strdup(path);
809                 if (!ret->fragment_path) {
810                         unit_free(ret);
811                         return -ENOMEM;
812                 }
813         }
814
815         if ((r = unit_add_name(ret, name)) < 0) {
816                 unit_free(ret);
817                 return r;
818         }
819
820         unit_add_to_load_queue(ret);
821         unit_add_to_dbus_queue(ret);
822         unit_add_to_gc_queue(ret);
823
824         if (_ret)
825                 *_ret = ret;
826
827         return 0;
828 }
829
830 int manager_load_unit(Manager *m, const char *name, const char *path, DBusError *e, Unit **_ret) {
831         int r;
832
833         assert(m);
834
835         /* This will load the service information files, but not actually
836          * start any services or anything. */
837
838         if ((r = manager_load_unit_prepare(m, name, path, e, _ret)) != 0)
839                 return r;
840
841         manager_dispatch_load_queue(m);
842
843         if (_ret)
844                 *_ret = unit_follow_merge(*_ret);
845
846         return 0;
847 }
848
849 void manager_dump_jobs(Manager *s, FILE *f, const char *prefix) {
850         Iterator i;
851         Job *j;
852
853         assert(s);
854         assert(f);
855
856         HASHMAP_FOREACH(j, s->jobs, i)
857                 job_dump(j, f, prefix);
858 }
859
860 void manager_dump_units(Manager *s, FILE *f, const char *prefix) {
861         Iterator i;
862         Unit *u;
863         const char *t;
864
865         assert(s);
866         assert(f);
867
868         HASHMAP_FOREACH_KEY(u, t, s->units, i)
869                 if (u->id == t)
870                         unit_dump(u, f, prefix);
871 }
872
873 void manager_clear_jobs(Manager *m) {
874         Job *j;
875
876         assert(m);
877
878         while ((j = hashmap_first(m->jobs)))
879                 /* No need to recurse. We're cancelling all jobs. */
880                 job_finish_and_invalidate(j, JOB_CANCELED, false);
881 }
882
883 unsigned manager_dispatch_run_queue(Manager *m) {
884         Job *j;
885         unsigned n = 0;
886
887         if (m->dispatching_run_queue)
888                 return 0;
889
890         m->dispatching_run_queue = true;
891
892         while ((j = m->run_queue)) {
893                 assert(j->installed);
894                 assert(j->in_run_queue);
895
896                 job_run_and_invalidate(j);
897                 n++;
898         }
899
900         m->dispatching_run_queue = false;
901         return n;
902 }
903
904 unsigned manager_dispatch_dbus_queue(Manager *m) {
905         Job *j;
906         Unit *u;
907         unsigned n = 0;
908
909         assert(m);
910
911         if (m->dispatching_dbus_queue)
912                 return 0;
913
914         m->dispatching_dbus_queue = true;
915
916         while ((u = m->dbus_unit_queue)) {
917                 assert(u->in_dbus_queue);
918
919                 bus_unit_send_change_signal(u);
920                 n++;
921         }
922
923         while ((j = m->dbus_job_queue)) {
924                 assert(j->in_dbus_queue);
925
926                 bus_job_send_change_signal(j);
927                 n++;
928         }
929
930         m->dispatching_dbus_queue = false;
931         return n;
932 }
933
934 static int manager_process_notify_fd(Manager *m) {
935         ssize_t n;
936
937         assert(m);
938
939         for (;;) {
940                 char buf[4096];
941                 struct msghdr msghdr;
942                 struct iovec iovec;
943                 struct ucred *ucred;
944                 union {
945                         struct cmsghdr cmsghdr;
946                         uint8_t buf[CMSG_SPACE(sizeof(struct ucred))];
947                 } control;
948                 Unit *u;
949                 char **tags;
950
951                 zero(iovec);
952                 iovec.iov_base = buf;
953                 iovec.iov_len = sizeof(buf)-1;
954
955                 zero(control);
956                 zero(msghdr);
957                 msghdr.msg_iov = &iovec;
958                 msghdr.msg_iovlen = 1;
959                 msghdr.msg_control = &control;
960                 msghdr.msg_controllen = sizeof(control);
961
962                 if ((n = recvmsg(m->notify_watch.fd, &msghdr, MSG_DONTWAIT)) <= 0) {
963                         if (n >= 0)
964                                 return -EIO;
965
966                         if (errno == EAGAIN || errno == EINTR)
967                                 break;
968
969                         return -errno;
970                 }
971
972                 if (msghdr.msg_controllen < CMSG_LEN(sizeof(struct ucred)) ||
973                     control.cmsghdr.cmsg_level != SOL_SOCKET ||
974                     control.cmsghdr.cmsg_type != SCM_CREDENTIALS ||
975                     control.cmsghdr.cmsg_len != CMSG_LEN(sizeof(struct ucred))) {
976                         log_warning("Received notify message without credentials. Ignoring.");
977                         continue;
978                 }
979
980                 ucred = (struct ucred*) CMSG_DATA(&control.cmsghdr);
981
982                 if (!(u = hashmap_get(m->watch_pids, LONG_TO_PTR(ucred->pid))))
983                         if (!(u = cgroup_unit_by_pid(m, ucred->pid))) {
984                                 log_warning("Cannot find unit for notify message of PID %lu.", (unsigned long) ucred->pid);
985                                 continue;
986                         }
987
988                 assert((size_t) n < sizeof(buf));
989                 buf[n] = 0;
990                 if (!(tags = strv_split(buf, "\n\r")))
991                         return -ENOMEM;
992
993                 log_debug("Got notification message for unit %s", u->id);
994
995                 if (UNIT_VTABLE(u)->notify_message)
996                         UNIT_VTABLE(u)->notify_message(u, ucred->pid, tags);
997
998                 strv_free(tags);
999         }
1000
1001         return 0;
1002 }
1003
1004 static int manager_dispatch_sigchld(Manager *m) {
1005         assert(m);
1006
1007         for (;;) {
1008                 siginfo_t si;
1009                 Unit *u;
1010                 int r;
1011
1012                 zero(si);
1013
1014                 /* First we call waitd() for a PID and do not reap the
1015                  * zombie. That way we can still access /proc/$PID for
1016                  * it while it is a zombie. */
1017                 if (waitid(P_ALL, 0, &si, WEXITED|WNOHANG|WNOWAIT) < 0) {
1018
1019                         if (errno == ECHILD)
1020                                 break;
1021
1022                         if (errno == EINTR)
1023                                 continue;
1024
1025                         return -errno;
1026                 }
1027
1028                 if (si.si_pid <= 0)
1029                         break;
1030
1031                 if (si.si_code == CLD_EXITED || si.si_code == CLD_KILLED || si.si_code == CLD_DUMPED) {
1032                         char *name = NULL;
1033
1034                         get_process_comm(si.si_pid, &name);
1035                         log_debug("Got SIGCHLD for process %lu (%s)", (unsigned long) si.si_pid, strna(name));
1036                         free(name);
1037                 }
1038
1039                 /* Let's flush any message the dying child might still
1040                  * have queued for us. This ensures that the process
1041                  * still exists in /proc so that we can figure out
1042                  * which cgroup and hence unit it belongs to. */
1043                 if ((r = manager_process_notify_fd(m)) < 0)
1044                         return r;
1045
1046                 /* And now figure out the unit this belongs to */
1047                 if (!(u = hashmap_get(m->watch_pids, LONG_TO_PTR(si.si_pid))))
1048                         u = cgroup_unit_by_pid(m, si.si_pid);
1049
1050                 /* And now, we actually reap the zombie. */
1051                 if (waitid(P_PID, si.si_pid, &si, WEXITED) < 0) {
1052                         if (errno == EINTR)
1053                                 continue;
1054
1055                         return -errno;
1056                 }
1057
1058                 if (si.si_code != CLD_EXITED && si.si_code != CLD_KILLED && si.si_code != CLD_DUMPED)
1059                         continue;
1060
1061                 log_debug("Child %lu died (code=%s, status=%i/%s)",
1062                           (long unsigned) si.si_pid,
1063                           sigchld_code_to_string(si.si_code),
1064                           si.si_status,
1065                           strna(si.si_code == CLD_EXITED
1066                                 ? exit_status_to_string(si.si_status, EXIT_STATUS_FULL)
1067                                 : signal_to_string(si.si_status)));
1068
1069                 if (!u)
1070                         continue;
1071
1072                 log_debug("Child %lu belongs to %s", (long unsigned) si.si_pid, u->id);
1073
1074                 hashmap_remove(m->watch_pids, LONG_TO_PTR(si.si_pid));
1075                 UNIT_VTABLE(u)->sigchld_event(u, si.si_pid, si.si_code, si.si_status);
1076         }
1077
1078         return 0;
1079 }
1080
1081 static int manager_start_target(Manager *m, const char *name, JobMode mode) {
1082         int r;
1083         DBusError error;
1084
1085         dbus_error_init(&error);
1086
1087         log_debug("Activating special unit %s", name);
1088
1089         if ((r = manager_add_job_by_name(m, JOB_START, name, mode, true, &error, NULL)) < 0)
1090                 log_error("Failed to enqueue %s job: %s", name, bus_error(&error, r));
1091
1092         dbus_error_free(&error);
1093
1094         return r;
1095 }
1096
1097 static int manager_process_signal_fd(Manager *m) {
1098         ssize_t n;
1099         struct signalfd_siginfo sfsi;
1100         bool sigchld = false;
1101
1102         assert(m);
1103
1104         for (;;) {
1105                 if ((n = read(m->signal_watch.fd, &sfsi, sizeof(sfsi))) != sizeof(sfsi)) {
1106
1107                         if (n >= 0)
1108                                 return -EIO;
1109
1110                         if (errno == EINTR || errno == EAGAIN)
1111                                 break;
1112
1113                         return -errno;
1114                 }
1115
1116                 if (sfsi.ssi_pid > 0) {
1117                         char *p = NULL;
1118
1119                         get_process_comm(sfsi.ssi_pid, &p);
1120
1121                         log_debug("Received SIG%s from PID %lu (%s).",
1122                                   signal_to_string(sfsi.ssi_signo),
1123                                   (unsigned long) sfsi.ssi_pid, strna(p));
1124                         free(p);
1125                 } else
1126                         log_debug("Received SIG%s.", signal_to_string(sfsi.ssi_signo));
1127
1128                 switch (sfsi.ssi_signo) {
1129
1130                 case SIGCHLD:
1131                         sigchld = true;
1132                         break;
1133
1134                 case SIGTERM:
1135                         if (m->running_as == MANAGER_SYSTEM) {
1136                                 /* This is for compatibility with the
1137                                  * original sysvinit */
1138                                 m->exit_code = MANAGER_REEXECUTE;
1139                                 break;
1140                         }
1141
1142                         /* Fall through */
1143
1144                 case SIGINT:
1145                         if (m->running_as == MANAGER_SYSTEM) {
1146                                 manager_start_target(m, SPECIAL_CTRL_ALT_DEL_TARGET, JOB_REPLACE);
1147                                 break;
1148                         }
1149
1150                         /* Run the exit target if there is one, if not, just exit. */
1151                         if (manager_start_target(m, SPECIAL_EXIT_TARGET, JOB_REPLACE) < 0) {
1152                                 m->exit_code = MANAGER_EXIT;
1153                                 return 0;
1154                         }
1155
1156                         break;
1157
1158                 case SIGWINCH:
1159                         if (m->running_as == MANAGER_SYSTEM)
1160                                 manager_start_target(m, SPECIAL_KBREQUEST_TARGET, JOB_REPLACE);
1161
1162                         /* This is a nop on non-init */
1163                         break;
1164
1165                 case SIGPWR:
1166                         if (m->running_as == MANAGER_SYSTEM)
1167                                 manager_start_target(m, SPECIAL_SIGPWR_TARGET, JOB_REPLACE);
1168
1169                         /* This is a nop on non-init */
1170                         break;
1171
1172                 case SIGUSR1: {
1173                         Unit *u;
1174
1175                         u = manager_get_unit(m, SPECIAL_DBUS_SERVICE);
1176
1177                         if (!u || UNIT_IS_ACTIVE_OR_RELOADING(unit_active_state(u))) {
1178                                 log_info("Trying to reconnect to bus...");
1179                                 bus_init(m, true);
1180                         }
1181
1182                         if (!u || !UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(u))) {
1183                                 log_info("Loading D-Bus service...");
1184                                 manager_start_target(m, SPECIAL_DBUS_SERVICE, JOB_REPLACE);
1185                         }
1186
1187                         break;
1188                 }
1189
1190                 case SIGUSR2: {
1191                         FILE *f;
1192                         char *dump = NULL;
1193                         size_t size;
1194
1195                         if (!(f = open_memstream(&dump, &size))) {
1196                                 log_warning("Failed to allocate memory stream.");
1197                                 break;
1198                         }
1199
1200                         manager_dump_units(m, f, "\t");
1201                         manager_dump_jobs(m, f, "\t");
1202
1203                         if (ferror(f)) {
1204                                 fclose(f);
1205                                 free(dump);
1206                                 log_warning("Failed to write status stream");
1207                                 break;
1208                         }
1209
1210                         fclose(f);
1211                         log_dump(LOG_INFO, dump);
1212                         free(dump);
1213
1214                         break;
1215                 }
1216
1217                 case SIGHUP:
1218                         m->exit_code = MANAGER_RELOAD;
1219                         break;
1220
1221                 default: {
1222
1223                         /* Starting SIGRTMIN+0 */
1224                         static const char * const target_table[] = {
1225                                 [0] = SPECIAL_DEFAULT_TARGET,
1226                                 [1] = SPECIAL_RESCUE_TARGET,
1227                                 [2] = SPECIAL_EMERGENCY_TARGET,
1228                                 [3] = SPECIAL_HALT_TARGET,
1229                                 [4] = SPECIAL_POWEROFF_TARGET,
1230                                 [5] = SPECIAL_REBOOT_TARGET,
1231                                 [6] = SPECIAL_KEXEC_TARGET
1232                         };
1233
1234                         /* Starting SIGRTMIN+13, so that target halt and system halt are 10 apart */
1235                         static const ManagerExitCode code_table[] = {
1236                                 [0] = MANAGER_HALT,
1237                                 [1] = MANAGER_POWEROFF,
1238                                 [2] = MANAGER_REBOOT,
1239                                 [3] = MANAGER_KEXEC
1240                         };
1241
1242                         if ((int) sfsi.ssi_signo >= SIGRTMIN+0 &&
1243                             (int) sfsi.ssi_signo < SIGRTMIN+(int) ELEMENTSOF(target_table)) {
1244                                 int idx = (int) sfsi.ssi_signo - SIGRTMIN;
1245                                 manager_start_target(m, target_table[idx],
1246                                                      (idx == 1 || idx == 2) ? JOB_ISOLATE : JOB_REPLACE);
1247                                 break;
1248                         }
1249
1250                         if ((int) sfsi.ssi_signo >= SIGRTMIN+13 &&
1251                             (int) sfsi.ssi_signo < SIGRTMIN+13+(int) ELEMENTSOF(code_table)) {
1252                                 m->exit_code = code_table[sfsi.ssi_signo - SIGRTMIN - 13];
1253                                 break;
1254                         }
1255
1256                         switch (sfsi.ssi_signo - SIGRTMIN) {
1257
1258                         case 20:
1259                                 log_debug("Enabling showing of status.");
1260                                 manager_set_show_status(m, true);
1261                                 break;
1262
1263                         case 21:
1264                                 log_debug("Disabling showing of status.");
1265                                 manager_set_show_status(m, false);
1266                                 break;
1267
1268                         case 22:
1269                                 log_set_max_level(LOG_DEBUG);
1270                                 log_notice("Setting log level to debug.");
1271                                 break;
1272
1273                         case 23:
1274                                 log_set_max_level(LOG_INFO);
1275                                 log_notice("Setting log level to info.");
1276                                 break;
1277
1278                         case 26:
1279                                 log_set_target(LOG_TARGET_JOURNAL_OR_KMSG);
1280                                 log_notice("Setting log target to journal-or-kmsg.");
1281                                 break;
1282
1283                         case 27:
1284                                 log_set_target(LOG_TARGET_CONSOLE);
1285                                 log_notice("Setting log target to console.");
1286                                 break;
1287
1288                         case 28:
1289                                 log_set_target(LOG_TARGET_KMSG);
1290                                 log_notice("Setting log target to kmsg.");
1291                                 break;
1292
1293                         case 29:
1294                                 log_set_target(LOG_TARGET_SYSLOG_OR_KMSG);
1295                                 log_notice("Setting log target to syslog-or-kmsg.");
1296                                 break;
1297
1298                         default:
1299                                 log_warning("Got unhandled signal <%s>.", signal_to_string(sfsi.ssi_signo));
1300                         }
1301                 }
1302                 }
1303         }
1304
1305         if (sigchld)
1306                 return manager_dispatch_sigchld(m);
1307
1308         return 0;
1309 }
1310
1311 static int process_event(Manager *m, struct epoll_event *ev) {
1312         int r;
1313         Watch *w;
1314
1315         assert(m);
1316         assert(ev);
1317
1318         assert_se(w = ev->data.ptr);
1319
1320         if (w->type == WATCH_INVALID)
1321                 return 0;
1322
1323         switch (w->type) {
1324
1325         case WATCH_SIGNAL:
1326
1327                 /* An incoming signal? */
1328                 if (ev->events != EPOLLIN)
1329                         return -EINVAL;
1330
1331                 if ((r = manager_process_signal_fd(m)) < 0)
1332                         return r;
1333
1334                 break;
1335
1336         case WATCH_NOTIFY:
1337
1338                 /* An incoming daemon notification event? */
1339                 if (ev->events != EPOLLIN)
1340                         return -EINVAL;
1341
1342                 if ((r = manager_process_notify_fd(m)) < 0)
1343                         return r;
1344
1345                 break;
1346
1347         case WATCH_FD:
1348
1349                 /* Some fd event, to be dispatched to the units */
1350                 UNIT_VTABLE(w->data.unit)->fd_event(w->data.unit, w->fd, ev->events, w);
1351                 break;
1352
1353         case WATCH_UNIT_TIMER:
1354         case WATCH_JOB_TIMER: {
1355                 uint64_t v;
1356                 ssize_t k;
1357
1358                 /* Some timer event, to be dispatched to the units */
1359                 if ((k = read(w->fd, &v, sizeof(v))) != sizeof(v)) {
1360
1361                         if (k < 0 && (errno == EINTR || errno == EAGAIN))
1362                                 break;
1363
1364                         return k < 0 ? -errno : -EIO;
1365                 }
1366
1367                 if (w->type == WATCH_UNIT_TIMER)
1368                         UNIT_VTABLE(w->data.unit)->timer_event(w->data.unit, v, w);
1369                 else
1370                         job_timer_event(w->data.job, v, w);
1371                 break;
1372         }
1373
1374         case WATCH_MOUNT:
1375                 /* Some mount table change, intended for the mount subsystem */
1376                 mount_fd_event(m, ev->events);
1377                 break;
1378
1379         case WATCH_SWAP:
1380                 /* Some swap table change, intended for the swap subsystem */
1381                 swap_fd_event(m, ev->events);
1382                 break;
1383
1384         case WATCH_UDEV:
1385                 /* Some notification from udev, intended for the device subsystem */
1386                 device_fd_event(m, ev->events);
1387                 break;
1388
1389         case WATCH_DBUS_WATCH:
1390                 bus_watch_event(m, w, ev->events);
1391                 break;
1392
1393         case WATCH_DBUS_TIMEOUT:
1394                 bus_timeout_event(m, w, ev->events);
1395                 break;
1396
1397         default:
1398                 log_error("event type=%i", w->type);
1399                 assert_not_reached("Unknown epoll event type.");
1400         }
1401
1402         return 0;
1403 }
1404
1405 int manager_loop(Manager *m) {
1406         int r;
1407
1408         RATELIMIT_DEFINE(rl, 1*USEC_PER_SEC, 50000);
1409
1410         assert(m);
1411         m->exit_code = MANAGER_RUNNING;
1412
1413         /* Release the path cache */
1414         set_free_free(m->unit_path_cache);
1415         m->unit_path_cache = NULL;
1416
1417         manager_check_finished(m);
1418
1419         /* There might still be some zombies hanging around from
1420          * before we were exec()'ed. Leat's reap them */
1421         r = manager_dispatch_sigchld(m);
1422         if (r < 0)
1423                 return r;
1424
1425         while (m->exit_code == MANAGER_RUNNING) {
1426                 struct epoll_event event;
1427                 int n;
1428                 int wait_msec = -1;
1429
1430                 if (m->runtime_watchdog > 0 && m->running_as == MANAGER_SYSTEM)
1431                         watchdog_ping();
1432
1433                 if (!ratelimit_test(&rl)) {
1434                         /* Yay, something is going seriously wrong, pause a little */
1435                         log_warning("Looping too fast. Throttling execution a little.");
1436                         sleep(1);
1437                         continue;
1438                 }
1439
1440                 if (manager_dispatch_load_queue(m) > 0)
1441                         continue;
1442
1443                 if (manager_dispatch_run_queue(m) > 0)
1444                         continue;
1445
1446                 if (bus_dispatch(m) > 0)
1447                         continue;
1448
1449                 if (manager_dispatch_cleanup_queue(m) > 0)
1450                         continue;
1451
1452                 if (manager_dispatch_gc_queue(m) > 0)
1453                         continue;
1454
1455                 if (manager_dispatch_dbus_queue(m) > 0)
1456                         continue;
1457
1458                 if (swap_dispatch_reload(m) > 0)
1459                         continue;
1460
1461                 /* Sleep for half the watchdog time */
1462                 if (m->runtime_watchdog > 0 && m->running_as == MANAGER_SYSTEM) {
1463                         wait_msec = (int) (m->runtime_watchdog / 2 / USEC_PER_MSEC);
1464                         if (wait_msec <= 0)
1465                                 wait_msec = 1;
1466                 } else
1467                         wait_msec = -1;
1468
1469                 n = epoll_wait(m->epoll_fd, &event, 1, wait_msec);
1470                 if (n < 0) {
1471
1472                         if (errno == EINTR)
1473                                 continue;
1474
1475                         return -errno;
1476                 } else if (n == 0)
1477                         continue;
1478
1479                 assert(n == 1);
1480
1481                 r = process_event(m, &event);
1482                 if (r < 0)
1483                         return r;
1484         }
1485
1486         return m->exit_code;
1487 }
1488
1489 int manager_get_unit_from_dbus_path(Manager *m, const char *s, Unit **_u) {
1490         char *n;
1491         Unit *u;
1492
1493         assert(m);
1494         assert(s);
1495         assert(_u);
1496
1497         if (!startswith(s, "/org/freedesktop/systemd1/unit/"))
1498                 return -EINVAL;
1499
1500         if (!(n = bus_path_unescape(s+31)))
1501                 return -ENOMEM;
1502
1503         u = manager_get_unit(m, n);
1504         free(n);
1505
1506         if (!u)
1507                 return -ENOENT;
1508
1509         *_u = u;
1510
1511         return 0;
1512 }
1513
1514 int manager_get_job_from_dbus_path(Manager *m, const char *s, Job **_j) {
1515         Job *j;
1516         unsigned id;
1517         int r;
1518
1519         assert(m);
1520         assert(s);
1521         assert(_j);
1522
1523         if (!startswith(s, "/org/freedesktop/systemd1/job/"))
1524                 return -EINVAL;
1525
1526         if ((r = safe_atou(s + 30, &id)) < 0)
1527                 return r;
1528
1529         if (!(j = manager_get_job(m, id)))
1530                 return -ENOENT;
1531
1532         *_j = j;
1533
1534         return 0;
1535 }
1536
1537 void manager_send_unit_audit(Manager *m, Unit *u, int type, bool success) {
1538
1539 #ifdef HAVE_AUDIT
1540         char *p;
1541
1542         if (m->audit_fd < 0)
1543                 return;
1544
1545         /* Don't generate audit events if the service was already
1546          * started and we're just deserializing */
1547         if (m->n_reloading > 0)
1548                 return;
1549
1550         if (m->running_as != MANAGER_SYSTEM)
1551                 return;
1552
1553         if (u->type != UNIT_SERVICE)
1554                 return;
1555
1556         if (!(p = unit_name_to_prefix_and_instance(u->id))) {
1557                 log_error("Failed to allocate unit name for audit message: %s", strerror(ENOMEM));
1558                 return;
1559         }
1560
1561         if (audit_log_user_comm_message(m->audit_fd, type, "", p, NULL, NULL, NULL, success) < 0) {
1562                 if (errno == EPERM) {
1563                         /* We aren't allowed to send audit messages?
1564                          * Then let's not retry again. */
1565                         audit_close(m->audit_fd);
1566                         m->audit_fd = -1;
1567                 } else
1568                         log_warning("Failed to send audit message: %m");
1569         }
1570
1571         free(p);
1572 #endif
1573
1574 }
1575
1576 void manager_send_unit_plymouth(Manager *m, Unit *u) {
1577         int fd = -1;
1578         union sockaddr_union sa;
1579         int n = 0;
1580         char *message = NULL;
1581
1582         /* Don't generate plymouth events if the service was already
1583          * started and we're just deserializing */
1584         if (m->n_reloading > 0)
1585                 return;
1586
1587         if (m->running_as != MANAGER_SYSTEM)
1588                 return;
1589
1590         if (u->type != UNIT_SERVICE &&
1591             u->type != UNIT_MOUNT &&
1592             u->type != UNIT_SWAP)
1593                 return;
1594
1595         /* We set SOCK_NONBLOCK here so that we rather drop the
1596          * message then wait for plymouth */
1597         if ((fd = socket(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0)) < 0) {
1598                 log_error("socket() failed: %m");
1599                 return;
1600         }
1601
1602         zero(sa);
1603         sa.sa.sa_family = AF_UNIX;
1604         strncpy(sa.un.sun_path+1, "/org/freedesktop/plymouthd", sizeof(sa.un.sun_path)-1);
1605         if (connect(fd, &sa.sa, offsetof(struct sockaddr_un, sun_path) + 1 + strlen(sa.un.sun_path+1)) < 0) {
1606
1607                 if (errno != EPIPE &&
1608                     errno != EAGAIN &&
1609                     errno != ENOENT &&
1610                     errno != ECONNREFUSED &&
1611                     errno != ECONNRESET &&
1612                     errno != ECONNABORTED)
1613                         log_error("connect() failed: %m");
1614
1615                 goto finish;
1616         }
1617
1618         if (asprintf(&message, "U\002%c%s%n", (int) (strlen(u->id) + 1), u->id, &n) < 0) {
1619                 log_error("Out of memory");
1620                 goto finish;
1621         }
1622
1623         errno = 0;
1624         if (write(fd, message, n + 1) != n + 1) {
1625
1626                 if (errno != EPIPE &&
1627                     errno != EAGAIN &&
1628                     errno != ENOENT &&
1629                     errno != ECONNREFUSED &&
1630                     errno != ECONNRESET &&
1631                     errno != ECONNABORTED)
1632                         log_error("Failed to write Plymouth message: %m");
1633
1634                 goto finish;
1635         }
1636
1637 finish:
1638         if (fd >= 0)
1639                 close_nointr_nofail(fd);
1640
1641         free(message);
1642 }
1643
1644 void manager_dispatch_bus_name_owner_changed(
1645                 Manager *m,
1646                 const char *name,
1647                 const char* old_owner,
1648                 const char *new_owner) {
1649
1650         Unit *u;
1651
1652         assert(m);
1653         assert(name);
1654
1655         if (!(u = hashmap_get(m->watch_bus, name)))
1656                 return;
1657
1658         UNIT_VTABLE(u)->bus_name_owner_change(u, name, old_owner, new_owner);
1659 }
1660
1661 void manager_dispatch_bus_query_pid_done(
1662                 Manager *m,
1663                 const char *name,
1664                 pid_t pid) {
1665
1666         Unit *u;
1667
1668         assert(m);
1669         assert(name);
1670         assert(pid >= 1);
1671
1672         if (!(u = hashmap_get(m->watch_bus, name)))
1673                 return;
1674
1675         UNIT_VTABLE(u)->bus_query_pid_done(u, name, pid);
1676 }
1677
1678 int manager_open_serialization(Manager *m, FILE **_f) {
1679         char *path = NULL;
1680         mode_t saved_umask;
1681         int fd;
1682         FILE *f;
1683
1684         assert(_f);
1685
1686         if (m->running_as == MANAGER_SYSTEM)
1687                 asprintf(&path, "/run/systemd/dump-%lu-XXXXXX", (unsigned long) getpid());
1688         else
1689                 asprintf(&path, "/tmp/systemd-dump-%lu-XXXXXX", (unsigned long) getpid());
1690
1691         if (!path)
1692                 return -ENOMEM;
1693
1694         saved_umask = umask(0077);
1695         fd = mkostemp(path, O_RDWR|O_CLOEXEC);
1696         umask(saved_umask);
1697
1698         if (fd < 0) {
1699                 free(path);
1700                 return -errno;
1701         }
1702
1703         unlink(path);
1704
1705         log_debug("Serializing state to %s", path);
1706         free(path);
1707
1708         if (!(f = fdopen(fd, "w+")))
1709                 return -errno;
1710
1711         *_f = f;
1712
1713         return 0;
1714 }
1715
1716 int manager_serialize(Manager *m, FILE *f, FDSet *fds) {
1717         Iterator i;
1718         Unit *u;
1719         const char *t;
1720         int r;
1721
1722         assert(m);
1723         assert(f);
1724         assert(fds);
1725
1726         m->n_reloading ++;
1727
1728         fprintf(f, "current-job-id=%i\n", m->current_job_id);
1729         fprintf(f, "taint-usr=%s\n", yes_no(m->taint_usr));
1730
1731         dual_timestamp_serialize(f, "initrd-timestamp", &m->initrd_timestamp);
1732         dual_timestamp_serialize(f, "startup-timestamp", &m->startup_timestamp);
1733         dual_timestamp_serialize(f, "finish-timestamp", &m->finish_timestamp);
1734
1735         fputc('\n', f);
1736
1737         HASHMAP_FOREACH_KEY(u, t, m->units, i) {
1738                 if (u->id != t)
1739                         continue;
1740
1741                 if (!unit_can_serialize(u))
1742                         continue;
1743
1744                 /* Start marker */
1745                 fputs(u->id, f);
1746                 fputc('\n', f);
1747
1748                 if ((r = unit_serialize(u, f, fds)) < 0) {
1749                         m->n_reloading --;
1750                         return r;
1751                 }
1752         }
1753
1754         assert(m->n_reloading > 0);
1755         m->n_reloading --;
1756
1757         if (ferror(f))
1758                 return -EIO;
1759
1760         r = bus_fdset_add_all(m, fds);
1761         if (r < 0)
1762                 return r;
1763
1764         return 0;
1765 }
1766
1767 int manager_deserialize(Manager *m, FILE *f, FDSet *fds) {
1768         int r = 0;
1769
1770         assert(m);
1771         assert(f);
1772
1773         log_debug("Deserializing state...");
1774
1775         m->n_reloading ++;
1776
1777         for (;;) {
1778                 char line[LINE_MAX], *l;
1779
1780                 if (!fgets(line, sizeof(line), f)) {
1781                         if (feof(f))
1782                                 r = 0;
1783                         else
1784                                 r = -errno;
1785
1786                         goto finish;
1787                 }
1788
1789                 char_array_0(line);
1790                 l = strstrip(line);
1791
1792                 if (l[0] == 0)
1793                         break;
1794
1795                 if (startswith(l, "current-job-id=")) {
1796                         uint32_t id;
1797
1798                         if (safe_atou32(l+15, &id) < 0)
1799                                 log_debug("Failed to parse current job id value %s", l+15);
1800                         else
1801                                 m->current_job_id = MAX(m->current_job_id, id);
1802                 } else if (startswith(l, "taint-usr=")) {
1803                         int b;
1804
1805                         if ((b = parse_boolean(l+10)) < 0)
1806                                 log_debug("Failed to parse taint /usr flag %s", l+10);
1807                         else
1808                                 m->taint_usr = m->taint_usr || b;
1809                 } else if (startswith(l, "initrd-timestamp="))
1810                         dual_timestamp_deserialize(l+17, &m->initrd_timestamp);
1811                 else if (startswith(l, "startup-timestamp="))
1812                         dual_timestamp_deserialize(l+18, &m->startup_timestamp);
1813                 else if (startswith(l, "finish-timestamp="))
1814                         dual_timestamp_deserialize(l+17, &m->finish_timestamp);
1815                 else
1816                         log_debug("Unknown serialization item '%s'", l);
1817         }
1818
1819         for (;;) {
1820                 Unit *u;
1821                 char name[UNIT_NAME_MAX+2];
1822
1823                 /* Start marker */
1824                 if (!fgets(name, sizeof(name), f)) {
1825                         if (feof(f))
1826                                 r = 0;
1827                         else
1828                                 r = -errno;
1829
1830                         goto finish;
1831                 }
1832
1833                 char_array_0(name);
1834
1835                 if ((r = manager_load_unit(m, strstrip(name), NULL, NULL, &u)) < 0)
1836                         goto finish;
1837
1838                 if ((r = unit_deserialize(u, f, fds)) < 0)
1839                         goto finish;
1840         }
1841
1842 finish:
1843         if (ferror(f)) {
1844                 r = -EIO;
1845                 goto finish;
1846         }
1847
1848         assert(m->n_reloading > 0);
1849         m->n_reloading --;
1850
1851         return r;
1852 }
1853
1854 int manager_reload(Manager *m) {
1855         int r, q;
1856         FILE *f;
1857         FDSet *fds;
1858
1859         assert(m);
1860
1861         if ((r = manager_open_serialization(m, &f)) < 0)
1862                 return r;
1863
1864         m->n_reloading ++;
1865
1866         if (!(fds = fdset_new())) {
1867                 m->n_reloading --;
1868                 r = -ENOMEM;
1869                 goto finish;
1870         }
1871
1872         if ((r = manager_serialize(m, f, fds)) < 0) {
1873                 m->n_reloading --;
1874                 goto finish;
1875         }
1876
1877         if (fseeko(f, 0, SEEK_SET) < 0) {
1878                 m->n_reloading --;
1879                 r = -errno;
1880                 goto finish;
1881         }
1882
1883         /* From here on there is no way back. */
1884         manager_clear_jobs_and_units(m);
1885         manager_undo_generators(m);
1886
1887         /* Find new unit paths */
1888         lookup_paths_free(&m->lookup_paths);
1889         if ((q = lookup_paths_init(&m->lookup_paths, m->running_as, true)) < 0)
1890                 r = q;
1891
1892         manager_run_generators(m);
1893
1894         manager_build_unit_path_cache(m);
1895
1896         /* First, enumerate what we can from all config files */
1897         if ((q = manager_enumerate(m)) < 0)
1898                 r = q;
1899
1900         /* Second, deserialize our stored data */
1901         if ((q = manager_deserialize(m, f, fds)) < 0)
1902                 r = q;
1903
1904         fclose(f);
1905         f = NULL;
1906
1907         /* Third, fire things up! */
1908         if ((q = manager_coldplug(m)) < 0)
1909                 r = q;
1910
1911         assert(m->n_reloading > 0);
1912         m->n_reloading--;
1913
1914 finish:
1915         if (f)
1916                 fclose(f);
1917
1918         if (fds)
1919                 fdset_free(fds);
1920
1921         return r;
1922 }
1923
1924 bool manager_is_booting_or_shutting_down(Manager *m) {
1925         Unit *u;
1926
1927         assert(m);
1928
1929         /* Is the initial job still around? */
1930         if (manager_get_job(m, m->default_unit_job_id))
1931                 return true;
1932
1933         /* Is there a job for the shutdown target? */
1934         u = manager_get_unit(m, SPECIAL_SHUTDOWN_TARGET);
1935         if (u)
1936                 return !!u->job;
1937
1938         return false;
1939 }
1940
1941 void manager_reset_failed(Manager *m) {
1942         Unit *u;
1943         Iterator i;
1944
1945         assert(m);
1946
1947         HASHMAP_FOREACH(u, m->units, i)
1948                 unit_reset_failed(u);
1949 }
1950
1951 bool manager_unit_pending_inactive(Manager *m, const char *name) {
1952         Unit *u;
1953
1954         assert(m);
1955         assert(name);
1956
1957         /* Returns true if the unit is inactive or going down */
1958         if (!(u = manager_get_unit(m, name)))
1959                 return true;
1960
1961         return unit_pending_inactive(u);
1962 }
1963
1964 void manager_check_finished(Manager *m) {
1965         char userspace[FORMAT_TIMESPAN_MAX], initrd[FORMAT_TIMESPAN_MAX], kernel[FORMAT_TIMESPAN_MAX], sum[FORMAT_TIMESPAN_MAX];
1966         usec_t kernel_usec, initrd_usec, userspace_usec, total_usec;
1967
1968         assert(m);
1969
1970         if (hashmap_size(m->jobs) > 0)
1971                 return;
1972
1973         /* Notify Type=idle units that we are done now */
1974         close_pipe(m->idle_pipe);
1975
1976         if (dual_timestamp_is_set(&m->finish_timestamp))
1977                 return;
1978
1979         dual_timestamp_get(&m->finish_timestamp);
1980
1981         if (m->running_as == MANAGER_SYSTEM && detect_container(NULL) <= 0) {
1982
1983                 userspace_usec = m->finish_timestamp.monotonic - m->startup_timestamp.monotonic;
1984                 total_usec = m->finish_timestamp.monotonic;
1985
1986                 if (dual_timestamp_is_set(&m->initrd_timestamp)) {
1987
1988                         kernel_usec = m->initrd_timestamp.monotonic;
1989                         initrd_usec = m->startup_timestamp.monotonic - m->initrd_timestamp.monotonic;
1990
1991                         log_info("Startup finished in %s (kernel) + %s (initrd) + %s (userspace) = %s.",
1992                                  format_timespan(kernel, sizeof(kernel), kernel_usec),
1993                                  format_timespan(initrd, sizeof(initrd), initrd_usec),
1994                                  format_timespan(userspace, sizeof(userspace), userspace_usec),
1995                                  format_timespan(sum, sizeof(sum), total_usec));
1996                 } else {
1997                         kernel_usec = m->startup_timestamp.monotonic;
1998                         initrd_usec = 0;
1999
2000                         log_info("Startup finished in %s (kernel) + %s (userspace) = %s.",
2001                                  format_timespan(kernel, sizeof(kernel), kernel_usec),
2002                                  format_timespan(userspace, sizeof(userspace), userspace_usec),
2003                                  format_timespan(sum, sizeof(sum), total_usec));
2004                 }
2005         } else {
2006                 userspace_usec = initrd_usec = kernel_usec = 0;
2007                 total_usec = m->finish_timestamp.monotonic - m->startup_timestamp.monotonic;
2008
2009                 log_debug("Startup finished in %s.",
2010                           format_timespan(sum, sizeof(sum), total_usec));
2011         }
2012
2013         bus_broadcast_finished(m, kernel_usec, initrd_usec, userspace_usec, total_usec);
2014
2015         sd_notifyf(false,
2016                    "READY=1\nSTATUS=Startup finished in %s.",
2017                    format_timespan(sum, sizeof(sum), total_usec));
2018 }
2019
2020 void manager_run_generators(Manager *m) {
2021         DIR *d = NULL;
2022         const char *generator_path;
2023         const char *argv[3];
2024         mode_t u;
2025
2026         assert(m);
2027
2028         generator_path = m->running_as == MANAGER_SYSTEM ? SYSTEM_GENERATOR_PATH : USER_GENERATOR_PATH;
2029         if (!(d = opendir(generator_path))) {
2030
2031                 if (errno == ENOENT)
2032                         return;
2033
2034                 log_error("Failed to enumerate generator directory: %m");
2035                 return;
2036         }
2037
2038         if (!m->generator_unit_path) {
2039                 const char *p;
2040                 char user_path[] = "/tmp/systemd-generator-XXXXXX";
2041
2042                 if (m->running_as == MANAGER_SYSTEM && getpid() == 1) {
2043                         p = "/run/systemd/generator";
2044
2045                         if (mkdir_p(p, 0755) < 0) {
2046                                 log_error("Failed to create generator directory: %m");
2047                                 goto finish;
2048                         }
2049
2050                 } else {
2051                         if (!(p = mkdtemp(user_path))) {
2052                                 log_error("Failed to create generator directory: %m");
2053                                 goto finish;
2054                         }
2055                 }
2056
2057                 if (!(m->generator_unit_path = strdup(p))) {
2058                         log_error("Failed to allocate generator unit path.");
2059                         goto finish;
2060                 }
2061         }
2062
2063         argv[0] = NULL; /* Leave this empty, execute_directory() will fill something in */
2064         argv[1] = m->generator_unit_path;
2065         argv[2] = NULL;
2066
2067         u = umask(0022);
2068         execute_directory(generator_path, d, (char**) argv);
2069         umask(u);
2070
2071         if (rmdir(m->generator_unit_path) >= 0) {
2072                 /* Uh? we were able to remove this dir? I guess that
2073                  * means the directory was empty, hence let's shortcut
2074                  * this */
2075
2076                 free(m->generator_unit_path);
2077                 m->generator_unit_path = NULL;
2078                 goto finish;
2079         }
2080
2081         if (!strv_find(m->lookup_paths.unit_path, m->generator_unit_path)) {
2082                 char **l;
2083
2084                 if (!(l = strv_append(m->lookup_paths.unit_path, m->generator_unit_path))) {
2085                         log_error("Failed to add generator directory to unit search path: %m");
2086                         goto finish;
2087                 }
2088
2089                 strv_free(m->lookup_paths.unit_path);
2090                 m->lookup_paths.unit_path = l;
2091
2092                 log_debug("Added generator unit path %s to search path.", m->generator_unit_path);
2093         }
2094
2095 finish:
2096         if (d)
2097                 closedir(d);
2098 }
2099
2100 void manager_undo_generators(Manager *m) {
2101         assert(m);
2102
2103         if (!m->generator_unit_path)
2104                 return;
2105
2106         strv_remove(m->lookup_paths.unit_path, m->generator_unit_path);
2107         rm_rf(m->generator_unit_path, false, true, false);
2108
2109         free(m->generator_unit_path);
2110         m->generator_unit_path = NULL;
2111 }
2112
2113 int manager_set_default_controllers(Manager *m, char **controllers) {
2114         char **l;
2115
2116         assert(m);
2117
2118         l = strv_copy(controllers);
2119         if (!l)
2120                 return -ENOMEM;
2121
2122         strv_free(m->default_controllers);
2123         m->default_controllers = l;
2124
2125         cg_shorten_controllers(m->default_controllers);
2126
2127         return 0;
2128 }
2129
2130 void manager_recheck_journal(Manager *m) {
2131         Unit *u;
2132
2133         assert(m);
2134
2135         if (m->running_as != MANAGER_SYSTEM)
2136                 return;
2137
2138         u = manager_get_unit(m, SPECIAL_JOURNALD_SOCKET);
2139         if (u && SOCKET(u)->state != SOCKET_RUNNING) {
2140                 log_close_journal();
2141                 return;
2142         }
2143
2144         u = manager_get_unit(m, SPECIAL_JOURNALD_SERVICE);
2145         if (u && SERVICE(u)->state != SERVICE_RUNNING) {
2146                 log_close_journal();
2147                 return;
2148         }
2149
2150         /* Hmm, OK, so the socket is fully up and the service is up
2151          * too, then let's make use of the thing. */
2152         log_open();
2153 }
2154
2155 void manager_set_show_status(Manager *m, bool b) {
2156         assert(m);
2157
2158         if (m->running_as != MANAGER_SYSTEM)
2159                 return;
2160
2161         m->show_status = b;
2162
2163         if (b)
2164                 touch("/run/systemd/show-status");
2165         else
2166                 unlink("/run/systemd/show-status");
2167 }
2168
2169 bool manager_get_show_status(Manager *m) {
2170         assert(m);
2171
2172         if (m->running_as != MANAGER_SYSTEM)
2173                 return false;
2174
2175         if (m->show_status)
2176                 return true;
2177
2178         /* If Plymouth is running make sure we show the status, so
2179          * that there's something nice to see when people press Esc */
2180
2181         return plymouth_running();
2182 }
2183
2184 static const char* const manager_running_as_table[_MANAGER_RUNNING_AS_MAX] = {
2185         [MANAGER_SYSTEM] = "system",
2186         [MANAGER_USER] = "user"
2187 };
2188
2189 DEFINE_STRING_TABLE_LOOKUP(manager_running_as, ManagerRunningAs);