[Sheepdog] [PATCH] Enable sheepdog generate core dump file

Yunkai Zhang yunkai.me at gmail.com
Fri Mar 2 16:42:28 CET 2012


If sheepdog received SIGSEGV signal and we have enabled
OS to generate it, core dump file would be created in
LOCALSTATEDIR/lib/sheepdog directory.

We can find the vaule of LOCALSTATEDIR in include/config.h.

Signed-off-by: Yunkai Zhang <qiushu.zyk at taobao.com>
---
 lib/logger.c |   20 +++++++++++++++-----
 1 files changed, 15 insertions(+), 5 deletions(-)

diff --git a/lib/logger.c b/lib/logger.c
index 5be035a..c78329e 100644
--- a/lib/logger.c
+++ b/lib/logger.c
@@ -29,6 +29,7 @@
 #include <sys/wait.h>
 #include <sys/prctl.h>
 
+#include "../include/config.h"
 #include "logger.h"
 #include "util.h"
 
@@ -355,11 +356,12 @@ static void log_flush(void)
 
 static void log_sigsegv(void)
 {
+	(void)signal(SIGSEGV, SIG_DFL);
 	vprintf(SDOG_ERR, "logger pid %d exiting abnormally\n", getpid());
 	log_flush();
 	closelog();
 	free_logarea();
-	exit(1);
+	raise(SIGSEGV);
 }
 
 int log_init(char *program_name, int size, int is_daemon, int level, char *outfile)
@@ -372,6 +374,7 @@ int log_init(char *program_name, int size, int is_daemon, int level, char *outfi
 	semkey = random();
 
 	if (is_daemon) {
+		const char *run_dir;
 		struct sigaction sa_old;
 		struct sigaction sa_new;
 		int fd;
@@ -395,6 +398,17 @@ int log_init(char *program_name, int size, int is_daemon, int level, char *outfi
 
 		la->active = 1;
 		la->fd = fd;
+
+		run_dir = LOCALSTATEDIR "/lib/sheepdog";
+	        if ((mkdir(run_dir, 0700)) == -1 && errno != EEXIST) {
+			syslog(LOG_ERR, "failed to mkdir -p to %s: %m\n", run_dir);
+		}
+
+		if (chdir(run_dir) < 0) {
+			syslog(LOG_ERR, "failed to chdir to %s: %m\n", run_dir);
+			exit(1);
+		}
+
 		pid = fork();
 		if (pid < 0) {
 			syslog(LOG_ERR, "failed to fork the logger process: %m\n");
@@ -414,10 +428,6 @@ int log_init(char *program_name, int size, int is_daemon, int level, char *outfi
 		dup2(fd, 1);
 		dup2(fd, 2);
 		setsid();
-		if (chdir("/") < 0) {
-			syslog(LOG_ERR, "failed to chdir to /: %m\n");
-			exit(1);
-		}
 
 		/* flush on daemon's crash */
 		sa_new.sa_handler = (void*)log_sigsegv;
-- 
1.7.7.6




More information about the sheepdog mailing list