]>
Commit | Line | Data |
---|---|---|
db9ecf05 | 1 | /* SPDX-License-Identifier: LGPL-2.1-or-later */ |
1ee306e1 LP |
2 | |
3 | #include <errno.h> | |
1ee306e1 | 4 | #include <unistd.h> |
1ee306e1 | 5 | |
c3350683 | 6 | #include "sd-id128.h" |
3ffd4af2 | 7 | |
b5efdb8a | 8 | #include "alloc-util.h" |
3ffd4af2 | 9 | #include "btrfs-util.h" |
96aad8d1 | 10 | #include "bus-common-errors.h" |
40af3d02 | 11 | #include "bus-get-properties.h" |
9b71e4ab | 12 | #include "bus-locator.h" |
269e4d2d | 13 | #include "bus-polkit.h" |
23c80348 | 14 | #include "cgroup-util.h" |
57f1b61b | 15 | #include "discover-image.h" |
66855de7 | 16 | #include "errno-util.h" |
3ffd4af2 | 17 | #include "fd-util.h" |
03c2b288 | 18 | #include "fileio.h" |
f97b34a6 | 19 | #include "format-util.h" |
25300b5a | 20 | #include "hostname-util.h" |
3ffd4af2 | 21 | #include "image-dbus.h" |
a90fb858 | 22 | #include "io-util.h" |
3ffd4af2 | 23 | #include "machine-dbus.h" |
4cee5eed | 24 | #include "machine-pool.h" |
c3350683 | 25 | #include "machined.h" |
204f52e3 | 26 | #include "missing_capability.h" |
6ef06723 | 27 | #include "os-util.h" |
3ffd4af2 LP |
28 | #include "path-util.h" |
29 | #include "process-util.h" | |
15a5e950 | 30 | #include "stdio-util.h" |
3ffd4af2 | 31 | #include "strv.h" |
e4de7287 | 32 | #include "tmpfile-util.h" |
3ffd4af2 | 33 | #include "unit-name.h" |
b1d4f8e1 | 34 | #include "user-util.h" |
1ee306e1 | 35 | |
74c308ae | 36 | static BUS_DEFINE_PROPERTY_GET_GLOBAL(property_get_pool_path, "s", "/var/lib/machines"); |
160e3793 LP |
37 | |
38 | static int property_get_pool_usage( | |
39 | sd_bus *bus, | |
40 | const char *path, | |
41 | const char *interface, | |
42 | const char *property, | |
43 | sd_bus_message *reply, | |
44 | void *userdata, | |
45 | sd_bus_error *error) { | |
46 | ||
47 | _cleanup_close_ int fd = -1; | |
f5fbe71d | 48 | uint64_t usage = UINT64_MAX; |
160e3793 LP |
49 | |
50 | assert(bus); | |
51 | assert(reply); | |
52 | ||
160e3793 LP |
53 | fd = open("/var/lib/machines", O_RDONLY|O_CLOEXEC|O_DIRECTORY); |
54 | if (fd >= 0) { | |
55 | BtrfsQuotaInfo q; | |
56 | ||
5bcd08db | 57 | if (btrfs_subvol_get_subtree_quota_fd(fd, 0, &q) >= 0) |
cb81cd80 | 58 | usage = q.referenced; |
160e3793 LP |
59 | } |
60 | ||
160e3793 LP |
61 | return sd_bus_message_append(reply, "t", usage); |
62 | } | |
63 | ||
64 | static int property_get_pool_limit( | |
65 | sd_bus *bus, | |
66 | const char *path, | |
67 | const char *interface, | |
68 | const char *property, | |
69 | sd_bus_message *reply, | |
70 | void *userdata, | |
71 | sd_bus_error *error) { | |
72 | ||
73 | _cleanup_close_ int fd = -1; | |
f5fbe71d | 74 | uint64_t size = UINT64_MAX; |
160e3793 LP |
75 | |
76 | assert(bus); | |
77 | assert(reply); | |
78 | ||
160e3793 LP |
79 | fd = open("/var/lib/machines", O_RDONLY|O_CLOEXEC|O_DIRECTORY); |
80 | if (fd >= 0) { | |
81 | BtrfsQuotaInfo q; | |
82 | ||
5bcd08db | 83 | if (btrfs_subvol_get_subtree_quota_fd(fd, 0, &q) >= 0) |
cb81cd80 | 84 | size = q.referenced_max; |
160e3793 LP |
85 | } |
86 | ||
160e3793 LP |
87 | return sd_bus_message_append(reply, "t", size); |
88 | } | |
89 | ||
19070062 | 90 | static int method_get_machine(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
c3350683 | 91 | _cleanup_free_ char *p = NULL; |
99534007 | 92 | Manager *m = ASSERT_PTR(userdata); |
c3350683 LP |
93 | Machine *machine; |
94 | const char *name; | |
95 | int r; | |
96 | ||
c3350683 | 97 | assert(message); |
c3350683 LP |
98 | |
99 | r = sd_bus_message_read(message, "s", &name); | |
100 | if (r < 0) | |
ebcf1f97 | 101 | return r; |
c3350683 LP |
102 | |
103 | machine = hashmap_get(m->machines, name); | |
104 | if (!machine) | |
ebcf1f97 | 105 | return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_MACHINE, "No machine '%s' known", name); |
c3350683 LP |
106 | |
107 | p = machine_bus_path(machine); | |
108 | if (!p) | |
ebcf1f97 | 109 | return -ENOMEM; |
c3350683 | 110 | |
df2d202e | 111 | return sd_bus_reply_method_return(message, "o", p); |
c3350683 LP |
112 | } |
113 | ||
19070062 | 114 | static int method_get_image(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
c2ce6a3d | 115 | _cleanup_free_ char *p = NULL; |
99534007 | 116 | _unused_ Manager *m = ASSERT_PTR(userdata); |
c2ce6a3d LP |
117 | const char *name; |
118 | int r; | |
119 | ||
c2ce6a3d | 120 | assert(message); |
c2ce6a3d LP |
121 | |
122 | r = sd_bus_message_read(message, "s", &name); | |
123 | if (r < 0) | |
124 | return r; | |
125 | ||
d577d4a4 | 126 | r = image_find(IMAGE_MACHINE, name, NULL, NULL); |
3a6ce860 | 127 | if (r == -ENOENT) |
c2ce6a3d LP |
128 | return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_IMAGE, "No image '%s' known", name); |
129 | if (r < 0) | |
130 | return r; | |
131 | ||
132 | p = image_bus_path(name); | |
133 | if (!p) | |
134 | return -ENOMEM; | |
135 | ||
136 | return sd_bus_reply_method_return(message, "o", p); | |
137 | } | |
138 | ||
19070062 | 139 | static int method_get_machine_by_pid(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
c3350683 | 140 | _cleanup_free_ char *p = NULL; |
99534007 | 141 | Manager *m = ASSERT_PTR(userdata); |
c3350683 | 142 | Machine *machine = NULL; |
4e724d9c | 143 | pid_t pid; |
c3350683 LP |
144 | int r; |
145 | ||
c3350683 | 146 | assert(message); |
c3350683 | 147 | |
4e724d9c LP |
148 | assert_cc(sizeof(pid_t) == sizeof(uint32_t)); |
149 | ||
c3350683 LP |
150 | r = sd_bus_message_read(message, "u", &pid); |
151 | if (r < 0) | |
ebcf1f97 | 152 | return r; |
c3350683 | 153 | |
07b38ba5 | 154 | if (pid < 0) |
06820eaf LP |
155 | return -EINVAL; |
156 | ||
4e724d9c | 157 | if (pid == 0) { |
4afd3348 | 158 | _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL; |
5b12334d LP |
159 | |
160 | r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_PID, &creds); | |
161 | if (r < 0) | |
162 | return r; | |
163 | ||
164 | r = sd_bus_creds_get_pid(creds, &pid); | |
4e724d9c | 165 | if (r < 0) |
ebcf1f97 | 166 | return r; |
4e724d9c LP |
167 | } |
168 | ||
c3350683 LP |
169 | r = manager_get_machine_by_pid(m, pid, &machine); |
170 | if (r < 0) | |
ebcf1f97 | 171 | return r; |
c3350683 | 172 | if (!machine) |
de0671ee | 173 | return sd_bus_error_setf(error, BUS_ERROR_NO_MACHINE_FOR_PID, "PID "PID_FMT" does not belong to any known machine", pid); |
c3350683 LP |
174 | |
175 | p = machine_bus_path(machine); | |
176 | if (!p) | |
ebcf1f97 | 177 | return -ENOMEM; |
c3350683 | 178 | |
df2d202e | 179 | return sd_bus_reply_method_return(message, "o", p); |
c3350683 LP |
180 | } |
181 | ||
19070062 | 182 | static int method_list_machines(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
4afd3348 | 183 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; |
99534007 | 184 | Manager *m = ASSERT_PTR(userdata); |
c3350683 | 185 | Machine *machine; |
c3350683 LP |
186 | int r; |
187 | ||
c3350683 | 188 | assert(message); |
c3350683 | 189 | |
df2d202e | 190 | r = sd_bus_message_new_method_return(message, &reply); |
c3350683 | 191 | if (r < 0) |
ebcf1f97 | 192 | return sd_bus_error_set_errno(error, r); |
c3350683 LP |
193 | |
194 | r = sd_bus_message_open_container(reply, 'a', "(ssso)"); | |
195 | if (r < 0) | |
ebcf1f97 | 196 | return sd_bus_error_set_errno(error, r); |
c3350683 | 197 | |
90e74a66 | 198 | HASHMAP_FOREACH(machine, m->machines) { |
c3350683 LP |
199 | _cleanup_free_ char *p = NULL; |
200 | ||
201 | p = machine_bus_path(machine); | |
202 | if (!p) | |
ebcf1f97 | 203 | return -ENOMEM; |
c3350683 LP |
204 | |
205 | r = sd_bus_message_append(reply, "(ssso)", | |
206 | machine->name, | |
207 | strempty(machine_class_to_string(machine->class)), | |
208 | machine->service, | |
209 | p); | |
210 | if (r < 0) | |
ebcf1f97 | 211 | return sd_bus_error_set_errno(error, r); |
c3350683 | 212 | } |
1ee306e1 | 213 | |
c3350683 LP |
214 | r = sd_bus_message_close_container(reply); |
215 | if (r < 0) | |
ebcf1f97 | 216 | return sd_bus_error_set_errno(error, r); |
c3350683 | 217 | |
9030ca46 | 218 | return sd_bus_send(NULL, reply, NULL); |
c3350683 LP |
219 | } |
220 | ||
9b5ed6fe | 221 | static int method_create_or_register_machine(Manager *manager, sd_bus_message *message, bool read_network, Machine **_m, sd_bus_error *error) { |
8aec412f | 222 | const char *name, *service, *class, *root_directory; |
9b5ed6fe | 223 | const int32_t *netif = NULL; |
1ee306e1 LP |
224 | MachineClass c; |
225 | uint32_t leader; | |
226 | sd_id128_t id; | |
c3350683 | 227 | const void *v; |
1ee306e1 | 228 | Machine *m; |
9b5ed6fe | 229 | size_t n, n_netif = 0; |
c3350683 | 230 | int r; |
1ee306e1 | 231 | |
c3350683 | 232 | assert(manager); |
89f7c846 LP |
233 | assert(message); |
234 | assert(_m); | |
1ee306e1 | 235 | |
c3350683 LP |
236 | r = sd_bus_message_read(message, "s", &name); |
237 | if (r < 0) | |
ebcf1f97 | 238 | return r; |
52ef5dd7 | 239 | if (!hostname_is_valid(name, 0)) |
1b09b81c | 240 | return sd_bus_error_set(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid machine name"); |
1ee306e1 | 241 | |
c3350683 LP |
242 | r = sd_bus_message_read_array(message, 'y', &v, &n); |
243 | if (r < 0) | |
ebcf1f97 | 244 | return r; |
1ee306e1 LP |
245 | if (n == 0) |
246 | id = SD_ID128_NULL; | |
247 | else if (n == 16) | |
248 | memcpy(&id, v, n); | |
249 | else | |
1b09b81c | 250 | return sd_bus_error_set(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid machine ID parameter"); |
1ee306e1 | 251 | |
c3350683 LP |
252 | r = sd_bus_message_read(message, "ssus", &service, &class, &leader, &root_directory); |
253 | if (r < 0) | |
ebcf1f97 | 254 | return r; |
1ee306e1 | 255 | |
9b5ed6fe | 256 | if (read_network) { |
9b5ed6fe LP |
257 | r = sd_bus_message_read_array(message, 'i', (const void**) &netif, &n_netif); |
258 | if (r < 0) | |
259 | return r; | |
260 | ||
261 | n_netif /= sizeof(int32_t); | |
262 | ||
d007c583 | 263 | for (size_t i = 0; i < n_netif; i++) { |
9b5ed6fe LP |
264 | if (netif[i] <= 0) |
265 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid network interface index %i", netif[i]); | |
266 | } | |
267 | } | |
268 | ||
1ee306e1 LP |
269 | if (isempty(class)) |
270 | c = _MACHINE_CLASS_INVALID; | |
271 | else { | |
272 | c = machine_class_from_string(class); | |
273 | if (c < 0) | |
1b09b81c | 274 | return sd_bus_error_set(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid machine class parameter"); |
1ee306e1 LP |
275 | } |
276 | ||
c3350683 | 277 | if (leader == 1) |
1b09b81c | 278 | return sd_bus_error_set(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid leader PID"); |
1ee306e1 | 279 | |
c3350683 | 280 | if (!isempty(root_directory) && !path_is_absolute(root_directory)) |
1b09b81c | 281 | return sd_bus_error_set(error, SD_BUS_ERROR_INVALID_ARGS, "Root directory must be empty or an absolute path"); |
1ee306e1 | 282 | |
c3350683 | 283 | if (leader == 0) { |
4afd3348 | 284 | _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL; |
5b12334d LP |
285 | |
286 | r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_PID, &creds); | |
287 | if (r < 0) | |
288 | return r; | |
289 | ||
c3350683 | 290 | assert_cc(sizeof(uint32_t) == sizeof(pid_t)); |
554604b3 | 291 | |
5b12334d | 292 | r = sd_bus_creds_get_pid(creds, (pid_t*) &leader); |
c3350683 | 293 | if (r < 0) |
ebcf1f97 | 294 | return r; |
c3350683 | 295 | } |
554604b3 | 296 | |
1ee306e1 | 297 | if (hashmap_get(manager->machines, name)) |
ebcf1f97 | 298 | return sd_bus_error_setf(error, BUS_ERROR_MACHINE_EXISTS, "Machine '%s' already exists", name); |
1ee306e1 LP |
299 | |
300 | r = manager_add_machine(manager, name, &m); | |
301 | if (r < 0) | |
ebcf1f97 | 302 | return r; |
1ee306e1 LP |
303 | |
304 | m->leader = leader; | |
305 | m->class = c; | |
306 | m->id = id; | |
307 | ||
308 | if (!isempty(service)) { | |
309 | m->service = strdup(service); | |
310 | if (!m->service) { | |
ebcf1f97 | 311 | r = -ENOMEM; |
1ee306e1 LP |
312 | goto fail; |
313 | } | |
314 | } | |
315 | ||
316 | if (!isempty(root_directory)) { | |
317 | m->root_directory = strdup(root_directory); | |
318 | if (!m->root_directory) { | |
ebcf1f97 | 319 | r = -ENOMEM; |
1ee306e1 LP |
320 | goto fail; |
321 | } | |
322 | } | |
323 | ||
9b5ed6fe LP |
324 | if (n_netif > 0) { |
325 | assert_cc(sizeof(int32_t) == sizeof(int)); | |
326 | m->netif = memdup(netif, sizeof(int32_t) * n_netif); | |
327 | if (!m->netif) { | |
328 | r = -ENOMEM; | |
329 | goto fail; | |
330 | } | |
331 | ||
332 | m->n_netif = n_netif; | |
333 | } | |
334 | ||
89f7c846 LP |
335 | *_m = m; |
336 | ||
337 | return 1; | |
338 | ||
339 | fail: | |
340 | machine_add_to_gc_queue(m); | |
341 | return r; | |
342 | } | |
343 | ||
19070062 | 344 | static int method_create_machine_internal(sd_bus_message *message, bool read_network, void *userdata, sd_bus_error *error) { |
99534007 | 345 | Manager *manager = ASSERT_PTR(userdata); |
89f7c846 LP |
346 | Machine *m = NULL; |
347 | int r; | |
348 | ||
19070062 | 349 | assert(message); |
19070062 | 350 | |
9b5ed6fe | 351 | r = method_create_or_register_machine(manager, message, read_network, &m, error); |
89f7c846 LP |
352 | if (r < 0) |
353 | return r; | |
354 | ||
355 | r = sd_bus_message_enter_container(message, 'a', "(sv)"); | |
356 | if (r < 0) | |
357 | goto fail; | |
358 | ||
ebcf1f97 LP |
359 | r = machine_start(m, message, error); |
360 | if (r < 0) | |
1ee306e1 LP |
361 | goto fail; |
362 | ||
c3350683 | 363 | m->create_message = sd_bus_message_ref(message); |
c3350683 | 364 | return 1; |
1ee306e1 LP |
365 | |
366 | fail: | |
a3e7f417 | 367 | machine_add_to_gc_queue(m); |
89f7c846 LP |
368 | return r; |
369 | } | |
1ee306e1 | 370 | |
19070062 LP |
371 | static int method_create_machine_with_network(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
372 | return method_create_machine_internal(message, true, userdata, error); | |
9b5ed6fe LP |
373 | } |
374 | ||
19070062 LP |
375 | static int method_create_machine(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
376 | return method_create_machine_internal(message, false, userdata, error); | |
9b5ed6fe LP |
377 | } |
378 | ||
19070062 | 379 | static int method_register_machine_internal(sd_bus_message *message, bool read_network, void *userdata, sd_bus_error *error) { |
99534007 | 380 | Manager *manager = ASSERT_PTR(userdata); |
89f7c846 LP |
381 | _cleanup_free_ char *p = NULL; |
382 | Machine *m = NULL; | |
383 | int r; | |
384 | ||
19070062 | 385 | assert(message); |
19070062 | 386 | |
9b5ed6fe | 387 | r = method_create_or_register_machine(manager, message, read_network, &m, error); |
89f7c846 LP |
388 | if (r < 0) |
389 | return r; | |
390 | ||
391 | r = cg_pid_get_unit(m->leader, &m->unit); | |
392 | if (r < 0) { | |
048c386e ZJS |
393 | r = sd_bus_error_set_errnof(error, r, |
394 | "Failed to determine unit of process "PID_FMT" : %m", | |
395 | m->leader); | |
89f7c846 LP |
396 | goto fail; |
397 | } | |
398 | ||
399 | r = machine_start(m, NULL, error); | |
400 | if (r < 0) | |
401 | goto fail; | |
402 | ||
403 | p = machine_bus_path(m); | |
404 | if (!p) { | |
405 | r = -ENOMEM; | |
406 | goto fail; | |
407 | } | |
408 | ||
409 | return sd_bus_reply_method_return(message, "o", p); | |
410 | ||
411 | fail: | |
412 | machine_add_to_gc_queue(m); | |
1ee306e1 LP |
413 | return r; |
414 | } | |
415 | ||
19070062 LP |
416 | static int method_register_machine_with_network(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
417 | return method_register_machine_internal(message, true, userdata, error); | |
9b5ed6fe LP |
418 | } |
419 | ||
19070062 LP |
420 | static int method_register_machine(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
421 | return method_register_machine_internal(message, false, userdata, error); | |
9b5ed6fe LP |
422 | } |
423 | ||
eecf0181 | 424 | static int redirect_method_to_machine(sd_bus_message *message, Manager *m, sd_bus_error *error, sd_bus_message_handler_t method) { |
c3350683 LP |
425 | Machine *machine; |
426 | const char *name; | |
1ee306e1 LP |
427 | int r; |
428 | ||
1ee306e1 LP |
429 | assert(message); |
430 | assert(m); | |
eecf0181 | 431 | assert(method); |
1ee306e1 | 432 | |
c3350683 LP |
433 | r = sd_bus_message_read(message, "s", &name); |
434 | if (r < 0) | |
ebcf1f97 | 435 | return sd_bus_error_set_errno(error, r); |
1ee306e1 | 436 | |
c3350683 LP |
437 | machine = hashmap_get(m->machines, name); |
438 | if (!machine) | |
ebcf1f97 | 439 | return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_MACHINE, "No machine '%s' known", name); |
1ee306e1 | 440 | |
eecf0181 | 441 | return method(message, machine, error); |
c3350683 | 442 | } |
1ee306e1 | 443 | |
ef8ff92e ZJS |
444 | static int method_unregister_machine(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
445 | return redirect_method_to_machine(message, userdata, error, bus_machine_method_unregister); | |
446 | } | |
447 | ||
eecf0181 LP |
448 | static int method_terminate_machine(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
449 | return redirect_method_to_machine(message, userdata, error, bus_machine_method_terminate); | |
450 | } | |
1ee306e1 | 451 | |
eecf0181 LP |
452 | static int method_kill_machine(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
453 | return redirect_method_to_machine(message, userdata, error, bus_machine_method_kill); | |
878cd7e9 LP |
454 | } |
455 | ||
19070062 | 456 | static int method_get_machine_addresses(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
eecf0181 | 457 | return redirect_method_to_machine(message, userdata, error, bus_machine_method_get_addresses); |
c3350683 | 458 | } |
1ee306e1 | 459 | |
19070062 | 460 | static int method_get_machine_os_release(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
eecf0181 | 461 | return redirect_method_to_machine(message, userdata, error, bus_machine_method_get_os_release); |
717603e3 LP |
462 | } |
463 | ||
19070062 | 464 | static int method_list_images(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
4afd3348 | 465 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; |
b07ec5a1 | 466 | _cleanup_hashmap_free_ Hashmap *images = NULL; |
99534007 | 467 | _unused_ Manager *m = ASSERT_PTR(userdata); |
cd61c3bf | 468 | Image *image; |
cd61c3bf LP |
469 | int r; |
470 | ||
cd61c3bf | 471 | assert(message); |
cd61c3bf | 472 | |
b07ec5a1 | 473 | images = hashmap_new(&image_hash_ops); |
cd61c3bf LP |
474 | if (!images) |
475 | return -ENOMEM; | |
476 | ||
d577d4a4 | 477 | r = image_discover(IMAGE_MACHINE, NULL, images); |
cd61c3bf LP |
478 | if (r < 0) |
479 | return r; | |
480 | ||
481 | r = sd_bus_message_new_method_return(message, &reply); | |
482 | if (r < 0) | |
483 | return r; | |
484 | ||
b6b18498 | 485 | r = sd_bus_message_open_container(reply, 'a', "(ssbttto)"); |
cd61c3bf LP |
486 | if (r < 0) |
487 | return r; | |
488 | ||
90e74a66 | 489 | HASHMAP_FOREACH(image, images) { |
cd61c3bf LP |
490 | _cleanup_free_ char *p = NULL; |
491 | ||
492 | p = image_bus_path(image->name); | |
493 | if (!p) | |
494 | return -ENOMEM; | |
495 | ||
b6b18498 | 496 | r = sd_bus_message_append(reply, "(ssbttto)", |
cd61c3bf LP |
497 | image->name, |
498 | image_type_to_string(image->type), | |
499 | image->read_only, | |
10f9c755 LP |
500 | image->crtime, |
501 | image->mtime, | |
c19de711 | 502 | image->usage, |
cd61c3bf LP |
503 | p); |
504 | if (r < 0) | |
505 | return r; | |
506 | } | |
507 | ||
508 | r = sd_bus_message_close_container(reply); | |
509 | if (r < 0) | |
510 | return r; | |
511 | ||
9030ca46 | 512 | return sd_bus_send(NULL, reply, NULL); |
cd61c3bf LP |
513 | } |
514 | ||
19070062 | 515 | static int method_open_machine_pty(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
eecf0181 | 516 | return redirect_method_to_machine(message, userdata, error, bus_machine_method_open_pty); |
40205d70 LP |
517 | } |
518 | ||
19070062 | 519 | static int method_open_machine_login(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
eecf0181 | 520 | return redirect_method_to_machine(message, userdata, error, bus_machine_method_open_login); |
5f8cc96a LP |
521 | } |
522 | ||
49af9e13 | 523 | static int method_open_machine_shell(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
eecf0181 | 524 | return redirect_method_to_machine(message, userdata, error, bus_machine_method_open_shell); |
49af9e13 LP |
525 | } |
526 | ||
19070062 | 527 | static int method_bind_mount_machine(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
eecf0181 | 528 | return redirect_method_to_machine(message, userdata, error, bus_machine_method_bind_mount); |
90adaa25 LP |
529 | } |
530 | ||
19070062 | 531 | static int method_copy_machine(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
eecf0181 | 532 | return redirect_method_to_machine(message, userdata, error, bus_machine_method_copy); |
0370612e LP |
533 | } |
534 | ||
ae203207 | 535 | static int method_open_machine_root_directory(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
eecf0181 | 536 | return redirect_method_to_machine(message, userdata, error, bus_machine_method_open_root_directory); |
ae203207 LP |
537 | } |
538 | ||
3401419b | 539 | static int method_get_machine_uid_shift(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
eecf0181 | 540 | return redirect_method_to_machine(message, userdata, error, bus_machine_method_get_uid_shift); |
3401419b LP |
541 | } |
542 | ||
9b06c1e1 | 543 | static int redirect_method_to_image(sd_bus_message *message, Manager *m, sd_bus_error *error, sd_bus_message_handler_t method) { |
08682124 LP |
544 | _cleanup_(image_unrefp) Image* i = NULL; |
545 | const char *name; | |
546 | int r; | |
547 | ||
08682124 | 548 | assert(message); |
9b06c1e1 LP |
549 | assert(m); |
550 | assert(method); | |
08682124 LP |
551 | |
552 | r = sd_bus_message_read(message, "s", &name); | |
553 | if (r < 0) | |
554 | return r; | |
555 | ||
556 | if (!image_name_is_valid(name)) | |
557 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Image name '%s' is invalid.", name); | |
558 | ||
d577d4a4 | 559 | r = image_find(IMAGE_MACHINE, name, NULL, &i); |
3a6ce860 LP |
560 | if (r == -ENOENT) |
561 | return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_IMAGE, "No image '%s' known", name); | |
08682124 LP |
562 | if (r < 0) |
563 | return r; | |
08682124 | 564 | |
9b06c1e1 LP |
565 | i->userdata = m; |
566 | return method(message, i, error); | |
08682124 LP |
567 | } |
568 | ||
9b06c1e1 LP |
569 | static int method_remove_image(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
570 | return redirect_method_to_image(message, userdata, error, bus_image_method_remove); | |
571 | } | |
ebd93cb6 | 572 | |
9b06c1e1 LP |
573 | static int method_rename_image(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
574 | return redirect_method_to_image(message, userdata, error, bus_image_method_rename); | |
ebd93cb6 LP |
575 | } |
576 | ||
19070062 | 577 | static int method_clone_image(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
9b06c1e1 | 578 | return redirect_method_to_image(message, userdata, error, bus_image_method_clone); |
ebd93cb6 LP |
579 | } |
580 | ||
19070062 | 581 | static int method_mark_image_read_only(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
9b06c1e1 | 582 | return redirect_method_to_image(message, userdata, error, bus_image_method_mark_read_only); |
ebd93cb6 LP |
583 | } |
584 | ||
cf30a8c1 | 585 | static int method_get_image_hostname(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
9b06c1e1 | 586 | return redirect_method_to_image(message, userdata, error, bus_image_method_get_hostname); |
cf30a8c1 LP |
587 | } |
588 | ||
589 | static int method_get_image_machine_id(sd_bus_message *message, void *userdata, sd_bus_error *error) { | |
9b06c1e1 | 590 | return redirect_method_to_image(message, userdata, error, bus_image_method_get_machine_id); |
cf30a8c1 LP |
591 | } |
592 | ||
593 | static int method_get_image_machine_info(sd_bus_message *message, void *userdata, sd_bus_error *error) { | |
9b06c1e1 | 594 | return redirect_method_to_image(message, userdata, error, bus_image_method_get_machine_info); |
cf30a8c1 LP |
595 | } |
596 | ||
9153b02b | 597 | static int method_get_image_os_release(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
9b06c1e1 | 598 | return redirect_method_to_image(message, userdata, error, bus_image_method_get_os_release); |
9153b02b LP |
599 | } |
600 | ||
03c2b288 LP |
601 | static int clean_pool_done(Operation *operation, int ret, sd_bus_error *error) { |
602 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; | |
603 | _cleanup_fclose_ FILE *f = NULL; | |
604 | bool success; | |
605 | size_t n; | |
606 | int r; | |
607 | ||
608 | assert(operation); | |
609 | assert(operation->extra_fd >= 0); | |
610 | ||
611 | if (lseek(operation->extra_fd, 0, SEEK_SET) == (off_t) -1) | |
612 | return -errno; | |
613 | ||
4fa744a3 | 614 | f = take_fdopen(&operation->extra_fd, "r"); |
03c2b288 LP |
615 | if (!f) |
616 | return -errno; | |
617 | ||
03c2b288 LP |
618 | /* The resulting temporary file starts with a boolean value that indicates success or not. */ |
619 | errno = 0; | |
620 | n = fread(&success, 1, sizeof(success), f); | |
621 | if (n != sizeof(success)) | |
66855de7 | 622 | return ret < 0 ? ret : errno_or_else(EIO); |
03c2b288 LP |
623 | |
624 | if (ret < 0) { | |
625 | _cleanup_free_ char *name = NULL; | |
626 | ||
627 | /* The clean-up operation failed. In this case the resulting temporary file should contain a boolean | |
628 | * set to false followed by the name of the failed image. Let's try to read this and use it for the | |
629 | * error message. If we can't read it, don't mind, and return the naked error. */ | |
630 | ||
631 | if (success) /* The resulting temporary file could not be updated, ignore it. */ | |
632 | return ret; | |
633 | ||
41f11239 LP |
634 | r = read_nul_string(f, LONG_LINE_MAX, &name); |
635 | if (r <= 0) /* Same here... */ | |
03c2b288 LP |
636 | return ret; |
637 | ||
638 | return sd_bus_error_set_errnof(error, ret, "Failed to remove image %s: %m", name); | |
639 | } | |
640 | ||
641 | assert(success); | |
642 | ||
643 | r = sd_bus_message_new_method_return(operation->message, &reply); | |
644 | if (r < 0) | |
645 | return r; | |
646 | ||
647 | r = sd_bus_message_open_container(reply, 'a', "(st)"); | |
648 | if (r < 0) | |
649 | return r; | |
650 | ||
651 | /* On success the resulting temporary file will contain a list of image names that were removed followed by | |
652 | * their size on disk. Let's read that and turn it into a bus message. */ | |
653 | for (;;) { | |
654 | _cleanup_free_ char *name = NULL; | |
655 | uint64_t size; | |
656 | ||
41f11239 | 657 | r = read_nul_string(f, LONG_LINE_MAX, &name); |
03c2b288 LP |
658 | if (r < 0) |
659 | return r; | |
41f11239 | 660 | if (r == 0) /* reached the end */ |
03c2b288 LP |
661 | break; |
662 | ||
663 | errno = 0; | |
664 | n = fread(&size, 1, sizeof(size), f); | |
665 | if (n != sizeof(size)) | |
66855de7 | 666 | return errno_or_else(EIO); |
03c2b288 LP |
667 | |
668 | r = sd_bus_message_append(reply, "(st)", name, size); | |
669 | if (r < 0) | |
670 | return r; | |
671 | } | |
672 | ||
673 | r = sd_bus_message_close_container(reply); | |
674 | if (r < 0) | |
675 | return r; | |
676 | ||
677 | return sd_bus_send(NULL, reply, NULL); | |
678 | } | |
679 | ||
d94c2b06 LP |
680 | static int method_clean_pool(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
681 | enum { | |
682 | REMOVE_ALL, | |
683 | REMOVE_HIDDEN, | |
684 | } mode; | |
685 | ||
03c2b288 LP |
686 | _cleanup_close_pair_ int errno_pipe_fd[2] = { -1, -1 }; |
687 | _cleanup_close_ int result_fd = -1; | |
d94c2b06 | 688 | Manager *m = userdata; |
03c2b288 | 689 | Operation *operation; |
d94c2b06 | 690 | const char *mm; |
03c2b288 | 691 | pid_t child; |
d94c2b06 LP |
692 | int r; |
693 | ||
694 | assert(message); | |
695 | ||
03c2b288 | 696 | if (m->n_operations >= OPERATIONS_MAX) |
1b09b81c | 697 | return sd_bus_error_set(error, SD_BUS_ERROR_LIMITS_EXCEEDED, "Too many ongoing operations."); |
03c2b288 | 698 | |
d94c2b06 LP |
699 | r = sd_bus_message_read(message, "s", &mm); |
700 | if (r < 0) | |
701 | return r; | |
702 | ||
703 | if (streq(mm, "all")) | |
704 | mode = REMOVE_ALL; | |
705 | else if (streq(mm, "hidden")) | |
706 | mode = REMOVE_HIDDEN; | |
707 | else | |
708 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Unknown mode '%s'.", mm); | |
709 | ||
8dd3f6a3 LN |
710 | const char *details[] = { |
711 | "verb", "clean_pool", | |
712 | "mode", mm, | |
713 | NULL | |
714 | }; | |
715 | ||
d94c2b06 LP |
716 | r = bus_verify_polkit_async( |
717 | message, | |
718 | CAP_SYS_ADMIN, | |
719 | "org.freedesktop.machine1.manage-machines", | |
8dd3f6a3 | 720 | details, |
d94c2b06 LP |
721 | false, |
722 | UID_INVALID, | |
723 | &m->polkit_registry, | |
724 | error); | |
725 | if (r < 0) | |
726 | return r; | |
727 | if (r == 0) | |
728 | return 1; /* Will call us back */ | |
729 | ||
03c2b288 LP |
730 | if (pipe2(errno_pipe_fd, O_CLOEXEC|O_NONBLOCK) < 0) |
731 | return sd_bus_error_set_errnof(error, errno, "Failed to create pipe: %m"); | |
d94c2b06 | 732 | |
03c2b288 LP |
733 | /* Create a temporary file we can dump information about deleted images into. We use a temporary file for this |
734 | * instead of a pipe or so, since this might grow quit large in theory and we don't want to process this | |
595bfe7d | 735 | * continuously */ |
992e8f22 | 736 | result_fd = open_tmpfile_unlinkable(NULL, O_RDWR|O_CLOEXEC); |
03c2b288 LP |
737 | if (result_fd < 0) |
738 | return -errno; | |
d94c2b06 | 739 | |
03c2b288 | 740 | /* This might be a slow operation, run it asynchronously in a background process */ |
4c253ed1 LP |
741 | r = safe_fork("(sd-clean)", FORK_RESET_SIGNALS, &child); |
742 | if (r < 0) | |
743 | return sd_bus_error_set_errnof(error, r, "Failed to fork(): %m"); | |
744 | if (r == 0) { | |
b07ec5a1 | 745 | _cleanup_hashmap_free_ Hashmap *images = NULL; |
03c2b288 LP |
746 | bool success = true; |
747 | Image *image; | |
03c2b288 | 748 | ssize_t l; |
d94c2b06 | 749 | |
03c2b288 | 750 | errno_pipe_fd[0] = safe_close(errno_pipe_fd[0]); |
d94c2b06 | 751 | |
b07ec5a1 | 752 | images = hashmap_new(&image_hash_ops); |
03c2b288 LP |
753 | if (!images) { |
754 | r = -ENOMEM; | |
755 | goto child_fail; | |
756 | } | |
d94c2b06 | 757 | |
d577d4a4 | 758 | r = image_discover(IMAGE_MACHINE, NULL, images); |
03c2b288 LP |
759 | if (r < 0) |
760 | goto child_fail; | |
d94c2b06 | 761 | |
03c2b288 LP |
762 | l = write(result_fd, &success, sizeof(success)); |
763 | if (l < 0) { | |
764 | r = -errno; | |
765 | goto child_fail; | |
766 | } | |
d94c2b06 | 767 | |
90e74a66 | 768 | HASHMAP_FOREACH(image, images) { |
03c2b288 LP |
769 | |
770 | /* We can't remove vendor images (i.e. those in /usr) */ | |
771 | if (IMAGE_IS_VENDOR(image)) | |
772 | continue; | |
773 | ||
774 | if (IMAGE_IS_HOST(image)) | |
775 | continue; | |
776 | ||
777 | if (mode == REMOVE_HIDDEN && !IMAGE_IS_HIDDEN(image)) | |
778 | continue; | |
779 | ||
780 | r = image_remove(image); | |
781 | if (r == -EBUSY) /* keep images that are currently being used. */ | |
782 | continue; | |
783 | if (r < 0) { | |
784 | /* If the operation failed, let's override everything we wrote, and instead write there at which image we failed. */ | |
785 | success = false; | |
786 | (void) ftruncate(result_fd, 0); | |
787 | (void) lseek(result_fd, 0, SEEK_SET); | |
788 | (void) write(result_fd, &success, sizeof(success)); | |
789 | (void) write(result_fd, image->name, strlen(image->name)+1); | |
790 | goto child_fail; | |
791 | } | |
792 | ||
793 | l = write(result_fd, image->name, strlen(image->name)+1); | |
794 | if (l < 0) { | |
795 | r = -errno; | |
796 | goto child_fail; | |
797 | } | |
798 | ||
799 | l = write(result_fd, &image->usage_exclusive, sizeof(image->usage_exclusive)); | |
800 | if (l < 0) { | |
801 | r = -errno; | |
802 | goto child_fail; | |
803 | } | |
804 | } | |
d94c2b06 | 805 | |
03c2b288 LP |
806 | result_fd = safe_close(result_fd); |
807 | _exit(EXIT_SUCCESS); | |
808 | ||
809 | child_fail: | |
810 | (void) write(errno_pipe_fd[1], &r, sizeof(r)); | |
811 | _exit(EXIT_FAILURE); | |
d94c2b06 LP |
812 | } |
813 | ||
03c2b288 LP |
814 | errno_pipe_fd[1] = safe_close(errno_pipe_fd[1]); |
815 | ||
816 | /* The clean-up might take a while, hence install a watch on the child and return */ | |
817 | ||
818 | r = operation_new(m, NULL, child, message, errno_pipe_fd[0], &operation); | |
819 | if (r < 0) { | |
820 | (void) sigkill_wait(child); | |
d94c2b06 | 821 | return r; |
03c2b288 | 822 | } |
d94c2b06 | 823 | |
03c2b288 LP |
824 | operation->extra_fd = result_fd; |
825 | operation->done = clean_pool_done; | |
826 | ||
827 | result_fd = -1; | |
828 | errno_pipe_fd[0] = -1; | |
829 | ||
830 | return 1; | |
d94c2b06 LP |
831 | } |
832 | ||
19070062 | 833 | static int method_set_pool_limit(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
d6ce17c7 LP |
834 | Manager *m = userdata; |
835 | uint64_t limit; | |
836 | int r; | |
837 | ||
19070062 LP |
838 | assert(message); |
839 | ||
d6ce17c7 LP |
840 | r = sd_bus_message_read(message, "t", &limit); |
841 | if (r < 0) | |
842 | return r; | |
a90fb858 | 843 | if (!FILE_SIZE_VALID_OR_INFINITY(limit)) |
1b09b81c | 844 | return sd_bus_error_set(error, SD_BUS_ERROR_INVALID_ARGS, "New limit out of range"); |
d6ce17c7 | 845 | |
8dd3f6a3 LN |
846 | const char *details[] = { |
847 | "verb", "set_pool_limit", | |
848 | NULL | |
849 | }; | |
850 | ||
d6ce17c7 LP |
851 | r = bus_verify_polkit_async( |
852 | message, | |
853 | CAP_SYS_ADMIN, | |
854 | "org.freedesktop.machine1.manage-machines", | |
8dd3f6a3 | 855 | details, |
d6ce17c7 LP |
856 | false, |
857 | UID_INVALID, | |
858 | &m->polkit_registry, | |
859 | error); | |
860 | if (r < 0) | |
861 | return r; | |
862 | if (r == 0) | |
863 | return 1; /* Will call us back */ | |
864 | ||
4cee5eed | 865 | /* Set up the machine directory if necessary */ |
c7779a61 | 866 | r = setup_machine_directory(error, /* use_btrfs_subvol= */ true, /* use_btrfs_quota= */ true); |
4cee5eed LP |
867 | if (r < 0) |
868 | return r; | |
869 | ||
5bcd08db LP |
870 | (void) btrfs_qgroup_set_limit("/var/lib/machines", 0, limit); |
871 | ||
872 | r = btrfs_subvol_set_subtree_quota_limit("/var/lib/machines", 0, limit); | |
d6ce17c7 | 873 | if (r == -ENOTTY) |
1b09b81c | 874 | return sd_bus_error_set(error, SD_BUS_ERROR_NOT_SUPPORTED, "Quota is only supported on btrfs."); |
26166c88 | 875 | if (r < 0) |
d6ce17c7 LP |
876 | return sd_bus_error_set_errnof(error, r, "Failed to adjust quota limit: %m"); |
877 | ||
878 | return sd_bus_reply_method_return(message, NULL); | |
879 | } | |
880 | ||
19070062 | 881 | static int method_set_image_limit(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
9b06c1e1 | 882 | return redirect_method_to_image(message, userdata, error, bus_image_method_set_limit); |
d6ce17c7 LP |
883 | } |
884 | ||
c01ff965 | 885 | static int method_map_from_machine_user(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
c01ff965 | 886 | Manager *m = userdata; |
74d1b7d2 | 887 | const char *name; |
c01ff965 LP |
888 | Machine *machine; |
889 | uint32_t uid; | |
74d1b7d2 | 890 | uid_t converted; |
c01ff965 LP |
891 | int r; |
892 | ||
893 | r = sd_bus_message_read(message, "su", &name, &uid); | |
894 | if (r < 0) | |
895 | return r; | |
896 | ||
c077529b | 897 | if (!uid_is_valid(uid)) |
c01ff965 LP |
898 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid user ID " UID_FMT, uid); |
899 | ||
900 | machine = hashmap_get(m->machines, name); | |
901 | if (!machine) | |
902 | return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_MACHINE, "No machine '%s' known", name); | |
903 | ||
a79366e2 | 904 | if (machine->class != MACHINE_CONTAINER) |
1b09b81c | 905 | return sd_bus_error_set(error, SD_BUS_ERROR_INVALID_ARGS, "Not supported for non-container machines."); |
a79366e2 | 906 | |
74d1b7d2 LP |
907 | r = machine_translate_uid(machine, uid, &converted); |
908 | if (r == -ESRCH) | |
909 | return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_USER_MAPPING, "Machine '%s' has no matching user mappings.", name); | |
910 | if (r < 0) | |
911 | return r; | |
c01ff965 | 912 | |
74d1b7d2 | 913 | return sd_bus_reply_method_return(message, "u", (uint32_t) converted); |
c01ff965 LP |
914 | } |
915 | ||
916 | static int method_map_to_machine_user(sd_bus_message *message, void *userdata, sd_bus_error *error) { | |
74d1b7d2 | 917 | _cleanup_free_ char *o = NULL; |
c01ff965 LP |
918 | Manager *m = userdata; |
919 | Machine *machine; | |
74d1b7d2 | 920 | uid_t uid, converted; |
c01ff965 LP |
921 | int r; |
922 | ||
923 | r = sd_bus_message_read(message, "u", &uid); | |
924 | if (r < 0) | |
925 | return r; | |
c077529b | 926 | if (!uid_is_valid(uid)) |
c01ff965 LP |
927 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid user ID " UID_FMT, uid); |
928 | if (uid < 0x10000) | |
929 | return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_USER_MAPPING, "User " UID_FMT " belongs to host UID range", uid); | |
930 | ||
74d1b7d2 LP |
931 | r = manager_find_machine_for_uid(m, uid, &machine, &converted); |
932 | if (r < 0) | |
933 | return r; | |
934 | if (!r) | |
935 | return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_USER_MAPPING, "No matching user mapping for " UID_FMT ".", uid); | |
c01ff965 | 936 | |
74d1b7d2 LP |
937 | o = machine_bus_path(machine); |
938 | if (!o) | |
939 | return -ENOMEM; | |
c01ff965 | 940 | |
74d1b7d2 | 941 | return sd_bus_reply_method_return(message, "sou", machine->name, o, (uint32_t) converted); |
c01ff965 LP |
942 | } |
943 | ||
74d1b7d2 LP |
944 | static int method_map_from_machine_group(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
945 | Manager *m = userdata; | |
946 | const char *name; | |
c01ff965 | 947 | Machine *machine; |
74d1b7d2 | 948 | gid_t converted; |
c01ff965 LP |
949 | uint32_t gid; |
950 | int r; | |
951 | ||
952 | r = sd_bus_message_read(message, "su", &name, &gid); | |
953 | if (r < 0) | |
954 | return r; | |
955 | ||
c077529b | 956 | if (!gid_is_valid(gid)) |
c01ff965 LP |
957 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid group ID " GID_FMT, gid); |
958 | ||
959 | machine = hashmap_get(m->machines, name); | |
960 | if (!machine) | |
961 | return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_MACHINE, "No machine '%s' known", name); | |
962 | ||
a79366e2 | 963 | if (machine->class != MACHINE_CONTAINER) |
1b09b81c | 964 | return sd_bus_error_set(error, SD_BUS_ERROR_INVALID_ARGS, "Not supported for non-container machines."); |
a79366e2 | 965 | |
74d1b7d2 LP |
966 | r = machine_translate_gid(machine, gid, &converted); |
967 | if (r == -ESRCH) | |
968 | return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_USER_MAPPING, "Machine '%s' has no matching group mappings.", name); | |
969 | if (r < 0) | |
970 | return r; | |
c01ff965 | 971 | |
74d1b7d2 | 972 | return sd_bus_reply_method_return(message, "u", (uint32_t) converted); |
c01ff965 LP |
973 | } |
974 | ||
74d1b7d2 LP |
975 | static int method_map_to_machine_group(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
976 | _cleanup_free_ char *o = NULL; | |
977 | Manager *m = userdata; | |
c01ff965 | 978 | Machine *machine; |
74d1b7d2 | 979 | gid_t gid, converted; |
c01ff965 LP |
980 | int r; |
981 | ||
982 | r = sd_bus_message_read(message, "u", &gid); | |
983 | if (r < 0) | |
984 | return r; | |
c077529b | 985 | if (!gid_is_valid(gid)) |
c01ff965 LP |
986 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid group ID " GID_FMT, gid); |
987 | if (gid < 0x10000) | |
988 | return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_GROUP_MAPPING, "Group " GID_FMT " belongs to host GID range", gid); | |
989 | ||
74d1b7d2 LP |
990 | r = manager_find_machine_for_gid(m, gid, &machine, &converted); |
991 | if (r < 0) | |
992 | return r; | |
993 | if (!r) | |
994 | return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_GROUP_MAPPING, "No matching group mapping for " GID_FMT ".", gid); | |
c01ff965 | 995 | |
74d1b7d2 LP |
996 | o = machine_bus_path(machine); |
997 | if (!o) | |
998 | return -ENOMEM; | |
c01ff965 | 999 | |
74d1b7d2 | 1000 | return sd_bus_reply_method_return(message, "sou", machine->name, o, (uint32_t) converted); |
c01ff965 LP |
1001 | } |
1002 | ||
c3350683 LP |
1003 | const sd_bus_vtable manager_vtable[] = { |
1004 | SD_BUS_VTABLE_START(0), | |
bbe17ca1 | 1005 | |
160e3793 LP |
1006 | SD_BUS_PROPERTY("PoolPath", "s", property_get_pool_path, 0, 0), |
1007 | SD_BUS_PROPERTY("PoolUsage", "t", property_get_pool_usage, 0, 0), | |
1008 | SD_BUS_PROPERTY("PoolLimit", "t", property_get_pool_limit, 0, 0), | |
bbe17ca1 | 1009 | |
d5e4e60b A |
1010 | SD_BUS_METHOD_WITH_ARGS("GetMachine", |
1011 | SD_BUS_ARGS("s", name), | |
1012 | SD_BUS_RESULT("o", machine), | |
1013 | method_get_machine, | |
1014 | SD_BUS_VTABLE_UNPRIVILEGED), | |
1015 | SD_BUS_METHOD_WITH_ARGS("GetImage", | |
1016 | SD_BUS_ARGS("s", name), | |
1017 | SD_BUS_RESULT("o", image), | |
1018 | method_get_image, | |
1019 | SD_BUS_VTABLE_UNPRIVILEGED), | |
1020 | SD_BUS_METHOD_WITH_ARGS("GetMachineByPID", | |
1021 | SD_BUS_ARGS("u", pid), | |
1022 | SD_BUS_RESULT("o", machine), | |
1023 | method_get_machine_by_pid, | |
1024 | SD_BUS_VTABLE_UNPRIVILEGED), | |
1025 | SD_BUS_METHOD_WITH_ARGS("ListMachines", | |
1026 | SD_BUS_NO_ARGS, | |
1027 | SD_BUS_RESULT("a(ssso)", machines), | |
1028 | method_list_machines, | |
1029 | SD_BUS_VTABLE_UNPRIVILEGED), | |
1030 | SD_BUS_METHOD_WITH_ARGS("ListImages", | |
1031 | SD_BUS_NO_ARGS, | |
1032 | SD_BUS_RESULT("a(ssbttto)", images), | |
1033 | method_list_images, | |
1034 | SD_BUS_VTABLE_UNPRIVILEGED), | |
1035 | SD_BUS_METHOD_WITH_ARGS("CreateMachine", | |
1036 | SD_BUS_ARGS("s", name, "ay", id, "s", service, "s", class, "u", leader, "s", root_directory, "a(sv)", scope_properties), | |
1037 | SD_BUS_RESULT("o", path), | |
1038 | method_create_machine, 0), | |
1039 | SD_BUS_METHOD_WITH_ARGS("CreateMachineWithNetwork", | |
1040 | SD_BUS_ARGS("s", name, "ay", id, "s", service, "s", class, "u", leader, "s", root_directory, "ai", ifindices, "a(sv)", scope_properties), | |
1041 | SD_BUS_RESULT("o", path), | |
1042 | method_create_machine_with_network, 0), | |
1043 | SD_BUS_METHOD_WITH_ARGS("RegisterMachine", | |
1044 | SD_BUS_ARGS("s", name, "ay", id, "s", service, "s", class, "u", leader, "s", root_directory), | |
1045 | SD_BUS_RESULT("o", path), | |
1046 | method_register_machine, 0), | |
1047 | SD_BUS_METHOD_WITH_ARGS("RegisterMachineWithNetwork", | |
1048 | SD_BUS_ARGS("s", name, "ay", id, "s", service, "s", class, "u", leader, "s", root_directory, "ai", ifindices), | |
1049 | SD_BUS_RESULT("o", path), | |
1050 | method_register_machine_with_network, 0), | |
1051 | SD_BUS_METHOD_WITH_ARGS("UnregisterMachine", | |
1052 | SD_BUS_ARGS("s", name), | |
1053 | SD_BUS_NO_RESULT, | |
1054 | method_unregister_machine, | |
1055 | SD_BUS_VTABLE_UNPRIVILEGED), | |
1056 | SD_BUS_METHOD_WITH_ARGS("TerminateMachine", | |
1057 | SD_BUS_ARGS("s", id), | |
1058 | SD_BUS_NO_RESULT, | |
1059 | method_terminate_machine, | |
1060 | SD_BUS_VTABLE_UNPRIVILEGED), | |
1061 | SD_BUS_METHOD_WITH_ARGS("KillMachine", | |
1062 | SD_BUS_ARGS("s", name, "s", who, "i", signal), | |
1063 | SD_BUS_NO_RESULT, | |
1064 | method_kill_machine, | |
1065 | SD_BUS_VTABLE_UNPRIVILEGED), | |
1066 | SD_BUS_METHOD_WITH_ARGS("GetMachineAddresses", | |
1067 | SD_BUS_ARGS("s", name), | |
1068 | SD_BUS_RESULT("a(iay)", addresses), | |
1069 | method_get_machine_addresses, | |
1070 | SD_BUS_VTABLE_UNPRIVILEGED), | |
1071 | SD_BUS_METHOD_WITH_ARGS("GetMachineOSRelease", | |
1072 | SD_BUS_ARGS("s", name), | |
1073 | SD_BUS_RESULT("a{ss}", fields), | |
1074 | method_get_machine_os_release, | |
1075 | SD_BUS_VTABLE_UNPRIVILEGED), | |
1076 | SD_BUS_METHOD_WITH_ARGS("OpenMachinePTY", | |
1077 | SD_BUS_ARGS("s", name), | |
1078 | SD_BUS_RESULT("h", pty, "s", pty_path), | |
1079 | method_open_machine_pty, | |
1080 | 0), | |
1081 | SD_BUS_METHOD_WITH_ARGS("OpenMachineLogin", | |
1082 | SD_BUS_ARGS("s", name), | |
1083 | SD_BUS_RESULT("h", pty, "s", pty_path), | |
1084 | method_open_machine_login, | |
1085 | SD_BUS_VTABLE_UNPRIVILEGED), | |
1086 | SD_BUS_METHOD_WITH_ARGS("OpenMachineShell", | |
1087 | SD_BUS_ARGS("s", name, "s", user, "s", path, "as", args, "as", environment), | |
1088 | SD_BUS_RESULT("h", pty, "s", pty_path), | |
1089 | method_open_machine_shell, | |
1090 | SD_BUS_VTABLE_UNPRIVILEGED), | |
1091 | SD_BUS_METHOD_WITH_ARGS("BindMountMachine", | |
1092 | SD_BUS_ARGS("s", name, "s", source, "s", destination, "b", read_only, "b", mkdir), | |
1093 | SD_BUS_NO_RESULT, | |
1094 | method_bind_mount_machine, | |
1095 | SD_BUS_VTABLE_UNPRIVILEGED), | |
1096 | SD_BUS_METHOD_WITH_ARGS("CopyFromMachine", | |
1097 | SD_BUS_ARGS("s", name, "s", source, "s", destination), | |
1098 | SD_BUS_NO_RESULT, | |
1099 | method_copy_machine, | |
1100 | SD_BUS_VTABLE_UNPRIVILEGED), | |
1101 | SD_BUS_METHOD_WITH_ARGS("CopyToMachine", | |
1102 | SD_BUS_ARGS("s", name, "s", source, "s", destination), | |
1103 | SD_BUS_NO_RESULT, | |
1104 | method_copy_machine, | |
1105 | SD_BUS_VTABLE_UNPRIVILEGED), | |
ae03e1a9 AW |
1106 | SD_BUS_METHOD_WITH_ARGS("CopyFromMachineWithFlags", |
1107 | SD_BUS_ARGS("s", name, "s", source, "s", destination, "t", flags), | |
1108 | SD_BUS_NO_RESULT, | |
1109 | method_copy_machine, | |
1110 | SD_BUS_VTABLE_UNPRIVILEGED), | |
1111 | SD_BUS_METHOD_WITH_ARGS("CopyToMachineWithFlags", | |
1112 | SD_BUS_ARGS("s", name, "s", source, "s", destination, "t", flags), | |
1113 | SD_BUS_NO_RESULT, | |
1114 | method_copy_machine, | |
1115 | SD_BUS_VTABLE_UNPRIVILEGED), | |
d5e4e60b A |
1116 | SD_BUS_METHOD_WITH_ARGS("OpenMachineRootDirectory", |
1117 | SD_BUS_ARGS("s", name), | |
1118 | SD_BUS_RESULT("h", fd), | |
1119 | method_open_machine_root_directory, | |
1120 | SD_BUS_VTABLE_UNPRIVILEGED), | |
1121 | SD_BUS_METHOD_WITH_ARGS("GetMachineUIDShift", | |
1122 | SD_BUS_ARGS("s", name), | |
1123 | SD_BUS_RESULT("u", shift), | |
1124 | method_get_machine_uid_shift, | |
1125 | SD_BUS_VTABLE_UNPRIVILEGED), | |
1126 | SD_BUS_METHOD_WITH_ARGS("RemoveImage", | |
1127 | SD_BUS_ARGS("s", name), | |
1128 | SD_BUS_NO_RESULT, | |
1129 | method_remove_image, | |
1130 | SD_BUS_VTABLE_UNPRIVILEGED), | |
1131 | SD_BUS_METHOD_WITH_ARGS("RenameImage", | |
1132 | SD_BUS_ARGS("s", name, "s", new_name), | |
1133 | SD_BUS_NO_RESULT, | |
1134 | method_rename_image, | |
1135 | SD_BUS_VTABLE_UNPRIVILEGED), | |
1136 | SD_BUS_METHOD_WITH_ARGS("CloneImage", | |
1137 | SD_BUS_ARGS("s", name, "s", new_name, "b", read_only), | |
1138 | SD_BUS_NO_RESULT, | |
1139 | method_clone_image, | |
1140 | SD_BUS_VTABLE_UNPRIVILEGED), | |
1141 | SD_BUS_METHOD_WITH_ARGS("MarkImageReadOnly", | |
1142 | SD_BUS_ARGS("s", name, "b", read_only), | |
1143 | SD_BUS_NO_RESULT, | |
1144 | method_mark_image_read_only, | |
1145 | SD_BUS_VTABLE_UNPRIVILEGED), | |
1146 | SD_BUS_METHOD_WITH_ARGS("GetImageHostname", | |
1147 | SD_BUS_ARGS("s", name), | |
1148 | SD_BUS_RESULT("s", hostname), | |
1149 | method_get_image_hostname, | |
1150 | SD_BUS_VTABLE_UNPRIVILEGED), | |
1151 | SD_BUS_METHOD_WITH_ARGS("GetImageMachineID", | |
1152 | SD_BUS_ARGS("s", name), | |
1153 | SD_BUS_RESULT("ay", id), | |
1154 | method_get_image_machine_id, | |
1155 | SD_BUS_VTABLE_UNPRIVILEGED), | |
1156 | SD_BUS_METHOD_WITH_ARGS("GetImageMachineInfo", | |
1157 | SD_BUS_ARGS("s", name), | |
1158 | SD_BUS_RESULT("a{ss}", machine_info), | |
1159 | method_get_image_machine_info, | |
1160 | SD_BUS_VTABLE_UNPRIVILEGED), | |
1161 | SD_BUS_METHOD_WITH_ARGS("GetImageOSRelease", | |
1162 | SD_BUS_ARGS("s", name), | |
1163 | SD_BUS_RESULT("a{ss}", os_release), | |
1164 | method_get_image_os_release, | |
1165 | SD_BUS_VTABLE_UNPRIVILEGED), | |
1166 | SD_BUS_METHOD_WITH_ARGS("SetPoolLimit", | |
1167 | SD_BUS_ARGS("t", size), | |
1168 | SD_BUS_NO_RESULT, | |
1169 | method_set_pool_limit, | |
1170 | SD_BUS_VTABLE_UNPRIVILEGED), | |
1171 | SD_BUS_METHOD_WITH_ARGS("SetImageLimit", | |
1172 | SD_BUS_ARGS("s", name, "t", size), | |
1173 | SD_BUS_NO_RESULT, | |
1174 | method_set_image_limit, | |
1175 | SD_BUS_VTABLE_UNPRIVILEGED), | |
1176 | SD_BUS_METHOD_WITH_ARGS("CleanPool", | |
1177 | SD_BUS_ARGS("s", mode), | |
1178 | SD_BUS_RESULT("a(st)",images), | |
1179 | method_clean_pool, | |
1180 | SD_BUS_VTABLE_UNPRIVILEGED), | |
1181 | SD_BUS_METHOD_WITH_ARGS("MapFromMachineUser", | |
1182 | SD_BUS_ARGS("s", name, "u", uid_inner), | |
1183 | SD_BUS_RESULT("u", uid_outer), | |
1184 | method_map_from_machine_user, | |
1185 | SD_BUS_VTABLE_UNPRIVILEGED), | |
1186 | SD_BUS_METHOD_WITH_ARGS("MapToMachineUser", | |
1187 | SD_BUS_ARGS("u", uid_outer), | |
1188 | SD_BUS_RESULT("s", machine_name, "o", machine_path, "u", uid_inner), | |
1189 | method_map_to_machine_user, | |
1190 | SD_BUS_VTABLE_UNPRIVILEGED), | |
1191 | SD_BUS_METHOD_WITH_ARGS("MapFromMachineGroup", | |
1192 | SD_BUS_ARGS("s", name, "u", gid_inner), | |
1193 | SD_BUS_RESULT("u", gid_outer), | |
1194 | method_map_from_machine_group, | |
1195 | SD_BUS_VTABLE_UNPRIVILEGED), | |
1196 | SD_BUS_METHOD_WITH_ARGS("MapToMachineGroup", | |
1197 | SD_BUS_ARGS("u", gid_outer), | |
1198 | SD_BUS_RESULT("s", machine_name, "o", machine_path, "u", gid_inner), | |
1199 | method_map_to_machine_group, | |
1200 | SD_BUS_VTABLE_UNPRIVILEGED), | |
1201 | ||
1202 | SD_BUS_SIGNAL_WITH_ARGS("MachineNew", | |
1203 | SD_BUS_ARGS("s", machine, "o", path), | |
1204 | 0), | |
1205 | SD_BUS_SIGNAL_WITH_ARGS("MachineRemoved", | |
1206 | SD_BUS_ARGS("s", machine, "o", path), | |
1207 | 0), | |
bbe17ca1 | 1208 | |
c3350683 LP |
1209 | SD_BUS_VTABLE_END |
1210 | }; | |
1ee306e1 | 1211 | |
4faa530c ZJS |
1212 | const BusObjectImplementation manager_object = { |
1213 | "/org/freedesktop/machine1", | |
1214 | "org.freedesktop.machine1.Manager", | |
1215 | .vtables = BUS_VTABLES(manager_vtable), | |
1216 | .children = BUS_IMPLEMENTATIONS( &machine_object, | |
1217 | &image_object ), | |
1218 | }; | |
1219 | ||
19070062 | 1220 | int match_job_removed(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
c3350683 | 1221 | const char *path, *result, *unit; |
99534007 | 1222 | Manager *m = ASSERT_PTR(userdata); |
c3350683 LP |
1223 | Machine *machine; |
1224 | uint32_t id; | |
1225 | int r; | |
1ee306e1 | 1226 | |
1ee306e1 LP |
1227 | assert(message); |
1228 | ||
c3350683 LP |
1229 | r = sd_bus_message_read(message, "uoss", &id, &path, &unit, &result); |
1230 | if (r < 0) { | |
ebcf1f97 | 1231 | bus_log_parse_error(r); |
65d73cf0 | 1232 | return 0; |
c3350683 | 1233 | } |
6797c324 | 1234 | |
c3350683 LP |
1235 | machine = hashmap_get(m->machine_units, unit); |
1236 | if (!machine) | |
1237 | return 0; | |
6797c324 | 1238 | |
c3350683 | 1239 | if (streq_ptr(path, machine->scope_job)) { |
491ac9f2 | 1240 | machine->scope_job = mfree(machine->scope_job); |
6797c324 | 1241 | |
c3350683 LP |
1242 | if (machine->started) { |
1243 | if (streq(result, "done")) | |
1244 | machine_send_create_reply(machine, NULL); | |
1245 | else { | |
4afd3348 | 1246 | _cleanup_(sd_bus_error_free) sd_bus_error e = SD_BUS_ERROR_NULL; |
6797c324 | 1247 | |
ebcf1f97 | 1248 | sd_bus_error_setf(&e, BUS_ERROR_JOB_FAILED, "Start job for unit %s failed with '%s'", unit, result); |
6797c324 | 1249 | |
ebcf1f97 | 1250 | machine_send_create_reply(machine, &e); |
c3350683 | 1251 | } |
49f3fffd LP |
1252 | } |
1253 | ||
1254 | machine_save(machine); | |
1ee306e1 LP |
1255 | } |
1256 | ||
c3350683 LP |
1257 | machine_add_to_gc_queue(machine); |
1258 | return 0; | |
1ee306e1 LP |
1259 | } |
1260 | ||
19070062 | 1261 | int match_properties_changed(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
c3350683 | 1262 | _cleanup_free_ char *unit = NULL; |
49f3fffd | 1263 | const char *path; |
99534007 | 1264 | Manager *m = ASSERT_PTR(userdata); |
c3350683 | 1265 | Machine *machine; |
ebcf1f97 | 1266 | int r; |
554604b3 | 1267 | |
c3350683 | 1268 | assert(message); |
554604b3 | 1269 | |
c3350683 LP |
1270 | path = sd_bus_message_get_path(message); |
1271 | if (!path) | |
554604b3 | 1272 | return 0; |
554604b3 | 1273 | |
ebcf1f97 | 1274 | r = unit_name_from_dbus_path(path, &unit); |
e5f5b5b9 LP |
1275 | if (r == -EINVAL) /* not for a unit */ |
1276 | return 0; | |
9ed794a3 | 1277 | if (r < 0) { |
9b420b3c LP |
1278 | log_oom(); |
1279 | return 0; | |
1280 | } | |
554604b3 | 1281 | |
c3350683 | 1282 | machine = hashmap_get(m->machine_units, unit); |
9b420b3c LP |
1283 | if (!machine) |
1284 | return 0; | |
1285 | ||
9b420b3c | 1286 | machine_add_to_gc_queue(machine); |
c3350683 LP |
1287 | return 0; |
1288 | } | |
554604b3 | 1289 | |
19070062 | 1290 | int match_unit_removed(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
c3350683 | 1291 | const char *path, *unit; |
99534007 | 1292 | Manager *m = ASSERT_PTR(userdata); |
c3350683 LP |
1293 | Machine *machine; |
1294 | int r; | |
554604b3 | 1295 | |
c3350683 | 1296 | assert(message); |
554604b3 | 1297 | |
c3350683 LP |
1298 | r = sd_bus_message_read(message, "so", &unit, &path); |
1299 | if (r < 0) { | |
ebcf1f97 | 1300 | bus_log_parse_error(r); |
9b420b3c | 1301 | return 0; |
554604b3 LP |
1302 | } |
1303 | ||
c3350683 | 1304 | machine = hashmap_get(m->machine_units, unit); |
9b420b3c LP |
1305 | if (!machine) |
1306 | return 0; | |
1307 | ||
9b420b3c | 1308 | machine_add_to_gc_queue(machine); |
c3350683 LP |
1309 | return 0; |
1310 | } | |
554604b3 | 1311 | |
19070062 | 1312 | int match_reloading(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
99534007 | 1313 | Manager *m = ASSERT_PTR(userdata); |
a658cafa | 1314 | Machine *machine; |
c3350683 | 1315 | int b, r; |
554604b3 | 1316 | |
19070062 | 1317 | assert(message); |
554604b3 | 1318 | |
c3350683 LP |
1319 | r = sd_bus_message_read(message, "b", &b); |
1320 | if (r < 0) { | |
ebcf1f97 | 1321 | bus_log_parse_error(r); |
65d73cf0 | 1322 | return 0; |
554604b3 | 1323 | } |
a658cafa LP |
1324 | if (b) |
1325 | return 0; | |
554604b3 | 1326 | |
a658cafa LP |
1327 | /* systemd finished reloading, let's recheck all our machines */ |
1328 | log_debug("System manager has been reloaded, rechecking machines..."); | |
554604b3 | 1329 | |
90e74a66 | 1330 | HASHMAP_FOREACH(machine, m->machines) |
a658cafa | 1331 | machine_add_to_gc_queue(machine); |
554604b3 LP |
1332 | |
1333 | return 0; | |
1334 | } | |
1335 | ||
b92d0b4c LP |
1336 | int manager_unref_unit( |
1337 | Manager *m, | |
1338 | const char *unit, | |
1339 | sd_bus_error *error) { | |
1340 | ||
1341 | assert(m); | |
1342 | assert(unit); | |
1343 | ||
14456f76 | 1344 | return bus_call_method(m->bus, bus_systemd_mgr, "UnrefUnit", error, NULL, "s", unit); |
b92d0b4c LP |
1345 | } |
1346 | ||
c3350683 | 1347 | int manager_stop_unit(Manager *manager, const char *unit, sd_bus_error *error, char **job) { |
4afd3348 | 1348 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; |
1ee306e1 LP |
1349 | int r; |
1350 | ||
1351 | assert(manager); | |
1352 | assert(unit); | |
1353 | ||
14456f76 | 1354 | r = bus_call_method(manager->bus, bus_systemd_mgr, "StopUnit", error, &reply, "ss", unit, "fail"); |
1ee306e1 | 1355 | if (r < 0) { |
955a6329 ZJS |
1356 | if (sd_bus_error_has_names(error, BUS_ERROR_NO_SUCH_UNIT, |
1357 | BUS_ERROR_LOAD_FAILED)) { | |
6797c324 LP |
1358 | |
1359 | if (job) | |
1360 | *job = NULL; | |
1361 | ||
c3350683 | 1362 | sd_bus_error_free(error); |
6797c324 LP |
1363 | return 0; |
1364 | } | |
1365 | ||
1ee306e1 LP |
1366 | return r; |
1367 | } | |
1368 | ||
1369 | if (job) { | |
1370 | const char *j; | |
1371 | char *copy; | |
1372 | ||
c3350683 LP |
1373 | r = sd_bus_message_read(reply, "o", &j); |
1374 | if (r < 0) | |
1375 | return r; | |
1ee306e1 LP |
1376 | |
1377 | copy = strdup(j); | |
1378 | if (!copy) | |
1379 | return -ENOMEM; | |
1380 | ||
1381 | *job = copy; | |
1382 | } | |
1383 | ||
6797c324 | 1384 | return 1; |
1ee306e1 LP |
1385 | } |
1386 | ||
de58a50e | 1387 | int manager_kill_unit(Manager *manager, const char *unit, int signo, sd_bus_error *error) { |
1ee306e1 LP |
1388 | assert(manager); |
1389 | assert(unit); | |
1390 | ||
14456f76 | 1391 | return bus_call_method(manager->bus, bus_systemd_mgr, "KillUnit", error, NULL, "ssi", unit, "all", signo); |
1ee306e1 LP |
1392 | } |
1393 | ||
1394 | int manager_unit_is_active(Manager *manager, const char *unit) { | |
4afd3348 LP |
1395 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
1396 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; | |
1ee306e1 | 1397 | _cleanup_free_ char *path = NULL; |
1ee306e1 | 1398 | const char *state; |
1ee306e1 LP |
1399 | int r; |
1400 | ||
1401 | assert(manager); | |
1402 | assert(unit); | |
1403 | ||
1ee306e1 LP |
1404 | path = unit_dbus_path_from_name(unit); |
1405 | if (!path) | |
1406 | return -ENOMEM; | |
1407 | ||
c3350683 | 1408 | r = sd_bus_get_property( |
1ee306e1 LP |
1409 | manager->bus, |
1410 | "org.freedesktop.systemd1", | |
1411 | path, | |
c3350683 LP |
1412 | "org.freedesktop.systemd1.Unit", |
1413 | "ActiveState", | |
1ee306e1 | 1414 | &error, |
c3350683 LP |
1415 | &reply, |
1416 | "s"); | |
1ee306e1 | 1417 | if (r < 0) { |
955a6329 ZJS |
1418 | if (sd_bus_error_has_names(&error, SD_BUS_ERROR_NO_REPLY, |
1419 | SD_BUS_ERROR_DISCONNECTED)) | |
6797c324 | 1420 | return true; |
6797c324 | 1421 | |
955a6329 ZJS |
1422 | if (sd_bus_error_has_names(&error, BUS_ERROR_NO_SUCH_UNIT, |
1423 | BUS_ERROR_LOAD_FAILED)) | |
6797c324 | 1424 | return false; |
6797c324 | 1425 | |
1ee306e1 LP |
1426 | return r; |
1427 | } | |
1428 | ||
c3350683 LP |
1429 | r = sd_bus_message_read(reply, "s", &state); |
1430 | if (r < 0) | |
1ee306e1 | 1431 | return -EINVAL; |
1ee306e1 | 1432 | |
9b420b3c | 1433 | return !STR_IN_SET(state, "inactive", "failed"); |
1ee306e1 | 1434 | } |
bd16acf3 | 1435 | |
c3350683 | 1436 | int manager_job_is_active(Manager *manager, const char *path) { |
4afd3348 LP |
1437 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
1438 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; | |
c3350683 | 1439 | int r; |
bd16acf3 | 1440 | |
c3350683 LP |
1441 | assert(manager); |
1442 | assert(path); | |
bd16acf3 | 1443 | |
c3350683 LP |
1444 | r = sd_bus_get_property( |
1445 | manager->bus, | |
1446 | "org.freedesktop.systemd1", | |
1447 | path, | |
1448 | "org.freedesktop.systemd1.Job", | |
1449 | "State", | |
1450 | &error, | |
1451 | &reply, | |
1452 | "s"); | |
1453 | if (r < 0) { | |
955a6329 ZJS |
1454 | if (sd_bus_error_has_names(&error, SD_BUS_ERROR_NO_REPLY, |
1455 | SD_BUS_ERROR_DISCONNECTED)) | |
c3350683 | 1456 | return true; |
bd16acf3 | 1457 | |
c3350683 LP |
1458 | if (sd_bus_error_has_name(&error, SD_BUS_ERROR_UNKNOWN_OBJECT)) |
1459 | return false; | |
bd16acf3 | 1460 | |
bd16acf3 | 1461 | return r; |
c3350683 | 1462 | } |
bd16acf3 | 1463 | |
c3350683 LP |
1464 | /* We don't actually care about the state really. The fact |
1465 | * that we could read the job state is enough for us */ | |
bd16acf3 | 1466 | |
c3350683 | 1467 | return true; |
bd16acf3 | 1468 | } |
ab49725f KS |
1469 | |
1470 | int manager_get_machine_by_pid(Manager *m, pid_t pid, Machine **machine) { | |
ab49725f KS |
1471 | Machine *mm; |
1472 | int r; | |
1473 | ||
1474 | assert(m); | |
1475 | assert(pid >= 1); | |
1476 | assert(machine); | |
1477 | ||
4a0b58c4 | 1478 | mm = hashmap_get(m->machine_leaders, PID_TO_PTR(pid)); |
077c8c36 LP |
1479 | if (!mm) { |
1480 | _cleanup_free_ char *unit = NULL; | |
ab49725f | 1481 | |
077c8c36 LP |
1482 | r = cg_pid_get_unit(pid, &unit); |
1483 | if (r >= 0) | |
1484 | mm = hashmap_get(m->machine_units, unit); | |
1485 | } | |
ab49725f KS |
1486 | if (!mm) |
1487 | return 0; | |
1488 | ||
1489 | *machine = mm; | |
1490 | return 1; | |
1491 | } | |
1492 | ||
1493 | int manager_add_machine(Manager *m, const char *name, Machine **_machine) { | |
1494 | Machine *machine; | |
359e8d76 | 1495 | int r; |
ab49725f KS |
1496 | |
1497 | assert(m); | |
1498 | assert(name); | |
1499 | ||
1500 | machine = hashmap_get(m->machines, name); | |
1501 | if (!machine) { | |
359e8d76 DT |
1502 | r = machine_new(m, _MACHINE_CLASS_INVALID, name, &machine); |
1503 | if (r < 0) | |
1504 | return r; | |
ab49725f KS |
1505 | } |
1506 | ||
1507 | if (_machine) | |
1508 | *_machine = machine; | |
1509 | ||
1510 | return 0; | |
1511 | } |