Hey Sasha,

I thought of a way to make it slightly cleaner.  New patch attached.

Al

On Wed, 2008-09-03 at 11:21 -0700, Al Chu wrote:
> Hey Sasha,
> 
> If the call to osm_console_init() fails (most typically b/c bind fails
> b/c the port is already used), we can fall through into osm_console()
> and segfault b/c a bunch of stuff isn't initialized properly.  Can be
> handled multiple ways.  The patch below makes osm_console_init() return
> a non-void so we can recognize if an error occurred.
> 
> Al
> 
> _______________________________________________
> general mailing list
> [email protected]
> http:// lists.openfabrics.org/cgi-bin/mailman/listinfo/general
> 
> To unsubscribe, please visit http:// 
> openib.org/mailman/listinfo/openib-general
-- 
Albert Chu
[EMAIL PROTECTED]
925-422-5311
Computer Scientist
High Performance Systems Division
Lawrence Livermore National Laboratory
>From 28b61a86e83f547409be6cd6b4a3c6a613e1123f Mon Sep 17 00:00:00 2001
From: Albert Chu <[EMAIL PROTECTED]>
Date: Thu, 4 Sep 2008 09:58:01 -0700
Subject: [PATCH] fix segfault corner case when osm_console_init fails


Signed-off-by: Albert Chu <[EMAIL PROTECTED]>
---
 opensm/include/opensm/osm_console_io.h |    2 +-
 opensm/opensm/main.c                   |   10 +++++++---
 opensm/opensm/osm_console_io.c         |   10 ++++++----
 3 files changed, 14 insertions(+), 8 deletions(-)

diff --git a/opensm/include/opensm/osm_console_io.h b/opensm/include/opensm/osm_console_io.h
index cf9019f..5fe233d 100644
--- a/opensm/include/opensm/osm_console_io.h
+++ b/opensm/include/opensm/osm_console_io.h
@@ -77,7 +77,7 @@ typedef struct osm_console {
 } osm_console_t;
 
 void osm_console_prompt(FILE * out);
-void osm_console_init(osm_subn_opt_t * opt, osm_console_t * p_oct, osm_log_t * p_log);
+int osm_console_init(osm_subn_opt_t * opt, osm_console_t * p_oct, osm_log_t * p_log);
 void osm_console_exit(osm_console_t * p_oct, osm_log_t * p_log);
 int is_console_enabled(osm_subn_opt_t *p_opt);
 
diff --git a/opensm/opensm/main.c b/opensm/opensm/main.c
index e32f539..7db4ec9 100644
--- a/opensm/opensm/main.c
+++ b/opensm/opensm/main.c
@@ -496,14 +496,18 @@ static int daemonize(osm_opensm_t * osm)
  **********************************************************************/
 int osm_manager_loop(osm_subn_opt_t * p_opt, osm_opensm_t * p_osm)
 {
-	if (is_console_enabled(p_opt))
-		osm_console_init(p_opt, &p_osm->console, &p_osm->log);
+	int console_init_flag = 0;
+
+	if (is_console_enabled(p_opt)) {
+		if (!osm_console_init(p_opt, &p_osm->console, &p_osm->log))
+			console_init_flag = 1;
+	}
 
 	/*
 	   Sit here forever - dwell or do console i/o & cmds
 	 */
 	while (!osm_exit_flag) {
-		if (is_console_enabled(p_opt))
+		if (console_init_flag)
 			osm_console(p_osm);
 		else
 			cl_thread_suspend(10000);
diff --git a/opensm/opensm/osm_console_io.c b/opensm/opensm/osm_console_io.c
index b46ab7b..2822737 100644
--- a/opensm/opensm/osm_console_io.c
+++ b/opensm/opensm/osm_console_io.c
@@ -144,7 +144,7 @@ void osm_console_prompt(FILE * out)
 	}
 }
 
-void osm_console_init(osm_subn_opt_t * opt, osm_console_t * p_oct, osm_log_t * p_log)
+int osm_console_init(osm_subn_opt_t * opt, osm_console_t * p_oct, osm_log_t * p_log)
 {
 	p_oct->socket = -1;
 	strncpy(p_oct->client_type, opt->console, sizeof(p_oct->client_type));
@@ -167,7 +167,7 @@ void osm_console_init(osm_subn_opt_t * opt, osm_console_t * p_oct, osm_log_t * p
 			OSM_LOG(p_log, OSM_LOG_ERROR,
 				"ERR 4B01: Failed to open console socket: %s\n",
 				strerror(errno));
-			return;
+			return -1;
 		}
 		setsockopt(p_oct->socket, SOL_SOCKET, SO_REUSEADDR,
 			   &optval, sizeof(optval));
@@ -181,13 +181,13 @@ void osm_console_init(osm_subn_opt_t * opt, osm_console_t * p_oct, osm_log_t * p
 			OSM_LOG(p_log, OSM_LOG_ERROR,
 				"ERR 4B02: Failed to bind console socket: %s\n",
 				strerror(errno));
-			return;
+			return -1;
 		}
 		if (listen(p_oct->socket, 1) < 0) {
 			OSM_LOG(p_log, OSM_LOG_ERROR,
 				"ERR 4B03: Failed to listen on socket: %s\n",
 				strerror(errno));
-			return;
+			return -1;
 		}
 
 		signal(SIGPIPE, SIG_IGN);	/* protect ourselves from closed pipes */
@@ -199,6 +199,8 @@ void osm_console_init(osm_subn_opt_t * opt, osm_console_t * p_oct, osm_log_t * p
 			"Console listening on port %d\n", opt->console_port);
 #endif
 	}
+
+	return 0;
 }
 
 /* clean up and release resources */
-- 
1.5.4.5

_______________________________________________
general mailing list
[email protected]
http://lists.openfabrics.org/cgi-bin/mailman/listinfo/general

To unsubscribe, please visit http://openib.org/mailman/listinfo/openib-general

Reply via email to