From: Christian Brauner Date: Mon, 3 Sep 2018 21:16:13 +0000 (+0200) Subject: nl: save errno on lxc_netns_set_nsid() X-Git-Tag: lxc-3.1.0~118^2~1 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=bfcedc7ea07d360221c832ad94943057e3add923;p=thirdparty%2Flxc.git nl: save errno on lxc_netns_set_nsid() Signed-off-by: Christian Brauner --- diff --git a/src/lxc/network.c b/src/lxc/network.c index 7c9653820..c4e292e12 100644 --- a/src/lxc/network.c +++ b/src/lxc/network.c @@ -3203,6 +3203,7 @@ int lxc_netns_set_nsid(int fd) struct nl_handler nlh; struct nlmsghdr *hdr; struct rtgenmsg *msg; + int saved_errno; __s32 ns_id = -1; __u32 netns_fd = fd; @@ -3225,7 +3226,9 @@ int lxc_netns_set_nsid(int fd) addattr(hdr, 1024, __LXC_NETNSA_NSID, &ns_id, sizeof(ns_id)); ret = __netlink_transaction(&nlh, hdr, hdr); + saved_errno = errno; netlink_close(&nlh); + errno = saved_errno; if (ret < 0) return -1; diff --git a/src/lxc/nl.c b/src/lxc/nl.c index 9625d1453..bf2452f0e 100644 --- a/src/lxc/nl.c +++ b/src/lxc/nl.c @@ -20,6 +20,9 @@ * License along with this library; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ + +#include "config.h" + #include #include #include @@ -30,8 +33,11 @@ #include #include +#include "log.h" #include "nl.h" +lxc_log_define(nl, lxc); + extern size_t nlmsg_len(const struct nlmsg *nlmsg) { return nlmsg->nlmsghdr->nlmsg_len - NLMSG_HDRLEN; @@ -201,8 +207,10 @@ again: if (!ret) return 0; - if (msg.msg_flags & MSG_TRUNC && (ret == nlmsghdr->nlmsg_len)) - return -EMSGSIZE; + if (msg.msg_flags & MSG_TRUNC && (ret == nlmsghdr->nlmsg_len)) { + errno = EMSGSIZE; + ret = -1; + } return ret; } @@ -252,18 +260,21 @@ extern int __netlink_transaction(struct nl_handler *handler, ret = __netlink_send(handler, request); if (ret < 0) - return ret; + return -1; ret = __netlink_recv(handler, answer); if (ret < 0) - return ret; + return -1; + ret = 0; if (answer->nlmsg_type == NLMSG_ERROR) { struct nlmsgerr *err = (struct nlmsgerr *)NLMSG_DATA(answer); - return err->error; + errno = -err->error; + if (err->error < 0) + ret = -1; } - return 0; + return ret; } extern int netlink_transaction(struct nl_handler *handler, diff --git a/src/lxc/start.c b/src/lxc/start.c index 4fe5c8d85..66a801eaf 100644 --- a/src/lxc/start.c +++ b/src/lxc/start.c @@ -1789,12 +1789,10 @@ static int lxc_spawn(struct lxc_handler *handler) DEBUG("Preserved net namespace via fd %d", ret); ret = lxc_netns_set_nsid(handler->nsfd[LXC_NS_NET]); - if (ret < 0) { - errno = -ret; + if (ret < 0) SYSERROR("Failed to allocate new network namespace id"); - } else { + else TRACE("Allocated new network namespace id"); - } } /* Create the network configuration. */