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