From: Sven Eden Date: Wed, 13 Dec 2017 18:21:30 +0000 (+0100) Subject: Prep 235: Implement an alternative approach for sd_pid_get_owner_uid(), making test... X-Git-Tag: v235.1~14 X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=commitdiff_plain;h=6ce305d6240b20ff7be3d75264f2d5a33be04e85;p=elogind.git Prep 235: Implement an alternative approach for sd_pid_get_owner_uid(), making test-login to no longer fail. --- diff --git a/src/basic/cgroup-util.c b/src/basic/cgroup-util.c index 9b4a56085..c4aec37ff 100644 --- a/src/basic/cgroup-util.c +++ b/src/basic/cgroup-util.c @@ -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 "/" + 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; diff --git a/src/basic/cgroup-util.h b/src/basic/cgroup-util.h index 1f1a33873..e8423dd0d 100644 --- a/src/basic/cgroup-util.h +++ b/src/basic/cgroup-util.h @@ -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); diff --git a/src/libelogind/sd-login/sd-login.c b/src/libelogind/sd-login/sd-login.c index 7859f1c47..91b3d1243 100644 --- a/src/libelogind/sd-login/sd-login.c +++ b/src/libelogind/sd-login/sd-login.c @@ -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) {