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