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