[31102] trunk/base/src/programs/daemondo/main.c

source_changes at macosforge.org source_changes at macosforge.org
Thu Nov 15 17:36:51 PST 2007


Revision: 31102
          http://trac.macosforge.org/projects/macports/changeset/31102
Author:   jberry at macports.org
Date:     2007-11-15 17:36:49 -0800 (Thu, 15 Nov 2007)

Log Message:
-----------
Daemondo:

	- Add new option --restart-wait=n which specifies the number of seconds to
	  wait during restart of the process between stop and start. Default is 3,
	  which gives other software that might be monitoring the target process a
	  bit of a chance to sit up and notice before we restart the process.
	- Add some additional debug level logging.
	

Modified Paths:
--------------
    trunk/base/src/programs/daemondo/main.c

Modified: trunk/base/src/programs/daemondo/main.c
===================================================================
--- trunk/base/src/programs/daemondo/main.c	2007-11-15 22:34:23 UTC (rev 31101)
+++ trunk/base/src/programs/daemondo/main.c	2007-11-16 01:36:49 UTC (rev 31102)
@@ -109,6 +109,7 @@
 int                 restartOnWakeup     = 0;        // TRUE to restart daemon on wake from sleep
 CFRunLoopTimerRef   restartTimer        = NULL;     // Timer for scheduled restart
 CFTimeInterval      restartHysteresis   = 5.0;      // Default hysteresis is 5 seconds
+int				    restartWait		   	= 3;      	// Default wait during restart is 3 seconds
 
 
 void
@@ -292,9 +293,15 @@
             break;
         case kPidStyleExec:         // We wrote the file, and we'll remove it
         case kPidStyleFileClean:    // The process wrote the file, but we'll remove it
-            unlink(pidFile);
+			if (verbosity >= 5)
+				LogMessage("Attempting to delete pidfile %s\n", pidFile);
+            if (unlink(pidFile) && verbosity >= 3)
+				LogMessage("Failed attempt to delete pidfile %s (%d)\n", pidFile, errno);            
             break;
         }
+    } else {
+		if (verbosity >= 5)
+			LogMessage("No pidfile to delete: none specified\n");
     }
 }
 
@@ -323,6 +330,45 @@
 
 
 pid_t
