]>
Commit | Line | Data |
---|---|---|
ef416fc2 | 1 | /* |
b19ccc9e | 2 | * "$Id: lpd.c 7740 2008-07-14 23:58:05Z mike $" |
ef416fc2 | 3 | * |
0837b7e8 | 4 | * Line Printer Daemon backend for CUPS. |
ef416fc2 | 5 | * |
0268488e | 6 | * Copyright 2007-2011 by Apple Inc. |
f42414bf | 7 | * Copyright 1997-2007 by Easy Software Products, all rights reserved. |
ef416fc2 | 8 | * |
9 | * These coded instructions, statements, and computer programs are the | |
bc44d920 | 10 | * property of Apple Inc. and are protected by Federal copyright |
11 | * law. Distribution and use rights are outlined in the file "LICENSE.txt" | |
ef416fc2 | 12 | * "LICENSE" which should have been included with this file. If this |
bc44d920 | 13 | * file is missing or damaged, see the license at "http://www.cups.org/". |
ef416fc2 | 14 | * |
15 | * This file is subject to the Apple OS-Developed Software exception. | |
16 | * | |
17 | * Contents: | |
18 | * | |
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_timeout() - Handle timeout alarms... | |
23 | * lpd_write() - Write a buffer of data to an LPD server. | |
24 | * rresvport_af() - A simple implementation of rresvport_af(). | |
25 | * sigterm_handler() - Handle 'terminate' signals that stop the backend. | |
26 | */ | |
27 | ||
28 | /* | |
29 | * Include necessary headers. | |
30 | */ | |
31 | ||
ef416fc2 | 32 | #include <cups/http-private.h> |
7a14d768 | 33 | #include "backend-private.h" |
ef416fc2 | 34 | #include <stdarg.h> |
ef416fc2 | 35 | #include <sys/types.h> |
36 | #include <sys/stat.h> | |
c7017ecc | 37 | #include <stdio.h> |
ef416fc2 | 38 | |
39 | #ifdef WIN32 | |
40 | # include <winsock.h> | |
41 | #else | |
42 | # include <sys/socket.h> | |
43 | # include <netinet/in.h> | |
44 | # include <arpa/inet.h> | |
45 | # include <netdb.h> | |
46 | #endif /* WIN32 */ | |
fa73b229 | 47 | #ifdef __APPLE__ |
48 | # include <CoreFoundation/CFNumber.h> | |
49 | # include <CoreFoundation/CFPreferences.h> | |
50 | #endif /* __APPLE__ */ | |
ef416fc2 | 51 | |
52 | ||
53 | /* | |
54 | * Globals... | |
55 | */ | |
56 | ||
57 | static char tmpfilename[1024] = ""; /* Temporary spool file name */ | |
58 | static int abort_job = 0; /* Non-zero if we get SIGTERM */ | |
59 | ||
60 | ||
f42414bf | 61 | /* |
62 | * Print mode... | |
63 | */ | |
64 | ||
65 | #define MODE_STANDARD 0 /* Queue a copy */ | |
66 | #define MODE_STREAM 1 /* Stream a copy */ | |
67 | ||
68 | ||
ef416fc2 | 69 | /* |
70 | * The order for control and data files in LPD requests... | |
71 | */ | |
72 | ||
73 | #define ORDER_CONTROL_DATA 0 /* Control file first, then data */ | |
74 | #define ORDER_DATA_CONTROL 1 /* Data file first, then control */ | |
75 | ||
76 | ||
77 | /* | |
78 | * What to reserve... | |
79 | */ | |
80 | ||
81 | #define RESERVE_NONE 0 /* Don't reserve a priviledged port */ | |
82 | #define RESERVE_RFC1179 1 /* Reserve port 721-731 */ | |
83 | #define RESERVE_ANY 2 /* Reserve port 1-1023 */ | |
84 | ||
85 | ||
86 | /* | |
87 | * Local functions... | |
88 | */ | |
89 | ||
90 | static int lpd_command(int lpd_fd, int timeout, char *format, ...); | |
c8fef167 MS |
91 | static int lpd_queue(const char *hostname, http_addrlist_t *addrlist, |
92 | const char *printer, int print_fd, int snmp_fd, | |
93 | int mode, const char *user, const char *title, | |
94 | int copies, int banner, int format, int order, | |
95 | int reserve, int manual_copies, int timeout, | |
96 | int contimeout); | |
ef416fc2 | 97 | static void lpd_timeout(int sig); |
98 | static int lpd_write(int lpd_fd, char *buffer, int length); | |
99 | #ifndef HAVE_RRESVPORT_AF | |
100 | static int rresvport_af(int *port, int family); | |
101 | #endif /* !HAVE_RRESVPORT_AF */ | |
102 | static void sigterm_handler(int sig); | |
103 | ||
104 | ||
105 | /* | |
106 | * 'main()' - Send a file to the printer or server. | |
107 | * | |
108 | * Usage: | |
109 | * | |
110 | * printer-uri job-id user title copies options [file] | |
111 | */ | |
112 | ||
113 | int /* O - Exit status */ | |
114 | main(int argc, /* I - Number of command-line arguments (6 or 7) */ | |
115 | char *argv[]) /* I - Command-line arguments */ | |
116 | { | |
1f0275e3 | 117 | const char *device_uri; /* Device URI */ |
acb056cb | 118 | char scheme[255], /* Scheme in URI */ |
7a14d768 MS |
119 | hostname[1024], /* Hostname */ |
120 | username[255], /* Username info */ | |
121 | resource[1024], /* Resource info (printer name) */ | |
122 | *options, /* Pointer to options */ | |
123 | *name, /* Name of option */ | |
124 | *value, /* Value of option */ | |
125 | sep, /* Separator character */ | |
126 | *filename, /* File to print */ | |
127 | title[256]; /* Title string */ | |
128 | int port; /* Port number */ | |
c8fef167 MS |
129 | char portname[256]; /* Port name (string) */ |
130 | http_addrlist_t *addrlist; /* List of addresses for printer */ | |
131 | int snmp_fd; /* SNMP socket */ | |
7a14d768 MS |
132 | int fd; /* Print file */ |
133 | int status; /* Status of LPD job */ | |
134 | int mode; /* Print mode */ | |
135 | int banner; /* Print banner page? */ | |
136 | int format; /* Print format */ | |
137 | int order; /* Order of control/data files */ | |
138 | int reserve; /* Reserve priviledged port? */ | |
139 | int sanitize_title; /* Sanitize title string? */ | |
140 | int manual_copies, /* Do manual copies? */ | |
141 | timeout, /* Timeout */ | |
142 | contimeout, /* Connection timeout */ | |
143 | copies; /* Number of copies */ | |
eac3a0a0 MS |
144 | ssize_t bytes = 0; /* Initial bytes read */ |
145 | char buffer[16384]; /* Initial print buffer */ | |
ef416fc2 | 146 | #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET) |
7a14d768 | 147 | struct sigaction action; /* Actions for POSIX signals */ |
ef416fc2 | 148 | #endif /* HAVE_SIGACTION && !HAVE_SIGSET */ |
149 | ||
150 | ||
151 | /* | |
152 | * Make sure status messages are not buffered... | |
153 | */ | |
154 | ||
155 | setbuf(stderr, NULL); | |
156 | ||
157 | /* | |
158 | * Ignore SIGPIPE and catch SIGTERM signals... | |
159 | */ | |
160 | ||
161 | #ifdef HAVE_SIGSET | |
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); | |
168 | ||
169 | sigemptyset(&action.sa_mask); | |
170 | sigaddset(&action.sa_mask, SIGTERM); | |
171 | action.sa_handler = sigterm_handler; | |
172 | sigaction(SIGTERM, &action, NULL); | |
173 | #else | |
174 | signal(SIGPIPE, SIG_IGN); | |
175 | signal(SIGTERM, sigterm_handler); | |
176 | #endif /* HAVE_SIGSET */ | |
177 | ||
178 | /* | |
179 | * Check command-line... | |
180 | */ | |
181 | ||
182 | if (argc == 1) | |
183 | { | |
8b450588 MS |
184 | printf("network lpd \"Unknown\" \"%s\"\n", |
185 | _cupsLangString(cupsLangDefault(), _("LPD/LPR Host or Printer"))); | |
ef416fc2 | 186 | return (CUPS_BACKEND_OK); |
187 | } | |
188 | else if (argc < 6 || argc > 7) | |
189 | { | |
db1f069b | 190 | _cupsLangPrintf(stderr, |
0837b7e8 | 191 | _("Usage: %s job-id user title copies options [file]"), |
db1f069b | 192 | argv[0]); |
ef416fc2 | 193 | return (CUPS_BACKEND_FAILED); |
194 | } | |
195 | ||
ef416fc2 | 196 | /* |
197 | * Extract the hostname and printer name from the URI... | |
198 | */ | |
199 | ||
0268488e MS |
200 | while ((device_uri = cupsBackendDeviceURI(argv)) == NULL) |
201 | { | |
202 | _cupsLangPrintFilter(stderr, "INFO", _("Unable to locate printer.")); | |
203 | sleep(10); | |
204 | ||
205 | if (getenv("CLASS") != NULL) | |
206 | return (CUPS_BACKEND_FAILED); | |
207 | } | |
1f0275e3 | 208 | |
acb056cb MS |
209 | httpSeparateURI(HTTP_URI_CODING_ALL, device_uri, scheme, sizeof(scheme), |
210 | username, sizeof(username), hostname, sizeof(hostname), &port, | |
ef416fc2 | 211 | resource, sizeof(resource)); |
212 | ||
a41f09e2 MS |
213 | if (!port) |
214 | port = 515; /* Default to port 515 */ | |
215 | ||
ef416fc2 | 216 | if (!username[0]) |
217 | { | |
218 | /* | |
219 | * If no username is in the device URI, then use the print job user... | |
220 | */ | |
221 | ||
222 | strlcpy(username, argv[2], sizeof(username)); | |
223 | } | |
224 | ||
225 | /* | |
226 | * See if there are any options... | |
227 | */ | |
228 | ||
f42414bf | 229 | mode = MODE_STANDARD; |
fa73b229 | 230 | banner = 0; |
231 | format = 'l'; | |
232 | order = ORDER_CONTROL_DATA; | |
233 | reserve = RESERVE_ANY; | |
234 | manual_copies = 1; | |
235 | timeout = 300; | |
236 | contimeout = 7 * 24 * 60 * 60; | |
ef416fc2 | 237 | |
fa73b229 | 238 | #ifdef __APPLE__ |
c0e1af83 | 239 | /* |
0268488e | 240 | * We want to pass UTF-8 characters by default, not re-map them (3071945) |
c0e1af83 | 241 | */ |
242 | ||
fa73b229 | 243 | sanitize_title = 0; |
0268488e | 244 | #else |
c0e1af83 | 245 | /* |
0268488e | 246 | * Otherwise we want to re-map UTF-8 to "safe" characters by default... |
c0e1af83 | 247 | */ |
248 | ||
fa73b229 | 249 | sanitize_title = 1; |
250 | #endif /* __APPLE__ */ | |
ef416fc2 | 251 | |
252 | if ((options = strchr(resource, '?')) != NULL) | |
253 | { | |
254 | /* | |
255 | * Yup, terminate the device name string and move to the first | |
256 | * character of the options... | |
257 | */ | |
258 | ||
259 | *options++ = '\0'; | |
260 | ||
261 | /* | |
262 | * Parse options... | |
263 | */ | |
264 | ||
265 | while (*options) | |
266 | { | |
267 | /* | |
268 | * Get the name... | |
269 | */ | |
270 | ||
db1f069b | 271 | name = options; |
ef416fc2 | 272 | |
db1f069b MS |
273 | while (*options && *options != '=' && *options != '+' && *options != '&') |
274 | options ++; | |
275 | ||
276 | if ((sep = *options) != '\0') | |
277 | *options++ = '\0'; | |
278 | ||
279 | if (sep == '=') | |
ef416fc2 | 280 | { |
281 | /* | |
282 | * Get the value... | |
283 | */ | |
284 | ||
db1f069b | 285 | value = options; |
ef416fc2 | 286 | |
db1f069b | 287 | while (*options && *options != '+' && *options != '&') |
ef416fc2 | 288 | options ++; |
db1f069b MS |
289 | |
290 | if (*options) | |
291 | *options++ = '\0'; | |
ef416fc2 | 292 | } |
293 | else | |
db1f069b | 294 | value = (char *)""; |
ef416fc2 | 295 | |
296 | /* | |
297 | * Process the option... | |
298 | */ | |
299 | ||
fa73b229 | 300 | if (!strcasecmp(name, "banner")) |
ef416fc2 | 301 | { |
302 | /* | |
303 | * Set the banner... | |
304 | */ | |
305 | ||
fa73b229 | 306 | banner = !value[0] || !strcasecmp(value, "on") || |
307 | !strcasecmp(value, "yes") || !strcasecmp(value, "true"); | |
ef416fc2 | 308 | } |
fa73b229 | 309 | else if (!strcasecmp(name, "format") && value[0]) |
ef416fc2 | 310 | { |
311 | /* | |
312 | * Set output format... | |
313 | */ | |
314 | ||
fa73b229 | 315 | if (strchr("cdfglnoprtv", value[0])) |
ef416fc2 | 316 | format = value[0]; |
317 | else | |
0837b7e8 MS |
318 | _cupsLangPrintFilter(stderr, "ERROR", |
319 | _("Unknown format character: \"%c\"."), | |
320 | value[0]); | |
ef416fc2 | 321 | } |
f42414bf | 322 | else if (!strcasecmp(name, "mode") && value[0]) |
323 | { | |
324 | /* | |
325 | * Set control/data order... | |
326 | */ | |
327 | ||
328 | if (!strcasecmp(value, "standard")) | |
bc44d920 | 329 | mode = MODE_STANDARD; |
f42414bf | 330 | else if (!strcasecmp(value, "stream")) |
bc44d920 | 331 | mode = MODE_STREAM; |
f42414bf | 332 | else |
0837b7e8 MS |
333 | _cupsLangPrintFilter(stderr, "ERROR", |
334 | _("Unknown print mode: \"%s\"."), value); | |
f42414bf | 335 | } |
fa73b229 | 336 | else if (!strcasecmp(name, "order") && value[0]) |
ef416fc2 | 337 | { |
338 | /* | |
339 | * Set control/data order... | |
340 | */ | |
341 | ||
fa73b229 | 342 | if (!strcasecmp(value, "control,data")) |
ef416fc2 | 343 | order = ORDER_CONTROL_DATA; |
fa73b229 | 344 | else if (!strcasecmp(value, "data,control")) |
ef416fc2 | 345 | order = ORDER_DATA_CONTROL; |
346 | else | |
0837b7e8 MS |
347 | _cupsLangPrintFilter(stderr, "ERROR", |
348 | _("Unknown file order: \"%s\"."), value); | |
ef416fc2 | 349 | } |
fa73b229 | 350 | else if (!strcasecmp(name, "reserve")) |
ef416fc2 | 351 | { |
352 | /* | |
353 | * Set port reservation mode... | |
354 | */ | |
355 | ||
fa73b229 | 356 | if (!value[0] || !strcasecmp(value, "on") || |
357 | !strcasecmp(value, "yes") || !strcasecmp(value, "true") || | |
ef416fc2 | 358 | !strcasecmp(value, "rfc1179")) |
359 | reserve = RESERVE_RFC1179; | |
360 | else if (!strcasecmp(value, "any")) | |
361 | reserve = RESERVE_ANY; | |
362 | else | |
363 | reserve = RESERVE_NONE; | |
364 | } | |
fa73b229 | 365 | else if (!strcasecmp(name, "manual_copies")) |
ef416fc2 | 366 | { |
367 | /* | |
368 | * Set manual copies... | |
369 | */ | |
370 | ||
fa73b229 | 371 | manual_copies = !value[0] || !strcasecmp(value, "on") || |
372 | !strcasecmp(value, "yes") || !strcasecmp(value, "true"); | |
ef416fc2 | 373 | } |
fa73b229 | 374 | else if (!strcasecmp(name, "sanitize_title")) |
ef416fc2 | 375 | { |
376 | /* | |
377 | * Set sanitize title... | |
378 | */ | |
379 | ||
fa73b229 | 380 | sanitize_title = !value[0] || !strcasecmp(value, "on") || |
8b116e60 | 381 | !strcasecmp(value, "yes") || !strcasecmp(value, "true"); |
ef416fc2 | 382 | } |
fa73b229 | 383 | else if (!strcasecmp(name, "timeout")) |
ef416fc2 | 384 | { |
385 | /* | |
386 | * Set the timeout... | |
387 | */ | |
388 | ||
389 | if (atoi(value) > 0) | |
390 | timeout = atoi(value); | |
391 | } | |
fa73b229 | 392 | else if (!strcasecmp(name, "contimeout")) |
393 | { | |
394 | /* | |
c0e1af83 | 395 | * Set the connection timeout... |
fa73b229 | 396 | */ |
397 | ||
398 | if (atoi(value) > 0) | |
399 | contimeout = atoi(value); | |
400 | } | |
ef416fc2 | 401 | } |
402 | } | |
403 | ||
f42414bf | 404 | if (mode == MODE_STREAM) |
405 | order = ORDER_CONTROL_DATA; | |
406 | ||
c8fef167 MS |
407 | /* |
408 | * Find the printer... | |
409 | */ | |
410 | ||
411 | snprintf(portname, sizeof(portname), "%d", port); | |
412 | ||
413 | fputs("STATE: +connecting-to-device\n", stderr); | |
414 | fprintf(stderr, "DEBUG: Looking up \"%s\"...\n", hostname); | |
415 | ||
416 | while ((addrlist = httpAddrGetList(hostname, AF_UNSPEC, portname)) == NULL) | |
417 | { | |
418 | _cupsLangPrintFilter(stderr, "INFO", | |
419 | _("Unable to locate printer \"%s\"."), hostname); | |
420 | sleep(10); | |
421 | ||
422 | if (getenv("CLASS") != NULL) | |
423 | { | |
424 | fputs("STATE: -connecting-to-device\n", stderr); | |
425 | exit(CUPS_BACKEND_FAILED); | |
426 | } | |
427 | } | |
428 | ||
429 | snmp_fd = _cupsSNMPOpen(addrlist->addr.addr.sa_family); | |
430 | ||
431 | /* | |
432 | * Wait for data from the filter... | |
433 | */ | |
434 | ||
435 | if (argc == 6) | |
eac3a0a0 | 436 | { |
f14324a7 | 437 | if (!backendWaitLoop(snmp_fd, &(addrlist->addr), 0, backendNetworkSideCB)) |
c8fef167 | 438 | return (CUPS_BACKEND_OK); |
eac3a0a0 MS |
439 | else if (mode == MODE_STANDARD && |
440 | (bytes = read(0, buffer, sizeof(buffer))) <= 0) | |
441 | return (CUPS_BACKEND_OK); | |
442 | } | |
c8fef167 | 443 | |
f42414bf | 444 | /* |
445 | * If we have 7 arguments, print the file named on the command-line. | |
446 | * Otherwise, copy stdin to a temporary file and print the temporary | |
447 | * file. | |
448 | */ | |
449 | ||
450 | if (argc == 6 && mode == MODE_STANDARD) | |
451 | { | |
452 | /* | |
453 | * Copy stdin to a temporary file... | |
454 | */ | |
455 | ||
f42414bf | 456 | if ((fd = cupsTempFd(tmpfilename, sizeof(tmpfilename))) < 0) |
457 | { | |
c7017ecc | 458 | perror("DEBUG: Unable to create temporary file"); |
f42414bf | 459 | return (CUPS_BACKEND_FAILED); |
460 | } | |
461 | ||
0837b7e8 | 462 | _cupsLangPrintFilter(stderr, "INFO", _("Copying print data.")); |
8b116e60 | 463 | |
eac3a0a0 MS |
464 | if (bytes > 0) |
465 | write(fd, buffer, bytes); | |
466 | ||
467 | backendRunLoop(-1, fd, snmp_fd, &(addrlist->addr), 0, 0, | |
ef55b745 | 468 | backendNetworkSideCB); |
f42414bf | 469 | } |
470 | else if (argc == 6) | |
471 | { | |
472 | /* | |
473 | * Stream from stdin... | |
474 | */ | |
475 | ||
476 | filename = NULL; | |
477 | fd = 0; | |
478 | } | |
479 | else | |
480 | { | |
481 | filename = argv[6]; | |
482 | fd = open(filename, O_RDONLY); | |
483 | ||
484 | if (fd == -1) | |
485 | { | |
0837b7e8 | 486 | _cupsLangPrintError("ERROR", _("Unable to open print file")); |
f42414bf | 487 | return (CUPS_BACKEND_FAILED); |
488 | } | |
489 | } | |
490 | ||
ef416fc2 | 491 | /* |
492 | * Sanitize the document title... | |
493 | */ | |
494 | ||
495 | strlcpy(title, argv[3], sizeof(title)); | |
496 | ||
497 | if (sanitize_title) | |
498 | { | |
499 | /* | |
500 | * Sanitize the title string so that we don't cause problems on | |
501 | * the remote end... | |
502 | */ | |
503 | ||
db1f069b MS |
504 | char *ptr; |
505 | ||
ef416fc2 | 506 | for (ptr = title; *ptr; ptr ++) |
507 | if (!isalnum(*ptr & 255) && !isspace(*ptr & 255)) | |
508 | *ptr = '_'; | |
509 | } | |
510 | ||
511 | /* | |
512 | * Queue the job... | |
513 | */ | |
514 | ||
515 | if (argc > 6) | |
516 | { | |
517 | if (manual_copies) | |
518 | { | |
519 | manual_copies = atoi(argv[4]); | |
520 | copies = 1; | |
521 | } | |
522 | else | |
523 | { | |
524 | manual_copies = 1; | |
525 | copies = atoi(argv[4]); | |
526 | } | |
527 | ||
c8fef167 MS |
528 | status = lpd_queue(hostname, addrlist, resource + 1, fd, snmp_fd, mode, |
529 | username, title, copies, banner, format, order, reserve, | |
530 | manual_copies, timeout, contimeout); | |
ef416fc2 | 531 | |
532 | if (!status) | |
533 | fprintf(stderr, "PAGE: 1 %d\n", atoi(argv[4])); | |
534 | } | |
535 | else | |
c8fef167 MS |
536 | status = lpd_queue(hostname, addrlist, resource + 1, fd, snmp_fd, mode, |
537 | username, title, 1, banner, format, order, reserve, 1, | |
fa73b229 | 538 | timeout, contimeout); |
ef416fc2 | 539 | |
540 | /* | |
541 | * Remove the temporary file if necessary... | |
542 | */ | |
543 | ||
544 | if (tmpfilename[0]) | |
545 | unlink(tmpfilename); | |
546 | ||
f42414bf | 547 | if (fd) |
548 | close(fd); | |
549 | ||
c8fef167 MS |
550 | if (snmp_fd >= 0) |
551 | _cupsSNMPClose(snmp_fd); | |
552 | ||
ef416fc2 | 553 | /* |
554 | * Return the queue status... | |
555 | */ | |
556 | ||
557 | return (status); | |
558 | } | |
559 | ||
560 | ||
561 | /* | |
562 | * 'lpd_command()' - Send an LPR command sequence and wait for a reply. | |
563 | */ | |
564 | ||
565 | static int /* O - Status of command */ | |
566 | lpd_command(int fd, /* I - Socket connection to LPD host */ | |
567 | int timeout, /* I - Seconds to wait for a response */ | |
568 | char *format, /* I - printf()-style format string */ | |
569 | ...) /* I - Additional args as necessary */ | |
570 | { | |
571 | va_list ap; /* Argument pointer */ | |
572 | char buf[1024]; /* Output buffer */ | |
573 | int bytes; /* Number of bytes to output */ | |
574 | char status; /* Status from command */ | |
575 | ||
576 | ||
577 | /* | |
c8fef167 | 578 | * Don't try to send commands if the job has been canceled... |
ef416fc2 | 579 | */ |
580 | ||
581 | if (abort_job) | |
582 | return (-1); | |
583 | ||
584 | /* | |
585 | * Format the string... | |
586 | */ | |
587 | ||
588 | va_start(ap, format); | |
589 | bytes = vsnprintf(buf, sizeof(buf), format, ap); | |
590 | va_end(ap); | |
591 | ||
592 | fprintf(stderr, "DEBUG: lpd_command %2.2x %s", buf[0], buf + 1); | |
593 | ||
594 | /* | |
595 | * Send the command... | |
596 | */ | |
597 | ||
598 | fprintf(stderr, "DEBUG: Sending command string (%d bytes)...\n", bytes); | |
599 | ||
600 | if (lpd_write(fd, buf, bytes) < bytes) | |
601 | { | |
c7017ecc | 602 | perror("DEBUG: Unable to send LPD command"); |
ef416fc2 | 603 | return (-1); |
604 | } | |
605 | ||
606 | /* | |
607 | * Read back the status from the command and return it... | |
608 | */ | |
609 | ||
c0e1af83 | 610 | fputs("DEBUG: Reading command status...\n", stderr); |
ef416fc2 | 611 | |
612 | alarm(timeout); | |
613 | ||
614 | if (recv(fd, &status, 1, 0) < 1) | |
615 | { | |
0837b7e8 MS |
616 | _cupsLangPrintFilter(stderr, "WARNING", |
617 | _("Printer did not respond after %d seconds."), | |
618 | timeout); | |
ef416fc2 | 619 | status = errno; |
620 | } | |
621 | ||
622 | alarm(0); | |
623 | ||
624 | fprintf(stderr, "DEBUG: lpd_command returning %d\n", status); | |
625 | ||
626 | return (status); | |
627 | } | |
628 | ||
629 | ||
630 | /* | |
631 | * 'lpd_queue()' - Queue a file using the Line Printer Daemon protocol. | |
632 | */ | |
633 | ||
634 | static int /* O - Zero on success, non-zero on failure */ | |
c8fef167 MS |
635 | lpd_queue(const char *hostname, /* I - Host to connect to */ |
636 | http_addrlist_t *addrlist, /* I - List of host addresses */ | |
637 | const char *printer, /* I - Printer/queue name */ | |
638 | int print_fd, /* I - File to print */ | |
639 | int snmp_fd, /* I - SNMP socket */ | |
640 | int mode, /* I - Print mode */ | |
641 | const char *user, /* I - Requesting user */ | |
642 | const char *title, /* I - Job title */ | |
643 | int copies, /* I - Number of copies */ | |
644 | int banner, /* I - Print LPD banner? */ | |
645 | int format, /* I - Format specifier */ | |
646 | int order, /* I - Order of data/control files */ | |
647 | int reserve, /* I - Reserve ports? */ | |
648 | int manual_copies,/* I - Do copies by hand... */ | |
649 | int timeout, /* I - Timeout... */ | |
650 | int contimeout) /* I - Connection timeout */ | |
ef416fc2 | 651 | { |
ef416fc2 | 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 */ | |
c0e1af83 | 660 | int delay; /* Delay for retries... */ |
26d47ec6 | 661 | char addrname[256]; /* Address name */ |
c8fef167 MS |
662 | http_addrlist_t *addr; /* Socket address */ |
663 | int have_supplies; /* Printer supports supply levels? */ | |
ef416fc2 | 664 | int copy; /* Copies written */ |
fa73b229 | 665 | time_t start_time; /* Time of first connect */ |
ef416fc2 | 666 | size_t nbytes; /* Number of bytes written */ |
667 | off_t tbytes; /* Total bytes written */ | |
a74454a7 | 668 | char buffer[32768]; /* Output buffer */ |
ef416fc2 | 669 | #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET) |
670 | struct sigaction action; /* Actions for POSIX signals */ | |
671 | #endif /* HAVE_SIGACTION && !HAVE_SIGSET */ | |
672 | ||
673 | ||
674 | /* | |
675 | * Setup an alarm handler for timeouts... | |
676 | */ | |
677 | ||
678 | #ifdef HAVE_SIGSET /* Use System V signals over POSIX to avoid bugs */ | |
679 | sigset(SIGALRM, lpd_timeout); | |
680 | #elif defined(HAVE_SIGACTION) | |
681 | memset(&action, 0, sizeof(action)); | |
682 | ||
683 | sigemptyset(&action.sa_mask); | |
684 | action.sa_handler = lpd_timeout; | |
685 | sigaction(SIGALRM, &action, NULL); | |
686 | #else | |
687 | signal(SIGALRM, lpd_timeout); | |
688 | #endif /* HAVE_SIGSET */ | |
689 | ||
fa73b229 | 690 | /* |
c0e1af83 | 691 | * Remember when we started trying to connect to the printer... |
fa73b229 | 692 | */ |
693 | ||
4d301e69 | 694 | start_time = time(NULL); |
fa73b229 | 695 | |
ef416fc2 | 696 | /* |
697 | * Loop forever trying to print the file... | |
698 | */ | |
699 | ||
700 | while (!abort_job) | |
701 | { | |
702 | /* | |
703 | * First try to reserve a port for this connection... | |
704 | */ | |
705 | ||
acb056cb | 706 | fprintf(stderr, "DEBUG: Connecting to %s:%d for printer %s\n", hostname, |
c8fef167 | 707 | _httpAddrPort(&(addrlist->addr)), printer); |
0837b7e8 | 708 | _cupsLangPrintFilter(stderr, "INFO", _("Connecting to printer.")); |
ef416fc2 | 709 | |
c0e1af83 | 710 | for (lport = reserve == RESERVE_RFC1179 ? 732 : 1024, addr = addrlist, |
711 | delay = 5;; | |
ef416fc2 | 712 | addr = addr->next) |
713 | { | |
714 | /* | |
c8fef167 | 715 | * Stop if this job has been canceled... |
ef416fc2 | 716 | */ |
717 | ||
718 | if (abort_job) | |
ef416fc2 | 719 | return (CUPS_BACKEND_FAILED); |
ef416fc2 | 720 | |
721 | /* | |
722 | * Choose the next priviledged port... | |
723 | */ | |
724 | ||
725 | if (!addr) | |
726 | addr = addrlist; | |
727 | ||
728 | lport --; | |
729 | ||
730 | if (lport < 721 && reserve == RESERVE_RFC1179) | |
731 | lport = 731; | |
732 | else if (lport < 1) | |
733 | lport = 1023; | |
734 | ||
735 | #ifdef HAVE_GETEUID | |
736 | if (geteuid() || !reserve) | |
737 | #else | |
738 | if (getuid() || !reserve) | |
739 | #endif /* HAVE_GETEUID */ | |
740 | { | |
741 | /* | |
742 | * Just create a regular socket... | |
743 | */ | |
744 | ||
745 | if ((fd = socket(addr->addr.addr.sa_family, SOCK_STREAM, 0)) < 0) | |
746 | { | |
c7017ecc | 747 | perror("DEBUG: Unable to create socket"); |
ef416fc2 | 748 | sleep(1); |
749 | ||
750 | continue; | |
751 | } | |
752 | ||
753 | lport = 0; | |
754 | } | |
755 | else | |
756 | { | |
757 | /* | |
758 | * We're running as root and want to comply with RFC 1179. Reserve a | |
759 | * priviledged lport between 721 and 731... | |
760 | */ | |
761 | ||
762 | if ((fd = rresvport_af(&lport, addr->addr.addr.sa_family)) < 0) | |
763 | { | |
c7017ecc | 764 | perror("DEBUG: Unable to reserve port"); |
ef416fc2 | 765 | sleep(1); |
766 | ||
767 | continue; | |
768 | } | |
769 | } | |
770 | ||
771 | /* | |
772 | * Connect to the printer or server... | |
773 | */ | |
774 | ||
775 | if (abort_job) | |
776 | { | |
ef416fc2 | 777 | close(fd); |
778 | ||
779 | return (CUPS_BACKEND_FAILED); | |
780 | } | |
781 | ||
782 | if (!connect(fd, &(addr->addr.addr), httpAddrLength(&(addr->addr)))) | |
783 | break; | |
784 | ||
785 | error = errno; | |
786 | close(fd); | |
ef416fc2 | 787 | |
788 | if (addr->next) | |
789 | continue; | |
790 | ||
791 | if (getenv("CLASS") != NULL) | |
792 | { | |
793 | /* | |
794 | * If the CLASS environment variable is set, the job was submitted | |
795 | * to a class and not to a specific queue. In this case, we want | |
796 | * to abort immediately so that the job can be requeued on the next | |
797 | * available printer in the class. | |
798 | */ | |
799 | ||
0837b7e8 MS |
800 | _cupsLangPrintFilter(stderr, "INFO", |
801 | _("Unable to contact printer, queuing on next " | |
802 | "printer in class.")); | |
ef416fc2 | 803 | |
ef416fc2 | 804 | /* |
805 | * Sleep 5 seconds to keep the job from requeuing too rapidly... | |
806 | */ | |
807 | ||
808 | sleep(5); | |
809 | ||
810 | return (CUPS_BACKEND_FAILED); | |
811 | } | |
812 | ||
c7017ecc MS |
813 | fprintf(stderr, "DEBUG: Connection error: %s\n", strerror(error)); |
814 | ||
ef416fc2 | 815 | if (error == ECONNREFUSED || error == EHOSTDOWN || |
816 | error == EHOSTUNREACH) | |
817 | { | |
fa73b229 | 818 | if (contimeout && (time(NULL) - start_time) > contimeout) |
819 | { | |
0837b7e8 MS |
820 | _cupsLangPrintFilter(stderr, "ERROR", |
821 | _("The printer is not responding.")); | |
fa73b229 | 822 | return (CUPS_BACKEND_FAILED); |
823 | } | |
824 | ||
c7017ecc MS |
825 | switch (error) |
826 | { | |
827 | case EHOSTDOWN : | |
0837b7e8 | 828 | _cupsLangPrintFilter(stderr, "WARNING", |
22c9029b MS |
829 | _("The printer may not exist or " |
830 | "is unavailable at this time.")); | |
c7017ecc MS |
831 | break; |
832 | ||
833 | case EHOSTUNREACH : | |
0837b7e8 | 834 | _cupsLangPrintFilter(stderr, "WARNING", |
22c9029b MS |
835 | _("The printer is unreachable at " |
836 | "this time.")); | |
c7017ecc MS |
837 | break; |
838 | ||
839 | case ECONNREFUSED : | |
840 | default : | |
0837b7e8 | 841 | _cupsLangPrintFilter(stderr, "WARNING", |
22c9029b | 842 | _("The printer is busy.")); |
c7017ecc MS |
843 | break; |
844 | } | |
c0e1af83 | 845 | |
846 | sleep(delay); | |
847 | ||
848 | if (delay < 30) | |
849 | delay += 5; | |
ef416fc2 | 850 | } |
851 | else if (error == EADDRINUSE) | |
852 | { | |
853 | /* | |
854 | * Try on another port... | |
855 | */ | |
856 | ||
857 | sleep(1); | |
858 | } | |
859 | else | |
860 | { | |
0837b7e8 | 861 | _cupsLangPrintFilter(stderr, "ERROR", |
22c9029b | 862 | _("The printer is not responding.")); |
c0e1af83 | 863 | sleep(30); |
ef416fc2 | 864 | } |
865 | } | |
866 | ||
757d2cad | 867 | fputs("STATE: -connecting-to-device\n", stderr); |
0837b7e8 | 868 | _cupsLangPrintFilter(stderr, "INFO", _("Connected to printer.")); |
26d47ec6 | 869 | |
22c9029b MS |
870 | fprintf(stderr, "DEBUG: Connected to %s:%d (local port %d)...\n", |
871 | httpAddrString(&(addr->addr), addrname, sizeof(addrname)), | |
872 | _httpAddrPort(&(addr->addr)), lport); | |
ef416fc2 | 873 | |
7a14d768 MS |
874 | /* |
875 | * See if the printer supports SNMP... | |
876 | */ | |
877 | ||
c8fef167 MS |
878 | if (snmp_fd >= 0) |
879 | have_supplies = !backendSNMPSupplies(snmp_fd, &(addrlist->addr), NULL, | |
880 | NULL); | |
426c6a59 MS |
881 | else |
882 | have_supplies = 0; | |
7a14d768 MS |
883 | |
884 | /* | |
885 | * Check for side-channel requests... | |
886 | */ | |
887 | ||
c8fef167 | 888 | backendCheckSideChannel(snmp_fd, &(addrlist->addr)); |
7a14d768 | 889 | |
ef416fc2 | 890 | /* |
891 | * Next, open the print file and figure out its size... | |
892 | */ | |
893 | ||
f42414bf | 894 | if (print_fd) |
ef416fc2 | 895 | { |
c0e1af83 | 896 | /* |
897 | * Use the size from the print file... | |
898 | */ | |
899 | ||
f42414bf | 900 | if (fstat(print_fd, &filestats)) |
901 | { | |
f42414bf | 902 | close(fd); |
ef416fc2 | 903 | |
c7017ecc | 904 | perror("DEBUG: unable to stat print file"); |
f42414bf | 905 | return (CUPS_BACKEND_FAILED); |
906 | } | |
ef416fc2 | 907 | |
f42414bf | 908 | filestats.st_size *= manual_copies; |
ef416fc2 | 909 | } |
f42414bf | 910 | else |
c0e1af83 | 911 | { |
912 | /* | |
913 | * Use a "very large value" for the size so that the printer will | |
914 | * keep printing until we close the connection... | |
915 | */ | |
916 | ||
f42414bf | 917 | #ifdef _LARGEFILE_SOURCE |
918 | filestats.st_size = (size_t)(999999999999.0); | |
919 | #else | |
920 | filestats.st_size = 2147483647; | |
921 | #endif /* _LARGEFILE_SOURCE */ | |
c0e1af83 | 922 | } |
ef416fc2 | 923 | |
924 | /* | |
925 | * Send a job header to the printer, specifying no banner page and | |
926 | * literal output... | |
927 | */ | |
928 | ||
929 | if (lpd_command(fd, timeout, "\002%s\n", | |
930 | printer)) /* Receive print job(s) */ | |
931 | { | |
ef416fc2 | 932 | close(fd); |
933 | return (CUPS_BACKEND_FAILED); | |
934 | } | |
935 | ||
757d2cad | 936 | httpGetHostname(NULL, localhost, sizeof(localhost)); |
ef416fc2 | 937 | |
938 | snprintf(control, sizeof(control), | |
939 | "H%.31s\n" /* RFC 1179, Section 7.2 - host name <= 31 chars */ | |
940 | "P%.31s\n" /* RFC 1179, Section 7.2 - user name <= 31 chars */ | |
941 | "J%.99s\n", /* RFC 1179, Section 7.2 - job name <= 99 chars */ | |
942 | localhost, user, title); | |
943 | cptr = control + strlen(control); | |
944 | ||
945 | if (banner) | |
946 | { | |
947 | snprintf(cptr, sizeof(control) - (cptr - control), | |
948 | "C%.31s\n" /* RFC 1179, Section 7.2 - class name <= 31 chars */ | |
949 | "L%s\n", | |
950 | localhost, user); | |
951 | cptr += strlen(cptr); | |
952 | } | |
953 | ||
954 | while (copies > 0) | |
955 | { | |
956 | snprintf(cptr, sizeof(control) - (cptr - control), "%cdfA%03d%.15s\n", | |
957 | format, (int)getpid() % 1000, localhost); | |
958 | cptr += strlen(cptr); | |
959 | copies --; | |
960 | } | |
961 | ||
962 | snprintf(cptr, sizeof(control) - (cptr - control), | |
963 | "UdfA%03d%.15s\n" | |
964 | "N%.131s\n", /* RFC 1179, Section 7.2 - sourcefile name <= 131 chars */ | |
965 | (int)getpid() % 1000, localhost, title); | |
966 | ||
967 | fprintf(stderr, "DEBUG: Control file is:\n%s", control); | |
968 | ||
969 | if (order == ORDER_CONTROL_DATA) | |
970 | { | |
7a14d768 MS |
971 | /* |
972 | * Check for side-channel requests... | |
973 | */ | |
974 | ||
975 | backendCheckSideChannel(snmp_fd, &(addr->addr)); | |
976 | ||
977 | /* | |
978 | * Send the control file... | |
979 | */ | |
980 | ||
ef416fc2 | 981 | if (lpd_command(fd, timeout, "\002%d cfA%03.3d%.15s\n", strlen(control), |
982 | (int)getpid() % 1000, localhost)) | |
983 | { | |
ef416fc2 | 984 | close(fd); |
985 | ||
986 | return (CUPS_BACKEND_FAILED); | |
987 | } | |
988 | ||
0837b7e8 MS |
989 | fprintf(stderr, "DEBUG: Sending control file (%u bytes)\n", |
990 | (unsigned)strlen(control)); | |
ef416fc2 | 991 | |
992 | if (lpd_write(fd, control, strlen(control) + 1) < (strlen(control) + 1)) | |
993 | { | |
994 | status = errno; | |
c7017ecc MS |
995 | perror("DEBUG: Unable to write control file"); |
996 | ||
ef416fc2 | 997 | } |
998 | else | |
999 | { | |
1000 | alarm(timeout); | |
1001 | ||
1002 | if (read(fd, &status, 1) < 1) | |
1003 | { | |
0837b7e8 MS |
1004 | _cupsLangPrintFilter(stderr, "WARNING", |
1005 | _("Printer did not respond after %d seconds."), | |
1006 | timeout); | |
ef416fc2 | 1007 | status = errno; |
1008 | } | |
1009 | ||
1010 | alarm(0); | |
1011 | } | |
1012 | ||
1013 | if (status != 0) | |
0837b7e8 MS |
1014 | _cupsLangPrintFilter(stderr, "ERROR", |
1015 | _("Remote host did not accept control file (%d)."), | |
1016 | status); | |
ef416fc2 | 1017 | else |
0837b7e8 MS |
1018 | _cupsLangPrintFilter(stderr, "INFO", |
1019 | _("Control file sent successfully.")); | |
ef416fc2 | 1020 | } |
1021 | else | |
1022 | status = 0; | |
1023 | ||
1024 | if (status == 0) | |
1025 | { | |
7a14d768 MS |
1026 | /* |
1027 | * Check for side-channel requests... | |
1028 | */ | |
1029 | ||
1030 | backendCheckSideChannel(snmp_fd, &(addr->addr)); | |
1031 | ||
ef416fc2 | 1032 | /* |
1033 | * Send the print file... | |
1034 | */ | |
1035 | ||
1036 | if (lpd_command(fd, timeout, "\003" CUPS_LLFMT " dfA%03.3d%.15s\n", | |
1037 | CUPS_LLCAST filestats.st_size, (int)getpid() % 1000, | |
1038 | localhost)) | |
1039 | { | |
ef416fc2 | 1040 | close(fd); |
1041 | ||
1042 | return (CUPS_BACKEND_FAILED); | |
1043 | } | |
1044 | ||
0837b7e8 MS |
1045 | fprintf(stderr, "DEBUG: Sending data file (" CUPS_LLFMT " bytes)\n", |
1046 | CUPS_LLCAST filestats.st_size); | |
ef416fc2 | 1047 | |
1048 | tbytes = 0; | |
1049 | for (copy = 0; copy < manual_copies; copy ++) | |
1050 | { | |
f42414bf | 1051 | lseek(print_fd, 0, SEEK_SET); |
ef416fc2 | 1052 | |
f42414bf | 1053 | while ((nbytes = read(print_fd, buffer, sizeof(buffer))) > 0) |
ef416fc2 | 1054 | { |
0837b7e8 MS |
1055 | _cupsLangPrintFilter(stderr, "INFO", |
1056 | _("Spooling job, %.0f%% complete."), | |
1057 | 100.0 * tbytes / filestats.st_size); | |
ef416fc2 | 1058 | |
1059 | if (lpd_write(fd, buffer, nbytes) < nbytes) | |
1060 | { | |
c7017ecc | 1061 | perror("DEBUG: Unable to send print file to printer"); |
ef416fc2 | 1062 | break; |
1063 | } | |
1064 | else | |
1065 | tbytes += nbytes; | |
1066 | } | |
1067 | } | |
1068 | ||
f42414bf | 1069 | if (mode == MODE_STANDARD) |
ef416fc2 | 1070 | { |
f42414bf | 1071 | if (tbytes < filestats.st_size) |
1072 | status = errno; | |
1073 | else if (lpd_write(fd, "", 1) < 1) | |
1074 | { | |
c7017ecc | 1075 | perror("DEBUG: Unable to send trailing nul to printer"); |
f42414bf | 1076 | status = errno; |
1077 | } | |
1078 | else | |
1079 | { | |
1080 | /* | |
1081 | * Read the status byte from the printer; if we can't read the byte | |
1082 | * back now, we should set status to "errno", however at this point | |
1083 | * we know the printer got the whole file and we don't necessarily | |
1084 | * want to requeue it over and over... | |
1085 | */ | |
ef416fc2 | 1086 | |
f42414bf | 1087 | alarm(timeout); |
ef416fc2 | 1088 | |
f42414bf | 1089 | if (recv(fd, &status, 1, 0) < 1) |
1090 | { | |
0837b7e8 MS |
1091 | _cupsLangPrintFilter(stderr, "WARNING", |
1092 | _("Printer did not respond after %d seconds."), | |
1093 | timeout); | |
f42414bf | 1094 | status = 0; |
1095 | } | |
ef416fc2 | 1096 | |
f42414bf | 1097 | alarm(0); |
1098 | } | |
ef416fc2 | 1099 | } |
f42414bf | 1100 | else |
1101 | status = 0; | |
ef416fc2 | 1102 | |
1103 | if (status != 0) | |
0837b7e8 MS |
1104 | _cupsLangPrintFilter(stderr, "ERROR", |
1105 | _("Remote host did not accept data file (%d)."), | |
1106 | status); | |
ef416fc2 | 1107 | else |
0837b7e8 MS |
1108 | _cupsLangPrintFilter(stderr, "INFO", |
1109 | _("Data file sent successfully.")); | |
ef416fc2 | 1110 | } |
1111 | ||
1112 | if (status == 0 && order == ORDER_DATA_CONTROL) | |
1113 | { | |
7a14d768 MS |
1114 | /* |
1115 | * Check for side-channel requests... | |
1116 | */ | |
1117 | ||
1118 | backendCheckSideChannel(snmp_fd, &(addr->addr)); | |
1119 | ||
1120 | /* | |
1121 | * Send control file... | |
1122 | */ | |
1123 | ||
ef416fc2 | 1124 | if (lpd_command(fd, timeout, "\002%d cfA%03.3d%.15s\n", strlen(control), |
1125 | (int)getpid() % 1000, localhost)) | |
1126 | { | |
ef416fc2 | 1127 | close(fd); |
1128 | ||
1129 | return (CUPS_BACKEND_FAILED); | |
1130 | } | |
1131 | ||
0837b7e8 MS |
1132 | fprintf(stderr, "DEBUG: Sending control file (%lu bytes)\n", |
1133 | (unsigned long)strlen(control)); | |
ef416fc2 | 1134 | |
1135 | if (lpd_write(fd, control, strlen(control) + 1) < (strlen(control) + 1)) | |
1136 | { | |
1137 | status = errno; | |
c7017ecc | 1138 | perror("DEBUG: Unable to write control file"); |
ef416fc2 | 1139 | } |
1140 | else | |
1141 | { | |
1142 | alarm(timeout); | |
1143 | ||
1144 | if (read(fd, &status, 1) < 1) | |
1145 | { | |
0837b7e8 MS |
1146 | _cupsLangPrintFilter(stderr, "WARNING", |
1147 | _("Printer did not respond after %d seconds."), | |
1148 | timeout); | |
ef416fc2 | 1149 | status = errno; |
1150 | } | |
1151 | ||
1152 | alarm(0); | |
1153 | } | |
1154 | ||
1155 | if (status != 0) | |
0837b7e8 MS |
1156 | _cupsLangPrintFilter(stderr, "ERROR", |
1157 | _("Remote host did not accept control file (%d)."), | |
1158 | status); | |
ef416fc2 | 1159 | else |
0837b7e8 MS |
1160 | _cupsLangPrintFilter(stderr, "INFO", |
1161 | _("Control file sent successfully.")); | |
ef416fc2 | 1162 | } |
1163 | ||
7a14d768 | 1164 | /* |
52f6f666 | 1165 | * Collect the final supply levels as needed... |
7a14d768 MS |
1166 | */ |
1167 | ||
426c6a59 | 1168 | if (have_supplies) |
52f6f666 | 1169 | backendSNMPSupplies(snmp_fd, &(addr->addr), NULL, NULL); |
7a14d768 | 1170 | |
ef416fc2 | 1171 | /* |
1172 | * Close the socket connection and input file... | |
1173 | */ | |
1174 | ||
1175 | close(fd); | |
ef416fc2 | 1176 | |
1177 | if (status == 0) | |
ef416fc2 | 1178 | return (CUPS_BACKEND_OK); |
ef416fc2 | 1179 | |
1180 | /* | |
1181 | * Waiting for a retry... | |
1182 | */ | |
1183 | ||
1184 | sleep(30); | |
1185 | } | |
1186 | ||
ef416fc2 | 1187 | /* |
c8fef167 | 1188 | * If we get here, then the job has been canceled... |
ef416fc2 | 1189 | */ |
1190 | ||
1191 | return (CUPS_BACKEND_FAILED); | |
1192 | } | |
1193 | ||
1194 | ||
1195 | /* | |
1196 | * 'lpd_timeout()' - Handle timeout alarms... | |
1197 | */ | |
1198 | ||
1199 | static void | |
1200 | lpd_timeout(int sig) /* I - Signal number */ | |
1201 | { | |
1202 | (void)sig; | |
1203 | ||
1204 | #if !defined(HAVE_SIGSET) && !defined(HAVE_SIGACTION) | |
1205 | signal(SIGALRM, lpd_timeout); | |
1206 | #endif /* !HAVE_SIGSET && !HAVE_SIGACTION */ | |
1207 | } | |
1208 | ||
1209 | ||
1210 | /* | |
1211 | * 'lpd_write()' - Write a buffer of data to an LPD server. | |
1212 | */ | |
1213 | ||
1214 | static int /* O - Number of bytes written or -1 on error */ | |
1215 | lpd_write(int lpd_fd, /* I - LPD socket */ | |
1216 | char *buffer, /* I - Buffer to write */ | |
1217 | int length) /* I - Number of bytes to write */ | |
1218 | { | |
1219 | int bytes, /* Number of bytes written */ | |
1220 | total; /* Total number of bytes written */ | |
1221 | ||
1222 | ||
1223 | if (abort_job) | |
1224 | return (-1); | |
1225 | ||
1226 | total = 0; | |
1227 | while ((bytes = send(lpd_fd, buffer, length - total, 0)) >= 0) | |
1228 | { | |
1229 | total += bytes; | |
1230 | buffer += bytes; | |
1231 | ||
1232 | if (total == length) | |
1233 | break; | |
1234 | } | |
1235 | ||
1236 | if (bytes < 0) | |
1237 | return (-1); | |
1238 | else | |
1239 | return (length); | |
1240 | } | |
1241 | ||
1242 | ||
1243 | #ifndef HAVE_RRESVPORT_AF | |
1244 | /* | |
1245 | * 'rresvport_af()' - A simple implementation of rresvport_af(). | |
1246 | */ | |
1247 | ||
1248 | static int /* O - Socket or -1 on error */ | |
1249 | rresvport_af(int *port, /* IO - Port number to bind to */ | |
1250 | int family) /* I - Address family */ | |
1251 | { | |
1252 | http_addr_t addr; /* Socket address */ | |
1253 | int fd; /* Socket file descriptor */ | |
1254 | ||
1255 | ||
1256 | /* | |
1257 | * Try to create an IPv4 socket... | |
1258 | */ | |
1259 | ||
1260 | if ((fd = socket(family, SOCK_STREAM, 0)) < 0) | |
1261 | return (-1); | |
1262 | ||
1263 | /* | |
1264 | * Initialize the address buffer... | |
1265 | */ | |
1266 | ||
1267 | memset(&addr, 0, sizeof(addr)); | |
1268 | addr.addr.sa_family = family; | |
1269 | ||
1270 | /* | |
1271 | * Try to bind the socket to a reserved port... | |
1272 | */ | |
1273 | ||
1274 | while (*port > 511) | |
1275 | { | |
1276 | /* | |
1277 | * Set the port number... | |
1278 | */ | |
1279 | ||
22c9029b | 1280 | _httpAddrSetPort(&addr, *port); |
ef416fc2 | 1281 | |
1282 | /* | |
1283 | * Try binding the port to the socket; return if all is OK... | |
1284 | */ | |
1285 | ||
1286 | if (!bind(fd, (struct sockaddr *)&addr, sizeof(addr))) | |
1287 | return (fd); | |
1288 | ||
1289 | /* | |
1290 | * Stop if we have any error other than "address already in use"... | |
1291 | */ | |
1292 | ||
1293 | if (errno != EADDRINUSE) | |
1294 | { | |
1295 | # ifdef WIN32 | |
1296 | closesocket(fd); | |
1297 | # else | |
1298 | close(fd); | |
1299 | # endif /* WIN32 */ | |
1300 | ||
1301 | return (-1); | |
1302 | } | |
1303 | ||
1304 | /* | |
1305 | * Try the next port... | |
1306 | */ | |
1307 | ||
1308 | (*port)--; | |
1309 | } | |
1310 | ||
1311 | /* | |
1312 | * Wasn't able to bind to a reserved port, so close the socket and return | |
1313 | * -1... | |
1314 | */ | |
1315 | ||
1316 | # ifdef WIN32 | |
1317 | closesocket(fd); | |
1318 | # else | |
1319 | close(fd); | |
1320 | # endif /* WIN32 */ | |
1321 | ||
1322 | return (-1); | |
1323 | } | |
1324 | #endif /* !HAVE_RRESVPORT_AF */ | |
1325 | ||
1326 | ||
1327 | /* | |
1328 | * 'sigterm_handler()' - Handle 'terminate' signals that stop the backend. | |
1329 | */ | |
1330 | ||
1331 | static void | |
1332 | sigterm_handler(int sig) /* I - Signal */ | |
1333 | { | |
1334 | (void)sig; /* remove compiler warnings... */ | |
1335 | ||
1336 | abort_job = 1; | |
1337 | } | |
1338 | ||
1339 | ||
1340 | /* | |
b19ccc9e | 1341 | * End of "$Id: lpd.c 7740 2008-07-14 23:58:05Z mike $". |
ef416fc2 | 1342 | */ |