]> git.ipfire.org Git - thirdparty/cups.git/blobdiff - cups/sidechannel.c
Merge changes from CUPS 1.5svn-r9049 (private header support)
[thirdparty/cups.git] / cups / sidechannel.c
index 9a009e9690fd7e83dcfec5e19dc68dae6d4ddf18..17abb2b3f4704f64925b073a42a526a5f1feb737 100644 (file)
@@ -1,9 +1,9 @@
 /*
- * "$Id: sidechannel.c 6649 2007-07-11 21:46:42Z mike $"
+ * "$Id: sidechannel.c 7720 2008-07-11 22:46:21Z mike $"
  *
- *   Side-channel API code for the Common UNIX Printing System (CUPS).
+ *   Side-channel API code for CUPS.
  *
- *   Copyright 2007-2008 by Apple Inc.
+ *   Copyright 2007-2010 by Apple Inc.
  *   Copyright 2006 by Easy Software Products.
  *
  *   These coded instructions, statements, and computer programs are the
  */
 
 #include "sidechannel.h"
-#include "string.h"
-#include "debug.h"
-#include <unistd.h>
-#include <errno.h>
+#include "string-private.h"
+#include "debug-private.h"
+#ifdef WIN32
+#  include <io.h>
+#else
+#  include <unistd.h>
+#endif /* WIN32 */
 #ifdef __hpux
 #  include <sys/time.h>
-#else
+#elif !defined(WIN32)
 #  include <sys/select.h>
 #endif /* __hpux */
 #ifndef WIN32
@@ -59,7 +62,7 @@
  * pointed to by the "data" parameter.  cupsSideChannelDoRequest() will
  * update the value to contain the number of data bytes in the buffer.
  *
- * @since CUPS 1.3@
+ * @since CUPS 1.3/Mac OS X 10.5@
  */
 
 cups_sc_status_t                       /* O  - Status of command */
