diff options
author | Lennart Poettering <lennart@poettering.net> | 2014-01-15 15:21:29 +0800 |
---|---|---|
committer | Lennart Poettering <lennart@poettering.net> | 2014-01-15 20:16:20 +0100 |
commit | ac3efa8ac62b60261d6c101bc98831316523b07a (patch) | |
tree | e1d2968af629ab87b69ab93353e8a36c56b49d4c /src/systemctl/systemctl.c | |
parent | 6a38d04a036475bfc222f40961e2adcaa70a6174 (diff) |
systemctl: introduce new "import-environment" command
This may be used in graphical session start-up scripts to upload
environment variables such as $DISPLAY into the systemd manager easily.
Diffstat (limited to 'src/systemctl/systemctl.c')
-rw-r--r-- | src/systemctl/systemctl.c | 68 |
1 files changed, 67 insertions, 1 deletions
diff --git a/src/systemctl/systemctl.c b/src/systemctl/systemctl.c index dd95df14e7..b9d9b3aa96 100644 --- a/src/systemctl/systemctl.c +++ b/src/systemctl/systemctl.c @@ -68,6 +68,7 @@ #include "logs-show.h" #include "socket-util.h" #include "fileio.h" +#include "env-util.h" #include "bus-util.h" #include "bus-message.h" #include "bus-error.h" @@ -4467,6 +4468,69 @@ static int set_environment(sd_bus *bus, char **args) { return 0; } +static int import_environment(sd_bus *bus, char **args) { + _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL; + _cleanup_bus_message_unref_ sd_bus_message *m = NULL; + int r; + + assert(bus); + assert(args); + + r = sd_bus_message_new_method_call( + bus, + "org.freedesktop.systemd1", + "/org/freedesktop/systemd1", + "org.freedesktop.systemd1.Manager", + "SetEnvironment", + &m); + if (r < 0) + return bus_log_create_error(r); + + if (strv_isempty(args + 1)) + r = sd_bus_message_append_strv(m, environ); + else { + char **a, **b; + + r = sd_bus_message_open_container(m, 'a', "s"); + if (r < 0) + return bus_log_create_error(r); + + STRV_FOREACH(a, args + 1) { + + if (!env_name_is_valid(*a)) { + log_error("Not a valid environment variable name: %s", *a); + return -EINVAL; + } + + STRV_FOREACH(b, environ) { + const char *eq; + + eq = startswith(*b, *a); + if (eq && *eq == '=') { + + r = sd_bus_message_append(m, "s", *b); + if (r < 0) + return bus_log_create_error(r); + + break; + } + } + } + + r = sd_bus_message_close_container(m); + } + if (r < 0) + return bus_log_create_error(r); + + r = sd_bus_call(bus, m, 0, &error, NULL); + if (r < 0) { + log_error("Failed to import environment: %s", bus_error_message(&error, r)); + return r; + } + + return 0; +} + static int enable_sysv_units(const char *verb, char **args) { int r = 0; @@ -4971,7 +5035,8 @@ static int systemctl_help(void) { "Environment Commands:\n" " show-environment Dump environment\n" " set-environment NAME=VALUE... Set one or more environment variables\n" - " unset-environment NAME... Unset one or more environment variables\n\n" + " unset-environment NAME... Unset one or more environment variables\n" + " import-environment NAME... Import all, one or more environment variables\n\n" "Manager Lifecycle Commands:\n" " daemon-reload Reload systemd manager configuration\n" " daemon-reexec Reexecute systemd manager\n\n" @@ -5937,6 +6002,7 @@ static int systemctl_main(sd_bus *bus, int argc, char *argv[], int bus_error) { { "show-environment", EQUAL, 1, show_environment }, { "set-environment", MORE, 2, set_environment }, { "unset-environment", MORE, 2, set_environment }, + { "import-environment", MORE, 1, import_environment}, { "halt", EQUAL, 1, start_special, FORCE }, { "poweroff", EQUAL, 1, start_special, FORCE }, { "reboot", EQUAL, 1, start_special, FORCE }, |