]>
git.ipfire.org Git - thirdparty/cups.git/blob - backend/lpd.c
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_fd
; /* SNMP socket */
130 int fd
; /* Print file */
131 int status
; /* Status of LPD job */
132 int mode
; /* Print mode */
133 int banner
; /* Print banner page? */
134 int format
; /* Print format */
135 int order
; /* Order of control/data files */
136 int reserve
; /* Reserve priviledged port? */
137 int sanitize_title
; /* Sanitize title string? */
138 int manual_copies
, /* Do manual copies? */
139 timeout
, /* Timeout */
140 contimeout
, /* Connection timeout */
141 copies
; /* Number of copies */
142 ssize_t bytes
= 0; /* Initial bytes read */
143 char buffer
[16384]; /* Initial print buffer */
144 #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
145 struct sigaction action
; /* Actions for POSIX signals */
146 #endif /* HAVE_SIGACTION && !HAVE_SIGSET */
147 int num_jobopts
; /* Number of job options */
148 cups_option_t
*jobopts
= NULL
; /* Job options */
152 * Make sure status messages are not buffered...
155 setbuf(stderr
, NULL
);
158 * Ignore SIGPIPE and catch SIGTERM signals...
162 sigset(SIGPIPE
, SIG_IGN
);
163 sigset(SIGTERM
, sigterm_handler
);
164 #elif defined(HAVE_SIGACTION)
165 memset(&action
, 0, sizeof(action
));
166 action
.sa_handler
= SIG_IGN
;
167 sigaction(SIGPIPE
, &action
, NULL
);
169 sigemptyset(&action
.sa_mask
);
170 sigaddset(&action
.sa_mask
, SIGTERM
);
171 action
.sa_handler
= sigterm_handler
;
172 sigaction(SIGTERM
, &action
, NULL
);
174 signal(SIGPIPE
, SIG_IGN
);
175 signal(SIGTERM
, sigterm_handler
);
176 #endif /* HAVE_SIGSET */
179 * Check command-line...
184 printf("network lpd \"Unknown\" \"%s\"\n",
185 _cupsLangString(cupsLangDefault(), _("LPD/LPR Host or Printer")));
186 return (CUPS_BACKEND_OK
);
188 else if (argc
< 6 || argc
> 7)
190 _cupsLangPrintf(stderr
,
191 _("Usage: %s job-id user title copies options [file]"),
193 return (CUPS_BACKEND_FAILED
);
196 num_jobopts
= cupsParseOptions(argv
[5], 0, &jobopts
);
199 * Extract the hostname and printer name from the URI...
202 while ((device_uri
= cupsBackendDeviceURI(argv
)) == NULL
)
204 _cupsLangPrintFilter(stderr
, "INFO", _("Unable to locate printer."));
207 if (getenv("CLASS") != NULL
)
208 return (CUPS_BACKEND_FAILED
);
211 httpSeparateURI(HTTP_URI_CODING_ALL
, device_uri
, scheme
, sizeof(scheme
),
212 username
, sizeof(username
), hostname
, sizeof(hostname
), &port
,
213 resource
, sizeof(resource
));
216 port
= 515; /* Default to port 515 */
221 * If no username is in the device URI, then use the print job user...
224 strlcpy(username
, argv
[2], sizeof(username
));
228 * See if there are any options...
231 mode
= MODE_STANDARD
;
234 order
= ORDER_CONTROL_DATA
;
235 reserve
= RESERVE_ANY
;
238 contimeout
= 7 * 24 * 60 * 60;
242 * We want to pass UTF-8 characters by default, not re-map them (3071945)
248 * Otherwise we want to re-map UTF-8 to "safe" characters by default...
252 #endif /* __APPLE__ */
254 if ((options
= strchr(resource
, '?')) != NULL
)
257 * Yup, terminate the device name string and move to the first
258 * character of the options...
275 while (*options
&& *options
!= '=' && *options
!= '+' && *options
!= '&')
278 if ((sep
= *options
) != '\0')
289 while (*options
&& *options
!= '+' && *options
!= '&')
299 * Process the option...
302 if (!_cups_strcasecmp(name
, "banner"))
308 banner
= !value
[0] || !_cups_strcasecmp(value
, "on") ||
309 !_cups_strcasecmp(value
, "yes") || !_cups_strcasecmp(value
, "true");
311 else if (!_cups_strcasecmp(name
, "format") && value
[0])
314 * Set output format...
317 if (strchr("cdfglnoprtv", value
[0]))
320 _cupsLangPrintFilter(stderr
, "ERROR",
321 _("Unknown format character: \"%c\"."),
324 else if (!_cups_strcasecmp(name
, "mode") && value
[0])
327 * Set control/data order...
330 if (!_cups_strcasecmp(value
, "standard"))
331 mode
= MODE_STANDARD
;
332 else if (!_cups_strcasecmp(value
, "stream"))
335 _cupsLangPrintFilter(stderr
, "ERROR",
336 _("Unknown print mode: \"%s\"."), value
);
338 else if (!_cups_strcasecmp(name
, "order") && value
[0])
341 * Set control/data order...
344 if (!_cups_strcasecmp(value
, "control,data"))
345 order
= ORDER_CONTROL_DATA
;
346 else if (!_cups_strcasecmp(value
, "data,control"))
347 order
= ORDER_DATA_CONTROL
;
349 _cupsLangPrintFilter(stderr
, "ERROR",
350 _("Unknown file order: \"%s\"."), value
);
352 else if (!_cups_strcasecmp(name
, "reserve"))
355 * Set port reservation mode...
358 if (!value
[0] || !_cups_strcasecmp(value
, "on") ||
359 !_cups_strcasecmp(value
, "yes") || !_cups_strcasecmp(value
, "true") ||
360 !_cups_strcasecmp(value
, "rfc1179"))
361 reserve
= RESERVE_RFC1179
;
362 else if (!_cups_strcasecmp(value
, "any"))
363 reserve
= RESERVE_ANY
;
365 reserve
= RESERVE_NONE
;
367 else if (!_cups_strcasecmp(name
, "manual_copies"))
370 * Set manual copies...
373 manual_copies
= !value
[0] || !_cups_strcasecmp(value
, "on") ||
374 !_cups_strcasecmp(value
, "yes") || !_cups_strcasecmp(value
, "true");
376 else if (!_cups_strcasecmp(name
, "sanitize_title"))
379 * Set sanitize title...
382 sanitize_title
= !value
[0] || !_cups_strcasecmp(value
, "on") ||
383 !_cups_strcasecmp(value
, "yes") || !_cups_strcasecmp(value
, "true");
385 else if (!_cups_strcasecmp(name
, "timeout"))
392 timeout
= atoi(value
);
394 else if (!_cups_strcasecmp(name
, "contimeout"))
397 * Set the connection timeout...
401 contimeout
= atoi(value
);
406 if (mode
== MODE_STREAM
)
407 order
= ORDER_CONTROL_DATA
;
410 * Find the printer...
413 snprintf(portname
, sizeof(portname
), "%d", port
);
415 fputs("STATE: +connecting-to-device\n", stderr
);
416 fprintf(stderr
, "DEBUG: Looking up \"%s\"...\n", hostname
);
418 while ((addrlist
= httpAddrGetList(hostname
, AF_UNSPEC
, portname
)) == NULL
)
420 _cupsLangPrintFilter(stderr
, "INFO",
421 _("Unable to locate printer \"%s\"."), hostname
);
424 if (getenv("CLASS") != NULL
)
426 fputs("STATE: -connecting-to-device\n", stderr
);
427 exit(CUPS_BACKEND_FAILED
);
431 snmp_fd
= _cupsSNMPOpen(addrlist
->addr
.addr
.sa_family
);
434 * Wait for data from the filter...
439 if (!backendWaitLoop(snmp_fd
, &(addrlist
->addr
), 0, backendNetworkSideCB
))
440 return (CUPS_BACKEND_OK
);
441 else if (mode
== MODE_STANDARD
&&
442 (bytes
= read(0, buffer
, sizeof(buffer
))) <= 0)
443 return (CUPS_BACKEND_OK
);
447 * If we have 7 arguments, print the file named on the command-line.
448 * Otherwise, copy stdin to a temporary file and print the temporary
452 if (argc
== 6 && mode
== MODE_STANDARD
)
455 * Copy stdin to a temporary file...
458 if ((fd
= cupsTempFd(tmpfilename
, sizeof(tmpfilename
))) < 0)
460 perror("DEBUG: Unable to create temporary file");
461 return (CUPS_BACKEND_FAILED
);
464 _cupsLangPrintFilter(stderr
, "INFO", _("Copying print data."));
467 write(fd
, buffer
, bytes
);
469 backendRunLoop(-1, fd
, snmp_fd
, &(addrlist
->addr
), 0, 0,
470 backendNetworkSideCB
);
475 * Stream from stdin...
484 fd
= open(filename
, O_RDONLY
);
488 _cupsLangPrintError("ERROR", _("Unable to open print file"));
489 return (CUPS_BACKEND_FAILED
);
494 * Sanitize the document title...
497 strlcpy(title
, argv
[3], sizeof(title
));
502 * Sanitize the title string so that we don't cause problems on
508 for (ptr
= title
; *ptr
; ptr
++)
509 if (!isalnum(*ptr
& 255) && !isspace(*ptr
& 255))
521 manual_copies
= atoi(argv
[4]);
527 copies
= atoi(argv
[4]);
530 status
= lpd_queue(hostname
, addrlist
, resource
+ 1, fd
, snmp_fd
, mode
,
531 username
, title
, copies
, banner
, format
, order
, reserve
,
532 manual_copies
, timeout
, contimeout
,
533 cupsGetOption("job-originating-host-name", num_jobopts
,
537 fprintf(stderr
, "PAGE: 1 %d\n", atoi(argv
[4]));
540 status
= lpd_queue(hostname
, addrlist
, resource
+ 1, fd
, snmp_fd
, mode
,
541 username
, title
, 1, banner
, format
, order
, reserve
, 1,
543 cupsGetOption("job-originating-host-name", num_jobopts
,
547 * Remove the temporary file if necessary...
557 _cupsSNMPClose(snmp_fd
);
560 * Return the queue status...
568 * 'lpd_command()' - Send an LPR command sequence and wait for a reply.
571 static int /* O - Status of command */
572 lpd_command(int fd
, /* I - Socket connection to LPD host */
573 char *format
, /* I - printf()-style format string */
574 ...) /* I - Additional args as necessary */
576 va_list ap
; /* Argument pointer */
577 char buf
[1024]; /* Output buffer */
578 int bytes
; /* Number of bytes to output */
579 char status
; /* Status from command */
583 * Don't try to send commands if the job has been canceled...
590 * Format the string...
593 va_start(ap
, format
);
594 bytes
= vsnprintf(buf
, sizeof(buf
), format
, ap
);
597 fprintf(stderr
, "DEBUG: lpd_command %2.2x %s", buf
[0], buf
+ 1);
600 * Send the command...
603 fprintf(stderr
, "DEBUG: Sending command string (%d bytes)...\n", bytes
);
605 if (lpd_write(fd
, buf
, bytes
) < bytes
)
607 perror("DEBUG: Unable to send LPD command");
612 * Read back the status from the command and return it...
615 fputs("DEBUG: Reading command status...\n", stderr
);
617 if (recv(fd
, &status
, 1, 0) < 1)
619 _cupsLangPrintFilter(stderr
, "WARNING", _("The printer did not respond."));
623 fprintf(stderr
, "DEBUG: lpd_command returning %d\n", status
);
630 * 'lpd_queue()' - Queue a file using the Line Printer Daemon protocol.
633 static int /* O - Zero on success, non-zero on failure */
634 lpd_queue(const char *hostname
, /* I - Host to connect to */
635 http_addrlist_t
*addrlist
, /* I - List of host addresses */
636 const char *printer
, /* I - Printer/queue name */
637 int print_fd
, /* I - File to print */
638 int snmp_fd
, /* I - SNMP socket */
639 int mode
, /* I - Print mode */
640 const char *user
, /* I - Requesting user */
641 const char *title
, /* I - Job title */
642 int copies
, /* I - Number of copies */
643 int banner
, /* I - Print LPD banner? */
644 int format
, /* I - Format specifier */
645 int order
, /* I - Order of data/control files */
646 int reserve
, /* I - Reserve ports? */
647 int manual_copies
,/* I - Do copies by hand... */
648 int timeout
, /* I - Timeout... */
649 int contimeout
, /* I - Connection timeout */
650 const char *orighost
) /* I - job-originating-host-name */
652 char localhost
[255]; /* Local host name */
653 int error
; /* Error number */
654 struct stat filestats
; /* File statistics */
655 int lport
; /* LPD connection local port */
656 int fd
; /* LPD socket */
657 char control
[10240], /* LPD control 'file' */
658 *cptr
; /* Pointer into control file string */
659 char status
; /* Status byte from command */
660 int delay
; /* Delay for retries... */
661 char addrname
[256]; /* Address name */
662 http_addrlist_t
*addr
; /* Socket address */
663 int have_supplies
; /* Printer supports supply levels? */
664 int copy
; /* Copies written */
665 time_t start_time
; /* Time of first connect */
666 size_t nbytes
; /* Number of bytes written */
667 off_t tbytes
; /* Total bytes written */
668 char buffer
[32768]; /* Output buffer */
670 DWORD tv
; /* Timeout in milliseconds */
672 struct timeval tv
; /* Timeout in secs and usecs */
677 * Remember when we started trying to connect to the printer...
680 start_time
= time(NULL
);
683 * Loop forever trying to print the file...
689 * First try to reserve a port for this connection...
692 fprintf(stderr
, "DEBUG: Connecting to %s:%d for printer %s\n", hostname
,
693 _httpAddrPort(&(addrlist
->addr
)), printer
);
694 _cupsLangPrintFilter(stderr
, "INFO", _("Connecting to printer."));
696 for (lport
= reserve
== RESERVE_RFC1179
? 732 : 1024, addr
= addrlist
,
701 * Stop if this job has been canceled...
705 return (CUPS_BACKEND_FAILED
);
708 * Choose the next priviledged port...
716 if (lport
< 721 && reserve
== RESERVE_RFC1179
)
722 if (geteuid() || !reserve
)
724 if (getuid() || !reserve
)
725 #endif /* HAVE_GETEUID */
728 * Just create a regular socket...
731 if ((fd
= socket(addr
->addr
.addr
.sa_family
, SOCK_STREAM
, 0)) < 0)
733 perror("DEBUG: Unable to create socket");
744 * We're running as root and want to comply with RFC 1179. Reserve a
745 * priviledged lport between 721 and 731...
748 if ((fd
= rresvport_af(&lport
, addr
->addr
.addr
.sa_family
)) < 0)
750 perror("DEBUG: Unable to reserve port");
758 * Connect to the printer or server...
765 return (CUPS_BACKEND_FAILED
);
768 if (!connect(fd
, &(addr
->addr
.addr
), httpAddrLength(&(addr
->addr
))))
777 if (getenv("CLASS") != NULL
)
780 * If the CLASS environment variable is set, the job was submitted
781 * to a class and not to a specific queue. In this case, we want
782 * to abort immediately so that the job can be requeued on the next
783 * available printer in the class.
786 _cupsLangPrintFilter(stderr
, "INFO",
787 _("Unable to contact printer, queuing on next "
788 "printer in class."));
791 * Sleep 5 seconds to keep the job from requeuing too rapidly...
796 return (CUPS_BACKEND_FAILED
);
799 fprintf(stderr
, "DEBUG: Connection error: %s\n", strerror(error
));
801 if (error
== ECONNREFUSED
|| error
== EHOSTDOWN
||
802 error
== EHOSTUNREACH
)
804 if (contimeout
&& (time(NULL
) - start_time
) > contimeout
)
806 _cupsLangPrintFilter(stderr
, "ERROR",
807 _("The printer is not responding."));
808 return (CUPS_BACKEND_FAILED
);
814 _cupsLangPrintFilter(stderr
, "WARNING",
815 _("The printer may not exist or "
816 "is unavailable at this time."));
820 _cupsLangPrintFilter(stderr
, "WARNING",
821 _("The printer is unreachable at "
827 _cupsLangPrintFilter(stderr
, "WARNING",
828 _("The printer is in use."));
837 else if (error
== EADDRINUSE
)
840 * Try on another port...
847 _cupsLangPrintFilter(stderr
, "ERROR",
848 _("The printer is not responding."));
858 tv
= (DWORD
)(timeout
* 1000);
860 setsockopt(fd
, SOL_SOCKET
, SO_RCVTIMEO
, (char *)&tv
, sizeof(tv
));
861 setsockopt(fd
, SOL_SOCKET
, SO_SNDTIMEO
, (char *)&tv
, sizeof(tv
));
866 setsockopt(fd
, SOL_SOCKET
, SO_RCVTIMEO
, &tv
, sizeof(tv
));
867 setsockopt(fd
, SOL_SOCKET
, SO_SNDTIMEO
, &tv
, sizeof(tv
));
870 fputs("STATE: -connecting-to-device\n", stderr
);
871 _cupsLangPrintFilter(stderr
, "INFO", _("Connected to printer."));
873 fprintf(stderr
, "DEBUG: Connected to %s:%d (local port %d)...\n",
874 httpAddrString(&(addr
->addr
), addrname
, sizeof(addrname
)),
875 _httpAddrPort(&(addr
->addr
)), lport
);
878 * See if the printer supports SNMP...
882 have_supplies
= !backendSNMPSupplies(snmp_fd
, &(addrlist
->addr
), NULL
,
888 * Check for side-channel requests...
891 backendCheckSideChannel(snmp_fd
, &(addrlist
->addr
));
894 * Next, open the print file and figure out its size...
900 * Use the size from the print file...
903 if (fstat(print_fd
, &filestats
))
907 perror("DEBUG: unable to stat print file");
908 return (CUPS_BACKEND_FAILED
);
911 filestats
.st_size
*= manual_copies
;
916 * Use a "very large value" for the size so that the printer will
917 * keep printing until we close the connection...
920 #ifdef _LARGEFILE_SOURCE
921 filestats
.st_size
= (size_t)(999999999999.0);
923 filestats
.st_size
= 2147483647;
924 #endif /* _LARGEFILE_SOURCE */
928 * Send a job header to the printer, specifying no banner page and
932 if (lpd_command(fd
, "\002%s\n",
933 printer
)) /* Receive print job(s) */
936 return (CUPS_BACKEND_FAILED
);
940 strlcpy(localhost
, orighost
, sizeof(localhost
));
942 httpGetHostname(NULL
, localhost
, sizeof(localhost
));
944 snprintf(control
, sizeof(control
),
945 "H%.31s\n" /* RFC 1179, Section 7.2 - host name <= 31 chars */
946 "P%.31s\n" /* RFC 1179, Section 7.2 - user name <= 31 chars */
947 "J%.99s\n", /* RFC 1179, Section 7.2 - job name <= 99 chars */
948 localhost
, user
, title
);
949 cptr
= control
+ strlen(control
);
953 snprintf(cptr
, sizeof(control
) - (cptr
- control
),
954 "C%.31s\n" /* RFC 1179, Section 7.2 - class name <= 31 chars */
957 cptr
+= strlen(cptr
);
962 snprintf(cptr
, sizeof(control
) - (cptr
- control
), "%cdfA%03d%.15s\n",
963 format
, (int)getpid() % 1000, localhost
);
964 cptr
+= strlen(cptr
);
968 snprintf(cptr
, sizeof(control
) - (cptr
- control
),
970 "N%.131s\n", /* RFC 1179, Section 7.2 - sourcefile name <= 131 chars */
971 (int)getpid() % 1000, localhost
, title
);
973 fprintf(stderr
, "DEBUG: Control file is:\n%s", control
);
975 if (order
== ORDER_CONTROL_DATA
)
978 * Check for side-channel requests...
981 backendCheckSideChannel(snmp_fd
, &(addr
->addr
));
984 * Send the control file...
987 if (lpd_command(fd
, "\002%d cfA%03.3d%.15s\n", strlen(control
),
988 (int)getpid() % 1000, localhost
))
992 return (CUPS_BACKEND_FAILED
);
995 fprintf(stderr
, "DEBUG: Sending control file (%u bytes)\n",
996 (unsigned)strlen(control
));
998 if (lpd_write(fd
, control
, strlen(control
) + 1) < (strlen(control
) + 1))
1001 perror("DEBUG: Unable to write control file");
1006 if (read(fd
, &status
, 1) < 1)
1008 _cupsLangPrintFilter(stderr
, "WARNING",
1009 _("The printer did not respond."));
1015 _cupsLangPrintFilter(stderr
, "ERROR",
1016 _("Remote host did not accept control file (%d)."),
1019 _cupsLangPrintFilter(stderr
, "INFO",
1020 _("Control file sent successfully."));
1028 * Check for side-channel requests...
1031 backendCheckSideChannel(snmp_fd
, &(addr
->addr
));
1034 * Send the print file...
1037 if (lpd_command(fd
, "\003" CUPS_LLFMT
" dfA%03.3d%.15s\n",
1038 CUPS_LLCAST filestats
.st_size
, (int)getpid() % 1000,
1043 return (CUPS_BACKEND_FAILED
);
1046 fprintf(stderr
, "DEBUG: Sending data file (" CUPS_LLFMT
" bytes)\n",
1047 CUPS_LLCAST filestats
.st_size
);
1050 for (copy
= 0; copy
< manual_copies
; copy
++)
1052 lseek(print_fd
, 0, SEEK_SET
);
1054 while ((nbytes
= read(print_fd
, buffer
, sizeof(buffer
))) > 0)
1056 _cupsLangPrintFilter(stderr
, "INFO",
1057 _("Spooling job, %.0f%% complete."),
1058 100.0 * tbytes
/ filestats
.st_size
);
1060 if (lpd_write(fd
, buffer
, nbytes
) < nbytes
)
1062 perror("DEBUG: Unable to send print file to printer");
1070 if (mode
== MODE_STANDARD
)
1072 if (tbytes
< filestats
.st_size
)
1074 else if (lpd_write(fd
, "", 1) < 1)
1076 perror("DEBUG: Unable to send trailing nul to printer");
1082 * Read the status byte from the printer; if we can't read the byte
1083 * back now, we should set status to "errno", however at this point
1084 * we know the printer got the whole file and we don't necessarily
1085 * want to requeue it over and over...
1088 if (recv(fd
, &status
, 1, 0) < 1)
1090 _cupsLangPrintFilter(stderr
, "WARNING",
1091 _("The printer did not respond."));
1100 _cupsLangPrintFilter(stderr
, "ERROR",
1101 _("Remote host did not accept data file (%d)."),
1104 _cupsLangPrintFilter(stderr
, "INFO",
1105 _("Data file sent successfully."));
1108 if (status
== 0 && order
== ORDER_DATA_CONTROL
)
1111 * Check for side-channel requests...
1114 backendCheckSideChannel(snmp_fd
, &(addr
->addr
));
1117 * Send control file...
1120 if (lpd_command(fd
, "\002%d cfA%03.3d%.15s\n", strlen(control
),
1121 (int)getpid() % 1000, localhost
))
1125 return (CUPS_BACKEND_FAILED
);
1128 fprintf(stderr
, "DEBUG: Sending control file (%lu bytes)\n",
1129 (unsigned long)strlen(control
));
1131 if (lpd_write(fd
, control
, strlen(control
) + 1) < (strlen(control
) + 1))
1134 perror("DEBUG: Unable to write control file");
1138 if (read(fd
, &status
, 1) < 1)
1140 _cupsLangPrintFilter(stderr
, "WARNING",
1141 _("The printer did not respond."));
1147 _cupsLangPrintFilter(stderr
, "ERROR",
1148 _("Remote host did not accept control file (%d)."),
1151 _cupsLangPrintFilter(stderr
, "INFO",
1152 _("Control file sent successfully."));
1156 * Collect the final supply levels as needed...
1160 backendSNMPSupplies(snmp_fd
, &(addr
->addr
), NULL
, NULL
);
1163 * Close the socket connection and input file...
1169 return (CUPS_BACKEND_OK
);
1172 * Waiting for a retry...
1179 * If we get here, then the job has been canceled...
1182 return (CUPS_BACKEND_FAILED
);
1187 * 'lpd_write()' - Write a buffer of data to an LPD server.
1190 static int /* O - Number of bytes written or -1 on error */
1191 lpd_write(int lpd_fd
, /* I - LPD socket */
1192 char *buffer
, /* I - Buffer to write */
1193 int length
) /* I - Number of bytes to write */
1195 int bytes
, /* Number of bytes written */
1196 total
; /* Total number of bytes written */
1203 while ((bytes
= send(lpd_fd
, buffer
, length
- total
, 0)) >= 0)
1208 if (total
== length
)
1219 #ifndef HAVE_RRESVPORT_AF
1221 * 'rresvport_af()' - A simple implementation of rresvport_af().
1224 static int /* O - Socket or -1 on error */
1225 rresvport_af(int *port
, /* IO - Port number to bind to */
1226 int family
) /* I - Address family */
1228 http_addr_t addr
; /* Socket address */
1229 int fd
; /* Socket file descriptor */
1233 * Try to create an IPv4 socket...
1236 if ((fd
= socket(family
, SOCK_STREAM
, 0)) < 0)
1240 * Initialize the address buffer...
1243 memset(&addr
, 0, sizeof(addr
));
1244 addr
.addr
.sa_family
= family
;
1247 * Try to bind the socket to a reserved port...
1253 * Set the port number...
1256 _httpAddrSetPort(&addr
, *port
);
1259 * Try binding the port to the socket; return if all is OK...
1262 if (!bind(fd
, (struct sockaddr
*)&addr
, sizeof(addr
)))
1266 * Stop if we have any error other than "address already in use"...
1269 if (errno
!= EADDRINUSE
)
1281 * Try the next port...
1288 * Wasn't able to bind to a reserved port, so close the socket and return
1300 #endif /* !HAVE_RRESVPORT_AF */
1304 * 'sigterm_handler()' - Handle 'terminate' signals that stop the backend.
1308 sigterm_handler(int sig
) /* I - Signal */
1310 (void)sig
; /* remove compiler warnings... */