0d86476
diff -up cups-1.5.2/scheduler/cupsd.h.avahi-3-timeouts cups-1.5.2/scheduler/cupsd.h
0d86476
--- cups-1.5.2/scheduler/cupsd.h.avahi-3-timeouts	2011-05-11 23:17:34.000000000 +0100
0d86476
+++ cups-1.5.2/scheduler/cupsd.h	2012-03-14 15:06:36.509476983 +0000
9829b01
@@ -140,6 +140,15 @@ extern const char *cups_hstrerror(int);
9829b01
 
9829b01
 typedef void (*cupsd_selfunc_t)(void *data);
9829b01
 
9829b01
+#ifdef HAVE_AVAHI
9829b01
+/*
9829b01
+ * Timeout callback function type...
9829b01
+ */
9829b01
+
9829b01
+typedef struct _cupsd_timeout_s cupsd_timeout_t;
9829b01
+typedef void (*cupsd_timeoutfunc_t)(cupsd_timeout_t *timeout, void *data);
9829b01
+#endif /* HAVE_AVAHI */
9829b01
+
9829b01
 
9829b01
 /*
9829b01
  * Globals...
9829b01
@@ -173,6 +182,11 @@ VAR int			Launchd		VALUE(0);
9829b01
 					/* Running from launchd */
9829b01
 #endif /* HAVE_LAUNCH_H */
9829b01
 
9829b01
+#ifdef HAVE_AVAHI
9829b01
+VAR cups_array_t *Timeouts;		/* Timed callbacks for main loop */
9829b01
+#endif /* HAVE_AVAHI */
9829b01
+
9829b01
+
9829b01
 
9829b01
 /*
9829b01
  * Prototypes...
9829b01
@@ -242,6 +256,20 @@ extern void		cupsdStopSelect(void);
9829b01
 extern void		cupsdStartServer(void);
9829b01
 extern void		cupsdStopServer(void);
9829b01
 
9829b01
+#ifdef HAVE_AVAHI
9829b01
+extern void     cupsdInitTimeouts(void);
9829b01
+extern cupsd_timeout_t *cupsdAddTimeout (const struct timeval *tv,
9829b01
+					 cupsd_timeoutfunc_t cb,
9829b01
+					 void *data);
9829b01
+extern cupsd_timeout_t *cupsdNextTimeout (long *delay);
9829b01
+extern void     cupsdRunTimeout (cupsd_timeout_t *timeout);
9829b01
+extern void     cupsdUpdateTimeout (cupsd_timeout_t *timeout,
9829b01
+				    const struct timeval *tv);
9829b01
+extern void     cupsdRemoveTimeout (cupsd_timeout_t *timeout);
9829b01
+#endif /* HAVE_AVAHI */
9829b01
+
9829b01
+extern int	cupsdRemoveFile(const char *filename);
9829b01
+
9829b01
 
9829b01
 /*
9829b01
  * End of "$Id: cupsd.h 9766 2011-05-11 22:17:34Z mike $".
0d86476
diff -up cups-1.5.2/scheduler/main.c.avahi-3-timeouts cups-1.5.2/scheduler/main.c
0d86476
--- cups-1.5.2/scheduler/main.c.avahi-3-timeouts	2012-03-14 15:04:17.655305548 +0000
0d86476
+++ cups-1.5.2/scheduler/main.c	2012-03-14 15:06:36.511476986 +0000
9829b01
@@ -146,6 +146,10 @@ main(int  argc,				/* I - Number of comm
9829b01
   int			launchd_idle_exit;
9829b01
 					/* Idle exit on select timeout? */
9829b01
 #endif	/* HAVE_LAUNCHD */
9829b01
+#ifdef HAVE_AVAHI
9829b01
+  cupsd_timeout_t	*tmo;		/* Next scheduled timed callback */
9829b01
+  long			tmo_delay;	/* Time before it must be called */
9829b01
+#endif /* HAVE_AVAHI */
9829b01
 
9829b01
 
9829b01
 #ifdef HAVE_GETEUID
