[sheepdog] [PATCH] logger: init log formatter for collie

Liu Yuan namei.unix at gmail.com
Wed Feb 6 08:53:16 CET 2013


From: Liu Yuan <tailai.ly at taobao.com>

Collie doesn't call select_log_formatter(), so some functions shared by collie
sheep such as connect_to() will call into logger, which then leade to segfault.

We should init log formatter on our own, collie dosen't need bother initinig it.

Signed-off-by: Liu Yuan <tailai.ly at taobao.com>
---
 lib/logger.c  |   19 +++++++++++++++++++
 sheep/sheep.c |    9 +--------
 2 files changed, 20 insertions(+), 8 deletions(-)

diff --git a/lib/logger.c b/lib/logger.c
index f3f03e3..9605ae6 100644
--- a/lib/logger.c
+++ b/lib/logger.c
@@ -101,6 +101,25 @@ static int64_t max_logsize = 500 * 1024 * 1024;  /*500MB*/
 
 static pthread_mutex_t logsize_lock = PTHREAD_MUTEX_INITIALIZER;
 
+/*
+ * We need to set default log formatter because collie doesn't want to call
+ * select_log_formatter().
+ */
+static void __attribute__((constructor))
+init_log_formatter(void)
+{
+	struct log_format *f;
+
+	list_for_each_entry(f, &log_formats, list) {
+		if (!strcmp(f->name, "default")) {
+			format = f;
+			return;
+		}
+	}
+	syslog(LOG_ERR, "failed to set default formatter\n");
+	exit(1);
+}
+
 static notrace int logarea_init(int size)
 {
 	int shmid;
diff --git a/sheep/sheep.c b/sheep/sheep.c
index c743859..316d4de 100644
--- a/sheep/sheep.c
+++ b/sheep/sheep.c
@@ -432,7 +432,6 @@ int main(int argc, char **argv)
 	int64_t zone = -1, free_space = 0;
 	struct cluster_driver *cdrv;
 	struct option *long_options;
-	const char *log_format = "default";
 
 	signal(SIGPIPE, SIG_IGN);
 
@@ -572,7 +571,7 @@ int main(int argc, char **argv)
 			exit(0);
 			break;
 		case 'F':
-			log_format = optarg;
+			select_log_formatter(optarg);
 			break;
 		default:
 			usage(1);
@@ -580,12 +579,6 @@ int main(int argc, char **argv)
 		}
 	}
 
-	/*
-	 * select_log_formatter() must be called before any calling of
-	 * sd_printf() series
-	 */
-	select_log_formatter(log_format);
-
 	if (nr_vnodes == 0) {
 		sys->gateway_only = true;
 		sys->disk_space = 0;
-- 
1.7.9.5




More information about the sheepdog mailing list