From d4d00020d6ad855d65d31020fefa5003e1bb477f Mon Sep 17 00:00:00 2001 From: Kay Sievers Date: Tue, 11 Aug 2015 20:46:05 +0200 Subject: sd-bus: do not connect to dbus-1 socket when kdbus is available We should not fall back to dbus-1 and connect to the proxy when kdbus returns an error that indicates that kdbus is running but just does not accept new connections because of quota limits or something similar. Using is_kdbus_available() in libsystemd/ requires it to move from shared/ to libsystemd/. Based on a patch from David Herrmann: https://github.com/systemd/systemd/pull/886 --- src/libsystemd/sd-bus/bus-internal.c | 42 ++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) (limited to 'src/libsystemd/sd-bus/bus-internal.c') diff --git a/src/libsystemd/sd-bus/bus-internal.c b/src/libsystemd/sd-bus/bus-internal.c index fea796cd30..f4ab57f5bc 100644 --- a/src/libsystemd/sd-bus/bus-internal.c +++ b/src/libsystemd/sd-bus/bus-internal.c @@ -371,3 +371,45 @@ int bus_maybe_reply_error(sd_bus_message *m, int r, sd_bus_error *error) { return 1; } + +bool is_kdbus_wanted(void) { + _cleanup_free_ char *value = NULL; +#ifdef ENABLE_KDBUS + const bool configured = true; +#else + const bool configured = false; +#endif + int r; + + if (get_proc_cmdline_key("kdbus", NULL) > 0) + return true; + + r = get_proc_cmdline_key("kdbus=", &value); + if (r <= 0) + return configured; + + return parse_boolean(value) == 1; +} + +bool is_kdbus_available(void) { + static int cached = -1; + _cleanup_close_ int fd = -1; + struct kdbus_cmd cmd = { .size = sizeof(cmd), .flags = KDBUS_FLAG_NEGOTIATE }; + + if (cached >= 0) + return (bool) cached; + + if (!is_kdbus_wanted()) { + cached = false; + return false; + } + + fd = open("/sys/fs/kdbus/control", O_RDWR | O_CLOEXEC | O_NONBLOCK | O_NOCTTY); + if (fd < 0) { + cached = false; + return false; + } + + cached = ioctl(fd, KDBUS_CMD_BUS_MAKE, &cmd) >= 0; + return cached; +} -- cgit v1.2.3-54-g00ecf