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