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.
This commit is contained in:
Yu Watanabe 2017-06-01 00:12:32 +09:00 committed by Zbigniew Jędrzejewski-Szmek
parent 2b5e92673d
commit 76ed21e1e6
2 changed files with 8 additions and 7 deletions

View 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;
}

View 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);
}
}