]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/machine/machined-dbus.c
8031dafe158bcc3980136cc842d532cf7ca4aef8
[thirdparty/systemd.git] / src / machine / machined-dbus.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2
3 #include <errno.h>
4 #include <string.h>
5 #include <unistd.h>
6
7 #include "sd-id128.h"
8
9 #include "alloc-util.h"
10 #include "btrfs-util.h"
11 #include "bus-common-errors.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_terminate_machine(sd_bus_message *message, void *userdata, sd_bus_error *error) {
452 return redirect_method_to_machine(message, userdata, error, bus_machine_method_terminate);
453 }
454
455 static int method_kill_machine(sd_bus_message *message, void *userdata, sd_bus_error *error) {
456 return redirect_method_to_machine(message, userdata, error, bus_machine_method_kill);
457 }
458
459 static int method_get_machine_addresses(sd_bus_message *message, void *userdata, sd_bus_error *error) {
460 return redirect_method_to_machine(message, userdata, error, bus_machine_method_get_addresses);
461 }
462
463 static int method_get_machine_os_release(sd_bus_message *message, void *userdata, sd_bus_error *error) {
464 return redirect_method_to_machine(message, userdata, error, bus_machine_method_get_os_release);
465 }
466
467 static int method_list_images(sd_bus_message *message, void *userdata, sd_bus_error *error) {
468 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
469 _cleanup_hashmap_free_ Hashmap *images = NULL;
470 Manager *m = userdata;
471 Image *image;
472 Iterator i;
473 int r;
474
475 assert(message);
476 assert(m);
477
478 images = hashmap_new(&image_hash_ops);
479 if (!images)
480 return -ENOMEM;
481
482 r = image_discover(IMAGE_MACHINE, 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, i) {
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, &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) == (off_t) -1)
617 return -errno;
618
619 f = fdopen(operation->extra_fd, "r");
620 if (!f)
621 return -errno;
622
623 operation->extra_fd = -1;
624
625 /* The resulting temporary file starts with a boolean value that indicates success or not. */
626 errno = 0;
627 n = fread(&success, 1, sizeof(success), f);
628 if (n != sizeof(success))
629 return ret < 0 ? ret : errno_or_else(EIO);
630
631 if (ret < 0) {
632 _cleanup_free_ char *name = NULL;
633
634 /* The clean-up operation failed. In this case the resulting temporary file should contain a boolean
635 * set to false followed by the name of the failed image. Let's try to read this and use it for the
636 * error message. If we can't read it, don't mind, and return the naked error. */
637
638 if (success) /* The resulting temporary file could not be updated, ignore it. */
639 return ret;
640
641 r = read_nul_string(f, LONG_LINE_MAX, &name);
642 if (r <= 0) /* Same here... */
643 return ret;
644
645 return sd_bus_error_set_errnof(error, ret, "Failed to remove image %s: %m", name);
646 }
647
648 assert(success);
649
650 r = sd_bus_message_new_method_return(operation->message, &reply);
651 if (r < 0)
652 return r;
653
654 r = sd_bus_message_open_container(reply, 'a', "(st)");
655 if (r < 0)
656 return r;
657
658 /* On success the resulting temporary file will contain a list of image names that were removed followed by
659 * their size on disk. Let's read that and turn it into a bus message. */
660 for (;;) {
661 _cleanup_free_ char *name = NULL;
662 uint64_t size;
663
664 r = read_nul_string(f, LONG_LINE_MAX, &name);
665 if (r < 0)
666 return r;
667 if (r == 0) /* reached the end */
668 break;
669
670 errno = 0;
671 n = fread(&size, 1, sizeof(size), f);
672 if (n != sizeof(size))
673 return errno_or_else(EIO);
674
675 r = sd_bus_message_append(reply, "(st)", name, size);
676 if (r < 0)
677 return r;
678 }
679
680 r = sd_bus_message_close_container(reply);
681 if (r < 0)
682 return r;
683
684 return sd_bus_send(NULL, reply, NULL);
685 }
686
687 static int method_clean_pool(sd_bus_message *message, void *userdata, sd_bus_error *error) {
688 enum {
689 REMOVE_ALL,
690 REMOVE_HIDDEN,
691 } mode;
692
693 _cleanup_close_pair_ int errno_pipe_fd[2] = { -1, -1 };
694 _cleanup_close_ int result_fd = -1;
695 Manager *m = userdata;
696 Operation *operation;
697 const char *mm;
698 pid_t child;
699 int r;
700
701 assert(message);
702
703 if (m->n_operations >= OPERATIONS_MAX)
704 return sd_bus_error_setf(error, SD_BUS_ERROR_LIMITS_EXCEEDED, "Too many ongoing operations.");
705
706 r = sd_bus_message_read(message, "s", &mm);
707 if (r < 0)
708 return r;
709
710 if (streq(mm, "all"))
711 mode = REMOVE_ALL;
712 else if (streq(mm, "hidden"))
713 mode = REMOVE_HIDDEN;
714 else
715 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Unknown mode '%s'.", mm);
716
717 r = bus_verify_polkit_async(
718 message,
719 CAP_SYS_ADMIN,
720 "org.freedesktop.machine1.manage-machines",
721 NULL,
722 false,
723 UID_INVALID,
724 &m->polkit_registry,
725 error);
726 if (r < 0)
727 return r;
728 if (r == 0)
729 return 1; /* Will call us back */
730
731 if (pipe2(errno_pipe_fd, O_CLOEXEC|O_NONBLOCK) < 0)
732 return sd_bus_error_set_errnof(error, errno, "Failed to create pipe: %m");
733
734 /* Create a temporary file we can dump information about deleted images into. We use a temporary file for this
735 * instead of a pipe or so, since this might grow quit large in theory and we don't want to process this
736 * continuously */
737 result_fd = open_tmpfile_unlinkable(NULL, O_RDWR|O_CLOEXEC);
738 if (result_fd < 0)
739 return -errno;
740
741 /* This might be a slow operation, run it asynchronously in a background process */
742 r = safe_fork("(sd-clean)", FORK_RESET_SIGNALS, &child);
743 if (r < 0)
744 return sd_bus_error_set_errnof(error, r, "Failed to fork(): %m");
745 if (r == 0) {
746 _cleanup_hashmap_free_ Hashmap *images = NULL;
747 bool success = true;
748 Image *image;
749 Iterator i;
750 ssize_t l;
751
752 errno_pipe_fd[0] = safe_close(errno_pipe_fd[0]);
753
754 images = hashmap_new(&image_hash_ops);
755 if (!images) {
756 r = -ENOMEM;
757 goto child_fail;
758 }
759
760 r = image_discover(IMAGE_MACHINE, images);
761 if (r < 0)
762 goto child_fail;
763
764 l = write(result_fd, &success, sizeof(success));
765 if (l < 0) {
766 r = -errno;
767 goto child_fail;
768 }
769
770 HASHMAP_FOREACH(image, images, i) {
771
772 /* We can't remove vendor images (i.e. those in /usr) */
773 if (IMAGE_IS_VENDOR(image))
774 continue;
775
776 if (IMAGE_IS_HOST(image))
777 continue;
778
779 if (mode == REMOVE_HIDDEN && !IMAGE_IS_HIDDEN(image))
780 continue;
781
782 r = image_remove(image);
783 if (r == -EBUSY) /* keep images that are currently being used. */
784 continue;
785 if (r < 0) {
786 /* If the operation failed, let's override everything we wrote, and instead write there at which image we failed. */
787 success = false;
788 (void) ftruncate(result_fd, 0);
789 (void) lseek(result_fd, 0, SEEK_SET);
790 (void) write(result_fd, &success, sizeof(success));
791 (void) write(result_fd, image->name, strlen(image->name)+1);
792 goto child_fail;
793 }
794
795 l = write(result_fd, image->name, strlen(image->name)+1);
796 if (l < 0) {
797 r = -errno;
798 goto child_fail;
799 }
800
801 l = write(result_fd, &image->usage_exclusive, sizeof(image->usage_exclusive));
802 if (l < 0) {
803 r = -errno;
804 goto child_fail;
805 }
806 }
807
808 result_fd = safe_close(result_fd);
809 _exit(EXIT_SUCCESS);
810
811 child_fail:
812 (void) write(errno_pipe_fd[1], &r, sizeof(r));
813 _exit(EXIT_FAILURE);
814 }
815
816 errno_pipe_fd[1] = safe_close(errno_pipe_fd[1]);
817
818 /* The clean-up might take a while, hence install a watch on the child and return */
819
820 r = operation_new(m, NULL, child, message, errno_pipe_fd[0], &operation);
821 if (r < 0) {
822 (void) sigkill_wait(child);
823 return r;
824 }
825
826 operation->extra_fd = result_fd;
827 operation->done = clean_pool_done;
828
829 result_fd = -1;
830 errno_pipe_fd[0] = -1;
831
832 return 1;
833 }
834
835 static int method_set_pool_limit(sd_bus_message *message, void *userdata, sd_bus_error *error) {
836 Manager *m = userdata;
837 uint64_t limit;
838 int r;
839
840 assert(message);
841
842 r = sd_bus_message_read(message, "t", &limit);
843 if (r < 0)
844 return r;
845 if (!FILE_SIZE_VALID_OR_INFINITY(limit))
846 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "New limit out of range");
847
848 r = bus_verify_polkit_async(
849 message,
850 CAP_SYS_ADMIN,
851 "org.freedesktop.machine1.manage-machines",
852 NULL,
853 false,
854 UID_INVALID,
855 &m->polkit_registry,
856 error);
857 if (r < 0)
858 return r;
859 if (r == 0)
860 return 1; /* Will call us back */
861
862 /* Set up the machine directory if necessary */
863 r = setup_machine_directory(error);
864 if (r < 0)
865 return r;
866
867 (void) btrfs_qgroup_set_limit("/var/lib/machines", 0, limit);
868
869 r = btrfs_subvol_set_subtree_quota_limit("/var/lib/machines", 0, limit);
870 if (r == -ENOTTY)
871 return sd_bus_error_setf(error, SD_BUS_ERROR_NOT_SUPPORTED, "Quota is only supported on btrfs.");
872 if (r < 0)
873 return sd_bus_error_set_errnof(error, r, "Failed to adjust quota limit: %m");
874
875 return sd_bus_reply_method_return(message, NULL);
876 }
877
878 static int method_set_image_limit(sd_bus_message *message, void *userdata, sd_bus_error *error) {
879 return redirect_method_to_image(message, userdata, error, bus_image_method_set_limit);
880 }
881
882 static int method_map_from_machine_user(sd_bus_message *message, void *userdata, sd_bus_error *error) {
883 _cleanup_fclose_ FILE *f = NULL;
884 Manager *m = userdata;
885 const char *name, *p;
886 Machine *machine;
887 uint32_t uid;
888 int r;
889
890 r = sd_bus_message_read(message, "su", &name, &uid);
891 if (r < 0)
892 return r;
893
894 if (!uid_is_valid(uid))
895 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid user ID " UID_FMT, uid);
896
897 machine = hashmap_get(m->machines, name);
898 if (!machine)
899 return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_MACHINE, "No machine '%s' known", name);
900
901 if (machine->class != MACHINE_CONTAINER)
902 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Not supported for non-container machines.");
903
904 p = procfs_file_alloca(machine->leader, "uid_map");
905 f = fopen(p, "re");
906 if (!f)
907 return -errno;
908
909 for (;;) {
910 uid_t uid_base, uid_shift, uid_range, converted;
911 int k;
912
913 errno = 0;
914 k = fscanf(f, UID_FMT " " UID_FMT " " UID_FMT, &uid_base, &uid_shift, &uid_range);
915 if (k < 0 && feof(f))
916 break;
917 if (k != 3) {
918 if (ferror(f))
919 return errno_or_else(EIO);
920
921 return -EIO;
922 }
923
924 if (uid < uid_base || uid >= uid_base + uid_range)
925 continue;
926
927 converted = uid - uid_base + uid_shift;
928 if (!uid_is_valid(converted))
929 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid user ID " UID_FMT, uid);
930
931 return sd_bus_reply_method_return(message, "u", (uint32_t) converted);
932 }
933
934 return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_USER_MAPPING, "Machine '%s' has no matching user mappings.", name);
935 }
936
937 static int method_map_to_machine_user(sd_bus_message *message, void *userdata, sd_bus_error *error) {
938 Manager *m = userdata;
939 Machine *machine;
940 uid_t uid;
941 Iterator i;
942 int r;
943
944 r = sd_bus_message_read(message, "u", &uid);
945 if (r < 0)
946 return r;
947 if (!uid_is_valid(uid))
948 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid user ID " UID_FMT, uid);
949 if (uid < 0x10000)
950 return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_USER_MAPPING, "User " UID_FMT " belongs to host UID range", uid);
951
952 HASHMAP_FOREACH(machine, m->machines, i) {
953 _cleanup_fclose_ FILE *f = NULL;
954 char p[STRLEN("/proc//uid_map") + DECIMAL_STR_MAX(pid_t) + 1];
955
956 if (machine->class != MACHINE_CONTAINER)
957 continue;
958
959 xsprintf(p, "/proc/" UID_FMT "/uid_map", machine->leader);
960 f = fopen(p, "re");
961 if (!f) {
962 log_warning_errno(errno, "Failed to open %s, ignoring,", p);
963 continue;
964 }
965
966 for (;;) {
967 _cleanup_free_ char *o = NULL;
968 uid_t uid_base, uid_shift, uid_range, converted;
969 int k;
970
971 errno = 0;
972 k = fscanf(f, UID_FMT " " UID_FMT " " UID_FMT, &uid_base, &uid_shift, &uid_range);
973 if (k < 0 && feof(f))
974 break;
975 if (k != 3) {
976 if (ferror(f))
977 return errno_or_else(EIO);
978
979 return -EIO;
980 }
981
982 /* The private user namespace is disabled, ignoring. */
983 if (uid_shift == 0)
984 continue;
985
986 if (uid < uid_shift || uid >= uid_shift + uid_range)
987 continue;
988
989 converted = (uid - uid_shift + uid_base);
990 if (!uid_is_valid(converted))
991 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid user ID " UID_FMT, uid);
992
993 o = machine_bus_path(machine);
994 if (!o)
995 return -ENOMEM;
996
997 return sd_bus_reply_method_return(message, "sou", machine->name, o, (uint32_t) converted);
998 }
999 }
1000
1001 return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_USER_MAPPING, "No matching user mapping for " UID_FMT ".", uid);
1002 }
1003
1004 static int method_map_from_machine_group(sd_bus_message *message, void *groupdata, sd_bus_error *error) {
1005 _cleanup_fclose_ FILE *f = NULL;
1006 Manager *m = groupdata;
1007 const char *name, *p;
1008 Machine *machine;
1009 uint32_t gid;
1010 int r;
1011
1012 r = sd_bus_message_read(message, "su", &name, &gid);
1013 if (r < 0)
1014 return r;
1015
1016 if (!gid_is_valid(gid))
1017 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid group ID " GID_FMT, gid);
1018
1019 machine = hashmap_get(m->machines, name);
1020 if (!machine)
1021 return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_MACHINE, "No machine '%s' known", name);
1022
1023 if (machine->class != MACHINE_CONTAINER)
1024 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Not supported for non-container machines.");
1025
1026 p = procfs_file_alloca(machine->leader, "gid_map");
1027 f = fopen(p, "re");
1028 if (!f)
1029 return -errno;
1030
1031 for (;;) {
1032 gid_t gid_base, gid_shift, gid_range, converted;
1033 int k;
1034
1035 errno = 0;
1036 k = fscanf(f, GID_FMT " " GID_FMT " " GID_FMT, &gid_base, &gid_shift, &gid_range);
1037 if (k < 0 && feof(f))
1038 break;
1039 if (k != 3) {
1040 if (ferror(f))
1041 return errno_or_else(EIO);
1042
1043 return -EIO;
1044 }
1045
1046 if (gid < gid_base || gid >= gid_base + gid_range)
1047 continue;
1048
1049 converted = gid - gid_base + gid_shift;
1050 if (!gid_is_valid(converted))
1051 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid group ID " GID_FMT, gid);
1052
1053 return sd_bus_reply_method_return(message, "u", (uint32_t) converted);
1054 }
1055
1056 return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_GROUP_MAPPING, "Machine '%s' has no matching group mappings.", name);
1057 }
1058
1059 static int method_map_to_machine_group(sd_bus_message *message, void *groupdata, sd_bus_error *error) {
1060 Manager *m = groupdata;
1061 Machine *machine;
1062 gid_t gid;
1063 Iterator i;
1064 int r;
1065
1066 r = sd_bus_message_read(message, "u", &gid);
1067 if (r < 0)
1068 return r;
1069 if (!gid_is_valid(gid))
1070 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid group ID " GID_FMT, gid);
1071 if (gid < 0x10000)
1072 return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_GROUP_MAPPING, "Group " GID_FMT " belongs to host GID range", gid);
1073
1074 HASHMAP_FOREACH(machine, m->machines, i) {
1075 _cleanup_fclose_ FILE *f = NULL;
1076 char p[STRLEN("/proc//gid_map") + DECIMAL_STR_MAX(pid_t) + 1];
1077
1078 if (machine->class != MACHINE_CONTAINER)
1079 continue;
1080
1081 xsprintf(p, "/proc/" GID_FMT "/gid_map", machine->leader);
1082 f = fopen(p, "re");
1083 if (!f) {
1084 log_warning_errno(errno, "Failed to open %s, ignoring,", p);
1085 continue;
1086 }
1087
1088 for (;;) {
1089 _cleanup_free_ char *o = NULL;
1090 gid_t gid_base, gid_shift, gid_range, converted;
1091 int k;
1092
1093 errno = 0;
1094 k = fscanf(f, GID_FMT " " GID_FMT " " GID_FMT, &gid_base, &gid_shift, &gid_range);
1095 if (k < 0 && feof(f))
1096 break;
1097 if (k != 3) {
1098 if (ferror(f))
1099 return errno_or_else(EIO);
1100
1101 return -EIO;
1102 }
1103
1104 /* The private user namespace is disabled, ignoring. */
1105 if (gid_shift == 0)
1106 continue;
1107
1108 if (gid < gid_shift || gid >= gid_shift + gid_range)
1109 continue;
1110
1111 converted = (gid - gid_shift + gid_base);
1112 if (!gid_is_valid(converted))
1113 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid group ID " GID_FMT, gid);
1114
1115 o = machine_bus_path(machine);
1116 if (!o)
1117 return -ENOMEM;
1118
1119 return sd_bus_reply_method_return(message, "sou", machine->name, o, (uint32_t) converted);
1120 }
1121 }
1122
1123 return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_GROUP_MAPPING, "No matching group mapping for " GID_FMT ".", gid);
1124 }
1125
1126 const sd_bus_vtable manager_vtable[] = {
1127 SD_BUS_VTABLE_START(0),
1128 SD_BUS_PROPERTY("PoolPath", "s", property_get_pool_path, 0, 0),
1129 SD_BUS_PROPERTY("PoolUsage", "t", property_get_pool_usage, 0, 0),
1130 SD_BUS_PROPERTY("PoolLimit", "t", property_get_pool_limit, 0, 0),
1131 SD_BUS_METHOD("GetMachine", "s", "o", method_get_machine, SD_BUS_VTABLE_UNPRIVILEGED),
1132 SD_BUS_METHOD("GetImage", "s", "o", method_get_image, SD_BUS_VTABLE_UNPRIVILEGED),
1133 SD_BUS_METHOD("GetMachineByPID", "u", "o", method_get_machine_by_pid, SD_BUS_VTABLE_UNPRIVILEGED),
1134 SD_BUS_METHOD("ListMachines", NULL, "a(ssso)", method_list_machines, SD_BUS_VTABLE_UNPRIVILEGED),
1135 SD_BUS_METHOD("ListImages", NULL, "a(ssbttto)", method_list_images, SD_BUS_VTABLE_UNPRIVILEGED),
1136 SD_BUS_METHOD("CreateMachine", "sayssusa(sv)", "o", method_create_machine, 0),
1137 SD_BUS_METHOD("CreateMachineWithNetwork", "sayssusaia(sv)", "o", method_create_machine_with_network, 0),
1138 SD_BUS_METHOD("RegisterMachine", "sayssus", "o", method_register_machine, 0),
1139 SD_BUS_METHOD("RegisterMachineWithNetwork", "sayssusai", "o", method_register_machine_with_network, 0),
1140 SD_BUS_METHOD("TerminateMachine", "s", NULL, method_terminate_machine, SD_BUS_VTABLE_UNPRIVILEGED),
1141 SD_BUS_METHOD("KillMachine", "ssi", NULL, method_kill_machine, SD_BUS_VTABLE_UNPRIVILEGED),
1142 SD_BUS_METHOD("GetMachineAddresses", "s", "a(iay)", method_get_machine_addresses, SD_BUS_VTABLE_UNPRIVILEGED),
1143 SD_BUS_METHOD("GetMachineOSRelease", "s", "a{ss}", method_get_machine_os_release, SD_BUS_VTABLE_UNPRIVILEGED),
1144 SD_BUS_METHOD("OpenMachinePTY", "s", "hs", method_open_machine_pty, 0),
1145 SD_BUS_METHOD("OpenMachineLogin", "s", "hs", method_open_machine_login, SD_BUS_VTABLE_UNPRIVILEGED),
1146 SD_BUS_METHOD("OpenMachineShell", "sssasas", "hs", method_open_machine_shell, SD_BUS_VTABLE_UNPRIVILEGED),
1147 SD_BUS_METHOD("BindMountMachine", "sssbb", NULL, method_bind_mount_machine, SD_BUS_VTABLE_UNPRIVILEGED),
1148 SD_BUS_METHOD("CopyFromMachine", "sss", NULL, method_copy_machine, SD_BUS_VTABLE_UNPRIVILEGED),
1149 SD_BUS_METHOD("CopyToMachine", "sss", NULL, method_copy_machine, SD_BUS_VTABLE_UNPRIVILEGED),
1150 SD_BUS_METHOD("OpenMachineRootDirectory", "s", "h", method_open_machine_root_directory, SD_BUS_VTABLE_UNPRIVILEGED),
1151 SD_BUS_METHOD("GetMachineUIDShift", "s", "u", method_get_machine_uid_shift, SD_BUS_VTABLE_UNPRIVILEGED),
1152 SD_BUS_METHOD("RemoveImage", "s", NULL, method_remove_image, SD_BUS_VTABLE_UNPRIVILEGED),
1153 SD_BUS_METHOD("RenameImage", "ss", NULL, method_rename_image, SD_BUS_VTABLE_UNPRIVILEGED),
1154 SD_BUS_METHOD("CloneImage", "ssb", NULL, method_clone_image, SD_BUS_VTABLE_UNPRIVILEGED),
1155 SD_BUS_METHOD("MarkImageReadOnly", "sb", NULL, method_mark_image_read_only, SD_BUS_VTABLE_UNPRIVILEGED),
1156 SD_BUS_METHOD("GetImageHostname", "s", "s", method_get_image_hostname, SD_BUS_VTABLE_UNPRIVILEGED),
1157 SD_BUS_METHOD("GetImageMachineID", "s", "ay", method_get_image_machine_id, SD_BUS_VTABLE_UNPRIVILEGED),
1158 SD_BUS_METHOD("GetImageMachineInfo", "s", "a{ss}", method_get_image_machine_info, SD_BUS_VTABLE_UNPRIVILEGED),
1159 SD_BUS_METHOD("GetImageOSRelease", "s", "a{ss}", method_get_image_os_release, SD_BUS_VTABLE_UNPRIVILEGED),
1160 SD_BUS_METHOD("SetPoolLimit", "t", NULL, method_set_pool_limit, SD_BUS_VTABLE_UNPRIVILEGED),
1161 SD_BUS_METHOD("SetImageLimit", "st", NULL, method_set_image_limit, SD_BUS_VTABLE_UNPRIVILEGED),
1162 SD_BUS_METHOD("CleanPool", "s", "a(st)", method_clean_pool, SD_BUS_VTABLE_UNPRIVILEGED),
1163 SD_BUS_METHOD("MapFromMachineUser", "su", "u", method_map_from_machine_user, SD_BUS_VTABLE_UNPRIVILEGED),
1164 SD_BUS_METHOD("MapToMachineUser", "u", "sou", method_map_to_machine_user, SD_BUS_VTABLE_UNPRIVILEGED),
1165 SD_BUS_METHOD("MapFromMachineGroup", "su", "u", method_map_from_machine_group, SD_BUS_VTABLE_UNPRIVILEGED),
1166 SD_BUS_METHOD("MapToMachineGroup", "u", "sou", method_map_to_machine_group, SD_BUS_VTABLE_UNPRIVILEGED),
1167 SD_BUS_SIGNAL("MachineNew", "so", 0),
1168 SD_BUS_SIGNAL("MachineRemoved", "so", 0),
1169 SD_BUS_VTABLE_END
1170 };
1171
1172 int match_job_removed(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1173 const char *path, *result, *unit;
1174 Manager *m = userdata;
1175 Machine *machine;
1176 uint32_t id;
1177 int r;
1178
1179 assert(message);
1180 assert(m);
1181
1182 r = sd_bus_message_read(message, "uoss", &id, &path, &unit, &result);
1183 if (r < 0) {
1184 bus_log_parse_error(r);
1185 return 0;
1186 }
1187
1188 machine = hashmap_get(m->machine_units, unit);
1189 if (!machine)
1190 return 0;
1191
1192 if (streq_ptr(path, machine->scope_job)) {
1193 machine->scope_job = mfree(machine->scope_job);
1194
1195 if (machine->started) {
1196 if (streq(result, "done"))
1197 machine_send_create_reply(machine, NULL);
1198 else {
1199 _cleanup_(sd_bus_error_free) sd_bus_error e = SD_BUS_ERROR_NULL;
1200
1201 sd_bus_error_setf(&e, BUS_ERROR_JOB_FAILED, "Start job for unit %s failed with '%s'", unit, result);
1202
1203 machine_send_create_reply(machine, &e);
1204 }
1205 }
1206
1207 machine_save(machine);
1208 }
1209
1210 machine_add_to_gc_queue(machine);
1211 return 0;
1212 }
1213
1214 int match_properties_changed(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1215 _cleanup_free_ char *unit = NULL;
1216 const char *path;
1217 Manager *m = userdata;
1218 Machine *machine;
1219 int r;
1220
1221 assert(message);
1222 assert(m);
1223
1224 path = sd_bus_message_get_path(message);
1225 if (!path)
1226 return 0;
1227
1228 r = unit_name_from_dbus_path(path, &unit);
1229 if (r == -EINVAL) /* not for a unit */
1230 return 0;
1231 if (r < 0) {
1232 log_oom();
1233 return 0;
1234 }
1235
1236 machine = hashmap_get(m->machine_units, unit);
1237 if (!machine)
1238 return 0;
1239
1240 machine_add_to_gc_queue(machine);
1241 return 0;
1242 }
1243
1244 int match_unit_removed(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1245 const char *path, *unit;
1246 Manager *m = userdata;
1247 Machine *machine;
1248 int r;
1249
1250 assert(message);
1251 assert(m);
1252
1253 r = sd_bus_message_read(message, "so", &unit, &path);
1254 if (r < 0) {
1255 bus_log_parse_error(r);
1256 return 0;
1257 }
1258
1259 machine = hashmap_get(m->machine_units, unit);
1260 if (!machine)
1261 return 0;
1262
1263 machine_add_to_gc_queue(machine);
1264 return 0;
1265 }
1266
1267 int match_reloading(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1268 Manager *m = userdata;
1269 Machine *machine;
1270 Iterator i;
1271 int b, r;
1272
1273 assert(message);
1274 assert(m);
1275
1276 r = sd_bus_message_read(message, "b", &b);
1277 if (r < 0) {
1278 bus_log_parse_error(r);
1279 return 0;
1280 }
1281 if (b)
1282 return 0;
1283
1284 /* systemd finished reloading, let's recheck all our machines */
1285 log_debug("System manager has been reloaded, rechecking machines...");
1286
1287 HASHMAP_FOREACH(machine, m->machines, i)
1288 machine_add_to_gc_queue(machine);
1289
1290 return 0;
1291 }
1292
1293 int manager_start_scope(
1294 Manager *manager,
1295 const char *scope,
1296 pid_t pid,
1297 const char *slice,
1298 const char *description,
1299 sd_bus_message *more_properties,
1300 sd_bus_error *error,
1301 char **job) {
1302
1303 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL, *reply = NULL;
1304 int r;
1305
1306 assert(manager);
1307 assert(scope);
1308 assert(pid > 1);
1309
1310 r = sd_bus_message_new_method_call(
1311 manager->bus,
1312 &m,
1313 "org.freedesktop.systemd1",
1314 "/org/freedesktop/systemd1",
1315 "org.freedesktop.systemd1.Manager",
1316 "StartTransientUnit");
1317 if (r < 0)
1318 return r;
1319
1320 r = sd_bus_message_append(m, "ss", strempty(scope), "fail");
1321 if (r < 0)
1322 return r;
1323
1324 r = sd_bus_message_open_container(m, 'a', "(sv)");
1325 if (r < 0)
1326 return r;
1327
1328 if (!isempty(slice)) {
1329 r = sd_bus_message_append(m, "(sv)", "Slice", "s", slice);
1330 if (r < 0)
1331 return r;
1332 }
1333
1334 if (!isempty(description)) {
1335 r = sd_bus_message_append(m, "(sv)", "Description", "s", description);
1336 if (r < 0)
1337 return r;
1338 }
1339
1340 r = sd_bus_message_append(m, "(sv)(sv)(sv)(sv)(sv)",
1341 "PIDs", "au", 1, pid,
1342 "Delegate", "b", 1,
1343 "CollectMode", "s", "inactive-or-failed",
1344 "AddRef", "b", 1,
1345 "TasksMax", "t", UINT64_C(16384));
1346 if (r < 0)
1347 return r;
1348
1349 if (more_properties) {
1350 r = sd_bus_message_copy(m, more_properties, true);
1351 if (r < 0)
1352 return r;
1353 }
1354
1355 r = sd_bus_message_close_container(m);
1356 if (r < 0)
1357 return r;
1358
1359 r = sd_bus_message_append(m, "a(sa(sv))", 0);
1360 if (r < 0)
1361 return r;
1362
1363 r = sd_bus_call(manager->bus, m, 0, error, &reply);
1364 if (r < 0)
1365 return r;
1366
1367 if (job) {
1368 const char *j;
1369 char *copy;
1370
1371 r = sd_bus_message_read(reply, "o", &j);
1372 if (r < 0)
1373 return r;
1374
1375 copy = strdup(j);
1376 if (!copy)
1377 return -ENOMEM;
1378
1379 *job = copy;
1380 }
1381
1382 return 1;
1383 }
1384
1385 int manager_unref_unit(
1386 Manager *m,
1387 const char *unit,
1388 sd_bus_error *error) {
1389
1390 assert(m);
1391 assert(unit);
1392
1393 return sd_bus_call_method(
1394 m->bus,
1395 "org.freedesktop.systemd1",
1396 "/org/freedesktop/systemd1",
1397 "org.freedesktop.systemd1.Manager",
1398 "UnrefUnit",
1399 error,
1400 NULL,
1401 "s",
1402 unit);
1403 }
1404
1405 int manager_stop_unit(Manager *manager, const char *unit, sd_bus_error *error, char **job) {
1406 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
1407 int r;
1408
1409 assert(manager);
1410 assert(unit);
1411
1412 r = sd_bus_call_method(
1413 manager->bus,
1414 "org.freedesktop.systemd1",
1415 "/org/freedesktop/systemd1",
1416 "org.freedesktop.systemd1.Manager",
1417 "StopUnit",
1418 error,
1419 &reply,
1420 "ss", unit, "fail");
1421 if (r < 0) {
1422 if (sd_bus_error_has_name(error, BUS_ERROR_NO_SUCH_UNIT) ||
1423 sd_bus_error_has_name(error, BUS_ERROR_LOAD_FAILED)) {
1424
1425 if (job)
1426 *job = NULL;
1427
1428 sd_bus_error_free(error);
1429 return 0;
1430 }
1431
1432 return r;
1433 }
1434
1435 if (job) {
1436 const char *j;
1437 char *copy;
1438
1439 r = sd_bus_message_read(reply, "o", &j);
1440 if (r < 0)
1441 return r;
1442
1443 copy = strdup(j);
1444 if (!copy)
1445 return -ENOMEM;
1446
1447 *job = copy;
1448 }
1449
1450 return 1;
1451 }
1452
1453 int manager_kill_unit(Manager *manager, const char *unit, int signo, sd_bus_error *error) {
1454 assert(manager);
1455 assert(unit);
1456
1457 return sd_bus_call_method(
1458 manager->bus,
1459 "org.freedesktop.systemd1",
1460 "/org/freedesktop/systemd1",
1461 "org.freedesktop.systemd1.Manager",
1462 "KillUnit",
1463 error,
1464 NULL,
1465 "ssi", unit, "all", signo);
1466 }
1467
1468 int manager_unit_is_active(Manager *manager, const char *unit) {
1469 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1470 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
1471 _cleanup_free_ char *path = NULL;
1472 const char *state;
1473 int r;
1474
1475 assert(manager);
1476 assert(unit);
1477
1478 path = unit_dbus_path_from_name(unit);
1479 if (!path)
1480 return -ENOMEM;
1481
1482 r = sd_bus_get_property(
1483 manager->bus,
1484 "org.freedesktop.systemd1",
1485 path,
1486 "org.freedesktop.systemd1.Unit",
1487 "ActiveState",
1488 &error,
1489 &reply,
1490 "s");
1491 if (r < 0) {
1492 if (sd_bus_error_has_name(&error, SD_BUS_ERROR_NO_REPLY) ||
1493 sd_bus_error_has_name(&error, SD_BUS_ERROR_DISCONNECTED))
1494 return true;
1495
1496 if (sd_bus_error_has_name(&error, BUS_ERROR_NO_SUCH_UNIT) ||
1497 sd_bus_error_has_name(&error, BUS_ERROR_LOAD_FAILED))
1498 return false;
1499
1500 return r;
1501 }
1502
1503 r = sd_bus_message_read(reply, "s", &state);
1504 if (r < 0)
1505 return -EINVAL;
1506
1507 return !STR_IN_SET(state, "inactive", "failed");
1508 }
1509
1510 int manager_job_is_active(Manager *manager, const char *path) {
1511 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1512 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
1513 int r;
1514
1515 assert(manager);
1516 assert(path);
1517
1518 r = sd_bus_get_property(
1519 manager->bus,
1520 "org.freedesktop.systemd1",
1521 path,
1522 "org.freedesktop.systemd1.Job",
1523 "State",
1524 &error,
1525 &reply,
1526 "s");
1527 if (r < 0) {
1528 if (sd_bus_error_has_name(&error, SD_BUS_ERROR_NO_REPLY) ||
1529 sd_bus_error_has_name(&error, SD_BUS_ERROR_DISCONNECTED))
1530 return true;
1531
1532 if (sd_bus_error_has_name(&error, SD_BUS_ERROR_UNKNOWN_OBJECT))
1533 return false;
1534
1535 return r;
1536 }
1537
1538 /* We don't actually care about the state really. The fact
1539 * that we could read the job state is enough for us */
1540
1541 return true;
1542 }
1543
1544 int manager_get_machine_by_pid(Manager *m, pid_t pid, Machine **machine) {
1545 Machine *mm;
1546 int r;
1547
1548 assert(m);
1549 assert(pid >= 1);
1550 assert(machine);
1551
1552 mm = hashmap_get(m->machine_leaders, PID_TO_PTR(pid));
1553 if (!mm) {
1554 _cleanup_free_ char *unit = NULL;
1555
1556 r = cg_pid_get_unit(pid, &unit);
1557 if (r >= 0)
1558 mm = hashmap_get(m->machine_units, unit);
1559 }
1560 if (!mm)
1561 return 0;
1562
1563 *machine = mm;
1564 return 1;
1565 }
1566
1567 int manager_add_machine(Manager *m, const char *name, Machine **_machine) {
1568 Machine *machine;
1569
1570 assert(m);
1571 assert(name);
1572
1573 machine = hashmap_get(m->machines, name);
1574 if (!machine) {
1575 machine = machine_new(m, _MACHINE_CLASS_INVALID, name);
1576 if (!machine)
1577 return -ENOMEM;
1578 }
1579
1580 if (_machine)
1581 *_machine = machine;
1582
1583 return 0;
1584 }