]> git.ipfire.org Git - thirdparty/squid.git/commitdiff
Only one process (called the "primary" process) should handle WCCP. In SMP
authorAlex Rousskov <rousskov@measurement-factory.com>
Sat, 5 Jun 2010 19:08:44 +0000 (13:08 -0600)
committerAlex Rousskov <rousskov@measurement-factory.com>
Sat, 5 Jun 2010 19:08:44 +0000 (13:08 -0600)
mode, this is the Coordinator process.

Identified several commonly use process kinds and added the corresponding
Iam*Process() tests to avoid duplicating complex conditions throughout the
code. Note that kinds are not mutually exclusive. For example, a primary
process is also the master process and the worker process when running
in a no-daemon mode.

src/main.cc
src/protos.h
src/tools.cc

index 7eced83f5843b22e18a6d6d9dc5c66c853bdfef3..058b4d2c03f9f971785929d635fd042ce1a20e99 100644 (file)
@@ -621,11 +621,19 @@ shut_down(int sig)
 static void
 serverConnectionsOpen(void)
 {
-    // Coordinator does not start proxying services
-    if (!opt_no_daemon && Config.main_processes > 1 &&
-       KidIdentifier == Config.main_processes + 1)
-       return;
+    if (IamPrimaryProcess()) {
+#if USE_WCCP
+
+    wccpConnectionOpen();
+#endif
 
+#if USE_WCCPv2
+
+    wccp2ConnectionOpen();
+#endif
+    }
+    // Coordinator does not start proxying services
+    if (!IamCoordinatorProcess()) {
     clientOpenListenSockets();
     icpConnectionsOpen();
 #if USE_HTCP
@@ -636,15 +644,6 @@ serverConnectionsOpen(void)
 
     snmpConnectionOpen();
 #endif
-#if USE_WCCP
-
-    wccpConnectionOpen();
-#endif
-
-#if USE_WCCPv2
-
-    wccp2ConnectionOpen();
-#endif
 
     clientdbInit();
     icmpEngine.Open();
@@ -656,12 +655,25 @@ serverConnectionsOpen(void)
     carpInit();
     peerUserHashInit();
     peerSourceHashInit();
+    }
 }
 
 static void
 serverConnectionsClose(void)
 {
     assert(shutting_down || reconfiguring);
+
+    if (IamPrimaryProcess()) {
+#if USE_WCCP
+
+    wccpConnectionClose();
+#endif
+#if USE_WCCPv2
+
+    wccp2ConnectionClose();
+#endif
+    }
+    if (!IamCoordinatorProcess()) {
     clientHttpConnectionsClose();
     icpConnectionShutdown();
 #if USE_HTCP
@@ -674,16 +686,9 @@ serverConnectionsClose(void)
 
     snmpConnectionShutdown();
 #endif
-#if USE_WCCP
-
-    wccpConnectionClose();
-#endif
-#if USE_WCCPv2
-
-    wccp2ConnectionClose();
-#endif
 
     asnFreeMemory();
+    }
 }
 
 static void
@@ -779,6 +784,8 @@ mainReconfigureFinish(void *)
     redirectInit();
     authenticateInit(&Config.authConfiguration);
     externalAclInit();
+
+    if (IamPrimaryProcess()) {
 #if USE_WCCP
 
     wccpInit();
@@ -787,6 +794,7 @@ mainReconfigureFinish(void *)
 
     wccp2Init();
 #endif
+    }
 
     serverConnectionsOpen();
 
@@ -1041,6 +1049,7 @@ mainInitialize(void)
         //   moved to PconnModule::PconnModule()
     }
 
+    if (IamPrimaryProcess()) {
 #if USE_WCCP
     wccpInit();
 
@@ -1050,6 +1059,7 @@ mainInitialize(void)
     wccp2Init();
 
 #endif
+    }
 
     serverConnectionsOpen();
 
@@ -1421,12 +1431,10 @@ SquidMain(int argc, char **argv)
 
     mainLoop.setTimeService(&time_engine);
 
-    if (!opt_no_daemon && Config.main_processes > 1) {
-        if (KidIdentifier == Config.main_processes + 1)
-            AsyncJob::AsyncStart(Ipc::Coordinator::Instance());
-        else if (KidIdentifier != 0)
-            AsyncJob::AsyncStart(new Ipc::Strand);
-    }
+    if (IamCoordinatorProcess())
+        AsyncJob::AsyncStart(Ipc::Coordinator::Instance());
+    else if (UsingSmp() && IamWorkerProcess())
+        AsyncJob::AsyncStart(new Ipc::Strand);
 
     /* at this point we are finished the synchronous startup. */
     starting_up = 0;
