manager.c revision d9814c76ec35e53a6b6448c0f522da0c71907c81
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt This file is part of systemd.
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt Copyright 2010 Lennart Poettering
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt systemd is free software; you can redistribute it and/or modify it
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt under the terms of the GNU Lesser General Public License as published by
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt the Free Software Foundation; either version 2.1 of the License, or
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt (at your option) any later version.
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt systemd is distributed in the hope that it will be useful, but
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt WITHOUT ANY WARRANTY; without even the implied warranty of
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt Lesser General Public License for more details.
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt You should have received a copy of the GNU Lesser General Public License
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt along with systemd; If not, see <http://www.gnu.org/licenses/>.
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt/* Initial delay and the interval for printing status messages about running jobs */
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt#define JOBS_IN_PROGRESS_WAIT_USEC (5*USEC_PER_SEC)
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt#define JOBS_IN_PROGRESS_PERIOD_USEC (USEC_PER_SEC / 3)
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidtstatic int manager_dispatch_notify_fd(sd_event_source *source, int fd, uint32_t revents, void *userdata);
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidtstatic int manager_dispatch_signal_fd(sd_event_source *source, int fd, uint32_t revents, void *userdata);
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidtstatic int manager_dispatch_time_change_fd(sd_event_source *source, int fd, uint32_t revents, void *userdata);
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidtstatic int manager_dispatch_idle_pipe_fd(sd_event_source *source, int fd, uint32_t revents, void *userdata);
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidtstatic int manager_dispatch_jobs_in_progress(sd_event_source *source, usec_t usec, void *userdata);
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidtstatic int manager_dispatch_run_queue(sd_event_source *source, void *userdata);
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidtstatic int manager_run_generators(Manager *m);
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidtstatic void manager_undo_generators(Manager *m);
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidtstatic void manager_watch_jobs_in_progress(Manager *m) {
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt next = now(CLOCK_MONOTONIC) + JOBS_IN_PROGRESS_WAIT_USEC;
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt (void) sd_event_source_set_description(m->jobs_in_progress_event_source, "manager-jobs-in-progress");
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt#define CYLON_BUFFER_EXTRA (2*(sizeof(ANSI_RED)-1) + sizeof(ANSI_HIGHLIGHT_RED)-1 + 2*(sizeof(ANSI_NORMAL)-1))
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidtstatic void draw_cylon(char buffer[], size_t buflen, unsigned width, unsigned pos) {
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt assert(buflen >= CYLON_BUFFER_EXTRA + width + 1);
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt assert(pos <= width+1); /* 0 or width+1 mean that the center light is behind the corner */
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidtvoid manager_flip_auto_status(Manager *m, bool enable) {
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt manager_set_show_status(m, SHOW_STATUS_TEMPORARY);
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt manager_set_show_status(m, SHOW_STATUS_AUTO);
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidtstatic void manager_print_jobs_in_progress(Manager *m) {
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt char time[FORMAT_TIMESPAN_MAX], limit[FORMAT_TIMESPAN_MAX] = "no limit";
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt print_nr = (m->jobs_in_progress_iteration / JOBS_IN_PROGRESS_PERIOD_DIVISOR) % m->n_running_jobs;
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt if (j->state == JOB_RUNNING && counter++ == print_nr)
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt /* m->n_running_jobs must be consistent with the contents of m->jobs,
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt * so the above loop must have succeeded in finding j. */
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt cylon_pos = m->jobs_in_progress_iteration % 14;
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt draw_cylon(cylon, sizeof(cylon), 6, cylon_pos);
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt if (asprintf(&job_of_n, "(%u of %u) ", counter, m->n_running_jobs) < 0)
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt format_timespan(time, sizeof(time), now(CLOCK_MONOTONIC) - j->begin_usec, 1*USEC_PER_SEC);
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt format_timespan(limit, sizeof(limit), x - j->begin_usec, 1*USEC_PER_SEC);
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt manager_status_printf(m, STATUS_TYPE_EPHEMERAL, cylon,
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt "%sA %s job is running for %s (%s / %s)",
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidtstatic int have_ask_password(void) {
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt return false;
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt return false;
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidtstatic int manager_dispatch_ask_password_fd(sd_event_source *source,
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt /* Log error but continue. Negative have_ask_password
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt * is treated as unknown status. */
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt log_error_errno(m->have_ask_password, "Failed to list /run/systemd/ask-password: %m");
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidtstatic void manager_close_ask_password(Manager *m) {
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt m->ask_password_event_source = sd_event_source_unref(m->ask_password_event_source);
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt m->ask_password_inotify_fd = safe_close(m->ask_password_inotify_fd);
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidtstatic int manager_check_ask_password(Manager *m) {
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt mkdir_p_label("/run/systemd/ask-password", 0755);
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt m->ask_password_inotify_fd = inotify_init1(IN_NONBLOCK|IN_CLOEXEC);
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt return log_error_errno(errno, "inotify_init1() failed: %m");
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt if (inotify_add_watch(m->ask_password_inotify_fd, "/run/systemd/ask-password", IN_CREATE|IN_DELETE|IN_MOVE) < 0) {
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt log_error_errno(errno, "Failed to add watch on /run/systemd/ask-password: %m");
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt r = sd_event_add_io(m->event, &m->ask_password_event_source,
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt log_error_errno(errno, "Failed to add event source for /run/systemd/ask-password: %m");
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt (void) sd_event_source_set_description(m->ask_password_event_source, "manager-ask-password");
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt /* Queries might have been added meanwhile... */
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt manager_dispatch_ask_password_fd(m->ask_password_event_source,
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidtstatic int manager_watch_idle_pipe(Manager *m) {
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt r = sd_event_add_io(m->event, &m->idle_pipe_event_source, m->idle_pipe[2], EPOLLIN, manager_dispatch_idle_pipe_fd, m);
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt return log_error_errno(r, "Failed to watch idle pipe: %m");
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt (void) sd_event_source_set_description(m->idle_pipe_event_source, "manager-idle-pipe");
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidtstatic void manager_close_idle_pipe(Manager *m) {
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt m->idle_pipe_event_source = sd_event_source_unref(m->idle_pipe_event_source);
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidtstatic int manager_setup_time_change(Manager *m) {
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt /* We only care for the cancellation event, hence we set the
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt * timeout to the latest possible value. */
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt assert_cc(sizeof(time_t) == sizeof(TIME_T_MAX));
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt /* Uses TFD_TIMER_CANCEL_ON_SET to get notifications whenever
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt * CLOCK_REALTIME makes a jump relative to CLOCK_MONOTONIC */
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt m->time_change_fd = timerfd_create(CLOCK_REALTIME, TFD_NONBLOCK|TFD_CLOEXEC);
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt return log_error_errno(errno, "Failed to create timerfd: %m");
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt if (timerfd_settime(m->time_change_fd, TFD_TIMER_ABSTIME|TFD_TIMER_CANCEL_ON_SET, &its, NULL) < 0) {
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt log_debug_errno(errno, "Failed to set up TFD_TIMER_CANCEL_ON_SET, ignoring: %m");
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt m->time_change_fd = safe_close(m->time_change_fd);
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt r = sd_event_add_io(m->event, &m->time_change_event_source, m->time_change_fd, EPOLLIN, manager_dispatch_time_change_fd, m);
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt return log_error_errno(r, "Failed to create time change event source: %m");
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt (void) sd_event_source_set_description(m->time_change_event_source, "manager-time-change");
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt log_debug("Set up TFD_TIMER_CANCEL_ON_SET timerfd.");
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidtstatic int enable_special_signals(Manager *m) {
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt /* Enable that we get SIGINT on control-alt-del. In containers
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt * this will fail with EPERM (older) or EINVAL (newer), so
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt * ignore that. */
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt if (reboot(RB_DISABLE_CAD) < 0 && errno != EPERM && errno != EINVAL)
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt log_warning_errno(errno, "Failed to enable ctrl-alt-del handling: %m");
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt fd = open_terminal("/dev/tty0", O_RDWR|O_NOCTTY|O_CLOEXEC);
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt /* Support systems without virtual console */
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt log_warning_errno(errno, "Failed to open /dev/tty0: %m");
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt /* Enable that we get SIGWINCH on kbrequest */
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt log_warning_errno(errno, "Failed to enable kbrequest handling: %m");
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidtstatic int manager_setup_signals(Manager *m) {
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt assert_se(sigaction(SIGCHLD, &sa, NULL) == 0);
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt /* We make liberal use of realtime signals here. On
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt * Linux/glibc we have 30 of them (with the exception of Linux
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt * on hppa, see below), between SIGRTMIN+0 ... SIGRTMIN+30
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt * (aka SIGRTMAX). */
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt SIGUSR1, /* systemd/upstart: reconnect to D-Bus */
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt SIGINT, /* Kernel sends us this on control-alt-del */
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt SIGWINCH, /* Kernel sends us this on kbrequest (alt-arrowup) */
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt SIGPWR, /* Some kernel drivers and upsd send us this on power failure */
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt SIGRTMIN+0, /* systemd: start default.target */
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt SIGRTMIN+1, /* systemd: isolate rescue.target */
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt SIGRTMIN+2, /* systemd: isolate emergency.target */
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt SIGRTMIN+4, /* systemd: start poweroff.target */
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt SIGRTMIN+5, /* systemd: start reboot.target */
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt SIGRTMIN+6, /* systemd: start kexec.target */
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt /* ... space for more special targets ... */
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt SIGRTMIN+14, /* systemd: Immediate poweroff */
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt /* ... space for more immediate system state changes ... */
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt SIGRTMIN+20, /* systemd: enable status messages */
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt SIGRTMIN+21, /* systemd: disable status messages */
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt SIGRTMIN+22, /* systemd: set log level to LOG_DEBUG */
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt SIGRTMIN+23, /* systemd: set log level to LOG_INFO */
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt SIGRTMIN+24, /* systemd: Immediate exit (--user only) */
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt /* .. one free signal here ... */
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt#if !defined(__hppa64__) && !defined(__hppa__)
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt /* Apparently Linux on hppa has fewer RT
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt * signals (SIGRTMAX is SIGRTMIN+25 there),
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt * hence let's not try to make use of them
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt * here. Since these commands are accessible
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt * by different means and only really a safety
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt * net, the missing functionality on hppa
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt * shouldn't matter. */
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt SIGRTMIN+26, /* systemd: set log target to journal-or-kmsg */
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt SIGRTMIN+27, /* systemd: set log target to console */
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt SIGRTMIN+28, /* systemd: set log target to kmsg */
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt SIGRTMIN+29, /* systemd: set log target to syslog-or-kmsg (obsolete) */
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt /* ... one free signal here SIGRTMIN+30 ... */
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt assert_se(sigprocmask(SIG_SETMASK, &mask, NULL) == 0);
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt m->signal_fd = signalfd(-1, &mask, SFD_NONBLOCK|SFD_CLOEXEC);
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt r = sd_event_add_io(m->event, &m->signal_event_source, m->signal_fd, EPOLLIN, manager_dispatch_signal_fd, m);
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt (void) sd_event_source_set_description(m->signal_event_source, "manager-signal");
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt /* Process signals a bit earlier than the rest of things, but
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt * later than notify_fd processing, so that the notify
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt * processing can still figure out to which process/service a
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt * message belongs, before we reap the process. */
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt r = sd_event_source_set_priority(m->signal_event_source, SD_EVENT_PRIORITY_NORMAL-5);
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidtstatic void manager_clean_environment(Manager *m) {
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt /* Let's remove some environment variables that we
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt * need ourselves to communicate with our clients */
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt "NOTIFY_SOCKET",
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt "MANAGERPID",
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt "LISTEN_PID",
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt "LISTEN_FDS",
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt "LISTEN_FDNAMES",
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt "WATCHDOG_PID",
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt "WATCHDOG_USEC",
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidtstatic int manager_default_environment(Manager *m) {
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt /* The system manager always starts with a clean
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt * environment for its children. It does not import
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt * the kernel or the parents exported variables.
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt * The initial passed environ is untouched to keep
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt * /proc/self/environ valid; it is used for tagging
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt * the init process inside containers. */
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt m->environment = strv_new("PATH=" DEFAULT_PATH,
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt /* Import locale variables LC_*= from configuration */
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt /* The user manager passes its own environment
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt * along to its children. */
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidtint manager_new(ManagerRunningAs running_as, bool test_run, Manager **_m) {
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt static const char * const unit_log_fields[_MANAGER_RUNNING_AS_MAX] = {
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt static const char * const unit_log_format_strings[_MANAGER_RUNNING_AS_MAX] = {
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt assert(running_as < _MANAGER_RUNNING_AS_MAX);
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt if (running_as == MANAGER_SYSTEM && detect_container() <= 0)
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt boot_timestamps(&m->userspace_timestamp, &m->firmware_timestamp, &m->loader_timestamp);
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt m->default_timer_accuracy_usec = USEC_PER_MINUTE;
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt /* Prepare log fields we can use for structured logging */
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt m->unit_log_field = unit_log_fields[running_as];
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt m->unit_log_format_string = unit_log_format_strings[running_as];
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt m->idle_pipe[0] = m->idle_pipe[1] = m->idle_pipe[2] = m->idle_pipe[3] = -1;
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt m->pin_cgroupfs_fd = m->notify_fd = m->signal_fd = m->time_change_fd =
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt m->dev_autofs_fd = m->private_listen_fd = m->kdbus_fd = m->cgroup_inotify_fd = -1;
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt m->current_job_id = 1; /* start as id #1, so that we can leave #0 around as "null-like" value */
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt m->have_ask_password = -EINVAL; /* we don't know */
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt m->cgroup_netclass_registry_last = CGROUP_NETCLASS_FIXED_MAX;
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt /* Reboot immediately if the user hits C-A-D more often than 7x per 2s */
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt RATELIMIT_INIT(m->ctrl_alt_del_ratelimit, 2 * USEC_PER_SEC, 7);
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt r = hashmap_ensure_allocated(&m->units, &string_hash_ops);
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt r = hashmap_ensure_allocated(&m->jobs, NULL);
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt r = hashmap_ensure_allocated(&m->cgroup_unit, &string_hash_ops);
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt r = hashmap_ensure_allocated(&m->watch_bus, &string_hash_ops);
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt r = sd_event_add_defer(m->event, &m->run_queue_event_source, manager_dispatch_run_queue, m);
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt r = sd_event_source_set_priority(m->run_queue_event_source, SD_EVENT_PRIORITY_IDLE);
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt r = sd_event_source_set_enabled(m->run_queue_event_source, SD_EVENT_OFF);
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt (void) sd_event_source_set_description(m->run_queue_event_source, "manager-run-queue");
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt /* Note that we set up neither kdbus, nor the notify fd
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt * here. We do that after deserialization, since they might
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt * have gotten serialized across the reexec. */
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt /* First free all secondary fields */
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt m->notify_event_source = sd_event_source_unref(m->notify_event_source);
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0);
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt return log_error_errno(errno, "Failed to allocate notification socket: %m");
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt m->notify_socket = strdup("/run/systemd/notify");
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt const char *e;
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt log_error_errno(errno, "XDG_RUNTIME_DIR is not set: %m");
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt m->notify_socket = strappend(e, "/systemd/notify");
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt (void) mkdir_parents_label(m->notify_socket, 0755);
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt strncpy(sa.un.sun_path, m->notify_socket, sizeof(sa.un.sun_path)-1);
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt r = bind(fd, &sa.sa, offsetof(struct sockaddr_un, sun_path) + strlen(sa.un.sun_path));
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt return log_error_errno(errno, "bind(%s) failed: %m", sa.un.sun_path);
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt r = setsockopt(fd, SOL_SOCKET, SO_PASSCRED, &one, sizeof(one));
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt return log_error_errno(errno, "SO_PASSCRED failed: %m");
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt log_debug("Using notification socket %s", m->notify_socket);
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt r = sd_event_add_io(m->event, &m->notify_event_source, m->notify_fd, EPOLLIN, manager_dispatch_notify_fd, m);
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt return log_error_errno(r, "Failed to allocate notify event source: %m");
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt /* Process signals a bit earlier than SIGCHLD, so that we can
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt * still identify to which service an exit message belongs */
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt r = sd_event_source_set_priority(m->notify_event_source, SD_EVENT_PRIORITY_NORMAL-7);
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt return log_error_errno(r, "Failed to set priority of notify event source: %m");
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt (void) sd_event_source_set_description(m->notify_event_source, "manager-notify");
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt m->running_as == MANAGER_SYSTEM ? "system" : "user",
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt return log_debug_errno(m->kdbus_fd, "Failed to set up kdbus: %m");
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt log_debug("Successfully set up kdbus on %s", p);
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidtstatic int manager_connect_bus(Manager *m, bool reexecuting) {
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt (m->running_as == MANAGER_USER && getenv("DBUS_SESSION_BUS_ADDRESS"));
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt /* Try to connect to the buses, if possible. */
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidtstatic unsigned manager_dispatch_cleanup_queue(Manager *m) {
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt unsigned n = 0;
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt while ((u = m->cleanup_queue)) {
8f88aed740ded77af443bb1b7c79bb229b50f8f8Michal Schmidt GC_OFFSET_IN_PATH, /* This one is on the path we were traveling */
8f88aed740ded77af443bb1b7c79bb229b50f8f8Michal Schmidt GC_OFFSET_GOOD, /* We still need this unit */
8f88aed740ded77af443bb1b7c79bb229b50f8f8Michal Schmidt GC_OFFSET_BAD, /* We don't need this unit anymore */
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidtstatic void unit_gc_sweep(Unit *u, unsigned gc_marker) {
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt if (u->gc_marker == gc_marker + GC_OFFSET_GOOD ||
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt u->gc_marker == gc_marker + GC_OFFSET_IN_PATH)
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt u->gc_marker = gc_marker + GC_OFFSET_IN_PATH;
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt SET_FOREACH(other, u->dependencies[UNIT_REFERENCED_BY], i) {
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt if (other->gc_marker == gc_marker + GC_OFFSET_GOOD)
8f88aed740ded77af443bb1b7c79bb229b50f8f8Michal Schmidt if (other->gc_marker != gc_marker + GC_OFFSET_BAD)
if (is_bad)
goto bad;
bad:
good:
Unit *u;
unsigned gc_marker;
assert(m);
while ((u = m->gc_queue)) {
u->in_gc_queue = false;
if (u->id)
m->n_in_gc_queue = 0;
Unit *u;
assert(m);
unit_free(u);
m->n_on_console = 0;
m->n_running_jobs = 0;
UnitType c;
return NULL;
for (c = 0; c < _UNIT_TYPE_MAX; c++)
bus_done(m);
for (i = 0; i < _RLIMIT_MAX; i++)
free(m);
return NULL;
UnitType c;
assert(m);
for (c = 0; c < _UNIT_TYPE_MAX; c++) {
if (!unit_type_supported(c)) {
Iterator i;
Unit *u;
assert(m);
if (u->id != k)
r = unit_coldplug(u);
assert(m);
if (!m->unit_path_cache) {
d = opendir(*i);
r = -ENOMEM;
goto fail;
goto fail;
d = safe_closedir(d);
fail:
Iterator i;
Unit *u;
assert(m);
assert(m);
r = manager_run_generators(m);
r = lookup_paths_init(
NULL,
if (serialization)
m->n_reloading ++;
if (serialization)
q = manager_setup_notify(m);
manager_coldplug(m);
if (serialization) {
m->n_reloading --;
m->send_reloading_done = true;
int manager_add_job(Manager *m, JobType type, Unit *unit, JobMode mode, sd_bus_error *e, Job **_ret) {
assert(m);
return sd_bus_error_setf(e, BUS_ERROR_NO_ISOLATION, "Operation refused, unit may not be isolated.");
log_unit_debug(unit, "Trying to enqueue job %s/%s/%s", unit->id, job_type_to_string(type), job_mode_to_string(mode));
if (!tr)
return -ENOMEM;
goto tr_abort;
goto tr_abort;
goto tr_abort;
if (_ret)
int manager_add_job_by_name(Manager *m, JobType type, const char *name, JobMode mode, sd_bus_error *e, Job **ret) {
assert(m);
int manager_add_job_by_name_and_warn(Manager *m, JobType type, const char *name, JobMode mode, Job **ret) {
assert(m);
return log_warning_errno(r, "Failed to enqueue %s job for %s: %s", job_mode_to_string(mode), name, bus_error_message(&error, r));
assert(m);
assert(m);
Unit *u;
assert(m);
if (m->dispatching_load_queue)
m->dispatching_load_queue = true;
while ((u = m->load_queue)) {
unit_load(u);
m->dispatching_load_queue = false;
Manager *m,
const char *name,
const char *path,
sd_bus_error *e,
UnitType t;
assert(m);
if (!name)
if (ret) {
if (!ret)
return -ENOMEM;
if (path) {
return -ENOMEM;
if (_ret)
int manager_load_unit(
Manager *m,
const char *name,
const char *path,
sd_bus_error *e,
assert(m);
if (_ret)
Iterator i;
Job *j;
assert(s);
assert(f);
Iterator i;
Unit *u;
assert(s);
assert(f);
if (u->id == t)
Job *j;
assert(m);
Job *j;
assert(m);
while ((j = m->run_queue)) {
if (m->n_running_jobs > 0)
if (m->n_on_console > 0)
Job *j;
Unit *u;
assert(m);
if (m->dispatching_dbus_queue)
m->dispatching_dbus_queue = true;
while ((u = m->dbus_unit_queue)) {
while ((j = m->dbus_job_queue)) {
m->dispatching_dbus_queue = false;
if (m->send_reloading_done) {
m->send_reloading_done = false;
bus_manager_send_reloading(m, false);
if (m->queued_message)
static void manager_invoke_notify_message(Manager *m, Unit *u, pid_t pid, const char *buf, size_t n, FDSet *fds) {
assert(m);
assert(u);
assert(n > 0);
if (!tags) {
log_oom();
static int manager_dispatch_notify_fd(sd_event_source *source, int fd, uint32_t revents, void *userdata) {
} control = {};
bool found = false;
unsigned n_fds = 0;
ssize_t n;
assert(m);
return -errno;
if (n_fds > 0) {
return log_oom();
buf[n] = 0;
if (u1) {
found = true;
found = true;
found = true;
if (!found)
assert(m);
assert(u);
assert(m);
return -errno;
if (u1)
return -errno;
static int manager_dispatch_signal_fd(sd_event_source *source, int fd, uint32_t revents, void *userdata) {
ssize_t n;
bool sigchld = false;
assert(m);
if (n != sizeof(sfsi)) {
return -EIO;
return -errno;
&sfsi);
case SIGCHLD:
sigchld = true;
case SIGTERM:
case SIGINT:
status_printf(NULL, true, false, "Ctrl-Alt-Del was pressed more than 7 times within 2s, rebooting immediately.");
case SIGWINCH:
case SIGPWR:
case SIGUSR1: {
Unit *u;
bus_init(m, true);
case SIGUSR2: {
r = fflush_and_check(f);
case SIGHUP:
static const char * const target_table[] = {
[0] = SPECIAL_DEFAULT_TARGET,
[0] = MANAGER_HALT,
if (sigchld)
static int manager_dispatch_time_change_fd(sd_event_source *source, int fd, uint32_t revents, void *userdata) {
Iterator i;
Unit *u;
assert(m);
NULL);
static int manager_dispatch_idle_pipe_fd(sd_event_source *source, int fd, uint32_t revents, void *userdata) {
assert(m);
static int manager_dispatch_jobs_in_progress(sd_event_source *source, usec_t usec, void *userdata) {
assert(m);
assert(m);
r = manager_dispatch_sigchld(m);
if (manager_dispatch_load_queue(m) > 0)
if (manager_dispatch_gc_queue(m) > 0)
if (manager_dispatch_cleanup_queue(m) > 0)
if (manager_dispatch_cgroup_queue(m) > 0)
if (manager_dispatch_dbus_queue(m) > 0)
if (wait_usec <= 0)
return m->exit_code;
Unit *u;
assert(m);
assert(s);
r = unit_name_from_dbus_path(s, &n);
*_u = u;
unsigned id;
Job *j;
assert(m);
assert(s);
return -EINVAL;
return -ENOENT;
*_j = j;
#ifdef HAVE_AUDIT
const char *msg;
int audit_fd, r;
if (audit_fd < 0)
if (m->n_reloading > 0)
if (m->n_reloading > 0)
if (detect_container() > 0)
if (fd < 0) {
if (connect(fd, &sa.sa, offsetof(struct sockaddr_un, sun_path) + 1 + strlen(sa.un.sun_path+1)) < 0) {
log_oom();
errno = 0;
const char *path;
FILE *f;
if (fd < 0)
return -errno;
return -errno;
*_f = f;
Iterator i;
Unit *u;
assert(m);
assert(f);
m->n_reloading ++;
if (!in_initrd()) {
if (!switching_root) {
if (!ce)
return -ENOMEM;
if (m->notify_fd >= 0) {
int copy;
if (copy < 0)
return copy;
if (m->kdbus_fd >= 0) {
int copy;
if (copy < 0)
return copy;
if (u->id != t)
m->n_reloading --;
m->n_reloading --;
if (ferror(f))
return -EIO;
assert(m);
assert(f);
m->n_reloading ++;
if (feof(f))
r = -errno;
goto finish;
uint32_t n;
m->n_installed_jobs += n;
uint32_t n;
m->n_failed_jobs += n;
goto finish;
r = -ENOMEM;
goto finish;
m->environment = e;
int fd;
r = -ENOMEM;
goto finish;
m->notify_socket = n;
int fd;
Unit *u;
if (feof(f))
r = -errno;
goto finish;
goto finish;
goto finish;
if (ferror(f))
r = -EIO;
m->n_reloading --;
assert(m);
r = manager_open_serialization(m, &f);
m->n_reloading ++;
bus_manager_send_reloading(m, true);
if (!fds) {
m->n_reloading --;
return -ENOMEM;
m->n_reloading --;
m->n_reloading --;
return -errno;
q = manager_run_generators(m);
q = lookup_paths_init(
NULL,
fclose(f);
f = NULL;
q = manager_setup_notify(m);
manager_coldplug(m);
if (m->api_bus)
m->n_reloading--;
m->send_reloading_done = true;
assert(m);
return m->n_reloading != 0;
Unit *u;
Iterator i;
assert(m);
Unit *u;
assert(m);
return unit_inactive_or_pending(u);
char userspace[FORMAT_TIMESPAN_MAX], initrd[FORMAT_TIMESPAN_MAX], kernel[FORMAT_TIMESPAN_MAX], sum[FORMAT_TIMESPAN_MAX];
if (m->test_run)
NULL);
initrd_usec = 0;
NULL);
NULL);
bus_manager_send_finished(m, firmware_usec, loader_usec, kernel_usec, initrd_usec, userspace_usec, total_usec);
sd_notifyf(false,
assert(m);
if (m->n_reloading > 0)
if (m->jobs_in_progress_event_source)
(void) sd_event_source_set_time(m->jobs_in_progress_event_source, now(CLOCK_MONOTONIC) + JOBS_IN_PROGRESS_WAIT_USEC);
manager_flip_auto_status(m, false);
m->confirm_spawn = false;
manager_set_first_boot(m, false);
assert(m);
if (*generator)
return log_oom();
free(p);
const char *s = NULL;
return -EINVAL;
return log_oom();
free(p);
return log_oom();
if (!mkdtemp(p)) {
free(p);
return -errno;
*generator = p;
assert(m);
if (!*generator)
char **path;
assert(m);
if (m->test_run)
if (!paths)
return log_oom();
goto found;
goto finish;
goto finish;
goto finish;
assert(m);
if (!*generator)
assert(m);
assert(m);
l = m->environment;
return -ENOMEM;
strv_free(a);
return -ENOMEM;
if (m->environment != l)
strv_free(a);
strv_free(b);
m->environment = l;
assert(m);
for (i = 0; i < _RLIMIT_MAX; i++) {
if (!default_rlimit[i])
if (!m->rlimit[i])
return -ENOMEM;
Unit *u;
assert(m);
log_open();
assert(m);
if (mode > 0)
assert(m);
if (m->no_console_output)
if (m->show_status > 0)
assert(m);
if (m->first_boot != (int) b) {
m->first_boot = b;
void manager_status_printf(Manager *m, StatusType type, const char *status, const char *format, ...) {
assert(m);
assert(m);
unsigned size;
assert(m);
if (failed) {
return log_oom();
return log_oom();
Unit *u;
assert(m);
return MANAGER_INITIALIZING;
return MANAGER_STARTING;
if (u && u->job && IN_SET(u->job->type, JOB_START, JOB_RESTART, JOB_TRY_RESTART, JOB_RELOAD_OR_START))
return MANAGER_STOPPING;
return MANAGER_MAINTENANCE;
return MANAGER_MAINTENANCE;
return MANAGER_DEGRADED;
return MANAGER_RUNNING;