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