1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2010 Lennart Poettering
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.
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.
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/>.
29 #include <sys/reboot.h>
30 #include <sys/ioctl.h>
34 #include <sys/types.h>
37 #include <sys/timerfd.h>
43 #include "sd-daemon.h"
45 #include "sd-messages.h"
48 #include "transaction.h"
55 #include "ratelimit.h"
56 #include "locale-setup.h"
57 #include "mount-setup.h"
58 #include "unit-name.h"
60 #include "path-lookup.h"
62 #include "exit-status.h"
65 #include "cgroup-util.h"
66 #include "path-util.h"
68 #include "boot-timestamps.h"
70 #include "bus-errors.h"
71 #include "bus-error.h"
74 #include "dbus-unit.h"
76 #include "dbus-manager.h"
77 #include "bus-kernel.h"
79 /* As soon as 5s passed since a unit was added to our GC queue, make sure to run a gc sweep */
80 #define GC_QUEUE_USEC_MAX (10*USEC_PER_SEC)
82 /* Initial delay and the interval for printing status messages about running jobs */
83 #define JOBS_IN_PROGRESS_WAIT_USEC (5*USEC_PER_SEC)
84 #define JOBS_IN_PROGRESS_PERIOD_USEC (USEC_PER_SEC / 3)
85 #define JOBS_IN_PROGRESS_PERIOD_DIVISOR 3
87 /* Where clients shall send notification messages to */
88 #define NOTIFY_SOCKET "@/org/freedesktop/systemd1/notify"
90 #define TIME_T_MAX (time_t)((1UL << ((sizeof(time_t) << 3) - 1)) - 1)
92 static int manager_dispatch_notify_fd(sd_event_source *source, int fd, uint32_t revents, void *userdata);
93 static int manager_dispatch_signal_fd(sd_event_source *source, int fd, uint32_t revents, void *userdata);
94 static int manager_dispatch_time_change_fd(sd_event_source *source, int fd, uint32_t revents, void *userdata);
95 static int manager_dispatch_idle_pipe_fd(sd_event_source *source, int fd, uint32_t revents, void *userdata);
96 static int manager_dispatch_jobs_in_progress(sd_event_source *source, usec_t usec, void *userdata);
97 static int manager_dispatch_run_queue(sd_event_source *source, void *userdata);
99 static int manager_watch_jobs_in_progress(Manager *m) {
102 if (m->jobs_in_progress_event_source)
105 return sd_event_add_monotonic(m->event, now(CLOCK_MONOTONIC) + JOBS_IN_PROGRESS_WAIT_USEC, 0, manager_dispatch_jobs_in_progress, m, &m->jobs_in_progress_event_source);
108 #define CYLON_BUFFER_EXTRA (2*(sizeof(ANSI_RED_ON)-1) + sizeof(ANSI_HIGHLIGHT_RED_ON)-1 + 2*(sizeof(ANSI_HIGHLIGHT_OFF)-1))
110 static void draw_cylon(char buffer[], size_t buflen, unsigned width, unsigned pos) {
113 assert(buflen >= CYLON_BUFFER_EXTRA + width + 1);
114 assert(pos <= width+1); /* 0 or width+1 mean that the center light is behind the corner */
118 p = mempset(p, ' ', pos-2);
119 p = stpcpy(p, ANSI_RED_ON);
123 if (pos > 0 && pos <= width) {
124 p = stpcpy(p, ANSI_HIGHLIGHT_RED_ON);
128 p = stpcpy(p, ANSI_HIGHLIGHT_OFF);
131 p = stpcpy(p, ANSI_RED_ON);
134 p = mempset(p, ' ', width-1-pos);
135 strcpy(p, ANSI_HIGHLIGHT_OFF);
139 void manager_flip_auto_status(Manager *m, bool enable) {
141 if (m->show_status == SHOW_STATUS_AUTO)
142 manager_set_show_status(m, SHOW_STATUS_TEMPORARY);
144 if (m->show_status == SHOW_STATUS_TEMPORARY)
145 manager_set_show_status(m, SHOW_STATUS_AUTO);
149 static void manager_print_jobs_in_progress(Manager *m) {
150 _cleanup_free_ char *job_of_n = NULL;
153 unsigned counter = 0, print_nr;
154 char cylon[6 + CYLON_BUFFER_EXTRA + 1];
159 manager_flip_auto_status(m, true);
161 print_nr = (m->jobs_in_progress_iteration / JOBS_IN_PROGRESS_PERIOD_DIVISOR) % m->n_running_jobs;
163 HASHMAP_FOREACH(j, m->jobs, i)
164 if (j->state == JOB_RUNNING && counter++ == print_nr)
167 /* m->n_running_jobs must be consistent with the contents of m->jobs,
168 * so the above loop must have succeeded in finding j. */
169 assert(counter == print_nr + 1);
172 cylon_pos = m->jobs_in_progress_iteration % 14;
174 cylon_pos = 14 - cylon_pos;
175 draw_cylon(cylon, sizeof(cylon), 6, cylon_pos);
177 if (m->n_running_jobs > 1)
178 if (asprintf(&job_of_n, "(%u of %u) ", counter, m->n_running_jobs) < 0)
181 manager_status_printf(m, true, cylon, "%sA %s job is running for %s",
182 strempty(job_of_n), job_type_to_string(j->type), unit_description(j->unit));
184 m->jobs_in_progress_iteration++;
187 static int manager_watch_idle_pipe(Manager *m) {
192 if (m->idle_pipe_event_source)
195 if (m->idle_pipe[2] < 0)
198 r = sd_event_add_io(m->event, m->idle_pipe[2], EPOLLIN, manager_dispatch_idle_pipe_fd, m, &m->idle_pipe_event_source);
200 log_error("Failed to watch idle pipe: %s", strerror(-r));
207 static void manager_close_idle_pipe(Manager *m) {
210 close_pipe(m->idle_pipe);
211 close_pipe(m->idle_pipe + 2);
214 static int manager_setup_time_change(Manager *m) {
217 /* We only care for the cancellation event, hence we set the
218 * timeout to the latest possible value. */
219 struct itimerspec its = {
220 .it_value.tv_sec = TIME_T_MAX,
224 assert_cc(sizeof(time_t) == sizeof(TIME_T_MAX));
226 /* Uses TFD_TIMER_CANCEL_ON_SET to get notifications whenever
227 * CLOCK_REALTIME makes a jump relative to CLOCK_MONOTONIC */
229 m->time_change_fd = timerfd_create(CLOCK_REALTIME, TFD_NONBLOCK|TFD_CLOEXEC);
230 if (m->time_change_fd < 0) {
231 log_error("Failed to create timerfd: %m");
235 if (timerfd_settime(m->time_change_fd, TFD_TIMER_ABSTIME|TFD_TIMER_CANCEL_ON_SET, &its, NULL) < 0) {
236 log_debug("Failed to set up TFD_TIMER_CANCEL_ON_SET, ignoring: %m");
237 close_nointr_nofail(m->time_change_fd);
238 m->time_change_fd = -1;
242 r = sd_event_add_io(m->event, m->time_change_fd, EPOLLIN, manager_dispatch_time_change_fd, m, &m->time_change_event_source);
244 log_error("Failed to create time change event source: %s", strerror(-r));
248 log_debug("Set up TFD_TIMER_CANCEL_ON_SET timerfd.");
253 static int enable_special_signals(Manager *m) {
254 _cleanup_close_ int fd = -1;
258 /* Enable that we get SIGINT on control-alt-del. In containers
259 * this will fail with EPERM (older) or EINVAL (newer), so
261 if (reboot(RB_DISABLE_CAD) < 0 && errno != EPERM && errno != EINVAL)
262 log_warning("Failed to enable ctrl-alt-del handling: %m");
264 fd = open_terminal("/dev/tty0", O_RDWR|O_NOCTTY|O_CLOEXEC);
266 /* Support systems without virtual console */
268 log_warning("Failed to open /dev/tty0: %m");
270 /* Enable that we get SIGWINCH on kbrequest */
271 if (ioctl(fd, KDSIGACCEPT, SIGWINCH) < 0)
272 log_warning("Failed to enable kbrequest handling: %m");
278 static int manager_setup_signals(Manager *m) {
279 struct sigaction sa = {
280 .sa_handler = SIG_DFL,
281 .sa_flags = SA_NOCLDSTOP|SA_RESTART,
288 /* We are not interested in SIGSTOP and friends. */
289 assert_se(sigaction(SIGCHLD, &sa, NULL) == 0);
291 assert_se(sigemptyset(&mask) == 0);
293 sigset_add_many(&mask,
294 SIGCHLD, /* Child died */
295 SIGTERM, /* Reexecute daemon */
296 SIGHUP, /* Reload configuration */
297 SIGUSR1, /* systemd/upstart: reconnect to D-Bus */
298 SIGUSR2, /* systemd: dump status */
299 SIGINT, /* Kernel sends us this on control-alt-del */
300 SIGWINCH, /* Kernel sends us this on kbrequest (alt-arrowup) */
301 SIGPWR, /* Some kernel drivers and upsd send us this on power failure */
302 SIGRTMIN+0, /* systemd: start default.target */
303 SIGRTMIN+1, /* systemd: isolate rescue.target */
304 SIGRTMIN+2, /* systemd: isolate emergency.target */
305 SIGRTMIN+3, /* systemd: start halt.target */
306 SIGRTMIN+4, /* systemd: start poweroff.target */
307 SIGRTMIN+5, /* systemd: start reboot.target */
308 SIGRTMIN+6, /* systemd: start kexec.target */
309 SIGRTMIN+13, /* systemd: Immediate halt */
310 SIGRTMIN+14, /* systemd: Immediate poweroff */
311 SIGRTMIN+15, /* systemd: Immediate reboot */
312 SIGRTMIN+16, /* systemd: Immediate kexec */
313 SIGRTMIN+20, /* systemd: enable status messages */
314 SIGRTMIN+21, /* systemd: disable status messages */
315 SIGRTMIN+22, /* systemd: set log level to LOG_DEBUG */
316 SIGRTMIN+23, /* systemd: set log level to LOG_INFO */
317 SIGRTMIN+24, /* systemd: Immediate exit (--user only) */
318 SIGRTMIN+26, /* systemd: set log target to journal-or-kmsg */
319 SIGRTMIN+27, /* systemd: set log target to console */
320 SIGRTMIN+28, /* systemd: set log target to kmsg */
321 SIGRTMIN+29, /* systemd: set log target to syslog-or-kmsg */
323 assert_se(sigprocmask(SIG_SETMASK, &mask, NULL) == 0);
325 m->signal_fd = signalfd(-1, &mask, SFD_NONBLOCK|SFD_CLOEXEC);
326 if (m->signal_fd < 0)
329 r = sd_event_add_io(m->event, m->signal_fd, EPOLLIN, manager_dispatch_signal_fd, m, &m->signal_event_source);
333 /* Process signals a bit earlier than the rest of things, but
334 * later that notify_fd processing, so that the notify
335 * processing can still figure out to which process/service a
336 * message belongs, before we reap the process. */
337 r = sd_event_source_set_priority(m->signal_event_source, -5);
341 if (m->running_as == SYSTEMD_SYSTEM)
342 return enable_special_signals(m);
347 static void manager_clean_environment(Manager *m) {
350 /* Let's remove some environment variables that we
351 * need ourselves to communicate with our clients */
364 static int manager_default_environment(Manager *m) {
367 if (m->running_as == SYSTEMD_SYSTEM) {
368 /* The system manager always starts with a clean
369 * environment for its children. It does not import
370 * the kernel or the parents exported variables.
372 * The initial passed environ is untouched to keep
373 * /proc/self/environ valid; it is used for tagging
374 * the init process inside containers. */
375 m->environment = strv_new("PATH=" DEFAULT_PATH,
378 /* Import locale variables LC_*= from configuration */
379 locale_setup(&m->environment);
381 /* The user manager passes its own environment
382 * along to its children. */
383 m->environment = strv_copy(environ);
389 manager_clean_environment(m);
390 strv_sort(m->environment);
395 int manager_new(SystemdRunningAs running_as, Manager **_m) {
400 assert(running_as >= 0);
401 assert(running_as < _SYSTEMD_RUNNING_AS_MAX);
403 m = new0(Manager, 1);
408 if (detect_container(NULL) <= 0)
409 boot_timestamps(&m->userspace_timestamp, &m->firmware_timestamp, &m->loader_timestamp);
412 m->running_as = running_as;
413 m->exit_code = _MANAGER_EXIT_CODE_INVALID;
415 m->idle_pipe[0] = m->idle_pipe[1] = m->idle_pipe[2] = m->idle_pipe[3] = -1;
417 m->pin_cgroupfs_fd = m->notify_fd = m->signal_fd = m->time_change_fd = m->dev_autofs_fd = m->private_listen_fd = m->kdbus_fd = -1;
418 m->current_job_id = 1; /* start as id #1, so that we can leave #0 around as "null-like" value */
420 r = manager_default_environment(m);
424 r = hashmap_ensure_allocated(&m->units, string_hash_func, string_compare_func);
428 r = hashmap_ensure_allocated(&m->jobs, trivial_hash_func, trivial_compare_func);
432 r = hashmap_ensure_allocated(&m->cgroup_unit, string_hash_func, string_compare_func);
436 r = hashmap_ensure_allocated(&m->watch_pids, trivial_hash_func, trivial_compare_func);
440 r = hashmap_ensure_allocated(&m->watch_bus, string_hash_func, string_compare_func);
444 r = sd_event_default(&m->event);
448 r = sd_event_add_defer(m->event, manager_dispatch_run_queue, m, &m->run_queue_event_source);
452 r = sd_event_source_set_priority(m->run_queue_event_source, SD_EVENT_PRIORITY_IDLE);
456 r = sd_event_source_set_enabled(m->run_queue_event_source, SD_EVENT_OFF);
460 r = manager_setup_signals(m);
464 r = manager_setup_cgroup(m);
468 r = manager_setup_time_change(m);
472 m->udev = udev_new();
478 /* Note that we set up neither kdbus, nor the notify fd
479 * here. We do that after deserialization, since they might
480 * have gotten serialized across the reexec. */
482 m->taint_usr = dir_is_empty("/usr") > 0;
492 static int manager_setup_notify(Manager *m) {
495 struct sockaddr_un un;
497 .sa.sa_family = AF_UNIX,
501 if (m->notify_fd < 0) {
502 _cleanup_close_ int fd = -1;
504 /* First free all secondary fields */
505 free(m->notify_socket);
506 m->notify_socket = NULL;
507 m->notify_event_source = sd_event_source_unref(m->notify_event_source);
509 fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0);
511 log_error("Failed to allocate notification socket: %m");
515 if (getpid() != 1 || detect_container(NULL) > 0)
516 snprintf(sa.un.sun_path, sizeof(sa.un.sun_path), NOTIFY_SOCKET "/%" PRIx64, random_u64());
518 strncpy(sa.un.sun_path, NOTIFY_SOCKET, sizeof(sa.un.sun_path));
519 sa.un.sun_path[0] = 0;
521 r = bind(fd, &sa.sa, offsetof(struct sockaddr_un, sun_path) + 1 + strlen(sa.un.sun_path+1));
523 log_error("bind() failed: %m");
527 r = setsockopt(fd, SOL_SOCKET, SO_PASSCRED, &one, sizeof(one));
529 log_error("SO_PASSCRED failed: %m");
533 sa.un.sun_path[0] = '@';
534 m->notify_socket = strdup(sa.un.sun_path);
535 if (!m->notify_socket)
541 log_debug("Using notification socket %s", m->notify_socket);
544 if (!m->notify_event_source) {
545 r = sd_event_add_io(m->event, m->notify_fd, EPOLLIN, manager_dispatch_notify_fd, m, &m->notify_event_source);
547 log_error("Failed to allocate notify event source: %s", strerror(-r));
551 /* Process signals a bit earlier than SIGCHLD, so that we can
552 * still identify to which service an exit message belongs */
553 r = sd_event_source_set_priority(m->notify_event_source, -7);
555 log_error("Failed to set priority of notify event source: %s", strerror(-r));
563 static int manager_setup_kdbus(Manager *m) {
565 _cleanup_free_ char *p = NULL;
571 if (m->kdbus_fd >= 0)
574 m->kdbus_fd = bus_kernel_create_bus(m->running_as == SYSTEMD_SYSTEM ? "system" : "user", m->running_as == SYSTEMD_SYSTEM, &p);
575 if (m->kdbus_fd < 0) {
576 log_debug("Failed to set up kdbus: %s", strerror(-m->kdbus_fd));
580 log_debug("Successfully set up kdbus on %s", p);
582 /* Create the namespace directory here, so that the contents
583 * of that directory is not visible to non-root users. This is
584 * necessary to ensure that users cannot get access to busses
585 * of virtualized users when no UID namespacing is used. */
586 if (m->running_as == SYSTEMD_SYSTEM)
587 mkdir_p_label("/dev/kdbus/ns", 0700);
593 static int manager_connect_bus(Manager *m, bool reexecuting) {
594 bool try_bus_connect;
601 (m->running_as == SYSTEMD_USER && getenv("DBUS_SESSION_BUS_ADDRESS"));
603 /* Try to connect to the busses, if possible. */
604 return bus_init(m, try_bus_connect);
607 static unsigned manager_dispatch_cleanup_queue(Manager *m) {
613 while ((u = m->cleanup_queue)) {
614 assert(u->in_cleanup_queue);
624 GC_OFFSET_IN_PATH, /* This one is on the path we were traveling */
625 GC_OFFSET_UNSURE, /* No clue */
626 GC_OFFSET_GOOD, /* We still need this unit */
627 GC_OFFSET_BAD, /* We don't need this unit anymore */
631 static void unit_gc_sweep(Unit *u, unsigned gc_marker) {
638 if (u->gc_marker == gc_marker + GC_OFFSET_GOOD ||
639 u->gc_marker == gc_marker + GC_OFFSET_BAD ||
640 u->gc_marker == gc_marker + GC_OFFSET_IN_PATH)
643 if (u->in_cleanup_queue)
646 if (unit_check_gc(u))
649 u->gc_marker = gc_marker + GC_OFFSET_IN_PATH;
653 SET_FOREACH(other, u->dependencies[UNIT_REFERENCED_BY], i) {
654 unit_gc_sweep(other, gc_marker);
656 if (other->gc_marker == gc_marker + GC_OFFSET_GOOD)
659 if (other->gc_marker != gc_marker + GC_OFFSET_BAD)
666 /* We were unable to find anything out about this entry, so
667 * let's investigate it later */
668 u->gc_marker = gc_marker + GC_OFFSET_UNSURE;
669 unit_add_to_gc_queue(u);
673 /* We definitely know that this one is not useful anymore, so
674 * let's mark it for deletion */
675 u->gc_marker = gc_marker + GC_OFFSET_BAD;
676 unit_add_to_cleanup_queue(u);
680 u->gc_marker = gc_marker + GC_OFFSET_GOOD;
683 static unsigned manager_dispatch_gc_queue(Manager *m) {
690 /* log_debug("Running GC..."); */
692 m->gc_marker += _GC_OFFSET_MAX;
693 if (m->gc_marker + _GC_OFFSET_MAX <= _GC_OFFSET_MAX)
696 gc_marker = m->gc_marker;
698 while ((u = m->gc_queue)) {
699 assert(u->in_gc_queue);
701 unit_gc_sweep(u, gc_marker);
703 LIST_REMOVE(gc_queue, m->gc_queue, u);
704 u->in_gc_queue = false;
708 if (u->gc_marker == gc_marker + GC_OFFSET_BAD ||
709 u->gc_marker == gc_marker + GC_OFFSET_UNSURE) {
710 log_debug_unit(u->id, "Collecting %s", u->id);
711 u->gc_marker = gc_marker + GC_OFFSET_BAD;
712 unit_add_to_cleanup_queue(u);
716 m->n_in_gc_queue = 0;
721 static void manager_clear_jobs_and_units(Manager *m) {
726 while ((u = hashmap_first(m->units)))
729 manager_dispatch_cleanup_queue(m);
731 assert(!m->load_queue);
732 assert(!m->run_queue);
733 assert(!m->dbus_unit_queue);
734 assert(!m->dbus_job_queue);
735 assert(!m->cleanup_queue);
736 assert(!m->gc_queue);
738 assert(hashmap_isempty(m->jobs));
739 assert(hashmap_isempty(m->units));
742 m->n_running_jobs = 0;
745 void manager_free(Manager *m) {
751 manager_clear_jobs_and_units(m);
753 for (c = 0; c < _UNIT_TYPE_MAX; c++)
754 if (unit_vtable[c]->shutdown)
755 unit_vtable[c]->shutdown(m);
757 /* If we reexecute ourselves, we keep the root cgroup
759 manager_shutdown_cgroup(m, m->exit_code != MANAGER_REEXECUTE);
761 manager_undo_generators(m);
765 hashmap_free(m->units);
766 hashmap_free(m->jobs);
767 hashmap_free(m->watch_pids);
768 hashmap_free(m->watch_bus);
770 sd_event_source_unref(m->signal_event_source);
771 sd_event_source_unref(m->notify_event_source);
772 sd_event_source_unref(m->time_change_event_source);
773 sd_event_source_unref(m->jobs_in_progress_event_source);
774 sd_event_source_unref(m->idle_pipe_event_source);
775 sd_event_source_unref(m->run_queue_event_source);
777 if (m->signal_fd >= 0)
778 close_nointr_nofail(m->signal_fd);
779 if (m->notify_fd >= 0)
780 close_nointr_nofail(m->notify_fd);
781 if (m->time_change_fd >= 0)
782 close_nointr_nofail(m->time_change_fd);
783 if (m->kdbus_fd >= 0)
784 close_nointr_nofail(m->kdbus_fd);
786 manager_close_idle_pipe(m);
789 sd_event_unref(m->event);
791 free(m->notify_socket);
793 lookup_paths_free(&m->lookup_paths);
794 strv_free(m->environment);
796 hashmap_free(m->cgroup_unit);
797 set_free_free(m->unit_path_cache);
799 free(m->switch_root);
800 free(m->switch_root_init);
802 for (i = 0; i < RLIMIT_NLIMITS; i++)
805 assert(hashmap_isempty(m->units_requiring_mounts_for));
806 hashmap_free(m->units_requiring_mounts_for);
811 int manager_enumerate(Manager *m) {
817 /* Let's ask every type to load all units from disk/kernel
818 * that it might know */
819 for (c = 0; c < _UNIT_TYPE_MAX; c++)
820 if (unit_vtable[c]->enumerate) {
821 q = unit_vtable[c]->enumerate(m);
826 manager_dispatch_load_queue(m);
830 static int manager_coldplug(Manager *m) {
838 /* Then, let's set up their initial state. */
839 HASHMAP_FOREACH_KEY(u, k, m->units, i) {
845 if ((q = unit_coldplug(u)) < 0)
852 static void manager_build_unit_path_cache(Manager *m) {
854 _cleanup_free_ DIR *d = NULL;
859 set_free_free(m->unit_path_cache);
861 m->unit_path_cache = set_new(string_hash_func, string_compare_func);
862 if (!m->unit_path_cache) {
863 log_error("Failed to allocate unit path cache.");
867 /* This simply builds a list of files we know exist, so that
868 * we don't always have to go to disk */
870 STRV_FOREACH(i, m->lookup_paths.unit_path) {
876 log_error("Failed to open directory %s: %m", *i);
880 while ((de = readdir(d))) {
883 if (ignore_file(de->d_name))
886 p = strjoin(streq(*i, "/") ? "" : *i, "/", de->d_name, NULL);
892 r = set_consume(m->unit_path_cache, p);
904 log_error("Failed to build unit path cache: %s", strerror(-r));
906 set_free_free(m->unit_path_cache);
907 m->unit_path_cache = NULL;
911 static int manager_distribute_fds(Manager *m, FDSet *fds) {
918 HASHMAP_FOREACH(u, m->units, i) {
920 if (fdset_size(fds) <= 0)
923 if (UNIT_VTABLE(u)->distribute_fds) {
924 r = UNIT_VTABLE(u)->distribute_fds(u, fds);
933 int manager_startup(Manager *m, FILE *serialization, FDSet *fds) {
938 dual_timestamp_get(&m->generators_start_timestamp);
939 manager_run_generators(m);
940 dual_timestamp_get(&m->generators_finish_timestamp);
942 r = lookup_paths_init(
943 &m->lookup_paths, m->running_as, true,
944 m->generator_unit_path,
945 m->generator_unit_path_early,
946 m->generator_unit_path_late);
950 manager_build_unit_path_cache(m);
952 /* If we will deserialize make sure that during enumeration
953 * this is already known, so we increase the counter here
958 /* First, enumerate what we can from all config files */
959 dual_timestamp_get(&m->units_load_start_timestamp);
960 r = manager_enumerate(m);
961 dual_timestamp_get(&m->units_load_finish_timestamp);
963 /* Second, deserialize if there is something to deserialize */
965 q = manager_deserialize(m, serialization, fds);
970 /* Any fds left? Find some unit which wants them. This is
971 * useful to allow container managers to pass some file
972 * descriptors to us pre-initialized. This enables
973 * socket-based activation of entire containers. */
974 if (fdset_size(fds) > 0) {
975 q = manager_distribute_fds(m, fds);
980 /* We might have deserialized the notify fd, but if we didn't
981 * then let's create the bus now */
982 manager_setup_notify(m);
984 /* We might have deserialized the kdbus control fd, but if we
985 * didn't, then let's create the bus now. */
986 manager_setup_kdbus(m);
987 manager_connect_bus(m, !!serialization);
989 /* Third, fire things up! */
990 q = manager_coldplug(m);
995 assert(m->n_reloading > 0);
998 /* Let's wait for the UnitNew/JobNew messages being
999 * sent, before we notify that the reload is
1001 m->send_reloading_done = true;
1007 int manager_add_job(Manager *m, JobType type, Unit *unit, JobMode mode, bool override, sd_bus_error *e, Job **_ret) {
1012 assert(type < _JOB_TYPE_MAX);
1014 assert(mode < _JOB_MODE_MAX);
1016 if (mode == JOB_ISOLATE && type != JOB_START) {
1017 sd_bus_error_setf(e, SD_BUS_ERROR_INVALID_ARGS, "Isolate is only valid for start.");
1021 if (mode == JOB_ISOLATE && !unit->allow_isolate) {
1022 sd_bus_error_setf(e, BUS_ERROR_NO_ISOLATION, "Operation refused, unit may not be isolated.");
1026 log_debug_unit(unit->id,
1027 "Trying to enqueue job %s/%s/%s", unit->id,
1028 job_type_to_string(type), job_mode_to_string(mode));
1030 job_type_collapse(&type, unit);
1032 tr = transaction_new(mode == JOB_REPLACE_IRREVERSIBLY);
1036 r = transaction_add_job_and_dependencies(tr, type, unit, NULL, true, override, false,
1037 mode == JOB_IGNORE_DEPENDENCIES || mode == JOB_IGNORE_REQUIREMENTS,
1038 mode == JOB_IGNORE_DEPENDENCIES, e);
1042 if (mode == JOB_ISOLATE) {
1043 r = transaction_add_isolate_jobs(tr, m);
1048 r = transaction_activate(tr, m, mode, e);
1052 log_debug_unit(unit->id,
1053 "Enqueued job %s/%s as %u", unit->id,
1054 job_type_to_string(type), (unsigned) tr->anchor_job->id);
1057 *_ret = tr->anchor_job;
1059 transaction_free(tr);
1063 transaction_abort(tr);
1064 transaction_free(tr);
1068 int manager_add_job_by_name(Manager *m, JobType type, const char *name, JobMode mode, bool override, sd_bus_error *e, Job **_ret) {
1073 assert(type < _JOB_TYPE_MAX);
1075 assert(mode < _JOB_MODE_MAX);
1077 r = manager_load_unit(m, name, NULL, NULL, &unit);
1081 return manager_add_job(m, type, unit, mode, override, e, _ret);
1084 Job *manager_get_job(Manager *m, uint32_t id) {
1087 return hashmap_get(m->jobs, UINT32_TO_PTR(id));
1090 Unit *manager_get_unit(Manager *m, const char *name) {
1094 return hashmap_get(m->units, name);
1097 unsigned manager_dispatch_load_queue(Manager *m) {
1103 /* Make sure we are not run recursively */
1104 if (m->dispatching_load_queue)
1107 m->dispatching_load_queue = true;
1109 /* Dispatches the load queue. Takes a unit from the queue and
1110 * tries to load its data until the queue is empty */
1112 while ((u = m->load_queue)) {
1113 assert(u->in_load_queue);
1119 m->dispatching_load_queue = false;
1123 int manager_load_unit_prepare(
1135 assert(name || path);
1137 /* This will prepare the unit for loading, but not actually
1138 * load anything from disk. */
1140 if (path && !is_path(path))
1141 return sd_bus_error_setf(e, SD_BUS_ERROR_INVALID_ARGS, "Path %s is not absolute.", path);
1144 name = basename(path);
1146 t = unit_name_to_type(name);
1148 if (t == _UNIT_TYPE_INVALID || !unit_name_is_valid(name, TEMPLATE_INVALID))
1149 return sd_bus_error_setf(e, SD_BUS_ERROR_INVALID_ARGS, "Unit name %s is not valid.", name);
1151 ret = manager_get_unit(m, name);
1157 ret = unit_new(m, unit_vtable[t]->object_size);
1162 ret->fragment_path = strdup(path);
1163 if (!ret->fragment_path) {
1169 r = unit_add_name(ret, name);
1175 unit_add_to_load_queue(ret);
1176 unit_add_to_dbus_queue(ret);
1177 unit_add_to_gc_queue(ret);
1185 int manager_load_unit(
1196 /* This will load the service information files, but not actually
1197 * start any services or anything. */
1199 r = manager_load_unit_prepare(m, name, path, e, _ret);
1203 manager_dispatch_load_queue(m);
1206 *_ret = unit_follow_merge(*_ret);
1211 void manager_dump_jobs(Manager *s, FILE *f, const char *prefix) {
1218 HASHMAP_FOREACH(j, s->jobs, i)
1219 job_dump(j, f, prefix);
1222 void manager_dump_units(Manager *s, FILE *f, const char *prefix) {
1230 HASHMAP_FOREACH_KEY(u, t, s->units, i)
1232 unit_dump(u, f, prefix);
1235 void manager_clear_jobs(Manager *m) {
1240 while ((j = hashmap_first(m->jobs)))
1241 /* No need to recurse. We're cancelling all jobs. */
1242 job_finish_and_invalidate(j, JOB_CANCELED, false);
1245 static int manager_dispatch_run_queue(sd_event_source *source, void *userdata) {
1246 Manager *m = userdata;
1252 while ((j = m->run_queue)) {
1253 assert(j->installed);
1254 assert(j->in_run_queue);
1256 job_run_and_invalidate(j);
1259 if (m->n_running_jobs > 0)
1260 manager_watch_jobs_in_progress(m);
1262 if (m->n_on_console > 0)
1263 manager_watch_idle_pipe(m);
1268 static unsigned manager_dispatch_dbus_queue(Manager *m) {
1275 if (m->dispatching_dbus_queue)
1278 m->dispatching_dbus_queue = true;
1280 while ((u = m->dbus_unit_queue)) {
1281 assert(u->in_dbus_queue);
1283 bus_unit_send_change_signal(u);
1287 while ((j = m->dbus_job_queue)) {
1288 assert(j->in_dbus_queue);
1290 bus_job_send_change_signal(j);
1294 m->dispatching_dbus_queue = false;
1296 if (m->send_reloading_done) {
1297 m->send_reloading_done = false;
1299 bus_manager_send_reloading(m, false);
1302 if (m->queued_message)
1303 bus_send_queued_message(m);
1308 static int manager_dispatch_notify_fd(sd_event_source *source, int fd, uint32_t revents, void *userdata) {
1309 Manager *m = userdata;
1313 assert(m->notify_fd == fd);
1315 if (revents != EPOLLIN) {
1316 log_warning("Got unexpected poll event for notify fd.");
1322 struct iovec iovec = {
1324 .iov_len = sizeof(buf)-1,
1328 struct cmsghdr cmsghdr;
1329 uint8_t buf[CMSG_SPACE(sizeof(struct ucred))];
1332 struct msghdr msghdr = {
1335 .msg_control = &control,
1336 .msg_controllen = sizeof(control),
1338 struct ucred *ucred;
1340 _cleanup_strv_free_ char **tags = NULL;
1342 n = recvmsg(m->notify_fd, &msghdr, MSG_DONTWAIT);
1347 if (errno == EAGAIN || errno == EINTR)
1353 if (msghdr.msg_controllen < CMSG_LEN(sizeof(struct ucred)) ||
1354 control.cmsghdr.cmsg_level != SOL_SOCKET ||
1355 control.cmsghdr.cmsg_type != SCM_CREDENTIALS ||
1356 control.cmsghdr.cmsg_len != CMSG_LEN(sizeof(struct ucred))) {
1357 log_warning("Received notify message without credentials. Ignoring.");
1361 ucred = (struct ucred*) CMSG_DATA(&control.cmsghdr);
1363 u = hashmap_get(m->watch_pids, LONG_TO_PTR(ucred->pid));
1365 u = manager_get_unit_by_pid(m, ucred->pid);
1367 log_warning("Cannot find unit for notify message of PID "PID_FMT".", ucred->pid);
1372 assert((size_t) n < sizeof(buf));
1374 tags = strv_split(buf, "\n\r");
1378 log_debug_unit(u->id, "Got notification message for unit %s", u->id);
1380 if (UNIT_VTABLE(u)->notify_message)
1381 UNIT_VTABLE(u)->notify_message(u, ucred->pid, tags);
1387 static int manager_dispatch_sigchld(Manager *m) {
1394 /* First we call waitd() for a PID and do not reap the
1395 * zombie. That way we can still access /proc/$PID for
1396 * it while it is a zombie. */
1397 if (waitid(P_ALL, 0, &si, WEXITED|WNOHANG|WNOWAIT) < 0) {
1399 if (errno == ECHILD)
1411 if (si.si_code == CLD_EXITED || si.si_code == CLD_KILLED || si.si_code == CLD_DUMPED) {
1412 _cleanup_free_ char *name = NULL;
1414 get_process_comm(si.si_pid, &name);
1415 log_debug("Got SIGCHLD for process "PID_FMT" (%s)", si.si_pid, strna(name));
1418 /* And now figure out the unit this belongs to */
1419 u = hashmap_get(m->watch_pids, LONG_TO_PTR(si.si_pid));
1421 u = manager_get_unit_by_pid(m, si.si_pid);
1423 /* And now, we actually reap the zombie. */
1424 if (waitid(P_PID, si.si_pid, &si, WEXITED) < 0) {
1431 if (si.si_code != CLD_EXITED && si.si_code != CLD_KILLED && si.si_code != CLD_DUMPED)
1434 log_debug("Child %lu died (code=%s, status=%i/%s)",
1435 (long unsigned) si.si_pid,
1436 sigchld_code_to_string(si.si_code),
1438 strna(si.si_code == CLD_EXITED
1439 ? exit_status_to_string(si.si_status, EXIT_STATUS_FULL)
1440 : signal_to_string(si.si_status)));
1445 log_debug_unit(u->id,
1446 "Child %lu belongs to %s", (long unsigned) si.si_pid, u->id);
1448 hashmap_remove(m->watch_pids, LONG_TO_PTR(si.si_pid));
1449 UNIT_VTABLE(u)->sigchld_event(u, si.si_pid, si.si_code, si.si_status);
1455 static int manager_start_target(Manager *m, const char *name, JobMode mode) {
1456 _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
1459 log_debug_unit(name, "Activating special unit %s", name);
1461 r = manager_add_job_by_name(m, JOB_START, name, mode, true, &error, NULL);
1463 log_error_unit(name, "Failed to enqueue %s job: %s", name, bus_error_message(&error, r));
1468 static int manager_dispatch_signal_fd(sd_event_source *source, int fd, uint32_t revents, void *userdata) {
1469 Manager *m = userdata;
1471 struct signalfd_siginfo sfsi;
1472 bool sigchld = false;
1475 assert(m->signal_fd == fd);
1477 if (revents != EPOLLIN) {
1478 log_warning("Got unexpected events from signal file descriptor.");
1483 n = read(m->signal_fd, &sfsi, sizeof(sfsi));
1484 if (n != sizeof(sfsi)) {
1489 if (errno == EINTR || errno == EAGAIN)
1495 if (sfsi.ssi_pid > 0) {
1496 _cleanup_free_ char *p = NULL;
1498 get_process_comm(sfsi.ssi_pid, &p);
1500 log_full(sfsi.ssi_signo == SIGCHLD ||
1501 (sfsi.ssi_signo == SIGTERM && m->running_as == SYSTEMD_USER)
1502 ? LOG_DEBUG : LOG_INFO,
1503 "Received SIG%s from PID "PID_FMT" (%s).",
1504 signal_to_string(sfsi.ssi_signo),
1505 sfsi.ssi_pid, strna(p));
1507 log_full(sfsi.ssi_signo == SIGCHLD ||
1508 (sfsi.ssi_signo == SIGTERM && m->running_as == SYSTEMD_USER)
1509 ? LOG_DEBUG : LOG_INFO,
1511 signal_to_string(sfsi.ssi_signo));
1513 switch (sfsi.ssi_signo) {
1520 if (m->running_as == SYSTEMD_SYSTEM) {
1521 /* This is for compatibility with the
1522 * original sysvinit */
1523 m->exit_code = MANAGER_REEXECUTE;
1530 if (m->running_as == SYSTEMD_SYSTEM) {
1531 manager_start_target(m, SPECIAL_CTRL_ALT_DEL_TARGET, JOB_REPLACE_IRREVERSIBLY);
1535 /* Run the exit target if there is one, if not, just exit. */
1536 if (manager_start_target(m, SPECIAL_EXIT_TARGET, JOB_REPLACE) < 0) {
1537 m->exit_code = MANAGER_EXIT;
1544 if (m->running_as == SYSTEMD_SYSTEM)
1545 manager_start_target(m, SPECIAL_KBREQUEST_TARGET, JOB_REPLACE);
1547 /* This is a nop on non-init */
1551 if (m->running_as == SYSTEMD_SYSTEM)
1552 manager_start_target(m, SPECIAL_SIGPWR_TARGET, JOB_REPLACE);
1554 /* This is a nop on non-init */
1560 u = manager_get_unit(m, SPECIAL_DBUS_SERVICE);
1562 if (!u || UNIT_IS_ACTIVE_OR_RELOADING(unit_active_state(u))) {
1563 log_info("Trying to reconnect to bus...");
1567 if (!u || !UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(u))) {
1568 log_info("Loading D-Bus service...");
1569 manager_start_target(m, SPECIAL_DBUS_SERVICE, JOB_REPLACE);
1576 _cleanup_free_ char *dump = NULL;
1577 _cleanup_fclose_ FILE *f = NULL;
1580 f = open_memstream(&dump, &size);
1582 log_warning("Failed to allocate memory stream.");
1586 manager_dump_units(m, f, "\t");
1587 manager_dump_jobs(m, f, "\t");
1590 log_warning("Failed to write status stream");
1594 log_dump(LOG_INFO, dump);
1599 m->exit_code = MANAGER_RELOAD;
1604 /* Starting SIGRTMIN+0 */
1605 static const char * const target_table[] = {
1606 [0] = SPECIAL_DEFAULT_TARGET,
1607 [1] = SPECIAL_RESCUE_TARGET,
1608 [2] = SPECIAL_EMERGENCY_TARGET,
1609 [3] = SPECIAL_HALT_TARGET,
1610 [4] = SPECIAL_POWEROFF_TARGET,
1611 [5] = SPECIAL_REBOOT_TARGET,
1612 [6] = SPECIAL_KEXEC_TARGET
1615 /* Starting SIGRTMIN+13, so that target halt and system halt are 10 apart */
1616 static const ManagerExitCode code_table[] = {
1618 [1] = MANAGER_POWEROFF,
1619 [2] = MANAGER_REBOOT,
1623 if ((int) sfsi.ssi_signo >= SIGRTMIN+0 &&
1624 (int) sfsi.ssi_signo < SIGRTMIN+(int) ELEMENTSOF(target_table)) {
1625 int idx = (int) sfsi.ssi_signo - SIGRTMIN;
1626 manager_start_target(m, target_table[idx],
1627 (idx == 1 || idx == 2) ? JOB_ISOLATE : JOB_REPLACE);
1631 if ((int) sfsi.ssi_signo >= SIGRTMIN+13 &&
1632 (int) sfsi.ssi_signo < SIGRTMIN+13+(int) ELEMENTSOF(code_table)) {
1633 m->exit_code = code_table[sfsi.ssi_signo - SIGRTMIN - 13];
1637 switch (sfsi.ssi_signo - SIGRTMIN) {
1640 log_debug("Enabling showing of status.");
1641 manager_set_show_status(m, SHOW_STATUS_YES);
1645 log_debug("Disabling showing of status.");
1646 manager_set_show_status(m, SHOW_STATUS_NO);
1650 log_set_max_level(LOG_DEBUG);
1651 log_notice("Setting log level to debug.");
1655 log_set_max_level(LOG_INFO);
1656 log_notice("Setting log level to info.");
1660 if (m->running_as == SYSTEMD_USER) {
1661 m->exit_code = MANAGER_EXIT;
1665 /* This is a nop on init */
1669 log_set_target(LOG_TARGET_JOURNAL_OR_KMSG);
1670 log_notice("Setting log target to journal-or-kmsg.");
1674 log_set_target(LOG_TARGET_CONSOLE);
1675 log_notice("Setting log target to console.");
1679 log_set_target(LOG_TARGET_KMSG);
1680 log_notice("Setting log target to kmsg.");
1684 log_set_target(LOG_TARGET_SYSLOG_OR_KMSG);
1685 log_notice("Setting log target to syslog-or-kmsg.");
1689 log_warning("Got unhandled signal <%s>.", signal_to_string(sfsi.ssi_signo));
1696 manager_dispatch_sigchld(m);
1701 static int manager_dispatch_time_change_fd(sd_event_source *source, int fd, uint32_t revents, void *userdata) {
1702 Manager *m = userdata;
1707 assert(m->time_change_fd == fd);
1709 log_struct(LOG_INFO,
1710 MESSAGE_ID(SD_MESSAGE_TIME_CHANGE),
1711 "MESSAGE=Time has been changed",
1714 /* Restart the watch */
1715 m->time_change_event_source = sd_event_source_unref(m->time_change_event_source);
1717 close_nointr_nofail(m->time_change_fd);
1718 m->time_change_fd = -1;
1720 manager_setup_time_change(m);
1722 HASHMAP_FOREACH(u, m->units, i)
1723 if (UNIT_VTABLE(u)->time_change)
1724 UNIT_VTABLE(u)->time_change(u);
1729 static int manager_dispatch_idle_pipe_fd(sd_event_source *source, int fd, uint32_t revents, void *userdata) {
1730 Manager *m = userdata;
1733 assert(m->idle_pipe[2] == fd);
1735 m->no_console_output = m->n_on_console > 0;
1737 m->idle_pipe_event_source = sd_event_source_unref(m->idle_pipe_event_source);
1738 manager_close_idle_pipe(m);
1743 static int manager_dispatch_jobs_in_progress(sd_event_source *source, usec_t usec, void *userdata) {
1744 Manager *m = userdata;
1751 manager_print_jobs_in_progress(m);
1753 next = now(CLOCK_MONOTONIC) + JOBS_IN_PROGRESS_PERIOD_USEC;
1754 r = sd_event_source_set_time(source, next);
1758 return sd_event_source_set_enabled(source, SD_EVENT_ONESHOT);
1761 int manager_loop(Manager *m) {
1764 RATELIMIT_DEFINE(rl, 1*USEC_PER_SEC, 50000);
1767 m->exit_code = MANAGER_RUNNING;
1769 /* Release the path cache */
1770 set_free_free(m->unit_path_cache);
1771 m->unit_path_cache = NULL;
1773 manager_check_finished(m);
1775 /* There might still be some zombies hanging around from
1776 * before we were exec()'ed. Let's reap them. */
1777 r = manager_dispatch_sigchld(m);
1781 while (m->exit_code == MANAGER_RUNNING) {
1784 if (m->runtime_watchdog > 0 && m->running_as == SYSTEMD_SYSTEM)
1787 if (!ratelimit_test(&rl)) {
1788 /* Yay, something is going seriously wrong, pause a little */
1789 log_warning("Looping too fast. Throttling execution a little.");
1794 if (manager_dispatch_load_queue(m) > 0)
1797 if (manager_dispatch_gc_queue(m) > 0)
1800 if (manager_dispatch_cleanup_queue(m) > 0)
1803 if (manager_dispatch_cgroup_queue(m) > 0)
1806 if (manager_dispatch_dbus_queue(m) > 0)
1809 /* Sleep for half the watchdog time */
1810 if (m->runtime_watchdog > 0 && m->running_as == SYSTEMD_SYSTEM) {
1811 wait_usec = m->runtime_watchdog / 2;
1815 wait_usec = (usec_t) -1;
1817 r = sd_event_run(m->event, wait_usec);
1819 log_error("Failed to run event loop: %s", strerror(-r));
1824 return m->exit_code;
1827 int manager_load_unit_from_dbus_path(Manager *m, const char *s, sd_bus_error *e, Unit **_u) {
1828 _cleanup_free_ char *n = NULL;
1836 r = unit_name_from_dbus_path(s, &n);
1840 r = manager_load_unit(m, n, NULL, e, &u);
1849 int manager_get_job_from_dbus_path(Manager *m, const char *s, Job **_j) {
1859 p = startswith(s, "/org/freedesktop/systemd1/job/");
1863 r = safe_atou(p, &id);
1867 j = manager_get_job(m, id);
1876 void manager_send_unit_audit(Manager *m, Unit *u, int type, bool success) {
1882 audit_fd = get_audit_fd();
1886 /* Don't generate audit events if the service was already
1887 * started and we're just deserializing */
1888 if (m->n_reloading > 0)
1891 if (m->running_as != SYSTEMD_SYSTEM)
1894 if (u->type != UNIT_SERVICE)
1897 p = unit_name_to_prefix_and_instance(u->id);
1899 log_error_unit(u->id,
1900 "Failed to allocate unit name for audit message: %s", strerror(ENOMEM));
1904 if (audit_log_user_comm_message(audit_fd, type, "", p, NULL, NULL, NULL, success) < 0) {
1905 if (errno == EPERM) {
1906 /* We aren't allowed to send audit messages?
1907 * Then let's not retry again. */
1910 log_warning("Failed to send audit message: %m");
1918 void manager_send_unit_plymouth(Manager *m, Unit *u) {
1920 union sockaddr_union sa;
1922 char *message = NULL;
1924 /* Don't generate plymouth events if the service was already
1925 * started and we're just deserializing */
1926 if (m->n_reloading > 0)
1929 if (m->running_as != SYSTEMD_SYSTEM)
1932 if (detect_container(NULL) > 0)
1935 if (u->type != UNIT_SERVICE &&
1936 u->type != UNIT_MOUNT &&
1937 u->type != UNIT_SWAP)
1940 /* We set SOCK_NONBLOCK here so that we rather drop the
1941 * message then wait for plymouth */
1942 fd = socket(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0);
1944 log_error("socket() failed: %m");
1949 sa.sa.sa_family = AF_UNIX;
1950 strncpy(sa.un.sun_path+1, "/org/freedesktop/plymouthd", sizeof(sa.un.sun_path)-1);
1951 if (connect(fd, &sa.sa, offsetof(struct sockaddr_un, sun_path) + 1 + strlen(sa.un.sun_path+1)) < 0) {
1953 if (errno != EPIPE &&
1956 errno != ECONNREFUSED &&
1957 errno != ECONNRESET &&
1958 errno != ECONNABORTED)
1959 log_error("connect() failed: %m");
1964 if (asprintf(&message, "U\002%c%s%n", (int) (strlen(u->id) + 1), u->id, &n) < 0) {
1970 if (write(fd, message, n + 1) != n + 1) {
1972 if (errno != EPIPE &&
1975 errno != ECONNREFUSED &&
1976 errno != ECONNRESET &&
1977 errno != ECONNABORTED)
1978 log_error("Failed to write Plymouth message: %m");
1985 close_nointr_nofail(fd);
1990 void manager_dispatch_bus_name_owner_changed(
1993 const char* old_owner,
1994 const char *new_owner) {
2001 u = hashmap_get(m->watch_bus, name);
2005 UNIT_VTABLE(u)->bus_name_owner_change(u, name, old_owner, new_owner);
2008 int manager_open_serialization(Manager *m, FILE **_f) {
2015 path = m->running_as == SYSTEMD_SYSTEM ? "/run/systemd" : "/tmp";
2016 fd = open_tmpfile(path, O_RDWR|O_CLOEXEC);
2020 log_debug("Serializing state to %s", path);
2022 f = fdopen(fd, "w+");
2024 close_nointr_nofail(fd);
2033 int manager_serialize(Manager *m, FILE *f, FDSet *fds, bool switching_root) {
2046 fprintf(f, "current-job-id=%i\n", m->current_job_id);
2047 fprintf(f, "taint-usr=%s\n", yes_no(m->taint_usr));
2048 fprintf(f, "n-installed-jobs=%u\n", m->n_installed_jobs);
2049 fprintf(f, "n-failed-jobs=%u\n", m->n_failed_jobs);
2051 dual_timestamp_serialize(f, "firmware-timestamp", &m->firmware_timestamp);
2052 dual_timestamp_serialize(f, "loader-timestamp", &m->loader_timestamp);
2053 dual_timestamp_serialize(f, "kernel-timestamp", &m->kernel_timestamp);
2054 dual_timestamp_serialize(f, "initrd-timestamp", &m->initrd_timestamp);
2057 dual_timestamp_serialize(f, "userspace-timestamp", &m->userspace_timestamp);
2058 dual_timestamp_serialize(f, "finish-timestamp", &m->finish_timestamp);
2059 dual_timestamp_serialize(f, "security-start-timestamp", &m->security_start_timestamp);
2060 dual_timestamp_serialize(f, "security-finish-timestamp", &m->security_finish_timestamp);
2061 dual_timestamp_serialize(f, "generators-start-timestamp", &m->generators_start_timestamp);
2062 dual_timestamp_serialize(f, "generators-finish-timestamp", &m->generators_finish_timestamp);
2063 dual_timestamp_serialize(f, "units-load-start-timestamp", &m->units_load_start_timestamp);
2064 dual_timestamp_serialize(f, "units-load-finish-timestamp", &m->units_load_finish_timestamp);
2067 if (!switching_root) {
2068 STRV_FOREACH(e, m->environment) {
2069 _cleanup_free_ char *ce;
2075 fprintf(f, "env=%s\n", *e);
2079 if (m->notify_fd >= 0) {
2082 copy = fdset_put_dup(fds, m->notify_fd);
2086 fprintf(f, "notify-fd=%i\n", copy);
2087 fprintf(f, "notify-socket=%s\n", m->notify_socket);
2090 if (m->kdbus_fd >= 0) {
2093 copy = fdset_put_dup(fds, m->kdbus_fd);
2097 fprintf(f, "kdbus-fd=%i\n", copy);
2100 bus_serialize(m, f);
2104 HASHMAP_FOREACH_KEY(u, t, m->units, i) {
2108 if (!unit_can_serialize(u))
2115 r = unit_serialize(u, f, fds, !switching_root);
2122 assert(m->n_reloading > 0);
2128 r = bus_fdset_add_all(m, fds);
2135 int manager_deserialize(Manager *m, FILE *f, FDSet *fds) {
2141 log_debug("Deserializing state...");
2146 char line[LINE_MAX], *l;
2148 if (!fgets(line, sizeof(line), f)) {
2163 if (startswith(l, "current-job-id=")) {
2166 if (safe_atou32(l+15, &id) < 0)
2167 log_debug("Failed to parse current job id value %s", l+15);
2169 m->current_job_id = MAX(m->current_job_id, id);
2171 } else if (startswith(l, "n-installed-jobs=")) {
2174 if (safe_atou32(l+17, &n) < 0)
2175 log_debug("Failed to parse installed jobs counter %s", l+17);
2177 m->n_installed_jobs += n;
2179 } else if (startswith(l, "n-failed-jobs=")) {
2182 if (safe_atou32(l+14, &n) < 0)
2183 log_debug("Failed to parse failed jobs counter %s", l+14);
2185 m->n_failed_jobs += n;
2187 } else if (startswith(l, "taint-usr=")) {
2190 b = parse_boolean(l+10);
2192 log_debug("Failed to parse taint /usr flag %s", l+10);
2194 m->taint_usr = m->taint_usr || b;
2196 } else if (startswith(l, "firmware-timestamp="))
2197 dual_timestamp_deserialize(l+19, &m->firmware_timestamp);
2198 else if (startswith(l, "loader-timestamp="))
2199 dual_timestamp_deserialize(l+17, &m->loader_timestamp);
2200 else if (startswith(l, "kernel-timestamp="))
2201 dual_timestamp_deserialize(l+17, &m->kernel_timestamp);
2202 else if (startswith(l, "initrd-timestamp="))
2203 dual_timestamp_deserialize(l+17, &m->initrd_timestamp);
2204 else if (startswith(l, "userspace-timestamp="))
2205 dual_timestamp_deserialize(l+20, &m->userspace_timestamp);
2206 else if (startswith(l, "finish-timestamp="))
2207 dual_timestamp_deserialize(l+17, &m->finish_timestamp);
2208 else if (startswith(l, "security-start-timestamp="))
2209 dual_timestamp_deserialize(l+25, &m->security_start_timestamp);
2210 else if (startswith(l, "security-finish-timestamp="))
2211 dual_timestamp_deserialize(l+26, &m->security_finish_timestamp);
2212 else if (startswith(l, "generators-start-timestamp="))
2213 dual_timestamp_deserialize(l+27, &m->generators_start_timestamp);
2214 else if (startswith(l, "generators-finish-timestamp="))
2215 dual_timestamp_deserialize(l+28, &m->generators_finish_timestamp);
2216 else if (startswith(l, "units-load-start-timestamp="))
2217 dual_timestamp_deserialize(l+27, &m->units_load_start_timestamp);
2218 else if (startswith(l, "units-load-finish-timestamp="))
2219 dual_timestamp_deserialize(l+28, &m->units_load_finish_timestamp);
2220 else if (startswith(l, "env=")) {
2221 _cleanup_free_ char *uce = NULL;
2224 uce = cunescape(l+4);
2230 e = strv_env_set(m->environment, uce);
2236 strv_free(m->environment);
2239 } else if (startswith(l, "notify-fd=")) {
2242 if (safe_atoi(l + 10, &fd) < 0 || fd < 0 || !fdset_contains(fds, fd))
2243 log_debug("Failed to parse notify fd: %s", l + 10);
2245 if (m->notify_fd >= 0) {
2246 m->notify_event_source = sd_event_source_unref(m->notify_event_source);
2247 close_nointr_nofail(m->notify_fd);
2250 m->notify_fd = fdset_remove(fds, fd);
2253 } else if (startswith(l, "notify-socket=")) {
2262 free(m->notify_socket);
2263 m->notify_socket = n;
2265 } else if (startswith(l, "kdbus-fd=")) {
2268 if (safe_atoi(l + 9, &fd) < 0 || fd < 0 || !fdset_contains(fds, fd))
2269 log_debug("Failed to parse kdbus fd: %s", l + 9);
2271 if (m->kdbus_fd >= 0)
2272 close_nointr_nofail(m->kdbus_fd);
2274 m->kdbus_fd = fdset_remove(fds, fd);
2277 } else if (bus_deserialize_item(m, l) == 0)
2278 log_debug("Unknown serialization item '%s'", l);
2283 char name[UNIT_NAME_MAX+2];
2286 if (!fgets(name, sizeof(name), f)) {
2297 r = manager_load_unit(m, strstrip(name), NULL, NULL, &u);
2301 r = unit_deserialize(u, f, fds);
2310 assert(m->n_reloading > 0);
2316 int manager_reload(Manager *m) {
2318 _cleanup_fclose_ FILE *f = NULL;
2319 _cleanup_fdset_free_ FDSet *fds = NULL;
2323 r = manager_open_serialization(m, &f);
2328 bus_manager_send_reloading(m, true);
2336 r = manager_serialize(m, f, fds, false);
2342 if (fseeko(f, 0, SEEK_SET) < 0) {
2347 /* From here on there is no way back. */
2348 manager_clear_jobs_and_units(m);
2349 manager_undo_generators(m);
2350 lookup_paths_free(&m->lookup_paths);
2352 /* Find new unit paths */
2353 manager_run_generators(m);
2355 q = lookup_paths_init(
2356 &m->lookup_paths, m->running_as, true,
2357 m->generator_unit_path,
2358 m->generator_unit_path_early,
2359 m->generator_unit_path_late);
2363 manager_build_unit_path_cache(m);
2365 /* First, enumerate what we can from all config files */
2366 q = manager_enumerate(m);
2370 /* Second, deserialize our stored data */
2371 q = manager_deserialize(m, f, fds);
2378 /* Re-register notify_fd as event source */
2379 q = manager_setup_notify(m);
2383 /* Third, fire things up! */
2384 q = manager_coldplug(m);
2388 assert(m->n_reloading > 0);
2391 m->send_reloading_done = true;
2396 static bool manager_is_booting_or_shutting_down(Manager *m) {
2401 /* Is the initial job still around? */
2402 if (manager_get_job(m, m->default_unit_job_id))
2405 /* Is there a job for the shutdown target? */
2406 u = manager_get_unit(m, SPECIAL_SHUTDOWN_TARGET);
2413 bool manager_is_reloading_or_reexecuting(Manager *m) {
2416 return m->n_reloading != 0;
2419 void manager_reset_failed(Manager *m) {
2425 HASHMAP_FOREACH(u, m->units, i)
2426 unit_reset_failed(u);
2429 bool manager_unit_inactive_or_pending(Manager *m, const char *name) {
2435 /* Returns true if the unit is inactive or going down */
2436 u = manager_get_unit(m, name);
2440 return unit_inactive_or_pending(u);
2443 void manager_check_finished(Manager *m) {
2444 char userspace[FORMAT_TIMESPAN_MAX], initrd[FORMAT_TIMESPAN_MAX], kernel[FORMAT_TIMESPAN_MAX], sum[FORMAT_TIMESPAN_MAX];
2445 usec_t firmware_usec, loader_usec, kernel_usec, initrd_usec, userspace_usec, total_usec;
2449 if (m->n_running_jobs == 0)
2450 m->jobs_in_progress_event_source = sd_event_source_unref(m->jobs_in_progress_event_source);
2452 if (hashmap_size(m->jobs) > 0) {
2453 if (m->jobs_in_progress_event_source) {
2454 uint64_t next = now(CLOCK_MONOTONIC) + JOBS_IN_PROGRESS_PERIOD_USEC;
2455 sd_event_source_set_time(m->jobs_in_progress_event_source, next);
2460 manager_flip_auto_status(m, false);
2462 /* Notify Type=idle units that we are done now */
2463 m->idle_pipe_event_source = sd_event_source_unref(m->idle_pipe_event_source);
2464 manager_close_idle_pipe(m);
2466 /* Turn off confirm spawn now */
2467 m->confirm_spawn = false;
2469 if (dual_timestamp_is_set(&m->finish_timestamp))
2472 dual_timestamp_get(&m->finish_timestamp);
2474 if (m->running_as == SYSTEMD_SYSTEM && detect_container(NULL) <= 0) {
2476 /* Note that m->kernel_usec.monotonic is always at 0,
2477 * and m->firmware_usec.monotonic and
2478 * m->loader_usec.monotonic should be considered
2479 * negative values. */
2481 firmware_usec = m->firmware_timestamp.monotonic - m->loader_timestamp.monotonic;
2482 loader_usec = m->loader_timestamp.monotonic - m->kernel_timestamp.monotonic;
2483 userspace_usec = m->finish_timestamp.monotonic - m->userspace_timestamp.monotonic;
2484 total_usec = m->firmware_timestamp.monotonic + m->finish_timestamp.monotonic;
2486 if (dual_timestamp_is_set(&m->initrd_timestamp)) {
2488 kernel_usec = m->initrd_timestamp.monotonic - m->kernel_timestamp.monotonic;
2489 initrd_usec = m->userspace_timestamp.monotonic - m->initrd_timestamp.monotonic;
2491 if (!log_on_console())
2492 log_struct(LOG_INFO,
2493 MESSAGE_ID(SD_MESSAGE_STARTUP_FINISHED),
2494 "KERNEL_USEC="USEC_FMT, kernel_usec,
2495 "INITRD_USEC="USEC_FMT, initrd_usec,
2496 "USERSPACE_USEC="USEC_FMT, userspace_usec,
2497 "MESSAGE=Startup finished in %s (kernel) + %s (initrd) + %s (userspace) = %s.",
2498 format_timespan(kernel, sizeof(kernel), kernel_usec, USEC_PER_MSEC),
2499 format_timespan(initrd, sizeof(initrd), initrd_usec, USEC_PER_MSEC),
2500 format_timespan(userspace, sizeof(userspace), userspace_usec, USEC_PER_MSEC),
2501 format_timespan(sum, sizeof(sum), total_usec, USEC_PER_MSEC),
2504 kernel_usec = m->userspace_timestamp.monotonic - m->kernel_timestamp.monotonic;
2507 if (!log_on_console())
2508 log_struct(LOG_INFO,
2509 MESSAGE_ID(SD_MESSAGE_STARTUP_FINISHED),
2510 "KERNEL_USEC="USEC_FMT, kernel_usec,
2511 "USERSPACE_USEC="USEC_FMT, userspace_usec,
2512 "MESSAGE=Startup finished in %s (kernel) + %s (userspace) = %s.",
2513 format_timespan(kernel, sizeof(kernel), kernel_usec, USEC_PER_MSEC),
2514 format_timespan(userspace, sizeof(userspace), userspace_usec, USEC_PER_MSEC),
2515 format_timespan(sum, sizeof(sum), total_usec, USEC_PER_MSEC),
2519 firmware_usec = loader_usec = initrd_usec = kernel_usec = 0;
2520 total_usec = userspace_usec = m->finish_timestamp.monotonic - m->userspace_timestamp.monotonic;
2522 if (!log_on_console())
2523 log_struct(LOG_INFO,
2524 MESSAGE_ID(SD_MESSAGE_STARTUP_FINISHED),
2525 "USERSPACE_USEC="USEC_FMT, userspace_usec,
2526 "MESSAGE=Startup finished in %s.",
2527 format_timespan(sum, sizeof(sum), total_usec, USEC_PER_MSEC),
2531 bus_manager_send_finished(m, firmware_usec, loader_usec, kernel_usec, initrd_usec, userspace_usec, total_usec);
2534 "READY=1\nSTATUS=Startup finished in %s.",
2535 format_timespan(sum, sizeof(sum), total_usec, USEC_PER_MSEC));
2538 static int create_generator_dir(Manager *m, char **generator, const char *name) {
2549 if (m->running_as == SYSTEMD_SYSTEM && getpid() == 1) {
2550 /* systemd --system, not running --test */
2552 p = strappend("/run/systemd/", name);
2556 r = mkdir_p_label(p, 0755);
2558 log_error("Failed to create generator directory %s: %s",
2563 } else if (m->running_as == SYSTEMD_USER) {
2564 const char *s = NULL;
2566 s = getenv("XDG_RUNTIME_DIR");
2569 p = strjoin(s, "/systemd/", name, NULL);
2573 r = mkdir_p_label(p, 0755);
2575 log_error("Failed to create generator directory %s: %s",
2581 /* systemd --system --test */
2583 p = strjoin("/tmp/systemd-", name, ".XXXXXX", NULL);
2588 log_error("Failed to create generator directory %s: %m",
2599 static void trim_generator_dir(Manager *m, char **generator) {
2606 if (rmdir(*generator) >= 0) {
2614 void manager_run_generators(Manager *m) {
2615 _cleanup_closedir_ DIR *d = NULL;
2616 const char *generator_path;
2617 const char *argv[5];
2622 generator_path = m->running_as == SYSTEMD_SYSTEM ? SYSTEM_GENERATOR_PATH : USER_GENERATOR_PATH;
2623 d = opendir(generator_path);
2625 if (errno == ENOENT)
2628 log_error("Failed to enumerate generator directory %s: %m",
2633 r = create_generator_dir(m, &m->generator_unit_path, "generator");
2637 r = create_generator_dir(m, &m->generator_unit_path_early, "generator.early");
2641 r = create_generator_dir(m, &m->generator_unit_path_late, "generator.late");
2645 argv[0] = NULL; /* Leave this empty, execute_directory() will fill something in */
2646 argv[1] = m->generator_unit_path;
2647 argv[2] = m->generator_unit_path_early;
2648 argv[3] = m->generator_unit_path_late;
2651 RUN_WITH_UMASK(0022)
2652 execute_directory(generator_path, d, (char**) argv);
2655 trim_generator_dir(m, &m->generator_unit_path);
2656 trim_generator_dir(m, &m->generator_unit_path_early);
2657 trim_generator_dir(m, &m->generator_unit_path_late);
2660 static void remove_generator_dir(Manager *m, char **generator) {
2667 strv_remove(m->lookup_paths.unit_path, *generator);
2668 rm_rf(*generator, false, true, false);
2674 void manager_undo_generators(Manager *m) {
2677 remove_generator_dir(m, &m->generator_unit_path);
2678 remove_generator_dir(m, &m->generator_unit_path_early);
2679 remove_generator_dir(m, &m->generator_unit_path_late);
2682 int manager_environment_add(Manager *m, char **minus, char **plus) {
2683 char **a = NULL, **b = NULL, **l;
2688 if (!strv_isempty(minus)) {
2689 a = strv_env_delete(l, 1, minus);
2696 if (!strv_isempty(plus)) {
2697 b = strv_env_merge(2, l, plus);
2704 if (m->environment != l)
2705 strv_free(m->environment);
2712 manager_clean_environment(m);
2713 strv_sort(m->environment);
2718 int manager_set_default_rlimits(Manager *m, struct rlimit **default_rlimit) {
2723 for (i = 0; i < RLIMIT_NLIMITS; i++) {
2724 if (!default_rlimit[i])
2727 m->rlimit[i] = newdup(struct rlimit, default_rlimit[i], 1);
2735 void manager_recheck_journal(Manager *m) {
2740 if (m->running_as != SYSTEMD_SYSTEM)
2743 u = manager_get_unit(m, SPECIAL_JOURNALD_SOCKET);
2744 if (u && SOCKET(u)->state != SOCKET_RUNNING) {
2745 log_close_journal();
2749 u = manager_get_unit(m, SPECIAL_JOURNALD_SERVICE);
2750 if (u && SERVICE(u)->state != SERVICE_RUNNING) {
2751 log_close_journal();
2755 /* Hmm, OK, so the socket is fully up and the service is up
2756 * too, then let's make use of the thing. */
2760 void manager_set_show_status(Manager *m, ShowStatus mode) {
2762 assert(IN_SET(mode, SHOW_STATUS_AUTO, SHOW_STATUS_NO, SHOW_STATUS_YES, SHOW_STATUS_TEMPORARY));
2764 if (m->running_as != SYSTEMD_SYSTEM)
2767 m->show_status = mode;
2770 touch("/run/systemd/show-status");
2772 unlink("/run/systemd/show-status");
2775 static bool manager_get_show_status(Manager *m) {
2778 if (m->running_as != SYSTEMD_SYSTEM)
2781 if (m->no_console_output)
2784 if (m->show_status > 0)
2787 /* If Plymouth is running make sure we show the status, so
2788 * that there's something nice to see when people press Esc */
2790 return plymouth_running();
2793 void manager_status_printf(Manager *m, bool ephemeral, const char *status, const char *format, ...) {
2796 if (!manager_get_show_status(m))
2799 /* XXX We should totally drop the check for ephemeral here
2800 * and thus effectively make 'Type=idle' pointless. */
2801 if (ephemeral && m->n_on_console > 0)
2804 if (!manager_is_booting_or_shutting_down(m))
2807 va_start(ap, format);
2808 status_vprintf(status, true, ephemeral, format, ap);
2812 int manager_get_unit_by_path(Manager *m, const char *path, const char *suffix, Unit **_found) {
2813 _cleanup_free_ char *p = NULL;
2821 p = unit_name_from_path(path, suffix);
2825 found = manager_get_unit(m, p);
2835 Set *manager_get_units_requiring_mounts_for(Manager *m, const char *path) {
2836 char p[strlen(path)+1];
2842 path_kill_slashes(p);
2844 return hashmap_get(m->units_requiring_mounts_for, streq(p, "/") ? "" : p);