summaryrefslogtreecommitdiff
path: root/src/libsystemd-bus
diff options
context:
space:
mode:
authorLennart Poettering <lennart@poettering.net>2013-03-21 22:50:25 +0100
committerLennart Poettering <lennart@poettering.net>2013-03-21 22:53:29 +0100
commite3017af97310da024ffb378ed155bc1676922ce7 (patch)
treea09b0a555b89f7ec00d0ab62294770db99c5642a /src/libsystemd-bus
parent43b4c9aaf56aefdc55c4b6c1df78e30cdcf3988f (diff)
bus: implement full method call timeout logic
Diffstat (limited to 'src/libsystemd-bus')
-rw-r--r--src/libsystemd-bus/bus-control.c18
-rw-r--r--src/libsystemd-bus/bus-error.c7
-rw-r--r--src/libsystemd-bus/bus-error.h2
-rw-r--r--src/libsystemd-bus/bus-internal.h6
-rw-r--r--src/libsystemd-bus/bus-message.c7
-rw-r--r--src/libsystemd-bus/bus-message.h1
-rw-r--r--src/libsystemd-bus/sd-bus-protocol.h2
-rw-r--r--src/libsystemd-bus/sd-bus.c385
-rw-r--r--src/libsystemd-bus/sd-bus.h11
-rw-r--r--src/libsystemd-bus/test-bus-chat.c187
10 files changed, 499 insertions, 127 deletions
diff --git a/src/libsystemd-bus/bus-control.c b/src/libsystemd-bus/bus-control.c
index 50e1a2c00a..9d242dc413 100644
--- a/src/libsystemd-bus/bus-control.c
+++ b/src/libsystemd-bus/bus-control.c
@@ -59,7 +59,7 @@ int sd_bus_request_name(sd_bus *bus, const char *name, int flags) {
if (r < 0)
return r;
- r = sd_bus_send_with_reply_and_block(bus, m, (uint64_t) -1, NULL, &reply);
+ r = sd_bus_send_with_reply_and_block(bus, m, 0, NULL, &reply);
if (r < 0)
return r;
@@ -94,7 +94,7 @@ int sd_bus_release_name(sd_bus *bus, const char *name) {
if (r < 0)
return r;
- r = sd_bus_send_with_reply_and_block(bus, m, (uint64_t) -1, NULL, &reply);
+ r = sd_bus_send_with_reply_and_block(bus, m, 0, NULL, &reply);
if (r < 0)
return r;
@@ -135,11 +135,11 @@ int sd_bus_list_names(sd_bus *bus, char ***l) {
if (r < 0)
return r;
- r = sd_bus_send_with_reply_and_block(bus, m1, (uint64_t) -1, NULL, &reply1);
+ r = sd_bus_send_with_reply_and_block(bus, m1, 0, NULL, &reply1);
if (r < 0)
return r;
- r = sd_bus_send_with_reply_and_block(bus, m2, (uint64_t) -1, NULL, &reply2);
+ r = sd_bus_send_with_reply_and_block(bus, m2, 0, NULL, &reply2);
if (r < 0)
return r;
@@ -183,7 +183,7 @@ int sd_bus_get_owner(sd_bus *bus, const char *name, char **owner) {
if (r < 0)
return r;
- r = sd_bus_send_with_reply_and_block(bus, m, (uint64_t) -1, NULL, &reply);
+ r = sd_bus_send_with_reply_and_block(bus, m, 0, NULL, &reply);
if (r < 0)
return r;
@@ -230,7 +230,7 @@ int sd_bus_get_owner_uid(sd_bus *bus, const char *name, uid_t *uid) {
if (r < 0)
return r;
- r = sd_bus_send_with_reply_and_block(bus, m, (uint64_t) -1, NULL, &reply);
+ r = sd_bus_send_with_reply_and_block(bus, m, 0, NULL, &reply);
if (r < 0)
return r;
@@ -268,7 +268,7 @@ int sd_bus_get_owner_pid(sd_bus *bus, const char *name, pid_t *pid) {
if (r < 0)
return r;
- r = sd_bus_send_with_reply_and_block(bus, m, (uint64_t) -1, NULL, &reply);
+ r = sd_bus_send_with_reply_and_block(bus, m, 0, NULL, &reply);
if (r < 0)
return r;
@@ -306,7 +306,7 @@ int sd_bus_add_match(sd_bus *bus, const char *match) {
if (r < 0)
return r;
- return sd_bus_send_with_reply_and_block(bus, m, (uint64_t) -1, NULL, &reply);
+ return sd_bus_send_with_reply_and_block(bus, m, 0, NULL, &reply);
}
int sd_bus_remove_match(sd_bus *bus, const char *match) {
@@ -332,5 +332,5 @@ int sd_bus_remove_match(sd_bus *bus, const char *match) {
if (r < 0)
return r;
- return sd_bus_send_with_reply_and_block(bus, m, (uint64_t) -1, NULL, &reply);
+ return sd_bus_send_with_reply_and_block(bus, m, 0, NULL, &reply);
}
diff --git a/src/libsystemd-bus/bus-error.c b/src/libsystemd-bus/bus-error.c
index 55b418768a..5faa17384e 100644
--- a/src/libsystemd-bus/bus-error.c
+++ b/src/libsystemd-bus/bus-error.c
@@ -168,3 +168,10 @@ int bus_error_from_errno(sd_bus_error *e, int error) {
return error;
}
+
+const char *bus_error_message(const sd_bus_error *e, int error) {
+ if (e && e->message)
+ return e->message;
+
+ return strerror(error);
+}
diff --git a/src/libsystemd-bus/bus-error.h b/src/libsystemd-bus/bus-error.h
index c6e072eab4..ceca7d678f 100644
--- a/src/libsystemd-bus/bus-error.h
+++ b/src/libsystemd-bus/bus-error.h
@@ -27,3 +27,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);
+
+const char *bus_error_message(const sd_bus_error *e, int error);
diff --git a/src/libsystemd-bus/bus-internal.h b/src/libsystemd-bus/bus-internal.h
index 9a12d661e1..9e4cf16b7f 100644
--- a/src/libsystemd-bus/bus-internal.h
+++ b/src/libsystemd-bus/bus-internal.h
@@ -26,6 +26,7 @@
#include <netinet/in.h>
#include "hashmap.h"
+#include "prioq.h"
#include "list.h"
#include "util.h"
@@ -37,6 +38,7 @@ struct reply_callback {
void *userdata;
usec_t timeout;
uint64_t serial;
+ unsigned prioq_idx;
};
struct filter_callback {
@@ -75,6 +77,7 @@ struct sd_bus {
char *unique_name;
+ Prioq *reply_callbacks_prioq;
Hashmap *reply_callbacks;
LIST_HEAD(struct filter_callback, filter_callbacks);
@@ -97,6 +100,7 @@ struct sd_bus {
unsigned auth_index;
size_t auth_size;
char *auth_uid;
+ usec_t auth_timeout;
};
static inline void bus_unrefp(sd_bus **b) {
@@ -104,3 +108,5 @@ static inline void bus_unrefp(sd_bus **b) {
}
#define _cleanup_bus_unref_ __attribute__((cleanup(bus_unrefp)))
+
+#define BUS_DEFAULT_TIMEOUT ((usec_t) (25 * USEC_PER_SEC))
diff --git a/src/libsystemd-bus/bus-message.c b/src/libsystemd-bus/bus-message.c
index ec2dd07dba..c0a0242fd6 100644
--- a/src/libsystemd-bus/bus-message.c
+++ b/src/libsystemd-bus/bus-message.c
@@ -403,6 +403,8 @@ static int message_new_reply(
if (!call)
return -EINVAL;
+ if (!call->sealed)
+ return -EPERM;
if (call->header->type != SD_BUS_MESSAGE_TYPE_METHOD_CALL)
return -EINVAL;
if (!m)
@@ -2441,14 +2443,17 @@ static void setup_iovec(sd_bus_message *m) {
assert(m->sealed);
m->n_iovec = 0;
+ m->size = 0;
m->iovec[m->n_iovec].iov_base = m->header;
m->iovec[m->n_iovec].iov_len = sizeof(*m->header);
+ m->size += m->iovec[m->n_iovec].iov_len;
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->size += m->iovec[m->n_iovec].iov_len;
m->n_iovec++;
if (m->header->fields_size % 8 != 0) {
@@ -2456,6 +2461,7 @@ static void setup_iovec(sd_bus_message *m) {
m->iovec[m->n_iovec].iov_base = (void*) padding;
m->iovec[m->n_iovec].iov_len = 8 - m->header->fields_size % 8;
+ m->size += m->iovec[m->n_iovec].iov_len;
m->n_iovec++;
}
}
@@ -2463,6 +2469,7 @@ static void setup_iovec(sd_bus_message *m) {
if (m->body) {
m->iovec[m->n_iovec].iov_base = m->body;
m->iovec[m->n_iovec].iov_len = m->header->body_size;
+ m->size += m->iovec[m->n_iovec].iov_len;
m->n_iovec++;
}
}
diff --git a/src/libsystemd-bus/bus-message.h b/src/libsystemd-bus/bus-message.h
index 0184d18dd9..ed1a4b87b1 100644
--- a/src/libsystemd-bus/bus-message.h
+++ b/src/libsystemd-bus/bus-message.h
@@ -87,6 +87,7 @@ struct sd_bus_message {
struct iovec iovec[4];
unsigned n_iovec;
+ size_t size;
char *peeked_signature;
};
diff --git a/src/libsystemd-bus/sd-bus-protocol.h b/src/libsystemd-bus/sd-bus-protocol.h
index 942422f008..f9a5217dda 100644
--- a/src/libsystemd-bus/sd-bus-protocol.h
+++ b/src/libsystemd-bus/sd-bus-protocol.h
@@ -26,8 +26,6 @@
/* 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,
diff --git a/src/libsystemd-bus/sd-bus.c b/src/libsystemd-bus/sd-bus.c
index f27d47cdea..d0a36081bc 100644
--- a/src/libsystemd-bus/sd-bus.c
+++ b/src/libsystemd-bus/sd-bus.c
@@ -37,6 +37,8 @@
#define WQUEUE_MAX 128
+static int bus_poll(sd_bus *bus, bool need_more, uint64_t timeout_usec);
+
static void bus_free(sd_bus *b) {
struct filter_callback *f;
unsigned i;
@@ -60,6 +62,7 @@ static void bus_free(sd_bus *b) {
free(b->wqueue);
hashmap_free_free(b->reply_callbacks);
+ prioq_free(b->reply_callbacks_prioq);
while ((f = b->filter_callbacks)) {
LIST_REMOVE(struct filter_callback, callbacks, b->filter_callbacks, f);
@@ -91,11 +94,15 @@ static sd_bus* bus_new(void) {
return r;
};
-static int hello_callback(sd_bus *bus, sd_bus_message *reply, void *userdata) {
+static int hello_callback(sd_bus *bus, int error, sd_bus_message *reply, void *userdata) {
const char *s;
int r;
assert(bus);
+
+ if (error != 0)
+ return -error;
+
assert(reply);
bus->state = BUS_RUNNING;
@@ -127,7 +134,7 @@ static int bus_send_hello(sd_bus *bus) {
if (r < 0)
return r;
- r = sd_bus_send_with_reply(bus, m, hello_callback, NULL, (uint64_t) -1, NULL);
+ r = sd_bus_send_with_reply(bus, m, hello_callback, NULL, 0, NULL);
if (r < 0)
return r;
@@ -140,11 +147,11 @@ static int bus_start_running(sd_bus *bus) {
if (bus->sent_hello) {
bus->state = BUS_HELLO;
- return 0;
+ return 1;
}
bus->state = BUS_RUNNING;
- return 0;
+ return 1;
}
static int parse_address_key(const char **p, const char *key, char **value) {
@@ -402,6 +409,9 @@ static int bus_write_auth(sd_bus *b) {
if (b->auth_index >= ELEMENTSOF(b->auth_iovec))
return 0;
+ if (b->auth_timeout == 0)
+ b->auth_timeout = now(CLOCK_MONOTONIC) + BUS_DEFAULT_TIMEOUT;
+
zero(mh);
mh.msg_iov = b->auth_iovec + b->auth_index;
mh.msg_iovlen = ELEMENTSOF(b->auth_iovec) - b->auth_index;
@@ -460,12 +470,8 @@ static int bus_auth_verify(sd_bus *b) {
(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);
- }
-
- b->rbuffer_size = 0;
+ b->rbuffer_size -= (f + 2 - (char*) b->rbuffer);
+ memmove(b->rbuffer, f + 2, b->rbuffer_size);
r = bus_start_running(b);
if (r < 0)
@@ -513,7 +519,7 @@ static int bus_read_auth(sd_bus *b) {
if (r != 0)
return r;
- return 0;
+ return 1;
}
static int bus_start_auth(sd_bus *b) {
@@ -558,12 +564,12 @@ static int bus_start_connect(sd_bus *b) {
if (r < 0)
return r;
if (r == 0)
- return b->last_connect_error ? b->last_connect_error : -ECONNREFUSED;
+ 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;
+ b->last_connect_error = errno;
zero(b->sockaddr);
continue;
}
@@ -571,9 +577,9 @@ static int bus_start_connect(sd_bus *b) {
r = connect(b->fd, &b->sockaddr.sa, b->sockaddr_size);
if (r < 0) {
if (errno == EINPROGRESS)
- return 0;
+ return 1;
- b->last_connect_error = -errno;
+ b->last_connect_error = errno;
close_nointr_nofail(b->fd);
b->fd = -1;
zero(b->sockaddr);
@@ -760,6 +766,13 @@ sd_bus *sd_bus_unref(sd_bus *bus) {
return NULL;
}
+int sd_bus_is_open(sd_bus *bus) {
+ if (!bus)
+ return -EINVAL;
+
+ return bus->fd >= 0;
+}
+
int sd_bus_is_running(sd_bus *bus) {
if (!bus)
return -EINVAL;
@@ -807,6 +820,9 @@ static int message_write(sd_bus *bus, sd_bus_message *m, size_t *idx) {
assert(idx);
assert(bus->state == BUS_RUNNING || bus->state == BUS_HELLO);
+ if (*idx >= m->size)
+ return 0;
+
n = m->n_iovec * sizeof(struct iovec);
iov = alloca(n);
memcpy(iov, m->iovec, n);
@@ -820,12 +836,10 @@ static int message_write(sd_bus *bus, sd_bus_message *m, size_t *idx) {
k = sendmsg(bus->fd, &mh, MSG_DONTWAIT|MSG_NOSIGNAL);
if (k < 0)
- return -errno;
+ return errno == EAGAIN ? 0 : -errno;
*idx += (size_t) k;
- iovec_advance(iov, &j, *idx);
-
- return j >= m->n_iovec;
+ return 1;
}
static int message_read_need(sd_bus *bus, size_t *need) {
@@ -837,7 +851,24 @@ static int message_read_need(sd_bus *bus, size_t *need) {
assert(bus->state == BUS_RUNNING || bus->state == BUS_HELLO);
if (bus->rbuffer_size < sizeof(struct bus_header)) {
- *need = sizeof(struct bus_header);
+ *need = sizeof(struct bus_header) + 8;
+
+ /* Minimum message size:
+ *
+ * Header +
+ *
+ * Method Call: +2 string headers
+ * Signal: +3 string headers
+ * Method Error: +1 string headers
+ * +1 uint32 headers
+ * Method Reply: +1 uint32 headers
+ *
+ * A string header is at least 9 bytes
+ * A uint32 header is at least 8 bytes
+ *
+ * Hence the minimum message size of a valid message
+ * is header + 8 bytes */
+
return 0;
}
@@ -935,11 +966,11 @@ static int message_read(sd_bus *bus, sd_bus_message **m) {
if (bus->rbuffer_size >= need)
return message_make(bus, need, m);
- return 0;
+ return 1;
}
static int dispatch_wqueue(sd_bus *bus) {
- int r, c = 0;
+ int r, ret = 0;
assert(bus);
assert(bus->state == BUS_RUNNING || bus->state == BUS_HELLO);
@@ -954,9 +985,9 @@ static int dispatch_wqueue(sd_bus *bus) {
sd_bus_close(bus);
return r;
} else if (r == 0)
- /* Wasn't fully written yet... */
- break;
- else {
+ /* Didn't do anything this time */
+ return ret;
+ else if (bus->windex >= bus->wqueue[0]->size) {
/* Fully written. Let's drop the entry from
* the queue.
*
@@ -972,15 +1003,16 @@ static int dispatch_wqueue(sd_bus *bus) {
memmove(bus->wqueue, bus->wqueue + 1, sizeof(sd_bus_message*) * bus->wqueue_size);
bus->windex = 0;
- c++;
+ ret = 1;
}
}
- return c;
+ return ret;
}
static int dispatch_rqueue(sd_bus *bus, sd_bus_message **m) {
- int r;
+ sd_bus_message *z;
+ int r, ret = 0;
assert(bus);
assert(m);
@@ -999,13 +1031,20 @@ static int dispatch_rqueue(sd_bus *bus, sd_bus_message **m) {
}
/* Try to read a new message */
- r = message_read(bus, m);
- if (r < 0) {
- sd_bus_close(bus);
- return r;
- }
+ do {
+ r = message_read(bus, &z);
+ if (r < 0) {
+ sd_bus_close(bus);
+ return r;
+ }
+ if (r == 0)
+ return ret;
- return r;
+ r = 1;
+ } while (!z);
+
+ *m = z;
+ return 1;
}
int sd_bus_send(sd_bus *bus, sd_bus_message *m, uint64_t *serial) {
@@ -1034,7 +1073,7 @@ int sd_bus_send(sd_bus *bus, sd_bus_message *m, uint64_t *serial) {
if (r < 0) {
sd_bus_close(bus);
return r;
- } else if (r == 0) {
+ } else if (idx < m->size) {
/* 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
@@ -1071,11 +1110,29 @@ static usec_t calc_elapse(uint64_t usec) {
return 0;
if (usec == 0)
- usec = SD_BUS_DEFAULT_TIMEOUT;
+ usec = BUS_DEFAULT_TIMEOUT;
return now(CLOCK_MONOTONIC) + usec;
}
+static int timeout_compare(const void *a, const void *b) {
+ const struct reply_callback *x = a, *y = b;
+
+ if (x->timeout != 0 && y->timeout == 0)
+ return -1;
+
+ if (x->timeout == 0 && y->timeout != 0)
+ return 1;
+
+ if (x->timeout < y->timeout)
+ return -1;
+
+ if (x->timeout > y->timeout)
+ return 1;
+
+ return 0;
+}
+
int sd_bus_send_with_reply(
sd_bus *bus,
sd_bus_message *m,
@@ -1104,6 +1161,12 @@ int sd_bus_send_with_reply(
if (r < 0)
return r;
+ if (usec != (uint64_t) -1) {
+ r = prioq_ensure_allocated(&bus->reply_callbacks_prioq, timeout_compare);
+ if (r < 0)
+ return r;
+ }
+
r = bus_seal_message(bus, m);
if (r < 0)
return r;
@@ -1123,10 +1186,18 @@ int sd_bus_send_with_reply(
return r;
}
+ if (c->timeout != 0) {
+ r = prioq_put(bus->reply_callbacks_prioq, c, &c->prioq_idx);
+ if (r < 0) {
+ c->timeout = 0;
+ sd_bus_send_with_reply_cancel(bus, c->serial);
+ return r;
+ }
+ }
+
r = sd_bus_send(bus, m, serial);
if (r < 0) {
- hashmap_remove(bus->reply_callbacks, &c->serial);
- free(c);
+ sd_bus_send_with_reply_cancel(bus, c->serial);
return r;
}
@@ -1134,7 +1205,7 @@ int sd_bus_send_with_reply(
}
int sd_bus_send_with_reply_cancel(sd_bus *bus, uint64_t serial) {
- struct reply_callbacks *c;
+ struct reply_callback *c;
if (!bus)
return -EINVAL;
@@ -1145,6 +1216,9 @@ int sd_bus_send_with_reply_cancel(sd_bus *bus, uint64_t serial) {
if (!c)
return 0;
+ if (c->timeout != 0)
+ prioq_remove(bus->reply_callbacks_prioq, c, &c->prioq_idx);
+
free(c);
return 1;
}
@@ -1159,13 +1233,19 @@ static int ensure_running(sd_bus *bus) {
return r;
for (;;) {
+ int k;
+
r = sd_bus_process(bus, NULL);
+
if (r < 0)
return r;
- r = sd_bus_is_running(bus);
- if (r != 0)
- return r;
+ k = sd_bus_is_running(bus);
+ if (k != 0)
+ return k;
+
+ if (r > 0)
+ continue;
r = sd_bus_wait(bus, (uint64_t) -1);
if (r < 0)
@@ -1210,7 +1290,7 @@ int sd_bus_send_with_reply_and_block(
for (;;) {
usec_t left;
- sd_bus_message *incoming;
+ sd_bus_message *incoming = NULL;
if (!room) {
sd_bus_message **q;
@@ -1229,9 +1309,7 @@ int sd_bus_send_with_reply_and_block(
r = message_read(bus, &incoming);
if (r < 0)
return r;
- if (r > 0) {
- /* bus_message_dump(incoming); */
- /* sd_bus_message_rewind(incoming, true); */
+ if (incoming) {
if (incoming->reply_serial == serial) {
/* Found a match! */
@@ -1267,6 +1345,8 @@ int sd_bus_send_with_reply_and_block(
/* Try to read more, right-away */
continue;
}
+ if (r != 0)
+ continue;
if (timeout > 0) {
usec_t n;
@@ -1279,7 +1359,7 @@ int sd_bus_send_with_reply_and_block(
} else
left = (uint64_t) -1;
- r = sd_bus_wait(bus, left);
+ r = bus_poll(bus, true, left);
if (r < 0)
return r;
@@ -1326,9 +1406,97 @@ int sd_bus_get_events(sd_bus *bus) {
return flags;
}
+int sd_bus_get_timeout(sd_bus *bus, uint64_t *timeout_usec) {
+ struct reply_callback *c;
+
+ if (!bus)
+ return -EINVAL;
+ if (!timeout_usec)
+ return -EINVAL;
+ if (bus->fd < 0)
+ return -ENOTCONN;
+
+ if (bus->state == BUS_AUTHENTICATING) {
+ *timeout_usec = bus->auth_timeout;
+ return 1;
+ }
+
+ if (bus->state != BUS_RUNNING && bus->state != BUS_HELLO)
+ return 0;
+
+ c = prioq_peek(bus->reply_callbacks_prioq);
+ if (!c)
+ return 0;
+
+ *timeout_usec = c->timeout;
+ return 1;
+}
+
+static int process_timeout(sd_bus *bus) {
+ _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
+ struct reply_callback *c;
+ usec_t n;
+ int r;
+
+ assert(bus);
+
+ c = prioq_peek(bus->reply_callbacks_prioq);
+ if (!c)
+ return 0;
+
+ n = now(CLOCK_MONOTONIC);
+ if (c->timeout > n)
+ return 0;
+
+ assert_se(prioq_pop(bus->reply_callbacks_prioq) == c);
+ hashmap_remove(bus->reply_callbacks, &c->serial);
+
+ r = c->callback(bus, ETIMEDOUT, NULL, c->userdata);
+ free(c);
+
+ return r < 0 ? r : 1;
+}
+
+static int process_message(sd_bus *bus, sd_bus_message *m) {
+ struct filter_callback *l;
+ int r;
+
+ assert(bus);
+ assert(m);
+
+ 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) {
+ if (c->timeout != 0)
+ prioq_remove(bus->reply_callbacks_prioq, c, &c->prioq_idx);
+
+ r = c->callback(bus, 0, m, c->userdata);
+ free(c);
+
+ if (r != 0)
+ return r;
+ }
+ }
+
+ LIST_FOREACH(callbacks, l, bus->filter_callbacks) {
+ r = l->callback(bus, 0, m, l->userdata);
+ if (r != 0)
+ return r;
+ }
+
+ return 0;
+}
+
int sd_bus_process(sd_bus *bus, sd_bus_message **ret) {
int r;
+ /* Returns 0 when we didn't do anything. This should cause the
+ * caller to invoke sd_bus_wait() before returning the next
+ * time. Returns > 0 when we did something, which possibly
+ * means *ret is filled in with an unprocessed message. */
+
if (!bus)
return -EINVAL;
if (bus->fd < 0)
@@ -1351,63 +1519,61 @@ int sd_bus_process(sd_bus *bus, sd_bus_message **ret) {
r = getsockopt(bus->fd, SOL_SOCKET, SO_ERROR, &error, &slen);
if (r < 0)
- return -errno;
-
- if (error != 0)
- bus->last_connect_error = -error;
+ bus->last_connect_error = errno;
+ else 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);
+ bus->last_connect_error = ECONNREFUSED;
+ else {
+ r = bus_start_auth(bus);
+ goto null_message;
+ }
/* Try next address */
- return bus_start_connect(bus);
+ r = bus_start_connect(bus);
+ goto null_message;
}
- return 0;
+ r = 0;
+ goto null_message;
} else if (bus->state == BUS_AUTHENTICATING) {
+ if (now(CLOCK_MONOTONIC) >= bus->auth_timeout)
+ return -ETIMEDOUT;
+
r = bus_write_auth(bus);
- if (r < 0)
- return r;
+ if (r != 0)
+ goto null_message;
r = bus_read_auth(bus);
- if (r < 0)
- return r;
-
- return 0;
+ goto null_message;
} else if (bus->state == BUS_RUNNING || bus->state == BUS_HELLO) {
- struct filter_callback *l;
_cleanup_bus_message_unref_ sd_bus_message *m = NULL;
+ int k;
+
+ r = process_timeout(bus);
+ if (r != 0)
+ goto null_message;
r = dispatch_wqueue(bus);
- if (r < 0)
- return r;
+ if (r != 0)
+ goto null_message;
+ k = r;
r = dispatch_rqueue(bus, &m);
- if (r <= 0)
+ 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)
- return r < 0 ? r : 0;
- }
+ if (!m) {
+ if (r == 0)
+ r = k;
+ goto null_message;
}
- LIST_FOREACH(callbacks, l, bus->filter_callbacks) {
- r = l->callback(bus, m, l->userdata);
- if (r != 0)
- return r < 0 ? r : 0;
- }
+ r = process_message(bus, m);
+ if (r != 0)
+ goto null_message;
if (ret) {
*ret = m;
@@ -1428,38 +1594,71 @@ int sd_bus_process(sd_bus *bus, sd_bus_message **ret) {
return r;
}
- return 0;
+ return 1;
}
assert_not_reached("Unknown state");
+
+null_message:
+ if (r >= 0 && ret)
+ *ret = NULL;
+
+ return r;
}
-int sd_bus_wait(sd_bus *bus, uint64_t timeout_usec) {
+static int bus_poll(sd_bus *bus, bool need_more, uint64_t timeout_usec) {
struct pollfd p;
int r, e;
struct timespec ts;
+ usec_t until, m;
+
+ assert(bus);
- if (!bus)
- return -EINVAL;
if (bus->fd < 0)
return -ENOTCONN;
- if (bus->rqueue_size > 0)
- return 0;
-
e = sd_bus_get_events(bus);
if (e < 0)
return e;
+ if (need_more)
+ e |= POLLIN;
+
+ r = sd_bus_get_timeout(bus, &until);
+ if (r < 0)
+ return r;
+ if (r == 0)
+ m = (uint64_t) -1;
+ else {
+ usec_t n;
+ n = now(CLOCK_MONOTONIC);
+ m = until > n ? until - n : 0;
+ }
+
+ if (timeout_usec != (uint64_t) -1 && (m == (uint64_t) -1 || timeout_usec < m))
+ m = timeout_usec;
+
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);
+ r = ppoll(&p, 1, m == (uint64_t) -1 ? NULL : timespec_store(&ts, m), NULL);
if (r < 0)
return -errno;
- return r;
+ return r > 0 ? 1 : 0;
+}
+
+int sd_bus_wait(sd_bus *bus, uint64_t timeout_usec) {
+
+ if (!bus)
+ return -EINVAL;
+ if (bus->fd < 0)
+ return -ENOTCONN;
+ if (bus->rqueue_size > 0)
+ return 0;
+
+ return bus_poll(bus, false, timeout_usec);
}
int sd_bus_flush(sd_bus *bus) {
@@ -1485,7 +1684,7 @@ int sd_bus_flush(sd_bus *bus) {
if (bus->wqueue_size <= 0)
return 0;
- r = sd_bus_wait(bus, (uint64_t) -1);
+ r = bus_poll(bus, false, (uint64_t) -1);
if (r < 0)
return r;
}
diff --git a/src/libsystemd-bus/sd-bus.h b/src/libsystemd-bus/sd-bus.h
index 03ea4b8848..ce92ee7df6 100644
--- a/src/libsystemd-bus/sd-bus.h
+++ b/src/libsystemd-bus/sd-bus.h
@@ -33,11 +33,14 @@
* - 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?
+ * - add object handlers
+ * - add peer message handlers
+ * - verify object paths
+ * - when reading a message, verify its size
+ * - add limits to wqueue and rqueue alike
*/
typedef struct sd_bus sd_bus;
@@ -49,7 +52,7 @@ typedef struct {
int need_free;
} sd_bus_error;
-typedef int (*sd_message_handler_t)(sd_bus *bus, sd_bus_message *m, void *userdata);
+typedef int (*sd_message_handler_t)(sd_bus *bus, int ret, sd_bus_message *m, void *userdata);
/* Connections */
@@ -62,6 +65,7 @@ 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_open(sd_bus *bus);
int sd_bus_is_running(sd_bus *bus);
int sd_bus_can_send(sd_bus *bus, char type);
@@ -72,6 +76,7 @@ int sd_bus_send_with_reply_and_block(sd_bus *bus, sd_bus_message *m, uint64_t us
int sd_bus_get_fd(sd_bus *bus);
int sd_bus_get_events(sd_bus *bus);
+int sd_bus_get_timeout(sd_bus *bus, uint64_t *timeout_usec);
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);
diff --git a/src/libsystemd-bus/test-bus-chat.c b/src/libsystemd-bus/test-bus-chat.c
index 4260ca3bc0..9e14824186 100644
--- a/src/libsystemd-bus/test-bus-chat.c
+++ b/src/libsystemd-bus/test-bus-chat.c
@@ -22,12 +22,14 @@
#include <assert.h>
#include <stdlib.h>
#include <pthread.h>
+#include <unistd.h>
#include "log.h"
#include "util.h"
#include "sd-bus.h"
#include "bus-message.h"
+#include "bus-error.h"
static int server_init(sd_bus **_bus) {
sd_bus *bus = NULL;
@@ -57,11 +59,11 @@ fail:
return r;
}
-static void* server(void *p) {
- sd_bus *bus = p;
+static int server(sd_bus *bus) {
int r;
+ bool client1_gone = false, client2_gone = false;
- for (;;) {
+ while (!client1_gone || !client2_gone) {
_cleanup_bus_message_unref_ sd_bus_message *m = NULL, *reply = NULL;
r = sd_bus_process(bus, &m);
@@ -69,6 +71,7 @@ static void* server(void *p) {
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) {
@@ -79,6 +82,9 @@ static void* server(void *p) {
continue;
}
+ if (!m)
+ continue;
+
log_info("Got message! %s", strna(sd_bus_message_get_member(m)));
/* bus_message_dump(m); */
/* sd_bus_message_rewind(m, true); */
@@ -112,9 +118,34 @@ static void* server(void *p) {
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)) {
+ } else if (sd_bus_message_is_method_call(m, "org.freedesktop.systemd.test", "ExitClient1")) {
+
+ r = sd_bus_message_new_method_return(bus, m, &reply);
+ if (r < 0) {
+ log_error("Failed to allocate return: %s", strerror(-r));
+ goto fail;
+ }
+
+ client1_gone = true;
+ } else if (sd_bus_message_is_method_call(m, "org.freedesktop.systemd.test", "ExitClient2")) {
+
+ r = sd_bus_message_new_method_return(bus, m, &reply);
+ if (r < 0) {
+ log_error("Failed to allocate return: %s", strerror(-r));
+ goto fail;
+ }
+
+ client2_gone = true;
+ } else if (sd_bus_message_is_method_call(m, "org.freedesktop.systemd.test", "Slow")) {
+
+ r = sd_bus_message_new_method_return(bus, m, &reply);
+ if (r < 0) {
+ log_error("Failed to allocate return: %s", strerror(-r));
+ goto fail;
+ }
+
+ sleep(1);
+ } 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);
@@ -130,19 +161,25 @@ static void* server(void *p) {
log_error("Failed to send reply: %s", strerror(-r));
goto fail;
}
+
+ /* log_info("Sent"); */
+ /* bus_message_dump(reply); */
+ /* sd_bus_message_rewind(reply, true); */
}
}
r = 0;
fail:
- if (bus)
+ if (bus) {
+ sd_bus_flush(bus);
sd_bus_unref(bus);
+ }
- return INT_TO_PTR(r);
+ return r;
}
-static int client(void) {
+static void* client1(void*p) {
_cleanup_bus_message_unref_ sd_bus_message *m = NULL, *reply = NULL;
sd_bus *bus = NULL;
sd_bus_error error = SD_BUS_ERROR_INIT;
@@ -173,9 +210,9 @@ static int client(void) {
goto finish;
}
- r = sd_bus_send_with_reply_and_block(bus, m, (uint64_t) -1, &error, &reply);
+ r = sd_bus_send_with_reply_and_block(bus, m, 0, &error, &reply);
if (r < 0) {
- log_error("Failed to issue method call: %s", error.message);
+ log_error("Failed to issue method call: %s", bus_error_message(&error, -r));
goto finish;
}
@@ -198,7 +235,7 @@ finish:
"org.freedesktop.systemd.test",
"/",
"org.freedesktop.systemd.test",
- "Exit",
+ "ExitClient1",
&q);
if (r < 0) {
log_error("Failed to allocate method call: %s", strerror(-r));
@@ -211,11 +248,114 @@ finish:
}
sd_bus_error_free(&error);
- return r;
+ return INT_TO_PTR(r);
+}
+
+static int quit_callback(sd_bus *b, int ret, sd_bus_message *m, void *userdata) {
+ bool *x = userdata;
+
+ log_error("Quit callback: %s", strerror(ret));
+
+ *x = 1;
+ return 1;
+}
+
+static void* client2(void*p) {
+ _cleanup_bus_message_unref_ sd_bus_message *m = NULL, *reply = NULL;
+ sd_bus *bus = NULL;
+ sd_bus_error error = SD_BUS_ERROR_INIT;
+ int r;
+ bool quit = false;
+
+ 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",
+ "Slow",
+ &m);
+ if (r < 0) {
+ log_error("Failed to allocate method call: %s", strerror(-r));
+ goto finish;
+ }
+
+ r = sd_bus_send_with_reply_and_block(bus, m, 200 * USEC_PER_MSEC, &error, &reply);
+ if (r < 0)
+ log_info("Failed to issue method call: %s", bus_error_message(&error, -r));
+ else
+ log_info("Slow call succeed.");
+
+ sd_bus_message_unref(m);
+ m = NULL;
+
+ r = sd_bus_message_new_method_call(
+ bus,
+ "org.freedesktop.systemd.test",
+ "/",
+ "org.freedesktop.systemd.test",
+ "Slow",
+ &m);
+ if (r < 0) {
+ log_error("Failed to allocate method call: %s", strerror(-r));
+ goto finish;
+ }
+
+ r = sd_bus_send_with_reply(bus, m, quit_callback, &quit, 200 * USEC_PER_MSEC, NULL);
+ if (r < 0) {
+ log_info("Failed to issue method call: %s", bus_error_message(&error, -r));
+ goto finish;
+ }
+
+ while (!quit) {
+ r = sd_bus_process(bus, NULL);
+ if (r < 0) {
+ log_error("Failed to process requests: %s", strerror(-r));
+ goto finish;
+ }
+ if (r == 0) {
+ r = sd_bus_wait(bus, (uint64_t) -1);
+ if (r < 0) {
+ log_error("Failed to wait: %s", strerror(-r));
+ goto finish;
+ }
+ }
+ }
+
+ 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",
+ "ExitClient2",
+ &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 INT_TO_PTR(r);
}
int main(int argc, char *argv[]) {
- pthread_t t;
+ pthread_t c1, c2;
sd_bus *bus;
void *p;
int q, r;
@@ -224,15 +364,22 @@ int main(int argc, char *argv[]) {
if (r < 0)
return EXIT_FAILURE;
- r = pthread_create(&t, NULL, server, bus);
- if (r != 0) {
- sd_bus_unref(bus);
+ log_info("Initialized...");
+
+ r = pthread_create(&c1, NULL, client1, bus);
+ if (r != 0)
return EXIT_FAILURE;
- }
- r = client();
+ r = pthread_create(&c2, NULL, client2, bus);
+ if (r != 0)
+ return EXIT_FAILURE;
- q = pthread_join(t, &p);
+ r = server(bus);
+
+ q = pthread_join(c1, &p);
+ if (q != 0)
+ return EXIT_FAILURE;
+ q = pthread_join(c2, &p);
if (q != 0)
return EXIT_FAILURE;
if (r < 0)