chiark / gitweb /
sd-login: improve error handling
[elogind.git] / src / libelogind / sd-login / sd-login.c
index 0eadc8c747ab6a0031c10f2250cc85d6ff44c12b..2041d87ed789bc94644d1e71057523fa84f11f97 100644 (file)
 #include "hostname-util.h"
 #include "sd-login.h"
 
+/* Error codes:
+ *
+ *    invalid input parameters                → -EINVAL
+ *    invalid fd                              → -EBADF
+ *    process does not exist                  → -ESRCH
+ *    cgroup does not exist                   → -ENOENT
+ *    machine, session does not exist         → -ENXIO
+ *    requested metadata on object is missing → -ENODATA
+ */
+
 _public_ int sd_pid_get_session(pid_t pid, char **session) {
 
         assert_return(pid >= 0, -EINVAL);
@@ -48,7 +58,12 @@ _public_ int sd_pid_get_unit(pid_t pid, char **unit) {
         assert_return(pid >= 0, -EINVAL);
         assert_return(unit, -EINVAL);
 
+/// elogind does not support systemd units
+#if 0
         return cg_pid_get_unit(pid, unit);
+#else
+        return -ESRCH;
+#endif // 0
 }
 
 _public_ int sd_pid_get_user_unit(pid_t pid, char **unit) {
@@ -56,7 +71,12 @@ _public_ int sd_pid_get_user_unit(pid_t pid, char **unit) {
         assert_return(pid >= 0, -EINVAL);
         assert_return(unit, -EINVAL);
 
+/// elogind does not support systemd units
+#if 0
         return cg_pid_get_user_unit(pid, unit);
+#else
+        return -ESRCH;
+#endif // 0
 }
 
 _public_ int sd_pid_get_machine_name(pid_t pid, char **name) {
@@ -64,7 +84,12 @@ _public_ int sd_pid_get_machine_name(pid_t pid, char **name) {
         assert_return(pid >= 0, -EINVAL);
         assert_return(name, -EINVAL);
 
+/// elogind does not support systemd units
+#if 0
         return cg_pid_get_machine_name(pid, name);
+#else
+        return -ESRCH;
+#endif // 0
 }
 
 _public_ int sd_pid_get_slice(pid_t pid, char **slice) {
@@ -72,7 +97,12 @@ _public_ int sd_pid_get_slice(pid_t pid, char **slice) {
         assert_return(pid >= 0, -EINVAL);
         assert_return(slice, -EINVAL);
 
+/// elogind does not support systemd slices
+#if 0
         return cg_pid_get_slice(pid, slice);
+#else
+        return -ESRCH;
+#endif // 0
 }
 
 _public_ int sd_pid_get_user_slice(pid_t pid, char **slice) {
@@ -80,7 +110,12 @@ _public_ int sd_pid_get_user_slice(pid_t pid, char **slice) {
         assert_return(pid >= 0, -EINVAL);
         assert_return(slice, -EINVAL);
 
+/// elogind does not support systemd slices
+#if 0
         return cg_pid_get_user_slice(pid, slice);
+#else
+        return -ESRCH;
+#endif // 0
 }
 
 _public_ int sd_pid_get_owner_uid(pid_t pid, uid_t *uid) {
@@ -88,7 +123,38 @@ _public_ int sd_pid_get_owner_uid(pid_t pid, uid_t *uid) {
         assert_return(pid >= 0, -EINVAL);
         assert_return(uid, -EINVAL);
 
+/// elogind does not support systemd slices
+#if 0
         return cg_pid_get_owner_uid(pid, uid);
+#else
+        return -ESRCH;
+#endif // 0
+}
+
+_public_ int sd_pid_get_cgroup(pid_t pid, char **cgroup) {
+        char *c;
+        int r;
+
+        assert_return(pid >= 0, -EINVAL);
+        assert_return(cgroup, -EINVAL);
+
+        r = cg_pid_get_path(SYSTEMD_CGROUP_CONTROLLER, pid, &c);
+        if (r < 0)
+                return r;
+
+        /* The internal APIs return the empty string for the root
+         * cgroup, let's return the "/" in the public APIs instead, as
+         * that's easier and less ambigious for people to grok. */
+        if (isempty(c)) {
+                free(c);
+                c = strdup("/");
+                if (!c)
+                        return -ENOMEM;
+
+        }
+
+        *cgroup = c;
+        return 0;
 }
 
 _public_ int sd_peer_get_session(int fd, char **session) {
@@ -102,7 +168,12 @@ _public_ int sd_peer_get_session(int fd, char **session) {
         if (r < 0)
                 return r;
 
+/// elogind does not support systemd scopes
+#if 0
         return cg_pid_get_session(ucred.pid, session);
+#else
+        return -ESRCH;
+#endif // 0
 }
 
 _public_ int sd_peer_get_owner_uid(int fd, uid_t *uid) {
@@ -116,7 +187,12 @@ _public_ int sd_peer_get_owner_uid(int fd, uid_t *uid) {
         if (r < 0)
                 return r;
 
+/// elogind does not support systemd units
+#if 0
         return cg_pid_get_owner_uid(ucred.pid, uid);
+#else
+        return -ESRCH;
+#endif // 0
 }
 
 _public_ int sd_peer_get_unit(int fd, char **unit) {
@@ -130,7 +206,12 @@ _public_ int sd_peer_get_unit(int fd, char **unit) {
         if (r < 0)
                 return r;
 
+/// elogind does not support systemd units
+#if 0
         return cg_pid_get_unit(ucred.pid, unit);
+#else
+        return -ESRCH;
+#endif // 0
 }
 
 _public_ int sd_peer_get_user_unit(int fd, char **unit) {
@@ -144,7 +225,12 @@ _public_ int sd_peer_get_user_unit(int fd, char **unit) {
         if (r < 0)
                 return r;
 
+/// elogind does not support systemd units
+#if 0
         return cg_pid_get_user_unit(ucred.pid, unit);
+#else
+        return -ESRCH;
+#endif // 0
 }
 
 _public_ int sd_peer_get_machine_name(int fd, char **machine) {
@@ -158,7 +244,12 @@ _public_ int sd_peer_get_machine_name(int fd, char **machine) {
         if (r < 0)
                 return r;
 
+/// elogind does not support systemd units
+#if 0
         return cg_pid_get_machine_name(ucred.pid, machine);
+#else
+        return -ESRCH;
+#endif // 0
 }
 
 _public_ int sd_peer_get_slice(int fd, char **slice) {
@@ -172,7 +263,12 @@ _public_ int sd_peer_get_slice(int fd, char **slice) {
         if (r < 0)
                 return r;
 
+/// elogind does not support systemd slices
+#if 0
         return cg_pid_get_slice(ucred.pid, slice);
+#else
+        return -ESRCH;
+#endif // 0
 }
 
 _public_ int sd_peer_get_user_slice(int fd, char **slice) {
@@ -186,7 +282,26 @@ _public_ int sd_peer_get_user_slice(int fd, char **slice) {
         if (r < 0)
                 return r;
 
+/// elogind does not support systemd slices
+#if 0
         return cg_pid_get_user_slice(ucred.pid, slice);
+#else
+        return -ESRCH;
+#endif // 0
+}
+
+_public_ int sd_peer_get_cgroup(int fd, char **cgroup) {
+        struct ucred ucred;
+        int r;
+
+        assert_return(fd >= 0, -EBADF);
+        assert_return(cgroup, -EINVAL);
+
+        r = getpeercred(fd, &ucred);
+        if (r < 0)
+                return r;
+
+        return sd_pid_get_cgroup(ucred.pid, cgroup);
 }
 
 static int file_of_uid(uid_t uid, char **p) {
@@ -237,11 +352,13 @@ _public_ int sd_uid_get_display(uid_t uid, char **session) {
                 return r;
 
         r = parse_env_file(p, NEWLINE, "DISPLAY", &s, NULL);
+        if (r == -ENOENT)
+                return -ENXIO;
         if (r < 0)
                 return r;
 
         if (isempty(s))
-                return -ENOENT;
+                return -ENXIO;
 
         *session = s;
         s = NULL;
@@ -465,7 +582,7 @@ static int session_get_string(const char *session, const char *field, char **val
                 return r;
 
         if (isempty(s))
-                return -ENOENT;
+                return -ENXIO;
 
         *value = s;
         s = NULL;