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