chiark / gitweb /
logind: be more verbose on errors
[elogind.git] / src / login / logind.c
index 5a39440..9094567 100644 (file)
@@ -75,23 +75,22 @@ Manager *manager_new(void) {
         m->inhibitors = hashmap_new(string_hash_func, string_compare_func);
         m->buttons = hashmap_new(string_hash_func, string_compare_func);
 
-        m->user_cgroups = hashmap_new(string_hash_func, string_compare_func);
-        m->session_cgroups = 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);
 
         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_cgroups || !m->session_cgroups ||
+            !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;
         }
@@ -102,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;
 }
 
@@ -145,8 +139,8 @@ void manager_free(Manager *m) {
         hashmap_free(m->inhibitors);
         hashmap_free(m->buttons);
 
-        hashmap_free(m->user_cgroups);
-        hashmap_free(m->session_cgroups);
+        hashmap_free(m->user_units);
+        hashmap_free(m->session_units);
 
         hashmap_free(m->session_fds);
         hashmap_free(m->inhibitor_fds);
@@ -183,14 +177,10 @@ void manager_free(Manager *m) {
         if (m->idle_action_fd >= 0)
                 close_nointr_nofail(m->idle_action_fd);
 
-        strv_free(m->controllers);
-        strv_free(m->reset_controllers);
         strv_free(m->kill_only_users);
         strv_free(m->kill_exclude_users);
 
         free(m->action_job);
-
-        free(m->cgroup_path);
         free(m);
 }
 
@@ -242,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);
@@ -256,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;
 
@@ -554,7 +544,7 @@ finish:
 }
 
 int manager_enumerate_seats(Manager *m) {
-        DIR *d;
+        _cleanup_closedir_ DIR *d = NULL;
         struct dirent *de;
         int r = 0;
 
@@ -573,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;
 
@@ -591,66 +581,16 @@ int manager_enumerate_seats(Manager *m) {
                         r = k;
         }
 
-        closedir(d);
-
-        return r;
-}
-
-static int manager_enumerate_users_from_cgroup(Manager *m) {
-        _cleanup_closedir_ DIR *d = NULL;
-        int r = 0, k;
-        char *name;
-
-        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;
-                char *e;
-
-                e = endswith(name, ".user");
-                if (e) {
-                        *e = 0;
-
-                        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) {
-                                user->cgroup_path = strjoin(m->cgroup_path, "/", name, NULL);
-                                if (!user->cgroup_path) {
-                                        k = log_oom();
-                                        free(name);
-                                        break;
-                                }
-                        }
-                }
-
-                free(name);
-        }
-
-        if (k < 0)
-                r = k;
-
         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)
@@ -660,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))
@@ -673,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);
-
-        /* Second, add lingering users on top */
-        k = manager_enumerate_linger_users(m);
-        if (k < 0)
-                r = k;
+        /* Add lingering users */
+        r = manager_enumerate_linger_users(m);
 
