[openib-general] [PATCH 2/2] opensm: sigusr1: synchronous SIGUSR1 processing.

Sasha Khapyorsky sashak at voltaire.com
Sun Jan 7 18:44:01 PST 2007


Process SIGUSR1 synchronously similar to other signals. Move signal
handler setup from potentially shared library code. Use osm log locking
with file reopening in order to prevent races with osm_log().

Signed-off-by: Sasha Khapyorsky <sashak at voltaire.com>
---
 osm/include/opensm/osm_log.h |   21 +++++++++++++++++++++
 osm/opensm/libopensm.map     |    3 ++-
 osm/opensm/main.c            |   17 +++++++++++++++++
 osm/opensm/osm_log.c         |   41 +++++++++++------------------------------
 4 files changed, 51 insertions(+), 31 deletions(-)

diff --git a/osm/include/opensm/osm_log.h b/osm/include/opensm/osm_log.h
index 7197439..55e996a 100644
--- a/osm/include/opensm/osm_log.h
+++ b/osm/include/opensm/osm_log.h
@@ -253,6 +253,27 @@ osm_log_init_v2(
 *	osm_log_destroy
 *********/
 
+/****f* OpenSM: Log/osm_log_reopen_file
+* NAME
+*	osm_log_reopen_file
+*
+* DESCRIPTION
+*	The osm_log_reopen_file function reopens the log file
+*
+* SYNOPSIS
+*/
+int
+osm_log_reopen_file(
+	osm_log_t *p_log);
+/*
+* PARAMETERS
+*	p_log
+*		[in] Pointer to the log object.
+*
+* RETURN VALUES
+*	0 on success or nonzero value otherwise.
+*********/
+
 /****f* OpenSM: Log/osm_log_init
 * NAME
 *	osm_log_init
diff --git a/osm/opensm/libopensm.map b/osm/opensm/libopensm.map
index c200384..909b641 100644
--- a/osm/opensm/libopensm.map
+++ b/osm/opensm/libopensm.map
@@ -1,10 +1,11 @@
-OPENSM_1.4 {
+OPENSM_1.5 {
 	global:
 		osm_log;
 		osm_log_printf;
 		osm_is_debug;
 		osm_log_init;
 		osm_log_init_v2;
+		osm_log_reopen_file;
 		osm_mad_pool_construct;
 		osm_mad_pool_destroy;
 		osm_mad_pool_init;
diff --git a/osm/opensm/main.c b/osm/opensm/main.c
index 374d323..d63c95c 100644
--- a/osm/opensm/main.c
+++ b/osm/opensm/main.c
@@ -71,6 +71,7 @@ osm_opensm_t osm;
 volatile unsigned int osm_exit_flag = 0;
 
 static volatile unsigned int osm_hup_flag = 0;
+static volatile unsigned int osm_usr1_flag = 0;
 
 #define GUID_ARRAY_SIZE 64
 #define INVALID_GUID (0xFFFFFFFFFFFFFFFFULL)
@@ -91,6 +92,11 @@ static void mark_hup_flag(int signum)
 	osm_hup_flag = 1;
 }
 
+static void mark_usr1_flag(int signum)
+{
+	osm_usr1_flag = 1;
+}
+
 static sigset_t saved_sigset;
 
 static void block_signals()
@@ -101,6 +107,9 @@ static void block_signals()
 	sigaddset(&set, SIGINT);
 	sigaddset(&set, SIGTERM);
 	sigaddset(&set, SIGHUP);
+#ifndef HAVE_OLD_LINUX_THREADS
+	sigaddset(&set, SIGUSR1);
+#endif
 	pthread_sigmask(SIG_SETMASK, &set, &saved_sigset);
 }
 
@@ -115,6 +124,10 @@ static void setup_signals()
 	sigaction(SIGTERM, &act, NULL);
 	act.sa_handler = mark_hup_flag;
 	sigaction(SIGHUP, &act, NULL);
+#ifndef HAVE_OLD_LINUX_THREADS
+	act.sa_handler = mark_usr1_flag;
+	sigaction(SIGUSR1, &act, NULL);
+#endif
 	pthread_sigmask(SIG_SETMASK, &saved_sigset, NULL);
 }
 #endif /* __WIN__ */
@@ -946,6 +959,10 @@ main(
       else
         cl_thread_suspend( 10000 );
 
+      if (osm_usr1_flag) {
+        osm_usr1_flag = 0;
+        osm_log_reopen_file(&osm.log);
+      }
       if (osm_hup_flag) {
         osm_hup_flag = 0;
         /* a HUP signal should only start a new heavy sweep */
diff --git a/osm/opensm/osm_log.c b/osm/opensm/osm_log.c
index 56c6482..6d9a450 100644
--- a/osm/opensm/osm_log.c
+++ b/osm/opensm/osm_log.c
@@ -58,7 +58,6 @@
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <errno.h>
-#include <signal.h>
 
 static int log_exit_count = 0;
 
@@ -290,24 +289,16 @@ open_out_port(IN osm_log_t *p_log)
     return (0);
 }
 
-
-osm_log_t *handler_log = NULL;
-void
-sigusr1_handler(int signal)
-{
-	fclose(handler_log->out_port);
-	open_out_port(handler_log);
-}
-
-static void
-setup_sigusr1_handler(osm_log_t *p_log)
+int osm_log_reopen_file(osm_log_t *p_log)
 {
-	struct sigaction act;
-	act.sa_handler = sigusr1_handler;
-	sigemptyset(&(act.sa_mask));
-	act.sa_flags = 0;
-	handler_log = p_log;
-	sigaction(SIGUSR1, &act, NULL);
+	int ret;
+	if (p_log->out_port == stdout || p_log->out_port == stderr)
+		return 0;
+	cl_spinlock_acquire(&p_log->lock);
+	fclose(p_log->out_port);
+	ret = open_out_port(p_log);
+	cl_spinlock_release(&p_log->lock);
+	return ret;
 }
 
 ib_api_status_t
@@ -330,21 +321,11 @@ osm_log_init_v2(
 
   if (log_file == NULL || !strcmp(log_file, "-") ||
       !strcmp(log_file, "stdout"))
-  {
     p_log->out_port = stdout;
-  }
   else if (!strcmp(log_file, "stderr"))
-  {
     p_log->out_port = stderr;
-  }
-  else
-  {
-	  setup_sigusr1_handler(p_log);
-	  if (open_out_port(p_log))
-	  {
-		  return (IB_UNKNOWN_ERROR);
-	  }
-  }
+  else if (open_out_port(p_log))
+    return (IB_UNKNOWN_ERROR);
 
   if (cl_spinlock_init( &p_log->lock ) == CL_SUCCESS) 
     return IB_SUCCESS;
-- 
1.5.0.rc0.g2484-dirty





More information about the general mailing list