chiark / gitweb /
execute: when running in session mode, still enforce proper ordering of logger socket
[elogind.git] / manager.c
index 25bc7528825650489e04c18e4c5e813c211a01be..4f389793559e77a28d57118babe9326d0a606c07 100644 (file)
--- a/manager.c
+++ b/manager.c
@@ -28,6 +28,9 @@
 #include <sys/wait.h>
 #include <unistd.h>
 #include <sys/poll.h>
+#include <sys/reboot.h>
+#include <sys/ioctl.h>
+#include <linux/kd.h>
 
 #include "manager.h"
 #include "hashmap.h"
@@ -54,6 +57,8 @@ static int manager_setup_signals(Manager *m) {
         assert_se(sigaddset(&mask, SIGUSR1) == 0);
         assert_se(sigaddset(&mask, SIGUSR2) == 0);
         assert_se(sigaddset(&mask, SIGPIPE) == 0);
+        assert_se(sigaddset(&mask, SIGPWR) == 0);
+        assert_se(sigaddset(&mask, SIGTTIN) == 0);
         assert_se(sigprocmask(SIG_SETMASK, &mask, NULL) == 0);
 
         m->signal_watch.type = WATCH_SIGNAL;
@@ -67,6 +72,185 @@ static int manager_setup_signals(Manager *m) {
         if (epoll_ctl(m->epoll_fd, EPOLL_CTL_ADD, m->signal_watch.fd, &ev) < 0)
                 return -errno;
 
+        if (m->running_as == MANAGER_INIT) {
+                /* Enable that we get SIGINT on control-alt-del */
+                if (reboot(RB_DISABLE_CAD) < 0)
+                        log_warning("Failed to enable ctrl-alt-del handling: %s", strerror(errno));
+
+                /* Enable that we get SIGWINCH on kbrequest */
+                if (ioctl(0, KDSIGACCEPT, SIGWINCH) < 0)
+                        log_warning("Failed to enable kbrequest handling: %s", strerror(errno));
+        }
+
+        return 0;
+}
+
+static char** session_dirs(void) {
+        const char *home, *e;
+        char *config_home = NULL, *data_home = NULL;
+        char **config_dirs = NULL, **data_dirs = NULL;
+        char **r = NULL, **t;
+
+        /* Implement the mechanisms defined in
+         *
+         * http://standards.freedesktop.org/basedir-spec/basedir-spec-0.6.html
+         *
+         * We look in both the config and the data dirs because we
+         * want to encourage that distributors ship their unit files
+         * as data, and allow overriding as configuration.
+         */
+
+        home = getenv("HOME");
+
+        if ((e = getenv("XDG_CONFIG_HOME"))) {
+                if (asprintf(&config_home, "%s/systemd/session", e) < 0)
+                        goto fail;
+
+        } else if (home) {
+                if (asprintf(&config_home, "%s/.config/systemd/session", home) < 0)
+                        goto fail;
+        }
+
+        if ((e = getenv("XDG_CONFIG_DIRS")))
+                config_dirs = strv_split(e, ":");
+        else
+                config_dirs = strv_new("/etc/xdg", NULL);
+
+        if (!config_dirs)
+                goto fail;
+
+        if ((e = getenv("XDG_DATA_HOME"))) {
+                if (asprintf(&data_home, "%s/systemd/session", e) < 0)
+                        goto fail;
+
+        } else if (home) {
+                if (asprintf(&data_home, "%s/.local/share/systemd/session", home) < 0)
+                        goto fail;
+        }
+
+        if ((e = getenv("XDG_DATA_DIRS")))
+                data_dirs = strv_split(e, ":");
+        else
+                data_dirs = strv_new("/usr/local/share", "/usr/share", NULL);
+
+        if (!data_dirs)
+                goto fail;
+
+        /* Now merge everything we found. */
+        if (config_home) {
+                if (!(t = strv_append(r, config_home)))
+                        goto fail;
+                strv_free(r);
+                r = t;
+        }
+
+        if (!(t = strv_merge_concat(r, config_dirs, "/systemd/session")))
+                goto finish;
+        strv_free(r);
+        r = t;
+
+        if (!(t = strv_append(r, SESSION_CONFIG_UNIT_PATH)))
+                goto fail;
+        strv_free(r);
+        r = t;
+
+        if (data_home) {
+                if (!(t = strv_append(r, data_home)))
+                        goto fail;
+                strv_free(r);
+                r = t;
+        }
+
+        if (!(t = strv_merge_concat(r, data_dirs, "/systemd/session")))
+                goto fail;
+        strv_free(r);
+        r = t;
+
+        if (!(t = strv_append(r, SESSION_DATA_UNIT_PATH)))
+                goto fail;
+        strv_free(r);
+        r = t;
+
+        if (!strv_path_make_absolute_cwd(r))
+            goto fail;
+
+finish:
+        free(config_home);
+        strv_free(config_dirs);
+        free(data_home);
+        strv_free(data_dirs);
+
+        return r;
+
+fail:
+        strv_free(r);
+        r = NULL;
+        goto finish;
+}
+
+static int manager_find_paths(Manager *m) {
+        const char *e;
+        char *t;
+        assert(m);
+
+        /* First priority is whatever has been passed to us via env
+         * vars */
+        if ((e = getenv("SYSTEMD_UNIT_PATH")))
+                if (!(m->unit_path = split_path_and_make_absolute(e)))
+                        return -ENOMEM;
+
+        if (strv_isempty(m->unit_path)) {
+
+                /* Nothing is set, so let's figure something out. */
+                strv_free(m->unit_path);
+
+                if (m->running_as == MANAGER_SESSION) {
+                        if (!(m->unit_path = session_dirs()))
+                                return -ENOMEM;
+                } else
+                        if (!(m->unit_path = strv_new(
+                                              SYSTEM_CONFIG_UNIT_PATH,  /* /etc/systemd/system/ */
+                                              SYSTEM_DATA_UNIT_PATH,    /* /lib/systemd/system/ */
+                                              NULL)))
+                                return -ENOMEM;
+        }
+
+        if (m->running_as == MANAGER_INIT) {
+                /* /etc/init.d/ compativility does not matter to users */
+
+                if ((e = getenv("SYSTEMD_SYSVINIT_PATH")))
+                        if (!(m->sysvinit_path = split_path_and_make_absolute(e)))
+                                return -ENOMEM;
+
+                if (strv_isempty(m->sysvinit_path)) {
+                        strv_free(m->sysvinit_path);
+
+                        if (!(m->sysvinit_path = strv_new(
+                                              SYSTEM_SYSVINIT_PATH,     /* /etc/init.d/ */
+                                              NULL)))
+                                return -ENOMEM;
+                }
+        }
+
+        strv_uniq(m->unit_path);
+        strv_uniq(m->sysvinit_path);
+
+        assert(!strv_isempty(m->unit_path));
+        if (!(t = strv_join(m->unit_path, "\n\t")))
+                return -ENOMEM;
+        log_debug("Looking for unit files in:\n\t%s", t);
+        free(t);
+
+        if (!strv_isempty(m->sysvinit_path)) {
+
+                if (!(t = strv_join(m->sysvinit_path, "\n\t")))
+                        return -ENOMEM;
+
+                log_debug("Looking for SysV init scripts in:\n\t%s", t);
+                free(t);
+        } else
+                log_debug("Ignoring SysV init scripts.");
+
         return 0;
 }
 
@@ -76,15 +260,6 @@ Manager* manager_new(void) {
         if (!(m = new0(Manager, 1)))
                 return NULL;
 
-        if (getpid() == 1)
-                m->running_as = MANAGER_INIT;
-        else if (getuid() == 0)
-                m->running_as = MANAGER_SYSTEM;
-        else
-                m->running_as = MANAGER_USER;
-
-        log_debug("systemd running in %s mode.", manager_running_as_to_string(m->running_as));
-
         m->signal_watch.fd = m->mount_watch.fd = m->udev_watch.fd = m->epoll_fd = -1;
         m->current_job_id = 1; /* start as id #1, so that we can leave #0 around as "null-like" value */
 
@@ -103,6 +278,24 @@ Manager* manager_new(void) {
         if ((m->epoll_fd = epoll_create1(EPOLL_CLOEXEC)) < 0)
                 goto fail;
 
+        if (getpid() == 1)
+                m->running_as = MANAGER_INIT;
+        else if (getuid() == 0)
+                m->running_as = MANAGER_SYSTEM;
+        else
+                m->running_as = MANAGER_SESSION;
+
+        log_debug("systemd running in %s mode.", manager_running_as_to_string(m->running_as));
+
+        if (manager_find_paths(m) < 0)
+                goto fail;
+
+        if (chdir("/") < 0)
+                log_warning("Failed to chdir to /: %s", strerror(errno));
+
+        /* Become a session leader if we aren't one yet. */
+        setsid();
+
         if (manager_setup_signals(m) < 0)
                 goto fail;
 
@@ -146,6 +339,9 @@ void manager_free(Manager *m) {
         if (m->signal_watch.fd >= 0)
                 close_nointr(m->signal_watch.fd);
 
+        strv_free(m->unit_path);
+        strv_free(m->sysvinit_path);
+
         free(m);
 }
 
@@ -1008,7 +1204,7 @@ int manager_load_unit(Manager *m, const char *path, Unit **_ret) {
                 return -ENOMEM;
 
         if (is_path(path)) {
-                if (!(ret->meta.load_path = strdup(path))) {
+                if (!(ret->meta.fragment_path = strdup(path))) {
                         unit_free(ret);
                         return -ENOMEM;
                 }
@@ -1177,8 +1373,40 @@ static int manager_process_signal_fd(Manager *m, bool *quit) {
 
                 case SIGINT:
                 case SIGTERM:
-                        *quit = true;
-                        return 0;
+
+                        if (m->running_as != MANAGER_INIT) {
+                                *quit = true;
+                                return 0;
+
+                        } else {
+                                Unit *target;
+                                int r;
+
+                                if ((r = manager_load_unit(m, SPECIAL_CTRL_ALT_DEL_TARGET, &target)) < 0)
+                                        log_error("Failed to load ctrl-alt-del target: %s", strerror(-r));
+                                else if ((r = manager_add_job(m, JOB_START, target, JOB_REPLACE, true, NULL)) < 0)
+                                        log_error("Failed to enqueue ctrl-alt-del job: %s", strerror(-r));
+
+                                break;
+                        }
+
+                case SIGWINCH:
+
+                        if (m->running_as == MANAGER_INIT) {
+                                Unit *target;
+                                int r;
+
+                                if ((r = manager_load_unit(m, SPECIAL_KBREQUEST_TARGET, &target)) < 0)
+                                        log_error("Failed to load kbrequest target: %s", strerror(-r));
+                                else if ((r = manager_add_job(m, JOB_START, target, JOB_REPLACE, true, NULL)) < 0)
+                                        log_error("Failed to enqueue kbrequest job: %s", strerror(-r));
+
+                                break;
+                        }
+
+                        /* This is a nop on non-init systemd's */
+
+                        break;
 
                 default:
                         log_info("Got unhandled signal <%s>.", strsignal(sfsi.ssi_signo));
@@ -1360,7 +1588,7 @@ int manager_get_job_from_dbus_path(Manager *m, const char *s, Job **_j) {
 static const char* const manager_running_as_table[_MANAGER_RUNNING_AS_MAX] = {
         [MANAGER_INIT] = "init",
         [MANAGER_SYSTEM] = "system",
-        [MANAGER_USER] = "user"
+        [MANAGER_SESSION] = "session"
 };
 
 DEFINE_STRING_TABLE_LOOKUP(manager_running_as, ManagerRunningAs);