X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=blobdiff_plain;f=src%2Flibsystemd%2Fsd-bus%2Fsd-bus.c;h=118769086e811869d1a459da99341fa43dc012eb;hb=f4d140e9a60ca76d36d4539059e463a5db6a3a9d;hp=2c458f311c0912aaef1e85e15d89256376cf8624;hpb=069f5e61eb128aa08b6fdff12fc6ac71c3897b48;p=elogind.git diff --git a/src/libsystemd/sd-bus/sd-bus.c b/src/libsystemd/sd-bus/sd-bus.c index 2c458f311..118769086 100644 --- a/src/libsystemd/sd-bus/sd-bus.c +++ b/src/libsystemd/sd-bus/sd-bus.c @@ -35,6 +35,7 @@ #include "set.h" #include "missing.h" #include "def.h" +#include "cgroup-util.h" #include "sd-bus.h" #include "bus-internal.h" @@ -106,21 +107,21 @@ static void bus_node_destroy(sd_bus *b, struct node *n) { } 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) { @@ -131,6 +132,9 @@ static void bus_free(sd_bus *b) { sd_bus_detach_event(b); + if (b->default_bus_ptr) + *b->default_bus_ptr = NULL; + bus_close_fds(b); if (b->kdbus_buffer) @@ -144,6 +148,7 @@ static void bus_free(sd_bus *b) { free(b->machine); free(b->fake_label); free(b->cgroup_root); + free(b->connection_name); free(b->exec_path); strv_free(b->exec_argv); @@ -284,7 +289,7 @@ _public_ int sd_bus_negotiate_fds(sd_bus *bus, int b) { return 0; } -_public_ int sd_bus_negotiate_attach_timestamp(sd_bus *bus, int b) { +_public_ int sd_bus_negotiate_timestamp(sd_bus *bus, int b) { assert_return(bus, -EINVAL); assert_return(bus->state == BUS_UNSET, -EPERM); assert_return(!bus_pid_changed(bus), -ECHILD); @@ -293,7 +298,7 @@ _public_ int sd_bus_negotiate_attach_timestamp(sd_bus *bus, int b) { return 0; } -_public_ int sd_bus_negotiate_attach_creds(sd_bus *bus, uint64_t mask) { +_public_ int sd_bus_negotiate_creds(sd_bus *bus, uint64_t mask) { assert_return(bus, -EINVAL); assert_return(mask <= _SD_BUS_CREDS_ALL, -EINVAL); assert_return(bus->state == BUS_UNSET, -EPERM); @@ -334,6 +339,24 @@ _public_ int sd_bus_set_trusted(sd_bus *bus, int b) { return 0; } +_public_ int sd_bus_set_name(sd_bus *bus, const char *name) { + char *n; + + assert_return(bus, -EINVAL); + assert_return(name, -EINVAL); + assert_return(bus->state == BUS_UNSET, -EPERM); + assert_return(!bus_pid_changed(bus), -ECHILD); + + n = strdup(name); + if (!n) + return -ENOMEM; + + free(bus->connection_name); + bus->connection_name = n; + + return 0; +} + static int hello_callback(sd_bus *bus, sd_bus_message *reply, void *userdata, sd_bus_error *error) { const char *s; int r; @@ -1033,6 +1056,60 @@ _public_ int sd_bus_start(sd_bus *bus) { return bus_send_hello(bus); } +_public_ int sd_bus_open(sd_bus **ret) { + const char *e; + sd_bus *b; + int r; + + assert_return(ret, -EINVAL); + + /* Let's connect to the starter bus if it is set, and + * otherwise to the bus that is appropropriate for the scope + * we are running in */ + + e = secure_getenv("DBUS_STARTER_BUS_TYPE"); + if (e) { + if (streq(e, "system")) + return sd_bus_open_system(ret); + else if (streq(e, "session") || streq(e, "user")) + return sd_bus_open_user(ret); + } + + e = secure_getenv("DBUS_STARTER_ADDRESS"); + if (!e) { + if (cg_pid_get_owner_uid(0, NULL) >= 0) + return sd_bus_open_user(ret); + else + return sd_bus_open_system(ret); + } + + r = sd_bus_new(&b); + if (r < 0) + return r; + + r = sd_bus_set_address(b, e); + if (r < 0) + goto fail; + + b->bus_client = true; + + /* We don't know whether the bus is trusted or not, so better + * be safe, and authenticate everything */ + b->trusted = false; + b->attach_flags |= KDBUS_ATTACH_CAPS | KDBUS_ATTACH_CREDS; + + r = sd_bus_start(b); + if (r < 0) + goto fail; + + *ret = b; + return 0; + +fail: + bus_free(b); + return r; +} + _public_ int sd_bus_open_system(sd_bus **ret) { const char *e; sd_bus *b; @@ -1053,6 +1130,7 @@ _public_ int sd_bus_open_system(sd_bus **ret) { goto fail; b->bus_client = true; + b->is_system = true; /* Let's do per-method access control on the system bus. We * need the caller's UID and capability set for that. */ @@ -1118,6 +1196,7 @@ _public_ int sd_bus_open_user(sd_bus **ret) { } b->bus_client = true; + b->is_user = true; /* We don't do any per-method access control on the user * bus. */ @@ -1264,21 +1343,36 @@ _public_ sd_bus *sd_bus_unref(sd_bus *bus) { 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) - return NULL; + for (i = 0; i < bus->rqueue_size; i++) + if (bus->rqueue[i]->n_ref > 1) { + q = false; + break; + } - for (i = 0; i < bus->wqueue_size; i++) - if (bus->wqueue[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; + } + } - /* we are the only holders on the messages */ - bus_free(bus); + /* 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 */ + bus_reset_queues(bus); + } + + i = REFCNT_DEC(bus->n_ref); + if (i > 0) + return NULL; + + bus_free(bus); return NULL; } @@ -1437,11 +1531,11 @@ static int dispatch_wqueue(sd_bus *bus) { return ret; } -static int bus_read_message(sd_bus *bus) { +static int bus_read_message(sd_bus *bus, bool hint_priority, int64_t priority) { assert(bus); if (bus->is_kernel) - return bus_kernel_read_message(bus); + return bus_kernel_read_message(bus, hint_priority, priority); else return bus_socket_read_message(bus); } @@ -1458,13 +1552,17 @@ int bus_rqueue_make_room(sd_bus *bus) { return 0; } -static int dispatch_rqueue(sd_bus *bus, sd_bus_message **m) { +static int dispatch_rqueue(sd_bus *bus, bool hint_priority, int64_t priority, sd_bus_message **m) { int r, ret = 0; assert(bus); assert(m); assert(bus->state == BUS_RUNNING || bus->state == BUS_HELLO); + /* Note that the priority logic is only available on kdbus, + * where the rqueue is unused. We check the rqueue here + * anyway, because it's simple... */ + for (;;) { if (bus->rqueue_size > 0) { /* Dispatch a queued message */ @@ -1476,7 +1574,7 @@ static int dispatch_rqueue(sd_bus *bus, sd_bus_message **m) { } /* Try to read a new message */ - r = bus_read_message(bus); + r = bus_read_message(bus, hint_priority, priority); if (r < 0) return r; if (r == 0) @@ -1816,7 +1914,7 @@ _public_ int sd_bus_call( i++; } - r = bus_read_message(bus); + r = bus_read_message(bus, false, 0); if (r < 0) { if (r == -ENOTCONN || r == -ECONNRESET || r == -EPIPE || r == -ESHUTDOWN) { bus_enter_closing(bus); @@ -2182,7 +2280,7 @@ finish: return r; } -static int process_running(sd_bus *bus, sd_bus_message **ret) { +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; @@ -2197,7 +2295,7 @@ static int process_running(sd_bus *bus, sd_bus_message **ret) { if (r != 0) goto null_message; - r = dispatch_rqueue(bus, &m); + r = dispatch_rqueue(bus, hint_priority, priority, &m); if (r < 0) return r; if (!m) @@ -2323,7 +2421,7 @@ finish: return r; } -_public_ int sd_bus_process(sd_bus *bus, sd_bus_message **ret) { +static int bus_process_internal(sd_bus *bus, bool hint_priority, int64_t priority, sd_bus_message **ret) { BUS_DONT_DESTROY(bus); int r; @@ -2372,7 +2470,7 @@ _public_ int sd_bus_process(sd_bus *bus, sd_bus_message **ret) { case BUS_RUNNING: case BUS_HELLO: - r = process_running(bus, ret); + r = process_running(bus, hint_priority, priority, ret); if (r == -ENOTCONN || r == -ECONNRESET || r == -EPIPE || r == -ESHUTDOWN) { bus_enter_closing(bus); r = 1; @@ -2390,6 +2488,14 @@ _public_ int sd_bus_process(sd_bus *bus, sd_bus_message **ret) { assert_not_reached("Unknown state"); } +_public_ int sd_bus_process(sd_bus *bus, sd_bus_message **ret) { + return bus_process_internal(bus, false, 0, ret); +} + +_public_ int sd_bus_process_priority(sd_bus *bus, int64_t priority, sd_bus_message **ret) { + return bus_process_internal(bus, true, priority, ret); +} + static int bus_poll(sd_bus *bus, bool need_more, uint64_t timeout_usec) { struct pollfd p[2] = {}; int r, e, n; @@ -2880,6 +2986,43 @@ _public_ int sd_bus_default_user(sd_bus **ret) { return bus_default(sd_bus_open_user, &default_user_bus, ret); } +_public_ int sd_bus_default(sd_bus **ret) { + + const char *e; + + /* Let's try our best to reuse another cached connection. If + * the starter bus type is set, connect via our normal + * connection logic, ignoring $DBUS_STARTER_ADDRESS, so that + * we can share the connection with the user/system default + * bus. */ + + e = secure_getenv("DBUS_STARTER_BUS_TYPE"); + if (e) { + if (streq(e, "system")) + return sd_bus_default_system(ret); + else if (streq(e, "user") || streq(e, "session")) + return sd_bus_default_user(ret); + } + + /* No type is specified, so we have not other option than to + * use the starter address if it is set. */ + + e = secure_getenv("DBUS_STARTER_ADDRESS"); + if (e) { + static thread_local sd_bus *default_starter_bus = NULL; + + return bus_default(sd_bus_open, &default_starter_bus, ret); + } + + /* Finally, if nothing is set use the cached connection for + * the right scope */ + + if (cg_pid_get_owner_uid(0, NULL) >= 0) + return sd_bus_default_user(ret); + else + return sd_bus_default_system(ret); +} + _public_ int sd_bus_get_tid(sd_bus *b, pid_t *tid) { assert_return(b, -EINVAL); assert_return(tid, -EINVAL); @@ -3034,3 +3177,12 @@ _public_ int sd_bus_try_close(sd_bus *bus) { sd_bus_close(bus); return 0; } + +_public_ int sd_bus_get_name(sd_bus *bus, const char **name) { + assert_return(bus, -EINVAL); + assert_return(name, -EINVAL); + assert_return(!bus_pid_changed(bus), -ECHILD); + + *name = bus->connection_name; + return 0; +}