diff options
Diffstat (limited to 'notifier/dbus.c')
-rw-r--r-- | notifier/dbus.c | 91 |
1 files changed, 61 insertions, 30 deletions
diff --git a/notifier/dbus.c b/notifier/dbus.c index 08449d3..2a01f24 100644 --- a/notifier/dbus.c +++ b/notifier/dbus.c @@ -1,23 +1,18 @@ /* - * "$Id: dbus.c 11173 2013-07-23 12:31:34Z msweet $" + * "$Id: dbus.c 11594 2014-02-14 20:09:01Z 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/". */ /* @@ -154,10 +149,18 @@ 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 +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 */ /* @@ -416,7 +417,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 +446,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 +465,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 +485,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 +495,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 +518,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 +543,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,7 +577,7 @@ main(int argc, /* I - Number of command-line args */ if (lock_fd >= 0) { close(lock_fd); - unlink(lock_filename); + release_lock(); } return (0); @@ -584,6 +585,27 @@ main(int argc, /* I - Number of command-line args */ /* + * '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 +614,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 +633,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 @@ -623,5 +654,5 @@ main(void) /* - * End of "$Id: dbus.c 11173 2013-07-23 12:31:34Z msweet $". + * End of "$Id: dbus.c 11594 2014-02-14 20:09:01Z msweet $". */ |