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