[ofa-general] [PATCH] opensm: daemon mode

Sasha Khapyorsky sashak at voltaire.com
Tue Mar 20 18:33:06 PDT 2007


This adds daemon mode support for OpenSM. The process will be detached
from terminal and backgrounded. Use '-B' or '--daemon' options to
activate.

Signed-off-by: Sasha Khapyorsky <sashak at voltaire.com>
---
 osm/include/opensm/osm_log.h    |    1 +
 osm/include/opensm/osm_subnet.h |    4 ++
 osm/opensm/main.c               |   59 +++++++++++++++++++++++++++++++++++++--
 osm/opensm/osm_log.c            |    6 ++++
 osm/opensm/osm_opensm.c         |    3 ++
 osm/opensm/osm_subnet.c         |   13 ++++++++
 6 files changed, 83 insertions(+), 3 deletions(-)

diff --git a/osm/include/opensm/osm_log.h b/osm/include/opensm/osm_log.h
index ec79ca7..a556ad2 100644
--- a/osm/include/opensm/osm_log.h
+++ b/osm/include/opensm/osm_log.h
@@ -128,6 +128,7 @@ typedef struct _osm_log
 	boolean_t			flush;
 	FILE*				out_port;
   	boolean_t			accum_log_file;
+	boolean_t			daemon;
         char*				log_file_name;
 } osm_log_t;
 /*********/
diff --git a/osm/include/opensm/osm_subnet.h b/osm/include/opensm/osm_subnet.h
index 5bfba44..3091f65 100644
--- a/osm/include/opensm/osm_subnet.h
+++ b/osm/include/opensm/osm_subnet.h
@@ -282,6 +282,7 @@ typedef struct _osm_subn_opt
   char *                   sa_db_file;
   boolean_t                exit_on_fatal;
   boolean_t                honor_guid2lid_file;
+  boolean_t                daemon;
   osm_qos_options_t        qos_options;
   osm_qos_options_t        qos_ca_options;
   osm_qos_options_t        qos_sw0_options;
@@ -460,6 +461,9 @@ typedef struct _osm_subn_opt
 *		means that the file will be honored when SM is coming out of
 *		STANDBY. By default this is FALSE. 
 *
+*	daemon
+*		OpenSM will run in daemon mode.
+*
 *	qos_options
 *		Default set of QoS options
 *
diff --git a/osm/opensm/main.c b/osm/opensm/main.c
index 5f26638..3f465e9 100644
--- a/osm/opensm/main.c
+++ b/osm/opensm/main.c
@@ -50,6 +50,9 @@
 #include <stdlib.h>
 #include <getopt.h>
 #include <unistd.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
 #include <complib/cl_types.h>
 #include <complib/cl_debug.h>
 #include <vendor/osm_vendor_api.h>
@@ -263,6 +266,9 @@ show_usage(void)
           "          issues: if SM discovers duplicated guids or 12x link with\n"
           "          lane reversal badly configured.\n"
           "          By default, the SM will exit on these errors.\n\n");
