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