]>
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 | * | |
8b116e60 | 6 | * Copyright 2007-2009 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 */ |
acb056cb | 116 | char scheme[255], /* Scheme in URI */ |
7a14d768 MS |
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 | { | |
8b450588 MS |
177 | printf("network lpd \"Unknown\" \"%s\"\n", |
178 | _cupsLangString(cupsLangDefault(), _("LPD/LPR Host or Printer"))); | |
ef416fc2 | 179 | return (CUPS_BACKEND_OK); |
180 | } | |
181 | else if (argc < 6 || argc > 7) | |
182 | { | |
db1f069b MS |
183 | _cupsLangPrintf(stderr, |
184 | _("Usage: %s job-id user title copies options [file]\n"), | |
185 | argv[0]); | |
ef416fc2 | 186 | return (CUPS_BACKEND_FAILED); |
187 | } | |
188 | ||
ef416fc2 | 189 | /* |
190 | * Extract the hostname and printer name from the URI... | |
191 | */ | |
192 | ||
1f0275e3 MS |
193 | if ((device_uri = cupsBackendDeviceURI(argv)) == NULL) |
194 | return (CUPS_BACKEND_FAILED); | |
195 | ||
acb056cb MS |
196 | httpSeparateURI(HTTP_URI_CODING_ALL, device_uri, scheme, sizeof(scheme), |
197 | username, sizeof(username), 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") || |
8b116e60 | 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 | ||
e07d4801 MS |
426 | http_addrlist_t *addrlist; /* Address list */ |
427 | int snmp_fd; /* SNMP socket */ | |
f42414bf | 428 | |
429 | ||
acb056cb MS |
430 | fputs("STATE: +connecting-to-device\n", stderr); |
431 | fprintf(stderr, "DEBUG: Looking up \"%s\"...\n", hostname); | |
432 | ||
e07d4801 MS |
433 | if ((addrlist = httpAddrGetList(hostname, AF_UNSPEC, "1")) == NULL) |
434 | { | |
4d301e69 | 435 | _cupsLangPrintf(stderr, _("ERROR: Unable to locate printer \'%s\'\n"), |
e07d4801 MS |
436 | hostname); |
437 | return (CUPS_BACKEND_STOP); | |
438 | } | |
439 | ||
440 | snmp_fd = _cupsSNMPOpen(addrlist->addr.addr.sa_family); | |
441 | ||
f42414bf | 442 | if ((fd = cupsTempFd(tmpfilename, sizeof(tmpfilename))) < 0) |
443 | { | |
080811b1 | 444 | _cupsLangPrintError(_("ERROR: Unable to create temporary file")); |
f42414bf | 445 | return (CUPS_BACKEND_FAILED); |
446 | } | |
447 | ||
8b116e60 MS |
448 | _cupsLangPuts(stderr, _("INFO: Copying print data...\n")); |
449 | ||
4e6f60f0 | 450 | backendRunLoop(-1, fd, snmp_fd, &(addrlist->addr), 0, backendNetworkSideCB); |
e07d4801 MS |
451 | |
452 | if (snmp_fd >= 0) | |
453 | _cupsSNMPClose(snmp_fd); | |
454 | ||
455 | httpAddrFreeList(addrlist); | |
f42414bf | 456 | } |
457 | else if (argc == 6) | |
458 | { | |
459 | /* | |
460 | * Stream from stdin... | |
461 | */ | |
462 | ||
463 | filename = NULL; | |
464 | fd = 0; | |
465 | } | |
466 | else | |
467 | { | |
468 | filename = argv[6]; | |
469 | fd = open(filename, O_RDONLY); | |
470 | ||
471 | if (fd == -1) | |
472 | { | |
db1f069b MS |
473 | _cupsLangPrintf(stderr, _("ERROR: Unable to open print file %s: %s\n"), |
474 | filename, strerror(errno)); | |
f42414bf | 475 | return (CUPS_BACKEND_FAILED); |
476 | } | |
477 | } | |
478 | ||
ef416fc2 | 479 | /* |
480 | * Sanitize the document title... | |
481 | */ | |
482 | ||
483 | strlcpy(title, argv[3], sizeof(title)); | |
484 | ||
485 | if (sanitize_title) | |
486 | { | |
487 | /* | |
488 | * Sanitize the title string so that we don't cause problems on | |
489 | * the remote end... | |
490 | */ | |
491 | ||
db1f069b MS |
492 | char *ptr; |
493 | ||
ef416fc2 | 494 | for (ptr = title; *ptr; ptr ++) |
495 | if (!isalnum(*ptr & 255) && !isspace(*ptr & 255)) | |
496 | *ptr = '_'; | |
497 | } | |
498 | ||
499 | /* | |
500 | * Queue the job... | |
501 | */ | |
502 | ||
503 | if (argc > 6) | |
504 | { | |
505 | if (manual_copies) | |
506 | { | |
507 | manual_copies = atoi(argv[4]); | |
508 | copies = 1; | |
509 | } | |
510 | else | |
511 | { | |
512 | manual_copies = 1; | |
513 | copies = atoi(argv[4]); | |
514 | } | |
515 | ||
f42414bf | 516 | status = lpd_queue(hostname, port, resource + 1, fd, mode, |
ef416fc2 | 517 | username, title, copies, |
fa73b229 | 518 | banner, format, order, reserve, manual_copies, |
519 | timeout, contimeout); | |
ef416fc2 | 520 | |
521 | if (!status) | |
522 | fprintf(stderr, "PAGE: 1 %d\n", atoi(argv[4])); | |
523 | } | |
524 | else | |
f42414bf | 525 | status = lpd_queue(hostname, port, resource + 1, fd, mode, |
ef416fc2 | 526 | username, title, 1, |
fa73b229 | 527 | banner, format, order, reserve, 1, |
528 | timeout, contimeout); | |
ef416fc2 | 529 | |
530 | /* | |
531 | * Remove the temporary file if necessary... | |
532 | */ | |
533 | ||
534 | if (tmpfilename[0]) | |
535 | unlink(tmpfilename); | |
536 | ||
f42414bf | 537 | if (fd) |
538 | close(fd); | |
539 | ||
ef416fc2 | 540 | /* |
541 | * Return the queue status... | |
542 | */ | |
543 | ||
544 | return (status); | |
545 | } | |
546 | ||
547 | ||
548 | /* | |
549 | * 'lpd_command()' - Send an LPR command sequence and wait for a reply. | |
550 | */ | |
551 | ||
552 | static int /* O - Status of command */ | |
553 | lpd_command(int fd, /* I - Socket connection to LPD host */ | |
554 | int timeout, /* I - Seconds to wait for a response */ | |
555 | char *format, /* I - printf()-style format string */ | |
556 | ...) /* I - Additional args as necessary */ | |
557 | { | |
558 | va_list ap; /* Argument pointer */ | |
559 | char buf[1024]; /* Output buffer */ | |
560 | int bytes; /* Number of bytes to output */ | |
561 | char status; /* Status from command */ | |
562 | ||
563 | ||
564 | /* | |
565 | * Don't try to send commands if the job has been cancelled... | |
566 | */ | |
567 | ||
568 | if (abort_job) | |
569 | return (-1); | |
570 | ||
571 | /* | |
572 | * Format the string... | |
573 | */ | |
574 | ||
575 | va_start(ap, format); | |
576 | bytes = vsnprintf(buf, sizeof(buf), format, ap); | |
577 | va_end(ap); | |
578 | ||
579 | fprintf(stderr, "DEBUG: lpd_command %2.2x %s", buf[0], buf + 1); | |
580 | ||
581 | /* | |
582 | * Send the command... | |
583 | */ | |
584 | ||
585 | fprintf(stderr, "DEBUG: Sending command string (%d bytes)...\n", bytes); | |
586 | ||
587 | if (lpd_write(fd, buf, bytes) < bytes) | |
588 | { | |
080811b1 | 589 | _cupsLangPrintError(_("ERROR: Unable to send LPD command")); |
ef416fc2 | 590 | return (-1); |
591 | } | |
592 | ||
593 | /* | |
594 | * Read back the status from the command and return it... | |
595 | */ | |
596 | ||
c0e1af83 | 597 | fputs("DEBUG: Reading command status...\n", stderr); |
ef416fc2 | 598 | |
599 | alarm(timeout); | |
600 | ||
601 | if (recv(fd, &status, 1, 0) < 1) | |
602 | { | |
db1f069b MS |
603 | _cupsLangPrintf(stderr, |
604 | _("WARNING: Remote host did not respond with command " | |
4d301e69 | 605 | "status byte after %d seconds\n"), timeout); |
ef416fc2 | 606 | status = errno; |
607 | } | |
608 | ||
609 | alarm(0); | |
610 | ||
611 | fprintf(stderr, "DEBUG: lpd_command returning %d\n", status); | |
612 | ||
613 | return (status); | |
614 | } | |
615 | ||
616 | ||
617 | /* | |
618 | * 'lpd_queue()' - Queue a file using the Line Printer Daemon protocol. | |
619 | */ | |
620 | ||
621 | static int /* O - Zero on success, non-zero on failure */ | |
622 | lpd_queue(const char *hostname, /* I - Host to connect to */ | |
623 | int port, /* I - Port to connect on */ | |
624 | const char *printer, /* I - Printer/queue name */ | |
f42414bf | 625 | int print_fd, /* I - File to print */ |
626 | int mode, /* I - Print mode */ | |
ef416fc2 | 627 | const char *user, /* I - Requesting user */ |
628 | const char *title, /* I - Job title */ | |
629 | int copies, /* I - Number of copies */ | |
630 | int banner, /* I - Print LPD banner? */ | |
631 | int format, /* I - Format specifier */ | |
632 | int order, /* I - Order of data/control files */ | |
633 | int reserve, /* I - Reserve ports? */ | |
634 | int manual_copies, /* I - Do copies by hand... */ | |
fa73b229 | 635 | int timeout, /* I - Timeout... */ |
636 | int contimeout) /* I - Connection timeout */ | |
ef416fc2 | 637 | { |
ef416fc2 | 638 | char localhost[255]; /* Local host name */ |
639 | int error; /* Error number */ | |
640 | struct stat filestats; /* File statistics */ | |
641 | int lport; /* LPD connection local port */ | |
642 | int fd; /* LPD socket */ | |
643 | char control[10240], /* LPD control 'file' */ | |
644 | *cptr; /* Pointer into control file string */ | |
645 | char status; /* Status byte from command */ | |
646 | char portname[255]; /* Port name */ | |
c0e1af83 | 647 | int delay; /* Delay for retries... */ |
26d47ec6 | 648 | char addrname[256]; /* Address name */ |
ef416fc2 | 649 | http_addrlist_t *addrlist, /* Address list */ |
650 | *addr; /* Socket address */ | |
426c6a59 MS |
651 | int snmp_fd, /* SNMP socket */ |
652 | have_supplies; /* Printer supports supply levels? */ | |
ef416fc2 | 653 | int copy; /* Copies written */ |
fa73b229 | 654 | time_t start_time; /* Time of first connect */ |
ef416fc2 | 655 | size_t nbytes; /* Number of bytes written */ |
656 | off_t tbytes; /* Total bytes written */ | |
a74454a7 | 657 | char buffer[32768]; /* Output buffer */ |
ef416fc2 | 658 | #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET) |
659 | struct sigaction action; /* Actions for POSIX signals */ | |
660 | #endif /* HAVE_SIGACTION && !HAVE_SIGSET */ | |
661 | ||
662 | ||
663 | /* | |
664 | * Setup an alarm handler for timeouts... | |
665 | */ | |
666 | ||
667 | #ifdef HAVE_SIGSET /* Use System V signals over POSIX to avoid bugs */ | |
668 | sigset(SIGALRM, lpd_timeout); | |
669 | #elif defined(HAVE_SIGACTION) | |
670 | memset(&action, 0, sizeof(action)); | |
671 | ||
672 | sigemptyset(&action.sa_mask); | |
673 | action.sa_handler = lpd_timeout; | |
674 | sigaction(SIGALRM, &action, NULL); | |
675 | #else | |
676 | signal(SIGALRM, lpd_timeout); | |
677 | #endif /* HAVE_SIGSET */ | |
678 | ||
679 | /* | |
680 | * Find the printer... | |
681 | */ | |
682 | ||
683 | sprintf(portname, "%d", port); | |
684 | ||
acb056cb | 685 | fputs("STATE: +connecting-to-device\n", stderr); |
1340db2d MS |
686 | fprintf(stderr, "DEBUG: Looking up \"%s\"...\n", hostname); |
687 | ||
ef416fc2 | 688 | if ((addrlist = httpAddrGetList(hostname, AF_UNSPEC, portname)) == NULL) |
689 | { | |
4d301e69 | 690 | _cupsLangPrintf(stderr, _("ERROR: Unable to locate printer \'%s\'\n"), |
db1f069b | 691 | hostname); |
ef416fc2 | 692 | return (CUPS_BACKEND_STOP); |
693 | } | |
694 | ||
fa73b229 | 695 | /* |
c0e1af83 | 696 | * Remember when we started trying to connect to the printer... |
fa73b229 | 697 | */ |
698 | ||
4d301e69 | 699 | start_time = time(NULL); |
fa73b229 | 700 | |
ef416fc2 | 701 | /* |
702 | * Loop forever trying to print the file... | |
703 | */ | |
704 | ||
705 | while (!abort_job) | |
706 | { | |
707 | /* | |
708 | * First try to reserve a port for this connection... | |
709 | */ | |
710 | ||
acb056cb MS |
711 | fprintf(stderr, "DEBUG: Connecting to %s:%d for printer %s\n", hostname, |
712 | port, printer); | |
8b450588 | 713 | _cupsLangPuts(stderr, _("INFO: Connecting to printer...\n")); |
ef416fc2 | 714 | |
c0e1af83 | 715 | for (lport = reserve == RESERVE_RFC1179 ? 732 : 1024, addr = addrlist, |
716 | delay = 5;; | |
ef416fc2 | 717 | addr = addr->next) |
718 | { | |
719 | /* | |
720 | * Stop if this job has been cancelled... | |
721 | */ | |
722 | ||
723 | if (abort_job) | |
724 | { | |
725 | httpAddrFreeList(addrlist); | |
726 | ||
727 | return (CUPS_BACKEND_FAILED); | |
728 | } | |
729 | ||
730 | /* | |
731 | * Choose the next priviledged port... | |
732 | */ | |
733 | ||
734 | if (!addr) | |
735 | addr = addrlist; | |
736 | ||
737 | lport --; | |
738 | ||
739 | if (lport < 721 && reserve == RESERVE_RFC1179) | |
740 | lport = 731; | |
741 | else if (lport < 1) | |
742 | lport = 1023; | |
743 | ||
744 | #ifdef HAVE_GETEUID | |
745 | if (geteuid() || !reserve) | |
746 | #else | |
747 | if (getuid() || !reserve) | |
748 | #endif /* HAVE_GETEUID */ | |
749 | { | |
750 | /* | |
751 | * Just create a regular socket... | |
752 | */ | |
753 | ||
754 | if ((fd = socket(addr->addr.addr.sa_family, SOCK_STREAM, 0)) < 0) | |
755 | { | |
080811b1 | 756 | _cupsLangPrintError(_("ERROR: Unable to create socket")); |
ef416fc2 | 757 | sleep(1); |
758 | ||
759 | continue; | |
760 | } | |
761 | ||
762 | lport = 0; | |
763 | } | |
764 | else | |
765 | { | |
766 | /* | |
767 | * We're running as root and want to comply with RFC 1179. Reserve a | |
768 | * priviledged lport between 721 and 731... | |
769 | */ | |
770 | ||
771 | if ((fd = rresvport_af(&lport, addr->addr.addr.sa_family)) < 0) | |
772 | { | |
080811b1 | 773 | _cupsLangPrintError(_("ERROR: Unable to reserve port")); |
ef416fc2 | 774 | sleep(1); |
775 | ||
776 | continue; | |
777 | } | |
778 | } | |
779 | ||
780 | /* | |
781 | * Connect to the printer or server... | |
782 | */ | |
783 | ||
784 | if (abort_job) | |
785 | { | |
786 | httpAddrFreeList(addrlist); | |
787 | ||
788 | close(fd); | |
789 | ||
790 | return (CUPS_BACKEND_FAILED); | |
791 | } | |
792 | ||
793 | if (!connect(fd, &(addr->addr.addr), httpAddrLength(&(addr->addr)))) | |
794 | break; | |
795 | ||
796 | error = errno; | |
797 | close(fd); | |
798 | fd = -1; | |
799 | ||
800 | if (addr->next) | |
801 | continue; | |
802 | ||
803 | if (getenv("CLASS") != NULL) | |
804 | { | |
805 | /* | |
806 | * If the CLASS environment variable is set, the job was submitted | |
807 | * to a class and not to a specific queue. In this case, we want | |
808 | * to abort immediately so that the job can be requeued on the next | |
809 | * available printer in the class. | |
810 | */ | |
811 | ||
db1f069b MS |
812 | _cupsLangPuts(stderr, |
813 | _("INFO: Unable to contact printer, queuing on next " | |
814 | "printer in class...\n")); | |
ef416fc2 | 815 | |
816 | httpAddrFreeList(addrlist); | |
817 | ||
818 | /* | |
819 | * Sleep 5 seconds to keep the job from requeuing too rapidly... | |
820 | */ | |
821 | ||
822 | sleep(5); | |
823 | ||
824 | return (CUPS_BACKEND_FAILED); | |
825 | } | |
826 | ||
827 | if (error == ECONNREFUSED || error == EHOSTDOWN || | |
828 | error == EHOSTUNREACH) | |
829 | { | |
fa73b229 | 830 | if (contimeout && (time(NULL) - start_time) > contimeout) |
831 | { | |
4d301e69 | 832 | _cupsLangPuts(stderr, _("ERROR: Printer not responding\n")); |
fa73b229 | 833 | return (CUPS_BACKEND_FAILED); |
834 | } | |
835 | ||
db1f069b | 836 | _cupsLangPrintf(stderr, |
4d301e69 MS |
837 | _("WARNING: Network host \'%s\' is busy; will retry in " |
838 | "%d seconds...\n"), hostname, delay); | |
c0e1af83 | 839 | |
840 | sleep(delay); | |
841 | ||
842 | if (delay < 30) | |
843 | delay += 5; | |
ef416fc2 | 844 | } |
845 | else if (error == EADDRINUSE) | |
846 | { | |
847 | /* | |
848 | * Try on another port... | |
849 | */ | |
850 | ||
851 | sleep(1); | |
852 | } | |
853 | else | |
854 | { | |
c0e1af83 | 855 | fprintf(stderr, "DEBUG: Connection error: %s\n", strerror(errno)); |
db1f069b | 856 | _cupsLangPuts(stderr, |
4d301e69 MS |
857 | _("ERROR: Unable to connect to printer; will retry in 30 " |
858 | "seconds...\n")); | |
c0e1af83 | 859 | sleep(30); |
ef416fc2 | 860 | } |
861 | } | |
862 | ||
757d2cad | 863 | fputs("STATE: -connecting-to-device\n", stderr); |
8b450588 | 864 | _cupsLangPuts(stderr, _("INFO: Connected to printer...\n")); |
26d47ec6 | 865 | |
866 | #ifdef AF_INET6 | |
867 | if (addr->addr.addr.sa_family == AF_INET6) | |
868 | fprintf(stderr, "DEBUG: Connected to [%s]:%d (IPv6) (local port %d)...\n", | |
c0e1af83 | 869 | httpAddrString(&addr->addr, addrname, sizeof(addrname)), |
870 | ntohs(addr->addr.ipv6.sin6_port), lport); | |
26d47ec6 | 871 | else |
872 | #endif /* AF_INET6 */ | |
873 | if (addr->addr.addr.sa_family == AF_INET) | |
874 | fprintf(stderr, "DEBUG: Connected to %s:%d (IPv4) (local port %d)...\n", | |
c0e1af83 | 875 | httpAddrString(&addr->addr, addrname, sizeof(addrname)), |
876 | ntohs(addr->addr.ipv4.sin_port), lport); | |
ef416fc2 | 877 | |
7a14d768 MS |
878 | /* |
879 | * See if the printer supports SNMP... | |
880 | */ | |
881 | ||
882 | if ((snmp_fd = _cupsSNMPOpen(addr->addr.addr.sa_family)) >= 0) | |
426c6a59 MS |
883 | have_supplies = !backendSNMPSupplies(snmp_fd, &(addr->addr), NULL, NULL); |
884 | else | |
885 | have_supplies = 0; | |
7a14d768 MS |
886 | |
887 | /* | |
888 | * Check for side-channel requests... | |
889 | */ | |
890 | ||
891 | backendCheckSideChannel(snmp_fd, &(addr->addr)); | |
892 | ||
ef416fc2 | 893 | /* |
894 | * Next, open the print file and figure out its size... | |
895 | */ | |
896 | ||
f42414bf | 897 | if (print_fd) |
ef416fc2 | 898 | { |
c0e1af83 | 899 | /* |
900 | * Use the size from the print file... | |
901 | */ | |
902 | ||
f42414bf | 903 | if (fstat(print_fd, &filestats)) |
904 | { | |
905 | httpAddrFreeList(addrlist); | |
906 | close(fd); | |
ef416fc2 | 907 | |
080811b1 | 908 | _cupsLangPrintError(_("ERROR: unable to stat print file")); |
f42414bf | 909 | return (CUPS_BACKEND_FAILED); |
910 | } | |
ef416fc2 | 911 | |
f42414bf | 912 | filestats.st_size *= manual_copies; |
ef416fc2 | 913 | } |
f42414bf | 914 | else |
c0e1af83 | 915 | { |
916 | /* | |
917 | * Use a "very large value" for the size so that the printer will | |
918 | * keep printing until we close the connection... | |
919 | */ | |
920 | ||
f42414bf | 921 | #ifdef _LARGEFILE_SOURCE |
922 | filestats.st_size = (size_t)(999999999999.0); | |
923 | #else | |
924 | filestats.st_size = 2147483647; | |
925 | #endif /* _LARGEFILE_SOURCE */ | |
c0e1af83 | 926 | } |
ef416fc2 | 927 | |
928 | /* | |
929 | * Send a job header to the printer, specifying no banner page and | |
930 | * literal output... | |
931 | */ | |
932 | ||
933 | if (lpd_command(fd, timeout, "\002%s\n", | |
934 | printer)) /* Receive print job(s) */ | |
935 | { | |
936 | httpAddrFreeList(addrlist); | |
937 | close(fd); | |
938 | return (CUPS_BACKEND_FAILED); | |
939 | } | |
940 | ||
757d2cad | 941 | httpGetHostname(NULL, localhost, sizeof(localhost)); |
ef416fc2 | 942 | |
943 | snprintf(control, sizeof(control), | |
944 | "H%.31s\n" /* RFC 1179, Section 7.2 - host name <= 31 chars */ | |
945 | "P%.31s\n" /* RFC 1179, Section 7.2 - user name <= 31 chars */ | |
946 | "J%.99s\n", /* RFC 1179, Section 7.2 - job name <= 99 chars */ | |
947 | localhost, user, title); | |
948 | cptr = control + strlen(control); | |
949 | ||
950 | if (banner) | |
951 | { | |
952 | snprintf(cptr, sizeof(control) - (cptr - control), | |
953 | "C%.31s\n" /* RFC 1179, Section 7.2 - class name <= 31 chars */ | |
954 | "L%s\n", | |
955 | localhost, user); | |
956 | cptr += strlen(cptr); | |
957 | } | |
958 | ||
959 | while (copies > 0) | |
960 | { | |
961 | snprintf(cptr, sizeof(control) - (cptr - control), "%cdfA%03d%.15s\n", | |
962 | format, (int)getpid() % 1000, localhost); | |
963 | cptr += strlen(cptr); | |
964 | copies --; | |
965 | } | |
966 | ||
967 | snprintf(cptr, sizeof(control) - (cptr - control), | |
968 | "UdfA%03d%.15s\n" | |
969 | "N%.131s\n", /* RFC 1179, Section 7.2 - sourcefile name <= 131 chars */ | |
970 | (int)getpid() % 1000, localhost, title); | |
971 | ||
972 | fprintf(stderr, "DEBUG: Control file is:\n%s", control); | |
973 | ||
974 | if (order == ORDER_CONTROL_DATA) | |
975 | { | |
7a14d768 MS |
976 | /* |
977 | * Check for side-channel requests... | |
978 | */ | |
979 | ||
980 | backendCheckSideChannel(snmp_fd, &(addr->addr)); | |
981 | ||
982 | /* | |
983 | * Send the control file... | |
984 | */ | |
985 | ||
ef416fc2 | 986 | if (lpd_command(fd, timeout, "\002%d cfA%03.3d%.15s\n", strlen(control), |
987 | (int)getpid() % 1000, localhost)) | |
988 | { | |
989 | httpAddrFreeList(addrlist); | |
990 | close(fd); | |
991 | ||
992 | return (CUPS_BACKEND_FAILED); | |
993 | } | |
994 | ||
db1f069b MS |
995 | _cupsLangPrintf(stderr, _("INFO: Sending control file (%u bytes)\n"), |
996 | (unsigned)strlen(control)); | |
ef416fc2 | 997 | |
998 | if (lpd_write(fd, control, strlen(control) + 1) < (strlen(control) + 1)) | |
999 | { | |
1000 | status = errno; | |
080811b1 | 1001 | _cupsLangPrintError(_("ERROR: Unable to write control file")); |
ef416fc2 | 1002 | } |
1003 | else | |
1004 | { | |
1005 | alarm(timeout); | |
1006 | ||
1007 | if (read(fd, &status, 1) < 1) | |
1008 | { | |
db1f069b MS |
1009 | _cupsLangPrintf(stderr, |
1010 | _("WARNING: Remote host did not respond with control " | |
4d301e69 | 1011 | "status byte after %d seconds\n"), timeout); |
ef416fc2 | 1012 | status = errno; |
1013 | } | |
1014 | ||
1015 | alarm(0); | |
1016 | } | |
1017 | ||
1018 | if (status != 0) | |
db1f069b MS |
1019 | _cupsLangPrintf(stderr, |
1020 | _("ERROR: Remote host did not accept control file " | |
1021 | "(%d)\n"), status); | |
ef416fc2 | 1022 | else |
db1f069b | 1023 | _cupsLangPuts(stderr, _("INFO: Control file sent successfully\n")); |
ef416fc2 | 1024 | } |
1025 | else | |
1026 | status = 0; | |
1027 | ||
1028 | if (status == 0) | |
1029 | { | |
7a14d768 MS |
1030 | /* |
1031 | * Check for side-channel requests... | |
1032 | */ | |
1033 | ||
1034 | backendCheckSideChannel(snmp_fd, &(addr->addr)); | |
1035 | ||
ef416fc2 | 1036 | /* |
1037 | * Send the print file... | |
1038 | */ | |
1039 | ||
1040 | if (lpd_command(fd, timeout, "\003" CUPS_LLFMT " dfA%03.3d%.15s\n", | |
1041 | CUPS_LLCAST filestats.st_size, (int)getpid() % 1000, | |
1042 | localhost)) | |
1043 | { | |
1044 | httpAddrFreeList(addrlist); | |
1045 | close(fd); | |
1046 | ||
1047 | return (CUPS_BACKEND_FAILED); | |
1048 | } | |
1049 | ||
db1f069b | 1050 | _cupsLangPrintf(stderr, |
c0e1af83 | 1051 | #ifdef HAVE_LONG_LONG |
db1f069b | 1052 | _("INFO: Sending data file (%lld bytes)\n"), |
c0e1af83 | 1053 | #else |
db1f069b | 1054 | _("INFO: Sending data file (%ld bytes)\n"), |
c0e1af83 | 1055 | #endif /* HAVE_LONG_LONG */ |
db1f069b | 1056 | CUPS_LLCAST filestats.st_size); |
ef416fc2 | 1057 | |
1058 | tbytes = 0; | |
1059 | for (copy = 0; copy < manual_copies; copy ++) | |
1060 | { | |
f42414bf | 1061 | lseek(print_fd, 0, SEEK_SET); |
ef416fc2 | 1062 | |
f42414bf | 1063 | while ((nbytes = read(print_fd, buffer, sizeof(buffer))) > 0) |
ef416fc2 | 1064 | { |
db1f069b MS |
1065 | _cupsLangPrintf(stderr, |
1066 | _("INFO: Spooling LPR job, %.0f%% complete...\n"), | |
1067 | 100.0 * tbytes / filestats.st_size); | |
ef416fc2 | 1068 | |
1069 | if (lpd_write(fd, buffer, nbytes) < nbytes) | |
1070 | { | |
080811b1 | 1071 | _cupsLangPrintError(_("ERROR: Unable to send print file to printer")); |
ef416fc2 | 1072 | break; |
1073 | } | |
1074 | else | |
1075 | tbytes += nbytes; | |
1076 | } | |
1077 | } | |
1078 | ||
f42414bf | 1079 | if (mode == MODE_STANDARD) |
ef416fc2 | 1080 | { |
f42414bf | 1081 | if (tbytes < filestats.st_size) |
1082 | status = errno; | |
1083 | else if (lpd_write(fd, "", 1) < 1) | |
1084 | { | |
080811b1 | 1085 | _cupsLangPrintError(_("ERROR: Unable to send trailing nul to printer")); |
f42414bf | 1086 | status = errno; |
1087 | } | |
1088 | else | |
1089 | { | |
1090 | /* | |
1091 | * Read the status byte from the printer; if we can't read the byte | |
1092 | * back now, we should set status to "errno", however at this point | |
1093 | * we know the printer got the whole file and we don't necessarily | |
1094 | * want to requeue it over and over... | |
1095 | */ | |
ef416fc2 | 1096 | |
f42414bf | 1097 | alarm(timeout); |
ef416fc2 | 1098 | |
f42414bf | 1099 | if (recv(fd, &status, 1, 0) < 1) |
1100 | { | |
db1f069b MS |
1101 | _cupsLangPrintf(stderr, |
1102 | _("WARNING: Remote host did not respond with data " | |
4d301e69 | 1103 | "status byte after %d seconds\n"), timeout); |
f42414bf | 1104 | status = 0; |
1105 | } | |
ef416fc2 | 1106 | |
f42414bf | 1107 | alarm(0); |
1108 | } | |
ef416fc2 | 1109 | } |
f42414bf | 1110 | else |
1111 | status = 0; | |
ef416fc2 | 1112 | |
1113 | if (status != 0) | |
db1f069b MS |
1114 | _cupsLangPrintf(stderr, |
1115 | _("ERROR: Remote host did not accept data file (%d)\n"), | |
1116 | status); | |
ef416fc2 | 1117 | else |
db1f069b | 1118 | _cupsLangPuts(stderr, _("INFO: Data file sent successfully\n")); |
ef416fc2 | 1119 | } |
1120 | ||
1121 | if (status == 0 && order == ORDER_DATA_CONTROL) | |
1122 | { | |
7a14d768 MS |
1123 | /* |
1124 | * Check for side-channel requests... | |
1125 | */ | |
1126 | ||
1127 | backendCheckSideChannel(snmp_fd, &(addr->addr)); | |
1128 | ||
1129 | /* | |
1130 | * Send control file... | |
1131 | */ | |
1132 | ||
ef416fc2 | 1133 | if (lpd_command(fd, timeout, "\002%d cfA%03.3d%.15s\n", strlen(control), |
1134 | (int)getpid() % 1000, localhost)) | |
1135 | { | |
1136 | httpAddrFreeList(addrlist); | |
1137 | close(fd); | |
1138 | ||
1139 | return (CUPS_BACKEND_FAILED); | |
1140 | } | |
1141 | ||
db1f069b MS |
1142 | _cupsLangPrintf(stderr, _("INFO: Sending control file (%lu bytes)\n"), |
1143 | (unsigned long)strlen(control)); | |
ef416fc2 | 1144 | |
1145 | if (lpd_write(fd, control, strlen(control) + 1) < (strlen(control) + 1)) | |
1146 | { | |
1147 | status = errno; | |
080811b1 | 1148 | _cupsLangPrintError(_("ERROR: Unable to write control file")); |
ef416fc2 | 1149 | } |
1150 | else | |
1151 | { | |
1152 | alarm(timeout); | |
1153 | ||
1154 | if (read(fd, &status, 1) < 1) | |
1155 | { | |
db1f069b MS |
1156 | _cupsLangPrintf(stderr, |
1157 | _("WARNING: Remote host did not respond with control " | |
4d301e69 | 1158 | "status byte after %d seconds\n"), timeout); |
ef416fc2 | 1159 | status = errno; |
1160 | } | |
1161 | ||
1162 | alarm(0); | |
1163 | } | |
1164 | ||
1165 | if (status != 0) | |
db1f069b MS |
1166 | _cupsLangPrintf(stderr, |
1167 | _("ERROR: Remote host did not accept control file " | |
1168 | "(%d)\n"), status); | |
ef416fc2 | 1169 | else |
db1f069b | 1170 | _cupsLangPuts(stderr, _("INFO: Control file sent successfully\n")); |
ef416fc2 | 1171 | } |
1172 | ||
7a14d768 | 1173 | /* |
52f6f666 | 1174 | * Collect the final supply levels as needed... |
7a14d768 MS |
1175 | */ |
1176 | ||
426c6a59 | 1177 | if (have_supplies) |
52f6f666 | 1178 | backendSNMPSupplies(snmp_fd, &(addr->addr), NULL, NULL); |
7a14d768 | 1179 | |
ef416fc2 | 1180 | /* |
1181 | * Close the socket connection and input file... | |
1182 | */ | |
1183 | ||
1184 | close(fd); | |
ef416fc2 | 1185 | |
1186 | if (status == 0) | |
1187 | { | |
1188 | httpAddrFreeList(addrlist); | |
1189 | ||
1190 | return (CUPS_BACKEND_OK); | |
1191 | } | |
1192 | ||
1193 | /* | |
1194 | * Waiting for a retry... | |
1195 | */ | |
1196 | ||
1197 | sleep(30); | |
1198 | } | |
1199 | ||
1200 | httpAddrFreeList(addrlist); | |
1201 | ||
1202 | /* | |
1203 | * If we get here, then the job has been cancelled... | |
1204 | */ | |
1205 | ||
1206 | return (CUPS_BACKEND_FAILED); | |
1207 | } | |
1208 | ||
1209 | ||
1210 | /* | |
1211 | * 'lpd_timeout()' - Handle timeout alarms... | |
1212 | */ | |
1213 | ||
1214 | static void | |
1215 | lpd_timeout(int sig) /* I - Signal number */ | |
1216 | { | |
1217 | (void)sig; | |
1218 | ||
1219 | #if !defined(HAVE_SIGSET) && !defined(HAVE_SIGACTION) | |
1220 | signal(SIGALRM, lpd_timeout); | |
1221 | #endif /* !HAVE_SIGSET && !HAVE_SIGACTION */ | |
1222 | } | |
1223 | ||
1224 | ||
1225 | /* | |
1226 | * 'lpd_write()' - Write a buffer of data to an LPD server. | |
1227 | */ | |
1228 | ||
1229 | static int /* O - Number of bytes written or -1 on error */ | |
1230 | lpd_write(int lpd_fd, /* I - LPD socket */ | |
1231 | char *buffer, /* I - Buffer to write */ | |
1232 | int length) /* I - Number of bytes to write */ | |
1233 | { | |
1234 | int bytes, /* Number of bytes written */ | |
1235 | total; /* Total number of bytes written */ | |
1236 | ||
1237 | ||
1238 | if (abort_job) | |
1239 | return (-1); | |
1240 | ||
1241 | total = 0; | |
1242 | while ((bytes = send(lpd_fd, buffer, length - total, 0)) >= 0) | |
1243 | { | |
1244 | total += bytes; | |
1245 | buffer += bytes; | |
1246 | ||
1247 | if (total == length) | |
1248 | break; | |
1249 | } | |
1250 | ||
1251 | if (bytes < 0) | |
1252 | return (-1); | |
1253 | else | |
1254 | return (length); | |
1255 | } | |
1256 | ||
1257 | ||
1258 | #ifndef HAVE_RRESVPORT_AF | |
1259 | /* | |
1260 | * 'rresvport_af()' - A simple implementation of rresvport_af(). | |
1261 | */ | |
1262 | ||
1263 | static int /* O - Socket or -1 on error */ | |
1264 | rresvport_af(int *port, /* IO - Port number to bind to */ | |
1265 | int family) /* I - Address family */ | |
1266 | { | |
1267 | http_addr_t addr; /* Socket address */ | |
1268 | int fd; /* Socket file descriptor */ | |
1269 | ||
1270 | ||
1271 | /* | |
1272 | * Try to create an IPv4 socket... | |
1273 | */ | |
1274 | ||
1275 | if ((fd = socket(family, SOCK_STREAM, 0)) < 0) | |
1276 | return (-1); | |
1277 | ||
1278 | /* | |
1279 | * Initialize the address buffer... | |
1280 | */ | |
1281 | ||
1282 | memset(&addr, 0, sizeof(addr)); | |
1283 | addr.addr.sa_family = family; | |
1284 | ||
1285 | /* | |
1286 | * Try to bind the socket to a reserved port... | |
1287 | */ | |
1288 | ||
1289 | while (*port > 511) | |
1290 | { | |
1291 | /* | |
1292 | * Set the port number... | |
1293 | */ | |
1294 | ||
1295 | # ifdef AF_INET6 | |
1296 | if (family == AF_INET6) | |
1297 | addr.ipv6.sin6_port = htons(*port); | |
1298 | else | |
1299 | # endif /* AF_INET6 */ | |
1300 | addr.ipv4.sin_port = htons(*port); | |
1301 | ||
1302 | /* | |
1303 | * Try binding the port to the socket; return if all is OK... | |
1304 | */ | |
1305 | ||
1306 | if (!bind(fd, (struct sockaddr *)&addr, sizeof(addr))) | |
1307 | return (fd); | |
1308 | ||
1309 | /* | |
1310 | * Stop if we have any error other than "address already in use"... | |
1311 | */ | |
1312 | ||
1313 | if (errno != EADDRINUSE) | |
1314 | { | |
1315 | # ifdef WIN32 | |
1316 | closesocket(fd); | |
1317 | # else | |
1318 | close(fd); | |
1319 | # endif /* WIN32 */ | |
1320 | ||
1321 | return (-1); | |
1322 | } | |
1323 | ||
1324 | /* | |
1325 | * Try the next port... | |
1326 | */ | |
1327 | ||
1328 | (*port)--; | |
1329 | } | |
1330 | ||
1331 | /* | |
1332 | * Wasn't able to bind to a reserved port, so close the socket and return | |
1333 | * -1... | |
1334 | */ | |
1335 | ||
1336 | # ifdef WIN32 | |
1337 | closesocket(fd); | |
1338 | # else | |
1339 | close(fd); | |
1340 | # endif /* WIN32 */ | |
1341 | ||
1342 | return (-1); | |
1343 | } | |
1344 | #endif /* !HAVE_RRESVPORT_AF */ | |
1345 | ||
1346 | ||
1347 | /* | |
1348 | * 'sigterm_handler()' - Handle 'terminate' signals that stop the backend. | |
1349 | */ | |
1350 | ||
1351 | static void | |
1352 | sigterm_handler(int sig) /* I - Signal */ | |
1353 | { | |
1354 | (void)sig; /* remove compiler warnings... */ | |
1355 | ||
1356 | abort_job = 1; | |
1357 | } | |
1358 | ||
1359 | ||
1360 | /* | |
b19ccc9e | 1361 | * End of "$Id: lpd.c 7740 2008-07-14 23:58:05Z mike $". |
ef416fc2 | 1362 | */ |