]> git.ipfire.org Git - thirdparty/cups.git/blobdiff - scheduler/listen.c
Merge changes from CUPS 1.4svn-r7961.
[thirdparty/cups.git] / scheduler / listen.c
index 37911059e4ba793e59aab7bdd717d2eaa1dbc237..2ab8798d8d9f84f88459705ca3fe143d4f50bbec 100644 (file)
@@ -1,26 +1,17 @@
 /*
- * "$Id$"
+ * "$Id: listen.c 7918 2008-09-08 22:03:01Z mike $"
  *
  *   Server listening routines for the Common UNIX Printing System (CUPS)
  *   scheduler.
  *
+ *   Copyright 2007-2008 by Apple Inc.
  *   Copyright 1997-2006 by Easy Software Products, all rights reserved.
  *
  *   These coded instructions, statements, and computer programs are the
- *   property of Easy Software Products and are protected by Federal
- *   copyright law.  Distribution and use rights are outlined in the file
- *   "LICENSE.txt" which should have been included with this file.  If this
- *   file is missing or damaged please contact Easy Software Products
- *   at:
- *
- *       Attn: CUPS Licensing Information
- *       Easy Software Products
- *       44141 Airport View Drive, Suite 204
- *       Hollywood, Maryland 20636 USA
- *
- *       Voice: (301) 373-9600
- *       EMail: cups-info@cups.org
- *         WWW: http://www.cups.org
+ *   property of Apple Inc. and are protected by Federal copyright
+ *   law.  Distribution and use rights are outlined in the file "LICENSE.txt"
+ *   which should have been included with this file.  If this file is
+ *   file is missing or damaged, see the license at "http://www.cups.org/".
  *
  * Contents:
  *
@@ -84,20 +75,19 @@ cupsdPauseListening(void)
   if (cupsArrayCount(Clients) == MaxClients)
     cupsdLogMessage(CUPSD_LOG_WARN,
                     "Max clients reached, holding new connections...");
+  else if (errno == ENFILE || errno == EMFILE)
+    cupsdLogMessage(CUPSD_LOG_WARN,
+                    "Too many open files, holding new connections for "
+                   "30 seconds...");
 
   cupsdLogMessage(CUPSD_LOG_DEBUG2, "cupsdPauseListening: Clearing input bits...");
 
   for (lis = (cupsd_listener_t *)cupsArrayFirst(Listeners);
        lis;
        lis = (cupsd_listener_t *)cupsArrayNext(Listeners))
-    if (lis->fd >= 0)
-    {
-      cupsdLogMessage(CUPSD_LOG_DEBUG2,
-                      "cupsdPauseListening: Removing fd %d from InputSet...",
-                      lis->fd);
+    cupsdRemoveSelect(lis->fd);
 
-      FD_CLR(lis->fd, InputSet);
-    }
+  ListeningPaused = time(NULL) + 30;
 }
 
 
@@ -114,22 +104,16 @@ cupsdResumeListening(void)
   if (cupsArrayCount(Listeners) < 1)
     return;
 
-  if (cupsArrayCount(Clients) >= (MaxClients - 1))
-    cupsdLogMessage(CUPSD_LOG_WARN, "Resuming new connection processing...");
-
+  cupsdLogMessage(CUPSD_LOG_INFO, "Resuming new connection processing...");
   cupsdLogMessage(CUPSD_LOG_DEBUG2,
                   "cupsdResumeListening: Setting input bits...");
 
   for (lis = (cupsd_listener_t *)cupsArrayFirst(Listeners);
        lis;
        lis = (cupsd_listener_t *)cupsArrayNext(Listeners))
-    if (lis->fd >= 0)
-    {
-      cupsdLogMessage(CUPSD_LOG_DEBUG2,
-                      "cupsdResumeListening: Adding fd %d to InputSet...",
-                      lis->fd);
-      FD_SET(lis->fd, InputSet);
-    }
+    cupsdAddSelect(lis->fd, (cupsd_selfunc_t)cupsdAcceptClient, NULL, lis);
+
+  ListeningPaused = 0;
 }
 
 
@@ -166,10 +150,15 @@ cupsdStartListening(void)
     httpAddrFreeList(ServerAddrs);
 
   if ((ServerAddrs = httpAddrGetList(ServerName, AF_UNSPEC, NULL)) == NULL)
+  {
     cupsdLogMessage(CUPSD_LOG_ERROR,
-                    "Unable to find IP address for server name \"%s\"!\n",
+                    "Unable to find IP address for server name \"%s\"!",
                    ServerName);
 
+    if (FatalErrors & CUPSD_FATAL_LISTEN)
+      cupsdEndProcess(getpid(), 0);
+  }
+
  /*
   * Setup socket listeners...
   */
