]> git.ipfire.org Git - thirdparty/cups.git/blobdiff - notifier/dbus.c
License change: Apache License, Version 2.0.
[thirdparty/cups.git] / notifier / dbus.c
index 4f5b48c55b81aad9fb684dd40954cf594967d09b..1ebd2eaf47e6d64a21958678ac840c8055f6f1fc 100644 (file)
@@ -1,23 +1,12 @@
 /*
- * "$Id$"
+ * D-Bus notifier for CUPS.
  *
- *   D-Bus notifier for CUPS.
+ * Copyright 2008-2014 by Apple Inc.
+ * Copyright (C) 2011, 2013 Red Hat, Inc.
+ * Copyright (C) 2007 Tim Waugh <twaugh@redhat.com>
+ * Copyright 1997-2005 by Easy Software Products.
  *
- *   Copyright 2008-2010 by Apple Inc.
- *   Copyright (C) 2011 Red Hat, Inc.
- *   Copyright (C) 2007 Tim Waugh <twaugh@redhat.com>
- *   Copyright 1997-2005 by Easy Software Products.
- *
- *   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:
- *
- *   main()         - Read events and send DBUS notifications.
- *   acquire_lock() - Acquire a lock so we only have a single notifier running.
+ * Licensed under Apache License v2.0.  See the file "LICENSE" for more information.
  */
 
 /*
 #include <sys/types.h>
 #include <unistd.h>
 
-#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)
-#  define dbus_message_iter_append_boolean(i,v) dbus_message_iter_append_basic(i, DBUS_TYPE_BOOLEAN, v)
-#endif /* HAVE_DBUS_MESSAGE_ITER_INIT_APPEND */
+#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)
+#    define dbus_message_iter_append_boolean(i,v) dbus_message_iter_append_basic(i, DBUS_TYPE_BOOLEAN, v)
+#  endif /* HAVE_DBUS_MESSAGE_ITER_INIT_APPEND */
 
 
 /*
@@ -152,11 +142,19 @@ enum
 };
 
 
+/*
+ * Global variables...
+ */
+
+static char            lock_filename[1024];    /* Lock filename */
+
+
 /*
  * Local functions...
  */
 
 static int     acquire_lock(int *fd, char *lockfile, size_t locksize);
+static void    release_lock(void);
 
 
 /*
@@ -175,8 +173,6 @@ main(int  argc,                             /* I - Number of command-line args */
   DBusMessage          *message;       /* Message to send */
   DBusMessageIter      iter;           /* Iterator for message data */
   int                  lock_fd = -1;   /* Lock file descriptor */
-  char                 lock_filename[1024];
-                                       /* Lock filename */
 
 
  /*
@@ -220,6 +216,8 @@ main(int  argc,                             /* I - Number of command-line args */
     const char         *signame = NULL;/* DBUS signal name */
     char               *printer_reasons = NULL;
                                        /* Printer reasons string */
+    char               *job_reasons = NULL;
+                                       /* Job reasons string */
     const char         *nul = "";      /* Empty string value */
     int                        no = 0;         /* Boolean "no" value */
     int                        params = PARAMS_NONE;
@@ -281,11 +279,11 @@ main(int  argc,                           /* I - Number of command-line args */
       continue;
 
     attr = ippFindAttribute(msg, "notify-subscribed-event",
-                            IPP_TAG_KEYWORD);
+                           IPP_TAG_KEYWORD);
     if (!attr)
       continue;
 
