From d0e0cc557dba264c4228e29edb178d57b25af2f5 Mon Sep 17 00:00:00 2001
From: LiFeng <lifeng68@huawei.com>
Date: Wed, 22 Apr 2020 20:54:19 +0800
Subject: [PATCH] terminal: not close pipe when lxc_poll exit

Signed-off-by: LiFeng <lifeng68@huawei.com>
---
 src/lxc/attach.c           | 4 +++-
 src/lxc/terminal.c         | 5 +++--
 src/lxc/tools/lxc_attach.c | 3 +++
 3 files changed, 9 insertions(+), 3 deletions(-)

diff --git a/src/lxc/attach.c b/src/lxc/attach.c
index 5539fb1..51f5c08 100644
--- a/src/lxc/attach.c
+++ b/src/lxc/attach.c
@@ -1542,7 +1542,9 @@ int lxc_attach(struct lxc_container *container, lxc_attach_exec_t exec_function,
 			if (ret < 0)
 				goto on_error;
 #ifdef HAVE_ISULAD
-			(void)lxc_exec_cmd_mainloop_add(&descr, &exec_command);
+			if (suffix != NULL) {
+				(void)lxc_exec_cmd_mainloop_add(&descr, &exec_command);
+			}
 #endif
 			TRACE("Initialized terminal mainloop");
 		}
diff --git a/src/lxc/terminal.c b/src/lxc/terminal.c
index 57def93..0197503 100644
--- a/src/lxc/terminal.c
+++ b/src/lxc/terminal.c
@@ -760,12 +760,13 @@ int lxc_terminal_io_cb(int fd, uint32_t events, void *data,
 				}
 				terminal->pipes[2][0] = -EBADF;
 			}
-			close(fd);
+			/* notes: do not close the master fd due to if we close the fd, the process may
+			 * recive SIGHUP and the exit code will be 141 (128 + 13)
+			 */
 			return LXC_MAINLOOP_CLOSE;
 		} else if (fd == terminal->pipes[0][1]) {
 			TRACE("closed stdin pipe of container stdin");
 			terminal->pipes[0][1] = -EBADF;
-			close(fd);
 			return LXC_MAINLOOP_CLOSE;
 		} else {
 			ERROR("Handler received unexpected file descriptor");
diff --git a/src/lxc/tools/lxc_attach.c b/src/lxc/tools/lxc_attach.c
index da7a7d2..190f3c8 100644
--- a/src/lxc/tools/lxc_attach.c
+++ b/src/lxc/tools/lxc_attach.c
@@ -417,6 +417,9 @@ static int do_attach_foreground(struct lxc_container *c, lxc_attach_command_t *c
 		signal = WTERMSIG(ret);
 		wexit = EXIT_SIGNAL_OFFSET + signal;
 	}
+
+	ERROR("Execd pid %d exit with %d", pid, wexit);
+
 out:
 	if (c->lxc_conf->errmsg)
 		*errmsg = safe_strdup(c->lxc_conf->errmsg);
-- 
1.8.3.1

