Add optional dbus support

As we might need dbus for inter-process-communication later, this adds a
very rudimentary dbus client integration for epoll-based loops.

Signed-off-by: David Herrmann <dh.herrmann@googlemail.com>
This commit is contained in:
David Herrmann 2012-07-14 14:00:23 +02:00
parent e1095b5ef2
commit 2ca0c915a7
6 changed files with 423 additions and 2 deletions

View File

@ -40,6 +40,12 @@ licensed under the conditions of the LGPL-2.1 or later as published by the FSF:
Author: Rusty Russel <rusty@rustcorp.com.au>
The dbus-loop code that allows using dbus in epoll loops easily is copied from
the systemd-distribution and licensed under the conditions of the LGPL-2.1 or
later as published by the FSF. It applies to external/dbus-loop.*
Copyright 2011 Lennart Poettering
The vt.c source file is based on the VT handling of the wayland demo
compositor:

View File

@ -218,10 +218,18 @@ libeloop_la_SOURCES = \
src/eloop.h \
src/eloop.c
if EV_HAVE_DBUS
libeloop_la_SOURCES += \
external/dbus-loop.h \
external/dbus-loop.c
endif
libeloop_la_CPPFLAGS = \
$(AM_CPPFLAGS)
$(AM_CPPFLAGS) \
$(DBUS_CFLAGS)
libeloop_la_LIBADD = \
libkmscon-static.la
libkmscon-static.la \
$(DBUS_LIBS)
libeloop_la_LDFLAGS = \
-version-info 1:0:0

View File

