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