]> git.ipfire.org Git - thirdparty/cups.git/blobdiff - backend/runloop.c
Add option to specify a single operation to test with.
[thirdparty/cups.git] / backend / runloop.c
index bbf565d5fee73cdc742cd1eabbb9052da3d207ad..0b3f65f7864a55a80304a3b16f24eb6ba348e839 100644 (file)
@@ -1,9 +1,9 @@
 /*
- * "$Id: runloop.c 6834 2007-08-22 18:29:25Z mike $"
+ * "$Id: runloop.c 9565 2011-02-23 00:08:08Z mike $"
  *
- *   Common run loop APIs for the Common UNIX Printing System (CUPS).
+ *   Common run loop APIs for CUPS backends.
  *
- *   Copyright 2007-2008 by Apple Inc.
+ *   Copyright 2007-2012 by Apple Inc.
  *   Copyright 2006-2007 by Easy Software Products, all rights reserved.
  *
  *   These coded instructions, statements, and computer programs are the
@@ -18,6 +18,8 @@
  *
  *   backendDrainOutput() - Drain pending print data to the device.
  *   backendRunLoop()     - Read and write print and back-channel data.
+ *   backendWaitLoop()    - Wait for input from stdin while handling
+ *                          side-channel queries.
  */
 
 /*
@@ -90,7 +92,8 @@ backendDrainOutput(int print_fd,      /* I - Print file descriptor */
 
       if (errno != EAGAIN || errno != EINTR)
       {
-       perror("ERROR: Unable to read print data");
+       fprintf(stderr, "DEBUG: Read failed: %s\n", strerror(errno));
+       _cupsLangPrintFilter(stderr, "ERROR", _("Unable to read print data."));
        return (-1);
       }
 
@@ -119,8 +122,7 @@ backendDrainOutput(int print_fd,    /* I - Print file descriptor */
         if (errno != ENOSPC && errno != ENXIO && errno != EAGAIN &&
            errno != EINTR && errno != ENOTTY)
        {
-         _cupsLangPrintf(stderr, _("ERROR: Unable to write print data: %s\n"),
-                         strerror(errno));
+         _cupsLangPrintError("ERROR", _("Unable to write print data"));
          return (-1);
        }
       }
@@ -136,84 +138,19 @@ backendDrainOutput(int print_fd,  /* I - Print file descriptor */
 }
 
 
-/*
- * '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         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          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? */
+    int          update_state,         /* I - Update printer-state-reasons? */
+    _cups_sccb_t side_cb)              /* I - Side-channel callback */
 {
   int          nfds;                   /* Maximum file descriptor value + 1 */
   fd_set       input,                  /* Input set for reading */
@@ -261,6 +198,14 @@ backendRunLoop(
     signal(SIGTERM, SIG_IGN);
 #endif /* HAVE_SIGSET */
   }
+  else if (print_fd < 0)
+  {
+   /*
+    * Copy print data from stdin, but don't mess with the signal handlers...
+    */
+
+    print_fd = 0;
+  }
 
  /*
   * Figure out the maximum file descriptor value to use with select()...
@@ -284,7 +229,7 @@ backendRunLoop(
       FD_SET(print_fd, &input);
     if (use_bc)
       FD_SET(device_fd, &input);
-    if (side_cb)
+    if (!print_bytes && side_cb)
       FD_SET(CUPS_SC_FD, &input);
 
     FD_ZERO(&output);
@@ -302,16 +247,17 @@ backendRunLoop(
        * Pause printing to clear any pending errors...
        */
 
-       if (errno == ENXIO && offline != 1)
+       if (errno == ENXIO && offline != 1 && update_state)
        {
-         fputs("STATE: +offline-error\n", stderr);
-         _cupsLangPuts(stderr, _("INFO: Printer is currently offline.\n"));
+         fputs("STATE: +offline-report\n", stderr);
+         _cupsLangPrintFilter(stderr, "INFO",
+                              _("The printer is not connected."));
          offline = 1;
        }
        else if (errno == EINTR && total_bytes == 0)
        {
          fputs("DEBUG: Received an interrupt before any bytes were "
-               "written, aborting!\n", stderr);
+               "written, aborting.\n", stderr);
           return (0);
        }
 
