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