Blame SOURCES/cups-avahi-no-threaded.patch

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