]> git.pld-linux.org Git - packages/cups.git/blob - cups-avahi-no-threaded.patch
942bbf5ec7f54971158792e9d85cf9c755133228
[packages/cups.git] / cups-avahi-no-threaded.patch
1 diff -up cups-2.2.5/scheduler/avahi.c.avahi-no-threaded cups-2.2.5/scheduler/avahi.c
2 --- cups-2.2.5/scheduler/avahi.c.avahi-no-threaded      2017-10-17 19:03:00.760881016 +0200
3 +++ cups-2.2.5/scheduler/avahi.c        2017-10-17 19:03:00.760881016 +0200
4 @@ -0,0 +1,441 @@
5 +/*
6 + * "$Id$"
7 + *
8 + *   Avahi poll implementation for the CUPS scheduler.
9 + *
10 + *   Copyright (C) 2010, 2011 Red Hat, Inc.
11 + *   Authors:
12 + *    Tim Waugh <twaugh@redhat.com>
13 + *
14 + *   Redistribution and use in source and binary forms, with or without
15 + *   modification, are permitted provided that the following conditions
16 + *   are met:
17 + *
18 + *   Redistributions of source code must retain the above copyright
19 + *   notice, this list of conditions and the following disclaimer.
20 + *
21 + *   Redistributions in binary form must reproduce the above copyright
22 + *   notice, this list of conditions and the following disclaimer in the
23 + *   documentation and/or other materials provided with the distribution.
24 + *
25 + *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26 + *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27 + *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
28 + *   FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
29 + *   COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
30 + *   INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
31 + *   (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
32 + *   SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 + *   HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
34 + *   STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 + *   ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
36 + *   OF THE POSSIBILITY OF SUCH DAMAGE.
37 + *
38 + * Contents:
39 + *
40 + *   watch_read_cb         - Read callback for file descriptor
41 + *   watch_write_cb        - Write callback for file descriptor
42 + *   watched_fd_add_select() - Call cupsdAddSelect() as needed
43 + *   watch_new()           - Create a new file descriptor watch
44 + *   watch_free()          - Free a file descriptor watch
45 + *   watch_update()        - Update watched events for a file descriptor
46 + *   watch_get_events()    - Get events that happened for a file descriptor
47 + *   timeout_cb()          - Run a timed Avahi callback
48 + *   timeout_new()         - Set a wakeup time
49 + *   timeout_update()      - Update the expiration time for a timeout
50 + *   timeout_free()        - Free a timeout
51 + *   compare_watched_fds() - Compare watched file descriptors for array sorting
52 + *   avahi_cups_poll_new() - Create a new Avahi main loop object for CUPS
53 + *   avahi_cups_poll_free() - Free an Avahi main loop object for CUPS
54 + *   avahi_cups_poll_get() - Get the abstract poll API structure
55 + */
56 +
57 +#include <config.h>
58 +
59 +#ifdef HAVE_AVAHI /* Applies to entire file... */
60 +
61 +/*
62 + * Include necessary headers...
63 + */
64 +
65 +#include "cupsd.h"
66 +
67 +#if defined(HAVE_MALLOC_H) && defined(HAVE_MALLINFO)
68 +#  include <malloc.h>
69 +#endif /* HAVE_MALLOC_H && HAVE_MALLINFO */
70 +
71 +#ifdef HAVE_AVAHI
72 +#  include <avahi-common/timeval.h>
73 +#endif /* HAVE_AVAHI */
74 +
75 +
76 +typedef struct
77 +{
78 +  AvahiCupsPoll *cups_poll;
79 +
80 +  int fd;
81 +  AvahiWatchEvent occurred;
82 +  cups_array_t *watches;
83 +} cupsd_watched_fd_t;
84 +
85 +struct AvahiWatch
86 +{
87 +  cupsd_watched_fd_t *watched_fd;
88 +
89 +  AvahiWatchEvent events;
90 +  AvahiWatchCallback callback;
91 +  void *userdata;
92 +};
93 +
94 +struct AvahiTimeout
95 +{
96 +  AvahiCupsPoll *cups_poll;
97 +  AvahiTimeoutCallback callback;
98 +  void *userdata;
99 +  cupsd_timeout_t *cupsd_timeout;
100 +};
101 +
102 +/*
103 + * Local functions...
104 + */
105 +
106 +static AvahiWatch *    watch_new(const AvahiPoll *api,
107 +                                 int fd,
108 +                                 AvahiWatchEvent events,
109 +                                 AvahiWatchCallback callback,
110 +                                 void *userdata);
111 +static void            watch_free(AvahiWatch *watch);
112 +static void            watch_update(AvahiWatch *watch,
113 +                                    AvahiWatchEvent events);
114 +static AvahiWatchEvent watch_get_events(AvahiWatch *watch);
115 +
116 +
117 +/*
118 + * 'watch_read_cb' - Read callback for file descriptor
119 + */
120 +
121 +static void
122 +watch_read_cb (void *userdata)
123 +{
124 +  AvahiWatch *watch;
125 +  cupsd_watched_fd_t *watched_fd = userdata;
126 +  watched_fd->occurred |= AVAHI_WATCH_IN;
127 +  for (watch = (AvahiWatch *)cupsArrayFirst(watched_fd->watches);
128 +       watch;
129 +       watch = (AvahiWatch *)cupsArrayNext(watched_fd->watches))
130 +  {
131 +    if (watch->events & watched_fd->occurred)
132 +    {
133 +      (watch->callback) (watch, watched_fd->fd,
134 +                        AVAHI_WATCH_IN, watch->userdata);
135 +      watched_fd->occurred &= ~AVAHI_WATCH_IN;
136 +      break;
137 +    }
138 +  }
139 +}
140 +
141 +
142 +/*
143 + * 'watch_write_cb' - Write callback for file descriptor
144 + */
145 +
146 +static void
147 +watch_write_cb (void *userdata)
148 +{
149 +  AvahiWatch *watch;
150 +  cupsd_watched_fd_t *watched_fd = userdata;
151 +  watched_fd->occurred |= AVAHI_WATCH_OUT;
152 +  for (watch = (AvahiWatch *)cupsArrayFirst(watched_fd->watches);
153 +       watch;
154 +       watch = (AvahiWatch *)cupsArrayNext(watched_fd->watches))
155 +  {
156 +    if (watch->events & watched_fd->occurred)
157 +    {
158 +      (watch->callback) (watch, watched_fd->fd,
159 +                        AVAHI_WATCH_OUT, watch->userdata);
160 +      watched_fd->occurred &= ~AVAHI_WATCH_OUT;
161 +      break;
162 +    }
163 +  }
164 +}
165 +
166 +
167 +/*
168 + * 'watched_fd_add_select' - Call cupsdAddSelect() as needed
169 + */
170 +
171 +static int                                             /* O - Watches? */
172 +watched_fd_add_select (cupsd_watched_fd_t *watched_fd)
173 +{
174 +  AvahiWatch *watch;
175 +  cupsd_selfunc_t read_cb = NULL, write_cb = NULL;
176 +  int any_watches = 0;
177 +
178 +  for (watch = (AvahiWatch *)cupsArrayFirst(watched_fd->watches);
179 +       watch;
180 +       watch = (AvahiWatch *)cupsArrayNext(watched_fd->watches))
181 +  {
182 +    any_watches = 1;
183 +    if (watch->events & (AVAHI_WATCH_IN |
184 +                            AVAHI_WATCH_ERR |
185 +                            AVAHI_WATCH_HUP))
186 +    {
187 +      read_cb = (cupsd_selfunc_t)watch_read_cb;
188 +      if (write_cb != NULL)
189 +       break;
190 +    }
191 +
192 +    if (watch->events & AVAHI_WATCH_OUT)
193 +    {
194 +      write_cb = (cupsd_selfunc_t)watch_write_cb;
195 +      if (read_cb != NULL)
196 +       break;
197 +    }
198 +  }
199 +
200 +  if (read_cb || write_cb)
201 +    cupsdAddSelect (watched_fd->fd, read_cb, write_cb, watched_fd);
202 +  else
203 +    cupsdRemoveSelect (watched_fd->fd);
204 +
205 +  return (any_watches);
206 +}
207 +
208 +/*
209 + * 'watch_new' - Create a new file descriptor watch
210 + */
211 +
212 +static AvahiWatch *
213 +watch_new (const AvahiPoll *api,
214 +          int fd,
215 +          AvahiWatchEvent events,
216 +          AvahiWatchCallback callback,
217 +          void *userdata)
218 +{
219 +  cupsd_watched_fd_t key, *watched_fd;
220 +  AvahiCupsPoll *cups_poll = api->userdata;
221 +  AvahiWatch *watch = malloc(sizeof(AvahiWatch));
222 +  if (watch == NULL)
223 +    return (NULL);
224 +
225 +  watch->events = events;
226 +  watch->callback = callback;
227 +  watch->userdata = userdata;
228 +
229 +  key.fd = fd;
230 +  watched_fd = cupsArrayFind (cups_poll->watched_fds, &key);
231 +  if (watched_fd == NULL)
232 +  {
233 +    watched_fd = malloc(sizeof(cupsd_watched_fd_t));
234 +    if (watched_fd == NULL)
235 +    {
236 +      free (watch);
237 +      return (NULL);
238 +    }
239 +
240 +    watched_fd->fd = fd;
241 +    watched_fd->occurred = 0;
242 +    watched_fd->cups_poll = cups_poll;
243 +    watched_fd->watches = cupsArrayNew (NULL, NULL);
244 +    cupsArrayAdd (cups_poll->watched_fds, watched_fd);
245 +  }
246 +
247 +  watch->watched_fd = watched_fd;
248 +  cupsArrayAdd(watched_fd->watches, watch);
249 +  watched_fd_add_select (watched_fd);
250 +  return (watch);
251 +}
252 +
253 +
254 +/*
255 + * 'watch_free' - Free a file descriptor watch
256 + */
257 +
258 +static void
259 +watch_free (AvahiWatch *watch)
260 +{
261 +  cupsd_watched_fd_t *watched_fd = watch->watched_fd;
262 +  AvahiCupsPoll *cups_poll = watched_fd->cups_poll;
263 +
264 +  cupsArrayRemove (watched_fd->watches, watch);
265 +  free (watch);
266 +
267 +  if (!watched_fd_add_select (watched_fd))
268 +  {
269 +    /* No more watches */
270 +    cupsArrayRemove (cups_poll->watched_fds, watched_fd);
271 +    free (watched_fd);
272 +  }
273 +}
274 +
275 +
276 +/*
277 + * 'watch_update' - Update watched events for a file descriptor
278 + */
279 +
280 +static void
281 +watch_update (AvahiWatch *watch,
282 +             AvahiWatchEvent events)
283 +{
284 +  watch->events = events;
285 +  watched_fd_add_select (watch->watched_fd);
286 +}
287 +
288 +
289 +/*
290 + * 'watch_get_events' - Get events that happened for a file descriptor
291 + */
292 +
293 +static AvahiWatchEvent
294 +watch_get_events (AvahiWatch *watch)
295 +{
296 +  return (watch->watched_fd->occurred);
297 +}
298 +
299 +
300 +/*
301 + * 'timeout_cb()' - Run a timed Avahi callback
302 + */
303 +
304 +static void
305 +timeout_cb (cupsd_timeout_t *cupsd_timeout, void *userdata)
306 +{
307 +  AvahiTimeout *timeout = userdata;
308 +  (timeout->callback) (timeout, timeout->userdata);
309 +}
310 +
311 +
312 +/*
313 + * 'timeout_new' - Set a wakeup time
314 + */
315 +
316 +static AvahiTimeout *
317 +timeout_new (const AvahiPoll *api,
318 +            const struct timeval *tv,
319 +            AvahiTimeoutCallback callback,
320 +            void *userdata)
321 +{
322 +  AvahiTimeout *timeout;
323 +  AvahiCupsPoll *cups_poll = api->userdata;
324 +
325 +  timeout = malloc(sizeof(AvahiTimeout));
326 +  if (timeout == NULL)
327 +    return (NULL);
328 +
329 +  timeout->cups_poll = cups_poll;
330 +  timeout->callback = callback;
331 +  timeout->userdata = userdata;
332 +  timeout->cupsd_timeout = cupsdAddTimeout (tv,
333 +                                           (cupsd_timeoutfunc_t)timeout_cb,
334 +                                           timeout);
335 +  cupsArrayAdd (cups_poll->timeouts, timeout);
336 +  return (timeout);
337 +}
338 +
339 +
340 +/*
341 + * 'timeout_update' - Update the expiration time for a timeout
342 + */
343 +
344 +static void
345 +timeout_update (AvahiTimeout *timeout,
346 +               const struct timeval *tv)
347 +{
348 +  cupsdUpdateTimeout (timeout->cupsd_timeout, tv);
349 +}
350 +
351 +
352 +/*
353 + * ' timeout_free' - Free a timeout
354 + */
355 +
356 +static void
357 +timeout_free (AvahiTimeout *timeout)
358 +{
359 +  cupsArrayRemove (timeout->cups_poll->timeouts, timeout);
360 +  cupsdRemoveTimeout (timeout->cupsd_timeout);
361 +  free (timeout);
362 +}
363 +
364 +
365 +/*
366 + * 'compare_watched_fds' - Compare watched file descriptors for array sorting
367 + */
368 +static int
369 +compare_watched_fds(cupsd_watched_fd_t *p0,
370 +                   cupsd_watched_fd_t *p1)
371 +{
372 +  /*
373 +   * Compare by fd (no two elements have the same fd)
374 +   */
375 +
376 +  if (p0->fd == p1->fd)
377 +    return 0;
378 +
379 +  return (p0->fd < p1->fd ? -1 : 1);
380 +}
381 +
382 +
383 +/*
384 + * 'avahi_cups_poll_new' - Create a new Avahi main loop object for CUPS
385 + */
386 +
387 +AvahiCupsPoll *
388 +avahi_cups_poll_new (void)
389 +{
390 +  AvahiCupsPoll *cups_poll = malloc(sizeof(AvahiCupsPoll));
391 +  if (cups_poll == NULL)
392 +    return (NULL);
393 +
394 +  cups_poll->watched_fds = cupsArrayNew ((cups_array_func_t)compare_watched_fds,
395 +                                        NULL);
396 +  cups_poll->timeouts = cupsArrayNew (NULL, NULL);
397 +
398 +  cups_poll->api.userdata = cups_poll;
399 +  cups_poll->api.watch_new = watch_new;
400 +  cups_poll->api.watch_free = watch_free;
401 +  cups_poll->api.watch_update = watch_update;
402 +  cups_poll->api.watch_get_events = watch_get_events;
403 +
404 +  cups_poll->api.timeout_new = timeout_new;
405 +  cups_poll->api.timeout_update = timeout_update;
406 +  cups_poll->api.timeout_free = timeout_free;
407 +
408 +  return (cups_poll);
409 +}
410 +
411 +
412 +/*
413 + * 'avahi_cups_poll_free' - Free an Avahi main loop object for CUPS
414 + */
415 +void
416 +avahi_cups_poll_free (AvahiCupsPoll *cups_poll)
417 +{
418 +  cupsd_watched_fd_t *watched_fd;
419 +
420 +  for (watched_fd = (cupsd_watched_fd_t*)cupsArrayFirst(cups_poll->watched_fds);
421 +       watched_fd;
422 +       watched_fd = (cupsd_watched_fd_t*)cupsArrayNext(cups_poll->watched_fds))
423 +    cupsArrayClear (watched_fd->watches);
424 +
425 +  cupsArrayClear (cups_poll->watched_fds);
426 +  cupsArrayClear (cups_poll->timeouts);
427 +}
428 +
429 +
430 +/*
431 + * 'avahi_cups_poll_get' - Get the abstract poll API structure
432 + */
433 +
434 +const AvahiPoll *
435 +avahi_cups_poll_get (AvahiCupsPoll *cups_poll)
436 +{
437 +  return (&cups_poll->api);
438 +}
439 +
440 +
441 +#endif /* HAVE_AVAHI ... from top of file */
442 +
443 +/*
444 + * End of "$Id$".
445 + */
446 diff -up cups-2.2.5/scheduler/avahi.h.avahi-no-threaded cups-2.2.5/scheduler/avahi.h
447 --- cups-2.2.5/scheduler/avahi.h.avahi-no-threaded      2017-10-17 19:03:00.760881016 +0200
448 +++ cups-2.2.5/scheduler/avahi.h        2017-10-17 19:03:00.760881016 +0200
449 @@ -0,0 +1,69 @@
450 +/*
451 + * "$Id$"
452 + *
453 + *   Avahi poll implementation for the CUPS scheduler.
454 + *
455 + *   Copyright (C) 2010, 2011 Red Hat, Inc.
456 + *   Authors:
457 + *    Tim Waugh <twaugh@redhat.com>
458 + *
459 + *   Redistribution and use in source and binary forms, with or without
460 + *   modification, are permitted provided that the following conditions
461 + *   are met:
462 + *
463 + *   Redistributions of source code must retain the above copyright
464 + *   notice, this list of conditions and the following disclaimer.
465 + *
466 + *   Redistributions in binary form must reproduce the above copyright
467 + *   notice, this list of conditions and the following disclaimer in the
468 + *   documentation and/or other materials provided with the distribution.
469 + *
470 + *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
471 + *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
472 + *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
473 + *   FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
474 + *   COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
475 + *   INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
476 + *   (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
477 + *   SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
478 + *   HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
479 + *   STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
480 + *   ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
481 + *   OF THE POSSIBILITY OF SUCH DAMAGE.
482 + */
483 +
484 +#include <config.h>
485 +
486 +#ifdef HAVE_AVAHI
487 +#  include <avahi-client/client.h>
488 +#  include <avahi-client/publish.h>
489 +#endif /* HAVE_AVAHI */
490 +
491 +#ifdef HAVE_AUTHORIZATION_H
492 +#  include <Security/Authorization.h>
493 +#endif /* HAVE_AUTHORIZATION_H */
494 +
495 +
496 +#ifdef HAVE_AVAHI
497 +typedef struct
498 +{
499 +    AvahiPoll api;
500 +    cups_array_t *watched_fds;
501 +    cups_array_t *timeouts;
502 +} AvahiCupsPoll;
503 +#endif /* HAVE_AVAHI */
504 +
505 +/*
506 + * Prototypes...
507 + */
508 +
509 +#ifdef HAVE_AVAHI
510 +extern AvahiCupsPoll * avahi_cups_poll_new(void);
511 +extern void            avahi_cups_poll_free(AvahiCupsPoll *cups_poll);
512 +extern const AvahiPoll *avahi_cups_poll_get(AvahiCupsPoll *cups_poll);
513 +#endif /* HAVE_AVAHI */
514 +
515 +
516 +/*
517 + * End of "$Id$".
518 + */
519 diff -up cups-2.2.5/scheduler/cupsd.h.avahi-no-threaded cups-2.2.5/scheduler/cupsd.h
520 --- cups-2.2.5/scheduler/cupsd.h.avahi-no-threaded      2017-10-13 20:22:26.000000000 +0200
521 +++ cups-2.2.5/scheduler/cupsd.h        2017-10-17 19:03:00.760881016 +0200
522 @@ -118,6 +118,7 @@ extern const char *cups_hstrerror(int);
523  #include "colorman.h"
524  #include "conf.h"
525  #include "banners.h"
526 +#include "avahi.h"
527  #include "dirsvc.h"
528  #include "network.h"
529  #include "subscriptions.h"
530 @@ -138,6 +139,15 @@ extern const char *cups_hstrerror(int);
531  
532  typedef void (*cupsd_selfunc_t)(void *data);
533  
534 +#ifdef HAVE_AVAHI
535 +/*
536 + * Timeout callback function type...
537 + */
538 +
539 +typedef struct _cupsd_timeout_s cupsd_timeout_t;
540 +typedef void (*cupsd_timeoutfunc_t)(cupsd_timeout_t *timeout, void *data);
541 +#endif /* HAVE_AVAHI */
542 +
543  
544  /*
545   * Globals...
546 @@ -162,6 +172,9 @@ VAR int                     OnDemand        VALUE(0);
547                                         /* Launched on demand */
548  #endif /* HAVE_ONDEMAND */
549  
550 +#ifdef HAVE_AVAHI
551 +VAR cups_array_t *Timeouts;            /* Timed callbacks for main loop */
552 +#endif /* HAVE_AVAHI */
553  
554  /*
555   * Prototypes...
556 @@ -224,3 +237,15 @@ extern void                cupsdStopSelect(void);
557  /* server.c */
558  extern void            cupsdStartServer(void);
559  extern void            cupsdStopServer(void);
560 +
561 +#ifdef HAVE_AVAHI
562 +extern void     cupsdInitTimeouts(void);
563 +extern cupsd_timeout_t *cupsdAddTimeout (const struct timeval *tv,
564 +                                        cupsd_timeoutfunc_t cb,
565 +                                        void *data);
566 +extern cupsd_timeout_t *cupsdNextTimeout (long *delay);
567 +extern void     cupsdRunTimeout (cupsd_timeout_t *timeout);
568 +extern void     cupsdUpdateTimeout (cupsd_timeout_t *timeout,
569 +                                   const struct timeval *tv);
570 +extern void     cupsdRemoveTimeout (cupsd_timeout_t *timeout);
571 +#endif /* HAVE_AVAHI */
572 \ No newline at end of file
573 diff -up cups-2.2.5/scheduler/dirsvc.c.avahi-no-threaded cups-2.2.5/scheduler/dirsvc.c
574 --- cups-2.2.5/scheduler/dirsvc.c.avahi-no-threaded     2017-10-13 20:22:26.000000000 +0200
575 +++ cups-2.2.5/scheduler/dirsvc.c       2017-10-17 19:05:35.938592292 +0200
576 @@ -193,7 +193,7 @@ cupsdStartBrowsing(void)
577      cupsdUpdateDNSSDName();
578  
579  #  else /* HAVE_AVAHI */
580 -    if ((DNSSDMaster = avahi_threaded_poll_new()) == NULL)
581 +    if ((DNSSDMaster = avahi_cups_poll_new()) == NULL)
582      {
583        cupsdLogMessage(CUPSD_LOG_ERROR, "Unable to create DNS-SD thread.");
584  
585 @@ -204,7 +204,7 @@ cupsdStartBrowsing(void)
586      {
587        int error;                       /* Error code, if any */
588  
589 -      DNSSDClient = avahi_client_new(avahi_threaded_poll_get(DNSSDMaster), AVAHI_CLIENT_NO_FAIL, dnssdClientCallback, NULL, &error);
590 +      DNSSDClient = avahi_client_new(avahi_cups_poll_get(DNSSDMaster), AVAHI_CLIENT_NO_FAIL, dnssdClientCallback, NULL, &error);
591  
592        if (DNSSDClient == NULL)
593        {
594 @@ -215,11 +215,9 @@ cupsdStartBrowsing(void)
595          if (FatalErrors & CUPSD_FATAL_BROWSE)
596           cupsdEndProcess(getpid(), 0);
597  
598 -        avahi_threaded_poll_free(DNSSDMaster);
599 +        avahi_cups_poll_free(DNSSDMaster);
600          DNSSDMaster = NULL;
601        }
602 -      else
603 -       avahi_threaded_poll_start(DNSSDMaster);
604      }
605  #  endif /* HAVE_DNSSD */
606    }
607 @@ -635,7 +633,7 @@ dnssdClientCallback(
608           * Renew Avahi client...
609           */
610  
611 -         DNSSDClient = avahi_client_new(avahi_threaded_poll_get(DNSSDMaster), AVAHI_CLIENT_NO_FAIL, dnssdClientCallback, NULL, &error);
612 +         DNSSDClient = avahi_client_new(avahi_cups_poll_get(DNSSDMaster), AVAHI_CLIENT_NO_FAIL, dnssdClientCallback, NULL, &error);
613  
614           if (!DNSSDClient)
615           {
616 @@ -701,13 +699,7 @@ dnssdDeregisterInstance(
617  #  else /* HAVE_AVAHI */
618    if (*srv)
619    {
620 -    if (!from_callback)
621 -      avahi_threaded_poll_lock(DNSSDMaster);
622 -
623      avahi_entry_group_free(*srv);
624 -
625 -    if (!from_callback)
626 -      avahi_threaded_poll_unlock(DNSSDMaster);
627    }
628  #  endif /* HAVE_DNSSD */
629  
630 @@ -1029,16 +1021,10 @@ dnssdRegisterInstance(
631    (void)commit;
632  
633  #  else /* HAVE_AVAHI */
634 -  if (!from_callback)
635 -    avahi_threaded_poll_lock(DNSSDMaster);
636 -
637    if (!*srv)
638      *srv = avahi_entry_group_new(DNSSDClient, dnssdRegisterCallback, NULL);
639    if (!*srv)
640    {
641 -    if (!from_callback)
642 -      avahi_threaded_poll_unlock(DNSSDMaster);
643 -
644      cupsdLogMessage(CUPSD_LOG_WARN, "DNS-SD registration of \"%s\" failed: %s",
645                      name, dnssdErrorString(avahi_client_errno(DNSSDClient)));
646      return (0);
647 @@ -1153,9 +1139,6 @@ dnssdRegisterInstance(
648        cupsdLogMessage(CUPSD_LOG_DEBUG, "DNS-SD commit of \"%s\" failed.",
649                        name);
650    }
651 -
652 -  if (!from_callback)
653 -    avahi_threaded_poll_unlock(DNSSDMaster);
654  #  endif /* HAVE_DNSSD */
655  
656    if (error)
657 @@ -1326,9 +1309,6 @@ dnssdStop(void)
658    DNSSDMaster = NULL;
659  
660  #  else /* HAVE_AVAHI */
661 -  if (DNSSDMaster)
662 -    avahi_threaded_poll_stop(DNSSDMaster);
663 -
664    if (DNSSDClient)
665    {
666      avahi_client_free(DNSSDClient);
667 @@ -1337,7 +1317,7 @@ dnssdStop(void)
668  
669    if (DNSSDMaster)
670    {
671 -    avahi_threaded_poll_free(DNSSDMaster);
672 +    avahi_cups_poll_free(DNSSDMaster);
673      DNSSDMaster = NULL;
674    }
675  #  endif /* HAVE_DNSSD */
676 diff -up cups-2.2.5/scheduler/dirsvc.h.avahi-no-threaded cups-2.2.5/scheduler/dirsvc.h
677 --- cups-2.2.5/scheduler/dirsvc.h.avahi-no-threaded     2017-10-13 20:22:26.000000000 +0200
678 +++ cups-2.2.5/scheduler/dirsvc.h       2017-10-17 19:03:00.761881007 +0200
679 @@ -49,7 +49,7 @@ VAR cups_array_t      *DNSSDPrinters  VALUE(NU
680  VAR DNSServiceRef      DNSSDMaster     VALUE(NULL);
681                                         /* Master DNS-SD service reference */
682  #  else /* HAVE_AVAHI */
683 -VAR AvahiThreadedPoll  *DNSSDMaster    VALUE(NULL);
684 +VAR AvahiCupsPoll      *DNSSDMaster    VALUE(NULL);
685                                         /* Master polling interface for Avahi */
686  VAR AvahiClient                *DNSSDClient    VALUE(NULL);
687                                         /* Client information */
688 diff -up cups-2.2.5/scheduler/main.c.avahi-no-threaded cups-2.2.5/scheduler/main.c
689 --- cups-2.2.5/scheduler/main.c.avahi-no-threaded       2017-10-17 19:03:00.753881074 +0200
690 +++ cups-2.2.5/scheduler/main.c 2017-10-17 19:03:00.761881007 +0200
691 @@ -131,7 +131,10 @@ main(int  argc,                            /* I - Number of comm
692    int                  service_idle_exit;
693                                         /* Idle exit on select timeout? */
694  #endif /* HAVE_ONDEMAND */
695 -
696 +#ifdef HAVE_AVAHI
697 +  cupsd_timeout_t      *tmo;           /* Next scheduled timed callback */
698 +  long                 tmo_delay;      /* Time before it must be called */
699 +#endif /* HAVE_AVAHI */
700  
701  #ifdef HAVE_GETEUID
702   /*
703 @@ -610,6 +613,14 @@ main(int  argc,                            /* I - Number of comm
704  
705    httpInitialize();
706  
707 +#ifdef HAVE_AVAHI
708 + /*
709 +  * Initialize timed callback structures.
710 +  */
711 +
712 +  cupsdInitTimeouts();
713 +#endif /* HAVE_AVAHI */
714 +
715    cupsdStartServer();
716  
717   /*
718 @@ -928,6 +939,16 @@ main(int  argc,                            /* I - Number of comm
719      }
720  #endif /* __APPLE__ */
721  
722 +#ifdef HAVE_AVAHI
723 +   /*
724 +    * If a timed callback is due, run it.
725 +    */
726 +
727 +    tmo = cupsdNextTimeout (&tmo_delay);
728 +    if (tmo && tmo_delay == 0)
729 +      cupsdRunTimeout (tmo);
730 +#endif /* HAVE_AVAHI */
731 +
732  #ifndef __APPLE__
733     /*
734      * Update the network interfaces once a minute...
735 @@ -1632,6 +1653,10 @@ select_timeout(int fds)                  /* I - Number
736    cupsd_job_t          *job;           /* Job information */
737    cupsd_printer_t       *printer;       /* Printer information */
738    const char           *why;           /* Debugging aid */
739 +#ifdef HAVE_AVAHI
740 +  cupsd_timeout_t      *tmo;           /* Timed callback */
741 +  long                 tmo_delay;      /* Seconds before calling it */
742 +#endif /* HAVE_AVAHI */
743  
744  
745    cupsdLogMessage(CUPSD_LOG_DEBUG2, "select_timeout: JobHistoryUpdate=%ld",
746 @@ -1677,6 +1702,19 @@ select_timeout(int fds)                  /* I - Number
747    }
748  #endif /* __APPLE__ */
749  
750 +#ifdef HAVE_AVAHI
751 + /*
752 +  * See if there are any scheduled timed callbacks to run.
753 +  */
754 +
755 +  if ((tmo = cupsdNextTimeout(&tmo_delay)) != NULL &&
756 +      (now + tmo_delay) < timeout)
757 +  {
758 +    timeout = tmo_delay;
759 +    why = "run a timed callback";
760 +  }
761 +#endif /* HAVE_AVAHI */
762 +
763   /*
764    * Check whether we are accepting new connections...
765    */
766 diff -up cups-2.2.5/scheduler/Makefile.avahi-no-threaded cups-2.2.5/scheduler/Makefile
767 --- cups-2.2.5/scheduler/Makefile.avahi-no-threaded     2017-10-13 20:22:26.000000000 +0200
768 +++ cups-2.2.5/scheduler/Makefile       2017-10-17 19:03:00.762880999 +0200
769 @@ -15,6 +15,7 @@ include ../Makedefs
770  
771  CUPSDOBJS =    \
772                 auth.o \
773 +               avahi.o \
774                 banners.o \
775                 cert.o \
776                 classes.o \
777 @@ -38,7 +39,8 @@ CUPSDOBJS =   \
778                 server.o \
779                 statbuf.o \
780                 subscriptions.o \
781 -               sysman.o
782 +               sysman.o \
783 +               timeout.o
784  LIBOBJS =      \
785                 filter.o \
786                 mime.o \
787 diff -up cups-2.2.5/scheduler/timeout.c.avahi-no-threaded cups-2.2.5/scheduler/timeout.c
788 --- cups-2.2.5/scheduler/timeout.c.avahi-no-threaded    2017-10-17 19:03:00.762880999 +0200
789 +++ cups-2.2.5/scheduler/timeout.c      2017-10-17 19:03:00.762880999 +0200
790 @@ -0,0 +1,235 @@
791 +/*
792 + * "$Id$"
793 + *
794 + *   Timeout functions for the Common UNIX Printing System (CUPS).
795 + *
796 + *   Copyright (C) 2010, 2011 Red Hat, Inc.
797 + *   Authors:
798 + *     Tim Waugh <twaugh@redhat.com>
799 + *
800 + *   Redistribution and use in source and binary forms, with or without
801 + *   modification, are permitted provided that the following conditions
802 + *   are met:
803 + *
804 + *   Redistributions of source code must retain the above copyright
805 + *   notice, this list of conditions and the following disclaimer.
806 + *
807 + *   Redistributions in binary form must reproduce the above copyright
808 + *   notice, this list of conditions and the following disclaimer in the
809 + *   documentation and/or other materials provided with the distribution.
810 + *
811 + *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
812 + *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
813 + *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
814 + *   FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
815 + *   COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
816 + *   INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
817 + *   (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
818 + *   SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
819 + *   HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
820 + *   STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
821 + *   ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
822 + *   OF THE POSSIBILITY OF SUCH DAMAGE.
823 + *
824 + * Contents:
825 + *
826 + *   cupsdInitTimeouts()  - Initialise timeout structure.
827 + *   cupsdAddTimeout()    - Add a timed callback.
828 + *   cupsdNextTimeout()   - Find the next enabled timed callback.
829 + *   cupsdUpdateTimeout() - Adjust the time of a timed callback or disable it.
830 + *   cupsdRemoveTimeout() - Discard a timed callback.
831 + *   compare_timeouts()   - Compare timed callbacks for array sorting.
832 + */
833 +
834 +#include <config.h>
835 +
836 +#ifdef HAVE_AVAHI /* Applies to entire file... */
837 +
838 +/*
839 + * Include necessary headers...
840 + */
841 +
842 +#include "cupsd.h"
843 +
844 +#if defined(HAVE_MALLOC_H) && defined(HAVE_MALLINFO)
845 +#  include <malloc.h>
846 +#endif /* HAVE_MALLOC_H && HAVE_MALLINFO */
847 +
848 +#ifdef HAVE_AVAHI
849 +#  include <avahi-common/timeval.h>
850 +#endif /* HAVE_AVAHI */
851 +
852 +
853 +struct _cupsd_timeout_s
854 +{
855 +  struct timeval when;
856 +  int enabled;
857 +  cupsd_timeoutfunc_t callback;
858 +  void *data;
859 +};
860 +
861 +/*
862 + * Local functions...
863 + */
864 +
865 +/*
866 + * 'compare_timeouts()' - Compare timed callbacks for array sorting.
867 + */
868 +
869 +static int
870 +compare_addrs (void *p0, void *p1)
871 +{
872 +  if (p0 == p1)
873 +    return (0);
874 +  if (p0 < p1)
875 +    return (-1);
876 +  return (1);
877 +}
878 +
879 +static int
880 +compare_timeouts (cupsd_timeout_t *p0, cupsd_timeout_t *p1)
881 +{
882 +  int addrsdiff = compare_addrs (p0, p1);
883 +  int tvdiff;
884 +
885 +  if (addrsdiff == 0)
886 +    return (0);
887 +
888 +  if (!p0->enabled || !p1->enabled)
889 +  {
890 +    if (!p0->enabled && !p1->enabled)
891 +      return (addrsdiff);
892 +
893 +    return (p0->enabled ? -1 : 1);
894 +  }
895 +
896 +  tvdiff = avahi_timeval_compare (&p0->when, &p1->when);
897 +  if (tvdiff != 0)
898 +    return (tvdiff);
899 +
900 +  return (addrsdiff);
901 +}
902 +
903 +
904 +/*
905 + * 'cupsdInitTimeouts()' - Initialise timeout structures.
906 + */
907 +
908 +void
909 +cupsdInitTimeouts(void)
910 +{
911 +  Timeouts = cupsArrayNew ((cups_array_func_t)compare_timeouts, NULL);
912 +}
913 +
914 +
915 +/*
916 + * 'cupsdAddTimeout()' - Add a timed callback.
917 + */
918 +
919 +cupsd_timeout_t *                              /* O - Timeout handle */
920 +cupsdAddTimeout(const struct timeval *tv,      /* I - Absolute time */
921 +               cupsd_timeoutfunc_t cb,         /* I - Callback function */
922 +               void *data)                     /* I - User data */
923 +{
924 +  cupsd_timeout_t *timeout;
925 +
926 +  timeout = malloc (sizeof(cupsd_timeout_t));
927 +  if (timeout != NULL)
928 +  {
929 +    timeout->enabled = (tv != NULL);
930 +    if (tv)
931 +    {
932 +      timeout->when.tv_sec = tv->tv_sec;
933 +      timeout->when.tv_usec = tv->tv_usec;
934 +    }
935 +
936 +    timeout->callback = cb;
937 +    timeout->data = data;
938 +    cupsArrayAdd (Timeouts, timeout);
939 +  }
940 +
941 +  return timeout;
942 +}
943 +
944 +
945 +/*
946 + * 'cupsdNextTimeout()' - Find the next enabled timed callback.
947 + */
948 +
949 +cupsd_timeout_t *              /* O - Next enabled timeout or NULL */
950 +cupsdNextTimeout(long *delay)  /* O - Seconds before scheduled */
951 +{
952 +  cupsd_timeout_t *first = cupsArrayFirst (Timeouts);
953 +  struct timeval curtime;
954 +
955 +  if (first && !first->enabled)
956 +    first = NULL;
957 +
958 +  if (first && delay)
959 +  {
960 +    gettimeofday (&curtime, NULL);
961 +    if (avahi_timeval_compare (&curtime, &first->when) > 0)
962 +    {
963 +      *delay = 0;
964 +    } else {
965 +      *delay = 1 + first->when.tv_sec - curtime.tv_sec;
966 +      if (first->when.tv_usec < curtime.tv_usec)
967 +       (*delay)--;
968 +    }
969 +  }
970 +
971 +  return (first);
972 +}
973 +
974 +
975 +/*
976 + * 'cupsdRunTimeout()' - Run a timed callback.
977 + */
978 +
979 +void
980 +cupsdRunTimeout(cupsd_timeout_t *timeout)      /* I - Timeout */
981 +{
982 +  if (!timeout)
983 +    return;
984 +  timeout->enabled = 0;
985 +  if (!timeout->callback)
986 +    return;
987 +  timeout->callback (timeout, timeout->data);
988 +}
989 +
990 +/*
991 + * 'cupsdUpdateTimeout()' - Adjust the time of a timed callback or disable it.
992 + */
993 +
994 +void
995 +cupsdUpdateTimeout(cupsd_timeout_t *timeout,   /* I - Timeout */
996 +                  const struct timeval *tv)    /* I - Absolute time or NULL */
997 +{
998 +  cupsArrayRemove (Timeouts, timeout);
999 +  timeout->enabled = (tv != NULL);
1000 +  if (tv)
1001 +  {
1002 +    timeout->when.tv_sec = tv->tv_sec;
1003 +    timeout->when.tv_usec = tv->tv_usec;
1004 +  }
1005 +  cupsArrayAdd (Timeouts, timeout);
1006 +}
1007 +
1008 +
1009 +/*
1010 + * 'cupsdRemoveTimeout()' - Discard a timed callback.
1011 + */
1012 +
1013 +void
1014 +cupsdRemoveTimeout(cupsd_timeout_t *timeout)   /* I - Timeout */
1015 +{
1016 +  cupsArrayRemove (Timeouts, timeout);
1017 +  free (timeout);
1018 +}
1019 +
1020 +
1021 +#endif /* HAVE_AVAHI ... from top of file */
1022 +
1023 +/*
1024 + * End of "$Id$".
1025 + */
This page took 0.146943 seconds and 2 git commands to generate.