From 1e2ac640b439da580d364e69ebdd2cfc344c6cd5 Mon Sep 17 00:00:00 2001
From: liuhao <liuhao27@huawei.com>
Date: Fri, 11 Jan 2019 16:11:34 +0800
Subject: [PATCH 004/131] support isulad fifo log

support isulad fifo log in lxc3.0

Signed-off-by: LiFeng <lifeng68@huawei.com>
---
 src/lxc/log.c | 42 ++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 40 insertions(+), 2 deletions(-)

diff --git a/src/lxc/log.c b/src/lxc/log.c
index 1e0cc6a6..4e744599 100644
--- a/src/lxc/log.c
+++ b/src/lxc/log.c
@@ -68,6 +68,7 @@ static int syslog_enable = 0;
 int lxc_quiet_specified;
 int lxc_log_use_global_fd;
 static int lxc_loglevel_specified;
+static bool isulad_use_log_fifo_flag = false;
 
 static char log_prefix[LXC_LOG_PREFIX_SIZE] = "lxc";
 static char *log_fname = NULL;
@@ -138,6 +139,37 @@ static char *lxc_log_get_va_msg(struct lxc_log_event *event)
 	return msg;
 }
 
+static const char *isulad_use_log_fifo(const char *file)
+{
+#define ISULAD_FIFO_PREFIX "fifo:"
+
+	if (strncmp(file, ISULAD_FIFO_PREFIX, strlen(ISULAD_FIFO_PREFIX)) == 0) {
+		isulad_use_log_fifo_flag = true;
+		return (file + strlen(ISULAD_FIFO_PREFIX));
+	}
+	return file;
+}
+
+static int isulad_open_fifo(const char *file_path)
+{
+#define LOG_FIFO_SIZE (1024 * 1024)
+	int fd = -1;
+
+	fd = lxc_unpriv(open(file_path, O_RDWR | O_NONBLOCK | O_CLOEXEC, 0640));
+	if (fd == -1) {
+		fprintf(stderr, "Open fifo %s failed: %s\n", file_path, strerror(errno));
+		return -1;
+	}
+
+	if (fcntl(fd, F_SETPIPE_SZ, LOG_FIFO_SIZE) == -1) {
+		printf("Set fifo buffer size failed: %s", strerror(errno));
+		close(fd);
+		return -1;
+	}
+
+	return fd;
+}
+
 /*---------------------------------------------------------------------------*/
 static int log_append_syslog(const struct lxc_log_appender *appender,
 			     struct lxc_log_event *event)
@@ -609,7 +641,11 @@ static int __lxc_log_set_file(const char *fname, int create_dirs)
 		return -1;
 	}
 
-	lxc_log_fd = log_open(fname);
+	if (isulad_use_log_fifo_flag) {
+		lxc_log_fd = isulad_open_fifo(fname);
+	} else {
+		lxc_log_fd = log_open(fname);
+	}
 	if (lxc_log_fd == -1)
 		return -1;
 
@@ -642,6 +678,7 @@ int lxc_log_init(struct lxc_log *log)
 {
 	int ret;
 	int lxc_priority = LXC_LOG_LEVEL_ERROR;
+	const char *tmp_log_fname;
 
 	if (!log)
 		return -1;
@@ -673,7 +710,8 @@ int lxc_log_init(struct lxc_log *log)
 		if (strcmp(log->file, "none") == 0)
 			return 0;
 
-		ret = __lxc_log_set_file(log->file, 1);
+		tmp_log_fname = isulad_use_log_fifo(log->file);
+		ret = __lxc_log_set_file(tmp_log_fname, 1);
 		if (ret < 0) {
 			ERROR("Failed to enable logfile");
 			return -1;
-- 
2.23.0

