]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/core/dbus-unit.c
core: add DBUS method to bind mount new nodes without service restart
[thirdparty/systemd.git] / src / core / dbus-unit.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include "sd-bus.h"
4
5 #include "alloc-util.h"
6 #include "bpf-firewall.h"
7 #include "bus-common-errors.h"
8 #include "bus-get-properties.h"
9 #include "bus-polkit.h"
10 #include "cgroup-util.h"
11 #include "condition.h"
12 #include "dbus-job.h"
13 #include "dbus-unit.h"
14 #include "dbus-util.h"
15 #include "dbus.h"
16 #include "fd-util.h"
17 #include "install.h"
18 #include "locale-util.h"
19 #include "log.h"
20 #include "path-util.h"
21 #include "process-util.h"
22 #include "selinux-access.h"
23 #include "signal-util.h"
24 #include "special.h"
25 #include "string-table.h"
26 #include "string-util.h"
27 #include "strv.h"
28 #include "user-util.h"
29 #include "web-util.h"
30
31 static bool unit_can_start_refuse_manual(Unit *u) {
32 return unit_can_start(u) && !u->refuse_manual_start;
33 }
34
35 static bool unit_can_stop_refuse_manual(Unit *u) {
36 return unit_can_stop(u) && !u->refuse_manual_stop;
37 }
38
39 static bool unit_can_isolate_refuse_manual(Unit *u) {
40 return unit_can_isolate(u) && !u->refuse_manual_start;
41 }
42
43 static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_collect_mode, collect_mode, CollectMode);
44 static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_load_state, unit_load_state, UnitLoadState);
45 static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_job_mode, job_mode, JobMode);
46 static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_emergency_action, emergency_action, EmergencyAction);
47 static BUS_DEFINE_PROPERTY_GET(property_get_description, "s", Unit, unit_description);
48 static BUS_DEFINE_PROPERTY_GET2(property_get_active_state, "s", Unit, unit_active_state, unit_active_state_to_string);
49 static BUS_DEFINE_PROPERTY_GET2(property_get_freezer_state, "s", Unit, unit_freezer_state, freezer_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_can_freeze, "b", Unit, unit_can_freeze);
57 static BUS_DEFINE_PROPERTY_GET(property_get_need_daemon_reload, "b", Unit, unit_need_daemon_reload);
58 static BUS_DEFINE_PROPERTY_GET_GLOBAL(property_get_empty_strv, "as", 0);
59
60 static int property_get_can_clean(
61 sd_bus *bus,
62 const char *path,
63 const char *interface,
64 const char *property,
65 sd_bus_message *reply,
66 void *userdata,
67 sd_bus_error *error) {
68
69 Unit *u = userdata;
70 ExecCleanMask mask;
71 int r;
72
73 assert(bus);
74 assert(reply);
75
76 r = unit_can_clean(u, &mask);
77 if (r < 0)
78 return r;
79
80 r = sd_bus_message_open_container(reply, 'a', "s");
81 if (r < 0)
82 return r;
83
84 for (ExecDirectoryType t = 0; t < _EXEC_DIRECTORY_TYPE_MAX; t++) {
85 if (!FLAGS_SET(mask, 1U << t))
86 continue;
87
88 r = sd_bus_message_append(reply, "s", exec_resource_type_to_string(t));
89 if (r < 0)
90 return r;
91 }
92
93 return sd_bus_message_close_container(reply);
94 }
95
96 static int property_get_names(
97 sd_bus *bus,
98 const char *path,
99 const char *interface,
100 const char *property,
101 sd_bus_message *reply,
102 void *userdata,
103 sd_bus_error *error) {
104
105 Unit *u = userdata;
106 const char *t;
107 int r;
108
109 assert(bus);
110 assert(reply);
111 assert(u);
112
113 r = sd_bus_message_open_container(reply, 'a', "s");
114 if (r < 0)
115 return r;
116
117 r = sd_bus_message_append(reply, "s", u->id);
118 if (r < 0)
119 return r;
120
121 SET_FOREACH(t, u->aliases) {
122 r = sd_bus_message_append(reply, "s", t);
123 if (r < 0)
124 return r;
125 }
126
127 return sd_bus_message_close_container(reply);
128 }
129
130 static int property_get_following(
131 sd_bus *bus,
132 const char *path,
133 const char *interface,
134 const char *property,
135 sd_bus_message *reply,
136 void *userdata,
137 sd_bus_error *error) {
138
139 Unit *u = userdata, *f;
140
141 assert(bus);
142 assert(reply);
143 assert(u);
144
145 f = unit_following(u);
146 return sd_bus_message_append(reply, "s", f ? f->id : NULL);
147 }
148
149 static int property_get_dependencies(
150 sd_bus *bus,
151 const char *path,
152 const char *interface,
153 const char *property,
154 sd_bus_message *reply,
155 void *userdata,
156 sd_bus_error *error) {
157
158 Hashmap **h = userdata;
159 Unit *u;
160 void *v;
161 int r;
162
163 assert(bus);
164 assert(reply);
165 assert(h);
166
167 r = sd_bus_message_open_container(reply, 'a', "s");
168 if (r < 0)
169 return r;
170
171 HASHMAP_FOREACH_KEY(v, u, *h) {
172 r = sd_bus_message_append(reply, "s", u->id);
173 if (r < 0)
174 return r;
175 }
176
177 return sd_bus_message_close_container(reply);
178 }
179
180 static int property_get_requires_mounts_for(
181 sd_bus *bus,
182 const char *path,
183 const char *interface,
184 const char *property,
185 sd_bus_message *reply,
186 void *userdata,
187 sd_bus_error *error) {
188
189 Hashmap **h = userdata;
190 const char *p;
191 void *v;
192 int r;
193
194 assert(bus);
195 assert(reply);
196 assert(h);
197
198 r = sd_bus_message_open_container(reply, 'a', "s");
199 if (r < 0)
200 return r;
201
202 HASHMAP_FOREACH_KEY(v, p, *h) {
203 r = sd_bus_message_append(reply, "s", p);
204 if (r < 0)
205 return r;
206 }
207
208 return sd_bus_message_close_container(reply);
209 }
210
211 static int property_get_unit_file_preset(
212 sd_bus *bus,
213 const char *path,
214 const char *interface,
215 const char *property,
216 sd_bus_message *reply,
217 void *userdata,
218 sd_bus_error *error) {
219
220 Unit *u = userdata;
221 int r;
222
223 assert(bus);
224 assert(reply);
225 assert(u);
226
227 r = unit_get_unit_file_preset(u);
228
229 return sd_bus_message_append(reply, "s",
230 r < 0 ? NULL:
231 r > 0 ? "enabled" : "disabled");
232 }
233
234 static int property_get_job(
235 sd_bus *bus,
236 const char *path,
237 const char *interface,
238 const char *property,
239 sd_bus_message *reply,
240 void *userdata,
241 sd_bus_error *error) {
242
243 _cleanup_free_ char *p = NULL;
244 Job **j = userdata;
245
246 assert(bus);
247 assert(reply);
248 assert(j);
249
250 if (!*j)
251 return sd_bus_message_append(reply, "(uo)", 0, "/");
252
253 p = job_dbus_path(*j);
254 if (!p)
255 return -ENOMEM;
256
257 return sd_bus_message_append(reply, "(uo)", (*j)->id, p);
258 }
259
260 static int property_get_conditions(
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 const char *(*to_string)(ConditionType type) = NULL;
270 Condition **list = userdata, *c;
271 int r;
272
273 assert(bus);
274 assert(reply);
275 assert(list);
276
277 to_string = streq(property, "Asserts") ? assert_type_to_string : condition_type_to_string;
278
279 r = sd_bus_message_open_container(reply, 'a', "(sbbsi)");
280 if (r < 0)
281 return r;
282
283 LIST_FOREACH(conditions, c, *list) {
284 int tristate;
285
286 tristate =
287 c->result == CONDITION_UNTESTED ? 0 :
288 c->result == CONDITION_SUCCEEDED ? 1 : -1;
289
290 r = sd_bus_message_append(reply, "(sbbsi)",
291 to_string(c->type),
292 c->trigger, c->negate,
293 c->parameter, tristate);
294 if (r < 0)
295 return r;
296
297 }
298
299 return sd_bus_message_close_container(reply);
300 }
301
302 static int property_get_load_error(
303 sd_bus *bus,
304 const char *path,
305 const char *interface,
306 const char *property,
307 sd_bus_message *reply,
308 void *userdata,
309 sd_bus_error *error) {
310
311 _cleanup_(sd_bus_error_free) sd_bus_error e = SD_BUS_ERROR_NULL;
312 Unit *u = userdata;
313 int r;
314
315 assert(bus);
316 assert(reply);
317 assert(u);
318
319 r = bus_unit_validate_load_state(u, &e);
320 if (r < 0)
321 return sd_bus_message_append(reply, "(ss)", e.name, e.message);
322
323 return sd_bus_message_append(reply, "(ss)", NULL, NULL);
324 }
325
326 static const char *const polkit_message_for_job[_JOB_TYPE_MAX] = {
327 [JOB_START] = N_("Authentication is required to start '$(unit)'."),
328 [JOB_STOP] = N_("Authentication is required to stop '$(unit)'."),
329 [JOB_RELOAD] = N_("Authentication is required to reload '$(unit)'."),
330 [JOB_RESTART] = N_("Authentication is required to restart '$(unit)'."),
331 [JOB_TRY_RESTART] = N_("Authentication is required to restart '$(unit)'."),
332 };
333
334 int bus_unit_method_start_generic(
335 sd_bus_message *message,
336 Unit *u,
337 JobType job_type,
338 bool reload_if_possible,
339 sd_bus_error *error) {
340
341 const char *smode, *verb;
342 JobMode mode;
343 int r;
344
345 assert(message);
346 assert(u);
347 assert(job_type >= 0 && job_type < _JOB_TYPE_MAX);
348
349 r = mac_selinux_unit_access_check(
350 u, message,
351 job_type_to_access_method(job_type),
352 error);
353 if (r < 0)
354 return r;
355
356 r = sd_bus_message_read(message, "s", &smode);
357 if (r < 0)
358 return r;
359
360 mode = job_mode_from_string(smode);
361 if (mode < 0)
362 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Job mode %s invalid", smode);
363
364 if (reload_if_possible)
365 verb = strjoina("reload-or-", job_type_to_string(job_type));
366 else
367 verb = job_type_to_string(job_type);
368
369 r = bus_verify_manage_units_async_full(
370 u,
371 verb,
372 CAP_SYS_ADMIN,
373 polkit_message_for_job[job_type],
374 true,
375 message,
376 error);
377 if (r < 0)
378 return r;
379 if (r == 0)
380 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
381
382 return bus_unit_queue_job(message, u, job_type, mode,
383 reload_if_possible ? BUS_UNIT_QUEUE_RELOAD_IF_POSSIBLE : 0, error);
384 }
385
386 static int method_start(sd_bus_message *message, void *userdata, sd_bus_error *error) {
387 return bus_unit_method_start_generic(message, userdata, JOB_START, false, error);
388 }
389
390 static int method_stop(sd_bus_message *message, void *userdata, sd_bus_error *error) {
391 return bus_unit_method_start_generic(message, userdata, JOB_STOP, false, error);
392 }
393
394 static int method_reload(sd_bus_message *message, void *userdata, sd_bus_error *error) {
395 return bus_unit_method_start_generic(message, userdata, JOB_RELOAD, false, error);
396 }
397
398 static int method_restart(sd_bus_message *message, void *userdata, sd_bus_error *error) {
399 return bus_unit_method_start_generic(message, userdata, JOB_RESTART, false, error);
400 }
401
402 static int method_try_restart(sd_bus_message *message, void *userdata, sd_bus_error *error) {
403 return bus_unit_method_start_generic(message, userdata, JOB_TRY_RESTART, false, error);
404 }
405
406 static int method_reload_or_restart(sd_bus_message *message, void *userdata, sd_bus_error *error) {
407 return bus_unit_method_start_generic(message, userdata, JOB_RESTART, true, error);
408 }
409
410 static int method_reload_or_try_restart(sd_bus_message *message, void *userdata, sd_bus_error *error) {
411 return bus_unit_method_start_generic(message, userdata, JOB_TRY_RESTART, true, error);
412 }
413
414 int bus_unit_method_enqueue_job(sd_bus_message *message, void *userdata, sd_bus_error *error) {
415 BusUnitQueueFlags flags = BUS_UNIT_QUEUE_VERBOSE_REPLY;
416 const char *jtype, *smode;
417 Unit *u = userdata;
418 JobType type;
419 JobMode mode;
420 int r;
421
422 assert(message);
423 assert(u);
424
425 r = sd_bus_message_read(message, "ss", &jtype, &smode);
426 if (r < 0)
427 return r;
428
429 /* Parse the two magic reload types "reload-or-…" manually */
430 if (streq(jtype, "reload-or-restart")) {
431 type = JOB_RESTART;
432 flags |= BUS_UNIT_QUEUE_RELOAD_IF_POSSIBLE;
433 } else if (streq(jtype, "reload-or-try-restart")) {
434 type = JOB_TRY_RESTART;
435 flags |= BUS_UNIT_QUEUE_RELOAD_IF_POSSIBLE;
436 } else {
437 /* And the rest generically */
438 type = job_type_from_string(jtype);
439 if (type < 0)
440 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Job type %s invalid", jtype);
441 }
442
443 mode = job_mode_from_string(smode);
444 if (mode < 0)
445 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Job mode %s invalid", smode);
446
447 r = mac_selinux_unit_access_check(
448 u, message,
449 job_type_to_access_method(type),
450 error);
451 if (r < 0)
452 return r;
453
454 r = bus_verify_manage_units_async_full(
455 u,
456 jtype,
457 CAP_SYS_ADMIN,
458 polkit_message_for_job[type],
459 true,
460 message,
461 error);
462 if (r < 0)
463 return r;
464 if (r == 0)
465 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
466
467 return bus_unit_queue_job(message, u, type, mode, flags, error);
468 }
469
470 int bus_unit_method_kill(sd_bus_message *message, void *userdata, sd_bus_error *error) {
471 Unit *u = userdata;
472 const char *swho;
473 int32_t signo;
474 KillWho who;
475 int r;
476
477 assert(message);
478 assert(u);
479
480 r = mac_selinux_unit_access_check(u, message, "stop", error);
481 if (r < 0)
482 return r;
483
484 r = sd_bus_message_read(message, "si", &swho, &signo);
485 if (r < 0)
486 return r;
487
488 if (isempty(swho))
489 who = KILL_ALL;
490 else {
491 who = kill_who_from_string(swho);
492 if (who < 0)
493 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid who argument %s", swho);
494 }
495
496 if (!SIGNAL_VALID(signo))
497 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Signal number out of range.");
498
499 r = bus_verify_manage_units_async_full(
500 u,
501 "kill",
502 CAP_KILL,
503 N_("Authentication is required to send a UNIX signal to the processes of '$(unit)'."),
504 true,
505 message,
506 error);
507 if (r < 0)
508 return r;
509 if (r == 0)
510 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
511
512 r = unit_kill(u, who, signo, error);
513 if (r < 0)
514 return r;
515
516 return sd_bus_reply_method_return(message, NULL);
517 }
518
519 int bus_unit_method_reset_failed(sd_bus_message *message, void *userdata, sd_bus_error *error) {
520 Unit *u = userdata;
521 int r;
522
523 assert(message);
524 assert(u);
525
526 r = mac_selinux_unit_access_check(u, message, "reload", error);
527 if (r < 0)
528 return r;
529
530 r = bus_verify_manage_units_async_full(
531 u,
532 "reset-failed",
533 CAP_SYS_ADMIN,
534 N_("Authentication is required to reset the \"failed\" state of '$(unit)'."),
535 true,
536 message,
537 error);
538 if (r < 0)
539 return r;
540 if (r == 0)
541 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
542
543 unit_reset_failed(u);
544
545 return sd_bus_reply_method_return(message, NULL);
546 }
547
548 int bus_unit_method_set_properties(sd_bus_message *message, void *userdata, sd_bus_error *error) {
549 Unit *u = userdata;
550 int runtime, r;
551
552 assert(message);
553 assert(u);
554
555 r = mac_selinux_unit_access_check(u, message, "start", error);
556 if (r < 0)
557 return r;
558
559 r = sd_bus_message_read(message, "b", &runtime);
560 if (r < 0)
561 return r;
562
563 r = bus_verify_manage_units_async_full(
564 u,
565 "set-property",
566 CAP_SYS_ADMIN,
567 N_("Authentication is required to set properties on '$(unit)'."),
568 true,
569 message,
570 error);
571 if (r < 0)
572 return r;
573 if (r == 0)
574 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
575
576 r = bus_unit_set_properties(u, message, runtime ? UNIT_RUNTIME : UNIT_PERSISTENT, true, error);
577 if (r < 0)
578 return r;
579
580 return sd_bus_reply_method_return(message, NULL);
581 }
582
583 int bus_unit_method_ref(sd_bus_message *message, void *userdata, sd_bus_error *error) {
584 Unit *u = userdata;
585 int r;
586
587 assert(message);
588 assert(u);
589
590 r = mac_selinux_unit_access_check(u, message, "start", error);
591 if (r < 0)
592 return r;
593
594 r = bus_verify_manage_units_async_full(
595 u,
596 "ref",
597 CAP_SYS_ADMIN,
598 NULL,
599 false,
600 message,
601 error);
602 if (r < 0)
603 return r;
604 if (r == 0)
605 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
606
607 r = bus_unit_track_add_sender(u, message);
608 if (r < 0)
609 return r;
610
611 return sd_bus_reply_method_return(message, NULL);
612 }
613
614 int bus_unit_method_unref(sd_bus_message *message, void *userdata, sd_bus_error *error) {
615 Unit *u = userdata;
616 int r;
617
618 assert(message);
619 assert(u);
620
621 r = bus_unit_track_remove_sender(u, message);
622 if (r == -EUNATCH)
623 return sd_bus_error_setf(error, BUS_ERROR_NOT_REFERENCED, "Unit has not been referenced yet.");
624 if (r < 0)
625 return r;
626
627 return sd_bus_reply_method_return(message, NULL);
628 }
629
630 int bus_unit_method_clean(sd_bus_message *message, void *userdata, sd_bus_error *error) {
631 ExecCleanMask mask = 0;
632 Unit *u = userdata;
633 int r;
634
635 assert(message);
636 assert(u);
637
638 r = mac_selinux_unit_access_check(u, message, "stop", error);
639 if (r < 0)
640 return r;
641
642 r = sd_bus_message_enter_container(message, 'a', "s");
643 if (r < 0)
644 return r;
645
646 for (;;) {
647 const char *i;
648
649 r = sd_bus_message_read(message, "s", &i);
650 if (r < 0)
651 return r;
652 if (r == 0)
653 break;
654
655 if (streq(i, "all"))
656 mask |= EXEC_CLEAN_ALL;
657 else {
658 ExecDirectoryType t;
659
660 t = exec_resource_type_from_string(i);
661 if (t < 0)
662 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid resource type: %s", i);
663
664 mask |= 1U << t;
665 }
666 }
667
668 r = sd_bus_message_exit_container(message);
669 if (r < 0)
670 return r;
671
672 r = bus_verify_manage_units_async_full(
673 u,
674 "clean",
675 CAP_DAC_OVERRIDE,
676 N_("Authentication is required to delete files and directories associated with '$(unit)'."),
677 true,
678 message,
679 error);
680 if (r < 0)
681 return r;
682 if (r == 0)
683 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
684
685 r = unit_clean(u, mask);
686 if (r == -EOPNOTSUPP)
687 return sd_bus_error_setf(error, SD_BUS_ERROR_NOT_SUPPORTED, "Unit '%s' does not supporting cleaning.", u->id);
688 if (r == -EUNATCH)
689 return sd_bus_error_setf(error, BUS_ERROR_NOTHING_TO_CLEAN, "No matching resources found.");
690 if (r == -EBUSY)
691 return sd_bus_error_setf(error, BUS_ERROR_UNIT_BUSY, "Unit is not inactive or has pending job.");
692 if (r < 0)
693 return r;
694
695 return sd_bus_reply_method_return(message, NULL);
696 }
697
698 static int bus_unit_method_freezer_generic(sd_bus_message *message, void *userdata, sd_bus_error *error, FreezerAction action) {
699 const char* perm;
700 int (*method)(Unit*);
701 Unit *u = userdata;
702 bool reply_no_delay = false;
703 int r;
704
705 assert(message);
706 assert(u);
707 assert(IN_SET(action, FREEZER_FREEZE, FREEZER_THAW));
708
709 if (action == FREEZER_FREEZE) {
710 perm = "stop";
711 method = unit_freeze;
712 } else {
713 perm = "start";
714 method = unit_thaw;
715 }
716
717 r = mac_selinux_unit_access_check(u, message, perm, error);
718 if (r < 0)
719 return r;
720
721 r = bus_verify_manage_units_async_full(
722 u,
723 perm,
724 CAP_SYS_ADMIN,
725 N_("Authentication is required to freeze or thaw the processes of '$(unit)' unit."),
726 true,
727 message,
728 error);
729 if (r < 0)
730 return r;
731 if (r == 0)
732 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
733
734 r = method(u);
735 if (r == -EOPNOTSUPP)
736 return sd_bus_error_setf(error, SD_BUS_ERROR_NOT_SUPPORTED, "Unit '%s' does not support freezing.", u->id);
737 if (r == -EBUSY)
738 return sd_bus_error_setf(error, BUS_ERROR_UNIT_BUSY, "Unit has a pending job.");
739 if (r == -EHOSTDOWN)
740 return sd_bus_error_setf(error, BUS_ERROR_UNIT_INACTIVE, "Unit is inactive.");
741 if (r == -EALREADY)
742 return sd_bus_error_setf(error, SD_BUS_ERROR_FAILED, "Previously requested freezer operation for unit '%s' is still in progress.", u->id);
743 if (r < 0)
744 return r;
745 if (r == 0)
746 reply_no_delay = true;
747
748 assert(!u->pending_freezer_message);
749
750 r = sd_bus_message_new_method_return(message, &u->pending_freezer_message);
751 if (r < 0)
752 return r;
753
754 if (reply_no_delay) {
755 r = bus_unit_send_pending_freezer_message(u);
756 if (r < 0)
757 return r;
758 }
759
760 return 1;
761 }
762
763 int bus_unit_method_thaw(sd_bus_message *message, void *userdata, sd_bus_error *error) {
764 return bus_unit_method_freezer_generic(message, userdata, error, FREEZER_THAW);
765 }
766
767 int bus_unit_method_freeze(sd_bus_message *message, void *userdata, sd_bus_error *error) {
768 return bus_unit_method_freezer_generic(message, userdata, error, FREEZER_FREEZE);
769 }
770
771 static int property_get_refs(
772 sd_bus *bus,
773 const char *path,
774 const char *interface,
775 const char *property,
776 sd_bus_message *reply,
777 void *userdata,
778 sd_bus_error *error) {
779
780 Unit *u = userdata;
781 const char *i;
782 int r;
783
784 assert(bus);
785 assert(reply);
786
787 r = sd_bus_message_open_container(reply, 'a', "s");
788 if (r < 0)
789 return r;
790
791 for (i = sd_bus_track_first(u->bus_track); i; i = sd_bus_track_next(u->bus_track)) {
792 int c, k;
793
794 c = sd_bus_track_count_name(u->bus_track, i);
795 if (c < 0)
796 return c;
797
798 /* Add the item multiple times if the ref count for each is above 1 */
799 for (k = 0; k < c; k++) {
800 r = sd_bus_message_append(reply, "s", i);
801 if (r < 0)
802 return r;
803 }
804 }
805
806 return sd_bus_message_close_container(reply);
807 }
808
809 const sd_bus_vtable bus_unit_vtable[] = {
810 SD_BUS_VTABLE_START(0),
811
812 SD_BUS_PROPERTY("Id", "s", NULL, offsetof(Unit, id), SD_BUS_VTABLE_PROPERTY_CONST),
813 SD_BUS_PROPERTY("Names", "as", property_get_names, 0, SD_BUS_VTABLE_PROPERTY_CONST),
814 SD_BUS_PROPERTY("Following", "s", property_get_following, 0, 0),
815 SD_BUS_PROPERTY("Requires", "as", property_get_dependencies, offsetof(Unit, dependencies[UNIT_REQUIRES]), SD_BUS_VTABLE_PROPERTY_CONST),
816 SD_BUS_PROPERTY("Requisite", "as", property_get_dependencies, offsetof(Unit, dependencies[UNIT_REQUISITE]), SD_BUS_VTABLE_PROPERTY_CONST),
817 SD_BUS_PROPERTY("Wants", "as", property_get_dependencies, offsetof(Unit, dependencies[UNIT_WANTS]), SD_BUS_VTABLE_PROPERTY_CONST),
818 SD_BUS_PROPERTY("BindsTo", "as", property_get_dependencies, offsetof(Unit, dependencies[UNIT_BINDS_TO]), SD_BUS_VTABLE_PROPERTY_CONST),
819 SD_BUS_PROPERTY("PartOf", "as", property_get_dependencies, offsetof(Unit, dependencies[UNIT_PART_OF]), SD_BUS_VTABLE_PROPERTY_CONST),
820 SD_BUS_PROPERTY("RequiredBy", "as", property_get_dependencies, offsetof(Unit, dependencies[UNIT_REQUIRED_BY]), SD_BUS_VTABLE_PROPERTY_CONST),
821 SD_BUS_PROPERTY("RequisiteOf", "as", property_get_dependencies, offsetof(Unit, dependencies[UNIT_REQUISITE_OF]), SD_BUS_VTABLE_PROPERTY_CONST),
822 SD_BUS_PROPERTY("WantedBy", "as", property_get_dependencies, offsetof(Unit, dependencies[UNIT_WANTED_BY]), SD_BUS_VTABLE_PROPERTY_CONST),
823 SD_BUS_PROPERTY("BoundBy", "as", property_get_dependencies, offsetof(Unit, dependencies[UNIT_BOUND_BY]), SD_BUS_VTABLE_PROPERTY_CONST),
824 SD_BUS_PROPERTY("ConsistsOf", "as", property_get_dependencies, offsetof(Unit, dependencies[UNIT_CONSISTS_OF]), SD_BUS_VTABLE_PROPERTY_CONST),
825 SD_BUS_PROPERTY("Conflicts", "as", property_get_dependencies, offsetof(Unit, dependencies[UNIT_CONFLICTS]), SD_BUS_VTABLE_PROPERTY_CONST),
826 SD_BUS_PROPERTY("ConflictedBy", "as", property_get_dependencies, offsetof(Unit, dependencies[UNIT_CONFLICTED_BY]), SD_BUS_VTABLE_PROPERTY_CONST),
827 SD_BUS_PROPERTY("Before", "as", property_get_dependencies, offsetof(Unit, dependencies[UNIT_BEFORE]), SD_BUS_VTABLE_PROPERTY_CONST),
828 SD_BUS_PROPERTY("After", "as", property_get_dependencies, offsetof(Unit, dependencies[UNIT_AFTER]), SD_BUS_VTABLE_PROPERTY_CONST),
829 SD_BUS_PROPERTY("OnFailure", "as", property_get_dependencies, offsetof(Unit, dependencies[UNIT_ON_FAILURE]), SD_BUS_VTABLE_PROPERTY_CONST),
830 SD_BUS_PROPERTY("Triggers", "as", property_get_dependencies, offsetof(Unit, dependencies[UNIT_TRIGGERS]), SD_BUS_VTABLE_PROPERTY_CONST),
831 SD_BUS_PROPERTY("TriggeredBy", "as", property_get_dependencies, offsetof(Unit, dependencies[UNIT_TRIGGERED_BY]), SD_BUS_VTABLE_PROPERTY_CONST),
832 SD_BUS_PROPERTY("PropagatesReloadTo", "as", property_get_dependencies, offsetof(Unit, dependencies[UNIT_PROPAGATES_RELOAD_TO]), SD_BUS_VTABLE_PROPERTY_CONST),
833 SD_BUS_PROPERTY("ReloadPropagatedFrom", "as", property_get_dependencies, offsetof(Unit, dependencies[UNIT_RELOAD_PROPAGATED_FROM]), SD_BUS_VTABLE_PROPERTY_CONST),
834 SD_BUS_PROPERTY("JoinsNamespaceOf", "as", property_get_dependencies, offsetof(Unit, dependencies[UNIT_JOINS_NAMESPACE_OF]), SD_BUS_VTABLE_PROPERTY_CONST),
835 SD_BUS_PROPERTY("RequiresMountsFor", "as", property_get_requires_mounts_for, offsetof(Unit, requires_mounts_for), SD_BUS_VTABLE_PROPERTY_CONST),
836 SD_BUS_PROPERTY("Documentation", "as", NULL, offsetof(Unit, documentation), SD_BUS_VTABLE_PROPERTY_CONST),
837 SD_BUS_PROPERTY("Description", "s", property_get_description, 0, SD_BUS_VTABLE_PROPERTY_CONST),
838 SD_BUS_PROPERTY("LoadState", "s", property_get_load_state, offsetof(Unit, load_state), SD_BUS_VTABLE_PROPERTY_CONST),
839 SD_BUS_PROPERTY("ActiveState", "s", property_get_active_state, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
840 SD_BUS_PROPERTY("FreezerState", "s", property_get_freezer_state, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
841 SD_BUS_PROPERTY("SubState", "s", property_get_sub_state, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
842 SD_BUS_PROPERTY("FragmentPath", "s", NULL, offsetof(Unit, fragment_path), SD_BUS_VTABLE_PROPERTY_CONST),
843 SD_BUS_PROPERTY("SourcePath", "s", NULL, offsetof(Unit, source_path), SD_BUS_VTABLE_PROPERTY_CONST),
844 SD_BUS_PROPERTY("DropInPaths", "as", NULL, offsetof(Unit, dropin_paths), SD_BUS_VTABLE_PROPERTY_CONST),
845 SD_BUS_PROPERTY("UnitFileState", "s", property_get_unit_file_state, 0, 0),
846 SD_BUS_PROPERTY("UnitFilePreset", "s", property_get_unit_file_preset, 0, 0),
847 BUS_PROPERTY_DUAL_TIMESTAMP("StateChangeTimestamp", offsetof(Unit, state_change_timestamp), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
848 BUS_PROPERTY_DUAL_TIMESTAMP("InactiveExitTimestamp", offsetof(Unit, inactive_exit_timestamp), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
849 BUS_PROPERTY_DUAL_TIMESTAMP("ActiveEnterTimestamp", offsetof(Unit, active_enter_timestamp), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
850 BUS_PROPERTY_DUAL_TIMESTAMP("ActiveExitTimestamp", offsetof(Unit, active_exit_timestamp), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
851 BUS_PROPERTY_DUAL_TIMESTAMP("InactiveEnterTimestamp", offsetof(Unit, inactive_enter_timestamp), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
852 SD_BUS_PROPERTY("CanStart", "b", property_get_can_start, 0, SD_BUS_VTABLE_PROPERTY_CONST),
853 SD_BUS_PROPERTY("CanStop", "b", property_get_can_stop, 0, SD_BUS_VTABLE_PROPERTY_CONST),
854 SD_BUS_PROPERTY("CanReload", "b", property_get_can_reload, 0, SD_BUS_VTABLE_PROPERTY_CONST),
855 SD_BUS_PROPERTY("CanIsolate", "b", property_get_can_isolate, 0, SD_BUS_VTABLE_PROPERTY_CONST),
856 SD_BUS_PROPERTY("CanClean", "as", property_get_can_clean, 0, SD_BUS_VTABLE_PROPERTY_CONST),
857 SD_BUS_PROPERTY("CanFreeze", "b", property_get_can_freeze, 0, SD_BUS_VTABLE_PROPERTY_CONST),
858 SD_BUS_PROPERTY("Job", "(uo)", property_get_job, offsetof(Unit, job), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
859 SD_BUS_PROPERTY("StopWhenUnneeded", "b", bus_property_get_bool, offsetof(Unit, stop_when_unneeded), SD_BUS_VTABLE_PROPERTY_CONST),
860 SD_BUS_PROPERTY("RefuseManualStart", "b", bus_property_get_bool, offsetof(Unit, refuse_manual_start), SD_BUS_VTABLE_PROPERTY_CONST),
861 SD_BUS_PROPERTY("RefuseManualStop", "b", bus_property_get_bool, offsetof(Unit, refuse_manual_stop), SD_BUS_VTABLE_PROPERTY_CONST),
862 SD_BUS_PROPERTY("AllowIsolate", "b", bus_property_get_bool, offsetof(Unit, allow_isolate), SD_BUS_VTABLE_PROPERTY_CONST),
863 SD_BUS_PROPERTY("DefaultDependencies", "b", bus_property_get_bool, offsetof(Unit, default_dependencies), SD_BUS_VTABLE_PROPERTY_CONST),
864 SD_BUS_PROPERTY("OnFailureJobMode", "s", property_get_job_mode, offsetof(Unit, on_failure_job_mode), SD_BUS_VTABLE_PROPERTY_CONST),
865 SD_BUS_PROPERTY("IgnoreOnIsolate", "b", bus_property_get_bool, offsetof(Unit, ignore_on_isolate), SD_BUS_VTABLE_PROPERTY_CONST),
866 SD_BUS_PROPERTY("NeedDaemonReload", "b", property_get_need_daemon_reload, 0, SD_BUS_VTABLE_PROPERTY_CONST),
867 SD_BUS_PROPERTY("JobTimeoutUSec", "t", bus_property_get_usec, offsetof(Unit, job_timeout), SD_BUS_VTABLE_PROPERTY_CONST),
868 SD_BUS_PROPERTY("JobRunningTimeoutUSec", "t", bus_property_get_usec, offsetof(Unit, job_running_timeout), SD_BUS_VTABLE_PROPERTY_CONST),
869 SD_BUS_PROPERTY("JobTimeoutAction", "s", property_get_emergency_action, offsetof(Unit, job_timeout_action), SD_BUS_VTABLE_PROPERTY_CONST),
870 SD_BUS_PROPERTY("JobTimeoutRebootArgument", "s", NULL, offsetof(Unit, job_timeout_reboot_arg), SD_BUS_VTABLE_PROPERTY_CONST),
871 SD_BUS_PROPERTY("ConditionResult", "b", bus_property_get_bool, offsetof(Unit, condition_result), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
872 SD_BUS_PROPERTY("AssertResult", "b", bus_property_get_bool, offsetof(Unit, assert_result), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
873 BUS_PROPERTY_DUAL_TIMESTAMP("ConditionTimestamp", offsetof(Unit, condition_timestamp), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
874 BUS_PROPERTY_DUAL_TIMESTAMP("AssertTimestamp", offsetof(Unit, assert_timestamp), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
875 SD_BUS_PROPERTY("Conditions", "a(sbbsi)", property_get_conditions, offsetof(Unit, conditions), SD_BUS_VTABLE_PROPERTY_EMITS_INVALIDATION),
876 SD_BUS_PROPERTY("Asserts", "a(sbbsi)", property_get_conditions, offsetof(Unit, asserts), SD_BUS_VTABLE_PROPERTY_EMITS_INVALIDATION),
877 SD_BUS_PROPERTY("LoadError", "(ss)", property_get_load_error, 0, SD_BUS_VTABLE_PROPERTY_CONST),
878 SD_BUS_PROPERTY("Transient", "b", bus_property_get_bool, offsetof(Unit, transient), SD_BUS_VTABLE_PROPERTY_CONST),
879 SD_BUS_PROPERTY("Perpetual", "b", bus_property_get_bool, offsetof(Unit, perpetual), SD_BUS_VTABLE_PROPERTY_CONST),
880 SD_BUS_PROPERTY("StartLimitIntervalUSec", "t", bus_property_get_usec, offsetof(Unit, start_ratelimit.interval), SD_BUS_VTABLE_PROPERTY_CONST),
881 SD_BUS_PROPERTY("StartLimitBurst", "u", bus_property_get_unsigned, offsetof(Unit, start_ratelimit.burst), SD_BUS_VTABLE_PROPERTY_CONST),
882 SD_BUS_PROPERTY("StartLimitAction", "s", property_get_emergency_action, offsetof(Unit, start_limit_action), SD_BUS_VTABLE_PROPERTY_CONST),
883 SD_BUS_PROPERTY("FailureAction", "s", property_get_emergency_action, offsetof(Unit, failure_action), SD_BUS_VTABLE_PROPERTY_CONST),
884 SD_BUS_PROPERTY("FailureActionExitStatus", "i", bus_property_get_int, offsetof(Unit, failure_action_exit_status), SD_BUS_VTABLE_PROPERTY_CONST),
885 SD_BUS_PROPERTY("SuccessAction", "s", property_get_emergency_action, offsetof(Unit, success_action), SD_BUS_VTABLE_PROPERTY_CONST),
886 SD_BUS_PROPERTY("SuccessActionExitStatus", "i", bus_property_get_int, offsetof(Unit, success_action_exit_status), SD_BUS_VTABLE_PROPERTY_CONST),
887 SD_BUS_PROPERTY("RebootArgument", "s", NULL, offsetof(Unit, reboot_arg), SD_BUS_VTABLE_PROPERTY_CONST),
888 SD_BUS_PROPERTY("InvocationID", "ay", bus_property_get_id128, offsetof(Unit, invocation_id), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
889 SD_BUS_PROPERTY("CollectMode", "s", property_get_collect_mode, offsetof(Unit, collect_mode), SD_BUS_VTABLE_PROPERTY_CONST),
890 SD_BUS_PROPERTY("Refs", "as", property_get_refs, 0, 0),
891
892 SD_BUS_METHOD_WITH_NAMES("Start",
893 "s",
894 SD_BUS_PARAM(mode),
895 "o",
896 SD_BUS_PARAM(job),
897 method_start,
898 SD_BUS_VTABLE_UNPRIVILEGED),
899 SD_BUS_METHOD_WITH_NAMES("Stop",
900 "s",
901 SD_BUS_PARAM(mode),
902 "o",
903 SD_BUS_PARAM(job),
904 method_stop,
905 SD_BUS_VTABLE_UNPRIVILEGED),
906 SD_BUS_METHOD_WITH_NAMES("Reload",
907 "s",
908 SD_BUS_PARAM(mode),
909 "o",
910 SD_BUS_PARAM(job),
911 method_reload,
912 SD_BUS_VTABLE_UNPRIVILEGED),
913 SD_BUS_METHOD_WITH_NAMES("Restart",
914 "s",
915 SD_BUS_PARAM(mode),
916 "o",
917 SD_BUS_PARAM(job),
918 method_restart,
919 SD_BUS_VTABLE_UNPRIVILEGED),
920 SD_BUS_METHOD_WITH_NAMES("TryRestart",
921 "s",
922 SD_BUS_PARAM(mode),
923 "o",
924 SD_BUS_PARAM(job),
925 method_try_restart,
926 SD_BUS_VTABLE_UNPRIVILEGED),
927 SD_BUS_METHOD_WITH_NAMES("ReloadOrRestart",
928 "s",
929 SD_BUS_PARAM(mode),
930 "o",
931 SD_BUS_PARAM(job),
932 method_reload_or_restart,
933 SD_BUS_VTABLE_UNPRIVILEGED),
934 SD_BUS_METHOD_WITH_NAMES("ReloadOrTryRestart",
935 "s",
936 SD_BUS_PARAM(mode),
937 "o",
938 SD_BUS_PARAM(job),
939 method_reload_or_try_restart,
940 SD_BUS_VTABLE_UNPRIVILEGED),
941 SD_BUS_METHOD_WITH_NAMES("EnqueueJob",
942 "ss",
943 SD_BUS_PARAM(job_type)
944 SD_BUS_PARAM(job_mode),
945 "uososa(uosos)",
946 SD_BUS_PARAM(job_id)
947 SD_BUS_PARAM(job_path)
948 SD_BUS_PARAM(unit_id)
949 SD_BUS_PARAM(unit_path)
950 SD_BUS_PARAM(job_type)
951 SD_BUS_PARAM(affected_jobs),
952 bus_unit_method_enqueue_job,
953 SD_BUS_VTABLE_UNPRIVILEGED),
954 SD_BUS_METHOD_WITH_NAMES("Kill",
955 "si",
956 SD_BUS_PARAM(whom)
957 SD_BUS_PARAM(signal),
958 NULL,,
959 bus_unit_method_kill,
960 SD_BUS_VTABLE_UNPRIVILEGED),
961 SD_BUS_METHOD("ResetFailed",
962 NULL,
963 NULL,
964 bus_unit_method_reset_failed,
965 SD_BUS_VTABLE_UNPRIVILEGED),
966 SD_BUS_METHOD_WITH_NAMES("SetProperties",
967 "ba(sv)",
968 SD_BUS_PARAM(runtime)
969 SD_BUS_PARAM(properties),
970 NULL,,
971 bus_unit_method_set_properties,
972 SD_BUS_VTABLE_UNPRIVILEGED),
973 SD_BUS_METHOD("Ref",
974 NULL,
975 NULL,
976 bus_unit_method_ref,
977 SD_BUS_VTABLE_UNPRIVILEGED),
978 SD_BUS_METHOD("Unref",
979 NULL,
980 NULL,
981 bus_unit_method_unref,
982 SD_BUS_VTABLE_UNPRIVILEGED),
983 SD_BUS_METHOD_WITH_NAMES("Clean",
984 "as",
985 SD_BUS_PARAM(mask),
986 NULL,,
987 bus_unit_method_clean,
988 SD_BUS_VTABLE_UNPRIVILEGED),
989 SD_BUS_METHOD("Freeze",
990 NULL,
991 NULL,
992 bus_unit_method_freeze,
993 SD_BUS_VTABLE_UNPRIVILEGED),
994 SD_BUS_METHOD("Thaw",
995 NULL,
996 NULL,
997 bus_unit_method_thaw,
998 SD_BUS_VTABLE_UNPRIVILEGED),
999
1000 /* For dependency types we don't support anymore always return an empty array */
1001 SD_BUS_PROPERTY("RequiresOverridable", "as", property_get_empty_strv, 0, SD_BUS_VTABLE_HIDDEN),
1002 SD_BUS_PROPERTY("RequisiteOverridable", "as", property_get_empty_strv, 0, SD_BUS_VTABLE_HIDDEN),
1003 SD_BUS_PROPERTY("RequiredByOverridable", "as", property_get_empty_strv, 0, SD_BUS_VTABLE_HIDDEN),
1004 SD_BUS_PROPERTY("RequisiteOfOverridable", "as", property_get_empty_strv, 0, SD_BUS_VTABLE_HIDDEN),
1005 /* Obsolete alias names */
1006 SD_BUS_PROPERTY("StartLimitInterval", "t", bus_property_get_usec, offsetof(Unit, start_ratelimit.interval), SD_BUS_VTABLE_PROPERTY_CONST|SD_BUS_VTABLE_HIDDEN),
1007 SD_BUS_PROPERTY("StartLimitIntervalSec", "t", bus_property_get_usec, offsetof(Unit, start_ratelimit.interval), SD_BUS_VTABLE_PROPERTY_CONST|SD_BUS_VTABLE_HIDDEN),
1008
1009 SD_BUS_VTABLE_END
1010 };
1011
1012 static int property_get_slice(
1013 sd_bus *bus,
1014 const char *path,
1015 const char *interface,
1016 const char *property,
1017 sd_bus_message *reply,
1018 void *userdata,
1019 sd_bus_error *error) {
1020
1021 Unit *u = userdata;
1022
1023 assert(bus);
1024 assert(reply);
1025 assert(u);
1026
1027 return sd_bus_message_append(reply, "s", unit_slice_name(u));
1028 }
1029
1030 static int property_get_current_memory(
1031 sd_bus *bus,
1032 const char *path,
1033 const char *interface,
1034 const char *property,
1035 sd_bus_message *reply,
1036 void *userdata,
1037 sd_bus_error *error) {
1038
1039 uint64_t sz = (uint64_t) -1;
1040 Unit *u = userdata;
1041 int r;
1042
1043 assert(bus);
1044 assert(reply);
1045 assert(u);
1046
1047 r = unit_get_memory_current(u, &sz);
1048 if (r < 0 && r != -ENODATA)
1049 log_unit_warning_errno(u, r, "Failed to get memory.usage_in_bytes attribute: %m");
1050
1051 return sd_bus_message_append(reply, "t", sz);
1052 }
1053
1054 static int property_get_current_tasks(
1055 sd_bus *bus,
1056 const char *path,
1057 const char *interface,
1058 const char *property,
1059 sd_bus_message *reply,
1060 void *userdata,
1061 sd_bus_error *error) {
1062
1063 uint64_t cn = (uint64_t) -1;
1064 Unit *u = userdata;
1065 int r;
1066
1067 assert(bus);
1068 assert(reply);
1069 assert(u);
1070
1071 r = unit_get_tasks_current(u, &cn);
1072 if (r < 0 && r != -ENODATA)
1073 log_unit_warning_errno(u, r, "Failed to get pids.current attribute: %m");
1074
1075 return sd_bus_message_append(reply, "t", cn);
1076 }
1077
1078 static int property_get_cpu_usage(
1079 sd_bus *bus,
1080 const char *path,
1081 const char *interface,
1082 const char *property,
1083 sd_bus_message *reply,
1084 void *userdata,
1085 sd_bus_error *error) {
1086
1087 nsec_t ns = (nsec_t) -1;
1088 Unit *u = userdata;
1089 int r;
1090
1091 assert(bus);
1092 assert(reply);
1093 assert(u);
1094
1095 r = unit_get_cpu_usage(u, &ns);
1096 if (r < 0 && r != -ENODATA)
1097 log_unit_warning_errno(u, r, "Failed to get cpuacct.usage attribute: %m");
1098
1099 return sd_bus_message_append(reply, "t", ns);
1100 }
1101
1102 static int property_get_cpuset_cpus(
1103 sd_bus *bus,
1104 const char *path,
1105 const char *interface,
1106 const char *property,
1107 sd_bus_message *reply,
1108 void *userdata,
1109 sd_bus_error *error) {
1110
1111 Unit *u = userdata;
1112 _cleanup_(cpu_set_reset) CPUSet cpus = {};
1113 _cleanup_free_ uint8_t *array = NULL;
1114 size_t allocated;
1115
1116 assert(bus);
1117 assert(reply);
1118 assert(u);
1119
1120 (void) unit_get_cpuset(u, &cpus, "cpuset.cpus.effective");
1121 (void) cpu_set_to_dbus(&cpus, &array, &allocated);
1122 return sd_bus_message_append_array(reply, 'y', array, allocated);
1123 }
1124
1125 static int property_get_cpuset_mems(
1126 sd_bus *bus,
1127 const char *path,
1128 const char *interface,
1129 const char *property,
1130 sd_bus_message *reply,
1131 void *userdata,
1132 sd_bus_error *error) {
1133
1134 Unit *u = userdata;
1135 _cleanup_(cpu_set_reset) CPUSet mems = {};
1136 _cleanup_free_ uint8_t *array = NULL;
1137 size_t allocated;
1138
1139 assert(bus);
1140 assert(reply);
1141 assert(u);
1142
1143 (void) unit_get_cpuset(u, &mems, "cpuset.mems.effective");
1144 (void) cpu_set_to_dbus(&mems, &array, &allocated);
1145 return sd_bus_message_append_array(reply, 'y', array, allocated);
1146 }
1147
1148 static int property_get_cgroup(
1149 sd_bus *bus,
1150 const char *path,
1151 const char *interface,
1152 const char *property,
1153 sd_bus_message *reply,
1154 void *userdata,
1155 sd_bus_error *error) {
1156
1157 Unit *u = userdata;
1158 const char *t = NULL;
1159
1160 assert(bus);
1161 assert(reply);
1162 assert(u);
1163
1164 /* Three cases: a) u->cgroup_path is NULL, in which case the
1165 * unit has no control group, which we report as the empty
1166 * string. b) u->cgroup_path is the empty string, which
1167 * indicates the root cgroup, which we report as "/". c) all
1168 * other cases we report as-is. */
1169
1170 if (u->cgroup_path)
1171 t = empty_to_root(u->cgroup_path);
1172
1173 return sd_bus_message_append(reply, "s", t);
1174 }
1175
1176 static int append_process(sd_bus_message *reply, const char *p, pid_t pid, Set *pids) {
1177 _cleanup_free_ char *buf = NULL, *cmdline = NULL;
1178 int r;
1179
1180 assert(reply);
1181 assert(pid > 0);
1182
1183 r = set_put(pids, PID_TO_PTR(pid));
1184 if (IN_SET(r, 0, -EEXIST))
1185 return 0;
1186 if (r < 0)
1187 return r;
1188
1189 if (!p) {
1190 r = cg_pid_get_path(SYSTEMD_CGROUP_CONTROLLER, pid, &buf);
1191 if (r == -ESRCH)
1192 return 0;
1193 if (r < 0)
1194 return r;
1195
1196 p = buf;
1197 }
1198
1199 (void) get_process_cmdline(pid, SIZE_MAX, PROCESS_CMDLINE_COMM_FALLBACK, &cmdline);
1200
1201 return sd_bus_message_append(reply,
1202 "(sus)",
1203 p,
1204 (uint32_t) pid,
1205 cmdline);
1206 }
1207
1208 static int append_cgroup(sd_bus_message *reply, const char *p, Set *pids) {
1209 _cleanup_closedir_ DIR *d = NULL;
1210 _cleanup_fclose_ FILE *f = NULL;
1211 int r;
1212
1213 assert(reply);
1214 assert(p);
1215
1216 r = cg_enumerate_processes(SYSTEMD_CGROUP_CONTROLLER, p, &f);
1217 if (r == -ENOENT)
1218 return 0;
1219 if (r < 0)
1220 return r;
1221
1222 for (;;) {
1223 pid_t pid;
1224
1225 r = cg_read_pid(f, &pid);
1226 if (r < 0)
1227 return r;
1228 if (r == 0)
1229 break;
1230
1231 if (is_kernel_thread(pid) > 0)
1232 continue;
1233
1234 r = append_process(reply, p, pid, pids);
1235 if (r < 0)
1236 return r;
1237 }
1238
1239 r = cg_enumerate_subgroups(SYSTEMD_CGROUP_CONTROLLER, p, &d);
1240 if (r == -ENOENT)
1241 return 0;
1242 if (r < 0)
1243 return r;
1244
1245 for (;;) {
1246 _cleanup_free_ char *g = NULL, *j = NULL;
1247
1248 r = cg_read_subgroup(d, &g);
1249 if (r < 0)
1250 return r;
1251 if (r == 0)
1252 break;
1253
1254 j = path_join(empty_to_root(p), g);
1255 if (!j)
1256 return -ENOMEM;
1257
1258 r = append_cgroup(reply, j, pids);
1259 if (r < 0)
1260 return r;
1261 }
1262
1263 return 0;
1264 }
1265
1266 int bus_unit_method_get_processes(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1267 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
1268 _cleanup_set_free_ Set *pids = NULL;
1269 Unit *u = userdata;
1270 pid_t pid;
1271 int r;
1272
1273 assert(message);
1274
1275 r = mac_selinux_unit_access_check(u, message, "status", error);
1276 if (r < 0)
1277 return r;
1278
1279 pids = set_new(NULL);
1280 if (!pids)
1281 return -ENOMEM;
1282
1283 r = sd_bus_message_new_method_return(message, &reply);
1284 if (r < 0)
1285 return r;
1286
1287 r = sd_bus_message_open_container(reply, 'a', "(sus)");
1288 if (r < 0)
1289 return r;
1290
1291 if (u->cgroup_path) {
1292 r = append_cgroup(reply, u->cgroup_path, pids);
1293 if (r < 0)
1294 return r;
1295 }
1296
1297 /* The main and control pids might live outside of the cgroup, hence fetch them separately */
1298 pid = unit_main_pid(u);
1299 if (pid > 0) {
1300 r = append_process(reply, NULL, pid, pids);
1301 if (r < 0)
1302 return r;
1303 }
1304
1305 pid = unit_control_pid(u);
1306 if (pid > 0) {
1307 r = append_process(reply, NULL, pid, pids);
1308 if (r < 0)
1309 return r;
1310 }
1311
1312 r = sd_bus_message_close_container(reply);
1313 if (r < 0)
1314 return r;
1315
1316 return sd_bus_send(NULL, reply, NULL);
1317 }
1318
1319 static int property_get_ip_counter(
1320 sd_bus *bus,
1321 const char *path,
1322 const char *interface,
1323 const char *property,
1324 sd_bus_message *reply,
1325 void *userdata,
1326 sd_bus_error *error) {
1327
1328 static const char *const table[_CGROUP_IP_ACCOUNTING_METRIC_MAX] = {
1329 [CGROUP_IP_INGRESS_BYTES] = "IPIngressBytes",
1330 [CGROUP_IP_EGRESS_BYTES] = "IPEgressBytes",
1331 [CGROUP_IP_INGRESS_PACKETS] = "IPIngressPackets",
1332 [CGROUP_IP_EGRESS_PACKETS] = "IPEgressPackets",
1333 };
1334
1335 uint64_t value = UINT64_MAX;
1336 Unit *u = userdata;
1337 ssize_t metric;
1338
1339 assert(bus);
1340 assert(reply);
1341 assert(property);
1342 assert(u);
1343
1344 assert_se((metric = string_table_lookup(table, ELEMENTSOF(table), property)) >= 0);
1345 (void) unit_get_ip_accounting(u, metric, &value);
1346 return sd_bus_message_append(reply, "t", value);
1347 }
1348
1349 static int property_get_io_counter(
1350 sd_bus *bus,
1351 const char *path,
1352 const char *interface,
1353 const char *property,
1354 sd_bus_message *reply,
1355 void *userdata,
1356 sd_bus_error *error) {
1357
1358 static const char *const table[_CGROUP_IO_ACCOUNTING_METRIC_MAX] = {
1359 [CGROUP_IO_READ_BYTES] = "IOReadBytes",
1360 [CGROUP_IO_WRITE_BYTES] = "IOWriteBytes",
1361 [CGROUP_IO_READ_OPERATIONS] = "IOReadOperations",
1362 [CGROUP_IO_WRITE_OPERATIONS] = "IOWriteOperations",
1363 };
1364
1365 uint64_t value = UINT64_MAX;
1366 Unit *u = userdata;
1367 ssize_t metric;
1368
1369 assert(bus);
1370 assert(reply);
1371 assert(property);
1372 assert(u);
1373
1374 assert_se((metric = string_table_lookup(table, ELEMENTSOF(table), property)) >= 0);
1375 (void) unit_get_io_accounting(u, metric, false, &value);
1376 return sd_bus_message_append(reply, "t", value);
1377 }
1378
1379 int bus_unit_method_attach_processes(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1380
1381 _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL;
1382 _cleanup_set_free_ Set *pids = NULL;
1383 Unit *u = userdata;
1384 const char *path;
1385 int r;
1386
1387 assert(message);
1388
1389 /* This migrates the processes with the specified PIDs into the cgroup of this unit, optionally below a
1390 * specified cgroup path. Obviously this only works for units that actually maintain a cgroup
1391 * representation. If a process is already in the cgroup no operation is executed – in this case the specified
1392 * subcgroup path has no effect! */
1393
1394 r = mac_selinux_unit_access_check(u, message, "start", error);
1395 if (r < 0)
1396 return r;
1397
1398 r = sd_bus_message_read(message, "s", &path);
1399 if (r < 0)
1400 return r;
1401
1402 path = empty_to_null(path);
1403 if (path) {
1404 if (!path_is_absolute(path))
1405 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Control group path is not absolute: %s", path);
1406
1407 if (!path_is_normalized(path))
1408 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Control group path is not normalized: %s", path);
1409 }
1410
1411 if (!unit_cgroup_delegate(u))
1412 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Process migration not available on non-delegated units.");
1413
1414 if (UNIT_IS_INACTIVE_OR_FAILED(unit_active_state(u)))
1415 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Unit is not active, refusing.");
1416
1417 r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_EUID|SD_BUS_CREDS_PID, &creds);
1418 if (r < 0)
1419 return r;
1420
1421 r = sd_bus_message_enter_container(message, 'a', "u");
1422 if (r < 0)
1423 return r;
1424 for (;;) {
1425 uid_t process_uid, sender_uid;
1426 uint32_t upid;
1427 pid_t pid;
1428
1429 r = sd_bus_message_read(message, "u", &upid);
1430 if (r < 0)
1431 return r;
1432 if (r == 0)
1433 break;
1434
1435 if (upid == 0) {
1436 r = sd_bus_creds_get_pid(creds, &pid);
1437 if (r < 0)
1438 return r;
1439 } else
1440 pid = (uid_t) upid;
1441
1442 /* Filter out duplicates */
1443 if (set_contains(pids, PID_TO_PTR(pid)))
1444 continue;
1445
1446 /* Check if this process is suitable for attaching to this unit */
1447 r = unit_pid_attachable(u, pid, error);
1448 if (r < 0)
1449 return r;
1450
1451 /* Let's query the sender's UID, so that we can make our security decisions */
1452 r = sd_bus_creds_get_euid(creds, &sender_uid);
1453 if (r < 0)
1454 return r;
1455
1456 /* Let's validate security: if the sender is root, then all is OK. If the sender is any other unit,
1457 * then the process' UID and the target unit's UID have to match the sender's UID */
1458 if (sender_uid != 0 && sender_uid != getuid()) {
1459 r = get_process_uid(pid, &process_uid);
1460 if (r < 0)
1461 return sd_bus_error_set_errnof(error, r, "Failed to retrieve process UID: %m");
1462
1463 if (process_uid != sender_uid)
1464 return sd_bus_error_setf(error, SD_BUS_ERROR_ACCESS_DENIED, "Process " PID_FMT " not owned by client's UID. Refusing.", pid);
1465 if (process_uid != u->ref_uid)
1466 return sd_bus_error_setf(error, SD_BUS_ERROR_ACCESS_DENIED, "Process " PID_FMT " not owned by target unit's UID. Refusing.", pid);
1467 }
1468
1469 if (!pids) {
1470 pids = set_new(NULL);
1471 if (!pids)
1472 return -ENOMEM;
1473 }
1474
1475 r = set_put(pids, PID_TO_PTR(pid));
1476 if (r < 0)
1477 return r;
1478 }
1479
1480 r = sd_bus_message_exit_container(message);
1481 if (r < 0)
1482 return r;
1483
1484 r = unit_attach_pids_to_cgroup(u, pids, path);
1485 if (r < 0)
1486 return sd_bus_error_set_errnof(error, r, "Failed to attach processes to control group: %m");
1487
1488 return sd_bus_reply_method_return(message, NULL);
1489 }
1490
1491 const sd_bus_vtable bus_unit_cgroup_vtable[] = {
1492 SD_BUS_VTABLE_START(0),
1493 SD_BUS_PROPERTY("Slice", "s", property_get_slice, 0, 0),
1494 SD_BUS_PROPERTY("ControlGroup", "s", property_get_cgroup, 0, 0),
1495 SD_BUS_PROPERTY("MemoryCurrent", "t", property_get_current_memory, 0, 0),
1496 SD_BUS_PROPERTY("CPUUsageNSec", "t", property_get_cpu_usage, 0, 0),
1497 SD_BUS_PROPERTY("EffectiveCPUs", "ay", property_get_cpuset_cpus, 0, 0),
1498 SD_BUS_PROPERTY("EffectiveMemoryNodes", "ay", property_get_cpuset_mems, 0, 0),
1499 SD_BUS_PROPERTY("TasksCurrent", "t", property_get_current_tasks, 0, 0),
1500 SD_BUS_PROPERTY("IPIngressBytes", "t", property_get_ip_counter, 0, 0),
1501 SD_BUS_PROPERTY("IPIngressPackets", "t", property_get_ip_counter, 0, 0),
1502 SD_BUS_PROPERTY("IPEgressBytes", "t", property_get_ip_counter, 0, 0),
1503 SD_BUS_PROPERTY("IPEgressPackets", "t", property_get_ip_counter, 0, 0),
1504 SD_BUS_PROPERTY("IOReadBytes", "t", property_get_io_counter, 0, 0),
1505 SD_BUS_PROPERTY("IOReadOperations", "t", property_get_io_counter, 0, 0),
1506 SD_BUS_PROPERTY("IOWriteBytes", "t", property_get_io_counter, 0, 0),
1507 SD_BUS_PROPERTY("IOWriteOperations", "t", property_get_io_counter, 0, 0),
1508
1509 SD_BUS_METHOD_WITH_NAMES("GetProcesses",
1510 NULL,,
1511 "a(sus)",
1512 SD_BUS_PARAM(processes),
1513 bus_unit_method_get_processes,
1514 SD_BUS_VTABLE_UNPRIVILEGED),
1515
1516 SD_BUS_METHOD_WITH_NAMES("AttachProcesses",
1517 "sau",
1518 SD_BUS_PARAM(subcgroup)
1519 SD_BUS_PARAM(pids),
1520 NULL,,
1521 bus_unit_method_attach_processes,
1522 SD_BUS_VTABLE_UNPRIVILEGED),
1523
1524 SD_BUS_VTABLE_END
1525 };
1526
1527 static int send_new_signal(sd_bus *bus, void *userdata) {
1528 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
1529 _cleanup_free_ char *p = NULL;
1530 Unit *u = userdata;
1531 int r;
1532
1533 assert(bus);
1534 assert(u);
1535
1536 p = unit_dbus_path(u);
1537 if (!p)
1538 return -ENOMEM;
1539
1540 r = sd_bus_message_new_signal(
1541 bus,
1542 &m,
1543 "/org/freedesktop/systemd1",
1544 "org.freedesktop.systemd1.Manager",
1545 "UnitNew");
1546 if (r < 0)
1547 return r;
1548
1549 r = sd_bus_message_append(m, "so", u->id, p);
1550 if (r < 0)
1551 return r;
1552
1553 return sd_bus_send(bus, m, NULL);
1554 }
1555
1556 static int send_changed_signal(sd_bus *bus, void *userdata) {
1557 _cleanup_free_ char *p = NULL;
1558 Unit *u = userdata;
1559 int r;
1560
1561 assert(bus);
1562 assert(u);
1563
1564 p = unit_dbus_path(u);
1565 if (!p)
1566 return -ENOMEM;
1567
1568 /* Send a properties changed signal. First for the specific
1569 * type, then for the generic unit. The clients may rely on
1570 * this order to get atomic behavior if needed. */
1571
1572 r = sd_bus_emit_properties_changed_strv(
1573 bus, p,
1574 unit_dbus_interface_from_type(u->type),
1575 NULL);
1576 if (r < 0)
1577 return r;
1578
1579 return sd_bus_emit_properties_changed_strv(
1580 bus, p,
1581 "org.freedesktop.systemd1.Unit",
1582 NULL);
1583 }
1584
1585 void bus_unit_send_change_signal(Unit *u) {
1586 int r;
1587 assert(u);
1588
1589 if (u->in_dbus_queue) {
1590 LIST_REMOVE(dbus_queue, u->manager->dbus_unit_queue, u);
1591 u->in_dbus_queue = false;
1592 }
1593
1594 if (!u->id)
1595 return;
1596
1597 r = bus_foreach_bus(u->manager, u->bus_track, u->sent_dbus_new_signal ? send_changed_signal : send_new_signal, u);
1598 if (r < 0)
1599 log_unit_debug_errno(u, r, "Failed to send unit change signal for %s: %m", u->id);
1600
1601 u->sent_dbus_new_signal = true;
1602 }
1603
1604 void bus_unit_send_pending_change_signal(Unit *u, bool including_new) {
1605
1606 /* Sends out any pending change signals, but only if they really are pending. This call is used when we are
1607 * about to change state in order to force out a PropertiesChanged signal beforehand if there was one pending
1608 * so that clients can follow the full state transition */
1609
1610 if (!u->in_dbus_queue) /* If not enqueued, don't bother */
1611 return;
1612
1613 if (!u->sent_dbus_new_signal && !including_new) /* If the unit was never announced, don't bother, it's fine if
1614 * the unit appears in the new state right-away (except if the
1615 * caller explicitly asked us to send it anyway) */
1616 return;
1617
1618 if (MANAGER_IS_RELOADING(u->manager)) /* Don't generate unnecessary PropertiesChanged signals for the same unit
1619 * when we are reloading. */
1620 return;
1621
1622 bus_unit_send_change_signal(u);
1623 }
1624
1625 int bus_unit_send_pending_freezer_message(Unit *u) {
1626 int r;
1627
1628 assert(u);
1629
1630 if (!u->pending_freezer_message)
1631 return 0;
1632
1633 r = sd_bus_send(NULL, u->pending_freezer_message, NULL);
1634 if (r < 0)
1635 log_warning_errno(r, "Failed to send queued message, ignoring: %m");
1636
1637 u->pending_freezer_message = sd_bus_message_unref(u->pending_freezer_message);
1638
1639 return 0;
1640 }
1641
1642 static int send_removed_signal(sd_bus *bus, void *userdata) {
1643 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
1644 _cleanup_free_ char *p = NULL;
1645 Unit *u = userdata;
1646 int r;
1647
1648 assert(bus);
1649 assert(u);
1650
1651 p = unit_dbus_path(u);
1652 if (!p)
1653 return -ENOMEM;
1654
1655 r = sd_bus_message_new_signal(
1656 bus,
1657 &m,
1658 "/org/freedesktop/systemd1",
1659 "org.freedesktop.systemd1.Manager",
1660 "UnitRemoved");
1661 if (r < 0)
1662 return r;
1663
1664 r = sd_bus_message_append(m, "so", u->id, p);
1665 if (r < 0)
1666 return r;
1667
1668 return sd_bus_send(bus, m, NULL);
1669 }
1670
1671 void bus_unit_send_removed_signal(Unit *u) {
1672 int r;
1673 assert(u);
1674
1675 if (!u->sent_dbus_new_signal || u->in_dbus_queue)
1676 bus_unit_send_change_signal(u);
1677
1678 if (!u->id)
1679 return;
1680
1681 r = bus_foreach_bus(u->manager, u->bus_track, send_removed_signal, u);
1682 if (r < 0)
1683 log_unit_debug_errno(u, r, "Failed to send unit remove signal for %s: %m", u->id);
1684 }
1685
1686 int bus_unit_queue_job(
1687 sd_bus_message *message,
1688 Unit *u,
1689 JobType type,
1690 JobMode mode,
1691 BusUnitQueueFlags flags,
1692 sd_bus_error *error) {
1693
1694 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
1695 _cleanup_free_ char *job_path = NULL, *unit_path = NULL;
1696 _cleanup_set_free_ Set *affected = NULL;
1697 Job *j, *a;
1698 int r;
1699
1700 assert(message);
1701 assert(u);
1702 assert(type >= 0 && type < _JOB_TYPE_MAX);
1703 assert(mode >= 0 && mode < _JOB_MODE_MAX);
1704
1705 r = mac_selinux_unit_access_check(
1706 u, message,
1707 job_type_to_access_method(type),
1708 error);
1709 if (r < 0)
1710 return r;
1711
1712 if (FLAGS_SET(flags, BUS_UNIT_QUEUE_RELOAD_IF_POSSIBLE) && unit_can_reload(u)) {
1713 if (type == JOB_RESTART)
1714 type = JOB_RELOAD_OR_START;
1715 else if (type == JOB_TRY_RESTART)
1716 type = JOB_TRY_RELOAD;
1717 }
1718
1719 if (type == JOB_STOP &&
1720 IN_SET(u->load_state, UNIT_NOT_FOUND, UNIT_ERROR, UNIT_BAD_SETTING) &&
1721 unit_active_state(u) == UNIT_INACTIVE)
1722 return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_UNIT, "Unit %s not loaded.", u->id);
1723
1724 if ((type == JOB_START && u->refuse_manual_start) ||
1725 (type == JOB_STOP && u->refuse_manual_stop) ||
1726 (IN_SET(type, JOB_RESTART, JOB_TRY_RESTART) && (u->refuse_manual_start || u->refuse_manual_stop)) ||
1727 (type == JOB_RELOAD_OR_START && job_type_collapse(type, u) == JOB_START && u->refuse_manual_start))
1728 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);
1729
1730 if (FLAGS_SET(flags, BUS_UNIT_QUEUE_VERBOSE_REPLY)) {
1731 affected = set_new(NULL);
1732 if (!affected)
1733 return -ENOMEM;
1734 }
1735
1736 r = manager_add_job(u->manager, type, u, mode, affected, error, &j);
1737 if (r < 0)
1738 return r;
1739
1740 r = bus_job_track_sender(j, message);
1741 if (r < 0)
1742 return r;
1743
1744 /* Before we send the method reply, force out the announcement JobNew for this job */
1745 bus_job_send_pending_change_signal(j, true);
1746
1747 job_path = job_dbus_path(j);
1748 if (!job_path)
1749 return -ENOMEM;
1750
1751 /* The classic response is just a job object path */
1752 if (!FLAGS_SET(flags, BUS_UNIT_QUEUE_VERBOSE_REPLY))
1753 return sd_bus_reply_method_return(message, "o", job_path);
1754
1755 /* In verbose mode respond with the anchor job plus everything that has been affected */
1756 r = sd_bus_message_new_method_return(message, &reply);
1757 if (r < 0)
1758 return r;
1759
1760 unit_path = unit_dbus_path(j->unit);
1761 if (!unit_path)
1762 return -ENOMEM;
1763
1764 r = sd_bus_message_append(reply, "uosos",
1765 j->id, job_path,
1766 j->unit->id, unit_path,
1767 job_type_to_string(j->type));
1768 if (r < 0)
1769 return r;
1770
1771 r = sd_bus_message_open_container(reply, 'a', "(uosos)");
1772 if (r < 0)
1773 return r;
1774
1775 SET_FOREACH(a, affected) {
1776
1777 if (a->id == j->id)
1778 continue;
1779
1780 /* Free paths from previous iteration */
1781 job_path = mfree(job_path);
1782 unit_path = mfree(unit_path);
1783
1784 job_path = job_dbus_path(a);
1785 if (!job_path)
1786 return -ENOMEM;
1787
1788 unit_path = unit_dbus_path(a->unit);
1789 if (!unit_path)
1790 return -ENOMEM;
1791
1792 r = sd_bus_message_append(reply, "(uosos)",
1793 a->id, job_path,
1794 a->unit->id, unit_path,
1795 job_type_to_string(a->type));
1796 if (r < 0)
1797 return r;
1798 }
1799
1800 r = sd_bus_message_close_container(reply);
1801 if (r < 0)
1802 return r;
1803
1804 return sd_bus_send(NULL, reply, NULL);
1805 }
1806
1807 static int bus_unit_set_live_property(
1808 Unit *u,
1809 const char *name,
1810 sd_bus_message *message,
1811 UnitWriteFlags flags,
1812 sd_bus_error *error) {
1813
1814 int r;
1815
1816 assert(u);
1817 assert(name);
1818 assert(message);
1819
1820 /* Handles setting properties both "live" (i.e. at any time during runtime), and during creation (for transient
1821 * units that are being created). */
1822
1823 if (streq(name, "Description")) {
1824 const char *d;
1825
1826 r = sd_bus_message_read(message, "s", &d);
1827 if (r < 0)
1828 return r;
1829
1830 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
1831 r = unit_set_description(u, d);
1832 if (r < 0)
1833 return r;
1834
1835 unit_write_settingf(u, flags|UNIT_ESCAPE_SPECIFIERS, name, "Description=%s", d);
1836 }
1837
1838 return 1;
1839 }
1840
1841 return 0;
1842 }
1843
1844 static int bus_set_transient_emergency_action(
1845 Unit *u,
1846 const char *name,
1847 EmergencyAction *p,
1848 sd_bus_message *message,
1849 UnitWriteFlags flags,
1850 sd_bus_error *error) {
1851
1852 const char *s;
1853 EmergencyAction v;
1854 int r;
1855 bool system;
1856
1857 assert(p);
1858
1859 r = sd_bus_message_read(message, "s", &s);
1860 if (r < 0)
1861 return r;
1862
1863 system = MANAGER_IS_SYSTEM(u->manager);
1864 r = parse_emergency_action(s, system, &v);
1865 if (r < 0)
1866 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS,
1867 r == -EOPNOTSUPP ? "%s setting invalid for manager type: %s"
1868 : "Invalid %s setting: %s",
1869 name, s);
1870
1871 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
1872 *p = v;
1873 unit_write_settingf(u, flags, name,
1874 "%s=%s", name, s);
1875 }
1876
1877 return 1;
1878 }
1879
1880 static int bus_set_transient_exit_status(
1881 Unit *u,
1882 const char *name,
1883 int *p,
1884 sd_bus_message *message,
1885 UnitWriteFlags flags,
1886 sd_bus_error *error) {
1887
1888 int32_t k;
1889 int r;
1890
1891 assert(p);
1892
1893 r = sd_bus_message_read(message, "i", &k);
1894 if (r < 0)
1895 return r;
1896
1897 if (k > 255)
1898 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Exit status must be in range 0…255 or negative.");
1899
1900 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
1901 *p = k < 0 ? -1 : k;
1902
1903 if (k < 0)
1904 unit_write_settingf(u, flags, name, "%s=", name);
1905 else
1906 unit_write_settingf(u, flags, name, "%s=%i", name, k);
1907 }
1908
1909 return 1;
1910 }
1911
1912 static BUS_DEFINE_SET_TRANSIENT_PARSE(collect_mode, CollectMode, collect_mode_from_string);
1913 static BUS_DEFINE_SET_TRANSIENT_PARSE(job_mode, JobMode, job_mode_from_string);
1914
1915 static int bus_set_transient_conditions(
1916 Unit *u,
1917 const char *name,
1918 Condition **list,
1919 bool is_condition,
1920 sd_bus_message *message,
1921 UnitWriteFlags flags,
1922 sd_bus_error *error) {
1923
1924 const char *type_name, *param;
1925 int trigger, negate, r;
1926 bool empty = true;
1927
1928 assert(list);
1929
1930 r = sd_bus_message_enter_container(message, 'a', "(sbbs)");
1931 if (r < 0)
1932 return r;
1933
1934 while ((r = sd_bus_message_read(message, "(sbbs)", &type_name, &trigger, &negate, &param)) > 0) {
1935 ConditionType t;
1936
1937 t = is_condition ? condition_type_from_string(type_name) : assert_type_from_string(type_name);
1938 if (t < 0)
1939 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid condition type: %s", type_name);
1940
1941 if (isempty(param))
1942 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Condition parameter in %s is empty", type_name);
1943
1944 if (condition_takes_path(t) && !path_is_absolute(param))
1945 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Path in condition %s is not absolute: %s", type_name, param);
1946
1947 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
1948 Condition *c;
1949
1950 c = condition_new(t, param, trigger, negate);
1951 if (!c)
1952 return -ENOMEM;
1953
1954 LIST_PREPEND(conditions, *list, c);
1955
1956 unit_write_settingf(u, flags|UNIT_ESCAPE_SPECIFIERS, name,
1957 "%s=%s%s%s", type_name,
1958 trigger ? "|" : "", negate ? "!" : "", param);
1959 }
1960
1961 empty = false;
1962 }
1963 if (r < 0)
1964 return r;
1965
1966 r = sd_bus_message_exit_container(message);
1967 if (r < 0)
1968 return r;
1969
1970 if (!UNIT_WRITE_FLAGS_NOOP(flags) && empty) {
1971 *list = condition_free_list(*list);
1972 unit_write_settingf(u, flags, name, "%sNull=", is_condition ? "Condition" : "Assert");
1973 }
1974
1975 return 1;
1976 }
1977
1978 static int bus_unit_set_transient_property(
1979 Unit *u,
1980 const char *name,
1981 sd_bus_message *message,
1982 UnitWriteFlags flags,
1983 sd_bus_error *error) {
1984
1985 UnitDependency d = _UNIT_DEPENDENCY_INVALID;
1986 int r;
1987
1988 assert(u);
1989 assert(name);
1990 assert(message);
1991
1992 /* Handles settings when transient units are created. This settings cannot be altered anymore after the unit
1993 * has been created. */
1994
1995 if (streq(name, "SourcePath"))
1996 return bus_set_transient_path(u, name, &u->source_path, message, flags, error);
1997
1998 if (streq(name, "StopWhenUnneeded"))
1999 return bus_set_transient_bool(u, name, &u->stop_when_unneeded, message, flags, error);
2000
2001 if (streq(name, "RefuseManualStart"))
2002 return bus_set_transient_bool(u, name, &u->refuse_manual_start, message, flags, error);
2003
2004 if (streq(name, "RefuseManualStop"))
2005 return bus_set_transient_bool(u, name, &u->refuse_manual_stop, message, flags, error);
2006
2007 if (streq(name, "AllowIsolate"))
2008 return bus_set_transient_bool(u, name, &u->allow_isolate, message, flags, error);
2009
2010 if (streq(name, "DefaultDependencies"))
2011 return bus_set_transient_bool(u, name, &u->default_dependencies, message, flags, error);
2012
2013 if (streq(name, "OnFailureJobMode"))
2014 return bus_set_transient_job_mode(u, name, &u->on_failure_job_mode, message, flags, error);
2015
2016 if (streq(name, "IgnoreOnIsolate"))
2017 return bus_set_transient_bool(u, name, &u->ignore_on_isolate, message, flags, error);
2018
2019 if (streq(name, "JobTimeoutUSec")) {
2020 r = bus_set_transient_usec_fix_0(u, name, &u->job_timeout, message, flags, error);
2021 if (r >= 0 && !UNIT_WRITE_FLAGS_NOOP(flags) && !u->job_running_timeout_set)
2022 u->job_running_timeout = u->job_timeout;
2023 }
2024
2025 if (streq(name, "JobRunningTimeoutUSec")) {
2026 r = bus_set_transient_usec_fix_0(u, name, &u->job_running_timeout, message, flags, error);
2027 if (r >= 0 && !UNIT_WRITE_FLAGS_NOOP(flags))
2028 u->job_running_timeout_set = true;
2029
2030 return r;
2031 }
2032
2033 if (streq(name, "JobTimeoutAction"))
2034 return bus_set_transient_emergency_action(u, name, &u->job_timeout_action, message, flags, error);
2035
2036 if (streq(name, "JobTimeoutRebootArgument"))
2037 return bus_set_transient_string(u, name, &u->job_timeout_reboot_arg, message, flags, error);
2038
2039 if (streq(name, "StartLimitIntervalUSec"))
2040 return bus_set_transient_usec(u, name, &u->start_ratelimit.interval, message, flags, error);
2041
2042 if (streq(name, "StartLimitBurst"))
2043 return bus_set_transient_unsigned(u, name, &u->start_ratelimit.burst, message, flags, error);
2044
2045 if (streq(name, "StartLimitAction"))
2046 return bus_set_transient_emergency_action(u, name, &u->start_limit_action, message, flags, error);
2047
2048 if (streq(name, "FailureAction"))
2049 return bus_set_transient_emergency_action(u, name, &u->failure_action, message, flags, error);
2050
2051 if (streq(name, "SuccessAction"))
2052 return bus_set_transient_emergency_action(u, name, &u->success_action, message, flags, error);
2053
2054 if (streq(name, "FailureActionExitStatus"))
2055 return bus_set_transient_exit_status(u, name, &u->failure_action_exit_status, message, flags, error);
2056
2057 if (streq(name, "SuccessActionExitStatus"))
2058 return bus_set_transient_exit_status(u, name, &u->success_action_exit_status, message, flags, error);
2059
2060 if (streq(name, "RebootArgument"))
2061 return bus_set_transient_string(u, name, &u->reboot_arg, message, flags, error);
2062
2063 if (streq(name, "CollectMode"))
2064 return bus_set_transient_collect_mode(u, name, &u->collect_mode, message, flags, error);
2065
2066 if (streq(name, "Conditions"))
2067 return bus_set_transient_conditions(u, name, &u->conditions, true, message, flags, error);
2068
2069 if (streq(name, "Asserts"))
2070 return bus_set_transient_conditions(u, name, &u->asserts, false, message, flags, error);
2071
2072 if (streq(name, "Documentation")) {
2073 _cleanup_strv_free_ char **l = NULL;
2074 char **p;
2075
2076 r = sd_bus_message_read_strv(message, &l);
2077 if (r < 0)
2078 return r;
2079
2080 STRV_FOREACH(p, l) {
2081 if (!documentation_url_is_valid(*p))
2082 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid URL in %s: %s", name, *p);
2083 }
2084
2085 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
2086 if (strv_isempty(l)) {
2087 u->documentation = strv_free(u->documentation);
2088 unit_write_settingf(u, flags, name, "%s=", name);
2089 } else {
2090 strv_extend_strv(&u->documentation, l, false);
2091
2092 STRV_FOREACH(p, l)
2093 unit_write_settingf(u, flags, name, "%s=%s", name, *p);
2094 }
2095 }
2096
2097 return 1;
2098
2099 } else if (streq(name, "Slice")) {
2100 Unit *slice;
2101 const char *s;
2102
2103 if (!UNIT_HAS_CGROUP_CONTEXT(u))
2104 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "The slice property is only available for units with control groups.");
2105 if (u->type == UNIT_SLICE)
2106 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Slice may not be set for slice units.");
2107 if (unit_has_name(u, SPECIAL_INIT_SCOPE))
2108 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Cannot set slice for init.scope");
2109
2110 r = sd_bus_message_read(message, "s", &s);
2111 if (r < 0)
2112 return r;
2113
2114 if (!unit_name_is_valid(s, UNIT_NAME_PLAIN))
2115 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid unit name '%s'", s);
2116
2117 /* Note that we do not dispatch the load queue here yet, as we don't want our own transient unit to be
2118 * loaded while we are still setting it up. Or in other words, we use manager_load_unit_prepare()
2119 * instead of manager_load_unit() on purpose, here. */
2120 r = manager_load_unit_prepare(u->manager, s, NULL, error, &slice);
2121 if (r < 0)
2122 return r;
2123
2124 if (slice->type != UNIT_SLICE)
2125 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Unit name '%s' is not a slice", s);
2126
2127 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
2128 r = unit_set_slice(u, slice);
2129 if (r < 0)
2130 return r;
2131
2132 unit_write_settingf(u, flags|UNIT_PRIVATE, name, "Slice=%s", s);
2133 }
2134
2135 return 1;
2136
2137 } else if (streq(name, "RequiresMountsFor")) {
2138 _cleanup_strv_free_ char **l = NULL;
2139 char **p;
2140
2141 r = sd_bus_message_read_strv(message, &l);
2142 if (r < 0)
2143 return r;
2144
2145 STRV_FOREACH(p, l) {
2146 path_simplify(*p, true);
2147
2148 if (!path_is_absolute(*p))
2149 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Path specified in %s is not absolute: %s", name, *p);
2150
2151 if (!path_is_valid(*p))
2152 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Path specified in %s has invalid length: %s", name, *p);
2153
2154 if (!path_is_normalized(*p))
2155 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Path specified in %s is not normalized: %s", name, *p);
2156
2157 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
2158 r = unit_require_mounts_for(u, *p, UNIT_DEPENDENCY_FILE);
2159 if (r < 0)
2160 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Failed to add required mount \"%s\": %m", *p);
2161
2162 unit_write_settingf(u, flags, name, "%s=%s", name, *p);
2163 }
2164 }
2165
2166 return 1;
2167 }
2168
2169 if (streq(name, "RequiresOverridable"))
2170 d = UNIT_REQUIRES; /* redirect for obsolete unit dependency type */
2171 else if (streq(name, "RequisiteOverridable"))
2172 d = UNIT_REQUISITE; /* same here */
2173 else
2174 d = unit_dependency_from_string(name);
2175
2176 if (d >= 0) {
2177 const char *other;
2178
2179 if (!IN_SET(d,
2180 UNIT_REQUIRES,
2181 UNIT_REQUISITE,
2182 UNIT_WANTS,
2183 UNIT_BINDS_TO,
2184 UNIT_PART_OF,
2185 UNIT_CONFLICTS,
2186 UNIT_BEFORE,
2187 UNIT_AFTER,
2188 UNIT_ON_FAILURE,
2189 UNIT_PROPAGATES_RELOAD_TO,
2190 UNIT_RELOAD_PROPAGATED_FROM,
2191 UNIT_JOINS_NAMESPACE_OF))
2192 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Dependency type %s may not be created transiently.", unit_dependency_to_string(d));
2193
2194 r = sd_bus_message_enter_container(message, 'a', "s");
2195 if (r < 0)
2196 return r;
2197
2198 while ((r = sd_bus_message_read(message, "s", &other)) > 0) {
2199 if (!unit_name_is_valid(other, UNIT_NAME_PLAIN|UNIT_NAME_INSTANCE))
2200 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid unit name %s", other);
2201
2202 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
2203 _cleanup_free_ char *label = NULL;
2204
2205 r = unit_add_dependency_by_name(u, d, other, true, UNIT_DEPENDENCY_FILE);
2206 if (r < 0)
2207 return r;
2208
2209 label = strjoin(name, "-", other);
2210 if (!label)
2211 return -ENOMEM;
2212
2213 unit_write_settingf(u, flags, label, "%s=%s", unit_dependency_to_string(d), other);
2214 }
2215
2216 }
2217 if (r < 0)
2218 return r;
2219
2220 r = sd_bus_message_exit_container(message);
2221 if (r < 0)
2222 return r;
2223
2224 return 1;
2225
2226 } else if (streq(name, "AddRef")) {
2227
2228 int b;
2229
2230 /* Why is this called "AddRef" rather than just "Ref", or "Reference"? There's already a "Ref()" method
2231 * on the Unit interface, and it's probably not a good idea to expose a property and a method on the
2232 * same interface (well, strictly speaking AddRef isn't exposed as full property, we just read it for
2233 * transient units, but still). And "References" and "ReferencedBy" is already used as unit reference
2234 * dependency type, hence let's not confuse things with that.
2235 *
2236 * Note that we don't actually add the reference to the bus track. We do that only after the setup of
2237 * the transient unit is complete, so that setting this property multiple times in the same transient
2238 * unit creation call doesn't count as individual references. */
2239
2240 r = sd_bus_message_read(message, "b", &b);
2241 if (r < 0)
2242 return r;
2243
2244 if (!UNIT_WRITE_FLAGS_NOOP(flags))
2245 u->bus_track_add = b;
2246
2247 return 1;
2248 }
2249
2250 return 0;
2251 }
2252
2253 int bus_unit_set_properties(
2254 Unit *u,
2255 sd_bus_message *message,
2256 UnitWriteFlags flags,
2257 bool commit,
2258 sd_bus_error *error) {
2259
2260 bool for_real = false;
2261 unsigned n = 0;
2262 int r;
2263
2264 assert(u);
2265 assert(message);
2266
2267 /* We iterate through the array twice. First run we just check
2268 * if all passed data is valid, second run actually applies
2269 * it. This is to implement transaction-like behaviour without
2270 * actually providing full transactions. */
2271
2272 r = sd_bus_message_enter_container(message, 'a', "(sv)");
2273 if (r < 0)
2274 return r;
2275
2276 for (;;) {
2277 const char *name;
2278 UnitWriteFlags f;
2279
2280 r = sd_bus_message_enter_container(message, 'r', "sv");
2281 if (r < 0)
2282 return r;
2283 if (r == 0) {
2284 if (for_real || UNIT_WRITE_FLAGS_NOOP(flags))
2285 break;
2286
2287 /* Reached EOF. Let's try again, and this time for realz... */
2288 r = sd_bus_message_rewind(message, false);
2289 if (r < 0)
2290 return r;
2291
2292 for_real = true;
2293 continue;
2294 }
2295
2296 r = sd_bus_message_read(message, "s", &name);
2297 if (r < 0)
2298 return r;
2299
2300 if (!UNIT_VTABLE(u)->bus_set_property)
2301 return sd_bus_error_setf(error, SD_BUS_ERROR_PROPERTY_READ_ONLY, "Objects of this type do not support setting properties.");
2302
2303 r = sd_bus_message_enter_container(message, 'v', NULL);
2304 if (r < 0)
2305 return r;
2306
2307 /* If not for real, then mask out the two target flags */
2308 f = for_real ? flags : (flags & ~(UNIT_RUNTIME|UNIT_PERSISTENT));
2309
2310 r = UNIT_VTABLE(u)->bus_set_property(u, name, message, f, error);
2311 if (r == 0 && u->transient && u->load_state == UNIT_STUB)
2312 r = bus_unit_set_transient_property(u, name, message, f, error);
2313 if (r == 0)
2314 r = bus_unit_set_live_property(u, name, message, f, error);
2315 if (r < 0)
2316 return r;
2317
2318 if (r == 0)
2319 return sd_bus_error_setf(error, SD_BUS_ERROR_PROPERTY_READ_ONLY, "Cannot set property %s, or unknown property.", name);
2320
2321 r = sd_bus_message_exit_container(message);
2322 if (r < 0)
2323 return r;
2324
2325 r = sd_bus_message_exit_container(message);
2326 if (r < 0)
2327 return r;
2328
2329 n += for_real;
2330 }
2331
2332 r = sd_bus_message_exit_container(message);
2333 if (r < 0)
2334 return r;
2335
2336 if (commit && n > 0 && UNIT_VTABLE(u)->bus_commit_properties)
2337 UNIT_VTABLE(u)->bus_commit_properties(u);
2338
2339 return n;
2340 }
2341
2342 int bus_unit_validate_load_state(Unit *u, sd_bus_error *error) {
2343 assert(u);
2344
2345 /* Generates a pretty error if a unit isn't properly loaded. */
2346
2347 switch (u->load_state) {
2348
2349 case UNIT_LOADED:
2350 return 0;
2351
2352 case UNIT_NOT_FOUND:
2353 return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_UNIT, "Unit %s not found.", u->id);
2354
2355 case UNIT_BAD_SETTING:
2356 return sd_bus_error_setf(error, BUS_ERROR_BAD_UNIT_SETTING, "Unit %s has a bad unit file setting.", u->id);
2357
2358 case UNIT_ERROR: /* Only show .load_error in UNIT_ERROR state */
2359 return sd_bus_error_set_errnof(error, u->load_error, "Unit %s failed to load properly: %m.", u->id);
2360
2361 case UNIT_MASKED:
2362 return sd_bus_error_setf(error, BUS_ERROR_UNIT_MASKED, "Unit %s is masked.", u->id);
2363
2364 case UNIT_STUB:
2365 case UNIT_MERGED:
2366 default:
2367 return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_UNIT, "Unexpected load state of unit %s", u->id);
2368 }
2369 }
2370
2371 static int bus_unit_track_handler(sd_bus_track *t, void *userdata) {
2372 Unit *u = userdata;
2373
2374 assert(t);
2375 assert(u);
2376
2377 u->bus_track = sd_bus_track_unref(u->bus_track); /* make sure we aren't called again */
2378
2379 /* If the client that tracks us disappeared, then there's reason to believe that the cgroup is empty now too,
2380 * let's see */
2381 unit_add_to_cgroup_empty_queue(u);
2382
2383 /* Also add the unit to the GC queue, after all if the client left it might be time to GC this unit */
2384 unit_add_to_gc_queue(u);
2385
2386 return 0;
2387 }
2388
2389 static int bus_unit_allocate_bus_track(Unit *u) {
2390 int r;
2391
2392 assert(u);
2393
2394 if (u->bus_track)
2395 return 0;
2396
2397 r = sd_bus_track_new(u->manager->api_bus, &u->bus_track, bus_unit_track_handler, u);
2398 if (r < 0)
2399 return r;
2400
2401 r = sd_bus_track_set_recursive(u->bus_track, true);
2402 if (r < 0) {
2403 u->bus_track = sd_bus_track_unref(u->bus_track);
2404 return r;
2405 }
2406
2407 return 0;
2408 }
2409
2410 int bus_unit_track_add_name(Unit *u, const char *name) {
2411 int r;
2412
2413 assert(u);
2414
2415 r = bus_unit_allocate_bus_track(u);
2416 if (r < 0)
2417 return r;
2418
2419 return sd_bus_track_add_name(u->bus_track, name);
2420 }
2421
2422 int bus_unit_track_add_sender(Unit *u, sd_bus_message *m) {
2423 int r;
2424
2425 assert(u);
2426
2427 r = bus_unit_allocate_bus_track(u);
2428 if (r < 0)
2429 return r;
2430
2431 return sd_bus_track_add_sender(u->bus_track, m);
2432 }
2433
2434 int bus_unit_track_remove_sender(Unit *u, sd_bus_message *m) {
2435 assert(u);
2436
2437 /* If we haven't allocated the bus track object yet, then there's definitely no reference taken yet, return an
2438 * error */
2439 if (!u->bus_track)
2440 return -EUNATCH;
2441
2442 return sd_bus_track_remove_sender(u->bus_track, m);
2443 }