]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/machine/machined-dbus.c
shared: actually move all BusLocator related calls to bus-locator.c
[thirdparty/systemd.git] / src / machine / machined-dbus.c
CommitLineData
53e1b683 1/* SPDX-License-Identifier: LGPL-2.1+ */
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"
9b71e4ab 11#include "bus-locator.h"
269e4d2d 12#include "bus-polkit.h"
23c80348 13#include "cgroup-util.h"
66855de7 14#include "errno-util.h"
3ffd4af2 15#include "fd-util.h"
03c2b288 16#include "fileio.h"
f97b34a6 17#include "format-util.h"
25300b5a 18#include "hostname-util.h"
3ffd4af2 19#include "image-dbus.h"
a90fb858 20#include "io-util.h"
3ffd4af2 21#include "machine-dbus.h"
003dffde 22#include "machine-image.h"
4cee5eed 23#include "machine-pool.h"
c3350683 24#include "machined.h"
204f52e3 25#include "missing_capability.h"
3ffd4af2
LP
26#include "path-util.h"
27#include "process-util.h"
15a5e950 28#include "stdio-util.h"
3ffd4af2 29#include "strv.h"
e4de7287 30#include "tmpfile-util.h"
3ffd4af2 31#include "unit-name.h"
b1d4f8e1 32#include "user-util.h"
1ee306e1 33
74c308ae 34static BUS_DEFINE_PROPERTY_GET_GLOBAL(property_get_pool_path, "s", "/var/lib/machines");
160e3793
LP
35
36static int property_get_pool_usage(
37 sd_bus *bus,
38 const char *path,
39 const char *interface,
40 const char *property,
41 sd_bus_message *reply,
42 void *userdata,
43 sd_bus_error *error) {
44
45 _cleanup_close_ int fd = -1;
46 uint64_t usage = (uint64_t) -1;
160e3793
LP
47
48 assert(bus);
49 assert(reply);
50
160e3793
LP
51 fd = open("/var/lib/machines", O_RDONLY|O_CLOEXEC|O_DIRECTORY);
52 if (fd >= 0) {
53 BtrfsQuotaInfo q;
54
5bcd08db 55 if (btrfs_subvol_get_subtree_quota_fd(fd, 0, &q) >= 0)
cb81cd80 56 usage = q.referenced;
160e3793
LP
57 }
58
160e3793
LP
59 return sd_bus_message_append(reply, "t", usage);
60}
61
62static int property_get_pool_limit(
63 sd_bus *bus,
64 const char *path,
65 const char *interface,
66 const char *property,
67 sd_bus_message *reply,
68 void *userdata,
69 sd_bus_error *error) {
70
71 _cleanup_close_ int fd = -1;
72 uint64_t size = (uint64_t) -1;
160e3793
LP
73
74 assert(bus);
75 assert(reply);
76
160e3793
LP
77 fd = open("/var/lib/machines", O_RDONLY|O_CLOEXEC|O_DIRECTORY);
78 if (fd >= 0) {
79 BtrfsQuotaInfo q;
80
5bcd08db 81 if (btrfs_subvol_get_subtree_quota_fd(fd, 0, &q) >= 0)
cb81cd80 82 size = q.referenced_max;
160e3793
LP
83 }
84
160e3793
LP
85 return sd_bus_message_append(reply, "t", size);
86}
87
19070062 88static int method_get_machine(sd_bus_message *message, void *userdata, sd_bus_error *error) {
c3350683
LP
89 _cleanup_free_ char *p = NULL;
90 Manager *m = userdata;
91 Machine *machine;
92 const char *name;
93 int r;
94
c3350683
LP
95 assert(message);
96 assert(m);
97
98 r = sd_bus_message_read(message, "s", &name);
99 if (r < 0)
ebcf1f97 100 return r;
c3350683
LP
101
102 machine = hashmap_get(m->machines, name);
103 if (!machine)
ebcf1f97 104 return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_MACHINE, "No machine '%s' known", name);
c3350683
LP
105
106 p = machine_bus_path(machine);
107 if (!p)
ebcf1f97 108 return -ENOMEM;
c3350683 109
df2d202e 110 return sd_bus_reply_method_return(message, "o", p);
c3350683
LP
111}
112
19070062 113static int method_get_image(sd_bus_message *message, void *userdata, sd_bus_error *error) {
c2ce6a3d 114 _cleanup_free_ char *p = NULL;
0a0e594a 115 _unused_ Manager *m = userdata;
c2ce6a3d
LP
116 const char *name;
117 int r;
118
c2ce6a3d
LP
119 assert(message);
120 assert(m);
121
122 r = sd_bus_message_read(message, "s", &name);
123 if (r < 0)
124 return r;
125
5ef46e5f 126 r = image_find(IMAGE_MACHINE, name, 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
LP
140 _cleanup_free_ char *p = NULL;
141 Manager *m = userdata;
142 Machine *machine = NULL;
4e724d9c 143 pid_t pid;
c3350683
LP
144 int r;
145
c3350683
LP
146 assert(message);
147 assert(m);
148
4e724d9c
LP
149 assert_cc(sizeof(pid_t) == sizeof(uint32_t));
150
c3350683
LP
151 r = sd_bus_message_read(message, "u", &pid);
152 if (r < 0)
ebcf1f97 153 return r;
c3350683 154
07b38ba5 155 if (pid < 0)
06820eaf
LP
156 return -EINVAL;
157
4e724d9c 158 if (pid == 0) {
4afd3348 159 _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL;
5b12334d
LP
160
161 r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_PID, &creds);
162 if (r < 0)
163 return r;
164
165 r = sd_bus_creds_get_pid(creds, &pid);
4e724d9c 166 if (r < 0)
ebcf1f97 167 return r;
4e724d9c
LP
168 }
169
c3350683
LP
170 r = manager_get_machine_by_pid(m, pid, &machine);
171 if (r < 0)
ebcf1f97 172 return r;
c3350683 173 if (!machine)
de0671ee 174 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
175
176 p = machine_bus_path(machine);
177 if (!p)
ebcf1f97 178 return -ENOMEM;
c3350683 179
df2d202e 180 return sd_bus_reply_method_return(message, "o", p);
c3350683
LP
181}
182
19070062 183static int method_list_machines(sd_bus_message *message, void *userdata, sd_bus_error *error) {
4afd3348 184 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
c3350683
LP
185 Manager *m = userdata;
186 Machine *machine;
187 Iterator i;
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
LP
200
201 HASHMAP_FOREACH(machine, m->machines, i) {
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;
7f0d207d 242 if (!machine_name_is_valid(name))
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
LP
259 if (read_network) {
260 size_t i;
261
262 r = sd_bus_message_read_array(message, 'i', (const void**) &netif, &n_netif);
263 if (r < 0)
264 return r;
265
266 n_netif /= sizeof(int32_t);
267
268 for (i = 0; i < n_netif; i++) {
269 if (netif[i] <= 0)
270 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid network interface index %i", netif[i]);
271 }
272 }
273
1ee306e1
LP
274 if (isempty(class))
275 c = _MACHINE_CLASS_INVALID;
276 else {
277 c = machine_class_from_string(class);
278 if (c < 0)
ebcf1f97 279 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid machine class parameter");
1ee306e1
LP
280 }
281
c3350683 282 if (leader == 1)
ebcf1f97 283 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid leader PID");
1ee306e1 284
c3350683 285 if (!isempty(root_directory) && !path_is_absolute(root_directory))
ebcf1f97 286 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Root directory must be empty or an absolute path");
1ee306e1 287
c3350683 288 if (leader == 0) {
4afd3348 289 _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL;
5b12334d
LP
290
291 r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_PID, &creds);
292 if (r < 0)
293 return r;
294
c3350683 295 assert_cc(sizeof(uint32_t) == sizeof(pid_t));
554604b3 296
5b12334d 297 r = sd_bus_creds_get_pid(creds, (pid_t*) &leader);
c3350683 298 if (r < 0)
ebcf1f97 299 return r;
c3350683 300 }
554604b3 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
LP
308
309 m->leader = leader;
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) {
89f7c846
LP
350 Manager *manager = userdata;
351 Machine *m = NULL;
352 int r;
353
19070062
LP
354 assert(message);
355 assert(manager);
356
9b5ed6fe 357 r = method_create_or_register_machine(manager, message, read_network, &m, error);
89f7c846
LP
358 if (r < 0)
359 return r;
360
361 r = sd_bus_message_enter_container(message, 'a', "(sv)");
362 if (r < 0)
363 goto fail;
364
ebcf1f97
LP
365 r = machine_start(m, message, error);
366 if (r < 0)
1ee306e1
LP
367 goto fail;
368
c3350683 369 m->create_message = sd_bus_message_ref(message);
c3350683 370 return 1;
1ee306e1
LP
371
372fail:
a3e7f417 373 machine_add_to_gc_queue(m);
89f7c846
LP
374 return r;
375}
1ee306e1 376
19070062
LP
377static int method_create_machine_with_network(sd_bus_message *message, void *userdata, sd_bus_error *error) {
378 return method_create_machine_internal(message, true, userdata, error);
9b5ed6fe
LP
379}
380
19070062
LP
381static int method_create_machine(sd_bus_message *message, void *userdata, sd_bus_error *error) {
382 return method_create_machine_internal(message, false, userdata, error);
9b5ed6fe
LP
383}
384
19070062 385static int method_register_machine_internal(sd_bus_message *message, bool read_network, void *userdata, sd_bus_error *error) {
89f7c846
LP
386 Manager *manager = userdata;
387 _cleanup_free_ char *p = NULL;
388 Machine *m = NULL;
389 int r;
390
19070062
LP
391 assert(message);
392 assert(manager);
393
9b5ed6fe 394 r = method_create_or_register_machine(manager, message, read_network, &m, error);
89f7c846
LP
395 if (r < 0)
396 return r;
397
398 r = cg_pid_get_unit(m->leader, &m->unit);
399 if (r < 0) {
048c386e
ZJS
400 r = sd_bus_error_set_errnof(error, r,
401 "Failed to determine unit of process "PID_FMT" : %m",
402 m->leader);
89f7c846
LP
403 goto fail;
404 }
405
406 r = machine_start(m, NULL, error);
407 if (r < 0)
408 goto fail;
409
410 p = machine_bus_path(m);
411 if (!p) {
412 r = -ENOMEM;
413 goto fail;
414 }
415
416 return sd_bus_reply_method_return(message, "o", p);
417
418fail:
419 machine_add_to_gc_queue(m);
1ee306e1
LP
420 return r;
421}
422
19070062
LP
423static int method_register_machine_with_network(sd_bus_message *message, void *userdata, sd_bus_error *error) {
424 return method_register_machine_internal(message, true, userdata, error);
9b5ed6fe
LP
425}
426
19070062
LP
427static int method_register_machine(sd_bus_message *message, void *userdata, sd_bus_error *error) {
428 return method_register_machine_internal(message, false, userdata, error);
9b5ed6fe
LP
429}
430
eecf0181 431static int redirect_method_to_machine(sd_bus_message *message, Manager *m, sd_bus_error *error, sd_bus_message_handler_t method) {
c3350683
LP
432 Machine *machine;
433 const char *name;
1ee306e1
LP
434 int r;
435
1ee306e1
LP
436 assert(message);
437 assert(m);
eecf0181 438 assert(method);
1ee306e1 439
c3350683
LP
440 r = sd_bus_message_read(message, "s", &name);
441 if (r < 0)
ebcf1f97 442 return sd_bus_error_set_errno(error, r);
1ee306e1 443
c3350683
LP
444 machine = hashmap_get(m->machines, name);
445 if (!machine)
ebcf1f97 446 return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_MACHINE, "No machine '%s' known", name);
1ee306e1 447
eecf0181 448 return method(message, machine, error);
c3350683 449}
1ee306e1 450
ef8ff92e
ZJS
451static int method_unregister_machine(sd_bus_message *message, void *userdata, sd_bus_error *error) {
452 return redirect_method_to_machine(message, userdata, error, bus_machine_method_unregister);
453}
454
eecf0181
LP
455static int method_terminate_machine(sd_bus_message *message, void *userdata, sd_bus_error *error) {
456 return redirect_method_to_machine(message, userdata, error, bus_machine_method_terminate);
457}
1ee306e1 458
eecf0181
LP
459static int method_kill_machine(sd_bus_message *message, void *userdata, sd_bus_error *error) {
460 return redirect_method_to_machine(message, userdata, error, bus_machine_method_kill);
878cd7e9
LP
461}
462
19070062 463static int method_get_machine_addresses(sd_bus_message *message, void *userdata, sd_bus_error *error) {
eecf0181 464 return redirect_method_to_machine(message, userdata, error, bus_machine_method_get_addresses);
c3350683 465}
1ee306e1 466
19070062 467static int method_get_machine_os_release(sd_bus_message *message, void *userdata, sd_bus_error *error) {
eecf0181 468 return redirect_method_to_machine(message, userdata, error, bus_machine_method_get_os_release);
717603e3
LP
469}
470
19070062 471static int method_list_images(sd_bus_message *message, void *userdata, sd_bus_error *error) {
4afd3348 472 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
b07ec5a1 473 _cleanup_hashmap_free_ Hashmap *images = NULL;
0a0e594a 474 _unused_ Manager *m = userdata;
cd61c3bf
LP
475 Image *image;
476 Iterator i;
477 int r;
478
cd61c3bf
LP
479 assert(message);
480 assert(m);
481
b07ec5a1 482 images = hashmap_new(&image_hash_ops);
cd61c3bf
LP
483 if (!images)
484 return -ENOMEM;
485
5ef46e5f 486 r = image_discover(IMAGE_MACHINE, images);
cd61c3bf
LP
487 if (r < 0)
488 return r;
489
490 r = sd_bus_message_new_method_return(message, &reply);
491 if (r < 0)
492 return r;
493
b6b18498 494 r = sd_bus_message_open_container(reply, 'a', "(ssbttto)");
cd61c3bf
LP
495 if (r < 0)
496 return r;
497
498 HASHMAP_FOREACH(image, images, i) {
499 _cleanup_free_ char *p = NULL;
500
501 p = image_bus_path(image->name);
502 if (!p)
503 return -ENOMEM;
504
b6b18498 505 r = sd_bus_message_append(reply, "(ssbttto)",
cd61c3bf
LP
506 image->name,
507 image_type_to_string(image->type),
508 image->read_only,
10f9c755
LP
509 image->crtime,
510 image->mtime,
c19de711 511 image->usage,
cd61c3bf
LP
512 p);
513 if (r < 0)
514 return r;
515 }
516
517 r = sd_bus_message_close_container(reply);
518 if (r < 0)
519 return r;
520
9030ca46 521 return sd_bus_send(NULL, reply, NULL);
cd61c3bf
LP
522}
523
19070062 524static int method_open_machine_pty(sd_bus_message *message, void *userdata, sd_bus_error *error) {
eecf0181 525 return redirect_method_to_machine(message, userdata, error, bus_machine_method_open_pty);
40205d70
LP
526}
527
19070062 528static int method_open_machine_login(sd_bus_message *message, void *userdata, sd_bus_error *error) {
eecf0181 529 return redirect_method_to_machine(message, userdata, error, bus_machine_method_open_login);
5f8cc96a
LP
530}
531
49af9e13 532static int method_open_machine_shell(sd_bus_message *message, void *userdata, sd_bus_error *error) {
eecf0181 533 return redirect_method_to_machine(message, userdata, error, bus_machine_method_open_shell);
49af9e13
LP
534}
535
19070062 536static int method_bind_mount_machine(sd_bus_message *message, void *userdata, sd_bus_error *error) {
eecf0181 537 return redirect_method_to_machine(message, userdata, error, bus_machine_method_bind_mount);
90adaa25
LP
538}
539
19070062 540static int method_copy_machine(sd_bus_message *message, void *userdata, sd_bus_error *error) {
eecf0181 541 return redirect_method_to_machine(message, userdata, error, bus_machine_method_copy);
0370612e
LP
542}
543
ae203207 544static int method_open_machine_root_directory(sd_bus_message *message, void *userdata, sd_bus_error *error) {
eecf0181 545 return redirect_method_to_machine(message, userdata, error, bus_machine_method_open_root_directory);
ae203207
LP
546}
547
3401419b 548static int method_get_machine_uid_shift(sd_bus_message *message, void *userdata, sd_bus_error *error) {
eecf0181 549 return redirect_method_to_machine(message, userdata, error, bus_machine_method_get_uid_shift);
3401419b
LP
550}
551
9b06c1e1 552static int redirect_method_to_image(sd_bus_message *message, Manager *m, sd_bus_error *error, sd_bus_message_handler_t method) {
08682124
LP
553 _cleanup_(image_unrefp) Image* i = NULL;
554 const char *name;
555 int r;
556
08682124 557 assert(message);
9b06c1e1
LP
558 assert(m);
559 assert(method);
08682124
LP
560
561 r = sd_bus_message_read(message, "s", &name);
562 if (r < 0)
563 return r;
564
565 if (!image_name_is_valid(name))
566 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Image name '%s' is invalid.", name);
567
5ef46e5f 568 r = image_find(IMAGE_MACHINE, name, &i);
3a6ce860
LP
569 if (r == -ENOENT)
570 return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_IMAGE, "No image '%s' known", name);
08682124
LP
571 if (r < 0)
572 return r;
08682124 573
9b06c1e1
LP
574 i->userdata = m;
575 return method(message, i, error);
08682124
LP
576}
577
9b06c1e1
LP
578static int method_remove_image(sd_bus_message *message, void *userdata, sd_bus_error *error) {
579 return redirect_method_to_image(message, userdata, error, bus_image_method_remove);
580}
ebd93cb6 581
9b06c1e1
LP
582static int method_rename_image(sd_bus_message *message, void *userdata, sd_bus_error *error) {
583 return redirect_method_to_image(message, userdata, error, bus_image_method_rename);
ebd93cb6
LP
584}
585
19070062 586static int method_clone_image(sd_bus_message *message, void *userdata, sd_bus_error *error) {
9b06c1e1 587 return redirect_method_to_image(message, userdata, error, bus_image_method_clone);
ebd93cb6
LP
588}
589
19070062 590static int method_mark_image_read_only(sd_bus_message *message, void *userdata, sd_bus_error *error) {
9b06c1e1 591 return redirect_method_to_image(message, userdata, error, bus_image_method_mark_read_only);
ebd93cb6
LP
592}
593
cf30a8c1 594static int method_get_image_hostname(sd_bus_message *message, void *userdata, sd_bus_error *error) {
9b06c1e1 595 return redirect_method_to_image(message, userdata, error, bus_image_method_get_hostname);
cf30a8c1
LP
596}
597
598static int method_get_image_machine_id(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_id);
cf30a8c1
LP
600}
601
602static int method_get_image_machine_info(sd_bus_message *message, void *userdata, sd_bus_error *error) {
9b06c1e1 603 return redirect_method_to_image(message, userdata, error, bus_image_method_get_machine_info);
cf30a8c1
LP
604}
605
9153b02b 606static int method_get_image_os_release(sd_bus_message *message, void *userdata, sd_bus_error *error) {
9b06c1e1 607 return redirect_method_to_image(message, userdata, error, bus_image_method_get_os_release);
9153b02b
LP
608}
609
03c2b288
LP
610static int clean_pool_done(Operation *operation, int ret, sd_bus_error *error) {
611 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
612 _cleanup_fclose_ FILE *f = NULL;
613 bool success;
614 size_t n;
615 int r;
616
617 assert(operation);
618 assert(operation->extra_fd >= 0);
619
620 if (lseek(operation->extra_fd, 0, SEEK_SET) == (off_t) -1)
621 return -errno;
622
4fa744a3 623 f = take_fdopen(&operation->extra_fd, "r");
03c2b288
LP
624 if (!f)
625 return -errno;
626
03c2b288
LP
627 /* The resulting temporary file starts with a boolean value that indicates success or not. */
628 errno = 0;
629 n = fread(&success, 1, sizeof(success), f);
630 if (n != sizeof(success))
66855de7 631 return ret < 0 ? ret : errno_or_else(EIO);
03c2b288
LP
632
633 if (ret < 0) {
634 _cleanup_free_ char *name = NULL;
635
636 /* The clean-up operation failed. In this case the resulting temporary file should contain a boolean
637 * set to false followed by the name of the failed image. Let's try to read this and use it for the
638 * error message. If we can't read it, don't mind, and return the naked error. */
639
640 if (success) /* The resulting temporary file could not be updated, ignore it. */
641 return ret;
642
41f11239
LP
643 r = read_nul_string(f, LONG_LINE_MAX, &name);
644 if (r <= 0) /* Same here... */
03c2b288
LP
645 return ret;
646
647 return sd_bus_error_set_errnof(error, ret, "Failed to remove image %s: %m", name);
648 }
649
650 assert(success);
651
652 r = sd_bus_message_new_method_return(operation->message, &reply);
653 if (r < 0)
654 return r;
655
656 r = sd_bus_message_open_container(reply, 'a', "(st)");
657 if (r < 0)
658 return r;
659
660 /* On success the resulting temporary file will contain a list of image names that were removed followed by
661 * their size on disk. Let's read that and turn it into a bus message. */
662 for (;;) {
663 _cleanup_free_ char *name = NULL;
664 uint64_t size;
665
41f11239 666 r = read_nul_string(f, LONG_LINE_MAX, &name);
03c2b288
LP
667 if (r < 0)
668 return r;
41f11239 669 if (r == 0) /* reached the end */
03c2b288
LP
670 break;
671
672 errno = 0;
673 n = fread(&size, 1, sizeof(size), f);
674 if (n != sizeof(size))
66855de7 675 return errno_or_else(EIO);
03c2b288
LP
676
677 r = sd_bus_message_append(reply, "(st)", name, size);
678 if (r < 0)
679 return r;
680 }
681
682 r = sd_bus_message_close_container(reply);
683 if (r < 0)
684 return r;
685
686 return sd_bus_send(NULL, reply, NULL);
687}
688
d94c2b06
LP
689static int method_clean_pool(sd_bus_message *message, void *userdata, sd_bus_error *error) {
690 enum {
691 REMOVE_ALL,
692 REMOVE_HIDDEN,
693 } mode;
694
03c2b288
LP
695 _cleanup_close_pair_ int errno_pipe_fd[2] = { -1, -1 };
696 _cleanup_close_ int result_fd = -1;
d94c2b06 697 Manager *m = userdata;
03c2b288 698 Operation *operation;
d94c2b06 699 const char *mm;
03c2b288 700 pid_t child;
d94c2b06
LP
701 int r;
702
703 assert(message);
704
03c2b288
LP
705 if (m->n_operations >= OPERATIONS_MAX)
706 return sd_bus_error_setf(error, SD_BUS_ERROR_LIMITS_EXCEEDED, "Too many ongoing operations.");
707
d94c2b06
LP
708 r = sd_bus_message_read(message, "s", &mm);
709 if (r < 0)
710 return r;
711
712 if (streq(mm, "all"))
713 mode = REMOVE_ALL;
714 else if (streq(mm, "hidden"))
715 mode = REMOVE_HIDDEN;
716 else
717 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Unknown mode '%s'.", mm);
718
719 r = bus_verify_polkit_async(
720 message,
721 CAP_SYS_ADMIN,
722 "org.freedesktop.machine1.manage-machines",
723 NULL,
724 false,
725 UID_INVALID,
726 &m->polkit_registry,
727 error);
728 if (r < 0)
729 return r;
730 if (r == 0)
731 return 1; /* Will call us back */
732
03c2b288
LP
733 if (pipe2(errno_pipe_fd, O_CLOEXEC|O_NONBLOCK) < 0)
734 return sd_bus_error_set_errnof(error, errno, "Failed to create pipe: %m");
d94c2b06 735
03c2b288
LP
736 /* Create a temporary file we can dump information about deleted images into. We use a temporary file for this
737 * instead of a pipe or so, since this might grow quit large in theory and we don't want to process this
595bfe7d 738 * continuously */
992e8f22 739 result_fd = open_tmpfile_unlinkable(NULL, O_RDWR|O_CLOEXEC);
03c2b288
LP
740 if (result_fd < 0)
741 return -errno;
d94c2b06 742
03c2b288 743 /* This might be a slow operation, run it asynchronously in a background process */
4c253ed1
LP
744 r = safe_fork("(sd-clean)", FORK_RESET_SIGNALS, &child);
745 if (r < 0)
746 return sd_bus_error_set_errnof(error, r, "Failed to fork(): %m");
747 if (r == 0) {
b07ec5a1 748 _cleanup_hashmap_free_ Hashmap *images = NULL;
03c2b288
LP
749 bool success = true;
750 Image *image;
751 Iterator i;
752 ssize_t l;
d94c2b06 753
03c2b288 754 errno_pipe_fd[0] = safe_close(errno_pipe_fd[0]);
d94c2b06 755
b07ec5a1 756 images = hashmap_new(&image_hash_ops);
03c2b288
LP
757 if (!images) {
758 r = -ENOMEM;
759 goto child_fail;
760 }
d94c2b06 761
5ef46e5f 762 r = image_discover(IMAGE_MACHINE, images);
03c2b288
LP
763 if (r < 0)
764 goto child_fail;
d94c2b06 765
03c2b288
LP
766 l = write(result_fd, &success, sizeof(success));
767 if (l < 0) {
768 r = -errno;
769 goto child_fail;
770 }
d94c2b06 771
03c2b288
LP
772 HASHMAP_FOREACH(image, images, i) {
773
774 /* We can't remove vendor images (i.e. those in /usr) */
775 if (IMAGE_IS_VENDOR(image))
776 continue;
777
778 if (IMAGE_IS_HOST(image))
779 continue;
780
781 if (mode == REMOVE_HIDDEN && !IMAGE_IS_HIDDEN(image))
782 continue;
783
784 r = image_remove(image);
785 if (r == -EBUSY) /* keep images that are currently being used. */
786 continue;
787 if (r < 0) {
788 /* If the operation failed, let's override everything we wrote, and instead write there at which image we failed. */
789 success = false;
790 (void) ftruncate(result_fd, 0);
791 (void) lseek(result_fd, 0, SEEK_SET);
792 (void) write(result_fd, &success, sizeof(success));
793 (void) write(result_fd, image->name, strlen(image->name)+1);
794 goto child_fail;
795 }
796
797 l = write(result_fd, image->name, strlen(image->name)+1);
798 if (l < 0) {
799 r = -errno;
800 goto child_fail;
801 }
802
803 l = write(result_fd, &image->usage_exclusive, sizeof(image->usage_exclusive));
804 if (l < 0) {
805 r = -errno;
806 goto child_fail;
807 }
808 }
d94c2b06 809
03c2b288
LP
810 result_fd = safe_close(result_fd);
811 _exit(EXIT_SUCCESS);
812
813 child_fail:
814 (void) write(errno_pipe_fd[1], &r, sizeof(r));
815 _exit(EXIT_FAILURE);
d94c2b06
LP
816 }
817
03c2b288
LP
818 errno_pipe_fd[1] = safe_close(errno_pipe_fd[1]);
819
820 /* The clean-up might take a while, hence install a watch on the child and return */
821
822 r = operation_new(m, NULL, child, message, errno_pipe_fd[0], &operation);
823 if (r < 0) {
824 (void) sigkill_wait(child);
d94c2b06 825 return r;
03c2b288 826 }
d94c2b06 827
03c2b288
LP
828 operation->extra_fd = result_fd;
829 operation->done = clean_pool_done;
830
831 result_fd = -1;
832 errno_pipe_fd[0] = -1;
833
834 return 1;
d94c2b06
LP
835}
836
19070062 837static int method_set_pool_limit(sd_bus_message *message, void *userdata, sd_bus_error *error) {
d6ce17c7
LP
838 Manager *m = userdata;
839 uint64_t limit;
840 int r;
841
19070062
LP
842 assert(message);
843
d6ce17c7
LP
844 r = sd_bus_message_read(message, "t", &limit);
845 if (r < 0)
846 return r;
a90fb858
LP
847 if (!FILE_SIZE_VALID_OR_INFINITY(limit))
848 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "New limit out of range");
d6ce17c7
LP
849
850 r = bus_verify_polkit_async(
851 message,
852 CAP_SYS_ADMIN,
853 "org.freedesktop.machine1.manage-machines",
403ed0e5 854 NULL,
d6ce17c7
LP
855 false,
856 UID_INVALID,
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 */
5f7ecd61 865 r = setup_machine_directory(error);
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
LP
872 if (r == -ENOTTY)
873 return sd_bus_error_setf(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
LP
884static int method_map_from_machine_user(sd_bus_message *message, void *userdata, sd_bus_error *error) {
885 _cleanup_fclose_ FILE *f = NULL;
886 Manager *m = userdata;
887 const char *name, *p;
888 Machine *machine;
889 uint32_t uid;
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
LP
903 if (machine->class != MACHINE_CONTAINER)
904 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Not supported for non-container machines.");
905
c01ff965
LP
906 p = procfs_file_alloca(machine->leader, "uid_map");
907 f = fopen(p, "re");
908 if (!f)
909 return -errno;
910
911 for (;;) {
912 uid_t uid_base, uid_shift, uid_range, converted;
913 int k;
914
915 errno = 0;
916 k = fscanf(f, UID_FMT " " UID_FMT " " UID_FMT, &uid_base, &uid_shift, &uid_range);
917 if (k < 0 && feof(f))
918 break;
919 if (k != 3) {
66855de7
LP
920 if (ferror(f))
921 return errno_or_else(EIO);
c01ff965
LP
922
923 return -EIO;
924 }
925
926 if (uid < uid_base || uid >= uid_base + uid_range)
927 continue;
928
929 converted = uid - uid_base + uid_shift;
c077529b 930 if (!uid_is_valid(converted))
c01ff965
LP
931 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid user ID " UID_FMT, uid);
932
933 return sd_bus_reply_method_return(message, "u", (uint32_t) converted);
934 }
935
936 return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_USER_MAPPING, "Machine '%s' has no matching user mappings.", name);
937}
938
939static int method_map_to_machine_user(sd_bus_message *message, void *userdata, sd_bus_error *error) {
940 Manager *m = userdata;
941 Machine *machine;
942 uid_t uid;
943 Iterator i;
944 int r;
945
946 r = sd_bus_message_read(message, "u", &uid);
947 if (r < 0)
948 return r;
c077529b 949 if (!uid_is_valid(uid))
c01ff965
LP
950 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid user ID " UID_FMT, uid);
951 if (uid < 0x10000)
952 return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_USER_MAPPING, "User " UID_FMT " belongs to host UID range", uid);
953
954 HASHMAP_FOREACH(machine, m->machines, i) {
955 _cleanup_fclose_ FILE *f = NULL;
fbd0b64f 956 char p[STRLEN("/proc//uid_map") + DECIMAL_STR_MAX(pid_t) + 1];
c01ff965 957
a79366e2
LP
958 if (machine->class != MACHINE_CONTAINER)
959 continue;
960
c01ff965
LP
961 xsprintf(p, "/proc/" UID_FMT "/uid_map", machine->leader);
962 f = fopen(p, "re");
963 if (!f) {
fe4a1d0f 964 log_warning_errno(errno, "Failed to open %s, ignoring,", p);
c01ff965
LP
965 continue;
966 }
967
968 for (;;) {
969 _cleanup_free_ char *o = NULL;
970 uid_t uid_base, uid_shift, uid_range, converted;
971 int k;
972
973 errno = 0;
974 k = fscanf(f, UID_FMT " " UID_FMT " " UID_FMT, &uid_base, &uid_shift, &uid_range);
975 if (k < 0 && feof(f))
976 break;
977 if (k != 3) {
66855de7
LP
978 if (ferror(f))
979 return errno_or_else(EIO);
c01ff965
LP
980
981 return -EIO;
982 }
983
24f5a4c7
YW
984 /* The private user namespace is disabled, ignoring. */
985 if (uid_shift == 0)
986 continue;
987
c01ff965
LP
988 if (uid < uid_shift || uid >= uid_shift + uid_range)
989 continue;
990
991 converted = (uid - uid_shift + uid_base);
c077529b 992 if (!uid_is_valid(converted))
c01ff965
LP
993 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid user ID " UID_FMT, uid);
994
995 o = machine_bus_path(machine);
996 if (!o)
997 return -ENOMEM;
998
999 return sd_bus_reply_method_return(message, "sou", machine->name, o, (uint32_t) converted);
1000 }
1001 }
1002
1003 return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_USER_MAPPING, "No matching user mapping for " UID_FMT ".", uid);
1004}
1005
1006static int method_map_from_machine_group(sd_bus_message *message, void *groupdata, sd_bus_error *error) {
1007 _cleanup_fclose_ FILE *f = NULL;
1008 Manager *m = groupdata;
1009 const char *name, *p;
1010 Machine *machine;
1011 uint32_t gid;
1012 int r;
1013
1014 r = sd_bus_message_read(message, "su", &name, &gid);
1015 if (r < 0)
1016 return r;
1017
c077529b 1018 if (!gid_is_valid(gid))
c01ff965
LP
1019 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid group ID " GID_FMT, gid);
1020
1021 machine = hashmap_get(m->machines, name);
1022 if (!machine)
1023 return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_MACHINE, "No machine '%s' known", name);
1024
a79366e2
LP
1025 if (machine->class != MACHINE_CONTAINER)
1026 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Not supported for non-container machines.");
1027
c01ff965
LP
1028 p = procfs_file_alloca(machine->leader, "gid_map");
1029 f = fopen(p, "re");
1030 if (!f)
1031 return -errno;
1032
1033 for (;;) {
1034 gid_t gid_base, gid_shift, gid_range, converted;
1035 int k;
1036
1037 errno = 0;
1038 k = fscanf(f, GID_FMT " " GID_FMT " " GID_FMT, &gid_base, &gid_shift, &gid_range);
1039 if (k < 0 && feof(f))
1040 break;
1041 if (k != 3) {
66855de7
LP
1042 if (ferror(f))
1043 return errno_or_else(EIO);
c01ff965
LP
1044
1045 return -EIO;
1046 }
1047
1048 if (gid < gid_base || gid >= gid_base + gid_range)
1049 continue;
1050
1051 converted = gid - gid_base + gid_shift;
c077529b 1052 if (!gid_is_valid(converted))
c01ff965
LP
1053 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid group ID " GID_FMT, gid);
1054
1055 return sd_bus_reply_method_return(message, "u", (uint32_t) converted);
1056 }
1057
1058 return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_GROUP_MAPPING, "Machine '%s' has no matching group mappings.", name);
1059}
1060
1061static int method_map_to_machine_group(sd_bus_message *message, void *groupdata, sd_bus_error *error) {
1062 Manager *m = groupdata;
1063 Machine *machine;
1064 gid_t gid;
1065 Iterator i;
1066 int r;
1067
1068 r = sd_bus_message_read(message, "u", &gid);
1069 if (r < 0)
1070 return r;
c077529b 1071 if (!gid_is_valid(gid))
c01ff965
LP
1072 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid group ID " GID_FMT, gid);
1073 if (gid < 0x10000)
1074 return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_GROUP_MAPPING, "Group " GID_FMT " belongs to host GID range", gid);
1075
1076 HASHMAP_FOREACH(machine, m->machines, i) {
1077 _cleanup_fclose_ FILE *f = NULL;
fbd0b64f 1078 char p[STRLEN("/proc//gid_map") + DECIMAL_STR_MAX(pid_t) + 1];
c01ff965 1079
a79366e2
LP
1080 if (machine->class != MACHINE_CONTAINER)
1081 continue;
1082
c01ff965
LP
1083 xsprintf(p, "/proc/" GID_FMT "/gid_map", machine->leader);
1084 f = fopen(p, "re");
1085 if (!f) {
fe4a1d0f 1086 log_warning_errno(errno, "Failed to open %s, ignoring,", p);
c01ff965
LP
1087 continue;
1088 }
1089
1090 for (;;) {
1091 _cleanup_free_ char *o = NULL;
1092 gid_t gid_base, gid_shift, gid_range, converted;
1093 int k;
1094
1095 errno = 0;
1096 k = fscanf(f, GID_FMT " " GID_FMT " " GID_FMT, &gid_base, &gid_shift, &gid_range);
1097 if (k < 0 && feof(f))
1098 break;
1099 if (k != 3) {
66855de7
LP
1100 if (ferror(f))
1101 return errno_or_else(EIO);
c01ff965
LP
1102
1103 return -EIO;
1104 }
1105
24f5a4c7
YW
1106 /* The private user namespace is disabled, ignoring. */
1107 if (gid_shift == 0)
1108 continue;
1109
c01ff965
LP
1110 if (gid < gid_shift || gid >= gid_shift + gid_range)
1111 continue;
1112
1113 converted = (gid - gid_shift + gid_base);
c077529b 1114 if (!gid_is_valid(converted))
c01ff965
LP
1115 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid group ID " GID_FMT, gid);
1116
1117 o = machine_bus_path(machine);
1118 if (!o)
1119 return -ENOMEM;
1120
1121 return sd_bus_reply_method_return(message, "sou", machine->name, o, (uint32_t) converted);
1122 }
1123 }
1124
1125 return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_GROUP_MAPPING, "No matching group mapping for " GID_FMT ".", gid);
1126}
1127
c3350683
LP
1128const sd_bus_vtable manager_vtable[] = {
1129 SD_BUS_VTABLE_START(0),
bbe17ca1 1130
160e3793
LP
1131 SD_BUS_PROPERTY("PoolPath", "s", property_get_pool_path, 0, 0),
1132 SD_BUS_PROPERTY("PoolUsage", "t", property_get_pool_usage, 0, 0),
1133 SD_BUS_PROPERTY("PoolLimit", "t", property_get_pool_limit, 0, 0),
bbe17ca1
ZJS
1134
1135 SD_BUS_METHOD_WITH_NAMES("GetMachine",
1136 "s",
1137 SD_BUS_PARAM(name),
1138 "o",
1139 SD_BUS_PARAM(machine),
1140 method_get_machine,
1141 SD_BUS_VTABLE_UNPRIVILEGED),
1142 SD_BUS_METHOD_WITH_NAMES("GetImage",
1143 "s",
1144 SD_BUS_PARAM(name),
1145 "o",
1146 SD_BUS_PARAM(image),
1147 method_get_image,
1148 SD_BUS_VTABLE_UNPRIVILEGED),
1149 SD_BUS_METHOD_WITH_NAMES("GetMachineByPID",
1150 "u",
1151 SD_BUS_PARAM(pid),
1152 "o",
1153 SD_BUS_PARAM(machine),
1154 method_get_machine_by_pid,
1155 SD_BUS_VTABLE_UNPRIVILEGED),
1156 SD_BUS_METHOD_WITH_NAMES("ListMachines",
1157 NULL,,
1158 "a(ssso)",
1159 SD_BUS_PARAM(machines),
1160 method_list_machines,
1161 SD_BUS_VTABLE_UNPRIVILEGED),
1162 SD_BUS_METHOD_WITH_NAMES("ListImages",
1163 NULL,,
1164 "a(ssbttto)",
1165 SD_BUS_PARAM(images),
1166 method_list_images,
1167 SD_BUS_VTABLE_UNPRIVILEGED),
1168 SD_BUS_METHOD_WITH_NAMES("CreateMachine",
1169 "sayssusa(sv)",
1170 SD_BUS_PARAM(name)
1171 SD_BUS_PARAM(id)
1172 SD_BUS_PARAM(service)
1173 SD_BUS_PARAM(class)
1174 SD_BUS_PARAM(leader)
1175 SD_BUS_PARAM(root_directory)
1176 SD_BUS_PARAM(scope_properties),
1177 "o",
1178 SD_BUS_PARAM(path),
1179 method_create_machine, 0),
1180 SD_BUS_METHOD_WITH_NAMES("CreateMachineWithNetwork",
1181 "sayssusaia(sv)",
1182 SD_BUS_PARAM(name)
1183 SD_BUS_PARAM(id)
1184 SD_BUS_PARAM(service)
1185 SD_BUS_PARAM(class)
1186 SD_BUS_PARAM(leader)
1187 SD_BUS_PARAM(root_directory)
1188 SD_BUS_PARAM(ifindices)
1189 SD_BUS_PARAM(scope_properties),
1190 "o",
1191 SD_BUS_PARAM(path),
1192 method_create_machine_with_network, 0),
1193 SD_BUS_METHOD_WITH_NAMES("RegisterMachine",
1194 "sayssus",
1195 SD_BUS_PARAM(name)
1196 SD_BUS_PARAM(id)
1197 SD_BUS_PARAM(service)
1198 SD_BUS_PARAM(class)
1199 SD_BUS_PARAM(leader)
1200 SD_BUS_PARAM(root_directory),
1201 "o",
1202 SD_BUS_PARAM(path),
1203 method_register_machine, 0),
1204 SD_BUS_METHOD_WITH_NAMES("RegisterMachineWithNetwork",
1205 "sayssusai",
1206 SD_BUS_PARAM(name)
1207 SD_BUS_PARAM(id)
1208 SD_BUS_PARAM(service)
1209 SD_BUS_PARAM(class)
1210 SD_BUS_PARAM(leader)
1211 SD_BUS_PARAM(root_directory)
1212 SD_BUS_PARAM(ifindices),
1213 "o",
1214 SD_BUS_PARAM(path),
1215 method_register_machine_with_network, 0),
1216 SD_BUS_METHOD_WITH_NAMES("UnregisterMachine",
1217 "s",
1218 SD_BUS_PARAM(name),
1219 NULL,,
1220 method_unregister_machine,
1221 SD_BUS_VTABLE_UNPRIVILEGED),
1222 SD_BUS_METHOD_WITH_NAMES("TerminateMachine",
1223 "s",
1224 SD_BUS_PARAM(id),
1225 NULL,,
1226 method_terminate_machine,
1227 SD_BUS_VTABLE_UNPRIVILEGED),
1228 SD_BUS_METHOD_WITH_NAMES("KillMachine",
1229 "ssi",
1230 SD_BUS_PARAM(name)
1231 SD_BUS_PARAM(who)
1232 SD_BUS_PARAM(signal),
1233 NULL,,
1234 method_kill_machine,
1235 SD_BUS_VTABLE_UNPRIVILEGED),
1236 SD_BUS_METHOD_WITH_NAMES("GetMachineAddresses",
1237 "s",
1238 SD_BUS_PARAM(name),
1239 "a(iay)",
1240 SD_BUS_PARAM(addresses),
1241 method_get_machine_addresses,
1242 SD_BUS_VTABLE_UNPRIVILEGED),
1243 SD_BUS_METHOD_WITH_NAMES("GetMachineOSRelease",
1244 "s",
1245 SD_BUS_PARAM(name),
1246 "a{ss}",
1247 SD_BUS_PARAM(fields),
1248 method_get_machine_os_release,
1249 SD_BUS_VTABLE_UNPRIVILEGED),
1250 SD_BUS_METHOD_WITH_NAMES("OpenMachinePTY",
1251 "s",
1252 SD_BUS_PARAM(name),
1253 "hs",
1254 SD_BUS_PARAM(pty)
1255 SD_BUS_PARAM(pty_path),
1256 method_open_machine_pty,
1257 0),
1258 SD_BUS_METHOD_WITH_NAMES("OpenMachineLogin",
1259 "s",
1260 SD_BUS_PARAM(name),
1261 "hs",
1262 SD_BUS_PARAM(pty)
1263 SD_BUS_PARAM(pty_path),
1264 method_open_machine_login,
1265 SD_BUS_VTABLE_UNPRIVILEGED),
1266 SD_BUS_METHOD_WITH_NAMES("OpenMachineShell",
1267 "sssasas",
1268 SD_BUS_PARAM(name)
1269 SD_BUS_PARAM(user)
1270 SD_BUS_PARAM(path)
1271 SD_BUS_PARAM(args)
1272 SD_BUS_PARAM(environment),
1273 "hs",
1274 SD_BUS_PARAM(pty)
1275 SD_BUS_PARAM(pty_path),
1276 method_open_machine_shell,
1277 SD_BUS_VTABLE_UNPRIVILEGED),
1278 SD_BUS_METHOD_WITH_NAMES("BindMountMachine",
1279 "sssbb",
1280 SD_BUS_PARAM(name)
1281 SD_BUS_PARAM(source)
1282 SD_BUS_PARAM(destination)
1283 SD_BUS_PARAM(read_only)
1284 SD_BUS_PARAM(mkdir),
1285 NULL,,
1286 method_bind_mount_machine,
1287 SD_BUS_VTABLE_UNPRIVILEGED),
1288 SD_BUS_METHOD_WITH_NAMES("CopyFromMachine",
1289 "sss",
1290 SD_BUS_PARAM(name)
1291 SD_BUS_PARAM(source)
1292 SD_BUS_PARAM(destination),
1293 NULL,,
1294 method_copy_machine,
1295 SD_BUS_VTABLE_UNPRIVILEGED),
1296 SD_BUS_METHOD_WITH_NAMES("CopyToMachine",
1297 "sss",
1298 SD_BUS_PARAM(name)
1299 SD_BUS_PARAM(source)
1300 SD_BUS_PARAM(destination),
1301 NULL,,
1302 method_copy_machine,
1303 SD_BUS_VTABLE_UNPRIVILEGED),
1304 SD_BUS_METHOD_WITH_NAMES("OpenMachineRootDirectory",
1305 "s",
1306 SD_BUS_PARAM(name),
1307 "h",
1308 SD_BUS_PARAM(fd),
1309 method_open_machine_root_directory,
1310 SD_BUS_VTABLE_UNPRIVILEGED),
1311 SD_BUS_METHOD_WITH_NAMES("GetMachineUIDShift",
1312 "s",
1313 SD_BUS_PARAM(name),
1314 "u",
1315 SD_BUS_PARAM(shift),
1316 method_get_machine_uid_shift,
1317 SD_BUS_VTABLE_UNPRIVILEGED),
1318 SD_BUS_METHOD_WITH_NAMES("RemoveImage",
1319 "s",
1320 SD_BUS_PARAM(name),
1321 NULL,,
1322 method_remove_image,
1323 SD_BUS_VTABLE_UNPRIVILEGED),
1324 SD_BUS_METHOD_WITH_NAMES("RenameImage",
1325 "ss",
1326 SD_BUS_PARAM(name)
1327 SD_BUS_PARAM(new_name),
1328 NULL,,
1329 method_rename_image,
1330 SD_BUS_VTABLE_UNPRIVILEGED),
1331 SD_BUS_METHOD_WITH_NAMES("CloneImage",
1332 "ssb",
1333 SD_BUS_PARAM(name)
1334 SD_BUS_PARAM(new_name)
1335 SD_BUS_PARAM(read_only),
1336 NULL,,
1337 method_clone_image,
1338 SD_BUS_VTABLE_UNPRIVILEGED),
1339 SD_BUS_METHOD_WITH_NAMES("MarkImageReadOnly",
1340 "sb",
1341 SD_BUS_PARAM(name)
1342 SD_BUS_PARAM(read_only),
1343 NULL,,
1344 method_mark_image_read_only,
1345 SD_BUS_VTABLE_UNPRIVILEGED),
1346 SD_BUS_METHOD_WITH_NAMES("GetImageHostname",
1347 "s",
1348 SD_BUS_PARAM(name),
1349 "s",
1350 SD_BUS_PARAM(hostname),
1351 method_get_image_hostname,
1352 SD_BUS_VTABLE_UNPRIVILEGED),
1353 SD_BUS_METHOD_WITH_NAMES("GetImageMachineID",
1354 "s",
1355 SD_BUS_PARAM(name),
1356 "ay",
1357 SD_BUS_PARAM(id),
1358 method_get_image_machine_id,
1359 SD_BUS_VTABLE_UNPRIVILEGED),
1360 SD_BUS_METHOD_WITH_NAMES("GetImageMachineInfo",
1361 "s",
1362 SD_BUS_PARAM(name),
1363 "a{ss}",
1364 SD_BUS_PARAM(machine_info),
1365 method_get_image_machine_info,
1366 SD_BUS_VTABLE_UNPRIVILEGED),
1367 SD_BUS_METHOD_WITH_NAMES("GetImageOSRelease",
1368 "s",
1369 SD_BUS_PARAM(name),
1370 "a{ss}",
1371 SD_BUS_PARAM(os_release),
1372 method_get_image_os_release,
1373 SD_BUS_VTABLE_UNPRIVILEGED),
1374 SD_BUS_METHOD_WITH_NAMES("SetPoolLimit",
1375 "t",
1376 SD_BUS_PARAM(size),
1377 NULL,,
1378 method_set_pool_limit,
1379 SD_BUS_VTABLE_UNPRIVILEGED),
1380 SD_BUS_METHOD_WITH_NAMES("SetImageLimit",
1381 "st",
1382 SD_BUS_PARAM(name)
1383 SD_BUS_PARAM(size),
1384 NULL,,
1385 method_set_image_limit,
1386 SD_BUS_VTABLE_UNPRIVILEGED),
1387 SD_BUS_METHOD_WITH_NAMES("CleanPool",
1388 "s",
1389 SD_BUS_PARAM(mode),
1390 "a(st)",
1391 SD_BUS_PARAM(images),
1392 method_clean_pool,
1393 SD_BUS_VTABLE_UNPRIVILEGED),
1394 SD_BUS_METHOD_WITH_NAMES("MapFromMachineUser",
1395 "su",
1396 SD_BUS_PARAM(name)
1397 SD_BUS_PARAM(uid_inner),
1398 "u",
1399 SD_BUS_PARAM(uid_outer),
1400 method_map_from_machine_user,
1401 SD_BUS_VTABLE_UNPRIVILEGED),
1402 SD_BUS_METHOD_WITH_NAMES("MapToMachineUser",
1403 "u",
1404 SD_BUS_PARAM(uid_outer),
1405 "sou",
1406 SD_BUS_PARAM(machine_name)
1407 SD_BUS_PARAM(machine_path)
1408 SD_BUS_PARAM(uid_inner),
1409 method_map_to_machine_user,
1410 SD_BUS_VTABLE_UNPRIVILEGED),
1411 SD_BUS_METHOD_WITH_NAMES("MapFromMachineGroup",
1412 "su",
1413 SD_BUS_PARAM(name)
1414 SD_BUS_PARAM(gid_inner),
1415 "u",
1416 SD_BUS_PARAM(gid_outer),
1417 method_map_from_machine_group,
1418 SD_BUS_VTABLE_UNPRIVILEGED),
1419 SD_BUS_METHOD_WITH_NAMES("MapToMachineGroup",
1420 "u",
1421 SD_BUS_PARAM(gid_outer),
1422 "sou",
1423 SD_BUS_PARAM(machine_name)
1424 SD_BUS_PARAM(machine_path)
1425 SD_BUS_PARAM(gid_inner),
1426 method_map_to_machine_group,
1427 SD_BUS_VTABLE_UNPRIVILEGED),
1428
1429 SD_BUS_SIGNAL_WITH_NAMES("MachineNew",
1430 "so",
1431 SD_BUS_PARAM(machine)
1432 SD_BUS_PARAM(path),
1433 0),
1434 SD_BUS_SIGNAL_WITH_NAMES("MachineRemoved",
1435 "so",
1436 SD_BUS_PARAM(machine)
1437 SD_BUS_PARAM(path),
1438 0),
1439
c3350683
LP
1440 SD_BUS_VTABLE_END
1441};
1ee306e1 1442
4faa530c
ZJS
1443const BusObjectImplementation manager_object = {
1444 "/org/freedesktop/machine1",
1445 "org.freedesktop.machine1.Manager",
1446 .vtables = BUS_VTABLES(manager_vtable),
1447 .children = BUS_IMPLEMENTATIONS( &machine_object,
1448 &image_object ),
1449};
1450
19070062 1451int match_job_removed(sd_bus_message *message, void *userdata, sd_bus_error *error) {
c3350683 1452 const char *path, *result, *unit;
1ee306e1 1453 Manager *m = userdata;
c3350683
LP
1454 Machine *machine;
1455 uint32_t id;
1456 int r;
1ee306e1 1457
1ee306e1 1458 assert(message);
c3350683 1459 assert(m);
1ee306e1 1460
c3350683
LP
1461 r = sd_bus_message_read(message, "uoss", &id, &path, &unit, &result);
1462 if (r < 0) {
ebcf1f97 1463 bus_log_parse_error(r);
65d73cf0 1464 return 0;
c3350683 1465 }
6797c324 1466
c3350683
LP
1467 machine = hashmap_get(m->machine_units, unit);
1468 if (!machine)
1469 return 0;
6797c324 1470
c3350683 1471 if (streq_ptr(path, machine->scope_job)) {
491ac9f2 1472 machine->scope_job = mfree(machine->scope_job);
6797c324 1473
c3350683
LP
1474 if (machine->started) {
1475 if (streq(result, "done"))
1476 machine_send_create_reply(machine, NULL);
1477 else {
4afd3348 1478 _cleanup_(sd_bus_error_free) sd_bus_error e = SD_BUS_ERROR_NULL;
6797c324 1479
ebcf1f97 1480 sd_bus_error_setf(&e, BUS_ERROR_JOB_FAILED, "Start job for unit %s failed with '%s'", unit, result);
6797c324 1481
ebcf1f97 1482 machine_send_create_reply(machine, &e);
c3350683 1483 }
49f3fffd
LP
1484 }
1485
1486 machine_save(machine);
1ee306e1
LP
1487 }
1488
c3350683
LP
1489 machine_add_to_gc_queue(machine);
1490 return 0;
1ee306e1
LP
1491}
1492
19070062 1493int match_properties_changed(sd_bus_message *message, void *userdata, sd_bus_error *error) {
c3350683 1494 _cleanup_free_ char *unit = NULL;
49f3fffd 1495 const char *path;
c3350683
LP
1496 Manager *m = userdata;
1497 Machine *machine;
ebcf1f97 1498 int r;
554604b3 1499
c3350683
LP
1500 assert(message);
1501 assert(m);
554604b3 1502
c3350683
LP
1503 path = sd_bus_message_get_path(message);
1504 if (!path)
554604b3 1505 return 0;
554604b3 1506
ebcf1f97 1507 r = unit_name_from_dbus_path(path, &unit);
e5f5b5b9
LP
1508 if (r == -EINVAL) /* not for a unit */
1509 return 0;
9ed794a3 1510 if (r < 0) {
9b420b3c
LP
1511 log_oom();
1512 return 0;
1513 }
554604b3 1514
c3350683 1515 machine = hashmap_get(m->machine_units, unit);
9b420b3c
LP
1516 if (!machine)
1517 return 0;
1518
9b420b3c 1519 machine_add_to_gc_queue(machine);
c3350683
LP
1520 return 0;
1521}
554604b3 1522
19070062 1523int match_unit_removed(sd_bus_message *message, void *userdata, sd_bus_error *error) {
c3350683
LP
1524 const char *path, *unit;
1525 Manager *m = userdata;
1526 Machine *machine;
1527 int r;
554604b3 1528
c3350683
LP
1529 assert(message);
1530 assert(m);
554604b3 1531
c3350683
LP
1532 r = sd_bus_message_read(message, "so", &unit, &path);
1533 if (r < 0) {
ebcf1f97 1534 bus_log_parse_error(r);
9b420b3c 1535 return 0;
554604b3
LP
1536 }
1537
c3350683 1538 machine = hashmap_get(m->machine_units, unit);
9b420b3c
LP
1539 if (!machine)
1540 return 0;
1541
9b420b3c 1542 machine_add_to_gc_queue(machine);
c3350683
LP
1543 return 0;
1544}
554604b3 1545
19070062 1546int match_reloading(sd_bus_message *message, void *userdata, sd_bus_error *error) {
c3350683 1547 Manager *m = userdata;
a658cafa
LP
1548 Machine *machine;
1549 Iterator i;
c3350683 1550 int b, r;
554604b3 1551
19070062
LP
1552 assert(message);
1553 assert(m);
554604b3 1554
c3350683
LP
1555 r = sd_bus_message_read(message, "b", &b);
1556 if (r < 0) {
ebcf1f97 1557 bus_log_parse_error(r);
65d73cf0 1558 return 0;
554604b3 1559 }
a658cafa
LP
1560 if (b)
1561 return 0;
554604b3 1562
a658cafa
LP
1563 /* systemd finished reloading, let's recheck all our machines */
1564 log_debug("System manager has been reloaded, rechecking machines...");
554604b3 1565
a658cafa
LP
1566 HASHMAP_FOREACH(machine, m->machines, i)
1567 machine_add_to_gc_queue(machine);
554604b3
LP
1568
1569 return 0;
1570}
1571
b92d0b4c
LP
1572int manager_unref_unit(
1573 Manager *m,
1574 const char *unit,
1575 sd_bus_error *error) {
1576
1577 assert(m);
1578 assert(unit);
1579
14456f76 1580 return bus_call_method(m->bus, bus_systemd_mgr, "UnrefUnit", error, NULL, "s", unit);
b92d0b4c
LP
1581}
1582
c3350683 1583int manager_stop_unit(Manager *manager, const char *unit, sd_bus_error *error, char **job) {
4afd3348 1584 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
1ee306e1
LP
1585 int r;
1586
1587 assert(manager);
1588 assert(unit);
1589
14456f76 1590 r = bus_call_method(manager->bus, bus_systemd_mgr, "StopUnit", error, &reply, "ss", unit, "fail");
1ee306e1 1591 if (r < 0) {
c3350683
LP
1592 if (sd_bus_error_has_name(error, BUS_ERROR_NO_SUCH_UNIT) ||
1593 sd_bus_error_has_name(error, BUS_ERROR_LOAD_FAILED)) {
6797c324
LP
1594
1595 if (job)
1596 *job = NULL;
1597
c3350683 1598 sd_bus_error_free(error);
6797c324
LP
1599 return 0;
1600 }
1601
1ee306e1
LP
1602 return r;
1603 }
1604
1605 if (job) {
1606 const char *j;
1607 char *copy;
1608
c3350683
LP
1609 r = sd_bus_message_read(reply, "o", &j);
1610 if (r < 0)
1611 return r;
1ee306e1
LP
1612
1613 copy = strdup(j);
1614 if (!copy)
1615 return -ENOMEM;
1616
1617 *job = copy;
1618 }
1619
6797c324 1620 return 1;
1ee306e1
LP
1621}
1622
de58a50e 1623int manager_kill_unit(Manager *manager, const char *unit, int signo, sd_bus_error *error) {
1ee306e1
LP
1624 assert(manager);
1625 assert(unit);
1626
14456f76 1627 return bus_call_method(manager->bus, bus_systemd_mgr, "KillUnit", error, NULL, "ssi", unit, "all", signo);
1ee306e1
LP
1628}
1629
1630int manager_unit_is_active(Manager *manager, const char *unit) {
4afd3348
LP
1631 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1632 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
1ee306e1 1633 _cleanup_free_ char *path = NULL;
1ee306e1 1634 const char *state;
1ee306e1
LP
1635 int r;
1636
1637 assert(manager);
1638 assert(unit);
1639
1ee306e1
LP
1640 path = unit_dbus_path_from_name(unit);
1641 if (!path)
1642 return -ENOMEM;
1643
c3350683 1644 r = sd_bus_get_property(
1ee306e1
LP
1645 manager->bus,
1646 "org.freedesktop.systemd1",
1647 path,
c3350683
LP
1648 "org.freedesktop.systemd1.Unit",
1649 "ActiveState",
1ee306e1 1650 &error,
c3350683
LP
1651 &reply,
1652 "s");
1ee306e1 1653 if (r < 0) {
c3350683
LP
1654 if (sd_bus_error_has_name(&error, SD_BUS_ERROR_NO_REPLY) ||
1655 sd_bus_error_has_name(&error, SD_BUS_ERROR_DISCONNECTED))
6797c324 1656 return true;
6797c324 1657
c3350683
LP
1658 if (sd_bus_error_has_name(&error, BUS_ERROR_NO_SUCH_UNIT) ||
1659 sd_bus_error_has_name(&error, BUS_ERROR_LOAD_FAILED))
6797c324 1660 return false;
6797c324 1661
1ee306e1
LP
1662 return r;
1663 }
1664
c3350683
LP
1665 r = sd_bus_message_read(reply, "s", &state);
1666 if (r < 0)
1ee306e1 1667 return -EINVAL;
1ee306e1 1668
9b420b3c 1669 return !STR_IN_SET(state, "inactive", "failed");
1ee306e1 1670}
bd16acf3 1671
c3350683 1672int manager_job_is_active(Manager *manager, const char *path) {
4afd3348
LP
1673 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1674 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
c3350683 1675 int r;
bd16acf3 1676
c3350683
LP
1677 assert(manager);
1678 assert(path);
bd16acf3 1679
c3350683
LP
1680 r = sd_bus_get_property(
1681 manager->bus,
1682 "org.freedesktop.systemd1",
1683 path,
1684 "org.freedesktop.systemd1.Job",
1685 "State",
1686 &error,
1687 &reply,
1688 "s");
1689 if (r < 0) {
1690 if (sd_bus_error_has_name(&error, SD_BUS_ERROR_NO_REPLY) ||
1691 sd_bus_error_has_name(&error, SD_BUS_ERROR_DISCONNECTED))
1692 return true;
bd16acf3 1693
c3350683
LP
1694 if (sd_bus_error_has_name(&error, SD_BUS_ERROR_UNKNOWN_OBJECT))
1695 return false;
bd16acf3 1696
bd16acf3 1697 return r;
c3350683 1698 }
bd16acf3 1699
c3350683
LP
1700 /* We don't actually care about the state really. The fact
1701 * that we could read the job state is enough for us */
bd16acf3 1702
c3350683 1703 return true;
bd16acf3 1704}
ab49725f
KS
1705
1706int manager_get_machine_by_pid(Manager *m, pid_t pid, Machine **machine) {
ab49725f
KS
1707 Machine *mm;
1708 int r;
1709
1710 assert(m);
1711 assert(pid >= 1);
1712 assert(machine);
1713
4a0b58c4 1714 mm = hashmap_get(m->machine_leaders, PID_TO_PTR(pid));
077c8c36
LP
1715 if (!mm) {
1716 _cleanup_free_ char *unit = NULL;
ab49725f 1717
077c8c36
LP
1718 r = cg_pid_get_unit(pid, &unit);
1719 if (r >= 0)
1720 mm = hashmap_get(m->machine_units, unit);
1721 }
ab49725f
KS
1722 if (!mm)
1723 return 0;
1724
1725 *machine = mm;
1726 return 1;
1727}
1728
1729int manager_add_machine(Manager *m, const char *name, Machine **_machine) {
1730 Machine *machine;
1731
1732 assert(m);
1733 assert(name);
1734
1735 machine = hashmap_get(m->machines, name);
1736 if (!machine) {
fbe55073 1737 machine = machine_new(m, _MACHINE_CLASS_INVALID, name);
ab49725f
KS
1738 if (!machine)
1739 return -ENOMEM;
1740 }
1741
1742 if (_machine)
1743 *_machine = machine;
1744
1745 return 0;
1746}