]>
git.ipfire.org Git - thirdparty/cups.git/blob - backend/lpd.c
41f14913e7313562dd0ed835f71c82fe1ffc01c5
4 * Line Printer Daemon backend for CUPS.
6 * Copyright 2007-2016 by Apple Inc.
7 * Copyright 1997-2007 by Easy Software Products, all rights reserved.
9 * These coded instructions, statements, and computer programs are the
10 * property of Apple Inc. and are protected by Federal copyright
11 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
12 * "LICENSE" which should have been included with this file. If this
13 * file is missing or damaged, see the license at "http://www.cups.org/".
15 * This file is subject to the Apple OS-Developed Software exception.
19 * Include necessary headers.
22 #include <cups/http-private.h>
23 #include "backend-private.h"
25 #include <sys/types.h>
32 # include <sys/socket.h>
33 # include <netinet/in.h>
34 # include <arpa/inet.h>
38 # include <CoreFoundation/CFNumber.h>
39 # include <CoreFoundation/CFPreferences.h>
40 #endif /* __APPLE__ */
47 static char tmpfilename
[1024] = ""; /* Temporary spool file name */
48 static int abort_job
= 0; /* Non-zero if we get SIGTERM */
55 #define MODE_STANDARD 0 /* Queue a copy */
56 #define MODE_STREAM 1 /* Stream a copy */
60 * The order for control and data files in LPD requests...
63 #define ORDER_CONTROL_DATA 0 /* Control file first, then data */
64 #define ORDER_DATA_CONTROL 1 /* Data file first, then control */
71 #define RESERVE_NONE 0 /* Don't reserve a priviledged port */
72 #define RESERVE_RFC1179 1 /* Reserve port 721-731 */
73 #define RESERVE_ANY 2 /* Reserve port 1-1023 */
80 static int cups_rresvport(int *port
, int family
);
81 static int lpd_command(int lpd_fd
, char *format
, ...);
82 static int lpd_queue(const char *hostname
, http_addrlist_t
*addrlist
,
83 const char *printer
, int print_fd
, int snmp_fd
,
84 int mode
, const char *user
, const char *title
,
85 int copies
, int banner
, int format
, int order
,
86 int reserve
, int manual_copies
, int timeout
,
87 int contimeout
, const char *orighost
);
88 static ssize_t
lpd_write(int lpd_fd
, char *buffer
, size_t length
);
89 static void sigterm_handler(int sig
);
93 * 'main()' - Send a file to the printer or server.
97 * printer-uri job-id user title copies options [file]
100 int /* O - Exit status */
101 main(int argc
, /* I - Number of command-line arguments (6 or 7) */
102 char *argv
[]) /* I - Command-line arguments */
104 const char *device_uri
; /* Device URI */
105 char scheme
[255], /* Scheme in URI */
106 hostname
[1024], /* Hostname */
107 username
[255], /* Username info */
108 resource
[1024], /* Resource info (printer name) */
109 *options
, /* Pointer to options */
110 *name
, /* Name of option */
111 *value
, /* Value of option */
112 sep
, /* Separator character */
113 *filename
, /* File to print */
114 title
[256]; /* Title string */
115 int port
; /* Port number */
116 char portname
[256]; /* Port name (string) */
117 http_addrlist_t
*addrlist
; /* List of addresses for printer */
118 int snmp_enabled
= 1; /* Is SNMP enabled? */
119 int snmp_fd
; /* SNMP socket */
120 int fd
; /* Print file */
121 int status
; /* Status of LPD job */
122 int mode
; /* Print mode */
123 int banner
; /* Print banner page? */
124 int format
; /* Print format */
125 int order
; /* Order of control/data files */
126 int reserve
; /* Reserve priviledged port? */
127 int sanitize_title
; /* Sanitize title string? */
128 int manual_copies
, /* Do manual copies? */
129 timeout
, /* Timeout */
130 contimeout
, /* Connection timeout */
131 copies
; /* Number of copies */
132 ssize_t bytes
= 0; /* Initial bytes read */
133 char buffer
[16384]; /* Initial print buffer */
134 #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
135 struct sigaction action
; /* Actions for POSIX signals */
136 #endif /* HAVE_SIGACTION && !HAVE_SIGSET */
137 int num_jobopts
; /* Number of job options */
138 cups_option_t
*jobopts
= NULL
; /* Job options */
142 * Make sure status messages are not buffered...
145 setbuf(stderr
, NULL
);
148 * Ignore SIGPIPE and catch SIGTERM signals...
152 sigset(SIGPIPE
, SIG_IGN
);
153 sigset(SIGTERM
, sigterm_handler
);
154 #elif defined(HAVE_SIGACTION)
155 memset(&action
, 0, sizeof(action
));
156 action
.sa_handler
= SIG_IGN
;
157 sigaction(SIGPIPE
, &action
, NULL
);
159 sigemptyset(&action
.sa_mask
);
160 sigaddset(&action
.sa_mask
, SIGTERM
);
161 action
.sa_handler
= sigterm_handler
;
162 sigaction(SIGTERM
, &action
, NULL
);
164 signal(SIGPIPE
, SIG_IGN
);
165 signal(SIGTERM
, sigterm_handler
);
166 #endif /* HAVE_SIGSET */
169 * Check command-line...
174 printf("network lpd \"Unknown\" \"%s\"\n",
175 _cupsLangString(cupsLangDefault(), _("LPD/LPR Host or Printer")));
176 return (CUPS_BACKEND_OK
);
178 else if (argc
< 6 || argc
> 7)
180 _cupsLangPrintf(stderr
,
181 _("Usage: %s job-id user title copies options [file]"),
183 return (CUPS_BACKEND_FAILED
);
186 num_jobopts
= cupsParseOptions(argv
[5], 0, &jobopts
);
189 * Extract the hostname and printer name from the URI...
192 while ((device_uri
= cupsBackendDeviceURI(argv
)) == NULL
)
194 _cupsLangPrintFilter(stderr
, "INFO", _("Unable to locate printer."));
197 if (getenv("CLASS") != NULL
)
198 return (CUPS_BACKEND_FAILED
);
201 httpSeparateURI(HTTP_URI_CODING_ALL
, device_uri
, scheme
, sizeof(scheme
),
202 username
, sizeof(username
), hostname
, sizeof(hostname
), &port
,
203 resource
, sizeof(resource
));
206 port
= 515; /* Default to port 515 */
211 * If no username is in the device URI, then use the print job user...
214 strlcpy(username
, argv
[2], sizeof(username
));
218 * See if there are any options...
221 mode
= MODE_STANDARD
;
224 order
= ORDER_CONTROL_DATA
;
225 reserve
= RESERVE_ANY
;
228 contimeout
= 7 * 24 * 60 * 60;
232 * We want to pass UTF-8 characters by default, not re-map them (3071945)
238 * Otherwise we want to re-map UTF-8 to "safe" characters by default...
242 #endif /* __APPLE__ */
244 if ((options
= strchr(resource
, '?')) != NULL
)
247 * Yup, terminate the device name string and move to the first
248 * character of the options...
265 while (*options
&& *options
!= '=' && *options
!= '+' && *options
!= '&')
268 if ((sep
= *options
) != '\0')
279 while (*options
&& *options
!= '+' && *options
!= '&')
289 * Process the option...
292 if (!_cups_strcasecmp(name
, "banner"))
298 banner
= !value
[0] || !_cups_strcasecmp(value
, "on") ||
299 !_cups_strcasecmp(value
, "yes") || !_cups_strcasecmp(value
, "true");
301 else if (!_cups_strcasecmp(name
, "format") && value
[0])
304 * Set output format...
307 if (strchr("cdfglnoprtv", value
[0]))
310 _cupsLangPrintFilter(stderr
, "ERROR",
311 _("Unknown format character: \"%c\"."),
314 else if (!_cups_strcasecmp(name
, "mode") && value
[0])
317 * Set control/data order...
320 if (!_cups_strcasecmp(value
, "standard"))
321 mode
= MODE_STANDARD
;
322 else if (!_cups_strcasecmp(value
, "stream"))
325 _cupsLangPrintFilter(stderr
, "ERROR",
326 _("Unknown print mode: \"%s\"."), value
);
328 else if (!_cups_strcasecmp(name
, "order") && value
[0])
331 * Set control/data order...
334 if (!_cups_strcasecmp(value
, "control,data"))
335 order
= ORDER_CONTROL_DATA
;
336 else if (!_cups_strcasecmp(value
, "data,control"))
337 order
= ORDER_DATA_CONTROL
;
339 _cupsLangPrintFilter(stderr
, "ERROR",
340 _("Unknown file order: \"%s\"."), value
);
342 else if (!_cups_strcasecmp(name
, "reserve"))
345 * Set port reservation mode...
348 if (!value
[0] || !_cups_strcasecmp(value
, "on") ||
349 !_cups_strcasecmp(value
, "yes") ||
350 !_cups_strcasecmp(value
, "true") ||
351 !_cups_strcasecmp(value
, "rfc1179"))
352 reserve
= RESERVE_RFC1179
;
353 else if (!_cups_strcasecmp(value
, "any"))
354 reserve
= RESERVE_ANY
;
356 reserve
= RESERVE_NONE
;
358 else if (!_cups_strcasecmp(name
, "manual_copies"))
361 * Set manual copies...
364 manual_copies
= !value
[0] || !_cups_strcasecmp(value
, "on") ||
365 !_cups_strcasecmp(value
, "yes") ||
366 !_cups_strcasecmp(value
, "true");
368 else if (!_cups_strcasecmp(name
, "sanitize_title"))
371 * Set sanitize title...
374 sanitize_title
= !value
[0] || !_cups_strcasecmp(value
, "on") ||
375 !_cups_strcasecmp(value
, "yes") ||
376 !_cups_strcasecmp(value
, "true");
378 else if (!_cups_strcasecmp(name
, "snmp"))
381 * Enable/disable SNMP stuff...
384 snmp_enabled
= !value
[0] || !_cups_strcasecmp(value
, "on") ||
385 !_cups_strcasecmp(value
, "yes") ||
386 !_cups_strcasecmp(value
, "true");
388 else if (!_cups_strcasecmp(name
, "timeout"))
395 timeout
= atoi(value
);
397 else if (!_cups_strcasecmp(name
, "contimeout"))
400 * Set the connection timeout...
404 contimeout
= atoi(value
);
409 if (mode
== MODE_STREAM
)
410 order
= ORDER_CONTROL_DATA
;
413 * Find the printer...
416 snprintf(portname
, sizeof(portname
), "%d", port
);
418 fputs("STATE: +connecting-to-device\n", stderr
);
419 fprintf(stderr
, "DEBUG: Looking up \"%s\"...\n", hostname
);
421 while ((addrlist
= httpAddrGetList(hostname
, AF_UNSPEC
, portname
)) == NULL
)
423 _cupsLangPrintFilter(stderr
, "INFO",
424 _("Unable to locate printer \"%s\"."), hostname
);
427 if (getenv("CLASS") != NULL
)
429 fputs("STATE: -connecting-to-device\n", stderr
);
430 exit(CUPS_BACKEND_FAILED
);
435 snmp_fd
= _cupsSNMPOpen(addrlist
->addr
.addr
.sa_family
);
440 * Wait for data from the filter...
445 if (!backendWaitLoop(snmp_fd
, &(addrlist
->addr
), 0, backendNetworkSideCB
))
446 return (CUPS_BACKEND_OK
);
447 else if (mode
== MODE_STANDARD
&&
448 (bytes
= read(0, buffer
, sizeof(buffer
))) <= 0)
449 return (CUPS_BACKEND_OK
);
453 * If we have 7 arguments, print the file named on the command-line.
454 * Otherwise, copy stdin to a temporary file and print the temporary
458 if (argc
== 6 && mode
== MODE_STANDARD
)
461 * Copy stdin to a temporary file...
464 if ((fd
= cupsTempFd(tmpfilename
, sizeof(tmpfilename
))) < 0)
466 perror("DEBUG: Unable to create temporary file");
467 return (CUPS_BACKEND_FAILED
);
470 _cupsLangPrintFilter(stderr
, "INFO", _("Copying print data."));
473 write(fd
, buffer
, (size_t)bytes
);
475 backendRunLoop(-1, fd
, snmp_fd
, &(addrlist
->addr
), 0, 0,
476 backendNetworkSideCB
);
481 * Stream from stdin...
490 fd
= open(filename
, O_RDONLY
);
494 _cupsLangPrintError("ERROR", _("Unable to open print file"));
495 return (CUPS_BACKEND_FAILED
);
500 * Sanitize the document title...
503 strlcpy(title
, argv
[3], sizeof(title
));
508 * Sanitize the title string so that we don't cause problems on
514 for (ptr
= title
; *ptr
; ptr
++)
515 if (!isalnum(*ptr
& 255) && !isspace(*ptr
& 255))
527 manual_copies
= atoi(argv
[4]);
533 copies
= atoi(argv
[4]);
536 status
= lpd_queue(hostname
, addrlist
, resource
+ 1, fd
, snmp_fd
, mode
,
537 username
, title
, copies
, banner
, format
, order
, reserve
,
538 manual_copies
, timeout
, contimeout
,
539 cupsGetOption("job-originating-host-name", num_jobopts
,
543 fprintf(stderr
, "PAGE: 1 %d\n", atoi(argv
[4]));
546 status
= lpd_queue(hostname
, addrlist
, resource
+ 1, fd
, snmp_fd
, mode
,
547 username
, title
, 1, banner
, format
, order
, reserve
, 1,
549 cupsGetOption("job-originating-host-name", num_jobopts
,
553 * Remove the temporary file if necessary...
563 _cupsSNMPClose(snmp_fd
);
566 * Return the queue status...
574 * 'cups_rresvport()' - A simple implementation of rresvport_af().
577 static int /* O - Socket or -1 on error */
578 cups_rresvport(int *port
, /* IO - Port number to bind to */
579 int family
) /* I - Address family */
581 http_addr_t addr
; /* Socket address */
582 int fd
; /* Socket file descriptor */
586 * Try to create an IPv4 socket...
589 if ((fd
= socket(family
, SOCK_STREAM
, 0)) < 0)
593 * Initialize the address buffer...
596 memset(&addr
, 0, sizeof(addr
));
597 addr
.addr
.sa_family
= (sa_family_t
)family
;
600 * Try to bind the socket to a reserved port...
606 * Set the port number...
609 _httpAddrSetPort(&addr
, *port
);
612 * Try binding the port to the socket; return if all is OK...
615 if (!bind(fd
, (struct sockaddr
*)&addr
, (socklen_t
)httpAddrLength(&addr
)))
619 * Stop if we have any error other than "address already in use"...
622 if (errno
!= EADDRINUSE
)
624 httpAddrClose(NULL
, fd
);
630 * Try the next port...
637 * Wasn't able to bind to a reserved port, so close the socket and return
652 * 'lpd_command()' - Send an LPR command sequence and wait for a reply.
655 static int /* O - Status of command */
656 lpd_command(int fd
, /* I - Socket connection to LPD host */
657 char *format
, /* I - printf()-style format string */
658 ...) /* I - Additional args as necessary */
660 va_list ap
; /* Argument pointer */
661 char buf
[1024]; /* Output buffer */
662 ssize_t bytes
; /* Number of bytes to output */
663 char status
; /* Status from command */
667 * Don't try to send commands if the job has been canceled...
674 * Format the string...
677 va_start(ap
, format
);
678 bytes
= vsnprintf(buf
, sizeof(buf
), format
, ap
);
681 fprintf(stderr
, "DEBUG: lpd_command %2.2x %s", buf
[0], buf
+ 1);
684 * Send the command...
687 fprintf(stderr
, "DEBUG: Sending command string (" CUPS_LLFMT
" bytes)...\n", CUPS_LLCAST bytes
);
689 if (lpd_write(fd
, buf
, (size_t)bytes
) < bytes
)
691 perror("DEBUG: Unable to send LPD command");
696 * Read back the status from the command and return it...
699 fputs("DEBUG: Reading command status...\n", stderr
);
701 if (recv(fd
, &status
, 1, 0) < 1)
703 _cupsLangPrintFilter(stderr
, "WARNING", _("The printer did not respond."));
704 status
= (char)errno
;
707 fprintf(stderr
, "DEBUG: lpd_command returning %d\n", status
);
714 * 'lpd_queue()' - Queue a file using the Line Printer Daemon protocol.
717 static int /* O - Zero on success, non-zero on failure */
718 lpd_queue(const char *hostname
, /* I - Host to connect to */
719 http_addrlist_t
*addrlist
, /* I - List of host addresses */
720 const char *printer
, /* I - Printer/queue name */
721 int print_fd
, /* I - File to print */
722 int snmp_fd
, /* I - SNMP socket */
723 int mode
, /* I - Print mode */
724 const char *user
, /* I - Requesting user */
725 const char *title
, /* I - Job title */
726 int copies
, /* I - Number of copies */
727 int banner
, /* I - Print LPD banner? */
728 int format
, /* I - Format specifier */
729 int order
, /* I - Order of data/control files */
730 int reserve
, /* I - Reserve ports? */
731 int manual_copies
,/* I - Do copies by hand... */
732 int timeout
, /* I - Timeout... */
733 int contimeout
, /* I - Connection timeout */
734 const char *orighost
) /* I - job-originating-host-name */
736 char localhost
[255]; /* Local host name */
737 int error
; /* Error number */
738 struct stat filestats
; /* File statistics */
739 int lport
; /* LPD connection local port */
740 int fd
; /* LPD socket */
741 char control
[10240], /* LPD control 'file' */
742 *cptr
; /* Pointer into control file string */
743 char status
; /* Status byte from command */
744 int delay
; /* Delay for retries... */
745 char addrname
[256]; /* Address name */
746 http_addrlist_t
*addr
; /* Socket address */
747 int have_supplies
; /* Printer supports supply levels? */
748 int copy
; /* Copies written */
749 time_t start_time
; /* Time of first connect */
750 ssize_t nbytes
; /* Number of bytes written */
751 off_t tbytes
; /* Total bytes written */
752 char buffer
[32768]; /* Output buffer */
754 DWORD tv
; /* Timeout in milliseconds */
756 struct timeval tv
; /* Timeout in secs and usecs */
761 * Remember when we started trying to connect to the printer...
764 start_time
= time(NULL
);
767 * Loop forever trying to print the file...
773 * First try to reserve a port for this connection...
776 fprintf(stderr
, "DEBUG: Connecting to %s:%d for printer %s\n", hostname
,
777 httpAddrPort(&(addrlist
->addr
)), printer
);
778 _cupsLangPrintFilter(stderr
, "INFO", _("Connecting to printer."));
780 for (lport
= reserve
== RESERVE_RFC1179
? 732 : 1024, addr
= addrlist
,
785 * Stop if this job has been canceled...
789 return (CUPS_BACKEND_FAILED
);
792 * Choose the next priviledged port...
800 if (lport
< 721 && reserve
== RESERVE_RFC1179
)
806 if (geteuid() || !reserve
)
808 if (getuid() || !reserve
)
809 #endif /* HAVE_GETEUID */
812 * Just create a regular socket...
815 if ((fd
= socket(addr
->addr
.addr
.sa_family
, SOCK_STREAM
, 0)) < 0)
817 perror("DEBUG: Unable to create socket");
828 * We're running as root and want to comply with RFC 1179. Reserve a
829 * priviledged lport between 721 and 731...
832 if ((fd
= cups_rresvport(&lport
, addr
->addr
.addr
.sa_family
)) < 0)
834 perror("DEBUG: Unable to reserve port");
842 * Connect to the printer or server...
849 return (CUPS_BACKEND_FAILED
);
852 if (!connect(fd
, &(addr
->addr
.addr
), (socklen_t
)httpAddrLength(&(addr
->addr
))))
861 if (getenv("CLASS") != NULL
)
864 * If the CLASS environment variable is set, the job was submitted
865 * to a class and not to a specific queue. In this case, we want
866 * to abort immediately so that the job can be requeued on the next
867 * available printer in the class.
870 _cupsLangPrintFilter(stderr
, "INFO",
871 _("Unable to contact printer, queuing on next "
872 "printer in class."));
875 * Sleep 5 seconds to keep the job from requeuing too rapidly...
880 return (CUPS_BACKEND_FAILED
);
883 fprintf(stderr
, "DEBUG: Connection error: %s\n", strerror(error
));
885 if (error
== ECONNREFUSED
|| error
== EHOSTDOWN
||
886 error
== EHOSTUNREACH
)
888 if (contimeout
&& (time(NULL
) - start_time
) > contimeout
)
890 _cupsLangPrintFilter(stderr
, "ERROR",
891 _("The printer is not responding."));
892 return (CUPS_BACKEND_FAILED
);
898 _cupsLangPrintFilter(stderr
, "WARNING",
899 _("The printer may not exist or "
900 "is unavailable at this time."));
904 _cupsLangPrintFilter(stderr
, "WARNING",
905 _("The printer is unreachable at "
911 _cupsLangPrintFilter(stderr
, "WARNING",
912 _("The printer is in use."));
916 sleep((unsigned)delay
);
921 else if (error
== EADDRINUSE
)
924 * Try on another port...
931 _cupsLangPrintFilter(stderr
, "ERROR",
932 _("The printer is not responding."));
942 tv
= (DWORD
)(timeout
* 1000);
944 setsockopt(fd
, SOL_SOCKET
, SO_RCVTIMEO
, (char *)&tv
, sizeof(tv
));
945 setsockopt(fd
, SOL_SOCKET
, SO_SNDTIMEO
, (char *)&tv
, sizeof(tv
));
950 setsockopt(fd
, SOL_SOCKET
, SO_RCVTIMEO
, &tv
, sizeof(tv
));
951 setsockopt(fd
, SOL_SOCKET
, SO_SNDTIMEO
, &tv
, sizeof(tv
));
954 fputs("STATE: -connecting-to-device\n", stderr
);
955 _cupsLangPrintFilter(stderr
, "INFO", _("Connected to printer."));
957 fprintf(stderr
, "DEBUG: Connected to %s:%d (local port %d)...\n",
958 httpAddrString(&(addr
->addr
), addrname
, sizeof(addrname
)),
959 httpAddrPort(&(addr
->addr
)), lport
);
962 * See if the printer supports SNMP...
966 have_supplies
= !backendSNMPSupplies(snmp_fd
, &(addrlist
->addr
), NULL
,
972 * Check for side-channel requests...
975 backendCheckSideChannel(snmp_fd
, &(addrlist
->addr
));
978 * Next, open the print file and figure out its size...
984 * Use the size from the print file...
987 if (fstat(print_fd
, &filestats
))
991 perror("DEBUG: unable to stat print file");
992 return (CUPS_BACKEND_FAILED
);
995 filestats
.st_size
*= manual_copies
;
1000 * Use a "very large value" for the size so that the printer will
1001 * keep printing until we close the connection...
1004 #ifdef _LARGEFILE_SOURCE
1005 filestats
.st_size
= (size_t)(999999999999.0);
1007 filestats
.st_size
= 2147483647;
1008 #endif /* _LARGEFILE_SOURCE */
1012 * Send a job header to the printer, specifying no banner page and
1016 if (lpd_command(fd
, "\002%s\n",
1017 printer
)) /* Receive print job(s) */
1020 return (CUPS_BACKEND_FAILED
);
1023 if (orighost
&& _cups_strcasecmp(orighost
, "localhost"))
1024 strlcpy(localhost
, orighost
, sizeof(localhost
));
1026 httpGetHostname(NULL
, localhost
, sizeof(localhost
));
1028 snprintf(control
, sizeof(control
),
1029 "H%.31s\n" /* RFC 1179, Section 7.2 - host name <= 31 chars */
1030 "P%.31s\n" /* RFC 1179, Section 7.2 - user name <= 31 chars */
1031 "J%.99s\n", /* RFC 1179, Section 7.2 - job name <= 99 chars */
1032 localhost
, user
, title
);
1033 cptr
= control
+ strlen(control
);
1037 snprintf(cptr
, sizeof(control
) - (size_t)(cptr
- control
),
1038 "C%.31s\n" /* RFC 1179, Section 7.2 - class name <= 31 chars */
1041 cptr
+= strlen(cptr
);
1046 snprintf(cptr
, sizeof(control
) - (size_t)(cptr
- control
), "%cdfA%03d%.15s\n",
1047 format
, (int)getpid() % 1000, localhost
);
1048 cptr
+= strlen(cptr
);
1052 snprintf(cptr
, sizeof(control
) - (size_t)(cptr
- control
),
1054 "N%.131s\n", /* RFC 1179, Section 7.2 - sourcefile name <= 131 chars */
1055 (int)getpid() % 1000, localhost
, title
);
1057 fprintf(stderr
, "DEBUG: Control file is:\n%s", control
);
1059 if (order
== ORDER_CONTROL_DATA
)
1062 * Check for side-channel requests...
1065 backendCheckSideChannel(snmp_fd
, &(addr
->addr
));
1068 * Send the control file...
1071 if (lpd_command(fd
, "\002%d cfA%03.3d%.15s\n", strlen(control
),
1072 (int)getpid() % 1000, localhost
))
1076 return (CUPS_BACKEND_FAILED
);
1079 fprintf(stderr
, "DEBUG: Sending control file (%u bytes)\n",
1080 (unsigned)strlen(control
));
1082 if ((size_t)lpd_write(fd
, control
, strlen(control
) + 1) < (strlen(control
) + 1))
1084 status
= (char)errno
;
1085 perror("DEBUG: Unable to write control file");
1090 if (read(fd
, &status
, 1) < 1)
1092 _cupsLangPrintFilter(stderr
, "WARNING",
1093 _("The printer did not respond."));
1094 status
= (char)errno
;
1099 _cupsLangPrintFilter(stderr
, "ERROR",
1100 _("Remote host did not accept control file (%d)."),
1103 _cupsLangPrintFilter(stderr
, "INFO",
1104 _("Control file sent successfully."));
1112 * Check for side-channel requests...
1115 backendCheckSideChannel(snmp_fd
, &(addr
->addr
));
1118 * Send the print file...
1121 if (lpd_command(fd
, "\003" CUPS_LLFMT
" dfA%03.3d%.15s\n",
1122 CUPS_LLCAST filestats
.st_size
, (int)getpid() % 1000,
1127 return (CUPS_BACKEND_FAILED
);
1130 fprintf(stderr
, "DEBUG: Sending data file (" CUPS_LLFMT
" bytes)\n",
1131 CUPS_LLCAST filestats
.st_size
);
1134 for (copy
= 0; copy
< manual_copies
; copy
++)
1136 lseek(print_fd
, 0, SEEK_SET
);
1138 while ((nbytes
= read(print_fd
, buffer
, sizeof(buffer
))) > 0)
1140 _cupsLangPrintFilter(stderr
, "INFO",
1141 _("Spooling job, %.0f%% complete."),
1142 100.0 * tbytes
/ filestats
.st_size
);
1144 if (lpd_write(fd
, buffer
, (size_t)nbytes
) < nbytes
)
1146 perror("DEBUG: Unable to send print file to printer");
1154 if (mode
== MODE_STANDARD
)
1156 if (tbytes
< filestats
.st_size
)
1157 status
= (char)errno
;
1158 else if (lpd_write(fd
, "", 1) < 1)
1160 perror("DEBUG: Unable to send trailing nul to printer");
1161 status
= (char)errno
;
1166 * Read the status byte from the printer; if we can't read the byte
1167 * back now, we should set status to "errno", however at this point
1168 * we know the printer got the whole file and we don't necessarily
1169 * want to requeue it over and over...
1172 if (recv(fd
, &status
, 1, 0) < 1)
1174 _cupsLangPrintFilter(stderr
, "WARNING",
1175 _("The printer did not respond."));
1184 _cupsLangPrintFilter(stderr
, "ERROR",
1185 _("Remote host did not accept data file (%d)."),
1188 _cupsLangPrintFilter(stderr
, "INFO",
1189 _("Data file sent successfully."));
1192 if (status
== 0 && order
== ORDER_DATA_CONTROL
)
1195 * Check for side-channel requests...
1198 backendCheckSideChannel(snmp_fd
, &(addr
->addr
));
1201 * Send control file...
1204 if (lpd_command(fd
, "\002%d cfA%03.3d%.15s\n", strlen(control
),
1205 (int)getpid() % 1000, localhost
))
1209 return (CUPS_BACKEND_FAILED
);
1212 fprintf(stderr
, "DEBUG: Sending control file (%lu bytes)\n",
1213 (unsigned long)strlen(control
));
1215 if ((size_t)lpd_write(fd
, control
, strlen(control
) + 1) < (strlen(control
) + 1))
1217 status
= (char)errno
;
1218 perror("DEBUG: Unable to write control file");
1222 if (read(fd
, &status
, 1) < 1)
1224 _cupsLangPrintFilter(stderr
, "WARNING",
1225 _("The printer did not respond."));
1226 status
= (char)errno
;
1231 _cupsLangPrintFilter(stderr
, "ERROR",
1232 _("Remote host did not accept control file (%d)."),
1235 _cupsLangPrintFilter(stderr
, "INFO",
1236 _("Control file sent successfully."));
1239 fputs("STATE: +cups-waiting-for-job-completed\n", stderr
);
1242 * Collect the final supply levels as needed...
1246 backendSNMPSupplies(snmp_fd
, &(addr
->addr
), NULL
, NULL
);
1249 * Close the socket connection and input file...
1255 return (CUPS_BACKEND_OK
);
1258 * Waiting for a retry...
1265 * If we get here, then the job has been canceled...
1268 return (CUPS_BACKEND_FAILED
);
1273 * 'lpd_write()' - Write a buffer of data to an LPD server.
1276 static ssize_t
/* O - Number of bytes written or -1 on error */
1277 lpd_write(int lpd_fd
, /* I - LPD socket */
1278 char *buffer
, /* I - Buffer to write */
1279 size_t length
) /* I - Number of bytes to write */
1281 ssize_t bytes
, /* Number of bytes written */
1282 total
; /* Total number of bytes written */
1289 while ((bytes
= send(lpd_fd
, buffer
, length
- (size_t)total
, 0)) >= 0)
1294 if ((size_t)total
== length
)
1306 * 'sigterm_handler()' - Handle 'terminate' signals that stop the backend.
1310 sigterm_handler(int sig
) /* I - Signal */
1312 (void)sig
; /* remove compiler warnings... */