From a4ccce22d9552dc74b6916cc5ec57f2a0b686b4f Mon Sep 17 00:00:00 2001 From: =?utf8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= Date: Mon, 28 Sep 2020 17:29:34 +0200 Subject: [PATCH] systemctl: ignore invalid variables in import-environment When doing import-environment, we shouldn't fail if some assignment is invalid. OTOH, if the invalid assignment is specified as a positional argument, we should keep failing. This would also fix https://bugzilla.redhat.com/show_bug.cgi?id=1754395, by ignoring certain variables which are not important in that scenario. It seems like the right thing to do in general. --- src/systemctl/systemctl-set-environment.c | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/src/systemctl/systemctl-set-environment.c b/src/systemctl/systemctl-set-environment.c index 462924f5c9a..ac1ec7d6fea 100644 --- a/src/systemctl/systemctl-set-environment.c +++ b/src/systemctl/systemctl-set-environment.c @@ -61,6 +61,12 @@ int show_environment(int argc, char *argv[], void *userdata) { return 0; } +static void invalid_callback(const char *p, void *userdata) { + _cleanup_free_ char *t = cescape(p); + + log_debug("Ignoring invalid environment assignment \"%s\".", strnull(t)); +} + int set_environment(int argc, char *argv[], void *userdata) { _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL; @@ -112,9 +118,18 @@ int import_environment(int argc, char *argv[], void *userdata) { if (r < 0) return bus_log_create_error(r); - if (argc < 2) - r = sd_bus_message_append_strv(m, environ); - else { + if (argc < 2) { + _cleanup_strv_free_ char **copy = NULL; + + copy = strv_copy(environ); + if (!copy) + return log_oom(); + + strv_env_clean_with_callback(copy, invalid_callback, NULL); + + r = sd_bus_message_append_strv(m, copy); + + } else { char **a, **b; r = sd_bus_message_open_container(m, 'a', "s"); -- 2.47.3