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