richardphibel / rpms / systemd

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