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