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.
This commit is contained in:
Lennart Poettering 2013-03-22 01:49:56 +01:00
parent 9f26c90cb5
commit d728d708c3
3 changed files with 42 additions and 26 deletions

View File

@ -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,29 +779,39 @@ int sd_bus_is_open(sd_bus *bus) {
return bus->fd >= 0;
}
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) {
int r;
if (!bus)
return -EINVAL;
if (bus->state != BUS_RUNNING && bus->state != BUS_HELLO)
return -EAGAIN;
if (type == SD_BUS_TYPE_UNIX_FD)
if (type == SD_BUS_TYPE_UNIX_FD) {
r = ensure_running(bus);
if (r < 0)
return r;
return bus->can_fds;
}
return bus_type_is_valid(type);
}
int sd_bus_get_peer(sd_bus *bus, sd_id128_t *peer) {
int r;
if (!bus)
return -EINVAL;
if (!peer)
return -EINVAL;
r = ensure_running(bus);
if (r < 0)
return r;
*peer = bus->peer;
return 0;
}
static int bus_seal_message(sd_bus *b, sd_bus_message *m) {
assert(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;

View File

@ -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);

View File

@ -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));