chiark / gitweb /
bus: properly detect and handle if a callback is installed/removed from within a...
[elogind.git] / src / libsystemd-bus / bus-control.c
index e4cc251a4b2937046fc159d6a6f7c089e4be3c54..dd404442cf1c95658fa57b8e8776030c92f79842 100644 (file)
 #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;
+
+        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) {
@@ -59,7 +69,7 @@ int sd_bus_request_name(sd_bus *bus, const char *name, int flags) {
         if (r < 0)
                 return r;
 
-        r = sd_bus_send_with_reply_and_block(bus, m, (uint64_t) -1, NULL, &reply);
+        r = sd_bus_send_with_reply_and_block(bus, m, 0, NULL, &reply);
         if (r < 0)
                 return r;
 
@@ -94,7 +104,7 @@ int sd_bus_release_name(sd_bus *bus, const char *name) {
         if (r < 0)
                 return r;
 
-        r = sd_bus_send_with_reply_and_block(bus, m, (uint64_t) -1, NULL, &reply);
+        r = sd_bus_send_with_reply_and_block(bus, m, 0, NULL, &reply);
         if (r < 0)
                 return r;
 
@@ -107,7 +117,6 @@ int sd_bus_release_name(sd_bus *bus, const char *name) {
 
 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;
         char **x = NULL;
         int r;
 
@@ -136,25 +145,25 @@ int sd_bus_list_names(sd_bus *bus, char ***l) {
         if (r < 0)
                 return r;
 
-        r = sd_bus_send_with_reply_and_block(bus, m1, (uint64_t) -1, NULL, &reply1);
+        r = sd_bus_send_with_reply_and_block(bus, m1, 0, NULL, &reply1);
         if (r < 0)
                 return r;
 
-        r = sd_bus_send_with_reply_and_block(bus, m2, (uint64_t) -1, NULL, &reply2);
+        r = sd_bus_send_with_reply_and_block(bus, m2, 0, NULL, &reply2);
         if (r < 0)
                 return r;
 
-        r = sd_bus_message_read(reply1, "as", &a);
-        if (r < 0)
+        r = bus_message_read_strv_extend(reply1, &x);
+        if (r < 0) {
+                strv_free(x);
                 return r;
+        }
 
-        r = sd_bus_message_read(reply2, "as", &b);
-        if (r < 0)
+        r = bus_message_read_strv_extend(reply2, &x);
+        if (r < 0) {
+                strv_free(x);
                 return r;
-
-        x = strv_merge(a, b);
-        if (!x)
-                return -ENOMEM;
+        }
 
         *l = strv_uniq(x);
         return 0;
@@ -162,6 +171,7 @@ int sd_bus_list_names(sd_bus *bus, char ***l) {
 
 int sd_bus_get_owner(sd_bus *bus, const char *name, char **owner) {
         _cleanup_bus_message_unref_ sd_bus_message *m = NULL, *reply = NULL;
+        const char *found;
         int r;
 
         if (!bus)
@@ -183,11 +193,25 @@ int sd_bus_get_owner(sd_bus *bus, const char *name, char **owner) {
         if (r < 0)
                 return r;
 
-        r = sd_bus_send_with_reply_and_block(bus, m, (uint64_t) -1, NULL, &reply);
+        r = sd_bus_send_with_reply_and_block(bus, m, 0, NULL, &reply);
         if (r < 0)
                 return r;
 
-        return sd_bus_message_read(reply, "s", owner);
+        r = sd_bus_message_read(reply, "s", &found);
+        if (r < 0)
+                return r;
+
+        if (owner) {
+                char *t;
+
+                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) {
@@ -216,7 +240,7 @@ int sd_bus_get_owner_uid(sd_bus *bus, const char *name, uid_t *uid) {
         if (r < 0)
                 return r;
 
-        r = sd_bus_send_with_reply_and_block(bus, m, (uint64_t) -1, NULL, &reply);
+        r = sd_bus_send_with_reply_and_block(bus, m, 0, NULL, &reply);
         if (r < 0)
                 return r;
 
@@ -245,7 +269,7 @@ int sd_bus_get_owner_pid(sd_bus *bus, const char *name, pid_t *pid) {
                         "org.freedesktop.DBus",
                         "/",
                         "org.freedesktop.DBus",
-                        "GetConnectionUnixUser",
+                        "GetConnectionUnixProcessID",
                         &m);
         if (r < 0)
                 return r;
@@ -254,7 +278,7 @@ int sd_bus_get_owner_pid(sd_bus *bus, const char *name, pid_t *pid) {
         if (r < 0)
                 return r;
 
-        r = sd_bus_send_with_reply_and_block(bus, m, (uint64_t) -1, NULL, &reply);
+        r = sd_bus_send_with_reply_and_block(bus, m, 0, NULL, &reply);
         if (r < 0)
                 return r;
 
@@ -269,14 +293,12 @@ 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) {
+int bus_add_match_internal(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;
+        assert(bus);
+        assert(match);
 
         r = sd_bus_message_new_method_call(
                         bus,
@@ -292,17 +314,15 @@ int sd_bus_add_match(sd_bus *bus, const char *match) {
         if (r < 0)
                 return r;
 
-        return sd_bus_send_with_reply_and_block(bus, m, (uint64_t) -1, NULL, &reply);
+        return sd_bus_send_with_reply_and_block(bus, m, 0, NULL, &reply);
 }
 
-int sd_bus_remove_match(sd_bus *bus, const char *match) {
+int bus_remove_match_internal(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;
+        assert(bus);
+        assert(match);
 
         r = sd_bus_message_new_method_call(
                         bus,
@@ -318,5 +338,5 @@ int sd_bus_remove_match(sd_bus *bus, const char *match) {
         if (r < 0)
                 return r;
 
-        return sd_bus_send_with_reply_and_block(bus, m, (uint64_t) -1, NULL, &reply);
+        return sd_bus_send_with_reply_and_block(bus, m, 0, NULL, &reply);
 }