]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/systemctl/systemctl-set-environment.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
4 #include "bus-locator.h"
7 #include "systemctl-set-environment.h"
8 #include "systemctl-util.h"
11 static int json_transform_message(sd_bus_message
*m
, JsonVariant
**ret
) {
12 _cleanup_(json_variant_unrefp
) JsonVariant
*v
= NULL
;
19 while ((r
= sd_bus_message_read_basic(m
, SD_BUS_TYPE_STRING
, &text
)) > 0) {
20 _cleanup_free_
char *n
= NULL
;
23 sep
= strchr(text
, '=');
25 return log_error_errno(SYNTHETIC_ERRNO(EUCLEAN
),
26 "Invalid environment block");
28 n
= strndup(text
, sep
- text
);
34 r
= json_variant_set_field_string(&v
, n
, sep
);
36 return log_error_errno(r
, "Failed to set JSON field '%s' to '%s': %m", n
, sep
);
39 return bus_log_parse_error(r
);
45 static int print_variable(const char *s
) {
47 _cleanup_free_
char *esc
= NULL
;
51 return log_error_errno(SYNTHETIC_ERRNO(EUCLEAN
),
52 "Invalid environment block");
54 esc
= shell_maybe_quote(sep
+ 1, SHELL_ESCAPE_POSIX
);
58 printf("%.*s=%s\n", (int)(sep
-s
), s
, esc
);
62 int verb_show_environment(int argc
, char *argv
[], void *userdata
) {
63 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
64 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
69 r
= acquire_bus(BUS_MANAGER
, &bus
);
73 pager_open(arg_pager_flags
);
75 r
= bus_get_property(bus
, bus_systemd_mgr
, "Environment", &error
, &reply
, "as");
77 return log_error_errno(r
, "Failed to get environment: %s", bus_error_message(&error
, r
));
79 r
= sd_bus_message_enter_container(reply
, SD_BUS_TYPE_ARRAY
, "s");
81 return bus_log_parse_error(r
);
83 if (OUTPUT_MODE_IS_JSON(arg_output
)) {
84 _cleanup_(json_variant_unrefp
) JsonVariant
*v
= NULL
;
86 r
= json_transform_message(reply
, &v
);
90 json_variant_dump(v
, output_mode_to_json_format_flags(arg_output
), stdout
, NULL
);
92 while ((r
= sd_bus_message_read_basic(reply
, SD_BUS_TYPE_STRING
, &text
)) > 0) {
93 r
= print_variable(text
);
98 return bus_log_parse_error(r
);
101 r
= sd_bus_message_exit_container(reply
);
103 return bus_log_parse_error(r
);
108 static void invalid_callback(const char *p
, void *userdata
) {
109 _cleanup_free_
char *t
= cescape(p
);
111 log_debug("Ignoring invalid environment assignment \"%s\".", strnull(t
));
114 int verb_set_environment(int argc
, char *argv
[], void *userdata
) {
115 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
116 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
124 r
= acquire_bus(BUS_MANAGER
, &bus
);
128 polkit_agent_open_maybe();
130 method
= streq(argv
[0], "set-environment")
132 : "UnsetEnvironment";
134 r
= bus_message_new_method_call(bus
, &m
, bus_systemd_mgr
, method
);
136 return bus_log_create_error(r
);
138 r
= sd_bus_message_append_strv(m
, strv_skip(argv
, 1));
140 return bus_log_create_error(r
);
142 r
= sd_bus_call(bus
, m
, 0, &error
, NULL
);
144 return log_error_errno(r
, "Failed to set environment: %s", bus_error_message(&error
, r
));
149 int verb_import_environment(int argc
, char *argv
[], void *userdata
) {
150 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
151 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
155 r
= acquire_bus(BUS_MANAGER
, &bus
);
159 polkit_agent_open_maybe();
161 r
= bus_message_new_method_call(bus
, &m
, bus_systemd_mgr
, "SetEnvironment");
163 return bus_log_create_error(r
);
166 log_warning("Calling import-environment without a list of variable names is deprecated.");
168 _cleanup_strv_free_
char **copy
= strv_copy(environ
);
172 strv_env_clean_with_callback(copy
, invalid_callback
, NULL
);
174 STRV_FOREACH(e
, copy
)
175 if (string_has_cc(*e
, NULL
))
176 log_notice("Environment variable $%.*s contains control characters, importing anyway.",
177 (int) strcspn(*e
, "="), *e
);
179 r
= sd_bus_message_append_strv(m
, copy
);
182 r
= sd_bus_message_open_container(m
, 'a', "s");
184 return bus_log_create_error(r
);
186 STRV_FOREACH(a
, strv_skip(argv
, 1)) {
188 if (!env_name_is_valid(*a
))
189 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
190 "Not a valid environment variable name: %s", *a
);
193 STRV_FOREACH(b
, environ
) {
196 eq
= startswith(*b
, *a
);
197 if (eq
&& *eq
== '=') {
198 if (string_has_cc(eq
+ 1, NULL
))
199 log_notice("Environment variable $%.*s contains control characters, importing anyway.",
200 (int) (eq
- *b
), *b
);
202 r
= sd_bus_message_append(m
, "s", *b
);
204 return bus_log_create_error(r
);
212 log_notice("Environment variable $%s not set, ignoring.", *a
);
215 r
= sd_bus_message_close_container(m
);
218 return bus_log_create_error(r
);
220 r
= sd_bus_call(bus
, m
, 0, &error
, NULL
);
222 return log_error_errno(r
, "Failed to import environment: %s", bus_error_message(&error
, r
));