[PATCH 2/2] tgtd: support log file for tgtd foreground mode

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Running tgtd as init process in a docker, command like this:
	# docker run -it tgt:v2 /sbin/tgtd -f
We must run tgtd in foreground mode, because init process can not be
killed. And in this mode, wo can not get tgtd error log from syslog.
So support log file for tgtd foreground mode, command like this:
	# docker run -it tgt:v2 /sbin/tgtd -f --log-file /var/log/tgtd.log

Signed-off-by: zhenwei pi <pizhenwei@xxxxxxxxxxxxx>
---
 usr/log.c  | 16 +++++++++++++++-
 usr/log.h  |  3 ++-
 usr/tgtd.c | 15 ++++++++++++---
 3 files changed, 29 insertions(+), 5 deletions(-)

diff --git a/usr/log.c b/usr/log.c
index 2f0c4d4..a178198 100644
--- a/usr/log.c
+++ b/usr/log.c
@@ -25,6 +25,7 @@
 #include <syslog.h>
 #include <signal.h>
 #include <errno.h>
+#include <fcntl.h>
 #include <sys/shm.h>
 #include <sys/ipc.h>
 #include <sys/types.h>
@@ -354,7 +355,7 @@ static void log_sigsegv(void)
 	exit(1);
 }
 
-int log_init(char *program_name, int size, int daemon, int debug)
+int log_init(char *program_name, int size, int daemon, int debug, char *logfile)
 {
 	is_debug = debug;
 
@@ -400,6 +401,19 @@ int log_init(char *program_name, int size, int daemon, int debug)
 		}
 
 		exit(0);
+	} else if (logfile != NULL) {
+		int mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
+		int fd;
+
+		fd = open(logfile, O_WRONLY | O_CREAT | O_APPEND, mode);
+		if (fd < 0) {
+			log_error("failed to open %s, %m, log to stderr\n",
+					logfile);
+			return 0;
+		}
+
+		dup2(fd, fileno(stderr));
+		close(fd);
 	}
 
 	return 0;
diff --git a/usr/log.h b/usr/log.h
index 5f8d7bd..b94b164 100644
--- a/usr/log.h
+++ b/usr/log.h
@@ -62,7 +62,8 @@ struct logarea {
 	union semun semarg;
 };
 
-extern int log_init(char *progname, int size, int daemon, int debug);
+extern int log_init(char *progname, int size, int daemon, int debug,
+		char *logfile);
 extern void log_close(void);
 extern void dump_logmsg(void *);
 extern void log_warning(const char *fmt, ...)
diff --git a/usr/tgtd.c b/usr/tgtd.c
index dd6e6f2..41f9bfb 100644
--- a/usr/tgtd.c
+++ b/usr/tgtd.c
@@ -56,13 +56,14 @@ static struct option const long_options[] = {
 	{"control-port", required_argument, 0, 'C'},
 	{"nr_iothreads", required_argument, 0, 't'},
 	{"pid-file", required_argument, 0, 'p'},
+	{"log-file", required_argument, 0, 'l'},
 	{"debug", required_argument, 0, 'd'},
 	{"version", no_argument, 0, 'V'},
 	{"help", no_argument, 0, 'h'},
 	{0, 0, 0, 0},
 };
 
-static char *short_options = "fC:d:t:p:Vh";
+static char *short_options = "fC:d:t:p:l:Vh";
 static char *spare_args;
 
 static void usage(int status)
@@ -549,7 +550,7 @@ int main(int argc, char **argv)
 	int err, ch, longindex, nr_lld = 0;
 	int is_daemon = 1, is_debug = 0;
 	int ret;
-	char *pidfile = NULL;
+	char *pidfile = NULL, *logfile = NULL;
 
 	sa_new.sa_handler = signal_catch;
 	sigemptyset(&sa_new.sa_mask);
@@ -587,6 +588,13 @@ int main(int argc, char **argv)
 				exit(1);
 			}
 			break;
+		case 'l':
+			logfile = strdup(optarg);
+			if (logfile == NULL) {
+				fprintf(stderr, "can't duplicate log file\n");
+				exit(1);
+			}
+			break;
 		case 'd':
 			ret = str_to_int_range(optarg, is_debug, 0, 1);
 			if (ret)
@@ -625,7 +633,8 @@ int main(int argc, char **argv)
 	if (err)
 		exit(1);
 
-	err = log_init(program_name, LOG_SPACE_SIZE, is_daemon, is_debug);
+	err = log_init(program_name, LOG_SPACE_SIZE, is_daemon, is_debug,
+			logfile);
 	if (err)
 		exit(1);
 
-- 
2.11.0




[Index of Archives]     [Linux SCSI]     [Linux RAID]     [Linux Clusters]     [Linux USB Devel]     [Linux Audio Users]     [Yosemite News]     [Linux Kernel]

  Powered by Linux