]>
Commit | Line | Data |
---|---|---|
db9ecf05 | 1 | /* SPDX-License-Identifier: LGPL-2.1-or-later */ |
a7334b09 | 2 | |
ea430986 | 3 | #include <errno.h> |
eed67a30 | 4 | #include <sys/prctl.h> |
ae57dad3 | 5 | #include <sys/statvfs.h> |
871c44a7 | 6 | #include <unistd.h> |
ea430986 | 7 | |
b5efdb8a | 8 | #include "alloc-util.h" |
07630cea | 9 | #include "architecture.h" |
09bde77f | 10 | #include "build.h" |
07630cea | 11 | #include "bus-common-errors.h" |
40af3d02 | 12 | #include "bus-get-properties.h" |
25141692 | 13 | #include "bus-log-control-api.h" |
4ac08d8a | 14 | #include "bus-util.h" |
f461a28d | 15 | #include "chase.h" |
1257274a | 16 | #include "confidential-virt.h" |
6a818c3c | 17 | #include "data-fd-util.h" |
3a0f06c4 | 18 | #include "dbus-cgroup.h" |
07630cea | 19 | #include "dbus-execute.h" |
2ea31e5b | 20 | #include "dbus-job.h" |
3ffd4af2 | 21 | #include "dbus-manager.h" |
c20076a8 | 22 | #include "dbus-scope.h" |
5e8deb94 | 23 | #include "dbus-service.h" |
07630cea LP |
24 | #include "dbus-unit.h" |
25 | #include "dbus.h" | |
26 | #include "env-util.h" | |
3ffd4af2 | 27 | #include "fd-util.h" |
0d39fa9c | 28 | #include "fileio.h" |
f97b34a6 | 29 | #include "format-util.h" |
4da159bc | 30 | #include "initrd-util.h" |
07630cea LP |
31 | #include "install.h" |
32 | #include "log.h" | |
2a341bb9 | 33 | #include "manager-dump.h" |
d58ad743 | 34 | #include "os-util.h" |
ae57dad3 | 35 | #include "parse-util.h" |
07630cea | 36 | #include "path-util.h" |
d4a402e4 | 37 | #include "process-util.h" |
07630cea | 38 | #include "selinux-access.h" |
8fcde012 | 39 | #include "stat-util.h" |
07630cea LP |
40 | #include "string-util.h" |
41 | #include "strv.h" | |
7ccbd1ae | 42 | #include "syslog-util.h" |
d851637c | 43 | #include "taint.h" |
29206d46 | 44 | #include "user-util.h" |
bdb577f5 | 45 | #include "version.h" |
07630cea LP |
46 | #include "virt.h" |
47 | #include "watchdog.h" | |
ea430986 | 48 | |
cb31470f ZJS |
49 | /* Require 16MiB free in /run/systemd for reloading/reexecing. After all we need to serialize our state |
50 | * there, and if we can't we'll fail badly. */ | |
ae57dad3 LP |
51 | #define RELOAD_DISK_SPACE_MIN (UINT64_C(16) * UINT64_C(1024) * UINT64_C(1024)) |
52 | ||
b3796dd8 JS |
53 | static UnitFileFlags unit_file_bools_to_flags(bool runtime, bool force) { |
54 | return (runtime ? UNIT_FILE_RUNTIME : 0) | | |
55 | (force ? UNIT_FILE_FORCE : 0); | |
56 | } | |
57 | ||
afcfaa69 | 58 | BUS_DEFINE_PROPERTY_GET_ENUM(bus_property_get_oom_policy, oom_policy, OOMPolicy); |
c44a285c | 59 | BUS_DEFINE_PROPERTY_GET_ENUM(bus_property_get_emergency_action, emergency_action, EmergencyAction); |
afcfaa69 | 60 | |
681bd2c5 | 61 | static BUS_DEFINE_PROPERTY_GET_GLOBAL(property_get_version, "s", GIT_VERSION); |
91b79ba8 | 62 | static BUS_DEFINE_PROPERTY_GET_GLOBAL(property_get_features, "s", systemd_features); |
23c9a63a | 63 | static BUS_DEFINE_PROPERTY_GET_GLOBAL(property_get_architecture, "s", architecture_to_string(uname_architecture())); |
23c9a63a YW |
64 | static BUS_DEFINE_PROPERTY_GET2(property_get_system_state, "s", Manager, manager_state, manager_state_to_string); |
65 | static BUS_DEFINE_PROPERTY_GET_GLOBAL(property_get_timer_slack_nsec, "t", (uint64_t) prctl(PR_GET_TIMERSLACK)); | |
92c23c5a YW |
66 | static BUS_DEFINE_PROPERTY_GET_REF(property_get_hashmap_size, "u", Hashmap *, hashmap_size); |
67 | static BUS_DEFINE_PROPERTY_GET_REF(property_get_set_size, "u", Set *, set_size); | |
3bf0cb65 | 68 | static BUS_DEFINE_PROPERTY_GET(property_get_default_timeout_abort_usec, "t", Manager, manager_default_timeout_abort_usec); |
10f3f4ed | 69 | static BUS_DEFINE_PROPERTY_GET_GLOBAL(property_get_watchdog_device, "s", watchdog_get_device()); |
10f3f4ed YW |
70 | static BUS_DEFINE_PROPERTY_GET_GLOBAL(property_get_watchdog_last_ping_realtime, "t", watchdog_get_last_ping(CLOCK_REALTIME)); |
71 | static BUS_DEFINE_PROPERTY_GET_GLOBAL(property_get_watchdog_last_ping_monotonic, "t", watchdog_get_last_ping(CLOCK_MONOTONIC)); | |
cc156539 | 72 | static BUS_DEFINE_PROPERTY_GET(property_get_progress, "d", Manager, manager_get_progress); |
718db961 LP |
73 | |
74 | static int property_get_virtualization( | |
75 | sd_bus *bus, | |
76 | const char *path, | |
77 | const char *interface, | |
78 | const char *property, | |
79 | sd_bus_message *reply, | |
ebcf1f97 LP |
80 | void *userdata, |
81 | sd_bus_error *error) { | |
718db961 | 82 | |
1b86c7c5 | 83 | Virtualization v; |
5c22925a | 84 | |
718db961 LP |
85 | assert(bus); |
86 | assert(reply); | |
87 | ||
5c22925a LP |
88 | v = detect_virtualization(); |
89 | ||
90 | /* Make sure to return the empty string when we detect no virtualization, as that is the API. | |
91 | * | |
92 | * https://github.com/systemd/systemd/issues/1423 | |
93 | */ | |
94 | ||
95 | return sd_bus_message_append( | |
96 | reply, "s", | |
79a60375 | 97 | v == VIRTUALIZATION_NONE ? NULL : virtualization_to_string(v)); |
718db961 LP |
98 | } |
99 | ||
1257274a DB |
100 | static int property_get_confidential_virtualization( |
101 | sd_bus *bus, | |
102 | const char *path, | |
103 | const char *interface, | |
104 | const char *property, | |
105 | sd_bus_message *reply, | |
106 | void *userdata, | |
107 | sd_bus_error *error) { | |
108 | ||
109 | ConfidentialVirtualization v; | |
110 | ||
111 | assert(bus); | |
112 | assert(reply); | |
113 | ||
114 | v = detect_confidential_virtualization(); | |
115 | ||
116 | return sd_bus_message_append( | |
117 | reply, "s", | |
118 | v <= 0 ? NULL : confidential_virtualization_to_string(v)); | |
119 | } | |
120 | ||
718db961 LP |
121 | static int property_get_tainted( |
122 | sd_bus *bus, | |
123 | const char *path, | |
124 | const char *interface, | |
125 | const char *property, | |
126 | sd_bus_message *reply, | |
ebcf1f97 LP |
127 | void *userdata, |
128 | sd_bus_error *error) { | |
718db961 | 129 | |
718db961 LP |
130 | assert(bus); |
131 | assert(reply); | |
c5d34390 | 132 | |
d851637c | 133 | _cleanup_free_ char *s = taint_string(); |
af6b0ecc LP |
134 | if (!s) |
135 | return log_oom(); | |
c5d34390 | 136 | |
af6b0ecc | 137 | return sd_bus_message_append(reply, "s", s); |
c5d34390 LP |
138 | } |
139 | ||
718db961 LP |
140 | static int property_set_log_target( |
141 | sd_bus *bus, | |
142 | const char *path, | |
143 | const char *interface, | |
144 | const char *property, | |
145 | sd_bus_message *value, | |
ebcf1f97 LP |
146 | void *userdata, |
147 | sd_bus_error *error) { | |
718db961 | 148 | |
bda7d78b | 149 | Manager *m = userdata; |
c826cda4 | 150 | const char *t; |
718db961 | 151 | int r; |
c826cda4 | 152 | |
718db961 LP |
153 | assert(bus); |
154 | assert(value); | |
c826cda4 | 155 | |
718db961 LP |
156 | r = sd_bus_message_read(value, "s", &t); |
157 | if (r < 0) | |
158 | return r; | |
c826cda4 | 159 | |
bda7d78b FB |
160 | if (isempty(t)) |
161 | manager_restore_original_log_target(m); | |
162 | else { | |
163 | LogTarget target; | |
164 | ||
165 | target = log_target_from_string(t); | |
166 | if (target < 0) | |
167 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid log target '%s'", t); | |
168 | ||
169 | manager_override_log_target(m, target); | |
170 | } | |
171 | ||
172 | return 0; | |
c826cda4 AB |
173 | } |
174 | ||
718db961 LP |
175 | static int property_set_log_level( |
176 | sd_bus *bus, | |
177 | const char *path, | |
178 | const char *interface, | |
179 | const char *property, | |
180 | sd_bus_message *value, | |
ebcf1f97 LP |
181 | void *userdata, |
182 | sd_bus_error *error) { | |
718db961 | 183 | |
a6ecbf83 | 184 | Manager *m = userdata; |
c826cda4 | 185 | const char *t; |
718db961 | 186 | int r; |
c826cda4 | 187 | |
718db961 LP |
188 | assert(bus); |
189 | assert(value); | |
c826cda4 | 190 | |
718db961 LP |
191 | r = sd_bus_message_read(value, "s", &t); |
192 | if (r < 0) | |
193 | return r; | |
c826cda4 | 194 | |
a6ecbf83 FB |
195 | if (isempty(t)) |
196 | manager_restore_original_log_level(m); | |
197 | else { | |
198 | int level; | |
199 | ||
200 | level = log_level_from_string(t); | |
201 | if (level < 0) | |
202 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid log level '%s'", t); | |
203 | ||
204 | manager_override_log_level(m, level); | |
205 | } | |
206 | ||
207 | return 0; | |
c826cda4 AB |
208 | } |
209 | ||
1ad6e8b3 LP |
210 | static int property_get_environment( |
211 | sd_bus *bus, | |
212 | const char *path, | |
213 | const char *interface, | |
214 | const char *property, | |
215 | sd_bus_message *reply, | |
216 | void *userdata, | |
217 | sd_bus_error *error) { | |
218 | ||
219 | _cleanup_strv_free_ char **l = NULL; | |
99534007 | 220 | Manager *m = ASSERT_PTR(userdata); |
1ad6e8b3 LP |
221 | int r; |
222 | ||
223 | assert(bus); | |
224 | assert(reply); | |
1ad6e8b3 LP |
225 | |
226 | r = manager_get_effective_environment(m, &l); | |
227 | if (r < 0) | |
228 | return r; | |
229 | ||
230 | return sd_bus_message_append_strv(reply, l); | |
231 | } | |
232 | ||
52d2566a ZJS |
233 | static int property_get_show_status( |
234 | sd_bus *bus, | |
235 | const char *path, | |
236 | const char *interface, | |
237 | const char *property, | |
238 | sd_bus_message *reply, | |
239 | void *userdata, | |
240 | sd_bus_error *error) { | |
241 | ||
99534007 | 242 | Manager *m = ASSERT_PTR(userdata); |
52d2566a ZJS |
243 | |
244 | assert(bus); | |
245 | assert(reply); | |
52d2566a | 246 | |
44a41954 | 247 | return sd_bus_message_append(reply, "b", manager_get_show_status_on(m)); |
52d2566a ZJS |
248 | } |
249 | ||
986935cf | 250 | static int property_get_runtime_watchdog( |
718db961 LP |
251 | sd_bus *bus, |
252 | const char *path, | |
253 | const char *interface, | |
254 | const char *property, | |
986935cf | 255 | sd_bus_message *reply, |
ebcf1f97 LP |
256 | void *userdata, |
257 | sd_bus_error *error) { | |
05d6a3b6 | 258 | |
99534007 | 259 | Manager *m = ASSERT_PTR(userdata); |
718db961 LP |
260 | |
261 | assert(bus); | |
986935cf FB |
262 | assert(reply); |
263 | ||
264 | return sd_bus_message_append(reply, "t", manager_get_watchdog(m, WATCHDOG_RUNTIME)); | |
265 | } | |
266 | ||
5717062e CK |
267 | static int property_get_pretimeout_watchdog( |
268 | sd_bus *bus, | |
269 | const char *path, | |
270 | const char *interface, | |
271 | const char *property, | |
272 | sd_bus_message *reply, | |
273 | void *userdata, | |
274 | sd_bus_error *error) { | |
275 | ||
99534007 | 276 | Manager *m = ASSERT_PTR(userdata); |
5717062e | 277 | |
5717062e CK |
278 | assert(bus); |
279 | assert(reply); | |
280 | ||
281 | return sd_bus_message_append(reply, "t", manager_get_watchdog(m, WATCHDOG_PRETIMEOUT)); | |
282 | } | |
283 | ||
aff3a9e1 LB |
284 | static int property_get_pretimeout_watchdog_governor( |
285 | sd_bus *bus, | |
286 | const char *path, | |
287 | const char *interface, | |
288 | const char *property, | |
289 | sd_bus_message *reply, | |
290 | void *userdata, | |
291 | sd_bus_error *error) { | |
292 | ||
99534007 | 293 | Manager *m = ASSERT_PTR(userdata); |
aff3a9e1 | 294 | |
aff3a9e1 LB |
295 | assert(bus); |
296 | assert(reply); | |
297 | ||
298 | return sd_bus_message_append(reply, "s", m->watchdog_pretimeout_governor); | |
299 | } | |
300 | ||
986935cf FB |
301 | static int property_get_reboot_watchdog( |
302 | sd_bus *bus, | |
303 | const char *path, | |
304 | const char *interface, | |
305 | const char *property, | |
306 | sd_bus_message *reply, | |
307 | void *userdata, | |
308 | sd_bus_error *error) { | |
309 | ||
99534007 | 310 | Manager *m = ASSERT_PTR(userdata); |
986935cf | 311 | |
986935cf FB |
312 | assert(bus); |
313 | assert(reply); | |
314 | ||
315 | return sd_bus_message_append(reply, "t", manager_get_watchdog(m, WATCHDOG_REBOOT)); | |
316 | } | |
317 | ||
318 | static int property_get_kexec_watchdog( | |
319 | sd_bus *bus, | |
320 | const char *path, | |
321 | const char *interface, | |
322 | const char *property, | |
323 | sd_bus_message *reply, | |
324 | void *userdata, | |
325 | sd_bus_error *error) { | |
326 | ||
99534007 | 327 | Manager *m = ASSERT_PTR(userdata); |
986935cf | 328 | |
986935cf FB |
329 | assert(bus); |
330 | assert(reply); | |
331 | ||
332 | return sd_bus_message_append(reply, "t", manager_get_watchdog(m, WATCHDOG_KEXEC)); | |
333 | } | |
334 | ||
335 | static int property_set_watchdog(Manager *m, WatchdogType type, sd_bus_message *value) { | |
336 | usec_t timeout; | |
337 | int r; | |
338 | ||
339 | assert(m); | |
718db961 LP |
340 | assert(value); |
341 | ||
342 | assert_cc(sizeof(usec_t) == sizeof(uint64_t)); | |
343 | ||
986935cf | 344 | r = sd_bus_message_read(value, "t", &timeout); |
718db961 LP |
345 | if (r < 0) |
346 | return r; | |
347 | ||
aac47032 YW |
348 | manager_override_watchdog(m, type, timeout); |
349 | return 0; | |
986935cf FB |
350 | } |
351 | ||
352 | static int property_set_runtime_watchdog( | |
353 | sd_bus *bus, | |
354 | const char *path, | |
355 | const char *interface, | |
356 | const char *property, | |
357 | sd_bus_message *value, | |
358 | void *userdata, | |
359 | sd_bus_error *error) { | |
360 | ||
361 | return property_set_watchdog(userdata, WATCHDOG_RUNTIME, value); | |
362 | } | |
363 | ||
5717062e CK |
364 | static int property_set_pretimeout_watchdog( |
365 | sd_bus *bus, | |
366 | const char *path, | |
367 | const char *interface, | |
368 | const char *property, | |
369 | sd_bus_message *value, | |
370 | void *userdata, | |
371 | sd_bus_error *error) { | |
372 | ||
373 | return property_set_watchdog(userdata, WATCHDOG_PRETIMEOUT, value); | |
374 | } | |
375 | ||
aff3a9e1 LB |
376 | static int property_set_pretimeout_watchdog_governor( |
377 | sd_bus *bus, | |
378 | const char *path, | |
379 | const char *interface, | |
380 | const char *property, | |
381 | sd_bus_message *value, | |
382 | void *userdata, | |
383 | sd_bus_error *error) { | |
384 | ||
99534007 | 385 | Manager *m = ASSERT_PTR(userdata); |
aff3a9e1 LB |
386 | char *governor; |
387 | int r; | |
388 | ||
aff3a9e1 LB |
389 | r = sd_bus_message_read(value, "s", &governor); |
390 | if (r < 0) | |
391 | return r; | |
392 | if (!string_is_safe(governor)) | |
393 | return -EINVAL; | |
394 | ||
395 | return manager_override_watchdog_pretimeout_governor(m, governor); | |
396 | } | |
397 | ||
986935cf FB |
398 | static int property_set_reboot_watchdog( |
399 | sd_bus *bus, | |
400 | const char *path, | |
401 | const char *interface, | |
402 | const char *property, | |
403 | sd_bus_message *value, | |
404 | void *userdata, | |
405 | sd_bus_error *error) { | |
406 | ||
407 | return property_set_watchdog(userdata, WATCHDOG_REBOOT, value); | |
408 | } | |
409 | ||
410 | static int property_set_kexec_watchdog( | |
411 | sd_bus *bus, | |
412 | const char *path, | |
413 | const char *interface, | |
414 | const char *property, | |
415 | sd_bus_message *value, | |
416 | void *userdata, | |
417 | sd_bus_error *error) { | |
418 | ||
99534007 | 419 | _unused_ Manager *m = ASSERT_PTR(userdata); |
986935cf | 420 | |
986935cf FB |
421 | assert(bus); |
422 | assert(value); | |
423 | ||
424 | return property_set_watchdog(userdata, WATCHDOG_KEXEC, value); | |
05d6a3b6 LP |
425 | } |
426 | ||
d4a402e4 LP |
427 | static int property_get_oom_score_adjust( |
428 | sd_bus *bus, | |
429 | const char *path, | |
430 | const char *interface, | |
431 | const char *property, | |
432 | sd_bus_message *reply, | |
433 | void *userdata, | |
434 | sd_bus_error *error) { | |
435 | ||
99534007 | 436 | Manager *m = ASSERT_PTR(userdata); |
d4a402e4 LP |
437 | int r, n; |
438 | ||
d4a402e4 LP |
439 | assert(bus); |
440 | assert(reply); | |
441 | ||
c9e120e0 LP |
442 | if (m->defaults.oom_score_adjust_set) |
443 | n = m->defaults.oom_score_adjust; | |
d4a402e4 LP |
444 | else { |
445 | n = 0; | |
446 | r = get_oom_score_adjust(&n); | |
447 | if (r < 0) | |
448 | log_debug_errno(r, "Failed to read current OOM score adjustment value, ignoring: %m"); | |
449 | } | |
450 | ||
451 | return sd_bus_message_append(reply, "i", n); | |
452 | } | |
453 | ||
96cc4453 | 454 | static int bus_get_unit_by_name(Manager *m, sd_bus_message *message, const char *name, Unit **ret_unit, sd_bus_error *error) { |
718db961 LP |
455 | Unit *u; |
456 | int r; | |
dfae3488 | 457 | |
dfae3488 | 458 | assert(m); |
96cc4453 LP |
459 | assert(message); |
460 | assert(ret_unit); | |
dfae3488 | 461 | |
cb31470f ZJS |
462 | /* More or less a wrapper around manager_get_unit() that generates nice errors and has one trick up |
463 | * its sleeve: if the name is specified empty we use the client's unit. */ | |
dfae3488 | 464 | |
1d22e906 | 465 | if (isempty(name)) { |
4ac08d8a | 466 | _cleanup_(pidref_done) PidRef pidref = PIDREF_NULL; |
1d22e906 | 467 | |
4ac08d8a | 468 | r = bus_query_sender_pidref(message, &pidref); |
1d22e906 LP |
469 | if (r < 0) |
470 | return r; | |
471 | ||
4ac08d8a | 472 | u = manager_get_unit_by_pidref(m, &pidref); |
1d22e906 | 473 | if (!u) |
1b09b81c | 474 | return sd_bus_error_set(error, BUS_ERROR_NO_SUCH_UNIT, "Client not member of any unit."); |
1d22e906 LP |
475 | } else { |
476 | u = manager_get_unit(m, name); | |
477 | if (!u) | |
478 | return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_UNIT, "Unit %s not loaded.", name); | |
479 | } | |
dfae3488 | 480 | |
96cc4453 LP |
481 | *ret_unit = u; |
482 | return 0; | |
483 | } | |
484 | ||
485 | static int bus_load_unit_by_name(Manager *m, sd_bus_message *message, const char *name, Unit **ret_unit, sd_bus_error *error) { | |
486 | assert(m); | |
487 | assert(message); | |
488 | assert(ret_unit); | |
489 | ||
490 | /* Pretty much the same as bus_get_unit_by_name(), but we also load the unit if necessary. */ | |
491 | ||
492 | if (isempty(name)) | |
493 | return bus_get_unit_by_name(m, message, name, ret_unit, error); | |
494 | ||
495 | return manager_load_unit(m, name, NULL, error, ret_unit); | |
496 | } | |
497 | ||
4659bf6f | 498 | static int reply_unit_path(Unit *u, sd_bus_message *message, sd_bus_error *error) { |
96cc4453 | 499 | _cleanup_free_ char *path = NULL; |
4659bf6f LP |
500 | int r; |
501 | ||
502 | assert(u); | |
503 | assert(message); | |
504 | ||
505 | r = mac_selinux_unit_access_check(u, message, "status", error); | |
506 | if (r < 0) | |
507 | return r; | |
508 | ||
509 | path = unit_dbus_path(u); | |
510 | if (!path) | |
511 | return log_oom(); | |
512 | ||
513 | return sd_bus_reply_method_return(message, "o", path); | |
514 | } | |
515 | ||
516 | static int method_get_unit(sd_bus_message *message, void *userdata, sd_bus_error *error) { | |
99534007 | 517 | Manager *m = ASSERT_PTR(userdata); |
96cc4453 LP |
518 | const char *name; |
519 | Unit *u; | |
520 | int r; | |
521 | ||
522 | assert(message); | |
96cc4453 LP |
523 | |
524 | /* Anyone can call this method */ | |
525 | ||
526 | r = sd_bus_message_read(message, "s", &name); | |
527 | if (r < 0) | |
528 | return r; | |
529 | ||
530 | r = bus_get_unit_by_name(m, message, name, &u, error); | |
531 | if (r < 0) | |
532 | return r; | |
533 | ||
4659bf6f | 534 | return reply_unit_path(u, message, error); |
718db961 | 535 | } |
c0576cd6 | 536 | |
19070062 | 537 | static int method_get_unit_by_pid(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
99534007 | 538 | Manager *m = ASSERT_PTR(userdata); |
4ac08d8a | 539 | _cleanup_(pidref_done) PidRef pidref = PIDREF_NULL; |
718db961 LP |
540 | Unit *u; |
541 | int r; | |
c0576cd6 | 542 | |
718db961 | 543 | assert(message); |
c0576cd6 | 544 | |
718db961 | 545 | assert_cc(sizeof(pid_t) == sizeof(uint32_t)); |
729e3769 | 546 | |
283868e1 SW |
547 | /* Anyone can call this method */ |
548 | ||
4ac08d8a | 549 | r = sd_bus_message_read(message, "u", &pidref.pid); |
718db961 | 550 | if (r < 0) |
ebcf1f97 | 551 | return r; |
4ac08d8a LP |
552 | if (pidref.pid < 0) |
553 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid PID " PID_FMT, pidref.pid); | |
554 | if (pidref.pid == 0) { | |
555 | r = bus_query_sender_pidref(message, &pidref); | |
718db961 | 556 | if (r < 0) |
ebcf1f97 | 557 | return r; |
729e3769 LP |
558 | } |
559 | ||
4ac08d8a | 560 | u = manager_get_unit_by_pidref(m, &pidref); |
718db961 | 561 | if (!u) |
4ac08d8a | 562 | return sd_bus_error_setf(error, BUS_ERROR_NO_UNIT_FOR_PID, "PID "PID_FMT" does not belong to any loaded unit.", pidref.pid); |
c0576cd6 | 563 | |
4659bf6f | 564 | return reply_unit_path(u, message, error); |
718db961 LP |
565 | } |
566 | ||
4b58153d LP |
567 | static int method_get_unit_by_invocation_id(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
568 | _cleanup_free_ char *path = NULL; | |
99534007 | 569 | Manager *m = ASSERT_PTR(userdata); |
4b58153d | 570 | sd_id128_t id; |
4b58153d | 571 | Unit *u; |
4b58153d LP |
572 | int r; |
573 | ||
574 | assert(message); | |
4b58153d LP |
575 | |
576 | /* Anyone can call this method */ | |
577 | ||
8157cc0e | 578 | if (bus_message_read_id128(message, &id) < 0) |
1b09b81c | 579 | return sd_bus_error_set(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid invocation ID"); |
4b58153d LP |
580 | |
581 | if (sd_id128_is_null(id)) { | |
4ac08d8a | 582 | _cleanup_(pidref_done) PidRef pidref = PIDREF_NULL; |
4b58153d | 583 | |
4ac08d8a | 584 | r = bus_query_sender_pidref(message, &pidref); |
4b58153d LP |
585 | if (r < 0) |
586 | return r; | |
587 | ||
4ac08d8a | 588 | u = manager_get_unit_by_pidref(m, &pidref); |
4b58153d | 589 | if (!u) |
cb31470f | 590 | return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_UNIT, |
4ac08d8a | 591 | "Client " PID_FMT " not member of any unit.", pidref.pid); |
4b58153d LP |
592 | } else { |
593 | u = hashmap_get(m->units_by_invocation_id, &id); | |
594 | if (!u) | |
595 | return sd_bus_error_setf(error, BUS_ERROR_NO_UNIT_FOR_INVOCATION_ID, "No unit with the specified invocation ID " SD_ID128_FORMAT_STR " known.", SD_ID128_FORMAT_VAL(id)); | |
596 | } | |
597 | ||
598 | r = mac_selinux_unit_access_check(u, message, "status", error); | |
599 | if (r < 0) | |
600 | return r; | |
601 | ||
cb31470f ZJS |
602 | /* So here's a special trick: the bus path we return actually references the unit by its invocation |
603 | * ID instead of the unit name. This means it stays valid only as long as the invocation ID stays the | |
604 | * same. */ | |
4b58153d LP |
605 | path = unit_dbus_path_invocation_id(u); |
606 | if (!path) | |
607 | return -ENOMEM; | |
608 | ||
609 | return sd_bus_reply_method_return(message, "o", path); | |
610 | } | |
611 | ||
267dd427 | 612 | static int method_get_unit_by_control_group(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
267dd427 LP |
613 | Manager *m = userdata; |
614 | const char *cgroup; | |
615 | Unit *u; | |
616 | int r; | |
617 | ||
618 | r = sd_bus_message_read(message, "s", &cgroup); | |
619 | if (r < 0) | |
620 | return r; | |
621 | ||
622 | u = manager_get_unit_by_cgroup(m, cgroup); | |
623 | if (!u) | |
cb31470f ZJS |
624 | return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_UNIT, |
625 | "Control group '%s' is not valid or not managed by this instance", | |
626 | cgroup); | |
267dd427 | 627 | |
4659bf6f | 628 | return reply_unit_path(u, message, error); |
267dd427 LP |
629 | } |
630 | ||
e0e7bc82 LB |
631 | static int method_get_unit_by_pidfd(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
632 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; | |
495e75ed | 633 | _cleanup_(pidref_done) PidRef pidref = PIDREF_NULL; |
e0e7bc82 LB |
634 | Manager *m = ASSERT_PTR(userdata); |
635 | _cleanup_free_ char *path = NULL; | |
636 | int r, pidfd; | |
e0e7bc82 LB |
637 | Unit *u; |
638 | ||
639 | assert(message); | |
640 | ||
641 | r = sd_bus_message_read(message, "h", &pidfd); | |
642 | if (r < 0) | |
643 | return r; | |
644 | ||
495e75ed | 645 | r = pidref_set_pidfd(&pidref, pidfd); |
e0e7bc82 LB |
646 | if (r < 0) |
647 | return sd_bus_error_set_errnof(error, r, "Failed to get PID from PIDFD: %m"); | |
648 | ||
495e75ed | 649 | u = manager_get_unit_by_pidref(m, &pidref); |
e0e7bc82 | 650 | if (!u) |
495e75ed | 651 | return sd_bus_error_setf(error, BUS_ERROR_NO_UNIT_FOR_PID, "PID "PID_FMT" does not belong to any loaded unit.", pidref.pid); |
e0e7bc82 LB |
652 | |
653 | r = mac_selinux_unit_access_check(u, message, "status", error); | |
654 | if (r < 0) | |
655 | return r; | |
656 | ||
657 | path = unit_dbus_path(u); | |
658 | if (!path) | |
659 | return log_oom(); | |
660 | ||
661 | r = sd_bus_message_new_method_return(message, &reply); | |
662 | if (r < 0) | |
663 | return r; | |
664 | ||
665 | r = sd_bus_message_append(reply, "os", path, u->id); | |
666 | if (r < 0) | |
667 | return r; | |
668 | ||
669 | r = sd_bus_message_append_array(reply, 'y', u->invocation_id.bytes, sizeof(u->invocation_id.bytes)); | |
670 | if (r < 0) | |
671 | return r; | |
672 | ||
673 | /* Double-check that the process is still alive and that the PID did not change before returning the | |
674 | * answer. */ | |
495e75ed | 675 | r = pidref_verify(&pidref); |
f840c7d5 | 676 | if (r == -ESRCH) |
e0e7bc82 LB |
677 | return sd_bus_error_setf(error, |
678 | BUS_ERROR_NO_SUCH_PROCESS, | |
f840c7d5 | 679 | "The PIDFD's PID "PID_FMT" changed during the lookup operation.", |
495e75ed | 680 | pidref.pid); |
f840c7d5 LB |
681 | if (r < 0) |
682 | return sd_bus_error_set_errnof(error, r, "Failed to get PID from PIDFD: %m"); | |
e0e7bc82 LB |
683 | |
684 | return sd_bus_send(NULL, reply, NULL); | |
685 | } | |
686 | ||
19070062 | 687 | static int method_load_unit(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
99534007 | 688 | Manager *m = ASSERT_PTR(userdata); |
718db961 LP |
689 | const char *name; |
690 | Unit *u; | |
691 | int r; | |
c0576cd6 | 692 | |
718db961 | 693 | assert(message); |
718db961 | 694 | |
283868e1 SW |
695 | /* Anyone can call this method */ |
696 | ||
718db961 LP |
697 | r = sd_bus_message_read(message, "s", &name); |
698 | if (r < 0) | |
ebcf1f97 | 699 | return r; |
c0576cd6 | 700 | |
96cc4453 LP |
701 | r = bus_load_unit_by_name(m, message, name, &u, error); |
702 | if (r < 0) | |
703 | return r; | |
718db961 | 704 | |
4659bf6f | 705 | return reply_unit_path(u, message, error); |
c0576cd6 LP |
706 | } |
707 | ||
19070062 | 708 | static int method_start_unit_generic(sd_bus_message *message, Manager *m, JobType job_type, bool reload_if_possible, sd_bus_error *error) { |
718db961 LP |
709 | const char *name; |
710 | Unit *u; | |
c0576cd6 LP |
711 | int r; |
712 | ||
718db961 LP |
713 | assert(message); |
714 | assert(m); | |
c0576cd6 | 715 | |
718db961 LP |
716 | r = sd_bus_message_read(message, "s", &name); |
717 | if (r < 0) | |
ebcf1f97 | 718 | return r; |
c0576cd6 | 719 | |
ebcf1f97 | 720 | r = manager_load_unit(m, name, NULL, error, &u); |
718db961 | 721 | if (r < 0) |
ebcf1f97 | 722 | return r; |
718db961 | 723 | |
19070062 | 724 | return bus_unit_method_start_generic(message, u, job_type, reload_if_possible, error); |
c0576cd6 LP |
725 | } |
726 | ||
19070062 | 727 | static int method_start_unit(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
2d8cc7a7 | 728 | return method_start_unit_generic(message, userdata, JOB_START, /* reload_if_possible = */ false, error); |
718db961 | 729 | } |
c757a65b | 730 | |
19070062 | 731 | static int method_stop_unit(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
2d8cc7a7 | 732 | return method_start_unit_generic(message, userdata, JOB_STOP, /* reload_if_possible = */ false, error); |
718db961 | 733 | } |
c757a65b | 734 | |
19070062 | 735 | static int method_reload_unit(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
2d8cc7a7 | 736 | return method_start_unit_generic(message, userdata, JOB_RELOAD, /* reload_if_possible = */ false, error); |
718db961 | 737 | } |
c757a65b | 738 | |
19070062 | 739 | static int method_restart_unit(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
2d8cc7a7 | 740 | return method_start_unit_generic(message, userdata, JOB_RESTART, /* reload_if_possible = */ false, error); |
c757a65b LP |
741 | } |
742 | ||
19070062 | 743 | static int method_try_restart_unit(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
2d8cc7a7 | 744 | return method_start_unit_generic(message, userdata, JOB_TRY_RESTART, /* reload_if_possible = */ false, error); |
718db961 | 745 | } |
d200735e | 746 | |
19070062 | 747 | static int method_reload_or_restart_unit(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
2d8cc7a7 | 748 | return method_start_unit_generic(message, userdata, JOB_RESTART, /* reload_if_possible = */ true, error); |
718db961 | 749 | } |
d200735e | 750 | |
19070062 | 751 | static int method_reload_or_try_restart_unit(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
2d8cc7a7 | 752 | return method_start_unit_generic(message, userdata, JOB_TRY_RESTART, /* reload_if_possible = */ true, error); |
718db961 | 753 | } |
d200735e | 754 | |
42561fc9 LP |
755 | typedef enum GenericUnitOperationFlags { |
756 | GENERIC_UNIT_LOAD = 1 << 0, /* Load if the unit is not loaded yet */ | |
757 | GENERIC_UNIT_VALIDATE_LOADED = 1 << 1, /* Verify unit is properly loaded before forwarding call */ | |
758 | } GenericUnitOperationFlags; | |
759 | ||
760 | static int method_generic_unit_operation( | |
761 | sd_bus_message *message, | |
762 | Manager *m, | |
763 | sd_bus_error *error, | |
764 | sd_bus_message_handler_t handler, | |
765 | GenericUnitOperationFlags flags) { | |
766 | ||
50cbaba4 LP |
767 | const char *name; |
768 | Unit *u; | |
769 | int r; | |
770 | ||
771 | assert(message); | |
772 | assert(m); | |
8a70edc1 | 773 | assert(handler); |
50cbaba4 | 774 | |
42561fc9 LP |
775 | /* Read the first argument from the command and pass the operation to the specified per-unit |
776 | * method. */ | |
777 | ||
50cbaba4 LP |
778 | r = sd_bus_message_read(message, "s", &name); |
779 | if (r < 0) | |
780 | return r; | |
781 | ||
42561fc9 LP |
782 | if (!isempty(name) && FLAGS_SET(flags, GENERIC_UNIT_LOAD)) |
783 | r = manager_load_unit(m, name, NULL, error, &u); | |
784 | else | |
785 | r = bus_get_unit_by_name(m, message, name, &u, error); | |
50cbaba4 LP |
786 | if (r < 0) |
787 | return r; | |
788 | ||
42561fc9 LP |
789 | if (FLAGS_SET(flags, GENERIC_UNIT_VALIDATE_LOADED)) { |
790 | r = bus_unit_validate_load_state(u, error); | |
791 | if (r < 0) | |
792 | return r; | |
793 | } | |
794 | ||
795 | return handler(message, u, error); | |
796 | } | |
797 | ||
798 | static int method_enqueue_unit_job(sd_bus_message *message, void *userdata, sd_bus_error *error) { | |
799 | /* We don't bother with GENERIC_UNIT_VALIDATE_LOADED here, as the job logic validates that anyway */ | |
800 | return method_generic_unit_operation(message, userdata, error, bus_unit_method_enqueue_job, GENERIC_UNIT_LOAD); | |
50cbaba4 LP |
801 | } |
802 | ||
19070062 | 803 | static int method_start_unit_replace(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
99534007 | 804 | Manager *m = ASSERT_PTR(userdata); |
718db961 LP |
805 | const char *old_name; |
806 | Unit *u; | |
1adf1049 | 807 | int r; |
ea430986 | 808 | |
ea430986 | 809 | assert(message); |
ea430986 | 810 | |
718db961 LP |
811 | r = sd_bus_message_read(message, "s", &old_name); |
812 | if (r < 0) | |
ebcf1f97 | 813 | return r; |
ea430986 | 814 | |
96cc4453 LP |
815 | r = bus_get_unit_by_name(m, message, old_name, &u, error); |
816 | if (r < 0) | |
817 | return r; | |
818 | if (!u->job || u->job->type != JOB_START) | |
ebcf1f97 | 819 | return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_JOB, "No job queued for unit %s", old_name); |
c0576cd6 | 820 | |
2d8cc7a7 | 821 | return method_start_unit_generic(message, m, JOB_START, /* reload_if_possible = */ false, error); |
718db961 | 822 | } |
ea430986 | 823 | |
19070062 | 824 | static int method_kill_unit(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
42561fc9 LP |
825 | /* We don't bother with GENERIC_UNIT_LOAD nor GENERIC_UNIT_VALIDATE_LOADED here, as it shouldn't |
826 | * matter whether a unit is loaded for killing any processes possibly in the unit's cgroup. */ | |
827 | return method_generic_unit_operation(message, userdata, error, bus_unit_method_kill, 0); | |
718db961 | 828 | } |
8a0867d6 | 829 | |
4d3bac56 LP |
830 | static int method_clean_unit(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
831 | /* Load the unit if necessary, in order to load it, and insist on the unit being loaded to be | |
832 | * cleaned */ | |
833 | return method_generic_unit_operation(message, userdata, error, bus_unit_method_clean, GENERIC_UNIT_LOAD|GENERIC_UNIT_VALIDATE_LOADED); | |
834 | } | |
835 | ||
d9e45bc3 | 836 | static int method_freeze_unit(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
47266ea2 MY |
837 | /* Only active units can be frozen, which must be properly loaded already */ |
838 | return method_generic_unit_operation(message, userdata, error, bus_unit_method_freeze, GENERIC_UNIT_VALIDATE_LOADED); | |
d9e45bc3 MS |
839 | } |
840 | ||
841 | static int method_thaw_unit(sd_bus_message *message, void *userdata, sd_bus_error *error) { | |
47266ea2 MY |
842 | /* Same as freeze above */ |
843 | return method_generic_unit_operation(message, userdata, error, bus_unit_method_thaw, GENERIC_UNIT_VALIDATE_LOADED); | |
d9e45bc3 MS |
844 | } |
845 | ||
19070062 | 846 | static int method_reset_failed_unit(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
42561fc9 LP |
847 | /* Don't load the unit (because unloaded units can't be in failed state), and don't insist on the |
848 | * unit to be loaded properly (since a failed unit might have its unit file disappeared) */ | |
849 | return method_generic_unit_operation(message, userdata, error, bus_unit_method_reset_failed, 0); | |
718db961 | 850 | } |
5dd9014f | 851 | |
19070062 | 852 | static int method_set_unit_properties(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
42561fc9 LP |
853 | /* Only change properties on fully loaded units, and load them in order to set properties */ |
854 | return method_generic_unit_operation(message, userdata, error, bus_unit_method_set_properties, GENERIC_UNIT_LOAD|GENERIC_UNIT_VALIDATE_LOADED); | |
718db961 LP |
855 | } |
856 | ||
5e8deb94 LB |
857 | static int method_bind_mount_unit(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
858 | /* Only add mounts on fully loaded units */ | |
859 | return method_generic_unit_operation(message, userdata, error, bus_service_method_bind_mount, GENERIC_UNIT_VALIDATE_LOADED); | |
860 | } | |
861 | ||
af477139 LB |
862 | static int method_mount_image_unit(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
863 | /* Only add mounts on fully loaded units */ | |
864 | return method_generic_unit_operation(message, userdata, error, bus_service_method_mount_image, GENERIC_UNIT_VALIDATE_LOADED); | |
865 | } | |
866 | ||
05a98afd | 867 | static int method_ref_unit(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
42561fc9 LP |
868 | /* Only allow reffing of fully loaded units, and make sure reffing a unit loads it. */ |
869 | return method_generic_unit_operation(message, userdata, error, bus_unit_method_ref, GENERIC_UNIT_LOAD|GENERIC_UNIT_VALIDATE_LOADED); | |
05a98afd LP |
870 | } |
871 | ||
872 | static int method_unref_unit(sd_bus_message *message, void *userdata, sd_bus_error *error) { | |
42561fc9 LP |
873 | /* Dropping a ref OTOH should not require the unit to still be loaded. And since a reffed unit is a |
874 | * loaded unit there's no need to load the unit for unreffing it. */ | |
875 | return method_generic_unit_operation(message, userdata, error, bus_unit_method_unref, 0); | |
05a98afd LP |
876 | } |
877 | ||
6900c740 | 878 | static int reply_unit_info(sd_bus_message *reply, Unit *u) { |
879 | _cleanup_free_ char *unit_path = NULL, *job_path = NULL; | |
880 | Unit *following; | |
881 | ||
882 | following = unit_following(u); | |
883 | ||
884 | unit_path = unit_dbus_path(u); | |
885 | if (!unit_path) | |
886 | return -ENOMEM; | |
887 | ||
888 | if (u->job) { | |
889 | job_path = job_dbus_path(u->job); | |
890 | if (!job_path) | |
891 | return -ENOMEM; | |
892 | } | |
893 | ||
894 | return sd_bus_message_append( | |
895 | reply, "(ssssssouso)", | |
896 | u->id, | |
897 | unit_description(u), | |
898 | unit_load_state_to_string(u->load_state), | |
899 | unit_active_state_to_string(unit_active_state(u)), | |
900 | unit_sub_state_to_string(u), | |
901 | following ? following->id : "", | |
902 | unit_path, | |
903 | u->job ? u->job->id : 0, | |
904 | u->job ? job_type_to_string(u->job->type) : "", | |
c1d95b71 | 905 | empty_to_root(job_path)); |
6900c740 | 906 | } |
907 | ||
908 | static int method_list_units_by_names(sd_bus_message *message, void *userdata, sd_bus_error *error) { | |
909 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; | |
99534007 | 910 | Manager *m = ASSERT_PTR(userdata); |
6900c740 | 911 | int r; |
6900c740 | 912 | _cleanup_strv_free_ char **units = NULL; |
913 | ||
914 | assert(message); | |
6900c740 | 915 | |
916 | r = sd_bus_message_read_strv(message, &units); | |
917 | if (r < 0) | |
918 | return r; | |
919 | ||
920 | r = sd_bus_message_new_method_return(message, &reply); | |
921 | if (r < 0) | |
922 | return r; | |
923 | ||
924 | r = sd_bus_message_open_container(reply, 'a', "(ssssssouso)"); | |
925 | if (r < 0) | |
926 | return r; | |
927 | ||
928 | STRV_FOREACH(unit, units) { | |
929 | Unit *u; | |
930 | ||
931 | if (!unit_name_is_valid(*unit, UNIT_NAME_ANY)) | |
932 | continue; | |
933 | ||
96cc4453 | 934 | r = bus_load_unit_by_name(m, message, *unit, &u, error); |
6900c740 | 935 | if (r < 0) |
936 | return r; | |
937 | ||
938 | r = reply_unit_info(reply, u); | |
939 | if (r < 0) | |
940 | return r; | |
941 | } | |
942 | ||
943 | r = sd_bus_message_close_container(reply); | |
944 | if (r < 0) | |
945 | return r; | |
946 | ||
947 | return sd_bus_send(NULL, reply, NULL); | |
948 | } | |
949 | ||
291d565a | 950 | static int method_get_unit_processes(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
ad68a4e5 MY |
951 | /* Don't load a unit actively (since it won't have any processes if it's not loaded), but don't |
952 | * insist on the unit being loaded either (because even improperly loaded units might still have | |
953 | * processes around). */ | |
954 | return method_generic_unit_operation(message, userdata, error, bus_unit_method_get_processes, /* flags = */ 0); | |
291d565a LP |
955 | } |
956 | ||
6592b975 | 957 | static int method_attach_processes_to_unit(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
42561fc9 LP |
958 | /* Don't allow attaching new processes to units that aren't loaded. Don't bother with loading a unit |
959 | * for this purpose though, as an unloaded unit is a stopped unit, and we don't allow attaching | |
960 | * processes to stopped units anyway. */ | |
961 | return method_generic_unit_operation(message, userdata, error, bus_unit_method_attach_processes, GENERIC_UNIT_VALIDATE_LOADED); | |
6592b975 LP |
962 | } |
963 | ||
ab31f6b8 WC |
964 | static int transient_unit_from_message( |
965 | Manager *m, | |
966 | sd_bus_message *message, | |
967 | const char *name, | |
968 | Unit **unit, | |
969 | sd_bus_error *error) { | |
970 | ||
06cc6afa | 971 | UnitType t; |
ab31f6b8 WC |
972 | Unit *u; |
973 | int r; | |
974 | ||
975 | assert(m); | |
976 | assert(message); | |
977 | assert(name); | |
978 | ||
06cc6afa LP |
979 | t = unit_name_to_type(name); |
980 | if (t < 0) | |
cb31470f ZJS |
981 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, |
982 | "Invalid unit name or type."); | |
06cc6afa LP |
983 | |
984 | if (!unit_vtable[t]->can_transient) | |
cb31470f ZJS |
985 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, |
986 | "Unit type %s does not support transient units.", | |
987 | unit_type_to_string(t)); | |
06cc6afa | 988 | |
ab31f6b8 WC |
989 | r = manager_load_unit(m, name, NULL, error, &u); |
990 | if (r < 0) | |
991 | return r; | |
992 | ||
0f13f3bd | 993 | if (!unit_is_pristine(u)) |
cb31470f | 994 | return sd_bus_error_setf(error, BUS_ERROR_UNIT_EXISTS, |
1f832446 | 995 | "Unit %s was already loaded or has a fragment file.", name); |
ab31f6b8 WC |
996 | |
997 | /* OK, the unit failed to load and is unreferenced, now let's | |
998 | * fill in the transient data instead */ | |
999 | r = unit_make_transient(u); | |
1000 | if (r < 0) | |
1001 | return r; | |
1002 | ||
1003 | /* Set our properties */ | |
1004 | r = bus_unit_set_properties(u, message, UNIT_RUNTIME, false, error); | |
1005 | if (r < 0) | |
1006 | return r; | |
1007 | ||
05a98afd LP |
1008 | /* If the client asked for it, automatically add a reference to this unit. */ |
1009 | if (u->bus_track_add) { | |
1010 | r = bus_unit_track_add_sender(u, message); | |
1011 | if (r < 0) | |
1012 | return log_error_errno(r, "Failed to watch sender: %m"); | |
1013 | } | |
1014 | ||
97329d20 | 1015 | /* Now load the missing bits of the unit we just created */ |
4f952a3f | 1016 | unit_add_to_load_queue(u); |
97329d20 LP |
1017 | manager_dispatch_load_queue(m); |
1018 | ||
ab31f6b8 WC |
1019 | *unit = u; |
1020 | ||
1021 | return 0; | |
1022 | } | |
1023 | ||
1024 | static int transient_aux_units_from_message( | |
1025 | Manager *m, | |
1026 | sd_bus_message *message, | |
1027 | sd_bus_error *error) { | |
1028 | ||
ab31f6b8 WC |
1029 | int r; |
1030 | ||
1031 | assert(m); | |
1032 | assert(message); | |
1033 | ||
1034 | r = sd_bus_message_enter_container(message, 'a', "(sa(sv))"); | |
1035 | if (r < 0) | |
1036 | return r; | |
1037 | ||
1038 | while ((r = sd_bus_message_enter_container(message, 'r', "sa(sv)")) > 0) { | |
97329d20 LP |
1039 | const char *name = NULL; |
1040 | Unit *u; | |
1041 | ||
ab31f6b8 WC |
1042 | r = sd_bus_message_read(message, "s", &name); |
1043 | if (r < 0) | |
1044 | return r; | |
1045 | ||
1046 | r = transient_unit_from_message(m, message, name, &u, error); | |
97329d20 | 1047 | if (r < 0) |
ab31f6b8 WC |
1048 | return r; |
1049 | ||
ab31f6b8 WC |
1050 | r = sd_bus_message_exit_container(message); |
1051 | if (r < 0) | |
1052 | return r; | |
1053 | } | |
1054 | if (r < 0) | |
1055 | return r; | |
1056 | ||
1057 | r = sd_bus_message_exit_container(message); | |
1058 | if (r < 0) | |
1059 | return r; | |
1060 | ||
1061 | return 0; | |
1062 | } | |
1063 | ||
19070062 | 1064 | static int method_start_transient_unit(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
718db961 | 1065 | const char *name, *smode; |
99534007 | 1066 | Manager *m = ASSERT_PTR(userdata); |
718db961 | 1067 | JobMode mode; |
718db961 LP |
1068 | Unit *u; |
1069 | int r; | |
ea430986 | 1070 | |
718db961 | 1071 | assert(message); |
ea430986 | 1072 | |
1d22e906 | 1073 | r = mac_selinux_access_check(message, "start", error); |
283868e1 SW |
1074 | if (r < 0) |
1075 | return r; | |
283868e1 | 1076 | |
718db961 LP |
1077 | r = sd_bus_message_read(message, "ss", &name, &smode); |
1078 | if (r < 0) | |
ebcf1f97 | 1079 | return r; |
5632e374 | 1080 | |
718db961 LP |
1081 | mode = job_mode_from_string(smode); |
1082 | if (mode < 0) | |
ebcf1f97 | 1083 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Job mode %s is invalid.", smode); |
5632e374 | 1084 | |
1d22e906 | 1085 | r = bus_verify_manage_units_async(m, message, error); |
718db961 | 1086 | if (r < 0) |
ebcf1f97 | 1087 | return r; |
1d22e906 LP |
1088 | if (r == 0) |
1089 | return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */ | |
5632e374 | 1090 | |
ab31f6b8 | 1091 | r = transient_unit_from_message(m, message, name, &u, error); |
ebcf1f97 LP |
1092 | if (r < 0) |
1093 | return r; | |
5632e374 | 1094 | |
ab31f6b8 | 1095 | r = transient_aux_units_from_message(m, message, error); |
718db961 | 1096 | if (r < 0) |
ebcf1f97 | 1097 | return r; |
ea430986 | 1098 | |
718db961 | 1099 | /* Finally, start it */ |
50cbaba4 | 1100 | return bus_unit_queue_job(message, u, JOB_START, mode, 0, error); |
718db961 | 1101 | } |
ea430986 | 1102 | |
19070062 | 1103 | static int method_get_job(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
718db961 | 1104 | _cleanup_free_ char *path = NULL; |
99534007 | 1105 | Manager *m = ASSERT_PTR(userdata); |
718db961 LP |
1106 | uint32_t id; |
1107 | Job *j; | |
1108 | int r; | |
ea430986 | 1109 | |
718db961 | 1110 | assert(message); |
ea430986 | 1111 | |
283868e1 SW |
1112 | /* Anyone can call this method */ |
1113 | ||
718db961 LP |
1114 | r = sd_bus_message_read(message, "u", &id); |
1115 | if (r < 0) | |
ebcf1f97 | 1116 | return r; |
ea430986 | 1117 | |
718db961 LP |
1118 | j = manager_get_job(m, id); |
1119 | if (!j) | |
ebcf1f97 | 1120 | return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_JOB, "Job %u does not exist.", (unsigned) id); |
ea430986 | 1121 | |
8a188de9 | 1122 | r = mac_selinux_unit_access_check(j->unit, message, "status", error); |
ebcf1f97 LP |
1123 | if (r < 0) |
1124 | return r; | |
ea430986 | 1125 | |
718db961 LP |
1126 | path = job_dbus_path(j); |
1127 | if (!path) | |
ebcf1f97 | 1128 | return -ENOMEM; |
ea430986 | 1129 | |
df2d202e | 1130 | return sd_bus_reply_method_return(message, "o", path); |
718db961 | 1131 | } |
ea430986 | 1132 | |
19070062 | 1133 | static int method_cancel_job(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
99534007 | 1134 | Manager *m = ASSERT_PTR(userdata); |
718db961 LP |
1135 | uint32_t id; |
1136 | Job *j; | |
1137 | int r; | |
ea430986 | 1138 | |
718db961 | 1139 | assert(message); |
ea430986 | 1140 | |
718db961 LP |
1141 | r = sd_bus_message_read(message, "u", &id); |
1142 | if (r < 0) | |
ebcf1f97 | 1143 | return r; |
ea430986 | 1144 | |
718db961 LP |
1145 | j = manager_get_job(m, id); |
1146 | if (!j) | |
ebcf1f97 | 1147 | return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_JOB, "Job %u does not exist.", (unsigned) id); |
ea430986 | 1148 | |
19070062 | 1149 | return bus_job_method_cancel(message, j, error); |
718db961 | 1150 | } |
c1e1601e | 1151 | |
19070062 | 1152 | static int method_clear_jobs(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
99534007 | 1153 | Manager *m = ASSERT_PTR(userdata); |
ebcf1f97 | 1154 | int r; |
c1e1601e | 1155 | |
718db961 | 1156 | assert(message); |
c1e1601e | 1157 | |
1d22e906 | 1158 | r = mac_selinux_access_check(message, "reload", error); |
ebcf1f97 LP |
1159 | if (r < 0) |
1160 | return r; | |
1161 | ||
1d22e906 LP |
1162 | r = bus_verify_manage_units_async(m, message, error); |
1163 | if (r < 0) | |
1164 | return r; | |
1165 | if (r == 0) | |
1166 | return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */ | |
1167 | ||
718db961 | 1168 | manager_clear_jobs(m); |
c1e1601e | 1169 | |
df2d202e | 1170 | return sd_bus_reply_method_return(message, NULL); |
718db961 | 1171 | } |
c1e1601e | 1172 | |
19070062 | 1173 | static int method_reset_failed(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
99534007 | 1174 | Manager *m = ASSERT_PTR(userdata); |
ebcf1f97 | 1175 | int r; |
cad45ba1 | 1176 | |
718db961 | 1177 | assert(message); |
718db961 | 1178 | |
8a188de9 | 1179 | r = mac_selinux_access_check(message, "reload", error); |
ebcf1f97 LP |
1180 | if (r < 0) |
1181 | return r; | |
1182 | ||
1d22e906 LP |
1183 | r = bus_verify_manage_units_async(m, message, error); |
1184 | if (r < 0) | |
1185 | return r; | |
1186 | if (r == 0) | |
1187 | return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */ | |
1188 | ||
718db961 | 1189 | manager_reset_failed(m); |
c1e1601e | 1190 | |
df2d202e | 1191 | return sd_bus_reply_method_return(message, NULL); |
718db961 | 1192 | } |
c1e1601e | 1193 | |
313fe66f | 1194 | static int list_units_filtered(sd_bus_message *message, void *userdata, sd_bus_error *error, char **states, char **patterns) { |
4afd3348 | 1195 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; |
99534007 | 1196 | Manager *m = ASSERT_PTR(userdata); |
718db961 | 1197 | const char *k; |
718db961 LP |
1198 | Unit *u; |
1199 | int r; | |
c1e1601e | 1200 | |
718db961 | 1201 | assert(message); |
b152adec | 1202 | |
283868e1 SW |
1203 | /* Anyone can call this method */ |
1204 | ||
8a188de9 | 1205 | r = mac_selinux_access_check(message, "status", error); |
ebcf1f97 LP |
1206 | if (r < 0) |
1207 | return r; | |
cad45ba1 | 1208 | |
df2d202e | 1209 | r = sd_bus_message_new_method_return(message, &reply); |
718db961 | 1210 | if (r < 0) |
ebcf1f97 | 1211 | return r; |
b152adec | 1212 | |
718db961 LP |
1213 | r = sd_bus_message_open_container(reply, 'a', "(ssssssouso)"); |
1214 | if (r < 0) | |
ebcf1f97 | 1215 | return r; |
b152adec | 1216 | |
90e74a66 | 1217 | HASHMAP_FOREACH_KEY(u, k, m->units) { |
718db961 LP |
1218 | if (k != u->id) |
1219 | continue; | |
b152adec | 1220 | |
cdc06ed7 DS |
1221 | if (!strv_isempty(states) && |
1222 | !strv_contains(states, unit_load_state_to_string(u->load_state)) && | |
1223 | !strv_contains(states, unit_active_state_to_string(unit_active_state(u))) && | |
1224 | !strv_contains(states, unit_sub_state_to_string(u))) | |
1225 | continue; | |
1226 | ||
313fe66f | 1227 | if (!strv_isempty(patterns) && |
1228 | !strv_fnmatch_or_empty(patterns, u->id, FNM_NOESCAPE)) | |
1229 | continue; | |
1230 | ||
6900c740 | 1231 | r = reply_unit_info(reply, u); |
718db961 | 1232 | if (r < 0) |
ebcf1f97 | 1233 | return r; |
718db961 | 1234 | } |
4139c1b2 | 1235 | |
718db961 LP |
1236 | r = sd_bus_message_close_container(reply); |
1237 | if (r < 0) | |
ebcf1f97 | 1238 | return r; |
cad45ba1 | 1239 | |
9030ca46 | 1240 | return sd_bus_send(NULL, reply, NULL); |
718db961 | 1241 | } |
4139c1b2 | 1242 | |
19070062 | 1243 | static int method_list_units(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
313fe66f | 1244 | return list_units_filtered(message, userdata, error, NULL, NULL); |
cdc06ed7 DS |
1245 | } |
1246 | ||
19070062 | 1247 | static int method_list_units_filtered(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
cdc06ed7 DS |
1248 | _cleanup_strv_free_ char **states = NULL; |
1249 | int r; | |
1250 | ||
1251 | r = sd_bus_message_read_strv(message, &states); | |
1252 | if (r < 0) | |
1253 | return r; | |
1254 | ||
313fe66f | 1255 | return list_units_filtered(message, userdata, error, states, NULL); |
1256 | } | |
1257 | ||
1258 | static int method_list_units_by_patterns(sd_bus_message *message, void *userdata, sd_bus_error *error) { | |
1259 | _cleanup_strv_free_ char **states = NULL; | |
1260 | _cleanup_strv_free_ char **patterns = NULL; | |
1261 | int r; | |
1262 | ||
1263 | r = sd_bus_message_read_strv(message, &states); | |
1264 | if (r < 0) | |
1265 | return r; | |
1266 | ||
1267 | r = sd_bus_message_read_strv(message, &patterns); | |
1268 | if (r < 0) | |
1269 | return r; | |
1270 | ||
1271 | return list_units_filtered(message, userdata, error, states, patterns); | |
cdc06ed7 DS |
1272 | } |
1273 | ||
19070062 | 1274 | static int method_list_jobs(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
4afd3348 | 1275 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; |
99534007 | 1276 | Manager *m = ASSERT_PTR(userdata); |
718db961 LP |
1277 | Job *j; |
1278 | int r; | |
4139c1b2 | 1279 | |
718db961 | 1280 | assert(message); |
718db961 | 1281 | |
283868e1 SW |
1282 | /* Anyone can call this method */ |
1283 | ||
8a188de9 | 1284 | r = mac_selinux_access_check(message, "status", error); |
ebcf1f97 LP |
1285 | if (r < 0) |
1286 | return r; | |
718db961 | 1287 | |
df2d202e | 1288 | r = sd_bus_message_new_method_return(message, &reply); |
718db961 | 1289 | if (r < 0) |
ebcf1f97 | 1290 | return r; |
718db961 LP |
1291 | |
1292 | r = sd_bus_message_open_container(reply, 'a', "(usssoo)"); | |
1293 | if (r < 0) | |
ebcf1f97 | 1294 | return r; |
718db961 | 1295 | |
90e74a66 | 1296 | HASHMAP_FOREACH(j, m->jobs) { |
718db961 LP |
1297 | _cleanup_free_ char *unit_path = NULL, *job_path = NULL; |
1298 | ||
1299 | job_path = job_dbus_path(j); | |
1300 | if (!job_path) | |
ebcf1f97 | 1301 | return -ENOMEM; |
718db961 LP |
1302 | |
1303 | unit_path = unit_dbus_path(j->unit); | |
1304 | if (!unit_path) | |
ebcf1f97 | 1305 | return -ENOMEM; |
718db961 LP |
1306 | |
1307 | r = sd_bus_message_append( | |
1308 | reply, "(usssoo)", | |
1309 | j->id, | |
1302759d | 1310 | j->unit->id, |
718db961 | 1311 | job_type_to_string(j->type), |
1302759d | 1312 | job_state_to_string(j->state), |
718db961 LP |
1313 | job_path, |
1314 | unit_path); | |
cad45ba1 | 1315 | if (r < 0) |
ebcf1f97 | 1316 | return r; |
718db961 | 1317 | } |
5dd9014f | 1318 | |
718db961 LP |
1319 | r = sd_bus_message_close_container(reply); |
1320 | if (r < 0) | |
ebcf1f97 | 1321 | return r; |
5dd9014f | 1322 | |
9030ca46 | 1323 | return sd_bus_send(NULL, reply, NULL); |
718db961 | 1324 | } |
5dd9014f | 1325 | |
19070062 | 1326 | static int method_subscribe(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
99534007 | 1327 | Manager *m = ASSERT_PTR(userdata); |
718db961 | 1328 | int r; |
5dd9014f | 1329 | |
718db961 | 1330 | assert(message); |
ea430986 | 1331 | |
283868e1 SW |
1332 | /* Anyone can call this method */ |
1333 | ||
8a188de9 | 1334 | r = mac_selinux_access_check(message, "status", error); |
ebcf1f97 LP |
1335 | if (r < 0) |
1336 | return r; | |
cad45ba1 | 1337 | |
19070062 | 1338 | if (sd_bus_message_get_bus(message) == m->api_bus) { |
8f8f05a9 LP |
1339 | |
1340 | /* Note that direct bus connection subscribe by | |
1341 | * default, we only track peers on the API bus here */ | |
1342 | ||
1343 | if (!m->subscribed) { | |
19070062 | 1344 | r = sd_bus_track_new(sd_bus_message_get_bus(message), &m->subscribed, NULL, NULL); |
8f8f05a9 LP |
1345 | if (r < 0) |
1346 | return r; | |
1347 | } | |
1348 | ||
1349 | r = sd_bus_track_add_sender(m->subscribed, message); | |
1350 | if (r < 0) | |
1351 | return r; | |
1352 | if (r == 0) | |
1b09b81c | 1353 | return sd_bus_error_set(error, BUS_ERROR_ALREADY_SUBSCRIBED, "Client is already subscribed."); |
8f8f05a9 | 1354 | } |
ea430986 | 1355 | |
df2d202e | 1356 | return sd_bus_reply_method_return(message, NULL); |
718db961 | 1357 | } |
ea430986 | 1358 | |
19070062 | 1359 | static int method_unsubscribe(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
99534007 | 1360 | Manager *m = ASSERT_PTR(userdata); |
718db961 | 1361 | int r; |
ea430986 | 1362 | |
718db961 | 1363 | assert(message); |
ea430986 | 1364 | |
283868e1 SW |
1365 | /* Anyone can call this method */ |
1366 | ||
8a188de9 | 1367 | r = mac_selinux_access_check(message, "status", error); |
ebcf1f97 LP |
1368 | if (r < 0) |
1369 | return r; | |
ea430986 | 1370 | |
19070062 | 1371 | if (sd_bus_message_get_bus(message) == m->api_bus) { |
8f8f05a9 LP |
1372 | r = sd_bus_track_remove_sender(m->subscribed, message); |
1373 | if (r < 0) | |
1374 | return r; | |
1375 | if (r == 0) | |
1b09b81c | 1376 | return sd_bus_error_set(error, BUS_ERROR_NOT_SUBSCRIBED, "Client is not subscribed."); |
8f8f05a9 | 1377 | } |
ea430986 | 1378 | |
df2d202e | 1379 | return sd_bus_reply_method_return(message, NULL); |
718db961 | 1380 | } |
ea430986 | 1381 | |
d1d8786c FB |
1382 | static int dump_impl( |
1383 | sd_bus_message *message, | |
1384 | void *userdata, | |
1385 | sd_bus_error *error, | |
1386 | char **patterns, | |
1387 | int (*reply)(sd_bus_message *, char *)) { | |
1388 | ||
718db961 | 1389 | _cleanup_free_ char *dump = NULL; |
99534007 | 1390 | Manager *m = ASSERT_PTR(userdata); |
ebcf1f97 | 1391 | int r; |
ea430986 | 1392 | |
718db961 | 1393 | assert(message); |
ea430986 | 1394 | |
d9365956 LB |
1395 | /* 'status' access is the bare minimum always needed for this, as the policy might straight out |
1396 | * forbid a client from querying any information from systemd, regardless of any rate limiting. */ | |
8a188de9 | 1397 | r = mac_selinux_access_check(message, "status", error); |
ebcf1f97 LP |
1398 | if (r < 0) |
1399 | return r; | |
ea430986 | 1400 | |
d9365956 LB |
1401 | /* Rate limit reached? Check if the caller is privileged/allowed by policy to bypass this. We |
1402 | * check the rate limit first to avoid the expensive roundtrip to polkit when not needed. */ | |
1403 | if (!ratelimit_below(&m->dump_ratelimit)) { | |
1404 | /* We need a way for SELinux to constrain the operation when the rate limit is active, even | |
1405 | * if polkit would allow it, but we cannot easily add new named permissions, so we need to | |
1406 | * use an existing one. Reload/reexec are also slow but non-destructive/modifying | |
1407 | * operations, and can cause PID1 to stall. So it seems similar enough in terms of security | |
1408 | * considerations and impact, and thus use the same access check for dumps which, given the | |
1409 | * large amount of data to fetch, can stall PID1 for quite some time. */ | |
ba4c69a0 | 1410 | r = mac_selinux_access_check(message, "reload", /* error = */ NULL); |
d9365956 LB |
1411 | if (r < 0) |
1412 | goto ratelimited; | |
1413 | ||
ba4c69a0 | 1414 | r = bus_verify_bypass_dump_ratelimit_async(m, message, /* error = */ NULL); |
d9365956 LB |
1415 | if (r < 0) |
1416 | goto ratelimited; | |
1417 | if (r == 0) | |
1418 | /* No authorization for now, but the async polkit stuff will call us again when it | |
1419 | * has it */ | |
1420 | return 1; | |
1421 | } | |
1422 | ||
d1d8786c | 1423 | r = manager_get_dump_string(m, patterns, &dump); |
dacd6cee LP |
1424 | if (r < 0) |
1425 | return r; | |
a16e1123 | 1426 | |
c0a1bfac | 1427 | return reply(message, dump); |
d9365956 LB |
1428 | |
1429 | ratelimited: | |
1430 | log_warning("Dump request rejected due to rate limit on unprivileged callers, blocked for %s.", | |
1431 | FORMAT_TIMESPAN(ratelimit_left(&m->dump_ratelimit), USEC_PER_SEC)); | |
1432 | return sd_bus_error_setf(error, | |
1433 | SD_BUS_ERROR_LIMITS_EXCEEDED, | |
1434 | "Dump request rejected due to rate limit on unprivileged callers, blocked for %s.", | |
1435 | FORMAT_TIMESPAN(ratelimit_left(&m->dump_ratelimit), USEC_PER_SEC)); | |
c0a1bfac DT |
1436 | } |
1437 | ||
1438 | static int reply_dump(sd_bus_message *message, char *dump) { | |
df2d202e | 1439 | return sd_bus_reply_method_return(message, "s", dump); |
718db961 | 1440 | } |
cad45ba1 | 1441 | |
c0a1bfac | 1442 | static int method_dump(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
d1d8786c | 1443 | return dump_impl(message, userdata, error, NULL, reply_dump); |
c0a1bfac DT |
1444 | } |
1445 | ||
1446 | static int reply_dump_by_fd(sd_bus_message *message, char *dump) { | |
254d1313 | 1447 | _cleanup_close_ int fd = -EBADF; |
c0a1bfac | 1448 | |
0870fc24 | 1449 | fd = acquire_data_fd(dump); |
c0a1bfac DT |
1450 | if (fd < 0) |
1451 | return fd; | |
1452 | ||
1453 | return sd_bus_reply_method_return(message, "h", fd); | |
1454 | } | |
1455 | ||
1456 | static int method_dump_by_fd(sd_bus_message *message, void *userdata, sd_bus_error *error) { | |
d1d8786c FB |
1457 | return dump_impl(message, userdata, error, NULL, reply_dump_by_fd); |
1458 | } | |
1459 | ||
f6cce15b ZJS |
1460 | static int dump_units_matching_patterns( |
1461 | sd_bus_message *message, | |
1462 | void *userdata, | |
1463 | sd_bus_error *error, | |
1464 | int (*reply)(sd_bus_message *, char *)) { | |
d1d8786c FB |
1465 | _cleanup_strv_free_ char **patterns = NULL; |
1466 | int r; | |
1467 | ||
1468 | r = sd_bus_message_read_strv(message, &patterns); | |
1469 | if (r < 0) | |
1470 | return r; | |
1471 | ||
f6cce15b ZJS |
1472 | return dump_impl(message, userdata, error, patterns, reply); |
1473 | } | |
1474 | ||
1475 | static int method_dump_units_matching_patterns(sd_bus_message *message, void *userdata, sd_bus_error *error) { | |
1476 | return dump_units_matching_patterns(message, userdata, error, reply_dump); | |
1477 | } | |
1478 | ||
1479 | static int method_dump_units_matching_patterns_by_fd(sd_bus_message *message, void *userdata, sd_bus_error *error) { | |
1480 | return dump_units_matching_patterns(message, userdata, error, reply_dump_by_fd); | |
c0a1bfac DT |
1481 | } |
1482 | ||
36b4a7ba | 1483 | static int method_refuse_snapshot(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
1b09b81c | 1484 | return sd_bus_error_set(error, SD_BUS_ERROR_NOT_SUPPORTED, "Support for snapshots has been removed."); |
718db961 | 1485 | } |
cad45ba1 | 1486 | |
c23e9b6a | 1487 | static int get_run_space(uint64_t *ret, sd_bus_error *error) { |
ae57dad3 | 1488 | struct statvfs svfs; |
c23e9b6a LP |
1489 | |
1490 | assert(ret); | |
ae57dad3 LP |
1491 | |
1492 | if (statvfs("/run/systemd", &svfs) < 0) | |
1493 | return sd_bus_error_set_errnof(error, errno, "Failed to statvfs(/run/systemd): %m"); | |
1494 | ||
c23e9b6a LP |
1495 | *ret = (uint64_t) svfs.f_bfree * (uint64_t) svfs.f_bsize; |
1496 | return 0; | |
1497 | } | |
1498 | ||
1499 | static int verify_run_space(const char *message, sd_bus_error *error) { | |
1500 | uint64_t available = 0; /* unnecessary, but used to trick out gcc's incorrect maybe-uninitialized warning */ | |
1501 | int r; | |
1502 | ||
1503 | assert(message); | |
1504 | ||
1505 | r = get_run_space(&available, error); | |
1506 | if (r < 0) | |
1507 | return r; | |
ae57dad3 | 1508 | |
2b59bf51 | 1509 | if (available < RELOAD_DISK_SPACE_MIN) |
ae57dad3 LP |
1510 | return sd_bus_error_setf(error, |
1511 | BUS_ERROR_DISK_FULL, | |
c23e9b6a | 1512 | "%s, not enough space available on /run/systemd/. " |
ae57dad3 LP |
1513 | "Currently, %s are free, but a safety buffer of %s is enforced.", |
1514 | message, | |
2b59bf51 ZJS |
1515 | FORMAT_BYTES(available), |
1516 | FORMAT_BYTES(RELOAD_DISK_SPACE_MIN)); | |
ae57dad3 LP |
1517 | |
1518 | return 0; | |
1519 | } | |
1520 | ||
1521 | int verify_run_space_and_log(const char *message) { | |
4dae8ae6 | 1522 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
ae57dad3 LP |
1523 | int r; |
1524 | ||
c23e9b6a LP |
1525 | assert(message); |
1526 | ||
ae57dad3 LP |
1527 | r = verify_run_space(message, &error); |
1528 | if (r < 0) | |
4ae25393 | 1529 | return log_error_errno(r, "%s", bus_error_message(&error, r)); |
ae57dad3 | 1530 | |
4ae25393 | 1531 | return 0; |
ae57dad3 LP |
1532 | } |
1533 | ||
c23e9b6a LP |
1534 | static int verify_run_space_permissive(const char *message, sd_bus_error *error) { |
1535 | uint64_t available = 0; /* unnecessary, but used to trick out gcc's incorrect maybe-uninitialized warning */ | |
1536 | int r; | |
1537 | ||
1538 | assert(message); | |
1539 | ||
1540 | r = get_run_space(&available, error); | |
1541 | if (r < 0) | |
1542 | return r; | |
1543 | ||
1544 | if (available < RELOAD_DISK_SPACE_MIN) | |
1545 | log_warning("Dangerously low amount of free space on /run/systemd/, %s.\n" | |
1546 | "Currently, %s are free, but %s are suggested. Proceeding anyway.", | |
1547 | message, | |
1548 | FORMAT_BYTES(available), | |
1549 | FORMAT_BYTES(RELOAD_DISK_SPACE_MIN)); | |
1550 | ||
1551 | return 0; | |
1552 | } | |
1553 | ||
4895bacc | 1554 | static void log_caller(sd_bus_message *message, Manager *manager, const char *method) { |
9524c2fd LB |
1555 | _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL; |
1556 | const char *comm = NULL; | |
1557 | Unit *caller; | |
1558 | pid_t pid; | |
1559 | ||
1560 | assert(message); | |
1561 | assert(manager); | |
4895bacc | 1562 | assert(method); |
9524c2fd LB |
1563 | |
1564 | if (sd_bus_query_sender_creds(message, SD_BUS_CREDS_PID|SD_BUS_CREDS_AUGMENT|SD_BUS_CREDS_COMM, &creds) < 0) | |
1565 | return; | |
1566 | ||
1567 | /* We need at least the PID, otherwise there's nothing to log, the rest is optional */ | |
1568 | if (sd_bus_creds_get_pid(creds, &pid) < 0) | |
1569 | return; | |
1570 | ||
1571 | (void) sd_bus_creds_get_comm(creds, &comm); | |
1572 | caller = manager_get_unit_by_pid(manager, pid); | |
1573 | ||
1980a25d ZJS |
1574 | log_info("%s requested from client PID " PID_FMT "%s%s%s%s%s%s...", |
1575 | method, pid, | |
1576 | comm ? " ('" : "", strempty(comm), comm ? "')" : "", | |
add74820 | 1577 | caller ? " (unit " : "", caller ? caller->id : "", caller ? ")" : ""); |
9524c2fd LB |
1578 | } |
1579 | ||
19070062 | 1580 | static int method_reload(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
99534007 | 1581 | Manager *m = ASSERT_PTR(userdata); |
718db961 | 1582 | int r; |
6652a2b9 | 1583 | |
718db961 | 1584 | assert(message); |
6652a2b9 | 1585 | |
ae57dad3 LP |
1586 | r = verify_run_space("Refusing to reload", error); |
1587 | if (r < 0) | |
1588 | return r; | |
1589 | ||
1d22e906 | 1590 | r = mac_selinux_access_check(message, "reload", error); |
283868e1 SW |
1591 | if (r < 0) |
1592 | return r; | |
283868e1 | 1593 | |
1d22e906 | 1594 | r = bus_verify_reload_daemon_async(m, message, error); |
ebcf1f97 LP |
1595 | if (r < 0) |
1596 | return r; | |
1d22e906 LP |
1597 | if (r == 0) |
1598 | return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */ | |
6652a2b9 | 1599 | |
9524c2fd | 1600 | /* Write a log message noting the unit or process who requested the Reload() */ |
71322b95 | 1601 | log_caller(message, m, "Reload"); |
9524c2fd | 1602 | |
856bfaeb | 1603 | /* Check the rate limit after the authorization succeeds, to avoid denial-of-service issues. */ |
8312b17a | 1604 | if (!ratelimit_below(&m->reload_reexec_ratelimit)) { |
856bfaeb LB |
1605 | log_warning("Reloading request rejected due to rate limit."); |
1606 | return sd_bus_error_setf(error, | |
1607 | SD_BUS_ERROR_LIMITS_EXCEEDED, | |
1608 | "Reload() request rejected due to rate limit."); | |
1609 | } | |
1610 | ||
718db961 LP |
1611 | /* Instead of sending the reply back right away, we just |
1612 | * remember that we need to and then send it after the reload | |
1613 | * is finished. That way the caller knows when the reload | |
1614 | * finished. */ | |
6652a2b9 | 1615 | |
209de525 LP |
1616 | assert(!m->pending_reload_message); |
1617 | r = sd_bus_message_new_method_return(message, &m->pending_reload_message); | |
718db961 | 1618 | if (r < 0) |
ebcf1f97 | 1619 | return r; |
cad45ba1 | 1620 | |
af41e508 | 1621 | m->objective = MANAGER_RELOAD; |
6652a2b9 | 1622 | |
718db961 LP |
1623 | return 1; |
1624 | } | |
6652a2b9 | 1625 | |
19070062 | 1626 | static int method_reexecute(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
99534007 | 1627 | Manager *m = ASSERT_PTR(userdata); |
ebcf1f97 | 1628 | int r; |
6652a2b9 | 1629 | |
718db961 | 1630 | assert(message); |
6652a2b9 | 1631 | |
ae57dad3 LP |
1632 | r = verify_run_space("Refusing to reexecute", error); |
1633 | if (r < 0) | |
1634 | return r; | |
1635 | ||
1d22e906 | 1636 | r = mac_selinux_access_check(message, "reload", error); |
283868e1 SW |
1637 | if (r < 0) |
1638 | return r; | |
283868e1 | 1639 | |
1d22e906 | 1640 | r = bus_verify_reload_daemon_async(m, message, error); |
ebcf1f97 LP |
1641 | if (r < 0) |
1642 | return r; | |
1d22e906 LP |
1643 | if (r == 0) |
1644 | return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */ | |
cad45ba1 | 1645 | |
4895bacc | 1646 | /* Write a log message noting the unit or process who requested the Reexecute() */ |
71322b95 | 1647 | log_caller(message, m, "Reexecution"); |
4895bacc | 1648 | |
8312b17a LB |
1649 | /* Check the rate limit after the authorization succeeds, to avoid denial-of-service issues. */ |
1650 | if (!ratelimit_below(&m->reload_reexec_ratelimit)) { | |
71322b95 | 1651 | log_warning("Reexecution request rejected due to rate limit."); |
8312b17a LB |
1652 | return sd_bus_error_setf(error, |
1653 | SD_BUS_ERROR_LIMITS_EXCEEDED, | |
1654 | "Reexecute() request rejected due to rate limit."); | |
1655 | } | |
1656 | ||
718db961 LP |
1657 | /* We don't send a reply back here, the client should |
1658 | * just wait for us disconnecting. */ | |
6652a2b9 | 1659 | |
af41e508 | 1660 | m->objective = MANAGER_REEXECUTE; |
718db961 LP |
1661 | return 1; |
1662 | } | |
6652a2b9 | 1663 | |
19070062 | 1664 | static int method_exit(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
99534007 | 1665 | Manager *m = ASSERT_PTR(userdata); |
ebcf1f97 | 1666 | int r; |
6652a2b9 | 1667 | |
718db961 | 1668 | assert(message); |
6652a2b9 | 1669 | |
8a188de9 | 1670 | r = mac_selinux_access_check(message, "halt", error); |
ebcf1f97 LP |
1671 | if (r < 0) |
1672 | return r; | |
cad45ba1 | 1673 | |
287419c1 AC |
1674 | /* Exit() (in contrast to SetExitCode()) is actually allowed even if |
1675 | * we are running on the host. It will fall back on reboot() in | |
1676 | * systemd-shutdown if it cannot do the exit() because it isn't a | |
1677 | * container. */ | |
6652a2b9 | 1678 | |
af41e508 | 1679 | m->objective = MANAGER_EXIT; |
6652a2b9 | 1680 | |
df2d202e | 1681 | return sd_bus_reply_method_return(message, NULL); |
718db961 | 1682 | } |
6652a2b9 | 1683 | |
19070062 | 1684 | static int method_reboot(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
99534007 | 1685 | Manager *m = ASSERT_PTR(userdata); |
ebcf1f97 | 1686 | int r; |
664f88a7 | 1687 | |
718db961 | 1688 | assert(message); |
cad45ba1 | 1689 | |
8a188de9 | 1690 | r = mac_selinux_access_check(message, "reboot", error); |
ebcf1f97 LP |
1691 | if (r < 0) |
1692 | return r; | |
664f88a7 | 1693 | |
463d0d15 | 1694 | if (!MANAGER_IS_SYSTEM(m)) |
cb31470f ZJS |
1695 | return sd_bus_error_setf(error, SD_BUS_ERROR_NOT_SUPPORTED, |
1696 | "Reboot is only supported for system managers."); | |
664f88a7 | 1697 | |
af41e508 | 1698 | m->objective = MANAGER_REBOOT; |
664f88a7 | 1699 | |
df2d202e | 1700 | return sd_bus_reply_method_return(message, NULL); |
718db961 | 1701 | } |
664f88a7 | 1702 | |
13ffc607 LP |
1703 | static int method_soft_reboot(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
1704 | _cleanup_free_ char *rt = NULL; | |
1705 | Manager *m = ASSERT_PTR(userdata); | |
1706 | const char *root; | |
1707 | int r; | |
1708 | ||
1709 | assert(message); | |
1710 | ||
1711 | r = verify_run_space_permissive("soft reboot may fail", error); | |
1712 | if (r < 0) | |
1713 | return r; | |
1714 | ||
1715 | r = mac_selinux_access_check(message, "reboot", error); | |
1716 | if (r < 0) | |
1717 | return r; | |
1718 | ||
1719 | r = sd_bus_message_read(message, "s", &root); | |
1720 | if (r < 0) | |
1721 | return r; | |
1722 | ||
1723 | if (!isempty(root)) { | |
1724 | if (!path_is_valid(root)) | |
1725 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, | |
1726 | "New root directory '%s' must be a valid path.", root); | |
1727 | if (!path_is_absolute(root)) | |
1728 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, | |
1729 | "New root directory path '%s' is not absolute.", root); | |
1730 | ||
1731 | rt = strdup(root); | |
1732 | if (!rt) | |
1733 | return -ENOMEM; | |
1734 | } | |
1735 | ||
1736 | free_and_replace(m->switch_root, rt); | |
1737 | m->objective = MANAGER_SOFT_REBOOT; | |
1738 | ||
1739 | return sd_bus_reply_method_return(message, NULL); | |
1740 | } | |
1741 | ||
19070062 | 1742 | static int method_poweroff(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
99534007 | 1743 | Manager *m = ASSERT_PTR(userdata); |
ebcf1f97 | 1744 | int r; |
50913bc0 | 1745 | |
718db961 | 1746 | assert(message); |
1137a57c | 1747 | |
8a188de9 | 1748 | r = mac_selinux_access_check(message, "halt", error); |
ebcf1f97 LP |
1749 | if (r < 0) |
1750 | return r; | |
1137a57c | 1751 | |
463d0d15 | 1752 | if (!MANAGER_IS_SYSTEM(m)) |
cb31470f ZJS |
1753 | return sd_bus_error_setf(error, SD_BUS_ERROR_NOT_SUPPORTED, |
1754 | "Powering off is only supported for system managers."); | |
1137a57c | 1755 | |
af41e508 | 1756 | m->objective = MANAGER_POWEROFF; |
1137a57c | 1757 | |
df2d202e | 1758 | return sd_bus_reply_method_return(message, NULL); |
718db961 | 1759 | } |
1137a57c | 1760 | |
19070062 | 1761 | static int method_halt(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
99534007 | 1762 | Manager *m = ASSERT_PTR(userdata); |
ebcf1f97 | 1763 | int r; |
1137a57c | 1764 | |
718db961 | 1765 | assert(message); |
8d0e38a2 | 1766 | |
8a188de9 | 1767 | r = mac_selinux_access_check(message, "halt", error); |
ebcf1f97 LP |
1768 | if (r < 0) |
1769 | return r; | |
cad45ba1 | 1770 | |
463d0d15 | 1771 | if (!MANAGER_IS_SYSTEM(m)) |
cb31470f ZJS |
1772 | return sd_bus_error_setf(error, SD_BUS_ERROR_NOT_SUPPORTED, |
1773 | "Halt is only supported for system managers."); | |
8d0e38a2 | 1774 | |
af41e508 | 1775 | m->objective = MANAGER_HALT; |
8d0e38a2 | 1776 | |
df2d202e | 1777 | return sd_bus_reply_method_return(message, NULL); |
718db961 | 1778 | } |
8d0e38a2 | 1779 | |
19070062 | 1780 | static int method_kexec(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
99534007 | 1781 | Manager *m = ASSERT_PTR(userdata); |
ebcf1f97 | 1782 | int r; |
8d0e38a2 | 1783 | |
718db961 | 1784 | assert(message); |
c0576cd6 | 1785 | |
8a188de9 | 1786 | r = mac_selinux_access_check(message, "reboot", error); |
ebcf1f97 LP |
1787 | if (r < 0) |
1788 | return r; | |
cad45ba1 | 1789 | |
463d0d15 | 1790 | if (!MANAGER_IS_SYSTEM(m)) |
cb31470f ZJS |
1791 | return sd_bus_error_setf(error, SD_BUS_ERROR_NOT_SUPPORTED, |
1792 | "KExec is only supported for system managers."); | |
c0576cd6 | 1793 | |
af41e508 | 1794 | m->objective = MANAGER_KEXEC; |
c0576cd6 | 1795 | |
df2d202e | 1796 | return sd_bus_reply_method_return(message, NULL); |
718db961 | 1797 | } |
c0576cd6 | 1798 | |
19070062 | 1799 | static int method_switch_root(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
87a19bfe | 1800 | _cleanup_free_ char *ri = NULL, *rt = NULL; |
99534007 | 1801 | Manager *m = ASSERT_PTR(userdata); |
13ffc607 | 1802 | const char *root, *init; |
718db961 | 1803 | int r; |
c0576cd6 | 1804 | |
718db961 | 1805 | assert(message); |
c0576cd6 | 1806 | |
c23e9b6a LP |
1807 | r = verify_run_space_permissive("root switching may fail", error); |
1808 | if (r < 0) | |
1809 | return r; | |
ae57dad3 | 1810 | |
8a188de9 | 1811 | r = mac_selinux_access_check(message, "reboot", error); |
ebcf1f97 LP |
1812 | if (r < 0) |
1813 | return r; | |
c0576cd6 | 1814 | |
463d0d15 | 1815 | if (!MANAGER_IS_SYSTEM(m)) |
cb31470f ZJS |
1816 | return sd_bus_error_setf(error, SD_BUS_ERROR_NOT_SUPPORTED, |
1817 | "Root switching is only supported by system manager."); | |
c0576cd6 | 1818 | |
718db961 LP |
1819 | r = sd_bus_message_read(message, "ss", &root, &init); |
1820 | if (r < 0) | |
ebcf1f97 | 1821 | return r; |
c0576cd6 | 1822 | |
5ae89ef3 LP |
1823 | if (isempty(root)) |
1824 | /* If path is not specified, default to "/sysroot" which is what we generally expect initrds | |
1825 | * to use */ | |
1826 | root = "/sysroot"; | |
1827 | else { | |
1828 | if (!path_is_valid(root)) | |
1829 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, | |
1830 | "New root directory must be a valid path."); | |
b0c5f0e1 | 1831 | |
5ae89ef3 LP |
1832 | if (!path_is_absolute(root)) |
1833 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, | |
1834 | "New root path '%s' is not absolute.", root); | |
b0c5f0e1 MY |
1835 | |
1836 | r = path_is_root(root); | |
1837 | if (r < 0) | |
1838 | return sd_bus_error_set_errnof(error, r, | |
1839 | "Failed to check if new root directory '%s' is the same as old root: %m", | |
1840 | root); | |
1841 | if (r > 0) | |
5ae89ef3 LP |
1842 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, |
1843 | "New root directory cannot be the old root directory."); | |
1844 | } | |
c0576cd6 | 1845 | |
718db961 | 1846 | /* Safety check */ |
4da159bc MY |
1847 | if (!in_initrd()) |
1848 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, | |
1849 | "Not in initrd, refusing switch-root operation."); | |
1850 | ||
1851 | r = path_is_os_tree(root); | |
1852 | if (r < 0) | |
1853 | return sd_bus_error_set_errnof(error, r, | |
1854 | "Failed to determine whether root path '%s' contains an OS tree: %m", | |
1855 | root); | |
1856 | if (r == 0) | |
1857 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, | |
1858 | "Specified switch root path '%s' does not seem to be an OS tree. os-release file is missing.", | |
1859 | root); | |
1860 | ||
1861 | if (!isempty(init)) { | |
5f4c9c27 LP |
1862 | if (!path_is_valid(init)) |
1863 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, | |
1864 | "Path to init binary '%s' is not a valid path.", init); | |
1865 | ||
718db961 | 1866 | if (!path_is_absolute(init)) |
cb31470f ZJS |
1867 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, |
1868 | "Path to init binary '%s' not absolute.", init); | |
c0576cd6 | 1869 | |
f461a28d | 1870 | r = chase_and_access(init, root, CHASE_PREFIX_ROOT, X_OK, NULL); |
e10086ac LP |
1871 | if (r == -EACCES) |
1872 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, | |
1873 | "Init binary %s is not executable.", init); | |
710028b4 | 1874 | if (r < 0) |
cb31470f ZJS |
1875 | return sd_bus_error_set_errnof(error, r, |
1876 | "Could not resolve init executable %s: %m", init); | |
718db961 LP |
1877 | } |
1878 | ||
1879 | rt = strdup(root); | |
1880 | if (!rt) | |
ebcf1f97 | 1881 | return -ENOMEM; |
718db961 LP |
1882 | |
1883 | if (!isempty(init)) { | |
1884 | ri = strdup(init); | |
87a19bfe | 1885 | if (!ri) |
ebcf1f97 | 1886 | return -ENOMEM; |
718db961 | 1887 | } |
c0576cd6 | 1888 | |
87a19bfe YW |
1889 | free_and_replace(m->switch_root, rt); |
1890 | free_and_replace(m->switch_root_init, ri); | |
c0576cd6 | 1891 | |
af41e508 | 1892 | m->objective = MANAGER_SWITCH_ROOT; |
92b315df | 1893 | |
df2d202e | 1894 | return sd_bus_reply_method_return(message, NULL); |
718db961 | 1895 | } |
c0576cd6 | 1896 | |
19070062 | 1897 | static int method_set_environment(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
718db961 | 1898 | _cleanup_strv_free_ char **plus = NULL; |
99534007 | 1899 | Manager *m = ASSERT_PTR(userdata); |
718db961 | 1900 | int r; |
c0576cd6 | 1901 | |
718db961 | 1902 | assert(message); |
cad45ba1 | 1903 | |
8a188de9 | 1904 | r = mac_selinux_access_check(message, "reload", error); |
ebcf1f97 LP |
1905 | if (r < 0) |
1906 | return r; | |
c0576cd6 | 1907 | |
718db961 LP |
1908 | r = sd_bus_message_read_strv(message, &plus); |
1909 | if (r < 0) | |
ebcf1f97 | 1910 | return r; |
718db961 | 1911 | if (!strv_env_is_valid(plus)) |
1b09b81c | 1912 | return sd_bus_error_set(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid environment assignments"); |
c0576cd6 | 1913 | |
1d22e906 LP |
1914 | r = bus_verify_set_environment_async(m, message, error); |
1915 | if (r < 0) | |
1916 | return r; | |
1917 | if (r == 0) | |
1918 | return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */ | |
1919 | ||
1ad6e8b3 | 1920 | r = manager_client_environment_modify(m, NULL, plus); |
718db961 | 1921 | if (r < 0) |
ebcf1f97 | 1922 | return r; |
c0576cd6 | 1923 | |
df2d202e | 1924 | return sd_bus_reply_method_return(message, NULL); |
718db961 LP |
1925 | } |
1926 | ||
19070062 | 1927 | static int method_unset_environment(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
718db961 | 1928 | _cleanup_strv_free_ char **minus = NULL; |
99534007 | 1929 | Manager *m = ASSERT_PTR(userdata); |
718db961 LP |
1930 | int r; |
1931 | ||
718db961 | 1932 | assert(message); |
718db961 | 1933 | |
8a188de9 | 1934 | r = mac_selinux_access_check(message, "reload", error); |
ebcf1f97 LP |
1935 | if (r < 0) |
1936 | return r; | |
c0576cd6 | 1937 | |
718db961 LP |
1938 | r = sd_bus_message_read_strv(message, &minus); |
1939 | if (r < 0) | |
ebcf1f97 | 1940 | return r; |
718db961 LP |
1941 | |
1942 | if (!strv_env_name_or_assignment_is_valid(minus)) | |
cb31470f ZJS |
1943 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, |
1944 | "Invalid environment variable names or assignments"); | |
718db961 | 1945 | |
1d22e906 LP |
1946 | r = bus_verify_set_environment_async(m, message, error); |
1947 | if (r < 0) | |
1948 | return r; | |
1949 | if (r == 0) | |
1950 | return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */ | |
1951 | ||
1ad6e8b3 | 1952 | r = manager_client_environment_modify(m, minus, NULL); |
718db961 | 1953 | if (r < 0) |
ebcf1f97 | 1954 | return r; |
718db961 | 1955 | |
df2d202e | 1956 | return sd_bus_reply_method_return(message, NULL); |
718db961 LP |
1957 | } |
1958 | ||
19070062 | 1959 | static int method_unset_and_set_environment(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
718db961 | 1960 | _cleanup_strv_free_ char **minus = NULL, **plus = NULL; |
99534007 | 1961 | Manager *m = ASSERT_PTR(userdata); |
718db961 LP |
1962 | int r; |
1963 | ||
718db961 | 1964 | assert(message); |
718db961 | 1965 | |
8a188de9 | 1966 | r = mac_selinux_access_check(message, "reload", error); |
ebcf1f97 LP |
1967 | if (r < 0) |
1968 | return r; | |
718db961 | 1969 | |
eb6c7d20 | 1970 | r = sd_bus_message_read_strv(message, &minus); |
718db961 | 1971 | if (r < 0) |
ebcf1f97 | 1972 | return r; |
718db961 | 1973 | |
eb6c7d20 | 1974 | r = sd_bus_message_read_strv(message, &plus); |
718db961 | 1975 | if (r < 0) |
ebcf1f97 | 1976 | return r; |
718db961 | 1977 | |
718db961 | 1978 | if (!strv_env_name_or_assignment_is_valid(minus)) |
cb31470f ZJS |
1979 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, |
1980 | "Invalid environment variable names or assignments"); | |
eb6c7d20 | 1981 | if (!strv_env_is_valid(plus)) |
cb31470f ZJS |
1982 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, |
1983 | "Invalid environment assignments"); | |
718db961 | 1984 | |
1d22e906 LP |
1985 | r = bus_verify_set_environment_async(m, message, error); |
1986 | if (r < 0) | |
1987 | return r; | |
1988 | if (r == 0) | |
1989 | return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */ | |
1990 | ||
1ad6e8b3 | 1991 | r = manager_client_environment_modify(m, minus, plus); |
718db961 | 1992 | if (r < 0) |
ebcf1f97 | 1993 | return r; |
718db961 | 1994 | |
df2d202e | 1995 | return sd_bus_reply_method_return(message, NULL); |
718db961 LP |
1996 | } |
1997 | ||
287419c1 | 1998 | static int method_set_exit_code(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
99534007 | 1999 | Manager *m = ASSERT_PTR(userdata); |
29206d46 | 2000 | uint8_t code; |
287419c1 AC |
2001 | int r; |
2002 | ||
2003 | assert(message); | |
287419c1 AC |
2004 | |
2005 | r = mac_selinux_access_check(message, "exit", error); | |
2006 | if (r < 0) | |
2007 | return r; | |
2008 | ||
2009 | r = sd_bus_message_read_basic(message, 'y', &code); | |
2010 | if (r < 0) | |
2011 | return r; | |
2012 | ||
287419c1 AC |
2013 | m->return_value = code; |
2014 | ||
2015 | return sd_bus_reply_method_return(message, NULL); | |
2016 | } | |
2017 | ||
29206d46 | 2018 | static int method_lookup_dynamic_user_by_name(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
99534007 | 2019 | Manager *m = ASSERT_PTR(userdata); |
29206d46 LP |
2020 | const char *name; |
2021 | uid_t uid; | |
2022 | int r; | |
2023 | ||
2024 | assert(message); | |
29206d46 LP |
2025 | |
2026 | r = sd_bus_message_read_basic(message, 's', &name); | |
2027 | if (r < 0) | |
2028 | return r; | |
2029 | ||
2030 | if (!MANAGER_IS_SYSTEM(m)) | |
cb31470f ZJS |
2031 | return sd_bus_error_setf(error, SD_BUS_ERROR_NOT_SUPPORTED, |
2032 | "Dynamic users are only supported in the system instance."); | |
7a8867ab | 2033 | if (!valid_user_group_name(name, VALID_USER_RELAX)) |
cb31470f ZJS |
2034 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, |
2035 | "User name invalid: %s", name); | |
29206d46 LP |
2036 | |
2037 | r = dynamic_user_lookup_name(m, name, &uid); | |
2038 | if (r == -ESRCH) | |
cb31470f ZJS |
2039 | return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_DYNAMIC_USER, |
2040 | "Dynamic user %s does not exist.", name); | |
29206d46 LP |
2041 | if (r < 0) |
2042 | return r; | |
2043 | ||
2044 | return sd_bus_reply_method_return(message, "u", (uint32_t) uid); | |
2045 | } | |
2046 | ||
2047 | static int method_lookup_dynamic_user_by_uid(sd_bus_message *message, void *userdata, sd_bus_error *error) { | |
2048 | _cleanup_free_ char *name = NULL; | |
99534007 | 2049 | Manager *m = ASSERT_PTR(userdata); |
29206d46 LP |
2050 | uid_t uid; |
2051 | int r; | |
2052 | ||
2053 | assert(message); | |
29206d46 | 2054 | |
60d0a509 | 2055 | assert_cc(sizeof(uid_t) == sizeof(uint32_t)); |
29206d46 LP |
2056 | r = sd_bus_message_read_basic(message, 'u', &uid); |
2057 | if (r < 0) | |
2058 | return r; | |
2059 | ||
2060 | if (!MANAGER_IS_SYSTEM(m)) | |
cb31470f ZJS |
2061 | return sd_bus_error_setf(error, SD_BUS_ERROR_NOT_SUPPORTED, |
2062 | "Dynamic users are only supported in the system instance."); | |
29206d46 | 2063 | if (!uid_is_valid(uid)) |
cb31470f ZJS |
2064 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, |
2065 | "User ID invalid: " UID_FMT, uid); | |
29206d46 LP |
2066 | |
2067 | r = dynamic_user_lookup_uid(m, uid, &name); | |
2068 | if (r == -ESRCH) | |
cb31470f ZJS |
2069 | return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_DYNAMIC_USER, |
2070 | "Dynamic user ID " UID_FMT " does not exist.", uid); | |
29206d46 LP |
2071 | if (r < 0) |
2072 | return r; | |
2073 | ||
2074 | return sd_bus_reply_method_return(message, "s", name); | |
2075 | } | |
2076 | ||
f9bfa696 YW |
2077 | static int method_get_dynamic_users(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
2078 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; | |
99534007 | 2079 | Manager *m = ASSERT_PTR(userdata); |
f9bfa696 | 2080 | DynamicUser *d; |
f9bfa696 YW |
2081 | int r; |
2082 | ||
2083 | assert(message); | |
f9bfa696 YW |
2084 | |
2085 | assert_cc(sizeof(uid_t) == sizeof(uint32_t)); | |
2086 | ||
2087 | if (!MANAGER_IS_SYSTEM(m)) | |
cb31470f ZJS |
2088 | return sd_bus_error_setf(error, SD_BUS_ERROR_NOT_SUPPORTED, |
2089 | "Dynamic users are only supported in the system instance."); | |
f9bfa696 YW |
2090 | |
2091 | r = sd_bus_message_new_method_return(message, &reply); | |
2092 | if (r < 0) | |
2093 | return r; | |
2094 | ||
2095 | r = sd_bus_message_open_container(reply, 'a', "(us)"); | |
2096 | if (r < 0) | |
2097 | return r; | |
2098 | ||
90e74a66 | 2099 | HASHMAP_FOREACH(d, m->dynamic_users) { |
f9bfa696 YW |
2100 | uid_t uid; |
2101 | ||
2102 | r = dynamic_user_current(d, &uid); | |
2103 | if (r == -EAGAIN) /* not realized yet? */ | |
2104 | continue; | |
2105 | if (r < 0) | |
cb31470f ZJS |
2106 | return sd_bus_error_setf(error, SD_BUS_ERROR_FAILED, |
2107 | "Failed to look up a dynamic user."); | |
f9bfa696 YW |
2108 | |
2109 | r = sd_bus_message_append(reply, "(us)", uid, d->name); | |
2110 | if (r < 0) | |
2111 | return r; | |
2112 | } | |
2113 | ||
2114 | r = sd_bus_message_close_container(reply); | |
2115 | if (r < 0) | |
2116 | return r; | |
2117 | ||
2118 | return sd_bus_send(NULL, reply, NULL); | |
2119 | } | |
2120 | ||
70666e28 | 2121 | static int method_enqueue_marked_jobs(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
99534007 | 2122 | Manager *m = ASSERT_PTR(userdata); |
70666e28 ZJS |
2123 | int r; |
2124 | ||
2125 | assert(message); | |
70666e28 ZJS |
2126 | |
2127 | r = mac_selinux_access_check(message, "start", error); | |
2128 | if (r < 0) | |
2129 | return r; | |
2130 | ||
2131 | r = bus_verify_manage_units_async(m, message, error); | |
2132 | if (r < 0) | |
2133 | return r; | |
2134 | if (r == 0) | |
2135 | return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */ | |
2136 | ||
28e5e1e9 | 2137 | log_info("Queuing reload/restart jobs for marked units%s", special_glyph(SPECIAL_GLYPH_ELLIPSIS)); |
70666e28 ZJS |
2138 | |
2139 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; | |
2140 | r = sd_bus_message_new_method_return(message, &reply); | |
2141 | if (r < 0) | |
2142 | return r; | |
2143 | ||
2144 | r = sd_bus_message_open_container(reply, 'a', "o"); | |
2145 | if (r < 0) | |
2146 | return r; | |
2147 | ||
2148 | Unit *u; | |
2149 | char *k; | |
2150 | int ret = 0; | |
2151 | HASHMAP_FOREACH_KEY(u, k, m->units) { | |
2152 | /* ignore aliases */ | |
2153 | if (u->id != k) | |
2154 | continue; | |
2155 | ||
2156 | BusUnitQueueFlags flags; | |
2157 | if (FLAGS_SET(u->markers, 1u << UNIT_MARKER_NEEDS_RESTART)) | |
2158 | flags = 0; | |
2159 | else if (FLAGS_SET(u->markers, 1u << UNIT_MARKER_NEEDS_RELOAD)) | |
2160 | flags = BUS_UNIT_QUEUE_RELOAD_IF_POSSIBLE; | |
2161 | else | |
2162 | continue; | |
2163 | ||
2164 | r = mac_selinux_unit_access_check(u, message, "start", error); | |
2165 | if (r >= 0) | |
2166 | r = bus_unit_queue_job_one(message, u, | |
2167 | JOB_TRY_RESTART, JOB_FAIL, flags, | |
2168 | reply, error); | |
bb44fd07 ZJS |
2169 | if (ERRNO_IS_NEG_RESOURCE(r)) |
2170 | return r; | |
70666e28 | 2171 | if (r < 0) { |
70666e28 ZJS |
2172 | if (ret >= 0) |
2173 | ret = r; | |
2174 | sd_bus_error_free(error); | |
2175 | } | |
2176 | } | |
2177 | ||
2178 | if (ret < 0) | |
2179 | return sd_bus_error_set_errnof(error, ret, | |
2180 | "Failed to enqueue some jobs, see logs for details: %m"); | |
2181 | ||
2182 | r = sd_bus_message_close_container(reply); | |
2183 | if (r < 0) | |
2184 | return r; | |
2185 | ||
2186 | return sd_bus_send(NULL, reply, NULL); | |
2187 | } | |
2188 | ||
313fe66f | 2189 | static int list_unit_files_by_patterns(sd_bus_message *message, void *userdata, sd_bus_error *error, char **states, char **patterns) { |
99534007 | 2190 | Manager *m = ASSERT_PTR(userdata); |
ca3c95c1 | 2191 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; |
5d2a48da | 2192 | _cleanup_hashmap_free_ Hashmap *h = NULL; |
718db961 LP |
2193 | int r; |
2194 | ||
718db961 | 2195 | assert(message); |
718db961 | 2196 | |
283868e1 SW |
2197 | /* Anyone can call this method */ |
2198 | ||
8a188de9 | 2199 | r = mac_selinux_access_check(message, "status", error); |
ebcf1f97 LP |
2200 | if (r < 0) |
2201 | return r; | |
718db961 | 2202 | |
df2d202e | 2203 | r = sd_bus_message_new_method_return(message, &reply); |
718db961 | 2204 | if (r < 0) |
ebcf1f97 | 2205 | return r; |
c0576cd6 | 2206 | |
ca3c95c1 | 2207 | r = unit_file_get_list(m->runtime_scope, /* root_dir = */ NULL, states, patterns, &h); |
ebcf1f97 | 2208 | if (r < 0) |
066ba68e | 2209 | return r; |
718db961 LP |
2210 | |
2211 | r = sd_bus_message_open_container(reply, 'a', "(ss)"); | |
ebcf1f97 | 2212 | if (r < 0) |
066ba68e | 2213 | return r; |
718db961 | 2214 | |
ca3c95c1 | 2215 | UnitFileList *item; |
90e74a66 | 2216 | HASHMAP_FOREACH(item, h) { |
718db961 | 2217 | r = sd_bus_message_append(reply, "(ss)", item->path, unit_file_state_to_string(item->state)); |
ebcf1f97 | 2218 | if (r < 0) |
066ba68e | 2219 | return r; |
718db961 | 2220 | } |
c0576cd6 | 2221 | |
718db961 LP |
2222 | r = sd_bus_message_close_container(reply); |
2223 | if (r < 0) | |
ebcf1f97 | 2224 | return r; |
8d0e38a2 | 2225 | |
9030ca46 | 2226 | return sd_bus_send(NULL, reply, NULL); |
718db961 | 2227 | } |
99504dd4 | 2228 | |
313fe66f | 2229 | static int method_list_unit_files(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
2230 | return list_unit_files_by_patterns(message, userdata, error, NULL, NULL); | |
2231 | } | |
2232 | ||
2233 | static int method_list_unit_files_by_patterns(sd_bus_message *message, void *userdata, sd_bus_error *error) { | |
2234 | _cleanup_strv_free_ char **states = NULL; | |
2235 | _cleanup_strv_free_ char **patterns = NULL; | |
2236 | int r; | |
2237 | ||
2238 | r = sd_bus_message_read_strv(message, &states); | |
2239 | if (r < 0) | |
2240 | return r; | |
2241 | ||
2242 | r = sd_bus_message_read_strv(message, &patterns); | |
2243 | if (r < 0) | |
2244 | return r; | |
2245 | ||
2246 | return list_unit_files_by_patterns(message, userdata, error, states, patterns); | |
2247 | } | |
2248 | ||
19070062 | 2249 | static int method_get_unit_file_state(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
99534007 | 2250 | Manager *m = ASSERT_PTR(userdata); |
718db961 LP |
2251 | const char *name; |
2252 | UnitFileState state; | |
718db961 | 2253 | int r; |
99504dd4 | 2254 | |
718db961 | 2255 | assert(message); |
8e2af478 | 2256 | |
283868e1 SW |
2257 | /* Anyone can call this method */ |
2258 | ||
8a188de9 | 2259 | r = mac_selinux_access_check(message, "status", error); |
ebcf1f97 LP |
2260 | if (r < 0) |
2261 | return r; | |
8e2af478 | 2262 | |
718db961 LP |
2263 | r = sd_bus_message_read(message, "s", &name); |
2264 | if (r < 0) | |
ebcf1f97 | 2265 | return r; |
8e2af478 | 2266 | |
4870133b | 2267 | r = unit_file_get_state(m->runtime_scope, NULL, name, &state); |
0ec0deaa LP |
2268 | if (r < 0) |
2269 | return r; | |
8e2af478 | 2270 | |
df2d202e | 2271 | return sd_bus_reply_method_return(message, "s", unit_file_state_to_string(state)); |
718db961 | 2272 | } |
8e2af478 | 2273 | |
19070062 | 2274 | static int method_get_default_target(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
718db961 | 2275 | _cleanup_free_ char *default_target = NULL; |
99534007 | 2276 | Manager *m = ASSERT_PTR(userdata); |
718db961 | 2277 | int r; |
c2756a68 | 2278 | |
718db961 | 2279 | assert(message); |
c2756a68 | 2280 | |
283868e1 SW |
2281 | /* Anyone can call this method */ |
2282 | ||
8a188de9 | 2283 | r = mac_selinux_access_check(message, "status", error); |
ebcf1f97 LP |
2284 | if (r < 0) |
2285 | return r; | |
c2756a68 | 2286 | |
4870133b | 2287 | r = unit_file_get_default(m->runtime_scope, NULL, &default_target); |
7c78a193 DT |
2288 | if (r == -ERFKILL) |
2289 | sd_bus_error_setf(error, BUS_ERROR_UNIT_MASKED, "Unit file is masked."); | |
718db961 | 2290 | if (r < 0) |
ebcf1f97 | 2291 | return r; |
c2756a68 | 2292 | |
df2d202e | 2293 | return sd_bus_reply_method_return(message, "s", default_target); |
718db961 | 2294 | } |
c2756a68 | 2295 | |
8f8f05a9 | 2296 | static int send_unit_files_changed(sd_bus *bus, void *userdata) { |
4afd3348 | 2297 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *message = NULL; |
718db961 LP |
2298 | int r; |
2299 | ||
2300 | assert(bus); | |
2301 | ||
cb31470f ZJS |
2302 | r = sd_bus_message_new_signal(bus, &message, |
2303 | "/org/freedesktop/systemd1", | |
2304 | "org.freedesktop.systemd1.Manager", | |
2305 | "UnitFilesChanged"); | |
718db961 LP |
2306 | if (r < 0) |
2307 | return r; | |
2308 | ||
8f8f05a9 | 2309 | return sd_bus_send(bus, message, NULL); |
718db961 LP |
2310 | } |
2311 | ||
d69cba3b MY |
2312 | static void manager_unit_files_changed(Manager *m, const InstallChange *changes, size_t n_changes) { |
2313 | int r; | |
2314 | ||
2315 | assert(m); | |
2316 | assert(changes || n_changes == 0); | |
2317 | ||
2318 | if (!install_changes_have_modification(changes, n_changes)) | |
2319 | return; | |
2320 | ||
2321 | /* See comments for this variable in manager.h */ | |
2322 | m->unit_file_state_outdated = true; | |
2323 | ||
2324 | r = bus_foreach_bus(m, NULL, send_unit_files_changed, NULL); | |
2325 | if (r < 0) | |
2326 | log_debug_errno(r, "Failed to send UnitFilesChanged signal, ignoring: %m"); | |
2327 | } | |
2328 | ||
af3d8113 ZJS |
2329 | static int install_error( |
2330 | sd_bus_error *error, | |
2331 | int c, | |
cd44ec5a | 2332 | InstallChange *changes, |
da6053d0 LP |
2333 | size_t n_changes) { |
2334 | ||
d41f08bd MY |
2335 | int r; |
2336 | ||
2337 | /* Create an error reply, using the error information from changes[] if possible, and fall back to | |
2338 | * generating an error from error code c. The error message only describes the first error. */ | |
2339 | ||
2340 | assert(changes || n_changes == 0); | |
2341 | ||
f8888b9a | 2342 | CLEANUP_ARRAY(changes, n_changes, install_changes_free); |
8e20adca | 2343 | |
d41f08bd MY |
2344 | FOREACH_ARRAY(i, changes, n_changes) { |
2345 | _cleanup_free_ char *err_message = NULL; | |
2346 | const char *bus_error; | |
2347 | ||
2348 | if (i->type >= 0) | |
2349 | continue; | |
637d6e5b | 2350 | |
d41f08bd MY |
2351 | r = install_change_dump_error(i, &err_message, &bus_error); |
2352 | if (r == -ENOMEM) | |
2353 | return r; | |
2354 | if (r < 0) | |
2355 | return sd_bus_error_set_errnof(error, r, "File %s: %m", i->path); | |
c89d0c3b | 2356 | |
d41f08bd MY |
2357 | return sd_bus_error_set(error, bus_error, err_message); |
2358 | } | |
8e20adca | 2359 | |
f8888b9a | 2360 | return c < 0 ? c : -EINVAL; |
8e20adca LP |
2361 | } |
2362 | ||
cd44ec5a | 2363 | static int reply_install_changes_and_free( |
637d6e5b LP |
2364 | Manager *m, |
2365 | sd_bus_message *message, | |
2366 | int carries_install_info, | |
cd44ec5a | 2367 | InstallChange *changes, |
da6053d0 | 2368 | size_t n_changes, |
637d6e5b LP |
2369 | sd_bus_error *error) { |
2370 | ||
2371 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; | |
2372 | bool bad = false, good = false; | |
637d6e5b LP |
2373 | int r; |
2374 | ||
8c2d0d3a LP |
2375 | CLEANUP_ARRAY(changes, n_changes, install_changes_free); |
2376 | ||
637d6e5b LP |
2377 | r = sd_bus_message_new_method_return(message, &reply); |
2378 | if (r < 0) | |
8c2d0d3a | 2379 | return r; |
637d6e5b LP |
2380 | |
2381 | if (carries_install_info >= 0) { | |
2382 | r = sd_bus_message_append(reply, "b", carries_install_info); | |
2383 | if (r < 0) | |
8c2d0d3a | 2384 | return r; |
637d6e5b LP |
2385 | } |
2386 | ||
2387 | r = sd_bus_message_open_container(reply, 'a', "(sss)"); | |
2388 | if (r < 0) | |
8c2d0d3a | 2389 | return r; |
637d6e5b | 2390 | |
d41f08bd MY |
2391 | FOREACH_ARRAY(i, changes, n_changes) { |
2392 | if (i->type < 0) { | |
637d6e5b LP |
2393 | bad = true; |
2394 | continue; | |
2395 | } | |
2396 | ||
2397 | r = sd_bus_message_append( | |
2398 | reply, "(sss)", | |
d41f08bd MY |
2399 | install_change_type_to_string(i->type), |
2400 | i->path, | |
2401 | i->source); | |
637d6e5b | 2402 | if (r < 0) |
8c2d0d3a | 2403 | return r; |
637d6e5b LP |
2404 | |
2405 | good = true; | |
2406 | } | |
2407 | ||
cb31470f ZJS |
2408 | /* If there was a failed change, and no successful change, then return the first failure as proper |
2409 | * method call error. */ | |
637d6e5b | 2410 | if (bad && !good) |
8c2d0d3a | 2411 | return install_error(error, 0, TAKE_PTR(changes), n_changes); |
637d6e5b LP |
2412 | |
2413 | r = sd_bus_message_close_container(reply); | |
2414 | if (r < 0) | |
8c2d0d3a | 2415 | return r; |
637d6e5b | 2416 | |
637d6e5b | 2417 | return sd_bus_send(NULL, reply, NULL); |
637d6e5b LP |
2418 | } |
2419 | ||
718db961 | 2420 | static int method_enable_unit_files_generic( |
718db961 | 2421 | sd_bus_message *message, |
d309c1c3 | 2422 | Manager *m, |
e9e77e44 | 2423 | int (*call)(RuntimeScope scope, UnitFileFlags flags, const char *root_dir, char * const *files, InstallChange **changes, size_t *n_changes), |
ebcf1f97 LP |
2424 | bool carries_install_info, |
2425 | sd_bus_error *error) { | |
718db961 LP |
2426 | |
2427 | _cleanup_strv_free_ char **l = NULL; | |
cd44ec5a | 2428 | InstallChange *changes = NULL; |
da6053d0 | 2429 | size_t n_changes = 0; |
b3796dd8 | 2430 | UnitFileFlags flags; |
83654007 | 2431 | int r; |
718db961 | 2432 | |
718db961 LP |
2433 | assert(message); |
2434 | assert(m); | |
cad45ba1 | 2435 | |
718db961 LP |
2436 | r = sd_bus_message_read_strv(message, &l); |
2437 | if (r < 0) | |
ebcf1f97 | 2438 | return r; |
90bb85e1 | 2439 | |
83654007 LB |
2440 | if (sd_bus_message_is_method_call(message, NULL, "EnableUnitFilesWithFlags")) { |
2441 | uint64_t raw_flags; | |
d309c1c3 | 2442 | |
83654007 LB |
2443 | r = sd_bus_message_read(message, "t", &raw_flags); |
2444 | if (r < 0) | |
2445 | return r; | |
2446 | if ((raw_flags & ~_UNIT_FILE_FLAGS_MASK_PUBLIC) != 0) | |
2447 | return -EINVAL; | |
2448 | flags = raw_flags; | |
2449 | } else { | |
2450 | int runtime, force; | |
2451 | ||
2452 | r = sd_bus_message_read(message, "bb", &runtime, &force); | |
2453 | if (r < 0) | |
2454 | return r; | |
2455 | flags = unit_file_bools_to_flags(runtime, force); | |
2456 | } | |
b3796dd8 | 2457 | |
1d22e906 LP |
2458 | r = bus_verify_manage_unit_files_async(m, message, error); |
2459 | if (r < 0) | |
2460 | return r; | |
2461 | if (r == 0) | |
2462 | return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */ | |
2463 | ||
4870133b | 2464 | r = call(m->runtime_scope, flags, NULL, l, &changes, &n_changes); |
d69cba3b | 2465 | manager_unit_files_changed(m, changes, n_changes); |
af3d8113 ZJS |
2466 | if (r < 0) |
2467 | return install_error(error, r, changes, n_changes); | |
718db961 | 2468 | |
cd44ec5a | 2469 | return reply_install_changes_and_free(m, message, carries_install_info ? r : -1, changes, n_changes, error); |
718db961 | 2470 | } |
c87eba54 | 2471 | |
83654007 | 2472 | static int method_enable_unit_files_with_flags(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
2d8cc7a7 | 2473 | return method_enable_unit_files_generic(message, userdata, unit_file_enable, /* carries_install_info = */ true, error); |
83654007 LB |
2474 | } |
2475 | ||
19070062 | 2476 | static int method_enable_unit_files(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
2d8cc7a7 | 2477 | return method_enable_unit_files_generic(message, userdata, unit_file_enable, /* carries_install_info = */ true, error); |
718db961 | 2478 | } |
ea430986 | 2479 | |
19070062 | 2480 | static int method_reenable_unit_files(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
2d8cc7a7 | 2481 | return method_enable_unit_files_generic(message, userdata, unit_file_reenable, /* carries_install_info = */ true, error); |
718db961 LP |
2482 | } |
2483 | ||
19070062 | 2484 | static int method_link_unit_files(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
2d8cc7a7 | 2485 | return method_enable_unit_files_generic(message, userdata, unit_file_link, /* carries_install_info = */ false, error); |
718db961 LP |
2486 | } |
2487 | ||
e9e77e44 | 2488 | static int unit_file_preset_without_mode(RuntimeScope scope, UnitFileFlags flags, const char *root_dir, char * const *files, InstallChange **changes, size_t *n_changes) { |
b3796dd8 | 2489 | return unit_file_preset(scope, flags, root_dir, files, UNIT_FILE_PRESET_FULL, changes, n_changes); |
d309c1c3 LP |
2490 | } |
2491 | ||
19070062 | 2492 | static int method_preset_unit_files(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
2d8cc7a7 | 2493 | return method_enable_unit_files_generic(message, userdata, unit_file_preset_without_mode, /* carries_install_info = */ true, error); |
718db961 LP |
2494 | } |
2495 | ||
19070062 | 2496 | static int method_mask_unit_files(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
2d8cc7a7 | 2497 | return method_enable_unit_files_generic(message, userdata, unit_file_mask, /* carries_install_info = */ false, error); |
718db961 | 2498 | } |
ea430986 | 2499 | |
19070062 | 2500 | static int method_preset_unit_files_with_mode(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
d309c1c3 LP |
2501 | |
2502 | _cleanup_strv_free_ char **l = NULL; | |
cd44ec5a | 2503 | InstallChange *changes = NULL; |
da6053d0 | 2504 | size_t n_changes = 0; |
99534007 | 2505 | Manager *m = ASSERT_PTR(userdata); |
8331b221 | 2506 | UnitFilePresetMode preset_mode; |
d309c1c3 | 2507 | int runtime, force, r; |
b3796dd8 | 2508 | UnitFileFlags flags; |
d309c1c3 LP |
2509 | const char *mode; |
2510 | ||
d309c1c3 | 2511 | assert(message); |
d309c1c3 LP |
2512 | |
2513 | r = sd_bus_message_read_strv(message, &l); | |
2514 | if (r < 0) | |
2515 | return r; | |
2516 | ||
2517 | r = sd_bus_message_read(message, "sbb", &mode, &runtime, &force); | |
2518 | if (r < 0) | |
2519 | return r; | |
2520 | ||
b3796dd8 JS |
2521 | flags = unit_file_bools_to_flags(runtime, force); |
2522 | ||
d309c1c3 | 2523 | if (isempty(mode)) |
8331b221 | 2524 | preset_mode = UNIT_FILE_PRESET_FULL; |
d309c1c3 | 2525 | else { |
8331b221 ZJS |
2526 | preset_mode = unit_file_preset_mode_from_string(mode); |
2527 | if (preset_mode < 0) | |
d309c1c3 LP |
2528 | return -EINVAL; |
2529 | } | |
2530 | ||
1d22e906 LP |
2531 | r = bus_verify_manage_unit_files_async(m, message, error); |
2532 | if (r < 0) | |
2533 | return r; | |
2534 | if (r == 0) | |
2535 | return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */ | |
2536 | ||
4870133b | 2537 | r = unit_file_preset(m->runtime_scope, flags, NULL, l, preset_mode, &changes, &n_changes); |
d69cba3b | 2538 | manager_unit_files_changed(m, changes, n_changes); |
af3d8113 ZJS |
2539 | if (r < 0) |
2540 | return install_error(error, r, changes, n_changes); | |
d309c1c3 | 2541 | |
cd44ec5a | 2542 | return reply_install_changes_and_free(m, message, r, changes, n_changes, error); |
d309c1c3 LP |
2543 | } |
2544 | ||
718db961 | 2545 | static int method_disable_unit_files_generic( |
718db961 | 2546 | sd_bus_message *message, |
596fc263 | 2547 | Manager *m, |
e9e77e44 | 2548 | int (*call)(RuntimeScope scope, UnitFileFlags flags, const char *root_dir, char * const *files, InstallChange **changes, size_t *n_changes), |
bf1bea43 | 2549 | bool carries_install_info, |
ebcf1f97 | 2550 | sd_bus_error *error) { |
718db961 LP |
2551 | |
2552 | _cleanup_strv_free_ char **l = NULL; | |
cd44ec5a | 2553 | InstallChange *changes = NULL; |
83654007 | 2554 | UnitFileFlags flags; |
da6053d0 | 2555 | size_t n_changes = 0; |
83654007 | 2556 | int r; |
718db961 | 2557 | |
718db961 LP |
2558 | assert(message); |
2559 | assert(m); | |
2560 | ||
df823e23 | 2561 | r = sd_bus_message_read_strv(message, &l); |
ebcf1f97 LP |
2562 | if (r < 0) |
2563 | return r; | |
718db961 | 2564 | |
bf1bea43 MY |
2565 | if (sd_bus_message_is_method_call(message, NULL, "DisableUnitFilesWithFlags") || |
2566 | sd_bus_message_is_method_call(message, NULL, "DisableUnitFilesWithFlagsAndInstallInfo")) { | |
83654007 LB |
2567 | uint64_t raw_flags; |
2568 | ||
2569 | r = sd_bus_message_read(message, "t", &raw_flags); | |
2570 | if (r < 0) | |
2571 | return r; | |
2572 | if ((raw_flags & ~_UNIT_FILE_FLAGS_MASK_PUBLIC) != 0 || | |
2573 | FLAGS_SET(raw_flags, UNIT_FILE_FORCE)) | |
2574 | return -EINVAL; | |
2575 | flags = raw_flags; | |
2576 | } else { | |
2577 | int runtime; | |
2578 | ||
2579 | r = sd_bus_message_read(message, "b", &runtime); | |
2580 | if (r < 0) | |
2581 | return r; | |
2582 | flags = unit_file_bools_to_flags(runtime, false); | |
2583 | } | |
718db961 | 2584 | |
1d22e906 LP |
2585 | r = bus_verify_manage_unit_files_async(m, message, error); |
2586 | if (r < 0) | |
2587 | return r; | |
2588 | if (r == 0) | |
2589 | return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */ | |
2590 | ||
4870133b | 2591 | r = call(m->runtime_scope, flags, NULL, l, &changes, &n_changes); |
d69cba3b | 2592 | manager_unit_files_changed(m, changes, n_changes); |
af3d8113 ZJS |
2593 | if (r < 0) |
2594 | return install_error(error, r, changes, n_changes); | |
718db961 | 2595 | |
bf1bea43 | 2596 | return reply_install_changes_and_free(m, message, carries_install_info ? r : -1, changes, n_changes, error); |
718db961 | 2597 | } |
ea430986 | 2598 | |
83654007 | 2599 | static int method_disable_unit_files_with_flags(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
bf1bea43 MY |
2600 | return method_disable_unit_files_generic(message, userdata, unit_file_disable, /* carries_install_info = */ false, error); |
2601 | } | |
2602 | ||
2603 | static int method_disable_unit_files_with_flags_and_install_info(sd_bus_message *message, void *userdata, sd_bus_error *error) { | |
2604 | return method_disable_unit_files_generic(message, userdata, unit_file_disable, /* carries_install_info = */ true, error); | |
83654007 LB |
2605 | } |
2606 | ||
19070062 | 2607 | static int method_disable_unit_files(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
bf1bea43 | 2608 | return method_disable_unit_files_generic(message, userdata, unit_file_disable, /* carries_install_info = */ false, error); |
ea430986 LP |
2609 | } |
2610 | ||
19070062 | 2611 | static int method_unmask_unit_files(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
bf1bea43 | 2612 | return method_disable_unit_files_generic(message, userdata, unit_file_unmask, /* carries_install_info = */ false, error); |
718db961 LP |
2613 | } |
2614 | ||
344ca755 LP |
2615 | static int method_revert_unit_files(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
2616 | _cleanup_strv_free_ char **l = NULL; | |
cd44ec5a | 2617 | InstallChange *changes = NULL; |
da6053d0 | 2618 | size_t n_changes = 0; |
99534007 | 2619 | Manager *m = ASSERT_PTR(userdata); |
344ca755 LP |
2620 | int r; |
2621 | ||
2622 | assert(message); | |
344ca755 LP |
2623 | |
2624 | r = sd_bus_message_read_strv(message, &l); | |
2625 | if (r < 0) | |
2626 | return r; | |
2627 | ||
2628 | r = bus_verify_manage_unit_files_async(m, message, error); | |
2629 | if (r < 0) | |
2630 | return r; | |
2631 | if (r == 0) | |
2632 | return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */ | |
2633 | ||
4870133b | 2634 | r = unit_file_revert(m->runtime_scope, NULL, l, &changes, &n_changes); |
d69cba3b | 2635 | manager_unit_files_changed(m, changes, n_changes); |
af3d8113 ZJS |
2636 | if (r < 0) |
2637 | return install_error(error, r, changes, n_changes); | |
344ca755 | 2638 | |
cd44ec5a | 2639 | return reply_install_changes_and_free(m, message, -1, changes, n_changes, error); |
344ca755 LP |
2640 | } |
2641 | ||
19070062 | 2642 | static int method_set_default_target(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
cd44ec5a | 2643 | InstallChange *changes = NULL; |
da6053d0 | 2644 | size_t n_changes = 0; |
99534007 | 2645 | Manager *m = ASSERT_PTR(userdata); |
718db961 LP |
2646 | const char *name; |
2647 | int force, r; | |
2648 | ||
718db961 | 2649 | assert(message); |
718db961 | 2650 | |
1d22e906 | 2651 | r = mac_selinux_access_check(message, "enable", error); |
283868e1 SW |
2652 | if (r < 0) |
2653 | return r; | |
283868e1 | 2654 | |
1d22e906 | 2655 | r = sd_bus_message_read(message, "sb", &name, &force); |
ebcf1f97 LP |
2656 | if (r < 0) |
2657 | return r; | |
718db961 | 2658 | |
1d22e906 | 2659 | r = bus_verify_manage_unit_files_async(m, message, error); |
718db961 | 2660 | if (r < 0) |
ebcf1f97 | 2661 | return r; |
1d22e906 LP |
2662 | if (r == 0) |
2663 | return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */ | |
718db961 | 2664 | |
4870133b | 2665 | r = unit_file_set_default(m->runtime_scope, force ? UNIT_FILE_FORCE : 0, NULL, name, &changes, &n_changes); |
d69cba3b | 2666 | manager_unit_files_changed(m, changes, n_changes); |
af3d8113 ZJS |
2667 | if (r < 0) |
2668 | return install_error(error, r, changes, n_changes); | |
718db961 | 2669 | |
cd44ec5a | 2670 | return reply_install_changes_and_free(m, message, -1, changes, n_changes, error); |
718db961 LP |
2671 | } |
2672 | ||
19070062 | 2673 | static int method_preset_all_unit_files(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
cd44ec5a | 2674 | InstallChange *changes = NULL; |
da6053d0 | 2675 | size_t n_changes = 0; |
99534007 | 2676 | Manager *m = ASSERT_PTR(userdata); |
8331b221 | 2677 | UnitFilePresetMode preset_mode; |
d309c1c3 | 2678 | const char *mode; |
b3796dd8 | 2679 | UnitFileFlags flags; |
d309c1c3 LP |
2680 | int force, runtime, r; |
2681 | ||
d309c1c3 | 2682 | assert(message); |
d309c1c3 | 2683 | |
8a188de9 | 2684 | r = mac_selinux_access_check(message, "enable", error); |
d309c1c3 LP |
2685 | if (r < 0) |
2686 | return r; | |
2687 | ||
2688 | r = sd_bus_message_read(message, "sbb", &mode, &runtime, &force); | |
2689 | if (r < 0) | |
2690 | return r; | |
2691 | ||
b3796dd8 JS |
2692 | flags = unit_file_bools_to_flags(runtime, force); |
2693 | ||
d309c1c3 | 2694 | if (isempty(mode)) |
8331b221 | 2695 | preset_mode = UNIT_FILE_PRESET_FULL; |
d309c1c3 | 2696 | else { |
8331b221 ZJS |
2697 | preset_mode = unit_file_preset_mode_from_string(mode); |
2698 | if (preset_mode < 0) | |
d309c1c3 LP |
2699 | return -EINVAL; |
2700 | } | |
2701 | ||
1d22e906 LP |
2702 | r = bus_verify_manage_unit_files_async(m, message, error); |
2703 | if (r < 0) | |
2704 | return r; | |
2705 | if (r == 0) | |
2706 | return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */ | |
2707 | ||
4870133b | 2708 | r = unit_file_preset_all(m->runtime_scope, flags, NULL, preset_mode, &changes, &n_changes); |
d69cba3b | 2709 | manager_unit_files_changed(m, changes, n_changes); |
af3d8113 ZJS |
2710 | if (r < 0) |
2711 | return install_error(error, r, changes, n_changes); | |
d309c1c3 | 2712 | |
cd44ec5a | 2713 | return reply_install_changes_and_free(m, message, -1, changes, n_changes, error); |
d309c1c3 LP |
2714 | } |
2715 | ||
19070062 | 2716 | static int method_add_dependency_unit_files(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
e94937df | 2717 | _cleanup_strv_free_ char **l = NULL; |
99534007 | 2718 | Manager *m = ASSERT_PTR(userdata); |
cd44ec5a | 2719 | InstallChange *changes = NULL; |
da6053d0 | 2720 | size_t n_changes = 0; |
e94937df | 2721 | int runtime, force, r; |
596fc263 | 2722 | char *target, *type; |
e94937df | 2723 | UnitDependency dep; |
b3796dd8 | 2724 | UnitFileFlags flags; |
e94937df | 2725 | |
e94937df | 2726 | assert(message); |
e94937df LN |
2727 | |
2728 | r = bus_verify_manage_unit_files_async(m, message, error); | |
2729 | if (r < 0) | |
2730 | return r; | |
2731 | if (r == 0) | |
2732 | return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */ | |
2733 | ||
2734 | r = sd_bus_message_read_strv(message, &l); | |
2735 | if (r < 0) | |
2736 | return r; | |
2737 | ||
2738 | r = sd_bus_message_read(message, "ssbb", &target, &type, &runtime, &force); | |
2739 | if (r < 0) | |
2740 | return r; | |
2741 | ||
b3796dd8 JS |
2742 | flags = unit_file_bools_to_flags(runtime, force); |
2743 | ||
e94937df LN |
2744 | dep = unit_dependency_from_string(type); |
2745 | if (dep < 0) | |
2746 | return -EINVAL; | |
2747 | ||
4870133b | 2748 | r = unit_file_add_dependency(m->runtime_scope, flags, NULL, l, target, dep, &changes, &n_changes); |
d69cba3b | 2749 | manager_unit_files_changed(m, changes, n_changes); |
af3d8113 ZJS |
2750 | if (r < 0) |
2751 | return install_error(error, r, changes, n_changes); | |
e94937df | 2752 | |
cd44ec5a | 2753 | return reply_install_changes_and_free(m, message, -1, changes, n_changes, error); |
e94937df LN |
2754 | } |
2755 | ||
3b3557c4 JS |
2756 | static int method_get_unit_file_links(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
2757 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; | |
47c57b48 | 2758 | Manager *m = ASSERT_PTR(userdata); |
cd44ec5a | 2759 | InstallChange *changes = NULL; |
da6053d0 | 2760 | size_t n_changes = 0, i; |
3b3557c4 | 2761 | const char *name; |
3b3557c4 JS |
2762 | int runtime, r; |
2763 | ||
2a711edd DT |
2764 | CLEANUP_ARRAY(changes, n_changes, install_changes_free); |
2765 | ||
3b3557c4 JS |
2766 | r = sd_bus_message_read(message, "sb", &name, &runtime); |
2767 | if (r < 0) | |
2768 | return r; | |
2769 | ||
2770 | r = sd_bus_message_new_method_return(message, &reply); | |
2771 | if (r < 0) | |
2772 | return r; | |
2773 | ||
2774 | r = sd_bus_message_open_container(reply, SD_BUS_TYPE_ARRAY, "s"); | |
2775 | if (r < 0) | |
2776 | return r; | |
2777 | ||
4870133b | 2778 | r = unit_file_disable(m->runtime_scope, |
47c57b48 YW |
2779 | UNIT_FILE_DRY_RUN | (runtime ? UNIT_FILE_RUNTIME : 0), |
2780 | NULL, STRV_MAKE(name), &changes, &n_changes); | |
2a711edd DT |
2781 | if (r < 0) |
2782 | return log_error_errno(r, "Failed to get file links for %s: %m", name); | |
3b3557c4 JS |
2783 | |
2784 | for (i = 0; i < n_changes; i++) | |
f8662fee | 2785 | if (changes[i].type == INSTALL_CHANGE_UNLINK) { |
3b3557c4 JS |
2786 | r = sd_bus_message_append(reply, "s", changes[i].path); |
2787 | if (r < 0) | |
2a711edd | 2788 | return r; |
3b3557c4 JS |
2789 | } |
2790 | ||
2791 | r = sd_bus_message_close_container(reply); | |
2792 | if (r < 0) | |
2a711edd | 2793 | return r; |
a12ba535 | 2794 | |
2a711edd | 2795 | return sd_bus_send(NULL, reply, NULL); |
3b3557c4 JS |
2796 | } |
2797 | ||
15ea79f8 | 2798 | static int method_get_job_waiting(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
99534007 | 2799 | Manager *m = ASSERT_PTR(userdata); |
15ea79f8 LP |
2800 | uint32_t id; |
2801 | Job *j; | |
2802 | int r; | |
2803 | ||
2804 | assert(message); | |
15ea79f8 LP |
2805 | |
2806 | r = sd_bus_message_read(message, "u", &id); | |
2807 | if (r < 0) | |
2808 | return r; | |
2809 | ||
2810 | j = manager_get_job(m, id); | |
2811 | if (!j) | |
2812 | return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_JOB, "Job %u does not exist.", (unsigned) id); | |
2813 | ||
2814 | return bus_job_method_get_waiting_jobs(message, j, error); | |
2815 | } | |
2816 | ||
c20076a8 | 2817 | static int method_abandon_scope(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
99534007 | 2818 | Manager *m = ASSERT_PTR(userdata); |
c20076a8 LP |
2819 | const char *name; |
2820 | Unit *u; | |
2821 | int r; | |
2822 | ||
2823 | assert(message); | |
c20076a8 LP |
2824 | |
2825 | r = sd_bus_message_read(message, "s", &name); | |
2826 | if (r < 0) | |
2827 | return r; | |
2828 | ||
2829 | r = bus_get_unit_by_name(m, message, name, &u, error); | |
2830 | if (r < 0) | |
2831 | return r; | |
2832 | ||
2833 | if (u->type != UNIT_SCOPE) | |
cb31470f ZJS |
2834 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, |
2835 | "Unit '%s' is not a scope unit, refusing.", name); | |
c20076a8 LP |
2836 | |
2837 | return bus_scope_method_abandon(message, u, error); | |
2838 | } | |
2839 | ||
0bb007f7 | 2840 | static int method_set_show_status(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
99534007 | 2841 | Manager *m = ASSERT_PTR(userdata); |
0bb007f7 FB |
2842 | ShowStatus mode = _SHOW_STATUS_INVALID; |
2843 | const char *t; | |
2844 | int r; | |
2845 | ||
0bb007f7 FB |
2846 | assert(message); |
2847 | ||
4db62561 ZJS |
2848 | r = mac_selinux_access_check(message, "reload", error); |
2849 | if (r < 0) | |
2850 | return r; | |
2851 | ||
2852 | r = bus_verify_set_environment_async(m, message, error); | |
2853 | if (r < 0) | |
2854 | return r; | |
2855 | if (r == 0) | |
2856 | return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */ | |
2857 | ||
0bb007f7 FB |
2858 | r = sd_bus_message_read(message, "s", &t); |
2859 | if (r < 0) | |
2860 | return r; | |
2861 | ||
2862 | if (!isempty(t)) { | |
2863 | mode = show_status_from_string(t); | |
2864 | if (mode < 0) | |
cb31470f ZJS |
2865 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, |
2866 | "Invalid show status '%s'", t); | |
0bb007f7 FB |
2867 | } |
2868 | ||
43bba15a | 2869 | manager_override_show_status(m, mode, "bus"); |
0bb007f7 FB |
2870 | |
2871 | return sd_bus_reply_method_return(message, NULL); | |
2872 | } | |
2873 | ||
2ea24611 LP |
2874 | static int method_dump_unit_descriptor_store(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
2875 | return method_generic_unit_operation(message, userdata, error, bus_service_method_dump_file_descriptor_store, 0); | |
2876 | } | |
2877 | ||
84c01612 | 2878 | static int aux_scope_from_message(Manager *m, sd_bus_message *message, Unit **ret_scope, sd_bus_error *error) { |
4ac08d8a | 2879 | _cleanup_(pidref_done) PidRef sender_pidref = PIDREF_NULL; |
84c01612 MS |
2880 | _cleanup_free_ PidRef *pidrefs = NULL; |
2881 | const char *name; | |
2882 | Unit *from, *scope; | |
2883 | PidRef *main_pid; | |
2884 | CGroupContext *cc; | |
2885 | size_t n_pids = 0; | |
2886 | uint64_t flags; | |
84c01612 MS |
2887 | int r; |
2888 | ||
2889 | assert(ret_scope); | |
2890 | ||
4ac08d8a | 2891 | r = bus_query_sender_pidref(message, &sender_pidref); |
84c01612 MS |
2892 | if (r < 0) |
2893 | return r; | |
2894 | ||
4ac08d8a | 2895 | from = manager_get_unit_by_pidref(m, &sender_pidref); |
84c01612 MS |
2896 | if (!from) |
2897 | return sd_bus_error_set(error, BUS_ERROR_NO_SUCH_UNIT, "Client not member of any unit."); | |
2898 | ||
2899 | if (!IN_SET(from->type, UNIT_SERVICE, UNIT_SCOPE)) | |
2900 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, | |
2901 | "Starting auxiliary scope is supported only for service and scope units, refusing."); | |
2902 | ||
2903 | if (!unit_name_is_valid(from->id, UNIT_NAME_PLAIN)) | |
2904 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, | |
2905 | "Auxiliary scope can be started only for non-template service units and scope units, refusing."); | |
2906 | ||
2907 | r = sd_bus_message_read(message, "s", &name); | |
2908 | if (r < 0) | |
2909 | return r; | |
2910 | ||
2911 | if (!unit_name_is_valid(name, UNIT_NAME_PLAIN)) | |
2912 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, | |
2913 | "Invalid name \"%s\" for auxiliary scope.", name); | |
2914 | ||
2915 | if (unit_name_to_type(name) != UNIT_SCOPE) | |
2916 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, | |
2917 | "Name \"%s\" of auxiliary scope doesn't have .scope suffix.", name); | |
2918 | ||
2919 | main_pid = unit_main_pid(from); | |
2920 | ||
2921 | r = sd_bus_message_enter_container(message, 'a', "h"); | |
2922 | if (r < 0) | |
2923 | return r; | |
2924 | ||
2925 | for (;;) { | |
2926 | _cleanup_(pidref_done) PidRef p = PIDREF_NULL; | |
2927 | Unit *unit; | |
2928 | int fd; | |
2929 | ||
2930 | r = sd_bus_message_read(message, "h", &fd); | |
2931 | if (r < 0) | |
2932 | return r; | |
2933 | if (r == 0) | |
2934 | break; | |
2935 | ||
2936 | r = pidref_set_pidfd(&p, fd); | |
2937 | if (r < 0) { | |
2938 | log_unit_warning_errno(from, r, "Failed to create process reference from PIDFD, ignoring: %m"); | |
2939 | continue; | |
2940 | } | |
2941 | ||
2942 | unit = manager_get_unit_by_pidref(m, &p); | |
2943 | if (!unit) { | |
4e494e6a | 2944 | log_unit_warning(from, "Failed to get unit from PIDFD, ignoring."); |
84c01612 MS |
2945 | continue; |
2946 | } | |
2947 | ||
2948 | if (!streq(unit->id, from->id)) { | |
2949 | log_unit_warning(from, "PID " PID_FMT " is not running in the same service as the calling process, ignoring.", p.pid); | |
2950 | continue; | |
2951 | } | |
2952 | ||
2953 | if (pidref_equal(main_pid, &p)) { | |
2954 | log_unit_warning(from, "Main PID cannot be migrated into auxiliary scope, ignoring."); | |
2955 | continue; | |
2956 | } | |
2957 | ||
2958 | if (!GREEDY_REALLOC(pidrefs, n_pids+1)) | |
2959 | return -ENOMEM; | |
2960 | ||
2961 | pidrefs[n_pids++] = TAKE_PIDREF(p); | |
2962 | } | |
2963 | ||
2964 | if (n_pids == 0) | |
2965 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "No processes can be migrated to auxiliary scope."); | |
2966 | ||
2967 | r = sd_bus_message_exit_container(message); | |
2968 | if (r < 0) | |
2969 | return r; | |
2970 | ||
2971 | r = sd_bus_message_read(message, "t", &flags); | |
2972 | if (r < 0) | |
2973 | return r; | |
2974 | ||
2975 | if (flags != 0) | |
2976 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Flags must be zero."); | |
2977 | ||
2978 | r = manager_load_unit(m, name, NULL, error, &scope); | |
2979 | if (r < 0) | |
2980 | return r; | |
2981 | ||
2982 | if (!unit_is_pristine(scope)) | |
2983 | return sd_bus_error_setf(error, BUS_ERROR_UNIT_EXISTS, | |
2984 | "Unit %s was already loaded or has a fragment file.", name); | |
2985 | ||
2986 | r = unit_set_slice(scope, UNIT_GET_SLICE(from)); | |
2987 | if (r < 0) | |
2988 | return r; | |
2989 | ||
2990 | cc = unit_get_cgroup_context(scope); | |
2991 | ||
2992 | r = cgroup_context_copy(cc, unit_get_cgroup_context(from)); | |
2993 | if (r < 0) | |
2994 | return r; | |
2995 | ||
2996 | r = unit_make_transient(scope); | |
2997 | if (r < 0) | |
2998 | return r; | |
2999 | ||
3000 | r = bus_unit_set_properties(scope, message, UNIT_RUNTIME, true, error); | |
3001 | if (r < 0) | |
3002 | return r; | |
3003 | ||
3004 | FOREACH_ARRAY(p, pidrefs, n_pids) { | |
3005 | r = unit_pid_attachable(scope, p, error); | |
3006 | if (r < 0) | |
3007 | return r; | |
3008 | ||
3009 | r = unit_watch_pidref(scope, p, /* exclusive= */ false); | |
3010 | if (r < 0 && r != -EEXIST) | |
3011 | return r; | |
3012 | } | |
3013 | ||
3014 | /* Now load the missing bits of the unit we just created */ | |
3015 | unit_add_to_load_queue(scope); | |
3016 | manager_dispatch_load_queue(m); | |
3017 | ||
3018 | *ret_scope = TAKE_PTR(scope); | |
3019 | ||
3020 | return 1; | |
3021 | } | |
3022 | ||
3023 | static int method_start_aux_scope(sd_bus_message *message, void *userdata, sd_bus_error *error) { | |
3024 | Manager *m = ASSERT_PTR(userdata); | |
3025 | Unit *u = NULL; /* avoid false maybe-uninitialized warning */ | |
3026 | int r; | |
3027 | ||
3028 | assert(message); | |
3029 | ||
3030 | r = mac_selinux_access_check(message, "start", error); | |
3031 | if (r < 0) | |
3032 | return r; | |
3033 | ||
3034 | r = bus_verify_manage_units_async(m, message, error); | |
3035 | if (r < 0) | |
3036 | return r; | |
3037 | if (r == 0) | |
3038 | return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */ | |
3039 | ||
3040 | r = aux_scope_from_message(m, message, &u, error); | |
3041 | if (r < 0) | |
3042 | return r; | |
3043 | ||
3044 | return bus_unit_queue_job(message, u, JOB_START, JOB_REPLACE, 0, error); | |
3045 | } | |
3046 | ||
718db961 LP |
3047 | const sd_bus_vtable bus_manager_vtable[] = { |
3048 | SD_BUS_VTABLE_START(0), | |
3049 | ||
556089dc LP |
3050 | SD_BUS_PROPERTY("Version", "s", property_get_version, 0, SD_BUS_VTABLE_PROPERTY_CONST), |
3051 | SD_BUS_PROPERTY("Features", "s", property_get_features, 0, SD_BUS_VTABLE_PROPERTY_CONST), | |
3052 | SD_BUS_PROPERTY("Virtualization", "s", property_get_virtualization, 0, SD_BUS_VTABLE_PROPERTY_CONST), | |
1257274a | 3053 | SD_BUS_PROPERTY("ConfidentialVirtualization", "s", property_get_confidential_virtualization, 0, SD_BUS_VTABLE_PROPERTY_CONST), |
7452394e | 3054 | SD_BUS_PROPERTY("Architecture", "s", property_get_architecture, 0, SD_BUS_VTABLE_PROPERTY_CONST), |
556089dc | 3055 | SD_BUS_PROPERTY("Tainted", "s", property_get_tainted, 0, SD_BUS_VTABLE_PROPERTY_CONST), |
9f9f0342 LP |
3056 | BUS_PROPERTY_DUAL_TIMESTAMP("FirmwareTimestamp", offsetof(Manager, timestamps[MANAGER_TIMESTAMP_FIRMWARE]), SD_BUS_VTABLE_PROPERTY_CONST), |
3057 | BUS_PROPERTY_DUAL_TIMESTAMP("LoaderTimestamp", offsetof(Manager, timestamps[MANAGER_TIMESTAMP_LOADER]), SD_BUS_VTABLE_PROPERTY_CONST), | |
3058 | BUS_PROPERTY_DUAL_TIMESTAMP("KernelTimestamp", offsetof(Manager, timestamps[MANAGER_TIMESTAMP_KERNEL]), SD_BUS_VTABLE_PROPERTY_CONST), | |
3059 | BUS_PROPERTY_DUAL_TIMESTAMP("InitRDTimestamp", offsetof(Manager, timestamps[MANAGER_TIMESTAMP_INITRD]), SD_BUS_VTABLE_PROPERTY_CONST), | |
3060 | BUS_PROPERTY_DUAL_TIMESTAMP("UserspaceTimestamp", offsetof(Manager, timestamps[MANAGER_TIMESTAMP_USERSPACE]), SD_BUS_VTABLE_PROPERTY_CONST), | |
3061 | BUS_PROPERTY_DUAL_TIMESTAMP("FinishTimestamp", offsetof(Manager, timestamps[MANAGER_TIMESTAMP_FINISH]), SD_BUS_VTABLE_PROPERTY_CONST), | |
b3f54861 | 3062 | BUS_PROPERTY_DUAL_TIMESTAMP("ShutdownStartTimestamp", offsetof(Manager, timestamps[MANAGER_TIMESTAMP_SHUTDOWN_START]), SD_BUS_VTABLE_PROPERTY_CONST), |
9f9f0342 LP |
3063 | BUS_PROPERTY_DUAL_TIMESTAMP("SecurityStartTimestamp", offsetof(Manager, timestamps[MANAGER_TIMESTAMP_SECURITY_START]), SD_BUS_VTABLE_PROPERTY_CONST), |
3064 | BUS_PROPERTY_DUAL_TIMESTAMP("SecurityFinishTimestamp", offsetof(Manager, timestamps[MANAGER_TIMESTAMP_SECURITY_FINISH]), SD_BUS_VTABLE_PROPERTY_CONST), | |
3065 | BUS_PROPERTY_DUAL_TIMESTAMP("GeneratorsStartTimestamp", offsetof(Manager, timestamps[MANAGER_TIMESTAMP_GENERATORS_START]), SD_BUS_VTABLE_PROPERTY_CONST), | |
3066 | BUS_PROPERTY_DUAL_TIMESTAMP("GeneratorsFinishTimestamp", offsetof(Manager, timestamps[MANAGER_TIMESTAMP_GENERATORS_FINISH]), SD_BUS_VTABLE_PROPERTY_CONST), | |
3067 | BUS_PROPERTY_DUAL_TIMESTAMP("UnitsLoadStartTimestamp", offsetof(Manager, timestamps[MANAGER_TIMESTAMP_UNITS_LOAD_START]), SD_BUS_VTABLE_PROPERTY_CONST), | |
3068 | BUS_PROPERTY_DUAL_TIMESTAMP("UnitsLoadFinishTimestamp", offsetof(Manager, timestamps[MANAGER_TIMESTAMP_UNITS_LOAD_FINISH]), SD_BUS_VTABLE_PROPERTY_CONST), | |
49fbe940 | 3069 | BUS_PROPERTY_DUAL_TIMESTAMP("UnitsLoadTimestamp", offsetof(Manager, timestamps[MANAGER_TIMESTAMP_UNITS_LOAD]), SD_BUS_VTABLE_PROPERTY_CONST), |
665a9774 YW |
3070 | BUS_PROPERTY_DUAL_TIMESTAMP("InitRDSecurityStartTimestamp", offsetof(Manager, timestamps[MANAGER_TIMESTAMP_INITRD_SECURITY_START]), SD_BUS_VTABLE_PROPERTY_CONST), |
3071 | BUS_PROPERTY_DUAL_TIMESTAMP("InitRDSecurityFinishTimestamp", offsetof(Manager, timestamps[MANAGER_TIMESTAMP_INITRD_SECURITY_FINISH]), SD_BUS_VTABLE_PROPERTY_CONST), | |
3072 | BUS_PROPERTY_DUAL_TIMESTAMP("InitRDGeneratorsStartTimestamp", offsetof(Manager, timestamps[MANAGER_TIMESTAMP_INITRD_GENERATORS_START]), SD_BUS_VTABLE_PROPERTY_CONST), | |
3073 | BUS_PROPERTY_DUAL_TIMESTAMP("InitRDGeneratorsFinishTimestamp", offsetof(Manager, timestamps[MANAGER_TIMESTAMP_INITRD_GENERATORS_FINISH]), SD_BUS_VTABLE_PROPERTY_CONST), | |
3074 | BUS_PROPERTY_DUAL_TIMESTAMP("InitRDUnitsLoadStartTimestamp", offsetof(Manager, timestamps[MANAGER_TIMESTAMP_INITRD_UNITS_LOAD_START]), SD_BUS_VTABLE_PROPERTY_CONST), | |
3075 | BUS_PROPERTY_DUAL_TIMESTAMP("InitRDUnitsLoadFinishTimestamp", offsetof(Manager, timestamps[MANAGER_TIMESTAMP_INITRD_UNITS_LOAD_FINISH]), SD_BUS_VTABLE_PROPERTY_CONST), | |
25141692 LP |
3076 | SD_BUS_WRITABLE_PROPERTY("LogLevel", "s", bus_property_get_log_level, property_set_log_level, 0, 0), |
3077 | SD_BUS_WRITABLE_PROPERTY("LogTarget", "s", bus_property_get_log_target, property_set_log_target, 0, 0), | |
3ff52e8f YW |
3078 | SD_BUS_PROPERTY("NNames", "u", property_get_hashmap_size, offsetof(Manager, units), 0), |
3079 | SD_BUS_PROPERTY("NFailedUnits", "u", property_get_set_size, offsetof(Manager, failed_units), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE), | |
3080 | SD_BUS_PROPERTY("NJobs", "u", property_get_hashmap_size, offsetof(Manager, jobs), 0), | |
718db961 LP |
3081 | SD_BUS_PROPERTY("NInstalledJobs", "u", bus_property_get_unsigned, offsetof(Manager, n_installed_jobs), 0), |
3082 | SD_BUS_PROPERTY("NFailedJobs", "u", bus_property_get_unsigned, offsetof(Manager, n_failed_jobs), 0), | |
3083 | SD_BUS_PROPERTY("Progress", "d", property_get_progress, 0, 0), | |
1ad6e8b3 | 3084 | SD_BUS_PROPERTY("Environment", "as", property_get_environment, 0, 0), |
556089dc | 3085 | SD_BUS_PROPERTY("ConfirmSpawn", "b", bus_property_get_bool, offsetof(Manager, confirm_spawn), SD_BUS_VTABLE_PROPERTY_CONST), |
52d2566a | 3086 | SD_BUS_PROPERTY("ShowStatus", "b", property_get_show_status, 0, 0), |
a3c4eb07 | 3087 | SD_BUS_PROPERTY("UnitPath", "as", NULL, offsetof(Manager, lookup_paths.search_path), SD_BUS_VTABLE_PROPERTY_CONST), |
c9e120e0 LP |
3088 | SD_BUS_PROPERTY("DefaultStandardOutput", "s", bus_property_get_exec_output, offsetof(Manager, defaults.std_output), SD_BUS_VTABLE_PROPERTY_CONST), |
3089 | SD_BUS_PROPERTY("DefaultStandardError", "s", bus_property_get_exec_output, offsetof(Manager, defaults.std_error), SD_BUS_VTABLE_PROPERTY_CONST), | |
3e911a0d YW |
3090 | SD_BUS_PROPERTY("WatchdogDevice", "s", property_get_watchdog_device, 0, SD_BUS_VTABLE_PROPERTY_CONST), |
3091 | SD_BUS_PROPERTY("WatchdogLastPingTimestamp", "t", property_get_watchdog_last_ping_realtime, 0, 0), | |
3092 | SD_BUS_PROPERTY("WatchdogLastPingTimestampMonotonic", "t", property_get_watchdog_last_ping_monotonic, 0, 0), | |
986935cf | 3093 | SD_BUS_WRITABLE_PROPERTY("RuntimeWatchdogUSec", "t", property_get_runtime_watchdog, property_set_runtime_watchdog, 0, 0), |
5717062e | 3094 | SD_BUS_WRITABLE_PROPERTY("RuntimeWatchdogPreUSec", "t", property_get_pretimeout_watchdog, property_set_pretimeout_watchdog, 0, 0), |
aff3a9e1 | 3095 | SD_BUS_WRITABLE_PROPERTY("RuntimeWatchdogPreGovernor", "s", property_get_pretimeout_watchdog_governor, property_set_pretimeout_watchdog_governor, 0, 0), |
986935cf | 3096 | SD_BUS_WRITABLE_PROPERTY("RebootWatchdogUSec", "t", property_get_reboot_watchdog, property_set_reboot_watchdog, 0, 0), |
65224c1d | 3097 | /* The following item is an obsolete alias */ |
986935cf FB |
3098 | SD_BUS_WRITABLE_PROPERTY("ShutdownWatchdogUSec", "t", property_get_reboot_watchdog, property_set_reboot_watchdog, 0, SD_BUS_VTABLE_HIDDEN), |
3099 | SD_BUS_WRITABLE_PROPERTY("KExecWatchdogUSec", "t", property_get_kexec_watchdog, property_set_kexec_watchdog, 0, 0), | |
2a12e32e | 3100 | SD_BUS_WRITABLE_PROPERTY("ServiceWatchdogs", "b", bus_property_get_bool, bus_property_set_bool, offsetof(Manager, service_watchdogs), 0), |
b272b74d | 3101 | SD_BUS_PROPERTY("ControlGroup", "s", NULL, offsetof(Manager, cgroup_root), 0), |
f755e3b7 | 3102 | SD_BUS_PROPERTY("SystemState", "s", property_get_system_state, 0, 0), |
287419c1 | 3103 | SD_BUS_PROPERTY("ExitCode", "y", bus_property_get_unsigned, offsetof(Manager, return_value), 0), |
c9e120e0 LP |
3104 | SD_BUS_PROPERTY("DefaultTimerAccuracyUSec", "t", bus_property_get_usec, offsetof(Manager, defaults.timer_accuracy_usec), SD_BUS_VTABLE_PROPERTY_CONST), |
3105 | SD_BUS_PROPERTY("DefaultTimeoutStartUSec", "t", bus_property_get_usec, offsetof(Manager, defaults.timeout_start_usec), SD_BUS_VTABLE_PROPERTY_CONST), | |
3106 | SD_BUS_PROPERTY("DefaultTimeoutStopUSec", "t", bus_property_get_usec, offsetof(Manager, defaults.timeout_stop_usec), SD_BUS_VTABLE_PROPERTY_CONST), | |
dc653bf4 | 3107 | SD_BUS_PROPERTY("DefaultTimeoutAbortUSec", "t", property_get_default_timeout_abort_usec, 0, 0), |
c9e120e0 LP |
3108 | SD_BUS_PROPERTY("DefaultDeviceTimeoutUSec", "t", bus_property_get_usec, offsetof(Manager, defaults.device_timeout_usec), SD_BUS_VTABLE_PROPERTY_CONST), |
3109 | SD_BUS_PROPERTY("DefaultRestartUSec", "t", bus_property_get_usec, offsetof(Manager, defaults.restart_usec), SD_BUS_VTABLE_PROPERTY_CONST), | |
3110 | SD_BUS_PROPERTY("DefaultStartLimitIntervalUSec", "t", bus_property_get_usec, offsetof(Manager, defaults.start_limit_interval), SD_BUS_VTABLE_PROPERTY_CONST), | |
c075f5fc | 3111 | /* The following two items are obsolete alias */ |
c9e120e0 LP |
3112 | SD_BUS_PROPERTY("DefaultStartLimitIntervalSec", "t", bus_property_get_usec, offsetof(Manager, defaults.start_limit_interval), SD_BUS_VTABLE_PROPERTY_CONST|SD_BUS_VTABLE_HIDDEN), |
3113 | SD_BUS_PROPERTY("DefaultStartLimitInterval", "t", bus_property_get_usec, offsetof(Manager, defaults.start_limit_interval), SD_BUS_VTABLE_PROPERTY_CONST|SD_BUS_VTABLE_HIDDEN), | |
3114 | SD_BUS_PROPERTY("DefaultStartLimitBurst", "u", bus_property_get_unsigned, offsetof(Manager, defaults.start_limit_burst), SD_BUS_VTABLE_PROPERTY_CONST), | |
3115 | SD_BUS_PROPERTY("DefaultCPUAccounting", "b", bus_property_get_bool, offsetof(Manager, defaults.cpu_accounting), SD_BUS_VTABLE_PROPERTY_CONST), | |
3116 | SD_BUS_PROPERTY("DefaultBlockIOAccounting", "b", bus_property_get_bool, offsetof(Manager, defaults.blockio_accounting), SD_BUS_VTABLE_PROPERTY_CONST), | |
3117 | SD_BUS_PROPERTY("DefaultIOAccounting", "b", bus_property_get_bool, offsetof(Manager, defaults.io_accounting), SD_BUS_VTABLE_PROPERTY_CONST), | |
3118 | SD_BUS_PROPERTY("DefaultIPAccounting", "b", bus_property_get_bool, offsetof(Manager, defaults.ip_accounting), SD_BUS_VTABLE_PROPERTY_CONST), | |
3119 | SD_BUS_PROPERTY("DefaultMemoryAccounting", "b", bus_property_get_bool, offsetof(Manager, defaults.memory_accounting), SD_BUS_VTABLE_PROPERTY_CONST), | |
3120 | SD_BUS_PROPERTY("DefaultTasksAccounting", "b", bus_property_get_bool, offsetof(Manager, defaults.tasks_accounting), SD_BUS_VTABLE_PROPERTY_CONST), | |
3121 | SD_BUS_PROPERTY("DefaultLimitCPU", "t", bus_property_get_rlimit, offsetof(Manager, defaults.rlimit[RLIMIT_CPU]), SD_BUS_VTABLE_PROPERTY_CONST), | |
3122 | SD_BUS_PROPERTY("DefaultLimitCPUSoft", "t", bus_property_get_rlimit, offsetof(Manager, defaults.rlimit[RLIMIT_CPU]), SD_BUS_VTABLE_PROPERTY_CONST), | |
3123 | SD_BUS_PROPERTY("DefaultLimitFSIZE", "t", bus_property_get_rlimit, offsetof(Manager, defaults.rlimit[RLIMIT_FSIZE]), SD_BUS_VTABLE_PROPERTY_CONST), | |
3124 | SD_BUS_PROPERTY("DefaultLimitFSIZESoft", "t", bus_property_get_rlimit, offsetof(Manager, defaults.rlimit[RLIMIT_FSIZE]), SD_BUS_VTABLE_PROPERTY_CONST), | |
3125 | SD_BUS_PROPERTY("DefaultLimitDATA", "t", bus_property_get_rlimit, offsetof(Manager, defaults.rlimit[RLIMIT_DATA]), SD_BUS_VTABLE_PROPERTY_CONST), | |
3126 | SD_BUS_PROPERTY("DefaultLimitDATASoft", "t", bus_property_get_rlimit, offsetof(Manager, defaults.rlimit[RLIMIT_DATA]), SD_BUS_VTABLE_PROPERTY_CONST), | |
3127 | SD_BUS_PROPERTY("DefaultLimitSTACK", "t", bus_property_get_rlimit, offsetof(Manager, defaults.rlimit[RLIMIT_STACK]), SD_BUS_VTABLE_PROPERTY_CONST), | |
3128 | SD_BUS_PROPERTY("DefaultLimitSTACKSoft", "t", bus_property_get_rlimit, offsetof(Manager, defaults.rlimit[RLIMIT_STACK]), SD_BUS_VTABLE_PROPERTY_CONST), | |
3129 | SD_BUS_PROPERTY("DefaultLimitCORE", "t", bus_property_get_rlimit, offsetof(Manager, defaults.rlimit[RLIMIT_CORE]), SD_BUS_VTABLE_PROPERTY_CONST), | |
3130 | SD_BUS_PROPERTY("DefaultLimitCORESoft", "t", bus_property_get_rlimit, offsetof(Manager, defaults.rlimit[RLIMIT_CORE]), SD_BUS_VTABLE_PROPERTY_CONST), | |
3131 | SD_BUS_PROPERTY("DefaultLimitRSS", "t", bus_property_get_rlimit, offsetof(Manager, defaults.rlimit[RLIMIT_RSS]), SD_BUS_VTABLE_PROPERTY_CONST), | |
3132 | SD_BUS_PROPERTY("DefaultLimitRSSSoft", "t", bus_property_get_rlimit, offsetof(Manager, defaults.rlimit[RLIMIT_RSS]), SD_BUS_VTABLE_PROPERTY_CONST), | |
3133 | SD_BUS_PROPERTY("DefaultLimitNOFILE", "t", bus_property_get_rlimit, offsetof(Manager, defaults.rlimit[RLIMIT_NOFILE]), SD_BUS_VTABLE_PROPERTY_CONST), | |
3134 | SD_BUS_PROPERTY("DefaultLimitNOFILESoft", "t", bus_property_get_rlimit, offsetof(Manager, defaults.rlimit[RLIMIT_NOFILE]), SD_BUS_VTABLE_PROPERTY_CONST), | |
3135 | SD_BUS_PROPERTY("DefaultLimitAS", "t", bus_property_get_rlimit, offsetof(Manager, defaults.rlimit[RLIMIT_AS]), SD_BUS_VTABLE_PROPERTY_CONST), | |
3136 | SD_BUS_PROPERTY("DefaultLimitASSoft", "t", bus_property_get_rlimit, offsetof(Manager, defaults.rlimit[RLIMIT_AS]), SD_BUS_VTABLE_PROPERTY_CONST), | |
3137 | SD_BUS_PROPERTY("DefaultLimitNPROC", "t", bus_property_get_rlimit, offsetof(Manager, defaults.rlimit[RLIMIT_NPROC]), SD_BUS_VTABLE_PROPERTY_CONST), | |
3138 | SD_BUS_PROPERTY("DefaultLimitNPROCSoft", "t", bus_property_get_rlimit, offsetof(Manager, defaults.rlimit[RLIMIT_NPROC]), SD_BUS_VTABLE_PROPERTY_CONST), | |
3139 | SD_BUS_PROPERTY("DefaultLimitMEMLOCK", "t", bus_property_get_rlimit, offsetof(Manager, defaults.rlimit[RLIMIT_MEMLOCK]), SD_BUS_VTABLE_PROPERTY_CONST), | |
3140 | SD_BUS_PROPERTY("DefaultLimitMEMLOCKSoft", "t", bus_property_get_rlimit, offsetof(Manager, defaults.rlimit[RLIMIT_MEMLOCK]), SD_BUS_VTABLE_PROPERTY_CONST), | |
3141 | SD_BUS_PROPERTY("DefaultLimitLOCKS", "t", bus_property_get_rlimit, offsetof(Manager, defaults.rlimit[RLIMIT_LOCKS]), SD_BUS_VTABLE_PROPERTY_CONST), | |
3142 | SD_BUS_PROPERTY("DefaultLimitLOCKSSoft", "t", bus_property_get_rlimit, offsetof(Manager, defaults.rlimit[RLIMIT_LOCKS]), SD_BUS_VTABLE_PROPERTY_CONST), | |
3143 | SD_BUS_PROPERTY("DefaultLimitSIGPENDING", "t", bus_property_get_rlimit, offsetof(Manager, defaults.rlimit[RLIMIT_SIGPENDING]), SD_BUS_VTABLE_PROPERTY_CONST), | |
3144 | SD_BUS_PROPERTY("DefaultLimitSIGPENDINGSoft", "t", bus_property_get_rlimit, offsetof(Manager, defaults.rlimit[RLIMIT_SIGPENDING]), SD_BUS_VTABLE_PROPERTY_CONST), | |
3145 | SD_BUS_PROPERTY("DefaultLimitMSGQUEUE", "t", bus_property_get_rlimit, offsetof(Manager, defaults.rlimit[RLIMIT_MSGQUEUE]), SD_BUS_VTABLE_PROPERTY_CONST), | |
3146 | SD_BUS_PROPERTY("DefaultLimitMSGQUEUESoft", "t", bus_property_get_rlimit, offsetof(Manager, defaults.rlimit[RLIMIT_MSGQUEUE]), SD_BUS_VTABLE_PROPERTY_CONST), | |
3147 | SD_BUS_PROPERTY("DefaultLimitNICE", "t", bus_property_get_rlimit, offsetof(Manager, defaults.rlimit[RLIMIT_NICE]), SD_BUS_VTABLE_PROPERTY_CONST), | |
3148 | SD_BUS_PROPERTY("DefaultLimitNICESoft", "t", bus_property_get_rlimit, offsetof(Manager, defaults.rlimit[RLIMIT_NICE]), SD_BUS_VTABLE_PROPERTY_CONST), | |
3149 | SD_BUS_PROPERTY("DefaultLimitRTPRIO", "t", bus_property_get_rlimit, offsetof(Manager, defaults.rlimit[RLIMIT_RTPRIO]), SD_BUS_VTABLE_PROPERTY_CONST), | |
3150 | SD_BUS_PROPERTY("DefaultLimitRTPRIOSoft", "t", bus_property_get_rlimit, offsetof(Manager, defaults.rlimit[RLIMIT_RTPRIO]), SD_BUS_VTABLE_PROPERTY_CONST), | |
3151 | SD_BUS_PROPERTY("DefaultLimitRTTIME", "t", bus_property_get_rlimit, offsetof(Manager, defaults.rlimit[RLIMIT_RTTIME]), SD_BUS_VTABLE_PROPERTY_CONST), | |
3152 | SD_BUS_PROPERTY("DefaultLimitRTTIMESoft", "t", bus_property_get_rlimit, offsetof(Manager, defaults.rlimit[RLIMIT_RTTIME]), SD_BUS_VTABLE_PROPERTY_CONST), | |
3153 | SD_BUS_PROPERTY("DefaultTasksMax", "t", bus_property_get_tasks_max, offsetof(Manager, defaults.tasks_max), 0), | |
3154 | SD_BUS_PROPERTY("DefaultMemoryPressureThresholdUSec", "t", bus_property_get_usec, offsetof(Manager, defaults.memory_pressure_threshold_usec), 0), | |
3155 | SD_BUS_PROPERTY("DefaultMemoryPressureWatch", "s", bus_property_get_cgroup_pressure_watch, offsetof(Manager, defaults.memory_pressure_watch), 0), | |
eed67a30 | 3156 | SD_BUS_PROPERTY("TimerSlackNSec", "t", property_get_timer_slack_nsec, 0, SD_BUS_VTABLE_PROPERTY_CONST), |
c9e120e0 | 3157 | SD_BUS_PROPERTY("DefaultOOMPolicy", "s", bus_property_get_oom_policy, offsetof(Manager, defaults.oom_policy), SD_BUS_VTABLE_PROPERTY_CONST), |
d4a402e4 | 3158 | SD_BUS_PROPERTY("DefaultOOMScoreAdjust", "i", property_get_oom_score_adjust, 0, SD_BUS_VTABLE_PROPERTY_CONST), |
c44a285c | 3159 | SD_BUS_PROPERTY("CtrlAltDelBurstAction", "s", bus_property_get_emergency_action, offsetof(Manager, cad_burst_action), SD_BUS_VTABLE_PROPERTY_CONST), |
66f35161 | 3160 | SD_BUS_PROPERTY("SoftRebootsCount", "u", bus_property_get_unsigned, offsetof(Manager, soft_reboots_count), SD_BUS_VTABLE_PROPERTY_CONST), |
718db961 | 3161 | |
a008b6d7 | 3162 | SD_BUS_METHOD_WITH_ARGS("GetUnit", |
3163 | SD_BUS_ARGS("s", name), | |
3164 | SD_BUS_RESULT("o", unit), | |
3165 | method_get_unit, | |
3166 | SD_BUS_VTABLE_UNPRIVILEGED), | |
3167 | SD_BUS_METHOD_WITH_ARGS("GetUnitByPID", | |
3168 | SD_BUS_ARGS("u", pid), | |
3169 | SD_BUS_RESULT("o", unit), | |
3170 | method_get_unit_by_pid, | |
3171 | SD_BUS_VTABLE_UNPRIVILEGED), | |
3172 | SD_BUS_METHOD_WITH_ARGS("GetUnitByInvocationID", | |
3173 | SD_BUS_ARGS("ay", invocation_id), | |
3174 | SD_BUS_RESULT("o", unit), | |
3175 | method_get_unit_by_invocation_id, | |
3176 | SD_BUS_VTABLE_UNPRIVILEGED), | |
3177 | SD_BUS_METHOD_WITH_ARGS("GetUnitByControlGroup", | |
3178 | SD_BUS_ARGS("s", cgroup), | |
3179 | SD_BUS_RESULT("o", unit), | |
3180 | method_get_unit_by_control_group, | |
3181 | SD_BUS_VTABLE_UNPRIVILEGED), | |
e0e7bc82 LB |
3182 | SD_BUS_METHOD_WITH_ARGS("GetUnitByPIDFD", |
3183 | SD_BUS_ARGS("h", pidfd), | |
3184 | SD_BUS_RESULT("o", unit, "s", unit_id, "ay", invocation_id), | |
3185 | method_get_unit_by_pidfd, | |
3186 | SD_BUS_VTABLE_UNPRIVILEGED), | |
a008b6d7 | 3187 | SD_BUS_METHOD_WITH_ARGS("LoadUnit", |
3188 | SD_BUS_ARGS("s", name), | |
3189 | SD_BUS_RESULT("o", unit), | |
3190 | method_load_unit, | |
3191 | SD_BUS_VTABLE_UNPRIVILEGED), | |
3192 | SD_BUS_METHOD_WITH_ARGS("StartUnit", | |
3193 | SD_BUS_ARGS("s", name, "s", mode), | |
3194 | SD_BUS_RESULT("o", job), | |
3195 | method_start_unit, | |
3196 | SD_BUS_VTABLE_UNPRIVILEGED), | |
3197 | SD_BUS_METHOD_WITH_ARGS("StartUnitWithFlags", | |
3198 | SD_BUS_ARGS("s", name, "s", mode, "t", flags), | |
3199 | SD_BUS_RESULT("o", job), | |
3200 | method_start_unit, | |
3201 | SD_BUS_VTABLE_UNPRIVILEGED), | |
3202 | SD_BUS_METHOD_WITH_ARGS("StartUnitReplace", | |
3203 | SD_BUS_ARGS("s", old_unit, "s", new_unit, "s", mode), | |
3204 | SD_BUS_RESULT("o", job), | |
3205 | method_start_unit_replace, | |
3206 | SD_BUS_VTABLE_UNPRIVILEGED), | |
3207 | SD_BUS_METHOD_WITH_ARGS("StopUnit", | |
3208 | SD_BUS_ARGS("s", name, "s", mode), | |
3209 | SD_BUS_RESULT("o", job), | |
3210 | method_stop_unit, | |
3211 | SD_BUS_VTABLE_UNPRIVILEGED), | |
3212 | SD_BUS_METHOD_WITH_ARGS("ReloadUnit", | |
3213 | SD_BUS_ARGS("s", name, "s", mode), | |
3214 | SD_BUS_RESULT("o", job), | |
3215 | method_reload_unit, | |
3216 | SD_BUS_VTABLE_UNPRIVILEGED), | |
3217 | SD_BUS_METHOD_WITH_ARGS("RestartUnit", | |
3218 | SD_BUS_ARGS("s", name, "s", mode), | |
3219 | SD_BUS_RESULT("o", job), | |
3220 | method_restart_unit, | |
3221 | SD_BUS_VTABLE_UNPRIVILEGED), | |
3222 | SD_BUS_METHOD_WITH_ARGS("TryRestartUnit", | |
3223 | SD_BUS_ARGS("s", name, "s", mode), | |
3224 | SD_BUS_RESULT("o", job), | |
3225 | method_try_restart_unit, | |
3226 | SD_BUS_VTABLE_UNPRIVILEGED), | |
3227 | SD_BUS_METHOD_WITH_ARGS("ReloadOrRestartUnit", | |
3228 | SD_BUS_ARGS("s", name, "s", mode), | |
3229 | SD_BUS_RESULT("o", job), | |
3230 | method_reload_or_restart_unit, | |
3231 | SD_BUS_VTABLE_UNPRIVILEGED), | |
3232 | SD_BUS_METHOD_WITH_ARGS("ReloadOrTryRestartUnit", | |
3233 | SD_BUS_ARGS("s", name, "s", mode), | |
3234 | SD_BUS_RESULT("o", job), | |
3235 | method_reload_or_try_restart_unit, | |
3236 | SD_BUS_VTABLE_UNPRIVILEGED), | |
3237 | SD_BUS_METHOD_WITH_ARGS("EnqueueUnitJob", | |
3238 | SD_BUS_ARGS("s", name, "s", job_type, "s", job_mode), | |
3239 | SD_BUS_RESULT("u", job_id, "o", job_path, "s", unit_id, "o", unit_path, "s", job_type, "a(uosos)", affected_jobs), | |
3240 | method_enqueue_unit_job, | |
3241 | SD_BUS_VTABLE_UNPRIVILEGED), | |
3242 | SD_BUS_METHOD_WITH_ARGS("KillUnit", | |
3243 | SD_BUS_ARGS("s", name, "s", whom, "i", signal), | |
3244 | SD_BUS_NO_RESULT, | |
a721cd00 LP |
3245 | method_kill_unit, |
3246 | SD_BUS_VTABLE_UNPRIVILEGED), | |
3247 | SD_BUS_METHOD_WITH_ARGS("QueueSignalUnit", | |
3248 | SD_BUS_ARGS("s", name, "s", whom, "i", signal, "i", value), | |
3249 | SD_BUS_NO_RESULT, | |
a008b6d7 | 3250 | method_kill_unit, |
3251 | SD_BUS_VTABLE_UNPRIVILEGED), | |
3252 | SD_BUS_METHOD_WITH_ARGS("CleanUnit", | |
3253 | SD_BUS_ARGS("s", name, "as", mask), | |
3254 | SD_BUS_NO_RESULT, | |
3255 | method_clean_unit, | |
3256 | SD_BUS_VTABLE_UNPRIVILEGED), | |
3257 | SD_BUS_METHOD_WITH_ARGS("FreezeUnit", | |
3258 | SD_BUS_ARGS("s", name), | |
3259 | SD_BUS_NO_RESULT, | |
3260 | method_freeze_unit, | |
3261 | SD_BUS_VTABLE_UNPRIVILEGED), | |
3262 | SD_BUS_METHOD_WITH_ARGS("ThawUnit", | |
3263 | SD_BUS_ARGS("s", name), | |
3264 | SD_BUS_NO_RESULT, | |
3265 | method_thaw_unit, | |
3266 | SD_BUS_VTABLE_UNPRIVILEGED), | |
3267 | SD_BUS_METHOD_WITH_ARGS("ResetFailedUnit", | |
3268 | SD_BUS_ARGS("s", name), | |
3269 | SD_BUS_NO_RESULT, | |
3270 | method_reset_failed_unit, | |
3271 | SD_BUS_VTABLE_UNPRIVILEGED), | |
3272 | SD_BUS_METHOD_WITH_ARGS("SetUnitProperties", | |
3273 | SD_BUS_ARGS("s", name, "b", runtime, "a(sv)", properties), | |
3274 | SD_BUS_NO_RESULT, | |
3275 | method_set_unit_properties, | |
3276 | SD_BUS_VTABLE_UNPRIVILEGED), | |
3277 | SD_BUS_METHOD_WITH_ARGS("BindMountUnit", | |
3278 | SD_BUS_ARGS("s", name, "s", source, "s", destination, "b", read_only, "b", mkdir), | |
3279 | SD_BUS_NO_RESULT, | |
3280 | method_bind_mount_unit, | |
3281 | SD_BUS_VTABLE_UNPRIVILEGED), | |
3282 | SD_BUS_METHOD_WITH_ARGS("MountImageUnit", | |
3283 | SD_BUS_ARGS("s", name, "s", source, "s", destination, "b", read_only, "b", mkdir, "a(ss)", options), | |
3284 | SD_BUS_NO_RESULT, | |
3285 | method_mount_image_unit, | |
3286 | SD_BUS_VTABLE_UNPRIVILEGED), | |
3287 | SD_BUS_METHOD_WITH_ARGS("RefUnit", | |
3288 | SD_BUS_ARGS("s", name), | |
3289 | SD_BUS_NO_RESULT, | |
3290 | method_ref_unit, | |
3291 | SD_BUS_VTABLE_UNPRIVILEGED), | |
3292 | SD_BUS_METHOD_WITH_ARGS("UnrefUnit", | |
3293 | SD_BUS_ARGS("s", name), | |
3294 | SD_BUS_NO_RESULT, | |
3295 | method_unref_unit, | |
3296 | SD_BUS_VTABLE_UNPRIVILEGED), | |
3297 | SD_BUS_METHOD_WITH_ARGS("StartTransientUnit", | |
3298 | SD_BUS_ARGS("s", name, "s", mode, "a(sv)", properties, "a(sa(sv))", aux), | |
3299 | SD_BUS_RESULT("o", job), | |
3300 | method_start_transient_unit, | |
3301 | SD_BUS_VTABLE_UNPRIVILEGED), | |
3302 | SD_BUS_METHOD_WITH_ARGS("GetUnitProcesses", | |
3303 | SD_BUS_ARGS("s", name), | |
3304 | SD_BUS_RESULT("a(sus)", processes), | |
3305 | method_get_unit_processes, | |
3306 | SD_BUS_VTABLE_UNPRIVILEGED), | |
3307 | SD_BUS_METHOD_WITH_ARGS("AttachProcessesToUnit", | |
3308 | SD_BUS_ARGS("s", unit_name, "s", subcgroup, "au", pids), | |
3309 | SD_BUS_NO_RESULT, | |
3310 | method_attach_processes_to_unit, | |
3311 | SD_BUS_VTABLE_UNPRIVILEGED), | |
3312 | SD_BUS_METHOD_WITH_ARGS("AbandonScope", | |
3313 | SD_BUS_ARGS("s", name), | |
3314 | SD_BUS_NO_RESULT, | |
3315 | method_abandon_scope, | |
3316 | SD_BUS_VTABLE_UNPRIVILEGED), | |
3317 | SD_BUS_METHOD_WITH_ARGS("GetJob", | |
3318 | SD_BUS_ARGS("u", id), | |
3319 | SD_BUS_RESULT("o", job), | |
3320 | method_get_job, | |
3321 | SD_BUS_VTABLE_UNPRIVILEGED), | |
3322 | SD_BUS_METHOD_WITH_ARGS("GetJobAfter", | |
3323 | SD_BUS_ARGS("u", id), | |
3324 | SD_BUS_RESULT("a(usssoo)", jobs), | |
3325 | method_get_job_waiting, | |
3326 | SD_BUS_VTABLE_UNPRIVILEGED), | |
3327 | SD_BUS_METHOD_WITH_ARGS("GetJobBefore", | |
3328 | SD_BUS_ARGS("u", id), | |
3329 | SD_BUS_RESULT("a(usssoo)", jobs), | |
3330 | method_get_job_waiting, | |
3331 | SD_BUS_VTABLE_UNPRIVILEGED), | |
3332 | SD_BUS_METHOD_WITH_ARGS("CancelJob", | |
3333 | SD_BUS_ARGS("u", id), | |
3334 | SD_BUS_NO_RESULT, | |
3335 | method_cancel_job, | |
3336 | SD_BUS_VTABLE_UNPRIVILEGED), | |
dad97f04 ZJS |
3337 | SD_BUS_METHOD("ClearJobs", |
3338 | NULL, | |
3339 | NULL, | |
3340 | method_clear_jobs, | |
3341 | SD_BUS_VTABLE_UNPRIVILEGED), | |
3342 | SD_BUS_METHOD("ResetFailed", | |
3343 | NULL, | |
3344 | NULL, | |
3345 | method_reset_failed, | |
3346 | SD_BUS_VTABLE_UNPRIVILEGED), | |
a008b6d7 | 3347 | SD_BUS_METHOD_WITH_ARGS("SetShowStatus", |
3348 | SD_BUS_ARGS("s", mode), | |
3349 | SD_BUS_NO_RESULT, | |
3350 | method_set_show_status, | |
3351 | SD_BUS_VTABLE_UNPRIVILEGED), | |
3352 | SD_BUS_METHOD_WITH_ARGS("ListUnits", | |
3353 | SD_BUS_NO_ARGS, | |
3354 | SD_BUS_RESULT("a(ssssssouso)", units), | |
3355 | method_list_units, | |
3356 | SD_BUS_VTABLE_UNPRIVILEGED), | |
3357 | SD_BUS_METHOD_WITH_ARGS("ListUnitsFiltered", | |
3358 | SD_BUS_ARGS("as", states), | |
3359 | SD_BUS_RESULT("a(ssssssouso)", units), | |
3360 | method_list_units_filtered, | |
3361 | SD_BUS_VTABLE_UNPRIVILEGED), | |
3362 | SD_BUS_METHOD_WITH_ARGS("ListUnitsByPatterns", | |
3363 | SD_BUS_ARGS("as", states, "as", patterns), | |
3364 | SD_BUS_RESULT("a(ssssssouso)", units), | |
3365 | method_list_units_by_patterns, | |
3366 | SD_BUS_VTABLE_UNPRIVILEGED), | |
3367 | SD_BUS_METHOD_WITH_ARGS("ListUnitsByNames", | |
3368 | SD_BUS_ARGS("as", names), | |
3369 | SD_BUS_RESULT("a(ssssssouso)", units), | |
3370 | method_list_units_by_names, | |
3371 | SD_BUS_VTABLE_UNPRIVILEGED), | |
3372 | SD_BUS_METHOD_WITH_ARGS("ListJobs", | |
3373 | SD_BUS_NO_ARGS, | |
3374 | SD_BUS_RESULT("a(usssoo)", jobs), | |
3375 | method_list_jobs, | |
3376 | SD_BUS_VTABLE_UNPRIVILEGED), | |
dad97f04 ZJS |
3377 | SD_BUS_METHOD("Subscribe", |
3378 | NULL, | |
3379 | NULL, | |
3380 | method_subscribe, | |
3381 | SD_BUS_VTABLE_UNPRIVILEGED), | |
3382 | SD_BUS_METHOD("Unsubscribe", | |
3383 | NULL, | |
3384 | NULL, | |
3385 | method_unsubscribe, | |
3386 | SD_BUS_VTABLE_UNPRIVILEGED), | |
a008b6d7 | 3387 | SD_BUS_METHOD_WITH_ARGS("Dump", |
3388 | SD_BUS_NO_ARGS, | |
3389 | SD_BUS_RESULT("s", output), | |
3390 | method_dump, | |
3391 | SD_BUS_VTABLE_UNPRIVILEGED), | |
293b9aa3 | 3392 | SD_BUS_METHOD_WITH_ARGS("DumpUnitsMatchingPatterns", |
d1d8786c FB |
3393 | SD_BUS_ARGS("as", patterns), |
3394 | SD_BUS_RESULT("s", output), | |
293b9aa3 | 3395 | method_dump_units_matching_patterns, |
d1d8786c | 3396 | SD_BUS_VTABLE_UNPRIVILEGED), |
a008b6d7 | 3397 | SD_BUS_METHOD_WITH_ARGS("DumpByFileDescriptor", |
3398 | SD_BUS_NO_ARGS, | |
3399 | SD_BUS_RESULT("h", fd), | |
3400 | method_dump_by_fd, | |
3401 | SD_BUS_VTABLE_UNPRIVILEGED), | |
f6cce15b ZJS |
3402 | SD_BUS_METHOD_WITH_ARGS("DumpUnitsMatchingPatternsByFileDescriptor", |
3403 | SD_BUS_ARGS("as", patterns), | |
3404 | SD_BUS_RESULT("h", fd), | |
3405 | method_dump_units_matching_patterns_by_fd, | |
3406 | SD_BUS_VTABLE_UNPRIVILEGED), | |
a008b6d7 | 3407 | SD_BUS_METHOD_WITH_ARGS("CreateSnapshot", |
3408 | SD_BUS_ARGS("s", name, "b", cleanup), | |
3409 | SD_BUS_RESULT("o", unit), | |
3410 | method_refuse_snapshot, | |
3411 | SD_BUS_VTABLE_UNPRIVILEGED|SD_BUS_VTABLE_HIDDEN), | |
3412 | SD_BUS_METHOD_WITH_ARGS("RemoveSnapshot", | |
3413 | SD_BUS_ARGS("s", name), | |
3414 | SD_BUS_NO_RESULT, | |
3415 | method_refuse_snapshot, | |
3416 | SD_BUS_VTABLE_UNPRIVILEGED|SD_BUS_VTABLE_HIDDEN), | |
dad97f04 ZJS |
3417 | SD_BUS_METHOD("Reload", |
3418 | NULL, | |
3419 | NULL, | |
3420 | method_reload, | |
3421 | SD_BUS_VTABLE_UNPRIVILEGED), | |
3422 | SD_BUS_METHOD("Reexecute", | |
3423 | NULL, | |
3424 | NULL, | |
3425 | method_reexecute, | |
624f685f | 3426 | SD_BUS_VTABLE_UNPRIVILEGED|SD_BUS_VTABLE_METHOD_NO_REPLY), |
dad97f04 ZJS |
3427 | SD_BUS_METHOD("Exit", |
3428 | NULL, | |
3429 | NULL, | |
3430 | method_exit, | |
3431 | 0), | |
3432 | SD_BUS_METHOD("Reboot", | |
3433 | NULL, | |
3434 | NULL, | |
3435 | method_reboot, | |
3436 | SD_BUS_VTABLE_CAPABILITY(CAP_SYS_BOOT)), | |
13ffc607 LP |
3437 | SD_BUS_METHOD_WITH_ARGS("SoftReboot", |
3438 | SD_BUS_ARGS("s", new_root), | |
3439 | SD_BUS_NO_RESULT, | |
3440 | method_soft_reboot, | |
3441 | SD_BUS_VTABLE_CAPABILITY(CAP_SYS_BOOT)), | |
dad97f04 ZJS |
3442 | SD_BUS_METHOD("PowerOff", |
3443 | NULL, | |
3444 | NULL, | |
3445 | method_poweroff, | |
3446 | SD_BUS_VTABLE_CAPABILITY(CAP_SYS_BOOT)), | |
3447 | SD_BUS_METHOD("Halt", | |
3448 | NULL, | |
3449 | NULL, | |
3450 | method_halt, | |
3451 | SD_BUS_VTABLE_CAPABILITY(CAP_SYS_BOOT)), | |
3452 | SD_BUS_METHOD("KExec", | |
3453 | NULL, | |
3454 | NULL, | |
3455 | method_kexec, | |
3456 | SD_BUS_VTABLE_CAPABILITY(CAP_SYS_BOOT)), | |
a008b6d7 | 3457 | SD_BUS_METHOD_WITH_ARGS("SwitchRoot", |
3458 | SD_BUS_ARGS("s", new_root, "s", init), | |
3459 | SD_BUS_NO_RESULT, | |
3460 | method_switch_root, | |
3461 | SD_BUS_VTABLE_CAPABILITY(CAP_SYS_BOOT)), | |
3462 | SD_BUS_METHOD_WITH_ARGS("SetEnvironment", | |
3463 | SD_BUS_ARGS("as", assignments), | |
3464 | SD_BUS_NO_RESULT, | |
3465 | method_set_environment, | |
3466 | SD_BUS_VTABLE_UNPRIVILEGED), | |
3467 | SD_BUS_METHOD_WITH_ARGS("UnsetEnvironment", | |
3468 | SD_BUS_ARGS("as", names), | |
3469 | SD_BUS_NO_RESULT, | |
3470 | method_unset_environment, | |
3471 | SD_BUS_VTABLE_UNPRIVILEGED), | |
3472 | SD_BUS_METHOD_WITH_ARGS("UnsetAndSetEnvironment", | |
3473 | SD_BUS_ARGS("as", names, "as", assignments), | |
3474 | SD_BUS_NO_RESULT, | |
3475 | method_unset_and_set_environment, | |
3476 | SD_BUS_VTABLE_UNPRIVILEGED), | |
3477 | SD_BUS_METHOD_WITH_ARGS("EnqueueMarkedJobs", | |
3478 | SD_BUS_NO_ARGS, | |
3479 | SD_BUS_RESULT("ao", jobs), | |
3480 | method_enqueue_marked_jobs, | |
3481 | SD_BUS_VTABLE_UNPRIVILEGED), | |
3482 | SD_BUS_METHOD_WITH_ARGS("ListUnitFiles", | |
3483 | SD_BUS_NO_ARGS, | |
3484 | SD_BUS_RESULT("a(ss)", unit_files), | |
3485 | method_list_unit_files, | |
3486 | SD_BUS_VTABLE_UNPRIVILEGED), | |
3487 | SD_BUS_METHOD_WITH_ARGS("ListUnitFilesByPatterns", | |
3488 | SD_BUS_ARGS("as", states, "as", patterns), | |
3489 | SD_BUS_RESULT("a(ss)", unit_files), | |
3490 | method_list_unit_files_by_patterns, | |
3491 | SD_BUS_VTABLE_UNPRIVILEGED), | |
3492 | SD_BUS_METHOD_WITH_ARGS("GetUnitFileState", | |
3493 | SD_BUS_ARGS("s", file), | |
3494 | SD_BUS_RESULT("s", state), | |
3495 | method_get_unit_file_state, | |
3496 | SD_BUS_VTABLE_UNPRIVILEGED), | |
3497 | SD_BUS_METHOD_WITH_ARGS("EnableUnitFiles", | |
3498 | SD_BUS_ARGS("as", files, "b", runtime, "b", force), | |
3499 | SD_BUS_RESULT("b", carries_install_info, "a(sss)", changes), | |
3500 | method_enable_unit_files, | |
3501 | SD_BUS_VTABLE_UNPRIVILEGED), | |
3502 | SD_BUS_METHOD_WITH_ARGS("DisableUnitFiles", | |
3503 | SD_BUS_ARGS("as", files, "b", runtime), | |
3504 | SD_BUS_RESULT("a(sss)", changes), | |
3505 | method_disable_unit_files, | |
3506 | SD_BUS_VTABLE_UNPRIVILEGED), | |
3507 | SD_BUS_METHOD_WITH_ARGS("EnableUnitFilesWithFlags", | |
3508 | SD_BUS_ARGS("as", files, "t", flags), | |
3509 | SD_BUS_RESULT("b", carries_install_info, "a(sss)", changes), | |
3510 | method_enable_unit_files_with_flags, | |
3511 | SD_BUS_VTABLE_UNPRIVILEGED), | |
3512 | SD_BUS_METHOD_WITH_ARGS("DisableUnitFilesWithFlags", | |
3513 | SD_BUS_ARGS("as", files, "t", flags), | |
3514 | SD_BUS_RESULT("a(sss)", changes), | |
3515 | method_disable_unit_files_with_flags, | |
3516 | SD_BUS_VTABLE_UNPRIVILEGED), | |
bf1bea43 MY |
3517 | SD_BUS_METHOD_WITH_ARGS("DisableUnitFilesWithFlagsAndInstallInfo", |
3518 | SD_BUS_ARGS("as", files, "t", flags), | |
3519 | SD_BUS_RESULT("b", carries_install_info, "a(sss)", changes), | |
3520 | method_disable_unit_files_with_flags_and_install_info, | |
3521 | SD_BUS_VTABLE_UNPRIVILEGED), | |
a008b6d7 | 3522 | SD_BUS_METHOD_WITH_ARGS("ReenableUnitFiles", |
3523 | SD_BUS_ARGS("as", files, "b", runtime, "b", force), | |
3524 | SD_BUS_RESULT("b", carries_install_info, "a(sss)", changes), | |
3525 | method_reenable_unit_files, | |
3526 | SD_BUS_VTABLE_UNPRIVILEGED), | |
3527 | SD_BUS_METHOD_WITH_ARGS("LinkUnitFiles", | |
3528 | SD_BUS_ARGS("as", files, "b", runtime, "b", force), | |
3529 | SD_BUS_RESULT("a(sss)", changes), | |
3530 | method_link_unit_files, | |
3531 | SD_BUS_VTABLE_UNPRIVILEGED), | |
3532 | SD_BUS_METHOD_WITH_ARGS("PresetUnitFiles", | |
3533 | SD_BUS_ARGS("as", files, "b", runtime, "b", force), | |
3534 | SD_BUS_RESULT("b", carries_install_info, "a(sss)", changes), | |
3535 | method_preset_unit_files, | |
3536 | SD_BUS_VTABLE_UNPRIVILEGED), | |
3537 | SD_BUS_METHOD_WITH_ARGS("PresetUnitFilesWithMode", | |
3538 | SD_BUS_ARGS("as", files, "s", mode, "b", runtime, "b", force), | |
3539 | SD_BUS_RESULT("b", carries_install_info, "a(sss)", changes), | |
3540 | method_preset_unit_files_with_mode, | |
3541 | SD_BUS_VTABLE_UNPRIVILEGED), | |
3542 | SD_BUS_METHOD_WITH_ARGS("MaskUnitFiles", | |
3543 | SD_BUS_ARGS("as", files, "b", runtime, "b", force), | |
3544 | SD_BUS_RESULT("a(sss)", changes), | |
3545 | method_mask_unit_files, | |
3546 | SD_BUS_VTABLE_UNPRIVILEGED), | |
3547 | SD_BUS_METHOD_WITH_ARGS("UnmaskUnitFiles", | |
3548 | SD_BUS_ARGS("as", files, "b", runtime), | |
3549 | SD_BUS_RESULT("a(sss)", changes), | |
3550 | method_unmask_unit_files, | |
3551 | SD_BUS_VTABLE_UNPRIVILEGED), | |
3552 | SD_BUS_METHOD_WITH_ARGS("RevertUnitFiles", | |
3553 | SD_BUS_ARGS("as", files), | |
3554 | SD_BUS_RESULT("a(sss)", changes), | |
3555 | method_revert_unit_files, | |
3556 | SD_BUS_VTABLE_UNPRIVILEGED), | |
3557 | SD_BUS_METHOD_WITH_ARGS("SetDefaultTarget", | |
3558 | SD_BUS_ARGS("s", name, "b", force), | |
3559 | SD_BUS_RESULT("a(sss)", changes), | |
3560 | method_set_default_target, | |
3561 | SD_BUS_VTABLE_UNPRIVILEGED), | |
3562 | SD_BUS_METHOD_WITH_ARGS("GetDefaultTarget", | |
3563 | SD_BUS_NO_ARGS, | |
3564 | SD_BUS_RESULT("s", name), | |
3565 | method_get_default_target, | |
3566 | SD_BUS_VTABLE_UNPRIVILEGED), | |
3567 | SD_BUS_METHOD_WITH_ARGS("PresetAllUnitFiles", | |
3568 | SD_BUS_ARGS("s", mode, "b", runtime, "b", force), | |
3569 | SD_BUS_RESULT("a(sss)", changes), | |
3570 | method_preset_all_unit_files, | |
3571 | SD_BUS_VTABLE_UNPRIVILEGED), | |
3572 | SD_BUS_METHOD_WITH_ARGS("AddDependencyUnitFiles", | |
3573 | SD_BUS_ARGS("as", files, "s", target, "s", type, "b", runtime, "b", force), | |
3574 | SD_BUS_RESULT("a(sss)", changes), | |
3575 | method_add_dependency_unit_files, | |
3576 | SD_BUS_VTABLE_UNPRIVILEGED), | |
3577 | SD_BUS_METHOD_WITH_ARGS("GetUnitFileLinks", | |
3578 | SD_BUS_ARGS("s", name, "b", runtime), | |
3579 | SD_BUS_RESULT("as", links), | |
3580 | method_get_unit_file_links, | |
3581 | SD_BUS_VTABLE_UNPRIVILEGED), | |
3582 | SD_BUS_METHOD_WITH_ARGS("SetExitCode", | |
3583 | SD_BUS_ARGS("y", number), | |
3584 | SD_BUS_NO_RESULT, | |
3585 | method_set_exit_code, | |
3586 | SD_BUS_VTABLE_UNPRIVILEGED), | |
3587 | SD_BUS_METHOD_WITH_ARGS("LookupDynamicUserByName", | |
3588 | SD_BUS_ARGS("s", name), | |
3589 | SD_BUS_RESULT("u", uid), | |
3590 | method_lookup_dynamic_user_by_name, | |
3591 | SD_BUS_VTABLE_UNPRIVILEGED), | |
3592 | SD_BUS_METHOD_WITH_ARGS("LookupDynamicUserByUID", | |
3593 | SD_BUS_ARGS("u", uid), | |
3594 | SD_BUS_RESULT("s", name), | |
3595 | method_lookup_dynamic_user_by_uid, | |
3596 | SD_BUS_VTABLE_UNPRIVILEGED), | |
3597 | SD_BUS_METHOD_WITH_ARGS("GetDynamicUsers", | |
3598 | SD_BUS_NO_ARGS, | |
3599 | SD_BUS_RESULT("a(us)", users), | |
3600 | method_get_dynamic_users, | |
3601 | SD_BUS_VTABLE_UNPRIVILEGED), | |
2ea24611 LP |
3602 | SD_BUS_METHOD_WITH_ARGS("DumpUnitFileDescriptorStore", |
3603 | SD_BUS_ARGS("s", name), | |
3604 | SD_BUS_RESULT("a(suuutuusu)", entries), | |
3605 | method_dump_unit_descriptor_store, | |
3606 | SD_BUS_VTABLE_UNPRIVILEGED), | |
84c01612 MS |
3607 | SD_BUS_METHOD_WITH_ARGS("StartAuxiliaryScope", |
3608 | SD_BUS_ARGS("s", name, "ah", pidfds, "t", flags, "a(sv)", properties), | |
3609 | SD_BUS_RESULT("o", job), | |
3610 | method_start_aux_scope, | |
3611 | SD_BUS_VTABLE_UNPRIVILEGED), | |
a008b6d7 | 3612 | |
3613 | SD_BUS_SIGNAL_WITH_ARGS("UnitNew", | |
3614 | SD_BUS_ARGS("s", id, "o", unit), | |
3615 | 0), | |
3616 | SD_BUS_SIGNAL_WITH_ARGS("UnitRemoved", | |
3617 | SD_BUS_ARGS("s", id, "o", unit), | |
3618 | 0), | |
3619 | SD_BUS_SIGNAL_WITH_ARGS("JobNew", | |
3620 | SD_BUS_ARGS("u", id, "o", job, "s", unit), | |
3621 | 0), | |
3622 | SD_BUS_SIGNAL_WITH_ARGS("JobRemoved", | |
3623 | SD_BUS_ARGS("u", id, "o", job, "s", unit, "s", result), | |
3624 | 0), | |
3625 | SD_BUS_SIGNAL_WITH_ARGS("StartupFinished", | |
3626 | SD_BUS_ARGS("t", firmware, "t", loader, "t", kernel, "t", initrd, "t", userspace, "t", total), | |
3627 | 0), | |
718db961 | 3628 | SD_BUS_SIGNAL("UnitFilesChanged", NULL, 0), |
a008b6d7 | 3629 | SD_BUS_SIGNAL_WITH_ARGS("Reloading", |
3630 | SD_BUS_ARGS("b", active), | |
3631 | 0), | |
718db961 LP |
3632 | |
3633 | SD_BUS_VTABLE_END | |
ea430986 | 3634 | }; |
718db961 | 3635 | |
893f801d LP |
3636 | const sd_bus_vtable bus_manager_log_control_vtable[] = { |
3637 | SD_BUS_VTABLE_START(0), | |
3638 | ||
3639 | /* We define a private version of this interface here, since we want slightly different | |
3640 | * implementations for the setters. We'll still use the generic getters however, and we share the | |
3641 | * setters with the implementations for the Manager interface above (which pre-dates the generic | |
3642 | * service API interface). */ | |
3643 | ||
3644 | SD_BUS_WRITABLE_PROPERTY("LogLevel", "s", bus_property_get_log_level, property_set_log_level, 0, 0), | |
3645 | SD_BUS_WRITABLE_PROPERTY("LogTarget", "s", bus_property_get_log_target, property_set_log_target, 0, 0), | |
3646 | SD_BUS_PROPERTY("SyslogIdentifier", "s", bus_property_get_syslog_identifier, 0, 0), | |
3647 | ||
3648 | SD_BUS_VTABLE_END, | |
3649 | }; | |
3650 | ||
8f8f05a9 | 3651 | static int send_finished(sd_bus *bus, void *userdata) { |
4afd3348 | 3652 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *message = NULL; |
99534007 | 3653 | usec_t *times = ASSERT_PTR(userdata); |
718db961 LP |
3654 | int r; |
3655 | ||
3656 | assert(bus); | |
718db961 | 3657 | |
cb31470f ZJS |
3658 | r = sd_bus_message_new_signal(bus, |
3659 | &message, | |
3660 | "/org/freedesktop/systemd1", | |
3661 | "org.freedesktop.systemd1.Manager", | |
3662 | "StartupFinished"); | |
718db961 LP |
3663 | if (r < 0) |
3664 | return r; | |
3665 | ||
3666 | r = sd_bus_message_append(message, "tttttt", times[0], times[1], times[2], times[3], times[4], times[5]); | |
3667 | if (r < 0) | |
3668 | return r; | |
3669 | ||
8f8f05a9 | 3670 | return sd_bus_send(bus, message, NULL); |
718db961 LP |
3671 | } |
3672 | ||
39abcaee | 3673 | void bus_manager_send_finished( |
718db961 LP |
3674 | Manager *m, |
3675 | usec_t firmware_usec, | |
3676 | usec_t loader_usec, | |
3677 | usec_t kernel_usec, | |
3678 | usec_t initrd_usec, | |
3679 | usec_t userspace_usec, | |
3680 | usec_t total_usec) { | |
3681 | ||
39abcaee LP |
3682 | int r; |
3683 | ||
718db961 LP |
3684 | assert(m); |
3685 | ||
8f8f05a9 LP |
3686 | r = bus_foreach_bus( |
3687 | m, | |
3688 | NULL, | |
3689 | send_finished, | |
3690 | (usec_t[6]) { | |
3691 | firmware_usec, | |
3692 | loader_usec, | |
3693 | kernel_usec, | |
3694 | initrd_usec, | |
3695 | userspace_usec, | |
3696 | total_usec | |
3697 | }); | |
39abcaee | 3698 | if (r < 0) |
da927ba9 | 3699 | log_debug_errno(r, "Failed to send finished signal: %m"); |
718db961 LP |
3700 | } |
3701 | ||
8f8f05a9 | 3702 | static int send_reloading(sd_bus *bus, void *userdata) { |
4afd3348 | 3703 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *message = NULL; |
718db961 LP |
3704 | int r; |
3705 | ||
3706 | assert(bus); | |
3707 | ||
151b9b96 | 3708 | r = sd_bus_message_new_signal(bus, &message, "/org/freedesktop/systemd1", "org.freedesktop.systemd1.Manager", "Reloading"); |
718db961 LP |
3709 | if (r < 0) |
3710 | return r; | |
3711 | ||
3712 | r = sd_bus_message_append(message, "b", PTR_TO_INT(userdata)); | |
3713 | if (r < 0) | |
3714 | return r; | |
3715 | ||
8f8f05a9 | 3716 | return sd_bus_send(bus, message, NULL); |
718db961 LP |
3717 | } |
3718 | ||
39abcaee LP |
3719 | void bus_manager_send_reloading(Manager *m, bool active) { |
3720 | int r; | |
3721 | ||
718db961 LP |
3722 | assert(m); |
3723 | ||
8f8f05a9 | 3724 | r = bus_foreach_bus(m, NULL, send_reloading, INT_TO_PTR(active)); |
39abcaee | 3725 | if (r < 0) |
da927ba9 | 3726 | log_debug_errno(r, "Failed to send reloading signal: %m"); |
03455c28 LDM |
3727 | } |
3728 | ||
3729 | static int send_changed_signal(sd_bus *bus, void *userdata) { | |
3730 | assert(bus); | |
3731 | ||
3732 | return sd_bus_emit_properties_changed_strv(bus, | |
3733 | "/org/freedesktop/systemd1", | |
3734 | "org.freedesktop.systemd1.Manager", | |
3735 | NULL); | |
3736 | } | |
39abcaee | 3737 | |
03455c28 LDM |
3738 | void bus_manager_send_change_signal(Manager *m) { |
3739 | int r; | |
3740 | ||
3741 | assert(m); | |
3742 | ||
3743 | r = bus_foreach_bus(m, NULL, send_changed_signal, NULL); | |
3744 | if (r < 0) | |
3745 | log_debug_errno(r, "Failed to send manager change signal: %m"); | |
718db961 | 3746 | } |