chiark / gitweb /
sd-login: always use "indices" as plural of "index"
[elogind.git] / src / libsystemd / sd-login / sd-login.c
index 375a5388aa73d37124a47b4b4b1ff2d66e47b058..38ff944892ca5abcf75c01add375b16759378703 100644 (file)
@@ -770,7 +770,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);
@@ -786,6 +786,49 @@ _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;
+        char *w, *state;
+        int *ni = NULL;
+        const char *p;
+        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(w, l, netif, state) {
+                char buf[l+1];
+                int ifi;
+
+                *(char*) (mempcpy(buf, w, 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;
 }