1
1
mirror of https://github.com/systemd/systemd-stable.git synced 2024-12-23 17:34:00 +03:00

Rewrite sd_machine_get_ifindices() to avoid FOREACH_WORD()

If we fail to parse the index, the failure is propogated as -EUNCLEAN.
(-EINVAL would be confused with invalid args to the function itself.)
This commit is contained in:
Zbigniew Jędrzejewski-Szmek 2020-07-30 12:56:51 +02:00
parent aa3b40c3f9
commit 0ef14adc1c
3 changed files with 25 additions and 25 deletions

View File

@ -35,7 +35,7 @@
<funcprototype> <funcprototype>
<funcdef>int <function>sd_machine_get_ifindices</function></funcdef> <funcdef>int <function>sd_machine_get_ifindices</function></funcdef>
<paramdef>const char* <parameter>machine</parameter></paramdef> <paramdef>const char* <parameter>machine</parameter></paramdef>
<paramdef>int **<parameter>ifindices</parameter></paramdef> <paramdef>int **<parameter>ret_ifindices</parameter></paramdef>
</funcprototype> </funcprototype>
</funcsynopsis> </funcsynopsis>
</refsynopsisdiv> </refsynopsisdiv>

View File

@ -894,47 +894,47 @@ _public_ int sd_machine_get_class(const char *machine, char **class) {
return 0; return 0;
} }
_public_ int sd_machine_get_ifindices(const char *machine, int **ifindices) { _public_ int sd_machine_get_ifindices(const char *machine, int **ret_ifindices) {
_cleanup_free_ char *netif = NULL; _cleanup_free_ char *netif_line = NULL;
size_t l, allocated = 0, nr = 0; const char *p;
int *ni = NULL;
const char *p, *word, *state;
int r; int r;
assert_return(machine_name_is_valid(machine), -EINVAL); assert_return(machine_name_is_valid(machine), -EINVAL);
assert_return(ifindices, -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); r = parse_env_file(NULL, p, "NETIF", &netif_line);
if (r == -ENOENT) if (r == -ENOENT)
return -ENXIO; return -ENXIO;
if (r < 0) if (r < 0)
return r; return r;
if (!netif) { if (!netif_line) {
*ifindices = NULL; *ret_ifindices = NULL;
return 0; return 0;
} }
FOREACH_WORD(word, l, netif, state) { _cleanup_strv_free_ char **tt = strv_split(netif_line, NULL);
char buf[l+1]; if (!tt)
int ifi; return -ENOMEM;
*(char*) (mempcpy(buf, word, l)) = 0; size_t n = 0;
int *ifindices = new(int, strv_length(tt));
if (!ifindices)
return -ENOMEM;
ifi = parse_ifindex(buf); for (size_t i = 0; tt[i]; i++) {
if (ifi < 0) int ind;
continue;
if (!GREEDY_REALLOC(ni, allocated, nr+1)) { ind = parse_ifindex(tt[i]);
free(ni); if (ind < 0)
return -ENOMEM; /* Return -EUCLEAN to distinguish from -EINVAL for invalid args */
} return ind == -EINVAL ? -EUCLEAN : ind;
ni[nr++] = ifi; ifindices[n++] = ind;
} }
*ifindices = ni; *ret_ifindices = ifindices;
return nr; return n;
} }
static int MONITOR_TO_FD(sd_login_monitor *m) { static int MONITOR_TO_FD(sd_login_monitor *m) {

View File

@ -199,7 +199,7 @@ int sd_seat_can_graphical(const char *seat);
int sd_machine_get_class(const char *machine, char **clazz); int sd_machine_get_class(const char *machine, char **clazz);
/* Return the list if host-side network interface indices of a machine */ /* Return the list if host-side network interface indices of a machine */
int sd_machine_get_ifindices(const char *machine, int **ifindices); int sd_machine_get_ifindices(const char *machine, int **ret_ifindices);
/* Get all seats, store in *seats. Returns the number of seats. If /* Get all seats, store in *seats. Returns the number of seats. If
* seats is NULL, this only returns the number of seats. */ * seats is NULL, this only returns the number of seats. */