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