dcavalca / rpms / systemd

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