@@ -1546,7 +1554,7 @@ static int
 checkRunningPid(void)
 {
     // master process must start alone, but its kids processes may co-exist
-    if (KidIdentifier != 0)
+    if (!IamMasterProcess())
         return 0;
 
     pid_t pid;
@@ -1588,7 +1596,7 @@ watch_child(char *argv[])
 
     int nullfd;
 
-    if (KidIdentifier != 0)
+    if (!IamMasterProcess())
         return;
 
     openlog(APP_SHORTNAME, LOG_PID | LOG_NDELAY | LOG_CONS, LOG_LOCAL4);
@@ -1853,7 +1861,7 @@ SquidShutdown()
 
 #endif
 
-    if (IsPidFileMaintainer()) {
+    if (IamPrimaryProcess()) {
         if (Config.pidFilename && strcmp(Config.pidFilename, "none") != 0) {
             enter_suid();
             safeunlink(Config.pidFilename, 0);
index b60f915e53c315471fcb1175787982251b513a35..37960375bc9a241011ce0d1dc0389ef6b79d2cdc 100644 (file)
@@ -576,7 +576,19 @@ SQUIDCEXTERN pid_t readPidFile(void);
 SQUIDCEXTERN void keepCapabilities(void);
 
 SQUIDCEXTERN void BroadcastSignalIfAny(int& sig);
-SQUIDCEXTERN bool IsPidFileMaintainer();
+/// whether the current process is the parent of all other Squid processes
+SQUIDCEXTERN bool IamMasterProcess();
+/** 
+    whether the current process is dedicated to doing things that only
+    a single process should do, such as PID file maintenance and WCCP
+*/
+SQUIDCEXTERN bool IamPrimaryProcess();
+/// whether the current process coordinates worker processes
+SQUIDCEXTERN bool IamCoordinatorProcess();
+/// whether the current process handles HTTP transactions and such
+SQUIDCEXTERN bool IamWorkerProcess();
+/// Whether there should be more than one worker process running
+SQUIDCEXTERN bool UsingSmp(); // try using specific Iam*() checks above first
 SQUIDCEXTERN int DebugSignal;
 
 /* AYJ debugs function to show locations being reset with memset() */
index df4eeeac01d615a6dfc12904ec1e4d78ffa16100..8b2e45f43f1299c8a343806902d8b7d198ee7d03 100644 (file)
@@ -404,10 +404,8 @@ void
 BroadcastSignalIfAny(int& sig)
 {
     if (sig > 0) {
-        if (!opt_no_daemon && Config.main_processes > 1) {
-            if (KidIdentifier == Config.main_processes + 1)
-                Ipc::Coordinator::Instance()->broadcastSignal(sig);
-        }
+        if (IamCoordinatorProcess())
+            Ipc::Coordinator::Instance()->broadcastSignal(sig);
         sig = -1;
     }
 }
@@ -807,23 +805,47 @@ no_suid(void)
 }
 
 bool
-IsPidFileMaintainer()
+IamMasterProcess()
 {
-    if (!opt_no_daemon && Config.main_processes > 0) {
-        if (Config.main_processes > 1) {
-            // multiple kids delegate PID file maintanence to the coordinator
-            if (KidIdentifier <= Config.main_processes)
-                return false;
-        } else {
-            // master process does not maintain PID file when
-            // Config.main_processes == 1
-            if (KidIdentifier == 0)
-                return false;
-        }
-    }
+    return KidIdentifier == 0;
+}
+
+bool
+IamWorkerProcess()
+{
+    // when there is only one process, it has to be the worker
+    if (opt_no_daemon || Config.main_processes == 0)
+        return true;
+
+    return 0 < KidIdentifier && KidIdentifier <= Config.main_processes;
+}
+
+bool
+UsingSmp()
+{
+    return !opt_no_daemon && Config.main_processes > 1;
+}
+
+bool
+IamCoordinatorProcess()
+{
+    return UsingSmp() && KidIdentifier == Config.main_processes + 1;
+}
+
+bool
+IamPrimaryProcess()
+{
+    // when there is only one process, it has to be primary
+    if (opt_no_daemon || Config.main_processes == 0)
+        return true;
+
+    // when there is a master and worker process, the master delegates
+    // primary functions to its only kid
+    if (Config.main_processes == 1)
+        return IamWorkerProcess();
 
-    // if there are no kids, then the master process maintains the PID file
-    return true;
+    // in SMP mode, multiple kids delegate primary functions to the coordinator
+    return IamCoordinatorProcess();
 }
 
 void
@@ -834,7 +856,7 @@ writePidFile(void)
     mode_t old_umask;
     char buf[32];
 
-    if (!IsPidFileMaintainer())
+    if (!IamPrimaryProcess())
         return;
 
     if ((f = Config.pidFilename) == NULL)