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