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