This is the mail archive of the cluster-cvs@sourceware.org mailing list for the cluster.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

cluster: master - logthread: Make multiple init/exit calls work


Gitweb:        http://git.fedorahosted.org/git/cluster.git?p=cluster.git;a=commitdiff;h=9f1a03dd08f26648c6b9f76ef4704a603407216a
Commit:        9f1a03dd08f26648c6b9f76ef4704a603407216a
Parent:        8776b161773b4ca3a6ce077db1f232ba68ee9918
Author:        Lon Hohberger <lhh@redhat.com>
AuthorDate:    Fri Dec 12 12:30:44 2008 -0500
Committer:     Lon Hohberger <lhh@redhat.com>
CommitterDate: Fri Dec 12 12:30:44 2008 -0500

logthread: Make multiple init/exit calls work

* This patch allows a user to call logt_init()/logt_exit()
as much as is needed.

* A new function, logt_reinit() is added for convenience.
This function uses values previously supplied to the
logt_init() function to reinitialize the log system.  This
can be done post-fork, for example.  Note: you MUST call
logt_init() and logt_exit() before logt_reinit() can be
used.
---
 common/liblogthread/liblogthread.c |   79 +++++++++++++++++++++++++++++++++++-
 1 files changed, 77 insertions(+), 2 deletions(-)

diff --git a/common/liblogthread/liblogthread.c b/common/liblogthread/liblogthread.c
index af00c8f..9816149 100644
--- a/common/liblogthread/liblogthread.c
+++ b/common/liblogthread/liblogthread.c
@@ -222,6 +222,37 @@ int logt_init(char *name, int mode, int syslog_facility, int syslog_priority,
 	return 0;
 }
 
+
+/*
+ * Reinitialize logt w/ previous values (e.g. use after
+ * a call to fork())
+ *
+ * Only works after you call logt_init and logt_exit
+ */
+int logt_reinit(void)
+{
+	char name_tmp[PATH_MAX];
+	char file_tmp[PATH_MAX];
+
+	if (!done || init)
+		return -1;
+
+	/* Use copies on the stack for these */
+	memset(name_tmp, 0, sizeof(name_tmp));
+	memset(file_tmp, 0, sizeof(file_tmp));
+
+	strncpy(name_tmp, logt_name, sizeof(name_tmp));
+	if (!strlen(name_tmp))
+		return -1;
+	if (strlen(logt_logfile))
+		strncpy(file_tmp, logt_logfile, sizeof(file_tmp));
+
+	return logt_init(name_tmp, logt_mode, logt_syslog_facility,
+			 logt_syslog_priority, logt_logfile_priority,
+			 file_tmp);
+}
+
+
 void logt_exit(void)
 {
 	pthread_mutex_lock(&mutex);
@@ -230,14 +261,29 @@ void logt_exit(void)
 	pthread_cond_signal(&cond);
 	pthread_mutex_unlock(&mutex);
 	pthread_join(thread_handle, NULL);
-	pthread_cond_destroy(&cond);
-	pthread_mutex_destroy(&mutex);
+
+	pthread_mutex_lock(&mutex);
+	/* close syslog + log file */
+	closelog();
+	if (logt_logfile_fp)
+		fclose(logt_logfile_fp);
+	logt_logfile_fp = NULL;
+
+	/* clean up any pending log messages */
+	dropped = 0;
+	pending_ents = 0;
+	head_ent = tail_ent = 0;
 	free(ents);
+	ents = NULL;
+
+	pthread_mutex_unlock(&mutex);
 }
 
 #ifdef TEST
 int main(int argc, char **argv)
 {
+	int pid;
+
 	logt_init("test", LOG_MODE_OUTPUT_FILE|LOG_MODE_OUTPUT_SYSLOG,
 		  LOG_DAEMON, LOG_DEBUG, LOG_DEBUG, "/tmp/logthread");
 	logt_print(LOG_DEBUG, "debugging message %d\n", argc);
@@ -245,6 +291,35 @@ int main(int argc, char **argv)
 	sleep(1);
 	logt_print(LOG_DEBUG, "second debug message\n");
 	logt_exit();
+
+	logt_print(LOG_ERR, "If you see this, it's a bug\n");
+
+	logt_init("test2", LOG_MODE_OUTPUT_FILE|LOG_MODE_OUTPUT_SYSLOG,
+		  LOG_DAEMON, LOG_DEBUG, LOG_DEBUG, "/tmp/logthread");
+	logt_print(LOG_DEBUG, "after 2nd init %d\n", argc);
+	logt_print(LOG_ERR, "error message %d\n", argc);
+	logt_print(LOG_DEBUG, "third debug message\n");
+	logt_exit();
+
+	logt_print(LOG_ERR, "If you see this, it's a bug\n");
+
+	logt_reinit();
+	logt_print(LOG_DEBUG, "after reinit\n");
+	logt_print(LOG_DEBUG, "<-- should say test2\n");
+
+	logt_exit();
+
+	if ((pid = fork()) < 0)
+		return -1;
+
+	if (pid) 
+		exit(0);
+
+	/* child process */
+	logt_reinit();
+	logt_print(LOG_DEBUG, "HELLO from child process\n");
+	logt_exit();
+
 	return 0;
 }
 #endif


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]