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