[Opensrf-commits] r987 - trunk/src/libopensrf

svn at svn.open-ils.org svn at svn.open-ils.org
Fri Jun 29 23:17:44 EDT 2007


Author: miker
Date: 2007-06-29 23:14:36 -0400 (Fri, 29 Jun 2007)
New Revision: 987

Modified:
   trunk/src/libopensrf/osrf_system.c
   trunk/src/libopensrf/utils.c
Log:
Patch from Scott McKellar implementing cleaner daemonization; moved daemonizing code above worker forking in system boostrapping:

1. As long as I was in the neighborhood, I replaced the error messages
going to stderr with messages going to the logging machinery.

2. I altered the comment just above daemonize(), because the original
was inaccurate.  This function does not change the process title.

3. Pedantic point: I captured the return value of fork() with a pid_t
instead of an int.

4. After the fork, the parent process terminates with _exit()
instead of exit().  That way it doesn't flush any buffers or close
any files.  It also doesn't call any functions registered with
atexit().  In fact it doesn't do much of anything except get out of
the way and let the child process take its place in every way.

5. The child process switches to the root directory, calls setsid()
to create a new session, and freopens the three standard streams
to /dev/null.

I could have changed directories before the fork, or at any of
several other places.  I don't think it makes any difference.

I could also have done the freopens before the fork, but if the
fork failed, I might not have a way to report it (if the logging is
going to stderr for some reason).

Note that I'm not flushing any buffers, apart from the three standard
streams.  There's no need to.  The child process inherits the file
descriptors intact and can do with them whatever it likes.  The
parent may have died, but the estate doesn't have to go through
probate.

I didn't add any error checking for chdir(), setsid(), or freopen().
The first two are unlikely to fail, as is the freopen of stdin.  The
freopens of stderr and stdout could fail, if for example they are
redirected to a full disk.  I don't know how paranoid we need to be.

For more details, see archives starting at

  http://list.georgialibraries.org/pipermail/open-ils-dev/2007-June/001378.html

and also

  http://list.georgialibraries.org/pipermail/open-ils-dev/2007-June/001405.html



Modified: trunk/src/libopensrf/osrf_system.c
===================================================================
--- trunk/src/libopensrf/osrf_system.c	2007-06-29 15:36:58 UTC (rev 986)
+++ trunk/src/libopensrf/osrf_system.c	2007-06-30 03:14:36 UTC (rev 987)
@@ -79,7 +79,12 @@
 			hostname, configfile );
 		return -1;
 	}
-	
+
+	/** daemonize me **/
+	/* background and let our children do their thing */
+	/* NOTE: This has been moved from below the 'if (apps)' block below ... move it back if things go crazy */
+	daemonize();
+
 	jsonObject* apps = osrf_settings_host_value_object("/activeapps/appname");
 	osrfStringArray* arr = osrfNewStringArray(8);
 	
@@ -122,7 +127,7 @@
 	
 				} else {
 		
-					fprintf(stderr, " * Running application %s\n", appname);
+					osrfLogError( OSRF_LOG_MARK, " * Running application %s\n", appname);
 					if( osrfAppRegisterApplication( appname, libfile ) == 0 ) 
 						osrf_prefork_run(appname);
 	
@@ -131,27 +136,22 @@
 				}
 			} // language == c
 		} 
+	} // should we do something if there are no apps? does the wait(NULL) below do that for us?
+
+	while(1) {
+		errno = 0;
+		pid_t pid = wait(NULL);
+		if(-1 == pid) {
+			if(errno == ECHILD)
+				osrfLogError(OSRF_LOG_MARK, "We have no more live services... exiting");
+			else
+				osrfLogError(OSRF_LOG_MARK, "Exiting top-level system loop with error: %s", strerror(errno));
+			break;
+		} else {
+			osrfLogError(OSRF_LOG_MARK, "We lost a top-level service process with PID %ld", pid);
+		}
 	}
 
-	/** daemonize me **/
-
-	/* background and let our children do their thing */
-	daemonize();
-    while(1) {
-        errno = 0;
-        pid_t pid = wait(NULL);
-        if(-1 == pid) {
-            if(errno == ECHILD)
-                osrfLogError(OSRF_LOG_MARK, "We have no more live services... exiting");
-            else
-                osrfLogError(OSRF_LOG_MARK, "Exiting top-level system loop with error: %s", strerror(errno));
-            break;
-        } else {
-            osrfLogError(OSRF_LOG_MARK, "We lost a top-level service process with PID %ld", pid);
-        }
-    }
-
-
 	return 0;
 }
 

Modified: trunk/src/libopensrf/utils.c
===================================================================
--- trunk/src/libopensrf/utils.c	2007-06-29 15:36:58 UTC (rev 986)
+++ trunk/src/libopensrf/utils.c	2007-06-30 03:14:36 UTC (rev 987)
@@ -13,12 +13,13 @@
 */
 
 #include <opensrf/utils.h>
+#include <opensrf/log.h>
 #include <errno.h>
 
 inline void* safe_malloc( int size ) {
 	void* ptr = (void*) malloc( size );
 	if( ptr == NULL ) {
-		perror("safe_malloc(): Out of Memory" );
+		osrfLogError( OSRF_LOG_MARK, "Out of Memory" );
 		exit(99);
 	}
 	memset( ptr, 0, size );
@@ -189,7 +190,7 @@
 		}
 	
 		if( gb->size > BUFFER_MAX_SIZE ) {
-			fprintf(stderr, "Buffer reached MAX_SIZE of %d", BUFFER_MAX_SIZE );
+			osrfLogError( OSRF_LOG_MARK, "Buffer reached MAX_SIZE of %d", BUFFER_MAX_SIZE );
 			buffer_free( gb );
 			return 0;
 		}
@@ -366,20 +367,34 @@
 }
 
 
-// A function to turn a process into a daemon and set it's process name in ps/top
+// A function to turn a process into a daemon 
 int daemonize() {
-	int f = fork();
+	pid_t f = fork();
 
 	if (f == -1) {
-		perror("Failed to fork!");
+		osrfLogError( OSRF_LOG_MARK, "Failed to fork!" );
 		return -1;
 
 	} else if (f == 0) { // We're in the child now...
+		
+		// Change directories.  Otherwise whatever directory
+		// we're in couldn't be deleted until the program
+		// terminated -- possibly causing some inconvenience.
+		chdir( "/" );
+
+		/* create new session */
 		setsid();
+
+		// Now that we're no longer attached to a terminal,
+		// we don't want any traffic on the standard streams
+		freopen( "/dev/null", "r", stdin );
+		freopen( "/dev/null", "w", stdout );
+		freopen( "/dev/null", "w", stderr );
+		
 		return 0;
 
 	} else { // We're in the parent...
-		exit(0);
+		_exit(0);
 	}
 }
 
@@ -406,10 +421,7 @@
 
 	FILE* file = fopen(filename, "r");
 	if(!file) {
-		int l = strlen(filename) + 64;
-		char b[l];
-		snprintf(b,l,"Unable to open file [%s] in file_to_string()", filename);
-		perror(b);
+		osrfLogError( OSRF_LOG_MARK, "Unable to open file [%s]", filename );
 		return NULL;
 	}
 



More information about the opensrf-commits mailing list