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