]> git.ipfire.org Git - thirdparty/cups.git/blobdiff - backend/runloop.c
Import CUPS 1.4svn-r7356.
[thirdparty/cups.git] / backend / runloop.c
index 82673a447a3220f12c517b768c1f319868ca48f7..bbf565d5fee73cdc742cd1eabbb9052da3d207ad 100644 (file)
@@ -1,31 +1,23 @@
 /*
- * "$Id: runloop.c 5776 2006-07-26 20:55:13Z mike $"
+ * "$Id: runloop.c 6834 2007-08-22 18:29:25Z mike $"
  *
- *   Common run loop API for the Common UNIX Printing System (CUPS).
+ *   Common run loop APIs for the Common UNIX Printing System (CUPS).
  *
- *   Copyright 2006 by Easy Software Products, all rights reserved.
+ *   Copyright 2007-2008 by Apple Inc.
+ *   Copyright 2006-2007 by Easy Software Products, all rights reserved.
  *
  *   These coded instructions, statements, and computer programs are the
- *   property of Easy Software Products and are protected by Federal
- *   copyright law.  Distribution and use rights are outlined in the file
+ *   property of Apple Inc. and are protected by Federal copyright
+ *   law.  Distribution and use rights are outlined in the file "LICENSE.txt"
  *   "LICENSE" which should have been included with this file.  If this
- *   file is missing or damaged please contact Easy Software Products
- *   at:
- *
- *       Attn: CUPS Licensing Information
- *       Easy Software Products
- *       44141 Airport View Drive, Suite 204
- *       Hollywood, Maryland 20636 USA
- *
- *       Voice: (301) 373-9600
- *       EMail: cups-info@cups.org
- *         WWW: http://www.cups.org
+ *   file is missing or damaged, see the license at "http://www.cups.org/".
  *
  *   This file is subject to the Apple OS-Developed Software exception.
  *
  * Contents:
  *
- *   backendRunLoop() - Read and write print and back-channel data.
+ *   backendDrainOutput() - Drain pending print data to the device.
+ *   backendRunLoop()     - Read and write print and back-channel data.
  */
 
 /*
@@ -33,6 +25,7 @@
  */
 
 #include "backend-private.h"
+#include <limits.h>
 #ifdef __hpux
 #  include <sys/time.h>
 #else
 #endif /* __hpux */
 
 
