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