[Opensrf-commits] r1954 - trunk/src/libopensrf (scottmk)

svn at svn.open-ils.org svn at svn.open-ils.org
Fri May 7 12:40:38 EDT 2010


Author: scottmk
Date: 2010-05-07 12:40:35 -0400 (Fri, 07 May 2010)
New Revision: 1954

Modified:
   trunk/src/libopensrf/osrf_system.c
Log:
Write the PID file from the child process (the one that launches the
listeners), not from the parent process; and only if it actually launches
a listener.

Otherwise if there are no C apps to launch, the child would die immediately,
leaving the PID file hanging around to confuse the surrounding shell script.

M    src/libopensrf/osrf_system.c


Modified: trunk/src/libopensrf/osrf_system.c
===================================================================
--- trunk/src/libopensrf/osrf_system.c	2010-05-04 13:41:16 UTC (rev 1953)
+++ trunk/src/libopensrf/osrf_system.c	2010-05-07 16:40:35 UTC (rev 1954)
@@ -246,23 +246,7 @@
 	// Turn into a daemon.  The parent forks and exits.  Only the
 	// child returns, with the standard streams (stdin, stdout, and
 	// stderr) redirected to /dev/null.
-	FILE* pidfile = NULL;
-	if( pidfile_name ) {
-		pidfile = fopen( pidfile_name, "w" );
-		if( !pidfile ) {
-			osrfLogError( OSRF_LOG_MARK, "Unable to open PID file \"%s\": %s",
-				pidfile_name, strerror( errno ) );
-			free( pidfile_name );
-			pidfile_name = NULL;
-			return -1;
-		}
-	}
-	daemonize_write_pid( pidfile );
-	if( pidfile ) {
-		fclose( pidfile );
-		free( pidfile_name );
-		pidfile_name = NULL;
-	}
+	daemonize();
 
 	jsonObject* apps = osrf_settings_host_value_object("/activeapps/appname");
 	osrfStringArray* arr = osrfNewStringArray(8);
@@ -281,6 +265,7 @@
 		jsonObjectFree(apps);
 
 		const char* appname = NULL;
+		int first_launch = 1;             // Boolean
 		i = 0;
 		while( (appname = osrfStringArrayGetString(arr, i++)) ) {
 
@@ -306,9 +291,35 @@
 					osrfLogInfo( OSRF_LOG_MARK, "Running application child %s: process id %ld",
 								 appname, (long) pid );
 
+					if( first_launch ) {
+						if( pidfile_name ) {
+							// Write our own PID to a PID file so that somebody can use it to
+							// send us a signal later.  If we don't find any C apps to launch,
+							// then we will quietly exit without writing a PID file, and without
+							// waiting to be killed by a signal.
+
+							FILE* pidfile = fopen( pidfile_name, "w" );
+							if( !pidfile ) {
+								osrfLogError( OSRF_LOG_MARK, "Unable to open PID file \"%s\": %s",
+									pidfile_name, strerror( errno ) );
+								free( pidfile_name );
+								pidfile_name = NULL;
+								return -1;
+							} else {
+								fprintf( pidfile, "%ld\n", (long) getpid() );
+								fclose( pidfile );
+							}
+						}
+						first_launch = 0;
+					}
+
 				} else {         // if child, run the application
 
 					osrfLogInfo( OSRF_LOG_MARK, " * Running application %s\n", appname);
+					if( pidfile_name ) {
+						free( pidfile_name );    // tidy up some debris from the parent
+						pidfile_name = NULL;
+					}
 					if( osrfAppRegisterApplication( appname, libfile ) == 0 )
 						osrf_prefork_run(appname);
 
@@ -339,6 +350,10 @@
 				osrfLogError(OSRF_LOG_MARK, "Exiting top-level system loop with error: %s",
 						strerror( errno ) );
 
+			// Since we're not being killed by a signal as usual, delete the PID file
+			// so that no one will try to kill us when we're already dead.
+			if( pidfile_name )
+				remove( pidfile_name );
 			break;
 		} else {
 			report_child_status( pid, status );
@@ -349,6 +364,8 @@
 	osrfConfigCleanup();
 	osrf_system_disconnect_client();
 	osrf_settings_free_host_config(NULL);
+	free( pidfile_name );
+	pidfile_name = NULL;
 	return 0;
 }
 



More information about the opensrf-commits mailing list