]> git.ipfire.org Git - thirdparty/cups.git/blobdiff - scheduler/subscriptions.c
Load cups into easysw/current.
[thirdparty/cups.git] / scheduler / subscriptions.c
index d77b33f78c8d5b57b35a798dd29680e0a3bfee66..f3a320594776364db53d70ad64be4dba41193300 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * "$Id: subscriptions.c 5046 2006-02-01 22:11:58Z mike $"
+ * "$Id: subscriptions.c 5878 2006-08-24 15:55:42Z mike $"
  *
  *   Subscription routines for the Common UNIX Printing System (CUPS) scheduler.
  *
@@ -25,7 +25,6 @@
  *
  *   cupsdAddEvent()               - Add an event to the global event cache.
  *   cupsdAddSubscription()        - Add a new subscription object.
- *   cupsdDeleteAllEvents()        - Delete all cached events.
  *   cupsdDeleteAllSubscriptions() - Delete all subscriptions.
  *   cupsdDeleteSubscription()     - Delete a subscription object.
  *   cupsdEventName()              - Return a single event name.
  *   cupsdFindSubscription()       - Find a subscription by ID.
  *   cupsdLoadAllSubscriptions()   - Load all subscriptions from the .conf file.
  *   cupsdSaveAllSubscriptions()   - Save all subscriptions to the .conf file.
- *   cupsdSendNotification()       - Send a notification for the specified event.
  *   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...
  */
 
@@ -65,6 +65,8 @@ static void   cupsd_delete_event(cupsd_event_t *event);
 static void    cupsd_send_dbus(cupsd_eventmask_t event, cupsd_printer_t *dest,
                                cupsd_job_t *job);
 #endif /* HAVE_DBUS */
+static void    cupsd_send_notification(cupsd_subscription_t *sub,
+                                       cupsd_event_t *event);
 static void    cupsd_start_notifier(cupsd_subscription_t *sub);
 
 
@@ -87,6 +89,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...
   */
@@ -109,30 +116,12 @@ cupsdAddEvent(
     return;
   }
 
- /*
-  * Allocate memory for the event cache as needed...
-  */
-
-  if (!Events)
-  {
-    Events    = calloc(MaxEvents, sizeof(cupsd_event_t *));
-    NumEvents = 0;
-
-    if (!Events)
-    {
-      cupsdLogMessage(CUPSD_LOG_CRIT,
-                      "Unable to allocate memory for event cache - %s",
-                     strerror(errno));
-      return;
-    }
-  }
-
  /*
   * Then loop through the subscriptions and add the event to the corresponding
   * caches...
   */
 
