summaryrefslogtreecommitdiffstats
path: root/abs/extra/cups/cups-avahi-3-timeouts.patch
blob: daf852a0fda64535365729f8fb89e1bf6461dd8d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
diff -up cups-1.5.2/scheduler/cupsd.h.avahi-3-timeouts cups-1.5.2/scheduler/cupsd.h
--- cups-1.5.2/scheduler/cupsd.h.avahi-3-timeouts	2011-05-11 23:17:34.000000000 +0100
+++ cups-1.5.2/scheduler/cupsd.h	2012-03-14 15:06:36.509476983 +0000
@@ -140,6 +140,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...
@@ -173,6 +182,11 @@ VAR int			Launchd		VALUE(0);
 					/* Running from launchd */
 #endif /* HAVE_LAUNCH_H */
 
+#ifdef HAVE_AVAHI
+VAR cups_array_t *Timeouts;		/* Timed callbacks for main loop */
+#endif /* HAVE_AVAHI */
+
+
 
 /*
  * Prototypes...
@@ -242,6 +256,20 @@ extern void		cupsdStopSelect(void);
 extern void		cupsdStartServer(void);
 extern void		cupsdStopServer(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);
+
 
 /*
  * End of "$Id: cupsd.h 9766 2011-05-11 22:17:34Z mike $".
diff -up cups-1.5.2/scheduler/main.c.avahi-3-timeouts cups-1.5.2/scheduler/main.c
--- cups-1.5.2/scheduler/main.c.avahi-3-timeouts	2012-03-14 15:04:17.655305548 +0000
+++ cups-1.5.2/scheduler/main.c	2012-03-14 15:06:36.511476986 +0000
@@ -146,6 +146,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
@@ -535,6 +539,14 @@ main(int  argc,				/* I - Number of comm
 
   httpInitialize();
 
+#ifdef HAVE_AVAHI
+ /*
+  * Initialize timed callback structures.
+  */
+
+  cupsdInitTimeouts();
+#endif /* HAVE_AVAHI */
+
   cupsdStartServer();
 
  /*
@@ -874,6 +886,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...
@@ -1787,6 +1809,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 */
 
 
  /*
@@ -1829,6 +1855,19 @@ select_timeout(int fds)			/* I - Number
   }
 #endif /* __APPLE__ */
 
+#ifdef HAVE_AVAHI
+ /*
+  * See if there are any scheduled timed callbacks to run.
+  */
+
+  if ((tmo = cupsdNextTimeout(&tmo_delay)) != NULL &&
+      (now + tmo_delay) < timeout)
+  {
+    timeout = tmo_delay;
+    why = "run a timed callback";
+  }
+#endif /* HAVE_AVAHI */
+
  /*
   * Check whether we are accepting new connections...
   */
diff -up cups-1.5.2/scheduler/Makefile.avahi-3-timeouts cups-1.5.2/scheduler/Makefile
--- cups-1.5.2/scheduler/Makefile.avahi-3-timeouts	2012-03-14 15:04:17.685305586 +0000
+++ cups-1.5.2/scheduler/Makefile	2012-03-14 15:06:36.508476980 +0000
@@ -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.5.2/scheduler/timeout.c.avahi-3-timeouts cups-1.5.2/scheduler/timeout.c
--- cups-1.5.2/scheduler/timeout.c.avahi-3-timeouts	2012-03-14 15:06:36.552477037 +0000
+++ cups-1.5.2/scheduler/timeout.c	2012-03-14 15:06:36.552477037 +0000
@@ -0,0 +1,235 @@
+/*
+ * "$Id$"
+ *
+ *   Timeout functions for the Common UNIX Printing System (CUPS).
+ *
+ *   Copyright (C) 2010, 2011 Red Hat, Inc.
+ *   Authors:
+ *     Tim Waugh <twaugh@redhat.com>
+ *
+ *   Redistribution and use in source and binary forms, with or without
+ *   modification, are permitted provided that the following conditions
+ *   are met:
+ *
+ *   Redistributions of source code must retain the above copyright
+ *   notice, this list of conditions and the following disclaimer.
+ *
+ *   Redistributions in binary form must reproduce the above copyright
+ *   notice, this list of conditions and the following disclaimer in the
+ *   documentation and/or other materials provided with the distribution.
+ *
+ *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ *   FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ *   COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
+ *   INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ *   (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+ *   SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ *   HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
+ *   STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ *   ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
+ *   OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ * 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_addrs (void *p0, void *p1)
+{
+  if (p0 == p1)
+    return (0);
+  if (p0 < p1)
+    return (-1);
+  return (1);
+}
+
+static int
+compare_timeouts (cupsd_timeout_t *p0, cupsd_timeout_t *p1)
+{
+  int addrsdiff = compare_addrs (p0, p1);
+  int tvdiff;
+
+  if (addrsdiff == 0)
+    return (0);
+
+  if (!p0->enabled || !p1->enabled)
+  {
+    if (!p0->enabled && !p1->enabled)
+      return (addrsdiff);
+
+    return (p0->enabled ? -1 : 1);
+  }
+
+  tvdiff = avahi_timeval_compare (&p0->when, &p1->when);
+  if (tvdiff != 0)
+    return (tvdiff);
+
+  return (addrsdiff);
+}
+
+
+/*
+ * '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$".
+ */