Blob Blame Raw
diff -up cups-1.4.7/scheduler/cupsd.h.avahi-3-timeouts cups-1.4.7/scheduler/cupsd.h
--- cups-1.4.7/scheduler/cupsd.h.avahi-3-timeouts	2011-01-11 07:05:58.000000000 +0000
+++ cups-1.4.7/scheduler/cupsd.h	2011-06-28 12:54:06.906728322 +0100
@@ -147,6 +147,15 @@ extern const char *cups_hstrerror(int);
 
 typedef void (*cupsd_selfunc_t)(void *data);
 
+#ifdef HAVE_AVAHI
+/*
+ * Timeout callback function type...
+ */
+
+typedef struct _cupsd_timeout_s cupsd_timeout_t;
+typedef void (*cupsd_timeoutfunc_t)(cupsd_timeout_t *timeout, void *data);
+#endif /* HAVE_AVAHI */
+
 
 /*
  * Globals...
@@ -188,6 +197,9 @@ VAR PSQUpdateQuotaProcPtr PSQUpdateQuota
 					/* Apple PrintService quota function */
 #endif /* __APPLE__ && HAVE_DLFCN_H */
 
+#ifdef HAVE_AVAHI
+VAR cups_array_t *Timeouts;		/* Timed callbacks for main loop */
+#endif /* HAVE_AVAHI */
 
 
 
@@ -241,6 +253,18 @@ extern void	cupsdRemoveSelect(int fd);
 extern void	cupsdStartSelect(void);
 extern void	cupsdStopSelect(void);
 
+#ifdef HAVE_AVAHI
+extern void     cupsdInitTimeouts(void);
+extern cupsd_timeout_t *cupsdAddTimeout (const struct timeval *tv,
+					 cupsd_timeoutfunc_t cb,
+					 void *data);
+extern cupsd_timeout_t *cupsdNextTimeout (long *delay);
+extern void     cupsdRunTimeout (cupsd_timeout_t *timeout);
+extern void     cupsdUpdateTimeout (cupsd_timeout_t *timeout,
+				    const struct timeval *tv);
+extern void     cupsdRemoveTimeout (cupsd_timeout_t *timeout);
+#endif /* HAVE_AVAHI */
+
 extern int	cupsdRemoveFile(const char *filename);
 
 
