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