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