]> git.ipfire.org Git - thirdparty/cups.git/blobdiff - scheduler/subscriptions.c
Remove all of the Subversion keywords from various source files.
[thirdparty/cups.git] / scheduler / subscriptions.c
index f8fe59c959fd3da416a83868c274782b43c78993..52e12af1697e3af591c7fbba7d674e5e2bd2b62c 100644 (file)
@@ -1,46 +1,14 @@
 /*
- * "$Id: subscriptions.c 6376 2007-03-21 06:39:10Z mike $"
+ * Subscription routines for the CUPS scheduler.
  *
- *   Subscription routines for the Common UNIX Printing System (CUPS) scheduler.
+ * Copyright 2007-2014 by Apple Inc.
+ * Copyright 1997-2007 by Easy Software Products, all rights reserved.
  *
- *   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
- *
- * Contents:
- *
- *   cupsdAddEvent()               - Add an event to the global event cache.
- *   cupsdAddSubscription()        - Add a new subscription object.
- *   cupsdDeleteAllSubscriptions() - Delete all subscriptions.
- *   cupsdDeleteSubscription()     - Delete a subscription object.
- *   cupsdEventName()              - Return a single event name.
- *   cupsdEventValue()             - Return the event mask value for a name.
- *   cupsdExpireSubscriptions()    - Expire old subscription objects.
- *   cupsdFindSubscription()       - Find a subscription by ID.
- *   cupsdLoadAllSubscriptions()   - Load all subscriptions from the .conf file.
- *   cupsdSaveAllSubscriptions()   - Save all subscriptions to the .conf file.
- *   cupsdStopAllNotifiers()       - Stop all notifier processes.
- *   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.
+ * These coded instructions, statements, and computer programs are the
+ * 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/".
  */
 
 /*
@@ -135,9 +103,7 @@ cupsdAddEvent(
     * Check if this subscription requires this event...
     */
 
-    if ((sub->mask & event) != 0 &&
-        (sub->dest == dest || !sub->dest) &&
-       (sub->job == job || !sub->job))
+    if ((sub->mask & event) != 0 && (sub->dest == dest || !sub->dest || sub->job == job))
     {
      /*
       * Need this event, so create a new event record...
@@ -155,7 +121,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...
@@ -165,7 +135,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);
@@ -217,7 +187,7 @@ cupsdAddEvent(
                        (const char * const *)dest->reasons);
 
        ippAddBoolean(temp->attrs, IPP_TAG_EVENT_NOTIFICATION,
-                     "printer-is-accepting-jobs", dest->accepting);
+                     "printer-is-accepting-jobs", (char)dest->accepting);
       }
 
       if (job)
@@ -306,7 +276,7 @@ cupsdAddEvent(
   }
 
   if (temp)
-    cupsdSaveAllSubscriptions();
+    cupsdMarkDirty(CUPSD_DIRTY_SUBSCRIPTIONS);
   else
     cupsdLogMessage(CUPSD_LOG_DEBUG, "Discarding unused %s event...",
                     cupsdEventName(event));
@@ -332,7 +302,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,
@@ -350,8 +320,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...
@@ -398,6 +416,13 @@ cupsdAddSubscription(
 
   cupsArrayAdd(Subscriptions, temp);
 
+ /*
+  * For RSS subscriptions, run the notifier immediately...
+  */
+
+  if (uri && !strncmp(uri, "rss:", 4))
+    cupsd_start_notifier(temp);
+
   return (temp);
 }
 
@@ -434,9 +459,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...
   */
@@ -457,13 +479,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);
 
@@ -472,7 +488,7 @@ cupsdDeleteSubscription(
   */
 
   if (update)
-    cupsdSaveAllSubscriptions();
+    cupsdMarkDirty(CUPSD_DIRTY_SUBSCRIPTIONS);
 }
 
 
@@ -513,9 +529,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");
 