+/*
+ * 'backendDrainOutput()' - Drain pending print data to the device.
+ */
+
+int                                    /* O - 0 on success, -1 on error */
+backendDrainOutput(int print_fd,       /* I - Print file descriptor */
+                   int device_fd)      /* I - Device file descriptor */
+{
+  int          nfds;                   /* Maximum file descriptor value + 1 */
+  fd_set       input;                  /* Input set for reading */
+  ssize_t      print_bytes,            /* Print bytes read */
+               bytes;                  /* Bytes written */
+  char         print_buffer[8192],     /* Print data buffer */
+               *print_ptr;             /* Pointer into print data buffer */
+  struct timeval timeout;              /* Timeout for read... */
+
+
+  fprintf(stderr, "DEBUG: backendDrainOutput(print_fd=%d, device_fd=%d)\n",
+          print_fd, device_fd);
+
+ /*
+  * Figure out the maximum file descriptor value to use with select()...
+  */
+
+  nfds = (print_fd > device_fd ? print_fd : device_fd) + 1;
+
+ /*
+  * Now loop until we are out of data from print_fd...
+  */
+
+  for (;;)
+  {
+   /*
+    * Use select() to determine whether we have data to copy around...
+    */
+
+    FD_ZERO(&input);
+    FD_SET(print_fd, &input);
+
+    timeout.tv_sec  = 0;
+    timeout.tv_usec = 0;
+
+    if (select(nfds, &input, NULL, NULL, &timeout) < 0)
+      return (-1);
+
+    if (!FD_ISSET(print_fd, &input))
+      return (0);
+
+    if ((print_bytes = read(print_fd, print_buffer,
+                           sizeof(print_buffer))) < 0)
+    {
+     /*
+      * Read error - bail if we don't see EAGAIN or EINTR...
+      */
+
+      if (errno != EAGAIN || errno != EINTR)
+      {
+       perror("ERROR: Unable to read print data");
+       return (-1);
+      }
+
+      print_bytes = 0;
+    }
+    else if (print_bytes == 0)
+    {
+     /*
+      * End of file, return...
+      */
+
+      return (0);
+    }
+
+    fprintf(stderr, "DEBUG: Read %d bytes of print data...\n",
+           (int)print_bytes);
+
+    for (print_ptr = print_buffer; print_bytes > 0;)
+    {
+      if ((bytes = write(device_fd, print_ptr, print_bytes)) < 0)
+      {
+       /*
+        * Write error - bail if we don't see an error we can retry...
+       */
+
+        if (errno != ENOSPC && errno != ENXIO && errno != EAGAIN &&
+           errno != EINTR && errno != ENOTTY)
+       {
+         _cupsLangPrintf(stderr, _("ERROR: Unable to write print data: %s\n"),
+                         strerror(errno));
+         return (-1);
+       }
+      }
+      else
+      {
+        fprintf(stderr, "DEBUG: Wrote %d bytes of print data...\n", (int)bytes);
+
+        print_bytes -= bytes;
+       print_ptr   += bytes;
+      }
+    }
+  }
+}
+
+
+/*
+ * 'backendNetworkSideCB()' - Handle common network side-channel commands.
+ */
+
+void
+backendNetworkSideCB(
+    int         print_fd,              /* I - Print file or -1 */
+    int         device_fd,             /* I - Device file or -1 */
+    int         snmp_fd,               /* I - SNMP socket */
+    http_addr_t *addr,                 /* I - Address of device */
+    int         use_bc)                        /* I - Use back-channel data? */
+{
+  cups_sc_command_t    command;        /* Request command */
+  cups_sc_status_t     status;         /* Request/response status */
+  char                 data[2048];     /* Request/response data */
+  int                  datalen;        /* Request/response data size */
+  const char           *device_id;     /* 1284DEVICEID env var */
+
+
+  datalen = sizeof(data);
+
+  if (cupsSideChannelRead(&command, &status, data, &datalen, 1.0))
+  {
+    _cupsLangPuts(stderr, _("WARNING: Failed to read side-channel request!\n"));
+    return;
+  }
+
+  switch (command)
+  {
+    case CUPS_SC_CMD_DRAIN_OUTPUT :
+       /*
+        * Our sockets disable the Nagle algorithm and data is sent immediately.
+       */
+
+        if (backendDrainOutput(print_fd, device_fd))
+         status = CUPS_SC_STATUS_IO_ERROR;
+       else 
+          status = CUPS_SC_STATUS_OK;
+
+       datalen = 0;
+        break;
+
+    case CUPS_SC_CMD_GET_BIDI :
+        data[0] = use_bc;
+        datalen = 1;
+        break;
+
+    case CUPS_SC_CMD_GET_DEVICE_ID :
+        if ((device_id = getenv("1284DEVICEID")) != NULL)
+       {
+         strlcpy(data, device_id, sizeof(data));
+         datalen = (int)strlen(data);
+         break;
+       }
+
+    default :
+        status  = CUPS_SC_STATUS_NOT_IMPLEMENTED;
+       datalen = 0;
+       break;
+  }
+
+  cupsSideChannelWrite(command, status, data, datalen, 1.0);
+}
+
+
 /*
  * 'backendRunLoop()' - Read and write print and back-channel data.
  */
 
 ssize_t                                        /* O - Total bytes on success, -1 on error */
