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