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