Debian Codegraph @

src/core/dbus-unit.c

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