dbus-shell.c \
dbus-shell.h \
$(DBUS_UTIL_arch_sources) \
+ dbus-socket-set.h \
+ dbus-socket-set.c \
+ dbus-socket-set-poll.c \
dbus-spawn.h \
dbus-string-util.c \
dbus-sysdeps-util.c \
* state passed to
* dbus_watch_handle()).
*/
+ /* Internal to libdbus, there is also _DBUS_WATCH_NVAL in dbus-watch.h */
} DBusWatchFlags;
/**
/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
/* dbus-mainloop.c Main loop utility
*
- * Copyright (C) 2003, 2004 Red Hat, Inc.
+ * Copyright © 2003, 2004 Red Hat, Inc.
+ * Copyright © 2011 Nokia Corporation
*
* Licensed under the Academic Free License version 2.1
*
#include <dbus/dbus-hash.h>
#include <dbus/dbus-list.h>
-#include <dbus/dbus-sysdeps.h>
+#include <dbus/dbus-socket-set.h>
#include <dbus/dbus-watch.h>
#define MAINLOOP_SPEW 0
DBusList *need_dispatch;
};
-static short
-watch_flags_to_poll_events (unsigned int flags)
-{
- short events = 0;
-
- if (flags & DBUS_WATCH_READABLE)
- events |= _DBUS_POLLIN;
- if (flags & DBUS_WATCH_WRITABLE)
- events |= _DBUS_POLLOUT;
-
- return events;
-}
-
-static unsigned int
-watch_flags_from_poll_revents (short revents)
-{
- unsigned int condition = 0;
-
- if (revents & _DBUS_POLLIN)
- condition |= DBUS_WATCH_READABLE;
- if (revents & _DBUS_POLLOUT)
- condition |= DBUS_WATCH_WRITABLE;
- if (revents & _DBUS_POLLHUP)
- condition |= DBUS_WATCH_HANGUP;
- if (revents & _DBUS_POLLERR)
- condition |= DBUS_WATCH_ERROR;
-
- return condition;
-}
-
typedef struct
{
int refcount;
{
#define N_STACK_DESCRIPTORS 64
dbus_bool_t retval;
- DBusPollFD *fds;
- DBusPollFD stack_fds[N_STACK_DESCRIPTORS];
- int n_fds;
+ DBusSocketSet *socket_set = NULL;
+ DBusSocketEvent ready_fds[N_STACK_DESCRIPTORS];
int i;
DBusList *link;
int n_ready;
retval = FALSE;
- fds = NULL;
- n_fds = 0;
oom_watch_pending = FALSE;
orig_depth = loop->depth;
loop->timeouts == NULL)
goto next_iteration;
- if (loop->watch_count > N_STACK_DESCRIPTORS)
+ socket_set = _dbus_socket_set_new (loop->watch_count);
+
+ while (socket_set == NULL)
{
- fds = dbus_new0 (DBusPollFD, loop->watch_count);
-
- while (fds == NULL)
- {
- _dbus_wait_for_memory ();
- fds = dbus_new0 (DBusPollFD, loop->watch_count);
- }
- }
- else
- {
- fds = stack_fds;
+ _dbus_wait_for_memory ();
+ socket_set = _dbus_socket_set_new (loop->watch_count);
}
/* fill our array of fds and watches */
- n_fds = 0;
_dbus_hash_iter_init (loop->watches, &hash_iter);
while (_dbus_hash_iter_next (&hash_iter))
DBusList **watches;
unsigned int flags;
int fd;
+ dbus_bool_t enabled;
fd = _dbus_hash_iter_get_int_key (&hash_iter);
watches = _dbus_hash_iter_get_value (&hash_iter);
flags = 0;
+ enabled = FALSE;
for (link = _dbus_list_get_first_link (watches);
link != NULL;
else if (dbus_watch_get_enabled (watch))
{
flags |= dbus_watch_get_flags (watch);
+ enabled = TRUE;
}
}
- if (flags != 0)
+ if (enabled)
{
- fds[n_fds].fd = fd;
- fds[n_fds].revents = 0;
- fds[n_fds].events = watch_flags_to_poll_events (flags);
+ _dbus_socket_set_add (socket_set, fd, flags, TRUE);
#if MAINLOOP_SPEW
_dbus_verbose (" polling watch on fd %d %s\n",
- loop->fds[loop->n_fds].fd, watch_flags_to_string (flags));
+ fd, watch_flags_to_string (flags));
#endif
-
- n_fds += 1;
}
else
{
#if MAINLOOP_SPEW
_dbus_verbose (" skipping disabled watch on fd %d %s\n",
- fd,
- watch_flags_to_string (dbus_watch_get_flags (watch)));
+ fd, watch_flags_to_string (flags));
#endif
}
}
#if MAINLOOP_SPEW
_dbus_verbose (" polling on %d descriptors timeout %ld\n", n_fds, timeout);
#endif
-
- n_ready = _dbus_poll (fds, n_fds, timeout);
+
+ n_ready = _dbus_socket_set_poll (socket_set, ready_fds,
+ _DBUS_N_ELEMENTS (ready_fds), timeout);
initial_serial = loop->callback_list_serial;
link = next;
}
}
-
+
if (n_ready > 0)
{
- for (i = 0; i < n_fds; i++)
+ for (i = 0; i < n_ready; i++)
{
DBusList **watches;
DBusList *next;
if (loop->depth != orig_depth)
goto next_iteration;
- if (fds[i].revents == 0)
- continue;
+ _dbus_assert (ready_fds[i].flags != 0);
- if (_DBUS_UNLIKELY (fds[i].revents & _DBUS_POLLNVAL))
+ if (_DBUS_UNLIKELY (ready_fds[i].flags & _DBUS_WATCH_NVAL))
{
- cull_watches_for_invalid_fd (loop, fds[i].fd);
+ cull_watches_for_invalid_fd (loop, ready_fds[i].fd);
goto next_iteration;
}
- condition = watch_flags_from_poll_revents (fds[i].revents);
+ condition = ready_fds[i].flags;
+ _dbus_assert ((condition & _DBUS_WATCH_NVAL) == 0);
/* condition may still be 0 if we got some
* weird POLLFOO thing like POLLWRBAND
if (condition == 0)
continue;
- watches = _dbus_hash_table_lookup_int (loop->watches, fds[i].fd);
+ watches = _dbus_hash_table_lookup_int (loop->watches,
+ ready_fds[i].fd);
if (watches == NULL)
continue;
#if MAINLOOP_SPEW
_dbus_verbose (" moving to next iteration\n");
#endif
-
- if (fds && fds != stack_fds)
- dbus_free (fds);
+
+ if (socket_set)
+ _dbus_socket_set_free (socket_set);
if (_dbus_loop_dispatch (loop))
retval = TRUE;
--- /dev/null
+/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
+/* dbus-socket-set-poll.c - a socket set implemented via _dbus_poll
+ *
+ * Copyright © 2011 Nokia Corporation
+ *
+ * Licensed under the Academic Free License version 2.1
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
+ * MA 02110-1301 USA
+ *
+ */
+
+#include <config.h>
+#include "dbus-socket-set.h"
+
+#include <dbus/dbus-internals.h>
+#include <dbus/dbus-list.h>
+#include <dbus/dbus-sysdeps.h>
+#include <dbus/dbus-watch.h>
+
+#ifndef DOXYGEN_SHOULD_SKIP_THIS
+
+typedef struct {
+ DBusSocketSet parent;
+ DBusPollFD *fds;
+ int n_fds;
+ int n_reserved;
+ int n_allocated;
+} DBusSocketSetPoll;
+
+#define REALLOC_INCREMENT 8
+#define MINIMUM_SIZE 8
+
+/* If we're in the regression tests, force reallocation to happen sooner */
+#ifdef DBUS_BUILD_TESTS
+#define DEFAULT_SIZE_HINT 1
+#else
+#define DEFAULT_SIZE_HINT MINIMUM_SIZE
+#endif
+
+static inline DBusSocketSetPoll *
+socket_set_poll_cast (DBusSocketSet *set)
+{
+ _dbus_assert (set->cls == &_dbus_socket_set_poll_class);
+ return (DBusSocketSetPoll *) set;
+}
+
+/* this is safe to call on a partially-allocated socket set */
+static void
+socket_set_poll_free (DBusSocketSet *set)
+{
+ DBusSocketSetPoll *self = socket_set_poll_cast (set);
+
+ dbus_free (self->fds);
+ dbus_free (self);
+ _dbus_verbose ("freed socket set %p\n", self);
+}
+
+DBusSocketSet *
+_dbus_socket_set_poll_new (int size_hint)
+{
+ DBusSocketSetPoll *ret;
+
+ if (size_hint <= 0)
+ size_hint = DEFAULT_SIZE_HINT;
+
+ ret = dbus_new0 (DBusSocketSetPoll, 1);
+
+ if (ret == NULL)
+ return NULL;
+
+ ret->parent.cls = &_dbus_socket_set_poll_class;
+ ret->n_fds = 0;
+ ret->n_allocated = size_hint;
+
+ ret->fds = dbus_new0 (DBusPollFD, size_hint);
+
+ if (ret->fds == NULL)
+ {
+ /* socket_set_poll_free specifically supports half-constructed
+ * socket sets */
+ socket_set_poll_free ((DBusSocketSet *) ret);
+ return NULL;
+ }
+
+ _dbus_verbose ("new socket set at %p\n", ret);
+ return (DBusSocketSet *) ret;
+}
+
+static short
+watch_flags_to_poll_events (unsigned int flags)
+{
+ short events = 0;
+
+ if (flags & DBUS_WATCH_READABLE)
+ events |= _DBUS_POLLIN;
+ if (flags & DBUS_WATCH_WRITABLE)
+ events |= _DBUS_POLLOUT;
+
+ return events;
+}
+
+static dbus_bool_t
+socket_set_poll_add (DBusSocketSet *set,
+ int fd,
+ unsigned int flags,
+ dbus_bool_t enabled)
+{
+ DBusSocketSetPoll *self = socket_set_poll_cast (set);
+#ifndef DBUS_DISABLE_ASSERT
+ int i;
+
+ for (i = 0; i < self->n_fds; i++)
+ _dbus_assert (self->fds[i].fd != fd);
+#endif
+
+ if (self->n_reserved >= self->n_allocated)
+ {
+ DBusPollFD *new_fds = dbus_realloc (self->fds,
+ sizeof (DBusPollFD) * (self->n_allocated + REALLOC_INCREMENT));
+
+ _dbus_verbose ("inflating set %p from %d en/%d res/%d alloc to %d\n",
+ self, self->n_fds, self->n_reserved, self->n_allocated,
+ self->n_allocated + REALLOC_INCREMENT);
+
+ if (new_fds == NULL)
+ return FALSE;
+
+ self->fds = new_fds;
+ self->n_allocated += REALLOC_INCREMENT;
+ }
+
+ _dbus_verbose ("before adding fd %d to %p, %d en/%d res/%d alloc\n",
+ fd, self, self->n_fds, self->n_reserved, self->n_allocated);
+ _dbus_assert (self->n_reserved >= self->n_fds);
+ _dbus_assert (self->n_allocated > self->n_reserved);
+
+ self->n_reserved++;
+
+ if (enabled)
+ {
+ self->fds[self->n_fds].fd = fd;
+ self->fds[self->n_fds].events = watch_flags_to_poll_events (flags);
+ self->n_fds++;
+ }
+
+ return TRUE;
+}
+
+static void
+socket_set_poll_enable (DBusSocketSet *set,
+ int fd,
+ unsigned int flags)
+{
+ DBusSocketSetPoll *self = socket_set_poll_cast (set);
+ int i;
+
+ for (i = 0; i < self->n_fds; i++)
+ {
+ if (self->fds[i].fd == fd)
+ {
+ self->fds[i].events = watch_flags_to_poll_events (flags);
+ return;
+ }
+ }
+
+ /* we allocated space when the socket was added */
+ _dbus_assert (self->n_fds < self->n_reserved);
+ _dbus_assert (self->n_reserved <= self->n_allocated);
+
+ self->fds[self->n_fds].fd = fd;
+ self->fds[self->n_fds].events = watch_flags_to_poll_events (flags);
+ self->n_fds++;
+}
+
+static void
+socket_set_poll_disable (DBusSocketSet *set,
+ int fd)
+{
+ DBusSocketSetPoll *self = socket_set_poll_cast (set);
+ int i;
+
+ for (i = 0; i < self->n_fds; i++)
+ {
+ if (self->fds[i].fd == fd)
+ {
+ if (i != self->n_fds - 1)
+ {
+ self->fds[i].fd = self->fds[self->n_fds - 1].fd;
+ self->fds[i].events = self->fds[self->n_fds - 1].events;
+ }
+
+ self->n_fds--;
+ return;
+ }
+ }
+}
+
+static void
+socket_set_poll_remove (DBusSocketSet *set,
+ int fd)
+{
+ DBusSocketSetPoll *self = socket_set_poll_cast (set);
+
+ socket_set_poll_disable (set, fd);
+ self->n_reserved--;
+
+ _dbus_verbose ("after removing fd %d from %p, %d en/%d res/%d alloc\n",
+ fd, self, self->n_fds, self->n_reserved, self->n_allocated);
+ _dbus_assert (self->n_fds <= self->n_reserved);
+ _dbus_assert (self->n_reserved <= self->n_allocated);
+
+ if (self->n_reserved + MINIMUM_SIZE < self->n_allocated / 2)
+ {
+ /* Our array is twice as big as it needs to be - deflate it until it's
+ * only slightly larger than the number reserved. */
+ DBusPollFD *new_fds = dbus_realloc (self->fds,
+ sizeof (DBusPollFD) * (self->n_reserved + MINIMUM_SIZE));
+
+ _dbus_verbose ("before deflating %p, %d en/%d res/%d alloc\n",
+ self, self->n_fds, self->n_reserved, self->n_allocated);
+
+ if (_DBUS_UNLIKELY (new_fds == NULL))
+ {
+ /* Weird. Oh well, never mind, the too-big array is untouched */
+ return;
+ }
+
+ self->fds = new_fds;
+ self->n_allocated = self->n_reserved;
+ }
+}
+
+static unsigned int
+watch_flags_from_poll_revents (short revents)
+{
+ unsigned int condition = 0;
+
+ if (revents & _DBUS_POLLIN)
+ condition |= DBUS_WATCH_READABLE;
+ if (revents & _DBUS_POLLOUT)
+ condition |= DBUS_WATCH_WRITABLE;
+ if (revents & _DBUS_POLLHUP)
+ condition |= DBUS_WATCH_HANGUP;
+ if (revents & _DBUS_POLLERR)
+ condition |= DBUS_WATCH_ERROR;
+
+ if (_DBUS_UNLIKELY (revents & _DBUS_POLLNVAL))
+ condition |= _DBUS_WATCH_NVAL;
+
+ return condition;
+}
+
+/** This is basically Linux's epoll_wait(2) implemented in terms of poll(2);
+ * it returns results into a caller-supplied buffer so we can be reentrant. */
+static int
+socket_set_poll_poll (DBusSocketSet *set,
+ DBusSocketEvent *revents,
+ int max_events,
+ int timeout_ms)
+{
+ DBusSocketSetPoll *self = socket_set_poll_cast (set);
+ int i;
+ int n_events;
+ int n_ready;
+
+ _dbus_assert (max_events > 0);
+
+ for (i = 0; i < self->n_fds; i++)
+ self->fds[i].revents = 0;
+
+ n_ready = _dbus_poll (self->fds, self->n_fds, timeout_ms);
+
+ if (n_ready <= 0)
+ return n_ready;
+
+ n_events = 0;
+
+ for (i = 0; i < self->n_fds; i++)
+ {
+ if (self->fds[i].revents != 0)
+ {
+ revents[n_events].fd = self->fds[i].fd;
+ revents[n_events].flags = watch_flags_from_poll_revents (self->fds[i].revents);
+
+ n_events += 1;
+
+ /* We ignore events beyond max_events because we have nowhere to
+ * put them. _dbus_poll is level-triggered, so we'll just be told
+ * about them next time round the main loop anyway. */
+ if (n_events == max_events)
+ return n_events;
+ }
+ }
+
+ return n_events;
+}
+
+DBusSocketSetClass _dbus_socket_set_poll_class = {
+ socket_set_poll_free,
+ socket_set_poll_add,
+ socket_set_poll_remove,
+ socket_set_poll_enable,
+ socket_set_poll_disable,
+ socket_set_poll_poll
+};
+
+#endif /* !DOXYGEN_SHOULD_SKIP_THIS */
--- /dev/null
+/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
+/*
+ * dbus-socket-set.c - used to bolt file descriptors onto a bus
+ *
+ * Copyright © 2011 Nokia Corporation
+ *
+ * Licensed under the Academic Free License version 2.1
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
+ * MA 02110-1301 USA
+ *
+ */
+
+#include <config.h>
+#include <dbus/dbus-socket-set.h>
+
+DBusSocketSet *
+_dbus_socket_set_new (int size_hint)
+{
+ DBusSocketSet *ret;
+
+ ret = _dbus_socket_set_poll_new (size_hint);
+
+ if (ret != NULL)
+ return ret;
+
+ return NULL;
+}
--- /dev/null
+/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
+/*
+ * dbus-socket-set.h - used to bolt file descriptors onto a bus
+ *
+ * Copyright © 2011 Nokia Corporation
+ *
+ * Licensed under the Academic Free License version 2.1
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
+ * MA 02110-1301 USA
+ *
+ */
+
+#ifndef DBUS_SOCKET_SET_H
+#define DBUS_SOCKET_SET_H
+
+#ifndef DOXYGEN_SHOULD_SKIP_THIS
+
+#include <dbus/dbus.h>
+
+typedef struct {
+ int fd;
+ unsigned int flags;
+} DBusSocketEvent;
+
+typedef struct DBusSocketSet DBusSocketSet;
+
+typedef struct DBusSocketSetClass DBusSocketSetClass;
+struct DBusSocketSetClass {
+ void (*free) (DBusSocketSet *self);
+ dbus_bool_t (*add) (DBusSocketSet *self,
+ int fd,
+ unsigned int flags,
+ dbus_bool_t enabled);
+ void (*remove) (DBusSocketSet *self,
+ int fd);
+ void (*enable) (DBusSocketSet *self,
+ int fd,
+ unsigned int flags);
+ void (*disable) (DBusSocketSet *self,
+ int fd);
+ int (*poll) (DBusSocketSet *self,
+ DBusSocketEvent *revents,
+ int max_events,
+ int timeout_ms);
+};
+
+struct DBusSocketSet {
+ DBusSocketSetClass *cls;
+};
+
+DBusSocketSet *_dbus_socket_set_new (int size_hint);
+
+static inline void
+_dbus_socket_set_free (DBusSocketSet *self)
+{
+ (self->cls->free) (self);
+}
+
+static inline dbus_bool_t
+_dbus_socket_set_add (DBusSocketSet *self,
+ int fd,
+ unsigned int flags,
+ dbus_bool_t enabled)
+{
+ return (self->cls->add) (self, fd, flags, enabled);
+}
+
+static inline void
+_dbus_socket_set_remove (DBusSocketSet *self,
+ int fd)
+{
+ (self->cls->remove) (self, fd);
+}
+
+static inline void
+_dbus_socket_set_enable (DBusSocketSet *self,
+ int fd,
+ unsigned int flags)
+{
+ (self->cls->enable) (self, fd, flags);
+}
+
+static inline void
+_dbus_socket_set_disable (DBusSocketSet *self,
+ int fd)
+{
+ (self->cls->disable) (self, fd);
+}
+
+
+static inline int
+_dbus_socket_set_poll (DBusSocketSet *self,
+ DBusSocketEvent *revents,
+ int max_events,
+ int timeout_ms)
+{
+ return (self->cls->poll) (self, revents, max_events, timeout_ms);
+}
+
+/* concrete implementations, not necessarily built on all platforms */
+
+extern DBusSocketSetClass _dbus_socket_set_poll_class;
+
+DBusSocketSet *_dbus_socket_set_poll_new (int size_hint);
+
+#endif /* !DOXYGEN_SHOULD_SKIP_THIS */
+#endif /* multiple-inclusion guard */
typedef struct DBusWatchList DBusWatchList;
+#define _DBUS_WATCH_NVAL (1<<4)
+
/** function to run when the watch is handled */
typedef dbus_bool_t (* DBusWatchHandler) (DBusWatch *watch,
unsigned int flags,