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