chiark / gitweb /
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)
committerSven Eden <yamakuzure@gmx.net>
Tue, 25 Jul 2017 07:46:52 +0000 (09:46 +0200)
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/libelogind/sd-login/sd-login.c
src/libelogind/sd-login/test-login.c

index 7630e6265334f3f7d60e4248bd0b728648650faa..5f90ba70c4121ab13fdec1077ee974b9caffab4f 100644 (file)
@@ -942,14 +942,13 @@ _public_ int sd_get_uids(uid_t **users) {
 }
 
 _public_ int sd_get_machine_names(char ***machines) {
 }
 
 _public_ int sd_get_machine_names(char ***machines) {
-        char **l = NULL, **a, **b;
+        char **l, **a, **b;
         int r;
 
         int r;
 
-        assert_return(machines, -EINVAL);
-
         r = get_files_in_directory("/run/systemd/machines/", &l);
         if (r == -ENOENT) {
         r = get_files_in_directory("/run/systemd/machines/", &l);
         if (r == -ENOENT) {
-                *machines = NULL;
+                if (machines)
+                        *machines = NULL;
                 return 0;
         }
         if (r < 0)
                 return 0;
         }
         if (r < 0)
@@ -959,7 +958,7 @@ _public_ int sd_get_machine_names(char ***machines) {
                 r = 0;
 
                 /* Filter out the unit: symlinks */
                 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 {
                         if (startswith(*a, "unit:") || !machine_name_is_valid(*a))
                                 free(*a);
                         else {
@@ -972,7 +971,8 @@ _public_ int sd_get_machine_names(char ***machines) {
                 *b = NULL;
         }
 
                 *b = NULL;
         }
 
-        *machines = l;
+        if (machines)
+                *machines = l;
         return r;
 }
 
         return r;
 }
 
index 3dc7975305ad6f81dbded23aec07e8626b6887b2..3eb11cf4a0ebdd8b0e0be08c9f28bed587bf7719 100644 (file)
@@ -251,8 +251,9 @@ static void test_login(void) {
                 assert_se(r >= 0);
                 assert_se(r == (int) strv_length(machines));
                 assert_se(buf = strv_join(machines, " "));
                 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);
                 log_info("sd_get_machines(…) → [%i] \"%s\"", r, buf);
+
+                assert_se(sd_get_machine_names(NULL) == r);
         }
 }
 
         }
 }