Blame backend/usb-darwin.c

Packit 2fc92b
/*
Packit 2fc92b
 * Copyright 2005-2016 Apple Inc. All rights reserved.
Packit 2fc92b
 *
Packit 2fc92b
 * IMPORTANT:  This Apple software is supplied to you by Apple Computer,
Packit 2fc92b
 * Inc. ("Apple") in consideration of your agreement to the following
Packit 2fc92b
 * terms, and your use, installation, modification or redistribution of
Packit 2fc92b
 * this Apple software constitutes acceptance of these terms.  If you do
Packit 2fc92b
 * not agree with these terms, please do not use, install, modify or
Packit 2fc92b
 * redistribute this Apple software.
Packit 2fc92b
 *
Packit 2fc92b
 * In consideration of your agreement to abide by the following terms, and
Packit 2fc92b
 * subject to these terms, Apple grants you a personal, non-exclusive
Packit 2fc92b
 * license, under Apple's copyrights in this original Apple software (the
Packit 2fc92b
 * "Apple Software"), to use, reproduce, modify and redistribute the Apple
Packit 2fc92b
 * Software, with or without modifications, in source and/or binary forms;
Packit 2fc92b
 * provided that if you redistribute the Apple Software in its entirety and
Packit 2fc92b
 * without modifications, you must retain this notice and the following
Packit 2fc92b
 * text and disclaimers in all such redistributions of the Apple Software.
Packit 2fc92b
 * Neither the name, trademarks, service marks or logos of Apple Computer,
Packit 2fc92b
 * Inc. may be used to endorse or promote products derived from the Apple
Packit 2fc92b
 * Software without specific prior written permission from Apple.  Except
Packit 2fc92b
 * as expressly stated in this notice, no other rights or licenses, express
Packit 2fc92b
 * or implied, are granted by Apple herein, including but not limited to
Packit 2fc92b
 * any patent rights that may be infringed by your derivative works or by
Packit 2fc92b
 * other works in which the Apple Software may be incorporated.
Packit 2fc92b
 *
Packit 2fc92b
 * The Apple Software is provided by Apple on an "AS IS" basis.  APPLE
Packit 2fc92b
 * MAKES NO WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION
Packit 2fc92b
 * THE IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS
Packit 2fc92b
 * FOR A PARTICULAR PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND
Packit 2fc92b
 * OPERATION ALONE OR IN COMBINATION WITH YOUR PRODUCTS.
Packit 2fc92b
 *
Packit 2fc92b
 * IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL
Packit 2fc92b
 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
Packit 2fc92b
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
Packit 2fc92b
 * INTERRUPTION) ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION,
Packit 2fc92b
 * MODIFICATION AND/OR DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED
Packit 2fc92b
 * AND WHETHER UNDER THEORY OF CONTRACT, TORT (INCLUDING NEGLIGENCE),
Packit 2fc92b
 * STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN ADVISED OF THE
Packit 2fc92b
 * POSSIBILITY OF SUCH DAMAGE.
Packit 2fc92b
 */
Packit 2fc92b
Packit 2fc92b
/*
Packit 2fc92b
 * Include necessary headers.
Packit 2fc92b
 */
Packit 2fc92b
Packit 2fc92b
#include <stdio.h>
Packit 2fc92b
#include <stdlib.h>
Packit 2fc92b
#include <errno.h>
Packit 2fc92b
#include <signal.h>
Packit 2fc92b
#include <fcntl.h>
Packit 2fc92b
#include <termios.h>
Packit 2fc92b
#include <unistd.h>
Packit 2fc92b
#include <sys/stat.h>
Packit 2fc92b
#include <sys/sysctl.h>
Packit 2fc92b
#include <libgen.h>
Packit 2fc92b
#include <mach/mach.h>
Packit 2fc92b
#include <mach/mach_error.h>
Packit 2fc92b
#include <mach/mach_time.h>
Packit 2fc92b
#include <cups/debug-private.h>
Packit 2fc92b
#include <cups/file-private.h>
Packit 2fc92b
#include <cups/sidechannel.h>
Packit 2fc92b
#include <cups/language-private.h>
Packit 2fc92b
#include <cups/ppd-private.h>
Packit 2fc92b
#include "backend-private.h"
Packit 2fc92b
#include <CoreFoundation/CoreFoundation.h>
Packit 2fc92b
#include <IOKit/usb/IOUSBLib.h>
Packit 2fc92b
#include <IOKit/IOCFPlugIn.h>
Packit 2fc92b
#include <libproc.h>
Packit 2fc92b
#include <asl.h>
Packit 2fc92b
#include <spawn.h>
Packit 2fc92b
#include <pthread.h>
Packit 2fc92b
Packit 2fc92b
/*
Packit 2fc92b
 * Include necessary headers.
Packit 2fc92b
 */
Packit 2fc92b
Packit 2fc92b
extern char **environ;
Packit 2fc92b
Packit 2fc92b
Packit 2fc92b
/*
Packit 2fc92b
 * DEBUG_WRITES, if defined, causes the backend to write data to the printer in
Packit 2fc92b
 * 512 byte increments, up to 8192 bytes, to make debugging with a USB bus
Packit 2fc92b
 * analyzer easier.
Packit 2fc92b
 */
Packit 2fc92b
Packit 2fc92b
#define DEBUG_WRITES 0
Packit 2fc92b
Packit 2fc92b
/*
Packit 2fc92b
 * WAIT_EOF_DELAY is number of seconds we'll wait for responses from
Packit 2fc92b
 * the printer after we've finished sending all the data
Packit 2fc92b
 */
Packit 2fc92b
#define WAIT_EOF_DELAY			7
Packit 2fc92b
#define WAIT_SIDE_DELAY			3
Packit 2fc92b
#define DEFAULT_TIMEOUT			5000L
Packit 2fc92b
Packit 2fc92b
#define	USB_INTERFACE_KIND		CFUUIDGetUUIDBytes(kIOUSBInterfaceInterfaceID245)
Packit 2fc92b
#define kUSBLanguageEnglish		0x409
Packit 2fc92b
Packit 2fc92b
#define PRINTER_POLLING_INTERVAL	5			/* seconds */
Packit 2fc92b
#define INITIAL_LOG_INTERVAL		PRINTER_POLLING_INTERVAL
Packit 2fc92b
#define SUBSEQUENT_LOG_INTERVAL		3 * INITIAL_LOG_INTERVAL
Packit 2fc92b
Packit 2fc92b
#define kUSBPrinterClassTypeID		CFUUIDGetConstantUUIDWithBytes(NULL, 0x06, 0x04, 0x7D, 0x16, 0x53, 0xA2, 0x11, 0xD6, 0x92, 0x06, 0x00, 0x30, 0x65, 0x52, 0x45, 0x92)
Packit 2fc92b
#define	kUSBPrinterClassInterfaceID	CFUUIDGetConstantUUIDWithBytes(NULL, 0x03, 0x34, 0x6D, 0x74, 0x53, 0xA3, 0x11, 0xD6, 0x9E, 0xA1, 0x76, 0x30, 0x65, 0x52, 0x45, 0x92)
Packit 2fc92b
Packit 2fc92b
#define kUSBClassDriverProperty		CFSTR("USB Printing Class")
Packit 2fc92b
Packit 2fc92b
#define kUSBGenericTOPrinterClassDriver	CFSTR("/System/Library/Printers/Libraries/USBGenericPrintingClass.plugin")
Packit 2fc92b
#define kUSBPrinterClassDeviceNotOpen	-9664	/*kPMInvalidIOMContext*/
Packit 2fc92b
Packit 2fc92b
#define CRSetCrashLogMessage(m) _crc_make_setter(message, m)
Packit 2fc92b
#define _crc_make_setter(attr, arg) (gCRAnnotations.attr = (uint64_t)(unsigned long)(arg))
Packit 2fc92b
#define CRASH_REPORTER_CLIENT_HIDDEN __attribute__((visibility("hidden")))
Packit 2fc92b
#define CRASHREPORTER_ANNOTATIONS_VERSION 4
Packit 2fc92b
#define CRASHREPORTER_ANNOTATIONS_SECTION "__crash_info"
Packit 2fc92b
Packit 2fc92b
struct crashreporter_annotations_t {
Packit 2fc92b
	uint64_t version;		// unsigned long
Packit 2fc92b
	uint64_t message;		// char *
Packit 2fc92b
	uint64_t signature_string;	// char *
Packit 2fc92b
	uint64_t backtrace;		// char *
Packit 2fc92b
	uint64_t message2;		// char *
Packit 2fc92b
	uint64_t thread;		// uint64_t
Packit 2fc92b
	uint64_t dialog_mode;		// unsigned int
Packit 2fc92b
};
Packit 2fc92b
Packit 2fc92b
CRASH_REPORTER_CLIENT_HIDDEN
Packit 2fc92b
struct crashreporter_annotations_t gCRAnnotations
Packit 2fc92b
	__attribute__((section("__DATA," CRASHREPORTER_ANNOTATIONS_SECTION)))
Packit 2fc92b
	= { CRASHREPORTER_ANNOTATIONS_VERSION, 0, 0, 0, 0, 0, 0 };
Packit 2fc92b
Packit 2fc92b
/*
Packit 2fc92b
 * Section 5.3 USB Printing Class spec
Packit 2fc92b
 */
Packit 2fc92b
#define kUSBPrintingSubclass			1
Packit 2fc92b
#define kUSBPrintingProtocolNoOpen		0
Packit 2fc92b
#define kUSBPrintingProtocolUnidirectional	1
Packit 2fc92b
#define kUSBPrintingProtocolBidirectional	2
Packit 2fc92b
#define kUSBPrintingProtocolIPP			4
Packit 2fc92b
Packit 2fc92b
typedef IOUSBInterfaceInterface245	**printer_interface_t;
Packit 2fc92b
Packit 2fc92b
typedef struct iodevice_request_s	/**** Device request ****/
Packit 2fc92b
{
Packit 2fc92b
  UInt8		requestType;
Packit 2fc92b
  UInt8		request;
Packit 2fc92b
  UInt16	value;
Packit 2fc92b
  UInt16	index;
Packit 2fc92b
  UInt16	length;
Packit 2fc92b
  void		*buffer;
Packit 2fc92b
} iodevice_request_t;
Packit 2fc92b
Packit 2fc92b
typedef union				/**** Centronics status byte ****/
Packit 2fc92b
{
Packit 2fc92b
  char		b;
Packit 2fc92b
  struct
Packit 2fc92b
  {
Packit 2fc92b
    unsigned	reserved0:2;
Packit 2fc92b
    unsigned	paperError:1;
Packit 2fc92b
    unsigned	select:1;
Packit 2fc92b
    unsigned	notError:1;
Packit 2fc92b
    unsigned	reserved1:3;
Packit 2fc92b
  } status;
Packit 2fc92b
} centronics_status_t;
Packit 2fc92b
Packit 2fc92b
typedef struct classdriver_s		/**** g.classdriver context ****/
Packit 2fc92b
{
Packit 2fc92b
  IUNKNOWN_C_GUTS;
Packit 2fc92b
  CFPlugInRef		plugin;			/* release plugin */
Packit 2fc92b
  IUnknownVTbl		**factory;		/* Factory */
Packit 2fc92b
  void			*vendorReference;	/* vendor class specific usage */
Packit 2fc92b
  UInt32		location;		/* unique location in bus topology */
Packit 2fc92b
  UInt8			interfaceNumber;	/* Interface number */
Packit 2fc92b
  UInt16		vendorID;		/* Vendor id */
Packit 2fc92b
  UInt16		productID;		/* Product id */
Packit 2fc92b
  printer_interface_t	interface;		/* identify the device to IOKit */
Packit 2fc92b
  UInt8			outpipe;		/* mandatory bulkOut pipe */
Packit 2fc92b
  UInt8			inpipe;			/* optional bulkIn pipe */
Packit 2fc92b
Packit 2fc92b
  /* general class requests */
Packit 2fc92b
  kern_return_t (*DeviceRequest)(struct classdriver_s **printer, iodevice_request_t *iorequest, UInt16 timeout);
Packit 2fc92b
  kern_return_t	(*GetString)(struct classdriver_s **printer, UInt8 whichString, UInt16 language, UInt16 timeout, CFStringRef *result);
Packit 2fc92b
Packit 2fc92b
  /* standard printer class requests */
Packit 2fc92b
  kern_return_t	(*SoftReset)(struct classdriver_s **printer, UInt16 timeout);
Packit 2fc92b
  kern_return_t	(*GetCentronicsStatus)(struct classdriver_s **printer, centronics_status_t *result, UInt16 timeout);
Packit 2fc92b
  kern_return_t	(*GetDeviceID)(struct classdriver_s **printer, CFStringRef *devid, UInt16 timeout);
Packit 2fc92b
Packit 2fc92b
  /* standard bulk device requests */
Packit 2fc92b
  kern_return_t (*ReadPipe)(struct classdriver_s **printer, UInt8 *buffer, UInt32 *count);
Packit 2fc92b
  kern_return_t (*WritePipe)(struct classdriver_s **printer, UInt8 *buffer, UInt32 *count, Boolean eoj);
Packit 2fc92b
Packit 2fc92b
  /* interface requests */
Packit 2fc92b
  kern_return_t (*Open)(struct classdriver_s **printer, UInt32 location, UInt8 protocol);
Packit 2fc92b
  kern_return_t (*Abort)(struct classdriver_s **printer);
Packit 2fc92b
  kern_return_t (*Close)(struct classdriver_s **printer);
Packit 2fc92b
Packit 2fc92b
  /* initialize and terminate */
Packit 2fc92b
  kern_return_t (*Initialize)(struct classdriver_s **printer, struct classdriver_s **baseclass);
Packit 2fc92b
  kern_return_t (*Terminate)(struct classdriver_s **printer);
Packit 2fc92b
Packit 2fc92b
} classdriver_t;
Packit 2fc92b
Packit 2fc92b
typedef Boolean (*iterator_callback_t)(io_service_t obj, printer_interface_t printerIntf, void *refcon);
Packit 2fc92b
Packit 2fc92b
typedef struct iterator_reference_s	/**** Iterator reference data */
Packit 2fc92b
{
Packit 2fc92b
  iterator_callback_t callback;
Packit 2fc92b
  void		*userdata;
Packit 2fc92b
  Boolean	keepRunning;
Packit 2fc92b
} iterator_reference_t;
Packit 2fc92b
Packit 2fc92b
typedef struct globals_s
Packit 2fc92b
{
Packit 2fc92b
  io_service_t		printer_obj;
Packit 2fc92b
  classdriver_t		**classdriver;
Packit 2fc92b
Packit 2fc92b
  pthread_mutex_t	read_thread_mutex;
Packit 2fc92b
  pthread_cond_t	read_thread_cond;
Packit 2fc92b
  int			read_thread_stop;
Packit 2fc92b
  int			read_thread_done;
Packit 2fc92b
Packit 2fc92b
  pthread_mutex_t	readwrite_lock_mutex;
Packit 2fc92b
  pthread_cond_t	readwrite_lock_cond;
Packit 2fc92b
  int			readwrite_lock;
Packit 2fc92b
Packit 2fc92b
  CFStringRef		make;
Packit 2fc92b
  CFStringRef		model;
Packit 2fc92b
  CFStringRef		serial;
Packit 2fc92b
  UInt32		location;
Packit 2fc92b
  UInt8			interfaceNum;
Packit 2fc92b
  UInt8			alternateSetting;
Packit 2fc92b
  UInt8                 interfaceProtocol;
Packit 2fc92b
Packit 2fc92b
  CFRunLoopTimerRef 	status_timer;
Packit 2fc92b
Packit 2fc92b
  int			print_fd;	/* File descriptor to print */
Packit 2fc92b
  ssize_t		print_bytes;	/* Print bytes read */
Packit 2fc92b
#if DEBUG_WRITES
Packit 2fc92b
  ssize_t		debug_bytes;	/* Current bytes to read */
Packit 2fc92b
#endif /* DEBUG_WRITES */
Packit 2fc92b
Packit 2fc92b
  Boolean		use_generic_class_driver;
Packit 2fc92b
  Boolean		wait_eof;
Packit 2fc92b
  int			drain_output;	/* Drain all pending output */
Packit 2fc92b
  int			bidi_flag;	/* 0=unidirectional, 1=bidirectional */
Packit 2fc92b
Packit 2fc92b
  pthread_mutex_t	sidechannel_thread_mutex;
Packit 2fc92b
  pthread_cond_t	sidechannel_thread_cond;
Packit 2fc92b
  int			sidechannel_thread_stop;
Packit 2fc92b
  int			sidechannel_thread_done;
Packit 2fc92b
} globals_t;
Packit 2fc92b
Packit 2fc92b
Packit 2fc92b
/*
Packit 2fc92b
 * Globals...
Packit 2fc92b
 */
Packit 2fc92b
Packit 2fc92b
globals_t g = { 0 };			/* Globals */
Packit 2fc92b
int Iterating = 0;			/* Are we iterating the bus? */
Packit 2fc92b
Packit 2fc92b
Packit 2fc92b
/*
Packit 2fc92b
 * Local functions...
Packit 2fc92b
 */
