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