1 diff -up cups-1.5.0/scheduler/cupsd.h.avahi-3-timeouts cups-1.5.0/scheduler/cupsd.h
2 --- cups-1.5.0/scheduler/cupsd.h.avahi-3-timeouts 2011-05-11 23:17:34.000000000 +0100
3 +++ cups-1.5.0/scheduler/cupsd.h 2011-08-05 15:06:13.570811440 +0100
4 @@ -140,6 +140,15 @@ extern const char *cups_hstrerror(int);
6 typedef void (*cupsd_selfunc_t)(void *data);
10 + * Timeout callback function type...
13 +typedef struct _cupsd_timeout_s cupsd_timeout_t;
14 +typedef void (*cupsd_timeoutfunc_t)(cupsd_timeout_t *timeout, void *data);
15 +#endif /* HAVE_AVAHI */
20 @@ -173,6 +182,11 @@ VAR int Launchd VALUE(0);
21 /* Running from launchd */
22 #endif /* HAVE_LAUNCH_H */
25 +VAR cups_array_t *Timeouts; /* Timed callbacks for main loop */
26 +#endif /* HAVE_AVAHI */
32 @@ -242,6 +256,20 @@ extern void cupsdStopSelect(void);
33 extern void cupsdStartServer(void);
34 extern void cupsdStopServer(void);
37 +extern void cupsdInitTimeouts(void);
38 +extern cupsd_timeout_t *cupsdAddTimeout (const struct timeval *tv,
39 + cupsd_timeoutfunc_t cb,
41 +extern cupsd_timeout_t *cupsdNextTimeout (long *delay);
42 +extern void cupsdRunTimeout (cupsd_timeout_t *timeout);
43 +extern void cupsdUpdateTimeout (cupsd_timeout_t *timeout,
44 + const struct timeval *tv);
45 +extern void cupsdRemoveTimeout (cupsd_timeout_t *timeout);
46 +#endif /* HAVE_AVAHI */
48 +extern int cupsdRemoveFile(const char *filename);
53 diff -up cups-1.5.0/scheduler/main.c.avahi-3-timeouts cups-1.5.0/scheduler/main.c
54 --- cups-1.5.0/scheduler/main.c.avahi-3-timeouts 2011-08-05 15:05:45.590700888 +0100
55 +++ cups-1.5.0/scheduler/main.c 2011-08-05 15:06:13.572811372 +0100
56 @@ -146,6 +146,10 @@ main(int argc, /* I - Number of comm
57 int launchd_idle_exit;
58 /* Idle exit on select timeout? */
59 #endif /* HAVE_LAUNCHD */
61 + cupsd_timeout_t *tmo; /* Next scheduled timed callback */
62 + long tmo_delay; /* Time before it must be called */
63 +#endif /* HAVE_AVAHI */
67 @@ -535,6 +539,14 @@ main(int argc, /* I - Number of comm
73 + * Initialize timed callback structures.
76 + cupsdInitTimeouts();
77 +#endif /* HAVE_AVAHI */
82 @@ -874,6 +886,16 @@ main(int argc, /* I - Number of comm
84 #endif /* __APPLE__ */
88 + * If a timed callback is due, run it.
91 + tmo = cupsdNextTimeout (&tmo_delay);
92 + if (tmo && tmo_delay == 0)
93 + cupsdRunTimeout (tmo);
94 +#endif /* HAVE_AVAHI */
98 * Update the network interfaces once a minute...
99 @@ -1787,6 +1809,10 @@ select_timeout(int fds) /* I - Number
100 cupsd_job_t *job; /* Job information */
101 cupsd_subscription_t *sub; /* Subscription information */
102 const char *why; /* Debugging aid */
104 + cupsd_timeout_t *tmo; /* Timed callback */
105 + long tmo_delay; /* Seconds before calling it */
106 +#endif /* HAVE_AVAHI */
110 @@ -1829,6 +1855,19 @@ select_timeout(int fds) /* I - Number
112 #endif /* __APPLE__ */
116 + * See if there are any scheduled timed callbacks to run.
119 + tmo = cupsdNextTimeout (&tmo_delay);
122 + timeout = tmo_delay;
123 + why = "run a timed callback";
125 +#endif /* HAVE_AVAHI */
128 * Check whether we are accepting new connections...
130 diff -up cups-1.5.0/scheduler/Makefile.avahi-3-timeouts cups-1.5.0/scheduler/Makefile
131 --- cups-1.5.0/scheduler/Makefile.avahi-3-timeouts 2011-08-05 15:05:45.673698248 +0100
132 +++ cups-1.5.0/scheduler/Makefile 2011-08-05 15:06:13.569811476 +0100
133 @@ -39,7 +39,8 @@ CUPSDOBJS = \
143 diff -up cups-1.5.0/scheduler/timeout.c.avahi-3-timeouts cups-1.5.0/scheduler/timeout.c
144 --- cups-1.5.0/scheduler/timeout.c.avahi-3-timeouts 2011-08-05 15:06:13.573811341 +0100
145 +++ cups-1.5.0/scheduler/timeout.c 2011-08-05 15:06:13.573811341 +0100
150 + * Timeout functions for the Common UNIX Printing System (CUPS).
152 + * Copyright (C) 2010 Red Hat, Inc.
154 + * Tim Waugh <twaugh@redhat.com>
156 + * Distribution and use rights are outlined in the file "LICENSE.txt"
157 + * which should have been included with this file. If this file is
158 + * file is missing or damaged, see the license at "http://www.cups.org/".
162 + * cupsdInitTimeouts() - Initialise timeout structure.
163 + * cupsdAddTimeout() - Add a timed callback.
164 + * cupsdNextTimeout() - Find the next enabled timed callback.
165 + * cupsdUpdateTimeout() - Adjust the time of a timed callback or disable it.
166 + * cupsdRemoveTimeout() - Discard a timed callback.
167 + * compare_timeouts() - Compare timed callbacks for array sorting.
172 +#ifdef HAVE_AVAHI /* Applies to entire file... */
175 + * Include necessary headers...
180 +#if defined(HAVE_MALLOC_H) && defined(HAVE_MALLINFO)
181 +# include <malloc.h>
182 +#endif /* HAVE_MALLOC_H && HAVE_MALLINFO */
185 +# include <avahi-common/timeval.h>
186 +#endif /* HAVE_AVAHI */
189 +struct _cupsd_timeout_s
191 + struct timeval when;
193 + cupsd_timeoutfunc_t callback;
198 + * Local functions...
202 + * 'compare_timeouts()' - Compare timed callbacks for array sorting.
206 +compare_timeouts (cupsd_timeout_t *p0, cupsd_timeout_t *p1)
208 + if (!p0->enabled || !p1->enabled)
210 + if (!p0->enabled && !p1->enabled)
213 + return (p0->enabled ? -1 : 1);
216 + return (avahi_timeval_compare (&p0->when, &p1->when));
221 + * 'cupsdInitTimeouts()' - Initialise timeout structures.
225 +cupsdInitTimeouts(void)
227 + Timeouts = cupsArrayNew ((cups_array_func_t)compare_timeouts, NULL);
232 + * 'cupsdAddTimeout()' - Add a timed callback.
235 +cupsd_timeout_t * /* O - Timeout handle */
236 +cupsdAddTimeout(const struct timeval *tv, /* I - Absolute time */
237 + cupsd_timeoutfunc_t cb, /* I - Callback function */
238 + void *data) /* I - User data */
240 + cupsd_timeout_t *timeout;
242 + timeout = malloc (sizeof(cupsd_timeout_t));
243 + if (timeout != NULL)
245 + timeout->enabled = (tv != NULL);
248 + timeout->when.tv_sec = tv->tv_sec;
249 + timeout->when.tv_usec = tv->tv_usec;
252 + timeout->callback = cb;
253 + timeout->data = data;
254 + cupsArrayAdd (Timeouts, timeout);
262 + * 'cupsdNextTimeout()' - Find the next enabled timed callback.
265 +cupsd_timeout_t * /* O - Next enabled timeout or NULL */
266 +cupsdNextTimeout(long *delay) /* O - Seconds before scheduled */
268 + cupsd_timeout_t *first = cupsArrayFirst (Timeouts);
269 + struct timeval curtime;
271 + if (first && !first->enabled)
274 + if (first && delay)
276 + gettimeofday (&curtime, NULL);
277 + if (avahi_timeval_compare (&curtime, &first->when) > 0)
281 + *delay = 1 + first->when.tv_sec - curtime.tv_sec;
282 + if (first->when.tv_usec < curtime.tv_usec)
292 + * 'cupsdRunTimeout()' - Run a timed callback.
296 +cupsdRunTimeout(cupsd_timeout_t *timeout) /* I - Timeout */
300 + timeout->enabled = 0;
301 + if (!timeout->callback)
303 + timeout->callback (timeout, timeout->data);
307 + * 'cupsdUpdateTimeout()' - Adjust the time of a timed callback or disable it.
311 +cupsdUpdateTimeout(cupsd_timeout_t *timeout, /* I - Timeout */
312 + const struct timeval *tv) /* I - Absolute time or NULL */
314 + cupsArrayRemove (Timeouts, timeout);
315 + timeout->enabled = (tv != NULL);
318 + timeout->when.tv_sec = tv->tv_sec;
319 + timeout->when.tv_usec = tv->tv_usec;
321 + cupsArrayAdd (Timeouts, timeout);
326 + * 'cupsdRemoveTimeout()' - Discard a timed callback.
330 +cupsdRemoveTimeout(cupsd_timeout_t *timeout) /* I - Timeout */
332 + cupsArrayRemove (Timeouts, timeout);
337 +#endif /* HAVE_AVAHI ... from top of file */