Commit e3017af9 authored by Lennart Poettering's avatar Lennart Poettering

bus: implement full method call timeout logic

parent 43b4c9aa
......@@ -1668,7 +1668,8 @@ libsystemd_bus_la_SOURCES = \
src/libsystemd-bus/bus-type.h
libsystemd_bus_la_LIBADD = \
libsystemd-id128-internal.la
libsystemd-id128-internal.la \
libsystemd-shared.la
noinst_LTLIBRARIES += \
libsystemd-bus.la
......
......@@ -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);
}
......@@ -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);
}
......@@ -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);
......@@ -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))
......@@ -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++;
}
}
......
......@@ -87,6 +87,7 @@ struct sd_bus_message {
struct iovec iovec[4];
unsigned n_iovec;
size_t size;
char *peeked_signature;
};
......
......@@ -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,
......
This diff is collapsed.
......@@ -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);
......
......@@ -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)
......
......@@ -53,6 +53,19 @@ void prioq_free(Prioq *q) {
free(q);
}
int prioq_ensure_allocated(Prioq **q, compare_func_t compare_func) {
assert(q);
if (*q)
return 0;
*q = prioq_new(compare_func);
if (!*q)
return -ENOMEM;
return 0;
}
static void swap(Prioq *q, unsigned j, unsigned k) {
void *saved_data;
unsigned *saved_idx;
......@@ -204,9 +217,12 @@ static struct prioq_item* find_item(Prioq *q, void *data, unsigned *idx) {
assert(q);
if (idx) {
assert(*idx < q->n_items);
if (*idx > q->n_items)
return NULL;
i = q->items + *idx;
assert(i->data == data);
if (i->data != data)
return NULL;
return i;
} else {
......
......@@ -27,6 +27,7 @@ typedef struct Prioq Prioq;
Prioq *prioq_new(compare_func_t compare);
void prioq_free(Prioq *q);
int prioq_ensure_allocated(Prioq **q, compare_func_t compare_func);
int prioq_put(Prioq *q, void *data, unsigned *idx);
int prioq_remove(Prioq *q, void *data, unsigned *idx);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment