]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
sd-login: sd_get_machine_names(): do not return -EINVAL when output parameter is...
authorYu Watanabe <watanabe.yu+github@gmail.com>
Wed, 31 May 2017 15:12:32 +0000 (00:12 +0900)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Wed, 31 May 2017 23:22:38 +0000 (19:22 -0400)
Other functions in sd-login generally allow the output parameter to be NULL, in
which case only the number of items that would be stored in the array is returned.
Be nice and do the same here.

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

index ce080acd4c7fca52b03c473d73aa710a45f1a9a8..b75acef82c09675f7d9dfb221ba6bed8f1813fdf 100644 (file)
@@ -878,14 +878,13 @@ _public_ int sd_get_uids(uid_t **users) {
 }
 
 _public_ int sd_get_machine_names(char ***machines) {
-        char **l = NULL, **a, **b;
+        char **l, **a, **b;
         int r;
 
-        assert_return(machines, -EINVAL);
-
         r = get_files_in_directory("/run/systemd/machines/", &l);
         if (r == -ENOENT) {
-                *machines = NULL;
+                if (machines)
+                        *machines = NULL;
                 return 0;
         }
         if (r < 0)
@@ -895,7 +894,7 @@ _public_ int sd_get_machine_names(char ***machines) {
                 r = 0;
 
                 /* Filter out the unit: symlinks */
-                for (a = l, b = l; *a; a++) {
+                for (a = b = l; *a; a++) {
                         if (startswith(*a, "unit:") || !machine_name_is_valid(*a))
                                 free(*a);
                         else {
@@ -908,7 +907,8 @@ _public_ int sd_get_machine_names(char ***machines) {
                 *b = NULL;
         }
 
-        *machines = l;
+        if (machines)
+                *machines = l;
         return r;
 }
 
index bc8488c4de60bbac0c2e051c748cb92417884511..b618b79b2812bec13fb710273690034b0be488e5 100644 (file)
@@ -249,8 +249,9 @@ static void test_login(void) {
                 assert_se(r >= 0);
                 assert_se(r == (int) strv_length(machines));
                 assert_se(buf = strv_join(machines, " "));
-
                 log_info("sd_get_machines(…) → [%i] \"%s\"", r, buf);
+
+                assert_se(sd_get_machine_names(NULL) == r);
         }
 }