Array handling fixes for Avahi poll implementation.

This commit is contained in:
Tim Waugh 2011-10-11 11:31:32 +01:00
parent 6085d5e059
commit 3f9aa63f17
2 changed files with 91 additions and 74 deletions

View File

@ -1,19 +1,39 @@
diff -up cups-1.5.0/scheduler/avahi.c.avahi-4-poll cups-1.5.0/scheduler/avahi.c 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-08-05 15:07:09.570033486 +0100 --- 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-08-05 15:07:09.570033486 +0100 +++ cups-1.5.0/scheduler/avahi.c 2011-10-11 10:56:50.102288037 +0100
@@ -0,0 +1,445 @@ @@ -0,0 +1,441 @@
+/* +/*
+ * "$Id$" + * "$Id$"
+ * + *
+ * Avahi poll implementation for the CUPS scheduler. + * Avahi poll implementation for the CUPS scheduler.
+ * + *
+ * Copyright (C) 2010 Red Hat, Inc. + * Copyright (C) 2010, 2011 Red Hat, Inc.
+ * Authors: + * Authors:
+ * Tim Waugh <twaugh@redhat.com> + * Tim Waugh <twaugh@redhat.com>
+ * + *
+ * Distribution and use rights are outlined in the file "LICENSE.txt" + * Redistribution and use in source and binary forms, with or without
+ * "LICENSE" which should have been included with this file. If this + * modification, are permitted provided that the following conditions
+ * file is missing or damaged, see the license at "http://www.cups.org/". + * 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: + * Contents:
+ * + *
@ -29,7 +49,6 @@ diff -up cups-1.5.0/scheduler/avahi.c.avahi-4-poll cups-1.5.0/scheduler/avahi.c
+ * timeout_update() - Update the expiration time for a timeout + * timeout_update() - Update the expiration time for a timeout
+ * timeout_free() - Free a timeout + * timeout_free() - Free a timeout
+ * compare_watched_fds() - Compare watched file descriptors for array sorting + * compare_watched_fds() - Compare watched file descriptors for array sorting
+ * compare_timeouts() - Compare timeouts for array sorting
+ * avahi_cups_poll_new() - Create a new Avahi main loop object for CUPS + * 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_free() - Free an Avahi main loop object for CUPS
+ * avahi_cups_poll_get() - Get the abstract poll API structure + * avahi_cups_poll_get() - Get the abstract poll API structure
@ -93,8 +112,6 @@ diff -up cups-1.5.0/scheduler/avahi.c.avahi-4-poll cups-1.5.0/scheduler/avahi.c
+static void watch_update(AvahiWatch *watch, +static void watch_update(AvahiWatch *watch,
+ AvahiWatchEvent events); + AvahiWatchEvent events);
+static AvahiWatchEvent watch_get_events(AvahiWatch *watch); +static AvahiWatchEvent watch_get_events(AvahiWatch *watch);
+static int compare_watches(AvahiWatch *p0,
+ AvahiWatch *p1);
+ +
+ +
+/* +/*
@ -109,8 +126,10 @@ diff -up cups-1.5.0/scheduler/avahi.c.avahi-4-poll cups-1.5.0/scheduler/avahi.c
+ watched_fd->occurred |= AVAHI_WATCH_IN; + watched_fd->occurred |= AVAHI_WATCH_IN;
+ for (watch = (AvahiWatch *)cupsArrayFirst(watched_fd->watches); + for (watch = (AvahiWatch *)cupsArrayFirst(watched_fd->watches);
+ watch; + watch;
+ watch = (AvahiWatch *)cupsArrayNext(watched_fd->watches)) { + watch = (AvahiWatch *)cupsArrayNext(watched_fd->watches))
+ if (watch->events & watched_fd->occurred) { + {
+ if (watch->events & watched_fd->occurred)
+ {
+ (watch->callback) (watch, watched_fd->fd, + (watch->callback) (watch, watched_fd->fd,
+ AVAHI_WATCH_IN, watch->userdata); + AVAHI_WATCH_IN, watch->userdata);
+ watched_fd->occurred &= ~AVAHI_WATCH_IN; + watched_fd->occurred &= ~AVAHI_WATCH_IN;
@ -132,8 +151,10 @@ diff -up cups-1.5.0/scheduler/avahi.c.avahi-4-poll cups-1.5.0/scheduler/avahi.c
+ watched_fd->occurred |= AVAHI_WATCH_OUT; + watched_fd->occurred |= AVAHI_WATCH_OUT;
+ for (watch = (AvahiWatch *)cupsArrayFirst(watched_fd->watches); + for (watch = (AvahiWatch *)cupsArrayFirst(watched_fd->watches);
+ watch; + watch;
+ watch = (AvahiWatch *)cupsArrayNext(watched_fd->watches)) { + watch = (AvahiWatch *)cupsArrayNext(watched_fd->watches))
+ if (watch->events & watched_fd->occurred) { + {
+ if (watch->events & watched_fd->occurred)
+ {
+ (watch->callback) (watch, watched_fd->fd, + (watch->callback) (watch, watched_fd->fd,
+ AVAHI_WATCH_OUT, watch->userdata); + AVAHI_WATCH_OUT, watch->userdata);
+ watched_fd->occurred &= ~AVAHI_WATCH_OUT; + watched_fd->occurred &= ~AVAHI_WATCH_OUT;
@ -152,19 +173,24 @@ diff -up cups-1.5.0/scheduler/avahi.c.avahi-4-poll cups-1.5.0/scheduler/avahi.c
+{ +{
+ AvahiWatch *watch; + AvahiWatch *watch;
+ cupsd_selfunc_t read_cb = NULL, write_cb = NULL; + cupsd_selfunc_t read_cb = NULL, write_cb = NULL;
+ int any_watches = 0;
+ +
+ for (watch = (AvahiWatch *)cupsArrayFirst(watched_fd->watches); + for (watch = (AvahiWatch *)cupsArrayFirst(watched_fd->watches);
+ watch; + watch;
+ watch = (AvahiWatch *)cupsArrayNext(watched_fd->watches)) { + watch = (AvahiWatch *)cupsArrayNext(watched_fd->watches))
+ {
+ any_watches = 1;
+ if (watch->events & (AVAHI_WATCH_IN | + if (watch->events & (AVAHI_WATCH_IN |
+ AVAHI_WATCH_ERR | + AVAHI_WATCH_ERR |
+ AVAHI_WATCH_HUP)) { + AVAHI_WATCH_HUP))
+ {
+ read_cb = (cupsd_selfunc_t)watch_read_cb; + read_cb = (cupsd_selfunc_t)watch_read_cb;
+ if (write_cb != NULL) + if (write_cb != NULL)
+ break; + break;
+ } + }
+ +
+ if (watch->events & AVAHI_WATCH_OUT) { + if (watch->events & AVAHI_WATCH_OUT)
+ {
+ write_cb = (cupsd_selfunc_t)watch_write_cb; + write_cb = (cupsd_selfunc_t)watch_write_cb;
+ if (read_cb != NULL) + if (read_cb != NULL)
+ break; + break;
@ -176,7 +202,7 @@ diff -up cups-1.5.0/scheduler/avahi.c.avahi-4-poll cups-1.5.0/scheduler/avahi.c
+ else + else
+ cupsdRemoveSelect (watched_fd->fd); + cupsdRemoveSelect (watched_fd->fd);
+ +
+ return (read_cb || write_cb); + return (any_watches);
+} +}
+ +
+/* +/*
@ -202,9 +228,11 @@ diff -up cups-1.5.0/scheduler/avahi.c.avahi-4-poll cups-1.5.0/scheduler/avahi.c
+ +
+ key.fd = fd; + key.fd = fd;
+ watched_fd = cupsArrayFind (cups_poll->watched_fds, &key); + watched_fd = cupsArrayFind (cups_poll->watched_fds, &key);
+ if (watched_fd == NULL) { + if (watched_fd == NULL)
+ {
+ watched_fd = malloc(sizeof(cupsd_watched_fd_t)); + watched_fd = malloc(sizeof(cupsd_watched_fd_t));
+ if (watched_fd == NULL) { + if (watched_fd == NULL)
+ {
+ free (watch); + free (watch);
+ return (NULL); + return (NULL);
+ } + }
@ -212,8 +240,8 @@ diff -up cups-1.5.0/scheduler/avahi.c.avahi-4-poll cups-1.5.0/scheduler/avahi.c
+ watched_fd->fd = fd; + watched_fd->fd = fd;
+ watched_fd->occurred = 0; + watched_fd->occurred = 0;
+ watched_fd->cups_poll = cups_poll; + watched_fd->cups_poll = cups_poll;
+ watched_fd->watches = cupsArrayNew ((cups_array_func_t)compare_watches, + watched_fd->watches = cupsArrayNew (NULL, NULL);
+ NULL); + cupsArrayAdd (cups_poll->watched_fds, watched_fd);
+ } + }
+ +
+ watch->watched_fd = watched_fd; + watch->watched_fd = watched_fd;
@ -236,7 +264,8 @@ diff -up cups-1.5.0/scheduler/avahi.c.avahi-4-poll cups-1.5.0/scheduler/avahi.c
+ cupsArrayRemove (watched_fd->watches, watch); + cupsArrayRemove (watched_fd->watches, watch);
+ free (watch); + free (watch);
+ +
+ if (!watched_fd_add_select (watched_fd)) { + if (!watched_fd_add_select (watched_fd))
+ {
+ /* No more watches */ + /* No more watches */
+ cupsArrayRemove (cups_poll->watched_fds, watched_fd); + cupsArrayRemove (cups_poll->watched_fds, watched_fd);
+ free (watched_fd); + free (watched_fd);
@ -269,21 +298,6 @@ diff -up cups-1.5.0/scheduler/avahi.c.avahi-4-poll cups-1.5.0/scheduler/avahi.c
+ +
+ +
+/* +/*
+ * 'compare_watches' - Compare watches for array sorting
+ */
+
+static int
+compare_watches (AvahiWatch *p0,
+ AvahiWatch *p1)
+{
+ if (p0->watched_fd->fd < p1->watched_fd->fd)
+ return (-1);
+
+ return ((p0->watched_fd->fd == p1->watched_fd->fd) ? 0 : 1);
+}
+
+
+/*
+ * 'timeout_cb()' - Run a timed Avahi callback + * 'timeout_cb()' - Run a timed Avahi callback
+ */ + */
+ +
@ -355,30 +369,14 @@ diff -up cups-1.5.0/scheduler/avahi.c.avahi-4-poll cups-1.5.0/scheduler/avahi.c
+compare_watched_fds(cupsd_watched_fd_t *p0, +compare_watched_fds(cupsd_watched_fd_t *p0,
+ cupsd_watched_fd_t *p1) + cupsd_watched_fd_t *p1)
+{ +{
+ if (p0->fd != p1->fd) + /*
+ * Compare by fd (no two elements have the same fd)
+ */
+
+ if (p0->fd == p1->fd)
+ return 0;
+
+ return (p0->fd < p1->fd ? -1 : 1); + return (p0->fd < p1->fd ? -1 : 1);
+
+ if (p0 == p1)
+ return (0);
+
+ return (p0 < p1 ? -1 : 1);
+}
+
+
+/*
+ * 'compare_timeouts' - Compare timeouts for array sorting
+ */
+static int
+compare_timeouts(AvahiTimeout *p0,
+ AvahiTimeout *p1)
+{
+ /*
+ * Just compare pointers to make it a stable sort.
+ */
+
+ if (p0->cupsd_timeout < p1->cupsd_timeout)
+ return (-1);
+ return ((p0->cupsd_timeout == p1->cupsd_timeout) ? 0 : 1);
+} +}
+ +
+ +
@ -395,8 +393,7 @@ diff -up cups-1.5.0/scheduler/avahi.c.avahi-4-poll cups-1.5.0/scheduler/avahi.c
+ +
+ cups_poll->watched_fds = cupsArrayNew ((cups_array_func_t)compare_watched_fds, + cups_poll->watched_fds = cupsArrayNew ((cups_array_func_t)compare_watched_fds,
+ NULL); + NULL);
+ cups_poll->timeouts = cupsArrayNew ((cups_array_func_t)compare_timeouts, + cups_poll->timeouts = cupsArrayNew (NULL, NULL);
+ NULL);
+ +
+ cups_poll->api.userdata = cups_poll; + cups_poll->api.userdata = cups_poll;
+ cups_poll->api.watch_new = watch_new; + cups_poll->api.watch_new = watch_new;
@ -422,9 +419,8 @@ diff -up cups-1.5.0/scheduler/avahi.c.avahi-4-poll cups-1.5.0/scheduler/avahi.c
+ +
+ for (watched_fd = (cupsd_watched_fd_t*)cupsArrayFirst(cups_poll->watched_fds); + for (watched_fd = (cupsd_watched_fd_t*)cupsArrayFirst(cups_poll->watched_fds);
+ watched_fd; + watched_fd;
+ watched_fd = (cupsd_watched_fd_t*)cupsArrayNext(cups_poll->watched_fds)){ + watched_fd = (cupsd_watched_fd_t*)cupsArrayNext(cups_poll->watched_fds))
+ cupsArrayClear (watched_fd->watches); + cupsArrayClear (watched_fd->watches);
+ }
+ +
+ cupsArrayClear (cups_poll->watched_fds); + cupsArrayClear (cups_poll->watched_fds);
+ cupsArrayClear (cups_poll->timeouts); + cupsArrayClear (cups_poll->timeouts);
@ -448,21 +444,41 @@ diff -up cups-1.5.0/scheduler/avahi.c.avahi-4-poll cups-1.5.0/scheduler/avahi.c
+ * End of "$Id$". + * End of "$Id$".
+ */ + */
diff -up cups-1.5.0/scheduler/avahi.h.avahi-4-poll cups-1.5.0/scheduler/avahi.h 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-08-05 15:07:09.594032723 +0100 --- 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-08-05 15:07:09.595032692 +0100 +++ cups-1.5.0/scheduler/avahi.h 2011-10-11 10:56:50.119287724 +0100
@@ -0,0 +1,49 @@ @@ -0,0 +1,69 @@
+/* +/*
+ * "$Id$" + * "$Id$"
+ * + *
+ * Avahi poll implementation for the CUPS scheduler. + * Avahi poll implementation for the CUPS scheduler.
+ * + *
+ * Copyright (C) 2010 Red Hat, Inc. + * Copyright (C) 2010, 2011 Red Hat, Inc.
+ * Authors: + * Authors:
+ * Tim Waugh <twaugh@redhat.com> + * Tim Waugh <twaugh@redhat.com>
+ * + *
+ * Distribution and use rights are outlined in the file "LICENSE.txt" + * Redistribution and use in source and binary forms, with or without
+ * which should have been included with this file. If this file is + * modification, are permitted provided that the following conditions
+ * file is missing or damaged, see the license at "http://www.cups.org/". + * 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> +#include <config.h>
@ -501,8 +517,8 @@ diff -up cups-1.5.0/scheduler/avahi.h.avahi-4-poll cups-1.5.0/scheduler/avahi.h
+ * End of "$Id$". + * End of "$Id$".
+ */ + */
diff -up cups-1.5.0/scheduler/Makefile.avahi-4-poll cups-1.5.0/scheduler/Makefile 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-08-05 15:06:48.548700563 +0100 --- cups-1.5.0/scheduler/Makefile.avahi-4-poll 2011-10-11 10:56:45.868365861 +0100
+++ cups-1.5.0/scheduler/Makefile 2011-08-05 15:07:09.570033486 +0100 +++ cups-1.5.0/scheduler/Makefile 2011-10-11 10:56:50.101288055 +0100
@@ -17,6 +17,7 @@ include ../Makedefs @@ -17,6 +17,7 @@ include ../Makedefs
CUPSDOBJS = \ CUPSDOBJS = \

View File

@ -646,7 +646,8 @@ rm -rf $RPM_BUILD_ROOT
%{_mandir}/man1/ipptool.1.gz %{_mandir}/man1/ipptool.1.gz
%changelog %changelog
* Mon Oct 10 2011 Tim Waugh <twaugh@redhat.com> 1:1.5.0-16 * Tue Oct 11 2011 Tim Waugh <twaugh@redhat.com> 1:1.5.0-16
- Array handling fixes for Avahi poll implementation.
- Increase client blocking timeout from 30s to 70s (bug #744715). - Increase client blocking timeout from 30s to 70s (bug #744715).
- Set BindIPv6Only=ipv6-only in systemd socket unit file as better fix - Set BindIPv6Only=ipv6-only in systemd socket unit file as better fix
for bug #737230. for bug #737230.