chiark / gitweb /
Prep 235: Implement an alternative approach for sd_pid_get_owner_uid(), making test...
authorSven Eden <yamakuzure@gmx.net>
Wed, 13 Dec 2017 18:21:30 +0000 (19:21 +0100)
committerSven Eden <yamakuzure@gmx.net>
Wed, 13 Dec 2017 18:21:30 +0000 (19:21 +0100)
src/basic/cgroup-util.c
src/basic/cgroup-util.h
src/libelogind/sd-login/sd-login.c

index 9b4a5608552c65c71c7b29e2ec64dc36f756371b..c4aec37ffdf3093d7e7e31b618ee6c6ecba33bef 100644 (file)
@@ -1811,8 +1811,8 @@ int cg_pid_get_session(pid_t pid, char **session) {
         return cg_path_get_session(cgroup, session);
 }
 
-#if 0 /// UNNEEDED by elogind
 int cg_path_get_owner_uid(const char *path, uid_t *uid) {
+#if 0 /// elogind does not support systemd slices
         _cleanup_free_ char *slice = NULL;
         char *start, *end;
         int r;
@@ -1835,6 +1835,34 @@ int cg_path_get_owner_uid(const char *path, uid_t *uid) {
                 return -ENXIO;
 
         return 0;
+#else
+        _cleanup_free_ char *p = NULL, *s = NULL;
+        char *start;
+        int r;
+
+        assert(path);
+
+        /* Basically this is a simple session->uid mapping here.
+           The path argument will be something like "/<session id>"
+           and that is something we can map.
+        */
+
+        start = startswith(path, "/");
+        if (start)
+                p = strappend("/run/systemd/sessions/", start);
+        else
+                p = strappend("/run/systemd/sessions/", path);
+
+        r = parse_env_file(p, NEWLINE, "UID", &s, NULL);
+        if (r == -ENOENT)
+                return -ENXIO;
+        if (r < 0)
+                return r;
+        if (isempty(s))
+                return -EIO;
+
+        return parse_uid(s, uid);
+#endif // 0
 }
 
 int cg_pid_get_owner_uid(pid_t pid, uid_t *uid) {
@@ -1848,6 +1876,7 @@ int cg_pid_get_owner_uid(pid_t pid, uid_t *uid) {
         return cg_path_get_owner_uid(cgroup, uid);
 }
 
+#if 0 /// UNNEEDED by elogind
 int cg_path_get_slice(const char *p, char **slice) {
         const char *e = NULL;
 
index 1f1a338739c7cef62875b34b28416187df777f26..e8423dd0d8e5b8da29214950ff4ff5725ad73fad 100644 (file)
@@ -206,8 +206,8 @@ int cg_is_empty_recursive(const char *controller, const char *path);
 int cg_get_root_path(char **path);
 
 int cg_path_get_session(const char *path, char **session);
-#if 0 /// UNNEEDED by elogind
 int cg_path_get_owner_uid(const char *path, uid_t *uid);
+#if 0 /// UNNEEDED by elogind
 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);
@@ -219,8 +219,8 @@ int cg_shift_path(const char *cgroup, const char *cached_root, const char **shif
 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);
-#if 0 /// UNNEEDED by elogind
 int cg_pid_get_owner_uid(pid_t pid, uid_t *uid);
+#if 0 /// UNNEEDED by elogind
 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);
index 7859f1c47037566c49b976125319074d10255fcc..91b3d124336df9ccd4406f6e6502b3d218c5560e 100644 (file)
@@ -146,19 +146,13 @@ _public_ int sd_pid_get_user_slice(pid_t pid, char **slice) {
 }
 
 _public_ int sd_pid_get_owner_uid(pid_t pid, uid_t *uid) {
-#if 0 /// UNNEEDED by elogind
         int r;
-#endif // 0
 
         assert_return(pid >= 0, -EINVAL);
         assert_return(uid, -EINVAL);
 
-#if 0 /// elogind does not support systemd slices
         r = cg_pid_get_owner_uid(pid, uid);
         return IN_SET(r, -ENXIO, -ENOMEDIUM) ? -ENODATA : r;
-#else
-        return -ESRCH;
-#endif // 0
 }
 
 _public_ int sd_pid_get_cgroup(pid_t pid, char **cgroup) {