chiark / gitweb /
bus: add macro for iterating through body parts of a message
[elogind.git] / src / libsystemd-bus / bus-control.c
index 185f77c968d2873e67b1ed5612dc18c63cf2be20..a4dc9bf511e92584a1dc81c0e1cc4267e5410755 100644 (file)
   along with systemd; If not, see <http://www.gnu.org/licenses/>.
 ***/
 
+#ifdef HAVE_VALGRIND_MEMCHECK_H
+#include <valgrind/memcheck.h>
+#endif
+
 #include <stddef.h>
 #include <errno.h>
 
@@ -62,12 +66,15 @@ int sd_bus_request_name(sd_bus *bus, const char *name, int flags) {
                 size_t l;
 
                 l = strlen(name);
-                n = alloca(offsetof(struct kdbus_cmd_name, name) + l + 1);
+                n = alloca0(offsetof(struct kdbus_cmd_name, name) + l + 1);
                 n->size = offsetof(struct kdbus_cmd_name, name) + l + 1;
                 n->name_flags = flags;
-                n->id = 0;
                 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;
@@ -113,12 +120,13 @@ int sd_bus_release_name(sd_bus *bus, const char *name) {
                 size_t l;
 
                 l = strlen(name);
-                n = alloca(offsetof(struct kdbus_cmd_name, name) + l + 1);
+                n = alloca0(offsetof(struct kdbus_cmd_name, name) + l + 1);
                 n->size = offsetof(struct kdbus_cmd_name, name) + l + 1;
-                n->name_flags = 0;
-                n->id = 0;
                 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;
@@ -336,3 +344,35 @@ int bus_remove_match_internal(sd_bus *bus, const char *match) {
                         "s",
                         match);
 }
+
+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 (!name)
+                return -EINVAL;
+
+        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);
+
+        if (r < 0)
+                return r;
+
+        r = sd_bus_message_read(reply, "s", &mid);
+        if (r < 0)
+                return r;
+
+        return sd_id128_from_string(mid, machine);
+}