Packit 2fc92b
Packit 2fc92b
static Boolean list_device_cb(io_service_t obj, printer_interface_t printerIntf, void *refcon);
Packit 2fc92b
static Boolean find_device_cb(io_service_t obj, printer_interface_t printerIntf, void *refcon);
Packit 2fc92b
Packit 2fc92b
static CFStringRef cfstr_create_trim(const char *cstr);
Packit 2fc92b
static CFStringRef copy_value_for_key(CFStringRef deviceID, CFStringRef *keys);
Packit 2fc92b
static kern_return_t load_classdriver(CFStringRef driverPath, printer_interface_t interface, classdriver_t ***printerDriver);
Packit 2fc92b
static kern_return_t load_printerdriver(CFStringRef *driverBundlePath);
Packit 2fc92b
static kern_return_t registry_close(void);
Packit 2fc92b
static kern_return_t registry_open(CFStringRef *driverBundlePath);
Packit 2fc92b
static kern_return_t unload_classdriver(classdriver_t ***classdriver);
Packit 2fc92b
Packit 2fc92b
static void *read_thread(void *reference);
Packit 2fc92b
static void *sidechannel_thread(void *reference);
Packit 2fc92b
static void device_added(void *userdata, io_iterator_t iterator);
Packit 2fc92b
static void get_device_id(cups_sc_status_t *status, char *data, int *datalen);
Packit 2fc92b
static void iterate_printers(iterator_callback_t callBack, void *userdata);
Packit 2fc92b
static void parse_options(char *options, char *serial, int serial_size, UInt32 *location, Boolean *wait_eof);
Packit 2fc92b
static void setup_cfLanguage(void);
Packit 2fc92b
static void soft_reset(void);
Packit 2fc92b
static void status_timer_cb(CFRunLoopTimerRef timer, void *info);
Packit 2fc92b
#define IS_64BIT 1
Packit 2fc92b
#define IS_NOT_64BIT 0
Packit 2fc92b
Packit 2fc92b
#if defined(__i386__) || defined(__x86_64__)
Packit 2fc92b
static pid_t	child_pid;		/* Child PID */
Packit 2fc92b
static void run_legacy_backend(int argc, char *argv[], int fd) __attribute__((noreturn));	/* Starts child backend process running as a ppc executable */
Packit 2fc92b
#endif /* __i386__ || __x86_64__ */
Packit 2fc92b
static void sigterm_handler(int sig);	/* SIGTERM handler */
Packit 2fc92b
static void sigquit_handler(int sig, siginfo_t *si, void *unused) __attribute__((noreturn));
Packit 2fc92b
Packit 2fc92b
#ifdef PARSE_PS_ERRORS
Packit 2fc92b
static const char *next_line (const char *buffer);
Packit 2fc92b
static void parse_pserror (char *sockBuffer, int len);
Packit 2fc92b
#endif /* PARSE_PS_ERRORS */
Packit 2fc92b
Packit 2fc92b
static printer_interface_t usb_printer_interface_interface(io_service_t usbClass);
Packit 2fc92b
Packit 2fc92b
static CFStringRef copy_printer_interface_deviceid(printer_interface_t printer, UInt8 alternateSetting);
Packit 2fc92b
static CFStringRef copy_printer_interface_indexed_description(printer_interface_t  printer, UInt8 index, UInt16 language);
Packit 2fc92b
static CFStringRef deviceIDCopyManufacturer(CFStringRef deviceID);
Packit 2fc92b
static CFStringRef deviceIDCopyModel(CFStringRef deviceID);
Packit 2fc92b
static CFStringRef deviceIDCopySerialNumber(CFStringRef deviceID);
Packit 2fc92b
Packit 2fc92b
#pragma mark -
Packit 2fc92b
Packit 2fc92b
/*
Packit 2fc92b
 * 'list_devices()' - List all USB devices.
Packit 2fc92b
 */
Packit 2fc92b
Packit 2fc92b
void list_devices()
Packit 2fc92b
{
Packit 2fc92b
  iterate_printers(list_device_cb, NULL);
Packit 2fc92b
}
Packit 2fc92b
Packit 2fc92b
Packit 2fc92b
/*
Packit 2fc92b
 * 'print_device()' - Print a file to a USB device.
Packit 2fc92b
 */
Packit 2fc92b
Packit 2fc92b
int					/* O - Exit status */
Packit 2fc92b
print_device(const char *uri,		/* I - Device URI */
Packit 2fc92b
             const char *hostname,	/* I - Hostname/manufacturer */
Packit 2fc92b
             const char *resource,	/* I - Resource/modelname */
Packit 2fc92b
	     char       *options,	/* I - Device options/serial number */
Packit 2fc92b
	     int        print_fd,	/* I - File descriptor to print */
Packit 2fc92b
	     int        copies,		/* I - Copies to print */
Packit 2fc92b
	     int	argc,		/* I - Number of command-line arguments (6 or 7) */
Packit 2fc92b
	     char	*argv[])	/* I - Command-line arguments */
