]> git.ipfire.org Git - thirdparty/cups.git/blobdiff - scheduler/subscriptions.c
Update svn:keyword properties.
[thirdparty/cups.git] / scheduler / subscriptions.c
index a0e2dc60212d719c32f26ff81a84716e7080b6b8..2e9eee3770b5115a387d7529e737d5041d72cd7f 100644 (file)
@@ -1,9 +1,9 @@
 /*
- * "$Id: subscriptions.c 7673 2008-06-18 22:31:26Z mike $"
+ * "$Id$"
  *
- *   Subscription routines for the Common UNIX Printing System (CUPS) scheduler.
+ *   Subscription routines for the CUPS scheduler.
  *
- *   Copyright 2007-2008 by Apple Inc.
+ *   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
@@ -146,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...
@@ -156,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);
@@ -341,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...
@@ -432,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...
   */
@@ -455,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);
 
@@ -696,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...
@@ -715,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
@@ -725,7 +762,7 @@ cupsdLoadAllSubscriptions(void)
       if (i >= NextSubscriptionId && i > 0)
         NextSubscriptionId = i;
     }
-    else if (!strcasecmp(line, "<Subscription"))
+    else if (!_cups_strcasecmp(line, "<Subscription"))
     {
      /*
       * <Subscription #>
@@ -744,7 +781,7 @@ cupsdLoadAllSubscriptions(void)
         break;
       }
     }
-    else if (!strcasecmp(line, "</Subscription>"))
+    else if (!_cups_strcasecmp(line, "</Subscription>"))
     {
       if (!sub)
       {
@@ -765,9 +802,8 @@ cupsdLoadAllSubscriptions(void)
       cupsdLogMessage(CUPSD_LOG_ERROR,
                       "Syntax error on line %d of subscriptions.conf.",
                      linenum);
-      break;
     }
-    else if (!strcasecmp(line, "Events"))
+    else if (!_cups_strcasecmp(line, "Events"))
     {
      /*
       * Events name
@@ -808,7 +844,7 @@ cupsdLoadAllSubscriptions(void)
        value = valueptr;
       }
     }
-    else if (!strcasecmp(line, "Owner"))
+    else if (!_cups_strcasecmp(line, "Owner"))
     {
      /*
       * Owner
@@ -824,7 +860,7 @@ cupsdLoadAllSubscriptions(void)
        break;
       }
     }
-    else if (!strcasecmp(line, "Recipient"))
+    else if (!_cups_strcasecmp(line, "Recipient"))
     {
      /*
       * Recipient uri
@@ -840,7 +876,7 @@ cupsdLoadAllSubscriptions(void)
        break;
       }
     }
-    else if (!strcasecmp(line, "JobId"))
+    else if (!_cups_strcasecmp(line, "JobId"))
     {
      /*
       * JobId #
@@ -864,7 +900,7 @@ cupsdLoadAllSubscriptions(void)
        break;
       }
     }
-    else if (!strcasecmp(line, "PrinterName"))
+    else if (!_cups_strcasecmp(line, "PrinterName"))
     {
      /*
       * PrinterName name
@@ -888,7 +924,7 @@ cupsdLoadAllSubscriptions(void)
        break;
       }
     }
-    else if (!strcasecmp(line, "UserData"))
+    else if (!_cups_strcasecmp(line, "UserData"))
     {
      /*
       * UserData encoded-string
@@ -950,7 +986,7 @@ cupsdLoadAllSubscriptions(void)
        break;
       }
     }
-    else if (!strcasecmp(line, "LeaseDuration"))
+    else if (!_cups_strcasecmp(line, "LeaseDuration"))
     {
      /*
       * LeaseDuration #
@@ -969,7 +1005,7 @@ cupsdLoadAllSubscriptions(void)
        break;
       }
     }
-    else if (!strcasecmp(line, "Interval"))
+    else if (!_cups_strcasecmp(line, "Interval"))
     {
      /*
       * Interval #
@@ -985,7 +1021,7 @@ cupsdLoadAllSubscriptions(void)
        break;
       }
     }
-    else if (!strcasecmp(line, "ExpirationTime"))
+    else if (!_cups_strcasecmp(line, "ExpirationTime"))
     {
      /*
       * ExpirationTime #
@@ -1001,7 +1037,7 @@ cupsdLoadAllSubscriptions(void)
        break;
       }
     }
-    else if (!strcasecmp(line, "NextEventId"))
+    else if (!_cups_strcasecmp(line, "NextEventId"))
     {
      /*
       * NextEventId #
@@ -1042,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 */
@@ -1056,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...
@@ -1186,7 +1198,7 @@ cupsdSaveAllSubscriptions(void)
     cupsFilePuts(fp, "</Subscription>\n");
   }
 
-  cupsFileClose(fp);
+  cupsdCloseCreatedConfFile(fp, filename);
 }
 
 
@@ -1379,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)
     {
@@ -1394,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 *));
   }
 
  /*
@@ -1416,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...
@@ -1575,7 +1585,7 @@ cupsd_start_notifier(
   */
 
   if (cupsdStartProcess(command, argv, envp, fds[0], -1, NotifierPipes[1],
-                       -1, -1, 0, DefaultProfile, &pid) < 0)
+                       -1, -1, 0, DefaultProfile, NULL, &pid) < 0)
   {
    /*
     * Error - can't fork!
@@ -1617,11 +1627,16 @@ 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 7673 2008-06-18 22:31:26Z mike $".
+ * End of "$Id$".
  */