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