]> git.ipfire.org Git - thirdparty/cups.git/blobdiff - notifier/dbus.c
Import CUPS v1.7.1
[thirdparty/cups.git] / notifier / dbus.c
index 99efbd8590623b15928204d8883a2caf38d873bf..fc63aac4b4403b75ace255ddbf21fc542494e97f 100644 (file)
@@ -1,23 +1,18 @@
 /*
- * "$Id$"
+ * "$Id: dbus.c 11500 2014-01-06 22:21:15Z msweet $"
  *
- *   D-Bus notifier for CUPS.
+ * D-Bus notifier for CUPS.
  *
- *   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.
+ * 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.
  *
- *   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.
+ * 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/".
  */
 
 /*
 #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 +148,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 +179,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 */
 
 
  /*
@@ -444,7 +446,8 @@ 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 - (p - printer_reasons));
            p += strlen(p);
          }
          if (!dbus_message_iter_append_string(&iter, &printer_reasons))
@@ -516,7 +519,8 @@ 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 - (p - job_reasons));
          p += strlen(p);
        }
        if (!dbus_message_iter_append_string(&iter, &job_reasons))
@@ -575,13 +579,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.
  */
@@ -591,7 +616,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 */
 
 
  /*
@@ -609,11 +635,26 @@ 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
+main(void)
+{
+  return (1);
 }
+#endif /* HAVE_DBUS */
 
 
 /*
- * End of "$Id$".
+ * End of "$Id: dbus.c 11500 2014-01-06 22:21:15Z msweet $".
  */