]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/machine/machined-dbus.c
Merge pull request #32942 from yuwata/test-journal-sync-more
[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 _cleanup_(pidref_done) PidRef pidref = PIDREF_NULL;
223 const char *name, *service, *class, *root_directory;
224 const int32_t *netif = NULL;
225 MachineClass c;
226 uint32_t leader;
227 sd_id128_t id;
228 const void *v;
229 Machine *m;
230 size_t n, n_netif = 0;
231 int r;
232
233 assert(manager);
234 assert(message);
235 assert(_m);
236
237 r = sd_bus_message_read(message, "s", &name);
238 if (r < 0)
239 return r;
240 if (!hostname_is_valid(name, 0))
241 return sd_bus_error_set(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid machine name");
242
243 r = sd_bus_message_read_array(message, 'y', &v, &n);
244 if (r < 0)
245 return r;
246 if (n == 0)
247 id = SD_ID128_NULL;
248 else if (n == 16)
249 memcpy(&id, v, n);
250 else
251 return sd_bus_error_set(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid machine ID parameter");
252
253 r = sd_bus_message_read(message, "ssus", &service, &class, &leader, &root_directory);
254 if (r < 0)
255 return r;
256
257 if (read_network) {
258 r = sd_bus_message_read_array(message, 'i', (const void**) &netif, &n_netif);
259 if (r < 0)
260 return r;
261
262 n_netif /= sizeof(int32_t);
263
264 for (size_t i = 0; i < n_netif; i++) {
265 if (netif[i] <= 0)
266 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid network interface index %i", netif[i]);
267 }
268 }
269
270 if (isempty(class))
271 c = _MACHINE_CLASS_INVALID;
272 else {
273 c = machine_class_from_string(class);
274 if (c < 0)
275 return sd_bus_error_set(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid machine class parameter");
276 }
277
278 if (leader == 1)
279 return sd_bus_error_set(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid leader PID");
280
281 if (!isempty(root_directory) && !path_is_absolute(root_directory))
282 return sd_bus_error_set(error, SD_BUS_ERROR_INVALID_ARGS, "Root directory must be empty or an absolute path");
283
284 if (leader == 0) {
285 _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL;
286
287 r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_PID, &creds);
288 if (r < 0)
289 return r;
290
291 assert_cc(sizeof(uint32_t) == sizeof(pid_t));
292
293 r = sd_bus_creds_get_pid(creds, (pid_t*) &leader);
294 if (r < 0)
295 return r;
296 }
297
298 r = pidref_set_pid(&pidref, leader);
299 if (r < 0)
300 return sd_bus_error_set_errnof(error, r, "Failed to pin process " PID_FMT ": %m", pidref.pid);
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 = TAKE_PIDREF(pidref);
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 = ASSERT_PTR(userdata);
351 Machine *m = NULL;
352 int r;
353
354 assert(message);
355
356 r = method_create_or_register_machine(manager, message, read_network, &m, error);
357 if (r < 0)
358 return r;
359
360 r = sd_bus_message_enter_container(message, 'a', "(sv)");
361 if (r < 0)
362 goto fail;
363
364 r = machine_start(m, message, error);
365 if (r < 0)
366 goto fail;
367
368 m->create_message = sd_bus_message_ref(message);
369 return 1;
370
371 fail:
372 machine_add_to_gc_queue(m);
373 return r;
374 }
375
376 static int method_create_machine_with_network(sd_bus_message *message, void *userdata, sd_bus_error *error) {
377 return method_create_machine_internal(message, true, userdata, error);
378 }
379
380 static int method_create_machine(sd_bus_message *message, void *userdata, sd_bus_error *error) {
381 return method_create_machine_internal(message, false, userdata, error);
382 }
383
384 static int method_register_machine_internal(sd_bus_message *message, bool read_network, void *userdata, sd_bus_error *error) {
385 Manager *manager = ASSERT_PTR(userdata);
386 _cleanup_free_ char *p = NULL;
387 Machine *m = NULL;
388 int r;
389
390 assert(message);
391
392 r = method_create_or_register_machine(manager, message, read_network, &m, error);
393 if (r < 0)
394 return r;
395
396 r = cg_pid_get_unit(m->leader.pid, &m->unit);
397 if (r < 0) {
398 r = sd_bus_error_set_errnof(error, r,
399 "Failed to determine unit of process "PID_FMT" : %m",
400 m->leader.pid);
401 goto fail;
402 }
403
404 r = machine_start(m, NULL, error);
405 if (r < 0)
406 goto fail;
407
408 p = machine_bus_path(m);
409 if (!p) {
410 r = -ENOMEM;
411 goto fail;
412 }
413
414 return sd_bus_reply_method_return(message, "o", p);
415
416 fail:
417 machine_add_to_gc_queue(m);
418 return r;
419 }
420
421 static int method_register_machine_with_network(sd_bus_message *message, void *userdata, sd_bus_error *error) {
422 return method_register_machine_internal(message, true, userdata, error);
423 }
424
425 static int method_register_machine(sd_bus_message *message, void *userdata, sd_bus_error *error) {
426 return method_register_machine_internal(message, false, userdata, error);
427 }
428
429 static int redirect_method_to_machine(sd_bus_message *message, Manager *m, sd_bus_error *error, sd_bus_message_handler_t method) {
430 Machine *machine;
431 const char *name;
432 int r;
433
434 assert(message);
435 assert(m);
436 assert(method);
437
438 r = sd_bus_message_read(message, "s", &name);
439 if (r < 0)
440 return sd_bus_error_set_errno(error, r);
441
442 machine = hashmap_get(m->machines, name);
443 if (!machine)
444 return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_MACHINE, "No machine '%s' known", name);
445
446 return method(message, machine, error);
447 }
448
449 static int method_unregister_machine(sd_bus_message *message, void *userdata, sd_bus_error *error) {
450 return redirect_method_to_machine(message, userdata, error, bus_machine_method_unregister);
451 }
452
453 static int method_terminate_machine(sd_bus_message *message, void *userdata, sd_bus_error *error) {
454 return redirect_method_to_machine(message, userdata, error, bus_machine_method_terminate);
455 }
456
457 static int method_kill_machine(sd_bus_message *message, void *userdata, sd_bus_error *error) {
458 return redirect_method_to_machine(message, userdata, error, bus_machine_method_kill);
459 }
460
461 static int method_get_machine_addresses(sd_bus_message *message, void *userdata, sd_bus_error *error) {
462 return redirect_method_to_machine(message, userdata, error, bus_machine_method_get_addresses);
463 }
464
465 static int method_get_machine_os_release(sd_bus_message *message, void *userdata, sd_bus_error *error) {
466 return redirect_method_to_machine(message, userdata, error, bus_machine_method_get_os_release);
467 }
468
469 static int method_list_images(sd_bus_message *message, void *userdata, sd_bus_error *error) {
470 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
471 _cleanup_hashmap_free_ Hashmap *images = NULL;
472 _unused_ Manager *m = ASSERT_PTR(userdata);
473 Image *image;
474 int r;
475
476 assert(message);
477
478 images = hashmap_new(&image_hash_ops);
479 if (!images)
480 return -ENOMEM;
481
482 r = image_discover(IMAGE_MACHINE, NULL, images);
483 if (r < 0)
484 return r;
485
486 r = sd_bus_message_new_method_return(message, &reply);
487 if (r < 0)
488 return r;
489
490 r = sd_bus_message_open_container(reply, 'a', "(ssbttto)");
491 if (r < 0)
492 return r;
493
494 HASHMAP_FOREACH(image, images) {
495 _cleanup_free_ char *p = NULL;
496
497 p = image_bus_path(image->name);
498 if (!p)
499 return -ENOMEM;
500
501 r = sd_bus_message_append(reply, "(ssbttto)",
502 image->name,
503 image_type_to_string(image->type),
504 image->read_only,
505 image->crtime,
506 image->mtime,
507 image->usage,
508 p);
509 if (r < 0)
510 return r;
511 }
512
513 r = sd_bus_message_close_container(reply);
514 if (r < 0)
515 return r;
516
517 return sd_bus_send(NULL, reply, NULL);
518 }
519
520 static int method_open_machine_pty(sd_bus_message *message, void *userdata, sd_bus_error *error) {
521 return redirect_method_to_machine(message, userdata, error, bus_machine_method_open_pty);
522 }
523
524 static int method_open_machine_login(sd_bus_message *message, void *userdata, sd_bus_error *error) {
525 return redirect_method_to_machine(message, userdata, error, bus_machine_method_open_login);
526 }
527
528 static int method_open_machine_shell(sd_bus_message *message, void *userdata, sd_bus_error *error) {
529 return redirect_method_to_machine(message, userdata, error, bus_machine_method_open_shell);
530 }
531
532 static int method_bind_mount_machine(sd_bus_message *message, void *userdata, sd_bus_error *error) {
533 return redirect_method_to_machine(message, userdata, error, bus_machine_method_bind_mount);
534 }
535
536 static int method_copy_machine(sd_bus_message *message, void *userdata, sd_bus_error *error) {
537 return redirect_method_to_machine(message, userdata, error, bus_machine_method_copy);
538 }
539
540 static int method_open_machine_root_directory(sd_bus_message *message, void *userdata, sd_bus_error *error) {
541 return redirect_method_to_machine(message, userdata, error, bus_machine_method_open_root_directory);
542 }
543
544 static int method_get_machine_uid_shift(sd_bus_message *message, void *userdata, sd_bus_error *error) {
545 return redirect_method_to_machine(message, userdata, error, bus_machine_method_get_uid_shift);
546 }
547
548 static int redirect_method_to_image(sd_bus_message *message, Manager *m, sd_bus_error *error, sd_bus_message_handler_t method) {
549 _cleanup_(image_unrefp) Image* i = NULL;
550 const char *name;
551 int r;
552
553 assert(message);
554 assert(m);
555 assert(method);
556
557 r = sd_bus_message_read(message, "s", &name);
558 if (r < 0)
559 return r;
560
561 if (!image_name_is_valid(name))
562 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Image name '%s' is invalid.", name);
563
564 r = image_find(IMAGE_MACHINE, name, NULL, &i);
565 if (r == -ENOENT)
566 return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_IMAGE, "No image '%s' known", name);
567 if (r < 0)
568 return r;
569
570 i->userdata = m;
571 return method(message, i, error);
572 }
573
574 static int method_remove_image(sd_bus_message *message, void *userdata, sd_bus_error *error) {
575 return redirect_method_to_image(message, userdata, error, bus_image_method_remove);
576 }
577
578 static int method_rename_image(sd_bus_message *message, void *userdata, sd_bus_error *error) {
579 return redirect_method_to_image(message, userdata, error, bus_image_method_rename);
580 }
581
582 static int method_clone_image(sd_bus_message *message, void *userdata, sd_bus_error *error) {
583 return redirect_method_to_image(message, userdata, error, bus_image_method_clone);
584 }
585
586 static int method_mark_image_read_only(sd_bus_message *message, void *userdata, sd_bus_error *error) {
587 return redirect_method_to_image(message, userdata, error, bus_image_method_mark_read_only);
588 }
589
590 static int method_get_image_hostname(sd_bus_message *message, void *userdata, sd_bus_error *error) {
591 return redirect_method_to_image(message, userdata, error, bus_image_method_get_hostname);
592 }
593
594 static int method_get_image_machine_id(sd_bus_message *message, void *userdata, sd_bus_error *error) {
595 return redirect_method_to_image(message, userdata, error, bus_image_method_get_machine_id);
596 }
597
598 static int method_get_image_machine_info(sd_bus_message *message, void *userdata, sd_bus_error *error) {
599 return redirect_method_to_image(message, userdata, error, bus_image_method_get_machine_info);
600 }
601
602 static int method_get_image_os_release(sd_bus_message *message, void *userdata, sd_bus_error *error) {
603 return redirect_method_to_image(message, userdata, error, bus_image_method_get_os_release);
604 }
605
606 static int clean_pool_done(Operation *operation, int ret, sd_bus_error *error) {
607 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
608 _cleanup_fclose_ FILE *f = NULL;
609 bool success;
610 size_t n;
611 int r;
612
613 assert(operation);
614 assert(operation->extra_fd >= 0);
615
616 if (lseek(operation->extra_fd, 0, SEEK_SET) < 0)
617 return -errno;
618
619 f = take_fdopen(&operation->extra_fd, "r");
620 if (!f)
621 return -errno;
622
623 /* The resulting temporary file starts with a boolean value that indicates success or not. */
624 errno = 0;
625 n = fread(&success, 1, sizeof(success), f);
626 if (n != sizeof(success))
627 return ret < 0 ? ret : errno_or_else(EIO);
628
629 if (ret < 0) {
630 _cleanup_free_ char *name = NULL;
631
632 /* The clean-up operation failed. In this case the resulting temporary file should contain a boolean
633 * set to false followed by the name of the failed image. Let's try to read this and use it for the
634 * error message. If we can't read it, don't mind, and return the naked error. */
635
636 if (success) /* The resulting temporary file could not be updated, ignore it. */
637 return ret;
638
639 r = read_nul_string(f, LONG_LINE_MAX, &name);
640 if (r <= 0) /* Same here... */
641 return ret;
642
643 return sd_bus_error_set_errnof(error, ret, "Failed to remove image %s: %m", name);
644 }
645
646 assert(success);
647
648 r = sd_bus_message_new_method_return(operation->message, &reply);
649 if (r < 0)
650 return r;
651
652 r = sd_bus_message_open_container(reply, 'a', "(st)");
653 if (r < 0)
654 return r;
655
656 /* On success the resulting temporary file will contain a list of image names that were removed followed by
657 * their size on disk. Let's read that and turn it into a bus message. */
658 for (;;) {
659 _cleanup_free_ char *name = NULL;
660 uint64_t size;
661
662 r = read_nul_string(f, LONG_LINE_MAX, &name);
663 if (r < 0)
664 return r;
665 if (r == 0) /* reached the end */
666 break;
667
668 errno = 0;
669 n = fread(&size, 1, sizeof(size), f);
670 if (n != sizeof(size))
671 return errno_or_else(EIO);
672
673 r = sd_bus_message_append(reply, "(st)", name, size);
674 if (r < 0)
675 return r;
676 }
677
678 r = sd_bus_message_close_container(reply);
679 if (r < 0)
680 return r;
681
682 return sd_bus_send(NULL, reply, NULL);
683 }
684
685 static int method_clean_pool(sd_bus_message *message, void *userdata, sd_bus_error *error) {
686 enum {
687 REMOVE_ALL,
688 REMOVE_HIDDEN,
689 } mode;
690
691 _cleanup_close_pair_ int errno_pipe_fd[2] = EBADF_PAIR;
692 _cleanup_close_ int result_fd = -EBADF;
693 Manager *m = userdata;
694 Operation *operation;
695 const char *mm;
696 pid_t child;
697 int r;
698
699 assert(message);
700
701 if (m->n_operations >= OPERATIONS_MAX)
702 return sd_bus_error_set(error, SD_BUS_ERROR_LIMITS_EXCEEDED, "Too many ongoing operations.");
703
704 r = sd_bus_message_read(message, "s", &mm);
705 if (r < 0)
706 return r;
707
708 if (streq(mm, "all"))
709 mode = REMOVE_ALL;
710 else if (streq(mm, "hidden"))
711 mode = REMOVE_HIDDEN;
712 else
713 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Unknown mode '%s'.", mm);
714
715 const char *details[] = {
716 "verb", "clean_pool",
717 "mode", mm,
718 NULL
719 };
720
721 r = bus_verify_polkit_async(
722 message,
723 "org.freedesktop.machine1.manage-machines",
724 details,
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, NULL, 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 = -EBADF;
830 errno_pipe_fd[0] = -EBADF;
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_set(error, SD_BUS_ERROR_INVALID_ARGS, "New limit out of range");
847
848 const char *details[] = {
849 "verb", "set_pool_limit",
850 NULL
851 };
852
853 r = bus_verify_polkit_async(
854 message,
855 "org.freedesktop.machine1.manage-machines",
856 details,
857 &m->polkit_registry,
858 error);
859 if (r < 0)
860 return r;
861 if (r == 0)
862 return 1; /* Will call us back */
863
864 /* Set up the machine directory if necessary */
865 r = setup_machine_directory(error, /* use_btrfs_subvol= */ true, /* use_btrfs_quota= */ true);
866 if (r < 0)
867 return r;
868
869 (void) btrfs_qgroup_set_limit("/var/lib/machines", 0, limit);
870
871 r = btrfs_subvol_set_subtree_quota_limit("/var/lib/machines", 0, limit);
872 if (r == -ENOTTY)
873 return sd_bus_error_set(error, SD_BUS_ERROR_NOT_SUPPORTED, "Quota is only supported on btrfs.");
874 if (r < 0)
875 return sd_bus_error_set_errnof(error, r, "Failed to adjust quota limit: %m");
876
877 return sd_bus_reply_method_return(message, NULL);
878 }
879
880 static int method_set_image_limit(sd_bus_message *message, void *userdata, sd_bus_error *error) {
881 return redirect_method_to_image(message, userdata, error, bus_image_method_set_limit);
882 }
883
884 static int method_map_from_machine_user(sd_bus_message *message, void *userdata, sd_bus_error *error) {
885 Manager *m = userdata;
886 const char *name;
887 Machine *machine;
888 uint32_t uid;
889 uid_t converted;
890 int r;
891
892 r = sd_bus_message_read(message, "su", &name, &uid);
893 if (r < 0)
894 return r;
895
896 if (!uid_is_valid(uid))
897 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid user ID " UID_FMT, uid);
898
899 machine = hashmap_get(m->machines, name);
900 if (!machine)
901 return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_MACHINE, "No machine '%s' known", name);
902
903 if (machine->class != MACHINE_CONTAINER)
904 return sd_bus_error_set(error, SD_BUS_ERROR_INVALID_ARGS, "Not supported for non-container machines.");
905
906 r = machine_translate_uid(machine, uid, &converted);
907 if (r == -ESRCH)
908 return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_USER_MAPPING, "Machine '%s' has no matching user mappings.", name);
909 if (r < 0)
910 return r;
911
912 return sd_bus_reply_method_return(message, "u", (uint32_t) converted);
913 }
914
915 static int method_map_to_machine_user(sd_bus_message *message, void *userdata, sd_bus_error *error) {
916 _cleanup_free_ char *o = NULL;
917 Manager *m = userdata;
918 Machine *machine;
919 uid_t uid, converted;
920 int r;
921
922 r = sd_bus_message_read(message, "u", &uid);
923 if (r < 0)
924 return r;
925 if (!uid_is_valid(uid))
926 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid user ID " UID_FMT, uid);
927 if (uid < 0x10000)
928 return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_USER_MAPPING, "User " UID_FMT " belongs to host UID range", uid);
929
930 r = manager_find_machine_for_uid(m, uid, &machine, &converted);
931 if (r < 0)
932 return r;
933 if (!r)
934 return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_USER_MAPPING, "No matching user mapping for " UID_FMT ".", uid);
935
936 o = machine_bus_path(machine);
937 if (!o)
938 return -ENOMEM;
939
940 return sd_bus_reply_method_return(message, "sou", machine->name, o, (uint32_t) converted);
941 }
942
943 static int method_map_from_machine_group(sd_bus_message *message, void *userdata, sd_bus_error *error) {
944 Manager *m = userdata;
945 const char *name;
946 Machine *machine;
947 gid_t converted;
948 uint32_t gid;
949 int r;
950
951 r = sd_bus_message_read(message, "su", &name, &gid);
952 if (r < 0)
953 return r;
954
955 if (!gid_is_valid(gid))
956 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid group ID " GID_FMT, gid);
957
958 machine = hashmap_get(m->machines, name);
959 if (!machine)
960 return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_MACHINE, "No machine '%s' known", name);
961
962 if (machine->class != MACHINE_CONTAINER)
963 return sd_bus_error_set(error, SD_BUS_ERROR_INVALID_ARGS, "Not supported for non-container machines.");
964
965 r = machine_translate_gid(machine, gid, &converted);
966 if (r == -ESRCH)
967 return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_USER_MAPPING, "Machine '%s' has no matching group mappings.", name);
968 if (r < 0)
969 return r;
970
971 return sd_bus_reply_method_return(message, "u", (uint32_t) converted);
972 }
973
974 static int method_map_to_machine_group(sd_bus_message *message, void *userdata, sd_bus_error *error) {
975 _cleanup_free_ char *o = NULL;
976 Manager *m = userdata;
977 Machine *machine;
978 gid_t gid, converted;
979 int r;
980
981 r = sd_bus_message_read(message, "u", &gid);
982 if (r < 0)
983 return r;
984 if (!gid_is_valid(gid))
985 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid group ID " GID_FMT, gid);
986 if (gid < 0x10000)
987 return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_GROUP_MAPPING, "Group " GID_FMT " belongs to host GID range", gid);
988
989 r = manager_find_machine_for_gid(m, gid, &machine, &converted);
990 if (r < 0)
991 return r;
992 if (!r)
993 return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_GROUP_MAPPING, "No matching group mapping for " GID_FMT ".", gid);
994
995 o = machine_bus_path(machine);
996 if (!o)
997 return -ENOMEM;
998
999 return sd_bus_reply_method_return(message, "sou", machine->name, o, (uint32_t) converted);
1000 }
1001
1002 const sd_bus_vtable manager_vtable[] = {
1003 SD_BUS_VTABLE_START(0),
1004
1005 SD_BUS_PROPERTY("PoolPath", "s", property_get_pool_path, 0, 0),
1006 SD_BUS_PROPERTY("PoolUsage", "t", property_get_pool_usage, 0, 0),
1007 SD_BUS_PROPERTY("PoolLimit", "t", property_get_pool_limit, 0, 0),
1008
1009 SD_BUS_METHOD_WITH_ARGS("GetMachine",
1010 SD_BUS_ARGS("s", name),
1011 SD_BUS_RESULT("o", machine),
1012 method_get_machine,
1013 SD_BUS_VTABLE_UNPRIVILEGED),
1014 SD_BUS_METHOD_WITH_ARGS("GetImage",
1015 SD_BUS_ARGS("s", name),
1016 SD_BUS_RESULT("o", image),
1017 method_get_image,
1018 SD_BUS_VTABLE_UNPRIVILEGED),
1019 SD_BUS_METHOD_WITH_ARGS("GetMachineByPID",
1020 SD_BUS_ARGS("u", pid),
1021 SD_BUS_RESULT("o", machine),
1022 method_get_machine_by_pid,
1023 SD_BUS_VTABLE_UNPRIVILEGED),
1024 SD_BUS_METHOD_WITH_ARGS("ListMachines",
1025 SD_BUS_NO_ARGS,
1026 SD_BUS_RESULT("a(ssso)", machines),
1027 method_list_machines,
1028 SD_BUS_VTABLE_UNPRIVILEGED),
1029 SD_BUS_METHOD_WITH_ARGS("ListImages",
1030 SD_BUS_NO_ARGS,
1031 SD_BUS_RESULT("a(ssbttto)", images),
1032 method_list_images,
1033 SD_BUS_VTABLE_UNPRIVILEGED),
1034 SD_BUS_METHOD_WITH_ARGS("CreateMachine",
1035 SD_BUS_ARGS("s", name, "ay", id, "s", service, "s", class, "u", leader, "s", root_directory, "a(sv)", scope_properties),
1036 SD_BUS_RESULT("o", path),
1037 method_create_machine, 0),
1038 SD_BUS_METHOD_WITH_ARGS("CreateMachineWithNetwork",
1039 SD_BUS_ARGS("s", name, "ay", id, "s", service, "s", class, "u", leader, "s", root_directory, "ai", ifindices, "a(sv)", scope_properties),
1040 SD_BUS_RESULT("o", path),
1041 method_create_machine_with_network, 0),
1042 SD_BUS_METHOD_WITH_ARGS("RegisterMachine",
1043 SD_BUS_ARGS("s", name, "ay", id, "s", service, "s", class, "u", leader, "s", root_directory),
1044 SD_BUS_RESULT("o", path),
1045 method_register_machine, 0),
1046 SD_BUS_METHOD_WITH_ARGS("RegisterMachineWithNetwork",
1047 SD_BUS_ARGS("s", name, "ay", id, "s", service, "s", class, "u", leader, "s", root_directory, "ai", ifindices),
1048 SD_BUS_RESULT("o", path),
1049 method_register_machine_with_network, 0),
1050 SD_BUS_METHOD_WITH_ARGS("UnregisterMachine",
1051 SD_BUS_ARGS("s", name),
1052 SD_BUS_NO_RESULT,
1053 method_unregister_machine,
1054 SD_BUS_VTABLE_UNPRIVILEGED),
1055 SD_BUS_METHOD_WITH_ARGS("TerminateMachine",
1056 SD_BUS_ARGS("s", id),
1057 SD_BUS_NO_RESULT,
1058 method_terminate_machine,
1059 SD_BUS_VTABLE_UNPRIVILEGED),
1060 SD_BUS_METHOD_WITH_ARGS("KillMachine",
1061 SD_BUS_ARGS("s", name, "s", who, "i", signal),
1062 SD_BUS_NO_RESULT,
1063 method_kill_machine,
1064 SD_BUS_VTABLE_UNPRIVILEGED),
1065 SD_BUS_METHOD_WITH_ARGS("GetMachineAddresses",
1066 SD_BUS_ARGS("s", name),
1067 SD_BUS_RESULT("a(iay)", addresses),
1068 method_get_machine_addresses,
1069 SD_BUS_VTABLE_UNPRIVILEGED),
1070 SD_BUS_METHOD_WITH_ARGS("GetMachineOSRelease",
1071 SD_BUS_ARGS("s", name),
1072 SD_BUS_RESULT("a{ss}", fields),
1073 method_get_machine_os_release,
1074 SD_BUS_VTABLE_UNPRIVILEGED),
1075 SD_BUS_METHOD_WITH_ARGS("OpenMachinePTY",
1076 SD_BUS_ARGS("s", name),
1077 SD_BUS_RESULT("h", pty, "s", pty_path),
1078 method_open_machine_pty,
1079 0),
1080 SD_BUS_METHOD_WITH_ARGS("OpenMachineLogin",
1081 SD_BUS_ARGS("s", name),
1082 SD_BUS_RESULT("h", pty, "s", pty_path),
1083 method_open_machine_login,
1084 SD_BUS_VTABLE_UNPRIVILEGED),
1085 SD_BUS_METHOD_WITH_ARGS("OpenMachineShell",
1086 SD_BUS_ARGS("s", name, "s", user, "s", path, "as", args, "as", environment),
1087 SD_BUS_RESULT("h", pty, "s", pty_path),
1088 method_open_machine_shell,
1089 SD_BUS_VTABLE_UNPRIVILEGED),
1090 SD_BUS_METHOD_WITH_ARGS("BindMountMachine",
1091 SD_BUS_ARGS("s", name, "s", source, "s", destination, "b", read_only, "b", mkdir),
1092 SD_BUS_NO_RESULT,
1093 method_bind_mount_machine,
1094 SD_BUS_VTABLE_UNPRIVILEGED),
1095 SD_BUS_METHOD_WITH_ARGS("CopyFromMachine",
1096 SD_BUS_ARGS("s", name, "s", source, "s", destination),
1097 SD_BUS_NO_RESULT,
1098 method_copy_machine,
1099 SD_BUS_VTABLE_UNPRIVILEGED),
1100 SD_BUS_METHOD_WITH_ARGS("CopyToMachine",
1101 SD_BUS_ARGS("s", name, "s", source, "s", destination),
1102 SD_BUS_NO_RESULT,
1103 method_copy_machine,
1104 SD_BUS_VTABLE_UNPRIVILEGED),
1105 SD_BUS_METHOD_WITH_ARGS("CopyFromMachineWithFlags",
1106 SD_BUS_ARGS("s", name, "s", source, "s", destination, "t", flags),
1107 SD_BUS_NO_RESULT,
1108 method_copy_machine,
1109 SD_BUS_VTABLE_UNPRIVILEGED),
1110 SD_BUS_METHOD_WITH_ARGS("CopyToMachineWithFlags",
1111 SD_BUS_ARGS("s", name, "s", source, "s", destination, "t", flags),
1112 SD_BUS_NO_RESULT,
1113 method_copy_machine,
1114 SD_BUS_VTABLE_UNPRIVILEGED),
1115 SD_BUS_METHOD_WITH_ARGS("OpenMachineRootDirectory",
1116 SD_BUS_ARGS("s", name),
1117 SD_BUS_RESULT("h", fd),
1118 method_open_machine_root_directory,
1119 SD_BUS_VTABLE_UNPRIVILEGED),
1120 SD_BUS_METHOD_WITH_ARGS("GetMachineUIDShift",
1121 SD_BUS_ARGS("s", name),
1122 SD_BUS_RESULT("u", shift),
1123 method_get_machine_uid_shift,
1124 SD_BUS_VTABLE_UNPRIVILEGED),
1125 SD_BUS_METHOD_WITH_ARGS("RemoveImage",
1126 SD_BUS_ARGS("s", name),
1127 SD_BUS_NO_RESULT,
1128 method_remove_image,
1129 SD_BUS_VTABLE_UNPRIVILEGED),
1130 SD_BUS_METHOD_WITH_ARGS("RenameImage",
1131 SD_BUS_ARGS("s", name, "s", new_name),
1132 SD_BUS_NO_RESULT,
1133 method_rename_image,
1134 SD_BUS_VTABLE_UNPRIVILEGED),
1135 SD_BUS_METHOD_WITH_ARGS("CloneImage",
1136 SD_BUS_ARGS("s", name, "s", new_name, "b", read_only),
1137 SD_BUS_NO_RESULT,
1138 method_clone_image,
1139 SD_BUS_VTABLE_UNPRIVILEGED),
1140 SD_BUS_METHOD_WITH_ARGS("MarkImageReadOnly",
1141 SD_BUS_ARGS("s", name, "b", read_only),
1142 SD_BUS_NO_RESULT,
1143 method_mark_image_read_only,
1144 SD_BUS_VTABLE_UNPRIVILEGED),
1145 SD_BUS_METHOD_WITH_ARGS("GetImageHostname",
1146 SD_BUS_ARGS("s", name),
1147 SD_BUS_RESULT("s", hostname),
1148 method_get_image_hostname,
1149 SD_BUS_VTABLE_UNPRIVILEGED),
1150 SD_BUS_METHOD_WITH_ARGS("GetImageMachineID",
1151 SD_BUS_ARGS("s", name),
1152 SD_BUS_RESULT("ay", id),
1153 method_get_image_machine_id,
1154 SD_BUS_VTABLE_UNPRIVILEGED),
1155 SD_BUS_METHOD_WITH_ARGS("GetImageMachineInfo",
1156 SD_BUS_ARGS("s", name),
1157 SD_BUS_RESULT("a{ss}", machine_info),
1158 method_get_image_machine_info,
1159 SD_BUS_VTABLE_UNPRIVILEGED),
1160 SD_BUS_METHOD_WITH_ARGS("GetImageOSRelease",
1161 SD_BUS_ARGS("s", name),
1162 SD_BUS_RESULT("a{ss}", os_release),
1163 method_get_image_os_release,
1164 SD_BUS_VTABLE_UNPRIVILEGED),
1165 SD_BUS_METHOD_WITH_ARGS("SetPoolLimit",
1166 SD_BUS_ARGS("t", size),
1167 SD_BUS_NO_RESULT,
1168 method_set_pool_limit,
1169 SD_BUS_VTABLE_UNPRIVILEGED),
1170 SD_BUS_METHOD_WITH_ARGS("SetImageLimit",
1171 SD_BUS_ARGS("s", name, "t", size),
1172 SD_BUS_NO_RESULT,
1173 method_set_image_limit,
1174 SD_BUS_VTABLE_UNPRIVILEGED),
1175 SD_BUS_METHOD_WITH_ARGS("CleanPool",
1176 SD_BUS_ARGS("s", mode),
1177 SD_BUS_RESULT("a(st)",images),
1178 method_clean_pool,
1179 SD_BUS_VTABLE_UNPRIVILEGED),
1180 SD_BUS_METHOD_WITH_ARGS("MapFromMachineUser",
1181 SD_BUS_ARGS("s", name, "u", uid_inner),
1182 SD_BUS_RESULT("u", uid_outer),
1183 method_map_from_machine_user,
1184 SD_BUS_VTABLE_UNPRIVILEGED),
1185 SD_BUS_METHOD_WITH_ARGS("MapToMachineUser",
1186 SD_BUS_ARGS("u", uid_outer),
1187 SD_BUS_RESULT("s", machine_name, "o", machine_path, "u", uid_inner),
1188 method_map_to_machine_user,
1189 SD_BUS_VTABLE_UNPRIVILEGED),
1190 SD_BUS_METHOD_WITH_ARGS("MapFromMachineGroup",
1191 SD_BUS_ARGS("s", name, "u", gid_inner),
1192 SD_BUS_RESULT("u", gid_outer),
1193 method_map_from_machine_group,
1194 SD_BUS_VTABLE_UNPRIVILEGED),
1195 SD_BUS_METHOD_WITH_ARGS("MapToMachineGroup",
1196 SD_BUS_ARGS("u", gid_outer),
1197 SD_BUS_RESULT("s", machine_name, "o", machine_path, "u", gid_inner),
1198 method_map_to_machine_group,
1199 SD_BUS_VTABLE_UNPRIVILEGED),
1200
1201 SD_BUS_SIGNAL_WITH_ARGS("MachineNew",
1202 SD_BUS_ARGS("s", machine, "o", path),
1203 0),
1204 SD_BUS_SIGNAL_WITH_ARGS("MachineRemoved",
1205 SD_BUS_ARGS("s", machine, "o", path),
1206 0),
1207
1208 SD_BUS_VTABLE_END
1209 };
1210
1211 const BusObjectImplementation manager_object = {
1212 "/org/freedesktop/machine1",
1213 "org.freedesktop.machine1.Manager",
1214 .vtables = BUS_VTABLES(manager_vtable),
1215 .children = BUS_IMPLEMENTATIONS( &machine_object,
1216 &image_object ),
1217 };
1218
1219 int match_job_removed(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1220 const char *path, *result, *unit;
1221 Manager *m = ASSERT_PTR(userdata);
1222 Machine *machine;
1223 uint32_t id;
1224 int r;
1225
1226 assert(message);
1227
1228 r = sd_bus_message_read(message, "uoss", &id, &path, &unit, &result);
1229 if (r < 0) {
1230 bus_log_parse_error(r);
1231 return 0;
1232 }
1233
1234 machine = hashmap_get(m->machine_units, unit);
1235 if (!machine)
1236 return 0;
1237
1238 if (streq_ptr(path, machine->scope_job)) {
1239 machine->scope_job = mfree(machine->scope_job);
1240
1241 if (machine->started) {
1242 if (streq(result, "done"))
1243 machine_send_create_reply(machine, NULL);
1244 else {
1245 _cleanup_(sd_bus_error_free) sd_bus_error e = SD_BUS_ERROR_NULL;
1246
1247 sd_bus_error_setf(&e, BUS_ERROR_JOB_FAILED, "Start job for unit %s failed with '%s'", unit, result);
1248
1249 machine_send_create_reply(machine, &e);
1250 }
1251 }
1252
1253 machine_save(machine);
1254 }
1255
1256 machine_add_to_gc_queue(machine);
1257 return 0;
1258 }
1259
1260 int match_properties_changed(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1261 _cleanup_free_ char *unit = NULL;
1262 const char *path;
1263 Manager *m = ASSERT_PTR(userdata);
1264 Machine *machine;
1265 int r;
1266
1267 assert(message);
1268
1269 path = sd_bus_message_get_path(message);
1270 if (!path)
1271 return 0;
1272
1273 r = unit_name_from_dbus_path(path, &unit);
1274 if (r == -EINVAL) /* not for a unit */
1275 return 0;
1276 if (r < 0) {
1277 log_oom();
1278 return 0;
1279 }
1280
1281 machine = hashmap_get(m->machine_units, unit);
1282 if (!machine)
1283 return 0;
1284
1285 machine_add_to_gc_queue(machine);
1286 return 0;
1287 }
1288
1289 int match_unit_removed(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1290 const char *path, *unit;
1291 Manager *m = ASSERT_PTR(userdata);
1292 Machine *machine;
1293 int r;
1294
1295 assert(message);
1296
1297 r = sd_bus_message_read(message, "so", &unit, &path);
1298 if (r < 0) {
1299 bus_log_parse_error(r);
1300 return 0;
1301 }
1302
1303 machine = hashmap_get(m->machine_units, unit);
1304 if (!machine)
1305 return 0;
1306
1307 machine_add_to_gc_queue(machine);
1308 return 0;
1309 }
1310
1311 int match_reloading(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1312 Manager *m = ASSERT_PTR(userdata);
1313 Machine *machine;
1314 int b, r;
1315
1316 assert(message);
1317
1318 r = sd_bus_message_read(message, "b", &b);
1319 if (r < 0) {
1320 bus_log_parse_error(r);
1321 return 0;
1322 }
1323 if (b)
1324 return 0;
1325
1326 /* systemd finished reloading, let's recheck all our machines */
1327 log_debug("System manager has been reloaded, rechecking machines...");
1328
1329 HASHMAP_FOREACH(machine, m->machines)
1330 machine_add_to_gc_queue(machine);
1331
1332 return 0;
1333 }
1334
1335 int manager_unref_unit(
1336 Manager *m,
1337 const char *unit,
1338 sd_bus_error *error) {
1339
1340 assert(m);
1341 assert(unit);
1342
1343 return bus_call_method(m->bus, bus_systemd_mgr, "UnrefUnit", error, NULL, "s", unit);
1344 }
1345
1346 int manager_stop_unit(Manager *manager, const char *unit, sd_bus_error *error, char **job) {
1347 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
1348 int r;
1349
1350 assert(manager);
1351 assert(unit);
1352
1353 r = bus_call_method(manager->bus, bus_systemd_mgr, "StopUnit", error, &reply, "ss", unit, "fail");
1354 if (r < 0) {
1355 if (sd_bus_error_has_names(error, BUS_ERROR_NO_SUCH_UNIT,
1356 BUS_ERROR_LOAD_FAILED)) {
1357
1358 if (job)
1359 *job = NULL;
1360
1361 sd_bus_error_free(error);
1362 return 0;
1363 }
1364
1365 return r;
1366 }
1367
1368 if (job) {
1369 const char *j;
1370 char *copy;
1371
1372 r = sd_bus_message_read(reply, "o", &j);
1373 if (r < 0)
1374 return r;
1375
1376 copy = strdup(j);
1377 if (!copy)
1378 return -ENOMEM;
1379
1380 *job = copy;
1381 }
1382
1383 return 1;
1384 }
1385
1386 int manager_kill_unit(Manager *manager, const char *unit, int signo, sd_bus_error *error) {
1387 assert(manager);
1388 assert(unit);
1389
1390 return bus_call_method(manager->bus, bus_systemd_mgr, "KillUnit", error, NULL, "ssi", unit, "all", signo);
1391 }
1392
1393 int manager_unit_is_active(Manager *manager, const char *unit) {
1394 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1395 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
1396 _cleanup_free_ char *path = NULL;
1397 const char *state;
1398 int r;
1399
1400 assert(manager);
1401 assert(unit);
1402
1403 path = unit_dbus_path_from_name(unit);
1404 if (!path)
1405 return -ENOMEM;
1406
1407 r = sd_bus_get_property(
1408 manager->bus,
1409 "org.freedesktop.systemd1",
1410 path,
1411 "org.freedesktop.systemd1.Unit",
1412 "ActiveState",
1413 &error,
1414 &reply,
1415 "s");
1416 if (r < 0) {
1417 if (sd_bus_error_has_names(&error, SD_BUS_ERROR_NO_REPLY,
1418 SD_BUS_ERROR_DISCONNECTED))
1419 return true;
1420
1421 if (sd_bus_error_has_names(&error, BUS_ERROR_NO_SUCH_UNIT,
1422 BUS_ERROR_LOAD_FAILED))
1423 return false;
1424
1425 return r;
1426 }
1427
1428 r = sd_bus_message_read(reply, "s", &state);
1429 if (r < 0)
1430 return -EINVAL;
1431
1432 return !STR_IN_SET(state, "inactive", "failed");
1433 }
1434
1435 int manager_job_is_active(Manager *manager, const char *path) {
1436 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1437 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
1438 int r;
1439
1440 assert(manager);
1441 assert(path);
1442
1443 r = sd_bus_get_property(
1444 manager->bus,
1445 "org.freedesktop.systemd1",
1446 path,
1447 "org.freedesktop.systemd1.Job",
1448 "State",
1449 &error,
1450 &reply,
1451 "s");
1452 if (r < 0) {
1453 if (sd_bus_error_has_names(&error, SD_BUS_ERROR_NO_REPLY,
1454 SD_BUS_ERROR_DISCONNECTED))
1455 return true;
1456
1457 if (sd_bus_error_has_name(&error, SD_BUS_ERROR_UNKNOWN_OBJECT))
1458 return false;
1459
1460 return r;
1461 }
1462
1463 /* We don't actually care about the state really. The fact
1464 * that we could read the job state is enough for us */
1465
1466 return true;
1467 }
1468
1469 int manager_get_machine_by_pid(Manager *m, pid_t pid, Machine **machine) {
1470 Machine *mm;
1471 int r;
1472
1473 assert(m);
1474 assert(pid >= 1);
1475 assert(machine);
1476
1477 mm = hashmap_get(m->machine_leaders, PID_TO_PTR(pid));
1478 if (!mm) {
1479 _cleanup_free_ char *unit = NULL;
1480
1481 r = cg_pid_get_unit(pid, &unit);
1482 if (r >= 0)
1483 mm = hashmap_get(m->machine_units, unit);
1484 }
1485 if (!mm)
1486 return 0;
1487
1488 *machine = mm;
1489 return 1;
1490 }
1491
1492 int manager_add_machine(Manager *m, const char *name, Machine **_machine) {
1493 Machine *machine;
1494 int r;
1495
1496 assert(m);
1497 assert(name);
1498
1499 machine = hashmap_get(m->machines, name);
1500 if (!machine) {
1501 r = machine_new(m, _MACHINE_CLASS_INVALID, name, &machine);
1502 if (r < 0)
1503 return r;
1504 }
1505
1506 if (_machine)
1507 *_machine = machine;
1508
1509 return 0;
1510 }