chiark / gitweb /
sd-login: improve error handling
authorLennart Poettering <lennart@poettering.net>
Mon, 31 Aug 2015 22:40:20 +0000 (00:40 +0200)
committerSven Eden <yamakuzure@gmx.net>
Wed, 29 Mar 2017 08:45:08 +0000 (10:45 +0200)
let's return ENXIO whenever we don't know something rather than ENOENT.

ENOENT suggests this was really about a file or directory, while ENXIO
is a more generic "not found" indicator.

src/libelogind/sd-login/sd-login.c
src/libelogind/sd-login/test-login.c

index 03d9cdbcb3ee10346a2ff121448fa31da362240a..2041d87ed789bc94644d1e71057523fa84f11f97 100644 (file)
@@ -305,8 +305,6 @@ _public_ int sd_peer_get_cgroup(int fd, char **cgroup) {
 }
 
 static int file_of_uid(uid_t uid, char **p) {
-
-        assert_return(uid_is_valid(uid), -EINVAL);
         assert(p);
 
         if (asprintf(p, "/run/systemd/users/" UID_FMT, uid) < 0)
@@ -333,15 +331,11 @@ _public_ int sd_uid_get_state(uid_t uid, char**state) {
                 if (!s)
                         return -ENOMEM;
 
-        }
-        if (r < 0) {
+        } else if (r < 0) {
                 free(s);
                 return r;
-        }
-        if (isempty(s)) {
-                free(s);
+        } else if (!s)
                 return -EIO;
-        }
 
         *state = s;
         return 0;
