Commit de1c301e authored by Lennart Poettering's avatar Lennart Poettering

bus: add basic implementation of a native bus client library

parent 76f0199f
......@@ -80,6 +80,8 @@
/systemd-user-sessions
/systemd-vconsole-setup
/tags
/test-bus-marshal
/test-bus-signature
/test-calendarspec
/test-catalog
/test-cgroup
......
......@@ -1638,6 +1638,50 @@ EXTRA_DIST += \
src/libsystemd-daemon/libsystemd-daemon.pc.in \
src/libsystemd-daemon/libsystemd-daemon.sym
# ------------------------------------------------------------------------------
libsystemd_bus_la_SOURCES = \
src/libsystemd-bus/sd-bus.c \
src/libsystemd-bus/sd-bus.h \
src/libsystemd-bus/sd-bus-protocol.h \
src/libsystemd-bus/bus-control.c \
src/libsystemd-bus/bus-error.c \
src/libsystemd-bus/bus-error.h \
src/libsystemd-bus/bus-internal.c \
src/libsystemd-bus/bus-internal.h \
src/libsystemd-bus/bus-message.c \
src/libsystemd-bus/bus-message.h \
src/libsystemd-bus/bus-signature.c \
src/libsystemd-bus/bus-type.c \
src/libsystemd-bus/bus-type.h
noinst_LTLIBRARIES += \
libsystemd-bus.la
noinst_tests += \
test-bus-marshal \
test-bus-signature
test_bus_marshal_SOURCES = \
src/libsystemd-bus/test-bus-marshal.c
test_bus_marshal_LDADD = \
libsystemd-shared.la \
libsystemd-bus.la \
$(GLIB_LIBS) \
$(DBUS_LIBS)
test_bus_marshal_CFLAGS = \
$(AM_CFLAGS) \
$(GLIB_CFLAGS) \
$(DBUS_CFLAGS)
test_bus_signature_SOURCES = \
src/libsystemd-bus/test-bus-signature.c
test_bus_signature_LDADD = \
libsystemd-shared.la \
libsystemd-bus.la
# ------------------------------------------------------------------------------
if ENABLE_GTK_DOC
SUBDIRS += \
......
......@@ -724,9 +724,11 @@ AM_CONDITIONAL(ENABLE_FIRMWARE, [test "x${FIRMWARE_PATH}" != "x"])
AC_ARG_ENABLE([gudev],
AS_HELP_STRING([--disable-gudev], [disable Gobject libudev support @<:@default=enabled@:>@]),
[], [enable_gudev=yes])
AS_IF([test "x$enable_gudev" = "xyes"], [ PKG_CHECK_MODULES([GLIB], [glib-2.0 >= 2.22.0 gobject-2.0 >= 2.22.0]) ])
AS_IF([test "x$enable_gudev" = "xyes"], [ PKG_CHECK_MODULES([GLIB], [glib-2.0 >= 2.22.0 gobject-2.0 >= 2.22.0 gio-2.0]) ])
AM_CONDITIONAL([ENABLE_GUDEV], [test "x$enable_gudev" = "xyes"])
AS_IF([test "x$enable_gudev" = "xyes"], [ AC_DEFINE(HAVE_GLIB, 1, [Define if glib is available]) ])
# ------------------------------------------------------------------------------
AC_ARG_ENABLE([keymap],
AS_HELP_STRING([--disable-keymap], [disable keymap fixup support @<:@default=enabled@:>@]),
......
../Makefile
\ No newline at end of file
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
/***
This file is part of systemd.
Copyright 2013 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 <stddef.h>
#include <errno.h>
#include "strv.h"
#include "sd-bus.h"
#include "bus-internal.h"
#include "bus-message.h"
const char *sd_bus_get_unique_name(sd_bus *bus) {
if (!bus)
return NULL;
return bus->unique_name;
}
int sd_bus_request_name(sd_bus *bus, const char *name, int flags) {
_cleanup_bus_message_unref_ sd_bus_message *m = NULL, *reply = NULL;
uint32_t ret;
int r;
if (!bus)
return -EINVAL;
if (!name)
return -EINVAL;
r = sd_bus_message_new_method_call(
bus,
"org.freedesktop.DBus",
"/",
"org.freedesktop.DBus",
"RequestName",
&m);
if (r < 0)
return r;
r = sd_bus_message_append(m, "su", name, flags);
if (r < 0)
return r;
r = sd_bus_send_with_reply_and_block(bus, m, (uint64_t) -1, NULL, &reply);
if (r < 0)
return r;
r = sd_bus_message_read(reply, "u", &ret);
if (r < 0)
return r;
return ret;
}
int sd_bus_release_name(sd_bus *bus, const char *name) {
_cleanup_bus_message_unref_ sd_bus_message *m = NULL, *reply = NULL;
uint32_t ret;
int r;
if (!bus)
return -EINVAL;
if (!name)
return -EINVAL;
r = sd_bus_message_new_method_call(
bus,
"org.freedesktop.DBus",
"/",
"org.freedesktop.DBus",
"ReleaseName",
&m);
if (r < 0)
return r;
r = sd_bus_message_append(m, "s", name);
if (r < 0)
return r;
r = sd_bus_send_with_reply_and_block(bus, m, (uint64_t) -1, NULL, &reply);
if (r < 0)
return r;
r = sd_bus_message_read(reply, "u", &ret);
if (r < 0)
return r;
return ret;
}
int sd_bus_list_names(sd_bus *bus, char ***l) {
_cleanup_bus_message_unref_ sd_bus_message *m1 = NULL, *reply1 = NULL, *m2 = NULL, *reply2 = NULL;
_cleanup_strv_free_ char **a = NULL, **b = NULL;
char **x = NULL;
int r;
if (!bus)
return -EINVAL;
if (!l)
return -EINVAL;
r = sd_bus_message_new_method_call(
bus,
"org.freedesktop.DBus",
"/",
"org.freedesktop.DBus",
"ListNames",
&m1);
if (r < 0)
return r;
r = sd_bus_message_new_method_call(
bus,
"org.freedesktop.DBus",
"/",
"org.freedesktop.DBus",
"ListActivatableNames",
&m2);
if (r < 0)
return r;
r = sd_bus_send_with_reply_and_block(bus, m1, (uint64_t) -1, NULL, &reply1);
if (r < 0)
return r;
r = sd_bus_send_with_reply_and_block(bus, m2, (uint64_t) -1, NULL, &reply2);
if (r < 0)
return r;
r = sd_bus_message_read(reply1, "as", &a);
if (r < 0)
return r;
r = sd_bus_message_read(reply2, "as", &b);
if (r < 0)
return r;
x = strv_merge(a, b);
if (!x)
return -ENOMEM;
*l = strv_uniq(x);
return 0;
}
int sd_bus_get_owner(sd_bus *bus, const char *name, char **owner) {
_cleanup_bus_message_unref_ sd_bus_message *m = NULL, *reply = NULL;
int r;
if (!bus)
return -EINVAL;
if (!name)
return -EINVAL;
r = sd_bus_message_new_method_call(
bus,
"org.freedesktop.DBus",
"/",
"org.freedesktop.DBus",
"GetNameOwner",
&m);
if (r < 0)
return r;
r = sd_bus_message_append(m, "s", name);
if (r < 0)
return r;
r = sd_bus_send_with_reply_and_block(bus, m, (uint64_t) -1, NULL, &reply);
if (r < 0)
return r;
return sd_bus_message_read(reply, "s", owner);
}
int sd_bus_get_owner_uid(sd_bus *bus, const char *name, uid_t *uid) {
_cleanup_bus_message_unref_ sd_bus_message *m = NULL, *reply = NULL;
uint32_t u;
int r;
if (!bus)
return -EINVAL;
if (!name)
return -EINVAL;
if (!uid)
return -EINVAL;
r = sd_bus_message_new_method_call(
bus,
"org.freedesktop.DBus",
"/",
"org.freedesktop.DBus",
"GetConnectionUnixUser",
&m);
if (r < 0)
return r;
r = sd_bus_message_append(m, "s", name);
if (r < 0)
return r;
r = sd_bus_send_with_reply_and_block(bus, m, (uint64_t) -1, NULL, &reply);
if (r < 0)
return r;
r = sd_bus_message_read(reply, "u", &u);
if (r < 0)
return r;
*uid = (uid_t) u;
return 0;
}
int sd_bus_get_owner_pid(sd_bus *bus, const char *name, pid_t *pid) {
_cleanup_bus_message_unref_ sd_bus_message *m = NULL, *reply = NULL;
uint32_t u;
int r;
if (!bus)
return -EINVAL;
if (!name)
return -EINVAL;
if (!pid)
return -EINVAL;
r = sd_bus_message_new_method_call(
bus,
"org.freedesktop.DBus",
"/",
"org.freedesktop.DBus",
"GetConnectionUnixUser",
&m);
if (r < 0)
return r;
r = sd_bus_message_append(m, "s", name);
if (r < 0)
return r;
r = sd_bus_send_with_reply_and_block(bus, m, (uint64_t) -1, NULL, &reply);
if (r < 0)
return r;
r = sd_bus_message_read(reply, "u", &u);
if (r < 0)
return r;
if (u == 0)
return -EIO;
*pid = (uid_t) u;
return 0;
}
int sd_bus_add_match(sd_bus *bus, const char *match) {
_cleanup_bus_message_unref_ sd_bus_message *m = NULL, *reply = NULL;
int r;
if (!bus)
return -EINVAL;
if (!match)
return -EINVAL;
r = sd_bus_message_new_method_call(
bus,
"org.freedesktop.DBus",
"/",
"org.freedesktop.DBus",
"AddMatch",
&m);
if (r < 0)
return r;
r = sd_bus_message_append(m, "s", match);
if (r < 0)
return r;
return sd_bus_send_with_reply_and_block(bus, m, (uint64_t) -1, NULL, &reply);
}
int sd_bus_remove_match(sd_bus *bus, const char *match) {
_cleanup_bus_message_unref_ sd_bus_message *m = NULL, *reply = NULL;
int r;
if (!bus)
return -EINVAL;
if (!match)
return -EINVAL;
r = sd_bus_message_new_method_call(
bus,
"org.freedesktop.DBus",
"/",
"org.freedesktop.DBus",
"RemoveMatch",
&m);
if (r < 0)
return r;
r = sd_bus_message_append(m, "s", match);
if (r < 0)
return r;
return sd_bus_send_with_reply_and_block(bus, m, (uint64_t) -1, NULL, &reply);
}
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
/***
This file is part of systemd.
Copyright 2013 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 <errno.h>
#include <stdlib.h>
#include <stdarg.h>
#include <stdbool.h>
#include <string.h>
#include <stdio.h>
#include "util.h"
#include "sd-bus.h"
#include "bus-error.h"
void sd_bus_error_free(sd_bus_error *e) {
if (!e)
return;
if (e->need_free) {
free((void*) e->name);
free((void*) e->message);
}
e->name = e->message = NULL;
e->need_free = false;
}
int sd_bus_error_set(sd_bus_error *e, const char *name, const char *format, ...) {
char *n, *m = NULL;
va_list ap;
int r;
if (!e)
return 0;
if (sd_bus_error_is_set(e))
return -EINVAL;
if (!name)
return -EINVAL;
n = strdup(name);
if (!n)
return -ENOMEM;
if (format) {
va_start(ap, format);
r = vasprintf(&m, format, ap);
va_end(ap);
if (r < 0) {
free(n);
return -ENOMEM;
}
}
e->name = n;
e->message = m;
e->need_free = true;
return 0;
}
int sd_bus_error_copy(sd_bus_error *dest, const sd_bus_error *e) {
if (!dest)
return 0;
if (sd_bus_error_is_set(dest))
return -EINVAL;
if (!sd_bus_error_is_set(e))
return 0;
if (e->need_free) {
char *x, *y = NULL;
x = strdup(e->name);
if (!x)
return -ENOMEM;
if (e->message) {
y = strdup(e->message);
if (!y) {
free(x);
return -ENOMEM;
}
}
dest->name = x;
dest->message = y;
dest->need_free = true;
} else
*dest = *e;
return 0;
}
void sd_bus_error_set_const(sd_bus_error *e, const char *name, const char *message) {
if (!e)
return;
if (sd_bus_error_is_set(e))
return;
e->name = name;
e->message = message;
}
int sd_bus_error_is_set(const sd_bus_error *e) {
if (!e)
return 0;
return e->name || e->message || e->need_free;
}
int sd_bus_error_has_name(const sd_bus_error *e, const char *name) {
if (!e)
return 0;
return streq_ptr(e->name, name);
}
int bus_error_to_errno(const sd_bus_error* e) {
/* Better replce this with a gperf table */
if (!e->name)
return -EIO;
if (streq(e->name, "org.freedesktop.DBus.Error.NoMemory"))
return -ENOMEM;
if (streq(e->name, "org.freedesktop.DBus.Error.AuthFailed") ||
streq(e->name, "org.freedesktop.DBus.Error.AccessDenied"))
return -EPERM;
return -EIO;
}
int bus_error_from_errno(sd_bus_error *e, int error) {
if (!e)
return error;
if (error == -ENOMEM)
sd_bus_error_set_const(e, "org.freedesktop.DBus.Error.NoMemory", strerror(-error));
else if (error == -EPERM || error == EACCES)
sd_bus_error_set_const(e, "org.freedesktop.DBus.Error.AccessDenied", strerror(-error));
else
sd_bus_error_set_const(e, "org.freedesktop.DBus.Error.Failed", "Operation failed");
return error;
}
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
#pragma once
/***
This file is part of systemd.
Copyright 2013 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 "sd-bus.h"
int bus_error_to_errno(const sd_bus_error *e);
int bus_error_from_errno(sd_bus_error *e, int error);
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
/***
This file is part of systemd.
Copyright 2013 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 "bus-internal.h"
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
#pragma once
/***
This file is part of systemd.
Copyright 2013 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 <sys/socket.h>
#include <sys/un.h>
#include <netinet/in.h>