chiark / gitweb /
Classify processes from sessions into cgroups
authorAndy Wingo <wingo@pobox.com>
Sun, 6 Mar 2016 15:02:31 +0000 (16:02 +0100)
committerAndy Wingo <wingo@pobox.com>
Sun, 6 Mar 2016 15:02:31 +0000 (16:02 +0100)
Create a private cgroup tree associated with no controllers, and use it
to map PIDs to sessions.  Since we use our own path structure, remove
internal cgroup-related helpers that interpret the cgroup path structure
to pull out users, slices, and scopes.

README
src/cgroups-agent/cgroups-agent.c
src/libelogind/sd-bus/bus-creds.c
src/libelogind/sd-bus/sd-bus.c
src/libelogind/sd-login/sd-login.c
src/login/logind-core.c
src/login/logind-session.c
src/login/logind-user.c
src/login/logind.c
src/shared/cgroup-util.c
src/shared/cgroup-util.h

diff --git a/README b/README
index 928bd7d0c4447d6b3ed1a98cf2cf311069867d4d..80e1ab9dcc0f6fb2ed48e26355e38ba937cb492f 100644 (file)
--- a/README
+++ b/README
@@ -29,7 +29,7 @@ provide a subset of the interfaces of systemd 219.
 
 To contribute to elogind, fork the current source code from github:
 
 
 To contribute to elogind, fork the current source code from github:
 
-  https://github.com/andywingo/elogind
+  https://github.com/wingo/elogind
 
 Send a pull request for the changes you like.
 
 
 Send a pull request for the changes you like.
 
@@ -39,7 +39,7 @@ To chat about elogind:
 
 Finally, bug reports:
 
 
 Finally, bug reports:
 
-  https://github.com/andywingo/elogind/issues
+  https://github.com/wingo/elogind/issues
 
 Why bother?
 -----------
 
 Why bother?
 -----------
@@ -77,21 +77,26 @@ of <systemd/sd-login.h>.
 Libelogind just implements login-related functionality.  It also
 provides the sd-bus API.
 
 Libelogind just implements login-related functionality.  It also
 provides the sd-bus API.
 
-Unlike systemd, whose logind arranges to run user sessions in cgroups
-via RPC calls to systemd, in elogind there is no systemd so there are
-no cgroups.  This has a few implications:
+Unlike systemd, whose logind arranges to manage resources for user
+sessions via RPC calls to systemd, in elogind there is no systemd so
+there is no global cgroup-based resource management.  This has a few
+implications:
 
   * Elogind does not create "slices" for users.  Elogind will not
     record that users are associated with slices.
 
 
   * Elogind does not create "slices" for users.  Elogind will not
     record that users are associated with slices.
 
-  * Systemd's logind waits for all user jobs to stop before recording
-    that a user's session has gone away.  Since we have no cgroups,
-    elogind just removes the session directly when pam_elogind.so
-    indicates the user has logged out.
-
   * The /run/systemd/slices directory will always be empty.
 
   * The /run/systemd/slices directory will always be empty.
 
