[systemd-devel] [PATCH] Add support for transient presets, applied on every boot.

Dimitri John Ledkov dimitri.j.ledkov at intel.com
Thu Feb 5 08:11:11 PST 2015


---
 man/systemd-system.conf.xml |  1 +
 src/core/main.c             | 30 +++++++++++++++++++++++
 src/core/system.conf        |  1 +
 src/core/unit.c             |  2 +-
 src/shared/install.c        | 59 ++++++++++++++++++++++++++++++---------------
 src/shared/install.h        |  2 +-
 src/shared/path-lookup.c    |  2 ++
 7 files changed, 76 insertions(+), 21 deletions(-)

diff --git a/man/systemd-system.conf.xml b/man/systemd-system.conf.xml
index 7137fdb..fe2d484 100644
--- a/man/systemd-system.conf.xml
+++ b/man/systemd-system.conf.xml
@@ -94,6 +94,7 @@
         <term><varname>DumpCore=yes</varname></term>
         <term><varname>CrashShell=no</varname></term>
         <term><varname>ShowStatus=yes</varname></term>
+        <term><varname>TransientPreset=no</varname></term>
         <term><varname>CrashChVT=1</varname></term>
         <term><varname>DefaultStandardOutput=journal</varname></term>
         <term><varname>DefaultStandardError=inherit</varname></term>
diff --git a/src/core/main.c b/src/core/main.c
index 0749f04..d3328f5 100644
--- a/src/core/main.c
+++ b/src/core/main.c
@@ -93,6 +93,7 @@ static bool arg_crash_shell = false;
 static int arg_crash_chvt = -1;
 static bool arg_confirm_spawn = false;
 static ShowStatus arg_show_status = _SHOW_STATUS_UNSET;
+static bool arg_transient_preset = false;
 static bool arg_switched_root = false;
 static int arg_no_pager = -1;
 static char ***arg_join_controllers = NULL;
