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