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