@@ -336,6 +337,14 @@ static int parse_proc_cmdline_item(const char *key, const char *value) {
                 if (r < 0)
                         log_warning("Failed to parse show status switch %s. Ignoring.", value);
 
+        } else if (streq(key, "systemd.transient_preset") && value) {
+
+                r = parse_boolean(value);
+                if (r < 0)
+                        log_warning("Failed to parse transient_preset switch %s. Ignoring.", value);
+                else
+                        arg_transient_preset = r;
+
         } else if (streq(key, "systemd.default_standard_output") && value) {
 
                 r = exec_output_from_string(value);
@@ -635,6 +644,7 @@ static int parse_config_file(void) {
                 { "Manager", "DumpCore",                  config_parse_bool,             0, &arg_dump_core                         },
                 { "Manager", "CrashShell",                config_parse_bool,             0, &arg_crash_shell                       },
                 { "Manager", "ShowStatus",                config_parse_show_status,      0, &arg_show_status                       },
+                { "Manager", "TransientPreset",           config_parse_bool,             0, &arg_transient_preset                  },
                 { "Manager", "CrashChVT",                 config_parse_int,              0, &arg_crash_chvt                        },
                 { "Manager", "CPUAffinity",               config_parse_cpu_affinity2,    0, NULL                                   },
                 { "Manager", "JoinControllers",           config_parse_join_controllers, 0, &arg_join_controllers                  },
@@ -704,6 +714,7 @@ static int parse_argv(int argc, char *argv[]) {
                 ARG_CRASH_SHELL,
                 ARG_CONFIRM_SPAWN,
                 ARG_SHOW_STATUS,
+                ARG_TRANSIENT_PRESET,
                 ARG_DESERIALIZE,
                 ARG_SWITCHED_ROOT,
                 ARG_DEFAULT_STD_OUTPUT,
@@ -727,6 +738,7 @@ static int parse_argv(int argc, char *argv[]) {
                 { "crash-shell",              optional_argument, NULL, ARG_CRASH_SHELL              },
                 { "confirm-spawn",            optional_argument, NULL, ARG_CONFIRM_SPAWN            },
                 { "show-status",              optional_argument, NULL, ARG_SHOW_STATUS              },
+                { "transient-preset",         optional_argument, NULL, ARG_TRANSIENT_PRESET         },
                 { "deserialize",              required_argument, NULL, ARG_DESERIALIZE              },
                 { "switched-root",            no_argument,       NULL, ARG_SWITCHED_ROOT            },
                 { "default-standard-output",  required_argument, NULL, ARG_DEFAULT_STD_OUTPUT,      },
@@ -879,6 +891,15 @@ static int parse_argv(int argc, char *argv[]) {
                                 arg_show_status = SHOW_STATUS_YES;
                         break;
 
+                case ARG_TRANSIENT_PRESET:
+                        r = optarg ? parse_boolean(optarg) : 1;
+                        if (r < 0) {
+                                log_error("Failed to parse transient preset boolean %s.", optarg);
+                                return r;
+                        }
+                        arg_transient_preset = r;
+                        break;
+
                 case ARG_DESERIALIZE: {
                         int fd;
                         FILE *f;
@@ -961,6 +982,7 @@ static int help(void) {
                "     --crash-shell[=0|1]         Run shell on crash\n"
                "     --confirm-spawn[=0|1]       Ask for confirmation when spawning processes\n"
                "     --show-status[=0|1]         Show status updates on the console during bootup\n"
+               "     --transient-preset[=0|1]    Apply transient preset files\n"
                "     --log-target=TARGET         Set log target (console, journal, kmsg, journal-or-kmsg, null)\n"
                "     --log-level=LEVEL           Set log level (debug, info, notice, warning, err, crit, alert, emerg)\n"
                "     --log-color[=0|1]           Highlight important log messages\n"
@@ -1626,6 +1648,14 @@ int main(int argc, char *argv[]) {
                         else
                                 log_info("Populated /etc with preset unit settings.");
                 }
+
+                if (arg_transient_preset) {
+                        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.");
+                }
         }
 
         r = manager_new(arg_running_as, arg_action == ACTION_TEST, &m);
diff --git a/src/core/system.conf b/src/core/system.conf
index a372720..f1612c8 100644
--- a/src/core/system.conf
+++ b/src/core/system.conf
@@ -18,6 +18,7 @@
 #DumpCore=yes
 #CrashShell=no
 #ShowStatus=yes
+#TransientPreset=no
 #CrashChVT=1
 #CPUAffinity=1 2
 #JoinControllers=cpu,cpuacct net_cls,net_prio
diff --git a/src/core/unit.c b/src/core/unit.c
index 2f95737..ed775c8 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..62b98fc 100644
--- a/src/shared/install.c
+++ b/src/shared/install.c
@@ -58,7 +58,7 @@ static int in_search_path(const char *path, char **search) {
         return strv_contains(search, parent);
 }
 
-static int get_config_path(UnitFileScope scope, bool runtime, const char *root_dir, char **ret) {
+static int get_config_path(UnitFileScope scope, bool runtime, bool preset, const char *root_dir, char **ret) {
         char *p = NULL;
         int r;
 
@@ -71,7 +71,10 @@ static int get_config_path(UnitFileScope scope, bool runtime, const char *root_d
         case UNIT_FILE_SYSTEM:
 
                 if (runtime)
-                        p = path_join(root_dir, "/run/systemd/system", NULL);
+                        if (preset)
+                                p = path_join(root_dir, "/run/systemd/system-preset-transient", NULL);
+                        else
+                                p = path_join(root_dir, "/run/systemd/system", NULL);
                 else
                         p = path_join(root_dir, SYSTEM_CONFIG_UNIT_PATH, NULL);
                 break;
@@ -82,7 +85,10 @@ static int get_config_path(UnitFileScope scope, bool runtime, const char *root_d
                         return -EINVAL;
 
                 if (runtime)
-                        p = strdup("/run/systemd/user");
+                        if (preset)
+                                p = path_join(root_dir, "/run/systemd/user-preset-transient", NULL);
+                        else
+                                p = path_join(root_dir, "/run/systemd/user", NULL);
                 else
                         p = strdup(USER_CONFIG_UNIT_PATH);
                 break;
@@ -523,7 +529,7 @@ static int find_symlinks_in_scope(
         assert(name);
 
         /* First look in runtime config path */
-        r = get_config_path(scope, true, root_dir, &path);
+        r = get_config_path(scope, true, false, root_dir, &path);
         if (r < 0)
                 return r;
 
@@ -536,7 +542,7 @@ static int find_symlinks_in_scope(
         }
 
         /* Then look in the normal config path */
-        r = get_config_path(scope, false, root_dir, &path);
+        r = get_config_path(scope, false, false, root_dir, &path);
         if (r < 0)
                 return r;
 
@@ -577,7 +583,7 @@ int unit_file_mask(
         assert(scope >= 0);
         assert(scope < _UNIT_FILE_SCOPE_MAX);
 
-        r = get_config_path(scope, runtime, root_dir, &prefix);
+        r = get_config_path(scope, runtime, false, root_dir, &prefix);
         if (r < 0)
                 return r;
 
@@ -640,7 +646,7 @@ int unit_file_unmask(
         assert(scope >= 0);
         assert(scope < _UNIT_FILE_SCOPE_MAX);
 
-        r = get_config_path(scope, runtime, root_dir, &config_path);
+        r = get_config_path(scope, runtime, false, root_dir, &config_path);
         if (r < 0)
                 goto finish;
 
@@ -706,7 +712,7 @@ int unit_file_link(
         if (r < 0)
                 return r;
 
-        r = get_config_path(scope, runtime, root_dir, &config_path);
+        r = get_config_path(scope, runtime, false, root_dir, &config_path);
         if (r < 0)
                 return r;
 
@@ -1523,7 +1529,7 @@ int unit_file_add_dependency(
         if (r < 0)
                 return r;
 
-        r = get_config_path(scope, runtime, root_dir, &config_path);
+        r = get_config_path(scope, runtime, false, root_dir, &config_path);
         if (r < 0)
                 return r;
 
@@ -1601,7 +1607,7 @@ int unit_file_enable(
         if (r < 0)
                 return r;
 
-        r = get_config_path(scope, runtime, root_dir, &config_path);
+        r = get_config_path(scope, runtime, false, root_dir, &config_path);
         if (r < 0)
                 return r;
 
@@ -1652,7 +1658,7 @@ int unit_file_disable(
         if (r < 0)
                 return r;
 
-        r = get_config_path(scope, runtime, root_dir, &config_path);
+        r = get_config_path(scope, runtime, false, root_dir, &config_path);
         if (r < 0)
                 return r;
 
@@ -1716,7 +1722,7 @@ int unit_file_set_default(
         if (r < 0)
                 return r;
 
-        r = get_config_path(scope, false, root_dir, &config_path);
+        r = get_config_path(scope, false, false, root_dir, &config_path);
         if (r < 0)
                 return r;
 
@@ -1873,7 +1879,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 +1888,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 +1897,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;
 
@@ -1979,7 +2000,7 @@ int unit_file_preset(
         if (r < 0)
                 return r;
 
-        r = get_config_path(scope, runtime, root_dir, &config_path);
+        r = get_config_path(scope, runtime, true, root_dir, &config_path);
         if (r < 0)
                 return r;
 
@@ -1988,7 +2009,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;
 
@@ -2047,7 +2068,7 @@ int unit_file_preset_all(
         if (r < 0)
                 return r;
 
-        r = get_config_path(scope, runtime, root_dir, &config_path);
+        r = get_config_path(scope, runtime, true, root_dir, &config_path);
         if (r < 0)
                 return r;
 
@@ -2089,7 +2110,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_;
diff --git a/src/shared/path-lookup.c b/src/shared/path-lookup.c
index 291a2f4..9472dbf 100644
--- a/src/shared/path-lookup.c
+++ b/src/shared/path-lookup.c
@@ -294,6 +294,7 @@ int lookup_paths_init(
                                         STRV_IFNOTNULL(generator),
                                         "/usr/local/lib/systemd/user",
                                         "/usr/local/share/systemd/user",
+                                        "/run/systemd/user-preset-transient",
                                         USER_DATA_UNIT_PATH,
                                         "/usr/lib/systemd/user",
                                         "/usr/share/systemd/user",
@@ -309,6 +310,7 @@ int lookup_paths_init(
                                 "/run/systemd/system",
                                 STRV_IFNOTNULL(generator),
                                 "/usr/local/lib/systemd/system",
+                                "/run/systemd/system-preset-transient",
                                 SYSTEM_DATA_UNIT_PATH,
                                 "/usr/lib/systemd/system",
 #ifdef HAVE_SPLIT_USR
-- 
2.1.0



More information about the systemd-devel mailing list