]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
a7334b09 LP |
2 | /*** |
3 | This file is part of systemd. | |
4 | ||
5 | Copyright 2010 Lennart Poettering | |
a7334b09 LP |
6 | ***/ |
7 | ||
718db961 | 8 | #include "sd-bus.h" |
07630cea | 9 | |
b5efdb8a | 10 | #include "alloc-util.h" |
906c06f6 | 11 | #include "bpf-firewall.h" |
07630cea LP |
12 | #include "bus-common-errors.h" |
13 | #include "cgroup-util.h" | |
faa781cf | 14 | #include "condition.h" |
c5a97ed1 | 15 | #include "dbus-job.h" |
8752c575 | 16 | #include "dbus-unit.h" |
faa781cf | 17 | #include "dbus-util.h" |
07630cea | 18 | #include "dbus.h" |
291d565a | 19 | #include "fd-util.h" |
8752c575 | 20 | #include "locale-util.h" |
ea430986 | 21 | #include "log.h" |
faa781cf | 22 | #include "path-util.h" |
291d565a | 23 | #include "process-util.h" |
e2417e41 | 24 | #include "selinux-access.h" |
6eb7c172 | 25 | #include "signal-util.h" |
efdb0237 | 26 | #include "special.h" |
07630cea LP |
27 | #include "string-util.h" |
28 | #include "strv.h" | |
ee104e11 | 29 | #include "user-util.h" |
faa781cf | 30 | #include "web-util.h" |
718db961 | 31 | |
5afe510c | 32 | static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_collect_mode, collect_mode, CollectMode); |
718db961 | 33 | static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_load_state, unit_load_state, UnitLoadState); |
d420282b | 34 | static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_job_mode, job_mode, JobMode); |
87a47f99 | 35 | static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_emergency_action, emergency_action, EmergencyAction); |
718db961 LP |
36 | |
37 | static int property_get_names( | |
38 | sd_bus *bus, | |
39 | const char *path, | |
40 | const char *interface, | |
41 | const char *property, | |
42 | sd_bus_message *reply, | |
ebcf1f97 LP |
43 | void *userdata, |
44 | sd_bus_error *error) { | |
718db961 LP |
45 | |
46 | Unit *u = userdata; | |
47 | Iterator i; | |
48 | const char *t; | |
49 | int r; | |
ea430986 | 50 | |
718db961 LP |
51 | assert(bus); |
52 | assert(reply); | |
53 | assert(u); | |
4139c1b2 | 54 | |
718db961 LP |
55 | r = sd_bus_message_open_container(reply, 'a', "s"); |
56 | if (r < 0) | |
57 | return r; | |
4139c1b2 | 58 | |
718db961 LP |
59 | SET_FOREACH(t, u->names, i) { |
60 | r = sd_bus_message_append(reply, "s", t); | |
61 | if (r < 0) | |
62 | return r; | |
63 | } | |
4139c1b2 | 64 | |
718db961 | 65 | return sd_bus_message_close_container(reply); |
4139c1b2 LP |
66 | } |
67 | ||
718db961 LP |
68 | static int property_get_following( |
69 | sd_bus *bus, | |
70 | const char *path, | |
71 | const char *interface, | |
72 | const char *property, | |
73 | sd_bus_message *reply, | |
ebcf1f97 LP |
74 | void *userdata, |
75 | sd_bus_error *error) { | |
718db961 LP |
76 | |
77 | Unit *u = userdata, *f; | |
8fe914ec | 78 | |
718db961 LP |
79 | assert(bus); |
80 | assert(reply); | |
8fe914ec LP |
81 | assert(u); |
82 | ||
a7f241db | 83 | f = unit_following(u); |
79a60375 | 84 | return sd_bus_message_append(reply, "s", f ? f->id : NULL); |
8fe914ec LP |
85 | } |
86 | ||
718db961 LP |
87 | static int property_get_dependencies( |
88 | sd_bus *bus, | |
89 | const char *path, | |
90 | const char *interface, | |
91 | const char *property, | |
92 | sd_bus_message *reply, | |
ebcf1f97 LP |
93 | void *userdata, |
94 | sd_bus_error *error) { | |
4ec9a8a4 | 95 | |
eef85c4a | 96 | Hashmap *h = *(Hashmap**) userdata; |
5301be81 | 97 | Iterator j; |
718db961 | 98 | Unit *u; |
eef85c4a | 99 | void *v; |
718db961 | 100 | int r; |
5301be81 | 101 | |
718db961 LP |
102 | assert(bus); |
103 | assert(reply); | |
5301be81 | 104 | |
718db961 LP |
105 | r = sd_bus_message_open_container(reply, 'a', "s"); |
106 | if (r < 0) | |
107 | return r; | |
5301be81 | 108 | |
eef85c4a | 109 | HASHMAP_FOREACH_KEY(v, u, h, j) { |
718db961 LP |
110 | r = sd_bus_message_append(reply, "s", u->id); |
111 | if (r < 0) | |
112 | return r; | |
113 | } | |
5301be81 | 114 | |
718db961 | 115 | return sd_bus_message_close_container(reply); |
5301be81 LP |
116 | } |
117 | ||
f32b43bd LP |
118 | static int property_get_obsolete_dependencies( |
119 | sd_bus *bus, | |
120 | const char *path, | |
121 | const char *interface, | |
122 | const char *property, | |
123 | sd_bus_message *reply, | |
124 | void *userdata, | |
125 | sd_bus_error *error) { | |
126 | ||
127 | assert(bus); | |
128 | assert(reply); | |
129 | ||
130 | /* For dependency types we don't support anymore always return an empty array */ | |
131 | return sd_bus_message_append(reply, "as", 0); | |
132 | } | |
133 | ||
99c14018 YW |
134 | static int property_get_requires_mounts_for( |
135 | sd_bus *bus, | |
136 | const char *path, | |
137 | const char *interface, | |
138 | const char *property, | |
139 | sd_bus_message *reply, | |
140 | void *userdata, | |
141 | sd_bus_error *error) { | |
142 | ||
f6e11b39 | 143 | Hashmap *h = *(Hashmap**) userdata; |
99c14018 | 144 | const char *p; |
f6e11b39 | 145 | Iterator j; |
99c14018 YW |
146 | void *v; |
147 | int r; | |
148 | ||
149 | assert(bus); | |
150 | assert(reply); | |
151 | ||
152 | r = sd_bus_message_open_container(reply, 'a', "s"); | |
153 | if (r < 0) | |
154 | return r; | |
155 | ||
156 | HASHMAP_FOREACH_KEY(v, p, h, j) { | |
157 | r = sd_bus_message_append(reply, "s", p); | |
158 | if (r < 0) | |
159 | return r; | |
160 | } | |
161 | ||
162 | return sd_bus_message_close_container(reply); | |
163 | } | |
164 | ||
718db961 LP |
165 | static int property_get_description( |
166 | sd_bus *bus, | |
167 | const char *path, | |
168 | const char *interface, | |
169 | const char *property, | |
170 | sd_bus_message *reply, | |
ebcf1f97 LP |
171 | void *userdata, |
172 | sd_bus_error *error) { | |
ea430986 | 173 | |
718db961 | 174 | Unit *u = userdata; |
ea430986 | 175 | |
718db961 LP |
176 | assert(bus); |
177 | assert(reply); | |
178 | assert(u); | |
ea430986 | 179 | |
718db961 | 180 | return sd_bus_message_append(reply, "s", unit_description(u)); |
ea430986 LP |
181 | } |
182 | ||
718db961 LP |
183 | static int property_get_active_state( |
184 | sd_bus *bus, | |
185 | const char *path, | |
186 | const char *interface, | |
187 | const char *property, | |
188 | sd_bus_message *reply, | |
ebcf1f97 LP |
189 | void *userdata, |
190 | sd_bus_error *error) { | |
ea430986 | 191 | |
718db961 | 192 | Unit *u = userdata; |
ea430986 | 193 | |
718db961 LP |
194 | assert(bus); |
195 | assert(reply); | |
ea430986 LP |
196 | assert(u); |
197 | ||
718db961 | 198 | return sd_bus_message_append(reply, "s", unit_active_state_to_string(unit_active_state(u))); |
ea430986 LP |
199 | } |
200 | ||
718db961 LP |
201 | static int property_get_sub_state( |
202 | sd_bus *bus, | |
203 | const char *path, | |
204 | const char *interface, | |
205 | const char *property, | |
206 | sd_bus_message *reply, | |
ebcf1f97 LP |
207 | void *userdata, |
208 | sd_bus_error *error) { | |
10a94420 | 209 | |
718db961 | 210 | Unit *u = userdata; |
10a94420 | 211 | |
718db961 LP |
212 | assert(bus); |
213 | assert(reply); | |
214 | assert(u); | |
10a94420 | 215 | |
718db961 | 216 | return sd_bus_message_append(reply, "s", unit_sub_state_to_string(u)); |
10a94420 LP |
217 | } |
218 | ||
d2dc52db LP |
219 | static int property_get_unit_file_preset( |
220 | sd_bus *bus, | |
221 | const char *path, | |
222 | const char *interface, | |
223 | const char *property, | |
224 | sd_bus_message *reply, | |
225 | void *userdata, | |
226 | sd_bus_error *error) { | |
227 | ||
228 | Unit *u = userdata; | |
229 | int r; | |
230 | ||
231 | assert(bus); | |
232 | assert(reply); | |
233 | assert(u); | |
234 | ||
235 | r = unit_get_unit_file_preset(u); | |
236 | ||
237 | return sd_bus_message_append(reply, "s", | |
79a60375 | 238 | r < 0 ? NULL: |
d2dc52db LP |
239 | r > 0 ? "enabled" : "disabled"); |
240 | } | |
241 | ||
718db961 LP |
242 | static int property_get_unit_file_state( |
243 | sd_bus *bus, | |
244 | const char *path, | |
245 | const char *interface, | |
246 | const char *property, | |
247 | sd_bus_message *reply, | |
ebcf1f97 LP |
248 | void *userdata, |
249 | sd_bus_error *error) { | |
a4375746 | 250 | |
718db961 | 251 | Unit *u = userdata; |
a4375746 | 252 | |
718db961 LP |
253 | assert(bus); |
254 | assert(reply); | |
255 | assert(u); | |
a4375746 | 256 | |
76445832 | 257 | return sd_bus_message_append(reply, "s", unit_file_state_to_string(unit_get_unit_file_state(u))); |
a4375746 LP |
258 | } |
259 | ||
718db961 LP |
260 | static int property_get_can_start( |
261 | sd_bus *bus, | |
262 | const char *path, | |
263 | const char *interface, | |
264 | const char *property, | |
265 | sd_bus_message *reply, | |
ebcf1f97 LP |
266 | void *userdata, |
267 | sd_bus_error *error) { | |
38131695 | 268 | |
718db961 | 269 | Unit *u = userdata; |
b5e9dba8 | 270 | |
718db961 LP |
271 | assert(bus); |
272 | assert(reply); | |
273 | assert(u); | |
b5e9dba8 | 274 | |
718db961 | 275 | return sd_bus_message_append(reply, "b", unit_can_start(u) && !u->refuse_manual_start); |
b5e9dba8 LP |
276 | } |
277 | ||
718db961 LP |
278 | static int property_get_can_stop( |
279 | sd_bus *bus, | |
280 | const char *path, | |
281 | const char *interface, | |
282 | const char *property, | |
283 | sd_bus_message *reply, | |
ebcf1f97 LP |
284 | void *userdata, |
285 | sd_bus_error *error) { | |
718db961 LP |
286 | |
287 | Unit *u = userdata; | |
b5e9dba8 | 288 | |
718db961 LP |
289 | assert(bus); |
290 | assert(reply); | |
b5e9dba8 LP |
291 | assert(u); |
292 | ||
f5869324 | 293 | return sd_bus_message_append(reply, "b", unit_can_stop(u) && !u->refuse_manual_stop); |
38131695 LP |
294 | } |
295 | ||
718db961 LP |
296 | static int property_get_can_reload( |
297 | sd_bus *bus, | |
298 | const char *path, | |
299 | const char *interface, | |
300 | const char *property, | |
301 | sd_bus_message *reply, | |
ebcf1f97 LP |
302 | void *userdata, |
303 | sd_bus_error *error) { | |
38131695 | 304 | |
718db961 | 305 | Unit *u = userdata; |
38131695 | 306 | |
718db961 LP |
307 | assert(bus); |
308 | assert(reply); | |
309 | assert(u); | |
38131695 | 310 | |
718db961 | 311 | return sd_bus_message_append(reply, "b", unit_can_reload(u)); |
38131695 LP |
312 | } |
313 | ||
718db961 LP |
314 | static int property_get_can_isolate( |
315 | sd_bus *bus, | |
316 | const char *path, | |
317 | const char *interface, | |
318 | const char *property, | |
319 | sd_bus_message *reply, | |
ebcf1f97 LP |
320 | void *userdata, |
321 | sd_bus_error *error) { | |
2528a7a6 | 322 | |
718db961 | 323 | Unit *u = userdata; |
2528a7a6 | 324 | |
718db961 LP |
325 | assert(bus); |
326 | assert(reply); | |
327 | assert(u); | |
2528a7a6 | 328 | |
718db961 | 329 | return sd_bus_message_append(reply, "b", unit_can_isolate(u) && !u->refuse_manual_start); |
2528a7a6 LP |
330 | } |
331 | ||
718db961 LP |
332 | static int property_get_job( |
333 | sd_bus *bus, | |
334 | const char *path, | |
335 | const char *interface, | |
336 | const char *property, | |
337 | sd_bus_message *reply, | |
ebcf1f97 LP |
338 | void *userdata, |
339 | sd_bus_error *error) { | |
718db961 | 340 | |
f93891f3 | 341 | _cleanup_free_ char *p = NULL; |
718db961 | 342 | Unit *u = userdata; |
38131695 | 343 | |
718db961 LP |
344 | assert(bus); |
345 | assert(reply); | |
38131695 LP |
346 | assert(u); |
347 | ||
718db961 LP |
348 | if (!u->job) |
349 | return sd_bus_message_append(reply, "(uo)", 0, "/"); | |
38131695 | 350 | |
718db961 LP |
351 | p = job_dbus_path(u->job); |
352 | if (!p) | |
353 | return -ENOMEM; | |
38131695 | 354 | |
718db961 LP |
355 | return sd_bus_message_append(reply, "(uo)", u->job->id, p); |
356 | } | |
38131695 | 357 | |
718db961 LP |
358 | static int property_get_need_daemon_reload( |
359 | sd_bus *bus, | |
360 | const char *path, | |
361 | const char *interface, | |
362 | const char *property, | |
363 | sd_bus_message *reply, | |
ebcf1f97 LP |
364 | void *userdata, |
365 | sd_bus_error *error) { | |
38131695 | 366 | |
718db961 | 367 | Unit *u = userdata; |
38131695 | 368 | |
718db961 LP |
369 | assert(bus); |
370 | assert(reply); | |
371 | assert(u); | |
38131695 | 372 | |
718db961 | 373 | return sd_bus_message_append(reply, "b", unit_need_daemon_reload(u)); |
38131695 LP |
374 | } |
375 | ||
718db961 LP |
376 | static int property_get_conditions( |
377 | sd_bus *bus, | |
378 | const char *path, | |
379 | const char *interface, | |
380 | const char *property, | |
381 | sd_bus_message *reply, | |
ebcf1f97 LP |
382 | void *userdata, |
383 | sd_bus_error *error) { | |
45fb0699 | 384 | |
59fccdc5 LP |
385 | const char *(*to_string)(ConditionType type) = NULL; |
386 | Condition **list = userdata, *c; | |
718db961 | 387 | int r; |
45fb0699 | 388 | |
718db961 LP |
389 | assert(bus); |
390 | assert(reply); | |
59fccdc5 LP |
391 | assert(list); |
392 | ||
393 | to_string = streq(property, "Asserts") ? assert_type_to_string : condition_type_to_string; | |
45fb0699 | 394 | |
718db961 LP |
395 | r = sd_bus_message_open_container(reply, 'a', "(sbbsi)"); |
396 | if (r < 0) | |
397 | return r; | |
45fb0699 | 398 | |
59fccdc5 | 399 | LIST_FOREACH(conditions, c, *list) { |
cc50ef13 LP |
400 | int tristate; |
401 | ||
402 | tristate = | |
403 | c->result == CONDITION_UNTESTED ? 0 : | |
404 | c->result == CONDITION_SUCCEEDED ? 1 : -1; | |
405 | ||
2c7e050f | 406 | r = sd_bus_message_append(reply, "(sbbsi)", |
59fccdc5 | 407 | to_string(c->type), |
2c7e050f | 408 | c->trigger, c->negate, |
cc50ef13 | 409 | c->parameter, tristate); |
718db961 LP |
410 | if (r < 0) |
411 | return r; | |
45fb0699 | 412 | |
718db961 | 413 | } |
52990c2e | 414 | |
718db961 | 415 | return sd_bus_message_close_container(reply); |
52990c2e ZJS |
416 | } |
417 | ||
718db961 LP |
418 | static int property_get_load_error( |
419 | sd_bus *bus, | |
420 | const char *path, | |
421 | const char *interface, | |
422 | const char *property, | |
423 | sd_bus_message *reply, | |
ebcf1f97 LP |
424 | void *userdata, |
425 | sd_bus_error *error) { | |
52990c2e | 426 | |
4afd3348 | 427 | _cleanup_(sd_bus_error_free) sd_bus_error e = SD_BUS_ERROR_NULL; |
718db961 | 428 | Unit *u = userdata; |
52990c2e | 429 | |
718db961 LP |
430 | assert(bus); |
431 | assert(reply); | |
432 | assert(u); | |
52990c2e | 433 | |
718db961 LP |
434 | if (u->load_error != 0) |
435 | sd_bus_error_set_errno(&e, u->load_error); | |
52990c2e | 436 | |
718db961 | 437 | return sd_bus_message_append(reply, "(ss)", e.name, e.message); |
52990c2e ZJS |
438 | } |
439 | ||
88ced61b MC |
440 | static int bus_verify_manage_units_async_full( |
441 | Unit *u, | |
442 | const char *verb, | |
443 | int capability, | |
444 | const char *polkit_message, | |
05a98afd | 445 | bool interactive, |
88ced61b MC |
446 | sd_bus_message *call, |
447 | sd_bus_error *error) { | |
448 | ||
449 | const char *details[9] = { | |
450 | "unit", u->id, | |
451 | "verb", verb, | |
452 | }; | |
453 | ||
454 | if (polkit_message) { | |
455 | details[4] = "polkit.message"; | |
456 | details[5] = polkit_message; | |
457 | details[6] = "polkit.gettext_domain"; | |
458 | details[7] = GETTEXT_PACKAGE; | |
459 | } | |
460 | ||
05a98afd LP |
461 | return bus_verify_polkit_async( |
462 | call, | |
463 | capability, | |
464 | "org.freedesktop.systemd1.manage-units", | |
465 | details, | |
466 | interactive, | |
467 | UID_INVALID, | |
468 | &u->manager->polkit_registry, | |
469 | error); | |
88ced61b MC |
470 | } |
471 | ||
1d22e906 | 472 | int bus_unit_method_start_generic( |
1d22e906 LP |
473 | sd_bus_message *message, |
474 | Unit *u, | |
475 | JobType job_type, | |
476 | bool reload_if_possible, | |
477 | sd_bus_error *error) { | |
478 | ||
718db961 LP |
479 | const char *smode; |
480 | JobMode mode; | |
88ced61b MC |
481 | _cleanup_free_ char *verb = NULL; |
482 | static const char *const polkit_message_for_job[_JOB_TYPE_MAX] = { | |
483 | [JOB_START] = N_("Authentication is required to start '$(unit)'."), | |
484 | [JOB_STOP] = N_("Authentication is required to stop '$(unit)'."), | |
485 | [JOB_RELOAD] = N_("Authentication is required to reload '$(unit)'."), | |
486 | [JOB_RESTART] = N_("Authentication is required to restart '$(unit)'."), | |
487 | [JOB_TRY_RESTART] = N_("Authentication is required to restart '$(unit)'."), | |
488 | }; | |
718db961 | 489 | int r; |
9f39404c | 490 | |
718db961 | 491 | assert(message); |
9f39404c | 492 | assert(u); |
718db961 | 493 | assert(job_type >= 0 && job_type < _JOB_TYPE_MAX); |
9f39404c | 494 | |
61ea63f1 EV |
495 | r = mac_selinux_unit_access_check( |
496 | u, message, | |
94bd7323 EV |
497 | job_type_to_access_method(job_type), |
498 | error); | |
1d22e906 LP |
499 | if (r < 0) |
500 | return r; | |
501 | ||
718db961 LP |
502 | r = sd_bus_message_read(message, "s", &smode); |
503 | if (r < 0) | |
ebcf1f97 | 504 | return r; |
9f39404c | 505 | |
718db961 LP |
506 | mode = job_mode_from_string(smode); |
507 | if (mode < 0) | |
ebcf1f97 | 508 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Job mode %s invalid", smode); |
9f39404c | 509 | |
88ced61b | 510 | if (reload_if_possible) |
605405c6 | 511 | verb = strjoin("reload-or-", job_type_to_string(job_type)); |
88ced61b MC |
512 | else |
513 | verb = strdup(job_type_to_string(job_type)); | |
514 | if (!verb) | |
515 | return -ENOMEM; | |
516 | ||
517 | r = bus_verify_manage_units_async_full( | |
518 | u, | |
519 | verb, | |
520 | CAP_SYS_ADMIN, | |
521 | job_type < _JOB_TYPE_MAX ? polkit_message_for_job[job_type] : NULL, | |
05a98afd | 522 | true, |
88ced61b MC |
523 | message, |
524 | error); | |
1d22e906 LP |
525 | if (r < 0) |
526 | return r; | |
527 | if (r == 0) | |
528 | return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */ | |
529 | ||
19070062 | 530 | return bus_unit_queue_job(message, u, job_type, mode, reload_if_possible, error); |
9f39404c LP |
531 | } |
532 | ||
19070062 LP |
533 | static int method_start(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
534 | return bus_unit_method_start_generic(message, userdata, JOB_START, false, error); | |
718db961 | 535 | } |
b548631a | 536 | |
19070062 LP |
537 | static int method_stop(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
538 | return bus_unit_method_start_generic(message, userdata, JOB_STOP, false, error); | |
718db961 LP |
539 | } |
540 | ||
19070062 LP |
541 | static int method_reload(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
542 | return bus_unit_method_start_generic(message, userdata, JOB_RELOAD, false, error); | |
718db961 | 543 | } |
0a524ba7 | 544 | |
19070062 LP |
545 | static int method_restart(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
546 | return bus_unit_method_start_generic(message, userdata, JOB_RESTART, false, error); | |
718db961 | 547 | } |
8a0867d6 | 548 | |
19070062 LP |
549 | static int method_try_restart(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
550 | return bus_unit_method_start_generic(message, userdata, JOB_TRY_RESTART, false, error); | |
718db961 | 551 | } |
cad45ba1 | 552 | |
19070062 LP |
553 | static int method_reload_or_restart(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
554 | return bus_unit_method_start_generic(message, userdata, JOB_RESTART, true, error); | |
718db961 | 555 | } |
8a0867d6 | 556 | |
19070062 LP |
557 | static int method_reload_or_try_restart(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
558 | return bus_unit_method_start_generic(message, userdata, JOB_TRY_RESTART, true, error); | |
718db961 | 559 | } |
8a0867d6 | 560 | |
19070062 | 561 | int bus_unit_method_kill(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
718db961 LP |
562 | Unit *u = userdata; |
563 | const char *swho; | |
564 | int32_t signo; | |
565 | KillWho who; | |
566 | int r; | |
5632e374 | 567 | |
718db961 LP |
568 | assert(message); |
569 | assert(u); | |
cad45ba1 | 570 | |
1d22e906 | 571 | r = mac_selinux_unit_access_check(u, message, "stop", error); |
283868e1 SW |
572 | if (r < 0) |
573 | return r; | |
283868e1 | 574 | |
718db961 LP |
575 | r = sd_bus_message_read(message, "si", &swho, &signo); |
576 | if (r < 0) | |
ebcf1f97 | 577 | return r; |
718db961 LP |
578 | |
579 | if (isempty(swho)) | |
580 | who = KILL_ALL; | |
581 | else { | |
582 | who = kill_who_from_string(swho); | |
583 | if (who < 0) | |
ebcf1f97 | 584 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid who argument %s", swho); |
718db961 | 585 | } |
5632e374 | 586 | |
6eb7c172 | 587 | if (!SIGNAL_VALID(signo)) |
ebcf1f97 | 588 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Signal number out of range."); |
8e2af478 | 589 | |
88ced61b MC |
590 | r = bus_verify_manage_units_async_full( |
591 | u, | |
592 | "kill", | |
593 | CAP_KILL, | |
594 | N_("Authentication is required to kill '$(unit)'."), | |
05a98afd | 595 | true, |
88ced61b MC |
596 | message, |
597 | error); | |
ebcf1f97 LP |
598 | if (r < 0) |
599 | return r; | |
1d22e906 LP |
600 | if (r == 0) |
601 | return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */ | |
8e2af478 | 602 | |
ebcf1f97 | 603 | r = unit_kill(u, who, signo, error); |
718db961 | 604 | if (r < 0) |
ebcf1f97 | 605 | return r; |
8e2af478 | 606 | |
df2d202e | 607 | return sd_bus_reply_method_return(message, NULL); |
718db961 | 608 | } |
8e2af478 | 609 | |
19070062 | 610 | int bus_unit_method_reset_failed(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
718db961 | 611 | Unit *u = userdata; |
ebcf1f97 | 612 | int r; |
b548631a | 613 | |
718db961 LP |
614 | assert(message); |
615 | assert(u); | |
b548631a | 616 | |
1d22e906 | 617 | r = mac_selinux_unit_access_check(u, message, "reload", error); |
283868e1 SW |
618 | if (r < 0) |
619 | return r; | |
283868e1 | 620 | |
88ced61b MC |
621 | r = bus_verify_manage_units_async_full( |
622 | u, | |
623 | "reset-failed", | |
624 | CAP_SYS_ADMIN, | |
625 | N_("Authentication is required to reset the \"failed\" state of '$(unit)'."), | |
05a98afd | 626 | true, |
88ced61b MC |
627 | message, |
628 | error); | |
ebcf1f97 LP |
629 | if (r < 0) |
630 | return r; | |
1d22e906 LP |
631 | if (r == 0) |
632 | return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */ | |
b548631a | 633 | |
718db961 | 634 | unit_reset_failed(u); |
b548631a | 635 | |
df2d202e | 636 | return sd_bus_reply_method_return(message, NULL); |
ea430986 LP |
637 | } |
638 | ||
19070062 | 639 | int bus_unit_method_set_properties(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
718db961 LP |
640 | Unit *u = userdata; |
641 | int runtime, r; | |
ea430986 | 642 | |
ea430986 | 643 | assert(message); |
718db961 | 644 | assert(u); |
80fbf05e | 645 | |
1d22e906 | 646 | r = mac_selinux_unit_access_check(u, message, "start", error); |
283868e1 SW |
647 | if (r < 0) |
648 | return r; | |
283868e1 | 649 | |
718db961 LP |
650 | r = sd_bus_message_read(message, "b", &runtime); |
651 | if (r < 0) | |
ebcf1f97 | 652 | return r; |
2cccbca4 | 653 | |
88ced61b MC |
654 | r = bus_verify_manage_units_async_full( |
655 | u, | |
656 | "set-property", | |
657 | CAP_SYS_ADMIN, | |
658 | N_("Authentication is required to set properties on '$(unit)'."), | |
05a98afd | 659 | true, |
88ced61b MC |
660 | message, |
661 | error); | |
ebcf1f97 LP |
662 | if (r < 0) |
663 | return r; | |
1d22e906 LP |
664 | if (r == 0) |
665 | return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */ | |
cad45ba1 | 666 | |
ebcf1f97 | 667 | r = bus_unit_set_properties(u, message, runtime ? UNIT_RUNTIME : UNIT_PERSISTENT, true, error); |
718db961 | 668 | if (r < 0) |
ebcf1f97 | 669 | return r; |
2cccbca4 | 670 | |
df2d202e | 671 | return sd_bus_reply_method_return(message, NULL); |
718db961 | 672 | } |
2cccbca4 | 673 | |
05a98afd LP |
674 | int bus_unit_method_ref(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
675 | Unit *u = userdata; | |
676 | int r; | |
677 | ||
678 | assert(message); | |
679 | assert(u); | |
680 | ||
681 | r = mac_selinux_unit_access_check(u, message, "start", error); | |
682 | if (r < 0) | |
683 | return r; | |
684 | ||
685 | r = bus_verify_manage_units_async_full( | |
686 | u, | |
687 | "ref", | |
688 | CAP_SYS_ADMIN, | |
689 | NULL, | |
690 | false, | |
691 | message, | |
692 | error); | |
693 | if (r < 0) | |
694 | return r; | |
695 | if (r == 0) | |
696 | return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */ | |
697 | ||
698 | r = bus_unit_track_add_sender(u, message); | |
699 | if (r < 0) | |
700 | return r; | |
701 | ||
702 | return sd_bus_reply_method_return(message, NULL); | |
703 | } | |
704 | ||
705 | int bus_unit_method_unref(sd_bus_message *message, void *userdata, sd_bus_error *error) { | |
706 | Unit *u = userdata; | |
707 | int r; | |
708 | ||
709 | assert(message); | |
710 | assert(u); | |
711 | ||
712 | r = bus_unit_track_remove_sender(u, message); | |
713 | if (r == -EUNATCH) | |
714 | return sd_bus_error_setf(error, BUS_ERROR_NOT_REFERENCED, "Unit has not been referenced yet."); | |
715 | if (r < 0) | |
716 | return r; | |
717 | ||
718 | return sd_bus_reply_method_return(message, NULL); | |
719 | } | |
720 | ||
718db961 LP |
721 | const sd_bus_vtable bus_unit_vtable[] = { |
722 | SD_BUS_VTABLE_START(0), | |
723 | ||
556089dc LP |
724 | SD_BUS_PROPERTY("Id", "s", NULL, offsetof(Unit, id), SD_BUS_VTABLE_PROPERTY_CONST), |
725 | SD_BUS_PROPERTY("Names", "as", property_get_names, 0, SD_BUS_VTABLE_PROPERTY_CONST), | |
718db961 | 726 | SD_BUS_PROPERTY("Following", "s", property_get_following, 0, 0), |
556089dc | 727 | SD_BUS_PROPERTY("Requires", "as", property_get_dependencies, offsetof(Unit, dependencies[UNIT_REQUIRES]), SD_BUS_VTABLE_PROPERTY_CONST), |
556089dc | 728 | SD_BUS_PROPERTY("Requisite", "as", property_get_dependencies, offsetof(Unit, dependencies[UNIT_REQUISITE]), SD_BUS_VTABLE_PROPERTY_CONST), |
556089dc LP |
729 | SD_BUS_PROPERTY("Wants", "as", property_get_dependencies, offsetof(Unit, dependencies[UNIT_WANTS]), SD_BUS_VTABLE_PROPERTY_CONST), |
730 | SD_BUS_PROPERTY("BindsTo", "as", property_get_dependencies, offsetof(Unit, dependencies[UNIT_BINDS_TO]), SD_BUS_VTABLE_PROPERTY_CONST), | |
731 | SD_BUS_PROPERTY("PartOf", "as", property_get_dependencies, offsetof(Unit, dependencies[UNIT_PART_OF]), SD_BUS_VTABLE_PROPERTY_CONST), | |
732 | SD_BUS_PROPERTY("RequiredBy", "as", property_get_dependencies, offsetof(Unit, dependencies[UNIT_REQUIRED_BY]), SD_BUS_VTABLE_PROPERTY_CONST), | |
be7d9ff7 | 733 | SD_BUS_PROPERTY("RequisiteOf", "as", property_get_dependencies, offsetof(Unit, dependencies[UNIT_REQUISITE_OF]), SD_BUS_VTABLE_PROPERTY_CONST), |
556089dc LP |
734 | SD_BUS_PROPERTY("WantedBy", "as", property_get_dependencies, offsetof(Unit, dependencies[UNIT_WANTED_BY]), SD_BUS_VTABLE_PROPERTY_CONST), |
735 | SD_BUS_PROPERTY("BoundBy", "as", property_get_dependencies, offsetof(Unit, dependencies[UNIT_BOUND_BY]), SD_BUS_VTABLE_PROPERTY_CONST), | |
736 | SD_BUS_PROPERTY("ConsistsOf", "as", property_get_dependencies, offsetof(Unit, dependencies[UNIT_CONSISTS_OF]), SD_BUS_VTABLE_PROPERTY_CONST), | |
737 | SD_BUS_PROPERTY("Conflicts", "as", property_get_dependencies, offsetof(Unit, dependencies[UNIT_CONFLICTS]), SD_BUS_VTABLE_PROPERTY_CONST), | |
738 | SD_BUS_PROPERTY("ConflictedBy", "as", property_get_dependencies, offsetof(Unit, dependencies[UNIT_CONFLICTED_BY]), SD_BUS_VTABLE_PROPERTY_CONST), | |
739 | SD_BUS_PROPERTY("Before", "as", property_get_dependencies, offsetof(Unit, dependencies[UNIT_BEFORE]), SD_BUS_VTABLE_PROPERTY_CONST), | |
740 | SD_BUS_PROPERTY("After", "as", property_get_dependencies, offsetof(Unit, dependencies[UNIT_AFTER]), SD_BUS_VTABLE_PROPERTY_CONST), | |
741 | SD_BUS_PROPERTY("OnFailure", "as", property_get_dependencies, offsetof(Unit, dependencies[UNIT_ON_FAILURE]), SD_BUS_VTABLE_PROPERTY_CONST), | |
742 | SD_BUS_PROPERTY("Triggers", "as", property_get_dependencies, offsetof(Unit, dependencies[UNIT_TRIGGERS]), SD_BUS_VTABLE_PROPERTY_CONST), | |
743 | SD_BUS_PROPERTY("TriggeredBy", "as", property_get_dependencies, offsetof(Unit, dependencies[UNIT_TRIGGERED_BY]), SD_BUS_VTABLE_PROPERTY_CONST), | |
744 | SD_BUS_PROPERTY("PropagatesReloadTo", "as", property_get_dependencies, offsetof(Unit, dependencies[UNIT_PROPAGATES_RELOAD_TO]), SD_BUS_VTABLE_PROPERTY_CONST), | |
745 | SD_BUS_PROPERTY("ReloadPropagatedFrom", "as", property_get_dependencies, offsetof(Unit, dependencies[UNIT_RELOAD_PROPAGATED_FROM]), SD_BUS_VTABLE_PROPERTY_CONST), | |
746 | SD_BUS_PROPERTY("JoinsNamespaceOf", "as", property_get_dependencies, offsetof(Unit, dependencies[UNIT_JOINS_NAMESPACE_OF]), SD_BUS_VTABLE_PROPERTY_CONST), | |
99c14018 | 747 | SD_BUS_PROPERTY("RequiresMountsFor", "as", property_get_requires_mounts_for, offsetof(Unit, requires_mounts_for), SD_BUS_VTABLE_PROPERTY_CONST), |
556089dc LP |
748 | SD_BUS_PROPERTY("Documentation", "as", NULL, offsetof(Unit, documentation), SD_BUS_VTABLE_PROPERTY_CONST), |
749 | SD_BUS_PROPERTY("Description", "s", property_get_description, 0, SD_BUS_VTABLE_PROPERTY_CONST), | |
750 | SD_BUS_PROPERTY("LoadState", "s", property_get_load_state, offsetof(Unit, load_state), SD_BUS_VTABLE_PROPERTY_CONST), | |
718db961 LP |
751 | SD_BUS_PROPERTY("ActiveState", "s", property_get_active_state, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE), |
752 | SD_BUS_PROPERTY("SubState", "s", property_get_sub_state, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE), | |
556089dc LP |
753 | SD_BUS_PROPERTY("FragmentPath", "s", NULL, offsetof(Unit, fragment_path), SD_BUS_VTABLE_PROPERTY_CONST), |
754 | SD_BUS_PROPERTY("SourcePath", "s", NULL, offsetof(Unit, source_path), SD_BUS_VTABLE_PROPERTY_CONST), | |
755 | SD_BUS_PROPERTY("DropInPaths", "as", NULL, offsetof(Unit, dropin_paths), SD_BUS_VTABLE_PROPERTY_CONST), | |
718db961 | 756 | SD_BUS_PROPERTY("UnitFileState", "s", property_get_unit_file_state, 0, 0), |
d2dc52db | 757 | SD_BUS_PROPERTY("UnitFilePreset", "s", property_get_unit_file_preset, 0, 0), |
a483fb59 | 758 | BUS_PROPERTY_DUAL_TIMESTAMP("StateChangeTimestamp", offsetof(Unit, state_change_timestamp), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE), |
718db961 LP |
759 | BUS_PROPERTY_DUAL_TIMESTAMP("InactiveExitTimestamp", offsetof(Unit, inactive_exit_timestamp), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE), |
760 | BUS_PROPERTY_DUAL_TIMESTAMP("ActiveEnterTimestamp", offsetof(Unit, active_enter_timestamp), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE), | |
761 | BUS_PROPERTY_DUAL_TIMESTAMP("ActiveExitTimestamp", offsetof(Unit, active_exit_timestamp), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE), | |
762 | BUS_PROPERTY_DUAL_TIMESTAMP("InactiveEnterTimestamp", offsetof(Unit, inactive_enter_timestamp), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE), | |
556089dc LP |
763 | SD_BUS_PROPERTY("CanStart", "b", property_get_can_start, 0, SD_BUS_VTABLE_PROPERTY_CONST), |
764 | SD_BUS_PROPERTY("CanStop", "b", property_get_can_stop, 0, SD_BUS_VTABLE_PROPERTY_CONST), | |
765 | SD_BUS_PROPERTY("CanReload", "b", property_get_can_reload, 0, SD_BUS_VTABLE_PROPERTY_CONST), | |
766 | SD_BUS_PROPERTY("CanIsolate", "b", property_get_can_isolate, 0, SD_BUS_VTABLE_PROPERTY_CONST), | |
718db961 | 767 | SD_BUS_PROPERTY("Job", "(uo)", property_get_job, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE), |
556089dc LP |
768 | SD_BUS_PROPERTY("StopWhenUnneeded", "b", bus_property_get_bool, offsetof(Unit, stop_when_unneeded), SD_BUS_VTABLE_PROPERTY_CONST), |
769 | SD_BUS_PROPERTY("RefuseManualStart", "b", bus_property_get_bool, offsetof(Unit, refuse_manual_start), SD_BUS_VTABLE_PROPERTY_CONST), | |
770 | SD_BUS_PROPERTY("RefuseManualStop", "b", bus_property_get_bool, offsetof(Unit, refuse_manual_stop), SD_BUS_VTABLE_PROPERTY_CONST), | |
771 | SD_BUS_PROPERTY("AllowIsolate", "b", bus_property_get_bool, offsetof(Unit, allow_isolate), SD_BUS_VTABLE_PROPERTY_CONST), | |
772 | SD_BUS_PROPERTY("DefaultDependencies", "b", bus_property_get_bool, offsetof(Unit, default_dependencies), SD_BUS_VTABLE_PROPERTY_CONST), | |
773 | SD_BUS_PROPERTY("OnFailureJobMode", "s", property_get_job_mode, offsetof(Unit, on_failure_job_mode), SD_BUS_VTABLE_PROPERTY_CONST), | |
774 | SD_BUS_PROPERTY("IgnoreOnIsolate", "b", bus_property_get_bool, offsetof(Unit, ignore_on_isolate), SD_BUS_VTABLE_PROPERTY_CONST), | |
556089dc LP |
775 | SD_BUS_PROPERTY("NeedDaemonReload", "b", property_get_need_daemon_reload, 0, SD_BUS_VTABLE_PROPERTY_CONST), |
776 | SD_BUS_PROPERTY("JobTimeoutUSec", "t", bus_property_get_usec, offsetof(Unit, job_timeout), SD_BUS_VTABLE_PROPERTY_CONST), | |
a2df3ea4 | 777 | SD_BUS_PROPERTY("JobRunningTimeoutUSec", "t", bus_property_get_usec, offsetof(Unit, job_running_timeout), SD_BUS_VTABLE_PROPERTY_CONST), |
87a47f99 | 778 | SD_BUS_PROPERTY("JobTimeoutAction", "s", property_get_emergency_action, offsetof(Unit, job_timeout_action), SD_BUS_VTABLE_PROPERTY_CONST), |
f189ab18 | 779 | SD_BUS_PROPERTY("JobTimeoutRebootArgument", "s", NULL, offsetof(Unit, job_timeout_reboot_arg), SD_BUS_VTABLE_PROPERTY_CONST), |
718db961 | 780 | SD_BUS_PROPERTY("ConditionResult", "b", bus_property_get_bool, offsetof(Unit, condition_result), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE), |
59fccdc5 | 781 | SD_BUS_PROPERTY("AssertResult", "b", bus_property_get_bool, offsetof(Unit, assert_result), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE), |
718db961 | 782 | BUS_PROPERTY_DUAL_TIMESTAMP("ConditionTimestamp", offsetof(Unit, condition_timestamp), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE), |
59fccdc5 LP |
783 | BUS_PROPERTY_DUAL_TIMESTAMP("AssertTimestamp", offsetof(Unit, assert_timestamp), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE), |
784 | SD_BUS_PROPERTY("Conditions", "a(sbbsi)", property_get_conditions, offsetof(Unit, conditions), 0), | |
785 | SD_BUS_PROPERTY("Asserts", "a(sbbsi)", property_get_conditions, offsetof(Unit, asserts), 0), | |
556089dc LP |
786 | SD_BUS_PROPERTY("LoadError", "(ss)", property_get_load_error, 0, SD_BUS_VTABLE_PROPERTY_CONST), |
787 | SD_BUS_PROPERTY("Transient", "b", bus_property_get_bool, offsetof(Unit, transient), SD_BUS_VTABLE_PROPERTY_CONST), | |
f5869324 | 788 | SD_BUS_PROPERTY("Perpetual", "b", bus_property_get_bool, offsetof(Unit, perpetual), SD_BUS_VTABLE_PROPERTY_CONST), |
c075f5fc | 789 | SD_BUS_PROPERTY("StartLimitIntervalUSec", "t", bus_property_get_usec, offsetof(Unit, start_limit.interval), SD_BUS_VTABLE_PROPERTY_CONST), |
6bf0f408 | 790 | SD_BUS_PROPERTY("StartLimitBurst", "u", bus_property_get_unsigned, offsetof(Unit, start_limit.burst), SD_BUS_VTABLE_PROPERTY_CONST), |
87a47f99 | 791 | SD_BUS_PROPERTY("StartLimitAction", "s", property_get_emergency_action, offsetof(Unit, start_limit_action), SD_BUS_VTABLE_PROPERTY_CONST), |
53c35a76 | 792 | SD_BUS_PROPERTY("FailureAction", "s", property_get_emergency_action, offsetof(Unit, failure_action), SD_BUS_VTABLE_PROPERTY_CONST), |
e7dfbb4e | 793 | SD_BUS_PROPERTY("SuccessAction", "s", property_get_emergency_action, offsetof(Unit, success_action), SD_BUS_VTABLE_PROPERTY_CONST), |
6bf0f408 | 794 | SD_BUS_PROPERTY("RebootArgument", "s", NULL, offsetof(Unit, reboot_arg), SD_BUS_VTABLE_PROPERTY_CONST), |
4b58153d | 795 | SD_BUS_PROPERTY("InvocationID", "ay", bus_property_get_id128, offsetof(Unit, invocation_id), 0), |
5afe510c | 796 | SD_BUS_PROPERTY("CollectMode", "s", property_get_collect_mode, offsetof(Unit, collect_mode), 0), |
718db961 | 797 | |
1d22e906 LP |
798 | SD_BUS_METHOD("Start", "s", "o", method_start, SD_BUS_VTABLE_UNPRIVILEGED), |
799 | SD_BUS_METHOD("Stop", "s", "o", method_stop, SD_BUS_VTABLE_UNPRIVILEGED), | |
800 | SD_BUS_METHOD("Reload", "s", "o", method_reload, SD_BUS_VTABLE_UNPRIVILEGED), | |
801 | SD_BUS_METHOD("Restart", "s", "o", method_restart, SD_BUS_VTABLE_UNPRIVILEGED), | |
802 | SD_BUS_METHOD("TryRestart", "s", "o", method_try_restart, SD_BUS_VTABLE_UNPRIVILEGED), | |
803 | SD_BUS_METHOD("ReloadOrRestart", "s", "o", method_reload_or_restart, SD_BUS_VTABLE_UNPRIVILEGED), | |
804 | SD_BUS_METHOD("ReloadOrTryRestart", "s", "o", method_reload_or_try_restart, SD_BUS_VTABLE_UNPRIVILEGED), | |
805 | SD_BUS_METHOD("Kill", "si", NULL, bus_unit_method_kill, SD_BUS_VTABLE_UNPRIVILEGED), | |
806 | SD_BUS_METHOD("ResetFailed", NULL, NULL, bus_unit_method_reset_failed, SD_BUS_VTABLE_UNPRIVILEGED), | |
807 | SD_BUS_METHOD("SetProperties", "ba(sv)", NULL, bus_unit_method_set_properties, SD_BUS_VTABLE_UNPRIVILEGED), | |
05a98afd LP |
808 | SD_BUS_METHOD("Ref", NULL, NULL, bus_unit_method_ref, SD_BUS_VTABLE_UNPRIVILEGED), |
809 | SD_BUS_METHOD("Unref", NULL, NULL, bus_unit_method_unref, SD_BUS_VTABLE_UNPRIVILEGED), | |
718db961 | 810 | |
51d73fd9 LP |
811 | /* Obsolete properties or obsolete alias names */ |
812 | SD_BUS_PROPERTY("RequiresOverridable", "as", property_get_obsolete_dependencies, 0, SD_BUS_VTABLE_HIDDEN), | |
813 | SD_BUS_PROPERTY("RequisiteOverridable", "as", property_get_obsolete_dependencies, 0, SD_BUS_VTABLE_HIDDEN), | |
814 | SD_BUS_PROPERTY("RequiredByOverridable", "as", property_get_obsolete_dependencies, 0, SD_BUS_VTABLE_HIDDEN), | |
815 | SD_BUS_PROPERTY("RequisiteOfOverridable", "as", property_get_obsolete_dependencies, 0, SD_BUS_VTABLE_HIDDEN), | |
816 | SD_BUS_PROPERTY("StartLimitInterval", "t", bus_property_get_usec, offsetof(Unit, start_limit.interval), SD_BUS_VTABLE_PROPERTY_CONST|SD_BUS_VTABLE_HIDDEN), | |
c075f5fc | 817 | SD_BUS_PROPERTY("StartLimitIntervalSec", "t", bus_property_get_usec, offsetof(Unit, start_limit.interval), SD_BUS_VTABLE_PROPERTY_CONST|SD_BUS_VTABLE_HIDDEN), |
718db961 LP |
818 | SD_BUS_VTABLE_END |
819 | }; | |
2cccbca4 | 820 | |
718db961 LP |
821 | static int property_get_slice( |
822 | sd_bus *bus, | |
823 | const char *path, | |
824 | const char *interface, | |
825 | const char *property, | |
826 | sd_bus_message *reply, | |
ebcf1f97 LP |
827 | void *userdata, |
828 | sd_bus_error *error) { | |
2cccbca4 | 829 | |
718db961 | 830 | Unit *u = userdata; |
2cccbca4 | 831 | |
718db961 LP |
832 | assert(bus); |
833 | assert(reply); | |
834 | assert(u); | |
2cccbca4 | 835 | |
718db961 LP |
836 | return sd_bus_message_append(reply, "s", unit_slice_name(u)); |
837 | } | |
2cccbca4 | 838 | |
934277fe LP |
839 | static int property_get_current_memory( |
840 | sd_bus *bus, | |
841 | const char *path, | |
842 | const char *interface, | |
843 | const char *property, | |
844 | sd_bus_message *reply, | |
845 | void *userdata, | |
846 | sd_bus_error *error) { | |
847 | ||
934277fe | 848 | uint64_t sz = (uint64_t) -1; |
5ad096b3 | 849 | Unit *u = userdata; |
934277fe LP |
850 | int r; |
851 | ||
852 | assert(bus); | |
853 | assert(reply); | |
854 | assert(u); | |
855 | ||
5ad096b3 LP |
856 | r = unit_get_memory_current(u, &sz); |
857 | if (r < 0 && r != -ENODATA) | |
f2341e0a | 858 | log_unit_warning_errno(u, r, "Failed to get memory.usage_in_bytes attribute: %m"); |
934277fe | 859 | |
5ad096b3 LP |
860 | return sd_bus_message_append(reply, "t", sz); |
861 | } | |
934277fe | 862 | |
03a7b521 LP |
863 | static int property_get_current_tasks( |
864 | sd_bus *bus, | |
865 | const char *path, | |
866 | const char *interface, | |
867 | const char *property, | |
868 | sd_bus_message *reply, | |
869 | void *userdata, | |
870 | sd_bus_error *error) { | |
871 | ||
872 | uint64_t cn = (uint64_t) -1; | |
873 | Unit *u = userdata; | |
874 | int r; | |
875 | ||
876 | assert(bus); | |
877 | assert(reply); | |
878 | assert(u); | |
879 | ||
880 | r = unit_get_tasks_current(u, &cn); | |
881 | if (r < 0 && r != -ENODATA) | |
882 | log_unit_warning_errno(u, r, "Failed to get pids.current attribute: %m"); | |
883 | ||
884 | return sd_bus_message_append(reply, "t", cn); | |
885 | } | |
886 | ||
5ad096b3 LP |
887 | static int property_get_cpu_usage( |
888 | sd_bus *bus, | |
889 | const char *path, | |
890 | const char *interface, | |
891 | const char *property, | |
892 | sd_bus_message *reply, | |
893 | void *userdata, | |
894 | sd_bus_error *error) { | |
934277fe | 895 | |
5ad096b3 LP |
896 | nsec_t ns = (nsec_t) -1; |
897 | Unit *u = userdata; | |
898 | int r; | |
899 | ||
900 | assert(bus); | |
901 | assert(reply); | |
902 | assert(u); | |
903 | ||
904 | r = unit_get_cpu_usage(u, &ns); | |
905 | if (r < 0 && r != -ENODATA) | |
f2341e0a | 906 | log_unit_warning_errno(u, r, "Failed to get cpuacct.usage attribute: %m"); |
5ad096b3 LP |
907 | |
908 | return sd_bus_message_append(reply, "t", ns); | |
934277fe LP |
909 | } |
910 | ||
98bac605 LP |
911 | static int property_get_cgroup( |
912 | sd_bus *bus, | |
913 | const char *path, | |
914 | const char *interface, | |
915 | const char *property, | |
916 | sd_bus_message *reply, | |
917 | void *userdata, | |
918 | sd_bus_error *error) { | |
919 | ||
920 | Unit *u = userdata; | |
79a60375 | 921 | const char *t = NULL; |
98bac605 LP |
922 | |
923 | assert(bus); | |
924 | assert(reply); | |
925 | assert(u); | |
926 | ||
927 | /* Three cases: a) u->cgroup_path is NULL, in which case the | |
928 | * unit has no control group, which we report as the empty | |
929 | * string. b) u->cgroup_path is the empty string, which | |
930 | * indicates the root cgroup, which we report as "/". c) all | |
931 | * other cases we report as-is. */ | |
932 | ||
933 | if (u->cgroup_path) | |
945403e6 | 934 | t = empty_to_root(u->cgroup_path); |
98bac605 LP |
935 | |
936 | return sd_bus_message_append(reply, "s", t); | |
937 | } | |
938 | ||
291d565a LP |
939 | static int append_process(sd_bus_message *reply, const char *p, pid_t pid, Set *pids) { |
940 | _cleanup_free_ char *buf = NULL, *cmdline = NULL; | |
941 | int r; | |
942 | ||
943 | assert(reply); | |
944 | assert(pid > 0); | |
945 | ||
946 | r = set_put(pids, PID_TO_PTR(pid)); | |
4c701096 | 947 | if (IN_SET(r, 0, -EEXIST)) |
291d565a LP |
948 | return 0; |
949 | if (r < 0) | |
950 | return r; | |
951 | ||
952 | if (!p) { | |
953 | r = cg_pid_get_path(SYSTEMD_CGROUP_CONTROLLER, pid, &buf); | |
954 | if (r == -ESRCH) | |
955 | return 0; | |
956 | if (r < 0) | |
957 | return r; | |
958 | ||
959 | p = buf; | |
960 | } | |
961 | ||
962 | (void) get_process_cmdline(pid, 0, true, &cmdline); | |
963 | ||
964 | return sd_bus_message_append(reply, | |
965 | "(sus)", | |
966 | p, | |
967 | (uint32_t) pid, | |
968 | cmdline); | |
969 | } | |
970 | ||
971 | static int append_cgroup(sd_bus_message *reply, const char *p, Set *pids) { | |
972 | _cleanup_closedir_ DIR *d = NULL; | |
973 | _cleanup_fclose_ FILE *f = NULL; | |
974 | int r; | |
975 | ||
976 | assert(reply); | |
977 | assert(p); | |
978 | ||
979 | r = cg_enumerate_processes(SYSTEMD_CGROUP_CONTROLLER, p, &f); | |
980 | if (r == ENOENT) | |
981 | return 0; | |
982 | if (r < 0) | |
983 | return r; | |
984 | ||
985 | for (;;) { | |
986 | pid_t pid; | |
987 | ||
988 | r = cg_read_pid(f, &pid); | |
989 | if (r < 0) | |
990 | return r; | |
991 | if (r == 0) | |
992 | break; | |
993 | ||
994 | if (is_kernel_thread(pid) > 0) | |
995 | continue; | |
996 | ||
997 | r = append_process(reply, p, pid, pids); | |
998 | if (r < 0) | |
999 | return r; | |
1000 | } | |
1001 | ||
1002 | r = cg_enumerate_subgroups(SYSTEMD_CGROUP_CONTROLLER, p, &d); | |
1003 | if (r == -ENOENT) | |
1004 | return 0; | |
1005 | if (r < 0) | |
1006 | return r; | |
1007 | ||
1008 | for (;;) { | |
1009 | _cleanup_free_ char *g = NULL, *j = NULL; | |
1010 | ||
1011 | r = cg_read_subgroup(d, &g); | |
1012 | if (r < 0) | |
1013 | return r; | |
1014 | if (r == 0) | |
1015 | break; | |
1016 | ||
605405c6 | 1017 | j = strjoin(p, "/", g); |
291d565a LP |
1018 | if (!j) |
1019 | return -ENOMEM; | |
1020 | ||
1021 | r = append_cgroup(reply, j, pids); | |
1022 | if (r < 0) | |
1023 | return r; | |
1024 | } | |
1025 | ||
1026 | return 0; | |
1027 | } | |
1028 | ||
1029 | int bus_unit_method_get_processes(sd_bus_message *message, void *userdata, sd_bus_error *error) { | |
1030 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; | |
1031 | _cleanup_(set_freep) Set *pids = NULL; | |
291d565a LP |
1032 | Unit *u = userdata; |
1033 | pid_t pid; | |
1034 | int r; | |
1035 | ||
1036 | assert(message); | |
1037 | ||
807fa5d9 LP |
1038 | r = mac_selinux_unit_access_check(u, message, "status", error); |
1039 | if (r < 0) | |
1040 | return r; | |
1041 | ||
291d565a LP |
1042 | pids = set_new(NULL); |
1043 | if (!pids) | |
1044 | return -ENOMEM; | |
1045 | ||
1046 | r = sd_bus_message_new_method_return(message, &reply); | |
1047 | if (r < 0) | |
1048 | return r; | |
1049 | ||
1050 | r = sd_bus_message_open_container(reply, 'a', "(sus)"); | |
1051 | if (r < 0) | |
1052 | return r; | |
1053 | ||
1054 | if (u->cgroup_path) { | |
1055 | r = append_cgroup(reply, u->cgroup_path, pids); | |
1056 | if (r < 0) | |
1057 | return r; | |
1058 | } | |
1059 | ||
1060 | /* The main and control pids might live outside of the cgroup, hence fetch them separately */ | |
1061 | pid = unit_main_pid(u); | |
1062 | if (pid > 0) { | |
1063 | r = append_process(reply, NULL, pid, pids); | |
1064 | if (r < 0) | |
1065 | return r; | |
1066 | } | |
1067 | ||
1068 | pid = unit_control_pid(u); | |
1069 | if (pid > 0) { | |
1070 | r = append_process(reply, NULL, pid, pids); | |
1071 | if (r < 0) | |
1072 | return r; | |
1073 | } | |
1074 | ||
1075 | r = sd_bus_message_close_container(reply); | |
1076 | if (r < 0) | |
1077 | return r; | |
1078 | ||
1079 | return sd_bus_send(NULL, reply, NULL); | |
1080 | } | |
1081 | ||
906c06f6 DM |
1082 | static int property_get_ip_counter( |
1083 | sd_bus *bus, | |
1084 | const char *path, | |
1085 | const char *interface, | |
1086 | const char *property, | |
1087 | sd_bus_message *reply, | |
1088 | void *userdata, | |
1089 | sd_bus_error *error) { | |
1090 | ||
1091 | CGroupIPAccountingMetric metric; | |
1092 | uint64_t value = (uint64_t) -1; | |
1093 | Unit *u = userdata; | |
1094 | ||
1095 | assert(bus); | |
1096 | assert(reply); | |
1097 | assert(property); | |
1098 | assert(u); | |
1099 | ||
1100 | if (streq(property, "IPIngressBytes")) | |
1101 | metric = CGROUP_IP_INGRESS_BYTES; | |
1102 | else if (streq(property, "IPIngressPackets")) | |
1103 | metric = CGROUP_IP_INGRESS_PACKETS; | |
1104 | else if (streq(property, "IPEgressBytes")) | |
1105 | metric = CGROUP_IP_EGRESS_BYTES; | |
1106 | else { | |
1107 | assert(streq(property, "IPEgressPackets")); | |
1108 | metric = CGROUP_IP_EGRESS_PACKETS; | |
1109 | } | |
1110 | ||
1111 | (void) unit_get_ip_accounting(u, metric, &value); | |
1112 | return sd_bus_message_append(reply, "t", value); | |
1113 | } | |
1114 | ||
6592b975 LP |
1115 | int bus_unit_method_attach_processes(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
1116 | ||
1117 | _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL; | |
1118 | _cleanup_(set_freep) Set *pids = NULL; | |
1119 | Unit *u = userdata; | |
1120 | const char *path; | |
1121 | int r; | |
1122 | ||
1123 | assert(message); | |
1124 | ||
1125 | /* This migrates the processes with the specified PIDs into the cgroup of this unit, optionally below a | |
1126 | * specified cgroup path. Obviously this only works for units that actually maintain a cgroup | |
1127 | * representation. If a process is already in the cgroup no operation is executed – in this case the specified | |
1128 | * subcgroup path has no effect! */ | |
1129 | ||
1130 | r = mac_selinux_unit_access_check(u, message, "start", error); | |
1131 | if (r < 0) | |
1132 | return r; | |
1133 | ||
1134 | r = sd_bus_message_read(message, "s", &path); | |
1135 | if (r < 0) | |
1136 | return r; | |
1137 | ||
1138 | path = empty_to_null(path); | |
1139 | if (path) { | |
1140 | if (!path_is_absolute(path)) | |
1141 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Control group path is not absolute: %s", path); | |
1142 | ||
1143 | if (!path_is_normalized(path)) | |
1144 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Control group path is not normalized: %s", path); | |
1145 | } | |
1146 | ||
1147 | if (!unit_cgroup_delegate(u)) | |
1148 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Process migration not available on non-delegated units."); | |
1149 | ||
1150 | if (UNIT_IS_INACTIVE_OR_FAILED(unit_active_state(u))) | |
1151 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Unit is not active, refusing."); | |
1152 | ||
1153 | r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_EUID|SD_BUS_CREDS_PID, &creds); | |
1154 | if (r < 0) | |
1155 | return r; | |
1156 | ||
1157 | r = sd_bus_message_enter_container(message, 'a', "u"); | |
1158 | if (r < 0) | |
1159 | return r; | |
1160 | for (;;) { | |
1161 | uid_t process_uid, sender_uid; | |
1162 | uint32_t upid; | |
1163 | pid_t pid; | |
1164 | ||
1165 | r = sd_bus_message_read(message, "u", &upid); | |
1166 | if (r < 0) | |
1167 | return r; | |
1168 | if (r == 0) | |
1169 | break; | |
1170 | ||
1171 | if (upid == 0) { | |
1172 | r = sd_bus_creds_get_pid(creds, &pid); | |
1173 | if (r < 0) | |
1174 | return r; | |
1175 | } else | |
1176 | pid = (uid_t) upid; | |
1177 | ||
1178 | /* Filter out duplicates */ | |
1179 | if (set_contains(pids, PID_TO_PTR(pid))) | |
1180 | continue; | |
1181 | ||
1182 | /* Check if this process is suitable for attaching to this unit */ | |
1183 | r = unit_pid_attachable(u, pid, error); | |
1184 | if (r < 0) | |
1185 | return r; | |
1186 | ||
1187 | /* Let's query the sender's UID, so that we can make our security decisions */ | |
1188 | r = sd_bus_creds_get_euid(creds, &sender_uid); | |
1189 | if (r < 0) | |
1190 | return r; | |
1191 | ||
1192 | /* Let's validate security: if the sender is root, then all is OK. If the sender is is any other unit, | |
1193 | * then the process' UID and the target unit's UID have to match the sender's UID */ | |
1194 | if (sender_uid != 0 && sender_uid != getuid()) { | |
1195 | r = get_process_uid(pid, &process_uid); | |
1196 | if (r < 0) | |
1197 | return sd_bus_error_set_errnof(error, r, "Failed to retrieve process UID: %m"); | |
1198 | ||
1199 | if (process_uid != sender_uid) | |
1200 | return sd_bus_error_setf(error, SD_BUS_ERROR_ACCESS_DENIED, "Process " PID_FMT " not owned by client's UID. Refusing.", pid); | |
1201 | if (process_uid != u->ref_uid) | |
1202 | return sd_bus_error_setf(error, SD_BUS_ERROR_ACCESS_DENIED, "Process " PID_FMT " not owned by target unit's UID. Refusing.", pid); | |
1203 | } | |
1204 | ||
1205 | if (!pids) { | |
1206 | pids = set_new(NULL); | |
1207 | if (!pids) | |
1208 | return -ENOMEM; | |
1209 | } | |
1210 | ||
1211 | r = set_put(pids, PID_TO_PTR(pid)); | |
1212 | if (r < 0) | |
1213 | return r; | |
1214 | } | |
1215 | ||
1216 | r = sd_bus_message_exit_container(message); | |
1217 | if (r < 0) | |
1218 | return r; | |
1219 | ||
1220 | r = unit_attach_pids_to_cgroup(u, pids, path); | |
1221 | if (r < 0) | |
1222 | return sd_bus_error_set_errnof(error, r, "Failed to attach processes to control group: %m"); | |
1223 | ||
1224 | return sd_bus_reply_method_return(message, NULL); | |
1225 | } | |
1226 | ||
718db961 LP |
1227 | const sd_bus_vtable bus_unit_cgroup_vtable[] = { |
1228 | SD_BUS_VTABLE_START(0), | |
1229 | SD_BUS_PROPERTY("Slice", "s", property_get_slice, 0, 0), | |
98bac605 | 1230 | SD_BUS_PROPERTY("ControlGroup", "s", property_get_cgroup, 0, 0), |
934277fe | 1231 | SD_BUS_PROPERTY("MemoryCurrent", "t", property_get_current_memory, 0, 0), |
5ad096b3 | 1232 | SD_BUS_PROPERTY("CPUUsageNSec", "t", property_get_cpu_usage, 0, 0), |
03a7b521 | 1233 | SD_BUS_PROPERTY("TasksCurrent", "t", property_get_current_tasks, 0, 0), |
906c06f6 DM |
1234 | SD_BUS_PROPERTY("IPIngressBytes", "t", property_get_ip_counter, 0, 0), |
1235 | SD_BUS_PROPERTY("IPIngressPackets", "t", property_get_ip_counter, 0, 0), | |
1236 | SD_BUS_PROPERTY("IPEgressBytes", "t", property_get_ip_counter, 0, 0), | |
1237 | SD_BUS_PROPERTY("IPEgressPackets", "t", property_get_ip_counter, 0, 0), | |
291d565a | 1238 | SD_BUS_METHOD("GetProcesses", NULL, "a(sus)", bus_unit_method_get_processes, SD_BUS_VTABLE_UNPRIVILEGED), |
6592b975 | 1239 | SD_BUS_METHOD("AttachProcesses", "sau", NULL, bus_unit_method_attach_processes, SD_BUS_VTABLE_UNPRIVILEGED), |
718db961 LP |
1240 | SD_BUS_VTABLE_END |
1241 | }; | |
2cccbca4 | 1242 | |
8f8f05a9 | 1243 | static int send_new_signal(sd_bus *bus, void *userdata) { |
4afd3348 | 1244 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL; |
718db961 LP |
1245 | _cleanup_free_ char *p = NULL; |
1246 | Unit *u = userdata; | |
1247 | int r; | |
2cccbca4 | 1248 | |
718db961 LP |
1249 | assert(bus); |
1250 | assert(u); | |
2cccbca4 | 1251 | |
718db961 | 1252 | p = unit_dbus_path(u); |
e861098b | 1253 | if (!p) |
718db961 | 1254 | return -ENOMEM; |
2cccbca4 | 1255 | |
718db961 LP |
1256 | r = sd_bus_message_new_signal( |
1257 | bus, | |
151b9b96 | 1258 | &m, |
718db961 LP |
1259 | "/org/freedesktop/systemd1", |
1260 | "org.freedesktop.systemd1.Manager", | |
151b9b96 | 1261 | "UnitNew"); |
718db961 LP |
1262 | if (r < 0) |
1263 | return r; | |
2cccbca4 | 1264 | |
718db961 LP |
1265 | r = sd_bus_message_append(m, "so", u->id, p); |
1266 | if (r < 0) | |
1267 | return r; | |
2cccbca4 | 1268 | |
8f8f05a9 | 1269 | return sd_bus_send(bus, m, NULL); |
718db961 | 1270 | } |
2cccbca4 | 1271 | |
8f8f05a9 | 1272 | static int send_changed_signal(sd_bus *bus, void *userdata) { |
718db961 LP |
1273 | _cleanup_free_ char *p = NULL; |
1274 | Unit *u = userdata; | |
1275 | int r; | |
2cccbca4 | 1276 | |
718db961 LP |
1277 | assert(bus); |
1278 | assert(u); | |
2cccbca4 | 1279 | |
718db961 | 1280 | p = unit_dbus_path(u); |
9ceefc81 | 1281 | if (!p) |
718db961 | 1282 | return -ENOMEM; |
2cccbca4 | 1283 | |
718db961 LP |
1284 | /* Send a properties changed signal. First for the specific |
1285 | * type, then for the generic unit. The clients may rely on | |
1286 | * this order to get atomic behavior if needed. */ | |
ea430986 | 1287 | |
aec8de63 LP |
1288 | r = sd_bus_emit_properties_changed_strv( |
1289 | bus, p, | |
21b735e7 | 1290 | unit_dbus_interface_from_type(u->type), |
aec8de63 | 1291 | NULL); |
fe7f06f1 | 1292 | if (r < 0) |
aec8de63 | 1293 | return r; |
80fbf05e | 1294 | |
fe7f06f1 | 1295 | return sd_bus_emit_properties_changed_strv( |
718db961 LP |
1296 | bus, p, |
1297 | "org.freedesktop.systemd1.Unit", | |
718db961 | 1298 | NULL); |
ea430986 LP |
1299 | } |
1300 | ||
c1e1601e | 1301 | void bus_unit_send_change_signal(Unit *u) { |
b170dd80 | 1302 | int r; |
c1e1601e | 1303 | assert(u); |
c1e1601e | 1304 | |
ac155bb8 | 1305 | if (u->in_dbus_queue) { |
71fda00f | 1306 | LIST_REMOVE(dbus_queue, u->manager->dbus_unit_queue, u); |
ac155bb8 | 1307 | u->in_dbus_queue = false; |
c0bd0cf7 | 1308 | } |
c1e1601e | 1309 | |
ac155bb8 | 1310 | if (!u->id) |
04ade7d2 LP |
1311 | return; |
1312 | ||
ae572acd | 1313 | r = bus_foreach_bus(u->manager, u->bus_track, u->sent_dbus_new_signal ? send_changed_signal : send_new_signal, u); |
718db961 | 1314 | if (r < 0) |
f2341e0a | 1315 | log_unit_debug_errno(u, r, "Failed to send unit change signal for %s: %m", u->id); |
c1e1601e | 1316 | |
718db961 LP |
1317 | u->sent_dbus_new_signal = true; |
1318 | } | |
c4e2ceae | 1319 | |
8f8f05a9 | 1320 | static int send_removed_signal(sd_bus *bus, void *userdata) { |
4afd3348 | 1321 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL; |
718db961 LP |
1322 | _cleanup_free_ char *p = NULL; |
1323 | Unit *u = userdata; | |
1324 | int r; | |
c4e2ceae | 1325 | |
718db961 LP |
1326 | assert(bus); |
1327 | assert(u); | |
c1e1601e | 1328 | |
718db961 | 1329 | p = unit_dbus_path(u); |
e861098b | 1330 | if (!p) |
718db961 | 1331 | return -ENOMEM; |
c1e1601e | 1332 | |
718db961 LP |
1333 | r = sd_bus_message_new_signal( |
1334 | bus, | |
151b9b96 | 1335 | &m, |
718db961 LP |
1336 | "/org/freedesktop/systemd1", |
1337 | "org.freedesktop.systemd1.Manager", | |
151b9b96 | 1338 | "UnitRemoved"); |
718db961 LP |
1339 | if (r < 0) |
1340 | return r; | |
c1e1601e | 1341 | |
718db961 LP |
1342 | r = sd_bus_message_append(m, "so", u->id, p); |
1343 | if (r < 0) | |
1344 | return r; | |
c1e1601e | 1345 | |
8f8f05a9 | 1346 | return sd_bus_send(bus, m, NULL); |
c1e1601e LP |
1347 | } |
1348 | ||
1349 | void bus_unit_send_removed_signal(Unit *u) { | |
718db961 | 1350 | int r; |
c1e1601e LP |
1351 | assert(u); |
1352 | ||
0dd99f86 | 1353 | if (!u->sent_dbus_new_signal || u->in_dbus_queue) |
7535cc78 LP |
1354 | bus_unit_send_change_signal(u); |
1355 | ||
ac155bb8 | 1356 | if (!u->id) |
04ade7d2 LP |
1357 | return; |
1358 | ||
ae572acd | 1359 | r = bus_foreach_bus(u->manager, u->bus_track, send_removed_signal, u); |
718db961 | 1360 | if (r < 0) |
f2341e0a | 1361 | log_unit_debug_errno(u, r, "Failed to send unit remove signal for %s: %m", u->id); |
c1e1601e | 1362 | } |
e2110e5d | 1363 | |
718db961 | 1364 | int bus_unit_queue_job( |
718db961 | 1365 | sd_bus_message *message, |
cad45ba1 LP |
1366 | Unit *u, |
1367 | JobType type, | |
1368 | JobMode mode, | |
ebcf1f97 LP |
1369 | bool reload_if_possible, |
1370 | sd_bus_error *error) { | |
cad45ba1 | 1371 | |
cad45ba1 LP |
1372 | _cleanup_free_ char *path = NULL; |
1373 | Job *j; | |
cad45ba1 LP |
1374 | int r; |
1375 | ||
cad45ba1 LP |
1376 | assert(message); |
1377 | assert(u); | |
1378 | assert(type >= 0 && type < _JOB_TYPE_MAX); | |
1379 | assert(mode >= 0 && mode < _JOB_MODE_MAX); | |
1380 | ||
f596e00f EV |
1381 | r = mac_selinux_unit_access_check( |
1382 | u, message, | |
1383 | job_type_to_access_method(type), | |
1384 | error); | |
1385 | if (r < 0) | |
1386 | return r; | |
1387 | ||
cad45ba1 LP |
1388 | if (reload_if_possible && unit_can_reload(u)) { |
1389 | if (type == JOB_RESTART) | |
1390 | type = JOB_RELOAD_OR_START; | |
1391 | else if (type == JOB_TRY_RESTART) | |
3282591d | 1392 | type = JOB_TRY_RELOAD; |
cad45ba1 LP |
1393 | } |
1394 | ||
718db961 | 1395 | if (type == JOB_STOP && |
3742095b | 1396 | (IN_SET(u->load_state, UNIT_NOT_FOUND, UNIT_ERROR)) && |
718db961 | 1397 | unit_active_state(u) == UNIT_INACTIVE) |
ebcf1f97 | 1398 | return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_UNIT, "Unit %s not loaded.", u->id); |
cad45ba1 LP |
1399 | |
1400 | if ((type == JOB_START && u->refuse_manual_start) || | |
1401 | (type == JOB_STOP && u->refuse_manual_stop) || | |
3742095b | 1402 | (IN_SET(type, JOB_RESTART, JOB_TRY_RESTART) && (u->refuse_manual_start || u->refuse_manual_stop)) || |
efb30ba1 | 1403 | (type == JOB_RELOAD_OR_START && job_type_collapse(type, u) == JOB_START && u->refuse_manual_start)) |
7e974e85 | 1404 | 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); |
cad45ba1 | 1405 | |
4bd29fe5 | 1406 | r = manager_add_job(u->manager, type, u, mode, error, &j); |
cad45ba1 | 1407 | if (r < 0) |
ebcf1f97 | 1408 | return r; |
cad45ba1 | 1409 | |
c5a97ed1 LP |
1410 | r = bus_job_track_sender(j, message); |
1411 | if (r < 0) | |
1412 | return r; | |
cad45ba1 LP |
1413 | |
1414 | path = job_dbus_path(j); | |
1415 | if (!path) | |
6ce270b1 | 1416 | return -ENOMEM; |
cad45ba1 | 1417 | |
df2d202e | 1418 | return sd_bus_reply_method_return(message, "o", path); |
cad45ba1 LP |
1419 | } |
1420 | ||
721060d4 | 1421 | static int bus_unit_set_live_property( |
9f2e86af LP |
1422 | Unit *u, |
1423 | const char *name, | |
718db961 | 1424 | sd_bus_message *message, |
2e59b241 | 1425 | UnitWriteFlags flags, |
718db961 | 1426 | sd_bus_error *error) { |
9f2e86af LP |
1427 | |
1428 | int r; | |
1429 | ||
1430 | assert(u); | |
1431 | assert(name); | |
718db961 | 1432 | assert(message); |
9f2e86af | 1433 | |
721060d4 LP |
1434 | /* Handles setting properties both "live" (i.e. at any time during runtime), and during creation (for transient |
1435 | * units that are being created). */ | |
1436 | ||
9f2e86af | 1437 | if (streq(name, "Description")) { |
718db961 | 1438 | const char *d; |
9f2e86af | 1439 | |
718db961 LP |
1440 | r = sd_bus_message_read(message, "s", &d); |
1441 | if (r < 0) | |
1442 | return r; | |
8aec412f | 1443 | |
2e59b241 | 1444 | if (!UNIT_WRITE_FLAGS_NOOP(flags)) { |
718db961 | 1445 | r = unit_set_description(u, d); |
8aec412f LP |
1446 | if (r < 0) |
1447 | return r; | |
b9316fb0 | 1448 | |
2e59b241 | 1449 | unit_write_settingf(u, flags|UNIT_ESCAPE_SPECIFIERS, name, "Description=%s", d); |
8aec412f | 1450 | } |
9f2e86af LP |
1451 | |
1452 | return 1; | |
721060d4 LP |
1453 | } |
1454 | ||
1455 | return 0; | |
1456 | } | |
1457 | ||
faa781cf YW |
1458 | static BUS_DEFINE_SET_TRANSIENT_PARSE(collect_mode, CollectMode, collect_mode_from_string); |
1459 | static BUS_DEFINE_SET_TRANSIENT_PARSE(emergency_action, EmergencyAction, emergency_action_from_string); | |
1460 | static BUS_DEFINE_SET_TRANSIENT_PARSE(job_mode, JobMode, job_mode_from_string); | |
1461 | ||
1462 | static int bus_set_transient_conditions( | |
1463 | Unit *u, | |
1464 | const char *name, | |
1465 | Condition **list, | |
1466 | bool is_condition, | |
1467 | sd_bus_message *message, | |
1468 | UnitWriteFlags flags, | |
1469 | sd_bus_error *error) { | |
1470 | ||
1471 | const char *type_name, *param; | |
1472 | int trigger, negate, r; | |
1473 | bool empty = true; | |
1474 | ||
1475 | assert(list); | |
1476 | ||
1477 | r = sd_bus_message_enter_container(message, 'a', "(sbbs)"); | |
1478 | if (r < 0) | |
1479 | return r; | |
1480 | ||
1481 | while ((r = sd_bus_message_read(message, "(sbbs)", &type_name, &trigger, &negate, ¶m)) > 0) { | |
1482 | ConditionType t; | |
1483 | ||
1484 | t = is_condition ? condition_type_from_string(type_name) : assert_type_from_string(type_name); | |
1485 | if (t < 0) | |
1486 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid condition type: %s", type_name); | |
1487 | ||
1488 | if (t != CONDITION_NULL) { | |
1489 | if (isempty(param)) | |
1490 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Condition parameter in %s is empty", type_name); | |
1491 | ||
1492 | if (condition_takes_path(t) && !path_is_absolute(param)) | |
1493 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Path in condition %s is not absolute: %s", type_name, param); | |
1494 | } else | |
1495 | param = NULL; | |
1496 | ||
1497 | if (!UNIT_WRITE_FLAGS_NOOP(flags)) { | |
1498 | Condition *c; | |
1499 | ||
1500 | c = condition_new(t, param, trigger, negate); | |
1501 | if (!c) | |
1502 | return -ENOMEM; | |
1503 | ||
1504 | LIST_PREPEND(conditions, *list, c); | |
1505 | ||
1506 | if (t != CONDITION_NULL) | |
1507 | unit_write_settingf(u, flags|UNIT_ESCAPE_SPECIFIERS, name, | |
1508 | "%s=%s%s%s", type_name, | |
1509 | trigger ? "|" : "", negate ? "!" : "", param); | |
1510 | else | |
1511 | unit_write_settingf(u, flags, name, | |
1512 | "%s=%s%s", type_name, | |
1513 | trigger ? "|" : "", yes_no(!negate)); | |
1514 | } | |
1515 | ||
1516 | empty = false; | |
1517 | } | |
1518 | if (r < 0) | |
1519 | return r; | |
1520 | ||
1521 | r = sd_bus_message_exit_container(message); | |
1522 | if (r < 0) | |
1523 | return r; | |
1524 | ||
1525 | if (!UNIT_WRITE_FLAGS_NOOP(flags) && empty) { | |
1526 | *list = condition_free_list(*list); | |
1527 | unit_write_settingf(u, flags, name, "%sNull=", is_condition ? "Condition" : "Assert"); | |
1528 | } | |
1529 | ||
1530 | return 1; | |
1531 | } | |
1532 | ||
721060d4 LP |
1533 | static int bus_unit_set_transient_property( |
1534 | Unit *u, | |
1535 | const char *name, | |
1536 | sd_bus_message *message, | |
1537 | UnitWriteFlags flags, | |
1538 | sd_bus_error *error) { | |
1539 | ||
faa781cf | 1540 | UnitDependency d = _UNIT_DEPENDENCY_INVALID; |
721060d4 | 1541 | int r; |
261420ba | 1542 | |
721060d4 LP |
1543 | assert(u); |
1544 | assert(name); | |
1545 | assert(message); | |
1546 | ||
1547 | /* Handles settings when transient units are created. This settings cannot be altered anymore after the unit | |
1548 | * has been created. */ | |
1549 | ||
faa781cf YW |
1550 | if (streq(name, "SourcePath")) |
1551 | return bus_set_transient_path(u, name, &u->source_path, message, flags, error); | |
261420ba | 1552 | |
faa781cf YW |
1553 | if (streq(name, "StopWhenUnneeded")) |
1554 | return bus_set_transient_bool(u, name, &u->stop_when_unneeded, message, flags, error); | |
261420ba | 1555 | |
faa781cf YW |
1556 | if (streq(name, "RefuseManualStart")) |
1557 | return bus_set_transient_bool(u, name, &u->refuse_manual_start, message, flags, error); | |
261420ba | 1558 | |
faa781cf YW |
1559 | if (streq(name, "RefuseManualStop")) |
1560 | return bus_set_transient_bool(u, name, &u->refuse_manual_stop, message, flags, error); | |
c221420b | 1561 | |
faa781cf YW |
1562 | if (streq(name, "AllowIsolate")) |
1563 | return bus_set_transient_bool(u, name, &u->allow_isolate, message, flags, error); | |
5afe510c | 1564 | |
faa781cf YW |
1565 | if (streq(name, "DefaultDependencies")) |
1566 | return bus_set_transient_bool(u, name, &u->default_dependencies, message, flags, error); | |
1567 | ||
1568 | if (streq(name, "OnFailureJobMode")) | |
1569 | return bus_set_transient_job_mode(u, name, &u->on_failure_job_mode, message, flags, error); | |
1570 | ||
1571 | if (streq(name, "IgnoreOnIsolate")) | |
1572 | return bus_set_transient_bool(u, name, &u->ignore_on_isolate, message, flags, error); | |
1573 | ||
1574 | if (streq(name, "JobTimeoutUSec")) { | |
1575 | r = bus_set_transient_usec_fix_0(u, name, &u->job_timeout, message, flags, error); | |
1576 | if (r >= 0 && !UNIT_WRITE_FLAGS_NOOP(flags) && !u->job_running_timeout_set) | |
1577 | u->job_running_timeout = u->job_timeout; | |
1578 | } | |
1579 | ||
1580 | if (streq(name, "JobRunningTimeoutUSec")) { | |
1581 | r = bus_set_transient_usec_fix_0(u, name, &u->job_running_timeout, message, flags, error); | |
1582 | if (r >= 0 && !UNIT_WRITE_FLAGS_NOOP(flags)) | |
1583 | u->job_running_timeout_set = true; | |
1584 | ||
1585 | return r; | |
1586 | } | |
1587 | ||
1588 | if (streq(name, "JobTimeoutAction")) | |
1589 | return bus_set_transient_emergency_action(u, name, &u->job_timeout_action, message, flags, error); | |
1590 | ||
1591 | if (streq(name, "JobTimeoutRebootArgument")) | |
1592 | return bus_set_transient_string(u, name, &u->job_timeout_reboot_arg, message, flags, error); | |
1593 | ||
1594 | if (streq(name, "StartLimitIntervalUSec")) | |
1595 | return bus_set_transient_usec(u, name, &u->start_limit.interval, message, flags, error); | |
1596 | ||
1597 | if (streq(name, "StartLimitBurst")) | |
1598 | return bus_set_transient_unsigned(u, name, &u->start_limit.burst, message, flags, error); | |
1599 | ||
1600 | if (streq(name, "StartLimitAction")) | |
1601 | return bus_set_transient_emergency_action(u, name, &u->start_limit_action, message, flags, error); | |
1602 | ||
1603 | if (streq(name, "FailureAction")) | |
1604 | return bus_set_transient_emergency_action(u, name, &u->failure_action, message, flags, error); | |
1605 | ||
1606 | if (streq(name, "SuccessAction")) | |
1607 | return bus_set_transient_emergency_action(u, name, &u->success_action, message, flags, error); | |
1608 | ||
1609 | if (streq(name, "RebootArgument")) | |
1610 | return bus_set_transient_string(u, name, &u->reboot_arg, message, flags, error); | |
1611 | ||
1612 | if (streq(name, "CollectMode")) | |
1613 | return bus_set_transient_collect_mode(u, name, &u->collect_mode, message, flags, error); | |
1614 | ||
1615 | if (streq(name, "Conditions")) | |
1616 | return bus_set_transient_conditions(u, name, &u->conditions, true, message, flags, error); | |
1617 | ||
1618 | if (streq(name, "Asserts")) | |
1619 | return bus_set_transient_conditions(u, name, &u->asserts, false, message, flags, error); | |
1620 | ||
1621 | if (streq(name, "Documentation")) { | |
1622 | _cleanup_strv_free_ char **l = NULL; | |
1623 | char **p; | |
1624 | ||
1625 | r = sd_bus_message_read_strv(message, &l); | |
5afe510c LP |
1626 | if (r < 0) |
1627 | return r; | |
1628 | ||
faa781cf YW |
1629 | STRV_FOREACH(p, l) { |
1630 | if (!documentation_url_is_valid(*p)) | |
1631 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid URL in %s: %s", name, *p); | |
1632 | } | |
5afe510c | 1633 | |
2e59b241 | 1634 | if (!UNIT_WRITE_FLAGS_NOOP(flags)) { |
faa781cf YW |
1635 | if (strv_isempty(l)) { |
1636 | u->documentation = strv_free(u->documentation); | |
1637 | unit_write_settingf(u, flags, name, "%s=", name); | |
1638 | } else { | |
1639 | strv_extend_strv(&u->documentation, l, false); | |
1640 | ||
1641 | STRV_FOREACH(p, l) | |
1642 | unit_write_settingf(u, flags, name, "%s=%s", name, *p); | |
1643 | } | |
5afe510c LP |
1644 | } |
1645 | ||
1646 | return 1; | |
1647 | ||
d79200e2 LP |
1648 | } else if (streq(name, "Slice")) { |
1649 | Unit *slice; | |
c221420b | 1650 | const char *s; |
c221420b | 1651 | |
d79200e2 LP |
1652 | if (!UNIT_HAS_CGROUP_CONTEXT(u)) |
1653 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "The slice property is only available for units with control groups."); | |
1654 | if (u->type == UNIT_SLICE) | |
1655 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Slice may not be set for slice units."); | |
efdb0237 LP |
1656 | if (unit_has_name(u, SPECIAL_INIT_SCOPE)) |
1657 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Cannot set slice for init.scope"); | |
d79200e2 | 1658 | |
718db961 LP |
1659 | r = sd_bus_message_read(message, "s", &s); |
1660 | if (r < 0) | |
1661 | return r; | |
c221420b | 1662 | |
d79200e2 LP |
1663 | if (!unit_name_is_valid(s, UNIT_NAME_PLAIN)) |
1664 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid unit name '%s'", s); | |
1665 | ||
aea529e5 LP |
1666 | /* Note that we do not dispatch the load queue here yet, as we don't want our own transient unit to be |
1667 | * loaded while we are still setting it up. Or in other words, we use manager_load_unit_prepare() | |
1668 | * instead of manager_load_unit() on purpose, here. */ | |
1669 | r = manager_load_unit_prepare(u->manager, s, NULL, error, &slice); | |
d79200e2 LP |
1670 | if (r < 0) |
1671 | return r; | |
c221420b | 1672 | |
d79200e2 LP |
1673 | if (slice->type != UNIT_SLICE) |
1674 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Unit name '%s' is not a slice", s); | |
b9316fb0 | 1675 | |
2e59b241 | 1676 | if (!UNIT_WRITE_FLAGS_NOOP(flags)) { |
d79200e2 | 1677 | r = unit_set_slice(u, slice); |
8aec412f LP |
1678 | if (r < 0) |
1679 | return r; | |
c221420b | 1680 | |
2e59b241 | 1681 | unit_write_settingf(u, flags|UNIT_PRIVATE, name, "Slice=%s", s); |
8aec412f | 1682 | } |
b9ec9359 | 1683 | |
c221420b | 1684 | return 1; |
d79200e2 | 1685 | |
faa781cf YW |
1686 | } else if (streq(name, "RequiresMountsFor")) { |
1687 | _cleanup_strv_free_ char **l = NULL; | |
1688 | char **p; | |
1689 | ||
1690 | r = sd_bus_message_read_strv(message, &l); | |
1691 | if (r < 0) | |
1692 | return r; | |
1693 | ||
1694 | STRV_FOREACH(p, l) { | |
1695 | if (!path_is_absolute(*p)) | |
1696 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Path specified in %s is not absolute: %s", name, *p); | |
1697 | ||
1698 | if (!UNIT_WRITE_FLAGS_NOOP(flags)) { | |
1699 | r = unit_require_mounts_for(u, *p, UNIT_DEPENDENCY_FILE); | |
1700 | if (r < 0) | |
1701 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Failed to add required mount \"%s\": %m", *p); | |
7fb3ee51 | 1702 | |
faa781cf YW |
1703 | unit_write_settingf(u, flags, name, "%s=%s", name, *p); |
1704 | } | |
f32b43bd | 1705 | } |
7fb3ee51 | 1706 | |
faa781cf YW |
1707 | return 1; |
1708 | } | |
1709 | ||
1710 | if (streq(name, "RequiresOverridable")) | |
1711 | d = UNIT_REQUIRES; /* redirect for obsolete unit dependency type */ | |
1712 | else if (streq(name, "RequisiteOverridable")) | |
1713 | d = UNIT_REQUISITE; /* same here */ | |
1714 | else | |
1715 | d = unit_dependency_from_string(name); | |
1716 | ||
1717 | if (d >= 0) { | |
1718 | const char *other; | |
1719 | ||
718db961 LP |
1720 | r = sd_bus_message_enter_container(message, 'a', "s"); |
1721 | if (r < 0) | |
1722 | return r; | |
7fb3ee51 | 1723 | |
718db961 | 1724 | while ((r = sd_bus_message_read(message, "s", &other)) > 0) { |
7410616c | 1725 | if (!unit_name_is_valid(other, UNIT_NAME_PLAIN|UNIT_NAME_INSTANCE)) |
718db961 | 1726 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid unit name %s", other); |
7fb3ee51 | 1727 | |
2e59b241 | 1728 | if (!UNIT_WRITE_FLAGS_NOOP(flags)) { |
b9ec9359 | 1729 | _cleanup_free_ char *label = NULL; |
7fb3ee51 | 1730 | |
eef85c4a | 1731 | r = unit_add_dependency_by_name(u, d, other, NULL, true, UNIT_DEPENDENCY_FILE); |
7fb3ee51 LP |
1732 | if (r < 0) |
1733 | return r; | |
1734 | ||
605405c6 | 1735 | label = strjoin(name, "-", other); |
7fb3ee51 LP |
1736 | if (!label) |
1737 | return -ENOMEM; | |
1738 | ||
faa781cf | 1739 | unit_write_settingf(u, flags, label, "%s=%s", unit_dependency_to_string(d), other); |
7fb3ee51 LP |
1740 | } |
1741 | ||
7fb3ee51 | 1742 | } |
718db961 LP |
1743 | if (r < 0) |
1744 | return r; | |
7fb3ee51 | 1745 | |
6ce270b1 LP |
1746 | r = sd_bus_message_exit_container(message); |
1747 | if (r < 0) | |
1748 | return r; | |
1749 | ||
7fb3ee51 | 1750 | return 1; |
05a98afd LP |
1751 | |
1752 | } else if (streq(name, "AddRef")) { | |
1753 | ||
1754 | int b; | |
1755 | ||
1756 | /* Why is this called "AddRef" rather than just "Ref", or "Reference"? There's already a "Ref()" method | |
1757 | * on the Unit interface, and it's probably not a good idea to expose a property and a method on the | |
1758 | * same interface (well, strictly speaking AddRef isn't exposed as full property, we just read it for | |
1759 | * transient units, but still). And "References" and "ReferencedBy" is already used as unit reference | |
1760 | * dependency type, hence let's not confuse things with that. | |
1761 | * | |
1762 | * Note that we don't acually add the reference to the bus track. We do that only after the setup of | |
1763 | * the transient unit is complete, so that setting this property multiple times in the same transient | |
1764 | * unit creation call doesn't count as individual references. */ | |
1765 | ||
1766 | r = sd_bus_message_read(message, "b", &b); | |
1767 | if (r < 0) | |
1768 | return r; | |
1769 | ||
2e59b241 | 1770 | if (!UNIT_WRITE_FLAGS_NOOP(flags)) |
05a98afd LP |
1771 | u->bus_track_add = b; |
1772 | ||
1773 | return 1; | |
9f2e86af LP |
1774 | } |
1775 | ||
1776 | return 0; | |
1777 | } | |
1778 | ||
c2756a68 LP |
1779 | int bus_unit_set_properties( |
1780 | Unit *u, | |
718db961 | 1781 | sd_bus_message *message, |
2e59b241 | 1782 | UnitWriteFlags flags, |
c2756a68 | 1783 | bool commit, |
718db961 | 1784 | sd_bus_error *error) { |
c2756a68 | 1785 | |
8e2af478 | 1786 | bool for_real = false; |
8e2af478 LP |
1787 | unsigned n = 0; |
1788 | int r; | |
1789 | ||
1790 | assert(u); | |
718db961 | 1791 | assert(message); |
8e2af478 LP |
1792 | |
1793 | /* We iterate through the array twice. First run we just check | |
1794 | * if all passed data is valid, second run actually applies | |
1795 | * it. This is to implement transaction-like behaviour without | |
1796 | * actually providing full transactions. */ | |
1797 | ||
718db961 LP |
1798 | r = sd_bus_message_enter_container(message, 'a', "(sv)"); |
1799 | if (r < 0) | |
1800 | return r; | |
8e2af478 | 1801 | |
8e2af478 | 1802 | for (;;) { |
8e2af478 | 1803 | const char *name; |
2e59b241 | 1804 | UnitWriteFlags f; |
8e2af478 | 1805 | |
718db961 LP |
1806 | r = sd_bus_message_enter_container(message, 'r', "sv"); |
1807 | if (r < 0) | |
1808 | return r; | |
1809 | if (r == 0) { | |
2e59b241 | 1810 | if (for_real || UNIT_WRITE_FLAGS_NOOP(flags)) |
8e2af478 LP |
1811 | break; |
1812 | ||
1813 | /* Reached EOF. Let's try again, and this time for realz... */ | |
718db961 LP |
1814 | r = sd_bus_message_rewind(message, false); |
1815 | if (r < 0) | |
1816 | return r; | |
6ce270b1 | 1817 | |
8e2af478 LP |
1818 | for_real = true; |
1819 | continue; | |
1820 | } | |
1821 | ||
718db961 LP |
1822 | r = sd_bus_message_read(message, "s", &name); |
1823 | if (r < 0) | |
1824 | return r; | |
8e2af478 | 1825 | |
718db961 LP |
1826 | if (!UNIT_VTABLE(u)->bus_set_property) |
1827 | return sd_bus_error_setf(error, SD_BUS_ERROR_PROPERTY_READ_ONLY, "Objects of this type do not support setting properties."); | |
8e2af478 | 1828 | |
718db961 LP |
1829 | r = sd_bus_message_enter_container(message, 'v', NULL); |
1830 | if (r < 0) | |
1831 | return r; | |
8e2af478 | 1832 | |
2e59b241 LP |
1833 | /* If not for real, then mask out the two target flags */ |
1834 | f = for_real ? flags : (flags & ~(UNIT_RUNTIME|UNIT_PERSISTENT)); | |
1835 | ||
1836 | r = UNIT_VTABLE(u)->bus_set_property(u, name, message, f, error); | |
9f2e86af | 1837 | if (r == 0 && u->transient && u->load_state == UNIT_STUB) |
2e59b241 | 1838 | r = bus_unit_set_transient_property(u, name, message, f, error); |
721060d4 LP |
1839 | if (r == 0) |
1840 | r = bus_unit_set_live_property(u, name, message, f, error); | |
718db961 LP |
1841 | if (r < 0) |
1842 | return r; | |
721060d4 | 1843 | |
718db961 LP |
1844 | if (r == 0) |
1845 | return sd_bus_error_setf(error, SD_BUS_ERROR_PROPERTY_READ_ONLY, "Cannot set property %s, or unknown property.", name); | |
1846 | ||
1847 | r = sd_bus_message_exit_container(message); | |
8e2af478 LP |
1848 | if (r < 0) |
1849 | return r; | |
8e2af478 | 1850 | |
718db961 LP |
1851 | r = sd_bus_message_exit_container(message); |
1852 | if (r < 0) | |
1853 | return r; | |
8e2af478 LP |
1854 | |
1855 | n += for_real; | |
1856 | } | |
1857 | ||
6ce270b1 LP |
1858 | r = sd_bus_message_exit_container(message); |
1859 | if (r < 0) | |
1860 | return r; | |
1861 | ||
c2756a68 | 1862 | if (commit && n > 0 && UNIT_VTABLE(u)->bus_commit_properties) |
8e2af478 LP |
1863 | UNIT_VTABLE(u)->bus_commit_properties(u); |
1864 | ||
241da328 | 1865 | return n; |
8e2af478 | 1866 | } |
000a996d FB |
1867 | |
1868 | int bus_unit_check_load_state(Unit *u, sd_bus_error *error) { | |
6eb7c172 | 1869 | assert(u); |
000a996d FB |
1870 | |
1871 | if (u->load_state == UNIT_LOADED) | |
1872 | return 0; | |
1873 | ||
1874 | /* Give a better description of the unit error when | |
1875 | * possible. Note that in the case of UNIT_MASKED, load_error | |
1876 | * is not set. */ | |
1877 | if (u->load_state == UNIT_MASKED) | |
b21d2f81 | 1878 | return sd_bus_error_setf(error, BUS_ERROR_UNIT_MASKED, "Unit %s is masked.", u->id); |
000a996d FB |
1879 | |
1880 | if (u->load_state == UNIT_NOT_FOUND) | |
b21d2f81 | 1881 | return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_UNIT, "Unit %s not found.", u->id); |
000a996d | 1882 | |
b21d2f81 | 1883 | return sd_bus_error_set_errnof(error, u->load_error, "Unit %s is not loaded properly: %m.", u->id); |
000a996d | 1884 | } |
05a98afd | 1885 | |
c5a97ed1 | 1886 | static int bus_unit_track_handler(sd_bus_track *t, void *userdata) { |
05a98afd LP |
1887 | Unit *u = userdata; |
1888 | ||
1889 | assert(t); | |
1890 | assert(u); | |
1891 | ||
1892 | u->bus_track = sd_bus_track_unref(u->bus_track); /* make sure we aren't called again */ | |
1893 | ||
1894 | unit_add_to_gc_queue(u); | |
1895 | return 0; | |
1896 | } | |
1897 | ||
c5a97ed1 | 1898 | static int bus_unit_allocate_bus_track(Unit *u) { |
05a98afd LP |
1899 | int r; |
1900 | ||
1901 | assert(u); | |
1902 | ||
1903 | if (u->bus_track) | |
1904 | return 0; | |
1905 | ||
c5a97ed1 | 1906 | r = sd_bus_track_new(u->manager->api_bus, &u->bus_track, bus_unit_track_handler, u); |
05a98afd LP |
1907 | if (r < 0) |
1908 | return r; | |
1909 | ||
1910 | r = sd_bus_track_set_recursive(u->bus_track, true); | |
1911 | if (r < 0) { | |
1912 | u->bus_track = sd_bus_track_unref(u->bus_track); | |
1913 | return r; | |
1914 | } | |
1915 | ||
1916 | return 0; | |
1917 | } | |
1918 | ||
1919 | int bus_unit_track_add_name(Unit *u, const char *name) { | |
1920 | int r; | |
1921 | ||
1922 | assert(u); | |
1923 | ||
c5a97ed1 | 1924 | r = bus_unit_allocate_bus_track(u); |
05a98afd LP |
1925 | if (r < 0) |
1926 | return r; | |
1927 | ||
1928 | return sd_bus_track_add_name(u->bus_track, name); | |
1929 | } | |
1930 | ||
1931 | int bus_unit_track_add_sender(Unit *u, sd_bus_message *m) { | |
1932 | int r; | |
1933 | ||
1934 | assert(u); | |
1935 | ||
c5a97ed1 | 1936 | r = bus_unit_allocate_bus_track(u); |
05a98afd LP |
1937 | if (r < 0) |
1938 | return r; | |
1939 | ||
1940 | return sd_bus_track_add_sender(u->bus_track, m); | |
1941 | } | |
1942 | ||
1943 | int bus_unit_track_remove_sender(Unit *u, sd_bus_message *m) { | |
1944 | assert(u); | |
1945 | ||
1946 | /* If we haven't allocated the bus track object yet, then there's definitely no reference taken yet, return an | |
1947 | * error */ | |
1948 | if (!u->bus_track) | |
1949 | return -EUNATCH; | |
1950 | ||
1951 | return sd_bus_track_remove_sender(u->bus_track, m); | |
1952 | } |