+DeletePreexistingPidFile(void)
+{
+    // Try to read the pid from the pid file
+    pid_t pid = -1;
+    FILE* f = fopen(pidFile, "r");
+    if (f != NULL)
+    {
+        if (1 != fscanf(f, "%d", &pid))
+            pid = -1;
+        if (pid == 0)
+            pid = -1;
+        fclose(f);
+    }
+    
+    // Check whether the pid represents a valid process
+    int valid = (pid != -1 && 0 != kill(pid, 0));
+    
+    // Log information about the discovered pid file
+    if (verbosity >= 3 && pid != -1) {
+    	LogMessage("Discovered preexisting pidfile %s containing pid %d which is a %s process\n", pidFile, pid, 
+    		(valid) ? "valid" : "invalid");
+    }
+    
+    // Try to delete the pidfile if it's present
+    if (pid != -1) {
+	    if (unlink(pidFile)) {
+	    	if (verbosity >= 3)
+		  		LogMessage("Error %d while trying to cleanup prexisting pidfile %s\n", errno, pidFile);
+	  	} else {
+ 	  		if (verbosity >= 3)
+		  		LogMessage("Deleted preexisting pidfile %s\n", pidFile);
+		}
+	}
+    
+    return pid;
+}
+
+
+pid_t
 WaitForValidPidFile(void)
 {
     CFAbsoluteTime patience = CFAbsoluteTimeGetCurrent() + kChildStartPidTimeout;
@@ -379,7 +425,7 @@
     if (runningPid != 0 && runningPid != -1 && childPid == runningPid)
     {
         if (verbosity >= 1)
-            LogMessage("Target process %d has died.\n", childPid);
+            LogMessage("Target process %d has died\n", childPid);
             
         UnmonitorChild();
         DestroyPidFile();
@@ -493,14 +539,16 @@
     
     if (!startArgs || !startArgs[0])
     {
-        LogMessage("There is nothing to start. No start-cmd was specified.\n");
+        LogMessage("There is nothing to start. No start-cmd was specified\n");
         return 2;
     }
     
     if (verbosity >= 1)
         LogMessage("Starting process\n");
+	if (pidFile != NULL)
+		DeletePreexistingPidFile();
     if (verbosity >= 2)
-        LogMessage("Running start-cmd %s.\n", CatArray(startArgs, buf, sizeof(buf)));
+        LogMessage("Running start-cmd %s\n", CatArray(startArgs, buf, sizeof(buf)));
         
     // Exec the start-cmd
     pid_t pid = Exec(startArgs, pidStyle == kPidStyleNone);
@@ -509,7 +557,7 @@
     if (pid == -1)
     {
         if (verbosity >= 2)
-            LogMessage("Error running start-cmd %s.\n", CatArray(startArgs, buf, sizeof(buf)));
+            LogMessage("Error running start-cmd %s\n", CatArray(startArgs, buf, sizeof(buf)));
         if (verbosity >= 1)
             LogMessage("error while starting\n");
         return 2;
@@ -531,7 +579,7 @@
         if (pid == -1)
         {
             if (verbosity >= 2)
-                LogMessage("Error; expected pidfile not found following Exec of start-cmd %s.\n", CatArray(startArgs, buf, sizeof(buf)));
+                LogMessage("Error; expected pidfile not found following Exec of start-cmd %s\n", CatArray(startArgs, buf, sizeof(buf)));
             if (verbosity >= 1)
                 LogMessage("error while starting\n");
             return 2;
@@ -591,7 +639,7 @@
         if (verbosity >= 1)
             LogMessage("Stopping process\n");
         if (verbosity >= 2)
-            LogMessage("Running stop-cmd %s.\n", CatArray(stopArgs, buf, sizeof(buf)));
+            LogMessage("Running stop-cmd %s\n", CatArray(stopArgs, buf, sizeof(buf)));
         pid = Exec(stopArgs, TRUE);
         if (pid == -1)
         {
@@ -621,16 +669,28 @@
         // We weren't given a restart command, so just use stop/start
         if (verbosity >= 1)
             LogMessage("Restarting process\n");
+            
+        // Stop the process
         Stop();
+        
+        // Delay for a restartWait seconds to allow other process support to stabilize
+        // (This gives a chance for other processes that might be monitoring the process,
+        // for instance, to detect its death and cleanup).
+        sleep(restartWait);
+        
+        // Start it again
         Start();
     }
     else
     {
+    	// Bug: we should recapture the target process id from the pidfile in this case
+    	
         // Execute the restart-cmd and trust it to do the job
         if (verbosity >= 1)
             LogMessage("Restarting process\n");
         if (verbosity >= 2)
-            LogMessage("Running restart-cmd %s.\n", CatArray(restartArgs, buf, sizeof(buf)));
+            LogMessage("Running restart-cmd %s\n", CatArray(restartArgs, buf, sizeof(buf)));
+            
         pid_t pid = Exec(restartArgs, TRUE);
         if (pid == -1)
         {
@@ -1086,7 +1146,8 @@
     kRestartNetChangeOpt,
     kPidOpt,
     kPidFileOpt,
-    kRestartHysteresisOpt
+    kRestartHysteresisOpt,
+    kRestartWaitOpt
 };
 
 
@@ -1141,6 +1202,8 @@
         { "label",          required_argument,      0,              'l' },
         { "restart-hysteresis",
                             required_argument,      0,              kRestartHysteresisOpt },
+        { "restart-wait",
+                            required_argument,      0,              kRestartWaitOpt },
         
         { 0,                0,                      0,              0 }
     };
@@ -1225,6 +1288,12 @@
                 restartHysteresis = 0;
             break;
             
+        case kRestartWaitOpt:
+            restartWait = strtol(optarg, NULL, 10);
+            if (restartWait < 0)
+                restartWait = 0;
+            break;
+            
         case kPidOpt:
             if      (0 == strcasecmp(optarg, "none"))
                 pidStyle = kPidStyleNone;

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.macosforge.org/pipermail/macports-changes/attachments/20071115/6ea5db35/attachment.html


More information about the macports-changes mailing list