-  * Support for lingering is not so great.
+  * Elogind does not have the concept of a "scope", internally, as
+    it's the same as a session.  Any API that refers to scopes will
+    always return an error code.
+
+On the other hand, elogind does use a similar strategy to systemd in
+that it places processes in a private cgroup for organizational
+purposes, without installing any controllers (see
+http://0pointer.de/blog/projects/cgroups-vs-cgroups.html).  This
+allows elogind to map arbitrary processes to sessions, even if the
+process does the usual double-fork to be reparented to PID 1.
 
 Elogind does not manage virtual terminals.
 
 
 Elogind does not manage virtual terminals.
 
index 529e8430303207458f15f9074ac9492b4fd1d808..3be4cff80199088d232b0f70d7dbceb88c5cf633 100644 (file)
@@ -38,17 +38,13 @@ int main(int argc, char *argv[]) {
         log_parse_environment();
         log_open();
 
         log_parse_environment();
         log_open();
 
-        /* We send this event to the private D-Bus socket and then the
-         * system instance will forward this to the system bus. We do
-         * this to avoid an activation loop when we start dbus when we
-         * are called when the dbus service is shut down. */
-
-        r = bus_open_system_systemd(&bus);
+        /* Unlike in systemd where this has to use a private socket,
+           since logind doesn't associate control groups with services
+           and doesn't manage the dbus service, we can just use the
+           system bus.  */
+        r = sd_bus_open_system(&bus);
         if (r < 0) {
         if (r < 0) {
-                /* If we couldn't connect we assume this was triggered
-                 * while systemd got restarted/transitioned from
-                 * initrd to the system, so let's ignore this */
-                log_debug_errno(r, "Failed to get D-Bus connection: %m");
+                log_debug_errno(r, "Failed to open system bus: %m");
                 return EXIT_FAILURE;
         }
 
                 return EXIT_FAILURE;
         }
 
@@ -58,7 +54,7 @@ int main(int argc, char *argv[]) {
                                "Released",
                                "s", argv[1]);
         if (r < 0) {
                                "Released",
                                "s", argv[1]);
         if (r < 0) {
-                log_debug_errno(r, "Failed to send signal message on private connection: %m");
+                log_debug_errno(r, "Failed to send signal message: %m");
                 return EXIT_FAILURE;
         }
 
                 return EXIT_FAILURE;
         }
 
index cd1c5e232ad07a35301d1302623605a658801761..aea379d669b9c9e905b77ace228291457aa81401 100644 (file)
@@ -358,8 +358,6 @@ _public_ int sd_bus_creds_get_cgroup(sd_bus_creds *c, const char **ret) {
 }
 
 _public_ int sd_bus_creds_get_unit(sd_bus_creds *c, const char **ret) {
 }
 
 _public_ int sd_bus_creds_get_unit(sd_bus_creds *c, const char **ret) {
-        int r;
-
         assert_return(c, -EINVAL);
         assert_return(ret, -EINVAL);
 
         assert_return(c, -EINVAL);
         assert_return(ret, -EINVAL);
 
@@ -368,25 +366,14 @@ _public_ int sd_bus_creds_get_unit(sd_bus_creds *c, const char **ret) {
 
         assert(c->cgroup);
 
 
         assert(c->cgroup);
 
-        if (!c->unit) {
-                const char *shifted;
-
-                r = cg_shift_path(c->cgroup, c->cgroup_root, &shifted);
-                if (r < 0)
-                        return r;
-
-                r = cg_path_get_unit(shifted, (char**) &c->unit);
-                if (r < 0)
-                        return r;
-        }
+        if (!c->unit)
+                return -ESRCH;
 
         *ret = c->unit;
         return 0;
 }
 
 _public_ int sd_bus_creds_get_user_unit(sd_bus_creds *c, const char **ret) {
 
         *ret = c->unit;
         return 0;
 }
 
 _public_ int sd_bus_creds_get_user_unit(sd_bus_creds *c, const char **ret) {
-        int r;
-
         assert_return(c, -EINVAL);
         assert_return(ret, -EINVAL);
 
         assert_return(c, -EINVAL);
         assert_return(ret, -EINVAL);
 
@@ -395,25 +382,14 @@ _public_ int sd_bus_creds_get_user_unit(sd_bus_creds *c, const char **ret) {
 
         assert(c->cgroup);
 
 
         assert(c->cgroup);
 
-        if (!c->user_unit) {
-                const char *shifted;
-
-                r = cg_shift_path(c->cgroup, c->cgroup_root, &shifted);
-                if (r < 0)
-                        return r;
-
-                r = cg_path_get_user_unit(shifted, (char**) &c->user_unit);
-                if (r < 0)
-                        return r;
-        }
+        if (!c->user_unit)
+                return -ESRCH;
 
         *ret = c->user_unit;
         return 0;
 }
 
 _public_ int sd_bus_creds_get_slice(sd_bus_creds *c, const char **ret) {
 
         *ret = c->user_unit;
         return 0;
 }
 
 _public_ int sd_bus_creds_get_slice(sd_bus_creds *c, const char **ret) {
-        int r;
-
         assert_return(c, -EINVAL);
         assert_return(ret, -EINVAL);
 
         assert_return(c, -EINVAL);
         assert_return(ret, -EINVAL);
 
@@ -422,17 +398,8 @@ _public_ int sd_bus_creds_get_slice(sd_bus_creds *c, const char **ret) {
 
         assert(c->cgroup);
 
 
         assert(c->cgroup);
 
-        if (!c->slice) {
-                const char *shifted;
-
-                r = cg_shift_path(c->cgroup, c->cgroup_root, &shifted);
-                if (r < 0)
-                        return r;
-
-                r = cg_path_get_slice(shifted, (char**) &c->slice);
-                if (r < 0)
-                        return r;
-        }
+        if (!c->slice)
+                return -ESRCH;
 
         *ret = c->slice;
         return 0;
 
         *ret = c->slice;
         return 0;
@@ -481,7 +448,7 @@ _public_ int sd_bus_creds_get_owner_uid(sd_bus_creds *c, uid_t *uid) {
         if (r < 0)
                 return r;
 
         if (r < 0)
                 return r;
 
-        return cg_path_get_owner_uid(shifted, uid);
+        return -ESRCH;
 }
 
 _public_ int sd_bus_creds_get_cmdline(sd_bus_creds *c, char ***cmdline) {
 }
 
 _public_ int sd_bus_creds_get_cmdline(sd_bus_creds *c, char ***cmdline) {
index 2cb1aafa2b147914650bf6135413227682764100..35de5b051e93dee908a56717eb27a5a95a04800b 100644 (file)
@@ -1124,10 +1124,7 @@ _public_ int sd_bus_open(sd_bus **ret) {
 
         e = secure_getenv("DBUS_STARTER_ADDRESS");
         if (!e) {
 
         e = secure_getenv("DBUS_STARTER_ADDRESS");
         if (!e) {
-                if (cg_pid_get_owner_uid(0, NULL) >= 0)
-                        return sd_bus_open_user(ret);
-                else
-                        return sd_bus_open_system(ret);
+                return sd_bus_open_system(ret);
         }
 
         r = sd_bus_new(&b);
         }
 
         r = sd_bus_new(&b);
@@ -3327,10 +3324,7 @@ _public_ int sd_bus_default(sd_bus **ret) {
         /* Finally, if nothing is set use the cached connection for
          * the right scope */
 
         /* Finally, if nothing is set use the cached connection for
          * the right scope */
 
-        if (cg_pid_get_owner_uid(0, NULL) >= 0)
-                return sd_bus_default_user(ret);
-        else
-                return sd_bus_default_system(ret);
+        return sd_bus_default_system(ret);
 }
 
 _public_ int sd_bus_get_tid(sd_bus *b, pid_t *tid) {
 }
 
 _public_ int sd_bus_get_tid(sd_bus *b, pid_t *tid) {
index cc0677bdf21c94a72e9ea36d1ac76aebd699f6f4..8e2b7c1dc7c239c2cdd546ab76c1713f38e0cef2 100644 (file)
@@ -46,7 +46,7 @@ _public_ int sd_pid_get_unit(pid_t pid, char **unit) {
         assert_return(pid >= 0, -EINVAL);
         assert_return(unit, -EINVAL);
 
         assert_return(pid >= 0, -EINVAL);
         assert_return(unit, -EINVAL);
 
-        return cg_pid_get_unit(pid, unit);
+        return -ESRCH;
 }
 
 _public_ int sd_pid_get_user_unit(pid_t pid, char **unit) {
 }
 
 _public_ int sd_pid_get_user_unit(pid_t pid, char **unit) {
@@ -54,7 +54,7 @@ _public_ int sd_pid_get_user_unit(pid_t pid, char **unit) {
         assert_return(pid >= 0, -EINVAL);
         assert_return(unit, -EINVAL);
 
         assert_return(pid >= 0, -EINVAL);
         assert_return(unit, -EINVAL);
 
-        return cg_pid_get_user_unit(pid, unit);
+        return -ESRCH;
 }
 
 _public_ int sd_pid_get_machine_name(pid_t pid, char **name) {
 }
 
 _public_ int sd_pid_get_machine_name(pid_t pid, char **name) {
@@ -62,7 +62,7 @@ _public_ int sd_pid_get_machine_name(pid_t pid, char **name) {
         assert_return(pid >= 0, -EINVAL);
         assert_return(name, -EINVAL);
 
         assert_return(pid >= 0, -EINVAL);
         assert_return(name, -EINVAL);
 
-        return cg_pid_get_machine_name(pid, name);
+        return -ESRCH;
 }
 
 _public_ int sd_pid_get_slice(pid_t pid, char **slice) {
 }
 
 _public_ int sd_pid_get_slice(pid_t pid, char **slice) {
@@ -70,7 +70,7 @@ _public_ int sd_pid_get_slice(pid_t pid, char **slice) {
         assert_return(pid >= 0, -EINVAL);
         assert_return(slice, -EINVAL);
 
         assert_return(pid >= 0, -EINVAL);
         assert_return(slice, -EINVAL);
 
-        return cg_pid_get_slice(pid, slice);
+        return -ESRCH;
 }
 
 _public_ int sd_pid_get_owner_uid(pid_t pid, uid_t *uid) {
 }
 
 _public_ int sd_pid_get_owner_uid(pid_t pid, uid_t *uid) {
@@ -78,7 +78,7 @@ _public_ int sd_pid_get_owner_uid(pid_t pid, uid_t *uid) {
         assert_return(pid >= 0, -EINVAL);
         assert_return(uid, -EINVAL);
 
         assert_return(pid >= 0, -EINVAL);
         assert_return(uid, -EINVAL);
 
-        return cg_pid_get_owner_uid(pid, uid);
+        return -ESRCH;
 }
 
 _public_ int sd_peer_get_session(int fd, char **session) {
 }
 
 _public_ int sd_peer_get_session(int fd, char **session) {
@@ -106,7 +106,7 @@ _public_ int sd_peer_get_owner_uid(int fd, uid_t *uid) {
         if (r < 0)
                 return r;
 
         if (r < 0)
                 return r;
 
-        return cg_pid_get_owner_uid(ucred.pid, uid);
+        return -ESRCH;
 }
 
 _public_ int sd_peer_get_unit(int fd, char **unit) {
 }
 
 _public_ int sd_peer_get_unit(int fd, char **unit) {
@@ -120,7 +120,7 @@ _public_ int sd_peer_get_unit(int fd, char **unit) {
         if (r < 0)
                 return r;
 
         if (r < 0)
                 return r;
 
-        return cg_pid_get_unit(ucred.pid, unit);
+        return -ESRCH;
 }
 
 _public_ int sd_peer_get_user_unit(int fd, char **unit) {
 }
 
 _public_ int sd_peer_get_user_unit(int fd, char **unit) {
@@ -134,7 +134,7 @@ _public_ int sd_peer_get_user_unit(int fd, char **unit) {
         if (r < 0)
                 return r;
 
         if (r < 0)
                 return r;
 
-        return cg_pid_get_user_unit(ucred.pid, unit);
+        return -ESRCH;
 }
 
 _public_ int sd_peer_get_machine_name(int fd, char **machine) {
 }
 
 _public_ int sd_peer_get_machine_name(int fd, char **machine) {
@@ -148,7 +148,7 @@ _public_ int sd_peer_get_machine_name(int fd, char **machine) {
         if (r < 0)
                 return r;
 
         if (r < 0)
                 return r;
 
-        return cg_pid_get_machine_name(ucred.pid, machine);
+        return -ESRCH;
 }
 
 _public_ int sd_peer_get_slice(int fd, char **slice) {
 }
 
 _public_ int sd_peer_get_slice(int fd, char **slice) {
@@ -162,7 +162,7 @@ _public_ int sd_peer_get_slice(int fd, char **slice) {
         if (r < 0)
                 return r;
 
         if (r < 0)
                 return r;
 
-        return cg_pid_get_slice(ucred.pid, slice);
+        return -ESRCH;
 }
 
 static int file_of_uid(uid_t uid, char **p) {
 }
 
 static int file_of_uid(uid_t uid, char **p) {
index 67b03623acccb20dd24f11a0a9053403e94bc864..041593c5ab933f673fcda5ff451d521e8f7d0897 100644 (file)
@@ -311,24 +311,41 @@ int manager_process_button_device(Manager *m, struct udev_device *d) {
 }
 
 int manager_get_session_by_pid(Manager *m, pid_t pid, Session **session) {
 }
 
 int manager_get_session_by_pid(Manager *m, pid_t pid, Session **session) {
+        _cleanup_free_ char *session_name = NULL;
+        Session *s;
+        int r;
+
         assert(m);
         assert(session);
 
         assert(m);
         assert(session);
 
-        /* Without cgroups, we have no way to map from pid to
-           session.  */
-        return 0;
+        if (pid < 1)
+                return -EINVAL;
+
+        r = cg_pid_get_session(pid, &session_name);
+        if (r < 0)
+                return 0;
+
+        s = hashmap_get(m->sessions, session_name);
+        if (!s)
+                return 0;
+
+        *session = s;
+        return 1;
 }
 
 int manager_get_user_by_pid(Manager *m, pid_t pid, User **user) {
 }
 
 int manager_get_user_by_pid(Manager *m, pid_t pid, User **user) {
+        Session *s;
+        int r;
+
         assert(m);
         assert(user);
 
         assert(m);
         assert(user);
 
-        if (pid < 1)
-                return -EINVAL;
+        r = manager_get_session_by_pid (m, pid, &s);
+        if (r <= 0)
+                return r;
 
 
-        /* Without cgroups, we have no way to map from pid to
-           user.  */
-        return 0;
+        *user = s->user;
+        return 1;
 }
 
 int manager_get_idle_hint(Manager *m, dual_timestamp *t) {
 }
 
 int manager_get_idle_hint(Manager *m, dual_timestamp *t) {
index 109e3ef133b8c9274461503fe187e5afcfc7dae6..f03c2e565d6f69a6e10cd176ae8a53d00b88535d 100644 (file)
@@ -36,6 +36,8 @@
 #include "audit.h"
 #include "bus-util.h"
 #include "bus-error.h"
 #include "audit.h"
 #include "bus-util.h"
 #include "bus-error.h"
+#include "cgroup-util.h"
+#include "def.h"
 #include "logind-session.h"
 
 static void session_remove_fifo(Session *s);
 #include "logind-session.h"
 
 static void session_remove_fifo(Session *s);
@@ -475,6 +477,25 @@ int session_activate(Session *s) {
         return 0;
 }
 
         return 0;
 }
 
+static int session_start_cgroup(Session *s) {
+        int r;
+
+        assert(s);
+        assert(s->user);
+        assert(s->leader > 0);
+
+        /* First, create our own group */
+        r = cg_create(SYSTEMD_CGROUP_CONTROLLER, s->id);
+        if (r < 0)
+                return log_error_errno(r, "Failed to create cgroup %s: %m", s->id);
+
+        r = cg_attach(SYSTEMD_CGROUP_CONTROLLER, s->id, s->leader);
+        if (r < 0)
+                log_warning_errno(r, "Failed to attach PID %d to cgroup %s: %m", s->leader, s->id);
+
+        return 0;
+}
+
 int session_start(Session *s) {
         int r;
 
 int session_start(Session *s) {
         int r;
 
@@ -490,6 +511,10 @@ int session_start(Session *s) {
         if (r < 0)
                 return r;
 
         if (r < 0)
                 return r;
 
+        r = session_start_cgroup(s);
+        if (r < 0)
+                return r;
+
         log_struct(s->class == SESSION_BACKGROUND ? LOG_DEBUG : LOG_INFO,
                    LOG_MESSAGE_ID(SD_MESSAGE_SESSION_START),
                    "SESSION_ID=%s", s->id,
         log_struct(s->class == SESSION_BACKGROUND ? LOG_DEBUG : LOG_INFO,
                    LOG_MESSAGE_ID(SD_MESSAGE_SESSION_START),
                    "SESSION_ID=%s", s->id,
@@ -527,8 +552,23 @@ int session_start(Session *s) {
         return 0;
 }
 
         return 0;
 }
 
+static int session_stop_cgroup(Session *s, bool force) {
+        _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
+        int r;
+
+        assert(s);
+
+        if (force || manager_shall_kill(s->manager, s->user->name)) {
+                r = session_kill(s, KILL_ALL, SIGTERM);
+                if (r < 0)
+                        return r;
+        }
+
+        return 0;
+}
+
 int session_stop(Session *s, bool force) {
 int session_stop(Session *s, bool force) {
-        int r = 0;
+        int r;
 
         assert(s);
 
 
         assert(s);
 
@@ -540,6 +580,9 @@ int session_stop(Session *s, bool force) {
         /* We are going down, don't care about FIFOs anymore */
         session_remove_fifo(s);
 
         /* We are going down, don't care about FIFOs anymore */
         session_remove_fifo(s);
 
+        /* Kill cgroup */
+        r = session_stop_cgroup(s, force);
+
         s->stopping = true;
 
         user_elect_display(s->user);
         s->stopping = true;
 
         user_elect_display(s->user);
@@ -609,7 +652,7 @@ int session_release(Session *s) {
         /* In systemd, session release is triggered by user jobs
            dying.  In elogind we don't have that so go ahead and stop
            now.  */
         /* In systemd, session release is triggered by user jobs
            dying.  In elogind we don't have that so go ahead and stop
            now.  */
-        session_stop(s, false);
+        return session_stop(s, false);
 }
 
 bool session_is_active(Session *s) {
 }
 
 bool session_is_active(Session *s) {
@@ -814,6 +857,9 @@ bool session_check_gc(Session *s, bool drop_not_started) {
                         return true;
         }
 
                         return true;
         }
 
+        if (cg_is_empty_recursive (SYSTEMD_CGROUP_CONTROLLER, s->id, false) > 0)
+                return true;
+
         return false;
 }
 
         return false;
 }
 
@@ -846,8 +892,23 @@ SessionState session_get_state(Session *s) {
 int session_kill(Session *s, KillWho who, int signo) {
         assert(s);
 
 int session_kill(Session *s, KillWho who, int signo) {
         assert(s);
 
-        /* No way to kill the session without cgroups.  */
-        return -ESRCH;
+        if (who == KILL_LEADER) {
+                if (s->leader <= 0)
+                        return -ESRCH;
+
+                /* FIXME: verify that leader is in cgroup?  */
+
+                if (kill(s->leader, signo) < 0) {
+                        return log_error_errno(errno, "Failed to kill process leader %d for session %s: %m", s->leader, s->id);
+                }
+                return 0;
+        } else {
+                bool sigcont = false;
+                bool ignore_self = true;
+                bool rem = true;
+                return cg_kill_recursive (SYSTEMD_CGROUP_CONTROLLER, s->id, signo,
+                                          sigcont, ignore_self, rem, NULL);
+        }
 }
 
 static int session_open_vt(Session *s) {
 }
 
 static int session_open_vt(Session *s) {
index 539c316ad665cc8f6ec164872cb0c7773498d3ef..0aac3bcf52dc68d2d5890f4900c1d7aaab687661 100644 (file)
@@ -562,10 +562,18 @@ UserState user_get_state(User *u) {
 }
 
 int user_kill(User *u, int signo) {
 }
 
 int user_kill(User *u, int signo) {
+        Session *s;
+        int res = 0;
+
         assert(u);
 
         assert(u);
 
-        /* FIXME: No way to kill a user without systemd.  */
-        return -ESRCH;
+        LIST_FOREACH(sessions_by_user, s, u->sessions) {
+                int r = session_kill(s, KILL_ALL, signo);
+                if (res == 0 && r < 0)
+                        res = r;
+        }
+
+        return res;
 }
 
 void user_elect_display(User *u) {
 }
 
 void user_elect_display(User *u) {
index 49dae036c487e23e36b7e1d5ed69ffe431038e24..8e8fe1915705d2f098fc0f18412943a885777fa9 100644 (file)
@@ -542,6 +542,35 @@ static int manager_dispatch_console(sd_event_source *s, int fd, uint32_t revents
         return 0;
 }
 
         return 0;
 }
 
+static int signal_agent_released(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
+        Manager *m = userdata;
+        Session *s;
+        const char *cgroup;
+        int r;
+
+        assert(bus);
+        assert(message);
+        assert(m);
+
+        r = sd_bus_message_read(message, "s", &cgroup);
+        if (r < 0) {
+                bus_log_parse_error(r);
+                return 0;
+        }
+
+        s = hashmap_get(m->sessions, cgroup);
+
+        if (!s) {
+                log_warning("Session not found: %s", cgroup);
+                return 0;
+        }
+
+        session_finalize(s);
+        session_free(s);
+
+        return 0;
+}
+
 static int manager_connect_bus(Manager *m) {
         _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
         int r;
 static int manager_connect_bus(Manager *m) {
         _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
         int r;
@@ -557,6 +586,13 @@ static int manager_connect_bus(Manager *m) {
         if (r < 0)
                 return log_error_errno(r, "Failed to add manager object vtable: %m");
 
         if (r < 0)
                 return log_error_errno(r, "Failed to add manager object vtable: %m");
 
+        r = sd_bus_add_match(m->bus, NULL,
+                             "type='signal',"
+                             "interface='org.freedesktop.systemd1.Agent',"
+                             "member='Released',"
+                             "path='/org/freedesktop/systemd1/agent'",
+                             signal_agent_released, m);
+
         r = sd_bus_add_fallback_vtable(m->bus, NULL, "/org/freedesktop/login1/seat", "org.freedesktop.login1.Seat", seat_vtable, seat_object_find, m);
         if (r < 0)
                 return log_error_errno(r, "Failed to add seat object vtable: %m");
         r = sd_bus_add_fallback_vtable(m->bus, NULL, "/org/freedesktop/login1/seat", "org.freedesktop.login1.Seat", seat_vtable, seat_object_find, m);
         if (r < 0)
                 return log_error_errno(r, "Failed to add seat object vtable: %m");
index 05c3125aec79ca0ba2fafb2c27eb94ec65ccc085..99d06540a1579df953bd4a88c5d84a242e718beb 100644 (file)
@@ -34,7 +34,6 @@
 #include "macro.h"
 #include "util.h"
 #include "path-util.h"
 #include "macro.h"
 #include "util.h"
 #include "path-util.h"
-#include "unit-name.h"
 #include "fileio.h"
 #include "special.h"
 #include "mkdir.h"
 #include "fileio.h"
 #include "special.h"
 #include "mkdir.h"
@@ -439,7 +438,7 @@ static const char *normalize_controller(const char *controller) {
         assert(controller);
 
         if (streq(controller, SYSTEMD_CGROUP_CONTROLLER))
         assert(controller);
 
         if (streq(controller, SYSTEMD_CGROUP_CONTROLLER))
-                return "systemd";
+                return "elogind";
         else if (startswith(controller, "name="))
                 return controller + 5;
         else
         else if (startswith(controller, "name="))
                 return controller + 5;
         else
@@ -1058,21 +1057,9 @@ int cg_mangle_path(const char *path, char **result) {
 }
 
 int cg_get_root_path(char **path) {
 }
 
 int cg_get_root_path(char **path) {
-        char *p, *e;
-        int r;
-
         assert(path);
 
         assert(path);
 
-        r = cg_pid_get_path(SYSTEMD_CGROUP_CONTROLLER, 1, &p);
-        if (r < 0)
-                return r;
-
-        e = endswith(p, "/" SPECIAL_SYSTEM_SLICE);
-        if (e)
-                *e = 0;
-
-        *path = p;
-        return 0;
+        return cg_pid_get_path(SYSTEMD_CGROUP_CONTROLLER, 1, path);
 }
 
 int cg_shift_path(const char *cgroup, const char *root, const char **shifted) {
 }
 
 int cg_shift_path(const char *cgroup, const char *root, const char **shifted) {
@@ -1135,183 +1122,16 @@ int cg_pid_get_path_shifted(pid_t pid, const char *root, char **cgroup) {
         return 0;
 }
 
         return 0;
 }
 
-int cg_path_decode_unit(const char *cgroup, char **unit){
-        char *e, *c, *s;
-
-        assert(cgroup);
-        assert(unit);
-
-        e = strchrnul(cgroup, '/');
-        c = strndupa(cgroup, e - cgroup);
-        c = cg_unescape(c);
-
-        if (!unit_name_is_valid(c, TEMPLATE_INVALID))
-                return -EINVAL;
-
-        s = strdup(c);
-        if (!s)
-                return -ENOMEM;
-
-        *unit = s;
-        return 0;
-}
-
-static const char *skip_slices(const char *p) {
-        /* Skips over all slice assignments */
-
-        for (;;) {
-                size_t n;
-
-                p += strspn(p, "/");
-
-                n = strcspn(p, "/");
-                if (n <= 6 || memcmp(p + n - 6, ".slice", 6) != 0)
-                        return p;
-
-                p += n;
-        }
-}
-
-int cg_path_get_unit(const char *path, char **unit) {
-        const char *e;
-
-        assert(path);
-        assert(unit);
-
-        e = skip_slices(path);
-
-        return cg_path_decode_unit(e, unit);
-}
-
-int cg_pid_get_unit(pid_t pid, char **unit) {
-        _cleanup_free_ char *cgroup = NULL;
-        int r;
-
-        assert(unit);
-
-        r = cg_pid_get_path_shifted(pid, NULL, &cgroup);
-        if (r < 0)
-                return r;
-
-        return cg_path_get_unit(cgroup, unit);
-}
-
-/**
- * Skip session-*.scope, but require it to be there.
- */
-static const char *skip_session(const char *p) {
-        size_t n;
-
-        assert(p);
-
-        p += strspn(p, "/");
-
-        n = strcspn(p, "/");
-        if (n < strlen("session-x.scope") || memcmp(p, "session-", 8) != 0 || memcmp(p + n - 6, ".scope", 6) != 0)
-                return NULL;
-
-        p += n;
-        p += strspn(p, "/");
-
-        return p;
-}
-
-/**
- * Skip user@*.service, but require it to be there.
- */
-static const char *skip_user_manager(const char *p) {
-        size_t n;
-
-        assert(p);
-
-        p += strspn(p, "/");
-
-        n = strcspn(p, "/");
-        if (n < strlen("user@x.service") || memcmp(p, "user@", 5) != 0 || memcmp(p + n - 8, ".service", 8) != 0)
-                return NULL;
-
-        p += n;
-        p += strspn(p, "/");
-
-        return p;
-}
-
-int cg_path_get_user_unit(const char *path, char **unit) {
-        const char *e, *t;
-
-        assert(path);
-        assert(unit);
-
-        /* We always have to parse the path from the beginning as unit
-         * cgroups might have arbitrary child cgroups and we shouldn't get
-         * confused by those */
-
-        /* Skip slices, if there are any */
-        e = skip_slices(path);
-
-        /* Skip the session scope or user manager... */
-        t = skip_session(e);
-        if (!t)
-                t = skip_user_manager(e);
-        if (!t)
-                return -ENOENT;
-
-        /* ... and skip more slices if there are any */
-        e = skip_slices(t);
-
-        return cg_path_decode_unit(e, unit);
-}
-
-int cg_pid_get_user_unit(pid_t pid, char **unit) {
-        _cleanup_free_ char *cgroup = NULL;
-        int r;
-
-        assert(unit);
-
-        r = cg_pid_get_path_shifted(pid, NULL, &cgroup);
-        if (r < 0)
-                return r;
-
-        return cg_path_get_user_unit(cgroup, unit);
-}
-
-int cg_path_get_machine_name(const char *path, char **machine) {
-        _cleanup_free_ char *u = NULL, *sl = NULL;
-        int r;
-
-        r = cg_path_get_unit(path, &u);
-        if (r < 0)
-                return r;
-
-        sl = strjoin("/run/systemd/machines/unit:", u, NULL);
-        if (!sl)
-                return -ENOMEM;
-
-        return readlink_malloc(sl, machine);
-}
-
-int cg_pid_get_machine_name(pid_t pid, char **machine) {
-        _cleanup_free_ char *cgroup = NULL;
-        int r;
-
-        assert(machine);
-
-        r = cg_pid_get_path_shifted(pid, NULL, &cgroup);
-        if (r < 0)
-                return r;
-
-        return cg_path_get_machine_name(cgroup, machine);
-}
-
 int cg_path_get_session(const char *path, char **session) {
 int cg_path_get_session(const char *path, char **session) {
-        const char *e, *n, *x, *y;
-        char *s;
+        const char *e, *n, *s;
 
 
-        assert(path);
+        /* Elogind uses a flat hierarchy, just "/SESSION".  The only
+           wrinkle is that SESSION might be escaped.  */
 
 
-        /* Skip slices, if there are any */
-        e = skip_slices(path);
+        assert(path);
+        assert(path[0] == '/');
 
 
+        e = path + 1;
         n = strchrnul(e, '/');
         if (e == n)
                 return -ENOENT;
         n = strchrnul(e, '/');
         if (e == n)
                 return -ENOENT;
@@ -1319,17 +1139,13 @@ int cg_path_get_session(const char *path, char **session) {
         s = strndupa(e, n - e);
         s = cg_unescape(s);
 
         s = strndupa(e, n - e);
         s = cg_unescape(s);
 
-        x = startswith(s, "session-");
-        if (!x)
-                return -ENOENT;
-        y = endswith(x, ".scope");
-        if (!y || x == y)
+        if (!s[0])
                 return -ENOENT;
 
         if (session) {
                 char *r;
 
                 return -ENOENT;
 
         if (session) {
                 char *r;
 
-                r = strndup(x, y - x);
+                r = strdup(s);
                 if (!r)
                         return -ENOMEM;
 
                 if (!r)
                         return -ENOMEM;
 
@@ -1350,97 +1166,6 @@ int cg_pid_get_session(pid_t pid, char **session) {
         return cg_path_get_session(cgroup, session);
 }
 
         return cg_path_get_session(cgroup, session);
 }
 
-int cg_path_get_owner_uid(const char *path, uid_t *uid) {
-        _cleanup_free_ char *slice = NULL;
-        const char *start, *end;
-        char *s;
-        uid_t u;
-        int r;
-
-        assert(path);
-
-        r = cg_path_get_slice(path, &slice);
-        if (r < 0)
-                return r;
-
-        start = startswith(slice, "user-");
-        if (!start)
-                return -ENOENT;
-        end = endswith(slice, ".slice");
-        if (!end)
-                return -ENOENT;
-
-        s = strndupa(start, end - start);
-        if (!s)
-                return -ENOENT;
-
-        if (parse_uid(s, &u) < 0)
-                return -EIO;
-
-        if (uid)
-                *uid = u;
-
-        return 0;
-}
-
-int cg_pid_get_owner_uid(pid_t pid, uid_t *uid) {
-        _cleanup_free_ char *cgroup = NULL;
-        int r;
-
-        r = cg_pid_get_path_shifted(pid, NULL, &cgroup);
-        if (r < 0)
-                return r;
-
-        return cg_path_get_owner_uid(cgroup, uid);
-}
-
-int cg_path_get_slice(const char *p, char **slice) {
-        const char *e = NULL;
-        size_t m = 0;
-
-        assert(p);
-        assert(slice);
-
-        for (;;) {
-                size_t n;
-
-                p += strspn(p, "/");
-
-                n = strcspn(p, "/");
-                if (n <= 6 || memcmp(p + n - 6, ".slice", 6) != 0) {
-                        char *s;
-
-                        if (!e)
-                                return -ENOENT;
-
-                        s = strndup(e, m);
-                        if (!s)
-                                return -ENOMEM;
-
-                        *slice = s;
-                        return 0;
-                }
-
-                e = p;
-                m = n;
-
-                p += n;
-        }
-}
-
-int cg_pid_get_slice(pid_t pid, char **slice) {
-        _cleanup_free_ char *cgroup = NULL;
-        int r;
-
-        assert(slice);
-
-        r = cg_pid_get_path_shifted(pid, NULL, &cgroup);
-        if (r < 0)
-                return r;
-
-        return cg_path_get_slice(cgroup, slice);
-}
-
 char *cg_escape(const char *p) {
         bool need_prefix = false;
 
 char *cg_escape(const char *p) {
         bool need_prefix = false;
 
@@ -1527,56 +1252,6 @@ bool cg_controller_is_valid(const char *p, bool allow_named) {
         return true;
 }
 
         return true;
 }
 
-int cg_slice_to_path(const char *unit, char **ret) {
-        _cleanup_free_ char *p = NULL, *s = NULL, *e = NULL;
-        const char *dash;
-
-        assert(unit);
-        assert(ret);
-
-        if (!unit_name_is_valid(unit, TEMPLATE_INVALID))
-                return -EINVAL;
-
-        if (!endswith(unit, ".slice"))
-                return -EINVAL;
-
-        p = unit_name_to_prefix(unit);
-        if (!p)
-                return -ENOMEM;
-
-        dash = strchr(p, '-');
-        while (dash) {
-                _cleanup_free_ char *escaped = NULL;
-                char n[dash - p + sizeof(".slice")];
-
-                strcpy(stpncpy(n, p, dash - p), ".slice");
-
-                if (!unit_name_is_valid(n, TEMPLATE_INVALID))
-                        return -EINVAL;
-
-                escaped = cg_escape(n);
-                if (!escaped)
-                        return -ENOMEM;
-
-                if (!strextend(&s, escaped, "/", NULL))
-                        return -ENOMEM;
-
-                dash = strchr(dash+1, '-');
-        }
-
-        e = cg_escape(unit);
-        if (!e)
-                return -ENOMEM;
-
-        if (!strextend(&s, e, NULL))
-                return -ENOMEM;
-
-        *ret = s;
-        s = NULL;
-
-        return 0;
-}
-
 int cg_set_attribute(const char *controller, const char *path, const char *attribute, const char *value) {
         _cleanup_free_ char *p = NULL;
         int r;
 int cg_set_attribute(const char *controller, const char *path, const char *attribute, const char *value) {
         _cleanup_free_ char *p = NULL;
         int r;
index 96a3d3bafa4662eae59fc11e9f28123d2eea9bb7..cc2aeeb971a9643d731ac135fce5364aa9991c56 100644 (file)
@@ -99,31 +99,17 @@ int cg_is_empty_recursive(const char *controller, const char *path, bool ignore_
 int cg_get_root_path(char **path);
 
 int cg_path_get_session(const char *path, char **session);
 int cg_get_root_path(char **path);
 
 int cg_path_get_session(const char *path, char **session);
-int cg_path_get_owner_uid(const char *path, uid_t *uid);
-int cg_path_get_unit(const char *path, char **unit);
-int cg_path_get_user_unit(const char *path, char **unit);
-int cg_path_get_machine_name(const char *path, char **machine);
-int cg_path_get_slice(const char *path, char **slice);
 
 int cg_shift_path(const char *cgroup, const char *cached_root, const char **shifted);
 int cg_pid_get_path_shifted(pid_t pid, const char *cached_root, char **cgroup);
 
 int cg_pid_get_session(pid_t pid, char **session);
 
 int cg_shift_path(const char *cgroup, const char *cached_root, const char **shifted);
 int cg_pid_get_path_shifted(pid_t pid, const char *cached_root, char **cgroup);
 
 int cg_pid_get_session(pid_t pid, char **session);
-int cg_pid_get_owner_uid(pid_t pid, uid_t *uid);
-int cg_pid_get_unit(pid_t pid, char **unit);
-int cg_pid_get_user_unit(pid_t pid, char **unit);
-int cg_pid_get_machine_name(pid_t pid, char **machine);
-int cg_pid_get_slice(pid_t pid, char **slice);
-
-int cg_path_decode_unit(const char *cgroup, char **unit);
 
 char *cg_escape(const char *p);
 char *cg_unescape(const char *p) _pure_;
 
 bool cg_controller_is_valid(const char *p, bool allow_named);
 
 
 char *cg_escape(const char *p);
 char *cg_unescape(const char *p) _pure_;
 
 bool cg_controller_is_valid(const char *p, bool allow_named);
 
-int cg_slice_to_path(const char *unit, char **ret);
-
 typedef const char* (*cg_migrate_callback_t)(CGroupControllerMask mask, void *userdata);
 
 int cg_create_everywhere(CGroupControllerMask supported, CGroupControllerMask mask, const char *path);
 typedef const char* (*cg_migrate_callback_t)(CGroupControllerMask mask, void *userdata);
 
 int cg_create_everywhere(CGroupControllerMask supported, CGroupControllerMask mask, const char *path);