summaryrefslogtreecommitdiff
path: root/~mtjm/cups-usblp/cups-avahi-4-poll.patch
blob: 189e83da69c31f251871e0569125e127e364a9e1 (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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
diff -up cups-1.5.0/scheduler/avahi.c.avahi-4-poll cups-1.5.0/scheduler/avahi.c
--- cups-1.5.0/scheduler/avahi.c.avahi-4-poll	2011-10-11 10:56:50.102288037 +0100
+++ cups-1.5.0/scheduler/avahi.c	2011-10-11 10:56:50.102288037 +0100
@@ -0,0 +1,441 @@
+/*
+ * "$Id$"
+ *
+ *   Avahi poll implementation for the CUPS scheduler.
+ *
+ *   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:
+ *
+ *   watch_read_cb         - Read callback for file descriptor
+ *   watch_write_cb        - Write callback for file descriptor
+ *   watched_fd_add_select() - Call cupsdAddSelect() as needed
+ *   watch_new()           - Create a new file descriptor watch
+ *   watch_free()          - Free a file descriptor watch
+ *   watch_update()        - Update watched events for a file descriptor
+ *   watch_get_events()    - Get events that happened for a file descriptor
+ *   timeout_cb()          - Run a timed Avahi callback
+ *   timeout_new()         - Set a wakeup time
+ *   timeout_update()      - Update the expiration time for a timeout
+ *   timeout_free()        - Free a timeout
+ *   compare_watched_fds() - Compare watched file descriptors for array sorting
+ *   avahi_cups_poll_new() - Create a new Avahi main loop object for CUPS
+ *   avahi_cups_poll_free() - Free an Avahi main loop object for CUPS
+ *   avahi_cups_poll_get() - Get the abstract poll API structure
+ */
+
+#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 */
+
+
+typedef struct
+{
+  AvahiCupsPoll *cups_poll;
+
+  int fd;
+  AvahiWatchEvent occurred;
+  cups_array_t *watches;
+} cupsd_watched_fd_t;
+
+struct AvahiWatch
+{
+  cupsd_watched_fd_t *watched_fd;
+
+  AvahiWatchEvent events;
+  AvahiWatchCallback callback;
+  void *userdata;
+};
+
+struct AvahiTimeout
+{
+  AvahiCupsPoll *cups_poll;
+  AvahiTimeoutCallback callback;
+  void *userdata;
+  cupsd_timeout_t *cupsd_timeout;
+};
+
+/*
+ * Local functions...
+ */
+
+static AvahiWatch *	watch_new(const AvahiPoll *api,
+				  int fd,
+				  AvahiWatchEvent events,
+				  AvahiWatchCallback callback,
+				  void *userdata);
+static void		watch_free(AvahiWatch *watch);
+static void		watch_update(AvahiWatch *watch,
+				     AvahiWatchEvent events);
+static AvahiWatchEvent	watch_get_events(AvahiWatch *watch);
+
+
+/*
+ * 'watch_read_cb' - Read callback for file descriptor
+ */
+
+static void
+watch_read_cb (void *userdata)
+{
+  AvahiWatch *watch;
+  cupsd_watched_fd_t *watched_fd = userdata;
+  watched_fd->occurred |= AVAHI_WATCH_IN;
+  for (watch = (AvahiWatch *)cupsArrayFirst(watched_fd->watches);
+       watch;
+       watch = (AvahiWatch *)cupsArrayNext(watched_fd->watches))
+  {
+    if (watch->events & watched_fd->occurred)
+    {
+      (watch->callback) (watch, watched_fd->fd,
+			 AVAHI_WATCH_IN, watch->userdata);
+      watched_fd->occurred &= ~AVAHI_WATCH_IN;
+      break;
+    }
+  }
+}
+
+
+/*
+ * 'watch_write_cb' - Write callback for file descriptor
+ */
+
+static void
+watch_write_cb (void *userdata)
+{
+  AvahiWatch *watch;
+  cupsd_watched_fd_t *watched_fd = userdata;
+  watched_fd->occurred |= AVAHI_WATCH_OUT;
+  for (watch = (AvahiWatch *)cupsArrayFirst(watched_fd->watches);
+       watch;
+       watch = (AvahiWatch *)cupsArrayNext(watched_fd->watches))
+  {
+    if (watch->events & watched_fd->occurred)
+    {
+      (watch->callback) (watch, watched_fd->fd,
+			 AVAHI_WATCH_OUT, watch->userdata);
+      watched_fd->occurred &= ~AVAHI_WATCH_OUT;
+      break;
+    }
+  }
+}
+
+
+/*
+ * 'watched_fd_add_select' - Call cupsdAddSelect() as needed
+ */
+
+static int						/* O - Watches? */
+watched_fd_add_select (cupsd_watched_fd_t *watched_fd)
+{
+  AvahiWatch *watch;
+  cupsd_selfunc_t read_cb = NULL, write_cb = NULL;
+  int any_watches = 0;
+
+  for (watch = (AvahiWatch *)cupsArrayFirst(watched_fd->watches);
+       watch;
+       watch = (AvahiWatch *)cupsArrayNext(watched_fd->watches))
+  {
+    any_watches = 1;
+    if (watch->events & (AVAHI_WATCH_IN |
+			     AVAHI_WATCH_ERR |
+			     AVAHI_WATCH_HUP))
+    {
+      read_cb = (cupsd_selfunc_t)watch_read_cb;
+      if (write_cb != NULL)
+	break;
+    }
+
+    if (watch->events & AVAHI_WATCH_OUT)
+    {
+      write_cb = (cupsd_selfunc_t)watch_write_cb;
+      if (read_cb != NULL)
+	break;
+    }
+  }
+
+  if (read_cb || write_cb)
+    cupsdAddSelect (watched_fd->fd, read_cb, write_cb, watched_fd);
+  else
+    cupsdRemoveSelect (watched_fd->fd);
+
+  return (any_watches);
+}
+
+/*
+ * 'watch_new' - Create a new file descriptor watch
+ */
+
+static AvahiWatch *
+watch_new (const AvahiPoll *api,
+	   int fd,
+	   AvahiWatchEvent events,
+	   AvahiWatchCallback callback,
+	   void *userdata)
+{
+  cupsd_watched_fd_t key, *watched_fd;
+  AvahiCupsPoll *cups_poll = api->userdata;
+  AvahiWatch *watch = malloc(sizeof(AvahiWatch));
+  if (watch == NULL)
+    return (NULL);
+
+  watch->events = events;
+  watch->callback = callback;
+  watch->userdata = userdata;
+
+  key.fd = fd;
+  watched_fd = cupsArrayFind (cups_poll->watched_fds, &key);
+  if (watched_fd == NULL)
+  {
+    watched_fd = malloc(sizeof(cupsd_watched_fd_t));
+    if (watched_fd == NULL)
+    {
+      free (watch);
+      return (NULL);
+    }
+
+    watched_fd->fd = fd;
+    watched_fd->occurred = 0;
+    watched_fd->cups_poll = cups_poll;
+    watched_fd->watches = cupsArrayNew (NULL, NULL);
+    cupsArrayAdd (cups_poll->watched_fds, watched_fd);
+  }
+
+  watch->watched_fd = watched_fd;
+  cupsArrayAdd(watched_fd->watches, watch);
+  watched_fd_add_select (watched_fd);
+  return (watch);
+}
+
+
+/*
+ * 'watch_free' - Free a file descriptor watch
+ */
+
+static void
+watch_free (AvahiWatch *watch)
+{
+  cupsd_watched_fd_t *watched_fd = watch->watched_fd;
+  AvahiCupsPoll *cups_poll = watched_fd->cups_poll;
+
+  cupsArrayRemove (watched_fd->watches, watch);
+  free (watch);
+
+  if (!watched_fd_add_select (watched_fd))
+  {
+    /* No more watches */
+    cupsArrayRemove (cups_poll->watched_fds, watched_fd);
+    free (watched_fd);
+  }
+}
+
+
+/*
+ * 'watch_update' - Update watched events for a file descriptor
+ */
+
+static void
+watch_update (AvahiWatch *watch,
+	      AvahiWatchEvent events)
+{
+  watch->events = events;
+  watched_fd_add_select (watch->watched_fd);
+}
+
+
+/*
+ * 'watch_get_events' - Get events that happened for a file descriptor
+ */
+
+static AvahiWatchEvent
+watch_get_events (AvahiWatch *watch)
+{
+  return (watch->watched_fd->occurred);
+}
+
+
+/*
+ * 'timeout_cb()' - Run a timed Avahi callback
+ */
+
+static void
+timeout_cb (cupsd_timeout_t *cupsd_timeout, void *userdata)
+{
+  AvahiTimeout *timeout = userdata;
+  (timeout->callback) (timeout, timeout->userdata);
+}
+
+
+/*
+ * 'timeout_new' - Set a wakeup time
+ */
+
+static AvahiTimeout *
+timeout_new (const AvahiPoll *api,
+	     const struct timeval *tv,
+	     AvahiTimeoutCallback callback,
+	     void *userdata)
+{
+  AvahiTimeout *timeout;
+  AvahiCupsPoll *cups_poll = api->userdata;
+
+  timeout = malloc(sizeof(AvahiTimeout));
+  if (timeout == NULL)
+    return (NULL);
+
+  timeout->cups_poll = cups_poll;
+  timeout->callback = callback;
+  timeout->userdata = userdata;
+  timeout->cupsd_timeout = cupsdAddTimeout (tv,
+					    (cupsd_timeoutfunc_t)timeout_cb,
+					    timeout);
+  cupsArrayAdd (cups_poll->timeouts, timeout);
+  return (timeout);
+}
+
+
+/*
+ * 'timeout_update' - Update the expiration time for a timeout
+ */
+
+static void
+timeout_update (AvahiTimeout *timeout,
+		const struct timeval *tv)
+{
+  cupsdUpdateTimeout (timeout->cupsd_timeout, tv);
+}
+
+
+/*
+ * ' timeout_free' - Free a timeout
+ */
+
+static void
+timeout_free (AvahiTimeout *timeout)
+{
+  cupsArrayRemove (timeout->cups_poll->timeouts, timeout);
+  cupsdRemoveTimeout (timeout->cupsd_timeout);
+  free (timeout);
+}
+
+
+/*
+ * 'compare_watched_fds' - Compare watched file descriptors for array sorting
+ */
+static int
+compare_watched_fds(cupsd_watched_fd_t *p0,
+		    cupsd_watched_fd_t *p1)
+{
+  /*
+   * Compare by fd (no two elements have the same fd)
+   */
+
+  if (p0->fd == p1->fd)
+    return 0;
+
+  return (p0->fd < p1->fd ? -1 : 1);
+}
+
+
+/*
+ * 'avahi_cups_poll_new' - Create a new Avahi main loop object for CUPS
+ */
+
+AvahiCupsPoll *
+avahi_cups_poll_new (void)
+{
+  AvahiCupsPoll *cups_poll = malloc(sizeof(AvahiCupsPoll));
+  if (cups_poll == NULL)
+    return (NULL);
+
+  cups_poll->watched_fds = cupsArrayNew ((cups_array_func_t)compare_watched_fds,
+					 NULL);
+  cups_poll->timeouts = cupsArrayNew (NULL, NULL);
+
+  cups_poll->api.userdata = cups_poll;
+  cups_poll->api.watch_new = watch_new;
+  cups_poll->api.watch_free = watch_free;
+  cups_poll->api.watch_update = watch_update;
+  cups_poll->api.watch_get_events = watch_get_events;
+
+  cups_poll->api.timeout_new = timeout_new;
+  cups_poll->api.timeout_update = timeout_update;
+  cups_poll->api.timeout_free = timeout_free;
+
+  return (cups_poll);
+}
+
+
+/*
+ * 'avahi_cups_poll_free' - Free an Avahi main loop object for CUPS
+ */
+void
+avahi_cups_poll_free (AvahiCupsPoll *cups_poll)
+{
+  cupsd_watched_fd_t *watched_fd;
+
+  for (watched_fd = (cupsd_watched_fd_t*)cupsArrayFirst(cups_poll->watched_fds);
+       watched_fd;
+       watched_fd = (cupsd_watched_fd_t*)cupsArrayNext(cups_poll->watched_fds))
+    cupsArrayClear (watched_fd->watches);
+
+  cupsArrayClear (cups_poll->watched_fds);
+  cupsArrayClear (cups_poll->timeouts);
+}
+
+
+/*
+ * 'avahi_cups_poll_get' - Get the abstract poll API structure
+ */
+
+const AvahiPoll *
+avahi_cups_poll_get (AvahiCupsPoll *cups_poll)
+{
+  return (&cups_poll->api);
+}
+
+
+#endif /* HAVE_AVAHI ... from top of file */
+
+/*
+ * End of "$Id$".
+ */
diff -up cups-1.5.0/scheduler/avahi.h.avahi-4-poll cups-1.5.0/scheduler/avahi.h
--- cups-1.5.0/scheduler/avahi.h.avahi-4-poll	2011-10-11 10:56:50.102288037 +0100
+++ cups-1.5.0/scheduler/avahi.h	2011-10-11 10:56:50.119287724 +0100
@@ -0,0 +1,69 @@
+/*
+ * "$Id$"
+ *
+ *   Avahi poll implementation for the CUPS scheduler.
+ *
+ *   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.
+ */
+
+#include <config.h>
+
+#ifdef HAVE_AVAHI
+#  include <avahi-client/client.h>
+#  include <avahi-client/publish.h>
+#endif /* HAVE_AVAHI */
+
+#ifdef HAVE_AUTHORIZATION_H
+#  include <Security/Authorization.h>
+#endif /* HAVE_AUTHORIZATION_H */
+
+
+#ifdef HAVE_AVAHI
+typedef struct
+{
+    AvahiPoll api;
+    cups_array_t *watched_fds;
+    cups_array_t *timeouts;
+} AvahiCupsPoll;
+#endif /* HAVE_AVAHI */
+
+/*
+ * Prototypes...
+ */
+
+#ifdef HAVE_AVAHI
+extern AvahiCupsPoll *	avahi_cups_poll_new(void);
+extern void		avahi_cups_poll_free(AvahiCupsPoll *cups_poll);
+extern const AvahiPoll *avahi_cups_poll_get(AvahiCupsPoll *cups_poll);
+#endif /* HAVE_AVAHI */
+
+
+/*
+ * End of "$Id$".
+ */
diff -up cups-1.5.0/scheduler/Makefile.avahi-4-poll cups-1.5.0/scheduler/Makefile
--- cups-1.5.0/scheduler/Makefile.avahi-4-poll	2011-10-11 10:56:45.868365861 +0100
+++ cups-1.5.0/scheduler/Makefile	2011-10-11 10:56:50.101288055 +0100
@@ -17,6 +17,7 @@ include ../Makedefs
 
 CUPSDOBJS =	\
 		auth.o \
+		avahi.o \
 		banners.o \
 		cert.o \
 		classes.o \