mirror of
https://github.com/systemd/systemd.git
synced 2024-12-22 17:35:35 +03:00
sd-bus: add custom return code when $XDG_RUNTIME_DIR is not set
We would return ENOENT, which is extremely confusing. Strace is not helpful because no *file* is actually missing. So let's add some logs at debug level and also use a custom return code. Let all user-facing utilities print a custom error message in that case.
This commit is contained in:
parent
165fee860a
commit
ab4a88eb92
@ -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>
|
||||
|
||||
|
@ -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);
|
||||
|
||||
|
@ -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);
|
||||
|
||||
|
@ -1340,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)
|
||||
|
@ -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;
|
||||
|
@ -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");
|
||||
|
@ -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);
|
||||
|
@ -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);
|
||||
|
||||
|
@ -38,8 +38,14 @@ 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_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, "Failed to create bus connection: %m")
|
||||
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) \
|
||||
log_error_errno(r, "Failed to parse bus message: %m")
|
||||
|
@ -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);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user