]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
exec-util: implement a set of callbacks to pass variables around
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Sat, 11 Feb 2017 02:49:01 +0000 (21:49 -0500)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Mon, 20 Feb 2017 23:49:14 +0000 (18:49 -0500)
Only tests are added, otherwise the new code is unused.

src/basic/exec-util.c
src/basic/exec-util.h
src/test/test-exec-util.c

index d69039c489c923c5eb05d50043ab073c73bd4929..207fac8dc1369ca7393caab900595a26b2170d10 100644 (file)
@@ -251,3 +251,105 @@ int execute_directories(
                 return log_error_errno(r, "Failed to parse returned data: %m");
         return 0;
 }
+
+static int gather_environment_generate(int fd, void *arg) {
+        char ***env = arg, **x, **y;
+        _cleanup_fclose_ FILE *f = NULL;
+        _cleanup_strv_free_ char **new;
+        int r;
+
+        /* Read a series of VAR=value assignments from fd, use them to update the list of
+         * variables in env. Also update the exported environment.
+         *
+         * fd is always consumed, even on error.
+         */
+
+        assert(env);
+
+        f = fdopen(fd, "r");
+        if (!f) {
+                safe_close(fd);
+                return -errno;
+        }
+
+        r = load_env_file_pairs(f, NULL, NULL, &new);
+        if (r < 0)
+                return r;
+
+        STRV_FOREACH_PAIR(x, y, new) {
+                char *p;
+
+                p = strjoin(*x, "=", *y);
+                if (!p)
+                        return -ENOMEM;
+
+                r = strv_env_replace(env, p);
+                if (r < 0)
+                        return r;
+
+                if (setenv(*x, *y, true) < 0)
+                        return -errno;
+        }
+
+        return r;
+}
+
+static int gather_environment_collect(int fd, void *arg) {
+        char ***env = arg;
+        _cleanup_fclose_ FILE *f = NULL;
+        int r;
+
+        /* Write out a series of env=cescape(VAR=value) assignments to fd. */
+
+        assert(env);
+
+        f = fdopen(fd, "w");
+        if (!f) {
+                safe_close(fd);
+                return -errno;
+        }
+
+        r = serialize_environment(f, *env);
+        if (r < 0)
+                return r;
+
+        if (ferror(f))
+                return errno > 0 ? -errno : -EIO;
+
+        return 0;
+}
+
+static int gather_environment_consume(int fd, void *arg) {
+        char ***env = arg;
+        _cleanup_fclose_ FILE *f = NULL;
+        char line[LINE_MAX];
+        int r = 0, k;
+
+        /* Read a series of env=cescape(VAR=value) assignments from fd into env. */
+
+        assert(env);
+
+        f = fdopen(fd, "r");
+        if (!f) {
+                safe_close(fd);
+                return -errno;
+        }
+
+        FOREACH_LINE(line, f, return -EIO) {
+                truncate_nl(line);
+
+                k = deserialize_environment(env, line);
+                if (k < 0)
+                        log_error_errno(k, "Invalid line \"%s\": %m", line);
+                if (k < 0 && r == 0)
+                        r = k;
+        }
+
+        return r;
+}
+
+const gather_stdout_callback_t gather_environment[] = {
+        gather_environment_generate,
+        gather_environment_collect,
+        gather_environment_consume,
+};
index 2c58e4bd5c66a2f15fff98825acad1e7edc6b79c..72009799b2876cf2e1eb144eefd25c90847fcd38 100644 (file)
@@ -36,3 +36,5 @@ int execute_directories(
                 gather_stdout_callback_t const callbacks[_STDOUT_CONSUME_MAX],
                 void* const callback_args[_STDOUT_CONSUME_MAX],
                 char *argv[]);
+
+extern const gather_stdout_callback_t gather_environment[_STDOUT_CONSUME_MAX];
index 41cbef74b161e072f62c143a6d93f51498883c04..94fe042aa984f7a1263bff791d881502b2242c4a 100644 (file)
@@ -27,6 +27,7 @@
 #include "alloc-util.h"
 #include "copy.h"
 #include "def.h"
+#include "env-util.h"
 #include "exec-util.h"
 #include "fd-util.h"
 #include "fileio.h"
@@ -271,6 +272,59 @@ static void test_stdout_gathering(void) {
         assert_se(streq(output, "a\nb\nc\nd\n"));
 }
 
+static void test_environment_gathering(void) {
+        char template[] = "/tmp/test-exec-util.XXXXXXX", **p;
+        const char *dirs[] = {template, NULL};
+        const char *name, *name2, *name3;
+        int r;
+
+        char **tmp = NULL; /* this is only used in the forked process, no cleanup here */
+        _cleanup_strv_free_ char **env = NULL;
+
+        void* const args[] = { &tmp, &tmp, &env };
+
+        assert_se(mkdtemp(template));
+
+        log_info("/* %s */", __func__);
+
+        /* write files */
+        name = strjoina(template, "/10-foo");
+        name2 = strjoina(template, "/20-bar");
+        name3 = strjoina(template, "/30-last");
+
+        assert_se(write_string_file(name,
+                                    "#!/bin/sh\n"
+                                    "echo A=23\n",
+                                    WRITE_STRING_FILE_CREATE) == 0);
+        assert_se(write_string_file(name2,
+                                    "#!/bin/sh\n"
+                                    "echo A=22:$A\n\n\n",            /* substitution from previous generator */
+                                    WRITE_STRING_FILE_CREATE) == 0);
+        assert_se(write_string_file(name3,
+                                    "#!/bin/sh\n"
+                                    "echo A=$A:24\n"
+                                    "echo B=12\n"
+                                    "echo C=000\n"
+                                    "echo C=001\n"                   /* variable overwriting */
+                                    "echo PATH=$PATH:/no/such/file", /* variable from manager */
+                                    WRITE_STRING_FILE_CREATE) == 0);
+
+        assert_se(chmod(name, 0755) == 0);
+        assert_se(chmod(name2, 0755) == 0);
+        assert_se(chmod(name3, 0755) == 0);
+
+        r = execute_directories(dirs, DEFAULT_TIMEOUT_USEC, gather_environment, args, NULL);
+        assert_se(r >= 0);
+
+        STRV_FOREACH(p, env)
+                log_info("got env: \"%s\"", *p);
+
+        assert_se(streq(strv_env_get(env, "A"), "22:23:24"));
+        assert_se(streq(strv_env_get(env, "B"), "12"));
+        assert_se(streq(strv_env_get(env, "C"), "001"));
+        assert_se(endswith(strv_env_get(env, "PATH"), ":/no/such/file"));
+}
+
 int main(int argc, char *argv[]) {
         log_set_max_level(LOG_DEBUG);
         log_parse_environment();
@@ -280,6 +334,7 @@ int main(int argc, char *argv[]) {
         test_execute_directory(false);
         test_execution_order();
         test_stdout_gathering();
+        test_environment_gathering();
 
         return 0;
 }