bdf6254
diff -up cups-2.2.7/scheduler/avahi.c.avahi-no-threaded cups-2.2.7/scheduler/avahi.c
bdf6254
--- cups-2.2.7/scheduler/avahi.c.avahi-no-threaded	2018-04-03 14:27:17.067410826 +0200
bdf6254
+++ cups-2.2.7/scheduler/avahi.c	2018-04-03 14:27:17.067410826 +0200
0c8d5f5
@@ -0,0 +1,441 @@
0c8d5f5
+/*
0c8d5f5
+ * "$Id$"
0c8d5f5
+ *
0c8d5f5
+ *   Avahi poll implementation for the CUPS scheduler.
0c8d5f5
+ *
0c8d5f5
+ *   Copyright (C) 2010, 2011 Red Hat, Inc.
0c8d5f5
+ *   Authors:
0c8d5f5
+ *    Tim Waugh <twaugh@redhat.com>
0c8d5f5
+ *
0c8d5f5
+ *   Redistribution and use in source and binary forms, with or without
0c8d5f5
+ *   modification, are permitted provided that the following conditions
0c8d5f5
+ *   are met:
0c8d5f5
+ *
0c8d5f5
+ *   Redistributions of source code must retain the above copyright
0c8d5f5
+ *   notice, this list of conditions and the following disclaimer.
0c8d5f5
+ *
0c8d5f5
+ *   Redistributions in binary form must reproduce the above copyright
0c8d5f5
+ *   notice, this list of conditions and the following disclaimer in the
0c8d5f5
+ *   documentation and/or other materials provided with the distribution.
0c8d5f5
+ *
0c8d5f5
+ *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
0c8d5f5
+ *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
0c8d5f5
+ *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
0c8d5f5
+ *   FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
0c8d5f5
+ *   COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
0c8d5f5
+ *   INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
0c8d5f5
+ *   (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
0c8d5f5
+ *   SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
0c8d5f5
+ *   HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
0c8d5f5
+ *   STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
0c8d5f5
+ *   ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
0c8d5f5
+ *   OF THE POSSIBILITY OF SUCH DAMAGE.
0c8d5f5
+ *
0c8d5f5
+ * Contents:
0c8d5f5
+ *
0c8d5f5
+ *   watch_read_cb         - Read callback for file descriptor
0c8d5f5
+ *   watch_write_cb        - Write callback for file descriptor
0c8d5f5
+ *   watched_fd_add_select() - Call cupsdAddSelect() as needed
0c8d5f5
+ *   watch_new()           - Create a new file descriptor watch
0c8d5f5
+ *   watch_free()          - Free a file descriptor watch
0c8d5f5
+ *   watch_update()        - Update watched events for a file descriptor
0c8d5f5
+ *   watch_get_events()    - Get events that happened for a file descriptor
0c8d5f5
+ *   timeout_cb()          - Run a timed Avahi callback
0c8d5f5
+ *   timeout_new()         - Set a wakeup time
0c8d5f5
+ *   timeout_update()      - Update the expiration time for a timeout
0c8d5f5
+ *   timeout_free()        - Free a timeout
0c8d5f5
+ *   compare_watched_fds() - Compare watched file descriptors for array sorting
0c8d5f5
+ *   avahi_cups_poll_new() - Create a new Avahi main loop object for CUPS
0c8d5f5
+ *   avahi_cups_poll_free() - Free an Avahi main loop object for CUPS
0c8d5f5
+ *   avahi_cups_poll_get() - Get the abstract poll API structure
0c8d5f5
+ */
0c8d5f5
+
0c8d5f5
+#include <config.h>
0c8d5f5
+
0c8d5f5
+#ifdef HAVE_AVAHI /* Applies to entire file... */
0c8d5f5
+
0c8d5f5
+/*
0c8d5f5
+ * Include necessary headers...
0c8d5f5
+ */
0c8d5f5
+
0c8d5f5
+#include "cupsd.h"
0c8d5f5
+
0c8d5f5
+#if defined(HAVE_MALLOC_H) && defined(HAVE_MALLINFO)
0c8d5f5
+#  include <malloc.h>
0c8d5f5
+#endif /* HAVE_MALLOC_H && HAVE_MALLINFO */
0c8d5f5
+
0c8d5f5
+#ifdef HAVE_AVAHI
0c8d5f5
+#  include <avahi-common/timeval.h>
0c8d5f5
+#endif /* HAVE_AVAHI */
0c8d5f5
+
0c8d5f5
+
0c8d5f5
+typedef struct
0c8d5f5
+{
0c8d5f5
+  AvahiCupsPoll *cups_poll;
0c8d5f5
+
0c8d5f5
+  int fd;
0c8d5f5
+  AvahiWatchEvent occurred;
0c8d5f5
+  cups_array_t *watches;
0c8d5f5
+} cupsd_watched_fd_t;
0c8d5f5
+
0c8d5f5
+struct AvahiWatch
0c8d5f5
+{
0c8d5f5
+  cupsd_watched_fd_t *watched_fd;
0c8d5f5
+
0c8d5f5
+  AvahiWatchEvent events;
0c8d5f5
+  AvahiWatchCallback callback;
0c8d5f5
+  void *userdata;
0c8d5f5
+};
0c8d5f5
+
0c8d5f5
+struct AvahiTimeout
0c8d5f5
+{
0c8d5f5
+  AvahiCupsPoll *cups_poll;
0c8d5f5
+  AvahiTimeoutCallback callback;
0c8d5f5
+  void *userdata;
0c8d5f5
+  cupsd_timeout_t *cupsd_timeout;
0c8d5f5
+};
0c8d5f5
+
0c8d5f5
+/*
0c8d5f5
+ * Local functions...
0c8d5f5
+ */
0c8d5f5
+
0c8d5f5
+static AvahiWatch *	watch_new(const AvahiPoll *api,
0c8d5f5
+				  int fd,
0c8d5f5
+				  AvahiWatchEvent events,
0c8d5f5
+				  AvahiWatchCallback callback,
0c8d5f5
+				  void *userdata);
0c8d5f5
+static void		watch_free(AvahiWatch *watch);
0c8d5f5
+static void		watch_update(AvahiWatch *watch,
0c8d5f5
+				     AvahiWatchEvent events);
0c8d5f5
+static AvahiWatchEvent	watch_get_events(AvahiWatch *watch);
0c8d5f5
+
0c8d5f5
+
0c8d5f5
+/*
0c8d5f5
+ * 'watch_read_cb' - Read callback for file descriptor
0c8d5f5
+ */
0c8d5f5
+
0c8d5f5
+static void
0c8d5f5
+watch_read_cb (void *userdata)
0c8d5f5
+{
0c8d5f5
+  AvahiWatch *watch;
0c8d5f5
+  cupsd_watched_fd_t *watched_fd = userdata;
0c8d5f5
+  watched_fd->occurred |= AVAHI_WATCH_IN;
0c8d5f5
+  for (watch = (AvahiWatch *)cupsArrayFirst(watched_fd->watches);
0c8d5f5
+       watch;
0c8d5f5
+       watch = (AvahiWatch *)cupsArrayNext(watched_fd->watches))
0c8d5f5
+  {
0c8d5f5
+    if (watch->events & watched_fd->occurred)
0c8d5f5
+    {
0c8d5f5
+      (watch->callback) (watch, watched_fd->fd,
0c8d5f5
+			 AVAHI_WATCH_IN, watch->userdata);
0c8d5f5
+      watched_fd->occurred &= ~AVAHI_WATCH_IN;
0c8d5f5
+      break;
0c8d5f5
+    }
0c8d5f5
+  }
0c8d5f5
+}
0c8d5f5
+
0c8d5f5
+
0c8d5f5
+/*
0c8d5f5
+ * 'watch_write_cb' - Write callback for file descriptor
0c8d5f5
+ */
0c8d5f5
+
0c8d5f5
+static void
0c8d5f5
+watch_write_cb (void *userdata)
0c8d5f5
+{
0c8d5f5
+  AvahiWatch *watch;
0c8d5f5
+  cupsd_watched_fd_t *watched_fd = userdata;
0c8d5f5
+  watched_fd->occurred |= AVAHI_WATCH_OUT;
0c8d5f5
+  for (watch = (AvahiWatch *)cupsArrayFirst(watched_fd->watches);
0c8d5f5
+       watch;
0c8d5f5
+       watch = (AvahiWatch *)cupsArrayNext(watched_fd->watches))
0c8d5f5
+  {
0c8d5f5
+    if (watch->events & watched_fd->occurred)
0c8d5f5
+    {
0c8d5f5
+      (watch->callback) (watch, watched_fd->fd,
0c8d5f5
+			 AVAHI_WATCH_OUT, watch->userdata);
0c8d5f5
+      watched_fd->occurred &= ~AVAHI_WATCH_OUT;
0c8d5f5
+      break;
0c8d5f5
+    }
0c8d5f5
+  }
0c8d5f5
+}
0c8d5f5
+
0c8d5f5
+
0c8d5f5
+/*
0c8d5f5
+ * 'watched_fd_add_select' - Call cupsdAddSelect() as needed
0c8d5f5
+ */
0c8d5f5
+
0c8d5f5
+static int						/* O - Watches? */
0c8d5f5
+watched_fd_add_select (cupsd_watched_fd_t *watched_fd)
0c8d5f5
+{
0c8d5f5
+  AvahiWatch *watch;
0c8d5f5
+  cupsd_selfunc_t read_cb = NULL, write_cb = NULL;
0c8d5f5
+  int any_watches = 0;
0c8d5f5
+
0c8d5f5
+  for (watch = (AvahiWatch *)cupsArrayFirst(watched_fd->watches);
0c8d5f5
+       watch;
0c8d5f5
+       watch = (AvahiWatch *)cupsArrayNext(watched_fd->watches))
0c8d5f5
+  {
0c8d5f5
+    any_watches = 1;
0c8d5f5
+    if (watch->events & (AVAHI_WATCH_IN |
0c8d5f5
+			     AVAHI_WATCH_ERR |
0c8d5f5
+			     AVAHI_WATCH_HUP))
0c8d5f5
+    {
0c8d5f5
+      read_cb = (cupsd_selfunc_t)watch_read_cb;
0c8d5f5
+      if (write_cb != NULL)
0c8d5f5
+	break;
0c8d5f5
+    }
0c8d5f5
+
0c8d5f5
+    if (watch->events & AVAHI_WATCH_OUT)
0c8d5f5
+    {
0c8d5f5
+      write_cb = (cupsd_selfunc_t)watch_write_cb;
0c8d5f5
+      if (read_cb != NULL)
0c8d5f5
+	break;
0c8d5f5
+    }
0c8d5f5
+  }
0c8d5f5
+
0c8d5f5
+  if (read_cb || write_cb)
0c8d5f5
+    cupsdAddSelect (watched_fd->fd, read_cb, write_cb, watched_fd);
0c8d5f5
+  else
0c8d5f5
+    cupsdRemoveSelect (watched_fd->fd);
0c8d5f5
+
0c8d5f5
+  return (any_watches);
0c8d5f5
+}
0c8d5f5
+
0c8d5f5
+/*
0c8d5f5
+ * 'watch_new' - Create a new file descriptor watch
0c8d5f5
+ */
0c8d5f5
+
0c8d5f5
+static AvahiWatch *
0c8d5f5
+watch_new (const AvahiPoll *api,
0c8d5f5
+	   int fd,
0c8d5f5
+	   AvahiWatchEvent events,
0c8d5f5
+	   AvahiWatchCallback callback,
0c8d5f5
+	   void *userdata)
0c8d5f5
+{
0c8d5f5
+  cupsd_watched_fd_t key, *watched_fd;
0c8d5f5
+  AvahiCupsPoll *cups_poll = api->userdata;
0c8d5f5
+  AvahiWatch *watch = malloc(sizeof(AvahiWatch));
0c8d5f5
+  if (watch == NULL)
0c8d5f5
+    return (NULL);
0c8d5f5
+
0c8d5f5
+  watch->events = events;
0c8d5f5
+  watch->callback = callback;
0c8d5f5
+  watch->userdata = userdata;
0c8d5f5
+
0c8d5f5
+  key.fd = fd;
0c8d5f5
+  watched_fd = cupsArrayFind (cups_poll->watched_fds, &key);
0c8d5f5
+  if (watched_fd == NULL)
0c8d5f5
+  {
0c8d5f5
+    watched_fd = malloc(sizeof(cupsd_watched_fd_t));
0c8d5f5
+    if (watched_fd == NULL)
0c8d5f5
+    {
0c8d5f5
+      free (watch);
0c8d5f5
+      return (NULL);
0c8d5f5
+    }
0c8d5f5
+
0c8d5f5
+    watched_fd->fd = fd;
0c8d5f5
+    watched_fd->occurred = 0;
0c8d5f5
+    watched_fd->cups_poll = cups_poll;
0c8d5f5
+    watched_fd->watches = cupsArrayNew (NULL, NULL);
0c8d5f5
+    cupsArrayAdd (cups_poll->watched_fds, watched_fd);
0c8d5f5
+  }
0c8d5f5
+
0c8d5f5
+  watch->watched_fd = watched_fd;
0c8d5f5
+  cupsArrayAdd(watched_fd->watches, watch);
0c8d5f5
+  watched_fd_add_select (watched_fd);
0c8d5f5
+  return (watch);
0c8d5f5
+}
0c8d5f5
+
0c8d5f5
+
0c8d5f5
+/*
0c8d5f5
+ * 'watch_free' - Free a file descriptor watch
0c8d5f5
+ */
0c8d5f5
+
0c8d5f5
+static void
0c8d5f5
+watch_free (AvahiWatch *watch)
0c8d5f5
+{
0c8d5f5
+  cupsd_watched_fd_t *watched_fd = watch->watched_fd;
0c8d5f5
+  AvahiCupsPoll *cups_poll = watched_fd->cups_poll;
0c8d5f5
+
0c8d5f5
+  cupsArrayRemove (watched_fd->watches, watch);
0c8d5f5
+  free (watch);
0c8d5f5
+
0c8d5f5
+  if (!watched_fd_add_select (watched_fd))
0c8d5f5
+  {
0c8d5f5
+    /* No more watches */
0c8d5f5
+    cupsArrayRemove (cups_poll->watched_fds, watched_fd);
0c8d5f5
+    free (watched_fd);
0c8d5f5
+  }
0c8d5f5
+}
0c8d5f5
+
0c8d5f5
+
0c8d5f5
+/*
0c8d5f5
+ * 'watch_update' - Update watched events for a file descriptor
0c8d5f5
+ */
0c8d5f5
+
0c8d5f5
+static void
0c8d5f5
+watch_update (AvahiWatch *watch,
0c8d5f5
+	      AvahiWatchEvent events)
0c8d5f5
+{
0c8d5f5
+  watch->events = events;
0c8d5f5
+  watched_fd_add_select (watch->watched_fd);
0c8d5f5
+}
0c8d5f5
+
0c8d5f5
+
0c8d5f5
+/*
0c8d5f5
+ * 'watch_get_events' - Get events that happened for a file descriptor
0c8d5f5
+ */
0c8d5f5
+
0c8d5f5
+static AvahiWatchEvent
0c8d5f5
+watch_get_events (AvahiWatch *watch)
0c8d5f5
+{
0c8d5f5
+  return (watch->watched_fd->occurred);
0c8d5f5
+}
0c8d5f5
+
0c8d5f5
+
0c8d5f5
+/*
0c8d5f5
+ * 'timeout_cb()' - Run a timed Avahi callback
0c8d5f5
+ */
0c8d5f5
+
0c8d5f5
+static void
0c8d5f5
+timeout_cb (cupsd_timeout_t *cupsd_timeout, void *userdata)
0c8d5f5
+{
0c8d5f5
+  AvahiTimeout *timeout = userdata;
0c8d5f5
+  (timeout->callback) (timeout, timeout->userdata);
0c8d5f5
+}
0c8d5f5
+
0c8d5f5
+
0c8d5f5
+/*
0c8d5f5
+ * 'timeout_new' - Set a wakeup time
0c8d5f5
+ */
0c8d5f5
+
0c8d5f5
+static AvahiTimeout *
0c8d5f5
+timeout_new (const AvahiPoll *api,
0c8d5f5
+	     const struct timeval *tv,
0c8d5f5
+	     AvahiTimeoutCallback callback,
0c8d5f5
+	     void *userdata)
0c8d5f5
+{
0c8d5f5
+  AvahiTimeout *timeout;
0c8d5f5
+  AvahiCupsPoll *cups_poll = api->userdata;
0c8d5f5
+
0c8d5f5
+  timeout = malloc(sizeof(AvahiTimeout));
0c8d5f5
+  if (timeout == NULL)
0c8d5f5
+    return (NULL);
0c8d5f5
+
0c8d5f5
+  timeout->cups_poll = cups_poll;
0c8d5f5
+  timeout->callback = callback;
0c8d5f5
+  timeout->userdata = userdata;
0c8d5f5
+  timeout->cupsd_timeout = cupsdAddTimeout (tv,
0c8d5f5
+					    (cupsd_timeoutfunc_t)timeout_cb,
0c8d5f5
+					    timeout);
0c8d5f5
+  cupsArrayAdd (cups_poll->timeouts, timeout);
0c8d5f5
+  return (timeout);
0c8d5f5
+}
0c8d5f5
+
0c8d5f5
+
0c8d5f5
+/*
0c8d5f5
+ * 'timeout_update' - Update the expiration time for a timeout
0c8d5f5
+ */
0c8d5f5
+
0c8d5f5
+static void
0c8d5f5
+timeout_update (AvahiTimeout *timeout,
0c8d5f5
+		const struct timeval *tv)
0c8d5f5
+{
0c8d5f5
+  cupsdUpdateTimeout (timeout->cupsd_timeout, tv);
0c8d5f5
+}
0c8d5f5
+
0c8d5f5
+
0c8d5f5
+/*
0c8d5f5
+ * ' timeout_free' - Free a timeout
0c8d5f5
+ */
0c8d5f5
+
0c8d5f5
+static void
0c8d5f5
+timeout_free (AvahiTimeout *timeout)
0c8d5f5
+{
0c8d5f5
+  cupsArrayRemove (timeout->cups_poll->timeouts, timeout);
0c8d5f5
+  cupsdRemoveTimeout (timeout->cupsd_timeout);
0c8d5f5
+  free (timeout);
0c8d5f5
+}
0c8d5f5
+
0c8d5f5
+
0c8d5f5
+/*
0c8d5f5
+ * 'compare_watched_fds' - Compare watched file descriptors for array sorting
0c8d5f5
+ */
0c8d5f5
+static int
0c8d5f5
+compare_watched_fds(cupsd_watched_fd_t *p0,
0c8d5f5
+		    cupsd_watched_fd_t *p1)
0c8d5f5
+{
0c8d5f5
+  /*
0c8d5f5
+   * Compare by fd (no two elements have the same fd)
0c8d5f5
+   */
0c8d5f5
+
0c8d5f5
+  if (p0->fd == p1->fd)
0c8d5f5
+    return 0;
0c8d5f5
+
0c8d5f5
+  return (p0->fd < p1->fd ? -1 : 1);
0c8d5f5
+}
0c8d5f5
+
0c8d5f5
+
0c8d5f5
+/*
0c8d5f5
+ * 'avahi_cups_poll_new' - Create a new Avahi main loop object for CUPS
0c8d5f5
+ */
0c8d5f5
+
0c8d5f5
+AvahiCupsPoll *
0c8d5f5
+avahi_cups_poll_new (void)
0c8d5f5
+{
0c8d5f5
+  AvahiCupsPoll *cups_poll = malloc(sizeof(AvahiCupsPoll));
0c8d5f5
+  if (cups_poll == NULL)
0c8d5f5
+    return (NULL);
0c8d5f5
+
0c8d5f5
+  cups_poll->watched_fds = cupsArrayNew ((cups_array_func_t)compare_watched_fds,
0c8d5f5
+					 NULL);
0c8d5f5
+  cups_poll->timeouts = cupsArrayNew (NULL, NULL);
0c8d5f5
+
0c8d5f5
+  cups_poll->api.userdata = cups_poll;
0c8d5f5
+  cups_poll->api.watch_new = watch_new;
0c8d5f5
+  cups_poll->api.watch_free = watch_free;
0c8d5f5
+  cups_poll->api.watch_update = watch_update;
0c8d5f5
+  cups_poll->api.watch_get_events = watch_get_events;
0c8d5f5
+
0c8d5f5
+  cups_poll->api.timeout_new = timeout_new;
0c8d5f5
+  cups_poll->api.timeout_update = timeout_update;
0c8d5f5
+  cups_poll->api.timeout_free = timeout_free;
0c8d5f5
+
0c8d5f5
+  return (cups_poll);
0c8d5f5
+}
0c8d5f5
+
0c8d5f5
+
0c8d5f5
+/*
0c8d5f5
+ * 'avahi_cups_poll_free' - Free an Avahi main loop object for CUPS
0c8d5f5
+ */
0c8d5f5
+void
0c8d5f5
+avahi_cups_poll_free (AvahiCupsPoll *cups_poll)
0c8d5f5
+{
0c8d5f5
+  cupsd_watched_fd_t *watched_fd;
0c8d5f5
+
0c8d5f5
+  for (watched_fd = (cupsd_watched_fd_t*)cupsArrayFirst(cups_poll->watched_fds);
0c8d5f5
+       watched_fd;
0c8d5f5
+       watched_fd = (cupsd_watched_fd_t*)cupsArrayNext(cups_poll->watched_fds))
0c8d5f5
+    cupsArrayClear (watched_fd->watches);
0c8d5f5
+
0c8d5f5
+  cupsArrayClear (cups_poll->watched_fds);
0c8d5f5
+  cupsArrayClear (cups_poll->timeouts);
0c8d5f5
+}
0c8d5f5
+
0c8d5f5
+
0c8d5f5
+/*
0c8d5f5
+ * 'avahi_cups_poll_get' - Get the abstract poll API structure
0c8d5f5
+ */
0c8d5f5
+
0c8d5f5
+const AvahiPoll *
0c8d5f5
+avahi_cups_poll_get (AvahiCupsPoll *cups_poll)
0c8d5f5
+{
0c8d5f5
+  return (&cups_poll->api);
0c8d5f5
+}
0c8d5f5
+
0c8d5f5
+
0c8d5f5
+#endif /* HAVE_AVAHI ... from top of file */
0c8d5f5
+
0c8d5f5
+/*
0c8d5f5
+ * End of "$Id$".
0c8d5f5
+ */
bdf6254
diff -up cups-2.2.7/scheduler/avahi.h.avahi-no-threaded cups-2.2.7/scheduler/avahi.h
bdf6254
--- cups-2.2.7/scheduler/avahi.h.avahi-no-threaded	2018-04-03 14:27:17.067410826 +0200
bdf6254
+++ cups-2.2.7/scheduler/avahi.h	2018-04-03 14:27:17.067410826 +0200
0c8d5f5
@@ -0,0 +1,69 @@
0c8d5f5
+/*
0c8d5f5
+ * "$Id$"
0c8d5f5
+ *
0c8d5f5
+ *   Avahi poll implementation for the CUPS scheduler.
0c8d5f5
+ *
0c8d5f5
+ *   Copyright (C) 2010, 2011 Red Hat, Inc.
0c8d5f5
+ *   Authors:
0c8d5f5
+ *    Tim Waugh <twaugh@redhat.com>
0c8d5f5
+ *
0c8d5f5
+ *   Redistribution and use in source and binary forms, with or without
0c8d5f5
+ *   modification, are permitted provided that the following conditions
0c8d5f5
+ *   are met:
0c8d5f5
+ *
0c8d5f5
+ *   Redistributions of source code must retain the above copyright
0c8d5f5
+ *   notice, this list of conditions and the following disclaimer.
0c8d5f5
+ *
0c8d5f5
+ *   Redistributions in binary form must reproduce the above copyright
0c8d5f5
+ *   notice, this list of conditions and the following disclaimer in the
0c8d5f5
+ *   documentation and/or other materials provided with the distribution.
0c8d5f5
+ *
0c8d5f5
+ *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
0c8d5f5
+ *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
0c8d5f5
+ *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
0c8d5f5
+ *   FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
0c8d5f5
+ *   COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
0c8d5f5
+ *   INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
0c8d5f5
+ *   (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
0c8d5f5
+ *   SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
0c8d5f5
+ *   HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
0c8d5f5
+ *   STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
0c8d5f5
+ *   ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
0c8d5f5
+ *   OF THE POSSIBILITY OF SUCH DAMAGE.
0c8d5f5
+ */
0c8d5f5
+
0c8d5f5
+#include <config.h>
0c8d5f5
+
0c8d5f5
+#ifdef HAVE_AVAHI
0c8d5f5
+#  include <avahi-client/client.h>
0c8d5f5
+#  include <avahi-client/publish.h>
0c8d5f5
+#endif /* HAVE_AVAHI */
0c8d5f5
+
0c8d5f5
+#ifdef HAVE_AUTHORIZATION_H
0c8d5f5
+#  include <Security/Authorization.h>
0c8d5f5
+#endif /* HAVE_AUTHORIZATION_H */
0c8d5f5
+
0c8d5f5
+
0c8d5f5
+#ifdef HAVE_AVAHI
0c8d5f5
+typedef struct
0c8d5f5
+{
0c8d5f5
+    AvahiPoll api;
0c8d5f5
+    cups_array_t *watched_fds;
0c8d5f5
+    cups_array_t *timeouts;
0c8d5f5
+} AvahiCupsPoll;
0c8d5f5
+#endif /* HAVE_AVAHI */
0c8d5f5
+
0c8d5f5
+/*
0c8d5f5
+ * Prototypes...
0c8d5f5
+ */
0c8d5f5
+
0c8d5f5
+#ifdef HAVE_AVAHI
0c8d5f5
+extern AvahiCupsPoll *	avahi_cups_poll_new(void);
0c8d5f5
+extern void		avahi_cups_poll_free(AvahiCupsPoll *cups_poll);
0c8d5f5
+extern const AvahiPoll *avahi_cups_poll_get(AvahiCupsPoll *cups_poll);
0c8d5f5
+#endif /* HAVE_AVAHI */
0c8d5f5
+
0c8d5f5
+
0c8d5f5
+/*
0c8d5f5
+ * End of "$Id$".
0c8d5f5
+ */
bdf6254
diff -up cups-2.2.7/scheduler/cupsd.h.avahi-no-threaded cups-2.2.7/scheduler/cupsd.h
bdf6254
--- cups-2.2.7/scheduler/cupsd.h.avahi-no-threaded	2018-03-23 04:48:36.000000000 +0100
bdf6254
+++ cups-2.2.7/scheduler/cupsd.h	2018-04-03 14:27:17.067410826 +0200
5d14999
@@ -118,6 +118,7 @@ extern const char *cups_hstrerror(int);
0c8d5f5
 #include "colorman.h"
