chiark / gitweb /
core/mount: add dependencies to dynamically mounted mounts too
[elogind.git] / src / libsystemd / sd-login / sd-login.c
index 39445a191b0b7c0c2ef36f64cc5aa3545ac47361..c72d23ed5342f2032a85c438ca7741897262d282 100644 (file)
@@ -165,6 +165,15 @@ _public_ int sd_peer_get_slice(int fd, char **slice) {
         return cg_pid_get_slice(ucred.pid, slice);
 }
 
+static int file_of_uid(uid_t uid, char **p) {
+        assert(p);
+
+        if (asprintf(p, "/run/systemd/users/" UID_FMT, uid) < 0)
+                return -ENOMEM;
+
+        return 0;
+}
+
 _public_ int sd_uid_get_state(uid_t uid, char**state) {
         _cleanup_free_ char *p = NULL;
         char *s = NULL;
@@ -172,8 +181,9 @@ _public_ int sd_uid_get_state(uid_t uid, char**state) {
 
         assert_return(state, -EINVAL);
 
-        if (asprintf(&p, "/run/systemd/users/%lu", (unsigned long) uid) < 0)
-                return -ENOMEM;
+        r = file_of_uid(uid, &p);
+        if (r < 0)
+                return r;
 
         r = parse_env_file(p, NEWLINE, "STATE", &s, NULL);
         if (r == -ENOENT) {
@@ -192,12 +202,34 @@ _public_ int sd_uid_get_state(uid_t uid, char**state) {
         return 0;
 }
 
+_public_ int sd_uid_get_display(uid_t uid, char **session) {
+        _cleanup_free_ char *p = NULL, *s = NULL;
+        int r;
+
+        assert_return(session, -EINVAL);
+
+        r = file_of_uid(uid, &p);
+        if (r < 0)
+                return r;
+
+        r = parse_env_file(p, NEWLINE, "DISPLAY", &s, NULL);
+        if (r < 0)
+                return r;
+
+        if (isempty(s))
+                return -ENOENT;
+
+        *session = s;
+        s = NULL;
+
+        return 0;
+}
+
 _public_ int sd_uid_is_on_seat(uid_t uid, int require_active, const char *seat) {
-        char *w, *state;
         _cleanup_free_ char *t = NULL, *s = NULL, *p = NULL;
         size_t l;
         int r;
-        const char *variable;
+        const char *word, *variable, *state;
 
         assert_return(seat, -EINVAL);
 
@@ -215,11 +247,11 @@ _public_ int sd_uid_is_on_seat(uid_t uid, int require_active, const char *seat)
         if (!s)
                 return -EIO;
 
-        if (asprintf(&t, "%lu", (unsigned long) uid) < 0)
+        if (asprintf(&t, UID_FMT, uid) < 0)
                 return -ENOMEM;
 
-        FOREACH_WORD(w, l, s, state) {
-                if (strneq(t, w, l))
+        FOREACH_WORD(word, l, s, state) {
+                if (strneq(t, word, l))
                         return 1;
         }
 
@@ -231,8 +263,9 @@ static int uid_get_array(uid_t uid, const char *variable, char ***array) {
         char **a;
         int r;
 
-        if (asprintf(&p, "/run/systemd/users/%lu", (unsigned long) uid) < 0)
-                return -ENOMEM;
+        r = file_of_uid(uid, &p);
+        if (r < 0)
+                return r;
 
         r = parse_env_file(p, NEWLINE,
                            variable, &s,
@@ -362,7 +395,6 @@ _public_ int sd_session_get_state(const char *session, char **state) {
                 return r;
 
         r = parse_env_file(p, NEWLINE, "STATE", &s, NULL);
-
         if (r < 0)
                 return r;
         else if (!s)
@@ -405,7 +437,6 @@ static int session_get_string(const char *session, const char *field, char **val
                 return r;
 
         r = parse_env_file(p, NEWLINE, field, &s, NULL);
-
         if (r < 0)
                 return r;
 
@@ -454,6 +485,25 @@ _public_ int sd_session_get_class(const char *session, char **class) {
         return session_get_string(session, "CLASS", class);
 }
 
+_public_ int sd_session_get_desktop(const char *session, char **desktop) {
+        _cleanup_free_ char *escaped = NULL;
+        char *t;
+        int r;
+
+        assert_return(desktop, -EINVAL);
+
+        r = session_get_string(session, "DESKTOP", &escaped);
+        if (r < 0)
+                return r;
+
+        t = cunescape(escaped);
+        if (!t)
+                return -ENOMEM;
+
+        *desktop = t;
+        return 0;
+}
+
 _public_ int sd_session_get_display(const char *session, char **display) {
         return session_get_string(session, "DISPLAY", display);
 }
@@ -555,10 +605,10 @@ _public_ int sd_seat_get_sessions(const char *seat, char ***sessions, uid_t **ui
         }
 
         if (uids && t) {
-                char *w, *state;
+                const char *word, *state;
                 size_t l;
 
-                FOREACH_WORD(w, l, t, state)
+                FOREACH_WORD(word, l, t, state)
                         n++;
 
                 if (n > 0) {
@@ -568,10 +618,10 @@ _public_ int sd_seat_get_sessions(const char *seat, char ***sessions, uid_t **ui
                         if (!b)
                                 return -ENOMEM;
 
-                        FOREACH_WORD(w, l, t, state) {
+                        FOREACH_WORD(word, l, t, state) {
                                 _cleanup_free_ char *k = NULL;
 
-                                k = strndup(w, l);
+                                k = strndup(word, l);
                                 if (!k)
                                         return -ENOMEM;
 
@@ -738,7 +788,7 @@ _public_ int sd_machine_get_class(const char *machine, char **class) {
         const char *p;
         int r;
 
-        assert_return(filename_is_safe(machine), -EINVAL);
+        assert_return(machine_name_is_valid(machine), -EINVAL);
         assert_return(class, -EINVAL);
 
         p = strappenda("/run/systemd/machines/", machine);
@@ -754,6 +804,48 @@ _public_ int sd_machine_get_class(const char *machine, char **class) {
         return 0;
 }
 
+_public_ int sd_machine_get_ifindices(const char *machine, int **ifindices) {
+        _cleanup_free_ char *netif = NULL;
+        size_t l, allocated = 0, nr = 0;
+        int *ni = NULL;
+        const char *p, *word, *state;
+        int r;
+
+        assert_return(machine_name_is_valid(machine), -EINVAL);
+        assert_return(ifindices, -EINVAL);
+
+        p = strappenda("/run/systemd/machines/", machine);
+        r = parse_env_file(p, NEWLINE, "NETIF", &netif, NULL);
+        if (r < 0)
+                return r;
+        if (!netif) {
+                *ifindices = NULL;
+                return 0;
+        }
+
+        FOREACH_WORD(word, l, netif, state) {
+                char buf[l+1];
+                int ifi;
+
+                *(char*) (mempcpy(buf, word, l)) = 0;
+
+                if (safe_atoi(buf, &ifi) < 0)
+                        continue;
+                if (ifi <= 0)
+                        continue;
+
+                if (!GREEDY_REALLOC(ni, allocated, nr+1)) {
+                        free(ni);
+                        return -ENOMEM;
+                }
+
+                ni[nr++] = ifi;
+        }
+
+        *ifindices = ni;
+        return nr;
+}
+
 static inline int MONITOR_TO_FD(sd_login_monitor *m) {
         return (int) (unsigned long) m - 1;
 }