-backendRunLoop(int print_fd,           /* I - Print file descriptor */
-               int device_fd,          /* I - Device file descriptor */
-              int use_bc)              /* I - Use back-channel? */
+backendRunLoop(
+    int         print_fd,              /* I - Print file descriptor */
+    int         device_fd,             /* I - Device file descriptor */
+    int         snmp_fd,               /* I - SNMP socket or -1 if none */
+    http_addr_t *addr,                 /* I - Address of device */
+    int         use_bc,                        /* I - Use back-channel? */
+    void        (*side_cb)(int, int, int, http_addr_t *, int))
+                                       /* I - Side-channel callback */
 {
   int          nfds;                   /* Maximum file descriptor value + 1 */
   fd_set       input,                  /* Input set for reading */
@@ -61,13 +227,18 @@ backendRunLoop(int print_fd,               /* I - Print file descriptor */
   char         print_buffer[8192],     /* Print data buffer */
                *print_ptr,             /* Pointer into print data buffer */
                bc_buffer[1024];        /* Back-channel data buffer */
+  struct timeval timeout;              /* Timeout for select() */
+  time_t       curtime,                /* Current time */
+               snmp_update = 0;
 #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
   struct sigaction action;             /* Actions for POSIX signals */
 #endif /* HAVE_SIGACTION && !HAVE_SIGSET */
 
 
-  fprintf(stderr, "DEBUG: backendRunLoop(print_fd=%d, device_fd=%d, use_bc=%d)\n",
-          print_fd, device_fd, use_bc);
+  fprintf(stderr,
+          "DEBUG: backendRunLoop(print_fd=%d, device_fd=%d, snmp_fd=%d, "
+         "addr=%p, use_bc=%d, side_cb=%p)\n",
+          print_fd, device_fd, snmp_fd, addr, use_bc, side_cb);
 
  /*
   * If we are printing data from a print driver on stdin, ignore SIGTERM
@@ -101,7 +272,8 @@ backendRunLoop(int print_fd,                /* I - Print file descriptor */
   * Now loop until we are out of data from print_fd...
   */
 
-  for (print_bytes = 0, print_ptr = print_buffer, offline = 0, paperout = 0, total_bytes = 0;;)
+  for (print_bytes = 0, print_ptr = print_buffer, offline = -1,
+           paperout = -1, total_bytes = 0;;)
   {
    /*
     * Use select() to determine whether we have data to copy around...
@@ -112,31 +284,49 @@ backendRunLoop(int print_fd,              /* I - Print file descriptor */
       FD_SET(print_fd, &input);
     if (use_bc)
       FD_SET(device_fd, &input);
+    if (side_cb)
+      FD_SET(CUPS_SC_FD, &input);
 
     FD_ZERO(&output);
-    if (print_bytes || !use_bc)
+    if (print_bytes || (!use_bc && !side_cb))
       FD_SET(device_fd, &output);
 
-    if (use_bc)
+    if (use_bc || side_cb)
     {
-      if (select(nfds, &input, &output, NULL, NULL) < 0)
+      timeout.tv_sec  = 5;
+      timeout.tv_usec = 0;
+
+      if (select(nfds, &input, &output, NULL, &timeout) < 0)
       {
        /*
        * Pause printing to clear any pending errors...
        */
 
-       if (errno == ENXIO && !offline)
+       if (errno == ENXIO && offline != 1)
        {
          fputs("STATE: +offline-error\n", stderr);
-         fputs("INFO: Printer is currently off-line.\n", stderr);
+         _cupsLangPuts(stderr, _("INFO: Printer is currently offline.\n"));
          offline = 1;
        }
+       else if (errno == EINTR && total_bytes == 0)
+       {
+         fputs("DEBUG: Received an interrupt before any bytes were "
+               "written, aborting!\n", stderr);
+          return (0);
+       }
 
        sleep(1);
        continue;
       }
     }
 
+   /*
+    * Check if we have a side-channel request ready...
+    */
+
+    if (side_cb && FD_ISSET(CUPS_SC_FD, &input))
+      (*side_cb)(print_fd, device_fd, snmp_fd, addr, use_bc);
+
    /*
     * Check if we have back-channel data ready...
     */
@@ -203,25 +393,26 @@ backendRunLoop(int print_fd,              /* I - Print file descriptor */
 
         if (errno == ENOSPC)
        {
-         if (!paperout)
+         if (paperout != 1)
          {
-           fputs("ERROR: Out of paper!\n", stderr);
-           fputs("STATUS: +media-tray-empty-error\n", stderr);
+           fputs("STATE: +media-empty-error\n", stderr);
+           _cupsLangPuts(stderr, _("ERROR: Out of paper!\n"));
            paperout = 1;
          }
         }
        else if (errno == ENXIO)
        {
-         if (!offline)
+         if (offline != 1)
          {
            fputs("STATE: +offline-error\n", stderr);
-           fputs("INFO: Printer is currently off-line.\n", stderr);
+           _cupsLangPuts(stderr, _("INFO: Printer is currently off-line.\n"));
            offline = 1;
          }
        }
        else if (errno != EAGAIN && errno != EINTR && errno != ENOTTY)
        {
-         perror("ERROR: Unable to write print data");
+         fprintf(stderr, _("ERROR: Unable to write print data: %s\n"),
+                 strerror(errno));
          return (-1);
        }
       }
@@ -229,14 +420,14 @@ backendRunLoop(int print_fd,              /* I - Print file descriptor */
       {
         if (paperout)
        {
-         fputs("STATUS: -media-tray-empty-error\n", stderr);
+         fputs("STATE: -media-empty-error\n", stderr);
          paperout = 0;
        }
 
        if (offline)
        {
          fputs("STATE: -offline-error\n", stderr);
-         fputs("INFO: Printer is now on-line.\n", stderr);
+         _cupsLangPuts(stderr, _("INFO: Printer is now online.\n"));
          offline = 0;
        }
 
@@ -247,6 +438,18 @@ backendRunLoop(int print_fd,               /* I - Print file descriptor */
        total_bytes += bytes;
       }
     }
+
+   /*
+    * Do SNMP updates periodically...
+    */
+
+    if (snmp_fd >= 0 && time(&curtime) >= snmp_update)
+    {
+      if (backendSNMPSupplies(snmp_fd, addr, NULL, NULL))
+        snmp_update = INT_MAX;
+      else
+        snmp_update = curtime + 5;
+    }
   }
 
  /*
@@ -258,5 +461,5 @@ backendRunLoop(int print_fd,                /* I - Print file descriptor */
 
 
 /*
- * End of "$Id: runloop.c 5776 2006-07-26 20:55:13Z mike $".
+ * End of "$Id: runloop.c 6834 2007-08-22 18:29:25Z mike $".
  */