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