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