+  printf( "-B\n"
+          "--daemon\n"
+          "          Run in daemon mode - OpenSM will run in the background.\n\n");
   printf( "-v\n"
           "--verbose\n"
           "          This option increases the log verbosity level.\n"
@@ -516,6 +522,45 @@ parse_ignore_guids_file(IN char *guids_file_name,
 
 /**********************************************************************
  **********************************************************************/
+
+static int daemonize(osm_opensm_t *osm)
+{
+	pid_t pid;
+	int fd;
+
+	fd = open("/dev/null", O_WRONLY);
+	if (fd < 0) {
+		perror("open");
+		return -1;
+	}
+
+	if ((pid = fork()) < 0) {
+		perror("fork");
+		exit(-1);
+	} else if (pid > 0)
+		exit(0);
+
+	setsid();
+
+	if ((pid = fork()) < 0) {
+		perror("fork");
+		exit(-1);
+	} else if (pid > 0)
+		exit(0);
+
+	close(0);
+	close(1);
+	close(2);
+
+	dup2(fd, 0);
+	dup2(fd, 1);
+	dup2(fd, 2);
+
+	return 0;
+}
+
+/**********************************************************************
+ **********************************************************************/
 int
 main(
   int                   argc,
@@ -536,7 +581,7 @@ main(
   boolean_t             cache_options = FALSE;
   char                 *ignore_guids_file_name = NULL;
   uint32_t              val;
-  const char * const    short_option = "i:f:ed:g:l:L:s:t:a:R:M:U:S:P:NQvVhorcyx";
+  const char * const    short_option = "i:f:ed:g:l:L:s:t:a:R:M:U:S:P:NBQvVhorcyx";
 
   /*
     In the array below, the 2nd parameter specified the number
@@ -580,6 +625,7 @@ main(
 #ifdef ENABLE_OSM_CONSOLE_SOCKET
       {  "console-port",  1, NULL, 'C'},
 #endif
+      {  "daemon",        0, NULL, 'B'},
       {  NULL,            0, NULL,  0 }  /* Required at the end of the array */
     };
 
@@ -846,6 +892,11 @@ main(
       printf (" Honor guid2lid file, if possible\n");
       break;
 
+    case 'B':
+      opt.daemon = TRUE;
+      printf (" Daemon mode.\n");
+      break;
+
     case 'h':
     case '?':
     case ':':
@@ -872,6 +923,9 @@ main(
 
   block_signals();
 
+  if (opt.daemon)
+    daemonize(&osm);
+
   complib_init();
 
   status = osm_opensm_init( &osm, &opt );
@@ -996,8 +1050,7 @@ main(
             osm.mad_pool.mads_out);
 
  Exit:
-  osm_opensm_destroy( &osm );
-
+  osm_opensm_destroy(&osm);
   complib_exit();
 
   exit( 0 );
diff --git a/osm/opensm/osm_log.c b/osm/opensm/osm_log.c
index 5bb0b9e..a1b1777 100644
--- a/osm/opensm/osm_log.c
+++ b/osm/opensm/osm_log.c
@@ -286,6 +286,12 @@ open_out_port(IN osm_log_t *p_log)
 
   syslog(LOG_NOTICE, "%s log file opened\n", p_log->log_file_name);
 
+  if (p_log->daemon) {
+    dup2(fileno(p_log->out_port), 0);
+    dup2(fileno(p_log->out_port), 1);
+    dup2(fileno(p_log->out_port), 2);
+  }
+
   return (0);
 }
 
diff --git a/osm/opensm/osm_opensm.c b/osm/opensm/osm_opensm.c
index 2344380..8430605 100644
--- a/osm/opensm/osm_opensm.c
+++ b/osm/opensm/osm_opensm.c
@@ -196,6 +196,9 @@ osm_opensm_init(
    /* Can't use log macros here, since we're initializing the log */
    osm_opensm_construct( p_osm );
 
+   if (p_opt->daemon)
+      p_osm->log.daemon = 1;
+
    status = osm_log_init_v2( &p_osm->log, p_opt->force_log_flush,
                              p_opt->log_flags, p_opt->log_file,
                              p_opt->log_max_size, p_opt->accum_log_file );
diff --git a/osm/opensm/osm_subnet.c b/osm/opensm/osm_subnet.c
index cbb3549..5f1dae3 100644
--- a/osm/opensm/osm_subnet.c
+++ b/osm/opensm/osm_subnet.c
@@ -460,6 +460,7 @@ osm_subn_set_default_opt(
   p_opt->force_heavy_sweep = FALSE;
   p_opt->log_flags = 0;
   p_opt->honor_guid2lid_file = FALSE;
+  p_opt->daemon = FALSE;
 
   p_opt->dump_files_dir = getenv("OSM_TMP_DIR");
   if (!p_opt->dump_files_dir || !(*p_opt->dump_files_dir))
@@ -1051,6 +1052,10 @@ osm_subn_parse_conf_file(
         "honor_guid2lid_file",
         p_key, p_val, &p_opts->honor_guid2lid_file);
 
+      __osm_subn_opts_unpack_boolean(
+        "daemon",
+        p_key, p_val, &p_opts->daemon);
+
       subn_parse_qos_options("qos",
         p_key, p_val, &p_opts->qos_options);
 
@@ -1281,6 +1286,14 @@ osm_subn_write_conf_file(
     p_opts->single_thread ? "TRUE" : "FALSE"
     );
   
+  fprintf(
+    opts_file,
+    "#\n# MISC OPTIONS\n#\n"
+    "# Daemon mode\n"
+    "daemon %s\n\n",
+    p_opts->daemon ? "TRUE" : "FALSE"
+    );
+
   fprintf( 
     opts_file,
     "#\n# DEBUG FEATURES\n#\n"
-- 
1.5.0.3.401.g27ebd




More information about the general mailing list