static int parse_tcp_address(sd_bus *b, const char **p, char **guid) {
_cleanup_free_ char *host = NULL, *port = NULL, *family = NULL;
- struct addrinfo hints, *result;
int r;
+ struct addrinfo *result, hints = {
+ .ai_socktype = SOCK_STREAM,
+ .ai_flags = AI_ADDRCONFIG,
+ };
assert(b);
assert(p);
if (!host || !port)
return -EINVAL;
- zero(hints);
- hints.ai_socktype = SOCK_STREAM;
- hints.ai_flags = AI_ADDRCONFIG;
-
if (family) {
if (streq(family, "ipv4"))
hints.ai_family = AF_INET;
if (r < 0)
return r;
- c = new(struct reply_callback, 1);
+ c = new0(struct reply_callback, 1);
if (!c)
return -ENOMEM;
/* Found a match! */
if (incoming->header->type == SD_BUS_MESSAGE_TYPE_METHOD_RETURN) {
- *reply = incoming;
+
+ if (reply)
+ *reply = incoming;
+ else
+ sd_bus_message_unref(incoming);
+
return 0;
}
assert(bus);
assert(m);
- LIST_FOREACH(callbacks, l, bus->filter_callbacks) {
- r = l->callback(bus, 0, m, l->userdata);
- if (r != 0)
- return r;
- }
+ do {
+ bus->filter_callbacks_modified = false;
+
+ LIST_FOREACH(callbacks, l, bus->filter_callbacks) {
+
+ if (bus->filter_callbacks_modified)
+ break;
+
+ /* Don't run this more than once per iteration */
+ if (l->last_iteration == bus->iteration_counter)
+ continue;
+
+ l->last_iteration = bus->iteration_counter;
+
+ r = l->callback(bus, 0, m, l->userdata);
+ if (r != 0)
+ return r;
+
+ }
+
+ } while (bus->filter_callbacks_modified);
return 0;
}
static int process_match(sd_bus *bus, sd_bus_message *m) {
+ int r;
+
assert(bus);
assert(m);
- return bus_match_run(bus, &bus->match_callbacks, 0, m);
+ do {
+ bus->match_callbacks_modified = false;
+
+ r = bus_match_run(bus, &bus->match_callbacks, 0, m);
+ if (r != 0)
+ return r;
+
+ } while (bus->match_callbacks_modified);
+
+ return 0;
}
static int process_builtin(sd_bus *bus, sd_bus_message *m) {
r = sd_bus_message_append(reply, "s", sd_id128_to_string(id, sid));
} else {
- _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_INIT;
+ _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
sd_bus_error_set(&error,
"org.freedesktop.DBus.Error.UnknownMethod",
}
static int process_object(sd_bus *bus, sd_bus_message *m) {
- _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_INIT;
+ _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
_cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
struct object_callback *c;
- char *p;
int r;
bool found = false;
+ size_t pl;
assert(bus);
assert(m);
if (hashmap_isempty(bus->object_callbacks))
return 0;
- c = hashmap_get(bus->object_callbacks, m->path);
- if (c) {
- r = c->callback(bus, 0, m, c->userdata);
- if (r != 0)
- return r;
+ pl = strlen(m->path);
- found = true;
- }
+ do {
+ char p[pl+1];
- /* Look for fallback prefixes */
- p = strdupa(m->path);
- for (;;) {
- char *e;
+ bus->object_callbacks_modified = false;
- e = strrchr(p, '/');
- if (e == p || !e)
- break;
+ c = hashmap_get(bus->object_callbacks, m->path);
+ if (c && c->last_iteration != bus->iteration_counter) {
- *e = 0;
+ c->last_iteration = bus->iteration_counter;
- c = hashmap_get(bus->object_callbacks, p);
- if (c && c->is_fallback) {
r = c->callback(bus, 0, m, c->userdata);
if (r != 0)
return r;
found = true;
}
- }
+
+ /* Look for fallback prefixes */
+ strcpy(p, m->path);
+ for (;;) {
+ char *e;
+
+ if (bus->object_callbacks_modified)
+ break;
+
+ e = strrchr(p, '/');
+ if (e == p || !e)
+ break;
+
+ *e = 0;
+
+ c = hashmap_get(bus->object_callbacks, p);
+ if (c && c->last_iteration != bus->iteration_counter && c->is_fallback) {
+
+ c->last_iteration = bus->iteration_counter;
+
+ r = c->callback(bus, 0, m, c->userdata);
+ if (r != 0)
+ return r;
+
+ found = true;
+ }
+ }
+
+ } while (bus->object_callbacks_modified);
/* We found some handlers but none wanted to take this, then
* return this -- with one exception, we can handle
assert(bus);
assert(m);
+ bus->iteration_counter++;
+
r = process_hello(bus, m);
if (r != 0)
return r;
if (m->header->type == SD_BUS_MESSAGE_TYPE_METHOD_CALL) {
_cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
- _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_INIT;
+ _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
sd_bus_error_set(&error, "org.freedesktop.DBus.Error.UnknownObject", "Unknown object '%s'.", m->path);
}
static int bus_poll(sd_bus *bus, bool need_more, uint64_t timeout_usec) {
- struct pollfd p[2];
+ struct pollfd p[2] = {};
int r, e, n;
struct timespec ts;
usec_t until, m;
if (timeout_usec != (uint64_t) -1 && (m == (uint64_t) -1 || timeout_usec < m))
m = timeout_usec;
- zero(p);
p[0].fd = bus->input_fd;
-
if (bus->output_fd == bus->input_fd) {
p[0].events = e;
n = 1;
if (!callback)
return -EINVAL;
- f = new(struct filter_callback, 1);
+ f = new0(struct filter_callback, 1);
if (!f)
return -ENOMEM;
f->callback = callback;
f->userdata = userdata;
+ bus->filter_callbacks_modified = true;
LIST_PREPEND(struct filter_callback, callbacks, bus->filter_callbacks, f);
return 0;
}
LIST_FOREACH(callbacks, f, bus->filter_callbacks) {
if (f->callback == callback && f->userdata == userdata) {
+ bus->filter_callbacks_modified = true;
LIST_REMOVE(struct filter_callback, callbacks, bus->filter_callbacks, f);
free(f);
return 1;
if (r < 0)
return r;
- c = new(struct object_callback, 1);
+ c = new0(struct object_callback, 1);
if (!c)
return -ENOMEM;
c->userdata = userdata;
c->is_fallback = fallback;
+ bus->object_callbacks_modified = true;
r = hashmap_put(bus->object_callbacks, c->path, c);
if (r < 0) {
free(c->path);
if (c->callback != callback || c->userdata != userdata || c->is_fallback != fallback)
return 0;
+ bus->object_callbacks_modified = true;
assert_se(c == hashmap_remove(bus->object_callbacks, c->path));
free(c->path);
}
if (callback) {
+ bus->match_callbacks_modified = true;
r = bus_match_add(&bus->match_callbacks, match, callback, userdata, NULL);
if (r < 0) {
if (bus->bus_client)
r = bus_remove_match_internal(bus, match);
- if (callback)
+ if (callback) {
+ bus->match_callbacks_modified = true;
q = bus_match_remove(&bus->match_callbacks, match, callback, userdata);
+ }
if (r < 0)
return r;
return q;
}
+
+int sd_bus_emit_signal(
+ sd_bus *bus,
+ const char *path,
+ const char *interface,
+ const char *member,
+ const char *types, ...) {
+
+ _cleanup_bus_message_unref_ sd_bus_message *m = NULL;
+ va_list ap;
+ int r;
+
+ if (!bus)
+ return -EINVAL;
+
+ r = sd_bus_message_new_signal(bus, path, interface, member, &m);
+ if (r < 0)
+ return r;
+
+ va_start(ap, types);
+ r = bus_message_append_ap(m, types, ap);
+ va_end(ap);
+ if (r < 0)
+ return r;
+
+ return sd_bus_send(bus, m, NULL);
+}
+
+int sd_bus_call_method(
+ sd_bus *bus,
+ const char *destination,
+ const char *path,
+ const char *interface,
+ const char *member,
+ sd_bus_error *error,
+ sd_bus_message **reply,
+ const char *types, ...) {
+
+ _cleanup_bus_message_unref_ sd_bus_message *m = NULL;
+ va_list ap;
+ int r;
+
+ if (!bus)
+ return -EINVAL;
+
+ r = sd_bus_message_new_method_call(bus, destination, path, interface, member, &m);
+ if (r < 0)
+ return r;
+
+ va_start(ap, types);
+ r = bus_message_append_ap(m, types, ap);
+ va_end(ap);
+ if (r < 0)
+ return r;
+
+ return sd_bus_send_with_reply_and_block(bus, m, 0, error, reply);
+}
+
+int sd_bus_reply_method_return(
+ sd_bus *bus,
+ sd_bus_message *call,
+ const char *types, ...) {
+
+ _cleanup_bus_message_unref_ sd_bus_message *m = NULL;
+ va_list ap;
+ int r;
+
+ if (!bus)
+ return -EINVAL;
+ if (!call)
+ return -EINVAL;
+ if (!call->sealed)
+ return -EPERM;
+ if (call->header->type != SD_BUS_MESSAGE_TYPE_METHOD_CALL)
+ return -EINVAL;
+
+ if (call->header->flags & SD_BUS_MESSAGE_NO_REPLY_EXPECTED)
+ return 0;
+
+ r = sd_bus_message_new_method_return(bus, call, &m);
+ if (r < 0)
+ return r;
+
+ va_start(ap, types);
+ r = bus_message_append_ap(m, types, ap);
+ va_end(ap);
+ if (r < 0)
+ return r;
+
+ return sd_bus_send(bus, m, NULL);
+}
+
+int sd_bus_reply_method_error(
+ sd_bus *bus,
+ sd_bus_message *call,
+ const sd_bus_error *e) {
+
+ _cleanup_bus_message_unref_ sd_bus_message *m = NULL;
+ int r;
+
+ if (!bus)
+ return -EINVAL;
+ if (!call)
+ return -EINVAL;
+ if (!call->sealed)
+ return -EPERM;
+ if (call->header->type != SD_BUS_MESSAGE_TYPE_METHOD_CALL)
+ return -EINVAL;
+ if (!sd_bus_error_is_set(e))
+ return -EINVAL;
+
+ if (call->header->flags & SD_BUS_MESSAGE_NO_REPLY_EXPECTED)
+ return 0;
+
+ r = sd_bus_message_new_method_error(bus, call, e, &m);
+ if (r < 0)
+ return r;
+
+ return sd_bus_send(bus, m, NULL);
+}