Packit 2fc92b
{
Packit 2fc92b
  char		  serial[1024];		/* Serial number buffer */
Packit 2fc92b
  OSStatus	  status;		/* Function results */
Packit 2fc92b
  IOReturn	  iostatus;		/* Current IO status */
Packit 2fc92b
  pthread_t	  read_thread_id,	/* Read thread */
Packit 2fc92b
		  sidechannel_thread_id;/* Side-channel thread */
Packit 2fc92b
  int		  have_sidechannel = 0;	/* Was the side-channel thread started? */
Packit 2fc92b
  struct stat     sidechannel_info;	/* Side-channel file descriptor info */
Packit 2fc92b
  char		  print_buffer[8192],	/* Print data buffer */
Packit 2fc92b
		  *print_ptr;		/* Pointer into print data buffer */
Packit 2fc92b
  UInt32	  location;		/* Unique location in bus topology */
Packit 2fc92b
  fd_set	  input_set;		/* Input set for select() */
Packit 2fc92b
  CFStringRef	  driverBundlePath;	/* Class driver path */
Packit 2fc92b
  int		  countdown,		/* Logging interval */
Packit 2fc92b
		  nfds;			/* Number of file descriptors */
Packit 2fc92b
  ssize_t	  total_bytes;		/* Total bytes written */
Packit 2fc92b
  UInt32	  bytes;		/* Bytes written */
Packit 2fc92b
  struct timeval  *timeout,		/* Timeout pointer */
Packit 2fc92b
		  tv;			/* Time value */
Packit 2fc92b
  struct timespec cond_timeout;		/* pthread condition timeout */
Packit 2fc92b
  struct sigaction action;		/* Actions for POSIX signals */
Packit 2fc92b
Packit 2fc92b
Packit 2fc92b
  (void)uri;
Packit 2fc92b
Packit 2fc92b
 /*
Packit 2fc92b
  * Catch SIGQUIT to determine who is sending it...
Packit 2fc92b
  */
Packit 2fc92b
Packit 2fc92b
  memset(&action, 0, sizeof(action));
Packit 2fc92b
  action.sa_sigaction = sigquit_handler;
Packit 2fc92b
  action.sa_flags = SA_SIGINFO;
Packit 2fc92b
  sigaction(SIGQUIT, &action, NULL);
Packit 2fc92b
Packit 2fc92b
 /*
Packit 2fc92b
  * See if the side-channel descriptor is valid...
Packit 2fc92b
  */
Packit 2fc92b
Packit 2fc92b
  have_sidechannel = !fstat(CUPS_SC_FD, &sidechannel_info) &&
Packit 2fc92b
                     S_ISSOCK(sidechannel_info.st_mode);
Packit 2fc92b
Packit 2fc92b
 /*
Packit 2fc92b
  * Localize using CoreFoundation...
Packit 2fc92b
  */
Packit 2fc92b
Packit 2fc92b
  setup_cfLanguage();
Packit 2fc92b
Packit 2fc92b
  parse_options(options, serial, sizeof(serial), &location, &g.wait_eof);
Packit 2fc92b
Packit 2fc92b
  if (resource[0] == '/')
Packit 2fc92b
    resource++;
Packit 2fc92b
Packit 2fc92b
  g.print_fd	= print_fd;
Packit 2fc92b
  g.make	= cfstr_create_trim(hostname);
Packit 2fc92b
  g.model	= cfstr_create_trim(resource);
Packit 2fc92b
  g.serial	= cfstr_create_trim(serial);
Packit 2fc92b
  g.location	= location;
Packit 2fc92b
Packit 2fc92b
  if (!g.make || !g.model)
Packit 2fc92b
  {
Packit 2fc92b
    fprintf(stderr, "DEBUG: Fatal USB error.\n");
Packit 2fc92b
    _cupsLangPrintFilter(stderr, "ERROR",
Packit 2fc92b
                         _("There was an unrecoverable USB error."));
Packit 2fc92b
Packit 2fc92b
    if (!g.make)
Packit 2fc92b
      fputs("DEBUG: USB make string is NULL\n", stderr);
Packit 2fc92b
    if (!g.model)
Packit 2fc92b
      fputs("DEBUG: USB model string is NULL\n", stderr);
Packit 2fc92b
Packit 2fc92b
    return (CUPS_BACKEND_STOP);
Packit 2fc92b
  }
Packit 2fc92b
Packit 2fc92b
  fputs("STATE: +connecting-to-device\n", stderr);
Packit 2fc92b
Packit 2fc92b
  countdown = INITIAL_LOG_INTERVAL;
Packit 2fc92b
Packit 2fc92b
  do
Packit 2fc92b
  {
Packit 2fc92b
    if (g.printer_obj)
Packit 2fc92b
    {
Packit 2fc92b
      IOObjectRelease(g.printer_obj);
Packit 2fc92b
      unload_classdriver(&g.classdriver);
Packit 2fc92b
      g.printer_obj = 0x0;
Packit 2fc92b
      g.classdriver = 0x0;
Packit 2fc92b
    }
Packit 2fc92b
    fprintf(stderr, "DEBUG: Looking for '%s %s'\n", hostname, resource);
Packit 2fc92b
Packit 2fc92b
    do
Packit 2fc92b
    {
Packit 2fc92b
      iterate_printers(find_device_cb, NULL);
Packit 2fc92b
      if (g.printer_obj != 0x0)
Packit 2fc92b
        break;
Packit 2fc92b
Packit 2fc92b
      _cupsLangPrintFilter(stderr, "INFO", _("Waiting for printer to become available."));
Packit 2fc92b
      sleep(5);
Packit 2fc92b
    } while (true);
Packit 2fc92b
Packit 2fc92b
    fputs("DEBUG: Opening connection\n", stderr);
Packit 2fc92b
Packit 2fc92b
    driverBundlePath = NULL;
Packit 2fc92b
Packit 2fc92b
    status = registry_open(&driverBundlePath);
Packit 2fc92b
Packit 2fc92b
#if defined(__i386__) || defined(__x86_64__)
Packit 2fc92b
    /*
Packit 2fc92b
     * If we were unable to load the class drivers for this printer it's
Packit 2fc92b
     * probably because they're ppc or i386. In this case try to run this
Packit 2fc92b
     * backend as i386 or ppc executables so we can use them...
Packit 2fc92b
     */
Packit 2fc92b
    if (status == -2)
Packit 2fc92b
    {
Packit 2fc92b
      run_legacy_backend(argc, argv, print_fd);
Packit 2fc92b
      /* Never returns here */
Packit 2fc92b
    }
Packit 2fc92b
#endif /* __i386__ || __x86_64__ */
Packit 2fc92b
Packit 2fc92b
    if (status ==  -2)
Packit 2fc92b
    {
Packit 2fc92b
     /*
Packit 2fc92b
      * If we still were unable to load the class drivers for this printer log
Packit 2fc92b
      * the error and stop the queue...
Packit 2fc92b
      */
Packit 2fc92b
Packit 2fc92b
      if (driverBundlePath == NULL || !CFStringGetCString(driverBundlePath, print_buffer, sizeof(print_buffer), kCFStringEncodingUTF8))
Packit 2fc92b
        strlcpy(print_buffer, "USB class driver", sizeof(print_buffer));
Packit 2fc92b
Packit 2fc92b
      fputs("STATE: +apple-missing-usbclassdriver-error\n", stderr);
Packit 2fc92b
      _cupsLangPrintFilter(stderr, "ERROR",
Packit 2fc92b
			   _("There was an unrecoverable USB error."));
Packit 2fc92b
      fprintf(stderr, "DEBUG: Could not load %s\n", print_buffer);
Packit 2fc92b
Packit 2fc92b
      if (driverBundlePath)
Packit 2fc92b
	CFRelease(driverBundlePath);
Packit 2fc92b
Packit 2fc92b
      return (CUPS_BACKEND_STOP);
Packit 2fc92b
    }
Packit 2fc92b
Packit 2fc92b
    if (driverBundlePath)
Packit 2fc92b
      CFRelease(driverBundlePath);
Packit 2fc92b
Packit 2fc92b
    if (status != noErr)
Packit 2fc92b
    {
Packit 2fc92b
      sleep(PRINTER_POLLING_INTERVAL);
Packit 2fc92b
      countdown -= PRINTER_POLLING_INTERVAL;
Packit 2fc92b
      if (countdown <= 0)
Packit 2fc92b
      {
Packit 2fc92b
	_cupsLangPrintFilter(stderr, "INFO",
Packit 2fc92b
		             _("Waiting for printer to become available."));
Packit 2fc92b
	fprintf(stderr, "DEBUG: USB printer status: 0x%08x\n", (int)status);
Packit 2fc92b
	countdown = SUBSEQUENT_LOG_INTERVAL;	/* subsequent log entries, every 15 seconds */
Packit 2fc92b
      }
Packit 2fc92b
    }
Packit 2fc92b
  } while (status != noErr);
Packit 2fc92b
Packit 2fc92b
  fputs("STATE: -connecting-to-device\n", stderr);
Packit 2fc92b
Packit 2fc92b
  /*
Packit 2fc92b
   * Now that we are "connected" to the port, ignore SIGTERM so that we
Packit 2fc92b
   * can finish out any page data the driver sends (e.g. to eject the
Packit 2fc92b
   * current page...  Only ignore SIGTERM if we are printing data from
Packit 2fc92b
   * stdin (otherwise you can't cancel raw jobs...)
Packit 2fc92b
   */
Packit 2fc92b
Packit 2fc92b
  if (!print_fd)
Packit 2fc92b
  {
Packit 2fc92b
    memset(&action, 0, sizeof(action));
Packit 2fc92b
Packit 2fc92b
    sigemptyset(&action.sa_mask);
Packit 2fc92b
    action.sa_handler = SIG_IGN;
Packit 2fc92b
    sigaction(SIGTERM, &action, NULL);
Packit 2fc92b
  }
Packit 2fc92b
Packit 2fc92b
 /*
Packit 2fc92b
  * Start the side channel thread if the descriptor is valid...
Packit 2fc92b
  */
Packit 2fc92b
Packit 2fc92b
  pthread_mutex_init(&g.readwrite_lock_mutex, NULL);
Packit 2fc92b
  pthread_cond_init(&g.readwrite_lock_cond, NULL);
Packit 2fc92b
  g.readwrite_lock = 1;
Packit 2fc92b
Packit 2fc92b
  if (have_sidechannel)
Packit 2fc92b
  {
Packit 2fc92b
    g.sidechannel_thread_stop = 0;
Packit 2fc92b
    g.sidechannel_thread_done = 0;
Packit 2fc92b
Packit 2fc92b
    pthread_cond_init(&g.sidechannel_thread_cond, NULL);
Packit 2fc92b
    pthread_mutex_init(&g.sidechannel_thread_mutex, NULL);
Packit 2fc92b
Packit 2fc92b
    if (pthread_create(&sidechannel_thread_id, NULL, sidechannel_thread, NULL))
Packit 2fc92b
    {
Packit 2fc92b
      fprintf(stderr, "DEBUG: Fatal USB error.\n");
Packit 2fc92b
      _cupsLangPrintFilter(stderr, "ERROR",
Packit 2fc92b
			   _("There was an unrecoverable USB error."));
Packit 2fc92b
      fputs("DEBUG: Couldn't create side-channel thread\n", stderr);
Packit 2fc92b
      registry_close();
Packit 2fc92b
      return (CUPS_BACKEND_STOP);
Packit 2fc92b
    }
Packit 2fc92b
  }
Packit 2fc92b
Packit 2fc92b
 /*
Packit 2fc92b
  * Get the read thread going...
Packit 2fc92b
  */
Packit 2fc92b
Packit 2fc92b
  g.read_thread_stop = 0;
Packit 2fc92b
  g.read_thread_done = 0;
Packit 2fc92b
Packit 2fc92b
  pthread_cond_init(&g.read_thread_cond, NULL);
Packit 2fc92b
  pthread_mutex_init(&g.read_thread_mutex, NULL);
Packit 2fc92b
Packit 2fc92b
  if (pthread_create(&read_thread_id, NULL, read_thread, NULL))
Packit 2fc92b
  {
Packit 2fc92b
    fprintf(stderr, "DEBUG: Fatal USB error.\n");
Packit 2fc92b
    _cupsLangPrintFilter(stderr, "ERROR",
Packit 2fc92b
                         _("There was an unrecoverable USB error."));
Packit 2fc92b
    fputs("DEBUG: Couldn't create read thread\n", stderr);
Packit 2fc92b
    registry_close();
Packit 2fc92b
    return (CUPS_BACKEND_STOP);
Packit 2fc92b
  }
Packit 2fc92b
Packit 2fc92b
 /*
Packit 2fc92b
  * The main thread sends the print file...
Packit 2fc92b
  */
Packit 2fc92b
Packit 2fc92b
  g.drain_output = 0;
Packit 2fc92b
  g.print_bytes	 = 0;
Packit 2fc92b
  total_bytes	 = 0;
Packit 2fc92b
  print_ptr	 = print_buffer;
Packit 2fc92b
Packit 2fc92b
  while (status == noErr && copies-- > 0)
Packit 2fc92b
  {
Packit 2fc92b
    _cupsLangPrintFilter(stderr, "INFO", _("Sending data to printer."));
Packit 2fc92b
Packit 2fc92b
    if (print_fd != STDIN_FILENO)
Packit 2fc92b
    {
Packit 2fc92b
      fputs("PAGE: 1 1\n", stderr);
Packit 2fc92b
      lseek(print_fd, 0, SEEK_SET);
Packit 2fc92b
    }
Packit 2fc92b
Packit 2fc92b
    while (status == noErr)
Packit 2fc92b
    {
Packit 2fc92b
      FD_ZERO(&input_set);
Packit 2fc92b
Packit 2fc92b
      if (!g.print_bytes)
Packit 2fc92b
	FD_SET(print_fd, &input_set);
Packit 2fc92b
Packit 2fc92b
     /*
Packit 2fc92b
      * Calculate select timeout...
Packit 2fc92b
      *   If we have data waiting to send timeout is 100ms.
Packit 2fc92b
      *   else if we're draining print_fd timeout is 0.
Packit 2fc92b
      *   else we're waiting forever...
Packit 2fc92b
      */
Packit 2fc92b
Packit 2fc92b
      if (g.print_bytes)
Packit 2fc92b
      {
Packit 2fc92b
	tv.tv_sec  = 0;
Packit 2fc92b
	tv.tv_usec = 100000;		/* 100ms */
Packit 2fc92b
	timeout = &tv;
Packit 2fc92b
      }
Packit 2fc92b
      else if (g.drain_output)
Packit 2fc92b
      {
Packit 2fc92b
	tv.tv_sec  = 0;
Packit 2fc92b
	tv.tv_usec = 0;
Packit 2fc92b
	timeout = &tv;
Packit 2fc92b
      }
Packit 2fc92b
      else
Packit 2fc92b
	timeout = NULL;
Packit 2fc92b
Packit 2fc92b
     /*
Packit 2fc92b
      * I/O is unlocked around select...
Packit 2fc92b
      */
Packit 2fc92b
Packit 2fc92b
      pthread_mutex_lock(&g.readwrite_lock_mutex);
Packit 2fc92b
      g.readwrite_lock = 0;
Packit 2fc92b
      pthread_cond_signal(&g.readwrite_lock_cond);
Packit 2fc92b
      pthread_mutex_unlock(&g.readwrite_lock_mutex);
Packit 2fc92b
Packit 2fc92b
      nfds = select(print_fd + 1, &input_set, NULL, NULL, timeout);
Packit 2fc92b
Packit 2fc92b
     /*
Packit 2fc92b
      * Reacquire the lock...
Packit 2fc92b
      */
Packit 2fc92b
Packit 2fc92b
      pthread_mutex_lock(&g.readwrite_lock_mutex);
Packit 2fc92b
      while (g.readwrite_lock)
Packit 2fc92b
	pthread_cond_wait(&g.readwrite_lock_cond, &g.readwrite_lock_mutex);
Packit 2fc92b
      g.readwrite_lock = 1;
Packit 2fc92b
      pthread_mutex_unlock(&g.readwrite_lock_mutex);
Packit 2fc92b
Packit 2fc92b
      if (nfds < 0)
Packit 2fc92b
      {
Packit 2fc92b
	if (errno == EINTR && total_bytes == 0)
Packit 2fc92b
	{
Packit 2fc92b
	  fputs("DEBUG: Received an interrupt before any bytes were "
Packit 2fc92b
	        "written, aborting\n", stderr);
Packit 2fc92b
          registry_close();
Packit 2fc92b
          return (CUPS_BACKEND_OK);
Packit 2fc92b
	}
Packit 2fc92b
	else if (errno != EAGAIN && errno != EINTR)
Packit 2fc92b
	{
Packit 2fc92b
	  _cupsLangPrintFilter(stderr, "ERROR",
Packit 2fc92b
	                       _("Unable to read print data."));
Packit 2fc92b
	  perror("DEBUG: select");
Packit 2fc92b
	  registry_close();
Packit 2fc92b
          return (CUPS_BACKEND_FAILED);
Packit 2fc92b
	}
Packit 2fc92b
      }
Packit 2fc92b
Packit 2fc92b
     /*
Packit 2fc92b
      * If drain output has finished send a response...
Packit 2fc92b
      */
Packit 2fc92b
Packit 2fc92b
      if (g.drain_output && !nfds && !g.print_bytes)
Packit 2fc92b
      {
Packit 2fc92b
	/* Send a response... */
Packit 2fc92b
	cupsSideChannelWrite(CUPS_SC_CMD_DRAIN_OUTPUT, CUPS_SC_STATUS_OK, NULL, 0, 1.0);
Packit 2fc92b
	g.drain_output = 0;
Packit 2fc92b
      }
Packit 2fc92b
Packit 2fc92b
     /*
Packit 2fc92b
      * Check if we have print data ready...
Packit 2fc92b
      */
Packit 2fc92b
Packit 2fc92b
      if (FD_ISSET(print_fd, &input_set))
Packit 2fc92b
      {
Packit 2fc92b
#if DEBUG_WRITES
Packit 2fc92b
	g.debug_bytes += 512;
Packit 2fc92b
        if (g.debug_bytes > sizeof(print_buffer))
Packit 2fc92b
	  g.debug_bytes = 512;
Packit 2fc92b
Packit 2fc92b
	g.print_bytes = read(print_fd, print_buffer, g.debug_bytes);
Packit 2fc92b
Packit 2fc92b
#else
Packit 2fc92b
	g.print_bytes = read(print_fd, print_buffer, sizeof(print_buffer));
Packit 2fc92b
#endif /* DEBUG_WRITES */
Packit 2fc92b
Packit 2fc92b
	if (g.print_bytes < 0)
Packit 2fc92b
	{
Packit 2fc92b
	 /*
Packit 2fc92b
	  * Read error - bail if we don't see EAGAIN or EINTR...
Packit 2fc92b
	  */
Packit 2fc92b
Packit 2fc92b
	  if (errno != EAGAIN && errno != EINTR)
Packit 2fc92b
	  {
Packit 2fc92b
	    _cupsLangPrintFilter(stderr, "ERROR",
Packit 2fc92b
				 _("Unable to read print data."));
Packit 2fc92b
	    perror("DEBUG: read");
Packit 2fc92b
	    registry_close();
Packit 2fc92b
	    return (CUPS_BACKEND_FAILED);
Packit 2fc92b
	  }
Packit 2fc92b
Packit 2fc92b
	  g.print_bytes = 0;
Packit 2fc92b
	}
Packit 2fc92b
	else if (g.print_bytes == 0)
Packit 2fc92b
	{
Packit 2fc92b
	 /*
Packit 2fc92b
	  * End of file, break out of the loop...
Packit 2fc92b
	  */
Packit 2fc92b
Packit 2fc92b
	  break;
Packit 2fc92b
	}
Packit 2fc92b
Packit 2fc92b
	print_ptr = print_buffer;
Packit 2fc92b
Packit 2fc92b
	fprintf(stderr, "DEBUG: Read %d bytes of print data...\n",
Packit 2fc92b
		(int)g.print_bytes);
Packit 2fc92b
      }
Packit 2fc92b
Packit 2fc92b
      if (g.print_bytes)
Packit 2fc92b
      {
Packit 2fc92b
	bytes    = (UInt32)g.print_bytes;
Packit 2fc92b
	iostatus = (*g.classdriver)->WritePipe(g.classdriver, (UInt8*)print_ptr, &bytes, 0);
Packit 2fc92b
Packit 2fc92b
       /*
Packit 2fc92b
	* Ignore timeout errors, but retain the number of bytes written to
Packit 2fc92b
	* avoid sending duplicate data...
Packit 2fc92b
	*/
Packit 2fc92b
Packit 2fc92b
	if (iostatus == kIOUSBTransactionTimeout)
Packit 2fc92b
	{
Packit 2fc92b
	  fputs("DEBUG: Got USB transaction timeout during write\n", stderr);
Packit 2fc92b
	  iostatus = 0;
Packit 2fc92b
	}
Packit 2fc92b
Packit 2fc92b
       /*
Packit 2fc92b
        * If we've stalled, retry the write...
Packit 2fc92b
	*/
Packit 2fc92b
Packit 2fc92b
	else if (iostatus == kIOUSBPipeStalled)
Packit 2fc92b
	{
Packit 2fc92b
	  fputs("DEBUG: Got USB pipe stalled during write\n", stderr);
Packit 2fc92b
Packit 2fc92b
	  bytes    = (UInt32)g.print_bytes;
Packit 2fc92b
	  iostatus = (*g.classdriver)->WritePipe(g.classdriver, (UInt8*)print_ptr, &bytes, 0);
Packit 2fc92b
	}
Packit 2fc92b
Packit 2fc92b
       /*
Packit 2fc92b
	* Retry a write after an aborted write since we probably just got
Packit 2fc92b
	* SIGTERM...
Packit 2fc92b
	*/
Packit 2fc92b
Packit 2fc92b
	else if (iostatus == kIOReturnAborted)
Packit 2fc92b
	{
Packit 2fc92b
	  fputs("DEBUG: Got USB return aborted during write\n", stderr);
Packit 2fc92b
Packit 2fc92b
	  IOReturn err = (*g.classdriver)->Abort(g.classdriver);
Packit 2fc92b
	  fprintf(stderr, "DEBUG: USB class driver Abort returned %x\n", err);
Packit 2fc92b
Packit 2fc92b
#if DEBUG_WRITES
Packit 2fc92b
          sleep(5);
Packit 2fc92b
#endif /* DEBUG_WRITES */
Packit 2fc92b
Packit 2fc92b
	  bytes    = (UInt32)g.print_bytes;
Packit 2fc92b
	  iostatus = (*g.classdriver)->WritePipe(g.classdriver, (UInt8*)print_ptr, &bytes, 0);
Packit 2fc92b
        }
Packit 2fc92b
Packit 2fc92b
	if (iostatus)
Packit 2fc92b
	{
Packit 2fc92b
	 /*
Packit 2fc92b
	  * Write error - bail if we don't see an error we can retry...
Packit 2fc92b
	  */
Packit 2fc92b
Packit 2fc92b
	  _cupsLangPrintFilter(stderr, "ERROR",
Packit 2fc92b
	                       _("Unable to send data to printer."));
Packit 2fc92b
	  fprintf(stderr, "DEBUG: USB class driver WritePipe returned %x\n",
Packit 2fc92b
	          iostatus);
Packit 2fc92b
Packit 2fc92b
	  IOReturn err = (*g.classdriver)->Abort(g.classdriver);
Packit 2fc92b
	  fprintf(stderr, "DEBUG: USB class driver Abort returned %x\n",
Packit 2fc92b
	          err);
Packit 2fc92b
Packit 2fc92b
	  status = CUPS_BACKEND_FAILED;
Packit 2fc92b
	  break;
Packit 2fc92b
	}
Packit 2fc92b
	else if (bytes > 0)
Packit 2fc92b
	{
Packit 2fc92b
	  fprintf(stderr, "DEBUG: Wrote %d bytes of print data...\n", (int)bytes);
Packit 2fc92b
Packit 2fc92b
	  g.print_bytes -= bytes;
Packit 2fc92b
	  print_ptr   += bytes;
Packit 2fc92b
	  total_bytes += bytes;
Packit 2fc92b
	}
Packit 2fc92b
      }
Packit 2fc92b
Packit 2fc92b
      if (print_fd != 0 && status == noErr)
Packit 2fc92b
	fprintf(stderr, "DEBUG: Sending print file, %lld bytes...\n",
Packit 2fc92b
		(off_t)total_bytes);
Packit 2fc92b
    }
Packit 2fc92b
  }
Packit 2fc92b
Packit 2fc92b
  fprintf(stderr, "DEBUG: Sent %lld bytes...\n", (off_t)total_bytes);
Packit 2fc92b
  fputs("STATE: +cups-waiting-for-job-completed\n", stderr);
Packit 2fc92b
Packit 2fc92b
 /*
Packit 2fc92b
  * Signal the side channel thread to exit...
Packit 2fc92b
  */
Packit 2fc92b
Packit 2fc92b
  if (have_sidechannel)
Packit 2fc92b
  {
Packit 2fc92b
    close(CUPS_SC_FD);
Packit 2fc92b
    pthread_mutex_lock(&g.readwrite_lock_mutex);
Packit 2fc92b
    g.readwrite_lock = 0;
Packit 2fc92b
    pthread_cond_signal(&g.readwrite_lock_cond);
Packit 2fc92b
    pthread_mutex_unlock(&g.readwrite_lock_mutex);
Packit 2fc92b
Packit 2fc92b
    g.sidechannel_thread_stop = 1;
Packit 2fc92b
    pthread_mutex_lock(&g.sidechannel_thread_mutex);
Packit 2fc92b
Packit 2fc92b
    if (!g.sidechannel_thread_done)
Packit 2fc92b
    {
Packit 2fc92b
      gettimeofday(&tv, NULL);
Packit 2fc92b
      cond_timeout.tv_sec  = tv.tv_sec + WAIT_SIDE_DELAY;
Packit 2fc92b
      cond_timeout.tv_nsec = tv.tv_usec * 1000;
Packit 2fc92b
Packit 2fc92b
      while (!g.sidechannel_thread_done)
Packit 2fc92b
      {
Packit 2fc92b
	if (pthread_cond_timedwait(&g.sidechannel_thread_cond,
Packit 2fc92b
				   &g.sidechannel_thread_mutex,
Packit 2fc92b
				   &cond_timeout) != 0)
Packit 2fc92b
	  break;
Packit 2fc92b
      }
Packit 2fc92b
    }
Packit 2fc92b
Packit 2fc92b
    pthread_mutex_unlock(&g.sidechannel_thread_mutex);
Packit 2fc92b
  }
Packit 2fc92b
Packit 2fc92b
 /*
Packit 2fc92b
  * Signal the read thread to exit then wait 7 seconds for it to complete...
Packit 2fc92b
  */
Packit 2fc92b
Packit 2fc92b
  g.read_thread_stop = 1;
Packit 2fc92b
Packit 2fc92b
  pthread_mutex_lock(&g.read_thread_mutex);
Packit 2fc92b
Packit 2fc92b
  if (!g.read_thread_done)
Packit 2fc92b
  {
Packit 2fc92b
    fputs("DEBUG: Waiting for read thread to exit...\n", stderr);
Packit 2fc92b
Packit 2fc92b
    gettimeofday(&tv, NULL);
Packit 2fc92b
    cond_timeout.tv_sec  = tv.tv_sec + WAIT_EOF_DELAY;
Packit 2fc92b
    cond_timeout.tv_nsec = tv.tv_usec * 1000;
Packit 2fc92b
Packit 2fc92b
    while (!g.read_thread_done)
Packit 2fc92b
    {
Packit 2fc92b
      if (pthread_cond_timedwait(&g.read_thread_cond, &g.read_thread_mutex,
Packit 2fc92b
				 &cond_timeout) != 0)
Packit 2fc92b
	break;
Packit 2fc92b
    }
Packit 2fc92b
Packit 2fc92b
   /*
Packit 2fc92b
    * If it didn't exit abort the pending read and wait an additional second...
Packit 2fc92b
    */
Packit 2fc92b
Packit 2fc92b
    if (!g.read_thread_done)
Packit 2fc92b
    {
Packit 2fc92b
      fputs("DEBUG: Read thread still active, aborting the pending read...\n",
Packit 2fc92b
	    stderr);
Packit 2fc92b
Packit 2fc92b
      g.wait_eof = 0;
Packit 2fc92b
Packit 2fc92b
      (*g.classdriver)->Abort(g.classdriver);
Packit 2fc92b
Packit 2fc92b
      gettimeofday(&tv, NULL);
Packit 2fc92b
      cond_timeout.tv_sec  = tv.tv_sec + 1;
Packit 2fc92b
      cond_timeout.tv_nsec = tv.tv_usec * 1000;
Packit 2fc92b
Packit 2fc92b
      while (!g.read_thread_done)
Packit 2fc92b
      {
Packit 2fc92b
	if (pthread_cond_timedwait(&g.read_thread_cond, &g.read_thread_mutex,
Packit 2fc92b
				   &cond_timeout) != 0)
Packit 2fc92b
	  break;
Packit 2fc92b
      }
Packit 2fc92b
    }
Packit 2fc92b
  }
Packit 2fc92b
Packit 2fc92b
  pthread_mutex_unlock(&g.read_thread_mutex);
Packit 2fc92b
Packit 2fc92b
 /*
Packit 2fc92b
  * Close the connection and input file and general clean up...
Packit 2fc92b
  */
Packit 2fc92b
Packit 2fc92b
  registry_close();
Packit 2fc92b
Packit 2fc92b
  if (print_fd != STDIN_FILENO)
Packit 2fc92b
    close(print_fd);
Packit 2fc92b
Packit 2fc92b
  if (g.make != NULL)
Packit 2fc92b
    CFRelease(g.make);
Packit 2fc92b
Packit 2fc92b
  if (g.model != NULL)
Packit 2fc92b
    CFRelease(g.model);
Packit 2fc92b
Packit 2fc92b
  if (g.serial != NULL)
Packit 2fc92b
    CFRelease(g.serial);
Packit 2fc92b
Packit 2fc92b
  if (g.printer_obj != 0x0)
Packit 2fc92b
    IOObjectRelease(g.printer_obj);
Packit 2fc92b
Packit 2fc92b
  return status;
Packit 2fc92b
}
Packit 2fc92b
Packit 2fc92b
Packit 2fc92b
/*
Packit 2fc92b
 * 'read_thread()' - Thread to read the backchannel data on.
Packit 2fc92b
 */
