]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/core/dbus-unit.c
core: before returning new job path to clients, force out JobNew signals
[thirdparty/systemd.git] / src / core / dbus-unit.c
CommitLineData
53e1b683 1/* SPDX-License-Identifier: LGPL-2.1+ */
a7334b09 2
718db961 3#include "sd-bus.h"
07630cea 4
b5efdb8a 5#include "alloc-util.h"
906c06f6 6#include "bpf-firewall.h"
07630cea
LP
7#include "bus-common-errors.h"
8#include "cgroup-util.h"
faa781cf 9#include "condition.h"
c5a97ed1 10#include "dbus-job.h"
8752c575 11#include "dbus-unit.h"
faa781cf 12#include "dbus-util.h"
07630cea 13#include "dbus.h"
291d565a 14#include "fd-util.h"
8752c575 15#include "locale-util.h"
ea430986 16#include "log.h"
faa781cf 17#include "path-util.h"
291d565a 18#include "process-util.h"
e2417e41 19#include "selinux-access.h"
6eb7c172 20#include "signal-util.h"
efdb0237 21#include "special.h"
07630cea
LP
22#include "string-util.h"
23#include "strv.h"
ee104e11 24#include "user-util.h"
faa781cf 25#include "web-util.h"
718db961 26
874bd264
YW
27static bool unit_can_start_refuse_manual(Unit *u) {
28 return unit_can_start(u) && !u->refuse_manual_start;
29}
30
31static bool unit_can_stop_refuse_manual(Unit *u) {
32 return unit_can_stop(u) && !u->refuse_manual_stop;
33}
34
35static bool unit_can_isolate_refuse_manual(Unit *u) {
36 return unit_can_isolate(u) && !u->refuse_manual_start;
37}
38
5afe510c 39static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_collect_mode, collect_mode, CollectMode);
718db961 40static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_load_state, unit_load_state, UnitLoadState);
d420282b 41static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_job_mode, job_mode, JobMode);
87a47f99 42static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_emergency_action, emergency_action, EmergencyAction);
cb7f88fc
YW
43static BUS_DEFINE_PROPERTY_GET(property_get_description, "s", Unit, unit_description);
44static BUS_DEFINE_PROPERTY_GET2(property_get_active_state, "s", Unit, unit_active_state, unit_active_state_to_string);
45static BUS_DEFINE_PROPERTY_GET(property_get_sub_state, "s", Unit, unit_sub_state_to_string);
46static BUS_DEFINE_PROPERTY_GET2(property_get_unit_file_state, "s", Unit, unit_get_unit_file_state, unit_file_state_to_string);
47static BUS_DEFINE_PROPERTY_GET(property_get_can_reload, "b", Unit, unit_can_reload);
874bd264
YW
48static BUS_DEFINE_PROPERTY_GET(property_get_can_start, "b", Unit, unit_can_start_refuse_manual);
49static BUS_DEFINE_PROPERTY_GET(property_get_can_stop, "b", Unit, unit_can_stop_refuse_manual);
50static BUS_DEFINE_PROPERTY_GET(property_get_can_isolate, "b", Unit, unit_can_isolate_refuse_manual);
cb7f88fc 51static BUS_DEFINE_PROPERTY_GET(property_get_need_daemon_reload, "b", Unit, unit_need_daemon_reload);
92c23c5a 52static BUS_DEFINE_PROPERTY_GET_GLOBAL(property_get_empty_strv, "as", 0);
718db961
LP
53
54static 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,
ebcf1f97
LP
60 void *userdata,
61 sd_bus_error *error) {
718db961 62
14f7edb0 63 Set **s = userdata;
718db961
LP
64 Iterator i;
65 const char *t;
66 int r;
ea430986 67
718db961
LP
68 assert(bus);
69 assert(reply);
14f7edb0 70 assert(s);
4139c1b2 71
718db961
LP
72 r = sd_bus_message_open_container(reply, 'a', "s");
73 if (r < 0)
74 return r;
4139c1b2 75
14f7edb0 76 SET_FOREACH(t, *s, i) {
718db961
LP
77 r = sd_bus_message_append(reply, "s", t);
78 if (r < 0)
79 return r;
80 }
4139c1b2 81
718db961 82 return sd_bus_message_close_container(reply);
4139c1b2
LP
83}
84
718db961
LP
85static 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,
ebcf1f97
LP
91 void *userdata,
92 sd_bus_error *error) {
718db961
LP
93
94 Unit *u = userdata, *f;
8fe914ec 95
718db961
LP
96 assert(bus);
97 assert(reply);
8fe914ec
LP
98 assert(u);
99
a7f241db 100 f = unit_following(u);
79a60375 101 return sd_bus_message_append(reply, "s", f ? f->id : NULL);
8fe914ec
LP
102}
103
718db961
LP
104static 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,
ebcf1f97
LP
110 void *userdata,
111 sd_bus_error *error) {
4ec9a8a4 112
d1d85471 113 Hashmap **h = userdata;
5301be81 114 Iterator j;
718db961 115 Unit *u;
eef85c4a 116 void *v;
718db961 117 int r;
5301be81 118
718db961
LP
119 assert(bus);
120 assert(reply);
d1d85471 121 assert(h);
5301be81 122
718db961
LP
123 r = sd_bus_message_open_container(reply, 'a', "s");
124 if (r < 0)
125 return r;
5301be81 126
d1d85471 127 HASHMAP_FOREACH_KEY(v, u, *h, j) {
718db961
LP
128 r = sd_bus_message_append(reply, "s", u->id);
129 if (r < 0)
130 return r;
131 }
5301be81 132
718db961 133 return sd_bus_message_close_container(reply);
5301be81
LP
134}
135
99c14018
YW
136static 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
d1d85471 145 Hashmap **h = userdata;
99c14018 146 const char *p;
f6e11b39 147 Iterator j;
99c14018
YW
148 void *v;
149 int r;
150
151 assert(bus);
152 assert(reply);
d1d85471 153 assert(h);
99c14018
YW
154
155 r = sd_bus_message_open_container(reply, 'a', "s");
156 if (r < 0)
157 return r;
158
d1d85471 159 HASHMAP_FOREACH_KEY(v, p, *h, j) {
99c14018
YW
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
d2dc52db
LP
168static 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",
79a60375 187 r < 0 ? NULL:
d2dc52db
LP
188 r > 0 ? "enabled" : "disabled");
189}
190
718db961
LP
191static 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,
ebcf1f97
LP
197 void *userdata,
198 sd_bus_error *error) {
718db961 199
f93891f3 200 _cleanup_free_ char *p = NULL;
14f7edb0 201 Job **j = userdata;
38131695 202
718db961
LP
203 assert(bus);
204 assert(reply);
14f7edb0 205 assert(j);
38131695 206
14f7edb0 207 if (!*j)
718db961 208 return sd_bus_message_append(reply, "(uo)", 0, "/");
38131695 209
14f7edb0 210 p = job_dbus_path(*j);
718db961
LP
211 if (!p)
212 return -ENOMEM;
38131695 213
14f7edb0 214 return sd_bus_message_append(reply, "(uo)", (*j)->id, p);
718db961 215}
38131695 216
718db961
LP
217static 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,
ebcf1f97
LP
223 void *userdata,
224 sd_bus_error *error) {
45fb0699 225
59fccdc5
LP
226 const char *(*to_string)(ConditionType type) = NULL;
227 Condition **list = userdata, *c;
718db961 228 int r;
45fb0699 229
718db961
LP
230 assert(bus);
231 assert(reply);
59fccdc5
LP
232 assert(list);
233
234 to_string = streq(property, "Asserts") ? assert_type_to_string : condition_type_to_string;
45fb0699 235
718db961
LP
236 r = sd_bus_message_open_container(reply, 'a', "(sbbsi)");
237 if (r < 0)
238 return r;
45fb0699 239
59fccdc5 240 LIST_FOREACH(conditions, c, *list) {
cc50ef13
LP
241 int tristate;
242
243 tristate =
244 c->result == CONDITION_UNTESTED ? 0 :
245 c->result == CONDITION_SUCCEEDED ? 1 : -1;
246
2c7e050f 247 r = sd_bus_message_append(reply, "(sbbsi)",
59fccdc5 248 to_string(c->type),
2c7e050f 249 c->trigger, c->negate,
cc50ef13 250 c->parameter, tristate);
718db961
LP
251 if (r < 0)
252 return r;
45fb0699 253
718db961 254 }
52990c2e 255
718db961 256 return sd_bus_message_close_container(reply);
52990c2e
ZJS
257}
258
718db961
LP
259static 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,
ebcf1f97
LP
265 void *userdata,
266 sd_bus_error *error) {
52990c2e 267
4afd3348 268 _cleanup_(sd_bus_error_free) sd_bus_error e = SD_BUS_ERROR_NULL;
fd1e3fd8
LP
269 Unit *u = userdata;
270 int r;
52990c2e 271
718db961
LP
272 assert(bus);
273 assert(reply);
fd1e3fd8 274 assert(u);
52990c2e 275
fd1e3fd8
LP
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);
52990c2e 279
fd1e3fd8 280 return sd_bus_message_append(reply, "(ss)", NULL, NULL);
52990c2e
ZJS
281}
282
88ced61b
MC
283static int bus_verify_manage_units_async_full(
284 Unit *u,
285 const char *verb,
286 int capability,
287 const char *polkit_message,
05a98afd 288 bool interactive,
88ced61b
MC
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
05a98afd
LP
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);
88ced61b
MC
313}
314
1d22e906 315int bus_unit_method_start_generic(
1d22e906
LP
316 sd_bus_message *message,
317 Unit *u,
318 JobType job_type,
319 bool reload_if_possible,
320 sd_bus_error *error) {
321
718db961
LP
322 const char *smode;
323 JobMode mode;
88ced61b
MC
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 };
718db961 332 int r;
9f39404c 333
718db961 334 assert(message);
9f39404c 335 assert(u);
718db961 336 assert(job_type >= 0 && job_type < _JOB_TYPE_MAX);
9f39404c 337
61ea63f1
EV
338 r = mac_selinux_unit_access_check(
339 u, message,
94bd7323
EV
340 job_type_to_access_method(job_type),
341 error);
1d22e906
LP
342 if (r < 0)
343 return r;
344
718db961
LP
345 r = sd_bus_message_read(message, "s", &smode);
346 if (r < 0)
ebcf1f97 347 return r;
9f39404c 348
718db961
LP
349 mode = job_mode_from_string(smode);
350 if (mode < 0)
ebcf1f97 351 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Job mode %s invalid", smode);
9f39404c 352
88ced61b 353 if (reload_if_possible)
605405c6 354 verb = strjoin("reload-or-", job_type_to_string(job_type));
88ced61b
MC
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,
05a98afd 365 true,
88ced61b
MC
366 message,
367 error);
1d22e906
LP
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
19070062 373 return bus_unit_queue_job(message, u, job_type, mode, reload_if_possible, error);
9f39404c
LP
374}
375
19070062
LP
376static 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);
718db961 378}
b548631a 379
19070062
LP
380static 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);
718db961
LP
382}
383
19070062
LP
384static 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);
718db961 386}
0a524ba7 387
19070062
LP
388static 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);
718db961 390}
8a0867d6 391
19070062
LP
392static 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);
718db961 394}
cad45ba1 395
19070062
LP
396static 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);
718db961 398}
8a0867d6 399
19070062
LP
400static 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);
718db961 402}
8a0867d6 403
19070062 404int bus_unit_method_kill(sd_bus_message *message, void *userdata, sd_bus_error *error) {
718db961
LP
405 Unit *u = userdata;
406 const char *swho;
407 int32_t signo;
408 KillWho who;
409 int r;
5632e374 410
718db961
LP
411 assert(message);
412 assert(u);
cad45ba1 413
1d22e906 414 r = mac_selinux_unit_access_check(u, message, "stop", error);
283868e1
SW
415 if (r < 0)
416 return r;
283868e1 417
718db961
LP
418 r = sd_bus_message_read(message, "si", &swho, &signo);
419 if (r < 0)
ebcf1f97 420 return r;
718db961
LP
421
422 if (isempty(swho))
423 who = KILL_ALL;
424 else {
425 who = kill_who_from_string(swho);
426 if (who < 0)
ebcf1f97 427 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid who argument %s", swho);
718db961 428 }
5632e374 429
6eb7c172 430 if (!SIGNAL_VALID(signo))
ebcf1f97 431 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Signal number out of range.");
8e2af478 432
88ced61b
MC
433 r = bus_verify_manage_units_async_full(
434 u,
435 "kill",
436 CAP_KILL,
1d445eda 437 N_("Authentication is required to send a UNIX signal to the processes of '$(unit)'."),
05a98afd 438 true,
88ced61b
MC
439 message,
440 error);
ebcf1f97
LP
441 if (r < 0)
442 return r;
1d22e906
LP
443 if (r == 0)
444 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
8e2af478 445
ebcf1f97 446 r = unit_kill(u, who, signo, error);
718db961 447 if (r < 0)
ebcf1f97 448 return r;
8e2af478 449
df2d202e 450 return sd_bus_reply_method_return(message, NULL);
718db961 451}
8e2af478 452
19070062 453int bus_unit_method_reset_failed(sd_bus_message *message, void *userdata, sd_bus_error *error) {
718db961 454 Unit *u = userdata;
ebcf1f97 455 int r;
b548631a 456
718db961
LP
457 assert(message);
458 assert(u);
b548631a 459
1d22e906 460 r = mac_selinux_unit_access_check(u, message, "reload", error);
283868e1
SW
461 if (r < 0)
462 return r;
283868e1 463
88ced61b
MC
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)'."),
05a98afd 469 true,
88ced61b
MC
470 message,
471 error);
ebcf1f97
LP
472 if (r < 0)
473 return r;
1d22e906
LP
474 if (r == 0)
475 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
b548631a 476
718db961 477 unit_reset_failed(u);
b548631a 478
df2d202e 479 return sd_bus_reply_method_return(message, NULL);
ea430986
LP
480}
481
19070062 482int bus_unit_method_set_properties(sd_bus_message *message, void *userdata, sd_bus_error *error) {
718db961
LP
483 Unit *u = userdata;
484 int runtime, r;
ea430986 485
ea430986 486 assert(message);
718db961 487 assert(u);
80fbf05e 488
1d22e906 489 r = mac_selinux_unit_access_check(u, message, "start", error);
283868e1
SW
490 if (r < 0)
491 return r;
283868e1 492
718db961
LP
493 r = sd_bus_message_read(message, "b", &runtime);
494 if (r < 0)
ebcf1f97 495 return r;
2cccbca4 496
88ced61b
MC
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)'."),
05a98afd 502 true,
88ced61b
MC
503 message,
504 error);
ebcf1f97
LP
505 if (r < 0)
506 return r;
1d22e906
LP
507 if (r == 0)
508 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
cad45ba1 509
ebcf1f97 510 r = bus_unit_set_properties(u, message, runtime ? UNIT_RUNTIME : UNIT_PERSISTENT, true, error);
718db961 511 if (r < 0)
ebcf1f97 512 return r;
2cccbca4 513
df2d202e 514 return sd_bus_reply_method_return(message, NULL);
718db961 515}
2cccbca4 516
05a98afd
LP
517int 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
548int 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
bedea99d
LP
564static 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
718db961
LP
602const sd_bus_vtable bus_unit_vtable[] = {
603 SD_BUS_VTABLE_START(0),
604
556089dc 605 SD_BUS_PROPERTY("Id", "s", NULL, offsetof(Unit, id), SD_BUS_VTABLE_PROPERTY_CONST),
14f7edb0 606 SD_BUS_PROPERTY("Names", "as", property_get_names, offsetof(Unit, names), SD_BUS_VTABLE_PROPERTY_CONST),
718db961 607 SD_BUS_PROPERTY("Following", "s", property_get_following, 0, 0),
556089dc 608 SD_BUS_PROPERTY("Requires", "as", property_get_dependencies, offsetof(Unit, dependencies[UNIT_REQUIRES]), SD_BUS_VTABLE_PROPERTY_CONST),
556089dc 609 SD_BUS_PROPERTY("Requisite", "as", property_get_dependencies, offsetof(Unit, dependencies[UNIT_REQUISITE]), SD_BUS_VTABLE_PROPERTY_CONST),
556089dc
LP
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),
be7d9ff7 614 SD_BUS_PROPERTY("RequisiteOf", "as", property_get_dependencies, offsetof(Unit, dependencies[UNIT_REQUISITE_OF]), SD_BUS_VTABLE_PROPERTY_CONST),
556089dc
LP
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),
99c14018 628 SD_BUS_PROPERTY("RequiresMountsFor", "as", property_get_requires_mounts_for, offsetof(Unit, requires_mounts_for), SD_BUS_VTABLE_PROPERTY_CONST),
556089dc
LP
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),
718db961
LP
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),
556089dc
LP
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),
718db961 637 SD_BUS_PROPERTY("UnitFileState", "s", property_get_unit_file_state, 0, 0),
d2dc52db 638 SD_BUS_PROPERTY("UnitFilePreset", "s", property_get_unit_file_preset, 0, 0),
a483fb59 639 BUS_PROPERTY_DUAL_TIMESTAMP("StateChangeTimestamp", offsetof(Unit, state_change_timestamp), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
718db961
LP
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),
556089dc
LP
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),
14f7edb0 648 SD_BUS_PROPERTY("Job", "(uo)", property_get_job, offsetof(Unit, job), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
556089dc
LP
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),
556089dc
LP
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),
a2df3ea4 658 SD_BUS_PROPERTY("JobRunningTimeoutUSec", "t", bus_property_get_usec, offsetof(Unit, job_running_timeout), SD_BUS_VTABLE_PROPERTY_CONST),
87a47f99 659 SD_BUS_PROPERTY("JobTimeoutAction", "s", property_get_emergency_action, offsetof(Unit, job_timeout_action), SD_BUS_VTABLE_PROPERTY_CONST),
f189ab18 660 SD_BUS_PROPERTY("JobTimeoutRebootArgument", "s", NULL, offsetof(Unit, job_timeout_reboot_arg), SD_BUS_VTABLE_PROPERTY_CONST),
718db961 661 SD_BUS_PROPERTY("ConditionResult", "b", bus_property_get_bool, offsetof(Unit, condition_result), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
59fccdc5 662 SD_BUS_PROPERTY("AssertResult", "b", bus_property_get_bool, offsetof(Unit, assert_result), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
718db961 663 BUS_PROPERTY_DUAL_TIMESTAMP("ConditionTimestamp", offsetof(Unit, condition_timestamp), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
59fccdc5
LP
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),
fd1e3fd8 667 SD_BUS_PROPERTY("LoadError", "(ss)", property_get_load_error, 0, SD_BUS_VTABLE_PROPERTY_CONST),
556089dc 668 SD_BUS_PROPERTY("Transient", "b", bus_property_get_bool, offsetof(Unit, transient), SD_BUS_VTABLE_PROPERTY_CONST),
f5869324 669 SD_BUS_PROPERTY("Perpetual", "b", bus_property_get_bool, offsetof(Unit, perpetual), SD_BUS_VTABLE_PROPERTY_CONST),
c075f5fc 670 SD_BUS_PROPERTY("StartLimitIntervalUSec", "t", bus_property_get_usec, offsetof(Unit, start_limit.interval), SD_BUS_VTABLE_PROPERTY_CONST),
6bf0f408 671 SD_BUS_PROPERTY("StartLimitBurst", "u", bus_property_get_unsigned, offsetof(Unit, start_limit.burst), SD_BUS_VTABLE_PROPERTY_CONST),
87a47f99 672 SD_BUS_PROPERTY("StartLimitAction", "s", property_get_emergency_action, offsetof(Unit, start_limit_action), SD_BUS_VTABLE_PROPERTY_CONST),
53c35a76 673 SD_BUS_PROPERTY("FailureAction", "s", property_get_emergency_action, offsetof(Unit, failure_action), SD_BUS_VTABLE_PROPERTY_CONST),
7af67e9a 674 SD_BUS_PROPERTY("FailureActionExitStatus", "i", bus_property_get_int, offsetof(Unit, failure_action_exit_status), SD_BUS_VTABLE_PROPERTY_CONST),
e7dfbb4e 675 SD_BUS_PROPERTY("SuccessAction", "s", property_get_emergency_action, offsetof(Unit, success_action), SD_BUS_VTABLE_PROPERTY_CONST),
7af67e9a 676 SD_BUS_PROPERTY("SuccessActionExitStatus", "i", bus_property_get_int, offsetof(Unit, success_action_exit_status), SD_BUS_VTABLE_PROPERTY_CONST),
6bf0f408 677 SD_BUS_PROPERTY("RebootArgument", "s", NULL, offsetof(Unit, reboot_arg), SD_BUS_VTABLE_PROPERTY_CONST),
4b58153d 678 SD_BUS_PROPERTY("InvocationID", "ay", bus_property_get_id128, offsetof(Unit, invocation_id), 0),
5afe510c 679 SD_BUS_PROPERTY("CollectMode", "s", property_get_collect_mode, offsetof(Unit, collect_mode), 0),
bedea99d 680 SD_BUS_PROPERTY("Refs", "as", property_get_refs, 0, 0),
718db961 681
1d22e906
LP
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),
05a98afd
LP
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),
718db961 694
92c23c5a
YW
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 */
51d73fd9 701 SD_BUS_PROPERTY("StartLimitInterval", "t", bus_property_get_usec, offsetof(Unit, start_limit.interval), SD_BUS_VTABLE_PROPERTY_CONST|SD_BUS_VTABLE_HIDDEN),
c075f5fc 702 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
703 SD_BUS_VTABLE_END
704};
2cccbca4 705
718db961
LP
706static 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,
ebcf1f97
LP
712 void *userdata,
713 sd_bus_error *error) {
2cccbca4 714
718db961 715 Unit *u = userdata;
2cccbca4 716
718db961
LP
717 assert(bus);
718 assert(reply);
719 assert(u);
2cccbca4 720
718db961
LP
721 return sd_bus_message_append(reply, "s", unit_slice_name(u));
722}
2cccbca4 723
934277fe
LP
724static 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
934277fe 733 uint64_t sz = (uint64_t) -1;
5ad096b3 734 Unit *u = userdata;
934277fe
LP
735 int r;
736
737 assert(bus);
738 assert(reply);
739 assert(u);
740
5ad096b3
LP
741 r = unit_get_memory_current(u, &sz);
742 if (r < 0 && r != -ENODATA)
f2341e0a 743 log_unit_warning_errno(u, r, "Failed to get memory.usage_in_bytes attribute: %m");
934277fe 744
5ad096b3
LP
745 return sd_bus_message_append(reply, "t", sz);
746}
934277fe 747
03a7b521
LP
748static 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
5ad096b3
LP
772static 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) {
934277fe 780
5ad096b3
LP
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)
f2341e0a 791 log_unit_warning_errno(u, r, "Failed to get cpuacct.usage attribute: %m");
5ad096b3
LP
792
793 return sd_bus_message_append(reply, "t", ns);
934277fe
LP
794}
795
98bac605
LP
796static 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;
79a60375 806 const char *t = NULL;
98bac605
LP
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)
945403e6 819 t = empty_to_root(u->cgroup_path);
98bac605
LP
820
821 return sd_bus_message_append(reply, "s", t);
822}
823
291d565a
LP
824static 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));
4c701096 832 if (IN_SET(r, 0, -EEXIST))
291d565a
LP
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
856static 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);
24715314 865 if (r == -ENOENT)
291d565a
LP
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
605405c6 902 j = strjoin(p, "/", g);
291d565a
LP
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
914int 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;
af4fa99d 916 _cleanup_set_free_ Set *pids = NULL;
291d565a
LP
917 Unit *u = userdata;
918 pid_t pid;
919 int r;
920
921 assert(message);
922
807fa5d9
LP
923 r = mac_selinux_unit_access_check(u, message, "status", error);
924 if (r < 0)
925 return r;
926
291d565a
LP
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
906c06f6
DM
967static 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
6592b975
LP
1000int 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;
af4fa99d 1003 _cleanup_set_free_ Set *pids = NULL;
6592b975
LP
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
874b5ad2 1077 /* Let's validate security: if the sender is root, then all is OK. If the sender is any other unit,
6592b975
LP
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
718db961
LP
1112const sd_bus_vtable bus_unit_cgroup_vtable[] = {
1113 SD_BUS_VTABLE_START(0),
1114 SD_BUS_PROPERTY("Slice", "s", property_get_slice, 0, 0),
98bac605 1115 SD_BUS_PROPERTY("ControlGroup", "s", property_get_cgroup, 0, 0),
934277fe 1116 SD_BUS_PROPERTY("MemoryCurrent", "t", property_get_current_memory, 0, 0),
5ad096b3 1117 SD_BUS_PROPERTY("CPUUsageNSec", "t", property_get_cpu_usage, 0, 0),
03a7b521 1118 SD_BUS_PROPERTY("TasksCurrent", "t", property_get_current_tasks, 0, 0),
906c06f6
DM
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),
291d565a 1123 SD_BUS_METHOD("GetProcesses", NULL, "a(sus)", bus_unit_method_get_processes, SD_BUS_VTABLE_UNPRIVILEGED),
6592b975 1124 SD_BUS_METHOD("AttachProcesses", "sau", NULL, bus_unit_method_attach_processes, SD_BUS_VTABLE_UNPRIVILEGED),
718db961
LP
1125 SD_BUS_VTABLE_END
1126};
2cccbca4 1127
8f8f05a9 1128static int send_new_signal(sd_bus *bus, void *userdata) {
4afd3348 1129 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
718db961
LP
1130 _cleanup_free_ char *p = NULL;
1131 Unit *u = userdata;
1132 int r;
2cccbca4 1133
718db961
LP
1134 assert(bus);
1135 assert(u);
2cccbca4 1136
718db961 1137 p = unit_dbus_path(u);
e861098b 1138 if (!p)
718db961 1139 return -ENOMEM;
2cccbca4 1140
718db961
LP
1141 r = sd_bus_message_new_signal(
1142 bus,
151b9b96 1143 &m,
718db961
LP
1144 "/org/freedesktop/systemd1",
1145 "org.freedesktop.systemd1.Manager",
151b9b96 1146 "UnitNew");
718db961
LP
1147 if (r < 0)
1148 return r;
2cccbca4 1149
718db961
LP
1150 r = sd_bus_message_append(m, "so", u->id, p);
1151 if (r < 0)
1152 return r;
2cccbca4 1153
8f8f05a9 1154 return sd_bus_send(bus, m, NULL);
718db961 1155}
2cccbca4 1156
8f8f05a9 1157static int send_changed_signal(sd_bus *bus, void *userdata) {
718db961
LP
1158 _cleanup_free_ char *p = NULL;
1159 Unit *u = userdata;
1160 int r;
2cccbca4 1161
718db961
LP
1162 assert(bus);
1163 assert(u);
2cccbca4 1164
718db961 1165 p = unit_dbus_path(u);
9ceefc81 1166 if (!p)
718db961 1167 return -ENOMEM;
2cccbca4 1168
718db961
LP
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. */
ea430986 1172
aec8de63
LP
1173 r = sd_bus_emit_properties_changed_strv(
1174 bus, p,
21b735e7 1175 unit_dbus_interface_from_type(u->type),
aec8de63 1176 NULL);
fe7f06f1 1177 if (r < 0)
aec8de63 1178 return r;
80fbf05e 1179
fe7f06f1 1180 return sd_bus_emit_properties_changed_strv(
718db961
LP
1181 bus, p,
1182 "org.freedesktop.systemd1.Unit",
718db961 1183 NULL);
ea430986
LP
1184}
1185
c1e1601e 1186void bus_unit_send_change_signal(Unit *u) {
b170dd80 1187 int r;
c1e1601e 1188 assert(u);
c1e1601e 1189
ac155bb8 1190 if (u->in_dbus_queue) {
71fda00f 1191 LIST_REMOVE(dbus_queue, u->manager->dbus_unit_queue, u);
ac155bb8 1192 u->in_dbus_queue = false;
c0bd0cf7 1193 }
c1e1601e 1194
ac155bb8 1195 if (!u->id)
04ade7d2
LP
1196 return;
1197
ae572acd 1198 r = bus_foreach_bus(u->manager, u->bus_track, u->sent_dbus_new_signal ? send_changed_signal : send_new_signal, u);
718db961 1199 if (r < 0)
f2341e0a 1200 log_unit_debug_errno(u, r, "Failed to send unit change signal for %s: %m", u->id);
c1e1601e 1201
718db961
LP
1202 u->sent_dbus_new_signal = true;
1203}
c4e2ceae 1204
6fcbec6f
LP
1205void bus_unit_send_pending_change_signal(Unit *u, bool including_new) {
1206
1207 /* Sends out any pending change signals, but only if they really are pending. This call is used when we are
1208 * about to change state in order to force out a PropertiesChanged signal beforehand if there was one pending
1209 * so that clients can follow the full state transition */
1210
1211 if (!u->in_dbus_queue) /* If not enqueued, don't bother */
1212 return;
1213
1214 if (!u->sent_dbus_new_signal && !including_new) /* If the unit was never announced, don't bother, it's fine if
1215 * the unit appears in the new state right-away (except if the
1216 * caller explicitly asked us to send it anyway) */
1217 return;
1218
1219 if (MANAGER_IS_RELOADING(u->manager)) /* Don't generate unnecessary PropertiesChanged signals for the same unit
1220 * when we are reloading. */
1221 return;
1222
1223 bus_unit_send_change_signal(u);
1224}
1225
8f8f05a9 1226static int send_removed_signal(sd_bus *bus, void *userdata) {
4afd3348 1227 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
718db961
LP
1228 _cleanup_free_ char *p = NULL;
1229 Unit *u = userdata;
1230 int r;
c4e2ceae 1231
718db961
LP
1232 assert(bus);
1233 assert(u);
c1e1601e 1234
718db961 1235 p = unit_dbus_path(u);
e861098b 1236 if (!p)
718db961 1237 return -ENOMEM;
c1e1601e 1238
718db961
LP
1239 r = sd_bus_message_new_signal(
1240 bus,
151b9b96 1241 &m,
718db961
LP
1242 "/org/freedesktop/systemd1",
1243 "org.freedesktop.systemd1.Manager",
151b9b96 1244 "UnitRemoved");
718db961
LP
1245 if (r < 0)
1246 return r;
c1e1601e 1247
718db961
LP
1248 r = sd_bus_message_append(m, "so", u->id, p);
1249 if (r < 0)
1250 return r;
c1e1601e 1251
8f8f05a9 1252 return sd_bus_send(bus, m, NULL);
c1e1601e
LP
1253}
1254
1255void bus_unit_send_removed_signal(Unit *u) {
718db961 1256 int r;
c1e1601e
LP
1257 assert(u);
1258
0dd99f86 1259 if (!u->sent_dbus_new_signal || u->in_dbus_queue)
7535cc78
LP
1260 bus_unit_send_change_signal(u);
1261
ac155bb8 1262 if (!u->id)
04ade7d2
LP
1263 return;
1264
ae572acd 1265 r = bus_foreach_bus(u->manager, u->bus_track, send_removed_signal, u);
718db961 1266 if (r < 0)
f2341e0a 1267 log_unit_debug_errno(u, r, "Failed to send unit remove signal for %s: %m", u->id);
c1e1601e 1268}
e2110e5d 1269
718db961 1270int bus_unit_queue_job(
718db961 1271 sd_bus_message *message,
cad45ba1
LP
1272 Unit *u,
1273 JobType type,
1274 JobMode mode,
ebcf1f97
LP
1275 bool reload_if_possible,
1276 sd_bus_error *error) {
cad45ba1 1277
cad45ba1
LP
1278 _cleanup_free_ char *path = NULL;
1279 Job *j;
cad45ba1
LP
1280 int r;
1281
cad45ba1
LP
1282 assert(message);
1283 assert(u);
1284 assert(type >= 0 && type < _JOB_TYPE_MAX);
1285 assert(mode >= 0 && mode < _JOB_MODE_MAX);
1286
f596e00f
EV
1287 r = mac_selinux_unit_access_check(
1288 u, message,
1289 job_type_to_access_method(type),
1290 error);
1291 if (r < 0)
1292 return r;
1293
cad45ba1
LP
1294 if (reload_if_possible && unit_can_reload(u)) {
1295 if (type == JOB_RESTART)
1296 type = JOB_RELOAD_OR_START;
1297 else if (type == JOB_TRY_RESTART)
3282591d 1298 type = JOB_TRY_RELOAD;
cad45ba1
LP
1299 }
1300
718db961 1301 if (type == JOB_STOP &&
c4555ad8 1302 IN_SET(u->load_state, UNIT_NOT_FOUND, UNIT_ERROR, UNIT_BAD_SETTING) &&
718db961 1303 unit_active_state(u) == UNIT_INACTIVE)
ebcf1f97 1304 return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_UNIT, "Unit %s not loaded.", u->id);
cad45ba1
LP
1305
1306 if ((type == JOB_START && u->refuse_manual_start) ||
1307 (type == JOB_STOP && u->refuse_manual_stop) ||
3742095b 1308 (IN_SET(type, JOB_RESTART, JOB_TRY_RESTART) && (u->refuse_manual_start || u->refuse_manual_stop)) ||
efb30ba1 1309 (type == JOB_RELOAD_OR_START && job_type_collapse(type, u) == JOB_START && u->refuse_manual_start))
7e974e85 1310 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 1311
4bd29fe5 1312 r = manager_add_job(u->manager, type, u, mode, error, &j);
cad45ba1 1313 if (r < 0)
ebcf1f97 1314 return r;
cad45ba1 1315
c5a97ed1
LP
1316 r = bus_job_track_sender(j, message);
1317 if (r < 0)
1318 return r;
cad45ba1
LP
1319
1320 path = job_dbus_path(j);
1321 if (!path)
6ce270b1 1322 return -ENOMEM;
cad45ba1 1323
13142276
LP
1324 /* Before we send the method reply, force out the announcement JobNew for this job */
1325 bus_job_send_pending_change_signal(j, true);
1326
df2d202e 1327 return sd_bus_reply_method_return(message, "o", path);
cad45ba1
LP
1328}
1329
721060d4 1330static int bus_unit_set_live_property(
9f2e86af
LP
1331 Unit *u,
1332 const char *name,
718db961 1333 sd_bus_message *message,
2e59b241 1334 UnitWriteFlags flags,
718db961 1335 sd_bus_error *error) {
9f2e86af
LP
1336
1337 int r;
1338
1339 assert(u);
1340 assert(name);
718db961 1341 assert(message);
9f2e86af 1342
721060d4
LP
1343 /* Handles setting properties both "live" (i.e. at any time during runtime), and during creation (for transient
1344 * units that are being created). */
1345
9f2e86af 1346 if (streq(name, "Description")) {
718db961 1347 const char *d;
9f2e86af 1348
718db961
LP
1349 r = sd_bus_message_read(message, "s", &d);
1350 if (r < 0)
1351 return r;
8aec412f 1352
2e59b241 1353 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
718db961 1354 r = unit_set_description(u, d);
8aec412f
LP
1355 if (r < 0)
1356 return r;
b9316fb0 1357
2e59b241 1358 unit_write_settingf(u, flags|UNIT_ESCAPE_SPECIFIERS, name, "Description=%s", d);
8aec412f 1359 }
9f2e86af
LP
1360
1361 return 1;
721060d4
LP
1362 }
1363
1364 return 0;
1365}
1366
54fcb619
ZJS
1367static int bus_set_transient_emergency_action(
1368 Unit *u,
1369 const char *name,
1370 EmergencyAction *p,
1371 sd_bus_message *message,
1372 UnitWriteFlags flags,
1373 sd_bus_error *error) {
1374
1375 const char *s;
1376 EmergencyAction v;
1377 int r;
1378 bool system;
1379
1380 assert(p);
1381
1382 r = sd_bus_message_read(message, "s", &s);
1383 if (r < 0)
1384 return r;
1385
1386 system = MANAGER_IS_SYSTEM(u->manager);
1387 r = parse_emergency_action(s, system, &v);
1388 if (v < 0)
1389 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS,
1390 v == -EOPNOTSUPP ? "EmergencyAction setting invalid for manager type: %s"
1391 : "Invalid %s setting: %s",
1392 name, s);
1393
1394 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
1395 *p = v;
1396 unit_write_settingf(u, flags, name,
1397 "%s=%s", name, s);
1398 }
1399
1400 return 1;
1401}
1402
7af67e9a
LP
1403static int bus_set_transient_exit_status(
1404 Unit *u,
1405 const char *name,
1406 int *p,
1407 sd_bus_message *message,
1408 UnitWriteFlags flags,
1409 sd_bus_error *error) {
1410
1411 int32_t k;
1412 int r;
1413
1414 assert(p);
1415
1416 r = sd_bus_message_read(message, "i", &k);
1417 if (r < 0)
1418 return r;
1419
1420 if (k > 255)
1421 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Exit status must be in range 0…255 or negative.");
1422
1423 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
1424 *p = k < 0 ? -1 : k;
1425
1426 if (k < 0)
1427 unit_write_settingf(u, flags, name, "%s=", name);
1428 else
1429 unit_write_settingf(u, flags, name, "%s=%i", name, k);
1430 }
1431
1432 return 1;
1433}
1434
faa781cf 1435static BUS_DEFINE_SET_TRANSIENT_PARSE(collect_mode, CollectMode, collect_mode_from_string);
faa781cf
YW
1436static BUS_DEFINE_SET_TRANSIENT_PARSE(job_mode, JobMode, job_mode_from_string);
1437
1438static int bus_set_transient_conditions(
1439 Unit *u,
1440 const char *name,
1441 Condition **list,
1442 bool is_condition,
1443 sd_bus_message *message,
1444 UnitWriteFlags flags,
1445 sd_bus_error *error) {
1446
1447 const char *type_name, *param;
1448 int trigger, negate, r;
1449 bool empty = true;
1450
1451 assert(list);
1452
1453 r = sd_bus_message_enter_container(message, 'a', "(sbbs)");
1454 if (r < 0)
1455 return r;
1456
1457 while ((r = sd_bus_message_read(message, "(sbbs)", &type_name, &trigger, &negate, &param)) > 0) {
1458 ConditionType t;
1459
1460 t = is_condition ? condition_type_from_string(type_name) : assert_type_from_string(type_name);
1461 if (t < 0)
1462 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid condition type: %s", type_name);
1463
1464 if (t != CONDITION_NULL) {
1465 if (isempty(param))
1466 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Condition parameter in %s is empty", type_name);
1467
1468 if (condition_takes_path(t) && !path_is_absolute(param))
1469 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Path in condition %s is not absolute: %s", type_name, param);
1470 } else
1471 param = NULL;
1472
1473 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
1474 Condition *c;
1475
1476 c = condition_new(t, param, trigger, negate);
1477 if (!c)
1478 return -ENOMEM;
1479
1480 LIST_PREPEND(conditions, *list, c);
1481
1482 if (t != CONDITION_NULL)
1483 unit_write_settingf(u, flags|UNIT_ESCAPE_SPECIFIERS, name,
1484 "%s=%s%s%s", type_name,
1485 trigger ? "|" : "", negate ? "!" : "", param);
1486 else
1487 unit_write_settingf(u, flags, name,
1488 "%s=%s%s", type_name,
1489 trigger ? "|" : "", yes_no(!negate));
1490 }
1491
1492 empty = false;
1493 }
1494 if (r < 0)
1495 return r;
1496
1497 r = sd_bus_message_exit_container(message);
1498 if (r < 0)
1499 return r;
1500
1501 if (!UNIT_WRITE_FLAGS_NOOP(flags) && empty) {
1502 *list = condition_free_list(*list);
1503 unit_write_settingf(u, flags, name, "%sNull=", is_condition ? "Condition" : "Assert");
1504 }
1505
1506 return 1;
1507}
1508
721060d4
LP
1509static int bus_unit_set_transient_property(
1510 Unit *u,
1511 const char *name,
1512 sd_bus_message *message,
1513 UnitWriteFlags flags,
1514 sd_bus_error *error) {
1515
faa781cf 1516 UnitDependency d = _UNIT_DEPENDENCY_INVALID;
721060d4 1517 int r;
261420ba 1518
721060d4
LP
1519 assert(u);
1520 assert(name);
1521 assert(message);
1522
1523 /* Handles settings when transient units are created. This settings cannot be altered anymore after the unit
1524 * has been created. */
1525
faa781cf
YW
1526 if (streq(name, "SourcePath"))
1527 return bus_set_transient_path(u, name, &u->source_path, message, flags, error);
261420ba 1528
faa781cf
YW
1529 if (streq(name, "StopWhenUnneeded"))
1530 return bus_set_transient_bool(u, name, &u->stop_when_unneeded, message, flags, error);
261420ba 1531
faa781cf
YW
1532 if (streq(name, "RefuseManualStart"))
1533 return bus_set_transient_bool(u, name, &u->refuse_manual_start, message, flags, error);
261420ba 1534
faa781cf
YW
1535 if (streq(name, "RefuseManualStop"))
1536 return bus_set_transient_bool(u, name, &u->refuse_manual_stop, message, flags, error);
c221420b 1537
faa781cf
YW
1538 if (streq(name, "AllowIsolate"))
1539 return bus_set_transient_bool(u, name, &u->allow_isolate, message, flags, error);
5afe510c 1540
faa781cf
YW
1541 if (streq(name, "DefaultDependencies"))
1542 return bus_set_transient_bool(u, name, &u->default_dependencies, message, flags, error);
1543
1544 if (streq(name, "OnFailureJobMode"))
1545 return bus_set_transient_job_mode(u, name, &u->on_failure_job_mode, message, flags, error);
1546
1547 if (streq(name, "IgnoreOnIsolate"))
1548 return bus_set_transient_bool(u, name, &u->ignore_on_isolate, message, flags, error);
1549
1550 if (streq(name, "JobTimeoutUSec")) {
1551 r = bus_set_transient_usec_fix_0(u, name, &u->job_timeout, message, flags, error);
1552 if (r >= 0 && !UNIT_WRITE_FLAGS_NOOP(flags) && !u->job_running_timeout_set)
1553 u->job_running_timeout = u->job_timeout;
1554 }
1555
1556 if (streq(name, "JobRunningTimeoutUSec")) {
1557 r = bus_set_transient_usec_fix_0(u, name, &u->job_running_timeout, message, flags, error);
1558 if (r >= 0 && !UNIT_WRITE_FLAGS_NOOP(flags))
1559 u->job_running_timeout_set = true;
1560
1561 return r;
1562 }
1563
1564 if (streq(name, "JobTimeoutAction"))
1565 return bus_set_transient_emergency_action(u, name, &u->job_timeout_action, message, flags, error);
1566
1567 if (streq(name, "JobTimeoutRebootArgument"))
1568 return bus_set_transient_string(u, name, &u->job_timeout_reboot_arg, message, flags, error);
1569
1570 if (streq(name, "StartLimitIntervalUSec"))
1571 return bus_set_transient_usec(u, name, &u->start_limit.interval, message, flags, error);
1572
1573 if (streq(name, "StartLimitBurst"))
1574 return bus_set_transient_unsigned(u, name, &u->start_limit.burst, message, flags, error);
1575
1576 if (streq(name, "StartLimitAction"))
1577 return bus_set_transient_emergency_action(u, name, &u->start_limit_action, message, flags, error);
1578
1579 if (streq(name, "FailureAction"))
1580 return bus_set_transient_emergency_action(u, name, &u->failure_action, message, flags, error);
1581
1582 if (streq(name, "SuccessAction"))
1583 return bus_set_transient_emergency_action(u, name, &u->success_action, message, flags, error);
1584
7af67e9a
LP
1585 if (streq(name, "FailureActionExitStatus"))
1586 return bus_set_transient_exit_status(u, name, &u->failure_action_exit_status, message, flags, error);
1587
1588 if (streq(name, "SuccessActionExitStatus"))
1589 return bus_set_transient_exit_status(u, name, &u->success_action_exit_status, message, flags, error);
1590
faa781cf
YW
1591 if (streq(name, "RebootArgument"))
1592 return bus_set_transient_string(u, name, &u->reboot_arg, message, flags, error);
1593
1594 if (streq(name, "CollectMode"))
1595 return bus_set_transient_collect_mode(u, name, &u->collect_mode, message, flags, error);
1596
1597 if (streq(name, "Conditions"))
1598 return bus_set_transient_conditions(u, name, &u->conditions, true, message, flags, error);
1599
1600 if (streq(name, "Asserts"))
1601 return bus_set_transient_conditions(u, name, &u->asserts, false, message, flags, error);
1602
1603 if (streq(name, "Documentation")) {
1604 _cleanup_strv_free_ char **l = NULL;
1605 char **p;
1606
1607 r = sd_bus_message_read_strv(message, &l);
5afe510c
LP
1608 if (r < 0)
1609 return r;
1610
faa781cf
YW
1611 STRV_FOREACH(p, l) {
1612 if (!documentation_url_is_valid(*p))
1613 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid URL in %s: %s", name, *p);
1614 }
5afe510c 1615
2e59b241 1616 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
faa781cf
YW
1617 if (strv_isempty(l)) {
1618 u->documentation = strv_free(u->documentation);
1619 unit_write_settingf(u, flags, name, "%s=", name);
1620 } else {
1621 strv_extend_strv(&u->documentation, l, false);
1622
1623 STRV_FOREACH(p, l)
1624 unit_write_settingf(u, flags, name, "%s=%s", name, *p);
1625 }
5afe510c
LP
1626 }
1627
1628 return 1;
1629
d79200e2
LP
1630 } else if (streq(name, "Slice")) {
1631 Unit *slice;
c221420b 1632 const char *s;
c221420b 1633
d79200e2
LP
1634 if (!UNIT_HAS_CGROUP_CONTEXT(u))
1635 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "The slice property is only available for units with control groups.");
1636 if (u->type == UNIT_SLICE)
1637 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Slice may not be set for slice units.");
efdb0237
LP
1638 if (unit_has_name(u, SPECIAL_INIT_SCOPE))
1639 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Cannot set slice for init.scope");
d79200e2 1640
718db961
LP
1641 r = sd_bus_message_read(message, "s", &s);
1642 if (r < 0)
1643 return r;
c221420b 1644
d79200e2
LP
1645 if (!unit_name_is_valid(s, UNIT_NAME_PLAIN))
1646 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid unit name '%s'", s);
1647
aea529e5
LP
1648 /* Note that we do not dispatch the load queue here yet, as we don't want our own transient unit to be
1649 * loaded while we are still setting it up. Or in other words, we use manager_load_unit_prepare()
1650 * instead of manager_load_unit() on purpose, here. */
1651 r = manager_load_unit_prepare(u->manager, s, NULL, error, &slice);
d79200e2
LP
1652 if (r < 0)
1653 return r;
c221420b 1654
d79200e2
LP
1655 if (slice->type != UNIT_SLICE)
1656 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Unit name '%s' is not a slice", s);
b9316fb0 1657
2e59b241 1658 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
d79200e2 1659 r = unit_set_slice(u, slice);
8aec412f
LP
1660 if (r < 0)
1661 return r;
c221420b 1662
2e59b241 1663 unit_write_settingf(u, flags|UNIT_PRIVATE, name, "Slice=%s", s);
8aec412f 1664 }
b9ec9359 1665
c221420b 1666 return 1;
d79200e2 1667
faa781cf
YW
1668 } else if (streq(name, "RequiresMountsFor")) {
1669 _cleanup_strv_free_ char **l = NULL;
1670 char **p;
1671
1672 r = sd_bus_message_read_strv(message, &l);
1673 if (r < 0)
1674 return r;
1675
1676 STRV_FOREACH(p, l) {
1677 if (!path_is_absolute(*p))
1678 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Path specified in %s is not absolute: %s", name, *p);
1679
1680 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
1681 r = unit_require_mounts_for(u, *p, UNIT_DEPENDENCY_FILE);
1682 if (r < 0)
1683 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Failed to add required mount \"%s\": %m", *p);
7fb3ee51 1684
faa781cf
YW
1685 unit_write_settingf(u, flags, name, "%s=%s", name, *p);
1686 }
f32b43bd 1687 }
7fb3ee51 1688
faa781cf
YW
1689 return 1;
1690 }
1691
1692 if (streq(name, "RequiresOverridable"))
1693 d = UNIT_REQUIRES; /* redirect for obsolete unit dependency type */
1694 else if (streq(name, "RequisiteOverridable"))
1695 d = UNIT_REQUISITE; /* same here */
1696 else
1697 d = unit_dependency_from_string(name);
1698
1699 if (d >= 0) {
1700 const char *other;
1701
718db961
LP
1702 r = sd_bus_message_enter_container(message, 'a', "s");
1703 if (r < 0)
1704 return r;
7fb3ee51 1705
718db961 1706 while ((r = sd_bus_message_read(message, "s", &other)) > 0) {
7410616c 1707 if (!unit_name_is_valid(other, UNIT_NAME_PLAIN|UNIT_NAME_INSTANCE))
718db961 1708 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid unit name %s", other);
7fb3ee51 1709
2e59b241 1710 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
b9ec9359 1711 _cleanup_free_ char *label = NULL;
7fb3ee51 1712
35d8c19a 1713 r = unit_add_dependency_by_name(u, d, other, true, UNIT_DEPENDENCY_FILE);
7fb3ee51
LP
1714 if (r < 0)
1715 return r;
1716
605405c6 1717 label = strjoin(name, "-", other);
7fb3ee51
LP
1718 if (!label)
1719 return -ENOMEM;
1720
faa781cf 1721 unit_write_settingf(u, flags, label, "%s=%s", unit_dependency_to_string(d), other);
7fb3ee51
LP
1722 }
1723
7fb3ee51 1724 }
718db961
LP
1725 if (r < 0)
1726 return r;
7fb3ee51 1727
6ce270b1
LP
1728 r = sd_bus_message_exit_container(message);
1729 if (r < 0)
1730 return r;
1731
7fb3ee51 1732 return 1;
05a98afd
LP
1733
1734 } else if (streq(name, "AddRef")) {
1735
1736 int b;
1737
1738 /* Why is this called "AddRef" rather than just "Ref", or "Reference"? There's already a "Ref()" method
1739 * on the Unit interface, and it's probably not a good idea to expose a property and a method on the
1740 * same interface (well, strictly speaking AddRef isn't exposed as full property, we just read it for
1741 * transient units, but still). And "References" and "ReferencedBy" is already used as unit reference
1742 * dependency type, hence let's not confuse things with that.
1743 *
1744 * Note that we don't acually add the reference to the bus track. We do that only after the setup of
1745 * the transient unit is complete, so that setting this property multiple times in the same transient
1746 * unit creation call doesn't count as individual references. */
1747
1748 r = sd_bus_message_read(message, "b", &b);
1749 if (r < 0)
1750 return r;
1751
2e59b241 1752 if (!UNIT_WRITE_FLAGS_NOOP(flags))
05a98afd
LP
1753 u->bus_track_add = b;
1754
1755 return 1;
9f2e86af
LP
1756 }
1757
1758 return 0;
1759}
1760
c2756a68
LP
1761int bus_unit_set_properties(
1762 Unit *u,
718db961 1763 sd_bus_message *message,
2e59b241 1764 UnitWriteFlags flags,
c2756a68 1765 bool commit,
718db961 1766 sd_bus_error *error) {
c2756a68 1767
8e2af478 1768 bool for_real = false;
8e2af478
LP
1769 unsigned n = 0;
1770 int r;
1771
1772 assert(u);
718db961 1773 assert(message);
8e2af478
LP
1774
1775 /* We iterate through the array twice. First run we just check
1776 * if all passed data is valid, second run actually applies
1777 * it. This is to implement transaction-like behaviour without
1778 * actually providing full transactions. */
1779
718db961
LP
1780 r = sd_bus_message_enter_container(message, 'a', "(sv)");
1781 if (r < 0)
1782 return r;
8e2af478 1783
8e2af478 1784 for (;;) {
8e2af478 1785 const char *name;
2e59b241 1786 UnitWriteFlags f;
8e2af478 1787
718db961
LP
1788 r = sd_bus_message_enter_container(message, 'r', "sv");
1789 if (r < 0)
1790 return r;
1791 if (r == 0) {
2e59b241 1792 if (for_real || UNIT_WRITE_FLAGS_NOOP(flags))
8e2af478
LP
1793 break;
1794
1795 /* Reached EOF. Let's try again, and this time for realz... */
718db961
LP
1796 r = sd_bus_message_rewind(message, false);
1797 if (r < 0)
1798 return r;
6ce270b1 1799
8e2af478
LP
1800 for_real = true;
1801 continue;
1802 }
1803
718db961
LP
1804 r = sd_bus_message_read(message, "s", &name);
1805 if (r < 0)
1806 return r;
8e2af478 1807
718db961
LP
1808 if (!UNIT_VTABLE(u)->bus_set_property)
1809 return sd_bus_error_setf(error, SD_BUS_ERROR_PROPERTY_READ_ONLY, "Objects of this type do not support setting properties.");
8e2af478 1810
718db961
LP
1811 r = sd_bus_message_enter_container(message, 'v', NULL);
1812 if (r < 0)
1813 return r;
8e2af478 1814
2e59b241
LP
1815 /* If not for real, then mask out the two target flags */
1816 f = for_real ? flags : (flags & ~(UNIT_RUNTIME|UNIT_PERSISTENT));
1817
1818 r = UNIT_VTABLE(u)->bus_set_property(u, name, message, f, error);
9f2e86af 1819 if (r == 0 && u->transient && u->load_state == UNIT_STUB)
2e59b241 1820 r = bus_unit_set_transient_property(u, name, message, f, error);
721060d4
LP
1821 if (r == 0)
1822 r = bus_unit_set_live_property(u, name, message, f, error);
718db961
LP
1823 if (r < 0)
1824 return r;
721060d4 1825
718db961
LP
1826 if (r == 0)
1827 return sd_bus_error_setf(error, SD_BUS_ERROR_PROPERTY_READ_ONLY, "Cannot set property %s, or unknown property.", name);
1828
1829 r = sd_bus_message_exit_container(message);
8e2af478
LP
1830 if (r < 0)
1831 return r;
8e2af478 1832
718db961
LP
1833 r = sd_bus_message_exit_container(message);
1834 if (r < 0)
1835 return r;
8e2af478
LP
1836
1837 n += for_real;
1838 }
1839
6ce270b1
LP
1840 r = sd_bus_message_exit_container(message);
1841 if (r < 0)
1842 return r;
1843
c2756a68 1844 if (commit && n > 0 && UNIT_VTABLE(u)->bus_commit_properties)
8e2af478
LP
1845 UNIT_VTABLE(u)->bus_commit_properties(u);
1846
241da328 1847 return n;
8e2af478 1848}
000a996d 1849
e49da001 1850int bus_unit_validate_load_state(Unit *u, sd_bus_error *error) {
6eb7c172 1851 assert(u);
000a996d 1852
e49da001 1853 /* Generates a pretty error if a unit isn't properly loaded. */
000a996d 1854
e49da001
LP
1855 switch (u->load_state) {
1856
1857 case UNIT_LOADED:
1858 return 0;
000a996d 1859
e49da001 1860 case UNIT_NOT_FOUND:
b21d2f81 1861 return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_UNIT, "Unit %s not found.", u->id);
000a996d 1862
c4555ad8
LP
1863 case UNIT_BAD_SETTING:
1864 return sd_bus_error_setf(error, BUS_ERROR_BAD_UNIT_SETTING, "Unit %s has a bad unit file setting.", u->id);
1865
e49da001 1866 case UNIT_ERROR: /* Only show .load_error in UNIT_ERROR state */
06bed19e 1867 return sd_bus_error_set_errnof(error, u->load_error, "Unit %s failed to load properly: %m.", u->id);
e49da001
LP
1868
1869 case UNIT_MASKED:
1870 return sd_bus_error_setf(error, BUS_ERROR_UNIT_MASKED, "Unit %s is masked.", u->id);
1871
1872 case UNIT_STUB:
1873 case UNIT_MERGED:
1874 default:
1875 return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_UNIT, "Unexpected load state of unit %s", u->id);
1876 }
000a996d 1877}
05a98afd 1878
c5a97ed1 1879static int bus_unit_track_handler(sd_bus_track *t, void *userdata) {
05a98afd
LP
1880 Unit *u = userdata;
1881
1882 assert(t);
1883 assert(u);
1884
1885 u->bus_track = sd_bus_track_unref(u->bus_track); /* make sure we aren't called again */
1886
e5c36295
LP
1887 /* If the client that tracks us disappeared, then there's reason to believe that the cgroup is empty now too,
1888 * let's see */
1889 unit_add_to_cgroup_empty_queue(u);
1890
1891 /* Also add the unit to the GC queue, after all if the client left it might be time to GC this unit */
05a98afd 1892 unit_add_to_gc_queue(u);
e5c36295 1893
05a98afd
LP
1894 return 0;
1895}
1896
c5a97ed1 1897static int bus_unit_allocate_bus_track(Unit *u) {
05a98afd
LP
1898 int r;
1899
1900 assert(u);
1901
1902 if (u->bus_track)
1903 return 0;
1904
c5a97ed1 1905 r = sd_bus_track_new(u->manager->api_bus, &u->bus_track, bus_unit_track_handler, u);
05a98afd
LP
1906 if (r < 0)
1907 return r;
1908
1909 r = sd_bus_track_set_recursive(u->bus_track, true);
1910 if (r < 0) {
1911 u->bus_track = sd_bus_track_unref(u->bus_track);
1912 return r;
1913 }
1914
1915 return 0;
1916}
1917
1918int bus_unit_track_add_name(Unit *u, const char *name) {
1919 int r;
1920
1921 assert(u);
1922
c5a97ed1 1923 r = bus_unit_allocate_bus_track(u);
05a98afd
LP
1924 if (r < 0)
1925 return r;
1926
1927 return sd_bus_track_add_name(u->bus_track, name);
1928}
1929
1930int bus_unit_track_add_sender(Unit *u, sd_bus_message *m) {
1931 int r;
1932
1933 assert(u);
1934
c5a97ed1 1935 r = bus_unit_allocate_bus_track(u);
05a98afd
LP
1936 if (r < 0)
1937 return r;
1938
1939 return sd_bus_track_add_sender(u->bus_track, m);
1940}
1941
1942int bus_unit_track_remove_sender(Unit *u, sd_bus_message *m) {
1943 assert(u);
1944
1945 /* If we haven't allocated the bus track object yet, then there's definitely no reference taken yet, return an
1946 * error */
1947 if (!u->bus_track)
1948 return -EUNATCH;
1949
1950 return sd_bus_track_remove_sender(u->bus_track, m);
1951}