From: Yu Watanabe Date: Wed, 31 May 2017 15:12:32 +0000 (+0900) Subject: sd-login: sd_get_machine_names(): do not return -EINVAL when output parameter is... X-Git-Tag: v234~150^2~1 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=76ed21e1e68493b60167fb746872d0dd11278bce;p=thirdparty%2Fsystemd.git sd-login: sd_get_machine_names(): do not return -EINVAL when output parameter is NULL 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. --- diff --git a/src/libsystemd/sd-login/sd-login.c b/src/libsystemd/sd-login/sd-login.c index ce080acd4c7..b75acef82c0 100644 --- a/src/libsystemd/sd-login/sd-login.c +++ b/src/libsystemd/sd-login/sd-login.c @@ -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; } diff --git a/src/libsystemd/sd-login/test-login.c b/src/libsystemd/sd-login/test-login.c index bc8488c4de6..b618b79b281 100644 --- a/src/libsystemd/sd-login/test-login.c +++ b/src/libsystemd/sd-login/test-login.c @@ -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); } }