Merge pull request #17344 from keszybz/bus-connect-more-logs

Add some debug logs to help diagnose bus connections
This commit is contained in:
Lennart Poettering 2020-10-19 17:39:37 +02:00 committed by GitHub
commit 43e7dd70bc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 55 additions and 15 deletions

View File

@ -276,13 +276,20 @@
<para>Returned errors may indicate the following problems:</para>
<variablelist>
<varlistentry>
<term><constant>-EINVAL</constant></term>
<listitem><para>The specified parameters are invalid.</para></listitem>
</varlistentry>
<varlistentry>
<term><constant>-ENOMEDIUM</constant></term>
<listitem><para>The requested bus type is not available because of invalid environment (for example
the user session bus is not available because <varname>$XDG_RUNTIME_DIR</varname> is not set).
</para></listitem>
</varlistentry>
<varlistentry>
<term><constant>-ENOMEM</constant></term>

View File

@ -129,11 +129,11 @@ static int acquire_bus(bool set_monitor, sd_bus **ret) {
}
}
if (r < 0)
return log_error_errno(r, "Failed to set address: %m");
return bus_log_address_error(r);
r = sd_bus_start(bus);
if (r < 0)
return log_error_errno(r, "Failed to connect to bus: %m");
return bus_log_connect_error(r);
*ret = TAKE_PTR(bus);

View File

