]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/systemctl/systemctl-set-environment.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
4 #include "bus-locator.h"
7 #include "systemctl-set-environment.h"
8 #include "systemctl-util.h"
11 static int print_variable(const char *s
) {
13 _cleanup_free_
char *esc
= NULL
;
17 return log_error_errno(SYNTHETIC_ERRNO(EUCLEAN
),
18 "Invalid environment block");
20 esc
= shell_maybe_quote(sep
+ 1, ESCAPE_POSIX
);
24 printf("%.*s=%s\n", (int)(sep
-s
), s
, esc
);
28 int show_environment(int argc
, char *argv
[], void *userdata
) {
29 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
30 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
35 r
= acquire_bus(BUS_MANAGER
, &bus
);
39 (void) pager_open(arg_pager_flags
);
41 r
= bus_get_property(bus
, bus_systemd_mgr
, "Environment", &error
, &reply
, "as");
43 return log_error_errno(r
, "Failed to get environment: %s", bus_error_message(&error
, r
));
45 r
= sd_bus_message_enter_container(reply
, SD_BUS_TYPE_ARRAY
, "s");
47 return bus_log_parse_error(r
);
49 while ((r
= sd_bus_message_read_basic(reply
, SD_BUS_TYPE_STRING
, &text
)) > 0) {
50 r
= print_variable(text
);
55 return bus_log_parse_error(r
);
57 r
= sd_bus_message_exit_container(reply
);
59 return bus_log_parse_error(r
);
64 static void invalid_callback(const char *p
, void *userdata
) {
65 _cleanup_free_
char *t
= cescape(p
);
67 log_debug("Ignoring invalid environment assignment \"%s\".", strnull(t
));
70 int set_environment(int argc
, char *argv
[], void *userdata
) {
71 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
72 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
80 r
= acquire_bus(BUS_MANAGER
, &bus
);
84 polkit_agent_open_maybe();
86 method
= streq(argv
[0], "set-environment")
90 r
= bus_message_new_method_call(bus
, &m
, bus_systemd_mgr
, method
);
92 return bus_log_create_error(r
);
94 r
= sd_bus_message_append_strv(m
, strv_skip(argv
, 1));
96 return bus_log_create_error(r
);
98 r
= sd_bus_call(bus
, m
, 0, &error
, NULL
);
100 return log_error_errno(r
, "Failed to set environment: %s", bus_error_message(&error
, r
));
105 int import_environment(int argc
, char *argv
[], void *userdata
) {
106 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
107 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
111 r
= acquire_bus(BUS_MANAGER
, &bus
);
115 polkit_agent_open_maybe();
117 r
= bus_message_new_method_call(bus
, &m
, bus_systemd_mgr
, "SetEnvironment");
119 return bus_log_create_error(r
);
122 _cleanup_strv_free_
char **copy
= NULL
;
124 copy
= strv_copy(environ
);
128 strv_env_clean_with_callback(copy
, invalid_callback
, NULL
);
130 r
= sd_bus_message_append_strv(m
, copy
);
135 r
= sd_bus_message_open_container(m
, 'a', "s");
137 return bus_log_create_error(r
);
139 STRV_FOREACH(a
, strv_skip(argv
, 1)) {
141 if (!env_name_is_valid(*a
))
142 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Not a valid environment variable name: %s", *a
);
144 STRV_FOREACH(b
, environ
) {
147 eq
= startswith(*b
, *a
);
148 if (eq
&& *eq
== '=') {
150 r
= sd_bus_message_append(m
, "s", *b
);
152 return bus_log_create_error(r
);
159 r
= sd_bus_message_close_container(m
);
162 return bus_log_create_error(r
);
164 r
= sd_bus_call(bus
, m
, 0, &error
, NULL
);
166 return log_error_errno(r
, "Failed to import environment: %s", bus_error_message(&error
, r
));