]>
git.ipfire.org Git - thirdparty/cups.git/blob - backend/lpd.c
d73d1bcf0894ee5b871ccbc6087e594f80ab3f17
4 * Line Printer Daemon backend for CUPS.
6 * Copyright 2007-2012 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 * main() - Send a file to the printer or server.
20 * lpd_command() - Send an LPR command sequence and wait for a reply.
21 * lpd_queue() - Queue a file using the Line Printer Daemon protocol.
22 * lpd_write() - Write a buffer of data to an LPD server.
23 * rresvport_af() - A simple implementation of rresvport_af().
24 * sigterm_handler() - Handle 'terminate' signals that stop the backend.
28 * Include necessary headers.
31 #include <cups/http-private.h>
32 #include "backend-private.h"
34 #include <sys/types.h>
41 # include <sys/socket.h>
42 # include <netinet/in.h>
43 # include <arpa/inet.h>
47 # include <CoreFoundation/CFNumber.h>
48 # include <CoreFoundation/CFPreferences.h>
49 #endif /* __APPLE__ */
56 static char tmpfilename
[1024] = ""; /* Temporary spool file name */
57 static int abort_job
= 0; /* Non-zero if we get SIGTERM */
64 #define MODE_STANDARD 0 /* Queue a copy */
65 #define MODE_STREAM 1 /* Stream a copy */
69 * The order for control and data files in LPD requests...
72 #define ORDER_CONTROL_DATA 0 /* Control file first, then data */
73 #define ORDER_DATA_CONTROL 1 /* Data file first, then control */
80 #define RESERVE_NONE 0 /* Don't reserve a priviledged port */
81 #define RESERVE_RFC1179 1 /* Reserve port 721-731 */
82 #define RESERVE_ANY 2 /* Reserve port 1-1023 */
89 static int lpd_command(int lpd_fd
, char *format
, ...);
90 static int lpd_queue(const char *hostname
, http_addrlist_t
*addrlist
,
91 const char *printer
, int print_fd
, int snmp_fd
,
92 int mode
, const char *user
, const char *title
,
93 int copies
, int banner
, int format
, int order
,
94 int reserve
, int manual_copies
, int timeout
,
95 int contimeout
, const char *orighost
);
96 static int lpd_write(int lpd_fd
, char *buffer
, int length
);
97 #ifndef HAVE_RRESVPORT_AF
98 static int rresvport_af(int *port
, int family
);
99 #endif /* !HAVE_RRESVPORT_AF */
100 static void sigterm_handler(int sig
);
104 * 'main()' - Send a file to the printer or server.
108 * printer-uri job-id user title copies options [file]
111 int /* O - Exit status */
112 main(int argc
, /* I - Number of command-line arguments (6 or 7) */
113 char *argv
[]) /* I - Command-line arguments */
115 const char *device_uri
; /* Device URI */
116 char scheme
[255], /* Scheme in URI */
117 hostname
[1024], /* Hostname */
118 username
[255], /* Username info */
119 resource
[1024], /* Resource info (printer name) */
120 *options
, /* Pointer to options */
121 *name
, /* Name of option */
122 *value
, /* Value of option */
123 sep
, /* Separator character */
124 *filename
, /* File to print */
125 title
[256]; /* Title string */
126 int port
; /* Port number */
127 char portname
[256]; /* Port name (string) */
128 http_addrlist_t
*addrlist
; /* List of addresses for printer */
129 int snmp_enabled
= 1; /* Is SNMP enabled? */
130 int snmp_fd
; /* SNMP socket */
131 int fd
; /* Print file */
132 int status
; /* Status of LPD job */
133 int mode
; /* Print mode */
134 int banner
; /* Print banner page? */
135 int format
; /* Print format */
136 int order
; /* Order of control/data files */
137 int reserve
; /* Reserve priviledged port? */
138 int sanitize_title
; /* Sanitize title string? */
139 int manual_copies
, /* Do manual copies? */
140 timeout
, /* Timeout */
141 contimeout
, /* Connection timeout */
142 copies
; /* Number of copies */
143 ssize_t bytes
= 0; /* Initial bytes read */
144 char buffer
[16384]; /* Initial print buffer */
145 #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
146 struct sigaction action
; /* Actions for POSIX signals */
147 #endif /* HAVE_SIGACTION && !HAVE_SIGSET */
148 int num_jobopts
; /* Number of job options */
149 cups_option_t
*jobopts
= NULL
; /* Job options */
153 * Make sure status messages are not buffered...
156 setbuf(stderr
, NULL
);
159 * Ignore SIGPIPE and catch SIGTERM signals...
163 sigset(SIGPIPE
, SIG_IGN
);
164 sigset(SIGTERM
, sigterm_handler
);
165 #elif defined(HAVE_SIGACTION)
166 memset(&action
, 0, sizeof(action
));
167 action
.sa_handler
= SIG_IGN
;
168 sigaction(SIGPIPE
, &action
, NULL
);
170 sigemptyset(&action
.sa_mask
);
171 sigaddset(&action
.sa_mask
, SIGTERM
);
172 action
.sa_handler
= sigterm_handler
;
173 sigaction(SIGTERM
, &action
, NULL
);
175 signal(SIGPIPE
, SIG_IGN
);
176 signal(SIGTERM
, sigterm_handler
);
177 #endif /* HAVE_SIGSET */
180 * Check command-line...
185 printf("network lpd \"Unknown\" \"%s\"\n",
186 _cupsLangString(cupsLangDefault(), _("LPD/LPR Host or Printer")));
187 return (CUPS_BACKEND_OK
);
189 else if (argc
< 6 || argc
> 7)
191 _cupsLangPrintf(stderr
,
192 _("Usage: %s job-id user title copies options [file]"),
194 return (CUPS_BACKEND_FAILED
);
197 num_jobopts
= cupsParseOptions(argv
[5], 0, &jobopts
);
200 * Extract the hostname and printer name from the URI...
203 while ((device_uri
= cupsBackendDeviceURI(argv
)) == NULL
)
205 _cupsLangPrintFilter(stderr
, "INFO", _("Unable to locate printer."));
208 if (getenv("CLASS") != NULL
)
209 return (CUPS_BACKEND_FAILED
);
212 httpSeparateURI(HTTP_URI_CODING_ALL
, device_uri
, scheme
, sizeof(scheme
),
213 username
, sizeof(username
), hostname
, sizeof(hostname
), &port
,
214 resource
, sizeof(resource
));
217 port
= 515; /* Default to port 515 */
222 * If no username is in the device URI, then use the print job user...
225 strlcpy(username
, argv
[2], sizeof(username
));
229 * See if there are any options...
232 mode
= MODE_STANDARD
;
235 order
= ORDER_CONTROL_DATA
;
236 reserve
= RESERVE_ANY
;
239 contimeout
= 7 * 24 * 60 * 60;
243 * We want to pass UTF-8 characters by default, not re-map them (3071945)
249 * Otherwise we want to re-map UTF-8 to "safe" characters by default...
253 #endif /* __APPLE__ */
255 if ((options
= strchr(resource
, '?')) != NULL
)
258 * Yup, terminate the device name string and move to the first
259 * character of the options...
276 while (*options
&& *options
!= '=' && *options
!= '+' && *options
!= '&')
279 if ((sep
= *options
) != '\0')
290 while (*options
&& *options
!= '+' && *options
!= '&')
300 * Process the option...
303 if (!_cups_strcasecmp(name
, "banner"))
309 banner
= !value
[0] || !_cups_strcasecmp(value
, "on") ||
310 !_cups_strcasecmp(value
, "yes") || !_cups_strcasecmp(value
, "true");
312 else if (!_cups_strcasecmp(name
, "format") && value
[0])
315 * Set output format...
318 if (strchr("cdfglnoprtv", value
[0]))
321 _cupsLangPrintFilter(stderr
, "ERROR",
322 _("Unknown format character: \"%c\"."),
325 else if (!_cups_strcasecmp(name
, "mode") && value
[0])
328 * Set control/data order...
331 if (!_cups_strcasecmp(value
, "standard"))
332 mode
= MODE_STANDARD
;
333 else if (!_cups_strcasecmp(value
, "stream"))
336 _cupsLangPrintFilter(stderr
, "ERROR",
337 _("Unknown print mode: \"%s\"."), value
);
339 else if (!_cups_strcasecmp(name
, "order") && value
[0])
342 * Set control/data order...
345 if (!_cups_strcasecmp(value
, "control,data"))
346 order
= ORDER_CONTROL_DATA
;
347 else if (!_cups_strcasecmp(value
, "data,control"))
348 order
= ORDER_DATA_CONTROL
;
350 _cupsLangPrintFilter(stderr
, "ERROR",
351 _("Unknown file order: \"%s\"."), value
);
353 else if (!_cups_strcasecmp(name
, "reserve"))
356 * Set port reservation mode...
359 if (!value
[0] || !_cups_strcasecmp(value
, "on") ||
360 !_cups_strcasecmp(value
, "yes") ||
361 !_cups_strcasecmp(value
, "true") ||
362 !_cups_strcasecmp(value
, "rfc1179"))
363 reserve
= RESERVE_RFC1179
;
364 else if (!_cups_strcasecmp(value
, "any"))
365 reserve
= RESERVE_ANY
;
367 reserve
= RESERVE_NONE
;
369 else if (!_cups_strcasecmp(name
, "manual_copies"))
372 * Set manual copies...
375 manual_copies
= !value
[0] || !_cups_strcasecmp(value
, "on") ||
376 !_cups_strcasecmp(value
, "yes") ||
377 !_cups_strcasecmp(value
, "true");
379 else if (!_cups_strcasecmp(name
, "sanitize_title"))
382 * Set sanitize title...
385 sanitize_title
= !value
[0] || !_cups_strcasecmp(value
, "on") ||
386 !_cups_strcasecmp(value
, "yes") ||
387 !_cups_strcasecmp(value
, "true");
389 else if (!_cups_strcasecmp(name
, "snmp"))
392 * Enable/disable SNMP stuff...
395 snmp_enabled
= !value
[0] || !_cups_strcasecmp(value
, "on") ||
396 _cups_strcasecmp(value
, "yes") ||
397 _cups_strcasecmp(value
, "true");
399 else if (!_cups_strcasecmp(name
, "timeout"))
406 timeout
= atoi(value
);
408 else if (!_cups_strcasecmp(name
, "contimeout"))
411 * Set the connection timeout...
415 contimeout
= atoi(value
);
420 if (mode
== MODE_STREAM
)
421 order
= ORDER_CONTROL_DATA
;
424 * Find the printer...
427 snprintf(portname
, sizeof(portname
), "%d", port
);
429 fputs("STATE: +connecting-to-device\n", stderr
);
430 fprintf(stderr
, "DEBUG: Looking up \"%s\"...\n", hostname
);
432 while ((addrlist
= httpAddrGetList(hostname
, AF_UNSPEC
, portname
)) == NULL
)
434 _cupsLangPrintFilter(stderr
, "INFO",
435 _("Unable to locate printer \"%s\"."), hostname
);
438 if (getenv("CLASS") != NULL
)
440 fputs("STATE: -connecting-to-device\n", stderr
);
441 exit(CUPS_BACKEND_FAILED
);
446 snmp_fd
= _cupsSNMPOpen(addrlist
->addr
.addr
.sa_family
);
451 * Wait for data from the filter...
456 if (!backendWaitLoop(snmp_fd
, &(addrlist
->addr
), 0, backendNetworkSideCB
))
457 return (CUPS_BACKEND_OK
);
458 else if (mode
== MODE_STANDARD
&&
459 (bytes
= read(0, buffer
, sizeof(buffer
))) <= 0)
460 return (CUPS_BACKEND_OK
);
464 * If we have 7 arguments, print the file named on the command-line.
465 * Otherwise, copy stdin to a temporary file and print the temporary
469 if (argc
== 6 && mode
== MODE_STANDARD
)
472 * Copy stdin to a temporary file...
475 if ((fd
= cupsTempFd(tmpfilename
, sizeof(tmpfilename
))) < 0)
477 perror("DEBUG: Unable to create temporary file");
478 return (CUPS_BACKEND_FAILED
);
481 _cupsLangPrintFilter(stderr
, "INFO", _("Copying print data."));
484 write(fd
, buffer
, bytes
);
486 backendRunLoop(-1, fd
, snmp_fd
, &(addrlist
->addr
), 0, 0,
487 backendNetworkSideCB
);
492 * Stream from stdin...
501 fd
= open(filename
, O_RDONLY
);
505 _cupsLangPrintError("ERROR", _("Unable to open print file"));
506 return (CUPS_BACKEND_FAILED
);
511 * Sanitize the document title...
514 strlcpy(title
, argv
[3], sizeof(title
));
519 * Sanitize the title string so that we don't cause problems on
525 for (ptr
= title
; *ptr
; ptr
++)
526 if (!isalnum(*ptr
& 255) && !isspace(*ptr
& 255))
538 manual_copies
= atoi(argv
[4]);
544 copies
= atoi(argv
[4]);
547 status
= lpd_queue(hostname
, addrlist
, resource
+ 1, fd
, snmp_fd
, mode
,
548 username
, title
, copies
, banner
, format
, order
, reserve
,
549 manual_copies
, timeout
, contimeout
,
550 cupsGetOption("job-originating-host-name", num_jobopts
,
554 fprintf(stderr
, "PAGE: 1 %d\n", atoi(argv
[4]));
557 status
= lpd_queue(hostname
, addrlist
, resource
+ 1, fd
, snmp_fd
, mode
,
558 username
, title
, 1, banner
, format
, order
, reserve
, 1,
560 cupsGetOption("job-originating-host-name", num_jobopts
,
564 * Remove the temporary file if necessary...
574 _cupsSNMPClose(snmp_fd
);
577 * Return the queue status...
585 * 'lpd_command()' - Send an LPR command sequence and wait for a reply.
588 static int /* O - Status of command */
589 lpd_command(int fd
, /* I - Socket connection to LPD host */
590 char *format
, /* I - printf()-style format string */
591 ...) /* I - Additional args as necessary */
593 va_list ap
; /* Argument pointer */
594 char buf
[1024]; /* Output buffer */
595 int bytes
; /* Number of bytes to output */
596 char status
; /* Status from command */
600 * Don't try to send commands if the job has been canceled...
607 * Format the string...
610 va_start(ap
, format
);
611 bytes
= vsnprintf(buf
, sizeof(buf
), format
, ap
);
614 fprintf(stderr
, "DEBUG: lpd_command %2.2x %s", buf
[0], buf
+ 1);
617 * Send the command...
620 fprintf(stderr
, "DEBUG: Sending command string (%d bytes)...\n", bytes
);
622 if (lpd_write(fd
, buf
, bytes
) < bytes
)
624 perror("DEBUG: Unable to send LPD command");
629 * Read back the status from the command and return it...
632 fputs("DEBUG: Reading command status...\n", stderr
);
634 if (recv(fd
, &status
, 1, 0) < 1)
636 _cupsLangPrintFilter(stderr
, "WARNING", _("The printer did not respond."));
640 fprintf(stderr
, "DEBUG: lpd_command returning %d\n", status
);
647 * 'lpd_queue()' - Queue a file using the Line Printer Daemon protocol.
650 static int /* O - Zero on success, non-zero on failure */
651 lpd_queue(const char *hostname
, /* I - Host to connect to */
652 http_addrlist_t
*addrlist
, /* I - List of host addresses */
653 const char *printer
, /* I - Printer/queue name */
654 int print_fd
, /* I - File to print */
655 int snmp_fd
, /* I - SNMP socket */
656 int mode
, /* I - Print mode */
657 const char *user
, /* I - Requesting user */
658 const char *title
, /* I - Job title */
659 int copies
, /* I - Number of copies */
660 int banner
, /* I - Print LPD banner? */
661 int format
, /* I - Format specifier */
662 int order
, /* I - Order of data/control files */
663 int reserve
, /* I - Reserve ports? */
664 int manual_copies
,/* I - Do copies by hand... */
665 int timeout
, /* I - Timeout... */
666 int contimeout
, /* I - Connection timeout */
667 const char *orighost
) /* I - job-originating-host-name */
669 char localhost
[255]; /* Local host name */
670 int error
; /* Error number */
671 struct stat filestats
; /* File statistics */
672 int lport
; /* LPD connection local port */
673 int fd
; /* LPD socket */
674 char control
[10240], /* LPD control 'file' */
675 *cptr
; /* Pointer into control file string */
676 char status
; /* Status byte from command */
677 int delay
; /* Delay for retries... */
678 char addrname
[256]; /* Address name */
679 http_addrlist_t
*addr
; /* Socket address */
680 int have_supplies
; /* Printer supports supply levels? */
681 int copy
; /* Copies written */
682 time_t start_time
; /* Time of first connect */
683 size_t nbytes
; /* Number of bytes written */
684 off_t tbytes
; /* Total bytes written */
685 char buffer
[32768]; /* Output buffer */
687 DWORD tv
; /* Timeout in milliseconds */
689 struct timeval tv
; /* Timeout in secs and usecs */
694 * Remember when we started trying to connect to the printer...
697 start_time
= time(NULL
);
700 * Loop forever trying to print the file...
706 * First try to reserve a port for this connection...
709 fprintf(stderr
, "DEBUG: Connecting to %s:%d for printer %s\n", hostname
,
710 httpAddrPort(&(addrlist
->addr
)), printer
);
711 _cupsLangPrintFilter(stderr
, "INFO", _("Connecting to printer."));
713 for (lport
= reserve
== RESERVE_RFC1179
? 732 : 1024, addr
= addrlist
,
718 * Stop if this job has been canceled...
722 return (CUPS_BACKEND_FAILED
);
725 * Choose the next priviledged port...
733 if (lport
< 721 && reserve
== RESERVE_RFC1179
)
739 if (geteuid() || !reserve
)
741 if (getuid() || !reserve
)
742 #endif /* HAVE_GETEUID */
745 * Just create a regular socket...
748 if ((fd
= socket(addr
->addr
.addr
.sa_family
, SOCK_STREAM
, 0)) < 0)
750 perror("DEBUG: Unable to create socket");
761 * We're running as root and want to comply with RFC 1179. Reserve a
762 * priviledged lport between 721 and 731...
765 if ((fd
= rresvport_af(&lport
, addr
->addr
.addr
.sa_family
)) < 0)
767 perror("DEBUG: Unable to reserve port");
775 * Connect to the printer or server...
782 return (CUPS_BACKEND_FAILED
);
785 if (!connect(fd
, &(addr
->addr
.addr
), httpAddrLength(&(addr
->addr
))))
794 if (getenv("CLASS") != NULL
)
797 * If the CLASS environment variable is set, the job was submitted
798 * to a class and not to a specific queue. In this case, we want
799 * to abort immediately so that the job can be requeued on the next
800 * available printer in the class.
803 _cupsLangPrintFilter(stderr
, "INFO",
804 _("Unable to contact printer, queuing on next "
805 "printer in class."));
808 * Sleep 5 seconds to keep the job from requeuing too rapidly...
813 return (CUPS_BACKEND_FAILED
);
816 fprintf(stderr
, "DEBUG: Connection error: %s\n", strerror(error
));
818 if (error
== ECONNREFUSED
|| error
== EHOSTDOWN
||
819 error
== EHOSTUNREACH
)
821 if (contimeout
&& (time(NULL
) - start_time
) > contimeout
)
823 _cupsLangPrintFilter(stderr
, "ERROR",
824 _("The printer is not responding."));
825 return (CUPS_BACKEND_FAILED
);
831 _cupsLangPrintFilter(stderr
, "WARNING",
832 _("The printer may not exist or "
833 "is unavailable at this time."));
837 _cupsLangPrintFilter(stderr
, "WARNING",
838 _("The printer is unreachable at "
844 _cupsLangPrintFilter(stderr
, "WARNING",
845 _("The printer is in use."));
854 else if (error
== EADDRINUSE
)
857 * Try on another port...
864 _cupsLangPrintFilter(stderr
, "ERROR",
865 _("The printer is not responding."));
875 tv
= (DWORD
)(timeout
* 1000);
877 setsockopt(fd
, SOL_SOCKET
, SO_RCVTIMEO
, (char *)&tv
, sizeof(tv
));
878 setsockopt(fd
, SOL_SOCKET
, SO_SNDTIMEO
, (char *)&tv
, sizeof(tv
));
883 setsockopt(fd
, SOL_SOCKET
, SO_RCVTIMEO
, &tv
, sizeof(tv
));
884 setsockopt(fd
, SOL_SOCKET
, SO_SNDTIMEO
, &tv
, sizeof(tv
));
887 fputs("STATE: -connecting-to-device\n", stderr
);
888 _cupsLangPrintFilter(stderr
, "INFO", _("Connected to printer."));
890 fprintf(stderr
, "DEBUG: Connected to %s:%d (local port %d)...\n",
891 httpAddrString(&(addr
->addr
), addrname
, sizeof(addrname
)),
892 httpAddrPort(&(addr
->addr
)), lport
);
895 * See if the printer supports SNMP...
899 have_supplies
= !backendSNMPSupplies(snmp_fd
, &(addrlist
->addr
), NULL
,
905 * Check for side-channel requests...
908 backendCheckSideChannel(snmp_fd
, &(addrlist
->addr
));
911 * Next, open the print file and figure out its size...
917 * Use the size from the print file...
920 if (fstat(print_fd
, &filestats
))
924 perror("DEBUG: unable to stat print file");
925 return (CUPS_BACKEND_FAILED
);
928 filestats
.st_size
*= manual_copies
;
933 * Use a "very large value" for the size so that the printer will
934 * keep printing until we close the connection...
937 #ifdef _LARGEFILE_SOURCE
938 filestats
.st_size
= (size_t)(999999999999.0);
940 filestats
.st_size
= 2147483647;
941 #endif /* _LARGEFILE_SOURCE */
945 * Send a job header to the printer, specifying no banner page and
949 if (lpd_command(fd
, "\002%s\n",
950 printer
)) /* Receive print job(s) */
953 return (CUPS_BACKEND_FAILED
);
956 if (orighost
&& _cups_strcasecmp(orighost
, "localhost"))
957 strlcpy(localhost
, orighost
, sizeof(localhost
));
959 httpGetHostname(NULL
, localhost
, sizeof(localhost
));
961 snprintf(control
, sizeof(control
),
962 "H%.31s\n" /* RFC 1179, Section 7.2 - host name <= 31 chars */
963 "P%.31s\n" /* RFC 1179, Section 7.2 - user name <= 31 chars */
964 "J%.99s\n", /* RFC 1179, Section 7.2 - job name <= 99 chars */
965 localhost
, user
, title
);
966 cptr
= control
+ strlen(control
);
970 snprintf(cptr
, sizeof(control
) - (cptr
- control
),
971 "C%.31s\n" /* RFC 1179, Section 7.2 - class name <= 31 chars */
974 cptr
+= strlen(cptr
);
979 snprintf(cptr
, sizeof(control
) - (cptr
- control
), "%cdfA%03d%.15s\n",
980 format
, (int)getpid() % 1000, localhost
);
981 cptr
+= strlen(cptr
);
985 snprintf(cptr
, sizeof(control
) - (cptr
- control
),
987 "N%.131s\n", /* RFC 1179, Section 7.2 - sourcefile name <= 131 chars */
988 (int)getpid() % 1000, localhost
, title
);
990 fprintf(stderr
, "DEBUG: Control file is:\n%s", control
);
992 if (order
== ORDER_CONTROL_DATA
)
995 * Check for side-channel requests...
998 backendCheckSideChannel(snmp_fd
, &(addr
->addr
));
1001 * Send the control file...
1004 if (lpd_command(fd
, "\002%d cfA%03.3d%.15s\n", strlen(control
),
1005 (int)getpid() % 1000, localhost
))
1009 return (CUPS_BACKEND_FAILED
);
1012 fprintf(stderr
, "DEBUG: Sending control file (%u bytes)\n",
1013 (unsigned)strlen(control
));
1015 if (lpd_write(fd
, control
, strlen(control
) + 1) < (strlen(control
) + 1))
1018 perror("DEBUG: Unable to write control file");
1023 if (read(fd
, &status
, 1) < 1)
1025 _cupsLangPrintFilter(stderr
, "WARNING",
1026 _("The printer did not respond."));
1032 _cupsLangPrintFilter(stderr
, "ERROR",
1033 _("Remote host did not accept control file (%d)."),
1036 _cupsLangPrintFilter(stderr
, "INFO",
1037 _("Control file sent successfully."));
1045 * Check for side-channel requests...
1048 backendCheckSideChannel(snmp_fd
, &(addr
->addr
));
1051 * Send the print file...
1054 if (lpd_command(fd
, "\003" CUPS_LLFMT
" dfA%03.3d%.15s\n",
1055 CUPS_LLCAST filestats
.st_size
, (int)getpid() % 1000,
1060 return (CUPS_BACKEND_FAILED
);
1063 fprintf(stderr
, "DEBUG: Sending data file (" CUPS_LLFMT
" bytes)\n",
1064 CUPS_LLCAST filestats
.st_size
);
1067 for (copy
= 0; copy
< manual_copies
; copy
++)
1069 lseek(print_fd
, 0, SEEK_SET
);
1071 while ((nbytes
= read(print_fd
, buffer
, sizeof(buffer
))) > 0)
1073 _cupsLangPrintFilter(stderr
, "INFO",
1074 _("Spooling job, %.0f%% complete."),
1075 100.0 * tbytes
/ filestats
.st_size
);
1077 if (lpd_write(fd
, buffer
, nbytes
) < nbytes
)
1079 perror("DEBUG: Unable to send print file to printer");
1087 if (mode
== MODE_STANDARD
)
1089 if (tbytes
< filestats
.st_size
)
1091 else if (lpd_write(fd
, "", 1) < 1)
1093 perror("DEBUG: Unable to send trailing nul to printer");
1099 * Read the status byte from the printer; if we can't read the byte
1100 * back now, we should set status to "errno", however at this point
1101 * we know the printer got the whole file and we don't necessarily
1102 * want to requeue it over and over...
1105 if (recv(fd
, &status
, 1, 0) < 1)
1107 _cupsLangPrintFilter(stderr
, "WARNING",
1108 _("The printer did not respond."));
1117 _cupsLangPrintFilter(stderr
, "ERROR",
1118 _("Remote host did not accept data file (%d)."),
1121 _cupsLangPrintFilter(stderr
, "INFO",
1122 _("Data file sent successfully."));
1125 if (status
== 0 && order
== ORDER_DATA_CONTROL
)
1128 * Check for side-channel requests...
1131 backendCheckSideChannel(snmp_fd
, &(addr
->addr
));
1134 * Send control file...
1137 if (lpd_command(fd
, "\002%d cfA%03.3d%.15s\n", strlen(control
),
1138 (int)getpid() % 1000, localhost
))
1142 return (CUPS_BACKEND_FAILED
);
1145 fprintf(stderr
, "DEBUG: Sending control file (%lu bytes)\n",
1146 (unsigned long)strlen(control
));
1148 if (lpd_write(fd
, control
, strlen(control
) + 1) < (strlen(control
) + 1))
1151 perror("DEBUG: Unable to write control file");
1155 if (read(fd
, &status
, 1) < 1)
1157 _cupsLangPrintFilter(stderr
, "WARNING",
1158 _("The printer did not respond."));
1164 _cupsLangPrintFilter(stderr
, "ERROR",
1165 _("Remote host did not accept control file (%d)."),
1168 _cupsLangPrintFilter(stderr
, "INFO",
1169 _("Control file sent successfully."));
1173 * Collect the final supply levels as needed...
1177 backendSNMPSupplies(snmp_fd
, &(addr
->addr
), NULL
, NULL
);
1180 * Close the socket connection and input file...
1186 return (CUPS_BACKEND_OK
);
1189 * Waiting for a retry...
1196 * If we get here, then the job has been canceled...
1199 return (CUPS_BACKEND_FAILED
);
1204 * 'lpd_write()' - Write a buffer of data to an LPD server.
1207 static int /* O - Number of bytes written or -1 on error */
1208 lpd_write(int lpd_fd
, /* I - LPD socket */
1209 char *buffer
, /* I - Buffer to write */
1210 int length
) /* I - Number of bytes to write */
1212 int bytes
, /* Number of bytes written */
1213 total
; /* Total number of bytes written */
1220 while ((bytes
= send(lpd_fd
, buffer
, length
- total
, 0)) >= 0)
1225 if (total
== length
)
1236 #ifndef HAVE_RRESVPORT_AF
1238 * 'rresvport_af()' - A simple implementation of rresvport_af().
1241 static int /* O - Socket or -1 on error */
1242 rresvport_af(int *port
, /* IO - Port number to bind to */
1243 int family
) /* I - Address family */
1245 http_addr_t addr
; /* Socket address */
1246 int fd
; /* Socket file descriptor */
1250 * Try to create an IPv4 socket...
1253 if ((fd
= socket(family
, SOCK_STREAM
, 0)) < 0)
1257 * Initialize the address buffer...
1260 memset(&addr
, 0, sizeof(addr
));
1261 addr
.addr
.sa_family
= family
;
1264 * Try to bind the socket to a reserved port...
1270 * Set the port number...
1273 _httpAddrSetPort(&addr
, *port
);
1276 * Try binding the port to the socket; return if all is OK...
1279 if (!bind(fd
, (struct sockaddr
*)&addr
, sizeof(addr
)))
1283 * Stop if we have any error other than "address already in use"...
1286 if (errno
!= EADDRINUSE
)
1298 * Try the next port...
1305 * Wasn't able to bind to a reserved port, so close the socket and return
1317 #endif /* !HAVE_RRESVPORT_AF */
1321 * 'sigterm_handler()' - Handle 'terminate' signals that stop the backend.
1325 sigterm_handler(int sig
) /* I - Signal */
1327 (void)sig
; /* remove compiler warnings... */