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