]> git.ipfire.org Git - thirdparty/squid.git/blobdiff - src/main.cc
Renamed squid.h to squid-old.h and config.h to squid.h
[thirdparty/squid.git] / src / main.cc
index 485f1726bb64c190368342531534887c7a8a4cd0..b4a3c5df6e853cd65ff21360c2a5b66cb4dfa89b 100644 (file)
@@ -32,7 +32,7 @@
  *
  */
 
-#include "squid.h"
+#include "squid-old.h"
 #include "AccessLogEntry.h"
 #if USE_ADAPTATION
 #include "adaptation/Config.h"
@@ -47,6 +47,7 @@
 #if USE_AUTH
 #include "auth/Gadgets.h"
 #endif
+#include "base/RunnersRegistry.h"
 #include "base/Subscription.h"
 #include "base/TextException.h"
 #if USE_DELAY_POOLS
 #include "event.h"
 #include "EventLoop.h"
 #include "ExternalACL.h"
+#include "format/Token.h"
 #include "fs/Module.h"
 #include "PeerSelectState.h"
+#include "SquidDns.h"
 #include "Store.h"
 #include "ICP.h"
 #include "ident/Ident.h"
@@ -80,6 +83,7 @@
 #include "ipc/Strand.h"
 #include "ip/tools.h"
 #include "SquidTime.h"
+#include "StatCounters.h"
 #include "SwapDir.h"
 #include "forward.h"
 #include "MemPool.h"
@@ -130,7 +134,7 @@ void WINAPI WIN32_svcHandler(DWORD);
 
 #endif
 
-#ifndef SQUID_BUILD_INFO
+#if !defined(SQUID_BUILD_INFO)
 #define SQUID_BUILD_INFO ""
 #endif
 
@@ -168,7 +172,7 @@ static void SquidShutdown(void);
 static void mainSetCwd(void);
 static int checkRunningPid(void);
 
-#ifndef _SQUID_MSWIN_
+#if !_SQUID_MSWIN_
 static const char *squid_start_script = "squid_start";
 #endif
 
@@ -418,26 +422,18 @@ mainParseOptions(int argc, char *argv[])
                 opt_send_signal = SIGHUP;
             else if (!strncmp(optarg, "rotate", strlen(optarg)))
                 /** \li On rotate send SIGQUIT or SIGUSR1. */
-#ifdef _SQUID_LINUX_THREADS_
-
+#if defined(_SQUID_LINUX_THREADS_)
                 opt_send_signal = SIGQUIT;
-
 #else
-
                 opt_send_signal = SIGUSR1;
-
 #endif
 
             else if (!strncmp(optarg, "debug", strlen(optarg)))
                 /** \li On debug send SIGTRAP or SIGUSR2. */
-#ifdef _SQUID_LINUX_THREADS_
-
+#if defined(_SQUID_LINUX_THREADS_)
                 opt_send_signal = SIGTRAP;
-
 #else
-
                 opt_send_signal = SIGUSR2;
-
 #endif
 
             else if (!strncmp(optarg, "shutdown", strlen(optarg)))
@@ -591,7 +587,7 @@ rotate_logs(int sig)
 {
     do_rotate = 1;
     RotateSignal = sig;
-#ifndef _SQUID_MSWIN_
+#if !_SQUID_MSWIN_
 #if !HAVE_SIGACTION
 
     signal(sig, rotate_logs);
@@ -605,7 +601,7 @@ reconfigure(int sig)
 {
     do_reconfigure = 1;
     ReconfigureSignal = sig;
-#ifndef _SQUID_MSWIN_
+#if !_SQUID_MSWIN_
 #if !HAVE_SIGACTION
 
     signal(sig, reconfigure);
@@ -634,7 +630,7 @@ shut_down(int sig)
                    " pid " << ppid << ": " << xstrerror());
     }
 
-#ifndef _SQUID_MSWIN_
+#if !_SQUID_MSWIN_
 #if KILL_PARENT_OPT
 
     if (!IamMasterProcess() && ppid > 1) {
@@ -668,8 +664,8 @@ serverConnectionsOpen(void)
         wccp2ConnectionOpen();
 #endif
     }
