#include "missing.h"
#include "def.h"
#include "cgroup-util.h"
+#include "bus-label.h"
#include "sd-bus.h"
#include "bus-internal.h"
#include "bus-util.h"
#include "bus-container.h"
#include "bus-protocol.h"
+#include "bus-track.h"
static int bus_poll(sd_bus *bus, bool need_more, uint64_t timeout_usec);
static int attach_io_events(sd_bus *b);
}
static void bus_reset_queues(sd_bus *b) {
- unsigned i;
-
assert(b);
- for (i = 0; i < b->rqueue_size; i++)
- sd_bus_message_unref(b->rqueue[i]);
+ while (b->rqueue_size > 0)
+ sd_bus_message_unref(b->rqueue[--b->rqueue_size]);
+
free(b->rqueue);
+ b->rqueue = NULL;
+ b->rqueue_allocated = 0;
- for (i = 0; i < b->wqueue_size; i++)
- sd_bus_message_unref(b->wqueue[i]);
- free(b->wqueue);
+ while (b->wqueue_size > 0)
+ sd_bus_message_unref(b->wqueue[--b->wqueue_size]);
- b->rqueue = b->wqueue = NULL;
- b->rqueue_allocated = b->wqueue_allocated = 0;
- b->rqueue_size = b->wqueue_size = 0;
+ free(b->wqueue);
+ b->wqueue = NULL;
+ b->wqueue_allocated = 0;
}
static void bus_free(sd_bus *b) {
assert(b);
+ assert(!b->track_queue);
+
sd_bus_detach_event(b);
+ if (b->default_bus_ptr)
+ *b->default_bus_ptr = NULL;
+
bus_close_fds(b);
if (b->kdbus_buffer)
r = sd_bus_message_new_method_call(
bus,
+ &m,
"org.freedesktop.DBus",
"/org/freedesktop/DBus",
"org.freedesktop.DBus",
- "Hello",
- &m);
+ "Hello");
if (r < 0)
return r;
b->sockaddr.un.sun_family = AF_UNIX;
strncpy(b->sockaddr.un.sun_path, "/var/run/dbus/system_bus_socket", sizeof(b->sockaddr.un.sun_path));
- b->sockaddr_size = offsetof(struct sockaddr_un, sun_path) + sizeof("/var/run/dbus/system_bus_socket") - 1;
+ b->sockaddr_size = offsetof(struct sockaddr_un, sun_path) + strlen("/var/run/dbus/system_bus_socket");
return 0;
}
#ifdef ENABLE_KDBUS
asprintf(&b->address, KERNEL_USER_BUS_FMT, (unsigned long) getuid());
#else
- return -ECONNREFUSED;
+ r = -ECONNREFUSED;
+ goto fail;
#endif
}
return r;
}
-_public_ int sd_bus_open_system_remote(const char *host, sd_bus **ret) {
+_public_ int sd_bus_open_system_remote(sd_bus **ret, const char *host) {
_cleanup_free_ char *e = NULL;
char *p = NULL;
sd_bus *bus;
return 0;
}
-_public_ int sd_bus_open_system_container(const char *machine, sd_bus **ret) {
+_public_ int sd_bus_open_system_container(sd_bus **ret, const char *machine) {
_cleanup_free_ char *e = NULL;
sd_bus *bus;
char *p;
if (!bus)
return NULL;
- i = REFCNT_DEC(bus->n_ref);
- if (i != bus->rqueue_size + bus->wqueue_size)
- return NULL;
+ if (REFCNT_GET(bus->n_ref) == bus->rqueue_size + bus->wqueue_size + 1) {
+ bool q = true;
+
+ for (i = 0; i < bus->rqueue_size; i++)
+ if (bus->rqueue[i]->n_ref > 1) {
+ q = false;
+ break;
+ }
- for (i = 0; i < bus->rqueue_size; i++)
- if (bus->rqueue[i]->n_ref > 1)
- return NULL;
+ if (q) {
+ for (i = 0; i < bus->wqueue_size; i++)
+ if (bus->wqueue[i]->n_ref > 1) {
+ q = false;
+ break;
+ }
+ }
- for (i = 0; i < bus->wqueue_size; i++)
- if (bus->wqueue[i]->n_ref > 1)
- return NULL;
+ /* We are the only holders on the messages, and the
+ * messages are the only holders on us, so let's drop
+ * the messages and thus implicitly also kill our own
+ * last references */
- /* we are the only holders on the messages */
- bus_free(bus);
+ if (q)
+ bus_reset_queues(bus);
+ }
+ i = REFCNT_DEC(bus->n_ref);
+ if (i > 0)
+ return NULL;
+
+ bus_free(bus);
return NULL;
}
return r;
if (bus->is_kernel || *idx >= BUS_MESSAGE_SIZE(m))
- log_debug("Sent message type=%s sender=%s destination=%s object=%s interface=%s member=%s cookie=%lu reply_cookie=%lu error=%s",
+ log_debug("Sent message type=%s sender=%s destination=%s object=%s interface=%s member=%s cookie=%" PRIu64 " reply_cookie=%" PRIu64 " error=%s",
bus_message_type_to_string(m->header->type),
strna(sd_bus_message_get_sender(m)),
strna(sd_bus_message_get_destination(m)),
strna(sd_bus_message_get_path(m)),
strna(sd_bus_message_get_interface(m)),
strna(sd_bus_message_get_member(m)),
- (unsigned long) BUS_MESSAGE_COOKIE(m),
- (unsigned long) m->reply_cookie,
+ BUS_MESSAGE_COOKIE(m),
+ m->reply_cookie,
strna(m->error.message));
return r;
assert_return(BUS_IS_OPEN(bus->state) || bus->state == BUS_CLOSING, -ENOTCONN);
assert_return(!bus_pid_changed(bus), -ECHILD);
+ if (bus->track_queue) {
+ *timeout_usec = 0;
+ return 1;
+ }
+
if (bus->state == BUS_CLOSING) {
*timeout_usec = 0;
return 1;
bus->current = m;
bus->iteration_counter++;
- log_debug("Got message type=%s sender=%s destination=%s object=%s interface=%s member=%s cookie=%lu reply_cookie=%lu error=%s",
+ log_debug("Got message type=%s sender=%s destination=%s object=%s interface=%s member=%s cookie=%" PRIu64 " reply_cookie=%" PRIu64 " error=%s",
bus_message_type_to_string(m->header->type),
strna(sd_bus_message_get_sender(m)),
strna(sd_bus_message_get_destination(m)),
strna(sd_bus_message_get_path(m)),
strna(sd_bus_message_get_interface(m)),
strna(sd_bus_message_get_member(m)),
- (unsigned long) BUS_MESSAGE_COOKIE(m),
- (unsigned long) m->reply_cookie,
+ BUS_MESSAGE_COOKIE(m),
+ m->reply_cookie,
strna(m->error.message));
r = process_hello(bus, m);
return r;
}
+static int dispatch_track(sd_bus *bus) {
+ assert(bus);
+
+ if (!bus->track_queue)
+ return 0;
+
+ bus_track_dispatch(bus->track_queue);
+ return 1;
+}
+
static int process_running(sd_bus *bus, bool hint_priority, int64_t priority, sd_bus_message **ret) {
_cleanup_bus_message_unref_ sd_bus_message *m = NULL;
int r;
if (r != 0)
goto null_message;
+ r = dispatch_track(bus);
+ if (r != 0)
+ goto null_message;
+
r = dispatch_rqueue(bus, hint_priority, priority, &m);
if (r < 0)
return r;
/* Then, synthesize a Disconnected message */
r = sd_bus_message_new_signal(
bus,
+ &m,
"/org/freedesktop/DBus/Local",
"org.freedesktop.DBus.Local",
- "Disconnected",
- &m);
+ "Disconnected");
if (r < 0)
return r;
return 0;
if (!bus->input_io_event_source) {
- r = sd_event_add_io(bus->event, bus->input_fd, 0, io_callback, bus, &bus->input_io_event_source);
+ r = sd_event_add_io(bus->event, &bus->input_io_event_source, bus->input_fd, 0, io_callback, bus);
if (r < 0)
return r;
assert(bus->output_fd >= 0);
if (!bus->output_io_event_source) {
- r = sd_event_add_io(bus->event, bus->output_fd, 0, io_callback, bus, &bus->output_io_event_source);
+ r = sd_event_add_io(bus->event, &bus->output_io_event_source, bus->output_fd, 0, io_callback, bus);
if (r < 0)
return r;
bus->event_priority = priority;
- r = sd_event_add_monotonic(bus->event, 0, 0, time_callback, bus, &bus->time_event_source);
+ r = sd_event_add_monotonic(bus->event, &bus->time_event_source, 0, 0, time_callback, bus);
if (r < 0)
goto fail;
if (r < 0)
goto fail;
- r = sd_event_add_exit(bus->event, quit_callback, bus, &bus->quit_event_source);
+ r = sd_event_add_exit(bus->event, &bus->quit_event_source, quit_callback, bus);
if (r < 0)
goto fail;
return -ENXIO;
}
-_public_ char *sd_bus_label_escape(const char *s) {
- char *r, *t;
- const char *f;
-
- assert_return(s, NULL);
-
- /* Escapes all chars that D-Bus' object path cannot deal
- * with. Can be reversed with bus_path_unescape(). We special
- * case the empty string. */
-
- if (*s == 0)
- return strdup("_");
-
- r = new(char, strlen(s)*3 + 1);
- if (!r)
- return NULL;
-
- for (f = s, t = r; *f; f++) {
+_public_ int sd_bus_path_encode(const char *prefix, const char *external_id, char **ret_path) {
+ _cleanup_free_ char *e = NULL;
+ char *ret;
- /* Escape everything that is not a-zA-Z0-9. We also
- * escape 0-9 if it's the first character */
+ assert_return(object_path_is_valid(prefix), -EINVAL);
+ assert_return(external_id, -EINVAL);
+ assert_return(ret_path, -EINVAL);
- if (!(*f >= 'A' && *f <= 'Z') &&
- !(*f >= 'a' && *f <= 'z') &&
- !(f > s && *f >= '0' && *f <= '9')) {
- *(t++) = '_';
- *(t++) = hexchar(*f >> 4);
- *(t++) = hexchar(*f);
- } else
- *(t++) = *f;
- }
+ e = bus_label_escape(external_id);
+ if (!e)
+ return -ENOMEM;
- *t = 0;
+ ret = strjoin(prefix, "/", e, NULL);
+ if (!ret)
+ return -ENOMEM;
- return r;
+ *ret_path = ret;
+ return 0;
}
-_public_ char *sd_bus_label_unescape(const char *f) {
- char *r, *t;
-
- assert_return(f, NULL);
-
- /* Special case for the empty string */
- if (streq(f, "_"))
- return strdup("");
-
- r = new(char, strlen(f) + 1);
- if (!r)
- return NULL;
-
- for (t = r; *f; f++) {
+_public_ int sd_bus_path_decode(const char *path, const char *prefix, char **external_id) {
+ const char *e;
+ char *ret;
- if (*f == '_') {
- int a, b;
+ assert_return(object_path_is_valid(path), -EINVAL);
+ assert_return(object_path_is_valid(prefix), -EINVAL);
+ assert_return(external_id, -EINVAL);
- if ((a = unhexchar(f[1])) < 0 ||
- (b = unhexchar(f[2])) < 0) {
- /* Invalid escape code, let's take it literal then */
- *(t++) = '_';
- } else {
- *(t++) = (char) ((a << 4) | b);
- f += 2;
- }
- } else
- *(t++) = *f;
+ e = object_path_startswith(path, prefix);
+ if (!e) {
+ *external_id = NULL;
+ return 0;
}
- *t = 0;
+ ret = bus_label_unescape(e);
+ if (!ret)
+ return -ENOMEM;
- return r;
+ *external_id = ret;
+ return 1;
}
_public_ int sd_bus_get_peer_creds(sd_bus *bus, uint64_t mask, sd_bus_creds **ret) {