0c8d5f5
 #include "conf.h"
0c8d5f5
 #include "banners.h"
0c8d5f5
+#include "avahi.h"
0c8d5f5
 #include "dirsvc.h"
0c8d5f5
 #include "network.h"
0c8d5f5
 #include "subscriptions.h"
5d14999
@@ -138,6 +139,15 @@ extern const char *cups_hstrerror(int);
0c8d5f5
 
0c8d5f5
 typedef void (*cupsd_selfunc_t)(void *data);
0c8d5f5
 
0c8d5f5
+#ifdef HAVE_AVAHI
0c8d5f5
+/*
0c8d5f5
+ * Timeout callback function type...
0c8d5f5
+ */
0c8d5f5
+
0c8d5f5
+typedef struct _cupsd_timeout_s cupsd_timeout_t;
0c8d5f5
+typedef void (*cupsd_timeoutfunc_t)(cupsd_timeout_t *timeout, void *data);
0c8d5f5
+#endif /* HAVE_AVAHI */
0c8d5f5
+
0c8d5f5
 
0c8d5f5
 /*
0c8d5f5
  * Globals...
5d14999
@@ -162,6 +172,9 @@ VAR int			OnDemand	VALUE(0);
6dd0746
 					/* Launched on demand */
5d14999
 #endif /* HAVE_ONDEMAND */
