1
Description: Don't use D-Bus from two threads
2
Author: Tim Waugh <twaugh@redhat.com>
3
Bug-Redhat: https://bugzilla.redhat.com/show_bug.cgi?id=979748
4
Last-Updated: 2013-11-28
6
+++ b/scheduler/avahi.c
11
+ * Avahi poll implementation for the CUPS scheduler.
13
+ * Copyright (C) 2010, 2011 Red Hat, Inc.
15
+ * Tim Waugh <twaugh@redhat.com>
17
+ * Redistribution and use in source and binary forms, with or without
18
+ * modification, are permitted provided that the following conditions
21
+ * Redistributions of source code must retain the above copyright
22
+ * notice, this list of conditions and the following disclaimer.
24
+ * Redistributions in binary form must reproduce the above copyright
25
+ * notice, this list of conditions and the following disclaimer in the
26
+ * documentation and/or other materials provided with the distribution.
28
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
29
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
30
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
31
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
32
+ * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
33
+ * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
34
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
35
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
37
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
39
+ * OF THE POSSIBILITY OF SUCH DAMAGE.
43
+ * watch_read_cb - Read callback for file descriptor
44
+ * watch_write_cb - Write callback for file descriptor
45
+ * watched_fd_add_select() - Call cupsdAddSelect() as needed
46
+ * watch_new() - Create a new file descriptor watch
47
+ * watch_free() - Free a file descriptor watch
48
+ * watch_update() - Update watched events for a file descriptor
49
+ * watch_get_events() - Get events that happened for a file descriptor
50
+ * timeout_cb() - Run a timed Avahi callback
51
+ * timeout_new() - Set a wakeup time
52
+ * timeout_update() - Update the expiration time for a timeout
53
+ * timeout_free() - Free a timeout
54
+ * compare_watched_fds() - Compare watched file descriptors for array sorting
55
+ * avahi_cups_poll_new() - Create a new Avahi main loop object for CUPS
56
+ * avahi_cups_poll_free() - Free an Avahi main loop object for CUPS
57
+ * avahi_cups_poll_get() - Get the abstract poll API structure
62
+#ifdef HAVE_AVAHI /* Applies to entire file... */
65
+ * Include necessary headers...
70
+#if defined(HAVE_MALLOC_H) && defined(HAVE_MALLINFO)
72
+#endif /* HAVE_MALLOC_H && HAVE_MALLINFO */
75
+# include <avahi-common/timeval.h>
76
+#endif /* HAVE_AVAHI */
81
+ AvahiCupsPoll *cups_poll;
84
+ AvahiWatchEvent occurred;
85
+ cups_array_t *watches;
86
+} cupsd_watched_fd_t;
90
+ cupsd_watched_fd_t *watched_fd;
92
+ AvahiWatchEvent events;
93
+ AvahiWatchCallback callback;
99
+ AvahiCupsPoll *cups_poll;
100
+ AvahiTimeoutCallback callback;
102
+ cupsd_timeout_t *cupsd_timeout;
106
+ * Local functions...
109
+static AvahiWatch * watch_new(const AvahiPoll *api,
111
+ AvahiWatchEvent events,
112
+ AvahiWatchCallback callback,
114
+static void watch_free(AvahiWatch *watch);
115
+static void watch_update(AvahiWatch *watch,
116
+ AvahiWatchEvent events);
117
+static AvahiWatchEvent watch_get_events(AvahiWatch *watch);
121
+ * 'watch_read_cb' - Read callback for file descriptor
125
+watch_read_cb (void *userdata)
128
+ cupsd_watched_fd_t *watched_fd = userdata;
129
+ watched_fd->occurred |= AVAHI_WATCH_IN;
130
+ for (watch = (AvahiWatch *)cupsArrayFirst(watched_fd->watches);
132
+ watch = (AvahiWatch *)cupsArrayNext(watched_fd->watches))
134
+ if (watch->events & watched_fd->occurred)
136
+ (watch->callback) (watch, watched_fd->fd,
137
+ AVAHI_WATCH_IN, watch->userdata);
138
+ watched_fd->occurred &= ~AVAHI_WATCH_IN;
146
+ * 'watch_write_cb' - Write callback for file descriptor
150
+watch_write_cb (void *userdata)
153
+ cupsd_watched_fd_t *watched_fd = userdata;
154
+ watched_fd->occurred |= AVAHI_WATCH_OUT;
155
+ for (watch = (AvahiWatch *)cupsArrayFirst(watched_fd->watches);
157
+ watch = (AvahiWatch *)cupsArrayNext(watched_fd->watches))
159
+ if (watch->events & watched_fd->occurred)
161
+ (watch->callback) (watch, watched_fd->fd,
162
+ AVAHI_WATCH_OUT, watch->userdata);
163
+ watched_fd->occurred &= ~AVAHI_WATCH_OUT;
171
+ * 'watched_fd_add_select' - Call cupsdAddSelect() as needed
174
+static int /* O - Watches? */
175
+watched_fd_add_select (cupsd_watched_fd_t *watched_fd)
178
+ cupsd_selfunc_t read_cb = NULL, write_cb = NULL;
179
+ int any_watches = 0;
181
+ for (watch = (AvahiWatch *)cupsArrayFirst(watched_fd->watches);
183
+ watch = (AvahiWatch *)cupsArrayNext(watched_fd->watches))
186
+ if (watch->events & (AVAHI_WATCH_IN |
190
+ read_cb = (cupsd_selfunc_t)watch_read_cb;
191
+ if (write_cb != NULL)
195
+ if (watch->events & AVAHI_WATCH_OUT)
197
+ write_cb = (cupsd_selfunc_t)watch_write_cb;
198
+ if (read_cb != NULL)
203
+ if (read_cb || write_cb)
204
+ cupsdAddSelect (watched_fd->fd, read_cb, write_cb, watched_fd);
206
+ cupsdRemoveSelect (watched_fd->fd);
208
+ return (any_watches);
212
+ * 'watch_new' - Create a new file descriptor watch
216
+watch_new (const AvahiPoll *api,
218
+ AvahiWatchEvent events,
219
+ AvahiWatchCallback callback,
222
+ cupsd_watched_fd_t key, *watched_fd;
223
+ AvahiCupsPoll *cups_poll = api->userdata;
224
+ AvahiWatch *watch = malloc(sizeof(AvahiWatch));
228
+ watch->events = events;
229
+ watch->callback = callback;
230
+ watch->userdata = userdata;
233
+ watched_fd = cupsArrayFind (cups_poll->watched_fds, &key);
234
+ if (watched_fd == NULL)
236
+ watched_fd = malloc(sizeof(cupsd_watched_fd_t));
237
+ if (watched_fd == NULL)
243
+ watched_fd->fd = fd;
244
+ watched_fd->occurred = 0;
245
+ watched_fd->cups_poll = cups_poll;
246
+ watched_fd->watches = cupsArrayNew (NULL, NULL);
247
+ cupsArrayAdd (cups_poll->watched_fds, watched_fd);
250
+ watch->watched_fd = watched_fd;
251
+ cupsArrayAdd(watched_fd->watches, watch);
252
+ watched_fd_add_select (watched_fd);
258
+ * 'watch_free' - Free a file descriptor watch
262
+watch_free (AvahiWatch *watch)
264
+ cupsd_watched_fd_t *watched_fd = watch->watched_fd;
265
+ AvahiCupsPoll *cups_poll = watched_fd->cups_poll;
267
+ cupsArrayRemove (watched_fd->watches, watch);
270
+ if (!watched_fd_add_select (watched_fd))
272
+ /* No more watches */
273
+ cupsArrayRemove (cups_poll->watched_fds, watched_fd);
280
+ * 'watch_update' - Update watched events for a file descriptor
284
+watch_update (AvahiWatch *watch,
285
+ AvahiWatchEvent events)
287
+ watch->events = events;
288
+ watched_fd_add_select (watch->watched_fd);
293
+ * 'watch_get_events' - Get events that happened for a file descriptor
296
+static AvahiWatchEvent
297
+watch_get_events (AvahiWatch *watch)
299
+ return (watch->watched_fd->occurred);
304
+ * 'timeout_cb()' - Run a timed Avahi callback
308
+timeout_cb (cupsd_timeout_t *cupsd_timeout, void *userdata)
310
+ AvahiTimeout *timeout = userdata;
311
+ (timeout->callback) (timeout, timeout->userdata);
316
+ * 'timeout_new' - Set a wakeup time
319
+static AvahiTimeout *
320
+timeout_new (const AvahiPoll *api,
321
+ const struct timeval *tv,
322
+ AvahiTimeoutCallback callback,
325
+ AvahiTimeout *timeout;
326
+ AvahiCupsPoll *cups_poll = api->userdata;
328
+ timeout = malloc(sizeof(AvahiTimeout));
329
+ if (timeout == NULL)
332
+ timeout->cups_poll = cups_poll;
333
+ timeout->callback = callback;
334
+ timeout->userdata = userdata;
335
+ timeout->cupsd_timeout = cupsdAddTimeout (tv,
336
+ (cupsd_timeoutfunc_t)timeout_cb,
338
+ cupsArrayAdd (cups_poll->timeouts, timeout);
344
+ * 'timeout_update' - Update the expiration time for a timeout
348
+timeout_update (AvahiTimeout *timeout,
349
+ const struct timeval *tv)
351
+ cupsdUpdateTimeout (timeout->cupsd_timeout, tv);
356
+ * ' timeout_free' - Free a timeout
360
+timeout_free (AvahiTimeout *timeout)
362
+ cupsArrayRemove (timeout->cups_poll->timeouts, timeout);
363
+ cupsdRemoveTimeout (timeout->cupsd_timeout);
369
+ * 'compare_watched_fds' - Compare watched file descriptors for array sorting
372
+compare_watched_fds(cupsd_watched_fd_t *p0,
373
+ cupsd_watched_fd_t *p1)
376
+ * Compare by fd (no two elements have the same fd)
379
+ if (p0->fd == p1->fd)
382
+ return (p0->fd < p1->fd ? -1 : 1);
387
+ * 'avahi_cups_poll_new' - Create a new Avahi main loop object for CUPS
391
+avahi_cups_poll_new (void)
393
+ AvahiCupsPoll *cups_poll = malloc(sizeof(AvahiCupsPoll));
394
+ if (cups_poll == NULL)
397
+ cups_poll->watched_fds = cupsArrayNew ((cups_array_func_t)compare_watched_fds,
399
+ cups_poll->timeouts = cupsArrayNew (NULL, NULL);
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;
407
+ cups_poll->api.timeout_new = timeout_new;
408
+ cups_poll->api.timeout_update = timeout_update;
409
+ cups_poll->api.timeout_free = timeout_free;
411
+ return (cups_poll);
416
+ * 'avahi_cups_poll_free' - Free an Avahi main loop object for CUPS
419
+avahi_cups_poll_free (AvahiCupsPoll *cups_poll)
421
+ cupsd_watched_fd_t *watched_fd;
423
+ for (watched_fd = (cupsd_watched_fd_t*)cupsArrayFirst(cups_poll->watched_fds);
425
+ watched_fd = (cupsd_watched_fd_t*)cupsArrayNext(cups_poll->watched_fds))
426
+ cupsArrayClear (watched_fd->watches);
428
+ cupsArrayClear (cups_poll->watched_fds);
429
+ cupsArrayClear (cups_poll->timeouts);
434
+ * 'avahi_cups_poll_get' - Get the abstract poll API structure
438
+avahi_cups_poll_get (AvahiCupsPoll *cups_poll)
440
+ return (&cups_poll->api);
444
+#endif /* HAVE_AVAHI ... from top of file */
450
+++ b/scheduler/avahi.h
455
+ * Avahi poll implementation for the CUPS scheduler.
457
+ * Copyright (C) 2010, 2011 Red Hat, Inc.
459
+ * Tim Waugh <twaugh@redhat.com>
461
+ * Redistribution and use in source and binary forms, with or without
462
+ * modification, are permitted provided that the following conditions
465
+ * Redistributions of source code must retain the above copyright
466
+ * notice, this list of conditions and the following disclaimer.
468
+ * Redistributions in binary form must reproduce the above copyright
469
+ * notice, this list of conditions and the following disclaimer in the
470
+ * documentation and/or other materials provided with the distribution.
472
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
473
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
474
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
475
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
476
+ * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
477
+ * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
478
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
479
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
480
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
481
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
482
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
483
+ * OF THE POSSIBILITY OF SUCH DAMAGE.
489
+# include <avahi-client/client.h>
490
+# include <avahi-client/publish.h>
491
+#endif /* HAVE_AVAHI */
493
+#ifdef HAVE_AUTHORIZATION_H
494
+# include <Security/Authorization.h>
495
+#endif /* HAVE_AUTHORIZATION_H */
502
+ cups_array_t *watched_fds;
503
+ cups_array_t *timeouts;
505
+#endif /* HAVE_AVAHI */
512
+extern AvahiCupsPoll * avahi_cups_poll_new(void);
513
+extern void avahi_cups_poll_free(AvahiCupsPoll *cups_poll);
514
+extern const AvahiPoll *avahi_cups_poll_get(AvahiCupsPoll *cups_poll);
515
+#endif /* HAVE_AVAHI */
521
--- a/scheduler/cupsd.h
522
+++ b/scheduler/cupsd.h
524
#include "colorman.h"
530
#include "subscriptions.h"
533
typedef void (*cupsd_selfunc_t)(void *data);
537
+ * Timeout callback function type...
540
+typedef struct _cupsd_timeout_s cupsd_timeout_t;
541
+typedef void (*cupsd_timeoutfunc_t)(cupsd_timeout_t *timeout, void *data);
542
+#endif /* HAVE_AVAHI */
548
/* Running from launchd */
549
#endif /* HAVE_LAUNCH_H */
552
+VAR cups_array_t *Timeouts; /* Timed callbacks for main loop */
553
+#endif /* HAVE_AVAHI */
560
extern void cupsdStartServer(void);
561
extern void cupsdStopServer(void);
564
+extern void cupsdInitTimeouts(void);
565
+extern cupsd_timeout_t *cupsdAddTimeout (const struct timeval *tv,
566
+ cupsd_timeoutfunc_t cb,
568
+extern cupsd_timeout_t *cupsdNextTimeout (long *delay);
569
+extern void cupsdRunTimeout (cupsd_timeout_t *timeout);
570
+extern void cupsdUpdateTimeout (cupsd_timeout_t *timeout,
571
+ const struct timeval *tv);
572
+extern void cupsdRemoveTimeout (cupsd_timeout_t *timeout);
573
+#endif /* HAVE_AVAHI */
576
* End of "$Id: cupsd.h 10996 2013-05-29 11:51:34Z msweet $".
577
--- a/scheduler/dirsvc.c
578
+++ b/scheduler/dirsvc.c
582
# else /* HAVE_AVAHI */
583
- if ((DNSSDMaster = avahi_threaded_poll_new()) == NULL)
584
+ if ((DNSSDMaster = avahi_cups_poll_new()) == NULL)
586
cupsdLogMessage(CUPSD_LOG_ERROR, "Unable to create DNS-SD thread.");
590
int error; /* Error code, if any */
592
- DNSSDClient = avahi_client_new(avahi_threaded_poll_get(DNSSDMaster), 0,
593
+ DNSSDClient = avahi_client_new(avahi_cups_poll_get(DNSSDMaster), 0,
596
if (DNSSDClient == NULL)
598
if (FatalErrors & CUPSD_FATAL_BROWSE)
599
cupsdEndProcess(getpid(), 0);
601
- avahi_threaded_poll_free(DNSSDMaster);
602
+ avahi_cups_poll_free(DNSSDMaster);
606
- avahi_threaded_poll_start(DNSSDMaster);
608
# endif /* HAVE_DNSSD */
611
DNSServiceRefDeallocate(*srv);
613
# else /* HAVE_AVAHI */
614
- avahi_threaded_poll_lock(DNSSDMaster);
615
avahi_entry_group_free(*srv);
616
- avahi_threaded_poll_unlock(DNSSDMaster);
617
# endif /* HAVE_DNSSD */
620
@@ -1051,14 +1047,10 @@
623
# else /* HAVE_AVAHI */
624
- avahi_threaded_poll_lock(DNSSDMaster);
627
*srv = avahi_entry_group_new(DNSSDClient, dnssdRegisterCallback, NULL);
630
- avahi_threaded_poll_unlock(DNSSDMaster);
632
cupsdLogMessage(CUPSD_LOG_WARN, "DNS-SD registration of \"%s\" failed: %s",
633
name, dnssdErrorString(avahi_client_errno(DNSSDClient)));
635
@@ -1173,8 +1165,6 @@
636
cupsdLogMessage(CUPSD_LOG_DEBUG, "DNS-SD commit of \"%s\" failed.",
640
- avahi_threaded_poll_unlock(DNSSDMaster);
641
# endif /* HAVE_DNSSD */
644
@@ -1341,12 +1331,10 @@
647
# else /* HAVE_AVAHI */
648
- avahi_threaded_poll_stop(DNSSDMaster);
650
avahi_client_free(DNSSDClient);
653
- avahi_threaded_poll_free(DNSSDMaster);
654
+ avahi_cups_poll_free(DNSSDMaster);
656
# endif /* HAVE_DNSSD */
658
--- a/scheduler/dirsvc.h
659
+++ b/scheduler/dirsvc.h
661
VAR DNSServiceRef DNSSDMaster VALUE(NULL);
662
/* Master DNS-SD service reference */
663
# else /* HAVE_AVAHI */
664
-VAR AvahiThreadedPoll *DNSSDMaster VALUE(NULL);
665
+VAR AvahiCupsPoll *DNSSDMaster VALUE(NULL);
666
/* Master polling interface for Avahi */
667
VAR AvahiClient *DNSSDClient VALUE(NULL);
668
/* Client information */
669
--- a/scheduler/main.c
670
+++ b/scheduler/main.c
672
int launchd_idle_exit;
673
/* Idle exit on select timeout? */
674
#endif /* HAVE_LAUNCHD */
676
+ cupsd_timeout_t *tmo; /* Next scheduled timed callback */
677
+ long tmo_delay; /* Time before it must be called */
678
+#endif /* HAVE_AVAHI */
688
+ * Initialize timed callback structures.
691
+ cupsdInitTimeouts();
692
+#endif /* HAVE_AVAHI */
699
#endif /* __APPLE__ */
703
+ * If a timed callback is due, run it.
706
+ tmo = cupsdNextTimeout (&tmo_delay);
707
+ if (tmo && tmo_delay == 0)
708
+ cupsdRunTimeout (tmo);
709
+#endif /* HAVE_AVAHI */
713
* Update the network interfaces once a minute...
714
@@ -1804,6 +1826,10 @@
715
cupsd_job_t *job; /* Job information */
716
cupsd_subscription_t *sub; /* Subscription information */
717
const char *why; /* Debugging aid */
719
+ cupsd_timeout_t *tmo; /* Timed callback */
720
+ long tmo_delay; /* Seconds before calling it */
721
+#endif /* HAVE_AVAHI */
724
cupsdLogMessage(CUPSD_LOG_DEBUG2, "select_timeout: JobHistoryUpdate=%ld",
725
@@ -1849,6 +1875,19 @@
727
#endif /* __APPLE__ */
731
+ * See if there are any scheduled timed callbacks to run.
734
+ if ((tmo = cupsdNextTimeout(&tmo_delay)) != NULL &&
735
+ (now + tmo_delay) < timeout)
737
+ timeout = tmo_delay;
738
+ why = "run a timed callback";
740
+#endif /* HAVE_AVAHI */
743
* Check whether we are accepting new connections...
745
--- a/scheduler/Makefile
746
+++ b/scheduler/Makefile
764
+++ b/scheduler/timeout.c
769
+ * Timeout functions for the Common UNIX Printing System (CUPS).
771
+ * Copyright (C) 2010, 2011 Red Hat, Inc.
773
+ * Tim Waugh <twaugh@redhat.com>
775
+ * Redistribution and use in source and binary forms, with or without
776
+ * modification, are permitted provided that the following conditions
779
+ * Redistributions of source code must retain the above copyright
780
+ * notice, this list of conditions and the following disclaimer.
782
+ * Redistributions in binary form must reproduce the above copyright
783
+ * notice, this list of conditions and the following disclaimer in the
784
+ * documentation and/or other materials provided with the distribution.
786
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
787
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
788
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
789
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
790
+ * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
791
+ * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
792
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
793
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
794
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
795
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
796
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
797
+ * OF THE POSSIBILITY OF SUCH DAMAGE.
801
+ * cupsdInitTimeouts() - Initialise timeout structure.
802
+ * cupsdAddTimeout() - Add a timed callback.
803
+ * cupsdNextTimeout() - Find the next enabled timed callback.
804
+ * cupsdUpdateTimeout() - Adjust the time of a timed callback or disable it.
805
+ * cupsdRemoveTimeout() - Discard a timed callback.
806
+ * compare_timeouts() - Compare timed callbacks for array sorting.
811
+#ifdef HAVE_AVAHI /* Applies to entire file... */
814
+ * Include necessary headers...
819
+#if defined(HAVE_MALLOC_H) && defined(HAVE_MALLINFO)
820
+# include <malloc.h>
821
+#endif /* HAVE_MALLOC_H && HAVE_MALLINFO */
824
+# include <avahi-common/timeval.h>
825
+#endif /* HAVE_AVAHI */
828
+struct _cupsd_timeout_s
830
+ struct timeval when;
832
+ cupsd_timeoutfunc_t callback;
837
+ * Local functions...
841
+ * 'compare_timeouts()' - Compare timed callbacks for array sorting.
845
+compare_addrs (void *p0, void *p1)
855
+compare_timeouts (cupsd_timeout_t *p0, cupsd_timeout_t *p1)
857
+ int addrsdiff = compare_addrs (p0, p1);
860
+ if (addrsdiff == 0)
863
+ if (!p0->enabled || !p1->enabled)
865
+ if (!p0->enabled && !p1->enabled)
866
+ return (addrsdiff);
868
+ return (p0->enabled ? -1 : 1);
871
+ tvdiff = avahi_timeval_compare (&p0->when, &p1->when);
875
+ return (addrsdiff);
880
+ * 'cupsdInitTimeouts()' - Initialise timeout structures.
884
+cupsdInitTimeouts(void)
886
+ Timeouts = cupsArrayNew ((cups_array_func_t)compare_timeouts, NULL);
891
+ * 'cupsdAddTimeout()' - Add a timed callback.
894
+cupsd_timeout_t * /* O - Timeout handle */
895
+cupsdAddTimeout(const struct timeval *tv, /* I - Absolute time */
896
+ cupsd_timeoutfunc_t cb, /* I - Callback function */
897
+ void *data) /* I - User data */
899
+ cupsd_timeout_t *timeout;
901
+ timeout = malloc (sizeof(cupsd_timeout_t));
902
+ if (timeout != NULL)
904
+ timeout->enabled = (tv != NULL);
907
+ timeout->when.tv_sec = tv->tv_sec;
908
+ timeout->when.tv_usec = tv->tv_usec;
911
+ timeout->callback = cb;
912
+ timeout->data = data;
913
+ cupsArrayAdd (Timeouts, timeout);
921
+ * 'cupsdNextTimeout()' - Find the next enabled timed callback.
924
+cupsd_timeout_t * /* O - Next enabled timeout or NULL */
925
+cupsdNextTimeout(long *delay) /* O - Seconds before scheduled */
927
+ cupsd_timeout_t *first = cupsArrayFirst (Timeouts);
928
+ struct timeval curtime;
930
+ if (first && !first->enabled)
933
+ if (first && delay)
935
+ gettimeofday (&curtime, NULL);
936
+ if (avahi_timeval_compare (&curtime, &first->when) > 0)
940
+ *delay = 1 + first->when.tv_sec - curtime.tv_sec;
941
+ if (first->when.tv_usec < curtime.tv_usec)
951
+ * 'cupsdRunTimeout()' - Run a timed callback.
955
+cupsdRunTimeout(cupsd_timeout_t *timeout) /* I - Timeout */
959
+ timeout->enabled = 0;
960
+ if (!timeout->callback)
962
+ timeout->callback (timeout, timeout->data);
966
+ * 'cupsdUpdateTimeout()' - Adjust the time of a timed callback or disable it.
970
+cupsdUpdateTimeout(cupsd_timeout_t *timeout, /* I - Timeout */
971
+ const struct timeval *tv) /* I - Absolute time or NULL */
973
+ cupsArrayRemove (Timeouts, timeout);
974
+ timeout->enabled = (tv != NULL);
977
+ timeout->when.tv_sec = tv->tv_sec;
978
+ timeout->when.tv_usec = tv->tv_usec;
980
+ cupsArrayAdd (Timeouts, timeout);
985
+ * 'cupsdRemoveTimeout()' - Discard a timed callback.
989
+cupsdRemoveTimeout(cupsd_timeout_t *timeout) /* I - Timeout */
991
+ cupsArrayRemove (Timeouts, timeout);
996
+#endif /* HAVE_AVAHI ... from top of file */