[sheepdog] [PATCH v2 2/3] logger: use syslog() when outfile == NULL

Hitoshi Mitake mitake.hitoshi at lab.ntt.co.jp
Mon Nov 12 09:58:18 CET 2012


logger will be used by sheepkeeper. So this patch make logger use
syslog() for logging when the parameter outfile of logger() is NULL.

Signed-off-by: Hitoshi Mitake <mitake.hitoshi at lab.ntt.co.jp>
---
 lib/logger.c |   44 +++++++++++++++++++++++++++-----------------
 1 files changed, 27 insertions(+), 17 deletions(-)

diff --git a/lib/logger.c b/lib/logger.c
index 3979bee..12d6d53 100644
--- a/lib/logger.c
+++ b/lib/logger.c
@@ -71,7 +71,8 @@ static struct logarea *la;
 static const char *log_name;
 static char *log_nowname;
 static int log_level = SDOG_INFO;
-static pid_t sheep_pid;
+/* user_pid: pid of user, e.g. sheep and sheepkeeper */
+static pid_t user_pid;
 static pid_t logger_pid;
 static key_t semkey;
 static char *log_buff;
@@ -320,8 +321,8 @@ static notrace void crash_handler(int signo)
 		vprintf(SDOG_ERR, "logger pid %d segfaulted.\n",
 			getpid());
 	} else if (signo == SIGHUP) {
-		vprintf(SDOG_ERR, "sheep pid %d exited unexpectedly.\n",
-			sheep_pid);
+		vprintf(SDOG_ERR, "%s pid %d exited unexpectedly.\n",
+			log_name, user_pid);
 	} else {
 		vprintf(SDOG_ERR, "logger pid %d got unexpected signal %d.\n",
 			getpid(), signo);
@@ -341,11 +342,15 @@ static notrace void logger(char *log_dir, char *outfile)
 
 	log_buff = xzalloc(la->end - la->start);
 
-	log_fd = open(outfile, O_CREAT | O_RDWR | O_APPEND, 0644);
-	if (log_fd < 0) {
-		syslog(LOG_ERR, "failed to open %s\n", outfile);
-		exit(1);
-	}
+	if (outfile) {
+		log_fd = open(outfile, O_CREAT | O_RDWR | O_APPEND, 0644);
+		if (log_fd < 0) {
+			syslog(LOG_ERR, "failed to open %s\n", outfile);
+			exit(1);
+		}
+	} else
+		openlog(log_name, LOG_PID, LOG_USER);
+
 	la->active = true;
 
 	fd = open("/dev/null", O_RDWR);
@@ -373,21 +378,21 @@ static notrace void logger(char *log_dir, char *outfile)
 
 	prctl(PR_SET_PDEATHSIG, SIGHUP);
 
-	/* we need to check the aliveness of the sheep process since
+	/* we need to check the aliveness of the user process since
 	 * it could die before the logger call prctl. */
-	if (kill(sheep_pid, 0) < 0)
+	if (kill(user_pid, 0) < 0)
 		kill(logger_pid, SIGHUP);
 
 	while (la->active) {
 		log_flush();
 
-		if (max_logsize) {
+		if (-1 != log_fd && max_logsize) {
 			off_t offset;
 
 			pthread_mutex_lock(&logsize_lock);
 			offset = lseek(log_fd, 0, SEEK_END);
 			if (offset < 0) {
-				syslog(LOG_ERR, "sheep log error\n");
+				syslog(LOG_ERR, "%s log error\n", log_name);
 			} else {
 				size_t log_size = (size_t)offset;
 				if (log_size >= max_logsize)
@@ -409,11 +414,16 @@ notrace int log_init(const char *program_name, int size, bool to_stdout,
 	char log_dir[PATH_MAX], tmp[PATH_MAX];
 
 	log_level = level;
-
 	log_name = program_name;
-	log_nowname = outfile;
-	pstrcpy(tmp, sizeof(tmp), outfile);
-	pstrcpy(log_dir, sizeof(log_dir), dirname(tmp));
+
+	if (outfile) {
+		log_nowname = outfile;
+		pstrcpy(tmp, sizeof(tmp), outfile);
+		pstrcpy(log_dir, sizeof(log_dir), dirname(tmp));
+	} else {
+		const char *root_dir = "/";
+		pstrcpy(log_dir, sizeof(log_dir), root_dir);
+	}
 
 	semkey = random();
 
@@ -429,7 +439,7 @@ notrace int log_init(const char *program_name, int size, bool to_stdout,
 		 * the parents death the parent has been reparanted to init
 		 * and getppid() will always return 1.
 		 */
-		sheep_pid = getpid();
+		user_pid = getpid();
 		logger_pid = fork();
 		if (logger_pid < 0) {
 			syslog(LOG_ERR, "failed to fork the logger process: %m\n");
-- 
1.7.2.5




More information about the sheepdog mailing list