diff --git a/man/sd_machine_get_class.xml b/man/sd_machine_get_class.xml
index 0a0d601899..a8db371230 100644
--- a/man/sd_machine_get_class.xml
+++ b/man/sd_machine_get_class.xml
@@ -53,21 +53,22 @@
project='man-pages'>free3
call after use.
- sd_machine_get_ifindices() may be used
- to determine the numeric indices of the network interfaces on the
- host that are pointing towards the specified locally running
- virtual machine or container that is registered with
+ sd_machine_get_ifindices() may be used to determine the numeric indices of the
+ network interfaces on the host that are pointing towards the specified locally running virtual machine or
+ container. The vm or container must be registered with
systemd-machined.service8.
- The returned array needs to be freed with the libc free3
- call after use.
+ The output parameter ret_ifindices may be passed as NULL when
+ the output value is not needed. The returned array needs to be freed with the libc free3 call after
+ use.
Return Value
- On success, these calls return 0 or a positive integer. On failure, these calls return a negative
- errno-style error code.
+ On success, these functions return a non-negative integer.
+ sd_machine_get_ifindices() returns the number of the relevant network interfaces.
+ On failure, these calls return a negative errno-style error code.
Errors
diff --git a/src/libsystemd/sd-login/sd-login.c b/src/libsystemd/sd-login/sd-login.c
index 3828fa58e4..601a27ab57 100644
--- a/src/libsystemd/sd-login/sd-login.c
+++ b/src/libsystemd/sd-login/sd-login.c
@@ -900,7 +900,6 @@ _public_ int sd_machine_get_ifindices(const char *machine, int **ret_ifindices)
int r;
assert_return(machine_name_is_valid(machine), -EINVAL);
- assert_return(ret_ifindices, -EINVAL);
p = strjoina("/run/systemd/machines/", machine);
r = parse_env_file(NULL, p, "NETIF", &netif_line);
@@ -918,9 +917,12 @@ _public_ int sd_machine_get_ifindices(const char *machine, int **ret_ifindices)
return -ENOMEM;
size_t n = 0;
- int *ifindices = new(int, strv_length(tt));
- if (!ifindices)
- return -ENOMEM;
+ int *ifindices;
+ if (ret_ifindices) {
+ ifindices = new(int, strv_length(tt));
+ if (!ifindices)
+ return -ENOMEM;
+ }
for (size_t i = 0; tt[i]; i++) {
int ind;
@@ -930,10 +932,13 @@ _public_ int sd_machine_get_ifindices(const char *machine, int **ret_ifindices)
/* Return -EUCLEAN to distinguish from -EINVAL for invalid args */
return ind == -EINVAL ? -EUCLEAN : ind;
- ifindices[n++] = ind;
+ if (ret_ifindices)
+ ifindices[n] = ind;
+ n++;
}
- *ret_ifindices = ifindices;
+ if (ret_ifindices)
+ *ret_ifindices = ifindices;
return n;
}