]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/machine/machined-dbus.c
mkosi: Fix particle profile
[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
86cbbc6d 616 if (lseek(operation->extra_fd, 0, SEEK_SET) < 0)
03c2b288
LP
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
71136404 691 _cleanup_close_pair_ int errno_pipe_fd[2] = EBADF_PAIR;
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,
d94c2b06 723 "org.freedesktop.machine1.manage-machines",
8dd3f6a3 724 details,
d94c2b06
LP
725 &m->polkit_registry,
726 error);
727 if (r < 0)
728 return r;
729 if (r == 0)
730 return 1; /* Will call us back */
731
03c2b288
LP
732 if (pipe2(errno_pipe_fd, O_CLOEXEC|O_NONBLOCK) < 0)
733 return sd_bus_error_set_errnof(error, errno, "Failed to create pipe: %m");
d94c2b06 734
03c2b288
LP
735 /* Create a temporary file we can dump information about deleted images into. We use a temporary file for this
736 * instead of a pipe or so, since this might grow quit large in theory and we don't want to process this
595bfe7d 737 * continuously */
992e8f22 738 result_fd = open_tmpfile_unlinkable(NULL, O_RDWR|O_CLOEXEC);
03c2b288
LP
739 if (result_fd < 0)
740 return -errno;
d94c2b06 741
03c2b288 742 /* This might be a slow operation, run it asynchronously in a background process */
4c253ed1
LP
743 r = safe_fork("(sd-clean)", FORK_RESET_SIGNALS, &child);
744 if (r < 0)
745 return sd_bus_error_set_errnof(error, r, "Failed to fork(): %m");
746 if (r == 0) {
b07ec5a1 747 _cleanup_hashmap_free_ Hashmap *images = NULL;
03c2b288
LP
748 bool success = true;
749 Image *image;
03c2b288 750 ssize_t l;
d94c2b06 751
03c2b288 752 errno_pipe_fd[0] = safe_close(errno_pipe_fd[0]);
d94c2b06 753
b07ec5a1 754 images = hashmap_new(&image_hash_ops);
03c2b288
LP
755 if (!images) {
756 r = -ENOMEM;
757 goto child_fail;
758 }
d94c2b06 759
d577d4a4 760 r = image_discover(IMAGE_MACHINE, NULL, images);
03c2b288
LP
761 if (r < 0)
762 goto child_fail;
d94c2b06 763
03c2b288
LP
764 l = write(result_fd, &success, sizeof(success));
765 if (l < 0) {
766 r = -errno;
767 goto child_fail;
768 }
d94c2b06 769
90e74a66 770 HASHMAP_FOREACH(image, images) {
03c2b288
LP
771
772 /* We can't remove vendor images (i.e. those in /usr) */
773 if (IMAGE_IS_VENDOR(image))
774 continue;
775
776 if (IMAGE_IS_HOST(image))
777 continue;
778
779 if (mode == REMOVE_HIDDEN && !IMAGE_IS_HIDDEN(image))
780 continue;
781
782 r = image_remove(image);
783 if (r == -EBUSY) /* keep images that are currently being used. */
784 continue;
785 if (r < 0) {
786 /* If the operation failed, let's override everything we wrote, and instead write there at which image we failed. */
787 success = false;
788 (void) ftruncate(result_fd, 0);
789 (void) lseek(result_fd, 0, SEEK_SET);
790 (void) write(result_fd, &success, sizeof(success));
791 (void) write(result_fd, image->name, strlen(image->name)+1);
792 goto child_fail;
793 }
794
795 l = write(result_fd, image->name, strlen(image->name)+1);
796 if (l < 0) {
797 r = -errno;
798 goto child_fail;
799 }
800
801 l = write(result_fd, &image->usage_exclusive, sizeof(image->usage_exclusive));
802 if (l < 0) {
803 r = -errno;
804 goto child_fail;
805 }
806 }
d94c2b06 807
03c2b288
LP
808 result_fd = safe_close(result_fd);
809 _exit(EXIT_SUCCESS);
810
811 child_fail:
812 (void) write(errno_pipe_fd[1], &r, sizeof(r));
813 _exit(EXIT_FAILURE);
d94c2b06
LP
814 }
815
03c2b288
LP
816 errno_pipe_fd[1] = safe_close(errno_pipe_fd[1]);
817
818 /* The clean-up might take a while, hence install a watch on the child and return */
819
820 r = operation_new(m, NULL, child, message, errno_pipe_fd[0], &operation);
821 if (r < 0) {
822 (void) sigkill_wait(child);
d94c2b06 823 return r;
03c2b288 824 }
d94c2b06 825
03c2b288
LP
826 operation->extra_fd = result_fd;
827 operation->done = clean_pool_done;
828
254d1313
ZJS
829 result_fd = -EBADF;
830 errno_pipe_fd[0] = -EBADF;
03c2b288
LP
831
832 return 1;
d94c2b06
LP
833}
834
19070062 835static int method_set_pool_limit(sd_bus_message *message, void *userdata, sd_bus_error *error) {
d6ce17c7
LP
836 Manager *m = userdata;
837 uint64_t limit;
838 int r;
839
19070062
LP
840 assert(message);
841
d6ce17c7
LP
842 r = sd_bus_message_read(message, "t", &limit);
843 if (r < 0)
844 return r;
a90fb858 845 if (!FILE_SIZE_VALID_OR_INFINITY(limit))
1b09b81c 846 return sd_bus_error_set(error, SD_BUS_ERROR_INVALID_ARGS, "New limit out of range");
d6ce17c7 847
8dd3f6a3
LN
848 const char *details[] = {
849 "verb", "set_pool_limit",
850 NULL
851 };
852
d6ce17c7
LP
853 r = bus_verify_polkit_async(
854 message,
d6ce17c7 855 "org.freedesktop.machine1.manage-machines",
8dd3f6a3 856 details,
d6ce17c7
LP
857 &m->polkit_registry,
858 error);
859 if (r < 0)
860 return r;
861 if (r == 0)
862 return 1; /* Will call us back */
863
4cee5eed 864 /* Set up the machine directory if necessary */
c7779a61 865 r = setup_machine_directory(error, /* use_btrfs_subvol= */ true, /* use_btrfs_quota= */ true);
4cee5eed
LP
866 if (r < 0)
867 return r;
868
5bcd08db
LP
869 (void) btrfs_qgroup_set_limit("/var/lib/machines", 0, limit);
870
871 r = btrfs_subvol_set_subtree_quota_limit("/var/lib/machines", 0, limit);
d6ce17c7 872 if (r == -ENOTTY)
1b09b81c 873 return sd_bus_error_set(error, SD_BUS_ERROR_NOT_SUPPORTED, "Quota is only supported on btrfs.");
26166c88 874 if (r < 0)
d6ce17c7
LP
875 return sd_bus_error_set_errnof(error, r, "Failed to adjust quota limit: %m");
876
877 return sd_bus_reply_method_return(message, NULL);
878}
879
19070062 880static int method_set_image_limit(sd_bus_message *message, void *userdata, sd_bus_error *error) {
9b06c1e1 881 return redirect_method_to_image(message, userdata, error, bus_image_method_set_limit);
d6ce17c7
LP
882}
883
c01ff965 884static int method_map_from_machine_user(sd_bus_message *message, void *userdata, sd_bus_error *error) {
c01ff965 885 Manager *m = userdata;
74d1b7d2 886 const char *name;
c01ff965
LP
887 Machine *machine;
888 uint32_t uid;
74d1b7d2 889 uid_t converted;
c01ff965
LP
890 int r;
891
892 r = sd_bus_message_read(message, "su", &name, &uid);
893 if (r < 0)
894 return r;
895
c077529b 896 if (!uid_is_valid(uid))
c01ff965
LP
897 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid user ID " UID_FMT, uid);
898
899 machine = hashmap_get(m->machines, name);
900 if (!machine)
901 return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_MACHINE, "No machine '%s' known", name);
902
a79366e2 903 if (machine->class != MACHINE_CONTAINER)
1b09b81c 904 return sd_bus_error_set(error, SD_BUS_ERROR_INVALID_ARGS, "Not supported for non-container machines.");
a79366e2 905
74d1b7d2
LP
906 r = machine_translate_uid(machine, uid, &converted);
907 if (r == -ESRCH)
908 return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_USER_MAPPING, "Machine '%s' has no matching user mappings.", name);
909 if (r < 0)
910 return r;
c01ff965 911
74d1b7d2 912 return sd_bus_reply_method_return(message, "u", (uint32_t) converted);
c01ff965
LP
913}
914
915static int method_map_to_machine_user(sd_bus_message *message, void *userdata, sd_bus_error *error) {
74d1b7d2 916 _cleanup_free_ char *o = NULL;
c01ff965
LP
917 Manager *m = userdata;
918 Machine *machine;
74d1b7d2 919 uid_t uid, converted;
c01ff965
LP
920 int r;
921
922 r = sd_bus_message_read(message, "u", &uid);
923 if (r < 0)
924 return r;
c077529b 925 if (!uid_is_valid(uid))
c01ff965
LP
926 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid user ID " UID_FMT, uid);
927 if (uid < 0x10000)
928 return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_USER_MAPPING, "User " UID_FMT " belongs to host UID range", uid);
929
74d1b7d2
LP
930 r = manager_find_machine_for_uid(m, uid, &machine, &converted);
931 if (r < 0)
932 return r;
933 if (!r)
934 return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_USER_MAPPING, "No matching user mapping for " UID_FMT ".", uid);
c01ff965 935
74d1b7d2
LP
936 o = machine_bus_path(machine);
937 if (!o)
938 return -ENOMEM;
c01ff965 939
74d1b7d2 940 return sd_bus_reply_method_return(message, "sou", machine->name, o, (uint32_t) converted);
c01ff965
LP
941}
942
74d1b7d2
LP
943static int method_map_from_machine_group(sd_bus_message *message, void *userdata, sd_bus_error *error) {
944 Manager *m = userdata;
945 const char *name;
c01ff965 946 Machine *machine;
74d1b7d2 947 gid_t converted;
c01ff965
LP
948 uint32_t gid;
949 int r;
950
951 r = sd_bus_message_read(message, "su", &name, &gid);
952 if (r < 0)
953 return r;
954
c077529b 955 if (!gid_is_valid(gid))
c01ff965
LP
956 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid group ID " GID_FMT, gid);
957
958 machine = hashmap_get(m->machines, name);
959 if (!machine)
960 return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_MACHINE, "No machine '%s' known", name);
961
a79366e2 962 if (machine->class != MACHINE_CONTAINER)
1b09b81c 963 return sd_bus_error_set(error, SD_BUS_ERROR_INVALID_ARGS, "Not supported for non-container machines.");
a79366e2 964
74d1b7d2
LP
965 r = machine_translate_gid(machine, gid, &converted);
966 if (r == -ESRCH)
967 return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_USER_MAPPING, "Machine '%s' has no matching group mappings.", name);
968 if (r < 0)
969 return r;
c01ff965 970
74d1b7d2 971 return sd_bus_reply_method_return(message, "u", (uint32_t) converted);
c01ff965
LP
972}
973
74d1b7d2
LP
974static int method_map_to_machine_group(sd_bus_message *message, void *userdata, sd_bus_error *error) {
975 _cleanup_free_ char *o = NULL;
976 Manager *m = userdata;
c01ff965 977 Machine *machine;
74d1b7d2 978 gid_t gid, converted;
c01ff965
LP
979 int r;
980
981 r = sd_bus_message_read(message, "u", &gid);
982 if (r < 0)
983 return r;
c077529b 984 if (!gid_is_valid(gid))
c01ff965
LP
985 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid group ID " GID_FMT, gid);
986 if (gid < 0x10000)
987 return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_GROUP_MAPPING, "Group " GID_FMT " belongs to host GID range", gid);
988
74d1b7d2
LP
989 r = manager_find_machine_for_gid(m, gid, &machine, &converted);
990 if (r < 0)
991 return r;
992 if (!r)
993 return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_GROUP_MAPPING, "No matching group mapping for " GID_FMT ".", gid);
c01ff965 994
74d1b7d2
LP
995 o = machine_bus_path(machine);
996 if (!o)
997 return -ENOMEM;
c01ff965 998
74d1b7d2 999 return sd_bus_reply_method_return(message, "sou", machine->name, o, (uint32_t) converted);
c01ff965
LP
1000}
1001
c3350683
LP
1002const sd_bus_vtable manager_vtable[] = {
1003 SD_BUS_VTABLE_START(0),
bbe17ca1 1004
160e3793
LP
1005 SD_BUS_PROPERTY("PoolPath", "s", property_get_pool_path, 0, 0),
1006 SD_BUS_PROPERTY("PoolUsage", "t", property_get_pool_usage, 0, 0),
1007 SD_BUS_PROPERTY("PoolLimit", "t", property_get_pool_limit, 0, 0),
bbe17ca1 1008
d5e4e60b
A
1009 SD_BUS_METHOD_WITH_ARGS("GetMachine",
1010 SD_BUS_ARGS("s", name),
1011 SD_BUS_RESULT("o", machine),
1012 method_get_machine,
1013 SD_BUS_VTABLE_UNPRIVILEGED),
1014 SD_BUS_METHOD_WITH_ARGS("GetImage",
1015 SD_BUS_ARGS("s", name),
1016 SD_BUS_RESULT("o", image),
1017 method_get_image,
1018 SD_BUS_VTABLE_UNPRIVILEGED),
1019 SD_BUS_METHOD_WITH_ARGS("GetMachineByPID",
1020 SD_BUS_ARGS("u", pid),
1021 SD_BUS_RESULT("o", machine),
1022 method_get_machine_by_pid,
1023 SD_BUS_VTABLE_UNPRIVILEGED),
1024 SD_BUS_METHOD_WITH_ARGS("ListMachines",
1025 SD_BUS_NO_ARGS,
1026 SD_BUS_RESULT("a(ssso)", machines),
1027 method_list_machines,
1028 SD_BUS_VTABLE_UNPRIVILEGED),
1029 SD_BUS_METHOD_WITH_ARGS("ListImages",
1030 SD_BUS_NO_ARGS,
1031 SD_BUS_RESULT("a(ssbttto)", images),
1032 method_list_images,
1033 SD_BUS_VTABLE_UNPRIVILEGED),
1034 SD_BUS_METHOD_WITH_ARGS("CreateMachine",
1035 SD_BUS_ARGS("s", name, "ay", id, "s", service, "s", class, "u", leader, "s", root_directory, "a(sv)", scope_properties),
1036 SD_BUS_RESULT("o", path),
1037 method_create_machine, 0),
1038 SD_BUS_METHOD_WITH_ARGS("CreateMachineWithNetwork",
1039 SD_BUS_ARGS("s", name, "ay", id, "s", service, "s", class, "u", leader, "s", root_directory, "ai", ifindices, "a(sv)", scope_properties),
1040 SD_BUS_RESULT("o", path),
1041 method_create_machine_with_network, 0),
1042 SD_BUS_METHOD_WITH_ARGS("RegisterMachine",
1043 SD_BUS_ARGS("s", name, "ay", id, "s", service, "s", class, "u", leader, "s", root_directory),
1044 SD_BUS_RESULT("o", path),
1045 method_register_machine, 0),
1046 SD_BUS_METHOD_WITH_ARGS("RegisterMachineWithNetwork",
1047 SD_BUS_ARGS("s", name, "ay", id, "s", service, "s", class, "u", leader, "s", root_directory, "ai", ifindices),
1048 SD_BUS_RESULT("o", path),
1049 method_register_machine_with_network, 0),
1050 SD_BUS_METHOD_WITH_ARGS("UnregisterMachine",
1051 SD_BUS_ARGS("s", name),
1052 SD_BUS_NO_RESULT,
1053 method_unregister_machine,
1054 SD_BUS_VTABLE_UNPRIVILEGED),
1055 SD_BUS_METHOD_WITH_ARGS("TerminateMachine",
1056 SD_BUS_ARGS("s", id),
1057 SD_BUS_NO_RESULT,
1058 method_terminate_machine,
1059 SD_BUS_VTABLE_UNPRIVILEGED),
1060 SD_BUS_METHOD_WITH_ARGS("KillMachine",
1061 SD_BUS_ARGS("s", name, "s", who, "i", signal),
1062 SD_BUS_NO_RESULT,
1063 method_kill_machine,
1064 SD_BUS_VTABLE_UNPRIVILEGED),
1065 SD_BUS_METHOD_WITH_ARGS("GetMachineAddresses",
1066 SD_BUS_ARGS("s", name),
1067 SD_BUS_RESULT("a(iay)", addresses),
1068 method_get_machine_addresses,
1069 SD_BUS_VTABLE_UNPRIVILEGED),
1070 SD_BUS_METHOD_WITH_ARGS("GetMachineOSRelease",
1071 SD_BUS_ARGS("s", name),
1072 SD_BUS_RESULT("a{ss}", fields),
1073 method_get_machine_os_release,
1074 SD_BUS_VTABLE_UNPRIVILEGED),
1075 SD_BUS_METHOD_WITH_ARGS("OpenMachinePTY",
1076 SD_BUS_ARGS("s", name),
1077 SD_BUS_RESULT("h", pty, "s", pty_path),
1078 method_open_machine_pty,
1079 0),
1080 SD_BUS_METHOD_WITH_ARGS("OpenMachineLogin",
1081 SD_BUS_ARGS("s", name),
1082 SD_BUS_RESULT("h", pty, "s", pty_path),
1083 method_open_machine_login,
1084 SD_BUS_VTABLE_UNPRIVILEGED),
1085 SD_BUS_METHOD_WITH_ARGS("OpenMachineShell",
1086 SD_BUS_ARGS("s", name, "s", user, "s", path, "as", args, "as", environment),
1087 SD_BUS_RESULT("h", pty, "s", pty_path),
1088 method_open_machine_shell,
1089 SD_BUS_VTABLE_UNPRIVILEGED),
1090 SD_BUS_METHOD_WITH_ARGS("BindMountMachine",
1091 SD_BUS_ARGS("s", name, "s", source, "s", destination, "b", read_only, "b", mkdir),
1092 SD_BUS_NO_RESULT,
1093 method_bind_mount_machine,
1094 SD_BUS_VTABLE_UNPRIVILEGED),
1095 SD_BUS_METHOD_WITH_ARGS("CopyFromMachine",
1096 SD_BUS_ARGS("s", name, "s", source, "s", destination),
1097 SD_BUS_NO_RESULT,
1098 method_copy_machine,
1099 SD_BUS_VTABLE_UNPRIVILEGED),
1100 SD_BUS_METHOD_WITH_ARGS("CopyToMachine",
1101 SD_BUS_ARGS("s", name, "s", source, "s", destination),
1102 SD_BUS_NO_RESULT,
1103 method_copy_machine,
1104 SD_BUS_VTABLE_UNPRIVILEGED),
ae03e1a9
AW
1105 SD_BUS_METHOD_WITH_ARGS("CopyFromMachineWithFlags",
1106 SD_BUS_ARGS("s", name, "s", source, "s", destination, "t", flags),
1107 SD_BUS_NO_RESULT,
1108 method_copy_machine,
1109 SD_BUS_VTABLE_UNPRIVILEGED),
1110 SD_BUS_METHOD_WITH_ARGS("CopyToMachineWithFlags",
1111 SD_BUS_ARGS("s", name, "s", source, "s", destination, "t", flags),
1112 SD_BUS_NO_RESULT,
1113 method_copy_machine,
1114 SD_BUS_VTABLE_UNPRIVILEGED),
d5e4e60b
A
1115 SD_BUS_METHOD_WITH_ARGS("OpenMachineRootDirectory",
1116 SD_BUS_ARGS("s", name),
1117 SD_BUS_RESULT("h", fd),
1118 method_open_machine_root_directory,
1119 SD_BUS_VTABLE_UNPRIVILEGED),
1120 SD_BUS_METHOD_WITH_ARGS("GetMachineUIDShift",
1121 SD_BUS_ARGS("s", name),
1122 SD_BUS_RESULT("u", shift),
1123 method_get_machine_uid_shift,
1124 SD_BUS_VTABLE_UNPRIVILEGED),
1125 SD_BUS_METHOD_WITH_ARGS("RemoveImage",
1126 SD_BUS_ARGS("s", name),
1127 SD_BUS_NO_RESULT,
1128 method_remove_image,
1129 SD_BUS_VTABLE_UNPRIVILEGED),
1130 SD_BUS_METHOD_WITH_ARGS("RenameImage",
1131 SD_BUS_ARGS("s", name, "s", new_name),
1132 SD_BUS_NO_RESULT,
1133 method_rename_image,
1134 SD_BUS_VTABLE_UNPRIVILEGED),
1135 SD_BUS_METHOD_WITH_ARGS("CloneImage",
1136 SD_BUS_ARGS("s", name, "s", new_name, "b", read_only),
1137 SD_BUS_NO_RESULT,
1138 method_clone_image,
1139 SD_BUS_VTABLE_UNPRIVILEGED),
1140 SD_BUS_METHOD_WITH_ARGS("MarkImageReadOnly",
1141 SD_BUS_ARGS("s", name, "b", read_only),
1142 SD_BUS_NO_RESULT,
1143 method_mark_image_read_only,
1144 SD_BUS_VTABLE_UNPRIVILEGED),
1145 SD_BUS_METHOD_WITH_ARGS("GetImageHostname",
1146 SD_BUS_ARGS("s", name),
1147 SD_BUS_RESULT("s", hostname),
1148 method_get_image_hostname,
1149 SD_BUS_VTABLE_UNPRIVILEGED),
1150 SD_BUS_METHOD_WITH_ARGS("GetImageMachineID",
1151 SD_BUS_ARGS("s", name),
1152 SD_BUS_RESULT("ay", id),
1153 method_get_image_machine_id,
1154 SD_BUS_VTABLE_UNPRIVILEGED),
1155 SD_BUS_METHOD_WITH_ARGS("GetImageMachineInfo",
1156 SD_BUS_ARGS("s", name),
1157 SD_BUS_RESULT("a{ss}", machine_info),
1158 method_get_image_machine_info,
1159 SD_BUS_VTABLE_UNPRIVILEGED),
1160 SD_BUS_METHOD_WITH_ARGS("GetImageOSRelease",
1161 SD_BUS_ARGS("s", name),
1162 SD_BUS_RESULT("a{ss}", os_release),
1163 method_get_image_os_release,
1164 SD_BUS_VTABLE_UNPRIVILEGED),
1165 SD_BUS_METHOD_WITH_ARGS("SetPoolLimit",
1166 SD_BUS_ARGS("t", size),
1167 SD_BUS_NO_RESULT,
1168 method_set_pool_limit,
1169 SD_BUS_VTABLE_UNPRIVILEGED),
1170 SD_BUS_METHOD_WITH_ARGS("SetImageLimit",
1171 SD_BUS_ARGS("s", name, "t", size),
1172 SD_BUS_NO_RESULT,
1173 method_set_image_limit,
1174 SD_BUS_VTABLE_UNPRIVILEGED),
1175 SD_BUS_METHOD_WITH_ARGS("CleanPool",
1176 SD_BUS_ARGS("s", mode),
1177 SD_BUS_RESULT("a(st)",images),
1178 method_clean_pool,
1179 SD_BUS_VTABLE_UNPRIVILEGED),
1180 SD_BUS_METHOD_WITH_ARGS("MapFromMachineUser",
1181 SD_BUS_ARGS("s", name, "u", uid_inner),
1182 SD_BUS_RESULT("u", uid_outer),
1183 method_map_from_machine_user,
1184 SD_BUS_VTABLE_UNPRIVILEGED),
1185 SD_BUS_METHOD_WITH_ARGS("MapToMachineUser",
1186 SD_BUS_ARGS("u", uid_outer),
1187 SD_BUS_RESULT("s", machine_name, "o", machine_path, "u", uid_inner),
1188 method_map_to_machine_user,
1189 SD_BUS_VTABLE_UNPRIVILEGED),
1190 SD_BUS_METHOD_WITH_ARGS("MapFromMachineGroup",
1191 SD_BUS_ARGS("s", name, "u", gid_inner),
1192 SD_BUS_RESULT("u", gid_outer),
1193 method_map_from_machine_group,
1194 SD_BUS_VTABLE_UNPRIVILEGED),
1195 SD_BUS_METHOD_WITH_ARGS("MapToMachineGroup",
1196 SD_BUS_ARGS("u", gid_outer),
1197 SD_BUS_RESULT("s", machine_name, "o", machine_path, "u", gid_inner),
1198 method_map_to_machine_group,
1199 SD_BUS_VTABLE_UNPRIVILEGED),
1200
1201 SD_BUS_SIGNAL_WITH_ARGS("MachineNew",
1202 SD_BUS_ARGS("s", machine, "o", path),
1203 0),
1204 SD_BUS_SIGNAL_WITH_ARGS("MachineRemoved",
1205 SD_BUS_ARGS("s", machine, "o", path),
1206 0),
bbe17ca1 1207
c3350683
LP
1208 SD_BUS_VTABLE_END
1209};
1ee306e1 1210
4faa530c
ZJS
1211const BusObjectImplementation manager_object = {
1212 "/org/freedesktop/machine1",
1213 "org.freedesktop.machine1.Manager",
1214 .vtables = BUS_VTABLES(manager_vtable),
1215 .children = BUS_IMPLEMENTATIONS( &machine_object,
1216 &image_object ),
1217};
1218
19070062 1219int match_job_removed(sd_bus_message *message, void *userdata, sd_bus_error *error) {
c3350683 1220 const char *path, *result, *unit;
99534007 1221 Manager *m = ASSERT_PTR(userdata);
c3350683
LP
1222 Machine *machine;
1223 uint32_t id;
1224 int r;
1ee306e1 1225
1ee306e1
LP
1226 assert(message);
1227
c3350683
LP
1228 r = sd_bus_message_read(message, "uoss", &id, &path, &unit, &result);
1229 if (r < 0) {
ebcf1f97 1230 bus_log_parse_error(r);
65d73cf0 1231 return 0;
c3350683 1232 }
6797c324 1233
c3350683
LP
1234 machine = hashmap_get(m->machine_units, unit);
1235 if (!machine)
1236 return 0;
6797c324 1237
c3350683 1238 if (streq_ptr(path, machine->scope_job)) {
491ac9f2 1239 machine->scope_job = mfree(machine->scope_job);
6797c324 1240
c3350683
LP
1241 if (machine->started) {
1242 if (streq(result, "done"))
1243 machine_send_create_reply(machine, NULL);
1244 else {
4afd3348 1245 _cleanup_(sd_bus_error_free) sd_bus_error e = SD_BUS_ERROR_NULL;
6797c324 1246
ebcf1f97 1247 sd_bus_error_setf(&e, BUS_ERROR_JOB_FAILED, "Start job for unit %s failed with '%s'", unit, result);
6797c324 1248
ebcf1f97 1249 machine_send_create_reply(machine, &e);
c3350683 1250 }
49f3fffd
LP
1251 }
1252
1253 machine_save(machine);
1ee306e1
LP
1254 }
1255
c3350683
LP
1256 machine_add_to_gc_queue(machine);
1257 return 0;
1ee306e1
LP
1258}
1259
19070062 1260int match_properties_changed(sd_bus_message *message, void *userdata, sd_bus_error *error) {
c3350683 1261 _cleanup_free_ char *unit = NULL;
49f3fffd 1262 const char *path;
99534007 1263 Manager *m = ASSERT_PTR(userdata);
c3350683 1264 Machine *machine;
ebcf1f97 1265 int r;
554604b3 1266
c3350683 1267 assert(message);
554604b3 1268
c3350683
LP
1269 path = sd_bus_message_get_path(message);
1270 if (!path)
554604b3 1271 return 0;
554604b3 1272
ebcf1f97 1273 r = unit_name_from_dbus_path(path, &unit);
e5f5b5b9
LP
1274 if (r == -EINVAL) /* not for a unit */
1275 return 0;
9ed794a3 1276 if (r < 0) {
9b420b3c
LP
1277 log_oom();
1278 return 0;
1279 }
554604b3 1280
c3350683 1281 machine = hashmap_get(m->machine_units, unit);
9b420b3c
LP
1282 if (!machine)
1283 return 0;
1284
9b420b3c 1285 machine_add_to_gc_queue(machine);
c3350683
LP
1286 return 0;
1287}
554604b3 1288
19070062 1289int match_unit_removed(sd_bus_message *message, void *userdata, sd_bus_error *error) {
c3350683 1290 const char *path, *unit;
99534007 1291 Manager *m = ASSERT_PTR(userdata);
c3350683
LP
1292 Machine *machine;
1293 int r;
554604b3 1294
c3350683 1295 assert(message);
554604b3 1296
c3350683
LP
1297 r = sd_bus_message_read(message, "so", &unit, &path);
1298 if (r < 0) {
ebcf1f97 1299 bus_log_parse_error(r);
9b420b3c 1300 return 0;
554604b3
LP
1301 }
1302
c3350683 1303 machine = hashmap_get(m->machine_units, unit);
9b420b3c
LP
1304 if (!machine)
1305 return 0;
1306
9b420b3c 1307 machine_add_to_gc_queue(machine);
c3350683
LP
1308 return 0;
1309}
554604b3 1310
19070062 1311int match_reloading(sd_bus_message *message, void *userdata, sd_bus_error *error) {
99534007 1312 Manager *m = ASSERT_PTR(userdata);
a658cafa 1313 Machine *machine;
c3350683 1314 int b, r;
554604b3 1315
19070062 1316 assert(message);
554604b3 1317
c3350683
LP
1318 r = sd_bus_message_read(message, "b", &b);
1319 if (r < 0) {
ebcf1f97 1320 bus_log_parse_error(r);
65d73cf0 1321 return 0;
554604b3 1322 }
a658cafa
LP
1323 if (b)
1324 return 0;
554604b3 1325
a658cafa
LP
1326 /* systemd finished reloading, let's recheck all our machines */
1327 log_debug("System manager has been reloaded, rechecking machines...");
554604b3 1328
90e74a66 1329 HASHMAP_FOREACH(machine, m->machines)
a658cafa 1330 machine_add_to_gc_queue(machine);
554604b3
LP
1331
1332 return 0;
1333}
1334
b92d0b4c
LP
1335int manager_unref_unit(
1336 Manager *m,
1337 const char *unit,
1338 sd_bus_error *error) {
1339
1340 assert(m);
1341 assert(unit);
1342
14456f76 1343 return bus_call_method(m->bus, bus_systemd_mgr, "UnrefUnit", error, NULL, "s", unit);
b92d0b4c
LP
1344}
1345
c3350683 1346int manager_stop_unit(Manager *manager, const char *unit, sd_bus_error *error, char **job) {
4afd3348 1347 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
1ee306e1
LP
1348 int r;
1349
1350 assert(manager);
1351 assert(unit);
1352
14456f76 1353 r = bus_call_method(manager->bus, bus_systemd_mgr, "StopUnit", error, &reply, "ss", unit, "fail");
1ee306e1 1354 if (r < 0) {
955a6329
ZJS
1355 if (sd_bus_error_has_names(error, BUS_ERROR_NO_SUCH_UNIT,
1356 BUS_ERROR_LOAD_FAILED)) {
6797c324
LP
1357
1358 if (job)
1359 *job = NULL;
1360
c3350683 1361 sd_bus_error_free(error);
6797c324
LP
1362 return 0;
1363 }
1364
1ee306e1
LP
1365 return r;
1366 }
1367
1368 if (job) {
1369 const char *j;
1370 char *copy;
1371
c3350683
LP
1372 r = sd_bus_message_read(reply, "o", &j);
1373 if (r < 0)
1374 return r;
1ee306e1
LP
1375
1376 copy = strdup(j);
1377 if (!copy)
1378 return -ENOMEM;
1379
1380 *job = copy;
1381 }
1382
6797c324 1383 return 1;
1ee306e1
LP
1384}
1385
de58a50e 1386int manager_kill_unit(Manager *manager, const char *unit, int signo, sd_bus_error *error) {
1ee306e1
LP
1387 assert(manager);
1388 assert(unit);
1389
14456f76 1390 return bus_call_method(manager->bus, bus_systemd_mgr, "KillUnit", error, NULL, "ssi", unit, "all", signo);
1ee306e1
LP
1391}
1392
1393int manager_unit_is_active(Manager *manager, const char *unit) {
4afd3348
LP
1394 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1395 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
1ee306e1 1396 _cleanup_free_ char *path = NULL;
1ee306e1 1397 const char *state;
1ee306e1
LP
1398 int r;
1399
1400 assert(manager);
1401 assert(unit);
1402
1ee306e1
LP
1403 path = unit_dbus_path_from_name(unit);
1404 if (!path)
1405 return -ENOMEM;
1406
c3350683 1407 r = sd_bus_get_property(
1ee306e1
LP
1408 manager->bus,
1409 "org.freedesktop.systemd1",
1410 path,
c3350683
LP
1411 "org.freedesktop.systemd1.Unit",
1412 "ActiveState",
1ee306e1 1413 &error,
c3350683
LP
1414 &reply,
1415 "s");
1ee306e1 1416 if (r < 0) {
955a6329
ZJS
1417 if (sd_bus_error_has_names(&error, SD_BUS_ERROR_NO_REPLY,
1418 SD_BUS_ERROR_DISCONNECTED))
6797c324 1419 return true;
6797c324 1420
955a6329
ZJS
1421 if (sd_bus_error_has_names(&error, BUS_ERROR_NO_SUCH_UNIT,
1422 BUS_ERROR_LOAD_FAILED))
6797c324 1423 return false;
6797c324 1424
1ee306e1
LP
1425 return r;
1426 }
1427
c3350683
LP
1428 r = sd_bus_message_read(reply, "s", &state);
1429 if (r < 0)
1ee306e1 1430 return -EINVAL;
1ee306e1 1431
9b420b3c 1432 return !STR_IN_SET(state, "inactive", "failed");
1ee306e1 1433}
bd16acf3 1434
c3350683 1435int manager_job_is_active(Manager *manager, const char *path) {
4afd3348
LP
1436 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1437 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
c3350683 1438 int r;
bd16acf3 1439
c3350683
LP
1440 assert(manager);
1441 assert(path);
bd16acf3 1442
c3350683
LP
1443 r = sd_bus_get_property(
1444 manager->bus,
1445 "org.freedesktop.systemd1",
1446 path,
1447 "org.freedesktop.systemd1.Job",
1448 "State",
1449 &error,
1450 &reply,
1451 "s");
1452 if (r < 0) {
955a6329
ZJS
1453 if (sd_bus_error_has_names(&error, SD_BUS_ERROR_NO_REPLY,
1454 SD_BUS_ERROR_DISCONNECTED))
c3350683 1455 return true;
bd16acf3 1456
c3350683
LP
1457 if (sd_bus_error_has_name(&error, SD_BUS_ERROR_UNKNOWN_OBJECT))
1458 return false;
bd16acf3 1459
bd16acf3 1460 return r;
c3350683 1461 }
bd16acf3 1462
c3350683
LP
1463 /* We don't actually care about the state really. The fact
1464 * that we could read the job state is enough for us */
bd16acf3 1465
c3350683 1466 return true;
bd16acf3 1467}
ab49725f
KS
1468
1469int manager_get_machine_by_pid(Manager *m, pid_t pid, Machine **machine) {
ab49725f
KS
1470 Machine *mm;
1471 int r;
1472
1473 assert(m);
1474 assert(pid >= 1);
1475 assert(machine);
1476
4a0b58c4 1477 mm = hashmap_get(m->machine_leaders, PID_TO_PTR(pid));
077c8c36
LP
1478 if (!mm) {
1479 _cleanup_free_ char *unit = NULL;
ab49725f 1480
077c8c36
LP
1481 r = cg_pid_get_unit(pid, &unit);
1482 if (r >= 0)
1483 mm = hashmap_get(m->machine_units, unit);
1484 }
ab49725f
KS
1485 if (!mm)
1486 return 0;
1487
1488 *machine = mm;
1489 return 1;
1490}
1491
1492int manager_add_machine(Manager *m, const char *name, Machine **_machine) {
1493 Machine *machine;
359e8d76 1494 int r;
ab49725f
KS
1495
1496 assert(m);
1497 assert(name);
1498
1499 machine = hashmap_get(m->machines, name);
1500 if (!machine) {
359e8d76
DT
1501 r = machine_new(m, _MACHINE_CLASS_INVALID, name, &machine);
1502 if (r < 0)
1503 return r;
ab49725f
KS
1504 }
1505
1506 if (_machine)
1507 *_machine = machine;
1508
1509 return 0;
1510}