X-Git-Url: http://git.ipfire.org/?p=thirdparty%2Fcups.git;a=blobdiff_plain;f=scheduler%2Fcups-polld.c;h=be6bbb5e62f1bb5b365a63f2d70ef91e1335142f;hp=259361d5ba9e178783af8b85ff6a8f3a5bd30f4c;hb=d09495fadd23469999a64b1812ffb880bf4372de;hpb=8ca02f3c0539293422509d03219786fb6d7db48b diff --git a/scheduler/cups-polld.c b/scheduler/cups-polld.c index 259361d5b..be6bbb5e6 100644 --- a/scheduler/cups-polld.c +++ b/scheduler/cups-polld.c @@ -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). * @@ -23,9 +23,11 @@ * * 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. */ /* @@ -38,6 +40,14 @@ #include #include #include +#include + + +/* + * 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 $". */