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