0fed0d
diff -up cups-1.7rc1/scheduler/avahi.c.avahi-no-threaded cups-1.7rc1/scheduler/avahi.c
0fed0d
--- cups-1.7rc1/scheduler/avahi.c.avahi-no-threaded	2013-07-22 11:47:27.599101572 +0100
0fed0d
+++ cups-1.7rc1/scheduler/avahi.c	2013-07-22 11:47:27.599101572 +0100
0c8d5f
@@ -0,0 +1,441 @@
0c8d5f
+/*
0c8d5f
+ * "$Id$"
0c8d5f
+ *
0c8d5f
+ *   Avahi poll implementation for the CUPS scheduler.
0c8d5f
+ *
0c8d5f
+ *   Copyright (C) 2010, 2011 Red Hat, Inc.
0c8d5f
+ *   Authors:
0c8d5f
+ *    Tim Waugh <twaugh@redhat.com>
0c8d5f
+ *
0c8d5f
+ *   Redistribution and use in source and binary forms, with or without
0c8d5f
+ *   modification, are permitted provided that the following conditions
0c8d5f
+ *   are met:
0c8d5f
+ *
0c8d5f
+ *   Redistributions of source code must retain the above copyright
0c8d5f
+ *   notice, this list of conditions and the following disclaimer.
0c8d5f
+ *
0c8d5f
+ *   Redistributions in binary form must reproduce the above copyright
0c8d5f
+ *   notice, this list of conditions and the following disclaimer in the
0c8d5f
+ *   documentation and/or other materials provided with the distribution.
0c8d5f
+ *
0c8d5f
+ *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
0c8d5f
+ *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
0c8d5f
+ *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
0c8d5f
+ *   FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
0c8d5f
+ *   COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
0c8d5f
+ *   INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
0c8d5f
+ *   (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
0c8d5f
+ *   SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
0c8d5f
+ *   HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
0c8d5f
+ *   STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
0c8d5f
+ *   ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
0c8d5f
+ *   OF THE POSSIBILITY OF SUCH DAMAGE.
0c8d5f
+ *
0c8d5f
+ * Contents:
0c8d5f
+ *
0c8d5f
+ *   watch_read_cb         - Read callback for file descriptor
0c8d5f
+ *   watch_write_cb        - Write callback for file descriptor
0c8d5f
+ *   watched_fd_add_select() - Call cupsdAddSelect() as needed
0c8d5f
+ *   watch_new()           - Create a new file descriptor watch
0c8d5f
+ *   watch_free()          - Free a file descriptor watch
0c8d5f
+ *   watch_update()        - Update watched events for a file descriptor
0c8d5f
+ *   watch_get_events()    - Get events that happened for a file descriptor
0c8d5f
+ *   timeout_cb()          - Run a timed Avahi callback
0c8d5f
+ *   timeout_new()         - Set a wakeup time
0c8d5f
+ *   timeout_update()      - Update the expiration time for a timeout
0c8d5f
+ *   timeout_free()        - Free a timeout
0c8d5f
+ *   compare_watched_fds() - Compare watched file descriptors for array sorting
0c8d5f
+ *   avahi_cups_poll_new() - Create a new Avahi main loop object for CUPS
0c8d5f
+ *   avahi_cups_poll_free() - Free an Avahi main loop object for CUPS
0c8d5f
+ *   avahi_cups_poll_get() - Get the abstract poll API structure
0c8d5f
+ */
0c8d5f
+
0c8d5f
+#include <config.h>
0c8d5f
+
0c8d5f
+#ifdef HAVE_AVAHI /* Applies to entire file... */
0c8d5f
+
0c8d5f
+/*
0c8d5f
+ * Include necessary headers...
0c8d5f
+ */
0c8d5f
+
0c8d5f
+#include "cupsd.h"
0c8d5f
+
0c8d5f
+#if defined(HAVE_MALLOC_H) && defined(HAVE_MALLINFO)
0c8d5f
+#  include <malloc.h>
0c8d5f
+#endif /* HAVE_MALLOC_H && HAVE_MALLINFO */
0c8d5f
+
0c8d5f
+#ifdef HAVE_AVAHI
0c8d5f
+#  include <avahi-common timeval.h="">
0c8d5f
+#endif /* HAVE_AVAHI */
0c8d5f
+
0c8d5f
+
0c8d5f
+typedef struct
0c8d5f
+{
0c8d5f
+  AvahiCupsPoll *cups_poll;
0c8d5f
+
0c8d5f
+  int fd;
0c8d5f
+  AvahiWatchEvent occurred;
0c8d5f
+  cups_array_t *watches;
0c8d5f
+} cupsd_watched_fd_t;
0c8d5f
+
0c8d5f
+struct AvahiWatch
0c8d5f
+{
0c8d5f
+  cupsd_watched_fd_t *watched_fd;
0c8d5f
+
0c8d5f
+  AvahiWatchEvent events;
0c8d5f
+  AvahiWatchCallback callback;
0c8d5f
+  void *userdata;
0c8d5f
+};
0c8d5f
+
0c8d5f
+struct AvahiTimeout
0c8d5f
+{
0c8d5f
+  AvahiCupsPoll *cups_poll;
0c8d5f
+  AvahiTimeoutCallback callback;
0c8d5f
+  void *userdata;
0c8d5f
+  cupsd_timeout_t *cupsd_timeout;
0c8d5f
+};
0c8d5f
+
0c8d5f
+/*
0c8d5f
+ * Local functions...
0c8d5f
+ */
0c8d5f
+
0c8d5f
+static AvahiWatch *	watch_new(const AvahiPoll *api,
0c8d5f
+				  int fd,
0c8d5f
+				  AvahiWatchEvent events,
0c8d5f
+				  AvahiWatchCallback callback,
0c8d5f
+				  void *userdata);
0c8d5f
+static void		watch_free(AvahiWatch *watch);
0c8d5f
+static void		watch_update(AvahiWatch *watch,
0c8d5f
+				     AvahiWatchEvent events);
0c8d5f
+static AvahiWatchEvent	watch_get_events(AvahiWatch *watch);
0c8d5f
+
0c8d5f
+
0c8d5f
+/*
0c8d5f
+ * 'watch_read_cb' - Read callback for file descriptor
0c8d5f
+ */
0c8d5f
+
0c8d5f
+static void
0c8d5f
+watch_read_cb (void *userdata)
0c8d5f
+{
0c8d5f
+  AvahiWatch *watch;
0c8d5f
+  cupsd_watched_fd_t *watched_fd = userdata;
0c8d5f
+  watched_fd->occurred |= AVAHI_WATCH_IN;
0c8d5f
+  for (watch = (AvahiWatch *)cupsArrayFirst(watched_fd->watches);
0c8d5f
+       watch;
0c8d5f
+       watch = (AvahiWatch *)cupsArrayNext(watched_fd->watches))
0c8d5f
+  {
0c8d5f
+    if (watch->events & watched_fd->occurred)
0c8d5f
+    {
0c8d5f
+      (watch->callback) (watch, watched_fd->fd,
0c8d5f
+			 AVAHI_WATCH_IN, watch->userdata);
0c8d5f
+      watched_fd->occurred &= ~AVAHI_WATCH_IN;
0c8d5f
+      break;
0c8d5f
+    }
0c8d5f
+  }
0c8d5f
+}
0c8d5f
+
0c8d5f
+
0c8d5f
+/*
0c8d5f
+ * 'watch_write_cb' - Write callback for file descriptor
0c8d5f
+ */
0c8d5f
+
0c8d5f
+static void
0c8d5f
+watch_write_cb (void *userdata)
0c8d5f
+{
0c8d5f
+  AvahiWatch *watch;
0c8d5f
+  cupsd_watched_fd_t *watched_fd = userdata;
0c8d5f
+  watched_fd->occurred |= AVAHI_WATCH_OUT;
0c8d5f
+  for (watch = (AvahiWatch *)cupsArrayFirst(watched_fd->watches);
0c8d5f
+       watch;
0c8d5f
+       watch = (AvahiWatch *)cupsArrayNext(watched_fd->watches))
0c8d5f
+  {
0c8d5f
+    if (watch->events & watched_fd->occurred)
0c8d5f
+    {
0c8d5f
+      (watch->callback) (watch, watched_fd->fd,
0c8d5f
+			 AVAHI_WATCH_OUT, watch->userdata);
0c8d5f
+      watched_fd->occurred &= ~AVAHI_WATCH_OUT;
0c8d5f
+      break;
0c8d5f
+    }
0c8d5f
+  }
0c8d5f
+}
0c8d5f
+
0c8d5f
+
0c8d5f
+/*
0c8d5f
+ * 'watched_fd_add_select' - Call cupsdAddSelect() as needed
0c8d5f
+ */
0c8d5f
+
0c8d5f
+static int						/* O - Watches? */
0c8d5f
+watched_fd_add_select (cupsd_watched_fd_t *watched_fd)
0c8d5f
+{
0c8d5f
+  AvahiWatch *watch;
0c8d5f
+  cupsd_selfunc_t read_cb = NULL, write_cb = NULL;
0c8d5f
+  int any_watches = 0;
0c8d5f
+
0c8d5f
+  for (watch = (AvahiWatch *)cupsArrayFirst(watched_fd->watches);
0c8d5f
+       watch;
0c8d5f
+       watch = (AvahiWatch *)cupsArrayNext(watched_fd->watches))
0c8d5f
+  {
0c8d5f
+    any_watches = 1;
0c8d5f
+    if (watch->events & (AVAHI_WATCH_IN |
0c8d5f
+			     AVAHI_WATCH_ERR |
0c8d5f
+			     AVAHI_WATCH_HUP))
0c8d5f
+    {
0c8d5f
+      read_cb = (cupsd_selfunc_t)watch_read_cb;
0c8d5f
+      if (write_cb != NULL)
0c8d5f
+	break;
0c8d5f
+    }
0c8d5f
+
0c8d5f
+    if (watch->events & AVAHI_WATCH_OUT)
0c8d5f
+    {
0c8d5f
+      write_cb = (cupsd_selfunc_t)watch_write_cb;
0c8d5f
+      if (read_cb != NULL)
0c8d5f
+	break;
0c8d5f
+    }
0c8d5f
+  }
0c8d5f
+
0c8d5f
+  if (read_cb || write_cb)
0c8d5f
+    cupsdAddSelect (watched_fd->fd, read_cb, write_cb, watched_fd);
0c8d5f
+  else
0c8d5f
+    cupsdRemoveSelect (watched_fd->fd);
0c8d5f
+
0c8d5f
+  return (any_watches);
0c8d5f
+}
0c8d5f
+
0c8d5f
+/*
0c8d5f
+ * 'watch_new' - Create a new file descriptor watch
0c8d5f
+ */
0c8d5f
+
0c8d5f
+static AvahiWatch *
0c8d5f
+watch_new (const AvahiPoll *api,
0c8d5f
+	   int fd,
0c8d5f
+	   AvahiWatchEvent events,
0c8d5f
+	   AvahiWatchCallback callback,
0c8d5f
+	   void *userdata)
0c8d5f
+{
0c8d5f
+  cupsd_watched_fd_t key, *watched_fd;
0c8d5f
+  AvahiCupsPoll *cups_poll = api->userdata;
0c8d5f
+  AvahiWatch *watch = malloc(sizeof(AvahiWatch));
0c8d5f
+  if (watch == NULL)
0c8d5f
+    return (NULL);
0c8d5f
+
0c8d5f
+  watch->events = events;
0c8d5f
+  watch->callback = callback;
0c8d5f
+  watch->userdata = userdata;
0c8d5f
+
0c8d5f
+  key.fd = fd;
0c8d5f
+  watched_fd = cupsArrayFind (cups_poll->watched_fds, &key);
0c8d5f
+  if (watched_fd == NULL)
0c8d5f
+  {
0c8d5f
+    watched_fd = malloc(sizeof(cupsd_watched_fd_t));
0c8d5f
+    if (watched_fd == NULL)
0c8d5f
+    {
0c8d5f
+      free (watch);
0c8d5f
+      return (NULL);
0c8d5f
+    }
0c8d5f
+
0c8d5f
+    watched_fd->fd = fd;
0c8d5f
+    watched_fd->occurred = 0;
0c8d5f
+    watched_fd->cups_poll = cups_poll;
0c8d5f
+    watched_fd->watches = cupsArrayNew (NULL, NULL);
0c8d5f
+    cupsArrayAdd (cups_poll->watched_fds, watched_fd);
0c8d5f
+  }
0c8d5f
+
0c8d5f
+  watch->watched_fd = watched_fd;
0c8d5f
+  cupsArrayAdd(watched_fd->watches, watch);
0c8d5f
+  watched_fd_add_select (watched_fd);
0c8d5f
+  return (watch);
0c8d5f
+}
0c8d5f
+
0c8d5f
+
0c8d5f
+/*
0c8d5f
+ * 'watch_free' - Free a file descriptor watch
0c8d5f
+ */
0c8d5f
+
0c8d5f
+static void
0c8d5f
+watch_free (AvahiWatch *watch)
0c8d5f
+{
0c8d5f
+  cupsd_watched_fd_t *watched_fd = watch->watched_fd;
0c8d5f
+  AvahiCupsPoll *cups_poll = watched_fd->cups_poll;
0c8d5f
+
0c8d5f
+  cupsArrayRemove (watched_fd->watches, watch);
0c8d5f
+  free (watch);
0c8d5f
+
0c8d5f
+  if (!watched_fd_add_select (watched_fd))
0c8d5f
+  {
0c8d5f
+    /* No more watches */
0c8d5f
+    cupsArrayRemove (cups_poll->watched_fds, watched_fd);
0c8d5f
+    free (watched_fd);
0c8d5f
+  }
0c8d5f
+}
0c8d5f
+
0c8d5f
+
0c8d5f
+/*
0c8d5f
+ * 'watch_update' - Update watched events for a file descriptor
0c8d5f
+ */
0c8d5f
+
0c8d5f
+static void
0c8d5f
+watch_update (AvahiWatch *watch,
0c8d5f
+	      AvahiWatchEvent events)
0c8d5f
+{
0c8d5f
+  watch->events = events;
0c8d5f
+  watched_fd_add_select (watch->watched_fd);
0c8d5f
+}
0c8d5f
+
0c8d5f
+
0c8d5f
+/*
0c8d5f
+ * 'watch_get_events' - Get events that happened for a file descriptor
0c8d5f
+ */
0c8d5f
+
0c8d5f
+static AvahiWatchEvent
0c8d5f
+watch_get_events (AvahiWatch *watch)
0c8d5f
+{
0c8d5f
+  return (watch->watched_fd->occurred);
0c8d5f
+}
0c8d5f
+
0c8d5f
+
0c8d5f
+/*
0c8d5f
+ * 'timeout_cb()' - Run a timed Avahi callback
0c8d5f
+ */
0c8d5f
+
0c8d5f
+static void
0c8d5f
+timeout_cb (cupsd_timeout_t *cupsd_timeout, void *userdata)
0c8d5f
+{
0c8d5f
+  AvahiTimeout *timeout = userdata;
0c8d5f
+  (timeout->callback) (timeout, timeout->userdata);
0c8d5f
+}
0c8d5f
+
0c8d5f
+
0c8d5f
+/*
0c8d5f
+ * 'timeout_new' - Set a wakeup time
0c8d5f
+ */
0c8d5f
+
0c8d5f
+static AvahiTimeout *
0c8d5f
+timeout_new (const AvahiPoll *api,
0c8d5f
+	     const struct timeval *tv,
0c8d5f
+	     AvahiTimeoutCallback callback,
0c8d5f
+	     void *userdata)
0c8d5f
+{
0c8d5f
+  AvahiTimeout *timeout;
0c8d5f
+  AvahiCupsPoll *cups_poll = api->userdata;
0c8d5f
+
0c8d5f
+  timeout = malloc(sizeof(AvahiTimeout));
0c8d5f
+  if (timeout == NULL)
0c8d5f
+    return (NULL);
0c8d5f
+
0c8d5f
+  timeout->cups_poll = cups_poll;
0c8d5f
+  timeout->callback = callback;
0c8d5f
+  timeout->userdata = userdata;
0c8d5f
+  timeout->cupsd_timeout = cupsdAddTimeout (tv,
0c8d5f
+					    (cupsd_timeoutfunc_t)timeout_cb,
0c8d5f
+					    timeout);
0c8d5f
+  cupsArrayAdd (cups_poll->timeouts, timeout);
0c8d5f
+  return (timeout);
0c8d5f
+}
0c8d5f
+
0c8d5f
+
0c8d5f
+/*
0c8d5f
+ * 'timeout_update' - Update the expiration time for a timeout
0c8d5f
+ */
0c8d5f
+
0c8d5f
+static void
0c8d5f
+timeout_update (AvahiTimeout *timeout,
0c8d5f
+		const struct timeval *tv)
0c8d5f
+{
0c8d5f
+  cupsdUpdateTimeout (timeout->cupsd_timeout, tv);
0c8d5f
+}
0c8d5f
+
0c8d5f
+
0c8d5f
+/*
0c8d5f
+ * ' timeout_free' - Free a timeout
0c8d5f
+ */
0c8d5f
+
0c8d5f
+static void
0c8d5f
+timeout_free (AvahiTimeout *timeout)
0c8d5f
+{
0c8d5f
+  cupsArrayRemove (timeout->cups_poll->timeouts, timeout);
0c8d5f
+  cupsdRemoveTimeout (timeout->cupsd_timeout);
0c8d5f
+  free (timeout);
0c8d5f
+}
0c8d5f
+
0c8d5f
+
0c8d5f
+/*
0c8d5f
+ * 'compare_watched_fds' - Compare watched file descriptors for array sorting
0c8d5f
+ */
0c8d5f
+static int
0c8d5f
+compare_watched_fds(cupsd_watched_fd_t *p0,
0c8d5f
+		    cupsd_watched_fd_t *p1)
0c8d5f
+{
0c8d5f
+  /*
0c8d5f
+   * Compare by fd (no two elements have the same fd)
0c8d5f
+   */
0c8d5f
+
0c8d5f
+  if (p0->fd == p1->fd)
0c8d5f
+    return 0;
0c8d5f
+
0c8d5f
+  return (p0->fd < p1->fd ? -1 : 1);
0c8d5f
+}
0c8d5f
+
0c8d5f
+
0c8d5f
+/*
0c8d5f
+ * 'avahi_cups_poll_new' - Create a new Avahi main loop object for CUPS
0c8d5f
+ */
0c8d5f
+
0c8d5f
+AvahiCupsPoll *
0c8d5f
+avahi_cups_poll_new (void)
0c8d5f
+{
0c8d5f
+  AvahiCupsPoll *cups_poll = malloc(sizeof(AvahiCupsPoll));
0c8d5f
+  if (cups_poll == NULL)
0c8d5f
+    return (NULL);
0c8d5f
+
0c8d5f
+  cups_poll->watched_fds = cupsArrayNew ((cups_array_func_t)compare_watched_fds,
0c8d5f
+					 NULL);
0c8d5f
+  cups_poll->timeouts = cupsArrayNew (NULL, NULL);
0c8d5f
+
0c8d5f
+  cups_poll->api.userdata = cups_poll;
0c8d5f
+  cups_poll->api.watch_new = watch_new;
0c8d5f
+  cups_poll->api.watch_free = watch_free;
0c8d5f
+  cups_poll->api.watch_update = watch_update;
0c8d5f
+  cups_poll->api.watch_get_events = watch_get_events;
0c8d5f
+
0c8d5f
+  cups_poll->api.timeout_new = timeout_new;
0c8d5f
+  cups_poll->api.timeout_update = timeout_update;
0c8d5f
+  cups_poll->api.timeout_free = timeout_free;
0c8d5f
+
0c8d5f
+  return (cups_poll);
0c8d5f
+}
0c8d5f
+
0c8d5f
+
0c8d5f
+/*
0c8d5f
+ * 'avahi_cups_poll_free' - Free an Avahi main loop object for CUPS
0c8d5f
+ */
0c8d5f
+void
0c8d5f
+avahi_cups_poll_free (AvahiCupsPoll *cups_poll)
0c8d5f
+{
0c8d5f
+  cupsd_watched_fd_t *watched_fd;
0c8d5f
+
0c8d5f
+  for (watched_fd = (cupsd_watched_fd_t*)cupsArrayFirst(cups_poll->watched_fds);
0c8d5f
+       watched_fd;
0c8d5f
+       watched_fd = (cupsd_watched_fd_t*)cupsArrayNext(cups_poll->watched_fds))
0c8d5f
+    cupsArrayClear (watched_fd->watches);
0c8d5f
+
0c8d5f
+  cupsArrayClear (cups_poll->watched_fds);
0c8d5f
+  cupsArrayClear (cups_poll->timeouts);
0c8d5f
+}
0c8d5f
+
0c8d5f
+
0c8d5f
+/*
0c8d5f
+ * 'avahi_cups_poll_get' - Get the abstract poll API structure
0c8d5f
+ */
0c8d5f
+
0c8d5f
+const AvahiPoll *
0c8d5f
+avahi_cups_poll_get (AvahiCupsPoll *cups_poll)
0c8d5f
+{
0c8d5f
+  return (&cups_poll->api);
0c8d5f
+}
0c8d5f
+
0c8d5f
+
0c8d5f
+#endif /* HAVE_AVAHI ... from top of file */
0c8d5f
+
0c8d5f
+/*
0c8d5f
+ * End of "$Id$".
0c8d5f
+ */
0fed0d
diff -up cups-1.7rc1/scheduler/avahi.h.avahi-no-threaded cups-1.7rc1/scheduler/avahi.h
0fed0d
--- cups-1.7rc1/scheduler/avahi.h.avahi-no-threaded	2013-07-22 11:47:27.599101572 +0100
0fed0d
+++ cups-1.7rc1/scheduler/avahi.h	2013-07-22 11:47:27.599101572 +0100
0c8d5f
@@ -0,0 +1,69 @@
0c8d5f
+/*
0c8d5f
+ * "$Id$"
0c8d5f
+ *
0c8d5f
+ *   Avahi poll implementation for the CUPS scheduler.
0c8d5f
+ *
0c8d5f
+ *   Copyright (C) 2010, 2011 Red Hat, Inc.
0c8d5f
+ *   Authors:
0c8d5f
+ *    Tim Waugh <twaugh@redhat.com>
0c8d5f
+ *
0c8d5f
+ *   Redistribution and use in source and binary forms, with or without
0c8d5f
+ *   modification, are permitted provided that the following conditions
0c8d5f
+ *   are met:
0c8d5f
+ *
0c8d5f
+ *   Redistributions of source code must retain the above copyright
0c8d5f
+ *   notice, this list of conditions and the following disclaimer.
0c8d5f
+ *
0c8d5f
+ *   Redistributions in binary form must reproduce the above copyright
0c8d5f
+ *   notice, this list of conditions and the following disclaimer in the
0c8d5f
+ *   documentation and/or other materials provided with the distribution.
0c8d5f
+ *
0c8d5f
+ *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
0c8d5f
+ *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
0c8d5f
+ *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
0c8d5f
+ *   FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
0c8d5f
+ *   COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
0c8d5f
+ *   INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
0c8d5f
+ *   (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
0c8d5f
+ *   SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
0c8d5f
+ *   HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
0c8d5f
+ *   STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
0c8d5f
+ *   ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
0c8d5f
+ *   OF THE POSSIBILITY OF SUCH DAMAGE.
0c8d5f
+ */
0c8d5f
+
0c8d5f
+#include <config.h>
0c8d5f
+
0c8d5f
+#ifdef HAVE_AVAHI
0c8d5f
+#  include <avahi-client client.h="">
0c8d5f
+#  include <avahi-client publish.h="">
0c8d5f
+#endif /* HAVE_AVAHI */
0c8d5f
+
0c8d5f
+#ifdef HAVE_AUTHORIZATION_H
0c8d5f
+#  include <security authorization.h="">
0c8d5f
+#endif /* HAVE_AUTHORIZATION_H */
0c8d5f
+
0c8d5f
+
0c8d5f
+#ifdef HAVE_AVAHI
0c8d5f
+typedef struct
0c8d5f
+{
0c8d5f
+    AvahiPoll api;
0c8d5f
+    cups_array_t *watched_fds;
0c8d5f
+    cups_array_t *timeouts;
0c8d5f
+} AvahiCupsPoll;
0c8d5f
+#endif /* HAVE_AVAHI */
0c8d5f
+
0c8d5f
+/*
0c8d5f
+ * Prototypes...
0c8d5f
+ */
0c8d5f
+
0c8d5f
+#ifdef HAVE_AVAHI
0c8d5f
+extern AvahiCupsPoll *	avahi_cups_poll_new(void);
0c8d5f
+extern void		avahi_cups_poll_free(AvahiCupsPoll *cups_poll);
0c8d5f
+extern const AvahiPoll *avahi_cups_poll_get(AvahiCupsPoll *cups_poll);
0c8d5f
+#endif /* HAVE_AVAHI */
0c8d5f
+
0c8d5f
+
0c8d5f
+/*
0c8d5f
+ * End of "$Id$".
0c8d5f
+ */
0fed0d
diff -up cups-1.7rc1/scheduler/cupsd.h.avahi-no-threaded cups-1.7rc1/scheduler/cupsd.h
0fed0d
--- cups-1.7rc1/scheduler/cupsd.h.avahi-no-threaded	2013-05-29 12:51:34.000000000 +0100
0fed0d
+++ cups-1.7rc1/scheduler/cupsd.h	2013-07-22 11:47:31.319118984 +0100
0c8d5f
@@ -119,6 +119,7 @@ extern const char *cups_hstrerror(int);
0c8d5f
 #include "colorman.h"
