]> 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 890b2ae09b4b3ec64084b69eda0443e56463b93d..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-2011 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.
  */
 
 /*
@@ -153,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);
 
 
 /*
@@ -176,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 */
 
 
  /*
@@ -416,7 +411,7 @@ main(int  argc,                             /* I - Number of command-line args */
        attr = ippFindAttribute(msg, "printer-state", IPP_TAG_ENUM);
        if (attr)
        {
-         dbus_uint32_t val = ippGetInteger(attr, 0);
+         dbus_uint32_t val = (dbus_uint32_t)ippGetInteger(attr, 0);
          dbus_message_iter_append_uint32(&iter, &val);
        }
        else
@@ -445,7 +440,7 @@ main(int  argc,                             /* I - Number of command-line args */
            if (i)
              *p++ = ',';
 
-           strcpy(p, ippGetString(attr, i, NULL));
+           strlcpy(p, ippGetString(attr, i, NULL), reasons_length - (size_t)(p - printer_reasons));
            p += strlen(p);
          }
          if (!dbus_message_iter_append_string(&iter, &printer_reasons))
@@ -464,7 +459,7 @@ main(int  argc,                             /* I - Number of command-line args */
                                IPP_TAG_BOOLEAN);
        if (attr)
        {
-         dbus_bool_t val = ippGetBoolean(attr, 0);
+         dbus_bool_t val = (dbus_bool_t)ippGetBoolean(attr, 0);
          dbus_message_iter_append_boolean(&iter, &val);
        }
        else
@@ -484,7 +479,7 @@ main(int  argc,                             /* I - Number of command-line args */
       attr = ippFindAttribute(msg, "notify-job-id", IPP_TAG_INTEGER);
       if (attr)
       {
-        dbus_uint32_t val = ippGetInteger(attr, 0);
+        dbus_uint32_t val = (dbus_uint32_t)ippGetInteger(attr, 0);
         dbus_message_iter_append_uint32(&iter, &val);
       }
       else
@@ -494,7 +489,7 @@ main(int  argc,                             /* I - Number of command-line args */
       attr = ippFindAttribute(msg, "job-state", IPP_TAG_ENUM);
       if (attr)
       {
-        dbus_uint32_t val = ippGetInteger(attr, 0);
+        dbus_uint32_t val = (dbus_uint32_t)ippGetInteger(attr, 0);
         dbus_message_iter_append_uint32(&iter, &val);
       }
       else
@@ -517,7 +512,7 @@ main(int  argc,                             /* I - Number of command-line args */
          if (i)
            *p++ = ',';
 
-         strcpy(p, ippGetString(attr, i, NULL));
+         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))
@@ -542,7 +537,7 @@ main(int  argc,                             /* I - Number of command-line args */
                              IPP_TAG_INTEGER);
       if (attr)
       {
-        dbus_uint32_t val = ippGetInteger(attr, 0);
+        dbus_uint32_t val = (dbus_uint32_t)ippGetInteger(attr, 0);
         dbus_message_iter_append_uint32(&iter, &val);
       }
       else
@@ -576,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.
  */
@@ -592,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 */
 
 
  /*
@@ -610,8 +627,16 @@ 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);
+
+  return (0);
 }
 #else /* !HAVE_DBUS */
 int
@@ -620,8 +645,3 @@ main(void)
   return (1);
 }
 #endif /* HAVE_DBUS */
-
-
-/*
- * End of "$Id$".
- */