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
This commit is contained in:
Kay Sievers
2015-08-11 20:49:36 +02:00
parent 3f1818cf70
commit d4d00020d6
31 changed files with 95 additions and 98 deletions
-1
View File
@@ -32,7 +32,6 @@
#include "bus-message.h"
#include "bus-control.h"
#include "bus-bloom.h"
#include "bus-util.h"
#include "capability.h"
_public_ int sd_bus_get_unique_name(sd_bus *bus, const char **unique) {
-1
View File
@@ -22,7 +22,6 @@
#include "bus-internal.h"
#include "bus-message.h"
#include "bus-signature.h"
#include "bus-util.h"
#include "bus-type.h"
_public_ int sd_bus_emit_signal(
+1 -1
View File
@@ -31,10 +31,10 @@
#include "fileio.h"
#include "audit.h"
#include "bus-message.h"
#include "bus-util.h"
#include "strv.h"
#include "bus-creds.h"
#include "bus-label.h"
#include "bus-internal.h"
enum {
CAP_OFFSET_INHERITABLE = 0,
+42
View File
@@ -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;
}
+26
View File
@@ -37,6 +37,29 @@
#include "bus-kernel.h"
#include "kdbus.h"
typedef enum BusTransport {
BUS_TRANSPORT_LOCAL,
BUS_TRANSPORT_REMOTE,
BUS_TRANSPORT_MACHINE,
_BUS_TRANSPORT_MAX,
_BUS_TRANSPORT_INVALID = -1
} BusTransport;
DEFINE_TRIVIAL_CLEANUP_FUNC(sd_bus*, sd_bus_unref);
DEFINE_TRIVIAL_CLEANUP_FUNC(sd_bus*, sd_bus_flush_close_unref);
DEFINE_TRIVIAL_CLEANUP_FUNC(sd_bus_slot*, sd_bus_slot_unref);
DEFINE_TRIVIAL_CLEANUP_FUNC(sd_bus_message*, sd_bus_message_unref);
DEFINE_TRIVIAL_CLEANUP_FUNC(sd_bus_creds*, sd_bus_creds_unref);
DEFINE_TRIVIAL_CLEANUP_FUNC(sd_bus_track*, sd_bus_track_unref);
#define _cleanup_bus_unref_ _cleanup_(sd_bus_unrefp)
#define _cleanup_bus_flush_close_unref_ _cleanup_(sd_bus_flush_close_unrefp)
#define _cleanup_bus_slot_unref_ _cleanup_(sd_bus_slot_unrefp)
#define _cleanup_bus_message_unref_ _cleanup_(sd_bus_message_unrefp)
#define _cleanup_bus_creds_unref_ _cleanup_(sd_bus_creds_unrefp)
#define _cleanup_bus_track_unref_ _cleanup_(sd_bus_slot_unrefp)
#define _cleanup_bus_error_free_ _cleanup_(sd_bus_error_free)
struct reply_callback {
sd_bus_message_handler_t callback;
usec_t timeout;
@@ -393,3 +416,6 @@ int bus_remove_match_by_string(sd_bus *bus, const char *match, sd_bus_message_ha
int bus_get_root_path(sd_bus *bus);
int bus_maybe_reply_error(sd_bus_message *m, int r, sd_bus_error *error);
bool is_kdbus_wanted(void);
bool is_kdbus_available(void);
-1
View File
@@ -45,7 +45,6 @@
#include "bus-message.h"
#include "bus-kernel.h"
#include "bus-bloom.h"
#include "bus-util.h"
#include "bus-label.h"
#define UNIQUE_NAME_MAX (3+DECIMAL_STR_MAX(uint64_t))
-1
View File
@@ -22,7 +22,6 @@
#include "bus-internal.h"
#include "bus-message.h"
#include "bus-match.h"
#include "bus-util.h"
#include "strv.h"
/* Example:
-1
View File
@@ -35,7 +35,6 @@
#include "bus-type.h"
#include "bus-signature.h"
#include "bus-gvariant.h"
#include "bus-util.h"
static int message_append_basic(sd_bus_message *m, char type, const void *p, const void **stored);
-1
View File
@@ -26,7 +26,6 @@
#include "bus-type.h"
#include "bus-signature.h"
#include "bus-introspect.h"
#include "bus-util.h"
#include "bus-slot.h"
#include "bus-objects.h"
-1
View File
@@ -20,7 +20,6 @@
***/
#include "sd-bus.h"
#include "bus-util.h"
#include "bus-internal.h"
#include "bus-track.h"
-1
View File
@@ -30,7 +30,6 @@
#include "set.h"
#include "sd-bus.h"
#include "bus-internal.h"
#include "bus-util.h"
#include "bus-dump.h"
#include "bus-signature.h"
+9 -6
View File
@@ -43,7 +43,6 @@
#include "bus-kernel.h"
#include "bus-control.h"
#include "bus-objects.h"
#include "bus-util.h"
#include "bus-container.h"
#include "bus-protocol.h"
#include "bus-track.h"
@@ -1173,7 +1172,10 @@ int bus_set_address_system(sd_bus *b) {
if (e)
return sd_bus_set_address(b, e);
return sd_bus_set_address(b, DEFAULT_SYSTEM_BUS_ADDRESS);
if (is_kdbus_available())
return sd_bus_set_address(b, KERNEL_SYSTEM_BUS_ADDRESS);
return sd_bus_set_address(b, UNIX_SYSTEM_BUS_ADDRESS);
}
_public_ int sd_bus_open_system(sd_bus **ret) {
@@ -1221,16 +1223,17 @@ int bus_set_address_user(sd_bus *b) {
return sd_bus_set_address(b, e);
e = secure_getenv("XDG_RUNTIME_DIR");
if (e) {
if (is_kdbus_available())
(void) asprintf(&b->address, KERNEL_USER_BUS_ADDRESS_FMT, getuid());
else if (e) {
_cleanup_free_ char *ee = NULL;
ee = bus_address_escape(e);
if (!ee)
return -ENOMEM;
(void) asprintf(&b->address, KERNEL_USER_BUS_ADDRESS_FMT ";" UNIX_USER_BUS_ADDRESS_FMT, getuid(), ee);
} else
(void) asprintf(&b->address, KERNEL_USER_BUS_ADDRESS_FMT, getuid());
(void) asprintf(&b->address, UNIX_USER_BUS_ADDRESS_FMT, ee);
}
if (!b->address)
return -ENOMEM;
@@ -28,7 +28,6 @@
#include "sd-bus.h"
#include "bus-kernel.h"
#include "bus-internal.h"
#include "bus-util.h"
#define MAX_SIZE (2*1024*1024)
-1
View File
@@ -33,7 +33,6 @@
#include "bus-error.h"
#include "bus-match.h"
#include "bus-internal.h"
#include "bus-util.h"
static int match_callback(sd_bus_message *m, void *userdata, sd_bus_error *ret_error) {
log_info("Match triggered! interface=%s member=%s", strna(sd_bus_message_get_interface(m)), strna(sd_bus_message_get_member(m)));
-1
View File
@@ -22,7 +22,6 @@
#include <stdio.h>
#include "sd-bus.h"
#include "bus-util.h"
#include "bus-internal.h"
#include "bus-message.h"
#include "refcnt.h"
-1
View File
@@ -20,7 +20,6 @@
***/
#include "sd-bus.h"
#include "bus-error.h"
#include "bus-util.h"
#include "errno-list.h"
#include "bus-common-errors.h"
@@ -27,7 +27,6 @@
#include "macro.h"
#include "sd-bus.h"
#include "bus-gvariant.h"
#include "bus-util.h"
#include "bus-internal.h"
#include "bus-message.h"
#include "bus-dump.h"
+1 -1
View File
@@ -25,8 +25,8 @@
#include "log.h"
#include "sd-bus.h"
#include "bus-kernel.h"
#include "bus-util.h"
#include "bus-kernel.h"
#include "bus-dump.h"
int main(int argc, char *argv[]) {
-1
View File
@@ -24,7 +24,6 @@
#include "bus-match.h"
#include "bus-message.h"
#include "bus-util.h"
#include "bus-slot.h"
static bool mask[32];
-1
View File
@@ -30,7 +30,6 @@
#include "sd-bus.h"
#include "bus-internal.h"
#include "bus-message.h"
#include "bus-util.h"
#include "bus-dump.h"
struct context {
-1
View File
@@ -28,7 +28,6 @@
#include "sd-bus.h"
#include "bus-internal.h"
#include "bus-util.h"
struct context {
int fds[2];