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