]> git.pld-linux.org Git - packages/cups.git/blob - cups-avahi-4-poll.patch
- enable dnssd via avahi (using patches from fedora)
[packages/cups.git] / cups-avahi-4-poll.patch
1 diff -up cups-1.5.0/scheduler/avahi.c.avahi-4-poll cups-1.5.0/scheduler/avahi.c
2 --- cups-1.5.0/scheduler/avahi.c.avahi-4-poll   2011-08-05 15:07:09.570033486 +0100
3 +++ cups-1.5.0/scheduler/avahi.c        2011-08-05 15:07:09.570033486 +0100
4 @@ -0,0 +1,445 @@
5 +/*
6 + * "$Id$"
7 + *
8 + *   Avahi poll implementation for the CUPS scheduler.
9 + *
10 + *   Copyright (C) 2010 Red Hat, Inc.
11 + *   Authors:
12 + *    Tim Waugh <twaugh@redhat.com>
13 + *
14 + *   Distribution and use rights are outlined in the file "LICENSE.txt"
15 + *   "LICENSE" which should have been included with this file.  If this
16 + *   file is missing or damaged, see the license at "http://www.cups.org/".
17 + *
18 + * Contents:
19 + *
20 + *   watch_read_cb         - Read callback for file descriptor
21 + *   watch_write_cb        - Write callback for file descriptor
22 + *   watched_fd_add_select() - Call cupsdAddSelect() as needed
23 + *   watch_new()           - Create a new file descriptor watch
24 + *   watch_free()          - Free a file descriptor watch
25 + *   watch_update()        - Update watched events for a file descriptor
26 + *   watch_get_events()    - Get events that happened for a file descriptor
27 + *   timeout_cb()          - Run a timed Avahi callback
28 + *   timeout_new()         - Set a wakeup time
29 + *   timeout_update()      - Update the expiration time for a timeout
30 + *   timeout_free()        - Free a timeout
31 + *   compare_watched_fds() - Compare watched file descriptors for array sorting
32 + *   compare_timeouts()    - Compare timeouts for array sorting
33 + *   avahi_cups_poll_new() - Create a new Avahi main loop object for CUPS
34 + *   avahi_cups_poll_free() - Free an Avahi main loop object for CUPS
35 + *   avahi_cups_poll_get() - Get the abstract poll API structure
36 + */
37 +
38 +#include <config.h>
39 +
40 +#ifdef HAVE_AVAHI /* Applies to entire file... */
41 +
42 +/*
43 + * Include necessary headers...
44 + */
45 +
46 +#include "cupsd.h"
47 +
48 +#if defined(HAVE_MALLOC_H) && defined(HAVE_MALLINFO)
49 +#  include <malloc.h>
50 +#endif /* HAVE_MALLOC_H && HAVE_MALLINFO */
51 +
52 +#ifdef HAVE_AVAHI
53 +#  include <avahi-common/timeval.h>
54 +#endif /* HAVE_AVAHI */
55 +
56 +
57 +typedef struct
58 +{
59 +  AvahiCupsPoll *cups_poll;
60 +
61 +  int fd;
62 +  AvahiWatchEvent occurred;
63 +  cups_array_t *watches;
64 +} cupsd_watched_fd_t;
65 +
66 +struct AvahiWatch
67 +{
68 +  cupsd_watched_fd_t *watched_fd;
69 +
70 +  AvahiWatchEvent events;
71 +  AvahiWatchCallback callback;
72 +  void *userdata;
73 +};
74 +
75 +struct AvahiTimeout
76 +{
77 +  AvahiCupsPoll *cups_poll;
78 +  AvahiTimeoutCallback callback;
79 +  void *userdata;
80 +  cupsd_timeout_t *cupsd_timeout;
81 +};
82 +
83 +/*
84 + * Local functions...
85 + */
86 +
87 +static AvahiWatch *    watch_new(const AvahiPoll *api,
88 +                                 int fd,
89 +                                 AvahiWatchEvent events,
90 +                                 AvahiWatchCallback callback,
91 +                                 void *userdata);
92 +static void            watch_free(AvahiWatch *watch);
93 +static void            watch_update(AvahiWatch *watch,
94 +                                    AvahiWatchEvent events);
95 +static AvahiWatchEvent watch_get_events(AvahiWatch *watch);
96 +static int             compare_watches(AvahiWatch *p0,
97 +                                       AvahiWatch *p1);
98 +
99 +
100 +/*
101 + * 'watch_read_cb' - Read callback for file descriptor
102 + */
103 +
104 +static void
105 +watch_read_cb (void *userdata)
106 +{
107 +  AvahiWatch *watch;
108 +  cupsd_watched_fd_t *watched_fd = userdata;
109 +  watched_fd->occurred |= AVAHI_WATCH_IN;
110 +  for (watch = (AvahiWatch *)cupsArrayFirst(watched_fd->watches);
111 +       watch;
112 +       watch = (AvahiWatch *)cupsArrayNext(watched_fd->watches)) {
113 +    if (watch->events & watched_fd->occurred) {
114 +      (watch->callback) (watch, watched_fd->fd,
115 +                        AVAHI_WATCH_IN, watch->userdata);
116 +      watched_fd->occurred &= ~AVAHI_WATCH_IN;
117 +      break;
118 +    }
119 +  }
120 +}
121 +
122 +
123 +/*
124 + * 'watch_write_cb' - Write callback for file descriptor
125 + */
126 +
127 +static void
128 +watch_write_cb (void *userdata)
129 +{
130 +  AvahiWatch *watch;
131 +  cupsd_watched_fd_t *watched_fd = userdata;
132 +  watched_fd->occurred |= AVAHI_WATCH_OUT;
133 +  for (watch = (AvahiWatch *)cupsArrayFirst(watched_fd->watches);
134 +       watch;
135 +       watch = (AvahiWatch *)cupsArrayNext(watched_fd->watches)) {
136 +    if (watch->events & watched_fd->occurred) {
137 +      (watch->callback) (watch, watched_fd->fd,
138 +                        AVAHI_WATCH_OUT, watch->userdata);
139 +      watched_fd->occurred &= ~AVAHI_WATCH_OUT;
140 +      break;
141 +    }
142 +  }
143 +}
144 +
145 +
146 +/*
147 + * 'watched_fd_add_select' - Call cupsdAddSelect() as needed
148 + */
149 +
150 +static int                                             /* O - Watches? */
151 +watched_fd_add_select (cupsd_watched_fd_t *watched_fd)
152 +{
153 +  AvahiWatch *watch;
154 +  cupsd_selfunc_t read_cb = NULL, write_cb = NULL;
155 +
156 +  for (watch = (AvahiWatch *)cupsArrayFirst(watched_fd->watches);
157 +       watch;
158 +       watch = (AvahiWatch *)cupsArrayNext(watched_fd->watches)) {
159 +    if (watch->events & (AVAHI_WATCH_IN |
160 +                            AVAHI_WATCH_ERR |
161 +                            AVAHI_WATCH_HUP)) {
162 +      read_cb = (cupsd_selfunc_t)watch_read_cb;
163 +      if (write_cb != NULL)
164 +       break;
165 +    }
166 +
167 +    if (watch->events & AVAHI_WATCH_OUT) {
168 +      write_cb = (cupsd_selfunc_t)watch_write_cb;
169 +      if (read_cb != NULL)
170 +       break;
171 +    }
172 +  }
173 +
174 +  if (read_cb || write_cb)
175 +    cupsdAddSelect (watched_fd->fd, read_cb, write_cb, watched_fd);
176 +  else
177 +    cupsdRemoveSelect (watched_fd->fd);
178 +
179 +  return (read_cb || write_cb);
180 +}
181 +
182 +/*
183 + * 'watch_new' - Create a new file descriptor watch
184 + */
185 +
186 +static AvahiWatch *
187 +watch_new (const AvahiPoll *api,
188 +          int fd,
189 +          AvahiWatchEvent events,
190 +          AvahiWatchCallback callback,
191 +          void *userdata)
192 +{
193 +  cupsd_watched_fd_t key, *watched_fd;
194 +  AvahiCupsPoll *cups_poll = api->userdata;
195 +  AvahiWatch *watch = malloc(sizeof(AvahiWatch));
196 +  if (watch == NULL)
197 +    return (NULL);
198 +
199 +  watch->events = events;
200 +  watch->callback = callback;
201 +  watch->userdata = userdata;
202 +
203 +  key.fd = fd;
204 +  watched_fd = cupsArrayFind (cups_poll->watched_fds, &key);
205 +  if (watched_fd == NULL) {
206 +    watched_fd = malloc(sizeof(cupsd_watched_fd_t));
207 +    if (watched_fd == NULL) {
208 +      free (watch);
209 +      return (NULL);
210 +    }
211 +
212 +    watched_fd->fd = fd;
213 +    watched_fd->occurred = 0;
214 +    watched_fd->cups_poll = cups_poll;
215 +    watched_fd->watches = cupsArrayNew ((cups_array_func_t)compare_watches,
216 +                                       NULL);
217 +  }
218 +
219 +  watch->watched_fd = watched_fd;
220 +  cupsArrayAdd(watched_fd->watches, watch);
221 +  watched_fd_add_select (watched_fd);
222 +  return (watch);
223 +}
224 +
225 +
226 +/*
227 + * 'watch_free' - Free a file descriptor watch
228 + */
229 +
230 +static void
231 +watch_free (AvahiWatch *watch)
232 +{
233 +  cupsd_watched_fd_t *watched_fd = watch->watched_fd;
234 +  AvahiCupsPoll *cups_poll = watched_fd->cups_poll;
235 +
236 +  cupsArrayRemove (watched_fd->watches, watch);
237 +  free (watch);
238 +
239 +  if (!watched_fd_add_select (watched_fd)) {
240 +    /* No more watches */
241 +    cupsArrayRemove (cups_poll->watched_fds, watched_fd);
242 +    free (watched_fd);
243 +  }
244 +}
245 +
246 +
247 +/*
248 + * 'watch_update' - Update watched events for a file descriptor
249 + */
250 +
251 +static void
252 +watch_update (AvahiWatch *watch,
253 +             AvahiWatchEvent events)
254 +{
255 +  watch->events = events;
256 +  watched_fd_add_select (watch->watched_fd);
257 +}
258 +
259 +
260 +/*
261 + * 'watch_get_events' - Get events that happened for a file descriptor
262 + */
263 +
264 +static AvahiWatchEvent
265 +watch_get_events (AvahiWatch *watch)
266 +{
267 +  return (watch->watched_fd->occurred);
268 +}
269 +
270 +
271 +/*
272 + * 'compare_watches' - Compare watches for array sorting
273 + */
274 +
275 +static int
276 +compare_watches (AvahiWatch *p0,
277 +                AvahiWatch *p1)
278 +{
279 +  if (p0->watched_fd->fd < p1->watched_fd->fd)
280 +    return (-1);
281 +
282 +  return ((p0->watched_fd->fd == p1->watched_fd->fd) ? 0 : 1);
283 +}
284 +
285 +
286 +/*
287 + * 'timeout_cb()' - Run a timed Avahi callback
288 + */
289 +
290 +static void
291 +timeout_cb (cupsd_timeout_t *cupsd_timeout, void *userdata)
292 +{
293 +  AvahiTimeout *timeout = userdata;
294 +  (timeout->callback) (timeout, timeout->userdata);
295 +}
296 +
297 +
298 +/*
299 + * 'timeout_new' - Set a wakeup time
300 + */
301 +
302 +static AvahiTimeout *
303 +timeout_new (const AvahiPoll *api,
304 +            const struct timeval *tv,
305 +            AvahiTimeoutCallback callback,
306 +            void *userdata)
307 +{
308 +  AvahiTimeout *timeout;
309 +  AvahiCupsPoll *cups_poll = api->userdata;
310 +
311 +  timeout = malloc(sizeof(AvahiTimeout));
312 +  if (timeout == NULL)
313 +    return (NULL);
314 +
315 +  timeout->cups_poll = cups_poll;
316 +  timeout->callback = callback;
317 +  timeout->userdata = userdata;
318 +  timeout->cupsd_timeout = cupsdAddTimeout (tv,
319 +                                           (cupsd_timeoutfunc_t)timeout_cb,
320 +                                           timeout);
321 +  cupsArrayAdd (cups_poll->timeouts, timeout);
322 +  return (timeout);
323 +}
324 +
325 +
326 +/*
327 + * 'timeout_update' - Update the expiration time for a timeout
328 + */
329 +
330 +static void
331 +timeout_update (AvahiTimeout *timeout,
332 +               const struct timeval *tv)
333 +{
334 +  cupsdUpdateTimeout (timeout->cupsd_timeout, tv);
335 +}
336 +
337 +
338 +/*
339 + * ' timeout_free' - Free a timeout
340 + */
341 +
342 +static void
343 +timeout_free (AvahiTimeout *timeout)
344 +{
345 +  cupsArrayRemove (timeout->cups_poll->timeouts, timeout);
346 +  cupsdRemoveTimeout (timeout->cupsd_timeout);
347 +  free (timeout);
348 +}
349 +
350 +
351 +/*
352 + * 'compare_watched_fds' - Compare watched file descriptors for array sorting
353 + */
354 +static int
355 +compare_watched_fds(cupsd_watched_fd_t *p0,
356 +                   cupsd_watched_fd_t *p1)
357 +{
358 +  if (p0->fd != p1->fd)
359 +    return (p0->fd < p1->fd ? -1 : 1);
360 +
361 +  if (p0 == p1)
362 +    return (0);
363 +
364 +  return (p0 < p1 ? -1 : 1);
365 +}
366 +
367 +
368 +/*
369 + * 'compare_timeouts' - Compare timeouts for array sorting
370 + */
371 +static int
372 +compare_timeouts(AvahiTimeout *p0,
373 +                AvahiTimeout *p1)
374 +{
375 + /*
376 +  * Just compare pointers to make it a stable sort.
377 +  */
378 +
379 +  if (p0->cupsd_timeout < p1->cupsd_timeout)
380 +    return (-1);
381 +  return ((p0->cupsd_timeout == p1->cupsd_timeout) ? 0 : 1);
382 +}
383 +
384 +
385 +/*
386 + * 'avahi_cups_poll_new' - Create a new Avahi main loop object for CUPS
387 + */
388 +
389 +AvahiCupsPoll *
390 +avahi_cups_poll_new (void)
391 +{
392 +  AvahiCupsPoll *cups_poll = malloc(sizeof(AvahiCupsPoll));
393 +  if (cups_poll == NULL)
394 +    return (NULL);
395 +
396 +  cups_poll->watched_fds = cupsArrayNew ((cups_array_func_t)compare_watched_fds,
397 +                                        NULL);
398 +  cups_poll->timeouts = cupsArrayNew ((cups_array_func_t)compare_timeouts,
399 +                                     NULL);
400 +
401 +  cups_poll->api.userdata = cups_poll;
402 +  cups_poll->api.watch_new = watch_new;
403 +  cups_poll->api.watch_free = watch_free;
404 +  cups_poll->api.watch_update = watch_update;
405 +  cups_poll->api.watch_get_events = watch_get_events;
406 +
407 +  cups_poll->api.timeout_new = timeout_new;
408 +  cups_poll->api.timeout_update = timeout_update;
409 +  cups_poll->api.timeout_free = timeout_free;
410 +
411 +  return (cups_poll);
412 +}
413 +
414 +
415 +/*
416 + * 'avahi_cups_poll_free' - Free an Avahi main loop object for CUPS
417 + */
418 +void
419 +avahi_cups_poll_free (AvahiCupsPoll *cups_poll)
420 +{
421 +  cupsd_watched_fd_t *watched_fd;
422 +
423 +  for (watched_fd = (cupsd_watched_fd_t*)cupsArrayFirst(cups_poll->watched_fds);
424 +       watched_fd;
425 +       watched_fd = (cupsd_watched_fd_t*)cupsArrayNext(cups_poll->watched_fds)){
426 +    cupsArrayClear (watched_fd->watches);
427 +  }
428 +
429 +  cupsArrayClear (cups_poll->watched_fds);
430 +  cupsArrayClear (cups_poll->timeouts);
431 +}
432 +
433 +
434 +/*
435 + * 'avahi_cups_poll_get' - Get the abstract poll API structure
436 + */
437 +
438 +const AvahiPoll *
439 +avahi_cups_poll_get (AvahiCupsPoll *cups_poll)
440 +{
441 +  return (&cups_poll->api);
442 +}
443 +
444 +
445 +#endif /* HAVE_AVAHI ... from top of file */
446 +
447 +/*
448 + * End of "$Id$".
449 + */
450 diff -up cups-1.5.0/scheduler/avahi.h.avahi-4-poll cups-1.5.0/scheduler/avahi.h
451 --- cups-1.5.0/scheduler/avahi.h.avahi-4-poll   2011-08-05 15:07:09.594032723 +0100
452 +++ cups-1.5.0/scheduler/avahi.h        2011-08-05 15:07:09.595032692 +0100
453 @@ -0,0 +1,49 @@
454 +/*
455 + * "$Id$"
456 + *
457 + *   Avahi poll implementation for the CUPS scheduler.
458 + *
459 + *   Copyright (C) 2010 Red Hat, Inc.
460 + *   Authors:
461 + *    Tim Waugh <twaugh@redhat.com>
462 + *
463 + *   Distribution and use rights are outlined in the file "LICENSE.txt"
464 + *   which should have been included with this file.  If this file is
465 + *   file is missing or damaged, see the license at "http://www.cups.org/".
466 + */
467 +
468 +#include <config.h>
469 +
470 +#ifdef HAVE_AVAHI
471 +#  include <avahi-client/client.h>
472 +#  include <avahi-client/publish.h>
473 +#endif /* HAVE_AVAHI */
474 +
475 +#ifdef HAVE_AUTHORIZATION_H
476 +#  include <Security/Authorization.h>
477 +#endif /* HAVE_AUTHORIZATION_H */
478 +
479 +
480 +#ifdef HAVE_AVAHI
481 +typedef struct
482 +{
483 +    AvahiPoll api;
484 +    cups_array_t *watched_fds;
485 +    cups_array_t *timeouts;
486 +} AvahiCupsPoll;
487 +#endif /* HAVE_AVAHI */
488 +
489 +/*
490 + * Prototypes...
491 + */
492 +
493 +#ifdef HAVE_AVAHI
494 +extern AvahiCupsPoll * avahi_cups_poll_new(void);
495 +extern void            avahi_cups_poll_free(AvahiCupsPoll *cups_poll);
496 +extern const AvahiPoll *avahi_cups_poll_get(AvahiCupsPoll *cups_poll);
497 +#endif /* HAVE_AVAHI */
498 +
499 +
500 +/*
501 + * End of "$Id$".
502 + */
503 diff -up cups-1.5.0/scheduler/Makefile.avahi-4-poll cups-1.5.0/scheduler/Makefile
504 --- cups-1.5.0/scheduler/Makefile.avahi-4-poll  2011-08-05 15:06:48.548700563 +0100
505 +++ cups-1.5.0/scheduler/Makefile       2011-08-05 15:07:09.570033486 +0100
506 @@ -17,6 +17,7 @@ include ../Makedefs
507  
508  CUPSDOBJS =    \
509                 auth.o \
510 +               avahi.o \
511                 banners.o \
512                 cert.o \
513                 classes.o \
This page took 0.077419 seconds and 3 git commands to generate.