]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
core: add possibility to not track certain unit types
authorMichal Sekletar <msekleta@redhat.com>
Wed, 7 Sep 2022 15:37:34 +0000 (17:37 +0200)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Thu, 24 Nov 2022 00:28:22 +0000 (09:28 +0900)
docs/ENVIRONMENT.md
src/core/unit.c

index 32bd9598b7f4330181d41168bbfad62ed574687c..01ee0655833061b58ac18767f0399992734ea9aa 100644 (file)
@@ -274,6 +274,15 @@ All tools:
   it is either set to `system` or `user` depending on whether the NSS/PAM
   module is called by systemd in `--system` or `--user` mode.
 
+* `$SYSTEMD_SUPPORT_DEVICE`, `$SYSTEMD_SUPPORT_MOUNT`, `$SYSTEMD_SUPPORT_SWAP` -
+  can be set to `0` to mark respective unit type as unsupported. Generally,
+  having less units saves system resources so these options might be useful
+  for cases where we don't need to track given unit type, e.g. `--user` manager
+  often doesn't need to deal with device or swap units because they are
+  handled by the `--system` manager (PID 1). Note that setting certain unit
+  type as unsupported may not prevent loading some units of that type if they
+  are referenced by other units of another supported type.
+
 `systemd-remount-fs`:
 
 * `$SYSTEMD_REMOUNT_ROOT_RW=1` — if set and no entry for the root directory
index c8dc97b99d7bfa05d6717ba270b1c5268be5f287..29b07a6e7a84dc43b657ac81fc1ee6da3794b2bb 100644 (file)
@@ -22,6 +22,7 @@
 #include "dbus-unit.h"
 #include "dbus.h"
 #include "dropin.h"
+#include "env-util.h"
 #include "escape.h"
 #include "execute.h"
 #include "fd-util.h"
@@ -4781,11 +4782,28 @@ int unit_setup_dynamic_creds(Unit *u) {
 }
 
 bool unit_type_supported(UnitType t) {
+        static int8_t cache[_UNIT_TYPE_MAX] = {}; /* -1: disabled, 1: enabled: 0: don't know */
+        int r;
+
         if (_unlikely_(t < 0))
                 return false;
         if (_unlikely_(t >= _UNIT_TYPE_MAX))
                 return false;
 
+        if (cache[t] == 0) {
+                char *e;
+
+                e = strjoina("SYSTEMD_SUPPORT_", unit_type_to_string(t));
+
+                r = getenv_bool(ascii_strupper(e));
+                if (r < 0 && r != -ENXIO)
+                        log_debug_errno(r, "Failed to parse $%s, ignoring: %m", e);
+
+                cache[t] = r == 0 ? -1 : 1;
+        }
+        if (cache[t] < 0)
+                return false;
+
         if (!unit_vtable[t]->supported)
                 return true;