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