Unverified Commit 4c9ffb3e by Wolfgang Bumiller Committed by GitHub

Merge pull request #3594 from brauner/2020-12-08/fixes

tree-wide: fixes and cleanups
parents d1042c9d ed1454e8
...@@ -13,7 +13,7 @@ extern "C" { ...@@ -13,7 +13,7 @@ extern "C" {
#include <sys/socket.h> #include <sys/socket.h>
#include "compiler.h" #include "compiler.h"
#include "netns_ifaddrs.h" #include "memory_utils.h"
struct netns_ifaddrs { struct netns_ifaddrs {
struct netns_ifaddrs *ifa_next; struct netns_ifaddrs *ifa_next;
...@@ -52,6 +52,7 @@ struct netns_ifaddrs { ...@@ -52,6 +52,7 @@ struct netns_ifaddrs {
#define __ifa_dstaddr ifa_ifu.ifu_dstaddr #define __ifa_dstaddr ifa_ifu.ifu_dstaddr
__hidden extern void netns_freeifaddrs(struct netns_ifaddrs *); __hidden extern void netns_freeifaddrs(struct netns_ifaddrs *);
define_cleanup_function(struct netns_ifaddrs *, netns_freeifaddrs);
__hidden extern int netns_getifaddrs(struct netns_ifaddrs **ifap, __s32 netns_id, __hidden extern int netns_getifaddrs(struct netns_ifaddrs **ifap, __s32 netns_id,
bool *netnsid_aware); bool *netnsid_aware);
......
...@@ -2340,20 +2340,21 @@ static char **do_lxcapi_get_interfaces(struct lxc_container *c) ...@@ -2340,20 +2340,21 @@ static char **do_lxcapi_get_interfaces(struct lxc_container *c)
char **interfaces = NULL; char **interfaces = NULL;
char interface[IFNAMSIZ]; char interface[IFNAMSIZ];
if (pipe2(pipefd, O_CLOEXEC) < 0) if (pipe2(pipefd, O_CLOEXEC))
return NULL; return log_error_errno(NULL, errno, "Failed to create pipe");
pid = fork(); pid = fork();
if (pid < 0) { if (pid < 0) {
SYSERROR("Failed to fork task to get interfaces information");
close(pipefd[0]); close(pipefd[0]);
close(pipefd[1]); close(pipefd[1]);
return NULL; return log_error_errno(NULL, errno, "Failed to fork task to get interfaces information");
} }
if (pid == 0) { /* child */ if (pid == 0) {
int ret = 1, nbytes; call_cleaner(netns_freeifaddrs) struct netns_ifaddrs *ifaddrs = NULL;
struct netns_ifaddrs *interfaceArray = NULL, *tempIfAddr = NULL; struct netns_ifaddrs *ifa = NULL;
int ret = 1;
int nbytes;
/* close the read-end of the pipe */ /* close the read-end of the pipe */
close(pipefd[0]); close(pipefd[0]);
...@@ -2364,15 +2365,15 @@ static char **do_lxcapi_get_interfaces(struct lxc_container *c) ...@@ -2364,15 +2365,15 @@ static char **do_lxcapi_get_interfaces(struct lxc_container *c)
} }
/* Grab the list of interfaces */ /* Grab the list of interfaces */
if (netns_getifaddrs(&interfaceArray, -1, &(bool){false})) { if (netns_getifaddrs(&ifaddrs, -1, &(bool){false})) {
SYSERROR("Failed to get interfaces list"); SYSERROR("Failed to get interfaces list");
goto out; goto out;
} }
/* Iterate through the interfaces */ /* Iterate through the interfaces */
for (tempIfAddr = interfaceArray; tempIfAddr != NULL; for (ifa = ifaddrs; ifa != NULL;
tempIfAddr = tempIfAddr->ifa_next) { ifa = ifa->ifa_next) {
nbytes = lxc_write_nointr(pipefd[1], tempIfAddr->ifa_name, IFNAMSIZ); nbytes = lxc_write_nointr(pipefd[1], ifa->ifa_name, IFNAMSIZ);
if (nbytes < 0) if (nbytes < 0)
goto out; goto out;
...@@ -2382,9 +2383,6 @@ static char **do_lxcapi_get_interfaces(struct lxc_container *c) ...@@ -2382,9 +2383,6 @@ static char **do_lxcapi_get_interfaces(struct lxc_container *c)
ret = 0; ret = 0;
out: out:
if (interfaceArray)
netns_freeifaddrs(interfaceArray);
/* close the write-end of the pipe, thus sending EOF to the reader */ /* close the write-end of the pipe, thus sending EOF to the reader */
close(pipefd[1]); close(pipefd[1]);
_exit(ret); _exit(ret);
...@@ -2405,7 +2403,7 @@ static char **do_lxcapi_get_interfaces(struct lxc_container *c) ...@@ -2405,7 +2403,7 @@ static char **do_lxcapi_get_interfaces(struct lxc_container *c)
count++; count++;
} }
if (wait_for_pid(pid) != 0) { if (wait_for_pid(pid)) {
for (i = 0; i < count; i++) for (i = 0; i < count; i++)
free(interfaces[i]); free(interfaces[i]);
...@@ -2436,10 +2434,8 @@ static char **do_lxcapi_get_ips(struct lxc_container *c, const char *interface, ...@@ -2436,10 +2434,8 @@ static char **do_lxcapi_get_ips(struct lxc_container *c, const char *interface,
char **addresses = NULL; char **addresses = NULL;
ret = pipe2(pipefd, O_CLOEXEC); ret = pipe2(pipefd, O_CLOEXEC);
if (ret < 0) { if (ret < 0)
SYSERROR("Failed to create pipe"); return log_error_errno(NULL, errno, "Failed to create pipe");
return NULL;
}
pid = fork(); pid = fork();
if (pid < 0) { if (pid < 0) {
...@@ -2450,11 +2446,12 @@ static char **do_lxcapi_get_ips(struct lxc_container *c, const char *interface, ...@@ -2450,11 +2446,12 @@ static char **do_lxcapi_get_ips(struct lxc_container *c, const char *interface,
} }
if (pid == 0) { if (pid == 0) {
call_cleaner(netns_freeifaddrs) struct netns_ifaddrs *ifaddrs = NULL;
struct netns_ifaddrs *ifa = NULL;
ssize_t nbytes; ssize_t nbytes;
char addressOutputBuffer[INET6_ADDRSTRLEN]; char addressOutputBuffer[INET6_ADDRSTRLEN];
char *address_ptr = NULL; char *address_ptr = NULL;
void *tempAddrPtr = NULL; void *address_ptr_tmp = NULL;
struct netns_ifaddrs *interfaceArray = NULL, *tempIfAddr = NULL;
/* close the read-end of the pipe */ /* close the read-end of the pipe */
close(pipefd[0]); close(pipefd[0]);
...@@ -2465,52 +2462,50 @@ static char **do_lxcapi_get_ips(struct lxc_container *c, const char *interface, ...@@ -2465,52 +2462,50 @@ static char **do_lxcapi_get_ips(struct lxc_container *c, const char *interface,
} }
/* Grab the list of interfaces */ /* Grab the list of interfaces */
if (netns_getifaddrs(&interfaceArray, -1, &(bool){false})) { if (netns_getifaddrs(&ifaddrs, -1, &(bool){false})) {
SYSERROR("Failed to get interfaces list"); SYSERROR("Failed to get interfaces list");
goto out; goto out;
} }
/* Iterate through the interfaces */ /* Iterate through the interfaces */
for (tempIfAddr = interfaceArray; tempIfAddr; for (ifa = ifaddrs; ifa; ifa = ifa->ifa_next) {
tempIfAddr = tempIfAddr->ifa_next) { if (ifa->ifa_addr == NULL)
if (tempIfAddr->ifa_addr == NULL)
continue; continue;
#pragma GCC diagnostic push #pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wcast-align" #pragma GCC diagnostic ignored "-Wcast-align"
if (tempIfAddr->ifa_addr->sa_family == AF_INET) { if (ifa->ifa_addr->sa_family == AF_INET) {
if (family && strcmp(family, "inet")) if (family && strcmp(family, "inet"))
continue; continue;
tempAddrPtr = &((struct sockaddr_in *)tempIfAddr->ifa_addr)->sin_addr; address_ptr_tmp = &((struct sockaddr_in *)ifa->ifa_addr)->sin_addr;
} else { } else {
if (family && strcmp(family, "inet6")) if (family && strcmp(family, "inet6"))
continue; continue;
if (((struct sockaddr_in6 *)tempIfAddr->ifa_addr)->sin6_scope_id != scope) if (((struct sockaddr_in6 *)ifa->ifa_addr)->sin6_scope_id != scope)
continue; continue;
tempAddrPtr = &((struct sockaddr_in6 *)tempIfAddr->ifa_addr)->sin6_addr; address_ptr_tmp = &((struct sockaddr_in6 *)ifa->ifa_addr)->sin6_addr;
} }
#pragma GCC diagnostic pop #pragma GCC diagnostic pop
if (interface && strcmp(interface, tempIfAddr->ifa_name)) if (interface && strcmp(interface, ifa->ifa_name))
continue; continue;
else if (!interface && strcmp("lo", tempIfAddr->ifa_name) == 0) else if (!interface && strcmp("lo", ifa->ifa_name) == 0)
continue; continue;
address_ptr = (char *)inet_ntop(tempIfAddr->ifa_addr->sa_family, address_ptr = (char *)inet_ntop(ifa->ifa_addr->sa_family, address_ptr_tmp,
tempAddrPtr, addressOutputBuffer, addressOutputBuffer,
sizeof(addressOutputBuffer)); sizeof(addressOutputBuffer));
if (!address_ptr) if (!address_ptr)
continue; continue;
nbytes = lxc_write_nointr(pipefd[1], address_ptr, INET6_ADDRSTRLEN); nbytes = lxc_write_nointr(pipefd[1], address_ptr, INET6_ADDRSTRLEN);
if (nbytes != INET6_ADDRSTRLEN) { if (nbytes != INET6_ADDRSTRLEN) {
SYSERROR("Failed to send ipv6 address \"%s\"", SYSERROR("Failed to send ipv6 address \"%s\"", address_ptr);
address_ptr);
goto out; goto out;
} }
...@@ -2520,9 +2515,6 @@ static char **do_lxcapi_get_ips(struct lxc_container *c, const char *interface, ...@@ -2520,9 +2515,6 @@ static char **do_lxcapi_get_ips(struct lxc_container *c, const char *interface,
ret = 0; ret = 0;
out: out:
if (interfaceArray)
netns_freeifaddrs(interfaceArray);
/* close the write-end of the pipe, thus sending EOF to the reader */ /* close the write-end of the pipe, thus sending EOF to the reader */
close(pipefd[1]); close(pipefd[1]);
_exit(ret); _exit(ret);
...@@ -2540,7 +2532,7 @@ static char **do_lxcapi_get_ips(struct lxc_container *c, const char *interface, ...@@ -2540,7 +2532,7 @@ static char **do_lxcapi_get_ips(struct lxc_container *c, const char *interface,
count++; count++;
} }
if (wait_for_pid(pid) != 0) { if (wait_for_pid(pid)) {
for (i = 0; i < count; i++) for (i = 0; i < count; i++)
free(addresses[i]); free(addresses[i]);
......
...@@ -201,12 +201,12 @@ extern int get_u16(unsigned short *val, const char *arg, int base) ...@@ -201,12 +201,12 @@ extern int get_u16(unsigned short *val, const char *arg, int base)
char *ptr; char *ptr;
if (!arg || !*arg) if (!arg || !*arg)
return -1; return ret_errno(EINVAL);
errno = 0; errno = 0;
res = strtoul(arg, &ptr, base); res = strtoul(arg, &ptr, base);
if (!ptr || ptr == arg || *ptr || res > 0xFFFF || errno != 0) if (!ptr || ptr == arg || *ptr || res > 0xFFFF || errno != 0)
return -1; return ret_errno(ERANGE);
*val = res; *val = res;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment