]>
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 * These coded instructions, statements, and computer programs are the
8 * property of Apple Inc. and are protected by Federal copyright
9 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
10 * "LICENSE" which should have been included with this file. If this
11 * file is missing or damaged, see the license at "http://www.cups.org/".
13 * This file is subject to the Apple OS-Developed Software exception.
17 * Include necessary headers.
20 #include <cups/http-private.h>
21 #include "backend-private.h"
23 #include <sys/types.h>
30 # include <sys/socket.h>
31 # include <netinet/in.h>
32 # include <arpa/inet.h>
36 # include <CoreFoundation/CFNumber.h>
37 # include <CoreFoundation/CFPreferences.h>
38 #endif /* __APPLE__ */
45 static char tmpfilename
[1024] = ""; /* Temporary spool file name */
46 static int abort_job
= 0; /* Non-zero if we get SIGTERM */
53 #define MODE_STANDARD 0 /* Queue a copy */
54 #define MODE_STREAM 1 /* Stream a copy */
58 * The order for control and data files in LPD requests...
61 #define ORDER_CONTROL_DATA 0 /* Control file first, then data */
62 #define ORDER_DATA_CONTROL 1 /* Data file first, then control */
69 #define RESERVE_NONE 0 /* Don't reserve a priviledged port */
70 #define RESERVE_RFC1179 1 /* Reserve port 721-731 */
71 #define RESERVE_ANY 2 /* Reserve port 1-1023 */
78 static int cups_rresvport(int *port
, int family
);
79 static int lpd_command(int lpd_fd
, char *format
, ...);
80 static int lpd_queue(const char *hostname
, http_addrlist_t
*addrlist
,
81 const char *printer
, int print_fd
, int snmp_fd
,
82 int mode
, const char *user
, const char *title
,
83 int copies
, int banner
, int format
, int order
,
84 int reserve
, int manual_copies
, int timeout
,
85 int contimeout
, const char *orighost
);
86 static ssize_t
lpd_write(int lpd_fd
, char *buffer
, size_t length
);
87 static void sigterm_handler(int sig
);
91 * 'main()' - Send a file to the printer or server.
95 * printer-uri job-id user title copies options [file]
98 int /* O - Exit status */
99 main(int argc
, /* I - Number of command-line arguments (6 or 7) */
100 char *argv
[]) /* I - Command-line arguments */
102 const char *device_uri
; /* Device URI */
103 char scheme
[255], /* Scheme in URI */
104 hostname
[1024], /* Hostname */
105 username
[255], /* Username info */
106 resource
[1024], /* Resource info (printer name) */
107 *options
, /* Pointer to options */
108 *name
, /* Name of option */
109 *value
, /* Value of option */
110 sep
, /* Separator character */
111 *filename
, /* File to print */
112 title
[256]; /* Title string */
113 int port
; /* Port number */
114 http_addrlist_t
*addrlist
; /* List of addresses for printer */
115 int snmp_enabled
= 1; /* Is SNMP enabled? */
116 int snmp_fd
; /* SNMP socket */
117 int fd
; /* Print file */
118 int status
; /* Status of LPD job */
119 int mode
; /* Print mode */
120 int banner
; /* Print banner page? */
121 int format
; /* Print format */
122 int order
; /* Order of control/data files */
123 int reserve
; /* Reserve priviledged port? */
124 int sanitize_title
; /* Sanitize title string? */
125 int manual_copies
, /* Do manual copies? */
126 timeout
, /* Timeout */
127 contimeout
, /* Connection timeout */
128 copies
; /* Number of copies */
129 ssize_t bytes
= 0; /* Initial bytes read */
130 char buffer
[16384]; /* Initial print buffer */
131 #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
132 struct sigaction action
; /* Actions for POSIX signals */
133 #endif /* HAVE_SIGACTION && !HAVE_SIGSET */
134 int num_jobopts
; /* Number of job options */
135 cups_option_t
*jobopts
= NULL
; /* Job options */
139 * Make sure status messages are not buffered...
142 setbuf(stderr
, NULL
);
145 * Ignore SIGPIPE and catch SIGTERM signals...
149 sigset(SIGPIPE
, SIG_IGN
);
150 sigset(SIGTERM
, sigterm_handler
);
151 #elif defined(HAVE_SIGACTION)
152 memset(&action
, 0, sizeof(action
));
153 action
.sa_handler
= SIG_IGN
;
154 sigaction(SIGPIPE
, &action
, NULL
);
156 sigemptyset(&action
.sa_mask
);
157 sigaddset(&action
.sa_mask
, SIGTERM
);
158 action
.sa_handler
= sigterm_handler
;
159 sigaction(SIGTERM
, &action
, NULL
);
161 signal(SIGPIPE
, SIG_IGN
);
162 signal(SIGTERM
, sigterm_handler
);
163 #endif /* HAVE_SIGSET */
166 * Check command-line...
171 printf("network lpd \"Unknown\" \"%s\"\n",
172 _cupsLangString(cupsLangDefault(), _("LPD/LPR Host or Printer")));
173 return (CUPS_BACKEND_OK
);
175 else if (argc
< 6 || argc
> 7)
177 _cupsLangPrintf(stderr
,
178 _("Usage: %s job-id user title copies options [file]"),
180 return (CUPS_BACKEND_FAILED
);
183 num_jobopts
= cupsParseOptions(argv
[5], 0, &jobopts
);
186 * Extract the hostname and printer name from the URI...
189 while ((device_uri
= cupsBackendDeviceURI(argv
)) == NULL
)
191 _cupsLangPrintFilter(stderr
, "INFO", _("Unable to locate printer."));
194 if (getenv("CLASS") != NULL
)
195 return (CUPS_BACKEND_FAILED
);
198 httpSeparateURI(HTTP_URI_CODING_ALL
, device_uri
, scheme
, sizeof(scheme
),
199 username
, sizeof(username
), hostname
, sizeof(hostname
), &port
,
200 resource
, sizeof(resource
));
203 port
= 515; /* Default to port 515 */
208 * If no username is in the device URI, then use the print job user...
211 strlcpy(username
, argv
[2], sizeof(username
));
215 * See if there are any options...
218 mode
= MODE_STANDARD
;
221 order
= ORDER_CONTROL_DATA
;
222 reserve
= RESERVE_ANY
;
225 contimeout
= 7 * 24 * 60 * 60;
229 * We want to pass UTF-8 characters by default, not re-map them (3071945)
235 * Otherwise we want to re-map UTF-8 to "safe" characters by default...
239 #endif /* __APPLE__ */
241 if ((options
= strchr(resource
, '?')) != NULL
)
244 * Yup, terminate the device name string and move to the first
245 * character of the options...
262 while (*options
&& *options
!= '=' && *options
!= '+' && *options
!= '&')
265 if ((sep
= *options
) != '\0')
276 while (*options
&& *options
!= '+' && *options
!= '&')
286 * Process the option...
289 if (!_cups_strcasecmp(name
, "banner"))
295 banner
= !value
[0] || !_cups_strcasecmp(value
, "on") ||
296 !_cups_strcasecmp(value
, "yes") || !_cups_strcasecmp(value
, "true");
298 else if (!_cups_strcasecmp(name
, "format") && value
[0])
301 * Set output format...
304 if (strchr("cdfglnoprtv", value
[0]))
307 _cupsLangPrintFilter(stderr
, "ERROR",
308 _("Unknown format character: \"%c\"."),
311 else if (!_cups_strcasecmp(name
, "mode") && value
[0])
314 * Set control/data order...
317 if (!_cups_strcasecmp(value
, "standard"))
318 mode
= MODE_STANDARD
;
319 else if (!_cups_strcasecmp(value
, "stream"))
322 _cupsLangPrintFilter(stderr
, "ERROR",
323 _("Unknown print mode: \"%s\"."), value
);
325 else if (!_cups_strcasecmp(name
, "order") && value
[0])
328 * Set control/data order...
331 if (!_cups_strcasecmp(value
, "control,data"))
332 order
= ORDER_CONTROL_DATA
;
333 else if (!_cups_strcasecmp(value
, "data,control"))
334 order
= ORDER_DATA_CONTROL
;
336 _cupsLangPrintFilter(stderr
, "ERROR",
337 _("Unknown file order: \"%s\"."), value
);
339 else if (!_cups_strcasecmp(name
, "reserve"))
342 * Set port reservation mode...
345 if (!value
[0] || !_cups_strcasecmp(value
, "on") ||
346 !_cups_strcasecmp(value
, "yes") ||
347 !_cups_strcasecmp(value
, "true") ||
348 !_cups_strcasecmp(value
, "rfc1179"))
349 reserve
= RESERVE_RFC1179
;
350 else if (!_cups_strcasecmp(value
, "any"))
351 reserve
= RESERVE_ANY
;
353 reserve
= RESERVE_NONE
;
355 else if (!_cups_strcasecmp(name
, "manual_copies"))
358 * Set manual copies...
361 manual_copies
= !value
[0] || !_cups_strcasecmp(value
, "on") ||
362 !_cups_strcasecmp(value
, "yes") ||
363 !_cups_strcasecmp(value
, "true");
365 else if (!_cups_strcasecmp(name
, "sanitize_title"))
368 * Set sanitize title...
371 sanitize_title
= !value
[0] || !_cups_strcasecmp(value
, "on") ||
372 !_cups_strcasecmp(value
, "yes") ||
373 !_cups_strcasecmp(value
, "true");
375 else if (!_cups_strcasecmp(name
, "snmp"))
378 * Enable/disable SNMP stuff...
381 snmp_enabled
= !value
[0] || !_cups_strcasecmp(value
, "on") ||
382 !_cups_strcasecmp(value
, "yes") ||
383 !_cups_strcasecmp(value
, "true");
385 else if (!_cups_strcasecmp(name
, "timeout"))
392 timeout
= atoi(value
);
394 else if (!_cups_strcasecmp(name
, "contimeout"))
397 * Set the connection timeout...
401 contimeout
= atoi(value
);
406 if (mode
== MODE_STREAM
)
407 order
= ORDER_CONTROL_DATA
;
410 * Find the printer...
413 addrlist
= backendLookup(hostname
, port
, NULL
);
416 * See if the printer supports SNMP...
420 snmp_fd
= _cupsSNMPOpen(addrlist
->addr
.addr
.sa_family
);
425 * Wait for data from the filter...
430 if (!backendWaitLoop(snmp_fd
, &(addrlist
->addr
), 0, backendNetworkSideCB
))
431 return (CUPS_BACKEND_OK
);
432 else if (mode
== MODE_STANDARD
&&
433 (bytes
= read(0, buffer
, sizeof(buffer
))) <= 0)
434 return (CUPS_BACKEND_OK
);
438 * If we have 7 arguments, print the file named on the command-line.
439 * Otherwise, copy stdin to a temporary file and print the temporary
443 if (argc
== 6 && mode
== MODE_STANDARD
)
446 * Copy stdin to a temporary file...
449 if ((fd
= cupsTempFd(tmpfilename
, sizeof(tmpfilename
))) < 0)
451 perror("DEBUG: Unable to create temporary file");
452 return (CUPS_BACKEND_FAILED
);
455 _cupsLangPrintFilter(stderr
, "INFO", _("Copying print data."));
458 write(fd
, buffer
, (size_t)bytes
);
460 backendRunLoop(-1, fd
, snmp_fd
, &(addrlist
->addr
), 0, 0,
461 backendNetworkSideCB
);
466 * Stream from stdin...
475 fd
= open(filename
, O_RDONLY
);
479 _cupsLangPrintError("ERROR", _("Unable to open print file"));
480 return (CUPS_BACKEND_FAILED
);
485 * Sanitize the document title...
488 strlcpy(title
, argv
[3], sizeof(title
));
493 * Sanitize the title string so that we don't cause problems on
499 for (ptr
= title
; *ptr
; ptr
++)
500 if (!isalnum(*ptr
& 255) && !isspace(*ptr
& 255))
512 manual_copies
= atoi(argv
[4]);
518 copies
= atoi(argv
[4]);
521 status
= lpd_queue(hostname
, addrlist
, resource
+ 1, fd
, snmp_fd
, mode
,
522 username
, title
, copies
, banner
, format
, order
, reserve
,
523 manual_copies
, timeout
, contimeout
,
524 cupsGetOption("job-originating-host-name", num_jobopts
,
528 fprintf(stderr
, "PAGE: 1 %d\n", atoi(argv
[4]));
531 status
= lpd_queue(hostname
, addrlist
, resource
+ 1, fd
, snmp_fd
, mode
,
532 username
, title
, 1, banner
, format
, order
, reserve
, 1,
534 cupsGetOption("job-originating-host-name", num_jobopts
,
538 * Remove the temporary file if necessary...
548 _cupsSNMPClose(snmp_fd
);
551 * Return the queue status...
559 * 'cups_rresvport()' - A simple implementation of rresvport_af().
562 static int /* O - Socket or -1 on error */
563 cups_rresvport(int *port
, /* IO - Port number to bind to */
564 int family
) /* I - Address family */
566 http_addr_t addr
; /* Socket address */
567 int fd
; /* Socket file descriptor */
571 * Try to create an IPv4 socket...
574 if ((fd
= socket(family
, SOCK_STREAM
, 0)) < 0)
578 * Initialize the address buffer...
581 memset(&addr
, 0, sizeof(addr
));
582 addr
.addr
.sa_family
= (sa_family_t
)family
;
585 * Try to bind the socket to a reserved port...
591 * Set the port number...
594 _httpAddrSetPort(&addr
, *port
);
597 * Try binding the port to the socket; return if all is OK...
600 if (!bind(fd
, (struct sockaddr
*)&addr
, (socklen_t
)httpAddrLength(&addr
)))
604 * Stop if we have any error other than "address already in use"...
607 if (errno
!= EADDRINUSE
)
609 httpAddrClose(NULL
, fd
);
615 * Try the next port...
622 * Wasn't able to bind to a reserved port, so close the socket and return
637 * 'lpd_command()' - Send an LPR command sequence and wait for a reply.
640 static int /* O - Status of command */
641 lpd_command(int fd
, /* I - Socket connection to LPD host */
642 char *format
, /* I - printf()-style format string */
643 ...) /* I - Additional args as necessary */
645 va_list ap
; /* Argument pointer */
646 char buf
[1024]; /* Output buffer */
647 ssize_t bytes
; /* Number of bytes to output */
648 char status
; /* Status from command */
652 * Don't try to send commands if the job has been canceled...
659 * Format the string...
662 va_start(ap
, format
);
663 bytes
= vsnprintf(buf
, sizeof(buf
), format
, ap
);
666 fprintf(stderr
, "DEBUG: lpd_command %2.2x %s", buf
[0], buf
+ 1);
669 * Send the command...
672 fprintf(stderr
, "DEBUG: Sending command string (" CUPS_LLFMT
" bytes)...\n", CUPS_LLCAST bytes
);
674 if (lpd_write(fd
, buf
, (size_t)bytes
) < bytes
)
676 perror("DEBUG: Unable to send LPD command");
681 * Read back the status from the command and return it...
684 fputs("DEBUG: Reading command status...\n", stderr
);
686 if (recv(fd
, &status
, 1, 0) < 1)
688 _cupsLangPrintFilter(stderr
, "WARNING", _("The printer did not respond."));
689 status
= (char)errno
;
692 fprintf(stderr
, "DEBUG: lpd_command returning %d\n", status
);
699 * 'lpd_queue()' - Queue a file using the Line Printer Daemon protocol.
702 static int /* O - Zero on success, non-zero on failure */
703 lpd_queue(const char *hostname
, /* I - Host to connect to */
704 http_addrlist_t
*addrlist
, /* I - List of host addresses */
705 const char *printer
, /* I - Printer/queue name */
706 int print_fd
, /* I - File to print */
707 int snmp_fd
, /* I - SNMP socket */
708 int mode
, /* I - Print mode */
709 const char *user
, /* I - Requesting user */
710 const char *title
, /* I - Job title */
711 int copies
, /* I - Number of copies */
712 int banner
, /* I - Print LPD banner? */
713 int format
, /* I - Format specifier */
714 int order
, /* I - Order of data/control files */
715 int reserve
, /* I - Reserve ports? */
716 int manual_copies
,/* I - Do copies by hand... */
717 int timeout
, /* I - Timeout... */
718 int contimeout
, /* I - Connection timeout */
719 const char *orighost
) /* I - job-originating-host-name */
721 char localhost
[255]; /* Local host name */
722 int error
; /* Error number */
723 struct stat filestats
; /* File statistics */
724 int lport
; /* LPD connection local port */
725 int fd
; /* LPD socket */
726 char control
[10240], /* LPD control 'file' */
727 *cptr
; /* Pointer into control file string */
728 char status
; /* Status byte from command */
729 int delay
; /* Delay for retries... */
730 char addrname
[256]; /* Address name */
731 http_addrlist_t
*addr
; /* Socket address */
732 int have_supplies
; /* Printer supports supply levels? */
733 int copy
; /* Copies written */
734 time_t start_time
; /* Time of first connect */
735 ssize_t nbytes
; /* Number of bytes written */
736 off_t tbytes
; /* Total bytes written */
737 char buffer
[32768]; /* Output buffer */
739 DWORD tv
; /* Timeout in milliseconds */
741 struct timeval tv
; /* Timeout in secs and usecs */
746 * Remember when we started trying to connect to the printer...
749 start_time
= time(NULL
);
752 * Loop forever trying to print the file...
758 * First try to reserve a port for this connection...
761 fprintf(stderr
, "DEBUG: Connecting to %s:%d for printer %s\n", hostname
,
762 httpAddrPort(&(addrlist
->addr
)), printer
);
763 _cupsLangPrintFilter(stderr
, "INFO", _("Connecting to printer."));
765 for (lport
= reserve
== RESERVE_RFC1179
? 732 : 1024, addr
= addrlist
,
770 * Stop if this job has been canceled...
774 return (CUPS_BACKEND_FAILED
);
777 * Choose the next priviledged port...
785 if (lport
< 721 && reserve
== RESERVE_RFC1179
)
791 if (geteuid() || !reserve
)
793 if (getuid() || !reserve
)
794 #endif /* HAVE_GETEUID */
797 * Just create a regular socket...
800 if ((fd
= socket(addr
->addr
.addr
.sa_family
, SOCK_STREAM
, 0)) < 0)
802 perror("DEBUG: Unable to create socket");
813 * We're running as root and want to comply with RFC 1179. Reserve a
814 * priviledged lport between 721 and 731...
817 if ((fd
= cups_rresvport(&lport
, addr
->addr
.addr
.sa_family
)) < 0)
819 perror("DEBUG: Unable to reserve port");
827 * Connect to the printer or server...
834 return (CUPS_BACKEND_FAILED
);
837 if (!connect(fd
, &(addr
->addr
.addr
), (socklen_t
)httpAddrLength(&(addr
->addr
))))
846 if (getenv("CLASS") != NULL
)
849 * If the CLASS environment variable is set, the job was submitted
850 * to a class and not to a specific queue. In this case, we want
851 * to abort immediately so that the job can be requeued on the next
852 * available printer in the class.
855 _cupsLangPrintFilter(stderr
, "INFO",
856 _("Unable to contact printer, queuing on next "
857 "printer in class."));
860 * Sleep 5 seconds to keep the job from requeuing too rapidly...
865 return (CUPS_BACKEND_FAILED
);
868 fprintf(stderr
, "DEBUG: Connection error: %s\n", strerror(error
));
870 if (errno
== ECONNREFUSED
|| errno
== EHOSTDOWN
|| errno
== EHOSTUNREACH
|| errno
== ETIMEDOUT
|| errno
== ENOTCONN
)
872 if (contimeout
&& (time(NULL
) - start_time
) > contimeout
)
874 _cupsLangPrintFilter(stderr
, "ERROR",
875 _("The printer is not responding."));
876 return (CUPS_BACKEND_FAILED
);
882 _cupsLangPrintFilter(stderr
, "WARNING",
883 _("The printer may not exist or "
884 "is unavailable at this time."));
889 _cupsLangPrintFilter(stderr
, "WARNING",
890 _("The printer is unreachable at "
895 _cupsLangPrintFilter(stderr
, "WARNING",
896 _("The printer is in use."));
900 sleep((unsigned)delay
);
905 else if (error
== EADDRINUSE
)
908 * Try on another port...
915 _cupsLangPrintFilter(stderr
, "ERROR",
916 _("The printer is not responding."));
926 tv
= (DWORD
)(timeout
* 1000);
928 setsockopt(fd
, SOL_SOCKET
, SO_RCVTIMEO
, (char *)&tv
, sizeof(tv
));
929 setsockopt(fd
, SOL_SOCKET
, SO_SNDTIMEO
, (char *)&tv
, sizeof(tv
));
934 setsockopt(fd
, SOL_SOCKET
, SO_RCVTIMEO
, &tv
, sizeof(tv
));
935 setsockopt(fd
, SOL_SOCKET
, SO_SNDTIMEO
, &tv
, sizeof(tv
));
938 fputs("STATE: -connecting-to-device\n", stderr
);
939 _cupsLangPrintFilter(stderr
, "INFO", _("Connected to printer."));
941 fprintf(stderr
, "DEBUG: Connected to %s:%d (local port %d)...\n",
942 httpAddrString(&(addr
->addr
), addrname
, sizeof(addrname
)),
943 httpAddrPort(&(addr
->addr
)), lport
);
946 * See if the printer supports SNMP...
950 have_supplies
= !backendSNMPSupplies(snmp_fd
, &(addrlist
->addr
), NULL
,
956 * Check for side-channel requests...
959 backendCheckSideChannel(snmp_fd
, &(addrlist
->addr
));
962 * Next, open the print file and figure out its size...
968 * Use the size from the print file...
971 if (fstat(print_fd
, &filestats
))
975 perror("DEBUG: unable to stat print file");
976 return (CUPS_BACKEND_FAILED
);
979 filestats
.st_size
*= manual_copies
;
984 * Use a "very large value" for the size so that the printer will
985 * keep printing until we close the connection...
988 #ifdef _LARGEFILE_SOURCE
989 filestats
.st_size
= (size_t)(999999999999.0);
991 filestats
.st_size
= 2147483647;
992 #endif /* _LARGEFILE_SOURCE */
996 * Send a job header to the printer, specifying no banner page and
1000 if (lpd_command(fd
, "\002%s\n",
1001 printer
)) /* Receive print job(s) */
1004 return (CUPS_BACKEND_FAILED
);
1007 if (orighost
&& _cups_strcasecmp(orighost
, "localhost"))
1008 strlcpy(localhost
, orighost
, sizeof(localhost
));
1010 httpGetHostname(NULL
, localhost
, sizeof(localhost
));
1012 snprintf(control
, sizeof(control
),
1013 "H%.31s\n" /* RFC 1179, Section 7.2 - host name <= 31 chars */
1014 "P%.31s\n" /* RFC 1179, Section 7.2 - user name <= 31 chars */
1015 "J%.99s\n", /* RFC 1179, Section 7.2 - job name <= 99 chars */
1016 localhost
, user
, title
);
1017 cptr
= control
+ strlen(control
);
1021 snprintf(cptr
, sizeof(control
) - (size_t)(cptr
- control
),
1022 "C%.31s\n" /* RFC 1179, Section 7.2 - class name <= 31 chars */
1025 cptr
+= strlen(cptr
);
1030 snprintf(cptr
, sizeof(control
) - (size_t)(cptr
- control
), "%cdfA%03d%.15s\n",
1031 format
, (int)getpid() % 1000, localhost
);
1032 cptr
+= strlen(cptr
);
1036 snprintf(cptr
, sizeof(control
) - (size_t)(cptr
- control
),
1038 "N%.131s\n", /* RFC 1179, Section 7.2 - sourcefile name <= 131 chars */
1039 (int)getpid() % 1000, localhost
, title
);
1041 fprintf(stderr
, "DEBUG: Control file is:\n%s", control
);
1043 if (order
== ORDER_CONTROL_DATA
)
1046 * Check for side-channel requests...
1049 backendCheckSideChannel(snmp_fd
, &(addr
->addr
));
1052 * Send the control file...
1055 if (lpd_command(fd
, "\002%d cfA%03.3d%.15s\n", strlen(control
),
1056 (int)getpid() % 1000, localhost
))
1060 return (CUPS_BACKEND_FAILED
);
1063 fprintf(stderr
, "DEBUG: Sending control file (%u bytes)\n",
1064 (unsigned)strlen(control
));
1066 if ((size_t)lpd_write(fd
, control
, strlen(control
) + 1) < (strlen(control
) + 1))
1068 status
= (char)errno
;
1069 perror("DEBUG: Unable to write control file");
1074 if (read(fd
, &status
, 1) < 1)
1076 _cupsLangPrintFilter(stderr
, "WARNING",
1077 _("The printer did not respond."));
1078 status
= (char)errno
;
1083 _cupsLangPrintFilter(stderr
, "ERROR",
1084 _("Remote host did not accept control file (%d)."),
1087 _cupsLangPrintFilter(stderr
, "INFO",
1088 _("Control file sent successfully."));
1096 * Check for side-channel requests...
1099 backendCheckSideChannel(snmp_fd
, &(addr
->addr
));
1102 * Send the print file...
1105 if (lpd_command(fd
, "\003" CUPS_LLFMT
" dfA%03.3d%.15s\n",
1106 CUPS_LLCAST filestats
.st_size
, (int)getpid() % 1000,
1111 return (CUPS_BACKEND_FAILED
);
1114 fprintf(stderr
, "DEBUG: Sending data file (" CUPS_LLFMT
" bytes)\n",
1115 CUPS_LLCAST filestats
.st_size
);
1118 for (copy
= 0; copy
< manual_copies
; copy
++)
1120 lseek(print_fd
, 0, SEEK_SET
);
1122 while ((nbytes
= read(print_fd
, buffer
, sizeof(buffer
))) > 0)
1124 _cupsLangPrintFilter(stderr
, "INFO",
1125 _("Spooling job, %.0f%% complete."),
1126 100.0 * tbytes
/ filestats
.st_size
);
1128 if (lpd_write(fd
, buffer
, (size_t)nbytes
) < nbytes
)
1130 perror("DEBUG: Unable to send print file to printer");
1138 if (mode
== MODE_STANDARD
)
1140 if (tbytes
< filestats
.st_size
)
1141 status
= (char)errno
;
1142 else if (lpd_write(fd
, "", 1) < 1)
1144 perror("DEBUG: Unable to send trailing nul to printer");
1145 status
= (char)errno
;
1150 * Read the status byte from the printer; if we can't read the byte
1151 * back now, we should set status to "errno", however at this point
1152 * we know the printer got the whole file and we don't necessarily
1153 * want to requeue it over and over...
1156 if (recv(fd
, &status
, 1, 0) < 1)
1158 _cupsLangPrintFilter(stderr
, "WARNING",
1159 _("The printer did not respond."));
1168 _cupsLangPrintFilter(stderr
, "ERROR",
1169 _("Remote host did not accept data file (%d)."),
1172 _cupsLangPrintFilter(stderr
, "INFO",
1173 _("Data file sent successfully."));
1176 if (status
== 0 && order
== ORDER_DATA_CONTROL
)
1179 * Check for side-channel requests...
1182 backendCheckSideChannel(snmp_fd
, &(addr
->addr
));
1185 * Send control file...
1188 if (lpd_command(fd
, "\002%d cfA%03.3d%.15s\n", strlen(control
),
1189 (int)getpid() % 1000, localhost
))
1193 return (CUPS_BACKEND_FAILED
);
1196 fprintf(stderr
, "DEBUG: Sending control file (%lu bytes)\n",
1197 (unsigned long)strlen(control
));
1199 if ((size_t)lpd_write(fd
, control
, strlen(control
) + 1) < (strlen(control
) + 1))
1201 status
= (char)errno
;
1202 perror("DEBUG: Unable to write control file");
1206 if (read(fd
, &status
, 1) < 1)
1208 _cupsLangPrintFilter(stderr
, "WARNING",
1209 _("The printer did not respond."));
1210 status
= (char)errno
;
1215 _cupsLangPrintFilter(stderr
, "ERROR",
1216 _("Remote host did not accept control file (%d)."),
1219 _cupsLangPrintFilter(stderr
, "INFO",
1220 _("Control file sent successfully."));
1223 fputs("STATE: +cups-waiting-for-job-completed\n", stderr
);
1226 * Collect the final supply levels as needed...
1230 backendSNMPSupplies(snmp_fd
, &(addr
->addr
), NULL
, NULL
);
1233 * Close the socket connection and input file...
1239 return (CUPS_BACKEND_OK
);
1242 * Waiting for a retry...
1249 * If we get here, then the job has been canceled...
1252 return (CUPS_BACKEND_FAILED
);
1257 * 'lpd_write()' - Write a buffer of data to an LPD server.
1260 static ssize_t
/* O - Number of bytes written or -1 on error */
1261 lpd_write(int lpd_fd
, /* I - LPD socket */
1262 char *buffer
, /* I - Buffer to write */
1263 size_t length
) /* I - Number of bytes to write */
1265 ssize_t bytes
, /* Number of bytes written */
1266 total
; /* Total number of bytes written */
1273 while ((bytes
= send(lpd_fd
, buffer
, length
- (size_t)total
, 0)) >= 0)
1278 if ((size_t)total
== length
)
1290 * 'sigterm_handler()' - Handle 'terminate' signals that stop the backend.
1294 sigterm_handler(int sig
) /* I - Signal */
1296 (void)sig
; /* remove compiler warnings... */