]> git.ipfire.org Git - thirdparty/squid.git/commitdiff
Port from 2.7: max_filedescriptor config option
authorAmos Jeffries <amosjeffries@squid-cache.org>
Fri, 11 Jun 2010 05:24:52 +0000 (23:24 -0600)
committerAmos Jeffries <amosjeffries@squid-cache.org>
Fri, 11 Jun 2010 05:24:52 +0000 (23:24 -0600)
Since Squid no longer really has any hardcoded filedescriptor limitations
it makes sense to have a squid.conf directive allowing the number of
filedescriptors to be tuned runtime. Default if unset is to obey whatever
ulimit settings as before.

 setMaxFD: figures out what to we can use for Squid_MaxFD

 setSystemLimits: Configures the system limitations to match our
expectations which might be lower than what setMaxFD finds if
the comm loop has additional restrictions

src/cf.data.pre
src/main.cc
src/protos.h
src/structs.h
src/tools.cc

index e16572918a61a3c16647942613f59f255e48cd15..b13b1c01bce3801d781a3321fb890b61bbaafc91 100644 (file)
@@ -6972,4 +6972,17 @@ DOC_START
        Whether to lookup the EUI or MAC address of a connected client.
 DOC_END
 
+NAME: max_filedescriptors max_filedesc
+TYPE: int
+DEFAULT: 0
+LOC: Config.max_filedescriptors
+DOC_START
+       The maximum number of filedescriptors supported.
+
+       The default "0" means Squid inherits the current ulimit setting.
+
+       Note: Changing this requires a restart of Squid. Also
+       not all comm loops supports large values.
+DOC_END
+
 EOF
index b4037ed2433f14a78579b4efea3631ef56adf882..b251d44af37bc594f60b123a3ed31afd701c3a7b 100644 (file)
@@ -932,7 +932,7 @@ mainInitialize(void)
 #endif
 
     debugs(1, 1, "Process ID " << getpid());
-
+    setSystemLimits();
     debugs(1, 1, "With " << Squid_MaxFD << " file descriptors available");
 
 #ifdef _SQUID_MSWIN_
index 1e216621d9a91c7ebf3a473ea2261cac849b64d7..ab87c9590d0244b4d09875a680af8fc590cc6129 100644 (file)
@@ -568,6 +568,7 @@ SQUIDCEXTERN void no_suid(void);
 SQUIDCEXTERN void writePidFile(void);
 SQUIDCEXTERN void setSocketShutdownLifetimes(int);
 SQUIDCEXTERN void setMaxFD(void);
+SQUIDCEXTERN void setSystemLimits(void);
 SQUIDCEXTERN void squid_signal(int sig, SIGHDLR *, int flags);
 SQUIDCEXTERN pid_t readPidFile(void);
 SQUIDCEXTERN void keepCapabilities(void);
index 258a4dfbc8578609244574aeaf5f027ab4c8623c..aac9ffcfe7b75f59831bb7fc5d977a779d46fb82 100644 (file)
@@ -610,6 +610,7 @@ struct SquidConfig {
 
     char *accept_filter;
     int umask;
+    int max_filedescriptors;
 
 #if USE_LOADABLE_MODULES
     wordlist *loadable_module_names;
index a27372d902e8403e1d9ee6d1d2216d96188d2b46..cd4b71fbeb7662cbf661027ef89984377d3668c0 100644 (file)
@@ -872,69 +872,76 @@ readPidFile(void)
     return pid;
 }
 
+/* A little piece of glue for odd systems */
+#ifndef RLIMIT_NOFILE
+#ifdef RLIMIT_OFILE
+#define RLIMIT_NOFILE RLIMIT_OFILE
+#endif
+#endif
 
