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