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