@@ -359,11 +353,12 @@ _public_ int sd_uid_get_display(uid_t uid, char **session) {
 
         r = parse_env_file(p, NEWLINE, "DISPLAY", &s, NULL);
         if (r == -ENOENT)
-                return -ENODATA;
+                return -ENXIO;
         if (r < 0)
                 return r;
+
         if (isempty(s))
-                return -ENODATA;
+                return -ENXIO;
 
         *session = s;
         s = NULL;
@@ -371,63 +366,35 @@ _public_ int sd_uid_get_display(uid_t uid, char **session) {
         return 0;
 }
 
-static int file_of_seat(const char *seat, char **_p) {
-        char *p;
-        int r;
-
-        assert(_p);
-
-        if (seat) {
-                if (!filename_is_valid(seat))
-                        return -EINVAL;
-
-                p = strappend("/run/systemd/seats/", seat);
-        } else {
-                _cleanup_free_ char *buf = NULL;
-
-                r = sd_session_get_seat(NULL, &buf);
-                if (r < 0)
-                        return r;
-
-                p = strappend("/run/systemd/seats/", buf);
-        }
-
-        if (!p)
-                return -ENOMEM;
-
-        *_p = p;
-        p = NULL;
-        return 0;
-}
-
 _public_ int sd_uid_is_on_seat(uid_t uid, int require_active, const char *seat) {
         _cleanup_free_ char *t = NULL, *s = NULL, *p = NULL;
         size_t l;
         int r;
         const char *word, *variable, *state;
 
-        assert_return(uid_is_valid(uid), -EINVAL);
-
-        r = file_of_seat(seat, &p);
-        if (r < 0)
-                return r;
+        assert_return(seat, -EINVAL);
 
         variable = require_active ? "ACTIVE_UID" : "UIDS";
 
+        p = strappend("/run/systemd/seats/", seat);
+        if (!p)
+                return -ENOMEM;
+
         r = parse_env_file(p, NEWLINE, variable, &s, NULL);
-        if (r == -ENOENT)
-                return 0;
+
         if (r < 0)
                 return r;
-        if (isempty(s))
-                return 0;
+
+        if (!s)
+                return -EIO;
 
         if (asprintf(&t, UID_FMT, uid) < 0)
                 return -ENOMEM;
 
-        FOREACH_WORD(word, l, s, state)
+        FOREACH_WORD(word, l, s, state) {
                 if (strneq(t, word, l))
                         return 1;
+        }
 
         return 0;
 }
@@ -437,22 +404,31 @@ static int uid_get_array(uid_t uid, const char *variable, char ***array) {
         char **a;
         int r;
 
-        assert(variable);
-
         r = file_of_uid(uid, &p);
         if (r < 0)
                 return r;
 
-        r = parse_env_file(p, NEWLINE, variable, &s, NULL);
-        if (r == -ENOENT || (r >= 0 && isempty(s))) {
+        r = parse_env_file(p, NEWLINE,
+                           variable, &s,
+                           NULL);
+        if (r < 0) {
+                if (r == -ENOENT) {
+                        if (array)
+                                *array = NULL;
+                        return 0;
+                }
+
+                return r;
+        }
+
+        if (!s) {
                 if (array)
                         *array = NULL;
                 return 0;
         }
-        if (r < 0)
-                return r;
 
         a = strv_split(s, " ");
+
         if (!a)
                 return -ENOMEM;
 
@@ -514,39 +490,37 @@ static int file_of_session(const char *session, char **_p) {
 }
 
 _public_ int sd_session_is_active(const char *session) {
-        _cleanup_free_ char *p = NULL, *s = NULL;
         int r;
+        _cleanup_free_ char *p = NULL, *s = NULL;
 
         r = file_of_session(session, &p);
         if (r < 0)
                 return r;
 
         r = parse_env_file(p, NEWLINE, "ACTIVE", &s, NULL);
-        if (r == -ENOENT)
-                return -ENXIO;
         if (r < 0)
                 return r;
-        if (isempty(s))
+
+        if (!s)
                 return -EIO;
 
         return parse_boolean(s);
 }
 
 _public_ int sd_session_is_remote(const char *session) {
-        _cleanup_free_ char *p = NULL, *s = NULL;
         int r;
+        _cleanup_free_ char *p = NULL, *s = NULL;
 
         r = file_of_session(session, &p);
         if (r < 0)
                 return r;
 
         r = parse_env_file(p, NEWLINE, "REMOTE", &s, NULL);
-        if (r == -ENOENT)
-                return -ENXIO;
         if (r < 0)
                 return r;
-        if (isempty(s))
-                return -ENODATA;
+
+        if (!s)
+                return -EIO;
 
         return parse_boolean(s);
 }
@@ -562,11 +536,9 @@ _public_ int sd_session_get_state(const char *session, char **state) {
                 return r;
 
         r = parse_env_file(p, NEWLINE, "STATE", &s, NULL);
-        if (r == -ENOENT)
-                return -ENXIO;
         if (r < 0)
                 return r;
-        if (isempty(s))
+        else if (!s)
                 return -EIO;
 
         *state = s;
@@ -586,11 +558,10 @@ _public_ int sd_session_get_uid(const char *session, uid_t *uid) {
                 return r;
 
         r = parse_env_file(p, NEWLINE, "UID", &s, NULL);
-        if (r == -ENOENT)
-                return -ENXIO;
         if (r < 0)
                 return r;
-        if (isempty(s))
+
+        if (!s)
                 return -EIO;
 
         return parse_uid(s, uid);
@@ -601,19 +572,17 @@ static int session_get_string(const char *session, const char *field, char **val
         int r;
 
         assert_return(value, -EINVAL);
-        assert(field);
 
         r = file_of_session(session, &p);
         if (r < 0)
                 return r;
 
         r = parse_env_file(p, NEWLINE, field, &s, NULL);
-        if (r == -ENOENT)
-                return -ENXIO;
         if (r < 0)
                 return r;
+
         if (isempty(s))
-                return -ENODATA;
+                return -ENXIO;
 
         *value = s;
         s = NULL;
@@ -633,8 +602,6 @@ _public_ int sd_session_get_vt(const char *session, unsigned *vtnr) {
         unsigned u;
         int r;
 
-        assert_return(vtnr, -EINVAL);
-
         r = session_get_string(session, "VTNR", &vtnr_string);
         if (r < 0)
                 return r;
@@ -690,6 +657,32 @@ _public_ int sd_session_get_remote_host(const char *session, char **remote_host)
         return session_get_string(session, "REMOTE_HOST", remote_host);
 }
 
+static int file_of_seat(const char *seat, char **_p) {
+        char *p;
+        int r;
+
+        assert(_p);
+
+        if (seat)
+                p = strappend("/run/systemd/seats/", seat);
+        else {
+                _cleanup_free_ char *buf = NULL;
+
+                r = sd_session_get_seat(NULL, &buf);
+                if (r < 0)
+                        return r;
+
+                p = strappend("/run/systemd/seats/", buf);
+        }
+
+        if (!p)
+                return -ENOMEM;
+
+        *_p = p;
+        p = NULL;
+        return 0;
+}
+
 _public_ int sd_seat_get_active(const char *seat, char **session, uid_t *uid) {
         _cleanup_free_ char *p = NULL, *s = NULL, *t = NULL;
         int r;
@@ -704,16 +697,14 @@ _public_ int sd_seat_get_active(const char *seat, char **session, uid_t *uid) {
                            "ACTIVE", &s,
                            "ACTIVE_UID", &t,
                            NULL);
-        if (r == -ENOENT)
-                return -ENXIO;
         if (r < 0)
                 return r;
 
         if (session && !s)
-                return -ENODATA;
+                return -ENOENT;
 
         if (uid && !t)
-                return -ENODATA;
+                return -ENOENT;
 
         if (uid && t) {
                 r = parse_uid(t, uid);
@@ -744,8 +735,7 @@ _public_ int sd_seat_get_sessions(const char *seat, char ***sessions, uid_t **ui
                            "SESSIONS", &s,
                            "ACTIVE_SESSIONS", &t,
                            NULL);
-        if (r == -ENOENT)
-                return -ENXIO;
+
         if (r < 0)
                 return r;
 
@@ -777,6 +767,7 @@ _public_ int sd_seat_get_sessions(const char *seat, char ***sessions, uid_t **ui
                                         return -ENOMEM;
 
                                 r = parse_uid(k, b + i);
+
                                 if (r < 0)
                                         continue;
 
@@ -807,7 +798,7 @@ static int seat_get_can(const char *seat, const char *variable) {
         _cleanup_free_ char *p = NULL, *s = NULL;
         int r;
 
-        assert(variable);
+        assert_return(variable, -EINVAL);
 
         r = file_of_seat(seat, &p);
         if (r < 0)
@@ -816,12 +807,10 @@ static int seat_get_can(const char *seat, const char *variable) {
         r = parse_env_file(p, NEWLINE,
                            variable, &s,
                            NULL);
-        if (r == -ENOENT)
-                return -ENXIO;
         if (r < 0)
                 return r;
-        if (isempty(s))
-                return -ENODATA;
+        if (!s)
+                return 0;
 
         return parse_boolean(s);
 }
@@ -945,8 +934,6 @@ _public_ int sd_machine_get_class(const char *machine, char **class) {
 
         p = strjoina("/run/systemd/machines/", machine);
         r = parse_env_file(p, NEWLINE, "CLASS", &c, NULL);
-        if (r == -ENOENT)
-                return -ENXIO;
         if (r < 0)
                 return r;
         if (!c)
@@ -970,8 +957,6 @@ _public_ int sd_machine_get_ifindices(const char *machine, int **ifindices) {
 
         p = strjoina("/run/systemd/machines/", machine);
         r = parse_env_file(p, NEWLINE, "NETIF", &netif, NULL);
-        if (r == -ENOENT)
-                return -ENXIO;
         if (r < 0)
                 return r;
         if (!netif) {
index b60ad0c38603faf0ec12a59979ed46b332f8aa5d..6531b9db384514f844332ba39eb2e533076d68fa 100644 (file)
@@ -33,7 +33,7 @@ static void test_login(void) {
         _cleanup_free_ char *pp = NULL, *qq = NULL;
         int r, k;
         uid_t u, u2;
-        char *seat, *type, *class, *display, *remote_user, *remote_host, *display_session, *cgroup;
+        char *seat, *type, *class, *display, *remote_user, *remote_host, *display_session;
         char *session;
         char *state;
         char *session2;
@@ -50,13 +50,9 @@ static void test_login(void) {
         assert_se(sd_pid_get_owner_uid(0, &u2) == 0);
         printf("user = "UID_FMT"\n", u2);
 
-        assert_se(sd_pid_get_cgroup(0, &cgroup) == 0);
-        printf("cgroup = %s\n", cgroup);
-        free(cgroup);
-
         display_session = NULL;
         r = sd_uid_get_display(u2, &display_session);
-        assert_se(r >= 0 || r == -ENODATA);
+        assert_se(r >= 0 || r == -ENXIO);
         printf("user's display session = %s\n", strna(display_session));
         free(display_session);
 
@@ -112,19 +108,19 @@ static void test_login(void) {
 
         display = NULL;
         r = sd_session_get_display(session, &display);
-        assert_se(r >= 0 || r == -ENODATA);
+        assert_se(r >= 0 || r == -ENXIO);
         printf("display = %s\n", strna(display));
         free(display);
 
         remote_user = NULL;
         r = sd_session_get_remote_user(session, &remote_user);
-        assert_se(r >= 0 || r == -ENODATA);
+        assert_se(r >= 0 || r == -ENXIO);
         printf("remote_user = %s\n", strna(remote_user));
         free(remote_user);
 
         remote_host = NULL;
         r = sd_session_get_remote_host(session, &remote_host);
-        assert_se(r >= 0 || r == -ENODATA);
+        assert_se(r >= 0 || r == -ENXIO);
         printf("remote_host = %s\n", strna(remote_host));
         free(remote_host);