[lxc-devel] [lxc/master] lxc-attach: fix cleanup code

brauner on Github lxc-bot at linuxcontainers.org
Thu Feb 25 14:31:37 UTC 2016


A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 462 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20160225/e17fff44/attachment.bin>
-------------- next part --------------
From 653ed513eb25c84ec3db4e528dc3cdb1a13b3a1f Mon Sep 17 00:00:00 2001
From: Christian Brauner <christian.brauner at mailbox.org>
Date: Thu, 25 Feb 2016 15:22:53 +0100
Subject: [PATCH] lxc-attach: fix cleanup code

Previously, we called lxc_container_put() before waitpid(). Let's do it the
other way around.

Signed-off-by: Christian Brauner <christian.brauner at mailbox.org>
---
 src/lxc/lxc_attach.c | 14 ++++++++------
 1 file changed, 8 insertions(+), 6 deletions(-)

diff --git a/src/lxc/lxc_attach.c b/src/lxc/lxc_attach.c
index 3439b2f..6501140 100644
--- a/src/lxc/lxc_attach.c
+++ b/src/lxc/lxc_attach.c
@@ -475,6 +475,7 @@ static int pty_on_host(struct lxc_container *c, struct wrapargs *wrap, int *pid)
 int main(int argc, char *argv[])
 {
 	int ret = -1;
+	int wexit = 0;
 	pid_t pid;
 	lxc_attach_options_t attach_options = LXC_ATTACH_OPTIONS_DEFAULT;
 	lxc_attach_command_t command = (lxc_attach_command_t){.program = NULL};
@@ -554,17 +555,18 @@ int main(int argc, char *argv[])
 			ret = c->attach(c, lxc_attach_run_shell, NULL, &attach_options, &pid);
 	}
 
-	lxc_container_put(c);
-
 	if (ret < 0)
-		exit(EXIT_FAILURE);
+		goto out;
 
 	ret = lxc_wait_for_pid_status(pid);
 	if (ret < 0)
-		exit(EXIT_FAILURE);
+		goto out;
 
 	if (WIFEXITED(ret))
-		return WEXITSTATUS(ret);
-
+		wexit = WEXITSTATUS(ret);
+out:
+	lxc_container_put(c);
+	if (ret >= 0)
+		exit(wexit);
 	exit(EXIT_FAILURE);
 }


More information about the lxc-devel mailing list