richardphibel / rpms / systemd

Forked from rpms/systemd 2 years ago
Clone
984f77
From f41927c026d65e9005c0ba418c6bfff511055bd2 Mon Sep 17 00:00:00 2001
984f77
From: Yu Watanabe <watanabe.yu+github@gmail.com>
984f77
Date: Tue, 11 Sep 2018 14:05:08 +0900
984f77
Subject: [PATCH] core: add new environment variable $RUNTIME_DIRECTORY= or
984f77
 friends
984f77
984f77
The variable is generated from RuntimeDirectory= or friends.
984f77
If multiple directories are set, then they are concatenated with
984f77
the separator ':'.
984f77
984f77
(cherry picked from commit fb2042dd55de5019f55931b4f20a44700ec1222b)
984f77
984f77
Resolves: #2049788
984f77
---
984f77
 TODO               |  9 ---------
984f77
 src/core/execute.c | 46 ++++++++++++++++++++++++++++++++++++++++++++--
984f77
 2 files changed, 44 insertions(+), 11 deletions(-)
984f77
984f77
diff --git a/TODO b/TODO
984f77
index 0705b6b08e..c52f9b25f3 100644
984f77
--- a/TODO
984f77
+++ b/TODO
984f77
@@ -214,15 +214,6 @@ Features:
984f77
   for all units. It should be both a way to pin units into memory as well as a
984f77
   wait to retrieve their exit data.
984f77
 
984f77
-* maybe set a new set of env vars for services, based on RuntimeDirectory=,
984f77
-  StateDirectory=, LogsDirectory=, CacheDirectory= and ConfigurationDirectory=
984f77
-  automatically. For example, there could be $RUNTIME_DIRECTORY,
984f77
-  $STATE_DIRECTORY, $LOGS_DIRECTORY=, $CACHE_DIRECTORY and
984f77
-  $CONFIGURATION_DIRECTORY or so. This could be useful to write services that
984f77
-  can adapt to varying directories for these purposes. Special care has to be
984f77
-  taken if multiple dirs are configured. Maybe avoid setting the env vars in
984f77
-  that case?
984f77
-
984f77
 * expose IO accounting data on the bus, show it in systemd-run --wait and log
984f77
   about it in the resource log message
984f77
 
984f77
diff --git a/src/core/execute.c b/src/core/execute.c
984f77
index 3ff1a51aa1..9cbb678ac4 100644
984f77
--- a/src/core/execute.c
984f77
+++ b/src/core/execute.c
984f77
@@ -1606,6 +1606,8 @@ static void do_idle_pipe_dance(int idle_pipe[4]) {
984f77
         idle_pipe[3] = safe_close(idle_pipe[3]);
984f77
 }
984f77
 
984f77
+static const char *exec_directory_env_name_to_string(ExecDirectoryType t);
984f77
+
984f77
 static int build_environment(
984f77
                 const Unit *u,
984f77
                 const ExecContext *c,
984f77
@@ -1619,6 +1621,7 @@ static int build_environment(
984f77
                 char ***ret) {
984f77
 
984f77
         _cleanup_strv_free_ char **our_env = NULL;
984f77
+        ExecDirectoryType t;
984f77
         size_t n_env = 0;
984f77
         char *x;
984f77
 
984f77
@@ -1627,7 +1630,7 @@ static int build_environment(
984f77
         assert(p);
984f77
         assert(ret);
984f77
 
984f77
-        our_env = new0(char*, 14);
984f77
+        our_env = new0(char*, 14 + _EXEC_DIRECTORY_TYPE_MAX);
984f77
         if (!our_env)
984f77
                 return -ENOMEM;
984f77
 
984f77
@@ -1733,8 +1736,37 @@ static int build_environment(
984f77
                 our_env[n_env++] = x;
984f77
         }
984f77
 
984f77
+        for (t = 0; t < _EXEC_DIRECTORY_TYPE_MAX; t++) {
984f77
+                _cleanup_free_ char *pre = NULL, *joined = NULL;
984f77
+                const char *n;
984f77
+
984f77
+                if (!p->prefix[t])
984f77
+                        continue;
984f77
+
984f77
+                if (strv_isempty(c->directories[t].paths))
984f77
+                        continue;
984f77
+
984f77
+                n = exec_directory_env_name_to_string(t);
984f77
+                if (!n)
984f77
+                        continue;
984f77
+
984f77
+                pre = strjoin(p->prefix[t], "/");
984f77
+                if (!pre)
984f77
+                        return -ENOMEM;
984f77
+
984f77
+                joined = strv_join_prefix(c->directories[t].paths, ":", pre);
984f77
+                if (!joined)
984f77
+                        return -ENOMEM;
984f77
+
984f77
+                x = strjoin(n, "=", joined);
984f77
+                if (!x)
984f77
+                        return -ENOMEM;
984f77
+
984f77
+                our_env[n_env++] = x;
984f77
+        }
984f77
+
984f77
         our_env[n_env++] = NULL;
984f77
-        assert(n_env <= 14);
984f77
+        assert(n_env <= 14 + _EXEC_DIRECTORY_TYPE_MAX);
984f77
 
984f77
         *ret = TAKE_PTR(our_env);
984f77
 
984f77
@@ -5197,6 +5229,16 @@ static const char* const exec_directory_type_table[_EXEC_DIRECTORY_TYPE_MAX] = {
984f77
 
984f77
 DEFINE_STRING_TABLE_LOOKUP(exec_directory_type, ExecDirectoryType);
984f77
 
984f77
+static const char* const exec_directory_env_name_table[_EXEC_DIRECTORY_TYPE_MAX] = {
984f77
+        [EXEC_DIRECTORY_RUNTIME] = "RUNTIME_DIRECTORY",
984f77
+        [EXEC_DIRECTORY_STATE] = "STATE_DIRECTORY",
984f77
+        [EXEC_DIRECTORY_CACHE] = "CACHE_DIRECTORY",
984f77
+        [EXEC_DIRECTORY_LOGS] = "LOGS_DIRECTORY",
984f77
+        [EXEC_DIRECTORY_CONFIGURATION] = "CONFIGURATION_DIRECTORY",
984f77
+};
984f77
+
984f77
+DEFINE_PRIVATE_STRING_TABLE_LOOKUP_TO_STRING(exec_directory_env_name, ExecDirectoryType);
984f77
+
984f77
 static const char* const exec_keyring_mode_table[_EXEC_KEYRING_MODE_MAX] = {
984f77
         [EXEC_KEYRING_INHERIT] = "inherit",
984f77
         [EXEC_KEYRING_PRIVATE] = "private",