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