chiark / gitweb /
sd-bus: add new call sd_bus_get_scope() for querying whether one is connected to...
[elogind.git] / src / libsystemd / sd-bus / sd-bus.c
index ea056d81191c0c34e70fb4f9f124d39ad398c1c8..9ce6bfab8faf9adb4e6a90b9607c0cce2a5d69bf 100644 (file)
@@ -1283,6 +1283,7 @@ _public_ int sd_bus_open_system_remote(sd_bus **ret, const char *host) {
 
         bus->bus_client = true;
         bus->trusted = false;
+        bus->is_system = true;
 
         r = sd_bus_start(bus);
         if (r < 0)
@@ -1335,6 +1336,7 @@ _public_ int sd_bus_open_system_container(sd_bus **ret, const char *machine) {
 
         bus->bus_client = true;
         bus->trusted = false;
+        bus->is_system = true;
 
         r = sd_bus_start(bus);
         if (r < 0)
@@ -2505,6 +2507,15 @@ null_message:
         return r;
 }
 
+static void bus_message_set_sender_local(sd_bus *bus, sd_bus_message *m) {
+        assert(bus);
+        assert(m);
+
+        m->sender = m->creds.unique_name = (char*) "org.freedesktop.DBus.Local";
+        m->creds.well_known_names_local = true;
+        m->creds.mask |= (SD_BUS_CREDS_UNIQUE_NAME|SD_BUS_CREDS_WELL_KNOWN_NAMES) & bus->creds_mask;
+}
+
 static int process_closing(sd_bus *bus, sd_bus_message **ret) {
         _cleanup_bus_message_unref_ sd_bus_message *m = NULL;
         struct reply_callback *c;
@@ -2573,7 +2584,7 @@ static int process_closing(sd_bus *bus, sd_bus_message **ret) {
         if (r < 0)
                 return r;
 
-        m->sender = "org.freedesktop.DBus.Local";
+        bus_message_set_sender_local(bus, m);
 
         r = bus_seal_synthetic_message(bus, m);
         if (r < 0)
@@ -3367,3 +3378,43 @@ int bus_get_root_path(sd_bus *bus) {
 
         return r;
 }
+
+_public_ int sd_bus_get_scope(sd_bus *bus, const char **scope) {
+        int r;
+
+        assert_return(bus, -EINVAL);
+        assert_return(scope, -EINVAL);
+        assert_return(!bus_pid_changed(bus), -ECHILD);
+
+        if (bus->is_kernel) {
+                _cleanup_free_ char *n = NULL;
+                const char *dash;
+
+                r = bus_kernel_get_bus_name(bus, &n);
+                if (r < 0)
+                        return r;
+
+                if (streq(n, "0-system")) {
+                        *scope = "system";
+                        return 1;
+                }
+
+                dash = strchr(n, '-');
+                if (streq(dash, "-user")) {
+                        *scope = "user";
+                        return 1;
+                }
+        }
+
+        if (bus->is_user) {
+                *scope = "user";
+                return 1;
+        }
+
+        if (bus->is_system) {
+                *scope = "system";
+                return 1;
+        }
+
+        return -ENODATA;
+}