[systemd-commits] 11 commits - .gitignore Makefile.am TODO configure.ac src/journal src/libsystemd-bus src/shared src/stdio-bridge
Lennart Poettering
lennart at kemper.freedesktop.org
Wed Mar 20 15:08:40 PDT 2013
.gitignore | 4
Makefile.am | 70
TODO | 3
configure.ac | 4
src/journal/journal-gatewayd.c | 1
src/libsystemd-bus/Makefile | 1
src/libsystemd-bus/bus-control.c | 336 +++
src/libsystemd-bus/bus-error.c | 170 +
src/libsystemd-bus/bus-error.h | 29
src/libsystemd-bus/bus-internal.c | 22
src/libsystemd-bus/bus-internal.h | 106 +
src/libsystemd-bus/bus-message.c | 2766 ++++++++++++++++++++++++++++++++
src/libsystemd-bus/bus-message.h | 130 +
src/libsystemd-bus/bus-signature.c | 141 +
src/libsystemd-bus/bus-signature.h | 31
src/libsystemd-bus/bus-type.c | 163 +
src/libsystemd-bus/bus-type.h | 34
src/libsystemd-bus/busctl.c | 106 +
src/libsystemd-bus/sd-bus-protocol.h | 105 +
src/libsystemd-bus/sd-bus.c | 1529 +++++++++++++++++
src/libsystemd-bus/sd-bus.h | 152 +
src/libsystemd-bus/test-bus-chat.c | 242 ++
src/libsystemd-bus/test-bus-marshal.c | 170 +
src/libsystemd-bus/test-bus-signature.c | 74
src/shared/macro.h | 4
src/shared/util.c | 89 +
src/shared/util.h | 4
src/stdio-bridge/stdio-bridge.c | 2
28 files changed, 6484 insertions(+), 4 deletions(-)
New commits:
commit 09f727eebd87661f263d3c2c1e0de7b7771acd40
Author: Lennart Poettering <lennart at poettering.net>
Date: Wed Mar 20 22:57:55 2013 +0100
update TODO
diff --git a/TODO b/TODO
index b441700..1915412 100644
--- a/TODO
+++ b/TODO
@@ -49,6 +49,9 @@ Features:
- Use for seamless upgrades, by running the new server before stopping the
old.
+* after all byte-wise realloc() is slow, even on glibc, so i guess we
+ need manual exponential loops after all
+
* BootLoaderSpec: drop allowing ext234 for $BOOT. Clarify that the
kernel has to be in $BOOT. Clarify that the boot loader should be
installed to the ESP. Define a way how an installer can figure out
commit 89ffcd2ad5bf87866314e96c4179ba622851cd6e
Author: Lennart Poettering <lennart at poettering.net>
Date: Wed Mar 20 22:56:25 2013 +0100
bus: hook up client with socket communication
diff --git a/.gitignore b/.gitignore
index 917b066..2d95f14 100644
--- a/.gitignore
+++ b/.gitignore
@@ -4,6 +4,7 @@
/ata_id
/bootctl
/build-aux
+/busctl
/cdrom_id
/collect
/gtk-doc.make
@@ -82,6 +83,7 @@
/tags
/test-bus-marshal
/test-bus-signature
+/test-bus-chat
/test-calendarspec
/test-catalog
/test-cgroup
diff --git a/Makefile.am b/Makefile.am
index c94c192..d8e9b16 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -1654,12 +1654,19 @@ libsystemd_bus_la_SOURCES = \
src/libsystemd-bus/bus-type.c \
src/libsystemd-bus/bus-type.h
+libsystemd_bus_la_LIBADD = \
+ libsystemd-id128-internal.la
+
noinst_LTLIBRARIES += \
libsystemd-bus.la
noinst_tests += \
test-bus-marshal \
- test-bus-signature
+ test-bus-signature \
+ test-bus-chat
+
+noinst_PROGRAMS += \
+ busctl
test_bus_marshal_SOURCES = \
src/libsystemd-bus/test-bus-marshal.c
@@ -1682,6 +1689,25 @@ test_bus_signature_LDADD = \
libsystemd-shared.la \
libsystemd-bus.la
+test_bus_chat_SOURCES = \
+ src/libsystemd-bus/test-bus-chat.c
+
+test_bus_chat_CFLAGS = \
+ $(AM_CFLAGS) \
+ -pthread
+
+test_bus_chat_LDADD = \
+ libsystemd-shared.la \
+ libsystemd-bus.la \
+ libsystemd-id128-internal.la
+
+busctl_SOURCES = \
+ src/libsystemd-bus/busctl.c
+
+busctl_LDADD = \
+ libsystemd-shared.la \
+ libsystemd-bus.la
+
# ------------------------------------------------------------------------------
if ENABLE_GTK_DOC
SUBDIRS += \
diff --git a/src/libsystemd-bus/bus-control.c b/src/libsystemd-bus/bus-control.c
index e4cc251..50e1a2c 100644
--- a/src/libsystemd-bus/bus-control.c
+++ b/src/libsystemd-bus/bus-control.c
@@ -107,7 +107,6 @@ int sd_bus_release_name(sd_bus *bus, const char *name) {
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;
@@ -144,17 +143,17 @@ int sd_bus_list_names(sd_bus *bus, char ***l) {
if (r < 0)
return r;
- r = sd_bus_message_read(reply1, "as", &a);
- if (r < 0)
+ r = bus_message_read_strv_extend(reply1, &x);
+ if (r < 0) {
+ strv_free(x);
return r;
+ }
- r = sd_bus_message_read(reply2, "as", &b);
- if (r < 0)
+ r = bus_message_read_strv_extend(reply2, &x);
+ if (r < 0) {
+ strv_free(x);
return r;
-
- x = strv_merge(a, b);
- if (!x)
- return -ENOMEM;
+ }
*l = strv_uniq(x);
return 0;
@@ -162,6 +161,7 @@ int sd_bus_list_names(sd_bus *bus, char ***l) {
int sd_bus_get_owner(sd_bus *bus, const char *name, char **owner) {
_cleanup_bus_message_unref_ sd_bus_message *m = NULL, *reply = NULL;
+ const char *found;
int r;
if (!bus)
@@ -187,7 +187,21 @@ int sd_bus_get_owner(sd_bus *bus, const char *name, char **owner) {
if (r < 0)
return r;
- return sd_bus_message_read(reply, "s", owner);
+ r = sd_bus_message_read(reply, "s", &found);
+ if (r < 0)
+ return r;
+
+ if (owner) {
+ char *t;
+
+ t = strdup(found);
+ if (!t)
+ return -ENOMEM;
+
+ *owner = t;
+ }
+
+ return 0;
}
int sd_bus_get_owner_uid(sd_bus *bus, const char *name, uid_t *uid) {
@@ -245,7 +259,7 @@ int sd_bus_get_owner_pid(sd_bus *bus, const char *name, pid_t *pid) {
"org.freedesktop.DBus",
"/",
"org.freedesktop.DBus",
- "GetConnectionUnixUser",
+ "GetConnectionUnixProcessID",
&m);
if (r < 0)
return r;
diff --git a/src/libsystemd-bus/bus-error.c b/src/libsystemd-bus/bus-error.c
index 015fae7..f86da62 100644
--- a/src/libsystemd-bus/bus-error.c
+++ b/src/libsystemd-bus/bus-error.c
@@ -86,6 +86,8 @@ int sd_bus_error_set(sd_bus_error *e, const char *name, const char *format, ...)
}
int sd_bus_error_copy(sd_bus_error *dest, const sd_bus_error *e) {
+ char *x, *y = NULL;
+
if (!dest)
return 0;
if (bus_error_is_dirty(dest))
@@ -93,27 +95,21 @@ int sd_bus_error_copy(sd_bus_error *dest, const sd_bus_error *e) {
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;
- x = strdup(e->name);
- if (!x)
+ if (e->message) {
+ y = strdup(e->message);
+ if (!y) {
+ free(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;
-
+ dest->name = x;
+ dest->message = y;
+ dest->need_free = true;
return 0;
}
diff --git a/src/libsystemd-bus/bus-internal.h b/src/libsystemd-bus/bus-internal.h
index d8cdc48..9a12d66 100644
--- a/src/libsystemd-bus/bus-internal.h
+++ b/src/libsystemd-bus/bus-internal.h
@@ -50,8 +50,7 @@ enum bus_state {
BUS_OPENING,
BUS_AUTHENTICATING,
BUS_HELLO,
- BUS_RUNNING,
- BUS_CLOSED
+ BUS_RUNNING
};
struct sd_bus {
@@ -60,7 +59,7 @@ struct sd_bus {
int fd;
int message_version;
bool can_fds:1;
- bool send_hello:1;
+ bool sent_hello:1;
void *rbuffer;
size_t rbuffer_size;
@@ -99,3 +98,9 @@ struct sd_bus {
size_t auth_size;
char *auth_uid;
};
+
+static inline void bus_unrefp(sd_bus **b) {
+ sd_bus_unref(*b);
+}
+
+#define _cleanup_bus_unref_ __attribute__((cleanup(bus_unrefp)))
diff --git a/src/libsystemd-bus/bus-message.c b/src/libsystemd-bus/bus-message.c
index 5f3dbdb..c385ef5 100644
--- a/src/libsystemd-bus/bus-message.c
+++ b/src/libsystemd-bus/bus-message.c
@@ -23,6 +23,7 @@
#include "util.h"
#include "utf8.h"
+#include "strv.h"
#include "sd-bus.h"
#include "bus-message.h"
@@ -322,6 +323,8 @@ int sd_bus_message_new_signal(
if (!t)
return -ENOMEM;
+ t->header->flags |= SD_BUS_MESSAGE_NO_REPLY_EXPECTED;
+
r = message_append_field_string(t, SD_BUS_MESSAGE_HEADER_PATH, SD_BUS_TYPE_OBJECT_PATH, path, &t->path);
if (r < 0)
goto fail;
@@ -409,6 +412,7 @@ static int message_new_reply(
if (!t)
return -ENOMEM;
+ t->header->flags |= SD_BUS_MESSAGE_NO_REPLY_EXPECTED;
t->reply_serial = BUS_MESSAGE_SERIAL(call);
r = message_append_field_uint32(t, SD_BUS_MESSAGE_HEADER_REPLY_SERIAL, t->reply_serial);
@@ -424,6 +428,7 @@ static int message_new_reply(
t->dont_send = !!(call->header->flags & SD_BUS_MESSAGE_NO_REPLY_EXPECTED);
*m = t;
+ return 0;
fail:
message_free(t);
@@ -1891,7 +1896,7 @@ eof:
return 0;
}
-int sd_bus_message_rewind(sd_bus_message *m, bool complete) {
+int sd_bus_message_rewind(sd_bus_message *m, int complete) {
struct bus_container *c;
if (!m)
@@ -2424,6 +2429,10 @@ static int message_parse_fields(sd_bus_message *m) {
break;
}
+ /* Try to read the error message, but if we can't it's a non-issue */
+ if (m->header->type == SD_BUS_MESSAGE_TYPE_METHOD_ERROR)
+ sd_bus_message_read(m, "s", &m->error.message);
+
return 0;
}
@@ -2724,3 +2733,34 @@ int bus_message_get_blob(sd_bus_message *m, void **buffer, size_t *sz) {
return 0;
}
+
+int bus_message_read_strv_extend(sd_bus_message *m, char ***l) {
+ int r;
+
+ assert(m);
+ assert(l);
+
+ r = sd_bus_message_enter_container(m, 'a', "s");
+ if (r < 0)
+ return r;
+
+ for (;;) {
+ const char *s;
+
+ r = sd_bus_message_read_basic(m, 's', &s);
+ if (r < 0)
+ return r;
+ if (r == 0)
+ break;
+
+ r = strv_extend(l, s);
+ if (r < 0)
+ return r;
+ }
+
+ r = sd_bus_message_exit_container(m);
+ if (r < 0)
+ return r;
+
+ return 0;
+}
diff --git a/src/libsystemd-bus/bus-message.h b/src/libsystemd-bus/bus-message.h
index 3035efc..0184d18 100644
--- a/src/libsystemd-bus/bus-message.h
+++ b/src/libsystemd-bus/bus-message.h
@@ -123,8 +123,8 @@ static inline void bus_message_unrefp(sd_bus_message **m) {
#define _cleanup_bus_message_unref_ __attribute__((cleanup(bus_message_unrefp)))
-int bus_message_parse(sd_bus_message *m);
int bus_message_seal(sd_bus_message *m, uint64_t serial);
int bus_message_dump(sd_bus_message *m);
int bus_message_get_blob(sd_bus_message *m, void **buffer, size_t *sz);
int bus_message_from_malloc(void *buffer, size_t length, sd_bus_message **ret);
+int bus_message_read_strv_extend(sd_bus_message *m, char ***l);
diff --git a/src/libsystemd-bus/busctl.c b/src/libsystemd-bus/busctl.c
index 88446fc..7348894 100644
--- a/src/libsystemd-bus/busctl.c
+++ b/src/libsystemd-bus/busctl.c
@@ -19,15 +19,22 @@
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
+#include "strv.h"
+#include "util.h"
+#include "log.h"
+
#include "sd-bus.h"
+#include "bus-message.h"
+#include "bus-internal.h"
int main(int argc, char *argv[]) {
_cleanup_bus_unref_ sd_bus *bus = NULL;
_cleanup_strv_free_ char **l = NULL;
char **i;
int r;
+ size_t max_i = 0;
- r = bus_open_system(&bus);
+ r = sd_bus_open_user(&bus);
if (r < 0) {
log_error("Failed to connect to bus: %s", strerror(-r));
goto fail;
@@ -39,26 +46,57 @@ int main(int argc, char *argv[]) {
goto fail;
}
+ strv_sort(l);
+
+ STRV_FOREACH(i, l)
+ max_i = MAX(max_i, strlen(*i));
+
+ printf("%-*s %*s %-*s %-*s CONNECTION\n",
+ (int) max_i, "NAME", 10, "PID", 15, "PROCESS", 16, "USER");
+
STRV_FOREACH(i, l) {
_cleanup_free_ char *owner = NULL;
- pid_t pid = 0;
+ pid_t pid;
uid_t uid;
- bool uid_valid;
- r = sd_bus_get_owner(bus, *i, &owner);
- if (r == -ENXIO)
- continue;
+ /* if ((*i)[0] == ':') */
+ /* continue; */
+
+ printf("%-*s", (int) max_i, *i);
- r = sd_get_owner_pid(bus, *i, &pid);
- if (r == -ENXIO)
- continue;
+ r = sd_bus_get_owner_pid(bus, *i, &pid);
+ if (r >= 0) {
+ _cleanup_free_ char *comm = NULL;
- r = sd_get_owner_uid(bus, *i, &pid);
- if (r == -ENXIO)
- continue;
- uid_valid = r >= 0;
+ printf(" %10lu", (unsigned long) pid);
- printf("%s (%s) %llu %llu\n", *i, owner, (unsigned long long) pid, (unsigned long long) uid);
+ get_process_comm(pid, &comm);
+ printf(" %-15s", strna(comm));
+ } else
+ printf(" - - ");
+
+ r = sd_bus_get_owner_uid(bus, *i, &uid);
+ if (r >= 0) {
+ _cleanup_free_ char *u = NULL;
+
+ u = uid_to_name(uid);
+ if (!u) {
+ log_oom();
+ goto fail;
+ }
+
+ if (strlen(u) > 16)
+ u[16] = 0;
+
+ printf(" %-16s", u);
+ } else
+ printf(" - ");
+
+ r = sd_bus_get_owner(bus, *i, &owner);
+ if (r >= 0)
+ printf(" %s\n", owner);
+ else
+ printf(" -\n");
}
r = 0;
diff --git a/src/libsystemd-bus/sd-bus.c b/src/libsystemd-bus/sd-bus.c
index c5cbb56..f27d47c 100644
--- a/src/libsystemd-bus/sd-bus.c
+++ b/src/libsystemd-bus/sd-bus.c
@@ -39,6 +39,7 @@
static void bus_free(sd_bus *b) {
struct filter_callback *f;
+ unsigned i;
assert(b);
@@ -46,9 +47,17 @@ static void bus_free(sd_bus *b) {
close_nointr_nofail(b->fd);
free(b->rbuffer);
+ free(b->unique_name);
+ free(b->auth_uid);
+ free(b->address);
+
+ for (i = 0; i < b->rqueue_size; i++)
+ sd_bus_message_unref(b->rqueue[i]);
free(b->rqueue);
+
+ for (i = 0; i < b->wqueue_size; i++)
+ sd_bus_message_unref(b->wqueue[i]);
free(b->wqueue);
- free(b->unique_name);
hashmap_free_free(b->reply_callbacks);
@@ -122,24 +131,19 @@ static int bus_send_hello(sd_bus *bus) {
if (r < 0)
return r;
- return 0;
+ bus->sent_hello = true;
+ return r;
}
static int bus_start_running(sd_bus *bus) {
- int r;
-
assert(bus);
- if (bus->send_hello) {
+ if (bus->sent_hello) {
bus->state = BUS_HELLO;
-
- r = bus_send_hello(bus);
- if (r < 0)
- return r;
+ return 0;
}
bus->state = BUS_RUNNING;
-
return 0;
}
@@ -154,7 +158,7 @@ static int parse_address_key(const char **p, const char *key, char **value) {
assert(value);
l = strlen(key);
- if (!strncmp(*p, key, l) != 0)
+ if (strncmp(*p, key, l) != 0)
return 0;
if ((*p)[l] != '=')
@@ -164,7 +168,7 @@ static int parse_address_key(const char **p, const char *key, char **value) {
return -EINVAL;
a = *p + l + 1;
- while (*a != ';' && *a != 0) {
+ while (*a != ',' && *a != 0) {
char c, *t;
if (*a == '%') {
@@ -182,12 +186,14 @@ static int parse_address_key(const char **p, const char *key, char **value) {
return y;
}
- a += 3;
c = (char) ((x << 4) | y);
- } else
+ a += 3;
+ } else {
c = *a;
+ a++;
+ }
- t = realloc(r, n + 1);
+ t = realloc(r, n + 2);
if (!t) {
free(r);
return -ENOMEM;
@@ -197,6 +203,16 @@ static int parse_address_key(const char **p, const char *key, char **value) {
r[n++] = c;
}
+ if (!r) {
+ r = strdup("");
+ if (!r)
+ return -ENOMEM;
+ } else
+ r[n] = 0;
+
+ if (*a == ',')
+ a++;
+
*p = a;
*value = r;
return 1;
@@ -206,7 +222,10 @@ static void skip_address_key(const char **p) {
assert(p);
assert(*p);
- *p += strcspn(*p, ";");
+ *p += strcspn(*p, ",");
+
+ if (**p == ',')
+ (*p) ++;
}
static int bus_parse_next_address(sd_bus *b) {
@@ -231,7 +250,7 @@ static int bus_parse_next_address(sd_bus *b) {
_cleanup_free_ char *path = NULL, *abstract = NULL;
p = a + 5;
- while (*p != 0 && *p != ';') {
+ while (*p != 0) {
r = parse_address_key(&p, "guid", &guid);
if (r < 0)
return r;
@@ -272,13 +291,13 @@ static int bus_parse_next_address(sd_bus *b) {
} else if (abstract) {
size_t l;
- l = strlen(path);
+ l = strlen(abstract);
if (l > sizeof(b->sockaddr.un.sun_path) - 1)
return -E2BIG;
b->sockaddr.un.sun_family = AF_UNIX;
b->sockaddr.un.sun_path[0] = 0;
- strncpy(b->sockaddr.un.sun_path+1, path, sizeof(b->sockaddr.un.sun_path)-1);
+ strncpy(b->sockaddr.un.sun_path+1, abstract, sizeof(b->sockaddr.un.sun_path)-1);
b->sockaddr_size = offsetof(struct sockaddr_un, sun_path) + 1 + l;
}
@@ -287,7 +306,7 @@ static int bus_parse_next_address(sd_bus *b) {
struct addrinfo hints, *result;
p = a + 4;
- while (*p != 0 && *p != ';') {
+ while (*p != 0) {
r = parse_address_key(&p, "guid", &guid);
if (r < 0)
return r;
@@ -409,7 +428,7 @@ static int bus_auth_verify(sd_bus *b) {
if (!e)
return 0;
- f = memmem(e, b->rbuffer_size - (e - (char*) b->rbuffer), "\r\n", 2);
+ f = memmem(e + 2, b->rbuffer_size - (e - (char*) b->rbuffer) - 2, "\r\n", 2);
if (!f)
return 0;
@@ -423,7 +442,7 @@ static int bus_auth_verify(sd_bus *b) {
int x, y;
x = unhexchar(((char*) b->rbuffer)[3 + i]);
- y = unhexchar(((char*) b->rbuffer)[3 + i + 2]);
+ y = unhexchar(((char*) b->rbuffer)[3 + i + 1]);
if (x < 0 || y < 0)
return -EINVAL;
@@ -446,6 +465,8 @@ static int bus_auth_verify(sd_bus *b) {
memmove(b->rbuffer, f + 2, b->rbuffer_size);
}
+ b->rbuffer_size = 0;
+
r = bus_start_running(b);
if (r < 0)
return r;
@@ -459,6 +480,7 @@ static int bus_read_auth(sd_bus *b) {
size_t n;
ssize_t k;
int r;
+ void *p;
assert(b);
@@ -467,6 +489,11 @@ static int bus_read_auth(sd_bus *b) {
return r;
n = MAX(3 + 32 + 2 + sizeof("AGREE_UNIX_FD") - 1 + 2, b->rbuffer_size * 2);
+ p = realloc(b->rbuffer, n);
+ if (!p)
+ return -ENOMEM;
+
+ b->rbuffer = p;
zero(iov);
iov.iov_base = (uint8_t*) b->rbuffer + b->rbuffer_size;
@@ -490,7 +517,7 @@ static int bus_read_auth(sd_bus *b) {
}
static int bus_start_auth(sd_bus *b) {
- static const char auth_prefix[] = "\0AUTH_EXTERNAL ";
+ static const char auth_prefix[] = "\0AUTH EXTERNAL ";
static const char auth_suffix[] = "\r\nNEGOTIATE_UNIX_FD\r\nBEGIN\r\n";
char text[20 + 1]; /* enough space for a 64bit integer plus NUL */
@@ -570,25 +597,25 @@ int sd_bus_open_system(sd_bus **ret) {
r = sd_bus_open_address(e, &b);
if (r < 0)
return r;
+ } else {
+ b = bus_new();
+ if (!b)
+ return -ENOMEM;
- b->send_hello = true;
- *ret = b;
- return r;
- }
-
- b = bus_new();
- if (!b)
- return -ENOMEM;
-
- b->send_hello = true;
+ b->sockaddr.un.sun_family = AF_UNIX;
+ strncpy(b->sockaddr.un.sun_path, "/run/dbus/system_bus_socket", sizeof(b->sockaddr.un.sun_path));
+ b->sockaddr_size = offsetof(struct sockaddr_un, sun_path) + sizeof("/run/dbus/system_bus_socket") - 1;
- b->sockaddr.un.sun_family = AF_UNIX;
- strncpy(b->sockaddr.un.sun_path, "/run/dbus/system_bus_socket", sizeof(b->sockaddr.un.sun_path));
- b->sockaddr_size = offsetof(struct sockaddr_un, sun_path) + sizeof("/run/dbus/system_bus_socket") - 1;
+ r = bus_start_connect(b);
+ if (r < 0) {
+ bus_free(b);
+ return r;
+ }
+ }
- r = bus_start_connect(b);
+ r = bus_send_hello(b);
if (r < 0) {
- bus_free(b);
+ sd_bus_unref(b);
return r;
}
@@ -610,33 +637,33 @@ int sd_bus_open_user(sd_bus **ret) {
r = sd_bus_open_address(e, &b);
if (r < 0)
return r;
+ } else {
+ e = getenv("XDG_RUNTIME_DIR");
+ if (!e)
+ return -ENOENT;
- b->send_hello = true;
- *ret = b;
- return r;
- }
-
- e = getenv("XDG_RUNTIME_DIR");
- if (!e)
- return -ENOENT;
-
- l = strlen(e);
- if (l + 4 > sizeof(b->sockaddr.un.sun_path))
- return -E2BIG;
+ l = strlen(e);
+ if (l + 4 > sizeof(b->sockaddr.un.sun_path))
+ return -E2BIG;
- b = bus_new();
- if (!b)
- return -ENOMEM;
+ b = bus_new();
+ if (!b)
+ return -ENOMEM;
- b->send_hello = true;
+ b->sockaddr.un.sun_family = AF_UNIX;
+ memcpy(mempcpy(b->sockaddr.un.sun_path, e, l), "/bus", 4);
+ b->sockaddr_size = offsetof(struct sockaddr_un, sun_path) + l + 4;
- b->sockaddr.un.sun_family = AF_UNIX;
- memcpy(mempcpy(b->sockaddr.un.sun_path, e, l), "/bus", 4);
- b->sockaddr_size = offsetof(struct sockaddr_un, sun_path) + l + 4;
+ r = bus_start_connect(b);
+ if (r < 0) {
+ bus_free(b);
+ return r;
+ }
+ }
- r = bus_start_connect(b);
+ r = bus_send_hello(b);
if (r < 0) {
- bus_free(b);
+ sd_bus_unref(b);
return r;
}
@@ -747,6 +774,8 @@ int sd_bus_can_send(sd_bus *bus, char type) {
if (!bus)
return -EINVAL;
+ if (bus->state != BUS_RUNNING && bus->state != BUS_HELLO)
+ return -EAGAIN;
if (type == SD_BUS_TYPE_UNIX_FD)
return bus->can_fds;
@@ -757,6 +786,9 @@ int sd_bus_can_send(sd_bus *bus, char type) {
static int bus_seal_message(sd_bus *b, sd_bus_message *m) {
assert(m);
+ if (m->header->version > b->message_version)
+ return -EPERM;
+
if (m->sealed)
return 0;
@@ -773,6 +805,7 @@ static int message_write(sd_bus *bus, sd_bus_message *m, size_t *idx) {
assert(bus);
assert(m);
assert(idx);
+ assert(bus->state == BUS_RUNNING || bus->state == BUS_HELLO);
n = m->n_iovec * sizeof(struct iovec);
iov = alloca(n);
@@ -792,7 +825,7 @@ static int message_write(sd_bus *bus, sd_bus_message *m, size_t *idx) {
*idx += (size_t) k;
iovec_advance(iov, &j, *idx);
- return j > m->n_iovec;
+ return j >= m->n_iovec;
}
static int message_read_need(sd_bus *bus, size_t *need) {
@@ -801,8 +834,9 @@ static int message_read_need(sd_bus *bus, size_t *need) {
assert(bus);
assert(need);
+ assert(bus->state == BUS_RUNNING || bus->state == BUS_HELLO);
- if (bus->rbuffer_size <= sizeof(struct bus_header)) {
+ if (bus->rbuffer_size < sizeof(struct bus_header)) {
*need = sizeof(struct bus_header);
return 0;
}
@@ -818,9 +852,9 @@ static int message_read_need(sd_bus *bus, size_t *need) {
a = be32toh(a);
b = be32toh(b);
} else
- return -EIO;
+ return -EBADMSG;
- *need = sizeof(struct bus_header) + ALIGN_TO(a, 8) + b;
+ *need = sizeof(struct bus_header) + ALIGN_TO(b, 8) + a;
return 0;
}
@@ -832,6 +866,7 @@ static int message_make(sd_bus *bus, size_t size, sd_bus_message **m) {
assert(bus);
assert(m);
assert(bus->rbuffer_size >= size);
+ assert(bus->state == BUS_RUNNING || bus->state == BUS_HELLO);
if (bus->rbuffer_size > size) {
b = memdup((const uint8_t*) bus->rbuffer + size, bus->rbuffer_size - size);
@@ -850,12 +885,6 @@ static int message_make(sd_bus *bus, size_t size, sd_bus_message **m) {
bus->rbuffer = b;
bus->rbuffer_size -= size;
- r = bus_message_parse(t);
- if (r < 0) {
- sd_bus_message_unref(t);
- return r;
- }
-
*m = t;
return 1;
}
@@ -870,6 +899,7 @@ static int message_read(sd_bus *bus, sd_bus_message **m) {
assert(bus);
assert(m);
+ assert(bus->state == BUS_RUNNING || bus->state == BUS_HELLO);
r = message_read_need(bus, &need);
if (r < 0)
@@ -882,6 +912,8 @@ static int message_read(sd_bus *bus, sd_bus_message **m) {
if (!b)
return -ENOMEM;
+ bus->rbuffer = b;
+
zero(iov);
iov.iov_base = (uint8_t*) bus->rbuffer + bus->rbuffer_size;
iov.iov_len = need - bus->rbuffer_size;
@@ -910,6 +942,7 @@ static int dispatch_wqueue(sd_bus *bus) {
int r, c = 0;
assert(bus);
+ assert(bus->state == BUS_RUNNING || bus->state == BUS_HELLO);
if (bus->fd < 0)
return -ENOTCONN;
@@ -951,6 +984,7 @@ static int dispatch_rqueue(sd_bus *bus, sd_bus_message **m) {
assert(bus);
assert(m);
+ assert(bus->state == BUS_RUNNING || bus->state == BUS_HELLO);
if (bus->fd < 0)
return -ENOTCONN;
@@ -983,8 +1017,6 @@ int sd_bus_send(sd_bus *bus, sd_bus_message *m, uint64_t *serial) {
return -ENOTCONN;
if (!m)
return -EINVAL;
- if (m->header->version > bus->message_version)
- return -EPERM;
r = bus_seal_message(bus, m);
if (r < 0)
@@ -995,7 +1027,7 @@ int sd_bus_send(sd_bus *bus, sd_bus_message *m, uint64_t *serial) {
if (m->dont_send && !serial)
return 0;
- if (bus->wqueue_size <= 0) {
+ if ((bus->state == BUS_RUNNING || bus->state == BUS_HELLO) && bus->wqueue_size <= 0) {
size_t idx = 0;
r = message_write(bus, m, &idx);
@@ -1057,14 +1089,20 @@ int sd_bus_send_with_reply(
if (!bus)
return -EINVAL;
- if (!bus->fd < 0)
+ if (bus->fd < 0)
return -ENOTCONN;
if (!m)
return -EINVAL;
if (!callback)
return -EINVAL;
- if (!m->header->type != SD_BUS_MESSAGE_TYPE_METHOD_CALL)
+ if (m->header->type != SD_BUS_MESSAGE_TYPE_METHOD_CALL)
return -EINVAL;
+ if (m->header->flags & SD_BUS_MESSAGE_NO_REPLY_EXPECTED)
+ return -EINVAL;
+
+ r = hashmap_ensure_allocated(&bus->reply_callbacks, uint64_hash_func, uint64_compare_func);
+ if (r < 0)
+ return r;
r = bus_seal_message(bus, m);
if (r < 0)
@@ -1111,6 +1149,30 @@ int sd_bus_send_with_reply_cancel(sd_bus *bus, uint64_t serial) {
return 1;
}
+static int ensure_running(sd_bus *bus) {
+ int r;
+
+ assert(bus);
+
+ r = sd_bus_is_running(bus);
+ if (r != 0)
+ return r;
+
+ for (;;) {
+ r = sd_bus_process(bus, NULL);
+ if (r < 0)
+ return r;
+
+ r = sd_bus_is_running(bus);
+ if (r != 0)
+ return r;
+
+ r = sd_bus_wait(bus, (uint64_t) -1);
+ if (r < 0)
+ return r;
+ }
+}
+
int sd_bus_send_with_reply_and_block(
sd_bus *bus,
sd_bus_message *m,
@@ -1125,14 +1187,20 @@ int sd_bus_send_with_reply_and_block(
if (!bus)
return -EINVAL;
- if (!bus->fd < 0)
+ if (bus->fd < 0)
return -ENOTCONN;
if (!m)
return -EINVAL;
- if (!m->header->type != SD_BUS_MESSAGE_TYPE_METHOD_CALL)
+ if (m->header->type != SD_BUS_MESSAGE_TYPE_METHOD_CALL)
return -EINVAL;
- if (sd_bus_error_is_dirty(error))
+ if (m->header->flags & SD_BUS_MESSAGE_NO_REPLY_EXPECTED)
return -EINVAL;
+ if (bus_error_is_dirty(error))
+ return -EINVAL;
+
+ r = ensure_running(bus);
+ if (r < 0)
+ return r;
r = sd_bus_send(bus, m, &serial);
if (r < 0)
@@ -1162,6 +1230,9 @@ int sd_bus_send_with_reply_and_block(
if (r < 0)
return r;
if (r > 0) {
+ /* bus_message_dump(incoming); */
+ /* sd_bus_message_rewind(incoming, true); */
+
if (incoming->reply_serial == serial) {
/* Found a match! */
@@ -1223,7 +1294,7 @@ int sd_bus_get_fd(sd_bus *bus) {
return -EINVAL;
if (bus->fd < 0)
- return -EINVAL;
+ return -ENOTCONN;
return bus->fd;
}
@@ -1233,13 +1304,19 @@ int sd_bus_get_events(sd_bus *bus) {
if (!bus)
return -EINVAL;
-
if (bus->fd < 0)
- return -EINVAL;
+ return -ENOTCONN;
if (bus->state == BUS_OPENING)
flags |= POLLOUT;
- else if (bus->state == BUS_RUNNING || bus->state == BUS_HELLO) {
+ else if (bus->state == BUS_AUTHENTICATING) {
+
+ if (bus->auth_index < ELEMENTSOF(bus->auth_iovec))
+ flags |= POLLOUT;
+
+ flags |= POLLIN;
+
+ } else if (bus->state == BUS_RUNNING || bus->state == BUS_HELLO) {
if (bus->rqueue_size <= 0)
flags |= POLLIN;
if (bus->wqueue_size > 0)
@@ -1250,7 +1327,6 @@ int sd_bus_get_events(sd_bus *bus) {
}
int sd_bus_process(sd_bus *bus, sd_bus_message **ret) {
- sd_bus_message *m;
int r;
if (!bus)
@@ -1270,7 +1346,7 @@ int sd_bus_process(sd_bus *bus, sd_bus_message **ret) {
return -errno;
if (p.revents & (POLLOUT|POLLERR|POLLHUP)) {
- int error;
+ int error = 0;
socklen_t slen = sizeof(error);
r = getsockopt(bus->fd, SOL_SOCKET, SO_ERROR, &error, &slen);
@@ -1297,13 +1373,14 @@ int sd_bus_process(sd_bus *bus, sd_bus_message **ret) {
return r;
r = bus_read_auth(bus);
- if (r <= 0)
+ if (r < 0)
return r;
- return bus_start_running(bus);
+ return 0;
} else if (bus->state == BUS_RUNNING || bus->state == BUS_HELLO) {
struct filter_callback *l;
+ _cleanup_bus_message_unref_ sd_bus_message *m = NULL;
r = dispatch_wqueue(bus);
if (r < 0)
@@ -1321,31 +1398,40 @@ int sd_bus_process(sd_bus *bus, sd_bus_message **ret) {
r = c->callback(bus, m, c->userdata);
free(c);
- if (r != 0) {
- sd_bus_message_unref(m);
+ if (r != 0)
return r < 0 ? r : 0;
- }
}
}
LIST_FOREACH(callbacks, l, bus->filter_callbacks) {
r = l->callback(bus, m, l->userdata);
- if (r != 0) {
- sd_bus_message_unref(m);
+ if (r != 0)
return r < 0 ? r : 0;
- }
}
if (ret) {
*ret = m;
+ m = NULL;
return 1;
}
- sd_bus_message_unref(m);
+ if (sd_bus_message_is_method_call(m, NULL, NULL)) {
+ const sd_bus_error e = SD_BUS_ERROR_INIT_CONST("org.freedesktop.DBus.Error.UnknownObject", "Unknown object.");
+ _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
+
+ r = sd_bus_message_new_method_error(bus, m, &e, &reply);
+ if (r < 0)
+ return r;
+
+ r = sd_bus_send(bus, reply, NULL);
+ if (r < 0)
+ return r;
+ }
+
return 0;
}
- return -ENOTSUP;
+ assert_not_reached("Unknown state");
}
int sd_bus_wait(sd_bus *bus, uint64_t timeout_usec) {
@@ -1356,7 +1442,10 @@ int sd_bus_wait(sd_bus *bus, uint64_t timeout_usec) {
if (!bus)
return -EINVAL;
if (bus->fd < 0)
- return -ECONNREFUSED;
+ return -ENOTCONN;
+
+ if (bus->rqueue_size > 0)
+ return 0;
e = sd_bus_get_events(bus);
if (e < 0)
@@ -1368,7 +1457,7 @@ int sd_bus_wait(sd_bus *bus, uint64_t timeout_usec) {
r = ppoll(&p, 1, timeout_usec == (uint64_t) -1 ? NULL : timespec_store(&ts, timeout_usec), NULL);
if (r < 0)
- return -EINVAL;
+ return -errno;
return r;
}
@@ -1381,7 +1470,11 @@ int sd_bus_flush(sd_bus *bus) {
if (bus->fd < 0)
return -ENOTCONN;
- if (bus->state == BUS_RUNNING && bus->wqueue_size <= 0)
+ r = ensure_running(bus);
+ if (r < 0)
+ return r;
+
+ if (bus->wqueue_size <= 0)
return 0;
for (;;) {
@@ -1389,7 +1482,7 @@ int sd_bus_flush(sd_bus *bus) {
if (r < 0)
return r;
- if (bus->state == BUS_RUNNING && bus->wqueue_size <= 0)
+ if (bus->wqueue_size <= 0)
return 0;
r = sd_bus_wait(bus, (uint64_t) -1);
diff --git a/src/libsystemd-bus/sd-bus.h b/src/libsystemd-bus/sd-bus.h
index d892562..03ea4b8 100644
--- a/src/libsystemd-bus/sd-bus.h
+++ b/src/libsystemd-bus/sd-bus.h
@@ -32,6 +32,12 @@
* - make unix fd passing work
* - add page donation logic
* - api for appending/reading fixed arrays
+ * - always verify container depth
+ * - implement method timeout logic
+ * - implicitly set no_reply when a message-call is sent an the serial number ignored
+ * - reduce number of ppoll()s if we can avoid it
+ * - handle NULL strings nicer when appending
+ * - merge busctl into systemctl or so?
*/
typedef struct sd_bus sd_bus;
@@ -117,7 +123,7 @@ int sd_bus_message_read_basic(sd_bus_message *m, char type, void *p);
int sd_bus_message_enter_container(sd_bus_message *m, char type, const char *contents);
int sd_bus_message_exit_container(sd_bus_message *m);
int sd_bus_message_peek_type(sd_bus_message *m, char *type, const char **contents);
-int sd_bus_message_rewind(sd_bus_message *m, bool complete);
+int sd_bus_message_rewind(sd_bus_message *m, int complete);
/* Bus management */
@@ -133,7 +139,8 @@ int sd_bus_remove_match(sd_bus *bus, const char *match);
/* Error structures */
-#define SD_BUS_ERROR_INIT (NULL, NULL, 0)
+#define SD_BUS_ERROR_INIT {NULL, NULL, 0}
+#define SD_BUS_ERROR_INIT_CONST(name, message) {(name), (message), 0}
void sd_bus_error_free(sd_bus_error *e);
int sd_bus_error_set(sd_bus_error *e, const char *name, const char *format, ...);
diff --git a/src/libsystemd-bus/test-bus-chat.c b/src/libsystemd-bus/test-bus-chat.c
new file mode 100644
index 0000000..4260ca3
--- /dev/null
+++ b/src/libsystemd-bus/test-bus-chat.c
@@ -0,0 +1,242 @@
+/*-*- 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 <assert.h>
+#include <stdlib.h>
+#include <pthread.h>
+
+#include "log.h"
+#include "util.h"
+
+#include "sd-bus.h"
+#include "bus-message.h"
+
+static int server_init(sd_bus **_bus) {
+ sd_bus *bus = NULL;
+ int r;
+
+ assert(_bus);
+
+ r = sd_bus_open_user(&bus);
+ if (r < 0) {
+ log_error("Failed to connect to user bus: %s", strerror(-r));
+ goto fail;
+ }
+
+ r = sd_bus_request_name(bus, "org.freedesktop.systemd.test", 0);
+ if (r < 0) {
+ log_error("Failed to acquire name: %s", strerror(-r));
+ goto fail;
+ }
+
+ *_bus = bus;
+ return 0;
+
+fail:
+ if (bus)
+ sd_bus_unref(bus);
+
+ return r;
+}
+
+static void* server(void *p) {
+ sd_bus *bus = p;
+ int r;
+
+ for (;;) {
+ _cleanup_bus_message_unref_ sd_bus_message *m = NULL, *reply = NULL;
+
+ r = sd_bus_process(bus, &m);
+ if (r < 0) {
+ log_error("Failed to process requests: %s", strerror(-r));
+ goto fail;
+ }
+ if (r == 0) {
+ r = sd_bus_wait(bus, (uint64_t) -1);
+ if (r < 0) {
+ log_error("Failed to wait: %s", strerror(-r));
+ goto fail;
+ }
+
+ continue;
+ }
+
+ log_info("Got message! %s", strna(sd_bus_message_get_member(m)));
+ /* bus_message_dump(m); */
+ /* sd_bus_message_rewind(m, true); */
+
+ if (sd_bus_message_is_method_call(m, "org.freedesktop.systemd.test", "LowerCase")) {
+ const char *hello;
+ _cleanup_free_ char *lowercase = NULL;
+
+ r = sd_bus_message_read(m, "s", &hello);
+ if (r < 0) {
+ log_error("Failed to get parameter: %s", strerror(-r));
+ goto fail;
+ }
+
+ r = sd_bus_message_new_method_return(bus, m, &reply);
+ if (r < 0) {
+ log_error("Failed to allocate return: %s", strerror(-r));
+ goto fail;
+ }
+
+ lowercase = strdup(hello);
+ if (!lowercase) {
+ r = log_oom();
+ goto fail;
+ }
+
+ ascii_strlower(lowercase);
+
+ r = sd_bus_message_append(reply, "s", lowercase);
+ if (r < 0) {
+ log_error("Failed to append message: %s", strerror(-r));
+ goto fail;
+ }
+ } else if (sd_bus_message_is_method_call(m, "org.freedesktop.systemd.test", "Exit"))
+ break;
+ else if (sd_bus_message_is_method_call(m, NULL, NULL)) {
+ const sd_bus_error e = SD_BUS_ERROR_INIT_CONST("org.freedesktop.DBus.Error.UnknownMethod", "Unknown method.");
+
+ r = sd_bus_message_new_method_error(bus, m, &e, &reply);
+ if (r < 0) {
+ log_error("Failed to allocate return: %s", strerror(-r));
+ goto fail;
+ }
+ }
+
+ if (reply) {
+ r = sd_bus_send(bus, reply, NULL);
+ if (r < 0) {
+ log_error("Failed to send reply: %s", strerror(-r));
+ goto fail;
+ }
+ }
+ }
+
+ r = 0;
+
+fail:
+ if (bus)
+ sd_bus_unref(bus);
+
+ return INT_TO_PTR(r);
+}
+
+static int client(void) {
+ _cleanup_bus_message_unref_ sd_bus_message *m = NULL, *reply = NULL;
+ sd_bus *bus = NULL;
+ sd_bus_error error = SD_BUS_ERROR_INIT;
+ const char *hello;
+ int r;
+
+ r = sd_bus_open_user(&bus);
+ if (r < 0) {
+ log_error("Failed to connect to user bus: %s", strerror(-r));
+ goto finish;
+ }
+
+ r = sd_bus_message_new_method_call(
+ bus,
+ "org.freedesktop.systemd.test",
+ "/",
+ "org.freedesktop.systemd.test",
+ "LowerCase",
+ &m);
+ if (r < 0) {
+ log_error("Failed to allocate method call: %s", strerror(-r));
+ goto finish;
+ }
+
+ r = sd_bus_message_append(m, "s", "HELLO");
+ if (r < 0) {
+ log_error("Failed to append string: %s", strerror(-r));
+ goto finish;
+ }
+
+ r = sd_bus_send_with_reply_and_block(bus, m, (uint64_t) -1, &error, &reply);
+ if (r < 0) {
+ log_error("Failed to issue method call: %s", error.message);
+ goto finish;
+ }
+
+ r = sd_bus_message_read(reply, "s", &hello);
+ if (r < 0) {
+ log_error("Failed to get string: %s", strerror(-r));
+ goto finish;
+ }
+
+ assert(streq(hello, "hello"));
+
+ r = 0;
+
+finish:
+ if (bus) {
+ _cleanup_bus_message_unref_ sd_bus_message *q;
+
+ r = sd_bus_message_new_method_call(
+ bus,
+ "org.freedesktop.systemd.test",
+ "/",
+ "org.freedesktop.systemd.test",
+ "Exit",
+ &q);
+ if (r < 0) {
+ log_error("Failed to allocate method call: %s", strerror(-r));
+ goto finish;
+ }
+
+ sd_bus_send(bus, q, NULL);
+ sd_bus_flush(bus);
+ sd_bus_unref(bus);
+ }
+
+ sd_bus_error_free(&error);
+ return r;
+}
+
+int main(int argc, char *argv[]) {
+ pthread_t t;
+ sd_bus *bus;
+ void *p;
+ int q, r;
+
+ r = server_init(&bus);
+ if (r < 0)
+ return EXIT_FAILURE;
+
+ r = pthread_create(&t, NULL, server, bus);
+ if (r != 0) {
+ sd_bus_unref(bus);
+ return EXIT_FAILURE;
+ }
+
+ r = client();
+
+ q = pthread_join(t, &p);
+ if (q != 0)
+ return EXIT_FAILURE;
+ if (r < 0)
+ return EXIT_FAILURE;
+
+ return EXIT_SUCCESS;
+}
commit fbfa72b0a1276366d50dce223c06efe9db9d6585
Author: Lennart Poettering <lennart at poettering.net>
Date: Wed Mar 20 13:10:12 2013 +0100
bus: introduce bus_error_is_dirty() independently of sd_bus_error_is_set()
diff --git a/src/libsystemd-bus/bus-error.c b/src/libsystemd-bus/bus-error.c
index 0832022..015fae7 100644
--- a/src/libsystemd-bus/bus-error.c
+++ b/src/libsystemd-bus/bus-error.c
@@ -31,6 +31,13 @@
#include "sd-bus.h"
#include "bus-error.h"
+bool bus_error_is_dirty(sd_bus_error *e) {
+ if (!e)
+ return 0;
+
+ return e->name || e->message || e->need_free;
+}
+
void sd_bus_error_free(sd_bus_error *e) {
if (!e)
return;
@@ -51,7 +58,7 @@ int sd_bus_error_set(sd_bus_error *e, const char *name, const char *format, ...)
if (!e)
return 0;
- if (sd_bus_error_is_set(e))
+ if (bus_error_is_dirty(e))
return -EINVAL;
if (!name)
return -EINVAL;
@@ -81,7 +88,7 @@ int sd_bus_error_set(sd_bus_error *e, const char *name, const char *format, ...)
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))
+ if (bus_error_is_dirty(dest))
return -EINVAL;
if (!sd_bus_error_is_set(e))
return 0;
@@ -113,18 +120,19 @@ int sd_bus_error_copy(sd_bus_error *dest, const sd_bus_error *e) {
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))
+ if (bus_error_is_dirty(e))
return;
e->name = name;
e->message = message;
+ e->need_free = false;
}
int sd_bus_error_is_set(const sd_bus_error *e) {
if (!e)
return 0;
- return e->name || e->message || e->need_free;
+ return !!e->name;
}
int sd_bus_error_has_name(const sd_bus_error *e, const char *name) {
diff --git a/src/libsystemd-bus/bus-error.h b/src/libsystemd-bus/bus-error.h
index f49e6c7..c6e072e 100644
--- a/src/libsystemd-bus/bus-error.h
+++ b/src/libsystemd-bus/bus-error.h
@@ -25,3 +25,5 @@
int bus_error_to_errno(const sd_bus_error *e);
int bus_error_from_errno(sd_bus_error *e, int error);
+
+bool bus_error_is_dirty(sd_bus_error *e);
diff --git a/src/libsystemd-bus/bus-message.c b/src/libsystemd-bus/bus-message.c
index a1bae4d..5f3dbdb 100644
--- a/src/libsystemd-bus/bus-message.c
+++ b/src/libsystemd-bus/bus-message.c
@@ -447,9 +447,7 @@ int sd_bus_message_new_method_error(
sd_bus_message *t;
int r;
- if (!e)
- return -EINVAL;
- if (!e->name)
+ if (!sd_bus_error_is_set(e))
return -EINVAL;
if (!m)
return -EINVAL;
diff --git a/src/libsystemd-bus/sd-bus.c b/src/libsystemd-bus/sd-bus.c
index 70288ee..c5cbb56 100644
--- a/src/libsystemd-bus/sd-bus.c
+++ b/src/libsystemd-bus/sd-bus.c
@@ -1131,7 +1131,7 @@ int sd_bus_send_with_reply_and_block(
return -EINVAL;
if (!m->header->type != SD_BUS_MESSAGE_TYPE_METHOD_CALL)
return -EINVAL;
- if (sd_bus_error_is_set(error))
+ if (sd_bus_error_is_dirty(error))
return -EINVAL;
r = sd_bus_send(bus, m, &serial);
diff --git a/src/libsystemd-bus/sd-bus.h b/src/libsystemd-bus/sd-bus.h
index d655f05..d892562 100644
--- a/src/libsystemd-bus/sd-bus.h
+++ b/src/libsystemd-bus/sd-bus.h
@@ -131,9 +131,9 @@ int sd_bus_get_owner_pid(sd_bus *bus, const char *name, pid_t *pid);
int sd_bus_add_match(sd_bus *bus, const char *match);
int sd_bus_remove_match(sd_bus *bus, const char *match);
-/* Error objects */
+/* Error structures */
-#define SD_BUS_ERROR_INIT (NULL, NULL, false)
+#define SD_BUS_ERROR_INIT (NULL, NULL, 0)
void sd_bus_error_free(sd_bus_error *e);
int sd_bus_error_set(sd_bus_error *e, const char *name, const char *format, ...);
commit 80a46c7313b8fc4682881cb3a2ca9e4d743fcb2b
Author: Lennart Poettering <lennart at poettering.net>
Date: Wed Mar 20 05:29:20 2013 +0100
bus: demarshal header fields properly
diff --git a/src/libsystemd-bus/bus-message.c b/src/libsystemd-bus/bus-message.c
index 3a74835..a1bae4d 100644
--- a/src/libsystemd-bus/bus-message.c
+++ b/src/libsystemd-bus/bus-message.c
@@ -30,7 +30,8 @@
#include "bus-type.h"
#include "bus-signature.h"
-int message_append_basic(sd_bus_message *m, char type, const void *p, const void **stored);
+static int message_parse_fields(sd_bus_message *m);
+static int message_append_basic(sd_bus_message *m, char type, const void *p, const void **stored);
static void reset_containers(sd_bus_message *m) {
unsigned i;
@@ -125,8 +126,6 @@ static void *message_extend_fields(sd_bus_message *m, size_t align, size_t sz) {
m->destination = (const char*) m->fields + (m->destination - (const char*) o);
if (m->sender)
m->sender = (const char*) m->fields + (m->sender - (const char*) o);
- if (m->signature)
- m->signature = (const char*) m->fields + (m->signature - (const char*) o);
if (m->error.name)
m->error.name = (const char*) m->fields + (m->error.name - (const char*) o);
}
@@ -224,21 +223,76 @@ static int message_append_field_uint32(sd_bus_message *m, uint8_t h, uint32_t x)
return 0;
}
+int bus_message_from_malloc(void *buffer, size_t length, sd_bus_message **ret) {
+ sd_bus_message *m;
+ struct bus_header *h;
+ size_t total, fs, bs;
+ int r;
+
+ assert(buffer || length <= 0);
+ assert(ret);
+
+ if (length < sizeof(struct bus_header))
+ return -EBADMSG;
+
+ h = buffer;
+ if (h->version != 1)
+ return -EBADMSG;
+
+ if (h->serial == 0)
+ return -EBADMSG;
+
+ if (h->type == _SD_BUS_MESSAGE_TYPE_INVALID)
+ return -EBADMSG;
+
+ if (h->endian == SD_BUS_NATIVE_ENDIAN) {
+ fs = h->fields_size;
+ bs = h->body_size;
+ } else if (h->endian == SD_BUS_REVERSE_ENDIAN) {
+ fs = bswap_32(h->fields_size);
+ bs = bswap_32(h->body_size);
+ } else
+ return -EBADMSG;
+
+ total = sizeof(struct bus_header) + ALIGN_TO(fs, 8) + bs;
+ if (length != total)
+ return -EBADMSG;
+
+ m = new0(sd_bus_message, 1);
+ if (!m)
+ return -ENOMEM;
+
+ m->n_ref = 1;
+ m->header = h;
+ m->free_header = true;
+ m->fields = (uint8_t*) buffer + sizeof(struct bus_header);
+ m->body = (uint8_t*) buffer + sizeof(struct bus_header) + ALIGN_TO(fs, 8);
+ m->sealed = true;
+
+ m->n_iovec = 1;
+ m->iovec[0].iov_base = buffer;
+ m->iovec[0].iov_len = length;
+
+ r = message_parse_fields(m);
+ if (r < 0) {
+ message_free(m);
+ return r;
+ }
+
+ *ret = m;
+ return 0;
+}
+
static sd_bus_message *message_new(sd_bus *bus, uint8_t type) {
sd_bus_message *m;
- m = malloc0(ALIGN(sizeof(struct sd_bus_message)) + sizeof(struct bus_header));
+ m = malloc0(ALIGN(sizeof(sd_bus_message)) + sizeof(struct bus_header));
if (!m)
return NULL;
m->n_ref = 1;
m->header = (struct bus_header*) ((uint8_t*) m + ALIGN(sizeof(struct sd_bus_message)));
-
-#if __BYTE_ORDER == __BIG_ENDIAN
- m->header->endian = SD_BUS_BIG_ENDIAN;
-#else
- m->header->endian = SD_BUS_LITTLE_ENDIAN;
-#endif
+ m->header->endian = SD_BUS_NATIVE_ENDIAN;
m->header->type = type;
m->header->version = bus ? bus->message_version : 1;
@@ -1491,7 +1545,7 @@ static int bus_message_enter_array(
return r;
if (BUS_MESSAGE_BSWAP32(m, *(uint32_t*) q) > 67108864)
- return -E2BIG;
+ return -EBADMSG;
r = message_peek_body(m, &rindex, alignment, 0, NULL);
if (r < 0)
@@ -1752,6 +1806,8 @@ int sd_bus_message_peek_type(sd_bus_message *m, char *type, const char **content
if (r < 0)
return r;
+ assert(l >= 1);
+
sig = strndup(c->signature + c->index + 1, l);
if (!sig)
return -ENOMEM;
@@ -2011,15 +2067,364 @@ int sd_bus_message_read(sd_bus_message *m, const char *types, ...) {
return r;
}
-int bus_message_parse(sd_bus_message *m) {
+static int message_peek_fields(
+ sd_bus_message *m,
+ size_t *rindex,
+ size_t align,
+ size_t nbytes,
+ void **ret) {
+
+ assert(m);
+ assert(rindex);
+ assert(align > 0);
+
+ return buffer_peek(m->fields, BUS_MESSAGE_FIELDS_SIZE(m), rindex, align, nbytes, ret);
+}
+
+static int message_peek_field_string(
+ sd_bus_message *m,
+ size_t *ri,
+ const char **ret) {
+
+ size_t l;
+ int r;
+ void *q;
+
assert(m);
+ assert(ri);
+
+ r = message_peek_fields(m, ri, 4, 4, &q);
+ if (r < 0)
+ return r;
+
+ l = BUS_MESSAGE_BSWAP32(m, *(uint32_t*) q);
+ r = message_peek_fields(m, ri, 1, l+1, &q);
+ if (r < 0)
+ return r;
- if (m->header->version != 1)
- return -EIO;
+ if (!validate_string(q, l))
+ return -EBADMSG;
+
+ if (ret)
+ *ret = q;
+
+ return 0;
+}
+
+static int message_peek_field_signature(
+ sd_bus_message *m,
+ size_t *ri,
+ const char **ret) {
+
+ size_t l;
+ int r;
+ void *q;
+
+ assert(m);
+ assert(ri);
+
+ r = message_peek_fields(m, ri, 1, 1, &q);
+ if (r < 0)
+ return r;
+
+ l = *(uint8_t*) q;
+ r = message_peek_fields(m, ri, 1, l+1, &q);
+ if (r < 0)
+ return r;
+
+ if (!validate_signature(q, l))
+ return -EBADMSG;
+
+ if (ret)
+ *ret = q;
+
+ return 0;
+}
+
+static int message_peek_field_uint32(
+ sd_bus_message *m,
+ size_t *ri,
+ uint32_t *ret) {
+
+ int r;
+ void *q;
+
+ assert(m);
+ assert(ri);
+
+ r = message_peek_fields(m, ri, 4, 4, &q);
+ if (r < 0)
+ return r;
+
+ if (ret)
+ *ret = BUS_MESSAGE_BSWAP32(m, *(uint32_t*) q);
+
+ return 0;
+}
+
+static int message_skip_fields(
+ sd_bus_message *m,
+ size_t *ri,
+ uint32_t array_size,
+ const char **signature) {
+
+ size_t original_index;
+ int r;
+
+ assert(m);
+ assert(ri);
+ assert(signature);
+
+ original_index = *ri;
+
+ for (;;) {
+ char t;
+ void *q;
+ size_t l;
+
+ if (array_size != (uint32_t) -1 &&
+ array_size <= *ri - original_index)
+ return 0;
+
+ t = **signature;
+ if (!t)
+ return 0;
+
+ if (t == SD_BUS_TYPE_STRING ||
+ t == SD_BUS_TYPE_OBJECT_PATH) {
+
+ r = message_peek_field_string(m, ri, NULL);
+ if (r < 0)
+ return r;
+
+ (*signature)++;
+
+ } else if (t == SD_BUS_TYPE_SIGNATURE) {
+
+ r = message_peek_field_signature(m, ri, NULL);
+ if (r < 0)
+ return r;
+
+ (*signature)++;
+
+ } else if (bus_type_is_basic(t)) {
+ size_t align, k;
+
+ align = bus_type_get_alignment(align);
+ k = bus_type_get_size(align);
+
+ r = message_peek_fields(m, ri, align, k, NULL);
+ if (r < 0)
+ return r;
- if (m->header->endian != SD_BUS_BIG_ENDIAN &&
- m->header->endian != SD_BUS_LITTLE_ENDIAN)
- return -EIO;
+ (*signature)++;
+
+ } else if (t == SD_BUS_TYPE_ARRAY) {
+
+ r = signature_element_length(*signature+1, &l);
+ if (r < 0)
+ return r;
+
+ assert(l >= 1);
+ {
+ char sig[l-1], *s;
+ size_t nas;
+ int alignment;
+
+ strncpy(sig, *signature + 1, l-1);
+ s = sig;
+
+ alignment = bus_type_get_alignment(sig[0]);
+ if (alignment < 0)
+ return alignment;
+
+ r = message_peek_fields(m, ri, 4, 4, &q);
+ if (r < 0)
+ return r;
+
+ nas = BUS_MESSAGE_BSWAP32(m, *(uint32_t*) q);
+ if (nas > 67108864)
+ return -EBADMSG;
+
+ r = message_peek_fields(m, ri, alignment, 0, NULL);
+ if (r < 0)
+ return r;
+
+ r = message_skip_fields(m, ri, nas, (const char**) &s);
+ if (r < 0)
+ return r;
+ }
+
+ (*signature) += 1 + l;
+
+ } else if (t == SD_BUS_TYPE_VARIANT) {
+ const char *s;
+
+ r = message_peek_field_signature(m, ri, &s);
+ if (r < 0)
+ return r;
+
+ r = message_skip_fields(m, ri, (uint32_t) -1, (const char**) &s);
+ if (r < 0)
+ return r;
+
+ (*signature)++;
+
+ } else if (t == SD_BUS_TYPE_STRUCT ||
+ t == SD_BUS_TYPE_DICT_ENTRY) {
+
+ r = signature_element_length(*signature, &l);
+ if (r < 0)
+ return r;
+
+ assert(l >= 2);
+ {
+ char sig[l-1], *s;
+ strncpy(sig, *signature + 1, l-1);
+ s = sig;
+
+ r = message_skip_fields(m, ri, (uint32_t) -1, (const char**) &s);
+ if (r < 0)
+ return r;
+ }
+
+ *signature += l;
+ } else
+ return -EINVAL;
+ }
+}
+
+static int message_parse_fields(sd_bus_message *m) {
+ size_t ri;
+ int r;
+
+ assert(m);
+
+ for (ri = 0; ri < BUS_MESSAGE_FIELDS_SIZE(m); ) {
+ const char *signature;
+ uint8_t *header;
+
+ r = message_peek_fields(m, &ri, 8, 1, (void**) &header);
+ if (r < 0)
+ return r;
+
+ r = message_peek_field_signature(m, &ri, &signature);
+ if (r < 0)
+ return r;
+
+ switch (*header) {
+ case _SD_BUS_MESSAGE_HEADER_INVALID:
+ return -EBADMSG;
+
+ case SD_BUS_MESSAGE_HEADER_PATH:
+ if (!streq(signature, "o"))
+ return -EBADMSG;
+
+ r = message_peek_field_string(m, &ri, &m->path);
+ break;
+
+ case SD_BUS_MESSAGE_HEADER_INTERFACE:
+ if (!streq(signature, "s"))
+ return -EBADMSG;
+
+ r = message_peek_field_string(m, &ri, &m->interface);
+ break;
+
+ case SD_BUS_MESSAGE_HEADER_MEMBER:
+ if (!streq(signature, "s"))
+ return -EBADMSG;
+
+ r = message_peek_field_string(m, &ri, &m->member);
+ break;
+
+ case SD_BUS_MESSAGE_HEADER_ERROR_NAME:
+ if (!streq(signature, "s"))
+ return -EBADMSG;
+
+ r = message_peek_field_string(m, &ri, &m->error.name);
+ break;
+
+ case SD_BUS_MESSAGE_HEADER_DESTINATION:
+ if (!streq(signature, "s"))
+ return -EBADMSG;
+
+ r = message_peek_field_string(m, &ri, &m->destination);
+ break;
+
+ case SD_BUS_MESSAGE_HEADER_SENDER:
+ if (!streq(signature, "s"))
+ return -EBADMSG;
+
+ r = message_peek_field_string(m, &ri, &m->sender);
+ break;
+
+
+ case SD_BUS_MESSAGE_HEADER_SIGNATURE: {
+ const char *s;
+ char *c;
+
+ if (!streq(signature, "g"))
+ return -EBADMSG;
+
+ r = message_peek_field_signature(m, &ri, &s);
+ if (r < 0)
+ return r;
+
+ c = strdup(s);
+ if (!c)
+ return -ENOMEM;
+
+ free(m->root_container.signature);
+ m->root_container.signature = c;
+
+ r = 0;
+ break;
+ }
+
+ case SD_BUS_MESSAGE_HEADER_REPLY_SERIAL:
+ if (!streq(signature, "u"))
+ return -EBADMSG;
+
+ r = message_peek_field_uint32(m, &ri, &m->reply_serial);
+ break;
+
+ default:
+ r = message_skip_fields(m, &ri, (uint32_t) -1, (const char **) &signature);
+ }
+
+ if (r < 0)
+ return r;
+ }
+
+ if (isempty(m->root_container.signature) != (BUS_MESSAGE_BODY_SIZE(m) == 0))
+ return -EBADMSG;
+
+ switch (m->header->type) {
+
+ case SD_BUS_MESSAGE_TYPE_SIGNAL:
+ if (!m->path || !m->interface || !m->member)
+ return -EBADMSG;
+ break;
+
+ case SD_BUS_MESSAGE_TYPE_METHOD_CALL:
+
+ if (!m->path || !m->member)
+ return -EBADMSG;
+
+ break;
+
+ case SD_BUS_MESSAGE_TYPE_METHOD_RETURN:
+
+ if (m->reply_serial == 0)
+ return -EBADMSG;
+ break;
+
+ case SD_BUS_MESSAGE_TYPE_METHOD_ERROR:
+
+ if (m->reply_serial == 0 || !m->error.name)
+ return -EBADMSG;
+ break;
+ }
return 0;
}
@@ -2068,7 +2473,7 @@ int bus_message_seal(sd_bus_message *m, uint64_t serial) {
/* If there's a non-trivial signature set, then add it in here */
if (!isempty(m->root_container.signature)) {
- r = message_append_field_signature(m, SD_BUS_MESSAGE_HEADER_SIGNATURE, m->root_container.signature, &m->signature);
+ r = message_append_field_signature(m, SD_BUS_MESSAGE_HEADER_SIGNATURE, m->root_container.signature, NULL);
if (r < 0)
return r;
}
@@ -2139,7 +2544,7 @@ int bus_message_dump(sd_bus_message *m) {
strna(m->member),
strna(m->destination),
strna(m->sender),
- strna(m->signature),
+ strna(m->root_container.signature),
m->reply_serial,
strna(m->error.name),
strna(m->error.message),
@@ -2151,7 +2556,7 @@ int bus_message_dump(sd_bus_message *m) {
return r;
}
- printf("BEGIN_MESSAGE \"%s\" {\n", strempty(m->signature));
+ printf("BEGIN_MESSAGE \"%s\" {\n", strempty(m->root_container.signature));
for(;;) {
_cleanup_free_ char *prefix = NULL;
diff --git a/src/libsystemd-bus/bus-message.h b/src/libsystemd-bus/bus-message.h
index 89cb3e1..3035efc 100644
--- a/src/libsystemd-bus/bus-message.h
+++ b/src/libsystemd-bus/bus-message.h
@@ -57,7 +57,6 @@ struct sd_bus_message {
const char *member;
const char *destination;
const char *sender;
- const char *signature;
sd_bus_error error;
@@ -92,11 +91,7 @@ struct sd_bus_message {
char *peeked_signature;
};
-#if __BYTE_ORDER == __BIG_ENDIAN
-#define BUS_MESSAGE_NEED_BSWAP(m) ((m)->header->endian != SD_BUS_BIG_ENDIAN)
-#else
-#define BUS_MESSAGE_NEED_BSWAP(m) ((m)->header->endian != SD_BUS_LITTLE_ENDIAN)
-#endif
+#define BUS_MESSAGE_NEED_BSWAP(m) ((m)->header->endian != SD_BUS_NATIVE_ENDIAN)
static inline uint16_t BUS_MESSAGE_BSWAP16(sd_bus_message *m, uint16_t u) {
return BUS_MESSAGE_NEED_BSWAP(m) ? bswap_16(u) : u;
@@ -132,3 +127,4 @@ int bus_message_parse(sd_bus_message *m);
int bus_message_seal(sd_bus_message *m, uint64_t serial);
int bus_message_dump(sd_bus_message *m);
int bus_message_get_blob(sd_bus_message *m, void **buffer, size_t *sz);
+int bus_message_from_malloc(void *buffer, size_t length, sd_bus_message **ret);
diff --git a/src/libsystemd-bus/sd-bus-protocol.h b/src/libsystemd-bus/sd-bus-protocol.h
index 9d7615f..942422f 100644
--- a/src/libsystemd-bus/sd-bus-protocol.h
+++ b/src/libsystemd-bus/sd-bus-protocol.h
@@ -22,6 +22,8 @@
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
+#include <endian.h>
+
/* Types of message */
#define SD_BUS_DEFAULT_TIMEOUT ((usec_t) (25 * USEC_PER_SEC))
@@ -67,7 +69,14 @@ enum {
enum {
_SD_BUS_INVALID_ENDIAN = 0,
SD_BUS_LITTLE_ENDIAN = 'l',
- SD_BUS_BIG_ENDIAN = 'B'
+ SD_BUS_BIG_ENDIAN = 'B',
+#if __BYTE_ORDER == __BIG_ENDIAN
+ SD_BUS_NATIVE_ENDIAN = SD_BUS_BIG_ENDIAN,
+ SD_BUS_REVERSE_ENDIAN = SD_BUS_LITTLE_ENDIAN
+#else
+ SD_BUS_NATIVE_ENDIAN = SD_BUS_LITTLE_ENDIAN,
+ SD_BUS_REVERSE_ENDIAN = SD_BUS_BIG_ENDIAN
+#endif
};
/* Flags */
diff --git a/src/libsystemd-bus/sd-bus.c b/src/libsystemd-bus/sd-bus.c
index 3f23da6..70288ee 100644
--- a/src/libsystemd-bus/sd-bus.c
+++ b/src/libsystemd-bus/sd-bus.c
@@ -833,10 +833,6 @@ static int message_make(sd_bus *bus, size_t size, sd_bus_message **m) {
assert(m);
assert(bus->rbuffer_size >= size);
- t = new0(sd_bus_message, 1);
- if (!t)
- return -ENOMEM;
-
if (bus->rbuffer_size > size) {
b = memdup((const uint8_t*) bus->rbuffer + size, bus->rbuffer_size - size);
if (!b) {
@@ -845,13 +841,11 @@ static int message_make(sd_bus *bus, size_t size, sd_bus_message **m) {
}
}
- t->n_ref = 1;
-
- t->header = bus->rbuffer;
- t->free_header = true;
-
- t->fields = (uint8_t*) bus->rbuffer + sizeof(struct bus_header);
- t->body = (uint8_t*) bus->rbuffer + sizeof(struct bus_header) + ALIGN_TO(BUS_MESSAGE_BODY_SIZE(t), 8);
+ r = bus_message_from_malloc(bus->rbuffer, size, &t);
+ if (r < 0) {
+ free(b);
+ return r;
+ }
bus->rbuffer = b;
bus->rbuffer_size -= size;
diff --git a/src/libsystemd-bus/test-bus-marshal.c b/src/libsystemd-bus/test-bus-marshal.c
index 9138f11..15c472c 100644
--- a/src/libsystemd-bus/test-bus-marshal.c
+++ b/src/libsystemd-bus/test-bus-marshal.c
@@ -116,7 +116,12 @@ int main(int argc, char *argv[]) {
dbus_message_unref(w);
}
- free(buffer);
+ m = sd_bus_message_unref(m);
+
+ r = bus_message_from_malloc(buffer, sz, &m);
+ assert_se(r >= 0);
+
+ bus_message_dump(m);
assert_se(sd_bus_message_rewind(m, true) >= 0);
commit 9a17484d986f6cb8828afdf911db425b574c63fd
Author: Lennart Poettering <lennart at poettering.net>
Date: Wed Mar 20 03:15:03 2013 +0100
bus: implement demarshaller
diff --git a/src/libsystemd-bus/bus-message.c b/src/libsystemd-bus/bus-message.c
index ccea120..3a74835 100644
--- a/src/libsystemd-bus/bus-message.c
+++ b/src/libsystemd-bus/bus-message.c
@@ -22,15 +22,31 @@
#include <errno.h>
#include "util.h"
+#include "utf8.h"
-#include "bus-message.h"
#include "sd-bus.h"
+#include "bus-message.h"
#include "bus-internal.h"
#include "bus-type.h"
#include "bus-signature.h"
int message_append_basic(sd_bus_message *m, char type, const void *p, const void **stored);
+static void reset_containers(sd_bus_message *m) {
+ unsigned i;
+
+ assert(m);
+
+ for (i = 0; i < m->n_containers; i++)
+ free(m->containers[i].signature);
+
+ free(m->containers);
+ m->containers = NULL;
+
+ m->n_containers = 0;
+ m->root_container.index = 0;
+}
+
static void message_free(sd_bus_message *m) {
unsigned i;
@@ -48,10 +64,10 @@ static void message_free(sd_bus_message *m) {
for (i = 0; i < m->n_fds; i++)
close_nointr_nofail(m->fds[i]);
- for (i = 0; i < m->n_containers; i++)
- free(m->sub_containers[i].signature);
+ reset_containers(m);
+ free(m->root_container.signature);
- free(m->sub_containers);
+ free(m->peeked_signature);
free(m);
}
@@ -621,8 +637,8 @@ static struct bus_container *message_get_container(sd_bus_message *m) {
if (m->n_containers == 0)
return &m->root_container;
- assert(m->sub_containers);
- return m->sub_containers + m->n_containers - 1;
+ assert(m->containers);
+ return m->containers + m->n_containers - 1;
}
static void *message_extend_body(sd_bus_message *m, size_t align, size_t sz) {
@@ -631,6 +647,7 @@ static void *message_extend_body(sd_bus_message *m, size_t align, size_t sz) {
struct bus_container *c;
assert(m);
+ assert(align > 0);
o = m->body;
added = m->header->body_size;
@@ -641,7 +658,7 @@ static void *message_extend_body(sd_bus_message *m, size_t align, size_t sz) {
added = m->header->body_size - added;
- for (c = m->sub_containers; c < m->sub_containers + m->n_containers; c++)
+ for (c = m->containers; c < m->containers + m->n_containers; c++)
if (c->array_size) {
c->array_size = (uint32_t*) ((uint8_t*) m->body + ((uint8_t*) c->array_size - (uint8_t*) o));
*c->array_size += added;
@@ -659,7 +676,7 @@ static void *message_extend_body(sd_bus_message *m, size_t align, size_t sz) {
int message_append_basic(sd_bus_message *m, char type, const void *p, const void **stored) {
struct bus_container *c;
- size_t sz, align, nindex;
+ size_t sz, align;
uint32_t k;
void *a;
char *e = NULL;
@@ -677,19 +694,17 @@ int message_append_basic(sd_bus_message *m, char type, const void *p, const void
/* Container signature is already set */
if (c->signature[c->index] != type)
- return -EINVAL;
+ return -ENXIO;
} else {
/* Maybe we can append to the signature? But only if this is the top-level container*/
if (c->enclosing != 0)
- return -EINVAL;
+ return -ENXIO;
e = strextend(&c->signature, CHAR_TO_STR(type), NULL);
if (!e)
return -ENOMEM;
}
- nindex = c->index + 1;
-
switch (type) {
case SD_BUS_TYPE_STRING:
@@ -752,7 +767,7 @@ int message_append_basic(sd_bus_message *m, char type, const void *p, const void
}
if (c->enclosing != SD_BUS_TYPE_ARRAY)
- c->index = nindex;
+ c->index++;
return 0;
}
@@ -767,8 +782,8 @@ static int bus_message_open_array(
const char *contents,
uint32_t **array_size) {
+ unsigned nindex;
char *e = NULL;
- size_t nindex;
void *a, *b;
int alignment;
size_t saved;
@@ -790,15 +805,15 @@ static int bus_message_open_array(
/* Verify the existing signature */
if (c->signature[c->index] != SD_BUS_TYPE_ARRAY)
- return -EINVAL;
+ return -ENXIO;
if (!startswith(c->signature + c->index + 1, contents))
- return -EINVAL;
+ return -ENXIO;
nindex = c->index + 1 + strlen(contents);
} else {
if (c->enclosing != 0)
- return -EINVAL;
+ return -ENXIO;
/* Extend the existing signature */
@@ -846,7 +861,7 @@ static int bus_message_open_variant(
const char *contents) {
char *e = NULL;
- size_t l, nindex;
+ size_t l;
void *a;
assert(m);
@@ -862,19 +877,17 @@ static int bus_message_open_variant(
if (c->signature && c->signature[c->index]) {
if (c->signature[c->index] != SD_BUS_TYPE_VARIANT)
- return -EINVAL;
+ return -ENXIO;
} else {
if (c->enclosing != 0)
- return -EINVAL;
+ return -ENXIO;
e = strextend(&c->signature, CHAR_TO_STR(SD_BUS_TYPE_VARIANT), NULL);
if (!e)
return -ENOMEM;
}
- nindex = c->index + 1;
-
l = strlen(contents);
a = message_extend_body(m, 1, 1 + l + 1);
if (!a) {
@@ -889,7 +902,7 @@ static int bus_message_open_variant(
memcpy((uint8_t*) a + 1, contents, l + 1);
if (c->enclosing != SD_BUS_TYPE_ARRAY)
- c->index = nindex;
+ c->index++;
return 0;
}
@@ -917,12 +930,12 @@ static int bus_message_open_struct(
if (c->signature[c->index] != SD_BUS_TYPE_STRUCT_BEGIN ||
!startswith(c->signature + c->index + 1, contents) ||
c->signature[c->index + 1 + l] != SD_BUS_TYPE_STRUCT_END)
- return -EINVAL;
+ return -ENXIO;
nindex = c->index + 1 + l + 1;
} else {
if (c->enclosing != 0)
- return -EINVAL;
+ return -ENXIO;
e = strextend(&c->signature, CHAR_TO_STR(SD_BUS_TYPE_STRUCT_BEGIN), contents, CHAR_TO_STR(SD_BUS_TYPE_STRUCT_END), NULL);
if (!e)
@@ -960,7 +973,7 @@ static int bus_message_open_dict_entry(
return -EINVAL;
if (c->enclosing != SD_BUS_TYPE_ARRAY)
- return -EINVAL;
+ return -ENXIO;
if (c->signature && c->signature[c->index]) {
size_t l;
@@ -970,11 +983,11 @@ static int bus_message_open_dict_entry(
if (c->signature[c->index] != SD_BUS_TYPE_DICT_ENTRY_BEGIN ||
!startswith(c->signature + c->index + 1, contents) ||
c->signature[c->index + 1 + l] != SD_BUS_TYPE_DICT_ENTRY_END)
- return -EINVAL;
+ return -ENXIO;
nindex = c->index + 1 + l + 1;
} else
- return -EINVAL;
+ return -ENXIO;
/* Align contents to 8 byte boundary */
if (!message_extend_body(m, 8, 0))
@@ -991,9 +1004,9 @@ int sd_bus_message_open_container(
char type,
const char *contents) {
- struct bus_container *c, *sub;
- char *signature;
+ struct bus_container *c, *w;
uint32_t *array_size = NULL;
+ char *signature;
int r;
if (!m)
@@ -1004,11 +1017,10 @@ int sd_bus_message_open_container(
return -EINVAL;
/* Make sure we have space for one more container */
- sub = realloc(m->sub_containers, sizeof(struct bus_container) * (m->n_containers + 1));
- if (!sub)
+ w = realloc(m->containers, sizeof(struct bus_container) * (m->n_containers + 1));
+ if (!w)
return -ENOMEM;
-
- m->sub_containers = sub;
+ m->containers = w;
c = message_get_container(m);
@@ -1033,12 +1045,12 @@ int sd_bus_message_open_container(
}
/* OK, let's fill it in */
- sub += m->n_containers++;
-
- sub->enclosing = type;
- sub->signature = signature;
- sub->index = 0;
- sub->array_size = array_size;
+ w += m->n_containers++;
+ w->enclosing = type;
+ w->signature = signature;
+ w->index = 0;
+ w->array_size = array_size;
+ w->begin = 0;
return 0;
}
@@ -1054,12 +1066,8 @@ int sd_bus_message_close_container(sd_bus_message *m) {
return -EINVAL;
c = message_get_container(m);
-
- if (!c->signature)
- return -EINVAL;
-
if (c->enclosing != SD_BUS_TYPE_ARRAY)
- if (c->signature[c->index] != 0)
+ if (c->signature && c->signature[c->index] != 0)
return -EINVAL;
free(c->signature);
@@ -1092,9 +1100,13 @@ static int message_append_ap(
case SD_BUS_TYPE_BOOLEAN:
case SD_BUS_TYPE_INT32:
- case SD_BUS_TYPE_UINT32: {
+ case SD_BUS_TYPE_UINT32:
+ case SD_BUS_TYPE_UNIX_FD: {
uint32_t x;
+ /* We assume a boolean is the same as int32_t */
+ assert_cc(sizeof(int32_t) == sizeof(int));
+
x = va_arg(ap, uint32_t);
r = sd_bus_message_append_basic(m, *t, &x);
break;
@@ -1129,16 +1141,7 @@ static int message_append_ap(
break;
}
- case SD_BUS_TYPE_UNIX_FD: {
- int x;
-
- x = va_arg(ap, int);
- r = sd_bus_message_append_basic(m, *t, &x);
- break;
- }
-
case SD_BUS_TYPE_ARRAY: {
- unsigned i, n;
size_t k;
r = signature_element_length(t + 1, &k);
@@ -1146,6 +1149,7 @@ static int message_append_ap(
return r;
{
+ unsigned i, n;
char s[k + 1];
memcpy(s, t + 1, k);
@@ -1157,7 +1161,6 @@ static int message_append_ap(
return r;
n = va_arg(ap, unsigned);
-
for (i = 0; i < n; i++) {
r = message_append_ap(m, s, ap);
if (r < 0)
@@ -1248,147 +1251,1050 @@ int sd_bus_message_append(sd_bus_message *m, const char *types, ...) {
return r;
}
-int sd_bus_message_read_type(sd_bus_message *m, char *type, char *element, size_t *length) {
- return -ENOTSUP;
-}
+static int buffer_peek(const void *p, uint32_t sz, size_t *rindex, size_t align, size_t nbytes, void **r) {
+ size_t k, start, n;
-int sd_bus_message_read_basic(sd_bus_message *m, char type, char element, const void **p, size_t *length) {
- return -ENOTSUP;
-}
+ assert(rindex);
+ assert(align > 0);
-int sd_bus_message_read(sd_bus_message *m, const char *types, ...) {
- return -ENOTSUP;
+ start = ALIGN_TO((size_t) *rindex, align);
+ n = start + nbytes;
+
+ if (n > sz)
+ return -EBADMSG;
+
+ /* Verify that padding is 0 */
+ for (k = *rindex; k < start; k++)
+ if (((const uint8_t*) p)[k] != 0)
+ return -EBADMSG;
+
+ if (r)
+ *r = (uint8_t*) p + start;
+
+ *rindex = n;
+
+ return 1;
}
-int message_parse(sd_bus_message *m) {
- assert(m);
+static bool message_end_of_array(sd_bus_message *m, size_t index) {
+ struct bus_container *c;
- if (m->header->version != 1)
- return -EIO;
+ assert(m);
- if (m->header->endian != SD_BUS_BIG_ENDIAN &&
- m->header->endian != SD_BUS_LITTLE_ENDIAN)
- return -EIO;
+ c = message_get_container(m);
+ if (!c->array_size)
+ return false;
- return 0;
+ return index >= c->begin + BUS_MESSAGE_BSWAP32(m, *c->array_size);
}
-static void setup_iovec(sd_bus_message *m) {
+static int message_peek_body(sd_bus_message *m, size_t *rindex, size_t align, size_t nbytes, void **ret) {
assert(m);
- assert(m->sealed);
+ assert(rindex);
+ assert(align > 0);
- m->n_iovec = 0;
+ if (message_end_of_array(m, *rindex))
+ return 0;
- m->iovec[m->n_iovec].iov_base = m->header;
- m->iovec[m->n_iovec].iov_len = sizeof(*m->header);
- m->n_iovec++;
+ return buffer_peek(m->body, BUS_MESSAGE_BODY_SIZE(m), rindex, align, nbytes, ret);
+}
- if (m->fields) {
- m->iovec[m->n_iovec].iov_base = m->fields;
- m->iovec[m->n_iovec].iov_len = m->header->fields_size;
- m->n_iovec++;
+static bool validate_string(const char *s, size_t l) {
+ assert(s);
- if (m->header->fields_size % 8 != 0) {
- static const uint8_t padding[7] = { 0, 0, 0, 0, 0, 0, 0 };
+ /* Check for NUL chars in the string */
+ if (memchr(s, 0, l))
+ return false;
- m->iovec[m->n_iovec].iov_base = (void*) padding;
- m->iovec[m->n_iovec].iov_len = 8 - m->header->fields_size % 8;
- m->n_iovec++;
- }
- }
+ /* Check for NUL termination */
+ if (s[l] != 0)
+ return false;
- if (m->body) {
- m->iovec[m->n_iovec].iov_base = m->body;
- m->iovec[m->n_iovec].iov_len = m->header->body_size;
- m->n_iovec++;
- }
+ /* Check if valid UTF8 */
+ if (!utf8_is_valid(s))
+ return false;
+
+ return true;
}
-int message_seal(sd_bus_message *m, uint64_t serial) {
- int r;
+static bool validate_signature(const char *s, size_t l) {
+ /* Check for NUL chars in the signature */
+ if (memchr(s, 0, l))
+ return false;
- assert(m);
+ /* Check for NUL termination */
+ if (s[l] != 0)
+ return false;
- if (m->sealed)
+ /* Check if valid signature */
+ if (!signature_is_valid(s, true))
+ return false;
+
+ return true;
+}
+
+int sd_bus_message_read_basic(sd_bus_message *m, char type, void *p) {
+ struct bus_container *c;
+ int r;
+ void *q;
+
+ if (!m)
+ return -EINVAL;
+ if (!m->sealed)
return -EPERM;
+ if (!bus_type_is_basic(type))
+ return -EINVAL;
- if (m->n_containers > 0)
- return -EBADMSG;
+ c = message_get_container(m);
- /* If there's a non-trivial signature set, then add it in here */
- if (!isempty(m->root_container.signature)) {
- r = message_append_field_signature(m, SD_BUS_MESSAGE_HEADER_SIGNATURE, m->root_container.signature, &m->signature);
+ if (!c->signature || c->signature[c->index] == 0)
+ return 0;
+
+ if (c->signature[c->index] != type)
+ return -ENXIO;
+
+ switch (type) {
+
+ case SD_BUS_TYPE_STRING:
+ case SD_BUS_TYPE_OBJECT_PATH: {
+ uint32_t l;
+ size_t rindex;
+
+ rindex = m->rindex;
+ r = message_peek_body(m, &rindex, 4, 4, &q);
+ if (r <= 0)
+ return r;
+
+ l = BUS_MESSAGE_BSWAP32(m, *(uint32_t*) q);
+ r = message_peek_body(m, &rindex, 1, l+1, &q);
if (r < 0)
return r;
+ if (r == 0)
+ return -EBADMSG;
+
+ if (!validate_string(q, l))
+ return -EBADMSG;
+
+ m->rindex = rindex;
+ *(const char**) p = q;
+ break;
}
- if (m->n_fds > 0) {
- r = message_append_field_uint32(m, SD_BUS_MESSAGE_HEADER_UNIX_FDS, m->n_fds);
+ case SD_BUS_TYPE_SIGNATURE: {
+ uint8_t l;
+ size_t rindex;
+
+ rindex = m->rindex;
+ r = message_peek_body(m, &rindex, 1, 1, &q);
+ if (r <= 0)
+ return r;
+
+ l = *(uint8_t*) q;
+ r = message_peek_body(m, &rindex, 1, l+1, &q);
if (r < 0)
return r;
+ if (r == 0)
+ return -EBADMSG;
+
+ if (!validate_signature(q, l))
+ return -EBADMSG;
+
+ m->rindex = rindex;
+ *(const char**) p = q;
+ break;
}
- m->header->serial = serial;
- m->sealed = true;
+ default: {
+ size_t sz, align;
- setup_iovec(m);
+ align = bus_type_get_alignment(type);
+ sz = bus_type_get_size(type);
- return 0;
+ r = message_peek_body(m, &m->rindex, align, sz, &q);
+ if (r <= 0)
+ return r;
+
+ switch (type) {
+
+ case SD_BUS_TYPE_BYTE:
+ *(uint8_t*) p = *(uint8_t*) q;
+ break;
+
+ case SD_BUS_TYPE_BOOLEAN:
+ *(int*) p = !!*(uint32_t*) q;
+ break;
+
+ case SD_BUS_TYPE_INT16:
+ case SD_BUS_TYPE_UINT16:
+ *(uint16_t*) p = BUS_MESSAGE_BSWAP16(m, *(uint16_t*) q);
+ break;
+
+ case SD_BUS_TYPE_INT32:
+ case SD_BUS_TYPE_UINT32:
+ *(uint32_t*) p = BUS_MESSAGE_BSWAP32(m, *(uint32_t*) q);
+ break;
+
+ case SD_BUS_TYPE_INT64:
+ case SD_BUS_TYPE_UINT64:
+ case SD_BUS_TYPE_DOUBLE:
+ *(uint64_t*) p = BUS_MESSAGE_BSWAP64(m, *(uint64_t*) q);
+ break;
+
+ default:
+ assert_not_reached("Unknown basic type...");
+ }
+
+ break;
+ }
+ }
+
+ if (c->enclosing != SD_BUS_TYPE_ARRAY)
+ c->index++;
+
+ return 1;
}
-int sd_bus_message_set_destination(sd_bus_message *m, const char *destination) {
- if (!m)
+static int bus_message_enter_array(
+ sd_bus_message *m,
+ struct bus_container *c,
+ const char *contents,
+ uint32_t **array_size) {
+
+ size_t rindex;
+ void *q;
+ int r, alignment;
+
+ assert(m);
+ assert(c);
+ assert(contents);
+ assert(array_size);
+
+ if (!signature_is_single(contents))
return -EINVAL;
- if (!destination)
+
+ alignment = bus_type_get_alignment(contents[0]);
+ if (alignment < 0)
+ return alignment;
+
+ if (!c->signature || c->signature[c->index] == 0)
+ return 0;
+
+ if (c->signature[c->index] != SD_BUS_TYPE_ARRAY)
+ return -ENXIO;
+
+ if (!startswith(c->signature + c->index + 1, contents))
+ return -ENXIO;
+
+ rindex = m->rindex;
+ r = message_peek_body(m, &rindex, 4, 4, &q);
+ if (r <= 0)
+ return r;
+
+ if (BUS_MESSAGE_BSWAP32(m, *(uint32_t*) q) > 67108864)
+ return -E2BIG;
+
+ r = message_peek_body(m, &rindex, alignment, 0, NULL);
+ if (r < 0)
+ return r;
+ if (r == 0)
+ return -EBADMSG;
+
+ if (c->enclosing != SD_BUS_TYPE_ARRAY)
+ c->index += 1 + strlen(contents);
+
+ m->rindex = rindex;
+
+ *array_size = (uint32_t*) q;
+
+ return 1;
+}
+
+static int bus_message_enter_variant(
+ sd_bus_message *m,
+ struct bus_container *c,
+ const char *contents) {
+
+ size_t rindex;
+ uint8_t l;
+ void *q;
+ int r;
+
+ assert(m);
+ assert(c);
+ assert(contents);
+
+ if (!signature_is_single(contents))
return -EINVAL;
- if (m->sealed)
- return -EPERM;
- if (m->destination)
- return -EEXIST;
- return message_append_field_string(m, SD_BUS_MESSAGE_HEADER_DESTINATION, SD_BUS_TYPE_STRING, destination, &m->destination);
+ if (*contents == SD_BUS_TYPE_DICT_ENTRY_BEGIN)
+ return -EINVAL;
+
+ if (!c->signature || c->signature[c->index] == 0)
+ return 0;
+
+ if (c->signature[c->index] != SD_BUS_TYPE_VARIANT)
+ return -ENXIO;
+
+ rindex = m->rindex;
+ r = message_peek_body(m, &rindex, 1, 1, &q);
+ if (r <= 0)
+ return r;
+
+ l = *(uint8_t*) q;
+ r = message_peek_body(m, &rindex, 1, l+1, &q);
+ if (r < 0)
+ return r;
+ if (r == 0)
+ return -EBADMSG;
+
+ if (!validate_signature(q, l))
+ return -EBADMSG;
+
+ if (!streq(q, contents))
+ return -ENXIO;
+
+ if (c->enclosing != SD_BUS_TYPE_ARRAY)
+ c->index++;
+
+ m->rindex = rindex;
+
+ return 1;
}
-void message_dump(sd_bus_message *m) {
-
- log_info("Message %p\n"
- "\tn_ref=%u\n"
- "\tendian=%c\n"
- "\ttype=%i\n"
- "\tflags=%u\n"
- "\tversion=%u\n"
- "\tserial=%u\n"
- "\tfields_size=%u\n"
- "\tbody_size=%u\n"
- "\tpath=%s\n"
- "\tinterface=%s\n"
- "\tmember=%s\n"
- "\tdestination=%s\n"
- "\tsender=%s\n"
- "\tsignature=%s\n"
- "\treply_serial=%u\n"
- "\terror.name=%s\n"
- "\terror.message=%s\n"
- "\tsealed=%s\n",
- m,
- m->n_ref,
- m->header->endian,
- m->header->type,
- m->header->flags,
- m->header->version,
- BUS_MESSAGE_SERIAL(m),
- BUS_MESSAGE_FIELDS_SIZE(m),
- BUS_MESSAGE_BODY_SIZE(m),
- strna(m->path),
- strna(m->interface),
- strna(m->member),
- strna(m->destination),
- strna(m->sender),
- strna(m->signature),
- m->reply_serial,
- strna(m->error.name),
- strna(m->error.message),
- yes_no(m->sealed));
+static int bus_message_enter_struct(
+ sd_bus_message *m,
+ struct bus_container *c,
+ const char *contents) {
+
+ size_t l;
+ int r;
+
+ assert(m);
+ assert(c);
+ assert(contents);
+
+ if (!signature_is_valid(contents, false))
+ return -EINVAL;
+
+ if (!c->signature || c->signature[c->index] == 0)
+ return 0;
+
+ l = strlen(contents);
+
+ if (c->signature[c->index] != SD_BUS_TYPE_STRUCT_BEGIN ||
+ !startswith(c->signature + c->index + 1, contents) ||
+ c->signature[c->index + 1 + l] != SD_BUS_TYPE_STRUCT_END)
+ return -ENXIO;
+
+ r = message_peek_body(m, &m->rindex, 8, 0, NULL);
+ if (r <= 0)
+ return r;
+
+ if (c->enclosing != SD_BUS_TYPE_ARRAY)
+ c->index += 1 + l + 1;
+
+ return 1;
+}
+
+static int bus_message_enter_dict_entry(
+ sd_bus_message *m,
+ struct bus_container *c,
+ const char *contents) {
+
+ size_t l;
+ int r;
+
+ assert(m);
+ assert(c);
+ assert(contents);
+
+ if (!signature_is_pair(contents))
+ return -EINVAL;
+
+ if (c->enclosing != SD_BUS_TYPE_ARRAY)
+ return -ENXIO;
+
+ if (!c->signature || c->signature[c->index] == 0)
+ return 0;
+
+ l = strlen(contents);
+
+ if (c->signature[c->index] != SD_BUS_TYPE_DICT_ENTRY_BEGIN ||
+ !startswith(c->signature + c->index + 1, contents) ||
+ c->signature[c->index + 1 + l] != SD_BUS_TYPE_DICT_ENTRY_END)
+ return -ENXIO;
+
+ r = message_peek_body(m, &m->rindex, 8, 0, NULL);
+ if (r <= 0)
+ return r;
+
+ if (c->enclosing != SD_BUS_TYPE_ARRAY)
+ c->index += 1 + l + 1;
+
+ return 1;
+}
+
+int sd_bus_message_enter_container(sd_bus_message *m, char type, const char *contents) {
+ struct bus_container *c, *w;
+ uint32_t *array_size = NULL;
+ char *signature;
+ int r;
+
+ if (!m)
+ return -EINVAL;
+ if (!m->sealed)
+ return -EPERM;
+ if (!contents)
+ return -EINVAL;
+
+ w = realloc(m->containers, sizeof(struct bus_container) * (m->n_containers + 1));
+ if (!w)
+ return -ENOMEM;
+ m->containers = w;
+
+ c = message_get_container(m);
+
+ if (!c->signature || c->signature[c->index] == 0)
+ return 0;
+
+ signature = strdup(contents);
+ if (!signature)
+ return -ENOMEM;
+
+ if (type == SD_BUS_TYPE_ARRAY)
+ r = bus_message_enter_array(m, c, contents, &array_size);
+ else if (type == SD_BUS_TYPE_VARIANT)
+ r = bus_message_enter_variant(m, c, contents);
+ else if (type == SD_BUS_TYPE_STRUCT)
+ r = bus_message_enter_struct(m, c, contents);
+ else if (type == SD_BUS_TYPE_DICT_ENTRY)
+ r = bus_message_enter_dict_entry(m, c, contents);
+ else
+ r = -EINVAL;
+
+ if (r <= 0) {
+ free(signature);
+ return r;
+ }
+
+ /* OK, let's fill it in */
+ w += m->n_containers++;
+ w->enclosing = type;
+ w->signature = signature;
+ w->index = 0;
+ w->array_size = array_size;
+ w->begin = m->rindex;
+
+ return 1;
+}
+
+int sd_bus_message_exit_container(sd_bus_message *m) {
+ struct bus_container *c;
+
+ if (!m)
+ return -EINVAL;
+ if (!m->sealed)
+ return -EPERM;
+ if (m->n_containers <= 0)
+ return -EINVAL;
+
+ c = message_get_container(m);
+ if (c->enclosing == SD_BUS_TYPE_ARRAY) {
+ uint32_t l;
+
+ l = BUS_MESSAGE_BSWAP32(m, *c->array_size);
+ if (c->begin + l != m->rindex)
+ return -EBUSY;
+
+ } else {
+ if (c->signature && c->signature[c->index] != 0)
+ return -EINVAL;
+ }
+
+ free(c->signature);
+ m->n_containers--;
+
+ return 1;
+}
+
+int sd_bus_message_peek_type(sd_bus_message *m, char *type, const char **contents) {
+ struct bus_container *c;
+ int r;
+
+ if (!m)
+ return -EINVAL;
+ if (!m->sealed)
+ return -EPERM;
+
+ c = message_get_container(m);
+
+ if (!c->signature || c->signature[c->index] == 0)
+ goto eof;
+
+ if (message_end_of_array(m, m->rindex))
+ goto eof;
+
+ if (bus_type_is_basic(c->signature[c->index])) {
+ if (contents)
+ *contents = NULL;
+ if (type)
+ *type = c->signature[c->index];
+ return 1;
+ }
+
+ if (c->signature[c->index] == SD_BUS_TYPE_ARRAY) {
+
+ if (contents) {
+ size_t l;
+ char *sig;
+
+ r = signature_element_length(c->signature+c->index+1, &l);
+ if (r < 0)
+ return r;
+
+ sig = strndup(c->signature + c->index + 1, l);
+ if (!sig)
+ return -ENOMEM;
+
+ free(m->peeked_signature);
+ m->peeked_signature = sig;
+
+ *contents = sig;
+ }
+
+ if (type)
+ *type = SD_BUS_TYPE_ARRAY;
+
+ return 1;
+ }
+
+ if (c->signature[c->index] == SD_BUS_TYPE_STRUCT_BEGIN ||
+ c->signature[c->index] == SD_BUS_TYPE_DICT_ENTRY_BEGIN) {
+
+ if (contents) {
+ size_t l;
+ char *sig;
+
+ r = signature_element_length(c->signature+c->index, &l);
+ if (r < 0)
+ return r;
+
+ assert(l >= 2);
+ sig = strndup(c->signature + c->index + 1, l - 2);
+ if (!sig)
+ return -ENOMEM;
+
+ free(m->peeked_signature);
+ m->peeked_signature = sig;
+
+ *contents = sig;
+ }
+
+ if (type)
+ *type = c->signature[c->index] == SD_BUS_TYPE_STRUCT_BEGIN ? SD_BUS_TYPE_STRUCT : SD_BUS_TYPE_DICT_ENTRY;
+
+ return 1;
+ }
+
+ if (c->signature[c->index] == SD_BUS_TYPE_VARIANT) {
+ if (contents) {
+ size_t rindex, l;
+ void *q;
+
+ rindex = m->rindex;
+ r = message_peek_body(m, &rindex, 1, 1, &q);
+ if (r < 0)
+ return r;
+ if (r == 0)
+ goto eof;
+
+ l = *(uint8_t*) q;
+ r = message_peek_body(m, &rindex, 1, l+1, &q);
+ if (r < 0)
+ return r;
+ if (r == 0)
+ return -EBADMSG;
+
+ if (!validate_signature(q, l))
+ return -EBADMSG;
+
+ *contents = q;
+ }
+
+ if (type)
+ *type = SD_BUS_TYPE_VARIANT;
+
+ return 1;
+ }
+
+ return -EINVAL;
+
+eof:
+ if (type)
+ *type = c->enclosing;
+ if (contents)
+ *contents = NULL;
+ return 0;
+}
+
+int sd_bus_message_rewind(sd_bus_message *m, bool complete) {
+ struct bus_container *c;
+
+ if (!m)
+ return -EINVAL;
+ if (!m->sealed)
+ return -EPERM;
+
+ if (complete) {
+ reset_containers(m);
+ m->rindex = 0;
+ m->root_container.index = 0;
+
+ c = message_get_container(m);
+ } else {
+ c = message_get_container(m);
+
+ c->index = 0;
+ m->rindex = c->begin;
+ }
+
+ return !isempty(c->signature);
+}
+
+static int message_read_ap(sd_bus_message *m, const char *types, va_list ap) {
+ const char *t;
+ int r;
+
+ assert(m);
+ assert(types);
+
+ for (t = types; *t; t++) {
+ switch (*t) {
+
+ case SD_BUS_TYPE_BYTE:
+ case SD_BUS_TYPE_BOOLEAN:
+ case SD_BUS_TYPE_INT16:
+ case SD_BUS_TYPE_UINT16:
+ case SD_BUS_TYPE_INT32:
+ case SD_BUS_TYPE_UINT32:
+ case SD_BUS_TYPE_INT64:
+ case SD_BUS_TYPE_UINT64:
+ case SD_BUS_TYPE_DOUBLE:
+ case SD_BUS_TYPE_STRING:
+ case SD_BUS_TYPE_OBJECT_PATH:
+ case SD_BUS_TYPE_SIGNATURE: {
+ void *p;
+
+ p = va_arg(ap, void*);
+ r = sd_bus_message_read_basic(m, *t, p);
+ break;
+ }
+
+ case SD_BUS_TYPE_ARRAY: {
+ size_t k;
+
+ r = signature_element_length(t + 1, &k);
+ if (r < 0)
+ return r;
+
+ {
+ unsigned i, n;
+ char s[k + 1];
+
+ memcpy(s, t + 1, k);
+ s[k] = 0;
+ t += k;
+
+ r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, s);
+ if (r < 0)
+ return r;
+ if (r == 0)
+ return -ENXIO;
+
+ n = va_arg(ap, unsigned);
+ for (i = 0; i < n; i++) {
+ r = message_read_ap(m, s, ap);
+ if (r < 0)
+ return r;
+ }
+
+ r = sd_bus_message_exit_container(m);
+ }
+
+ break;
+ }
+
+ case SD_BUS_TYPE_VARIANT: {
+ const char *s;
+
+ s = va_arg(ap, const char *);
+ if (!s)
+ return -EINVAL;
+
+ r = sd_bus_message_enter_container(m, SD_BUS_TYPE_VARIANT, s);
+ if (r < 0)
+ return r;
+ if (r == 0)
+ return -ENXIO;
+
+ r = message_read_ap(m, s, ap);
+ if (r < 0)
+ return r;
+ if (r == 0)
+ return -ENXIO;
+
+ r = sd_bus_message_exit_container(m);
+ break;
+ }
+
+ case SD_BUS_TYPE_STRUCT_BEGIN:
+ case SD_BUS_TYPE_DICT_ENTRY_BEGIN: {
+ size_t k;
+
+ r = signature_element_length(t, &k);
+ if (r < 0)
+ return r;
+
+ {
+ char s[k - 1];
+ memcpy(s, t + 1, k - 2);
+ s[k - 2] = 0;
+
+ r = sd_bus_message_enter_container(m, *t == SD_BUS_TYPE_STRUCT_BEGIN ? SD_BUS_TYPE_STRUCT : SD_BUS_TYPE_DICT_ENTRY, s);
+ if (r < 0)
+ return r;
+ if (r == 0)
+ return -ENXIO;
+
+ t += k - 1;
+
+ r = message_read_ap(m, s, ap);
+ if (r < 0)
+ return r;
+ if (r == 0)
+ return -ENXIO;
+
+ r = sd_bus_message_exit_container(m);
+ }
+
+ break;
+ }
+
+ default:
+ r = -EINVAL;
+ }
+
+ if (r < 0)
+ return r;
+ if (r == 0)
+ return -ENXIO;
+ }
+
+ return 1;
+}
+
+int sd_bus_message_read(sd_bus_message *m, const char *types, ...) {
+ va_list ap;
+ int r;
+
+ if (!m)
+ return -EINVAL;
+ if (!m->sealed)
+ return -EPERM;
+ if (!types)
+ return -EINVAL;
+
+ va_start(ap, types);
+ r = message_read_ap(m, types, ap);
+ va_end(ap);
+
+ return r;
+}
+
+int bus_message_parse(sd_bus_message *m) {
+ assert(m);
+
+ if (m->header->version != 1)
+ return -EIO;
+
+ if (m->header->endian != SD_BUS_BIG_ENDIAN &&
+ m->header->endian != SD_BUS_LITTLE_ENDIAN)
+ return -EIO;
+
+ return 0;
+}
+
+static void setup_iovec(sd_bus_message *m) {
+ assert(m);
+ assert(m->sealed);
+
+ m->n_iovec = 0;
+
+ m->iovec[m->n_iovec].iov_base = m->header;
+ m->iovec[m->n_iovec].iov_len = sizeof(*m->header);
+ m->n_iovec++;
+
+ if (m->fields) {
+ m->iovec[m->n_iovec].iov_base = m->fields;
+ m->iovec[m->n_iovec].iov_len = m->header->fields_size;
+ m->n_iovec++;
+
+ if (m->header->fields_size % 8 != 0) {
+ static const uint8_t padding[7] = { 0, 0, 0, 0, 0, 0, 0 };
+
+ m->iovec[m->n_iovec].iov_base = (void*) padding;
+ m->iovec[m->n_iovec].iov_len = 8 - m->header->fields_size % 8;
+ m->n_iovec++;
+ }
+ }
+
+ if (m->body) {
+ m->iovec[m->n_iovec].iov_base = m->body;
+ m->iovec[m->n_iovec].iov_len = m->header->body_size;
+ m->n_iovec++;
+ }
+}
+
+int bus_message_seal(sd_bus_message *m, uint64_t serial) {
+ int r;
+
+ assert(m);
+
+ if (m->sealed)
+ return -EPERM;
+
+ if (m->n_containers > 0)
+ return -EBADMSG;
+
+ /* If there's a non-trivial signature set, then add it in here */
+ if (!isempty(m->root_container.signature)) {
+ r = message_append_field_signature(m, SD_BUS_MESSAGE_HEADER_SIGNATURE, m->root_container.signature, &m->signature);
+ if (r < 0)
+ return r;
+ }
+
+ if (m->n_fds > 0) {
+ r = message_append_field_uint32(m, SD_BUS_MESSAGE_HEADER_UNIX_FDS, m->n_fds);
+ if (r < 0)
+ return r;
+ }
+
+ m->header->serial = serial;
+ m->sealed = true;
+
+ setup_iovec(m);
+
+ return 0;
+}
+
+int sd_bus_message_set_destination(sd_bus_message *m, const char *destination) {
+ if (!m)
+ return -EINVAL;
+ if (!destination)
+ return -EINVAL;
+ if (m->sealed)
+ return -EPERM;
+ if (m->destination)
+ return -EEXIST;
+
+ return message_append_field_string(m, SD_BUS_MESSAGE_HEADER_DESTINATION, SD_BUS_TYPE_STRING, destination, &m->destination);
+}
+
+int bus_message_dump(sd_bus_message *m) {
+ unsigned level = 1;
+ int r;
+
+ assert(m);
+
+ printf("Message %p\n"
+ "\tn_ref=%u\n"
+ "\tendian=%c\n"
+ "\ttype=%i\n"
+ "\tflags=%u\n"
+ "\tversion=%u\n"
+ "\tserial=%u\n"
+ "\tfields_size=%u\n"
+ "\tbody_size=%u\n"
+ "\tpath=%s\n"
+ "\tinterface=%s\n"
+ "\tmember=%s\n"
+ "\tdestination=%s\n"
+ "\tsender=%s\n"
+ "\tsignature=%s\n"
+ "\treply_serial=%u\n"
+ "\terror.name=%s\n"
+ "\terror.message=%s\n"
+ "\tsealed=%s\n",
+ m,
+ m->n_ref,
+ m->header->endian,
+ m->header->type,
+ m->header->flags,
+ m->header->version,
+ BUS_MESSAGE_SERIAL(m),
+ BUS_MESSAGE_FIELDS_SIZE(m),
+ BUS_MESSAGE_BODY_SIZE(m),
+ strna(m->path),
+ strna(m->interface),
+ strna(m->member),
+ strna(m->destination),
+ strna(m->sender),
+ strna(m->signature),
+ m->reply_serial,
+ strna(m->error.name),
+ strna(m->error.message),
+ yes_no(m->sealed));
+
+ r = sd_bus_message_rewind(m, true);
+ if (r < 0) {
+ log_error("Failed to rewind: %s", strerror(-r));
+ return r;
+ }
+
+ printf("BEGIN_MESSAGE \"%s\" {\n", strempty(m->signature));
+
+ for(;;) {
+ _cleanup_free_ char *prefix = NULL;
+ const char *contents = NULL;
+ char type;
+ union {
+ uint8_t u8;
+ uint16_t u16;
+ int16_t s16;
+ uint32_t u32;
+ int32_t s32;
+ uint64_t u64;
+ int64_t s64;
+ double d64;
+ const char *string;
+ int i;
+ } basic;
+
+ r = sd_bus_message_peek_type(m, &type, &contents);
+ if (r < 0) {
+ log_error("Failed to peek type: %s", strerror(-r));
+ return r;
+ }
+ if (r == 0) {
+ if (level <= 1)
+ break;
+
+ r = sd_bus_message_exit_container(m);
+ if (r < 0) {
+ log_error("Failed to exit container: %s", strerror(-r));
+ return r;
+ }
+
+ level--;
+
+ prefix = strrep("\t", level);
+ if (!prefix)
+ return log_oom();
+
+ if (type == SD_BUS_TYPE_ARRAY)
+ printf("%s} END_ARRAY \n", prefix);
+ else if (type == SD_BUS_TYPE_VARIANT)
+ printf("%s} END_VARIANT\n", prefix);
+ else if (type == SD_BUS_TYPE_STRUCT)
+ printf("%s} END_STRUCT\n", prefix);
+ else if (type == SD_BUS_TYPE_DICT_ENTRY)
+ printf("%s} END_DICT_ENTRY\n", prefix);
+
+ continue;
+ }
+
+ prefix = strrep("\t", level);
+ if (!prefix)
+ return log_oom();
+
+ if (bus_type_is_container(type) > 0) {
+ r = sd_bus_message_enter_container(m, type, contents);
+ if (r < 0) {
+ log_error("Failed to enter container: %s", strerror(-r));
+ return r;
+ }
+
+ if (type == SD_BUS_TYPE_ARRAY)
+ printf("%sBEGIN_ARRAY \"%s\" {\n", prefix, contents);
+ else if (type == SD_BUS_TYPE_VARIANT)
+ printf("%sBEGIN_VARIANT \"%s\" {\n", prefix, contents);
+ else if (type == SD_BUS_TYPE_STRUCT)
+ printf("%sBEGIN_STRUCT \"%s\" {\n", prefix, contents);
+ else if (type == SD_BUS_TYPE_DICT_ENTRY)
+ printf("%sBEGIN_DICT_ENTRY \"%s\" {\n", prefix, contents);
+
+ level ++;
+
+ continue;
+ }
+
+ r = sd_bus_message_read_basic(m, type, &basic);
+ if (r < 0) {
+ log_error("Failed to get basic: %s", strerror(-r));
+ return r;
+ }
+
+ switch (type) {
+
+ case SD_BUS_TYPE_BYTE:
+ printf("%sBYTE: %u\n", prefix, basic.u8);
+ break;
+
+ case SD_BUS_TYPE_BOOLEAN:
+ printf("%sBOOLEAN: %s\n", prefix, yes_no(basic.i));
+ break;
+
+ case SD_BUS_TYPE_INT16:
+ printf("%sINT16: %i\n", prefix, basic.s16);
+ break;
+
+ case SD_BUS_TYPE_UINT16:
+ printf("%sUINT16: %u\n", prefix, basic.u16);
+ break;
+
+ case SD_BUS_TYPE_INT32:
+ printf("%sINT32: %i\n", prefix, basic.s32);
+ break;
+
+ case SD_BUS_TYPE_UINT32:
+ printf("%sUINT32: %u\n", prefix, basic.u32);
+ break;
+
+ case SD_BUS_TYPE_INT64:
+ printf("%sINT64: %lli\n", prefix, (long long) basic.s64);
+ break;
+
+ case SD_BUS_TYPE_UINT64:
+ printf("%sUINT64: %llu\n", prefix, (unsigned long long) basic.u64);
+ break;
+
+ case SD_BUS_TYPE_DOUBLE:
+ printf("%sDOUBLE: %g\n", prefix, basic.d64);
+ break;
+
+ case SD_BUS_TYPE_STRING:
+ printf("%sSTRING: \"%s\"\n", prefix, basic.string);
+ break;
+
+ case SD_BUS_TYPE_OBJECT_PATH:
+ printf("%sOBJECT_PATH: \"%s\"\n", prefix, basic.string);
+ break;
+
+ case SD_BUS_TYPE_SIGNATURE:
+ printf("%sSIGNATURE: \"%s\"\n", prefix, basic.string);
+ break;
+
+ case SD_BUS_TYPE_UNIX_FD:
+ printf("%sUNIX_FD: %i\n", prefix, basic.i);
+ break;
+
+ default:
+ assert_not_reached("Unknown basic type.");
+ }
+ }
+
+ printf("} END_MESSAGE\n");
+ return 0;
}
int bus_message_get_blob(sd_bus_message *m, void **buffer, size_t *sz) {
diff --git a/src/libsystemd-bus/bus-message.h b/src/libsystemd-bus/bus-message.h
index bfd2b12..89cb3e1 100644
--- a/src/libsystemd-bus/bus-message.h
+++ b/src/libsystemd-bus/bus-message.h
@@ -34,6 +34,7 @@ struct bus_container {
unsigned index;
uint32_t *array_size;
+ size_t begin;
};
_packed_ struct bus_header {
@@ -77,14 +78,18 @@ struct sd_bus_message {
void *fields;
void *body;
+ size_t rindex;
+
uint32_t n_fds;
int *fds;
- struct bus_container root_container, *sub_containers;
+ struct bus_container root_container, *containers;
unsigned n_containers;
struct iovec iovec[4];
unsigned n_iovec;
+
+ char *peeked_signature;
};
#if __BYTE_ORDER == __BIG_ENDIAN
@@ -93,20 +98,28 @@ struct sd_bus_message {
#define BUS_MESSAGE_NEED_BSWAP(m) ((m)->header->endian != SD_BUS_LITTLE_ENDIAN)
#endif
-static inline uint32_t BUS_MESSAGE_BSWAP(sd_bus_message *m, uint32_t u) {
+static inline uint16_t BUS_MESSAGE_BSWAP16(sd_bus_message *m, uint16_t u) {
+ return BUS_MESSAGE_NEED_BSWAP(m) ? bswap_16(u) : u;
+}
+
+static inline uint32_t BUS_MESSAGE_BSWAP32(sd_bus_message *m, uint32_t u) {
return BUS_MESSAGE_NEED_BSWAP(m) ? bswap_32(u) : u;
}
+static inline uint64_t BUS_MESSAGE_BSWAP64(sd_bus_message *m, uint64_t u) {
+ return BUS_MESSAGE_NEED_BSWAP(m) ? bswap_64(u) : u;
+}
+
static inline uint32_t BUS_MESSAGE_SERIAL(sd_bus_message *m) {
- return BUS_MESSAGE_BSWAP(m, m->header->serial);
+ return BUS_MESSAGE_BSWAP32(m, m->header->serial);
}
static inline uint32_t BUS_MESSAGE_BODY_SIZE(sd_bus_message *m) {
- return BUS_MESSAGE_BSWAP(m, m->header->body_size);
+ return BUS_MESSAGE_BSWAP32(m, m->header->body_size);
}
static inline uint32_t BUS_MESSAGE_FIELDS_SIZE(sd_bus_message *m) {
- return BUS_MESSAGE_BSWAP(m, m->header->fields_size);
+ return BUS_MESSAGE_BSWAP32(m, m->header->fields_size);
}
static inline void bus_message_unrefp(sd_bus_message **m) {
@@ -115,7 +128,7 @@ static inline void bus_message_unrefp(sd_bus_message **m) {
#define _cleanup_bus_message_unref_ __attribute__((cleanup(bus_message_unrefp)))
-int message_parse(sd_bus_message *m);
-int message_seal(sd_bus_message *m, uint64_t serial);
-void message_dump(sd_bus_message *m);
+int bus_message_parse(sd_bus_message *m);
+int bus_message_seal(sd_bus_message *m, uint64_t serial);
+int bus_message_dump(sd_bus_message *m);
int bus_message_get_blob(sd_bus_message *m, void **buffer, size_t *sz);
diff --git a/src/libsystemd-bus/sd-bus.c b/src/libsystemd-bus/sd-bus.c
index f7d7644..3f23da6 100644
--- a/src/libsystemd-bus/sd-bus.c
+++ b/src/libsystemd-bus/sd-bus.c
@@ -760,7 +760,7 @@ static int bus_seal_message(sd_bus *b, sd_bus_message *m) {
if (m->sealed)
return 0;
- return message_seal(m, ++b->serial);
+ return bus_message_seal(m, ++b->serial);
}
static int message_write(sd_bus *bus, sd_bus_message *m, size_t *idx) {
@@ -856,7 +856,7 @@ static int message_make(sd_bus *bus, size_t size, sd_bus_message **m) {
bus->rbuffer = b;
bus->rbuffer_size -= size;
- r = message_parse(t);
+ r = bus_message_parse(t);
if (r < 0) {
sd_bus_message_unref(t);
return r;
diff --git a/src/libsystemd-bus/sd-bus.h b/src/libsystemd-bus/sd-bus.h
index 6d53d5a..d655f05 100644
--- a/src/libsystemd-bus/sd-bus.h
+++ b/src/libsystemd-bus/sd-bus.h
@@ -27,6 +27,13 @@
#include "sd-bus-protocol.h"
+/* TODO:
+ *
+ * - make unix fd passing work
+ * - add page donation logic
+ * - api for appending/reading fixed arrays
+ */
+
typedef struct sd_bus sd_bus;
typedef struct sd_bus_message sd_bus_message;
@@ -105,9 +112,12 @@ int sd_bus_message_append_basic(sd_bus_message *m, char type, const void *p);
int sd_bus_message_open_container(sd_bus_message *m, char type, const char *contents);
int sd_bus_message_close_container(sd_bus_message *m);
-int sd_bus_message_read_type(sd_bus_message *m, char *type, char *element, size_t *length);
-int sd_bus_message_read_basic(sd_bus_message *m, char type, char element, const void **p, size_t *length);
int sd_bus_message_read(sd_bus_message *m, const char *types, ...);
+int sd_bus_message_read_basic(sd_bus_message *m, char type, void *p);
+int sd_bus_message_enter_container(sd_bus_message *m, char type, const char *contents);
+int sd_bus_message_exit_container(sd_bus_message *m);
+int sd_bus_message_peek_type(sd_bus_message *m, char *type, const char **contents);
+int sd_bus_message_rewind(sd_bus_message *m, bool complete);
/* Bus management */
diff --git a/src/libsystemd-bus/test-bus-marshal.c b/src/libsystemd-bus/test-bus-marshal.c
index 08364ef..9138f11 100644
--- a/src/libsystemd-bus/test-bus-marshal.c
+++ b/src/libsystemd-bus/test-bus-marshal.c
@@ -37,8 +37,8 @@
int main(int argc, char *argv[]) {
_cleanup_bus_message_unref_ sd_bus_message *m = NULL;
- int r;
- const char *x, *y, *z, *a, *b, *c;
+ int r, boolean;
+ const char *x, *y, *z, *a, *b, *c, *d;
uint8_t u, v;
void *buffer = NULL;
size_t sz;
@@ -74,10 +74,10 @@ int main(int argc, char *argv[]) {
r = sd_bus_message_close_container(m);
assert_se(r >= 0);
- r = message_seal(m, 4711);
+ r = bus_message_seal(m, 4711);
assert_se(r >= 0);
- message_dump(m);
+ bus_message_dump(m);
r = bus_message_get_blob(m, &buffer, &sz);
assert_se(r >= 0);
@@ -118,12 +118,48 @@ int main(int argc, char *argv[]) {
free(buffer);
- /* r = sd_bus_message_read(m, "sas", &x, 5, &y, &z, &a, &b, &c); */
- /* assert_se(r >= 0); */
-
- /* r = sd_bus_message_read(m, "a{yv}", 2, */
- /* &u, "s", &x, */
- /* &v, "s", &y); */
+ assert_se(sd_bus_message_rewind(m, true) >= 0);
+
+ r = sd_bus_message_read(m, "sas", &x, 2, &y, &z);
+ assert_se(r > 0);
+ assert_se(streq(x, "a string"));
+ assert_se(streq(y, "string #1"));
+ assert_se(streq(z, "string #2"));
+
+ r = sd_bus_message_read(m, "sass", &x, 5, &y, &z, &a, &b, &c, &d);
+ assert_se(r > 0);
+ assert_se(streq(x, "foobar"));
+ assert_se(streq(y, "foo"));
+ assert_se(streq(z, "bar"));
+ assert_se(streq(a, "waldo"));
+ assert_se(streq(b, "piep"));
+ assert_se(streq(c, "pap"));
+ assert_se(streq(d, "after"));
+
+ r = sd_bus_message_read(m, "a{yv}", 2, &u, "s", &x, &v, "s", &y);
+ assert_se(r > 0);
+ assert_se(u == 3);
+ assert_se(streq(x, "foo"));
+ assert_se(v == 5);
+ assert_se(streq(y, "waldo"));
+
+ r = sd_bus_message_read(m, "ba(ss)", &boolean, 3, &x, &y, &a, &b, &c, &d);
+ assert_se(r > 0);
+ assert_se(boolean);
+ assert_se(streq(x, "aaa"));
+ assert_se(streq(y, "1"));
+ assert_se(streq(a, "bbb"));
+ assert_se(streq(b, "2"));
+ assert_se(streq(c, "ccc"));
+ assert_se(streq(d, "3"));
+
+ r = sd_bus_message_read(m, "as", 2, &x, &y);
+ assert_se(r > 0);
+ assert_se(streq(x, "foobar"));
+ assert_se(streq(y, "waldo"));
+
+ r = sd_bus_message_peek_type(m, NULL, NULL);
+ assert_se(r == 0);
return 0;
}
diff --git a/src/shared/util.c b/src/shared/util.c
index f94abbc..69f1b00 100644
--- a/src/shared/util.c
+++ b/src/shared/util.c
@@ -5797,3 +5797,22 @@ char *strextend(char **x, ...) {
return r + l;
}
+
+char *strrep(const char *s, unsigned n) {
+ size_t l;
+ char *r, *p;
+ unsigned i;
+
+ assert(s);
+
+ l = strlen(s);
+ p = r = malloc(l * n + 1);
+ if (!r)
+ return NULL;
+
+ for (i = 0; i < n; i++)
+ p = stpcpy(p, s);
+
+ *p = 0;
+ return r;
+}
diff --git a/src/shared/util.h b/src/shared/util.h
index 25c0084..f75c66b 100644
--- a/src/shared/util.h
+++ b/src/shared/util.h
@@ -608,3 +608,4 @@ static inline void *mempset(void *s, int c, size_t n) {
char *hexmem(const void *p, size_t l);
char *strextend(char **x, ...);
+char *strrep(const char *s, unsigned n);
commit 5407f2dea3efa5234140f1adb9c2be9916d77cdc
Author: Lennart Poettering <lennart at poettering.net>
Date: Tue Mar 19 20:16:27 2013 +0100
bus: suppress reply messages to method calls with no_reply set
diff --git a/src/libsystemd-bus/bus-message.c b/src/libsystemd-bus/bus-message.c
index 4c5588c..ccea120 100644
--- a/src/libsystemd-bus/bus-message.c
+++ b/src/libsystemd-bus/bus-message.c
@@ -245,6 +245,8 @@ int sd_bus_message_new_signal(
return -EINVAL;
if (!member)
return -EINVAL;
+ if (!m)
+ return -EINVAL;
t = message_new(bus, SD_BUS_MESSAGE_TYPE_SIGNAL);
if (!t)
@@ -283,6 +285,8 @@ int sd_bus_message_new_method_call(
return -EINVAL;
if (!member)
return -EINVAL;
+ if (!m)
+ return -EINVAL;
t = message_new(bus, SD_BUS_MESSAGE_TYPE_METHOD_CALL);
if (!t)
@@ -315,9 +319,10 @@ fail:
return r;
}
-int sd_bus_message_new_method_return(
+static int message_new_reply(
sd_bus *bus,
sd_bus_message *call,
+ uint8_t type,
sd_bus_message **m) {
sd_bus_message *t;
@@ -327,12 +332,15 @@ int sd_bus_message_new_method_return(
return -EINVAL;
if (call->header->type != SD_BUS_MESSAGE_TYPE_METHOD_CALL)
return -EINVAL;
+ if (!m)
+ return -EINVAL;
- t = message_new(bus, SD_BUS_MESSAGE_TYPE_METHOD_RETURN);
+ t = message_new(bus, type);
if (!t)
return -ENOMEM;
t->reply_serial = BUS_MESSAGE_SERIAL(call);
+
r = message_append_field_uint32(t, SD_BUS_MESSAGE_HEADER_REPLY_SERIAL, t->reply_serial);
if (r < 0)
goto fail;
@@ -343,14 +351,23 @@ int sd_bus_message_new_method_return(
goto fail;
}
+ t->dont_send = !!(call->header->flags & SD_BUS_MESSAGE_NO_REPLY_EXPECTED);
+
*m = t;
- return 0;
fail:
message_free(t);
return r;
}
+int sd_bus_message_new_method_return(
+ sd_bus *bus,
+ sd_bus_message *call,
+ sd_bus_message **m) {
+
+ return message_new_reply(bus, call, SD_BUS_MESSAGE_TYPE_METHOD_RETURN, m);
+}
+
int sd_bus_message_new_method_error(
sd_bus *bus,
sd_bus_message *call,
@@ -360,29 +377,16 @@ int sd_bus_message_new_method_error(
sd_bus_message *t;
int r;
- if (!call)
- return -EINVAL;
- if (call->header->type != SD_BUS_MESSAGE_TYPE_METHOD_CALL)
- return -EINVAL;
if (!e)
return -EINVAL;
if (!e->name)
return -EINVAL;
+ if (!m)
+ return -EINVAL;
- t = message_new(bus, SD_BUS_MESSAGE_TYPE_METHOD_ERROR);
- if (!t)
- return -ENOMEM;
-
- t->reply_serial = BUS_MESSAGE_SERIAL(call);
- r = message_append_field_uint32(t, SD_BUS_MESSAGE_HEADER_REPLY_SERIAL, t->reply_serial);
+ r = message_new_reply(bus, call, SD_BUS_MESSAGE_TYPE_METHOD_ERROR, &t);
if (r < 0)
- goto fail;
-
- if (call->sender) {
- r = message_append_field_string(t, SD_BUS_MESSAGE_HEADER_DESTINATION, SD_BUS_TYPE_STRING, call->sender, &t->sender);
- if (r < 0)
- goto fail;
- }
+ return r;
r = message_append_field_string(t, SD_BUS_MESSAGE_HEADER_ERROR_NAME, SD_BUS_TYPE_STRING, e->name, &t->error.name);
if (r < 0)
diff --git a/src/libsystemd-bus/bus-message.h b/src/libsystemd-bus/bus-message.h
index c62659e..bfd2b12 100644
--- a/src/libsystemd-bus/bus-message.h
+++ b/src/libsystemd-bus/bus-message.h
@@ -71,6 +71,7 @@ struct sd_bus_message {
bool free_header:1;
bool free_fields:1;
bool free_body:1;
+ bool dont_send:1;
struct bus_header *header;
void *fields;
diff --git a/src/libsystemd-bus/sd-bus.c b/src/libsystemd-bus/sd-bus.c
index d80f097..f7d7644 100644
--- a/src/libsystemd-bus/sd-bus.c
+++ b/src/libsystemd-bus/sd-bus.c
@@ -996,6 +996,11 @@ int sd_bus_send(sd_bus *bus, sd_bus_message *m, uint64_t *serial) {
if (r < 0)
return r;
+ /* If this is a reply and no reply was requested, then let's
+ * suppress this, if we can */
+ if (m->dont_send && !serial)
+ return 0;
+
if (bus->wqueue_size <= 0) {
size_t idx = 0;
commit de1c301ed165eb4d04a0c9d4babe97912b5233bb
Author: Lennart Poettering <lennart at poettering.net>
Date: Tue Mar 19 20:03:16 2013 +0100
bus: add basic implementation of a native bus client library
diff --git a/.gitignore b/.gitignore
index 7385107..917b066 100644
--- a/.gitignore
+++ b/.gitignore
@@ -80,6 +80,8 @@
/systemd-user-sessions
/systemd-vconsole-setup
/tags
+/test-bus-marshal
+/test-bus-signature
/test-calendarspec
/test-catalog
/test-cgroup
diff --git a/Makefile.am b/Makefile.am
index 37eeb01..c94c192 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -1639,6 +1639,50 @@ EXTRA_DIST += \
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 += \
docs/libudev
diff --git a/configure.ac b/configure.ac
index 5d1be3f..ae492cf 100644
--- a/configure.ac
+++ b/configure.ac
@@ -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@:>@]),
diff --git a/src/libsystemd-bus/Makefile b/src/libsystemd-bus/Makefile
new file mode 120000
index 0000000..d0b0e8e
--- /dev/null
+++ b/src/libsystemd-bus/Makefile
@@ -0,0 +1 @@
+../Makefile
\ No newline at end of file
diff --git a/src/libsystemd-bus/bus-control.c b/src/libsystemd-bus/bus-control.c
new file mode 100644
index 0000000..e4cc251
--- /dev/null
+++ b/src/libsystemd-bus/bus-control.c
@@ -0,0 +1,322 @@
+/*-*- 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);
+}
diff --git a/src/libsystemd-bus/bus-error.c b/src/libsystemd-bus/bus-error.c
new file mode 100644
index 0000000..0832022
--- /dev/null
+++ b/src/libsystemd-bus/bus-error.c
@@ -0,0 +1,166 @@
+/*-*- 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;
+}
diff --git a/src/libsystemd-bus/bus-error.h b/src/libsystemd-bus/bus-error.h
new file mode 100644
index 0000000..f49e6c7
--- /dev/null
+++ b/src/libsystemd-bus/bus-error.h
@@ -0,0 +1,27 @@
+/*-*- 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);
diff --git a/src/libsystemd-bus/bus-internal.c b/src/libsystemd-bus/bus-internal.c
new file mode 100644
index 0000000..19398af
--- /dev/null
+++ b/src/libsystemd-bus/bus-internal.c
@@ -0,0 +1,22 @@
+/*-*- 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"
diff --git a/src/libsystemd-bus/bus-internal.h b/src/libsystemd-bus/bus-internal.h
new file mode 100644
index 0000000..d8cdc48
--- /dev/null
+++ b/src/libsystemd-bus/bus-internal.h
@@ -0,0 +1,101 @@
+/*-*- 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>
+
+#include "hashmap.h"
+#include "list.h"
+#include "util.h"
+
+#include "sd-bus.h"
+#include "bus-error.h"
+
+struct reply_callback {
+ sd_message_handler_t callback;
+ void *userdata;
+ usec_t timeout;
+ uint64_t serial;
+};
+
+struct filter_callback {
+ sd_message_handler_t callback;
+ void *userdata;
+
+ LIST_FIELDS(struct filter_callback, callbacks);
+};
+
+enum bus_state {
+ BUS_OPENING,
+ BUS_AUTHENTICATING,
+ BUS_HELLO,
+ BUS_RUNNING,
+ BUS_CLOSED
+};
+
+struct sd_bus {
+ unsigned n_ref;
+ enum bus_state state;
+ int fd;
+ int message_version;
+ bool can_fds:1;
+ bool send_hello:1;
+
+ void *rbuffer;
+ size_t rbuffer_size;
+
+ sd_bus_message **rqueue;
+ unsigned rqueue_size;
+
+ sd_bus_message **wqueue;
+ unsigned wqueue_size;
+ size_t windex;
+
+ uint64_t serial;
+
+ char *unique_name;
+
+ Hashmap *reply_callbacks;
+ LIST_HEAD(struct filter_callback, filter_callbacks);
+
+ union {
+ struct sockaddr sa;
+ struct sockaddr_un un;
+ struct sockaddr_in in;
+ struct sockaddr_in6 in6;
+ } sockaddr;
+ socklen_t sockaddr_size;
+
+ sd_id128_t peer;
+
+ char *address;
+ unsigned address_index;
+
+ int last_connect_error;
+
+ struct iovec auth_iovec[3];
+ unsigned auth_index;
+ size_t auth_size;
+ char *auth_uid;
+};
diff --git a/src/libsystemd-bus/bus-message.c b/src/libsystemd-bus/bus-message.c
new file mode 100644
index 0000000..4c5588c
--- /dev/null
+++ b/src/libsystemd-bus/bus-message.c
@@ -0,0 +1,1413 @@
+/*-*- 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 "util.h"
+
+#include "bus-message.h"
+#include "sd-bus.h"
+#include "bus-internal.h"
+#include "bus-type.h"
+#include "bus-signature.h"
+
+int message_append_basic(sd_bus_message *m, char type, const void *p, const void **stored);
+
+static void message_free(sd_bus_message *m) {
+ unsigned i;
+
+ assert(m);
+
+ if (m->free_header)
+ free(m->header);
+
+ if (m->free_fields)
+ free(m->fields);
+
+ if (m->free_body)
+ free(m->body);
+
+ for (i = 0; i < m->n_fds; i++)
+ close_nointr_nofail(m->fds[i]);
+
+ for (i = 0; i < m->n_containers; i++)
+ free(m->sub_containers[i].signature);
+
+ free(m->sub_containers);
+ free(m);
+}
+
+static void* buffer_extend(void **p, uint32_t *sz, size_t align, size_t extend) {
+ size_t start, n;
+ void *k;
+
+ assert(p);
+ assert(sz);
+ assert(align > 0);
+
+ start = ALIGN_TO((size_t) *sz, align);
+ n = start + extend;
+
+ if (n == *sz)
+ return (uint8_t*) *p + start;
+
+ if (n > (size_t) ((uint32_t) -1))
+ return NULL;
+
+ k = realloc(*p, n);
+ if (!k)
+ return NULL;
+
+ /* Zero out padding */
+ if (start > *sz)
+ memset((uint8_t*) k + *sz, 0, start - *sz);
+
+ *p = k;
+ *sz = n;
+
+ return (uint8_t*) k + start;
+}
+
+static void *message_extend_fields(sd_bus_message *m, size_t align, size_t sz) {
+ void *p, *o;
+
+ assert(m);
+
+ o = m->fields;
+ p = buffer_extend(&m->fields, &m->header->fields_size, align, sz);
+ if (!p)
+ return NULL;
+
+ if (o != m->fields) {
+ /* Adjust quick access pointers */
+
+ if (m->path)
+ m->path = (const char*) m->fields + (m->path - (const char*) o);
+ if (m->interface)
+ m->interface = (const char*) m->fields + (m->interface - (const char*) o);
+ if (m->member)
+ m->member = (const char*) m->fields + (m->member - (const char*) o);
+ if (m->destination)
+ m->destination = (const char*) m->fields + (m->destination - (const char*) o);
+ if (m->sender)
+ m->sender = (const char*) m->fields + (m->sender - (const char*) o);
+ if (m->signature)
+ m->signature = (const char*) m->fields + (m->signature - (const char*) o);
+ if (m->error.name)
+ m->error.name = (const char*) m->fields + (m->error.name - (const char*) o);
+ }
+
+ m->free_fields = true;
+
+ return p;
+}
+
+static int message_append_field_string(
+ sd_bus_message *m,
+ uint8_t h,
+ char type,
+ const char *s,
+ const char **ret) {
+
+ size_t l;
+ uint8_t *p;
+
+ assert(m);
+
+ l = strlen(s);
+ if (l > (size_t) (uint32_t) -1)
+ return -EINVAL;
+
+ /* field id byte + signature length + signature 's' + NUL + string length + string + NUL */
+ p = message_extend_fields(m, 8, 4 + 4 + l + 1);
+ if (!p)
+ return -ENOMEM;
+
+ p[0] = h;
+ p[1] = 1;
+ p[2] = type;
+ p[3] = 0;
+
+ ((uint32_t*) p)[1] = l;
+ memcpy(p + 8, s, l + 1);
+
+ if (ret)
+ *ret = (const char*) p + 8;
+
+ return 0;
+}
+
+static int message_append_field_signature(
+ sd_bus_message *m,
+ uint8_t h,
+ const char *s,
+ const char **ret) {
+
+ size_t l;
+ uint8_t *p;
+
+ assert(m);
+
+ l = strlen(s);
+ if (l > 255)
+ return -EINVAL;
+
+ /* field id byte + signature length + signature 'g' + NUL + string length + string + NUL */
+ p = message_extend_fields(m, 8, 4 + 1 + l + 1);
+ if (!p)
+ return -ENOMEM;
+
+ p[0] = h;
+ p[1] = 1;
+ p[2] = SD_BUS_TYPE_SIGNATURE;
+ p[3] = 0;
+ p[4] = l;
+ memcpy(p + 5, s, l + 1);
+
+ if (ret)
+ *ret = (const char*) p + 5;
+
+ return 0;
+}
+
+static int message_append_field_uint32(sd_bus_message *m, uint8_t h, uint32_t x) {
+ uint8_t *p;
+
+ assert(m);
+
+ /* field id byte + signature length + signature 'u' + NUL + value */
+ p = message_extend_fields(m, 8, 4 + 4);
+ if (!p)
+ return -ENOMEM;
+
+ p[0] = h;
+ p[1] = 1;
+ p[2] = SD_BUS_TYPE_UINT32;
+ p[3] = 0;
+
+ ((uint32_t*) p)[1] = x;
+
+ return 0;
+}
+
+static sd_bus_message *message_new(sd_bus *bus, uint8_t type) {
+ sd_bus_message *m;
+
+ m = malloc0(ALIGN(sizeof(struct sd_bus_message)) + sizeof(struct bus_header));
+ if (!m)
+ return NULL;
+
+ m->n_ref = 1;
+ m->header = (struct bus_header*) ((uint8_t*) m + ALIGN(sizeof(struct sd_bus_message)));
+
+#if __BYTE_ORDER == __BIG_ENDIAN
+ m->header->endian = SD_BUS_BIG_ENDIAN;
+#else
+ m->header->endian = SD_BUS_LITTLE_ENDIAN;
+#endif
+ m->header->type = type;
+ m->header->version = bus ? bus->message_version : 1;
+
+ return m;
+}
+
+int sd_bus_message_new_signal(
+ sd_bus *bus,
+ const char *path,
+ const char *interface,
+ const char *member,
+ sd_bus_message **m) {
+
+ sd_bus_message *t;
+ int r;
+
+ if (!path)
+ return -EINVAL;
+ if (!interface)
+ return -EINVAL;
+ if (!member)
+ return -EINVAL;
+
+ t = message_new(bus, SD_BUS_MESSAGE_TYPE_SIGNAL);
+ if (!t)
+ return -ENOMEM;
+
+ r = message_append_field_string(t, SD_BUS_MESSAGE_HEADER_PATH, SD_BUS_TYPE_OBJECT_PATH, path, &t->path);
+ if (r < 0)
+ goto fail;
+ r = message_append_field_string(t, SD_BUS_MESSAGE_HEADER_INTERFACE, SD_BUS_TYPE_STRING, interface, &t->interface);
+ if (r < 0)
+ goto fail;
+ r = message_append_field_string(t, SD_BUS_MESSAGE_HEADER_MEMBER, SD_BUS_TYPE_STRING, member, &t->member);
+ if (r < 0)
+ goto fail;
+
+ *m = t;
+ return 0;
+
+fail:
+ sd_bus_message_unref(t);
+ return r;
+}
+
+int sd_bus_message_new_method_call(
+ sd_bus *bus,
+ const char *destination,
+ const char *path,
+ const char *interface,
+ const char *member,
+ sd_bus_message **m) {
+
+ sd_bus_message *t;
+ int r;
+
+ if (!path)
+ return -EINVAL;
+ if (!member)
+ return -EINVAL;
+
+ t = message_new(bus, SD_BUS_MESSAGE_TYPE_METHOD_CALL);
+ if (!t)
+ return -ENOMEM;
+
+ r = message_append_field_string(t, SD_BUS_MESSAGE_HEADER_PATH, SD_BUS_TYPE_OBJECT_PATH, path, &t->path);
+ if (r < 0)
+ goto fail;
+ r = message_append_field_string(t, SD_BUS_MESSAGE_HEADER_MEMBER, SD_BUS_TYPE_STRING, member, &t->member);
+ if (r < 0)
+ goto fail;
+
+ if (interface) {
+ r = message_append_field_string(t, SD_BUS_MESSAGE_HEADER_INTERFACE, SD_BUS_TYPE_STRING, interface, &t->interface);
+ if (r < 0)
+ goto fail;
+ }
+
+ if (destination) {
+ r = message_append_field_string(t, SD_BUS_MESSAGE_HEADER_DESTINATION, SD_BUS_TYPE_STRING, destination, &t->destination);
+ if (r < 0)
+ goto fail;
+ }
+
+ *m = t;
+ return 0;
+
+fail:
+ message_free(t);
+ return r;
+}
+
+int sd_bus_message_new_method_return(
+ sd_bus *bus,
+ sd_bus_message *call,
+ sd_bus_message **m) {
+
+ sd_bus_message *t;
+ int r;
+
+ if (!call)
+ return -EINVAL;
+ if (call->header->type != SD_BUS_MESSAGE_TYPE_METHOD_CALL)
+ return -EINVAL;
+
+ t = message_new(bus, SD_BUS_MESSAGE_TYPE_METHOD_RETURN);
+ if (!t)
+ return -ENOMEM;
+
+ t->reply_serial = BUS_MESSAGE_SERIAL(call);
+ r = message_append_field_uint32(t, SD_BUS_MESSAGE_HEADER_REPLY_SERIAL, t->reply_serial);
+ if (r < 0)
+ goto fail;
+
+ if (call->sender) {
+ r = message_append_field_string(t, SD_BUS_MESSAGE_HEADER_DESTINATION, SD_BUS_TYPE_STRING, call->sender, &t->sender);
+ if (r < 0)
+ goto fail;
+ }
+
+ *m = t;
+ return 0;
+
+fail:
+ message_free(t);
+ return r;
+}
+
+int sd_bus_message_new_method_error(
+ sd_bus *bus,
+ sd_bus_message *call,
+ const sd_bus_error *e,
+ sd_bus_message **m) {
+
+ sd_bus_message *t;
+ int r;
+
+ if (!call)
+ return -EINVAL;
+ if (call->header->type != SD_BUS_MESSAGE_TYPE_METHOD_CALL)
+ return -EINVAL;
+ if (!e)
+ return -EINVAL;
+ if (!e->name)
+ return -EINVAL;
+
+ t = message_new(bus, SD_BUS_MESSAGE_TYPE_METHOD_ERROR);
+ if (!t)
+ return -ENOMEM;
+
+ t->reply_serial = BUS_MESSAGE_SERIAL(call);
+ r = message_append_field_uint32(t, SD_BUS_MESSAGE_HEADER_REPLY_SERIAL, t->reply_serial);
+ if (r < 0)
+ goto fail;
+
+ if (call->sender) {
+ r = message_append_field_string(t, SD_BUS_MESSAGE_HEADER_DESTINATION, SD_BUS_TYPE_STRING, call->sender, &t->sender);
+ if (r < 0)
+ goto fail;
+ }
+
+ r = message_append_field_string(t, SD_BUS_MESSAGE_HEADER_ERROR_NAME, SD_BUS_TYPE_STRING, e->name, &t->error.name);
+ if (r < 0)
+ goto fail;
+
+ if (e->message) {
+ r = message_append_basic(t, SD_BUS_TYPE_STRING, e->message, (const void**) &t->error.message);
+ if (r < 0)
+ goto fail;
+ }
+
+ *m = t;
+ return 0;
+
+fail:
+ message_free(t);
+ return r;
+}
+
+sd_bus_message* sd_bus_message_ref(sd_bus_message *m) {
+ if (!m)
+ return NULL;
+
+ assert(m->n_ref > 0);
+ m->n_ref++;
+
+ return m;
+}
+
+sd_bus_message* sd_bus_message_unref(sd_bus_message *m) {
+ if (!m)
+ return NULL;
+
+ assert(m->n_ref > 0);
+ m->n_ref--;
+
+ if (m->n_ref <= 0)
+ message_free(m);
+
+ return NULL;
+}
+
+int sd_bus_message_get_type(sd_bus_message *m, uint8_t *type) {
+ if (!m)
+ return -EINVAL;
+ if (!type)
+ return -EINVAL;
+
+ *type = m->header->type;
+ return 0;
+}
+
+int sd_bus_message_get_serial(sd_bus_message *m, uint64_t *serial) {
+ if (!m)
+ return -EINVAL;
+ if (!serial)
+ return -EINVAL;
+ if (m->header->serial == 0)
+ return -ENOENT;
+
+ *serial = BUS_MESSAGE_SERIAL(m);
+ return 0;
+}
+
+int sd_bus_message_get_reply_serial(sd_bus_message *m, uint64_t *serial) {
+ if (!m)
+ return -EINVAL;
+ if (!serial)
+ return -EINVAL;
+ if (m->reply_serial == 0)
+ return -ENOENT;
+
+ *serial = m->reply_serial;
+ return 0;
+}
+
+int sd_bus_message_get_no_reply(sd_bus_message *m) {
+ if (!m)
+ return -EINVAL;
+
+ return m->header->type == SD_BUS_MESSAGE_TYPE_METHOD_CALL ? !!(m->header->flags & SD_BUS_MESSAGE_NO_REPLY_EXPECTED) : 0;
+}
+
+const char *sd_bus_message_get_path(sd_bus_message *m) {
+ if (!m)
+ return NULL;
+
+ return m->path;
+}
+
+const char *sd_bus_message_get_interface(sd_bus_message *m) {
+ if (!m)
+ return NULL;
+
+ return m->interface;
+}
+
+const char *sd_bus_message_get_member(sd_bus_message *m) {
+ if (!m)
+ return NULL;
+
+ return m->member;
+}
+const char *sd_bus_message_get_destination(sd_bus_message *m) {
+ if (!m)
+ return NULL;
+
+ return m->destination;
+}
+
+const char *sd_bus_message_get_sender(sd_bus_message *m) {
+ if (!m)
+ return NULL;
+
+ return m->sender;
+}
+
+const sd_bus_error *sd_bus_message_get_error(sd_bus_message *m) {
+ if (!m)
+ return NULL;
+
+ if (!sd_bus_error_is_set(&m->error))
+ return NULL;
+
+ return &m->error;
+}
+
+int sd_bus_message_get_uid(sd_bus_message *m, uid_t *uid) {
+ if (!m)
+ return -EINVAL;
+ if (!m->uid_valid)
+ return -ENOENT;
+
+ *uid = m->uid;
+ return 0;
+}
+
+int sd_bus_message_get_gid(sd_bus_message *m, gid_t *gid) {
+ if (!m)
+ return -EINVAL;
+ if (!m->gid_valid)
+ return -ENOENT;
+
+ *gid = m->gid;
+ return 0;
+}
+
+int sd_bus_message_get_pid(sd_bus_message *m, pid_t *pid) {
+ if (!m)
+ return -EINVAL;
+ if (m->pid <= 0)
+ return -ENOENT;
+
+ *pid = m->pid;
+ return 0;
+}
+
+int sd_bus_message_get_tid(sd_bus_message *m, pid_t *tid) {
+ if (!m)
+ return -EINVAL;
+ if (m->tid <= 0)
+ return -ENOENT;
+
+ *tid = m->tid;
+ return 0;
+}
+
+int sd_bus_message_is_signal(sd_bus_message *m, const char *interface, const char *member) {
+ if (!m)
+ return -EINVAL;
+
+ if (m->header->type != SD_BUS_MESSAGE_TYPE_SIGNAL)
+ return 0;
+
+ if (interface && (!m->interface || !streq(m->interface, interface)))
+ return 0;
+
+ if (member && (!m->member || !streq(m->member, member)))
+ return 0;
+
+ return 1;
+}
+
+int sd_bus_message_is_method_call(sd_bus_message *m, const char *interface, const char *member) {
+ if (!m)
+ return -EINVAL;
+
+ if (m->header->type != SD_BUS_MESSAGE_TYPE_METHOD_CALL)
+ return 0;
+
+ if (interface && (!m->interface || !streq(m->interface, interface)))
+ return 0;
+
+ if (member && (!m->member || !streq(m->member, member)))
+ return 0;
+
+ return 1;
+}
+
+int sd_bus_message_is_method_error(sd_bus_message *m, const char *name) {
+ if (!m)
+ return -EINVAL;
+
+ if (m->header->type != SD_BUS_MESSAGE_TYPE_METHOD_ERROR)
+ return 0;
+
+ if (name && (!m->error.name || !streq(m->error.name, name)))
+ return 0;
+
+ return 1;
+}
+
+int sd_bus_message_set_no_reply(sd_bus_message *m, int b) {
+ if (!m)
+ return -EINVAL;
+ if (m->sealed)
+ return -EPERM;
+ if (m->header->type != SD_BUS_MESSAGE_TYPE_METHOD_CALL)
+ return -EPERM;
+
+ if (b)
+ m->header->flags |= SD_BUS_MESSAGE_NO_REPLY_EXPECTED;
+ else
+ m->header->flags &= ~SD_BUS_MESSAGE_NO_REPLY_EXPECTED;
+
+ return 0;
+}
+
+static struct bus_container *message_get_container(sd_bus_message *m) {
+ assert(m);
+
+ if (m->n_containers == 0)
+ return &m->root_container;
+
+ assert(m->sub_containers);
+ return m->sub_containers + m->n_containers - 1;
+}
+
+static void *message_extend_body(sd_bus_message *m, size_t align, size_t sz) {
+ void *p, *o;
+ size_t added;
+ struct bus_container *c;
+
+ assert(m);
+
+ o = m->body;
+ added = m->header->body_size;
+
+ p = buffer_extend(&m->body, &m->header->body_size, align, sz);
+ if (!p)
+ return NULL;
+
+ added = m->header->body_size - added;
+
+ for (c = m->sub_containers; c < m->sub_containers + m->n_containers; c++)
+ if (c->array_size) {
+ c->array_size = (uint32_t*) ((uint8_t*) m->body + ((uint8_t*) c->array_size - (uint8_t*) o));
+ *c->array_size += added;
+ }
+
+ if (o != m->body) {
+ if (m->error.message)
+ m->error.message = (const char*) m->body + (m->error.message - (const char*) o);
+ }
+
+ m->free_body = true;
+
+ return p;
+}
+
+int message_append_basic(sd_bus_message *m, char type, const void *p, const void **stored) {
+ struct bus_container *c;
+ size_t sz, align, nindex;
+ uint32_t k;
+ void *a;
+ char *e = NULL;
+
+ if (!m)
+ return -EINVAL;
+ if (m->sealed)
+ return -EPERM;
+ if (!bus_type_is_basic(type))
+ return -EINVAL;
+
+ c = message_get_container(m);
+
+ if (c->signature && c->signature[c->index]) {
+ /* Container signature is already set */
+
+ if (c->signature[c->index] != type)
+ return -EINVAL;
+ } else {
+ /* Maybe we can append to the signature? But only if this is the top-level container*/
+ if (c->enclosing != 0)
+ return -EINVAL;
+
+ e = strextend(&c->signature, CHAR_TO_STR(type), NULL);
+ if (!e)
+ return -ENOMEM;
+ }
+
+ nindex = c->index + 1;
+
+ switch (type) {
+
+ case SD_BUS_TYPE_STRING:
+ case SD_BUS_TYPE_OBJECT_PATH:
+ align = 4;
+ sz = 4 + strlen(p) + 1;
+ break;
+
+ case SD_BUS_TYPE_SIGNATURE:
+ align = 1;
+ sz = 1 + strlen(p) + 1;
+ break;
+
+ case SD_BUS_TYPE_BOOLEAN:
+ align = sz = 4;
+
+ assert_cc(sizeof(int) == sizeof(uint32_t));
+ memcpy(&k, p, 4);
+ k = !!k;
+ p = &k;
+ break;
+
+ default:
+ align = bus_type_get_alignment(type);
+ sz = bus_type_get_size(type);
+ break;
+ }
+
+ assert(align > 0);
+ assert(sz > 0);
+
+ a = message_extend_body(m, align, sz);
+ if (!a) {
+ /* Truncate extended signature again */
+ if (e)
+ c->signature[c->index] = 0;
+
+ return -ENOMEM;
+ }
+
+ if (type == SD_BUS_TYPE_STRING || type == SD_BUS_TYPE_OBJECT_PATH) {
+ *(uint32_t*) a = sz - 5;
+ memcpy((uint8_t*) a + 4, p, sz - 4);
+
+ if (stored)
+ *stored = (const uint8_t*) a + 4;
+
+ } else if (type == SD_BUS_TYPE_SIGNATURE) {
+ *(uint8_t*) a = sz - 1;
+ memcpy((uint8_t*) a + 1, p, sz - 1);
+
+ if (stored)
+ *stored = (const uint8_t*) a + 1;
+
+ } else {
+ memcpy(a, p, sz);
+
+ if (stored)
+ *stored = a;
+ }
+
+ if (c->enclosing != SD_BUS_TYPE_ARRAY)
+ c->index = nindex;
+
+ return 0;
+}
+
+int sd_bus_message_append_basic(sd_bus_message *m, char type, const void *p) {
+ return message_append_basic(m, type, p, NULL);
+}
+
+static int bus_message_open_array(
+ sd_bus_message *m,
+ struct bus_container *c,
+ const char *contents,
+ uint32_t **array_size) {
+
+ char *e = NULL;
+ size_t nindex;
+ void *a, *b;
+ int alignment;
+ size_t saved;
+
+ assert(m);
+ assert(c);
+ assert(contents);
+ assert(array_size);
+
+ if (!signature_is_single(contents))
+ return -EINVAL;
+
+ alignment = bus_type_get_alignment(contents[0]);
+ if (alignment < 0)
+ return alignment;
+
+ if (c->signature && c->signature[c->index]) {
+
+ /* Verify the existing signature */
+
+ if (c->signature[c->index] != SD_BUS_TYPE_ARRAY)
+ return -EINVAL;
+
+ if (!startswith(c->signature + c->index + 1, contents))
+ return -EINVAL;
+
+ nindex = c->index + 1 + strlen(contents);
+ } else {
+ if (c->enclosing != 0)
+ return -EINVAL;
+
+ /* Extend the existing signature */
+
+ e = strextend(&c->signature, CHAR_TO_STR(SD_BUS_TYPE_ARRAY), contents, NULL);
+ if (!e)
+ return -ENOMEM;
+
+ nindex = e - c->signature;
+ }
+
+ saved = m->header->body_size;
+ a = message_extend_body(m, 4, 4);
+ if (!a) {
+ /* Truncate extended signature again */
+ if (e)
+ c->signature[c->index] = 0;
+
+ return -ENOMEM;
+ }
+ b = m->body;
+
+ if (!message_extend_body(m, alignment, 0)) {
+ /* Add alignment between size and first element */
+ if (e)
+ c->signature[c->index] = 0;
+
+ m->header->body_size = saved;
+ return -ENOMEM;
+ }
+
+ if (c->enclosing != SD_BUS_TYPE_ARRAY)
+ c->index = nindex;
+
+ /* m->body might have changed so let's readjust a */
+ a = (uint8_t*) m->body + ((uint8_t*) a - (uint8_t*) b);
+ *(uint32_t*) a = 0;
+
+ *array_size = a;
+ return 0;
+}
+
+static int bus_message_open_variant(
+ sd_bus_message *m,
+ struct bus_container *c,
+ const char *contents) {
+
+ char *e = NULL;
+ size_t l, nindex;
+ void *a;
+
+ assert(m);
+ assert(c);
+ assert(contents);
+
+ if (!signature_is_single(contents))
+ return -EINVAL;
+
+ if (*contents == SD_BUS_TYPE_DICT_ENTRY_BEGIN)
+ return -EINVAL;
+
+ if (c->signature && c->signature[c->index]) {
+
+ if (c->signature[c->index] != SD_BUS_TYPE_VARIANT)
+ return -EINVAL;
+
+ } else {
+ if (c->enclosing != 0)
+ return -EINVAL;
+
+ e = strextend(&c->signature, CHAR_TO_STR(SD_BUS_TYPE_VARIANT), NULL);
+ if (!e)
+ return -ENOMEM;
+ }
+
+ nindex = c->index + 1;
+
+ l = strlen(contents);
+ a = message_extend_body(m, 1, 1 + l + 1);
+ if (!a) {
+ /* Truncate extended signature again */
+ if (e)
+ c->signature[c->index] = 0;
+
+ return -ENOMEM;
+ }
+
+ *(uint8_t*) a = l;
+ memcpy((uint8_t*) a + 1, contents, l + 1);
+
+ if (c->enclosing != SD_BUS_TYPE_ARRAY)
+ c->index = nindex;
+
+ return 0;
+}
+
+static int bus_message_open_struct(
+ sd_bus_message *m,
+ struct bus_container *c,
+ const char *contents) {
+
+ size_t nindex;
+ char *e = NULL;
+
+ assert(m);
+ assert(c);
+ assert(contents);
+
+ if (!signature_is_valid(contents, false))
+ return -EINVAL;
+
+ if (c->signature && c->signature[c->index]) {
+ size_t l;
+
+ l = strlen(contents);
+
+ if (c->signature[c->index] != SD_BUS_TYPE_STRUCT_BEGIN ||
+ !startswith(c->signature + c->index + 1, contents) ||
+ c->signature[c->index + 1 + l] != SD_BUS_TYPE_STRUCT_END)
+ return -EINVAL;
+
+ nindex = c->index + 1 + l + 1;
+ } else {
+ if (c->enclosing != 0)
+ return -EINVAL;
+
+ e = strextend(&c->signature, CHAR_TO_STR(SD_BUS_TYPE_STRUCT_BEGIN), contents, CHAR_TO_STR(SD_BUS_TYPE_STRUCT_END), NULL);
+ if (!e)
+ return -ENOMEM;
+
+ nindex = e - c->signature;
+ }
+
+ /* Align contents to 8 byte boundary */
+ if (!message_extend_body(m, 8, 0)) {
+ if (e)
+ c->signature[c->index] = 0;
+
+ return -ENOMEM;
+ }
+
+ if (c->enclosing != SD_BUS_TYPE_ARRAY)
+ c->index = nindex;
+
+ return 0;
+}
+
+static int bus_message_open_dict_entry(
+ sd_bus_message *m,
+ struct bus_container *c,
+ const char *contents) {
+
+ size_t nindex;
+
+ assert(m);
+ assert(c);
+ assert(contents);
+
+ if (!signature_is_pair(contents))
+ return -EINVAL;
+
+ if (c->enclosing != SD_BUS_TYPE_ARRAY)
+ return -EINVAL;
+
+ if (c->signature && c->signature[c->index]) {
+ size_t l;
+
+ l = strlen(contents);
+
+ if (c->signature[c->index] != SD_BUS_TYPE_DICT_ENTRY_BEGIN ||
+ !startswith(c->signature + c->index + 1, contents) ||
+ c->signature[c->index + 1 + l] != SD_BUS_TYPE_DICT_ENTRY_END)
+ return -EINVAL;
+
+ nindex = c->index + 1 + l + 1;
+ } else
+ return -EINVAL;
+
+ /* Align contents to 8 byte boundary */
+ if (!message_extend_body(m, 8, 0))
+ return -ENOMEM;
+
+ if (c->enclosing != SD_BUS_TYPE_ARRAY)
+ c->index = nindex;
+
+ return 0;
+}
+
+int sd_bus_message_open_container(
+ sd_bus_message *m,
+ char type,
+ const char *contents) {
+
+ struct bus_container *c, *sub;
+ char *signature;
+ uint32_t *array_size = NULL;
+ int r;
+
+ if (!m)
+ return -EINVAL;
+ if (m->sealed)
+ return -EPERM;
+ if (!contents)
+ return -EINVAL;
+
+ /* Make sure we have space for one more container */
+ sub = realloc(m->sub_containers, sizeof(struct bus_container) * (m->n_containers + 1));
+ if (!sub)
+ return -ENOMEM;
+
+ m->sub_containers = sub;
+
+ c = message_get_container(m);
+
+ signature = strdup(contents);
+ if (!signature)
+ return -ENOMEM;
+
+ if (type == SD_BUS_TYPE_ARRAY)
+ r = bus_message_open_array(m, c, contents, &array_size);
+ else if (type == SD_BUS_TYPE_VARIANT)
+ r = bus_message_open_variant(m, c, contents);
+ else if (type == SD_BUS_TYPE_STRUCT)
+ r = bus_message_open_struct(m, c, contents);
+ else if (type == SD_BUS_TYPE_DICT_ENTRY)
+ r = bus_message_open_dict_entry(m, c, contents);
+ else
+ r = -EINVAL;
+
+ if (r < 0) {
+ free(signature);
+ return r;
+ }
+
+ /* OK, let's fill it in */
+ sub += m->n_containers++;
+
+ sub->enclosing = type;
+ sub->signature = signature;
+ sub->index = 0;
+ sub->array_size = array_size;
+
+ return 0;
+}
+
+int sd_bus_message_close_container(sd_bus_message *m) {
+ struct bus_container *c;
+
+ if (!m)
+ return -EINVAL;
+ if (m->sealed)
+ return -EPERM;
+ if (m->n_containers <= 0)
+ return -EINVAL;
+
+ c = message_get_container(m);
+
+ if (!c->signature)
+ return -EINVAL;
+
+ if (c->enclosing != SD_BUS_TYPE_ARRAY)
+ if (c->signature[c->index] != 0)
+ return -EINVAL;
+
+ free(c->signature);
+ m->n_containers--;
+
+ return 0;
+}
+
+static int message_append_ap(
+ sd_bus_message *m,
+ const char *types,
+ va_list ap) {
+
+ const char *t;
+ int r;
+
+ assert(m);
+ assert(types);
+
+ for (t = types; *t; t++) {
+ switch (*t) {
+
+ case SD_BUS_TYPE_BYTE: {
+ uint8_t x;
+
+ x = (uint8_t) va_arg(ap, int);
+ r = sd_bus_message_append_basic(m, *t, &x);
+ break;
+ }
+
+ case SD_BUS_TYPE_BOOLEAN:
+ case SD_BUS_TYPE_INT32:
+ case SD_BUS_TYPE_UINT32: {
+ uint32_t x;
+
+ x = va_arg(ap, uint32_t);
+ r = sd_bus_message_append_basic(m, *t, &x);
+ break;
+ }
+
+ case SD_BUS_TYPE_INT16:
+ case SD_BUS_TYPE_UINT16: {
+ uint16_t x;
+
+ x = (uint16_t) va_arg(ap, int);
+ r = sd_bus_message_append_basic(m, *t, &x);
+ break;
+ }
+
+ case SD_BUS_TYPE_INT64:
+ case SD_BUS_TYPE_UINT64:
+ case SD_BUS_TYPE_DOUBLE: {
+ uint64_t x;
+
+ x = va_arg(ap, uint64_t);
+ r = sd_bus_message_append_basic(m, *t, &x);
+ break;
+ }
+
+ case SD_BUS_TYPE_STRING:
+ case SD_BUS_TYPE_OBJECT_PATH:
+ case SD_BUS_TYPE_SIGNATURE: {
+ const char *x;
+
+ x = va_arg(ap, const char*);
+ r = sd_bus_message_append_basic(m, *t, x);
+ break;
+ }
+
+ case SD_BUS_TYPE_UNIX_FD: {
+ int x;
+
+ x = va_arg(ap, int);
+ r = sd_bus_message_append_basic(m, *t, &x);
+ break;
+ }
+
+ case SD_BUS_TYPE_ARRAY: {
+ unsigned i, n;
+ size_t k;
+
+ r = signature_element_length(t + 1, &k);
+ if (r < 0)
+ return r;
+
+ {
+ char s[k + 1];
+
+ memcpy(s, t + 1, k);
+ s[k] = 0;
+ t += k;
+
+ r = sd_bus_message_open_container(m, SD_BUS_TYPE_ARRAY, s);
+ if (r < 0)
+ return r;
+
+ n = va_arg(ap, unsigned);
+
+ for (i = 0; i < n; i++) {
+ r = message_append_ap(m, s, ap);
+ if (r < 0)
+ return r;
+ }
+
+ r = sd_bus_message_close_container(m);
+ }
+
+ break;
+ }
+
+ case SD_BUS_TYPE_VARIANT: {
+ const char *s;
+
+ s = va_arg(ap, const char*);
+ if (!s)
+ return -EINVAL;
+
+ r = sd_bus_message_open_container(m, SD_BUS_TYPE_VARIANT, s);
+ if (r < 0)
+ return r;
+
+ r = message_append_ap(m, s, ap);
+ if (r < 0)
+ return r;
+
+ r = sd_bus_message_close_container(m);
+ break;
+ }
+
+ case SD_BUS_TYPE_STRUCT_BEGIN:
+ case SD_BUS_TYPE_DICT_ENTRY_BEGIN: {
+ size_t k;
+
+ r = signature_element_length(t, &k);
+ if (r < 0)
+ return r;
+
+ {
+ char s[k - 1];
+
+ memcpy(s, t + 1, k - 2);
+ s[k - 2] = 0;
+
+ r = sd_bus_message_open_container(m, *t == SD_BUS_TYPE_STRUCT_BEGIN ? SD_BUS_TYPE_STRUCT : SD_BUS_TYPE_DICT_ENTRY, s);
+ if (r < 0)
+ return r;
+
+ t += k - 1;
+
+ r = message_append_ap(m, s, ap);
+ if (r < 0)
+ return r;
+
+ r = sd_bus_message_close_container(m);
+ }
+
+ break;
+ }
+
+ default:
+ r = -EINVAL;
+ }
+
+ if (r < 0)
+ return r;
+ }
+
+ return 0;
+}
+
+int sd_bus_message_append(sd_bus_message *m, const char *types, ...) {
+ va_list ap;
+ int r;
+
+ if (!m)
+ return -EINVAL;
+ if (m->sealed)
+ return -EPERM;
+ if (!types)
+ return -EINVAL;
+
+ va_start(ap, types);
+ r = message_append_ap(m, types, ap);
+ va_end(ap);
+
+ return r;
+}
+
+int sd_bus_message_read_type(sd_bus_message *m, char *type, char *element, size_t *length) {
+ return -ENOTSUP;
+}
+
+int sd_bus_message_read_basic(sd_bus_message *m, char type, char element, const void **p, size_t *length) {
+ return -ENOTSUP;
+}
+
+int sd_bus_message_read(sd_bus_message *m, const char *types, ...) {
+ return -ENOTSUP;
+}
+
+int message_parse(sd_bus_message *m) {
+ assert(m);
+
+ if (m->header->version != 1)
+ return -EIO;
+
+ if (m->header->endian != SD_BUS_BIG_ENDIAN &&
+ m->header->endian != SD_BUS_LITTLE_ENDIAN)
+ return -EIO;
+
+ return 0;
+}
+
+static void setup_iovec(sd_bus_message *m) {
+ assert(m);
+ assert(m->sealed);
+
+ m->n_iovec = 0;
+
+ m->iovec[m->n_iovec].iov_base = m->header;
+ m->iovec[m->n_iovec].iov_len = sizeof(*m->header);
+ m->n_iovec++;
+
+ if (m->fields) {
+ m->iovec[m->n_iovec].iov_base = m->fields;
+ m->iovec[m->n_iovec].iov_len = m->header->fields_size;
+ m->n_iovec++;
+
+ if (m->header->fields_size % 8 != 0) {
+ static const uint8_t padding[7] = { 0, 0, 0, 0, 0, 0, 0 };
+
+ m->iovec[m->n_iovec].iov_base = (void*) padding;
+ m->iovec[m->n_iovec].iov_len = 8 - m->header->fields_size % 8;
+ m->n_iovec++;
+ }
+ }
+
+ if (m->body) {
+ m->iovec[m->n_iovec].iov_base = m->body;
+ m->iovec[m->n_iovec].iov_len = m->header->body_size;
+ m->n_iovec++;
+ }
+}
+
+int message_seal(sd_bus_message *m, uint64_t serial) {
+ int r;
+
+ assert(m);
+
+ if (m->sealed)
+ return -EPERM;
+
+ if (m->n_containers > 0)
+ return -EBADMSG;
+
+ /* If there's a non-trivial signature set, then add it in here */
+ if (!isempty(m->root_container.signature)) {
+ r = message_append_field_signature(m, SD_BUS_MESSAGE_HEADER_SIGNATURE, m->root_container.signature, &m->signature);
+ if (r < 0)
+ return r;
+ }
+
+ if (m->n_fds > 0) {
+ r = message_append_field_uint32(m, SD_BUS_MESSAGE_HEADER_UNIX_FDS, m->n_fds);
+ if (r < 0)
+ return r;
+ }
+
+ m->header->serial = serial;
+ m->sealed = true;
+
+ setup_iovec(m);
+
+ return 0;
+}
+
+int sd_bus_message_set_destination(sd_bus_message *m, const char *destination) {
+ if (!m)
+ return -EINVAL;
+ if (!destination)
+ return -EINVAL;
+ if (m->sealed)
+ return -EPERM;
+ if (m->destination)
+ return -EEXIST;
+
+ return message_append_field_string(m, SD_BUS_MESSAGE_HEADER_DESTINATION, SD_BUS_TYPE_STRING, destination, &m->destination);
+}
+
+void message_dump(sd_bus_message *m) {
+
+ log_info("Message %p\n"
+ "\tn_ref=%u\n"
+ "\tendian=%c\n"
+ "\ttype=%i\n"
+ "\tflags=%u\n"
+ "\tversion=%u\n"
+ "\tserial=%u\n"
+ "\tfields_size=%u\n"
+ "\tbody_size=%u\n"
+ "\tpath=%s\n"
+ "\tinterface=%s\n"
+ "\tmember=%s\n"
+ "\tdestination=%s\n"
+ "\tsender=%s\n"
+ "\tsignature=%s\n"
+ "\treply_serial=%u\n"
+ "\terror.name=%s\n"
+ "\terror.message=%s\n"
+ "\tsealed=%s\n",
+ m,
+ m->n_ref,
+ m->header->endian,
+ m->header->type,
+ m->header->flags,
+ m->header->version,
+ BUS_MESSAGE_SERIAL(m),
+ BUS_MESSAGE_FIELDS_SIZE(m),
+ BUS_MESSAGE_BODY_SIZE(m),
+ strna(m->path),
+ strna(m->interface),
+ strna(m->member),
+ strna(m->destination),
+ strna(m->sender),
+ strna(m->signature),
+ m->reply_serial,
+ strna(m->error.name),
+ strna(m->error.message),
+ yes_no(m->sealed));
+}
+
+int bus_message_get_blob(sd_bus_message *m, void **buffer, size_t *sz) {
+ size_t total;
+ unsigned i;
+ void *p, *e;
+
+ assert(m);
+ assert(buffer);
+ assert(sz);
+
+ for (i = 0, total = 0; i < m->n_iovec; i++)
+ total += m->iovec[i].iov_len;
+
+ p = malloc(total);
+ if (!p)
+ return -ENOMEM;
+
+ for (i = 0, e = p; i < m->n_iovec; i++)
+ e = mempcpy(e, m->iovec[i].iov_base, m->iovec[i].iov_len);
+
+ *buffer = p;
+ *sz = total;
+
+ return 0;
+}
diff --git a/src/libsystemd-bus/bus-message.h b/src/libsystemd-bus/bus-message.h
new file mode 100644
index 0000000..c62659e
--- /dev/null
+++ b/src/libsystemd-bus/bus-message.h
@@ -0,0 +1,120 @@
+/*-*- 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 <stdbool.h>
+#include <byteswap.h>
+
+#include "macro.h"
+#include "sd-bus.h"
+
+struct bus_container {
+ char enclosing;
+
+ char *signature;
+ unsigned index;
+
+ uint32_t *array_size;
+};
+
+_packed_ struct bus_header {
+ uint8_t endian;
+ uint8_t type;
+ uint8_t flags;
+ uint8_t version;
+ uint32_t body_size;
+ uint32_t serial;
+ uint32_t fields_size;
+};
+
+struct sd_bus_message {
+ unsigned n_ref;
+
+ uint32_t reply_serial;
+
+ const char *path;
+ const char *interface;
+ const char *member;
+ const char *destination;
+ const char *sender;
+ const char *signature;
+
+ sd_bus_error error;
+
+ uid_t uid;
+ gid_t gid;
+ pid_t pid;
+ pid_t tid;
+
+ bool sealed:1;
+ bool uid_valid:1;
+ bool gid_valid:1;
+ bool free_header:1;
+ bool free_fields:1;
+ bool free_body:1;
+
+ struct bus_header *header;
+ void *fields;
+ void *body;
+
+ uint32_t n_fds;
+ int *fds;
+
+ struct bus_container root_container, *sub_containers;
+ unsigned n_containers;
+
+ struct iovec iovec[4];
+ unsigned n_iovec;
+};
+
+#if __BYTE_ORDER == __BIG_ENDIAN
+#define BUS_MESSAGE_NEED_BSWAP(m) ((m)->header->endian != SD_BUS_BIG_ENDIAN)
+#else
+#define BUS_MESSAGE_NEED_BSWAP(m) ((m)->header->endian != SD_BUS_LITTLE_ENDIAN)
+#endif
+
+static inline uint32_t BUS_MESSAGE_BSWAP(sd_bus_message *m, uint32_t u) {
+ return BUS_MESSAGE_NEED_BSWAP(m) ? bswap_32(u) : u;
+}
+
+static inline uint32_t BUS_MESSAGE_SERIAL(sd_bus_message *m) {
+ return BUS_MESSAGE_BSWAP(m, m->header->serial);
+}
+
+static inline uint32_t BUS_MESSAGE_BODY_SIZE(sd_bus_message *m) {
+ return BUS_MESSAGE_BSWAP(m, m->header->body_size);
+}
+
+static inline uint32_t BUS_MESSAGE_FIELDS_SIZE(sd_bus_message *m) {
+ return BUS_MESSAGE_BSWAP(m, m->header->fields_size);
+}
+
+static inline void bus_message_unrefp(sd_bus_message **m) {
+ sd_bus_message_unref(*m);
+}
+
+#define _cleanup_bus_message_unref_ __attribute__((cleanup(bus_message_unrefp)))
+
+int message_parse(sd_bus_message *m);
+int message_seal(sd_bus_message *m, uint64_t serial);
+void message_dump(sd_bus_message *m);
+int bus_message_get_blob(sd_bus_message *m, void **buffer, size_t *sz);
diff --git a/src/libsystemd-bus/bus-signature.c b/src/libsystemd-bus/bus-signature.c
new file mode 100644
index 0000000..db95c88
--- /dev/null
+++ b/src/libsystemd-bus/bus-signature.c
@@ -0,0 +1,141 @@
+/*-*- 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 <util.h>
+
+#include "bus-signature.h"
+#include "bus-type.h"
+
+static int signature_element_length_internal(
+ const char *s,
+ bool allow_dict_entry,
+ size_t *l) {
+
+ int r;
+
+ assert(s);
+
+ if (bus_type_is_basic(*s) || *s == SD_BUS_TYPE_VARIANT) {
+ *l = 1;
+ return 0;
+ }
+
+ if (*s == SD_BUS_TYPE_ARRAY) {
+ size_t t;
+
+ r = signature_element_length_internal(s + 1, true, &t);
+ if (r < 0)
+ return r;
+
+ *l = t + 1;
+ return 0;
+ }
+
+ if (*s == SD_BUS_TYPE_STRUCT_BEGIN) {
+ const char *p = s + 1;
+
+ while (*p != SD_BUS_TYPE_STRUCT_END) {
+ size_t t;
+
+ r = signature_element_length_internal(p, false, &t);
+ if (r < 0)
+ return r;
+
+ p += t;
+ }
+
+ *l = p - s + 1;
+ return 0;
+ }
+
+ if (*s == SD_BUS_TYPE_DICT_ENTRY_BEGIN && allow_dict_entry) {
+ const char *p = s + 1;
+ unsigned n = 0;
+
+ while (*p != SD_BUS_TYPE_DICT_ENTRY_END) {
+ size_t t;
+
+ if (n == 0 && !bus_type_is_basic(*p))
+ return -EINVAL;
+
+ r = signature_element_length_internal(p, false, &t);
+ if (r < 0)
+ return r;
+
+ p += t;
+ n++;
+ }
+
+ if (n != 2)
+ return -EINVAL;
+
+ *l = p - s + 1;
+ return 0;
+ }
+
+ return -EINVAL;
+}
+
+bool signature_is_single(const char *s) {
+ int r;
+ size_t t;
+
+ assert(s);
+
+ r = signature_element_length(s, &t);
+ if (r < 0)
+ return false;
+
+ return s[t] == 0;
+}
+
+bool signature_is_pair(const char *s) {
+ assert(s);
+
+ if (!bus_type_is_basic(*s))
+ return false;
+
+ return signature_is_single(s + 1);
+}
+
+bool signature_is_valid(const char *s, bool allow_dict_entry) {
+ const char *p;
+ int r;
+
+ assert(s);
+
+ p = s;
+ while (*p) {
+ size_t t;
+
+ r = signature_element_length_internal(p, allow_dict_entry, &t);
+ if (r < 0)
+ return false;
+
+ p += t;
+ }
+
+ return p - s <= 255;
+}
+
+int signature_element_length(const char *s, size_t *l) {
+ return signature_element_length_internal(s, true, l);
+}
diff --git a/src/libsystemd-bus/bus-signature.h b/src/libsystemd-bus/bus-signature.h
new file mode 100644
index 0000000..4000e06
--- /dev/null
+++ b/src/libsystemd-bus/bus-signature.h
@@ -0,0 +1,31 @@
+/*-*- 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 <stdbool.h>
+#include <sys/types.h>
+
+bool signature_is_single(const char *s);
+bool signature_is_pair(const char *s);
+bool signature_is_valid(const char *s, bool allow_dict_entry);
+
+int signature_element_length(const char *s, size_t *l);
diff --git a/src/libsystemd-bus/bus-type.c b/src/libsystemd-bus/bus-type.c
new file mode 100644
index 0000000..0557328
--- /dev/null
+++ b/src/libsystemd-bus/bus-type.c
@@ -0,0 +1,163 @@
+/*-*- 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 "util.h"
+#include "bus-type.h"
+
+bool bus_type_is_valid(char c) {
+ static const char valid[] = {
+ SD_BUS_TYPE_BYTE,
+ SD_BUS_TYPE_BOOLEAN,
+ SD_BUS_TYPE_INT16,
+ SD_BUS_TYPE_UINT16,
+ SD_BUS_TYPE_INT32,
+ SD_BUS_TYPE_UINT32,
+ SD_BUS_TYPE_INT64,
+ SD_BUS_TYPE_UINT64,
+ SD_BUS_TYPE_DOUBLE,
+ SD_BUS_TYPE_STRING,
+ SD_BUS_TYPE_OBJECT_PATH,
+ SD_BUS_TYPE_SIGNATURE,
+ SD_BUS_TYPE_ARRAY,
+ SD_BUS_TYPE_VARIANT,
+ SD_BUS_TYPE_STRUCT,
+ SD_BUS_TYPE_DICT_ENTRY,
+ SD_BUS_TYPE_UNIX_FD
+ };
+
+ return !!memchr(valid, c, sizeof(valid));
+}
+
+bool bus_type_is_valid_in_signature(char c) {
+ static const char valid[] = {
+ SD_BUS_TYPE_BYTE,
+ SD_BUS_TYPE_BOOLEAN,
+ SD_BUS_TYPE_INT16,
+ SD_BUS_TYPE_UINT16,
+ SD_BUS_TYPE_INT32,
+ SD_BUS_TYPE_UINT32,
+ SD_BUS_TYPE_INT64,
+ SD_BUS_TYPE_UINT64,
+ SD_BUS_TYPE_DOUBLE,
+ SD_BUS_TYPE_STRING,
+ SD_BUS_TYPE_OBJECT_PATH,
+ SD_BUS_TYPE_SIGNATURE,
+ SD_BUS_TYPE_ARRAY,
+ SD_BUS_TYPE_VARIANT,
+ SD_BUS_TYPE_STRUCT_BEGIN,
+ SD_BUS_TYPE_STRUCT_END,
+ SD_BUS_TYPE_DICT_ENTRY_BEGIN,
+ SD_BUS_TYPE_DICT_ENTRY_END,
+ SD_BUS_TYPE_UNIX_FD
+ };
+
+ return !!memchr(valid, c, sizeof(valid));
+}
+
+bool bus_type_is_basic(char c) {
+ static const char valid[] = {
+ SD_BUS_TYPE_BYTE,
+ SD_BUS_TYPE_BOOLEAN,
+ SD_BUS_TYPE_INT16,
+ SD_BUS_TYPE_UINT16,
+ SD_BUS_TYPE_INT32,
+ SD_BUS_TYPE_UINT32,
+ SD_BUS_TYPE_INT64,
+ SD_BUS_TYPE_UINT64,
+ SD_BUS_TYPE_DOUBLE,
+ SD_BUS_TYPE_STRING,
+ SD_BUS_TYPE_OBJECT_PATH,
+ SD_BUS_TYPE_SIGNATURE,
+ SD_BUS_TYPE_UNIX_FD
+ };
+
+ return !!memchr(valid, c, sizeof(valid));
+}
+
+bool bus_type_is_container(char c) {
+ static const char valid[] = {
+ SD_BUS_TYPE_ARRAY,
+ SD_BUS_TYPE_VARIANT,
+ SD_BUS_TYPE_STRUCT,
+ SD_BUS_TYPE_DICT_ENTRY
+ };
+
+ return !!memchr(valid, c, sizeof(valid));
+}
+
+int bus_type_get_alignment(char c) {
+
+ switch (c) {
+ case SD_BUS_TYPE_BYTE:
+ case SD_BUS_TYPE_SIGNATURE:
+ case SD_BUS_TYPE_VARIANT:
+ return 1;
+
+ case SD_BUS_TYPE_INT16:
+ case SD_BUS_TYPE_UINT16:
+ return 2;
+
+ case SD_BUS_TYPE_BOOLEAN:
+ case SD_BUS_TYPE_INT32:
+ case SD_BUS_TYPE_UINT32:
+ case SD_BUS_TYPE_STRING:
+ case SD_BUS_TYPE_OBJECT_PATH:
+ case SD_BUS_TYPE_ARRAY:
+ case SD_BUS_TYPE_UNIX_FD:
+ return 4;
+
+ case SD_BUS_TYPE_INT64:
+ case SD_BUS_TYPE_UINT64:
+ case SD_BUS_TYPE_DOUBLE:
+ case SD_BUS_TYPE_STRUCT:
+ case SD_BUS_TYPE_STRUCT_BEGIN:
+ case SD_BUS_TYPE_DICT_ENTRY:
+ case SD_BUS_TYPE_DICT_ENTRY_BEGIN:
+ return 8;
+ }
+
+ return -EINVAL;
+}
+
+int bus_type_get_size(char c) {
+
+ switch (c) {
+ case SD_BUS_TYPE_BYTE:
+ return 1;
+
+ case SD_BUS_TYPE_INT16:
+ case SD_BUS_TYPE_UINT16:
+ return 2;
+
+ case SD_BUS_TYPE_BOOLEAN:
+ case SD_BUS_TYPE_INT32:
+ case SD_BUS_TYPE_UINT32:
+ case SD_BUS_TYPE_UNIX_FD:
+ return 4;
+
+ case SD_BUS_TYPE_INT64:
+ case SD_BUS_TYPE_UINT64:
+ case SD_BUS_TYPE_DOUBLE:
+ return 8;
+ }
+
+ return -EINVAL;
+}
diff --git a/src/libsystemd-bus/bus-type.h b/src/libsystemd-bus/bus-type.h
new file mode 100644
index 0000000..e261136
--- /dev/null
+++ b/src/libsystemd-bus/bus-type.h
@@ -0,0 +1,34 @@
+/*-*- 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 <stdbool.h>
+
+#include "sd-bus.h"
+#include "sd-bus-protocol.h"
+
+bool bus_type_is_valid(char c);
+bool bus_type_is_valid_in_signature(char c);
+bool bus_type_is_basic(char c);
+bool bus_type_is_container(char c);
+int bus_type_get_alignment(char c);
+int bus_type_get_size(char c);
diff --git a/src/libsystemd-bus/busctl.c b/src/libsystemd-bus/busctl.c
new file mode 100644
index 0000000..88446fc
--- /dev/null
+++ b/src/libsystemd-bus/busctl.c
@@ -0,0 +1,68 @@
+/*-*- 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 "sd-bus.h"
+
+int main(int argc, char *argv[]) {
+ _cleanup_bus_unref_ sd_bus *bus = NULL;
+ _cleanup_strv_free_ char **l = NULL;
+ char **i;
+ int r;
+
+ r = bus_open_system(&bus);
+ if (r < 0) {
+ log_error("Failed to connect to bus: %s", strerror(-r));
+ goto fail;
+ }
+
+ r = sd_bus_list_names(bus, &l);
+ if (r < 0) {
+ log_error("Failed to list names: %s", strerror(-r));
+ goto fail;
+ }
+
+ STRV_FOREACH(i, l) {
+ _cleanup_free_ char *owner = NULL;
+ pid_t pid = 0;
+ uid_t uid;
+ bool uid_valid;
+
+ r = sd_bus_get_owner(bus, *i, &owner);
+ if (r == -ENXIO)
+ continue;
+
+ r = sd_get_owner_pid(bus, *i, &pid);
+ if (r == -ENXIO)
+ continue;
+
+ r = sd_get_owner_uid(bus, *i, &pid);
+ if (r == -ENXIO)
+ continue;
+ uid_valid = r >= 0;
+
+ printf("%s (%s) %llu %llu\n", *i, owner, (unsigned long long) pid, (unsigned long long) uid);
+ }
+
+ r = 0;
+
+fail:
+ return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
+}
diff --git a/src/libsystemd-bus/sd-bus-protocol.h b/src/libsystemd-bus/sd-bus-protocol.h
new file mode 100644
index 0000000..9d7615f
--- /dev/null
+++ b/src/libsystemd-bus/sd-bus-protocol.h
@@ -0,0 +1,96 @@
+/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
+
+#ifndef foosdbusprotocolhfoo
+#define foosdbusprotocolhfoo
+
+/***
+ 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/>.
+***/
+
+/* Types of message */
+
+#define SD_BUS_DEFAULT_TIMEOUT ((usec_t) (25 * USEC_PER_SEC))
+
+enum {
+ _SD_BUS_MESSAGE_TYPE_INVALID = 0,
+ SD_BUS_MESSAGE_TYPE_METHOD_CALL,
+ SD_BUS_MESSAGE_TYPE_METHOD_RETURN,
+ SD_BUS_MESSAGE_TYPE_METHOD_ERROR,
+ SD_BUS_MESSAGE_TYPE_SIGNAL,
+ _SD_BUS_MESSAGE_TYPE_MAX
+};
+
+/* Primitive types */
+
+enum {
+ _SD_BUS_TYPE_INVALID = 0,
+ SD_BUS_TYPE_BYTE = 'y',
+ SD_BUS_TYPE_BOOLEAN = 'b',
+ SD_BUS_TYPE_INT16 = 'n',
+ SD_BUS_TYPE_UINT16 = 'q',
+ SD_BUS_TYPE_INT32 = 'i',
+ SD_BUS_TYPE_UINT32 = 'u',
+ SD_BUS_TYPE_INT64 = 'x',
+ SD_BUS_TYPE_UINT64 = 't',
+ SD_BUS_TYPE_DOUBLE = 'd',
+ SD_BUS_TYPE_STRING = 's',
+ SD_BUS_TYPE_OBJECT_PATH = 'o',
+ SD_BUS_TYPE_SIGNATURE = 'g',
+ SD_BUS_TYPE_UNIX_FD = 'h',
+ SD_BUS_TYPE_ARRAY = 'a',
+ SD_BUS_TYPE_VARIANT = 'v',
+ SD_BUS_TYPE_STRUCT = 'r', /* not actually used in signatures */
+ SD_BUS_TYPE_STRUCT_BEGIN = '(',
+ SD_BUS_TYPE_STRUCT_END = ')',
+ SD_BUS_TYPE_DICT_ENTRY = 'e', /* not actually used in signatures */
+ SD_BUS_TYPE_DICT_ENTRY_BEGIN = '{',
+ SD_BUS_TYPE_DICT_ENTRY_END = '}',
+};
+
+/* Endianess */
+
+enum {
+ _SD_BUS_INVALID_ENDIAN = 0,
+ SD_BUS_LITTLE_ENDIAN = 'l',
+ SD_BUS_BIG_ENDIAN = 'B'
+};
+
+/* Flags */
+
+enum {
+ SD_BUS_MESSAGE_NO_REPLY_EXPECTED = 1,
+ SD_BUS_MESSAGE_NO_AUTO_START = 2
+};
+
+/* Header fields */
+
+enum {
+ _SD_BUS_MESSAGE_HEADER_INVALID = 0,
+ SD_BUS_MESSAGE_HEADER_PATH,
+ SD_BUS_MESSAGE_HEADER_INTERFACE,
+ SD_BUS_MESSAGE_HEADER_MEMBER,
+ SD_BUS_MESSAGE_HEADER_ERROR_NAME,
+ SD_BUS_MESSAGE_HEADER_REPLY_SERIAL,
+ SD_BUS_MESSAGE_HEADER_DESTINATION,
+ SD_BUS_MESSAGE_HEADER_SENDER,
+ SD_BUS_MESSAGE_HEADER_SIGNATURE,
+ SD_BUS_MESSAGE_HEADER_UNIX_FDS,
+ _SD_BUS_MESSAGE_HEADER_MAX
+};
+
+#endif
diff --git a/src/libsystemd-bus/sd-bus.c b/src/libsystemd-bus/sd-bus.c
new file mode 100644
index 0000000..d80f097
--- /dev/null
+++ b/src/libsystemd-bus/sd-bus.c
@@ -0,0 +1,1437 @@
+/*-*- 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 <endian.h>
+#include <assert.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <netdb.h>
+#include <sys/poll.h>
+#include <byteswap.h>
+
+#include "util.h"
+#include "macro.h"
+
+#include "sd-bus.h"
+#include "bus-internal.h"
+#include "bus-message.h"
+#include "bus-type.h"
+
+#define WQUEUE_MAX 128
+
+static void bus_free(sd_bus *b) {
+ struct filter_callback *f;
+
+ assert(b);
+
+ if (b->fd >= 0)
+ close_nointr_nofail(b->fd);
+
+ free(b->rbuffer);
+ free(b->rqueue);
+ free(b->wqueue);
+ free(b->unique_name);
+
+ hashmap_free_free(b->reply_callbacks);
+
+ while ((f = b->filter_callbacks)) {
+ LIST_REMOVE(struct filter_callback, callbacks, b->filter_callbacks, f);
+ free(f);
+ }
+
+ free(b);
+}
+
+static sd_bus* bus_new(void) {
+ sd_bus *r;
+
+ r = new0(sd_bus, 1);
+ if (!r)
+ return NULL;
+
+ r->n_ref = 1;
+ r->fd = -1;
+ r->message_version = 1;
+
+ /* We guarantee that wqueue always has space for at least one
+ * entry */
+ r->wqueue = new(sd_bus_message*, 1);
+ if (!r->wqueue) {
+ free(r);
+ return NULL;
+ }
+
+ return r;
+};
+
+static int hello_callback(sd_bus *bus, sd_bus_message *reply, void *userdata) {
+ const char *s;
+ int r;
+
+ assert(bus);
+ assert(reply);
+
+ bus->state = BUS_RUNNING;
+
+ r = sd_bus_message_read(reply, "s", &s);
+ if (r < 0)
+ return r;
+
+ bus->unique_name = strdup(s);
+ if (!bus->unique_name)
+ return -ENOMEM;
+
+ return 1;
+}
+
+static int bus_send_hello(sd_bus *bus) {
+ _cleanup_bus_message_unref_ sd_bus_message *m = NULL;
+ int r;
+
+ assert(bus);
+
+ r = sd_bus_message_new_method_call(
+ bus,
+ "org.freedesktop.DBus",
+ "/",
+ "org.freedesktop.DBus",
+ "Hello",
+ &m);
+ if (r < 0)
+ return r;
+
+ r = sd_bus_send_with_reply(bus, m, hello_callback, NULL, (uint64_t) -1, NULL);
+ if (r < 0)
+ return r;
+
+ return 0;
+}
+
+static int bus_start_running(sd_bus *bus) {
+ int r;
+
+ assert(bus);
+
+ if (bus->send_hello) {
+ bus->state = BUS_HELLO;
+
+ r = bus_send_hello(bus);
+ if (r < 0)
+ return r;
+ }
+
+ bus->state = BUS_RUNNING;
+
+ return 0;
+}
+
+static int parse_address_key(const char **p, const char *key, char **value) {
+ size_t l, n = 0;
+ const char *a;
+ char *r = NULL;
+
+ assert(p);
+ assert(*p);
+ assert(key);
+ assert(value);
+
+ l = strlen(key);
+ if (!strncmp(*p, key, l) != 0)
+ return 0;
+
+ if ((*p)[l] != '=')
+ return 0;
+
+ if (*value)
+ return -EINVAL;
+
+ a = *p + l + 1;
+ while (*a != ';' && *a != 0) {
+ char c, *t;
+
+ if (*a == '%') {
+ int x, y;
+
+ x = unhexchar(a[1]);
+ if (x < 0) {
+ free(r);
+ return x;
+ }
+
+ y = unhexchar(a[2]);
+ if (y < 0) {
+ free(r);
+ return y;
+ }
+
+ a += 3;
+ c = (char) ((x << 4) | y);
+ } else
+ c = *a;
+
+ t = realloc(r, n + 1);
+ if (!t) {
+ free(r);
+ return -ENOMEM;
+ }
+
+ r = t;
+ r[n++] = c;
+ }
+
+ *p = a;
+ *value = r;
+ return 1;
+}
+
+static void skip_address_key(const char **p) {
+ assert(p);
+ assert(*p);
+
+ *p += strcspn(*p, ";");
+}
+
+static int bus_parse_next_address(sd_bus *b) {
+ const char *a, *p;
+ _cleanup_free_ char *guid = NULL;
+ int r;
+
+ assert(b);
+
+ if (!b->address)
+ return 0;
+ if (b->address[b->address_index] == 0)
+ return 0;
+
+ a = b->address + b->address_index;
+
+ zero(b->sockaddr);
+ b->sockaddr_size = 0;
+ b->peer = SD_ID128_NULL;
+
+ if (startswith(a, "unix:")) {
+ _cleanup_free_ char *path = NULL, *abstract = NULL;
+
+ p = a + 5;
+ while (*p != 0 && *p != ';') {
+ r = parse_address_key(&p, "guid", &guid);
+ if (r < 0)
+ return r;
+ else if (r > 0)
+ continue;
+
+ r = parse_address_key(&p, "path", &path);
+ if (r < 0)
+ return r;
+ else if (r > 0)
+ continue;
+
+ r = parse_address_key(&p, "abstract", &abstract);
+ if (r < 0)
+ return r;
+ else if (r > 0)
+ continue;
+
+ skip_address_key(&p);
+ }
+
+ if (!path && !abstract)
+ return -EINVAL;
+
+ if (path && abstract)
+ return -EINVAL;
+
+ if (path) {
+ size_t l;
+
+ l = strlen(path);
+ if (l > sizeof(b->sockaddr.un.sun_path))
+ return -E2BIG;
+
+ b->sockaddr.un.sun_family = AF_UNIX;
+ strncpy(b->sockaddr.un.sun_path, path, sizeof(b->sockaddr.un.sun_path));
+ b->sockaddr_size = offsetof(struct sockaddr_un, sun_path) + l;
+ } else if (abstract) {
+ size_t l;
+
+ l = strlen(path);
+ if (l > sizeof(b->sockaddr.un.sun_path) - 1)
+ return -E2BIG;
+
+ b->sockaddr.un.sun_family = AF_UNIX;
+ b->sockaddr.un.sun_path[0] = 0;
+ strncpy(b->sockaddr.un.sun_path+1, path, sizeof(b->sockaddr.un.sun_path)-1);
+ b->sockaddr_size = offsetof(struct sockaddr_un, sun_path) + 1 + l;
+ }
+
+ } else if (startswith(a, "tcp:")) {
+ _cleanup_free_ char *host = NULL, *port = NULL, *family = NULL;
+ struct addrinfo hints, *result;
+
+ p = a + 4;
+ while (*p != 0 && *p != ';') {
+ r = parse_address_key(&p, "guid", &guid);
+ if (r < 0)
+ return r;
+ else if (r > 0)
+ continue;
+
+ r = parse_address_key(&p, "host", &host);
+ if (r < 0)
+ return r;
+ else if (r > 0)
+ continue;
+
+ r = parse_address_key(&p, "port", &port);
+ if (r < 0)
+ return r;
+ else if (r > 0)
+ continue;
+
+ r = parse_address_key(&p, "family", &family);
+ if (r < 0)
+ return r;
+ else if (r > 0)
+ continue;
+
+ skip_address_key(&p);
+ }
+
+ if (!host || !port)
+ return -EINVAL;
+
+ zero(hints);
+ hints.ai_socktype = SOCK_STREAM;
+ hints.ai_flags = AI_ADDRCONFIG;
+
+ if (family) {
+ if (streq(family, "ipv4"))
+ hints.ai_family = AF_INET;
+ else if (streq(family, "ipv6"))
+ hints.ai_family = AF_INET6;
+ else
+ return -EINVAL;
+ }
+
+ r = getaddrinfo(host, port, &hints, &result);
+ if (r == EAI_SYSTEM)
+ return -errno;
+ else if (r != 0)
+ return -EADDRNOTAVAIL;
+
+ memcpy(&b->sockaddr, result->ai_addr, result->ai_addrlen);
+ b->sockaddr_size = result->ai_addrlen;
+
+ freeaddrinfo(result);
+ }
+
+ if (guid) {
+ r = sd_id128_from_string(guid, &b->peer);
+ if (r < 0)
+ return r;
+ }
+
+ b->address_index = p - b->address;
+ return 1;
+}
+
+static void iovec_advance(struct iovec *iov, unsigned *idx, size_t size) {
+
+ while (size > 0) {
+ struct iovec *i = iov + *idx;
+
+ if (i->iov_len > size) {
+ i->iov_base = (uint8_t*) i->iov_base + size;
+ i->iov_len -= size;
+ return;
+ }
+
+ size -= i->iov_len;
+
+ i->iov_base = NULL;
+ i->iov_len = 0;
+
+ (*idx) ++;
+ }
+}
+
+static int bus_write_auth(sd_bus *b) {
+ struct msghdr mh;
+ ssize_t k;
+
+ assert(b);
+ assert(b->state == BUS_AUTHENTICATING);
+
+ if (b->auth_index >= ELEMENTSOF(b->auth_iovec))
+ return 0;
+
+ zero(mh);
+ mh.msg_iov = b->auth_iovec + b->auth_index;
+ mh.msg_iovlen = ELEMENTSOF(b->auth_iovec) - b->auth_index;
+
+ k = sendmsg(b->fd, &mh, MSG_DONTWAIT|MSG_NOSIGNAL);
+ if (k < 0)
+ return errno == EAGAIN ? 0 : -errno;
+
+ iovec_advance(b->auth_iovec, &b->auth_index, (size_t) k);
+
+ return 1;
+}
+
+static int bus_auth_verify(sd_bus *b) {
+ char *e, *f;
+ sd_id128_t peer;
+ unsigned i;
+ int r;
+
+ /* We expect two response lines: "OK", "AGREE_UNIX_FD", and
+ * that's it */
+
+ e = memmem(b->rbuffer, b->rbuffer_size, "\r\n", 2);
+ if (!e)
+ return 0;
+
+ f = memmem(e, b->rbuffer_size - (e - (char*) b->rbuffer), "\r\n", 2);
+ if (!f)
+ return 0;
+
+ if (e - (char*) b->rbuffer != 3 + 32)
+ return -EPERM;
+
+ if (memcmp(b->rbuffer, "OK ", 3))
+ return -EPERM;
+
+ for (i = 0; i < 32; i += 2) {
+ int x, y;
+
+ x = unhexchar(((char*) b->rbuffer)[3 + i]);
+ y = unhexchar(((char*) b->rbuffer)[3 + i + 2]);
+
+ if (x < 0 || y < 0)
+ return -EINVAL;
+
+ peer.bytes[i/2] = ((uint8_t) x << 4 | (uint8_t) y);
+ }
+
+ if (!sd_id128_equal(b->peer, SD_ID128_NULL) &&
+ !sd_id128_equal(b->peer, peer))
+ return -EPERM;
+
+ b->peer = peer;
+
+ b->can_fds =
+ (f - e == sizeof("\r\nAGREE_UNIX_FD") - 1) &&
+ memcmp(e + 2, "AGREE_UNIX_FD", sizeof("AGREE_UNIX_FD") - 1) == 0;
+
+ if (f + 2 > (char*) b->rbuffer + b->rbuffer_size) {
+ b->rbuffer_size -= (f - (char*) b->rbuffer);
+ memmove(b->rbuffer, f + 2, b->rbuffer_size);
+ }
+
+ r = bus_start_running(b);
+ if (r < 0)
+ return r;
+
+ return 1;
+}
+
+static int bus_read_auth(sd_bus *b) {
+ struct msghdr mh;
+ struct iovec iov;
+ size_t n;
+ ssize_t k;
+ int r;
+
+ assert(b);
+
+ r = bus_auth_verify(b);
+ if (r != 0)
+ return r;
+
+ n = MAX(3 + 32 + 2 + sizeof("AGREE_UNIX_FD") - 1 + 2, b->rbuffer_size * 2);
+
+ zero(iov);
+ iov.iov_base = (uint8_t*) b->rbuffer + b->rbuffer_size;
+ iov.iov_len = n - b->rbuffer_size;
+
+ zero(mh);
+ mh.msg_iov = &iov;
+ mh.msg_iovlen = 1;
+
+ k = recvmsg(b->fd, &mh, MSG_DONTWAIT|MSG_NOSIGNAL);
+ if (k < 0)
+ return errno == EAGAIN ? 0 : -errno;
+
+ b->rbuffer_size += k;
+
+ r = bus_auth_verify(b);
+ if (r != 0)
+ return r;
+
+ return 0;
+}
+
+static int bus_start_auth(sd_bus *b) {
+ static const char auth_prefix[] = "\0AUTH_EXTERNAL ";
+ static const char auth_suffix[] = "\r\nNEGOTIATE_UNIX_FD\r\nBEGIN\r\n";
+
+ char text[20 + 1]; /* enough space for a 64bit integer plus NUL */
+ size_t l;
+
+ assert(b);
+
+ b->state = BUS_AUTHENTICATING;
+
+ snprintf(text, sizeof(text), "%llu", (unsigned long long) geteuid());
+ char_array_0(text);
+
+ l = strlen(text);
+ b->auth_uid = hexmem(text, l);
+ if (!b->auth_uid)
+ return -ENOMEM;
+
+ b->auth_iovec[0].iov_base = (void*) auth_prefix;
+ b->auth_iovec[0].iov_len = sizeof(auth_prefix) -1;
+ b->auth_iovec[1].iov_base = (void*) b->auth_uid;
+ b->auth_iovec[1].iov_len = l * 2;
+ b->auth_iovec[2].iov_base = (void*) auth_suffix;
+ b->auth_iovec[2].iov_len = sizeof(auth_suffix) -1;
+ b->auth_size = sizeof(auth_prefix) - 1 + l * 2 + sizeof(auth_suffix) - 1;
+
+ return bus_write_auth(b);
+}
+
+static int bus_start_connect(sd_bus *b) {
+ int r;
+
+ assert(b);
+ assert(b->fd < 0);
+
+ for (;;) {
+ if (b->sockaddr.sa.sa_family == AF_UNSPEC) {
+ r = bus_parse_next_address(b);
+ if (r < 0)
+ return r;
+ if (r == 0)
+ return b->last_connect_error ? b->last_connect_error : -ECONNREFUSED;
+ }
+
+ b->fd = socket(b->sockaddr.sa.sa_family, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0);
+ if (b->fd < 0) {
+ b->last_connect_error = -errno;
+ zero(b->sockaddr);
+ continue;
+ }
+
+ r = connect(b->fd, &b->sockaddr.sa, b->sockaddr_size);
+ if (r < 0) {
+ if (errno == EINPROGRESS)
+ return 0;
+
+ b->last_connect_error = -errno;
+ close_nointr_nofail(b->fd);
+ b->fd = -1;
+ zero(b->sockaddr);
+ continue;
+ }
+
+ return bus_start_auth(b);
+ }
+}
+
+int sd_bus_open_system(sd_bus **ret) {
+ const char *e;
+ sd_bus *b;
+ int r;
+
+ if (!ret)
+ return -EINVAL;
+
+ e = getenv("DBUS_SYSTEM_BUS_ADDRESS");
+ if (e) {
+ r = sd_bus_open_address(e, &b);
+ if (r < 0)
+ return r;
+
+ b->send_hello = true;
+ *ret = b;
+ return r;
+ }
+
+ b = bus_new();
+ if (!b)
+ return -ENOMEM;
+
+ b->send_hello = true;
+
+ b->sockaddr.un.sun_family = AF_UNIX;
+ strncpy(b->sockaddr.un.sun_path, "/run/dbus/system_bus_socket", sizeof(b->sockaddr.un.sun_path));
+ b->sockaddr_size = offsetof(struct sockaddr_un, sun_path) + sizeof("/run/dbus/system_bus_socket") - 1;
+
+ r = bus_start_connect(b);
+ if (r < 0) {
+ bus_free(b);
+ return r;
+ }
+
+ *ret = b;
+ return 0;
+}
+
+int sd_bus_open_user(sd_bus **ret) {
+ const char *e;
+ sd_bus *b;
+ size_t l;
+ int r;
+
+ if (!ret)
+ return -EINVAL;
+
+ e = getenv("DBUS_SESSION_BUS_ADDRESS");
+ if (e) {
+ r = sd_bus_open_address(e, &b);
+ if (r < 0)
+ return r;
+
+ b->send_hello = true;
+ *ret = b;
+ return r;
+ }
+
+ e = getenv("XDG_RUNTIME_DIR");
+ if (!e)
+ return -ENOENT;
+
+ l = strlen(e);
+ if (l + 4 > sizeof(b->sockaddr.un.sun_path))
+ return -E2BIG;
+
+ b = bus_new();
+ if (!b)
+ return -ENOMEM;
+
+ b->send_hello = true;
+
+ b->sockaddr.un.sun_family = AF_UNIX;
+ memcpy(mempcpy(b->sockaddr.un.sun_path, e, l), "/bus", 4);
+ b->sockaddr_size = offsetof(struct sockaddr_un, sun_path) + l + 4;
+
+ r = bus_start_connect(b);
+ if (r < 0) {
+ bus_free(b);
+ return r;
+ }
+
+ *ret = b;
+ return 0;
+}
+
+int sd_bus_open_address(const char *address, sd_bus **ret) {
+ sd_bus *b;
+ int r;
+
+ if (!address)
+ return -EINVAL;
+ if (!ret)
+ return -EINVAL;
+
+ b = bus_new();
+ if (!b)
+ return -ENOMEM;
+
+ b->address = strdup(address);
+ if (!b->address) {
+ bus_free(b);
+ return -ENOMEM;
+ }
+
+ r = bus_start_connect(b);
+ if (r < 0) {
+ bus_free(b);
+ return r;
+ }
+
+ *ret = b;
+ return 0;
+}
+
+int sd_bus_open_fd(int fd, sd_bus **ret) {
+ sd_bus *b;
+ int r;
+
+ if (fd < 0)
+ return -EINVAL;
+ if (!ret)
+ return -EINVAL;
+
+ b = bus_new();
+ if (!b)
+ return -ENOMEM;
+
+ b->fd = fd;
+ fd_nonblock(b->fd, true);
+ fd_cloexec(b->fd, true);
+
+ r = bus_start_auth(b);
+ if (r < 0) {
+ bus_free(b);
+ return r;
+ }
+
+ *ret = b;
+ return 0;
+}
+
+void sd_bus_close(sd_bus *bus) {
+ if (!bus)
+ return;
+ if (bus->fd < 0)
+ return;
+
+ close_nointr_nofail(bus->fd);
+ bus->fd = -1;
+}
+
+sd_bus *sd_bus_ref(sd_bus *bus) {
+ if (!bus)
+ return NULL;
+
+ assert(bus->n_ref > 0);
+
+ bus->n_ref++;
+ return bus;
+}
+
+sd_bus *sd_bus_unref(sd_bus *bus) {
+ if (!bus)
+ return NULL;
+
+ assert(bus->n_ref > 0);
+ bus->n_ref--;
+
+ if (bus->n_ref <= 0)
+ bus_free(bus);
+
+ return NULL;
+}
+
+int sd_bus_is_running(sd_bus *bus) {
+ if (!bus)
+ return -EINVAL;
+
+ if (bus->fd < 0)
+ return -ENOTCONN;
+
+ return bus->state == BUS_RUNNING;
+}
+
+int sd_bus_can_send(sd_bus *bus, char type) {
+
+ if (!bus)
+ return -EINVAL;
+
+ if (type == SD_BUS_TYPE_UNIX_FD)
+ return bus->can_fds;
+
+ return bus_type_is_valid(type);
+}
+
+static int bus_seal_message(sd_bus *b, sd_bus_message *m) {
+ assert(m);
+
+ if (m->sealed)
+ return 0;
+
+ return message_seal(m, ++b->serial);
+}
+
+static int message_write(sd_bus *bus, sd_bus_message *m, size_t *idx) {
+ struct msghdr mh;
+ struct iovec *iov;
+ ssize_t k;
+ size_t n;
+ unsigned j;
+
+ assert(bus);
+ assert(m);
+ assert(idx);
+
+ n = m->n_iovec * sizeof(struct iovec);
+ iov = alloca(n);
+ memcpy(iov, m->iovec, n);
+
+ j = 0;
+ iovec_advance(iov, &j, *idx);
+
+ zero(mh);
+ mh.msg_iov = iov;
+ mh.msg_iovlen = m->n_iovec;
+
+ k = sendmsg(bus->fd, &mh, MSG_DONTWAIT|MSG_NOSIGNAL);
+ if (k < 0)
+ return -errno;
+
+ *idx += (size_t) k;
+ iovec_advance(iov, &j, *idx);
+
+ return j > m->n_iovec;
+}
+
+static int message_read_need(sd_bus *bus, size_t *need) {
+ uint32_t a, b;
+ uint8_t e;
+
+ assert(bus);
+ assert(need);
+
+ if (bus->rbuffer_size <= sizeof(struct bus_header)) {
+ *need = sizeof(struct bus_header);
+ return 0;
+ }
+
+ a = ((const uint32_t*) bus->rbuffer)[1];
+ b = ((const uint32_t*) bus->rbuffer)[3];
+
+ e = ((const uint8_t*) bus->rbuffer)[0];
+ if (e == SD_BUS_LITTLE_ENDIAN) {
+ a = le32toh(a);
+ b = le32toh(b);
+ } else if (e == SD_BUS_BIG_ENDIAN) {
+ a = be32toh(a);
+ b = be32toh(b);
+ } else
+ return -EIO;
+
+ *need = sizeof(struct bus_header) + ALIGN_TO(a, 8) + b;
+ return 0;
+}
+
+static int message_make(sd_bus *bus, size_t size, sd_bus_message **m) {
+ sd_bus_message *t;
+ void *b = NULL;
+ int r;
+
+ assert(bus);
+ assert(m);
+ assert(bus->rbuffer_size >= size);
+
+ t = new0(sd_bus_message, 1);
+ if (!t)
+ return -ENOMEM;
+
+ if (bus->rbuffer_size > size) {
+ b = memdup((const uint8_t*) bus->rbuffer + size, bus->rbuffer_size - size);
+ if (!b) {
+ free(t);
+ return -ENOMEM;
+ }
+ }
+
+ t->n_ref = 1;
+
+ t->header = bus->rbuffer;
+ t->free_header = true;
+
+ t->fields = (uint8_t*) bus->rbuffer + sizeof(struct bus_header);
+ t->body = (uint8_t*) bus->rbuffer + sizeof(struct bus_header) + ALIGN_TO(BUS_MESSAGE_BODY_SIZE(t), 8);
+
+ bus->rbuffer = b;
+ bus->rbuffer_size -= size;
+
+ r = message_parse(t);
+ if (r < 0) {
+ sd_bus_message_unref(t);
+ return r;
+ }
+
+ *m = t;
+ return 1;
+}
+
+static int message_read(sd_bus *bus, sd_bus_message **m) {
+ struct msghdr mh;
+ struct iovec iov;
+ ssize_t k;
+ size_t need;
+ int r;
+ void *b;
+
+ assert(bus);
+ assert(m);
+
+ r = message_read_need(bus, &need);
+ if (r < 0)
+ return r;
+
+ if (bus->rbuffer_size >= need)
+ return message_make(bus, need, m);
+
+ b = realloc(bus->rbuffer, need);
+ if (!b)
+ return -ENOMEM;
+
+ zero(iov);
+ iov.iov_base = (uint8_t*) bus->rbuffer + bus->rbuffer_size;
+ iov.iov_len = need - bus->rbuffer_size;
+
+ zero(mh);
+ mh.msg_iov = &iov;
+ mh.msg_iovlen = 1;
+
+ k = recvmsg(bus->fd, &mh, MSG_DONTWAIT|MSG_NOSIGNAL);
+ if (k < 0)
+ return errno == EAGAIN ? 0 : -errno;
+
+ bus->rbuffer_size += k;
+
+ r = message_read_need(bus, &need);
+ if (r < 0)
+ return r;
+
+ if (bus->rbuffer_size >= need)
+ return message_make(bus, need, m);
+
+ return 0;
+}
+
+static int dispatch_wqueue(sd_bus *bus) {
+ int r, c = 0;
+
+ assert(bus);
+
+ if (bus->fd < 0)
+ return -ENOTCONN;
+
+ while (bus->wqueue_size > 0) {
+
+ r = message_write(bus, bus->wqueue[0], &bus->windex);
+ if (r < 0) {
+ sd_bus_close(bus);
+ return r;
+ } else if (r == 0)
+ /* Wasn't fully written yet... */
+ break;
+ else {
+ /* Fully written. Let's drop the entry from
+ * the queue.
+ *
+ * This isn't particularly optimized, but
+ * well, this is supposed to be our worst-case
+ * buffer only, and the socket buffer is
+ * supposed to be our primary buffer, and if
+ * it got full, then all bets are off
+ * anyway. */
+
+ sd_bus_message_unref(bus->wqueue[0]);
+ bus->wqueue_size --;
+ memmove(bus->wqueue, bus->wqueue + 1, sizeof(sd_bus_message*) * bus->wqueue_size);
+ bus->windex = 0;
+
+ c++;
+ }
+ }
+
+ return c;
+}
+
+static int dispatch_rqueue(sd_bus *bus, sd_bus_message **m) {
+ int r;
+
+ assert(bus);
+ assert(m);
+
+ if (bus->fd < 0)
+ return -ENOTCONN;
+
+ if (bus->rqueue_size > 0) {
+ /* Dispatch a queued message */
+
+ *m = bus->rqueue[0];
+ bus->rqueue_size --;
+ memmove(bus->rqueue, bus->rqueue + 1, sizeof(sd_bus_message*) * bus->rqueue_size);
+ return 1;
+ }
+
+ /* Try to read a new message */
+ r = message_read(bus, m);
+ if (r < 0) {
+ sd_bus_close(bus);
+ return r;
+ }
+
+ return r;
+}
+
+int sd_bus_send(sd_bus *bus, sd_bus_message *m, uint64_t *serial) {
+ int r;
+
+ if (!bus)
+ return -EINVAL;
+ if (bus->fd < 0)
+ return -ENOTCONN;
+ if (!m)
+ return -EINVAL;
+ if (m->header->version > bus->message_version)
+ return -EPERM;
+
+ r = bus_seal_message(bus, m);
+ if (r < 0)
+ return r;
+
+ if (bus->wqueue_size <= 0) {
+ size_t idx = 0;
+
+ r = message_write(bus, m, &idx);
+ if (r < 0) {
+ sd_bus_close(bus);
+ return r;
+ } else if (r == 0) {
+ /* Wasn't fully written. So let's remember how
+ * much was written. Note that the first entry
+ * of the wqueue array is always allocated so
+ * that we always can remember how much was
+ * written. */
+ bus->wqueue[0] = sd_bus_message_ref(m);
+ bus->wqueue_size = 1;
+ bus->windex = idx;
+ }
+ } else {
+ sd_bus_message **q;
+
+ /* Just append it to the queue. */
+
+ if (bus->wqueue_size >= WQUEUE_MAX)
+ return -ENOBUFS;
+
+ q = realloc(bus->wqueue, sizeof(sd_bus_message*) * (bus->wqueue_size + 1));
+ if (!q)
+ return -ENOMEM;
+
+ bus->wqueue = q;
+ q[bus->wqueue_size ++] = sd_bus_message_ref(m);
+ }
+
+ if (serial)
+ *serial = BUS_MESSAGE_SERIAL(m);
+
+ return 0;
+}
+
+static usec_t calc_elapse(uint64_t usec) {
+ if (usec == (uint64_t) -1)
+ return 0;
+
+ if (usec == 0)
+ usec = SD_BUS_DEFAULT_TIMEOUT;
+
+ return now(CLOCK_MONOTONIC) + usec;
+}
+
+int sd_bus_send_with_reply(
+ sd_bus *bus,
+ sd_bus_message *m,
+ sd_message_handler_t callback,
+ void *userdata,
+ uint64_t usec,
+ uint64_t *serial) {
+
+ struct reply_callback *c;
+ int r;
+
+ if (!bus)
+ return -EINVAL;
+ if (!bus->fd < 0)
+ return -ENOTCONN;
+ if (!m)
+ return -EINVAL;
+ if (!callback)
+ return -EINVAL;
+ if (!m->header->type != SD_BUS_MESSAGE_TYPE_METHOD_CALL)
+ return -EINVAL;
+
+ r = bus_seal_message(bus, m);
+ if (r < 0)
+ return r;
+
+ c = new(struct reply_callback, 1);
+ if (!c)
+ return -ENOMEM;
+
+ c->callback = callback;
+ c->userdata = userdata;
+ c->serial = BUS_MESSAGE_SERIAL(m);
+ c->timeout = calc_elapse(usec);
+
+ r = hashmap_put(bus->reply_callbacks, &c->serial, c);
+ if (r < 0) {
+ free(c);
+ return r;
+ }
+
+ r = sd_bus_send(bus, m, serial);
+ if (r < 0) {
+ hashmap_remove(bus->reply_callbacks, &c->serial);
+ free(c);
+ return r;
+ }
+
+ return r;
+}
+
+int sd_bus_send_with_reply_cancel(sd_bus *bus, uint64_t serial) {
+ struct reply_callbacks *c;
+
+ if (!bus)
+ return -EINVAL;
+ if (serial == 0)
+ return -EINVAL;
+
+ c = hashmap_remove(bus->reply_callbacks, &serial);
+ if (!c)
+ return 0;
+
+ free(c);
+ return 1;
+}
+
+int sd_bus_send_with_reply_and_block(
+ sd_bus *bus,
+ sd_bus_message *m,
+ uint64_t usec,
+ sd_bus_error *error,
+ sd_bus_message **reply) {
+
+ int r;
+ usec_t timeout;
+ uint64_t serial;
+ bool room = false;
+
+ if (!bus)
+ return -EINVAL;
+ if (!bus->fd < 0)
+ return -ENOTCONN;
+ if (!m)
+ return -EINVAL;
+ if (!m->header->type != SD_BUS_MESSAGE_TYPE_METHOD_CALL)
+ return -EINVAL;
+ if (sd_bus_error_is_set(error))
+ return -EINVAL;
+
+ r = sd_bus_send(bus, m, &serial);
+ if (r < 0)
+ return r;
+
+ timeout = calc_elapse(usec);
+
+ for (;;) {
+ usec_t left;
+ sd_bus_message *incoming;
+
+ if (!room) {
+ sd_bus_message **q;
+
+ /* Make sure there's room for queuing this
+ * locally, before we read the message */
+
+ q = realloc(bus->rqueue, (bus->rqueue_size + 1) * sizeof(sd_bus_message*));
+ if (!q)
+ return -ENOMEM;
+
+ bus->rqueue = q;
+ room = true;
+ }
+
+ r = message_read(bus, &incoming);
+ if (r < 0)
+ return r;
+ if (r > 0) {
+ if (incoming->reply_serial == serial) {
+ /* Found a match! */
+
+ if (incoming->header->type == SD_BUS_MESSAGE_TYPE_METHOD_RETURN) {
+ *reply = incoming;
+ return 0;
+ }
+
+ if (incoming->header->type == SD_BUS_MESSAGE_TYPE_METHOD_ERROR) {
+ int k;
+
+ r = sd_bus_error_copy(error, &incoming->error);
+ if (r < 0) {
+ sd_bus_message_unref(incoming);
+ return r;
+ }
+
+ k = bus_error_to_errno(&incoming->error);
+ sd_bus_message_unref(incoming);
+ return k;
+ }
+
+ sd_bus_message_unref(incoming);
+ return -EIO;
+ }
+
+ /* There's already guaranteed to be room for
+ * this, so need to resize things here */
+ bus->rqueue[bus->rqueue_size ++] = incoming;
+ room = false;
+
+ /* Try to read more, right-away */
+ continue;
+ }
+
+ if (timeout > 0) {
+ usec_t n;
+
+ n = now(CLOCK_MONOTONIC);
+ if (n >= timeout)
+ return -ETIMEDOUT;
+
+ left = timeout - n;
+ } else
+ left = (uint64_t) -1;
+
+ r = sd_bus_wait(bus, left);
+ if (r < 0)
+ return r;
+
+ r = dispatch_wqueue(bus);
+ if (r < 0)
+ return r;
+ }
+}
+
+int sd_bus_get_fd(sd_bus *bus) {
+ if (!bus)
+ return -EINVAL;
+
+ if (bus->fd < 0)
+ return -EINVAL;
+
+ return bus->fd;
+}
+
+int sd_bus_get_events(sd_bus *bus) {
+ int flags = 0;
+
+ if (!bus)
+ return -EINVAL;
+
+ if (bus->fd < 0)
+ return -EINVAL;
+
+ if (bus->state == BUS_OPENING)
+ flags |= POLLOUT;
+ else if (bus->state == BUS_RUNNING || bus->state == BUS_HELLO) {
+ if (bus->rqueue_size <= 0)
+ flags |= POLLIN;
+ if (bus->wqueue_size > 0)
+ flags |= POLLOUT;
+ }
+
+ return flags;
+}
+
+int sd_bus_process(sd_bus *bus, sd_bus_message **ret) {
+ sd_bus_message *m;
+ int r;
+
+ if (!bus)
+ return -EINVAL;
+ if (bus->fd < 0)
+ return -ENOTCONN;
+
+ if (bus->state == BUS_OPENING) {
+ struct pollfd p;
+
+ zero(p);
+ p.fd = bus->fd;
+ p.events = POLLOUT;
+
+ r = poll(&p, 1, 0);
+ if (r < 0)
+ return -errno;
+
+ if (p.revents & (POLLOUT|POLLERR|POLLHUP)) {
+ int error;
+ socklen_t slen = sizeof(error);
+
+ r = getsockopt(bus->fd, SOL_SOCKET, SO_ERROR, &error, &slen);
+ if (r < 0)
+ return -errno;
+
+ if (error != 0)
+ bus->last_connect_error = -error;
+ else if (p.revents & (POLLERR|POLLHUP))
+ bus->last_connect_error = -ECONNREFUSED;
+ else
+ return bus_start_auth(bus);
+
+ /* Try next address */
+ return bus_start_connect(bus);
+ }
+
+ return 0;
+
+ } else if (bus->state == BUS_AUTHENTICATING) {
+
+ r = bus_write_auth(bus);
+ if (r < 0)
+ return r;
+
+ r = bus_read_auth(bus);
+ if (r <= 0)
+ return r;
+
+ return bus_start_running(bus);
+
+ } else if (bus->state == BUS_RUNNING || bus->state == BUS_HELLO) {
+ struct filter_callback *l;
+
+ r = dispatch_wqueue(bus);
+ if (r < 0)
+ return r;
+
+ r = dispatch_rqueue(bus, &m);
+ if (r <= 0)
+ return r;
+
+ if (m->header->type == SD_BUS_MESSAGE_TYPE_METHOD_CALL || m->header->type == SD_BUS_MESSAGE_TYPE_METHOD_RETURN) {
+ struct reply_callback *c;
+
+ c = hashmap_remove(bus->reply_callbacks, &m->reply_serial);
+ if (c) {
+ r = c->callback(bus, m, c->userdata);
+ free(c);
+
+ if (r != 0) {
+ sd_bus_message_unref(m);
+ return r < 0 ? r : 0;
+ }
+ }
+ }
+
+ LIST_FOREACH(callbacks, l, bus->filter_callbacks) {
+ r = l->callback(bus, m, l->userdata);
+ if (r != 0) {
+ sd_bus_message_unref(m);
+ return r < 0 ? r : 0;
+ }
+ }
+
+ if (ret) {
+ *ret = m;
+ return 1;
+ }
+
+ sd_bus_message_unref(m);
+ return 0;
+ }
+
+ return -ENOTSUP;
+}
+
+int sd_bus_wait(sd_bus *bus, uint64_t timeout_usec) {
+ struct pollfd p;
+ int r, e;
+ struct timespec ts;
+
+ if (!bus)
+ return -EINVAL;
+ if (bus->fd < 0)
+ return -ECONNREFUSED;
+
+ e = sd_bus_get_events(bus);
+ if (e < 0)
+ return e;
+
+ zero(p);
+ p.fd = bus->fd;
+ p.events = e;
+
+ r = ppoll(&p, 1, timeout_usec == (uint64_t) -1 ? NULL : timespec_store(&ts, timeout_usec), NULL);
+ if (r < 0)
+ return -EINVAL;
+
+ return r;
+}
+
+int sd_bus_flush(sd_bus *bus) {
+ int r;
+
+ if (!bus)
+ return -EINVAL;
+ if (bus->fd < 0)
+ return -ENOTCONN;
+
+ if (bus->state == BUS_RUNNING && bus->wqueue_size <= 0)
+ return 0;
+
+ for (;;) {
+ r = dispatch_wqueue(bus);
+ if (r < 0)
+ return r;
+
+ if (bus->state == BUS_RUNNING && bus->wqueue_size <= 0)
+ return 0;
+
+ r = sd_bus_wait(bus, (uint64_t) -1);
+ if (r < 0)
+ return r;
+ }
+}
+
+int sd_bus_add_filter(sd_bus *bus, sd_message_handler_t callback, void *userdata) {
+ struct filter_callback *f;
+
+ if (!bus)
+ return -EINVAL;
+ if (!callback)
+ return -EINVAL;
+
+ f = new(struct filter_callback, 1);
+ if (!f)
+ return -ENOMEM;
+ f->callback = callback;
+ f->userdata = userdata;
+
+ LIST_PREPEND(struct filter_callback, callbacks, bus->filter_callbacks, f);
+ return 0;
+}
+
+int sd_bus_remove_filter(sd_bus *bus, sd_message_handler_t callback, void *userdata) {
+ struct filter_callback *f;
+
+ if (!bus)
+ return -EINVAL;
+ if (!callback)
+ return -EINVAL;
+
+ LIST_FOREACH(callbacks, f, bus->filter_callbacks) {
+ if (f->callback == callback && f->userdata == userdata) {
+ LIST_REMOVE(struct filter_callback, callbacks, bus->filter_callbacks, f);
+ free(f);
+ return 1;
+ }
+ }
+
+ return 0;
+}
diff --git a/src/libsystemd-bus/sd-bus.h b/src/libsystemd-bus/sd-bus.h
new file mode 100644
index 0000000..6d53d5a
--- /dev/null
+++ b/src/libsystemd-bus/sd-bus.h
@@ -0,0 +1,135 @@
+/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
+
+#ifndef foosdbushfoo
+#define foosdbushfoo
+
+/***
+ 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 <inttypes.h>
+#include <sys/types.h>
+
+#include "sd-bus-protocol.h"
+
+typedef struct sd_bus sd_bus;
+typedef struct sd_bus_message sd_bus_message;
+
+typedef struct {
+ const char *name;
+ const char *message;
+ int need_free;
+} sd_bus_error;
+
+typedef int (*sd_message_handler_t)(sd_bus *bus, sd_bus_message *m, void *userdata);
+
+/* Connections */
+
+int sd_bus_open_system(sd_bus **ret);
+int sd_bus_open_user(sd_bus **ret);
+int sd_bus_open_address(const char *address, sd_bus **ret);
+int sd_bus_open_fd(int fd, sd_bus **ret);
+void sd_bus_close(sd_bus *bus);
+
+sd_bus *sd_bus_ref(sd_bus *bus);
+sd_bus *sd_bus_unref(sd_bus *bus);
+
+int sd_bus_is_running(sd_bus *bus);
+int sd_bus_can_send(sd_bus *bus, char type);
+
+int sd_bus_send(sd_bus *bus, sd_bus_message *m, uint64_t *serial);
+int sd_bus_send_with_reply(sd_bus *bus, sd_bus_message *m, sd_message_handler_t callback, void *userdata, uint64_t usec, uint64_t *serial);
+int sd_bus_send_with_reply_cancel(sd_bus *bus, uint64_t serial);
+int sd_bus_send_with_reply_and_block(sd_bus *bus, sd_bus_message *m, uint64_t usec, sd_bus_error *error, sd_bus_message **r);
+
+int sd_bus_get_fd(sd_bus *bus);
+int sd_bus_get_events(sd_bus *bus);
+int sd_bus_process(sd_bus *bus, sd_bus_message **r);
+int sd_bus_wait(sd_bus *bus, uint64_t timeout_usec);
+int sd_bus_flush(sd_bus *bus);
+
+int sd_bus_add_filter(sd_bus *bus, sd_message_handler_t callback, void *userdata);
+int sd_bus_remove_filter(sd_bus *bus, sd_message_handler_t callback, void *userdata);
+
+/* Message object */
+
+int sd_bus_message_new_signal(sd_bus *bus, const char *path, const char *interface, const char *member, sd_bus_message **m);
+int sd_bus_message_new_method_call(sd_bus *bus, const char *destination, const char *path, const char *interface, const char *member, sd_bus_message **m);
+int sd_bus_message_new_method_return(sd_bus *bus, sd_bus_message *call, sd_bus_message **m);
+int sd_bus_message_new_method_error(sd_bus *bus, sd_bus_message *call, const sd_bus_error *e, sd_bus_message **m);
+
+sd_bus_message* sd_bus_message_ref(sd_bus_message *m);
+sd_bus_message* sd_bus_message_unref(sd_bus_message *m);
+
+int sd_bus_message_get_type(sd_bus_message *m, uint8_t *type);
+int sd_bus_message_get_serial(sd_bus_message *m, uint64_t *serial);
+int sd_bus_message_get_reply_serial(sd_bus_message *m, uint64_t *serial);
+int sd_bus_message_get_no_reply(sd_bus_message *m);
+
+const char *sd_bus_message_get_path(sd_bus_message *m);
+const char *sd_bus_message_get_interface(sd_bus_message *m);
+const char *sd_bus_message_get_member(sd_bus_message *m);
+const char *sd_bus_message_get_destination(sd_bus_message *m);
+const char *sd_bus_message_get_sender(sd_bus_message *m);
+const sd_bus_error *sd_bus_message_get_error(sd_bus_message *m);
+
+int sd_bus_message_get_uid(sd_bus_message *m, uid_t *uid);
+int sd_bus_message_get_gid(sd_bus_message *m, gid_t *gid);
+int sd_bus_message_get_pid(sd_bus_message *m, pid_t *pid);
+int sd_bus_message_get_tid(sd_bus_message *m, pid_t *tid);
+
+int sd_bus_message_is_signal(sd_bus_message *m, const char *interface, const char *member);
+int sd_bus_message_is_method_call(sd_bus_message *m, const char *interface, const char *member);
+int sd_bus_message_is_method_error(sd_bus_message *m, const char *name);
+
+int sd_bus_message_set_no_reply(sd_bus_message *m, int b);
+int sd_bus_message_set_destination(sd_bus_message *m, const char *destination);
+
+int sd_bus_message_append(sd_bus_message *m, const char *types, ...);
+int sd_bus_message_append_basic(sd_bus_message *m, char type, const void *p);
+int sd_bus_message_open_container(sd_bus_message *m, char type, const char *contents);
+int sd_bus_message_close_container(sd_bus_message *m);
+
+int sd_bus_message_read_type(sd_bus_message *m, char *type, char *element, size_t *length);
+int sd_bus_message_read_basic(sd_bus_message *m, char type, char element, const void **p, size_t *length);
+int sd_bus_message_read(sd_bus_message *m, const char *types, ...);
+
+/* Bus management */
+
+const char *sd_bus_get_unique_name(sd_bus *bus);
+int sd_bus_request_name(sd_bus *bus, const char *name, int flags);
+int sd_bus_release_name(sd_bus *bus, const char *name);
+int sd_bus_list_names(sd_bus *bus, char ***l);
+int sd_bus_get_owner(sd_bus *bus, const char *name, char **owner);
+int sd_bus_get_owner_uid(sd_bus *bus, const char *name, uid_t *uid);
+int sd_bus_get_owner_pid(sd_bus *bus, const char *name, pid_t *pid);
+int sd_bus_add_match(sd_bus *bus, const char *match);
+int sd_bus_remove_match(sd_bus *bus, const char *match);
+
+/* Error objects */
+
+#define SD_BUS_ERROR_INIT (NULL, NULL, false)
+
+void sd_bus_error_free(sd_bus_error *e);
+int sd_bus_error_set(sd_bus_error *e, const char *name, const char *format, ...);
+void sd_bus_error_set_const(sd_bus_error *e, const char *name, const char *message);
+int sd_bus_error_copy(sd_bus_error *dest, const sd_bus_error *e);
+int sd_bus_error_is_set(const sd_bus_error *e);
+int sd_bus_error_has_name(const sd_bus_error *e, const char *name);
+
+#endif
diff --git a/src/libsystemd-bus/test-bus-marshal.c b/src/libsystemd-bus/test-bus-marshal.c
new file mode 100644
index 0000000..08364ef
--- /dev/null
+++ b/src/libsystemd-bus/test-bus-marshal.c
@@ -0,0 +1,129 @@
+/*-*- 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 <assert.h>
+#include <stdlib.h>
+#include <byteswap.h>
+
+#ifdef HAVE_GLIB
+#include <gio/gio.h>
+#endif
+
+#include <dbus.h>
+
+#include "log.h"
+#include "util.h"
+
+#include "sd-bus.h"
+#include "bus-message.h"
+
+int main(int argc, char *argv[]) {
+ _cleanup_bus_message_unref_ sd_bus_message *m = NULL;
+ int r;
+ const char *x, *y, *z, *a, *b, *c;
+ uint8_t u, v;
+ void *buffer = NULL;
+ size_t sz;
+ char *h;
+
+ r = sd_bus_message_new_method_call(NULL, "foobar.waldo", "/", "foobar.waldo", "Piep", &m);
+ assert_se(r >= 0);
+
+ r = sd_bus_message_append(m, "s", "a string");
+ assert_se(r >= 0);
+
+ r = sd_bus_message_append(m, "as", 2, "string #1", "string #2");
+ assert_se(r >= 0);
+
+ r = sd_bus_message_append(m, "sass", "foobar", 5, "foo", "bar", "waldo", "piep", "pap", "after");
+ assert_se(r >= 0);
+
+ r = sd_bus_message_append(m, "a{yv}", 2, 3, "s", "foo", 5, "s", "waldo");
+ assert_se(r >= 0);
+
+ r = sd_bus_message_append(m, "ba(ss)", 255, 3, "aaa", "1", "bbb", "2", "ccc", "3");
+ assert_se(r >= 0);
+
+ r = sd_bus_message_open_container(m, 'a', "s");
+ assert_se(r >= 0);
+
+ r = sd_bus_message_append_basic(m, 's', "foobar");
+ assert_se(r >= 0);
+
+ r = sd_bus_message_append_basic(m, 's', "waldo");
+ assert_se(r >= 0);
+
+ r = sd_bus_message_close_container(m);
+ assert_se(r >= 0);
+
+ r = message_seal(m, 4711);
+ assert_se(r >= 0);
+
+ message_dump(m);
+
+ r = bus_message_get_blob(m, &buffer, &sz);
+ assert_se(r >= 0);
+
+ h = hexmem(buffer, sz);
+ assert_se(h);
+
+ log_info("message size = %lu, contents =\n%s", (unsigned long) sz, h);
+ free(h);
+
+#ifdef HAVE_GLIB
+ {
+ GDBusMessage *g;
+ char *p;
+
+ g_type_init();
+
+ g = g_dbus_message_new_from_blob(buffer, sz, 0, NULL);
+ p = g_dbus_message_print(g, 0);
+ log_info("%s", p);
+ g_free(p);
+ g_object_unref(g);
+ }
+#endif
+
+ {
+ DBusMessage *w;
+ DBusError error;
+
+ dbus_error_init(&error);
+
+ w = dbus_message_demarshal(buffer, sz, &error);
+ if (!w) {
+ log_error("%s", error.message);
+ } else
+ dbus_message_unref(w);
+ }
+
+ free(buffer);
+
+ /* r = sd_bus_message_read(m, "sas", &x, 5, &y, &z, &a, &b, &c); */
+ /* assert_se(r >= 0); */
+
+ /* r = sd_bus_message_read(m, "a{yv}", 2, */
+ /* &u, "s", &x, */
+ /* &v, "s", &y); */
+
+ return 0;
+}
diff --git a/src/libsystemd-bus/test-bus-signature.c b/src/libsystemd-bus/test-bus-signature.c
new file mode 100644
index 0000000..4310d62
--- /dev/null
+++ b/src/libsystemd-bus/test-bus-signature.c
@@ -0,0 +1,74 @@
+/*-*- 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 <assert.h>
+#include <stdlib.h>
+
+#include "log.h"
+#include "bus-signature.h"
+
+int main(int argc, char *argv[]) {
+
+ assert_se(signature_is_single("y"));
+ assert_se(signature_is_single("u"));
+ assert_se(signature_is_single("v"));
+ assert_se(signature_is_single("as"));
+ assert_se(signature_is_single("(ss)"));
+ assert_se(signature_is_single("()"));
+ assert_se(signature_is_single("(()()()()())"));
+ assert_se(signature_is_single("(((())))"));
+ assert_se(signature_is_single("((((s))))"));
+ assert_se(signature_is_single("{ss}"));
+ assert_se(signature_is_single("a{ss}"));
+ assert_se(!signature_is_single("uu"));
+ assert_se(!signature_is_single(""));
+ assert_se(!signature_is_single("("));
+ assert_se(!signature_is_single(")"));
+ assert_se(!signature_is_single("())"));
+ assert_se(!signature_is_single("((())"));
+ assert_se(!signature_is_single("{)"));
+ assert_se(!signature_is_single("{}"));
+ assert_se(!signature_is_single("{sss}"));
+ assert_se(!signature_is_single("{s}"));
+ assert_se(!signature_is_single("{ass}"));
+ assert_se(!signature_is_single("a}"));
+
+ assert_se(signature_is_pair("yy"));
+ assert_se(signature_is_pair("ss"));
+ assert_se(signature_is_pair("sas"));
+ assert_se(signature_is_pair("sv"));
+ assert_se(signature_is_pair("sa(vs)"));
+ assert_se(!signature_is_pair(""));
+ assert_se(!signature_is_pair("va"));
+ assert_se(!signature_is_pair("sss"));
+ assert_se(!signature_is_pair("{s}ss"));
+
+ assert_se(signature_is_valid("ssa{ss}sssub", true));
+ assert_se(signature_is_valid("ssa{ss}sssub", false));
+ assert_se(signature_is_valid("{ss}", true));
+ assert_se(!signature_is_valid("{ss}", false));
+ assert_se(signature_is_valid("", true));
+ assert_se(signature_is_valid("", false));
+
+ assert_se(signature_is_valid("sssusa(uuubbba(uu)uuuu)a{u(uuuvas)}", false));
+
+ return 0;
+}
commit 76f0199f2ce007c50b5a38f7ff6c51167c3a05a0
Author: Lennart Poettering <lennart at poettering.net>
Date: Tue Mar 19 20:01:35 2013 +0100
stdio-bridge: modernization
diff --git a/src/stdio-bridge/stdio-bridge.c b/src/stdio-bridge/stdio-bridge.c
index adc692f..ea82c8f 100644
--- a/src/stdio-bridge/stdio-bridge.c
+++ b/src/stdio-bridge/stdio-bridge.c
@@ -46,7 +46,7 @@ static void format_uid(char *buf, size_t l) {
assert(l > 0);
snprintf(text, sizeof(text)-1, "%llu", (unsigned long long) geteuid());
- text[sizeof(text)-1] = 0;
+ char_array_0(text);
memset(buf, 0, l);
commit 66e35261ef51119189486e9a31e7b97e8e18e255
Author: Lennart Poettering <lennart at poettering.net>
Date: Tue Mar 19 20:01:18 2013 +0100
util: add hexmem() and strextend() calls
diff --git a/src/shared/util.c b/src/shared/util.c
index bdef9f0..f94abbc 100644
--- a/src/shared/util.c
+++ b/src/shared/util.c
@@ -1029,7 +1029,6 @@ int rmdir_parents(const char *path, const char *stop) {
return 0;
}
-
char hexchar(int x) {
static const char table[16] = "0123456789abcdef";
@@ -1050,6 +1049,23 @@ int unhexchar(char c) {
return -1;
}
+char *hexmem(const void *p, size_t l) {
+ char *r, *z;
+ const uint8_t *x;
+
+ z = r = malloc(l * 2 + 1);
+ if (!r)
+ return NULL;
+
+ for (x = p; x < (const uint8_t*) p + l; x++) {
+ *(z++) = hexchar(*x >> 4);
+ *(z++) = hexchar(*x & 15);
+ }
+
+ *z = 0;
+ return r;
+}
+
char octchar(int x) {
return '0' + (x & 7);
}
@@ -5729,3 +5745,55 @@ fail2:
rmdir(template);
return r;
}
+
+char *strextend(char **x, ...) {
+ va_list ap;
+ size_t f, l;
+ char *r, *p;
+
+ assert(x);
+
+ l = f = *x ? strlen(*x) : 0;
+
+ va_start(ap, x);
+ for (;;) {
+ const char *t;
+ size_t n;
+
+ t = va_arg(ap, const char *);
+ if (!t)
+ break;
+
+ n = strlen(t);
+ if (n > ((size_t) -1) - l) {
+ va_end(ap);
+ return NULL;
+ }
+
+ l += n;
+ }
+ va_end(ap);
+
+ r = realloc(*x, l+1);
+ if (!r)
+ return NULL;
+
+ p = r + f;
+
+ va_start(ap, x);
+ for (;;) {
+ const char *t;
+
+ t = va_arg(ap, const char *);
+ if (!t)
+ break;
+
+ p = stpcpy(p, t);
+ }
+ va_end(ap);
+
+ *p = 0;
+ *x = r;
+
+ return r + l;
+}
diff --git a/src/shared/util.h b/src/shared/util.h
index 7c032b6..25c0084 100644
--- a/src/shared/util.h
+++ b/src/shared/util.h
@@ -605,3 +605,6 @@ static inline void *mempset(void *s, int c, size_t n) {
memset(s, c, n);
return (char*)s + n;
}
+
+char *hexmem(const void *p, size_t l);
+char *strextend(char **x, ...);
commit a9c55a882f021318fa728af78f398f4fb7ad7c85
Author: Lennart Poettering <lennart at poettering.net>
Date: Tue Mar 19 20:00:55 2013 +0100
macro: add CHAR_TO_STR macro to make a one character string from a char
diff --git a/src/shared/macro.h b/src/shared/macro.h
index e5c33ce..80cb2f0 100644
--- a/src/shared/macro.h
+++ b/src/shared/macro.h
@@ -158,6 +158,8 @@ static inline size_t ALIGN_TO(size_t l, size_t ali) {
#define memzero(x,l) (memset((x), 0, (l)))
#define zero(x) (memzero(&(x), sizeof(x)))
+#define CHAR_TO_STR(x) ((char[2]) { x, 0 })
+
#define char_array_0(x) x[sizeof(x)-1] = 0;
#define IOVEC_SET_STRING(i, s) \
commit 907809fc6a9867869b8a237663af2eda335d333e
Author: Lennart Poettering <lennart at poettering.net>
Date: Tue Mar 19 20:00:29 2013 +0100
macro: don't redefine CLAMP if it is already defined by glib or some other library
diff --git a/src/journal/journal-gatewayd.c b/src/journal/journal-gatewayd.c
index 7e29c90..8161b7e 100644
--- a/src/journal/journal-gatewayd.c
+++ b/src/journal/journal-gatewayd.c
@@ -110,7 +110,6 @@ static int open_journal(RequestMeta *m) {
return sd_journal_open(&m->journal, SD_JOURNAL_LOCAL_ONLY|SD_JOURNAL_SYSTEM_ONLY);
}
-
static int respond_oom_internal(struct MHD_Connection *connection) {
struct MHD_Response *response;
const char m[] = "Out of memory.\n";
diff --git a/src/shared/macro.h b/src/shared/macro.h
index 90a663b..e5c33ce 100644
--- a/src/shared/macro.h
+++ b/src/shared/macro.h
@@ -92,6 +92,7 @@ static inline size_t ALIGN_TO(size_t l, size_t ali) {
#define MIN3(a,b,c) \
MIN(MIN(a,b),c)
+#ifndef CLAMP
#define CLAMP(x, low, high) \
__extension__ ({ \
typeof(x) _x = (x); \
@@ -99,6 +100,7 @@ static inline size_t ALIGN_TO(size_t l, size_t ali) {
typeof(high) _high = (high); \
((_x > _high) ? _high : ((_x < _low) ? _low : _x)); \
})
+#endif
#define assert_se(expr) \
do { \
More information about the systemd-commits
mailing list