]>
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 char portname
[256]; /* Port name (string) */
115 http_addrlist_t
*addrlist
; /* List of addresses for printer */
116 int snmp_enabled
= 1; /* Is SNMP enabled? */
117 int snmp_fd
; /* SNMP socket */
118 int fd
; /* Print file */
119 int status
; /* Status of LPD job */
120 int mode
; /* Print mode */
121 int banner
; /* Print banner page? */
122 int format
; /* Print format */
123 int order
; /* Order of control/data files */
124 int reserve
; /* Reserve priviledged port? */
125 int sanitize_title
; /* Sanitize title string? */
126 int manual_copies
, /* Do manual copies? */
127 timeout
, /* Timeout */
128 contimeout
, /* Connection timeout */
129 copies
; /* Number of copies */
130 ssize_t bytes
= 0; /* Initial bytes read */
131 char buffer
[16384]; /* Initial print buffer */
132 #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
133 struct sigaction action
; /* Actions for POSIX signals */
134 #endif /* HAVE_SIGACTION && !HAVE_SIGSET */
135 int num_jobopts
; /* Number of job options */
136 cups_option_t
*jobopts
= NULL
; /* Job options */
140 * Make sure status messages are not buffered...
143 setbuf(stderr
, NULL
);
146 * Ignore SIGPIPE and catch SIGTERM signals...
150 sigset(SIGPIPE
, SIG_IGN
);
151 sigset(SIGTERM
, sigterm_handler
);
152 #elif defined(HAVE_SIGACTION)
153 memset(&action
, 0, sizeof(action
));
154 action
.sa_handler
= SIG_IGN
;
155 sigaction(SIGPIPE
, &action
, NULL
);
157 sigemptyset(&action
.sa_mask
);
158 sigaddset(&action
.sa_mask
, SIGTERM
);
159 action
.sa_handler
= sigterm_handler
;
160 sigaction(SIGTERM
, &action
, NULL
);
162 signal(SIGPIPE
, SIG_IGN
);
163 signal(SIGTERM
, sigterm_handler
);
164 #endif /* HAVE_SIGSET */
167 * Check command-line...
172 printf("network lpd \"Unknown\" \"%s\"\n",
173 _cupsLangString(cupsLangDefault(), _("LPD/LPR Host or Printer")));
174 return (CUPS_BACKEND_OK
);
176 else if (argc
< 6 || argc
> 7)
178 _cupsLangPrintf(stderr
,
179 _("Usage: %s job-id user title copies options [file]"),
181 return (CUPS_BACKEND_FAILED
);
184 num_jobopts
= cupsParseOptions(argv
[5], 0, &jobopts
);
187 * Extract the hostname and printer name from the URI...
190 while ((device_uri
= cupsBackendDeviceURI(argv
)) == NULL
)
192 _cupsLangPrintFilter(stderr
, "INFO", _("Unable to locate printer."));
195 if (getenv("CLASS") != NULL
)
196 return (CUPS_BACKEND_FAILED
);
199 httpSeparateURI(HTTP_URI_CODING_ALL
, device_uri
, scheme
, sizeof(scheme
),
200 username
, sizeof(username
), hostname
, sizeof(hostname
), &port
,
201 resource
, sizeof(resource
));
204 port
= 515; /* Default to port 515 */
209 * If no username is in the device URI, then use the print job user...
212 strlcpy(username
, argv
[2], sizeof(username
));
216 * See if there are any options...
219 mode
= MODE_STANDARD
;
222 order
= ORDER_CONTROL_DATA
;
223 reserve
= RESERVE_ANY
;
226 contimeout
= 7 * 24 * 60 * 60;
230 * We want to pass UTF-8 characters by default, not re-map them (3071945)
236 * Otherwise we want to re-map UTF-8 to "safe" characters by default...
240 #endif /* __APPLE__ */
242 if ((options
= strchr(resource
, '?')) != NULL
)
245 * Yup, terminate the device name string and move to the first
246 * character of the options...
263 while (*options
&& *options
!= '=' && *options
!= '+' && *options
!= '&')
266 if ((sep
= *options
) != '\0')
277 while (*options
&& *options
!= '+' && *options
!= '&')
287 * Process the option...
290 if (!_cups_strcasecmp(name
, "banner"))
296 banner
= !value
[0] || !_cups_strcasecmp(value
, "on") ||
297 !_cups_strcasecmp(value
, "yes") || !_cups_strcasecmp(value
, "true");
299 else if (!_cups_strcasecmp(name
, "format") && value
[0])
302 * Set output format...
305 if (strchr("cdfglnoprtv", value
[0]))
308 _cupsLangPrintFilter(stderr
, "ERROR",
309 _("Unknown format character: \"%c\"."),
312 else if (!_cups_strcasecmp(name
, "mode") && value
[0])
315 * Set control/data order...
318 if (!_cups_strcasecmp(value
, "standard"))
319 mode
= MODE_STANDARD
;
320 else if (!_cups_strcasecmp(value
, "stream"))
323 _cupsLangPrintFilter(stderr
, "ERROR",
324 _("Unknown print mode: \"%s\"."), value
);
326 else if (!_cups_strcasecmp(name
, "order") && value
[0])
329 * Set control/data order...
332 if (!_cups_strcasecmp(value
, "control,data"))
333 order
= ORDER_CONTROL_DATA
;
334 else if (!_cups_strcasecmp(value
, "data,control"))
335 order
= ORDER_DATA_CONTROL
;
337 _cupsLangPrintFilter(stderr
, "ERROR",
338 _("Unknown file order: \"%s\"."), value
);
340 else if (!_cups_strcasecmp(name
, "reserve"))
343 * Set port reservation mode...
346 if (!value
[0] || !_cups_strcasecmp(value
, "on") ||
347 !_cups_strcasecmp(value
, "yes") ||
348 !_cups_strcasecmp(value
, "true") ||
349 !_cups_strcasecmp(value
, "rfc1179"))
350 reserve
= RESERVE_RFC1179
;
351 else if (!_cups_strcasecmp(value
, "any"))
352 reserve
= RESERVE_ANY
;
354 reserve
= RESERVE_NONE
;
356 else if (!_cups_strcasecmp(name
, "manual_copies"))
359 * Set manual copies...
362 manual_copies
= !value
[0] || !_cups_strcasecmp(value
, "on") ||
363 !_cups_strcasecmp(value
, "yes") ||
364 !_cups_strcasecmp(value
, "true");
366 else if (!_cups_strcasecmp(name
, "sanitize_title"))
369 * Set sanitize title...
372 sanitize_title
= !value
[0] || !_cups_strcasecmp(value
, "on") ||
373 !_cups_strcasecmp(value
, "yes") ||
374 !_cups_strcasecmp(value
, "true");
376 else if (!_cups_strcasecmp(name
, "snmp"))
379 * Enable/disable SNMP stuff...
382 snmp_enabled
= !value
[0] || !_cups_strcasecmp(value
, "on") ||
383 !_cups_strcasecmp(value
, "yes") ||
384 !_cups_strcasecmp(value
, "true");
386 else if (!_cups_strcasecmp(name
, "timeout"))
393 timeout
= atoi(value
);
395 else if (!_cups_strcasecmp(name
, "contimeout"))
398 * Set the connection timeout...
402 contimeout
= atoi(value
);
407 if (mode
== MODE_STREAM
)
408 order
= ORDER_CONTROL_DATA
;
411 * Find the printer...
414 snprintf(portname
, sizeof(portname
), "%d", port
);
416 fputs("STATE: +connecting-to-device\n", stderr
);
417 fprintf(stderr
, "DEBUG: Looking up \"%s\"...\n", hostname
);
419 while ((addrlist
= httpAddrGetList(hostname
, AF_UNSPEC
, portname
)) == NULL
)
421 _cupsLangPrintFilter(stderr
, "INFO",
422 _("Unable to locate printer \"%s\"."), hostname
);
425 if (getenv("CLASS") != NULL
)
427 fputs("STATE: -connecting-to-device\n", stderr
);
428 exit(CUPS_BACKEND_FAILED
);
433 snmp_fd
= _cupsSNMPOpen(addrlist
->addr
.addr
.sa_family
);
438 * Wait for data from the filter...
443 if (!backendWaitLoop(snmp_fd
, &(addrlist
->addr
), 0, backendNetworkSideCB
))
444 return (CUPS_BACKEND_OK
);
445 else if (mode
== MODE_STANDARD
&&
446 (bytes
= read(0, buffer
, sizeof(buffer
))) <= 0)
447 return (CUPS_BACKEND_OK
);
451 * If we have 7 arguments, print the file named on the command-line.
452 * Otherwise, copy stdin to a temporary file and print the temporary
456 if (argc
== 6 && mode
== MODE_STANDARD
)
459 * Copy stdin to a temporary file...
462 if ((fd
= cupsTempFd(tmpfilename
, sizeof(tmpfilename
))) < 0)
464 perror("DEBUG: Unable to create temporary file");
465 return (CUPS_BACKEND_FAILED
);
468 _cupsLangPrintFilter(stderr
, "INFO", _("Copying print data."));
471 write(fd
, buffer
, (size_t)bytes
);
473 backendRunLoop(-1, fd
, snmp_fd
, &(addrlist
->addr
), 0, 0,
474 backendNetworkSideCB
);
479 * Stream from stdin...
488 fd
= open(filename
, O_RDONLY
);
492 _cupsLangPrintError("ERROR", _("Unable to open print file"));
493 return (CUPS_BACKEND_FAILED
);
498 * Sanitize the document title...
501 strlcpy(title
, argv
[3], sizeof(title
));
506 * Sanitize the title string so that we don't cause problems on
512 for (ptr
= title
; *ptr
; ptr
++)
513 if (!isalnum(*ptr
& 255) && !isspace(*ptr
& 255))
525 manual_copies
= atoi(argv
[4]);
531 copies
= atoi(argv
[4]);
534 status
= lpd_queue(hostname
, addrlist
, resource
+ 1, fd
, snmp_fd
, mode
,
535 username
, title
, copies
, banner
, format
, order
, reserve
,
536 manual_copies
, timeout
, contimeout
,
537 cupsGetOption("job-originating-host-name", num_jobopts
,
541 fprintf(stderr
, "PAGE: 1 %d\n", atoi(argv
[4]));
544 status
= lpd_queue(hostname
, addrlist
, resource
+ 1, fd
, snmp_fd
, mode
,
545 username
, title
, 1, banner
, format
, order
, reserve
, 1,
547 cupsGetOption("job-originating-host-name", num_jobopts
,
551 * Remove the temporary file if necessary...
561 _cupsSNMPClose(snmp_fd
);
564 * Return the queue status...
572 * 'cups_rresvport()' - A simple implementation of rresvport_af().
575 static int /* O - Socket or -1 on error */
576 cups_rresvport(int *port
, /* IO - Port number to bind to */
577 int family
) /* I - Address family */
579 http_addr_t addr
; /* Socket address */
580 int fd
; /* Socket file descriptor */
584 * Try to create an IPv4 socket...
587 if ((fd
= socket(family
, SOCK_STREAM
, 0)) < 0)
591 * Initialize the address buffer...
594 memset(&addr
, 0, sizeof(addr
));
595 addr
.addr
.sa_family
= (sa_family_t
)family
;
598 * Try to bind the socket to a reserved port...
604 * Set the port number...
607 _httpAddrSetPort(&addr
, *port
);
610 * Try binding the port to the socket; return if all is OK...
613 if (!bind(fd
, (struct sockaddr
*)&addr
, (socklen_t
)httpAddrLength(&addr
)))
617 * Stop if we have any error other than "address already in use"...
620 if (errno
!= EADDRINUSE
)
622 httpAddrClose(NULL
, fd
);
628 * Try the next port...
635 * Wasn't able to bind to a reserved port, so close the socket and return
650 * 'lpd_command()' - Send an LPR command sequence and wait for a reply.
653 static int /* O - Status of command */
654 lpd_command(int fd
, /* I - Socket connection to LPD host */
655 char *format
, /* I - printf()-style format string */
656 ...) /* I - Additional args as necessary */
658 va_list ap
; /* Argument pointer */
659 char buf
[1024]; /* Output buffer */
660 ssize_t bytes
; /* Number of bytes to output */
661 char status
; /* Status from command */
665 * Don't try to send commands if the job has been canceled...
672 * Format the string...
675 va_start(ap
, format
);
676 bytes
= vsnprintf(buf
, sizeof(buf
), format
, ap
);
679 fprintf(stderr
, "DEBUG: lpd_command %2.2x %s", buf
[0], buf
+ 1);
682 * Send the command...
685 fprintf(stderr
, "DEBUG: Sending command string (" CUPS_LLFMT
" bytes)...\n", CUPS_LLCAST bytes
);
687 if (lpd_write(fd
, buf
, (size_t)bytes
) < bytes
)
689 perror("DEBUG: Unable to send LPD command");
694 * Read back the status from the command and return it...
697 fputs("DEBUG: Reading command status...\n", stderr
);
699 if (recv(fd
, &status
, 1, 0) < 1)
701 _cupsLangPrintFilter(stderr
, "WARNING", _("The printer did not respond."));
702 status
= (char)errno
;
705 fprintf(stderr
, "DEBUG: lpd_command returning %d\n", status
);
712 * 'lpd_queue()' - Queue a file using the Line Printer Daemon protocol.
715 static int /* O - Zero on success, non-zero on failure */
716 lpd_queue(const char *hostname
, /* I - Host to connect to */
717 http_addrlist_t
*addrlist
, /* I - List of host addresses */
718 const char *printer
, /* I - Printer/queue name */
719 int print_fd
, /* I - File to print */
720 int snmp_fd
, /* I - SNMP socket */
721 int mode
, /* I - Print mode */
722 const char *user
, /* I - Requesting user */
723 const char *title
, /* I - Job title */
724 int copies
, /* I - Number of copies */
725 int banner
, /* I - Print LPD banner? */
726 int format
, /* I - Format specifier */
727 int order
, /* I - Order of data/control files */
728 int reserve
, /* I - Reserve ports? */
729 int manual_copies
,/* I - Do copies by hand... */
730 int timeout
, /* I - Timeout... */
731 int contimeout
, /* I - Connection timeout */
732 const char *orighost
) /* I - job-originating-host-name */
734 char localhost
[255]; /* Local host name */
735 int error
; /* Error number */
736 struct stat filestats
; /* File statistics */
737 int lport
; /* LPD connection local port */
738 int fd
; /* LPD socket */
739 char control
[10240], /* LPD control 'file' */
740 *cptr
; /* Pointer into control file string */
741 char status
; /* Status byte from command */
742 int delay
; /* Delay for retries... */
743 char addrname
[256]; /* Address name */
744 http_addrlist_t
*addr
; /* Socket address */
745 int have_supplies
; /* Printer supports supply levels? */
746 int copy
; /* Copies written */
747 time_t start_time
; /* Time of first connect */
748 ssize_t nbytes
; /* Number of bytes written */
749 off_t tbytes
; /* Total bytes written */
750 char buffer
[32768]; /* Output buffer */
752 DWORD tv
; /* Timeout in milliseconds */
754 struct timeval tv
; /* Timeout in secs and usecs */
759 * Remember when we started trying to connect to the printer...
762 start_time
= time(NULL
);
765 * Loop forever trying to print the file...
771 * First try to reserve a port for this connection...
774 fprintf(stderr
, "DEBUG: Connecting to %s:%d for printer %s\n", hostname
,
775 httpAddrPort(&(addrlist
->addr
)), printer
);
776 _cupsLangPrintFilter(stderr
, "INFO", _("Connecting to printer."));
778 for (lport
= reserve
== RESERVE_RFC1179
? 732 : 1024, addr
= addrlist
,
783 * Stop if this job has been canceled...
787 return (CUPS_BACKEND_FAILED
);
790 * Choose the next priviledged port...
798 if (lport
< 721 && reserve
== RESERVE_RFC1179
)
804 if (geteuid() || !reserve
)
806 if (getuid() || !reserve
)
807 #endif /* HAVE_GETEUID */
810 * Just create a regular socket...
813 if ((fd
= socket(addr
->addr
.addr
.sa_family
, SOCK_STREAM
, 0)) < 0)
815 perror("DEBUG: Unable to create socket");
826 * We're running as root and want to comply with RFC 1179. Reserve a
827 * priviledged lport between 721 and 731...
830 if ((fd
= cups_rresvport(&lport
, addr
->addr
.addr
.sa_family
)) < 0)
832 perror("DEBUG: Unable to reserve port");
840 * Connect to the printer or server...
847 return (CUPS_BACKEND_FAILED
);
850 if (!connect(fd
, &(addr
->addr
.addr
), (socklen_t
)httpAddrLength(&(addr
->addr
))))
859 if (getenv("CLASS") != NULL
)
862 * If the CLASS environment variable is set, the job was submitted
863 * to a class and not to a specific queue. In this case, we want
864 * to abort immediately so that the job can be requeued on the next
865 * available printer in the class.
868 _cupsLangPrintFilter(stderr
, "INFO",
869 _("Unable to contact printer, queuing on next "
870 "printer in class."));
873 * Sleep 5 seconds to keep the job from requeuing too rapidly...
878 return (CUPS_BACKEND_FAILED
);
881 fprintf(stderr
, "DEBUG: Connection error: %s\n", strerror(error
));
883 if (error
== ECONNREFUSED
|| error
== EHOSTDOWN
||
884 error
== EHOSTUNREACH
)
886 if (contimeout
&& (time(NULL
) - start_time
) > contimeout
)
888 _cupsLangPrintFilter(stderr
, "ERROR",
889 _("The printer is not responding."));
890 return (CUPS_BACKEND_FAILED
);
896 _cupsLangPrintFilter(stderr
, "WARNING",
897 _("The printer may not exist or "
898 "is unavailable at this time."));
902 _cupsLangPrintFilter(stderr
, "WARNING",
903 _("The printer is unreachable at "
909 _cupsLangPrintFilter(stderr
, "WARNING",
910 _("The printer is in use."));
914 sleep((unsigned)delay
);
919 else if (error
== EADDRINUSE
)
922 * Try on another port...
929 _cupsLangPrintFilter(stderr
, "ERROR",
930 _("The printer is not responding."));
940 tv
= (DWORD
)(timeout
* 1000);
942 setsockopt(fd
, SOL_SOCKET
, SO_RCVTIMEO
, (char *)&tv
, sizeof(tv
));
943 setsockopt(fd
, SOL_SOCKET
, SO_SNDTIMEO
, (char *)&tv
, sizeof(tv
));
948 setsockopt(fd
, SOL_SOCKET
, SO_RCVTIMEO
, &tv
, sizeof(tv
));
949 setsockopt(fd
, SOL_SOCKET
, SO_SNDTIMEO
, &tv
, sizeof(tv
));
952 fputs("STATE: -connecting-to-device\n", stderr
);
953 _cupsLangPrintFilter(stderr
, "INFO", _("Connected to printer."));
955 fprintf(stderr
, "DEBUG: Connected to %s:%d (local port %d)...\n",
956 httpAddrString(&(addr
->addr
), addrname
, sizeof(addrname
)),
957 httpAddrPort(&(addr
->addr
)), lport
);
960 * See if the printer supports SNMP...
964 have_supplies
= !backendSNMPSupplies(snmp_fd
, &(addrlist
->addr
), NULL
,
970 * Check for side-channel requests...
973 backendCheckSideChannel(snmp_fd
, &(addrlist
->addr
));
976 * Next, open the print file and figure out its size...
982 * Use the size from the print file...
985 if (fstat(print_fd
, &filestats
))
989 perror("DEBUG: unable to stat print file");
990 return (CUPS_BACKEND_FAILED
);
993 filestats
.st_size
*= manual_copies
;
998 * Use a "very large value" for the size so that the printer will
999 * keep printing until we close the connection...
1002 #ifdef _LARGEFILE_SOURCE
1003 filestats
.st_size
= (size_t)(999999999999.0);
1005 filestats
.st_size
= 2147483647;
1006 #endif /* _LARGEFILE_SOURCE */
1010 * Send a job header to the printer, specifying no banner page and
1014 if (lpd_command(fd
, "\002%s\n",
1015 printer
)) /* Receive print job(s) */
1018 return (CUPS_BACKEND_FAILED
);
1021 if (orighost
&& _cups_strcasecmp(orighost
, "localhost"))
1022 strlcpy(localhost
, orighost
, sizeof(localhost
));
1024 httpGetHostname(NULL
, localhost
, sizeof(localhost
));
1026 snprintf(control
, sizeof(control
),
1027 "H%.31s\n" /* RFC 1179, Section 7.2 - host name <= 31 chars */
1028 "P%.31s\n" /* RFC 1179, Section 7.2 - user name <= 31 chars */
1029 "J%.99s\n", /* RFC 1179, Section 7.2 - job name <= 99 chars */
1030 localhost
, user
, title
);
1031 cptr
= control
+ strlen(control
);
1035 snprintf(cptr
, sizeof(control
) - (size_t)(cptr
- control
),
1036 "C%.31s\n" /* RFC 1179, Section 7.2 - class name <= 31 chars */
1039 cptr
+= strlen(cptr
);
1044 snprintf(cptr
, sizeof(control
) - (size_t)(cptr
- control
), "%cdfA%03d%.15s\n",
1045 format
, (int)getpid() % 1000, localhost
);
1046 cptr
+= strlen(cptr
);
1050 snprintf(cptr
, sizeof(control
) - (size_t)(cptr
- control
),
1052 "N%.131s\n", /* RFC 1179, Section 7.2 - sourcefile name <= 131 chars */
1053 (int)getpid() % 1000, localhost
, title
);
1055 fprintf(stderr
, "DEBUG: Control file is:\n%s", control
);
1057 if (order
== ORDER_CONTROL_DATA
)
1060 * Check for side-channel requests...
1063 backendCheckSideChannel(snmp_fd
, &(addr
->addr
));
1066 * Send the control file...
1069 if (lpd_command(fd
, "\002%d cfA%03.3d%.15s\n", strlen(control
),
1070 (int)getpid() % 1000, localhost
))
1074 return (CUPS_BACKEND_FAILED
);
1077 fprintf(stderr
, "DEBUG: Sending control file (%u bytes)\n",
1078 (unsigned)strlen(control
));
1080 if ((size_t)lpd_write(fd
, control
, strlen(control
) + 1) < (strlen(control
) + 1))
1082 status
= (char)errno
;
1083 perror("DEBUG: Unable to write control file");
1088 if (read(fd
, &status
, 1) < 1)
1090 _cupsLangPrintFilter(stderr
, "WARNING",
1091 _("The printer did not respond."));
1092 status
= (char)errno
;
1097 _cupsLangPrintFilter(stderr
, "ERROR",
1098 _("Remote host did not accept control file (%d)."),
1101 _cupsLangPrintFilter(stderr
, "INFO",
1102 _("Control file sent successfully."));
1110 * Check for side-channel requests...
1113 backendCheckSideChannel(snmp_fd
, &(addr
->addr
));
1116 * Send the print file...
1119 if (lpd_command(fd
, "\003" CUPS_LLFMT
" dfA%03.3d%.15s\n",
1120 CUPS_LLCAST filestats
.st_size
, (int)getpid() % 1000,
1125 return (CUPS_BACKEND_FAILED
);
1128 fprintf(stderr
, "DEBUG: Sending data file (" CUPS_LLFMT
" bytes)\n",
1129 CUPS_LLCAST filestats
.st_size
);
1132 for (copy
= 0; copy
< manual_copies
; copy
++)
1134 lseek(print_fd
, 0, SEEK_SET
);
1136 while ((nbytes
= read(print_fd
, buffer
, sizeof(buffer
))) > 0)
1138 _cupsLangPrintFilter(stderr
, "INFO",
1139 _("Spooling job, %.0f%% complete."),
1140 100.0 * tbytes
/ filestats
.st_size
);
1142 if (lpd_write(fd
, buffer
, (size_t)nbytes
) < nbytes
)
1144 perror("DEBUG: Unable to send print file to printer");
1152 if (mode
== MODE_STANDARD
)
1154 if (tbytes
< filestats
.st_size
)
1155 status
= (char)errno
;
1156 else if (lpd_write(fd
, "", 1) < 1)
1158 perror("DEBUG: Unable to send trailing nul to printer");
1159 status
= (char)errno
;
1164 * Read the status byte from the printer; if we can't read the byte
1165 * back now, we should set status to "errno", however at this point
1166 * we know the printer got the whole file and we don't necessarily
1167 * want to requeue it over and over...
1170 if (recv(fd
, &status
, 1, 0) < 1)
1172 _cupsLangPrintFilter(stderr
, "WARNING",
1173 _("The printer did not respond."));
1182 _cupsLangPrintFilter(stderr
, "ERROR",
1183 _("Remote host did not accept data file (%d)."),
1186 _cupsLangPrintFilter(stderr
, "INFO",
1187 _("Data file sent successfully."));
1190 if (status
== 0 && order
== ORDER_DATA_CONTROL
)
1193 * Check for side-channel requests...
1196 backendCheckSideChannel(snmp_fd
, &(addr
->addr
));
1199 * Send control file...
1202 if (lpd_command(fd
, "\002%d cfA%03.3d%.15s\n", strlen(control
),
1203 (int)getpid() % 1000, localhost
))
1207 return (CUPS_BACKEND_FAILED
);
1210 fprintf(stderr
, "DEBUG: Sending control file (%lu bytes)\n",
1211 (unsigned long)strlen(control
));
1213 if ((size_t)lpd_write(fd
, control
, strlen(control
) + 1) < (strlen(control
) + 1))
1215 status
= (char)errno
;
1216 perror("DEBUG: Unable to write control file");
1220 if (read(fd
, &status
, 1) < 1)
1222 _cupsLangPrintFilter(stderr
, "WARNING",
1223 _("The printer did not respond."));
1224 status
= (char)errno
;
1229 _cupsLangPrintFilter(stderr
, "ERROR",
1230 _("Remote host did not accept control file (%d)."),
1233 _cupsLangPrintFilter(stderr
, "INFO",
1234 _("Control file sent successfully."));
1237 fputs("STATE: +cups-waiting-for-job-completed\n", stderr
);
1240 * Collect the final supply levels as needed...
1244 backendSNMPSupplies(snmp_fd
, &(addr
->addr
), NULL
, NULL
);
1247 * Close the socket connection and input file...
1253 return (CUPS_BACKEND_OK
);
1256 * Waiting for a retry...
1263 * If we get here, then the job has been canceled...
1266 return (CUPS_BACKEND_FAILED
);
1271 * 'lpd_write()' - Write a buffer of data to an LPD server.
1274 static ssize_t
/* O - Number of bytes written or -1 on error */
1275 lpd_write(int lpd_fd
, /* I - LPD socket */
1276 char *buffer
, /* I - Buffer to write */
1277 size_t length
) /* I - Number of bytes to write */
1279 ssize_t bytes
, /* Number of bytes written */
1280 total
; /* Total number of bytes written */
1287 while ((bytes
= send(lpd_fd
, buffer
, length
- (size_t)total
, 0)) >= 0)
1292 if ((size_t)total
== length
)
1304 * 'sigterm_handler()' - Handle 'terminate' signals that stop the backend.
1308 sigterm_handler(int sig
) /* I - Signal */
1310 (void)sig
; /* remove compiler warnings... */