@@ -210,6 +199,20 @@ cupsdStartListening(void)
        cupsdLogMessage(CUPSD_LOG_ERROR,
                        "Unable to open listen socket for address %s:%d - %s.",
                        s, p, strerror(errno));
+
+#ifdef AF_INET6
+       /*
+        * IPv6 is often disabled while DNS returns IPv6 addresses...
+       */
+
+       if (lis->address.addr.sa_family != AF_INET6 &&
+           (FatalErrors & CUPSD_FATAL_LISTEN))
+         cupsdEndProcess(getpid(), 0);
+#else
+       if (FatalErrors & CUPSD_FATAL_LISTEN)
+         cupsdEndProcess(getpid(), 0);
+#endif /* AF_INET6 */
+
        continue;
       }
 
@@ -294,6 +297,10 @@ cupsdStartListening(void)
                        s, p, strerror(errno));
        close(lis->fd);
        lis->fd = -1;
+
+       if (FatalErrors & CUPSD_FATAL_LISTEN)
+         cupsdEndProcess(getpid(), 0);
+
        continue;
       }
 
@@ -306,7 +313,14 @@ cupsdStartListening(void)
        cupsdLogMessage(CUPSD_LOG_ERROR,
                        "Unable to listen for clients on address %s:%d - %s.",
                        s, p, strerror(errno));
-       exit(errno);
+
+       close(lis->fd);
+       lis->fd = -1;
+
+       if (FatalErrors & CUPSD_FATAL_LISTEN)
+         cupsdEndProcess(getpid(), 0);
+
+        continue;
       }
     }
 
@@ -331,7 +345,7 @@ cupsdStartListening(void)
     * "any" address...
     */
 
-    if (!LocalPort && p > 0 &&
+    if ((!LocalPort || LocalEncryption == HTTP_ENCRYPT_ALWAYS) && p > 0 &&
         (httpAddrLocalhost(&(lis->address)) ||
          httpAddrAny(&(lis->address))))
     {
@@ -352,13 +366,11 @@ cupsdStartListening(void)
   if (!LocalPort && !have_domain)
   {
     cupsdLogMessage(CUPSD_LOG_EMERG,
-                    "No Listen or Port lines were found to allow access via localhost!");
-
-   /*
-    * Commit suicide...
-    */
+                    "No Listen or Port lines were found to allow access via "
+                   "localhost!");
 
-    cupsdEndProcess(getpid(), 0);
+    if (FatalErrors & (CUPSD_FATAL_CONFIG | CUPSD_FATAL_LISTEN))
+      cupsdEndProcess(getpid(), 0);
   }
 
  /*
@@ -373,6 +385,8 @@ cupsdStartListening(void)
     */
 
     cupsdSetEnv("CUPS_SERVER", have_domain);
+
+    LocalEncryption = HTTP_ENCRYPT_IF_REQUESTED;
   }
   else
   {
@@ -384,7 +398,9 @@ cupsdStartListening(void)
   }
 
   cupsdSetEnv("CUPS_ENCRYPTION", encryptions[LocalEncryption]);
-  cupsdSetEnvf("IPP_PORT", "%d", LocalPort);
+
+  if (LocalPort)
+    cupsdSetEnvf("IPP_PORT", "%d", LocalPort);
 
  /*
   * Resume listening for connections...
@@ -439,5 +455,5 @@ cupsdStopListening(void)
 
 
 /*
- * End of "$Id$".
+ * End of "$Id: listen.c 7918 2008-09-08 22:03:01Z mike $".
  */