-    event = attr->values[0].string.text;
+    event = ippGetString(attr, 0, NULL);
     if (!strncmp(event, "server-", 7))
     {
       const char *word2 = event + 7;   /* Second word */
@@ -361,9 +359,14 @@ main(int  argc,                            /* I - Number of command-line args */
 
     dbus_message_append_iter_init(message, &iter);
     attr = ippFindAttribute(msg, "notify-text", IPP_TAG_TEXT);
-    if (!attr)
+    if (attr)
+    {
+      const char *val = ippGetString(attr, 0, NULL);
+      if (!dbus_message_iter_append_string(&iter, &val))
+        goto bail;
+    }
+    else
       goto bail;
-    dbus_message_iter_append_string(&iter, &(attr->values[0].string.text));
 
     if (params >= PARAMS_PRINTER)
     {
@@ -375,7 +378,11 @@ main(int  argc,                            /* I - Number of command-line args */
       /* STRING printer-uri or "" */
       attr = ippFindAttribute(msg, "notify-printer-uri", IPP_TAG_URI);
       if (attr)
-       dbus_message_iter_append_string(&iter, &(attr->values[0].string.text));
+      {
+        const char *val = ippGetString(attr, 0, NULL);
+        if (!dbus_message_iter_append_string(&iter, &val))
+         goto bail;
+      }
       else
       {
        have_printer_params = 0;
@@ -386,11 +393,14 @@ main(int  argc,                           /* I - Number of command-line args */
       if (have_printer_params)
       {
        attr = ippFindAttribute(msg, "printer-name", IPP_TAG_NAME);
-       if (attr)
-         dbus_message_iter_append_string(&iter,
-                                         &(attr->values[0].string.text));
-       else
-         goto bail;
+        if (attr)
+        {
+          const char *val = ippGetString(attr, 0, NULL);
+          if (!dbus_message_iter_append_string(&iter, &val))
+            goto bail;
+        }
+        else
+          goto bail;
       }
       else
        dbus_message_iter_append_string(&iter, &nul);
@@ -400,7 +410,10 @@ main(int  argc,                            /* I - Number of command-line args */
       {
        attr = ippFindAttribute(msg, "printer-state", IPP_TAG_ENUM);
        if (attr)
-         dbus_message_iter_append_uint32(&iter, &(attr->values[0].integer));
+       {
+         dbus_uint32_t val = (dbus_uint32_t)ippGetInteger(attr, 0);
+         dbus_message_iter_append_uint32(&iter, &val);
+       }
        else
          goto bail;
       }
@@ -414,22 +427,24 @@ main(int  argc,                           /* I - Number of command-line args */
                                IPP_TAG_KEYWORD);
        if (attr)
        {
-         for (reasons_length = 0, i = 0; i < attr->num_values; i++)
+         int num_values = ippGetCount(attr);
+         for (reasons_length = 0, i = 0; i < num_values; i++)
            /* All need commas except the last, which needs a nul byte. */
-           reasons_length += 1 + strlen(attr->values[i].string.text);
+           reasons_length += 1 + strlen(ippGetString(attr, i, NULL));
          printer_reasons = malloc(reasons_length);
          if (!printer_reasons)
            goto bail;
          p = printer_reasons;
-         for (i = 0; i < attr->num_values; i++)
+         for (i = 0; i < num_values; i++)
          {
            if (i)
              *p++ = ',';
 
-           strcpy(p, attr->values[i].string.text);
+           strlcpy(p, ippGetString(attr, i, NULL), reasons_length - (size_t)(p - printer_reasons));
            p += strlen(p);
          }
-         dbus_message_iter_append_string(&iter, &printer_reasons);
+         if (!dbus_message_iter_append_string(&iter, &printer_reasons))
+           goto bail;
        }
        else
          goto bail;
@@ -443,7 +458,10 @@ main(int  argc,                            /* I - Number of command-line args */
        attr = ippFindAttribute(msg, "printer-is-accepting-jobs",
                                IPP_TAG_BOOLEAN);
        if (attr)
-         dbus_message_iter_append_boolean(&iter, &(attr->values[0].boolean));
+       {
+         dbus_bool_t val = (dbus_bool_t)ippGetBoolean(attr, 0);
+         dbus_message_iter_append_boolean(&iter, &val);
+       }
        else
          goto bail;
       }
@@ -453,37 +471,77 @@ main(int  argc,                           /* I - Number of command-line args */
 
     if (params >= PARAMS_JOB)
     {
+      char     *p;                     /* Pointer into job_reasons */
+      size_t   reasons_length;         /* Required size of job_reasons */
+      int      i;                      /* Looping var */
+
       /* UINT32 job-id */
       attr = ippFindAttribute(msg, "notify-job-id", IPP_TAG_INTEGER);
-      if (!attr)
+      if (attr)
+      {
+        dbus_uint32_t val = (dbus_uint32_t)ippGetInteger(attr, 0);
+        dbus_message_iter_append_uint32(&iter, &val);
+      }
+      else
        goto bail;
-      dbus_message_iter_append_uint32(&iter, &(attr->values[0].integer));
 
       /* UINT32 job-state */
       attr = ippFindAttribute(msg, "job-state", IPP_TAG_ENUM);
-      if (!attr)
+      if (attr)
+      {
+        dbus_uint32_t val = (dbus_uint32_t)ippGetInteger(attr, 0);
+        dbus_message_iter_append_uint32(&iter, &val);
+      }
+      else
        goto bail;
-      dbus_message_iter_append_uint32(&iter, &(attr->values[0].integer));
 
       /* STRING job-state-reasons */
       attr = ippFindAttribute(msg, "job-state-reasons", IPP_TAG_KEYWORD);
-      if (!attr)
+      if (attr)
+      {
+       int num_values = ippGetCount(attr);
+       for (reasons_length = 0, i = 0; i < num_values; i++)
+         /* All need commas except the last, which needs a nul byte. */
+         reasons_length += 1 + strlen(ippGetString(attr, i, NULL));
+       job_reasons = malloc(reasons_length);
+       if (!job_reasons)
+         goto bail;
+       p = job_reasons;
+       for (i = 0; i < num_values; i++)
+       {
+         if (i)
+           *p++ = ',';
+
+         strlcpy(p, ippGetString(attr, i, NULL), reasons_length - (size_t)(p - job_reasons));
+         p += strlen(p);
+       }
+       if (!dbus_message_iter_append_string(&iter, &job_reasons))
+         goto bail;
+      }
+      else
        goto bail;
-      dbus_message_iter_append_string(&iter, &(attr->values[0].string.text));
 
       /* STRING job-name or "" */
       attr = ippFindAttribute(msg, "job-name", IPP_TAG_NAME);
       if (attr)
-       dbus_message_iter_append_string(&iter, &(attr->values[0].string.text));
+      {
+        const char *val = ippGetString(attr, 0, NULL);
+        if (!dbus_message_iter_append_string(&iter, &val))
+          goto bail;
+      }
       else
        dbus_message_iter_append_string(&iter, &nul);
 
       /* UINT32 job-impressions-completed */
       attr = ippFindAttribute(msg, "job-impressions-completed",
                              IPP_TAG_INTEGER);
-      if (!attr)
+      if (attr)
+      {
+        dbus_uint32_t val = (dbus_uint32_t)ippGetInteger(attr, 0);
+        dbus_message_iter_append_uint32(&iter, &val);
+      }
+      else
        goto bail;
-      dbus_message_iter_append_uint32(&iter, &(attr->values[0].integer));
     }
 
     dbus_connection_send(con, message, NULL);
@@ -495,10 +553,14 @@ main(int  argc,                           /* I - Number of command-line args */
 
     bail:
 
+    dbus_message_unref(message);
+
     if (printer_reasons)
       free(printer_reasons);
 
-    dbus_message_unref(message);
+    if (job_reasons)
+      free(job_reasons);
+
     ippDelete(msg);
   }
 
@@ -509,13 +571,34 @@ main(int  argc,                           /* I - Number of command-line args */
   if (lock_fd >= 0)
   {
     close(lock_fd);
-    unlink(lock_filename);
+    release_lock();
   }
 
   return (0);
 }
 
 
+/*
+ * 'release_lock()' - Release the singleton lock.
+ */
+
+static void
+release_lock(void)
+{
+  unlink(lock_filename);
+}
+
+
+/*
+ * 'handle_sigterm()' - Handle SIGTERM signal.
+ */
+static void
+handle_sigterm(int signum)
+{
+  release_lock();
+  _exit(0);
+}
+
 /*
  * 'acquire_lock()' - Acquire a lock so we only have a single notifier running.
  */
@@ -525,7 +608,8 @@ acquire_lock(int    *fd,            /* O - Lock file descriptor */
              char   *lockfile,         /* I - Lock filename buffer */
             size_t locksize)           /* I - Size of filename buffer */
 {
-  const char   *tmpdir;                /* Temporary directory */
+  const char           *tmpdir;        /* Temporary directory */
+  struct sigaction     action;         /* POSIX sigaction data */
 
 
  /*
@@ -543,11 +627,21 @@ acquire_lock(int    *fd,          /* O - Lock file descriptor */
 
   if ((*fd = open(lockfile, O_RDWR | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR)) < 0)
     return (-1);
-  else
-    return (0);
-}
 
+ /*
+  * Set a SIGTERM handler to make sure we release the lock if the
+  * scheduler decides to stop us.
+  */
+  memset(&action, 0, sizeof(action));
+  action.sa_handler = handle_sigterm;
+  sigaction(SIGTERM, &action, NULL);
 
-/*
- * End of "$Id$".
- */
+  return (0);
+}
+#else /* !HAVE_DBUS */
+int
+main(void)
+{
+  return (1);
+}
+#endif /* HAVE_DBUS */