0c8d5f5
 
0c8d5f5
+#ifdef HAVE_AVAHI
0c8d5f5
+VAR cups_array_t *Timeouts;		/* Timed callbacks for main loop */
0c8d5f5
+#endif /* HAVE_AVAHI */
0c8d5f5
 
0c8d5f5
 /*
0c8d5f5
  * Prototypes...
5d14999
@@ -224,3 +237,15 @@ extern void		cupsdStopSelect(void);
5d14999
 /* server.c */
0fed0db
 extern void		cupsdStartServer(void);
0fed0db
 extern void		cupsdStopServer(void);
5d14999
+
0fed0db
+#ifdef HAVE_AVAHI
0fed0db
+extern void     cupsdInitTimeouts(void);
0fed0db
+extern cupsd_timeout_t *cupsdAddTimeout (const struct timeval *tv,
0fed0db
+					 cupsd_timeoutfunc_t cb,
0fed0db
+					 void *data);
0fed0db
+extern cupsd_timeout_t *cupsdNextTimeout (long *delay);
0fed0db
+extern void     cupsdRunTimeout (cupsd_timeout_t *timeout);
0fed0db
+extern void     cupsdUpdateTimeout (cupsd_timeout_t *timeout,
0fed0db
+				    const struct timeval *tv);
0fed0db
+extern void     cupsdRemoveTimeout (cupsd_timeout_t *timeout);
0fed0db
+#endif /* HAVE_AVAHI */
5d14999
\ No newline at end of file
bdf6254
diff -up cups-2.2.7/scheduler/dirsvc.c.avahi-no-threaded cups-2.2.7/scheduler/dirsvc.c
bdf6254
--- cups-2.2.7/scheduler/dirsvc.c.avahi-no-threaded	2018-03-23 04:48:36.000000000 +0100
bdf6254
+++ cups-2.2.7/scheduler/dirsvc.c	2018-04-03 14:30:13.099422499 +0200
5d14999
@@ -193,7 +193,7 @@ cupsdStartBrowsing(void)
e53709b
     cupsdUpdateDNSSDName();
