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