Merge pull request #16824 from keszybz/no-such-unit-error

Add sd_bus_error_has_names() and use it to catch BUS_ERROR_NO_SUCH_UNIT
This commit is contained in:
Zbigniew Jędrzejewski-Szmek 2020-08-25 09:16:25 +02:00 committed by GitHub
commit d521e6993d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
16 changed files with 111 additions and 58 deletions

View File

@ -241,6 +241,8 @@ manpages = [
'sd_bus_error_free',
'sd_bus_error_get_errno',
'sd_bus_error_has_name',
'sd_bus_error_has_names',
'sd_bus_error_has_names_sentinel',
'sd_bus_error_is_set',
'sd_bus_error_move',
'sd_bus_error_set',

View File

@ -31,6 +31,8 @@
<refname>sd_bus_error_move</refname>
<refname>sd_bus_error_is_set</refname>
<refname>sd_bus_error_has_name</refname>
<refname>sd_bus_error_has_names_sentinel</refname>
<refname>sd_bus_error_has_names</refname>
<refpurpose>sd-bus error handling</refpurpose>
</refnamediv>
@ -128,6 +130,16 @@
<paramdef>const sd_bus_error *<parameter>e</parameter></paramdef>
<paramdef>const char *<parameter>name</parameter></paramdef>
</funcprototype>
<funcprototype>
<funcdef>int <function>sd_bus_error_has_names_sentinel</function></funcdef>
<paramdef>const sd_bus_error *<parameter>e</parameter></paramdef>
<paramdef>...</paramdef>
</funcprototype>
<para>
&#35;define sd_bus_error_has_names(e, ...) sd_bus_error_has_names_sentinel(e, ..., NULL)
</para>
</funcsynopsis>
</refsynopsisdiv>
@ -268,6 +280,12 @@
<parameter>name</parameter> has been set,
<constant>false</constant> otherwise.</para>
<para><function>sd_bus_error_has_names_sentinel()</function> is similar to
<function>sd_bus_error_has_name()</function>, but takes multiple names to check against. The list must be
terminated with <constant>NULL</constant>. <function>sd_bus_error_has_names()</function>
is a macro wrapper around <function>sd_bus_error_has_names_sentinel()</function> that adds the
<constant>NULL</constant> sentinel automatically.</para>
<para><function>sd_bus_error_free()</function> will destroy
resources held by <parameter>e</parameter>. The parameter itself
will not be deallocated, and must be <citerefentry
@ -307,11 +325,10 @@
<structfield>name</structfield> field are
non-<constant>NULL</constant>, zero otherwise.</para>
<para><function>sd_bus_error_has_name()</function> returns a
non-zero value when <parameter>e</parameter> is
non-<constant>NULL</constant> and the
<structfield>name</structfield> field is equal to
<parameter>name</parameter>, zero otherwise.</para>
<para><function>sd_bus_error_has_name()</function>, <function>sd_bus_error_has_names()</function>, and
<function>sd_bus_error_has_names_sentinel()</function> return a non-zero value when <parameter>e</parameter> is
non-<constant>NULL</constant> and the <structfield>name</structfield> field is equal to one of the given
names, zero otherwise.</para>
</refsect1>
<refsect1>

View File

@ -7,6 +7,7 @@
#include "bus-common-errors.h"
#include "bus-locator.h"
#include "bus-util.h"
#include "errno-util.h"
#include "fd-util.h"
#include "home-util.h"
@ -153,8 +154,7 @@ static int acquire_user_record(
r = bus_call_method(bus, bus_home_mgr, "GetUserRecordByName", &error, &reply, "s", username);
if (r < 0) {
if (sd_bus_error_has_name(&error, SD_BUS_ERROR_SERVICE_UNKNOWN) ||
sd_bus_error_has_name(&error, SD_BUS_ERROR_NAME_HAS_NO_OWNER)) {
if (bus_error_is_unknown_service(&error)) {
pam_syslog(handle, LOG_DEBUG, "systemd-homed is not available: %s", bus_error_message(&error, r));
goto user_unknown;
}

View File

@ -727,3 +727,8 @@ global:
sd_event_add_time_relative;
sd_event_source_set_time_relative;
} LIBSYSTEMD_246;
LIBSYSTEMD_248 {
global:
sd_bus_error_has_names_sentinel;
} LIBSYSTEMD_247;

View File

@ -13,6 +13,7 @@
#include "errno-list.h"
#include "errno-util.h"
#include "string-util.h"
#include "strv.h"
#include "util.h"
BUS_ERROR_MAP_ELF_REGISTER const sd_bus_error_map bus_standard_errors[] = {
@ -355,11 +356,23 @@ _public_ int sd_bus_error_has_name(const sd_bus_error *e, const char *name) {
return streq_ptr(e->name, name);
}
_public_ int sd_bus_error_get_errno(const sd_bus_error* e) {
if (!e)
_public_ int sd_bus_error_has_names_sentinel(const sd_bus_error *e, ...) {
if (!e || !e->name)
return 0;
if (!e->name)
va_list ap;
const char *p;
va_start(ap, e);
while ((p = va_arg(ap, const char *)))
if (streq(p, e->name))
break;
va_end(ap);
return !!p;
}
_public_ int sd_bus_error_get_errno(const sd_bus_error* e) {
if (!e || !e->name)
return 0;
return bus_error_name_to_errno(e->name);

View File

@ -22,6 +22,11 @@ static void test_error(void) {
assert_se(streq(error.name, SD_BUS_ERROR_NOT_SUPPORTED));
assert_se(streq(error.message, "xxx"));
assert_se(sd_bus_error_has_name(&error, SD_BUS_ERROR_NOT_SUPPORTED));
assert_se(sd_bus_error_has_names_sentinel(&error, SD_BUS_ERROR_NOT_SUPPORTED, NULL));
assert_se(sd_bus_error_has_names(&error, SD_BUS_ERROR_NOT_SUPPORTED));
assert_se(sd_bus_error_has_names(&error, SD_BUS_ERROR_NOT_SUPPORTED, SD_BUS_ERROR_FILE_NOT_FOUND));
assert_se(sd_bus_error_has_names(&error, SD_BUS_ERROR_FILE_NOT_FOUND, SD_BUS_ERROR_NOT_SUPPORTED, NULL));
assert_se(!sd_bus_error_has_names(&error, SD_BUS_ERROR_FILE_NOT_FOUND));
assert_se(sd_bus_error_get_errno(&error) == EOPNOTSUPP);
assert_se(sd_bus_error_is_set(&error));
sd_bus_error_free(&error);
@ -32,6 +37,7 @@ static void test_error(void) {
assert_se(error.name == NULL);
assert_se(error.message == NULL);
assert_se(!sd_bus_error_has_name(&error, SD_BUS_ERROR_FILE_NOT_FOUND));
assert_se(!sd_bus_error_has_names(&error, SD_BUS_ERROR_FILE_NOT_FOUND));
assert_se(sd_bus_error_get_errno(&error) == 0);
assert_se(!sd_bus_error_is_set(&error));
@ -39,6 +45,7 @@ static void test_error(void) {
assert_se(streq(error.name, SD_BUS_ERROR_FILE_NOT_FOUND));
assert_se(streq(error.message, "yyy -1"));
assert_se(sd_bus_error_has_name(&error, SD_BUS_ERROR_FILE_NOT_FOUND));
assert_se(sd_bus_error_has_names(&error, SD_BUS_ERROR_FILE_NOT_FOUND));
assert_se(sd_bus_error_get_errno(&error) == ENOENT);
assert_se(sd_bus_error_is_set(&error));
@ -51,6 +58,7 @@ static void test_error(void) {
assert_se(streq(error.message, second.message));
assert_se(sd_bus_error_get_errno(&second) == ENOENT);
assert_se(sd_bus_error_has_name(&second, SD_BUS_ERROR_FILE_NOT_FOUND));
assert_se(sd_bus_error_has_names(&second, SD_BUS_ERROR_FILE_NOT_FOUND));
assert_se(sd_bus_error_is_set(&second));
sd_bus_error_free(&error);

View File

@ -4093,8 +4093,8 @@ int manager_stop_unit(Manager *manager, const char *unit, sd_bus_error *error, c
&reply,
"ss", unit, "fail");
if (r < 0) {
if (sd_bus_error_has_name(error, BUS_ERROR_NO_SUCH_UNIT) ||
sd_bus_error_has_name(error, BUS_ERROR_LOAD_FAILED)) {
if (sd_bus_error_has_names(error, BUS_ERROR_NO_SUCH_UNIT,
BUS_ERROR_LOAD_FAILED)) {
*job = NULL;
sd_bus_error_free(error);
@ -4129,9 +4129,9 @@ int manager_abandon_scope(Manager *manager, const char *scope, sd_bus_error *ret
NULL,
NULL);
if (r < 0) {
if (sd_bus_error_has_name(&error, BUS_ERROR_NO_SUCH_UNIT) ||
sd_bus_error_has_name(&error, BUS_ERROR_LOAD_FAILED) ||
sd_bus_error_has_name(&error, BUS_ERROR_SCOPE_NOT_RUNNING))
if (sd_bus_error_has_names(&error, BUS_ERROR_NO_SUCH_UNIT,
BUS_ERROR_LOAD_FAILED,
BUS_ERROR_SCOPE_NOT_RUNNING))
return 0;
sd_bus_error_move(ret_error, &error);
@ -4180,14 +4180,14 @@ int manager_unit_is_active(Manager *manager, const char *unit, sd_bus_error *ret
if (r < 0) {
/* systemd might have dropped off momentarily, let's
* not make this an error */
if (sd_bus_error_has_name(&error, SD_BUS_ERROR_NO_REPLY) ||
sd_bus_error_has_name(&error, SD_BUS_ERROR_DISCONNECTED))
if (sd_bus_error_has_names(&error, SD_BUS_ERROR_NO_REPLY,
SD_BUS_ERROR_DISCONNECTED))
return true;
/* If the unit is already unloaded then it's not
* active */
if (sd_bus_error_has_name(&error, BUS_ERROR_NO_SUCH_UNIT) ||
sd_bus_error_has_name(&error, BUS_ERROR_LOAD_FAILED))
if (sd_bus_error_has_names(&error, BUS_ERROR_NO_SUCH_UNIT,
BUS_ERROR_LOAD_FAILED))
return false;
sd_bus_error_move(ret_error, &error);
@ -4219,8 +4219,8 @@ int manager_job_is_active(Manager *manager, const char *path, sd_bus_error *ret_
&reply,
"s");
if (r < 0) {
if (sd_bus_error_has_name(&error, SD_BUS_ERROR_NO_REPLY) ||
sd_bus_error_has_name(&error, SD_BUS_ERROR_DISCONNECTED))
if (sd_bus_error_has_names(&error, SD_BUS_ERROR_NO_REPLY,
SD_BUS_ERROR_DISCONNECTED))
return true;
if (sd_bus_error_has_name(&error, SD_BUS_ERROR_UNKNOWN_OBJECT))

View File

@ -1464,8 +1464,8 @@ int manager_stop_unit(Manager *manager, const char *unit, sd_bus_error *error, c
r = bus_call_method(manager->bus, bus_systemd_mgr, "StopUnit", error, &reply, "ss", unit, "fail");
if (r < 0) {
if (sd_bus_error_has_name(error, BUS_ERROR_NO_SUCH_UNIT) ||
sd_bus_error_has_name(error, BUS_ERROR_LOAD_FAILED)) {
if (sd_bus_error_has_names(error, BUS_ERROR_NO_SUCH_UNIT,
BUS_ERROR_LOAD_FAILED)) {
if (job)
*job = NULL;
@ -1526,12 +1526,12 @@ int manager_unit_is_active(Manager *manager, const char *unit) {
&reply,
"s");
if (r < 0) {
if (sd_bus_error_has_name(&error, SD_BUS_ERROR_NO_REPLY) ||
sd_bus_error_has_name(&error, SD_BUS_ERROR_DISCONNECTED))
if (sd_bus_error_has_names(&error, SD_BUS_ERROR_NO_REPLY,
SD_BUS_ERROR_DISCONNECTED))
return true;
if (sd_bus_error_has_name(&error, BUS_ERROR_NO_SUCH_UNIT) ||
sd_bus_error_has_name(&error, BUS_ERROR_LOAD_FAILED))
if (sd_bus_error_has_names(&error, BUS_ERROR_NO_SUCH_UNIT,
BUS_ERROR_LOAD_FAILED))
return false;
return r;
@ -1562,8 +1562,8 @@ int manager_job_is_active(Manager *manager, const char *path) {
&reply,
"s");
if (r < 0) {
if (sd_bus_error_has_name(&error, SD_BUS_ERROR_NO_REPLY) ||
sd_bus_error_has_name(&error, SD_BUS_ERROR_DISCONNECTED))
if (sd_bus_error_has_names(&error, SD_BUS_ERROR_NO_REPLY,
SD_BUS_ERROR_DISCONNECTED))
return true;
if (sd_bus_error_has_name(&error, SD_BUS_ERROR_UNKNOWN_OBJECT))

View File

@ -510,8 +510,8 @@ static int acquire_link_bitrates(sd_bus *bus, LinkInfo *link) {
r = link_get_property(bus, link, &error, &reply, "org.freedesktop.network1.Link", "BitRates");
if (r < 0) {
bool quiet = sd_bus_error_has_name(&error, SD_BUS_ERROR_UNKNOWN_PROPERTY) ||
sd_bus_error_has_name(&error, BUS_ERROR_SPEED_METER_INACTIVE);
bool quiet = sd_bus_error_has_names(&error, SD_BUS_ERROR_UNKNOWN_PROPERTY,
BUS_ERROR_SPEED_METER_INACTIVE);
return log_full_errno(quiet ? LOG_DEBUG : LOG_WARNING,
r, "Failed to query link bit rates: %s", bus_error_message(&error, r));

View File

@ -23,12 +23,14 @@ NSS_GETHOSTBYNAME_PROTOTYPES(resolve);
NSS_GETHOSTBYADDR_PROTOTYPES(resolve);
static bool bus_error_shall_fallback(sd_bus_error *e) {
return sd_bus_error_has_name(e, SD_BUS_ERROR_SERVICE_UNKNOWN) ||
sd_bus_error_has_name(e, SD_BUS_ERROR_NAME_HAS_NO_OWNER) ||
sd_bus_error_has_name(e, SD_BUS_ERROR_NO_REPLY) ||
sd_bus_error_has_name(e, SD_BUS_ERROR_ACCESS_DENIED) ||
sd_bus_error_has_name(e, SD_BUS_ERROR_DISCONNECTED) ||
sd_bus_error_has_name(e, SD_BUS_ERROR_TIMEOUT);
return sd_bus_error_has_names(e,
SD_BUS_ERROR_SERVICE_UNKNOWN,
SD_BUS_ERROR_NAME_HAS_NO_OWNER,
SD_BUS_ERROR_NO_REPLY,
SD_BUS_ERROR_ACCESS_DENIED,
SD_BUS_ERROR_DISCONNECTED,
SD_BUS_ERROR_TIMEOUT,
BUS_ERROR_NO_SUCH_UNIT);
}
static int count_addresses(sd_bus_message *m, int af, const char **canonical) {

View File

@ -3,6 +3,7 @@
#include "bus-internal.h"
#include "bus-message.h"
#include "bus-polkit.h"
#include "bus-util.h"
#include "strv.h"
#include "user-util.h"
@ -123,7 +124,7 @@ int bus_test_polkit(
r = sd_bus_call(call->bus, request, 0, ret_error, &reply);
if (r < 0) {
/* Treat no PK available as access denied */
if (sd_bus_error_has_name(ret_error, SD_BUS_ERROR_SERVICE_UNKNOWN)) {
if (bus_error_is_unknown_service(ret_error)) {
sd_bus_error_free(ret_error);
return -EACCES;
}
@ -296,8 +297,7 @@ int bus_verify_polkit_async(
e = sd_bus_message_get_error(q->reply);
/* Treat no PK available as access denied */
if (sd_bus_error_has_name(e, SD_BUS_ERROR_SERVICE_UNKNOWN) ||
sd_bus_error_has_name(e, SD_BUS_ERROR_NAME_HAS_NO_OWNER))
if (bus_error_is_unknown_service(e))
return -EACCES;
/* Copy error from polkit reply */

View File

@ -14,14 +14,13 @@
#include "sd-event.h"
#include "sd-id128.h"
/* #include "alloc-util.h" */
#include "bus-common-errors.h"
#include "bus-internal.h"
#include "bus-label.h"
#include "bus-util.h"
#include "path-util.h"
#include "socket-util.h"
#include "stdio-util.h"
/* #include "string-util.h" */
static int name_owner_change_callback(sd_bus_message *m, void *userdata, sd_bus_error *ret_error) {
sd_event *e = userdata;
@ -153,6 +152,13 @@ int bus_name_has_owner(sd_bus *c, const char *name, sd_bus_error *error) {
return has_owner;
}
bool bus_error_is_unknown_service(const sd_bus_error *error) {
return sd_bus_error_has_names(error,
SD_BUS_ERROR_SERVICE_UNKNOWN,
SD_BUS_ERROR_NAME_HAS_NO_OWNER,
BUS_ERROR_NO_SUCH_UNIT);
}
int bus_check_peercred(sd_bus *c) {
struct ucred ucred;
int fd, r;

View File

@ -28,6 +28,7 @@ typedef bool (*check_idle_t)(void *userdata);
int bus_event_loop_with_idle(sd_event *e, sd_bus *bus, const char *name, usec_t timeout, check_idle_t check_idle, void *userdata);
int bus_name_has_owner(sd_bus *c, const char *name, sd_bus_error *error);
bool bus_error_is_unknown_service(const sd_bus_error *error);
int bus_check_peercred(sd_bus *c);

View File

@ -160,11 +160,8 @@ static int lock_all_homes(void) {
r = sd_bus_call(bus, m, DEFAULT_TIMEOUT_USEC, &error, NULL);
if (r < 0) {
if (sd_bus_error_has_name(&error, SD_BUS_ERROR_SERVICE_UNKNOWN) ||
sd_bus_error_has_name(&error, SD_BUS_ERROR_NAME_HAS_NO_OWNER)) {
log_debug("systemd-homed is not running, skipping locking of home directories.");
return 0;
}
if (bus_error_is_unknown_service(&error))
return log_debug("systemd-homed is not running, skipping locking of home directories.");
return log_error_errno(r, "Failed to lock home directories: %s", bus_error_message(&error, r));
}

View File

@ -279,17 +279,17 @@ static int translate_bus_error_to_exit_status(int r, const sd_bus_error *error)
if (!sd_bus_error_is_set(error))
return r;
if (sd_bus_error_has_name(error, SD_BUS_ERROR_ACCESS_DENIED) ||
sd_bus_error_has_name(error, BUS_ERROR_ONLY_BY_DEPENDENCY) ||
sd_bus_error_has_name(error, BUS_ERROR_NO_ISOLATION) ||
sd_bus_error_has_name(error, BUS_ERROR_TRANSACTION_IS_DESTRUCTIVE))
if (sd_bus_error_has_names(error, SD_BUS_ERROR_ACCESS_DENIED,
BUS_ERROR_ONLY_BY_DEPENDENCY,
BUS_ERROR_NO_ISOLATION,
BUS_ERROR_TRANSACTION_IS_DESTRUCTIVE))
return EXIT_NOPERMISSION;
if (sd_bus_error_has_name(error, BUS_ERROR_NO_SUCH_UNIT))
return EXIT_NOTINSTALLED;
if (sd_bus_error_has_name(error, BUS_ERROR_JOB_TYPE_NOT_APPLICABLE) ||
sd_bus_error_has_name(error, SD_BUS_ERROR_NOT_SUPPORTED))
if (sd_bus_error_has_names(error, BUS_ERROR_JOB_TYPE_NOT_APPLICABLE,
SD_BUS_ERROR_NOT_SUPPORTED))
return EXIT_NOTIMPLEMENTED;
if (sd_bus_error_has_name(error, BUS_ERROR_LOAD_FAILED))
@ -552,8 +552,8 @@ static int get_unit_list(
return bus_log_create_error(r);
r = sd_bus_call(bus, m, 0, &error, &reply);
if (r < 0 && (sd_bus_error_has_name(&error, SD_BUS_ERROR_UNKNOWN_METHOD) ||
sd_bus_error_has_name(&error, SD_BUS_ERROR_ACCESS_DENIED))) {
if (r < 0 && (sd_bus_error_has_names(&error, SD_BUS_ERROR_UNKNOWN_METHOD,
SD_BUS_ERROR_ACCESS_DENIED))) {
/* Fallback to legacy ListUnitsFiltered method */
fallback = true;
log_debug_errno(r, "Failed to list units: %s Falling back to ListUnitsFiltered method.", bus_error_message(&error, r));
@ -2945,9 +2945,9 @@ fail:
log_error_errno(r, "Failed to %s %s: %s", job_type, name, bus_error_message(error, r));
if (!sd_bus_error_has_name(error, BUS_ERROR_NO_SUCH_UNIT) &&
!sd_bus_error_has_name(error, BUS_ERROR_UNIT_MASKED) &&
!sd_bus_error_has_name(error, BUS_ERROR_JOB_TYPE_NOT_APPLICABLE))
if (!sd_bus_error_has_names(error, BUS_ERROR_NO_SUCH_UNIT,
BUS_ERROR_UNIT_MASKED,
BUS_ERROR_JOB_TYPE_NOT_APPLICABLE))
log_error("See %s logs and 'systemctl%s status%s %s' for details.",
arg_scope == UNIT_FILE_SYSTEM ? "system" : "user",
arg_scope == UNIT_FILE_SYSTEM ? "" : " --user",

View File

@ -458,6 +458,8 @@ int sd_bus_error_copy(sd_bus_error *dest, const sd_bus_error *e);
int sd_bus_error_move(sd_bus_error *dest, sd_bus_error *e);
int sd_bus_error_is_set(const sd_bus_error *e);
int sd_bus_error_has_name(const sd_bus_error *e, const char *name);
int sd_bus_error_has_names_sentinel(const sd_bus_error *e, ...) _sd_sentinel_;
#define sd_bus_error_has_names(e, ...) sd_bus_error_has_names_sentinel(e, __VA_ARGS__, NULL)
#define SD_BUS_ERROR_MAP(_name, _code) \
{ \