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