-        /* 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)
@@ -703,88 +636,23 @@ 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);
-                if (k < 0) {
-                        log_error("Failed to parse file name %s: %s", de->d_name, strerror(-k));
-                        continue;
-                }
-
-                u = hashmap_get(m->users, ULONG_TO_PTR(uid));
-                if (!u) {
-                        unlinkat(dirfd(d), de->d_name, 0);
-                        continue;
-                }
-
-                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 r = 0;
-
-        HASHMAP_FOREACH(u, m->users, i) {
-                _cleanup_closedir_ DIR *d = NULL;
-                char *name;
-                int k;
-
-                if (!u->cgroup_path)
-                        continue;
-
-                k = cg_enumerate_subgroups(SYSTEMD_CGROUP_CONTROLLER, u->cgroup_path, &d);
+                k = manager_add_user_by_name(m, de->d_name, &u);
                 if (k < 0) {
-                        if (k == -ENOENT)
-                                continue;
+                        log_error("Failed to add user 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;
-                        char *e;
-
-                        e = endswith(name, ".session");
-                        if (e) {
-                                *e = 0;
-
-                                k = manager_add_session(m, u, name, &session);
-                                if (k < 0) {
-                                        free(name);
-                                        r = k;
-                                        continue;
-                                }
-
-                                session_add_to_gc_queue(session);
-
-                                if (!session->cgroup_path) {
-                                        session->cgroup_path = strjoin(m->cgroup_path, "/", name, NULL);
-                                        if (!session->cgroup_path) {
-                                                k = log_oom();
-                                                free(name);
-                                                break;
-                                        }
-                                }
-                        }
-
-                        free(name);
-                }
+                user_add_to_gc_queue(u);
 
+                k = user_load(u);
                 if (k < 0)
                         r = k;
         }
@@ -793,16 +661,13 @@ static int manager_enumerate_sessions_from_cgroup(Manager *m) {
 }
 
 int manager_enumerate_sessions(Manager *m) {
-        DIR *d;
+        _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 */
+        /* Read in session data stored on disk */
         d = opendir("/run/systemd/sessions");
         if (!d) {
                 if (errno == ENOENT)
@@ -812,31 +677,33 @@ int manager_enumerate_sessions(Manager *m) {
                 return -errno;
         }
 
-        while ((de = readdir(d))) {
+        FOREACH_DIRENT(de, d, return -errno) {
                 struct Session *s;
                 int k;
 
                 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_session(m, de->d_name, &s);
+                if (k < 0) {
+                        log_error("Failed to add session by file name %s: %s", de->d_name, strerror(-k));
+
+                        r = k;
                         continue;
                 }
 
+                session_add_to_gc_queue(s);
+
                 k = session_load(s);
                 if (k < 0)
                         r = k;
         }
 
-        closedir(d);
-
         return r;
 }
 
 int manager_enumerate_inhibitors(Manager *m) {
-        DIR *d;
+        _cleanup_closedir_ DIR *d = NULL;
         struct dirent *de;
         int r = 0;
 
@@ -851,7 +718,7 @@ int manager_enumerate_inhibitors(Manager *m) {
                 return -errno;
         }
 
-        while ((de = readdir(d))) {
+        FOREACH_DIRENT(de, d, return -errno) {
                 int k;
                 Inhibitor *i;
 
@@ -870,8 +737,6 @@ int manager_enumerate_inhibitors(Manager *m) {
                         r = k;
         }
 
-        closedir(d);
-
         return r;
 }
 
@@ -1037,105 +902,46 @@ static int manager_reserve_vt(Manager *m) {
         return 0;
 }
 
-int manager_get_session_by_cgroup(Manager *m, const char *cgroup, Session **session) {
+int manager_get_session_by_pid(Manager *m, pid_t pid, Session **session) {
+        _cleanup_free_ char *unit = NULL;
         Session *s;
-        char *p;
+        int r;
 
         assert(m);
-        assert(cgroup);
+        assert(pid >= 1);
         assert(session);
 
-        s = hashmap_get(m->session_cgroups, cgroup);
-        if (s) {
-                *session = s;
-                return 1;
-        }
-
-        p = strdupa(cgroup);
-
-        for (;;) {
-                char *e;
-
-                e = strrchr(p, '/');
-                if (!e || e == p) {
-                        *session = NULL;
-                        return 0;
-                }
+        r = cg_pid_get_unit(pid, &unit);
+        if (r < 0)
+                return r;
 
-                *e = 0;
+        s = hashmap_get(m->session_units, unit);
+        if (!s)
+                return 0;
 
-                s = hashmap_get(m->session_cgroups, p);
-                if (s) {
-                        *session = s;
-                        return 1;
-                }
-        }
+        *session = s;
+        return 1;
 }
 
-int manager_get_user_by_cgroup(Manager *m, const char *cgroup, User **user) {
+int manager_get_user_by_pid(Manager *m, pid_t pid, User **user) {
+        _cleanup_free_ char *unit = NULL;
         User *u;
-        char *p;
-
-        assert(m);
-        assert(cgroup);
-        assert(user);
-
-        u = hashmap_get(m->user_cgroups, cgroup);
-        if (u) {
-                *user = u;
-                return 1;
-        }
-
-        p = strdupa(cgroup);
-        if (!p)
-                return log_oom();
-
-        for (;;) {
-                char *e;
-
-                e = strrchr(p, '/');
-                if (!e || e == p) {
-                        *user = NULL;
-                        return 0;
-                }
-
-                *e = 0;
-
-                u = hashmap_get(m->user_cgroups, p);
-                if (u) {
-                        *user = u;
-                        return 1;
-                }
-        }
-}
-
-int manager_get_session_by_pid(Manager *m, pid_t pid, Session **session) {
-        _cleanup_free_ char *p = NULL;
         int r;
 
         assert(m);
         assert(pid >= 1);
-        assert(session);
+        assert(user);
 
-        r = cg_pid_get_path(SYSTEMD_CGROUP_CONTROLLER, pid, &p);
+        r = cg_pid_get_slice(pid, &unit);
         if (r < 0)
                 return r;
 
-        return manager_get_session_by_cgroup(m, p, session);
-}
-
-void manager_cgroup_notify_empty(Manager *m, const char *cgroup) {
-        Session *s;
-        User *u;
-        int r;
-
-        r = manager_get_session_by_cgroup(m, cgroup, &s);
-        if (r > 0)
-                session_add_to_gc_queue(s);
+        u = hashmap_get(m->user_units, unit);
+        if (!u)
+                return 0;
 
-        r = manager_get_user_by_cgroup(m, cgroup, &u);
-        if (r > 0)
-                user_add_to_gc_queue(u);
+        *user = u;
+        return 1;
 }
 
 static void manager_dispatch_other(Manager *m, int fd) {
@@ -1204,15 +1010,63 @@ static int manager_connect_bus(Manager *m) {
 
         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);
@@ -1390,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);
                 }
         }
@@ -1400,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);
                 }
         }
@@ -1444,6 +1300,22 @@ 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 = {};
@@ -1526,9 +1398,6 @@ int manager_startup(Manager *m) {
         assert(m);
         assert(m->epoll_fd <= 0);
 
-        cg_shorten_controllers(m->reset_controllers);
-        cg_shorten_controllers(m->controllers);
-
         m->epoll_fd = epoll_create1(EPOLL_CLOEXEC);
         if (m->epoll_fd < 0)
                 return -errno;
@@ -1554,12 +1423,29 @@ 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);
-        manager_enumerate_inhibitors(m);
-        manager_enumerate_buttons(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);