diff -up cups-1.4.7/scheduler/main.c.avahi-3-timeouts cups-1.4.7/scheduler/main.c
--- cups-1.4.7/scheduler/main.c.avahi-3-timeouts	2011-06-28 12:51:40.307369673 +0100
+++ cups-1.4.7/scheduler/main.c	2011-06-28 12:54:06.909728268 +0100
@@ -161,6 +161,10 @@ main(int  argc,				/* I - Number of comm
   int			launchd_idle_exit;
 					/* Idle exit on select timeout? */
 #endif	/* HAVE_LAUNCHD */
+#ifdef HAVE_AVAHI
+  cupsd_timeout_t	*tmo;		/* Next scheduled timed callback */
+  long			tmo_delay;	/* Time before it must be called */
+#endif /* HAVE_AVAHI */
 
 
 #ifdef HAVE_GETEUID
@@ -561,6 +565,14 @@ main(int  argc,				/* I - Number of comm
 
   httpInitialize();
 
+#ifdef HAVE_AVAHI
+ /*
+  * Initialize timed callback structures.
+  */
+
+  cupsdInitTimeouts();
+#endif /* HAVE_AVAHI */
+
   cupsdStartServer();
 
  /*
@@ -900,6 +912,16 @@ main(int  argc,				/* I - Number of comm
     }
 #endif /* __APPLE__ */
 
+#ifdef HAVE_AVAHI
+   /*
+    * If a timed callback is due, run it.
+    */
+
+    tmo = cupsdNextTimeout (&tmo_delay);
+    if (tmo && tmo_delay == 0)
+      cupsdRunTimeout (tmo);
+#endif /* HAVE_AVAHI */
+
 #ifndef __APPLE__
    /*
     * Update the network interfaces once a minute...
@@ -1925,6 +1947,10 @@ select_timeout(int fds)			/* I - Number 
   cupsd_job_t		*job;		/* Job information */
   cupsd_subscription_t	*sub;		/* Subscription information */
   const char		*why;		/* Debugging aid */
+#ifdef HAVE_AVAHI
+  cupsd_timeout_t	*tmo;		/* Timed callback */
+  long			tmo_delay;	/* Seconds before calling it */
+#endif /* HAVE_AVAHI */
 
 
  /*
@@ -1967,6 +1993,19 @@ select_timeout(int fds)			/* I - Number 
   }
 #endif /* __APPLE__ */
 
+#ifdef HAVE_AVAHI
+ /*
+  * See if there are any scheduled timed callbacks to run.
+  */
+
+  tmo = cupsdNextTimeout (&tmo_delay);
+  if (tmo)
+  {
+    timeout = tmo_delay;
+    why = "run a timed callback";
+  }
+#endif /* HAVE_AVAHI */
+
  /*
   * Check whether we are accepting new connections...
   */
diff -up cups-1.4.7/scheduler/Makefile.avahi-3-timeouts cups-1.4.7/scheduler/Makefile
--- cups-1.4.7/scheduler/Makefile.avahi-3-timeouts	2011-06-28 12:51:40.475366647 +0100
+++ cups-1.4.7/scheduler/Makefile	2011-06-28 12:54:06.906728322 +0100
@@ -39,7 +39,8 @@ CUPSDOBJS =	\
 		server.o \
 		statbuf.o \
 		subscriptions.o \
-		sysman.o
+		sysman.o \
+		timeout.o
 LIBOBJS =	\
 		filter.o \
 		mime.o \
diff -up cups-1.4.7/scheduler/timeout.c.avahi-3-timeouts cups-1.4.7/scheduler/timeout.c
--- cups-1.4.7/scheduler/timeout.c.avahi-3-timeouts	2011-06-28 12:54:06.910728250 +0100
+++ cups-1.4.7/scheduler/timeout.c	2011-06-28 12:54:06.910728250 +0100
@@ -0,0 +1,195 @@
+/*
+ * "$Id$"
+ *
+ *   Timeout functions for the Common UNIX Printing System (CUPS).
+ *
+ *   Copyright (C) 2010 Red Hat, Inc.
+ *   Authors:
+ *     Tim Waugh <twaugh@redhat.com>
+ *
+ *   Distribution and use rights are outlined in the file "LICENSE.txt"
+ *   which should have been included with this file.  If this file is
+ *   file is missing or damaged, see the license at "http://www.cups.org/".
+ *
+ * Contents:
+ *
+ *   cupsdInitTimeouts()  - Initialise timeout structure.
+ *   cupsdAddTimeout()    - Add a timed callback.
+ *   cupsdNextTimeout()   - Find the next enabled timed callback.
+ *   cupsdUpdateTimeout() - Adjust the time of a timed callback or disable it.
+ *   cupsdRemoveTimeout() - Discard a timed callback.
+ *   compare_timeouts()   - Compare timed callbacks for array sorting.
+ */
+
+#include <config.h>
+
+#ifdef HAVE_AVAHI /* Applies to entire file... */
+
+/*
+ * Include necessary headers...
+ */
+
+#include "cupsd.h"
+
+#if defined(HAVE_MALLOC_H) && defined(HAVE_MALLINFO)
+#  include <malloc.h>
+#endif /* HAVE_MALLOC_H && HAVE_MALLINFO */
+
+#ifdef HAVE_AVAHI
+#  include <avahi-common/timeval.h>
+#endif /* HAVE_AVAHI */
+
+
+struct _cupsd_timeout_s
+{
+  struct timeval when;
+  int enabled;
+  cupsd_timeoutfunc_t callback;
+  void *data;
+};
+
+/*
+ * Local functions...
+ */
+
+/*
+ * 'compare_timeouts()' - Compare timed callbacks for array sorting.
+ */
+
+static int
+compare_timeouts (cupsd_timeout_t *p0, cupsd_timeout_t *p1)
+{
+  if (!p0->enabled || !p1->enabled)
+  {
+    if (!p0->enabled && !p1->enabled)
+      return (0);
+
+    return (p0->enabled ? -1 : 1);
+  }
+
+  return (avahi_timeval_compare (&p0->when, &p1->when));
+}
+
+
+/*
+ * 'cupsdInitTimeouts()' - Initialise timeout structures.
+ */
+
+void
+cupsdInitTimeouts(void)
+{
+  Timeouts = cupsArrayNew ((cups_array_func_t)compare_timeouts, NULL);
+}
+
+
+/*
+ * 'cupsdAddTimeout()' - Add a timed callback.
+ */
+
+cupsd_timeout_t *				/* O - Timeout handle */
+cupsdAddTimeout(const struct timeval *tv,	/* I - Absolute time */
+		cupsd_timeoutfunc_t cb,		/* I - Callback function */
+		void *data)			/* I - User data */
+{
+  cupsd_timeout_t *timeout;
+
+  timeout = malloc (sizeof(cupsd_timeout_t));
+  if (timeout != NULL)
+  {
+    timeout->enabled = (tv != NULL);
+    if (tv)
+    {
+      timeout->when.tv_sec = tv->tv_sec;
+      timeout->when.tv_usec = tv->tv_usec;
+    }
+
+    timeout->callback = cb;
+    timeout->data = data;
+    cupsArrayAdd (Timeouts, timeout);
+  }
+
+  return timeout;
+}
+
+
+/*
+ * 'cupsdNextTimeout()' - Find the next enabled timed callback.
+ */
+
+cupsd_timeout_t *		/* O - Next enabled timeout or NULL */
+cupsdNextTimeout(long *delay)	/* O - Seconds before scheduled */
+{
+  cupsd_timeout_t *first = cupsArrayFirst (Timeouts);
+  struct timeval curtime;
+
+  if (first && !first->enabled)
+    first = NULL;
+
+  if (first && delay)
+  {
+    gettimeofday (&curtime, NULL);
+    if (avahi_timeval_compare (&curtime, &first->when) > 0)
+    {
+      *delay = 0;
+    } else {
+      *delay = 1 + first->when.tv_sec - curtime.tv_sec;
+      if (first->when.tv_usec < curtime.tv_usec)
+	(*delay)--;
+    }
+  }
+
+  return (first);
+}
+
+
+/*
+ * 'cupsdRunTimeout()' - Run a timed callback.
+ */
+
+void
+cupsdRunTimeout(cupsd_timeout_t *timeout)	/* I - Timeout */
+{
+  if (!timeout)
+    return;
+  timeout->enabled = 0;
+  if (!timeout->callback)
+    return;
+  timeout->callback (timeout, timeout->data);
+}
+
+/*
+ * 'cupsdUpdateTimeout()' - Adjust the time of a timed callback or disable it.
+ */
+
+void
+cupsdUpdateTimeout(cupsd_timeout_t *timeout,	/* I - Timeout */
+		   const struct timeval *tv)	/* I - Absolute time or NULL */
+{
+  cupsArrayRemove (Timeouts, timeout);
+  timeout->enabled = (tv != NULL);
+  if (tv)
+  {
+    timeout->when.tv_sec = tv->tv_sec;
+    timeout->when.tv_usec = tv->tv_usec;
+  }
+  cupsArrayAdd (Timeouts, timeout);
+}
+
+
+/*
+ * 'cupsdRemoveTimeout()' - Discard a timed callback.
+ */
+
+void
+cupsdRemoveTimeout(cupsd_timeout_t *timeout)	/* I - Timeout */
+{
+  cupsArrayRemove (Timeouts, timeout);
+  free (timeout);
+}
+
+
+#endif /* HAVE_AVAHI ... from top of file */
+
+/*
+ * End of "$Id$".
+ */