daandemeyer / rpms / systemd

Forked from rpms/systemd 2 years ago
Clone
803fb7
From 88d2ec272d3e503412e477d9abaebfe2ca199e78 Mon Sep 17 00:00:00 2001
803fb7
From: Filipe Brandenburger <filbranden@google.com>
803fb7
Date: Sun, 6 Sep 2015 23:06:53 -0700
803fb7
Subject: [PATCH] execute: Add new PassEnvironment= directive
803fb7
803fb7
This directive allows passing environment variables from the system
803fb7
manager to spawned services. Variables in the system manager can be set
803fb7
inside a container by passing `--set-env=...` options to systemd-spawn.
803fb7
803fb7
Tested with an on-disk test.service unit. Tested using multiple variable
803fb7
names on a single line, with an empty setting to clear the current list
803fb7
of variables, with non-existing variables.
803fb7
803fb7
Tested using `systemd-run -p PassEnvironment=VARNAME` to confirm it
803fb7
works with transient units.
803fb7
803fb7
Confirmed that `systemctl show` will display the PassEnvironment
803fb7
settings.
803fb7
803fb7
Checked that man pages are generated correctly.
803fb7
803fb7
No regressions in `make check`.
803fb7
803fb7
(cherry picked from commit b4c14404b3e8753c41bac0b1d49369230a15c544)
803fb7
803fb7
Resolves: #1426214
803fb7
---
803fb7
 man/systemd.exec.xml                  | 27 +++++++++++++++
803fb7
 shell-completion/bash/systemd-run     |  2 +-
803fb7
 src/core/dbus-execute.c               | 34 ++++++++++++++++++
803fb7
 src/core/execute.c                    | 44 ++++++++++++++++++++++--
803fb7
 src/core/execute.h                    |  1 +
803fb7
 src/core/load-fragment-gperf.gperf.m4 |  1 +
803fb7
 src/core/load-fragment.c              | 65 +++++++++++++++++++++++++++++++++++
803fb7
 src/core/load-fragment.h              |  1 +
803fb7
 src/libsystemd/sd-bus/bus-util.c      |  2 +-
803fb7
 src/shared/env-util.c                 | 15 ++++++++
803fb7
 src/shared/env-util.h                 |  1 +
803fb7
 11 files changed, 189 insertions(+), 4 deletions(-)
803fb7
803fb7
diff --git a/man/systemd.exec.xml b/man/systemd.exec.xml
803fb7
index c5199d3a5..aa5831cc2 100644
803fb7
--- a/man/systemd.exec.xml
803fb7
+++ b/man/systemd.exec.xml
803fb7
@@ -293,6 +293,33 @@
803fb7
         earlier setting.</para></listitem>
803fb7
       </varlistentry>
803fb7
 
803fb7
+      <varlistentry>
803fb7
+        <term><varname>PassEnvironment=</varname></term>
803fb7
+
803fb7
+        <listitem><para>Pass environment variables from the systemd system
803fb7
+        manager to executed processes. Takes a space-separated list of variable
803fb7
+        names. This option may be specified more than once, in which case all
803fb7
+        listed variables will be set. If the empty string is assigned to this
803fb7
+        option, the list of environment variables is reset, all prior
803fb7
+        assignments have no effect. Variables that are not set in the system
803fb7
+        manager will not be passed and will be silently ignored.</para>
803fb7
+
803fb7
+        <para>Variables passed from this setting are overridden by those passed
803fb7
+        from <varname>Environment=</varname> or
803fb7
+        <varname>EnvironmentFile=</varname>.</para>
803fb7
+
803fb7
+        <para>Example:
803fb7
+        <programlisting>PassEnvironment=VAR1 VAR2 VAR3</programlisting>
803fb7
+        passes three variables <literal>VAR1</literal>,
803fb7
+        <literal>VAR2</literal>, <literal>VAR3</literal>
803fb7
+        with the values set for those variables in PID1.</para>
803fb7
+
803fb7
+        <para>
803fb7
+        See
803fb7
+        <citerefentry project='man-pages'><refentrytitle>environ</refentrytitle><manvolnum>7</manvolnum></citerefentry>
803fb7
+        for details about environment variables.</para></listitem>
803fb7
+      </varlistentry>
803fb7
+
803fb7
       <varlistentry>
803fb7
         <term><varname>StandardInput=</varname></term>
803fb7
         <listitem><para>Controls where file descriptor 0 (STDIN) of
