dbus.c revision 8e27452380193a5f81bfd08a59aab8b07008ba0b
/*-*- Mode: C; c-basic-offset: 8 -*-*/
/***
This file is part of systemd.
Copyright 2010 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 <errno.h>
#include <unistd.h>
#include "dbus.h"
#include "log.h"
#include "strv.h"
#include "cgroup.h"
assert(m);
}
static void system_bus_dispatch_status(DBusConnection *bus, DBusDispatchStatus status, void *data) {
assert(m);
}
unsigned flags;
/* no watch flags for disabled watches */
if (!dbus_watch_get_enabled(bus_watch))
return 0;
if (flags & DBUS_WATCH_READABLE)
if (flags & DBUS_WATCH_WRITABLE)
}
unsigned flags = 0;
return flags;
}
assert(m);
assert(w);
/* This is called by the event loop whenever there is
* something happening on D-Bus' file handles. */
return;
}
Watch *w;
struct epoll_event ev;
assert(m);
return FALSE;
w->type = WATCH_DBUS_WATCH;
free(w);
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(). */
free(w);
return FALSE;
}
free(w);
close_nointr_nofail(w->fd);
return FALSE;
}
w->fd_is_dupped = true;
}
return TRUE;
}
Watch *w;
assert(m);
if (!(w = dbus_watch_get_data(bus_watch)))
return;
if (w->fd_is_dupped)
close_nointr_nofail(w->fd);
free(w);
}
Watch *w;
struct epoll_event ev;
assert(m);
}
struct itimerspec its;
assert(m);
assert(w);
}
return -errno;
return 0;
}
assert(m);
assert(w);
/* This is called by the event loop whenever there is
* something happening on D-Bus' file handles. */
return;
}
Watch *w;
struct epoll_event ev;
assert(m);
return FALSE;
goto fail;
w->type = WATCH_DBUS_TIMEOUT;
if (bus_timeout_arm(m, w) < 0)
goto fail;
goto fail;
return TRUE;
fail:
if (w->fd >= 0)
close_nointr_nofail(w->fd);
free(w);
return FALSE;
}
Watch *w;
assert(m);
if (!(w = dbus_timeout_get_data(timeout)))
return;
close_nointr_nofail(w->fd);
free(w);
}
Watch *w;
int r;
assert(m);
if ((r = bus_timeout_arm(m, w)) < 0)
}
static DBusHandlerResult bus_message_filter(DBusConnection *connection, DBusMessage *message, void *data) {
assert(m);
/* log_debug("Got D-Bus request: %s.%s() on %s", */
/* dbus_message_get_interface(message), */
/* dbus_message_get_member(message), */
/* dbus_message_get_path(message)); */
log_error("Warning! D-Bus connection terminated.");
/* FIXME: we probably should restart D-Bus here */
else {
}
}
}
static DBusHandlerResult system_bus_message_filter(DBusConnection *connection, DBusMessage *message, void *data) {
assert(m);
/* log_debug("Got D-Bus request: %s.%s() on %s", */
/* dbus_message_get_interface(message), */
/* dbus_message_get_member(message), */
/* dbus_message_get_path(message)); */
log_error("Warning! D-Bus connection terminated.");
/* FIXME: we probably should restart D-Bus here */
const char *cgroup;
else
}
}
unsigned bus_dispatch(Manager *m) {
assert(m);
if (m->request_bus_dispatch)
m->request_bus_dispatch = false;
return 1;
}
if (m->request_system_bus_dispatch)
m->request_system_bus_dispatch = false;
return 1;
}
return 0;
}
static int request_name(Manager *m) {
const char *name = "org.freedesktop.systemd1";
if (!(message = dbus_message_new_method_call(
"RequestName")))
return -ENOMEM;
if (!dbus_message_append_args(
return -ENOMEM;
}
return -ENOMEM;
}
/* We simple ask for the name and don't wait for it. Sooner or
* later we'll have it, and we wouldn't know what to do on
* error anyway. */
return 0;
}
assert(m);
if (!dbus_connection_set_watch_functions(bus, bus_add_watch, bus_remove_watch, bus_toggle_watch, m, NULL) ||
!dbus_connection_set_timeout_functions(bus, bus_add_timeout, bus_remove_timeout, bus_toggle_timeout, m, NULL))
return -ENOMEM;
return 0;
}
char *id;
int r;
assert(m);
if (m->bus)
return 0;
return -ENOMEM;
if (!(m->bus = dbus_bus_get_private(m->running_as == MANAGER_SESSION ? DBUS_BUS_SESSION : DBUS_BUS_SYSTEM, &error))) {
bus_done(m);
return -ECONNREFUSED;
}
if ((r = bus_setup_loop(m, m->bus)) < 0) {
bus_done(m);
return r;
}
if (m->running_as == MANAGER_SESSION) {
bus_done(m);
return -ECONNREFUSED;
}
if ((r = bus_setup_loop(m, m->system_bus)) < 0) {
bus_done(m);
return r;
}
} else
m->system_bus = m->bus;
if (!dbus_connection_register_object_path(m->bus, "/org/freedesktop/systemd1", &bus_manager_vtable, m) ||
!dbus_connection_register_fallback(m->bus, "/org/freedesktop/systemd1/unit", &bus_unit_vtable, m) ||
bus_done(m);
return -ENOMEM;
}
"type='signal',"
&error);
if (dbus_error_is_set(&error)) {
return -ENOMEM;
}
if ((r = request_name(m)) < 0) {
bus_done(m);
return r;
}
"type='signal',"
"interface='org.freedesktop.systemd1.Agent',"
"path='/org/freedesktop/systemd1/agent'",
&error);
if (dbus_error_is_set(&error)) {
bus_done(m);
return -ENOMEM;
}
log_debug("Successfully connected to D-Bus bus %s as %s",
log_debug("Successfully connected to system D-Bus bus %s as %s",
m->request_bus_dispatch = true;
m->request_system_bus_dispatch = true;
return 0;
}
assert(m);
m->system_bus = NULL;
}
if (m->bus) {
dbus_connection_close(m->bus);
dbus_connection_unref(m->bus);
}
if (m->subscribed) {
char *c;
while ((c = set_steal_first(m->subscribed)))
free(c);
set_free(m->subscribed);
m->subscribed = NULL;
}
}
DBusHandlerResult bus_default_message_handler(Manager *m, DBusMessage *message, const char*introspection, const BusProperty *properties) {
int r;
assert(m);
if (dbus_message_is_method_call(message, "org.freedesktop.DBus.Introspectable", "Introspect") && introspection) {
goto oom;
goto oom;
} else if (dbus_message_is_method_call(message, "org.freedesktop.DBus.Properties", "Get") && properties) {
const BusProperty *p;
if (!dbus_message_get_args(
&error,
for (p = properties; p->property; p++)
break;
if (p->property) {
goto oom;
goto oom;
if (r == -ENOMEM)
goto oom;
}
goto oom;
}
} else if (dbus_message_is_method_call(message, "org.freedesktop.DBus.Properties", "GetAll") && properties) {
const char *interface;
const BusProperty *p;
bool any = false;
if (!dbus_message_get_args(
&error,
goto oom;
goto oom;
for (p = properties; p->property; p++) {
continue;
goto oom;
if (r == -ENOMEM)
goto oom;
}
goto oom;
any = true;
}
goto oom;
}
if (reply) {
goto oom;
return DBUS_HANDLER_RESULT_HANDLED;
}
oom:
if (reply)
return DBUS_HANDLER_RESULT_NEED_MEMORY;
}
static const char *error_to_dbus(int error) {
switch(error) {
case -EINVAL:
return DBUS_ERROR_INVALID_ARGS;
case -ENOMEM:
return DBUS_ERROR_NO_MEMORY;
case -EPERM:
case -EACCES:
return DBUS_ERROR_ACCESS_DENIED;
case -ESRCH:
case -ENOENT:
return DBUS_ERROR_FILE_NOT_FOUND;
case -EEXIST:
return DBUS_ERROR_FILE_EXISTS;
case -ETIMEDOUT:
return DBUS_ERROR_TIMEOUT;
case -EIO:
return DBUS_ERROR_IO_ERROR;
case -ENETRESET:
case -ECONNABORTED:
case -ECONNRESET:
return DBUS_ERROR_DISCONNECTED;
}
return DBUS_ERROR_FAILED;
}
DBusHandlerResult bus_send_error_reply(Manager *m, DBusMessage *message, DBusError *bus_error, int error) {
} else {
}
goto oom;
goto oom;
if (bus_error)
return DBUS_HANDLER_RESULT_HANDLED;
oom:
if (reply)
if (bus_error)
return DBUS_HANDLER_RESULT_NEED_MEMORY;
}
const char *t = data;
assert(m);
assert(i);
if (!t)
t = "";
if (!dbus_message_iter_append_basic(i, DBUS_TYPE_STRING, &t))
return -ENOMEM;
return 0;
}
char **t = data;
assert(m);
assert(i);
return -ENOMEM;
STRV_FOREACH(t, t)
return -ENOMEM;
if (!dbus_message_iter_close_container(i, &sub))
return -ENOMEM;
return 0;
}
bool *b = data;
assert(m);
assert(i);
assert(b);
db = *b;
return -ENOMEM;
return 0;
}
assert(m);
assert(i);
return -ENOMEM;
return 0;
}
assert(m);
assert(i);
return -ENOMEM;
return 0;
}