Let sd_machine_get_ifindices() omit the output param too

Nowadays we do that almost everywhere, let's also do it here.
This commit is contained in:
Zbigniew Jędrzejewski-Szmek 2020-07-30 13:08:52 +02:00
parent 0ef14adc1c
commit dd630d3cac
2 changed files with 21 additions and 15 deletions

View File

@ -53,21 +53,22 @@
project='man-pages'><refentrytitle>free</refentrytitle><manvolnum>3</manvolnum></citerefentry> project='man-pages'><refentrytitle>free</refentrytitle><manvolnum>3</manvolnum></citerefentry>
call after use.</para> call after use.</para>
<para><function>sd_machine_get_ifindices()</function> may be used <para><function>sd_machine_get_ifindices()</function> may be used to determine the numeric indices of the
to determine the numeric indices of the network interfaces on the network interfaces on the host that are pointing towards the specified locally running virtual machine or
host that are pointing towards the specified locally running container. The vm or container must be registered with
virtual machine or container that is registered with
<citerefentry><refentrytitle>systemd-machined.service</refentrytitle><manvolnum>8</manvolnum></citerefentry>. <citerefentry><refentrytitle>systemd-machined.service</refentrytitle><manvolnum>8</manvolnum></citerefentry>.
The returned array needs to be freed with the libc <citerefentry The output parameter <parameter>ret_ifindices</parameter> may be passed as <constant>NULL</constant> when
project='man-pages'><refentrytitle>free</refentrytitle><manvolnum>3</manvolnum></citerefentry> the output value is not needed. The returned array needs to be freed with the libc <citerefentry
call after use.</para> project='man-pages'><refentrytitle>free</refentrytitle><manvolnum>3</manvolnum></citerefentry> call after
use.</para>
</refsect1> </refsect1>
<refsect1> <refsect1>
<title>Return Value</title> <title>Return Value</title>
<para>On success, these calls return 0 or a positive integer. On failure, these calls return a negative <para>On success, these functions return a non-negative integer.
errno-style error code.</para> <function>sd_machine_get_ifindices()</function> returns the number of the relevant network interfaces.
On failure, these calls return a negative errno-style error code.</para>
<refsect2> <refsect2>
<title>Errors</title> <title>Errors</title>

View File

@ -900,7 +900,6 @@ _public_ int sd_machine_get_ifindices(const char *machine, int **ret_ifindices)
int r; int r;
assert_return(machine_name_is_valid(machine), -EINVAL); assert_return(machine_name_is_valid(machine), -EINVAL);
assert_return(ret_ifindices, -EINVAL);
p = strjoina("/run/systemd/machines/", machine); p = strjoina("/run/systemd/machines/", machine);
r = parse_env_file(NULL, p, "NETIF", &netif_line); 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; return -ENOMEM;
size_t n = 0; size_t n = 0;
int *ifindices = new(int, strv_length(tt)); int *ifindices;
if (!ifindices) if (ret_ifindices) {
return -ENOMEM; ifindices = new(int, strv_length(tt));
if (!ifindices)
return -ENOMEM;
}
for (size_t i = 0; tt[i]; i++) { for (size_t i = 0; tt[i]; i++) {
int ind; 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 -EUCLEAN to distinguish from -EINVAL for invalid args */
return ind == -EINVAL ? -EUCLEAN : ind; 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; return n;
} }