/src/systemd/src/core/dbus-unit.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* SPDX-License-Identifier: LGPL-2.1+ */ |
2 | | |
3 | | #include "sd-bus.h" |
4 | | |
5 | | #include "alloc-util.h" |
6 | | #include "bpf-firewall.h" |
7 | | #include "bus-common-errors.h" |
8 | | #include "cgroup-util.h" |
9 | | #include "condition.h" |
10 | | #include "dbus-job.h" |
11 | | #include "dbus-unit.h" |
12 | | #include "dbus-util.h" |
13 | | #include "dbus.h" |
14 | | #include "fd-util.h" |
15 | | #include "locale-util.h" |
16 | | #include "log.h" |
17 | | #include "path-util.h" |
18 | | #include "process-util.h" |
19 | | #include "selinux-access.h" |
20 | | #include "signal-util.h" |
21 | | #include "special.h" |
22 | | #include "string-table.h" |
23 | | #include "string-util.h" |
24 | | #include "strv.h" |
25 | | #include "user-util.h" |
26 | | #include "web-util.h" |
27 | | |
28 | 0 | static bool unit_can_start_refuse_manual(Unit *u) { |
29 | 0 | return unit_can_start(u) && !u->refuse_manual_start; |
30 | 0 | } |
31 | | |
32 | 0 | static bool unit_can_stop_refuse_manual(Unit *u) { |
33 | 0 | return unit_can_stop(u) && !u->refuse_manual_stop; |
34 | 0 | } |
35 | | |
36 | 0 | static bool unit_can_isolate_refuse_manual(Unit *u) { |
37 | 0 | return unit_can_isolate(u) && !u->refuse_manual_start; |
38 | 0 | } |
39 | | |
40 | | static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_collect_mode, collect_mode, CollectMode); |
41 | | static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_load_state, unit_load_state, UnitLoadState); |
42 | | static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_job_mode, job_mode, JobMode); |
43 | | static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_emergency_action, emergency_action, EmergencyAction); |
44 | | static BUS_DEFINE_PROPERTY_GET(property_get_description, "s", Unit, unit_description); |
45 | | static BUS_DEFINE_PROPERTY_GET2(property_get_active_state, "s", Unit, unit_active_state, unit_active_state_to_string); |
46 | | static BUS_DEFINE_PROPERTY_GET(property_get_sub_state, "s", Unit, unit_sub_state_to_string); |
47 | | static BUS_DEFINE_PROPERTY_GET2(property_get_unit_file_state, "s", Unit, unit_get_unit_file_state, unit_file_state_to_string); |
48 | | static BUS_DEFINE_PROPERTY_GET(property_get_can_reload, "b", Unit, unit_can_reload); |
49 | | static BUS_DEFINE_PROPERTY_GET(property_get_can_start, "b", Unit, unit_can_start_refuse_manual); |
50 | | static BUS_DEFINE_PROPERTY_GET(property_get_can_stop, "b", Unit, unit_can_stop_refuse_manual); |
51 | | static BUS_DEFINE_PROPERTY_GET(property_get_can_isolate, "b", Unit, unit_can_isolate_refuse_manual); |
52 | | static BUS_DEFINE_PROPERTY_GET(property_get_need_daemon_reload, "b", Unit, unit_need_daemon_reload); |
53 | | static BUS_DEFINE_PROPERTY_GET_GLOBAL(property_get_empty_strv, "as", 0); |
54 | | |
55 | | static int property_get_names( |
56 | | sd_bus *bus, |
57 | | const char *path, |
58 | | const char *interface, |
59 | | const char *property, |
60 | | sd_bus_message *reply, |
61 | | void *userdata, |
62 | 0 | sd_bus_error *error) { |
63 | 0 |
|
64 | 0 | Set **s = userdata; |
65 | 0 | Iterator i; |
66 | 0 | const char *t; |
67 | 0 | int r; |
68 | 0 |
|
69 | 0 | assert(bus); |
70 | 0 | assert(reply); |
71 | 0 | assert(s); |
72 | 0 |
|
73 | 0 | r = sd_bus_message_open_container(reply, 'a', "s"); |
74 | 0 | if (r < 0) |
75 | 0 | return r; |
76 | 0 | |
77 | 0 | SET_FOREACH(t, *s, i) { |
78 | 0 | r = sd_bus_message_append(reply, "s", t); |
79 | 0 | if (r < 0) |
80 | 0 | return r; |
81 | 0 | } |
82 | 0 |
|
83 | 0 | return sd_bus_message_close_container(reply); |
84 | 0 | } |
85 | | |
86 | | static int property_get_following( |
87 | | sd_bus *bus, |
88 | | const char *path, |
89 | | const char *interface, |
90 | | const char *property, |
91 | | sd_bus_message *reply, |
92 | | void *userdata, |
93 | 0 | sd_bus_error *error) { |
94 | 0 |
|
95 | 0 | Unit *u = userdata, *f; |
96 | 0 |
|
97 | 0 | assert(bus); |
98 | 0 | assert(reply); |
99 | 0 | assert(u); |
100 | 0 |
|
101 | 0 | f = unit_following(u); |
102 | 0 | return sd_bus_message_append(reply, "s", f ? f->id : NULL); |
103 | 0 | } |
104 | | |
105 | | static int property_get_dependencies( |
106 | | sd_bus *bus, |
107 | | const char *path, |
108 | | const char *interface, |
109 | | const char *property, |
110 | | sd_bus_message *reply, |
111 | | void *userdata, |
112 | 0 | sd_bus_error *error) { |
113 | 0 |
|
114 | 0 | Hashmap **h = userdata; |
115 | 0 | Iterator j; |
116 | 0 | Unit *u; |
117 | 0 | void *v; |
118 | 0 | int r; |
119 | 0 |
|
120 | 0 | assert(bus); |
121 | 0 | assert(reply); |
122 | 0 | assert(h); |
123 | 0 |
|
124 | 0 | r = sd_bus_message_open_container(reply, 'a', "s"); |
125 | 0 | if (r < 0) |
126 | 0 | return r; |
127 | 0 | |
128 | 0 | HASHMAP_FOREACH_KEY(v, u, *h, j) { |
129 | 0 | r = sd_bus_message_append(reply, "s", u->id); |
130 | 0 | if (r < 0) |
131 | 0 | return r; |
132 | 0 | } |
133 | 0 |
|
134 | 0 | return sd_bus_message_close_container(reply); |
135 | 0 | } |
136 | | |
137 | | static int property_get_requires_mounts_for( |
138 | | sd_bus *bus, |
139 | | const char *path, |
140 | | const char *interface, |
141 | | const char *property, |
142 | | sd_bus_message *reply, |
143 | | void *userdata, |
144 | 0 | sd_bus_error *error) { |
145 | 0 |
|
146 | 0 | Hashmap **h = userdata; |
147 | 0 | const char *p; |
148 | 0 | Iterator j; |
149 | 0 | void *v; |
150 | 0 | int r; |
151 | 0 |
|
152 | 0 | assert(bus); |
153 | 0 | assert(reply); |
154 | 0 | assert(h); |
155 | 0 |
|
156 | 0 | r = sd_bus_message_open_container(reply, 'a', "s"); |
157 | 0 | if (r < 0) |
158 | 0 | return r; |
159 | 0 | |
160 | 0 | HASHMAP_FOREACH_KEY(v, p, *h, j) { |
161 | 0 | r = sd_bus_message_append(reply, "s", p); |
162 | 0 | if (r < 0) |
163 | 0 | return r; |
164 | 0 | } |
165 | 0 |
|
166 | 0 | return sd_bus_message_close_container(reply); |
167 | 0 | } |
168 | | |
169 | | static int property_get_unit_file_preset( |
170 | | sd_bus *bus, |
171 | | const char *path, |
172 | | const char *interface, |
173 | | const char *property, |
174 | | sd_bus_message *reply, |
175 | | void *userdata, |
176 | 0 | sd_bus_error *error) { |
177 | 0 |
|
178 | 0 | Unit *u = userdata; |
179 | 0 | int r; |
180 | 0 |
|
181 | 0 | assert(bus); |
182 | 0 | assert(reply); |
183 | 0 | assert(u); |
184 | 0 |
|
185 | 0 | r = unit_get_unit_file_preset(u); |
186 | 0 |
|
187 | 0 | return sd_bus_message_append(reply, "s", |
188 | 0 | r < 0 ? NULL: |
189 | 0 | r > 0 ? "enabled" : "disabled"); |
190 | 0 | } |
191 | | |
192 | | static int property_get_job( |
193 | | sd_bus *bus, |
194 | | const char *path, |
195 | | const char *interface, |
196 | | const char *property, |
197 | | sd_bus_message *reply, |
198 | | void *userdata, |
199 | 0 | sd_bus_error *error) { |
200 | 0 |
|
201 | 0 | _cleanup_free_ char *p = NULL; |
202 | 0 | Job **j = userdata; |
203 | 0 |
|
204 | 0 | assert(bus); |
205 | 0 | assert(reply); |
206 | 0 | assert(j); |
207 | 0 |
|
208 | 0 | if (!*j) |
209 | 0 | return sd_bus_message_append(reply, "(uo)", 0, "/"); |
210 | 0 | |
211 | 0 | p = job_dbus_path(*j); |
212 | 0 | if (!p) |
213 | 0 | return -ENOMEM; |
214 | 0 | |
215 | 0 | return sd_bus_message_append(reply, "(uo)", (*j)->id, p); |
216 | 0 | } |
217 | | |
218 | | static int property_get_conditions( |
219 | | sd_bus *bus, |
220 | | const char *path, |
221 | | const char *interface, |
222 | | const char *property, |
223 | | sd_bus_message *reply, |
224 | | void *userdata, |
225 | 0 | sd_bus_error *error) { |
226 | 0 |
|
227 | 0 | const char *(*to_string)(ConditionType type) = NULL; |
228 | 0 | Condition **list = userdata, *c; |
229 | 0 | int r; |
230 | 0 |
|
231 | 0 | assert(bus); |
232 | 0 | assert(reply); |
233 | 0 | assert(list); |
234 | 0 |
|
235 | 0 | to_string = streq(property, "Asserts") ? assert_type_to_string : condition_type_to_string; |
236 | 0 |
|
237 | 0 | r = sd_bus_message_open_container(reply, 'a', "(sbbsi)"); |
238 | 0 | if (r < 0) |
239 | 0 | return r; |
240 | 0 | |
241 | 0 | LIST_FOREACH(conditions, c, *list) { |
242 | 0 | int tristate; |
243 | 0 |
|
244 | 0 | tristate = |
245 | 0 | c->result == CONDITION_UNTESTED ? 0 : |
246 | 0 | c->result == CONDITION_SUCCEEDED ? 1 : -1; |
247 | 0 |
|
248 | 0 | r = sd_bus_message_append(reply, "(sbbsi)", |
249 | 0 | to_string(c->type), |
250 | 0 | c->trigger, c->negate, |
251 | 0 | c->parameter, tristate); |
252 | 0 | if (r < 0) |
253 | 0 | return r; |
254 | 0 |
|
255 | 0 | } |
256 | 0 |
|
257 | 0 | return sd_bus_message_close_container(reply); |
258 | 0 | } |
259 | | |
260 | | static int property_get_load_error( |
261 | | sd_bus *bus, |
262 | | const char *path, |
263 | | const char *interface, |
264 | | const char *property, |
265 | | sd_bus_message *reply, |
266 | | void *userdata, |
267 | 0 | sd_bus_error *error) { |
268 | 0 |
|
269 | 0 | _cleanup_(sd_bus_error_free) sd_bus_error e = SD_BUS_ERROR_NULL; |
270 | 0 | Unit *u = userdata; |
271 | 0 | int r; |
272 | 0 |
|
273 | 0 | assert(bus); |
274 | 0 | assert(reply); |
275 | 0 | assert(u); |
276 | 0 |
|
277 | 0 | r = bus_unit_validate_load_state(u, &e); |
278 | 0 | if (r < 0) |
279 | 0 | return sd_bus_message_append(reply, "(ss)", e.name, e.message); |
280 | 0 | |
281 | 0 | return sd_bus_message_append(reply, "(ss)", NULL, NULL); |
282 | 0 | } |
283 | | |
284 | | static int bus_verify_manage_units_async_full( |
285 | | Unit *u, |
286 | | const char *verb, |
287 | | int capability, |
288 | | const char *polkit_message, |
289 | | bool interactive, |
290 | | sd_bus_message *call, |
291 | 0 | sd_bus_error *error) { |
292 | 0 |
|
293 | 0 | const char *details[9] = { |
294 | 0 | "unit", u->id, |
295 | 0 | "verb", verb, |
296 | 0 | }; |
297 | 0 |
|
298 | 0 | if (polkit_message) { |
299 | 0 | details[4] = "polkit.message"; |
300 | 0 | details[5] = polkit_message; |
301 | 0 | details[6] = "polkit.gettext_domain"; |
302 | 0 | details[7] = GETTEXT_PACKAGE; |
303 | 0 | } |
304 | 0 |
|
305 | 0 | return bus_verify_polkit_async( |
306 | 0 | call, |
307 | 0 | capability, |
308 | 0 | "org.freedesktop.systemd1.manage-units", |
309 | 0 | details, |
310 | 0 | interactive, |
311 | 0 | UID_INVALID, |
312 | 0 | &u->manager->polkit_registry, |
313 | 0 | error); |
314 | 0 | } |
315 | | |
316 | | static const char *const polkit_message_for_job[_JOB_TYPE_MAX] = { |
317 | | [JOB_START] = N_("Authentication is required to start '$(unit)'."), |
318 | | [JOB_STOP] = N_("Authentication is required to stop '$(unit)'."), |
319 | | [JOB_RELOAD] = N_("Authentication is required to reload '$(unit)'."), |
320 | | [JOB_RESTART] = N_("Authentication is required to restart '$(unit)'."), |
321 | | [JOB_TRY_RESTART] = N_("Authentication is required to restart '$(unit)'."), |
322 | | }; |
323 | | |
324 | | int bus_unit_method_start_generic( |
325 | | sd_bus_message *message, |
326 | | Unit *u, |
327 | | JobType job_type, |
328 | | bool reload_if_possible, |
329 | 0 | sd_bus_error *error) { |
330 | 0 |
|
331 | 0 | const char *smode, *verb; |
332 | 0 | JobMode mode; |
333 | 0 | int r; |
334 | 0 |
|
335 | 0 | assert(message); |
336 | 0 | assert(u); |
337 | 0 | assert(job_type >= 0 && job_type < _JOB_TYPE_MAX); |
338 | 0 |
|
339 | 0 | r = mac_selinux_unit_access_check( |
340 | 0 | u, message, |
341 | 0 | job_type_to_access_method(job_type), |
342 | 0 | error); |
343 | 0 | if (r < 0) |
344 | 0 | return r; |
345 | 0 | |
346 | 0 | r = sd_bus_message_read(message, "s", &smode); |
347 | 0 | if (r < 0) |
348 | 0 | return r; |
349 | 0 | |
350 | 0 | mode = job_mode_from_string(smode); |
351 | 0 | if (mode < 0) |
352 | 0 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Job mode %s invalid", smode); |
353 | 0 |
|
354 | 0 | if (reload_if_possible) |
355 | 0 | verb = strjoina("reload-or-", job_type_to_string(job_type)); |
356 | 0 | else |
357 | 0 | verb = job_type_to_string(job_type); |
358 | 0 |
|
359 | 0 | r = bus_verify_manage_units_async_full( |
360 | 0 | u, |
361 | 0 | verb, |
362 | 0 | CAP_SYS_ADMIN, |
363 | 0 | polkit_message_for_job[job_type], |
364 | 0 | true, |
365 | 0 | message, |
366 | 0 | error); |
367 | 0 | if (r < 0) |
368 | 0 | return r; |
369 | 0 | if (r == 0) |
370 | 0 | return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */ |
371 | 0 | |
372 | 0 | return bus_unit_queue_job(message, u, job_type, mode, |
373 | 0 | reload_if_possible ? BUS_UNIT_QUEUE_RELOAD_IF_POSSIBLE : 0, error); |
374 | 0 | } |
375 | | |
376 | 0 | static int method_start(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
377 | 0 | return bus_unit_method_start_generic(message, userdata, JOB_START, false, error); |
378 | 0 | } |
379 | | |
380 | 0 | static int method_stop(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
381 | 0 | return bus_unit_method_start_generic(message, userdata, JOB_STOP, false, error); |
382 | 0 | } |
383 | | |
384 | 0 | static int method_reload(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
385 | 0 | return bus_unit_method_start_generic(message, userdata, JOB_RELOAD, false, error); |
386 | 0 | } |
387 | | |
388 | 0 | static int method_restart(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
389 | 0 | return bus_unit_method_start_generic(message, userdata, JOB_RESTART, false, error); |
390 | 0 | } |
391 | | |
392 | 0 | static int method_try_restart(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
393 | 0 | return bus_unit_method_start_generic(message, userdata, JOB_TRY_RESTART, false, error); |
394 | 0 | } |
395 | | |
396 | 0 | static int method_reload_or_restart(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
397 | 0 | return bus_unit_method_start_generic(message, userdata, JOB_RESTART, true, error); |
398 | 0 | } |
399 | | |
400 | 0 | static int method_reload_or_try_restart(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
401 | 0 | return bus_unit_method_start_generic(message, userdata, JOB_TRY_RESTART, true, error); |
402 | 0 | } |
403 | | |
404 | 0 | int bus_unit_method_enqueue_job(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
405 | 0 | BusUnitQueueFlags flags = BUS_UNIT_QUEUE_VERBOSE_REPLY; |
406 | 0 | const char *jtype, *smode; |
407 | 0 | Unit *u = userdata; |
408 | 0 | JobType type; |
409 | 0 | JobMode mode; |
410 | 0 | int r; |
411 | 0 |
|
412 | 0 | assert(message); |
413 | 0 | assert(u); |
414 | 0 |
|
415 | 0 | r = sd_bus_message_read(message, "ss", &jtype, &smode); |
416 | 0 | if (r < 0) |
417 | 0 | return r; |
418 | 0 | |
419 | 0 | /* Parse the two magic reload types "reload-or-…" manually */ |
420 | 0 | if (streq(jtype, "reload-or-restart")) { |
421 | 0 | type = JOB_RESTART; |
422 | 0 | flags |= BUS_UNIT_QUEUE_RELOAD_IF_POSSIBLE; |
423 | 0 | } else if (streq(jtype, "reload-or-try-restart")) { |
424 | 0 | type = JOB_TRY_RESTART; |
425 | 0 | flags |= BUS_UNIT_QUEUE_RELOAD_IF_POSSIBLE; |
426 | 0 | } else { |
427 | 0 | /* And the rest generically */ |
428 | 0 | type = job_type_from_string(jtype); |
429 | 0 | if (type < 0) |
430 | 0 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Job type %s invalid", jtype); |
431 | 0 | } |
432 | 0 |
|
433 | 0 | mode = job_mode_from_string(smode); |
434 | 0 | if (mode < 0) |
435 | 0 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Job mode %s invalid", smode); |
436 | 0 |
|
437 | 0 | r = mac_selinux_unit_access_check( |
438 | 0 | u, message, |
439 | 0 | job_type_to_access_method(type), |
440 | 0 | error); |
441 | 0 | if (r < 0) |
442 | 0 | return r; |
443 | 0 | |
444 | 0 | r = bus_verify_manage_units_async_full( |
445 | 0 | u, |
446 | 0 | jtype, |
447 | 0 | CAP_SYS_ADMIN, |
448 | 0 | polkit_message_for_job[type], |
449 | 0 | true, |
450 | 0 | message, |
451 | 0 | error); |
452 | 0 | if (r < 0) |
453 | 0 | return r; |
454 | 0 | if (r == 0) |
455 | 0 | return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */ |
456 | 0 | |
457 | 0 | return bus_unit_queue_job(message, u, type, mode, flags, error); |
458 | 0 | } |
459 | | |
460 | 0 | int bus_unit_method_kill(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
461 | 0 | Unit *u = userdata; |
462 | 0 | const char *swho; |
463 | 0 | int32_t signo; |
464 | 0 | KillWho who; |
465 | 0 | int r; |
466 | 0 |
|
467 | 0 | assert(message); |
468 | 0 | assert(u); |
469 | 0 |
|
470 | 0 | r = mac_selinux_unit_access_check(u, message, "stop", error); |
471 | 0 | if (r < 0) |
472 | 0 | return r; |
473 | 0 | |
474 | 0 | r = sd_bus_message_read(message, "si", &swho, &signo); |
475 | 0 | if (r < 0) |
476 | 0 | return r; |
477 | 0 | |
478 | 0 | if (isempty(swho)) |
479 | 0 | who = KILL_ALL; |
480 | 0 | else { |
481 | 0 | who = kill_who_from_string(swho); |
482 | 0 | if (who < 0) |
483 | 0 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid who argument %s", swho); |
484 | 0 | } |
485 | 0 |
|
486 | 0 | if (!SIGNAL_VALID(signo)) |
487 | 0 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Signal number out of range."); |
488 | 0 |
|
489 | 0 | r = bus_verify_manage_units_async_full( |
490 | 0 | u, |
491 | 0 | "kill", |
492 | 0 | CAP_KILL, |
493 | 0 | N_("Authentication is required to send a UNIX signal to the processes of '$(unit)'."), |
494 | 0 | true, |
495 | 0 | message, |
496 | 0 | error); |
497 | 0 | if (r < 0) |
498 | 0 | return r; |
499 | 0 | if (r == 0) |
500 | 0 | return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */ |
501 | 0 | |
502 | 0 | r = unit_kill(u, who, signo, error); |
503 | 0 | if (r < 0) |
504 | 0 | return r; |
505 | 0 | |
506 | 0 | return sd_bus_reply_method_return(message, NULL); |
507 | 0 | } |
508 | | |
509 | 0 | int bus_unit_method_reset_failed(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
510 | 0 | Unit *u = userdata; |
511 | 0 | int r; |
512 | 0 |
|
513 | 0 | assert(message); |
514 | 0 | assert(u); |
515 | 0 |
|
516 | 0 | r = mac_selinux_unit_access_check(u, message, "reload", error); |
517 | 0 | if (r < 0) |
518 | 0 | return r; |
519 | 0 | |
520 | 0 | r = bus_verify_manage_units_async_full( |
521 | 0 | u, |
522 | 0 | "reset-failed", |
523 | 0 | CAP_SYS_ADMIN, |
524 | 0 | N_("Authentication is required to reset the \"failed\" state of '$(unit)'."), |
525 | 0 | true, |
526 | 0 | message, |
527 | 0 | error); |
528 | 0 | if (r < 0) |
529 | 0 | return r; |
530 | 0 | if (r == 0) |
531 | 0 | return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */ |
532 | 0 | |
533 | 0 | unit_reset_failed(u); |
534 | 0 |
|
535 | 0 | return sd_bus_reply_method_return(message, NULL); |
536 | 0 | } |
537 | | |
538 | 0 | int bus_unit_method_set_properties(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
539 | 0 | Unit *u = userdata; |
540 | 0 | int runtime, r; |
541 | 0 |
|
542 | 0 | assert(message); |
543 | 0 | assert(u); |
544 | 0 |
|
545 | 0 | r = mac_selinux_unit_access_check(u, message, "start", error); |
546 | 0 | if (r < 0) |
547 | 0 | return r; |
548 | 0 | |
549 | 0 | r = sd_bus_message_read(message, "b", &runtime); |
550 | 0 | if (r < 0) |
551 | 0 | return r; |
552 | 0 | |
553 | 0 | r = bus_verify_manage_units_async_full( |
554 | 0 | u, |
555 | 0 | "set-property", |
556 | 0 | CAP_SYS_ADMIN, |
557 | 0 | N_("Authentication is required to set properties on '$(unit)'."), |
558 | 0 | true, |
559 | 0 | message, |
560 | 0 | error); |
561 | 0 | if (r < 0) |
562 | 0 | return r; |
563 | 0 | if (r == 0) |
564 | 0 | return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */ |
565 | 0 | |
566 | 0 | r = bus_unit_set_properties(u, message, runtime ? UNIT_RUNTIME : UNIT_PERSISTENT, true, error); |
567 | 0 | if (r < 0) |
568 | 0 | return r; |
569 | 0 | |
570 | 0 | return sd_bus_reply_method_return(message, NULL); |
571 | 0 | } |
572 | | |
573 | 0 | int bus_unit_method_ref(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
574 | 0 | Unit *u = userdata; |
575 | 0 | int r; |
576 | 0 |
|
577 | 0 | assert(message); |
578 | 0 | assert(u); |
579 | 0 |
|
580 | 0 | r = mac_selinux_unit_access_check(u, message, "start", error); |
581 | 0 | if (r < 0) |
582 | 0 | return r; |
583 | 0 | |
584 | 0 | r = bus_verify_manage_units_async_full( |
585 | 0 | u, |
586 | 0 | "ref", |
587 | 0 | CAP_SYS_ADMIN, |
588 | 0 | NULL, |
589 | 0 | false, |
590 | 0 | message, |
591 | 0 | error); |
592 | 0 | if (r < 0) |
593 | 0 | return r; |
594 | 0 | if (r == 0) |
595 | 0 | return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */ |
596 | 0 | |
597 | 0 | r = bus_unit_track_add_sender(u, message); |
598 | 0 | if (r < 0) |
599 | 0 | return r; |
600 | 0 | |
601 | 0 | return sd_bus_reply_method_return(message, NULL); |
602 | 0 | } |
603 | | |
604 | 0 | int bus_unit_method_unref(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
605 | 0 | Unit *u = userdata; |
606 | 0 | int r; |
607 | 0 |
|
608 | 0 | assert(message); |
609 | 0 | assert(u); |
610 | 0 |
|
611 | 0 | r = bus_unit_track_remove_sender(u, message); |
612 | 0 | if (r == -EUNATCH) |
613 | 0 | return sd_bus_error_setf(error, BUS_ERROR_NOT_REFERENCED, "Unit has not been referenced yet."); |
614 | 0 | if (r < 0) |
615 | 0 | return r; |
616 | 0 | |
617 | 0 | return sd_bus_reply_method_return(message, NULL); |
618 | 0 | } |
619 | | |
620 | | static int property_get_refs( |
621 | | sd_bus *bus, |
622 | | const char *path, |
623 | | const char *interface, |
624 | | const char *property, |
625 | | sd_bus_message *reply, |
626 | | void *userdata, |
627 | 0 | sd_bus_error *error) { |
628 | 0 |
|
629 | 0 | Unit *u = userdata; |
630 | 0 | const char *i; |
631 | 0 | int r; |
632 | 0 |
|
633 | 0 | assert(bus); |
634 | 0 | assert(reply); |
635 | 0 |
|
636 | 0 | r = sd_bus_message_open_container(reply, 'a', "s"); |
637 | 0 | if (r < 0) |
638 | 0 | return r; |
639 | 0 | |
640 | 0 | for (i = sd_bus_track_first(u->bus_track); i; i = sd_bus_track_next(u->bus_track)) { |
641 | 0 | int c, k; |
642 | 0 |
|
643 | 0 | c = sd_bus_track_count_name(u->bus_track, i); |
644 | 0 | if (c < 0) |
645 | 0 | return c; |
646 | 0 | |
647 | 0 | /* Add the item multiple times if the ref count for each is above 1 */ |
648 | 0 | for (k = 0; k < c; k++) { |
649 | 0 | r = sd_bus_message_append(reply, "s", i); |
650 | 0 | if (r < 0) |
651 | 0 | return r; |
652 | 0 | } |
653 | 0 | } |
654 | 0 |
|
655 | 0 | return sd_bus_message_close_container(reply); |
656 | 0 | } |
657 | | |
658 | | const sd_bus_vtable bus_unit_vtable[] = { |
659 | | SD_BUS_VTABLE_START(0), |
660 | | |
661 | | SD_BUS_PROPERTY("Id", "s", NULL, offsetof(Unit, id), SD_BUS_VTABLE_PROPERTY_CONST), |
662 | | SD_BUS_PROPERTY("Names", "as", property_get_names, offsetof(Unit, names), SD_BUS_VTABLE_PROPERTY_CONST), |
663 | | SD_BUS_PROPERTY("Following", "s", property_get_following, 0, 0), |
664 | | SD_BUS_PROPERTY("Requires", "as", property_get_dependencies, offsetof(Unit, dependencies[UNIT_REQUIRES]), SD_BUS_VTABLE_PROPERTY_CONST), |
665 | | SD_BUS_PROPERTY("Requisite", "as", property_get_dependencies, offsetof(Unit, dependencies[UNIT_REQUISITE]), SD_BUS_VTABLE_PROPERTY_CONST), |
666 | | SD_BUS_PROPERTY("Wants", "as", property_get_dependencies, offsetof(Unit, dependencies[UNIT_WANTS]), SD_BUS_VTABLE_PROPERTY_CONST), |
667 | | SD_BUS_PROPERTY("BindsTo", "as", property_get_dependencies, offsetof(Unit, dependencies[UNIT_BINDS_TO]), SD_BUS_VTABLE_PROPERTY_CONST), |
668 | | SD_BUS_PROPERTY("PartOf", "as", property_get_dependencies, offsetof(Unit, dependencies[UNIT_PART_OF]), SD_BUS_VTABLE_PROPERTY_CONST), |
669 | | SD_BUS_PROPERTY("RequiredBy", "as", property_get_dependencies, offsetof(Unit, dependencies[UNIT_REQUIRED_BY]), SD_BUS_VTABLE_PROPERTY_CONST), |
670 | | SD_BUS_PROPERTY("RequisiteOf", "as", property_get_dependencies, offsetof(Unit, dependencies[UNIT_REQUISITE_OF]), SD_BUS_VTABLE_PROPERTY_CONST), |
671 | | SD_BUS_PROPERTY("WantedBy", "as", property_get_dependencies, offsetof(Unit, dependencies[UNIT_WANTED_BY]), SD_BUS_VTABLE_PROPERTY_CONST), |
672 | | SD_BUS_PROPERTY("BoundBy", "as", property_get_dependencies, offsetof(Unit, dependencies[UNIT_BOUND_BY]), SD_BUS_VTABLE_PROPERTY_CONST), |
673 | | SD_BUS_PROPERTY("ConsistsOf", "as", property_get_dependencies, offsetof(Unit, dependencies[UNIT_CONSISTS_OF]), SD_BUS_VTABLE_PROPERTY_CONST), |
674 | | SD_BUS_PROPERTY("Conflicts", "as", property_get_dependencies, offsetof(Unit, dependencies[UNIT_CONFLICTS]), SD_BUS_VTABLE_PROPERTY_CONST), |
675 | | SD_BUS_PROPERTY("ConflictedBy", "as", property_get_dependencies, offsetof(Unit, dependencies[UNIT_CONFLICTED_BY]), SD_BUS_VTABLE_PROPERTY_CONST), |
676 | | SD_BUS_PROPERTY("Before", "as", property_get_dependencies, offsetof(Unit, dependencies[UNIT_BEFORE]), SD_BUS_VTABLE_PROPERTY_CONST), |
677 | | SD_BUS_PROPERTY("After", "as", property_get_dependencies, offsetof(Unit, dependencies[UNIT_AFTER]), SD_BUS_VTABLE_PROPERTY_CONST), |
678 | | SD_BUS_PROPERTY("OnFailure", "as", property_get_dependencies, offsetof(Unit, dependencies[UNIT_ON_FAILURE]), SD_BUS_VTABLE_PROPERTY_CONST), |
679 | | SD_BUS_PROPERTY("Triggers", "as", property_get_dependencies, offsetof(Unit, dependencies[UNIT_TRIGGERS]), SD_BUS_VTABLE_PROPERTY_CONST), |
680 | | SD_BUS_PROPERTY("TriggeredBy", "as", property_get_dependencies, offsetof(Unit, dependencies[UNIT_TRIGGERED_BY]), SD_BUS_VTABLE_PROPERTY_CONST), |
681 | | SD_BUS_PROPERTY("PropagatesReloadTo", "as", property_get_dependencies, offsetof(Unit, dependencies[UNIT_PROPAGATES_RELOAD_TO]), SD_BUS_VTABLE_PROPERTY_CONST), |
682 | | SD_BUS_PROPERTY("ReloadPropagatedFrom", "as", property_get_dependencies, offsetof(Unit, dependencies[UNIT_RELOAD_PROPAGATED_FROM]), SD_BUS_VTABLE_PROPERTY_CONST), |
683 | | SD_BUS_PROPERTY("JoinsNamespaceOf", "as", property_get_dependencies, offsetof(Unit, dependencies[UNIT_JOINS_NAMESPACE_OF]), SD_BUS_VTABLE_PROPERTY_CONST), |
684 | | SD_BUS_PROPERTY("RequiresMountsFor", "as", property_get_requires_mounts_for, offsetof(Unit, requires_mounts_for), SD_BUS_VTABLE_PROPERTY_CONST), |
685 | | SD_BUS_PROPERTY("Documentation", "as", NULL, offsetof(Unit, documentation), SD_BUS_VTABLE_PROPERTY_CONST), |
686 | | SD_BUS_PROPERTY("Description", "s", property_get_description, 0, SD_BUS_VTABLE_PROPERTY_CONST), |
687 | | SD_BUS_PROPERTY("LoadState", "s", property_get_load_state, offsetof(Unit, load_state), SD_BUS_VTABLE_PROPERTY_CONST), |
688 | | SD_BUS_PROPERTY("ActiveState", "s", property_get_active_state, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE), |
689 | | SD_BUS_PROPERTY("SubState", "s", property_get_sub_state, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE), |
690 | | SD_BUS_PROPERTY("FragmentPath", "s", NULL, offsetof(Unit, fragment_path), SD_BUS_VTABLE_PROPERTY_CONST), |
691 | | SD_BUS_PROPERTY("SourcePath", "s", NULL, offsetof(Unit, source_path), SD_BUS_VTABLE_PROPERTY_CONST), |
692 | | SD_BUS_PROPERTY("DropInPaths", "as", NULL, offsetof(Unit, dropin_paths), SD_BUS_VTABLE_PROPERTY_CONST), |
693 | | SD_BUS_PROPERTY("UnitFileState", "s", property_get_unit_file_state, 0, 0), |
694 | | SD_BUS_PROPERTY("UnitFilePreset", "s", property_get_unit_file_preset, 0, 0), |
695 | | BUS_PROPERTY_DUAL_TIMESTAMP("StateChangeTimestamp", offsetof(Unit, state_change_timestamp), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE), |
696 | | BUS_PROPERTY_DUAL_TIMESTAMP("InactiveExitTimestamp", offsetof(Unit, inactive_exit_timestamp), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE), |
697 | | BUS_PROPERTY_DUAL_TIMESTAMP("ActiveEnterTimestamp", offsetof(Unit, active_enter_timestamp), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE), |
698 | | BUS_PROPERTY_DUAL_TIMESTAMP("ActiveExitTimestamp", offsetof(Unit, active_exit_timestamp), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE), |
699 | | BUS_PROPERTY_DUAL_TIMESTAMP("InactiveEnterTimestamp", offsetof(Unit, inactive_enter_timestamp), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE), |
700 | | SD_BUS_PROPERTY("CanStart", "b", property_get_can_start, 0, SD_BUS_VTABLE_PROPERTY_CONST), |
701 | | SD_BUS_PROPERTY("CanStop", "b", property_get_can_stop, 0, SD_BUS_VTABLE_PROPERTY_CONST), |
702 | | SD_BUS_PROPERTY("CanReload", "b", property_get_can_reload, 0, SD_BUS_VTABLE_PROPERTY_CONST), |
703 | | SD_BUS_PROPERTY("CanIsolate", "b", property_get_can_isolate, 0, SD_BUS_VTABLE_PROPERTY_CONST), |
704 | | SD_BUS_PROPERTY("Job", "(uo)", property_get_job, offsetof(Unit, job), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE), |
705 | | SD_BUS_PROPERTY("StopWhenUnneeded", "b", bus_property_get_bool, offsetof(Unit, stop_when_unneeded), SD_BUS_VTABLE_PROPERTY_CONST), |
706 | | SD_BUS_PROPERTY("RefuseManualStart", "b", bus_property_get_bool, offsetof(Unit, refuse_manual_start), SD_BUS_VTABLE_PROPERTY_CONST), |
707 | | SD_BUS_PROPERTY("RefuseManualStop", "b", bus_property_get_bool, offsetof(Unit, refuse_manual_stop), SD_BUS_VTABLE_PROPERTY_CONST), |
708 | | SD_BUS_PROPERTY("AllowIsolate", "b", bus_property_get_bool, offsetof(Unit, allow_isolate), SD_BUS_VTABLE_PROPERTY_CONST), |
709 | | SD_BUS_PROPERTY("DefaultDependencies", "b", bus_property_get_bool, offsetof(Unit, default_dependencies), SD_BUS_VTABLE_PROPERTY_CONST), |
710 | | SD_BUS_PROPERTY("OnFailureJobMode", "s", property_get_job_mode, offsetof(Unit, on_failure_job_mode), SD_BUS_VTABLE_PROPERTY_CONST), |
711 | | SD_BUS_PROPERTY("IgnoreOnIsolate", "b", bus_property_get_bool, offsetof(Unit, ignore_on_isolate), SD_BUS_VTABLE_PROPERTY_CONST), |
712 | | SD_BUS_PROPERTY("NeedDaemonReload", "b", property_get_need_daemon_reload, 0, SD_BUS_VTABLE_PROPERTY_CONST), |
713 | | SD_BUS_PROPERTY("JobTimeoutUSec", "t", bus_property_get_usec, offsetof(Unit, job_timeout), SD_BUS_VTABLE_PROPERTY_CONST), |
714 | | SD_BUS_PROPERTY("JobRunningTimeoutUSec", "t", bus_property_get_usec, offsetof(Unit, job_running_timeout), SD_BUS_VTABLE_PROPERTY_CONST), |
715 | | SD_BUS_PROPERTY("JobTimeoutAction", "s", property_get_emergency_action, offsetof(Unit, job_timeout_action), SD_BUS_VTABLE_PROPERTY_CONST), |
716 | | SD_BUS_PROPERTY("JobTimeoutRebootArgument", "s", NULL, offsetof(Unit, job_timeout_reboot_arg), SD_BUS_VTABLE_PROPERTY_CONST), |
717 | | SD_BUS_PROPERTY("ConditionResult", "b", bus_property_get_bool, offsetof(Unit, condition_result), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE), |
718 | | SD_BUS_PROPERTY("AssertResult", "b", bus_property_get_bool, offsetof(Unit, assert_result), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE), |
719 | | BUS_PROPERTY_DUAL_TIMESTAMP("ConditionTimestamp", offsetof(Unit, condition_timestamp), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE), |
720 | | BUS_PROPERTY_DUAL_TIMESTAMP("AssertTimestamp", offsetof(Unit, assert_timestamp), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE), |
721 | | SD_BUS_PROPERTY("Conditions", "a(sbbsi)", property_get_conditions, offsetof(Unit, conditions), SD_BUS_VTABLE_PROPERTY_EMITS_INVALIDATION), |
722 | | SD_BUS_PROPERTY("Asserts", "a(sbbsi)", property_get_conditions, offsetof(Unit, asserts), SD_BUS_VTABLE_PROPERTY_EMITS_INVALIDATION), |
723 | | SD_BUS_PROPERTY("LoadError", "(ss)", property_get_load_error, 0, SD_BUS_VTABLE_PROPERTY_CONST), |
724 | | SD_BUS_PROPERTY("Transient", "b", bus_property_get_bool, offsetof(Unit, transient), SD_BUS_VTABLE_PROPERTY_CONST), |
725 | | SD_BUS_PROPERTY("Perpetual", "b", bus_property_get_bool, offsetof(Unit, perpetual), SD_BUS_VTABLE_PROPERTY_CONST), |
726 | | SD_BUS_PROPERTY("StartLimitIntervalUSec", "t", bus_property_get_usec, offsetof(Unit, start_limit.interval), SD_BUS_VTABLE_PROPERTY_CONST), |
727 | | SD_BUS_PROPERTY("StartLimitBurst", "u", bus_property_get_unsigned, offsetof(Unit, start_limit.burst), SD_BUS_VTABLE_PROPERTY_CONST), |
728 | | SD_BUS_PROPERTY("StartLimitAction", "s", property_get_emergency_action, offsetof(Unit, start_limit_action), SD_BUS_VTABLE_PROPERTY_CONST), |
729 | | SD_BUS_PROPERTY("FailureAction", "s", property_get_emergency_action, offsetof(Unit, failure_action), SD_BUS_VTABLE_PROPERTY_CONST), |
730 | | SD_BUS_PROPERTY("FailureActionExitStatus", "i", bus_property_get_int, offsetof(Unit, failure_action_exit_status), SD_BUS_VTABLE_PROPERTY_CONST), |
731 | | SD_BUS_PROPERTY("SuccessAction", "s", property_get_emergency_action, offsetof(Unit, success_action), SD_BUS_VTABLE_PROPERTY_CONST), |
732 | | SD_BUS_PROPERTY("SuccessActionExitStatus", "i", bus_property_get_int, offsetof(Unit, success_action_exit_status), SD_BUS_VTABLE_PROPERTY_CONST), |
733 | | SD_BUS_PROPERTY("RebootArgument", "s", NULL, offsetof(Unit, reboot_arg), SD_BUS_VTABLE_PROPERTY_CONST), |
734 | | SD_BUS_PROPERTY("InvocationID", "ay", bus_property_get_id128, offsetof(Unit, invocation_id), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE), |
735 | | SD_BUS_PROPERTY("CollectMode", "s", property_get_collect_mode, offsetof(Unit, collect_mode), SD_BUS_VTABLE_PROPERTY_CONST), |
736 | | SD_BUS_PROPERTY("Refs", "as", property_get_refs, 0, 0), |
737 | | |
738 | | SD_BUS_METHOD("Start", "s", "o", method_start, SD_BUS_VTABLE_UNPRIVILEGED), |
739 | | SD_BUS_METHOD("Stop", "s", "o", method_stop, SD_BUS_VTABLE_UNPRIVILEGED), |
740 | | SD_BUS_METHOD("Reload", "s", "o", method_reload, SD_BUS_VTABLE_UNPRIVILEGED), |
741 | | SD_BUS_METHOD("Restart", "s", "o", method_restart, SD_BUS_VTABLE_UNPRIVILEGED), |
742 | | SD_BUS_METHOD("TryRestart", "s", "o", method_try_restart, SD_BUS_VTABLE_UNPRIVILEGED), |
743 | | SD_BUS_METHOD("ReloadOrRestart", "s", "o", method_reload_or_restart, SD_BUS_VTABLE_UNPRIVILEGED), |
744 | | SD_BUS_METHOD("ReloadOrTryRestart", "s", "o", method_reload_or_try_restart, SD_BUS_VTABLE_UNPRIVILEGED), |
745 | | SD_BUS_METHOD("EnqueueJob", "ss", "uososa(uosos)", bus_unit_method_enqueue_job, SD_BUS_VTABLE_UNPRIVILEGED), |
746 | | SD_BUS_METHOD("Kill", "si", NULL, bus_unit_method_kill, SD_BUS_VTABLE_UNPRIVILEGED), |
747 | | SD_BUS_METHOD("ResetFailed", NULL, NULL, bus_unit_method_reset_failed, SD_BUS_VTABLE_UNPRIVILEGED), |
748 | | SD_BUS_METHOD("SetProperties", "ba(sv)", NULL, bus_unit_method_set_properties, SD_BUS_VTABLE_UNPRIVILEGED), |
749 | | SD_BUS_METHOD("Ref", NULL, NULL, bus_unit_method_ref, SD_BUS_VTABLE_UNPRIVILEGED), |
750 | | SD_BUS_METHOD("Unref", NULL, NULL, bus_unit_method_unref, SD_BUS_VTABLE_UNPRIVILEGED), |
751 | | |
752 | | /* For dependency types we don't support anymore always return an empty array */ |
753 | | SD_BUS_PROPERTY("RequiresOverridable", "as", property_get_empty_strv, 0, SD_BUS_VTABLE_HIDDEN), |
754 | | SD_BUS_PROPERTY("RequisiteOverridable", "as", property_get_empty_strv, 0, SD_BUS_VTABLE_HIDDEN), |
755 | | SD_BUS_PROPERTY("RequiredByOverridable", "as", property_get_empty_strv, 0, SD_BUS_VTABLE_HIDDEN), |
756 | | SD_BUS_PROPERTY("RequisiteOfOverridable", "as", property_get_empty_strv, 0, SD_BUS_VTABLE_HIDDEN), |
757 | | /* Obsolete alias names */ |
758 | | SD_BUS_PROPERTY("StartLimitInterval", "t", bus_property_get_usec, offsetof(Unit, start_limit.interval), SD_BUS_VTABLE_PROPERTY_CONST|SD_BUS_VTABLE_HIDDEN), |
759 | | SD_BUS_PROPERTY("StartLimitIntervalSec", "t", bus_property_get_usec, offsetof(Unit, start_limit.interval), SD_BUS_VTABLE_PROPERTY_CONST|SD_BUS_VTABLE_HIDDEN), |
760 | | SD_BUS_VTABLE_END |
761 | | }; |
762 | | |
763 | | static int property_get_slice( |
764 | | sd_bus *bus, |
765 | | const char *path, |
766 | | const char *interface, |
767 | | const char *property, |
768 | | sd_bus_message *reply, |
769 | | void *userdata, |
770 | 0 | sd_bus_error *error) { |
771 | 0 |
|
772 | 0 | Unit *u = userdata; |
773 | 0 |
|
774 | 0 | assert(bus); |
775 | 0 | assert(reply); |
776 | 0 | assert(u); |
777 | 0 |
|
778 | 0 | return sd_bus_message_append(reply, "s", unit_slice_name(u)); |
779 | 0 | } |
780 | | |
781 | | static int property_get_current_memory( |
782 | | sd_bus *bus, |
783 | | const char *path, |
784 | | const char *interface, |
785 | | const char *property, |
786 | | sd_bus_message *reply, |
787 | | void *userdata, |
788 | 0 | sd_bus_error *error) { |
789 | 0 |
|
790 | 0 | uint64_t sz = (uint64_t) -1; |
791 | 0 | Unit *u = userdata; |
792 | 0 | int r; |
793 | 0 |
|
794 | 0 | assert(bus); |
795 | 0 | assert(reply); |
796 | 0 | assert(u); |
797 | 0 |
|
798 | 0 | r = unit_get_memory_current(u, &sz); |
799 | 0 | if (r < 0 && r != -ENODATA) |
800 | 0 | log_unit_warning_errno(u, r, "Failed to get memory.usage_in_bytes attribute: %m"); |
801 | 0 |
|
802 | 0 | return sd_bus_message_append(reply, "t", sz); |
803 | 0 | } |
804 | | |
805 | | static int property_get_current_tasks( |
806 | | sd_bus *bus, |
807 | | const char *path, |
808 | | const char *interface, |
809 | | const char *property, |
810 | | sd_bus_message *reply, |
811 | | void *userdata, |
812 | 0 | sd_bus_error *error) { |
813 | 0 |
|
814 | 0 | uint64_t cn = (uint64_t) -1; |
815 | 0 | Unit *u = userdata; |
816 | 0 | int r; |
817 | 0 |
|
818 | 0 | assert(bus); |
819 | 0 | assert(reply); |
820 | 0 | assert(u); |
821 | 0 |
|
822 | 0 | r = unit_get_tasks_current(u, &cn); |
823 | 0 | if (r < 0 && r != -ENODATA) |
824 | 0 | log_unit_warning_errno(u, r, "Failed to get pids.current attribute: %m"); |
825 | 0 |
|
826 | 0 | return sd_bus_message_append(reply, "t", cn); |
827 | 0 | } |
828 | | |
829 | | static int property_get_cpu_usage( |
830 | | sd_bus *bus, |
831 | | const char *path, |
832 | | const char *interface, |
833 | | const char *property, |
834 | | sd_bus_message *reply, |
835 | | void *userdata, |
836 | 0 | sd_bus_error *error) { |
837 | 0 |
|
838 | 0 | nsec_t ns = (nsec_t) -1; |
839 | 0 | Unit *u = userdata; |
840 | 0 | int r; |
841 | 0 |
|
842 | 0 | assert(bus); |
843 | 0 | assert(reply); |
844 | 0 | assert(u); |
845 | 0 |
|
846 | 0 | r = unit_get_cpu_usage(u, &ns); |
847 | 0 | if (r < 0 && r != -ENODATA) |
848 | 0 | log_unit_warning_errno(u, r, "Failed to get cpuacct.usage attribute: %m"); |
849 | 0 |
|
850 | 0 | return sd_bus_message_append(reply, "t", ns); |
851 | 0 | } |
852 | | |
853 | | static int property_get_cgroup( |
854 | | sd_bus *bus, |
855 | | const char *path, |
856 | | const char *interface, |
857 | | const char *property, |
858 | | sd_bus_message *reply, |
859 | | void *userdata, |
860 | 0 | sd_bus_error *error) { |
861 | 0 |
|
862 | 0 | Unit *u = userdata; |
863 | 0 | const char *t = NULL; |
864 | 0 |
|
865 | 0 | assert(bus); |
866 | 0 | assert(reply); |
867 | 0 | assert(u); |
868 | 0 |
|
869 | 0 | /* Three cases: a) u->cgroup_path is NULL, in which case the |
870 | 0 | * unit has no control group, which we report as the empty |
871 | 0 | * string. b) u->cgroup_path is the empty string, which |
872 | 0 | * indicates the root cgroup, which we report as "/". c) all |
873 | 0 | * other cases we report as-is. */ |
874 | 0 |
|
875 | 0 | if (u->cgroup_path) |
876 | 0 | t = empty_to_root(u->cgroup_path); |
877 | 0 |
|
878 | 0 | return sd_bus_message_append(reply, "s", t); |
879 | 0 | } |
880 | | |
881 | 0 | static int append_process(sd_bus_message *reply, const char *p, pid_t pid, Set *pids) { |
882 | 0 | _cleanup_free_ char *buf = NULL, *cmdline = NULL; |
883 | 0 | int r; |
884 | 0 |
|
885 | 0 | assert(reply); |
886 | 0 | assert(pid > 0); |
887 | 0 |
|
888 | 0 | r = set_put(pids, PID_TO_PTR(pid)); |
889 | 0 | if (IN_SET(r, 0, -EEXIST)) |
890 | 0 | return 0; |
891 | 0 | if (r < 0) |
892 | 0 | return r; |
893 | 0 | |
894 | 0 | if (!p) { |
895 | 0 | r = cg_pid_get_path(SYSTEMD_CGROUP_CONTROLLER, pid, &buf); |
896 | 0 | if (r == -ESRCH) |
897 | 0 | return 0; |
898 | 0 | if (r < 0) |
899 | 0 | return r; |
900 | 0 | |
901 | 0 | p = buf; |
902 | 0 | } |
903 | 0 |
|
904 | 0 | (void) get_process_cmdline(pid, SIZE_MAX, PROCESS_CMDLINE_COMM_FALLBACK, &cmdline); |
905 | 0 |
|
906 | 0 | return sd_bus_message_append(reply, |
907 | 0 | "(sus)", |
908 | 0 | p, |
909 | 0 | (uint32_t) pid, |
910 | 0 | cmdline); |
911 | 0 | } |
912 | | |
913 | 0 | static int append_cgroup(sd_bus_message *reply, const char *p, Set *pids) { |
914 | 0 | _cleanup_closedir_ DIR *d = NULL; |
915 | 0 | _cleanup_fclose_ FILE *f = NULL; |
916 | 0 | int r; |
917 | 0 |
|
918 | 0 | assert(reply); |
919 | 0 | assert(p); |
920 | 0 |
|
921 | 0 | r = cg_enumerate_processes(SYSTEMD_CGROUP_CONTROLLER, p, &f); |
922 | 0 | if (r == -ENOENT) |
923 | 0 | return 0; |
924 | 0 | if (r < 0) |
925 | 0 | return r; |
926 | 0 | |
927 | 0 | for (;;) { |
928 | 0 | pid_t pid; |
929 | 0 |
|
930 | 0 | r = cg_read_pid(f, &pid); |
931 | 0 | if (r < 0) |
932 | 0 | return r; |
933 | 0 | if (r == 0) |
934 | 0 | break; |
935 | 0 | |
936 | 0 | if (is_kernel_thread(pid) > 0) |
937 | 0 | continue; |
938 | 0 | |
939 | 0 | r = append_process(reply, p, pid, pids); |
940 | 0 | if (r < 0) |
941 | 0 | return r; |
942 | 0 | } |
943 | 0 |
|
944 | 0 | r = cg_enumerate_subgroups(SYSTEMD_CGROUP_CONTROLLER, p, &d); |
945 | 0 | if (r == -ENOENT) |
946 | 0 | return 0; |
947 | 0 | if (r < 0) |
948 | 0 | return r; |
949 | 0 | |
950 | 0 | for (;;) { |
951 | 0 | _cleanup_free_ char *g = NULL, *j = NULL; |
952 | 0 |
|
953 | 0 | r = cg_read_subgroup(d, &g); |
954 | 0 | if (r < 0) |
955 | 0 | return r; |
956 | 0 | if (r == 0) |
957 | 0 | break; |
958 | 0 | |
959 | 0 | j = strjoin(p, "/", g); |
960 | 0 | if (!j) |
961 | 0 | return -ENOMEM; |
962 | 0 | |
963 | 0 | r = append_cgroup(reply, j, pids); |
964 | 0 | if (r < 0) |
965 | 0 | return r; |
966 | 0 | } |
967 | 0 |
|
968 | 0 | return 0; |
969 | 0 | } |
970 | | |
971 | 0 | int bus_unit_method_get_processes(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
972 | 0 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; |
973 | 0 | _cleanup_set_free_ Set *pids = NULL; |
974 | 0 | Unit *u = userdata; |
975 | 0 | pid_t pid; |
976 | 0 | int r; |
977 | 0 |
|
978 | 0 | assert(message); |
979 | 0 |
|
980 | 0 | r = mac_selinux_unit_access_check(u, message, "status", error); |
981 | 0 | if (r < 0) |
982 | 0 | return r; |
983 | 0 | |
984 | 0 | pids = set_new(NULL); |
985 | 0 | if (!pids) |
986 | 0 | return -ENOMEM; |
987 | 0 | |
988 | 0 | r = sd_bus_message_new_method_return(message, &reply); |
989 | 0 | if (r < 0) |
990 | 0 | return r; |
991 | 0 | |
992 | 0 | r = sd_bus_message_open_container(reply, 'a', "(sus)"); |
993 | 0 | if (r < 0) |
994 | 0 | return r; |
995 | 0 | |
996 | 0 | if (u->cgroup_path) { |
997 | 0 | r = append_cgroup(reply, u->cgroup_path, pids); |
998 | 0 | if (r < 0) |
999 | 0 | return r; |
1000 | 0 | } |
1001 | 0 | |
1002 | 0 | /* The main and control pids might live outside of the cgroup, hence fetch them separately */ |
1003 | 0 | pid = unit_main_pid(u); |
1004 | 0 | if (pid > 0) { |
1005 | 0 | r = append_process(reply, NULL, pid, pids); |
1006 | 0 | if (r < 0) |
1007 | 0 | return r; |
1008 | 0 | } |
1009 | 0 | |
1010 | 0 | pid = unit_control_pid(u); |
1011 | 0 | if (pid > 0) { |
1012 | 0 | r = append_process(reply, NULL, pid, pids); |
1013 | 0 | if (r < 0) |
1014 | 0 | return r; |
1015 | 0 | } |
1016 | 0 | |
1017 | 0 | r = sd_bus_message_close_container(reply); |
1018 | 0 | if (r < 0) |
1019 | 0 | return r; |
1020 | 0 | |
1021 | 0 | return sd_bus_send(NULL, reply, NULL); |
1022 | 0 | } |
1023 | | |
1024 | | static int property_get_ip_counter( |
1025 | | sd_bus *bus, |
1026 | | const char *path, |
1027 | | const char *interface, |
1028 | | const char *property, |
1029 | | sd_bus_message *reply, |
1030 | | void *userdata, |
1031 | 0 | sd_bus_error *error) { |
1032 | 0 |
|
1033 | 0 | static const char *const table[_CGROUP_IP_ACCOUNTING_METRIC_MAX] = { |
1034 | 0 | [CGROUP_IP_INGRESS_BYTES] = "IPIngressBytes", |
1035 | 0 | [CGROUP_IP_EGRESS_BYTES] = "IPEgressBytes", |
1036 | 0 | [CGROUP_IP_INGRESS_PACKETS] = "IPIngressPackets", |
1037 | 0 | [CGROUP_IP_EGRESS_PACKETS] = "IPEgressPackets", |
1038 | 0 | }; |
1039 | 0 |
|
1040 | 0 | uint64_t value = UINT64_MAX; |
1041 | 0 | Unit *u = userdata; |
1042 | 0 | ssize_t metric; |
1043 | 0 |
|
1044 | 0 | assert(bus); |
1045 | 0 | assert(reply); |
1046 | 0 | assert(property); |
1047 | 0 | assert(u); |
1048 | 0 |
|
1049 | 0 | assert_se((metric = string_table_lookup(table, ELEMENTSOF(table), property)) >= 0); |
1050 | 0 | (void) unit_get_ip_accounting(u, metric, &value); |
1051 | 0 | return sd_bus_message_append(reply, "t", value); |
1052 | 0 | } |
1053 | | |
1054 | | static int property_get_io_counter( |
1055 | | sd_bus *bus, |
1056 | | const char *path, |
1057 | | const char *interface, |
1058 | | const char *property, |
1059 | | sd_bus_message *reply, |
1060 | | void *userdata, |
1061 | 0 | sd_bus_error *error) { |
1062 | 0 |
|
1063 | 0 | static const char *const table[_CGROUP_IO_ACCOUNTING_METRIC_MAX] = { |
1064 | 0 | [CGROUP_IO_READ_BYTES] = "IOReadBytes", |
1065 | 0 | [CGROUP_IO_WRITE_BYTES] = "IOWriteBytes", |
1066 | 0 | [CGROUP_IO_READ_OPERATIONS] = "IOReadOperations", |
1067 | 0 | [CGROUP_IO_WRITE_OPERATIONS] = "IOWriteOperations", |
1068 | 0 | }; |
1069 | 0 |
|
1070 | 0 | uint64_t value = UINT64_MAX; |
1071 | 0 | Unit *u = userdata; |
1072 | 0 | ssize_t metric; |
1073 | 0 |
|
1074 | 0 | assert(bus); |
1075 | 0 | assert(reply); |
1076 | 0 | assert(property); |
1077 | 0 | assert(u); |
1078 | 0 |
|
1079 | 0 | assert_se((metric = string_table_lookup(table, ELEMENTSOF(table), property)) >= 0); |
1080 | 0 | (void) unit_get_io_accounting(u, metric, false, &value); |
1081 | 0 | return sd_bus_message_append(reply, "t", value); |
1082 | 0 | } |
1083 | | |
1084 | 0 | int bus_unit_method_attach_processes(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
1085 | 0 |
|
1086 | 0 | _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL; |
1087 | 0 | _cleanup_set_free_ Set *pids = NULL; |
1088 | 0 | Unit *u = userdata; |
1089 | 0 | const char *path; |
1090 | 0 | int r; |
1091 | 0 |
|
1092 | 0 | assert(message); |
1093 | 0 |
|
1094 | 0 | /* This migrates the processes with the specified PIDs into the cgroup of this unit, optionally below a |
1095 | 0 | * specified cgroup path. Obviously this only works for units that actually maintain a cgroup |
1096 | 0 | * representation. If a process is already in the cgroup no operation is executed – in this case the specified |
1097 | 0 | * subcgroup path has no effect! */ |
1098 | 0 |
|
1099 | 0 | r = mac_selinux_unit_access_check(u, message, "start", error); |
1100 | 0 | if (r < 0) |
1101 | 0 | return r; |
1102 | 0 | |
1103 | 0 | r = sd_bus_message_read(message, "s", &path); |
1104 | 0 | if (r < 0) |
1105 | 0 | return r; |
1106 | 0 | |
1107 | 0 | path = empty_to_null(path); |
1108 | 0 | if (path) { |
1109 | 0 | if (!path_is_absolute(path)) |
1110 | 0 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Control group path is not absolute: %s", path); |
1111 | 0 |
|
1112 | 0 | if (!path_is_normalized(path)) |
1113 | 0 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Control group path is not normalized: %s", path); |
1114 | 0 | } |
1115 | 0 |
|
1116 | 0 | if (!unit_cgroup_delegate(u)) |
1117 | 0 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Process migration not available on non-delegated units."); |
1118 | 0 |
|
1119 | 0 | if (UNIT_IS_INACTIVE_OR_FAILED(unit_active_state(u))) |
1120 | 0 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Unit is not active, refusing."); |
1121 | 0 |
|
1122 | 0 | r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_EUID|SD_BUS_CREDS_PID, &creds); |
1123 | 0 | if (r < 0) |
1124 | 0 | return r; |
1125 | 0 | |
1126 | 0 | r = sd_bus_message_enter_container(message, 'a', "u"); |
1127 | 0 | if (r < 0) |
1128 | 0 | return r; |
1129 | 0 | for (;;) { |
1130 | 0 | uid_t process_uid, sender_uid; |
1131 | 0 | uint32_t upid; |
1132 | 0 | pid_t pid; |
1133 | 0 |
|
1134 | 0 | r = sd_bus_message_read(message, "u", &upid); |
1135 | 0 | if (r < 0) |
1136 | 0 | return r; |
1137 | 0 | if (r == 0) |
1138 | 0 | break; |
1139 | 0 | |
1140 | 0 | if (upid == 0) { |
1141 | 0 | r = sd_bus_creds_get_pid(creds, &pid); |
1142 | 0 | if (r < 0) |
1143 | 0 | return r; |
1144 | 0 | } else |
1145 | 0 | pid = (uid_t) upid; |
1146 | 0 |
|
1147 | 0 | /* Filter out duplicates */ |
1148 | 0 | if (set_contains(pids, PID_TO_PTR(pid))) |
1149 | 0 | continue; |
1150 | 0 | |
1151 | 0 | /* Check if this process is suitable for attaching to this unit */ |
1152 | 0 | r = unit_pid_attachable(u, pid, error); |
1153 | 0 | if (r < 0) |
1154 | 0 | return r; |
1155 | 0 | |
1156 | 0 | /* Let's query the sender's UID, so that we can make our security decisions */ |
1157 | 0 | r = sd_bus_creds_get_euid(creds, &sender_uid); |
1158 | 0 | if (r < 0) |
1159 | 0 | return r; |
1160 | 0 | |
1161 | 0 | /* Let's validate security: if the sender is root, then all is OK. If the sender is any other unit, |
1162 | 0 | * then the process' UID and the target unit's UID have to match the sender's UID */ |
1163 | 0 | if (sender_uid != 0 && sender_uid != getuid()) { |
1164 | 0 | r = get_process_uid(pid, &process_uid); |
1165 | 0 | if (r < 0) |
1166 | 0 | return sd_bus_error_set_errnof(error, r, "Failed to retrieve process UID: %m"); |
1167 | 0 | |
1168 | 0 | if (process_uid != sender_uid) |
1169 | 0 | return sd_bus_error_setf(error, SD_BUS_ERROR_ACCESS_DENIED, "Process " PID_FMT " not owned by client's UID. Refusing.", pid); |
1170 | 0 | if (process_uid != u->ref_uid) |
1171 | 0 | return sd_bus_error_setf(error, SD_BUS_ERROR_ACCESS_DENIED, "Process " PID_FMT " not owned by target unit's UID. Refusing.", pid); |
1172 | 0 | } |
1173 | 0 |
|
1174 | 0 | if (!pids) { |
1175 | 0 | pids = set_new(NULL); |
1176 | 0 | if (!pids) |
1177 | 0 | return -ENOMEM; |
1178 | 0 | } |
1179 | 0 | |
1180 | 0 | r = set_put(pids, PID_TO_PTR(pid)); |
1181 | 0 | if (r < 0) |
1182 | 0 | return r; |
1183 | 0 | } |
1184 | 0 |
|
1185 | 0 | r = sd_bus_message_exit_container(message); |
1186 | 0 | if (r < 0) |
1187 | 0 | return r; |
1188 | 0 | |
1189 | 0 | r = unit_attach_pids_to_cgroup(u, pids, path); |
1190 | 0 | if (r < 0) |
1191 | 0 | return sd_bus_error_set_errnof(error, r, "Failed to attach processes to control group: %m"); |
1192 | 0 | |
1193 | 0 | return sd_bus_reply_method_return(message, NULL); |
1194 | 0 | } |
1195 | | |
1196 | | const sd_bus_vtable bus_unit_cgroup_vtable[] = { |
1197 | | SD_BUS_VTABLE_START(0), |
1198 | | SD_BUS_PROPERTY("Slice", "s", property_get_slice, 0, 0), |
1199 | | SD_BUS_PROPERTY("ControlGroup", "s", property_get_cgroup, 0, 0), |
1200 | | SD_BUS_PROPERTY("MemoryCurrent", "t", property_get_current_memory, 0, 0), |
1201 | | SD_BUS_PROPERTY("CPUUsageNSec", "t", property_get_cpu_usage, 0, 0), |
1202 | | SD_BUS_PROPERTY("TasksCurrent", "t", property_get_current_tasks, 0, 0), |
1203 | | SD_BUS_PROPERTY("IPIngressBytes", "t", property_get_ip_counter, 0, 0), |
1204 | | SD_BUS_PROPERTY("IPIngressPackets", "t", property_get_ip_counter, 0, 0), |
1205 | | SD_BUS_PROPERTY("IPEgressBytes", "t", property_get_ip_counter, 0, 0), |
1206 | | SD_BUS_PROPERTY("IPEgressPackets", "t", property_get_ip_counter, 0, 0), |
1207 | | SD_BUS_PROPERTY("IOReadBytes", "t", property_get_io_counter, 0, 0), |
1208 | | SD_BUS_PROPERTY("IOReadOperations", "t", property_get_io_counter, 0, 0), |
1209 | | SD_BUS_PROPERTY("IOWriteBytes", "t", property_get_io_counter, 0, 0), |
1210 | | SD_BUS_PROPERTY("IOWriteOperations", "t", property_get_io_counter, 0, 0), |
1211 | | SD_BUS_METHOD("GetProcesses", NULL, "a(sus)", bus_unit_method_get_processes, SD_BUS_VTABLE_UNPRIVILEGED), |
1212 | | SD_BUS_METHOD("AttachProcesses", "sau", NULL, bus_unit_method_attach_processes, SD_BUS_VTABLE_UNPRIVILEGED), |
1213 | | SD_BUS_VTABLE_END |
1214 | | }; |
1215 | | |
1216 | 0 | static int send_new_signal(sd_bus *bus, void *userdata) { |
1217 | 0 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL; |
1218 | 0 | _cleanup_free_ char *p = NULL; |
1219 | 0 | Unit *u = userdata; |
1220 | 0 | int r; |
1221 | 0 |
|
1222 | 0 | assert(bus); |
1223 | 0 | assert(u); |
1224 | 0 |
|
1225 | 0 | p = unit_dbus_path(u); |
1226 | 0 | if (!p) |
1227 | 0 | return -ENOMEM; |
1228 | 0 | |
1229 | 0 | r = sd_bus_message_new_signal( |
1230 | 0 | bus, |
1231 | 0 | &m, |
1232 | 0 | "/org/freedesktop/systemd1", |
1233 | 0 | "org.freedesktop.systemd1.Manager", |
1234 | 0 | "UnitNew"); |
1235 | 0 | if (r < 0) |
1236 | 0 | return r; |
1237 | 0 | |
1238 | 0 | r = sd_bus_message_append(m, "so", u->id, p); |
1239 | 0 | if (r < 0) |
1240 | 0 | return r; |
1241 | 0 | |
1242 | 0 | return sd_bus_send(bus, m, NULL); |
1243 | 0 | } |
1244 | | |
1245 | 0 | static int send_changed_signal(sd_bus *bus, void *userdata) { |
1246 | 0 | _cleanup_free_ char *p = NULL; |
1247 | 0 | Unit *u = userdata; |
1248 | 0 | int r; |
1249 | 0 |
|
1250 | 0 | assert(bus); |
1251 | 0 | assert(u); |
1252 | 0 |
|
1253 | 0 | p = unit_dbus_path(u); |
1254 | 0 | if (!p) |
1255 | 0 | return -ENOMEM; |
1256 | 0 | |
1257 | 0 | /* Send a properties changed signal. First for the specific |
1258 | 0 | * type, then for the generic unit. The clients may rely on |
1259 | 0 | * this order to get atomic behavior if needed. */ |
1260 | 0 | |
1261 | 0 | r = sd_bus_emit_properties_changed_strv( |
1262 | 0 | bus, p, |
1263 | 0 | unit_dbus_interface_from_type(u->type), |
1264 | 0 | NULL); |
1265 | 0 | if (r < 0) |
1266 | 0 | return r; |
1267 | 0 | |
1268 | 0 | return sd_bus_emit_properties_changed_strv( |
1269 | 0 | bus, p, |
1270 | 0 | "org.freedesktop.systemd1.Unit", |
1271 | 0 | NULL); |
1272 | 0 | } |
1273 | | |
1274 | 19.5k | void bus_unit_send_change_signal(Unit *u) { |
1275 | 19.5k | int r; |
1276 | 19.5k | assert(u); |
1277 | 19.5k | |
1278 | 19.5k | if (u->in_dbus_queue) { |
1279 | 0 | LIST_REMOVE(dbus_queue, u->manager->dbus_unit_queue, u); |
1280 | 0 | u->in_dbus_queue = false; |
1281 | 0 | } |
1282 | 19.5k | |
1283 | 19.5k | if (!u->id) |
1284 | 1.53k | return; |
1285 | 18.0k | |
1286 | 18.0k | r = bus_foreach_bus(u->manager, u->bus_track, u->sent_dbus_new_signal ? send_changed_signal : send_new_signal, u); |
1287 | 18.0k | if (r < 0) |
1288 | 18.0k | log_unit_debug_errno(u, r, "Failed to send unit change signal for %s: %m", u->id); |
1289 | 18.0k | |
1290 | 18.0k | u->sent_dbus_new_signal = true; |
1291 | 18.0k | } |
1292 | | |
1293 | 0 | void bus_unit_send_pending_change_signal(Unit *u, bool including_new) { |
1294 | 0 |
|
1295 | 0 | /* Sends out any pending change signals, but only if they really are pending. This call is used when we are |
1296 | 0 | * about to change state in order to force out a PropertiesChanged signal beforehand if there was one pending |
1297 | 0 | * so that clients can follow the full state transition */ |
1298 | 0 |
|
1299 | 0 | if (!u->in_dbus_queue) /* If not enqueued, don't bother */ |
1300 | 0 | return; |
1301 | 0 | |
1302 | 0 | if (!u->sent_dbus_new_signal && !including_new) /* If the unit was never announced, don't bother, it's fine if |
1303 | 0 | * the unit appears in the new state right-away (except if the |
1304 | 0 | * caller explicitly asked us to send it anyway) */ |
1305 | 0 | return; |
1306 | 0 | |
1307 | 0 | if (MANAGER_IS_RELOADING(u->manager)) /* Don't generate unnecessary PropertiesChanged signals for the same unit |
1308 | 0 | * when we are reloading. */ |
1309 | 0 | return; |
1310 | 0 | |
1311 | 0 | bus_unit_send_change_signal(u); |
1312 | 0 | } |
1313 | | |
1314 | 0 | static int send_removed_signal(sd_bus *bus, void *userdata) { |
1315 | 0 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL; |
1316 | 0 | _cleanup_free_ char *p = NULL; |
1317 | 0 | Unit *u = userdata; |
1318 | 0 | int r; |
1319 | 0 |
|
1320 | 0 | assert(bus); |
1321 | 0 | assert(u); |
1322 | 0 |
|
1323 | 0 | p = unit_dbus_path(u); |
1324 | 0 | if (!p) |
1325 | 0 | return -ENOMEM; |
1326 | 0 | |
1327 | 0 | r = sd_bus_message_new_signal( |
1328 | 0 | bus, |
1329 | 0 | &m, |
1330 | 0 | "/org/freedesktop/systemd1", |
1331 | 0 | "org.freedesktop.systemd1.Manager", |
1332 | 0 | "UnitRemoved"); |
1333 | 0 | if (r < 0) |
1334 | 0 | return r; |
1335 | 0 | |
1336 | 0 | r = sd_bus_message_append(m, "so", u->id, p); |
1337 | 0 | if (r < 0) |
1338 | 0 | return r; |
1339 | 0 | |
1340 | 0 | return sd_bus_send(bus, m, NULL); |
1341 | 0 | } |
1342 | | |
1343 | 36.8k | void bus_unit_send_removed_signal(Unit *u) { |
1344 | 36.8k | int r; |
1345 | 36.8k | assert(u); |
1346 | 36.8k | |
1347 | 36.8k | if (!u->sent_dbus_new_signal || u->in_dbus_queue) |
1348 | 19.5k | bus_unit_send_change_signal(u); |
1349 | 36.8k | |
1350 | 36.8k | if (!u->id) |
1351 | 1.53k | return; |
1352 | 35.2k | |
1353 | 35.2k | r = bus_foreach_bus(u->manager, u->bus_track, send_removed_signal, u); |
1354 | 35.2k | if (r < 0) |
1355 | 35.2k | log_unit_debug_errno(u, r, "Failed to send unit remove signal for %s: %m", u->id); |
1356 | 35.2k | } |
1357 | | |
1358 | | int bus_unit_queue_job( |
1359 | | sd_bus_message *message, |
1360 | | Unit *u, |
1361 | | JobType type, |
1362 | | JobMode mode, |
1363 | | BusUnitQueueFlags flags, |
1364 | 0 | sd_bus_error *error) { |
1365 | 0 |
|
1366 | 0 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; |
1367 | 0 | _cleanup_free_ char *job_path = NULL, *unit_path = NULL; |
1368 | 0 | _cleanup_(set_freep) Set *affected = NULL; |
1369 | 0 | Iterator i; |
1370 | 0 | Job *j, *a; |
1371 | 0 | int r; |
1372 | 0 |
|
1373 | 0 | assert(message); |
1374 | 0 | assert(u); |
1375 | 0 | assert(type >= 0 && type < _JOB_TYPE_MAX); |
1376 | 0 | assert(mode >= 0 && mode < _JOB_MODE_MAX); |
1377 | 0 |
|
1378 | 0 | r = mac_selinux_unit_access_check( |
1379 | 0 | u, message, |
1380 | 0 | job_type_to_access_method(type), |
1381 | 0 | error); |
1382 | 0 | if (r < 0) |
1383 | 0 | return r; |
1384 | 0 | |
1385 | 0 | if (FLAGS_SET(flags, BUS_UNIT_QUEUE_RELOAD_IF_POSSIBLE) && unit_can_reload(u)) { |
1386 | 0 | if (type == JOB_RESTART) |
1387 | 0 | type = JOB_RELOAD_OR_START; |
1388 | 0 | else if (type == JOB_TRY_RESTART) |
1389 | 0 | type = JOB_TRY_RELOAD; |
1390 | 0 | } |
1391 | 0 |
|
1392 | 0 | if (type == JOB_STOP && |
1393 | 0 | IN_SET(u->load_state, UNIT_NOT_FOUND, UNIT_ERROR, UNIT_BAD_SETTING) && |
1394 | 0 | unit_active_state(u) == UNIT_INACTIVE) |
1395 | 0 | return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_UNIT, "Unit %s not loaded.", u->id); |
1396 | 0 |
|
1397 | 0 | if ((type == JOB_START && u->refuse_manual_start) || |
1398 | 0 | (type == JOB_STOP && u->refuse_manual_stop) || |
1399 | 0 | (IN_SET(type, JOB_RESTART, JOB_TRY_RESTART) && (u->refuse_manual_start || u->refuse_manual_stop)) || |
1400 | 0 | (type == JOB_RELOAD_OR_START && job_type_collapse(type, u) == JOB_START && u->refuse_manual_start)) |
1401 | 0 | return sd_bus_error_setf(error, BUS_ERROR_ONLY_BY_DEPENDENCY, "Operation refused, unit %s may be requested by dependency only (it is configured to refuse manual start/stop).", u->id); |
1402 | 0 |
|
1403 | 0 | if (FLAGS_SET(flags, BUS_UNIT_QUEUE_VERBOSE_REPLY)) { |
1404 | 0 | affected = set_new(NULL); |
1405 | 0 | if (!affected) |
1406 | 0 | return -ENOMEM; |
1407 | 0 | } |
1408 | 0 | |
1409 | 0 | r = manager_add_job(u->manager, type, u, mode, affected, error, &j); |
1410 | 0 | if (r < 0) |
1411 | 0 | return r; |
1412 | 0 | |
1413 | 0 | r = bus_job_track_sender(j, message); |
1414 | 0 | if (r < 0) |
1415 | 0 | return r; |
1416 | 0 | |
1417 | 0 | /* Before we send the method reply, force out the announcement JobNew for this job */ |
1418 | 0 | bus_job_send_pending_change_signal(j, true); |
1419 | 0 |
|
1420 | 0 | job_path = job_dbus_path(j); |
1421 | 0 | if (!job_path) |
1422 | 0 | return -ENOMEM; |
1423 | 0 | |
1424 | 0 | /* The classic response is just a job object path */ |
1425 | 0 | if (!FLAGS_SET(flags, BUS_UNIT_QUEUE_VERBOSE_REPLY)) |
1426 | 0 | return sd_bus_reply_method_return(message, "o", job_path); |
1427 | 0 | |
1428 | 0 | /* In verbose mode respond with the anchor job plus everything that has been affected */ |
1429 | 0 | r = sd_bus_message_new_method_return(message, &reply); |
1430 | 0 | if (r < 0) |
1431 | 0 | return r; |
1432 | 0 | |
1433 | 0 | unit_path = unit_dbus_path(j->unit); |
1434 | 0 | if (!unit_path) |
1435 | 0 | return -ENOMEM; |
1436 | 0 | |
1437 | 0 | r = sd_bus_message_append(reply, "uosos", |
1438 | 0 | j->id, job_path, |
1439 | 0 | j->unit->id, unit_path, |
1440 | 0 | job_type_to_string(j->type)); |
1441 | 0 | if (r < 0) |
1442 | 0 | return r; |
1443 | 0 | |
1444 | 0 | r = sd_bus_message_open_container(reply, 'a', "(uosos)"); |
1445 | 0 | if (r < 0) |
1446 | 0 | return r; |
1447 | 0 | |
1448 | 0 | SET_FOREACH(a, affected, i) { |
1449 | 0 |
|
1450 | 0 | if (a->id == j->id) |
1451 | 0 | continue; |
1452 | 0 | |
1453 | 0 | /* Free paths from previous iteration */ |
1454 | 0 | job_path = mfree(job_path); |
1455 | 0 | unit_path = mfree(unit_path); |
1456 | 0 |
|
1457 | 0 | job_path = job_dbus_path(a); |
1458 | 0 | if (!job_path) |
1459 | 0 | return -ENOMEM; |
1460 | 0 | |
1461 | 0 | unit_path = unit_dbus_path(a->unit); |
1462 | 0 | if (!unit_path) |
1463 | 0 | return -ENOMEM; |
1464 | 0 | |
1465 | 0 | r = sd_bus_message_append(reply, "(uosos)", |
1466 | 0 | a->id, job_path, |
1467 | 0 | a->unit->id, unit_path, |
1468 | 0 | job_type_to_string(a->type)); |
1469 | 0 | if (r < 0) |
1470 | 0 | return r; |
1471 | 0 | } |
1472 | 0 |
|
1473 | 0 | r = sd_bus_message_close_container(reply); |
1474 | 0 | if (r < 0) |
1475 | 0 | return r; |
1476 | 0 | |
1477 | 0 | return sd_bus_send(NULL, reply, NULL); |
1478 | 0 | } |
1479 | | |
1480 | | static int bus_unit_set_live_property( |
1481 | | Unit *u, |
1482 | | const char *name, |
1483 | | sd_bus_message *message, |
1484 | | UnitWriteFlags flags, |
1485 | 0 | sd_bus_error *error) { |
1486 | 0 |
|
1487 | 0 | int r; |
1488 | 0 |
|
1489 | 0 | assert(u); |
1490 | 0 | assert(name); |
1491 | 0 | assert(message); |
1492 | 0 |
|
1493 | 0 | /* Handles setting properties both "live" (i.e. at any time during runtime), and during creation (for transient |
1494 | 0 | * units that are being created). */ |
1495 | 0 |
|
1496 | 0 | if (streq(name, "Description")) { |
1497 | 0 | const char *d; |
1498 | 0 |
|
1499 | 0 | r = sd_bus_message_read(message, "s", &d); |
1500 | 0 | if (r < 0) |
1501 | 0 | return r; |
1502 | 0 | |
1503 | 0 | if (!UNIT_WRITE_FLAGS_NOOP(flags)) { |
1504 | 0 | r = unit_set_description(u, d); |
1505 | 0 | if (r < 0) |
1506 | 0 | return r; |
1507 | 0 | |
1508 | 0 | unit_write_settingf(u, flags|UNIT_ESCAPE_SPECIFIERS, name, "Description=%s", d); |
1509 | 0 | } |
1510 | 0 |
|
1511 | 0 | return 1; |
1512 | 0 | } |
1513 | 0 | |
1514 | 0 | return 0; |
1515 | 0 | } |
1516 | | |
1517 | | static int bus_set_transient_emergency_action( |
1518 | | Unit *u, |
1519 | | const char *name, |
1520 | | EmergencyAction *p, |
1521 | | sd_bus_message *message, |
1522 | | UnitWriteFlags flags, |
1523 | 0 | sd_bus_error *error) { |
1524 | 0 |
|
1525 | 0 | const char *s; |
1526 | 0 | EmergencyAction v; |
1527 | 0 | int r; |
1528 | 0 | bool system; |
1529 | 0 |
|
1530 | 0 | assert(p); |
1531 | 0 |
|
1532 | 0 | r = sd_bus_message_read(message, "s", &s); |
1533 | 0 | if (r < 0) |
1534 | 0 | return r; |
1535 | 0 | |
1536 | 0 | system = MANAGER_IS_SYSTEM(u->manager); |
1537 | 0 | r = parse_emergency_action(s, system, &v); |
1538 | 0 | if (r < 0) |
1539 | 0 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, |
1540 | 0 | r == -EOPNOTSUPP ? "%s setting invalid for manager type: %s" |
1541 | 0 | : "Invalid %s setting: %s", |
1542 | 0 | name, s); |
1543 | 0 |
|
1544 | 0 | if (!UNIT_WRITE_FLAGS_NOOP(flags)) { |
1545 | 0 | *p = v; |
1546 | 0 | unit_write_settingf(u, flags, name, |
1547 | 0 | "%s=%s", name, s); |
1548 | 0 | } |
1549 | 0 |
|
1550 | 0 | return 1; |
1551 | 0 | } |
1552 | | |
1553 | | static int bus_set_transient_exit_status( |
1554 | | Unit *u, |
1555 | | const char *name, |
1556 | | int *p, |
1557 | | sd_bus_message *message, |
1558 | | UnitWriteFlags flags, |
1559 | 0 | sd_bus_error *error) { |
1560 | 0 |
|
1561 | 0 | int32_t k; |
1562 | 0 | int r; |
1563 | 0 |
|
1564 | 0 | assert(p); |
1565 | 0 |
|
1566 | 0 | r = sd_bus_message_read(message, "i", &k); |
1567 | 0 | if (r < 0) |
1568 | 0 | return r; |
1569 | 0 | |
1570 | 0 | if (k > 255) |
1571 | 0 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Exit status must be in range 0…255 or negative."); |
1572 | 0 |
|
1573 | 0 | if (!UNIT_WRITE_FLAGS_NOOP(flags)) { |
1574 | 0 | *p = k < 0 ? -1 : k; |
1575 | 0 |
|
1576 | 0 | if (k < 0) |
1577 | 0 | unit_write_settingf(u, flags, name, "%s=", name); |
1578 | 0 | else |
1579 | 0 | unit_write_settingf(u, flags, name, "%s=%i", name, k); |
1580 | 0 | } |
1581 | 0 |
|
1582 | 0 | return 1; |
1583 | 0 | } |
1584 | | |
1585 | | static BUS_DEFINE_SET_TRANSIENT_PARSE(collect_mode, CollectMode, collect_mode_from_string); |
1586 | | static BUS_DEFINE_SET_TRANSIENT_PARSE(job_mode, JobMode, job_mode_from_string); |
1587 | | |
1588 | | static int bus_set_transient_conditions( |
1589 | | Unit *u, |
1590 | | const char *name, |
1591 | | Condition **list, |
1592 | | bool is_condition, |
1593 | | sd_bus_message *message, |
1594 | | UnitWriteFlags flags, |
1595 | 0 | sd_bus_error *error) { |
1596 | 0 |
|
1597 | 0 | const char *type_name, *param; |
1598 | 0 | int trigger, negate, r; |
1599 | 0 | bool empty = true; |
1600 | 0 |
|
1601 | 0 | assert(list); |
1602 | 0 |
|
1603 | 0 | r = sd_bus_message_enter_container(message, 'a', "(sbbs)"); |
1604 | 0 | if (r < 0) |
1605 | 0 | return r; |
1606 | 0 | |
1607 | 0 | while ((r = sd_bus_message_read(message, "(sbbs)", &type_name, &trigger, &negate, ¶m)) > 0) { |
1608 | 0 | ConditionType t; |
1609 | 0 |
|
1610 | 0 | t = is_condition ? condition_type_from_string(type_name) : assert_type_from_string(type_name); |
1611 | 0 | if (t < 0) |
1612 | 0 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid condition type: %s", type_name); |
1613 | 0 |
|
1614 | 0 | if (t != CONDITION_NULL) { |
1615 | 0 | if (isempty(param)) |
1616 | 0 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Condition parameter in %s is empty", type_name); |
1617 | 0 |
|
1618 | 0 | if (condition_takes_path(t) && !path_is_absolute(param)) |
1619 | 0 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Path in condition %s is not absolute: %s", type_name, param); |
1620 | 0 | } else |
1621 | 0 | param = NULL; |
1622 | 0 |
|
1623 | 0 | if (!UNIT_WRITE_FLAGS_NOOP(flags)) { |
1624 | 0 | Condition *c; |
1625 | 0 |
|
1626 | 0 | c = condition_new(t, param, trigger, negate); |
1627 | 0 | if (!c) |
1628 | 0 | return -ENOMEM; |
1629 | 0 | |
1630 | 0 | LIST_PREPEND(conditions, *list, c); |
1631 | 0 |
|
1632 | 0 | if (t != CONDITION_NULL) |
1633 | 0 | unit_write_settingf(u, flags|UNIT_ESCAPE_SPECIFIERS, name, |
1634 | 0 | "%s=%s%s%s", type_name, |
1635 | 0 | trigger ? "|" : "", negate ? "!" : "", param); |
1636 | 0 | else |
1637 | 0 | unit_write_settingf(u, flags, name, |
1638 | 0 | "%s=%s%s", type_name, |
1639 | 0 | trigger ? "|" : "", yes_no(!negate)); |
1640 | 0 | } |
1641 | 0 |
|
1642 | 0 | empty = false; |
1643 | 0 | } |
1644 | 0 | if (r < 0) |
1645 | 0 | return r; |
1646 | 0 | |
1647 | 0 | r = sd_bus_message_exit_container(message); |
1648 | 0 | if (r < 0) |
1649 | 0 | return r; |
1650 | 0 | |
1651 | 0 | if (!UNIT_WRITE_FLAGS_NOOP(flags) && empty) { |
1652 | 0 | *list = condition_free_list(*list); |
1653 | 0 | unit_write_settingf(u, flags, name, "%sNull=", is_condition ? "Condition" : "Assert"); |
1654 | 0 | } |
1655 | 0 |
|
1656 | 0 | return 1; |
1657 | 0 | } |
1658 | | |
1659 | | static int bus_unit_set_transient_property( |
1660 | | Unit *u, |
1661 | | const char *name, |
1662 | | sd_bus_message *message, |
1663 | | UnitWriteFlags flags, |
1664 | | sd_bus_error *error) { |
1665 | | |
1666 | | UnitDependency d = _UNIT_DEPENDENCY_INVALID; |
1667 | | int r; |
1668 | | |
1669 | | assert(u); |
1670 | | assert(name); |
1671 | | assert(message); |
1672 | | |
1673 | | /* Handles settings when transient units are created. This settings cannot be altered anymore after the unit |
1674 | | * has been created. */ |
1675 | | |
1676 | | if (streq(name, "SourcePath")) |
1677 | | return bus_set_transient_path(u, name, &u->source_path, message, flags, error); |
1678 | | |
1679 | | if (streq(name, "StopWhenUnneeded")) |
1680 | | return bus_set_transient_bool(u, name, &u->stop_when_unneeded, message, flags, error); |
1681 | | |
1682 | | if (streq(name, "RefuseManualStart")) |
1683 | | return bus_set_transient_bool(u, name, &u->refuse_manual_start, message, flags, error); |
1684 | | |
1685 | | if (streq(name, "RefuseManualStop")) |
1686 | | return bus_set_transient_bool(u, name, &u->refuse_manual_stop, message, flags, error); |
1687 | | |
1688 | | if (streq(name, "AllowIsolate")) |
1689 | | return bus_set_transient_bool(u, name, &u->allow_isolate, message, flags, error); |
1690 | | |
1691 | | if (streq(name, "DefaultDependencies")) |
1692 | | return bus_set_transient_bool(u, name, &u->default_dependencies, message, flags, error); |
1693 | | |
1694 | | if (streq(name, "OnFailureJobMode")) |
1695 | | return bus_set_transient_job_mode(u, name, &u->on_failure_job_mode, message, flags, error); |
1696 | | |
1697 | | if (streq(name, "IgnoreOnIsolate")) |
1698 | | return bus_set_transient_bool(u, name, &u->ignore_on_isolate, message, flags, error); |
1699 | | |
1700 | | if (streq(name, "JobTimeoutUSec")) { |
1701 | | r = bus_set_transient_usec_fix_0(u, name, &u->job_timeout, message, flags, error); |
1702 | | if (r >= 0 && !UNIT_WRITE_FLAGS_NOOP(flags) && !u->job_running_timeout_set) |
1703 | | u->job_running_timeout = u->job_timeout; |
1704 | | } |
1705 | | |
1706 | | if (streq(name, "JobRunningTimeoutUSec")) { |
1707 | | r = bus_set_transient_usec_fix_0(u, name, &u->job_running_timeout, message, flags, error); |
1708 | | if (r >= 0 && !UNIT_WRITE_FLAGS_NOOP(flags)) |
1709 | | u->job_running_timeout_set = true; |
1710 | | |
1711 | | return r; |
1712 | | } |
1713 | | |
1714 | | if (streq(name, "JobTimeoutAction")) |
1715 | | return bus_set_transient_emergency_action(u, name, &u->job_timeout_action, message, flags, error); |
1716 | | |
1717 | | if (streq(name, "JobTimeoutRebootArgument")) |
1718 | | return bus_set_transient_string(u, name, &u->job_timeout_reboot_arg, message, flags, error); |
1719 | | |
1720 | | if (streq(name, "StartLimitIntervalUSec")) |
1721 | | return bus_set_transient_usec(u, name, &u->start_limit.interval, message, flags, error); |
1722 | | |
1723 | | if (streq(name, "StartLimitBurst")) |
1724 | | return bus_set_transient_unsigned(u, name, &u->start_limit.burst, message, flags, error); |
1725 | | |
1726 | | if (streq(name, "StartLimitAction")) |
1727 | | return bus_set_transient_emergency_action(u, name, &u->start_limit_action, message, flags, error); |
1728 | | |
1729 | | if (streq(name, "FailureAction")) |
1730 | | return bus_set_transient_emergency_action(u, name, &u->failure_action, message, flags, error); |
1731 | | |
1732 | | if (streq(name, "SuccessAction")) |
1733 | | return bus_set_transient_emergency_action(u, name, &u->success_action, message, flags, error); |
1734 | | |
1735 | | if (streq(name, "FailureActionExitStatus")) |
1736 | | return bus_set_transient_exit_status(u, name, &u->failure_action_exit_status, message, flags, error); |
1737 | | |
1738 | | if (streq(name, "SuccessActionExitStatus")) |
1739 | | return bus_set_transient_exit_status(u, name, &u->success_action_exit_status, message, flags, error); |
1740 | | |
1741 | | if (streq(name, "RebootArgument")) |
1742 | | return bus_set_transient_string(u, name, &u->reboot_arg, message, flags, error); |
1743 | | |
1744 | | if (streq(name, "CollectMode")) |
1745 | | return bus_set_transient_collect_mode(u, name, &u->collect_mode, message, flags, error); |
1746 | | |
1747 | | if (streq(name, "Conditions")) |
1748 | | return bus_set_transient_conditions(u, name, &u->conditions, true, message, flags, error); |
1749 | | |
1750 | | if (streq(name, "Asserts")) |
1751 | | return bus_set_transient_conditions(u, name, &u->asserts, false, message, flags, error); |
1752 | | |
1753 | | if (streq(name, "Documentation")) { |
1754 | | _cleanup_strv_free_ char **l = NULL; |
1755 | | char **p; |
1756 | | |
1757 | | r = sd_bus_message_read_strv(message, &l); |
1758 | | if (r < 0) |
1759 | | return r; |
1760 | | |
1761 | | STRV_FOREACH(p, l) { |
1762 | | if (!documentation_url_is_valid(*p)) |
1763 | | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid URL in %s: %s", name, *p); |
1764 | | } |
1765 | | |
1766 | | if (!UNIT_WRITE_FLAGS_NOOP(flags)) { |
1767 | | if (strv_isempty(l)) { |
1768 | | u->documentation = strv_free(u->documentation); |
1769 | | unit_write_settingf(u, flags, name, "%s=", name); |
1770 | | } else { |
1771 | | strv_extend_strv(&u->documentation, l, false); |
1772 | | |
1773 | | STRV_FOREACH(p, l) |
1774 | | unit_write_settingf(u, flags, name, "%s=%s", name, *p); |
1775 | | } |
1776 | | } |
1777 | | |
1778 | | return 1; |
1779 | | |
1780 | | } else if (streq(name, "Slice")) { |
1781 | | Unit *slice; |
1782 | | const char *s; |
1783 | | |
1784 | | if (!UNIT_HAS_CGROUP_CONTEXT(u)) |
1785 | | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "The slice property is only available for units with control groups."); |
1786 | | if (u->type == UNIT_SLICE) |
1787 | | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Slice may not be set for slice units."); |
1788 | | if (unit_has_name(u, SPECIAL_INIT_SCOPE)) |
1789 | | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Cannot set slice for init.scope"); |
1790 | | |
1791 | | r = sd_bus_message_read(message, "s", &s); |
1792 | | if (r < 0) |
1793 | | return r; |
1794 | | |
1795 | | if (!unit_name_is_valid(s, UNIT_NAME_PLAIN)) |
1796 | | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid unit name '%s'", s); |
1797 | | |
1798 | | /* Note that we do not dispatch the load queue here yet, as we don't want our own transient unit to be |
1799 | | * loaded while we are still setting it up. Or in other words, we use manager_load_unit_prepare() |
1800 | | * instead of manager_load_unit() on purpose, here. */ |
1801 | | r = manager_load_unit_prepare(u->manager, s, NULL, error, &slice); |
1802 | | if (r < 0) |
1803 | | return r; |
1804 | | |
1805 | | if (slice->type != UNIT_SLICE) |
1806 | | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Unit name '%s' is not a slice", s); |
1807 | | |
1808 | | if (!UNIT_WRITE_FLAGS_NOOP(flags)) { |
1809 | | r = unit_set_slice(u, slice); |
1810 | | if (r < 0) |
1811 | | return r; |
1812 | | |
1813 | | unit_write_settingf(u, flags|UNIT_PRIVATE, name, "Slice=%s", s); |
1814 | | } |
1815 | | |
1816 | | return 1; |
1817 | | |
1818 | | } else if (streq(name, "RequiresMountsFor")) { |
1819 | | _cleanup_strv_free_ char **l = NULL; |
1820 | | char **p; |
1821 | | |
1822 | | r = sd_bus_message_read_strv(message, &l); |
1823 | | if (r < 0) |
1824 | | return r; |
1825 | | |
1826 | | STRV_FOREACH(p, l) { |
1827 | | path_simplify(*p, true); |
1828 | | |
1829 | | if (!path_is_absolute(*p)) |
1830 | | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Path specified in %s is not absolute: %s", name, *p); |
1831 | | |
1832 | | if (!path_is_valid(*p)) |
1833 | | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Path specified in %s has invalid length: %s", name, *p); |
1834 | | |
1835 | | if (!path_is_normalized(*p)) |
1836 | | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Path specified in %s is not normalized: %s", name, *p); |
1837 | | |
1838 | | if (!UNIT_WRITE_FLAGS_NOOP(flags)) { |
1839 | | r = unit_require_mounts_for(u, *p, UNIT_DEPENDENCY_FILE); |
1840 | | if (r < 0) |
1841 | | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Failed to add required mount \"%s\": %m", *p); |
1842 | | |
1843 | | unit_write_settingf(u, flags, name, "%s=%s", name, *p); |
1844 | | } |
1845 | | } |
1846 | | |
1847 | | return 1; |
1848 | | } |
1849 | | |
1850 | | if (streq(name, "RequiresOverridable")) |
1851 | | d = UNIT_REQUIRES; /* redirect for obsolete unit dependency type */ |
1852 | | else if (streq(name, "RequisiteOverridable")) |
1853 | | d = UNIT_REQUISITE; /* same here */ |
1854 | | else |
1855 | | d = unit_dependency_from_string(name); |
1856 | | |
1857 | | if (d >= 0) { |
1858 | | const char *other; |
1859 | | |
1860 | | r = sd_bus_message_enter_container(message, 'a', "s"); |
1861 | | if (r < 0) |
1862 | | return r; |
1863 | | |
1864 | | while ((r = sd_bus_message_read(message, "s", &other)) > 0) { |
1865 | | if (!unit_name_is_valid(other, UNIT_NAME_PLAIN|UNIT_NAME_INSTANCE)) |
1866 | | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid unit name %s", other); |
1867 | | |
1868 | | if (!UNIT_WRITE_FLAGS_NOOP(flags)) { |
1869 | | _cleanup_free_ char *label = NULL; |
1870 | | |
1871 | | r = unit_add_dependency_by_name(u, d, other, true, UNIT_DEPENDENCY_FILE); |
1872 | | if (r < 0) |
1873 | | return r; |
1874 | | |
1875 | | label = strjoin(name, "-", other); |
1876 | | if (!label) |
1877 | | return -ENOMEM; |
1878 | | |
1879 | | unit_write_settingf(u, flags, label, "%s=%s", unit_dependency_to_string(d), other); |
1880 | | } |
1881 | | |
1882 | | } |
1883 | | if (r < 0) |
1884 | | return r; |
1885 | | |
1886 | | r = sd_bus_message_exit_container(message); |
1887 | | if (r < 0) |
1888 | | return r; |
1889 | | |
1890 | | return 1; |
1891 | | |
1892 | | } else if (streq(name, "AddRef")) { |
1893 | | |
1894 | | int b; |
1895 | | |
1896 | | /* Why is this called "AddRef" rather than just "Ref", or "Reference"? There's already a "Ref()" method |
1897 | | * on the Unit interface, and it's probably not a good idea to expose a property and a method on the |
1898 | | * same interface (well, strictly speaking AddRef isn't exposed as full property, we just read it for |
1899 | | * transient units, but still). And "References" and "ReferencedBy" is already used as unit reference |
1900 | | * dependency type, hence let's not confuse things with that. |
1901 | | * |
1902 | | * Note that we don't actually add the reference to the bus track. We do that only after the setup of |
1903 | | * the transient unit is complete, so that setting this property multiple times in the same transient |
1904 | | * unit creation call doesn't count as individual references. */ |
1905 | | |
1906 | | r = sd_bus_message_read(message, "b", &b); |
1907 | | if (r < 0) |
1908 | | return r; |
1909 | | |
1910 | | if (!UNIT_WRITE_FLAGS_NOOP(flags)) |
1911 | | u->bus_track_add = b; |
1912 | | |
1913 | | return 1; |
1914 | | } |
1915 | | |
1916 | | return 0; |
1917 | | } |
1918 | | |
1919 | | int bus_unit_set_properties( |
1920 | | Unit *u, |
1921 | | sd_bus_message *message, |
1922 | | UnitWriteFlags flags, |
1923 | | bool commit, |
1924 | 0 | sd_bus_error *error) { |
1925 | 0 |
|
1926 | 0 | bool for_real = false; |
1927 | 0 | unsigned n = 0; |
1928 | 0 | int r; |
1929 | 0 |
|
1930 | 0 | assert(u); |
1931 | 0 | assert(message); |
1932 | 0 |
|
1933 | 0 | /* We iterate through the array twice. First run we just check |
1934 | 0 | * if all passed data is valid, second run actually applies |
1935 | 0 | * it. This is to implement transaction-like behaviour without |
1936 | 0 | * actually providing full transactions. */ |
1937 | 0 |
|
1938 | 0 | r = sd_bus_message_enter_container(message, 'a', "(sv)"); |
1939 | 0 | if (r < 0) |
1940 | 0 | return r; |
1941 | 0 | |
1942 | 0 | for (;;) { |
1943 | 0 | const char *name; |
1944 | 0 | UnitWriteFlags f; |
1945 | 0 |
|
1946 | 0 | r = sd_bus_message_enter_container(message, 'r', "sv"); |
1947 | 0 | if (r < 0) |
1948 | 0 | return r; |
1949 | 0 | if (r == 0) { |
1950 | 0 | if (for_real || UNIT_WRITE_FLAGS_NOOP(flags)) |
1951 | 0 | break; |
1952 | 0 | |
1953 | 0 | /* Reached EOF. Let's try again, and this time for realz... */ |
1954 | 0 | r = sd_bus_message_rewind(message, false); |
1955 | 0 | if (r < 0) |
1956 | 0 | return r; |
1957 | 0 | |
1958 | 0 | for_real = true; |
1959 | 0 | continue; |
1960 | 0 | } |
1961 | 0 | |
1962 | 0 | r = sd_bus_message_read(message, "s", &name); |
1963 | 0 | if (r < 0) |
1964 | 0 | return r; |
1965 | 0 | |
1966 | 0 | if (!UNIT_VTABLE(u)->bus_set_property) |
1967 | 0 | return sd_bus_error_setf(error, SD_BUS_ERROR_PROPERTY_READ_ONLY, "Objects of this type do not support setting properties."); |
1968 | 0 |
|
1969 | 0 | r = sd_bus_message_enter_container(message, 'v', NULL); |
1970 | 0 | if (r < 0) |
1971 | 0 | return r; |
1972 | 0 | |
1973 | 0 | /* If not for real, then mask out the two target flags */ |
1974 | 0 | f = for_real ? flags : (flags & ~(UNIT_RUNTIME|UNIT_PERSISTENT)); |
1975 | 0 |
|
1976 | 0 | r = UNIT_VTABLE(u)->bus_set_property(u, name, message, f, error); |
1977 | 0 | if (r == 0 && u->transient && u->load_state == UNIT_STUB) |
1978 | 0 | r = bus_unit_set_transient_property(u, name, message, f, error); |
1979 | 0 | if (r == 0) |
1980 | 0 | r = bus_unit_set_live_property(u, name, message, f, error); |
1981 | 0 | if (r < 0) |
1982 | 0 | return r; |
1983 | 0 | |
1984 | 0 | if (r == 0) |
1985 | 0 | return sd_bus_error_setf(error, SD_BUS_ERROR_PROPERTY_READ_ONLY, "Cannot set property %s, or unknown property.", name); |
1986 | 0 |
|
1987 | 0 | r = sd_bus_message_exit_container(message); |
1988 | 0 | if (r < 0) |
1989 | 0 | return r; |
1990 | 0 | |
1991 | 0 | r = sd_bus_message_exit_container(message); |
1992 | 0 | if (r < 0) |
1993 | 0 | return r; |
1994 | 0 | |
1995 | 0 | n += for_real; |
1996 | 0 | } |
1997 | 0 |
|
1998 | 0 | r = sd_bus_message_exit_container(message); |
1999 | 0 | if (r < 0) |
2000 | 0 | return r; |
2001 | 0 | |
2002 | 0 | if (commit && n > 0 && UNIT_VTABLE(u)->bus_commit_properties) |
2003 | 0 | UNIT_VTABLE(u)->bus_commit_properties(u); |
2004 | 0 |
|
2005 | 0 | return n; |
2006 | 0 | } |
2007 | | |
2008 | 0 | int bus_unit_validate_load_state(Unit *u, sd_bus_error *error) { |
2009 | 0 | assert(u); |
2010 | 0 |
|
2011 | 0 | /* Generates a pretty error if a unit isn't properly loaded. */ |
2012 | 0 |
|
2013 | 0 | switch (u->load_state) { |
2014 | 0 |
|
2015 | 0 | case UNIT_LOADED: |
2016 | 0 | return 0; |
2017 | 0 |
|
2018 | 0 | case UNIT_NOT_FOUND: |
2019 | 0 | return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_UNIT, "Unit %s not found.", u->id); |
2020 | 0 |
|
2021 | 0 | case UNIT_BAD_SETTING: |
2022 | 0 | return sd_bus_error_setf(error, BUS_ERROR_BAD_UNIT_SETTING, "Unit %s has a bad unit file setting.", u->id); |
2023 | 0 |
|
2024 | 0 | case UNIT_ERROR: /* Only show .load_error in UNIT_ERROR state */ |
2025 | 0 | return sd_bus_error_set_errnof(error, u->load_error, "Unit %s failed to load properly: %m.", u->id); |
2026 | 0 |
|
2027 | 0 | case UNIT_MASKED: |
2028 | 0 | return sd_bus_error_setf(error, BUS_ERROR_UNIT_MASKED, "Unit %s is masked.", u->id); |
2029 | 0 |
|
2030 | 0 | case UNIT_STUB: |
2031 | 0 | case UNIT_MERGED: |
2032 | 0 | default: |
2033 | 0 | return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_UNIT, "Unexpected load state of unit %s", u->id); |
2034 | 0 | } |
2035 | 0 | } |
2036 | | |
2037 | 0 | static int bus_unit_track_handler(sd_bus_track *t, void *userdata) { |
2038 | 0 | Unit *u = userdata; |
2039 | 0 |
|
2040 | 0 | assert(t); |
2041 | 0 | assert(u); |
2042 | 0 |
|
2043 | 0 | u->bus_track = sd_bus_track_unref(u->bus_track); /* make sure we aren't called again */ |
2044 | 0 |
|
2045 | 0 | /* If the client that tracks us disappeared, then there's reason to believe that the cgroup is empty now too, |
2046 | 0 | * let's see */ |
2047 | 0 | unit_add_to_cgroup_empty_queue(u); |
2048 | 0 |
|
2049 | 0 | /* Also add the unit to the GC queue, after all if the client left it might be time to GC this unit */ |
2050 | 0 | unit_add_to_gc_queue(u); |
2051 | 0 |
|
2052 | 0 | return 0; |
2053 | 0 | } |
2054 | | |
2055 | 0 | static int bus_unit_allocate_bus_track(Unit *u) { |
2056 | 0 | int r; |
2057 | 0 |
|
2058 | 0 | assert(u); |
2059 | 0 |
|
2060 | 0 | if (u->bus_track) |
2061 | 0 | return 0; |
2062 | 0 | |
2063 | 0 | r = sd_bus_track_new(u->manager->api_bus, &u->bus_track, bus_unit_track_handler, u); |
2064 | 0 | if (r < 0) |
2065 | 0 | return r; |
2066 | 0 | |
2067 | 0 | r = sd_bus_track_set_recursive(u->bus_track, true); |
2068 | 0 | if (r < 0) { |
2069 | 0 | u->bus_track = sd_bus_track_unref(u->bus_track); |
2070 | 0 | return r; |
2071 | 0 | } |
2072 | 0 | |
2073 | 0 | return 0; |
2074 | 0 | } |
2075 | | |
2076 | 0 | int bus_unit_track_add_name(Unit *u, const char *name) { |
2077 | 0 | int r; |
2078 | 0 |
|
2079 | 0 | assert(u); |
2080 | 0 |
|
2081 | 0 | r = bus_unit_allocate_bus_track(u); |
2082 | 0 | if (r < 0) |
2083 | 0 | return r; |
2084 | 0 | |
2085 | 0 | return sd_bus_track_add_name(u->bus_track, name); |
2086 | 0 | } |
2087 | | |
2088 | 0 | int bus_unit_track_add_sender(Unit *u, sd_bus_message *m) { |
2089 | 0 | int r; |
2090 | 0 |
|
2091 | 0 | assert(u); |
2092 | 0 |
|
2093 | 0 | r = bus_unit_allocate_bus_track(u); |
2094 | 0 | if (r < 0) |
2095 | 0 | return r; |
2096 | 0 | |
2097 | 0 | return sd_bus_track_add_sender(u->bus_track, m); |
2098 | 0 | } |
2099 | | |
2100 | 0 | int bus_unit_track_remove_sender(Unit *u, sd_bus_message *m) { |
2101 | 0 | assert(u); |
2102 | 0 |
|
2103 | 0 | /* If we haven't allocated the bus track object yet, then there's definitely no reference taken yet, return an |
2104 | 0 | * error */ |
2105 | 0 | if (!u->bus_track) |
2106 | 0 | return -EUNATCH; |
2107 | 0 | |
2108 | 0 | return sd_bus_track_remove_sender(u->bus_track, m); |
2109 | 0 | } |