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