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