X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=blobdiff_plain;f=src%2Flibsystemd-bus%2Fbus-control.c;h=177bd882ada36c569bfc68d31f65459bc775c61e;hb=d5a2b9a6f455468a0f29483303657ab4fd7013d8;hp=e4cc251a4b2937046fc159d6a6f7c089e4be3c54;hpb=de1c301ed165eb4d04a0c9d4babe97912b5233bb;p=elogind.git diff --git a/src/libsystemd-bus/bus-control.c b/src/libsystemd-bus/bus-control.c index e4cc251a4..177bd882a 100644 --- a/src/libsystemd-bus/bus-control.c +++ b/src/libsystemd-bus/bus-control.c @@ -19,6 +19,10 @@ along with systemd; If not, see . ***/ +#ifdef HAVE_VALGRIND_MEMCHECK_H +#include +#endif + #include #include @@ -27,16 +31,28 @@ #include "sd-bus.h" #include "bus-internal.h" #include "bus-message.h" +#include "bus-control.h" + +int sd_bus_get_unique_name(sd_bus *bus, const char **unique) { + int r; -const char *sd_bus_get_unique_name(sd_bus *bus) { if (!bus) - return NULL; + return -EINVAL; + if (!unique) + return -EINVAL; + if (bus_pid_changed(bus)) + return -ECHILD; + + r = bus_ensure_running(bus); + if (r < 0) + return r; - return bus->unique_name; + *unique = bus->unique_name; + return 0; } int sd_bus_request_name(sd_bus *bus, const char *name, int flags) { - _cleanup_bus_message_unref_ sd_bus_message *m = NULL, *reply = NULL; + _cleanup_bus_message_unref_ sd_bus_message *reply = NULL; uint32_t ret; int r; @@ -44,34 +60,57 @@ int sd_bus_request_name(sd_bus *bus, const char *name, int flags) { return -EINVAL; if (!name) return -EINVAL; - - r = sd_bus_message_new_method_call( - bus, - "org.freedesktop.DBus", - "/", - "org.freedesktop.DBus", - "RequestName", - &m); - if (r < 0) - return r; - - r = sd_bus_message_append(m, "su", name, flags); - if (r < 0) - return r; - - r = sd_bus_send_with_reply_and_block(bus, m, (uint64_t) -1, NULL, &reply); - if (r < 0) - return r; - - r = sd_bus_message_read(reply, "u", &ret); - if (r < 0) - return r; - - return ret; + if (!bus->bus_client) + return -EINVAL; + if (!BUS_IS_OPEN(bus->state)) + return -ENOTCONN; + if (bus_pid_changed(bus)) + return -ECHILD; + + if (bus->is_kernel) { + struct kdbus_cmd_name *n; + size_t l; + + l = strlen(name); + n = alloca0(offsetof(struct kdbus_cmd_name, name) + l + 1); + n->size = offsetof(struct kdbus_cmd_name, name) + l + 1; + n->name_flags = flags; + memcpy(n->name, name, l+1); + +#ifdef HAVE_VALGRIND_MEMCHECK_H + VALGRIND_MAKE_MEM_DEFINED(n, n->size); +#endif + + r = ioctl(bus->input_fd, KDBUS_CMD_NAME_ACQUIRE, n); + if (r < 0) + return -errno; + + return n->name_flags; + } else { + r = sd_bus_call_method( + bus, + "org.freedesktop.DBus", + "/", + "org.freedesktop.DBus", + "RequestName", + NULL, + &reply, + "su", + name, + flags); + if (r < 0) + return r; + + r = sd_bus_message_read(reply, "u", &ret); + if (r < 0) + return r; + + return ret; + } } int sd_bus_release_name(sd_bus *bus, const char *name) { - _cleanup_bus_message_unref_ sd_bus_message *m = NULL, *reply = NULL; + _cleanup_bus_message_unref_ sd_bus_message *reply = NULL; uint32_t ret; int r; @@ -79,35 +118,54 @@ int sd_bus_release_name(sd_bus *bus, const char *name) { return -EINVAL; if (!name) return -EINVAL; - - r = sd_bus_message_new_method_call( - bus, - "org.freedesktop.DBus", - "/", - "org.freedesktop.DBus", - "ReleaseName", - &m); - if (r < 0) - return r; - - r = sd_bus_message_append(m, "s", name); - if (r < 0) - return r; - - r = sd_bus_send_with_reply_and_block(bus, m, (uint64_t) -1, NULL, &reply); - if (r < 0) - return r; - - r = sd_bus_message_read(reply, "u", &ret); - if (r < 0) - return r; + if (!bus->bus_client) + return -EINVAL; + if (!BUS_IS_OPEN(bus->state)) + return -ENOTCONN; + if (bus_pid_changed(bus)) + return -ECHILD; + + if (bus->is_kernel) { + struct kdbus_cmd_name *n; + size_t l; + + l = strlen(name); + n = alloca0(offsetof(struct kdbus_cmd_name, name) + l + 1); + n->size = offsetof(struct kdbus_cmd_name, name) + l + 1; + memcpy(n->name, name, l+1); + +#ifdef HAVE_VALGRIND_MEMCHECK_H + VALGRIND_MAKE_MEM_DEFINED(n, n->size); +#endif + r = ioctl(bus->input_fd, KDBUS_CMD_NAME_RELEASE, n); + if (r < 0) + return -errno; + + return n->name_flags; + } else { + r = sd_bus_call_method( + bus, + "org.freedesktop.DBus", + "/", + "org.freedesktop.DBus", + "ReleaseName", + NULL, + &reply, + "s", + name); + if (r < 0) + return r; + + r = sd_bus_message_read(reply, "u", &ret); + if (r < 0) + return r; + } return ret; } int sd_bus_list_names(sd_bus *bus, char ***l) { - _cleanup_bus_message_unref_ sd_bus_message *m1 = NULL, *reply1 = NULL, *m2 = NULL, *reply2 = NULL; - _cleanup_strv_free_ char **a = NULL, **b = NULL; + _cleanup_bus_message_unref_ sd_bus_message *reply1 = NULL, *reply2 = NULL; char **x = NULL; int r; @@ -115,83 +173,97 @@ int sd_bus_list_names(sd_bus *bus, char ***l) { return -EINVAL; if (!l) return -EINVAL; + if (!BUS_IS_OPEN(bus->state)) + return -ENOTCONN; + if (bus_pid_changed(bus)) + return -ECHILD; - r = sd_bus_message_new_method_call( + r = sd_bus_call_method( bus, "org.freedesktop.DBus", "/", "org.freedesktop.DBus", "ListNames", - &m1); + NULL, + &reply1, + NULL); if (r < 0) return r; - r = sd_bus_message_new_method_call( + r = sd_bus_call_method( bus, "org.freedesktop.DBus", "/", "org.freedesktop.DBus", "ListActivatableNames", - &m2); + NULL, + &reply2, + NULL); if (r < 0) return r; - r = sd_bus_send_with_reply_and_block(bus, m1, (uint64_t) -1, NULL, &reply1); - if (r < 0) + r = bus_message_read_strv_extend(reply1, &x); + if (r < 0) { + strv_free(x); return r; + } - r = sd_bus_send_with_reply_and_block(bus, m2, (uint64_t) -1, NULL, &reply2); - if (r < 0) + r = bus_message_read_strv_extend(reply2, &x); + if (r < 0) { + strv_free(x); return r; - - r = sd_bus_message_read(reply1, "as", &a); - if (r < 0) - return r; - - r = sd_bus_message_read(reply2, "as", &b); - if (r < 0) - return r; - - x = strv_merge(a, b); - if (!x) - return -ENOMEM; + } *l = strv_uniq(x); return 0; } int sd_bus_get_owner(sd_bus *bus, const char *name, char **owner) { - _cleanup_bus_message_unref_ sd_bus_message *m = NULL, *reply = NULL; + _cleanup_bus_message_unref_ sd_bus_message *reply = NULL; + const char *found; int r; if (!bus) return -EINVAL; if (!name) return -EINVAL; + if (!BUS_IS_OPEN(bus->state)) + return -ENOTCONN; + if (bus_pid_changed(bus)) + return -ECHILD; - r = sd_bus_message_new_method_call( + r = sd_bus_call_method( bus, "org.freedesktop.DBus", "/", "org.freedesktop.DBus", "GetNameOwner", - &m); + NULL, + &reply, + "s", + name); if (r < 0) return r; - r = sd_bus_message_append(m, "s", name); + r = sd_bus_message_read(reply, "s", &found); if (r < 0) return r; - r = sd_bus_send_with_reply_and_block(bus, m, (uint64_t) -1, NULL, &reply); - if (r < 0) - return r; + if (owner) { + char *t; - return sd_bus_message_read(reply, "s", owner); + t = strdup(found); + if (!t) + return -ENOMEM; + + *owner = t; + } + + return 0; } int sd_bus_get_owner_uid(sd_bus *bus, const char *name, uid_t *uid) { - _cleanup_bus_message_unref_ sd_bus_message *m = NULL, *reply = NULL; + _cleanup_bus_message_unref_ sd_bus_message *reply = NULL; uint32_t u; int r; @@ -201,22 +273,21 @@ int sd_bus_get_owner_uid(sd_bus *bus, const char *name, uid_t *uid) { return -EINVAL; if (!uid) return -EINVAL; + if (!BUS_IS_OPEN(bus->state)) + return -ENOTCONN; + if (bus_pid_changed(bus)) + return -ECHILD; - r = sd_bus_message_new_method_call( + r = sd_bus_call_method( bus, "org.freedesktop.DBus", "/", "org.freedesktop.DBus", "GetConnectionUnixUser", - &m); - if (r < 0) - return r; - - r = sd_bus_message_append(m, "s", name); - if (r < 0) - return r; - - r = sd_bus_send_with_reply_and_block(bus, m, (uint64_t) -1, NULL, &reply); + NULL, + &reply, + "s", + name); if (r < 0) return r; @@ -229,7 +300,7 @@ int sd_bus_get_owner_uid(sd_bus *bus, const char *name, uid_t *uid) { } int sd_bus_get_owner_pid(sd_bus *bus, const char *name, pid_t *pid) { - _cleanup_bus_message_unref_ sd_bus_message *m = NULL, *reply = NULL; + _cleanup_bus_message_unref_ sd_bus_message *reply = NULL; uint32_t u; int r; @@ -239,22 +310,21 @@ int sd_bus_get_owner_pid(sd_bus *bus, const char *name, pid_t *pid) { return -EINVAL; if (!pid) return -EINVAL; + if (!BUS_IS_OPEN(bus->state)) + return -ENOTCONN; + if (bus_pid_changed(bus)) + return -ECHILD; - r = sd_bus_message_new_method_call( + r = sd_bus_call_method( bus, "org.freedesktop.DBus", "/", "org.freedesktop.DBus", - "GetConnectionUnixUser", - &m); - if (r < 0) - return r; - - r = sd_bus_message_append(m, "s", name); - if (r < 0) - return r; - - r = sd_bus_send_with_reply_and_block(bus, m, (uint64_t) -1, NULL, &reply); + "GetConnectionUnixProcessID", + NULL, + &reply, + "s", + name); if (r < 0) return r; @@ -269,54 +339,70 @@ int sd_bus_get_owner_pid(sd_bus *bus, const char *name, pid_t *pid) { return 0; } -int sd_bus_add_match(sd_bus *bus, const char *match) { - _cleanup_bus_message_unref_ sd_bus_message *m = NULL, *reply = NULL; - int r; - - if (!bus) - return -EINVAL; - if (!match) - return -EINVAL; +int bus_add_match_internal(sd_bus *bus, const char *match) { + assert(bus); + assert(match); - r = sd_bus_message_new_method_call( + return sd_bus_call_method( bus, "org.freedesktop.DBus", "/", "org.freedesktop.DBus", "AddMatch", - &m); - if (r < 0) - return r; + NULL, + NULL, + "s", + match); +} - r = sd_bus_message_append(m, "s", match); - if (r < 0) - return r; +int bus_remove_match_internal(sd_bus *bus, const char *match) { + assert(bus); + assert(match); - return sd_bus_send_with_reply_and_block(bus, m, (uint64_t) -1, NULL, &reply); + return sd_bus_call_method( + bus, + "org.freedesktop.DBus", + "/", + "org.freedesktop.DBus", + "RemoveMatch", + NULL, + NULL, + "s", + match); } -int sd_bus_remove_match(sd_bus *bus, const char *match) { - _cleanup_bus_message_unref_ sd_bus_message *m = NULL, *reply = NULL; +int sd_bus_get_owner_machine_id(sd_bus *bus, const char *name, sd_id128_t *machine) { + _cleanup_bus_message_unref_ sd_bus_message *reply = NULL; + const char *mid; int r; if (!bus) return -EINVAL; - if (!match) + if (!name) return -EINVAL; + if (!BUS_IS_OPEN(bus->state)) + return -ENOTCONN; + if (bus_pid_changed(bus)) + return -ECHILD; + + if (streq_ptr(name, bus->unique_name)) + return sd_id128_get_machine(machine); + + r = sd_bus_call_method(bus, + name, + "/", + "org.freedesktop.DBus.Peer", + "GetMachineId", + NULL, + &reply, + NULL); - r = sd_bus_message_new_method_call( - bus, - "org.freedesktop.DBus", - "/", - "org.freedesktop.DBus", - "RemoveMatch", - &m); if (r < 0) return r; - r = sd_bus_message_append(m, "s", match); + r = sd_bus_message_read(reply, "s", &mid); if (r < 0) return r; - return sd_bus_send_with_reply_and_block(bus, m, (uint64_t) -1, NULL, &reply); + return sd_id128_from_string(mid, machine); }