]>
Commit | Line | Data |
---|---|---|
ef416fc2 | 1 | /* |
f2d18633 | 2 | * "$Id$" |
ef416fc2 | 3 | * |
581dae2d | 4 | * AppSocket backend for CUPS. |
ef416fc2 | 5 | * |
7e86f2f6 | 6 | * Copyright 2007-2014 by Apple Inc. |
581dae2d | 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> |
ed486911 | 23 | #include "backend-private.h" |
ef416fc2 | 24 | #include <stdarg.h> |
ef416fc2 | 25 | #include <sys/types.h> |
26 | #include <sys/stat.h> | |
ef416fc2 | 27 | |
28 | #ifdef WIN32 | |
29 | # include <winsock.h> | |
30 | #else | |
31 | # include <unistd.h> | |
32 | # include <fcntl.h> | |
33 | # include <sys/socket.h> | |
34 | # include <netinet/in.h> | |
35 | # include <arpa/inet.h> | |
36 | # include <netdb.h> | |
37 | #endif /* WIN32 */ | |
38 | ||
39 | ||
f7deaa1a | 40 | /* |
41 | * Local functions... | |
42 | */ | |
43 | ||
7e86f2f6 | 44 | static ssize_t wait_bc(int device_fd, int secs); |
f7deaa1a | 45 | |
46 | ||
ef416fc2 | 47 | /* |
48 | * 'main()' - Send a file to the printer or server. | |
49 | * | |
50 | * Usage: | |
51 | * | |
52 | * printer-uri job-id user title copies options [file] | |
53 | */ | |
54 | ||
55 | int /* O - Exit status */ | |
56 | main(int argc, /* I - Number of command-line arguments (6 or 7) */ | |
57 | char *argv[]) /* I - Command-line arguments */ | |
58 | { | |
1f0275e3 | 59 | const char *device_uri; /* Device URI */ |
acb056cb | 60 | char scheme[255], /* Scheme in URI */ |
ef416fc2 | 61 | hostname[1024], /* Hostname */ |
62 | username[255], /* Username info (not used) */ | |
fa73b229 | 63 | resource[1024], /* Resource info (not used) */ |
64 | *options, /* Pointer to options */ | |
db1f069b MS |
65 | *name, /* Name of option */ |
66 | *value, /* Value of option */ | |
67 | sep; /* Option separator */ | |
ed486911 | 68 | int print_fd; /* Print file */ |
ef416fc2 | 69 | int copies; /* Number of copies to print */ |
f8b3a85b | 70 | time_t start_time; /* Time of first connect */ |
c0e1af83 | 71 | int contimeout; /* Connection timeout */ |
fa73b229 | 72 | int waiteof; /* Wait for end-of-file? */ |
ef416fc2 | 73 | int port; /* Port number */ |
74 | char portname[255]; /* Port name */ | |
75 | int delay; /* Delay for retries... */ | |
ed486911 | 76 | int device_fd; /* AppSocket */ |
ef416fc2 | 77 | int error; /* Error code (if any) */ |
26d47ec6 | 78 | http_addrlist_t *addrlist, /* Address list */ |
db1f069b | 79 | *addr; /* Connected address */ |
26d47ec6 | 80 | char addrname[256]; /* Address name */ |
5a9febac | 81 | int snmp_enabled = 1; /* Is SNMP enabled? */ |
568fa3fa MS |
82 | int snmp_fd, /* SNMP socket */ |
83 | start_count, /* Page count via SNMP at start */ | |
426c6a59 MS |
84 | page_count, /* Page count via SNMP */ |
85 | have_supplies; /* Printer supports supply levels? */ | |
eac3a0a0 MS |
86 | ssize_t bytes = 0, /* Initial bytes read */ |
87 | tbytes; /* Total number of bytes written */ | |
88 | char buffer[1024]; /* Initial print buffer */ | |
ef416fc2 | 89 | #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET) |
90 | struct sigaction action; /* Actions for POSIX signals */ | |
91 | #endif /* HAVE_SIGACTION && !HAVE_SIGSET */ | |
92 | ||
93 | ||
94 | /* | |
95 | * Make sure status messages are not buffered... | |
96 | */ | |
97 | ||
98 | setbuf(stderr, NULL); | |
99 | ||
100 | /* | |
101 | * Ignore SIGPIPE signals... | |
102 | */ | |
103 | ||
104 | #ifdef HAVE_SIGSET | |
105 | sigset(SIGPIPE, SIG_IGN); | |
106 | #elif defined(HAVE_SIGACTION) | |
107 | memset(&action, 0, sizeof(action)); | |
108 | action.sa_handler = SIG_IGN; | |
109 | sigaction(SIGPIPE, &action, NULL); | |
110 | #else | |
111 | signal(SIGPIPE, SIG_IGN); | |
112 | #endif /* HAVE_SIGSET */ | |
113 | ||
114 | /* | |
115 | * Check command-line... | |
116 | */ | |
117 | ||
118 | if (argc == 1) | |
119 | { | |
8b450588 MS |
120 | printf("network socket \"Unknown\" \"%s\"\n", |
121 | _cupsLangString(cupsLangDefault(), _("AppSocket/HP JetDirect"))); | |
ef416fc2 | 122 | return (CUPS_BACKEND_OK); |
123 | } | |
124 | else if (argc < 6 || argc > 7) | |
125 | { | |
db1f069b | 126 | _cupsLangPrintf(stderr, |
0837b7e8 | 127 | _("Usage: %s job-id user title copies options [file]"), |
db1f069b | 128 | argv[0]); |
ef416fc2 | 129 | return (CUPS_BACKEND_FAILED); |
130 | } | |
131 | ||
132 | /* | |
133 | * If we have 7 arguments, print the file named on the command-line. | |
134 | * Otherwise, send stdin instead... | |
135 | */ | |
136 | ||
137 | if (argc == 6) | |
138 | { | |
ed486911 | 139 | print_fd = 0; |
140 | copies = 1; | |
ef416fc2 | 141 | } |
142 | else | |
143 | { | |
144 | /* | |
145 | * Try to open the print file... | |
146 | */ | |
147 | ||
ed486911 | 148 | if ((print_fd = open(argv[6], O_RDONLY)) < 0) |
ef416fc2 | 149 | { |
0837b7e8 | 150 | _cupsLangPrintError("ERROR", _("Unable to open print file")); |
ef416fc2 | 151 | return (CUPS_BACKEND_FAILED); |
152 | } | |
153 | ||
154 | copies = atoi(argv[4]); | |
155 | } | |
156 | ||
157 | /* | |
158 | * Extract the hostname and port number from the URI... | |
159 | */ | |
160 | ||
0268488e MS |
161 | while ((device_uri = cupsBackendDeviceURI(argv)) == NULL) |
162 | { | |
163 | _cupsLangPrintFilter(stderr, "INFO", _("Unable to locate printer.")); | |
164 | sleep(10); | |
165 | ||
166 | if (getenv("CLASS") != NULL) | |
167 | return (CUPS_BACKEND_FAILED); | |
168 | } | |
1f0275e3 | 169 | |
acb056cb MS |
170 | httpSeparateURI(HTTP_URI_CODING_ALL, device_uri, scheme, sizeof(scheme), |
171 | username, sizeof(username), hostname, sizeof(hostname), &port, | |
ef416fc2 | 172 | resource, sizeof(resource)); |
173 | ||
174 | if (port == 0) | |
175 | port = 9100; /* Default to HP JetDirect/Tektronix PhaserShare */ | |
176 | ||
fa73b229 | 177 | /* |
178 | * Get options, if any... | |
179 | */ | |
180 | ||
c0e1af83 | 181 | waiteof = 1; |
182 | contimeout = 7 * 24 * 60 * 60; | |
fa73b229 | 183 | |
184 | if ((options = strchr(resource, '?')) != NULL) | |
185 | { | |
186 | /* | |
187 | * Yup, terminate the device name string and move to the first | |
188 | * character of the options... | |
189 | */ | |
190 | ||
191 | *options++ = '\0'; | |
192 | ||
193 | /* | |
194 | * Parse options... | |
195 | */ | |
196 | ||
197 | while (*options) | |
198 | { | |
199 | /* | |
200 | * Get the name... | |
201 | */ | |
202 | ||
db1f069b | 203 | name = options; |
fa73b229 | 204 | |
db1f069b MS |
205 | while (*options && *options != '=' && *options != '+' && *options != '&') |
206 | options ++; | |
207 | ||
208 | if ((sep = *options) != '\0') | |
209 | *options++ = '\0'; | |
210 | ||
211 | if (sep == '=') | |
fa73b229 | 212 | { |
213 | /* | |
214 | * Get the value... | |
215 | */ | |
216 | ||
db1f069b | 217 | value = options; |
fa73b229 | 218 | |
db1f069b | 219 | while (*options && *options != '+' && *options != '&') |
fa73b229 | 220 | options ++; |
db1f069b MS |
221 | |
222 | if (*options) | |
223 | *options++ = '\0'; | |
fa73b229 | 224 | } |
225 | else | |
db1f069b | 226 | value = (char *)""; |
fa73b229 | 227 | |
228 | /* | |
229 | * Process the option... | |
230 | */ | |
231 | ||
88f9aafc | 232 | if (!_cups_strcasecmp(name, "waiteof")) |
fa73b229 | 233 | { |
234 | /* | |
235 | * Set the wait-for-eof value... | |
236 | */ | |
237 | ||
88f9aafc MS |
238 | waiteof = !value[0] || !_cups_strcasecmp(value, "on") || |
239 | !_cups_strcasecmp(value, "yes") || !_cups_strcasecmp(value, "true"); | |
fa73b229 | 240 | } |
5a9febac MS |
241 | else if (!_cups_strcasecmp(name, "snmp")) |
242 | { | |
243 | /* | |
244 | * Enable/disable SNMP stuff... | |
245 | */ | |
246 | ||
247 | snmp_enabled = !value[0] || !_cups_strcasecmp(value, "on") || | |
248 | _cups_strcasecmp(value, "yes") || | |
249 | _cups_strcasecmp(value, "true"); | |
250 | } | |
88f9aafc | 251 | else if (!_cups_strcasecmp(name, "contimeout")) |
c0e1af83 | 252 | { |
253 | /* | |
254 | * Set the connection timeout... | |
255 | */ | |
256 | ||
257 | if (atoi(value) > 0) | |
258 | contimeout = atoi(value); | |
259 | } | |
fa73b229 | 260 | } |
261 | } | |
262 | ||
ef416fc2 | 263 | /* |
c8fef167 | 264 | * Then try finding the remote host... |
ef416fc2 | 265 | */ |
266 | ||
4d301e69 | 267 | start_time = time(NULL); |
c0e1af83 | 268 | |
ef416fc2 | 269 | sprintf(portname, "%d", port); |
270 | ||
acb056cb | 271 | fputs("STATE: +connecting-to-device\n", stderr); |
1340db2d MS |
272 | fprintf(stderr, "DEBUG: Looking up \"%s\"...\n", hostname); |
273 | ||
0268488e | 274 | while ((addrlist = httpAddrGetList(hostname, AF_UNSPEC, portname)) == NULL) |
ef416fc2 | 275 | { |
0268488e | 276 | _cupsLangPrintFilter(stderr, "INFO", |
0837b7e8 | 277 | _("Unable to locate printer \"%s\"."), hostname); |
0268488e MS |
278 | sleep(10); |
279 | ||
280 | if (getenv("CLASS") != NULL) | |
c8fef167 MS |
281 | { |
282 | fputs("STATE: -connecting-to-device\n", stderr); | |
0268488e | 283 | return (CUPS_BACKEND_STOP); |
c8fef167 MS |
284 | } |
285 | } | |
286 | ||
287 | /* | |
288 | * See if the printer supports SNMP... | |
289 | */ | |
290 | ||
5a9febac MS |
291 | if (snmp_enabled) |
292 | snmp_fd = _cupsSNMPOpen(addrlist->addr.addr.sa_family); | |
293 | else | |
294 | snmp_fd = -1; | |
295 | ||
296 | if (snmp_fd >= 0) | |
f14324a7 MS |
297 | have_supplies = !backendSNMPSupplies(snmp_fd, &(addrlist->addr), |
298 | &start_count, NULL); | |
c8fef167 MS |
299 | else |
300 | have_supplies = start_count = 0; | |
301 | ||
302 | /* | |
303 | * Wait for data from the filter... | |
304 | */ | |
305 | ||
306 | if (print_fd == 0) | |
eac3a0a0 | 307 | { |
f14324a7 | 308 | if (!backendWaitLoop(snmp_fd, &(addrlist->addr), 1, backendNetworkSideCB)) |
c8fef167 | 309 | return (CUPS_BACKEND_OK); |
eac3a0a0 MS |
310 | else if ((bytes = read(0, buffer, sizeof(buffer))) <= 0) |
311 | return (CUPS_BACKEND_OK); | |
312 | } | |
c8fef167 MS |
313 | |
314 | /* | |
315 | * Connect to the printer... | |
316 | */ | |
ef416fc2 | 317 | |
acb056cb | 318 | fprintf(stderr, "DEBUG: Connecting to %s:%d\n", hostname, port); |
0837b7e8 | 319 | _cupsLangPrintFilter(stderr, "INFO", _("Connecting to printer.")); |
ef416fc2 | 320 | |
ed486911 | 321 | for (delay = 5;;) |
ef416fc2 | 322 | { |
26d47ec6 | 323 | if ((addr = httpAddrConnect(addrlist, &device_fd)) == NULL) |
ef416fc2 | 324 | { |
ed486911 | 325 | error = errno; |
326 | device_fd = -1; | |
ef416fc2 | 327 | |
ed486911 | 328 | if (getenv("CLASS") != NULL) |
329 | { | |
330 | /* | |
331 | * If the CLASS environment variable is set, the job was submitted | |
332 | * to a class and not to a specific queue. In this case, we want | |
333 | * to abort immediately so that the job can be requeued on the next | |
334 | * available printer in the class. | |
335 | */ | |
ef416fc2 | 336 | |
0837b7e8 MS |
337 | _cupsLangPrintFilter(stderr, "INFO", |
338 | _("Unable to contact printer, queuing on next " | |
339 | "printer in class.")); | |
ef416fc2 | 340 | |
ed486911 | 341 | /* |
342 | * Sleep 5 seconds to keep the job from requeuing too rapidly... | |
343 | */ | |
ef416fc2 | 344 | |
ed486911 | 345 | sleep(5); |
ef416fc2 | 346 | |
ed486911 | 347 | return (CUPS_BACKEND_FAILED); |
348 | } | |
ef416fc2 | 349 | |
c7017ecc MS |
350 | fprintf(stderr, "DEBUG: Connection error: %s\n", strerror(error)); |
351 | ||
ed486911 | 352 | if (error == ECONNREFUSED || error == EHOSTDOWN || |
353 | error == EHOSTUNREACH) | |
354 | { | |
c0e1af83 | 355 | if (contimeout && (time(NULL) - start_time) > contimeout) |
356 | { | |
0837b7e8 MS |
357 | _cupsLangPrintFilter(stderr, "ERROR", |
358 | _("The printer is not responding.")); | |
c0e1af83 | 359 | return (CUPS_BACKEND_FAILED); |
360 | } | |
361 | ||
c7017ecc MS |
362 | switch (error) |
363 | { | |
364 | case EHOSTDOWN : | |
0837b7e8 | 365 | _cupsLangPrintFilter(stderr, "WARNING", |
22c9029b MS |
366 | _("The printer may not exist or " |
367 | "is unavailable at this time.")); | |
c7017ecc MS |
368 | break; |
369 | ||
370 | case EHOSTUNREACH : | |
0837b7e8 | 371 | _cupsLangPrintFilter(stderr, "WARNING", |
22c9029b MS |
372 | _("The printer is unreachable at this " |
373 | "time.")); | |
c7017ecc MS |
374 | break; |
375 | ||
376 | case ECONNREFUSED : | |
377 | default : | |
0837b7e8 | 378 | _cupsLangPrintFilter(stderr, "WARNING", |
f3c17241 | 379 | _("The printer is in use.")); |
c7017ecc MS |
380 | break; |
381 | } | |
c0e1af83 | 382 | |
7e86f2f6 | 383 | sleep((unsigned)delay); |
ef416fc2 | 384 | |
ed486911 | 385 | if (delay < 30) |
386 | delay += 5; | |
ef416fc2 | 387 | } |
388 | else | |
ed486911 | 389 | { |
0837b7e8 | 390 | _cupsLangPrintFilter(stderr, "ERROR", |
22c9029b | 391 | _("The printer is not responding.")); |
ed486911 | 392 | sleep(30); |
393 | } | |
ef416fc2 | 394 | } |
ed486911 | 395 | else |
396 | break; | |
397 | } | |
ef416fc2 | 398 | |
ed486911 | 399 | fputs("STATE: -connecting-to-device\n", stderr); |
0837b7e8 | 400 | _cupsLangPrintFilter(stderr, "INFO", _("Connected to printer.")); |
26d47ec6 | 401 | |
22c9029b MS |
402 | fprintf(stderr, "DEBUG: Connected to %s:%d...\n", |
403 | httpAddrString(&(addr->addr), addrname, sizeof(addrname)), | |
a469f8a5 | 404 | httpAddrPort(&(addr->addr))); |
ef416fc2 | 405 | |
ed486911 | 406 | /* |
407 | * Print everything... | |
408 | */ | |
ef416fc2 | 409 | |
ed486911 | 410 | tbytes = 0; |
ef416fc2 | 411 | |
eac3a0a0 | 412 | if (bytes > 0) |
7e86f2f6 | 413 | tbytes += write(device_fd, buffer, (size_t)bytes); |
eac3a0a0 | 414 | |
ed486911 | 415 | while (copies > 0 && tbytes >= 0) |
416 | { | |
ef416fc2 | 417 | copies --; |
418 | ||
ed486911 | 419 | if (print_fd != 0) |
ef416fc2 | 420 | { |
421 | fputs("PAGE: 1 1\n", stderr); | |
ed486911 | 422 | lseek(print_fd, 0, SEEK_SET); |
ef416fc2 | 423 | } |
424 | ||
f14324a7 MS |
425 | tbytes = backendRunLoop(print_fd, device_fd, snmp_fd, &(addrlist->addr), 1, |
426 | 0, backendNetworkSideCB); | |
ef416fc2 | 427 | |
ed486911 | 428 | if (print_fd != 0 && tbytes >= 0) |
0837b7e8 | 429 | _cupsLangPrintFilter(stderr, "INFO", _("Print file sent.")); |
ed486911 | 430 | } |
ef416fc2 | 431 | |
1e3e80bb | 432 | fputs("STATE: +cups-waiting-for-job-completed\n", stderr); |
581dae2d | 433 | |
ed486911 | 434 | if (waiteof) |
435 | { | |
436 | /* | |
437 | * Shutdown the socket and wait for the other end to finish... | |
438 | */ | |
ef416fc2 | 439 | |
0837b7e8 | 440 | _cupsLangPrintFilter(stderr, "INFO", _("Waiting for printer to finish.")); |
ef416fc2 | 441 | |
ed486911 | 442 | shutdown(device_fd, 1); |
ef416fc2 | 443 | |
323c5de1 | 444 | while (wait_bc(device_fd, 90) > 0); |
ed486911 | 445 | } |
ef416fc2 | 446 | |
568fa3fa MS |
447 | /* |
448 | * Collect the final page count as needed... | |
449 | */ | |
450 | ||
f14324a7 MS |
451 | if (have_supplies && |
452 | !backendSNMPSupplies(snmp_fd, &(addrlist->addr), &page_count, NULL) && | |
568fa3fa MS |
453 | page_count > start_count) |
454 | fprintf(stderr, "PAGE: total %d\n", page_count - start_count); | |
455 | ||
ed486911 | 456 | /* |
457 | * Close the socket connection... | |
458 | */ | |
ef416fc2 | 459 | |
ed486911 | 460 | close(device_fd); |
ef416fc2 | 461 | |
462 | httpAddrFreeList(addrlist); | |
463 | ||
464 | /* | |
465 | * Close the input file and return... | |
466 | */ | |
467 | ||
ed486911 | 468 | if (print_fd != 0) |
469 | close(print_fd); | |
ef416fc2 | 470 | |
c8fef167 | 471 | return (CUPS_BACKEND_OK); |
ef416fc2 | 472 | } |
473 | ||
474 | ||
f7deaa1a | 475 | /* |
323c5de1 | 476 | * 'wait_bc()' - Wait for back-channel data... |
477 | */ | |
478 | ||
7e86f2f6 | 479 | static ssize_t /* O - # bytes read or -1 on error */ |
323c5de1 | 480 | wait_bc(int device_fd, /* I - Socket */ |
481 | int secs) /* I - Seconds to wait */ | |
482 | { | |
483 | struct timeval timeout; /* Timeout for select() */ | |
484 | fd_set input; /* Input set for select() */ | |
485 | ssize_t bytes; /* Number of back-channel bytes read */ | |
486 | char buffer[1024]; /* Back-channel buffer */ | |
487 | ||
488 | ||
489 | /* | |
490 | * Wait up to "secs" seconds for backchannel data... | |
491 | */ | |
492 | ||
493 | timeout.tv_sec = secs; | |
494 | timeout.tv_usec = 0; | |
495 | ||
496 | FD_ZERO(&input); | |
497 | FD_SET(device_fd, &input); | |
498 | ||
499 | if (select(device_fd + 1, &input, NULL, NULL, &timeout) > 0) | |
500 | { | |
501 | /* | |
502 | * Grab the data coming back and spit it out to stderr... | |
503 | */ | |
504 | ||
505 | if ((bytes = read(device_fd, buffer, sizeof(buffer))) > 0) | |
506 | { | |
4d301e69 | 507 | fprintf(stderr, "DEBUG: Received %d bytes of back-channel data\n", |
323c5de1 | 508 | (int)bytes); |
7e86f2f6 | 509 | cupsBackChannelWrite(buffer, (size_t)bytes, 1.0); |
323c5de1 | 510 | } |
511 | ||
512 | return (bytes); | |
513 | } | |
514 | else | |
515 | return (-1); | |
516 | } | |
517 | ||
518 | ||
519 | /* | |
f2d18633 | 520 | * End of "$Id$". |
ef416fc2 | 521 | */ |