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
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 */
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" ) )
// 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;