[systemd-devel] [PATCH v2] Add support for transient presets, applied on every boot.
Dimitri John Ledkov
dimitri.j.ledkov at intel.com
Fri Feb 20 18:38:06 PST 2015
---
src/core/main.c | 27 +++++++++++++++++++++++++++
src/core/unit.c | 2 +-
src/shared/install.c | 25 ++++++++++++++++++++-----
src/shared/install.h | 2 +-
4 files changed, 49 insertions(+), 7 deletions(-)
diff --git a/src/core/main.c b/src/core/main.c
index 08f46f5..2656779 100644
--- a/src/core/main.c
+++ b/src/core/main.c
@@ -1207,6 +1207,23 @@ static int write_container_id(void) {
return write_string_file("/run/systemd/container", c);
}
+static int transient_presets(void) {
+ struct stat st;
+
+ if (lstat("/usr/lib/systemd/system-preset-transient", &st) == 0)
+ return !!S_ISDIR(st.st_mode);
+
+#ifdef HAVE_SPLIT_USR
+ if (lstat("/lib/systemd/system-preset-transient", &st) == 0)
+ return !!S_ISDIR(st.st_mode);
+#endif
+
+ if (lstat("/etc/systemd/system-preset-transient", &st) == 0)
+ return !!S_ISDIR(st.st_mode);
+
+ return 0;
+}
+
int main(int argc, char *argv[]) {
Manager *m = NULL;
int r, retval = EXIT_FAILURE;
@@ -1619,6 +1636,16 @@ int main(int argc, char *argv[]) {
if (arg_running_as == SYSTEMD_SYSTEM) {
bump_rlimit_nofile(&saved_rlimit_nofile);
+ // NB! transient presets must be applied before normal
+ if (transient_presets()) {
+ r = unit_file_preset_all(UNIT_FILE_SYSTEM, true, NULL, UNIT_FILE_PRESET_ENABLE_ONLY, false, NULL, 0);
+ if (r < 0)
+ log_warning_errno(r, "Failed to populate transient preset unit settings, ignoring: %m");
+ else
+ log_info("Populated transient preset unit settings.");
+ }
+
+ // NB! normal presets must be applied after transient
if (empty_etc) {
r = unit_file_preset_all(UNIT_FILE_SYSTEM, false, NULL, UNIT_FILE_PRESET_FULL, false, NULL, 0);
if (r < 0)
diff --git a/src/core/unit.c b/src/core/unit.c
index ee8e607..dcafde0 100644
--- a/src/core/unit.c
+++ b/src/core/unit.c
@@ -3123,7 +3123,7 @@ int unit_get_unit_file_preset(Unit *u) {
if (u->unit_file_preset < 0 && u->fragment_path)
u->unit_file_preset = unit_file_query_preset(
u->manager->running_as == SYSTEMD_SYSTEM ? UNIT_FILE_SYSTEM : UNIT_FILE_USER,
- NULL, basename(u->fragment_path));
+ false, NULL, basename(u->fragment_path));
return u->unit_file_preset;
}
diff --git a/src/shared/install.c b/src/shared/install.c
index 65f1c24..7372e56 100644
--- a/src/shared/install.c
+++ b/src/shared/install.c
@@ -1873,7 +1873,7 @@ UnitFileState unit_file_get_state(
return r < 0 ? r : state;
}
-int unit_file_query_preset(UnitFileScope scope, const char *root_dir, const char *name) {
+int unit_file_query_preset(UnitFileScope scope, bool runtime, const char *root_dir, const char *name) {
_cleanup_strv_free_ char **files = NULL;
char **p;
int r;
@@ -1882,7 +1882,7 @@ int unit_file_query_preset(UnitFileScope scope, const char *root_dir, const char
assert(scope < _UNIT_FILE_SCOPE_MAX);
assert(name);
- if (scope == UNIT_FILE_SYSTEM)
+ if (scope == UNIT_FILE_SYSTEM && !runtime)
r = conf_files_list(&files, ".preset", root_dir,
"/etc/systemd/system-preset",
"/usr/local/lib/systemd/system-preset",
@@ -1891,12 +1891,27 @@ int unit_file_query_preset(UnitFileScope scope, const char *root_dir, const char
"/lib/systemd/system-preset",
#endif
NULL);
- else if (scope == UNIT_FILE_GLOBAL)
+ else if (scope == UNIT_FILE_GLOBAL && !runtime)
r = conf_files_list(&files, ".preset", root_dir,
"/etc/systemd/user-preset",
"/usr/local/lib/systemd/user-preset",
"/usr/lib/systemd/user-preset",
NULL);
+ else if (scope == UNIT_FILE_SYSTEM && runtime)
+ r = conf_files_list(&files, ".preset", root_dir,
+ "/etc/systemd/system-preset-transient",
+ "/usr/local/lib/systemd/system-preset-transient",
+ "/usr/lib/systemd/system-preset-transient",
+#ifdef HAVE_SPLIT_USR
+ "/lib/systemd/system-preset-transient",
+#endif
+ NULL);
+ else if (scope == UNIT_FILE_GLOBAL && runtime)
+ r = conf_files_list(&files, ".preset", root_dir,
+ "/etc/systemd/user-preset-transient",
+ "/usr/local/lib/systemd/user-preset-transient",
+ "/usr/lib/systemd/user-preset-transient",
+ NULL);
else
return 1;
@@ -1988,7 +2003,7 @@ int unit_file_preset(
if (!unit_name_is_valid(*i, TEMPLATE_VALID))
return -EINVAL;
- r = unit_file_query_preset(scope, root_dir, *i);
+ r = unit_file_query_preset(scope, runtime, root_dir, *i);
if (r < 0)
return r;
@@ -2089,7 +2104,7 @@ int unit_file_preset_all(
if (de->d_type != DT_REG)
continue;
- r = unit_file_query_preset(scope, root_dir, de->d_name);
+ r = unit_file_query_preset(scope, runtime, root_dir, de->d_name);
if (r < 0)
return r;
diff --git a/src/shared/install.h b/src/shared/install.h
index 357be0f..f621a3d 100644
--- a/src/shared/install.h
+++ b/src/shared/install.h
@@ -105,7 +105,7 @@ int unit_file_get_list(UnitFileScope scope, const char *root_dir, Hashmap *h);
void unit_file_list_free(Hashmap *h);
void unit_file_changes_free(UnitFileChange *changes, unsigned n_changes);
-int unit_file_query_preset(UnitFileScope scope, const char *root_dir, const char *name);
+int unit_file_query_preset(UnitFileScope scope, bool runtime, const char *root_dir, const char *name);
const char *unit_file_state_to_string(UnitFileState s) _const_;
UnitFileState unit_file_state_from_string(const char *s) _pure_;
--
2.1.0
More information about the systemd-devel
mailing list