Patch from Scott McKellar:
[OpenSRF.git] / src / libopensrf / osrf_system.c
index ca389f3..136558f 100644 (file)
@@ -3,11 +3,23 @@
 #include <opensrf/osrf_prefork.h>
 #include <signal.h>
 
-static int _osrfSystemInitCache( void );
 static void report_child_status( pid_t pid, int status );
 struct child_node;
 typedef struct child_node ChildNode;
 
+static void handleKillSignal(int signo) {
+    /* we are the top-level process and we've been 
+     * killed. Kill all of our children */
+    kill(0, SIGTERM);
+    sleep(1); /* give the children a chance to die before we reap them */
+    pid_t child_pid;
+    int status;
+    while( (child_pid=waitpid(-1,&status,WNOHANG)) > 0) 
+        osrfLogInfo(OSRF_LOG_MARK, "Killed child %d", child_pid);
+    _exit(0);
+}
+
+
 struct child_node
 {
        ChildNode* pNext;
@@ -38,7 +50,7 @@ int osrf_system_bootstrap_client( char* config_file, char* contextnode ) {
        return osrfSystemBootstrapClientResc(config_file, contextnode, NULL);
 }
 
-static int _osrfSystemInitCache( void ) {
+int osrfSystemInitCache( void ) {
 
        jsonObject* cacheServers = osrf_settings_host_value_object("/cache/global/servers/server");
        char* maxCache = osrf_settings_host_value("/cache/global/max_cache_time");
@@ -98,8 +110,6 @@ int osrfSystemBootstrap( char* hostname, char* configfile, char* contextNode ) {
        jsonObject* apps = osrf_settings_host_value_object("/activeapps/appname");
        osrfStringArray* arr = osrfNewStringArray(8);
        
-       _osrfSystemInitCache();
-
        if(apps) {
                int i = 0;
 
@@ -150,6 +160,11 @@ int osrfSystemBootstrap( char* hostname, char* configfile, char* contextNode ) {
                } 
        } // should we do something if there are no apps? does the wait(NULL) below do that for us?
 
+       osrfStringArrayFree(arr);
+
+    signal(SIGTERM, handleKillSignal);
+    signal(SIGINT, handleKillSignal);
+       
        while(1) {
                errno = 0;
                int status;
@@ -310,9 +325,15 @@ int osrfSystemBootstrapClientResc( char* config_file, char* contextnode, char* r
 
 
        char* log_file          = osrfConfigGetValue( NULL, "/logfile");
+       if(!log_file) {
+               fprintf(stderr, "No log file specified in configuration file %s\n",
+                               config_file);
+               return -1;
+       }
+
        char* log_level         = osrfConfigGetValue( NULL, "/loglevel" );
        osrfStringArray* arr    = osrfNewStringArray(8);
-       osrfConfigGetValueList(NULL, arr, "/domains/domain");
+       osrfConfigGetValueList(NULL, arr, "/domain");
 
        char* username          = osrfConfigGetValue( NULL, "/username" );
        char* password          = osrfConfigGetValue( NULL, "/passwd" );
@@ -321,19 +342,6 @@ int osrfSystemBootstrapClientResc( char* config_file, char* contextnode, char* r
        char* facility          = osrfConfigGetValue( NULL, "/syslog" );
        char* actlog            = osrfConfigGetValue( NULL, "/actlog" );
 
-       if(!log_file) {
-               fprintf(stderr, "No log file specified in configuration file %s\n",
-                          config_file);
-               free(log_level);
-               free(username);
-               free(password);
-               free(port);
-               free(unixpath);
-               free(facility);
-               free(actlog);
-               return -1;
-       }
-
        /* if we're a source-client, tell the logger */
        char* isclient = osrfConfigGetValue(NULL, "/client");
        if( isclient && !strcasecmp(isclient,"true") )
@@ -385,6 +393,7 @@ int osrfSystemBootstrapClientResc( char* config_file, char* contextnode, char* r
 
        if (failure) {
                osrfStringArrayFree(arr);
+               free(log_file);
                free(log_level);
                free(username);
                free(password);