]> git.ipfire.org Git - thirdparty/cups.git/blobdiff - scheduler/subscriptions.c
Update svn:keyword properties.
[thirdparty/cups.git] / scheduler / subscriptions.c
index 81dd255ac3d4f2f7661ff4fbee2fec82e5f9071e..2e9eee3770b5115a387d7529e737d5041d72cd7f 100644 (file)
@@ -1,25 +1,16 @@
 /*
  * "$Id$"
  *
- *   Subscription routines for the Common UNIX Printing System (CUPS) scheduler.
+ *   Subscription routines for the CUPS scheduler.
  *
- *   Copyright 1997-2006 by Easy Software Products, all rights reserved.
+ *   Copyright 2007-2011 by Apple Inc.
+ *   Copyright 1997-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
- *   "LICENSE.txt" 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
+ *   property of Apple Inc. and are protected by Federal copyright
+ *   law.  Distribution and use rights are outlined in the file "LICENSE.txt"
+ *   which should have been included with this file.  If this file is
+ *   file is missing or damaged, see the license at "http://www.cups.org/".
  *
  * Contents:
  *
  *   cupsdLoadAllSubscriptions()   - Load all subscriptions from the .conf file.
  *   cupsdSaveAllSubscriptions()   - Save all subscriptions to the .conf file.
  *   cupsdStopAllNotifiers()       - Stop all notifier processes.
- *   cupsdUpdateNotifierStatus()   - Read messages from notifiers.
  *   cupsd_compare_subscriptions() - Compare two subscriptions.
  *   cupsd_delete_event()          - Delete a single event...
  *   cupsd_send_dbus()             - Send a DBUS notification...
  *   cupsd_send_notification()     - Send a notification for the specified
  *                                   event.
  *   cupsd_start_notifier()        - Start a notifier subprocess...
+ *   cupsd_update_notifier()       - Read messages from notifiers.
  */
 
 /*
 #include "cupsd.h"
 #ifdef HAVE_DBUS
 #  include <dbus/dbus.h>
+#  ifdef HAVE_DBUS_MESSAGE_ITER_INIT_APPEND
+#    define dbus_message_append_iter_init dbus_message_iter_init_append
+#    define dbus_message_iter_append_string(i,v) dbus_message_iter_append_basic(i, DBUS_TYPE_STRING, &(v))
+#    define dbus_message_iter_append_uint32(i,v) dbus_message_iter_append_basic(i, DBUS_TYPE_UINT32, &(v))
+#  endif /* HAVE_DBUS_MESSAGE_ITER_INIT_APPEND */
 #endif /* HAVE_DBUS */
 
 
@@ -68,6 +64,7 @@ static void   cupsd_send_dbus(cupsd_eventmask_t event, cupsd_printer_t *dest,
 static void    cupsd_send_notification(cupsd_subscription_t *sub,
                                        cupsd_event_t *event);
 static void    cupsd_start_notifier(cupsd_subscription_t *sub);
+static void    cupsd_update_notifier(void);
 
 
 /*
@@ -89,6 +86,11 @@ cupsdAddEvent(
   cupsd_subscription_t *sub;           /* Current subscription */
 
 
+  cupsdLogMessage(CUPSD_LOG_DEBUG2,
+                  "cupsdAddEvent(event=%s, dest=%p(%s), job=%p(%d), text=\"%s\", ...)",
+                 cupsdEventName(event), dest, dest ? dest->name : "",
+                 job, job ? job->id : 0, text);
+
  /*
   * Keep track of events with any OS-supplied notification mechanisms...
   */
@@ -144,7 +146,11 @@ cupsdAddEvent(
       temp->time  = time(NULL);
       temp->attrs = ippNew();
       temp->job   = job;
-      temp->dest  = dest;
+
+      if (dest)
+        temp->dest = dest;
+      else if (job)
+        temp->dest = dest = cupsdFindPrinter(job->dest);
 
      /*
       * Add common event notification attributes...
@@ -154,7 +160,7 @@ cupsdAddEvent(
                    "notify-charset", NULL, "utf-8");
 
       ippAddString(temp->attrs, IPP_TAG_EVENT_NOTIFICATION, IPP_TAG_LANGUAGE,
-                   "notify-natural-langugage", NULL, "en-US");
+                   "notify-natural-language", NULL, "en-US");
 
       ippAddInteger(temp->attrs, IPP_TAG_EVENT_NOTIFICATION, IPP_TAG_INTEGER,
                    "notify-subscription-id", sub->id);
@@ -262,7 +268,7 @@ cupsdAddEvent(
                           "job-stopped");
               break;
 
-         case IPP_JOB_CANCELLED :
+         case IPP_JOB_CANCELED :
               ippAddString(temp->attrs, IPP_TAG_EVENT_NOTIFICATION,
                           IPP_TAG_KEYWORD, "job-state-reasons", NULL,
                           "job-canceled-by-user");
@@ -284,18 +290,18 @@ cupsdAddEvent(
        ippAddInteger(temp->attrs, IPP_TAG_EVENT_NOTIFICATION, IPP_TAG_INTEGER,
                      "job-impressions-completed",
                      job->sheets ? job->sheets->values[0].integer : 0);
+      }
 
-       /*
-       * Send the notification for this subscription...
-       */
+     /*
+      * Send the notification for this subscription...
+      */
 
-       cupsd_send_notification(sub, temp);
-      }
+      cupsd_send_notification(sub, temp);
     }
   }
 
   if (temp)
