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