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