0c8d5f
 #include "conf.h"
0c8d5f
 #include "banners.h"
0c8d5f
+#include "avahi.h"
0c8d5f
 #include "dirsvc.h"
0c8d5f
 #include "network.h"
0c8d5f
 #include "subscriptions.h"
0c8d5f
@@ -139,6 +140,15 @@ extern const char *cups_hstrerror(int);
0c8d5f
 
0c8d5f
 typedef void (*cupsd_selfunc_t)(void *data);
0c8d5f
 
0c8d5f
+#ifdef HAVE_AVAHI
0c8d5f
+/*
0c8d5f
+ * Timeout callback function type...
0c8d5f
+ */
0c8d5f
+
0c8d5f
+typedef struct _cupsd_timeout_s cupsd_timeout_t;
0c8d5f
+typedef void (*cupsd_timeoutfunc_t)(cupsd_timeout_t *timeout, void *data);
0c8d5f
+#endif /* HAVE_AVAHI */
0c8d5f
+
0c8d5f
 
0c8d5f
 /*
0c8d5f
  * Globals...
0c8d5f
@@ -165,6 +175,11 @@ VAR int			Launchd		VALUE(0);
0c8d5f
 					/* Running from launchd */
0c8d5f
 #endif /* HAVE_LAUNCH_H */
