]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/core/dbus-unit.c
Merge pull request #4459 from keszybz/commandline-parsing
[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_emergency_action, emergency_action, EmergencyAction);
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 bool interactive,
422 sd_bus_message *call,
423 sd_bus_error *error) {
424
425 const char *details[9] = {
426 "unit", u->id,
427 "verb", verb,
428 };
429
430 if (polkit_message) {
431 details[4] = "polkit.message";
432 details[5] = polkit_message;
433 details[6] = "polkit.gettext_domain";
434 details[7] = GETTEXT_PACKAGE;
435 }
436
437 return bus_verify_polkit_async(
438 call,
439 capability,
440 "org.freedesktop.systemd1.manage-units",
441 details,
442 interactive,
443 UID_INVALID,
444 &u->manager->polkit_registry,
445 error);
446 }
447
448 int bus_unit_method_start_generic(
449 sd_bus_message *message,
450 Unit *u,
451 JobType job_type,
452 bool reload_if_possible,
453 sd_bus_error *error) {
454
455 const char *smode;
456 JobMode mode;
457 _cleanup_free_ char *verb = NULL;
458 static const char *const polkit_message_for_job[_JOB_TYPE_MAX] = {
459 [JOB_START] = N_("Authentication is required to start '$(unit)'."),
460 [JOB_STOP] = N_("Authentication is required to stop '$(unit)'."),
461 [JOB_RELOAD] = N_("Authentication is required to reload '$(unit)'."),
462 [JOB_RESTART] = N_("Authentication is required to restart '$(unit)'."),
463 [JOB_TRY_RESTART] = N_("Authentication is required to restart '$(unit)'."),
464 };
465 int r;
466
467 assert(message);
468 assert(u);
469 assert(job_type >= 0 && job_type < _JOB_TYPE_MAX);
470
471 r = mac_selinux_unit_access_check(
472 u, message,
473 job_type_to_access_method(job_type),
474 error);
475 if (r < 0)
476 return r;
477
478 r = sd_bus_message_read(message, "s", &smode);
479 if (r < 0)
480 return r;
481
482 mode = job_mode_from_string(smode);
483 if (mode < 0)
484 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Job mode %s invalid", smode);
485
486 if (reload_if_possible)
487 verb = strjoin("reload-or-", job_type_to_string(job_type), NULL);
488 else
489 verb = strdup(job_type_to_string(job_type));
490 if (!verb)
491 return -ENOMEM;
492
493 r = bus_verify_manage_units_async_full(
494 u,
495 verb,
496 CAP_SYS_ADMIN,
497 job_type < _JOB_TYPE_MAX ? polkit_message_for_job[job_type] : NULL,
498 true,
499 message,
500 error);
501 if (r < 0)
502 return r;
503 if (r == 0)
504 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
505
506 return bus_unit_queue_job(message, u, job_type, mode, reload_if_possible, error);
507 }
508
509 static int method_start(sd_bus_message *message, void *userdata, sd_bus_error *error) {
510 return bus_unit_method_start_generic(message, userdata, JOB_START, false, error);
511 }
512
513 static int method_stop(sd_bus_message *message, void *userdata, sd_bus_error *error) {
514 return bus_unit_method_start_generic(message, userdata, JOB_STOP, false, error);
515 }
516
517 static int method_reload(sd_bus_message *message, void *userdata, sd_bus_error *error) {
518 return bus_unit_method_start_generic(message, userdata, JOB_RELOAD, false, error);
519 }
520
521 static int method_restart(sd_bus_message *message, void *userdata, sd_bus_error *error) {
522 return bus_unit_method_start_generic(message, userdata, JOB_RESTART, false, error);
523 }
524
525 static int method_try_restart(sd_bus_message *message, void *userdata, sd_bus_error *error) {
526 return bus_unit_method_start_generic(message, userdata, JOB_TRY_RESTART, false, error);
527 }
528
529 static int method_reload_or_restart(sd_bus_message *message, void *userdata, sd_bus_error *error) {
530 return bus_unit_method_start_generic(message, userdata, JOB_RESTART, true, error);
531 }
532
533 static int method_reload_or_try_restart(sd_bus_message *message, void *userdata, sd_bus_error *error) {
534 return bus_unit_method_start_generic(message, userdata, JOB_TRY_RESTART, true, error);
535 }
536
537 int bus_unit_method_kill(sd_bus_message *message, void *userdata, sd_bus_error *error) {
538 Unit *u = userdata;
539 const char *swho;
540 int32_t signo;
541 KillWho who;
542 int r;
543
544 assert(message);
545 assert(u);
546
547 r = mac_selinux_unit_access_check(u, message, "stop", error);
548 if (r < 0)
549 return r;
550
551 r = sd_bus_message_read(message, "si", &swho, &signo);
552 if (r < 0)
553 return r;
554
555 if (isempty(swho))
556 who = KILL_ALL;
557 else {
558 who = kill_who_from_string(swho);
559 if (who < 0)
560 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid who argument %s", swho);
561 }
562
563 if (!SIGNAL_VALID(signo))
564 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Signal number out of range.");
565
566 r = bus_verify_manage_units_async_full(
567 u,
568 "kill",
569 CAP_KILL,
570 N_("Authentication is required to kill '$(unit)'."),
571 true,
572 message,
573 error);
574 if (r < 0)
575 return r;
576 if (r == 0)
577 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
578
579 r = unit_kill(u, who, signo, error);
580 if (r < 0)
581 return r;
582
583 return sd_bus_reply_method_return(message, NULL);
584 }
585
586 int bus_unit_method_reset_failed(sd_bus_message *message, void *userdata, sd_bus_error *error) {
587 Unit *u = userdata;
588 int r;
589
590 assert(message);
591 assert(u);
592
593 r = mac_selinux_unit_access_check(u, message, "reload", error);
594 if (r < 0)
595 return r;
596
597 r = bus_verify_manage_units_async_full(
598 u,
599 "reset-failed",
600 CAP_SYS_ADMIN,
601 N_("Authentication is required to reset the \"failed\" state of '$(unit)'."),
602 true,
603 message,
604 error);
605 if (r < 0)
606 return r;
607 if (r == 0)
608 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
609
610 unit_reset_failed(u);
611
612 return sd_bus_reply_method_return(message, NULL);
613 }
614
615 int bus_unit_method_set_properties(sd_bus_message *message, void *userdata, sd_bus_error *error) {
616 Unit *u = userdata;
617 int runtime, r;
618
619 assert(message);
620 assert(u);
621
622 r = mac_selinux_unit_access_check(u, message, "start", error);
623 if (r < 0)
624 return r;
625
626 r = sd_bus_message_read(message, "b", &runtime);
627 if (r < 0)
628 return r;
629
630 r = bus_verify_manage_units_async_full(
631 u,
632 "set-property",
633 CAP_SYS_ADMIN,
634 N_("Authentication is required to set properties on '$(unit)'."),
635 true,
636 message,
637 error);
638 if (r < 0)
639 return r;
640 if (r == 0)
641 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
642
643 r = bus_unit_set_properties(u, message, runtime ? UNIT_RUNTIME : UNIT_PERSISTENT, true, error);
644 if (r < 0)
645 return r;
646
647 return sd_bus_reply_method_return(message, NULL);
648 }
649
650 int bus_unit_method_ref(sd_bus_message *message, void *userdata, sd_bus_error *error) {
651 Unit *u = userdata;
652 int r;
653
654 assert(message);
655 assert(u);
656
657 r = mac_selinux_unit_access_check(u, message, "start", error);
658 if (r < 0)
659 return r;
660
661 r = bus_verify_manage_units_async_full(
662 u,
663 "ref",
664 CAP_SYS_ADMIN,
665 NULL,
666 false,
667 message,
668 error);
669 if (r < 0)
670 return r;
671 if (r == 0)
672 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
673
674 r = bus_unit_track_add_sender(u, message);
675 if (r < 0)
676 return r;
677
678 return sd_bus_reply_method_return(message, NULL);
679 }
680
681 int bus_unit_method_unref(sd_bus_message *message, void *userdata, sd_bus_error *error) {
682 Unit *u = userdata;
683 int r;
684
685 assert(message);
686 assert(u);
687
688 r = bus_unit_track_remove_sender(u, message);
689 if (r == -EUNATCH)
690 return sd_bus_error_setf(error, BUS_ERROR_NOT_REFERENCED, "Unit has not been referenced yet.");
691 if (r < 0)
692 return r;
693
694 return sd_bus_reply_method_return(message, NULL);
695 }
696
697 const sd_bus_vtable bus_unit_vtable[] = {
698 SD_BUS_VTABLE_START(0),
699
700 SD_BUS_PROPERTY("Id", "s", NULL, offsetof(Unit, id), SD_BUS_VTABLE_PROPERTY_CONST),
701 SD_BUS_PROPERTY("Names", "as", property_get_names, 0, SD_BUS_VTABLE_PROPERTY_CONST),
702 SD_BUS_PROPERTY("Following", "s", property_get_following, 0, 0),
703 SD_BUS_PROPERTY("Requires", "as", property_get_dependencies, offsetof(Unit, dependencies[UNIT_REQUIRES]), SD_BUS_VTABLE_PROPERTY_CONST),
704 SD_BUS_PROPERTY("Requisite", "as", property_get_dependencies, offsetof(Unit, dependencies[UNIT_REQUISITE]), SD_BUS_VTABLE_PROPERTY_CONST),
705 SD_BUS_PROPERTY("Wants", "as", property_get_dependencies, offsetof(Unit, dependencies[UNIT_WANTS]), SD_BUS_VTABLE_PROPERTY_CONST),
706 SD_BUS_PROPERTY("BindsTo", "as", property_get_dependencies, offsetof(Unit, dependencies[UNIT_BINDS_TO]), SD_BUS_VTABLE_PROPERTY_CONST),
707 SD_BUS_PROPERTY("PartOf", "as", property_get_dependencies, offsetof(Unit, dependencies[UNIT_PART_OF]), SD_BUS_VTABLE_PROPERTY_CONST),
708 SD_BUS_PROPERTY("RequiredBy", "as", property_get_dependencies, offsetof(Unit, dependencies[UNIT_REQUIRED_BY]), SD_BUS_VTABLE_PROPERTY_CONST),
709 SD_BUS_PROPERTY("RequisiteOf", "as", property_get_dependencies, offsetof(Unit, dependencies[UNIT_REQUISITE_OF]), SD_BUS_VTABLE_PROPERTY_CONST),
710 SD_BUS_PROPERTY("WantedBy", "as", property_get_dependencies, offsetof(Unit, dependencies[UNIT_WANTED_BY]), SD_BUS_VTABLE_PROPERTY_CONST),
711 SD_BUS_PROPERTY("BoundBy", "as", property_get_dependencies, offsetof(Unit, dependencies[UNIT_BOUND_BY]), SD_BUS_VTABLE_PROPERTY_CONST),
712 SD_BUS_PROPERTY("ConsistsOf", "as", property_get_dependencies, offsetof(Unit, dependencies[UNIT_CONSISTS_OF]), SD_BUS_VTABLE_PROPERTY_CONST),
713 SD_BUS_PROPERTY("Conflicts", "as", property_get_dependencies, offsetof(Unit, dependencies[UNIT_CONFLICTS]), SD_BUS_VTABLE_PROPERTY_CONST),
714 SD_BUS_PROPERTY("ConflictedBy", "as", property_get_dependencies, offsetof(Unit, dependencies[UNIT_CONFLICTED_BY]), SD_BUS_VTABLE_PROPERTY_CONST),
715 SD_BUS_PROPERTY("Before", "as", property_get_dependencies, offsetof(Unit, dependencies[UNIT_BEFORE]), SD_BUS_VTABLE_PROPERTY_CONST),
716 SD_BUS_PROPERTY("After", "as", property_get_dependencies, offsetof(Unit, dependencies[UNIT_AFTER]), SD_BUS_VTABLE_PROPERTY_CONST),
717 SD_BUS_PROPERTY("OnFailure", "as", property_get_dependencies, offsetof(Unit, dependencies[UNIT_ON_FAILURE]), SD_BUS_VTABLE_PROPERTY_CONST),
718 SD_BUS_PROPERTY("Triggers", "as", property_get_dependencies, offsetof(Unit, dependencies[UNIT_TRIGGERS]), SD_BUS_VTABLE_PROPERTY_CONST),
719 SD_BUS_PROPERTY("TriggeredBy", "as", property_get_dependencies, offsetof(Unit, dependencies[UNIT_TRIGGERED_BY]), SD_BUS_VTABLE_PROPERTY_CONST),
720 SD_BUS_PROPERTY("PropagatesReloadTo", "as", property_get_dependencies, offsetof(Unit, dependencies[UNIT_PROPAGATES_RELOAD_TO]), SD_BUS_VTABLE_PROPERTY_CONST),
721 SD_BUS_PROPERTY("ReloadPropagatedFrom", "as", property_get_dependencies, offsetof(Unit, dependencies[UNIT_RELOAD_PROPAGATED_FROM]), SD_BUS_VTABLE_PROPERTY_CONST),
722 SD_BUS_PROPERTY("JoinsNamespaceOf", "as", property_get_dependencies, offsetof(Unit, dependencies[UNIT_JOINS_NAMESPACE_OF]), SD_BUS_VTABLE_PROPERTY_CONST),
723 SD_BUS_PROPERTY("RequiresMountsFor", "as", NULL, offsetof(Unit, requires_mounts_for), SD_BUS_VTABLE_PROPERTY_CONST),
724 SD_BUS_PROPERTY("Documentation", "as", NULL, offsetof(Unit, documentation), SD_BUS_VTABLE_PROPERTY_CONST),
725 SD_BUS_PROPERTY("Description", "s", property_get_description, 0, SD_BUS_VTABLE_PROPERTY_CONST),
726 SD_BUS_PROPERTY("LoadState", "s", property_get_load_state, offsetof(Unit, load_state), SD_BUS_VTABLE_PROPERTY_CONST),
727 SD_BUS_PROPERTY("ActiveState", "s", property_get_active_state, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
728 SD_BUS_PROPERTY("SubState", "s", property_get_sub_state, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
729 SD_BUS_PROPERTY("FragmentPath", "s", NULL, offsetof(Unit, fragment_path), SD_BUS_VTABLE_PROPERTY_CONST),
730 SD_BUS_PROPERTY("SourcePath", "s", NULL, offsetof(Unit, source_path), SD_BUS_VTABLE_PROPERTY_CONST),
731 SD_BUS_PROPERTY("DropInPaths", "as", NULL, offsetof(Unit, dropin_paths), SD_BUS_VTABLE_PROPERTY_CONST),
732 SD_BUS_PROPERTY("UnitFileState", "s", property_get_unit_file_state, 0, 0),
733 SD_BUS_PROPERTY("UnitFilePreset", "s", property_get_unit_file_preset, 0, 0),
734 BUS_PROPERTY_DUAL_TIMESTAMP("StateChangeTimestamp", offsetof(Unit, state_change_timestamp), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
735 BUS_PROPERTY_DUAL_TIMESTAMP("InactiveExitTimestamp", offsetof(Unit, inactive_exit_timestamp), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
736 BUS_PROPERTY_DUAL_TIMESTAMP("ActiveEnterTimestamp", offsetof(Unit, active_enter_timestamp), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
737 BUS_PROPERTY_DUAL_TIMESTAMP("ActiveExitTimestamp", offsetof(Unit, active_exit_timestamp), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
738 BUS_PROPERTY_DUAL_TIMESTAMP("InactiveEnterTimestamp", offsetof(Unit, inactive_enter_timestamp), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
739 SD_BUS_PROPERTY("CanStart", "b", property_get_can_start, 0, SD_BUS_VTABLE_PROPERTY_CONST),
740 SD_BUS_PROPERTY("CanStop", "b", property_get_can_stop, 0, SD_BUS_VTABLE_PROPERTY_CONST),
741 SD_BUS_PROPERTY("CanReload", "b", property_get_can_reload, 0, SD_BUS_VTABLE_PROPERTY_CONST),
742 SD_BUS_PROPERTY("CanIsolate", "b", property_get_can_isolate, 0, SD_BUS_VTABLE_PROPERTY_CONST),
743 SD_BUS_PROPERTY("Job", "(uo)", property_get_job, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
744 SD_BUS_PROPERTY("StopWhenUnneeded", "b", bus_property_get_bool, offsetof(Unit, stop_when_unneeded), SD_BUS_VTABLE_PROPERTY_CONST),
745 SD_BUS_PROPERTY("RefuseManualStart", "b", bus_property_get_bool, offsetof(Unit, refuse_manual_start), SD_BUS_VTABLE_PROPERTY_CONST),
746 SD_BUS_PROPERTY("RefuseManualStop", "b", bus_property_get_bool, offsetof(Unit, refuse_manual_stop), SD_BUS_VTABLE_PROPERTY_CONST),
747 SD_BUS_PROPERTY("AllowIsolate", "b", bus_property_get_bool, offsetof(Unit, allow_isolate), SD_BUS_VTABLE_PROPERTY_CONST),
748 SD_BUS_PROPERTY("DefaultDependencies", "b", bus_property_get_bool, offsetof(Unit, default_dependencies), SD_BUS_VTABLE_PROPERTY_CONST),
749 SD_BUS_PROPERTY("OnFailureJobMode", "s", property_get_job_mode, offsetof(Unit, on_failure_job_mode), SD_BUS_VTABLE_PROPERTY_CONST),
750 SD_BUS_PROPERTY("IgnoreOnIsolate", "b", bus_property_get_bool, offsetof(Unit, ignore_on_isolate), SD_BUS_VTABLE_PROPERTY_CONST),
751 SD_BUS_PROPERTY("NeedDaemonReload", "b", property_get_need_daemon_reload, 0, SD_BUS_VTABLE_PROPERTY_CONST),
752 SD_BUS_PROPERTY("JobTimeoutUSec", "t", bus_property_get_usec, offsetof(Unit, job_timeout), SD_BUS_VTABLE_PROPERTY_CONST),
753 SD_BUS_PROPERTY("JobTimeoutAction", "s", property_get_emergency_action, offsetof(Unit, job_timeout_action), SD_BUS_VTABLE_PROPERTY_CONST),
754 SD_BUS_PROPERTY("JobTimeoutRebootArgument", "s", NULL, offsetof(Unit, job_timeout_reboot_arg), SD_BUS_VTABLE_PROPERTY_CONST),
755 SD_BUS_PROPERTY("ConditionResult", "b", bus_property_get_bool, offsetof(Unit, condition_result), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
756 SD_BUS_PROPERTY("AssertResult", "b", bus_property_get_bool, offsetof(Unit, assert_result), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
757 BUS_PROPERTY_DUAL_TIMESTAMP("ConditionTimestamp", offsetof(Unit, condition_timestamp), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
758 BUS_PROPERTY_DUAL_TIMESTAMP("AssertTimestamp", offsetof(Unit, assert_timestamp), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
759 SD_BUS_PROPERTY("Conditions", "a(sbbsi)", property_get_conditions, offsetof(Unit, conditions), 0),
760 SD_BUS_PROPERTY("Asserts", "a(sbbsi)", property_get_conditions, offsetof(Unit, asserts), 0),
761 SD_BUS_PROPERTY("LoadError", "(ss)", property_get_load_error, 0, SD_BUS_VTABLE_PROPERTY_CONST),
762 SD_BUS_PROPERTY("Transient", "b", bus_property_get_bool, offsetof(Unit, transient), SD_BUS_VTABLE_PROPERTY_CONST),
763 SD_BUS_PROPERTY("StartLimitIntervalSec", "t", bus_property_get_usec, offsetof(Unit, start_limit.interval), SD_BUS_VTABLE_PROPERTY_CONST),
764 SD_BUS_PROPERTY("StartLimitBurst", "u", bus_property_get_unsigned, offsetof(Unit, start_limit.burst), SD_BUS_VTABLE_PROPERTY_CONST),
765 SD_BUS_PROPERTY("StartLimitAction", "s", property_get_emergency_action, offsetof(Unit, start_limit_action), SD_BUS_VTABLE_PROPERTY_CONST),
766 SD_BUS_PROPERTY("RebootArgument", "s", NULL, offsetof(Unit, reboot_arg), SD_BUS_VTABLE_PROPERTY_CONST),
767 SD_BUS_PROPERTY("InvocationID", "ay", bus_property_get_id128, offsetof(Unit, invocation_id), 0),
768
769 SD_BUS_METHOD("Start", "s", "o", method_start, SD_BUS_VTABLE_UNPRIVILEGED),
770 SD_BUS_METHOD("Stop", "s", "o", method_stop, SD_BUS_VTABLE_UNPRIVILEGED),
771 SD_BUS_METHOD("Reload", "s", "o", method_reload, SD_BUS_VTABLE_UNPRIVILEGED),
772 SD_BUS_METHOD("Restart", "s", "o", method_restart, SD_BUS_VTABLE_UNPRIVILEGED),
773 SD_BUS_METHOD("TryRestart", "s", "o", method_try_restart, SD_BUS_VTABLE_UNPRIVILEGED),
774 SD_BUS_METHOD("ReloadOrRestart", "s", "o", method_reload_or_restart, SD_BUS_VTABLE_UNPRIVILEGED),
775 SD_BUS_METHOD("ReloadOrTryRestart", "s", "o", method_reload_or_try_restart, SD_BUS_VTABLE_UNPRIVILEGED),
776 SD_BUS_METHOD("Kill", "si", NULL, bus_unit_method_kill, SD_BUS_VTABLE_UNPRIVILEGED),
777 SD_BUS_METHOD("ResetFailed", NULL, NULL, bus_unit_method_reset_failed, SD_BUS_VTABLE_UNPRIVILEGED),
778 SD_BUS_METHOD("SetProperties", "ba(sv)", NULL, bus_unit_method_set_properties, SD_BUS_VTABLE_UNPRIVILEGED),
779 SD_BUS_METHOD("Ref", NULL, NULL, bus_unit_method_ref, SD_BUS_VTABLE_UNPRIVILEGED),
780 SD_BUS_METHOD("Unref", NULL, NULL, bus_unit_method_unref, SD_BUS_VTABLE_UNPRIVILEGED),
781
782 /* Obsolete properties or obsolete alias names */
783 SD_BUS_PROPERTY("RequiresOverridable", "as", property_get_obsolete_dependencies, 0, SD_BUS_VTABLE_HIDDEN),
784 SD_BUS_PROPERTY("RequisiteOverridable", "as", property_get_obsolete_dependencies, 0, SD_BUS_VTABLE_HIDDEN),
785 SD_BUS_PROPERTY("RequiredByOverridable", "as", property_get_obsolete_dependencies, 0, SD_BUS_VTABLE_HIDDEN),
786 SD_BUS_PROPERTY("RequisiteOfOverridable", "as", property_get_obsolete_dependencies, 0, SD_BUS_VTABLE_HIDDEN),
787 SD_BUS_PROPERTY("StartLimitInterval", "t", bus_property_get_usec, offsetof(Unit, start_limit.interval), SD_BUS_VTABLE_PROPERTY_CONST|SD_BUS_VTABLE_HIDDEN),
788 SD_BUS_VTABLE_END
789 };
790
791 static int property_get_slice(
792 sd_bus *bus,
793 const char *path,
794 const char *interface,
795 const char *property,
796 sd_bus_message *reply,
797 void *userdata,
798 sd_bus_error *error) {
799
800 Unit *u = userdata;
801
802 assert(bus);
803 assert(reply);
804 assert(u);
805
806 return sd_bus_message_append(reply, "s", unit_slice_name(u));
807 }
808
809 static int property_get_current_memory(
810 sd_bus *bus,
811 const char *path,
812 const char *interface,
813 const char *property,
814 sd_bus_message *reply,
815 void *userdata,
816 sd_bus_error *error) {
817
818 uint64_t sz = (uint64_t) -1;
819 Unit *u = userdata;
820 int r;
821
822 assert(bus);
823 assert(reply);
824 assert(u);
825
826 r = unit_get_memory_current(u, &sz);
827 if (r < 0 && r != -ENODATA)
828 log_unit_warning_errno(u, r, "Failed to get memory.usage_in_bytes attribute: %m");
829
830 return sd_bus_message_append(reply, "t", sz);
831 }
832
833 static int property_get_current_tasks(
834 sd_bus *bus,
835 const char *path,
836 const char *interface,
837 const char *property,
838 sd_bus_message *reply,
839 void *userdata,
840 sd_bus_error *error) {
841
842 uint64_t cn = (uint64_t) -1;
843 Unit *u = userdata;
844 int r;
845
846 assert(bus);
847 assert(reply);
848 assert(u);
849
850 r = unit_get_tasks_current(u, &cn);
851 if (r < 0 && r != -ENODATA)
852 log_unit_warning_errno(u, r, "Failed to get pids.current attribute: %m");
853
854 return sd_bus_message_append(reply, "t", cn);
855 }
856
857 static int property_get_cpu_usage(
858 sd_bus *bus,
859 const char *path,
860 const char *interface,
861 const char *property,
862 sd_bus_message *reply,
863 void *userdata,
864 sd_bus_error *error) {
865
866 nsec_t ns = (nsec_t) -1;
867 Unit *u = userdata;
868 int r;
869
870 assert(bus);
871 assert(reply);
872 assert(u);
873
874 r = unit_get_cpu_usage(u, &ns);
875 if (r < 0 && r != -ENODATA)
876 log_unit_warning_errno(u, r, "Failed to get cpuacct.usage attribute: %m");
877
878 return sd_bus_message_append(reply, "t", ns);
879 }
880
881 static int property_get_cgroup(
882 sd_bus *bus,
883 const char *path,
884 const char *interface,
885 const char *property,
886 sd_bus_message *reply,
887 void *userdata,
888 sd_bus_error *error) {
889
890 Unit *u = userdata;
891 const char *t;
892
893 assert(bus);
894 assert(reply);
895 assert(u);
896
897 /* Three cases: a) u->cgroup_path is NULL, in which case the
898 * unit has no control group, which we report as the empty
899 * string. b) u->cgroup_path is the empty string, which
900 * indicates the root cgroup, which we report as "/". c) all
901 * other cases we report as-is. */
902
903 if (u->cgroup_path)
904 t = isempty(u->cgroup_path) ? "/" : u->cgroup_path;
905 else
906 t = "";
907
908 return sd_bus_message_append(reply, "s", t);
909 }
910
911 static int append_process(sd_bus_message *reply, const char *p, pid_t pid, Set *pids) {
912 _cleanup_free_ char *buf = NULL, *cmdline = NULL;
913 int r;
914
915 assert(reply);
916 assert(pid > 0);
917
918 r = set_put(pids, PID_TO_PTR(pid));
919 if (r == -EEXIST || r == 0)
920 return 0;
921 if (r < 0)
922 return r;
923
924 if (!p) {
925 r = cg_pid_get_path(SYSTEMD_CGROUP_CONTROLLER, pid, &buf);
926 if (r == -ESRCH)
927 return 0;
928 if (r < 0)
929 return r;
930
931 p = buf;
932 }
933
934 (void) get_process_cmdline(pid, 0, true, &cmdline);
935
936 return sd_bus_message_append(reply,
937 "(sus)",
938 p,
939 (uint32_t) pid,
940 cmdline);
941 }
942
943 static int append_cgroup(sd_bus_message *reply, const char *p, Set *pids) {
944 _cleanup_closedir_ DIR *d = NULL;
945 _cleanup_fclose_ FILE *f = NULL;
946 int r;
947
948 assert(reply);
949 assert(p);
950
951 r = cg_enumerate_processes(SYSTEMD_CGROUP_CONTROLLER, p, &f);
952 if (r == ENOENT)
953 return 0;
954 if (r < 0)
955 return r;
956
957 for (;;) {
958 pid_t pid;
959
960 r = cg_read_pid(f, &pid);
961 if (r < 0)
962 return r;
963 if (r == 0)
964 break;
965
966 if (is_kernel_thread(pid) > 0)
967 continue;
968
969 r = append_process(reply, p, pid, pids);
970 if (r < 0)
971 return r;
972 }
973
974 r = cg_enumerate_subgroups(SYSTEMD_CGROUP_CONTROLLER, p, &d);
975 if (r == -ENOENT)
976 return 0;
977 if (r < 0)
978 return r;
979
980 for (;;) {
981 _cleanup_free_ char *g = NULL, *j = NULL;
982
983 r = cg_read_subgroup(d, &g);
984 if (r < 0)
985 return r;
986 if (r == 0)
987 break;
988
989 j = strjoin(p, "/", g, NULL);
990 if (!j)
991 return -ENOMEM;
992
993 r = append_cgroup(reply, j, pids);
994 if (r < 0)
995 return r;
996 }
997
998 return 0;
999 }
1000
1001 int bus_unit_method_get_processes(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1002 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
1003 _cleanup_(set_freep) Set *pids = NULL;
1004 Unit *u = userdata;
1005 pid_t pid;
1006 int r;
1007
1008 assert(message);
1009
1010 pids = set_new(NULL);
1011 if (!pids)
1012 return -ENOMEM;
1013
1014 r = sd_bus_message_new_method_return(message, &reply);
1015 if (r < 0)
1016 return r;
1017
1018 r = sd_bus_message_open_container(reply, 'a', "(sus)");
1019 if (r < 0)
1020 return r;
1021
1022 if (u->cgroup_path) {
1023 r = append_cgroup(reply, u->cgroup_path, pids);
1024 if (r < 0)
1025 return r;
1026 }
1027
1028 /* The main and control pids might live outside of the cgroup, hence fetch them separately */
1029 pid = unit_main_pid(u);
1030 if (pid > 0) {
1031 r = append_process(reply, NULL, pid, pids);
1032 if (r < 0)
1033 return r;
1034 }
1035
1036 pid = unit_control_pid(u);
1037 if (pid > 0) {
1038 r = append_process(reply, NULL, pid, pids);
1039 if (r < 0)
1040 return r;
1041 }
1042
1043 r = sd_bus_message_close_container(reply);
1044 if (r < 0)
1045 return r;
1046
1047 return sd_bus_send(NULL, reply, NULL);
1048 }
1049
1050 const sd_bus_vtable bus_unit_cgroup_vtable[] = {
1051 SD_BUS_VTABLE_START(0),
1052 SD_BUS_PROPERTY("Slice", "s", property_get_slice, 0, 0),
1053 SD_BUS_PROPERTY("ControlGroup", "s", property_get_cgroup, 0, 0),
1054 SD_BUS_PROPERTY("MemoryCurrent", "t", property_get_current_memory, 0, 0),
1055 SD_BUS_PROPERTY("CPUUsageNSec", "t", property_get_cpu_usage, 0, 0),
1056 SD_BUS_PROPERTY("TasksCurrent", "t", property_get_current_tasks, 0, 0),
1057 SD_BUS_METHOD("GetProcesses", NULL, "a(sus)", bus_unit_method_get_processes, SD_BUS_VTABLE_UNPRIVILEGED),
1058 SD_BUS_VTABLE_END
1059 };
1060
1061 static int send_new_signal(sd_bus *bus, void *userdata) {
1062 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
1063 _cleanup_free_ char *p = NULL;
1064 Unit *u = userdata;
1065 int r;
1066
1067 assert(bus);
1068 assert(u);
1069
1070 p = unit_dbus_path(u);
1071 if (!p)
1072 return -ENOMEM;
1073
1074 r = sd_bus_message_new_signal(
1075 bus,
1076 &m,
1077 "/org/freedesktop/systemd1",
1078 "org.freedesktop.systemd1.Manager",
1079 "UnitNew");
1080 if (r < 0)
1081 return r;
1082
1083 r = sd_bus_message_append(m, "so", u->id, p);
1084 if (r < 0)
1085 return r;
1086
1087 return sd_bus_send(bus, m, NULL);
1088 }
1089
1090 static int send_changed_signal(sd_bus *bus, void *userdata) {
1091 _cleanup_free_ char *p = NULL;
1092 Unit *u = userdata;
1093 int r;
1094
1095 assert(bus);
1096 assert(u);
1097
1098 p = unit_dbus_path(u);
1099 if (!p)
1100 return -ENOMEM;
1101
1102 /* Send a properties changed signal. First for the specific
1103 * type, then for the generic unit. The clients may rely on
1104 * this order to get atomic behavior if needed. */
1105
1106 r = sd_bus_emit_properties_changed_strv(
1107 bus, p,
1108 unit_dbus_interface_from_type(u->type),
1109 NULL);
1110 if (r < 0)
1111 return r;
1112
1113 return sd_bus_emit_properties_changed_strv(
1114 bus, p,
1115 "org.freedesktop.systemd1.Unit",
1116 NULL);
1117 }
1118
1119 void bus_unit_send_change_signal(Unit *u) {
1120 int r;
1121 assert(u);
1122
1123 if (u->in_dbus_queue) {
1124 LIST_REMOVE(dbus_queue, u->manager->dbus_unit_queue, u);
1125 u->in_dbus_queue = false;
1126 }
1127
1128 if (!u->id)
1129 return;
1130
1131 r = bus_foreach_bus(u->manager, NULL, u->sent_dbus_new_signal ? send_changed_signal : send_new_signal, u);
1132 if (r < 0)
1133 log_unit_debug_errno(u, r, "Failed to send unit change signal for %s: %m", u->id);
1134
1135 u->sent_dbus_new_signal = true;
1136 }
1137
1138 static int send_removed_signal(sd_bus *bus, void *userdata) {
1139 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
1140 _cleanup_free_ char *p = NULL;
1141 Unit *u = userdata;
1142 int r;
1143
1144 assert(bus);
1145 assert(u);
1146
1147 p = unit_dbus_path(u);
1148 if (!p)
1149 return -ENOMEM;
1150
1151 r = sd_bus_message_new_signal(
1152 bus,
1153 &m,
1154 "/org/freedesktop/systemd1",
1155 "org.freedesktop.systemd1.Manager",
1156 "UnitRemoved");
1157 if (r < 0)
1158 return r;
1159
1160 r = sd_bus_message_append(m, "so", u->id, p);
1161 if (r < 0)
1162 return r;
1163
1164 return sd_bus_send(bus, m, NULL);
1165 }
1166
1167 void bus_unit_send_removed_signal(Unit *u) {
1168 int r;
1169 assert(u);
1170
1171 if (!u->sent_dbus_new_signal || u->in_dbus_queue)
1172 bus_unit_send_change_signal(u);
1173
1174 if (!u->id)
1175 return;
1176
1177 r = bus_foreach_bus(u->manager, NULL, send_removed_signal, u);
1178 if (r < 0)
1179 log_unit_debug_errno(u, r, "Failed to send unit remove signal for %s: %m", u->id);
1180 }
1181
1182 int bus_unit_queue_job(
1183 sd_bus_message *message,
1184 Unit *u,
1185 JobType type,
1186 JobMode mode,
1187 bool reload_if_possible,
1188 sd_bus_error *error) {
1189
1190 _cleanup_free_ char *path = NULL;
1191 Job *j;
1192 int r;
1193
1194 assert(message);
1195 assert(u);
1196 assert(type >= 0 && type < _JOB_TYPE_MAX);
1197 assert(mode >= 0 && mode < _JOB_MODE_MAX);
1198
1199 r = mac_selinux_unit_access_check(
1200 u, message,
1201 job_type_to_access_method(type),
1202 error);
1203 if (r < 0)
1204 return r;
1205
1206 if (reload_if_possible && unit_can_reload(u)) {
1207 if (type == JOB_RESTART)
1208 type = JOB_RELOAD_OR_START;
1209 else if (type == JOB_TRY_RESTART)
1210 type = JOB_TRY_RELOAD;
1211 }
1212
1213 if (type == JOB_STOP &&
1214 (u->load_state == UNIT_NOT_FOUND || u->load_state == UNIT_ERROR) &&
1215 unit_active_state(u) == UNIT_INACTIVE)
1216 return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_UNIT, "Unit %s not loaded.", u->id);
1217
1218 if ((type == JOB_START && u->refuse_manual_start) ||
1219 (type == JOB_STOP && u->refuse_manual_stop) ||
1220 ((type == JOB_RESTART || type == JOB_TRY_RESTART) && (u->refuse_manual_start || u->refuse_manual_stop)) ||
1221 (type == JOB_RELOAD_OR_START && job_type_collapse(type, u) == JOB_START && u->refuse_manual_start))
1222 return sd_bus_error_setf(error, BUS_ERROR_ONLY_BY_DEPENDENCY, "Operation refused, unit %s may be requested by dependency only.", u->id);
1223
1224 r = manager_add_job(u->manager, type, u, mode, error, &j);
1225 if (r < 0)
1226 return r;
1227
1228 if (sd_bus_message_get_bus(message) == u->manager->api_bus) {
1229 if (!j->clients) {
1230 r = sd_bus_track_new(sd_bus_message_get_bus(message), &j->clients, NULL, NULL);
1231 if (r < 0)
1232 return r;
1233 }
1234
1235 r = sd_bus_track_add_sender(j->clients, message);
1236 if (r < 0)
1237 return r;
1238 }
1239
1240 path = job_dbus_path(j);
1241 if (!path)
1242 return -ENOMEM;
1243
1244 return sd_bus_reply_method_return(message, "o", path);
1245 }
1246
1247 static int bus_unit_set_transient_property(
1248 Unit *u,
1249 const char *name,
1250 sd_bus_message *message,
1251 UnitSetPropertiesMode mode,
1252 sd_bus_error *error) {
1253
1254 int r;
1255
1256 assert(u);
1257 assert(name);
1258 assert(message);
1259
1260 if (streq(name, "Description")) {
1261 const char *d;
1262
1263 r = sd_bus_message_read(message, "s", &d);
1264 if (r < 0)
1265 return r;
1266
1267 if (mode != UNIT_CHECK) {
1268 r = unit_set_description(u, d);
1269 if (r < 0)
1270 return r;
1271
1272 unit_write_drop_in_format(u, mode, name, "[Unit]\nDescription=%s", d);
1273 }
1274
1275 return 1;
1276
1277 } else if (streq(name, "DefaultDependencies")) {
1278 int b;
1279
1280 r = sd_bus_message_read(message, "b", &b);
1281 if (r < 0)
1282 return r;
1283
1284 if (mode != UNIT_CHECK) {
1285 u->default_dependencies = b;
1286 unit_write_drop_in_format(u, mode, name, "[Unit]\nDefaultDependencies=%s", yes_no(b));
1287 }
1288
1289 return 1;
1290
1291 } else if (streq(name, "Slice")) {
1292 Unit *slice;
1293 const char *s;
1294
1295 if (!UNIT_HAS_CGROUP_CONTEXT(u))
1296 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "The slice property is only available for units with control groups.");
1297 if (u->type == UNIT_SLICE)
1298 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Slice may not be set for slice units.");
1299 if (unit_has_name(u, SPECIAL_INIT_SCOPE))
1300 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Cannot set slice for init.scope");
1301
1302 r = sd_bus_message_read(message, "s", &s);
1303 if (r < 0)
1304 return r;
1305
1306 if (!unit_name_is_valid(s, UNIT_NAME_PLAIN))
1307 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid unit name '%s'", s);
1308
1309 /* Note that we do not dispatch the load queue here yet, as we don't want our own transient unit to be
1310 * loaded while we are still setting it up. Or in other words, we use manager_load_unit_prepare()
1311 * instead of manager_load_unit() on purpose, here. */
1312 r = manager_load_unit_prepare(u->manager, s, NULL, error, &slice);
1313 if (r < 0)
1314 return r;
1315
1316 if (slice->type != UNIT_SLICE)
1317 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Unit name '%s' is not a slice", s);
1318
1319 if (mode != UNIT_CHECK) {
1320 r = unit_set_slice(u, slice);
1321 if (r < 0)
1322 return r;
1323
1324 unit_write_drop_in_private_format(u, mode, name, "Slice=%s", s);
1325 }
1326
1327 return 1;
1328
1329 } else if (STR_IN_SET(name,
1330 "Requires", "RequiresOverridable",
1331 "Requisite", "RequisiteOverridable",
1332 "Wants",
1333 "BindsTo",
1334 "Conflicts",
1335 "Before", "After",
1336 "OnFailure",
1337 "PropagatesReloadTo", "ReloadPropagatedFrom",
1338 "PartOf")) {
1339
1340 UnitDependency d;
1341 const char *other;
1342
1343 if (streq(name, "RequiresOverridable"))
1344 d = UNIT_REQUIRES; /* redirect for obsolete unit dependency type */
1345 else if (streq(name, "RequisiteOverridable"))
1346 d = UNIT_REQUISITE; /* same here */
1347 else {
1348 d = unit_dependency_from_string(name);
1349 if (d < 0)
1350 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid unit dependency: %s", name);
1351 }
1352
1353 r = sd_bus_message_enter_container(message, 'a', "s");
1354 if (r < 0)
1355 return r;
1356
1357 while ((r = sd_bus_message_read(message, "s", &other)) > 0) {
1358 if (!unit_name_is_valid(other, UNIT_NAME_PLAIN|UNIT_NAME_INSTANCE))
1359 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid unit name %s", other);
1360
1361 if (mode != UNIT_CHECK) {
1362 _cleanup_free_ char *label = NULL;
1363
1364 r = unit_add_dependency_by_name(u, d, other, NULL, true);
1365 if (r < 0)
1366 return r;
1367
1368 label = strjoin(name, "-", other, NULL);
1369 if (!label)
1370 return -ENOMEM;
1371
1372 unit_write_drop_in_format(u, mode, label, "[Unit]\n%s=%s", name, other);
1373 }
1374
1375 }
1376 if (r < 0)
1377 return r;
1378
1379 r = sd_bus_message_exit_container(message);
1380 if (r < 0)
1381 return r;
1382
1383 return 1;
1384
1385 } else if (streq(name, "AddRef")) {
1386
1387 int b;
1388
1389 /* Why is this called "AddRef" rather than just "Ref", or "Reference"? There's already a "Ref()" method
1390 * on the Unit interface, and it's probably not a good idea to expose a property and a method on the
1391 * same interface (well, strictly speaking AddRef isn't exposed as full property, we just read it for
1392 * transient units, but still). And "References" and "ReferencedBy" is already used as unit reference
1393 * dependency type, hence let's not confuse things with that.
1394 *
1395 * Note that we don't acually add the reference to the bus track. We do that only after the setup of
1396 * the transient unit is complete, so that setting this property multiple times in the same transient
1397 * unit creation call doesn't count as individual references. */
1398
1399 r = sd_bus_message_read(message, "b", &b);
1400 if (r < 0)
1401 return r;
1402
1403 if (mode != UNIT_CHECK)
1404 u->bus_track_add = b;
1405
1406 return 1;
1407 }
1408
1409 return 0;
1410 }
1411
1412 int bus_unit_set_properties(
1413 Unit *u,
1414 sd_bus_message *message,
1415 UnitSetPropertiesMode mode,
1416 bool commit,
1417 sd_bus_error *error) {
1418
1419 bool for_real = false;
1420 unsigned n = 0;
1421 int r;
1422
1423 assert(u);
1424 assert(message);
1425
1426 /* We iterate through the array twice. First run we just check
1427 * if all passed data is valid, second run actually applies
1428 * it. This is to implement transaction-like behaviour without
1429 * actually providing full transactions. */
1430
1431 r = sd_bus_message_enter_container(message, 'a', "(sv)");
1432 if (r < 0)
1433 return r;
1434
1435 for (;;) {
1436 const char *name;
1437
1438 r = sd_bus_message_enter_container(message, 'r', "sv");
1439 if (r < 0)
1440 return r;
1441 if (r == 0) {
1442 if (for_real || mode == UNIT_CHECK)
1443 break;
1444
1445 /* Reached EOF. Let's try again, and this time for realz... */
1446 r = sd_bus_message_rewind(message, false);
1447 if (r < 0)
1448 return r;
1449
1450 for_real = true;
1451 continue;
1452 }
1453
1454 r = sd_bus_message_read(message, "s", &name);
1455 if (r < 0)
1456 return r;
1457
1458 if (!UNIT_VTABLE(u)->bus_set_property)
1459 return sd_bus_error_setf(error, SD_BUS_ERROR_PROPERTY_READ_ONLY, "Objects of this type do not support setting properties.");
1460
1461 r = sd_bus_message_enter_container(message, 'v', NULL);
1462 if (r < 0)
1463 return r;
1464
1465 r = UNIT_VTABLE(u)->bus_set_property(u, name, message, for_real ? mode : UNIT_CHECK, error);
1466 if (r == 0 && u->transient && u->load_state == UNIT_STUB)
1467 r = bus_unit_set_transient_property(u, name, message, for_real ? mode : UNIT_CHECK, error);
1468 if (r < 0)
1469 return r;
1470 if (r == 0)
1471 return sd_bus_error_setf(error, SD_BUS_ERROR_PROPERTY_READ_ONLY, "Cannot set property %s, or unknown property.", name);
1472
1473 r = sd_bus_message_exit_container(message);
1474 if (r < 0)
1475 return r;
1476
1477 r = sd_bus_message_exit_container(message);
1478 if (r < 0)
1479 return r;
1480
1481 n += for_real;
1482 }
1483
1484 r = sd_bus_message_exit_container(message);
1485 if (r < 0)
1486 return r;
1487
1488 if (commit && n > 0 && UNIT_VTABLE(u)->bus_commit_properties)
1489 UNIT_VTABLE(u)->bus_commit_properties(u);
1490
1491 return n;
1492 }
1493
1494 int bus_unit_check_load_state(Unit *u, sd_bus_error *error) {
1495 assert(u);
1496
1497 if (u->load_state == UNIT_LOADED)
1498 return 0;
1499
1500 /* Give a better description of the unit error when
1501 * possible. Note that in the case of UNIT_MASKED, load_error
1502 * is not set. */
1503 if (u->load_state == UNIT_MASKED)
1504 return sd_bus_error_setf(error, BUS_ERROR_UNIT_MASKED, "Unit %s is masked.", u->id);
1505
1506 if (u->load_state == UNIT_NOT_FOUND)
1507 return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_UNIT, "Unit %s not found.", u->id);
1508
1509 return sd_bus_error_set_errnof(error, u->load_error, "Unit %s is not loaded properly: %m.", u->id);
1510 }
1511
1512 static int bus_track_handler(sd_bus_track *t, void *userdata) {
1513 Unit *u = userdata;
1514
1515 assert(t);
1516 assert(u);
1517
1518 u->bus_track = sd_bus_track_unref(u->bus_track); /* make sure we aren't called again */
1519
1520 unit_add_to_gc_queue(u);
1521 return 0;
1522 }
1523
1524 static int allocate_bus_track(Unit *u) {
1525 int r;
1526
1527 assert(u);
1528
1529 if (u->bus_track)
1530 return 0;
1531
1532 r = sd_bus_track_new(u->manager->api_bus, &u->bus_track, bus_track_handler, u);
1533 if (r < 0)
1534 return r;
1535
1536 r = sd_bus_track_set_recursive(u->bus_track, true);
1537 if (r < 0) {
1538 u->bus_track = sd_bus_track_unref(u->bus_track);
1539 return r;
1540 }
1541
1542 return 0;
1543 }
1544
1545 int bus_unit_track_add_name(Unit *u, const char *name) {
1546 int r;
1547
1548 assert(u);
1549
1550 r = allocate_bus_track(u);
1551 if (r < 0)
1552 return r;
1553
1554 return sd_bus_track_add_name(u->bus_track, name);
1555 }
1556
1557 int bus_unit_track_add_sender(Unit *u, sd_bus_message *m) {
1558 int r;
1559
1560 assert(u);
1561
1562 r = allocate_bus_track(u);
1563 if (r < 0)
1564 return r;
1565
1566 return sd_bus_track_add_sender(u->bus_track, m);
1567 }
1568
1569 int bus_unit_track_remove_sender(Unit *u, sd_bus_message *m) {
1570 assert(u);
1571
1572 /* If we haven't allocated the bus track object yet, then there's definitely no reference taken yet, return an
1573 * error */
1574 if (!u->bus_track)
1575 return -EUNATCH;
1576
1577 return sd_bus_track_remove_sender(u->bus_track, m);
1578 }