Index: backend/lpd.c =================================================================== --- backend/lpd.c (revision 10242) +++ backend/lpd.c (working copy) @@ -3,7 +3,7 @@ * * Line Printer Daemon backend for CUPS. * - * Copyright 2007-2011 by Apple Inc. + * Copyright 2007-2012 by Apple Inc. * Copyright 1997-2007 by Easy Software Products, all rights reserved. * * These coded instructions, statements, and computer programs are the @@ -19,7 +19,6 @@ * main() - Send a file to the printer or server. * lpd_command() - Send an LPR command sequence and wait for a reply. * lpd_queue() - Queue a file using the Line Printer Daemon protocol. - * lpd_timeout() - Handle timeout alarms... * lpd_write() - Write a buffer of data to an LPD server. * rresvport_af() - A simple implementation of rresvport_af(). * sigterm_handler() - Handle 'terminate' signals that stop the backend. @@ -87,14 +86,13 @@ * Local functions... */ -static int lpd_command(int lpd_fd, int timeout, char *format, ...); +static int lpd_command(int lpd_fd, char *format, ...); static int lpd_queue(const char *hostname, http_addrlist_t *addrlist, const char *printer, int print_fd, int snmp_fd, int mode, const char *user, const char *title, int copies, int banner, int format, int order, int reserve, int manual_copies, int timeout, int contimeout); -static void lpd_timeout(int sig); static int lpd_write(int lpd_fd, char *buffer, int length); #ifndef HAVE_RRESVPORT_AF static int rresvport_af(int *port, int family); @@ -564,7 +562,6 @@ static int /* O - Status of command */ lpd_command(int fd, /* I - Socket connection to LPD host */ - int timeout, /* I - Seconds to wait for a response */ char *format, /* I - printf()-style format string */ ...) /* I - Additional args as necessary */ { @@ -609,18 +606,12 @@ fputs("DEBUG: Reading command status...\n", stderr); - alarm(timeout); - if (recv(fd, &status, 1, 0) < 1) { - _cupsLangPrintFilter(stderr, "WARNING", - _("Printer did not respond after %d seconds."), - timeout); + _cupsLangPrintFilter(stderr, "WARNING", _("Printer did not respond.")); status = errno; } - alarm(0); - fprintf(stderr, "DEBUG: lpd_command returning %d\n", status); return (status); @@ -666,28 +657,14 @@ size_t nbytes; /* Number of bytes written */ off_t tbytes; /* Total bytes written */ char buffer[32768]; /* Output buffer */ -#if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET) - struct sigaction action; /* Actions for POSIX signals */ -#endif /* HAVE_SIGACTION && !HAVE_SIGSET */ +#ifdef WIN32 + DWORD tv; /* Timeout in milliseconds */ +#else + struct timeval tv; /* Timeout in secs and usecs */ +#endif /* WIN32 */ /* - * Setup an alarm handler for timeouts... - */ - -#ifdef HAVE_SIGSET /* Use System V signals over POSIX to avoid bugs */ - sigset(SIGALRM, lpd_timeout); -#elif defined(HAVE_SIGACTION) - memset(&action, 0, sizeof(action)); - - sigemptyset(&action.sa_mask); - action.sa_handler = lpd_timeout; - sigaction(SIGALRM, &action, NULL); -#else - signal(SIGALRM, lpd_timeout); -#endif /* HAVE_SIGSET */ - - /* * Remember when we started trying to connect to the printer... */ @@ -864,6 +841,23 @@ } } + /* + * Set the timeout... + */ + +#ifdef WIN32 + tv = (DWORD)(timeout * 1000); + + setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, (char *)&tv, sizeof(tv)); + setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, (char *)&tv, sizeof(tv)); +#else + tv.tv_sec = timeout; + tv.tv_usec = 0; + + setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)); + setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv)); +#endif /* WIN32 */ + fputs("STATE: -connecting-to-device\n", stderr); _cupsLangPrintFilter(stderr, "INFO", _("Connected to printer.")); @@ -926,7 +920,7 @@ * literal output... */ - if (lpd_command(fd, timeout, "\002%s\n", + if (lpd_command(fd, "\002%s\n", printer)) /* Receive print job(s) */ { close(fd); @@ -978,7 +972,7 @@ * Send the control file... */ - if (lpd_command(fd, timeout, "\002%d cfA%03.3d%.15s\n", strlen(control), + if (lpd_command(fd, "\002%d cfA%03.3d%.15s\n", strlen(control), (int)getpid() % 1000, localhost)) { close(fd); @@ -997,17 +991,12 @@ } else { - alarm(timeout); - if (read(fd, &status, 1) < 1) { _cupsLangPrintFilter(stderr, "WARNING", - _("Printer did not respond after %d seconds."), - timeout); + _("Printer did not respond.")); status = errno; } - - alarm(0); } if (status != 0) @@ -1033,7 +1022,7 @@ * Send the print file... */ - if (lpd_command(fd, timeout, "\003" CUPS_LLFMT " dfA%03.3d%.15s\n", + if (lpd_command(fd, "\003" CUPS_LLFMT " dfA%03.3d%.15s\n", CUPS_LLCAST filestats.st_size, (int)getpid() % 1000, localhost)) { @@ -1084,17 +1073,12 @@ * want to requeue it over and over... */ - alarm(timeout); - if (recv(fd, &status, 1, 0) < 1) { _cupsLangPrintFilter(stderr, "WARNING", - _("Printer did not respond after %d seconds."), - timeout); + _("Printer did not respond.")); status = 0; } - - alarm(0); } } else @@ -1121,7 +1105,7 @@ * Send control file... */ - if (lpd_command(fd, timeout, "\002%d cfA%03.3d%.15s\n", strlen(control), + if (lpd_command(fd, "\002%d cfA%03.3d%.15s\n", strlen(control), (int)getpid() % 1000, localhost)) { close(fd); @@ -1139,17 +1123,12 @@ } else { - alarm(timeout); - if (read(fd, &status, 1) < 1) { _cupsLangPrintFilter(stderr, "WARNING", - _("Printer did not respond after %d seconds."), - timeout); + _("Printer did not respond.")); status = errno; } - - alarm(0); } if (status != 0) @@ -1193,21 +1172,6 @@ /* - * 'lpd_timeout()' - Handle timeout alarms... - */ - -static void -lpd_timeout(int sig) /* I - Signal number */ -{ - (void)sig; - -#if !defined(HAVE_SIGSET) && !defined(HAVE_SIGACTION) - signal(SIGALRM, lpd_timeout); -#endif /* !HAVE_SIGSET && !HAVE_SIGACTION */ -} - - -/* * 'lpd_write()' - Write a buffer of data to an LPD server. */