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