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