]>
git.ipfire.org Git - thirdparty/cups.git/blob - backend/lpd.c
2 * Line Printer Daemon backend for CUPS.
4 * Copyright © 2007-2019 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
, const char *printer
, int print_fd
, int snmp_fd
, int mode
, const char *user
, const char *title
, int copies
, int banner
, int format
, int order
, int reserve
, int manual_copies
, int timeout
, int contimeout
, const char *orighost
) _CUPS_NONNULL((1,2,3,7,8,17));
76 static ssize_t
lpd_write(int lpd_fd
, char *buffer
, size_t length
);
77 static void sigterm_handler(int sig
);
81 * 'main()' - Send a file to the printer or server.
85 * printer-uri job-id user title copies options [file]
88 int /* O - Exit status */
89 main(int argc
, /* I - Number of command-line arguments (6 or 7) */
90 char *argv
[]) /* I - Command-line arguments */
92 const char *device_uri
; /* Device URI */
93 char scheme
[255], /* Scheme in URI */
94 hostname
[1024], /* Hostname */
95 username
[255], /* Username info */
96 resource
[1024], /* Resource info (printer name) */
97 *options
, /* Pointer to options */
98 *name
, /* Name of option */
99 *value
, /* Value of option */
100 sep
, /* Separator character */
101 *filename
, /* File to print */
102 title
[256]; /* Title string */
103 int port
; /* Port number */
104 http_addrlist_t
*addrlist
; /* List of addresses for printer */
105 int snmp_enabled
= 1; /* Is SNMP enabled? */
106 int snmp_fd
; /* SNMP socket */
107 int fd
; /* Print file */
108 int status
; /* Status of LPD job */
109 int mode
; /* Print mode */
110 int banner
; /* Print banner page? */
111 int format
; /* Print format */
112 int order
; /* Order of control/data files */
113 int reserve
; /* Reserve priviledged port? */
114 int sanitize_title
; /* Sanitize title string? */
115 int manual_copies
, /* Do manual copies? */
116 timeout
, /* Timeout */
117 contimeout
, /* Connection timeout */
118 copies
; /* Number of copies */
119 ssize_t bytes
= 0; /* Initial bytes read */
120 char buffer
[16384]; /* Initial print buffer */
121 #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
122 struct sigaction action
; /* Actions for POSIX signals */
123 #endif /* HAVE_SIGACTION && !HAVE_SIGSET */
124 int num_jobopts
; /* Number of job options */
125 cups_option_t
*jobopts
= NULL
; /* Job options */
129 * Make sure status messages are not buffered...
132 setbuf(stderr
, NULL
);
135 * Ignore SIGPIPE and catch SIGTERM signals...
139 sigset(SIGPIPE
, SIG_IGN
);
140 sigset(SIGTERM
, sigterm_handler
);
141 #elif defined(HAVE_SIGACTION)
142 memset(&action
, 0, sizeof(action
));
143 action
.sa_handler
= SIG_IGN
;
144 sigaction(SIGPIPE
, &action
, NULL
);
146 sigemptyset(&action
.sa_mask
);
147 sigaddset(&action
.sa_mask
, SIGTERM
);
148 action
.sa_handler
= sigterm_handler
;
149 sigaction(SIGTERM
, &action
, NULL
);
151 signal(SIGPIPE
, SIG_IGN
);
152 signal(SIGTERM
, sigterm_handler
);
153 #endif /* HAVE_SIGSET */
156 * Check command-line...
161 printf("network lpd \"Unknown\" \"%s\"\n",
162 _cupsLangString(cupsLangDefault(), _("LPD/LPR Host or Printer")));
163 return (CUPS_BACKEND_OK
);
165 else if (argc
< 6 || argc
> 7)
167 _cupsLangPrintf(stderr
,
168 _("Usage: %s job-id user title copies options [file]"),
170 return (CUPS_BACKEND_FAILED
);
173 num_jobopts
= cupsParseOptions(argv
[5], 0, &jobopts
);
176 * Extract the hostname and printer name from the URI...
179 while ((device_uri
= cupsBackendDeviceURI(argv
)) == NULL
)
181 _cupsLangPrintFilter(stderr
, "INFO", _("Unable to locate printer."));
184 if (getenv("CLASS") != NULL
)
185 return (CUPS_BACKEND_FAILED
);
188 httpSeparateURI(HTTP_URI_CODING_ALL
, device_uri
, scheme
, sizeof(scheme
),
189 username
, sizeof(username
), hostname
, sizeof(hostname
), &port
,
190 resource
, sizeof(resource
));
193 port
= 515; /* Default to port 515 */
198 * If no username is in the device URI, then use the print job user...
201 strlcpy(username
, argv
[2], sizeof(username
));
205 * See if there are any options...
208 mode
= MODE_STANDARD
;
211 order
= ORDER_CONTROL_DATA
;
212 reserve
= RESERVE_ANY
;
215 contimeout
= 7 * 24 * 60 * 60;
219 * We want to pass UTF-8 characters by default, not re-map them (3071945)
225 * Otherwise we want to re-map UTF-8 to "safe" characters by default...
229 #endif /* __APPLE__ */
231 if ((options
= strchr(resource
, '?')) != NULL
)
234 * Yup, terminate the device name string and move to the first
235 * character of the options...
252 while (*options
&& *options
!= '=' && *options
!= '+' && *options
!= '&')
255 if ((sep
= *options
) != '\0')
266 while (*options
&& *options
!= '+' && *options
!= '&')
276 * Process the option...
279 if (!_cups_strcasecmp(name
, "banner"))
285 banner
= !value
[0] || !_cups_strcasecmp(value
, "on") ||
286 !_cups_strcasecmp(value
, "yes") || !_cups_strcasecmp(value
, "true");
288 else if (!_cups_strcasecmp(name
, "format") && value
[0])
291 * Set output format...
294 if (strchr("cdfglnoprtv", value
[0]))
297 _cupsLangPrintFilter(stderr
, "ERROR",
298 _("Unknown format character: \"%c\"."),
301 else if (!_cups_strcasecmp(name
, "mode") && value
[0])
304 * Set control/data order...
307 if (!_cups_strcasecmp(value
, "standard"))
308 mode
= MODE_STANDARD
;
309 else if (!_cups_strcasecmp(value
, "stream"))
312 _cupsLangPrintFilter(stderr
, "ERROR",
313 _("Unknown print mode: \"%s\"."), value
);
315 else if (!_cups_strcasecmp(name
, "order") && value
[0])
318 * Set control/data order...
321 if (!_cups_strcasecmp(value
, "control,data"))
322 order
= ORDER_CONTROL_DATA
;
323 else if (!_cups_strcasecmp(value
, "data,control"))
324 order
= ORDER_DATA_CONTROL
;
326 _cupsLangPrintFilter(stderr
, "ERROR",
327 _("Unknown file order: \"%s\"."), value
);
329 else if (!_cups_strcasecmp(name
, "reserve"))
332 * Set port reservation mode...
335 if (!value
[0] || !_cups_strcasecmp(value
, "on") ||
336 !_cups_strcasecmp(value
, "yes") ||
337 !_cups_strcasecmp(value
, "true") ||
338 !_cups_strcasecmp(value
, "rfc1179"))
339 reserve
= RESERVE_RFC1179
;
340 else if (!_cups_strcasecmp(value
, "any"))
341 reserve
= RESERVE_ANY
;
343 reserve
= RESERVE_NONE
;
345 else if (!_cups_strcasecmp(name
, "manual_copies"))
348 * Set manual copies...
351 manual_copies
= !value
[0] || !_cups_strcasecmp(value
, "on") ||
352 !_cups_strcasecmp(value
, "yes") ||
353 !_cups_strcasecmp(value
, "true");
355 else if (!_cups_strcasecmp(name
, "sanitize_title"))
358 * Set sanitize title...
361 sanitize_title
= !value
[0] || !_cups_strcasecmp(value
, "on") ||
362 !_cups_strcasecmp(value
, "yes") ||
363 !_cups_strcasecmp(value
, "true");
365 else if (!_cups_strcasecmp(name
, "snmp"))
368 * Enable/disable SNMP stuff...
371 snmp_enabled
= !value
[0] || !_cups_strcasecmp(value
, "on") ||
372 !_cups_strcasecmp(value
, "yes") ||
373 !_cups_strcasecmp(value
, "true");
375 else if (!_cups_strcasecmp(name
, "timeout"))
382 timeout
= atoi(value
);
384 else if (!_cups_strcasecmp(name
, "contimeout"))
387 * Set the connection timeout...
391 contimeout
= atoi(value
);
396 if (mode
== MODE_STREAM
)
397 order
= ORDER_CONTROL_DATA
;
400 * Find the printer...
403 addrlist
= backendLookup(hostname
, port
, NULL
);
406 * See if the printer supports SNMP...
410 snmp_fd
= _cupsSNMPOpen(addrlist
->addr
.addr
.sa_family
);
415 * Wait for data from the filter...
420 if (!backendWaitLoop(snmp_fd
, &(addrlist
->addr
), 0, backendNetworkSideCB
))
421 return (CUPS_BACKEND_OK
);
422 else if (mode
== MODE_STANDARD
&&
423 (bytes
= read(0, buffer
, sizeof(buffer
))) <= 0)
424 return (CUPS_BACKEND_OK
);
428 * If we have 7 arguments, print the file named on the command-line.
429 * Otherwise, copy stdin to a temporary file and print the temporary
433 if (argc
== 6 && mode
== MODE_STANDARD
)
436 * Copy stdin to a temporary file...
439 if ((fd
= cupsTempFd(tmpfilename
, sizeof(tmpfilename
))) < 0)
441 perror("DEBUG: Unable to create temporary file");
442 return (CUPS_BACKEND_FAILED
);
445 _cupsLangPrintFilter(stderr
, "INFO", _("Copying print data."));
448 write(fd
, buffer
, (size_t)bytes
);
450 backendRunLoop(-1, fd
, snmp_fd
, &(addrlist
->addr
), 0, 0,
451 backendNetworkSideCB
);
456 * Stream from stdin...
465 fd
= open(filename
, O_RDONLY
);
469 _cupsLangPrintError("ERROR", _("Unable to open print file"));
470 return (CUPS_BACKEND_FAILED
);
475 * Sanitize the document title...
478 strlcpy(title
, argv
[3], sizeof(title
));
483 * Sanitize the title string so that we don't cause problems on
489 for (ptr
= title
; *ptr
; ptr
++)
490 if (!isalnum(*ptr
& 255) && !isspace(*ptr
& 255))
502 manual_copies
= atoi(argv
[4]);
508 copies
= atoi(argv
[4]);
511 status
= lpd_queue(hostname
, addrlist
, resource
+ 1, fd
, snmp_fd
, mode
,
512 username
, title
, copies
, banner
, format
, order
, reserve
,
513 manual_copies
, timeout
, contimeout
,
514 cupsGetOption("job-originating-host-name", num_jobopts
,
518 fprintf(stderr
, "PAGE: 1 %d\n", atoi(argv
[4]));
521 status
= lpd_queue(hostname
, addrlist
, resource
+ 1, fd
, snmp_fd
, mode
,
522 username
, title
, 1, banner
, format
, order
, reserve
, 1,
524 cupsGetOption("job-originating-host-name", num_jobopts
,
528 * Remove the temporary file if necessary...
538 _cupsSNMPClose(snmp_fd
);
541 * Return the queue status...
549 * 'cups_rresvport()' - A simple implementation of rresvport_af().
552 static int /* O - Socket or -1 on error */
553 cups_rresvport(int *port
, /* IO - Port number to bind to */
554 int family
) /* I - Address family */
556 http_addr_t addr
; /* Socket address */
557 int fd
; /* Socket file descriptor */
561 * Try to create an IPv4 socket...
564 if ((fd
= socket(family
, SOCK_STREAM
, 0)) < 0)
568 * Initialize the address buffer...
571 memset(&addr
, 0, sizeof(addr
));
572 addr
.addr
.sa_family
= (sa_family_t
)family
;
575 * Try to bind the socket to a reserved port...
581 * Set the port number...
584 _httpAddrSetPort(&addr
, *port
);
587 * Try binding the port to the socket; return if all is OK...
590 if (!bind(fd
, (struct sockaddr
*)&addr
, (socklen_t
)httpAddrLength(&addr
)))
594 * Stop if we have any error other than "address already in use"...
597 if (errno
!= EADDRINUSE
)
599 httpAddrClose(NULL
, fd
);
605 * Try the next port...
612 * Wasn't able to bind to a reserved port, so close the socket and return
627 * 'lpd_command()' - Send an LPR command sequence and wait for a reply.
630 static int /* O - Status of command */
631 lpd_command(int fd
, /* I - Socket connection to LPD host */
632 char *format
, /* I - printf()-style format string */
633 ...) /* I - Additional args as necessary */
635 va_list ap
; /* Argument pointer */
636 char buf
[1024]; /* Output buffer */
637 ssize_t bytes
; /* Number of bytes to output */
638 char status
; /* Status from command */
642 * Don't try to send commands if the job has been canceled...
649 * Format the string...
652 va_start(ap
, format
);
653 bytes
= vsnprintf(buf
, sizeof(buf
), format
, ap
);
656 fprintf(stderr
, "DEBUG: lpd_command %2.2x %s", buf
[0], buf
+ 1);
659 * Send the command...
662 fprintf(stderr
, "DEBUG: Sending command string (" CUPS_LLFMT
" bytes)...\n", CUPS_LLCAST bytes
);
664 if (lpd_write(fd
, buf
, (size_t)bytes
) < bytes
)
666 perror("DEBUG: Unable to send LPD command");
671 * Read back the status from the command and return it...
674 fputs("DEBUG: Reading command status...\n", stderr
);
676 if (recv(fd
, &status
, 1, 0) < 1)
678 _cupsLangPrintFilter(stderr
, "WARNING", _("The printer did not respond."));
679 status
= (char)errno
;
682 fprintf(stderr
, "DEBUG: lpd_command returning %d\n", status
);
689 * 'lpd_queue()' - Queue a file using the Line Printer Daemon protocol.
692 static int /* O - Zero on success, non-zero on failure */
693 lpd_queue(const char *hostname
, /* I - Host to connect to */
694 http_addrlist_t
*addrlist
, /* I - List of host addresses */
695 const char *printer
, /* I - Printer/queue name */
696 int print_fd
, /* I - File to print */
697 int snmp_fd
, /* I - SNMP socket */
698 int mode
, /* I - Print mode */
699 const char *user
, /* I - Requesting user */
700 const char *title
, /* I - Job title */
701 int copies
, /* I - Number of copies */
702 int banner
, /* I - Print LPD banner? */
703 int format
, /* I - Format specifier */
704 int order
, /* I - Order of data/control files */
705 int reserve
, /* I - Reserve ports? */
706 int manual_copies
,/* I - Do copies by hand... */
707 int timeout
, /* I - Timeout... */
708 int contimeout
, /* I - Connection timeout */
709 const char *orighost
) /* I - job-originating-host-name */
711 char localhost
[255]; /* Local host name */
712 int error
; /* Error number */
713 struct stat filestats
; /* File statistics */
714 int lport
; /* LPD connection local port */
715 int fd
; /* LPD socket */
716 char control
[10240], /* LPD control 'file' */
717 *cptr
; /* Pointer into control file string */
718 char status
; /* Status byte from command */
719 int delay
; /* Delay for retries... */
720 char addrname
[256]; /* Address name */
721 http_addrlist_t
*addr
; /* Socket address */
722 int have_supplies
; /* Printer supports supply levels? */
723 int copy
; /* Copies written */
724 time_t start_time
; /* Time of first connect */
725 ssize_t nbytes
; /* Number of bytes written */
726 off_t tbytes
; /* Total bytes written */
727 char buffer
[32768]; /* Output buffer */
729 DWORD tv
; /* Timeout in milliseconds */
731 struct timeval tv
; /* Timeout in secs and usecs */
736 * Remember when we started trying to connect to the printer...
739 start_time
= time(NULL
);
742 * Loop forever trying to print the file...
748 * First try to reserve a port for this connection...
751 fprintf(stderr
, "DEBUG: Connecting to %s:%d for printer %s\n", hostname
,
752 httpAddrPort(&(addrlist
->addr
)), printer
);
753 _cupsLangPrintFilter(stderr
, "INFO", _("Connecting to printer."));
755 for (lport
= reserve
== RESERVE_RFC1179
? 732 : 1024, addr
= addrlist
,
760 * Stop if this job has been canceled...
764 return (CUPS_BACKEND_FAILED
);
767 * Choose the next priviledged port...
775 if (lport
< 721 && reserve
== RESERVE_RFC1179
)
781 if (geteuid() || !reserve
)
783 if (getuid() || !reserve
)
784 #endif /* HAVE_GETEUID */
787 * Just create a regular socket...
790 if ((fd
= socket(addr
->addr
.addr
.sa_family
, SOCK_STREAM
, 0)) < 0)
792 perror("DEBUG: Unable to create socket");
803 * We're running as root and want to comply with RFC 1179. Reserve a
804 * priviledged lport between 721 and 731...
807 if ((fd
= cups_rresvport(&lport
, addr
->addr
.addr
.sa_family
)) < 0)
809 perror("DEBUG: Unable to reserve port");
817 * Connect to the printer or server...
824 return (CUPS_BACKEND_FAILED
);
827 if (!connect(fd
, &(addr
->addr
.addr
), (socklen_t
)httpAddrLength(&(addr
->addr
))))
836 if (getenv("CLASS") != NULL
)
839 * If the CLASS environment variable is set, the job was submitted
840 * to a class and not to a specific queue. In this case, we want
841 * to abort immediately so that the job can be requeued on the next
842 * available printer in the class.
845 _cupsLangPrintFilter(stderr
, "INFO",
846 _("Unable to contact printer, queuing on next "
847 "printer in class."));
850 * Sleep 5 seconds to keep the job from requeuing too rapidly...
855 return (CUPS_BACKEND_FAILED
);
858 fprintf(stderr
, "DEBUG: Connection error: %s\n", strerror(error
));
860 if (errno
== ECONNREFUSED
|| errno
== EHOSTDOWN
|| errno
== EHOSTUNREACH
|| errno
== ETIMEDOUT
|| errno
== ENOTCONN
)
862 if (contimeout
&& (time(NULL
) - start_time
) > contimeout
)
864 _cupsLangPrintFilter(stderr
, "ERROR",
865 _("The printer is not responding."));
866 return (CUPS_BACKEND_FAILED
);
872 _cupsLangPrintFilter(stderr
, "WARNING",
873 _("The printer may not exist or "
874 "is unavailable at this time."));
879 _cupsLangPrintFilter(stderr
, "WARNING",
880 _("The printer is unreachable at "
885 _cupsLangPrintFilter(stderr
, "WARNING",
886 _("The printer is in use."));
890 sleep((unsigned)delay
);
895 else if (error
== EADDRINUSE
)
898 * Try on another port...
905 _cupsLangPrintFilter(stderr
, "ERROR",
906 _("The printer is not responding."));
916 tv
= (DWORD
)(timeout
* 1000);
918 setsockopt(fd
, SOL_SOCKET
, SO_RCVTIMEO
, (char *)&tv
, sizeof(tv
));
919 setsockopt(fd
, SOL_SOCKET
, SO_SNDTIMEO
, (char *)&tv
, sizeof(tv
));
924 setsockopt(fd
, SOL_SOCKET
, SO_RCVTIMEO
, &tv
, sizeof(tv
));
925 setsockopt(fd
, SOL_SOCKET
, SO_SNDTIMEO
, &tv
, sizeof(tv
));
928 fputs("STATE: -connecting-to-device\n", stderr
);
929 _cupsLangPrintFilter(stderr
, "INFO", _("Connected to printer."));
931 fprintf(stderr
, "DEBUG: Connected to %s:%d (local port %d)...\n",
932 httpAddrString(&(addr
->addr
), addrname
, sizeof(addrname
)),
933 httpAddrPort(&(addr
->addr
)), lport
);
936 * See if the printer supports SNMP...
940 have_supplies
= !backendSNMPSupplies(snmp_fd
, &(addrlist
->addr
), NULL
,
946 * Check for side-channel requests...
949 backendCheckSideChannel(snmp_fd
, &(addrlist
->addr
));
952 * Next, open the print file and figure out its size...
958 * Use the size from the print file...
961 if (fstat(print_fd
, &filestats
))
965 perror("DEBUG: unable to stat print file");
966 return (CUPS_BACKEND_FAILED
);
969 filestats
.st_size
*= manual_copies
;
974 * Use a "very large value" for the size so that the printer will
975 * keep printing until we close the connection...
978 #ifdef _LARGEFILE_SOURCE
979 filestats
.st_size
= (size_t)(999999999999.0);
981 filestats
.st_size
= 2147483647;
982 #endif /* _LARGEFILE_SOURCE */
986 * Send a job header to the printer, specifying no banner page and
990 if (lpd_command(fd
, "\002%s\n",
991 printer
)) /* Receive print job(s) */
994 return (CUPS_BACKEND_FAILED
);
997 if (orighost
&& _cups_strcasecmp(orighost
, "localhost"))
998 strlcpy(localhost
, orighost
, sizeof(localhost
));
1000 httpGetHostname(NULL
, localhost
, sizeof(localhost
));
1002 snprintf(control
, sizeof(control
),
1003 "H%.31s\n" /* RFC 1179, Section 7.2 - host name <= 31 chars */
1004 "P%.31s\n" /* RFC 1179, Section 7.2 - user name <= 31 chars */
1005 "J%.99s\n", /* RFC 1179, Section 7.2 - job name <= 99 chars */
1006 localhost
, user
, title
);
1007 cptr
= control
+ strlen(control
);
1011 snprintf(cptr
, sizeof(control
) - (size_t)(cptr
- control
),
1012 "C%.31s\n" /* RFC 1179, Section 7.2 - class name <= 31 chars */
1015 cptr
+= strlen(cptr
);
1020 snprintf(cptr
, sizeof(control
) - (size_t)(cptr
- control
), "%cdfA%03d%.15s\n",
1021 format
, (int)getpid() % 1000, localhost
);
1022 cptr
+= strlen(cptr
);
1026 snprintf(cptr
, sizeof(control
) - (size_t)(cptr
- control
),
1028 "N%.131s\n", /* RFC 1179, Section 7.2 - sourcefile name <= 131 chars */
1029 (int)getpid() % 1000, localhost
, title
);
1031 fprintf(stderr
, "DEBUG: Control file is:\n%s", control
);
1033 if (order
== ORDER_CONTROL_DATA
)
1036 * Check for side-channel requests...
1039 backendCheckSideChannel(snmp_fd
, &(addr
->addr
));
1042 * Send the control file...
1045 if (lpd_command(fd
, "\002%d cfA%03.3d%.15s\n", strlen(control
),
1046 (int)getpid() % 1000, localhost
))
1050 return (CUPS_BACKEND_FAILED
);
1053 fprintf(stderr
, "DEBUG: Sending control file (%u bytes)\n",
1054 (unsigned)strlen(control
));
1056 if ((size_t)lpd_write(fd
, control
, strlen(control
) + 1) < (strlen(control
) + 1))
1058 status
= (char)errno
;
1059 perror("DEBUG: Unable to write control file");
1064 if (read(fd
, &status
, 1) < 1)
1066 _cupsLangPrintFilter(stderr
, "WARNING",
1067 _("The printer did not respond."));
1068 status
= (char)errno
;
1073 _cupsLangPrintFilter(stderr
, "ERROR",
1074 _("Remote host did not accept control file (%d)."),
1077 _cupsLangPrintFilter(stderr
, "INFO",
1078 _("Control file sent successfully."));
1086 * Check for side-channel requests...
1089 backendCheckSideChannel(snmp_fd
, &(addr
->addr
));
1092 * Send the print file...
1095 if (lpd_command(fd
, "\003" CUPS_LLFMT
" dfA%03.3d%.15s\n",
1096 CUPS_LLCAST filestats
.st_size
, (int)getpid() % 1000,
1101 return (CUPS_BACKEND_FAILED
);
1104 fprintf(stderr
, "DEBUG: Sending data file (" CUPS_LLFMT
" bytes)\n",
1105 CUPS_LLCAST filestats
.st_size
);
1108 for (copy
= 0; copy
< manual_copies
; copy
++)
1110 lseek(print_fd
, 0, SEEK_SET
);
1112 while ((nbytes
= read(print_fd
, buffer
, sizeof(buffer
))) > 0)
1114 _cupsLangPrintFilter(stderr
, "INFO",
1115 _("Spooling job, %.0f%% complete."),
1116 100.0 * tbytes
/ filestats
.st_size
);
1118 if (lpd_write(fd
, buffer
, (size_t)nbytes
) < nbytes
)
1120 perror("DEBUG: Unable to send print file to printer");
1128 if (mode
== MODE_STANDARD
)
1130 if (tbytes
< filestats
.st_size
)
1131 status
= (char)errno
;
1132 else if (lpd_write(fd
, "", 1) < 1)
1134 perror("DEBUG: Unable to send trailing nul to printer");
1135 status
= (char)errno
;
1140 * Read the status byte from the printer; if we can't read the byte
1141 * back now, we should set status to "errno", however at this point
1142 * we know the printer got the whole file and we don't necessarily
1143 * want to requeue it over and over...
1146 if (recv(fd
, &status
, 1, 0) < 1)
1148 _cupsLangPrintFilter(stderr
, "WARNING",
1149 _("The printer did not respond."));
1158 _cupsLangPrintFilter(stderr
, "ERROR",
1159 _("Remote host did not accept data file (%d)."),
1162 _cupsLangPrintFilter(stderr
, "INFO",
1163 _("Data file sent successfully."));
1166 if (status
== 0 && order
== ORDER_DATA_CONTROL
)
1169 * Check for side-channel requests...
1172 backendCheckSideChannel(snmp_fd
, &(addr
->addr
));
1175 * Send control file...
1178 if (lpd_command(fd
, "\002%d cfA%03.3d%.15s\n", strlen(control
),
1179 (int)getpid() % 1000, localhost
))
1183 return (CUPS_BACKEND_FAILED
);
1186 fprintf(stderr
, "DEBUG: Sending control file (%lu bytes)\n",
1187 (unsigned long)strlen(control
));
1189 if ((size_t)lpd_write(fd
, control
, strlen(control
) + 1) < (strlen(control
) + 1))
1191 status
= (char)errno
;
1192 perror("DEBUG: Unable to write control file");
1196 if (read(fd
, &status
, 1) < 1)
1198 _cupsLangPrintFilter(stderr
, "WARNING",
1199 _("The printer did not respond."));
1200 status
= (char)errno
;
1205 _cupsLangPrintFilter(stderr
, "ERROR",
1206 _("Remote host did not accept control file (%d)."),
1209 _cupsLangPrintFilter(stderr
, "INFO",
1210 _("Control file sent successfully."));
1213 fputs("STATE: +cups-waiting-for-job-completed\n", stderr
);
1216 * Collect the final supply levels as needed...
1220 backendSNMPSupplies(snmp_fd
, &(addr
->addr
), NULL
, NULL
);
1223 * Close the socket connection and input file...
1229 return (CUPS_BACKEND_OK
);
1232 * Waiting for a retry...
1239 * If we get here, then the job has been canceled...
1242 return (CUPS_BACKEND_FAILED
);
1247 * 'lpd_write()' - Write a buffer of data to an LPD server.
1250 static ssize_t
/* O - Number of bytes written or -1 on error */
1251 lpd_write(int lpd_fd
, /* I - LPD socket */
1252 char *buffer
, /* I - Buffer to write */
1253 size_t length
) /* I - Number of bytes to write */
1255 ssize_t bytes
, /* Number of bytes written */
1256 total
; /* Total number of bytes written */
1263 while ((bytes
= send(lpd_fd
, buffer
, length
- (size_t)total
, 0)) >= 0)
1268 if ((size_t)total
== length
)
1280 * 'sigterm_handler()' - Handle 'terminate' signals that stop the backend.
1284 sigterm_handler(int sig
) /* I - Signal */
1286 (void)sig
; /* remove compiler warnings... */