@ -61,6 +61,9 @@ PKG_CHECK_MODULES([SYSTEMD], [libsystemd-login],
PKG_CHECK_MODULES([UDEV], [libudev],
[have_udev=yes], [have_udev=no])
PKG_CHECK_MODULES([DBUS], [dbus-1],
[have_dbus=yes], [have_dbus=no])
PKG_CHECK_MODULES([DRM], [libdrm],
[have_drm=yes], [have_drm=no])
@ -212,6 +215,31 @@ else
UDEV_LIBS=""
fi
#
# DBus dependency
# For IPC mechanisms we use DBus. Especially multi-seat enabled multi-session
# capable applications need DBus to manage application and terminal switching.
#
dbus_enabled=no
if test ! x$enable_dbus = xno ; then
if test x$have_dbus = xyes ; then
dbus_enabled=yes
elif test x$enable_dbus = xyes; then
AC_ERROR([dbus libraries not found])
fi
fi
if test x$dbus_enabled = xyes; then
AC_DEFINE([EV_HAVE_DBUS], [1],
[Use dbus for IPC])
else
DBUS_CFLAGS=""
DBUS_LIBS=""
fi
AM_CONDITIONAL([EV_HAVE_DBUS], [test x$dbus_enabled = xyes])
#
# Uterm fbdev backend
# This checks whether the fbdev backend was requested and enables it then. There
@ -330,6 +358,8 @@ AM_CONDITIONAL([UTERM_HAVE_XKBCOMMON], [test x$xkbcommon_enabled = xyes])
AC_SUBST(SYSTEMD_CFLAGS)
AC_SUBST(SYSTEMD_LIBS)
AC_SUBST(DBUS_CFLAGS)
AC_SUBST(DBUS_LIBS)
AC_SUBST(DRM_CFLAGS)
AC_SUBST(DRM_LIBS)
AC_SUBST(EGL_CFLAGS)
@ -364,6 +394,7 @@ AC_MSG_NOTICE([Build configuration:
systemd: $systemd_enabled
udev: $udev_enabled
dbus: $dbus_enabled
xkbcommon: $xkbcommon_enabled
libuterm video backends:

84
external/dbus-common.h vendored Normal file
View File

@ -0,0 +1,84 @@
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
/***
This file is part of systemd.
Copyright 2011 Lennart Poettering
systemd is free software; you can redistribute it and/or modify it
under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
systemd 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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
#include <stdlib.h>
#define new0(_x, _y) zero_alloc(sizeof(_x) * (_y))
static void *zero_alloc(size_t s)
{
void *mem = malloc(s);
if (mem)
memset(mem, 0, s);
return mem;
}
#define zero(_x) memset(&(_x), 0, sizeof(_x))
#define PTR_TO_INT(_x) ((int) ((intptr_t) (_x)))
#define INT_TO_PTR(u) ((void*) ((intptr_t) (u)))
#define close_nointr_nofail(_x) close(_x)
#define assert_se(_x) (void)(_x)
#define USEC_PER_MSEC 1000ULL
#define USEC_PER_SEC 1000000ULL
#define NSEC_PER_USEC 1000ULL
#define log_error(...)
static struct timespec *timespec_store(struct timespec *ts, int64_t u) {
assert(ts);
ts->tv_sec = (time_t) (u / USEC_PER_SEC);
ts->tv_nsec = (long int) ((u % USEC_PER_SEC) * NSEC_PER_USEC);
return ts;
}
uint32_t bus_flags_to_events(DBusWatch *bus_watch) {
unsigned flags;
uint32_t events = 0;
assert(bus_watch);
/* no watch flags for disabled watches */
if (!dbus_watch_get_enabled(bus_watch))
return 0;
flags = dbus_watch_get_flags(bus_watch);
if (flags & DBUS_WATCH_READABLE)
events |= EPOLLIN;
if (flags & DBUS_WATCH_WRITABLE)
events |= EPOLLOUT;
return events | EPOLLHUP | EPOLLERR;
}
unsigned bus_events_to_flags(uint32_t events) {
unsigned flags = 0;
if (events & EPOLLIN)
flags |= DBUS_WATCH_READABLE;
if (events & EPOLLOUT)
flags |= DBUS_WATCH_WRITABLE;
if (events & EPOLLHUP)
flags |= DBUS_WATCH_HANGUP;
if (events & EPOLLERR)
flags |= DBUS_WATCH_ERROR;
return flags;
}

262
external/dbus-loop.c vendored Normal file
View File

@ -0,0 +1,262 @@
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
/***
This file is part of systemd.
Copyright 2011 Lennart Poettering
systemd is free software; you can redistribute it and/or modify it
under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
systemd 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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
#include <stdbool.h>
#include <assert.h>
#include <sys/epoll.h>
#include <string.h>
#include <errno.h>
#include <sys/timerfd.h>
#include <unistd.h>
#include "dbus-loop.h"
#include "dbus-common.h"
/* Minimal implementation of the dbus loop which integrates all dbus
* events into a single epoll fd which we can triviall integrate with
* other loops. Note that this is not used in the main systemd daemon
* since we run a more elaborate mainloop there. */
typedef struct EpollData {
int fd;
void *object;
bool is_timeout:1;
bool fd_is_dupped:1;
} EpollData;
static dbus_bool_t add_watch(DBusWatch *watch, void *data) {
EpollData *e;
struct epoll_event ev;
assert(watch);
e = new0(EpollData, 1);
if (!e)
return FALSE;
e->fd = dbus_watch_get_unix_fd(watch);
e->object = watch;
e->is_timeout = false;
zero(ev);
ev.events = bus_flags_to_events(watch);
ev.data.ptr = e;
if (epoll_ctl(PTR_TO_INT(data), EPOLL_CTL_ADD, e->fd, &ev) < 0) {
if (errno != EEXIST) {
free(e);
return FALSE;
}
/* Hmm, bloody D-Bus creates multiple watches on the
* same fd. epoll() does not like that. As a dirty
* hack we simply dup() the fd and hence get a second
* one we can safely add to the epoll(). */
e->fd = dup(e->fd);
if (e->fd < 0) {
free(e);
return FALSE;
}
if (epoll_ctl(PTR_TO_INT(data), EPOLL_CTL_ADD, e->fd, &ev) < 0) {
close_nointr_nofail(e->fd);
free(e);
return FALSE;
}
e->fd_is_dupped = true;
}
dbus_watch_set_data(watch, e, NULL);
return TRUE;
}
static void remove_watch(DBusWatch *watch, void *data) {
EpollData *e;
assert(watch);
e = dbus_watch_get_data(watch);
if (!e)
return;
assert_se(epoll_ctl(PTR_TO_INT(data), EPOLL_CTL_DEL, e->fd, NULL) >= 0);
if (e->fd_is_dupped)
close_nointr_nofail(e->fd);
free(e);
}
static void toggle_watch(DBusWatch *watch, void *data) {
EpollData *e;
struct epoll_event ev;
assert(watch);
e = dbus_watch_get_data(watch);
if (!e)
return;
zero(ev);
ev.events = bus_flags_to_events(watch);
ev.data.ptr = e;
assert_se(epoll_ctl(PTR_TO_INT(data), EPOLL_CTL_MOD, e->fd, &ev) == 0);
}
static int timeout_arm(EpollData *e) {
struct itimerspec its;
assert(e);
assert(e->is_timeout);
zero(its);
if (dbus_timeout_get_enabled(e->object)) {
timespec_store(&its.it_value, dbus_timeout_get_interval(e->object) * USEC_PER_MSEC);
its.it_interval = its.it_value;
}
if (timerfd_settime(e->fd, 0, &its, NULL) < 0)
return -errno;
return 0;
}
static dbus_bool_t add_timeout(DBusTimeout *timeout, void *data) {
EpollData *e;
struct epoll_event ev;
assert(timeout);
e = new0(EpollData, 1);
if (!e)
return FALSE;
e->fd = timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK|TFD_CLOEXEC);
if (e->fd < 0)
goto fail;
e->object = timeout;
e->is_timeout = true;
if (timeout_arm(e) < 0)
goto fail;
zero(ev);
ev.events = EPOLLIN;
ev.data.ptr = e;
if (epoll_ctl(PTR_TO_INT(data), EPOLL_CTL_ADD, e->fd, &ev) < 0)
goto fail;
dbus_timeout_set_data(timeout, e, NULL);
return TRUE;
fail:
if (e->fd >= 0)
close_nointr_nofail(e->fd);
free(e);
return FALSE;
}
static void remove_timeout(DBusTimeout *timeout, void *data) {
EpollData *e;
assert(timeout);
e = dbus_timeout_get_data(timeout);
if (!e)
return;
assert_se(epoll_ctl(PTR_TO_INT(data), EPOLL_CTL_DEL, e->fd, NULL) >= 0);
close_nointr_nofail(e->fd);
free(e);
}
static void toggle_timeout(DBusTimeout *timeout, void *data) {
EpollData *e;
int r;
assert(timeout);
e = dbus_timeout_get_data(timeout);
if (!e)
return;
r = timeout_arm(e);
if (r < 0)
log_error("Failed to rearm timer: %s", strerror(-r));
}
int bus_loop_open(DBusConnection *c) {
int fd;
assert(c);
fd = epoll_create1(EPOLL_CLOEXEC);
if (fd < 0)
return -errno;
if (!dbus_connection_set_watch_functions(c, add_watch, remove_watch, toggle_watch, INT_TO_PTR(fd), NULL) ||
!dbus_connection_set_timeout_functions(c, add_timeout, remove_timeout, toggle_timeout, INT_TO_PTR(fd), NULL)) {
close_nointr_nofail(fd);
return -ENOMEM;
}
return fd;
}
int bus_loop_dispatch(int fd) {
int n;
struct epoll_event event;
EpollData *d;
assert(fd >= 0);
zero(event);
n = epoll_wait(fd, &event, 1, 0);
if (n < 0)
return errno == EAGAIN || errno == EINTR ? 0 : -errno;
assert_se(d = event.data.ptr);
if (d->is_timeout) {
DBusTimeout *t = d->object;
if (dbus_timeout_get_enabled(t))
dbus_timeout_handle(t);
} else {
DBusWatch *w = d->object;
if (dbus_watch_get_enabled(w))
dbus_watch_handle(w, bus_events_to_flags(event.events));
}
return 0;
}

30
external/dbus-loop.h vendored Normal file
View File

@ -0,0 +1,30 @@
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
#ifndef foodbusloophfoo
#define foodbusloophfoo
/***
This file is part of systemd.
Copyright 2011 Lennart Poettering
systemd is free software; you can redistribute it and/or modify it
under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
systemd 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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
#include <dbus/dbus.h>
int bus_loop_open(DBusConnection *c);
int bus_loop_dispatch(int fd);
#endif