[Opensrf-commits] r1647 - trunk/src/router

svn at svn.open-ils.org svn at svn.open-ils.org
Wed Jan 28 22:58:28 EST 2009


Author: scottmk
Date: 2009-01-28 22:58:26 -0500 (Wed, 28 Jan 2009)
New Revision: 1647

Modified:
   trunk/src/router/osrf_router_main.c
Log:
This update mainly tightens the error handling.

1. If there aren't enough arguments on the command line, return EXIT_FAILURE
instead of zero.

2. Defer the call to set_proc_title() until we are done using the command
line arguments, so that we don't have to make copies of them.

3. Check the return value from osrfConfigInit().  Otherwise a NULL (caused,
e.g., by a missing config file) leads to a segfault.

4. If the config file doesn't define any routers to spawn, exit immediately
with an error message before entering the fork loop.

5. If a child process returns (due to an error) instead of entering the
normal endless loop, break out of the fork loop.  Otherwise the child
remains in the fork loop and spawns children of its own (unless it's the
last child to be spawned).  At best, that's just silly.

6. Append an newline to a message issued from setupRouter().  (It's not
clear why this message goes directly to stderr instead of to the usual
logging machinery, which at this point is directed to stderr anyway.)


Modified: trunk/src/router/osrf_router_main.c
===================================================================
--- trunk/src/router/osrf_router_main.c	2009-01-27 17:39:06 UTC (rev 1646)
+++ trunk/src/router/osrf_router_main.c	2009-01-29 03:58:26 UTC (rev 1647)
@@ -34,29 +34,49 @@
 int main( int argc, char* argv[] ) {
 
 	if( argc < 3 ) {
-		osrfLogError( OSRF_LOG_MARK,  "Usage: %s <path_to_config_file> <config_context>", argv[0] );
-		exit(0);
+		osrfLogError( OSRF_LOG_MARK,
+			"Usage: %s <path_to_config_file> <config_context>", argv[0] );
+		exit( EXIT_FAILURE );
 	}
 
-	char* config = strdup( argv[1] );
-	char* context = strdup( argv[2] );
-	init_proc_title( argc, argv );
-	set_proc_title( "OpenSRF Router" );
+	const char* config_file = argv[1];
+	const char* context = argv[2];
 
-	osrfConfig* cfg = osrfConfigInit(config, context);
+	/* Get a set of router definitions from a config file */
+	
+	osrfConfig* cfg = osrfConfigInit(config_file, context);
+	if( NULL == cfg ) {
+		osrfLogError( OSRF_LOG_MARK, "Router can't load config file %s", config_file );
+		exit( EXIT_FAILURE );
+	}
+	
 	osrfConfigSetDefaultConfig(cfg);
     jsonObject* configInfo = osrfConfigGetValueObject(NULL, "/router");
+	
+	if( configInfo->size < 1 || NULL == jsonObjectGetIndex( configInfo, 1 ) ) {
+		osrfLogError( OSRF_LOG_MARK, "No routers defined in config file %s, context \"%s\"",
+			config_file, context );
+		exit( EXIT_FAILURE );
+	}
+	
+	/* We're done with the command line now, */
+	/* so we can safely overlay it */
+	
+	init_proc_title( argc, argv );
+	set_proc_title( "OpenSRF Router" );
 
+	/* Spawn child process(es) */
+	
     int i;
     for(i = 0; i < configInfo->size; i++) {
         jsonObject* configChunk = jsonObjectGetIndex(configInfo, i);
-        if(fork() == 0) /* create a new child to run this router instance */
+        if(fork() == 0) { /* create a new child to run this router instance */
             setupRouter(configChunk);
+			break;  /* We're a child; don't spawn any more children here */
+		}
     }
 
-	free(config);
-	free(context);
-    return EXIT_SUCCESS;
+	return EXIT_SUCCESS;
 }
 
 int setupRouter(jsonObject* configChunk) {
@@ -146,7 +166,7 @@
 	signal(SIGTERM,routerSignalHandler);
 
 	if( (osrfRouterConnect(router)) != 0 ) {
-		fprintf(stderr, "Unable to connect router to jabber server %s... exiting", server );
+		fprintf(stderr, "Unable to connect router to jabber server %s... exiting\n", server );
 		osrfRouterFree(router);
 		return -1;
 	}



More information about the opensrf-commits mailing list