chiark / gitweb /
sd-resolve: rework sd-resolve to be callback based, similar in style to sd-bus and...
[elogind.git] / src / libsystemd / sd-bus / bus-control.c
index db0c11b0fe4811a0631cc64238a6aead1a4d5bc3..ecf74919b67b4989446bb176636f236a3f63d074 100644 (file)
@@ -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
@@ -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
@@ -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)
@@ -735,11 +741,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
@@ -917,7 +925,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) {
@@ -1055,7 +1062,6 @@ int bus_add_match_internal_kernel(
         m = alloca0(sz);
         m->size = sz;
         m->cookie = cookie;
-        m->owner_id = id;
 
         item = m->items;
 
@@ -1098,13 +1104,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 +1129,7 @@ static int bus_add_match_internal_dbus1(
                         NULL,
                         NULL,
                         "s",
-                        match);
+                        e);
 }
 
 int bus_add_match_internal(
@@ -1128,14 +1143,13 @@ int bus_add_match_internal(
         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 +1160,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 +1172,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 +1188,7 @@ static int bus_remove_match_internal_dbus1(
                         NULL,
                         NULL,
                         "s",
-                        match);
+                        e);
 }
 
 int bus_remove_match_internal(
@@ -1183,7 +1200,7 @@ int bus_remove_match_internal(
         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 +1213,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);