>From d38854b804caac77ba7985fdf2314e412420cdad Mon Sep 17 00:00:00 2001
From: Tim Meier <meier3@llnl.gov>
Date: Tue, 18 Nov 2008 16:51:14 -0800
Subject: [PATCH] Opensm: main exit codes

Defined a set of exits codes and modified main() to use them as much as
possible.

Signed-off-by: Tim Meier <meier3@llnl.gov>
---
 opensm/include/opensm/osm_opensm.h |   24 ++++++++++++++++++++++++
 opensm/opensm/main.c               |   30 ++++++++++++++++--------------
 2 files changed, 40 insertions(+), 14 deletions(-)

diff --git a/opensm/include/opensm/osm_opensm.h b/opensm/include/opensm/osm_opensm.h
index c121be4..5e78dba 100644
--- a/opensm/include/opensm/osm_opensm.h
+++ b/opensm/include/opensm/osm_opensm.h
@@ -87,6 +87,30 @@ BEGIN_C_DECLS
 *	Steve King, Intel
 *
 *********/
+/****d* OpenSM: OpenSM/osm_exit_type_t
+* NAME
+*       osm_exit_type_t
+*
+* DESCRIPTION
+*       Enumerates the possible exit codes that
+*       are provided by OpenSM.
+*
+* SYNOPSIS
+*/
+typedef enum _osm_exit_type {
+	OSM_EXIT_TYPE_NORMAL = 0,
+	OSM_EXIT_TYPE_GENERIC_ERR,
+	OSM_EXIT_TYPE_USAGE,
+	OSM_EXIT_TYPE_FORK_ERR,
+	OSM_EXIT_TYPE_DIFFERENT_DEBUG_MODE,
+	OSM_EXIT_TYPE_DUPLICATE_OSM_GUID,
+	OSM_EXIT_TYPE_CONFIG_PARSE_ERR,
+	OSM_EXIT_TYPE_CONF_FILE_WRITE_ERR,
+	OSM_EXIT_TYPE_INVALID_ARG_VAL,
+	OSM_EXIT_TYPE_UNKNOWN_CMDLINE_ARG,
+	OSM_EXIT_TYPE_UNKNOWN
+} osm_exit_type_t;
+/***********/
 /****d* OpenSM: OpenSM/osm_routing_engine_type_t
 * NAME
 *       osm_routing_engine_type_t
diff --git a/opensm/opensm/main.c b/opensm/opensm/main.c
index 53648d6..d3aa55c 100644
--- a/opensm/opensm/main.c
+++ b/opensm/opensm/main.c
@@ -347,7 +347,7 @@ static void show_usage(void)
 	printf("--help, -h, -?\n"
 	       "          Display this usage info then exit.\n\n");
 	fflush(stdout);
-	exit(2);
+	exit(OSM_EXIT_TYPE_USAGE);
 }
 
 /**********************************************************************
@@ -451,17 +451,17 @@ static int daemonize(osm_opensm_t * osm)
 
 	if ((pid = fork()) < 0) {
 		perror("fork");
-		exit(-1);
+		exit(OSM_EXIT_TYPE_FORK_ERR);
 	} else if (pid > 0)
-		exit(0);
+		exit(OSM_EXIT_TYPE_NORMAL);
 
 	setsid();
 
 	if ((pid = fork()) < 0) {
 		perror("fork");
-		exit(-1);
+		exit(OSM_EXIT_TYPE_FORK_ERR);
 	} else if (pid > 0)
-		exit(0);
+		exit(OSM_EXIT_TYPE_NORMAL);
 
 	close(0);
 	close(1);
@@ -516,6 +516,7 @@ int main(int argc, char *argv[])
 {
 	osm_opensm_t osm;
 	osm_subn_opt_t opt;
+	int exit_code = OSM_EXIT_TYPE_NORMAL;
 	ib_net64_t sm_key = 0;
 	ib_api_status_t status;
 	uint32_t temp, dbg_lvl;
@@ -595,7 +596,7 @@ int main(int argc, char *argv[])
 			"ERROR: OpenSM and Complib were compiled using different modes\n");
 		fprintf(stderr, "ERROR: OpenSM debug:%d Complib debug:%d \n",
 			osm_is_debug(), cl_is_debug());
-		exit(1);
+		exit(OSM_EXIT_TYPE_DIFFERENT_DEBUG_MODE);
 	}
 #if defined (_DEBUG_) && defined (OSM_VENDOR_INTF_OPENIB)
 	enable_stack_dump(1);
@@ -615,7 +616,7 @@ int main(int argc, char *argv[])
 					       long_option, NULL);
 		switch (next_option) {
 		case 12: /* --version - already printed above */
-			exit(0);
+			exit(OSM_EXIT_TYPE_NORMAL);
 			break;
 		case 'F':
 			if (config_file_done)
@@ -623,7 +624,7 @@ int main(int argc, char *argv[])
 			printf("Reloading config from `%s`:\n", optarg);
 			if (osm_subn_parse_conf_file(optarg, &opt)) {
 				printf("cannot parse config file.\n");
-				exit(1);
+				exit(OSM_EXIT_TYPE_CONFIG_PARSE_ERR);
 			}
 			printf("Rescaning command line:\n");
 			config_file_done = 1;
@@ -755,7 +756,7 @@ int main(int argc, char *argv[])
 			if (temp > 7) {
 				fprintf(stderr,
 					"ERROR: LMC must be 7 or less.\n");
-				return (-1);
+				exit(OSM_EXIT_TYPE_INVALID_ARG_VAL);
 			}
 			opt.lmc = (uint8_t) temp;
 			printf(" LMC = %d\n", temp);
@@ -821,7 +822,7 @@ int main(int argc, char *argv[])
 			if (0 > temp || 15 < temp) {
 				fprintf(stderr,
 					"ERROR: priority must be between 0 and 15\n");
-				return (-1);
+				exit (OSM_EXIT_TYPE_INVALID_ARG_VAL);
 			}
 			opt.sm_priority = (uint8_t) temp;
 			printf(" Priority = %d\n", temp);
@@ -931,7 +932,7 @@ int main(int argc, char *argv[])
 		case -1:
 			break;	/* done with option */
 		default:	/* something wrong */
-			abort();
+			exit(OSM_EXIT_TYPE_UNKNOWN_CMDLINE_ARG);
 		}
 	}
 	while (next_option != -1);
@@ -945,7 +946,7 @@ int main(int argc, char *argv[])
 		status = osm_subn_write_conf_file(conf_template, &opt);
 		if (status)
 			printf("\nosm_subn_write_conf_file failed!\n");
-		exit(status);
+		exit(status? OSM_EXIT_TYPE_CONF_FILE_WRITE_ERR: OSM_EXIT_TYPE_NORMAL);
 	}
 
 	if (vendor_debug)
@@ -967,7 +968,7 @@ int main(int argc, char *argv[])
 		/* We will just exit, and not go to Exit, since we don't
 		   want the destroy to be called. */
 		complib_exit();
-		return (status);
+		exit (status);
 	}
 
 	/*
@@ -982,6 +983,7 @@ int main(int argc, char *argv[])
 		printf("\nError from osm_opensm_bind (0x%X)\n", status);
 		printf
 		    ("Perhaps another instance of OpenSM is already running\n");
+		exit_code = OSM_EXIT_TYPE_DUPLICATE_OSM_GUID;
 		goto Exit;
 	}
 
@@ -1021,5 +1023,5 @@ Exit:
 	osm_opensm_destroy(&osm);
 	complib_exit();
 
-	exit(0);
+	exit(exit_code);
 }
-- 
1.5.4.5

