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