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