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