]> git.ipfire.org Git - thirdparty/cups.git/blobdiff - scheduler/subscriptions.c
License change: Apache License, Version 2.0.
[thirdparty/cups.git] / scheduler / subscriptions.c
index 92dffe6423aa9c1ad11baf00a785815cd7703054..36bf29cebbaea5c3252db2e412d6ef96d243d552 100644 (file)
@@ -1,37 +1,10 @@
 /*
- * "$Id: subscriptions.c 7824 2008-08-01 21:11:55Z mike $"
+ * Subscription routines for the CUPS scheduler.
  *
- *   Subscription routines for the CUPS scheduler.
+ * Copyright 2007-2014 by Apple Inc.
+ * Copyright 1997-2007 by Easy Software Products, all rights reserved.
  *
- *   Copyright 2007-2010 by Apple Inc.
- *   Copyright 1997-2007 by Easy Software Products, all rights reserved.
- *
- *   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/".
- *
- * 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.
+ * Licensed under Apache License v2.0.  See the file "LICENSE" for more information.
  */
 
 /*
@@ -126,9 +99,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...
@@ -146,7 +117,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 +131,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);
@@ -208,7 +183,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)
@@ -676,6 +651,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))
@@ -735,14 +712,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...
@@ -754,7 +725,7 @@ cupsdLoadAllSubscriptions(void)
 
   while (cupsFileGetConf(fp, line, sizeof(line), &value, &linenum))
   {
-    if (!strcasecmp(line, "NextSubscriptionId") && value)
+    if (!_cups_strcasecmp(line, "NextSubscriptionId") && value)
     {
      /*
       * NextSubscriptionId NNN
@@ -764,7 +735,7 @@ cupsdLoadAllSubscriptions(void)
       if (i >= NextSubscriptionId && i > 0)
         NextSubscriptionId = i;
     }
-    else if (!strcasecmp(line, "<Subscription"))
+    else if (!_cups_strcasecmp(line, "<Subscription"))
     {
      /*
       * <Subscription #>
@@ -783,7 +754,7 @@ cupsdLoadAllSubscriptions(void)
         break;
       }
     }
-    else if (!strcasecmp(line, "</Subscription>"))
+    else if (!_cups_strcasecmp(line, "</Subscription>"))
     {
       if (!sub)
       {
@@ -805,7 +776,7 @@ cupsdLoadAllSubscriptions(void)
                       "Syntax error on line %d of subscriptions.conf.",
                      linenum);
     }
-    else if (!strcasecmp(line, "Events"))
+    else if (!_cups_strcasecmp(line, "Events"))
     {
      /*
       * Events name
@@ -846,7 +817,7 @@ cupsdLoadAllSubscriptions(void)
        value = valueptr;
       }
     }
-    else if (!strcasecmp(line, "Owner"))
+    else if (!_cups_strcasecmp(line, "Owner"))
     {
      /*
       * Owner
@@ -862,7 +833,7 @@ cupsdLoadAllSubscriptions(void)
        break;
       }
     }
-    else if (!strcasecmp(line, "Recipient"))
+    else if (!_cups_strcasecmp(line, "Recipient"))
     {
      /*
       * Recipient uri
@@ -878,7 +849,7 @@ cupsdLoadAllSubscriptions(void)
        break;
       }
     }
-    else if (!strcasecmp(line, "JobId"))
+    else if (!_cups_strcasecmp(line, "JobId"))
     {
      /*
       * JobId #
@@ -902,7 +873,7 @@ cupsdLoadAllSubscriptions(void)
        break;
       }
     }
-    else if (!strcasecmp(line, "PrinterName"))
+    else if (!_cups_strcasecmp(line, "PrinterName"))
     {
      /*
       * PrinterName name
@@ -926,7 +897,7 @@ cupsdLoadAllSubscriptions(void)
        break;
       }
     }
-    else if (!strcasecmp(line, "UserData"))
+    else if (!_cups_strcasecmp(line, "UserData"))
     {
      /*
       * UserData encoded-string
@@ -947,9 +918,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';
@@ -968,7 +939,7 @@ cupsdLoadAllSubscriptions(void)
              break;
          }
          else
-           sub->user_data[i] = *valueptr++;
+           sub->user_data[i] = (unsigned char)*valueptr++;
        }
 
        if (*valueptr)
@@ -988,7 +959,7 @@ cupsdLoadAllSubscriptions(void)
        break;
       }
     }
-    else if (!strcasecmp(line, "LeaseDuration"))
+    else if (!_cups_strcasecmp(line, "LeaseDuration"))
     {
      /*
       * LeaseDuration #
@@ -1007,7 +978,7 @@ cupsdLoadAllSubscriptions(void)
        break;
       }
     }
-    else if (!strcasecmp(line, "Interval"))
+    else if (!_cups_strcasecmp(line, "Interval"))
     {
      /*
       * Interval #
@@ -1023,7 +994,7 @@ cupsdLoadAllSubscriptions(void)
        break;
       }
     }
-    else if (!strcasecmp(line, "ExpirationTime"))
+    else if (!_cups_strcasecmp(line, "ExpirationTime"))
     {
      /*
       * ExpirationTime #
@@ -1039,7 +1010,7 @@ cupsdLoadAllSubscriptions(void)
        break;
       }
     }
-    else if (!strcasecmp(line, "NextEventId"))
+    else if (!_cups_strcasecmp(line, "NextEventId"))
     {
      /*
       * NextEventId #
@@ -1080,8 +1051,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 */
@@ -1094,36 +1065,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...
@@ -1224,7 +1171,7 @@ cupsdSaveAllSubscriptions(void)
     cupsFilePuts(fp, "</Subscription>\n");
   }
 
-  cupsFileClose(fp);
+  cupsdCloseCreatedConfFile(fp, filename);
 }
 
 
@@ -1661,8 +1608,3 @@ cupsd_update_notifier(void)
       break;
   }
 }
-
-
-/*
- * End of "$Id: subscriptions.c 7824 2008-08-01 21:11:55Z mike $".
- */