-    cupsdSaveAllSubscriptions();
+    cupsdMarkDirty(CUPSD_DIRTY_SUBSCRIPTIONS);
   else
     cupsdLogMessage(CUPSD_LOG_DEBUG, "Discarding unused %s event...",
                     cupsdEventName(event));
@@ -321,7 +327,7 @@ cupsdAddSubscription(
                   "cupsdAddSubscription(mask=%x, dest=%p(%s), job=%p(%d), "
                  "uri=\"%s\")",
                   mask, dest, dest ? dest->name : "", job, job ? job->id : 0,
-                 uri);
+                 uri ? uri : "(null)");
 
   if (!Subscriptions)
     Subscriptions = cupsArrayNew((cups_array_func_t)cupsd_compare_subscriptions,
@@ -339,8 +345,56 @@ cupsdAddSubscription(
   * Limit the number of subscriptions...
   */
 
-  if (cupsArrayCount(Subscriptions) >= MaxSubscriptions)
+  if (MaxSubscriptions > 0 && cupsArrayCount(Subscriptions) >= MaxSubscriptions)
+  {
+    cupsdLogMessage(CUPSD_LOG_DEBUG,
+                    "cupsdAddSubscription: Reached MaxSubscriptions %d "
+                   "(count=%d)", MaxSubscriptions,
+                   cupsArrayCount(Subscriptions));
     return (NULL);
+  }
+
+  if (MaxSubscriptionsPerJob > 0 && job)
+  {
+    int        count;                          /* Number of job subscriptions */
+
+    for (temp = (cupsd_subscription_t *)cupsArrayFirst(Subscriptions),
+             count = 0;
+         temp;
+        temp = (cupsd_subscription_t *)cupsArrayNext(Subscriptions))
+      if (temp->job == job)
+        count ++;
+
+    if (count >= MaxSubscriptionsPerJob)
+    {
+      cupsdLogMessage(CUPSD_LOG_DEBUG,
+                     "cupsdAddSubscription: Reached MaxSubscriptionsPerJob %d "
+                     "for job #%d (count=%d)", MaxSubscriptionsPerJob,
+                     job->id, count);
+      return (NULL);
+    }
+  }
+
+  if (MaxSubscriptionsPerPrinter > 0 && dest)
+  {
+    int        count;                          /* Number of printer subscriptions */
+
+    for (temp = (cupsd_subscription_t *)cupsArrayFirst(Subscriptions),
+             count = 0;
+         temp;
+        temp = (cupsd_subscription_t *)cupsArrayNext(Subscriptions))
+      if (temp->dest == dest)
+        count ++;
+
+    if (count >= MaxSubscriptionsPerPrinter)
+    {
+      cupsdLogMessage(CUPSD_LOG_DEBUG,
+                     "cupsdAddSubscription: Reached "
+                     "MaxSubscriptionsPerPrinter %d for %s (count=%d)",
+                     MaxSubscriptionsPerPrinter, dest->name, count);
+      return (NULL);
+    }
+  }
 
  /*
   * Allocate memory for this subscription...
@@ -387,6 +441,13 @@ cupsdAddSubscription(
 
   cupsArrayAdd(Subscriptions, temp);
 
+ /*
+  * For RSS subscriptions, run the notifier immediately...
+  */
+
+  if (uri && !strncmp(uri, "rss:", 4))
+    cupsd_start_notifier(temp);
+
   return (temp);
 }
 
@@ -423,9 +484,6 @@ cupsdDeleteSubscription(
     cupsd_subscription_t *sub,         /* I - Subscription object */
     int                  update)       /* I - 1 = update subscriptions.conf */
 {
-  int  i;                              /* Looping var */
-
-
  /*
   * Close the pipe to the notifier as needed...
   */
@@ -446,13 +504,7 @@ cupsdDeleteSubscription(
   cupsdClearString(&(sub->owner));
   cupsdClearString(&(sub->recipient));
 
-  if (sub->events)
-  {
-    for (i = 0; i < sub->num_events; i ++)
-      cupsd_delete_event(sub->events[i]);
-
-    free(sub->events);
-  }
+  cupsArrayDelete(sub->events);
 
   free(sub);
 
@@ -461,7 +513,7 @@ cupsdDeleteSubscription(
   */
 
   if (update)
-    cupsdSaveAllSubscriptions();
+    cupsdMarkDirty(CUPSD_DIRTY_SUBSCRIPTIONS);
 }
 
 
@@ -502,9 +554,14 @@ cupsdEventName(
     case CUPSD_EVENT_PRINTER_MODIFIED :
         return ("printer-modified");
 
+    case CUPSD_EVENT_PRINTER_QUEUE_ORDER_CHANGED :
+        return ("printer-queue-order-changed");
+
+    case CUPSD_EVENT_PRINTER_STATE :
     case CUPSD_EVENT_PRINTER_STATE_CHANGED :
         return ("printer-state-changed");
 
+    case CUPSD_EVENT_PRINTER_CONFIG :
     case CUPSD_EVENT_PRINTER_CONFIG_CHANGED :
         return ("printer-config-changed");
 
@@ -527,8 +584,6 @@ cupsdEventName(
         return ("job-progress");
 
     case CUPSD_EVENT_JOB_STATE :
-        return ("job-state");
-
     case CUPSD_EVENT_JOB_STATE_CHANGED :
         return ("job-state-changed");
 
@@ -575,14 +630,14 @@ cupsdEventValue(const char *name) /* I - Name of event */
     return (CUPSD_EVENT_PRINTER_DELETED);
   else if (!strcmp(name, "printer-modified"))
     return (CUPSD_EVENT_PRINTER_MODIFIED);
+  else if (!strcmp(name, "printer-queue-order-changed"))
+    return (CUPSD_EVENT_PRINTER_QUEUE_ORDER_CHANGED);
   else if (!strcmp(name, "printer-state-changed"))
     return (CUPSD_EVENT_PRINTER_STATE_CHANGED);
   else if (!strcmp(name, "printer-config-changed"))
     return (CUPSD_EVENT_PRINTER_CONFIG_CHANGED);
   else if (!strcmp(name, "printer-changed"))
     return (CUPSD_EVENT_PRINTER_CHANGED);
-  else if (!strcmp(name, "job-state"))
-    return (CUPSD_EVENT_JOB_STATE);
   else if (!strcmp(name, "job-created"))
     return (CUPSD_EVENT_JOB_CREATED);
   else if (!strcmp(name, "job-completed"))
@@ -641,7 +696,7 @@ cupsdExpireSubscriptions(
     }
 
   if (update)
-    cupsdSaveAllSubscriptions();
+    cupsdMarkDirty(CUPSD_DIRTY_SUBSCRIPTIONS);
 }
 
 
@@ -684,14 +739,8 @@ cupsdLoadAllSubscriptions(void)
   */
 
   snprintf(line, sizeof(line), "%s/subscriptions.conf", ServerRoot);
-  if ((fp = cupsFileOpen(line, "r")) == NULL)
-  {
-    if (errno != ENOENT)
-      cupsdLogMessage(CUPSD_LOG_ERROR,
-                     "LoadAllSubscriptions: Unable to open %s - %s", line,
-                     strerror(errno));
+  if ((fp = cupsdOpenConfFile(line)) == NULL)
     return;
-  }
 
  /*
   * Read all of the lines from the file...
@@ -703,7 +752,7 @@ cupsdLoadAllSubscriptions(void)
 
   while (cupsFileGetConf(fp, line, sizeof(line), &value, &linenum))
   {
-    if (!strcasecmp(line, "NextSubscriptionId") && value)
+    if (!_cups_strcasecmp(line, "NextSubscriptionId") && value)
     {
      /*
       * NextSubscriptionId NNN
@@ -713,7 +762,7 @@ cupsdLoadAllSubscriptions(void)
       if (i >= NextSubscriptionId && i > 0)
         NextSubscriptionId = i;
     }
-    else if (!strcasecmp(line, "<Subscription"))
+    else if (!_cups_strcasecmp(line, "<Subscription"))
     {
      /*
       * <Subscription #>
@@ -729,17 +778,17 @@ cupsdLoadAllSubscriptions(void)
         cupsdLogMessage(CUPSD_LOG_ERROR,
                        "Syntax error on line %d of subscriptions.conf.",
                        linenum);
-        return;
+        break;
       }
     }
-    else if (!strcasecmp(line, "</Subscription>"))
+    else if (!_cups_strcasecmp(line, "</Subscription>"))
     {
       if (!sub)
       {
         cupsdLogMessage(CUPSD_LOG_ERROR,
                        "Syntax error on line %d of subscriptions.conf.",
                        linenum);
-        return;
+        break;
       }
 
       if (delete_sub)
@@ -753,9 +802,8 @@ cupsdLoadAllSubscriptions(void)
       cupsdLogMessage(CUPSD_LOG_ERROR,
                       "Syntax error on line %d of subscriptions.conf.",
                      linenum);
-      return;
     }
-    else if (!strcasecmp(line, "Events"))
+    else if (!_cups_strcasecmp(line, "Events"))
     {
      /*
       * Events name
@@ -767,7 +815,7 @@ cupsdLoadAllSubscriptions(void)
        cupsdLogMessage(CUPSD_LOG_ERROR,
                        "Syntax error on line %d of subscriptions.conf.",
                        linenum);
-       return;
+       break;
       }
 
       while (*value)
@@ -790,13 +838,13 @@ cupsdLoadAllSubscriptions(void)
          cupsdLogMessage(CUPSD_LOG_ERROR,
                          "Unknown event name \'%s\' on line %d of subscriptions.conf.",
                          value, linenum);
-         return;
+         break;
        }
 
        value = valueptr;
       }
     }
-    else if (!strcasecmp(line, "Owner"))
+    else if (!_cups_strcasecmp(line, "Owner"))
     {
      /*
       * Owner
@@ -809,10 +857,10 @@ cupsdLoadAllSubscriptions(void)
        cupsdLogMessage(CUPSD_LOG_ERROR,
                        "Syntax error on line %d of subscriptions.conf.",
                        linenum);
-       return;
+       break;
       }
     }
-    else if (!strcasecmp(line, "Recipient"))
+    else if (!_cups_strcasecmp(line, "Recipient"))
     {
      /*
       * Recipient uri
@@ -825,10 +873,10 @@ cupsdLoadAllSubscriptions(void)
        cupsdLogMessage(CUPSD_LOG_ERROR,
                        "Syntax error on line %d of subscriptions.conf.",
                        linenum);
-       return;
+       break;
       }
     }
-    else if (!strcasecmp(line, "JobId"))
+    else if (!_cups_strcasecmp(line, "JobId"))
     {
      /*
       * JobId #
@@ -849,10 +897,10 @@ cupsdLoadAllSubscriptions(void)
        cupsdLogMessage(CUPSD_LOG_ERROR,
                        "Syntax error on line %d of subscriptions.conf.",
                        linenum);
-       return;
+       break;
       }
     }
-    else if (!strcasecmp(line, "PrinterName"))
+    else if (!_cups_strcasecmp(line, "PrinterName"))
     {
      /*
       * PrinterName name
@@ -873,10 +921,10 @@ cupsdLoadAllSubscriptions(void)
        cupsdLogMessage(CUPSD_LOG_ERROR,
                        "Syntax error on line %d of subscriptions.conf.",
                        linenum);
-       return;
+       break;
       }
     }
-    else if (!strcasecmp(line, "UserData"))
+    else if (!_cups_strcasecmp(line, "UserData"))
     {
      /*
       * UserData encoded-string
@@ -935,10 +983,10 @@ cupsdLoadAllSubscriptions(void)
        cupsdLogMessage(CUPSD_LOG_ERROR,
                        "Syntax error on line %d of subscriptions.conf.",
                        linenum);
-       return;
+       break;
       }
     }
-    else if (!strcasecmp(line, "LeaseDuration"))
+    else if (!_cups_strcasecmp(line, "LeaseDuration"))
     {
      /*
       * LeaseDuration #
@@ -954,10 +1002,10 @@ cupsdLoadAllSubscriptions(void)
        cupsdLogMessage(CUPSD_LOG_ERROR,
                        "Syntax error on line %d of subscriptions.conf.",
                        linenum);
-       return;
+       break;
       }
     }
-    else if (!strcasecmp(line, "Interval"))
+    else if (!_cups_strcasecmp(line, "Interval"))
     {
      /*
       * Interval #
@@ -970,10 +1018,10 @@ cupsdLoadAllSubscriptions(void)
        cupsdLogMessage(CUPSD_LOG_ERROR,
                        "Syntax error on line %d of subscriptions.conf.",
                        linenum);
-       return;
+       break;
       }
     }
-    else if (!strcasecmp(line, "ExpirationTime"))
+    else if (!_cups_strcasecmp(line, "ExpirationTime"))
     {
      /*
       * ExpirationTime #
@@ -986,10 +1034,10 @@ cupsdLoadAllSubscriptions(void)
        cupsdLogMessage(CUPSD_LOG_ERROR,
                        "Syntax error on line %d of subscriptions.conf.",
                        linenum);
-       return;
+       break;
       }
     }
-    else if (!strcasecmp(line, "NextEventId"))
+    else if (!_cups_strcasecmp(line, "NextEventId"))
     {
      /*
       * NextEventId #
@@ -1002,7 +1050,7 @@ cupsdLoadAllSubscriptions(void)
        cupsdLogMessage(CUPSD_LOG_ERROR,
                        "Syntax error on line %d of subscriptions.conf.",
                        linenum);
-       return;
+       break;
       }
     }
     else
@@ -1030,8 +1078,8 @@ cupsdSaveAllSubscriptions(void)
 {
   int                  i;              /* Looping var */
   cups_file_t          *fp;            /* subscriptions.conf file */
-  char                 temp[1024];     /* Temporary string */
-  char                 backup[1024];   /* subscriptions.conf.O file */
+  char                 filename[1024], /* subscriptions.conf filename */
+                       temp[1024];     /* Temporary string */
   cupsd_subscription_t *sub;           /* Current subscription */
   time_t               curtime;        /* Current time */
   struct tm            *curdate;       /* Current date */
@@ -1044,36 +1092,12 @@ cupsdSaveAllSubscriptions(void)
   * Create the subscriptions.conf file...
   */
 
-  snprintf(temp, sizeof(temp), "%s/subscriptions.conf", ServerRoot);
-  snprintf(backup, sizeof(backup), "%s/subscriptions.conf.O", ServerRoot);
-
-  if (rename(temp, backup))
-  {
-    if (errno != ENOENT)
-      cupsdLogMessage(CUPSD_LOG_ERROR, "Unable to backup subscriptions.conf - %s",
-                      strerror(errno));
-  }
-
-  if ((fp = cupsFileOpen(temp, "w")) == NULL)
-  {
-    cupsdLogMessage(CUPSD_LOG_ERROR, "Unable to save subscriptions.conf - %s",
-                    strerror(errno));
+  snprintf(filename, sizeof(filename), "%s/subscriptions.conf", ServerRoot);
 
-    if (rename(backup, temp))
-      cupsdLogMessage(CUPSD_LOG_ERROR,
-                      "Unable to restore subscriptions.conf - %s",
-                      strerror(errno));
+  if ((fp = cupsdCreateConfFile(filename, ConfigFilePerm)) == NULL)
     return;
-  }
-  else
-    cupsdLogMessage(CUPSD_LOG_INFO, "Saving subscriptions.conf...");
 
- /*
-  * Restrict access to the file...
-  */
-
-  fchown(cupsFileNumber(fp), getuid(), Group);
-  fchmod(cupsFileNumber(fp), ConfigFilePerm);
+  cupsdLogMessage(CUPSD_LOG_INFO, "Saving subscriptions.conf...");
 
  /*
   * Write a small header to the file...
@@ -1174,7 +1198,7 @@ cupsdSaveAllSubscriptions(void)
     cupsFilePuts(fp, "</Subscription>\n");
   }
 
-  cupsFileClose(fp);
+  cupsdCloseCreatedConfFile(fp, filename);
 }
 
 
@@ -1216,10 +1240,7 @@ cupsdStopAllNotifiers(void)
 
   if (NotifierPipes[0] >= 0)
   {
-    cupsdLogMessage(CUPSD_LOG_DEBUG2,
-                    "cupsdStopAllNotifiers: Removing fd %d from InputSet...",
-                   NotifierPipes[0]);
-    FD_CLR(NotifierPipes[0], InputSet);
+    cupsdRemoveSelect(NotifierPipes[0]);
 
     cupsdStatBufDelete(NotifierStatusBuffer);
 
@@ -1233,25 +1254,6 @@ cupsdStopAllNotifiers(void)
 }
 
 
-/*
- * 'cupsdUpdateNotifierStatus()' - Read messages from notifiers.
- */
-
-void
-cupsdUpdateNotifierStatus(void)
-{
-  char         *ptr,                   /* Pointer to end of line in buffer */
-               message[1024];          /* Pointer to message text */
-  int          loglevel;               /* Log level for message */
-
-
-  while ((ptr = cupsdStatBufUpdate(NotifierStatusBuffer, &loglevel,
-                                   message, sizeof(message))) != NULL)
-    if (!strchr(NotifierStatusBuffer->buffer, '\n'))
-      break;
-}
-
-
 /*
  * 'cupsd_compare_subscriptions()' - Compare two subscriptions.
  */
@@ -1351,13 +1353,13 @@ cupsd_send_dbus(cupsd_eventmask_t event,/* I - Event to send */
   message = dbus_message_new_signal("/com/redhat/PrinterSpooler",
                                    "com.redhat.PrinterSpooler", what);
 
-  dbus_message_iter_init_append(message, &iter);
+  dbus_message_append_iter_init(message, &iter);
   if (dest)
-    dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &(dest->name));
+    dbus_message_iter_append_string(&iter, dest->name);
   if (job)
   {
-    dbus_message_iter_append_basic(&iter, DBUS_TYPE_UINT32, &(job->id));
-    dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &(job->username));
+    dbus_message_iter_append_uint32(&iter, job->id);
+    dbus_message_iter_append_string(&iter, job->username);
   }
 
   dbus_connection_send(con, message, NULL);
@@ -1379,8 +1381,8 @@ cupsd_send_notification(
   ipp_state_t  state;                  /* IPP event state */
 
 
-  cupsdLogMessage(CUPSD_LOG_DEBUG,
-                  "cupsd_send_notification(sub=%p(%d), event=%p(%s))\n",
+  cupsdLogMessage(CUPSD_LOG_DEBUG2,
+                  "cupsd_send_notification(sub=%p(%d), event=%p(%s))",
                   sub, sub->id, event, cupsdEventName(event->event));
 
  /*
@@ -1389,7 +1391,10 @@ cupsd_send_notification(
 
   if (!sub->events)
   {
-    sub->events = calloc(MaxEvents, sizeof(cupsd_event_t *));
+    sub->events = cupsArrayNew3((cups_array_func_t)NULL, NULL,
+                                (cups_ahash_func_t)NULL, 0,
+                               (cups_acopy_func_t)NULL,
+                               (cups_afree_func_t)cupsd_delete_event);
 
     if (!sub->events)
     {
@@ -1404,19 +1409,15 @@ cupsd_send_notification(
   * Purge an old event as needed...
   */
 
-  if (sub->num_events >= MaxEvents)
+  if (cupsArrayCount(sub->events) >= MaxEvents)
   {
    /*
     * Purge the oldest event in the cache...
     */
 
-    cupsd_delete_event(sub->events[0]);
+    cupsArrayRemove(sub->events, cupsArrayFirst(sub->events));
 
-    sub->num_events --;
     sub->first_event_id ++;
-
-    memmove(sub->events, sub->events + 1,
-           sub->num_events * sizeof(cupsd_event_t *));
   }
 
  /*
@@ -1426,8 +1427,7 @@ cupsd_send_notification(
   * event cache limit, we don't need to check for overflow here...
   */
 
-  sub->events[sub->num_events] = event;
-  sub->num_events ++;
+  cupsArrayAdd(sub->events, event);
 
  /*
   * Deliver the event...
@@ -1435,13 +1435,16 @@ cupsd_send_notification(
 
   if (sub->recipient)
   {
-    if (sub->pipe < 0)
-      cupsd_start_notifier(sub);
+    for (;;)
+    {
+      if (sub->pipe < 0)
+       cupsd_start_notifier(sub);
 
-    cupsdLogMessage(CUPSD_LOG_DEBUG, "sub->pipe=%d", sub->pipe);
+      cupsdLogMessage(CUPSD_LOG_DEBUG2, "sub->pipe=%d", sub->pipe);
+
+      if (sub->pipe < 0)
+       break;
 
-    if (sub->pipe >= 0)
-    {
       event->attrs->state = IPP_IDLE;
 
       while ((state = ippWriteFile(sub->pipe, event->attrs)) != IPP_DATA)
@@ -1449,9 +1452,34 @@ cupsd_send_notification(
          break;
 
       if (state == IPP_ERROR)
+      {
+        if (errno == EPIPE)
+       {
+        /*
+         * Notifier died, try restarting it...
+         */
+
+          cupsdLogMessage(CUPSD_LOG_WARN,
+                         "Notifier for subscription %d (%s) went away, "
+                         "retrying!",
+                         sub->id, sub->recipient);
+         cupsdEndProcess(sub->pid, 0);
+
+         close(sub->pipe);
+         sub->pipe = -1;
+          continue;
+       }
+
         cupsdLogMessage(CUPSD_LOG_ERROR,
                        "Unable to send event for subscription %d (%s)!",
                        sub->id, sub->recipient);
+      }
+
+     /*
+      * If we get this far, break out of the loop...
+      */
+
+      break;
     }
   }
 
@@ -1473,7 +1501,6 @@ cupsd_start_notifier(
 {
   int  pid;                            /* Notifier process ID */
   int  fds[2];                         /* Pipe file descriptors */
-  int  envc;                           /* Number of environment variables */
   char *argv[4],                       /* Command-line arguments */
        *envp[MAX_ENV],                 /* Environment variables */
        user_data[128],                 /* Base-64 encoded user data */
@@ -1513,7 +1540,7 @@ cupsd_start_notifier(
   * Setup the environment...
   */
 
-  envc = cupsdLoadEnv(envp, (int)(sizeof(envp) / sizeof(envp[0])));
+  cupsdLoadEnv(envp, (int)(sizeof(envp) / sizeof(envp[0])));
 
  /*
   * Create pipes as needed...
@@ -1535,11 +1562,8 @@ cupsd_start_notifier(
 
     NotifierStatusBuffer = cupsdStatBufNew(NotifierPipes[0], "[Notifier]");
 
-    cupsdLogMessage(CUPSD_LOG_DEBUG2,
-                    "start_notifier: Adding fd %d to InputSet...",
-                   NotifierPipes[0]);
-
-    FD_SET(NotifierPipes[0], InputSet);
+    cupsdAddSelect(NotifierPipes[0], (cupsd_selfunc_t)cupsd_update_notifier,
+                   NULL, NULL);
   }
 
   if (cupsdOpenPipe(fds))
@@ -1561,7 +1585,7 @@ cupsd_start_notifier(
   */
 
   if (cupsdStartProcess(command, argv, envp, fds[0], -1, NotifierPipes[1],
-                       -1, 0, &pid) < 0)
+                       -1, -1, 0, DefaultProfile, NULL, &pid) < 0)
   {
    /*
     * Error - can't fork!
@@ -1590,6 +1614,29 @@ cupsd_start_notifier(
 }
 
 
+/*
+ * 'cupsd_update_notifier()' - Read messages from notifiers.
+ */
+
+void
+cupsd_update_notifier(void)
+{
+  char         message[1024];          /* Pointer to message text */
+  int          loglevel;               /* Log level for message */
+
+
+  while (cupsdStatBufUpdate(NotifierStatusBuffer, &loglevel,
+                            message, sizeof(message)))
+  {
+    if (loglevel == CUPSD_LOG_INFO)
+      cupsdLogMessage(CUPSD_LOG_INFO, "%s", message);
+
+    if (!strchr(NotifierStatusBuffer->buffer, '\n'))
+      break;
+  }
+}
+
+
 /*
  * End of "$Id$".
  */