From bbf299af2244034d71d95858bab37de9598fa0ec Mon Sep 17 00:00:00 2001 From: Lebbeous Fogle-Weekley Date: Tue, 4 Feb 2014 17:56:07 -0500 Subject: [PATCH] Teach osrf_router to (optionally) write its own PID files Signed-off-by: Lebbeous Fogle-Weekley --- bin/opensrf-perl.pl.in | 15 +++------------ src/router/osrf_router_main.c | 31 +++++++++++++++++++++++++++++-- 2 files changed, 32 insertions(+), 14 deletions(-) diff --git a/bin/opensrf-perl.pl.in b/bin/opensrf-perl.pl.in index e0d4441..cca01da 100755 --- a/bin/opensrf-perl.pl.in +++ b/bin/opensrf-perl.pl.in @@ -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 diff --git a/src/router/osrf_router_main.c b/src/router/osrf_router_main.c index ba14551..c852e5b 100644 --- a/src/router/osrf_router_main.c +++ b/src/router/osrf_router_main.c @@ -58,12 +58,14 @@ int main( int argc, char* argv[] ) { if( argc < 3 ) { osrfLogError( OSRF_LOG_MARK, - "Usage: %s ", argv[0] ); + "Usage: %s [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; -- 2.11.0