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