Teach osrf_router to (optionally) write its own PID files
authorLebbeous Fogle-Weekley <lebbeous@esilibrary.com>
Tue, 4 Feb 2014 22:56:07 +0000 (17:56 -0500)
committerLebbeous Fogle-Weekley <lebbeous@esilibrary.com>
Tue, 4 Feb 2014 23:15:18 +0000 (18:15 -0500)
Signed-off-by: Lebbeous Fogle-Weekley <lebbeous@esilibrary.com>
bin/opensrf-perl.pl.in
src/router/osrf_router_main.c

index e0d4441..cca01da 100755 (executable)
@@ -276,20 +276,11 @@ sub do_diagnostic {
 
 
 sub do_start_router {
-    `opensrf_router $opt_config routers`;
-
-    sleep 2; # give the router time to fork
-    my @pids = `ps -C opensrf_router -o pid=`;
-    s/^\s*|\n//g for @pids;
 
     my $pidfile = get_pid_file('router');
-    open(PF, '>', $pidfile) or die "Cannot open $pidfile: $!\n";
-    foreach (@pids) {
-        chomp;
-        msg("starting service pid=$_ router");
-        print PF "$_\n";
-    }
-    close PF;
+    `opensrf_router $opt_config routers $pidfile`;
+
+    sleep 2; # give the router time to fork (probably not need now but w/e)
 }
 
 # stop a specific service
index ba14551..c852e5b 100644 (file)
@@ -58,12 +58,14 @@ int main( int argc, char* argv[] ) {
 
        if( argc < 3 ) {
                osrfLogError( OSRF_LOG_MARK,
-                       "Usage: %s <path_to_config_file> <config_context>", argv[0] );
+                       "Usage: %s <path_to_config_file> <config_context> [pid_file]",
+                       argv[0] );
                exit( EXIT_FAILURE );
        }
 
        const char* config_file = argv[1];
        const char* context = argv[2];
+       const char* pid_file = argc >= 4 ? argv[3] : NULL;
 
        /* Get a set of router definitions from a config file */
 
@@ -92,6 +94,11 @@ int main( int argc, char* argv[] ) {
        int rc = EXIT_SUCCESS;
        int parent = 1;    // boolean
        int i;
+
+       pid_t child_pid;
+       pid_t* all_child_pids = safe_calloc(configInfo->size * sizeof(pid_t));
+       FILE *pid_fp;
+
        for(i = 0; i < configInfo->size; i++) {
                const jsonObject* configChunk = jsonObjectGetIndex( configInfo, i );
                if( ! jsonObjectGetKeyConst( configChunk, "transport" ) )
@@ -107,14 +114,34 @@ int main( int argc, char* argv[] ) {
                        // typo or other such error, making it look spurious.  In that case, well, too bad.
                        continue;
                }
-               if(fork() == 0) { /* create a new child to run this router instance */
+
+               if((child_pid = fork()) == 0) { /* create a new child to run this router instance */
                        setupRouter(configChunk);
                        parent = 0;
                        break;  /* We're a child; don't spawn any more children here */
+               } else if (child_pid != -1) {
+                       all_child_pids[i] = child_pid;
                }
        }
 
        if( parent ) {
+
+               /* Write our PID file if we were asked to and can, then
+                * free the memory used to store that list. */
+               if (pid_file) {
+                       if ((pid_fp = fopen(pid_file, "w"))) {
+                               for (i = 0; i < configInfo->size; i++) {
+                                       fprintf(pid_fp, "%d\n", all_child_pids[i]);
+                               }
+                               fclose(pid_fp);
+                       } else {
+                               osrfLogWarning(OSRF_LOG_MARK,
+                                               "osrf_router was asked to write its own PID file at %s but could not do so (%s)",
+                                               pid_file, strerror(errno));
+                       }
+               }
+               free(all_child_pids);
+
                // Wait for all child processes to terminate; report their fates
                while( 1 ) {  // Loop until all children terminate
                        int status;