Commit 89ffcd2a authored by Lennart Poettering's avatar Lennart Poettering

bus: hook up client with socket communication

parent fbfa72b0
......@@ -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
......
......@@ -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 += \
......
......@@ -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;
......
......@@ -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;
}
......
......@@ -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)))
......@@ -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;
}
......@@ -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);
......@@ -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;
......
This diff is collapsed.
......@@ -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, ...);
......
/*-*- 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;
}