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