]> git.ipfire.org Git - thirdparty/cups.git/blame - backend/usb-darwin.c
Remove unnecessary logging.
[thirdparty/cups.git] / backend / usb-darwin.c
CommitLineData
ef416fc2 1/*
68a36223 2 * Copyright 2005-2016 Apple Inc. All rights reserved.
581dae2d
MS
3 *
4 * IMPORTANT: This Apple software is supplied to you by Apple Computer,
5 * Inc. ("Apple") in consideration of your agreement to the following
6 * terms, and your use, installation, modification or redistribution of
7 * this Apple software constitutes acceptance of these terms. If you do
8 * not agree with these terms, please do not use, install, modify or
9 * redistribute this Apple software.
10 *
11 * In consideration of your agreement to abide by the following terms, and
12 * subject to these terms, Apple grants you a personal, non-exclusive
13 * license, under Apple's copyrights in this original Apple software (the
14 * "Apple Software"), to use, reproduce, modify and redistribute the Apple
15 * Software, with or without modifications, in source and/or binary forms;
16 * provided that if you redistribute the Apple Software in its entirety and
17 * without modifications, you must retain this notice and the following
18 * text and disclaimers in all such redistributions of the Apple Software.
19 * Neither the name, trademarks, service marks or logos of Apple Computer,
20 * Inc. may be used to endorse or promote products derived from the Apple
21 * Software without specific prior written permission from Apple. Except
22 * as expressly stated in this notice, no other rights or licenses, express
23 * or implied, are granted by Apple herein, including but not limited to
24 * any patent rights that may be infringed by your derivative works or by
25 * other works in which the Apple Software may be incorporated.
26 *
27 * The Apple Software is provided by Apple on an "AS IS" basis. APPLE
28 * MAKES NO WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION
29 * THE IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS
30 * FOR A PARTICULAR PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND
31 * OPERATION ALONE OR IN COMBINATION WITH YOUR PRODUCTS.
32 *
33 * IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL
34 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
35 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
36 * INTERRUPTION) ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION,
37 * MODIFICATION AND/OR DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED
38 * AND WHETHER UNDER THEORY OF CONTRACT, TORT (INCLUDING NEGLIGENCE),
39 * STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN ADVISED OF THE
40 * POSSIBILITY OF SUCH DAMAGE.
41 */
ef416fc2 42
43/*
09a101d6 44 * Include necessary headers.
ef416fc2 45 */
09a101d6 46
ef416fc2 47#include <stdio.h>
48#include <stdlib.h>
49#include <errno.h>
50#include <signal.h>
51#include <fcntl.h>
52#include <termios.h>
53#include <unistd.h>
ac884b6a 54#include <sys/stat.h>
e53920b9 55#include <sys/sysctl.h>
56#include <libgen.h>
09a101d6 57#include <mach/mach.h>
e53920b9 58#include <mach/mach_error.h>
59#include <mach/mach_time.h>
71e16022 60#include <cups/debug-private.h>
22c9029b 61#include <cups/file-private.h>
f7deaa1a 62#include <cups/sidechannel.h>
71e16022 63#include <cups/language-private.h>
f787e1e3 64#include <cups/ppd-private.h>
db0bd74a 65#include "backend-private.h"
e53920b9 66#include <CoreFoundation/CoreFoundation.h>
67#include <IOKit/usb/IOUSBLib.h>
68#include <IOKit/IOCFPlugIn.h>
a469f8a5 69#include <libproc.h>
9fb65b89 70#include <asl.h>
749b1e90 71#include <spawn.h>
e53920b9 72#include <pthread.h>
ef416fc2 73
fe750b86
MS
74/*
75 * Include necessary headers.
76 */
77
749b1e90
MS
78extern char **environ;
79
f7deaa1a 80
acb056cb
MS
81/*
82 * DEBUG_WRITES, if defined, causes the backend to write data to the printer in
83 * 512 byte increments, up to 8192 bytes, to make debugging with a USB bus
84 * analyzer easier.
85 */
86
87#define DEBUG_WRITES 0
88
7cf5915e
MS
89/*
90 * WAIT_EOF_DELAY is number of seconds we'll wait for responses from
91 * the printer after we've finished sending all the data
92 */
93#define WAIT_EOF_DELAY 7
94#define WAIT_SIDE_DELAY 3
7dfedb92 95#define DEFAULT_TIMEOUT 5000L
ef416fc2 96
f1ebe30e 97#define USB_INTERFACE_KIND CFUUIDGetUUIDBytes(kIOUSBInterfaceInterfaceID245)
e53920b9 98#define kUSBLanguageEnglish 0x409
ef416fc2 99
09a101d6 100#define PRINTER_POLLING_INTERVAL 5 /* seconds */
101#define INITIAL_LOG_INTERVAL PRINTER_POLLING_INTERVAL
102#define SUBSEQUENT_LOG_INTERVAL 3 * INITIAL_LOG_INTERVAL
ef416fc2 103
09a101d6 104#define kUSBPrinterClassTypeID CFUUIDGetConstantUUIDWithBytes(NULL, 0x06, 0x04, 0x7D, 0x16, 0x53, 0xA2, 0x11, 0xD6, 0x92, 0x06, 0x00, 0x30, 0x65, 0x52, 0x45, 0x92)
105#define kUSBPrinterClassInterfaceID CFUUIDGetConstantUUIDWithBytes(NULL, 0x03, 0x34, 0x6D, 0x74, 0x53, 0xA3, 0x11, 0xD6, 0x9E, 0xA1, 0x76, 0x30, 0x65, 0x52, 0x45, 0x92)
ef416fc2 106
107#define kUSBClassDriverProperty CFSTR("USB Printing Class")
ef416fc2 108
84315f46 109#define kUSBGenericTOPrinterClassDriver CFSTR("/System/Library/Printers/Libraries/USBGenericPrintingClass.plugin")
e53920b9 110#define kUSBPrinterClassDeviceNotOpen -9664 /*kPMInvalidIOMContext*/
ef416fc2 111
a469f8a5
MS
112#define CRSetCrashLogMessage(m) _crc_make_setter(message, m)
113#define _crc_make_setter(attr, arg) (gCRAnnotations.attr = (uint64_t)(unsigned long)(arg))
114#define CRASH_REPORTER_CLIENT_HIDDEN __attribute__((visibility("hidden")))
115#define CRASHREPORTER_ANNOTATIONS_VERSION 4
116#define CRASHREPORTER_ANNOTATIONS_SECTION "__crash_info"
117
118struct crashreporter_annotations_t {
119 uint64_t version; // unsigned long
120 uint64_t message; // char *
121 uint64_t signature_string; // char *
122 uint64_t backtrace; // char *
123 uint64_t message2; // char *
124 uint64_t thread; // uint64_t
125 uint64_t dialog_mode; // unsigned int
126};
127
0fa6c7fa
MS
128CRASH_REPORTER_CLIENT_HIDDEN
129struct crashreporter_annotations_t gCRAnnotations
130 __attribute__((section("__DATA," CRASHREPORTER_ANNOTATIONS_SECTION)))
a469f8a5 131 = { CRASHREPORTER_ANNOTATIONS_VERSION, 0, 0, 0, 0, 0, 0 };
ef416fc2 132
e53920b9 133/*
134 * Section 5.3 USB Printing Class spec
135 */
136#define kUSBPrintingSubclass 1
137#define kUSBPrintingProtocolNoOpen 0
138#define kUSBPrintingProtocolUnidirectional 1
139#define kUSBPrintingProtocolBidirectional 2
fe750b86 140#define kUSBPrintingProtocolIPP 4
e53920b9 141
f1ebe30e 142typedef IOUSBInterfaceInterface245 **printer_interface_t;
ef416fc2 143
09a101d6 144typedef struct iodevice_request_s /**** Device request ****/
ef416fc2 145{
09a101d6 146 UInt8 requestType;
e53920b9 147 UInt8 request;
148 UInt16 value;
149 UInt16 index;
150 UInt16 length;
09a101d6 151 void *buffer;
e53920b9 152} iodevice_request_t;
153
09a101d6 154typedef union /**** Centronics status byte ****/
155{
e53920b9 156 char b;
09a101d6 157 struct
158 {
e53920b9 159 unsigned reserved0:2;
160 unsigned paperError:1;
161 unsigned select:1;
162 unsigned notError:1;
163 unsigned reserved1:3;
164 } status;
165} centronics_status_t;
166
09a101d6 167typedef struct classdriver_s /**** g.classdriver context ****/
ef416fc2 168{
169 IUNKNOWN_C_GUTS;
e53920b9 170 CFPlugInRef plugin; /* release plugin */
171 IUnknownVTbl **factory; /* Factory */
172 void *vendorReference; /* vendor class specific usage */
173 UInt32 location; /* unique location in bus topology */
174 UInt8 interfaceNumber; /* Interface number */
175 UInt16 vendorID; /* Vendor id */
176 UInt16 productID; /* Product id */
177 printer_interface_t interface; /* identify the device to IOKit */
a469f8a5 178 UInt8 outpipe; /* mandatory bulkOut pipe */
e53920b9 179 UInt8 inpipe; /* optional bulkIn pipe */
ef416fc2 180
e53920b9 181 /* general class requests */
09a101d6 182 kern_return_t (*DeviceRequest)(struct classdriver_s **printer, iodevice_request_t *iorequest, UInt16 timeout);
183 kern_return_t (*GetString)(struct classdriver_s **printer, UInt8 whichString, UInt16 language, UInt16 timeout, CFStringRef *result);
ef416fc2 184
e53920b9 185 /* standard printer class requests */
09a101d6 186 kern_return_t (*SoftReset)(struct classdriver_s **printer, UInt16 timeout);
187 kern_return_t (*GetCentronicsStatus)(struct classdriver_s **printer, centronics_status_t *result, UInt16 timeout);
188 kern_return_t (*GetDeviceID)(struct classdriver_s **printer, CFStringRef *devid, UInt16 timeout);
ef416fc2 189
e53920b9 190 /* standard bulk device requests */
09a101d6 191 kern_return_t (*ReadPipe)(struct classdriver_s **printer, UInt8 *buffer, UInt32 *count);
192 kern_return_t (*WritePipe)(struct classdriver_s **printer, UInt8 *buffer, UInt32 *count, Boolean eoj);
ef416fc2 193
e53920b9 194 /* interface requests */
09a101d6 195 kern_return_t (*Open)(struct classdriver_s **printer, UInt32 location, UInt8 protocol);
196 kern_return_t (*Abort)(struct classdriver_s **printer);
197 kern_return_t (*Close)(struct classdriver_s **printer);
ef416fc2 198
e53920b9 199 /* initialize and terminate */
09a101d6 200 kern_return_t (*Initialize)(struct classdriver_s **printer, struct classdriver_s **baseclass);
201 kern_return_t (*Terminate)(struct classdriver_s **printer);
ef416fc2 202
09a101d6 203} classdriver_t;
ef416fc2 204
68a36223 205typedef Boolean (*iterator_callback_t)(io_service_t obj, printer_interface_t printerIntf, void *refcon);
ef416fc2 206
09a101d6 207typedef struct iterator_reference_s /**** Iterator reference data */
208{
e53920b9 209 iterator_callback_t callback;
210 void *userdata;
211 Boolean keepRunning;
212} iterator_reference_t;
ef416fc2 213
09a101d6 214typedef struct globals_s
215{
216 io_service_t printer_obj;
217 classdriver_t **classdriver;
218
219 pthread_mutex_t read_thread_mutex;
220 pthread_cond_t read_thread_cond;
221 int read_thread_stop;
222 int read_thread_done;
ef416fc2 223
09a101d6 224 pthread_mutex_t readwrite_lock_mutex;
225 pthread_cond_t readwrite_lock_cond;
226 int readwrite_lock;
ef416fc2 227
e53920b9 228 CFStringRef make;
229 CFStringRef model;
230 CFStringRef serial;
e53920b9 231 UInt32 location;
bc44d920 232 UInt8 interfaceNum;
fe750b86 233 UInt8 alternateSetting;
68a36223 234 UInt8 interfaceProtocol;
09a101d6 235
236 CFRunLoopTimerRef status_timer;
237
238 int print_fd; /* File descriptor to print */
239 ssize_t print_bytes; /* Print bytes read */
acb056cb
MS
240#if DEBUG_WRITES
241 ssize_t debug_bytes; /* Current bytes to read */
242#endif /* DEBUG_WRITES */
09a101d6 243
7530b13a 244 Boolean use_generic_class_driver;
09a101d6 245 Boolean wait_eof;
246 int drain_output; /* Drain all pending output */
247 int bidi_flag; /* 0=unidirectional, 1=bidirectional */
7cf5915e
MS
248
249 pthread_mutex_t sidechannel_thread_mutex;
250 pthread_cond_t sidechannel_thread_cond;
251 int sidechannel_thread_stop;
252 int sidechannel_thread_done;
09a101d6 253} globals_t;
254
255
256/*
257 * Globals...
258 */
259
260globals_t g = { 0 }; /* Globals */
0fa6c7fa 261int Iterating = 0; /* Are we iterating the bus? */
ef416fc2 262
ef416fc2 263
e53920b9 264/*
265 * Local functions...
266 */
ef416fc2 267
68a36223
MS
268static Boolean list_device_cb(io_service_t obj, printer_interface_t printerIntf, void *refcon);
269static Boolean find_device_cb(io_service_t obj, printer_interface_t printerIntf, void *refcon);
fe750b86 270
09a101d6 271static CFStringRef cfstr_create_trim(const char *cstr);
272static CFStringRef copy_value_for_key(CFStringRef deviceID, CFStringRef *keys);
321d8d57 273static kern_return_t load_classdriver(CFStringRef driverPath, printer_interface_t interface, classdriver_t ***printerDriver);
09a101d6 274static kern_return_t load_printerdriver(CFStringRef *driverBundlePath);
eac3a0a0 275static kern_return_t registry_close(void);
09a101d6 276static kern_return_t registry_open(CFStringRef *driverBundlePath);
eac3a0a0 277static kern_return_t unload_classdriver(classdriver_t ***classdriver);
fe750b86 278
09a101d6 279static void *read_thread(void *reference);
280static void *sidechannel_thread(void *reference);
09a101d6 281static void device_added(void *userdata, io_iterator_t iterator);
282static void get_device_id(cups_sc_status_t *status, char *data, int *datalen);
283static void iterate_printers(iterator_callback_t callBack, void *userdata);
db1f069b 284static void parse_options(char *options, char *serial, int serial_size, UInt32 *location, Boolean *wait_eof);
e53920b9 285static void setup_cfLanguage(void);
eac3a0a0 286static void soft_reset(void);
09a101d6 287static void status_timer_cb(CFRunLoopTimerRef timer, void *info);
9fb65b89
MS
288#define IS_64BIT 1
289#define IS_NOT_64BIT 0
e53920b9 290
749b1e90 291#if defined(__i386__) || defined(__x86_64__)
f11a948a 292static pid_t child_pid; /* Child PID */
7e86f2f6 293static void run_legacy_backend(int argc, char *argv[], int fd) __attribute__((noreturn)); /* Starts child backend process running as a ppc executable */
18ecb428 294#endif /* __i386__ || __x86_64__ */
6c48a6ca 295static void sigterm_handler(int sig); /* SIGTERM handler */
7e86f2f6 296static void sigquit_handler(int sig, siginfo_t *si, void *unused) __attribute__((noreturn));
ef416fc2 297
e53920b9 298#ifdef PARSE_PS_ERRORS
299static const char *next_line (const char *buffer);
300static void parse_pserror (char *sockBuffer, int len);
301#endif /* PARSE_PS_ERRORS */
ef416fc2 302
f1ebe30e 303static printer_interface_t usb_printer_interface_interface(io_service_t usbClass);
68a36223 304
f1ebe30e
MS
305static CFStringRef copy_printer_interface_deviceid(printer_interface_t printer, UInt8 alternateSetting);
306static CFStringRef copy_printer_interface_indexed_description(printer_interface_t printer, UInt8 index, UInt16 language);
fe750b86
MS
307static CFStringRef deviceIDCopyManufacturer(CFStringRef deviceID);
308static CFStringRef deviceIDCopyModel(CFStringRef deviceID);
309static CFStringRef deviceIDCopySerialNumber(CFStringRef deviceID);
310
e53920b9 311#pragma mark -
ef416fc2 312
e53920b9 313/*
314 * 'list_devices()' - List all USB devices.
315 */
ef416fc2 316
e53920b9 317void list_devices()
ef416fc2 318{
09a101d6 319 iterate_printers(list_device_cb, NULL);
ef416fc2 320}
321
ef416fc2 322
e53920b9 323/*
324 * 'print_device()' - Print a file to a USB device.
325 */
ef416fc2 326
e53920b9 327int /* O - Exit status */
328print_device(const char *uri, /* I - Device URI */
329 const char *hostname, /* I - Hostname/manufacturer */
330 const char *resource, /* I - Resource/modelname */
db1f069b 331 char *options, /* I - Device options/serial number */
09a101d6 332 int print_fd, /* I - File descriptor to print */
e53920b9 333 int copies, /* I - Copies to print */
334 int argc, /* I - Number of command-line arguments (6 or 7) */
335 char *argv[]) /* I - Command-line arguments */
ef416fc2 336{
09a101d6 337 char serial[1024]; /* Serial number buffer */
338 OSStatus status; /* Function results */
acb056cb 339 IOReturn iostatus; /* Current IO status */
09a101d6 340 pthread_t read_thread_id, /* Read thread */
db1f069b 341 sidechannel_thread_id;/* Side-channel thread */
ac884b6a
MS
342 int have_sidechannel = 0; /* Was the side-channel thread started? */
343 struct stat sidechannel_info; /* Side-channel file descriptor info */
09a101d6 344 char print_buffer[8192], /* Print data buffer */
345 *print_ptr; /* Pointer into print data buffer */
346 UInt32 location; /* Unique location in bus topology */
347 fd_set input_set; /* Input set for select() */
348 CFStringRef driverBundlePath; /* Class driver path */
349 int countdown, /* Logging interval */
350 nfds; /* Number of file descriptors */
351 ssize_t total_bytes; /* Total bytes written */
352 UInt32 bytes; /* Bytes written */
353 struct timeval *timeout, /* Timeout pointer */
84315f46 354 tv; /* Time value */
7cf5915e 355 struct timespec cond_timeout; /* pthread condition timeout */
a469f8a5 356 struct sigaction action; /* Actions for POSIX signals */
e53920b9 357
db1f069b 358
321d8d57
MS
359 (void)uri;
360
a469f8a5
MS
361 /*
362 * Catch SIGQUIT to determine who is sending it...
363 */
364
365 memset(&action, 0, sizeof(action));
366 action.sa_sigaction = sigquit_handler;
367 action.sa_flags = SA_SIGINFO;
368 sigaction(SIGQUIT, &action, NULL);
369
ac884b6a
MS
370 /*
371 * See if the side-channel descriptor is valid...
372 */
373
374 have_sidechannel = !fstat(CUPS_SC_FD, &sidechannel_info) &&
375 S_ISSOCK(sidechannel_info.st_mode);
376
377 /*
378 * Localize using CoreFoundation...
379 */
380
e53920b9 381 setup_cfLanguage();
09a101d6 382
76cd9e37 383 parse_options(options, serial, sizeof(serial), &location, &g.wait_eof);
e53920b9 384
385 if (resource[0] == '/')
386 resource++;
387
09a101d6 388 g.print_fd = print_fd;
389 g.make = cfstr_create_trim(hostname);
390 g.model = cfstr_create_trim(resource);
391 g.serial = cfstr_create_trim(serial);
392 g.location = location;
e53920b9 393
7dfedb92
MS
394 if (!g.make || !g.model)
395 {
c7017ecc 396 fprintf(stderr, "DEBUG: Fatal USB error.\n");
0837b7e8
MS
397 _cupsLangPrintFilter(stderr, "ERROR",
398 _("There was an unrecoverable USB error."));
005dd1eb
MS
399
400 if (!g.make)
4d301e69 401 fputs("DEBUG: USB make string is NULL\n", stderr);
005dd1eb 402 if (!g.model)
4d301e69 403 fputs("DEBUG: USB model string is NULL\n", stderr);
005dd1eb
MS
404
405 return (CUPS_BACKEND_STOP);
7dfedb92
MS
406 }
407
e53920b9 408 fputs("STATE: +connecting-to-device\n", stderr);
409
09a101d6 410 countdown = INITIAL_LOG_INTERVAL;
411
412 do
413 {
414 if (g.printer_obj)
415 {
416 IOObjectRelease(g.printer_obj);
417 unload_classdriver(&g.classdriver);
418 g.printer_obj = 0x0;
419 g.classdriver = 0x0;
ef416fc2 420 }
c0e1af83 421 fprintf(stderr, "DEBUG: Looking for '%s %s'\n", hostname, resource);
09a101d6 422
68a36223
MS
423 do
424 {
425 iterate_printers(find_device_cb, NULL);
426 if (g.printer_obj != 0x0)
427 break;
428
429 _cupsLangPrintFilter(stderr, "INFO", _("Waiting for printer to become available."));
430 sleep(5);
431 } while (true);
e53920b9 432
c0e1af83 433 fputs("DEBUG: Opening connection\n", stderr);
411affcf 434
435 driverBundlePath = NULL;
09a101d6 436
437 status = registry_open(&driverBundlePath);
438
749b1e90 439#if defined(__i386__) || defined(__x86_64__)
e53920b9 440 /*
749b1e90
MS
441 * If we were unable to load the class drivers for this printer it's
442 * probably because they're ppc or i386. In this case try to run this
443 * backend as i386 or ppc executables so we can use them...
e53920b9 444 */
09a101d6 445 if (status == -2)
446 {
749b1e90 447 run_legacy_backend(argc, argv, print_fd);
e53920b9 448 /* Never returns here */
ef416fc2 449 }
749b1e90 450#endif /* __i386__ || __x86_64__ */
09a101d6 451
452 if (status == -2)
453 {
411affcf 454 /*
455 * If we still were unable to load the class drivers for this printer log
456 * the error and stop the queue...
457 */
458
09a101d6 459 if (driverBundlePath == NULL || !CFStringGetCString(driverBundlePath, print_buffer, sizeof(print_buffer), kCFStringEncodingUTF8))
460 strlcpy(print_buffer, "USB class driver", sizeof(print_buffer));
411affcf 461
c0e1af83 462 fputs("STATE: +apple-missing-usbclassdriver-error\n", stderr);
0837b7e8
MS
463 _cupsLangPrintFilter(stderr, "ERROR",
464 _("There was an unrecoverable USB error."));
005dd1eb 465 fprintf(stderr, "DEBUG: Could not load %s\n", print_buffer);
411affcf 466
467 if (driverBundlePath)
468 CFRelease(driverBundlePath);
469
005dd1eb 470 return (CUPS_BACKEND_STOP);
411affcf 471 }
472
473 if (driverBundlePath)
474 CFRelease(driverBundlePath);
e53920b9 475
09a101d6 476 if (status != noErr)
477 {
478 sleep(PRINTER_POLLING_INTERVAL);
e53920b9 479 countdown -= PRINTER_POLLING_INTERVAL;
09a101d6 480 if (countdown <= 0)
481 {
0837b7e8
MS
482 _cupsLangPrintFilter(stderr, "INFO",
483 _("Waiting for printer to become available."));
005dd1eb 484 fprintf(stderr, "DEBUG: USB printer status: 0x%08x\n", (int)status);
e53920b9 485 countdown = SUBSEQUENT_LOG_INTERVAL; /* subsequent log entries, every 15 seconds */
486 }
487 }
488 } while (status != noErr);
ef416fc2 489
e53920b9 490 fputs("STATE: -connecting-to-device\n", stderr);
ef416fc2 491
e53920b9 492 /*
18ecb428 493 * Now that we are "connected" to the port, ignore SIGTERM so that we
e53920b9 494 * can finish out any page data the driver sends (e.g. to eject the
18ecb428 495 * current page... Only ignore SIGTERM if we are printing data from
e53920b9 496 * stdin (otherwise you can't cancel raw jobs...)
497 */
ef416fc2 498
09a101d6 499 if (!print_fd)
500 {
e53920b9 501 memset(&action, 0, sizeof(action));
ef416fc2 502
e53920b9 503 sigemptyset(&action.sa_mask);
18ecb428 504 action.sa_handler = SIG_IGN;
e53920b9 505 sigaction(SIGTERM, &action, NULL);
e53920b9 506 }
ef416fc2 507
09a101d6 508 /*
ac884b6a 509 * Start the side channel thread if the descriptor is valid...
09a101d6 510 */
ef416fc2 511
09a101d6 512 pthread_mutex_init(&g.readwrite_lock_mutex, NULL);
513 pthread_cond_init(&g.readwrite_lock_cond, NULL);
514 g.readwrite_lock = 1;
f7deaa1a 515
ac884b6a 516 if (have_sidechannel)
09a101d6 517 {
7cf5915e
MS
518 g.sidechannel_thread_stop = 0;
519 g.sidechannel_thread_done = 0;
520
521 pthread_cond_init(&g.sidechannel_thread_cond, NULL);
522 pthread_mutex_init(&g.sidechannel_thread_mutex, NULL);
523
09a101d6 524 if (pthread_create(&sidechannel_thread_id, NULL, sidechannel_thread, NULL))
525 {
c7017ecc 526 fprintf(stderr, "DEBUG: Fatal USB error.\n");
0837b7e8
MS
527 _cupsLangPrintFilter(stderr, "ERROR",
528 _("There was an unrecoverable USB error."));
4d301e69 529 fputs("DEBUG: Couldn't create side-channel thread\n", stderr);
ef55b745 530 registry_close();
005dd1eb 531 return (CUPS_BACKEND_STOP);
09a101d6 532 }
533 }
f7deaa1a 534
09a101d6 535 /*
536 * Get the read thread going...
537 */
f7deaa1a 538
09a101d6 539 g.read_thread_stop = 0;
540 g.read_thread_done = 0;
f7deaa1a 541
09a101d6 542 pthread_cond_init(&g.read_thread_cond, NULL);
543 pthread_mutex_init(&g.read_thread_mutex, NULL);
f7deaa1a 544
09a101d6 545 if (pthread_create(&read_thread_id, NULL, read_thread, NULL))
546 {
c7017ecc 547 fprintf(stderr, "DEBUG: Fatal USB error.\n");
0837b7e8
MS
548 _cupsLangPrintFilter(stderr, "ERROR",
549 _("There was an unrecoverable USB error."));
4d301e69 550 fputs("DEBUG: Couldn't create read thread\n", stderr);
ef55b745 551 registry_close();
005dd1eb 552 return (CUPS_BACKEND_STOP);
e53920b9 553 }
ef416fc2 554
09a101d6 555 /*
556 * The main thread sends the print file...
557 */
f7deaa1a 558
09a101d6 559 g.drain_output = 0;
560 g.print_bytes = 0;
561 total_bytes = 0;
562 print_ptr = print_buffer;
ef416fc2 563
09a101d6 564 while (status == noErr && copies-- > 0)
565 {
0837b7e8 566 _cupsLangPrintFilter(stderr, "INFO", _("Sending data to printer."));
757d2cad 567
09a101d6 568 if (print_fd != STDIN_FILENO)
569 {
749b1e90 570 fputs("PAGE: 1 1\n", stderr);
09a101d6 571 lseek(print_fd, 0, SEEK_SET);
ef416fc2 572 }
573
09a101d6 574 while (status == noErr)
575 {
576 FD_ZERO(&input_set);
e53920b9 577
09a101d6 578 if (!g.print_bytes)
579 FD_SET(print_fd, &input_set);
f7deaa1a 580
09a101d6 581 /*
582 * Calculate select timeout...
583 * If we have data waiting to send timeout is 100ms.
584 * else if we're draining print_fd timeout is 0.
585 * else we're waiting forever...
586 */
587
588 if (g.print_bytes)
589 {
84315f46
MS
590 tv.tv_sec = 0;
591 tv.tv_usec = 100000; /* 100ms */
592 timeout = &tv;
09a101d6 593 }
594 else if (g.drain_output)
595 {
84315f46
MS
596 tv.tv_sec = 0;
597 tv.tv_usec = 0;
598 timeout = &tv;
09a101d6 599 }
600 else
601 timeout = NULL;
602
603 /*
604 * I/O is unlocked around select...
605 */
606
607 pthread_mutex_lock(&g.readwrite_lock_mutex);
608 g.readwrite_lock = 0;
609 pthread_cond_signal(&g.readwrite_lock_cond);
610 pthread_mutex_unlock(&g.readwrite_lock_mutex);
611
612 nfds = select(print_fd + 1, &input_set, NULL, NULL, timeout);
613
614 /*
615 * Reacquire the lock...
616 */
617
618 pthread_mutex_lock(&g.readwrite_lock_mutex);
619 while (g.readwrite_lock)
620 pthread_cond_wait(&g.readwrite_lock_cond, &g.readwrite_lock_mutex);
621 g.readwrite_lock = 1;
622 pthread_mutex_unlock(&g.readwrite_lock_mutex);
623
624 if (nfds < 0)
625 {
626 if (errno == EINTR && total_bytes == 0)
627 {
628 fputs("DEBUG: Received an interrupt before any bytes were "
4d301e69 629 "written, aborting\n", stderr);
ef55b745 630 registry_close();
f11a948a 631 return (CUPS_BACKEND_OK);
09a101d6 632 }
536bc2c6 633 else if (errno != EAGAIN && errno != EINTR)
09a101d6 634 {
0837b7e8
MS
635 _cupsLangPrintFilter(stderr, "ERROR",
636 _("Unable to read print data."));
005dd1eb 637 perror("DEBUG: select");
ef55b745
MS
638 registry_close();
639 return (CUPS_BACKEND_FAILED);
09a101d6 640 }
641 }
642
643 /*
644 * If drain output has finished send a response...
645 */
646
647 if (g.drain_output && !nfds && !g.print_bytes)
648 {
649 /* Send a response... */
650 cupsSideChannelWrite(CUPS_SC_CMD_DRAIN_OUTPUT, CUPS_SC_STATUS_OK, NULL, 0, 1.0);
651 g.drain_output = 0;
652 }
653
654 /*
655 * Check if we have print data ready...
656 */
657
658 if (FD_ISSET(print_fd, &input_set))
659 {
acb056cb
MS
660#if DEBUG_WRITES
661 g.debug_bytes += 512;
662 if (g.debug_bytes > sizeof(print_buffer))
663 g.debug_bytes = 512;
664
665 g.print_bytes = read(print_fd, print_buffer, g.debug_bytes);
666
667#else
09a101d6 668 g.print_bytes = read(print_fd, print_buffer, sizeof(print_buffer));
acb056cb 669#endif /* DEBUG_WRITES */
09a101d6 670
671 if (g.print_bytes < 0)
672 {
673 /*
674 * Read error - bail if we don't see EAGAIN or EINTR...
675 */
676
536bc2c6 677 if (errno != EAGAIN && errno != EINTR)
09a101d6 678 {
0837b7e8
MS
679 _cupsLangPrintFilter(stderr, "ERROR",
680 _("Unable to read print data."));
005dd1eb 681 perror("DEBUG: read");
ef55b745 682 registry_close();
f11a948a 683 return (CUPS_BACKEND_FAILED);
09a101d6 684 }
685
686 g.print_bytes = 0;
f7deaa1a 687 }
09a101d6 688 else if (g.print_bytes == 0)
689 {
690 /*
691 * End of file, break out of the loop...
692 */
f7deaa1a 693
e53920b9 694 break;
ef416fc2 695 }
696
09a101d6 697 print_ptr = print_buffer;
698
699 fprintf(stderr, "DEBUG: Read %d bytes of print data...\n",
700 (int)g.print_bytes);
e53920b9 701 }
ef416fc2 702
09a101d6 703 if (g.print_bytes)
704 {
7e86f2f6 705 bytes = (UInt32)g.print_bytes;
178cb736 706 iostatus = (*g.classdriver)->WritePipe(g.classdriver, (UInt8*)print_ptr, &bytes, 0);
f7deaa1a 707
09a101d6 708 /*
58dc1933 709 * Ignore timeout errors, but retain the number of bytes written to
5a9febac 710 * avoid sending duplicate data...
09a101d6 711 */
e53920b9 712
178cb736 713 if (iostatus == kIOUSBTransactionTimeout)
b0f6947b 714 {
4d301e69 715 fputs("DEBUG: Got USB transaction timeout during write\n", stderr);
178cb736 716 iostatus = 0;
b0f6947b 717 }
ef416fc2 718
178cb736 719 /*
acb056cb 720 * If we've stalled, retry the write...
178cb736
MS
721 */
722
b0f6947b
MS
723 else if (iostatus == kIOUSBPipeStalled)
724 {
4d301e69 725 fputs("DEBUG: Got USB pipe stalled during write\n", stderr);
b0f6947b 726
7e86f2f6 727 bytes = (UInt32)g.print_bytes;
acb056cb 728 iostatus = (*g.classdriver)->WritePipe(g.classdriver, (UInt8*)print_ptr, &bytes, 0);
b0f6947b 729 }
178cb736
MS
730
731 /*
acb056cb 732 * Retry a write after an aborted write since we probably just got
5a9febac 733 * SIGTERM...
178cb736
MS
734 */
735
b0f6947b 736 else if (iostatus == kIOReturnAborted)
178cb736 737 {
3fb9c47e 738 fputs("DEBUG: Got USB return aborted during write\n", stderr);
b0f6947b
MS
739
740 IOReturn err = (*g.classdriver)->Abort(g.classdriver);
741 fprintf(stderr, "DEBUG: USB class driver Abort returned %x\n", err);
742
acb056cb
MS
743#if DEBUG_WRITES
744 sleep(5);
745#endif /* DEBUG_WRITES */
b0f6947b 746
7e86f2f6 747 bytes = (UInt32)g.print_bytes;
acb056cb 748 iostatus = (*g.classdriver)->WritePipe(g.classdriver, (UInt8*)print_ptr, &bytes, 0);
b0f6947b 749 }
178cb736 750
82f97232 751 if (iostatus)
09a101d6 752 {
753 /*
754 * Write error - bail if we don't see an error we can retry...
755 */
84315f46 756
0837b7e8
MS
757 _cupsLangPrintFilter(stderr, "ERROR",
758 _("Unable to send data to printer."));
178cb736
MS
759 fprintf(stderr, "DEBUG: USB class driver WritePipe returned %x\n",
760 iostatus);
b0f6947b
MS
761
762 IOReturn err = (*g.classdriver)->Abort(g.classdriver);
178cb736
MS
763 fprintf(stderr, "DEBUG: USB class driver Abort returned %x\n",
764 err);
b0f6947b 765
18ecb428 766 status = CUPS_BACKEND_FAILED;
bc44d920 767 break;
09a101d6 768 }
b0f6947b
MS
769 else if (bytes > 0)
770 {
771 fprintf(stderr, "DEBUG: Wrote %d bytes of print data...\n", (int)bytes);
09a101d6 772
b0f6947b
MS
773 g.print_bytes -= bytes;
774 print_ptr += bytes;
775 total_bytes += bytes;
776 }
f7deaa1a 777 }
09a101d6 778
779 if (print_fd != 0 && status == noErr)
780 fprintf(stderr, "DEBUG: Sending print file, %lld bytes...\n",
781 (off_t)total_bytes);
f7deaa1a 782 }
f7deaa1a 783 }
784
09a101d6 785 fprintf(stderr, "DEBUG: Sent %lld bytes...\n", (off_t)total_bytes);
1e3e80bb 786 fputs("STATE: +cups-waiting-for-job-completed\n", stderr);
f7deaa1a 787
7cf5915e 788 /*
a4845881 789 * Signal the side channel thread to exit...
7cf5915e
MS
790 */
791
792 if (have_sidechannel)
793 {
794 close(CUPS_SC_FD);
795 pthread_mutex_lock(&g.readwrite_lock_mutex);
796 g.readwrite_lock = 0;
797 pthread_cond_signal(&g.readwrite_lock_cond);
798 pthread_mutex_unlock(&g.readwrite_lock_mutex);
799
800 g.sidechannel_thread_stop = 1;
801 pthread_mutex_lock(&g.sidechannel_thread_mutex);
84315f46 802
7cf5915e
MS
803 if (!g.sidechannel_thread_done)
804 {
84315f46
MS
805 gettimeofday(&tv, NULL);
806 cond_timeout.tv_sec = tv.tv_sec + WAIT_SIDE_DELAY;
807 cond_timeout.tv_nsec = tv.tv_usec * 1000;
7cf5915e 808
84315f46
MS
809 while (!g.sidechannel_thread_done)
810 {
811 if (pthread_cond_timedwait(&g.sidechannel_thread_cond,
812 &g.sidechannel_thread_mutex,
813 &cond_timeout) != 0)
814 break;
815 }
7cf5915e 816 }
84315f46 817
7cf5915e 818 pthread_mutex_unlock(&g.sidechannel_thread_mutex);
7cf5915e
MS
819 }
820
7cf5915e 821 /*
a4845881 822 * Signal the read thread to exit then wait 7 seconds for it to complete...
7cf5915e
MS
823 */
824
825 g.read_thread_stop = 1;
826
7cf5915e
MS
827 pthread_mutex_lock(&g.read_thread_mutex);
828
829 if (!g.read_thread_done)
830 {
a4845881
MS
831 fputs("DEBUG: Waiting for read thread to exit...\n", stderr);
832
84315f46
MS
833 gettimeofday(&tv, NULL);
834 cond_timeout.tv_sec = tv.tv_sec + WAIT_EOF_DELAY;
835 cond_timeout.tv_nsec = tv.tv_usec * 1000;
7cf5915e 836
84315f46
MS
837 while (!g.read_thread_done)
838 {
839 if (pthread_cond_timedwait(&g.read_thread_cond, &g.read_thread_mutex,
840 &cond_timeout) != 0)
841 break;
842 }
843
a4845881
MS
844 /*
845 * If it didn't exit abort the pending read and wait an additional second...
846 */
f3c17241 847
84315f46 848 if (!g.read_thread_done)
7cf5915e 849 {
f3c17241 850 fputs("DEBUG: Read thread still active, aborting the pending read...\n",
a4845881 851 stderr);
7cf5915e
MS
852
853 g.wait_eof = 0;
a4845881
MS
854
855 (*g.classdriver)->Abort(g.classdriver);
856
857 gettimeofday(&tv, NULL);
858 cond_timeout.tv_sec = tv.tv_sec + 1;
859 cond_timeout.tv_nsec = tv.tv_usec * 1000;
f3c17241 860
a4845881
MS
861 while (!g.read_thread_done)
862 {
863 if (pthread_cond_timedwait(&g.read_thread_cond, &g.read_thread_mutex,
864 &cond_timeout) != 0)
865 break;
866 }
7cf5915e
MS
867 }
868 }
84315f46 869
7cf5915e
MS
870 pthread_mutex_unlock(&g.read_thread_mutex);
871
09a101d6 872 /*
873 * Close the connection and input file and general clean up...
874 */
f7deaa1a 875
09a101d6 876 registry_close();
f7deaa1a 877
09a101d6 878 if (print_fd != STDIN_FILENO)
879 close(print_fd);
f7deaa1a 880
09a101d6 881 if (g.make != NULL)
882 CFRelease(g.make);
e53920b9 883
09a101d6 884 if (g.model != NULL)
885 CFRelease(g.model);
e53920b9 886
09a101d6 887 if (g.serial != NULL)
888 CFRelease(g.serial);
e53920b9 889
09a101d6 890 if (g.printer_obj != 0x0)
891 IOObjectRelease(g.printer_obj);
e53920b9 892
893 return status;
ef416fc2 894}
895
09a101d6 896
897/*
898 * 'read_thread()' - Thread to read the backchannel data on.
899 */
900
901static void *read_thread(void *reference)
902{
903 UInt8 readbuffer[512];
904 UInt32 rbytes;
905 kern_return_t readstatus;
906 struct mach_timebase_info timeBaseInfo;
907 uint64_t start,
908 delay;
909
321d8d57
MS
910
911 (void)reference;
912
09a101d6 913 /* Calculate what 250 milliSeconds are in mach absolute time...
914 */
915 mach_timebase_info(&timeBaseInfo);
916 delay = ((uint64_t)250000000 * (uint64_t)timeBaseInfo.denom) / (uint64_t)timeBaseInfo.numer;
917
918 do
919 {
920 /*
921 * Remember when we started so we can throttle the loop after the read call...
922 */
923
924 start = mach_absolute_time();
925
926 rbytes = sizeof(readbuffer);
927 readstatus = (*g.classdriver)->ReadPipe(g.classdriver, readbuffer, &rbytes);
928 if (readstatus == kIOReturnSuccess && rbytes > 0)
929 {
b0f6947b
MS
930 fprintf(stderr, "DEBUG: Read %d bytes of back-channel data...\n",
931 (int)rbytes);
09a101d6 932 cupsBackChannelWrite((char*)readbuffer, rbytes, 1.0);
933
934 /* cntrl-d is echoed by the printer.
935 * NOTES:
936 * Xerox Phaser 6250D doesn't echo the cntrl-d.
937 * Xerox Phaser 6250D doesn't always send the product query.
938 */
939 if (g.wait_eof && readbuffer[rbytes-1] == 0x4)
940 break;
941
942#ifdef PARSE_PS_ERRORS
943 parse_pserror(readbuffer, rbytes);
944#endif
945 }
b0f6947b 946 else if (readstatus == kIOUSBTransactionTimeout)
3fb9c47e 947 fputs("DEBUG: Got USB transaction timeout during read\n", stderr);
b0f6947b 948 else if (readstatus == kIOUSBPipeStalled)
4d301e69 949 fputs("DEBUG: Got USB pipe stalled during read\n", stderr);
b0f6947b 950 else if (readstatus == kIOReturnAborted)
3fb9c47e 951 fputs("DEBUG: Got USB return aborted during read\n", stderr);
09a101d6 952
953 /*
954 * Make sure this loop executes no more than once every 250 miliseconds...
955 */
956
957 if ((readstatus != kIOReturnSuccess || rbytes == 0) && (g.wait_eof || !g.read_thread_stop))
958 mach_wait_until(start + delay);
959
960 } while (g.wait_eof || !g.read_thread_stop); /* Abort from main thread tests error here */
961
7530b13a
MS
962 /* Workaround for usb race condition. <rdar://problem/21882551> */
963 if (!g.wait_eof && g.use_generic_class_driver)
964 {
965 const char *pdl = getenv("FINAL_CONTENT_TYPE");
966 if (pdl && strcmp(pdl, "application/vnd.cups-postscript") == 0)
967 {
968 while (readstatus == kIOReturnSuccess && ((rbytes > 0 && readbuffer[rbytes-1] != 0x4) || rbytes == 0))
969 {
970 start = mach_absolute_time();
971
972 rbytes = sizeof(readbuffer);
973 readstatus = (*g.classdriver)->ReadPipe(g.classdriver, readbuffer, &rbytes);
974 if (readstatus == kIOReturnSuccess && rbytes > 0 && readbuffer[rbytes-1] == 0x4)
975 break;
976
977 /* Make sure this loop executes no more than once every 250 miliseconds... */
978 mach_wait_until(start + delay);
979 }
980 }
981 }
982
09a101d6 983 /*
984 * Let the main thread know that we have completed the read thread...
985 */
986
987 pthread_mutex_lock(&g.read_thread_mutex);
988 g.read_thread_done = 1;
989 pthread_cond_signal(&g.read_thread_cond);
990 pthread_mutex_unlock(&g.read_thread_mutex);
991
992 return NULL;
993}
994
995
996/*
997 * 'sidechannel_thread()' - Handle side-channel requests.
998 */
999
1000static void*
1001sidechannel_thread(void *reference)
1002{
1003 cups_sc_command_t command; /* Request command */
1004 cups_sc_status_t status; /* Request/response status */
1005 char data[2048]; /* Request/response data */
1006 int datalen; /* Request/response data size */
1007
bc44d920 1008
321d8d57
MS
1009 (void)reference;
1010
bc44d920 1011 do
09a101d6 1012 {
1013 datalen = sizeof(data);
1014
1015 if (cupsSideChannelRead(&command, &status, data, &datalen, 1.0))
ef55b745
MS
1016 {
1017 if (status == CUPS_SC_STATUS_TIMEOUT)
1018 continue;
1019 else
1020 break;
1021 }
09a101d6 1022
1023 switch (command)
1024 {
1025 case CUPS_SC_CMD_SOFT_RESET: /* Do a soft reset */
b0f6947b
MS
1026 fputs("DEBUG: CUPS_SC_CMD_SOFT_RESET received from driver...\n",
1027 stderr);
1028
db1f069b
MS
1029 if ((*g.classdriver)->SoftReset != NULL)
1030 {
1031 soft_reset();
1032 cupsSideChannelWrite(command, CUPS_SC_STATUS_OK, NULL, 0, 1.0);
b0f6947b
MS
1033 fputs("DEBUG: Returning status CUPS_STATUS_OK with no bytes...\n",
1034 stderr);
db1f069b
MS
1035 }
1036 else
1037 {
1038 cupsSideChannelWrite(command, CUPS_SC_STATUS_NOT_IMPLEMENTED,
1039 NULL, 0, 1.0);
b0f6947b
MS
1040 fputs("DEBUG: Returning status CUPS_STATUS_NOT_IMPLEMENTED with "
1041 "no bytes...\n", stderr);
db1f069b 1042 }
09a101d6 1043 break;
1044
1045 case CUPS_SC_CMD_DRAIN_OUTPUT: /* Drain all pending output */
b0f6947b
MS
1046 fputs("DEBUG: CUPS_SC_CMD_DRAIN_OUTPUT received from driver...\n",
1047 stderr);
1048
09a101d6 1049 g.drain_output = 1;
1050 break;
1051
1052 case CUPS_SC_CMD_GET_BIDI: /* Is the connection bidirectional? */
b0f6947b
MS
1053 fputs("DEBUG: CUPS_SC_CMD_GET_BIDI received from driver...\n",
1054 stderr);
1055
7e86f2f6 1056 data[0] = (char)g.bidi_flag;
09a101d6 1057 cupsSideChannelWrite(command, CUPS_SC_STATUS_OK, data, 1, 1.0);
b0f6947b
MS
1058
1059 fprintf(stderr,
1060 "DEBUG: Returned CUPS_SC_STATUS_OK with 1 byte (%02X)...\n",
1061 data[0]);
09a101d6 1062 break;
1063
1064 case CUPS_SC_CMD_GET_DEVICE_ID: /* Return IEEE-1284 device ID */
b0f6947b
MS
1065 fputs("DEBUG: CUPS_SC_CMD_GET_DEVICE_ID received from driver...\n",
1066 stderr);
1067
09a101d6 1068 datalen = sizeof(data);
1069 get_device_id(&status, data, &datalen);
1070 cupsSideChannelWrite(command, CUPS_SC_STATUS_OK, data, datalen, 1.0);
b0f6947b 1071
7e86f2f6 1072 if ((size_t)datalen < sizeof(data))
b0f6947b
MS
1073 data[datalen] = '\0';
1074 else
1075 data[sizeof(data) - 1] = '\0';
1076
1077 fprintf(stderr,
1078 "DEBUG: Returning CUPS_SC_STATUS_OK with %d bytes (%s)...\n",
1079 datalen, data);
09a101d6 1080 break;
1081
1082 case CUPS_SC_CMD_GET_STATE: /* Return device state */
b0f6947b
MS
1083 fputs("DEBUG: CUPS_SC_CMD_GET_STATE received from driver...\n",
1084 stderr);
1085
09a101d6 1086 data[0] = CUPS_SC_STATE_ONLINE;
1087 cupsSideChannelWrite(command, CUPS_SC_STATUS_OK, data, 1, 1.0);
b0f6947b
MS
1088
1089 fprintf(stderr,
1090 "DEBUG: Returned CUPS_SC_STATUS_OK with 1 byte (%02X)...\n",
1091 data[0]);
09a101d6 1092 break;
1093
1094 default:
b0f6947b
MS
1095 fprintf(stderr, "DEBUG: Unknown side-channel command (%d) received "
1096 "from driver...\n", command);
1097
09a101d6 1098 cupsSideChannelWrite(command, CUPS_SC_STATUS_NOT_IMPLEMENTED,
1099 NULL, 0, 1.0);
b0f6947b
MS
1100
1101 fputs("DEBUG: Returned CUPS_SC_STATUS_NOT_IMPLEMENTED with no bytes...\n",
1102 stderr);
09a101d6 1103 break;
1104 }
1105 }
7cf5915e
MS
1106 while (!g.sidechannel_thread_stop);
1107
1108 pthread_mutex_lock(&g.sidechannel_thread_mutex);
1109 g.sidechannel_thread_done = 1;
1110 pthread_cond_signal(&g.sidechannel_thread_cond);
1111 pthread_mutex_unlock(&g.sidechannel_thread_mutex);
bc44d920 1112
09a101d6 1113 return NULL;
1114}
1115
1116
e53920b9 1117#pragma mark -
ef416fc2 1118/*
09a101d6 1119 * 'iterate_printers()' - Iterate over all the printers.
1120 */
fe750b86 1121static void iterate_printers(iterator_callback_t callBack, void *userdata)
09a101d6 1122{
68a36223 1123 Iterating = 1;
de3edeba 1124
68a36223 1125 iterator_reference_t reference = { callBack, userdata, true };
de3edeba 1126
68a36223 1127 IONotificationPortRef addNotification = IONotificationPortCreate(kIOMasterPortDefault);
de3edeba 1128
68a36223
MS
1129 int printingClass = kUSBPrintingClass;
1130 int printingSubclass = kUSBPrintingSubclass;
de3edeba 1131
68a36223
MS
1132 CFNumberRef interfaceClass = CFNumberCreate(kCFAllocatorDefault, kCFNumberIntType, &printingClass);
1133 CFNumberRef interfaceSubClass = CFNumberCreate(kCFAllocatorDefault, kCFNumberIntType, &printingSubclass);
1134
1135 CFMutableDictionaryRef usbPrinterMatchDictionary = IOServiceMatching(kIOUSBInterfaceClassName);
1136 CFDictionaryAddValue(usbPrinterMatchDictionary, CFSTR("bInterfaceClass"), interfaceClass);
1137 CFDictionaryAddValue(usbPrinterMatchDictionary, CFSTR("bInterfaceSubClass"), interfaceSubClass);
1138
1139 CFRelease(interfaceClass);
1140 CFRelease(interfaceSubClass);
de3edeba 1141
68a36223
MS
1142 io_iterator_t add_iterator = IO_OBJECT_NULL;
1143 IOServiceAddMatchingNotification(addNotification, kIOMatchedNotification,
1144 usbPrinterMatchDictionary, &device_added, &reference, &add_iterator);
1145 if (add_iterator != IO_OBJECT_NULL)
1146 {
1147 device_added (&reference, add_iterator);
1148 if (reference.keepRunning)
1149 {
1150 CFRunLoopAddSource(CFRunLoopGetCurrent(), IONotificationPortGetRunLoopSource(addNotification), kCFRunLoopDefaultMode);
1151 CFRunLoopRun();
1152 }
1153 IOObjectRelease(add_iterator);
1154 }
1155 Iterating = 0;
09a101d6 1156}
1157
1158
1159/*
1160 * 'device_added()' - Device added notifier.
e53920b9 1161 */
fe750b86 1162static void device_added(void *userdata, io_iterator_t iterator)
09a101d6 1163{
68a36223
MS
1164 iterator_reference_t *reference = userdata;
1165 io_service_t intf;
de3edeba 1166
68a36223
MS
1167 while (reference->keepRunning && (intf = IOIteratorNext(iterator)) != 0x0)
1168 {
1169 printer_interface_t printerIntf = usb_printer_interface_interface(intf);
1170 if (printerIntf != NULL)
fe750b86 1171 {
68a36223 1172 UInt8 intfClass = 0, intfSubClass = 0;
fe750b86 1173
68a36223
MS
1174 (*printerIntf)->GetInterfaceClass(printerIntf, &intfClass);
1175 (*printerIntf)->GetInterfaceSubClass(printerIntf, &intfSubClass);
1176 if (intfClass == kUSBPrintingInterfaceClass && intfSubClass == kUSBPrintingSubclass)
1177 reference->keepRunning = reference->callback(intf, printerIntf, userdata);
1178 (*printerIntf)->Release(printerIntf);
1179 }
1180 IOObjectRelease(intf);
fe750b86 1181 }
de3edeba 1182
fe750b86 1183 if (reference->keepRunning && reference->callback)
68a36223 1184 reference->keepRunning = reference->callback(IO_OBJECT_NULL, NULL, reference->userdata);
de3edeba 1185
fe750b86 1186 if (!reference->keepRunning)
68a36223 1187 CFRunLoopStop(CFRunLoopGetCurrent());
09a101d6 1188}
1189
09a101d6 1190/*
1191 * 'list_device_cb()' - list_device iterator callback.
1192 */
68a36223 1193static Boolean list_device_cb(io_service_t obj, printer_interface_t printerIntf, void *refcon)
ef416fc2 1194{
68a36223 1195 (void)refcon;
321d8d57 1196
68a36223
MS
1197 if (obj != IO_OBJECT_NULL)
1198 {
1199 CFStringRef deviceIDString = NULL;
1200 CFStringRef make = NULL;
1201 CFStringRef model = NULL;
1202 CFStringRef serial = NULL;
1203 UInt32 intfLocation;
de3edeba 1204
68a36223
MS
1205 deviceIDString = copy_printer_interface_deviceid(printerIntf, 0);
1206 if (deviceIDString == NULL)
1207 goto list_device_done;
de3edeba 1208
68a36223
MS
1209 make = deviceIDCopyManufacturer(deviceIDString);
1210 model = deviceIDCopyModel(deviceIDString);
1211 serial = deviceIDCopySerialNumber(deviceIDString);
de3edeba 1212
68a36223
MS
1213 char uristr[1024], makestr[1024], modelstr[1024], serialstr[1024];
1214 char optionsstr[1024], idstr[1024], make_modelstr[1024];
de3edeba 1215
68a36223
MS
1216 CFStringGetCString(deviceIDString, idstr, sizeof(idstr), kCFStringEncodingUTF8);
1217 backendGetMakeModel(idstr, make_modelstr, sizeof(make_modelstr));
de3edeba 1218
68a36223 1219 modelstr[0] = '/';
de3edeba 1220
68a36223
MS
1221 if (make == NULL || !CFStringGetCString(make, makestr, sizeof(makestr), kCFStringEncodingUTF8))
1222 strlcpy(makestr, "Unknown", sizeof(makestr));
de3edeba 1223
68a36223
MS
1224 if (model == NULL || !CFStringGetCString(model, &modelstr[1], sizeof(modelstr)-1, kCFStringEncodingUTF8))
1225 strlcpy(modelstr + 1, "Printer", sizeof(modelstr) - 1);
de3edeba 1226
68a36223
MS
1227 optionsstr[0] = '\0';
1228 if (serial != NULL && CFStringGetCString(serial, serialstr, sizeof(serialstr), kCFStringEncodingUTF8))
1229 snprintf(optionsstr, sizeof(optionsstr), "?serial=%s", serialstr);
1230 else if ((*printerIntf)->GetLocationID(printerIntf, &intfLocation) == kIOReturnSuccess)
1231 snprintf(optionsstr, sizeof(optionsstr), "?location=%x", (unsigned)intfLocation);
7530b13a 1232
68a36223
MS
1233 httpAssembleURI(HTTP_URI_CODING_ALL, uristr, sizeof(uristr), "usb", NULL, makestr, 0, modelstr);
1234 strlcat(uristr, optionsstr, sizeof(uristr));
1235
1236 cupsBackendReport("direct", uristr, make_modelstr, make_modelstr, idstr,
1237 NULL);
1238 list_device_done:
de3edeba 1239
68a36223
MS
1240 if (make != NULL) CFRelease(make);
1241 if (model != NULL) CFRelease(model);
1242 if (serial != NULL) CFRelease(serial);
1243 }
1244 return obj != IO_OBJECT_NULL;
ef416fc2 1245}
1246
e53920b9 1247/*
09a101d6 1248 * 'find_device_cb()' - print_device iterator callback.
e53920b9 1249 */
68a36223 1250static Boolean find_device_cb(io_service_t obj, printer_interface_t printerIntf, void *refcon)
e53920b9 1251{
68a36223 1252 (void)refcon;
de3edeba 1253
68a36223
MS
1254 Boolean keepLooking = true;
1255
1256 if (obj != IO_OBJECT_NULL)
1257 {
1258 CFStringRef deviceIDString = NULL;
1259 CFStringRef make = NULL;
1260 CFStringRef model = NULL;
1261 CFStringRef serial = NULL;
1262
1263 deviceIDString = copy_printer_interface_deviceid(printerIntf, 0);
1264 if (deviceIDString == NULL)
1265 goto find_device_done;
1266
1267 make = deviceIDCopyManufacturer(deviceIDString);
1268 model = deviceIDCopyModel(deviceIDString);
1269 serial = deviceIDCopySerialNumber(deviceIDString);
1270
1271 if (make && CFStringCompare(make, g.make, kCFCompareCaseInsensitive) == kCFCompareEqualTo)
09a101d6 1272 {
68a36223
MS
1273 if (model && CFStringCompare(model, g.model, kCFCompareCaseInsensitive) == kCFCompareEqualTo)
1274 {
1275 UInt8 intfAltSetting = 0, intfNumber = 0, intfProtocol = 0;
1276 UInt32 intfLocation = 0;
1277
1278 (*printerIntf)->GetInterfaceProtocol(printerIntf, &intfProtocol);
1279 (*printerIntf)->GetAlternateSetting(printerIntf, &intfAltSetting);
1280 (*printerIntf)->GetInterfaceNumber(printerIntf, &intfNumber);
1281 (*printerIntf)->GetLocationID(printerIntf, &intfLocation);
de3edeba 1282
68a36223 1283 if (g.serial != NULL && CFStringGetLength(g.serial) > 0)
fe750b86 1284 {
68a36223
MS
1285 if (serial != NULL && CFStringCompare(serial, g.serial, kCFCompareCaseInsensitive) == kCFCompareEqualTo)
1286 {
1287 g.interfaceProtocol = intfProtocol;
1288 g.location = intfLocation;
1289 g.alternateSetting = intfAltSetting;
1290 if (intfProtocol != kUSBPrintingProtocolIPP)
fe750b86 1291 {
68a36223
MS
1292 g.printer_obj = obj;
1293 IOObjectRetain(obj);
fe750b86 1294 }
68a36223
MS
1295 keepLooking = (intfProtocol == kUSBPrintingProtocolIPP);
1296 }
fe750b86 1297 }
68a36223 1298 else
fe750b86 1299 {
68a36223
MS
1300 if (g.printer_obj != 0)
1301 IOObjectRelease(g.printer_obj);
1302
1303 g.location = intfLocation;
1304 g.alternateSetting = intfAltSetting;
1305 g.interfaceProtocol = intfProtocol;
1306 g.printer_obj = obj;
1307 IOObjectRetain(obj);
1308
1309 if (g.location == 0 || g.location == intfLocation)
1310 keepLooking = false;
fe750b86 1311 }
68a36223
MS
1312
1313 if (!keepLooking)
1314 g.interfaceNum = intfNumber;
1315 }
7594b224 1316 }
de3edeba 1317
68a36223
MS
1318 find_device_done:
1319 if (deviceIDString != NULL) CFRelease(deviceIDString);
1320 if (make != NULL) CFRelease(make);
1321 if (model != NULL) CFRelease(model);
1322 if (serial != NULL) CFRelease(serial);
1323 }
1324 else
1325 {
1326 keepLooking = (g.printer_obj == 0 && g.interfaceProtocol != kUSBPrintingProtocolIPP);
1327 if (obj == IO_OBJECT_NULL && keepLooking)
fe750b86 1328 {
68a36223
MS
1329 CFRunLoopTimerContext context = { 0, refcon, NULL, NULL, NULL };
1330 CFRunLoopTimerRef timer = CFRunLoopTimerCreate(NULL, CFAbsoluteTimeGetCurrent() + 1.0, 10, 0x0, 0x0, status_timer_cb, &context);
1331 if (timer != NULL)
1332 {
1333 CFRunLoopAddTimer(CFRunLoopGetCurrent(), timer, kCFRunLoopDefaultMode);
1334 g.status_timer = timer;
1335 }
fe750b86 1336 }
68a36223
MS
1337 }
1338
1339 if (!keepLooking && g.status_timer != NULL)
1340 {
1341 fputs("STATE: -offline-report\n", stderr);
1342 _cupsLangPrintFilter(stderr, "INFO", _("The printer is now online."));
1343 CFRunLoopRemoveTimer(CFRunLoopGetCurrent(), g.status_timer, kCFRunLoopDefaultMode);
1344 CFRelease(g.status_timer);
1345 g.status_timer = NULL;
1346 }
de3edeba 1347
68a36223 1348 return keepLooking;
fe750b86 1349}
09a101d6 1350
fe750b86
MS
1351static CFStringRef deviceIDCopySerialNumber(CFStringRef deviceID)
1352{
1353 CFStringRef serialKeys[] = { CFSTR("SN:"), CFSTR("SERN:"), NULL };
de3edeba 1354
fe750b86
MS
1355 return copy_value_for_key(deviceID, serialKeys);
1356}
ef416fc2 1357
fe750b86
MS
1358static CFStringRef deviceIDCopyModel(CFStringRef deviceID)
1359{
1360 CFStringRef modelKeys[] = { CFSTR("MDL:"), CFSTR("MODEL:"), NULL };
1361 return copy_value_for_key(deviceID, modelKeys);
ef416fc2 1362}
1363
fe750b86
MS
1364static CFStringRef deviceIDCopyManufacturer(CFStringRef deviceID)
1365{
1366 CFStringRef makeKeys[] = { CFSTR("MFG:"), CFSTR("MANUFACTURER:"), NULL };
1367 return copy_value_for_key(deviceID, makeKeys);
1368}
ef416fc2 1369
e53920b9 1370/*
09a101d6 1371 * 'status_timer_cb()' - Status timer callback.
ef416fc2 1372 */
e53920b9 1373
09a101d6 1374static void status_timer_cb(CFRunLoopTimerRef timer,
1375 void *info)
ef416fc2 1376{
321d8d57
MS
1377 (void)timer;
1378 (void)info;
1379
c5571a1d 1380 fputs("STATE: +offline-report\n", stderr);
f3c17241 1381 _cupsLangPrintFilter(stderr, "INFO", _("The printer is offline."));
0a682745
MS
1382
1383 if (getenv("CLASS") != NULL)
1384 {
1385 /*
1386 * If the CLASS environment variable is set, the job was submitted
1387 * to a class and not to a specific queue. In this case, we want
1388 * to abort immediately so that the job can be requeued on the next
1389 * available printer in the class.
1390 *
1391 * Sleep 5 seconds to keep the job from requeuing too rapidly...
1392 */
1393
1394 sleep(5);
1395
1396 exit(CUPS_BACKEND_FAILED);
1397 }
ef416fc2 1398}
1399
ef416fc2 1400
e53920b9 1401#pragma mark -
1402/*
1403 * 'load_classdriver()' - Load a classdriver.
1404 */
1405
09a101d6 1406static kern_return_t load_classdriver(CFStringRef driverPath,
321d8d57 1407 printer_interface_t interface,
09a101d6 1408 classdriver_t ***printerDriver)
e53920b9 1409{
bf3816c7
MS
1410 kern_return_t kr = kUSBPrinterClassDeviceNotOpen;
1411 classdriver_t **driver = NULL;
1412 CFStringRef bundle = driverPath ? driverPath : kUSBGenericTOPrinterClassDriver;
1413 char bundlestr[1024]; /* Bundle path */
bf3816c7
MS
1414 CFURLRef url; /* URL for driver */
1415 CFPlugInRef plugin = NULL; /* Plug-in address */
e53920b9 1416
bf3816c7
MS
1417
1418 CFStringGetCString(bundle, bundlestr, sizeof(bundlestr), kCFStringEncodingUTF8);
1419
1420 /*
1421 * Validate permissions for the class driver...
1422 */
1423
22c9029b
MS
1424 _cups_fc_result_t result = _cupsFileCheck(bundlestr,
1425 _CUPS_FILE_CHECK_DIRECTORY, 1,
0fa6c7fa 1426 Iterating ? NULL : _cupsFileCheckFilter, NULL);
22c9029b
MS
1427
1428 if (result && driverPath)
321d8d57 1429 return (load_classdriver(NULL, interface, printerDriver));
22c9029b
MS
1430 else if (result)
1431 return (kr);
bf3816c7
MS
1432
1433 /*
1434 * Try loading the class driver...
1435 */
1436
1437 url = CFURLCreateWithFileSystemPath(NULL, bundle, kCFURLPOSIXPathStyle, true);
e53920b9 1438
bf3816c7
MS
1439 if (url)
1440 {
1441 plugin = CFPlugInCreate(NULL, url);
1442 CFRelease(url);
1443 }
1444 else
1445 plugin = NULL;
e53920b9 1446
bf3816c7
MS
1447 if (plugin)
1448 {
1449 CFArrayRef factories = CFPlugInFindFactoriesForPlugInTypeInPlugIn(kUSBPrinterClassTypeID, plugin);
1450 if (factories != NULL && CFArrayGetCount(factories) > 0)
09a101d6 1451 {
bf3816c7
MS
1452 CFUUIDRef factoryID = CFArrayGetValueAtIndex(factories, 0);
1453 IUnknownVTbl **iunknown = CFPlugInInstanceCreate(NULL, factoryID, kUSBPrinterClassTypeID);
1454 if (iunknown != NULL)
09a101d6 1455 {
bf3816c7
MS
1456 kr = (*iunknown)->QueryInterface(iunknown, CFUUIDGetUUIDBytes(kUSBPrinterClassInterfaceID), (LPVOID *)&driver);
1457 if (kr == kIOReturnSuccess && driver != NULL)
09a101d6 1458 {
bf3816c7
MS
1459 classdriver_t **genericDriver = NULL;
1460 if (driverPath != NULL && CFStringCompare(driverPath, kUSBGenericTOPrinterClassDriver, 0) != kCFCompareEqualTo)
321d8d57 1461 kr = load_classdriver(NULL, interface, &genericDriver);
e53920b9 1462
bf3816c7
MS
1463 if (kr == kIOReturnSuccess)
1464 {
321d8d57 1465 (*driver)->interface = interface;
bf3816c7 1466 (*driver)->Initialize(driver, genericDriver);
e53920b9 1467
bf3816c7 1468 (*driver)->plugin = plugin;
321d8d57 1469 (*driver)->interface = interface;
bf3816c7 1470 *printerDriver = driver;
e53920b9 1471 }
e53920b9 1472 }
bf3816c7 1473 (*iunknown)->Release(iunknown);
e53920b9 1474 }
bf3816c7 1475 CFRelease(factories);
ef416fc2 1476 }
e53920b9 1477 }
1478
c0e1af83 1479 fprintf(stderr, "DEBUG: load_classdriver(%s) (kr:0x%08x)\n", bundlestr, (int)kr);
ef416fc2 1480
bf3816c7 1481 return (kr);
ef416fc2 1482}
1483
1484
e53920b9 1485/*
1486 * 'unload_classdriver()' - Unload a classdriver.
1487 */
ef416fc2 1488
09a101d6 1489static kern_return_t unload_classdriver(classdriver_t ***classdriver)
e53920b9 1490{
09a101d6 1491 if (*classdriver != NULL)
1492 {
1493 (**classdriver)->Release(*classdriver);
1494 *classdriver = NULL;
e53920b9 1495 }
ef416fc2 1496
e53920b9 1497 return kIOReturnSuccess;
1498}
ef416fc2 1499
ef416fc2 1500
e53920b9 1501/*
09a101d6 1502 * 'load_printerdriver()' - Load vendor's classdriver.
411affcf 1503 *
1504 * If driverBundlePath is not NULL on return it is the callers responsbility to release it!
e53920b9 1505 */
ef416fc2 1506
09a101d6 1507static kern_return_t load_printerdriver(CFStringRef *driverBundlePath)
ef416fc2 1508{
7594b224 1509 IOCFPlugInInterface **iodev = NULL;
1510 SInt32 score;
1511 kern_return_t kr;
321d8d57 1512 printer_interface_t interface;
7594b224 1513 HRESULT res;
1514
09a101d6 1515 kr = IOCreatePlugInInterfaceForService(g.printer_obj, kIOUSBInterfaceUserClientTypeID, kIOCFPlugInInterfaceID, &iodev, &score);
7594b224 1516 if (kr == kIOReturnSuccess)
1517 {
321d8d57 1518 if ((res = (*iodev)->QueryInterface(iodev, USB_INTERFACE_KIND, (LPVOID *) &interface)) == noErr)
7594b224 1519 {
09a101d6 1520 *driverBundlePath = IORegistryEntryCreateCFProperty(g.printer_obj, kUSBClassDriverProperty, NULL, kNilOptions);
e53920b9 1521
7530b13a 1522 g.use_generic_class_driver = (*driverBundlePath == NULL || (CFStringCompare(*driverBundlePath, kUSBGenericTOPrinterClassDriver, 0x0) == kCFCompareEqualTo));
321d8d57 1523 kr = load_classdriver(*driverBundlePath, interface, &g.classdriver);
ef416fc2 1524
e53920b9 1525 if (kr != kIOReturnSuccess)
321d8d57 1526 (*interface)->Release(interface);
e53920b9 1527 }
1528 IODestroyPlugInInterface(iodev);
1529 }
1530 return kr;
ef416fc2 1531}
1532
f1ebe30e 1533static printer_interface_t usb_printer_interface_interface(io_service_t usbClass)
fe750b86 1534{
f1ebe30e 1535 printer_interface_t intf = NULL;
fe750b86
MS
1536 IOCFPlugInInterface **plugin = NULL;
1537 SInt32 score;
1538 int kr = IOCreatePlugInInterfaceForService(usbClass, kIOUSBInterfaceUserClientTypeID, kIOCFPlugInInterfaceID, &plugin, &score);
1539 if (kr == kIOReturnSuccess)
1540 {
f1ebe30e 1541 (*plugin)->QueryInterface(plugin, USB_INTERFACE_KIND, (LPVOID *)&intf);
fe750b86
MS
1542 IODestroyPlugInInterface(plugin);
1543 }
1544
1545 return intf;
1546}
1547
f1ebe30e 1548static CFStringRef copy_printer_interface_deviceid(printer_interface_t printer, UInt8 alternateSetting)
fe750b86
MS
1549{
1550 // I have tried to make this function as neat as I can, but the possibility of needing to resend
1551 // a request to get the entire string makes it hideous...
1552 //
1553 // We package the job of sending a request up into the block (^sendRequest), which takes the size
1554 // it should allocate for the message buffer. It frees the current buffer if one is set and
1555 // allocates one of the specified size, then performs the request. We can then easily retry by
1556 // calling the block again if we fail to get the whole string the first time around.
1557
1558 #define kUSBPrintClassGetDeviceID 0
1559 #define kDefaultNoDataTimeout 5000L
1560 #define pack_device_id_wIndex(intf, alt) ((UInt16)((((UInt16)(intf)) << 8) | ((UInt8)(alt))))
1561
de3edeba
MS
1562 if (printer == NULL)
1563 return NULL;
fe750b86
MS
1564
1565
1566 IOReturn err = kIOReturnError;
1567 UInt8 configurationIndex = 0;
1568 UInt8 interfaceNumber = 0;
1569 size_t bufferLength = 256;
1570 CFStringRef ret = NULL;
1571
1572 if ((*printer)->GetConfigurationValue( printer, &configurationIndex) == kIOReturnSuccess &&
1573 (*printer)->GetInterfaceNumber( printer, &interfaceNumber) == kIOReturnSuccess)
1574 {
1575 __block IOUSBDevRequestTO request;
1576 IOReturn (^sendRequest)(size_t) = ^ (size_t size)
1577 {
1578 if (request.pData)
1579 {
1580 free(request.pData);
1581 request.wLength = 0;
1582 request.pData = NULL;
1583 }
1584
f8f7636e 1585 IOReturn berr = kIOReturnError;
fe750b86
MS
1586 char *buffer = malloc(size);
1587 if (buffer == NULL)
1588 return kIOReturnNoMemory;
1589
1590 request.wLength = HostToUSBWord(size);
1591 request.pData = buffer;
f8f7636e
MS
1592 berr = (*printer)->ControlRequestTO(printer, (UInt8)0, &request);
1593 return berr;
fe750b86
MS
1594 };
1595
1596 /* This request takes the 0 based configuration index. IOKit returns a 1 based configuration index */
1597 configurationIndex -= 1;
1598
1599 bzero(&request, sizeof(request));
1600
1601 request.bmRequestType = USBmakebmRequestType(kUSBIn, kUSBClass, kUSBInterface);
1602 request.bRequest = kUSBPrintClassGetDeviceID;
1603 request.wValue = HostToUSBWord(configurationIndex);
1604 request.wIndex = HostToUSBWord(pack_device_id_wIndex(interfaceNumber, alternateSetting));
1605 request.noDataTimeout = kDefaultNoDataTimeout;
1606 request.completionTimeout = 0; // Copying behavior from Generic Class Driver
1607
1608 err = sendRequest(bufferLength);
1609
1610 if (err == kIOReturnSuccess && request.wLenDone > 1)
1611 {
1612 UInt16 actualLength = OSSwapBigToHostInt16(*((UInt16 *)request.pData));
1613
1614 if (actualLength > 2 && actualLength <= bufferLength - 2)
1615 {
1616 ret = CFStringCreateWithBytes(NULL, (const UInt8 *) &request.pData[2], actualLength - 2, kCFStringEncodingUTF8, false);
1617 }
fd5ce3a4 1618 else if (actualLength > 2) {
fe750b86
MS
1619 err = sendRequest(actualLength);
1620 if (err == kIOReturnSuccess && request.wLenDone > 0)
1621 {
1622 actualLength = OSSwapBigToHostInt16(*((UInt16 *)request.pData));
1623 ret = CFStringCreateWithBytes(NULL, (const UInt8 *) &request.pData[2], actualLength - 2, kCFStringEncodingUTF8, false);
1624 }
1625 }
1626 }
1627
1628 if (request.pData)
1629 free(request.pData);
1630 }
1631
1632 CFStringRef manufacturer = deviceIDCopyManufacturer(ret);
de3edeba 1633 CFStringRef model = deviceIDCopyModel(ret);
fe750b86
MS
1634 CFStringRef serial = deviceIDCopySerialNumber(ret);
1635
1636 if (manufacturer == NULL || serial == NULL || model == NULL)
1637 {
1638 IOUSBDevRequestTO request;
1639 IOUSBDeviceDescriptor desc;
1640
1641 bzero(&request, sizeof(request));
1642
1643 request.bmRequestType = USBmakebmRequestType( kUSBIn, kUSBStandard, kUSBDevice );
1644 request.bRequest = kUSBRqGetDescriptor;
1645 request.wValue = kUSBDeviceDesc << 8;
1646 request.wIndex = 0;
1647 request.wLength = sizeof(desc);
1648 request.pData = &desc;
1649 request.completionTimeout = 0;
1650 request.noDataTimeout = 60L;
1651
1652 err = (*printer)->ControlRequestTO(printer, 0, &request);
1653 if (err == kIOReturnSuccess)
1654 {
1655 CFMutableStringRef extras = CFStringCreateMutable(NULL, 0);
1656 if (manufacturer == NULL)
1657 {
de3edeba
MS
1658 manufacturer = copy_printer_interface_indexed_description(printer, desc.iManufacturer, kUSBLanguageEnglish);
1659 if (manufacturer && CFStringGetLength(manufacturer) > 0)
fe750b86
MS
1660 CFStringAppendFormat(extras, NULL, CFSTR("MFG:%@;"), manufacturer);
1661 }
1662
1663 if (model == NULL)
1664 {
de3edeba
MS
1665 model = copy_printer_interface_indexed_description(printer, desc.iProduct, kUSBLanguageEnglish);
1666 if (model && CFStringGetLength(model) > 0)
1667 CFStringAppendFormat(extras, NULL, CFSTR("MDL:%@;"), model);
fe750b86
MS
1668 }
1669
1670 if (serial == NULL && desc.iSerialNumber != 0)
1671 {
de3edeba
MS
1672 serial = copy_printer_interface_indexed_description(printer, desc.iSerialNumber, kUSBLanguageEnglish);
1673 if (serial && CFStringGetLength(serial) > 0)
fe750b86
MS
1674 CFStringAppendFormat(extras, NULL, CFSTR("SERN:%@;"), serial);
1675 }
1676
1677 if (ret != NULL)
1678 {
1679 CFStringAppend(extras, ret);
1680 CFRelease(ret);
1681
1682 ret = extras;
1683 }
1684 else
1685 {
1686 ret = extras;
1687 }
1688 }
1689 }
1690
de3edeba
MS
1691 if (ret != NULL)
1692 {
fe750b86
MS
1693 /* Remove special characters from the serial number */
1694 CFRange range = (serial != NULL ? CFStringFind(serial, CFSTR("+"), 0) : CFRangeMake(0, 0));
1695 if (range.length == 1)
1696 {
1697 range = CFStringFind(ret, serial, 0);
1698
1699 CFMutableStringRef deviceIDString = CFStringCreateMutableCopy(NULL, 0, ret);
1700 CFRelease(ret);
1701
1702 ret = deviceIDString;
1703 CFStringFindAndReplace(deviceIDString, CFSTR("+"), CFSTR(""), range, 0);
1704 }
de3edeba
MS
1705 }
1706
1707 if (manufacturer != NULL)
1708 CFRelease(manufacturer);
7530b13a 1709
de3edeba
MS
1710 if (model != NULL)
1711 CFRelease(model);
7530b13a 1712
de3edeba
MS
1713 if (serial != NULL)
1714 CFRelease(serial);
fe750b86 1715
de3edeba
MS
1716 if (ret != NULL && CFStringGetLength(ret) == 0)
1717 {
1718 CFRelease(ret);
1719 return NULL;
1720 }
7530b13a 1721
de3edeba 1722 return ret;
fe750b86
MS
1723}
1724
f1ebe30e 1725static CFStringRef copy_printer_interface_indexed_description(printer_interface_t printer, UInt8 index, UInt16 language)
fe750b86
MS
1726{
1727 IOReturn err;
1728 UInt8 description[256]; // Max possible descriptor length
1729 IOUSBDevRequestTO request;
1730
1731 bzero(description, 2);
1732
1733 request.bmRequestType = USBmakebmRequestType(kUSBIn, kUSBStandard, kUSBDevice);
1734 request.bRequest = kUSBRqGetDescriptor;
1735 request.wValue = (kUSBStringDesc << 8) | index;
1736 request.wIndex = language;
1737 request.wLength = 2;
1738 request.pData = &description;
1739 request.completionTimeout = 0;
1740 request.noDataTimeout = 60L;
1741
1742 err = (*printer)->ControlRequestTO(printer, 0, &request);
1743 if (err != kIOReturnSuccess && err != kIOReturnOverrun)
1744 {
1745 bzero(description, request.wLength);
1746
1747 // Let's try again full length. Here's why:
1748 // On USB 2.0 controllers, we will not get an overrun error. We just get a "babble" error
1749 // and no valid data. So, if we ask for the max size, we will either get it, or we'll get an underrun.
1750 // It looks like we get it w/out an underrun
1751
1752 request.bmRequestType = USBmakebmRequestType(kUSBIn, kUSBStandard, kUSBDevice);
1753 request.bRequest = kUSBRqGetDescriptor;
1754 request.wValue = (kUSBStringDesc << 8) | index;
1755 request.wIndex = language;
1756 request.wLength = sizeof description;
1757 request.pData = &description;
1758 request.completionTimeout = 0;
1759 request.noDataTimeout = 60L;
1760
1761 err = (*printer)->ControlRequestTO(printer, 0, &request);
1762 if (err != kIOReturnSuccess && err != kIOReturnUnderrun)
de3edeba 1763 return NULL;
fe750b86
MS
1764 }
1765
1766 unsigned int length = description[0];
1767 if (length == 0)
1768 return CFStringCreateWithCString(NULL, "", kCFStringEncodingUTF8);
1769
1770 if (description[1] != kUSBStringDesc)
1771 return NULL;
1772
1773 request.bmRequestType = USBmakebmRequestType(kUSBIn, kUSBStandard, kUSBDevice);
1774 request.bRequest = kUSBRqGetDescriptor;
1775 request.wValue = (kUSBStringDesc << 8) | index;
1776 request.wIndex = language;
1777
1778 bzero(description, length);
f8f7636e 1779 request.wLength = (UInt16)length;
fe750b86
MS
1780 request.pData = &description;
1781 request.completionTimeout = 0;
1782 request.noDataTimeout = 60L;
1783
1784 err = (*printer)->ControlRequestTO(printer, 0, &request);
1785 if (err != kIOReturnSuccess)
de3edeba 1786 return NULL;
fe750b86
MS
1787
1788 if (description[1] != kUSBStringDesc)
de3edeba 1789 return NULL;
fe750b86
MS
1790
1791 if ((description[0] & 1) != 0)
1792 description[0] &= 0xfe;
1793
1794 char buffer[258] = {};
1795 unsigned int maxLength = sizeof buffer;
1796 if (description[0] > 1)
1797 {
1798 length = (description[0]-2)/2;
1799
1800 if (length > maxLength - 1)
1801 length = maxLength -1;
1802
f8f7636e 1803 for (unsigned i = 0; i < length; i++)
fe750b86
MS
1804 buffer[i] = (char) description[2*i+2];
1805
1806 buffer[length] = 0;
1807 }
1808
1809 return CFStringCreateWithCString(NULL, buffer, kCFStringEncodingUTF8);
1810}
ef416fc2 1811
e53920b9 1812/*
1813 * 'registry_open()' - Open a connection to the printer.
1814 */
ef416fc2 1815
09a101d6 1816static kern_return_t registry_open(CFStringRef *driverBundlePath)
e53920b9 1817{
09a101d6 1818 g.bidi_flag = 0; /* 0=unidirectional */
f7deaa1a 1819
09a101d6 1820 kern_return_t kr = load_printerdriver(driverBundlePath);
1821 if (kr != kIOReturnSuccess)
e53920b9 1822 kr = -2;
ef416fc2 1823
09a101d6 1824 if (g.classdriver != NULL)
1825 {
bc44d920 1826 (*g.classdriver)->interfaceNumber = g.interfaceNum;
09a101d6 1827 kr = (*g.classdriver)->Open(g.classdriver, g.location, kUSBPrintingProtocolBidirectional);
1828 if (kr != kIOReturnSuccess || (*g.classdriver)->interface == NULL)
1829 {
1830 kr = (*g.classdriver)->Open(g.classdriver, g.location, kUSBPrintingProtocolUnidirectional);
1831 if (kr == kIOReturnSuccess)
1832 {
1833 if ((*g.classdriver)->interface == NULL)
1834 {
1835 (*g.classdriver)->Close(g.classdriver);
e53920b9 1836 kr = -1;
1837 }
1838 }
1839 }
09a101d6 1840 else
1841 g.bidi_flag = 1; /* 1=bidirectional */
e53920b9 1842 }
ef416fc2 1843
09a101d6 1844 if (kr != kIOReturnSuccess)
1845 unload_classdriver(&g.classdriver);
ef416fc2 1846
e53920b9 1847 return kr;
1848}
ef416fc2 1849
ef416fc2 1850
e53920b9 1851/*
1852 * 'registry_close()' - Close the connection to the printer.
1853 */
ef416fc2 1854
eac3a0a0 1855static kern_return_t registry_close(void)
ef416fc2 1856{
09a101d6 1857 if (g.classdriver != NULL)
1858 (*g.classdriver)->Close(g.classdriver);
1859
1860 unload_classdriver(&g.classdriver);
e53920b9 1861 return kIOReturnSuccess;
ef416fc2 1862}
1863
e53920b9 1864#pragma mark -
1865/*
1866 * 'copy_value_for_key()' - Copy value string associated with a key.
1867 */
ef416fc2 1868
09a101d6 1869static CFStringRef copy_value_for_key(CFStringRef deviceID,
1870 CFStringRef *keys)
ef416fc2 1871{
e53920b9 1872 CFStringRef value = NULL;
1873 CFArrayRef kvPairs = deviceID != NULL ? CFStringCreateArrayBySeparatingStrings(NULL, deviceID, CFSTR(";")) : NULL;
1874 CFIndex max = kvPairs != NULL ? CFArrayGetCount(kvPairs) : 0;
1875 CFIndex idx = 0;
1876
09a101d6 1877 while (idx < max && value == NULL)
1878 {
e53920b9 1879 CFStringRef kvpair = CFArrayGetValueAtIndex(kvPairs, idx);
1880 CFIndex idxx = 0;
09a101d6 1881 while (keys[idxx] != NULL && value == NULL)
1882 {
e53920b9 1883 CFRange range = CFStringFind(kvpair, keys[idxx], kCFCompareCaseInsensitive);
09a101d6 1884 if (range.length != -1)
1885 {
1886 if (range.location != 0)
1887 {
e53920b9 1888 CFMutableStringRef theString = CFStringCreateMutableCopy(NULL, 0, kvpair);
1889 CFStringTrimWhitespace(theString);
1890 range = CFStringFind(theString, keys[idxx], kCFCompareCaseInsensitive);
09a101d6 1891 if (range.location == 0)
e53920b9 1892 value = CFStringCreateWithSubstring(NULL, theString, CFRangeMake(range.length, CFStringGetLength(theString) - range.length));
09a101d6 1893
e53920b9 1894 CFRelease(theString);
1895 }
09a101d6 1896 else
1897 {
e53920b9 1898 CFStringRef theString = CFStringCreateWithSubstring(NULL, kvpair, CFRangeMake(range.length, CFStringGetLength(kvpair) - range.length));
1899 CFMutableStringRef theString2 = CFStringCreateMutableCopy(NULL, 0, theString);
1900 CFRelease(theString);
ef416fc2 1901
e53920b9 1902 CFStringTrimWhitespace(theString2);
1903 value = theString2;
ef416fc2 1904 }
e53920b9 1905 }
1906 idxx++;
ef416fc2 1907 }
e53920b9 1908 idx++;
1909 }
1910
1911 if (kvPairs != NULL)
09a101d6 1912 CFRelease(kvPairs);
e53920b9 1913 return value;
ef416fc2 1914}
1915
1916
411affcf 1917/*
09a101d6 1918 * 'cfstr_create_trim()' - Create CFString and trim whitespace characters.
411affcf 1919 */
1920
09a101d6 1921CFStringRef cfstr_create_trim(const char *cstr)
411affcf 1922{
1923 CFStringRef cfstr;
1924 CFMutableStringRef cfmutablestr = NULL;
09a101d6 1925
411affcf 1926 if ((cfstr = CFStringCreateWithCString(NULL, cstr, kCFStringEncodingUTF8)) != NULL)
1927 {
1928 if ((cfmutablestr = CFStringCreateMutableCopy(NULL, 1024, cfstr)) != NULL)
1929 CFStringTrimWhitespace(cfmutablestr);
1930
1931 CFRelease(cfstr);
1932 }
1933 return (CFStringRef) cfmutablestr;
1934}
1935
1936
e53920b9 1937#pragma mark -
1938/*
db1f069b 1939 * 'parse_options()' - Parse URI options.
e53920b9 1940 */
ef416fc2 1941
db1f069b 1942static void parse_options(char *options,
09a101d6 1943 char *serial,
76cd9e37 1944 int serial_size,
09a101d6 1945 UInt32 *location,
1946 Boolean *wait_eof)
e53920b9 1947{
db1f069b
MS
1948 char sep, /* Separator character */
1949 *name, /* Name of option */
1950 *value; /* Value of option */
1951
ef416fc2 1952
e53920b9 1953 if (serial)
1954 *serial = '\0';
1955 if (location)
1956 *location = 0;
ef416fc2 1957
e53920b9 1958 if (!options)
1959 return;
ef416fc2 1960
db1f069b 1961 while (*options)
09a101d6 1962 {
db1f069b
MS
1963 /*
1964 * Get the name...
1965 */
ef416fc2 1966
db1f069b 1967 name = options;
ef416fc2 1968
db1f069b 1969 while (*options && *options != '=' && *options != '+' && *options != '&')
e53920b9 1970 options ++;
ef416fc2 1971
db1f069b
MS
1972 if ((sep = *options) != '\0')
1973 *options++ = '\0';
ef416fc2 1974
db1f069b
MS
1975 if (sep == '=')
1976 {
1977 /*
1978 * Get the value...
1979 */
ef416fc2 1980
db1f069b
MS
1981 value = options;
1982
1983 while (*options && *options != '+' && *options != '&')
e53920b9 1984 options ++;
db1f069b
MS
1985
1986 if (*options)
1987 *options++ = '\0';
e53920b9 1988 }
db1f069b
MS
1989 else
1990 value = (char *)"";
ef416fc2 1991
db1f069b
MS
1992 /*
1993 * Process the option...
1994 */
1995
88f9aafc 1996 if (!_cups_strcasecmp(name, "waiteof"))
09a101d6 1997 {
88f9aafc
MS
1998 if (!_cups_strcasecmp(value, "on") ||
1999 !_cups_strcasecmp(value, "yes") ||
2000 !_cups_strcasecmp(value, "true"))
09a101d6 2001 *wait_eof = true;
88f9aafc
MS
2002 else if (!_cups_strcasecmp(value, "off") ||
2003 !_cups_strcasecmp(value, "no") ||
2004 !_cups_strcasecmp(value, "false"))
09a101d6 2005 *wait_eof = false;
2006 else
0837b7e8
MS
2007 _cupsLangPrintFilter(stderr, "WARNING",
2008 _("Boolean expected for waiteof option \"%s\"."),
2009 value);
e53920b9 2010 }
88f9aafc 2011 else if (!_cups_strcasecmp(name, "serial"))
76cd9e37 2012 strlcpy(serial, value, serial_size);
88f9aafc 2013 else if (!_cups_strcasecmp(name, "location") && location)
7e86f2f6 2014 *location = (UInt32)strtoul(value, NULL, 16);
e53920b9 2015 }
e53920b9 2016}
ef416fc2 2017
ef416fc2 2018
e53920b9 2019/*!
2020 * @function setup_cfLanguage
ac884b6a 2021 * @abstract Convert the contents of the CUPS 'APPLE_LANGUAGE' environment
e53920b9 2022 * variable into a one element CF array of languages.
2023 *
2024 * @discussion Each submitted job comes with a natural language. CUPS passes
2025 * that language in an environment variable. We take that language
2026 * and jam it into the AppleLanguages array so that CF will use
2027 * it when reading localized resources. We need to do this before
2028 * any CF code reads and caches the languages array, so this function
2029 * should be called early in main()
2030 */
2031static void setup_cfLanguage(void)
2032{
2033 CFStringRef lang[1] = {NULL};
2034 CFArrayRef langArray = NULL;
2035 const char *requestedLang = NULL;
2036
ac884b6a
MS
2037 if ((requestedLang = getenv("APPLE_LANGUAGE")) == NULL)
2038 requestedLang = getenv("LANG");
2039
09a101d6 2040 if (requestedLang != NULL)
2041 {
e53920b9 2042 lang[0] = CFStringCreateWithCString(kCFAllocatorDefault, requestedLang, kCFStringEncodingUTF8);
2043 langArray = CFArrayCreate(kCFAllocatorDefault, (const void **)lang, sizeof(lang) / sizeof(lang[0]), &kCFTypeArrayCallBacks);
2044
28b9d139 2045 CFPreferencesSetValue(CFSTR("AppleLanguages"), langArray, kCFPreferencesCurrentApplication, kCFPreferencesAnyUser, kCFPreferencesAnyHost);
005dd1eb 2046 fprintf(stderr, "DEBUG: usb: AppleLanguages=\"%s\"\n", requestedLang);
e53920b9 2047
2048 CFRelease(lang[0]);
2049 CFRelease(langArray);
e53920b9 2050 }
09a101d6 2051 else
ac884b6a 2052 fputs("DEBUG: usb: LANG and APPLE_LANGUAGE environment variables missing.\n", stderr);
e53920b9 2053}
ef416fc2 2054
e53920b9 2055#pragma mark -
749b1e90 2056#if defined(__i386__) || defined(__x86_64__)
e53920b9 2057/*!
749b1e90 2058 * @function run_legacy_backend
e53920b9 2059 *
749b1e90 2060 * @abstract Starts child backend process running as a ppc or i386 executable.
e53920b9 2061 *
2062 * @result Never returns; always calls exit().
2063 *
09a101d6 2064 * @discussion
e53920b9 2065 */
749b1e90
MS
2066static void run_legacy_backend(int argc,
2067 char *argv[],
2068 int fd)
e53920b9 2069{
2070 int i;
2071 int exitstatus = 0;
2072 int childstatus;
2073 pid_t waitpid_status;
2074 char *my_argv[32];
749b1e90 2075 char *usb_legacy_status;
ef416fc2 2076
7e86f2f6 2077
749b1e90
MS
2078 /*
2079 * If we're running as x86_64 or i386 and couldn't load the class driver
2080 * (because it's ppc or i386), then try to re-exec ourselves in ppc or i386
2081 * mode to try again. If we don't have a ppc or i386 architecture we may be
2082 * running with the same architecture again so guard against this by setting
2083 * and testing an environment variable...
2084 */
e53920b9 2085
749b1e90
MS
2086# ifdef __x86_64__
2087 usb_legacy_status = getenv("USB_I386_STATUS");
2088# else
2089 usb_legacy_status = getenv("USB_PPC_STATUS");
2090# endif /* __x86_64__ */
2091
2092 if (!usb_legacy_status)
09a101d6 2093 {
bc44d920 2094 /*
2095 * Setup a SIGTERM handler then block it before forking...
2096 */
ef416fc2 2097
ef55b745 2098 int err; /* posix_spawn result */
bc44d920 2099 struct sigaction action; /* POSIX signal action */
2100 sigset_t newmask, /* New signal mask */
2101 oldmask; /* Old signal mask */
58dc1933
MS
2102 char usbpath[1024]; /* Path to USB backend */
2103 const char *cups_serverbin;/* Path to CUPS binaries */
2104
bc44d920 2105
2106 memset(&action, 0, sizeof(action));
2107 sigaddset(&action.sa_mask, SIGTERM);
2108 action.sa_handler = sigterm_handler;
2109 sigaction(SIGTERM, &action, NULL);
2110
2111 sigemptyset(&newmask);
2112 sigaddset(&newmask, SIGTERM);
2113 sigprocmask(SIG_BLOCK, &newmask, &oldmask);
ef416fc2 2114
749b1e90
MS
2115 /*
2116 * Set the environment variable...
2117 */
bc44d920 2118
749b1e90
MS
2119# ifdef __x86_64__
2120 setenv("USB_I386_STATUS", "1", false);
2121# else
2122 setenv("USB_PPC_STATUS", "1", false);
2123# endif /* __x86_64__ */
bc44d920 2124
749b1e90
MS
2125 /*
2126 * Tell the kernel to use the specified CPU architecture...
2127 */
bc44d920 2128
749b1e90
MS
2129# ifdef __x86_64__
2130 cpu_type_t cpu = CPU_TYPE_I386;
2131# else
2132 cpu_type_t cpu = CPU_TYPE_POWERPC;
2133# endif /* __x86_64__ */
9a4f8274 2134 size_t ocount = 1;
749b1e90 2135 posix_spawnattr_t attrs;
ef416fc2 2136
749b1e90
MS
2137 if (!posix_spawnattr_init(&attrs))
2138 {
b19ccc9e
MS
2139 posix_spawnattr_setsigdefault(&attrs, &oldmask);
2140 if (posix_spawnattr_setbinpref_np(&attrs, 1, &cpu, &ocount) || ocount != 1)
9a4f8274
MS
2141 {
2142# ifdef __x86_64__
2143 perror("DEBUG: Unable to set binary preference to i386");
2144# else
2145 perror("DEBUG: Unable to set binary preference to ppc");
2146# endif /* __x86_64__ */
0837b7e8
MS
2147 _cupsLangPrintFilter(stderr, "ERROR",
2148 _("Unable to use legacy USB class driver."));
9a4f8274
MS
2149 exit(CUPS_BACKEND_STOP);
2150 }
749b1e90 2151 }
bc44d920 2152
749b1e90
MS
2153 /*
2154 * Set up the arguments and call posix_spawn...
2155 */
ef416fc2 2156
58dc1933
MS
2157 if ((cups_serverbin = getenv("CUPS_SERVERBIN")) == NULL)
2158 cups_serverbin = CUPS_SERVERBIN;
2159 snprintf(usbpath, sizeof(usbpath), "%s/backend/usb", cups_serverbin);
2160
7e86f2f6 2161 for (i = 0; i < argc && i < (int)(sizeof(my_argv) / sizeof(my_argv[0])) - 1; i ++)
749b1e90 2162 my_argv[i] = argv[i];
ef416fc2 2163
749b1e90 2164 my_argv[i] = NULL;
ef416fc2 2165
ef55b745
MS
2166 if ((err = posix_spawn(&child_pid, usbpath, NULL, &attrs, my_argv,
2167 environ)) != 0)
09a101d6 2168 {
58dc1933 2169 fprintf(stderr, "DEBUG: Unable to exec %s: %s\n", usbpath,
ef55b745 2170 strerror(err));
0837b7e8
MS
2171 _cupsLangPrintFilter(stderr, "ERROR",
2172 _("Unable to use legacy USB class driver."));
749b1e90 2173 exit(CUPS_BACKEND_STOP);
bc44d920 2174 }
e53920b9 2175
bc44d920 2176 /*
2177 * Unblock signals...
2178 */
e53920b9 2179
bc44d920 2180 sigprocmask(SIG_SETMASK, &oldmask, NULL);
e53920b9 2181
bc44d920 2182 /*
2183 * Close the fds we won't be using then wait for the child backend to exit.
2184 */
2185
2186 close(fd);
2187 close(1);
2188
005dd1eb
MS
2189 fprintf(stderr, "DEBUG: Started usb(legacy) backend (PID %d)\n",
2190 (int)child_pid);
bc44d920 2191
2192 while ((waitpid_status = waitpid(child_pid, &childstatus, 0)) == (pid_t)-1 && errno == EINTR)
2193 usleep(1000);
2194
2195 if (WIFSIGNALED(childstatus))
2196 {
749b1e90 2197 exitstatus = CUPS_BACKEND_STOP;
4d301e69 2198 fprintf(stderr, "DEBUG: usb(legacy) backend %d crashed on signal %d\n",
749b1e90 2199 child_pid, WTERMSIG(childstatus));
ef416fc2 2200 }
09a101d6 2201 else
2202 {
bc44d920 2203 if ((exitstatus = WEXITSTATUS(childstatus)) != 0)
005dd1eb 2204 fprintf(stderr,
4d301e69 2205 "DEBUG: usb(legacy) backend %d stopped with status %d\n",
005dd1eb 2206 child_pid, exitstatus);
bc44d920 2207 else
005dd1eb
MS
2208 fprintf(stderr, "DEBUG: usb(legacy) backend %d exited with no errors\n",
2209 child_pid);
e53920b9 2210 }
2211 }
09a101d6 2212 else
2213 {
005dd1eb 2214 fputs("DEBUG: usb(legacy) backend running native again\n", stderr);
749b1e90 2215 exitstatus = CUPS_BACKEND_STOP;
e53920b9 2216 }
ef416fc2 2217
e53920b9 2218 exit(exitstatus);
2219}
6c48a6ca
MS
2220#endif /* __i386__ || __x86_64__ */
2221
ef416fc2 2222
e53920b9 2223/*
2224 * 'sigterm_handler()' - SIGTERM handler.
2225 */
ef416fc2 2226
f11a948a
MS
2227static void
2228sigterm_handler(int sig) /* I - Signal */
ef416fc2 2229{
6c48a6ca
MS
2230#if defined(__i386__) || defined(__x86_64__)
2231 /*
2232 * If we started a child process pass the signal on to it...
2233 */
2234
e53920b9 2235 if (child_pid)
f11a948a
MS
2236 {
2237 /*
2238 * If we started a child process pass the signal on to it...
2239 */
e53920b9 2240
f11a948a
MS
2241 int status;
2242
2243 kill(child_pid, sig);
2244 while (waitpid(child_pid, &status, 0) < 0 && errno == EINTR);
ef416fc2 2245
f11a948a 2246 if (WIFEXITED(status))
c1de66f9 2247 _exit(WEXITSTATUS(status));
f11a948a 2248 else if (status == SIGTERM || status == SIGKILL)
c1de66f9 2249 _exit(0);
f11a948a
MS
2250 else
2251 {
c1de66f9
MS
2252 write(2, "DEBUG: Child crashed.\n", 22);
2253 _exit(CUPS_BACKEND_STOP);
f11a948a
MS
2254 }
2255 }
18ecb428 2256#endif /* __i386__ || __x86_64__ */
6c48a6ca 2257}
18ecb428 2258
ef416fc2 2259
a469f8a5
MS
2260/*
2261 * 'sigquit_handler()' - SIGQUIT handler.
2262 */
2263
2264static void sigquit_handler(int sig, siginfo_t *si, void *unused)
2265{
2266 char *path;
2267 char pathbuf[PROC_PIDPATHINFO_MAXSIZE];
2268 static char msgbuf[256] = "";
2269
2270
2271 (void)sig;
2272 (void)unused;
2273
2274 if (proc_pidpath(si->si_pid, pathbuf, sizeof(pathbuf)) > 0 &&
2275 (path = basename(pathbuf)) != NULL)
2276 snprintf(msgbuf, sizeof(msgbuf), "SIGQUIT sent by %s(%d)", path, (int)si->si_pid);
2277 else
2278 snprintf(msgbuf, sizeof(msgbuf), "SIGQUIT sent by PID %d", (int)si->si_pid);
2279
2280 CRSetCrashLogMessage(msgbuf);
2281
2282 abort();
2283}
2284
2285
e53920b9 2286#ifdef PARSE_PS_ERRORS
2287/*
2288 * 'next_line()' - Find the next line in a buffer.
2289 */
ef416fc2 2290
e53920b9 2291static const char *next_line (const char *buffer)
ef416fc2 2292{
e53920b9 2293 const char *cptr, *lptr = NULL;
ef416fc2 2294
e53920b9 2295 for (cptr = buffer; *cptr && lptr == NULL; cptr++)
2296 if (*cptr == '\n' || *cptr == '\r')
2297 lptr = cptr;
2298 return lptr;
ef416fc2 2299}
2300
ef416fc2 2301
e53920b9 2302/*
2303 * 'parse_pserror()' - Scan the backchannel data for postscript errors.
2304 */
ef416fc2 2305
09a101d6 2306static void parse_pserror(char *sockBuffer,
2307 int len)
ef416fc2 2308{
e53920b9 2309 static char gErrorBuffer[1024] = "";
2310 static char *gErrorBufferPtr = gErrorBuffer;
2311 static char *gErrorBufferEndPtr = gErrorBuffer + sizeof(gErrorBuffer);
2312
2313 char *pCommentBegin, *pCommentEnd, *pLineEnd;
2314 char *logLevel;
2315 char logstr[1024];
2316 int logstrlen;
2317
2318 if (gErrorBufferPtr + len > gErrorBufferEndPtr - 1)
2319 gErrorBufferPtr = gErrorBuffer;
2320 if (len > sizeof(gErrorBuffer) - 1)
2321 len = sizeof(gErrorBuffer) - 1;
2322
2323 memcpy(gErrorBufferPtr, (const void *)sockBuffer, len);
2324 gErrorBufferPtr += len;
2325 *(gErrorBufferPtr + 1) = '\0';
2326
e53920b9 2327 pLineEnd = (char *)next_line((const char *)gErrorBuffer);
09a101d6 2328 while (pLineEnd != NULL)
2329 {
e53920b9 2330 *pLineEnd++ = '\0';
2331
2332 pCommentBegin = strstr(gErrorBuffer,"%%[");
2333 pCommentEnd = strstr(gErrorBuffer, "]%%");
09a101d6 2334 if (pCommentBegin != gErrorBuffer && pCommentEnd != NULL)
2335 {
e53920b9 2336 pCommentEnd += 3; /* Skip past "]%%" */
2337 *pCommentEnd = '\0'; /* There's always room for the nul */
2338
88f9aafc 2339 if (_cups_strncasecmp(pCommentBegin, "%%[ Error:", 10) == 0)
e53920b9 2340 logLevel = "DEBUG";
88f9aafc 2341 else if (_cups_strncasecmp(pCommentBegin, "%%[ Flushing", 12) == 0)
e53920b9 2342 logLevel = "DEBUG";
2343 else
2344 logLevel = "INFO";
2345
09a101d6 2346 if ((logstrlen = snprintf(logstr, sizeof(logstr), "%s: %s\n", logLevel, pCommentBegin)) >= sizeof(logstr))
2347 {
e53920b9 2348 /* If the string was trucnated make sure it has a linefeed before the nul */
2349 logstrlen = sizeof(logstr) - 1;
2350 logstr[logstrlen - 1] = '\n';
2351 }
2352 write(STDERR_FILENO, logstr, logstrlen);
ef416fc2 2353 }
ef416fc2 2354
e53920b9 2355 /* move everything over... */
5a9febac 2356 strlcpy(gErrorBuffer, pLineEnd, sizeof(gErrorBuffer));
e53920b9 2357 gErrorBufferPtr = gErrorBuffer;
2358 pLineEnd = (char *)next_line((const char *)gErrorBuffer);
2359 }
ef416fc2 2360}
e53920b9 2361#endif /* PARSE_PS_ERRORS */
ef416fc2 2362
e53920b9 2363
2364/*
db1f069b 2365 * 'soft_reset()' - Send a soft reset to the device.
ef416fc2 2366 */
e53920b9 2367
eac3a0a0 2368static void soft_reset(void)
ef416fc2 2369{
09a101d6 2370 fd_set input_set; /* Input set for select() */
84315f46 2371 struct timeval tv; /* Time value */
09a101d6 2372 char buffer[2048]; /* Buffer */
2373 struct timespec cond_timeout; /* pthread condition timeout */
e53920b9 2374
09a101d6 2375 /*
2376 * Send an abort once a second until the I/O lock is released by the main thread...
2377 */
e53920b9 2378
09a101d6 2379 pthread_mutex_lock(&g.readwrite_lock_mutex);
2380 while (g.readwrite_lock)
2381 {
2382 (*g.classdriver)->Abort(g.classdriver);
ef416fc2 2383
84315f46
MS
2384 gettimeofday(&tv, NULL);
2385 cond_timeout.tv_sec = tv.tv_sec + 1;
2386 cond_timeout.tv_nsec = tv.tv_usec * 1000;
e53920b9 2387
84315f46
MS
2388 while (g.readwrite_lock)
2389 {
2390 if (pthread_cond_timedwait(&g.readwrite_lock_cond,
2391 &g.readwrite_lock_mutex,
2392 &cond_timeout) != 0)
2393 break;
2394 }
09a101d6 2395 }
f7deaa1a 2396
09a101d6 2397 g.readwrite_lock = 1;
2398 pthread_mutex_unlock(&g.readwrite_lock_mutex);
f7deaa1a 2399
09a101d6 2400 /*
2401 * Flush bytes waiting on print_fd...
2402 */
f7deaa1a 2403
09a101d6 2404 g.print_bytes = 0;
f7deaa1a 2405
09a101d6 2406 FD_ZERO(&input_set);
2407 FD_SET(g.print_fd, &input_set);
f7deaa1a 2408
84315f46
MS
2409 tv.tv_sec = 0;
2410 tv.tv_usec = 0;
f7deaa1a 2411
84315f46 2412 while (select(g.print_fd+1, &input_set, NULL, NULL, &tv) > 0)
09a101d6 2413 if (read(g.print_fd, buffer, sizeof(buffer)) <= 0)
2414 break;
f7deaa1a 2415
09a101d6 2416 /*
2417 * Send the reset...
2418 */
f7deaa1a 2419
7dfedb92 2420 (*g.classdriver)->SoftReset(g.classdriver, DEFAULT_TIMEOUT);
f7deaa1a 2421
09a101d6 2422 /*
2423 * Release the I/O lock...
2424 */
f7deaa1a 2425
09a101d6 2426 pthread_mutex_lock(&g.readwrite_lock_mutex);
2427 g.readwrite_lock = 0;
2428 pthread_cond_signal(&g.readwrite_lock_cond);
2429 pthread_mutex_unlock(&g.readwrite_lock_mutex);
f7deaa1a 2430}
2431
f7deaa1a 2432
2433/*
09a101d6 2434 * 'get_device_id()' - Return IEEE-1284 device ID.
f7deaa1a 2435 */
f7deaa1a 2436
09a101d6 2437static void get_device_id(cups_sc_status_t *status,
2438 char *data,
2439 int *datalen)
2440{
f7deaa1a 2441 CFStringRef deviceIDString = NULL;
2442
fe750b86
MS
2443 if (g.printer_obj != IO_OBJECT_NULL)
2444 {
f1ebe30e
MS
2445 printer_interface_t printerIntf = usb_printer_interface_interface(g.printer_obj);
2446 if (printerIntf)
fe750b86 2447 {
f1ebe30e
MS
2448 deviceIDString = copy_printer_interface_deviceid(printerIntf, g.alternateSetting);
2449 (*printerIntf)->Release(printerIntf);
fe750b86
MS
2450 }
2451 }
2452
e38f5e9c 2453
09a101d6 2454 if (deviceIDString)
2455 {
fe750b86
MS
2456 if (CFStringGetCString(deviceIDString, data, *datalen, kCFStringEncodingUTF8))
2457 *datalen = (int)strlen(data);
2458 else
2459 *datalen = 0;
de3edeba 2460
09a101d6 2461 CFRelease(deviceIDString);
f7deaa1a 2462 }
fe750b86
MS
2463 else
2464 {
2465 *datalen = 0;
2466 }
2467
09a101d6 2468 *status = CUPS_SC_STATUS_OK;
f7deaa1a 2469}