udev: String substitutions can be done in ENV, too
[systemd_ALT.git] / src / core / dbus-util.c
blobe891cef1efdcc381e00a105b9e50b5e6aa49b59f
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
3 #include "bus-polkit.h"
4 #include "bus-util.h"
5 #include "dbus-util.h"
6 #include "escape.h"
7 #include "parse-util.h"
8 #include "path-util.h"
9 #include "unit-printf.h"
10 #include "user-util.h"
11 #include "unit.h"
13 int bus_property_get_triggered_unit(
14 sd_bus *bus,
15 const char *path,
16 const char *interface,
17 const char *property,
18 sd_bus_message *reply,
19 void *userdata,
20 sd_bus_error *error) {
22 Unit *u = userdata, *trigger;
24 assert(bus);
25 assert(reply);
26 assert(u);
28 trigger = UNIT_TRIGGER(u);
30 return sd_bus_message_append(reply, "s", trigger ? trigger->id : NULL);
33 BUS_DEFINE_SET_TRANSIENT(mode_t, "u", uint32_t, mode_t, "%04o");
34 BUS_DEFINE_SET_TRANSIENT(unsigned, "u", uint32_t, unsigned, "%" PRIu32);
36 static inline bool valid_user_group_name_or_id_relaxed(const char *u) {
37 return valid_user_group_name(u, VALID_USER_ALLOW_NUMERIC|VALID_USER_RELAX);
40 BUS_DEFINE_SET_TRANSIENT_STRING_WITH_CHECK(user_relaxed, valid_user_group_name_or_id_relaxed);
41 BUS_DEFINE_SET_TRANSIENT_STRING_WITH_CHECK(path, path_is_absolute);
43 int bus_set_transient_string(
44 Unit *u,
45 const char *name,
46 char **p,
47 sd_bus_message *message,
48 UnitWriteFlags flags,
49 sd_bus_error *error) {
51 const char *v;
52 int r;
54 assert(p);
56 r = sd_bus_message_read(message, "s", &v);
57 if (r < 0)
58 return r;
60 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
61 r = free_and_strdup(p, empty_to_null(v));
62 if (r < 0)
63 return r;
65 unit_write_settingf(u, flags|UNIT_ESCAPE_SPECIFIERS, name,
66 "%s=%s", name, strempty(v));
69 return 1;
72 int bus_set_transient_bool(
73 Unit *u,
74 const char *name,
75 bool *p,
76 sd_bus_message *message,
77 UnitWriteFlags flags,
78 sd_bus_error *error) {
80 int v, r;
82 assert(p);
84 r = sd_bus_message_read(message, "b", &v);
85 if (r < 0)
86 return r;
88 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
89 *p = v;
90 unit_write_settingf(u, flags, name, "%s=%s", name, yes_no(v));
93 return 1;
96 int bus_set_transient_tristate(
97 Unit *u,
98 const char *name,
99 int *p,
100 sd_bus_message *message,
101 UnitWriteFlags flags,
102 sd_bus_error *error) {
104 int v, r;
106 assert(p);
108 r = sd_bus_message_read(message, "b", &v);
109 if (r < 0)
110 return r;
112 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
113 *p = v;
114 unit_write_settingf(u, flags, name, "%s=%s", name, yes_no(v));
117 return 1;
120 int bus_set_transient_usec_internal(
121 Unit *u,
122 const char *name,
123 usec_t *p,
124 bool fix_0,
125 sd_bus_message *message,
126 UnitWriteFlags flags,
127 sd_bus_error *error) {
129 uint64_t v;
130 int r;
132 assert(p);
134 r = sd_bus_message_read(message, "t", &v);
135 if (r < 0)
136 return r;
138 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
139 if (fix_0)
140 *p = v != 0 ? v: USEC_INFINITY;
141 else
142 *p = v;
144 char *n = strndupa_safe(name, strlen(name) - 4);
145 unit_write_settingf(u, flags, name, "%sSec=%s", n, FORMAT_TIMESPAN(v, USEC_PER_MSEC));
148 return 1;
151 int bus_verify_manage_units_async_full(
152 Unit *u,
153 const char *verb,
154 int capability,
155 const char *polkit_message,
156 bool interactive,
157 sd_bus_message *call,
158 sd_bus_error *error) {
160 const char *details[9] = {
161 "unit", u->id,
162 "verb", verb,
165 if (polkit_message) {
166 details[4] = "polkit.message";
167 details[5] = polkit_message;
168 details[6] = "polkit.gettext_domain";
169 details[7] = GETTEXT_PACKAGE;
172 return bus_verify_polkit_async(
173 call,
174 capability,
175 "org.freedesktop.systemd1.manage-units",
176 details,
177 interactive,
178 UID_INVALID,
179 &u->manager->polkit_registry,
180 error);
183 /* ret_format_str is an accumulator, so if it has any pre-existing content, new options will be appended to it */
184 int bus_read_mount_options(
185 sd_bus_message *message,
186 sd_bus_error *error,
187 MountOptions **ret_options,
188 char **ret_format_str,
189 const char *separator) {
191 _cleanup_(mount_options_free_allp) MountOptions *options = NULL;
192 _cleanup_free_ char *format_str = NULL;
193 const char *mount_options, *partition;
194 int r;
196 assert(message);
197 assert(ret_options);
198 assert(separator);
200 r = sd_bus_message_enter_container(message, 'a', "(ss)");
201 if (r < 0)
202 return r;
204 while ((r = sd_bus_message_read(message, "(ss)", &partition, &mount_options)) > 0) {
205 _cleanup_free_ char *escaped = NULL;
206 _cleanup_free_ MountOptions *o = NULL;
207 PartitionDesignator partition_designator;
209 if (chars_intersect(mount_options, WHITESPACE))
210 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS,
211 "Invalid mount options string, contains whitespace character(s): %s", mount_options);
213 partition_designator = partition_designator_from_string(partition);
214 if (partition_designator < 0)
215 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid partition name %s", partition);
217 /* Need to store the options with the escapes, so that they can be parsed again */
218 escaped = shell_escape(mount_options, ":");
219 if (!escaped)
220 return -ENOMEM;
222 if (!strextend_with_separator(&format_str, separator, partition, ":", escaped))
223 return -ENOMEM;
225 o = new(MountOptions, 1);
226 if (!o)
227 return -ENOMEM;
228 *o = (MountOptions) {
229 .partition_designator = partition_designator,
230 .options = strdup(mount_options),
232 if (!o->options)
233 return -ENOMEM;
234 LIST_APPEND(mount_options, options, TAKE_PTR(o));
236 if (r < 0)
237 return r;
239 r = sd_bus_message_exit_container(message);
240 if (r < 0)
241 return r;
243 if (options) {
244 if (ret_format_str) {
245 char *final = strjoin(*ret_format_str, !isempty(*ret_format_str) ? separator : "", format_str);
246 if (!final)
247 return -ENOMEM;
248 free_and_replace(*ret_format_str, final);
250 LIST_JOIN(mount_options, *ret_options, options);
253 return 0;
256 int bus_property_get_activation_details(
257 sd_bus *bus,
258 const char *path,
259 const char *interface,
260 const char *property,
261 sd_bus_message *reply,
262 void *userdata,
263 sd_bus_error *error) {
265 ActivationDetails **details = ASSERT_PTR(userdata);
266 _cleanup_strv_free_ char **pairs = NULL;
267 int r;
269 assert(reply);
271 r = activation_details_append_pair(*details, &pairs);
272 if (r < 0)
273 return r;
275 r = sd_bus_message_open_container(reply, 'a', "(ss)");
276 if (r < 0)
277 return r;
279 STRV_FOREACH_PAIR(key, value, pairs) {
280 r = sd_bus_message_append(reply, "(ss)", *key, *value);
281 if (r < 0)
282 return r;
285 return sd_bus_message_close_container(reply);