0c8d5f
 
0c8d5f
+#ifdef HAVE_AVAHI
0c8d5f
+VAR cups_array_t *Timeouts;		/* Timed callbacks for main loop */
0c8d5f
+#endif /* HAVE_AVAHI */
0c8d5f
+
0c8d5f
+
0c8d5f
 
0c8d5f
 /*
0c8d5f
  * Prototypes...
0fed0d
@@ -229,6 +244,17 @@ extern void		cupsdStopSelect(void);
0fed0d
 extern void		cupsdStartServer(void);
0fed0d
 extern void		cupsdStopServer(void);
0fed0d
 
0fed0d
+#ifdef HAVE_AVAHI
0fed0d
+extern void     cupsdInitTimeouts(void);
0fed0d
+extern cupsd_timeout_t *cupsdAddTimeout (const struct timeval *tv,
0fed0d
+					 cupsd_timeoutfunc_t cb,
0fed0d
+					 void *data);
0fed0d
+extern cupsd_timeout_t *cupsdNextTimeout (long *delay);
0fed0d
+extern void     cupsdRunTimeout (cupsd_timeout_t *timeout);
0fed0d
+extern void     cupsdUpdateTimeout (cupsd_timeout_t *timeout,
0fed0d
+				    const struct timeval *tv);
0fed0d
+extern void     cupsdRemoveTimeout (cupsd_timeout_t *timeout);
0fed0d
+#endif /* HAVE_AVAHI */
0fed0d
 
