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