0c8d5f5
 
0c8d5f5
 #  else /* HAVE_AVAHI */
0c8d5f5
-    if ((DNSSDMaster = avahi_threaded_poll_new()) == NULL)
0c8d5f5
+    if ((DNSSDMaster = avahi_cups_poll_new()) == NULL)
0c8d5f5
     {
0c8d5f5
       cupsdLogMessage(CUPSD_LOG_ERROR, "Unable to create DNS-SD thread.");
0c8d5f5
 
5d14999
@@ -204,7 +204,7 @@ cupsdStartBrowsing(void)
0c8d5f5
     {
0c8d5f5
       int error;			/* Error code, if any */
0c8d5f5
 
e53709b
-      DNSSDClient = avahi_client_new(avahi_threaded_poll_get(DNSSDMaster), AVAHI_CLIENT_NO_FAIL, dnssdClientCallback, NULL, &error);
e53709b
+      DNSSDClient = avahi_client_new(avahi_cups_poll_get(DNSSDMaster), AVAHI_CLIENT_NO_FAIL, dnssdClientCallback, NULL, &error);
0c8d5f5
 
0c8d5f5
       if (DNSSDClient == NULL)
e53709b
       {
5d14999
@@ -215,11 +215,9 @@ cupsdStartBrowsing(void)
0c8d5f5
         if (FatalErrors & CUPSD_FATAL_BROWSE)
0c8d5f5
 	  cupsdEndProcess(getpid(), 0);
0c8d5f5
 
0c8d5f5
-        avahi_threaded_poll_free(DNSSDMaster);
0c8d5f5
+        avahi_cups_poll_free(DNSSDMaster);
0c8d5f5
         DNSSDMaster = NULL;
0c8d5f5
       }
0c8d5f5
-      else
0c8d5f5
-	avahi_threaded_poll_start(DNSSDMaster);
0c8d5f5
     }
0c8d5f5
 #  endif /* HAVE_DNSSD */
e53709b
   }
