2 * "$Id: lpd.c 7740 2008-07-14 23:58:05Z mike $"
4 * Line Printer Daemon backend for the Common UNIX Printing System (CUPS).
6 * Copyright 2007-2010 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>
42 # include <sys/socket.h>
43 # include <netinet/in.h>
44 # include <arpa/inet.h>
48 # include <CoreFoundation/CFNumber.h>
49 # include <CoreFoundation/CFPreferences.h>
50 #endif /* __APPLE__ */
57 static char tmpfilename
[1024] = ""; /* Temporary spool file name */
58 static int abort_job
= 0; /* Non-zero if we get SIGTERM */
65 #define MODE_STANDARD 0 /* Queue a copy */
66 #define MODE_STREAM 1 /* Stream a copy */
70 * The order for control and data files in LPD requests...
73 #define ORDER_CONTROL_DATA 0 /* Control file first, then data */
74 #define ORDER_DATA_CONTROL 1 /* Data file first, then control */
81 #define RESERVE_NONE 0 /* Don't reserve a priviledged port */
82 #define RESERVE_RFC1179 1 /* Reserve port 721-731 */
83 #define RESERVE_ANY 2 /* Reserve port 1-1023 */
90 static int lpd_command(int lpd_fd
, int timeout
, char *format
, ...);
91 static int lpd_queue(const char *hostname
, int port
, const char *printer
,
92 int print_fd
, int mode
, const char *user
,
93 const char *title
, int copies
, int banner
,
94 int format
, int order
, int reserve
,
95 int manual_copies
, int timeout
, int contimeout
);
96 static void lpd_timeout(int sig
);
97 static int lpd_write(int lpd_fd
, char *buffer
, int length
);
98 #ifndef HAVE_RRESVPORT_AF
99 static int rresvport_af(int *port
, int family
);
100 #endif /* !HAVE_RRESVPORT_AF */
101 static void sigterm_handler(int sig
);
105 * 'main()' - Send a file to the printer or server.
109 * printer-uri job-id user title copies options [file]
112 int /* O - Exit status */
113 main(int argc
, /* I - Number of command-line arguments (6 or 7) */
114 char *argv
[]) /* I - Command-line arguments */
116 const char *device_uri
; /* Device URI */
117 char scheme
[255], /* Scheme in URI */
118 hostname
[1024], /* Hostname */
119 username
[255], /* Username info */
120 resource
[1024], /* Resource info (printer name) */
121 *options
, /* Pointer to options */
122 *name
, /* Name of option */
123 *value
, /* Value of option */
124 sep
, /* Separator character */
125 *filename
, /* File to print */
126 title
[256]; /* Title string */
127 int port
; /* Port number */
128 int fd
; /* Print file */
129 int status
; /* Status of LPD job */
130 int mode
; /* Print mode */
131 int banner
; /* Print banner page? */
132 int format
; /* Print format */
133 int order
; /* Order of control/data files */
134 int reserve
; /* Reserve priviledged port? */
135 int sanitize_title
; /* Sanitize title string? */
136 int manual_copies
, /* Do manual copies? */
137 timeout
, /* Timeout */
138 contimeout
, /* Connection timeout */
139 copies
; /* Number of copies */
140 #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
141 struct sigaction action
; /* Actions for POSIX signals */
142 #endif /* HAVE_SIGACTION && !HAVE_SIGSET */
146 * Make sure status messages are not buffered...
149 setbuf(stderr
, NULL
);
152 * Ignore SIGPIPE and catch SIGTERM signals...
156 sigset(SIGPIPE
, SIG_IGN
);
157 sigset(SIGTERM
, sigterm_handler
);
158 #elif defined(HAVE_SIGACTION)
159 memset(&action
, 0, sizeof(action
));
160 action
.sa_handler
= SIG_IGN
;
161 sigaction(SIGPIPE
, &action
, NULL
);
163 sigemptyset(&action
.sa_mask
);
164 sigaddset(&action
.sa_mask
, SIGTERM
);
165 action
.sa_handler
= sigterm_handler
;
166 sigaction(SIGTERM
, &action
, NULL
);
168 signal(SIGPIPE
, SIG_IGN
);
169 signal(SIGTERM
, sigterm_handler
);
170 #endif /* HAVE_SIGSET */
173 * Check command-line...
178 printf("network lpd \"Unknown\" \"%s\"\n",
179 _cupsLangString(cupsLangDefault(), _("LPD/LPR Host or Printer")));
180 return (CUPS_BACKEND_OK
);
182 else if (argc
< 6 || argc
> 7)
184 _cupsLangPrintf(stderr
,
185 _("Usage: %s job-id user title copies options [file]\n"),
187 return (CUPS_BACKEND_FAILED
);
191 * Extract the hostname and printer name from the URI...
194 if ((device_uri
= cupsBackendDeviceURI(argv
)) == NULL
)
195 return (CUPS_BACKEND_FAILED
);
197 httpSeparateURI(HTTP_URI_CODING_ALL
, device_uri
, scheme
, sizeof(scheme
),
198 username
, sizeof(username
), hostname
, sizeof(hostname
), &port
,
199 resource
, sizeof(resource
));
202 port
= 515; /* Default to port 515 */
207 * If no username is in the device URI, then use the print job user...
210 strlcpy(username
, argv
[2], sizeof(username
));
214 * See if there are any options...
217 mode
= MODE_STANDARD
;
220 order
= ORDER_CONTROL_DATA
;
221 reserve
= RESERVE_ANY
;
224 contimeout
= 7 * 24 * 60 * 60;
228 * We want to pass utf-8 characters, not re-map them (3071945)
234 * Get the default timeout from a system preference...
238 CFPropertyListRef pvalue
; /* Preference value */
239 SInt32 toval
; /* Timeout value */
242 pvalue
= CFPreferencesCopyValue(CFSTR("timeout"),
243 CFSTR("com.apple.print.backends"),
244 kCFPreferencesAnyUser
,
245 kCFPreferencesCurrentHost
);
248 if (CFGetTypeID(pvalue
) == CFNumberGetTypeID())
250 CFNumberGetValue(pvalue
, kCFNumberSInt32Type
, &toval
);
251 contimeout
= (int)toval
;
259 #endif /* __APPLE__ */
261 if ((options
= strchr(resource
, '?')) != NULL
)
264 * Yup, terminate the device name string and move to the first
265 * character of the options...
282 while (*options
&& *options
!= '=' && *options
!= '+' && *options
!= '&')
285 if ((sep
= *options
) != '\0')
296 while (*options
&& *options
!= '+' && *options
!= '&')
306 * Process the option...
309 if (!strcasecmp(name
, "banner"))
315 banner
= !value
[0] || !strcasecmp(value
, "on") ||
316 !strcasecmp(value
, "yes") || !strcasecmp(value
, "true");
318 else if (!strcasecmp(name
, "format") && value
[0])
321 * Set output format...
324 if (strchr("cdfglnoprtv", value
[0]))
327 _cupsLangPrintf(stderr
, _("ERROR: Unknown format character \"%c\"\n"),
330 else if (!strcasecmp(name
, "mode") && value
[0])
333 * Set control/data order...
336 if (!strcasecmp(value
, "standard"))
337 mode
= MODE_STANDARD
;
338 else if (!strcasecmp(value
, "stream"))
341 _cupsLangPrintf(stderr
, _("ERROR: Unknown print mode \"%s\"\n"),
344 else if (!strcasecmp(name
, "order") && value
[0])
347 * Set control/data order...
350 if (!strcasecmp(value
, "control,data"))
351 order
= ORDER_CONTROL_DATA
;
352 else if (!strcasecmp(value
, "data,control"))
353 order
= ORDER_DATA_CONTROL
;
355 _cupsLangPrintf(stderr
, _("ERROR: Unknown file order \"%s\"\n"),
358 else if (!strcasecmp(name
, "reserve"))
361 * Set port reservation mode...
364 if (!value
[0] || !strcasecmp(value
, "on") ||
365 !strcasecmp(value
, "yes") || !strcasecmp(value
, "true") ||
366 !strcasecmp(value
, "rfc1179"))
367 reserve
= RESERVE_RFC1179
;
368 else if (!strcasecmp(value
, "any"))
369 reserve
= RESERVE_ANY
;
371 reserve
= RESERVE_NONE
;
373 else if (!strcasecmp(name
, "manual_copies"))
376 * Set manual copies...
379 manual_copies
= !value
[0] || !strcasecmp(value
, "on") ||
380 !strcasecmp(value
, "yes") || !strcasecmp(value
, "true");
382 else if (!strcasecmp(name
, "sanitize_title"))
385 * Set sanitize title...
388 sanitize_title
= !value
[0] || !strcasecmp(value
, "on") ||
389 !strcasecmp(value
, "yes") || !strcasecmp(value
, "true");
391 else if (!strcasecmp(name
, "timeout"))
398 timeout
= atoi(value
);
400 else if (!strcasecmp(name
, "contimeout"))
403 * Set the connection timeout...
407 contimeout
= atoi(value
);
412 if (mode
== MODE_STREAM
)
413 order
= ORDER_CONTROL_DATA
;
416 * If we have 7 arguments, print the file named on the command-line.
417 * Otherwise, copy stdin to a temporary file and print the temporary
421 if (argc
== 6 && mode
== MODE_STANDARD
)
424 * Copy stdin to a temporary file...
427 http_addrlist_t
*addrlist
; /* Address list */
428 int snmp_fd
; /* SNMP socket */
431 fputs("STATE: +connecting-to-device\n", stderr
);
432 fprintf(stderr
, "DEBUG: Looking up \"%s\"...\n", hostname
);
434 if ((addrlist
= httpAddrGetList(hostname
, AF_UNSPEC
, "1")) == NULL
)
436 _cupsLangPrintf(stderr
, _("ERROR: Unable to locate printer \'%s\'\n"),
438 return (CUPS_BACKEND_STOP
);
441 snmp_fd
= _cupsSNMPOpen(addrlist
->addr
.addr
.sa_family
);
443 if ((fd
= cupsTempFd(tmpfilename
, sizeof(tmpfilename
))) < 0)
445 perror("DEBUG: Unable to create temporary file");
446 return (CUPS_BACKEND_FAILED
);
449 _cupsLangPuts(stderr
, _("INFO: Copying print data...\n"));
451 backendRunLoop(-1, fd
, snmp_fd
, &(addrlist
->addr
), 0, 0,
452 backendNetworkSideCB
);
455 _cupsSNMPClose(snmp_fd
);
457 httpAddrFreeList(addrlist
);
462 * Stream from stdin...
471 fd
= open(filename
, O_RDONLY
);
475 _cupsLangPrintf(stderr
, _("ERROR: Unable to open print file %s: %s\n"),
476 filename
, strerror(errno
));
477 return (CUPS_BACKEND_FAILED
);
482 * Sanitize the document title...
485 strlcpy(title
, argv
[3], sizeof(title
));
490 * Sanitize the title string so that we don't cause problems on
496 for (ptr
= title
; *ptr
; ptr
++)
497 if (!isalnum(*ptr
& 255) && !isspace(*ptr
& 255))
509 manual_copies
= atoi(argv
[4]);
515 copies
= atoi(argv
[4]);
518 status
= lpd_queue(hostname
, port
, resource
+ 1, fd
, mode
,
519 username
, title
, copies
,
520 banner
, format
, order
, reserve
, manual_copies
,
521 timeout
, contimeout
);
524 fprintf(stderr
, "PAGE: 1 %d\n", atoi(argv
[4]));
527 status
= lpd_queue(hostname
, port
, resource
+ 1, fd
, mode
,
529 banner
, format
, order
, reserve
, 1,
530 timeout
, contimeout
);
533 * Remove the temporary file if necessary...
543 * Return the queue status...
551 * 'lpd_command()' - Send an LPR command sequence and wait for a reply.
554 static int /* O - Status of command */
555 lpd_command(int fd
, /* I - Socket connection to LPD host */
556 int timeout
, /* I - Seconds to wait for a response */
557 char *format
, /* I - printf()-style format string */
558 ...) /* I - Additional args as necessary */
560 va_list ap
; /* Argument pointer */
561 char buf
[1024]; /* Output buffer */
562 int bytes
; /* Number of bytes to output */
563 char status
; /* Status from command */
567 * Don't try to send commands if the job has been cancelled...
574 * Format the string...
577 va_start(ap
, format
);
578 bytes
= vsnprintf(buf
, sizeof(buf
), format
, ap
);
581 fprintf(stderr
, "DEBUG: lpd_command %2.2x %s", buf
[0], buf
+ 1);
584 * Send the command...
587 fprintf(stderr
, "DEBUG: Sending command string (%d bytes)...\n", bytes
);
589 if (lpd_write(fd
, buf
, bytes
) < bytes
)
591 perror("DEBUG: Unable to send LPD command");
596 * Read back the status from the command and return it...
599 fputs("DEBUG: Reading command status...\n", stderr
);
603 if (recv(fd
, &status
, 1, 0) < 1)
605 _cupsLangPrintf(stderr
,
606 _("WARNING: Remote host did not respond with command "
607 "status byte after %d seconds\n"), timeout
);
613 fprintf(stderr
, "DEBUG: lpd_command returning %d\n", status
);
620 * 'lpd_queue()' - Queue a file using the Line Printer Daemon protocol.
623 static int /* O - Zero on success, non-zero on failure */
624 lpd_queue(const char *hostname
, /* I - Host to connect to */
625 int port
, /* I - Port to connect on */
626 const char *printer
, /* I - Printer/queue name */
627 int print_fd
, /* I - File to print */
628 int mode
, /* I - Print mode */
629 const char *user
, /* I - Requesting user */
630 const char *title
, /* I - Job title */
631 int copies
, /* I - Number of copies */
632 int banner
, /* I - Print LPD banner? */
633 int format
, /* I - Format specifier */
634 int order
, /* I - Order of data/control files */
635 int reserve
, /* I - Reserve ports? */
636 int manual_copies
, /* I - Do copies by hand... */
637 int timeout
, /* I - Timeout... */
638 int contimeout
) /* I - Connection timeout */
640 char localhost
[255]; /* Local host name */
641 int error
; /* Error number */
642 struct stat filestats
; /* File statistics */
643 int lport
; /* LPD connection local port */
644 int fd
; /* LPD socket */
645 char control
[10240], /* LPD control 'file' */
646 *cptr
; /* Pointer into control file string */
647 char status
; /* Status byte from command */
648 char portname
[255]; /* Port name */
649 int delay
; /* Delay for retries... */
650 char addrname
[256]; /* Address name */
651 http_addrlist_t
*addrlist
, /* Address list */
652 *addr
; /* Socket address */
653 int snmp_fd
, /* SNMP socket */
654 have_supplies
; /* Printer supports supply levels? */
655 int copy
; /* Copies written */
656 time_t start_time
; /* Time of first connect */
657 size_t nbytes
; /* Number of bytes written */
658 off_t tbytes
; /* Total bytes written */
659 char buffer
[32768]; /* Output buffer */
660 #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
661 struct sigaction action
; /* Actions for POSIX signals */
662 #endif /* HAVE_SIGACTION && !HAVE_SIGSET */
666 * Setup an alarm handler for timeouts...
669 #ifdef HAVE_SIGSET /* Use System V signals over POSIX to avoid bugs */
670 sigset(SIGALRM
, lpd_timeout
);
671 #elif defined(HAVE_SIGACTION)
672 memset(&action
, 0, sizeof(action
));
674 sigemptyset(&action
.sa_mask
);
675 action
.sa_handler
= lpd_timeout
;
676 sigaction(SIGALRM
, &action
, NULL
);
678 signal(SIGALRM
, lpd_timeout
);
679 #endif /* HAVE_SIGSET */
682 * Find the printer...
685 sprintf(portname
, "%d", port
);
687 fputs("STATE: +connecting-to-device\n", stderr
);
688 fprintf(stderr
, "DEBUG: Looking up \"%s\"...\n", hostname
);
690 if ((addrlist
= httpAddrGetList(hostname
, AF_UNSPEC
, portname
)) == NULL
)
692 _cupsLangPrintf(stderr
, _("ERROR: Unable to locate printer \'%s\'\n"),
694 return (CUPS_BACKEND_STOP
);
698 * Remember when we started trying to connect to the printer...
701 start_time
= time(NULL
);
704 * Loop forever trying to print the file...
710 * First try to reserve a port for this connection...
713 fprintf(stderr
, "DEBUG: Connecting to %s:%d for printer %s\n", hostname
,
715 _cupsLangPuts(stderr
, _("INFO: Connecting to printer...\n"));
717 for (lport
= reserve
== RESERVE_RFC1179
? 732 : 1024, addr
= addrlist
,
722 * Stop if this job has been cancelled...
727 httpAddrFreeList(addrlist
);
729 return (CUPS_BACKEND_FAILED
);
733 * Choose the next priviledged port...
741 if (lport
< 721 && reserve
== RESERVE_RFC1179
)
747 if (geteuid() || !reserve
)
749 if (getuid() || !reserve
)
750 #endif /* HAVE_GETEUID */
753 * Just create a regular socket...
756 if ((fd
= socket(addr
->addr
.addr
.sa_family
, SOCK_STREAM
, 0)) < 0)
758 perror("DEBUG: Unable to create socket");
769 * We're running as root and want to comply with RFC 1179. Reserve a
770 * priviledged lport between 721 and 731...
773 if ((fd
= rresvport_af(&lport
, addr
->addr
.addr
.sa_family
)) < 0)
775 perror("DEBUG: Unable to reserve port");
783 * Connect to the printer or server...
788 httpAddrFreeList(addrlist
);
792 return (CUPS_BACKEND_FAILED
);
795 if (!connect(fd
, &(addr
->addr
.addr
), httpAddrLength(&(addr
->addr
))))
804 if (getenv("CLASS") != NULL
)
807 * If the CLASS environment variable is set, the job was submitted
808 * to a class and not to a specific queue. In this case, we want
809 * to abort immediately so that the job can be requeued on the next
810 * available printer in the class.
813 _cupsLangPuts(stderr
,
814 _("INFO: Unable to contact printer, queuing on next "
815 "printer in class...\n"));
817 httpAddrFreeList(addrlist
);
820 * Sleep 5 seconds to keep the job from requeuing too rapidly...
825 return (CUPS_BACKEND_FAILED
);
828 fprintf(stderr
, "DEBUG: Connection error: %s\n", strerror(error
));
830 if (error
== ECONNREFUSED
|| error
== EHOSTDOWN
||
831 error
== EHOSTUNREACH
)
833 if (contimeout
&& (time(NULL
) - start_time
) > contimeout
)
835 _cupsLangPuts(stderr
, _("ERROR: The printer is not responding.\n"));
836 return (CUPS_BACKEND_FAILED
);
842 _cupsLangPrintf(stderr
,
843 _("WARNING: Network printer \'%s\' may not exist "
844 "or is unavailable at this time.\n"),
849 _cupsLangPrintf(stderr
,
850 _("WARNING: Network printer \'%s\' is "
851 "unreachable at this time.\n"),
857 _cupsLangPrintf(stderr
,
858 _("WARNING: Network printer \'%s\' is busy.\n"),
868 else if (error
== EADDRINUSE
)
871 * Try on another port...
878 _cupsLangPrintf(stderr
, _("ERROR: Network printer \'%s\' is not "
879 "responding.\n"), hostname
);
884 fputs("STATE: -connecting-to-device\n", stderr
);
885 _cupsLangPuts(stderr
, _("INFO: Connected to printer...\n"));
888 if (addr
->addr
.addr
.sa_family
== AF_INET6
)
889 fprintf(stderr
, "DEBUG: Connected to [%s]:%d (IPv6) (local port %d)...\n",
890 httpAddrString(&addr
->addr
, addrname
, sizeof(addrname
)),
891 ntohs(addr
->addr
.ipv6
.sin6_port
), lport
);
893 #endif /* AF_INET6 */
894 if (addr
->addr
.addr
.sa_family
== AF_INET
)
895 fprintf(stderr
, "DEBUG: Connected to %s:%d (IPv4) (local port %d)...\n",
896 httpAddrString(&addr
->addr
, addrname
, sizeof(addrname
)),
897 ntohs(addr
->addr
.ipv4
.sin_port
), lport
);
900 * See if the printer supports SNMP...
903 if ((snmp_fd
= _cupsSNMPOpen(addr
->addr
.addr
.sa_family
)) >= 0)
904 have_supplies
= !backendSNMPSupplies(snmp_fd
, &(addr
->addr
), NULL
, NULL
);
909 * Check for side-channel requests...
912 backendCheckSideChannel(snmp_fd
, &(addr
->addr
));
915 * Next, open the print file and figure out its size...
921 * Use the size from the print file...
924 if (fstat(print_fd
, &filestats
))
926 httpAddrFreeList(addrlist
);
929 perror("DEBUG: unable to stat print file");
930 return (CUPS_BACKEND_FAILED
);
933 filestats
.st_size
*= manual_copies
;
938 * Use a "very large value" for the size so that the printer will
939 * keep printing until we close the connection...
942 #ifdef _LARGEFILE_SOURCE
943 filestats
.st_size
= (size_t)(999999999999.0);
945 filestats
.st_size
= 2147483647;
946 #endif /* _LARGEFILE_SOURCE */
950 * Send a job header to the printer, specifying no banner page and
954 if (lpd_command(fd
, timeout
, "\002%s\n",
955 printer
)) /* Receive print job(s) */
957 httpAddrFreeList(addrlist
);
959 return (CUPS_BACKEND_FAILED
);
962 httpGetHostname(NULL
, localhost
, sizeof(localhost
));
964 snprintf(control
, sizeof(control
),
965 "H%.31s\n" /* RFC 1179, Section 7.2 - host name <= 31 chars */
966 "P%.31s\n" /* RFC 1179, Section 7.2 - user name <= 31 chars */
967 "J%.99s\n", /* RFC 1179, Section 7.2 - job name <= 99 chars */
968 localhost
, user
, title
);
969 cptr
= control
+ strlen(control
);
973 snprintf(cptr
, sizeof(control
) - (cptr
- control
),
974 "C%.31s\n" /* RFC 1179, Section 7.2 - class name <= 31 chars */
977 cptr
+= strlen(cptr
);
982 snprintf(cptr
, sizeof(control
) - (cptr
- control
), "%cdfA%03d%.15s\n",
983 format
, (int)getpid() % 1000, localhost
);
984 cptr
+= strlen(cptr
);
988 snprintf(cptr
, sizeof(control
) - (cptr
- control
),
990 "N%.131s\n", /* RFC 1179, Section 7.2 - sourcefile name <= 131 chars */
991 (int)getpid() % 1000, localhost
, title
);
993 fprintf(stderr
, "DEBUG: Control file is:\n%s", control
);
995 if (order
== ORDER_CONTROL_DATA
)
998 * Check for side-channel requests...
1001 backendCheckSideChannel(snmp_fd
, &(addr
->addr
));
1004 * Send the control file...
1007 if (lpd_command(fd
, timeout
, "\002%d cfA%03.3d%.15s\n", strlen(control
),
1008 (int)getpid() % 1000, localhost
))
1010 httpAddrFreeList(addrlist
);
1013 return (CUPS_BACKEND_FAILED
);
1016 _cupsLangPrintf(stderr
, _("INFO: Sending control file (%u bytes)\n"),
1017 (unsigned)strlen(control
));
1019 if (lpd_write(fd
, control
, strlen(control
) + 1) < (strlen(control
) + 1))
1022 perror("DEBUG: Unable to write control file");
1029 if (read(fd
, &status
, 1) < 1)
1031 _cupsLangPrintf(stderr
,
1032 _("WARNING: Remote host did not respond with control "
1033 "status byte after %d seconds\n"), timeout
);
1041 _cupsLangPrintf(stderr
,
1042 _("ERROR: Remote host did not accept control file "
1045 _cupsLangPuts(stderr
, _("INFO: Control file sent successfully\n"));
1053 * Check for side-channel requests...
1056 backendCheckSideChannel(snmp_fd
, &(addr
->addr
));
1059 * Send the print file...
1062 if (lpd_command(fd
, timeout
, "\003" CUPS_LLFMT
" dfA%03.3d%.15s\n",
1063 CUPS_LLCAST filestats
.st_size
, (int)getpid() % 1000,
1066 httpAddrFreeList(addrlist
);
1069 return (CUPS_BACKEND_FAILED
);
1072 _cupsLangPrintf(stderr
,
1073 #ifdef HAVE_LONG_LONG
1074 _("INFO: Sending data file (%lld bytes)\n"),
1076 _("INFO: Sending data file (%ld bytes)\n"),
1077 #endif /* HAVE_LONG_LONG */
1078 CUPS_LLCAST filestats
.st_size
);
1081 for (copy
= 0; copy
< manual_copies
; copy
++)
1083 lseek(print_fd
, 0, SEEK_SET
);
1085 while ((nbytes
= read(print_fd
, buffer
, sizeof(buffer
))) > 0)
1087 _cupsLangPrintf(stderr
,
1088 _("INFO: Spooling LPR job, %.0f%% complete...\n"),
1089 100.0 * tbytes
/ filestats
.st_size
);
1091 if (lpd_write(fd
, buffer
, nbytes
) < nbytes
)
1093 perror("DEBUG: Unable to send print file to printer");
1101 if (mode
== MODE_STANDARD
)
1103 if (tbytes
< filestats
.st_size
)
1105 else if (lpd_write(fd
, "", 1) < 1)
1107 perror("DEBUG: Unable to send trailing nul to printer");
1113 * Read the status byte from the printer; if we can't read the byte
1114 * back now, we should set status to "errno", however at this point
1115 * we know the printer got the whole file and we don't necessarily
1116 * want to requeue it over and over...
1121 if (recv(fd
, &status
, 1, 0) < 1)
1123 _cupsLangPrintf(stderr
,
1124 _("WARNING: Remote host did not respond with data "
1125 "status byte after %d seconds\n"), timeout
);
1136 _cupsLangPrintf(stderr
,
1137 _("ERROR: Remote host did not accept data file (%d)\n"),
1140 _cupsLangPuts(stderr
, _("INFO: Data file sent successfully\n"));
1143 if (status
== 0 && order
== ORDER_DATA_CONTROL
)
1146 * Check for side-channel requests...
1149 backendCheckSideChannel(snmp_fd
, &(addr
->addr
));
1152 * Send control file...
1155 if (lpd_command(fd
, timeout
, "\002%d cfA%03.3d%.15s\n", strlen(control
),
1156 (int)getpid() % 1000, localhost
))
1158 httpAddrFreeList(addrlist
);
1161 return (CUPS_BACKEND_FAILED
);
1164 _cupsLangPrintf(stderr
, _("INFO: Sending control file (%lu bytes)\n"),
1165 (unsigned long)strlen(control
));
1167 if (lpd_write(fd
, control
, strlen(control
) + 1) < (strlen(control
) + 1))
1170 perror("DEBUG: Unable to write control file");
1176 if (read(fd
, &status
, 1) < 1)
1178 _cupsLangPrintf(stderr
,
1179 _("WARNING: Remote host did not respond with control "
1180 "status byte after %d seconds\n"), timeout
);
1188 _cupsLangPrintf(stderr
,
1189 _("ERROR: Remote host did not accept control file "
1192 _cupsLangPuts(stderr
, _("INFO: Control file sent successfully\n"));
1196 * Collect the final supply levels as needed...
1200 backendSNMPSupplies(snmp_fd
, &(addr
->addr
), NULL
, NULL
);
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... */
1383 * End of "$Id: lpd.c 7740 2008-07-14 23:58:05Z mike $".