]> git.ipfire.org Git - thirdparty/lxc.git/commitdiff
nl: save errno on lxc_netns_set_nsid()
authorChristian Brauner <christian.brauner@ubuntu.com>
Mon, 3 Sep 2018 21:16:13 +0000 (23:16 +0200)
committerChristian Brauner <christian.brauner@ubuntu.com>
Tue, 4 Sep 2018 12:05:28 +0000 (14:05 +0200)
Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
src/lxc/network.c
src/lxc/nl.c
src/lxc/start.c

index 7c965382009c8a396df950965fe3da36a1aa69c9..c4e292e12857c1ab1bd202bce73ca96ceea12367 100644 (file)
@@ -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;
 
index 9625d14533d51bafb73fd75fdcb75f0d64ac63fd..bf2452f0e3218709ee3a8c759ec2fb3b6240ac0f 100644 (file)
@@ -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 <sys/socket.h>
 #include <string.h>
 #include <stdio.h>
 #include <linux/netlink.h>
 #include <linux/rtnetlink.h>
 
+#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,
index 4fe5c8d85ecda6b7b13bd3c2f431acc5912d260e..66a801eaf21387e8ef473e9f0d5a03e26c606164 100644 (file)
@@ -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. */