]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
Let sd_machine_get_ifindices() omit the output param too
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Thu, 30 Jul 2020 11:08:52 +0000 (13:08 +0200)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Wed, 9 Sep 2020 07:34:54 +0000 (09:34 +0200)
Nowadays we do that almost everywhere, let's also do it here.

man/sd_machine_get_class.xml
src/libsystemd/sd-login/sd-login.c

index 0a0d601899c2880efbee4d369bbc48926002c215..a8db371230b956111f75ac6d98ad0d1e49cb9cee 100644 (file)
     project='man-pages'><refentrytitle>free</refentrytitle><manvolnum>3</manvolnum></citerefentry>
     call after use.</para>
 
-    <para><function>sd_machine_get_ifindices()</function> 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
+    <para><function>sd_machine_get_ifindices()</function> 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
     <citerefentry><refentrytitle>systemd-machined.service</refentrytitle><manvolnum>8</manvolnum></citerefentry>.
-    The returned array needs to be freed with the libc <citerefentry
-    project='man-pages'><refentrytitle>free</refentrytitle><manvolnum>3</manvolnum></citerefentry>
-    call after use.</para>
+    The output parameter <parameter>ret_ifindices</parameter> may be passed as <constant>NULL</constant> when
+    the output value is not needed. The returned array needs to be freed with the libc <citerefentry
+    project='man-pages'><refentrytitle>free</refentrytitle><manvolnum>3</manvolnum></citerefentry> call after
+    use.</para>
   </refsect1>
 
   <refsect1>
     <title>Return Value</title>
 
-    <para>On success, these calls return 0 or a positive integer. On failure, these calls return a negative
-    errno-style error code.</para>
+    <para>On success, these functions return a non-negative integer.
+    <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>
       <title>Errors</title>
index 3828fa58e48ad0219aebb5ccbba0cfcf6986e869..601a27ab570b9ad8b3de968831638f3143996151 100644 (file)
@@ -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;
 }