-    // Coordinator does not start proxying services
-    if (!IamCoordinatorProcess()) {
+    // start various proxying services if we are responsible for them
+    if (IamWorkerProcess()) {
         clientOpenListenSockets();
         icpConnectionsOpen();
 #if USE_HTCP
@@ -677,7 +673,6 @@ serverConnectionsOpen(void)
         htcpInit();
 #endif
 #if SQUID_SNMP
-
         snmpConnectionOpen();
 #endif
 
@@ -711,7 +706,7 @@ serverConnectionsClose(void)
         wccp2ConnectionClose();
 #endif
     }
-    if (!IamCoordinatorProcess()) {
+    if (IamWorkerProcess()) {
         clientHttpConnectionsClose();
         icpConnectionShutdown();
 #if USE_HTCP
@@ -721,8 +716,7 @@ serverConnectionsClose(void)
 
         icmpEngine.Close();
 #if SQUID_SNMP
-
-        snmpConnectionShutdown();
+        snmpConnectionClose();
 #endif
 
         asnFreeMemory();
@@ -742,17 +736,7 @@ mainReconfigureStart(void)
 
     htcpSocketClose();
 #endif
-#if SQUID_SNMP
-
-    snmpConnectionClose();
-#endif
-#if USE_DNSSERVERS
-
     dnsShutdown();
-#else
-
-    idnsShutdown();
-#endif
 #if USE_SSL_CRTD
     Ssl::Helper::GetInstance()->Shutdown();
 #endif
@@ -835,13 +819,7 @@ mainReconfigureFinish(void *)
     icapLogOpen();
 #endif
     storeLogOpen();
-#if USE_DNSSERVERS
-
     dnsInit();
-#else
-
-    idnsInit();
-#endif
 #if USE_SSL_CRTD
     Ssl::Helper::GetInstance()->Init();
 #endif
@@ -871,6 +849,11 @@ mainReconfigureFinish(void *)
 
     mimeInit(Config.mimeTablePathname);
 
+#if USE_UNLINKD
+    if (unlinkdNeeded())
+        unlinkdInit();
+#endif
+
 #if USE_DELAY_POOLS
     Config.ClientDelay.finalize();
 #endif
@@ -894,7 +877,7 @@ static void
 mainRotate(void)
 {
     icmpEngine.Close();
-#if USE_DNSSERVERS
+#if USE_DNSHELPER
     dnsShutdown();
 #endif
     redirectShutdown();
@@ -911,7 +894,7 @@ mainRotate(void)
     icapLogRotate();               /*icap.log*/
 #endif
     icmpEngine.Open();
-#if USE_DNSSERVERS
+#if USE_DNSHELPER
     dnsInit();
 #endif
     redirectInit();
@@ -926,7 +909,7 @@ setEffectiveUser(void)
 {
     keepCapabilities();
     leave_suid();              /* Run as non privilegied user */
-#ifdef _SQUID_OS2_
+#if _SQUID_OS2_
 
     return;
 #endif
@@ -984,7 +967,7 @@ mainInitialize(void)
     setEffectiveUser();
 
     if (icpPortNumOverride != 1)
-        Config.Port.icp = (u_short) icpPortNumOverride;
+        Config.Port.icp = (unsigned short) icpPortNumOverride;
 
     _db_init(Debug::cache_log, Debug::debugOptions);
 
@@ -1007,10 +990,13 @@ mainInitialize(void)
 #endif
 
     debugs(1, 1, "Process ID " << getpid());
+
+    debugs(1, 1, "Process Roles:" << ProcessRoles());
+
     setSystemLimits();
     debugs(1, 1, "With " << Squid_MaxFD << " file descriptors available");
 
-#ifdef _SQUID_MSWIN_
+#if _SQUID_MSWIN_
 
     debugs(1, 1, "With " << _getmaxstdio() << " CRT stdio descriptors available");
 
@@ -1032,14 +1018,10 @@ mainInitialize(void)
 
     parseEtcHosts();
 
-#if USE_DNSSERVERS
-
     dnsInit();
 
-#else
-
-    idnsInit();
-
+#if USE_SSL_CRTD
+    Ssl::Helper::GetInstance()->Init();
 #endif
 
     redirectInit();
@@ -1077,7 +1059,8 @@ mainInitialize(void)
 
     if (!configured_once) {
 #if USE_UNLINKD
-        unlinkdInit();
+        if (unlinkdNeeded())
+            unlinkdInit();
 #endif
 
         urlInitialize();
@@ -1131,7 +1114,7 @@ mainInitialize(void)
     if (!configured_once)
         writePidFile();                /* write PID file */
 
-#ifdef _SQUID_LINUX_THREADS_
+#if defined(_SQUID_LINUX_THREADS_)
 
     squid_signal(SIGQUIT, rotate_logs, SA_RESTART);
 
@@ -1238,10 +1221,11 @@ SquidMainSafe(int argc, char **argv)
     try {
         return SquidMain(argc, argv);
     } catch (const std::exception &e) {
-        std::cerr << "dying from an unhandled exception: " << e.what() << std::endl;
+        debugs(1, DBG_CRITICAL, "FATAL: dying from an unhandled exception: " <<
+               e.what());
         throw;
     } catch (...) {
-        std::cerr << "dying from an unhandled exception." << std::endl;
+        debugs(1, DBG_CRITICAL, "FATAL: dying from an unhandled exception.");
         throw;
     }
     return -1; // not reached
@@ -1258,6 +1242,14 @@ ConfigureCurrentKid(const char *processName)
             const size_t nameLen = idStart - (processName + 1);
             assert(nameLen < sizeof(TheKidName));
             xstrncpy(TheKidName, processName + 1, nameLen + 1);
+            if (!strcmp(TheKidName, "squid-coord"))
+                TheProcessKind = pkCoordinator;
+            else if (!strcmp(TheKidName, "squid"))
+                TheProcessKind = pkWorker;
+            else if (!strcmp(TheKidName, "squid-disk"))
+                TheProcessKind = pkDisker;
+            else
+                TheProcessKind = pkOther; // including coordinator
         }
     } else {
         xstrncpy(TheKidName, APP_SHORTNAME, sizeof(TheKidName));
@@ -1382,6 +1374,8 @@ SquidMain(int argc, char **argv)
 #endif
         Ip::ProbeTransport(); // determine IPv4 or IPv6 capabilities before parsing.
 
+        Format::Token::Init(); // XXX: temporary. Use a runners registry of pre-parse runners instead.
+
         parse_err = parseConfigFile(ConfigFile);
 
         Mem::Report();
@@ -1423,6 +1417,12 @@ SquidMain(int argc, char **argv)
         /* NOTREACHED */
     }
 
+    debugs(1,2, HERE << "Doing post-config initialization\n");
+    leave_suid();
+    ActivateRegistered(rrClaimMemoryNeeds);
+    ActivateRegistered(rrAfterConfig);
+    enter_suid();
+
     if (!opt_no_daemon && Config.workers > 0)
         watch_child(argv);
 
@@ -1497,7 +1497,7 @@ SquidMain(int argc, char **argv)
 
     if (IamCoordinatorProcess())
         AsyncJob::Start(Ipc::Coordinator::Instance());
-    else if (UsingSmp() && IamWorkerProcess())
+    else if (UsingSmp() && (IamWorkerProcess() || IamDiskProcess()))
         AsyncJob::Start(new Ipc::Strand);
 
     /* at this point we are finished the synchronous startup. */
@@ -1534,7 +1534,7 @@ sendSignal(void)
             WIN32_sendSignal(opt_send_signal);
             exit(0);
         } else
-#ifdef _SQUID_MSWIN_
+#if _SQUID_MSWIN_
         {
             fprintf(stderr, "%s: ERROR: Could not send ", APP_SHORTNAME);
             fprintf(stderr, "signal to Squid Service:\n");
@@ -1569,7 +1569,7 @@ sendSignal(void)
     exit(0);
 }
 
-#ifndef _SQUID_MSWIN_
+#if !_SQUID_MSWIN_
 /*
  * This function is run when Squid is in daemon mode, just
  * before the parent forks and starts up the child process.
@@ -1599,7 +1599,7 @@ mainStartScript(const char *prog)
         _exit(-1);
     } else {
         do {
-#ifdef _SQUID_NEXT_
+#if _SQUID_NEXT_
             union wait status;
             rpid = wait4(cpid, &status, 0, NULL);
 #else
@@ -1642,9 +1642,9 @@ checkRunningPid(void)
 static void
 watch_child(char *argv[])
 {
-#ifndef _SQUID_MSWIN_
+#if !_SQUID_MSWIN_
     char *prog;
-#ifdef _SQUID_NEXT_
+#if _SQUID_NEXT_
 
     union wait status;
 #else
@@ -1710,7 +1710,9 @@ watch_child(char *argv[])
                Config.workers);
         // but we keep going in hope that user knows best
     }
-    TheKids.init(Config.workers);
+    TheKids.init();
+
+    syslog(LOG_NOTICE, "Squid Parent: will start %d kids", (int)TheKids.count());
 
     // keep [re]starting kids until it is time to quit
     for (;;) {
@@ -1732,7 +1734,8 @@ watch_child(char *argv[])
             }
 
             kid.start(pid);
-            syslog(LOG_NOTICE, "Squid Parent: child process %d started", pid);
+            syslog(LOG_NOTICE, "Squid Parent: %s process %d started",
+                   kid.name().termedBuf(), pid);
         }
 
         /* parent */
@@ -1740,7 +1743,7 @@ watch_child(char *argv[])
 
         squid_signal(SIGINT, SIG_IGN, SA_RESTART);
 
-#ifdef _SQUID_NEXT_
+#if _SQUID_NEXT_
 
         pid = wait3(&status, 0, NULL);
 
@@ -1756,24 +1759,27 @@ watch_child(char *argv[])
                 kid->stop(status);
                 if (kid->calledExit()) {
                     syslog(LOG_NOTICE,
-                           "Squid Parent: child process %d exited with status %d",
+                           "Squid Parent: %s process %d exited with status %d",
+                           kid->name().termedBuf(),
                            kid->getPid(), kid->exitStatus());
                 } else if (kid->signaled()) {
                     syslog(LOG_NOTICE,
-                           "Squid Parent: child process %d exited due to signal %d with status %d",
+                           "Squid Parent: %s process %d exited due to signal %d with status %d",
+                           kid->name().termedBuf(),
                            kid->getPid(), kid->termSignal(), kid->exitStatus());
                 } else {
-                    syslog(LOG_NOTICE, "Squid Parent: child process %d exited", kid->getPid());
+                    syslog(LOG_NOTICE, "Squid Parent: %s process %d exited",
+                           kid->name().termedBuf(), kid->getPid());
                 }
                 if (kid->hopeless()) {
-                    syslog(LOG_NOTICE, "Squid Parent: child process %d will not"
+                    syslog(LOG_NOTICE, "Squid Parent: %s process %d will not"
                            " be restarted due to repeated, frequent failures",
-                           kid->getPid());
+                           kid->name().termedBuf(), kid->getPid());
                 }
             } else {
                 syslog(LOG_NOTICE, "Squid Parent: unknown child process %d exited", pid);
             }
-#ifdef _SQUID_NEXT_
+#if _SQUID_NEXT_
         } while ((pid = wait3(&status, WNOHANG, NULL)) > 0);
 #else
         }
