]>
git.ipfire.org Git - thirdparty/cups.git/blob - backend/lpd.c
872fb1cc630567bf8f249048e8070ad08b804e4f
4 * Line Printer Daemon backend for CUPS.
6 * Copyright 2007-2013 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 * Include necessary headers.
22 #include <cups/http-private.h>
23 #include "backend-private.h"
25 #include <sys/types.h>
32 # include <sys/socket.h>
33 # include <netinet/in.h>
34 # include <arpa/inet.h>
38 # include <CoreFoundation/CFNumber.h>
39 # include <CoreFoundation/CFPreferences.h>
40 #endif /* __APPLE__ */
47 static char tmpfilename
[1024] = ""; /* Temporary spool file name */
48 static int abort_job
= 0; /* Non-zero if we get SIGTERM */
55 #define MODE_STANDARD 0 /* Queue a copy */
56 #define MODE_STREAM 1 /* Stream a copy */
60 * The order for control and data files in LPD requests...
63 #define ORDER_CONTROL_DATA 0 /* Control file first, then data */
64 #define ORDER_DATA_CONTROL 1 /* Data file first, then control */
71 #define RESERVE_NONE 0 /* Don't reserve a priviledged port */
72 #define RESERVE_RFC1179 1 /* Reserve port 721-731 */
73 #define RESERVE_ANY 2 /* Reserve port 1-1023 */
80 static int lpd_command(int lpd_fd
, char *format
, ...);
81 static int lpd_queue(const char *hostname
, http_addrlist_t
*addrlist
,
82 const char *printer
, int print_fd
, int snmp_fd
,
83 int mode
, const char *user
, const char *title
,
84 int copies
, int banner
, int format
, int order
,
85 int reserve
, int manual_copies
, int timeout
,
86 int contimeout
, const char *orighost
);
87 static int lpd_write(int lpd_fd
, char *buffer
, int length
);
88 #ifndef HAVE_RRESVPORT_AF
89 static int rresvport_af(int *port
, int family
);
90 #endif /* !HAVE_RRESVPORT_AF */
91 static void sigterm_handler(int sig
);
95 * 'main()' - Send a file to the printer or server.
99 * printer-uri job-id user title copies options [file]
102 int /* O - Exit status */
103 main(int argc
, /* I - Number of command-line arguments (6 or 7) */
104 char *argv
[]) /* I - Command-line arguments */
106 const char *device_uri
; /* Device URI */
107 char scheme
[255], /* Scheme in URI */
108 hostname
[1024], /* Hostname */
109 username
[255], /* Username info */
110 resource
[1024], /* Resource info (printer name) */
111 *options
, /* Pointer to options */
112 *name
, /* Name of option */
113 *value
, /* Value of option */
114 sep
, /* Separator character */
115 *filename
, /* File to print */
116 title
[256]; /* Title string */
117 int port
; /* Port number */
118 char portname
[256]; /* Port name (string) */
119 http_addrlist_t
*addrlist
; /* List of addresses for printer */
120 int snmp_enabled
= 1; /* Is SNMP enabled? */
121 int snmp_fd
; /* SNMP socket */
122 int fd
; /* Print file */
123 int status
; /* Status of LPD job */
124 int mode
; /* Print mode */
125 int banner
; /* Print banner page? */
126 int format
; /* Print format */
127 int order
; /* Order of control/data files */
128 int reserve
; /* Reserve priviledged port? */
129 int sanitize_title
; /* Sanitize title string? */
130 int manual_copies
, /* Do manual copies? */
131 timeout
, /* Timeout */
132 contimeout
, /* Connection timeout */
133 copies
; /* Number of copies */
134 ssize_t bytes
= 0; /* Initial bytes read */
135 char buffer
[16384]; /* Initial print buffer */
136 #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
137 struct sigaction action
; /* Actions for POSIX signals */
138 #endif /* HAVE_SIGACTION && !HAVE_SIGSET */
139 int num_jobopts
; /* Number of job options */
140 cups_option_t
*jobopts
= NULL
; /* Job options */
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 printf("network lpd \"Unknown\" \"%s\"\n",
177 _cupsLangString(cupsLangDefault(), _("LPD/LPR Host or Printer")));
178 return (CUPS_BACKEND_OK
);
180 else if (argc
< 6 || argc
> 7)
182 _cupsLangPrintf(stderr
,
183 _("Usage: %s job-id user title copies options [file]"),
185 return (CUPS_BACKEND_FAILED
);
188 num_jobopts
= cupsParseOptions(argv
[5], 0, &jobopts
);
191 * Extract the hostname and printer name from the URI...
194 while ((device_uri
= cupsBackendDeviceURI(argv
)) == NULL
)
196 _cupsLangPrintFilter(stderr
, "INFO", _("Unable to locate printer."));
199 if (getenv("CLASS") != NULL
)
200 return (CUPS_BACKEND_FAILED
);
203 httpSeparateURI(HTTP_URI_CODING_ALL
, device_uri
, scheme
, sizeof(scheme
),
204 username
, sizeof(username
), hostname
, sizeof(hostname
), &port
,
205 resource
, sizeof(resource
));
208 port
= 515; /* Default to port 515 */
213 * If no username is in the device URI, then use the print job user...
216 strlcpy(username
, argv
[2], sizeof(username
));
220 * See if there are any options...
223 mode
= MODE_STANDARD
;
226 order
= ORDER_CONTROL_DATA
;
227 reserve
= RESERVE_ANY
;
230 contimeout
= 7 * 24 * 60 * 60;
234 * We want to pass UTF-8 characters by default, not re-map them (3071945)
240 * Otherwise we want to re-map UTF-8 to "safe" characters by default...
244 #endif /* __APPLE__ */
246 if ((options
= strchr(resource
, '?')) != NULL
)
249 * Yup, terminate the device name string and move to the first
250 * character of the options...
267 while (*options
&& *options
!= '=' && *options
!= '+' && *options
!= '&')
270 if ((sep
= *options
) != '\0')
281 while (*options
&& *options
!= '+' && *options
!= '&')
291 * Process the option...
294 if (!_cups_strcasecmp(name
, "banner"))
300 banner
= !value
[0] || !_cups_strcasecmp(value
, "on") ||
301 !_cups_strcasecmp(value
, "yes") || !_cups_strcasecmp(value
, "true");
303 else if (!_cups_strcasecmp(name
, "format") && value
[0])
306 * Set output format...
309 if (strchr("cdfglnoprtv", value
[0]))
312 _cupsLangPrintFilter(stderr
, "ERROR",
313 _("Unknown format character: \"%c\"."),
316 else if (!_cups_strcasecmp(name
, "mode") && value
[0])
319 * Set control/data order...
322 if (!_cups_strcasecmp(value
, "standard"))
323 mode
= MODE_STANDARD
;
324 else if (!_cups_strcasecmp(value
, "stream"))
327 _cupsLangPrintFilter(stderr
, "ERROR",
328 _("Unknown print mode: \"%s\"."), value
);
330 else if (!_cups_strcasecmp(name
, "order") && value
[0])
333 * Set control/data order...
336 if (!_cups_strcasecmp(value
, "control,data"))
337 order
= ORDER_CONTROL_DATA
;
338 else if (!_cups_strcasecmp(value
, "data,control"))
339 order
= ORDER_DATA_CONTROL
;
341 _cupsLangPrintFilter(stderr
, "ERROR",
342 _("Unknown file order: \"%s\"."), value
);
344 else if (!_cups_strcasecmp(name
, "reserve"))
347 * Set port reservation mode...
350 if (!value
[0] || !_cups_strcasecmp(value
, "on") ||
351 !_cups_strcasecmp(value
, "yes") ||
352 !_cups_strcasecmp(value
, "true") ||
353 !_cups_strcasecmp(value
, "rfc1179"))
354 reserve
= RESERVE_RFC1179
;
355 else if (!_cups_strcasecmp(value
, "any"))
356 reserve
= RESERVE_ANY
;
358 reserve
= RESERVE_NONE
;
360 else if (!_cups_strcasecmp(name
, "manual_copies"))
363 * Set manual copies...
366 manual_copies
= !value
[0] || !_cups_strcasecmp(value
, "on") ||
367 !_cups_strcasecmp(value
, "yes") ||
368 !_cups_strcasecmp(value
, "true");
370 else if (!_cups_strcasecmp(name
, "sanitize_title"))
373 * Set sanitize title...
376 sanitize_title
= !value
[0] || !_cups_strcasecmp(value
, "on") ||
377 !_cups_strcasecmp(value
, "yes") ||
378 !_cups_strcasecmp(value
, "true");
380 else if (!_cups_strcasecmp(name
, "snmp"))
383 * Enable/disable SNMP stuff...
386 snmp_enabled
= !value
[0] || !_cups_strcasecmp(value
, "on") ||
387 _cups_strcasecmp(value
, "yes") ||
388 _cups_strcasecmp(value
, "true");
390 else if (!_cups_strcasecmp(name
, "timeout"))
397 timeout
= atoi(value
);
399 else if (!_cups_strcasecmp(name
, "contimeout"))
402 * Set the connection timeout...
406 contimeout
= atoi(value
);
411 if (mode
== MODE_STREAM
)
412 order
= ORDER_CONTROL_DATA
;
415 * Find the printer...
418 snprintf(portname
, sizeof(portname
), "%d", port
);
420 fputs("STATE: +connecting-to-device\n", stderr
);
421 fprintf(stderr
, "DEBUG: Looking up \"%s\"...\n", hostname
);
423 while ((addrlist
= httpAddrGetList(hostname
, AF_UNSPEC
, portname
)) == NULL
)
425 _cupsLangPrintFilter(stderr
, "INFO",
426 _("Unable to locate printer \"%s\"."), hostname
);
429 if (getenv("CLASS") != NULL
)
431 fputs("STATE: -connecting-to-device\n", stderr
);
432 exit(CUPS_BACKEND_FAILED
);
437 snmp_fd
= _cupsSNMPOpen(addrlist
->addr
.addr
.sa_family
);
442 * Wait for data from the filter...
447 if (!backendWaitLoop(snmp_fd
, &(addrlist
->addr
), 0, backendNetworkSideCB
))
448 return (CUPS_BACKEND_OK
);
449 else if (mode
== MODE_STANDARD
&&
450 (bytes
= read(0, buffer
, sizeof(buffer
))) <= 0)
451 return (CUPS_BACKEND_OK
);
455 * If we have 7 arguments, print the file named on the command-line.
456 * Otherwise, copy stdin to a temporary file and print the temporary
460 if (argc
== 6 && mode
== MODE_STANDARD
)
463 * Copy stdin to a temporary file...
466 if ((fd
= cupsTempFd(tmpfilename
, sizeof(tmpfilename
))) < 0)
468 perror("DEBUG: Unable to create temporary file");
469 return (CUPS_BACKEND_FAILED
);
472 _cupsLangPrintFilter(stderr
, "INFO", _("Copying print data."));
475 write(fd
, buffer
, bytes
);
477 backendRunLoop(-1, fd
, snmp_fd
, &(addrlist
->addr
), 0, 0,
478 backendNetworkSideCB
);
483 * Stream from stdin...
492 fd
= open(filename
, O_RDONLY
);
496 _cupsLangPrintError("ERROR", _("Unable to open print file"));
497 return (CUPS_BACKEND_FAILED
);
502 * Sanitize the document title...
505 strlcpy(title
, argv
[3], sizeof(title
));
510 * Sanitize the title string so that we don't cause problems on
516 for (ptr
= title
; *ptr
; ptr
++)
517 if (!isalnum(*ptr
& 255) && !isspace(*ptr
& 255))
529 manual_copies
= atoi(argv
[4]);
535 copies
= atoi(argv
[4]);
538 status
= lpd_queue(hostname
, addrlist
, resource
+ 1, fd
, snmp_fd
, mode
,
539 username
, title
, copies
, banner
, format
, order
, reserve
,
540 manual_copies
, timeout
, contimeout
,
541 cupsGetOption("job-originating-host-name", num_jobopts
,
545 fprintf(stderr
, "PAGE: 1 %d\n", atoi(argv
[4]));
548 status
= lpd_queue(hostname
, addrlist
, resource
+ 1, fd
, snmp_fd
, mode
,
549 username
, title
, 1, banner
, format
, order
, reserve
, 1,
551 cupsGetOption("job-originating-host-name", num_jobopts
,
555 * Remove the temporary file if necessary...
565 _cupsSNMPClose(snmp_fd
);
568 * Return the queue status...
576 * 'lpd_command()' - Send an LPR command sequence and wait for a reply.
579 static int /* O - Status of command */
580 lpd_command(int fd
, /* I - Socket connection to LPD host */
581 char *format
, /* I - printf()-style format string */
582 ...) /* I - Additional args as necessary */
584 va_list ap
; /* Argument pointer */
585 char buf
[1024]; /* Output buffer */
586 int bytes
; /* Number of bytes to output */
587 char status
; /* Status from command */
591 * Don't try to send commands if the job has been canceled...
598 * Format the string...
601 va_start(ap
, format
);
602 bytes
= vsnprintf(buf
, sizeof(buf
), format
, ap
);
605 fprintf(stderr
, "DEBUG: lpd_command %2.2x %s", buf
[0], buf
+ 1);
608 * Send the command...
611 fprintf(stderr
, "DEBUG: Sending command string (%d bytes)...\n", bytes
);
613 if (lpd_write(fd
, buf
, bytes
) < bytes
)
615 perror("DEBUG: Unable to send LPD command");
620 * Read back the status from the command and return it...
623 fputs("DEBUG: Reading command status...\n", stderr
);
625 if (recv(fd
, &status
, 1, 0) < 1)
627 _cupsLangPrintFilter(stderr
, "WARNING", _("The printer did not respond."));
631 fprintf(stderr
, "DEBUG: lpd_command returning %d\n", status
);
638 * 'lpd_queue()' - Queue a file using the Line Printer Daemon protocol.
641 static int /* O - Zero on success, non-zero on failure */
642 lpd_queue(const char *hostname
, /* I - Host to connect to */
643 http_addrlist_t
*addrlist
, /* I - List of host addresses */
644 const char *printer
, /* I - Printer/queue name */
645 int print_fd
, /* I - File to print */
646 int snmp_fd
, /* I - SNMP socket */
647 int mode
, /* I - Print mode */
648 const char *user
, /* I - Requesting user */
649 const char *title
, /* I - Job title */
650 int copies
, /* I - Number of copies */
651 int banner
, /* I - Print LPD banner? */
652 int format
, /* I - Format specifier */
653 int order
, /* I - Order of data/control files */
654 int reserve
, /* I - Reserve ports? */
655 int manual_copies
,/* I - Do copies by hand... */
656 int timeout
, /* I - Timeout... */
657 int contimeout
, /* I - Connection timeout */
658 const char *orighost
) /* I - job-originating-host-name */
660 char localhost
[255]; /* Local host name */
661 int error
; /* Error number */
662 struct stat filestats
; /* File statistics */
663 int lport
; /* LPD connection local port */
664 int fd
; /* LPD socket */
665 char control
[10240], /* LPD control 'file' */
666 *cptr
; /* Pointer into control file string */
667 char status
; /* Status byte from command */
668 int delay
; /* Delay for retries... */
669 char addrname
[256]; /* Address name */
670 http_addrlist_t
*addr
; /* Socket address */
671 int have_supplies
; /* Printer supports supply levels? */
672 int copy
; /* Copies written */
673 time_t start_time
; /* Time of first connect */
674 size_t nbytes
; /* Number of bytes written */
675 off_t tbytes
; /* Total bytes written */
676 char buffer
[32768]; /* Output buffer */
678 DWORD tv
; /* Timeout in milliseconds */
680 struct timeval tv
; /* Timeout in secs and usecs */
685 * Remember when we started trying to connect to the printer...
688 start_time
= time(NULL
);
691 * Loop forever trying to print the file...
697 * First try to reserve a port for this connection...
700 fprintf(stderr
, "DEBUG: Connecting to %s:%d for printer %s\n", hostname
,
701 httpAddrPort(&(addrlist
->addr
)), printer
);
702 _cupsLangPrintFilter(stderr
, "INFO", _("Connecting to printer."));
704 for (lport
= reserve
== RESERVE_RFC1179
? 732 : 1024, addr
= addrlist
,
709 * Stop if this job has been canceled...
713 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 perror("DEBUG: 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 perror("DEBUG: Unable to reserve port");
766 * Connect to the printer or server...
773 return (CUPS_BACKEND_FAILED
);
776 if (!connect(fd
, &(addr
->addr
.addr
), httpAddrLength(&(addr
->addr
))))
785 if (getenv("CLASS") != NULL
)
788 * If the CLASS environment variable is set, the job was submitted
789 * to a class and not to a specific queue. In this case, we want
790 * to abort immediately so that the job can be requeued on the next
791 * available printer in the class.
794 _cupsLangPrintFilter(stderr
, "INFO",
795 _("Unable to contact printer, queuing on next "
796 "printer in class."));
799 * Sleep 5 seconds to keep the job from requeuing too rapidly...
804 return (CUPS_BACKEND_FAILED
);
807 fprintf(stderr
, "DEBUG: Connection error: %s\n", strerror(error
));
809 if (error
== ECONNREFUSED
|| error
== EHOSTDOWN
||
810 error
== EHOSTUNREACH
)
812 if (contimeout
&& (time(NULL
) - start_time
) > contimeout
)
814 _cupsLangPrintFilter(stderr
, "ERROR",
815 _("The printer is not responding."));
816 return (CUPS_BACKEND_FAILED
);
822 _cupsLangPrintFilter(stderr
, "WARNING",
823 _("The printer may not exist or "
824 "is unavailable at this time."));
828 _cupsLangPrintFilter(stderr
, "WARNING",
829 _("The printer is unreachable at "
835 _cupsLangPrintFilter(stderr
, "WARNING",
836 _("The printer is in use."));
845 else if (error
== EADDRINUSE
)
848 * Try on another port...
855 _cupsLangPrintFilter(stderr
, "ERROR",
856 _("The printer is not responding."));
866 tv
= (DWORD
)(timeout
* 1000);
868 setsockopt(fd
, SOL_SOCKET
, SO_RCVTIMEO
, (char *)&tv
, sizeof(tv
));
869 setsockopt(fd
, SOL_SOCKET
, SO_SNDTIMEO
, (char *)&tv
, sizeof(tv
));
874 setsockopt(fd
, SOL_SOCKET
, SO_RCVTIMEO
, &tv
, sizeof(tv
));
875 setsockopt(fd
, SOL_SOCKET
, SO_SNDTIMEO
, &tv
, sizeof(tv
));
878 fputs("STATE: -connecting-to-device\n", stderr
);
879 _cupsLangPrintFilter(stderr
, "INFO", _("Connected to printer."));
881 fprintf(stderr
, "DEBUG: Connected to %s:%d (local port %d)...\n",
882 httpAddrString(&(addr
->addr
), addrname
, sizeof(addrname
)),
883 httpAddrPort(&(addr
->addr
)), lport
);
886 * See if the printer supports SNMP...
890 have_supplies
= !backendSNMPSupplies(snmp_fd
, &(addrlist
->addr
), NULL
,
896 * Check for side-channel requests...
899 backendCheckSideChannel(snmp_fd
, &(addrlist
->addr
));
902 * Next, open the print file and figure out its size...
908 * Use the size from the print file...
911 if (fstat(print_fd
, &filestats
))
915 perror("DEBUG: unable to stat print file");
916 return (CUPS_BACKEND_FAILED
);
919 filestats
.st_size
*= manual_copies
;
924 * Use a "very large value" for the size so that the printer will
925 * keep printing until we close the connection...
928 #ifdef _LARGEFILE_SOURCE
929 filestats
.st_size
= (size_t)(999999999999.0);
931 filestats
.st_size
= 2147483647;
932 #endif /* _LARGEFILE_SOURCE */
936 * Send a job header to the printer, specifying no banner page and
940 if (lpd_command(fd
, "\002%s\n",
941 printer
)) /* Receive print job(s) */
944 return (CUPS_BACKEND_FAILED
);
947 if (orighost
&& _cups_strcasecmp(orighost
, "localhost"))
948 strlcpy(localhost
, orighost
, sizeof(localhost
));
950 httpGetHostname(NULL
, localhost
, sizeof(localhost
));
952 snprintf(control
, sizeof(control
),
953 "H%.31s\n" /* RFC 1179, Section 7.2 - host name <= 31 chars */
954 "P%.31s\n" /* RFC 1179, Section 7.2 - user name <= 31 chars */
955 "J%.99s\n", /* RFC 1179, Section 7.2 - job name <= 99 chars */
956 localhost
, user
, title
);
957 cptr
= control
+ strlen(control
);
961 snprintf(cptr
, sizeof(control
) - (cptr
- control
),
962 "C%.31s\n" /* RFC 1179, Section 7.2 - class name <= 31 chars */
965 cptr
+= strlen(cptr
);
970 snprintf(cptr
, sizeof(control
) - (cptr
- control
), "%cdfA%03d%.15s\n",
971 format
, (int)getpid() % 1000, localhost
);
972 cptr
+= strlen(cptr
);
976 snprintf(cptr
, sizeof(control
) - (cptr
- control
),
978 "N%.131s\n", /* RFC 1179, Section 7.2 - sourcefile name <= 131 chars */
979 (int)getpid() % 1000, localhost
, title
);
981 fprintf(stderr
, "DEBUG: Control file is:\n%s", control
);
983 if (order
== ORDER_CONTROL_DATA
)
986 * Check for side-channel requests...
989 backendCheckSideChannel(snmp_fd
, &(addr
->addr
));
992 * Send the control file...
995 if (lpd_command(fd
, "\002%d cfA%03.3d%.15s\n", strlen(control
),
996 (int)getpid() % 1000, localhost
))
1000 return (CUPS_BACKEND_FAILED
);
1003 fprintf(stderr
, "DEBUG: Sending control file (%u bytes)\n",
1004 (unsigned)strlen(control
));
1006 if (lpd_write(fd
, control
, strlen(control
) + 1) < (strlen(control
) + 1))
1009 perror("DEBUG: Unable to write control file");
1014 if (read(fd
, &status
, 1) < 1)
1016 _cupsLangPrintFilter(stderr
, "WARNING",
1017 _("The printer did not respond."));
1023 _cupsLangPrintFilter(stderr
, "ERROR",
1024 _("Remote host did not accept control file (%d)."),
1027 _cupsLangPrintFilter(stderr
, "INFO",
1028 _("Control file sent successfully."));
1036 * Check for side-channel requests...
1039 backendCheckSideChannel(snmp_fd
, &(addr
->addr
));
1042 * Send the print file...
1045 if (lpd_command(fd
, "\003" CUPS_LLFMT
" dfA%03.3d%.15s\n",
1046 CUPS_LLCAST filestats
.st_size
, (int)getpid() % 1000,
1051 return (CUPS_BACKEND_FAILED
);
1054 fprintf(stderr
, "DEBUG: Sending data file (" CUPS_LLFMT
" bytes)\n",
1055 CUPS_LLCAST filestats
.st_size
);
1058 for (copy
= 0; copy
< manual_copies
; copy
++)
1060 lseek(print_fd
, 0, SEEK_SET
);
1062 while ((nbytes
= read(print_fd
, buffer
, sizeof(buffer
))) > 0)
1064 _cupsLangPrintFilter(stderr
, "INFO",
1065 _("Spooling job, %.0f%% complete."),
1066 100.0 * tbytes
/ filestats
.st_size
);
1068 if (lpd_write(fd
, buffer
, nbytes
) < nbytes
)
1070 perror("DEBUG: Unable to send print file to printer");
1078 if (mode
== MODE_STANDARD
)
1080 if (tbytes
< filestats
.st_size
)
1082 else if (lpd_write(fd
, "", 1) < 1)
1084 perror("DEBUG: Unable to send trailing nul to printer");
1090 * Read the status byte from the printer; if we can't read the byte
1091 * back now, we should set status to "errno", however at this point
1092 * we know the printer got the whole file and we don't necessarily
1093 * want to requeue it over and over...
1096 if (recv(fd
, &status
, 1, 0) < 1)
1098 _cupsLangPrintFilter(stderr
, "WARNING",
1099 _("The printer did not respond."));
1108 _cupsLangPrintFilter(stderr
, "ERROR",
1109 _("Remote host did not accept data file (%d)."),
1112 _cupsLangPrintFilter(stderr
, "INFO",
1113 _("Data file sent successfully."));
1116 if (status
== 0 && order
== ORDER_DATA_CONTROL
)
1119 * Check for side-channel requests...
1122 backendCheckSideChannel(snmp_fd
, &(addr
->addr
));
1125 * Send control file...
1128 if (lpd_command(fd
, "\002%d cfA%03.3d%.15s\n", strlen(control
),
1129 (int)getpid() % 1000, localhost
))
1133 return (CUPS_BACKEND_FAILED
);
1136 fprintf(stderr
, "DEBUG: Sending control file (%lu bytes)\n",
1137 (unsigned long)strlen(control
));
1139 if (lpd_write(fd
, control
, strlen(control
) + 1) < (strlen(control
) + 1))
1142 perror("DEBUG: Unable to write control file");
1146 if (read(fd
, &status
, 1) < 1)
1148 _cupsLangPrintFilter(stderr
, "WARNING",
1149 _("The printer did not respond."));
1155 _cupsLangPrintFilter(stderr
, "ERROR",
1156 _("Remote host did not accept control file (%d)."),
1159 _cupsLangPrintFilter(stderr
, "INFO",
1160 _("Control file sent successfully."));
1163 fputs("STATE: +cups-waiting-for-job-completed\n", stderr
);
1166 * Collect the final supply levels as needed...
1170 backendSNMPSupplies(snmp_fd
, &(addr
->addr
), NULL
, NULL
);
1173 * Close the socket connection and input file...
1179 return (CUPS_BACKEND_OK
);
1182 * Waiting for a retry...
1189 * If we get here, then the job has been canceled...
1192 return (CUPS_BACKEND_FAILED
);
1197 * 'lpd_write()' - Write a buffer of data to an LPD server.
1200 static int /* O - Number of bytes written or -1 on error */
1201 lpd_write(int lpd_fd
, /* I - LPD socket */
1202 char *buffer
, /* I - Buffer to write */
1203 int length
) /* I - Number of bytes to write */
1205 int bytes
, /* Number of bytes written */
1206 total
; /* Total number of bytes written */
1213 while ((bytes
= send(lpd_fd
, buffer
, length
- total
, 0)) >= 0)
1218 if (total
== length
)
1229 #ifndef HAVE_RRESVPORT_AF
1231 * 'rresvport_af()' - A simple implementation of rresvport_af().
1234 static int /* O - Socket or -1 on error */
1235 rresvport_af(int *port
, /* IO - Port number to bind to */
1236 int family
) /* I - Address family */
1238 http_addr_t addr
; /* Socket address */
1239 int fd
; /* Socket file descriptor */
1243 * Try to create an IPv4 socket...
1246 if ((fd
= socket(family
, SOCK_STREAM
, 0)) < 0)
1250 * Initialize the address buffer...
1253 memset(&addr
, 0, sizeof(addr
));
1254 addr
.addr
.sa_family
= family
;
1257 * Try to bind the socket to a reserved port...
1263 * Set the port number...
1266 _httpAddrSetPort(&addr
, *port
);
1269 * Try binding the port to the socket; return if all is OK...
1272 if (!bind(fd
, (struct sockaddr
*)&addr
, sizeof(addr
)))
1276 * Stop if we have any error other than "address already in use"...
1279 if (errno
!= EADDRINUSE
)
1281 httpAddrClose(NULL
, fd
);
1287 * Try the next port...
1294 * Wasn't able to bind to a reserved port, so close the socket and return
1306 #endif /* !HAVE_RRESVPORT_AF */
1310 * 'sigterm_handler()' - Handle 'terminate' signals that stop the backend.
1314 sigterm_handler(int sig
) /* I - Signal */
1316 (void)sig
; /* remove compiler warnings... */