9829b01
@@ -535,6 +539,14 @@ main(int  argc,				/* I - Number of comm
9829b01
 
9829b01
   httpInitialize();
9829b01
 
9829b01
+#ifdef HAVE_AVAHI
9829b01
+ /*
9829b01
+  * Initialize timed callback structures.
9829b01
+  */
9829b01
+
9829b01
+  cupsdInitTimeouts();
9829b01
+#endif /* HAVE_AVAHI */
9829b01
+
9829b01
   cupsdStartServer();
9829b01
 
9829b01
  /*
9829b01
@@ -874,6 +886,16 @@ main(int  argc,				/* I - Number of comm
9829b01
     }
9829b01
 #endif /* __APPLE__ */
9829b01
 
9829b01
+#ifdef HAVE_AVAHI
9829b01
+   /*
9829b01
+    * If a timed callback is due, run it.
9829b01
+    */
9829b01
+
9829b01
+    tmo = cupsdNextTimeout (&tmo_delay);
9829b01
+    if (tmo && tmo_delay == 0)
9829b01
+      cupsdRunTimeout (tmo);
9829b01
+#endif /* HAVE_AVAHI */
9829b01
+
9829b01
 #ifndef __APPLE__
9829b01
    /*
9829b01
     * Update the network interfaces once a minute...
3d32f50
@@ -1787,6 +1809,10 @@ select_timeout(int fds)			/* I - Number
9829b01
   cupsd_job_t		*job;		/* Job information */
9829b01
   cupsd_subscription_t	*sub;		/* Subscription information */
9829b01
   const char		*why;		/* Debugging aid */
9829b01
+#ifdef HAVE_AVAHI
9829b01
+  cupsd_timeout_t	*tmo;		/* Timed callback */
9829b01
+  long			tmo_delay;	/* Seconds before calling it */
9829b01
+#endif /* HAVE_AVAHI */
9829b01
 
9829b01
 
9829b01
  /*
3d32f50
@@ -1829,6 +1855,19 @@ select_timeout(int fds)			/* I - Number
9829b01
   }
9829b01
 #endif /* __APPLE__ */
9829b01
 
9829b01
+#ifdef HAVE_AVAHI
9829b01
+ /*
9829b01
+  * See if there are any scheduled timed callbacks to run.
9829b01
+  */
9829b01
+
0d86476
+  if ((tmo = cupsdNextTimeout(&tmo_delay)) != NULL &&
0d86476
+      (now + tmo_delay) < timeout)
9829b01
+  {
9829b01
+    timeout = tmo_delay;
9829b01
+    why = "run a timed callback";
9829b01
+  }
9829b01
+#endif /* HAVE_AVAHI */
9829b01
+
9829b01
  /*
9829b01
   * Check whether we are accepting new connections...
9829b01
   */
0d86476
diff -up cups-1.5.2/scheduler/Makefile.avahi-3-timeouts cups-1.5.2/scheduler/Makefile
0d86476
--- cups-1.5.2/scheduler/Makefile.avahi-3-timeouts	2012-03-14 15:04:17.685305586 +0000
0d86476
+++ cups-1.5.2/scheduler/Makefile	2012-03-14 15:06:36.508476980 +0000
9829b01
@@ -39,7 +39,8 @@ CUPSDOBJS =	\
9829b01
 		server.o \
9829b01
 		statbuf.o \
9829b01
 		subscriptions.o \
9829b01
-		sysman.o
9829b01
+		sysman.o \
9829b01
+		timeout.o
9829b01
 LIBOBJS =	\
9829b01
 		filter.o \
9829b01
 		mime.o \
0d86476
diff -up cups-1.5.2/scheduler/timeout.c.avahi-3-timeouts cups-1.5.2/scheduler/timeout.c
0d86476
--- cups-1.5.2/scheduler/timeout.c.avahi-3-timeouts	2012-03-14 15:06:36.552477037 +0000
0d86476
+++ cups-1.5.2/scheduler/timeout.c	2012-03-14 15:06:36.552477037 +0000
3d32f50
@@ -0,0 +1,235 @@
9829b01
+/*
9829b01
+ * "$Id$"
9829b01
+ *
9829b01
+ *   Timeout functions for the Common UNIX Printing System (CUPS).
9829b01
+ *
3d32f50
+ *   Copyright (C) 2010, 2011 Red Hat, Inc.
9829b01
+ *   Authors:
9829b01
+ *     Tim Waugh <twaugh@redhat.com>
9829b01
+ *
3d32f50
+ *   Redistribution and use in source and binary forms, with or without
3d32f50
+ *   modification, are permitted provided that the following conditions
3d32f50
+ *   are met:
3d32f50
+ *
3d32f50
+ *   Redistributions of source code must retain the above copyright
3d32f50
+ *   notice, this list of conditions and the following disclaimer.
3d32f50
+ *
3d32f50
+ *   Redistributions in binary form must reproduce the above copyright
3d32f50
+ *   notice, this list of conditions and the following disclaimer in the
3d32f50
+ *   documentation and/or other materials provided with the distribution.
3d32f50
+ *
3d32f50
+ *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
3d32f50
+ *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
3d32f50
+ *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
3d32f50
+ *   FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
3d32f50
+ *   COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
3d32f50
+ *   INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
3d32f50
+ *   (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
3d32f50
+ *   SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3d32f50
+ *   HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
3d32f50
+ *   STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
3d32f50
+ *   ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
3d32f50
+ *   OF THE POSSIBILITY OF SUCH DAMAGE.
9829b01
+ *
9829b01
+ * Contents:
9829b01
+ *
9829b01
+ *   cupsdInitTimeouts()  - Initialise timeout structure.
9829b01
+ *   cupsdAddTimeout()    - Add a timed callback.
9829b01
+ *   cupsdNextTimeout()   - Find the next enabled timed callback.
9829b01
+ *   cupsdUpdateTimeout() - Adjust the time of a timed callback or disable it.
9829b01
+ *   cupsdRemoveTimeout() - Discard a timed callback.
9829b01
+ *   compare_timeouts()   - Compare timed callbacks for array sorting.
9829b01
+ */
9829b01
+
9829b01
+#include <config.h>
9829b01
+
9829b01
+#ifdef HAVE_AVAHI /* Applies to entire file... */
9829b01
+
9829b01
+/*
9829b01
+ * Include necessary headers...
9829b01
+ */
9829b01
+
9829b01
+#include "cupsd.h"
9829b01
+
9829b01
+#if defined(HAVE_MALLOC_H) && defined(HAVE_MALLINFO)
9829b01
+#  include <malloc.h>
9829b01
+#endif /* HAVE_MALLOC_H && HAVE_MALLINFO */
9829b01
+
9829b01
+#ifdef HAVE_AVAHI
9829b01
+#  include <avahi-common/timeval.h>
9829b01
+#endif /* HAVE_AVAHI */
9829b01
+
9829b01
+
9829b01
+struct _cupsd_timeout_s
9829b01
+{
9829b01
+  struct timeval when;
9829b01
+  int enabled;
9829b01
+  cupsd_timeoutfunc_t callback;
9829b01
+  void *data;
9829b01
+};
9829b01
+
9829b01
+/*
9829b01
+ * Local functions...
9829b01
+ */
9829b01
+
9829b01
+/*
9829b01
+ * 'compare_timeouts()' - Compare timed callbacks for array sorting.
9829b01
+ */
9829b01
+
9829b01
+static int
3d32f50
+compare_addrs (void *p0, void *p1)
3d32f50
+{
3d32f50
+  if (p0 == p1)
3d32f50
+    return (0);
3d32f50
+  if (p0 < p1)
3d32f50
+    return (-1);
3d32f50
+  return (1);
3d32f50
+}
3d32f50
+
3d32f50
+static int
9829b01
+compare_timeouts (cupsd_timeout_t *p0, cupsd_timeout_t *p1)
9829b01
+{
3d32f50
+  int addrsdiff = compare_addrs (p0, p1);
3d32f50
+  int tvdiff;
3d32f50
+
3d32f50
+  if (addrsdiff == 0)
3d32f50
+    return (0);
3d32f50
+
9829b01
+  if (!p0->enabled || !p1->enabled)
9829b01
+  {
9829b01
+    if (!p0->enabled && !p1->enabled)
3d32f50
+      return (addrsdiff);
9829b01
+
9829b01
+    return (p0->enabled ? -1 : 1);
9829b01
+  }
9829b01
+
3d32f50
+  tvdiff = avahi_timeval_compare (&p0->when, &p1->when);
3d32f50
+  if (tvdiff != 0)
3d32f50
+    return (tvdiff);
3d32f50
+
3d32f50
+  return (addrsdiff);
9829b01
+}
9829b01
+
9829b01
+
9829b01
+/*
9829b01
+ * 'cupsdInitTimeouts()' - Initialise timeout structures.
9829b01
+ */
9829b01
+
9829b01
+void
9829b01
+cupsdInitTimeouts(void)
9829b01
+{
9829b01
+  Timeouts = cupsArrayNew ((cups_array_func_t)compare_timeouts, NULL);
9829b01
+}
9829b01
+
9829b01
+
9829b01
+/*
9829b01
+ * 'cupsdAddTimeout()' - Add a timed callback.
9829b01
+ */
9829b01
+
9829b01
+cupsd_timeout_t *				/* O - Timeout handle */
9829b01
+cupsdAddTimeout(const struct timeval *tv,	/* I - Absolute time */
9829b01
+		cupsd_timeoutfunc_t cb,		/* I - Callback function */
9829b01
+		void *data)			/* I - User data */
9829b01
+{
9829b01
+  cupsd_timeout_t *timeout;
9829b01
+
9829b01
+  timeout = malloc (sizeof(cupsd_timeout_t));
9829b01
+  if (timeout != NULL)
9829b01
+  {
9829b01
+    timeout->enabled = (tv != NULL);
9829b01
+    if (tv)
9829b01
+    {
9829b01
+      timeout->when.tv_sec = tv->tv_sec;
9829b01
+      timeout->when.tv_usec = tv->tv_usec;
9829b01
+    }
9829b01
+
9829b01
+    timeout->callback = cb;
9829b01
+    timeout->data = data;
9829b01
+    cupsArrayAdd (Timeouts, timeout);
9829b01
+  }
9829b01
+
9829b01
+  return timeout;
9829b01
+}
9829b01
+
9829b01
+
9829b01
+/*
9829b01
+ * 'cupsdNextTimeout()' - Find the next enabled timed callback.
9829b01
+ */
9829b01
+
9829b01
+cupsd_timeout_t *		/* O - Next enabled timeout or NULL */
9829b01
+cupsdNextTimeout(long *delay)	/* O - Seconds before scheduled */
9829b01
+{
9829b01
+  cupsd_timeout_t *first = cupsArrayFirst (Timeouts);
9829b01
+  struct timeval curtime;
9829b01
+
9829b01
+  if (first && !first->enabled)
9829b01
+    first = NULL;
9829b01
+
9829b01
+  if (first && delay)
9829b01
+  {
9829b01
+    gettimeofday (&curtime, NULL);
9829b01
+    if (avahi_timeval_compare (&curtime, &first->when) > 0)
9829b01
+    {
9829b01
+      *delay = 0;
9829b01
+    } else {
9829b01
+      *delay = 1 + first->when.tv_sec - curtime.tv_sec;
9829b01
+      if (first->when.tv_usec < curtime.tv_usec)
9829b01
+	(*delay)--;
9829b01
+    }
9829b01
+  }
9829b01
+
9829b01
+  return (first);
9829b01
+}
9829b01
+
9829b01
+
9829b01
+/*
9829b01
+ * 'cupsdRunTimeout()' - Run a timed callback.
9829b01
+ */
9829b01
+
9829b01
+void
9829b01
+cupsdRunTimeout(cupsd_timeout_t *timeout)	/* I - Timeout */
9829b01
+{
9829b01
+  if (!timeout)
9829b01
+    return;
9829b01
+  timeout->enabled = 0;
9829b01
+  if (!timeout->callback)
9829b01
+    return;
9829b01
+  timeout->callback (timeout, timeout->data);
9829b01
+}
9829b01
+
9829b01
+/*
9829b01
+ * 'cupsdUpdateTimeout()' - Adjust the time of a timed callback or disable it.
9829b01
+ */
9829b01
+
9829b01
+void
9829b01
+cupsdUpdateTimeout(cupsd_timeout_t *timeout,	/* I - Timeout */
9829b01
+		   const struct timeval *tv)	/* I - Absolute time or NULL */
9829b01
+{
9829b01
+  cupsArrayRemove (Timeouts, timeout);
9829b01
+  timeout->enabled = (tv != NULL);
9829b01
+  if (tv)
9829b01
+  {
9829b01
+    timeout->when.tv_sec = tv->tv_sec;
9829b01
+    timeout->when.tv_usec = tv->tv_usec;
9829b01
+  }
9829b01
+  cupsArrayAdd (Timeouts, timeout);
9829b01
+}
9829b01
+
9829b01
+
9829b01
+/*
9829b01
+ * 'cupsdRemoveTimeout()' - Discard a timed callback.
9829b01
+ */
9829b01
+
9829b01
+void
9829b01
+cupsdRemoveTimeout(cupsd_timeout_t *timeout)	/* I - Timeout */
9829b01
+{
9829b01
+  cupsArrayRemove (Timeouts, timeout);
9829b01
+  free (timeout);
9829b01
+}
9829b01
+
9829b01
+
9829b01
+#endif /* HAVE_AVAHI ... from top of file */
9829b01
+
9829b01
+/*
9829b01
+ * End of "$Id$".
9829b01
+ */