]> git.ipfire.org Git - thirdparty/squid.git/blobdiff - src/ipc/Kid.cc
Docs: Copyright updates for 2018 (#114)
[thirdparty/squid.git] / src / ipc / Kid.cc
index f6f5d1ccf7ccdc4e78e987a6fdfc80f027e2c6a8..9a3cc1fa178b9435876bb05549d0f79d00082603 100644 (file)
@@ -1,27 +1,41 @@
 /*
- * $Id$
- *
- * DEBUG: section 54    Interprocess Communication
+ * Copyright (C) 1996-2018 The Squid Software Foundation and contributors
  *
+ * Squid software is distributed under GPLv2+ license and includes
+ * contributions from numerous individuals and organizations.
+ * Please see the COPYING and CONTRIBUTORS files for details.
  */
 
-#include "config.h"
+/* DEBUG: section 54    Interprocess Communication */
+
+#include "squid.h"
+#include "globals.h"
 #include "ipc/Kid.h"
+#include "SquidConfig.h"
+
+#include <ctime>
+#if HAVE_SYS_WAIT_H
+#include <sys/wait.h>
+#endif
+
+int TheProcessKind = pkOther;
 
 Kid::Kid():
-        badFailures(0),
-        pid(-1),
-        startTime(0),
-        isRunning(false)
+    badFailures(0),
+    pid(-1),
+    startTime(0),
+    isRunning(false),
+    status(0)
 {
 }
 
 Kid::Kid(const String& kid_name):
-        theName(kid_name),
-        badFailures(0),
-        pid(-1),
-        startTime(0),
-        isRunning(false)
+    theName(kid_name),
+    badFailures(0),
+    pid(-1),
+    startTime(0),
+    isRunning(false),
+    status(0)
 {
 }
 
@@ -32,26 +46,52 @@ void Kid::start(pid_t cpid)
     assert(cpid > 0);
 
     isRunning = true;
+    stopTime = 0;
     pid = cpid;
-    time(&startTime);
+    startTime = squid_curtime;
 }
 
 /// called when kid terminates, sets exiting status
-void Kid::stop(status_type exitStatus)
+void
+Kid::stop(PidStatus const theExitStatus)
 {
     assert(running());
     assert(startTime != 0);
 
     isRunning = false;
+    stopTime = squid_curtime;
+    status = theExitStatus;
 
-    time_t stop_time;
-    time(&stop_time);
-    if ((stop_time - startTime) < fastFailureTimeLimit)
-        badFailures++;
+    if ((stopTime - startTime) < fastFailureTimeLimit)
+        ++badFailures;
     else
         badFailures = 0; // the failures are not "frequent" [any more]
 
-    status = exitStatus;
+    reportStopped(); // after all state changes
+}
+
+/// describes a recently stopped kid
+void
+Kid::reportStopped() const
+{
+    if (calledExit()) {
+        syslog(LOG_NOTICE,
+               "Squid Parent: %s process %d exited with status %d",
+               theName.termedBuf(), pid, exitStatus());
+    } else if (signaled()) {
+        syslog(LOG_NOTICE,
+               "Squid Parent: %s process %d exited due to signal %d with status %d",
+               theName.termedBuf(), pid, termSignal(), exitStatus());
+    } else {
+        syslog(LOG_NOTICE, "Squid Parent: %s process %d exited",
+               theName.termedBuf(), pid);
+    }
+
+    if (hopeless() && Config.hopelessKidRevivalDelay) {
+        syslog(LOG_NOTICE, "Squid Parent: %s process %d will not be restarted for %ld "
+               "seconds due to repeated, frequent failures",
+               theName.termedBuf(), pid, Config.hopelessKidRevivalDelay);
+    }
 }
 
 /// returns true if tracking of kid is stopped
@@ -60,6 +100,18 @@ bool Kid::running() const
     return isRunning;
 }
 
+/// returns true if master process should restart this kid
+bool Kid::shouldRestart() const
+{
+    return !(running() ||
+             exitedHappy() ||
+             hopeless() ||
+             shutting_down ||
+             signaled(SIGKILL) || // squid -k kill
+             signaled(SIGINT) || // unexpected forced shutdown
+             signaled(SIGTERM)); // unexpected forced shutdown
+}
+
 /// returns current pid for a running kid and last pid for a stopped kid
 pid_t Kid::getPid() const
 {
@@ -120,3 +172,10 @@ const String& Kid::name() const
 {
     return theName;
 }
+
+time_t
+Kid::deathDuration() const
+{
+    return squid_curtime > stopTime ? squid_curtime - stopTime : 0;
+}
+