]>
git.ipfire.org Git - thirdparty/cups.git/blob - backend/lpd.c
2 * Line Printer Daemon backend for CUPS.
4 * Copyright © 2007-2016 by Apple Inc.
5 * Copyright © 1997-2007 by Easy Software Products, all rights reserved.
7 * Licensed under Apache License v2.0. See the file "LICENSE" for more
12 * Include necessary headers.
15 #include <cups/http-private.h>
16 #include "backend-private.h"
18 #include <sys/types.h>
25 # include <sys/socket.h>
26 # include <netinet/in.h>
27 # include <arpa/inet.h>
31 # include <CoreFoundation/CFNumber.h>
32 # include <CoreFoundation/CFPreferences.h>
33 #endif /* __APPLE__ */
40 static char tmpfilename
[1024] = ""; /* Temporary spool file name */
41 static int abort_job
= 0; /* Non-zero if we get SIGTERM */
48 #define MODE_STANDARD 0 /* Queue a copy */
49 #define MODE_STREAM 1 /* Stream a copy */
53 * The order for control and data files in LPD requests...
56 #define ORDER_CONTROL_DATA 0 /* Control file first, then data */
57 #define ORDER_DATA_CONTROL 1 /* Data file first, then control */
64 #define RESERVE_NONE 0 /* Don't reserve a priviledged port */
65 #define RESERVE_RFC1179 1 /* Reserve port 721-731 */
66 #define RESERVE_ANY 2 /* Reserve port 1-1023 */
73 static int cups_rresvport(int *port
, int family
);
74 static int lpd_command(int lpd_fd
, char *format
, ...);
75 static int lpd_queue(const char *hostname
, http_addrlist_t
*addrlist
,
76 const char *printer
, int print_fd
, int snmp_fd
,
77 int mode
, const char *user
, const char *title
,
78 int copies
, int banner
, int format
, int order
,
79 int reserve
, int manual_copies
, int timeout
,
80 int contimeout
, const char *orighost
);
81 static ssize_t
lpd_write(int lpd_fd
, char *buffer
, size_t length
);
82 static void sigterm_handler(int sig
);
86 * 'main()' - Send a file to the printer or server.
90 * printer-uri job-id user title copies options [file]
93 int /* O - Exit status */
94 main(int argc
, /* I - Number of command-line arguments (6 or 7) */
95 char *argv
[]) /* I - Command-line arguments */
97 const char *device_uri
; /* Device URI */
98 char scheme
[255], /* Scheme in URI */
99 hostname
[1024], /* Hostname */
100 username
[255], /* Username info */
101 resource
[1024], /* Resource info (printer name) */
102 *options
, /* Pointer to options */
103 *name
, /* Name of option */
104 *value
, /* Value of option */
105 sep
, /* Separator character */
106 *filename
, /* File to print */
107 title
[256]; /* Title string */
108 int port
; /* Port number */
109 http_addrlist_t
*addrlist
; /* List of addresses for printer */
110 int snmp_enabled
= 1; /* Is SNMP enabled? */
111 int snmp_fd
; /* SNMP socket */
112 int fd
; /* Print file */
113 int status
; /* Status of LPD job */
114 int mode
; /* Print mode */
115 int banner
; /* Print banner page? */
116 int format
; /* Print format */
117 int order
; /* Order of control/data files */
118 int reserve
; /* Reserve priviledged port? */
119 int sanitize_title
; /* Sanitize title string? */
120 int manual_copies
, /* Do manual copies? */
121 timeout
, /* Timeout */
122 contimeout
, /* Connection timeout */
123 copies
; /* Number of copies */
124 ssize_t bytes
= 0; /* Initial bytes read */
125 char buffer
[16384]; /* Initial print buffer */
126 #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
127 struct sigaction action
; /* Actions for POSIX signals */
128 #endif /* HAVE_SIGACTION && !HAVE_SIGSET */
129 int num_jobopts
; /* Number of job options */
130 cups_option_t
*jobopts
= NULL
; /* Job options */
134 * Make sure status messages are not buffered...
137 setbuf(stderr
, NULL
);
140 * Ignore SIGPIPE and catch SIGTERM signals...
144 sigset(SIGPIPE
, SIG_IGN
);
145 sigset(SIGTERM
, sigterm_handler
);
146 #elif defined(HAVE_SIGACTION)
147 memset(&action
, 0, sizeof(action
));
148 action
.sa_handler
= SIG_IGN
;
149 sigaction(SIGPIPE
, &action
, NULL
);
151 sigemptyset(&action
.sa_mask
);
152 sigaddset(&action
.sa_mask
, SIGTERM
);
153 action
.sa_handler
= sigterm_handler
;
154 sigaction(SIGTERM
, &action
, NULL
);
156 signal(SIGPIPE
, SIG_IGN
);
157 signal(SIGTERM
, sigterm_handler
);
158 #endif /* HAVE_SIGSET */
161 * Check command-line...
166 printf("network lpd \"Unknown\" \"%s\"\n",
167 _cupsLangString(cupsLangDefault(), _("LPD/LPR Host or Printer")));
168 return (CUPS_BACKEND_OK
);
170 else if (argc
< 6 || argc
> 7)
172 _cupsLangPrintf(stderr
,
173 _("Usage: %s job-id user title copies options [file]"),
175 return (CUPS_BACKEND_FAILED
);
178 num_jobopts
= cupsParseOptions(argv
[5], 0, &jobopts
);
181 * Extract the hostname and printer name from the URI...
184 while ((device_uri
= cupsBackendDeviceURI(argv
)) == NULL
)
186 _cupsLangPrintFilter(stderr
, "INFO", _("Unable to locate printer."));
189 if (getenv("CLASS") != NULL
)
190 return (CUPS_BACKEND_FAILED
);
193 httpSeparateURI(HTTP_URI_CODING_ALL
, device_uri
, scheme
, sizeof(scheme
),
194 username
, sizeof(username
), hostname
, sizeof(hostname
), &port
,
195 resource
, sizeof(resource
));
198 port
= 515; /* Default to port 515 */
203 * If no username is in the device URI, then use the print job user...
206 strlcpy(username
, argv
[2], sizeof(username
));
210 * See if there are any options...
213 mode
= MODE_STANDARD
;
216 order
= ORDER_CONTROL_DATA
;
217 reserve
= RESERVE_ANY
;
220 contimeout
= 7 * 24 * 60 * 60;
224 * We want to pass UTF-8 characters by default, not re-map them (3071945)
230 * Otherwise we want to re-map UTF-8 to "safe" characters by default...
234 #endif /* __APPLE__ */
236 if ((options
= strchr(resource
, '?')) != NULL
)
239 * Yup, terminate the device name string and move to the first
240 * character of the options...
257 while (*options
&& *options
!= '=' && *options
!= '+' && *options
!= '&')
260 if ((sep
= *options
) != '\0')
271 while (*options
&& *options
!= '+' && *options
!= '&')
281 * Process the option...
284 if (!_cups_strcasecmp(name
, "banner"))
290 banner
= !value
[0] || !_cups_strcasecmp(value
, "on") ||
291 !_cups_strcasecmp(value
, "yes") || !_cups_strcasecmp(value
, "true");
293 else if (!_cups_strcasecmp(name
, "format") && value
[0])
296 * Set output format...
299 if (strchr("cdfglnoprtv", value
[0]))
302 _cupsLangPrintFilter(stderr
, "ERROR",
303 _("Unknown format character: \"%c\"."),
306 else if (!_cups_strcasecmp(name
, "mode") && value
[0])
309 * Set control/data order...
312 if (!_cups_strcasecmp(value
, "standard"))
313 mode
= MODE_STANDARD
;
314 else if (!_cups_strcasecmp(value
, "stream"))
317 _cupsLangPrintFilter(stderr
, "ERROR",
318 _("Unknown print mode: \"%s\"."), value
);
320 else if (!_cups_strcasecmp(name
, "order") && value
[0])
323 * Set control/data order...
326 if (!_cups_strcasecmp(value
, "control,data"))
327 order
= ORDER_CONTROL_DATA
;
328 else if (!_cups_strcasecmp(value
, "data,control"))
329 order
= ORDER_DATA_CONTROL
;
331 _cupsLangPrintFilter(stderr
, "ERROR",
332 _("Unknown file order: \"%s\"."), value
);
334 else if (!_cups_strcasecmp(name
, "reserve"))
337 * Set port reservation mode...
340 if (!value
[0] || !_cups_strcasecmp(value
, "on") ||
341 !_cups_strcasecmp(value
, "yes") ||
342 !_cups_strcasecmp(value
, "true") ||
343 !_cups_strcasecmp(value
, "rfc1179"))
344 reserve
= RESERVE_RFC1179
;
345 else if (!_cups_strcasecmp(value
, "any"))
346 reserve
= RESERVE_ANY
;
348 reserve
= RESERVE_NONE
;
350 else if (!_cups_strcasecmp(name
, "manual_copies"))
353 * Set manual copies...
356 manual_copies
= !value
[0] || !_cups_strcasecmp(value
, "on") ||
357 !_cups_strcasecmp(value
, "yes") ||
358 !_cups_strcasecmp(value
, "true");
360 else if (!_cups_strcasecmp(name
, "sanitize_title"))
363 * Set sanitize title...
366 sanitize_title
= !value
[0] || !_cups_strcasecmp(value
, "on") ||
367 !_cups_strcasecmp(value
, "yes") ||
368 !_cups_strcasecmp(value
, "true");
370 else if (!_cups_strcasecmp(name
, "snmp"))
373 * Enable/disable SNMP stuff...
376 snmp_enabled
= !value
[0] || !_cups_strcasecmp(value
, "on") ||
377 !_cups_strcasecmp(value
, "yes") ||
378 !_cups_strcasecmp(value
, "true");
380 else if (!_cups_strcasecmp(name
, "timeout"))
387 timeout
= atoi(value
);
389 else if (!_cups_strcasecmp(name
, "contimeout"))
392 * Set the connection timeout...
396 contimeout
= atoi(value
);
401 if (mode
== MODE_STREAM
)
402 order
= ORDER_CONTROL_DATA
;
405 * Find the printer...
408 addrlist
= backendLookup(hostname
, port
, NULL
);
411 * See if the printer supports SNMP...
415 snmp_fd
= _cupsSNMPOpen(addrlist
->addr
.addr
.sa_family
);
420 * Wait for data from the filter...
425 if (!backendWaitLoop(snmp_fd
, &(addrlist
->addr
), 0, backendNetworkSideCB
))
426 return (CUPS_BACKEND_OK
);
427 else if (mode
== MODE_STANDARD
&&
428 (bytes
= read(0, buffer
, sizeof(buffer
))) <= 0)
429 return (CUPS_BACKEND_OK
);
433 * If we have 7 arguments, print the file named on the command-line.
434 * Otherwise, copy stdin to a temporary file and print the temporary
438 if (argc
== 6 && mode
== MODE_STANDARD
)
441 * Copy stdin to a temporary file...
444 if ((fd
= cupsTempFd(tmpfilename
, sizeof(tmpfilename
))) < 0)
446 perror("DEBUG: Unable to create temporary file");
447 return (CUPS_BACKEND_FAILED
);
450 _cupsLangPrintFilter(stderr
, "INFO", _("Copying print data."));
453 write(fd
, buffer
, (size_t)bytes
);
455 backendRunLoop(-1, fd
, snmp_fd
, &(addrlist
->addr
), 0, 0,
456 backendNetworkSideCB
);
461 * Stream from stdin...
470 fd
= open(filename
, O_RDONLY
);
474 _cupsLangPrintError("ERROR", _("Unable to open print file"));
475 return (CUPS_BACKEND_FAILED
);
480 * Sanitize the document title...
483 strlcpy(title
, argv
[3], sizeof(title
));
488 * Sanitize the title string so that we don't cause problems on
494 for (ptr
= title
; *ptr
; ptr
++)
495 if (!isalnum(*ptr
& 255) && !isspace(*ptr
& 255))
507 manual_copies
= atoi(argv
[4]);
513 copies
= atoi(argv
[4]);
516 status
= lpd_queue(hostname
, addrlist
, resource
+ 1, fd
, snmp_fd
, mode
,
517 username
, title
, copies
, banner
, format
, order
, reserve
,
518 manual_copies
, timeout
, contimeout
,
519 cupsGetOption("job-originating-host-name", num_jobopts
,
523 fprintf(stderr
, "PAGE: 1 %d\n", atoi(argv
[4]));
526 status
= lpd_queue(hostname
, addrlist
, resource
+ 1, fd
, snmp_fd
, mode
,
527 username
, title
, 1, banner
, format
, order
, reserve
, 1,
529 cupsGetOption("job-originating-host-name", num_jobopts
,
533 * Remove the temporary file if necessary...
543 _cupsSNMPClose(snmp_fd
);
546 * Return the queue status...
554 * 'cups_rresvport()' - A simple implementation of rresvport_af().
557 static int /* O - Socket or -1 on error */
558 cups_rresvport(int *port
, /* IO - Port number to bind to */
559 int family
) /* I - Address family */
561 http_addr_t addr
; /* Socket address */
562 int fd
; /* Socket file descriptor */
566 * Try to create an IPv4 socket...
569 if ((fd
= socket(family
, SOCK_STREAM
, 0)) < 0)
573 * Initialize the address buffer...
576 memset(&addr
, 0, sizeof(addr
));
577 addr
.addr
.sa_family
= (sa_family_t
)family
;
580 * Try to bind the socket to a reserved port...
586 * Set the port number...
589 _httpAddrSetPort(&addr
, *port
);
592 * Try binding the port to the socket; return if all is OK...
595 if (!bind(fd
, (struct sockaddr
*)&addr
, (socklen_t
)httpAddrLength(&addr
)))
599 * Stop if we have any error other than "address already in use"...
602 if (errno
!= EADDRINUSE
)
604 httpAddrClose(NULL
, fd
);
610 * Try the next port...
617 * Wasn't able to bind to a reserved port, so close the socket and return
632 * 'lpd_command()' - Send an LPR command sequence and wait for a reply.
635 static int /* O - Status of command */
636 lpd_command(int fd
, /* I - Socket connection to LPD host */
637 char *format
, /* I - printf()-style format string */
638 ...) /* I - Additional args as necessary */
640 va_list ap
; /* Argument pointer */
641 char buf
[1024]; /* Output buffer */
642 ssize_t bytes
; /* Number of bytes to output */
643 char status
; /* Status from command */
647 * Don't try to send commands if the job has been canceled...
654 * Format the string...
657 va_start(ap
, format
);
658 bytes
= vsnprintf(buf
, sizeof(buf
), format
, ap
);
661 fprintf(stderr
, "DEBUG: lpd_command %2.2x %s", buf
[0], buf
+ 1);
664 * Send the command...
667 fprintf(stderr
, "DEBUG: Sending command string (" CUPS_LLFMT
" bytes)...\n", CUPS_LLCAST bytes
);
669 if (lpd_write(fd
, buf
, (size_t)bytes
) < bytes
)
671 perror("DEBUG: Unable to send LPD command");
676 * Read back the status from the command and return it...
679 fputs("DEBUG: Reading command status...\n", stderr
);
681 if (recv(fd
, &status
, 1, 0) < 1)
683 _cupsLangPrintFilter(stderr
, "WARNING", _("The printer did not respond."));
684 status
= (char)errno
;
687 fprintf(stderr
, "DEBUG: lpd_command returning %d\n", status
);
694 * 'lpd_queue()' - Queue a file using the Line Printer Daemon protocol.
697 static int /* O - Zero on success, non-zero on failure */
698 lpd_queue(const char *hostname
, /* I - Host to connect to */
699 http_addrlist_t
*addrlist
, /* I - List of host addresses */
700 const char *printer
, /* I - Printer/queue name */
701 int print_fd
, /* I - File to print */
702 int snmp_fd
, /* I - SNMP socket */
703 int mode
, /* I - Print mode */
704 const char *user
, /* I - Requesting user */
705 const char *title
, /* I - Job title */
706 int copies
, /* I - Number of copies */
707 int banner
, /* I - Print LPD banner? */
708 int format
, /* I - Format specifier */
709 int order
, /* I - Order of data/control files */
710 int reserve
, /* I - Reserve ports? */
711 int manual_copies
,/* I - Do copies by hand... */
712 int timeout
, /* I - Timeout... */
713 int contimeout
, /* I - Connection timeout */
714 const char *orighost
) /* I - job-originating-host-name */
716 char localhost
[255]; /* Local host name */
717 int error
; /* Error number */
718 struct stat filestats
; /* File statistics */
719 int lport
; /* LPD connection local port */
720 int fd
; /* LPD socket */
721 char control
[10240], /* LPD control 'file' */
722 *cptr
; /* Pointer into control file string */
723 char status
; /* Status byte from command */
724 int delay
; /* Delay for retries... */
725 char addrname
[256]; /* Address name */
726 http_addrlist_t
*addr
; /* Socket address */
727 int have_supplies
; /* Printer supports supply levels? */
728 int copy
; /* Copies written */
729 time_t start_time
; /* Time of first connect */
730 ssize_t nbytes
; /* Number of bytes written */
731 off_t tbytes
; /* Total bytes written */
732 char buffer
[32768]; /* Output buffer */
734 DWORD tv
; /* Timeout in milliseconds */
736 struct timeval tv
; /* Timeout in secs and usecs */
741 * Remember when we started trying to connect to the printer...
744 start_time
= time(NULL
);
747 * Loop forever trying to print the file...
753 * First try to reserve a port for this connection...
756 fprintf(stderr
, "DEBUG: Connecting to %s:%d for printer %s\n", hostname
,
757 httpAddrPort(&(addrlist
->addr
)), printer
);
758 _cupsLangPrintFilter(stderr
, "INFO", _("Connecting to printer."));
760 for (lport
= reserve
== RESERVE_RFC1179
? 732 : 1024, addr
= addrlist
,
765 * Stop if this job has been canceled...
769 return (CUPS_BACKEND_FAILED
);
772 * Choose the next priviledged port...
780 if (lport
< 721 && reserve
== RESERVE_RFC1179
)
786 if (geteuid() || !reserve
)
788 if (getuid() || !reserve
)
789 #endif /* HAVE_GETEUID */
792 * Just create a regular socket...
795 if ((fd
= socket(addr
->addr
.addr
.sa_family
, SOCK_STREAM
, 0)) < 0)
797 perror("DEBUG: Unable to create socket");
808 * We're running as root and want to comply with RFC 1179. Reserve a
809 * priviledged lport between 721 and 731...
812 if ((fd
= cups_rresvport(&lport
, addr
->addr
.addr
.sa_family
)) < 0)
814 perror("DEBUG: Unable to reserve port");
822 * Connect to the printer or server...
829 return (CUPS_BACKEND_FAILED
);
832 if (!connect(fd
, &(addr
->addr
.addr
), (socklen_t
)httpAddrLength(&(addr
->addr
))))
841 if (getenv("CLASS") != NULL
)
844 * If the CLASS environment variable is set, the job was submitted
845 * to a class and not to a specific queue. In this case, we want
846 * to abort immediately so that the job can be requeued on the next
847 * available printer in the class.
850 _cupsLangPrintFilter(stderr
, "INFO",
851 _("Unable to contact printer, queuing on next "
852 "printer in class."));
855 * Sleep 5 seconds to keep the job from requeuing too rapidly...
860 return (CUPS_BACKEND_FAILED
);
863 fprintf(stderr
, "DEBUG: Connection error: %s\n", strerror(error
));
865 if (errno
== ECONNREFUSED
|| errno
== EHOSTDOWN
|| errno
== EHOSTUNREACH
|| errno
== ETIMEDOUT
|| errno
== ENOTCONN
)
867 if (contimeout
&& (time(NULL
) - start_time
) > contimeout
)
869 _cupsLangPrintFilter(stderr
, "ERROR",
870 _("The printer is not responding."));
871 return (CUPS_BACKEND_FAILED
);
877 _cupsLangPrintFilter(stderr
, "WARNING",
878 _("The printer may not exist or "
879 "is unavailable at this time."));
884 _cupsLangPrintFilter(stderr
, "WARNING",
885 _("The printer is unreachable at "
890 _cupsLangPrintFilter(stderr
, "WARNING",
891 _("The printer is in use."));
895 sleep((unsigned)delay
);
900 else if (error
== EADDRINUSE
)
903 * Try on another port...
910 _cupsLangPrintFilter(stderr
, "ERROR",
911 _("The printer is not responding."));
921 tv
= (DWORD
)(timeout
* 1000);
923 setsockopt(fd
, SOL_SOCKET
, SO_RCVTIMEO
, (char *)&tv
, sizeof(tv
));
924 setsockopt(fd
, SOL_SOCKET
, SO_SNDTIMEO
, (char *)&tv
, sizeof(tv
));
929 setsockopt(fd
, SOL_SOCKET
, SO_RCVTIMEO
, &tv
, sizeof(tv
));
930 setsockopt(fd
, SOL_SOCKET
, SO_SNDTIMEO
, &tv
, sizeof(tv
));
933 fputs("STATE: -connecting-to-device\n", stderr
);
934 _cupsLangPrintFilter(stderr
, "INFO", _("Connected to printer."));
936 fprintf(stderr
, "DEBUG: Connected to %s:%d (local port %d)...\n",
937 httpAddrString(&(addr
->addr
), addrname
, sizeof(addrname
)),
938 httpAddrPort(&(addr
->addr
)), lport
);
941 * See if the printer supports SNMP...
945 have_supplies
= !backendSNMPSupplies(snmp_fd
, &(addrlist
->addr
), NULL
,
951 * Check for side-channel requests...
954 backendCheckSideChannel(snmp_fd
, &(addrlist
->addr
));
957 * Next, open the print file and figure out its size...
963 * Use the size from the print file...
966 if (fstat(print_fd
, &filestats
))
970 perror("DEBUG: unable to stat print file");
971 return (CUPS_BACKEND_FAILED
);
974 filestats
.st_size
*= manual_copies
;
979 * Use a "very large value" for the size so that the printer will
980 * keep printing until we close the connection...
983 #ifdef _LARGEFILE_SOURCE
984 filestats
.st_size
= (size_t)(999999999999.0);
986 filestats
.st_size
= 2147483647;
987 #endif /* _LARGEFILE_SOURCE */
991 * Send a job header to the printer, specifying no banner page and
995 if (lpd_command(fd
, "\002%s\n",
996 printer
)) /* Receive print job(s) */
999 return (CUPS_BACKEND_FAILED
);
1002 if (orighost
&& _cups_strcasecmp(orighost
, "localhost"))
1003 strlcpy(localhost
, orighost
, sizeof(localhost
));
1005 httpGetHostname(NULL
, localhost
, sizeof(localhost
));
1007 snprintf(control
, sizeof(control
),
1008 "H%.31s\n" /* RFC 1179, Section 7.2 - host name <= 31 chars */
1009 "P%.31s\n" /* RFC 1179, Section 7.2 - user name <= 31 chars */
1010 "J%.99s\n", /* RFC 1179, Section 7.2 - job name <= 99 chars */
1011 localhost
, user
, title
);
1012 cptr
= control
+ strlen(control
);
1016 snprintf(cptr
, sizeof(control
) - (size_t)(cptr
- control
),
1017 "C%.31s\n" /* RFC 1179, Section 7.2 - class name <= 31 chars */
1020 cptr
+= strlen(cptr
);
1025 snprintf(cptr
, sizeof(control
) - (size_t)(cptr
- control
), "%cdfA%03d%.15s\n",
1026 format
, (int)getpid() % 1000, localhost
);
1027 cptr
+= strlen(cptr
);
1031 snprintf(cptr
, sizeof(control
) - (size_t)(cptr
- control
),
1033 "N%.131s\n", /* RFC 1179, Section 7.2 - sourcefile name <= 131 chars */
1034 (int)getpid() % 1000, localhost
, title
);
1036 fprintf(stderr
, "DEBUG: Control file is:\n%s", control
);
1038 if (order
== ORDER_CONTROL_DATA
)
1041 * Check for side-channel requests...
1044 backendCheckSideChannel(snmp_fd
, &(addr
->addr
));
1047 * Send the control file...
1050 if (lpd_command(fd
, "\002%d cfA%03.3d%.15s\n", strlen(control
),
1051 (int)getpid() % 1000, localhost
))
1055 return (CUPS_BACKEND_FAILED
);
1058 fprintf(stderr
, "DEBUG: Sending control file (%u bytes)\n",
1059 (unsigned)strlen(control
));
1061 if ((size_t)lpd_write(fd
, control
, strlen(control
) + 1) < (strlen(control
) + 1))
1063 status
= (char)errno
;
1064 perror("DEBUG: Unable to write control file");
1069 if (read(fd
, &status
, 1) < 1)
1071 _cupsLangPrintFilter(stderr
, "WARNING",
1072 _("The printer did not respond."));
1073 status
= (char)errno
;
1078 _cupsLangPrintFilter(stderr
, "ERROR",
1079 _("Remote host did not accept control file (%d)."),
1082 _cupsLangPrintFilter(stderr
, "INFO",
1083 _("Control file sent successfully."));
1091 * Check for side-channel requests...
1094 backendCheckSideChannel(snmp_fd
, &(addr
->addr
));
1097 * Send the print file...
1100 if (lpd_command(fd
, "\003" CUPS_LLFMT
" dfA%03.3d%.15s\n",
1101 CUPS_LLCAST filestats
.st_size
, (int)getpid() % 1000,
1106 return (CUPS_BACKEND_FAILED
);
1109 fprintf(stderr
, "DEBUG: Sending data file (" CUPS_LLFMT
" bytes)\n",
1110 CUPS_LLCAST filestats
.st_size
);
1113 for (copy
= 0; copy
< manual_copies
; copy
++)
1115 lseek(print_fd
, 0, SEEK_SET
);
1117 while ((nbytes
= read(print_fd
, buffer
, sizeof(buffer
))) > 0)
1119 _cupsLangPrintFilter(stderr
, "INFO",
1120 _("Spooling job, %.0f%% complete."),
1121 100.0 * tbytes
/ filestats
.st_size
);
1123 if (lpd_write(fd
, buffer
, (size_t)nbytes
) < nbytes
)
1125 perror("DEBUG: Unable to send print file to printer");
1133 if (mode
== MODE_STANDARD
)
1135 if (tbytes
< filestats
.st_size
)
1136 status
= (char)errno
;
1137 else if (lpd_write(fd
, "", 1) < 1)
1139 perror("DEBUG: Unable to send trailing nul to printer");
1140 status
= (char)errno
;
1145 * Read the status byte from the printer; if we can't read the byte
1146 * back now, we should set status to "errno", however at this point
1147 * we know the printer got the whole file and we don't necessarily
1148 * want to requeue it over and over...
1151 if (recv(fd
, &status
, 1, 0) < 1)
1153 _cupsLangPrintFilter(stderr
, "WARNING",
1154 _("The printer did not respond."));
1163 _cupsLangPrintFilter(stderr
, "ERROR",
1164 _("Remote host did not accept data file (%d)."),
1167 _cupsLangPrintFilter(stderr
, "INFO",
1168 _("Data file sent successfully."));
1171 if (status
== 0 && order
== ORDER_DATA_CONTROL
)
1174 * Check for side-channel requests...
1177 backendCheckSideChannel(snmp_fd
, &(addr
->addr
));
1180 * Send control file...
1183 if (lpd_command(fd
, "\002%d cfA%03.3d%.15s\n", strlen(control
),
1184 (int)getpid() % 1000, localhost
))
1188 return (CUPS_BACKEND_FAILED
);
1191 fprintf(stderr
, "DEBUG: Sending control file (%lu bytes)\n",
1192 (unsigned long)strlen(control
));
1194 if ((size_t)lpd_write(fd
, control
, strlen(control
) + 1) < (strlen(control
) + 1))
1196 status
= (char)errno
;
1197 perror("DEBUG: Unable to write control file");
1201 if (read(fd
, &status
, 1) < 1)
1203 _cupsLangPrintFilter(stderr
, "WARNING",
1204 _("The printer did not respond."));
1205 status
= (char)errno
;
1210 _cupsLangPrintFilter(stderr
, "ERROR",
1211 _("Remote host did not accept control file (%d)."),
1214 _cupsLangPrintFilter(stderr
, "INFO",
1215 _("Control file sent successfully."));
1218 fputs("STATE: +cups-waiting-for-job-completed\n", stderr
);
1221 * Collect the final supply levels as needed...
1225 backendSNMPSupplies(snmp_fd
, &(addr
->addr
), NULL
, NULL
);
1228 * Close the socket connection and input file...
1234 return (CUPS_BACKEND_OK
);
1237 * Waiting for a retry...
1244 * If we get here, then the job has been canceled...
1247 return (CUPS_BACKEND_FAILED
);
1252 * 'lpd_write()' - Write a buffer of data to an LPD server.
1255 static ssize_t
/* O - Number of bytes written or -1 on error */
1256 lpd_write(int lpd_fd
, /* I - LPD socket */
1257 char *buffer
, /* I - Buffer to write */
1258 size_t length
) /* I - Number of bytes to write */
1260 ssize_t bytes
, /* Number of bytes written */
1261 total
; /* Total number of bytes written */
1268 while ((bytes
= send(lpd_fd
, buffer
, length
- (size_t)total
, 0)) >= 0)
1273 if ((size_t)total
== length
)
1285 * 'sigterm_handler()' - Handle 'terminate' signals that stop the backend.
1289 sigterm_handler(int sig
) /* I - Signal */
1291 (void)sig
; /* remove compiler warnings... */