@@ -538,8 +559,6 @@ cupsdEventName(
         return ("job-progress");
 
     case CUPSD_EVENT_JOB_STATE :
-        return ("job-state");
-
     case CUPSD_EVENT_JOB_STATE_CHANGED :
         return ("job-state-changed");
 
@@ -586,14 +605,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"))
@@ -636,6 +655,8 @@ cupsdExpireSubscriptions(
   curtime = time(NULL);
   update  = 0;
 
+  cupsdLogMessage(CUPSD_LOG_INFO, "Expiring subscriptions...");
+
   for (sub = (cupsd_subscription_t *)cupsArrayFirst(Subscriptions);
        sub;
        sub = (cupsd_subscription_t *)cupsArrayNext(Subscriptions))
@@ -652,7 +673,7 @@ cupsdExpireSubscriptions(
     }
 
   if (update)
-    cupsdSaveAllSubscriptions();
+    cupsdMarkDirty(CUPSD_DIRTY_SUBSCRIPTIONS);
 }
 
 
@@ -695,14 +716,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...
@@ -714,7 +729,7 @@ cupsdLoadAllSubscriptions(void)
 
   while (cupsFileGetConf(fp, line, sizeof(line), &value, &linenum))
   {
-    if (!strcasecmp(line, "NextSubscriptionId") && value)
+    if (!_cups_strcasecmp(line, "NextSubscriptionId") && value)
     {
      /*
       * NextSubscriptionId NNN
@@ -724,7 +739,7 @@ cupsdLoadAllSubscriptions(void)
       if (i >= NextSubscriptionId && i > 0)
         NextSubscriptionId = i;
     }
-    else if (!strcasecmp(line, "<Subscription"))
+    else if (!_cups_strcasecmp(line, "<Subscription"))
     {
      /*
       * <Subscription #>
@@ -740,17 +755,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)
@@ -764,9 +779,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
@@ -778,7 +792,7 @@ cupsdLoadAllSubscriptions(void)
        cupsdLogMessage(CUPSD_LOG_ERROR,
                        "Syntax error on line %d of subscriptions.conf.",
                        linenum);
-       return;
+       break;
       }
 
       while (*value)
@@ -801,13 +815,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
@@ -820,10 +834,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
@@ -836,10 +850,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 #
@@ -860,10 +874,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
@@ -884,10 +898,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
@@ -908,9 +922,9 @@ cupsdLoadAllSubscriptions(void)
            if (isxdigit(valueptr[0]) && isxdigit(valueptr[1]))
            {
              if (isdigit(valueptr[0]))
-               sub->user_data[i] = (valueptr[0] - '0') << 4;
+               sub->user_data[i] = (unsigned char)((valueptr[0] - '0') << 4);
              else
-               sub->user_data[i] = (tolower(valueptr[0]) - 'a' + 10) << 4;
+               sub->user_data[i] = (unsigned char)((tolower(valueptr[0]) - 'a' + 10) << 4);
 
              if (isdigit(valueptr[1]))
                sub->user_data[i] |= valueptr[1] - '0';
@@ -929,7 +943,7 @@ cupsdLoadAllSubscriptions(void)
              break;
          }
          else
-           sub->user_data[i] = *valueptr++;
+           sub->user_data[i] = (unsigned char)*valueptr++;
        }
 
        if (*valueptr)
@@ -946,10 +960,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 #
@@ -965,10 +979,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 #
@@ -981,10 +995,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 #
@@ -997,10 +1011,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 #
@@ -1013,7 +1027,7 @@ cupsdLoadAllSubscriptions(void)
        cupsdLogMessage(CUPSD_LOG_ERROR,
                        "Syntax error on line %d of subscriptions.conf.",
                        linenum);
-       return;
+       break;
       }
     }
     else
@@ -1041,8 +1055,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 */
@@ -1055,36 +1069,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);
+  snprintf(filename, sizeof(filename), "%s/subscriptions.conf", 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));
-
-    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...
@@ -1185,7 +1175,7 @@ cupsdSaveAllSubscriptions(void)
     cupsFilePuts(fp, "</Subscription>\n");
   }
 
-  cupsFileClose(fp);
+  cupsdCloseCreatedConfFile(fp, filename);
 }
 
 
@@ -1369,7 +1359,7 @@ cupsd_send_notification(
 
 
   cupsdLogMessage(CUPSD_LOG_DEBUG2,
-                  "cupsd_send_notification(sub=%p(%d), event=%p(%s))\n",
+                  "cupsd_send_notification(sub=%p(%d), event=%p(%s))",
                   sub, sub->id, event, cupsdEventName(event->event));
 
  /*
@@ -1378,7 +1368,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)
     {
@@ -1393,19 +1386,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 *));
   }
 
  /*
@@ -1415,8 +1404,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...
@@ -1574,7 +1562,7 @@ cupsd_start_notifier(
   */
 
   if (cupsdStartProcess(command, argv, envp, fds[0], -1, NotifierPipes[1],
-                       -1, -1, 0, &pid) < 0)
+                       -1, -1, 0, DefaultProfile, NULL, &pid) < 0)
   {
    /*
     * Error - can't fork!
@@ -1616,11 +1604,11 @@ cupsd_update_notifier(void)
 
   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: subscriptions.c 6376 2007-03-21 06:39:10Z mike $".
- */