@@ -98,7 +101,7 @@ cupsSideChannelDoRequest(
  * pointed to by the "data" parameter.  cupsSideChannelDoRequest() will
  * update the value to contain the number of data bytes in the buffer.
  *
- * @since CUPS 1.3@
+ * @since CUPS 1.3/Mac OS X 10.5@
  */
 
 int                                    /* O - 0 on success, -1 on error */
@@ -112,6 +115,7 @@ cupsSideChannelRead(
   char         buffer[16388];          /* Message buffer */
   int          bytes;                  /* Bytes read */
   int          templen;                /* Data length from message */
+  int          nfds;                   /* Number of file descriptors */
 #ifdef HAVE_POLL
   struct pollfd        pfd;                    /* Poll structure for poll() */
 #else /* select() */
@@ -121,7 +125,7 @@ cupsSideChannelRead(
 
 
   DEBUG_printf(("cupsSideChannelRead(command=%p, status=%p, data=%p, "
-                "datalen=%p(%d), timeout=%.3f)\n", command, status, data,
+                "datalen=%p(%d), timeout=%.3f)", command, status, data,
                datalen, datalen ? *datalen : -1, timeout));
 
  /*
@@ -139,39 +143,31 @@ cupsSideChannelRead(
   pfd.fd     = CUPS_SC_FD;
   pfd.events = POLLIN;
 
-  if (timeout < 0.0)
-  {
-    if (poll(&pfd, 1, -1) < 1)
-      return (-1);
-  }
-  else if (poll(&pfd, 1, (long)(timeout * 1000)) < 1)
-    return (-1);
+  while ((nfds = poll(&pfd, 1, 
+                     timeout < 0.0 ? -1 : (long)(timeout * 1000))) < 0 &&
+        (errno == EINTR || errno == EAGAIN))
+    ;
 
 #else /* select() */
   FD_ZERO(&input_set);
   FD_SET(CUPS_SC_FD, &input_set);
 
-  if (timeout < 0.0)
-  {
-    if (select(CUPS_SC_FD + 1, &input_set, NULL, NULL, NULL) < 1)
-    {
-      DEBUG_printf(("cupsSideChannelRead: Select error: %s\n", strerror(errno)));
-      return (-1);
-    }
-  }
-  else
-  {
-    stimeout.tv_sec  = (int)timeout;
-    stimeout.tv_usec = (int)(timeout * 1000000) % 1000000;
+  stimeout.tv_sec  = (int)timeout;
+  stimeout.tv_usec = (int)(timeout * 1000000) % 1000000;
+
+  while ((nfds = select(CUPS_SC_FD + 1, &input_set, NULL, NULL, 
+                       timeout < 0.0 ? NULL : &stimeout)) < 0 &&
+        (errno == EINTR || errno == EAGAIN))
+    ;
 
-    if (select(CUPS_SC_FD + 1, &input_set, NULL, NULL, &stimeout) < 1)
-    {
-      DEBUG_puts("cupsSideChannelRead: Select timeout");
-      return (-1);
-    }
-  }
 #endif /* HAVE_POLL */
 
+  if  (nfds < 1)
+  {
+    *status = nfds==0 ? CUPS_SC_STATUS_TIMEOUT : CUPS_SC_STATUS_IO_ERROR;
+    return (-1);
+  }
+
  /*
   * Read a side-channel message for the format:
   *
@@ -186,10 +182,24 @@ cupsSideChannelRead(
   while ((bytes = read(CUPS_SC_FD, buffer, sizeof(buffer))) < 0)
     if (errno != EINTR && errno != EAGAIN)
     {
-      DEBUG_printf(("cupsSideChannelRead: Read error: %s\n", strerror(errno)));
+      DEBUG_printf(("1cupsSideChannelRead: Read error: %s", strerror(errno)));
+      *command = CUPS_SC_CMD_NONE;
+      *status  = CUPS_SC_STATUS_IO_ERROR;
       return (-1);
     }
 
+ /*
+  * Watch for EOF or too few bytes...
+  */
+
+  if (bytes < 4)
+  {
+    DEBUG_printf(("1cupsSideChannelRead: Short read of %d bytes", bytes));
+    *command = CUPS_SC_CMD_NONE;
+    *status  = CUPS_SC_STATUS_BAD_MESSAGE;
+    return (-1);
+  }
+
  /*
   * Validate the command code in the message...
   */
@@ -197,7 +207,9 @@ cupsSideChannelRead(
   if (buffer[0] < CUPS_SC_CMD_SOFT_RESET ||
       buffer[0] > CUPS_SC_CMD_SNMP_GET_NEXT)
   {
-    DEBUG_printf(("cupsSideChannelRead: Bad command %d!\n", buffer[0]));
+    DEBUG_printf(("1cupsSideChannelRead: Bad command %d!", buffer[0]));
+    *command = CUPS_SC_CMD_NONE;
+    *status  = CUPS_SC_STATUS_BAD_MESSAGE;
     return (-1);
   }
 
@@ -218,7 +230,7 @@ cupsSideChannelRead(
 
     *status = CUPS_SC_STATUS_TOO_BIG;
   }
-  else if (templen > *datalen || templen > (bytes - 4))
+  else if (!datalen || templen > *datalen || templen > (bytes - 4))
   {
    /*
     * Either the response is bigger than the provided buffer or the
@@ -240,7 +252,7 @@ cupsSideChannelRead(
     memcpy(data, buffer + 4, templen);
   }
 
-  DEBUG_printf(("cupsSideChannelRead: Returning status=%d\n", *status));
+  DEBUG_printf(("1cupsSideChannelRead: Returning status=%d", *status));
 
   return (0);
 }
@@ -266,7 +278,7 @@ cupsSideChannelRead(
  * support SNMP queries.  @code CUPS_SC_STATUS_NO_RESPONSE@ is returned when
  * the printer does not respond to the SNMP query.
  *
- * @since CUPS 1.4@ 
+ * @since CUPS 1.4/Mac OS X 10.6
  */
 
 cups_sc_status_t                       /* O  - Query status */
@@ -284,7 +296,7 @@ cupsSideChannelSNMPGet(
 
 
   DEBUG_printf(("cupsSideChannelSNMPGet(oid=\"%s\", data=%p, datalen=%p(%d), "
-                "timeout=%.3f)\n", oid, data, datalen, datalen ? *datalen : -1,
+                "timeout=%.3f)", oid, data, datalen, datalen ? *datalen : -1,
                timeout));
 
  /*
@@ -301,7 +313,7 @@ cupsSideChannelSNMPGet(
   */
 
   if (cupsSideChannelWrite(CUPS_SC_CMD_SNMP_GET, CUPS_SC_STATUS_NONE, oid,
-                           (int)strlen(oid), timeout))
+                           (int)strlen(oid) + 1, timeout))
     return (CUPS_SC_STATUS_TIMEOUT);
 
   real_datalen = sizeof(real_data);
@@ -358,7 +370,7 @@ cupsSideChannelSNMPGet(
  * support SNMP queries.  @code CUPS_SC_STATUS_NO_RESPONSE@ is returned when
  * the printer does not respond to the first SNMP query.
  *
- * @since CUPS 1.4@ 
+ * @since CUPS 1.4/Mac OS X 10.6
  */
 
 cups_sc_status_t                       /* O - Status of first query of @code CUPS_SC_STATUS_OK@ on success */
@@ -375,10 +387,11 @@ cupsSideChannelSNMPWalk(
                        real_oidlen,    /* Length of returned OID string */
                        oidlen;         /* Length of first OID */
   const char           *current_oid;   /* Current OID */
+  char                 last_oid[2048]; /* Last OID */
 
 
   DEBUG_printf(("cupsSideChannelSNMPWalk(oid=\"%s\", timeout=%.3f, cb=%p, "
-                "context=%p)\n", oid, timeout, cb, context));
+                "context=%p)", oid, timeout, cb, context));
 
  /*
   * Range check input...
@@ -393,6 +406,7 @@ cupsSideChannelSNMPWalk(
 
   current_oid = oid;
   oidlen      = (int)strlen(oid);
+  last_oid[0] = '\0';
 
   do
   {
@@ -401,7 +415,7 @@ cupsSideChannelSNMPWalk(
     */
 
     if (cupsSideChannelWrite(CUPS_SC_CMD_SNMP_GET_NEXT, CUPS_SC_STATUS_NONE,
-                             current_oid, (int)strlen(current_oid), timeout))
+                             current_oid, (int)strlen(current_oid) + 1, timeout))
       return (CUPS_SC_STATUS_TIMEOUT);
 
     real_datalen = sizeof(real_data);
@@ -418,7 +432,8 @@ cupsSideChannelSNMPWalk(
       * Parse the response of the form "oid\0value"...
       */
 
-      if (strncmp(real_data, oid, oidlen) || real_data[oidlen] != '.')
+      if (strncmp(real_data, oid, oidlen) || real_data[oidlen] != '.' ||
+          !strcmp(real_data, last_oid))
       {
        /*
         * Done with this set of OIDs...
@@ -427,6 +442,9 @@ cupsSideChannelSNMPWalk(
         return (CUPS_SC_STATUS_OK);
       }
 
+      if (real_datalen < sizeof(real_data))
+        real_data[real_datalen] = '\0';
+
       real_oidlen  = strlen(real_data) + 1;
       real_datalen -= real_oidlen;
 
@@ -441,6 +459,7 @@ cupsSideChannelSNMPWalk(
       */
 
       current_oid = real_data;
+      strlcpy(last_oid, current_oid, sizeof(last_oid));
     }
   }
   while (status == CUPS_SC_STATUS_OK);
@@ -455,7 +474,7 @@ cupsSideChannelSNMPWalk(
  * This function is normally only called by backend programs to send
  * responses to a filter, driver, or port monitor program.
  *
- * @since CUPS 1.3@
+ * @since CUPS 1.3/Mac OS X 10.5@
  */
 
 int                                    /* O - 0 on success, -1 on error */
@@ -552,5 +571,5 @@ cupsSideChannelWrite(
 
 
 /*
- * End of "$Id: sidechannel.c 6649 2007-07-11 21:46:42Z mike $".
+ * End of "$Id: sidechannel.c 7720 2008-07-11 22:46:21Z mike $".
  */