Packit 2fc92b
Packit 2fc92b
static void *read_thread(void *reference)
Packit 2fc92b
{
Packit 2fc92b
  UInt8				readbuffer[512];
Packit 2fc92b
  UInt32			rbytes;
Packit 2fc92b
  kern_return_t			readstatus;
Packit 2fc92b
  struct mach_timebase_info	timeBaseInfo;
Packit 2fc92b
  uint64_t			start,
Packit 2fc92b
				delay;
Packit 2fc92b
Packit 2fc92b
Packit 2fc92b
  (void)reference;
Packit 2fc92b
Packit 2fc92b
  /* Calculate what 250 milliSeconds are in mach absolute time...
Packit 2fc92b
   */
Packit 2fc92b
  mach_timebase_info(&timeBaseInfo);
Packit 2fc92b
  delay = ((uint64_t)250000000 * (uint64_t)timeBaseInfo.denom) / (uint64_t)timeBaseInfo.numer;
Packit 2fc92b
Packit 2fc92b
  do
Packit 2fc92b
  {
Packit 2fc92b
   /*
Packit 2fc92b
    * Remember when we started so we can throttle the loop after the read call...
Packit 2fc92b
    */
Packit 2fc92b
Packit 2fc92b
    start = mach_absolute_time();
Packit 2fc92b
Packit 2fc92b
    rbytes = sizeof(readbuffer);
Packit 2fc92b
    readstatus = (*g.classdriver)->ReadPipe(g.classdriver, readbuffer, &rbytes);
Packit 2fc92b
    if (readstatus == kIOReturnSuccess && rbytes > 0)
Packit 2fc92b
    {
Packit 2fc92b
      fprintf(stderr, "DEBUG: Read %d bytes of back-channel data...\n",
Packit 2fc92b
              (int)rbytes);
Packit 2fc92b
      cupsBackChannelWrite((char*)readbuffer, rbytes, 1.0);
Packit 2fc92b
Packit 2fc92b
      /* cntrl-d is echoed by the printer.
Packit 2fc92b
       * NOTES:
Packit 2fc92b
       *   Xerox Phaser 6250D doesn't echo the cntrl-d.
Packit 2fc92b
       *   Xerox Phaser 6250D doesn't always send the product query.
Packit 2fc92b
       */
Packit 2fc92b
      if (g.wait_eof && readbuffer[rbytes-1] == 0x4)
Packit 2fc92b
	break;
Packit 2fc92b
Packit 2fc92b
#ifdef PARSE_PS_ERRORS
Packit 2fc92b
      parse_pserror(readbuffer, rbytes);
Packit 2fc92b
#endif
Packit 2fc92b
    }
Packit 2fc92b
    else if (readstatus == kIOUSBTransactionTimeout)
Packit 2fc92b
      fputs("DEBUG: Got USB transaction timeout during read\n", stderr);
Packit 2fc92b
    else if (readstatus == kIOUSBPipeStalled)
Packit 2fc92b
      fputs("DEBUG: Got USB pipe stalled during read\n", stderr);
Packit 2fc92b
    else if (readstatus == kIOReturnAborted)
Packit 2fc92b
      fputs("DEBUG: Got USB return aborted during read\n", stderr);
Packit 2fc92b
Packit 2fc92b
   /*
Packit 2fc92b
    * Make sure this loop executes no more than once every 250 miliseconds...
Packit 2fc92b
    */
Packit 2fc92b
Packit 2fc92b
    if ((readstatus != kIOReturnSuccess || rbytes == 0) && (g.wait_eof || !g.read_thread_stop))
Packit 2fc92b
      mach_wait_until(start + delay);
Packit 2fc92b
Packit 2fc92b
  } while (g.wait_eof || !g.read_thread_stop);	/* Abort from main thread tests error here */
Packit 2fc92b
Packit 2fc92b
  /* Workaround for usb race condition. <rdar://problem/21882551> */
Packit 2fc92b
  if (!g.wait_eof && g.use_generic_class_driver)
Packit 2fc92b
  {
Packit 2fc92b
     const char *pdl = getenv("FINAL_CONTENT_TYPE");
Packit 2fc92b
     if (pdl && strcmp(pdl, "application/vnd.cups-postscript") == 0)
Packit 2fc92b
     {
Packit 2fc92b
       while (readstatus == kIOReturnSuccess && ((rbytes > 0 && readbuffer[rbytes-1] != 0x4) || rbytes == 0))
Packit 2fc92b
       {
Packit 2fc92b
         start = mach_absolute_time();
Packit 2fc92b
Packit 2fc92b
         rbytes = sizeof(readbuffer);
Packit 2fc92b
         readstatus = (*g.classdriver)->ReadPipe(g.classdriver, readbuffer, &rbytes);
Packit 2fc92b
         if (readstatus == kIOReturnSuccess && rbytes > 0 && readbuffer[rbytes-1] == 0x4)
Packit 2fc92b
           break;
Packit 2fc92b
Packit 2fc92b
         /* Make sure this loop executes no more than once every 250 miliseconds... */
Packit 2fc92b
         mach_wait_until(start + delay);
Packit 2fc92b
       }
Packit 2fc92b
     }
Packit 2fc92b
  }
Packit 2fc92b
Packit 2fc92b
 /*
Packit 2fc92b
  * Let the main thread know that we have completed the read thread...
Packit 2fc92b
  */
Packit 2fc92b
Packit 2fc92b
  pthread_mutex_lock(&g.read_thread_mutex);
Packit 2fc92b
  g.read_thread_done = 1;
Packit 2fc92b
  pthread_cond_signal(&g.read_thread_cond);
Packit 2fc92b
  pthread_mutex_unlock(&g.read_thread_mutex);
Packit 2fc92b
Packit 2fc92b
  return NULL;
Packit 2fc92b
}
Packit 2fc92b
Packit 2fc92b
Packit 2fc92b
/*
Packit 2fc92b
 * 'sidechannel_thread()' - Handle side-channel requests.
Packit 2fc92b
 */
Packit 2fc92b
Packit 2fc92b
static void*
Packit 2fc92b
sidechannel_thread(void *reference)
Packit 2fc92b
{
Packit 2fc92b
  cups_sc_command_t	command;	/* Request command */
Packit 2fc92b
  cups_sc_status_t	status;		/* Request/response status */
Packit 2fc92b
  char			data[2048];	/* Request/response data */
Packit 2fc92b
  int			datalen;	/* Request/response data size */
Packit 2fc92b
Packit 2fc92b
Packit 2fc92b
  (void)reference;
Packit 2fc92b
Packit 2fc92b
  do
Packit 2fc92b
  {
Packit 2fc92b
    datalen = sizeof(data);
Packit 2fc92b
Packit 2fc92b
    if (cupsSideChannelRead(&command, &status, data, &datalen, 1.0))
Packit 2fc92b
    {
Packit 2fc92b
      if (status == CUPS_SC_STATUS_TIMEOUT)
Packit 2fc92b
	continue;
Packit 2fc92b
      else
Packit 2fc92b
	break;
Packit 2fc92b
    }
Packit 2fc92b
Packit 2fc92b
    switch (command)
Packit 2fc92b
    {
Packit 2fc92b
      case CUPS_SC_CMD_SOFT_RESET:	/* Do a soft reset */
Packit 2fc92b
	  fputs("DEBUG: CUPS_SC_CMD_SOFT_RESET received from driver...\n",
Packit 2fc92b
		stderr);
Packit 2fc92b
Packit 2fc92b
          if ((*g.classdriver)->SoftReset != NULL)
Packit 2fc92b
	  {
Packit 2fc92b
	    soft_reset();
Packit 2fc92b
	    cupsSideChannelWrite(command, CUPS_SC_STATUS_OK, NULL, 0, 1.0);
Packit 2fc92b
	    fputs("DEBUG: Returning status CUPS_STATUS_OK with no bytes...\n",
Packit 2fc92b
	          stderr);
Packit 2fc92b
	  }
Packit 2fc92b
	  else
Packit 2fc92b
	  {
Packit 2fc92b
	    cupsSideChannelWrite(command, CUPS_SC_STATUS_NOT_IMPLEMENTED,
Packit 2fc92b
	                         NULL, 0, 1.0);
Packit 2fc92b
	    fputs("DEBUG: Returning status CUPS_STATUS_NOT_IMPLEMENTED with "
Packit 2fc92b
	          "no bytes...\n", stderr);
Packit 2fc92b
	  }
Packit 2fc92b
	  break;
Packit 2fc92b
Packit 2fc92b
      case CUPS_SC_CMD_DRAIN_OUTPUT:	/* Drain all pending output */
Packit 2fc92b
	  fputs("DEBUG: CUPS_SC_CMD_DRAIN_OUTPUT received from driver...\n",
Packit 2fc92b
		stderr);
Packit 2fc92b
Packit 2fc92b
	  g.drain_output = 1;
Packit 2fc92b
	  break;
Packit 2fc92b
Packit 2fc92b
      case CUPS_SC_CMD_GET_BIDI:		/* Is the connection bidirectional? */
Packit 2fc92b
	  fputs("DEBUG: CUPS_SC_CMD_GET_BIDI received from driver...\n",
Packit 2fc92b
		stderr);
Packit 2fc92b
Packit 2fc92b
	  data[0] = (char)g.bidi_flag;
Packit 2fc92b
	  cupsSideChannelWrite(command, CUPS_SC_STATUS_OK, data, 1, 1.0);
Packit 2fc92b
Packit 2fc92b
	  fprintf(stderr,
Packit 2fc92b
	          "DEBUG: Returned CUPS_SC_STATUS_OK with 1 byte (%02X)...\n",
Packit 2fc92b
		  data[0]);
Packit 2fc92b
	  break;
Packit 2fc92b
Packit 2fc92b
      case CUPS_SC_CMD_GET_DEVICE_ID:	/* Return IEEE-1284 device ID */
Packit 2fc92b
	  fputs("DEBUG: CUPS_SC_CMD_GET_DEVICE_ID received from driver...\n",
Packit 2fc92b
		stderr);
Packit 2fc92b
Packit 2fc92b
	  datalen = sizeof(data);
Packit 2fc92b
	  get_device_id(&status, data, &datalen);
Packit 2fc92b
	  cupsSideChannelWrite(command, CUPS_SC_STATUS_OK, data, datalen, 1.0);
Packit 2fc92b
Packit 2fc92b
          if ((size_t)datalen < sizeof(data))
Packit 2fc92b
	    data[datalen] = '\0';
Packit 2fc92b
	  else
Packit 2fc92b
	    data[sizeof(data) - 1] = '\0';
Packit 2fc92b
Packit 2fc92b
	  fprintf(stderr,
Packit 2fc92b
	          "DEBUG: Returning CUPS_SC_STATUS_OK with %d bytes (%s)...\n",
Packit 2fc92b
		  datalen, data);
Packit 2fc92b
	  break;
Packit 2fc92b
Packit 2fc92b
      case CUPS_SC_CMD_GET_STATE:		/* Return device state */
Packit 2fc92b
	  fputs("DEBUG: CUPS_SC_CMD_GET_STATE received from driver...\n",
Packit 2fc92b
		stderr);
Packit 2fc92b
Packit 2fc92b
	  data[0] = CUPS_SC_STATE_ONLINE;
Packit 2fc92b
	  cupsSideChannelWrite(command, CUPS_SC_STATUS_OK, data, 1, 1.0);
Packit 2fc92b
Packit 2fc92b
	  fprintf(stderr,
Packit 2fc92b
	          "DEBUG: Returned CUPS_SC_STATUS_OK with 1 byte (%02X)...\n",
Packit 2fc92b
		  data[0]);
Packit 2fc92b
	  break;
Packit 2fc92b
Packit 2fc92b
      default:
Packit 2fc92b
	  fprintf(stderr, "DEBUG: Unknown side-channel command (%d) received "
Packit 2fc92b
			  "from driver...\n", command);
Packit 2fc92b
Packit 2fc92b
	  cupsSideChannelWrite(command, CUPS_SC_STATUS_NOT_IMPLEMENTED,
Packit 2fc92b
			       NULL, 0, 1.0);
Packit 2fc92b
Packit 2fc92b
	  fputs("DEBUG: Returned CUPS_SC_STATUS_NOT_IMPLEMENTED with no bytes...\n",
Packit 2fc92b
		stderr);
Packit 2fc92b
	  break;
Packit 2fc92b
    }
Packit 2fc92b
  }
Packit 2fc92b
  while (!g.sidechannel_thread_stop);
Packit 2fc92b
Packit 2fc92b
  pthread_mutex_lock(&g.sidechannel_thread_mutex);
Packit 2fc92b
  g.sidechannel_thread_done = 1;
Packit 2fc92b
  pthread_cond_signal(&g.sidechannel_thread_cond);
Packit 2fc92b
  pthread_mutex_unlock(&g.sidechannel_thread_mutex);
Packit 2fc92b
Packit 2fc92b
  return NULL;
Packit 2fc92b
}
Packit 2fc92b
Packit 2fc92b
Packit 2fc92b
#pragma mark -
Packit 2fc92b
/*
Packit 2fc92b
 * 'iterate_printers()' - Iterate over all the printers.
Packit 2fc92b
 */
Packit 2fc92b
static void iterate_printers(iterator_callback_t callBack, void *userdata)
Packit 2fc92b
{
Packit 2fc92b
  Iterating = 1;
Packit 2fc92b
Packit 2fc92b
  iterator_reference_t reference = { callBack, userdata, true };
Packit 2fc92b
Packit 2fc92b
  IONotificationPortRef addNotification = IONotificationPortCreate(kIOMasterPortDefault);
Packit 2fc92b
Packit 2fc92b
  int printingClass = kUSBPrintingClass;
Packit 2fc92b
  int printingSubclass = kUSBPrintingSubclass;
Packit 2fc92b
Packit 2fc92b
  CFNumberRef interfaceClass = CFNumberCreate(kCFAllocatorDefault, kCFNumberIntType, &printingClass);
Packit 2fc92b
  CFNumberRef interfaceSubClass = CFNumberCreate(kCFAllocatorDefault, kCFNumberIntType, &printingSubclass);
Packit 2fc92b
Packit 2fc92b
  CFMutableDictionaryRef usbPrinterMatchDictionary = IOServiceMatching(kIOUSBInterfaceClassName);
Packit 2fc92b
  CFDictionaryAddValue(usbPrinterMatchDictionary, CFSTR("bInterfaceClass"), interfaceClass);
Packit 2fc92b
  CFDictionaryAddValue(usbPrinterMatchDictionary, CFSTR("bInterfaceSubClass"), interfaceSubClass);
Packit 2fc92b
Packit 2fc92b
  CFRelease(interfaceClass);
Packit 2fc92b
  CFRelease(interfaceSubClass);
Packit 2fc92b
Packit 2fc92b
  io_iterator_t add_iterator = IO_OBJECT_NULL;
Packit 2fc92b
  IOServiceAddMatchingNotification(addNotification, kIOMatchedNotification,
Packit 2fc92b
                usbPrinterMatchDictionary, &device_added, &reference, &add_iterator);
Packit 2fc92b
  if (add_iterator != IO_OBJECT_NULL)
Packit 2fc92b
  {
Packit 2fc92b
    device_added (&reference, add_iterator);
Packit 2fc92b
    if (reference.keepRunning)
Packit 2fc92b
    {
Packit 2fc92b
      CFRunLoopAddSource(CFRunLoopGetCurrent(), IONotificationPortGetRunLoopSource(addNotification), kCFRunLoopDefaultMode);
Packit 2fc92b
      CFRunLoopRun();
Packit 2fc92b
    }
Packit 2fc92b
    IOObjectRelease(add_iterator);
Packit 2fc92b
  }
Packit 2fc92b
  Iterating = 0;
Packit 2fc92b
}
Packit 2fc92b
Packit 2fc92b
Packit 2fc92b
/*
Packit 2fc92b
 * 'device_added()' - Device added notifier.
Packit 2fc92b
 */
Packit 2fc92b
static void device_added(void *userdata, io_iterator_t iterator)
Packit 2fc92b
{
Packit 2fc92b
  iterator_reference_t *reference = userdata;
Packit 2fc92b
  io_service_t intf;
Packit 2fc92b
Packit 2fc92b
  while (reference->keepRunning && (intf = IOIteratorNext(iterator)) != 0x0)
Packit 2fc92b
  {
Packit 2fc92b
    printer_interface_t printerIntf = usb_printer_interface_interface(intf);
Packit 2fc92b
    if (printerIntf != NULL)
Packit 2fc92b
    {
Packit 2fc92b
      UInt8 intfClass = 0, intfSubClass = 0;
Packit 2fc92b
Packit 2fc92b
      (*printerIntf)->GetInterfaceClass(printerIntf, &intfClass);
Packit 2fc92b
      (*printerIntf)->GetInterfaceSubClass(printerIntf, &intfSubClass);
Packit 2fc92b
      if (intfClass == kUSBPrintingInterfaceClass && intfSubClass == kUSBPrintingSubclass)
Packit 2fc92b
        reference->keepRunning = reference->callback(intf, printerIntf, userdata);
Packit 2fc92b
        (*printerIntf)->Release(printerIntf);
Packit 2fc92b
      }
Packit 2fc92b
      IOObjectRelease(intf);
Packit 2fc92b
    }
Packit 2fc92b
Packit 2fc92b
    if (reference->keepRunning && reference->callback)
Packit 2fc92b
      reference->keepRunning = reference->callback(IO_OBJECT_NULL, NULL, reference->userdata);
Packit 2fc92b
Packit 2fc92b
    if (!reference->keepRunning)
Packit 2fc92b
      CFRunLoopStop(CFRunLoopGetCurrent());
Packit 2fc92b
}
Packit 2fc92b
Packit 2fc92b
/*
Packit 2fc92b
 * 'list_device_cb()' - list_device iterator callback.
Packit 2fc92b
 */
