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