From d19376d8735651b23394cdeb560cbebe374c8bb9 Mon Sep 17 00:00:00 2001
From: wujing <wujing50@huawei.com>
Date: Wed, 21 Oct 2020 15:34:50 +0800
Subject: [PATCH 2/3] Streaming IO solution optimization and enhancement

Signed-off-by: wujing <wujing50@huawei.com>
---
 src/lxc/file_utils.c | 27 +++++++++++++++++++++++++++
 src/lxc/file_utils.h |  4 ++++
 src/lxc/terminal.c   | 14 ++++++++++----
 3 files changed, 41 insertions(+), 4 deletions(-)

diff --git a/src/lxc/file_utils.c b/src/lxc/file_utils.c
index 1689cbaa..2dbbbd3b 100644
--- a/src/lxc/file_utils.c
+++ b/src/lxc/file_utils.c
@@ -122,6 +122,33 @@ int lxc_read_from_file(const char *filename, void *buf, size_t count)
 	return ret;
 }
 
+#ifdef HAVE_ISULAD
+ssize_t lxc_write_nointr_for_fifo(int fd, const void *buf, size_t count)
+{
+    ssize_t nret = 0;
+    ssize_t nwritten;
+
+    if (buf == NULL) {
+        return -1;
+    }
+
+    for (nwritten = 0; nwritten < count;) {
+        nret = write(fd, buf + nwritten, count - nwritten);
+        if (nret < 0) {
+                if (errno == EINTR || errno == EAGAIN) {
+                        continue;
+                } else {
+                        return nret;
+                }
+        } else {
+                nwritten += nret;
+        }
+    }
+
+    return nwritten;
+}
+#endif
+
 ssize_t lxc_write_nointr(int fd, const void *buf, size_t count)
 {
 	ssize_t ret;
diff --git a/src/lxc/file_utils.h b/src/lxc/file_utils.h
index 6d5dbf68..29162b3f 100644
--- a/src/lxc/file_utils.h
+++ b/src/lxc/file_utils.h
@@ -58,4 +58,8 @@ extern FILE *fdopen_cached(int fd, const char *mode, void **caller_freed_buffer)
 extern FILE *fopen_cached(const char *path, const char *mode,
 			  void **caller_freed_buffer);
 
+#ifdef HAVE_ISULAD
+extern ssize_t lxc_write_nointr_for_fifo(int fd, const void *buf, size_t count);
+#endif
+
 #endif /* __LXC_FILE_UTILS_H */
diff --git a/src/lxc/terminal.c b/src/lxc/terminal.c
index 7441de79..a4c6ad0c 100644
--- a/src/lxc/terminal.c
+++ b/src/lxc/terminal.c
@@ -683,11 +683,17 @@ static void lxc_forward_data_to_fifo(struct lxc_list *list, bool is_err, const c
 	lxc_list_for_each_safe(it, list, next) {
 		elem = it->elem;
 		if (is_err) {
-			if (elem->err_fd >= 0)
-				lxc_write_nointr(elem->err_fd, buf, r);
+			if (elem->err_fd >= 0) {
+				if (lxc_write_nointr_for_fifo(elem->err_fd, buf, r) < 0) {
+					ERROR("Failed to write to fifo fd %d with error: %s", elem->err_fd, strerror(errno));
+				}
+			}
 		} else {
-			if (elem->out_fd >= 0)
-				lxc_write_nointr(elem->out_fd, buf, r);
+			if (elem->out_fd >= 0) {
+				if (lxc_write_nointr_for_fifo(elem->out_fd, buf, r) < 0) {
+					ERROR("Failed to write to fifo fd %d with error: %s", elem->out_fd, strerror(errno));
+				}
+			}
 		}
 	}
 
-- 
2.25.1