cd39102
@@ -635,7 +633,7 @@ dnssdClientCallback(
e53709b
 	  * Renew Avahi client...
e53709b
 	  */
0c8d5f5
 
e53709b
-	  DNSSDClient = avahi_client_new(avahi_threaded_poll_get(DNSSDMaster), AVAHI_CLIENT_NO_FAIL, dnssdClientCallback, NULL, &error);
e53709b
+	  DNSSDClient = avahi_client_new(avahi_cups_poll_get(DNSSDMaster), AVAHI_CLIENT_NO_FAIL, dnssdClientCallback, NULL, &error);
e53709b
 
e53709b
 	  if (!DNSSDClient)
e53709b
 	  {
bdf6254
@@ -701,17 +699,11 @@ dnssdDeregisterInstance(
bdf6254
   *srv = NULL;
bdf6254
 
0c8d5f5
 #  else /* HAVE_AVAHI */
bdf6254
-  if (!from_callback)
bdf6254
-    avahi_threaded_poll_lock(DNSSDMaster);
bdf6254
-
cd39102
   if (*srv)
cd39102
   {
cd39102
     avahi_entry_group_free(*srv);
bdf6254
     *srv = NULL;
cd39102
   }
bdf6254
-
bdf6254
-  if (!from_callback)
bdf6254
-    avahi_threaded_poll_unlock(DNSSDMaster);
0c8d5f5
 #  endif /* HAVE_DNSSD */
bdf6254
 }
0c8d5f5
 
bdf6254
@@ -1030,16 +1022,10 @@ dnssdRegisterInstance(
0c8d5f5
   (void)commit;
0c8d5f5
 
0c8d5f5
 #  else /* HAVE_AVAHI */
e53709b
-  if (!from_callback)
e53709b
-    avahi_threaded_poll_lock(DNSSDMaster);
0c8d5f5
-
0c8d5f5
   if (!*srv)
0c8d5f5
     *srv = avahi_entry_group_new(DNSSDClient, dnssdRegisterCallback, NULL);
0c8d5f5
   if (!*srv)
0c8d5f5
   {
e53709b
-    if (!from_callback)
e53709b
-      avahi_threaded_poll_unlock(DNSSDMaster);
0c8d5f5
-
0c8d5f5
     cupsdLogMessage(CUPSD_LOG_WARN, "DNS-SD registration of \"%s\" failed: %s",
0c8d5f5
                     name, dnssdErrorString(avahi_client_errno(DNSSDClient)));
0c8d5f5
     return (0);
bdf6254
@@ -1154,9 +1140,6 @@ dnssdRegisterInstance(
0c8d5f5
       cupsdLogMessage(CUPSD_LOG_DEBUG, "DNS-SD commit of \"%s\" failed.",
0c8d5f5
                       name);
0c8d5f5
   }
0c8d5f5
-
e53709b
-  if (!from_callback)
e53709b
-    avahi_threaded_poll_unlock(DNSSDMaster);
0c8d5f5
 #  endif /* HAVE_DNSSD */
0c8d5f5
 
0c8d5f5
   if (error)
bdf6254
@@ -1327,9 +1310,6 @@ dnssdStop(void)
9e47311
   DNSSDMaster = NULL;
9e47311
 
9e47311
 #  else /* HAVE_AVAHI */
9347f32
-  if (DNSSDMaster)
9347f32
-    avahi_threaded_poll_stop(DNSSDMaster);
9e47311
-
9347f32
   if (DNSSDClient)
9347f32
   {
9347f32
     avahi_client_free(DNSSDClient);
bdf6254
@@ -1338,7 +1318,7 @@ dnssdStop(void)
0c8d5f5
 
9347f32
   if (DNSSDMaster)
9347f32
   {
9347f32
-    avahi_threaded_poll_free(DNSSDMaster);
9347f32
+    avahi_cups_poll_free(DNSSDMaster);
9347f32
     DNSSDMaster = NULL;
9347f32
   }
0c8d5f5
 #  endif /* HAVE_DNSSD */
bdf6254
diff -up cups-2.2.7/scheduler/dirsvc.h.avahi-no-threaded cups-2.2.7/scheduler/dirsvc.h
bdf6254
--- cups-2.2.7/scheduler/dirsvc.h.avahi-no-threaded	2018-03-23 04:48:36.000000000 +0100
bdf6254
+++ cups-2.2.7/scheduler/dirsvc.h	2018-04-03 14:27:17.068410820 +0200
5d14999
@@ -49,7 +49,7 @@ VAR cups_array_t	*DNSSDPrinters	VALUE(NU
0c8d5f5
 VAR DNSServiceRef	DNSSDMaster	VALUE(NULL);
0c8d5f5
 					/* Master DNS-SD service reference */
0c8d5f5
 #  else /* HAVE_AVAHI */
0c8d5f5
-VAR AvahiThreadedPoll	*DNSSDMaster	VALUE(NULL);
0c8d5f5
+VAR AvahiCupsPoll	*DNSSDMaster	VALUE(NULL);
0c8d5f5
 					/* Master polling interface for Avahi */
0c8d5f5
 VAR AvahiClient		*DNSSDClient	VALUE(NULL);
0c8d5f5
 					/* Client information */
bdf6254
diff -up cups-2.2.7/scheduler/main.c.avahi-no-threaded cups-2.2.7/scheduler/main.c
bdf6254
--- cups-2.2.7/scheduler/main.c.avahi-no-threaded	2018-04-03 14:27:17.057410882 +0200
bdf6254
+++ cups-2.2.7/scheduler/main.c	2018-04-03 14:27:17.069410814 +0200
cd39102
@@ -131,7 +131,10 @@ main(int  argc,				/* I - Number of comm
6dd0746
   int			service_idle_exit;
0c8d5f5
 					/* Idle exit on select timeout? */
5d14999
 #endif /* HAVE_ONDEMAND */
5d14999
-
0c8d5f5
+#ifdef HAVE_AVAHI
0c8d5f5
+  cupsd_timeout_t	*tmo;		/* Next scheduled timed callback */
0c8d5f5
+  long			tmo_delay;	/* Time before it must be called */
0c8d5f5
+#endif /* HAVE_AVAHI */
0c8d5f5
 
0c8d5f5
 #ifdef HAVE_GETEUID
5d14999
  /*
cd39102
@@ -610,6 +613,14 @@ main(int  argc,				/* I - Number of comm
0c8d5f5
 
0c8d5f5
   httpInitialize();
0c8d5f5
 
0c8d5f5
+#ifdef HAVE_AVAHI
0c8d5f5
+ /*
0c8d5f5
+  * Initialize timed callback structures.
0c8d5f5
+  */
0c8d5f5
+
0c8d5f5
+  cupsdInitTimeouts();
0c8d5f5
+#endif /* HAVE_AVAHI */
0c8d5f5
+
0c8d5f5
   cupsdStartServer();
0c8d5f5
 
0c8d5f5
  /*
cd39102
@@ -928,6 +939,16 @@ main(int  argc,				/* I - Number of comm
0c8d5f5
     }
0c8d5f5
 #endif /* __APPLE__ */
0c8d5f5
 
0c8d5f5
+#ifdef HAVE_AVAHI
0c8d5f5
+   /*
0c8d5f5
+    * If a timed callback is due, run it.
0c8d5f5
+    */
0c8d5f5
+
0c8d5f5
+    tmo = cupsdNextTimeout (&tmo_delay);
0c8d5f5
+    if (tmo && tmo_delay == 0)
0c8d5f5
+      cupsdRunTimeout (tmo);
0c8d5f5
+#endif /* HAVE_AVAHI */
0c8d5f5
+
0c8d5f5
 #ifndef __APPLE__
0c8d5f5
    /*
0c8d5f5
     * Update the network interfaces once a minute...
cd39102
@@ -1632,6 +1653,10 @@ select_timeout(int fds)			/* I - Number
0c8d5f5
   cupsd_job_t		*job;		/* Job information */
7dc7717
   cupsd_printer_t       *printer;       /* Printer information */
0c8d5f5
   const char		*why;		/* Debugging aid */
0c8d5f5
+#ifdef HAVE_AVAHI
0c8d5f5
+  cupsd_timeout_t	*tmo;		/* Timed callback */
0c8d5f5
+  long			tmo_delay;	/* Seconds before calling it */
0c8d5f5
+#endif /* HAVE_AVAHI */
0c8d5f5
 
0c8d5f5
 
0c8d5f5
   cupsdLogMessage(CUPSD_LOG_DEBUG2, "select_timeout: JobHistoryUpdate=%ld",
cd39102
@@ -1677,6 +1702,19 @@ select_timeout(int fds)			/* I - Number
0c8d5f5
   }
0c8d5f5
 #endif /* __APPLE__ */
0c8d5f5
 
0c8d5f5
+#ifdef HAVE_AVAHI
0c8d5f5
+ /*
0c8d5f5
+  * See if there are any scheduled timed callbacks to run.
0c8d5f5
+  */
0c8d5f5
+
0c8d5f5
+  if ((tmo = cupsdNextTimeout(&tmo_delay)) != NULL &&
0c8d5f5
+      (now + tmo_delay) < timeout)
0c8d5f5
+  {
0c8d5f5
+    timeout = tmo_delay;
0c8d5f5
+    why = "run a timed callback";
0c8d5f5
+  }
0c8d5f5
+#endif /* HAVE_AVAHI */
0c8d5f5
+
0c8d5f5
  /*
0c8d5f5
   * Check whether we are accepting new connections...
0c8d5f5
   */
bdf6254
diff -up cups-2.2.7/scheduler/Makefile.avahi-no-threaded cups-2.2.7/scheduler/Makefile
bdf6254
--- cups-2.2.7/scheduler/Makefile.avahi-no-threaded	2018-03-23 04:48:36.000000000 +0100
bdf6254
+++ cups-2.2.7/scheduler/Makefile	2018-04-03 14:27:17.069410814 +0200
5d14999
@@ -15,6 +15,7 @@ include ../Makedefs
e53709b
 
e53709b
 CUPSDOBJS =	\
e53709b
 		auth.o \
e53709b
+		avahi.o \
e53709b
 		banners.o \
e53709b
 		cert.o \
e53709b
 		classes.o \
5d14999
@@ -38,7 +39,8 @@ CUPSDOBJS =	\
6dd0746
 		server.o \
e53709b
 		statbuf.o \
e53709b
 		subscriptions.o \
6dd0746
-		sysman.o
6dd0746
+		sysman.o \
6dd0746
+		timeout.o
e53709b
 LIBOBJS =	\
e53709b
 		filter.o \
6dd0746
 		mime.o \
bdf6254
diff -up cups-2.2.7/scheduler/timeout.c.avahi-no-threaded cups-2.2.7/scheduler/timeout.c
bdf6254
--- cups-2.2.7/scheduler/timeout.c.avahi-no-threaded	2018-04-03 14:27:17.069410814 +0200
bdf6254
+++ cups-2.2.7/scheduler/timeout.c	2018-04-03 14:27:17.069410814 +0200
0c8d5f5
@@ -0,0 +1,235 @@
0c8d5f5
+/*
0c8d5f5
+ * "$Id$"
0c8d5f5
+ *
0c8d5f5
+ *   Timeout functions for the Common UNIX Printing System (CUPS).
0c8d5f5
+ *
0c8d5f5
+ *   Copyright (C) 2010, 2011 Red Hat, Inc.
0c8d5f5
+ *   Authors:
0c8d5f5
+ *     Tim Waugh <twaugh@redhat.com>
0c8d5f5
+ *
0c8d5f5
+ *   Redistribution and use in source and binary forms, with or without
0c8d5f5
+ *   modification, are permitted provided that the following conditions
0c8d5f5
+ *   are met:
0c8d5f5
+ *
0c8d5f5
+ *   Redistributions of source code must retain the above copyright
0c8d5f5
+ *   notice, this list of conditions and the following disclaimer.
0c8d5f5
+ *
0c8d5f5
+ *   Redistributions in binary form must reproduce the above copyright
0c8d5f5
+ *   notice, this list of conditions and the following disclaimer in the
0c8d5f5
+ *   documentation and/or other materials provided with the distribution.
0c8d5f5
+ *
0c8d5f5
+ *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
0c8d5f5
+ *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
0c8d5f5
+ *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
0c8d5f5
+ *   FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
0c8d5f5
+ *   COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
0c8d5f5
+ *   INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
0c8d5f5
+ *   (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
0c8d5f5
+ *   SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
0c8d5f5
+ *   HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
0c8d5f5
+ *   STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
0c8d5f5
+ *   ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
0c8d5f5
+ *   OF THE POSSIBILITY OF SUCH DAMAGE.
0c8d5f5
+ *
0c8d5f5
+ * Contents:
0c8d5f5
+ *
0c8d5f5
+ *   cupsdInitTimeouts()  - Initialise timeout structure.
0c8d5f5
+ *   cupsdAddTimeout()    - Add a timed callback.
0c8d5f5
+ *   cupsdNextTimeout()   - Find the next enabled timed callback.
0c8d5f5
+ *   cupsdUpdateTimeout() - Adjust the time of a timed callback or disable it.
0c8d5f5
+ *   cupsdRemoveTimeout() - Discard a timed callback.
0c8d5f5
+ *   compare_timeouts()   - Compare timed callbacks for array sorting.
0c8d5f5
+ */
0c8d5f5
+
0c8d5f5
+#include <config.h>
0c8d5f5
+
0c8d5f5
+#ifdef HAVE_AVAHI /* Applies to entire file... */
0c8d5f5
+
0c8d5f5
+/*
0c8d5f5
+ * Include necessary headers...
0c8d5f5
+ */
0c8d5f5
+
0c8d5f5
+#include "cupsd.h"
0c8d5f5
+
0c8d5f5
+#if defined(HAVE_MALLOC_H) && defined(HAVE_MALLINFO)
0c8d5f5
+#  include <malloc.h>
0c8d5f5
+#endif /* HAVE_MALLOC_H && HAVE_MALLINFO */
0c8d5f5
+
0c8d5f5
+#ifdef HAVE_AVAHI
0c8d5f5
+#  include <avahi-common/timeval.h>
0c8d5f5
+#endif /* HAVE_AVAHI */
0c8d5f5
+
0c8d5f5
+
0c8d5f5
+struct _cupsd_timeout_s
0c8d5f5
+{
0c8d5f5
+  struct timeval when;
0c8d5f5
+  int enabled;
0c8d5f5
+  cupsd_timeoutfunc_t callback;
0c8d5f5
+  void *data;
0c8d5f5
+};
0c8d5f5
+
0c8d5f5
+/*
0c8d5f5
+ * Local functions...
0c8d5f5
+ */
0c8d5f5
+
0c8d5f5
+/*
0c8d5f5
+ * 'compare_timeouts()' - Compare timed callbacks for array sorting.
0c8d5f5
+ */
0c8d5f5
+
0c8d5f5
+static int
0c8d5f5
+compare_addrs (void *p0, void *p1)
0c8d5f5
+{
0c8d5f5
+  if (p0 == p1)
0c8d5f5
+    return (0);
0c8d5f5
+  if (p0 < p1)
0c8d5f5
+    return (-1);
0c8d5f5
+  return (1);
0c8d5f5
+}
0c8d5f5
+
0c8d5f5
+static int
0c8d5f5
+compare_timeouts (cupsd_timeout_t *p0, cupsd_timeout_t *p1)
0c8d5f5
+{
0c8d5f5
+  int addrsdiff = compare_addrs (p0, p1);
0c8d5f5
+  int tvdiff;
0c8d5f5
+
0c8d5f5
+  if (addrsdiff == 0)
0c8d5f5
+    return (0);
0c8d5f5
+
0c8d5f5
+  if (!p0->enabled || !p1->enabled)
0c8d5f5
+  {
0c8d5f5
+    if (!p0->enabled && !p1->enabled)
0c8d5f5
+      return (addrsdiff);
0c8d5f5
+
0c8d5f5
+    return (p0->enabled ? -1 : 1);
0c8d5f5
+  }
0c8d5f5
+
0c8d5f5
+  tvdiff = avahi_timeval_compare (&p0->when, &p1->when);
0c8d5f5
+  if (tvdiff != 0)
0c8d5f5
+    return (tvdiff);
0c8d5f5
+
0c8d5f5
+  return (addrsdiff);
0c8d5f5
+}
0c8d5f5
+
0c8d5f5
+
0c8d5f5
+/*
0c8d5f5
+ * 'cupsdInitTimeouts()' - Initialise timeout structures.
0c8d5f5
+ */
0c8d5f5
+
0c8d5f5
+void
0c8d5f5
+cupsdInitTimeouts(void)
0c8d5f5
+{
0c8d5f5
+  Timeouts = cupsArrayNew ((cups_array_func_t)compare_timeouts, NULL);
0c8d5f5
+}
0c8d5f5
+
0c8d5f5
+
0c8d5f5
+/*
0c8d5f5
+ * 'cupsdAddTimeout()' - Add a timed callback.
0c8d5f5
+ */
0c8d5f5
+
0c8d5f5
+cupsd_timeout_t *				/* O - Timeout handle */
0c8d5f5
+cupsdAddTimeout(const struct timeval *tv,	/* I - Absolute time */
0c8d5f5
+		cupsd_timeoutfunc_t cb,		/* I - Callback function */
0c8d5f5
+		void *data)			/* I - User data */
0c8d5f5
+{
0c8d5f5
+  cupsd_timeout_t *timeout;
0c8d5f5
+
0c8d5f5
+  timeout = malloc (sizeof(cupsd_timeout_t));
0c8d5f5
+  if (timeout != NULL)
0c8d5f5
+  {
0c8d5f5
+    timeout->enabled = (tv != NULL);
0c8d5f5
+    if (tv)
0c8d5f5
+    {
0c8d5f5
+      timeout->when.tv_sec = tv->tv_sec;
0c8d5f5
+      timeout->when.tv_usec = tv->tv_usec;
0c8d5f5
+    }
0c8d5f5
+
0c8d5f5
+    timeout->callback = cb;
0c8d5f5
+    timeout->data = data;
0c8d5f5
+    cupsArrayAdd (Timeouts, timeout);
0c8d5f5
+  }
0c8d5f5
+
0c8d5f5
+  return timeout;
0c8d5f5
+}
0c8d5f5
+
0c8d5f5
+
0c8d5f5
+/*
0c8d5f5
+ * 'cupsdNextTimeout()' - Find the next enabled timed callback.
0c8d5f5
+ */
0c8d5f5
+
0c8d5f5
+cupsd_timeout_t *		/* O - Next enabled timeout or NULL */
0c8d5f5
+cupsdNextTimeout(long *delay)	/* O - Seconds before scheduled */
0c8d5f5
+{
0c8d5f5
+  cupsd_timeout_t *first = cupsArrayFirst (Timeouts);
0c8d5f5
+  struct timeval curtime;
0c8d5f5
+
0c8d5f5
+  if (first && !first->enabled)
0c8d5f5
+    first = NULL;
0c8d5f5
+
0c8d5f5
+  if (first && delay)
0c8d5f5
+  {
0c8d5f5
+    gettimeofday (&curtime, NULL);
0c8d5f5
+    if (avahi_timeval_compare (&curtime, &first->when) > 0)
0c8d5f5
+    {
0c8d5f5
+      *delay = 0;
0c8d5f5
+    } else {
0c8d5f5
+      *delay = 1 + first->when.tv_sec - curtime.tv_sec;
0c8d5f5
+      if (first->when.tv_usec < curtime.tv_usec)
0c8d5f5
+	(*delay)--;
0c8d5f5
+    }
0c8d5f5
+  }
0c8d5f5
+
0c8d5f5
+  return (first);
0c8d5f5
+}
0c8d5f5
+
0c8d5f5
+
0c8d5f5
+/*
0c8d5f5
+ * 'cupsdRunTimeout()' - Run a timed callback.
0c8d5f5
+ */
0c8d5f5
+
0c8d5f5
+void
0c8d5f5
+cupsdRunTimeout(cupsd_timeout_t *timeout)	/* I - Timeout */
0c8d5f5
+{
0c8d5f5
+  if (!timeout)
0c8d5f5
+    return;
0c8d5f5
+  timeout->enabled = 0;
0c8d5f5
+  if (!timeout->callback)
0c8d5f5
+    return;
0c8d5f5
+  timeout->callback (timeout, timeout->data);
0c8d5f5
+}
0c8d5f5
+
0c8d5f5
+/*
0c8d5f5
+ * 'cupsdUpdateTimeout()' - Adjust the time of a timed callback or disable it.
0c8d5f5
+ */
0c8d5f5
+
0c8d5f5
+void
0c8d5f5
+cupsdUpdateTimeout(cupsd_timeout_t *timeout,	/* I - Timeout */
0c8d5f5
+		   const struct timeval *tv)	/* I - Absolute time or NULL */
0c8d5f5
+{
0c8d5f5
+  cupsArrayRemove (Timeouts, timeout);
0c8d5f5
+  timeout->enabled = (tv != NULL);
0c8d5f5
+  if (tv)
0c8d5f5
+  {
0c8d5f5
+    timeout->when.tv_sec = tv->tv_sec;
0c8d5f5
+    timeout->when.tv_usec = tv->tv_usec;
0c8d5f5
+  }
0c8d5f5
+  cupsArrayAdd (Timeouts, timeout);
0c8d5f5
+}
0c8d5f5
+
0c8d5f5
+
0c8d5f5
+/*
0c8d5f5
+ * 'cupsdRemoveTimeout()' - Discard a timed callback.
0c8d5f5
+ */
0c8d5f5
+
0c8d5f5
+void
0c8d5f5
+cupsdRemoveTimeout(cupsd_timeout_t *timeout)	/* I - Timeout */
0c8d5f5
+{
0c8d5f5
+  cupsArrayRemove (Timeouts, timeout);
0c8d5f5
+  free (timeout);
0c8d5f5
+}
0c8d5f5
+
0c8d5f5
+
0c8d5f5
+#endif /* HAVE_AVAHI ... from top of file */
0c8d5f5
+
0c8d5f5
+/*
0c8d5f5
+ * End of "$Id$".
0c8d5f5
+ */