4 * Line Printer Daemon backend for the Common UNIX Printing System (CUPS).
6 * Copyright 2007-2008 by Apple Inc.
7 * Copyright 1997-2007 by Easy Software Products, all rights reserved.
9 * These coded instructions, statements, and computer programs are the
10 * property of Apple Inc. and are protected by Federal copyright
11 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
12 * "LICENSE" which should have been included with this file. If this
13 * file is missing or damaged, see the license at "http://www.cups.org/".
15 * This file is subject to the Apple OS-Developed Software exception.
19 * main() - Send a file to the printer or server.
20 * lpd_command() - Send an LPR command sequence and wait for a reply.
21 * lpd_queue() - Queue a file using the Line Printer Daemon protocol.
22 * lpd_timeout() - Handle timeout alarms...
23 * lpd_write() - Write a buffer of data to an LPD server.
24 * rresvport_af() - A simple implementation of rresvport_af().
25 * sigterm_handler() - Handle 'terminate' signals that stop the backend.
29 * Include necessary headers.
32 #include <cups/http-private.h>
33 #include "backend-private.h"
35 #include <sys/types.h>
41 # include <sys/socket.h>
42 # include <netinet/in.h>
43 # include <arpa/inet.h>
47 # include <CoreFoundation/CFNumber.h>
48 # include <CoreFoundation/CFPreferences.h>
49 #endif /* __APPLE__ */
56 static char tmpfilename
[1024] = ""; /* Temporary spool file name */
57 static int abort_job
= 0; /* Non-zero if we get SIGTERM */
64 #define MODE_STANDARD 0 /* Queue a copy */
65 #define MODE_STREAM 1 /* Stream a copy */
69 * The order for control and data files in LPD requests...
72 #define ORDER_CONTROL_DATA 0 /* Control file first, then data */
73 #define ORDER_DATA_CONTROL 1 /* Data file first, then control */
80 #define RESERVE_NONE 0 /* Don't reserve a priviledged port */
81 #define RESERVE_RFC1179 1 /* Reserve port 721-731 */
82 #define RESERVE_ANY 2 /* Reserve port 1-1023 */
89 static int lpd_command(int lpd_fd
, int timeout
, char *format
, ...);
90 static int lpd_queue(const char *hostname
, int port
, const char *printer
,
91 int print_fd
, int mode
, const char *user
,
92 const char *title
, int copies
, int banner
,
93 int format
, int order
, int reserve
,
94 int manual_copies
, int timeout
, int contimeout
);
95 static void lpd_timeout(int sig
);
96 static int lpd_write(int lpd_fd
, char *buffer
, int length
);
97 #ifndef HAVE_RRESVPORT_AF
98 static int rresvport_af(int *port
, int family
);
99 #endif /* !HAVE_RRESVPORT_AF */
100 static void sigterm_handler(int sig
);
104 * 'main()' - Send a file to the printer or server.
108 * printer-uri job-id user title copies options [file]
111 int /* O - Exit status */
112 main(int argc
, /* I - Number of command-line arguments (6 or 7) */
113 char *argv
[]) /* I - Command-line arguments */
115 char method
[255], /* Method in URI */
116 hostname
[1024], /* Hostname */
117 username
[255], /* Username info */
118 resource
[1024], /* Resource info (printer name) */
119 *options
, /* Pointer to options */
120 *name
, /* Name of option */
121 *value
, /* Value of option */
122 sep
, /* Separator character */
123 *filename
, /* File to print */
124 title
[256]; /* Title string */
125 int port
; /* Port number */
126 int fd
; /* Print file */
127 int status
; /* Status of LPD job */
128 int mode
; /* Print mode */
129 int banner
; /* Print banner page? */
130 int format
; /* Print format */
131 int order
; /* Order of control/data files */
132 int reserve
; /* Reserve priviledged port? */
133 int sanitize_title
; /* Sanitize title string? */
134 int manual_copies
, /* Do manual copies? */
135 timeout
, /* Timeout */
136 contimeout
, /* Connection timeout */
137 copies
; /* Number of copies */
138 #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
139 struct sigaction action
; /* Actions for POSIX signals */
140 #endif /* HAVE_SIGACTION && !HAVE_SIGSET */
144 * Make sure status messages are not buffered...
147 setbuf(stderr
, NULL
);
150 * Ignore SIGPIPE and catch SIGTERM signals...
154 sigset(SIGPIPE
, SIG_IGN
);
155 sigset(SIGTERM
, sigterm_handler
);
156 #elif defined(HAVE_SIGACTION)
157 memset(&action
, 0, sizeof(action
));
158 action
.sa_handler
= SIG_IGN
;
159 sigaction(SIGPIPE
, &action
, NULL
);
161 sigemptyset(&action
.sa_mask
);
162 sigaddset(&action
.sa_mask
, SIGTERM
);
163 action
.sa_handler
= sigterm_handler
;
164 sigaction(SIGTERM
, &action
, NULL
);
166 signal(SIGPIPE
, SIG_IGN
);
167 signal(SIGTERM
, sigterm_handler
);
168 #endif /* HAVE_SIGSET */
171 * Check command-line...
176 puts("network lpd \"Unknown\" \"LPD/LPR Host or Printer\"");
177 return (CUPS_BACKEND_OK
);
179 else if (argc
< 6 || argc
> 7)
181 _cupsLangPrintf(stderr
,
182 _("Usage: %s job-id user title copies options [file]\n"),
184 return (CUPS_BACKEND_FAILED
);
188 * Extract the hostname and printer name from the URI...
191 httpSeparateURI(HTTP_URI_CODING_ALL
, cupsBackendDeviceURI(argv
),
192 method
, sizeof(method
), username
, sizeof(username
),
193 hostname
, sizeof(hostname
), &port
,
194 resource
, sizeof(resource
));
197 port
= 515; /* Default to port 515 */
202 * If no username is in the device URI, then use the print job user...
205 strlcpy(username
, argv
[2], sizeof(username
));
209 * See if there are any options...
212 mode
= MODE_STANDARD
;
215 order
= ORDER_CONTROL_DATA
;
216 reserve
= RESERVE_ANY
;
219 contimeout
= 7 * 24 * 60 * 60;
223 * We want to pass utf-8 characters, not re-map them (3071945)
229 * Get the default timeout from a system preference...
233 CFPropertyListRef pvalue
; /* Preference value */
234 SInt32 toval
; /* Timeout value */
237 pvalue
= CFPreferencesCopyValue(CFSTR("timeout"),
238 CFSTR("com.apple.print.backends"),
239 kCFPreferencesAnyUser
,
240 kCFPreferencesCurrentHost
);
243 if (CFGetTypeID(pvalue
) == CFNumberGetTypeID())
245 CFNumberGetValue(pvalue
, kCFNumberSInt32Type
, &toval
);
246 contimeout
= (int)toval
;
254 #endif /* __APPLE__ */
256 if ((options
= strchr(resource
, '?')) != NULL
)
259 * Yup, terminate the device name string and move to the first
260 * character of the options...
277 while (*options
&& *options
!= '=' && *options
!= '+' && *options
!= '&')
280 if ((sep
= *options
) != '\0')
291 while (*options
&& *options
!= '+' && *options
!= '&')
301 * Process the option...
304 if (!strcasecmp(name
, "banner"))
310 banner
= !value
[0] || !strcasecmp(value
, "on") ||
311 !strcasecmp(value
, "yes") || !strcasecmp(value
, "true");
313 else if (!strcasecmp(name
, "format") && value
[0])
316 * Set output format...
319 if (strchr("cdfglnoprtv", value
[0]))
322 _cupsLangPrintf(stderr
, _("ERROR: Unknown format character \"%c\"\n"),
325 else if (!strcasecmp(name
, "mode") && value
[0])
328 * Set control/data order...
331 if (!strcasecmp(value
, "standard"))
332 mode
= MODE_STANDARD
;
333 else if (!strcasecmp(value
, "stream"))
336 _cupsLangPrintf(stderr
, _("ERROR: Unknown print mode \"%s\"\n"),
339 else if (!strcasecmp(name
, "order") && value
[0])
342 * Set control/data order...
345 if (!strcasecmp(value
, "control,data"))
346 order
= ORDER_CONTROL_DATA
;
347 else if (!strcasecmp(value
, "data,control"))
348 order
= ORDER_DATA_CONTROL
;
350 _cupsLangPrintf(stderr
, _("ERROR: Unknown file order \"%s\"\n"),
353 else if (!strcasecmp(name
, "reserve"))
356 * Set port reservation mode...
359 if (!value
[0] || !strcasecmp(value
, "on") ||
360 !strcasecmp(value
, "yes") || !strcasecmp(value
, "true") ||
361 !strcasecmp(value
, "rfc1179"))
362 reserve
= RESERVE_RFC1179
;
363 else if (!strcasecmp(value
, "any"))
364 reserve
= RESERVE_ANY
;
366 reserve
= RESERVE_NONE
;
368 else if (!strcasecmp(name
, "manual_copies"))
371 * Set manual copies...
374 manual_copies
= !value
[0] || !strcasecmp(value
, "on") ||
375 !strcasecmp(value
, "yes") || !strcasecmp(value
, "true");
377 else if (!strcasecmp(name
, "sanitize_title"))
380 * Set sanitize title...
383 sanitize_title
= !value
[0] || !strcasecmp(value
, "on") ||
384 !strcasecmp(value
, "yes") || !strcasecmp(value
, "true");
386 else if (!strcasecmp(name
, "timeout"))
393 timeout
= atoi(value
);
395 else if (!strcasecmp(name
, "contimeout"))
398 * Set the connection timeout...
402 contimeout
= atoi(value
);
407 if (mode
== MODE_STREAM
)
408 order
= ORDER_CONTROL_DATA
;
411 * If we have 7 arguments, print the file named on the command-line.
412 * Otherwise, copy stdin to a temporary file and print the temporary
416 if (argc
== 6 && mode
== MODE_STANDARD
)
419 * Copy stdin to a temporary file...
422 char buffer
[8192]; /* Buffer for copying */
423 int bytes
; /* Number of bytes read */
426 if ((fd
= cupsTempFd(tmpfilename
, sizeof(tmpfilename
))) < 0)
428 _cupsLangPrintError(_("ERROR: Unable to create temporary file"));
429 return (CUPS_BACKEND_FAILED
);
432 while ((bytes
= fread(buffer
, 1, sizeof(buffer
), stdin
)) > 0)
433 if (write(fd
, buffer
, bytes
) < bytes
)
435 _cupsLangPrintError(_("ERROR: Unable to write to temporary file"));
438 return (CUPS_BACKEND_FAILED
);
444 * Stream from stdin...
453 fd
= open(filename
, O_RDONLY
);
457 _cupsLangPrintf(stderr
, _("ERROR: Unable to open print file %s: %s\n"),
458 filename
, strerror(errno
));
459 return (CUPS_BACKEND_FAILED
);
464 * Sanitize the document title...
467 strlcpy(title
, argv
[3], sizeof(title
));
472 * Sanitize the title string so that we don't cause problems on
478 for (ptr
= title
; *ptr
; ptr
++)
479 if (!isalnum(*ptr
& 255) && !isspace(*ptr
& 255))
491 manual_copies
= atoi(argv
[4]);
497 copies
= atoi(argv
[4]);
500 status
= lpd_queue(hostname
, port
, resource
+ 1, fd
, mode
,
501 username
, title
, copies
,
502 banner
, format
, order
, reserve
, manual_copies
,
503 timeout
, contimeout
);
506 fprintf(stderr
, "PAGE: 1 %d\n", atoi(argv
[4]));
509 status
= lpd_queue(hostname
, port
, resource
+ 1, fd
, mode
,
511 banner
, format
, order
, reserve
, 1,
512 timeout
, contimeout
);
515 * Remove the temporary file if necessary...
525 * Return the queue status...
533 * 'lpd_command()' - Send an LPR command sequence and wait for a reply.
536 static int /* O - Status of command */
537 lpd_command(int fd
, /* I - Socket connection to LPD host */
538 int timeout
, /* I - Seconds to wait for a response */
539 char *format
, /* I - printf()-style format string */
540 ...) /* I - Additional args as necessary */
542 va_list ap
; /* Argument pointer */
543 char buf
[1024]; /* Output buffer */
544 int bytes
; /* Number of bytes to output */
545 char status
; /* Status from command */
549 * Don't try to send commands if the job has been cancelled...
556 * Format the string...
559 va_start(ap
, format
);
560 bytes
= vsnprintf(buf
, sizeof(buf
), format
, ap
);
563 fprintf(stderr
, "DEBUG: lpd_command %2.2x %s", buf
[0], buf
+ 1);
566 * Send the command...
569 fprintf(stderr
, "DEBUG: Sending command string (%d bytes)...\n", bytes
);
571 if (lpd_write(fd
, buf
, bytes
) < bytes
)
573 _cupsLangPrintError(_("ERROR: Unable to send LPD command"));
578 * Read back the status from the command and return it...
581 fputs("DEBUG: Reading command status...\n", stderr
);
585 if (recv(fd
, &status
, 1, 0) < 1)
587 _cupsLangPrintf(stderr
,
588 _("WARNING: Remote host did not respond with command "
589 "status byte after %d seconds!\n"), timeout
);
595 fprintf(stderr
, "DEBUG: lpd_command returning %d\n", status
);
602 * 'lpd_queue()' - Queue a file using the Line Printer Daemon protocol.
605 static int /* O - Zero on success, non-zero on failure */
606 lpd_queue(const char *hostname
, /* I - Host to connect to */
607 int port
, /* I - Port to connect on */
608 const char *printer
, /* I - Printer/queue name */
609 int print_fd
, /* I - File to print */
610 int mode
, /* I - Print mode */
611 const char *user
, /* I - Requesting user */
612 const char *title
, /* I - Job title */
613 int copies
, /* I - Number of copies */
614 int banner
, /* I - Print LPD banner? */
615 int format
, /* I - Format specifier */
616 int order
, /* I - Order of data/control files */
617 int reserve
, /* I - Reserve ports? */
618 int manual_copies
, /* I - Do copies by hand... */
619 int timeout
, /* I - Timeout... */
620 int contimeout
) /* I - Connection timeout */
622 char localhost
[255]; /* Local host name */
623 int error
; /* Error number */
624 struct stat filestats
; /* File statistics */
625 int lport
; /* LPD connection local port */
626 int fd
; /* LPD socket */
627 char control
[10240], /* LPD control 'file' */
628 *cptr
; /* Pointer into control file string */
629 char status
; /* Status byte from command */
630 char portname
[255]; /* Port name */
631 int delay
; /* Delay for retries... */
632 char addrname
[256]; /* Address name */
633 http_addrlist_t
*addrlist
, /* Address list */
634 *addr
; /* Socket address */
635 int snmp_fd
, /* SNMP socket */
636 start_count
, /* Page count via SNMP at start */
637 page_count
; /* Page count via SNMP */
638 int copy
; /* Copies written */
639 time_t start_time
; /* Time of first connect */
640 int recoverable
; /* Recoverable error shown? */
641 size_t nbytes
; /* Number of bytes written */
642 off_t tbytes
; /* Total bytes written */
643 char buffer
[32768]; /* Output buffer */
644 #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
645 struct sigaction action
; /* Actions for POSIX signals */
646 #endif /* HAVE_SIGACTION && !HAVE_SIGSET */
650 * Setup an alarm handler for timeouts...
653 #ifdef HAVE_SIGSET /* Use System V signals over POSIX to avoid bugs */
654 sigset(SIGALRM
, lpd_timeout
);
655 #elif defined(HAVE_SIGACTION)
656 memset(&action
, 0, sizeof(action
));
658 sigemptyset(&action
.sa_mask
);
659 action
.sa_handler
= lpd_timeout
;
660 sigaction(SIGALRM
, &action
, NULL
);
662 signal(SIGALRM
, lpd_timeout
);
663 #endif /* HAVE_SIGSET */
666 * Find the printer...
669 sprintf(portname
, "%d", port
);
671 if ((addrlist
= httpAddrGetList(hostname
, AF_UNSPEC
, portname
)) == NULL
)
673 _cupsLangPrintf(stderr
, _("ERROR: Unable to locate printer \'%s\'!\n"),
675 return (CUPS_BACKEND_STOP
);
679 * Remember when we started trying to connect to the printer...
683 start_time
= time(NULL
);
686 * Loop forever trying to print the file...
692 * First try to reserve a port for this connection...
695 fputs("STATE: +connecting-to-device\n", stderr
);
696 _cupsLangPrintf(stderr
,
697 _("INFO: Attempting to connect to host %s for printer %s\n"),
700 for (lport
= reserve
== RESERVE_RFC1179
? 732 : 1024, addr
= addrlist
,
705 * Stop if this job has been cancelled...
710 httpAddrFreeList(addrlist
);
712 return (CUPS_BACKEND_FAILED
);
716 * Choose the next priviledged port...
724 if (lport
< 721 && reserve
== RESERVE_RFC1179
)
730 if (geteuid() || !reserve
)
732 if (getuid() || !reserve
)
733 #endif /* HAVE_GETEUID */
736 * Just create a regular socket...
739 if ((fd
= socket(addr
->addr
.addr
.sa_family
, SOCK_STREAM
, 0)) < 0)
741 _cupsLangPrintError(_("ERROR: Unable to create socket"));
752 * We're running as root and want to comply with RFC 1179. Reserve a
753 * priviledged lport between 721 and 731...
756 if ((fd
= rresvport_af(&lport
, addr
->addr
.addr
.sa_family
)) < 0)
758 _cupsLangPrintError(_("ERROR: Unable to reserve port"));
766 * Connect to the printer or server...
771 httpAddrFreeList(addrlist
);
775 return (CUPS_BACKEND_FAILED
);
778 if (!connect(fd
, &(addr
->addr
.addr
), httpAddrLength(&(addr
->addr
))))
788 if (getenv("CLASS") != NULL
)
791 * If the CLASS environment variable is set, the job was submitted
792 * to a class and not to a specific queue. In this case, we want
793 * to abort immediately so that the job can be requeued on the next
794 * available printer in the class.
797 _cupsLangPuts(stderr
,
798 _("INFO: Unable to contact printer, queuing on next "
799 "printer in class...\n"));
801 httpAddrFreeList(addrlist
);
804 * Sleep 5 seconds to keep the job from requeuing too rapidly...
809 return (CUPS_BACKEND_FAILED
);
812 if (error
== ECONNREFUSED
|| error
== EHOSTDOWN
||
813 error
== EHOSTUNREACH
)
815 if (contimeout
&& (time(NULL
) - start_time
) > contimeout
)
817 _cupsLangPuts(stderr
, _("ERROR: Printer not responding!\n"));
818 return (CUPS_BACKEND_FAILED
);
823 _cupsLangPrintf(stderr
,
824 _("WARNING: recoverable: Network host \'%s\' is busy; "
825 "will retry in %d seconds...\n"), hostname
, delay
);
832 else if (error
== EADDRINUSE
)
835 * Try on another port...
844 fprintf(stderr
, "DEBUG: Connection error: %s\n", strerror(errno
));
845 _cupsLangPuts(stderr
,
846 _("ERROR: recoverable: Unable to connect to printer; "
847 "will retry in 30 seconds...\n"));
855 * If we've shown a recoverable error make sure the printer proxies
856 * have a chance to see the recovered message. Not pretty but
857 * necessary for now...
860 fputs("INFO: recovered: \n", stderr
);
864 fputs("STATE: -connecting-to-device\n", stderr
);
865 _cupsLangPrintf(stderr
, _("INFO: Connected to %s...\n"), hostname
);
868 if (addr
->addr
.addr
.sa_family
== AF_INET6
)
869 fprintf(stderr
, "DEBUG: Connected to [%s]:%d (IPv6) (local port %d)...\n",
870 httpAddrString(&addr
->addr
, addrname
, sizeof(addrname
)),
871 ntohs(addr
->addr
.ipv6
.sin6_port
), lport
);
873 #endif /* AF_INET6 */
874 if (addr
->addr
.addr
.sa_family
== AF_INET
)
875 fprintf(stderr
, "DEBUG: Connected to %s:%d (IPv4) (local port %d)...\n",
876 httpAddrString(&addr
->addr
, addrname
, sizeof(addrname
)),
877 ntohs(addr
->addr
.ipv4
.sin_port
), lport
);
880 * See if the printer supports SNMP...
883 if ((snmp_fd
= _cupsSNMPOpen(addr
->addr
.addr
.sa_family
)) >= 0)
885 if (backendSNMPSupplies(snmp_fd
, &(addr
->addr
), &start_count
, NULL
))
891 _cupsSNMPClose(snmp_fd
);
899 * Check for side-channel requests...
902 backendCheckSideChannel(snmp_fd
, &(addr
->addr
));
905 * Next, open the print file and figure out its size...
911 * Use the size from the print file...
914 if (fstat(print_fd
, &filestats
))
916 httpAddrFreeList(addrlist
);
919 _cupsLangPrintError(_("ERROR: unable to stat print file"));
920 return (CUPS_BACKEND_FAILED
);
923 filestats
.st_size
*= manual_copies
;
928 * Use a "very large value" for the size so that the printer will
929 * keep printing until we close the connection...
932 #ifdef _LARGEFILE_SOURCE
933 filestats
.st_size
= (size_t)(999999999999.0);
935 filestats
.st_size
= 2147483647;
936 #endif /* _LARGEFILE_SOURCE */
940 * Send a job header to the printer, specifying no banner page and
944 if (lpd_command(fd
, timeout
, "\002%s\n",
945 printer
)) /* Receive print job(s) */
947 httpAddrFreeList(addrlist
);
949 return (CUPS_BACKEND_FAILED
);
952 httpGetHostname(NULL
, localhost
, sizeof(localhost
));
954 snprintf(control
, sizeof(control
),
955 "H%.31s\n" /* RFC 1179, Section 7.2 - host name <= 31 chars */
956 "P%.31s\n" /* RFC 1179, Section 7.2 - user name <= 31 chars */
957 "J%.99s\n", /* RFC 1179, Section 7.2 - job name <= 99 chars */
958 localhost
, user
, title
);
959 cptr
= control
+ strlen(control
);
963 snprintf(cptr
, sizeof(control
) - (cptr
- control
),
964 "C%.31s\n" /* RFC 1179, Section 7.2 - class name <= 31 chars */
967 cptr
+= strlen(cptr
);
972 snprintf(cptr
, sizeof(control
) - (cptr
- control
), "%cdfA%03d%.15s\n",
973 format
, (int)getpid() % 1000, localhost
);
974 cptr
+= strlen(cptr
);
978 snprintf(cptr
, sizeof(control
) - (cptr
- control
),
980 "N%.131s\n", /* RFC 1179, Section 7.2 - sourcefile name <= 131 chars */
981 (int)getpid() % 1000, localhost
, title
);
983 fprintf(stderr
, "DEBUG: Control file is:\n%s", control
);
985 if (order
== ORDER_CONTROL_DATA
)
988 * Check for side-channel requests...
991 backendCheckSideChannel(snmp_fd
, &(addr
->addr
));
994 * Send the control file...
997 if (lpd_command(fd
, timeout
, "\002%d cfA%03.3d%.15s\n", strlen(control
),
998 (int)getpid() % 1000, localhost
))
1000 httpAddrFreeList(addrlist
);
1003 return (CUPS_BACKEND_FAILED
);
1006 _cupsLangPrintf(stderr
, _("INFO: Sending control file (%u bytes)\n"),
1007 (unsigned)strlen(control
));
1009 if (lpd_write(fd
, control
, strlen(control
) + 1) < (strlen(control
) + 1))
1012 _cupsLangPrintError(_("ERROR: Unable to write control file"));
1018 if (read(fd
, &status
, 1) < 1)
1020 _cupsLangPrintf(stderr
,
1021 _("WARNING: Remote host did not respond with control "
1022 "status byte after %d seconds!\n"), timeout
);
1030 _cupsLangPrintf(stderr
,
1031 _("ERROR: Remote host did not accept control file "
1034 _cupsLangPuts(stderr
, _("INFO: Control file sent successfully\n"));
1042 * Check for side-channel requests...
1045 backendCheckSideChannel(snmp_fd
, &(addr
->addr
));
1048 * Send the print file...
1051 if (lpd_command(fd
, timeout
, "\003" CUPS_LLFMT
" dfA%03.3d%.15s\n",
1052 CUPS_LLCAST filestats
.st_size
, (int)getpid() % 1000,
1055 httpAddrFreeList(addrlist
);
1058 return (CUPS_BACKEND_FAILED
);
1061 _cupsLangPrintf(stderr
,
1062 #ifdef HAVE_LONG_LONG
1063 _("INFO: Sending data file (%lld bytes)\n"),
1065 _("INFO: Sending data file (%ld bytes)\n"),
1066 #endif /* HAVE_LONG_LONG */
1067 CUPS_LLCAST filestats
.st_size
);
1070 for (copy
= 0; copy
< manual_copies
; copy
++)
1072 lseek(print_fd
, 0, SEEK_SET
);
1074 while ((nbytes
= read(print_fd
, buffer
, sizeof(buffer
))) > 0)
1076 _cupsLangPrintf(stderr
,
1077 _("INFO: Spooling LPR job, %.0f%% complete...\n"),
1078 100.0 * tbytes
/ filestats
.st_size
);
1080 if (lpd_write(fd
, buffer
, nbytes
) < nbytes
)
1082 _cupsLangPrintError(_("ERROR: Unable to send print file to printer"));
1090 if (mode
== MODE_STANDARD
)
1092 if (tbytes
< filestats
.st_size
)
1094 else if (lpd_write(fd
, "", 1) < 1)
1096 _cupsLangPrintError(_("ERROR: Unable to send trailing nul to printer"));
1102 * Read the status byte from the printer; if we can't read the byte
1103 * back now, we should set status to "errno", however at this point
1104 * we know the printer got the whole file and we don't necessarily
1105 * want to requeue it over and over...
1110 if (recv(fd
, &status
, 1, 0) < 1)
1112 _cupsLangPrintf(stderr
,
1113 _("WARNING: Remote host did not respond with data "
1114 "status byte after %d seconds!\n"), timeout
);
1125 _cupsLangPrintf(stderr
,
1126 _("ERROR: Remote host did not accept data file (%d)\n"),
1129 _cupsLangPuts(stderr
, _("INFO: Data file sent successfully\n"));
1132 if (status
== 0 && order
== ORDER_DATA_CONTROL
)
1135 * Check for side-channel requests...
1138 backendCheckSideChannel(snmp_fd
, &(addr
->addr
));
1141 * Send control file...
1144 if (lpd_command(fd
, timeout
, "\002%d cfA%03.3d%.15s\n", strlen(control
),
1145 (int)getpid() % 1000, localhost
))
1147 httpAddrFreeList(addrlist
);
1150 return (CUPS_BACKEND_FAILED
);
1153 _cupsLangPrintf(stderr
, _("INFO: Sending control file (%lu bytes)\n"),
1154 (unsigned long)strlen(control
));
1156 if (lpd_write(fd
, control
, strlen(control
) + 1) < (strlen(control
) + 1))
1159 _cupsLangPrintError(_("ERROR: Unable to write control file"));
1165 if (read(fd
, &status
, 1) < 1)
1167 _cupsLangPrintf(stderr
,
1168 _("WARNING: Remote host did not respond with control "
1169 "status byte after %d seconds!\n"), timeout
);
1177 _cupsLangPrintf(stderr
,
1178 _("ERROR: Remote host did not accept control file "
1181 _cupsLangPuts(stderr
, _("INFO: Control file sent successfully\n"));
1185 * Collect the final page count as needed...
1190 int printer_state
; /* State of printer */
1193 while (!backendSNMPSupplies(snmp_fd
, &(addr
->addr
), &page_count
,
1195 printer_state
!= CUPS_TC_idle
)
1198 if (page_count
> start_count
)
1199 fprintf(stderr
, "PAGE: total %d\n", page_count
- start_count
);
1203 * Close the socket connection and input file...
1210 httpAddrFreeList(addrlist
);
1212 return (CUPS_BACKEND_OK
);
1216 * Waiting for a retry...
1222 httpAddrFreeList(addrlist
);
1225 * If we get here, then the job has been cancelled...
1228 return (CUPS_BACKEND_FAILED
);
1233 * 'lpd_timeout()' - Handle timeout alarms...
1237 lpd_timeout(int sig
) /* I - Signal number */
1241 #if !defined(HAVE_SIGSET) && !defined(HAVE_SIGACTION)
1242 signal(SIGALRM
, lpd_timeout
);
1243 #endif /* !HAVE_SIGSET && !HAVE_SIGACTION */
1248 * 'lpd_write()' - Write a buffer of data to an LPD server.
1251 static int /* O - Number of bytes written or -1 on error */
1252 lpd_write(int lpd_fd
, /* I - LPD socket */
1253 char *buffer
, /* I - Buffer to write */
1254 int length
) /* I - Number of bytes to write */
1256 int bytes
, /* Number of bytes written */
1257 total
; /* Total number of bytes written */
1264 while ((bytes
= send(lpd_fd
, buffer
, length
- total
, 0)) >= 0)
1269 if (total
== length
)
1280 #ifndef HAVE_RRESVPORT_AF
1282 * 'rresvport_af()' - A simple implementation of rresvport_af().
1285 static int /* O - Socket or -1 on error */
1286 rresvport_af(int *port
, /* IO - Port number to bind to */
1287 int family
) /* I - Address family */
1289 http_addr_t addr
; /* Socket address */
1290 int fd
; /* Socket file descriptor */
1294 * Try to create an IPv4 socket...
1297 if ((fd
= socket(family
, SOCK_STREAM
, 0)) < 0)
1301 * Initialize the address buffer...
1304 memset(&addr
, 0, sizeof(addr
));
1305 addr
.addr
.sa_family
= family
;
1308 * Try to bind the socket to a reserved port...
1314 * Set the port number...
1318 if (family
== AF_INET6
)
1319 addr
.ipv6
.sin6_port
= htons(*port
);
1321 # endif /* AF_INET6 */
1322 addr
.ipv4
.sin_port
= htons(*port
);
1325 * Try binding the port to the socket; return if all is OK...
1328 if (!bind(fd
, (struct sockaddr
*)&addr
, sizeof(addr
)))
1332 * Stop if we have any error other than "address already in use"...
1335 if (errno
!= EADDRINUSE
)
1347 * Try the next port...
1354 * Wasn't able to bind to a reserved port, so close the socket and return
1366 #endif /* !HAVE_RRESVPORT_AF */
1370 * 'sigterm_handler()' - Handle 'terminate' signals that stop the backend.
1374 sigterm_handler(int sig
) /* I - Signal */
1376 (void)sig
; /* remove compiler warnings... */