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