]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/core/dbus-util.c
Merge pull request #30284 from YHNdnzj/fstab-wantedby-defaultdeps
[thirdparty/systemd.git] / src / core / dbus-util.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
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"
12
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) {
21
22 Unit *u = userdata, *trigger;
23
24 assert(bus);
25 assert(reply);
26 assert(u);
27
28 trigger = UNIT_TRIGGER(u);
29
30 return sd_bus_message_append(reply, "s", trigger ? trigger->id : NULL);
31 }
32
33 BUS_DEFINE_SET_TRANSIENT(mode_t, "u", uint32_t, mode_t, "%04o");
34 BUS_DEFINE_SET_TRANSIENT(unsigned, "u", uint32_t, unsigned, "%" PRIu32);
35
36 static 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);
38 }
39
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);
42
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) {
50
51 const char *v;
52 int r;
53
54 assert(p);
55
56 r = sd_bus_message_read(message, "s", &v);
57 if (r < 0)
58 return r;
59
60 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
61 r = free_and_strdup(p, empty_to_null(v));
62 if (r < 0)
63 return r;
64
65 unit_write_settingf(u, flags|UNIT_ESCAPE_SPECIFIERS, name,
66 "%s=%s", name, strempty(v));
67 }
68
69 return 1;
70 }
71
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) {
79
80 int v, r;
81
82 assert(p);
83
84 r = sd_bus_message_read(message, "b", &v);
85 if (r < 0)
86 return r;
87
88 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
89 *p = v;
90 unit_write_settingf(u, flags, name, "%s=%s", name, yes_no(v));
91 }
92
93 return 1;
94 }
95
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) {
103
104 int v, r;
105
106 assert(p);
107
108 r = sd_bus_message_read(message, "b", &v);
109 if (r < 0)
110 return r;
111
112 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
113 *p = v;
114 unit_write_settingf(u, flags, name, "%s=%s", name, yes_no(v));
115 }
116
117 return 1;
118 }
119
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) {
128
129 uint64_t v;
130 int r;
131
132 assert(p);
133
134 r = sd_bus_message_read(message, "t", &v);
135 if (r < 0)
136 return r;
137
138 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
139 if (fix_0)
140 *p = v != 0 ? v: USEC_INFINITY;
141 else
142 *p = v;
143
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));
146 }
147
148 return 1;
149 }
150
151 int bus_verify_manage_units_async_full(
152 Unit *u,
153 const char *verb,
154 const char *polkit_message,
155 sd_bus_message *call,
156 sd_bus_error *error) {
157
158 const char *details[9] = {
159 "unit", u->id,
160 "verb", verb,
161 };
162
163 if (polkit_message) {
164 details[4] = "polkit.message";
165 details[5] = polkit_message;
166 details[6] = "polkit.gettext_domain";
167 details[7] = GETTEXT_PACKAGE;
168 }
169
170 return bus_verify_polkit_async(
171 call,
172 "org.freedesktop.systemd1.manage-units",
173 details,
174 &u->manager->polkit_registry,
175 error);
176 }
177
178 /* ret_format_str is an accumulator, so if it has any pre-existing content, new options will be appended to it */
179 int bus_read_mount_options(
180 sd_bus_message *message,
181 sd_bus_error *error,
182 MountOptions **ret_options,
183 char **ret_format_str,
184 const char *separator) {
185
186 _cleanup_(mount_options_free_allp) MountOptions *options = NULL;
187 _cleanup_free_ char *format_str = NULL;
188 const char *mount_options, *partition;
189 int r;
190
191 assert(message);
192 assert(ret_options);
193 assert(separator);
194
195 r = sd_bus_message_enter_container(message, 'a', "(ss)");
196 if (r < 0)
197 return r;
198
199 while ((r = sd_bus_message_read(message, "(ss)", &partition, &mount_options)) > 0) {
200 _cleanup_free_ char *escaped = NULL;
201 _cleanup_free_ MountOptions *o = NULL;
202 PartitionDesignator partition_designator;
203
204 if (chars_intersect(mount_options, WHITESPACE))
205 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS,
206 "Invalid mount options string, contains whitespace character(s): %s", mount_options);
207
208 partition_designator = partition_designator_from_string(partition);
209 if (partition_designator < 0)
210 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid partition name %s", partition);
211
212 /* Need to store the options with the escapes, so that they can be parsed again */
213 escaped = shell_escape(mount_options, ":");
214 if (!escaped)
215 return -ENOMEM;
216
217 if (!strextend_with_separator(&format_str, separator, partition, ":", escaped))
218 return -ENOMEM;
219
220 o = new(MountOptions, 1);
221 if (!o)
222 return -ENOMEM;
223 *o = (MountOptions) {
224 .partition_designator = partition_designator,
225 .options = strdup(mount_options),
226 };
227 if (!o->options)
228 return -ENOMEM;
229 LIST_APPEND(mount_options, options, TAKE_PTR(o));
230 }
231 if (r < 0)
232 return r;
233
234 r = sd_bus_message_exit_container(message);
235 if (r < 0)
236 return r;
237
238 if (options) {
239 if (ret_format_str) {
240 char *final = strjoin(*ret_format_str, !isempty(*ret_format_str) ? separator : "", format_str);
241 if (!final)
242 return -ENOMEM;
243 free_and_replace(*ret_format_str, final);
244 }
245 LIST_JOIN(mount_options, *ret_options, options);
246 }
247
248 return 0;
249 }
250
251 int bus_property_get_activation_details(
252 sd_bus *bus,
253 const char *path,
254 const char *interface,
255 const char *property,
256 sd_bus_message *reply,
257 void *userdata,
258 sd_bus_error *error) {
259
260 ActivationDetails **details = ASSERT_PTR(userdata);
261 _cleanup_strv_free_ char **pairs = NULL;
262 int r;
263
264 assert(reply);
265
266 r = activation_details_append_pair(*details, &pairs);
267 if (r < 0)
268 return r;
269
270 r = sd_bus_message_open_container(reply, 'a', "(ss)");
271 if (r < 0)
272 return r;
273
274 STRV_FOREACH_PAIR(key, value, pairs) {
275 r = sd_bus_message_append(reply, "(ss)", *key, *value);
276 if (r < 0)
277 return r;
278 }
279
280 return sd_bus_message_close_container(reply);
281 }