]>
Commit | Line | Data |
---|---|---|
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 | static bool unit_can_start_refuse_manual(Unit *u) { | |
29 | return unit_can_start(u) && !u->refuse_manual_start; | |
30 | } | |
31 | ||
32 | static bool unit_can_stop_refuse_manual(Unit *u) { | |
33 | return unit_can_stop(u) && !u->refuse_manual_stop; | |
34 | } | |
35 | ||
36 | static bool unit_can_isolate_refuse_manual(Unit *u) { | |
37 | return unit_can_isolate(u) && !u->refuse_manual_start; | |
38 | } | |
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 | sd_bus_error *error) { | |
63 | ||
64 | Set **s = userdata; | |
65 | Iterator i; | |
66 | const char *t; | |
67 | int r; | |
68 | ||
69 | assert(bus); | |
70 | assert(reply); | |
71 | assert(s); | |
72 | ||
73 | r = sd_bus_message_open_container(reply, 'a', "s"); | |
74 | if (r < 0) | |
75 | return r; | |
76 | ||
77 | SET_FOREACH(t, *s, i) { | |
78 | r = sd_bus_message_append(reply, "s", t); | |
79 | if (r < 0) | |
80 | return r; | |
81 | } | |
82 | ||
83 | return sd_bus_message_close_container(reply); | |
84 | } | |
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 | sd_bus_error *error) { | |
94 | ||
95 | Unit *u = userdata, *f; | |
96 | ||
97 | assert(bus); | |
98 | assert(reply); | |
99 | assert(u); | |
100 | ||
101 | f = unit_following(u); | |
102 | return sd_bus_message_append(reply, "s", f ? f->id : NULL); | |
103 | } | |
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 | sd_bus_error *error) { | |
113 | ||
114 | Hashmap **h = userdata; | |
115 | Iterator j; | |
116 | Unit *u; | |
117 | void *v; | |
118 | int r; | |
119 | ||
120 | assert(bus); | |
121 | assert(reply); | |
122 | assert(h); | |
123 | ||
124 | r = sd_bus_message_open_container(reply, 'a', "s"); | |
125 | if (r < 0) | |
126 | return r; | |
127 | ||
128 | HASHMAP_FOREACH_KEY(v, u, *h, j) { | |
129 | r = sd_bus_message_append(reply, "s", u->id); | |
130 | if (r < 0) | |
131 | return r; | |
132 | } | |
133 | ||
134 | return sd_bus_message_close_container(reply); | |
135 | } | |
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 | sd_bus_error *error) { | |
145 | ||
146 | Hashmap **h = userdata; | |
147 | const char *p; | |
148 | Iterator j; | |
149 | void *v; | |
150 | int r; | |
151 | ||
152 | assert(bus); | |
153 | assert(reply); | |
154 | assert(h); | |
155 | ||
156 | r = sd_bus_message_open_container(reply, 'a', "s"); | |
157 | if (r < 0) | |
158 | return r; | |
159 | ||
160 | HASHMAP_FOREACH_KEY(v, p, *h, j) { | |
161 | r = sd_bus_message_append(reply, "s", p); | |
162 | if (r < 0) | |
163 | return r; | |
164 | } | |
165 | ||
166 | return sd_bus_message_close_container(reply); | |
167 | } | |
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 | sd_bus_error *error) { | |
177 | ||
178 | Unit *u = userdata; | |
179 | int r; | |
180 | ||
181 | assert(bus); | |
182 | assert(reply); | |
183 | assert(u); | |
184 | ||
185 | r = unit_get_unit_file_preset(u); | |
186 | ||
187 | return sd_bus_message_append(reply, "s", | |
188 | r < 0 ? NULL: | |
189 | r > 0 ? "enabled" : "disabled"); | |
190 | } | |
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 | sd_bus_error *error) { | |
200 | ||
201 | _cleanup_free_ char *p = NULL; | |
202 | Job **j = userdata; | |
203 | ||
204 | assert(bus); | |
205 | assert(reply); | |
206 | assert(j); | |
207 | ||
208 | if (!*j) | |
209 | return sd_bus_message_append(reply, "(uo)", 0, "/"); | |
210 | ||
211 | p = job_dbus_path(*j); | |
212 | if (!p) | |
213 | return -ENOMEM; | |
214 | ||
215 | return sd_bus_message_append(reply, "(uo)", (*j)->id, p); | |
216 | } | |
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 | sd_bus_error *error) { | |
226 | ||
227 | const char *(*to_string)(ConditionType type) = NULL; | |
228 | Condition **list = userdata, *c; | |
229 | int r; | |
230 | ||
231 | assert(bus); | |
232 | assert(reply); | |
233 | assert(list); | |
234 | ||
235 | to_string = streq(property, "Asserts") ? assert_type_to_string : condition_type_to_string; | |
236 | ||
237 | r = sd_bus_message_open_container(reply, 'a', "(sbbsi)"); | |
238 | if (r < 0) | |
239 | return r; | |
240 | ||
241 | LIST_FOREACH(conditions, c, *list) { | |
242 | int tristate; | |
243 | ||
244 | tristate = | |
245 | c->result == CONDITION_UNTESTED ? 0 : | |
246 | c->result == CONDITION_SUCCEEDED ? 1 : -1; | |
247 | ||
248 | r = sd_bus_message_append(reply, "(sbbsi)", | |
249 | to_string(c->type), | |
250 | c->trigger, c->negate, | |
251 | c->parameter, tristate); | |
252 | if (r < 0) | |
253 | return r; | |
254 | ||
255 | } | |
256 | ||
257 | return sd_bus_message_close_container(reply); | |
258 | } | |
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 | sd_bus_error *error) { | |
268 | ||
269 | _cleanup_(sd_bus_error_free) sd_bus_error e = SD_BUS_ERROR_NULL; | |
270 | Unit *u = userdata; | |
271 | int r; | |
272 | ||
273 | assert(bus); | |
274 | assert(reply); | |
275 | assert(u); | |
276 | ||
277 | r = bus_unit_validate_load_state(u, &e); | |
278 | if (r < 0) | |
279 | return sd_bus_message_append(reply, "(ss)", e.name, e.message); | |
280 | ||
281 | return sd_bus_message_append(reply, "(ss)", NULL, NULL); | |
282 | } | |
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 | sd_bus_error *error) { | |
292 | ||
293 | const char *details[9] = { | |
294 | "unit", u->id, | |
295 | "verb", verb, | |
296 | }; | |
297 | ||
298 | if (polkit_message) { | |
299 | details[4] = "polkit.message"; | |
300 | details[5] = polkit_message; | |
301 | details[6] = "polkit.gettext_domain"; | |
302 | details[7] = GETTEXT_PACKAGE; | |
303 | } | |
304 | ||
305 | return bus_verify_polkit_async( | |
306 | call, | |
307 | capability, | |
308 | "org.freedesktop.systemd1.manage-units", | |
309 | details, | |
310 | interactive, | |
311 | UID_INVALID, | |
312 | &u->manager->polkit_registry, | |
313 | error); | |
314 | } | |
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 | sd_bus_error *error) { | |
330 | ||
331 | const char *smode, *verb; | |
332 | JobMode mode; | |
333 | int r; | |
334 | ||
335 | assert(message); | |
336 | assert(u); | |
337 | assert(job_type >= 0 && job_type < _JOB_TYPE_MAX); | |
338 | ||
339 | r = mac_selinux_unit_access_check( | |
340 | u, message, | |
341 | job_type_to_access_method(job_type), | |
342 | error); | |
343 | if (r < 0) | |
344 | return r; | |
345 | ||
346 | r = sd_bus_message_read(message, "s", &smode); | |
347 | if (r < 0) | |
348 | return r; | |
349 | ||
350 | mode = job_mode_from_string(smode); | |
351 | if (mode < 0) | |
352 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Job mode %s invalid", smode); | |
353 | ||
354 | if (reload_if_possible) | |
355 | verb = strjoina("reload-or-", job_type_to_string(job_type)); | |
356 | else | |
357 | verb = job_type_to_string(job_type); | |
358 | ||
359 | r = bus_verify_manage_units_async_full( | |
360 | u, | |
361 | verb, | |
362 | CAP_SYS_ADMIN, | |
363 | polkit_message_for_job[job_type], | |
364 | true, | |
365 | message, | |
366 | error); | |
367 | if (r < 0) | |
368 | return r; | |
369 | if (r == 0) | |
370 | return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */ | |
371 | ||
372 | return bus_unit_queue_job(message, u, job_type, mode, | |
373 | reload_if_possible ? BUS_UNIT_QUEUE_RELOAD_IF_POSSIBLE : 0, error); | |
374 | } | |
375 | ||
376 | static int method_start(sd_bus_message *message, void *userdata, sd_bus_error *error) { | |
377 | return bus_unit_method_start_generic(message, userdata, JOB_START, false, error); | |
378 | } | |
379 | ||
380 | static int method_stop(sd_bus_message *message, void *userdata, sd_bus_error *error) { | |
381 | return bus_unit_method_start_generic(message, userdata, JOB_STOP, false, error); | |
382 | } | |
383 | ||
384 | static int method_reload(sd_bus_message *message, void *userdata, sd_bus_error *error) { | |
385 | return bus_unit_method_start_generic(message, userdata, JOB_RELOAD, false, error); | |
386 | } | |
387 | ||
388 | static int method_restart(sd_bus_message *message, void *userdata, sd_bus_error *error) { | |
389 | return bus_unit_method_start_generic(message, userdata, JOB_RESTART, false, error); | |
390 | } | |
391 | ||
392 | static int method_try_restart(sd_bus_message *message, void *userdata, sd_bus_error *error) { | |
393 | return bus_unit_method_start_generic(message, userdata, JOB_TRY_RESTART, false, error); | |
394 | } | |
395 | ||
396 | static int method_reload_or_restart(sd_bus_message *message, void *userdata, sd_bus_error *error) { | |
397 | return bus_unit_method_start_generic(message, userdata, JOB_RESTART, true, error); | |
398 | } | |
399 | ||
400 | static int method_reload_or_try_restart(sd_bus_message *message, void *userdata, sd_bus_error *error) { | |
401 | return bus_unit_method_start_generic(message, userdata, JOB_TRY_RESTART, true, error); | |
402 | } | |
403 | ||
404 | int bus_unit_method_enqueue_job(sd_bus_message *message, void *userdata, sd_bus_error *error) { | |
405 | BusUnitQueueFlags flags = BUS_UNIT_QUEUE_VERBOSE_REPLY; | |
406 | const char *jtype, *smode; | |
407 | Unit *u = userdata; | |
408 | JobType type; | |
409 | JobMode mode; | |
410 | int r; | |
411 | ||
412 | assert(message); | |
413 | assert(u); | |
414 | ||
415 | r = sd_bus_message_read(message, "ss", &jtype, &smode); | |
416 | if (r < 0) | |
417 | return r; | |
418 | ||
419 | /* Parse the two magic reload types "reload-or-…" manually */ | |
420 | if (streq(jtype, "reload-or-restart")) { | |
421 | type = JOB_RESTART; | |
422 | flags |= BUS_UNIT_QUEUE_RELOAD_IF_POSSIBLE; | |
423 | } else if (streq(jtype, "reload-or-try-restart")) { | |
424 | type = JOB_TRY_RESTART; | |
425 | flags |= BUS_UNIT_QUEUE_RELOAD_IF_POSSIBLE; | |
426 | } else { | |
427 | /* And the rest generically */ | |
428 | type = job_type_from_string(jtype); | |
429 | if (type < 0) | |
430 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Job type %s invalid", jtype); | |
431 | } | |
432 | ||
433 | mode = job_mode_from_string(smode); | |
434 | if (mode < 0) | |
435 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Job mode %s invalid", smode); | |
436 | ||
437 | r = mac_selinux_unit_access_check( | |
438 | u, message, | |
439 | job_type_to_access_method(type), | |
440 | error); | |
441 | if (r < 0) | |
442 | return r; | |
443 | ||
444 | r = bus_verify_manage_units_async_full( | |
445 | u, | |
446 | jtype, | |
447 | CAP_SYS_ADMIN, | |
448 | polkit_message_for_job[type], | |
449 | true, | |
450 | message, | |
451 | error); | |
452 | if (r < 0) | |
453 | return r; | |
454 | if (r == 0) | |
455 | return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */ | |
456 | ||
457 | return bus_unit_queue_job(message, u, type, mode, flags, error); | |
458 | } | |
459 | ||
460 | int bus_unit_method_kill(sd_bus_message *message, void *userdata, sd_bus_error *error) { | |
461 | Unit *u = userdata; | |
462 | const char *swho; | |
463 | int32_t signo; | |
464 | KillWho who; | |
465 | int r; | |
466 | ||
467 | assert(message); | |
468 | assert(u); | |
469 | ||
470 | r = mac_selinux_unit_access_check(u, message, "stop", error); | |
471 | if (r < 0) | |
472 | return r; | |
473 | ||
474 | r = sd_bus_message_read(message, "si", &swho, &signo); | |
475 | if (r < 0) | |
476 | return r; | |
477 | ||
478 | if (isempty(swho)) | |
479 | who = KILL_ALL; | |
480 | else { | |
481 | who = kill_who_from_string(swho); | |
482 | if (who < 0) | |
483 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid who argument %s", swho); | |
484 | } | |
485 | ||
486 | if (!SIGNAL_VALID(signo)) | |
487 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Signal number out of range."); | |
488 | ||
489 | r = bus_verify_manage_units_async_full( | |
490 | u, | |
491 | "kill", | |
492 | CAP_KILL, | |
493 | N_("Authentication is required to send a UNIX signal to the processes of '$(unit)'."), | |
494 | true, | |
495 | message, | |
496 | error); | |
497 | if (r < 0) | |
498 | return r; | |
499 | if (r == 0) | |
500 | return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */ | |
501 | ||
502 | r = unit_kill(u, who, signo, error); | |
503 | if (r < 0) | |
504 | return r; | |
505 | ||
506 | return sd_bus_reply_method_return(message, NULL); | |
507 | } | |
508 | ||
509 | int bus_unit_method_reset_failed(sd_bus_message *message, void *userdata, sd_bus_error *error) { | |
510 | Unit *u = userdata; | |
511 | int r; | |
512 | ||
513 | assert(message); | |
514 | assert(u); | |
515 | ||
516 | r = mac_selinux_unit_access_check(u, message, "reload", error); | |
517 | if (r < 0) | |
518 | return r; | |
519 | ||
520 | r = bus_verify_manage_units_async_full( | |
521 | u, | |
522 | "reset-failed", | |
523 | CAP_SYS_ADMIN, | |
524 | N_("Authentication is required to reset the \"failed\" state of '$(unit)'."), | |
525 | true, | |
526 | message, | |
527 | error); | |
528 | if (r < 0) | |
529 | return r; | |
530 | if (r == 0) | |
531 | return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */ | |
532 | ||
533 | unit_reset_failed(u); | |
534 | ||
535 | return sd_bus_reply_method_return(message, NULL); | |
536 | } | |
537 | ||
538 | int bus_unit_method_set_properties(sd_bus_message *message, void *userdata, sd_bus_error *error) { | |
539 | Unit *u = userdata; | |
540 | int runtime, r; | |
541 | ||
542 | assert(message); | |
543 | assert(u); | |
544 | ||
545 | r = mac_selinux_unit_access_check(u, message, "start", error); | |
546 | if (r < 0) | |
547 | return r; | |
548 | ||
549 | r = sd_bus_message_read(message, "b", &runtime); | |
550 | if (r < 0) | |
551 | return r; | |
552 | ||
553 | r = bus_verify_manage_units_async_full( | |
554 | u, | |
555 | "set-property", | |
556 | CAP_SYS_ADMIN, | |
557 | N_("Authentication is required to set properties on '$(unit)'."), | |
558 | true, | |
559 | message, | |
560 | error); | |
561 | if (r < 0) | |
562 | return r; | |
563 | if (r == 0) | |
564 | return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */ | |
565 | ||
566 | r = bus_unit_set_properties(u, message, runtime ? UNIT_RUNTIME : UNIT_PERSISTENT, true, error); | |
567 | if (r < 0) | |
568 | return r; | |
569 | ||
570 | return sd_bus_reply_method_return(message, NULL); | |
571 | } | |
572 | ||
573 | int bus_unit_method_ref(sd_bus_message *message, void *userdata, sd_bus_error *error) { | |
574 | Unit *u = userdata; | |
575 | int r; | |
576 | ||
577 | assert(message); | |
578 | assert(u); | |
579 | ||
580 | r = mac_selinux_unit_access_check(u, message, "start", error); | |
581 | if (r < 0) | |
582 | return r; | |
583 | ||
584 | r = bus_verify_manage_units_async_full( | |
585 | u, | |
586 | "ref", | |
587 | CAP_SYS_ADMIN, | |
588 | NULL, | |
589 | false, | |
590 | message, | |
591 | error); | |
592 | if (r < 0) | |
593 | return r; | |
594 | if (r == 0) | |
595 | return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */ | |
596 | ||
597 | r = bus_unit_track_add_sender(u, message); | |
598 | if (r < 0) | |
599 | return r; | |
600 | ||
601 | return sd_bus_reply_method_return(message, NULL); | |
602 | } | |
603 | ||
604 | int bus_unit_method_unref(sd_bus_message *message, void *userdata, sd_bus_error *error) { | |
605 | Unit *u = userdata; | |
606 | int r; | |
607 | ||
608 | assert(message); | |
609 | assert(u); | |
610 | ||
611 | r = bus_unit_track_remove_sender(u, message); | |
612 | if (r == -EUNATCH) | |
613 | return sd_bus_error_setf(error, BUS_ERROR_NOT_REFERENCED, "Unit has not been referenced yet."); | |
614 | if (r < 0) | |
615 | return r; | |
616 | ||
617 | return sd_bus_reply_method_return(message, NULL); | |
618 | } | |
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 | sd_bus_error *error) { | |
628 | ||
629 | Unit *u = userdata; | |
630 | const char *i; | |
631 | int r; | |
632 | ||
633 | assert(bus); | |
634 | assert(reply); | |
635 | ||
636 | r = sd_bus_message_open_container(reply, 'a', "s"); | |
637 | if (r < 0) | |
638 | return r; | |
639 | ||
640 | for (i = sd_bus_track_first(u->bus_track); i; i = sd_bus_track_next(u->bus_track)) { | |
641 | int c, k; | |
642 | ||
643 | c = sd_bus_track_count_name(u->bus_track, i); | |
644 | if (c < 0) | |
645 | return c; | |
646 | ||
647 | /* Add the item multiple times if the ref count for each is above 1 */ | |
648 | for (k = 0; k < c; k++) { | |
649 | r = sd_bus_message_append(reply, "s", i); | |
650 | if (r < 0) | |
651 | return r; | |
652 | } | |
653 | } | |
654 | ||
655 | return sd_bus_message_close_container(reply); | |
656 | } | |
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 | sd_bus_error *error) { | |
771 | ||
772 | Unit *u = userdata; | |
773 | ||
774 | assert(bus); | |
775 | assert(reply); | |
776 | assert(u); | |
777 | ||
778 | return sd_bus_message_append(reply, "s", unit_slice_name(u)); | |
779 | } | |
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 | sd_bus_error *error) { | |
789 | ||
790 | uint64_t sz = (uint64_t) -1; | |
791 | Unit *u = userdata; | |
792 | int r; | |
793 | ||
794 | assert(bus); | |
795 | assert(reply); | |
796 | assert(u); | |
797 | ||
798 | r = unit_get_memory_current(u, &sz); | |
799 | if (r < 0 && r != -ENODATA) | |
800 | log_unit_warning_errno(u, r, "Failed to get memory.usage_in_bytes attribute: %m"); | |
801 | ||
802 | return sd_bus_message_append(reply, "t", sz); | |
803 | } | |
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 | sd_bus_error *error) { | |
813 | ||
814 | uint64_t cn = (uint64_t) -1; | |
815 | Unit *u = userdata; | |
816 | int r; | |
817 | ||
818 | assert(bus); | |
819 | assert(reply); | |
820 | assert(u); | |
821 | ||
822 | r = unit_get_tasks_current(u, &cn); | |
823 | if (r < 0 && r != -ENODATA) | |
824 | log_unit_warning_errno(u, r, "Failed to get pids.current attribute: %m"); | |
825 | ||
826 | return sd_bus_message_append(reply, "t", cn); | |
827 | } | |
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 | sd_bus_error *error) { | |
837 | ||
838 | nsec_t ns = (nsec_t) -1; | |
839 | Unit *u = userdata; | |
840 | int r; | |
841 | ||
842 | assert(bus); | |
843 | assert(reply); | |
844 | assert(u); | |
845 | ||
846 | r = unit_get_cpu_usage(u, &ns); | |
847 | if (r < 0 && r != -ENODATA) | |
848 | log_unit_warning_errno(u, r, "Failed to get cpuacct.usage attribute: %m"); | |
849 | ||
850 | return sd_bus_message_append(reply, "t", ns); | |
851 | } | |
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 | sd_bus_error *error) { | |
861 | ||
862 | Unit *u = userdata; | |
863 | const char *t = NULL; | |
864 | ||
865 | assert(bus); | |
866 | assert(reply); | |
867 | assert(u); | |
868 | ||
869 | /* Three cases: a) u->cgroup_path is NULL, in which case the | |
870 | * unit has no control group, which we report as the empty | |
871 | * string. b) u->cgroup_path is the empty string, which | |
872 | * indicates the root cgroup, which we report as "/". c) all | |
873 | * other cases we report as-is. */ | |
874 | ||
875 | if (u->cgroup_path) | |
876 | t = empty_to_root(u->cgroup_path); | |
877 | ||
878 | return sd_bus_message_append(reply, "s", t); | |
879 | } | |
880 | ||
881 | static int append_process(sd_bus_message *reply, const char *p, pid_t pid, Set *pids) { | |
882 | _cleanup_free_ char *buf = NULL, *cmdline = NULL; | |
883 | int r; | |
884 | ||
885 | assert(reply); | |
886 | assert(pid > 0); | |
887 | ||
888 | r = set_put(pids, PID_TO_PTR(pid)); | |
889 | if (IN_SET(r, 0, -EEXIST)) | |
890 | return 0; | |
891 | if (r < 0) | |
892 | return r; | |
893 | ||
894 | if (!p) { | |
895 | r = cg_pid_get_path(SYSTEMD_CGROUP_CONTROLLER, pid, &buf); | |
896 | if (r == -ESRCH) | |
897 | return 0; | |
898 | if (r < 0) | |
899 | return r; | |
900 | ||
901 | p = buf; | |
902 | } | |
903 | ||
904 | (void) get_process_cmdline(pid, SIZE_MAX, PROCESS_CMDLINE_COMM_FALLBACK, &cmdline); | |
905 | ||
906 | return sd_bus_message_append(reply, | |
907 | "(sus)", | |
908 | p, | |
909 | (uint32_t) pid, | |
910 | cmdline); | |
911 | } | |
912 | ||
913 | static int append_cgroup(sd_bus_message *reply, const char *p, Set *pids) { | |
914 | _cleanup_closedir_ DIR *d = NULL; | |
915 | _cleanup_fclose_ FILE *f = NULL; | |
916 | int r; | |
917 | ||
918 | assert(reply); | |
919 | assert(p); | |
920 | ||
921 | r = cg_enumerate_processes(SYSTEMD_CGROUP_CONTROLLER, p, &f); | |
922 | if (r == -ENOENT) | |
923 | return 0; | |
924 | if (r < 0) | |
925 | return r; | |
926 | ||
927 | for (;;) { | |
928 | pid_t pid; | |
929 | ||
930 | r = cg_read_pid(f, &pid); | |
931 | if (r < 0) | |
932 | return r; | |
933 | if (r == 0) | |
934 | break; | |
935 | ||
936 | if (is_kernel_thread(pid) > 0) | |
937 | continue; | |
938 | ||
939 | r = append_process(reply, p, pid, pids); | |
940 | if (r < 0) | |
941 | return r; | |
942 | } | |
943 | ||
944 | r = cg_enumerate_subgroups(SYSTEMD_CGROUP_CONTROLLER, p, &d); | |
945 | if (r == -ENOENT) | |
946 | return 0; | |
947 | if (r < 0) | |
948 | return r; | |
949 | ||
950 | for (;;) { | |
951 | _cleanup_free_ char *g = NULL, *j = NULL; | |
952 | ||
953 | r = cg_read_subgroup(d, &g); | |
954 | if (r < 0) | |
955 | return r; | |
956 | if (r == 0) | |
957 | break; | |
958 | ||
959 | j = strjoin(p, "/", g); | |
960 | if (!j) | |
961 | return -ENOMEM; | |
962 | ||
963 | r = append_cgroup(reply, j, pids); | |
964 | if (r < 0) | |
965 | return r; | |
966 | } | |
967 | ||
968 | return 0; | |
969 | } | |
970 | ||
971 | int bus_unit_method_get_processes(sd_bus_message *message, void *userdata, sd_bus_error *error) { | |
972 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; | |
973 | _cleanup_set_free_ Set *pids = NULL; | |
974 | Unit *u = userdata; | |
975 | pid_t pid; | |
976 | int r; | |
977 | ||
978 | assert(message); | |
979 | ||
980 | r = mac_selinux_unit_access_check(u, message, "status", error); | |
981 | if (r < 0) | |
982 | return r; | |
983 | ||
984 | pids = set_new(NULL); | |
985 | if (!pids) | |
986 | return -ENOMEM; | |
987 | ||
988 | r = sd_bus_message_new_method_return(message, &reply); | |
989 | if (r < 0) | |
990 | return r; | |
991 | ||
992 | r = sd_bus_message_open_container(reply, 'a', "(sus)"); | |
993 | if (r < 0) | |
994 | return r; | |
995 | ||
996 | if (u->cgroup_path) { | |
997 | r = append_cgroup(reply, u->cgroup_path, pids); | |
998 | if (r < 0) | |
999 | return r; | |
1000 | } | |
1001 | ||
1002 | /* The main and control pids might live outside of the cgroup, hence fetch them separately */ | |
1003 | pid = unit_main_pid(u); | |
1004 | if (pid > 0) { | |
1005 | r = append_process(reply, NULL, pid, pids); | |
1006 | if (r < 0) | |
1007 | return r; | |
1008 | } | |
1009 | ||
1010 | pid = unit_control_pid(u); | |
1011 | if (pid > 0) { | |
1012 | r = append_process(reply, NULL, pid, pids); | |
1013 | if (r < 0) | |
1014 | return r; | |
1015 | } | |
1016 | ||
1017 | r = sd_bus_message_close_container(reply); | |
1018 | if (r < 0) | |
1019 | return r; | |
1020 | ||
1021 | return sd_bus_send(NULL, reply, NULL); | |
1022 | } | |
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 | sd_bus_error *error) { | |
1032 | ||
1033 | static const char *const table[_CGROUP_IP_ACCOUNTING_METRIC_MAX] = { | |
1034 | [CGROUP_IP_INGRESS_BYTES] = "IPIngressBytes", | |
1035 | [CGROUP_IP_EGRESS_BYTES] = "IPEgressBytes", | |
1036 | [CGROUP_IP_INGRESS_PACKETS] = "IPIngressPackets", | |
1037 | [CGROUP_IP_EGRESS_PACKETS] = "IPEgressPackets", | |
1038 | }; | |
1039 | ||
1040 | uint64_t value = UINT64_MAX; | |
1041 | Unit *u = userdata; | |
1042 | ssize_t metric; | |
1043 | ||
1044 | assert(bus); | |
1045 | assert(reply); | |
1046 | assert(property); | |
1047 | assert(u); | |
1048 | ||
1049 | assert_se((metric = string_table_lookup(table, ELEMENTSOF(table), property)) >= 0); | |
1050 | (void) unit_get_ip_accounting(u, metric, &value); | |
1051 | return sd_bus_message_append(reply, "t", value); | |
1052 | } | |
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 | sd_bus_error *error) { | |
1062 | ||
1063 | static const char *const table[_CGROUP_IO_ACCOUNTING_METRIC_MAX] = { | |
1064 | [CGROUP_IO_READ_BYTES] = "IOReadBytes", | |
1065 | [CGROUP_IO_WRITE_BYTES] = "IOWriteBytes", | |
1066 | [CGROUP_IO_READ_OPERATIONS] = "IOReadOperations", | |
1067 | [CGROUP_IO_WRITE_OPERATIONS] = "IOWriteOperations", | |
1068 | }; | |
1069 | ||
1070 | uint64_t value = UINT64_MAX; | |
1071 | Unit *u = userdata; | |
1072 | ssize_t metric; | |
1073 | ||
1074 | assert(bus); | |
1075 | assert(reply); | |
1076 | assert(property); | |
1077 | assert(u); | |
1078 | ||
1079 | assert_se((metric = string_table_lookup(table, ELEMENTSOF(table), property)) >= 0); | |
1080 | (void) unit_get_io_accounting(u, metric, false, &value); | |
1081 | return sd_bus_message_append(reply, "t", value); | |
1082 | } | |
1083 | ||
1084 | int bus_unit_method_attach_processes(sd_bus_message *message, void *userdata, sd_bus_error *error) { | |
1085 | ||
1086 | _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL; | |
1087 | _cleanup_set_free_ Set *pids = NULL; | |
1088 | Unit *u = userdata; | |
1089 | const char *path; | |
1090 | int r; | |
1091 | ||
1092 | assert(message); | |
1093 | ||
1094 | /* This migrates the processes with the specified PIDs into the cgroup of this unit, optionally below a | |
1095 | * specified cgroup path. Obviously this only works for units that actually maintain a cgroup | |
1096 | * representation. If a process is already in the cgroup no operation is executed – in this case the specified | |
1097 | * subcgroup path has no effect! */ | |
1098 | ||
1099 | r = mac_selinux_unit_access_check(u, message, "start", error); | |
1100 | if (r < 0) | |
1101 | return r; | |
1102 | ||
1103 | r = sd_bus_message_read(message, "s", &path); | |
1104 | if (r < 0) | |
1105 | return r; | |
1106 | ||
1107 | path = empty_to_null(path); | |
1108 | if (path) { | |
1109 | if (!path_is_absolute(path)) | |
1110 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Control group path is not absolute: %s", path); | |
1111 | ||
1112 | if (!path_is_normalized(path)) | |
1113 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Control group path is not normalized: %s", path); | |
1114 | } | |
1115 | ||
1116 | if (!unit_cgroup_delegate(u)) | |
1117 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Process migration not available on non-delegated units."); | |
1118 | ||
1119 | if (UNIT_IS_INACTIVE_OR_FAILED(unit_active_state(u))) | |
1120 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Unit is not active, refusing."); | |
1121 | ||
1122 | r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_EUID|SD_BUS_CREDS_PID, &creds); | |
1123 | if (r < 0) | |
1124 | return r; | |
1125 | ||
1126 | r = sd_bus_message_enter_container(message, 'a', "u"); | |
1127 | if (r < 0) | |
1128 | return r; | |
1129 | for (;;) { | |
1130 | uid_t process_uid, sender_uid; | |
1131 | uint32_t upid; | |
1132 | pid_t pid; | |
1133 | ||
1134 | r = sd_bus_message_read(message, "u", &upid); | |
1135 | if (r < 0) | |
1136 | return r; | |
1137 | if (r == 0) | |
1138 | break; | |
1139 | ||
1140 | if (upid == 0) { | |
1141 | r = sd_bus_creds_get_pid(creds, &pid); | |
1142 | if (r < 0) | |
1143 | return r; | |
1144 | } else | |
1145 | pid = (uid_t) upid; | |
1146 | ||
1147 | /* Filter out duplicates */ | |
1148 | if (set_contains(pids, PID_TO_PTR(pid))) | |
1149 | continue; | |
1150 | ||
1151 | /* Check if this process is suitable for attaching to this unit */ | |
1152 | r = unit_pid_attachable(u, pid, error); | |
1153 | if (r < 0) | |
1154 | return r; | |
1155 | ||
1156 | /* Let's query the sender's UID, so that we can make our security decisions */ | |
1157 | r = sd_bus_creds_get_euid(creds, &sender_uid); | |
1158 | if (r < 0) | |
1159 | return r; | |
1160 | ||
1161 | /* Let's validate security: if the sender is root, then all is OK. If the sender is any other unit, | |
1162 | * then the process' UID and the target unit's UID have to match the sender's UID */ | |
1163 | if (sender_uid != 0 && sender_uid != getuid()) { | |
1164 | r = get_process_uid(pid, &process_uid); | |
1165 | if (r < 0) | |
1166 | return sd_bus_error_set_errnof(error, r, "Failed to retrieve process UID: %m"); | |
1167 | ||
1168 | if (process_uid != sender_uid) | |
1169 | return sd_bus_error_setf(error, SD_BUS_ERROR_ACCESS_DENIED, "Process " PID_FMT " not owned by client's UID. Refusing.", pid); | |
1170 | if (process_uid != u->ref_uid) | |
1171 | return sd_bus_error_setf(error, SD_BUS_ERROR_ACCESS_DENIED, "Process " PID_FMT " not owned by target unit's UID. Refusing.", pid); | |
1172 | } | |
1173 | ||
1174 | if (!pids) { | |
1175 | pids = set_new(NULL); | |
1176 | if (!pids) | |
1177 | return -ENOMEM; | |
1178 | } | |
1179 | ||
1180 | r = set_put(pids, PID_TO_PTR(pid)); | |
1181 | if (r < 0) | |
1182 | return r; | |
1183 | } | |
1184 | ||
1185 | r = sd_bus_message_exit_container(message); | |
1186 | if (r < 0) | |
1187 | return r; | |
1188 | ||
1189 | r = unit_attach_pids_to_cgroup(u, pids, path); | |
1190 | if (r < 0) | |
1191 | return sd_bus_error_set_errnof(error, r, "Failed to attach processes to control group: %m"); | |
1192 | ||
1193 | return sd_bus_reply_method_return(message, NULL); | |
1194 | } | |
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 | static int send_new_signal(sd_bus *bus, void *userdata) { | |
1217 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL; | |
1218 | _cleanup_free_ char *p = NULL; | |
1219 | Unit *u = userdata; | |
1220 | int r; | |
1221 | ||
1222 | assert(bus); | |
1223 | assert(u); | |
1224 | ||
1225 | p = unit_dbus_path(u); | |
1226 | if (!p) | |
1227 | return -ENOMEM; | |
1228 | ||
1229 | r = sd_bus_message_new_signal( | |
1230 | bus, | |
1231 | &m, | |
1232 | "/org/freedesktop/systemd1", | |
1233 | "org.freedesktop.systemd1.Manager", | |
1234 | "UnitNew"); | |
1235 | if (r < 0) | |
1236 | return r; | |
1237 | ||
1238 | r = sd_bus_message_append(m, "so", u->id, p); | |
1239 | if (r < 0) | |
1240 | return r; | |
1241 | ||
1242 | return sd_bus_send(bus, m, NULL); | |
1243 | } | |
1244 | ||
1245 | static int send_changed_signal(sd_bus *bus, void *userdata) { | |
1246 | _cleanup_free_ char *p = NULL; | |
1247 | Unit *u = userdata; | |
1248 | int r; | |
1249 | ||
1250 | assert(bus); | |
1251 | assert(u); | |
1252 | ||
1253 | p = unit_dbus_path(u); | |
1254 | if (!p) | |
1255 | return -ENOMEM; | |
1256 | ||
1257 | /* Send a properties changed signal. First for the specific | |
1258 | * type, then for the generic unit. The clients may rely on | |
1259 | * this order to get atomic behavior if needed. */ | |
1260 | ||
1261 | r = sd_bus_emit_properties_changed_strv( | |
1262 | bus, p, | |
1263 | unit_dbus_interface_from_type(u->type), | |
1264 | NULL); | |
1265 | if (r < 0) | |
1266 | return r; | |
1267 | ||
1268 | return sd_bus_emit_properties_changed_strv( | |
1269 | bus, p, | |
1270 | "org.freedesktop.systemd1.Unit", | |
1271 | NULL); | |
1272 | } | |
1273 | ||
1274 | void bus_unit_send_change_signal(Unit *u) { | |
1275 | int r; | |
1276 | assert(u); | |
1277 | ||
1278 | if (u->in_dbus_queue) { | |
1279 | LIST_REMOVE(dbus_queue, u->manager->dbus_unit_queue, u); | |
1280 | u->in_dbus_queue = false; | |
1281 | } | |
1282 | ||
1283 | if (!u->id) | |
1284 | return; | |
1285 | ||
1286 | r = bus_foreach_bus(u->manager, u->bus_track, u->sent_dbus_new_signal ? send_changed_signal : send_new_signal, u); | |
1287 | if (r < 0) | |
1288 | log_unit_debug_errno(u, r, "Failed to send unit change signal for %s: %m", u->id); | |
1289 | ||
1290 | u->sent_dbus_new_signal = true; | |
1291 | } | |
1292 | ||
1293 | void bus_unit_send_pending_change_signal(Unit *u, bool including_new) { | |
1294 | ||
1295 | /* Sends out any pending change signals, but only if they really are pending. This call is used when we are | |
1296 | * about to change state in order to force out a PropertiesChanged signal beforehand if there was one pending | |
1297 | * so that clients can follow the full state transition */ | |
1298 | ||
1299 | if (!u->in_dbus_queue) /* If not enqueued, don't bother */ | |
1300 | return; | |
1301 | ||
1302 | if (!u->sent_dbus_new_signal && !including_new) /* If the unit was never announced, don't bother, it's fine if | |
1303 | * the unit appears in the new state right-away (except if the | |
1304 | * caller explicitly asked us to send it anyway) */ | |
1305 | return; | |
1306 | ||
1307 | if (MANAGER_IS_RELOADING(u->manager)) /* Don't generate unnecessary PropertiesChanged signals for the same unit | |
1308 | * when we are reloading. */ | |
1309 | return; | |
1310 | ||
1311 | bus_unit_send_change_signal(u); | |
1312 | } | |
1313 | ||
1314 | static int send_removed_signal(sd_bus *bus, void *userdata) { | |
1315 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL; | |
1316 | _cleanup_free_ char *p = NULL; | |
1317 | Unit *u = userdata; | |
1318 | int r; | |
1319 | ||
1320 | assert(bus); | |
1321 | assert(u); | |
1322 | ||
1323 | p = unit_dbus_path(u); | |
1324 | if (!p) | |
1325 | return -ENOMEM; | |
1326 | ||
1327 | r = sd_bus_message_new_signal( | |
1328 | bus, | |
1329 | &m, | |
1330 | "/org/freedesktop/systemd1", | |
1331 | "org.freedesktop.systemd1.Manager", | |
1332 | "UnitRemoved"); | |
1333 | if (r < 0) | |
1334 | return r; | |
1335 | ||
1336 | r = sd_bus_message_append(m, "so", u->id, p); | |
1337 | if (r < 0) | |
1338 | return r; | |
1339 | ||
1340 | return sd_bus_send(bus, m, NULL); | |
1341 | } | |
1342 | ||
1343 | void bus_unit_send_removed_signal(Unit *u) { | |
1344 | int r; | |
1345 | assert(u); | |
1346 | ||
1347 | if (!u->sent_dbus_new_signal || u->in_dbus_queue) | |
1348 | bus_unit_send_change_signal(u); | |
1349 | ||
1350 | if (!u->id) | |
1351 | return; | |
1352 | ||
1353 | r = bus_foreach_bus(u->manager, u->bus_track, send_removed_signal, u); | |
1354 | if (r < 0) | |
1355 | log_unit_debug_errno(u, r, "Failed to send unit remove signal for %s: %m", u->id); | |
1356 | } | |
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 | sd_bus_error *error) { | |
1365 | ||
1366 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; | |
1367 | _cleanup_free_ char *job_path = NULL, *unit_path = NULL; | |
1368 | _cleanup_(set_freep) Set *affected = NULL; | |
1369 | Iterator i; | |
1370 | Job *j, *a; | |
1371 | int r; | |
1372 | ||
1373 | assert(message); | |
1374 | assert(u); | |
1375 | assert(type >= 0 && type < _JOB_TYPE_MAX); | |
1376 | assert(mode >= 0 && mode < _JOB_MODE_MAX); | |
1377 | ||
1378 | r = mac_selinux_unit_access_check( | |
1379 | u, message, | |
1380 | job_type_to_access_method(type), | |
1381 | error); | |
1382 | if (r < 0) | |
1383 | return r; | |
1384 | ||
1385 | if (FLAGS_SET(flags, BUS_UNIT_QUEUE_RELOAD_IF_POSSIBLE) && unit_can_reload(u)) { | |
1386 | if (type == JOB_RESTART) | |
1387 | type = JOB_RELOAD_OR_START; | |
1388 | else if (type == JOB_TRY_RESTART) | |
1389 | type = JOB_TRY_RELOAD; | |
1390 | } | |
1391 | ||
1392 | if (type == JOB_STOP && | |
1393 | IN_SET(u->load_state, UNIT_NOT_FOUND, UNIT_ERROR, UNIT_BAD_SETTING) && | |
1394 | unit_active_state(u) == UNIT_INACTIVE) | |
1395 | return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_UNIT, "Unit %s not loaded.", u->id); | |
1396 | ||
1397 | if ((type == JOB_START && u->refuse_manual_start) || | |
1398 | (type == JOB_STOP && u->refuse_manual_stop) || | |
1399 | (IN_SET(type, JOB_RESTART, JOB_TRY_RESTART) && (u->refuse_manual_start || u->refuse_manual_stop)) || | |
1400 | (type == JOB_RELOAD_OR_START && job_type_collapse(type, u) == JOB_START && u->refuse_manual_start)) | |
1401 | 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 | ||
1403 | if (FLAGS_SET(flags, BUS_UNIT_QUEUE_VERBOSE_REPLY)) { | |
1404 | affected = set_new(NULL); | |
1405 | if (!affected) | |
1406 | return -ENOMEM; | |
1407 | } | |
1408 | ||
1409 | r = manager_add_job(u->manager, type, u, mode, affected, error, &j); | |
1410 | if (r < 0) | |
1411 | return r; | |
1412 | ||
1413 | r = bus_job_track_sender(j, message); | |
1414 | if (r < 0) | |
1415 | return r; | |
1416 | ||
1417 | /* Before we send the method reply, force out the announcement JobNew for this job */ | |
1418 | bus_job_send_pending_change_signal(j, true); | |
1419 | ||
1420 | job_path = job_dbus_path(j); | |
1421 | if (!job_path) | |
1422 | return -ENOMEM; | |
1423 | ||
1424 | /* The classic response is just a job object path */ | |
1425 | if (!FLAGS_SET(flags, BUS_UNIT_QUEUE_VERBOSE_REPLY)) | |
1426 | return sd_bus_reply_method_return(message, "o", job_path); | |
1427 | ||
1428 | /* In verbose mode respond with the anchor job plus everything that has been affected */ | |
1429 | r = sd_bus_message_new_method_return(message, &reply); | |
1430 | if (r < 0) | |
1431 | return r; | |
1432 | ||
1433 | unit_path = unit_dbus_path(j->unit); | |
1434 | if (!unit_path) | |
1435 | return -ENOMEM; | |
1436 | ||
1437 | r = sd_bus_message_append(reply, "uosos", | |
1438 | j->id, job_path, | |
1439 | j->unit->id, unit_path, | |
1440 | job_type_to_string(j->type)); | |
1441 | if (r < 0) | |
1442 | return r; | |
1443 | ||
1444 | r = sd_bus_message_open_container(reply, 'a', "(uosos)"); | |
1445 | if (r < 0) | |
1446 | return r; | |
1447 | ||
1448 | SET_FOREACH(a, affected, i) { | |
1449 | ||
1450 | if (a->id == j->id) | |
1451 | continue; | |
1452 | ||
1453 | /* Free paths from previous iteration */ | |
1454 | job_path = mfree(job_path); | |
1455 | unit_path = mfree(unit_path); | |
1456 | ||
1457 | job_path = job_dbus_path(a); | |
1458 | if (!job_path) | |
1459 | return -ENOMEM; | |
1460 | ||
1461 | unit_path = unit_dbus_path(a->unit); | |
1462 | if (!unit_path) | |
1463 | return -ENOMEM; | |
1464 | ||
1465 | r = sd_bus_message_append(reply, "(uosos)", | |
1466 | a->id, job_path, | |
1467 | a->unit->id, unit_path, | |
1468 | job_type_to_string(a->type)); | |
1469 | if (r < 0) | |
1470 | return r; | |
1471 | } | |
1472 | ||
1473 | r = sd_bus_message_close_container(reply); | |
1474 | if (r < 0) | |
1475 | return r; | |
1476 | ||
1477 | return sd_bus_send(NULL, reply, NULL); | |
1478 | } | |
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 | sd_bus_error *error) { | |
1486 | ||
1487 | int r; | |
1488 | ||
1489 | assert(u); | |
1490 | assert(name); | |
1491 | assert(message); | |
1492 | ||
1493 | /* Handles setting properties both "live" (i.e. at any time during runtime), and during creation (for transient | |
1494 | * units that are being created). */ | |
1495 | ||
1496 | if (streq(name, "Description")) { | |
1497 | const char *d; | |
1498 | ||
1499 | r = sd_bus_message_read(message, "s", &d); | |
1500 | if (r < 0) | |
1501 | return r; | |
1502 | ||
1503 | if (!UNIT_WRITE_FLAGS_NOOP(flags)) { | |
1504 | r = unit_set_description(u, d); | |
1505 | if (r < 0) | |
1506 | return r; | |
1507 | ||
1508 | unit_write_settingf(u, flags|UNIT_ESCAPE_SPECIFIERS, name, "Description=%s", d); | |
1509 | } | |
1510 | ||
1511 | return 1; | |
1512 | } | |
1513 | ||
1514 | return 0; | |
1515 | } | |
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 | sd_bus_error *error) { | |
1524 | ||
1525 | const char *s; | |
1526 | EmergencyAction v; | |
1527 | int r; | |
1528 | bool system; | |
1529 | ||
1530 | assert(p); | |
1531 | ||
1532 | r = sd_bus_message_read(message, "s", &s); | |
1533 | if (r < 0) | |
1534 | return r; | |
1535 | ||
1536 | system = MANAGER_IS_SYSTEM(u->manager); | |
1537 | r = parse_emergency_action(s, system, &v); | |
1538 | if (r < 0) | |
1539 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, | |
1540 | r == -EOPNOTSUPP ? "%s setting invalid for manager type: %s" | |
1541 | : "Invalid %s setting: %s", | |
1542 | name, s); | |
1543 | ||
1544 | if (!UNIT_WRITE_FLAGS_NOOP(flags)) { | |
1545 | *p = v; | |
1546 | unit_write_settingf(u, flags, name, | |
1547 | "%s=%s", name, s); | |
1548 | } | |
1549 | ||
1550 | return 1; | |
1551 | } | |
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 | sd_bus_error *error) { | |
1560 | ||
1561 | int32_t k; | |
1562 | int r; | |
1563 | ||
1564 | assert(p); | |
1565 | ||
1566 | r = sd_bus_message_read(message, "i", &k); | |
1567 | if (r < 0) | |
1568 | return r; | |
1569 | ||
1570 | if (k > 255) | |
1571 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Exit status must be in range 0…255 or negative."); | |
1572 | ||
1573 | if (!UNIT_WRITE_FLAGS_NOOP(flags)) { | |
1574 | *p = k < 0 ? -1 : k; | |
1575 | ||
1576 | if (k < 0) | |
1577 | unit_write_settingf(u, flags, name, "%s=", name); | |
1578 | else | |
1579 | unit_write_settingf(u, flags, name, "%s=%i", name, k); | |
1580 | } | |
1581 | ||
1582 | return 1; | |
1583 | } | |
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 | sd_bus_error *error) { | |
1596 | ||
1597 | const char *type_name, *param; | |
1598 | int trigger, negate, r; | |
1599 | bool empty = true; | |
1600 | ||
1601 | assert(list); | |
1602 | ||
1603 | r = sd_bus_message_enter_container(message, 'a', "(sbbs)"); | |
1604 | if (r < 0) | |
1605 | return r; | |
1606 | ||
1607 | while ((r = sd_bus_message_read(message, "(sbbs)", &type_name, &trigger, &negate, ¶m)) > 0) { | |
1608 | ConditionType t; | |
1609 | ||
1610 | t = is_condition ? condition_type_from_string(type_name) : assert_type_from_string(type_name); | |
1611 | if (t < 0) | |
1612 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid condition type: %s", type_name); | |
1613 | ||
1614 | if (t != CONDITION_NULL) { | |
1615 | if (isempty(param)) | |
1616 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Condition parameter in %s is empty", type_name); | |
1617 | ||
1618 | if (condition_takes_path(t) && !path_is_absolute(param)) | |
1619 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Path in condition %s is not absolute: %s", type_name, param); | |
1620 | } else | |
1621 | param = NULL; | |
1622 | ||
1623 | if (!UNIT_WRITE_FLAGS_NOOP(flags)) { | |
1624 | Condition *c; | |
1625 | ||
1626 | c = condition_new(t, param, trigger, negate); | |
1627 | if (!c) | |
1628 | return -ENOMEM; | |
1629 | ||
1630 | LIST_PREPEND(conditions, *list, c); | |
1631 | ||
1632 | if (t != CONDITION_NULL) | |
1633 | unit_write_settingf(u, flags|UNIT_ESCAPE_SPECIFIERS, name, | |
1634 | "%s=%s%s%s", type_name, | |
1635 | trigger ? "|" : "", negate ? "!" : "", param); | |
1636 | else | |
1637 | unit_write_settingf(u, flags, name, | |
1638 | "%s=%s%s", type_name, | |
1639 | trigger ? "|" : "", yes_no(!negate)); | |
1640 | } | |
1641 | ||
1642 | empty = false; | |
1643 | } | |
1644 | if (r < 0) | |
1645 | return r; | |
1646 | ||
1647 | r = sd_bus_message_exit_container(message); | |
1648 | if (r < 0) | |
1649 | return r; | |
1650 | ||
1651 | if (!UNIT_WRITE_FLAGS_NOOP(flags) && empty) { | |
1652 | *list = condition_free_list(*list); | |
1653 | unit_write_settingf(u, flags, name, "%sNull=", is_condition ? "Condition" : "Assert"); | |
1654 | } | |
1655 | ||
1656 | return 1; | |
1657 | } | |
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 | sd_bus_error *error) { | |
1925 | ||
1926 | bool for_real = false; | |
1927 | unsigned n = 0; | |
1928 | int r; | |
1929 | ||
1930 | assert(u); | |
1931 | assert(message); | |
1932 | ||
1933 | /* We iterate through the array twice. First run we just check | |
1934 | * if all passed data is valid, second run actually applies | |
1935 | * it. This is to implement transaction-like behaviour without | |
1936 | * actually providing full transactions. */ | |
1937 | ||
1938 | r = sd_bus_message_enter_container(message, 'a', "(sv)"); | |
1939 | if (r < 0) | |
1940 | return r; | |
1941 | ||
1942 | for (;;) { | |
1943 | const char *name; | |
1944 | UnitWriteFlags f; | |
1945 | ||
1946 | r = sd_bus_message_enter_container(message, 'r', "sv"); | |
1947 | if (r < 0) | |
1948 | return r; | |
1949 | if (r == 0) { | |
1950 | if (for_real || UNIT_WRITE_FLAGS_NOOP(flags)) | |
1951 | break; | |
1952 | ||
1953 | /* Reached EOF. Let's try again, and this time for realz... */ | |
1954 | r = sd_bus_message_rewind(message, false); | |
1955 | if (r < 0) | |
1956 | return r; | |
1957 | ||
1958 | for_real = true; | |
1959 | continue; | |
1960 | } | |
1961 | ||
1962 | r = sd_bus_message_read(message, "s", &name); | |
1963 | if (r < 0) | |
1964 | return r; | |
1965 | ||
1966 | if (!UNIT_VTABLE(u)->bus_set_property) | |
1967 | return sd_bus_error_setf(error, SD_BUS_ERROR_PROPERTY_READ_ONLY, "Objects of this type do not support setting properties."); | |
1968 | ||
1969 | r = sd_bus_message_enter_container(message, 'v', NULL); | |
1970 | if (r < 0) | |
1971 | return r; | |
1972 | ||
1973 | /* If not for real, then mask out the two target flags */ | |
1974 | f = for_real ? flags : (flags & ~(UNIT_RUNTIME|UNIT_PERSISTENT)); | |
1975 | ||
1976 | r = UNIT_VTABLE(u)->bus_set_property(u, name, message, f, error); | |
1977 | if (r == 0 && u->transient && u->load_state == UNIT_STUB) | |
1978 | r = bus_unit_set_transient_property(u, name, message, f, error); | |
1979 | if (r == 0) | |
1980 | r = bus_unit_set_live_property(u, name, message, f, error); | |
1981 | if (r < 0) | |
1982 | return r; | |
1983 | ||
1984 | if (r == 0) | |
1985 | return sd_bus_error_setf(error, SD_BUS_ERROR_PROPERTY_READ_ONLY, "Cannot set property %s, or unknown property.", name); | |
1986 | ||
1987 | r = sd_bus_message_exit_container(message); | |
1988 | if (r < 0) | |
1989 | return r; | |
1990 | ||
1991 | r = sd_bus_message_exit_container(message); | |
1992 | if (r < 0) | |
1993 | return r; | |
1994 | ||
1995 | n += for_real; | |
1996 | } | |
1997 | ||
1998 | r = sd_bus_message_exit_container(message); | |
1999 | if (r < 0) | |
2000 | return r; | |
2001 | ||
2002 | if (commit && n > 0 && UNIT_VTABLE(u)->bus_commit_properties) | |
2003 | UNIT_VTABLE(u)->bus_commit_properties(u); | |
2004 | ||
2005 | return n; | |
2006 | } | |
2007 | ||
2008 | int bus_unit_validate_load_state(Unit *u, sd_bus_error *error) { | |
2009 | assert(u); | |
2010 | ||
2011 | /* Generates a pretty error if a unit isn't properly loaded. */ | |
2012 | ||
2013 | switch (u->load_state) { | |
2014 | ||
2015 | case UNIT_LOADED: | |
2016 | return 0; | |
2017 | ||
2018 | case UNIT_NOT_FOUND: | |
2019 | return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_UNIT, "Unit %s not found.", u->id); | |
2020 | ||
2021 | case UNIT_BAD_SETTING: | |
2022 | return sd_bus_error_setf(error, BUS_ERROR_BAD_UNIT_SETTING, "Unit %s has a bad unit file setting.", u->id); | |
2023 | ||
2024 | case UNIT_ERROR: /* Only show .load_error in UNIT_ERROR state */ | |
2025 | return sd_bus_error_set_errnof(error, u->load_error, "Unit %s failed to load properly: %m.", u->id); | |
2026 | ||
2027 | case UNIT_MASKED: | |
2028 | return sd_bus_error_setf(error, BUS_ERROR_UNIT_MASKED, "Unit %s is masked.", u->id); | |
2029 | ||
2030 | case UNIT_STUB: | |
2031 | case UNIT_MERGED: | |
2032 | default: | |
2033 | return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_UNIT, "Unexpected load state of unit %s", u->id); | |
2034 | } | |
2035 | } | |
2036 | ||
2037 | static int bus_unit_track_handler(sd_bus_track *t, void *userdata) { | |
2038 | Unit *u = userdata; | |
2039 | ||
2040 | assert(t); | |
2041 | assert(u); | |
2042 | ||
2043 | u->bus_track = sd_bus_track_unref(u->bus_track); /* make sure we aren't called again */ | |
2044 | ||
2045 | /* If the client that tracks us disappeared, then there's reason to believe that the cgroup is empty now too, | |
2046 | * let's see */ | |
2047 | unit_add_to_cgroup_empty_queue(u); | |
2048 | ||
2049 | /* Also add the unit to the GC queue, after all if the client left it might be time to GC this unit */ | |
2050 | unit_add_to_gc_queue(u); | |
2051 | ||
2052 | return 0; | |
2053 | } | |
2054 | ||
2055 | static int bus_unit_allocate_bus_track(Unit *u) { | |
2056 | int r; | |
2057 | ||
2058 | assert(u); | |
2059 | ||
2060 | if (u->bus_track) | |
2061 | return 0; | |
2062 | ||
2063 | r = sd_bus_track_new(u->manager->api_bus, &u->bus_track, bus_unit_track_handler, u); | |
2064 | if (r < 0) | |
2065 | return r; | |
2066 | ||
2067 | r = sd_bus_track_set_recursive(u->bus_track, true); | |
2068 | if (r < 0) { | |
2069 | u->bus_track = sd_bus_track_unref(u->bus_track); | |
2070 | return r; | |
2071 | } | |
2072 | ||
2073 | return 0; | |
2074 | } | |
2075 | ||
2076 | int bus_unit_track_add_name(Unit *u, const char *name) { | |
2077 | int r; | |
2078 | ||
2079 | assert(u); | |
2080 | ||
2081 | r = bus_unit_allocate_bus_track(u); | |
2082 | if (r < 0) | |
2083 | return r; | |
2084 | ||
2085 | return sd_bus_track_add_name(u->bus_track, name); | |
2086 | } | |
2087 | ||
2088 | int bus_unit_track_add_sender(Unit *u, sd_bus_message *m) { | |
2089 | int r; | |
2090 | ||
2091 | assert(u); | |
2092 | ||
2093 | r = bus_unit_allocate_bus_track(u); | |
2094 | if (r < 0) | |
2095 | return r; | |
2096 | ||
2097 | return sd_bus_track_add_sender(u->bus_track, m); | |
2098 | } | |
2099 | ||
2100 | int bus_unit_track_remove_sender(Unit *u, sd_bus_message *m) { | |
2101 | assert(u); | |
2102 | ||
2103 | /* If we haven't allocated the bus track object yet, then there's definitely no reference taken yet, return an | |
2104 | * error */ | |
2105 | if (!u->bus_track) | |
2106 | return -EUNATCH; | |
2107 | ||
2108 | return sd_bus_track_remove_sender(u->bus_track, m); | |
2109 | } |