core: simplify unit type detection logic
authorLennart Poettering <lennart@poettering.net>
Wed, 29 Apr 2015 23:29:00 +0000 (01:29 +0200)
committerLennart Poettering <lennart@poettering.net>
Wed, 29 Apr 2015 23:29:00 +0000 (01:29 +0200)
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
src/core/busname.c
src/core/device.c
src/core/manager.c
src/core/swap.c
src/core/unit.c
src/core/unit.h

index b1109bd..73b75f1 100644 (file)
@@ -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;
 
index 94db122..20d49fe 100644 (file)
@@ -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;
index 5656c96..f8e65b8 100644 (file)
@@ -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. */
index cf7337e..b494521 100644 (file)
@@ -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;
                 }
index ae45b80..a9834a7 100644 (file)
@@ -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
index 7ef8dbb..6071bd5 100644 (file)
@@ -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",
index 1a44271..31b1215 100644 (file)
@@ -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_;