-  for (sub = (cupsd_subscription_t *)cupsArrayFirst(Subscriptions), temp = NULL;
+  for (temp = NULL, sub = (cupsd_subscription_t *)cupsArrayFirst(Subscriptions);
        sub;
        sub = (cupsd_subscription_t *)cupsArrayNext(Subscriptions))
   {
@@ -145,185 +134,168 @@ cupsdAddEvent(
        (sub->job == job || !sub->job))
     {
      /*
-      * Need this event...
+      * Need this event, so create a new event record...
       */
 
-      if (!temp)
+      if ((temp = (cupsd_event_t *)calloc(1, sizeof(cupsd_event_t))) == NULL)
       {
-       /*
-       * Create the new event record...
-       */
+       cupsdLogMessage(CUPSD_LOG_CRIT,
+                       "Unable to allocate memory for event - %s",
+                       strerror(errno));
+       return;
+      }
 
-       if ((temp = (cupsd_event_t *)calloc(1, sizeof(cupsd_event_t))) == NULL)
-       {
-         cupsdLogMessage(CUPSD_LOG_CRIT,
-                         "Unable to allocate memory for event - %s",
-                         strerror(errno));
-         return;
-       }
+      temp->event = event;
+      temp->time  = time(NULL);
+      temp->attrs = ippNew();
+      temp->job   = job;
+      temp->dest  = dest;
 
-       temp->event = event;
-       temp->time  = time(NULL);
-       temp->attrs = ippNew();
-       temp->job   = job;
-       temp->dest  = dest;
+     /*
+      * Add common event notification attributes...
+      */
 
-       /*
-        * Add common event notification attributes...
-       */
+      ippAddString(temp->attrs, IPP_TAG_EVENT_NOTIFICATION, IPP_TAG_CHARSET,
+                   "notify-charset", NULL, "utf-8");
 
-        ippAddInteger(temp->attrs, IPP_TAG_EVENT_NOTIFICATION, IPP_TAG_INTEGER,
-                     "notify-subscription-id", sub->id);
+      ippAddString(temp->attrs, IPP_TAG_EVENT_NOTIFICATION, IPP_TAG_LANGUAGE,
+                   "notify-natural-langugage", NULL, "en-US");
 
-       ippAddString(temp->attrs, IPP_TAG_EVENT_NOTIFICATION, IPP_TAG_KEYWORD,
-                    "notify-subscribed-event", NULL, cupsdEventName(event));
+      ippAddInteger(temp->attrs, IPP_TAG_EVENT_NOTIFICATION, IPP_TAG_INTEGER,
+                   "notify-subscription-id", sub->id);
 
-        ippAddInteger(temp->attrs, IPP_TAG_EVENT_NOTIFICATION, IPP_TAG_INTEGER,
-                     "printer-up-time", time(NULL));
+      ippAddInteger(temp->attrs, IPP_TAG_EVENT_NOTIFICATION, IPP_TAG_INTEGER,
+                   "notify-sequence-number", sub->next_event_id);
 
-        va_start(ap, text);
-       vsnprintf(ftext, sizeof(ftext), text, ap);
-       va_end(ap);
+      ippAddString(temp->attrs, IPP_TAG_EVENT_NOTIFICATION, IPP_TAG_KEYWORD,
+                  "notify-subscribed-event", NULL, cupsdEventName(event));
 
-       ippAddString(temp->attrs, IPP_TAG_EVENT_NOTIFICATION, IPP_TAG_TEXT,
-                    "notify-text", NULL, ftext);
+      if (sub->user_data_len > 0)
+        ippAddOctetString(temp->attrs, IPP_TAG_EVENT_NOTIFICATION,
+                         "notify-user-data", sub->user_data,
+                         sub->user_data_len);
 
-        if (dest)
-       {
-        /*
-         * Add printer attributes...
-         */
+      ippAddInteger(temp->attrs, IPP_TAG_EVENT_NOTIFICATION, IPP_TAG_INTEGER,
+                   "printer-up-time", time(NULL));
 
-         ippAddString(temp->attrs, IPP_TAG_EVENT_NOTIFICATION, IPP_TAG_URI,
-                      "notify-printer-uri", NULL, dest->uri);
+      va_start(ap, text);
+      vsnprintf(ftext, sizeof(ftext), text, ap);
+      va_end(ap);
 
-         ippAddString(temp->attrs, IPP_TAG_EVENT_NOTIFICATION, IPP_TAG_NAME,
-                      "printer-name", NULL, dest->name);
+      ippAddString(temp->attrs, IPP_TAG_EVENT_NOTIFICATION, IPP_TAG_TEXT,
+                  "notify-text", NULL, ftext);
 
-         ippAddInteger(temp->attrs, IPP_TAG_EVENT_NOTIFICATION, IPP_TAG_ENUM,
-                       "printer-state", dest->state);
+      if (dest)
+      {
+       /*
+       * Add printer attributes...
+       */
 
-         if (dest->num_reasons == 0)
-           ippAddString(temp->attrs, IPP_TAG_EVENT_NOTIFICATION,
-                        IPP_TAG_KEYWORD, "printer-state-reasons", NULL,
-                        dest->state == IPP_PRINTER_STOPPED ? "paused" : "none");
-         else
-           ippAddStrings(temp->attrs, IPP_TAG_EVENT_NOTIFICATION,
-                         IPP_TAG_KEYWORD, "printer-state-reasons",
-                         dest->num_reasons, NULL,
-                         (const char * const *)dest->reasons);
+       ippAddString(temp->attrs, IPP_TAG_EVENT_NOTIFICATION, IPP_TAG_URI,
+                    "notify-printer-uri", NULL, dest->uri);
 
-         ippAddBoolean(temp->attrs, IPP_TAG_EVENT_NOTIFICATION,
-                       "printer-is-accepting-jobs", dest->accepting);
-        }
+       ippAddString(temp->attrs, IPP_TAG_EVENT_NOTIFICATION, IPP_TAG_NAME,
+                    "printer-name", NULL, dest->name);
 
-        if (job)
-       {
-        /*
-         * Add job attributes...
-         */
+       ippAddInteger(temp->attrs, IPP_TAG_EVENT_NOTIFICATION, IPP_TAG_ENUM,
+                     "printer-state", dest->state);
+
+       if (dest->num_reasons == 0)
+         ippAddString(temp->attrs, IPP_TAG_EVENT_NOTIFICATION,
+                      IPP_TAG_KEYWORD, "printer-state-reasons", NULL,
+                      dest->state == IPP_PRINTER_STOPPED ? "paused" : "none");
+       else
+         ippAddStrings(temp->attrs, IPP_TAG_EVENT_NOTIFICATION,
+                       IPP_TAG_KEYWORD, "printer-state-reasons",
+                       dest->num_reasons, NULL,
+                       (const char * const *)dest->reasons);
 
-         ippAddInteger(temp->attrs, IPP_TAG_EVENT_NOTIFICATION, IPP_TAG_INTEGER,
-                       "notify-job-id", job->id);
-         ippAddInteger(temp->attrs, IPP_TAG_EVENT_NOTIFICATION, IPP_TAG_ENUM,
-                       "job-state", (int)job->state);
+       ippAddBoolean(temp->attrs, IPP_TAG_EVENT_NOTIFICATION,
+                     "printer-is-accepting-jobs", dest->accepting);
+      }
 
-          if ((attr = ippFindAttribute(job->attrs, "job-name",
-                                      IPP_TAG_NAME)) != NULL)
-           ippAddString(temp->attrs, IPP_TAG_EVENT_NOTIFICATION, IPP_TAG_NAME,
-                        "job-name", NULL, attr->values[0].string.text);
+      if (job)
+      {
+       /*
+       * Add job attributes...
+       */
 
-         switch (job->state->values[0].integer)
-         {
-           case IPP_JOB_PENDING :
-               if (dest && dest->state == IPP_PRINTER_STOPPED)
-                 ippAddString(temp->attrs, IPP_TAG_EVENT_NOTIFICATION,
-                              IPP_TAG_KEYWORD, "job-state-reasons", NULL,
-                              "printer-stopped");
-               else
-                 ippAddString(temp->attrs, IPP_TAG_EVENT_NOTIFICATION,
-                              IPP_TAG_KEYWORD, "job-state-reasons", NULL,
-                              "none");
-               break;
-
-           case IPP_JOB_HELD :
-               if (ippFindAttribute(job->attrs, "job-hold-until", IPP_TAG_KEYWORD) != NULL ||
-                   ippFindAttribute(job->attrs, "job-hold-until", IPP_TAG_NAME) != NULL)
-                 ippAddString(temp->attrs, IPP_TAG_EVENT_NOTIFICATION,
-                              IPP_TAG_KEYWORD, "job-state-reasons", NULL,
-                              "job-hold-until-specified");
-               else
-                 ippAddString(temp->attrs, IPP_TAG_EVENT_NOTIFICATION,
-                              IPP_TAG_KEYWORD, "job-state-reasons", NULL,
-                              "job-incoming");
-               break;
-
-           case IPP_JOB_PROCESSING :
-               ippAddString(temp->attrs, IPP_TAG_EVENT_NOTIFICATION,
-                            IPP_TAG_KEYWORD, "job-state-reasons", NULL,
-                            "job-printing");
-               break;
+       ippAddInteger(temp->attrs, IPP_TAG_EVENT_NOTIFICATION, IPP_TAG_INTEGER,
+                     "notify-job-id", job->id);
+       ippAddInteger(temp->attrs, IPP_TAG_EVENT_NOTIFICATION, IPP_TAG_ENUM,
+                     "job-state", job->state_value);
+
+        if ((attr = ippFindAttribute(job->attrs, "job-name",
+                                    IPP_TAG_NAME)) != NULL)
+         ippAddString(temp->attrs, IPP_TAG_EVENT_NOTIFICATION, IPP_TAG_NAME,
+                      "job-name", NULL, attr->values[0].string.text);
 
-           case IPP_JOB_STOPPED :
+       switch (job->state_value)
+       {
+         case IPP_JOB_PENDING :
+              if (dest && dest->state == IPP_PRINTER_STOPPED)
                ippAddString(temp->attrs, IPP_TAG_EVENT_NOTIFICATION,
                             IPP_TAG_KEYWORD, "job-state-reasons", NULL,
-                            "job-stopped");
-               break;
-
-           case IPP_JOB_CANCELLED :
+                            "printer-stopped");
+              else
                ippAddString(temp->attrs, IPP_TAG_EVENT_NOTIFICATION,
                             IPP_TAG_KEYWORD, "job-state-reasons", NULL,
-                            "job-canceled-by-user");
-               break;
+                            "none");
+              break;
 
-           case IPP_JOB_ABORTED :
+         case IPP_JOB_HELD :
+              if (ippFindAttribute(job->attrs, "job-hold-until", IPP_TAG_KEYWORD) != NULL ||
+                 ippFindAttribute(job->attrs, "job-hold-until", IPP_TAG_NAME) != NULL)
                ippAddString(temp->attrs, IPP_TAG_EVENT_NOTIFICATION,
                             IPP_TAG_KEYWORD, "job-state-reasons", NULL,
-                            "aborted-by-system");
-               break;
-
-           case IPP_JOB_COMPLETED :
+                            "job-hold-until-specified");
+              else
                ippAddString(temp->attrs, IPP_TAG_EVENT_NOTIFICATION,
                             IPP_TAG_KEYWORD, "job-state-reasons", NULL,
-                            "job-completed-successfully");
-               break;
-         }
-
-         ippAddInteger(temp->attrs, IPP_TAG_EVENT_NOTIFICATION, IPP_TAG_INTEGER,
-                       "job-impressions-completed",
-                       job->sheets ? job->sheets->values[0].integer : 0);
-       }
-
-       /*
-       * Purge an old event as needed...
-       */
-
-       if (NumEvents >= MaxEvents)
-       {
-        /*
-         * Purge the oldest event in the cache...
-         */
-
-         cupsd_delete_event(Events[0]);
-
-         NumEvents --;
-
-         memmove(Events, Events + 1, NumEvents * sizeof(cupsd_event_t *));
+                            "job-incoming");
+              break;
+
+         case IPP_JOB_PROCESSING :
+              ippAddString(temp->attrs, IPP_TAG_EVENT_NOTIFICATION,
+                          IPP_TAG_KEYWORD, "job-state-reasons", NULL,
+                          "job-printing");
+              break;
+
+         case IPP_JOB_STOPPED :
+              ippAddString(temp->attrs, IPP_TAG_EVENT_NOTIFICATION,
+                          IPP_TAG_KEYWORD, "job-state-reasons", NULL,
+                          "job-stopped");
+              break;
+
+         case IPP_JOB_CANCELED :
+              ippAddString(temp->attrs, IPP_TAG_EVENT_NOTIFICATION,
+                          IPP_TAG_KEYWORD, "job-state-reasons", NULL,
+                          "job-canceled-by-user");
+              break;
+
+         case IPP_JOB_ABORTED :
+              ippAddString(temp->attrs, IPP_TAG_EVENT_NOTIFICATION,
+                          IPP_TAG_KEYWORD, "job-state-reasons", NULL,
+                          "aborted-by-system");
+              break;
+
+         case IPP_JOB_COMPLETED :
+              ippAddString(temp->attrs, IPP_TAG_EVENT_NOTIFICATION,
+                          IPP_TAG_KEYWORD, "job-state-reasons", NULL,
+                          "job-completed-successfully");
+              break;
        }
 
-       /*
-       * Add the new event to the main cache...
-       */
-
-       Events[NumEvents] = temp;
-       NumEvents ++;
+       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...
       */
 
-      cupsdSendNotification(sub, temp);
+      cupsd_send_notification(sub, temp);
     }
   }
 
