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