summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLennart Poettering <lennart@poettering.net>2013-03-22 01:49:56 +0100
committerLennart Poettering <lennart@poettering.net>2013-03-22 01:49:56 +0100
commitd728d708c3ccfcb34f6d7673f7855fbf0c10aeec (patch)
treed6d059970324202b65a5077da38a566f356c403d
parent9f26c90cb50c45d4549c4dd569917b4ac143b94d (diff)
bus: rework synchronization logic
Instead of allowing certain actions fail during authentication and connection setup, implicitly synchronize on the connection to be set up completely before returning.
-rw-r--r--src/libsystemd-bus/sd-bus.c46
-rw-r--r--src/libsystemd-bus/sd-bus.h5
-rw-r--r--src/libsystemd-bus/test-bus-chat.c11
3 files changed, 39 insertions, 23 deletions
diff --git a/src/libsystemd-bus/sd-bus.c b/src/libsystemd-bus/sd-bus.c
index 049bd356a0..6a6d43fac0 100644
--- a/src/libsystemd-bus/sd-bus.c
+++ b/src/libsystemd-bus/sd-bus.c
@@ -35,6 +35,7 @@
#include "bus-message.h"
#include "bus-type.h"
+static int ensure_running(sd_bus *bus);
static int bus_poll(sd_bus *bus, bool need_more, uint64_t timeout_usec);
static void bus_free(sd_bus *b) {
@@ -778,27 +779,37 @@ int sd_bus_is_open(sd_bus *bus) {
return bus->fd >= 0;
}
-int sd_bus_is_running(sd_bus *bus) {
+int sd_bus_can_send(sd_bus *bus, char type) {
+ int r;
+
if (!bus)
return -EINVAL;
- if (bus->fd < 0)
- return -ENOTCONN;
+ if (type == SD_BUS_TYPE_UNIX_FD) {
+ r = ensure_running(bus);
+ if (r < 0)
+ return r;
- return bus->state == BUS_RUNNING;
+ return bus->can_fds;
+ }
+
+ return bus_type_is_valid(type);
}
-int sd_bus_can_send(sd_bus *bus, char type) {
+int sd_bus_get_peer(sd_bus *bus, sd_id128_t *peer) {
+ int r;
if (!bus)
return -EINVAL;
- if (bus->state != BUS_RUNNING && bus->state != BUS_HELLO)
- return -EAGAIN;
+ if (!peer)
+ return -EINVAL;
- if (type == SD_BUS_TYPE_UNIX_FD)
- return bus->can_fds;
+ r = ensure_running(bus);
+ if (r < 0)
+ return r;
- return bus_type_is_valid(type);
+ *peer = bus->peer;
+ return 0;
}
static int bus_seal_message(sd_bus *b, sd_bus_message *m) {
@@ -1243,22 +1254,15 @@ static int ensure_running(sd_bus *bus) {
assert(bus);
- r = sd_bus_is_running(bus);
- if (r != 0)
- return r;
+ if (bus->state == BUS_RUNNING)
+ return 1;
for (;;) {
- int k;
-
r = sd_bus_process(bus, NULL);
-
if (r < 0)
return r;
-
- k = sd_bus_is_running(bus);
- if (k != 0)
- return k;
-
+ if (bus->state == BUS_RUNNING)
+ return 1;
if (r > 0)
continue;
diff --git a/src/libsystemd-bus/sd-bus.h b/src/libsystemd-bus/sd-bus.h
index 3ea4acc76f..adc7f8e2bd 100644
--- a/src/libsystemd-bus/sd-bus.h
+++ b/src/libsystemd-bus/sd-bus.h
@@ -35,8 +35,9 @@
* - always verify container depth
* - merge busctl into systemctl or so?
* - add object handlers
- * - verify object paths
* - implicitly add stub introspection calls
+ * - implement unix exec protocol
+ * - server side
*/
typedef struct sd_bus sd_bus;
@@ -62,8 +63,8 @@ sd_bus *sd_bus_ref(sd_bus *bus);
sd_bus *sd_bus_unref(sd_bus *bus);
int sd_bus_is_open(sd_bus *bus);
-int sd_bus_is_running(sd_bus *bus);
int sd_bus_can_send(sd_bus *bus, char type);
+int sd_bus_get_peer(sd_bus *bus, sd_id128_t *peer);
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);
diff --git a/src/libsystemd-bus/test-bus-chat.c b/src/libsystemd-bus/test-bus-chat.c
index d8677cc9e8..5f533cc6d0 100644
--- a/src/libsystemd-bus/test-bus-chat.c
+++ b/src/libsystemd-bus/test-bus-chat.c
@@ -33,6 +33,7 @@
static int server_init(sd_bus **_bus) {
sd_bus *bus = NULL;
+ sd_id128_t id;
int r;
assert(_bus);
@@ -43,6 +44,16 @@ static int server_init(sd_bus **_bus) {
goto fail;
}
+ r = sd_bus_get_peer(bus, &id);
+ if (r < 0) {
+ log_error("Failed to get peer ID: %s", strerror(-r));
+ goto fail;
+ }
+
+ log_info("Peer ID is " SD_ID128_FORMAT_STR ".", SD_ID128_FORMAT_VAL(id));
+ log_info("Can send file handles: %i", sd_bus_can_send(bus, 'h'));
+ log_info("Unique ID: %s", strna(sd_bus_get_unique_name(bus)));
+
r = sd_bus_request_name(bus, "org.freedesktop.systemd.test", 0);
if (r < 0) {
log_error("Failed to acquire name: %s", strerror(-r));