chiark / gitweb /
util: more join() optimizations
[elogind.git] / src / sd-login.c
index b7ae870af4d7577882a88441ac2412aeb9c10557..2489d78c603dba842113eda63d8ee663eb0100ce 100644 (file)
@@ -251,9 +251,6 @@ static int uid_get_array(uid_t uid, const char *variable, char ***array) {
         char **a;
         int r;
 
-        if (!array)
-                return -EINVAL;
-
         if (asprintf(&p, "/run/systemd/users/%lu", (unsigned long) uid) < 0)
                 return -ENOMEM;
 
@@ -266,7 +263,8 @@ static int uid_get_array(uid_t uid, const char *variable, char ***array) {
                 free(s);
 
                 if (r == -ENOENT) {
-                        *array = NULL;
+                        if (array)
+                                *array = NULL;
                         return 0;
                 }
 
@@ -274,7 +272,8 @@ static int uid_get_array(uid_t uid, const char *variable, char ***array) {
         }
 
         if (!s) {
-                *array = NULL;
+                if (array)
+                        *array = NULL;
                 return 0;
         }
 
@@ -284,8 +283,15 @@ static int uid_get_array(uid_t uid, const char *variable, char ***array) {
         if (!a)
                 return -ENOMEM;
 
-        *array = a;
-        return 0;
+        strv_uniq(a);
+        r = strv_length(a);
+
+        if (array)
+                *array = a;
+        else
+                strv_free(a);
+
+        return r;
 }
 
 _public_ int sd_uid_get_sessions(uid_t uid, int require_active, char ***sessions) {
@@ -327,7 +333,6 @@ _public_ int sd_session_is_active(const char *session) {
 _public_ int sd_session_get_uid(const char *session, uid_t *uid) {
         int r;
         char *p, *s = NULL;
-        unsigned long ul;
 
         if (!session)
                 return -EINVAL;
@@ -446,9 +451,6 @@ _public_ int sd_seat_get_sessions(const char *seat, char ***sessions, uid_t **ui
         if (!seat)
                 return -EINVAL;
 
-        if (!sessions && !uids)
-                return -EINVAL;
-
         p = strappend("/run/systemd/seats/", seat);
         if (!p)
                 return -ENOMEM;
@@ -465,7 +467,7 @@ _public_ int sd_seat_get_sessions(const char *seat, char ***sessions, uid_t **ui
                 return r;
         }
 
-        if (sessions && s) {
+        if (s) {
                 a = strv_split(s, " ");
                 if (!a) {
                         free(s);
@@ -511,8 +513,12 @@ _public_ int sd_seat_get_sessions(const char *seat, char ***sessions, uid_t **ui
 
         free(t);
 
+        r = strv_length(a);
+
         if (sessions)
                 *sessions = a;
+        else
+                strv_free(a);
 
         if (uids)
                 *uids = b;
@@ -520,22 +526,44 @@ _public_ int sd_seat_get_sessions(const char *seat, char ***sessions, uid_t **ui
         if (n_uids)
                 *n_uids = n;
 
-        return 0;
+        return r;
 }
 
-_public_ int sd_get_seats(char ***seats) {
+_public_ int sd_seat_can_multi_session(const char *seat) {
+        char *p, *s = NULL;
+        int r;
 
-        if (!seats)
+        if (!seat)
                 return -EINVAL;
 
+        p = strappend("/run/systemd/seats/", seat);
+        if (!p)
+                return -ENOMEM;
+
+        r = parse_env_file(p, NEWLINE,
+                           "IS_VTCONSOLE", &s,
+                           NULL);
+        free(p);
+
+        if (r < 0) {
+                free(s);
+                return r;
+        }
+
+        if (s) {
+                r = parse_boolean(s);
+                free(s);
+        } else
+                r = 0;
+
+        return r;
+}
+
+_public_ int sd_get_seats(char ***seats) {
         return get_files_in_directory("/run/systemd/seats/", seats);
 }
 
 _public_ int sd_get_sessions(char ***sessions) {
-
-        if (!sessions)
-                return -EINVAL;
-
         return get_files_in_directory("/run/systemd/sessions/", sessions);
 }
 
@@ -545,9 +573,6 @@ _public_ int sd_get_uids(uid_t **users) {
         unsigned n = 0;
         uid_t *l = NULL;
 
-        if (!users)
-                return -EINVAL;
-
         d = opendir("/run/systemd/users/");
         for (;;) {
                 struct dirent buffer, *de;
@@ -572,30 +597,34 @@ _public_ int sd_get_uids(uid_t **users) {
                 if (k < 0)
                         continue;
 
-                if ((unsigned) r >= n) {
-                        uid_t *t;
+                if (users) {
+                        if ((unsigned) r >= n) {
+                                uid_t *t;
 
-                        n = MAX(16, 2*r);
-                        t = realloc(l, sizeof(uid_t) * n);
-                        if (!t) {
-                                r = -ENOMEM;
-                                goto finish;
-                        }
+                                n = MAX(16, 2*r);
+                                t = realloc(l, sizeof(uid_t) * n);
+                                if (!t) {
+                                        r = -ENOMEM;
+                                        goto finish;
+                                }
 
-                        l = t;
-                }
+                                l = t;
+                        }
 
-                assert((unsigned) r < n);
-                l[r++] = uid;
+                        assert((unsigned) r < n);
+                        l[r++] = uid;
+                } else
+                        r++;
         }
 
 finish:
         if (d)
                 closedir(d);
 
-        if (r >= 0)
-                *users = l;
-        else
+        if (r >= 0) {
+                if (users)
+                        *users = l;
+        } else
                 free(l);
 
         return r;
@@ -610,7 +639,6 @@ static inline sd_login_monitor* FD_TO_MONITOR(int fd) {
 }
 
 _public_ int sd_login_monitor_new(const char *category, sd_login_monitor **m) {
-        const char *path;
         int fd, k;
         bool good = false;