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