]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/core/dbus-unit.c
Merge pull request #2594 from keszybz/spelling
[thirdparty/systemd.git] / src / core / dbus-unit.c
CommitLineData
a7334b09
LP
1/***
2 This file is part of systemd.
3
4 Copyright 2010 Lennart Poettering
5
6 systemd is free software; you can redistribute it and/or modify it
5430f7f2
LP
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
a7334b09
LP
9 (at your option) any later version.
10
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
5430f7f2 14 Lesser General Public License for more details.
a7334b09 15
5430f7f2 16 You should have received a copy of the GNU Lesser General Public License
a7334b09
LP
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
18***/
19
718db961 20#include "sd-bus.h"
07630cea 21
b5efdb8a 22#include "alloc-util.h"
07630cea
LP
23#include "bus-common-errors.h"
24#include "cgroup-util.h"
8752c575 25#include "dbus-unit.h"
07630cea 26#include "dbus.h"
8752c575 27#include "locale-util.h"
ea430986 28#include "log.h"
e2417e41 29#include "selinux-access.h"
efdb0237 30#include "special.h"
07630cea
LP
31#include "string-util.h"
32#include "strv.h"
ee104e11 33#include "user-util.h"
718db961
LP
34
35static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_load_state, unit_load_state, UnitLoadState);
d420282b 36static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_job_mode, job_mode, JobMode);
f189ab18 37static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_failure_action, failure_action, FailureAction);
718db961
LP
38
39static int property_get_names(
40 sd_bus *bus,
41 const char *path,
42 const char *interface,
43 const char *property,
44 sd_bus_message *reply,
ebcf1f97
LP
45 void *userdata,
46 sd_bus_error *error) {
718db961
LP
47
48 Unit *u = userdata;
49 Iterator i;
50 const char *t;
51 int r;
ea430986 52
718db961
LP
53 assert(bus);
54 assert(reply);
55 assert(u);
4139c1b2 56
718db961
LP
57 r = sd_bus_message_open_container(reply, 'a', "s");
58 if (r < 0)
59 return r;
4139c1b2 60
718db961
LP
61 SET_FOREACH(t, u->names, i) {
62 r = sd_bus_message_append(reply, "s", t);
63 if (r < 0)
64 return r;
65 }
4139c1b2 66
718db961 67 return sd_bus_message_close_container(reply);
4139c1b2
LP
68}
69
718db961
LP
70static int property_get_following(
71 sd_bus *bus,
72 const char *path,
73 const char *interface,
74 const char *property,
75 sd_bus_message *reply,
ebcf1f97
LP
76 void *userdata,
77 sd_bus_error *error) {
718db961
LP
78
79 Unit *u = userdata, *f;
8fe914ec 80
718db961
LP
81 assert(bus);
82 assert(reply);
8fe914ec
LP
83 assert(u);
84
a7f241db 85 f = unit_following(u);
718db961 86 return sd_bus_message_append(reply, "s", f ? f->id : "");
8fe914ec
LP
87}
88
718db961
LP
89static int property_get_dependencies(
90 sd_bus *bus,
91 const char *path,
92 const char *interface,
93 const char *property,
94 sd_bus_message *reply,
ebcf1f97
LP
95 void *userdata,
96 sd_bus_error *error) {
4ec9a8a4 97
718db961 98 Set *s = *(Set**) userdata;
5301be81 99 Iterator j;
718db961
LP
100 Unit *u;
101 int r;
5301be81 102
718db961
LP
103 assert(bus);
104 assert(reply);
5301be81 105
718db961
LP
106 r = sd_bus_message_open_container(reply, 'a', "s");
107 if (r < 0)
108 return r;
5301be81 109
718db961
LP
110 SET_FOREACH(u, s, j) {
111 r = sd_bus_message_append(reply, "s", u->id);
112 if (r < 0)
113 return r;
114 }
5301be81 115
718db961 116 return sd_bus_message_close_container(reply);
5301be81
LP
117}
118
f32b43bd
LP
119static int property_get_obsolete_dependencies(
120 sd_bus *bus,
121 const char *path,
122 const char *interface,
123 const char *property,
124 sd_bus_message *reply,
125 void *userdata,
126 sd_bus_error *error) {
127
128 assert(bus);
129 assert(reply);
130
131 /* For dependency types we don't support anymore always return an empty array */
132 return sd_bus_message_append(reply, "as", 0);
133}
134
718db961
LP
135static int property_get_description(
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) {
ea430986 143
718db961 144 Unit *u = userdata;
ea430986 145
718db961
LP
146 assert(bus);
147 assert(reply);
148 assert(u);
ea430986 149
718db961 150 return sd_bus_message_append(reply, "s", unit_description(u));
ea430986
LP
151}
152
718db961
LP
153static int property_get_active_state(
154 sd_bus *bus,
155 const char *path,
156 const char *interface,
157 const char *property,
158 sd_bus_message *reply,
ebcf1f97
LP
159 void *userdata,
160 sd_bus_error *error) {
ea430986 161
718db961 162 Unit *u = userdata;
ea430986 163
718db961
LP
164 assert(bus);
165 assert(reply);
ea430986
LP
166 assert(u);
167
718db961 168 return sd_bus_message_append(reply, "s", unit_active_state_to_string(unit_active_state(u)));
ea430986
LP
169}
170
718db961
LP
171static int property_get_sub_state(
172 sd_bus *bus,
173 const char *path,
174 const char *interface,
175 const char *property,
176 sd_bus_message *reply,
ebcf1f97
LP
177 void *userdata,
178 sd_bus_error *error) {
10a94420 179
718db961 180 Unit *u = userdata;
10a94420 181
718db961
LP
182 assert(bus);
183 assert(reply);
184 assert(u);
10a94420 185
718db961 186 return sd_bus_message_append(reply, "s", unit_sub_state_to_string(u));
10a94420
LP
187}
188
d2dc52db
LP
189static int property_get_unit_file_preset(
190 sd_bus *bus,
191 const char *path,
192 const char *interface,
193 const char *property,
194 sd_bus_message *reply,
195 void *userdata,
196 sd_bus_error *error) {
197
198 Unit *u = userdata;
199 int r;
200
201 assert(bus);
202 assert(reply);
203 assert(u);
204
205 r = unit_get_unit_file_preset(u);
206
207 return sd_bus_message_append(reply, "s",
208 r < 0 ? "":
209 r > 0 ? "enabled" : "disabled");
210}
211
718db961
LP
212static int property_get_unit_file_state(
213 sd_bus *bus,
214 const char *path,
215 const char *interface,
216 const char *property,
217 sd_bus_message *reply,
ebcf1f97
LP
218 void *userdata,
219 sd_bus_error *error) {
a4375746 220
718db961 221 Unit *u = userdata;
a4375746 222
718db961
LP
223 assert(bus);
224 assert(reply);
225 assert(u);
a4375746 226
76445832 227 return sd_bus_message_append(reply, "s", unit_file_state_to_string(unit_get_unit_file_state(u)));
a4375746
LP
228}
229
718db961
LP
230static int property_get_can_start(
231 sd_bus *bus,
232 const char *path,
233 const char *interface,
234 const char *property,
235 sd_bus_message *reply,
ebcf1f97
LP
236 void *userdata,
237 sd_bus_error *error) {
38131695 238
718db961 239 Unit *u = userdata;
b5e9dba8 240
718db961
LP
241 assert(bus);
242 assert(reply);
243 assert(u);
b5e9dba8 244
718db961 245 return sd_bus_message_append(reply, "b", unit_can_start(u) && !u->refuse_manual_start);
b5e9dba8
LP
246}
247
718db961
LP
248static int property_get_can_stop(
249 sd_bus *bus,
250 const char *path,
251 const char *interface,
252 const char *property,
253 sd_bus_message *reply,
ebcf1f97
LP
254 void *userdata,
255 sd_bus_error *error) {
718db961
LP
256
257 Unit *u = userdata;
b5e9dba8 258
718db961
LP
259 assert(bus);
260 assert(reply);
b5e9dba8
LP
261 assert(u);
262
263 /* On the lower levels we assume that every unit we can start
264 * we can also stop */
265
718db961 266 return sd_bus_message_append(reply, "b", unit_can_start(u) && !u->refuse_manual_stop);
38131695
LP
267}
268
718db961
LP
269static int property_get_can_reload(
270 sd_bus *bus,
271 const char *path,
272 const char *interface,
273 const char *property,
274 sd_bus_message *reply,
ebcf1f97
LP
275 void *userdata,
276 sd_bus_error *error) {
38131695 277
718db961 278 Unit *u = userdata;
38131695 279
718db961
LP
280 assert(bus);
281 assert(reply);
282 assert(u);
38131695 283
718db961 284 return sd_bus_message_append(reply, "b", unit_can_reload(u));
38131695
LP
285}
286
718db961
LP
287static int property_get_can_isolate(
288 sd_bus *bus,
289 const char *path,
290 const char *interface,
291 const char *property,
292 sd_bus_message *reply,
ebcf1f97
LP
293 void *userdata,
294 sd_bus_error *error) {
2528a7a6 295
718db961 296 Unit *u = userdata;
2528a7a6 297
718db961
LP
298 assert(bus);
299 assert(reply);
300 assert(u);
2528a7a6 301
718db961 302 return sd_bus_message_append(reply, "b", unit_can_isolate(u) && !u->refuse_manual_start);
2528a7a6
LP
303}
304
718db961
LP
305static int property_get_job(
306 sd_bus *bus,
307 const char *path,
308 const char *interface,
309 const char *property,
310 sd_bus_message *reply,
ebcf1f97
LP
311 void *userdata,
312 sd_bus_error *error) {
718db961 313
f93891f3 314 _cleanup_free_ char *p = NULL;
718db961 315 Unit *u = userdata;
38131695 316
718db961
LP
317 assert(bus);
318 assert(reply);
38131695
LP
319 assert(u);
320
718db961
LP
321 if (!u->job)
322 return sd_bus_message_append(reply, "(uo)", 0, "/");
38131695 323
718db961
LP
324 p = job_dbus_path(u->job);
325 if (!p)
326 return -ENOMEM;
38131695 327
718db961
LP
328 return sd_bus_message_append(reply, "(uo)", u->job->id, p);
329}
38131695 330
718db961
LP
331static int property_get_need_daemon_reload(
332 sd_bus *bus,
333 const char *path,
334 const char *interface,
335 const char *property,
336 sd_bus_message *reply,
ebcf1f97
LP
337 void *userdata,
338 sd_bus_error *error) {
38131695 339
718db961 340 Unit *u = userdata;
38131695 341
718db961
LP
342 assert(bus);
343 assert(reply);
344 assert(u);
38131695 345
718db961 346 return sd_bus_message_append(reply, "b", unit_need_daemon_reload(u));
38131695
LP
347}
348
718db961
LP
349static int property_get_conditions(
350 sd_bus *bus,
351 const char *path,
352 const char *interface,
353 const char *property,
354 sd_bus_message *reply,
ebcf1f97
LP
355 void *userdata,
356 sd_bus_error *error) {
45fb0699 357
59fccdc5
LP
358 const char *(*to_string)(ConditionType type) = NULL;
359 Condition **list = userdata, *c;
718db961 360 int r;
45fb0699 361
718db961
LP
362 assert(bus);
363 assert(reply);
59fccdc5
LP
364 assert(list);
365
366 to_string = streq(property, "Asserts") ? assert_type_to_string : condition_type_to_string;
45fb0699 367
718db961
LP
368 r = sd_bus_message_open_container(reply, 'a', "(sbbsi)");
369 if (r < 0)
370 return r;
45fb0699 371
59fccdc5 372 LIST_FOREACH(conditions, c, *list) {
cc50ef13
LP
373 int tristate;
374
375 tristate =
376 c->result == CONDITION_UNTESTED ? 0 :
377 c->result == CONDITION_SUCCEEDED ? 1 : -1;
378
2c7e050f 379 r = sd_bus_message_append(reply, "(sbbsi)",
59fccdc5 380 to_string(c->type),
2c7e050f 381 c->trigger, c->negate,
cc50ef13 382 c->parameter, tristate);
718db961
LP
383 if (r < 0)
384 return r;
45fb0699 385
718db961 386 }
52990c2e 387
718db961 388 return sd_bus_message_close_container(reply);
52990c2e
ZJS
389}
390
718db961
LP
391static int property_get_load_error(
392 sd_bus *bus,
393 const char *path,
394 const char *interface,
395 const char *property,
396 sd_bus_message *reply,
ebcf1f97
LP
397 void *userdata,
398 sd_bus_error *error) {
52990c2e 399
4afd3348 400 _cleanup_(sd_bus_error_free) sd_bus_error e = SD_BUS_ERROR_NULL;
718db961 401 Unit *u = userdata;
52990c2e 402
718db961
LP
403 assert(bus);
404 assert(reply);
405 assert(u);
52990c2e 406
718db961
LP
407 if (u->load_error != 0)
408 sd_bus_error_set_errno(&e, u->load_error);
52990c2e 409
718db961 410 return sd_bus_message_append(reply, "(ss)", e.name, e.message);
52990c2e
ZJS
411}
412
88ced61b
MC
413static int bus_verify_manage_units_async_full(
414 Unit *u,
415 const char *verb,
416 int capability,
417 const char *polkit_message,
418 sd_bus_message *call,
419 sd_bus_error *error) {
420
421 const char *details[9] = {
422 "unit", u->id,
423 "verb", verb,
424 };
425
426 if (polkit_message) {
427 details[4] = "polkit.message";
428 details[5] = polkit_message;
429 details[6] = "polkit.gettext_domain";
430 details[7] = GETTEXT_PACKAGE;
431 }
432
433 return bus_verify_polkit_async(call, capability, "org.freedesktop.systemd1.manage-units", details, false, UID_INVALID, &u->manager->polkit_registry, error);
434}
435
1d22e906 436int bus_unit_method_start_generic(
1d22e906
LP
437 sd_bus_message *message,
438 Unit *u,
439 JobType job_type,
440 bool reload_if_possible,
441 sd_bus_error *error) {
442
718db961
LP
443 const char *smode;
444 JobMode mode;
88ced61b
MC
445 _cleanup_free_ char *verb = NULL;
446 static const char *const polkit_message_for_job[_JOB_TYPE_MAX] = {
447 [JOB_START] = N_("Authentication is required to start '$(unit)'."),
448 [JOB_STOP] = N_("Authentication is required to stop '$(unit)'."),
449 [JOB_RELOAD] = N_("Authentication is required to reload '$(unit)'."),
450 [JOB_RESTART] = N_("Authentication is required to restart '$(unit)'."),
451 [JOB_TRY_RESTART] = N_("Authentication is required to restart '$(unit)'."),
452 };
718db961 453 int r;
9f39404c 454
718db961 455 assert(message);
9f39404c 456 assert(u);
718db961 457 assert(job_type >= 0 && job_type < _JOB_TYPE_MAX);
9f39404c 458
61ea63f1
EV
459 r = mac_selinux_unit_access_check(
460 u, message,
94bd7323
EV
461 job_type_to_access_method(job_type),
462 error);
1d22e906
LP
463 if (r < 0)
464 return r;
465
718db961
LP
466 r = sd_bus_message_read(message, "s", &smode);
467 if (r < 0)
ebcf1f97 468 return r;
9f39404c 469
718db961
LP
470 mode = job_mode_from_string(smode);
471 if (mode < 0)
ebcf1f97 472 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Job mode %s invalid", smode);
9f39404c 473
88ced61b
MC
474 if (reload_if_possible)
475 verb = strjoin("reload-or-", job_type_to_string(job_type), NULL);
476 else
477 verb = strdup(job_type_to_string(job_type));
478 if (!verb)
479 return -ENOMEM;
480
481 r = bus_verify_manage_units_async_full(
482 u,
483 verb,
484 CAP_SYS_ADMIN,
485 job_type < _JOB_TYPE_MAX ? polkit_message_for_job[job_type] : NULL,
486 message,
487 error);
1d22e906
LP
488 if (r < 0)
489 return r;
490 if (r == 0)
491 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
492
19070062 493 return bus_unit_queue_job(message, u, job_type, mode, reload_if_possible, error);
9f39404c
LP
494}
495
19070062
LP
496static int method_start(sd_bus_message *message, void *userdata, sd_bus_error *error) {
497 return bus_unit_method_start_generic(message, userdata, JOB_START, false, error);
718db961 498}
b548631a 499
19070062
LP
500static int method_stop(sd_bus_message *message, void *userdata, sd_bus_error *error) {
501 return bus_unit_method_start_generic(message, userdata, JOB_STOP, false, error);
718db961
LP
502}
503
19070062
LP
504static int method_reload(sd_bus_message *message, void *userdata, sd_bus_error *error) {
505 return bus_unit_method_start_generic(message, userdata, JOB_RELOAD, false, error);
718db961 506}
0a524ba7 507
19070062
LP
508static int method_restart(sd_bus_message *message, void *userdata, sd_bus_error *error) {
509 return bus_unit_method_start_generic(message, userdata, JOB_RESTART, false, error);
718db961 510}
8a0867d6 511
19070062
LP
512static int method_try_restart(sd_bus_message *message, void *userdata, sd_bus_error *error) {
513 return bus_unit_method_start_generic(message, userdata, JOB_TRY_RESTART, false, error);
718db961 514}
cad45ba1 515
19070062
LP
516static int method_reload_or_restart(sd_bus_message *message, void *userdata, sd_bus_error *error) {
517 return bus_unit_method_start_generic(message, userdata, JOB_RESTART, true, error);
718db961 518}
8a0867d6 519
19070062
LP
520static int method_reload_or_try_restart(sd_bus_message *message, void *userdata, sd_bus_error *error) {
521 return bus_unit_method_start_generic(message, userdata, JOB_TRY_RESTART, true, error);
718db961 522}
8a0867d6 523
19070062 524int bus_unit_method_kill(sd_bus_message *message, void *userdata, sd_bus_error *error) {
718db961
LP
525 Unit *u = userdata;
526 const char *swho;
527 int32_t signo;
528 KillWho who;
529 int r;
5632e374 530
718db961
LP
531 assert(message);
532 assert(u);
cad45ba1 533
1d22e906 534 r = mac_selinux_unit_access_check(u, message, "stop", error);
283868e1
SW
535 if (r < 0)
536 return r;
283868e1 537
718db961
LP
538 r = sd_bus_message_read(message, "si", &swho, &signo);
539 if (r < 0)
ebcf1f97 540 return r;
718db961
LP
541
542 if (isempty(swho))
543 who = KILL_ALL;
544 else {
545 who = kill_who_from_string(swho);
546 if (who < 0)
ebcf1f97 547 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid who argument %s", swho);
718db961 548 }
5632e374 549
718db961 550 if (signo <= 0 || signo >= _NSIG)
ebcf1f97 551 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Signal number out of range.");
8e2af478 552
88ced61b
MC
553 r = bus_verify_manage_units_async_full(
554 u,
555 "kill",
556 CAP_KILL,
557 N_("Authentication is required to kill '$(unit)'."),
558 message,
559 error);
ebcf1f97
LP
560 if (r < 0)
561 return r;
1d22e906
LP
562 if (r == 0)
563 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
8e2af478 564
ebcf1f97 565 r = unit_kill(u, who, signo, error);
718db961 566 if (r < 0)
ebcf1f97 567 return r;
8e2af478 568
df2d202e 569 return sd_bus_reply_method_return(message, NULL);
718db961 570}
8e2af478 571
19070062 572int bus_unit_method_reset_failed(sd_bus_message *message, void *userdata, sd_bus_error *error) {
718db961 573 Unit *u = userdata;
ebcf1f97 574 int r;
b548631a 575
718db961
LP
576 assert(message);
577 assert(u);
b548631a 578
1d22e906 579 r = mac_selinux_unit_access_check(u, message, "reload", error);
283868e1
SW
580 if (r < 0)
581 return r;
283868e1 582
88ced61b
MC
583 r = bus_verify_manage_units_async_full(
584 u,
585 "reset-failed",
586 CAP_SYS_ADMIN,
587 N_("Authentication is required to reset the \"failed\" state of '$(unit)'."),
588 message,
589 error);
ebcf1f97
LP
590 if (r < 0)
591 return r;
1d22e906
LP
592 if (r == 0)
593 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
b548631a 594
718db961 595 unit_reset_failed(u);
b548631a 596
df2d202e 597 return sd_bus_reply_method_return(message, NULL);
ea430986
LP
598}
599
19070062 600int bus_unit_method_set_properties(sd_bus_message *message, void *userdata, sd_bus_error *error) {
718db961
LP
601 Unit *u = userdata;
602 int runtime, r;
ea430986 603
ea430986 604 assert(message);
718db961 605 assert(u);
80fbf05e 606
1d22e906 607 r = mac_selinux_unit_access_check(u, message, "start", error);
283868e1
SW
608 if (r < 0)
609 return r;
283868e1 610
718db961
LP
611 r = sd_bus_message_read(message, "b", &runtime);
612 if (r < 0)
ebcf1f97 613 return r;
2cccbca4 614
88ced61b
MC
615 r = bus_verify_manage_units_async_full(
616 u,
617 "set-property",
618 CAP_SYS_ADMIN,
619 N_("Authentication is required to set properties on '$(unit)'."),
620 message,
621 error);
ebcf1f97
LP
622 if (r < 0)
623 return r;
1d22e906
LP
624 if (r == 0)
625 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
cad45ba1 626
ebcf1f97 627 r = bus_unit_set_properties(u, message, runtime ? UNIT_RUNTIME : UNIT_PERSISTENT, true, error);
718db961 628 if (r < 0)
ebcf1f97 629 return r;
2cccbca4 630
df2d202e 631 return sd_bus_reply_method_return(message, NULL);
718db961 632}
2cccbca4 633
718db961
LP
634const sd_bus_vtable bus_unit_vtable[] = {
635 SD_BUS_VTABLE_START(0),
636
556089dc
LP
637 SD_BUS_PROPERTY("Id", "s", NULL, offsetof(Unit, id), SD_BUS_VTABLE_PROPERTY_CONST),
638 SD_BUS_PROPERTY("Names", "as", property_get_names, 0, SD_BUS_VTABLE_PROPERTY_CONST),
718db961 639 SD_BUS_PROPERTY("Following", "s", property_get_following, 0, 0),
556089dc 640 SD_BUS_PROPERTY("Requires", "as", property_get_dependencies, offsetof(Unit, dependencies[UNIT_REQUIRES]), SD_BUS_VTABLE_PROPERTY_CONST),
556089dc 641 SD_BUS_PROPERTY("Requisite", "as", property_get_dependencies, offsetof(Unit, dependencies[UNIT_REQUISITE]), SD_BUS_VTABLE_PROPERTY_CONST),
556089dc
LP
642 SD_BUS_PROPERTY("Wants", "as", property_get_dependencies, offsetof(Unit, dependencies[UNIT_WANTS]), SD_BUS_VTABLE_PROPERTY_CONST),
643 SD_BUS_PROPERTY("BindsTo", "as", property_get_dependencies, offsetof(Unit, dependencies[UNIT_BINDS_TO]), SD_BUS_VTABLE_PROPERTY_CONST),
644 SD_BUS_PROPERTY("PartOf", "as", property_get_dependencies, offsetof(Unit, dependencies[UNIT_PART_OF]), SD_BUS_VTABLE_PROPERTY_CONST),
645 SD_BUS_PROPERTY("RequiredBy", "as", property_get_dependencies, offsetof(Unit, dependencies[UNIT_REQUIRED_BY]), SD_BUS_VTABLE_PROPERTY_CONST),
be7d9ff7 646 SD_BUS_PROPERTY("RequisiteOf", "as", property_get_dependencies, offsetof(Unit, dependencies[UNIT_REQUISITE_OF]), SD_BUS_VTABLE_PROPERTY_CONST),
556089dc
LP
647 SD_BUS_PROPERTY("WantedBy", "as", property_get_dependencies, offsetof(Unit, dependencies[UNIT_WANTED_BY]), SD_BUS_VTABLE_PROPERTY_CONST),
648 SD_BUS_PROPERTY("BoundBy", "as", property_get_dependencies, offsetof(Unit, dependencies[UNIT_BOUND_BY]), SD_BUS_VTABLE_PROPERTY_CONST),
649 SD_BUS_PROPERTY("ConsistsOf", "as", property_get_dependencies, offsetof(Unit, dependencies[UNIT_CONSISTS_OF]), SD_BUS_VTABLE_PROPERTY_CONST),
650 SD_BUS_PROPERTY("Conflicts", "as", property_get_dependencies, offsetof(Unit, dependencies[UNIT_CONFLICTS]), SD_BUS_VTABLE_PROPERTY_CONST),
651 SD_BUS_PROPERTY("ConflictedBy", "as", property_get_dependencies, offsetof(Unit, dependencies[UNIT_CONFLICTED_BY]), SD_BUS_VTABLE_PROPERTY_CONST),
652 SD_BUS_PROPERTY("Before", "as", property_get_dependencies, offsetof(Unit, dependencies[UNIT_BEFORE]), SD_BUS_VTABLE_PROPERTY_CONST),
653 SD_BUS_PROPERTY("After", "as", property_get_dependencies, offsetof(Unit, dependencies[UNIT_AFTER]), SD_BUS_VTABLE_PROPERTY_CONST),
654 SD_BUS_PROPERTY("OnFailure", "as", property_get_dependencies, offsetof(Unit, dependencies[UNIT_ON_FAILURE]), SD_BUS_VTABLE_PROPERTY_CONST),
655 SD_BUS_PROPERTY("Triggers", "as", property_get_dependencies, offsetof(Unit, dependencies[UNIT_TRIGGERS]), SD_BUS_VTABLE_PROPERTY_CONST),
656 SD_BUS_PROPERTY("TriggeredBy", "as", property_get_dependencies, offsetof(Unit, dependencies[UNIT_TRIGGERED_BY]), SD_BUS_VTABLE_PROPERTY_CONST),
657 SD_BUS_PROPERTY("PropagatesReloadTo", "as", property_get_dependencies, offsetof(Unit, dependencies[UNIT_PROPAGATES_RELOAD_TO]), SD_BUS_VTABLE_PROPERTY_CONST),
658 SD_BUS_PROPERTY("ReloadPropagatedFrom", "as", property_get_dependencies, offsetof(Unit, dependencies[UNIT_RELOAD_PROPAGATED_FROM]), SD_BUS_VTABLE_PROPERTY_CONST),
659 SD_BUS_PROPERTY("JoinsNamespaceOf", "as", property_get_dependencies, offsetof(Unit, dependencies[UNIT_JOINS_NAMESPACE_OF]), SD_BUS_VTABLE_PROPERTY_CONST),
f32b43bd
LP
660 SD_BUS_PROPERTY("RequiresOverridable", "as", property_get_obsolete_dependencies, 0, SD_BUS_VTABLE_HIDDEN),
661 SD_BUS_PROPERTY("RequisiteOverridable", "as", property_get_obsolete_dependencies, 0, SD_BUS_VTABLE_HIDDEN),
662 SD_BUS_PROPERTY("RequiredByOverridable", "as", property_get_obsolete_dependencies, 0, SD_BUS_VTABLE_HIDDEN),
663 SD_BUS_PROPERTY("RequisiteOfOverridable", "as", property_get_obsolete_dependencies, 0, SD_BUS_VTABLE_HIDDEN),
556089dc
LP
664 SD_BUS_PROPERTY("RequiresMountsFor", "as", NULL, offsetof(Unit, requires_mounts_for), SD_BUS_VTABLE_PROPERTY_CONST),
665 SD_BUS_PROPERTY("Documentation", "as", NULL, offsetof(Unit, documentation), SD_BUS_VTABLE_PROPERTY_CONST),
666 SD_BUS_PROPERTY("Description", "s", property_get_description, 0, SD_BUS_VTABLE_PROPERTY_CONST),
667 SD_BUS_PROPERTY("LoadState", "s", property_get_load_state, offsetof(Unit, load_state), SD_BUS_VTABLE_PROPERTY_CONST),
718db961
LP
668 SD_BUS_PROPERTY("ActiveState", "s", property_get_active_state, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
669 SD_BUS_PROPERTY("SubState", "s", property_get_sub_state, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
556089dc
LP
670 SD_BUS_PROPERTY("FragmentPath", "s", NULL, offsetof(Unit, fragment_path), SD_BUS_VTABLE_PROPERTY_CONST),
671 SD_BUS_PROPERTY("SourcePath", "s", NULL, offsetof(Unit, source_path), SD_BUS_VTABLE_PROPERTY_CONST),
672 SD_BUS_PROPERTY("DropInPaths", "as", NULL, offsetof(Unit, dropin_paths), SD_BUS_VTABLE_PROPERTY_CONST),
718db961 673 SD_BUS_PROPERTY("UnitFileState", "s", property_get_unit_file_state, 0, 0),
d2dc52db 674 SD_BUS_PROPERTY("UnitFilePreset", "s", property_get_unit_file_preset, 0, 0),
a483fb59 675 BUS_PROPERTY_DUAL_TIMESTAMP("StateChangeTimestamp", offsetof(Unit, state_change_timestamp), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
718db961
LP
676 BUS_PROPERTY_DUAL_TIMESTAMP("InactiveExitTimestamp", offsetof(Unit, inactive_exit_timestamp), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
677 BUS_PROPERTY_DUAL_TIMESTAMP("ActiveEnterTimestamp", offsetof(Unit, active_enter_timestamp), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
678 BUS_PROPERTY_DUAL_TIMESTAMP("ActiveExitTimestamp", offsetof(Unit, active_exit_timestamp), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
679 BUS_PROPERTY_DUAL_TIMESTAMP("InactiveEnterTimestamp", offsetof(Unit, inactive_enter_timestamp), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
556089dc
LP
680 SD_BUS_PROPERTY("CanStart", "b", property_get_can_start, 0, SD_BUS_VTABLE_PROPERTY_CONST),
681 SD_BUS_PROPERTY("CanStop", "b", property_get_can_stop, 0, SD_BUS_VTABLE_PROPERTY_CONST),
682 SD_BUS_PROPERTY("CanReload", "b", property_get_can_reload, 0, SD_BUS_VTABLE_PROPERTY_CONST),
683 SD_BUS_PROPERTY("CanIsolate", "b", property_get_can_isolate, 0, SD_BUS_VTABLE_PROPERTY_CONST),
718db961 684 SD_BUS_PROPERTY("Job", "(uo)", property_get_job, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
556089dc
LP
685 SD_BUS_PROPERTY("StopWhenUnneeded", "b", bus_property_get_bool, offsetof(Unit, stop_when_unneeded), SD_BUS_VTABLE_PROPERTY_CONST),
686 SD_BUS_PROPERTY("RefuseManualStart", "b", bus_property_get_bool, offsetof(Unit, refuse_manual_start), SD_BUS_VTABLE_PROPERTY_CONST),
687 SD_BUS_PROPERTY("RefuseManualStop", "b", bus_property_get_bool, offsetof(Unit, refuse_manual_stop), SD_BUS_VTABLE_PROPERTY_CONST),
688 SD_BUS_PROPERTY("AllowIsolate", "b", bus_property_get_bool, offsetof(Unit, allow_isolate), SD_BUS_VTABLE_PROPERTY_CONST),
689 SD_BUS_PROPERTY("DefaultDependencies", "b", bus_property_get_bool, offsetof(Unit, default_dependencies), SD_BUS_VTABLE_PROPERTY_CONST),
690 SD_BUS_PROPERTY("OnFailureJobMode", "s", property_get_job_mode, offsetof(Unit, on_failure_job_mode), SD_BUS_VTABLE_PROPERTY_CONST),
691 SD_BUS_PROPERTY("IgnoreOnIsolate", "b", bus_property_get_bool, offsetof(Unit, ignore_on_isolate), SD_BUS_VTABLE_PROPERTY_CONST),
556089dc
LP
692 SD_BUS_PROPERTY("NeedDaemonReload", "b", property_get_need_daemon_reload, 0, SD_BUS_VTABLE_PROPERTY_CONST),
693 SD_BUS_PROPERTY("JobTimeoutUSec", "t", bus_property_get_usec, offsetof(Unit, job_timeout), SD_BUS_VTABLE_PROPERTY_CONST),
f189ab18
LP
694 SD_BUS_PROPERTY("JobTimeoutAction", "s", property_get_failure_action, offsetof(Unit, job_timeout_action), SD_BUS_VTABLE_PROPERTY_CONST),
695 SD_BUS_PROPERTY("JobTimeoutRebootArgument", "s", NULL, offsetof(Unit, job_timeout_reboot_arg), SD_BUS_VTABLE_PROPERTY_CONST),
718db961 696 SD_BUS_PROPERTY("ConditionResult", "b", bus_property_get_bool, offsetof(Unit, condition_result), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
59fccdc5 697 SD_BUS_PROPERTY("AssertResult", "b", bus_property_get_bool, offsetof(Unit, assert_result), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
718db961 698 BUS_PROPERTY_DUAL_TIMESTAMP("ConditionTimestamp", offsetof(Unit, condition_timestamp), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
59fccdc5
LP
699 BUS_PROPERTY_DUAL_TIMESTAMP("AssertTimestamp", offsetof(Unit, assert_timestamp), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
700 SD_BUS_PROPERTY("Conditions", "a(sbbsi)", property_get_conditions, offsetof(Unit, conditions), 0),
701 SD_BUS_PROPERTY("Asserts", "a(sbbsi)", property_get_conditions, offsetof(Unit, asserts), 0),
556089dc
LP
702 SD_BUS_PROPERTY("LoadError", "(ss)", property_get_load_error, 0, SD_BUS_VTABLE_PROPERTY_CONST),
703 SD_BUS_PROPERTY("Transient", "b", bus_property_get_bool, offsetof(Unit, transient), SD_BUS_VTABLE_PROPERTY_CONST),
6bf0f408
LP
704 SD_BUS_PROPERTY("StartLimitInterval", "t", bus_property_get_usec, offsetof(Unit, start_limit.interval), SD_BUS_VTABLE_PROPERTY_CONST),
705 SD_BUS_PROPERTY("StartLimitBurst", "u", bus_property_get_unsigned, offsetof(Unit, start_limit.burst), SD_BUS_VTABLE_PROPERTY_CONST),
706 SD_BUS_PROPERTY("StartLimitAction", "s", property_get_failure_action, offsetof(Unit, start_limit_action), SD_BUS_VTABLE_PROPERTY_CONST),
707 SD_BUS_PROPERTY("RebootArgument", "s", NULL, offsetof(Unit, reboot_arg), SD_BUS_VTABLE_PROPERTY_CONST),
718db961 708
1d22e906
LP
709 SD_BUS_METHOD("Start", "s", "o", method_start, SD_BUS_VTABLE_UNPRIVILEGED),
710 SD_BUS_METHOD("Stop", "s", "o", method_stop, SD_BUS_VTABLE_UNPRIVILEGED),
711 SD_BUS_METHOD("Reload", "s", "o", method_reload, SD_BUS_VTABLE_UNPRIVILEGED),
712 SD_BUS_METHOD("Restart", "s", "o", method_restart, SD_BUS_VTABLE_UNPRIVILEGED),
713 SD_BUS_METHOD("TryRestart", "s", "o", method_try_restart, SD_BUS_VTABLE_UNPRIVILEGED),
714 SD_BUS_METHOD("ReloadOrRestart", "s", "o", method_reload_or_restart, SD_BUS_VTABLE_UNPRIVILEGED),
715 SD_BUS_METHOD("ReloadOrTryRestart", "s", "o", method_reload_or_try_restart, SD_BUS_VTABLE_UNPRIVILEGED),
716 SD_BUS_METHOD("Kill", "si", NULL, bus_unit_method_kill, SD_BUS_VTABLE_UNPRIVILEGED),
717 SD_BUS_METHOD("ResetFailed", NULL, NULL, bus_unit_method_reset_failed, SD_BUS_VTABLE_UNPRIVILEGED),
718 SD_BUS_METHOD("SetProperties", "ba(sv)", NULL, bus_unit_method_set_properties, SD_BUS_VTABLE_UNPRIVILEGED),
718db961
LP
719
720 SD_BUS_VTABLE_END
721};
2cccbca4 722
718db961
LP
723static int property_get_slice(
724 sd_bus *bus,
725 const char *path,
726 const char *interface,
727 const char *property,
728 sd_bus_message *reply,
ebcf1f97
LP
729 void *userdata,
730 sd_bus_error *error) {
2cccbca4 731
718db961 732 Unit *u = userdata;
2cccbca4 733
718db961
LP
734 assert(bus);
735 assert(reply);
736 assert(u);
2cccbca4 737
718db961
LP
738 return sd_bus_message_append(reply, "s", unit_slice_name(u));
739}
2cccbca4 740
934277fe
LP
741static int property_get_current_memory(
742 sd_bus *bus,
743 const char *path,
744 const char *interface,
745 const char *property,
746 sd_bus_message *reply,
747 void *userdata,
748 sd_bus_error *error) {
749
934277fe 750 uint64_t sz = (uint64_t) -1;
5ad096b3 751 Unit *u = userdata;
934277fe
LP
752 int r;
753
754 assert(bus);
755 assert(reply);
756 assert(u);
757
5ad096b3
LP
758 r = unit_get_memory_current(u, &sz);
759 if (r < 0 && r != -ENODATA)
f2341e0a 760 log_unit_warning_errno(u, r, "Failed to get memory.usage_in_bytes attribute: %m");
934277fe 761
5ad096b3
LP
762 return sd_bus_message_append(reply, "t", sz);
763}
934277fe 764
03a7b521
LP
765static int property_get_current_tasks(
766 sd_bus *bus,
767 const char *path,
768 const char *interface,
769 const char *property,
770 sd_bus_message *reply,
771 void *userdata,
772 sd_bus_error *error) {
773
774 uint64_t cn = (uint64_t) -1;
775 Unit *u = userdata;
776 int r;
777
778 assert(bus);
779 assert(reply);
780 assert(u);
781
782 r = unit_get_tasks_current(u, &cn);
783 if (r < 0 && r != -ENODATA)
784 log_unit_warning_errno(u, r, "Failed to get pids.current attribute: %m");
785
786 return sd_bus_message_append(reply, "t", cn);
787}
788
5ad096b3
LP
789static int property_get_cpu_usage(
790 sd_bus *bus,
791 const char *path,
792 const char *interface,
793 const char *property,
794 sd_bus_message *reply,
795 void *userdata,
796 sd_bus_error *error) {
934277fe 797
5ad096b3
LP
798 nsec_t ns = (nsec_t) -1;
799 Unit *u = userdata;
800 int r;
801
802 assert(bus);
803 assert(reply);
804 assert(u);
805
806 r = unit_get_cpu_usage(u, &ns);
807 if (r < 0 && r != -ENODATA)
f2341e0a 808 log_unit_warning_errno(u, r, "Failed to get cpuacct.usage attribute: %m");
5ad096b3
LP
809
810 return sd_bus_message_append(reply, "t", ns);
934277fe
LP
811}
812
98bac605
LP
813static int property_get_cgroup(
814 sd_bus *bus,
815 const char *path,
816 const char *interface,
817 const char *property,
818 sd_bus_message *reply,
819 void *userdata,
820 sd_bus_error *error) {
821
822 Unit *u = userdata;
823 const char *t;
824
825 assert(bus);
826 assert(reply);
827 assert(u);
828
829 /* Three cases: a) u->cgroup_path is NULL, in which case the
830 * unit has no control group, which we report as the empty
831 * string. b) u->cgroup_path is the empty string, which
832 * indicates the root cgroup, which we report as "/". c) all
833 * other cases we report as-is. */
834
835 if (u->cgroup_path)
836 t = isempty(u->cgroup_path) ? "/" : u->cgroup_path;
837 else
838 t = "";
839
840 return sd_bus_message_append(reply, "s", t);
841}
842
718db961
LP
843const sd_bus_vtable bus_unit_cgroup_vtable[] = {
844 SD_BUS_VTABLE_START(0),
845 SD_BUS_PROPERTY("Slice", "s", property_get_slice, 0, 0),
98bac605 846 SD_BUS_PROPERTY("ControlGroup", "s", property_get_cgroup, 0, 0),
934277fe 847 SD_BUS_PROPERTY("MemoryCurrent", "t", property_get_current_memory, 0, 0),
5ad096b3 848 SD_BUS_PROPERTY("CPUUsageNSec", "t", property_get_cpu_usage, 0, 0),
03a7b521 849 SD_BUS_PROPERTY("TasksCurrent", "t", property_get_current_tasks, 0, 0),
718db961
LP
850 SD_BUS_VTABLE_END
851};
2cccbca4 852
8f8f05a9 853static int send_new_signal(sd_bus *bus, void *userdata) {
4afd3348 854 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
718db961
LP
855 _cleanup_free_ char *p = NULL;
856 Unit *u = userdata;
857 int r;
2cccbca4 858
718db961
LP
859 assert(bus);
860 assert(u);
2cccbca4 861
718db961 862 p = unit_dbus_path(u);
e861098b 863 if (!p)
718db961 864 return -ENOMEM;
2cccbca4 865
718db961
LP
866 r = sd_bus_message_new_signal(
867 bus,
151b9b96 868 &m,
718db961
LP
869 "/org/freedesktop/systemd1",
870 "org.freedesktop.systemd1.Manager",
151b9b96 871 "UnitNew");
718db961
LP
872 if (r < 0)
873 return r;
2cccbca4 874
718db961
LP
875 r = sd_bus_message_append(m, "so", u->id, p);
876 if (r < 0)
877 return r;
2cccbca4 878
8f8f05a9 879 return sd_bus_send(bus, m, NULL);
718db961 880}
2cccbca4 881
8f8f05a9 882static int send_changed_signal(sd_bus *bus, void *userdata) {
718db961
LP
883 _cleanup_free_ char *p = NULL;
884 Unit *u = userdata;
885 int r;
2cccbca4 886
718db961
LP
887 assert(bus);
888 assert(u);
2cccbca4 889
718db961 890 p = unit_dbus_path(u);
9ceefc81 891 if (!p)
718db961 892 return -ENOMEM;
2cccbca4 893
718db961
LP
894 /* Send a properties changed signal. First for the specific
895 * type, then for the generic unit. The clients may rely on
896 * this order to get atomic behavior if needed. */
ea430986 897
aec8de63
LP
898 r = sd_bus_emit_properties_changed_strv(
899 bus, p,
21b735e7 900 unit_dbus_interface_from_type(u->type),
aec8de63 901 NULL);
fe7f06f1 902 if (r < 0)
aec8de63 903 return r;
80fbf05e 904
fe7f06f1 905 return sd_bus_emit_properties_changed_strv(
718db961
LP
906 bus, p,
907 "org.freedesktop.systemd1.Unit",
718db961 908 NULL);
ea430986
LP
909}
910
c1e1601e 911void bus_unit_send_change_signal(Unit *u) {
b170dd80 912 int r;
c1e1601e 913 assert(u);
c1e1601e 914
ac155bb8 915 if (u->in_dbus_queue) {
71fda00f 916 LIST_REMOVE(dbus_queue, u->manager->dbus_unit_queue, u);
ac155bb8 917 u->in_dbus_queue = false;
c0bd0cf7 918 }
c1e1601e 919
ac155bb8 920 if (!u->id)
04ade7d2
LP
921 return;
922
8f8f05a9 923 r = bus_foreach_bus(u->manager, NULL, u->sent_dbus_new_signal ? send_changed_signal : send_new_signal, u);
718db961 924 if (r < 0)
f2341e0a 925 log_unit_debug_errno(u, r, "Failed to send unit change signal for %s: %m", u->id);
c1e1601e 926
718db961
LP
927 u->sent_dbus_new_signal = true;
928}
c4e2ceae 929
8f8f05a9 930static int send_removed_signal(sd_bus *bus, void *userdata) {
4afd3348 931 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
718db961
LP
932 _cleanup_free_ char *p = NULL;
933 Unit *u = userdata;
934 int r;
c4e2ceae 935
718db961
LP
936 assert(bus);
937 assert(u);
c1e1601e 938
718db961 939 p = unit_dbus_path(u);
e861098b 940 if (!p)
718db961 941 return -ENOMEM;
c1e1601e 942
718db961
LP
943 r = sd_bus_message_new_signal(
944 bus,
151b9b96 945 &m,
718db961
LP
946 "/org/freedesktop/systemd1",
947 "org.freedesktop.systemd1.Manager",
151b9b96 948 "UnitRemoved");
718db961
LP
949 if (r < 0)
950 return r;
c1e1601e 951
718db961
LP
952 r = sd_bus_message_append(m, "so", u->id, p);
953 if (r < 0)
954 return r;
c1e1601e 955
8f8f05a9 956 return sd_bus_send(bus, m, NULL);
c1e1601e
LP
957}
958
959void bus_unit_send_removed_signal(Unit *u) {
718db961 960 int r;
c1e1601e
LP
961 assert(u);
962
ac155bb8 963 if (!u->sent_dbus_new_signal)
7535cc78
LP
964 bus_unit_send_change_signal(u);
965
ac155bb8 966 if (!u->id)
04ade7d2
LP
967 return;
968
8f8f05a9 969 r = bus_foreach_bus(u->manager, NULL, send_removed_signal, u);
718db961 970 if (r < 0)
f2341e0a 971 log_unit_debug_errno(u, r, "Failed to send unit remove signal for %s: %m", u->id);
c1e1601e 972}
e2110e5d 973
718db961 974int bus_unit_queue_job(
718db961 975 sd_bus_message *message,
cad45ba1
LP
976 Unit *u,
977 JobType type,
978 JobMode mode,
ebcf1f97
LP
979 bool reload_if_possible,
980 sd_bus_error *error) {
cad45ba1 981
cad45ba1
LP
982 _cleanup_free_ char *path = NULL;
983 Job *j;
cad45ba1
LP
984 int r;
985
cad45ba1
LP
986 assert(message);
987 assert(u);
988 assert(type >= 0 && type < _JOB_TYPE_MAX);
989 assert(mode >= 0 && mode < _JOB_MODE_MAX);
990
f596e00f
EV
991 r = mac_selinux_unit_access_check(
992 u, message,
993 job_type_to_access_method(type),
994 error);
995 if (r < 0)
996 return r;
997
cad45ba1
LP
998 if (reload_if_possible && unit_can_reload(u)) {
999 if (type == JOB_RESTART)
1000 type = JOB_RELOAD_OR_START;
1001 else if (type == JOB_TRY_RESTART)
3282591d 1002 type = JOB_TRY_RELOAD;
cad45ba1
LP
1003 }
1004
cad45ba1 1005
718db961
LP
1006 if (type == JOB_STOP &&
1007 (u->load_state == UNIT_NOT_FOUND || u->load_state == UNIT_ERROR) &&
1008 unit_active_state(u) == UNIT_INACTIVE)
ebcf1f97 1009 return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_UNIT, "Unit %s not loaded.", u->id);
cad45ba1
LP
1010
1011 if ((type == JOB_START && u->refuse_manual_start) ||
1012 (type == JOB_STOP && u->refuse_manual_stop) ||
efb30ba1
EV
1013 ((type == JOB_RESTART || type == JOB_TRY_RESTART) && (u->refuse_manual_start || u->refuse_manual_stop)) ||
1014 (type == JOB_RELOAD_OR_START && job_type_collapse(type, u) == JOB_START && u->refuse_manual_start))
ebcf1f97 1015 return sd_bus_error_setf(error, BUS_ERROR_ONLY_BY_DEPENDENCY, "Operation refused, unit %s may be requested by dependency only.", u->id);
cad45ba1 1016
4bd29fe5 1017 r = manager_add_job(u->manager, type, u, mode, error, &j);
cad45ba1 1018 if (r < 0)
ebcf1f97 1019 return r;
cad45ba1 1020
19070062 1021 if (sd_bus_message_get_bus(message) == u->manager->api_bus) {
b39a2770 1022 if (!j->clients) {
19070062 1023 r = sd_bus_track_new(sd_bus_message_get_bus(message), &j->clients, NULL, NULL);
8f8f05a9
LP
1024 if (r < 0)
1025 return r;
1026 }
1027
b39a2770 1028 r = sd_bus_track_add_sender(j->clients, message);
8f8f05a9
LP
1029 if (r < 0)
1030 return r;
1031 }
cad45ba1
LP
1032
1033 path = job_dbus_path(j);
1034 if (!path)
6ce270b1 1035 return -ENOMEM;
cad45ba1 1036
df2d202e 1037 return sd_bus_reply_method_return(message, "o", path);
cad45ba1
LP
1038}
1039
9f2e86af
LP
1040static int bus_unit_set_transient_property(
1041 Unit *u,
1042 const char *name,
718db961 1043 sd_bus_message *message,
9f2e86af 1044 UnitSetPropertiesMode mode,
718db961 1045 sd_bus_error *error) {
9f2e86af
LP
1046
1047 int r;
1048
1049 assert(u);
1050 assert(name);
718db961 1051 assert(message);
9f2e86af
LP
1052
1053 if (streq(name, "Description")) {
718db961 1054 const char *d;
9f2e86af 1055
718db961
LP
1056 r = sd_bus_message_read(message, "s", &d);
1057 if (r < 0)
1058 return r;
8aec412f 1059
718db961
LP
1060 if (mode != UNIT_CHECK) {
1061 r = unit_set_description(u, d);
8aec412f
LP
1062 if (r < 0)
1063 return r;
b9316fb0 1064
718db961 1065 unit_write_drop_in_format(u, mode, name, "[Unit]\nDescription=%s\n", d);
8aec412f 1066 }
9f2e86af
LP
1067
1068 return 1;
261420ba
LP
1069
1070 } else if (streq(name, "DefaultDependencies")) {
1071 int b;
1072
1073 r = sd_bus_message_read(message, "b", &b);
1074 if (r < 0)
1075 return r;
1076
1077 if (mode != UNIT_CHECK) {
1078 u->default_dependencies = b;
1079 unit_write_drop_in_format(u, mode, name, "[Unit]\nDefaultDependencies=%s\n", yes_no(b));
1080 }
1081
1082 return 1;
c221420b 1083
d79200e2
LP
1084 } else if (streq(name, "Slice")) {
1085 Unit *slice;
c221420b 1086 const char *s;
c221420b 1087
d79200e2
LP
1088 if (!UNIT_HAS_CGROUP_CONTEXT(u))
1089 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "The slice property is only available for units with control groups.");
1090 if (u->type == UNIT_SLICE)
1091 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Slice may not be set for slice units.");
efdb0237
LP
1092 if (unit_has_name(u, SPECIAL_INIT_SCOPE))
1093 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Cannot set slice for init.scope");
d79200e2 1094
718db961
LP
1095 r = sd_bus_message_read(message, "s", &s);
1096 if (r < 0)
1097 return r;
c221420b 1098
d79200e2
LP
1099 if (!unit_name_is_valid(s, UNIT_NAME_PLAIN))
1100 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid unit name '%s'", s);
1101
1102 r = manager_load_unit(u->manager, s, NULL, error, &slice);
1103 if (r < 0)
1104 return r;
c221420b 1105
d79200e2
LP
1106 if (slice->type != UNIT_SLICE)
1107 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Unit name '%s' is not a slice", s);
b9316fb0 1108
d79200e2
LP
1109 if (mode != UNIT_CHECK) {
1110 r = unit_set_slice(u, slice);
8aec412f
LP
1111 if (r < 0)
1112 return r;
c221420b 1113
d79200e2 1114 unit_write_drop_in_private_format(u, mode, name, "Slice=%s\n", s);
8aec412f 1115 }
b9ec9359 1116
c221420b 1117 return 1;
d79200e2 1118
311f6cf3
WC
1119 } else if (STR_IN_SET(name,
1120 "Requires", "RequiresOverridable",
1121 "Requisite", "RequisiteOverridable",
1122 "Wants",
1123 "BindsTo",
1124 "Conflicts",
1125 "Before", "After",
1126 "OnFailure",
1127 "PropagatesReloadTo", "ReloadPropagatedFrom",
1128 "PartOf")) {
7fb3ee51
LP
1129
1130 UnitDependency d;
718db961 1131 const char *other;
7fb3ee51 1132
f32b43bd
LP
1133 if (streq(name, "RequiresOverridable"))
1134 d = UNIT_REQUIRES; /* redirect for obsolete unit dependency type */
1135 else if (streq(name, "RequisiteOverridable"))
1136 d = UNIT_REQUISITE; /* same here */
1137 else {
1138 d = unit_dependency_from_string(name);
1139 if (d < 0)
1140 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid unit dependency: %s", name);
1141 }
7fb3ee51 1142
718db961
LP
1143 r = sd_bus_message_enter_container(message, 'a', "s");
1144 if (r < 0)
1145 return r;
7fb3ee51 1146
718db961 1147 while ((r = sd_bus_message_read(message, "s", &other)) > 0) {
7410616c 1148 if (!unit_name_is_valid(other, UNIT_NAME_PLAIN|UNIT_NAME_INSTANCE))
718db961 1149 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid unit name %s", other);
7fb3ee51
LP
1150
1151 if (mode != UNIT_CHECK) {
b9ec9359 1152 _cleanup_free_ char *label = NULL;
7fb3ee51
LP
1153
1154 r = unit_add_dependency_by_name(u, d, other, NULL, true);
1155 if (r < 0)
1156 return r;
1157
1158 label = strjoin(name, "-", other, NULL);
1159 if (!label)
1160 return -ENOMEM;
1161
b9ec9359 1162 unit_write_drop_in_format(u, mode, label, "[Unit]\n%s=%s\n", name, other);
7fb3ee51
LP
1163 }
1164
7fb3ee51 1165 }
718db961
LP
1166 if (r < 0)
1167 return r;
7fb3ee51 1168
6ce270b1
LP
1169 r = sd_bus_message_exit_container(message);
1170 if (r < 0)
1171 return r;
1172
7fb3ee51 1173 return 1;
9f2e86af
LP
1174 }
1175
1176 return 0;
1177}
1178
c2756a68
LP
1179int bus_unit_set_properties(
1180 Unit *u,
718db961 1181 sd_bus_message *message,
c2756a68
LP
1182 UnitSetPropertiesMode mode,
1183 bool commit,
718db961 1184 sd_bus_error *error) {
c2756a68 1185
8e2af478 1186 bool for_real = false;
8e2af478
LP
1187 unsigned n = 0;
1188 int r;
1189
1190 assert(u);
718db961 1191 assert(message);
8e2af478
LP
1192
1193 /* We iterate through the array twice. First run we just check
1194 * if all passed data is valid, second run actually applies
1195 * it. This is to implement transaction-like behaviour without
1196 * actually providing full transactions. */
1197
718db961
LP
1198 r = sd_bus_message_enter_container(message, 'a', "(sv)");
1199 if (r < 0)
1200 return r;
8e2af478 1201
8e2af478 1202 for (;;) {
8e2af478
LP
1203 const char *name;
1204
718db961
LP
1205 r = sd_bus_message_enter_container(message, 'r', "sv");
1206 if (r < 0)
1207 return r;
1208 if (r == 0) {
a255a7f1 1209 if (for_real || mode == UNIT_CHECK)
8e2af478
LP
1210 break;
1211
1212 /* Reached EOF. Let's try again, and this time for realz... */
718db961
LP
1213 r = sd_bus_message_rewind(message, false);
1214 if (r < 0)
1215 return r;
6ce270b1 1216
8e2af478
LP
1217 for_real = true;
1218 continue;
1219 }
1220
718db961
LP
1221 r = sd_bus_message_read(message, "s", &name);
1222 if (r < 0)
1223 return r;
8e2af478 1224
718db961
LP
1225 if (!UNIT_VTABLE(u)->bus_set_property)
1226 return sd_bus_error_setf(error, SD_BUS_ERROR_PROPERTY_READ_ONLY, "Objects of this type do not support setting properties.");
8e2af478 1227
718db961
LP
1228 r = sd_bus_message_enter_container(message, 'v', NULL);
1229 if (r < 0)
1230 return r;
8e2af478 1231
718db961 1232 r = UNIT_VTABLE(u)->bus_set_property(u, name, message, for_real ? mode : UNIT_CHECK, error);
9f2e86af 1233 if (r == 0 && u->transient && u->load_state == UNIT_STUB)
718db961
LP
1234 r = bus_unit_set_transient_property(u, name, message, for_real ? mode : UNIT_CHECK, error);
1235 if (r < 0)
1236 return r;
1237 if (r == 0)
1238 return sd_bus_error_setf(error, SD_BUS_ERROR_PROPERTY_READ_ONLY, "Cannot set property %s, or unknown property.", name);
1239
1240 r = sd_bus_message_exit_container(message);
8e2af478
LP
1241 if (r < 0)
1242 return r;
8e2af478 1243
718db961
LP
1244 r = sd_bus_message_exit_container(message);
1245 if (r < 0)
1246 return r;
8e2af478
LP
1247
1248 n += for_real;
1249 }
1250
6ce270b1
LP
1251 r = sd_bus_message_exit_container(message);
1252 if (r < 0)
1253 return r;
1254
c2756a68 1255 if (commit && n > 0 && UNIT_VTABLE(u)->bus_commit_properties)
8e2af478
LP
1256 UNIT_VTABLE(u)->bus_commit_properties(u);
1257
241da328 1258 return n;
8e2af478 1259}
000a996d
FB
1260
1261int bus_unit_check_load_state(Unit *u, sd_bus_error *error) {
1262
1263 if (u->load_state == UNIT_LOADED)
1264 return 0;
1265
1266 /* Give a better description of the unit error when
1267 * possible. Note that in the case of UNIT_MASKED, load_error
1268 * is not set. */
1269 if (u->load_state == UNIT_MASKED)
b21d2f81 1270 return sd_bus_error_setf(error, BUS_ERROR_UNIT_MASKED, "Unit %s is masked.", u->id);
000a996d
FB
1271
1272 if (u->load_state == UNIT_NOT_FOUND)
b21d2f81 1273 return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_UNIT, "Unit %s not found.", u->id);
000a996d 1274
b21d2f81 1275 return sd_bus_error_set_errnof(error, u->load_error, "Unit %s is not loaded properly: %m.", u->id);
000a996d 1276}