+/** Figure out the number of supported filedescriptors */
 void
 setMaxFD(void)
 {
-#if HAVE_SETRLIMIT
-    /* try to use as many file descriptors as possible */
-    /* System V uses RLIMIT_NOFILE and BSD uses RLIMIT_OFILE */
-
+#if HAVE_SETRLIMIT && defined(RLIMIT_NOFILE)
     struct rlimit rl;
-#if defined(RLIMIT_NOFILE)
-
     if (getrlimit(RLIMIT_NOFILE, &rl) < 0) {
-        debugs(50, 0, "setrlimit: RLIMIT_NOFILE: " << xstrerror());
-    } else {
-        rl.rlim_cur = Squid_MaxFD;
-
+        debugs(50, DBG_CRITICAL, "setrlimit: RLIMIT_NOFILE: " << xstrerror());
+    } else if (Config.max_filedescriptors > 0) {
+        rl.rlim_cur = Config.max_filedescriptors;
         if (rl.rlim_cur > rl.rlim_max)
-            Squid_MaxFD = rl.rlim_cur = rl.rlim_max;
-
-        if (setrlimit(RLIMIT_NOFILE, &rl) < 0) {
-            snprintf(tmp_error_buf, ERROR_BUF_SZ,
-                     "setrlimit: RLIMIT_NOFILE: %s", xstrerror());
-            fatal_dump(tmp_error_buf);
+            rl.rlim_max = rl.rlim_cur;
+        if (setrlimit(RLIMIT_NOFILE, &rl)) {
+            debugs(50, DBG_CRITICAL, "setrlimit: RLIMIT_NOFILE: " << xstrerror());
+            getrlimit(RLIMIT_NOFILE, &rl);
+            rl.rlim_cur = rl.rlim_max;
+            if (setrlimit(RLIMIT_NOFILE, &rl)) {
+                debugs(50, DBG_CRITICAL, "setrlimit: RLIMIT_NOFILE: " << xstrerror());
+            }
         }
     }
-
-#elif defined(RLIMIT_OFILE)
-    if (getrlimit(RLIMIT_OFILE, &rl) < 0) {
-        debugs(50, 0, "setrlimit: RLIMIT_NOFILE: " << xstrerror());
+    if (getrlimit(RLIMIT_NOFILE, &rl) < 0) {
+         debugs(50, DBG_CRITICAL, "setrlimit: RLIMIT_NOFILE: " << xstrerror());
     } else {
-        rl.rlim_cur = Squid_MaxFD;
+        Squid_MaxFD = rl.rlim_cur;
+    }
 
-        if (rl.rlim_cur > rl.rlim_max)
-            Squid_MaxFD = rl.rlim_cur = rl.rlim_max;
+#endif /* HAVE_SETRLIMIT */
+}
 
-        if (setrlimit(RLIMIT_OFILE, &rl) < 0) {
-            snprintf(tmp_error_buf, ERROR_BUF_SZ,
-                     "setrlimit: RLIMIT_OFILE: %s", xstrerror());
+void
+setSystemLimits(void)
+{
+#if HAVE_SETRLIMIT && defined(RLIMIT_NOFILE) && !defined(_SQUID_CYGWIN_)
+    /* limit system filedescriptors to our own limit */
+    struct rlimit rl;
+    if (getrlimit(RLIMIT_NOFILE, &rl) < 0) {
+        debugs(50, DBG_CRITICAL, "setrlimit: RLIMIT_NOFILE: " << xstrerror());
+    } else {
+        rl.rlim_cur = Squid_MaxFD;
+        if (setrlimit(RLIMIT_NOFILE, &rl) < 0) {
+            snprintf(tmp_error_buf, ERROR_BUF_SZ, "setrlimit: RLIMIT_NOFILE: %s", xstrerror());
             fatal_dump(tmp_error_buf);
         }
     }
-
-#endif
-#else /* HAVE_SETRLIMIT */
-    debugs(21, 1, "setMaxFD: Cannot increase: setrlimit() not supported on this system");
-
 #endif /* HAVE_SETRLIMIT */
 
 #if HAVE_SETRLIMIT && defined(RLIMIT_DATA)
-
     if (getrlimit(RLIMIT_DATA, &rl) < 0) {
-        debugs(50, 0, "getrlimit: RLIMIT_DATA: " << xstrerror());
+        debugs(50, DBG_CRITICAL, "getrlimit: RLIMIT_DATA: " << xstrerror());
     } else if (rl.rlim_max > rl.rlim_cur) {
         rl.rlim_cur = rl.rlim_max;     /* set it to the max */
 
         if (setrlimit(RLIMIT_DATA, &rl) < 0) {
-            snprintf(tmp_error_buf, ERROR_BUF_SZ,
-                     "setrlimit: RLIMIT_DATA: %s", xstrerror());
+            snprintf(tmp_error_buf, ERROR_BUF_SZ, "setrlimit: RLIMIT_DATA: %s", xstrerror());
             fatal_dump(tmp_error_buf);
         }
     }
-
 #endif /* RLIMIT_DATA */
+    if (Config.max_filedescriptors > Squid_MaxFD) {
+        debugs(50, DBG_IMPORTANT, "NOTICE: Could not increase the number of filedescriptors");
+    }
+
 #if HAVE_SETRLIMIT && defined(RLIMIT_VMEM)
     if (getrlimit(RLIMIT_VMEM, &rl) < 0) {
         debugs(50, 0, "getrlimit: RLIMIT_VMEM: " << xstrerror());
@@ -942,12 +949,10 @@ setMaxFD(void)
         rl.rlim_cur = rl.rlim_max;     /* set it to the max */
 
         if (setrlimit(RLIMIT_VMEM, &rl) < 0) {
-            snprintf(tmp_error_buf, ERROR_BUF_SZ,
-                     "setrlimit: RLIMIT_VMEM: %s", xstrerror());
+            snprintf(tmp_error_buf, ERROR_BUF_SZ, "setrlimit: RLIMIT_VMEM: %s", xstrerror());
             fatal_dump(tmp_error_buf);
         }
     }
-
 #endif /* RLIMIT_VMEM */
 }