803fb7
diff --git a/shell-completion/bash/systemd-run b/shell-completion/bash/systemd-run
803fb7
index 5145cd3f2..36ffa46db 100644
803fb7
--- a/shell-completion/bash/systemd-run
803fb7
+++ b/shell-completion/bash/systemd-run
803fb7
@@ -73,7 +73,7 @@ _systemd_run() {
803fb7
                          KillSignal= LimitCPU= LimitFSIZE= LimitDATA= LimitSTACK=
803fb7
                          LimitCORE= LimitRSS= LimitNOFILE= LimitAS= LimitNPROC=
803fb7
                          LimitMEMLOCK= LimitLOCKS= LimitSIGPENDING= LimitMSGQUEUE=
803fb7
-                         LimitNICE= LimitRTPRIO= LimitRTTIME='
803fb7
+                         LimitNICE= LimitRTPRIO= LimitRTTIME= PassEnvironment='
803fb7
 
803fb7
             COMPREPLY=( $(compgen -W '$comps' -- "$cur") )
803fb7
             return 0
803fb7
diff --git a/src/core/dbus-execute.c b/src/core/dbus-execute.c
803fb7
index a9f7971cd..da8b10d2b 100644
803fb7
--- a/src/core/dbus-execute.c
803fb7
+++ b/src/core/dbus-execute.c
803fb7
@@ -597,6 +597,7 @@ const sd_bus_vtable bus_exec_vtable[] = {
803fb7
         SD_BUS_VTABLE_START(0),
803fb7
         SD_BUS_PROPERTY("Environment", "as", NULL, offsetof(ExecContext, environment), SD_BUS_VTABLE_PROPERTY_CONST),
803fb7
         SD_BUS_PROPERTY("EnvironmentFiles", "a(sb)", property_get_environment_files, 0, SD_BUS_VTABLE_PROPERTY_CONST),
803fb7
+        SD_BUS_PROPERTY("PassEnvironment", "as", NULL, offsetof(ExecContext, pass_environment), SD_BUS_VTABLE_PROPERTY_CONST),
803fb7
         SD_BUS_PROPERTY("UMask", "u", bus_property_get_mode, offsetof(ExecContext, umask), SD_BUS_VTABLE_PROPERTY_CONST),
803fb7
         SD_BUS_PROPERTY("LimitCPU", "t", property_get_rlimit, offsetof(ExecContext, rlimit[RLIMIT_CPU]), SD_BUS_VTABLE_PROPERTY_CONST),
803fb7
         SD_BUS_PROPERTY("LimitFSIZE", "t", property_get_rlimit, offsetof(ExecContext, rlimit[RLIMIT_FSIZE]), SD_BUS_VTABLE_PROPERTY_CONST),
803fb7
@@ -963,6 +964,39 @@ int bus_exec_context_set_transient_property(
803fb7
 
803fb7
                 return 1;
803fb7
 
803fb7
+        } else if (streq(name, "PassEnvironment")) {
803fb7
+
803fb7
+                _cleanup_strv_free_ char **l = NULL;
803fb7
+
803fb7
+                r = sd_bus_message_read_strv(message, &l);
803fb7
+                if (r < 0)
803fb7
+                        return r;
803fb7
+
803fb7
+                if (!strv_env_name_is_valid(l))
803fb7
+                        return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid PassEnvironment block.");
803fb7
+
803fb7
+                if (mode != UNIT_CHECK) {
803fb7
+                        if (strv_isempty(l)) {
803fb7
+                                strv_free(c->pass_environment);
803fb7
+                                c->pass_environment = NULL;
803fb7
+                                unit_write_drop_in_private_format(u, mode, name, "PassEnvironment=\n");
803fb7
+                        } else {
803fb7
+                                _cleanup_free_ char *joined = NULL;
803fb7
+
803fb7
+                                r = strv_extend_strv(&c->pass_environment, l);
803fb7
+                                if (r < 0)
803fb7
+                                        return r;
803fb7
+
803fb7
+                                joined = strv_join_quoted(c->pass_environment);
803fb7
+                                if (!joined)
803fb7
+                                        return -ENOMEM;
803fb7
+
803fb7
+                                unit_write_drop_in_private_format(u, mode, name, "PassEnvironment=%s\n", joined);
803fb7
+                        }
803fb7
+                }
803fb7
+
803fb7
+                return 1;
803fb7
+
803fb7
         } else if (rlimit_from_string(name) >= 0) {
803fb7
                 uint64_t rl;
803fb7
                 rlim_t x;
803fb7
diff --git a/src/core/execute.c b/src/core/execute.c
803fb7
index 863babd76..f72b20966 100644
803fb7
--- a/src/core/execute.c
803fb7
+++ b/src/core/execute.c
803fb7
@@ -1256,6 +1256,34 @@ static int build_environment(
803fb7
         return 0;
803fb7
 }
803fb7
 
803fb7
+static int build_pass_environment(const ExecContext *c, char ***ret) {
803fb7
+        _cleanup_strv_free_ char **pass_env = NULL;
803fb7
+        size_t n_env = 0, n_bufsize = 0;
803fb7
+        char **i;
803fb7
+
803fb7
+        STRV_FOREACH(i, c->pass_environment) {
803fb7
+                _cleanup_free_ char *x = NULL;
803fb7
+                char *v;
803fb7
+
803fb7
+                v = getenv(*i);
803fb7
+                if (!v)
803fb7
+                        continue;
803fb7
+                x = strjoin(*i, "=", v, NULL);
803fb7
+                if (!x)
803fb7
+                        return -ENOMEM;
803fb7
+                if (!GREEDY_REALLOC(pass_env, n_bufsize, n_env + 2))
803fb7
+                        return -ENOMEM;
803fb7
+                pass_env[n_env++] = x;
803fb7
+                pass_env[n_env] = NULL;
803fb7
+                x = NULL;
803fb7
+        }
803fb7
+
803fb7
+        *ret = pass_env;
803fb7
+        pass_env = NULL;
803fb7
+
803fb7
+        return 0;
803fb7
+}
803fb7
+
803fb7
 static bool exec_needs_mount_namespace(
803fb7
                 const ExecContext *context,
803fb7
                 const ExecParameters *params,
803fb7
@@ -1297,7 +1325,7 @@ static int exec_child(
803fb7
                 char **files_env,
803fb7
                 int *exit_status) {
803fb7
 
803fb7
-        _cleanup_strv_free_ char **our_env = NULL, **pam_env = NULL, **final_env = NULL, **final_argv = NULL;
803fb7
+        _cleanup_strv_free_ char **our_env = NULL, **pass_env = NULL, **pam_env = NULL, **final_env = NULL, **final_argv = NULL;
803fb7
         _cleanup_free_ char *mac_selinux_context_net = NULL;
803fb7
         const char *username = NULL, *home = NULL, *shell = NULL;
803fb7
         unsigned n_dont_close = 0;
803fb7
@@ -1805,9 +1833,16 @@ static int exec_child(
803fb7
                 return r;
803fb7
         }
803fb7
 
803fb7
-        final_env = strv_env_merge(5,
803fb7
+        r = build_pass_environment(context, &pass_env);
803fb7
+        if (r < 0) {
803fb7
+                *exit_status = EXIT_MEMORY;
803fb7
+                return r;
803fb7
+        }
803fb7
+
803fb7
+        final_env = strv_env_merge(6,
803fb7
                                    params->environment,
803fb7
                                    our_env,
803fb7
+                                   pass_env,
803fb7
                                    context->environment,
803fb7
                                    files_env,
803fb7
                                    pam_env,
803fb7
@@ -1965,6 +2000,8 @@ void exec_context_done(ExecContext *c) {
803fb7
 
803fb7
         strv_free(c->environment_files);
803fb7
         c->environment_files = NULL;
803fb7
+        strv_free(c->pass_environment);
803fb7
+        c->pass_environment = NULL;
803fb7
 
803fb7
         for (l = 0; l < ELEMENTSOF(c->rlimit); l++) {
803fb7
                 free(c->rlimit[l]);
803fb7
@@ -2267,6 +2304,9 @@ void exec_context_dump(ExecContext *c, FILE* f, const char *prefix) {
803fb7
         STRV_FOREACH(e, c->environment_files)
803fb7
                 fprintf(f, "%sEnvironmentFile: %s\n", prefix, *e);
803fb7
 
803fb7
+        STRV_FOREACH(e, c->pass_environment)
803fb7
+                fprintf(f, "%sPassEnvironment: %s\n", prefix, *e);
803fb7
+
803fb7
         if (c->nice_set)
803fb7
                 fprintf(f,
803fb7
                         "%sNice: %i\n",
803fb7
diff --git a/src/core/execute.h b/src/core/execute.h
803fb7
index 6e0c9faa7..cadd0e6b4 100644
803fb7
--- a/src/core/execute.h
803fb7
+++ b/src/core/execute.h
803fb7
@@ -96,6 +96,7 @@ struct ExecRuntime {
803fb7
 struct ExecContext {
803fb7
         char **environment;
803fb7
         char **environment_files;
803fb7
+        char **pass_environment;
803fb7
 
803fb7
         struct rlimit *rlimit[_RLIMIT_MAX];
803fb7
         char *working_directory, *root_directory;
803fb7
diff --git a/src/core/load-fragment-gperf.gperf.m4 b/src/core/load-fragment-gperf.gperf.m4
803fb7
index c866a9cd0..b50fe45b4 100644
803fb7
--- a/src/core/load-fragment-gperf.gperf.m4
803fb7
+++ b/src/core/load-fragment-gperf.gperf.m4
803fb7
@@ -33,6 +33,7 @@ $1.CPUAffinity,                  config_parse_exec_cpu_affinity,     0,
803fb7
 $1.UMask,                        config_parse_mode,                  0,                             offsetof($1, exec_context.umask)
803fb7
 $1.Environment,                  config_parse_environ,               0,                             offsetof($1, exec_context.environment)
803fb7
 $1.EnvironmentFile,              config_parse_unit_env_file,         0,                             offsetof($1, exec_context.environment_files)
803fb7
+$1.PassEnvironment,              config_parse_pass_environ,          0,                             offsetof($1, exec_context.pass_environment)
803fb7
 $1.StandardInput,                config_parse_input,                 0,                             offsetof($1, exec_context.std_input)
803fb7
 $1.StandardOutput,               config_parse_output,                0,                             offsetof($1, exec_context.std_output)
803fb7
 $1.StandardError,                config_parse_output,                0,                             offsetof($1, exec_context.std_error)
803fb7
diff --git a/src/core/load-fragment.c b/src/core/load-fragment.c
803fb7
index 3a3c456da..c450fe2c7 100644
803fb7
--- a/src/core/load-fragment.c
803fb7
+++ b/src/core/load-fragment.c
803fb7
@@ -2265,6 +2265,71 @@ int config_parse_environ(const char *unit,
803fb7
         return 0;
803fb7
 }
803fb7
 
803fb7
+int config_parse_pass_environ(const char *unit,
803fb7
+                              const char *filename,
803fb7
+                              unsigned line,
803fb7
+                              const char *section,
803fb7
+                              unsigned section_line,
803fb7
+                              const char *lvalue,
803fb7
+                              int ltype,
803fb7
+                              const char *rvalue,
803fb7
+                              void *data,
803fb7
+                              void *userdata) {
803fb7
+
803fb7
+        const char *whole_rvalue = rvalue;
803fb7
+        char*** passenv = data;
803fb7
+        _cleanup_strv_free_ char **n = NULL;
803fb7
+        size_t nlen = 0, nbufsize = 0;
803fb7
+        int r;
803fb7
+
803fb7
+        assert(filename);
803fb7
+        assert(lvalue);
803fb7
+        assert(rvalue);
803fb7
+        assert(data);
803fb7
+
803fb7
+        if (isempty(rvalue)) {
803fb7
+                /* Empty assignment resets the list */
803fb7
+                strv_free(*passenv);
803fb7
+                *passenv = NULL;
803fb7
+                return 0;
803fb7
+        }
803fb7
+
803fb7
+        for (;;) {
803fb7
+                _cleanup_free_ char *word = NULL;
803fb7
+
803fb7
+                r = extract_first_word(&rvalue, &word, WHITESPACE, EXTRACT_QUOTES);
803fb7
+                if (r == 0)
803fb7
+                        break;
803fb7
+                if (r == -ENOMEM)
803fb7
+                        return log_oom();
803fb7
+                if (r < 0) {
803fb7
+                        log_syntax(unit, LOG_ERR, filename, line, r,
803fb7
+                                   "Trailing garbage in %s, ignoring: %s", lvalue, whole_rvalue);
803fb7
+                        break;
803fb7
+                }
803fb7
+
803fb7
+                if (!env_name_is_valid(word)) {
803fb7
+                        log_syntax(unit, LOG_ERR, filename, line, EINVAL,
803fb7
+                                   "Invalid environment name for %s, ignoring: %s", lvalue, word);
803fb7
+                        continue;
803fb7
+                }
803fb7
+
803fb7
+                if (!GREEDY_REALLOC(n, nbufsize, nlen + 2))
803fb7
+                        return log_oom();
803fb7
+                n[nlen++] = word;
803fb7
+                n[nlen] = NULL;
803fb7
+                word = NULL;
803fb7
+        }
803fb7
+
803fb7
+        if (n) {
803fb7
+                r = strv_extend_strv(passenv, n);
803fb7
+                if (r < 0)
803fb7
+                        return r;
803fb7
+        }
803fb7
+
803fb7
+        return 0;
803fb7
+}
803fb7
+
803fb7
 int config_parse_ip_tos(const char *unit,
803fb7
                         const char *filename,
803fb7
                         unsigned line,
803fb7
diff --git a/src/core/load-fragment.h b/src/core/load-fragment.h
803fb7
index 7c69e5369..9dd7d1bda 100644
803fb7
--- a/src/core/load-fragment.h
803fb7
+++ b/src/core/load-fragment.h
803fb7
@@ -85,6 +85,7 @@ int config_parse_syscall_filter(const char *unit, const char *filename, unsigned
803fb7
 int config_parse_syscall_archs(const char *unit, const char *filename, unsigned line, const char *section, unsigned section_line, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata);
803fb7
 int config_parse_syscall_errno(const char *unit, const char *filename, unsigned line, const char *section, unsigned section_line, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata);
803fb7
 int config_parse_environ(const char *unit, const char *filename, unsigned line, const char *section, unsigned section_line, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata);
803fb7
+int config_parse_pass_environ(const char *unit, const char *filename, unsigned line, const char *section, unsigned section_line, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata);
803fb7
 int config_parse_unit_slice(const char *unit, const char *filename, unsigned line, const char *section, unsigned section_line, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata);
803fb7
 int config_parse_cpu_shares(const char *unit, const char *filename, unsigned line, const char *section, unsigned section_line, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata);
803fb7
 int config_parse_memory_limit(const char *unit, const char *filename, unsigned line, const char *section, unsigned section_line, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata);
803fb7
diff --git a/src/libsystemd/sd-bus/bus-util.c b/src/libsystemd/sd-bus/bus-util.c
803fb7
index d35776087..ed0849b63 100644
803fb7
--- a/src/libsystemd/sd-bus/bus-util.c
803fb7
+++ b/src/libsystemd/sd-bus/bus-util.c
803fb7
@@ -1535,7 +1535,7 @@ int bus_append_unit_property_assignment(sd_bus_message *m, const char *assignmen
803fb7
 
803fb7
                 r = sd_bus_message_append(m, "v", "i", i);
803fb7
 
803fb7
-        } else if (streq(field, "Environment")) {
803fb7
+        } else if (STR_IN_SET(field, "Environment", "PassEnvironment")) {
803fb7
 
803fb7
                 r = sd_bus_message_append(m, "v", "as", 1, eq);
803fb7
 
803fb7
diff --git a/src/shared/env-util.c b/src/shared/env-util.c
803fb7
index e8da4c978..581d84a20 100644
803fb7
--- a/src/shared/env-util.c
803fb7
+++ b/src/shared/env-util.c
803fb7
@@ -136,6 +136,21 @@ bool strv_env_is_valid(char **e) {
803fb7
         return true;
803fb7
 }
803fb7
 
803fb7
+bool strv_env_name_is_valid(char **l) {
803fb7
+        char **p, **q;
803fb7
+
803fb7
+        STRV_FOREACH(p, l) {
803fb7
+                if (!env_name_is_valid(*p))
803fb7
+                        return false;
803fb7
+
803fb7
+                STRV_FOREACH(q, p + 1)
803fb7
+                        if (streq(*p, *q))
803fb7
+                                return false;
803fb7
+        }
803fb7
+
803fb7
+        return true;
803fb7
+}
803fb7
+
803fb7
 bool strv_env_name_or_assignment_is_valid(char **l) {
803fb7
         char **p, **q;
803fb7
 
803fb7
diff --git a/src/shared/env-util.h b/src/shared/env-util.h
803fb7
index 252d87be1..b8c2d81e4 100644
803fb7
--- a/src/shared/env-util.h
803fb7
+++ b/src/shared/env-util.h
803fb7
@@ -34,6 +34,7 @@ bool strv_env_is_valid(char **e);
803fb7
 #define strv_env_clean(l) strv_env_clean_with_callback(l, NULL, NULL)
803fb7
 char **strv_env_clean_with_callback(char **l, void (*invalid_callback)(const char *p, void *userdata), void *userdata);
803fb7
 
803fb7
+bool strv_env_name_is_valid(char **l);
803fb7
 bool strv_env_name_or_assignment_is_valid(char **l);
803fb7
 
803fb7
 char **strv_env_merge(unsigned n_lists, ...);