@@ -325,7 +271,16 @@ backendRunLoop(
     */
 
     if (side_cb && FD_ISSET(CUPS_SC_FD, &input))
-      (*side_cb)(print_fd, device_fd, snmp_fd, addr, use_bc);
+    {
+     /*
+      * Do the side-channel request, then start back over in the select
+      * loop since it may have read from print_fd...
+      */
+
+      if ((*side_cb)(print_fd, device_fd, snmp_fd, addr, use_bc))
+        side_cb = NULL;
+      continue;
+    }
 
    /*
     * Check if we have back-channel data ready...
@@ -336,10 +291,18 @@ backendRunLoop(
       if ((bc_bytes = read(device_fd, bc_buffer, sizeof(bc_buffer))) > 0)
       {
        fprintf(stderr,
-               "DEBUG: Received " CUPS_LLFMT " bytes of back-channel data!\n",
+               "DEBUG: Received " CUPS_LLFMT " bytes of back-channel data\n",
                CUPS_LLCAST bc_bytes);
         cupsBackChannelWrite(bc_buffer, bc_bytes, 1.0);
       }
+      else if (bc_bytes < 0 && errno != EAGAIN && errno != EINTR)
+      {
+        fprintf(stderr, "DEBUG: Error reading back-channel data: %s\n",
+               strerror(errno));
+       use_bc = 0;
+      }
+      else if (bc_bytes == 0)
+        use_bc = 0;
     }
 
    /*
@@ -357,7 +320,9 @@ backendRunLoop(
 
        if (errno != EAGAIN || errno != EINTR)
        {
-         perror("ERROR: Unable to read print data");
+         fprintf(stderr, "DEBUG: Read failed: %s\n", strerror(errno));
+         _cupsLangPrintFilter(stderr, "ERROR",
+                              _("Unable to read print data."));
          return (-1);
        }
 
@@ -393,41 +358,42 @@ backendRunLoop(
 
         if (errno == ENOSPC)
        {
-         if (paperout != 1)
+         if (paperout != 1 && update_state)
          {
-           fputs("STATE: +media-empty-error\n", stderr);
-           _cupsLangPuts(stderr, _("ERROR: Out of paper!\n"));
+           fputs("STATE: +media-empty-warning\n", stderr);
+           fputs("DEBUG: Out of paper\n", stderr);
            paperout = 1;
          }
         }
        else if (errno == ENXIO)
        {
-         if (offline != 1)
+         if (offline != 1 && update_state)
          {
-           fputs("STATE: +offline-error\n", stderr);
-           _cupsLangPuts(stderr, _("INFO: Printer is currently off-line.\n"));
+           fputs("STATE: +offline-report\n", stderr);
+           _cupsLangPrintFilter(stderr, "INFO",
+                                _("The printer is not connected."));
            offline = 1;
          }
        }
        else if (errno != EAGAIN && errno != EINTR && errno != ENOTTY)
        {
-         fprintf(stderr, _("ERROR: Unable to write print data: %s\n"),
-                 strerror(errno));
+         _cupsLangPrintError("ERROR", _("Unable to write print data"));
          return (-1);
        }
       }
       else
       {
-        if (paperout)
+        if (paperout && update_state)
        {
-         fputs("STATE: -media-empty-error\n", stderr);
+         fputs("STATE: -media-empty-warning\n", stderr);
          paperout = 0;
        }
 
-       if (offline)
+       if (offline && update_state)
        {
-         fputs("STATE: -offline-error\n", stderr);
-         _cupsLangPuts(stderr, _("INFO: Printer is now online.\n"));
+         fputs("STATE: -offline-report\n", stderr);
+         _cupsLangPrintFilter(stderr, "INFO",
+                              _("The printer is now connected."));
          offline = 0;
        }
 
@@ -461,5 +427,117 @@ backendRunLoop(
 
 
 /*
- * End of "$Id: runloop.c 6834 2007-08-22 18:29:25Z mike $".
+ * 'backendWaitLoop()' - Wait for input from stdin while handling side-channel
+ *                       queries.
+ */
+
+int                                    /* O - 1 if data is ready, 0 if not */
+backendWaitLoop(
+    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? */
+    _cups_sccb_t side_cb)              /* I - Side-channel callback */
+{
+  int                  nfds;           /* Number of file descriptors */
+  fd_set               input;          /* Input set for reading */
+  time_t               curtime = 0,    /* Current time */
+                       snmp_update = 0;/* Last SNMP status update */
+  struct timeval       timeout;        /* Timeout for select() */
+
+
+  fprintf(stderr, "DEBUG: backendWaitLoop(snmp_fd=%d, addr=%p, side_cb=%p)\n",
+         snmp_fd, addr, side_cb);
+
+ /*
+  * Now loop until we receive data from stdin...
+  */
+
+  if (snmp_fd >= 0)
+    snmp_update = time(NULL) + 5;
+
+  for (;;)
+  {
+   /*
+    * Use select() to determine whether we have data to copy around...
+    */
+
+    FD_ZERO(&input);
+    FD_SET(0, &input);
+    if (side_cb)
+      FD_SET(CUPS_SC_FD, &input);
+
+    if (snmp_fd >= 0)
+    {
+      curtime         = time(NULL);
+      timeout.tv_sec  = curtime >= snmp_update ? 0 : snmp_update - curtime;
+      timeout.tv_usec = 0;
+
+      nfds = select(CUPS_SC_FD + 1, &input, NULL, NULL, &timeout);
+    }
+    else
+      nfds = select(CUPS_SC_FD + 1, &input, NULL, NULL, NULL);
+
+    if (nfds < 0)
+    {
+     /*
+      * Pause printing to clear any pending errors...
+      */
+
+      if (errno == EINTR)
+      {
+       fputs("DEBUG: Received an interrupt before any bytes were "
+             "written, aborting.\n", stderr);
+       return (0);
+      }
+
+      sleep(1);
+      continue;
+    }
+
+   /*
+    * Check for input on stdin...
+    */
+
+    if (FD_ISSET(0, &input))
+      break;
+
+   /*
+    * Check if we have a side-channel request ready...
+    */
+
+    if (side_cb && FD_ISSET(CUPS_SC_FD, &input))
+    {
+     /*
+      * Do the side-channel request, then start back over in the select
+      * loop since it may have read from print_fd...
+      */
+
+      if ((*side_cb)(0, -1, snmp_fd, addr, use_bc))
+        side_cb = NULL;
+      continue;
+    }
+
+   /*
+    * Do SNMP updates periodically...
+    */
+
+    if (snmp_fd >= 0 && curtime >= snmp_update)
+    {
+      if (backendSNMPSupplies(snmp_fd, addr, NULL, NULL))
+        snmp_fd = -1;
+      else
+        snmp_update = curtime + 5;
+    }
+  }
+
+ /*
+  * Return with success...
+  */
+
+  return (1);
+}
+
+
+/*
+ * End of "$Id: runloop.c 9565 2011-02-23 00:08:08Z mike $".
  */