]> git.ipfire.org Git - thirdparty/cups.git/blobdiff - scheduler/cups-polld.c
Load cups into easysw/current.
[thirdparty/cups.git] / scheduler / cups-polld.c
index 259361d5ba9e178783af8b85ff6a8f3a5bd30f4c..be6bbb5e62f1bb5b365a63f2d70ef91e1335142f 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * "$Id: cups-polld.c 5753 2006-07-18 19:53:24Z mike $"
+ * "$Id: cups-polld.c 5871 2006-08-23 20:55:33Z mike $"
  *
  *   Polling daemon for the Common UNIX Printing System (CUPS).
  *
  *
  * Contents:
  *
- *   main()        - Open sockets and poll until we are killed...
- *   dequote()     - Remote quotes from a string.
- *   poll_server() - Poll the server for the given set of printers or classes.
+ *   main()           - Open sockets and poll until we are killed...
+ *   dequote()        - Remote quotes from a string.
+ *   poll_server()    - Poll the server for the given set of printers or
+ *                      classes.
+ *   sighup_handler() - Handle 'hangup' signals to restart polling.
  */
 
 /*
 #include <errno.h>
 #include <cups/language.h>
 #include <cups/string.h>
+#include <signal.h>
+
+
+/*
+ * Local globals...
+ */
+
+static int     restart_polling = 1;
 
 
 /*
@@ -47,6 +57,7 @@
 static char    *dequote(char *d, const char *s, int dlen);
 static int     poll_server(http_t *http, int sock, int port, int interval,
                            const char *prefix);
+static void    sighup_handler(int sig);
 
 
 /*
@@ -65,8 +76,28 @@ main(int  argc,                              /* I - Number of command-line args */
   int          seconds,                /* Seconds left from poll */
                remain;                 /* Total remaining time to sleep */
   char         prefix[1024];           /* Prefix for log messages */
+#if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
+  struct sigaction action;             /* Actions for POSIX signals */
+#endif /* HAVE_SIGACTION && !HAVE_SIGSET */
 
 
+ /*
+  * Catch hangup signals for when the network changes...
+  */
+
+#ifdef HAVE_SIGSET /* Use System V signals over POSIX to avoid bugs */
+  sigset(SIGHUP, sighup_handler);
+#elif defined(HAVE_SIGACTION)
+  memset(&action, 0, sizeof(action));
+
+  sigemptyset(&action.sa_mask);
+  sigaddset(&action.sa_mask, SIGHUP);
+  action.sa_handler = sighup_handler;
+  sigaction(SIGHUP, &action, NULL);
+#else
+  signal(SIGHUP, sighup_handler);
+#endif /* HAVE_SIGSET */
+
  /*
   * Don't buffer log messages...
   */
@@ -119,38 +150,43 @@ main(int  argc,                           /* I - Number of command-line args */
   }
 
  /*
-  * Open a connection to the server...
+  * Loop forever, asking for available printers and classes...
   */
 
-  while ((http = httpConnectEncrypt(argv[1], atoi(argv[2]),
-                                    cupsEncryption())) == NULL)
+  for (http = NULL;;)
   {
-    fprintf(stderr, "ERROR: %s Unable to connect to %s on port %s: %s\n",
-            prefix, argv[1], argv[2],
-           h_errno ? hstrerror(h_errno) : strerror(errno));
-    sleep(interval);
-  }
+   /*
+    * Open a connection to the server...
+    */
 
- /*
-  * Loop forever, asking for available printers and classes...
-  */
+    if (restart_polling || !http)
+    {
+      restart_polling = 0;
+      httpClose(http);
+
+      if ((http = httpConnectEncrypt(argv[1], atoi(argv[2]),
+                                     cupsEncryption())) == NULL)
+      {
+       fprintf(stderr, "ERROR: %s Unable to connect to %s on port %s: %s\n",
+               prefix, argv[1], argv[2],
+               h_errno ? hstrerror(h_errno) : strerror(errno));
+      }
+    }
 
-  for (;;)
-  {
    /*
     * Get the printers and classes...
     */
 
     remain = interval;
 
-    if ((seconds = poll_server(http, sock, port, interval, prefix)) > 0)
+    if (http && (seconds = poll_server(http, sock, port, interval, prefix)) > 0)
       remain -= seconds;
 
    /*
     * Sleep for any remaining time...
     */
 
-    if (remain > 0
+    if (remain > 0 && !restart_polling)
       sleep(remain);
   }
 }
@@ -260,6 +296,7 @@ poll_server(http_t      *http,              /* I - HTTP connection */
   * Do the request and get back a response...
   */
 
+  seconds  = time(NULL);
   response = cupsDoRequest(http, request, "/");
 
   if (cupsLastError() > IPP_OK_CONFLICT)
@@ -285,7 +322,6 @@ poll_server(http_t      *http,              /* I - HTTP connection */
     fprintf(stderr, "DEBUG: %s Found %d printers.\n", prefix, max_count);
 
     count     = 0;
-    seconds   = time(NULL);
     max_count = max_count / interval + 1;
 
    /*
@@ -409,7 +445,7 @@ poll_server(http_t      *http,              /* I - HTTP connection */
        sleep(1);
       }
 
-      if (!attr)
+      if (!attr || restart_polling)
         break;
     }
 
@@ -425,5 +461,22 @@ poll_server(http_t      *http,             /* I - HTTP connection */
 
 
 /*
- * End of "$Id: cups-polld.c 5753 2006-07-18 19:53:24Z mike $".
+ * 'sighup_handler()' - Handle 'hangup' signals to restart polling.
+ */
+
+static void
+sighup_handler(int sig)                        /* I - Signal number */
+{
+  (void)sig;
+
+  restart_polling = 1;
+
+#if !defined(HAVE_SIGSET) && !defined(HAVE_SIGACTION)
+  signal(SIGHUP, sighup_handler);
+#endif /* !HAVE_SIGSET && !HAVE_SIGACTION */
+}
+
+
+/*
+ * End of "$Id: cups-polld.c 5871 2006-08-23 20:55:33Z mike $".
  */