From 3881faf26a875fc5d3d0570237a3f18896e8c71f Mon Sep 17 00:00:00 2001 From: scottmk Date: Thu, 29 Jan 2009 03:58:26 +0000 Subject: [PATCH 1/1] 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.) git-svn-id: svn://svn.open-ils.org/OpenSRF/trunk@1647 9efc2488-bf62-4759-914b-345cdb29e865 --- src/router/osrf_router_main.c | 44 +++++++++++++++++++++++++---------- 1 file changed, 32 insertions(+), 12 deletions(-) diff --git a/src/router/osrf_router_main.c b/src/router/osrf_router_main.c index 30e4975..d15f164 100644 --- a/src/router/osrf_router_main.c +++ b/src/router/osrf_router_main.c @@ -34,29 +34,49 @@ static int setupRouter(jsonObject* configChunk); int main( int argc, char* argv[] ) { if( argc < 3 ) { - osrfLogError( OSRF_LOG_MARK, "Usage: %s ", argv[0] ); - exit(0); + osrfLogError( OSRF_LOG_MARK, + "Usage: %s ", 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 @@ int setupRouter(jsonObject* configChunk) { 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; } -- 2.43.2