From 1c2e9646e4a1720fc8ad35c705c195ae1a2c5ce0 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Thu, 30 Apr 2015 01:29:00 +0200 Subject: core: simplify unit type detection logic Introduce a new call unit_type_supported() and make use of it everywhere. Also, drop Manager parameter from per-type supported method prototype. --- src/core/automount.c | 4 +--- src/core/busname.c | 3 +-- src/core/device.c | 3 +-- src/core/manager.c | 2 +- src/core/swap.c | 2 +- src/core/unit.c | 16 ++++++++++++++-- src/core/unit.h | 8 +++++++- 7 files changed, 26 insertions(+), 12 deletions(-) (limited to 'src/core') diff --git a/src/core/automount.c b/src/core/automount.c index b1109bd67c..73b75f163e 100644 --- a/src/core/automount.c +++ b/src/core/automount.c @@ -1012,11 +1012,9 @@ static void automount_reset_failed(Unit *u) { a->result = AUTOMOUNT_SUCCESS; } -static bool automount_supported(Manager *m) { +static bool automount_supported(void) { static int supported = -1; - assert(m); - if (supported < 0) supported = access("/dev/autofs", F_OK) >= 0; diff --git a/src/core/busname.c b/src/core/busname.c index 94db122633..20d49fe3e9 100644 --- a/src/core/busname.c +++ b/src/core/busname.c @@ -984,9 +984,8 @@ static int busname_get_timeout(Unit *u, uint64_t *timeout) { return 1; } -static bool busname_supported(Manager *m) { +static bool busname_supported(void) { static int supported = -1; - assert(m); if (supported < 0) supported = access("/sys/fs/kdbus", F_OK) >= 0; diff --git a/src/core/device.c b/src/core/device.c index 5656c96669..f8e65b876f 100644 --- a/src/core/device.c +++ b/src/core/device.c @@ -750,9 +750,8 @@ static int device_dispatch_io(sd_event_source *source, int fd, uint32_t revents, return 0; } -static bool device_supported(Manager *m) { +static bool device_supported(void) { static int read_only = -1; - assert(m); /* If /sys is read-only we don't support device units, and any * attempts to start one should fail immediately. */ diff --git a/src/core/manager.c b/src/core/manager.c index cf7337eff5..b49452151b 100644 --- a/src/core/manager.c +++ b/src/core/manager.c @@ -975,7 +975,7 @@ int manager_enumerate(Manager *m) { for (c = 0; c < _UNIT_TYPE_MAX; c++) { int q; - if (unit_vtable[c]->supported && !unit_vtable[c]->supported(m)) { + if (!unit_type_supported(c)) { log_debug("Unit type .%s is not supported on this system.", unit_type_to_string(c)); continue; } diff --git a/src/core/swap.c b/src/core/swap.c index ae45b80967..a9834a7fe5 100644 --- a/src/core/swap.c +++ b/src/core/swap.c @@ -1407,7 +1407,7 @@ static int swap_get_timeout(Unit *u, uint64_t *timeout) { return 1; } -static bool swap_supported(Manager *m) { +static bool swap_supported(void) { static int supported = -1; /* If swap support is not available in the kernel, or we are diff --git a/src/core/unit.c b/src/core/unit.c index 7ef8dbbf48..6071bd51b1 100644 --- a/src/core/unit.c +++ b/src/core/unit.c @@ -1448,7 +1448,7 @@ int unit_start(Unit *u) { return unit_start(following); } - if (UNIT_VTABLE(u)->supported && !UNIT_VTABLE(u)->supported(u->manager)) + if (!unit_supported(u)) return -EOPNOTSUPP; /* If it is stopped, but we cannot start it, then fail */ @@ -2855,7 +2855,7 @@ int unit_add_node_link(Unit *u, const char *what, bool wants) { /* When device units aren't supported (such as in a * container), don't create dependencies on them. */ - if (unit_vtable[UNIT_DEVICE]->supported && !unit_vtable[UNIT_DEVICE]->supported(u->manager)) + if (!unit_type_supported(UNIT_DEVICE)) return 0; e = unit_name_from_path(what, ".device"); @@ -3690,6 +3690,18 @@ int unit_setup_exec_runtime(Unit *u) { return exec_runtime_make(rt, unit_get_exec_context(u), u->id); } +bool unit_type_supported(UnitType t) { + if (_unlikely_(t < 0)) + return false; + if (_unlikely_(t >= _UNIT_TYPE_MAX)) + return false; + + if (!unit_vtable[t]->supported) + return true; + + return unit_vtable[t]->supported(); +} + static const char* const unit_active_state_table[_UNIT_ACTIVE_STATE_MAX] = { [UNIT_ACTIVE] = "active", [UNIT_RELOADING] = "reloading", diff --git a/src/core/unit.h b/src/core/unit.h index 1a44271bc6..31b12157c2 100644 --- a/src/core/unit.h +++ b/src/core/unit.h @@ -402,7 +402,7 @@ struct UnitVTable { /* If this function is set and return false all jobs for units * of this type will immediately fail. */ - bool (*supported)(Manager *m); + bool (*supported)(void); /* The interface name */ const char *bus_interface; @@ -601,6 +601,12 @@ int unit_make_transient(Unit *u); int unit_require_mounts_for(Unit *u, const char *path); +bool unit_type_supported(UnitType t); + +static inline bool unit_supported(Unit *u) { + return unit_type_supported(u->type); +} + const char *unit_active_state_to_string(UnitActiveState i) _const_; UnitActiveState unit_active_state_from_string(const char *s) _pure_; -- cgit v1.2.3-54-g00ecf