]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/core/dbus-unit.c
cryptsetup: mention correct action in log message
[thirdparty/systemd.git] / src / core / dbus-unit.c
CommitLineData
db9ecf05 1/* SPDX-License-Identifier: LGPL-2.1-or-later */
a7334b09 2
718db961 3#include "sd-bus.h"
07630cea 4
b5efdb8a 5#include "alloc-util.h"
906c06f6 6#include "bpf-firewall.h"
07630cea 7#include "bus-common-errors.h"
40af3d02 8#include "bus-get-properties.h"
269e4d2d 9#include "bus-polkit.h"
4ac08d8a 10#include "bus-util.h"
07630cea 11#include "cgroup-util.h"
faa781cf 12#include "condition.h"
c5a97ed1 13#include "dbus-job.h"
c44a285c 14#include "dbus-manager.h"
8752c575 15#include "dbus-unit.h"
faa781cf 16#include "dbus-util.h"
07630cea 17#include "dbus.h"
291d565a 18#include "fd-util.h"
5cfa33e0 19#include "install.h"
8752c575 20#include "locale-util.h"
ea430986 21#include "log.h"
faa781cf 22#include "path-util.h"
291d565a 23#include "process-util.h"
e2417e41 24#include "selinux-access.h"
53964fd2 25#include "service.h"
6eb7c172 26#include "signal-util.h"
efdb0237 27#include "special.h"
83f18c91 28#include "string-table.h"
07630cea
LP
29#include "string-util.h"
30#include "strv.h"
ee104e11 31#include "user-util.h"
faa781cf 32#include "web-util.h"
718db961 33
5afe510c 34static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_collect_mode, collect_mode, CollectMode);
718db961 35static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_load_state, unit_load_state, UnitLoadState);
d420282b 36static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_job_mode, job_mode, JobMode);
cb7f88fc
YW
37static BUS_DEFINE_PROPERTY_GET(property_get_description, "s", Unit, unit_description);
38static BUS_DEFINE_PROPERTY_GET2(property_get_active_state, "s", Unit, unit_active_state, unit_active_state_to_string);
d9e45bc3 39static BUS_DEFINE_PROPERTY_GET2(property_get_freezer_state, "s", Unit, unit_freezer_state, freezer_state_to_string);
cb7f88fc
YW
40static BUS_DEFINE_PROPERTY_GET(property_get_sub_state, "s", Unit, unit_sub_state_to_string);
41static BUS_DEFINE_PROPERTY_GET2(property_get_unit_file_state, "s", Unit, unit_get_unit_file_state, unit_file_state_to_string);
42static BUS_DEFINE_PROPERTY_GET(property_get_can_reload, "b", Unit, unit_can_reload);
874bd264
YW
43static BUS_DEFINE_PROPERTY_GET(property_get_can_start, "b", Unit, unit_can_start_refuse_manual);
44static BUS_DEFINE_PROPERTY_GET(property_get_can_stop, "b", Unit, unit_can_stop_refuse_manual);
45static BUS_DEFINE_PROPERTY_GET(property_get_can_isolate, "b", Unit, unit_can_isolate_refuse_manual);
d9e45bc3 46static BUS_DEFINE_PROPERTY_GET(property_get_can_freeze, "b", Unit, unit_can_freeze);
cb7f88fc 47static BUS_DEFINE_PROPERTY_GET(property_get_need_daemon_reload, "b", Unit, unit_need_daemon_reload);
92c23c5a 48static BUS_DEFINE_PROPERTY_GET_GLOBAL(property_get_empty_strv, "as", 0);
718db961 49
4d3bac56
LP
50static int property_get_can_clean(
51 sd_bus *bus,
52 const char *path,
53 const char *interface,
54 const char *property,
55 sd_bus_message *reply,
56 void *userdata,
57 sd_bus_error *error) {
58
59 Unit *u = userdata;
60 ExecCleanMask mask;
61 int r;
62
63 assert(bus);
64 assert(reply);
65
66 r = unit_can_clean(u, &mask);
67 if (r < 0)
68 return r;
69
70 r = sd_bus_message_open_container(reply, 'a', "s");
71 if (r < 0)
72 return r;
73
74 for (ExecDirectoryType t = 0; t < _EXEC_DIRECTORY_TYPE_MAX; t++) {
75 if (!FLAGS_SET(mask, 1U << t))
76 continue;
77
78 r = sd_bus_message_append(reply, "s", exec_resource_type_to_string(t));
79 if (r < 0)
80 return r;
81 }
82
4fb8f1e8
LP
83 if (FLAGS_SET(mask, EXEC_CLEAN_FDSTORE)) {
84 r = sd_bus_message_append(reply, "s", "fdstore");
85 if (r < 0)
86 return r;
87 }
88
4d3bac56
LP
89 return sd_bus_message_close_container(reply);
90}
91
718db961
LP
92static int property_get_names(
93 sd_bus *bus,
94 const char *path,
95 const char *interface,
96 const char *property,
97 sd_bus_message *reply,
ebcf1f97
LP
98 void *userdata,
99 sd_bus_error *error) {
718db961 100
99534007 101 Unit *u = ASSERT_PTR(userdata);
718db961
LP
102 const char *t;
103 int r;
ea430986 104
718db961
LP
105 assert(bus);
106 assert(reply);
4139c1b2 107
718db961
LP
108 r = sd_bus_message_open_container(reply, 'a', "s");
109 if (r < 0)
110 return r;
4139c1b2 111
4562c355
ZJS
112 r = sd_bus_message_append(reply, "s", u->id);
113 if (r < 0)
114 return r;
115
90e74a66 116 SET_FOREACH(t, u->aliases) {
718db961
LP
117 r = sd_bus_message_append(reply, "s", t);
118 if (r < 0)
119 return r;
120 }
4139c1b2 121
718db961 122 return sd_bus_message_close_container(reply);
4139c1b2
LP
123}
124
718db961
LP
125static int property_get_following(
126 sd_bus *bus,
127 const char *path,
128 const char *interface,
129 const char *property,
130 sd_bus_message *reply,
ebcf1f97
LP
131 void *userdata,
132 sd_bus_error *error) {
718db961
LP
133
134 Unit *u = userdata, *f;
8fe914ec 135
718db961
LP
136 assert(bus);
137 assert(reply);
8fe914ec
LP
138 assert(u);
139
a7f241db 140 f = unit_following(u);
79a60375 141 return sd_bus_message_append(reply, "s", f ? f->id : NULL);
8fe914ec
LP
142}
143
718db961
LP
144static int property_get_dependencies(
145 sd_bus *bus,
146 const char *path,
147 const char *interface,
148 const char *property,
149 sd_bus_message *reply,
ebcf1f97
LP
150 void *userdata,
151 sd_bus_error *error) {
4ec9a8a4 152
15ed3c3a
LP
153 Unit *u = userdata, *other;
154 UnitDependency d;
155 Hashmap *deps;
eef85c4a 156 void *v;
718db961 157 int r;
5301be81 158
718db961
LP
159 assert(bus);
160 assert(reply);
15ed3c3a
LP
161 assert(u);
162
163 d = unit_dependency_from_string(property);
164 assert_se(d >= 0);
165
166 deps = unit_get_dependencies(u, d);
5301be81 167
718db961
LP
168 r = sd_bus_message_open_container(reply, 'a', "s");
169 if (r < 0)
170 return r;
5301be81 171
15ed3c3a
LP
172 HASHMAP_FOREACH_KEY(v, other, deps) {
173 r = sd_bus_message_append(reply, "s", other->id);
718db961
LP
174 if (r < 0)
175 return r;
176 }
5301be81 177
718db961 178 return sd_bus_message_close_container(reply);
5301be81
LP
179}
180
9e615fa3 181static int property_get_mounts_for(
99c14018
YW
182 sd_bus *bus,
183 const char *path,
184 const char *interface,
185 const char *property,
186 sd_bus_message *reply,
187 void *userdata,
188 sd_bus_error *error) {
189
99534007 190 Hashmap **h = ASSERT_PTR(userdata);
99c14018
YW
191 const char *p;
192 void *v;
193 int r;
194
195 assert(bus);
196 assert(reply);
197
198 r = sd_bus_message_open_container(reply, 'a', "s");
199 if (r < 0)
200 return r;
201
90e74a66 202 HASHMAP_FOREACH_KEY(v, p, *h) {
99c14018
YW
203 r = sd_bus_message_append(reply, "s", p);
204 if (r < 0)
205 return r;
206 }
207
208 return sd_bus_message_close_container(reply);
209}
210
d2dc52db
LP
211static int property_get_unit_file_preset(
212 sd_bus *bus,
213 const char *path,
214 const char *interface,
215 const char *property,
216 sd_bus_message *reply,
217 void *userdata,
218 sd_bus_error *error) {
219
99534007 220 Unit *u = ASSERT_PTR(userdata);
d2dc52db
LP
221 int r;
222
223 assert(bus);
224 assert(reply);
d2dc52db
LP
225
226 r = unit_get_unit_file_preset(u);
227
e77e07f6 228 return sd_bus_message_append(reply, "s", preset_action_past_tense_to_string(r));
d2dc52db
LP
229}
230
718db961
LP
231static int property_get_job(
232 sd_bus *bus,
233 const char *path,
234 const char *interface,
235 const char *property,
236 sd_bus_message *reply,
ebcf1f97
LP
237 void *userdata,
238 sd_bus_error *error) {
718db961 239
f93891f3 240 _cleanup_free_ char *p = NULL;
99534007 241 Job **j = ASSERT_PTR(userdata);
38131695 242
718db961
LP
243 assert(bus);
244 assert(reply);
38131695 245
14f7edb0 246 if (!*j)
718db961 247 return sd_bus_message_append(reply, "(uo)", 0, "/");
38131695 248
14f7edb0 249 p = job_dbus_path(*j);
718db961
LP
250 if (!p)
251 return -ENOMEM;
38131695 252
14f7edb0 253 return sd_bus_message_append(reply, "(uo)", (*j)->id, p);
718db961 254}
38131695 255
718db961
LP
256static int property_get_conditions(
257 sd_bus *bus,
258 const char *path,
259 const char *interface,
260 const char *property,
261 sd_bus_message *reply,
ebcf1f97
LP
262 void *userdata,
263 sd_bus_error *error) {
45fb0699 264
59fccdc5 265 const char *(*to_string)(ConditionType type) = NULL;
99534007 266 Condition **list = ASSERT_PTR(userdata);
718db961 267 int r;
45fb0699 268
718db961
LP
269 assert(bus);
270 assert(reply);
59fccdc5
LP
271
272 to_string = streq(property, "Asserts") ? assert_type_to_string : condition_type_to_string;
45fb0699 273
718db961
LP
274 r = sd_bus_message_open_container(reply, 'a', "(sbbsi)");
275 if (r < 0)
276 return r;
45fb0699 277
59fccdc5 278 LIST_FOREACH(conditions, c, *list) {
cc50ef13
LP
279 int tristate;
280
281 tristate =
282 c->result == CONDITION_UNTESTED ? 0 :
283 c->result == CONDITION_SUCCEEDED ? 1 : -1;
284
2c7e050f 285 r = sd_bus_message_append(reply, "(sbbsi)",
59fccdc5 286 to_string(c->type),
2c7e050f 287 c->trigger, c->negate,
cc50ef13 288 c->parameter, tristate);
718db961
LP
289 if (r < 0)
290 return r;
45fb0699 291
718db961 292 }
52990c2e 293
718db961 294 return sd_bus_message_close_container(reply);
52990c2e
ZJS
295}
296
718db961
LP
297static int property_get_load_error(
298 sd_bus *bus,
299 const char *path,
300 const char *interface,
301 const char *property,
302 sd_bus_message *reply,
ebcf1f97
LP
303 void *userdata,
304 sd_bus_error *error) {
52990c2e 305
4afd3348 306 _cleanup_(sd_bus_error_free) sd_bus_error e = SD_BUS_ERROR_NULL;
99534007 307 Unit *u = ASSERT_PTR(userdata);
fd1e3fd8 308 int r;
52990c2e 309
718db961
LP
310 assert(bus);
311 assert(reply);
52990c2e 312
fd1e3fd8
LP
313 r = bus_unit_validate_load_state(u, &e);
314 if (r < 0)
315 return sd_bus_message_append(reply, "(ss)", e.name, e.message);
52990c2e 316
fd1e3fd8 317 return sd_bus_message_append(reply, "(ss)", NULL, NULL);
52990c2e
ZJS
318}
319
ff68472a
ZJS
320static int property_get_markers(
321 sd_bus *bus,
322 const char *path,
323 const char *interface,
324 const char *property,
325 sd_bus_message *reply,
326 void *userdata,
327 sd_bus_error *error) {
328
99534007 329 unsigned *markers = ASSERT_PTR(userdata);
ff68472a
ZJS
330 int r;
331
332 assert(bus);
333 assert(reply);
ff68472a
ZJS
334
335 r = sd_bus_message_open_container(reply, 'a', "s");
336 if (r < 0)
337 return r;
338
339 /* Make sure out values fit in the bitfield. */
340 assert_cc(_UNIT_MARKER_MAX <= sizeof(((Unit){}).markers) * 8);
341
342 for (UnitMarker m = 0; m < _UNIT_MARKER_MAX; m++)
343 if (FLAGS_SET(*markers, 1u << m)) {
344 r = sd_bus_message_append(reply, "s", unit_marker_to_string(m));
345 if (r < 0)
346 return r;
347 }
348
349 return sd_bus_message_close_container(reply);
350}
351
50cbaba4
LP
352static const char *const polkit_message_for_job[_JOB_TYPE_MAX] = {
353 [JOB_START] = N_("Authentication is required to start '$(unit)'."),
354 [JOB_STOP] = N_("Authentication is required to stop '$(unit)'."),
355 [JOB_RELOAD] = N_("Authentication is required to reload '$(unit)'."),
356 [JOB_RESTART] = N_("Authentication is required to restart '$(unit)'."),
357 [JOB_TRY_RESTART] = N_("Authentication is required to restart '$(unit)'."),
358};
359
1d22e906 360int bus_unit_method_start_generic(
1d22e906
LP
361 sd_bus_message *message,
362 Unit *u,
363 JobType job_type,
364 bool reload_if_possible,
365 sd_bus_error *error) {
366
f4328267 367 BusUnitQueueFlags job_flags = reload_if_possible ? BUS_UNIT_QUEUE_RELOAD_IF_POSSIBLE : 0;
e59ba56d 368 const char *smode, *verb;
718db961
LP
369 JobMode mode;
370 int r;
9f39404c 371
718db961 372 assert(message);
9f39404c 373 assert(u);
718db961 374 assert(job_type >= 0 && job_type < _JOB_TYPE_MAX);
9f39404c 375
61ea63f1
EV
376 r = mac_selinux_unit_access_check(
377 u, message,
94bd7323
EV
378 job_type_to_access_method(job_type),
379 error);
1d22e906
LP
380 if (r < 0)
381 return r;
382
718db961
LP
383 r = sd_bus_message_read(message, "s", &smode);
384 if (r < 0)
ebcf1f97 385 return r;
9f39404c 386
718db961
LP
387 mode = job_mode_from_string(smode);
388 if (mode < 0)
ebcf1f97 389 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Job mode %s invalid", smode);
9f39404c 390
88ced61b 391 if (reload_if_possible)
e59ba56d 392 verb = strjoina("reload-or-", job_type_to_string(job_type));
88ced61b 393 else
e59ba56d 394 verb = job_type_to_string(job_type);
88ced61b 395
f4328267
LB
396 if (sd_bus_message_is_method_call(message, NULL, "StartUnitWithFlags")) {
397 uint64_t input_flags = 0;
398
399 r = sd_bus_message_read(message, "t", &input_flags);
400 if (r < 0)
401 return r;
402 /* Let clients know that this version doesn't support any flags at the moment. */
403 if (input_flags != 0)
404 return sd_bus_reply_method_errorf(message, SD_BUS_ERROR_INVALID_ARGS,
405 "Invalid 'flags' parameter '%" PRIu64 "'",
406 input_flags);
f4328267
LB
407 }
408
88ced61b
MC
409 r = bus_verify_manage_units_async_full(
410 u,
411 verb,
ebfb1b57 412 polkit_message_for_job[job_type],
88ced61b
MC
413 message,
414 error);
1d22e906
LP
415 if (r < 0)
416 return r;
417 if (r == 0)
418 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
419
f4328267 420 return bus_unit_queue_job(message, u, job_type, mode, job_flags, error);
9f39404c
LP
421}
422
7e570d57 423static int bus_unit_method_start(sd_bus_message *message, void *userdata, sd_bus_error *error) {
19070062 424 return bus_unit_method_start_generic(message, userdata, JOB_START, false, error);
718db961 425}
b548631a 426
7e570d57 427static int bus_unit_method_stop(sd_bus_message *message, void *userdata, sd_bus_error *error) {
19070062 428 return bus_unit_method_start_generic(message, userdata, JOB_STOP, false, error);
718db961
LP
429}
430
7e570d57 431static int bus_unit_method_reload(sd_bus_message *message, void *userdata, sd_bus_error *error) {
19070062 432 return bus_unit_method_start_generic(message, userdata, JOB_RELOAD, false, error);
718db961 433}
0a524ba7 434
7e570d57 435static int bus_unit_method_restart(sd_bus_message *message, void *userdata, sd_bus_error *error) {
19070062 436 return bus_unit_method_start_generic(message, userdata, JOB_RESTART, false, error);
718db961 437}
8a0867d6 438
7e570d57 439static int bus_unit_method_try_restart(sd_bus_message *message, void *userdata, sd_bus_error *error) {
19070062 440 return bus_unit_method_start_generic(message, userdata, JOB_TRY_RESTART, false, error);
718db961 441}
cad45ba1 442
7e570d57 443static int bus_unit_method_reload_or_restart(sd_bus_message *message, void *userdata, sd_bus_error *error) {
19070062 444 return bus_unit_method_start_generic(message, userdata, JOB_RESTART, true, error);
718db961 445}
8a0867d6 446
7e570d57 447static int bus_unit_method_reload_or_try_restart(sd_bus_message *message, void *userdata, sd_bus_error *error) {
19070062 448 return bus_unit_method_start_generic(message, userdata, JOB_TRY_RESTART, true, error);
718db961 449}
8a0867d6 450
50cbaba4
LP
451int bus_unit_method_enqueue_job(sd_bus_message *message, void *userdata, sd_bus_error *error) {
452 BusUnitQueueFlags flags = BUS_UNIT_QUEUE_VERBOSE_REPLY;
453 const char *jtype, *smode;
99534007 454 Unit *u = ASSERT_PTR(userdata);
50cbaba4
LP
455 JobType type;
456 JobMode mode;
457 int r;
458
459 assert(message);
50cbaba4
LP
460
461 r = sd_bus_message_read(message, "ss", &jtype, &smode);
462 if (r < 0)
463 return r;
464
465 /* Parse the two magic reload types "reload-or-…" manually */
466 if (streq(jtype, "reload-or-restart")) {
467 type = JOB_RESTART;
468 flags |= BUS_UNIT_QUEUE_RELOAD_IF_POSSIBLE;
469 } else if (streq(jtype, "reload-or-try-restart")) {
470 type = JOB_TRY_RESTART;
471 flags |= BUS_UNIT_QUEUE_RELOAD_IF_POSSIBLE;
472 } else {
473 /* And the rest generically */
474 type = job_type_from_string(jtype);
475 if (type < 0)
476 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Job type %s invalid", jtype);
477 }
478
479 mode = job_mode_from_string(smode);
480 if (mode < 0)
481 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Job mode %s invalid", smode);
482
483 r = mac_selinux_unit_access_check(
484 u, message,
485 job_type_to_access_method(type),
486 error);
487 if (r < 0)
488 return r;
489
490 r = bus_verify_manage_units_async_full(
491 u,
492 jtype,
50cbaba4 493 polkit_message_for_job[type],
50cbaba4
LP
494 message,
495 error);
496 if (r < 0)
497 return r;
498 if (r == 0)
499 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
500
501 return bus_unit_queue_job(message, u, type, mode, flags, error);
502}
503
19070062 504int bus_unit_method_kill(sd_bus_message *message, void *userdata, sd_bus_error *error) {
99534007 505 Unit *u = ASSERT_PTR(userdata);
a721cd00 506 int32_t value = 0;
718db961
LP
507 const char *swho;
508 int32_t signo;
509 KillWho who;
a721cd00 510 int r, code;
5632e374 511
718db961 512 assert(message);
cad45ba1 513
1d22e906 514 r = mac_selinux_unit_access_check(u, message, "stop", error);
283868e1
SW
515 if (r < 0)
516 return r;
283868e1 517
718db961
LP
518 r = sd_bus_message_read(message, "si", &swho, &signo);
519 if (r < 0)
ebcf1f97 520 return r;
718db961 521
a721cd00
LP
522 if (startswith(sd_bus_message_get_member(message), "QueueSignal")) {
523 r = sd_bus_message_read(message, "i", &value);
524 if (r < 0)
525 return r;
526
527 code = SI_QUEUE;
528 } else
529 code = SI_USER;
530
718db961
LP
531 if (isempty(swho))
532 who = KILL_ALL;
533 else {
534 who = kill_who_from_string(swho);
535 if (who < 0)
a721cd00 536 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid who argument: %s", swho);
718db961 537 }
5632e374 538
6eb7c172 539 if (!SIGNAL_VALID(signo))
1b09b81c 540 return sd_bus_error_set(error, SD_BUS_ERROR_INVALID_ARGS, "Signal number out of range.");
8e2af478 541
a721cd00
LP
542 if (code == SI_QUEUE && !((signo >= SIGRTMIN) && (signo <= SIGRTMAX)))
543 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS,
544 "Value parameter only accepted for realtime signals (SIGRTMIN…SIGRTMAX), refusing for signal SIG%s.", signal_to_string(signo));
545
88ced61b
MC
546 r = bus_verify_manage_units_async_full(
547 u,
548 "kill",
1d445eda 549 N_("Authentication is required to send a UNIX signal to the processes of '$(unit)'."),
88ced61b
MC
550 message,
551 error);
ebcf1f97
LP
552 if (r < 0)
553 return r;
1d22e906
LP
554 if (r == 0)
555 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
8e2af478 556
a721cd00 557 r = unit_kill(u, who, signo, code, value, error);
718db961 558 if (r < 0)
ebcf1f97 559 return r;
8e2af478 560
df2d202e 561 return sd_bus_reply_method_return(message, NULL);
718db961 562}
8e2af478 563
19070062 564int bus_unit_method_reset_failed(sd_bus_message *message, void *userdata, sd_bus_error *error) {
99534007 565 Unit *u = ASSERT_PTR(userdata);
ebcf1f97 566 int r;
b548631a 567
718db961 568 assert(message);
b548631a 569
1d22e906 570 r = mac_selinux_unit_access_check(u, message, "reload", error);
283868e1
SW
571 if (r < 0)
572 return r;
283868e1 573
88ced61b
MC
574 r = bus_verify_manage_units_async_full(
575 u,
576 "reset-failed",
88ced61b
MC
577 N_("Authentication is required to reset the \"failed\" state of '$(unit)'."),
578 message,
579 error);
ebcf1f97
LP
580 if (r < 0)
581 return r;
1d22e906
LP
582 if (r == 0)
583 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
b548631a 584
718db961 585 unit_reset_failed(u);
b548631a 586
df2d202e 587 return sd_bus_reply_method_return(message, NULL);
ea430986
LP
588}
589
19070062 590int bus_unit_method_set_properties(sd_bus_message *message, void *userdata, sd_bus_error *error) {
99534007 591 Unit *u = ASSERT_PTR(userdata);
718db961 592 int runtime, r;
ea430986 593
ea430986 594 assert(message);
80fbf05e 595
1d22e906 596 r = mac_selinux_unit_access_check(u, message, "start", error);
283868e1
SW
597 if (r < 0)
598 return r;
283868e1 599
718db961
LP
600 r = sd_bus_message_read(message, "b", &runtime);
601 if (r < 0)
ebcf1f97 602 return r;
2cccbca4 603
88ced61b
MC
604 r = bus_verify_manage_units_async_full(
605 u,
606 "set-property",
88ced61b
MC
607 N_("Authentication is required to set properties on '$(unit)'."),
608 message,
609 error);
ebcf1f97
LP
610 if (r < 0)
611 return r;
1d22e906
LP
612 if (r == 0)
613 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
cad45ba1 614
ebcf1f97 615 r = bus_unit_set_properties(u, message, runtime ? UNIT_RUNTIME : UNIT_PERSISTENT, true, error);
718db961 616 if (r < 0)
ebcf1f97 617 return r;
2cccbca4 618
df2d202e 619 return sd_bus_reply_method_return(message, NULL);
718db961 620}
2cccbca4 621
05a98afd 622int bus_unit_method_ref(sd_bus_message *message, void *userdata, sd_bus_error *error) {
99534007 623 Unit *u = ASSERT_PTR(userdata);
05a98afd
LP
624 int r;
625
626 assert(message);
05a98afd
LP
627
628 r = mac_selinux_unit_access_check(u, message, "start", error);
629 if (r < 0)
630 return r;
631
632 r = bus_verify_manage_units_async_full(
633 u,
634 "ref",
7b36fb9f 635 /* polkit_message= */ NULL,
05a98afd
LP
636 message,
637 error);
638 if (r < 0)
639 return r;
640 if (r == 0)
641 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
642
643 r = bus_unit_track_add_sender(u, message);
644 if (r < 0)
645 return r;
646
647 return sd_bus_reply_method_return(message, NULL);
648}
649
650int bus_unit_method_unref(sd_bus_message *message, void *userdata, sd_bus_error *error) {
99534007 651 Unit *u = ASSERT_PTR(userdata);
05a98afd
LP
652 int r;
653
654 assert(message);
05a98afd
LP
655
656 r = bus_unit_track_remove_sender(u, message);
657 if (r == -EUNATCH)
1b09b81c 658 return sd_bus_error_set(error, BUS_ERROR_NOT_REFERENCED, "Unit has not been referenced yet.");
05a98afd
LP
659 if (r < 0)
660 return r;
661
662 return sd_bus_reply_method_return(message, NULL);
663}
664
4d3bac56
LP
665int bus_unit_method_clean(sd_bus_message *message, void *userdata, sd_bus_error *error) {
666 ExecCleanMask mask = 0;
99534007 667 Unit *u = ASSERT_PTR(userdata);
4d3bac56
LP
668 int r;
669
670 assert(message);
4d3bac56
LP
671
672 r = mac_selinux_unit_access_check(u, message, "stop", error);
673 if (r < 0)
674 return r;
675
676 r = sd_bus_message_enter_container(message, 'a', "s");
677 if (r < 0)
678 return r;
679
680 for (;;) {
4fb8f1e8 681 ExecCleanMask m;
4d3bac56
LP
682 const char *i;
683
684 r = sd_bus_message_read(message, "s", &i);
685 if (r < 0)
686 return r;
687 if (r == 0)
688 break;
689
4fb8f1e8
LP
690 m = exec_clean_mask_from_string(i);
691 if (m < 0)
692 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid resource type: %s", i);
4d3bac56 693
4fb8f1e8 694 mask |= m;
4d3bac56
LP
695 }
696
697 r = sd_bus_message_exit_container(message);
698 if (r < 0)
699 return r;
700
701 r = bus_verify_manage_units_async_full(
702 u,
703 "clean",
4d3bac56 704 N_("Authentication is required to delete files and directories associated with '$(unit)'."),
4d3bac56
LP
705 message,
706 error);
707 if (r < 0)
708 return r;
709 if (r == 0)
710 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
711
712 r = unit_clean(u, mask);
713 if (r == -EOPNOTSUPP)
0017be9d 714 return sd_bus_error_setf(error, SD_BUS_ERROR_NOT_SUPPORTED, "Unit '%s' does not support cleaning.", u->id);
4d3bac56 715 if (r == -EUNATCH)
1b09b81c 716 return sd_bus_error_set(error, BUS_ERROR_NOTHING_TO_CLEAN, "No matching resources found.");
4d3bac56 717 if (r == -EBUSY)
1b09b81c 718 return sd_bus_error_set(error, BUS_ERROR_UNIT_BUSY, "Unit is not inactive or has pending job.");
4d3bac56
LP
719 if (r < 0)
720 return r;
721
722 return sd_bus_reply_method_return(message, NULL);
723}
724
d9e45bc3
MS
725static int bus_unit_method_freezer_generic(sd_bus_message *message, void *userdata, sd_bus_error *error, FreezerAction action) {
726 const char* perm;
99534007 727 Unit *u = ASSERT_PTR(userdata);
d9e45bc3
MS
728 bool reply_no_delay = false;
729 int r;
730
731 assert(message);
d9e45bc3
MS
732 assert(IN_SET(action, FREEZER_FREEZE, FREEZER_THAW));
733
16b6af6a 734 perm = action == FREEZER_FREEZE ? "stop" : "start";
d9e45bc3
MS
735
736 r = mac_selinux_unit_access_check(u, message, perm, error);
737 if (r < 0)
738 return r;
739
740 r = bus_verify_manage_units_async_full(
741 u,
742 perm,
d9e45bc3 743 N_("Authentication is required to freeze or thaw the processes of '$(unit)' unit."),
d9e45bc3
MS
744 message,
745 error);
746 if (r < 0)
747 return r;
748 if (r == 0)
749 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
750
16b6af6a 751 r = unit_freezer_action(u, action);
d9e45bc3 752 if (r == -EOPNOTSUPP)
16b6af6a 753 return sd_bus_error_setf(error, SD_BUS_ERROR_NOT_SUPPORTED, "Unit does not support freeze/thaw.");
d9e45bc3 754 if (r == -EBUSY)
1b09b81c 755 return sd_bus_error_set(error, BUS_ERROR_UNIT_BUSY, "Unit has a pending job.");
d9e45bc3 756 if (r == -EHOSTDOWN)
1b09b81c 757 return sd_bus_error_set(error, BUS_ERROR_UNIT_INACTIVE, "Unit is inactive.");
d9e45bc3 758 if (r == -EALREADY)
16b6af6a
AV
759 return sd_bus_error_setf(error, SD_BUS_ERROR_FAILED, "Previously requested freezer operation for unit is still in progress.");
760 if (r == -ECHILD)
761 return sd_bus_error_setf(error, SD_BUS_ERROR_FAILED, "Unit is frozen by a parent slice.");
d9e45bc3
MS
762 if (r < 0)
763 return r;
764 if (r == 0)
765 reply_no_delay = true;
766
af1e3365 767 if (u->pending_freezer_invocation) {
3d19e122 768 bus_unit_send_pending_freezer_message(u, true);
af1e3365 769 assert(!u->pending_freezer_invocation);
3d19e122 770 }
d9e45bc3 771
af1e3365 772 u->pending_freezer_invocation = sd_bus_message_ref(message);
d9e45bc3
MS
773
774 if (reply_no_delay) {
3d19e122 775 r = bus_unit_send_pending_freezer_message(u, false);
d9e45bc3
MS
776 if (r < 0)
777 return r;
778 }
779
780 return 1;
781}
782
783int bus_unit_method_thaw(sd_bus_message *message, void *userdata, sd_bus_error *error) {
784 return bus_unit_method_freezer_generic(message, userdata, error, FREEZER_THAW);
785}
786
787int bus_unit_method_freeze(sd_bus_message *message, void *userdata, sd_bus_error *error) {
788 return bus_unit_method_freezer_generic(message, userdata, error, FREEZER_FREEZE);
789}
790
bedea99d
LP
791static int property_get_refs(
792 sd_bus *bus,
793 const char *path,
794 const char *interface,
795 const char *property,
796 sd_bus_message *reply,
797 void *userdata,
798 sd_bus_error *error) {
799
800 Unit *u = userdata;
bedea99d
LP
801 int r;
802
803 assert(bus);
804 assert(reply);
805
806 r = sd_bus_message_open_container(reply, 'a', "s");
807 if (r < 0)
808 return r;
809
95d1e21e
ZJS
810 for (const char *i = sd_bus_track_first(u->bus_track); i; i = sd_bus_track_next(u->bus_track)) {
811 int c;
bedea99d
LP
812
813 c = sd_bus_track_count_name(u->bus_track, i);
814 if (c < 0)
815 return c;
816
817 /* Add the item multiple times if the ref count for each is above 1 */
95d1e21e 818 for (int k = 0; k < c; k++) {
bedea99d
LP
819 r = sd_bus_message_append(reply, "s", i);
820 if (r < 0)
821 return r;
822 }
823 }
824
825 return sd_bus_message_close_container(reply);
826}
827
718db961
LP
828const sd_bus_vtable bus_unit_vtable[] = {
829 SD_BUS_VTABLE_START(0),
830
556089dc 831 SD_BUS_PROPERTY("Id", "s", NULL, offsetof(Unit, id), SD_BUS_VTABLE_PROPERTY_CONST),
4562c355 832 SD_BUS_PROPERTY("Names", "as", property_get_names, 0, SD_BUS_VTABLE_PROPERTY_CONST),
718db961 833 SD_BUS_PROPERTY("Following", "s", property_get_following, 0, 0),
15ed3c3a
LP
834 SD_BUS_PROPERTY("Requires", "as", property_get_dependencies, 0, SD_BUS_VTABLE_PROPERTY_CONST),
835 SD_BUS_PROPERTY("Requisite", "as", property_get_dependencies, 0, SD_BUS_VTABLE_PROPERTY_CONST),
836 SD_BUS_PROPERTY("Wants", "as", property_get_dependencies, 0, SD_BUS_VTABLE_PROPERTY_CONST),
837 SD_BUS_PROPERTY("BindsTo", "as", property_get_dependencies, 0, SD_BUS_VTABLE_PROPERTY_CONST),
838 SD_BUS_PROPERTY("PartOf", "as", property_get_dependencies, 0, SD_BUS_VTABLE_PROPERTY_CONST),
adc1b76c 839 SD_BUS_PROPERTY("Upholds", "as", property_get_dependencies, 0, SD_BUS_VTABLE_PROPERTY_CONST),
15ed3c3a
LP
840 SD_BUS_PROPERTY("RequiredBy", "as", property_get_dependencies, 0, SD_BUS_VTABLE_PROPERTY_CONST),
841 SD_BUS_PROPERTY("RequisiteOf", "as", property_get_dependencies, 0, SD_BUS_VTABLE_PROPERTY_CONST),
842 SD_BUS_PROPERTY("WantedBy", "as", property_get_dependencies, 0, SD_BUS_VTABLE_PROPERTY_CONST),
843 SD_BUS_PROPERTY("BoundBy", "as", property_get_dependencies, 0, SD_BUS_VTABLE_PROPERTY_CONST),
adc1b76c 844 SD_BUS_PROPERTY("UpheldBy", "as", property_get_dependencies, 0, SD_BUS_VTABLE_PROPERTY_CONST),
15ed3c3a
LP
845 SD_BUS_PROPERTY("ConsistsOf", "as", property_get_dependencies, 0, SD_BUS_VTABLE_PROPERTY_CONST),
846 SD_BUS_PROPERTY("Conflicts", "as", property_get_dependencies, 0, SD_BUS_VTABLE_PROPERTY_CONST),
847 SD_BUS_PROPERTY("ConflictedBy", "as", property_get_dependencies, 0, SD_BUS_VTABLE_PROPERTY_CONST),
848 SD_BUS_PROPERTY("Before", "as", property_get_dependencies, 0, SD_BUS_VTABLE_PROPERTY_CONST),
849 SD_BUS_PROPERTY("After", "as", property_get_dependencies, 0, SD_BUS_VTABLE_PROPERTY_CONST),
294446dc
LP
850 SD_BUS_PROPERTY("OnSuccess", "as", property_get_dependencies, 0, SD_BUS_VTABLE_PROPERTY_CONST),
851 SD_BUS_PROPERTY("OnSuccessOf", "as", property_get_dependencies, 0, SD_BUS_VTABLE_PROPERTY_CONST),
adc1b76c
YW
852 SD_BUS_PROPERTY("OnFailure", "as", property_get_dependencies, 0, SD_BUS_VTABLE_PROPERTY_CONST),
853 SD_BUS_PROPERTY("OnFailureOf", "as", property_get_dependencies, 0, SD_BUS_VTABLE_PROPERTY_CONST),
15ed3c3a
LP
854 SD_BUS_PROPERTY("Triggers", "as", property_get_dependencies, 0, SD_BUS_VTABLE_PROPERTY_CONST),
855 SD_BUS_PROPERTY("TriggeredBy", "as", property_get_dependencies, 0, SD_BUS_VTABLE_PROPERTY_CONST),
856 SD_BUS_PROPERTY("PropagatesReloadTo", "as", property_get_dependencies, 0, SD_BUS_VTABLE_PROPERTY_CONST),
857 SD_BUS_PROPERTY("ReloadPropagatedFrom", "as", property_get_dependencies, 0, SD_BUS_VTABLE_PROPERTY_CONST),
ffec78c0
LP
858 SD_BUS_PROPERTY("PropagatesStopTo", "as", property_get_dependencies, 0, SD_BUS_VTABLE_PROPERTY_CONST),
859 SD_BUS_PROPERTY("StopPropagatedFrom", "as", property_get_dependencies, 0, SD_BUS_VTABLE_PROPERTY_CONST),
15ed3c3a 860 SD_BUS_PROPERTY("JoinsNamespaceOf", "as", property_get_dependencies, 0, SD_BUS_VTABLE_PROPERTY_CONST),
d219a2b0 861 SD_BUS_PROPERTY("SliceOf", "as", property_get_dependencies, 0, SD_BUS_VTABLE_PROPERTY_CONST),
9e615fa3
LB
862 SD_BUS_PROPERTY("RequiresMountsFor", "as", property_get_mounts_for, offsetof(Unit, mounts_for[UNIT_MOUNT_REQUIRES]), SD_BUS_VTABLE_PROPERTY_CONST),
863 SD_BUS_PROPERTY("WantsMountsFor", "as", property_get_mounts_for, offsetof(Unit, mounts_for[UNIT_MOUNT_WANTS]), SD_BUS_VTABLE_PROPERTY_CONST),
556089dc
LP
864 SD_BUS_PROPERTY("Documentation", "as", NULL, offsetof(Unit, documentation), SD_BUS_VTABLE_PROPERTY_CONST),
865 SD_BUS_PROPERTY("Description", "s", property_get_description, 0, SD_BUS_VTABLE_PROPERTY_CONST),
23e9a7dd 866 SD_BUS_PROPERTY("AccessSELinuxContext", "s", NULL, offsetof(Unit, access_selinux_context), SD_BUS_VTABLE_PROPERTY_CONST),
556089dc 867 SD_BUS_PROPERTY("LoadState", "s", property_get_load_state, offsetof(Unit, load_state), SD_BUS_VTABLE_PROPERTY_CONST),
718db961 868 SD_BUS_PROPERTY("ActiveState", "s", property_get_active_state, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
d9e45bc3 869 SD_BUS_PROPERTY("FreezerState", "s", property_get_freezer_state, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
718db961 870 SD_BUS_PROPERTY("SubState", "s", property_get_sub_state, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
556089dc
LP
871 SD_BUS_PROPERTY("FragmentPath", "s", NULL, offsetof(Unit, fragment_path), SD_BUS_VTABLE_PROPERTY_CONST),
872 SD_BUS_PROPERTY("SourcePath", "s", NULL, offsetof(Unit, source_path), SD_BUS_VTABLE_PROPERTY_CONST),
873 SD_BUS_PROPERTY("DropInPaths", "as", NULL, offsetof(Unit, dropin_paths), SD_BUS_VTABLE_PROPERTY_CONST),
718db961 874 SD_BUS_PROPERTY("UnitFileState", "s", property_get_unit_file_state, 0, 0),
d2dc52db 875 SD_BUS_PROPERTY("UnitFilePreset", "s", property_get_unit_file_preset, 0, 0),
a483fb59 876 BUS_PROPERTY_DUAL_TIMESTAMP("StateChangeTimestamp", offsetof(Unit, state_change_timestamp), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
718db961
LP
877 BUS_PROPERTY_DUAL_TIMESTAMP("InactiveExitTimestamp", offsetof(Unit, inactive_exit_timestamp), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
878 BUS_PROPERTY_DUAL_TIMESTAMP("ActiveEnterTimestamp", offsetof(Unit, active_enter_timestamp), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
879 BUS_PROPERTY_DUAL_TIMESTAMP("ActiveExitTimestamp", offsetof(Unit, active_exit_timestamp), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
880 BUS_PROPERTY_DUAL_TIMESTAMP("InactiveEnterTimestamp", offsetof(Unit, inactive_enter_timestamp), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
556089dc
LP
881 SD_BUS_PROPERTY("CanStart", "b", property_get_can_start, 0, SD_BUS_VTABLE_PROPERTY_CONST),
882 SD_BUS_PROPERTY("CanStop", "b", property_get_can_stop, 0, SD_BUS_VTABLE_PROPERTY_CONST),
883 SD_BUS_PROPERTY("CanReload", "b", property_get_can_reload, 0, SD_BUS_VTABLE_PROPERTY_CONST),
884 SD_BUS_PROPERTY("CanIsolate", "b", property_get_can_isolate, 0, SD_BUS_VTABLE_PROPERTY_CONST),
4d3bac56 885 SD_BUS_PROPERTY("CanClean", "as", property_get_can_clean, 0, SD_BUS_VTABLE_PROPERTY_CONST),
d9e45bc3 886 SD_BUS_PROPERTY("CanFreeze", "b", property_get_can_freeze, 0, SD_BUS_VTABLE_PROPERTY_CONST),
14f7edb0 887 SD_BUS_PROPERTY("Job", "(uo)", property_get_job, offsetof(Unit, job), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
556089dc
LP
888 SD_BUS_PROPERTY("StopWhenUnneeded", "b", bus_property_get_bool, offsetof(Unit, stop_when_unneeded), SD_BUS_VTABLE_PROPERTY_CONST),
889 SD_BUS_PROPERTY("RefuseManualStart", "b", bus_property_get_bool, offsetof(Unit, refuse_manual_start), SD_BUS_VTABLE_PROPERTY_CONST),
890 SD_BUS_PROPERTY("RefuseManualStop", "b", bus_property_get_bool, offsetof(Unit, refuse_manual_stop), SD_BUS_VTABLE_PROPERTY_CONST),
891 SD_BUS_PROPERTY("AllowIsolate", "b", bus_property_get_bool, offsetof(Unit, allow_isolate), SD_BUS_VTABLE_PROPERTY_CONST),
892 SD_BUS_PROPERTY("DefaultDependencies", "b", bus_property_get_bool, offsetof(Unit, default_dependencies), SD_BUS_VTABLE_PROPERTY_CONST),
559214cb 893 SD_BUS_PROPERTY("SurviveFinalKillSignal", "b", bus_property_get_bool, offsetof(Unit, survive_final_kill_signal), SD_BUS_VTABLE_PROPERTY_CONST),
ecfcf024
VP
894 SD_BUS_PROPERTY("OnSuccesJobMode", "s", property_get_job_mode, offsetof(Unit, on_success_job_mode), SD_BUS_VTABLE_PROPERTY_CONST|SD_BUS_VTABLE_HIDDEN), /* deprecated */
895 SD_BUS_PROPERTY("OnSuccessJobMode", "s", property_get_job_mode, offsetof(Unit, on_success_job_mode), SD_BUS_VTABLE_PROPERTY_CONST),
556089dc
LP
896 SD_BUS_PROPERTY("OnFailureJobMode", "s", property_get_job_mode, offsetof(Unit, on_failure_job_mode), SD_BUS_VTABLE_PROPERTY_CONST),
897 SD_BUS_PROPERTY("IgnoreOnIsolate", "b", bus_property_get_bool, offsetof(Unit, ignore_on_isolate), SD_BUS_VTABLE_PROPERTY_CONST),
e30a3ba1 898 SD_BUS_PROPERTY("NeedDaemonReload", "b", property_get_need_daemon_reload, 0, 0),
ff68472a 899 SD_BUS_PROPERTY("Markers", "as", property_get_markers, offsetof(Unit, markers), 0),
556089dc 900 SD_BUS_PROPERTY("JobTimeoutUSec", "t", bus_property_get_usec, offsetof(Unit, job_timeout), SD_BUS_VTABLE_PROPERTY_CONST),
a2df3ea4 901 SD_BUS_PROPERTY("JobRunningTimeoutUSec", "t", bus_property_get_usec, offsetof(Unit, job_running_timeout), SD_BUS_VTABLE_PROPERTY_CONST),
c44a285c 902 SD_BUS_PROPERTY("JobTimeoutAction", "s", bus_property_get_emergency_action, offsetof(Unit, job_timeout_action), SD_BUS_VTABLE_PROPERTY_CONST),
f189ab18 903 SD_BUS_PROPERTY("JobTimeoutRebootArgument", "s", NULL, offsetof(Unit, job_timeout_reboot_arg), SD_BUS_VTABLE_PROPERTY_CONST),
718db961 904 SD_BUS_PROPERTY("ConditionResult", "b", bus_property_get_bool, offsetof(Unit, condition_result), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
59fccdc5 905 SD_BUS_PROPERTY("AssertResult", "b", bus_property_get_bool, offsetof(Unit, assert_result), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
718db961 906 BUS_PROPERTY_DUAL_TIMESTAMP("ConditionTimestamp", offsetof(Unit, condition_timestamp), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
59fccdc5 907 BUS_PROPERTY_DUAL_TIMESTAMP("AssertTimestamp", offsetof(Unit, assert_timestamp), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
e18f8852
LP
908 SD_BUS_PROPERTY("Conditions", "a(sbbsi)", property_get_conditions, offsetof(Unit, conditions), SD_BUS_VTABLE_PROPERTY_EMITS_INVALIDATION),
909 SD_BUS_PROPERTY("Asserts", "a(sbbsi)", property_get_conditions, offsetof(Unit, asserts), SD_BUS_VTABLE_PROPERTY_EMITS_INVALIDATION),
fd1e3fd8 910 SD_BUS_PROPERTY("LoadError", "(ss)", property_get_load_error, 0, SD_BUS_VTABLE_PROPERTY_CONST),
556089dc 911 SD_BUS_PROPERTY("Transient", "b", bus_property_get_bool, offsetof(Unit, transient), SD_BUS_VTABLE_PROPERTY_CONST),
f5869324 912 SD_BUS_PROPERTY("Perpetual", "b", bus_property_get_bool, offsetof(Unit, perpetual), SD_BUS_VTABLE_PROPERTY_CONST),
7bf081a1
ZJS
913 SD_BUS_PROPERTY("StartLimitIntervalUSec", "t", bus_property_get_usec, offsetof(Unit, start_ratelimit.interval), SD_BUS_VTABLE_PROPERTY_CONST),
914 SD_BUS_PROPERTY("StartLimitBurst", "u", bus_property_get_unsigned, offsetof(Unit, start_ratelimit.burst), SD_BUS_VTABLE_PROPERTY_CONST),
c44a285c
FL
915 SD_BUS_PROPERTY("StartLimitAction", "s", bus_property_get_emergency_action, offsetof(Unit, start_limit_action), SD_BUS_VTABLE_PROPERTY_CONST),
916 SD_BUS_PROPERTY("FailureAction", "s", bus_property_get_emergency_action, offsetof(Unit, failure_action), SD_BUS_VTABLE_PROPERTY_CONST),
7af67e9a 917 SD_BUS_PROPERTY("FailureActionExitStatus", "i", bus_property_get_int, offsetof(Unit, failure_action_exit_status), SD_BUS_VTABLE_PROPERTY_CONST),
c44a285c 918 SD_BUS_PROPERTY("SuccessAction", "s", bus_property_get_emergency_action, offsetof(Unit, success_action), SD_BUS_VTABLE_PROPERTY_CONST),
7af67e9a 919 SD_BUS_PROPERTY("SuccessActionExitStatus", "i", bus_property_get_int, offsetof(Unit, success_action_exit_status), SD_BUS_VTABLE_PROPERTY_CONST),
6bf0f408 920 SD_BUS_PROPERTY("RebootArgument", "s", NULL, offsetof(Unit, reboot_arg), SD_BUS_VTABLE_PROPERTY_CONST),
af92c603 921 SD_BUS_PROPERTY("InvocationID", "ay", bus_property_get_id128, offsetof(Unit, invocation_id), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
641e0d7a 922 SD_BUS_PROPERTY("CollectMode", "s", property_get_collect_mode, offsetof(Unit, collect_mode), SD_BUS_VTABLE_PROPERTY_CONST),
bedea99d 923 SD_BUS_PROPERTY("Refs", "as", property_get_refs, 0, 0),
48b92b37 924 SD_BUS_PROPERTY("ActivationDetails", "a(ss)", bus_property_get_activation_details, offsetof(Unit, activation_details), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
718db961 925
a008b6d7 926 SD_BUS_METHOD_WITH_ARGS("Start",
927 SD_BUS_ARGS("s", mode),
928 SD_BUS_RESULT("o", job),
929 bus_unit_method_start,
930 SD_BUS_VTABLE_UNPRIVILEGED),
931 SD_BUS_METHOD_WITH_ARGS("Stop",
932 SD_BUS_ARGS("s", mode),
933 SD_BUS_RESULT("o", job),
934 bus_unit_method_stop,
935 SD_BUS_VTABLE_UNPRIVILEGED),
936 SD_BUS_METHOD_WITH_ARGS("Reload",
937 SD_BUS_ARGS("s", mode),
938 SD_BUS_RESULT("o", job),
939 bus_unit_method_reload,
940 SD_BUS_VTABLE_UNPRIVILEGED),
941 SD_BUS_METHOD_WITH_ARGS("Restart",
942 SD_BUS_ARGS("s", mode),
943 SD_BUS_RESULT("o", job),
944 bus_unit_method_restart,
945 SD_BUS_VTABLE_UNPRIVILEGED),
946 SD_BUS_METHOD_WITH_ARGS("TryRestart",
947 SD_BUS_ARGS("s", mode),
948 SD_BUS_RESULT("o", job),
949 bus_unit_method_try_restart,
950 SD_BUS_VTABLE_UNPRIVILEGED),
951 SD_BUS_METHOD_WITH_ARGS("ReloadOrRestart",
952 SD_BUS_ARGS("s", mode),
953 SD_BUS_RESULT("o", job),
954 bus_unit_method_reload_or_restart,
955 SD_BUS_VTABLE_UNPRIVILEGED),
956 SD_BUS_METHOD_WITH_ARGS("ReloadOrTryRestart",
957 SD_BUS_ARGS("s", mode),
958 SD_BUS_RESULT("o", job),
959 bus_unit_method_reload_or_try_restart,
960 SD_BUS_VTABLE_UNPRIVILEGED),
961 SD_BUS_METHOD_WITH_ARGS("EnqueueJob",
962 SD_BUS_ARGS("s", job_type, "s", job_mode),
963 SD_BUS_RESULT("u", job_id, "o", job_path, "s", unit_id, "o", unit_path, "s", job_type, "a(uosos)", affected_jobs),
964 bus_unit_method_enqueue_job,
965 SD_BUS_VTABLE_UNPRIVILEGED),
966 SD_BUS_METHOD_WITH_ARGS("Kill",
967 SD_BUS_ARGS("s", whom, "i", signal),
968 SD_BUS_NO_RESULT,
969 bus_unit_method_kill,
970 SD_BUS_VTABLE_UNPRIVILEGED),
a721cd00
LP
971 SD_BUS_METHOD_WITH_ARGS("QueueSignal",
972 SD_BUS_ARGS("s", whom, "i", signal, "i", value),
973 SD_BUS_NO_RESULT,
974 bus_unit_method_kill,
975 SD_BUS_VTABLE_UNPRIVILEGED),
dad97f04
ZJS
976 SD_BUS_METHOD("ResetFailed",
977 NULL,
978 NULL,
979 bus_unit_method_reset_failed,
980 SD_BUS_VTABLE_UNPRIVILEGED),
a008b6d7 981 SD_BUS_METHOD_WITH_ARGS("SetProperties",
982 SD_BUS_ARGS("b", runtime, "a(sv)", properties),
983 SD_BUS_NO_RESULT,
984 bus_unit_method_set_properties,
985 SD_BUS_VTABLE_UNPRIVILEGED),
dad97f04
ZJS
986 SD_BUS_METHOD("Ref",
987 NULL,
988 NULL,
989 bus_unit_method_ref,
990 SD_BUS_VTABLE_UNPRIVILEGED),
991 SD_BUS_METHOD("Unref",
992 NULL,
993 NULL,
994 bus_unit_method_unref,
995 SD_BUS_VTABLE_UNPRIVILEGED),
a008b6d7 996 SD_BUS_METHOD_WITH_ARGS("Clean",
997 SD_BUS_ARGS("as", mask),
998 SD_BUS_NO_RESULT,
999 bus_unit_method_clean,
1000 SD_BUS_VTABLE_UNPRIVILEGED),
d9e45bc3
MS
1001 SD_BUS_METHOD("Freeze",
1002 NULL,
1003 NULL,
1004 bus_unit_method_freeze,
1005 SD_BUS_VTABLE_UNPRIVILEGED),
1006 SD_BUS_METHOD("Thaw",
1007 NULL,
1008 NULL,
1009 bus_unit_method_thaw,
1010 SD_BUS_VTABLE_UNPRIVILEGED),
718db961 1011
92c23c5a
YW
1012 /* For dependency types we don't support anymore always return an empty array */
1013 SD_BUS_PROPERTY("RequiresOverridable", "as", property_get_empty_strv, 0, SD_BUS_VTABLE_HIDDEN),
1014 SD_BUS_PROPERTY("RequisiteOverridable", "as", property_get_empty_strv, 0, SD_BUS_VTABLE_HIDDEN),
1015 SD_BUS_PROPERTY("RequiredByOverridable", "as", property_get_empty_strv, 0, SD_BUS_VTABLE_HIDDEN),
1016 SD_BUS_PROPERTY("RequisiteOfOverridable", "as", property_get_empty_strv, 0, SD_BUS_VTABLE_HIDDEN),
1017 /* Obsolete alias names */
7bf081a1
ZJS
1018 SD_BUS_PROPERTY("StartLimitInterval", "t", bus_property_get_usec, offsetof(Unit, start_ratelimit.interval), SD_BUS_VTABLE_PROPERTY_CONST|SD_BUS_VTABLE_HIDDEN),
1019 SD_BUS_PROPERTY("StartLimitIntervalSec", "t", bus_property_get_usec, offsetof(Unit, start_ratelimit.interval), SD_BUS_VTABLE_PROPERTY_CONST|SD_BUS_VTABLE_HIDDEN),
dad97f04 1020
718db961
LP
1021 SD_BUS_VTABLE_END
1022};
2cccbca4 1023
718db961
LP
1024static int property_get_slice(
1025 sd_bus *bus,
1026 const char *path,
1027 const char *interface,
1028 const char *property,
1029 sd_bus_message *reply,
ebcf1f97
LP
1030 void *userdata,
1031 sd_bus_error *error) {
2cccbca4 1032
99534007 1033 Unit *u = ASSERT_PTR(userdata);
2cccbca4 1034
718db961
LP
1035 assert(bus);
1036 assert(reply);
2cccbca4 1037
718db961
LP
1038 return sd_bus_message_append(reply, "s", unit_slice_name(u));
1039}
2cccbca4 1040
934277fe
LP
1041static int property_get_current_memory(
1042 sd_bus *bus,
1043 const char *path,
1044 const char *interface,
1045 const char *property,
1046 sd_bus_message *reply,
1047 void *userdata,
1048 sd_bus_error *error) {
1049
f5fbe71d 1050 uint64_t sz = UINT64_MAX;
99534007 1051 Unit *u = ASSERT_PTR(userdata);
934277fe
LP
1052 int r;
1053
1054 assert(bus);
1055 assert(reply);
934277fe 1056
5ad096b3
LP
1057 r = unit_get_memory_current(u, &sz);
1058 if (r < 0 && r != -ENODATA)
35c08a56 1059 log_unit_warning_errno(u, r, "Failed to get current memory usage from cgroup: %m");
934277fe 1060
5ad096b3
LP
1061 return sd_bus_message_append(reply, "t", sz);
1062}
934277fe 1063
9824ab1f 1064static int property_get_available_memory(
aac3384e
FS
1065 sd_bus *bus,
1066 const char *path,
1067 const char *interface,
1068 const char *property,
1069 sd_bus_message *reply,
1070 void *userdata,
1071 sd_bus_error *error) {
1072
1073 uint64_t sz = UINT64_MAX;
1074 Unit *u = ASSERT_PTR(userdata);
1075 int r;
1076
1077 assert(bus);
1078 assert(reply);
1079
9824ab1f 1080 r = unit_get_memory_available(u, &sz);
aac3384e 1081 if (r < 0 && r != -ENODATA)
9824ab1f 1082 log_unit_warning_errno(u, r, "Failed to get total available memory from cgroup: %m");
aac3384e
FS
1083
1084 return sd_bus_message_append(reply, "t", sz);
1085}
1086
9824ab1f 1087static int property_get_memory_accounting(
26caa668
FS
1088 sd_bus *bus,
1089 const char *path,
1090 const char *interface,
1091 const char *property,
1092 sd_bus_message *reply,
1093 void *userdata,
1094 sd_bus_error *error) {
1095
26caa668 1096 Unit *u = ASSERT_PTR(userdata);
9824ab1f 1097 CGroupMemoryAccountingMetric metric;
93ff34e4 1098 uint64_t sz = UINT64_MAX;
93ff34e4
LB
1099
1100 assert(bus);
1101 assert(reply);
93ff34e4 1102
9824ab1f
MY
1103 assert_se((metric = cgroup_memory_accounting_metric_from_string(property)) >= 0);
1104 (void) unit_get_memory_accounting(u, metric, &sz);
93ff34e4
LB
1105 return sd_bus_message_append(reply, "t", sz);
1106}
1107
03a7b521
LP
1108static int property_get_current_tasks(
1109 sd_bus *bus,
1110 const char *path,
1111 const char *interface,
1112 const char *property,
1113 sd_bus_message *reply,
1114 void *userdata,
1115 sd_bus_error *error) {
1116
f5fbe71d 1117 uint64_t cn = UINT64_MAX;
99534007 1118 Unit *u = ASSERT_PTR(userdata);
03a7b521
LP
1119 int r;
1120
1121 assert(bus);
1122 assert(reply);
03a7b521
LP
1123
1124 r = unit_get_tasks_current(u, &cn);
1125 if (r < 0 && r != -ENODATA)
1126 log_unit_warning_errno(u, r, "Failed to get pids.current attribute: %m");
1127
1128 return sd_bus_message_append(reply, "t", cn);
1129}
1130
5ad096b3
LP
1131static int property_get_cpu_usage(
1132 sd_bus *bus,
1133 const char *path,
1134 const char *interface,
1135 const char *property,
1136 sd_bus_message *reply,
1137 void *userdata,
1138 sd_bus_error *error) {
934277fe 1139
f5fbe71d 1140 nsec_t ns = NSEC_INFINITY;
99534007 1141 Unit *u = ASSERT_PTR(userdata);
5ad096b3
LP
1142 int r;
1143
1144 assert(bus);
1145 assert(reply);
5ad096b3
LP
1146
1147 r = unit_get_cpu_usage(u, &ns);
1148 if (r < 0 && r != -ENODATA)
f2341e0a 1149 log_unit_warning_errno(u, r, "Failed to get cpuacct.usage attribute: %m");
5ad096b3
LP
1150
1151 return sd_bus_message_append(reply, "t", ns);
934277fe
LP
1152}
1153
047f5d63
PH
1154static int property_get_cpuset_cpus(
1155 sd_bus *bus,
1156 const char *path,
1157 const char *interface,
1158 const char *property,
1159 sd_bus_message *reply,
1160 void *userdata,
1161 sd_bus_error *error) {
1162
99534007 1163 Unit *u = ASSERT_PTR(userdata);
047f5d63
PH
1164 _cleanup_(cpu_set_reset) CPUSet cpus = {};
1165 _cleanup_free_ uint8_t *array = NULL;
1166 size_t allocated;
1167
1168 assert(bus);
1169 assert(reply);
047f5d63
PH
1170
1171 (void) unit_get_cpuset(u, &cpus, "cpuset.cpus.effective");
1172 (void) cpu_set_to_dbus(&cpus, &array, &allocated);
1173 return sd_bus_message_append_array(reply, 'y', array, allocated);
1174}
1175
1176static int property_get_cpuset_mems(
1177 sd_bus *bus,
1178 const char *path,
1179 const char *interface,
1180 const char *property,
1181 sd_bus_message *reply,
1182 void *userdata,
1183 sd_bus_error *error) {
1184
99534007 1185 Unit *u = ASSERT_PTR(userdata);
047f5d63
PH
1186 _cleanup_(cpu_set_reset) CPUSet mems = {};
1187 _cleanup_free_ uint8_t *array = NULL;
1188 size_t allocated;
1189
1190 assert(bus);
1191 assert(reply);
047f5d63
PH
1192
1193 (void) unit_get_cpuset(u, &mems, "cpuset.mems.effective");
1194 (void) cpu_set_to_dbus(&mems, &array, &allocated);
1195 return sd_bus_message_append_array(reply, 'y', array, allocated);
1196}
1197
98bac605
LP
1198static int property_get_cgroup(
1199 sd_bus *bus,
1200 const char *path,
1201 const char *interface,
1202 const char *property,
1203 sd_bus_message *reply,
1204 void *userdata,
1205 sd_bus_error *error) {
1206
99534007 1207 Unit *u = ASSERT_PTR(userdata);
79a60375 1208 const char *t = NULL;
98bac605
LP
1209
1210 assert(bus);
1211 assert(reply);
98bac605
LP
1212
1213 /* Three cases: a) u->cgroup_path is NULL, in which case the
1214 * unit has no control group, which we report as the empty
1215 * string. b) u->cgroup_path is the empty string, which
1216 * indicates the root cgroup, which we report as "/". c) all
1217 * other cases we report as-is. */
1218
9cc54544
LP
1219 CGroupRuntime *crt = unit_get_cgroup_runtime(u);
1220
1221 if (crt && crt->cgroup_path)
1222 t = empty_to_root(crt->cgroup_path);
98bac605
LP
1223
1224 return sd_bus_message_append(reply, "s", t);
1225}
1226
9cc54544
LP
1227static int property_get_cgroup_id(
1228 sd_bus *bus,
1229 const char *path,
1230 const char *interface,
1231 const char *property,
1232 sd_bus_message *reply,
1233 void *userdata,
1234 sd_bus_error *error) {
1235
1236 Unit *u = ASSERT_PTR(userdata);
1237
1238 assert(bus);
1239 assert(reply);
1240
1241 CGroupRuntime *crt = unit_get_cgroup_runtime(u);
04a3af3c 1242 return sd_bus_message_append(reply, "t", crt ? crt->cgroup_id : UINT64_C(0));
9cc54544
LP
1243}
1244
d57d521c 1245static int append_process(sd_bus_message *reply, const char *p, PidRef *pid, Set *pids) {
291d565a
LP
1246 _cleanup_free_ char *buf = NULL, *cmdline = NULL;
1247 int r;
1248
1249 assert(reply);
d57d521c 1250 assert(pidref_is_set(pid));
291d565a 1251
d57d521c 1252 r = set_put(pids, PID_TO_PTR(pid->pid));
4c701096 1253 if (IN_SET(r, 0, -EEXIST))
291d565a
LP
1254 return 0;
1255 if (r < 0)
1256 return r;
1257
1258 if (!p) {
d57d521c 1259 r = cg_pidref_get_path(SYSTEMD_CGROUP_CONTROLLER, pid, &buf);
291d565a
LP
1260 if (r == -ESRCH)
1261 return 0;
1262 if (r < 0)
1263 return r;
1264
1265 p = buf;
1266 }
1267
d57d521c 1268 (void) pidref_get_cmdline(
a034620f
LP
1269 pid,
1270 SIZE_MAX,
1271 PROCESS_CMDLINE_COMM_FALLBACK | PROCESS_CMDLINE_QUOTE,
1272 &cmdline);
291d565a
LP
1273
1274 return sd_bus_message_append(reply,
1275 "(sus)",
1276 p,
d57d521c 1277 (uint32_t) pid->pid,
291d565a
LP
1278 cmdline);
1279}
1280
1281static int append_cgroup(sd_bus_message *reply, const char *p, Set *pids) {
1282 _cleanup_closedir_ DIR *d = NULL;
1283 _cleanup_fclose_ FILE *f = NULL;
1284 int r;
1285
1286 assert(reply);
1287 assert(p);
1288
1289 r = cg_enumerate_processes(SYSTEMD_CGROUP_CONTROLLER, p, &f);
24715314 1290 if (r == -ENOENT)
291d565a
LP
1291 return 0;
1292 if (r < 0)
1293 return r;
1294
1295 for (;;) {
d57d521c 1296 _cleanup_(pidref_done) PidRef pidref = PIDREF_NULL;
291d565a 1297
1fb50408 1298 /* libvirt / qemu uses threaded mode and cgroup.procs cannot be read at the lower levels.
d57d521c
LP
1299 * From https://docs.kernel.org/admin-guide/cgroup-v2.html#threads, “cgroup.procs” in a
1300 * threaded domain cgroup contains the PIDs of all processes in the subtree and is not
1301 * readable in the subtree proper. */
1302
00f17143 1303 r = cg_read_pidref(f, &pidref, /* flags = */ 0);
1fb50408
YW
1304 if (IN_SET(r, 0, -EOPNOTSUPP))
1305 break;
291d565a
LP
1306 if (r < 0)
1307 return r;
291d565a 1308
d57d521c
LP
1309 r = pidref_is_kernel_thread(&pidref);
1310 if (r == -ESRCH) /* gone by now */
1311 continue;
1312 if (r < 0)
1313 log_debug_errno(r, "Failed to determine if " PID_FMT " is a kernel thread, assuming not: %m", pidref.pid);
1314 if (r > 0)
291d565a
LP
1315 continue;
1316
d57d521c 1317 r = append_process(reply, p, &pidref, pids);
291d565a
LP
1318 if (r < 0)
1319 return r;
1320 }
1321
1322 r = cg_enumerate_subgroups(SYSTEMD_CGROUP_CONTROLLER, p, &d);
1323 if (r == -ENOENT)
1324 return 0;
1325 if (r < 0)
1326 return r;
1327
1328 for (;;) {
1329 _cleanup_free_ char *g = NULL, *j = NULL;
1330
1331 r = cg_read_subgroup(d, &g);
1332 if (r < 0)
1333 return r;
1334 if (r == 0)
1335 break;
1336
624e4fcf 1337 j = path_join(empty_to_root(p), g);
291d565a
LP
1338 if (!j)
1339 return -ENOMEM;
1340
1341 r = append_cgroup(reply, j, pids);
1342 if (r < 0)
1343 return r;
1344 }
1345
1346 return 0;
1347}
1348
1349int bus_unit_method_get_processes(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1350 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
af4fa99d 1351 _cleanup_set_free_ Set *pids = NULL;
291d565a 1352 Unit *u = userdata;
291d565a
LP
1353 int r;
1354
1355 assert(message);
1356
807fa5d9
LP
1357 r = mac_selinux_unit_access_check(u, message, "status", error);
1358 if (r < 0)
1359 return r;
1360
291d565a
LP
1361 pids = set_new(NULL);
1362 if (!pids)
1363 return -ENOMEM;
1364
1365 r = sd_bus_message_new_method_return(message, &reply);
1366 if (r < 0)
1367 return r;
1368
1369 r = sd_bus_message_open_container(reply, 'a', "(sus)");
1370 if (r < 0)
1371 return r;
1372
9cc54544
LP
1373 CGroupRuntime *crt;
1374 crt = unit_get_cgroup_runtime(u);
1375 if (crt && crt->cgroup_path) {
1376 r = append_cgroup(reply, crt->cgroup_path, pids);
291d565a
LP
1377 if (r < 0)
1378 return r;
1379 }
1380
1381 /* The main and control pids might live outside of the cgroup, hence fetch them separately */
37eb258e
LP
1382 PidRef *pid = unit_main_pid(u);
1383 if (pidref_is_set(pid)) {
d57d521c 1384 r = append_process(reply, NULL, pid, pids);
291d565a
LP
1385 if (r < 0)
1386 return r;
1387 }
1388
1389 pid = unit_control_pid(u);
37eb258e 1390 if (pidref_is_set(pid)) {
d57d521c 1391 r = append_process(reply, NULL, pid, pids);
291d565a
LP
1392 if (r < 0)
1393 return r;
1394 }
1395
1396 r = sd_bus_message_close_container(reply);
1397 if (r < 0)
1398 return r;
1399
1400 return sd_bus_send(NULL, reply, NULL);
1401}
1402
906c06f6
DM
1403static int property_get_ip_counter(
1404 sd_bus *bus,
1405 const char *path,
1406 const char *interface,
1407 const char *property,
1408 sd_bus_message *reply,
1409 void *userdata,
1410 sd_bus_error *error) {
1411
83f18c91 1412 uint64_t value = UINT64_MAX;
99534007 1413 Unit *u = ASSERT_PTR(userdata);
435996e6 1414 CGroupIPAccountingMetric metric;
906c06f6
DM
1415
1416 assert(bus);
1417 assert(reply);
1418 assert(property);
906c06f6 1419
435996e6 1420 assert_se((metric = cgroup_ip_accounting_metric_from_string(property)) >= 0);
906c06f6
DM
1421 (void) unit_get_ip_accounting(u, metric, &value);
1422 return sd_bus_message_append(reply, "t", value);
1423}
1424
fbe14fc9
LP
1425static int property_get_io_counter(
1426 sd_bus *bus,
1427 const char *path,
1428 const char *interface,
1429 const char *property,
1430 sd_bus_message *reply,
1431 void *userdata,
1432 sd_bus_error *error) {
1433
fbe14fc9 1434 uint64_t value = UINT64_MAX;
99534007 1435 Unit *u = ASSERT_PTR(userdata);
fbe14fc9
LP
1436 ssize_t metric;
1437
1438 assert(bus);
1439 assert(reply);
1440 assert(property);
fbe14fc9 1441
435996e6
DDM
1442 assert_se((metric = cgroup_io_accounting_metric_from_string(property)) >= 0);
1443 (void) unit_get_io_accounting(u, metric, /* allow_cache= */ false, &value);
fbe14fc9
LP
1444 return sd_bus_message_append(reply, "t", value);
1445}
1446
4fb0d2dc
MK
1447static int property_get_effective_limit(
1448 sd_bus *bus,
1449 const char *path,
1450 const char *interface,
1451 const char *property,
1452 sd_bus_message *reply,
1453 void *userdata,
1454 sd_bus_error *error) {
1455
1456 uint64_t value = CGROUP_LIMIT_MAX;
1457 Unit *u = ASSERT_PTR(userdata);
1458 ssize_t type;
1459
1460 assert(bus);
1461 assert(reply);
1462 assert(property);
1463
8ad61489 1464 assert_se((type = cgroup_effective_limit_type_from_string(property)) >= 0);
4fb0d2dc
MK
1465 (void) unit_get_effective_limit(u, type, &value);
1466 return sd_bus_message_append(reply, "t", value);
1467}
1468
6592b975 1469int bus_unit_method_attach_processes(sd_bus_message *message, void *userdata, sd_bus_error *error) {
6592b975 1470 _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL;
af4fa99d 1471 _cleanup_set_free_ Set *pids = NULL;
6592b975
LP
1472 Unit *u = userdata;
1473 const char *path;
1474 int r;
1475
1476 assert(message);
1477
1478 /* This migrates the processes with the specified PIDs into the cgroup of this unit, optionally below a
1479 * specified cgroup path. Obviously this only works for units that actually maintain a cgroup
1480 * representation. If a process is already in the cgroup no operation is executed – in this case the specified
1481 * subcgroup path has no effect! */
1482
1483 r = mac_selinux_unit_access_check(u, message, "start", error);
1484 if (r < 0)
1485 return r;
1486
1487 r = sd_bus_message_read(message, "s", &path);
1488 if (r < 0)
1489 return r;
1490
1491 path = empty_to_null(path);
1492 if (path) {
1493 if (!path_is_absolute(path))
1494 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Control group path is not absolute: %s", path);
1495
1496 if (!path_is_normalized(path))
1497 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Control group path is not normalized: %s", path);
1498 }
1499
1500 if (!unit_cgroup_delegate(u))
1b09b81c 1501 return sd_bus_error_set(error, SD_BUS_ERROR_INVALID_ARGS, "Process migration not available on non-delegated units.");
6592b975
LP
1502
1503 if (UNIT_IS_INACTIVE_OR_FAILED(unit_active_state(u)))
1b09b81c 1504 return sd_bus_error_set(error, SD_BUS_ERROR_INVALID_ARGS, "Unit is not active, refusing.");
6592b975 1505
4ac08d8a 1506 r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_EUID|SD_BUS_CREDS_PID|SD_BUS_CREDS_PIDFD, &creds);
6592b975
LP
1507 if (r < 0)
1508 return r;
1509
1510 r = sd_bus_message_enter_container(message, 'a', "u");
1511 if (r < 0)
1512 return r;
1513 for (;;) {
495e75ed 1514 _cleanup_(pidref_freep) PidRef *pidref = NULL;
6592b975
LP
1515 uid_t process_uid, sender_uid;
1516 uint32_t upid;
6592b975
LP
1517
1518 r = sd_bus_message_read(message, "u", &upid);
1519 if (r < 0)
1520 return r;
1521 if (r == 0)
1522 break;
1523
1524 if (upid == 0) {
4ac08d8a
LP
1525 _cleanup_(pidref_done) PidRef p = PIDREF_NULL;
1526 r = bus_creds_get_pidref(creds, &p);
6592b975
LP
1527 if (r < 0)
1528 return r;
6592b975 1529
4ac08d8a
LP
1530 r = pidref_dup(&p, &pidref);
1531 } else
1532 r = pidref_new_from_pid(upid, &pidref);
495e75ed
LP
1533 if (r < 0)
1534 return r;
1535
6592b975 1536 /* Filter out duplicates */
495e75ed 1537 if (set_contains(pids, pidref))
6592b975
LP
1538 continue;
1539
1540 /* Check if this process is suitable for attaching to this unit */
495e75ed 1541 r = unit_pid_attachable(u, pidref, error);
6592b975
LP
1542 if (r < 0)
1543 return r;
1544
1545 /* Let's query the sender's UID, so that we can make our security decisions */
1546 r = sd_bus_creds_get_euid(creds, &sender_uid);
1547 if (r < 0)
1548 return r;
1549
874b5ad2 1550 /* Let's validate security: if the sender is root, then all is OK. If the sender is any other unit,
6592b975
LP
1551 * then the process' UID and the target unit's UID have to match the sender's UID */
1552 if (sender_uid != 0 && sender_uid != getuid()) {
8b513415 1553 r = pidref_get_uid(pidref, &process_uid);
6592b975
LP
1554 if (r < 0)
1555 return sd_bus_error_set_errnof(error, r, "Failed to retrieve process UID: %m");
1556
1557 if (process_uid != sender_uid)
4ac08d8a 1558 return sd_bus_error_setf(error, SD_BUS_ERROR_ACCESS_DENIED, "Process " PID_FMT " not owned by client's UID. Refusing.", pidref->pid);
6592b975 1559 if (process_uid != u->ref_uid)
4ac08d8a 1560 return sd_bus_error_setf(error, SD_BUS_ERROR_ACCESS_DENIED, "Process " PID_FMT " not owned by target unit's UID. Refusing.", pidref->pid);
6592b975
LP
1561 }
1562
4c8d5f02 1563 r = set_ensure_consume(&pids, &pidref_hash_ops_free, TAKE_PTR(pidref));
6592b975
LP
1564 if (r < 0)
1565 return r;
1566 }
1567
1568 r = sd_bus_message_exit_container(message);
1569 if (r < 0)
1570 return r;
1571
1572 r = unit_attach_pids_to_cgroup(u, pids, path);
1573 if (r < 0)
1574 return sd_bus_error_set_errnof(error, r, "Failed to attach processes to control group: %m");
1575
1576 return sd_bus_reply_method_return(message, NULL);
1577}
1578
718db961
LP
1579const sd_bus_vtable bus_unit_cgroup_vtable[] = {
1580 SD_BUS_VTABLE_START(0),
1581 SD_BUS_PROPERTY("Slice", "s", property_get_slice, 0, 0),
98bac605 1582 SD_BUS_PROPERTY("ControlGroup", "s", property_get_cgroup, 0, 0),
9cc54544 1583 SD_BUS_PROPERTY("ControlGroupId", "t", property_get_cgroup_id, 0, 0),
934277fe 1584 SD_BUS_PROPERTY("MemoryCurrent", "t", property_get_current_memory, 0, 0),
9824ab1f
MY
1585 SD_BUS_PROPERTY("MemoryPeak", "t", property_get_memory_accounting, 0, 0),
1586 SD_BUS_PROPERTY("MemorySwapCurrent", "t", property_get_memory_accounting, 0, 0),
1587 SD_BUS_PROPERTY("MemorySwapPeak", "t", property_get_memory_accounting, 0, 0),
1588 SD_BUS_PROPERTY("MemoryZSwapCurrent", "t", property_get_memory_accounting, 0, 0),
93ff34e4 1589 SD_BUS_PROPERTY("MemoryAvailable", "t", property_get_available_memory, 0, 0),
4fb0d2dc
MK
1590 SD_BUS_PROPERTY("EffectiveMemoryMax", "t", property_get_effective_limit, 0, 0),
1591 SD_BUS_PROPERTY("EffectiveMemoryHigh", "t", property_get_effective_limit, 0, 0),
5ad096b3 1592 SD_BUS_PROPERTY("CPUUsageNSec", "t", property_get_cpu_usage, 0, 0),
047f5d63
PH
1593 SD_BUS_PROPERTY("EffectiveCPUs", "ay", property_get_cpuset_cpus, 0, 0),
1594 SD_BUS_PROPERTY("EffectiveMemoryNodes", "ay", property_get_cpuset_mems, 0, 0),
03a7b521 1595 SD_BUS_PROPERTY("TasksCurrent", "t", property_get_current_tasks, 0, 0),
4fb0d2dc 1596 SD_BUS_PROPERTY("EffectiveTasksMax", "t", property_get_effective_limit, 0, 0),
906c06f6
DM
1597 SD_BUS_PROPERTY("IPIngressBytes", "t", property_get_ip_counter, 0, 0),
1598 SD_BUS_PROPERTY("IPIngressPackets", "t", property_get_ip_counter, 0, 0),
1599 SD_BUS_PROPERTY("IPEgressBytes", "t", property_get_ip_counter, 0, 0),
1600 SD_BUS_PROPERTY("IPEgressPackets", "t", property_get_ip_counter, 0, 0),
fbe14fc9
LP
1601 SD_BUS_PROPERTY("IOReadBytes", "t", property_get_io_counter, 0, 0),
1602 SD_BUS_PROPERTY("IOReadOperations", "t", property_get_io_counter, 0, 0),
1603 SD_BUS_PROPERTY("IOWriteBytes", "t", property_get_io_counter, 0, 0),
1604 SD_BUS_PROPERTY("IOWriteOperations", "t", property_get_io_counter, 0, 0),
dad97f04 1605
a008b6d7 1606 SD_BUS_METHOD_WITH_ARGS("GetProcesses",
696a8ec0
MY
1607 SD_BUS_NO_ARGS,
1608 SD_BUS_ARGS("a(sus)", processes),
1609 bus_unit_method_get_processes,
1610 SD_BUS_VTABLE_UNPRIVILEGED),
dad97f04 1611
a008b6d7 1612 SD_BUS_METHOD_WITH_ARGS("AttachProcesses",
696a8ec0
MY
1613 SD_BUS_ARGS("s", subcgroup, "au", pids),
1614 SD_BUS_NO_RESULT,
1615 bus_unit_method_attach_processes,
1616 SD_BUS_VTABLE_UNPRIVILEGED),
dad97f04 1617
718db961
LP
1618 SD_BUS_VTABLE_END
1619};
2cccbca4 1620
8f8f05a9 1621static int send_new_signal(sd_bus *bus, void *userdata) {
4afd3348 1622 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
718db961 1623 _cleanup_free_ char *p = NULL;
99534007 1624 Unit *u = ASSERT_PTR(userdata);
718db961 1625 int r;
2cccbca4 1626
718db961 1627 assert(bus);
2cccbca4 1628
718db961 1629 p = unit_dbus_path(u);
e861098b 1630 if (!p)
718db961 1631 return -ENOMEM;
2cccbca4 1632
718db961
LP
1633 r = sd_bus_message_new_signal(
1634 bus,
151b9b96 1635 &m,
718db961
LP
1636 "/org/freedesktop/systemd1",
1637 "org.freedesktop.systemd1.Manager",
151b9b96 1638 "UnitNew");
718db961
LP
1639 if (r < 0)
1640 return r;
2cccbca4 1641
718db961
LP
1642 r = sd_bus_message_append(m, "so", u->id, p);
1643 if (r < 0)
1644 return r;
2cccbca4 1645
8f8f05a9 1646 return sd_bus_send(bus, m, NULL);
718db961 1647}
2cccbca4 1648
8f8f05a9 1649static int send_changed_signal(sd_bus *bus, void *userdata) {
718db961 1650 _cleanup_free_ char *p = NULL;
99534007 1651 Unit *u = ASSERT_PTR(userdata);
718db961 1652 int r;
2cccbca4 1653
718db961 1654 assert(bus);
2cccbca4 1655
718db961 1656 p = unit_dbus_path(u);
9ceefc81 1657 if (!p)
718db961 1658 return -ENOMEM;
2cccbca4 1659
718db961
LP
1660 /* Send a properties changed signal. First for the specific
1661 * type, then for the generic unit. The clients may rely on
1662 * this order to get atomic behavior if needed. */
ea430986 1663
aec8de63
LP
1664 r = sd_bus_emit_properties_changed_strv(
1665 bus, p,
21b735e7 1666 unit_dbus_interface_from_type(u->type),
aec8de63 1667 NULL);
fe7f06f1 1668 if (r < 0)
aec8de63 1669 return r;
80fbf05e 1670
fe7f06f1 1671 return sd_bus_emit_properties_changed_strv(
718db961
LP
1672 bus, p,
1673 "org.freedesktop.systemd1.Unit",
718db961 1674 NULL);
ea430986
LP
1675}
1676
c1e1601e 1677void bus_unit_send_change_signal(Unit *u) {
b170dd80 1678 int r;
c1e1601e 1679 assert(u);
c1e1601e 1680
ac155bb8 1681 if (u->in_dbus_queue) {
71fda00f 1682 LIST_REMOVE(dbus_queue, u->manager->dbus_unit_queue, u);
ac155bb8 1683 u->in_dbus_queue = false;
af05bb97
LP
1684
1685 /* The unit might be good to be GC once its pending signals have been sent */
1686 unit_add_to_gc_queue(u);
c0bd0cf7 1687 }
c1e1601e 1688
ac155bb8 1689 if (!u->id)
04ade7d2
LP
1690 return;
1691
ae572acd 1692 r = bus_foreach_bus(u->manager, u->bus_track, u->sent_dbus_new_signal ? send_changed_signal : send_new_signal, u);
718db961 1693 if (r < 0)
f2341e0a 1694 log_unit_debug_errno(u, r, "Failed to send unit change signal for %s: %m", u->id);
c1e1601e 1695
718db961
LP
1696 u->sent_dbus_new_signal = true;
1697}
c4e2ceae 1698
6fcbec6f
LP
1699void bus_unit_send_pending_change_signal(Unit *u, bool including_new) {
1700
1701 /* Sends out any pending change signals, but only if they really are pending. This call is used when we are
1702 * about to change state in order to force out a PropertiesChanged signal beforehand if there was one pending
1703 * so that clients can follow the full state transition */
1704
1705 if (!u->in_dbus_queue) /* If not enqueued, don't bother */
1706 return;
1707
1708 if (!u->sent_dbus_new_signal && !including_new) /* If the unit was never announced, don't bother, it's fine if
1709 * the unit appears in the new state right-away (except if the
1710 * caller explicitly asked us to send it anyway) */
1711 return;
1712
1713 if (MANAGER_IS_RELOADING(u->manager)) /* Don't generate unnecessary PropertiesChanged signals for the same unit
1714 * when we are reloading. */
1715 return;
1716
1717 bus_unit_send_change_signal(u);
1718}
1719
3d19e122 1720int bus_unit_send_pending_freezer_message(Unit *u, bool cancelled) {
1721 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
d9e45bc3
MS
1722 int r;
1723
1724 assert(u);
1725
af1e3365 1726 if (!u->pending_freezer_invocation)
d9e45bc3
MS
1727 return 0;
1728
3d19e122 1729 if (cancelled)
1730 r = sd_bus_message_new_method_error(
af1e3365 1731 u->pending_freezer_invocation,
3d19e122 1732 &reply,
1733 &SD_BUS_ERROR_MAKE_CONST(
1734 BUS_ERROR_FREEZE_CANCELLED, "Freeze operation aborted"));
1735 else
af1e3365 1736 r = sd_bus_message_new_method_return(u->pending_freezer_invocation, &reply);
3d19e122 1737 if (r < 0)
1738 return r;
1739
1740 r = sd_bus_send(NULL, reply, NULL);
d9e45bc3
MS
1741 if (r < 0)
1742 log_warning_errno(r, "Failed to send queued message, ignoring: %m");
1743
af1e3365 1744 u->pending_freezer_invocation = sd_bus_message_unref(u->pending_freezer_invocation);
d9e45bc3
MS
1745
1746 return 0;
1747}
1748
8f8f05a9 1749static int send_removed_signal(sd_bus *bus, void *userdata) {
4afd3348 1750 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
718db961 1751 _cleanup_free_ char *p = NULL;
99534007 1752 Unit *u = ASSERT_PTR(userdata);
718db961 1753 int r;
c4e2ceae 1754
718db961 1755 assert(bus);
c1e1601e 1756
718db961 1757 p = unit_dbus_path(u);
e861098b 1758 if (!p)
718db961 1759 return -ENOMEM;
c1e1601e 1760
718db961
LP
1761 r = sd_bus_message_new_signal(
1762 bus,
151b9b96 1763 &m,
718db961
LP
1764 "/org/freedesktop/systemd1",
1765 "org.freedesktop.systemd1.Manager",
151b9b96 1766 "UnitRemoved");
718db961
LP
1767 if (r < 0)
1768 return r;
c1e1601e 1769
718db961
LP
1770 r = sd_bus_message_append(m, "so", u->id, p);
1771 if (r < 0)
1772 return r;
c1e1601e 1773
8f8f05a9 1774 return sd_bus_send(bus, m, NULL);
c1e1601e
LP
1775}
1776
1777void bus_unit_send_removed_signal(Unit *u) {
718db961 1778 int r;
c1e1601e
LP
1779 assert(u);
1780
0dd99f86 1781 if (!u->sent_dbus_new_signal || u->in_dbus_queue)
7535cc78
LP
1782 bus_unit_send_change_signal(u);
1783
ac155bb8 1784 if (!u->id)
04ade7d2
LP
1785 return;
1786
ae572acd 1787 r = bus_foreach_bus(u->manager, u->bus_track, send_removed_signal, u);
718db961 1788 if (r < 0)
f2341e0a 1789 log_unit_debug_errno(u, r, "Failed to send unit remove signal for %s: %m", u->id);
c1e1601e 1790}
e2110e5d 1791
70666e28 1792int bus_unit_queue_job_one(
718db961 1793 sd_bus_message *message,
cad45ba1
LP
1794 Unit *u,
1795 JobType type,
1796 JobMode mode,
50cbaba4 1797 BusUnitQueueFlags flags,
70666e28 1798 sd_bus_message *reply,
ebcf1f97 1799 sd_bus_error *error) {
cad45ba1 1800
e57ac1b0 1801 _cleanup_set_free_ Set *affected = NULL;
70666e28 1802 _cleanup_free_ char *job_path = NULL, *unit_path = NULL;
50cbaba4 1803 Job *j, *a;
cad45ba1
LP
1804 int r;
1805
8ea8e23f
ZJS
1806 if (FLAGS_SET(flags, BUS_UNIT_QUEUE_RELOAD_IF_POSSIBLE) && unit_can_reload(u)) {
1807 if (type == JOB_RESTART)
1808 type = JOB_RELOAD_OR_START;
1809 else if (type == JOB_TRY_RESTART)
1810 type = JOB_TRY_RELOAD;
1811 }
1812
1813 if (type == JOB_STOP &&
1814 IN_SET(u->load_state, UNIT_NOT_FOUND, UNIT_ERROR, UNIT_BAD_SETTING) &&
1815 unit_active_state(u) == UNIT_INACTIVE)
1816 return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_UNIT, "Unit %s not loaded.", u->id);
1817
1818 if ((type == JOB_START && u->refuse_manual_start) ||
1819 (type == JOB_STOP && u->refuse_manual_stop) ||
1820 (IN_SET(type, JOB_RESTART, JOB_TRY_RESTART) && (u->refuse_manual_start || u->refuse_manual_stop)) ||
1821 (type == JOB_RELOAD_OR_START && job_type_collapse(type, u) == JOB_START && u->refuse_manual_start))
1822 return sd_bus_error_setf(error,
1823 BUS_ERROR_ONLY_BY_DEPENDENCY,
1824 "Operation refused, unit %s may be requested by dependency only (it is configured to refuse manual start/stop).",
1825 u->id);
1826
1827 /* dbus-broker issues StartUnit for activation requests, and Type=dbus services automatically
1828 * gain dependency on dbus.socket. Therefore, if dbus has a pending stop job, the new start
1829 * job that pulls in dbus again would cause job type conflict. Let's avoid that by rejecting
1830 * job enqueuing early.
1831 *
1832 * Note that unlike signal_activation_request(), we can't use unit_inactive_or_pending()
1833 * here. StartUnit is a more generic interface, and thus users are allowed to use e.g. systemctl
1834 * to start Type=dbus services even when dbus is inactive. */
1835 if (type == JOB_START && u->type == UNIT_SERVICE && SERVICE(u)->type == SERVICE_DBUS)
1836 FOREACH_STRING(dbus_unit, SPECIAL_DBUS_SOCKET, SPECIAL_DBUS_SERVICE) {
1837 Unit *dbus;
1838
1839 dbus = manager_get_unit(u->manager, dbus_unit);
1840 if (dbus && unit_stop_pending(dbus))
1841 return sd_bus_error_setf(error,
1842 BUS_ERROR_SHUTTING_DOWN,
1843 "Operation for unit %s refused, D-Bus is shutting down.",
1844 u->id);
1845 }
1846
50cbaba4
LP
1847 if (FLAGS_SET(flags, BUS_UNIT_QUEUE_VERBOSE_REPLY)) {
1848 affected = set_new(NULL);
1849 if (!affected)
1850 return -ENOMEM;
1851 }
1852
1853 r = manager_add_job(u->manager, type, u, mode, affected, error, &j);
cad45ba1 1854 if (r < 0)
ebcf1f97 1855 return r;
cad45ba1 1856
c5a97ed1
LP
1857 r = bus_job_track_sender(j, message);
1858 if (r < 0)
1859 return r;
cad45ba1 1860
13142276
LP
1861 /* Before we send the method reply, force out the announcement JobNew for this job */
1862 bus_job_send_pending_change_signal(j, true);
1863
50cbaba4
LP
1864 job_path = job_dbus_path(j);
1865 if (!job_path)
1866 return -ENOMEM;
1867
1868 /* The classic response is just a job object path */
1869 if (!FLAGS_SET(flags, BUS_UNIT_QUEUE_VERBOSE_REPLY))
70666e28 1870 return sd_bus_message_append(reply, "o", job_path);
50cbaba4
LP
1871
1872 /* In verbose mode respond with the anchor job plus everything that has been affected */
50cbaba4
LP
1873
1874 unit_path = unit_dbus_path(j->unit);
1875 if (!unit_path)
1876 return -ENOMEM;
1877
1878 r = sd_bus_message_append(reply, "uosos",
1879 j->id, job_path,
1880 j->unit->id, unit_path,
1881 job_type_to_string(j->type));
1882 if (r < 0)
1883 return r;
1884
1885 r = sd_bus_message_open_container(reply, 'a', "(uosos)");
1886 if (r < 0)
1887 return r;
1888
90e74a66 1889 SET_FOREACH(a, affected) {
50cbaba4
LP
1890 if (a->id == j->id)
1891 continue;
1892
1893 /* Free paths from previous iteration */
1894 job_path = mfree(job_path);
1895 unit_path = mfree(unit_path);
1896
1897 job_path = job_dbus_path(a);
1898 if (!job_path)
1899 return -ENOMEM;
1900
1901 unit_path = unit_dbus_path(a->unit);
1902 if (!unit_path)
1903 return -ENOMEM;
1904
1905 r = sd_bus_message_append(reply, "(uosos)",
1906 a->id, job_path,
1907 a->unit->id, unit_path,
1908 job_type_to_string(a->type));
1909 if (r < 0)
1910 return r;
1911 }
1912
70666e28
ZJS
1913 return sd_bus_message_close_container(reply);
1914}
1915
1916int bus_unit_queue_job(
1917 sd_bus_message *message,
1918 Unit *u,
1919 JobType type,
1920 JobMode mode,
1921 BusUnitQueueFlags flags,
1922 sd_bus_error *error) {
1923
1924 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
1925 int r;
1926
1927 assert(message);
1928 assert(u);
1929 assert(type >= 0 && type < _JOB_TYPE_MAX);
1930 assert(mode >= 0 && mode < _JOB_MODE_MAX);
1931
1932 r = mac_selinux_unit_access_check(
1933 u, message,
1934 job_type_to_access_method(type),
1935 error);
1936 if (r < 0)
1937 return r;
1938
70666e28
ZJS
1939 r = sd_bus_message_new_method_return(message, &reply);
1940 if (r < 0)
1941 return r;
1942
1943 r = bus_unit_queue_job_one(message, u, type, mode, flags, reply, error);
50cbaba4
LP
1944 if (r < 0)
1945 return r;
1946
1947 return sd_bus_send(NULL, reply, NULL);
cad45ba1
LP
1948}
1949
721060d4 1950static int bus_unit_set_live_property(
9f2e86af
LP
1951 Unit *u,
1952 const char *name,
718db961 1953 sd_bus_message *message,
2e59b241 1954 UnitWriteFlags flags,
718db961 1955 sd_bus_error *error) {
9f2e86af
LP
1956
1957 int r;
1958
1959 assert(u);
1960 assert(name);
718db961 1961 assert(message);
9f2e86af 1962
cb31470f
ZJS
1963 /* Handles setting properties both "live" (i.e. at any time during runtime), and during creation (for
1964 * transient units that are being created). */
721060d4 1965
9f2e86af 1966 if (streq(name, "Description")) {
718db961 1967 const char *d;
9f2e86af 1968
718db961
LP
1969 r = sd_bus_message_read(message, "s", &d);
1970 if (r < 0)
1971 return r;
8aec412f 1972
2e59b241 1973 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
718db961 1974 r = unit_set_description(u, d);
8aec412f
LP
1975 if (r < 0)
1976 return r;
b9316fb0 1977
2e59b241 1978 unit_write_settingf(u, flags|UNIT_ESCAPE_SPECIFIERS, name, "Description=%s", d);
8aec412f 1979 }
9f2e86af
LP
1980
1981 return 1;
721060d4
LP
1982 }
1983
0dd3c090
ZJS
1984 /* A setting that only applies to active units. We don't actually write this to /run, this state is
1985 * managed internally. "+foo" sets flag foo, "-foo" unsets flag foo, just "foo" resets flags to
1986 * foo. The last type cannot be mixed with "+" or "-". */
1987
1988 if (streq(name, "Markers")) {
1989 unsigned settings = 0, mask = 0;
1990 bool some_plus_minus = false, some_absolute = false;
1991
1992 r = sd_bus_message_enter_container(message, 'a', "s");
1993 if (r < 0)
1994 return r;
1995
1996 for (;;) {
1997 const char *word;
1998 bool b;
1999
2000 r = sd_bus_message_read(message, "s", &word);
2001 if (r < 0)
2002 return r;
2003 if (r == 0)
2004 break;
2005
2006 if (IN_SET(word[0], '+', '-')) {
2007 b = word[0] == '+';
2008 word++;
2009 some_plus_minus = true;
2010 } else {
2011 b = true;
2012 some_absolute = true;
2013 }
2014
2015 UnitMarker m = unit_marker_from_string(word);
2016 if (m < 0)
2017 return sd_bus_error_setf(error, BUS_ERROR_BAD_UNIT_SETTING,
2018 "Unknown marker \"%s\".", word);
2019
2020 SET_FLAG(settings, 1u << m, b);
2021 SET_FLAG(mask, 1u << m, true);
2022 }
2023
2024 r = sd_bus_message_exit_container(message);
2025 if (r < 0)
2026 return r;
2027
2028 if (some_plus_minus && some_absolute)
1b09b81c 2029 return sd_bus_error_set(error, BUS_ERROR_BAD_UNIT_SETTING, "Bad marker syntax.");
0dd3c090
ZJS
2030
2031 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
2032 if (some_absolute)
2033 u->markers = settings;
2034 else
2035 u->markers = settings | (u->markers & ~mask);
2036 }
2037
2038 return 1;
2039 }
2040
721060d4
LP
2041 return 0;
2042}
2043
54fcb619
ZJS
2044static int bus_set_transient_emergency_action(
2045 Unit *u,
2046 const char *name,
2047 EmergencyAction *p,
2048 sd_bus_message *message,
2049 UnitWriteFlags flags,
2050 sd_bus_error *error) {
2051
2052 const char *s;
2053 EmergencyAction v;
2054 int r;
54fcb619
ZJS
2055
2056 assert(p);
2057
2058 r = sd_bus_message_read(message, "s", &s);
2059 if (r < 0)
2060 return r;
2061
4870133b 2062 r = parse_emergency_action(s, u->manager->runtime_scope, &v);
db2df550 2063 if (r < 0)
54fcb619 2064 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS,
119f0f28 2065 r == -EOPNOTSUPP ? "%s setting invalid for manager type: %s"
54fcb619
ZJS
2066 : "Invalid %s setting: %s",
2067 name, s);
2068
2069 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
2070 *p = v;
2071 unit_write_settingf(u, flags, name,
2072 "%s=%s", name, s);
2073 }
2074
2075 return 1;
2076}
2077
7af67e9a
LP
2078static int bus_set_transient_exit_status(
2079 Unit *u,
2080 const char *name,
2081 int *p,
2082 sd_bus_message *message,
2083 UnitWriteFlags flags,
2084 sd_bus_error *error) {
2085
2086 int32_t k;
2087 int r;
2088
2089 assert(p);
2090
2091 r = sd_bus_message_read(message, "i", &k);
2092 if (r < 0)
2093 return r;
2094
2095 if (k > 255)
1b09b81c 2096 return sd_bus_error_set(error, SD_BUS_ERROR_INVALID_ARGS, "Exit status must be in range 0…255 or negative.");
7af67e9a
LP
2097
2098 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
2099 *p = k < 0 ? -1 : k;
2100
2101 if (k < 0)
2102 unit_write_settingf(u, flags, name, "%s=", name);
2103 else
2104 unit_write_settingf(u, flags, name, "%s=%i", name, k);
2105 }
2106
2107 return 1;
2108}
2109
faa781cf 2110static BUS_DEFINE_SET_TRANSIENT_PARSE(collect_mode, CollectMode, collect_mode_from_string);
faa781cf
YW
2111static BUS_DEFINE_SET_TRANSIENT_PARSE(job_mode, JobMode, job_mode_from_string);
2112
2113static int bus_set_transient_conditions(
2114 Unit *u,
2115 const char *name,
2116 Condition **list,
2117 bool is_condition,
2118 sd_bus_message *message,
2119 UnitWriteFlags flags,
2120 sd_bus_error *error) {
2121
2122 const char *type_name, *param;
2123 int trigger, negate, r;
2124 bool empty = true;
2125
2126 assert(list);
2127
2128 r = sd_bus_message_enter_container(message, 'a', "(sbbs)");
2129 if (r < 0)
2130 return r;
2131
2132 while ((r = sd_bus_message_read(message, "(sbbs)", &type_name, &trigger, &negate, &param)) > 0) {
2133 ConditionType t;
2134
2135 t = is_condition ? condition_type_from_string(type_name) : assert_type_from_string(type_name);
2136 if (t < 0)
2137 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid condition type: %s", type_name);
2138
476cfe62
LP
2139 if (isempty(param))
2140 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Condition parameter in %s is empty", type_name);
faa781cf 2141
476cfe62
LP
2142 if (condition_takes_path(t) && !path_is_absolute(param))
2143 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Path in condition %s is not absolute: %s", type_name, param);
faa781cf
YW
2144
2145 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
2146 Condition *c;
2147
2148 c = condition_new(t, param, trigger, negate);
2149 if (!c)
2150 return -ENOMEM;
2151
2152 LIST_PREPEND(conditions, *list, c);
2153
476cfe62
LP
2154 unit_write_settingf(u, flags|UNIT_ESCAPE_SPECIFIERS, name,
2155 "%s=%s%s%s", type_name,
2156 trigger ? "|" : "", negate ? "!" : "", param);
faa781cf
YW
2157 }
2158
2159 empty = false;
2160 }
2161 if (r < 0)
2162 return r;
2163
2164 r = sd_bus_message_exit_container(message);
2165 if (r < 0)
2166 return r;
2167
2168 if (!UNIT_WRITE_FLAGS_NOOP(flags) && empty) {
2169 *list = condition_free_list(*list);
2170 unit_write_settingf(u, flags, name, "%sNull=", is_condition ? "Condition" : "Assert");
2171 }
2172
2173 return 1;
2174}
2175
721060d4
LP
2176static int bus_unit_set_transient_property(
2177 Unit *u,
2178 const char *name,
2179 sd_bus_message *message,
2180 UnitWriteFlags flags,
2181 sd_bus_error *error) {
2182
170d4026 2183 UnitDependency d;
721060d4 2184 int r;
261420ba 2185
721060d4
LP
2186 assert(u);
2187 assert(name);
2188 assert(message);
2189
cb31470f
ZJS
2190 /* Handles settings when transient units are created. This settings cannot be altered anymore after
2191 * the unit has been created. */
721060d4 2192
faa781cf
YW
2193 if (streq(name, "SourcePath"))
2194 return bus_set_transient_path(u, name, &u->source_path, message, flags, error);
261420ba 2195
faa781cf
YW
2196 if (streq(name, "StopWhenUnneeded"))
2197 return bus_set_transient_bool(u, name, &u->stop_when_unneeded, message, flags, error);
261420ba 2198
faa781cf
YW
2199 if (streq(name, "RefuseManualStart"))
2200 return bus_set_transient_bool(u, name, &u->refuse_manual_start, message, flags, error);
261420ba 2201
faa781cf
YW
2202 if (streq(name, "RefuseManualStop"))
2203 return bus_set_transient_bool(u, name, &u->refuse_manual_stop, message, flags, error);
c221420b 2204
faa781cf
YW
2205 if (streq(name, "AllowIsolate"))
2206 return bus_set_transient_bool(u, name, &u->allow_isolate, message, flags, error);
5afe510c 2207
faa781cf
YW
2208 if (streq(name, "DefaultDependencies"))
2209 return bus_set_transient_bool(u, name, &u->default_dependencies, message, flags, error);
2210
559214cb
LB
2211 if (streq(name, "SurviveFinalKillSignal"))
2212 return bus_set_transient_bool(u, name, &u->survive_final_kill_signal, message, flags, error);
2213
294446dc
LP
2214 if (streq(name, "OnSuccessJobMode"))
2215 return bus_set_transient_job_mode(u, name, &u->on_success_job_mode, message, flags, error);
2216
faa781cf
YW
2217 if (streq(name, "OnFailureJobMode"))
2218 return bus_set_transient_job_mode(u, name, &u->on_failure_job_mode, message, flags, error);
2219
2220 if (streq(name, "IgnoreOnIsolate"))
2221 return bus_set_transient_bool(u, name, &u->ignore_on_isolate, message, flags, error);
2222
2223 if (streq(name, "JobTimeoutUSec")) {
2224 r = bus_set_transient_usec_fix_0(u, name, &u->job_timeout, message, flags, error);
2225 if (r >= 0 && !UNIT_WRITE_FLAGS_NOOP(flags) && !u->job_running_timeout_set)
2226 u->job_running_timeout = u->job_timeout;
2227 }
2228
2229 if (streq(name, "JobRunningTimeoutUSec")) {
2230 r = bus_set_transient_usec_fix_0(u, name, &u->job_running_timeout, message, flags, error);
2231 if (r >= 0 && !UNIT_WRITE_FLAGS_NOOP(flags))
2232 u->job_running_timeout_set = true;
2233
2234 return r;
2235 }
2236
2237 if (streq(name, "JobTimeoutAction"))
2238 return bus_set_transient_emergency_action(u, name, &u->job_timeout_action, message, flags, error);
2239
2240 if (streq(name, "JobTimeoutRebootArgument"))
b7ad4778 2241 return bus_set_transient_reboot_parameter(u, name, &u->job_timeout_reboot_arg, message, flags, error);
faa781cf
YW
2242
2243 if (streq(name, "StartLimitIntervalUSec"))
7bf081a1 2244 return bus_set_transient_usec(u, name, &u->start_ratelimit.interval, message, flags, error);
faa781cf
YW
2245
2246 if (streq(name, "StartLimitBurst"))
7bf081a1 2247 return bus_set_transient_unsigned(u, name, &u->start_ratelimit.burst, message, flags, error);
faa781cf
YW
2248
2249 if (streq(name, "StartLimitAction"))
2250 return bus_set_transient_emergency_action(u, name, &u->start_limit_action, message, flags, error);
2251
2252 if (streq(name, "FailureAction"))
2253 return bus_set_transient_emergency_action(u, name, &u->failure_action, message, flags, error);
2254
2255 if (streq(name, "SuccessAction"))
2256 return bus_set_transient_emergency_action(u, name, &u->success_action, message, flags, error);
2257
7af67e9a
LP
2258 if (streq(name, "FailureActionExitStatus"))
2259 return bus_set_transient_exit_status(u, name, &u->failure_action_exit_status, message, flags, error);
2260
2261 if (streq(name, "SuccessActionExitStatus"))
2262 return bus_set_transient_exit_status(u, name, &u->success_action_exit_status, message, flags, error);
2263
faa781cf 2264 if (streq(name, "RebootArgument"))
b7ad4778 2265 return bus_set_transient_reboot_parameter(u, name, &u->reboot_arg, message, flags, error);
faa781cf
YW
2266
2267 if (streq(name, "CollectMode"))
2268 return bus_set_transient_collect_mode(u, name, &u->collect_mode, message, flags, error);
2269
2270 if (streq(name, "Conditions"))
2271 return bus_set_transient_conditions(u, name, &u->conditions, true, message, flags, error);
2272
2273 if (streq(name, "Asserts"))
2274 return bus_set_transient_conditions(u, name, &u->asserts, false, message, flags, error);
2275
2276 if (streq(name, "Documentation")) {
2277 _cleanup_strv_free_ char **l = NULL;
faa781cf
YW
2278
2279 r = sd_bus_message_read_strv(message, &l);
5afe510c
LP
2280 if (r < 0)
2281 return r;
2282
de010b0b 2283 STRV_FOREACH(p, l)
faa781cf
YW
2284 if (!documentation_url_is_valid(*p))
2285 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid URL in %s: %s", name, *p);
5afe510c 2286
2e59b241 2287 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
faa781cf
YW
2288 if (strv_isempty(l)) {
2289 u->documentation = strv_free(u->documentation);
2290 unit_write_settingf(u, flags, name, "%s=", name);
2291 } else {
42126367
LP
2292 r = strv_extend_strv(&u->documentation, l, /* filter_duplicates= */ false);
2293 if (r < 0)
2294 return r;
faa781cf
YW
2295
2296 STRV_FOREACH(p, l)
2297 unit_write_settingf(u, flags, name, "%s=%s", name, *p);
2298 }
5afe510c
LP
2299 }
2300
2301 return 1;
2302
d79200e2
LP
2303 } else if (streq(name, "Slice")) {
2304 Unit *slice;
c221420b 2305 const char *s;
c221420b 2306
d79200e2 2307 if (!UNIT_HAS_CGROUP_CONTEXT(u))
1b09b81c 2308 return sd_bus_error_set(error, SD_BUS_ERROR_INVALID_ARGS, "The slice property is only available for units with control groups.");
d79200e2 2309 if (u->type == UNIT_SLICE)
1b09b81c 2310 return sd_bus_error_set(error, SD_BUS_ERROR_INVALID_ARGS, "Slice may not be set for slice units.");
efdb0237 2311 if (unit_has_name(u, SPECIAL_INIT_SCOPE))
1b09b81c 2312 return sd_bus_error_set(error, SD_BUS_ERROR_INVALID_ARGS, "Cannot set slice for init.scope");
d79200e2 2313
718db961
LP
2314 r = sd_bus_message_read(message, "s", &s);
2315 if (r < 0)
2316 return r;
c221420b 2317
d79200e2
LP
2318 if (!unit_name_is_valid(s, UNIT_NAME_PLAIN))
2319 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid unit name '%s'", s);
2320
aea529e5
LP
2321 /* Note that we do not dispatch the load queue here yet, as we don't want our own transient unit to be
2322 * loaded while we are still setting it up. Or in other words, we use manager_load_unit_prepare()
2323 * instead of manager_load_unit() on purpose, here. */
2324 r = manager_load_unit_prepare(u->manager, s, NULL, error, &slice);
d79200e2
LP
2325 if (r < 0)
2326 return r;
c221420b 2327
d79200e2
LP
2328 if (slice->type != UNIT_SLICE)
2329 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Unit name '%s' is not a slice", s);
b9316fb0 2330
2e59b241 2331 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
899acf5c 2332 r = unit_set_slice(u, slice);
8aec412f
LP
2333 if (r < 0)
2334 return r;
c221420b 2335
2e59b241 2336 unit_write_settingf(u, flags|UNIT_PRIVATE, name, "Slice=%s", s);
8aec412f 2337 }
b9ec9359 2338
c221420b 2339 return 1;
d79200e2 2340
9e615fa3 2341 } else if (STR_IN_SET(name, "RequiresMountsFor", "WantsMountsFor")) {
faa781cf 2342 _cleanup_strv_free_ char **l = NULL;
faa781cf
YW
2343
2344 r = sd_bus_message_read_strv(message, &l);
2345 if (r < 0)
2346 return r;
2347
2348 STRV_FOREACH(p, l) {
4ff361cc 2349 path_simplify(*p);
b459700b 2350
faa781cf
YW
2351 if (!path_is_absolute(*p))
2352 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Path specified in %s is not absolute: %s", name, *p);
2353
b459700b
YW
2354 if (!path_is_valid(*p))
2355 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Path specified in %s has invalid length: %s", name, *p);
2356
2357 if (!path_is_normalized(*p))
2358 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Path specified in %s is not normalized: %s", name, *p);
2359
faa781cf 2360 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
9e615fa3 2361 r = unit_add_mounts_for(u, *p, UNIT_DEPENDENCY_FILE, unit_mount_dependency_type_from_string(name));
faa781cf 2362 if (r < 0)
9e615fa3 2363 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Failed to add requested mount \"%s\": %m", *p);
7fb3ee51 2364
faa781cf
YW
2365 unit_write_settingf(u, flags, name, "%s=%s", name, *p);
2366 }
f32b43bd 2367 }
7fb3ee51 2368
faa781cf
YW
2369 return 1;
2370 }
2371
2372 if (streq(name, "RequiresOverridable"))
2373 d = UNIT_REQUIRES; /* redirect for obsolete unit dependency type */
2374 else if (streq(name, "RequisiteOverridable"))
2375 d = UNIT_REQUISITE; /* same here */
2376 else
2377 d = unit_dependency_from_string(name);
2378
2379 if (d >= 0) {
2380 const char *other;
2381
40430848
LP
2382 if (!IN_SET(d,
2383 UNIT_REQUIRES,
2384 UNIT_REQUISITE,
2385 UNIT_WANTS,
2386 UNIT_BINDS_TO,
2387 UNIT_PART_OF,
0bc488c9 2388 UNIT_UPHOLDS,
40430848
LP
2389 UNIT_CONFLICTS,
2390 UNIT_BEFORE,
2391 UNIT_AFTER,
294446dc 2392 UNIT_ON_SUCCESS,
40430848
LP
2393 UNIT_ON_FAILURE,
2394 UNIT_PROPAGATES_RELOAD_TO,
2395 UNIT_RELOAD_PROPAGATED_FROM,
ffec78c0
LP
2396 UNIT_PROPAGATES_STOP_TO,
2397 UNIT_STOP_PROPAGATED_FROM,
40430848
LP
2398 UNIT_JOINS_NAMESPACE_OF))
2399 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Dependency type %s may not be created transiently.", unit_dependency_to_string(d));
2400
718db961
LP
2401 r = sd_bus_message_enter_container(message, 'a', "s");
2402 if (r < 0)
2403 return r;
7fb3ee51 2404
718db961 2405 while ((r = sd_bus_message_read(message, "s", &other)) > 0) {
7410616c 2406 if (!unit_name_is_valid(other, UNIT_NAME_PLAIN|UNIT_NAME_INSTANCE))
718db961 2407 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid unit name %s", other);
7fb3ee51 2408
2e59b241 2409 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
b9ec9359 2410 _cleanup_free_ char *label = NULL;
7fb3ee51 2411
35d8c19a 2412 r = unit_add_dependency_by_name(u, d, other, true, UNIT_DEPENDENCY_FILE);
7fb3ee51
LP
2413 if (r < 0)
2414 return r;
2415
605405c6 2416 label = strjoin(name, "-", other);
7fb3ee51
LP
2417 if (!label)
2418 return -ENOMEM;
2419
faa781cf 2420 unit_write_settingf(u, flags, label, "%s=%s", unit_dependency_to_string(d), other);
7fb3ee51
LP
2421 }
2422
7fb3ee51 2423 }
718db961
LP
2424 if (r < 0)
2425 return r;
7fb3ee51 2426
6ce270b1
LP
2427 r = sd_bus_message_exit_container(message);
2428 if (r < 0)
2429 return r;
2430
7fb3ee51 2431 return 1;
05a98afd
LP
2432
2433 } else if (streq(name, "AddRef")) {
2434
2435 int b;
2436
2437 /* Why is this called "AddRef" rather than just "Ref", or "Reference"? There's already a "Ref()" method
2438 * on the Unit interface, and it's probably not a good idea to expose a property and a method on the
2439 * same interface (well, strictly speaking AddRef isn't exposed as full property, we just read it for
2440 * transient units, but still). And "References" and "ReferencedBy" is already used as unit reference
2441 * dependency type, hence let's not confuse things with that.
2442 *
5238e957 2443 * Note that we don't actually add the reference to the bus track. We do that only after the setup of
05a98afd
LP
2444 * the transient unit is complete, so that setting this property multiple times in the same transient
2445 * unit creation call doesn't count as individual references. */
2446
2447 r = sd_bus_message_read(message, "b", &b);
2448 if (r < 0)
2449 return r;
2450
2e59b241 2451 if (!UNIT_WRITE_FLAGS_NOOP(flags))
05a98afd
LP
2452 u->bus_track_add = b;
2453
2454 return 1;
9f2e86af
LP
2455 }
2456
2457 return 0;
2458}
2459
c2756a68
LP
2460int bus_unit_set_properties(
2461 Unit *u,
718db961 2462 sd_bus_message *message,
2e59b241 2463 UnitWriteFlags flags,
c2756a68 2464 bool commit,
718db961 2465 sd_bus_error *error) {
c2756a68 2466
8e2af478 2467 bool for_real = false;
8e2af478
LP
2468 unsigned n = 0;
2469 int r;
2470
2471 assert(u);
718db961 2472 assert(message);
8e2af478 2473
4a055e5a
ZJS
2474 /* We iterate through the array twice. First run just checks if all passed data is valid, second run
2475 * actually applies it. This implements transaction-like behaviour without actually providing full
2476 * transactions. */
8e2af478 2477
718db961
LP
2478 r = sd_bus_message_enter_container(message, 'a', "(sv)");
2479 if (r < 0)
44d82df4 2480 goto error;
8e2af478 2481
8e2af478 2482 for (;;) {
8e2af478 2483 const char *name;
2e59b241 2484 UnitWriteFlags f;
8e2af478 2485
718db961
LP
2486 r = sd_bus_message_enter_container(message, 'r', "sv");
2487 if (r < 0)
44d82df4 2488 goto error;
718db961 2489 if (r == 0) {
2e59b241 2490 if (for_real || UNIT_WRITE_FLAGS_NOOP(flags))
8e2af478
LP
2491 break;
2492
2493 /* Reached EOF. Let's try again, and this time for realz... */
718db961
LP
2494 r = sd_bus_message_rewind(message, false);
2495 if (r < 0)
44d82df4 2496 goto error;
6ce270b1 2497
8e2af478
LP
2498 for_real = true;
2499 continue;
2500 }
2501
718db961
LP
2502 r = sd_bus_message_read(message, "s", &name);
2503 if (r < 0)
44d82df4 2504 goto error;
8e2af478 2505
718db961
LP
2506 r = sd_bus_message_enter_container(message, 'v', NULL);
2507 if (r < 0)
44d82df4 2508 goto error;
8e2af478 2509
2e59b241
LP
2510 /* If not for real, then mask out the two target flags */
2511 f = for_real ? flags : (flags & ~(UNIT_RUNTIME|UNIT_PERSISTENT));
2512
eab62c01
ZJS
2513 if (UNIT_VTABLE(u)->bus_set_property)
2514 r = UNIT_VTABLE(u)->bus_set_property(u, name, message, f, error);
2515 else
2516 r = 0;
9f2e86af 2517 if (r == 0 && u->transient && u->load_state == UNIT_STUB)
2e59b241 2518 r = bus_unit_set_transient_property(u, name, message, f, error);
721060d4
LP
2519 if (r == 0)
2520 r = bus_unit_set_live_property(u, name, message, f, error);
718db961 2521 if (r < 0)
44d82df4 2522 goto error;
721060d4 2523
718db961 2524 if (r == 0)
cb31470f
ZJS
2525 return sd_bus_error_setf(error, SD_BUS_ERROR_PROPERTY_READ_ONLY,
2526 "Cannot set property %s, or unknown property.", name);
718db961
LP
2527
2528 r = sd_bus_message_exit_container(message);
8e2af478 2529 if (r < 0)
44d82df4 2530 goto error;
8e2af478 2531
718db961
LP
2532 r = sd_bus_message_exit_container(message);
2533 if (r < 0)
44d82df4 2534 goto error;
8e2af478
LP
2535
2536 n += for_real;
2537 }
2538
6ce270b1
LP
2539 r = sd_bus_message_exit_container(message);
2540 if (r < 0)
44d82df4 2541 goto error;
6ce270b1 2542
c2756a68 2543 if (commit && n > 0 && UNIT_VTABLE(u)->bus_commit_properties)
8e2af478
LP
2544 UNIT_VTABLE(u)->bus_commit_properties(u);
2545
241da328 2546 return n;
44d82df4
ZJS
2547
2548 error:
2549 /* Pretty much any of the calls above can fail if the message is not formed properly
2550 * or if it has unexpected contents. Fill in a more informative error message here. */
2551 if (sd_bus_error_is_set(error))
2552 return r;
2553 return sd_bus_error_set_errnof(error, r,
2554 r == -ENXIO ? "Failed to set unit properties: Unexpected message contents"
2555 : "Failed to set unit properties: %m");
8e2af478 2556}
000a996d 2557
e49da001 2558int bus_unit_validate_load_state(Unit *u, sd_bus_error *error) {
6eb7c172 2559 assert(u);
000a996d 2560
e49da001 2561 /* Generates a pretty error if a unit isn't properly loaded. */
000a996d 2562
e49da001
LP
2563 switch (u->load_state) {
2564
2565 case UNIT_LOADED:
2566 return 0;
000a996d 2567
e49da001 2568 case UNIT_NOT_FOUND:
b21d2f81 2569 return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_UNIT, "Unit %s not found.", u->id);
000a996d 2570
c4555ad8
LP
2571 case UNIT_BAD_SETTING:
2572 return sd_bus_error_setf(error, BUS_ERROR_BAD_UNIT_SETTING, "Unit %s has a bad unit file setting.", u->id);
2573
e49da001 2574 case UNIT_ERROR: /* Only show .load_error in UNIT_ERROR state */
6b10a2e0
LP
2575 return sd_bus_error_set_errnof(error, u->load_error,
2576 "Unit %s failed to load properly, please adjust/correct and reload service manager: %m", u->id);
e49da001
LP
2577
2578 case UNIT_MASKED:
2579 return sd_bus_error_setf(error, BUS_ERROR_UNIT_MASKED, "Unit %s is masked.", u->id);
2580
2581 case UNIT_STUB:
2582 case UNIT_MERGED:
2583 default:
2584 return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_UNIT, "Unexpected load state of unit %s", u->id);
2585 }
000a996d 2586}
05a98afd 2587
c5a97ed1 2588static int bus_unit_track_handler(sd_bus_track *t, void *userdata) {
99534007 2589 Unit *u = ASSERT_PTR(userdata);
05a98afd
LP
2590
2591 assert(t);
05a98afd
LP
2592
2593 u->bus_track = sd_bus_track_unref(u->bus_track); /* make sure we aren't called again */
2594
e5c36295
LP
2595 /* If the client that tracks us disappeared, then there's reason to believe that the cgroup is empty now too,
2596 * let's see */
2597 unit_add_to_cgroup_empty_queue(u);
2598
2599 /* Also add the unit to the GC queue, after all if the client left it might be time to GC this unit */
05a98afd 2600 unit_add_to_gc_queue(u);
e5c36295 2601
05a98afd
LP
2602 return 0;
2603}
2604
c5a97ed1 2605static int bus_unit_allocate_bus_track(Unit *u) {
05a98afd
LP
2606 int r;
2607
2608 assert(u);
2609
2610 if (u->bus_track)
2611 return 0;
2612
c5a97ed1 2613 r = sd_bus_track_new(u->manager->api_bus, &u->bus_track, bus_unit_track_handler, u);
05a98afd
LP
2614 if (r < 0)
2615 return r;
2616
2617 r = sd_bus_track_set_recursive(u->bus_track, true);
2618 if (r < 0) {
2619 u->bus_track = sd_bus_track_unref(u->bus_track);
2620 return r;
2621 }
2622
2623 return 0;
2624}
2625
2626int bus_unit_track_add_name(Unit *u, const char *name) {
2627 int r;
2628
2629 assert(u);
2630
c5a97ed1 2631 r = bus_unit_allocate_bus_track(u);
05a98afd
LP
2632 if (r < 0)
2633 return r;
2634
2635 return sd_bus_track_add_name(u->bus_track, name);
2636}
2637
2638int bus_unit_track_add_sender(Unit *u, sd_bus_message *m) {
2639 int r;
2640
2641 assert(u);
2642
c5a97ed1 2643 r = bus_unit_allocate_bus_track(u);
05a98afd
LP
2644 if (r < 0)
2645 return r;
2646
2647 return sd_bus_track_add_sender(u->bus_track, m);
2648}
2649
2650int bus_unit_track_remove_sender(Unit *u, sd_bus_message *m) {
2651 assert(u);
2652
cb31470f
ZJS
2653 /* If we haven't allocated the bus track object yet, then there's definitely no reference taken yet,
2654 * return an error */
05a98afd
LP
2655 if (!u->bus_track)
2656 return -EUNATCH;
2657
2658 return sd_bus_track_remove_sender(u->bus_track, m);
2659}