[systemd-commits] 3 commits - Makefile.am src/core src/libsystemd src/shared src/systemd src/timedate
Zbigniew Jędrzejewski-Szmek
zbyszek at kemper.freedesktop.org
Thu Oct 30 17:38:56 PDT 2014
Makefile.am | 8 --
src/core/manager.c | 12 +--
src/core/snapshot.c | 2
src/core/transaction.c | 88 ++++++++++----------------
src/core/unit.c | 10 +-
src/libsystemd/sd-bus/.gitignore | 1
src/libsystemd/sd-bus/bus-error-mapping.gperf | 50 --------------
src/libsystemd/sd-bus/bus-error.c | 58 +++++++++++++++--
src/libsystemd/sd-bus/bus-error.h | 2
src/libsystemd/sd-bus/test-bus-error.c | 28 +++++++-
src/shared/bus-errors.c | 73 +++++++++++++++++++++
src/systemd/sd-bus.h | 13 +++
src/timedate/timedated.c | 16 ++--
13 files changed, 220 insertions(+), 141 deletions(-)
New commits:
commit 7cabba07745b388497e8c0fc19b61984167fd474
Author: Zbigniew Jędrzejewski-Szmek <zbyszek at in.waw.pl>
Date: Tue Oct 28 12:36:17 2014 -0400
snapshot: return error when snapshot exists
diff --git a/src/core/snapshot.c b/src/core/snapshot.c
index 5eed615..c2678cb 100644
--- a/src/core/snapshot.c
+++ b/src/core/snapshot.c
@@ -208,7 +208,7 @@ int snapshot_create(Manager *m, const char *name, bool cleanup, sd_bus_error *e,
return sd_bus_error_setf(e, SD_BUS_ERROR_INVALID_ARGS, "Unit name %s lacks snapshot suffix.", name);
if (manager_get_unit(m, name))
- sd_bus_error_setf(e, BUS_ERROR_UNIT_EXISTS, "Snapshot %s exists already.", name);
+ return sd_bus_error_setf(e, BUS_ERROR_UNIT_EXISTS, "Snapshot %s exists already.", name);
} else {
commit 7358dc029ab8cb8f9d83eb847389d84f822ccfe8
Author: Zbigniew Jędrzejewski-Szmek <zbyszek at in.waw.pl>
Date: Thu Oct 30 20:32:17 2014 -0400
Convert the rest to sd_bus_errnomap
I tried to preserve most errno values, but in some cases they were
inconsistent (different errno values for the same error name) or just
mismatched.
diff --git a/Makefile.am b/Makefile.am
index f1a972d..2e4f692 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -752,6 +752,7 @@ libsystemd_shared_la_SOURCES = \
src/shared/refcnt.h \
src/shared/udev-util.h \
src/shared/bus-errors.h \
+ src/shared/bus-errors.c \
src/shared/device-nodes.c \
src/shared/device-nodes.h \
src/shared/util.c \
diff --git a/src/core/manager.c b/src/core/manager.c
index d427d88..ed7fdc7 100644
--- a/src/core/manager.c
+++ b/src/core/manager.c
@@ -1189,15 +1189,11 @@ int manager_add_job(Manager *m, JobType type, Unit *unit, JobMode mode, bool ove
assert(unit);
assert(mode < _JOB_MODE_MAX);
- if (mode == JOB_ISOLATE && type != JOB_START) {
- sd_bus_error_setf(e, SD_BUS_ERROR_INVALID_ARGS, "Isolate is only valid for start.");
- return -EINVAL;
- }
+ if (mode == JOB_ISOLATE && type != JOB_START)
+ return sd_bus_error_setf(e, SD_BUS_ERROR_INVALID_ARGS, "Isolate is only valid for start.");
- if (mode == JOB_ISOLATE && !unit->allow_isolate) {
- sd_bus_error_setf(e, BUS_ERROR_NO_ISOLATION, "Operation refused, unit may not be isolated.");
- return -EPERM;
- }
+ if (mode == JOB_ISOLATE && !unit->allow_isolate)
+ return sd_bus_error_setf(e, BUS_ERROR_NO_ISOLATION, "Operation refused, unit may not be isolated.");
log_debug_unit(unit->id,
"Trying to enqueue job %s/%s/%s", unit->id,
diff --git a/src/core/transaction.c b/src/core/transaction.c
index dbb4133..488cb86 100644
--- a/src/core/transaction.c
+++ b/src/core/transaction.c
@@ -264,13 +264,12 @@ static int transaction_merge_jobs(Transaction *tr, sd_bus_error *e) {
return -EAGAIN;
/* We couldn't merge anything. Failure */
- sd_bus_error_setf(e, BUS_ERROR_TRANSACTION_JOBS_CONFLICTING,
- "Transaction contains conflicting jobs '%s' and '%s' for %s. "
- "Probably contradicting requirement dependencies configured.",
- job_type_to_string(t),
- job_type_to_string(k->type),
- k->unit->id);
- return r;
+ return sd_bus_error_setf(e, BUS_ERROR_TRANSACTION_JOBS_CONFLICTING,
+ "Transaction contains conflicting jobs '%s' and '%s' for %s. "
+ "Probably contradicting requirement dependencies configured.",
+ job_type_to_string(t),
+ job_type_to_string(k->type),
+ k->unit->id);
}
}
@@ -412,9 +411,8 @@ static int transaction_verify_order_one(Transaction *tr, Job *j, Job *from, unsi
log_error("Unable to break cycle");
- sd_bus_error_setf(e, BUS_ERROR_TRANSACTION_ORDER_IS_CYCLIC,
- "Transaction order is cyclic. See system logs for details.");
- return -ENOEXEC;
+ return sd_bus_error_setf(e, BUS_ERROR_TRANSACTION_ORDER_IS_CYCLIC,
+ "Transaction order is cyclic. See system logs for details.");
}
/* Make the marker point to where we come from, so that we can
@@ -513,12 +511,9 @@ static int transaction_is_destructive(Transaction *tr, JobMode mode, sd_bus_erro
assert(!j->transaction_next);
if (j->unit->job && (mode == JOB_FAIL || j->unit->job->irreversible) &&
- !job_type_is_superset(j->type, j->unit->job->type)) {
-
- sd_bus_error_setf(e, BUS_ERROR_TRANSACTION_IS_DESTRUCTIVE,
- "Transaction is destructive.");
- return -EEXIST;
- }
+ !job_type_is_superset(j->type, j->unit->job->type))
+ return sd_bus_error_setf(e, BUS_ERROR_TRANSACTION_IS_DESTRUCTIVE,
+ "Transaction is destructive.");
}
return 0;
@@ -860,50 +855,39 @@ int transaction_add_job_and_dependencies(
/* by ? by->unit->id : "NA", */
/* by ? job_type_to_string(by->type) : "NA"); */
- if (unit->load_state != UNIT_LOADED &&
- unit->load_state != UNIT_ERROR &&
- unit->load_state != UNIT_NOT_FOUND &&
- unit->load_state != UNIT_MASKED) {
- sd_bus_error_setf(e, BUS_ERROR_LOAD_FAILED,
- "Unit %s is not loaded properly.", unit->id);
- return -EINVAL;
- }
+ if (!IN_SET(unit->load_state, UNIT_LOADED, UNIT_ERROR, UNIT_NOT_FOUND, UNIT_MASKED))
+ return sd_bus_error_setf(e, BUS_ERROR_LOAD_FAILED,
+ "Unit %s is not loaded properly.", unit->id);
if (type != JOB_STOP && unit->load_state == UNIT_ERROR) {
if (unit->load_error == -ENOENT || unit->manager->test_run)
- sd_bus_error_setf(e, BUS_ERROR_LOAD_FAILED,
- "Unit %s failed to load: %s.",
- unit->id,
- strerror(-unit->load_error));
+ return sd_bus_error_setf(e, BUS_ERROR_LOAD_FAILED,
+ "Unit %s failed to load: %s.",
+ unit->id,
+ strerror(-unit->load_error));
else
- sd_bus_error_setf(e, BUS_ERROR_LOAD_FAILED,
- "Unit %s failed to load: %s. "
- "See system logs and 'systemctl status %s' for details.",
- unit->id,
- strerror(-unit->load_error),
- unit->id);
- return -EINVAL;
+ return sd_bus_error_setf(e, BUS_ERROR_LOAD_FAILED,
+ "Unit %s failed to load: %s. "
+ "See system logs and 'systemctl status %s' for details.",
+ unit->id,
+ strerror(-unit->load_error),
+ unit->id);
}
- if (type != JOB_STOP && unit->load_state == UNIT_NOT_FOUND) {
- sd_bus_error_setf(e, BUS_ERROR_LOAD_FAILED,
- "Unit %s failed to load: %s.",
- unit->id, strerror(-unit->load_error));
- return -EINVAL;
- }
+ if (type != JOB_STOP && unit->load_state == UNIT_NOT_FOUND)
+ return sd_bus_error_setf(e, BUS_ERROR_LOAD_FAILED,
+ "Unit %s failed to load: %s.",
+ unit->id, strerror(-unit->load_error));
- if (type != JOB_STOP && unit->load_state == UNIT_MASKED) {
- sd_bus_error_setf(e, BUS_ERROR_UNIT_MASKED,
- "Unit %s is masked.", unit->id);
- return -EADDRNOTAVAIL;
- }
+ if (type != JOB_STOP && unit->load_state == UNIT_MASKED)
+ return sd_bus_error_setf(e, BUS_ERROR_UNIT_MASKED,
+ "Unit %s is masked.", unit->id);
+
+ if (!unit_job_is_applicable(unit, type))
+ return sd_bus_error_setf(e, BUS_ERROR_JOB_TYPE_NOT_APPLICABLE,
+ "Job type %s is not applicable for unit %s.",
+ job_type_to_string(type), unit->id);
- if (!unit_job_is_applicable(unit, type)) {
- sd_bus_error_setf(e, BUS_ERROR_JOB_TYPE_NOT_APPLICABLE,
- "Job type %s is not applicable for unit %s.",
- job_type_to_string(type), unit->id);
- return -EBADR;
- }
/* First add the job. */
ret = transaction_add_one_job(tr, type, unit, override, &is_new);
diff --git a/src/core/unit.c b/src/core/unit.c
index 84f210a..d5acc72 100644
--- a/src/core/unit.c
+++ b/src/core/unit.c
@@ -2939,18 +2939,16 @@ int unit_kill_common(
if (who == KILL_MAIN && main_pid <= 0) {
if (main_pid < 0)
- sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_PROCESS, "%s units have no main processes", unit_type_to_string(u->type));
+ return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_PROCESS, "%s units have no main processes", unit_type_to_string(u->type));
else
- sd_bus_error_set_const(error, BUS_ERROR_NO_SUCH_PROCESS, "No main process to kill");
- return -ESRCH;
+ return sd_bus_error_set_const(error, BUS_ERROR_NO_SUCH_PROCESS, "No main process to kill");
}
if (who == KILL_CONTROL && control_pid <= 0) {
if (control_pid < 0)
- sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_PROCESS, "%s units have no control processes", unit_type_to_string(u->type));
+ return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_PROCESS, "%s units have no control processes", unit_type_to_string(u->type));
else
- sd_bus_error_set_const(error, BUS_ERROR_NO_SUCH_PROCESS, "No control process to kill");
- return -ESRCH;
+ return sd_bus_error_set_const(error, BUS_ERROR_NO_SUCH_PROCESS, "No control process to kill");
}
if (who == KILL_CONTROL || who == KILL_ALL)
diff --git a/src/shared/bus-errors.c b/src/shared/bus-errors.c
new file mode 100644
index 0000000..31d00ba
--- /dev/null
+++ b/src/shared/bus-errors.c
@@ -0,0 +1,73 @@
+/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
+
+/***
+ This file is part of systemd.
+
+ Copyright 2014 Zbigniew Jędrzejewski-Szmek
+
+ systemd is free software; you can redistribute it and/or modify it
+ under the terms of the GNU Lesser General Public License as published by
+ the Free Software Foundation; either version 2.1 of the License, or
+ (at your option) any later version.
+
+ systemd is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public License
+ along with systemd; If not, see <http://www.gnu.org/licenses/>.
+***/
+
+#include <errno.h>
+
+#include "sd-bus.h"
+#include "bus-errors.h"
+
+SD_BUS_ERROR_MAPPING = {
+ {BUS_ERROR_NO_SUCH_UNIT, ENOENT},
+ {BUS_ERROR_NO_UNIT_FOR_PID, ESRCH},
+ {BUS_ERROR_UNIT_EXISTS, EEXIST},
+ {BUS_ERROR_LOAD_FAILED, EIO},
+ {BUS_ERROR_JOB_FAILED, EREMOTEIO},
+ {BUS_ERROR_NO_SUCH_JOB, ENOENT},
+ {BUS_ERROR_NOT_SUBSCRIBED, EINVAL},
+ {BUS_ERROR_ALREADY_SUBSCRIBED, EINVAL},
+ {BUS_ERROR_ONLY_BY_DEPENDENCY, EINVAL},
+ {BUS_ERROR_TRANSACTION_JOBS_CONFLICTING, EDEADLOCK},
+ {BUS_ERROR_TRANSACTION_ORDER_IS_CYCLIC, EDEADLOCK},
+ {BUS_ERROR_TRANSACTION_IS_DESTRUCTIVE, EDEADLOCK},
+ {BUS_ERROR_UNIT_MASKED, ENOSYS},
+ {BUS_ERROR_JOB_TYPE_NOT_APPLICABLE, EBADR},
+ {BUS_ERROR_NO_ISOLATION, EPERM},
+ {BUS_ERROR_SHUTTING_DOWN, ECANCELED},
+ {BUS_ERROR_SCOPE_NOT_RUNNING, EHOSTDOWN},
+
+ {BUS_ERROR_NO_SUCH_MACHINE, ENXIO},
+ {BUS_ERROR_NO_MACHINE_FOR_PID, ENXIO},
+ {BUS_ERROR_MACHINE_EXISTS, EEXIST},
+ {BUS_ERROR_NO_PRIVATE_NETWORKING, ENOSYS},
+
+ {BUS_ERROR_NO_SUCH_SESSION, ENXIO},
+ {BUS_ERROR_NO_SESSION_FOR_PID, ENXIO},
+ {BUS_ERROR_NO_SUCH_USER, ENXIO},
+ {BUS_ERROR_NO_USER_FOR_PID, ENXIO},
+ {BUS_ERROR_NO_SUCH_SEAT, ENXIO},
+ {BUS_ERROR_SESSION_NOT_ON_SEAT, EINVAL},
+ {BUS_ERROR_NOT_IN_CONTROL, EINVAL},
+ {BUS_ERROR_DEVICE_IS_TAKEN, EINVAL},
+ {BUS_ERROR_DEVICE_NOT_TAKEN, EINVAL},
+ {BUS_ERROR_OPERATION_IN_PROGRESS, EINPROGRESS},
+ {BUS_ERROR_SLEEP_VERB_NOT_SUPPORTED, ENOSYS},
+
+ {BUS_ERROR_AUTOMATIC_TIME_SYNC_ENABLED, EALREADY},
+
+ {BUS_ERROR_NO_SUCH_PROCESS, ESRCH},
+
+ {BUS_ERROR_NO_NAME_SERVERS, EIO},
+ {BUS_ERROR_INVALID_REPLY, EINVAL},
+ {BUS_ERROR_NO_SUCH_RR, ENOENT},
+ {BUS_ERROR_NO_RESOURCES, ENOMEM},
+ {BUS_ERROR_CNAME_LOOP, EDEADLOCK},
+ {BUS_ERROR_ABORTED, ECANCELED},
+};
diff --git a/src/timedate/timedated.c b/src/timedate/timedated.c
index 8880812..49a957c 100644
--- a/src/timedate/timedated.c
+++ b/src/timedate/timedated.c
@@ -44,6 +44,10 @@
#define NULL_ADJTIME_UTC "0.0 0 0\n0\nUTC\n"
#define NULL_ADJTIME_LOCAL "0.0 0 0\n0\nLOCAL\n"
+SD_BUS_ERROR_MAPPING = {
+ {"org.freedesktop.timedate1.NoNTPSupport", ENOTSUP},
+};
+
typedef struct Context {
char *zone;
bool local_rtc;
@@ -254,10 +258,8 @@ static int context_start_ntp(Context *c, sd_bus *bus, sd_bus_error *error) {
if (r < 0) {
if (sd_bus_error_has_name(error, SD_BUS_ERROR_FILE_NOT_FOUND) ||
sd_bus_error_has_name(error, "org.freedesktop.systemd1.LoadFailed") ||
- sd_bus_error_has_name(error, "org.freedesktop.systemd1.NoSuchUnit")) {
- sd_bus_error_set_const(error, "org.freedesktop.timedate1.NoNTPSupport", "NTP not supported.");
- return -ENOTSUP;
- }
+ sd_bus_error_has_name(error, "org.freedesktop.systemd1.NoSuchUnit"))
+ return sd_bus_error_set_const(error, "org.freedesktop.timedate1.NoNTPSupport", "NTP not supported.");
return r;
}
@@ -298,10 +300,8 @@ static int context_enable_ntp(Context*c, sd_bus *bus, sd_bus_error *error) {
false);
if (r < 0) {
- if (sd_bus_error_has_name(error, SD_BUS_ERROR_FILE_NOT_FOUND)) {
- sd_bus_error_set_const(error, "org.freedesktop.timedate1.NoNTPSupport", "NTP not supported.");
- return -ENOTSUP;
- }
+ if (sd_bus_error_has_name(error, SD_BUS_ERROR_FILE_NOT_FOUND))
+ return sd_bus_error_set_const(error, "org.freedesktop.timedate1.NoNTPSupport", "NTP not supported.");
return r;
}
commit 4a0a74179fcfb85c456794fd91fa2ae5d4d3bc8f
Author: Zbigniew Jędrzejewski-Szmek <zbyszek at in.waw.pl>
Date: Thu Oct 30 20:31:48 2014 -0400
bus: add sd_bus_errnomap section
This allows custom "name" ↔ errno mappings to be registered.
Tables from all compilation units are concatenated.
diff --git a/Makefile.am b/Makefile.am
index 3b273d4..f1a972d 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -2592,8 +2592,7 @@ libsystemd_internal_la_SOURCES = \
src/libsystemd/sd-network/network-util.c
nodist_libsystemd_internal_la_SOURCES = \
- src/libsystemd/libsystemd.sym \
- src/libsystemd/sd-bus/bus-error-mapping.c
+ src/libsystemd/libsystemd.sym
libsystemd_internal_la_CFLAGS = \
$(AM_CFLAGS) \
@@ -2631,13 +2630,11 @@ noinst_LTLIBRARIES += \
EXTRA_DIST += \
src/libsystemd/libsystemd.sym.m4 \
src/libsystemd/libsystemd.pc.in \
- src/libsystemd/sd-bus/bus-error-mapping.gperf \
src/libsystemd/sd-bus/DIFFERENCES \
src/libsystemd/sd-bus/GVARIANT-SERIALIZATION
CLEANFILES += \
- src/libsystemd/libsystemd.sym \
- src/libsystemd/sd-bus/bus-error-mapping.c
+ src/libsystemd/libsystemd.sym
BUILT_SOURCES += \
src/libsystemd/libsystemd.sym
diff --git a/src/libsystemd/sd-bus/.gitignore b/src/libsystemd/sd-bus/.gitignore
deleted file mode 100644
index d32542e..0000000
--- a/src/libsystemd/sd-bus/.gitignore
+++ /dev/null
@@ -1 +0,0 @@
-/bus-error-mapping.c
diff --git a/src/libsystemd/sd-bus/bus-error-mapping.gperf b/src/libsystemd/sd-bus/bus-error-mapping.gperf
deleted file mode 100644
index 59eaa35..0000000
--- a/src/libsystemd/sd-bus/bus-error-mapping.gperf
+++ /dev/null
@@ -1,50 +0,0 @@
-%{
-#include <errno.h>
-#include "bus-error.h"
-%}
-name_error_mapping;
-%null_strings
-%language=ANSI-C
-%define slot-name name
-%define hash-function-name bus_error_mapping_hash
-%define lookup-function-name bus_error_mapping_lookup
-%readonly-tables
-%omit-struct-type
-%struct-type
-%includes
-%%
-org.freedesktop.DBus.Error.Failed, EACCES
-org.freedesktop.DBus.Error.NoMemory, ENOMEM
-org.freedesktop.DBus.Error.ServiceUnknown, EHOSTUNREACH
-org.freedesktop.DBus.Error.NameHasNoOwner, ENXIO
-org.freedesktop.DBus.Error.NoReply, ETIMEDOUT
-org.freedesktop.DBus.Error.IOError, EIO
-org.freedesktop.DBus.Error.BadAddress, EADDRNOTAVAIL
-org.freedesktop.DBus.Error.NotSupported, ENOTSUP
-org.freedesktop.DBus.Error.LimitsExceeded, ENOBUFS
-org.freedesktop.DBus.Error.AccessDenied, EACCES
-org.freedesktop.DBus.Error.AuthFailed, EACCES
-org.freedesktop.DBus.Error.InteractiveAuthorizationRequired EACCES
-org.freedesktop.DBus.Error.NoServer, EHOSTDOWN
-org.freedesktop.DBus.Error.Timeout, ETIMEDOUT
-org.freedesktop.DBus.Error.NoNetwork, ENONET
-org.freedesktop.DBus.Error.AddressInUse, EADDRINUSE
-org.freedesktop.DBus.Error.Disconnected, ECONNRESET
-org.freedesktop.DBus.Error.InvalidArgs, EINVAL
-org.freedesktop.DBus.Error.FileNotFound, ENOENT
-org.freedesktop.DBus.Error.FileExists, EEXIST
-org.freedesktop.DBus.Error.UnknownMethod, EBADR
-org.freedesktop.DBus.Error.UnknownObject, EBADR
-org.freedesktop.DBus.Error.UnknownInterface, EBADR
-org.freedesktop.DBus.Error.UnknownProperty, EBADR
-org.freedesktop.DBus.Error.PropertyReadOnly, EROFS
-org.freedesktop.DBus.Error.UnixProcessIdUnknown, ESRCH
-org.freedesktop.DBus.Error.InvalidSignature, EINVAL
-org.freedesktop.DBus.Error.InconsistentMessage, EBADMSG
-#
-org.freedesktop.DBus.Error.TimedOut, ETIMEDOUT
-org.freedesktop.DBus.Error.MatchRuleInvalid, EINVAL
-org.freedesktop.DBus.Error.InvalidFileContent, EINVAL
-org.freedesktop.DBus.Error.MatchRuleNotFound, ENOENT
-org.freedesktop.DBus.Error.SELinuxSecurityContextUnknown, ESRCH
-org.freedesktop.DBus.Error.ObjectPathInUse, EBUSY
diff --git a/src/libsystemd/sd-bus/bus-error.c b/src/libsystemd/sd-bus/bus-error.c
index af83c12..2e64b92 100644
--- a/src/libsystemd/sd-bus/bus-error.c
+++ b/src/libsystemd/sd-bus/bus-error.c
@@ -35,10 +35,60 @@
#define BUS_ERROR_OOM SD_BUS_ERROR_MAKE_CONST(SD_BUS_ERROR_NO_MEMORY, "Out of memory")
#define BUS_ERROR_FAILED SD_BUS_ERROR_MAKE_CONST(SD_BUS_ERROR_FAILED, "Operation failed")
+SD_BUS_ERROR_MAPPING = {
+ {"org.freedesktop.DBus.Error.Failed", EACCES},
+ {"org.freedesktop.DBus.Error.NoMemory", ENOMEM},
+ {"org.freedesktop.DBus.Error.ServiceUnknown", EHOSTUNREACH},
+ {"org.freedesktop.DBus.Error.NameHasNoOwner", ENXIO},
+ {"org.freedesktop.DBus.Error.NoReply", ETIMEDOUT},
+ {"org.freedesktop.DBus.Error.IOError", EIO},
+ {"org.freedesktop.DBus.Error.BadAddress", EADDRNOTAVAIL},
+ {"org.freedesktop.DBus.Error.NotSupported", ENOTSUP},
+ {"org.freedesktop.DBus.Error.LimitsExceeded", ENOBUFS},
+ {"org.freedesktop.DBus.Error.AccessDenied", EACCES},
+ {"org.freedesktop.DBus.Error.AuthFailed", EACCES},
+ {"org.freedesktop.DBus.Error.InteractiveAuthorizationRequired", EACCES},
+ {"org.freedesktop.DBus.Error.NoServer", EHOSTDOWN},
+ {"org.freedesktop.DBus.Error.Timeout", ETIMEDOUT},
+ {"org.freedesktop.DBus.Error.NoNetwork", ENONET},
+ {"org.freedesktop.DBus.Error.AddressInUse", EADDRINUSE},
+ {"org.freedesktop.DBus.Error.Disconnected", ECONNRESET},
+ {"org.freedesktop.DBus.Error.InvalidArgs", EINVAL},
+ {"org.freedesktop.DBus.Error.FileNotFound", ENOENT},
+ {"org.freedesktop.DBus.Error.FileExists", EEXIST},
+ {"org.freedesktop.DBus.Error.UnknownMethod", EBADR},
+ {"org.freedesktop.DBus.Error.UnknownObject", EBADR},
+ {"org.freedesktop.DBus.Error.UnknownInterface", EBADR},
+ {"org.freedesktop.DBus.Error.UnknownProperty", EBADR},
+ {"org.freedesktop.DBus.Error.PropertyReadOnly", EROFS},
+ {"org.freedesktop.DBus.Error.UnixProcessIdUnknown", ESRCH},
+ {"org.freedesktop.DBus.Error.InvalidSignature", EINVAL},
+ {"org.freedesktop.DBus.Error.InconsistentMessage", EBADMSG},
+
+ {"org.freedesktop.DBus.Error.TimedOut", ETIMEDOUT},
+ {"org.freedesktop.DBus.Error.MatchRuleInvalid", EINVAL},
+ {"org.freedesktop.DBus.Error.InvalidFileContent", EINVAL},
+ {"org.freedesktop.DBus.Error.MatchRuleNotFound", ENOENT},
+ {"org.freedesktop.DBus.Error.SELinuxSecurityContextUnknown", ESRCH},
+ {"org.freedesktop.DBus.Error.ObjectPathInUse", EBUSY},
+};
+
+extern const sd_bus_name_error_mapping __start_sd_bus_errnomap[];
+extern const sd_bus_name_error_mapping __stop_sd_bus_errnomap[];
+
+static int bus_error_mapping_lookup(const char *name, size_t len) {
+ const sd_bus_name_error_mapping *m;
+
+ for (m = __start_sd_bus_errnomap; m < __stop_sd_bus_errnomap; m++)
+ if (strneq(m->name, name, len))
+ return m->code;
+
+ return EIO;
+}
+
static int bus_error_name_to_errno(const char *name) {
const char *p;
int r;
- const name_error_mapping *m;
if (!name)
return EINVAL;
@@ -52,11 +102,7 @@ static int bus_error_name_to_errno(const char *name) {
return r;
}
- m = bus_error_mapping_lookup(name, strlen(name));
- if (m)
- return m->code;
-
- return EIO;
+ return bus_error_mapping_lookup(name, strlen(name));
}
static sd_bus_error errno_to_bus_error_const(int error) {
diff --git a/src/libsystemd/sd-bus/bus-error.h b/src/libsystemd/sd-bus/bus-error.h
index cf0ad9d..1469486 100644
--- a/src/libsystemd/sd-bus/bus-error.h
+++ b/src/libsystemd/sd-bus/bus-error.h
@@ -32,8 +32,6 @@ struct name_error_mapping {
};
typedef struct name_error_mapping name_error_mapping;
-const name_error_mapping* bus_error_mapping_lookup(const char *str, unsigned int len);
-
bool bus_error_is_dirty(sd_bus_error *e);
const char *bus_error_message(const sd_bus_error *e, int error);
diff --git a/src/libsystemd/sd-bus/test-bus-error.c b/src/libsystemd/sd-bus/test-bus-error.c
index b78be54..dd0cca4 100644
--- a/src/libsystemd/sd-bus/test-bus-error.c
+++ b/src/libsystemd/sd-bus/test-bus-error.c
@@ -23,8 +23,7 @@
#include "bus-error.h"
#include "bus-util.h"
-int main(int argc, char *argv[]) {
-
+static void test_error(void) {
_cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL, second = SD_BUS_ERROR_NULL;
const sd_bus_error const_error = SD_BUS_ERROR_MAKE_CONST(SD_BUS_ERROR_FILE_EXISTS, "const error");
const sd_bus_error temporarily_const_error = {
@@ -110,6 +109,31 @@ int main(int argc, char *argv[]) {
assert_se(sd_bus_error_has_name(&error, SD_BUS_ERROR_IO_ERROR));
assert_se(sd_bus_error_get_errno(&error) == EIO);
assert_se(sd_bus_error_is_set(&error));
+}
+
+static void test_errno_mapping_standard(void) {
+ assert_se(sd_bus_error_set(NULL, "System.Error.EUCLEAN", NULL) == -EUCLEAN);
+ assert_se(sd_bus_error_set(NULL, "System.Error.EBUSY", NULL) == -EBUSY);
+ assert_se(sd_bus_error_set(NULL, "System.Error.EINVAL", NULL) == -EINVAL);
+ assert_se(sd_bus_error_set(NULL, "System.Error.WHATSIT", NULL) == -EIO);
+}
+
+SD_BUS_ERROR_MAPPING = {
+ {"org.freedesktop.custom-dbus-error", 5},
+ {"org.freedesktop.custom-dbus-error-2", 52},
+};
+
+static void test_errno_mapping_custom(void) {
+ assert_se(sd_bus_error_set(NULL, "org.freedesktop.custom-dbus-error", NULL) == -5);
+ assert_se(sd_bus_error_set(NULL, "org.freedesktop.custom-dbus-error-2", NULL) == -52);
+ assert_se(sd_bus_error_set(NULL, "org.freedesktop.custom-dbus-error-x", NULL) == -EIO);
+}
+
+int main(int argc, char *argv[]) {
+
+ test_error();
+ test_errno_mapping_standard();
+ test_errno_mapping_custom();
return 0;
}
diff --git a/src/systemd/sd-bus.h b/src/systemd/sd-bus.h
index 18acfc2..c95b5e7 100644
--- a/src/systemd/sd-bus.h
+++ b/src/systemd/sd-bus.h
@@ -326,8 +326,21 @@ int sd_bus_creds_get_connection_name(sd_bus_creds *c, const char **name);
/* Error structures */
+struct sd_bus_name_error_mapping {
+ const char* name;
+ int code;
+};
+typedef struct sd_bus_name_error_mapping sd_bus_name_error_mapping;
+
#define SD_BUS_ERROR_MAKE_CONST(name, message) ((const sd_bus_error) {(name), (message), 0})
#define SD_BUS_ERROR_NULL SD_BUS_ERROR_MAKE_CONST(NULL, NULL)
+#ifndef SD_BUS_ERROR_MAPPING
+# define _SD_BUS_ERROR_XCONCAT(x, y) x ## y
+# define _SD_BUS_ERROR_CONCAT(x, y) _SD_BUS_ERROR_XCONCAT(x, y)
+# define SD_BUS_ERROR_MAPPING \
+ __attribute((__section__("sd_bus_errnomap"))) __attribute((__used__)) \
+ static const sd_bus_name_error_mapping _SD_BUS_ERROR_CONCAT(_sd_bus_errno_mapping_, __COUNTER__)[]
+#endif
void sd_bus_error_free(sd_bus_error *e);
int sd_bus_error_set(sd_bus_error *e, const char *name, const char *message);
More information about the systemd-commits
mailing list