@@ -351,9 +323,10 @@ cupsdAddSubscription(
 
 
   cupsdLogMessage(CUPSD_LOG_DEBUG,
-                  "cupsdAddSubscription(mask=%x(%s), dest=%p(%s), job=%p(%d), uri=\"%s\")",
-                  mask, cupsdEventName(mask), dest, dest ? dest->name : "",
-                 job, job ? job->id : 0, uri);
+                  "cupsdAddSubscription(mask=%x, dest=%p(%s), job=%p(%d), "
+                 "uri=\"%s\")",
+                  mask, dest, dest ? dest->name : "", job, job ? job->id : 0,
+                 uri);
 
   if (!Subscriptions)
     Subscriptions = cupsArrayNew((cups_array_func_t)cupsd_compare_subscriptions,
@@ -423,27 +396,6 @@ cupsdAddSubscription(
 }
 
 
-/*
- * 'cupsdDeleteAllEvents()' - Delete all cached events.
- */
-
-void
-cupsdDeleteAllEvents(void)
-{
-  int  i;                              /* Looping var */
-
-
-  if (MaxEvents <= 0 || !Events)
-    return;
-
-  for (i = 0; i < NumEvents; i ++)
-    cupsd_delete_event(Events[i]);
-
-  free(Events);
-  Events = NULL;
-}
-
-
 /*
  * 'cupsdDeleteAllSubscriptions()' - Delete all subscriptions.
  */
@@ -476,6 +428,9 @@ 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...
   */
@@ -497,7 +452,12 @@ cupsdDeleteSubscription(
   cupsdClearString(&(sub->recipient));
 
   if (sub->events)
+  {
+    for (i = 0; i < sub->num_events; i ++)
+      cupsd_delete_event(sub->events[i]);
+
     free(sub->events);
+  }
 
   free(sub);
 
@@ -571,6 +531,9 @@ cupsdEventName(
     case CUPSD_EVENT_JOB_PROGRESS :
         return ("job-progress");
 
+    case CUPSD_EVENT_JOB_STATE :
+        return ("job-state");
+
     case CUPSD_EVENT_JOB_STATE_CHANGED :
         return ("job-state-changed");
 
@@ -623,6 +586,8 @@ cupsdEventValue(const char *name)   /* I - Name of event */
     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"))
@@ -668,10 +633,12 @@ cupsdExpireSubscriptions(
   for (sub = (cupsd_subscription_t *)cupsArrayFirst(Subscriptions);
        sub;
        sub = (cupsd_subscription_t *)cupsArrayNext(Subscriptions))
-    if ((sub->expire <= curtime && dest && sub->dest == dest) ||
+    if ((!sub->job && !dest && sub->expire && sub->expire <= curtime) ||
+        (dest && sub->dest == dest) ||
        (job && sub->job == job))
     {
-      cupsdLogMessage(CUPSD_LOG_INFO, "Subscription %d has expired...", sub->id);
+      cupsdLogMessage(CUPSD_LOG_INFO, "Subscription %d has expired...",
+                      sub->id);
 
       cupsdDeleteSubscription(sub, 0);
 
@@ -741,7 +708,17 @@ cupsdLoadAllSubscriptions(void)
 
   while (cupsFileGetConf(fp, line, sizeof(line), &value, &linenum))
   {
-    if (!strcasecmp(line, "<Subscription"))
+    if (!strcasecmp(line, "NextSubscriptionId") && value)
+    {
+     /*
+      * NextSubscriptionId NNN
+      */
+
+      i = atoi(value);
+      if (i >= NextSubscriptionId && i > 0)
+        NextSubscriptionId = i;
+    }
+    else if (!strcasecmp(line, "<Subscription"))
     {
      /*
       * <Subscription #>
@@ -973,7 +950,10 @@ cupsdLoadAllSubscriptions(void)
       */
 
       if (value && isdigit(*value & 255))
-        sub->lease = atoi(value);
+      {
+        sub->lease  = atoi(value);
+        sub->expire = sub->lease ? time(NULL) + sub->lease : 0;
+      }
       else
       {
        cupsdLogMessage(CUPSD_LOG_ERROR,
@@ -1111,6 +1091,8 @@ cupsdSaveAllSubscriptions(void)
   cupsFilePuts(fp, "# Subscription configuration file for " CUPS_SVERSION "\n");
   cupsFilePrintf(fp, "# Written by cupsd on %s\n", temp);
 
+  cupsFilePrintf(fp, "NextSubscriptionId %d\n", NextSubscriptionId);
+
  /*
   * Write every subscription known to the system...
   */
@@ -1201,83 +1183,6 @@ cupsdSaveAllSubscriptions(void)
 }
 
 
-/*
- * 'cupsdSendNotification()' - Send a notification for the specified event.
- */
-
-void
-cupsdSendNotification(
-    cupsd_subscription_t *sub,         /* I - Subscription object */
-    cupsd_event_t        *event)       /* I - Event to send */
-{
-  ipp_state_t  state;                  /* IPP event state */
-
-
-  cupsdLogMessage(CUPSD_LOG_DEBUG,
-                  "cupsdSendNotification(sub=%p(%d), event=%p(%s))\n",
-                  sub, sub->id, event, cupsdEventName(event->event));
-
- /*
-  * Allocate the events array as needed...
-  */
-
-  if (!sub->events)
-  {
-    sub->events = calloc(MaxEvents, sizeof(cupsd_event_t *));
-
-    if (!sub->events)
-    {
-      cupsdLogMessage(CUPSD_LOG_CRIT,
-                      "Unable to allocate memory for subscription #%d!",
-                      sub->id);
-      return;
-    }
-  }
-
- /*
-  * Add the event to the subscription.  Since the events array is
-  * always MaxEvents in length, and since we will have already
-  * removed an event from the subscription cache if we hit the
-  * event cache limit, we don't need to check for overflow here...
-  */
-
-  sub->events[sub->num_events] = event;
-  sub->num_events ++;
-
- /*
-  * Deliver the event...
-  */
-
-  if (sub->recipient)
-  {
-    if (sub->pipe < 0)
-      cupsd_start_notifier(sub);
-
-    cupsdLogMessage(CUPSD_LOG_DEBUG, "sub->pipe=%d", sub->pipe);
-
-    if (sub->pipe >= 0)
-    {
-      event->attrs->state = IPP_IDLE;
-
-      while ((state = ippWriteFile(sub->pipe, event->attrs)) != IPP_DATA)
-        if (state == IPP_ERROR)
-         break;
-
-      if (state == IPP_ERROR)
-        cupsdLogMessage(CUPSD_LOG_ERROR,
-                       "Unable to send event for subscription %d (%s)!",
-                       sub->id, sub->recipient);
-    }
-  }
-
- /*
-  * Bump the event sequence number...
-  */
-
-  sub->next_event_id ++;
-}
-
-
 /*
  * 'cupsdStopAllNotifiers()' - Stop all notifier processes.
  */
@@ -1340,13 +1245,12 @@ cupsdStopAllNotifiers(void)
 void
 cupsdUpdateNotifierStatus(void)
 {
-  char         *ptr,                   /* Pointer to end of line in buffer */
-               message[1024];          /* Pointer to message text */
+  char         message[1024];          /* Pointer to message text */
   int          loglevel;               /* Log level for message */
 
 
-  while ((ptr = cupsdStatBufUpdate(NotifierStatusBuffer, &loglevel,
-                                   message, sizeof(message))) != NULL)
+  while (cupsdStatBufUpdate(NotifierStatusBuffer, &loglevel,
+                            message, sizeof(message)))
     if (!strchr(NotifierStatusBuffer->buffer, '\n'))
       break;
 }
@@ -1378,43 +1282,6 @@ cupsd_compare_subscriptions(
 static void
 cupsd_delete_event(cupsd_event_t *event)/* I - Event to delete */
 {
-  cupsd_subscription_t *sub;           /* Current subscription */
-
-
- /*
-  * Loop through the subscriptions and look for the event in the cache...
-  */
-
-  for (sub = (cupsd_subscription_t *)cupsArrayFirst(Subscriptions);
-       sub;
-       sub = (cupsd_subscription_t *)cupsArrayNext(Subscriptions))
-  {
-   /*
-    * Only check the first event in the subscription cache, since the
-    * caller will only delete the oldest event in the cache...
-    */
-
-    if (sub->num_events > 0 && sub->events[0] == event)
-    {
-     /*
-      * Remove this event...
-      */
-
-      sub->num_events --;
-      sub->first_event_id ++;
-
-      if (sub->num_events > 0)
-      {
-       /*
-        * Shift other events upward in cache...
-       */
-
-        memmove(sub->events, sub->events + 1,
-               sub->num_events * sizeof(cupsd_event_t *));
-      }
-    }
-  }
-
  /*
   * Free memory...
   */
@@ -1454,7 +1321,7 @@ cupsd_send_dbus(cupsd_eventmask_t event,/* I - Event to send */
   else if (event & CUPSD_EVENT_JOB_CREATED)
     what = "JobQueuedLocal";
   else if ((event & CUPSD_EVENT_JOB_STATE) && job &&
-           job->state->values[0].integer == IPP_JOB_PROCESSING)
+           job->state_value == IPP_JOB_PROCESSING)
     what = "JobStartedLocal";
   else
     return;
@@ -1504,6 +1371,130 @@ cupsd_send_dbus(cupsd_eventmask_t event,/* I - Event to send */
 #endif /* HAVE_DBUS */
 
 
+/*
+ * 'cupsd_send_notification()' - Send a notification for the specified event.
+ */
+
+static void
+cupsd_send_notification(
+    cupsd_subscription_t *sub,         /* I - Subscription object */
+    cupsd_event_t        *event)       /* I - Event to send */
+{
+  ipp_state_t  state;                  /* IPP event state */
+
+
+  cupsdLogMessage(CUPSD_LOG_DEBUG2,
+                  "cupsd_send_notification(sub=%p(%d), event=%p(%s))\n",
+                  sub, sub->id, event, cupsdEventName(event->event));
+
+ /*
+  * Allocate the events array as needed...
+  */
+
+  if (!sub->events)
+  {
+    sub->events = calloc(MaxEvents, sizeof(cupsd_event_t *));
+
+    if (!sub->events)
+    {
+      cupsdLogMessage(CUPSD_LOG_CRIT,
+                      "Unable to allocate memory for subscription #%d!",
+                      sub->id);
+      return;
+    }
+  }
+
+ /*
+  * Purge an old event as needed...
+  */
+
+  if (sub->num_events >= MaxEvents)
+  {
+   /*
+    * Purge the oldest event in the cache...
+    */
+
+    cupsd_delete_event(sub->events[0]);
+
+    sub->num_events --;
+    sub->first_event_id ++;
+
+    memmove(sub->events, sub->events + 1,
+           sub->num_events * sizeof(cupsd_event_t *));
+  }
+
+ /*
+  * Add the event to the subscription.  Since the events array is
+  * always MaxEvents in length, and since we will have already
+  * removed an event from the subscription cache if we hit the
+  * event cache limit, we don't need to check for overflow here...
+  */
+
+  sub->events[sub->num_events] = event;
+  sub->num_events ++;
+
+ /*
+  * Deliver the event...
+  */
+
+  if (sub->recipient)
+  {
+    for (;;)
+    {
+      if (sub->pipe < 0)
+       cupsd_start_notifier(sub);
+
+      cupsdLogMessage(CUPSD_LOG_DEBUG2, "sub->pipe=%d", sub->pipe);
+
+      if (sub->pipe < 0)
+       break;
+
+      event->attrs->state = IPP_IDLE;
+
+      while ((state = ippWriteFile(sub->pipe, event->attrs)) != IPP_DATA)
+        if (state == IPP_ERROR)
+         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;
+    }
+  }
+
+ /*
+  * Bump the event sequence number...
+  */
+
+  sub->next_event_id ++;
+}
+
+
 /*
  * 'cupsd_start_notifier()' - Start a notifier subprocess...
  */
@@ -1514,7 +1505,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 */
@@ -1554,7 +1544,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...
@@ -1632,5 +1622,5 @@ cupsd_start_notifier(
 
 
 /*
- * End of "$Id: subscriptions.c 5046 2006-02-01 22:11:58Z mike $".
+ * End of "$Id: subscriptions.c 5878 2006-08-24 15:55:42Z mike $".
  */