@ -99,7 +99,7 @@ static int acquire_bus(sd_bus **bus) {
r = bus_connect_transport(arg_transport, arg_host, false, bus);
if (r < 0)
return log_error_errno(r, "Failed to connect to bus: %m");
return bus_log_connect_error(r);
(void) sd_bus_set_allow_interactive_authorization(*bus, arg_ask_password);

View File

@ -9,6 +9,7 @@
#include "fd-util.h"
#include "namespace-util.h"
#include "process-util.h"
#include "string-util.h"
#include "util.h"
int bus_container_connect_socket(sd_bus *b) {
@ -24,10 +25,15 @@ int bus_container_connect_socket(sd_bus *b) {
assert(b->nspid > 0 || b->machine);
if (b->nspid <= 0) {
log_debug("sd-bus: connecting bus%s%s to machine %s...",
b->description ? " " : "", strempty(b->description), b->machine);
r = container_get_leader(b->machine, &b->nspid);
if (r < 0)
return r;
}
} else
log_debug("sd-bus: connecting bus%s%s to namespace of PID "PID_FMT"...",
b->description ? " " : "", strempty(b->description), b->nspid);
r = namespace_open(b->nspid, &pidnsfd, &mntnsfd, NULL, &usernsfd, &rootfd);
if (r < 0)

View File

@ -885,6 +885,13 @@ int bus_socket_connect(sd_bus *b) {
assert(b->output_fd < 0);
assert(b->sockaddr.sa.sa_family != AF_UNSPEC);
if (DEBUG_LOGGING) {
_cleanup_free_ char *pretty = NULL;
(void) sockaddr_pretty(&b->sockaddr.sa, b->sockaddr_size, false, true, &pretty);
log_debug("sd-bus: starting bus%s%s by connecting to %s...",
b->description ? " " : "", strempty(b->description), strnull(pretty));
}
b->input_fd = socket(b->sockaddr.sa.sa_family, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0);
if (b->input_fd < 0)
return -errno;
@ -956,6 +963,9 @@ int bus_socket_exec(sd_bus *b) {
assert(b->exec_path);
assert(b->busexec_pid == 0);
log_debug("sd-bus: starting bus%s%s with %s...",
b->description ? " " : "", strempty(b->description), b->exec_path);
r = socketpair(AF_UNIX, SOCK_STREAM|SOCK_NONBLOCK|SOCK_CLOEXEC, 0, s);
if (r < 0)
return -errno;

View File

@ -1155,6 +1155,16 @@ static int bus_start_fd(sd_bus *b) {
assert(b->input_fd >= 0);
assert(b->output_fd >= 0);
if (DEBUG_LOGGING) {
_cleanup_free_ char *pi = NULL, *po = NULL;
(void) fd_get_path(b->input_fd, &pi);
(void) fd_get_path(b->output_fd, &po);
log_debug("sd-bus: starting bus%s%s on fds %d/%d (%s, %s)...",
b->description ? " " : "", strempty(b->description),
b->input_fd, b->output_fd,
pi ?: "???", po ?: "???");
}
r = fd_nonblock(b->input_fd, true);
if (r < 0)
return r;
@ -1330,7 +1340,8 @@ int bus_set_address_user(sd_bus *b) {
e = secure_getenv("XDG_RUNTIME_DIR");
if (!e)
return -ENOENT;
return log_debug_errno(SYNTHETIC_ERRNO(ENOMEDIUM),
"sd-bus: $XDG_RUNTIME_DIR not set, cannot connect to user bus.");
ee = bus_address_escape(e);
if (!ee)

View File

@ -23,7 +23,7 @@ static int test_bus_open(void) {
int r;
r = sd_bus_open_user(&bus);
if (IN_SET(r, -ECONNREFUSED, -ENOENT)) {
if (IN_SET(r, -ECONNREFUSED, -ENOENT, -ENOMEDIUM)) {
r = sd_bus_open_system(&bus);
if (IN_SET(r, -ECONNREFUSED, -ENOENT))
return r;

View File

@ -55,7 +55,7 @@ int main(int argc, char *argv[]) {
assert_se(r >= 0);
r = sd_bus_open_user(&a);
if (IN_SET(r, -ECONNREFUSED, -ENOENT)) {
if (IN_SET(r, -ECONNREFUSED, -ENOENT, -ENOMEDIUM)) {
r = sd_bus_open_system(&a);
if (IN_SET(r, -ECONNREFUSED, -ENOENT))
return log_tests_skipped("Failed to connect to bus");

View File

@ -282,7 +282,7 @@ static int run(int argc, char *argv[]) {
r = sd_bus_default_system(&bus);
if (r < 0)
return log_error_errno(r, "Failed to connect to bus: %m");
return bus_log_connect_error(r);
if (arg_action == ACTION_LIST)
return print_inhibitors(bus);

View File

@ -178,7 +178,7 @@ static int acquire_bus(sd_bus **bus) {
r = bus_connect_transport(arg_transport, arg_host, false, bus);
if (r < 0)
return log_error_errno(r, "Failed to connect to bus: %m");
return bus_log_connect_error(r);
(void) sd_bus_set_allow_interactive_authorization(*bus, arg_ask_password);

View File

@ -38,13 +38,19 @@ int bus_connect_user_systemd(sd_bus **_bus);
int bus_connect_transport(BusTransport transport, const char *host, bool user, sd_bus **bus);
int bus_connect_transport_systemd(BusTransport transport, const char *host, bool user, sd_bus **bus);
#define bus_log_connect_error(r) \
log_error_errno(r, "Failed to create bus connection: %m")
#define bus_log_address_error(r) \
log_error_errno(r, \
r == -ENOMEDIUM ? "Failed to set bus address: $DBUS_SESSION_BUS_ADDRESS and $XDG_RUNTIME_DIR not defined" : \
"Failed to set bus address: %m")
#define bus_log_connect_error(r) \
log_error_errno(r, \
r == -ENOMEDIUM ? "Failed to connect to bus: $DBUS_SESSION_BUS_ADDRESS and $XDG_RUNTIME_DIR not defined" : \
"Failed to connect to bus: %m")
#define bus_log_parse_error(r) \
#define bus_log_parse_error(r) \
log_error_errno(r, "Failed to parse bus message: %m")
#define bus_log_create_error(r) \
#define bus_log_create_error(r) \
log_error_errno(r, "Failed to create bus message: %m")
int bus_path_encode_unique(sd_bus *b, const char *prefix, const char *sender_id, const char *external_id, char **ret_path);

View File

@ -52,7 +52,7 @@ int acquire_bus(BusFocus focus, sd_bus **ret) {
else
r = bus_connect_transport(arg_transport, arg_host, user, &buses[focus]);
if (r < 0)
return log_error_errno(r, "Failed to connect to bus: %m");
return bus_log_connect_error(r);
(void) sd_bus_set_allow_interactive_authorization(buses[focus], arg_ask_password);
}