dbus-loop.c revision 202630822f52e06dce8404633407329c38099278
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
/***
This file is part of systemd.
Copyright 2011 Lennart Poettering
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.
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
General Public License for more details.
You should have received a copy of the GNU General Public License
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
#include <stdbool.h>
#include <assert.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include "dbus-loop.h"
#include "dbus-common.h"
#include "util.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;
EpollData *e;
struct epoll_event ev;
if (!e)
return FALSE;
e->is_timeout = false;
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(). */
if (e->fd < 0) {
free(e);
return FALSE;
}
close_nointr_nofail(e->fd);
free(e);
return FALSE;
}
e->fd_is_dupped = true;
}
return TRUE;
}
EpollData *e;
e = dbus_watch_get_data(watch);
if (!e)
return;
if (e->fd_is_dupped)
close_nointr_nofail(e->fd);
free(e);
}
EpollData *e;
struct epoll_event ev;
e = dbus_watch_get_data(watch);
if (!e)
return;
}
static int timeout_arm(EpollData *e) {
struct itimerspec its;
assert(e);
assert(e->is_timeout);
if (dbus_timeout_get_enabled(e->object)) {
}
return -errno;
return 0;
}
EpollData *e;
struct epoll_event ev;
if (!e)
return FALSE;
if (e->fd < 0)
goto fail;
e->is_timeout = true;
if (timeout_arm(e) < 0)
goto fail;
goto fail;
return TRUE;
fail:
if (e->fd >= 0)
close_nointr_nofail(e->fd);
free(e);
return FALSE;
}
EpollData *e;
e = dbus_timeout_get_data(timeout);
if (!e)
return;
close_nointr_nofail(e->fd);
free(e);
}
EpollData *e;
int r;
e = dbus_timeout_get_data(timeout);
if (!e)
return;
r = timeout_arm(e);
if (r < 0)
}
int bus_loop_open(DBusConnection *c) {
int fd;
assert(c);
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)) {
return -ENOMEM;
}
return fd;
}
int bus_loop_dispatch(int fd) {
int n;
struct epoll_event event;
EpollData *d;
if (n < 0)
if (d->is_timeout) {
DBusTimeout *t = d->object;
if (dbus_timeout_get_enabled(t))
} else {
if (dbus_watch_get_enabled(w))
}
return 0;
}