Packit 2fc92b
static Boolean list_device_cb(io_service_t obj, printer_interface_t printerIntf, void *refcon)
Packit 2fc92b
{
Packit 2fc92b
  (void)refcon;
Packit 2fc92b
Packit 2fc92b
  if (obj != IO_OBJECT_NULL)
Packit 2fc92b
  {
Packit 2fc92b
    CFStringRef deviceIDString = NULL;
Packit 2fc92b
    CFStringRef make = NULL;
Packit 2fc92b
    CFStringRef model = NULL;
Packit 2fc92b
    CFStringRef serial = NULL;
Packit 2fc92b
    UInt32 intfLocation;
Packit 2fc92b
Packit 2fc92b
    deviceIDString = copy_printer_interface_deviceid(printerIntf, 0);
Packit 2fc92b
    if (deviceIDString == NULL)
Packit 2fc92b
      goto list_device_done;
Packit 2fc92b
Packit 2fc92b
    make = deviceIDCopyManufacturer(deviceIDString);
Packit 2fc92b
    model = deviceIDCopyModel(deviceIDString);
Packit 2fc92b
    serial = deviceIDCopySerialNumber(deviceIDString);
Packit 2fc92b
Packit 2fc92b
    char uristr[1024], makestr[1024], modelstr[1024], serialstr[1024];
Packit 2fc92b
    char optionsstr[1024], idstr[1024], make_modelstr[1024];
Packit 2fc92b
Packit 2fc92b
    CFStringGetCString(deviceIDString, idstr, sizeof(idstr), kCFStringEncodingUTF8);
Packit 2fc92b
    backendGetMakeModel(idstr, make_modelstr, sizeof(make_modelstr));
Packit 2fc92b
Packit 2fc92b
    modelstr[0] = '/';
Packit 2fc92b
Packit 2fc92b
    if (make  == NULL || !CFStringGetCString(make, makestr, sizeof(makestr), kCFStringEncodingUTF8))
Packit 2fc92b
      strlcpy(makestr, "Unknown", sizeof(makestr));
Packit 2fc92b
Packit 2fc92b
    if (model == NULL || !CFStringGetCString(model, &modelstr[1], sizeof(modelstr)-1, kCFStringEncodingUTF8))
Packit 2fc92b
      strlcpy(modelstr + 1, "Printer", sizeof(modelstr) - 1);
Packit 2fc92b
Packit 2fc92b
    optionsstr[0] = '\0';
Packit 2fc92b
    if (serial != NULL && CFStringGetCString(serial, serialstr, sizeof(serialstr), kCFStringEncodingUTF8))
Packit 2fc92b
      snprintf(optionsstr, sizeof(optionsstr), "?serial=%s", serialstr);
Packit 2fc92b
    else if ((*printerIntf)->GetLocationID(printerIntf, &intfLocation) == kIOReturnSuccess)
Packit 2fc92b
      snprintf(optionsstr, sizeof(optionsstr), "?location=%x", (unsigned)intfLocation);
Packit 2fc92b
Packit 2fc92b
    httpAssembleURI(HTTP_URI_CODING_ALL, uristr, sizeof(uristr), "usb", NULL, makestr, 0, modelstr);
Packit 2fc92b
    strlcat(uristr, optionsstr, sizeof(uristr));
Packit 2fc92b
Packit 2fc92b
    cupsBackendReport("direct", uristr, make_modelstr, make_modelstr, idstr,
Packit 2fc92b
                          NULL);
Packit 2fc92b
  list_device_done:
Packit 2fc92b
Packit 2fc92b
    if (make != NULL) CFRelease(make);
Packit 2fc92b
    if (model != NULL) CFRelease(model);
Packit 2fc92b
    if (serial != NULL) CFRelease(serial);
Packit 2fc92b
  }
Packit 2fc92b
  return obj != IO_OBJECT_NULL;
Packit 2fc92b
}
Packit 2fc92b
Packit 2fc92b
/*
Packit 2fc92b
 * 'find_device_cb()' - print_device iterator callback.
Packit 2fc92b
 */
Packit 2fc92b
static Boolean find_device_cb(io_service_t obj, printer_interface_t printerIntf, void *refcon)
Packit 2fc92b
{
Packit 2fc92b
  (void)refcon;
Packit 2fc92b
Packit 2fc92b
  Boolean keepLooking = true;
Packit 2fc92b
Packit 2fc92b
  if (obj != IO_OBJECT_NULL)
Packit 2fc92b
  {
Packit 2fc92b
    CFStringRef deviceIDString = NULL;
Packit 2fc92b
    CFStringRef make = NULL;
Packit 2fc92b
    CFStringRef model = NULL;
Packit 2fc92b
    CFStringRef serial = NULL;
Packit 2fc92b
Packit 2fc92b
    deviceIDString = copy_printer_interface_deviceid(printerIntf, 0);
Packit 2fc92b
    if (deviceIDString == NULL)
Packit 2fc92b
      goto find_device_done;
Packit 2fc92b
Packit 2fc92b
    make = deviceIDCopyManufacturer(deviceIDString);
Packit 2fc92b
    model = deviceIDCopyModel(deviceIDString);
Packit 2fc92b
    serial = deviceIDCopySerialNumber(deviceIDString);
Packit 2fc92b
Packit 2fc92b
    if (make && CFStringCompare(make, g.make, kCFCompareCaseInsensitive) == kCFCompareEqualTo)
Packit 2fc92b
    {
Packit 2fc92b
      if (model && CFStringCompare(model, g.model, kCFCompareCaseInsensitive) == kCFCompareEqualTo)
Packit 2fc92b
      {
Packit 2fc92b
        UInt8 intfAltSetting = 0, intfNumber = 0, intfProtocol = 0;
Packit 2fc92b
        UInt32 intfLocation = 0;
Packit 2fc92b
Packit 2fc92b
        (*printerIntf)->GetInterfaceProtocol(printerIntf, &intfProtocol);
Packit 2fc92b
        (*printerIntf)->GetAlternateSetting(printerIntf, &intfAltSetting);
Packit 2fc92b
        (*printerIntf)->GetInterfaceNumber(printerIntf, &intfNumber);
Packit 2fc92b
        (*printerIntf)->GetLocationID(printerIntf, &intfLocation);
Packit 2fc92b
Packit 2fc92b
        if (g.serial != NULL && CFStringGetLength(g.serial) > 0)
Packit 2fc92b
        {
Packit 2fc92b
          if (serial != NULL && CFStringCompare(serial, g.serial, kCFCompareCaseInsensitive) == kCFCompareEqualTo)
Packit 2fc92b
          {
Packit 2fc92b
            g.interfaceProtocol = intfProtocol;
Packit 2fc92b
            g.location = intfLocation;
Packit 2fc92b
            g.alternateSetting = intfAltSetting;
Packit 2fc92b
            if (intfProtocol != kUSBPrintingProtocolIPP)
Packit 2fc92b
            {
Packit 2fc92b
              g.printer_obj = obj;
Packit 2fc92b
              IOObjectRetain(obj);
Packit 2fc92b
            }
Packit 2fc92b
            keepLooking = (intfProtocol == kUSBPrintingProtocolIPP);
Packit 2fc92b
          }
Packit 2fc92b
        }
Packit 2fc92b
        else
Packit 2fc92b
        {
Packit 2fc92b
          if (g.printer_obj != 0)
Packit 2fc92b
            IOObjectRelease(g.printer_obj);
Packit 2fc92b
Packit 2fc92b
            g.location = intfLocation;
Packit 2fc92b
            g.alternateSetting = intfAltSetting;
Packit 2fc92b
            g.interfaceProtocol = intfProtocol;
Packit 2fc92b
            g.printer_obj = obj;
Packit 2fc92b
            IOObjectRetain(obj);
Packit 2fc92b
Packit 2fc92b
            if (g.location == 0 || g.location == intfLocation)
Packit 2fc92b
              keepLooking = false;
Packit 2fc92b
        }
Packit 2fc92b
Packit 2fc92b
        if (!keepLooking)
Packit 2fc92b
          g.interfaceNum = intfNumber;
Packit 2fc92b
      }
Packit 2fc92b
    }
Packit 2fc92b
Packit 2fc92b
  find_device_done:
Packit 2fc92b
    if (deviceIDString != NULL) CFRelease(deviceIDString);
Packit 2fc92b
    if (make != NULL) CFRelease(make);
Packit 2fc92b
    if (model != NULL) CFRelease(model);
Packit 2fc92b
    if (serial != NULL) CFRelease(serial);
Packit 2fc92b
  }
Packit 2fc92b
  else
Packit 2fc92b
  {
Packit 2fc92b
    keepLooking = (g.printer_obj == 0 && g.interfaceProtocol != kUSBPrintingProtocolIPP);
Packit 2fc92b
    if (obj == IO_OBJECT_NULL && keepLooking)
Packit 2fc92b
    {
Packit 2fc92b
      CFRunLoopTimerContext context = { 0, refcon, NULL, NULL, NULL };
Packit 2fc92b
      CFRunLoopTimerRef timer = CFRunLoopTimerCreate(NULL, CFAbsoluteTimeGetCurrent() + 1.0, 10, 0x0, 0x0, status_timer_cb, &context);
Packit 2fc92b
      if (timer != NULL)
Packit 2fc92b
      {
Packit 2fc92b
        CFRunLoopAddTimer(CFRunLoopGetCurrent(), timer, kCFRunLoopDefaultMode);
Packit 2fc92b
        g.status_timer = timer;
Packit 2fc92b
      }
Packit 2fc92b
    }
Packit 2fc92b
  }
Packit 2fc92b
Packit 2fc92b
  if (!keepLooking && g.status_timer != NULL)
Packit 2fc92b
  {
Packit 2fc92b
    fputs("STATE: -offline-report\n", stderr);
Packit 2fc92b
    _cupsLangPrintFilter(stderr, "INFO", _("The printer is now online."));
Packit 2fc92b
    CFRunLoopRemoveTimer(CFRunLoopGetCurrent(), g.status_timer, kCFRunLoopDefaultMode);
Packit 2fc92b
    CFRelease(g.status_timer);
Packit 2fc92b
    g.status_timer = NULL;
Packit 2fc92b
  }
Packit 2fc92b
Packit 2fc92b
  return keepLooking;
Packit 2fc92b
}
Packit 2fc92b
Packit 2fc92b
static CFStringRef deviceIDCopySerialNumber(CFStringRef deviceID)
Packit 2fc92b
{
Packit 2fc92b
    CFStringRef serialKeys[] = { CFSTR("SN:"),  CFSTR("SERN:"), NULL };
Packit 2fc92b
Packit 2fc92b
    return copy_value_for_key(deviceID, serialKeys);
Packit 2fc92b
}
Packit 2fc92b
Packit 2fc92b
static CFStringRef deviceIDCopyModel(CFStringRef deviceID)
Packit 2fc92b
{
Packit 2fc92b
    CFStringRef modelKeys[] = { CFSTR("MDL:"), CFSTR("MODEL:"), NULL };
Packit 2fc92b
    return copy_value_for_key(deviceID, modelKeys);
Packit 2fc92b
}
Packit 2fc92b
Packit 2fc92b
static CFStringRef deviceIDCopyManufacturer(CFStringRef deviceID)
Packit 2fc92b
{
Packit 2fc92b
    CFStringRef makeKeys[]   = { CFSTR("MFG:"), CFSTR("MANUFACTURER:"), NULL };
Packit 2fc92b
    return copy_value_for_key(deviceID, makeKeys);
Packit 2fc92b
}
Packit 2fc92b
Packit 2fc92b
/*
Packit 2fc92b
 * 'status_timer_cb()' - Status timer callback.
Packit 2fc92b
 */
Packit 2fc92b
Packit 2fc92b
static void status_timer_cb(CFRunLoopTimerRef timer,
Packit 2fc92b
			    void *info)
Packit 2fc92b
{
Packit 2fc92b
  (void)timer;
Packit 2fc92b
  (void)info;
Packit 2fc92b
Packit 2fc92b
  fputs("STATE: +offline-report\n", stderr);
Packit 2fc92b
  _cupsLangPrintFilter(stderr, "INFO", _("The printer is offline."));
Packit 2fc92b
Packit 2fc92b
  if (getenv("CLASS") != NULL)
Packit 2fc92b
  {
Packit 2fc92b
   /*
Packit 2fc92b
    * If the CLASS environment variable is set, the job was submitted
Packit 2fc92b
    * to a class and not to a specific queue.  In this case, we want
Packit 2fc92b
    * to abort immediately so that the job can be requeued on the next
Packit 2fc92b
    * available printer in the class.
Packit 2fc92b
    *
Packit 2fc92b
    * Sleep 5 seconds to keep the job from requeuing too rapidly...
Packit 2fc92b
    */
Packit 2fc92b
Packit 2fc92b
    sleep(5);
Packit 2fc92b
Packit 2fc92b
    exit(CUPS_BACKEND_FAILED);
Packit 2fc92b
  }
Packit 2fc92b
}
Packit 2fc92b
Packit 2fc92b
Packit 2fc92b
#pragma mark -
Packit 2fc92b
/*
Packit 2fc92b
 * 'load_classdriver()' - Load a classdriver.
Packit 2fc92b
 */
Packit 2fc92b
Packit 2fc92b
static kern_return_t load_classdriver(CFStringRef	    driverPath,
Packit 2fc92b
				      printer_interface_t   interface,
Packit 2fc92b
				      classdriver_t	    ***printerDriver)
Packit 2fc92b
{
Packit 2fc92b
  kern_return_t	kr = kUSBPrinterClassDeviceNotOpen;
Packit 2fc92b
  classdriver_t	**driver = NULL;
Packit 2fc92b
  CFStringRef	bundle = driverPath ? driverPath : kUSBGenericTOPrinterClassDriver;
Packit 2fc92b
  char 		bundlestr[1024];	/* Bundle path */
Packit 2fc92b
  CFURLRef	url;			/* URL for driver */
Packit 2fc92b
  CFPlugInRef	plugin = NULL;		/* Plug-in address */
Packit 2fc92b
Packit 2fc92b
Packit 2fc92b
  CFStringGetCString(bundle, bundlestr, sizeof(bundlestr), kCFStringEncodingUTF8);
Packit 2fc92b
Packit 2fc92b
 /*
Packit 2fc92b
  * Validate permissions for the class driver...
Packit 2fc92b
  */
Packit 2fc92b
Packit 2fc92b
  _cups_fc_result_t result = _cupsFileCheck(bundlestr,
Packit 2fc92b
                                            _CUPS_FILE_CHECK_DIRECTORY, 1,
Packit 2fc92b
                                            Iterating ? NULL : _cupsFileCheckFilter, NULL);
Packit 2fc92b
Packit 2fc92b
  if (result && driverPath)
Packit 2fc92b
    return (load_classdriver(NULL, interface, printerDriver));
Packit 2fc92b
  else if (result)
Packit 2fc92b
    return (kr);
Packit 2fc92b
Packit 2fc92b
 /*
Packit 2fc92b
  * Try loading the class driver...
Packit 2fc92b
  */
Packit 2fc92b
Packit 2fc92b
  url = CFURLCreateWithFileSystemPath(NULL, bundle, kCFURLPOSIXPathStyle, true);
Packit 2fc92b
Packit 2fc92b
  if (url)
Packit 2fc92b
  {
Packit 2fc92b
    plugin = CFPlugInCreate(NULL, url);
Packit 2fc92b
    CFRelease(url);
Packit 2fc92b
  }
Packit 2fc92b
  else
Packit 2fc92b
    plugin = NULL;
Packit 2fc92b
Packit 2fc92b
  if (plugin)
Packit 2fc92b
  {
Packit 2fc92b
    CFArrayRef factories = CFPlugInFindFactoriesForPlugInTypeInPlugIn(kUSBPrinterClassTypeID, plugin);
Packit 2fc92b
    if (factories != NULL && CFArrayGetCount(factories) > 0)
Packit 2fc92b
    {
Packit 2fc92b
      CFUUIDRef factoryID = CFArrayGetValueAtIndex(factories, 0);
Packit 2fc92b
      IUnknownVTbl **iunknown = CFPlugInInstanceCreate(NULL, factoryID, kUSBPrinterClassTypeID);
Packit 2fc92b
      if (iunknown != NULL)
Packit 2fc92b
      {
Packit 2fc92b
	kr = (*iunknown)->QueryInterface(iunknown, CFUUIDGetUUIDBytes(kUSBPrinterClassInterfaceID), (LPVOID *)&driver);
Packit 2fc92b
	if (kr == kIOReturnSuccess && driver != NULL)
Packit 2fc92b
	{
Packit 2fc92b
	  classdriver_t **genericDriver = NULL;
Packit 2fc92b
	  if (driverPath != NULL && CFStringCompare(driverPath, kUSBGenericTOPrinterClassDriver, 0) != kCFCompareEqualTo)
Packit 2fc92b
	    kr = load_classdriver(NULL, interface, &genericDriver);
Packit 2fc92b
Packit 2fc92b
	  if (kr == kIOReturnSuccess)
Packit 2fc92b
	  {
Packit 2fc92b
	    (*driver)->interface = interface;
Packit 2fc92b
	    (*driver)->Initialize(driver, genericDriver);
Packit 2fc92b
Packit 2fc92b
	    (*driver)->plugin = plugin;
Packit 2fc92b
	    (*driver)->interface = interface;
Packit 2fc92b
	    *printerDriver = driver;
Packit 2fc92b
	  }
Packit 2fc92b
	}
Packit 2fc92b
	(*iunknown)->Release(iunknown);
Packit 2fc92b
      }
Packit 2fc92b
      CFRelease(factories);
Packit 2fc92b
    }
Packit 2fc92b
  }
Packit 2fc92b
Packit 2fc92b
  fprintf(stderr, "DEBUG: load_classdriver(%s) (kr:0x%08x)\n", bundlestr, (int)kr);
Packit 2fc92b
Packit 2fc92b
  return (kr);
Packit 2fc92b
}
Packit 2fc92b
Packit 2fc92b
Packit 2fc92b
/*
Packit 2fc92b
 * 'unload_classdriver()' - Unload a classdriver.
Packit 2fc92b
 */
Packit 2fc92b
Packit 2fc92b
static kern_return_t unload_classdriver(classdriver_t ***classdriver)
Packit 2fc92b
{
Packit 2fc92b
  if (*classdriver != NULL)
Packit 2fc92b
  {
Packit 2fc92b
    (**classdriver)->Release(*classdriver);
Packit 2fc92b
    *classdriver = NULL;
Packit 2fc92b
  }
Packit 2fc92b
Packit 2fc92b
  return kIOReturnSuccess;
Packit 2fc92b
}
Packit 2fc92b
Packit 2fc92b
Packit 2fc92b
/*
Packit 2fc92b
 * 'load_printerdriver()' - Load vendor's classdriver.
Packit 2fc92b
 *
Packit 2fc92b
 * If driverBundlePath is not NULL on return it is the callers responsbility to release it!
Packit 2fc92b
 */
