/*
-* "$Id: usb-darwin.c 6680 2007-07-16 18:46:16Z mike $"
+* "$Id: usb-darwin.c 7953 2008-09-17 01:43:19Z mike $"
*
-* Copyright © 2005-2007 Apple Inc. All rights reserved.
+* Copyright 2005-2011 Apple Inc. All rights reserved.
*
* IMPORTANT: This Apple software is supplied to you by Apple Computer,
* Inc. ("Apple") in consideration of your agreement to the following
*
* Contents:
*
-* list_devices() - List all USB devices.
-* print_device() - Print a file to a USB device.
-* sidechannel_thread() - Thread to handle side-channel requests.
-* read_thread() - Thread to read the backchannel data on.
-* list_device_cb() - list_device iterator callback.
-* find_device_cb() - print_device iterator callback.
-* status_timer_cb() - Status timer callback.
-* iterate_printers() - Iterate over all the printers.
-* device_added() - Device added notifier.
-* copy_deviceinfo() - Copy strings from the 1284 device ID.
-* release_deviceinfo() - Release deviceinfo strings.
-* load_classdriver() - Load a classdriver.
-* unload_classdriver() - Unload a classdriver.
-* load_printerdriver() - Load vendor's classdriver.
-* registry_open() - Open a connection to the printer.
-* registry_close() - Close the connection to the printer.
-* copy_deviceid() - Copy the 1284 device id string.
-* copy_devicestring() - Copy the 1284 device id string.
-* copy_value_for_key() - Copy value string associated with a key.
-* cfstr_create_trim() - Create CFString and trim whitespace characters.
-* parse_options() - Parse uri options.
-* setup_cfLanguage() - Create AppleLanguages array from LANG environment var.
-* run_ppc_backend() - Re-exec i386 backend as ppc.
-* sigterm_handler() - SIGTERM handler.
-* next_line() - Find the next line in a buffer.
-* parse_pserror() - Scan the backchannel data for postscript errors.
-* get_device_id() - Return IEEE-1284 device ID.
+ * list_devices() - List all USB devices.
+ * print_device() - Print a file to a USB device.
+ * read_thread() - Thread to read the backchannel data on.
+ * sidechannel_thread() - Handle side-channel requests.
+ * iterate_printers() - Iterate over all the printers.
+ * device_added() - Device added notifier.
+ * list_device_cb() - list_device iterator callback.
+ * find_device_cb() - print_device iterator callback.
+ * status_timer_cb() - Status timer callback.
+ * copy_deviceinfo() - Copy strings from the 1284 device ID.
+ * release_deviceinfo() - Release deviceinfo strings.
+ * load_classdriver() - Load a classdriver.
+ * unload_classdriver() - Unload a classdriver.
+ * load_printerdriver() - Load vendor's classdriver.
+ * registry_open() - Open a connection to the printer.
+ * registry_close() - Close the connection to the printer.
+ * copy_deviceid() - Copy the 1284 device id string.
+ * copy_devicestring() - Copy the 1284 device id string.
+ * copy_value_for_key() - Copy value string associated with a key.
+ * cfstr_create_trim() - Create CFString and trim whitespace characters.
+ * parse_options() - Parse URI options.
+ * sigterm_handler() - SIGTERM handler.
+ * next_line() - Find the next line in a buffer.
+ * parse_pserror() - Scan the backchannel data for postscript errors.
+ * soft_reset() - Send a soft reset to the device.
+ * get_device_id() - Return IEEE-1284 device ID.
*/
/*
#include <fcntl.h>
#include <termios.h>
#include <unistd.h>
+#include <sys/stat.h>
#include <sys/sysctl.h>
#include <libgen.h>
#include <mach/mach.h>
#include <mach/mach_error.h>
#include <mach/mach_time.h>
-#include <cups/debug.h>
+#include <cups/debug-private.h>
+#include <cups/file-private.h>
#include <cups/sidechannel.h>
-#include <cups/i18n.h>
-
+#include <cups/language-private.h>
+#include "backend-private.h"
#include <CoreFoundation/CoreFoundation.h>
#include <IOKit/usb/IOUSBLib.h>
#include <IOKit/IOCFPlugIn.h>
+#include <spawn.h>
#include <pthread.h>
+extern char **environ;
+
+
+/*
+ * DEBUG_WRITES, if defined, causes the backend to write data to the printer in
+ * 512 byte increments, up to 8192 bytes, to make debugging with a USB bus
+ * analyzer easier.
+ */
+
+#define DEBUG_WRITES 0
/*
* WAIT_EOF_DELAY is number of seconds we'll wait for responses from
*/
#define WAIT_EOF_DELAY 7
#define WAIT_SIDE_DELAY 3
-#define DEFAULT_TIMEOUT 60L
+#define DEFAULT_TIMEOUT 5000L
#define USB_INTERFACE_KIND CFUUIDGetUUIDBytes(kIOUSBInterfaceInterfaceID190)
#define kUSBLanguageEnglish 0x409
#define kUSBClassDriverProperty CFSTR("USB Printing Class")
-#define kUSBGenericTOPrinterClassDriver CFSTR("/System/Library/Printers/Libraries/USBGenericTOPrintingClass.plugin")
+#define kUSBGenericTOPrinterClassDriver CFSTR("/System/Library/Printers/Libraries/USBGenericPrintingClass.plugin")
#define kUSBPrinterClassDeviceNotOpen -9664 /*kPMInvalidIOMContext*/
int print_fd; /* File descriptor to print */
ssize_t print_bytes; /* Print bytes read */
+#if DEBUG_WRITES
+ ssize_t debug_bytes; /* Current bytes to read */
+#endif /* DEBUG_WRITES */
Boolean wait_eof;
int drain_output; /* Drain all pending output */
static CFStringRef copy_value_for_key(CFStringRef deviceID, CFStringRef *keys);
static kern_return_t load_classdriver(CFStringRef driverPath, printer_interface_t intf, classdriver_t ***printerDriver);
static kern_return_t load_printerdriver(CFStringRef *driverBundlePath);
-static kern_return_t registry_close();
+static kern_return_t registry_close(void);
static kern_return_t registry_open(CFStringRef *driverBundlePath);
-static kern_return_t unload_classdriver();
+static kern_return_t unload_classdriver(classdriver_t ***classdriver);
static OSStatus copy_deviceid(classdriver_t **printer, CFStringRef *deviceID);
static void *read_thread(void *reference);
static void *sidechannel_thread(void *reference);
static void device_added(void *userdata, io_iterator_t iterator);
static void get_device_id(cups_sc_status_t *status, char *data, int *datalen);
static void iterate_printers(iterator_callback_t callBack, void *userdata);
-static void parse_options(const char *options, char *serial, int serial_size, UInt32 *location, Boolean *wait_eof);
+static void parse_options(char *options, char *serial, int serial_size, UInt32 *location, Boolean *wait_eof);
static void release_deviceinfo(CFStringRef *make, CFStringRef *model, CFStringRef *serial);
static void setup_cfLanguage(void);
-static void soft_reset();
+static void soft_reset(void);
static void status_timer_cb(CFRunLoopTimerRef timer, void *info);
-#if defined(__i386__)
-static pid_t child_pid; /* Child PID */
-static void run_ppc_backend(int argc, char *argv[], int fd); /* Starts child backend process running as a ppc executable */
-static void sigterm_handler(int sig); /* SIGTERM handler */
-#endif /* __i386__ */
+#if defined(__i386__) || defined(__x86_64__)
+static pid_t child_pid; /* Child PID */
+static void run_legacy_backend(int argc, char *argv[], int fd); /* Starts child backend process running as a ppc executable */
+#endif /* __i386__ || __x86_64__ */
+static void sigterm_handler(int sig); /* SIGTERM handler */
#ifdef PARSE_PS_ERRORS
static const char *next_line (const char *buffer);
print_device(const char *uri, /* I - Device URI */
const char *hostname, /* I - Hostname/manufacturer */
const char *resource, /* I - Resource/modelname */
- const char *options, /* I - Device options/serial number */
+ char *options, /* I - Device options/serial number */
int print_fd, /* I - File descriptor to print */
int copies, /* I - Copies to print */
int argc, /* I - Number of command-line arguments (6 or 7) */
{
char serial[1024]; /* Serial number buffer */
OSStatus status; /* Function results */
+ IOReturn iostatus; /* Current IO status */
pthread_t read_thread_id, /* Read thread */
- sidechannel_thread_id;/* Side channel thread */
+ sidechannel_thread_id;/* Side-channel thread */
+ int have_sidechannel = 0; /* Was the side-channel thread started? */
+ struct stat sidechannel_info; /* Side-channel file descriptor info */
char print_buffer[8192], /* Print data buffer */
*print_ptr; /* Pointer into print data buffer */
UInt32 location; /* Unique location in bus topology */
ssize_t total_bytes; /* Total bytes written */
UInt32 bytes; /* Bytes written */
struct timeval *timeout, /* Timeout pointer */
- stimeout; /* Timeout for select() */
+ tv; /* Time value */
struct timespec cond_timeout; /* pthread condition timeout */
+
+ /*
+ * See if the side-channel descriptor is valid...
+ */
+
+ have_sidechannel = !fstat(CUPS_SC_FD, &sidechannel_info) &&
+ S_ISSOCK(sidechannel_info.st_mode);
+
+ /*
+ * Localize using CoreFoundation...
+ */
+
setup_cfLanguage();
parse_options(options, serial, sizeof(serial), &location, &g.wait_eof);
g.serial = cfstr_create_trim(serial);
g.location = location;
+ if (!g.make || !g.model)
+ {
+ fprintf(stderr, "DEBUG: Fatal USB error.\n");
+ _cupsLangPrintFilter(stderr, "ERROR",
+ _("There was an unrecoverable USB error."));
+
+ if (!g.make)
+ fputs("DEBUG: USB make string is NULL\n", stderr);
+ if (!g.model)
+ fputs("DEBUG: USB model string is NULL\n", stderr);
+
+ return (CUPS_BACKEND_STOP);
+ }
+
fputs("STATE: +connecting-to-device\n", stderr);
countdown = INITIAL_LOG_INTERVAL;
status = registry_open(&driverBundlePath);
-#if defined(__i386__)
+#if defined(__i386__) || defined(__x86_64__)
/*
- * If we were unable to load the class drivers for this printer it's probably because they're ppc-only.
- * In this case try to fork & exec this backend as a ppc executable so we can use them...
+ * If we were unable to load the class drivers for this printer it's
+ * probably because they're ppc or i386. In this case try to run this
+ * backend as i386 or ppc executables so we can use them...
*/
if (status == -2)
{
- run_ppc_backend(argc, argv, print_fd);
+ run_legacy_backend(argc, argv, print_fd);
/* Never returns here */
}
-#endif /* __i386__ */
+#endif /* __i386__ || __x86_64__ */
if (status == -2)
{
strlcpy(print_buffer, "USB class driver", sizeof(print_buffer));
fputs("STATE: +apple-missing-usbclassdriver-error\n", stderr);
- fprintf(stderr, _("FATAL: Could not load %s\n"), print_buffer);
+ _cupsLangPrintFilter(stderr, "ERROR",
+ _("There was an unrecoverable USB error."));
+ fprintf(stderr, "DEBUG: Could not load %s\n", print_buffer);
if (driverBundlePath)
CFRelease(driverBundlePath);
- return CUPS_BACKEND_STOP;
+ return (CUPS_BACKEND_STOP);
}
if (driverBundlePath)
countdown -= PRINTER_POLLING_INTERVAL;
if (countdown <= 0)
{
- fprintf(stderr, _("INFO: Printer busy (status:0x%08x)\n"), (int)status);
+ _cupsLangPrintFilter(stderr, "INFO",
+ _("Waiting for printer to become available."));
+ fprintf(stderr, "DEBUG: USB printer status: 0x%08x\n", (int)status);
countdown = SUBSEQUENT_LOG_INTERVAL; /* subsequent log entries, every 15 seconds */
}
}
}
/*
- * Start the side channel thread only if the descriptor is valid
- * (i.e. it's not when the backend is used for auto-setup)...
+ * Start the side channel thread if the descriptor is valid...
*/
pthread_mutex_init(&g.readwrite_lock_mutex, NULL);
pthread_cond_init(&g.readwrite_lock_cond, NULL);
g.readwrite_lock = 1;
- FD_ZERO(&input_set);
- FD_SET(CUPS_SC_FD, &input_set);
-
- stimeout.tv_sec = 0;
- stimeout.tv_usec = 0;
-
- if ((select(CUPS_SC_FD+1, &input_set, NULL, NULL, &stimeout)) >= 0)
+ if (have_sidechannel)
{
g.sidechannel_thread_stop = 0;
g.sidechannel_thread_done = 0;
if (pthread_create(&sidechannel_thread_id, NULL, sidechannel_thread, NULL))
{
- fputs(_("WARNING: Couldn't create side channel\n"), stderr);
- return CUPS_BACKEND_STOP;
+ fprintf(stderr, "DEBUG: Fatal USB error.\n");
+ _cupsLangPrintFilter(stderr, "ERROR",
+ _("There was an unrecoverable USB error."));
+ fputs("DEBUG: Couldn't create side-channel thread\n", stderr);
+ registry_close();
+ return (CUPS_BACKEND_STOP);
}
}
if (pthread_create(&read_thread_id, NULL, read_thread, NULL))
{
- fputs(_("WARNING: Couldn't create read channel\n"), stderr);
- return CUPS_BACKEND_STOP;
+ fprintf(stderr, "DEBUG: Fatal USB error.\n");
+ _cupsLangPrintFilter(stderr, "ERROR",
+ _("There was an unrecoverable USB error."));
+ fputs("DEBUG: Couldn't create read thread\n", stderr);
+ registry_close();
+ return (CUPS_BACKEND_STOP);
}
/*
while (status == noErr && copies-- > 0)
{
- fputs(_("INFO: Sending data\n"), stderr);
+ _cupsLangPrintFilter(stderr, "INFO", _("Sending data to printer."));
if (print_fd != STDIN_FILENO)
{
- fputs("PAGE: 1 1", stderr);
+ fputs("PAGE: 1 1\n", stderr);
lseek(print_fd, 0, SEEK_SET);
}
if (g.print_bytes)
{
- stimeout.tv_sec = 0;
- stimeout.tv_usec = 100000; /* 100ms */
- timeout = &stimeout;
+ tv.tv_sec = 0;
+ tv.tv_usec = 100000; /* 100ms */
+ timeout = &tv;
}
else if (g.drain_output)
{
- stimeout.tv_sec = 0;
- stimeout.tv_usec = 0;
- timeout = &stimeout;
+ tv.tv_sec = 0;
+ tv.tv_usec = 0;
+ timeout = &tv;
}
else
timeout = NULL;
if (errno == EINTR && total_bytes == 0)
{
fputs("DEBUG: Received an interrupt before any bytes were "
- "written, aborting!\n", stderr);
- return (0);
+ "written, aborting\n", stderr);
+ registry_close();
+ return (CUPS_BACKEND_OK);
}
- else if (errno != EAGAIN)
+ else if (errno != EAGAIN && errno != EINTR)
{
- fprintf(stderr, _("ERROR: select() returned %d\n"), (int)errno);
- return CUPS_BACKEND_STOP;
+ _cupsLangPrintFilter(stderr, "ERROR",
+ _("Unable to read print data."));
+ perror("DEBUG: select");
+ registry_close();
+ return (CUPS_BACKEND_FAILED);
}
}
if (FD_ISSET(print_fd, &input_set))
{
+#if DEBUG_WRITES
+ g.debug_bytes += 512;
+ if (g.debug_bytes > sizeof(print_buffer))
+ g.debug_bytes = 512;
+
+ g.print_bytes = read(print_fd, print_buffer, g.debug_bytes);
+
+#else
g.print_bytes = read(print_fd, print_buffer, sizeof(print_buffer));
+#endif /* DEBUG_WRITES */
if (g.print_bytes < 0)
{
* Read error - bail if we don't see EAGAIN or EINTR...
*/
- if (errno != EAGAIN || errno != EINTR)
+ if (errno != EAGAIN && errno != EINTR)
{
- perror("ERROR: Unable to read print data");
- return CUPS_BACKEND_STOP;
+ _cupsLangPrintFilter(stderr, "ERROR",
+ _("Unable to read print data."));
+ perror("DEBUG: read");
+ registry_close();
+ return (CUPS_BACKEND_FAILED);
}
g.print_bytes = 0;
if (g.print_bytes)
{
- bytes = g.print_bytes;
+ bytes = g.print_bytes;
+ iostatus = (*g.classdriver)->WritePipe(g.classdriver, (UInt8*)print_ptr, &bytes, 0);
- status = (*g.classdriver)->WritePipe(g.classdriver, (UInt8*)print_ptr, &bytes, 0);
+ /*
+ * Ignore timeout errors, but retain the number of bytes written to
+ * avoid sending duplicate data (<rdar://problem/6254911>)...
+ */
+
+ if (iostatus == kIOUSBTransactionTimeout)
+ {
+ fputs("DEBUG: Got USB transaction timeout during write\n", stderr);
+ iostatus = 0;
+ }
/*
- * Ignore timeout errors...
+ * If we've stalled, retry the write...
*/
- if (status == kIOUSBTransactionTimeout)
+ else if (iostatus == kIOUSBPipeStalled)
{
- status = 0;
- bytes = 0;
+ fputs("DEBUG: Got USB pipe stalled during write\n", stderr);
+
+ bytes = g.print_bytes;
+ iostatus = (*g.classdriver)->WritePipe(g.classdriver, (UInt8*)print_ptr, &bytes, 0);
}
- if (status || bytes < 0)
+ /*
+ * Retry a write after an aborted write since we probably just got
+ * SIGTERM (<rdar://problem/6860126>)...
+ */
+
+ else if (iostatus == kIOReturnAborted)
+ {
+ fputs("DEBUG: Got USB return aborted during write\n", stderr);
+
+ IOReturn err = (*g.classdriver)->Abort(g.classdriver);
+ fprintf(stderr, "DEBUG: USB class driver Abort returned %x\n", err);
+
+#if DEBUG_WRITES
+ sleep(5);
+#endif /* DEBUG_WRITES */
+
+ bytes = g.print_bytes;
+ iostatus = (*g.classdriver)->WritePipe(g.classdriver, (UInt8*)print_ptr, &bytes, 0);
+ }
+
+ if (iostatus)
{
/*
* Write error - bail if we don't see an error we can retry...
*/
- OSStatus err = (*g.classdriver)->Abort(g.classdriver);
- fprintf(stderr, _("ERROR: %ld: (canceled:%ld)\n"), (long)status, (long)err);
- status = CUPS_BACKEND_STOP;
+ _cupsLangPrintFilter(stderr, "ERROR",
+ _("Unable to send data to printer."));
+ fprintf(stderr, "DEBUG: USB class driver WritePipe returned %x\n",
+ iostatus);
+
+ IOReturn err = (*g.classdriver)->Abort(g.classdriver);
+ fprintf(stderr, "DEBUG: USB class driver Abort returned %x\n",
+ err);
+
+ status = CUPS_BACKEND_FAILED;
break;
}
+ else if (bytes > 0)
+ {
+ fprintf(stderr, "DEBUG: Wrote %d bytes of print data...\n", (int)bytes);
- fprintf(stderr, "DEBUG: Wrote %d bytes of print data...\n", (int)bytes);
-
- g.print_bytes -= bytes;
- print_ptr += bytes;
- total_bytes += bytes;
+ g.print_bytes -= bytes;
+ print_ptr += bytes;
+ total_bytes += bytes;
+ }
}
if (print_fd != 0 && status == noErr)
fprintf(stderr, "DEBUG: Sent %lld bytes...\n", (off_t)total_bytes);
+ if (!print_fd)
+ {
+ /*
+ * Re-enable the SIGTERM handler so pthread_kill() will work...
+ */
+
+ struct sigaction action; /* POSIX signal action */
+
+ memset(&action, 0, sizeof(action));
+
+ sigemptyset(&action.sa_mask);
+ sigaddset(&action.sa_mask, SIGTERM);
+ action.sa_handler = sigterm_handler;
+ sigaction(SIGTERM, &action, NULL);
+ }
+
/*
* Wait for the side channel thread to exit...
*/
- close(CUPS_SC_FD);
- pthread_mutex_lock(&g.readwrite_lock_mutex);
- g.readwrite_lock = 0;
- pthread_cond_signal(&g.readwrite_lock_cond);
- pthread_mutex_unlock(&g.readwrite_lock_mutex);
-
- g.sidechannel_thread_stop = 1;
- pthread_mutex_lock(&g.sidechannel_thread_mutex);
- if (!g.sidechannel_thread_done)
+ if (have_sidechannel)
{
- cond_timeout.tv_sec = time(NULL) + WAIT_SIDE_DELAY;
- cond_timeout.tv_nsec = 0;
- pthread_cond_timedwait(&g.sidechannel_thread_cond,
- &g.sidechannel_thread_mutex, &cond_timeout);
- }
- pthread_mutex_unlock(&g.sidechannel_thread_mutex);
+ close(CUPS_SC_FD);
+ pthread_mutex_lock(&g.readwrite_lock_mutex);
+ g.readwrite_lock = 0;
+ pthread_cond_signal(&g.readwrite_lock_cond);
+ pthread_mutex_unlock(&g.readwrite_lock_mutex);
+
+ g.sidechannel_thread_stop = 1;
+ pthread_mutex_lock(&g.sidechannel_thread_mutex);
+
+ if (!g.sidechannel_thread_done)
+ {
+ gettimeofday(&tv, NULL);
+ cond_timeout.tv_sec = tv.tv_sec + WAIT_SIDE_DELAY;
+ cond_timeout.tv_nsec = tv.tv_usec * 1000;
+
+ while (!g.sidechannel_thread_done)
+ {
+ if (pthread_cond_timedwait(&g.sidechannel_thread_cond,
+ &g.sidechannel_thread_mutex,
+ &cond_timeout) != 0)
+ break;
+ }
+
+ if (!g.sidechannel_thread_done)
+ {
+ /*
+ * Force the side-channel thread to exit...
+ */
+
+ fputs("DEBUG: Force the side-channel thread to exit...\n", stderr);
+ pthread_kill(sidechannel_thread_id, SIGTERM);
+ }
+ }
- pthread_join(sidechannel_thread_id, NULL);
+ pthread_mutex_unlock(&g.sidechannel_thread_mutex);
- pthread_cond_destroy(&g.sidechannel_thread_cond);
- pthread_mutex_destroy(&g.sidechannel_thread_mutex);
+ pthread_join(sidechannel_thread_id, NULL);
+
+ pthread_cond_destroy(&g.sidechannel_thread_cond);
+ pthread_mutex_destroy(&g.sidechannel_thread_mutex);
+ }
pthread_cond_destroy(&g.readwrite_lock_cond);
pthread_mutex_destroy(&g.readwrite_lock_mutex);
/*
* Give the read thread WAIT_EOF_DELAY seconds to complete all the data. If
- * we are not signaled in that time then force the thread to exit by setting
- * the waiteof to be false. Plese note that this relies on us using the timeout
- * class driver.
+ * we are not signaled in that time then force the thread to exit.
*/
pthread_mutex_lock(&g.read_thread_mutex);
if (!g.read_thread_done)
{
- cond_timeout.tv_sec = time(NULL) + WAIT_EOF_DELAY;
- cond_timeout.tv_nsec = 0;
+ gettimeofday(&tv, NULL);
+ cond_timeout.tv_sec = tv.tv_sec + WAIT_EOF_DELAY;
+ cond_timeout.tv_nsec = tv.tv_usec * 1000;
+
+ while (!g.read_thread_done)
+ {
+ if (pthread_cond_timedwait(&g.read_thread_cond, &g.read_thread_mutex,
+ &cond_timeout) != 0)
+ break;
+ }
- if (pthread_cond_timedwait(&g.read_thread_cond, &g.read_thread_mutex,
- &cond_timeout) != 0)
- g.wait_eof = false;
+ if (!g.read_thread_done)
+ {
+ /*
+ * Force the read thread to exit...
+ */
+
+ g.wait_eof = 0;
+ fputs("DEBUG: Force the read thread to exit...\n", stderr);
+ pthread_kill(read_thread_id, SIGTERM);
+ }
}
+
pthread_mutex_unlock(&g.read_thread_mutex);
pthread_join(read_thread_id, NULL); /* wait for the read thread to return */
readstatus = (*g.classdriver)->ReadPipe(g.classdriver, readbuffer, &rbytes);
if (readstatus == kIOReturnSuccess && rbytes > 0)
{
+ fprintf(stderr, "DEBUG: Read %d bytes of back-channel data...\n",
+ (int)rbytes);
cupsBackChannelWrite((char*)readbuffer, rbytes, 1.0);
/* cntrl-d is echoed by the printer.
parse_pserror(readbuffer, rbytes);
#endif
}
+ else if (readstatus == kIOUSBTransactionTimeout)
+ fputs("DEBUG: Got USB transaction timeout during read\n", stderr);
+ else if (readstatus == kIOUSBPipeStalled)
+ fputs("DEBUG: Got USB pipe stalled during read\n", stderr);
+ else if (readstatus == kIOReturnAborted)
+ fputs("DEBUG: Got USB return aborted during read\n", stderr);
/*
* Make sure this loop executes no more than once every 250 miliseconds...
datalen = sizeof(data);
if (cupsSideChannelRead(&command, &status, data, &datalen, 1.0))
- continue;
+ {
+ if (status == CUPS_SC_STATUS_TIMEOUT)
+ continue;
+ else
+ break;
+ }
switch (command)
{
case CUPS_SC_CMD_SOFT_RESET: /* Do a soft reset */
- soft_reset();
- cupsSideChannelWrite(command, CUPS_SC_STATUS_OK, NULL, 0, 1.0);
+ fputs("DEBUG: CUPS_SC_CMD_SOFT_RESET received from driver...\n",
+ stderr);
+
+ if ((*g.classdriver)->SoftReset != NULL)
+ {
+ soft_reset();
+ cupsSideChannelWrite(command, CUPS_SC_STATUS_OK, NULL, 0, 1.0);
+ fputs("DEBUG: Returning status CUPS_STATUS_OK with no bytes...\n",
+ stderr);
+ }
+ else
+ {
+ cupsSideChannelWrite(command, CUPS_SC_STATUS_NOT_IMPLEMENTED,
+ NULL, 0, 1.0);
+ fputs("DEBUG: Returning status CUPS_STATUS_NOT_IMPLEMENTED with "
+ "no bytes...\n", stderr);
+ }
break;
case CUPS_SC_CMD_DRAIN_OUTPUT: /* Drain all pending output */
+ fputs("DEBUG: CUPS_SC_CMD_DRAIN_OUTPUT received from driver...\n",
+ stderr);
+
g.drain_output = 1;
break;
case CUPS_SC_CMD_GET_BIDI: /* Is the connection bidirectional? */
+ fputs("DEBUG: CUPS_SC_CMD_GET_BIDI received from driver...\n",
+ stderr);
+
data[0] = g.bidi_flag;
cupsSideChannelWrite(command, CUPS_SC_STATUS_OK, data, 1, 1.0);
+
+ fprintf(stderr,
+ "DEBUG: Returned CUPS_SC_STATUS_OK with 1 byte (%02X)...\n",
+ data[0]);
break;
case CUPS_SC_CMD_GET_DEVICE_ID: /* Return IEEE-1284 device ID */
+ fputs("DEBUG: CUPS_SC_CMD_GET_DEVICE_ID received from driver...\n",
+ stderr);
+
datalen = sizeof(data);
get_device_id(&status, data, &datalen);
cupsSideChannelWrite(command, CUPS_SC_STATUS_OK, data, datalen, 1.0);
+
+ if (datalen < sizeof(data))
+ data[datalen] = '\0';
+ else
+ data[sizeof(data) - 1] = '\0';
+
+ fprintf(stderr,
+ "DEBUG: Returning CUPS_SC_STATUS_OK with %d bytes (%s)...\n",
+ datalen, data);
break;
case CUPS_SC_CMD_GET_STATE: /* Return device state */
+ fputs("DEBUG: CUPS_SC_CMD_GET_STATE received from driver...\n",
+ stderr);
+
data[0] = CUPS_SC_STATE_ONLINE;
cupsSideChannelWrite(command, CUPS_SC_STATUS_OK, data, 1, 1.0);
+
+ fprintf(stderr,
+ "DEBUG: Returned CUPS_SC_STATUS_OK with 1 byte (%02X)...\n",
+ data[0]);
break;
default:
+ fprintf(stderr, "DEBUG: Unknown side-channel command (%d) received "
+ "from driver...\n", command);
+
cupsSideChannelWrite(command, CUPS_SC_STATUS_NOT_IMPLEMENTED,
NULL, 0, 1.0);
+
+ fputs("DEBUG: Returned CUPS_SC_STATUS_NOT_IMPLEMENTED with no bytes...\n",
+ stderr);
break;
}
}
{
CFStringRef make = NULL, model = NULL, serial = NULL;
char uristr[1024], makestr[1024], modelstr[1024], serialstr[1024];
- char optionsstr[1024], idstr[1024];
+ char optionsstr[1024], idstr[1024], make_modelstr[1024];
copy_deviceinfo(deviceIDString, &make, &model, &serial);
-
- modelstr[0] = '/';
-
CFStringGetCString(deviceIDString, idstr, sizeof(idstr),
kCFStringEncodingUTF8);
+ backendGetMakeModel(idstr, make_modelstr, sizeof(make_modelstr));
- if (make)
- CFStringGetCString(make, makestr, sizeof(makestr),
- kCFStringEncodingUTF8);
- else
+ modelstr[0] = '/';
+
+ if (!make ||
+ !CFStringGetCString(make, makestr, sizeof(makestr),
+ kCFStringEncodingUTF8))
strcpy(makestr, "Unknown");
- if (model)
- CFStringGetCString(model, &modelstr[1], sizeof(modelstr)-1,
- kCFStringEncodingUTF8);
- else
+ if (!model ||
+ !CFStringGetCString(model, &modelstr[1], sizeof(modelstr)-1,
+ kCFStringEncodingUTF8))
strcpy(modelstr + 1, "Printer");
optionsstr[0] = '\0';
snprintf(optionsstr, sizeof(optionsstr), "?location=%x", (unsigned)deviceLocation);
httpAssembleURI(HTTP_URI_CODING_ALL, uristr, sizeof(uristr), "usb", NULL, makestr, 0, modelstr);
- strncat(uristr, optionsstr, sizeof(uristr));
+ strlcat(uristr, optionsstr, sizeof(uristr));
- /*
- * Fix common HP 1284 bug...
- */
-
- if (!strcasecmp(makestr, "Hewlett-Packard"))
- strcpy(makestr, "HP");
-
- if (!strncasecmp(modelstr + 1, "hp ", 3))
- _cups_strcpy(modelstr + 1, modelstr + 4);
-
- printf("direct %s \"%s %s\" \"%s %s USB\" \"%s\"\n", uristr, makestr,
- &modelstr[1], makestr, &modelstr[1], idstr);
+ cupsBackendReport("direct", uristr, make_modelstr, make_modelstr, idstr,
+ NULL);
release_deviceinfo(&make, &model, &serial);
CFRelease(deviceIDString);
CFStringRef make = NULL, model = NULL, serial = NULL;
copy_deviceinfo(idString, &make, &model, &serial);
- if (CFStringCompare(make, g.make, kCFCompareCaseInsensitive) == kCFCompareEqualTo)
+ if (make && CFStringCompare(make, g.make, kCFCompareCaseInsensitive) == kCFCompareEqualTo)
{
- if (CFStringCompare(model, g.model, kCFCompareCaseInsensitive) == kCFCompareEqualTo)
+ if (model && CFStringCompare(model, g.model, kCFCompareCaseInsensitive) == kCFCompareEqualTo)
{
if (g.serial != NULL && CFStringGetLength(g.serial) > 0)
{
if (!keepLooking && g.status_timer != NULL)
{
- fputs("STATE: -offline-error\n", stderr);
- fputs(_("INFO: Printer is now on-line.\n"), stderr);
+ fputs("STATE: -offline-report\n", stderr);
+ _cupsLangPrintFilter(stderr, "INFO", _("Printer is now online."));
CFRunLoopRemoveTimer(CFRunLoopGetCurrent(), g.status_timer, kCFRunLoopDefaultMode);
CFRelease(g.status_timer);
g.status_timer = NULL;
static void status_timer_cb(CFRunLoopTimerRef timer,
void *info)
{
- fputs("STATE: +offline-error\n", stderr);
- fputs(_("INFO: Printer is currently off-line.\n"), stderr);
+ fputs("STATE: +offline-report\n", stderr);
+ _cupsLangPrintFilter(stderr, "INFO", _("Printer is offline."));
+
+ if (getenv("CLASS") != NULL)
+ {
+ /*
+ * If the CLASS environment variable is set, the job was submitted
+ * to a class and not to a specific queue. In this case, we want
+ * to abort immediately so that the job can be requeued on the next
+ * available printer in the class.
+ *
+ * Sleep 5 seconds to keep the job from requeuing too rapidly...
+ */
+
+ sleep(5);
+
+ exit(CUPS_BACKEND_FAILED);
+ }
}
if (make != NULL)
*make = copy_value_for_key(deviceIDString, makeKeys);
+
if (model != NULL)
*model = copy_value_for_key(deviceIDString, modelKeys);
+
if (serial != NULL)
*serial = copy_value_for_key(deviceIDString, serialKeys);
}
printer_interface_t intf,
classdriver_t ***printerDriver)
{
- kern_return_t kr = kUSBPrinterClassDeviceNotOpen;
- classdriver_t **driver = NULL;
- CFStringRef bundle = (driverPath == NULL ? kUSBGenericTOPrinterClassDriver : driverPath);
+ kern_return_t kr = kUSBPrinterClassDeviceNotOpen;
+ classdriver_t **driver = NULL;
+ CFStringRef bundle = driverPath ? driverPath : kUSBGenericTOPrinterClassDriver;
+ char bundlestr[1024]; /* Bundle path */
+ CFURLRef url; /* URL for driver */
+ CFPlugInRef plugin = NULL; /* Plug-in address */
- if (bundle != NULL)
- {
- CFURLRef url = CFURLCreateWithFileSystemPath(NULL, bundle, kCFURLPOSIXPathStyle, true);
- CFPlugInRef plugin = (url != NULL ? CFPlugInCreate(NULL, url) : NULL);
- if (url != NULL)
- CFRelease(url);
+ CFStringGetCString(bundle, bundlestr, sizeof(bundlestr), kCFStringEncodingUTF8);
+
+ /*
+ * Validate permissions for the class driver...
+ */
- if (plugin != NULL)
+ _cups_fc_result_t result = _cupsFileCheck(bundlestr,
+ _CUPS_FILE_CHECK_DIRECTORY, 1,
+ _cupsFileCheckFilter, NULL);
+
+ if (result && driverPath)
+ return (load_classdriver(NULL, intf, printerDriver));
+ else if (result)
+ return (kr);
+
+ /*
+ * Try loading the class driver...
+ */
+
+ url = CFURLCreateWithFileSystemPath(NULL, bundle, kCFURLPOSIXPathStyle, true);
+
+ if (url)
+ {
+ plugin = CFPlugInCreate(NULL, url);
+ CFRelease(url);
+ }
+ else
+ plugin = NULL;
+
+ if (plugin)
+ {
+ CFArrayRef factories = CFPlugInFindFactoriesForPlugInTypeInPlugIn(kUSBPrinterClassTypeID, plugin);
+ if (factories != NULL && CFArrayGetCount(factories) > 0)
{
- CFArrayRef factories = CFPlugInFindFactoriesForPlugInTypeInPlugIn(kUSBPrinterClassTypeID, plugin);
- if (factories != NULL && CFArrayGetCount(factories) > 0)
+ CFUUIDRef factoryID = CFArrayGetValueAtIndex(factories, 0);
+ IUnknownVTbl **iunknown = CFPlugInInstanceCreate(NULL, factoryID, kUSBPrinterClassTypeID);
+ if (iunknown != NULL)
{
- CFUUIDRef factoryID = CFArrayGetValueAtIndex(factories, 0);
- IUnknownVTbl **iunknown = CFPlugInInstanceCreate(NULL, factoryID, kUSBPrinterClassTypeID);
- if (iunknown != NULL)
+ kr = (*iunknown)->QueryInterface(iunknown, CFUUIDGetUUIDBytes(kUSBPrinterClassInterfaceID), (LPVOID *)&driver);
+ if (kr == kIOReturnSuccess && driver != NULL)
{
- kr = (*iunknown)->QueryInterface(iunknown, CFUUIDGetUUIDBytes(kUSBPrinterClassInterfaceID), (LPVOID *)&driver);
- if (kr == kIOReturnSuccess && driver != NULL)
- {
- classdriver_t **genericDriver = NULL;
- if (driverPath != NULL && CFStringCompare(driverPath, kUSBGenericTOPrinterClassDriver, 0) != kCFCompareEqualTo)
- kr = load_classdriver(NULL, intf, &genericDriver);
+ classdriver_t **genericDriver = NULL;
+ if (driverPath != NULL && CFStringCompare(driverPath, kUSBGenericTOPrinterClassDriver, 0) != kCFCompareEqualTo)
+ kr = load_classdriver(NULL, intf, &genericDriver);
- if (kr == kIOReturnSuccess)
- {
- (*driver)->interface = intf;
- (*driver)->Initialize(driver, genericDriver);
+ if (kr == kIOReturnSuccess)
+ {
+ (*driver)->interface = intf;
+ (*driver)->Initialize(driver, genericDriver);
- (*driver)->plugin = plugin;
- (*driver)->interface = intf;
- *printerDriver = driver;
- }
+ (*driver)->plugin = plugin;
+ (*driver)->interface = intf;
+ *printerDriver = driver;
}
- (*iunknown)->Release(iunknown);
}
- CFRelease(factories);
+ (*iunknown)->Release(iunknown);
}
+ CFRelease(factories);
}
}
-#ifdef DEBUG
- char bundlestr[1024];
- CFStringGetCString(bundle, bundlestr, sizeof(bundlestr), kCFStringEncodingUTF8);
fprintf(stderr, "DEBUG: load_classdriver(%s) (kr:0x%08x)\n", bundlestr, (int)kr);
-#endif /* DEBUG */
- return kr;
+ return (kr);
}
* 'registry_close()' - Close the connection to the printer.
*/
-static kern_return_t registry_close()
+static kern_return_t registry_close(void)
{
if (g.classdriver != NULL)
(*g.classdriver)->Close(g.classdriver);
#pragma mark -
/*
- * 'parse_options()' - Parse uri options.
+ * 'parse_options()' - Parse URI options.
*/
-static void parse_options(const char *options,
+static void parse_options(char *options,
char *serial,
int serial_size,
UInt32 *location,
Boolean *wait_eof)
{
- char optionName[255], /* Name of option */
- value[255], /* Value of option */
- *ptr; /* Pointer into name or value */
+ char sep, /* Separator character */
+ *name, /* Name of option */
+ *value; /* Value of option */
+
if (serial)
*serial = '\0';
if (!options)
return;
- while (*options != '\0')
+ while (*options)
{
- /* Get the name... */
- for (ptr = optionName; *options && *options != '=' && *options != '+';)
- *ptr++ = *options++;
+ /*
+ * Get the name...
+ */
- *ptr = '\0';
- value[0] = '\0';
+ name = options;
- if (*options == '=')
- {
- /* Get the value... */
+ while (*options && *options != '=' && *options != '+' && *options != '&')
options ++;
- for (ptr = value; *options && *options != '+';)
- *ptr++ = *options++;
+ if ((sep = *options) != '\0')
+ *options++ = '\0';
+
+ if (sep == '=')
+ {
+ /*
+ * Get the value...
+ */
- *ptr = '\0';
+ value = options;
- if (*options == '+')
+ while (*options && *options != '+' && *options != '&')
options ++;
+
+ if (*options)
+ *options++ = '\0';
}
- else if (*options == '+')
- options ++;
+ else
+ value = (char *)"";
- /*
- * Process the option...
- */
- if (strcasecmp(optionName, "waiteof") == 0)
+ /*
+ * Process the option...
+ */
+
+ if (!strcasecmp(name, "waiteof"))
{
- if (strcasecmp(value, "on") == 0 ||
- strcasecmp(value, "yes") == 0 ||
- strcasecmp(value, "true") == 0)
+ if (!strcasecmp(value, "on") ||
+ !strcasecmp(value, "yes") ||
+ !strcasecmp(value, "true"))
*wait_eof = true;
- else if (strcasecmp(value, "off") == 0 ||
- strcasecmp(value, "no") == 0 ||
- strcasecmp(value, "false") == 0)
+ else if (!strcasecmp(value, "off") ||
+ !strcasecmp(value, "no") ||
+ !strcasecmp(value, "false"))
*wait_eof = false;
else
- fprintf(stderr, _("WARNING: Boolean expected for waiteof option \"%s\"\n"), value);
+ _cupsLangPrintFilter(stderr, "WARNING",
+ _("Boolean expected for waiteof option \"%s\"."),
+ value);
}
- else if (strcasecmp(optionName, "serial") == 0)
- {
+ else if (!strcasecmp(name, "serial"))
strlcpy(serial, value, serial_size);
- }
- else if (strcasecmp(optionName, "location") == 0 && location)
+ else if (!strcasecmp(name, "location") && location)
*location = strtol(value, NULL, 16);
}
-
- return;
}
/*!
* @function setup_cfLanguage
- * @abstract Convert the contents of the CUPS 'LANG' environment
+ * @abstract Convert the contents of the CUPS 'APPLE_LANGUAGE' environment
* variable into a one element CF array of languages.
*
* @discussion Each submitted job comes with a natural language. CUPS passes
CFArrayRef langArray = NULL;
const char *requestedLang = NULL;
- requestedLang = getenv("LANG");
+ if ((requestedLang = getenv("APPLE_LANGUAGE")) == NULL)
+ requestedLang = getenv("LANG");
+
if (requestedLang != NULL)
{
lang[0] = CFStringCreateWithCString(kCFAllocatorDefault, requestedLang, kCFStringEncodingUTF8);
langArray = CFArrayCreate(kCFAllocatorDefault, (const void **)lang, sizeof(lang) / sizeof(lang[0]), &kCFTypeArrayCallBacks);
- CFPreferencesSetAppValue(CFSTR("AppleLanguages"), langArray, kCFPreferencesCurrentApplication);
- DEBUG_printf((stderr, "DEBUG: usb: AppleLanguages = \"%s\"\n", requestedLang));
+ CFPreferencesSetValue(CFSTR("AppleLanguages"), langArray, kCFPreferencesCurrentApplication, kCFPreferencesAnyUser, kCFPreferencesAnyHost);
+ fprintf(stderr, "DEBUG: usb: AppleLanguages=\"%s\"\n", requestedLang);
CFRelease(lang[0]);
CFRelease(langArray);
}
else
- fputs("DEBUG: usb: LANG environment variable missing.\n", stderr);
+ fputs("DEBUG: usb: LANG and APPLE_LANGUAGE environment variables missing.\n", stderr);
}
#pragma mark -
-#if defined(__i386__)
+#if defined(__i386__) || defined(__x86_64__)
/*!
- * @function run_ppc_backend
+ * @function run_legacy_backend
*
- * @abstract Starts child backend process running as a ppc executable.
+ * @abstract Starts child backend process running as a ppc or i386 executable.
*
* @result Never returns; always calls exit().
*
* @discussion
*/
-static void run_ppc_backend(int argc,
- char *argv[],
- int fd)
+static void run_legacy_backend(int argc,
+ char *argv[],
+ int fd)
{
int i;
int exitstatus = 0;
int childstatus;
pid_t waitpid_status;
char *my_argv[32];
- char *usb_ppc_status;
+ char *usb_legacy_status;
- /*
- * If we're running as i386 and couldn't load the class driver (because they'it's
- * ppc-only) then try to re-exec ourselves in ppc mode to try again. If we don't have
- * a ppc architecture we may be running i386 again so guard against this by setting
- * and testing an environment variable...
- */
- usb_ppc_status = getenv("USB_PPC_STATUS");
+ /*
+ * If we're running as x86_64 or i386 and couldn't load the class driver
+ * (because it's ppc or i386), then try to re-exec ourselves in ppc or i386
+ * mode to try again. If we don't have a ppc or i386 architecture we may be
+ * running with the same architecture again so guard against this by setting
+ * and testing an environment variable...
+ */
+
+# ifdef __x86_64__
+ usb_legacy_status = getenv("USB_I386_STATUS");
+# else
+ usb_legacy_status = getenv("USB_PPC_STATUS");
+# endif /* __x86_64__ */
- if (usb_ppc_status == NULL)
+ if (!usb_legacy_status)
{
/*
* Setup a SIGTERM handler then block it before forking...
*/
+ int err; /* posix_spawn result */
struct sigaction action; /* POSIX signal action */
sigset_t newmask, /* New signal mask */
oldmask; /* Old signal mask */
+ char usbpath[1024]; /* Path to USB backend */
+ const char *cups_serverbin;/* Path to CUPS binaries */
+
memset(&action, 0, sizeof(action));
sigaddset(&action.sa_mask, SIGTERM);
sigaddset(&newmask, SIGTERM);
sigprocmask(SIG_BLOCK, &newmask, &oldmask);
- if ((child_pid = fork()) == 0)
- {
- /*
- * Child comes here...
- */
-
- setenv("USB_PPC_STATUS", "1", false);
-
- /*
- * Unblock signals before doing the exec...
- */
+ /*
+ * Set the environment variable...
+ */
- memset(&action, 0, sizeof(action));
- sigemptyset(&action.sa_mask);
- action.sa_handler = SIG_DFL;
- sigaction(SIGTERM, &action, NULL);
+# ifdef __x86_64__
+ setenv("USB_I386_STATUS", "1", false);
+# else
+ setenv("USB_PPC_STATUS", "1", false);
+# endif /* __x86_64__ */
- sigprocmask(SIG_SETMASK, &oldmask, NULL);
+ /*
+ * Tell the kernel to use the specified CPU architecture...
+ */
- /*
- * Tell the kernel the next exec call should favor the ppc architecture...
- */
+# ifdef __x86_64__
+ cpu_type_t cpu = CPU_TYPE_I386;
+# else
+ cpu_type_t cpu = CPU_TYPE_POWERPC;
+# endif /* __x86_64__ */
+ size_t ocount = 1;
+ posix_spawnattr_t attrs;
- int mib[] = { CTL_KERN, KERN_AFFINITY, 1, 1 };
- int namelen = 4;
- sysctl(mib, namelen, NULL, NULL, NULL, 0);
+ if (!posix_spawnattr_init(&attrs))
+ {
+ posix_spawnattr_setsigdefault(&attrs, &oldmask);
+ if (posix_spawnattr_setbinpref_np(&attrs, 1, &cpu, &ocount) || ocount != 1)
+ {
+# ifdef __x86_64__
+ perror("DEBUG: Unable to set binary preference to i386");
+# else
+ perror("DEBUG: Unable to set binary preference to ppc");
+# endif /* __x86_64__ */
+ _cupsLangPrintFilter(stderr, "ERROR",
+ _("Unable to use legacy USB class driver."));
+ exit(CUPS_BACKEND_STOP);
+ }
+ }
- /*
- * Set up the arguments and call exec...
- */
+ /*
+ * Set up the arguments and call posix_spawn...
+ */
- for (i = 0; i < argc && i < (sizeof(my_argv)/sizeof(my_argv[0])) - 1; i++)
- my_argv[i] = argv[i];
+ if ((cups_serverbin = getenv("CUPS_SERVERBIN")) == NULL)
+ cups_serverbin = CUPS_SERVERBIN;
+ snprintf(usbpath, sizeof(usbpath), "%s/backend/usb", cups_serverbin);
- my_argv[i] = NULL;
+ for (i = 0; i < argc && i < (sizeof(my_argv) / sizeof(my_argv[0])) - 1; i ++)
+ my_argv[i] = argv[i];
- execv("/usr/libexec/cups/backend/usb", my_argv);
+ my_argv[i] = NULL;
- perror("/usr/libexec/cups/backend/usb");
- exit(errno);
- }
- else if (child_pid < 0)
+ if ((err = posix_spawn(&child_pid, usbpath, NULL, &attrs, my_argv,
+ environ)) != 0)
{
- /*
- * Error - couldn't fork a new process!
- */
-
- perror("fork");
- exit(errno);
+ fprintf(stderr, "DEBUG: Unable to exec %s: %s\n", usbpath,
+ strerror(err));
+ _cupsLangPrintFilter(stderr, "ERROR",
+ _("Unable to use legacy USB class driver."));
+ exit(CUPS_BACKEND_STOP);
}
/*
close(fd);
close(1);
- fprintf(stderr, "DEBUG: Started usb(ppc) backend (PID %d)\n", (int)child_pid);
+ fprintf(stderr, "DEBUG: Started usb(legacy) backend (PID %d)\n",
+ (int)child_pid);
while ((waitpid_status = waitpid(child_pid, &childstatus, 0)) == (pid_t)-1 && errno == EINTR)
usleep(1000);
if (WIFSIGNALED(childstatus))
{
- exitstatus = WTERMSIG(childstatus);
- fprintf(stderr, "DEBUG: usb(ppc) backend %d crashed on signal %d!\n", child_pid, exitstatus);
+ exitstatus = CUPS_BACKEND_STOP;
+ fprintf(stderr, "DEBUG: usb(legacy) backend %d crashed on signal %d\n",
+ child_pid, WTERMSIG(childstatus));
}
else
{
if ((exitstatus = WEXITSTATUS(childstatus)) != 0)
- fprintf(stderr, "DEBUG: usb(ppc) backend %d stopped with status %d!\n", child_pid, exitstatus);
+ fprintf(stderr,
+ "DEBUG: usb(legacy) backend %d stopped with status %d\n",
+ child_pid, exitstatus);
else
- fprintf(stderr, "DEBUG: PID %d exited with no errors\n", child_pid);
+ fprintf(stderr, "DEBUG: usb(legacy) backend %d exited with no errors\n",
+ child_pid);
}
}
else
{
- fputs("DEBUG: usb child running i386 again\n", stderr);
- exitstatus = ENOENT;
+ fputs("DEBUG: usb(legacy) backend running native again\n", stderr);
+ exitstatus = CUPS_BACKEND_STOP;
}
exit(exitstatus);
}
+#endif /* __i386__ || __x86_64__ */
+
/*
* 'sigterm_handler()' - SIGTERM handler.
*/
-static void sigterm_handler(int sig)
+static void
+sigterm_handler(int sig) /* I - Signal */
{
- /* If we started a child process pass the signal on to it...
- */
+#if defined(__i386__) || defined(__x86_64__)
+ /*
+ * If we started a child process pass the signal on to it...
+ */
+
if (child_pid)
+ {
+ /*
+ * If we started a child process pass the signal on to it...
+ */
+
+ int status;
+
kill(child_pid, sig);
+ while (waitpid(child_pid, &status, 0) < 0 && errno == EINTR);
- exit(1);
+ if (WIFEXITED(status))
+ exit(WEXITSTATUS(status));
+ else if (status == SIGTERM || status == SIGKILL)
+ exit(0);
+ else
+ {
+ fprintf(stderr, "DEBUG: Child crashed on signal %d\n", status);
+ exit(CUPS_BACKEND_STOP);
+ }
+ }
+#endif /* __i386__ || __x86_64__ */
}
-#endif /* __i386__ */
-
#ifdef PARSE_PS_ERRORS
/*
/*
- * 'soft_reset'
+ * 'soft_reset()' - Send a soft reset to the device.
*/
-static void soft_reset()
+static void soft_reset(void)
{
fd_set input_set; /* Input set for select() */
- struct timeval stimeout; /* Timeout for select() */
+ struct timeval tv; /* Time value */
char buffer[2048]; /* Buffer */
struct timespec cond_timeout; /* pthread condition timeout */
{
(*g.classdriver)->Abort(g.classdriver);
- cond_timeout.tv_sec = time(NULL) + 1;
- cond_timeout.tv_nsec = 0;
+ gettimeofday(&tv, NULL);
+ cond_timeout.tv_sec = tv.tv_sec + 1;
+ cond_timeout.tv_nsec = tv.tv_usec * 1000;
- pthread_cond_timedwait(&g.readwrite_lock_cond, &g.readwrite_lock_mutex, &cond_timeout);
+ while (g.readwrite_lock)
+ {
+ if (pthread_cond_timedwait(&g.readwrite_lock_cond,
+ &g.readwrite_lock_mutex,
+ &cond_timeout) != 0)
+ break;
+ }
}
g.readwrite_lock = 1;
FD_ZERO(&input_set);
FD_SET(g.print_fd, &input_set);
- stimeout.tv_sec = 0;
- stimeout.tv_usec = 0;
+ tv.tv_sec = 0;
+ tv.tv_usec = 0;
- while (select(g.print_fd+1, &input_set, NULL, NULL, &stimeout) > 0)
+ while (select(g.print_fd+1, &input_set, NULL, NULL, &tv) > 0)
if (read(g.print_fd, buffer, sizeof(buffer)) <= 0)
break;
* Send the reset...
*/
- (*g.classdriver)->SoftReset(g.classdriver, 0);
+ (*g.classdriver)->SoftReset(g.classdriver, DEFAULT_TIMEOUT);
/*
* Release the I/O lock...
char *data,
int *datalen)
{
- UInt32 deviceLocation = 0;
- UInt8 interfaceNum = 0;
CFStringRef deviceIDString = NULL;
/* GetDeviceID */
- copy_devicestring(g.printer_obj, &deviceIDString, &deviceLocation, &interfaceNum);
+ copy_deviceid(g.classdriver, &deviceIDString);
+
if (deviceIDString)
{
CFStringGetCString(deviceIDString, data, *datalen, kCFStringEncodingUTF8);
/*
- * End of "$Id: usb-darwin.c 6680 2007-07-16 18:46:16Z mike $".
+ * End of "$Id: usb-darwin.c 7953 2008-09-17 01:43:19Z mike $".
*/