/*
- * "$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;
/*
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);
/*
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...
*/
}
/*
- * 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);
}
}
* Do the request and get back a response...
*/
+ seconds = time(NULL);
response = cupsDoRequest(http, request, "/");
if (cupsLastError() > IPP_OK_CONFLICT)
fprintf(stderr, "DEBUG: %s Found %d printers.\n", prefix, max_count);
count = 0;
- seconds = time(NULL);
max_count = max_count / interval + 1;
/*
sleep(1);
}
- if (!attr)
+ if (!attr || restart_polling)
break;
}
/*
- * 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 $".
*/