Packit 2fc92b
Packit 2fc92b
static kern_return_t load_printerdriver(CFStringRef *driverBundlePath)
Packit 2fc92b
{
Packit 2fc92b
  IOCFPlugInInterface	**iodev = NULL;
Packit 2fc92b
  SInt32		score;
Packit 2fc92b
  kern_return_t		kr;
Packit 2fc92b
  printer_interface_t	interface;
Packit 2fc92b
  HRESULT		res;
Packit 2fc92b
Packit 2fc92b
  kr = IOCreatePlugInInterfaceForService(g.printer_obj, kIOUSBInterfaceUserClientTypeID, kIOCFPlugInInterfaceID, &iodev, &score);
Packit 2fc92b
  if (kr == kIOReturnSuccess)
Packit 2fc92b
  {
Packit 2fc92b
    if ((res = (*iodev)->QueryInterface(iodev, USB_INTERFACE_KIND, (LPVOID *) &interface)) == noErr)
Packit 2fc92b
    {
Packit 2fc92b
      *driverBundlePath = IORegistryEntryCreateCFProperty(g.printer_obj, kUSBClassDriverProperty, NULL, kNilOptions);
Packit 2fc92b
Packit 2fc92b
      g.use_generic_class_driver = (*driverBundlePath == NULL || (CFStringCompare(*driverBundlePath, kUSBGenericTOPrinterClassDriver, 0x0) == kCFCompareEqualTo));
Packit 2fc92b
      kr = load_classdriver(*driverBundlePath, interface, &g.classdriver);
Packit 2fc92b
Packit 2fc92b
      if (kr != kIOReturnSuccess)
Packit 2fc92b
	(*interface)->Release(interface);
Packit 2fc92b
    }
Packit 2fc92b
    IODestroyPlugInInterface(iodev);
Packit 2fc92b
  }
Packit 2fc92b
  return kr;
Packit 2fc92b
}
Packit 2fc92b
Packit 2fc92b
static printer_interface_t usb_printer_interface_interface(io_service_t usbClass)
Packit 2fc92b
{
Packit 2fc92b
	printer_interface_t  intf = NULL;
Packit 2fc92b
	IOCFPlugInInterface **plugin = NULL;
Packit 2fc92b
	SInt32	score;
Packit 2fc92b
	int kr = IOCreatePlugInInterfaceForService(usbClass, kIOUSBInterfaceUserClientTypeID, kIOCFPlugInInterfaceID, &plugin, &score);
Packit 2fc92b
	if (kr == kIOReturnSuccess)
Packit 2fc92b
	{
Packit 2fc92b
		(*plugin)->QueryInterface(plugin, USB_INTERFACE_KIND, (LPVOID *)&intf;;
Packit 2fc92b
		IODestroyPlugInInterface(plugin);
Packit 2fc92b
	}
Packit 2fc92b
Packit 2fc92b
	return intf;
Packit 2fc92b
}
Packit 2fc92b
Packit 2fc92b
static CFStringRef copy_printer_interface_deviceid(printer_interface_t printer, UInt8 alternateSetting)
Packit 2fc92b
{
Packit 2fc92b
	// I have tried to make this function as neat as I can, but the possibility of needing to resend
Packit 2fc92b
	// a request to get the entire string makes it hideous...
Packit 2fc92b
	//
Packit 2fc92b
	// We package the job of sending a request up into the block (^sendRequest), which takes the size
Packit 2fc92b
	// it should allocate for the message buffer. It frees the current buffer if one is set and
Packit 2fc92b
	// allocates one of the specified size, then performs the request. We can then easily retry by
Packit 2fc92b
	// calling the block again if we fail to get the whole string the first time around.
Packit 2fc92b
Packit 2fc92b
	#define kUSBPrintClassGetDeviceID           0
Packit 2fc92b
	#define kDefaultNoDataTimeout               5000L
Packit 2fc92b
	#define pack_device_id_wIndex(intf, alt)  ((UInt16)((((UInt16)(intf)) << 8) | ((UInt8)(alt))))
Packit 2fc92b
Packit 2fc92b
	if (printer == NULL)
Packit 2fc92b
			return NULL;
Packit 2fc92b
Packit 2fc92b
Packit 2fc92b
	IOReturn err        = kIOReturnError;
Packit 2fc92b
	UInt8    configurationIndex	= 0;
Packit 2fc92b
	UInt8    interfaceNumber	= 0;
Packit 2fc92b
	size_t   bufferLength		= 256;
Packit 2fc92b
	CFStringRef ret             = NULL;
Packit 2fc92b
Packit 2fc92b
	if ((*printer)->GetConfigurationValue( printer, &configurationIndex) == kIOReturnSuccess &&
Packit 2fc92b
			(*printer)->GetInterfaceNumber( printer, &interfaceNumber) == kIOReturnSuccess)
Packit 2fc92b
	{
Packit 2fc92b
		__block IOUSBDevRequestTO	request;
Packit 2fc92b
		IOReturn (^sendRequest)(size_t) = ^ (size_t size)
Packit 2fc92b
		{
Packit 2fc92b
			if (request.pData)
Packit 2fc92b
			{
Packit 2fc92b
				free(request.pData);
Packit 2fc92b
				request.wLength = 0;
Packit 2fc92b
				request.pData = NULL;
Packit 2fc92b
			}
Packit 2fc92b
Packit 2fc92b
			IOReturn berr = kIOReturnError;
Packit 2fc92b
			char *buffer = malloc(size);
Packit 2fc92b
			if (buffer == NULL)
Packit 2fc92b
				return kIOReturnNoMemory;
Packit 2fc92b
Packit 2fc92b
			request.wLength = HostToUSBWord(size);
Packit 2fc92b
			request.pData = buffer;
Packit 2fc92b
			berr = (*printer)->ControlRequestTO(printer, (UInt8)0, &request);
Packit 2fc92b
			return berr;
Packit 2fc92b
		};
Packit 2fc92b
Packit 2fc92b
		/* This request takes the 0 based configuration index. IOKit returns a 1 based configuration index */
Packit 2fc92b
		configurationIndex -= 1;
Packit 2fc92b
Packit 2fc92b
		bzero(&request, sizeof(request));
Packit 2fc92b
Packit 2fc92b
		request.bmRequestType		= USBmakebmRequestType(kUSBIn, kUSBClass, kUSBInterface);
Packit 2fc92b
		request.bRequest			= kUSBPrintClassGetDeviceID;
Packit 2fc92b
		request.wValue				= HostToUSBWord(configurationIndex);
Packit 2fc92b
		request.wIndex				= HostToUSBWord(pack_device_id_wIndex(interfaceNumber, alternateSetting));
Packit 2fc92b
		request.noDataTimeout		= kDefaultNoDataTimeout;
Packit 2fc92b
		request.completionTimeout	= 0; // Copying behavior from Generic Class Driver
Packit 2fc92b
Packit 2fc92b
		err = sendRequest(bufferLength);
Packit 2fc92b
Packit 2fc92b
		if (err == kIOReturnSuccess && request.wLenDone > 1)
Packit 2fc92b
		{
Packit 2fc92b
			UInt16 actualLength = OSSwapBigToHostInt16(*((UInt16 *)request.pData));
Packit 2fc92b
Packit 2fc92b
			if (actualLength > 2 && actualLength <= bufferLength - 2)
Packit 2fc92b
			{
Packit 2fc92b
				ret = CFStringCreateWithBytes(NULL, (const UInt8 *) &request.pData[2], actualLength - 2, kCFStringEncodingUTF8, false);
Packit 2fc92b
			}
Packit 2fc92b
			else if (actualLength > 2) {
Packit 2fc92b
				err = sendRequest(actualLength);
Packit 2fc92b
				if (err == kIOReturnSuccess && request.wLenDone > 0)
Packit 2fc92b
				{
Packit 2fc92b
					actualLength = OSSwapBigToHostInt16(*((UInt16 *)request.pData));
Packit 2fc92b
					ret = CFStringCreateWithBytes(NULL, (const UInt8 *) &request.pData[2], actualLength - 2, kCFStringEncodingUTF8, false);
Packit 2fc92b
				}
Packit 2fc92b
			}
Packit 2fc92b
		}
Packit 2fc92b
Packit 2fc92b
		if (request.pData)
Packit 2fc92b
			free(request.pData);
Packit 2fc92b
	}
Packit 2fc92b
Packit 2fc92b
	CFStringRef manufacturer = deviceIDCopyManufacturer(ret);
Packit 2fc92b
	CFStringRef model = deviceIDCopyModel(ret);
Packit 2fc92b
	CFStringRef serial = deviceIDCopySerialNumber(ret);
Packit 2fc92b
Packit 2fc92b
	if (manufacturer == NULL || serial == NULL || model == NULL)
Packit 2fc92b
	{
Packit 2fc92b
		IOUSBDevRequestTO		request;
Packit 2fc92b
		IOUSBDeviceDescriptor	desc;
Packit 2fc92b
Packit 2fc92b
		bzero(&request, sizeof(request));
Packit 2fc92b
Packit 2fc92b
		request.bmRequestType = USBmakebmRequestType( kUSBIn,  kUSBStandard, kUSBDevice );
Packit 2fc92b
		request.bRequest = kUSBRqGetDescriptor;
Packit 2fc92b
		request.wValue = kUSBDeviceDesc << 8;
Packit 2fc92b
		request.wIndex = 0;
Packit 2fc92b
		request.wLength = sizeof(desc);
Packit 2fc92b
		request.pData = &des;;
Packit 2fc92b
		request.completionTimeout = 0;
Packit 2fc92b
		request.noDataTimeout = 60L;
Packit 2fc92b
Packit 2fc92b
		err = (*printer)->ControlRequestTO(printer, 0, &request);
Packit 2fc92b
		if (err == kIOReturnSuccess)
Packit 2fc92b
		{
Packit 2fc92b
			CFMutableStringRef extras = CFStringCreateMutable(NULL, 0);
Packit 2fc92b
			if (manufacturer == NULL)
Packit 2fc92b
			{
Packit 2fc92b
				manufacturer = copy_printer_interface_indexed_description(printer, desc.iManufacturer, kUSBLanguageEnglish);
Packit 2fc92b
				if (manufacturer && CFStringGetLength(manufacturer) > 0)
Packit 2fc92b
					CFStringAppendFormat(extras, NULL, CFSTR("MFG:%@;"), manufacturer);
Packit 2fc92b
			}
Packit 2fc92b
Packit 2fc92b
			if (model == NULL)
Packit 2fc92b
			{
Packit 2fc92b
				model = copy_printer_interface_indexed_description(printer, desc.iProduct, kUSBLanguageEnglish);
Packit 2fc92b
				if (model && CFStringGetLength(model) > 0)
Packit 2fc92b
					CFStringAppendFormat(extras, NULL, CFSTR("MDL:%@;"), model);
Packit 2fc92b
			}
Packit 2fc92b
Packit 2fc92b
			if (serial == NULL && desc.iSerialNumber != 0)
Packit 2fc92b
			{
Packit 2fc92b
				serial = copy_printer_interface_indexed_description(printer, desc.iSerialNumber, kUSBLanguageEnglish);
Packit 2fc92b
				if (serial && CFStringGetLength(serial) > 0)
Packit 2fc92b
					CFStringAppendFormat(extras, NULL, CFSTR("SERN:%@;"), serial);
Packit 2fc92b
			}
Packit 2fc92b
Packit 2fc92b
			if (ret != NULL)
Packit 2fc92b
			{
Packit 2fc92b
				CFStringAppend(extras, ret);
Packit 2fc92b
				CFRelease(ret);
Packit 2fc92b
Packit 2fc92b
				ret = extras;
Packit 2fc92b
			}
Packit 2fc92b
			else
Packit 2fc92b
			{
Packit 2fc92b
				ret = extras;
Packit 2fc92b
			}
Packit 2fc92b
		}
Packit 2fc92b
	}
Packit 2fc92b
Packit 2fc92b
	if (ret != NULL)
Packit 2fc92b
	{
Packit 2fc92b
	/* Remove special characters from the serial number */
Packit 2fc92b
	CFRange range = (serial != NULL ? CFStringFind(serial, CFSTR("+"), 0) : CFRangeMake(0, 0));
Packit 2fc92b
	if (range.length == 1)
Packit 2fc92b
	{
Packit 2fc92b
		range = CFStringFind(ret, serial, 0);
Packit 2fc92b
Packit 2fc92b
		CFMutableStringRef deviceIDString = CFStringCreateMutableCopy(NULL, 0, ret);
Packit 2fc92b
		CFRelease(ret);
Packit 2fc92b
Packit 2fc92b
		ret = deviceIDString;
Packit 2fc92b
		CFStringFindAndReplace(deviceIDString, CFSTR("+"), CFSTR(""), range, 0);
Packit 2fc92b
	}
Packit 2fc92b
	}
Packit 2fc92b
Packit 2fc92b
	if (manufacturer != NULL)
Packit 2fc92b
		CFRelease(manufacturer);
Packit 2fc92b
Packit 2fc92b
	if (model != NULL)
Packit 2fc92b
		CFRelease(model);
Packit 2fc92b
Packit 2fc92b
	if (serial != NULL)
Packit 2fc92b
		CFRelease(serial);
Packit 2fc92b
Packit 2fc92b
	if (ret != NULL && CFStringGetLength(ret) == 0)
Packit 2fc92b
	{
Packit 2fc92b
		CFRelease(ret);
Packit 2fc92b
		return NULL;
Packit 2fc92b
	}
Packit 2fc92b
Packit 2fc92b
	return ret;
Packit 2fc92b
}
Packit 2fc92b
Packit 2fc92b
static CFStringRef copy_printer_interface_indexed_description(printer_interface_t  printer, UInt8 index, UInt16 language)
Packit 2fc92b
{
Packit 2fc92b
	IOReturn err;
Packit 2fc92b
	UInt8 description[256]; // Max possible descriptor length
Packit 2fc92b
	IOUSBDevRequestTO	request;
Packit 2fc92b
Packit 2fc92b
	bzero(description, 2);
Packit 2fc92b
Packit 2fc92b
	request.bmRequestType = USBmakebmRequestType(kUSBIn, kUSBStandard, kUSBDevice);
Packit 2fc92b
	request.bRequest = kUSBRqGetDescriptor;
Packit 2fc92b
	request.wValue = (kUSBStringDesc << 8) | index;
Packit 2fc92b
	request.wIndex = language;
Packit 2fc92b
	request.wLength = 2;
Packit 2fc92b
	request.pData = &description;
Packit 2fc92b
	request.completionTimeout = 0;
Packit 2fc92b
	request.noDataTimeout = 60L;
Packit 2fc92b
Packit 2fc92b
	err = (*printer)->ControlRequestTO(printer, 0, &request);
Packit 2fc92b
	if (err != kIOReturnSuccess && err != kIOReturnOverrun)
Packit 2fc92b
	{
Packit 2fc92b
		bzero(description, request.wLength);
Packit 2fc92b
Packit 2fc92b
		// Let's try again full length. Here's why:
Packit 2fc92b
		//      On USB 2.0 controllers, we will not get an overrun error.  We just get a "babble" error
Packit 2fc92b
		//      and no valid data.  So, if we ask for the max size, we will either get it, or we'll get an underrun.
Packit 2fc92b
		//      It looks like we get it w/out an underrun
Packit 2fc92b
Packit 2fc92b
		request.bmRequestType = USBmakebmRequestType(kUSBIn, kUSBStandard, kUSBDevice);
Packit 2fc92b
		request.bRequest = kUSBRqGetDescriptor;
Packit 2fc92b
		request.wValue = (kUSBStringDesc << 8) | index;
Packit 2fc92b
		request.wIndex = language;
Packit 2fc92b
		request.wLength = sizeof description;
Packit 2fc92b
		request.pData = &description;
Packit 2fc92b
		request.completionTimeout = 0;
Packit 2fc92b
		request.noDataTimeout = 60L;
Packit 2fc92b
Packit 2fc92b
		err = (*printer)->ControlRequestTO(printer, 0, &request);
Packit 2fc92b
		if (err != kIOReturnSuccess && err != kIOReturnUnderrun)
Packit 2fc92b
			return NULL;
Packit 2fc92b
	}
Packit 2fc92b
Packit 2fc92b
	unsigned int length = description[0];
Packit 2fc92b
	if (length == 0)
Packit 2fc92b
		return CFStringCreateWithCString(NULL, "", kCFStringEncodingUTF8);
Packit 2fc92b
Packit 2fc92b
	if (description[1] != kUSBStringDesc)
Packit 2fc92b
		return NULL;
Packit 2fc92b
Packit 2fc92b
	request.bmRequestType = USBmakebmRequestType(kUSBIn, kUSBStandard, kUSBDevice);
Packit 2fc92b
	request.bRequest = kUSBRqGetDescriptor;
Packit 2fc92b
	request.wValue = (kUSBStringDesc << 8) | index;
Packit 2fc92b
	request.wIndex = language;
Packit 2fc92b
Packit 2fc92b
	bzero(description, length);
Packit 2fc92b
	request.wLength = (UInt16)length;
Packit 2fc92b
	request.pData = &description;
Packit 2fc92b
	request.completionTimeout = 0;
Packit 2fc92b
	request.noDataTimeout = 60L;
Packit 2fc92b
Packit 2fc92b
	err = (*printer)->ControlRequestTO(printer, 0, &request);
Packit 2fc92b
	if (err != kIOReturnSuccess)
Packit 2fc92b
		return NULL;
Packit 2fc92b
Packit 2fc92b
	if (description[1] != kUSBStringDesc)
Packit 2fc92b
		return NULL;
Packit 2fc92b
Packit 2fc92b
	if ((description[0] & 1) != 0)
Packit 2fc92b
		description[0] &= 0xfe;
Packit 2fc92b
Packit 2fc92b
	char buffer[258] = {};
Packit 2fc92b
	unsigned int maxLength = sizeof buffer;
Packit 2fc92b
	if (description[0] > 1)
Packit 2fc92b
	{
Packit 2fc92b
		length = (description[0]-2)/2;
Packit 2fc92b
Packit 2fc92b
		if (length > maxLength - 1)
Packit 2fc92b
			length = maxLength -1;
Packit 2fc92b
Packit 2fc92b
		for (unsigned i = 0; i < length; i++)
Packit 2fc92b
			buffer[i] = (char) description[2*i+2];
Packit 2fc92b
Packit 2fc92b
		buffer[length] = 0;
Packit 2fc92b
	}
Packit 2fc92b
Packit 2fc92b
	return CFStringCreateWithCString(NULL, buffer, kCFStringEncodingUTF8);
Packit 2fc92b
}
Packit 2fc92b
Packit 2fc92b
/*
Packit 2fc92b
 * 'registry_open()' - Open a connection to the printer.
Packit 2fc92b
 */
Packit 2fc92b
Packit 2fc92b
static kern_return_t registry_open(CFStringRef *driverBundlePath)
Packit 2fc92b
{
Packit 2fc92b
  g.bidi_flag = 0;	/* 0=unidirectional */
Packit 2fc92b
Packit 2fc92b
  kern_return_t kr = load_printerdriver(driverBundlePath);
Packit 2fc92b
  if (kr != kIOReturnSuccess)
Packit 2fc92b
    kr = -2;
Packit 2fc92b
Packit 2fc92b
  if (g.classdriver != NULL)
Packit 2fc92b
  {
Packit 2fc92b
  	(*g.classdriver)->interfaceNumber = g.interfaceNum;
Packit 2fc92b
    kr = (*g.classdriver)->Open(g.classdriver, g.location, kUSBPrintingProtocolBidirectional);
Packit 2fc92b
    if (kr != kIOReturnSuccess || (*g.classdriver)->interface == NULL)
Packit 2fc92b
    {
Packit 2fc92b
      kr = (*g.classdriver)->Open(g.classdriver, g.location, kUSBPrintingProtocolUnidirectional);
Packit 2fc92b
      if (kr == kIOReturnSuccess)
Packit 2fc92b
      {
Packit 2fc92b
	if ((*g.classdriver)->interface == NULL)
Packit 2fc92b
	{
Packit 2fc92b
	  (*g.classdriver)->Close(g.classdriver);
Packit 2fc92b
	  kr = -1;
Packit 2fc92b
	}
Packit 2fc92b
      }
Packit 2fc92b
    }
Packit 2fc92b
    else
Packit 2fc92b
      g.bidi_flag = 1;	/* 1=bidirectional */
Packit 2fc92b
  }
Packit 2fc92b
Packit 2fc92b
  if (kr != kIOReturnSuccess)
Packit 2fc92b
    unload_classdriver(&g.classdriver);
Packit 2fc92b
Packit 2fc92b
  return kr;
Packit 2fc92b
}
Packit 2fc92b
Packit 2fc92b
Packit 2fc92b
/*
Packit 2fc92b
 * 'registry_close()' - Close the connection to the printer.
Packit 2fc92b
 */
Packit 2fc92b
Packit 2fc92b
static kern_return_t registry_close(void)
Packit 2fc92b
{
Packit 2fc92b
  if (g.classdriver != NULL)
Packit 2fc92b
    (*g.classdriver)->Close(g.classdriver);
Packit 2fc92b
Packit 2fc92b
  unload_classdriver(&g.classdriver);
Packit 2fc92b
  return kIOReturnSuccess;
Packit 2fc92b
}
Packit 2fc92b
Packit 2fc92b
#pragma mark -
Packit 2fc92b
/*
Packit 2fc92b
 * 'copy_value_for_key()' - Copy value string associated with a key.
Packit 2fc92b
 */
Packit 2fc92b
Packit 2fc92b
static CFStringRef copy_value_for_key(CFStringRef deviceID,
Packit 2fc92b
				      CFStringRef *keys)
Packit 2fc92b
{
Packit 2fc92b
  CFStringRef	value = NULL;
Packit 2fc92b
  CFArrayRef	kvPairs = deviceID != NULL ? CFStringCreateArrayBySeparatingStrings(NULL, deviceID, CFSTR(";")) : NULL;
Packit 2fc92b
  CFIndex	max = kvPairs != NULL ? CFArrayGetCount(kvPairs) : 0;
Packit 2fc92b
  CFIndex	idx = 0;
Packit 2fc92b
Packit 2fc92b
  while (idx < max && value == NULL)
Packit 2fc92b
  {
Packit 2fc92b
    CFStringRef kvpair = CFArrayGetValueAtIndex(kvPairs, idx);
Packit 2fc92b
    CFIndex idxx = 0;
Packit 2fc92b
    while (keys[idxx] != NULL && value == NULL)
Packit 2fc92b
    {
Packit 2fc92b
      CFRange range = CFStringFind(kvpair, keys[idxx], kCFCompareCaseInsensitive);
Packit 2fc92b
      if (range.length != -1)
Packit 2fc92b
      {
Packit 2fc92b
	if (range.location != 0)
Packit 2fc92b
	{
Packit 2fc92b
	  CFMutableStringRef theString = CFStringCreateMutableCopy(NULL, 0, kvpair);
Packit 2fc92b
	  CFStringTrimWhitespace(theString);
Packit 2fc92b
	  range = CFStringFind(theString, keys[idxx], kCFCompareCaseInsensitive);
Packit 2fc92b
	  if (range.location == 0)
Packit 2fc92b
	    value = CFStringCreateWithSubstring(NULL, theString, CFRangeMake(range.length, CFStringGetLength(theString) - range.length));
Packit 2fc92b
Packit 2fc92b
	  CFRelease(theString);
Packit 2fc92b
	}
Packit 2fc92b
	else
Packit 2fc92b
	{
Packit 2fc92b
	  CFStringRef theString = CFStringCreateWithSubstring(NULL, kvpair, CFRangeMake(range.length, CFStringGetLength(kvpair) - range.length));
Packit 2fc92b
	  CFMutableStringRef theString2 = CFStringCreateMutableCopy(NULL, 0, theString);
Packit 2fc92b
	  CFRelease(theString);
Packit 2fc92b
Packit 2fc92b
	  CFStringTrimWhitespace(theString2);
Packit 2fc92b
	  value = theString2;
Packit 2fc92b
	}
Packit 2fc92b
      }
Packit 2fc92b
      idxx++;
Packit 2fc92b
    }
Packit 2fc92b
    idx++;
Packit 2fc92b
  }
Packit 2fc92b
Packit 2fc92b
  if (kvPairs != NULL)
Packit 2fc92b
    CFRelease(kvPairs);
Packit 2fc92b
  return value;
Packit 2fc92b
}
Packit 2fc92b
Packit 2fc92b
Packit 2fc92b
/*
Packit 2fc92b
 * 'cfstr_create_trim()' - Create CFString and trim whitespace characters.
Packit 2fc92b
 */
Packit 2fc92b
Packit 2fc92b
CFStringRef cfstr_create_trim(const char *cstr)
Packit 2fc92b
{
Packit 2fc92b
  CFStringRef		cfstr;
Packit 2fc92b
  CFMutableStringRef	cfmutablestr = NULL;
Packit 2fc92b
Packit 2fc92b
  if ((cfstr = CFStringCreateWithCString(NULL, cstr, kCFStringEncodingUTF8)) != NULL)
Packit 2fc92b
  {
Packit 2fc92b
    if ((cfmutablestr = CFStringCreateMutableCopy(NULL, 1024, cfstr)) != NULL)
Packit 2fc92b
      CFStringTrimWhitespace(cfmutablestr);
Packit 2fc92b
Packit 2fc92b
    CFRelease(cfstr);
Packit 2fc92b
  }
Packit 2fc92b
  return (CFStringRef) cfmutablestr;
Packit 2fc92b
}
Packit 2fc92b
Packit 2fc92b
Packit 2fc92b
#pragma mark -
Packit 2fc92b
/*
Packit 2fc92b
 * 'parse_options()' - Parse URI options.
Packit 2fc92b
 */
Packit 2fc92b
Packit 2fc92b
static void parse_options(char *options,
Packit 2fc92b
			  char *serial,
Packit 2fc92b
			  int serial_size,
Packit 2fc92b
			  UInt32 *location,
Packit 2fc92b
			  Boolean *wait_eof)
Packit 2fc92b
{
Packit 2fc92b
  char	sep,				/* Separator character */
Packit 2fc92b
	*name,				/* Name of option */
Packit 2fc92b
	*value;				/* Value of option */
Packit 2fc92b
Packit 2fc92b
Packit 2fc92b
  if (serial)
Packit 2fc92b
    *serial = '\0';
Packit 2fc92b
  if (location)
Packit 2fc92b
    *location = 0;
Packit 2fc92b
Packit 2fc92b
  if (!options)
Packit 2fc92b
    return;
Packit 2fc92b
Packit 2fc92b
  while (*options)
Packit 2fc92b
  {
Packit 2fc92b
   /*
Packit 2fc92b
    * Get the name...
Packit 2fc92b
    */
Packit 2fc92b
Packit 2fc92b
    name = options;
Packit 2fc92b
Packit 2fc92b
    while (*options && *options != '=' && *options != '+' && *options != '&')
Packit 2fc92b
      options ++;
Packit 2fc92b
Packit 2fc92b
    if ((sep = *options) != '\0')
Packit 2fc92b
      *options++ = '\0';
Packit 2fc92b
Packit 2fc92b
    if (sep == '=')
Packit 2fc92b
    {
Packit 2fc92b
     /*
Packit 2fc92b
      * Get the value...
Packit 2fc92b
      */
Packit 2fc92b
Packit 2fc92b
      value = options;
Packit 2fc92b
Packit 2fc92b
      while (*options && *options != '+' && *options != '&')
Packit 2fc92b
	options ++;
Packit 2fc92b
Packit 2fc92b
      if (*options)
Packit 2fc92b
	*options++ = '\0';
Packit 2fc92b
    }
Packit 2fc92b
    else
Packit 2fc92b
      value = (char *)"";
Packit 2fc92b
Packit 2fc92b
   /*
Packit 2fc92b
    * Process the option...
Packit 2fc92b
    */
Packit 2fc92b
Packit 2fc92b
    if (!_cups_strcasecmp(name, "waiteof"))
Packit 2fc92b
    {
Packit 2fc92b
      if (!_cups_strcasecmp(value, "on") ||
Packit 2fc92b
	  !_cups_strcasecmp(value, "yes") ||
Packit 2fc92b
	  !_cups_strcasecmp(value, "true"))
Packit 2fc92b
	*wait_eof = true;
Packit 2fc92b
      else if (!_cups_strcasecmp(value, "off") ||
Packit 2fc92b
	       !_cups_strcasecmp(value, "no") ||
Packit 2fc92b
	       !_cups_strcasecmp(value, "false"))
Packit 2fc92b
	*wait_eof = false;
Packit 2fc92b
      else
Packit 2fc92b
	_cupsLangPrintFilter(stderr, "WARNING",
Packit 2fc92b
			     _("Boolean expected for waiteof option \"%s\"."),
Packit 2fc92b
			     value);
Packit 2fc92b
    }
Packit 2fc92b
    else if (!_cups_strcasecmp(name, "serial"))
Packit 2fc92b
      strlcpy(serial, value, serial_size);
Packit 2fc92b
    else if (!_cups_strcasecmp(name, "location") && location)
Packit 2fc92b
      *location = (UInt32)strtoul(value, NULL, 16);
Packit 2fc92b
  }
Packit 2fc92b
}
Packit 2fc92b
Packit 2fc92b
Packit 2fc92b
/*!
Packit 2fc92b
 * @function	setup_cfLanguage
Packit 2fc92b
 * @abstract	Convert the contents of the CUPS 'APPLE_LANGUAGE' environment
Packit 2fc92b
 *		variable into a one element CF array of languages.
Packit 2fc92b
 *
Packit 2fc92b
 * @discussion	Each submitted job comes with a natural language. CUPS passes
Packit 2fc92b
 * 		that language in an environment variable. We take that language
Packit 2fc92b
 * 		and jam it into the AppleLanguages array so that CF will use
Packit 2fc92b
 * 		it when reading localized resources. We need to do this before
Packit 2fc92b
 * 		any CF code reads and caches the languages array, so this function
Packit 2fc92b
 *		should be called early in main()
Packit 2fc92b
 */
Packit 2fc92b
static void setup_cfLanguage(void)
Packit 2fc92b
{
Packit 2fc92b
  CFStringRef	lang[1] = {NULL};
Packit 2fc92b
  CFArrayRef	langArray = NULL;
Packit 2fc92b
  const char	*requestedLang = NULL;
Packit 2fc92b
Packit 2fc92b
  if ((requestedLang = getenv("APPLE_LANGUAGE")) == NULL)
Packit 2fc92b
    requestedLang = getenv("LANG");
Packit 2fc92b
Packit 2fc92b
  if (requestedLang != NULL)
Packit 2fc92b
  {
Packit 2fc92b
    lang[0] = CFStringCreateWithCString(kCFAllocatorDefault, requestedLang, kCFStringEncodingUTF8);
Packit 2fc92b
    langArray = CFArrayCreate(kCFAllocatorDefault, (const void **)lang, sizeof(lang) / sizeof(lang[0]), &kCFTypeArrayCallBacks);
Packit 2fc92b
Packit 2fc92b
    CFPreferencesSetValue(CFSTR("AppleLanguages"), langArray, kCFPreferencesCurrentApplication, kCFPreferencesAnyUser, kCFPreferencesAnyHost);
Packit 2fc92b
    fprintf(stderr, "DEBUG: usb: AppleLanguages=\"%s\"\n", requestedLang);
Packit 2fc92b
Packit 2fc92b
    CFRelease(lang[0]);
Packit 2fc92b
    CFRelease(langArray);
Packit 2fc92b
  }
Packit 2fc92b
  else
Packit 2fc92b
    fputs("DEBUG: usb: LANG and APPLE_LANGUAGE environment variables missing.\n", stderr);
Packit 2fc92b
}
Packit 2fc92b
Packit 2fc92b
#pragma mark -
Packit 2fc92b
#if defined(__i386__) || defined(__x86_64__)
Packit 2fc92b
/*!
Packit 2fc92b
 * @function	run_legacy_backend
Packit 2fc92b
 *
Packit 2fc92b
 * @abstract	Starts child backend process running as a ppc or i386 executable.
Packit 2fc92b
 *
Packit 2fc92b
 * @result	Never returns; always calls exit().
Packit 2fc92b
 *
Packit 2fc92b
 * @discussion
Packit 2fc92b
 */
Packit 2fc92b
static void run_legacy_backend(int argc,
Packit 2fc92b
			       char *argv[],
Packit 2fc92b
			       int fd)
Packit 2fc92b
{
Packit 2fc92b
  int	i;
Packit 2fc92b
  int	exitstatus = 0;
Packit 2fc92b
  int	childstatus;
Packit 2fc92b
  pid_t	waitpid_status;
Packit 2fc92b
  char	*my_argv[32];
Packit 2fc92b
  char	*usb_legacy_status;
Packit 2fc92b
Packit 2fc92b
Packit 2fc92b
 /*
Packit 2fc92b
  * If we're running as x86_64 or i386 and couldn't load the class driver
Packit 2fc92b
  * (because it's ppc or i386), then try to re-exec ourselves in ppc or i386
Packit 2fc92b
  * mode to try again. If we don't have a ppc or i386 architecture we may be
Packit 2fc92b
  * running with the same architecture again so guard against this by setting
Packit 2fc92b
  * and testing an environment variable...
Packit 2fc92b
  */
Packit 2fc92b
Packit 2fc92b
#  ifdef __x86_64__
Packit 2fc92b
  usb_legacy_status = getenv("USB_I386_STATUS");
Packit 2fc92b
#  else
Packit 2fc92b
  usb_legacy_status = getenv("USB_PPC_STATUS");
Packit 2fc92b
#  endif /* __x86_64__ */
Packit 2fc92b
Packit 2fc92b
  if (!usb_legacy_status)
Packit 2fc92b
  {
Packit 2fc92b
   /*
Packit 2fc92b
    * Setup a SIGTERM handler then block it before forking...
Packit 2fc92b
    */
Packit 2fc92b
Packit 2fc92b
    int			err;		/* posix_spawn result */
Packit 2fc92b
    struct sigaction	action;		/* POSIX signal action */
Packit 2fc92b
    sigset_t		newmask,	/* New signal mask */
Packit 2fc92b
			oldmask;	/* Old signal mask */
Packit 2fc92b
    char		usbpath[1024];	/* Path to USB backend */
Packit 2fc92b
    const char		*cups_serverbin;/* Path to CUPS binaries */
Packit 2fc92b
Packit 2fc92b
Packit 2fc92b
    memset(&action, 0, sizeof(action));
Packit 2fc92b
    sigaddset(&action.sa_mask, SIGTERM);
Packit 2fc92b
    action.sa_handler = sigterm_handler;
Packit 2fc92b
    sigaction(SIGTERM, &action, NULL);
Packit 2fc92b
Packit 2fc92b
    sigemptyset(&newmask);
Packit 2fc92b
    sigaddset(&newmask, SIGTERM);
Packit 2fc92b
    sigprocmask(SIG_BLOCK, &newmask, &oldmask);
Packit 2fc92b
Packit 2fc92b
   /*
Packit 2fc92b
    * Set the environment variable...
Packit 2fc92b
    */
Packit 2fc92b
Packit 2fc92b
#  ifdef __x86_64__
Packit 2fc92b
    setenv("USB_I386_STATUS", "1", false);
Packit 2fc92b
#  else
Packit 2fc92b
    setenv("USB_PPC_STATUS", "1", false);
Packit 2fc92b
#  endif /* __x86_64__ */
Packit 2fc92b
Packit 2fc92b
   /*
Packit 2fc92b
    * Tell the kernel to use the specified CPU architecture...
Packit 2fc92b
    */
Packit 2fc92b
Packit 2fc92b
#  ifdef __x86_64__
Packit 2fc92b
    cpu_type_t cpu = CPU_TYPE_I386;
Packit 2fc92b
#  else
Packit 2fc92b
    cpu_type_t cpu = CPU_TYPE_POWERPC;
Packit 2fc92b
#  endif /* __x86_64__ */
Packit 2fc92b
    size_t ocount = 1;
Packit 2fc92b
    posix_spawnattr_t attrs;
Packit 2fc92b
Packit 2fc92b
    if (!posix_spawnattr_init(&attrs))
Packit 2fc92b
    {
Packit 2fc92b
      posix_spawnattr_setsigdefault(&attrs, &oldmask);
Packit 2fc92b
      if (posix_spawnattr_setbinpref_np(&attrs, 1, &cpu, &ocount) || ocount != 1)
Packit 2fc92b
      {
Packit 2fc92b
#  ifdef __x86_64__
Packit 2fc92b
	perror("DEBUG: Unable to set binary preference to i386");
Packit 2fc92b
#  else
Packit 2fc92b
	perror("DEBUG: Unable to set binary preference to ppc");
Packit 2fc92b
#  endif /* __x86_64__ */
Packit 2fc92b
	_cupsLangPrintFilter(stderr, "ERROR",
Packit 2fc92b
	                     _("Unable to use legacy USB class driver."));
Packit 2fc92b
	exit(CUPS_BACKEND_STOP);
Packit 2fc92b
      }
Packit 2fc92b
    }
Packit 2fc92b
Packit 2fc92b
   /*
Packit 2fc92b
    * Set up the arguments and call posix_spawn...
Packit 2fc92b
    */
Packit 2fc92b
Packit 2fc92b
    if ((cups_serverbin = getenv("CUPS_SERVERBIN")) == NULL)
Packit 2fc92b
      cups_serverbin = CUPS_SERVERBIN;
Packit 2fc92b
    snprintf(usbpath, sizeof(usbpath), "%s/backend/usb", cups_serverbin);
Packit 2fc92b
Packit 2fc92b
    for (i = 0; i < argc && i < (int)(sizeof(my_argv) / sizeof(my_argv[0])) - 1; i ++)
Packit 2fc92b
      my_argv[i] = argv[i];
Packit 2fc92b
Packit 2fc92b
    my_argv[i] = NULL;
Packit 2fc92b
Packit 2fc92b
    if ((err = posix_spawn(&child_pid, usbpath, NULL, &attrs, my_argv,
Packit 2fc92b
                           environ)) != 0)
Packit 2fc92b
    {
Packit 2fc92b
      fprintf(stderr, "DEBUG: Unable to exec %s: %s\n", usbpath,
Packit 2fc92b
              strerror(err));
Packit 2fc92b
      _cupsLangPrintFilter(stderr, "ERROR",
Packit 2fc92b
                           _("Unable to use legacy USB class driver."));
Packit 2fc92b
      exit(CUPS_BACKEND_STOP);
Packit 2fc92b
    }
Packit 2fc92b
Packit 2fc92b
   /*
Packit 2fc92b
    * Unblock signals...
Packit 2fc92b
    */
Packit 2fc92b
Packit 2fc92b
    sigprocmask(SIG_SETMASK, &oldmask, NULL);
Packit 2fc92b
Packit 2fc92b
   /*
Packit 2fc92b
    * Close the fds we won't be using then wait for the child backend to exit.
Packit 2fc92b
    */
Packit 2fc92b
Packit 2fc92b
    close(fd);
Packit 2fc92b
    close(1);
Packit 2fc92b
Packit 2fc92b
    fprintf(stderr, "DEBUG: Started usb(legacy) backend (PID %d)\n",
Packit 2fc92b
            (int)child_pid);
Packit 2fc92b
Packit 2fc92b
    while ((waitpid_status = waitpid(child_pid, &childstatus, 0)) == (pid_t)-1 && errno == EINTR)
Packit 2fc92b
      usleep(1000);
Packit 2fc92b
Packit 2fc92b
    if (WIFSIGNALED(childstatus))
Packit 2fc92b
    {
Packit 2fc92b
      exitstatus = CUPS_BACKEND_STOP;
Packit 2fc92b
      fprintf(stderr, "DEBUG: usb(legacy) backend %d crashed on signal %d\n",
Packit 2fc92b
              child_pid, WTERMSIG(childstatus));
Packit 2fc92b
    }
Packit 2fc92b
    else
Packit 2fc92b
    {
Packit 2fc92b
      if ((exitstatus = WEXITSTATUS(childstatus)) != 0)
Packit 2fc92b
	fprintf(stderr,
Packit 2fc92b
	        "DEBUG: usb(legacy) backend %d stopped with status %d\n",
Packit 2fc92b
		child_pid, exitstatus);
Packit 2fc92b
      else
Packit 2fc92b
	fprintf(stderr, "DEBUG: usb(legacy) backend %d exited with no errors\n",
Packit 2fc92b
	        child_pid);
Packit 2fc92b
    }
Packit 2fc92b
  }
Packit 2fc92b
  else
Packit 2fc92b
  {
Packit 2fc92b
    fputs("DEBUG: usb(legacy) backend running native again\n", stderr);
Packit 2fc92b
    exitstatus = CUPS_BACKEND_STOP;
Packit 2fc92b
  }
Packit 2fc92b
Packit 2fc92b
  exit(exitstatus);
Packit 2fc92b
}
Packit 2fc92b
#endif /* __i386__ || __x86_64__ */
Packit 2fc92b
Packit 2fc92b
Packit 2fc92b
/*
Packit 2fc92b
 * 'sigterm_handler()' - SIGTERM handler.
Packit 2fc92b
 */
Packit 2fc92b
Packit 2fc92b
static void
Packit 2fc92b
sigterm_handler(int sig)		/* I - Signal */
Packit 2fc92b
{
Packit 2fc92b
#if defined(__i386__) || defined(__x86_64__)
Packit 2fc92b
 /*
Packit 2fc92b
  * If we started a child process pass the signal on to it...
Packit 2fc92b
  */
Packit 2fc92b
Packit 2fc92b
  if (child_pid)
Packit 2fc92b
  {
Packit 2fc92b
   /*
Packit 2fc92b
    * If we started a child process pass the signal on to it...
Packit 2fc92b
    */
Packit 2fc92b
Packit 2fc92b
    int	status;
Packit 2fc92b
Packit 2fc92b
    kill(child_pid, sig);
Packit 2fc92b
    while (waitpid(child_pid, &status, 0) < 0 && errno == EINTR);
Packit 2fc92b
Packit 2fc92b
    if (WIFEXITED(status))
Packit 2fc92b
      _exit(WEXITSTATUS(status));
Packit 2fc92b
    else if (status == SIGTERM || status == SIGKILL)
Packit 2fc92b
      _exit(0);
Packit 2fc92b
    else
Packit 2fc92b
    {
Packit 2fc92b
      write(2, "DEBUG: Child crashed.\n", 22);
Packit 2fc92b
      _exit(CUPS_BACKEND_STOP);
Packit 2fc92b
    }
Packit 2fc92b
  }
Packit 2fc92b
#endif /* __i386__ || __x86_64__ */
Packit 2fc92b
}
Packit 2fc92b
Packit 2fc92b
Packit 2fc92b
/*
Packit 2fc92b
 * 'sigquit_handler()' - SIGQUIT handler.
Packit 2fc92b
 */
Packit 2fc92b
Packit 2fc92b
static void sigquit_handler(int sig, siginfo_t *si, void *unused)
Packit 2fc92b
{
Packit 2fc92b
  char  *path;
Packit 2fc92b
  char	pathbuf[PROC_PIDPATHINFO_MAXSIZE];
Packit 2fc92b
  static char msgbuf[256] = "";
Packit 2fc92b
Packit 2fc92b
Packit 2fc92b
  (void)sig;
Packit 2fc92b
  (void)unused;
Packit 2fc92b
Packit 2fc92b
  if (proc_pidpath(si->si_pid, pathbuf, sizeof(pathbuf)) > 0 &&
Packit 2fc92b
      (path = basename(pathbuf)) != NULL)
Packit 2fc92b
    snprintf(msgbuf, sizeof(msgbuf), "SIGQUIT sent by %s(%d)", path, (int)si->si_pid);
Packit 2fc92b
  else
Packit 2fc92b
    snprintf(msgbuf, sizeof(msgbuf), "SIGQUIT sent by PID %d", (int)si->si_pid);
Packit 2fc92b
Packit 2fc92b
  CRSetCrashLogMessage(msgbuf);
Packit 2fc92b
Packit 2fc92b
  abort();
Packit 2fc92b
}
Packit 2fc92b
Packit 2fc92b
Packit 2fc92b
#ifdef PARSE_PS_ERRORS
Packit 2fc92b
/*
Packit 2fc92b
 * 'next_line()' - Find the next line in a buffer.
Packit 2fc92b
 */
Packit 2fc92b
Packit 2fc92b
static const char *next_line (const char *buffer)
Packit 2fc92b
{
Packit 2fc92b
  const char *cptr, *lptr = NULL;
Packit 2fc92b
Packit 2fc92b
  for (cptr = buffer; *cptr && lptr == NULL; cptr++)
Packit 2fc92b
    if (*cptr == '\n' || *cptr == '\r')
Packit 2fc92b
      lptr = cptr;
Packit 2fc92b
  return lptr;
Packit 2fc92b
}
Packit 2fc92b
Packit 2fc92b
Packit 2fc92b
/*
Packit 2fc92b
 * 'parse_pserror()' - Scan the backchannel data for postscript errors.
Packit 2fc92b
 */
Packit 2fc92b
Packit 2fc92b
static void parse_pserror(char *sockBuffer,
Packit 2fc92b
			  int len)
Packit 2fc92b
{
Packit 2fc92b
  static char  gErrorBuffer[1024] = "";
Packit 2fc92b
  static char *gErrorBufferPtr = gErrorBuffer;
Packit 2fc92b
  static char *gErrorBufferEndPtr = gErrorBuffer + sizeof(gErrorBuffer);
Packit 2fc92b
Packit 2fc92b
  char *pCommentBegin, *pCommentEnd, *pLineEnd;
Packit 2fc92b
  char *logLevel;
Packit 2fc92b
  char logstr[1024];
Packit 2fc92b
  int  logstrlen;
Packit 2fc92b
Packit 2fc92b
  if (gErrorBufferPtr + len > gErrorBufferEndPtr - 1)
Packit 2fc92b
    gErrorBufferPtr = gErrorBuffer;
Packit 2fc92b
  if (len > sizeof(gErrorBuffer) - 1)
Packit 2fc92b
    len = sizeof(gErrorBuffer) - 1;
Packit 2fc92b
Packit 2fc92b
  memcpy(gErrorBufferPtr, (const void *)sockBuffer, len);
Packit 2fc92b
  gErrorBufferPtr += len;
Packit 2fc92b
  *(gErrorBufferPtr + 1) = '\0';
Packit 2fc92b
Packit 2fc92b
  pLineEnd = (char *)next_line((const char *)gErrorBuffer);
Packit 2fc92b
  while (pLineEnd != NULL)
Packit 2fc92b
  {
Packit 2fc92b
    *pLineEnd++ = '\0';
Packit 2fc92b
Packit 2fc92b
    pCommentBegin = strstr(gErrorBuffer,"%%[");
Packit 2fc92b
    pCommentEnd = strstr(gErrorBuffer, "]%%");
Packit 2fc92b
    if (pCommentBegin != gErrorBuffer && pCommentEnd != NULL)
Packit 2fc92b
    {
Packit 2fc92b
      pCommentEnd += 3;            /* Skip past "]%%" */
Packit 2fc92b
      *pCommentEnd = '\0';         /* There's always room for the nul */
Packit 2fc92b
Packit 2fc92b
      if (_cups_strncasecmp(pCommentBegin, "%%[ Error:", 10) == 0)
Packit 2fc92b
	logLevel = "DEBUG";
Packit 2fc92b
      else if (_cups_strncasecmp(pCommentBegin, "%%[ Flushing", 12) == 0)
Packit 2fc92b
	logLevel = "DEBUG";
Packit 2fc92b
      else
Packit 2fc92b
	logLevel = "INFO";
Packit 2fc92b
Packit 2fc92b
      if ((logstrlen = snprintf(logstr, sizeof(logstr), "%s: %s\n", logLevel, pCommentBegin)) >= sizeof(logstr))
Packit 2fc92b
      {
Packit 2fc92b
	/* If the string was trucnated make sure it has a linefeed before the nul */
Packit 2fc92b
	logstrlen = sizeof(logstr) - 1;
Packit 2fc92b
	logstr[logstrlen - 1] = '\n';
Packit 2fc92b
      }
Packit 2fc92b
      write(STDERR_FILENO, logstr, logstrlen);
Packit 2fc92b
    }
Packit 2fc92b
Packit 2fc92b
    /* move everything over... */
Packit 2fc92b
    strlcpy(gErrorBuffer, pLineEnd, sizeof(gErrorBuffer));
Packit 2fc92b
    gErrorBufferPtr = gErrorBuffer;
Packit 2fc92b
    pLineEnd = (char *)next_line((const char *)gErrorBuffer);
Packit 2fc92b
  }
Packit 2fc92b
}
Packit 2fc92b
#endif /* PARSE_PS_ERRORS */
Packit 2fc92b
Packit 2fc92b
Packit 2fc92b
/*
Packit 2fc92b
 * 'soft_reset()' - Send a soft reset to the device.
Packit 2fc92b
 */
Packit 2fc92b
Packit 2fc92b
static void soft_reset(void)
Packit 2fc92b
{
Packit 2fc92b
  fd_set	  input_set;		/* Input set for select() */
Packit 2fc92b
  struct timeval  tv;			/* Time value */
Packit 2fc92b
  char		  buffer[2048];		/* Buffer */
Packit 2fc92b
  struct timespec cond_timeout;		/* pthread condition timeout */
Packit 2fc92b
Packit 2fc92b
 /*
Packit 2fc92b
  * Send an abort once a second until the I/O lock is released by the main thread...
Packit 2fc92b
  */
Packit 2fc92b
Packit 2fc92b
  pthread_mutex_lock(&g.readwrite_lock_mutex);
Packit 2fc92b
  while (g.readwrite_lock)
Packit 2fc92b
  {
Packit 2fc92b
    (*g.classdriver)->Abort(g.classdriver);
Packit 2fc92b
Packit 2fc92b
    gettimeofday(&tv, NULL);
Packit 2fc92b
    cond_timeout.tv_sec  = tv.tv_sec + 1;
Packit 2fc92b
    cond_timeout.tv_nsec = tv.tv_usec * 1000;
Packit 2fc92b
Packit 2fc92b
    while (g.readwrite_lock)
Packit 2fc92b
    {
Packit 2fc92b
      if (pthread_cond_timedwait(&g.readwrite_lock_cond,
Packit 2fc92b
				 &g.readwrite_lock_mutex,
Packit 2fc92b
				 &cond_timeout) != 0)
Packit 2fc92b
	break;
Packit 2fc92b
    }
Packit 2fc92b
  }
Packit 2fc92b
Packit 2fc92b
  g.readwrite_lock = 1;
Packit 2fc92b
  pthread_mutex_unlock(&g.readwrite_lock_mutex);
Packit 2fc92b
Packit 2fc92b
 /*
Packit 2fc92b
  * Flush bytes waiting on print_fd...
Packit 2fc92b
  */
Packit 2fc92b
Packit 2fc92b
  g.print_bytes = 0;
Packit 2fc92b
Packit 2fc92b
  FD_ZERO(&input_set);
Packit 2fc92b
  FD_SET(g.print_fd, &input_set);
Packit 2fc92b
Packit 2fc92b
  tv.tv_sec  = 0;
Packit 2fc92b
  tv.tv_usec = 0;
Packit 2fc92b
Packit 2fc92b
  while (select(g.print_fd+1, &input_set, NULL, NULL, &tv) > 0)
Packit 2fc92b
    if (read(g.print_fd, buffer, sizeof(buffer)) <= 0)
Packit 2fc92b
      break;
Packit 2fc92b
Packit 2fc92b
 /*
Packit 2fc92b
  * Send the reset...
Packit 2fc92b
  */
Packit 2fc92b
Packit 2fc92b
  (*g.classdriver)->SoftReset(g.classdriver, DEFAULT_TIMEOUT);
Packit 2fc92b
Packit 2fc92b
 /*
Packit 2fc92b
  * Release the I/O lock...
Packit 2fc92b
  */
Packit 2fc92b
Packit 2fc92b
  pthread_mutex_lock(&g.readwrite_lock_mutex);
Packit 2fc92b
  g.readwrite_lock = 0;
Packit 2fc92b
  pthread_cond_signal(&g.readwrite_lock_cond);
Packit 2fc92b
  pthread_mutex_unlock(&g.readwrite_lock_mutex);
Packit 2fc92b
}
Packit 2fc92b
Packit 2fc92b
Packit 2fc92b
/*
Packit 2fc92b
 * 'get_device_id()' - Return IEEE-1284 device ID.
Packit 2fc92b
 */
Packit 2fc92b
Packit 2fc92b
static void get_device_id(cups_sc_status_t *status,
Packit 2fc92b
			  char *data,
Packit 2fc92b
			  int *datalen)
Packit 2fc92b
{
Packit 2fc92b
  CFStringRef deviceIDString = NULL;
Packit 2fc92b
Packit 2fc92b
  if (g.printer_obj != IO_OBJECT_NULL)
Packit 2fc92b
  {
Packit 2fc92b
    printer_interface_t printerIntf = usb_printer_interface_interface(g.printer_obj);
Packit 2fc92b
    if (printerIntf)
Packit 2fc92b
    {
Packit 2fc92b
      deviceIDString = copy_printer_interface_deviceid(printerIntf, g.alternateSetting);
Packit 2fc92b
      (*printerIntf)->Release(printerIntf);
Packit 2fc92b
    }
Packit 2fc92b
  }
Packit 2fc92b
Packit 2fc92b
Packit 2fc92b
  if (deviceIDString)
Packit 2fc92b
  {
Packit 2fc92b
    if (CFStringGetCString(deviceIDString, data, *datalen, kCFStringEncodingUTF8))
Packit 2fc92b
      *datalen = (int)strlen(data);
Packit 2fc92b
    else
Packit 2fc92b
      *datalen = 0;
Packit 2fc92b
Packit 2fc92b
    CFRelease(deviceIDString);
Packit 2fc92b
  }
Packit 2fc92b
  else
Packit 2fc92b
  {
Packit 2fc92b
    *datalen = 0;
Packit 2fc92b
  }
Packit 2fc92b
Packit 2fc92b
  *status  = CUPS_SC_STATUS_OK;
Packit 2fc92b
}