chiark / gitweb /
util: avoid non-portable __WORDSIZE
[elogind.git] / src / libsystemd / sd-bus / bus-control.c
index db0c11b0fe4811a0631cc64238a6aead1a4d5bc3..5ac48c081f9bac5297dfe44bd325e4094ec052ff 100644 (file)
@@ -60,7 +60,7 @@ static int bus_request_name_kernel(sd_bus *bus, const char *name, uint64_t flags
 
         l = strlen(name);
         size = offsetof(struct kdbus_cmd_name, name) + l + 1;
-        n = alloca0(size);
+        n = alloca0_align(size, 8);
         n->size = size;
         kdbus_translate_request_name_flags(flags, (uint64_t *) &n->flags);
         memcpy(n->name, name, l+1);
@@ -128,12 +128,14 @@ _public_ int sd_bus_request_name(sd_bus *bus, const char *name, uint64_t flags)
         assert_return(bus, -EINVAL);
         assert_return(name, -EINVAL);
         assert_return(bus->bus_client, -EINVAL);
-        assert_return(BUS_IS_OPEN(bus->state), -ENOTCONN);
         assert_return(!bus_pid_changed(bus), -ECHILD);
         assert_return(!(flags & ~(SD_BUS_NAME_ALLOW_REPLACEMENT|SD_BUS_NAME_REPLACE_EXISTING|SD_BUS_NAME_QUEUE)), -EINVAL);
         assert_return(service_name_is_valid(name), -EINVAL);
         assert_return(name[0] != ':', -EINVAL);
 
+        if (!BUS_IS_OPEN(bus->state))
+                return -ENOTCONN;
+
         if (bus->is_kernel)
                 return bus_request_name_kernel(bus, name, flags);
         else
@@ -149,7 +151,7 @@ static int bus_release_name_kernel(sd_bus *bus, const char *name) {
         assert(name);
 
         l = strlen(name);
-        n = alloca0(offsetof(struct kdbus_cmd_name, name) + l + 1);
+        n = alloca0_align(offsetof(struct kdbus_cmd_name, name) + l + 1, 8);
         n->size = offsetof(struct kdbus_cmd_name, name) + l + 1;
         memcpy(n->name, name, l+1);
 
@@ -201,11 +203,13 @@ _public_ int sd_bus_release_name(sd_bus *bus, const char *name) {
         assert_return(bus, -EINVAL);
         assert_return(name, -EINVAL);
         assert_return(bus->bus_client, -EINVAL);
-        assert_return(BUS_IS_OPEN(bus->state), -ENOTCONN);
         assert_return(!bus_pid_changed(bus), -ECHILD);
         assert_return(service_name_is_valid(name), -EINVAL);
         assert_return(name[0] != ':', -EINVAL);
 
+        if (!BUS_IS_OPEN(bus->state))
+                return -ENOTCONN;
+
         if (bus->is_kernel)
                 return bus_release_name_kernel(bus, name);
         else
@@ -342,9 +346,11 @@ static int bus_list_names_dbus1(sd_bus *bus, char ***acquired, char ***activatab
 _public_ int sd_bus_list_names(sd_bus *bus, char ***acquired, char ***activatable) {
         assert_return(bus, -EINVAL);
         assert_return(acquired || activatable, -EINVAL);
-        assert_return(BUS_IS_OPEN(bus->state), -ENOTCONN);
         assert_return(!bus_pid_changed(bus), -ECHILD);
 
+        if (!BUS_IS_OPEN(bus->state))
+                return -ENOTCONN;
+
         if (bus->is_kernel)
                 return bus_list_names_kernel(bus, acquired, activatable);
         else
@@ -370,11 +376,11 @@ static int bus_get_owner_kdbus(
                 return r;
         if (r > 0) {
                 size = offsetof(struct kdbus_cmd_conn_info, name);
-                cmd = alloca0(size);
+                cmd = alloca0_align(size, 8);
                 cmd->id = id;
         } else {
                 size = offsetof(struct kdbus_cmd_conn_info, name) + strlen(name) + 1;
-                cmd = alloca0(size);
+                cmd = alloca0_align(size, 8);
                 strcpy(cmd->name, name);
         }
 
@@ -389,7 +395,7 @@ static int bus_get_owner_kdbus(
 
         /* Non-activated names are considered not available */
         if (conn_info->flags & KDBUS_HELLO_ACTIVATOR)
-                return name[0] == ':' ? -ENXIO : -ENOENT;
+                return name[0] == ':' ? -ENXIO : -ESRCH;
 
         c = bus_creds_new();
         if (!c)
@@ -489,11 +495,9 @@ static int bus_get_owner_kdbus(
                                         goto fail;
                                 }
 
-                                if (!bus->cgroup_root) {
-                                        r = cg_get_root_path(&bus->cgroup_root);
-                                        if (r < 0)
-                                                goto fail;
-                                }
+                                r = bus_get_root_path(bus);
+                                if (r < 0)
+                                        goto fail;
 
                                 c->cgroup_root = strdup(bus->cgroup_root);
                                 if (!c->cgroup_root) {
@@ -735,11 +739,13 @@ _public_ int sd_bus_get_owner(
         assert_return(name, -EINVAL);
         assert_return(mask <= _SD_BUS_CREDS_ALL, -ENOTSUP);
         assert_return(mask == 0 || creds, -EINVAL);
-        assert_return(BUS_IS_OPEN(bus->state), -ENOTCONN);
         assert_return(!bus_pid_changed(bus), -ECHILD);
         assert_return(service_name_is_valid(name), -EINVAL);
         assert_return(bus->bus_client, -ENODATA);
 
+        if (!BUS_IS_OPEN(bus->state))
+                return -ENOTCONN;
+
         if (bus->is_kernel)
                 return bus_get_owner_kdbus(bus, name, mask, creds);
         else
@@ -821,7 +827,7 @@ static int add_name_change_match(sd_bus *bus,
                             offsetof(struct kdbus_notify_name_change, name) +
                             l);
 
-                m = alloca0(sz);
+                m = alloca0_align(sz, 8);
                 m->size = sz;
                 m->cookie = cookie;
 
@@ -881,7 +887,7 @@ static int add_name_change_match(sd_bus *bus,
                             offsetof(struct kdbus_item, id_change) +
                             sizeof(struct kdbus_notify_id_change));
 
-                m = alloca0(sz);
+                m = alloca0_align(sz, 8);
                 m->size = sz;
                 m->cookie = cookie;
 
@@ -917,7 +923,6 @@ static int add_name_change_match(sd_bus *bus,
 
 int bus_add_match_internal_kernel(
                 sd_bus *bus,
-                uint64_t id,
                 struct bus_match_component *components,
                 unsigned n_components,
                 uint64_t cookie) {
@@ -1052,10 +1057,9 @@ int bus_add_match_internal_kernel(
         if (using_bloom)
                 sz += ALIGN8(offsetof(struct kdbus_item, data64) + bus->bloom_size);
 
-        m = alloca0(sz);
+        m = alloca0_align(sz, 8);
         m->size = sz;
         m->cookie = cookie;
-        m->owner_id = id;
 
         item = m->items;
 
@@ -1098,13 +1102,22 @@ int bus_add_match_internal_kernel(
         return 0;
 }
 
+#define internal_match(bus, m)                                          \
+        ((bus)->hello_flags & KDBUS_HELLO_MONITOR                       \
+         ? (isempty(m) ? "eavesdrop='true'" : strappenda((m), ",eavesdrop='true'")) \
+         : (m))
+
 static int bus_add_match_internal_dbus1(
                 sd_bus *bus,
                 const char *match) {
 
+        const char *e;
+
         assert(bus);
         assert(match);
 
+        e = internal_match(bus, match);
+
         return sd_bus_call_method(
                         bus,
                         "org.freedesktop.DBus",
@@ -1114,7 +1127,7 @@ static int bus_add_match_internal_dbus1(
                         NULL,
                         NULL,
                         "s",
-                        match);
+                        e);
 }
 
 int bus_add_match_internal(
@@ -1125,17 +1138,15 @@ int bus_add_match_internal(
                 uint64_t cookie) {
 
         assert(bus);
-        assert(match);
 
         if (bus->is_kernel)
-                return bus_add_match_internal_kernel(bus, 0, components, n_components, cookie);
+                return bus_add_match_internal_kernel(bus, components, n_components, cookie);
         else
                 return bus_add_match_internal_dbus1(bus, match);
 }
 
 int bus_remove_match_internal_kernel(
                 sd_bus *bus,
-                uint64_t id,
                 uint64_t cookie) {
 
         struct kdbus_cmd_match m;
@@ -1146,7 +1157,6 @@ int bus_remove_match_internal_kernel(
         zero(m);
         m.size = offsetof(struct kdbus_cmd_match, items);
         m.cookie = cookie;
-        m.owner_id = id;
 
         r = ioctl(bus->input_fd, KDBUS_CMD_MATCH_REMOVE, &m);
         if (r < 0)
@@ -1159,9 +1169,13 @@ static int bus_remove_match_internal_dbus1(
                 sd_bus *bus,
                 const char *match) {
 
+        const char *e;
+
         assert(bus);
         assert(match);
 
+        e = internal_match(bus, match);
+
         return sd_bus_call_method(
                         bus,
                         "org.freedesktop.DBus",
@@ -1171,7 +1185,7 @@ static int bus_remove_match_internal_dbus1(
                         NULL,
                         NULL,
                         "s",
-                        match);
+                        e);
 }
 
 int bus_remove_match_internal(
@@ -1180,10 +1194,9 @@ int bus_remove_match_internal(
                 uint64_t cookie) {
 
         assert(bus);
-        assert(match);
 
         if (bus->is_kernel)
-                return bus_remove_match_internal_kernel(bus, 0, cookie);
+                return bus_remove_match_internal_kernel(bus, cookie);
         else
                 return bus_remove_match_internal_dbus1(bus, match);
 }
@@ -1196,10 +1209,12 @@ _public_ int sd_bus_get_owner_machine_id(sd_bus *bus, const char *name, sd_id128
         assert_return(bus, -EINVAL);
         assert_return(name, -EINVAL);
         assert_return(machine, -EINVAL);
-        assert_return(BUS_IS_OPEN(bus->state), -ENOTCONN);
         assert_return(!bus_pid_changed(bus), -ECHILD);
         assert_return(service_name_is_valid(name), -EINVAL);
 
+        if (!BUS_IS_OPEN(bus->state))
+                return -ENOTCONN;
+
         if (streq_ptr(name, bus->unique_name))
                 return sd_id128_get_machine(machine);