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