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