@@ -1781,6 +1787,11 @@ watch_child(char *argv[])
 #endif
 
         if (!TheKids.someRunning() && !TheKids.shouldRestartSome()) {
+            leave_suid();
+            DeactivateRegistered(rrAfterConfig);
+            DeactivateRegistered(rrClaimMemoryNeeds);
+            enter_suid();
+
             if (TheKids.someSignaled(SIGINT) || TheKids.someSignaled(SIGTERM)) {
                 syslog(LOG_ALERT, "Exiting due to unexpected forced shutdown");
                 exit(1);
@@ -1817,13 +1828,7 @@ SquidShutdown()
 #endif
 
     debugs(1, 1, "Shutting down...");
-#if USE_DNSSERVERS
-
     dnsShutdown();
-#else
-
-    idnsShutdown();
-#endif
 #if USE_SSL_CRTD
     Ssl::Helper::GetInstance()->Shutdown();
 #endif
@@ -1835,7 +1840,6 @@ SquidShutdown()
     htcpSocketClose();
 #endif
 #if SQUID_SNMP
-
     snmpConnectionClose();
 #endif
 #if USE_WCCP
@@ -1880,6 +1884,8 @@ SquidShutdown()
     Store::Root().sync();              /* Flush log close */
     StoreFileSystem::FreeAllFs();
     DiskIOModule::FreeAllModules();
+    DeactivateRegistered(rrAfterConfig);
+    DeactivateRegistered(rrClaimMemoryNeeds);
 #if LEAK_CHECK_MODE && 0 /* doesn't work at the moment */
 
     configFreeMemory();