chiark / gitweb /
bus: make sure exit-on-idle logic works on kdbus systems that do not generate NameLost
[elogind.git] / src / libsystemd-bus / bus-util.c
index bd005262ebc9d0cb082c7f68b6ad58dac445b541..3afcb82bcbe25c776d56e1861c0a6bd795695df1 100644 (file)
@@ -48,16 +48,29 @@ static int quit_callback(sd_bus *bus, sd_bus_message *m, void *userdata, sd_bus_
 
 int bus_async_unregister_and_quit(sd_event *e, sd_bus *bus, const char *name) {
         _cleanup_free_ char *match = NULL;
+        const char *unique;
         int r;
 
         assert(e);
         assert(bus);
         assert(name);
 
-        r = asprintf(&match, "type='signal',sender='org.freedesktop.DBus',interface='org.freedesktop.DBus',member='NameLost',arg0='%s'", name);
+        r = sd_bus_get_unique_name(bus, &unique);
         if (r < 0)
                 return r;
 
+        r = asprintf(&match,
+                     "sender='org.freedesktop.DBus',"
+                     "type='signal',"
+                     "interface='org.freedesktop.DBus',"
+                     "member='NameOwnerChanged',"
+                     "path='/org/freedesktop/DBus',"
+                     "arg0='%s',"
+                     "arg1='%s',"
+                     "arg2=''", name, unique);
+        if (r < 0)
+                return -ENOMEM;
+
         r = sd_bus_add_match(bus, match, quit_callback, e);
         if (r < 0)
                 return r;
@@ -66,9 +79,6 @@ int bus_async_unregister_and_quit(sd_event *e, sd_bus *bus, const char *name) {
         if (r < 0)
                 return r;
 
-        if (r != SD_BUS_NAME_RELEASED)
-                return -EIO;
-
         return 0;
 }
 
@@ -439,21 +449,43 @@ int bus_open_system_systemd(sd_bus **_bus) {
         if (geteuid() != 0)
                 return sd_bus_open_system(_bus);
 
-        /* If we are root, then let's talk directly to the system
-         * instance, instead of going via the bus */
+        /* If we are root and kdbus is not available, then let's talk
+         * directly to the system instance, instead of going via the
+         * bus */
 
+#ifdef ENABLE_KDBUS
         r = sd_bus_new(&bus);
         if (r < 0)
                 return r;
 
-        r = sd_bus_set_address(bus, "unix:path=/run/systemd/private");
+        r = sd_bus_set_address(bus, "kernel:path=/dev/kdbus/0-system/bus");
         if (r < 0)
                 return r;
 
+        bus->bus_client = true;
+
         r = sd_bus_start(bus);
+        if (r >= 0) {
+                *_bus = bus;
+                bus = NULL;
+                return 0;
+        }
+
+        bus = sd_bus_unref(bus);
+#endif
+
+        r = sd_bus_new(&bus);
+        if (r < 0)
+                return r;
+
+        r = sd_bus_set_address(bus, "unix:path=/run/systemd/private");
         if (r < 0)
                 return r;
 
+        r = sd_bus_start(bus);
+        if (r < 0)
+                return sd_bus_open_system(_bus);
+
         r = bus_check_peercred(bus);
         if (r < 0)
                 return r;
@@ -466,34 +498,53 @@ int bus_open_system_systemd(sd_bus **_bus) {
 
 int bus_open_user_systemd(sd_bus **_bus) {
         _cleanup_bus_unref_ sd_bus *bus = NULL;
-        _cleanup_free_ char *p = NULL;
+        _cleanup_free_ char *ee = NULL;
         const char *e;
         int r;
 
-        /* If we are supposed to talk to the instance, try via
-         * XDG_RUNTIME_DIR first, then fallback to normal bus
-         * access */
+        /* Try via kdbus first, and then directly */
 
         assert(_bus);
 
+#ifdef ENABLE_KDBUS
+        r = sd_bus_new(&bus);
+        if (r < 0)
+                return r;
+
+        if (asprintf(&bus->address, "kernel:path=/dev/kdbus/%lu-user/bus", (unsigned long) getuid()) < 0)
+                return -ENOMEM;
+
+        bus->bus_client = true;
+
+        r = sd_bus_start(bus);
+        if (r >= 0) {
+                *_bus = bus;
+                bus = NULL;
+                return 0;
+        }
+
+        bus = sd_bus_unref(bus);
+#endif
+
         e = secure_getenv("XDG_RUNTIME_DIR");
         if (!e)
-                return sd_bus_open_user(_bus);
+                return sd_bus_open_system(_bus);
 
-        if (asprintf(&p, "unix:path=%s/systemd/private", e) < 0)
+        ee = bus_address_escape(e);
+        if (!ee)
                 return -ENOMEM;
 
         r = sd_bus_new(&bus);
         if (r < 0)
                 return r;
 
-        r = sd_bus_set_address(bus, p);
-        if (r < 0)
-                return r;
+        bus->address = strjoin("unix:path=", ee, "/systemd/private", NULL);
+        if (!bus->address)
+                return -ENOMEM;
 
         r = sd_bus_start(bus);
         if (r < 0)
-                return r;
+                return sd_bus_open_system(_bus);
 
         r = bus_check_peercred(bus);
         if (r < 0)
@@ -890,7 +941,8 @@ int bus_map_all_properties(sd_bus *bus,
         assert(path);
         assert(map);
 
-        r = sd_bus_call_method( bus,
+        r = sd_bus_call_method(
+                        bus,
                         destination,
                         path,
                         "org.freedesktop.DBus.Properties",