chiark / gitweb /
logind: be more verbose on errors
[elogind.git] / src / login / logind.c
index 777d912..9094567 100644 (file)
@@ -6,16 +6,16 @@
   Copyright 2011 Lennart Poettering
 
   systemd is free software; you can redistribute it and/or modify it
-  under the terms of the GNU General Public License as published by
-  the Free Software Foundation; either version 2 of the License, or
+  under the terms of the GNU Lesser General Public License as published by
+  the Free Software Foundation; either version 2.1 of the License, or
   (at your option) any later version.
 
   systemd is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
-  General Public License for more details.
+  Lesser General Public License for more details.
 
-  You should have received a copy of the GNU General Public License
+  You should have received a copy of the GNU Lesser General Public License
   along with systemd; If not, see <http://www.gnu.org/licenses/>.
 ***/
 
@@ -28,6 +28,7 @@
 #include <sys/epoll.h>
 #include <sys/ioctl.h>
 #include <linux/vt.h>
+#include <sys/timerfd.h>
 
 #include <systemd/sd-daemon.h>
 
@@ -36,6 +37,7 @@
 #include "dbus-loop.h"
 #include "strv.h"
 #include "conf-parser.h"
+#include "mkdir.h"
 
 Manager *manager_new(void) {
         Manager *m;
@@ -48,24 +50,47 @@ Manager *manager_new(void) {
         m->bus_fd = -1;
         m->udev_seat_fd = -1;
         m->udev_vcsa_fd = -1;
+        m->udev_button_fd = -1;
         m->epoll_fd = -1;
+        m->reserve_vt_fd = -1;
+
         m->n_autovts = 6;
+        m->reserve_vt = 6;
+        m->inhibit_delay_max = 5 * USEC_PER_SEC;
+        m->handle_power_key = HANDLE_POWEROFF;
+        m->handle_suspend_key = HANDLE_SUSPEND;
+        m->handle_hibernate_key = HANDLE_HIBERNATE;
+        m->handle_lid_switch = HANDLE_SUSPEND;
+        m->lid_switch_ignore_inhibited = true;
+
+        m->idle_action_fd = -1;
+        m->idle_action_usec = 30 * USEC_PER_MINUTE;
+        m->idle_action = HANDLE_IGNORE;
+        m->idle_action_not_before_usec = now(CLOCK_MONOTONIC);
 
         m->devices = hashmap_new(string_hash_func, string_compare_func);
         m->seats = hashmap_new(string_hash_func, string_compare_func);
         m->sessions = hashmap_new(string_hash_func, string_compare_func);
         m->users = hashmap_new(trivial_hash_func, trivial_compare_func);
-        m->cgroups = hashmap_new(string_hash_func, string_compare_func);
-        m->fifo_fds = hashmap_new(trivial_hash_func, trivial_compare_func);
+        m->inhibitors = hashmap_new(string_hash_func, string_compare_func);
+        m->buttons = hashmap_new(string_hash_func, string_compare_func);
+
+        m->user_units = hashmap_new(string_hash_func, string_compare_func);
+        m->session_units = hashmap_new(string_hash_func, string_compare_func);
 
-        if (!m->devices || !m->seats || !m->sessions || !m->users || !m->cgroups || !m->fifo_fds) {
+        m->session_fds = hashmap_new(trivial_hash_func, trivial_compare_func);
+        m->inhibitor_fds = hashmap_new(trivial_hash_func, trivial_compare_func);
+        m->button_fds = hashmap_new(trivial_hash_func, trivial_compare_func);
+
+        if (!m->devices || !m->seats || !m->sessions || !m->users || !m->inhibitors || !m->buttons ||
+            !m->user_units || !m->session_units ||
+            !m->session_fds || !m->inhibitor_fds || !m->button_fds) {
                 manager_free(m);
                 return NULL;
         }
 
-        m->reset_controllers = strv_new("cpu", NULL);
         m->kill_exclude_users = strv_new("root", NULL);
-        if (!m->reset_controllers || !m->kill_exclude_users) {
+        if (!m->kill_exclude_users) {
                 manager_free(m);
                 return NULL;
         }
@@ -76,11 +101,6 @@ Manager *manager_new(void) {
                 return NULL;
         }
 
-        if (cg_get_user_path(&m->cgroup_path) < 0) {
-                manager_free(m);
-                return NULL;
-        }
-
         return m;
 }
 
@@ -89,6 +109,8 @@ void manager_free(Manager *m) {
         User *u;
         Device *d;
         Seat *s;
+        Inhibitor *i;
+        Button *b;
 
         assert(m);
 
@@ -104,21 +126,35 @@ void manager_free(Manager *m) {
         while ((s = hashmap_first(m->seats)))
                 seat_free(s);
 
-        hashmap_free(m->sessions);
-        hashmap_free(m->users);
+        while ((i = hashmap_first(m->inhibitors)))
+                inhibitor_free(i);
+
+        while ((b = hashmap_first(m->buttons)))
+                button_free(b);
+
         hashmap_free(m->devices);
         hashmap_free(m->seats);
-        hashmap_free(m->cgroups);
-        hashmap_free(m->fifo_fds);
+        hashmap_free(m->sessions);
+        hashmap_free(m->users);
+        hashmap_free(m->inhibitors);
+        hashmap_free(m->buttons);
+
+        hashmap_free(m->user_units);
+        hashmap_free(m->session_units);
+
+        hashmap_free(m->session_fds);
+        hashmap_free(m->inhibitor_fds);
+        hashmap_free(m->button_fds);
 
         if (m->console_active_fd >= 0)
                 close_nointr_nofail(m->console_active_fd);
 
         if (m->udev_seat_monitor)
                 udev_monitor_unref(m->udev_seat_monitor);
-
         if (m->udev_vcsa_monitor)
                 udev_monitor_unref(m->udev_vcsa_monitor);
+        if (m->udev_button_monitor)
+                udev_monitor_unref(m->udev_button_monitor);
 
         if (m->udev)
                 udev_unref(m->udev);
@@ -135,12 +171,16 @@ void manager_free(Manager *m) {
         if (m->epoll_fd >= 0)
                 close_nointr_nofail(m->epoll_fd);
 
-        strv_free(m->controllers);
-        strv_free(m->reset_controllers);
+        if (m->reserve_vt_fd >= 0)
+                close_nointr_nofail(m->reserve_vt_fd);
+
+        if (m->idle_action_fd >= 0)
+                close_nointr_nofail(m->idle_action_fd);
+
         strv_free(m->kill_only_users);
         strv_free(m->kill_exclude_users);
 
-        free(m->cgroup_path);
+        free(m->action_job);
         free(m);
 }
 
@@ -192,7 +232,7 @@ int manager_add_seat(Manager *m, const char *id, Seat **_seat) {
         return 0;
 }
 
-int manager_add_session(Manager *m, User *u, const char *id, Session **_session) {
+int manager_add_session(Manager *m, const char *id, Session **_session) {
         Session *s;
 
         assert(m);
@@ -206,7 +246,7 @@ int manager_add_session(Manager *m, User *u, const char *id, Session **_session)
                 return 0;
         }
 
-        s = session_new(m, u, id);
+        s = session_new(m, id);
         if (!s)
                 return -ENOMEM;
 
@@ -248,7 +288,7 @@ int manager_add_user_by_name(Manager *m, const char *name, User **_user) {
         assert(m);
         assert(name);
 
-        r = get_user_creds(&name, &uid, &gid, NULL);
+        r = get_user_creds(&name, &uid, &gid, NULL, NULL);
         if (r < 0)
                 return r;
 
@@ -268,6 +308,54 @@ int manager_add_user_by_uid(Manager *m, uid_t uid, User **_user) {
         return manager_add_user(m, uid, p->pw_gid, p->pw_name, _user);
 }
 
+int manager_add_inhibitor(Manager *m, const char* id, Inhibitor **_inhibitor) {
+        Inhibitor *i;
+
+        assert(m);
+        assert(id);
+
+        i = hashmap_get(m->inhibitors, id);
+        if (i) {
+                if (_inhibitor)
+                        *_inhibitor = i;
+
+                return 0;
+        }
+
+        i = inhibitor_new(m, id);
+        if (!i)
+                return -ENOMEM;
+
+        if (_inhibitor)
+                *_inhibitor = i;
+
+        return 0;
+}
+
+int manager_add_button(Manager *m, const char *name, Button **_button) {
+        Button *b;
+
+        assert(m);
+        assert(name);
+
+        b = hashmap_get(m->buttons, name);
+        if (b) {
+                if (_button)
+                        *_button = b;
+
+                return 0;
+        }
+
+        b = button_new(m, name);
+        if (!b)
+                return -ENOMEM;
+
+        if (_button)
+                *_button = b;
+
+        return 0;
+}
+
 int manager_process_seat_device(Manager *m, struct udev_device *d) {
         Device *device;
         int r;
@@ -315,6 +403,39 @@ int manager_process_seat_device(Manager *m, struct udev_device *d) {
         return 0;
 }
 
+int manager_process_button_device(Manager *m, struct udev_device *d) {
+        Button *b;
+
+        int r;
+
+        assert(m);
+
+        if (streq_ptr(udev_device_get_action(d), "remove")) {
+
+                b = hashmap_get(m->buttons, udev_device_get_sysname(d));
+                if (!b)
+                        return 0;
+
+                button_free(b);
+
+        } else {
+                const char *sn;
+
+                r = manager_add_button(m, udev_device_get_sysname(d), &b);
+                if (r < 0)
+                        return r;
+
+                sn = udev_device_get_property_value(d, "ID_SEAT");
+                if (isempty(sn))
+                        sn = "seat0";
+
+                button_set_seat(b, sn);
+                button_open(b);
+        }
+
+        return 0;
+}
+
 int manager_enumerate_devices(Manager *m) {
         struct udev_list_entry *item = NULL, *first = NULL;
         struct udev_enumerate *e;
@@ -331,11 +452,65 @@ int manager_enumerate_devices(Manager *m) {
                 goto finish;
         }
 
-        r = udev_enumerate_add_match_subsystem(e, "graphics");
+        r = udev_enumerate_add_match_tag(e, "master-of-seat");
         if (r < 0)
                 goto finish;
 
-        r = udev_enumerate_add_match_tag(e, "seat");
+        r = udev_enumerate_scan_devices(e);
+        if (r < 0)
+                goto finish;
+
+        first = udev_enumerate_get_list_entry(e);
+        udev_list_entry_foreach(item, first) {
+                struct udev_device *d;
+                int k;
+
+                d = udev_device_new_from_syspath(m->udev, udev_list_entry_get_name(item));
+                if (!d) {
+                        r = -ENOMEM;
+                        goto finish;
+                }
+
+                k = manager_process_seat_device(m, d);
+                udev_device_unref(d);
+
+                if (k < 0)
+                        r = k;
+        }
+
+finish:
+        if (e)
+                udev_enumerate_unref(e);
+
+        return r;
+}
+
+int manager_enumerate_buttons(Manager *m) {
+        struct udev_list_entry *item = NULL, *first = NULL;
+        struct udev_enumerate *e;
+        int r;
+
+        assert(m);
+
+        /* Loads buttons from udev */
+
+        if (m->handle_power_key == HANDLE_IGNORE &&
+            m->handle_suspend_key == HANDLE_IGNORE &&
+            m->handle_hibernate_key == HANDLE_IGNORE &&
+            m->handle_lid_switch == HANDLE_IGNORE)
+                return 0;
+
+        e = udev_enumerate_new(m->udev);
+        if (!e) {
+                r = -ENOMEM;
+                goto finish;
+        }
+
+        r = udev_enumerate_add_match_subsystem(e, "input");
+        if (r < 0)
+                goto finish;
+
+        r = udev_enumerate_add_match_tag(e, "power-switch");
         if (r < 0)
                 goto finish;
 
@@ -354,7 +529,7 @@ int manager_enumerate_devices(Manager *m) {
                         goto finish;
                 }
 
-                k = manager_process_seat_device(m, d);
+                k = manager_process_button_device(m, d);
                 udev_device_unref(d);
 
                 if (k < 0)
@@ -369,7 +544,7 @@ finish:
 }
 
 int manager_enumerate_seats(Manager *m) {
-        DIR *d;
+        _cleanup_closedir_ DIR *d = NULL;
         struct dirent *de;
         int r = 0;
 
@@ -388,7 +563,7 @@ int manager_enumerate_seats(Manager *m) {
                 return -errno;
         }
 
-        while ((de = readdir(d))) {
+        FOREACH_DIRENT(de, d, return -errno) {
                 Seat *s;
                 int k;
 
@@ -406,61 +581,16 @@ int manager_enumerate_seats(Manager *m) {
                         r = k;
         }
 
-        closedir(d);
-
-        return r;
-}
-
-static int manager_enumerate_users_from_cgroup(Manager *m) {
-        int r = 0;
-        char *name;
-        DIR *d;
-        int k;
-
-        r = cg_enumerate_subgroups(SYSTEMD_CGROUP_CONTROLLER, m->cgroup_path, &d);
-        if (r < 0) {
-                if (r == -ENOENT)
-                        return 0;
-
-                log_error("Failed to open %s: %s", m->cgroup_path, strerror(-r));
-                return r;
-        }
-
-        while ((k = cg_read_subgroup(d, &name)) > 0) {
-                User *user;
-
-                k = manager_add_user_by_name(m, name, &user);
-                if (k < 0) {
-                        free(name);
-                        r = k;
-                        continue;
-                }
-
-                user_add_to_gc_queue(user);
-
-                if (!user->cgroup_path)
-                        if (asprintf(&user->cgroup_path, "%s/%s", m->cgroup_path, name) < 0) {
-                                r = -ENOMEM;
-                                free(name);
-                                break;
-                        }
-
-                free(name);
-        }
-
-        if (r >= 0 && k < 0)
-                r = k;
-
-        closedir(d);
-
         return r;
 }
 
 static int manager_enumerate_linger_users(Manager *m) {
-        DIR *d;
+        _cleanup_closedir_ DIR *d = NULL;
         struct dirent *de;
         int r = 0;
 
+        assert(m);
+
         d = opendir("/var/lib/systemd/linger");
         if (!d) {
                 if (errno == ENOENT)
@@ -470,7 +600,7 @@ static int manager_enumerate_linger_users(Manager *m) {
                 return -errno;
         }
 
-        while ((de = readdir(d))) {
+        FOREACH_DIRENT(de, d, return -errno) {
                 int k;
 
                 if (!dirent_is_file(de))
@@ -483,27 +613,20 @@ static int manager_enumerate_linger_users(Manager *m) {
                 }
         }
 
-        closedir(d);
-
         return r;
 }
 
 int manager_enumerate_users(Manager *m) {
-        DIR *d;
+        _cleanup_closedir_ DIR *d = NULL;
         struct dirent *de;
         int r, k;
 
         assert(m);
 
-        /* First, enumerate user cgroups */
-        r = manager_enumerate_users_from_cgroup(m);
+        /* Add lingering users */
+        r = manager_enumerate_linger_users(m);
 
-        /* Second, add lingering users on top */
-        k = manager_enumerate_linger_users(m);
-        if (k < 0)
-                r = k;
-
-        /* Third, read in user data stored on disk */
+        /* Read in user data stored on disk */
         d = opendir("/run/systemd/users");
         if (!d) {
                 if (errno == ENOENT)
@@ -513,84 +636,65 @@ int manager_enumerate_users(Manager *m) {
                 return -errno;
         }
 
-        while ((de = readdir(d))) {
-                uid_t uid;
+        FOREACH_DIRENT(de, d, return -errno) {
                 User *u;
 
                 if (!dirent_is_file(de))
                         continue;
 
-                k = parse_uid(de->d_name, &uid);
+                k = manager_add_user_by_name(m, de->d_name, &u);
                 if (k < 0) {
-                        log_error("Failed to parse file name %s: %s", de->d_name, strerror(-k));
-                        continue;
-                }
+                        log_error("Failed to add user by file name %s: %s", de->d_name, strerror(-k));
 
-                u = hashmap_get(m->users, ULONG_TO_PTR(uid));
-                if (!u) {
-                        unlinkat(dirfd(d), de->d_name, 0);
+                        r = k;
                         continue;
                 }
 
+                user_add_to_gc_queue(u);
+
                 k = user_load(u);
                 if (k < 0)
                         r = k;
         }
 
-        closedir(d);
-
         return r;
 }
 
-static int manager_enumerate_sessions_from_cgroup(Manager *m) {
-        User *u;
-        Iterator i;
+int manager_enumerate_sessions(Manager *m) {
+        _cleanup_closedir_ DIR *d = NULL;
+        struct dirent *de;
         int r = 0;
 
-        HASHMAP_FOREACH(u, m->users, i) {
-                DIR *d;
-                char *name;
+        assert(m);
+
+        /* Read in session data stored on disk */
+        d = opendir("/run/systemd/sessions");
+        if (!d) {
+                if (errno == ENOENT)
+                        return 0;
+
+                log_error("Failed to open /run/systemd/sessions: %m");
+                return -errno;
+        }
+
+        FOREACH_DIRENT(de, d, return -errno) {
+                struct Session *s;
                 int k;
 
-                if (!u->cgroup_path)
+                if (!dirent_is_file(de))
                         continue;
 
-                k = cg_enumerate_subgroups(SYSTEMD_CGROUP_CONTROLLER, u->cgroup_path, &d);
+                k = manager_add_session(m, de->d_name, &s);
                 if (k < 0) {
-                        if (k == -ENOENT)
-                                continue;
+                        log_error("Failed to add session by file name %s: %s", de->d_name, strerror(-k));
 
-                        log_error("Failed to open %s: %s", u->cgroup_path, strerror(-k));
                         r = k;
                         continue;
                 }
 
-                while ((k = cg_read_subgroup(d, &name)) > 0) {
-                        Session *session;
-
-                        if (streq(name, "shared"))
-                                continue;
-
-                        k = manager_add_session(m, u, name, &session);
-                        if (k < 0) {
-                                free(name);
-                                break;
-                        }
-
-                        session_add_to_gc_queue(session);
-
-                        if (!session->cgroup_path)
-                                if (asprintf(&session->cgroup_path, "%s/%s", u->cgroup_path, name) < 0) {
-                                        k = -ENOMEM;
-                                        free(name);
-                                        break;
-                                }
-
-                        free(name);
-                }
-
-                closedir(d);
+                session_add_to_gc_queue(s);
 
+                k = session_load(s);
                 if (k < 0)
                         r = k;
         }
@@ -598,46 +702,41 @@ static int manager_enumerate_sessions_from_cgroup(Manager *m) {
         return r;
 }
 
-int manager_enumerate_sessions(Manager *m) {
-        DIR *d;
+int manager_enumerate_inhibitors(Manager *m) {
+        _cleanup_closedir_ DIR *d = NULL;
         struct dirent *de;
         int r = 0;
 
         assert(m);
 
-        /* First enumerate session cgroups */
-        r = manager_enumerate_sessions_from_cgroup(m);
-
-        /* Second, read in session data stored on disk */
-        d = opendir("/run/systemd/sessions");
+        d = opendir("/run/systemd/inhibit");
         if (!d) {
                 if (errno == ENOENT)
                         return 0;
 
-                log_error("Failed to open /run/systemd/sessions: %m");
+                log_error("Failed to open /run/systemd/inhibit: %m");
                 return -errno;
         }
 
-        while ((de = readdir(d))) {
-                struct Session *s;
+        FOREACH_DIRENT(de, d, return -errno) {
                 int k;
+                Inhibitor *i;
 
                 if (!dirent_is_file(de))
                         continue;
 
-                s = hashmap_get(m->sessions, de->d_name);
-                if (!s) {
-                        unlinkat(dirfd(d), de->d_name, 0);
+                k = manager_add_inhibitor(m, de->d_name, &i);
+                if (k < 0) {
+                        log_notice("Couldn't add inhibitor %s: %s", de->d_name, strerror(-k));
+                        r = k;
                         continue;
                 }
 
-                k = session_load(s);
+                k = inhibitor_load(i);
                 if (k < 0)
                         r = k;
         }
 
-        closedir(d);
-
         return r;
 }
 
@@ -681,6 +780,22 @@ int manager_dispatch_vcsa_udev(Manager *m) {
         return r;
 }
 
+int manager_dispatch_button_udev(Manager *m) {
+        struct udev_device *d;
+        int r;
+
+        assert(m);
+
+        d = udev_monitor_receive_device(m->udev_button_monitor);
+        if (!d)
+                return -ENOMEM;
+
+        r = manager_process_button_device(m, d);
+        udev_device_unref(d);
+
+        return r;
+}
+
 int manager_dispatch_console(Manager *m) {
         assert(m);
 
@@ -718,30 +833,26 @@ static int vt_is_busy(int vtnr) {
 
 int manager_spawn_autovt(Manager *m, int vtnr) {
         int r;
-        DBusMessage *message = NULL, *reply = NULL;
         char *name = NULL;
         const char *mode = "fail";
-        DBusError error;
 
         assert(m);
         assert(vtnr >= 1);
 
-        dbus_error_init(&error);
-
-        if ((unsigned) vtnr > m->n_autovts)
+        if ((unsigned) vtnr > m->n_autovts &&
+            (unsigned) vtnr != m->reserve_vt)
                 return 0;
 
-        r = vt_is_busy(vtnr);
-        if (r < 0)
-                return r;
-        else if (r > 0)
-                return -EBUSY;
+        if ((unsigned) vtnr != m->reserve_vt) {
+                /* If this is the reserved TTY, we'll start the getty
+                 * on it in any case, but otherwise only if it is not
+                 * busy. */
 
-        message = dbus_message_new_method_call("org.freedesktop.systemd1", "/org/freedesktop/systemd1", "org.freedesktop.systemd1.Manager", "StartUnit");
-        if (!message) {
-                log_error("Could not allocate message.");
-                r = -ENOMEM;
-                goto finish;
+                r = vt_is_busy(vtnr);
+                if (r < 0)
+                        return r;
+                else if (r > 0)
+                        return -EBUSY;
         }
 
         if (asprintf(&name, "autovt@tty%i.service", vtnr) < 0) {
@@ -750,124 +861,130 @@ int manager_spawn_autovt(Manager *m, int vtnr) {
                 goto finish;
         }
 
-        if (!dbus_message_append_args(message,
-                                      DBUS_TYPE_STRING, &name,
-                                      DBUS_TYPE_STRING, &mode,
-                                      DBUS_TYPE_INVALID)) {
-                log_error("Could not attach target and flag information to message.");
-                r = -ENOMEM;
-                goto finish;
-        }
-
-        reply = dbus_connection_send_with_reply_and_block(m->bus, message, -1, &error);
-        if (!reply) {
-                log_error("Failed to start unit: %s", bus_error_message(&error));
-                goto finish;
-        }
-
-        r = 0;
+        r = bus_method_call_with_reply (
+                        m->bus,
+                        "org.freedesktop.systemd1",
+                        "/org/freedesktop/systemd1",
+                        "org.freedesktop.systemd1.Manager",
+                        "StartUnit",
+                        NULL,
+                        NULL,
+                        DBUS_TYPE_STRING, &name,
+                        DBUS_TYPE_STRING, &mode,
+                        DBUS_TYPE_INVALID);
 
 finish:
         free(name);
 
-        if (message)
-                dbus_message_unref(message);
-
-        if (reply)
-                dbus_message_unref(reply);
-
-        dbus_error_free(&error);
-
         return r;
 }
 
-int manager_get_session_by_cgroup(Manager *m, const char *cgroup, Session **session) {
-        Session *s;
-        char *p;
+static int manager_reserve_vt(Manager *m) {
+        _cleanup_free_ char *p = NULL;
 
         assert(m);
-        assert(cgroup);
-        assert(session);
-
-        s = hashmap_get(m->cgroups, cgroup);
-        if (s) {
-                *session = s;
-                return 1;
-        }
-
-        p = strdup(cgroup);
-        if (!p) {
-                log_error("Out of memory.");
-                return -ENOMEM;
-        }
 
-        for (;;) {
-                char *e;
+        if (m->reserve_vt <= 0)
+                return 0;
 
-                e = strrchr(p, '/');
-                if (!e || e == p) {
-                        free(p);
-                        *session = NULL;
-                        return 0;
-                }
+        if (asprintf(&p, "/dev/tty%u", m->reserve_vt) < 0)
+                return log_oom();
 
-                *e = 0;
+        m->reserve_vt_fd = open(p, O_RDWR|O_NOCTTY|O_CLOEXEC|O_NONBLOCK);
+        if (m->reserve_vt_fd < 0) {
 
-                s = hashmap_get(m->cgroups, p);
-                if (s) {
-                        free(p);
-                        *session = s;
-                        return 1;
-                }
+                /* Don't complain on VT-less systems */
+                if (errno != ENOENT)
+                        log_warning("Failed to pin reserved VT: %m");
+                return -errno;
         }
+
+        return 0;
 }
 
 int manager_get_session_by_pid(Manager *m, pid_t pid, Session **session) {
-        char *p;
+        _cleanup_free_ char *unit = NULL;
+        Session *s;
         int r;
 
         assert(m);
         assert(pid >= 1);
         assert(session);
 
-        r = cg_get_by_pid(SYSTEMD_CGROUP_CONTROLLER, pid, &p);
+        r = cg_pid_get_unit(pid, &unit);
         if (r < 0)
                 return r;
 
-        r = manager_get_session_by_cgroup(m, p, session);
-        free(p);
+        s = hashmap_get(m->session_units, unit);
+        if (!s)
+                return 0;
 
-        return r;
+        *session = s;
+        return 1;
 }
 
-void manager_cgroup_notify_empty(Manager *m, const char *cgroup) {
-        Session *s;
+int manager_get_user_by_pid(Manager *m, pid_t pid, User **user) {
+        _cleanup_free_ char *unit = NULL;
+        User *u;
         int r;
 
-        r = manager_get_session_by_cgroup(m, cgroup, &s);
-        if (r <= 0)
-                return;
+        assert(m);
+        assert(pid >= 1);
+        assert(user);
+
+        r = cg_pid_get_slice(pid, &unit);
+        if (r < 0)
+                return r;
+
+        u = hashmap_get(m->user_units, unit);
+        if (!u)
+                return 0;
 
-        session_add_to_gc_queue(s);
+        *user = u;
+        return 1;
 }
 
-static void manager_pipe_notify_eof(Manager *m, int fd) {
+static void manager_dispatch_other(Manager *m, int fd) {
         Session *s;
+        Inhibitor *i;
+        Button *b;
 
         assert_se(m);
         assert_se(fd >= 0);
 
-        assert_se(s = hashmap_get(m->fifo_fds, INT_TO_PTR(fd + 1)));
-        assert(s->fifo_fd == fd);
-        session_remove_fifo(s);
+        s = hashmap_get(m->session_fds, INT_TO_PTR(fd + 1));
+        if (s) {
+                assert(s->fifo_fd == fd);
+                session_remove_fifo(s);
+                session_stop(s);
+                return;
+        }
 
-        session_stop(s);
+        i = hashmap_get(m->inhibitor_fds, INT_TO_PTR(fd + 1));
+        if (i) {
+                assert(i->fifo_fd == fd);
+                inhibitor_stop(i);
+                inhibitor_free(i);
+                return;
+        }
+
+        b = hashmap_get(m->button_fds, INT_TO_PTR(fd + 1));
+        if (b) {
+                assert(b->fd == fd);
+                button_process(b);
+                return;
+        }
+
+        assert_not_reached("Got event for unknown fd");
 }
 
 static int manager_connect_bus(Manager *m) {
         DBusError error;
         int r;
-        struct epoll_event ev;
+        struct epoll_event ev = {
+                .events = EPOLLIN,
+                .data.u32 = FD_BUS,
+        };
 
         assert(m);
         assert(!m->bus);
@@ -887,22 +1004,69 @@ static int manager_connect_bus(Manager *m) {
             !dbus_connection_register_fallback(m->bus, "/org/freedesktop/login1/session", &bus_session_vtable, m) ||
             !dbus_connection_register_fallback(m->bus, "/org/freedesktop/login1/user", &bus_user_vtable, m) ||
             !dbus_connection_add_filter(m->bus, bus_message_filter, m, NULL)) {
-                log_error("Not enough memory");
-                r = -ENOMEM;
+                r = log_oom();
                 goto fail;
         }
 
         dbus_bus_add_match(m->bus,
                            "type='signal',"
-                           "interface='org.freedesktop.systemd1.Agent',"
-                           "member='Released',"
-                           "path='/org/freedesktop/systemd1/agent'",
+                           "sender='org.freedesktop.systemd1',"
+                           "interface='org.freedesktop.systemd1.Manager',"
+                           "member='JobRemoved',"
+                           "path='/org/freedesktop/systemd1'",
+                           &error);
+        if (dbus_error_is_set(&error)) {
+                log_error("Failed to add match for JobRemoved: %s", bus_error_message(&error));
+                dbus_error_free(&error);
+        }
+
+        dbus_bus_add_match(m->bus,
+                           "type='signal',"
+                           "sender='org.freedesktop.systemd1',"
+                           "interface='org.freedesktop.systemd1.Manager',"
+                           "member='UnitRemoved',"
+                           "path='/org/freedesktop/systemd1'",
                            &error);
+        if (dbus_error_is_set(&error)) {
+                log_error("Failed to add match for UnitRemoved: %s", bus_error_message(&error));
+                dbus_error_free(&error);
+        }
 
+        dbus_bus_add_match(m->bus,
+                           "type='signal',"
+                           "sender='org.freedesktop.systemd1',"
+                           "interface='org.freedesktop.DBus.Properties',"
+                           "member='PropertiesChanged'",
+                           &error);
         if (dbus_error_is_set(&error)) {
-                log_error("Failed to register match: %s", bus_error_message(&error));
-                r = -EIO;
-                goto fail;
+                log_error("Failed to add match for PropertiesChanged: %s", bus_error_message(&error));
+                dbus_error_free(&error);
+        }
+
+        dbus_bus_add_match(m->bus,
+                           "type='signal',"
+                           "sender='org.freedesktop.systemd1',"
+                           "interface='org.freedesktop.systemd1.Manager',"
+                           "member='Reloading',"
+                           "path='/org/freedesktop/systemd1'",
+                           &error);
+        if (dbus_error_is_set(&error)) {
+                log_error("Failed to add match for Reloading: %s", bus_error_message(&error));
+                dbus_error_free(&error);
+        }
+
+        r = bus_method_call_with_reply(
+                        m->bus,
+                        "org.freedesktop.systemd1",
+                        "/org/freedesktop/systemd1",
+                        "org.freedesktop.systemd1.Manager",
+                        "Subscribe",
+                        NULL,
+                        &error,
+                        DBUS_TYPE_INVALID);
+        if (r < 0) {
+                log_error("Failed to enable subscription: %s", bus_error(&error, r));
+                dbus_error_free(&error);
         }
 
         r = dbus_bus_request_name(m->bus, "org.freedesktop.login1", DBUS_NAME_FLAG_DO_NOT_QUEUE, &error);
@@ -924,10 +1088,6 @@ static int manager_connect_bus(Manager *m) {
                 goto fail;
         }
 
-        zero(ev);
-        ev.events = EPOLLIN;
-        ev.data.u32 = FD_BUS;
-
         if (epoll_ctl(m->epoll_fd, EPOLL_CTL_ADD, m->bus_fd, &ev) < 0)
                 goto fail;
 
@@ -940,16 +1100,27 @@ fail:
 }
 
 static int manager_connect_console(Manager *m) {
-        struct epoll_event ev;
+        struct epoll_event ev = {
+                .events = 0,
+                .data.u32 = FD_CONSOLE,
+        };
 
         assert(m);
         assert(m->console_active_fd < 0);
 
+        /* On certain architectures (S390 and Xen, and containers),
+           /dev/tty0 does not exist, so don't fail if we can't open
+           it. */
+        if (access("/dev/tty0", F_OK) < 0) {
+                m->console_active_fd = -1;
+                return 0;
+        }
+
         m->console_active_fd = open("/sys/class/tty/tty0/active", O_RDONLY|O_NOCTTY|O_CLOEXEC);
         if (m->console_active_fd < 0) {
 
-                /* On certain architectures (S390 and Xen), /dev/tty0
-                   does not exist, so don't fail if we can't open it.*/
+                /* On some systems the device node /dev/tty0 may exist
+                 * even though /sys/class/tty/tty0 does not. */
                 if (errno == ENOENT)
                         return 0;
 
@@ -957,10 +1128,6 @@ static int manager_connect_console(Manager *m) {
                 return -errno;
         }
 
-        zero(ev);
-        ev.events = 0;
-        ev.data.u32 = FD_CONSOLE;
-
         if (epoll_ctl(m->epoll_fd, EPOLL_CTL_ADD, m->console_active_fd, &ev) < 0)
                 return -errno;
 
@@ -968,22 +1135,22 @@ static int manager_connect_console(Manager *m) {
 }
 
 static int manager_connect_udev(Manager *m) {
-        struct epoll_event ev;
         int r;
+        struct epoll_event ev = {
+                .events = EPOLLIN,
+                .data.u32 = FD_SEAT_UDEV,
+        };
 
         assert(m);
         assert(!m->udev_seat_monitor);
         assert(!m->udev_vcsa_monitor);
+        assert(!m->udev_button_monitor);
 
         m->udev_seat_monitor = udev_monitor_new_from_netlink(m->udev, "udev");
         if (!m->udev_seat_monitor)
                 return -ENOMEM;
 
-        r = udev_monitor_filter_add_match_tag(m->udev_seat_monitor, "seat");
-        if (r < 0)
-                return r;
-
-        r = udev_monitor_filter_add_match_subsystem_devtype(m->udev_seat_monitor, "graphics", NULL);
+        r = udev_monitor_filter_add_match_tag(m->udev_seat_monitor, "master-of-seat");
         if (r < 0)
                 return r;
 
@@ -993,37 +1160,63 @@ static int manager_connect_udev(Manager *m) {
 
         m->udev_seat_fd = udev_monitor_get_fd(m->udev_seat_monitor);
 
-        zero(ev);
-        ev.events = EPOLLIN;
-        ev.data.u32 = FD_SEAT_UDEV;
-
-        /* Don't bother watching VCSA devices, if nobody cares */
-        if (m->n_autovts <= 0 || m->console_active_fd < 0)
-                return 0;
-
         if (epoll_ctl(m->epoll_fd, EPOLL_CTL_ADD, m->udev_seat_fd, &ev) < 0)
                 return -errno;
 
-        m->udev_vcsa_monitor = udev_monitor_new_from_netlink(m->udev, "udev");
-        if (!m->udev_vcsa_monitor)
-                return -ENOMEM;
+        /* Don't watch keys if nobody cares */
+        if (m->handle_power_key != HANDLE_IGNORE ||
+            m->handle_suspend_key != HANDLE_IGNORE ||
+            m->handle_hibernate_key != HANDLE_IGNORE ||
+            m->handle_lid_switch != HANDLE_IGNORE) {
 
-        r = udev_monitor_filter_add_match_subsystem_devtype(m->udev_vcsa_monitor, "vc", NULL);
-        if (r < 0)
-                return r;
+                m->udev_button_monitor = udev_monitor_new_from_netlink(m->udev, "udev");
+                if (!m->udev_button_monitor)
+                        return -ENOMEM;
 
-        r = udev_monitor_enable_receiving(m->udev_vcsa_monitor);
-        if (r < 0)
-                return r;
+                r = udev_monitor_filter_add_match_tag(m->udev_button_monitor, "power-switch");
+                if (r < 0)
+                        return r;
 
-        m->udev_vcsa_fd = udev_monitor_get_fd(m->udev_vcsa_monitor);
+                r = udev_monitor_filter_add_match_subsystem_devtype(m->udev_button_monitor, "input", NULL);
+                if (r < 0)
+                        return r;
 
-        zero(ev);
-        ev.events = EPOLLIN;
-        ev.data.u32 = FD_VCSA_UDEV;
+                r = udev_monitor_enable_receiving(m->udev_button_monitor);
+                if (r < 0)
+                        return r;
 
-        if (epoll_ctl(m->epoll_fd, EPOLL_CTL_ADD, m->udev_vcsa_fd, &ev) < 0)
-                return -errno;
+                m->udev_button_fd = udev_monitor_get_fd(m->udev_button_monitor);
+
+                zero(ev);
+                ev.events = EPOLLIN;
+                ev.data.u32 = FD_BUTTON_UDEV;
+                if (epoll_ctl(m->epoll_fd, EPOLL_CTL_ADD, m->udev_button_fd, &ev) < 0)
+                        return -errno;
+        }
+
+        /* Don't bother watching VCSA devices, if nobody cares */
+        if (m->n_autovts > 0 && m->console_active_fd >= 0) {
+
+                m->udev_vcsa_monitor = udev_monitor_new_from_netlink(m->udev, "udev");
+                if (!m->udev_vcsa_monitor)
+                        return -ENOMEM;
+
+                r = udev_monitor_filter_add_match_subsystem_devtype(m->udev_vcsa_monitor, "vc", NULL);
+                if (r < 0)
+                        return r;
+
+                r = udev_monitor_enable_receiving(m->udev_vcsa_monitor);
+                if (r < 0)
+                        return r;
+
+                m->udev_vcsa_fd = udev_monitor_get_fd(m->udev_vcsa_monitor);
+
+                zero(ev);
+                ev.events = EPOLLIN;
+                ev.data.u32 = FD_VCSA_UDEV;
+                if (epoll_ctl(m->epoll_fd, EPOLL_CTL_ADD, m->udev_vcsa_fd, &ev) < 0)
+                        return -errno;
+        }
 
         return 0;
 }
@@ -1051,6 +1244,7 @@ void manager_gc(Manager *m, bool drop_not_started) {
 
                 if (session_check_gc(session, drop_not_started) == 0) {
                         session_stop(session);
+                        session_finalize(session);
                         session_free(session);
                 }
         }
@@ -1061,6 +1255,7 @@ void manager_gc(Manager *m, bool drop_not_started) {
 
                 if (user_check_gc(user, drop_not_started) == 0) {
                         user_stop(user);
+                        user_finalize(user);
                         user_free(user);
                 }
         }
@@ -1068,12 +1263,14 @@ void manager_gc(Manager *m, bool drop_not_started) {
 
 int manager_get_idle_hint(Manager *m, dual_timestamp *t) {
         Session *s;
-        bool idle_hint = true;
+        bool idle_hint;
         dual_timestamp ts = { 0, 0 };
         Iterator i;
 
         assert(m);
 
+        idle_hint = !manager_is_inhibited(m, INHIBIT_IDLE, INHIBIT_BLOCK, t, false, false, 0);
+
         HASHMAP_FOREACH(s, m->sessions, i) {
                 dual_timestamp k;
                 int ih;
@@ -1103,11 +1300,99 @@ int manager_get_idle_hint(Manager *m, dual_timestamp *t) {
         return idle_hint;
 }
 
+bool manager_shall_kill(Manager *m, const char *user) {
+        assert(m);
+        assert(user);
+
+        if (!m->kill_user_processes)
+                return false;
+
+        if (strv_contains(m->kill_exclude_users, user))
+                return false;
+
+        if (strv_isempty(m->kill_only_users))
+                return true;
+
+        return strv_contains(m->kill_only_users, user);
+}
+
+int manager_dispatch_idle_action(Manager *m) {
+        struct dual_timestamp since;
+        struct itimerspec its = {};
+        int r;
+        usec_t n;
+
+        assert(m);
+
+        if (m->idle_action == HANDLE_IGNORE ||
+            m->idle_action_usec <= 0) {
+                r = 0;
+                goto finish;
+        }
+
+        n = now(CLOCK_MONOTONIC);
+
+        r = manager_get_idle_hint(m, &since);
+        if (r <= 0)
+                /* Not idle. Let's check if after a timeout it might be idle then. */
+                timespec_store(&its.it_value, n + m->idle_action_usec);
+        else {
+                /* Idle! Let's see if it's time to do something, or if
+                 * we shall sleep for longer. */
+
+                if (n >= since.monotonic + m->idle_action_usec &&
+                    (m->idle_action_not_before_usec <= 0 || n >= m->idle_action_not_before_usec + m->idle_action_usec)) {
+                        log_info("System idle. Taking action.");
+
+                        manager_handle_action(m, 0, m->idle_action, false, false);
+                        m->idle_action_not_before_usec = n;
+                }
+
+                timespec_store(&its.it_value, MAX(since.monotonic, m->idle_action_not_before_usec) + m->idle_action_usec);
+        }
+
+        if (m->idle_action_fd < 0) {
+                struct epoll_event ev = {
+                        .events = EPOLLIN,
+                        .data.u32 = FD_IDLE_ACTION,
+                };
+
+                m->idle_action_fd = timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK|TFD_CLOEXEC);
+                if (m->idle_action_fd < 0) {
+                        log_error("Failed to create idle action timer: %m");
+                        r = -errno;
+                        goto finish;
+                }
+
+                if (epoll_ctl(m->epoll_fd, EPOLL_CTL_ADD, m->idle_action_fd, &ev) < 0) {
+                        log_error("Failed to add idle action timer to epoll: %m");
+                        r = -errno;
+                        goto finish;
+                }
+        }
+
+        if (timerfd_settime(m->idle_action_fd, TFD_TIMER_ABSTIME, &its, NULL) < 0) {
+                log_error("Failed to reset timerfd: %m");
+                r = -errno;
+                goto finish;
+        }
+
+        return 0;
+
+finish:
+        if (m->idle_action_fd >= 0) {
+                close_nointr_nofail(m->idle_action_fd);
+                m->idle_action_fd = -1;
+        }
+
+        return r;
+}
 int manager_startup(Manager *m) {
         int r;
         Seat *seat;
         Session *session;
         User *user;
+        Inhibitor *inhibitor;
         Iterator i;
 
         assert(m);
@@ -1138,14 +1423,36 @@ int manager_startup(Manager *m) {
                 return r;
 
         /* Deserialize state */
-        manager_enumerate_devices(m);
-        manager_enumerate_seats(m);
-        manager_enumerate_users(m);
-        manager_enumerate_sessions(m);
+        r = manager_enumerate_devices(m);
+        if (r < 0)
+                log_warning("Device enumeration failed: %s", strerror(-r));
+
+        r = manager_enumerate_seats(m);
+        if (r < 0)
+                log_warning("Seat enumeration failed: %s", strerror(-r));
+
+        r = manager_enumerate_users(m);
+        if (r < 0)
+                log_warning("User enumeration failed: %s", strerror(-r));
+
+        r = manager_enumerate_sessions(m);
+        if (r < 0)
+                log_warning("Session enumeration failed: %s", strerror(-r));
+
+        r = manager_enumerate_inhibitors(m);
+        if (r < 0)
+                log_warning("Inhibitor enumeration failed: %s", strerror(-r));
+
+        r = manager_enumerate_buttons(m);
+        if (r < 0)
+                log_warning("Button enumeration failed: %s", strerror(-r));
 
         /* Remove stale objects before we start them */
         manager_gc(m, false);
 
+        /* Reserve the special reserved VT */
+        manager_reserve_vt(m);
+
         /* And start everything */
         HASHMAP_FOREACH(seat, m->seats, i)
                 seat_start(seat);
@@ -1156,24 +1463,65 @@ int manager_startup(Manager *m) {
         HASHMAP_FOREACH(session, m->sessions, i)
                 session_start(session);
 
+        HASHMAP_FOREACH(inhibitor, m->inhibitors, i)
+                inhibitor_start(inhibitor);
+
+        manager_dispatch_idle_action(m);
+
         return 0;
 }
 
+static int manager_recheck_buttons(Manager *m) {
+        Iterator i;
+        Button *b;
+        int r = 0;
+
+        assert(m);
+
+        HASHMAP_FOREACH(b, m->buttons, i) {
+                int q;
+
+                q = button_recheck(b);
+                if (q > 0)
+                        return 1;
+                if (q < 0)
+                        r = q;
+        }
+
+        return r;
+}
+
 int manager_run(Manager *m) {
         assert(m);
 
         for (;;) {
                 struct epoll_event event;
                 int n;
+                int msec = -1;
 
                 manager_gc(m, true);
 
+                if (manager_dispatch_delayed(m) > 0)
+                        continue;
+
+                if (manager_recheck_buttons(m) > 0)
+                        continue;
+
                 if (dbus_connection_dispatch(m->bus) != DBUS_DISPATCH_COMPLETE)
                         continue;
 
                 manager_gc(m, true);
 
-                n = epoll_wait(m->epoll_fd, &event, 1, -1);
+                if (m->action_what != 0 && !m->action_job) {
+                        usec_t x, y;
+
+                        x = now(CLOCK_MONOTONIC);
+                        y = m->action_timestamp + m->inhibit_delay_max;
+
+                        msec = x >= y ? 0 : (int) ((y - x) / USEC_PER_MSEC);
+                }
+
+                n = epoll_wait(m->epoll_fd, &event, 1, msec);
                 if (n < 0) {
                         if (errno == EINTR || errno == EAGAIN)
                                 continue;
@@ -1182,6 +1530,9 @@ int manager_run(Manager *m) {
                         return -errno;
                 }
 
+                if (n == 0)
+                        continue;
+
                 switch (event.data.u32) {
 
                 case FD_SEAT_UDEV:
@@ -1192,17 +1543,25 @@ int manager_run(Manager *m) {
                         manager_dispatch_vcsa_udev(m);
                         break;
 
+                case FD_BUTTON_UDEV:
+                        manager_dispatch_button_udev(m);
+                        break;
+
                 case FD_CONSOLE:
                         manager_dispatch_console(m);
                         break;
 
+                case FD_IDLE_ACTION:
+                        manager_dispatch_idle_action(m);
+                        break;
+
                 case FD_BUS:
                         bus_loop_dispatch(m->bus_fd);
                         break;
 
                 default:
-                        if (event.data.u32 >= FD_FIFO_BASE)
-                                manager_pipe_notify_eof(m, event.data.u32 - FD_FIFO_BASE);
+                        if (event.data.u32 >= FD_OTHER_BASE)
+                                manager_dispatch_other(m, event.data.u32 - FD_OTHER_BASE);
                 }
         }
 
@@ -1210,13 +1569,12 @@ int manager_run(Manager *m) {
 }
 
 static int manager_parse_config_file(Manager *m) {
-        FILE *f;
-        const char *fn;
+        static const char fn[] = "/etc/systemd/logind.conf";
+        _cleanup_fclose_ FILE *f = NULL;
         int r;
 
         assert(m);
 
-        fn = "/etc/systemd/logind.conf";
         f = fopen(fn, "re");
         if (!f) {
                 if (errno == ENOENT)
@@ -1226,12 +1584,11 @@ static int manager_parse_config_file(Manager *m) {
                 return -errno;
         }
 
-        r = config_parse(fn, f, "Login\0", config_item_perf_lookup, (void*) logind_gperf_lookup, false, m);
+        r = config_parse(NULL, fn, f, "Login\0", config_item_perf_lookup,
+                         (void*) logind_gperf_lookup, false, false, m);
         if (r < 0)
                 log_warning("Failed to parse configuration file: %s", strerror(-r));
 
-        fclose(f);
-
         return r;
 }
 
@@ -1240,6 +1597,7 @@ int main(int argc, char *argv[]) {
         int r;
 
         log_set_target(LOG_TARGET_AUTO);
+        log_set_facility(LOG_AUTH);
         log_parse_environment();
         log_open();
 
@@ -1251,10 +1609,18 @@ int main(int argc, char *argv[]) {
                 goto finish;
         }
 
+        /* Always create the directories people can create inotify
+         * watches in. Note that some applications might check for the
+         * existence of /run/systemd/seats/ to determine whether
+         * logind is available, so please always make sure this check
+         * stays in. */
+        mkdir_label("/run/systemd/seats", 0755);
+        mkdir_label("/run/systemd/users", 0755);
+        mkdir_label("/run/systemd/sessions", 0755);
+
         m = manager_new();
         if (!m) {
-                log_error("Out of memory");
-                r = -ENOMEM;
+                r = log_oom();
                 goto finish;
         }