0fed0d
 /*
0fed0d
  * End of "$Id: cupsd.h 10996 2013-05-29 11:51:34Z msweet $".
0fed0d
diff -up cups-1.7rc1/scheduler/dirsvc.c.avahi-no-threaded cups-1.7rc1/scheduler/dirsvc.c
0fed0d
--- cups-1.7rc1/scheduler/dirsvc.c.avahi-no-threaded	2013-05-29 12:51:34.000000000 +0100
0fed0d
+++ cups-1.7rc1/scheduler/dirsvc.c	2013-07-22 11:47:27.600101576 +0100
0c8d5f
@@ -212,7 +212,7 @@ cupsdStartBrowsing(void)
0c8d5f
     }
0c8d5f
 
0c8d5f
 #  else /* HAVE_AVAHI */
0c8d5f
-    if ((DNSSDMaster = avahi_threaded_poll_new()) == NULL)
0c8d5f
+    if ((DNSSDMaster = avahi_cups_poll_new()) == NULL)
0c8d5f
     {
0c8d5f
       cupsdLogMessage(CUPSD_LOG_ERROR, "Unable to create DNS-SD thread.");
0c8d5f
 
0c8d5f
@@ -223,7 +223,7 @@ cupsdStartBrowsing(void)
0c8d5f
     {
0c8d5f
       int error;			/* Error code, if any */
0c8d5f
 
0c8d5f
-      DNSSDClient = avahi_client_new(avahi_threaded_poll_get(DNSSDMaster), 0,
0c8d5f
+      DNSSDClient = avahi_client_new(avahi_cups_poll_get(DNSSDMaster), 0,
0c8d5f
                                      NULL, NULL, &error);
0c8d5f
 
0c8d5f
       if (DNSSDClient == NULL)
0c8d5f
@@ -235,11 +235,9 @@ cupsdStartBrowsing(void)
0c8d5f
         if (FatalErrors & CUPSD_FATAL_BROWSE)
0c8d5f
 	  cupsdEndProcess(getpid(), 0);
0c8d5f
 
0c8d5f
-        avahi_threaded_poll_free(DNSSDMaster);
0c8d5f
+        avahi_cups_poll_free(DNSSDMaster);
0c8d5f
         DNSSDMaster = NULL;
0c8d5f
       }
0c8d5f
-      else
0c8d5f
-	avahi_threaded_poll_start(DNSSDMaster);
0c8d5f
     }
0c8d5f
 #  endif /* HAVE_DNSSD */
0c8d5f
 
0c8d5f
@@ -746,9 +744,7 @@ dnssdDeregisterInstance(
0c8d5f
   DNSServiceRefDeallocate(*srv);
0c8d5f
 
0c8d5f
 #  else /* HAVE_AVAHI */
0c8d5f
-  avahi_threaded_poll_lock(DNSSDMaster);
0c8d5f
   avahi_entry_group_free(*srv);
0c8d5f
-  avahi_threaded_poll_unlock(DNSSDMaster);
0c8d5f
 #  endif /* HAVE_DNSSD */
0c8d5f
 
0c8d5f
   *srv = NULL;
0c8d5f
@@ -1043,14 +1039,10 @@ dnssdRegisterInstance(
0c8d5f
   (void)commit;
0c8d5f
 
0c8d5f
 #  else /* HAVE_AVAHI */
0c8d5f
-  avahi_threaded_poll_lock(DNSSDMaster);
0c8d5f
-
0c8d5f
   if (!*srv)
0c8d5f
     *srv = avahi_entry_group_new(DNSSDClient, dnssdRegisterCallback, NULL);
0c8d5f
   if (!*srv)
0c8d5f
   {
0c8d5f
-    avahi_threaded_poll_unlock(DNSSDMaster);
0c8d5f
-
0c8d5f
     cupsdLogMessage(CUPSD_LOG_WARN, "DNS-SD registration of \"%s\" failed: %s",
0c8d5f
                     name, dnssdErrorString(avahi_client_errno(DNSSDClient)));
0c8d5f
     return (0);
0c8d5f
@@ -1165,8 +1157,6 @@ dnssdRegisterInstance(
0c8d5f
       cupsdLogMessage(CUPSD_LOG_DEBUG, "DNS-SD commit of \"%s\" failed.",
0c8d5f
                       name);
0c8d5f
   }
0c8d5f
-
0c8d5f
-  avahi_threaded_poll_unlock(DNSSDMaster);
0c8d5f
 #  endif /* HAVE_DNSSD */
0c8d5f
 
0c8d5f
   if (error)
d2f944
@@ -1338,7 +1328,7 @@ dnssdStop(void)
0c8d5f
   avahi_client_free(DNSSDClient);
0c8d5f
   DNSSDClient = NULL;
0c8d5f
 
0c8d5f
-  avahi_threaded_poll_free(DNSSDMaster);
0c8d5f
+  avahi_cups_poll_free(DNSSDMaster);
0c8d5f
   DNSSDMaster = NULL;
0c8d5f
 #  endif /* HAVE_DNSSD */
0c8d5f
 
0fed0d
diff -up cups-1.7rc1/scheduler/dirsvc.h.avahi-no-threaded cups-1.7rc1/scheduler/dirsvc.h
0fed0d
--- cups-1.7rc1/scheduler/dirsvc.h.avahi-no-threaded	2013-05-29 12:51:34.000000000 +0100
0fed0d
+++ cups-1.7rc1/scheduler/dirsvc.h	2013-07-22 11:47:27.600101576 +0100
0c8d5f
@@ -51,7 +51,7 @@ VAR cups_array_t	*DNSSDPrinters	VALUE(NU
0c8d5f
 VAR DNSServiceRef	DNSSDMaster	VALUE(NULL);
0c8d5f
 					/* Master DNS-SD service reference */
0c8d5f
 #  else /* HAVE_AVAHI */
0c8d5f
-VAR AvahiThreadedPoll	*DNSSDMaster	VALUE(NULL);
0c8d5f
+VAR AvahiCupsPoll	*DNSSDMaster	VALUE(NULL);
0c8d5f
 					/* Master polling interface for Avahi */
0c8d5f
 VAR AvahiClient		*DNSSDClient	VALUE(NULL);
0c8d5f
 					/* Client information */
0fed0d
diff -up cups-1.7rc1/scheduler/main.c.avahi-no-threaded cups-1.7rc1/scheduler/main.c
0fed0d
--- cups-1.7rc1/scheduler/main.c.avahi-no-threaded	2013-07-22 11:47:27.578101474 +0100
0fed0d
+++ cups-1.7rc1/scheduler/main.c	2013-07-22 11:47:27.601101581 +0100
d2f944
@@ -155,6 +155,10 @@ main(int  argc,				/* I - Number of comm
0c8d5f
   int			launchd_idle_exit;
0c8d5f
 					/* Idle exit on select timeout? */
0c8d5f
 #endif	/* HAVE_LAUNCHD */
0c8d5f
+#ifdef HAVE_AVAHI
0c8d5f
+  cupsd_timeout_t	*tmo;		/* Next scheduled timed callback */
0c8d5f
+  long			tmo_delay;	/* Time before it must be called */
0c8d5f
+#endif /* HAVE_AVAHI */
0c8d5f
 
0c8d5f
 
0c8d5f
 #ifdef HAVE_GETEUID
d2f944
@@ -596,6 +600,14 @@ main(int  argc,				/* I - Number of comm
0c8d5f
 
0c8d5f
   httpInitialize();
0c8d5f
 
0c8d5f
+#ifdef HAVE_AVAHI
0c8d5f
+ /*
0c8d5f
+  * Initialize timed callback structures.
0c8d5f
+  */
0c8d5f
+
0c8d5f
+  cupsdInitTimeouts();
0c8d5f
+#endif /* HAVE_AVAHI */
0c8d5f
+
0c8d5f
   cupsdStartServer();
0c8d5f
 
0c8d5f
  /*
d2f944
@@ -917,6 +929,16 @@ main(int  argc,				/* I - Number of comm
0c8d5f
     }
0c8d5f
 #endif /* __APPLE__ */
0c8d5f
 
0c8d5f
+#ifdef HAVE_AVAHI
0c8d5f
+   /*
0c8d5f
+    * If a timed callback is due, run it.
0c8d5f
+    */
0c8d5f
+
0c8d5f
+    tmo = cupsdNextTimeout (&tmo_delay);
0c8d5f
+    if (tmo && tmo_delay == 0)
0c8d5f
+      cupsdRunTimeout (tmo);
0c8d5f
+#endif /* HAVE_AVAHI */
0c8d5f
+
0c8d5f
 #ifndef __APPLE__
0c8d5f
    /*
0c8d5f
     * Update the network interfaces once a minute...
d2f944
@@ -1897,6 +1919,10 @@ select_timeout(int fds)			/* I - Number
0c8d5f
   cupsd_job_t		*job;		/* Job information */
0c8d5f
   cupsd_subscription_t	*sub;		/* Subscription information */
0c8d5f
   const char		*why;		/* Debugging aid */
0c8d5f
+#ifdef HAVE_AVAHI
0c8d5f
+  cupsd_timeout_t	*tmo;		/* Timed callback */
0c8d5f
+  long			tmo_delay;	/* Seconds before calling it */
0c8d5f
+#endif /* HAVE_AVAHI */
0c8d5f
 
0c8d5f
 
0c8d5f
   cupsdLogMessage(CUPSD_LOG_DEBUG2, "select_timeout: JobHistoryUpdate=%ld",
d2f944
@@ -1942,6 +1968,19 @@ select_timeout(int fds)			/* I - Number
0c8d5f
   }
0c8d5f
 #endif /* __APPLE__ */
0c8d5f
 
0c8d5f
+#ifdef HAVE_AVAHI
0c8d5f
+ /*
0c8d5f
+  * See if there are any scheduled timed callbacks to run.
0c8d5f
+  */
0c8d5f
+
0c8d5f
+  if ((tmo = cupsdNextTimeout(&tmo_delay)) != NULL &&
0c8d5f
+      (now + tmo_delay) < timeout)
0c8d5f
+  {
0c8d5f
+    timeout = tmo_delay;
0c8d5f
+    why = "run a timed callback";
0c8d5f
+  }
0c8d5f
+#endif /* HAVE_AVAHI */
0c8d5f
+
0c8d5f
  /*
0c8d5f
   * Check whether we are accepting new connections...
0c8d5f
   */
0fed0d
diff -up cups-1.7rc1/scheduler/Makefile.avahi-no-threaded cups-1.7rc1/scheduler/Makefile
0fed0d
--- cups-1.7rc1/scheduler/Makefile.avahi-no-threaded	2013-07-22 11:47:27.578101474 +0100
0fed0d
+++ cups-1.7rc1/scheduler/Makefile	2013-07-22 11:47:27.601101581 +0100
0c8d5f
@@ -17,6 +17,7 @@ include ../Makedefs
0c8d5f
 
0c8d5f
 CUPSDOBJS =	\
0c8d5f
 		auth.o \
0c8d5f
+		avahi.o \
0c8d5f
 		banners.o \
0c8d5f
 		cert.o \
0c8d5f
 		classes.o \
0c8d5f
@@ -41,6 +42,7 @@ CUPSDOBJS =	\
0c8d5f
 		statbuf.o \
0c8d5f
 		subscriptions.o \
0c8d5f
 		sysman.o \
0c8d5f
+		timeout.o \
0c8d5f
 		tls.o
0c8d5f
 LIBOBJS =	\
0c8d5f
 		filter.o \
0fed0d
diff -up cups-1.7rc1/scheduler/timeout.c.avahi-no-threaded cups-1.7rc1/scheduler/timeout.c
0fed0d
--- cups-1.7rc1/scheduler/timeout.c.avahi-no-threaded	2013-07-22 11:47:27.601101581 +0100
0fed0d
+++ cups-1.7rc1/scheduler/timeout.c	2013-07-22 11:47:27.601101581 +0100
0c8d5f
@@ -0,0 +1,235 @@
0c8d5f
+/*
0c8d5f
+ * "$Id$"
0c8d5f
+ *
0c8d5f
+ *   Timeout functions for the Common UNIX Printing System (CUPS).
0c8d5f
+ *
0c8d5f
+ *   Copyright (C) 2010, 2011 Red Hat, Inc.
0c8d5f
+ *   Authors:
0c8d5f
+ *     Tim Waugh <twaugh@redhat.com>
0c8d5f
+ *
0c8d5f
+ *   Redistribution and use in source and binary forms, with or without
0c8d5f
+ *   modification, are permitted provided that the following conditions
0c8d5f
+ *   are met:
0c8d5f
+ *
0c8d5f
+ *   Redistributions of source code must retain the above copyright
0c8d5f
+ *   notice, this list of conditions and the following disclaimer.
0c8d5f
+ *
0c8d5f
+ *   Redistributions in binary form must reproduce the above copyright
0c8d5f
+ *   notice, this list of conditions and the following disclaimer in the
0c8d5f
+ *   documentation and/or other materials provided with the distribution.
0c8d5f
+ *
0c8d5f
+ *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
0c8d5f
+ *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
0c8d5f
+ *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
0c8d5f
+ *   FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
0c8d5f
+ *   COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
0c8d5f
+ *   INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
0c8d5f
+ *   (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
0c8d5f
+ *   SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
0c8d5f
+ *   HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
0c8d5f
+ *   STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
0c8d5f
+ *   ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
0c8d5f
+ *   OF THE POSSIBILITY OF SUCH DAMAGE.
0c8d5f
+ *
0c8d5f
+ * Contents:
0c8d5f
+ *
0c8d5f
+ *   cupsdInitTimeouts()  - Initialise timeout structure.
0c8d5f
+ *   cupsdAddTimeout()    - Add a timed callback.
0c8d5f
+ *   cupsdNextTimeout()   - Find the next enabled timed callback.
0c8d5f
+ *   cupsdUpdateTimeout() - Adjust the time of a timed callback or disable it.
0c8d5f
+ *   cupsdRemoveTimeout() - Discard a timed callback.
0c8d5f
+ *   compare_timeouts()   - Compare timed callbacks for array sorting.
0c8d5f
+ */
0c8d5f
+
0c8d5f
+#include <config.h>
0c8d5f
+
0c8d5f
+#ifdef HAVE_AVAHI /* Applies to entire file... */
0c8d5f
+
0c8d5f
+/*
0c8d5f
+ * Include necessary headers...
0c8d5f
+ */
0c8d5f
+
0c8d5f
+#include "cupsd.h"
0c8d5f
+
0c8d5f
+#if defined(HAVE_MALLOC_H) && defined(HAVE_MALLINFO)
0c8d5f
+#  include <malloc.h>
0c8d5f
+#endif /* HAVE_MALLOC_H && HAVE_MALLINFO */
0c8d5f
+
0c8d5f
+#ifdef HAVE_AVAHI
0c8d5f
+#  include <avahi-common timeval.h="">
0c8d5f
+#endif /* HAVE_AVAHI */
0c8d5f
+
0c8d5f
+
0c8d5f
+struct _cupsd_timeout_s
0c8d5f
+{
0c8d5f
+  struct timeval when;
0c8d5f
+  int enabled;
0c8d5f
+  cupsd_timeoutfunc_t callback;
0c8d5f
+  void *data;
0c8d5f
+};
0c8d5f
+
0c8d5f
+/*
0c8d5f
+ * Local functions...
0c8d5f
+ */
0c8d5f
+
0c8d5f
+/*
0c8d5f
+ * 'compare_timeouts()' - Compare timed callbacks for array sorting.
0c8d5f
+ */
0c8d5f
+
0c8d5f
+static int
0c8d5f
+compare_addrs (void *p0, void *p1)
0c8d5f
+{
0c8d5f
+  if (p0 == p1)
0c8d5f
+    return (0);
0c8d5f
+  if (p0 < p1)
0c8d5f
+    return (-1);
0c8d5f
+  return (1);
0c8d5f
+}
0c8d5f
+
0c8d5f
+static int
0c8d5f
+compare_timeouts (cupsd_timeout_t *p0, cupsd_timeout_t *p1)
0c8d5f
+{
0c8d5f
+  int addrsdiff = compare_addrs (p0, p1);
0c8d5f
+  int tvdiff;
0c8d5f
+
0c8d5f
+  if (addrsdiff == 0)
0c8d5f
+    return (0);
0c8d5f
+
0c8d5f
+  if (!p0->enabled || !p1->enabled)
0c8d5f
+  {
0c8d5f
+    if (!p0->enabled && !p1->enabled)
0c8d5f
+      return (addrsdiff);
0c8d5f
+
0c8d5f
+    return (p0->enabled ? -1 : 1);
0c8d5f
+  }
0c8d5f
+
0c8d5f
+  tvdiff = avahi_timeval_compare (&p0->when, &p1->when);
0c8d5f
+  if (tvdiff != 0)
0c8d5f
+    return (tvdiff);
0c8d5f
+
0c8d5f
+  return (addrsdiff);
0c8d5f
+}
0c8d5f
+
0c8d5f
+
0c8d5f
+/*
0c8d5f
+ * 'cupsdInitTimeouts()' - Initialise timeout structures.
0c8d5f
+ */
0c8d5f
+
0c8d5f
+void
0c8d5f
+cupsdInitTimeouts(void)
0c8d5f
+{
0c8d5f
+  Timeouts = cupsArrayNew ((cups_array_func_t)compare_timeouts, NULL);
0c8d5f
+}
0c8d5f
+
0c8d5f
+
0c8d5f
+/*
0c8d5f
+ * 'cupsdAddTimeout()' - Add a timed callback.
0c8d5f
+ */
0c8d5f
+
0c8d5f
+cupsd_timeout_t *				/* O - Timeout handle */
0c8d5f
+cupsdAddTimeout(const struct timeval *tv,	/* I - Absolute time */
0c8d5f
+		cupsd_timeoutfunc_t cb,		/* I - Callback function */
0c8d5f
+		void *data)			/* I - User data */
0c8d5f
+{
0c8d5f
+  cupsd_timeout_t *timeout;
0c8d5f
+
0c8d5f
+  timeout = malloc (sizeof(cupsd_timeout_t));
0c8d5f
+  if (timeout != NULL)
0c8d5f
+  {
0c8d5f
+    timeout->enabled = (tv != NULL);
0c8d5f
+    if (tv)
0c8d5f
+    {
0c8d5f
+      timeout->when.tv_sec = tv->tv_sec;
0c8d5f
+      timeout->when.tv_usec = tv->tv_usec;
0c8d5f
+    }
0c8d5f
+
0c8d5f
+    timeout->callback = cb;
0c8d5f
+    timeout->data = data;
0c8d5f
+    cupsArrayAdd (Timeouts, timeout);
0c8d5f
+  }
0c8d5f
+
0c8d5f
+  return timeout;
0c8d5f
+}
0c8d5f
+
0c8d5f
+
0c8d5f
+/*
0c8d5f
+ * 'cupsdNextTimeout()' - Find the next enabled timed callback.
0c8d5f
+ */
0c8d5f
+
0c8d5f
+cupsd_timeout_t *		/* O - Next enabled timeout or NULL */
0c8d5f
+cupsdNextTimeout(long *delay)	/* O - Seconds before scheduled */
0c8d5f
+{
0c8d5f
+  cupsd_timeout_t *first = cupsArrayFirst (Timeouts);
0c8d5f
+  struct timeval curtime;
0c8d5f
+
0c8d5f
+  if (first && !first->enabled)
0c8d5f
+    first = NULL;
0c8d5f
+
0c8d5f
+  if (first && delay)
0c8d5f
+  {
0c8d5f
+    gettimeofday (&curtime, NULL);
0c8d5f
+    if (avahi_timeval_compare (&curtime, &first->when) > 0)
0c8d5f
+    {
0c8d5f
+      *delay = 0;
0c8d5f
+    } else {
0c8d5f
+      *delay = 1 + first->when.tv_sec - curtime.tv_sec;
0c8d5f
+      if (first->when.tv_usec < curtime.tv_usec)
0c8d5f
+	(*delay)--;
0c8d5f
+    }
0c8d5f
+  }
0c8d5f
+
0c8d5f
+  return (first);
0c8d5f
+}
0c8d5f
+
0c8d5f
+
0c8d5f
+/*
0c8d5f
+ * 'cupsdRunTimeout()' - Run a timed callback.
0c8d5f
+ */
0c8d5f
+
0c8d5f
+void
0c8d5f
+cupsdRunTimeout(cupsd_timeout_t *timeout)	/* I - Timeout */
0c8d5f
+{
0c8d5f
+  if (!timeout)
0c8d5f
+    return;
0c8d5f
+  timeout->enabled = 0;
0c8d5f
+  if (!timeout->callback)
0c8d5f
+    return;
0c8d5f
+  timeout->callback (timeout, timeout->data);
0c8d5f
+}
0c8d5f
+
0c8d5f
+/*
0c8d5f
+ * 'cupsdUpdateTimeout()' - Adjust the time of a timed callback or disable it.
0c8d5f
+ */
0c8d5f
+
0c8d5f
+void
0c8d5f
+cupsdUpdateTimeout(cupsd_timeout_t *timeout,	/* I - Timeout */
0c8d5f
+		   const struct timeval *tv)	/* I - Absolute time or NULL */
0c8d5f
+{
0c8d5f
+  cupsArrayRemove (Timeouts, timeout);
0c8d5f
+  timeout->enabled = (tv != NULL);
0c8d5f
+  if (tv)
0c8d5f
+  {
0c8d5f
+    timeout->when.tv_sec = tv->tv_sec;
0c8d5f
+    timeout->when.tv_usec = tv->tv_usec;
0c8d5f
+  }
0c8d5f
+  cupsArrayAdd (Timeouts, timeout);
0c8d5f
+}
0c8d5f
+
0c8d5f
+
0c8d5f
+/*
0c8d5f
+ * 'cupsdRemoveTimeout()' - Discard a timed callback.
0c8d5f
+ */
0c8d5f
+
0c8d5f
+void
0c8d5f
+cupsdRemoveTimeout(cupsd_timeout_t *timeout)	/* I - Timeout */
0c8d5f
+{
0c8d5f
+  cupsArrayRemove (Timeouts, timeout);
0c8d5f
+  free (timeout);
0c8d5f
+}
0c8d5f
+
0c8d5f
+
0c8d5f
+#endif /* HAVE_AVAHI ... from top of file */
0c8d5f
+
0c8d5f
+/*
0c8d5f
+ * End of "$Id$".
0c8d5f
+ */