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