]>
git.proxmox.com Git - 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 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, SHELL_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 log_warning("Calling import-environment without a list of variable names is deprecated.");
124 _cleanup_strv_free_
char **copy
= strv_copy(environ
);
128 strv_env_clean_with_callback(copy
, invalid_callback
, NULL
);
131 STRV_FOREACH(e
, copy
)
132 if (string_has_cc(*e
, NULL
))
133 log_notice("Environment variable $%.*s contains control characters, importing anyway.",
134 (int) strcspn(*e
, "="), *e
);
136 r
= sd_bus_message_append_strv(m
, copy
);
141 r
= sd_bus_message_open_container(m
, 'a', "s");
143 return bus_log_create_error(r
);
145 STRV_FOREACH(a
, strv_skip(argv
, 1)) {
147 if (!env_name_is_valid(*a
))
148 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
149 "Not a valid environment variable name: %s", *a
);
152 STRV_FOREACH(b
, environ
) {
155 eq
= startswith(*b
, *a
);
156 if (eq
&& *eq
== '=') {
157 if (string_has_cc(eq
+ 1, NULL
))
158 log_notice("Environment variable $%.*s contains control characters, importing anyway.",
159 (int) (eq
- *b
), *b
);
161 r
= sd_bus_message_append(m
, "s", *b
);
163 return bus_log_create_error(r
);
171 log_notice("Environment variable $%s not set, ignoring.", *a
);
174 r
= sd_bus_message_close_container(m
);
177 return bus_log_create_error(r
);
179 r
= sd_bus_call(bus
, m
, 0, &error
, NULL
);
181 return log_error_errno(r
, "Failed to import environment: %s", bus_error_message(&error
, r
));