From 8d2f80168d89abc317affee358120dcf25b8af19 Mon Sep 17 00:00:00 2001
From: LiFeng <lifeng68@huawei.com>
Date: Tue, 14 Apr 2020 12:50:55 +0800
Subject: [PATCH 18/49] pty: setup pty after setup rootfs mount options

Signed-off-by: LiFeng <lifeng68@huawei.com>
---
 src/lxc/conf.c     | 16 +++++++++----
 src/lxc/start.c    |  2 +-
 src/lxc/terminal.c | 67 ++++++++++++++++++++++++++++++++++++------------------
 3 files changed, 57 insertions(+), 28 deletions(-)

diff --git a/src/lxc/conf.c b/src/lxc/conf.c
index fce241b..2e93227 100644
--- a/src/lxc/conf.c
+++ b/src/lxc/conf.c
@@ -3748,14 +3748,20 @@ int lxc_setup(struct lxc_handler *handler)
 	if (lxc_conf->autodev > 0)
 		(void)lxc_setup_boot_id();
 
+#ifdef HAVE_ISULAD
+	if (setup_rootfs_mountopts(&lxc_conf->rootfs)) {
+			return log_error(-1, "failed to set rootfs for '%s'", name);
+	}
+	if (lxc_conf->rootfs.path) {
+		ret = lxc_setup_devpts(lxc_conf);
+		if (ret < 0) {
+			return log_error(-1, "Failed to setup new devpts instance for '%s'", name);
+		}
+	}
+#else
 	ret = lxc_setup_devpts(lxc_conf);
 	if (ret < 0)
 		return log_error(-1, "Failed to setup new devpts instance");
-
-#ifdef HAVE_ISULAD
-        if (setup_rootfs_mountopts(&lxc_conf->rootfs)) {
-               return log_error(-1, "failed to set rootfs for '%s'", name);
-        }
 #endif
 
 	ret = lxc_create_ttys(handler);
diff --git a/src/lxc/start.c b/src/lxc/start.c
index 145b015..800f884 100644
--- a/src/lxc/start.c
+++ b/src/lxc/start.c
@@ -596,7 +596,7 @@ int lxc_poll(const char *name, struct lxc_handler *handler)
 		goto out_mainloop_console;
 
 	if (has_console)
-		ret = lxc_mainloop(&descr_console, 0);
+		ret = lxc_mainloop(&descr_console, 100);
 
 out_mainloop_console:
 	if (has_console) {
diff --git a/src/lxc/terminal.c b/src/lxc/terminal.c
index 4d7c2cd..1f46d49 100644
--- a/src/lxc/terminal.c
+++ b/src/lxc/terminal.c
@@ -186,6 +186,42 @@ static int lxc_terminal_truncate_log_file(struct lxc_terminal *terminal)
 	return lxc_unpriv(ftruncate(terminal->log_fd, 0));
 }
 
+#ifdef HAVE_ISULAD
+/*
+ * isulad: support mult-logfiles
+ * */
+static int lxc_terminal_rename_old_log_file(struct lxc_terminal *terminal)
+{
+	int ret;
+	unsigned int i;
+	char tmp[PATH_MAX] = {0};
+	char *rename_fname = NULL;
+
+	for (i = terminal->log_rotate - 1; i > 1; i--) {
+		ret = snprintf(tmp, PATH_MAX, "%s.%u", terminal->log_path, i);
+		if (ret < 0 || ret >= PATH_MAX) {
+			free(rename_fname);
+			return -EFBIG;
+		}
+		free(rename_fname);
+		rename_fname = safe_strdup(tmp);
+		ret = snprintf(tmp, PATH_MAX, "%s.%u", terminal->log_path, (i - 1));
+		if (ret < 0 || ret >= PATH_MAX) {
+			free(rename_fname);
+			return -EFBIG;
+		}
+		ret = lxc_unpriv(rename(tmp, rename_fname));
+		if (ret < 0 && errno != ENOENT) {
+			free(rename_fname);
+			return ret;
+		}
+	}
+
+	free(rename_fname);
+	return 0;
+}
+#endif
+
 static int lxc_terminal_rotate_log_file(struct lxc_terminal *terminal)
 {
 	__do_free char *tmp = NULL;
@@ -199,6 +235,15 @@ static int lxc_terminal_rotate_log_file(struct lxc_terminal *terminal)
 	if (terminal->log_fd < 0)
 		return -EBADF;
 
+#ifdef HAVE_ISULAD
+	/* isuald: rotate old log file first */
+	ret = lxc_terminal_rename_old_log_file(terminal);
+	if(ret != 0) {
+		ERROR("Rename old log file failed");
+		return ret;
+	}
+#endif
+
 	len = strlen(terminal->log_path) + sizeof(".1");
 	tmp = must_realloc(NULL, len);
 
@@ -1512,21 +1557,6 @@ static int lxc_terminal_fifo_default(struct lxc_terminal *terminal)
 	return 0;
 }
 
-static int use_unix_newline(int master_fd)
-{
-	struct termios oldtios;
-	int ret;
-
-	ret = tcgetattr(master_fd, &oldtios);
-	if (ret < 0)
-		return -1;
-	oldtios.c_oflag &= ~ONLCR;
-	ret = tcsetattr(master_fd, TCSAFLUSH, &oldtios);
-	if (ret < 0)
-		return -1;
-	return 0;
-}
-
 int lxc_terminal_create(struct lxc_terminal *terminal)
 {
 	int ret;
@@ -1544,13 +1574,6 @@ int lxc_terminal_create(struct lxc_terminal *terminal)
 			goto err;
 		}
 
-		/* isulad: clear ONLCR flag */
-		ret = use_unix_newline(terminal->master);
-		if (ret < 0) {
-			SYSERROR("Failed to clear ONLCR flag on terminal master");
-			goto err;
-		}
-
 		ret = fd_cloexec(terminal->master, true);
 		if (ret < 0) {
 			SYSERROR("Failed to set FD_CLOEXEC flag on terminal master");
-- 
1.8.3.1

