From de512a0c11bff1bfc8f0045e144bddc689742a21 Mon Sep 17 00:00:00 2001
From: LiFeng <lifeng68@huawei.com>
Date: Thu, 30 Apr 2020 16:40:14 +0800
Subject: [PATCH] attach: append error msg when call attach failed

Signed-off-by: LiFeng <lifeng68@huawei.com>
---
 src/lxc/tools/lxc_attach.c | 13 ++++++++++---
 1 file changed, 10 insertions(+), 3 deletions(-)

diff --git a/src/lxc/tools/lxc_attach.c b/src/lxc/tools/lxc_attach.c
index 190f3c8..dbddc2a 100644
--- a/src/lxc/tools/lxc_attach.c
+++ b/src/lxc/tools/lxc_attach.c
@@ -401,12 +401,17 @@ static int do_attach_foreground(struct lxc_container *c, lxc_attach_command_t *c
 		ret = c->attach(c, lxc_attach_run_command, command, attach_options, &pid);
 	else
 		ret = c->attach(c, lxc_attach_run_shell, NULL, attach_options, &pid);
-	if (ret < 0)
+	if (ret < 0) {
+		*errmsg = safe_strdup("Internal error, failed to call attach");
 		goto out;
+	}
 
 	ret = lxc_wait_for_pid_status(pid);
-	if (ret < 0)
+	if (ret < 0) {
+		free(*errmsg);
+		*errmsg = safe_strdup("Internal error, failed to wait attached process");
 		goto out;
+	}
 
 	if (WIFEXITED(ret))
 		wexit = WEXITSTATUS(ret);
@@ -421,8 +426,10 @@ static int do_attach_foreground(struct lxc_container *c, lxc_attach_command_t *c
 	ERROR("Execd pid %d exit with %d", pid, wexit);
 
 out:
-	if (c->lxc_conf->errmsg)
+	if (c->lxc_conf->errmsg) {
+		free(*errmsg);
 		*errmsg = safe_strdup(c->lxc_conf->errmsg);
+	}
 	return wexit;
 }
 
-- 
1.8.3.1

