4 * Line Printer Daemon backend for the Common UNIX Printing System (CUPS).
6 * Copyright 2007 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/backend.h>
33 #include <cups/http-private.h>
34 #include <cups/cups.h>
35 #include <cups/i18n.h>
40 #include <cups/string.h>
42 #include <sys/types.h>
49 # include <sys/socket.h>
50 # include <netinet/in.h>
51 # include <arpa/inet.h>
55 # include <CoreFoundation/CFNumber.h>
56 # include <CoreFoundation/CFPreferences.h>
57 #endif /* __APPLE__ */
64 static char tmpfilename
[1024] = ""; /* Temporary spool file name */
65 static int abort_job
= 0; /* Non-zero if we get SIGTERM */
72 #define MODE_STANDARD 0 /* Queue a copy */
73 #define MODE_STREAM 1 /* Stream a copy */
77 * The order for control and data files in LPD requests...
80 #define ORDER_CONTROL_DATA 0 /* Control file first, then data */
81 #define ORDER_DATA_CONTROL 1 /* Data file first, then control */
88 #define RESERVE_NONE 0 /* Don't reserve a priviledged port */
89 #define RESERVE_RFC1179 1 /* Reserve port 721-731 */
90 #define RESERVE_ANY 2 /* Reserve port 1-1023 */
97 static int lpd_command(int lpd_fd
, int timeout
, char *format
, ...);
98 static int lpd_queue(const char *hostname
, int port
, const char *printer
,
99 int print_fd
, int mode
, const char *user
,
100 const char *title
, int copies
, int banner
,
101 int format
, int order
, int reserve
,
102 int manual_copies
, int timeout
, int contimeout
);
103 static void lpd_timeout(int sig
);
104 static int lpd_write(int lpd_fd
, char *buffer
, int length
);
105 #ifndef HAVE_RRESVPORT_AF
106 static int rresvport_af(int *port
, int family
);
107 #endif /* !HAVE_RRESVPORT_AF */
108 static void sigterm_handler(int sig
);
112 * 'main()' - Send a file to the printer or server.
116 * printer-uri job-id user title copies options [file]
119 int /* O - Exit status */
120 main(int argc
, /* I - Number of command-line arguments (6 or 7) */
121 char *argv
[]) /* I - Command-line arguments */
123 char method
[255], /* Method in URI */
124 hostname
[1024], /* Hostname */
125 username
[255], /* Username info */
126 resource
[1024], /* Resource info (printer name) */
127 *options
, /* Pointer to options */
128 *name
, /* Name of option */
129 *value
, /* Value of option */
130 sep
, /* Separator character */
131 *filename
, /* File to print */
132 title
[256]; /* Title string */
133 int port
; /* Port number */
134 int fd
; /* Print file */
135 int status
; /* Status of LPD job */
136 int mode
; /* Print mode */
137 int banner
; /* Print banner page? */
138 int format
; /* Print format */
139 int order
; /* Order of control/data files */
140 int reserve
; /* Reserve priviledged port? */
141 int sanitize_title
; /* Sanitize title string? */
142 int manual_copies
, /* Do manual copies? */
143 timeout
, /* Timeout */
144 contimeout
, /* Connection timeout */
145 copies
; /* Number of copies */
146 #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
147 struct sigaction action
; /* Actions for POSIX signals */
148 #endif /* HAVE_SIGACTION && !HAVE_SIGSET */
152 * Make sure status messages are not buffered...
155 setbuf(stderr
, NULL
);
158 * Ignore SIGPIPE and catch SIGTERM signals...
162 sigset(SIGPIPE
, SIG_IGN
);
163 sigset(SIGTERM
, sigterm_handler
);
164 #elif defined(HAVE_SIGACTION)
165 memset(&action
, 0, sizeof(action
));
166 action
.sa_handler
= SIG_IGN
;
167 sigaction(SIGPIPE
, &action
, NULL
);
169 sigemptyset(&action
.sa_mask
);
170 sigaddset(&action
.sa_mask
, SIGTERM
);
171 action
.sa_handler
= sigterm_handler
;
172 sigaction(SIGTERM
, &action
, NULL
);
174 signal(SIGPIPE
, SIG_IGN
);
175 signal(SIGTERM
, sigterm_handler
);
176 #endif /* HAVE_SIGSET */
179 * Check command-line...
184 puts("network lpd \"Unknown\" \"LPD/LPR Host or Printer\"");
185 return (CUPS_BACKEND_OK
);
187 else if (argc
< 6 || argc
> 7)
189 _cupsLangPrintf(stderr
,
190 _("Usage: %s job-id user title copies options [file]\n"),
192 return (CUPS_BACKEND_FAILED
);
196 * Extract the hostname and printer name from the URI...
199 httpSeparateURI(HTTP_URI_CODING_ALL
, cupsBackendDeviceURI(argv
),
200 method
, sizeof(method
), username
, sizeof(username
),
201 hostname
, sizeof(hostname
), &port
,
202 resource
, sizeof(resource
));
205 port
= 515; /* Default to port 515 */
210 * If no username is in the device URI, then use the print job user...
213 strlcpy(username
, argv
[2], sizeof(username
));
217 * See if there are any options...
220 mode
= MODE_STANDARD
;
223 order
= ORDER_CONTROL_DATA
;
224 reserve
= RESERVE_ANY
;
227 contimeout
= 7 * 24 * 60 * 60;
231 * We want to pass utf-8 characters, not re-map them (3071945)
237 * Get the default timeout from a system preference...
241 CFPropertyListRef pvalue
; /* Preference value */
242 SInt32 toval
; /* Timeout value */
245 pvalue
= CFPreferencesCopyValue(CFSTR("timeout"),
246 CFSTR("com.apple.print.backends"),
247 kCFPreferencesAnyUser
,
248 kCFPreferencesCurrentHost
);
251 if (CFGetTypeID(pvalue
) == CFNumberGetTypeID())
253 CFNumberGetValue(pvalue
, kCFNumberSInt32Type
, &toval
);
254 contimeout
= (int)toval
;
262 #endif /* __APPLE__ */
264 if ((options
= strchr(resource
, '?')) != NULL
)
267 * Yup, terminate the device name string and move to the first
268 * character of the options...
285 while (*options
&& *options
!= '=' && *options
!= '+' && *options
!= '&')
288 if ((sep
= *options
) != '\0')
299 while (*options
&& *options
!= '+' && *options
!= '&')
309 * Process the option...
312 if (!strcasecmp(name
, "banner"))
318 banner
= !value
[0] || !strcasecmp(value
, "on") ||
319 !strcasecmp(value
, "yes") || !strcasecmp(value
, "true");
321 else if (!strcasecmp(name
, "format") && value
[0])
324 * Set output format...
327 if (strchr("cdfglnoprtv", value
[0]))
330 _cupsLangPrintf(stderr
, _("ERROR: Unknown format character \"%c\"\n"),
333 else if (!strcasecmp(name
, "mode") && value
[0])
336 * Set control/data order...
339 if (!strcasecmp(value
, "standard"))
340 mode
= MODE_STANDARD
;
341 else if (!strcasecmp(value
, "stream"))
344 _cupsLangPrintf(stderr
, _("ERROR: Unknown print mode \"%s\"\n"),
347 else if (!strcasecmp(name
, "order") && value
[0])
350 * Set control/data order...
353 if (!strcasecmp(value
, "control,data"))
354 order
= ORDER_CONTROL_DATA
;
355 else if (!strcasecmp(value
, "data,control"))
356 order
= ORDER_DATA_CONTROL
;
358 _cupsLangPrintf(stderr
, _("ERROR: Unknown file order \"%s\"\n"),
361 else if (!strcasecmp(name
, "reserve"))
364 * Set port reservation mode...
367 if (!value
[0] || !strcasecmp(value
, "on") ||
368 !strcasecmp(value
, "yes") || !strcasecmp(value
, "true") ||
369 !strcasecmp(value
, "rfc1179"))
370 reserve
= RESERVE_RFC1179
;
371 else if (!strcasecmp(value
, "any"))
372 reserve
= RESERVE_ANY
;
374 reserve
= RESERVE_NONE
;
376 else if (!strcasecmp(name
, "manual_copies"))
379 * Set manual copies...
382 manual_copies
= !value
[0] || !strcasecmp(value
, "on") ||
383 !strcasecmp(value
, "yes") || !strcasecmp(value
, "true");
385 else if (!strcasecmp(name
, "sanitize_title"))
388 * Set sanitize title...
391 sanitize_title
= !value
[0] || !strcasecmp(value
, "on") ||
392 !strcasecmp(value
, "yes") || !strcasecmp(value
, "true");
394 else if (!strcasecmp(name
, "timeout"))
401 timeout
= atoi(value
);
403 else if (!strcasecmp(name
, "contimeout"))
406 * Set the connection timeout...
410 contimeout
= atoi(value
);
415 if (mode
== MODE_STREAM
)
416 order
= ORDER_CONTROL_DATA
;
419 * If we have 7 arguments, print the file named on the command-line.
420 * Otherwise, copy stdin to a temporary file and print the temporary
424 if (argc
== 6 && mode
== MODE_STANDARD
)
427 * Copy stdin to a temporary file...
430 char buffer
[8192]; /* Buffer for copying */
431 int bytes
; /* Number of bytes read */
434 if ((fd
= cupsTempFd(tmpfilename
, sizeof(tmpfilename
))) < 0)
436 perror("ERROR: unable to create temporary file");
437 return (CUPS_BACKEND_FAILED
);
440 while ((bytes
= fread(buffer
, 1, sizeof(buffer
), stdin
)) > 0)
441 if (write(fd
, buffer
, bytes
) < bytes
)
443 perror("ERROR: unable to write to temporary file");
446 return (CUPS_BACKEND_FAILED
);
449 filename
= tmpfilename
;
454 * Stream from stdin...
463 fd
= open(filename
, O_RDONLY
);
467 _cupsLangPrintf(stderr
, _("ERROR: Unable to open print file %s: %s\n"),
468 filename
, strerror(errno
));
469 return (CUPS_BACKEND_FAILED
);
474 * Sanitize the document title...
477 strlcpy(title
, argv
[3], sizeof(title
));
482 * Sanitize the title string so that we don't cause problems on
488 for (ptr
= title
; *ptr
; ptr
++)
489 if (!isalnum(*ptr
& 255) && !isspace(*ptr
& 255))
501 manual_copies
= atoi(argv
[4]);
507 copies
= atoi(argv
[4]);
510 status
= lpd_queue(hostname
, port
, resource
+ 1, fd
, mode
,
511 username
, title
, copies
,
512 banner
, format
, order
, reserve
, manual_copies
,
513 timeout
, contimeout
);
516 fprintf(stderr
, "PAGE: 1 %d\n", atoi(argv
[4]));
519 status
= lpd_queue(hostname
, port
, resource
+ 1, fd
, mode
,
521 banner
, format
, order
, reserve
, 1,
522 timeout
, contimeout
);
525 * Remove the temporary file if necessary...
535 * Return the queue status...
543 * 'lpd_command()' - Send an LPR command sequence and wait for a reply.
546 static int /* O - Status of command */
547 lpd_command(int fd
, /* I - Socket connection to LPD host */
548 int timeout
, /* I - Seconds to wait for a response */
549 char *format
, /* I - printf()-style format string */
550 ...) /* I - Additional args as necessary */
552 va_list ap
; /* Argument pointer */
553 char buf
[1024]; /* Output buffer */
554 int bytes
; /* Number of bytes to output */
555 char status
; /* Status from command */
559 * Don't try to send commands if the job has been cancelled...
566 * Format the string...
569 va_start(ap
, format
);
570 bytes
= vsnprintf(buf
, sizeof(buf
), format
, ap
);
573 fprintf(stderr
, "DEBUG: lpd_command %2.2x %s", buf
[0], buf
+ 1);
576 * Send the command...
579 fprintf(stderr
, "DEBUG: Sending command string (%d bytes)...\n", bytes
);
581 if (lpd_write(fd
, buf
, bytes
) < bytes
)
583 perror("ERROR: Unable to send LPD command");
588 * Read back the status from the command and return it...
591 fputs("DEBUG: Reading command status...\n", stderr
);
595 if (recv(fd
, &status
, 1, 0) < 1)
597 _cupsLangPrintf(stderr
,
598 _("WARNING: Remote host did not respond with command "
599 "status byte after %d seconds!\n"), timeout
);
605 fprintf(stderr
, "DEBUG: lpd_command returning %d\n", status
);
612 * 'lpd_queue()' - Queue a file using the Line Printer Daemon protocol.
615 static int /* O - Zero on success, non-zero on failure */
616 lpd_queue(const char *hostname
, /* I - Host to connect to */
617 int port
, /* I - Port to connect on */
618 const char *printer
, /* I - Printer/queue name */
619 int print_fd
, /* I - File to print */
620 int mode
, /* I - Print mode */
621 const char *user
, /* I - Requesting user */
622 const char *title
, /* I - Job title */
623 int copies
, /* I - Number of copies */
624 int banner
, /* I - Print LPD banner? */
625 int format
, /* I - Format specifier */
626 int order
, /* I - Order of data/control files */
627 int reserve
, /* I - Reserve ports? */
628 int manual_copies
, /* I - Do copies by hand... */
629 int timeout
, /* I - Timeout... */
630 int contimeout
) /* I - Connection timeout */
632 char localhost
[255]; /* Local host name */
633 int error
; /* Error number */
634 struct stat filestats
; /* File statistics */
635 int lport
; /* LPD connection local port */
636 int fd
; /* LPD socket */
637 char control
[10240], /* LPD control 'file' */
638 *cptr
; /* Pointer into control file string */
639 char status
; /* Status byte from command */
640 char portname
[255]; /* Port name */
641 int delay
; /* Delay for retries... */
642 char addrname
[256]; /* Address name */
643 http_addrlist_t
*addrlist
, /* Address list */
644 *addr
; /* Socket address */
645 int copy
; /* Copies written */
646 time_t start_time
; /* Time of first connect */
647 int recoverable
; /* Recoverable error shown? */
648 size_t nbytes
; /* Number of bytes written */
649 off_t tbytes
; /* Total bytes written */
650 char buffer
[32768]; /* Output buffer */
651 #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
652 struct sigaction action
; /* Actions for POSIX signals */
653 #endif /* HAVE_SIGACTION && !HAVE_SIGSET */
657 * Setup an alarm handler for timeouts...
660 #ifdef HAVE_SIGSET /* Use System V signals over POSIX to avoid bugs */
661 sigset(SIGALRM
, lpd_timeout
);
662 #elif defined(HAVE_SIGACTION)
663 memset(&action
, 0, sizeof(action
));
665 sigemptyset(&action
.sa_mask
);
666 action
.sa_handler
= lpd_timeout
;
667 sigaction(SIGALRM
, &action
, NULL
);
669 signal(SIGALRM
, lpd_timeout
);
670 #endif /* HAVE_SIGSET */
673 * Find the printer...
676 sprintf(portname
, "%d", port
);
678 if ((addrlist
= httpAddrGetList(hostname
, AF_UNSPEC
, portname
)) == NULL
)
680 _cupsLangPrintf(stderr
, _("ERROR: Unable to locate printer \'%s\'!\n"),
682 return (CUPS_BACKEND_STOP
);
686 * Remember when we started trying to connect to the printer...
690 start_time
= time(NULL
);
693 * Loop forever trying to print the file...
699 * First try to reserve a port for this connection...
702 fputs("STATE: +connecting-to-device\n", stderr
);
703 _cupsLangPrintf(stderr
,
704 _("INFO: Attempting to connect to host %s for printer %s\n"),
707 for (lport
= reserve
== RESERVE_RFC1179
? 732 : 1024, addr
= addrlist
,
712 * Stop if this job has been cancelled...
717 httpAddrFreeList(addrlist
);
719 return (CUPS_BACKEND_FAILED
);
723 * Choose the next priviledged port...
731 if (lport
< 721 && reserve
== RESERVE_RFC1179
)
737 if (geteuid() || !reserve
)
739 if (getuid() || !reserve
)
740 #endif /* HAVE_GETEUID */
743 * Just create a regular socket...
746 if ((fd
= socket(addr
->addr
.addr
.sa_family
, SOCK_STREAM
, 0)) < 0)
748 perror("ERROR: Unable to create socket");
759 * We're running as root and want to comply with RFC 1179. Reserve a
760 * priviledged lport between 721 and 731...
763 if ((fd
= rresvport_af(&lport
, addr
->addr
.addr
.sa_family
)) < 0)
765 perror("ERROR: Unable to reserve port");
773 * Connect to the printer or server...
778 httpAddrFreeList(addrlist
);
782 return (CUPS_BACKEND_FAILED
);
785 if (!connect(fd
, &(addr
->addr
.addr
), httpAddrLength(&(addr
->addr
))))
795 if (getenv("CLASS") != NULL
)
798 * If the CLASS environment variable is set, the job was submitted
799 * to a class and not to a specific queue. In this case, we want
800 * to abort immediately so that the job can be requeued on the next
801 * available printer in the class.
804 _cupsLangPuts(stderr
,
805 _("INFO: Unable to contact printer, queuing on next "
806 "printer in class...\n"));
808 httpAddrFreeList(addrlist
);
811 * Sleep 5 seconds to keep the job from requeuing too rapidly...
816 return (CUPS_BACKEND_FAILED
);
819 if (error
== ECONNREFUSED
|| error
== EHOSTDOWN
||
820 error
== EHOSTUNREACH
)
822 if (contimeout
&& (time(NULL
) - start_time
) > contimeout
)
824 _cupsLangPuts(stderr
, _("ERROR: Printer not responding!\n"));
825 return (CUPS_BACKEND_FAILED
);
830 _cupsLangPrintf(stderr
,
831 _("WARNING: recoverable: Network host \'%s\' is busy; "
832 "will retry in %d seconds...\n"), hostname
, delay
);
839 else if (error
== EADDRINUSE
)
842 * Try on another port...
851 fprintf(stderr
, "DEBUG: Connection error: %s\n", strerror(errno
));
852 _cupsLangPuts(stderr
,
853 _("ERROR: recoverable: Unable to connect to printer; "
854 "will retry in 30 seconds...\n"));
862 * If we've shown a recoverable error make sure the printer proxies
863 * have a chance to see the recovered message. Not pretty but
864 * necessary for now...
867 fputs("INFO: recovered: \n", stderr
);
871 fputs("STATE: -connecting-to-device\n", stderr
);
872 _cupsLangPrintf(stderr
, _("INFO: Connected to %s...\n"), hostname
);
875 if (addr
->addr
.addr
.sa_family
== AF_INET6
)
876 fprintf(stderr
, "DEBUG: Connected to [%s]:%d (IPv6) (local port %d)...\n",
877 httpAddrString(&addr
->addr
, addrname
, sizeof(addrname
)),
878 ntohs(addr
->addr
.ipv6
.sin6_port
), lport
);
880 #endif /* AF_INET6 */
881 if (addr
->addr
.addr
.sa_family
== AF_INET
)
882 fprintf(stderr
, "DEBUG: Connected to %s:%d (IPv4) (local port %d)...\n",
883 httpAddrString(&addr
->addr
, addrname
, sizeof(addrname
)),
884 ntohs(addr
->addr
.ipv4
.sin_port
), lport
);
887 * Next, open the print file and figure out its size...
893 * Use the size from the print file...
896 if (fstat(print_fd
, &filestats
))
898 httpAddrFreeList(addrlist
);
901 perror("ERROR: unable to stat print file");
902 return (CUPS_BACKEND_FAILED
);
905 filestats
.st_size
*= manual_copies
;
910 * Use a "very large value" for the size so that the printer will
911 * keep printing until we close the connection...
914 #ifdef _LARGEFILE_SOURCE
915 filestats
.st_size
= (size_t)(999999999999.0);
917 filestats
.st_size
= 2147483647;
918 #endif /* _LARGEFILE_SOURCE */
922 * Send a job header to the printer, specifying no banner page and
926 if (lpd_command(fd
, timeout
, "\002%s\n",
927 printer
)) /* Receive print job(s) */
929 httpAddrFreeList(addrlist
);
931 return (CUPS_BACKEND_FAILED
);
934 httpGetHostname(NULL
, localhost
, sizeof(localhost
));
936 snprintf(control
, sizeof(control
),
937 "H%.31s\n" /* RFC 1179, Section 7.2 - host name <= 31 chars */
938 "P%.31s\n" /* RFC 1179, Section 7.2 - user name <= 31 chars */
939 "J%.99s\n", /* RFC 1179, Section 7.2 - job name <= 99 chars */
940 localhost
, user
, title
);
941 cptr
= control
+ strlen(control
);
945 snprintf(cptr
, sizeof(control
) - (cptr
- control
),
946 "C%.31s\n" /* RFC 1179, Section 7.2 - class name <= 31 chars */
949 cptr
+= strlen(cptr
);
954 snprintf(cptr
, sizeof(control
) - (cptr
- control
), "%cdfA%03d%.15s\n",
955 format
, (int)getpid() % 1000, localhost
);
956 cptr
+= strlen(cptr
);
960 snprintf(cptr
, sizeof(control
) - (cptr
- control
),
962 "N%.131s\n", /* RFC 1179, Section 7.2 - sourcefile name <= 131 chars */
963 (int)getpid() % 1000, localhost
, title
);
965 fprintf(stderr
, "DEBUG: Control file is:\n%s", control
);
967 if (order
== ORDER_CONTROL_DATA
)
969 if (lpd_command(fd
, timeout
, "\002%d cfA%03.3d%.15s\n", strlen(control
),
970 (int)getpid() % 1000, localhost
))
972 httpAddrFreeList(addrlist
);
975 return (CUPS_BACKEND_FAILED
);
978 _cupsLangPrintf(stderr
, _("INFO: Sending control file (%u bytes)\n"),
979 (unsigned)strlen(control
));
981 if (lpd_write(fd
, control
, strlen(control
) + 1) < (strlen(control
) + 1))
984 perror("ERROR: Unable to write control file");
990 if (read(fd
, &status
, 1) < 1)
992 _cupsLangPrintf(stderr
,
993 _("WARNING: Remote host did not respond with control "
994 "status byte after %d seconds!\n"), timeout
);
1002 _cupsLangPrintf(stderr
,
1003 _("ERROR: Remote host did not accept control file "
1006 _cupsLangPuts(stderr
, _("INFO: Control file sent successfully\n"));
1014 * Send the print file...
1017 if (lpd_command(fd
, timeout
, "\003" CUPS_LLFMT
" dfA%03.3d%.15s\n",
1018 CUPS_LLCAST filestats
.st_size
, (int)getpid() % 1000,
1021 httpAddrFreeList(addrlist
);
1024 return (CUPS_BACKEND_FAILED
);
1027 _cupsLangPrintf(stderr
,
1028 #ifdef HAVE_LONG_LONG
1029 _("INFO: Sending data file (%lld bytes)\n"),
1031 _("INFO: Sending data file (%ld bytes)\n"),
1032 #endif /* HAVE_LONG_LONG */
1033 CUPS_LLCAST filestats
.st_size
);
1036 for (copy
= 0; copy
< manual_copies
; copy
++)
1038 lseek(print_fd
, 0, SEEK_SET
);
1040 while ((nbytes
= read(print_fd
, buffer
, sizeof(buffer
))) > 0)
1042 _cupsLangPrintf(stderr
,
1043 _("INFO: Spooling LPR job, %.0f%% complete...\n"),
1044 100.0 * tbytes
/ filestats
.st_size
);
1046 if (lpd_write(fd
, buffer
, nbytes
) < nbytes
)
1048 perror("ERROR: Unable to send print file to printer");
1056 if (mode
== MODE_STANDARD
)
1058 if (tbytes
< filestats
.st_size
)
1060 else if (lpd_write(fd
, "", 1) < 1)
1062 perror("ERROR: Unable to send trailing nul to printer");
1068 * Read the status byte from the printer; if we can't read the byte
1069 * back now, we should set status to "errno", however at this point
1070 * we know the printer got the whole file and we don't necessarily
1071 * want to requeue it over and over...
1076 if (recv(fd
, &status
, 1, 0) < 1)
1078 _cupsLangPrintf(stderr
,
1079 _("WARNING: Remote host did not respond with data "
1080 "status byte after %d seconds!\n"), timeout
);
1091 _cupsLangPrintf(stderr
,
1092 _("ERROR: Remote host did not accept data file (%d)\n"),
1095 _cupsLangPuts(stderr
, _("INFO: Data file sent successfully\n"));
1098 if (status
== 0 && order
== ORDER_DATA_CONTROL
)
1100 if (lpd_command(fd
, timeout
, "\002%d cfA%03.3d%.15s\n", strlen(control
),
1101 (int)getpid() % 1000, localhost
))
1103 httpAddrFreeList(addrlist
);
1106 return (CUPS_BACKEND_FAILED
);
1109 _cupsLangPrintf(stderr
, _("INFO: Sending control file (%lu bytes)\n"),
1110 (unsigned long)strlen(control
));
1112 if (lpd_write(fd
, control
, strlen(control
) + 1) < (strlen(control
) + 1))
1115 perror("ERROR: Unable to write control file");
1121 if (read(fd
, &status
, 1) < 1)
1123 _cupsLangPrintf(stderr
,
1124 _("WARNING: Remote host did not respond with control "
1125 "status byte after %d seconds!\n"), timeout
);
1133 _cupsLangPrintf(stderr
,
1134 _("ERROR: Remote host did not accept control file "
1137 _cupsLangPuts(stderr
, _("INFO: Control file sent successfully\n"));
1141 * Close the socket connection and input file...
1148 httpAddrFreeList(addrlist
);
1150 return (CUPS_BACKEND_OK
);
1154 * Waiting for a retry...
1160 httpAddrFreeList(addrlist
);
1163 * If we get here, then the job has been cancelled...
1166 return (CUPS_BACKEND_FAILED
);
1171 * 'lpd_timeout()' - Handle timeout alarms...
1175 lpd_timeout(int sig
) /* I - Signal number */
1179 #if !defined(HAVE_SIGSET) && !defined(HAVE_SIGACTION)
1180 signal(SIGALRM
, lpd_timeout
);
1181 #endif /* !HAVE_SIGSET && !HAVE_SIGACTION */
1186 * 'lpd_write()' - Write a buffer of data to an LPD server.
1189 static int /* O - Number of bytes written or -1 on error */
1190 lpd_write(int lpd_fd
, /* I - LPD socket */
1191 char *buffer
, /* I - Buffer to write */
1192 int length
) /* I - Number of bytes to write */
1194 int bytes
, /* Number of bytes written */
1195 total
; /* Total number of bytes written */
1202 while ((bytes
= send(lpd_fd
, buffer
, length
- total
, 0)) >= 0)
1207 if (total
== length
)
1218 #ifndef HAVE_RRESVPORT_AF
1220 * 'rresvport_af()' - A simple implementation of rresvport_af().
1223 static int /* O - Socket or -1 on error */
1224 rresvport_af(int *port
, /* IO - Port number to bind to */
1225 int family
) /* I - Address family */
1227 http_addr_t addr
; /* Socket address */
1228 int fd
; /* Socket file descriptor */
1232 * Try to create an IPv4 socket...
1235 if ((fd
= socket(family
, SOCK_STREAM
, 0)) < 0)
1239 * Initialize the address buffer...
1242 memset(&addr
, 0, sizeof(addr
));
1243 addr
.addr
.sa_family
= family
;
1246 * Try to bind the socket to a reserved port...
1252 * Set the port number...
1256 if (family
== AF_INET6
)
1257 addr
.ipv6
.sin6_port
= htons(*port
);
1259 # endif /* AF_INET6 */
1260 addr
.ipv4
.sin_port
= htons(*port
);
1263 * Try binding the port to the socket; return if all is OK...
1266 if (!bind(fd
, (struct sockaddr
*)&addr
, sizeof(addr
)))
1270 * Stop if we have any error other than "address already in use"...
1273 if (errno
!= EADDRINUSE
)
1285 * Try the next port...
1292 * Wasn't able to bind to a reserved port, so close the socket and return
1304 #endif /* !HAVE_RRESVPORT_AF */
1308 * 'sigterm_handler()' - Handle 'terminate' signals that stop the backend.
1312 sigterm_handler(int sig
) /* I - Signal */
1314 (void)sig
; /* remove compiler warnings... */