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