]>
Commit | Line | Data |
---|---|---|
663996b3 MS |
1 | /*** |
2 | This file is part of systemd. | |
3 | ||
4 | Copyright 2010 Lennart Poettering | |
5 | ||
6 | systemd is free software; you can redistribute it and/or modify it | |
7 | under the terms of the GNU Lesser General Public License as published by | |
8 | the Free Software Foundation; either version 2.1 of the License, or | |
9 | (at your option) any later version. | |
10 | ||
11 | systemd is distributed in the hope that it will be useful, but | |
12 | WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 | Lesser General Public License for more details. | |
15 | ||
16 | You should have received a copy of the GNU Lesser General Public License | |
17 | along with systemd; If not, see <http://www.gnu.org/licenses/>. | |
18 | ***/ | |
19 | ||
60f067b4 | 20 | #include "sd-bus.h" |
db2df898 MP |
21 | |
22 | #include "alloc-util.h" | |
23 | #include "bus-common-errors.h" | |
24 | #include "cgroup-util.h" | |
25 | #include "dbus-unit.h" | |
26 | #include "dbus.h" | |
27 | #include "locale-util.h" | |
663996b3 | 28 | #include "log.h" |
663996b3 | 29 | #include "selinux-access.h" |
d9dfd233 | 30 | #include "special.h" |
db2df898 MP |
31 | #include "string-util.h" |
32 | #include "strv.h" | |
33 | #include "user-util.h" | |
663996b3 | 34 | |
60f067b4 JS |
35 | static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_load_state, unit_load_state, UnitLoadState); |
36 | static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_job_mode, job_mode, JobMode); | |
5eef597e | 37 | static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_failure_action, failure_action, FailureAction); |
60f067b4 JS |
38 | |
39 | static int property_get_names( | |
40 | sd_bus *bus, | |
41 | const char *path, | |
42 | const char *interface, | |
43 | const char *property, | |
44 | sd_bus_message *reply, | |
45 | void *userdata, | |
46 | sd_bus_error *error) { | |
47 | ||
48 | Unit *u = userdata; | |
49 | Iterator i; | |
50 | const char *t; | |
51 | int r; | |
663996b3 | 52 | |
60f067b4 JS |
53 | assert(bus); |
54 | assert(reply); | |
55 | assert(u); | |
663996b3 | 56 | |
60f067b4 JS |
57 | r = sd_bus_message_open_container(reply, 'a', "s"); |
58 | if (r < 0) | |
59 | return r; | |
663996b3 | 60 | |
60f067b4 JS |
61 | SET_FOREACH(t, u->names, i) { |
62 | r = sd_bus_message_append(reply, "s", t); | |
63 | if (r < 0) | |
64 | return r; | |
65 | } | |
663996b3 | 66 | |
60f067b4 | 67 | return sd_bus_message_close_container(reply); |
663996b3 MS |
68 | } |
69 | ||
60f067b4 JS |
70 | static int property_get_following( |
71 | sd_bus *bus, | |
72 | const char *path, | |
73 | const char *interface, | |
74 | const char *property, | |
75 | sd_bus_message *reply, | |
76 | void *userdata, | |
77 | sd_bus_error *error) { | |
78 | ||
79 | Unit *u = userdata, *f; | |
663996b3 | 80 | |
60f067b4 JS |
81 | assert(bus); |
82 | assert(reply); | |
663996b3 MS |
83 | assert(u); |
84 | ||
85 | f = unit_following(u); | |
60f067b4 | 86 | return sd_bus_message_append(reply, "s", f ? f->id : ""); |
663996b3 MS |
87 | } |
88 | ||
60f067b4 JS |
89 | static int property_get_dependencies( |
90 | sd_bus *bus, | |
91 | const char *path, | |
92 | const char *interface, | |
93 | const char *property, | |
94 | sd_bus_message *reply, | |
95 | void *userdata, | |
96 | sd_bus_error *error) { | |
14228c0d | 97 | |
60f067b4 | 98 | Set *s = *(Set**) userdata; |
663996b3 | 99 | Iterator j; |
60f067b4 JS |
100 | Unit *u; |
101 | int r; | |
663996b3 | 102 | |
60f067b4 JS |
103 | assert(bus); |
104 | assert(reply); | |
663996b3 | 105 | |
60f067b4 JS |
106 | r = sd_bus_message_open_container(reply, 'a', "s"); |
107 | if (r < 0) | |
108 | return r; | |
663996b3 | 109 | |
60f067b4 JS |
110 | SET_FOREACH(u, s, j) { |
111 | r = sd_bus_message_append(reply, "s", u->id); | |
112 | if (r < 0) | |
113 | return r; | |
114 | } | |
663996b3 | 115 | |
60f067b4 | 116 | return sd_bus_message_close_container(reply); |
663996b3 MS |
117 | } |
118 | ||
db2df898 MP |
119 | static int property_get_obsolete_dependencies( |
120 | sd_bus *bus, | |
121 | const char *path, | |
122 | const char *interface, | |
123 | const char *property, | |
124 | sd_bus_message *reply, | |
125 | void *userdata, | |
126 | sd_bus_error *error) { | |
127 | ||
128 | assert(bus); | |
129 | assert(reply); | |
130 | ||
131 | /* For dependency types we don't support anymore always return an empty array */ | |
132 | return sd_bus_message_append(reply, "as", 0); | |
133 | } | |
134 | ||
60f067b4 JS |
135 | static int property_get_description( |
136 | sd_bus *bus, | |
137 | const char *path, | |
138 | const char *interface, | |
139 | const char *property, | |
140 | sd_bus_message *reply, | |
141 | void *userdata, | |
142 | sd_bus_error *error) { | |
663996b3 | 143 | |
60f067b4 | 144 | Unit *u = userdata; |
663996b3 | 145 | |
60f067b4 JS |
146 | assert(bus); |
147 | assert(reply); | |
148 | assert(u); | |
663996b3 | 149 | |
60f067b4 | 150 | return sd_bus_message_append(reply, "s", unit_description(u)); |
663996b3 MS |
151 | } |
152 | ||
60f067b4 JS |
153 | static int property_get_active_state( |
154 | sd_bus *bus, | |
155 | const char *path, | |
156 | const char *interface, | |
157 | const char *property, | |
158 | sd_bus_message *reply, | |
159 | void *userdata, | |
160 | sd_bus_error *error) { | |
663996b3 | 161 | |
60f067b4 | 162 | Unit *u = userdata; |
663996b3 | 163 | |
60f067b4 JS |
164 | assert(bus); |
165 | assert(reply); | |
663996b3 MS |
166 | assert(u); |
167 | ||
60f067b4 | 168 | return sd_bus_message_append(reply, "s", unit_active_state_to_string(unit_active_state(u))); |
663996b3 MS |
169 | } |
170 | ||
60f067b4 JS |
171 | static int property_get_sub_state( |
172 | sd_bus *bus, | |
173 | const char *path, | |
174 | const char *interface, | |
175 | const char *property, | |
176 | sd_bus_message *reply, | |
177 | void *userdata, | |
178 | sd_bus_error *error) { | |
663996b3 | 179 | |
60f067b4 | 180 | Unit *u = userdata; |
663996b3 | 181 | |
60f067b4 JS |
182 | assert(bus); |
183 | assert(reply); | |
184 | assert(u); | |
663996b3 | 185 | |
60f067b4 | 186 | return sd_bus_message_append(reply, "s", unit_sub_state_to_string(u)); |
663996b3 MS |
187 | } |
188 | ||
f47781d8 MP |
189 | static int property_get_unit_file_preset( |
190 | sd_bus *bus, | |
191 | const char *path, | |
192 | const char *interface, | |
193 | const char *property, | |
194 | sd_bus_message *reply, | |
195 | void *userdata, | |
196 | sd_bus_error *error) { | |
197 | ||
198 | Unit *u = userdata; | |
199 | int r; | |
200 | ||
201 | assert(bus); | |
202 | assert(reply); | |
203 | assert(u); | |
204 | ||
205 | r = unit_get_unit_file_preset(u); | |
206 | ||
207 | return sd_bus_message_append(reply, "s", | |
208 | r < 0 ? "": | |
209 | r > 0 ? "enabled" : "disabled"); | |
210 | } | |
211 | ||
60f067b4 JS |
212 | static int property_get_unit_file_state( |
213 | sd_bus *bus, | |
214 | const char *path, | |
215 | const char *interface, | |
216 | const char *property, | |
217 | sd_bus_message *reply, | |
218 | void *userdata, | |
219 | sd_bus_error *error) { | |
663996b3 | 220 | |
60f067b4 | 221 | Unit *u = userdata; |
663996b3 | 222 | |
60f067b4 JS |
223 | assert(bus); |
224 | assert(reply); | |
225 | assert(u); | |
663996b3 | 226 | |
60f067b4 | 227 | return sd_bus_message_append(reply, "s", unit_file_state_to_string(unit_get_unit_file_state(u))); |
663996b3 MS |
228 | } |
229 | ||
60f067b4 JS |
230 | static int property_get_can_start( |
231 | sd_bus *bus, | |
232 | const char *path, | |
233 | const char *interface, | |
234 | const char *property, | |
235 | sd_bus_message *reply, | |
236 | void *userdata, | |
237 | sd_bus_error *error) { | |
663996b3 | 238 | |
60f067b4 | 239 | Unit *u = userdata; |
663996b3 | 240 | |
60f067b4 JS |
241 | assert(bus); |
242 | assert(reply); | |
243 | assert(u); | |
663996b3 | 244 | |
60f067b4 | 245 | return sd_bus_message_append(reply, "b", unit_can_start(u) && !u->refuse_manual_start); |
663996b3 MS |
246 | } |
247 | ||
60f067b4 JS |
248 | static int property_get_can_stop( |
249 | sd_bus *bus, | |
250 | const char *path, | |
251 | const char *interface, | |
252 | const char *property, | |
253 | sd_bus_message *reply, | |
254 | void *userdata, | |
255 | sd_bus_error *error) { | |
256 | ||
257 | Unit *u = userdata; | |
663996b3 | 258 | |
60f067b4 JS |
259 | assert(bus); |
260 | assert(reply); | |
663996b3 MS |
261 | assert(u); |
262 | ||
263 | /* On the lower levels we assume that every unit we can start | |
264 | * we can also stop */ | |
265 | ||
60f067b4 | 266 | return sd_bus_message_append(reply, "b", unit_can_start(u) && !u->refuse_manual_stop); |
663996b3 MS |
267 | } |
268 | ||
60f067b4 JS |
269 | static int property_get_can_reload( |
270 | sd_bus *bus, | |
271 | const char *path, | |
272 | const char *interface, | |
273 | const char *property, | |
274 | sd_bus_message *reply, | |
275 | void *userdata, | |
276 | sd_bus_error *error) { | |
663996b3 | 277 | |
60f067b4 | 278 | Unit *u = userdata; |
663996b3 | 279 | |
60f067b4 JS |
280 | assert(bus); |
281 | assert(reply); | |
282 | assert(u); | |
663996b3 | 283 | |
60f067b4 | 284 | return sd_bus_message_append(reply, "b", unit_can_reload(u)); |
663996b3 MS |
285 | } |
286 | ||
60f067b4 JS |
287 | static int property_get_can_isolate( |
288 | sd_bus *bus, | |
289 | const char *path, | |
290 | const char *interface, | |
291 | const char *property, | |
292 | sd_bus_message *reply, | |
293 | void *userdata, | |
294 | sd_bus_error *error) { | |
663996b3 | 295 | |
60f067b4 | 296 | Unit *u = userdata; |
663996b3 | 297 | |
60f067b4 JS |
298 | assert(bus); |
299 | assert(reply); | |
300 | assert(u); | |
663996b3 | 301 | |
60f067b4 | 302 | return sd_bus_message_append(reply, "b", unit_can_isolate(u) && !u->refuse_manual_start); |
663996b3 MS |
303 | } |
304 | ||
60f067b4 JS |
305 | static int property_get_job( |
306 | sd_bus *bus, | |
307 | const char *path, | |
308 | const char *interface, | |
309 | const char *property, | |
310 | sd_bus_message *reply, | |
311 | void *userdata, | |
312 | sd_bus_error *error) { | |
313 | ||
663996b3 | 314 | _cleanup_free_ char *p = NULL; |
60f067b4 | 315 | Unit *u = userdata; |
663996b3 | 316 | |
60f067b4 JS |
317 | assert(bus); |
318 | assert(reply); | |
663996b3 MS |
319 | assert(u); |
320 | ||
60f067b4 JS |
321 | if (!u->job) |
322 | return sd_bus_message_append(reply, "(uo)", 0, "/"); | |
663996b3 | 323 | |
60f067b4 JS |
324 | p = job_dbus_path(u->job); |
325 | if (!p) | |
663996b3 MS |
326 | return -ENOMEM; |
327 | ||
60f067b4 | 328 | return sd_bus_message_append(reply, "(uo)", u->job->id, p); |
663996b3 MS |
329 | } |
330 | ||
60f067b4 JS |
331 | static int property_get_need_daemon_reload( |
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) { | |
663996b3 | 339 | |
60f067b4 | 340 | Unit *u = userdata; |
663996b3 | 341 | |
60f067b4 JS |
342 | assert(bus); |
343 | assert(reply); | |
344 | assert(u); | |
663996b3 | 345 | |
60f067b4 | 346 | return sd_bus_message_append(reply, "b", unit_need_daemon_reload(u)); |
663996b3 MS |
347 | } |
348 | ||
60f067b4 JS |
349 | static int property_get_conditions( |
350 | sd_bus *bus, | |
351 | const char *path, | |
352 | const char *interface, | |
353 | const char *property, | |
354 | sd_bus_message *reply, | |
355 | void *userdata, | |
356 | sd_bus_error *error) { | |
663996b3 | 357 | |
f47781d8 MP |
358 | const char *(*to_string)(ConditionType type) = NULL; |
359 | Condition **list = userdata, *c; | |
60f067b4 | 360 | int r; |
663996b3 | 361 | |
60f067b4 JS |
362 | assert(bus); |
363 | assert(reply); | |
f47781d8 MP |
364 | assert(list); |
365 | ||
366 | to_string = streq(property, "Asserts") ? assert_type_to_string : condition_type_to_string; | |
663996b3 | 367 | |
60f067b4 JS |
368 | r = sd_bus_message_open_container(reply, 'a', "(sbbsi)"); |
369 | if (r < 0) | |
370 | return r; | |
663996b3 | 371 | |
f47781d8 MP |
372 | LIST_FOREACH(conditions, c, *list) { |
373 | int tristate; | |
374 | ||
375 | tristate = | |
376 | c->result == CONDITION_UNTESTED ? 0 : | |
377 | c->result == CONDITION_SUCCEEDED ? 1 : -1; | |
378 | ||
60f067b4 | 379 | r = sd_bus_message_append(reply, "(sbbsi)", |
f47781d8 | 380 | to_string(c->type), |
60f067b4 | 381 | c->trigger, c->negate, |
f47781d8 | 382 | c->parameter, tristate); |
60f067b4 JS |
383 | if (r < 0) |
384 | return r; | |
14228c0d | 385 | |
60f067b4 | 386 | } |
663996b3 | 387 | |
60f067b4 | 388 | return sd_bus_message_close_container(reply); |
663996b3 MS |
389 | } |
390 | ||
60f067b4 JS |
391 | static int property_get_load_error( |
392 | sd_bus *bus, | |
393 | const char *path, | |
394 | const char *interface, | |
395 | const char *property, | |
396 | sd_bus_message *reply, | |
397 | void *userdata, | |
398 | sd_bus_error *error) { | |
663996b3 | 399 | |
4c89c718 | 400 | _cleanup_(sd_bus_error_free) sd_bus_error e = SD_BUS_ERROR_NULL; |
60f067b4 | 401 | Unit *u = userdata; |
663996b3 | 402 | |
60f067b4 JS |
403 | assert(bus); |
404 | assert(reply); | |
405 | assert(u); | |
663996b3 | 406 | |
60f067b4 JS |
407 | if (u->load_error != 0) |
408 | sd_bus_error_set_errno(&e, u->load_error); | |
663996b3 | 409 | |
60f067b4 | 410 | return sd_bus_message_append(reply, "(ss)", e.name, e.message); |
663996b3 MS |
411 | } |
412 | ||
d9dfd233 MP |
413 | static int bus_verify_manage_units_async_full( |
414 | Unit *u, | |
415 | const char *verb, | |
416 | int capability, | |
417 | const char *polkit_message, | |
418 | sd_bus_message *call, | |
419 | sd_bus_error *error) { | |
420 | ||
421 | const char *details[9] = { | |
422 | "unit", u->id, | |
423 | "verb", verb, | |
424 | }; | |
425 | ||
426 | if (polkit_message) { | |
427 | details[4] = "polkit.message"; | |
428 | details[5] = polkit_message; | |
429 | details[6] = "polkit.gettext_domain"; | |
430 | details[7] = GETTEXT_PACKAGE; | |
431 | } | |
432 | ||
433 | return bus_verify_polkit_async(call, capability, "org.freedesktop.systemd1.manage-units", details, false, UID_INVALID, &u->manager->polkit_registry, error); | |
434 | } | |
435 | ||
e3bff60a MP |
436 | int bus_unit_method_start_generic( |
437 | sd_bus_message *message, | |
438 | Unit *u, | |
439 | JobType job_type, | |
440 | bool reload_if_possible, | |
441 | sd_bus_error *error) { | |
442 | ||
60f067b4 JS |
443 | const char *smode; |
444 | JobMode mode; | |
d9dfd233 MP |
445 | _cleanup_free_ char *verb = NULL; |
446 | static const char *const polkit_message_for_job[_JOB_TYPE_MAX] = { | |
447 | [JOB_START] = N_("Authentication is required to start '$(unit)'."), | |
448 | [JOB_STOP] = N_("Authentication is required to stop '$(unit)'."), | |
449 | [JOB_RELOAD] = N_("Authentication is required to reload '$(unit)'."), | |
450 | [JOB_RESTART] = N_("Authentication is required to restart '$(unit)'."), | |
451 | [JOB_TRY_RESTART] = N_("Authentication is required to restart '$(unit)'."), | |
452 | }; | |
663996b3 MS |
453 | int r; |
454 | ||
60f067b4 JS |
455 | assert(message); |
456 | assert(u); | |
457 | assert(job_type >= 0 && job_type < _JOB_TYPE_MAX); | |
663996b3 | 458 | |
4c89c718 MP |
459 | r = mac_selinux_unit_access_check( |
460 | u, message, | |
461 | job_type_to_access_method(job_type), | |
462 | error); | |
e3bff60a MP |
463 | if (r < 0) |
464 | return r; | |
465 | ||
60f067b4 JS |
466 | r = sd_bus_message_read(message, "s", &smode); |
467 | if (r < 0) | |
468 | return r; | |
663996b3 | 469 | |
60f067b4 JS |
470 | mode = job_mode_from_string(smode); |
471 | if (mode < 0) | |
472 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Job mode %s invalid", smode); | |
663996b3 | 473 | |
d9dfd233 MP |
474 | if (reload_if_possible) |
475 | verb = strjoin("reload-or-", job_type_to_string(job_type), NULL); | |
476 | else | |
477 | verb = strdup(job_type_to_string(job_type)); | |
478 | if (!verb) | |
479 | return -ENOMEM; | |
480 | ||
481 | r = bus_verify_manage_units_async_full( | |
482 | u, | |
483 | verb, | |
484 | CAP_SYS_ADMIN, | |
485 | job_type < _JOB_TYPE_MAX ? polkit_message_for_job[job_type] : NULL, | |
486 | message, | |
487 | error); | |
e3bff60a MP |
488 | if (r < 0) |
489 | return r; | |
490 | if (r == 0) | |
491 | return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */ | |
492 | ||
493 | return bus_unit_queue_job(message, u, job_type, mode, reload_if_possible, error); | |
60f067b4 | 494 | } |
663996b3 | 495 | |
e3bff60a MP |
496 | static int method_start(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
497 | return bus_unit_method_start_generic(message, userdata, JOB_START, false, error); | |
60f067b4 | 498 | } |
663996b3 | 499 | |
e3bff60a MP |
500 | static int method_stop(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
501 | return bus_unit_method_start_generic(message, userdata, JOB_STOP, false, error); | |
60f067b4 | 502 | } |
663996b3 | 503 | |
e3bff60a MP |
504 | static int method_reload(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
505 | return bus_unit_method_start_generic(message, userdata, JOB_RELOAD, false, error); | |
60f067b4 | 506 | } |
663996b3 | 507 | |
e3bff60a MP |
508 | static int method_restart(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
509 | return bus_unit_method_start_generic(message, userdata, JOB_RESTART, false, error); | |
60f067b4 | 510 | } |
663996b3 | 511 | |
e3bff60a MP |
512 | static int method_try_restart(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
513 | return bus_unit_method_start_generic(message, userdata, JOB_TRY_RESTART, false, error); | |
60f067b4 | 514 | } |
663996b3 | 515 | |
e3bff60a MP |
516 | static int method_reload_or_restart(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
517 | return bus_unit_method_start_generic(message, userdata, JOB_RESTART, true, error); | |
60f067b4 | 518 | } |
663996b3 | 519 | |
e3bff60a MP |
520 | static int method_reload_or_try_restart(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
521 | return bus_unit_method_start_generic(message, userdata, JOB_TRY_RESTART, true, error); | |
60f067b4 | 522 | } |
663996b3 | 523 | |
e3bff60a | 524 | int bus_unit_method_kill(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
60f067b4 JS |
525 | Unit *u = userdata; |
526 | const char *swho; | |
527 | int32_t signo; | |
528 | KillWho who; | |
529 | int r; | |
663996b3 | 530 | |
60f067b4 JS |
531 | assert(message); |
532 | assert(u); | |
663996b3 | 533 | |
e3bff60a | 534 | r = mac_selinux_unit_access_check(u, message, "stop", error); |
5eef597e MP |
535 | if (r < 0) |
536 | return r; | |
5eef597e | 537 | |
60f067b4 JS |
538 | r = sd_bus_message_read(message, "si", &swho, &signo); |
539 | if (r < 0) | |
540 | return r; | |
541 | ||
542 | if (isempty(swho)) | |
543 | who = KILL_ALL; | |
544 | else { | |
545 | who = kill_who_from_string(swho); | |
546 | if (who < 0) | |
547 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid who argument %s", swho); | |
663996b3 MS |
548 | } |
549 | ||
60f067b4 JS |
550 | if (signo <= 0 || signo >= _NSIG) |
551 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Signal number out of range."); | |
552 | ||
d9dfd233 MP |
553 | r = bus_verify_manage_units_async_full( |
554 | u, | |
555 | "kill", | |
556 | CAP_KILL, | |
557 | N_("Authentication is required to kill '$(unit)'."), | |
558 | message, | |
559 | error); | |
60f067b4 JS |
560 | if (r < 0) |
561 | return r; | |
e3bff60a MP |
562 | if (r == 0) |
563 | return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */ | |
663996b3 | 564 | |
60f067b4 JS |
565 | r = unit_kill(u, who, signo, error); |
566 | if (r < 0) | |
567 | return r; | |
663996b3 | 568 | |
60f067b4 | 569 | return sd_bus_reply_method_return(message, NULL); |
663996b3 MS |
570 | } |
571 | ||
e3bff60a | 572 | int bus_unit_method_reset_failed(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
60f067b4 | 573 | Unit *u = userdata; |
663996b3 | 574 | int r; |
663996b3 | 575 | |
663996b3 | 576 | assert(message); |
60f067b4 | 577 | assert(u); |
663996b3 | 578 | |
e3bff60a | 579 | r = mac_selinux_unit_access_check(u, message, "reload", error); |
5eef597e MP |
580 | if (r < 0) |
581 | return r; | |
5eef597e | 582 | |
d9dfd233 MP |
583 | r = bus_verify_manage_units_async_full( |
584 | u, | |
585 | "reset-failed", | |
586 | CAP_SYS_ADMIN, | |
587 | N_("Authentication is required to reset the \"failed\" state of '$(unit)'."), | |
588 | message, | |
589 | error); | |
60f067b4 JS |
590 | if (r < 0) |
591 | return r; | |
e3bff60a MP |
592 | if (r == 0) |
593 | return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */ | |
663996b3 | 594 | |
60f067b4 | 595 | unit_reset_failed(u); |
663996b3 | 596 | |
60f067b4 JS |
597 | return sd_bus_reply_method_return(message, NULL); |
598 | } | |
663996b3 | 599 | |
e3bff60a | 600 | int bus_unit_method_set_properties(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
60f067b4 JS |
601 | Unit *u = userdata; |
602 | int runtime, r; | |
663996b3 | 603 | |
60f067b4 JS |
604 | assert(message); |
605 | assert(u); | |
663996b3 | 606 | |
e3bff60a | 607 | r = mac_selinux_unit_access_check(u, message, "start", error); |
5eef597e MP |
608 | if (r < 0) |
609 | return r; | |
5eef597e | 610 | |
60f067b4 JS |
611 | r = sd_bus_message_read(message, "b", &runtime); |
612 | if (r < 0) | |
613 | return r; | |
663996b3 | 614 | |
d9dfd233 MP |
615 | r = bus_verify_manage_units_async_full( |
616 | u, | |
617 | "set-property", | |
618 | CAP_SYS_ADMIN, | |
619 | N_("Authentication is required to set properties on '$(unit)'."), | |
620 | message, | |
621 | error); | |
60f067b4 JS |
622 | if (r < 0) |
623 | return r; | |
e3bff60a MP |
624 | if (r == 0) |
625 | return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */ | |
663996b3 | 626 | |
60f067b4 JS |
627 | r = bus_unit_set_properties(u, message, runtime ? UNIT_RUNTIME : UNIT_PERSISTENT, true, error); |
628 | if (r < 0) | |
629 | return r; | |
663996b3 | 630 | |
60f067b4 JS |
631 | return sd_bus_reply_method_return(message, NULL); |
632 | } | |
663996b3 | 633 | |
60f067b4 JS |
634 | const sd_bus_vtable bus_unit_vtable[] = { |
635 | SD_BUS_VTABLE_START(0), | |
636 | ||
637 | SD_BUS_PROPERTY("Id", "s", NULL, offsetof(Unit, id), SD_BUS_VTABLE_PROPERTY_CONST), | |
638 | SD_BUS_PROPERTY("Names", "as", property_get_names, 0, SD_BUS_VTABLE_PROPERTY_CONST), | |
639 | SD_BUS_PROPERTY("Following", "s", property_get_following, 0, 0), | |
640 | SD_BUS_PROPERTY("Requires", "as", property_get_dependencies, offsetof(Unit, dependencies[UNIT_REQUIRES]), SD_BUS_VTABLE_PROPERTY_CONST), | |
60f067b4 | 641 | SD_BUS_PROPERTY("Requisite", "as", property_get_dependencies, offsetof(Unit, dependencies[UNIT_REQUISITE]), SD_BUS_VTABLE_PROPERTY_CONST), |
60f067b4 JS |
642 | SD_BUS_PROPERTY("Wants", "as", property_get_dependencies, offsetof(Unit, dependencies[UNIT_WANTS]), SD_BUS_VTABLE_PROPERTY_CONST), |
643 | SD_BUS_PROPERTY("BindsTo", "as", property_get_dependencies, offsetof(Unit, dependencies[UNIT_BINDS_TO]), SD_BUS_VTABLE_PROPERTY_CONST), | |
644 | SD_BUS_PROPERTY("PartOf", "as", property_get_dependencies, offsetof(Unit, dependencies[UNIT_PART_OF]), SD_BUS_VTABLE_PROPERTY_CONST), | |
645 | SD_BUS_PROPERTY("RequiredBy", "as", property_get_dependencies, offsetof(Unit, dependencies[UNIT_REQUIRED_BY]), SD_BUS_VTABLE_PROPERTY_CONST), | |
e3bff60a | 646 | SD_BUS_PROPERTY("RequisiteOf", "as", property_get_dependencies, offsetof(Unit, dependencies[UNIT_REQUISITE_OF]), SD_BUS_VTABLE_PROPERTY_CONST), |
60f067b4 JS |
647 | SD_BUS_PROPERTY("WantedBy", "as", property_get_dependencies, offsetof(Unit, dependencies[UNIT_WANTED_BY]), SD_BUS_VTABLE_PROPERTY_CONST), |
648 | SD_BUS_PROPERTY("BoundBy", "as", property_get_dependencies, offsetof(Unit, dependencies[UNIT_BOUND_BY]), SD_BUS_VTABLE_PROPERTY_CONST), | |
649 | SD_BUS_PROPERTY("ConsistsOf", "as", property_get_dependencies, offsetof(Unit, dependencies[UNIT_CONSISTS_OF]), SD_BUS_VTABLE_PROPERTY_CONST), | |
650 | SD_BUS_PROPERTY("Conflicts", "as", property_get_dependencies, offsetof(Unit, dependencies[UNIT_CONFLICTS]), SD_BUS_VTABLE_PROPERTY_CONST), | |
651 | SD_BUS_PROPERTY("ConflictedBy", "as", property_get_dependencies, offsetof(Unit, dependencies[UNIT_CONFLICTED_BY]), SD_BUS_VTABLE_PROPERTY_CONST), | |
652 | SD_BUS_PROPERTY("Before", "as", property_get_dependencies, offsetof(Unit, dependencies[UNIT_BEFORE]), SD_BUS_VTABLE_PROPERTY_CONST), | |
653 | SD_BUS_PROPERTY("After", "as", property_get_dependencies, offsetof(Unit, dependencies[UNIT_AFTER]), SD_BUS_VTABLE_PROPERTY_CONST), | |
654 | SD_BUS_PROPERTY("OnFailure", "as", property_get_dependencies, offsetof(Unit, dependencies[UNIT_ON_FAILURE]), SD_BUS_VTABLE_PROPERTY_CONST), | |
655 | SD_BUS_PROPERTY("Triggers", "as", property_get_dependencies, offsetof(Unit, dependencies[UNIT_TRIGGERS]), SD_BUS_VTABLE_PROPERTY_CONST), | |
656 | SD_BUS_PROPERTY("TriggeredBy", "as", property_get_dependencies, offsetof(Unit, dependencies[UNIT_TRIGGERED_BY]), SD_BUS_VTABLE_PROPERTY_CONST), | |
657 | SD_BUS_PROPERTY("PropagatesReloadTo", "as", property_get_dependencies, offsetof(Unit, dependencies[UNIT_PROPAGATES_RELOAD_TO]), SD_BUS_VTABLE_PROPERTY_CONST), | |
658 | SD_BUS_PROPERTY("ReloadPropagatedFrom", "as", property_get_dependencies, offsetof(Unit, dependencies[UNIT_RELOAD_PROPAGATED_FROM]), SD_BUS_VTABLE_PROPERTY_CONST), | |
659 | SD_BUS_PROPERTY("JoinsNamespaceOf", "as", property_get_dependencies, offsetof(Unit, dependencies[UNIT_JOINS_NAMESPACE_OF]), SD_BUS_VTABLE_PROPERTY_CONST), | |
db2df898 MP |
660 | SD_BUS_PROPERTY("RequiresOverridable", "as", property_get_obsolete_dependencies, 0, SD_BUS_VTABLE_HIDDEN), |
661 | SD_BUS_PROPERTY("RequisiteOverridable", "as", property_get_obsolete_dependencies, 0, SD_BUS_VTABLE_HIDDEN), | |
662 | SD_BUS_PROPERTY("RequiredByOverridable", "as", property_get_obsolete_dependencies, 0, SD_BUS_VTABLE_HIDDEN), | |
663 | SD_BUS_PROPERTY("RequisiteOfOverridable", "as", property_get_obsolete_dependencies, 0, SD_BUS_VTABLE_HIDDEN), | |
60f067b4 JS |
664 | SD_BUS_PROPERTY("RequiresMountsFor", "as", NULL, offsetof(Unit, requires_mounts_for), SD_BUS_VTABLE_PROPERTY_CONST), |
665 | SD_BUS_PROPERTY("Documentation", "as", NULL, offsetof(Unit, documentation), SD_BUS_VTABLE_PROPERTY_CONST), | |
666 | SD_BUS_PROPERTY("Description", "s", property_get_description, 0, SD_BUS_VTABLE_PROPERTY_CONST), | |
667 | SD_BUS_PROPERTY("LoadState", "s", property_get_load_state, offsetof(Unit, load_state), SD_BUS_VTABLE_PROPERTY_CONST), | |
668 | SD_BUS_PROPERTY("ActiveState", "s", property_get_active_state, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE), | |
669 | SD_BUS_PROPERTY("SubState", "s", property_get_sub_state, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE), | |
670 | SD_BUS_PROPERTY("FragmentPath", "s", NULL, offsetof(Unit, fragment_path), SD_BUS_VTABLE_PROPERTY_CONST), | |
671 | SD_BUS_PROPERTY("SourcePath", "s", NULL, offsetof(Unit, source_path), SD_BUS_VTABLE_PROPERTY_CONST), | |
672 | SD_BUS_PROPERTY("DropInPaths", "as", NULL, offsetof(Unit, dropin_paths), SD_BUS_VTABLE_PROPERTY_CONST), | |
673 | SD_BUS_PROPERTY("UnitFileState", "s", property_get_unit_file_state, 0, 0), | |
f47781d8 | 674 | SD_BUS_PROPERTY("UnitFilePreset", "s", property_get_unit_file_preset, 0, 0), |
4c89c718 | 675 | BUS_PROPERTY_DUAL_TIMESTAMP("StateChangeTimestamp", offsetof(Unit, state_change_timestamp), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE), |
60f067b4 JS |
676 | BUS_PROPERTY_DUAL_TIMESTAMP("InactiveExitTimestamp", offsetof(Unit, inactive_exit_timestamp), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE), |
677 | BUS_PROPERTY_DUAL_TIMESTAMP("ActiveEnterTimestamp", offsetof(Unit, active_enter_timestamp), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE), | |
678 | BUS_PROPERTY_DUAL_TIMESTAMP("ActiveExitTimestamp", offsetof(Unit, active_exit_timestamp), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE), | |
679 | BUS_PROPERTY_DUAL_TIMESTAMP("InactiveEnterTimestamp", offsetof(Unit, inactive_enter_timestamp), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE), | |
680 | SD_BUS_PROPERTY("CanStart", "b", property_get_can_start, 0, SD_BUS_VTABLE_PROPERTY_CONST), | |
681 | SD_BUS_PROPERTY("CanStop", "b", property_get_can_stop, 0, SD_BUS_VTABLE_PROPERTY_CONST), | |
682 | SD_BUS_PROPERTY("CanReload", "b", property_get_can_reload, 0, SD_BUS_VTABLE_PROPERTY_CONST), | |
683 | SD_BUS_PROPERTY("CanIsolate", "b", property_get_can_isolate, 0, SD_BUS_VTABLE_PROPERTY_CONST), | |
684 | SD_BUS_PROPERTY("Job", "(uo)", property_get_job, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE), | |
685 | SD_BUS_PROPERTY("StopWhenUnneeded", "b", bus_property_get_bool, offsetof(Unit, stop_when_unneeded), SD_BUS_VTABLE_PROPERTY_CONST), | |
686 | SD_BUS_PROPERTY("RefuseManualStart", "b", bus_property_get_bool, offsetof(Unit, refuse_manual_start), SD_BUS_VTABLE_PROPERTY_CONST), | |
687 | SD_BUS_PROPERTY("RefuseManualStop", "b", bus_property_get_bool, offsetof(Unit, refuse_manual_stop), SD_BUS_VTABLE_PROPERTY_CONST), | |
688 | SD_BUS_PROPERTY("AllowIsolate", "b", bus_property_get_bool, offsetof(Unit, allow_isolate), SD_BUS_VTABLE_PROPERTY_CONST), | |
689 | SD_BUS_PROPERTY("DefaultDependencies", "b", bus_property_get_bool, offsetof(Unit, default_dependencies), SD_BUS_VTABLE_PROPERTY_CONST), | |
690 | SD_BUS_PROPERTY("OnFailureJobMode", "s", property_get_job_mode, offsetof(Unit, on_failure_job_mode), SD_BUS_VTABLE_PROPERTY_CONST), | |
691 | SD_BUS_PROPERTY("IgnoreOnIsolate", "b", bus_property_get_bool, offsetof(Unit, ignore_on_isolate), SD_BUS_VTABLE_PROPERTY_CONST), | |
60f067b4 JS |
692 | SD_BUS_PROPERTY("NeedDaemonReload", "b", property_get_need_daemon_reload, 0, SD_BUS_VTABLE_PROPERTY_CONST), |
693 | SD_BUS_PROPERTY("JobTimeoutUSec", "t", bus_property_get_usec, offsetof(Unit, job_timeout), SD_BUS_VTABLE_PROPERTY_CONST), | |
5eef597e MP |
694 | SD_BUS_PROPERTY("JobTimeoutAction", "s", property_get_failure_action, offsetof(Unit, job_timeout_action), SD_BUS_VTABLE_PROPERTY_CONST), |
695 | SD_BUS_PROPERTY("JobTimeoutRebootArgument", "s", NULL, offsetof(Unit, job_timeout_reboot_arg), SD_BUS_VTABLE_PROPERTY_CONST), | |
60f067b4 | 696 | SD_BUS_PROPERTY("ConditionResult", "b", bus_property_get_bool, offsetof(Unit, condition_result), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE), |
f47781d8 | 697 | SD_BUS_PROPERTY("AssertResult", "b", bus_property_get_bool, offsetof(Unit, assert_result), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE), |
60f067b4 | 698 | BUS_PROPERTY_DUAL_TIMESTAMP("ConditionTimestamp", offsetof(Unit, condition_timestamp), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE), |
f47781d8 MP |
699 | BUS_PROPERTY_DUAL_TIMESTAMP("AssertTimestamp", offsetof(Unit, assert_timestamp), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE), |
700 | SD_BUS_PROPERTY("Conditions", "a(sbbsi)", property_get_conditions, offsetof(Unit, conditions), 0), | |
701 | SD_BUS_PROPERTY("Asserts", "a(sbbsi)", property_get_conditions, offsetof(Unit, asserts), 0), | |
60f067b4 JS |
702 | SD_BUS_PROPERTY("LoadError", "(ss)", property_get_load_error, 0, SD_BUS_VTABLE_PROPERTY_CONST), |
703 | SD_BUS_PROPERTY("Transient", "b", bus_property_get_bool, offsetof(Unit, transient), SD_BUS_VTABLE_PROPERTY_CONST), | |
4c89c718 MP |
704 | SD_BUS_PROPERTY("StartLimitInterval", "t", bus_property_get_usec, offsetof(Unit, start_limit.interval), SD_BUS_VTABLE_PROPERTY_CONST), |
705 | SD_BUS_PROPERTY("StartLimitBurst", "u", bus_property_get_unsigned, offsetof(Unit, start_limit.burst), SD_BUS_VTABLE_PROPERTY_CONST), | |
706 | SD_BUS_PROPERTY("StartLimitAction", "s", property_get_failure_action, offsetof(Unit, start_limit_action), SD_BUS_VTABLE_PROPERTY_CONST), | |
707 | SD_BUS_PROPERTY("RebootArgument", "s", NULL, offsetof(Unit, reboot_arg), SD_BUS_VTABLE_PROPERTY_CONST), | |
60f067b4 | 708 | |
e3bff60a MP |
709 | SD_BUS_METHOD("Start", "s", "o", method_start, SD_BUS_VTABLE_UNPRIVILEGED), |
710 | SD_BUS_METHOD("Stop", "s", "o", method_stop, SD_BUS_VTABLE_UNPRIVILEGED), | |
711 | SD_BUS_METHOD("Reload", "s", "o", method_reload, SD_BUS_VTABLE_UNPRIVILEGED), | |
712 | SD_BUS_METHOD("Restart", "s", "o", method_restart, SD_BUS_VTABLE_UNPRIVILEGED), | |
713 | SD_BUS_METHOD("TryRestart", "s", "o", method_try_restart, SD_BUS_VTABLE_UNPRIVILEGED), | |
714 | SD_BUS_METHOD("ReloadOrRestart", "s", "o", method_reload_or_restart, SD_BUS_VTABLE_UNPRIVILEGED), | |
715 | SD_BUS_METHOD("ReloadOrTryRestart", "s", "o", method_reload_or_try_restart, SD_BUS_VTABLE_UNPRIVILEGED), | |
716 | SD_BUS_METHOD("Kill", "si", NULL, bus_unit_method_kill, SD_BUS_VTABLE_UNPRIVILEGED), | |
717 | SD_BUS_METHOD("ResetFailed", NULL, NULL, bus_unit_method_reset_failed, SD_BUS_VTABLE_UNPRIVILEGED), | |
718 | SD_BUS_METHOD("SetProperties", "ba(sv)", NULL, bus_unit_method_set_properties, SD_BUS_VTABLE_UNPRIVILEGED), | |
60f067b4 JS |
719 | |
720 | SD_BUS_VTABLE_END | |
721 | }; | |
663996b3 | 722 | |
60f067b4 JS |
723 | static int property_get_slice( |
724 | sd_bus *bus, | |
725 | const char *path, | |
726 | const char *interface, | |
727 | const char *property, | |
728 | sd_bus_message *reply, | |
729 | void *userdata, | |
730 | sd_bus_error *error) { | |
663996b3 | 731 | |
60f067b4 | 732 | Unit *u = userdata; |
663996b3 | 733 | |
60f067b4 JS |
734 | assert(bus); |
735 | assert(reply); | |
736 | assert(u); | |
663996b3 | 737 | |
60f067b4 JS |
738 | return sd_bus_message_append(reply, "s", unit_slice_name(u)); |
739 | } | |
663996b3 | 740 | |
e735f4d4 MP |
741 | static int property_get_current_memory( |
742 | sd_bus *bus, | |
743 | const char *path, | |
744 | const char *interface, | |
745 | const char *property, | |
746 | sd_bus_message *reply, | |
747 | void *userdata, | |
748 | sd_bus_error *error) { | |
749 | ||
e735f4d4 | 750 | uint64_t sz = (uint64_t) -1; |
e3bff60a | 751 | Unit *u = userdata; |
e735f4d4 MP |
752 | int r; |
753 | ||
754 | assert(bus); | |
755 | assert(reply); | |
756 | assert(u); | |
757 | ||
e3bff60a MP |
758 | r = unit_get_memory_current(u, &sz); |
759 | if (r < 0 && r != -ENODATA) | |
760 | log_unit_warning_errno(u, r, "Failed to get memory.usage_in_bytes attribute: %m"); | |
e735f4d4 | 761 | |
e3bff60a MP |
762 | return sd_bus_message_append(reply, "t", sz); |
763 | } | |
e735f4d4 | 764 | |
6300502b MP |
765 | static int property_get_current_tasks( |
766 | sd_bus *bus, | |
767 | const char *path, | |
768 | const char *interface, | |
769 | const char *property, | |
770 | sd_bus_message *reply, | |
771 | void *userdata, | |
772 | sd_bus_error *error) { | |
773 | ||
774 | uint64_t cn = (uint64_t) -1; | |
775 | Unit *u = userdata; | |
776 | int r; | |
777 | ||
778 | assert(bus); | |
779 | assert(reply); | |
780 | assert(u); | |
781 | ||
782 | r = unit_get_tasks_current(u, &cn); | |
783 | if (r < 0 && r != -ENODATA) | |
784 | log_unit_warning_errno(u, r, "Failed to get pids.current attribute: %m"); | |
785 | ||
786 | return sd_bus_message_append(reply, "t", cn); | |
787 | } | |
788 | ||
e3bff60a MP |
789 | static int property_get_cpu_usage( |
790 | sd_bus *bus, | |
791 | const char *path, | |
792 | const char *interface, | |
793 | const char *property, | |
794 | sd_bus_message *reply, | |
795 | void *userdata, | |
796 | sd_bus_error *error) { | |
e735f4d4 | 797 | |
e3bff60a MP |
798 | nsec_t ns = (nsec_t) -1; |
799 | Unit *u = userdata; | |
800 | int r; | |
801 | ||
802 | assert(bus); | |
803 | assert(reply); | |
804 | assert(u); | |
805 | ||
806 | r = unit_get_cpu_usage(u, &ns); | |
807 | if (r < 0 && r != -ENODATA) | |
808 | log_unit_warning_errno(u, r, "Failed to get cpuacct.usage attribute: %m"); | |
809 | ||
810 | return sd_bus_message_append(reply, "t", ns); | |
e735f4d4 MP |
811 | } |
812 | ||
13d276d0 MP |
813 | static int property_get_cgroup( |
814 | sd_bus *bus, | |
815 | const char *path, | |
816 | const char *interface, | |
817 | const char *property, | |
818 | sd_bus_message *reply, | |
819 | void *userdata, | |
820 | sd_bus_error *error) { | |
821 | ||
822 | Unit *u = userdata; | |
823 | const char *t; | |
824 | ||
825 | assert(bus); | |
826 | assert(reply); | |
827 | assert(u); | |
828 | ||
829 | /* Three cases: a) u->cgroup_path is NULL, in which case the | |
830 | * unit has no control group, which we report as the empty | |
831 | * string. b) u->cgroup_path is the empty string, which | |
832 | * indicates the root cgroup, which we report as "/". c) all | |
833 | * other cases we report as-is. */ | |
834 | ||
835 | if (u->cgroup_path) | |
836 | t = isempty(u->cgroup_path) ? "/" : u->cgroup_path; | |
837 | else | |
838 | t = ""; | |
839 | ||
840 | return sd_bus_message_append(reply, "s", t); | |
841 | } | |
842 | ||
60f067b4 JS |
843 | const sd_bus_vtable bus_unit_cgroup_vtable[] = { |
844 | SD_BUS_VTABLE_START(0), | |
845 | SD_BUS_PROPERTY("Slice", "s", property_get_slice, 0, 0), | |
13d276d0 | 846 | SD_BUS_PROPERTY("ControlGroup", "s", property_get_cgroup, 0, 0), |
e735f4d4 | 847 | SD_BUS_PROPERTY("MemoryCurrent", "t", property_get_current_memory, 0, 0), |
e3bff60a | 848 | SD_BUS_PROPERTY("CPUUsageNSec", "t", property_get_cpu_usage, 0, 0), |
6300502b | 849 | SD_BUS_PROPERTY("TasksCurrent", "t", property_get_current_tasks, 0, 0), |
60f067b4 JS |
850 | SD_BUS_VTABLE_END |
851 | }; | |
663996b3 | 852 | |
60f067b4 | 853 | static int send_new_signal(sd_bus *bus, void *userdata) { |
4c89c718 | 854 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL; |
60f067b4 JS |
855 | _cleanup_free_ char *p = NULL; |
856 | Unit *u = userdata; | |
857 | int r; | |
663996b3 | 858 | |
60f067b4 JS |
859 | assert(bus); |
860 | assert(u); | |
663996b3 | 861 | |
60f067b4 | 862 | p = unit_dbus_path(u); |
e3bff60a | 863 | if (!p) |
60f067b4 | 864 | return -ENOMEM; |
663996b3 | 865 | |
60f067b4 JS |
866 | r = sd_bus_message_new_signal( |
867 | bus, | |
868 | &m, | |
869 | "/org/freedesktop/systemd1", | |
870 | "org.freedesktop.systemd1.Manager", | |
871 | "UnitNew"); | |
872 | if (r < 0) | |
873 | return r; | |
663996b3 | 874 | |
60f067b4 JS |
875 | r = sd_bus_message_append(m, "so", u->id, p); |
876 | if (r < 0) | |
877 | return r; | |
663996b3 | 878 | |
60f067b4 JS |
879 | return sd_bus_send(bus, m, NULL); |
880 | } | |
663996b3 | 881 | |
60f067b4 JS |
882 | static int send_changed_signal(sd_bus *bus, void *userdata) { |
883 | _cleanup_free_ char *p = NULL; | |
884 | Unit *u = userdata; | |
885 | int r; | |
663996b3 | 886 | |
60f067b4 JS |
887 | assert(bus); |
888 | assert(u); | |
663996b3 | 889 | |
60f067b4 JS |
890 | p = unit_dbus_path(u); |
891 | if (!p) | |
892 | return -ENOMEM; | |
663996b3 | 893 | |
60f067b4 JS |
894 | /* Send a properties changed signal. First for the specific |
895 | * type, then for the generic unit. The clients may rely on | |
896 | * this order to get atomic behavior if needed. */ | |
663996b3 | 897 | |
60f067b4 JS |
898 | r = sd_bus_emit_properties_changed_strv( |
899 | bus, p, | |
d9dfd233 | 900 | unit_dbus_interface_from_type(u->type), |
60f067b4 JS |
901 | NULL); |
902 | if (r < 0) | |
903 | return r; | |
663996b3 | 904 | |
60f067b4 JS |
905 | return sd_bus_emit_properties_changed_strv( |
906 | bus, p, | |
907 | "org.freedesktop.systemd1.Unit", | |
908 | NULL); | |
909 | } | |
663996b3 MS |
910 | |
911 | void bus_unit_send_change_signal(Unit *u) { | |
14228c0d | 912 | int r; |
663996b3 MS |
913 | assert(u); |
914 | ||
915 | if (u->in_dbus_queue) { | |
60f067b4 | 916 | LIST_REMOVE(dbus_queue, u->manager->dbus_unit_queue, u); |
663996b3 MS |
917 | u->in_dbus_queue = false; |
918 | } | |
919 | ||
920 | if (!u->id) | |
921 | return; | |
922 | ||
60f067b4 JS |
923 | r = bus_foreach_bus(u->manager, NULL, u->sent_dbus_new_signal ? send_changed_signal : send_new_signal, u); |
924 | if (r < 0) | |
e3bff60a | 925 | log_unit_debug_errno(u, r, "Failed to send unit change signal for %s: %m", u->id); |
663996b3 | 926 | |
60f067b4 JS |
927 | u->sent_dbus_new_signal = true; |
928 | } | |
663996b3 | 929 | |
60f067b4 | 930 | static int send_removed_signal(sd_bus *bus, void *userdata) { |
4c89c718 | 931 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL; |
60f067b4 JS |
932 | _cleanup_free_ char *p = NULL; |
933 | Unit *u = userdata; | |
934 | int r; | |
663996b3 | 935 | |
60f067b4 JS |
936 | assert(bus); |
937 | assert(u); | |
663996b3 | 938 | |
60f067b4 | 939 | p = unit_dbus_path(u); |
e3bff60a | 940 | if (!p) |
60f067b4 | 941 | return -ENOMEM; |
663996b3 | 942 | |
60f067b4 JS |
943 | r = sd_bus_message_new_signal( |
944 | bus, | |
945 | &m, | |
946 | "/org/freedesktop/systemd1", | |
947 | "org.freedesktop.systemd1.Manager", | |
948 | "UnitRemoved"); | |
949 | if (r < 0) | |
950 | return r; | |
663996b3 | 951 | |
60f067b4 JS |
952 | r = sd_bus_message_append(m, "so", u->id, p); |
953 | if (r < 0) | |
954 | return r; | |
663996b3 | 955 | |
60f067b4 | 956 | return sd_bus_send(bus, m, NULL); |
663996b3 MS |
957 | } |
958 | ||
959 | void bus_unit_send_removed_signal(Unit *u) { | |
60f067b4 | 960 | int r; |
663996b3 MS |
961 | assert(u); |
962 | ||
663996b3 MS |
963 | if (!u->sent_dbus_new_signal) |
964 | bus_unit_send_change_signal(u); | |
965 | ||
966 | if (!u->id) | |
967 | return; | |
968 | ||
60f067b4 JS |
969 | r = bus_foreach_bus(u->manager, NULL, send_removed_signal, u); |
970 | if (r < 0) | |
e3bff60a | 971 | log_unit_debug_errno(u, r, "Failed to send unit remove signal for %s: %m", u->id); |
663996b3 MS |
972 | } |
973 | ||
60f067b4 | 974 | int bus_unit_queue_job( |
60f067b4 | 975 | sd_bus_message *message, |
663996b3 MS |
976 | Unit *u, |
977 | JobType type, | |
978 | JobMode mode, | |
60f067b4 JS |
979 | bool reload_if_possible, |
980 | sd_bus_error *error) { | |
663996b3 | 981 | |
663996b3 MS |
982 | _cleanup_free_ char *path = NULL; |
983 | Job *j; | |
663996b3 MS |
984 | int r; |
985 | ||
663996b3 MS |
986 | assert(message); |
987 | assert(u); | |
988 | assert(type >= 0 && type < _JOB_TYPE_MAX); | |
989 | assert(mode >= 0 && mode < _JOB_MODE_MAX); | |
990 | ||
4c89c718 MP |
991 | r = mac_selinux_unit_access_check( |
992 | u, message, | |
993 | job_type_to_access_method(type), | |
994 | error); | |
995 | if (r < 0) | |
996 | return r; | |
997 | ||
663996b3 MS |
998 | if (reload_if_possible && unit_can_reload(u)) { |
999 | if (type == JOB_RESTART) | |
1000 | type = JOB_RELOAD_OR_START; | |
1001 | else if (type == JOB_TRY_RESTART) | |
4c89c718 | 1002 | type = JOB_TRY_RELOAD; |
663996b3 MS |
1003 | } |
1004 | ||
663996b3 | 1005 | |
60f067b4 JS |
1006 | if (type == JOB_STOP && |
1007 | (u->load_state == UNIT_NOT_FOUND || u->load_state == UNIT_ERROR) && | |
1008 | unit_active_state(u) == UNIT_INACTIVE) | |
1009 | return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_UNIT, "Unit %s not loaded.", u->id); | |
663996b3 MS |
1010 | |
1011 | if ((type == JOB_START && u->refuse_manual_start) || | |
1012 | (type == JOB_STOP && u->refuse_manual_stop) || | |
db2df898 MP |
1013 | ((type == JOB_RESTART || type == JOB_TRY_RESTART) && (u->refuse_manual_start || u->refuse_manual_stop)) || |
1014 | (type == JOB_RELOAD_OR_START && job_type_collapse(type, u) == JOB_START && u->refuse_manual_start)) | |
60f067b4 | 1015 | return sd_bus_error_setf(error, BUS_ERROR_ONLY_BY_DEPENDENCY, "Operation refused, unit %s may be requested by dependency only.", u->id); |
663996b3 | 1016 | |
db2df898 | 1017 | r = manager_add_job(u->manager, type, u, mode, error, &j); |
663996b3 | 1018 | if (r < 0) |
60f067b4 | 1019 | return r; |
663996b3 | 1020 | |
e3bff60a | 1021 | if (sd_bus_message_get_bus(message) == u->manager->api_bus) { |
5eef597e | 1022 | if (!j->clients) { |
e3bff60a | 1023 | r = sd_bus_track_new(sd_bus_message_get_bus(message), &j->clients, NULL, NULL); |
60f067b4 JS |
1024 | if (r < 0) |
1025 | return r; | |
1026 | } | |
663996b3 | 1027 | |
5eef597e | 1028 | r = sd_bus_track_add_sender(j->clients, message); |
60f067b4 JS |
1029 | if (r < 0) |
1030 | return r; | |
1031 | } | |
663996b3 MS |
1032 | |
1033 | path = job_dbus_path(j); | |
1034 | if (!path) | |
60f067b4 | 1035 | return -ENOMEM; |
663996b3 | 1036 | |
60f067b4 | 1037 | return sd_bus_reply_method_return(message, "o", path); |
663996b3 MS |
1038 | } |
1039 | ||
14228c0d MB |
1040 | static int bus_unit_set_transient_property( |
1041 | Unit *u, | |
1042 | const char *name, | |
60f067b4 | 1043 | sd_bus_message *message, |
14228c0d | 1044 | UnitSetPropertiesMode mode, |
60f067b4 | 1045 | sd_bus_error *error) { |
663996b3 | 1046 | |
663996b3 MS |
1047 | int r; |
1048 | ||
1049 | assert(u); | |
14228c0d | 1050 | assert(name); |
60f067b4 | 1051 | assert(message); |
663996b3 | 1052 | |
14228c0d | 1053 | if (streq(name, "Description")) { |
60f067b4 | 1054 | const char *d; |
663996b3 | 1055 | |
60f067b4 JS |
1056 | r = sd_bus_message_read(message, "s", &d); |
1057 | if (r < 0) | |
1058 | return r; | |
663996b3 | 1059 | |
60f067b4 JS |
1060 | if (mode != UNIT_CHECK) { |
1061 | r = unit_set_description(u, d); | |
14228c0d MB |
1062 | if (r < 0) |
1063 | return r; | |
663996b3 | 1064 | |
60f067b4 | 1065 | unit_write_drop_in_format(u, mode, name, "[Unit]\nDescription=%s\n", d); |
14228c0d | 1066 | } |
663996b3 | 1067 | |
14228c0d | 1068 | return 1; |
e735f4d4 MP |
1069 | |
1070 | } else if (streq(name, "DefaultDependencies")) { | |
1071 | int b; | |
1072 | ||
1073 | r = sd_bus_message_read(message, "b", &b); | |
1074 | if (r < 0) | |
1075 | return r; | |
1076 | ||
1077 | if (mode != UNIT_CHECK) { | |
1078 | u->default_dependencies = b; | |
1079 | unit_write_drop_in_format(u, mode, name, "[Unit]\nDefaultDependencies=%s\n", yes_no(b)); | |
1080 | } | |
1081 | ||
1082 | return 1; | |
663996b3 | 1083 | |
d9dfd233 MP |
1084 | } else if (streq(name, "Slice")) { |
1085 | Unit *slice; | |
14228c0d | 1086 | const char *s; |
663996b3 | 1087 | |
d9dfd233 MP |
1088 | if (!UNIT_HAS_CGROUP_CONTEXT(u)) |
1089 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "The slice property is only available for units with control groups."); | |
1090 | if (u->type == UNIT_SLICE) | |
1091 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Slice may not be set for slice units."); | |
1092 | if (unit_has_name(u, SPECIAL_INIT_SCOPE)) | |
1093 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Cannot set slice for init.scope"); | |
1094 | ||
60f067b4 JS |
1095 | r = sd_bus_message_read(message, "s", &s); |
1096 | if (r < 0) | |
1097 | return r; | |
663996b3 | 1098 | |
d9dfd233 MP |
1099 | if (!unit_name_is_valid(s, UNIT_NAME_PLAIN)) |
1100 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid unit name '%s'", s); | |
663996b3 | 1101 | |
d9dfd233 MP |
1102 | r = manager_load_unit(u->manager, s, NULL, error, &slice); |
1103 | if (r < 0) | |
1104 | return r; | |
663996b3 | 1105 | |
d9dfd233 MP |
1106 | if (slice->type != UNIT_SLICE) |
1107 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Unit name '%s' is not a slice", s); | |
1108 | ||
1109 | if (mode != UNIT_CHECK) { | |
1110 | r = unit_set_slice(u, slice); | |
14228c0d MB |
1111 | if (r < 0) |
1112 | return r; | |
663996b3 | 1113 | |
d9dfd233 | 1114 | unit_write_drop_in_private_format(u, mode, name, "Slice=%s\n", s); |
14228c0d | 1115 | } |
663996b3 | 1116 | |
14228c0d | 1117 | return 1; |
d9dfd233 | 1118 | |
f47781d8 MP |
1119 | } else if (STR_IN_SET(name, |
1120 | "Requires", "RequiresOverridable", | |
1121 | "Requisite", "RequisiteOverridable", | |
1122 | "Wants", | |
1123 | "BindsTo", | |
1124 | "Conflicts", | |
1125 | "Before", "After", | |
1126 | "OnFailure", | |
1127 | "PropagatesReloadTo", "ReloadPropagatedFrom", | |
1128 | "PartOf")) { | |
14228c0d MB |
1129 | |
1130 | UnitDependency d; | |
60f067b4 | 1131 | const char *other; |
14228c0d | 1132 | |
db2df898 MP |
1133 | if (streq(name, "RequiresOverridable")) |
1134 | d = UNIT_REQUIRES; /* redirect for obsolete unit dependency type */ | |
1135 | else if (streq(name, "RequisiteOverridable")) | |
1136 | d = UNIT_REQUISITE; /* same here */ | |
1137 | else { | |
1138 | d = unit_dependency_from_string(name); | |
1139 | if (d < 0) | |
1140 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid unit dependency: %s", name); | |
1141 | } | |
663996b3 | 1142 | |
60f067b4 JS |
1143 | r = sd_bus_message_enter_container(message, 'a', "s"); |
1144 | if (r < 0) | |
1145 | return r; | |
663996b3 | 1146 | |
60f067b4 | 1147 | while ((r = sd_bus_message_read(message, "s", &other)) > 0) { |
e3bff60a | 1148 | if (!unit_name_is_valid(other, UNIT_NAME_PLAIN|UNIT_NAME_INSTANCE)) |
60f067b4 | 1149 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid unit name %s", other); |
663996b3 | 1150 | |
14228c0d MB |
1151 | if (mode != UNIT_CHECK) { |
1152 | _cleanup_free_ char *label = NULL; | |
663996b3 | 1153 | |
14228c0d MB |
1154 | r = unit_add_dependency_by_name(u, d, other, NULL, true); |
1155 | if (r < 0) | |
1156 | return r; | |
663996b3 | 1157 | |
14228c0d MB |
1158 | label = strjoin(name, "-", other, NULL); |
1159 | if (!label) | |
1160 | return -ENOMEM; | |
663996b3 | 1161 | |
14228c0d MB |
1162 | unit_write_drop_in_format(u, mode, label, "[Unit]\n%s=%s\n", name, other); |
1163 | } | |
663996b3 | 1164 | |
14228c0d | 1165 | } |
60f067b4 JS |
1166 | if (r < 0) |
1167 | return r; | |
1168 | ||
1169 | r = sd_bus_message_exit_container(message); | |
1170 | if (r < 0) | |
1171 | return r; | |
663996b3 | 1172 | |
14228c0d | 1173 | return 1; |
663996b3 MS |
1174 | } |
1175 | ||
1176 | return 0; | |
1177 | } | |
1178 | ||
14228c0d MB |
1179 | int bus_unit_set_properties( |
1180 | Unit *u, | |
60f067b4 | 1181 | sd_bus_message *message, |
14228c0d MB |
1182 | UnitSetPropertiesMode mode, |
1183 | bool commit, | |
60f067b4 | 1184 | sd_bus_error *error) { |
14228c0d MB |
1185 | |
1186 | bool for_real = false; | |
14228c0d | 1187 | unsigned n = 0; |
663996b3 MS |
1188 | int r; |
1189 | ||
1190 | assert(u); | |
60f067b4 | 1191 | assert(message); |
663996b3 | 1192 | |
14228c0d MB |
1193 | /* We iterate through the array twice. First run we just check |
1194 | * if all passed data is valid, second run actually applies | |
1195 | * it. This is to implement transaction-like behaviour without | |
1196 | * actually providing full transactions. */ | |
663996b3 | 1197 | |
60f067b4 JS |
1198 | r = sd_bus_message_enter_container(message, 'a', "(sv)"); |
1199 | if (r < 0) | |
1200 | return r; | |
663996b3 | 1201 | |
14228c0d | 1202 | for (;;) { |
14228c0d | 1203 | const char *name; |
663996b3 | 1204 | |
60f067b4 JS |
1205 | r = sd_bus_message_enter_container(message, 'r', "sv"); |
1206 | if (r < 0) | |
1207 | return r; | |
1208 | if (r == 0) { | |
14228c0d MB |
1209 | if (for_real || mode == UNIT_CHECK) |
1210 | break; | |
663996b3 | 1211 | |
14228c0d | 1212 | /* Reached EOF. Let's try again, and this time for realz... */ |
60f067b4 JS |
1213 | r = sd_bus_message_rewind(message, false); |
1214 | if (r < 0) | |
1215 | return r; | |
1216 | ||
14228c0d | 1217 | for_real = true; |
663996b3 | 1218 | continue; |
663996b3 | 1219 | } |
663996b3 | 1220 | |
60f067b4 JS |
1221 | r = sd_bus_message_read(message, "s", &name); |
1222 | if (r < 0) | |
1223 | return r; | |
663996b3 | 1224 | |
60f067b4 JS |
1225 | if (!UNIT_VTABLE(u)->bus_set_property) |
1226 | return sd_bus_error_setf(error, SD_BUS_ERROR_PROPERTY_READ_ONLY, "Objects of this type do not support setting properties."); | |
663996b3 | 1227 | |
60f067b4 JS |
1228 | r = sd_bus_message_enter_container(message, 'v', NULL); |
1229 | if (r < 0) | |
1230 | return r; | |
663996b3 | 1231 | |
60f067b4 | 1232 | r = UNIT_VTABLE(u)->bus_set_property(u, name, message, for_real ? mode : UNIT_CHECK, error); |
14228c0d | 1233 | if (r == 0 && u->transient && u->load_state == UNIT_STUB) |
60f067b4 | 1234 | r = bus_unit_set_transient_property(u, name, message, for_real ? mode : UNIT_CHECK, error); |
663996b3 MS |
1235 | if (r < 0) |
1236 | return r; | |
60f067b4 JS |
1237 | if (r == 0) |
1238 | return sd_bus_error_setf(error, SD_BUS_ERROR_PROPERTY_READ_ONLY, "Cannot set property %s, or unknown property.", name); | |
663996b3 | 1239 | |
60f067b4 JS |
1240 | r = sd_bus_message_exit_container(message); |
1241 | if (r < 0) | |
1242 | return r; | |
1243 | ||
1244 | r = sd_bus_message_exit_container(message); | |
1245 | if (r < 0) | |
1246 | return r; | |
663996b3 | 1247 | |
14228c0d MB |
1248 | n += for_real; |
1249 | } | |
663996b3 | 1250 | |
60f067b4 JS |
1251 | r = sd_bus_message_exit_container(message); |
1252 | if (r < 0) | |
1253 | return r; | |
1254 | ||
14228c0d MB |
1255 | if (commit && n > 0 && UNIT_VTABLE(u)->bus_commit_properties) |
1256 | UNIT_VTABLE(u)->bus_commit_properties(u); | |
663996b3 | 1257 | |
14228c0d | 1258 | return n; |
663996b3 | 1259 | } |
4c89c718 MP |
1260 | |
1261 | int bus_unit_check_load_state(Unit *u, sd_bus_error *error) { | |
1262 | ||
1263 | if (u->load_state == UNIT_LOADED) | |
1264 | return 0; | |
1265 | ||
1266 | /* Give a better description of the unit error when | |
1267 | * possible. Note that in the case of UNIT_MASKED, load_error | |
1268 | * is not set. */ | |
1269 | if (u->load_state == UNIT_MASKED) | |
1270 | return sd_bus_error_setf(error, BUS_ERROR_UNIT_MASKED, "Unit %s is masked.", u->id); | |
1271 | ||
1272 | if (u->load_state == UNIT_NOT_FOUND) | |
1273 | return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_UNIT, "Unit %s not found.", u->id); | |
1274 | ||
1275 | return sd_bus_error_set_errnof(error, u->load_error, "Unit %s is not loaded properly: %m.", u->id); | |
1276 | } |