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