]>
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" |
f97b34a6 | 33 | #include "format-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) { | |
048c386e ZJS |
447 | r = sd_bus_error_set_errnof(error, r, |
448 | "Failed to determine unit of process "PID_FMT" : %m", | |
449 | m->leader); | |
89f7c846 LP |
450 | goto fail; |
451 | } | |
452 | ||
453 | r = machine_start(m, NULL, error); | |
454 | if (r < 0) | |
455 | goto fail; | |
456 | ||
457 | p = machine_bus_path(m); | |
458 | if (!p) { | |
459 | r = -ENOMEM; | |
460 | goto fail; | |
461 | } | |
462 | ||
463 | return sd_bus_reply_method_return(message, "o", p); | |
464 | ||
465 | fail: | |
466 | machine_add_to_gc_queue(m); | |
1ee306e1 LP |
467 | return r; |
468 | } | |
469 | ||
19070062 LP |
470 | static int method_register_machine_with_network(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
471 | return method_register_machine_internal(message, true, userdata, error); | |
9b5ed6fe LP |
472 | } |
473 | ||
19070062 LP |
474 | static int method_register_machine(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
475 | return method_register_machine_internal(message, false, userdata, error); | |
9b5ed6fe LP |
476 | } |
477 | ||
19070062 | 478 | static int method_terminate_machine(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
1ee306e1 | 479 | Manager *m = userdata; |
c3350683 LP |
480 | Machine *machine; |
481 | const char *name; | |
1ee306e1 LP |
482 | int r; |
483 | ||
1ee306e1 LP |
484 | assert(message); |
485 | assert(m); | |
486 | ||
c3350683 LP |
487 | r = sd_bus_message_read(message, "s", &name); |
488 | if (r < 0) | |
ebcf1f97 | 489 | return sd_bus_error_set_errno(error, r); |
1ee306e1 | 490 | |
c3350683 LP |
491 | machine = hashmap_get(m->machines, name); |
492 | if (!machine) | |
ebcf1f97 | 493 | return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_MACHINE, "No machine '%s' known", name); |
1ee306e1 | 494 | |
19070062 | 495 | return bus_machine_method_terminate(message, machine, error); |
c3350683 | 496 | } |
1ee306e1 | 497 | |
19070062 | 498 | static int method_kill_machine(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
c3350683 LP |
499 | Manager *m = userdata; |
500 | Machine *machine; | |
501 | const char *name; | |
c3350683 | 502 | int r; |
1ee306e1 | 503 | |
c3350683 LP |
504 | assert(message); |
505 | assert(m); | |
1ee306e1 | 506 | |
878cd7e9 | 507 | r = sd_bus_message_read(message, "s", &name); |
c3350683 | 508 | if (r < 0) |
ebcf1f97 | 509 | return sd_bus_error_set_errno(error, r); |
1ee306e1 | 510 | |
c3350683 LP |
511 | machine = hashmap_get(m->machines, name); |
512 | if (!machine) | |
ebcf1f97 | 513 | return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_MACHINE, "No machine '%s' known", name); |
1ee306e1 | 514 | |
19070062 | 515 | return bus_machine_method_kill(message, machine, error); |
878cd7e9 LP |
516 | } |
517 | ||
19070062 | 518 | static int method_get_machine_addresses(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
878cd7e9 LP |
519 | Manager *m = userdata; |
520 | Machine *machine; | |
521 | const char *name; | |
522 | int r; | |
523 | ||
878cd7e9 LP |
524 | assert(message); |
525 | assert(m); | |
526 | ||
527 | r = sd_bus_message_read(message, "s", &name); | |
c3350683 | 528 | if (r < 0) |
ebcf1f97 | 529 | return sd_bus_error_set_errno(error, r); |
1ee306e1 | 530 | |
878cd7e9 LP |
531 | machine = hashmap_get(m->machines, name); |
532 | if (!machine) | |
533 | return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_MACHINE, "No machine '%s' known", name); | |
534 | ||
19070062 | 535 | return bus_machine_method_get_addresses(message, machine, error); |
c3350683 | 536 | } |
1ee306e1 | 537 | |
19070062 | 538 | static int method_get_machine_os_release(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
717603e3 LP |
539 | Manager *m = userdata; |
540 | Machine *machine; | |
541 | const char *name; | |
542 | int r; | |
543 | ||
717603e3 LP |
544 | assert(message); |
545 | assert(m); | |
546 | ||
547 | r = sd_bus_message_read(message, "s", &name); | |
548 | if (r < 0) | |
549 | return sd_bus_error_set_errno(error, r); | |
550 | ||
551 | machine = hashmap_get(m->machines, name); | |
552 | if (!machine) | |
553 | return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_MACHINE, "No machine '%s' known", name); | |
554 | ||
19070062 | 555 | return bus_machine_method_get_os_release(message, machine, error); |
717603e3 LP |
556 | } |
557 | ||
19070062 | 558 | static int method_list_images(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
4afd3348 | 559 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; |
cd61c3bf LP |
560 | _cleanup_(image_hashmap_freep) Hashmap *images = NULL; |
561 | Manager *m = userdata; | |
562 | Image *image; | |
563 | Iterator i; | |
564 | int r; | |
565 | ||
cd61c3bf LP |
566 | assert(message); |
567 | assert(m); | |
568 | ||
569 | images = hashmap_new(&string_hash_ops); | |
570 | if (!images) | |
571 | return -ENOMEM; | |
572 | ||
573 | r = image_discover(images); | |
574 | if (r < 0) | |
575 | return r; | |
576 | ||
577 | r = sd_bus_message_new_method_return(message, &reply); | |
578 | if (r < 0) | |
579 | return r; | |
580 | ||
b6b18498 | 581 | r = sd_bus_message_open_container(reply, 'a', "(ssbttto)"); |
cd61c3bf LP |
582 | if (r < 0) |
583 | return r; | |
584 | ||
585 | HASHMAP_FOREACH(image, images, i) { | |
586 | _cleanup_free_ char *p = NULL; | |
587 | ||
588 | p = image_bus_path(image->name); | |
589 | if (!p) | |
590 | return -ENOMEM; | |
591 | ||
b6b18498 | 592 | r = sd_bus_message_append(reply, "(ssbttto)", |
cd61c3bf LP |
593 | image->name, |
594 | image_type_to_string(image->type), | |
595 | image->read_only, | |
10f9c755 LP |
596 | image->crtime, |
597 | image->mtime, | |
c19de711 | 598 | image->usage, |
cd61c3bf LP |
599 | p); |
600 | if (r < 0) | |
601 | return r; | |
602 | } | |
603 | ||
604 | r = sd_bus_message_close_container(reply); | |
605 | if (r < 0) | |
606 | return r; | |
607 | ||
9030ca46 | 608 | return sd_bus_send(NULL, reply, NULL); |
cd61c3bf LP |
609 | } |
610 | ||
19070062 | 611 | static int method_open_machine_pty(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
40205d70 LP |
612 | Manager *m = userdata; |
613 | Machine *machine; | |
614 | const char *name; | |
615 | int r; | |
616 | ||
40205d70 LP |
617 | assert(message); |
618 | assert(m); | |
619 | ||
620 | r = sd_bus_message_read(message, "s", &name); | |
621 | if (r < 0) | |
622 | return sd_bus_error_set_errno(error, r); | |
623 | ||
624 | machine = hashmap_get(m->machines, name); | |
625 | if (!machine) | |
626 | return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_MACHINE, "No machine '%s' known", name); | |
627 | ||
19070062 | 628 | return bus_machine_method_open_pty(message, machine, error); |
40205d70 LP |
629 | } |
630 | ||
19070062 | 631 | static int method_open_machine_login(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
5f8cc96a LP |
632 | Manager *m = userdata; |
633 | Machine *machine; | |
634 | const char *name; | |
635 | int r; | |
636 | ||
5f8cc96a LP |
637 | assert(message); |
638 | assert(m); | |
639 | ||
640 | r = sd_bus_message_read(message, "s", &name); | |
641 | if (r < 0) | |
08682124 | 642 | return r; |
5f8cc96a LP |
643 | |
644 | machine = hashmap_get(m->machines, name); | |
645 | if (!machine) | |
646 | return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_MACHINE, "No machine '%s' known", name); | |
647 | ||
19070062 | 648 | return bus_machine_method_open_login(message, machine, error); |
5f8cc96a LP |
649 | } |
650 | ||
49af9e13 LP |
651 | static int method_open_machine_shell(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
652 | Manager *m = userdata; | |
653 | Machine *machine; | |
654 | const char *name; | |
655 | ||
656 | int r; | |
657 | ||
658 | assert(message); | |
659 | assert(m); | |
660 | ||
661 | r = sd_bus_message_read(message, "s", &name); | |
662 | if (r < 0) | |
663 | return r; | |
664 | ||
665 | machine = hashmap_get(m->machines, name); | |
666 | if (!machine) | |
667 | return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_MACHINE, "No machine '%s' known", name); | |
668 | ||
669 | return bus_machine_method_open_shell(message, machine, error); | |
670 | } | |
671 | ||
19070062 | 672 | static int method_bind_mount_machine(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
90adaa25 LP |
673 | Manager *m = userdata; |
674 | Machine *machine; | |
675 | const char *name; | |
676 | int r; | |
677 | ||
90adaa25 LP |
678 | assert(message); |
679 | assert(m); | |
680 | ||
681 | r = sd_bus_message_read(message, "s", &name); | |
682 | if (r < 0) | |
683 | return r; | |
684 | ||
685 | machine = hashmap_get(m->machines, name); | |
686 | if (!machine) | |
687 | return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_MACHINE, "No machine '%s' known", name); | |
688 | ||
19070062 | 689 | return bus_machine_method_bind_mount(message, machine, error); |
90adaa25 LP |
690 | } |
691 | ||
19070062 | 692 | static int method_copy_machine(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
0370612e LP |
693 | Manager *m = userdata; |
694 | Machine *machine; | |
695 | const char *name; | |
696 | int r; | |
697 | ||
0370612e LP |
698 | assert(message); |
699 | assert(m); | |
700 | ||
701 | r = sd_bus_message_read(message, "s", &name); | |
702 | if (r < 0) | |
703 | return r; | |
704 | ||
705 | machine = hashmap_get(m->machines, name); | |
706 | if (!machine) | |
707 | return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_MACHINE, "No machine '%s' known", name); | |
708 | ||
19070062 | 709 | return bus_machine_method_copy(message, machine, error); |
0370612e LP |
710 | } |
711 | ||
ae203207 LP |
712 | static int method_open_machine_root_directory(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
713 | Manager *m = userdata; | |
714 | Machine *machine; | |
715 | const char *name; | |
716 | int r; | |
717 | ||
718 | assert(message); | |
719 | assert(m); | |
720 | ||
721 | r = sd_bus_message_read(message, "s", &name); | |
722 | if (r < 0) | |
723 | return r; | |
724 | ||
725 | machine = hashmap_get(m->machines, name); | |
726 | if (!machine) | |
727 | return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_MACHINE, "No machine '%s' known", name); | |
728 | ||
729 | return bus_machine_method_open_root_directory(message, machine, error); | |
730 | } | |
731 | ||
19070062 | 732 | static int method_remove_image(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
08682124 LP |
733 | _cleanup_(image_unrefp) Image* i = NULL; |
734 | const char *name; | |
735 | int r; | |
736 | ||
08682124 LP |
737 | assert(message); |
738 | ||
739 | r = sd_bus_message_read(message, "s", &name); | |
740 | if (r < 0) | |
741 | return r; | |
742 | ||
743 | if (!image_name_is_valid(name)) | |
744 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Image name '%s' is invalid.", name); | |
745 | ||
746 | r = image_find(name, &i); | |
747 | if (r < 0) | |
748 | return r; | |
749 | if (r == 0) | |
750 | return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_IMAGE, "No image '%s' known", name); | |
751 | ||
70244d1d | 752 | i->userdata = userdata; |
19070062 | 753 | return bus_image_method_remove(message, i, error); |
08682124 LP |
754 | } |
755 | ||
19070062 | 756 | static int method_rename_image(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
ebd93cb6 | 757 | _cleanup_(image_unrefp) Image* i = NULL; |
1ddb263d | 758 | const char *old_name; |
ebd93cb6 LP |
759 | int r; |
760 | ||
ebd93cb6 LP |
761 | assert(message); |
762 | ||
1ddb263d | 763 | r = sd_bus_message_read(message, "s", &old_name); |
ebd93cb6 LP |
764 | if (r < 0) |
765 | return r; | |
766 | ||
767 | if (!image_name_is_valid(old_name)) | |
768 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Image name '%s' is invalid.", old_name); | |
ebd93cb6 LP |
769 | |
770 | r = image_find(old_name, &i); | |
771 | if (r < 0) | |
772 | return r; | |
773 | if (r == 0) | |
774 | return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_IMAGE, "No image '%s' known", old_name); | |
775 | ||
70244d1d | 776 | i->userdata = userdata; |
19070062 | 777 | return bus_image_method_rename(message, i, error); |
ebd93cb6 LP |
778 | } |
779 | ||
19070062 | 780 | static int method_clone_image(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
ebd93cb6 | 781 | _cleanup_(image_unrefp) Image *i = NULL; |
1ddb263d LP |
782 | const char *old_name; |
783 | int r; | |
ebd93cb6 | 784 | |
19070062 LP |
785 | assert(message); |
786 | ||
1ddb263d | 787 | r = sd_bus_message_read(message, "s", &old_name); |
ebd93cb6 LP |
788 | if (r < 0) |
789 | return r; | |
790 | ||
791 | if (!image_name_is_valid(old_name)) | |
792 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Image name '%s' is invalid.", old_name); | |
ebd93cb6 LP |
793 | |
794 | r = image_find(old_name, &i); | |
795 | if (r < 0) | |
796 | return r; | |
797 | if (r == 0) | |
798 | return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_IMAGE, "No image '%s' known", old_name); | |
799 | ||
70244d1d | 800 | i->userdata = userdata; |
19070062 | 801 | return bus_image_method_clone(message, i, error); |
ebd93cb6 LP |
802 | } |
803 | ||
19070062 | 804 | static int method_mark_image_read_only(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
ebd93cb6 LP |
805 | _cleanup_(image_unrefp) Image *i = NULL; |
806 | const char *name; | |
1ddb263d | 807 | int r; |
ebd93cb6 | 808 | |
19070062 LP |
809 | assert(message); |
810 | ||
1ddb263d | 811 | r = sd_bus_message_read(message, "s", &name); |
ebd93cb6 LP |
812 | if (r < 0) |
813 | return r; | |
814 | ||
815 | if (!image_name_is_valid(name)) | |
816 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Image name '%s' is invalid.", name); | |
817 | ||
818 | r = image_find(name, &i); | |
819 | if (r < 0) | |
820 | return r; | |
821 | if (r == 0) | |
822 | return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_IMAGE, "No image '%s' known", name); | |
823 | ||
70244d1d | 824 | i->userdata = userdata; |
19070062 | 825 | return bus_image_method_mark_read_only(message, i, error); |
ebd93cb6 LP |
826 | } |
827 | ||
9153b02b LP |
828 | static int method_get_image_os_release(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
829 | _cleanup_(image_unrefp) Image *i = NULL; | |
830 | const char *name; | |
831 | int r; | |
832 | ||
833 | assert(message); | |
834 | ||
835 | r = sd_bus_message_read(message, "s", &name); | |
836 | if (r < 0) | |
837 | return r; | |
838 | ||
839 | if (!image_name_is_valid(name)) | |
840 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Image name '%s' is invalid.", name); | |
841 | ||
842 | r = image_find(name, &i); | |
843 | if (r < 0) | |
844 | return r; | |
845 | if (r == 0) | |
846 | return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_IMAGE, "No image '%s' known", name); | |
847 | ||
848 | i->userdata = userdata; | |
849 | return bus_image_method_get_os_release(message, i, error); | |
850 | } | |
851 | ||
03c2b288 LP |
852 | static int clean_pool_done(Operation *operation, int ret, sd_bus_error *error) { |
853 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; | |
854 | _cleanup_fclose_ FILE *f = NULL; | |
855 | bool success; | |
856 | size_t n; | |
857 | int r; | |
858 | ||
859 | assert(operation); | |
860 | assert(operation->extra_fd >= 0); | |
861 | ||
862 | if (lseek(operation->extra_fd, 0, SEEK_SET) == (off_t) -1) | |
863 | return -errno; | |
864 | ||
865 | f = fdopen(operation->extra_fd, "re"); | |
866 | if (!f) | |
867 | return -errno; | |
868 | ||
869 | operation->extra_fd = -1; | |
870 | ||
871 | /* The resulting temporary file starts with a boolean value that indicates success or not. */ | |
872 | errno = 0; | |
873 | n = fread(&success, 1, sizeof(success), f); | |
874 | if (n != sizeof(success)) | |
875 | return ret < 0 ? ret : (errno != 0 ? -errno : -EIO); | |
876 | ||
877 | if (ret < 0) { | |
878 | _cleanup_free_ char *name = NULL; | |
879 | ||
880 | /* The clean-up operation failed. In this case the resulting temporary file should contain a boolean | |
881 | * set to false followed by the name of the failed image. Let's try to read this and use it for the | |
882 | * error message. If we can't read it, don't mind, and return the naked error. */ | |
883 | ||
884 | if (success) /* The resulting temporary file could not be updated, ignore it. */ | |
885 | return ret; | |
886 | ||
887 | r = read_nul_string(f, &name); | |
888 | if (r < 0 || isempty(name)) /* Same here... */ | |
889 | return ret; | |
890 | ||
891 | return sd_bus_error_set_errnof(error, ret, "Failed to remove image %s: %m", name); | |
892 | } | |
893 | ||
894 | assert(success); | |
895 | ||
896 | r = sd_bus_message_new_method_return(operation->message, &reply); | |
897 | if (r < 0) | |
898 | return r; | |
899 | ||
900 | r = sd_bus_message_open_container(reply, 'a', "(st)"); | |
901 | if (r < 0) | |
902 | return r; | |
903 | ||
904 | /* On success the resulting temporary file will contain a list of image names that were removed followed by | |
905 | * their size on disk. Let's read that and turn it into a bus message. */ | |
906 | for (;;) { | |
907 | _cleanup_free_ char *name = NULL; | |
908 | uint64_t size; | |
909 | ||
910 | r = read_nul_string(f, &name); | |
911 | if (r < 0) | |
912 | return r; | |
913 | if (isempty(name)) /* reached the end */ | |
914 | break; | |
915 | ||
916 | errno = 0; | |
917 | n = fread(&size, 1, sizeof(size), f); | |
918 | if (n != sizeof(size)) | |
919 | return errno != 0 ? -errno : -EIO; | |
920 | ||
921 | r = sd_bus_message_append(reply, "(st)", name, size); | |
922 | if (r < 0) | |
923 | return r; | |
924 | } | |
925 | ||
926 | r = sd_bus_message_close_container(reply); | |
927 | if (r < 0) | |
928 | return r; | |
929 | ||
930 | return sd_bus_send(NULL, reply, NULL); | |
931 | } | |
932 | ||
d94c2b06 LP |
933 | static int method_clean_pool(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
934 | enum { | |
935 | REMOVE_ALL, | |
936 | REMOVE_HIDDEN, | |
937 | } mode; | |
938 | ||
03c2b288 LP |
939 | _cleanup_close_pair_ int errno_pipe_fd[2] = { -1, -1 }; |
940 | _cleanup_close_ int result_fd = -1; | |
d94c2b06 | 941 | Manager *m = userdata; |
03c2b288 | 942 | Operation *operation; |
d94c2b06 | 943 | const char *mm; |
03c2b288 | 944 | pid_t child; |
d94c2b06 LP |
945 | int r; |
946 | ||
947 | assert(message); | |
948 | ||
03c2b288 LP |
949 | if (m->n_operations >= OPERATIONS_MAX) |
950 | return sd_bus_error_setf(error, SD_BUS_ERROR_LIMITS_EXCEEDED, "Too many ongoing operations."); | |
951 | ||
d94c2b06 LP |
952 | r = sd_bus_message_read(message, "s", &mm); |
953 | if (r < 0) | |
954 | return r; | |
955 | ||
956 | if (streq(mm, "all")) | |
957 | mode = REMOVE_ALL; | |
958 | else if (streq(mm, "hidden")) | |
959 | mode = REMOVE_HIDDEN; | |
960 | else | |
961 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Unknown mode '%s'.", mm); | |
962 | ||
963 | r = bus_verify_polkit_async( | |
964 | message, | |
965 | CAP_SYS_ADMIN, | |
966 | "org.freedesktop.machine1.manage-machines", | |
967 | NULL, | |
968 | false, | |
969 | UID_INVALID, | |
970 | &m->polkit_registry, | |
971 | error); | |
972 | if (r < 0) | |
973 | return r; | |
974 | if (r == 0) | |
975 | return 1; /* Will call us back */ | |
976 | ||
03c2b288 LP |
977 | if (pipe2(errno_pipe_fd, O_CLOEXEC|O_NONBLOCK) < 0) |
978 | return sd_bus_error_set_errnof(error, errno, "Failed to create pipe: %m"); | |
d94c2b06 | 979 | |
03c2b288 LP |
980 | /* Create a temporary file we can dump information about deleted images into. We use a temporary file for this |
981 | * instead of a pipe or so, since this might grow quit large in theory and we don't want to process this | |
595bfe7d | 982 | * continuously */ |
992e8f22 | 983 | result_fd = open_tmpfile_unlinkable(NULL, O_RDWR|O_CLOEXEC); |
03c2b288 LP |
984 | if (result_fd < 0) |
985 | return -errno; | |
d94c2b06 | 986 | |
03c2b288 LP |
987 | /* This might be a slow operation, run it asynchronously in a background process */ |
988 | child = fork(); | |
989 | if (child < 0) | |
990 | return sd_bus_error_set_errnof(error, errno, "Failed to fork(): %m"); | |
d94c2b06 | 991 | |
03c2b288 LP |
992 | if (child == 0) { |
993 | _cleanup_(image_hashmap_freep) Hashmap *images = NULL; | |
994 | bool success = true; | |
995 | Image *image; | |
996 | Iterator i; | |
997 | ssize_t l; | |
d94c2b06 | 998 | |
03c2b288 | 999 | errno_pipe_fd[0] = safe_close(errno_pipe_fd[0]); |
d94c2b06 | 1000 | |
03c2b288 LP |
1001 | images = hashmap_new(&string_hash_ops); |
1002 | if (!images) { | |
1003 | r = -ENOMEM; | |
1004 | goto child_fail; | |
1005 | } | |
d94c2b06 | 1006 | |
03c2b288 LP |
1007 | r = image_discover(images); |
1008 | if (r < 0) | |
1009 | goto child_fail; | |
d94c2b06 | 1010 | |
03c2b288 LP |
1011 | l = write(result_fd, &success, sizeof(success)); |
1012 | if (l < 0) { | |
1013 | r = -errno; | |
1014 | goto child_fail; | |
1015 | } | |
d94c2b06 | 1016 | |
03c2b288 LP |
1017 | HASHMAP_FOREACH(image, images, i) { |
1018 | ||
1019 | /* We can't remove vendor images (i.e. those in /usr) */ | |
1020 | if (IMAGE_IS_VENDOR(image)) | |
1021 | continue; | |
1022 | ||
1023 | if (IMAGE_IS_HOST(image)) | |
1024 | continue; | |
1025 | ||
1026 | if (mode == REMOVE_HIDDEN && !IMAGE_IS_HIDDEN(image)) | |
1027 | continue; | |
1028 | ||
1029 | r = image_remove(image); | |
1030 | if (r == -EBUSY) /* keep images that are currently being used. */ | |
1031 | continue; | |
1032 | if (r < 0) { | |
1033 | /* If the operation failed, let's override everything we wrote, and instead write there at which image we failed. */ | |
1034 | success = false; | |
1035 | (void) ftruncate(result_fd, 0); | |
1036 | (void) lseek(result_fd, 0, SEEK_SET); | |
1037 | (void) write(result_fd, &success, sizeof(success)); | |
1038 | (void) write(result_fd, image->name, strlen(image->name)+1); | |
1039 | goto child_fail; | |
1040 | } | |
1041 | ||
1042 | l = write(result_fd, image->name, strlen(image->name)+1); | |
1043 | if (l < 0) { | |
1044 | r = -errno; | |
1045 | goto child_fail; | |
1046 | } | |
1047 | ||
1048 | l = write(result_fd, &image->usage_exclusive, sizeof(image->usage_exclusive)); | |
1049 | if (l < 0) { | |
1050 | r = -errno; | |
1051 | goto child_fail; | |
1052 | } | |
1053 | } | |
d94c2b06 | 1054 | |
03c2b288 LP |
1055 | result_fd = safe_close(result_fd); |
1056 | _exit(EXIT_SUCCESS); | |
1057 | ||
1058 | child_fail: | |
1059 | (void) write(errno_pipe_fd[1], &r, sizeof(r)); | |
1060 | _exit(EXIT_FAILURE); | |
d94c2b06 LP |
1061 | } |
1062 | ||
03c2b288 LP |
1063 | errno_pipe_fd[1] = safe_close(errno_pipe_fd[1]); |
1064 | ||
1065 | /* The clean-up might take a while, hence install a watch on the child and return */ | |
1066 | ||
1067 | r = operation_new(m, NULL, child, message, errno_pipe_fd[0], &operation); | |
1068 | if (r < 0) { | |
1069 | (void) sigkill_wait(child); | |
d94c2b06 | 1070 | return r; |
03c2b288 | 1071 | } |
d94c2b06 | 1072 | |
03c2b288 LP |
1073 | operation->extra_fd = result_fd; |
1074 | operation->done = clean_pool_done; | |
1075 | ||
1076 | result_fd = -1; | |
1077 | errno_pipe_fd[0] = -1; | |
1078 | ||
1079 | return 1; | |
d94c2b06 LP |
1080 | } |
1081 | ||
19070062 | 1082 | static int method_set_pool_limit(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
d6ce17c7 LP |
1083 | Manager *m = userdata; |
1084 | uint64_t limit; | |
1085 | int r; | |
1086 | ||
19070062 LP |
1087 | assert(message); |
1088 | ||
d6ce17c7 LP |
1089 | r = sd_bus_message_read(message, "t", &limit); |
1090 | if (r < 0) | |
1091 | return r; | |
a90fb858 LP |
1092 | if (!FILE_SIZE_VALID_OR_INFINITY(limit)) |
1093 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "New limit out of range"); | |
d6ce17c7 LP |
1094 | |
1095 | r = bus_verify_polkit_async( | |
1096 | message, | |
1097 | CAP_SYS_ADMIN, | |
1098 | "org.freedesktop.machine1.manage-machines", | |
403ed0e5 | 1099 | NULL, |
d6ce17c7 LP |
1100 | false, |
1101 | UID_INVALID, | |
1102 | &m->polkit_registry, | |
1103 | error); | |
1104 | if (r < 0) | |
1105 | return r; | |
1106 | if (r == 0) | |
1107 | return 1; /* Will call us back */ | |
1108 | ||
4cee5eed LP |
1109 | /* Set up the machine directory if necessary */ |
1110 | r = setup_machine_directory(limit, error); | |
1111 | if (r < 0) | |
1112 | return r; | |
1113 | ||
05e8f270 LP |
1114 | /* Resize the backing loopback device, if there is one, except if we asked to drop any limit */ |
1115 | if (limit != (uint64_t) -1) { | |
1116 | r = btrfs_resize_loopback("/var/lib/machines", limit, false); | |
1117 | if (r == -ENOTTY) | |
1118 | return sd_bus_error_setf(error, SD_BUS_ERROR_NOT_SUPPORTED, "Quota is only supported on btrfs."); | |
1119 | if (r < 0 && r != -ENODEV) /* ignore ENODEV, as that's what is returned if the file system is not on loopback */ | |
1120 | return sd_bus_error_set_errnof(error, r, "Failed to adjust loopback limit: %m"); | |
1121 | } | |
efe02862 | 1122 | |
5bcd08db LP |
1123 | (void) btrfs_qgroup_set_limit("/var/lib/machines", 0, limit); |
1124 | ||
1125 | r = btrfs_subvol_set_subtree_quota_limit("/var/lib/machines", 0, limit); | |
d6ce17c7 LP |
1126 | if (r == -ENOTTY) |
1127 | return sd_bus_error_setf(error, SD_BUS_ERROR_NOT_SUPPORTED, "Quota is only supported on btrfs."); | |
26166c88 | 1128 | if (r < 0) |
d6ce17c7 LP |
1129 | return sd_bus_error_set_errnof(error, r, "Failed to adjust quota limit: %m"); |
1130 | ||
1131 | return sd_bus_reply_method_return(message, NULL); | |
1132 | } | |
1133 | ||
19070062 | 1134 | static int method_set_image_limit(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
d6ce17c7 LP |
1135 | _cleanup_(image_unrefp) Image *i = NULL; |
1136 | const char *name; | |
1137 | int r; | |
1138 | ||
19070062 LP |
1139 | assert(message); |
1140 | ||
d6ce17c7 LP |
1141 | r = sd_bus_message_read(message, "s", &name); |
1142 | if (r < 0) | |
1143 | return r; | |
1144 | ||
1145 | if (!image_name_is_valid(name)) | |
1146 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Image name '%s' is invalid.", name); | |
1147 | ||
1148 | r = image_find(name, &i); | |
1149 | if (r < 0) | |
1150 | return r; | |
1151 | if (r == 0) | |
1152 | return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_IMAGE, "No image '%s' known", name); | |
1153 | ||
1154 | i->userdata = userdata; | |
19070062 | 1155 | return bus_image_method_set_limit(message, i, error); |
d6ce17c7 LP |
1156 | } |
1157 | ||
c01ff965 LP |
1158 | static int method_map_from_machine_user(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
1159 | _cleanup_fclose_ FILE *f = NULL; | |
1160 | Manager *m = userdata; | |
1161 | const char *name, *p; | |
1162 | Machine *machine; | |
1163 | uint32_t uid; | |
1164 | int r; | |
1165 | ||
1166 | r = sd_bus_message_read(message, "su", &name, &uid); | |
1167 | if (r < 0) | |
1168 | return r; | |
1169 | ||
c077529b | 1170 | if (!uid_is_valid(uid)) |
c01ff965 LP |
1171 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid user ID " UID_FMT, uid); |
1172 | ||
1173 | machine = hashmap_get(m->machines, name); | |
1174 | if (!machine) | |
1175 | return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_MACHINE, "No machine '%s' known", name); | |
1176 | ||
a79366e2 LP |
1177 | if (machine->class != MACHINE_CONTAINER) |
1178 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Not supported for non-container machines."); | |
1179 | ||
c01ff965 LP |
1180 | p = procfs_file_alloca(machine->leader, "uid_map"); |
1181 | f = fopen(p, "re"); | |
1182 | if (!f) | |
1183 | return -errno; | |
1184 | ||
1185 | for (;;) { | |
1186 | uid_t uid_base, uid_shift, uid_range, converted; | |
1187 | int k; | |
1188 | ||
1189 | errno = 0; | |
1190 | k = fscanf(f, UID_FMT " " UID_FMT " " UID_FMT, &uid_base, &uid_shift, &uid_range); | |
1191 | if (k < 0 && feof(f)) | |
1192 | break; | |
1193 | if (k != 3) { | |
b3267152 | 1194 | if (ferror(f) && errno > 0) |
c01ff965 LP |
1195 | return -errno; |
1196 | ||
1197 | return -EIO; | |
1198 | } | |
1199 | ||
1200 | if (uid < uid_base || uid >= uid_base + uid_range) | |
1201 | continue; | |
1202 | ||
1203 | converted = uid - uid_base + uid_shift; | |
c077529b | 1204 | if (!uid_is_valid(converted)) |
c01ff965 LP |
1205 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid user ID " UID_FMT, uid); |
1206 | ||
1207 | return sd_bus_reply_method_return(message, "u", (uint32_t) converted); | |
1208 | } | |
1209 | ||
1210 | return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_USER_MAPPING, "Machine '%s' has no matching user mappings.", name); | |
1211 | } | |
1212 | ||
1213 | static int method_map_to_machine_user(sd_bus_message *message, void *userdata, sd_bus_error *error) { | |
1214 | Manager *m = userdata; | |
1215 | Machine *machine; | |
1216 | uid_t uid; | |
1217 | Iterator i; | |
1218 | int r; | |
1219 | ||
1220 | r = sd_bus_message_read(message, "u", &uid); | |
1221 | if (r < 0) | |
1222 | return r; | |
c077529b | 1223 | if (!uid_is_valid(uid)) |
c01ff965 LP |
1224 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid user ID " UID_FMT, uid); |
1225 | if (uid < 0x10000) | |
1226 | return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_USER_MAPPING, "User " UID_FMT " belongs to host UID range", uid); | |
1227 | ||
1228 | HASHMAP_FOREACH(machine, m->machines, i) { | |
1229 | _cleanup_fclose_ FILE *f = NULL; | |
1230 | char p[strlen("/proc//uid_map") + DECIMAL_STR_MAX(pid_t) + 1]; | |
1231 | ||
a79366e2 LP |
1232 | if (machine->class != MACHINE_CONTAINER) |
1233 | continue; | |
1234 | ||
c01ff965 LP |
1235 | xsprintf(p, "/proc/" UID_FMT "/uid_map", machine->leader); |
1236 | f = fopen(p, "re"); | |
1237 | if (!f) { | |
1238 | log_warning_errno(errno, "Failed top open %s, ignoring,", p); | |
1239 | continue; | |
1240 | } | |
1241 | ||
1242 | for (;;) { | |
1243 | _cleanup_free_ char *o = NULL; | |
1244 | uid_t uid_base, uid_shift, uid_range, converted; | |
1245 | int k; | |
1246 | ||
1247 | errno = 0; | |
1248 | k = fscanf(f, UID_FMT " " UID_FMT " " UID_FMT, &uid_base, &uid_shift, &uid_range); | |
1249 | if (k < 0 && feof(f)) | |
1250 | break; | |
1251 | if (k != 3) { | |
b3267152 | 1252 | if (ferror(f) && errno > 0) |
c01ff965 LP |
1253 | return -errno; |
1254 | ||
1255 | return -EIO; | |
1256 | } | |
1257 | ||
1258 | if (uid < uid_shift || uid >= uid_shift + uid_range) | |
1259 | continue; | |
1260 | ||
1261 | converted = (uid - uid_shift + uid_base); | |
c077529b | 1262 | if (!uid_is_valid(converted)) |
c01ff965 LP |
1263 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid user ID " UID_FMT, uid); |
1264 | ||
1265 | o = machine_bus_path(machine); | |
1266 | if (!o) | |
1267 | return -ENOMEM; | |
1268 | ||
1269 | return sd_bus_reply_method_return(message, "sou", machine->name, o, (uint32_t) converted); | |
1270 | } | |
1271 | } | |
1272 | ||
1273 | return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_USER_MAPPING, "No matching user mapping for " UID_FMT ".", uid); | |
1274 | } | |
1275 | ||
1276 | static int method_map_from_machine_group(sd_bus_message *message, void *groupdata, sd_bus_error *error) { | |
1277 | _cleanup_fclose_ FILE *f = NULL; | |
1278 | Manager *m = groupdata; | |
1279 | const char *name, *p; | |
1280 | Machine *machine; | |
1281 | uint32_t gid; | |
1282 | int r; | |
1283 | ||
1284 | r = sd_bus_message_read(message, "su", &name, &gid); | |
1285 | if (r < 0) | |
1286 | return r; | |
1287 | ||
c077529b | 1288 | if (!gid_is_valid(gid)) |
c01ff965 LP |
1289 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid group ID " GID_FMT, gid); |
1290 | ||
1291 | machine = hashmap_get(m->machines, name); | |
1292 | if (!machine) | |
1293 | return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_MACHINE, "No machine '%s' known", name); | |
1294 | ||
a79366e2 LP |
1295 | if (machine->class != MACHINE_CONTAINER) |
1296 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Not supported for non-container machines."); | |
1297 | ||
c01ff965 LP |
1298 | p = procfs_file_alloca(machine->leader, "gid_map"); |
1299 | f = fopen(p, "re"); | |
1300 | if (!f) | |
1301 | return -errno; | |
1302 | ||
1303 | for (;;) { | |
1304 | gid_t gid_base, gid_shift, gid_range, converted; | |
1305 | int k; | |
1306 | ||
1307 | errno = 0; | |
1308 | k = fscanf(f, GID_FMT " " GID_FMT " " GID_FMT, &gid_base, &gid_shift, &gid_range); | |
1309 | if (k < 0 && feof(f)) | |
1310 | break; | |
1311 | if (k != 3) { | |
b3267152 | 1312 | if (ferror(f) && errno > 0) |
c01ff965 LP |
1313 | return -errno; |
1314 | ||
1315 | return -EIO; | |
1316 | } | |
1317 | ||
1318 | if (gid < gid_base || gid >= gid_base + gid_range) | |
1319 | continue; | |
1320 | ||
1321 | converted = gid - gid_base + gid_shift; | |
c077529b | 1322 | if (!gid_is_valid(converted)) |
c01ff965 LP |
1323 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid group ID " GID_FMT, gid); |
1324 | ||
1325 | return sd_bus_reply_method_return(message, "u", (uint32_t) converted); | |
1326 | } | |
1327 | ||
1328 | return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_GROUP_MAPPING, "Machine '%s' has no matching group mappings.", name); | |
1329 | } | |
1330 | ||
1331 | static int method_map_to_machine_group(sd_bus_message *message, void *groupdata, sd_bus_error *error) { | |
1332 | Manager *m = groupdata; | |
1333 | Machine *machine; | |
1334 | gid_t gid; | |
1335 | Iterator i; | |
1336 | int r; | |
1337 | ||
1338 | r = sd_bus_message_read(message, "u", &gid); | |
1339 | if (r < 0) | |
1340 | return r; | |
c077529b | 1341 | if (!gid_is_valid(gid)) |
c01ff965 LP |
1342 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid group ID " GID_FMT, gid); |
1343 | if (gid < 0x10000) | |
1344 | return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_GROUP_MAPPING, "Group " GID_FMT " belongs to host GID range", gid); | |
1345 | ||
1346 | HASHMAP_FOREACH(machine, m->machines, i) { | |
1347 | _cleanup_fclose_ FILE *f = NULL; | |
1348 | char p[strlen("/proc//gid_map") + DECIMAL_STR_MAX(pid_t) + 1]; | |
1349 | ||
a79366e2 LP |
1350 | if (machine->class != MACHINE_CONTAINER) |
1351 | continue; | |
1352 | ||
c01ff965 LP |
1353 | xsprintf(p, "/proc/" GID_FMT "/gid_map", machine->leader); |
1354 | f = fopen(p, "re"); | |
1355 | if (!f) { | |
1356 | log_warning_errno(errno, "Failed top open %s, ignoring,", p); | |
1357 | continue; | |
1358 | } | |
1359 | ||
1360 | for (;;) { | |
1361 | _cleanup_free_ char *o = NULL; | |
1362 | gid_t gid_base, gid_shift, gid_range, converted; | |
1363 | int k; | |
1364 | ||
1365 | errno = 0; | |
1366 | k = fscanf(f, GID_FMT " " GID_FMT " " GID_FMT, &gid_base, &gid_shift, &gid_range); | |
1367 | if (k < 0 && feof(f)) | |
1368 | break; | |
1369 | if (k != 3) { | |
b3267152 | 1370 | if (ferror(f) && errno > 0) |
c01ff965 LP |
1371 | return -errno; |
1372 | ||
1373 | return -EIO; | |
1374 | } | |
1375 | ||
1376 | if (gid < gid_shift || gid >= gid_shift + gid_range) | |
1377 | continue; | |
1378 | ||
1379 | converted = (gid - gid_shift + gid_base); | |
c077529b | 1380 | if (!gid_is_valid(converted)) |
c01ff965 LP |
1381 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid group ID " GID_FMT, gid); |
1382 | ||
1383 | o = machine_bus_path(machine); | |
1384 | if (!o) | |
1385 | return -ENOMEM; | |
1386 | ||
1387 | return sd_bus_reply_method_return(message, "sou", machine->name, o, (uint32_t) converted); | |
1388 | } | |
1389 | } | |
1390 | ||
1391 | return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_GROUP_MAPPING, "No matching group mapping for " GID_FMT ".", gid); | |
1392 | } | |
1393 | ||
c3350683 LP |
1394 | const sd_bus_vtable manager_vtable[] = { |
1395 | SD_BUS_VTABLE_START(0), | |
160e3793 LP |
1396 | SD_BUS_PROPERTY("PoolPath", "s", property_get_pool_path, 0, 0), |
1397 | SD_BUS_PROPERTY("PoolUsage", "t", property_get_pool_usage, 0, 0), | |
1398 | SD_BUS_PROPERTY("PoolLimit", "t", property_get_pool_limit, 0, 0), | |
adacb957 | 1399 | SD_BUS_METHOD("GetMachine", "s", "o", method_get_machine, SD_BUS_VTABLE_UNPRIVILEGED), |
c2ce6a3d | 1400 | SD_BUS_METHOD("GetImage", "s", "o", method_get_image, SD_BUS_VTABLE_UNPRIVILEGED), |
adacb957 LP |
1401 | SD_BUS_METHOD("GetMachineByPID", "u", "o", method_get_machine_by_pid, SD_BUS_VTABLE_UNPRIVILEGED), |
1402 | SD_BUS_METHOD("ListMachines", NULL, "a(ssso)", method_list_machines, SD_BUS_VTABLE_UNPRIVILEGED), | |
b6b18498 | 1403 | SD_BUS_METHOD("ListImages", NULL, "a(ssbttto)", method_list_images, SD_BUS_VTABLE_UNPRIVILEGED), |
c3350683 | 1404 | SD_BUS_METHOD("CreateMachine", "sayssusa(sv)", "o", method_create_machine, 0), |
9b5ed6fe | 1405 | SD_BUS_METHOD("CreateMachineWithNetwork", "sayssusaia(sv)", "o", method_create_machine_with_network, 0), |
8d07a7c4 | 1406 | SD_BUS_METHOD("RegisterMachine", "sayssus", "o", method_register_machine, 0), |
9b5ed6fe | 1407 | SD_BUS_METHOD("RegisterMachineWithNetwork", "sayssusai", "o", method_register_machine_with_network, 0), |
70244d1d LP |
1408 | SD_BUS_METHOD("TerminateMachine", "s", NULL, method_terminate_machine, SD_BUS_VTABLE_UNPRIVILEGED), |
1409 | SD_BUS_METHOD("KillMachine", "ssi", NULL, method_kill_machine, SD_BUS_VTABLE_UNPRIVILEGED), | |
3a6fb33c | 1410 | SD_BUS_METHOD("GetMachineAddresses", "s", "a(iay)", method_get_machine_addresses, SD_BUS_VTABLE_UNPRIVILEGED), |
717603e3 | 1411 | SD_BUS_METHOD("GetMachineOSRelease", "s", "a{ss}", method_get_machine_os_release, SD_BUS_VTABLE_UNPRIVILEGED), |
40205d70 | 1412 | SD_BUS_METHOD("OpenMachinePTY", "s", "hs", method_open_machine_pty, 0), |
d04c1fb8 | 1413 | SD_BUS_METHOD("OpenMachineLogin", "s", "hs", method_open_machine_login, SD_BUS_VTABLE_UNPRIVILEGED), |
49af9e13 | 1414 | SD_BUS_METHOD("OpenMachineShell", "sssasas", "hs", method_open_machine_shell, SD_BUS_VTABLE_UNPRIVILEGED), |
70244d1d LP |
1415 | SD_BUS_METHOD("BindMountMachine", "sssbb", NULL, method_bind_mount_machine, SD_BUS_VTABLE_UNPRIVILEGED), |
1416 | SD_BUS_METHOD("CopyFromMachine", "sss", NULL, method_copy_machine, SD_BUS_VTABLE_UNPRIVILEGED), | |
1417 | SD_BUS_METHOD("CopyToMachine", "sss", NULL, method_copy_machine, SD_BUS_VTABLE_UNPRIVILEGED), | |
ae203207 | 1418 | SD_BUS_METHOD("OpenMachineRootDirectory", "s", "h", method_open_machine_root_directory, SD_BUS_VTABLE_UNPRIVILEGED), |
70244d1d LP |
1419 | SD_BUS_METHOD("RemoveImage", "s", NULL, method_remove_image, SD_BUS_VTABLE_UNPRIVILEGED), |
1420 | SD_BUS_METHOD("RenameImage", "ss", NULL, method_rename_image, SD_BUS_VTABLE_UNPRIVILEGED), | |
1421 | SD_BUS_METHOD("CloneImage", "ssb", NULL, method_clone_image, SD_BUS_VTABLE_UNPRIVILEGED), | |
1422 | SD_BUS_METHOD("MarkImageReadOnly", "sb", NULL, method_mark_image_read_only, SD_BUS_VTABLE_UNPRIVILEGED), | |
9153b02b | 1423 | SD_BUS_METHOD("GetImageOSRelease", "s", "a{ss}", method_get_image_os_release, SD_BUS_VTABLE_UNPRIVILEGED), |
d6ce17c7 LP |
1424 | SD_BUS_METHOD("SetPoolLimit", "t", NULL, method_set_pool_limit, SD_BUS_VTABLE_UNPRIVILEGED), |
1425 | SD_BUS_METHOD("SetImageLimit", "st", NULL, method_set_image_limit, SD_BUS_VTABLE_UNPRIVILEGED), | |
d94c2b06 | 1426 | SD_BUS_METHOD("CleanPool", "s", "a(st)", method_clean_pool, SD_BUS_VTABLE_UNPRIVILEGED), |
c01ff965 LP |
1427 | SD_BUS_METHOD("MapFromMachineUser", "su", "u", method_map_from_machine_user, SD_BUS_VTABLE_UNPRIVILEGED), |
1428 | SD_BUS_METHOD("MapToMachineUser", "u", "sou", method_map_to_machine_user, SD_BUS_VTABLE_UNPRIVILEGED), | |
1429 | SD_BUS_METHOD("MapFromMachineGroup", "su", "u", method_map_from_machine_group, SD_BUS_VTABLE_UNPRIVILEGED), | |
1430 | SD_BUS_METHOD("MapToMachineGroup", "u", "sou", method_map_to_machine_group, SD_BUS_VTABLE_UNPRIVILEGED), | |
c3350683 LP |
1431 | SD_BUS_SIGNAL("MachineNew", "so", 0), |
1432 | SD_BUS_SIGNAL("MachineRemoved", "so", 0), | |
1433 | SD_BUS_VTABLE_END | |
1434 | }; | |
1ee306e1 | 1435 | |
19070062 | 1436 | int match_job_removed(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
c3350683 | 1437 | const char *path, *result, *unit; |
1ee306e1 | 1438 | Manager *m = userdata; |
c3350683 LP |
1439 | Machine *machine; |
1440 | uint32_t id; | |
1441 | int r; | |
1ee306e1 | 1442 | |
1ee306e1 | 1443 | assert(message); |
c3350683 | 1444 | assert(m); |
1ee306e1 | 1445 | |
c3350683 LP |
1446 | r = sd_bus_message_read(message, "uoss", &id, &path, &unit, &result); |
1447 | if (r < 0) { | |
ebcf1f97 | 1448 | bus_log_parse_error(r); |
65d73cf0 | 1449 | return 0; |
c3350683 | 1450 | } |
6797c324 | 1451 | |
c3350683 LP |
1452 | machine = hashmap_get(m->machine_units, unit); |
1453 | if (!machine) | |
1454 | return 0; | |
6797c324 | 1455 | |
c3350683 | 1456 | if (streq_ptr(path, machine->scope_job)) { |
491ac9f2 | 1457 | machine->scope_job = mfree(machine->scope_job); |
6797c324 | 1458 | |
c3350683 LP |
1459 | if (machine->started) { |
1460 | if (streq(result, "done")) | |
1461 | machine_send_create_reply(machine, NULL); | |
1462 | else { | |
4afd3348 | 1463 | _cleanup_(sd_bus_error_free) sd_bus_error e = SD_BUS_ERROR_NULL; |
6797c324 | 1464 | |
ebcf1f97 | 1465 | sd_bus_error_setf(&e, BUS_ERROR_JOB_FAILED, "Start job for unit %s failed with '%s'", unit, result); |
6797c324 | 1466 | |
ebcf1f97 | 1467 | machine_send_create_reply(machine, &e); |
c3350683 | 1468 | } |
49f3fffd LP |
1469 | } |
1470 | ||
1471 | machine_save(machine); | |
1ee306e1 LP |
1472 | } |
1473 | ||
c3350683 LP |
1474 | machine_add_to_gc_queue(machine); |
1475 | return 0; | |
1ee306e1 LP |
1476 | } |
1477 | ||
19070062 | 1478 | int match_properties_changed(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
c3350683 | 1479 | _cleanup_free_ char *unit = NULL; |
49f3fffd | 1480 | const char *path; |
c3350683 LP |
1481 | Manager *m = userdata; |
1482 | Machine *machine; | |
ebcf1f97 | 1483 | int r; |
554604b3 | 1484 | |
c3350683 LP |
1485 | assert(message); |
1486 | assert(m); | |
554604b3 | 1487 | |
c3350683 LP |
1488 | path = sd_bus_message_get_path(message); |
1489 | if (!path) | |
554604b3 | 1490 | return 0; |
554604b3 | 1491 | |
ebcf1f97 | 1492 | r = unit_name_from_dbus_path(path, &unit); |
e5f5b5b9 LP |
1493 | if (r == -EINVAL) /* not for a unit */ |
1494 | return 0; | |
9ed794a3 | 1495 | if (r < 0) { |
9b420b3c LP |
1496 | log_oom(); |
1497 | return 0; | |
1498 | } | |
554604b3 | 1499 | |
c3350683 | 1500 | machine = hashmap_get(m->machine_units, unit); |
9b420b3c LP |
1501 | if (!machine) |
1502 | return 0; | |
1503 | ||
9b420b3c | 1504 | machine_add_to_gc_queue(machine); |
c3350683 LP |
1505 | return 0; |
1506 | } | |
554604b3 | 1507 | |
19070062 | 1508 | int match_unit_removed(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
c3350683 LP |
1509 | const char *path, *unit; |
1510 | Manager *m = userdata; | |
1511 | Machine *machine; | |
1512 | int r; | |
554604b3 | 1513 | |
c3350683 LP |
1514 | assert(message); |
1515 | assert(m); | |
554604b3 | 1516 | |
c3350683 LP |
1517 | r = sd_bus_message_read(message, "so", &unit, &path); |
1518 | if (r < 0) { | |
ebcf1f97 | 1519 | bus_log_parse_error(r); |
9b420b3c | 1520 | return 0; |
554604b3 LP |
1521 | } |
1522 | ||
c3350683 | 1523 | machine = hashmap_get(m->machine_units, unit); |
9b420b3c LP |
1524 | if (!machine) |
1525 | return 0; | |
1526 | ||
9b420b3c | 1527 | machine_add_to_gc_queue(machine); |
c3350683 LP |
1528 | return 0; |
1529 | } | |
554604b3 | 1530 | |
19070062 | 1531 | int match_reloading(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
c3350683 | 1532 | Manager *m = userdata; |
a658cafa LP |
1533 | Machine *machine; |
1534 | Iterator i; | |
c3350683 | 1535 | int b, r; |
554604b3 | 1536 | |
19070062 LP |
1537 | assert(message); |
1538 | assert(m); | |
554604b3 | 1539 | |
c3350683 LP |
1540 | r = sd_bus_message_read(message, "b", &b); |
1541 | if (r < 0) { | |
ebcf1f97 | 1542 | bus_log_parse_error(r); |
65d73cf0 | 1543 | return 0; |
554604b3 | 1544 | } |
a658cafa LP |
1545 | if (b) |
1546 | return 0; | |
554604b3 | 1547 | |
a658cafa LP |
1548 | /* systemd finished reloading, let's recheck all our machines */ |
1549 | log_debug("System manager has been reloaded, rechecking machines..."); | |
554604b3 | 1550 | |
a658cafa LP |
1551 | HASHMAP_FOREACH(machine, m->machines, i) |
1552 | machine_add_to_gc_queue(machine); | |
554604b3 LP |
1553 | |
1554 | return 0; | |
1555 | } | |
1556 | ||
1ee306e1 LP |
1557 | int manager_start_scope( |
1558 | Manager *manager, | |
1559 | const char *scope, | |
1560 | pid_t pid, | |
1561 | const char *slice, | |
1562 | const char *description, | |
c3350683 LP |
1563 | sd_bus_message *more_properties, |
1564 | sd_bus_error *error, | |
1ee306e1 LP |
1565 | char **job) { |
1566 | ||
4afd3348 | 1567 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL, *reply = NULL; |
554604b3 | 1568 | int r; |
1ee306e1 LP |
1569 | |
1570 | assert(manager); | |
1571 | assert(scope); | |
1572 | assert(pid > 1); | |
1573 | ||
c3350683 LP |
1574 | r = sd_bus_message_new_method_call( |
1575 | manager->bus, | |
151b9b96 | 1576 | &m, |
1ee306e1 LP |
1577 | "org.freedesktop.systemd1", |
1578 | "/org/freedesktop/systemd1", | |
1579 | "org.freedesktop.systemd1.Manager", | |
151b9b96 | 1580 | "StartTransientUnit"); |
c3350683 LP |
1581 | if (r < 0) |
1582 | return r; | |
1ee306e1 | 1583 | |
a658cafa | 1584 | r = sd_bus_message_append(m, "ss", strempty(scope), "fail"); |
c3350683 LP |
1585 | if (r < 0) |
1586 | return r; | |
1ee306e1 | 1587 | |
c3350683 LP |
1588 | r = sd_bus_message_open_container(m, 'a', "(sv)"); |
1589 | if (r < 0) | |
1590 | return r; | |
1ee306e1 LP |
1591 | |
1592 | if (!isempty(slice)) { | |
c3350683 LP |
1593 | r = sd_bus_message_append(m, "(sv)", "Slice", "s", slice); |
1594 | if (r < 0) | |
1595 | return r; | |
1ee306e1 LP |
1596 | } |
1597 | ||
1598 | if (!isempty(description)) { | |
c3350683 LP |
1599 | r = sd_bus_message_append(m, "(sv)", "Description", "s", description); |
1600 | if (r < 0) | |
1601 | return r; | |
1ee306e1 LP |
1602 | } |
1603 | ||
c3350683 LP |
1604 | r = sd_bus_message_append(m, "(sv)", "PIDs", "au", 1, pid); |
1605 | if (r < 0) | |
1606 | return r; | |
554604b3 | 1607 | |
a931ad47 LP |
1608 | r = sd_bus_message_append(m, "(sv)", "Delegate", "b", 1); |
1609 | if (r < 0) | |
1610 | return r; | |
1611 | ||
cf7d1a30 | 1612 | r = sd_bus_message_append(m, "(sv)", "TasksMax", "t", UINT64_C(16384)); |
b370fec2 AC |
1613 | if (r < 0) |
1614 | return bus_log_create_error(r); | |
1615 | ||
554604b3 | 1616 | if (more_properties) { |
c3350683 | 1617 | r = sd_bus_message_copy(m, more_properties, true); |
554604b3 LP |
1618 | if (r < 0) |
1619 | return r; | |
1620 | } | |
1621 | ||
c3350683 LP |
1622 | r = sd_bus_message_close_container(m); |
1623 | if (r < 0) | |
1624 | return r; | |
1ee306e1 | 1625 | |
86b8d289 LP |
1626 | r = sd_bus_message_append(m, "a(sa(sv))", 0); |
1627 | if (r < 0) | |
1628 | return r; | |
1629 | ||
c49b30a2 | 1630 | r = sd_bus_call(manager->bus, m, 0, error, &reply); |
c3350683 LP |
1631 | if (r < 0) |
1632 | return r; | |
1ee306e1 LP |
1633 | |
1634 | if (job) { | |
1635 | const char *j; | |
1636 | char *copy; | |
1637 | ||
c3350683 LP |
1638 | r = sd_bus_message_read(reply, "o", &j); |
1639 | if (r < 0) | |
1640 | return r; | |
1ee306e1 LP |
1641 | |
1642 | copy = strdup(j); | |
1643 | if (!copy) | |
1644 | return -ENOMEM; | |
1645 | ||
1646 | *job = copy; | |
1647 | } | |
1648 | ||
c3350683 | 1649 | return 1; |
1ee306e1 LP |
1650 | } |
1651 | ||
c3350683 | 1652 | int manager_stop_unit(Manager *manager, const char *unit, sd_bus_error *error, char **job) { |
4afd3348 | 1653 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; |
1ee306e1 LP |
1654 | int r; |
1655 | ||
1656 | assert(manager); | |
1657 | assert(unit); | |
1658 | ||
c3350683 | 1659 | r = sd_bus_call_method( |
1ee306e1 LP |
1660 | manager->bus, |
1661 | "org.freedesktop.systemd1", | |
1662 | "/org/freedesktop/systemd1", | |
1663 | "org.freedesktop.systemd1.Manager", | |
1664 | "StopUnit", | |
1ee306e1 | 1665 | error, |
c3350683 LP |
1666 | &reply, |
1667 | "ss", unit, "fail"); | |
1ee306e1 | 1668 | if (r < 0) { |
c3350683 LP |
1669 | if (sd_bus_error_has_name(error, BUS_ERROR_NO_SUCH_UNIT) || |
1670 | sd_bus_error_has_name(error, BUS_ERROR_LOAD_FAILED)) { | |
6797c324 LP |
1671 | |
1672 | if (job) | |
1673 | *job = NULL; | |
1674 | ||
c3350683 | 1675 | sd_bus_error_free(error); |
6797c324 LP |
1676 | return 0; |
1677 | } | |
1678 | ||
1ee306e1 LP |
1679 | return r; |
1680 | } | |
1681 | ||
1682 | if (job) { | |
1683 | const char *j; | |
1684 | char *copy; | |
1685 | ||
c3350683 LP |
1686 | r = sd_bus_message_read(reply, "o", &j); |
1687 | if (r < 0) | |
1688 | return r; | |
1ee306e1 LP |
1689 | |
1690 | copy = strdup(j); | |
1691 | if (!copy) | |
1692 | return -ENOMEM; | |
1693 | ||
1694 | *job = copy; | |
1695 | } | |
1696 | ||
6797c324 | 1697 | return 1; |
1ee306e1 LP |
1698 | } |
1699 | ||
de58a50e | 1700 | int manager_kill_unit(Manager *manager, const char *unit, int signo, sd_bus_error *error) { |
1ee306e1 LP |
1701 | assert(manager); |
1702 | assert(unit); | |
1703 | ||
a658cafa | 1704 | return sd_bus_call_method( |
1ee306e1 LP |
1705 | manager->bus, |
1706 | "org.freedesktop.systemd1", | |
1707 | "/org/freedesktop/systemd1", | |
1708 | "org.freedesktop.systemd1.Manager", | |
1709 | "KillUnit", | |
1ee306e1 | 1710 | error, |
a658cafa | 1711 | NULL, |
de58a50e | 1712 | "ssi", unit, "all", signo); |
1ee306e1 LP |
1713 | } |
1714 | ||
1715 | int manager_unit_is_active(Manager *manager, const char *unit) { | |
4afd3348 LP |
1716 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
1717 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; | |
1ee306e1 | 1718 | _cleanup_free_ char *path = NULL; |
1ee306e1 | 1719 | const char *state; |
1ee306e1 LP |
1720 | int r; |
1721 | ||
1722 | assert(manager); | |
1723 | assert(unit); | |
1724 | ||
1ee306e1 LP |
1725 | path = unit_dbus_path_from_name(unit); |
1726 | if (!path) | |
1727 | return -ENOMEM; | |
1728 | ||
c3350683 | 1729 | r = sd_bus_get_property( |
1ee306e1 LP |
1730 | manager->bus, |
1731 | "org.freedesktop.systemd1", | |
1732 | path, | |
c3350683 LP |
1733 | "org.freedesktop.systemd1.Unit", |
1734 | "ActiveState", | |
1ee306e1 | 1735 | &error, |
c3350683 LP |
1736 | &reply, |
1737 | "s"); | |
1ee306e1 | 1738 | if (r < 0) { |
c3350683 LP |
1739 | if (sd_bus_error_has_name(&error, SD_BUS_ERROR_NO_REPLY) || |
1740 | sd_bus_error_has_name(&error, SD_BUS_ERROR_DISCONNECTED)) | |
6797c324 | 1741 | return true; |
6797c324 | 1742 | |
c3350683 LP |
1743 | if (sd_bus_error_has_name(&error, BUS_ERROR_NO_SUCH_UNIT) || |
1744 | sd_bus_error_has_name(&error, BUS_ERROR_LOAD_FAILED)) | |
6797c324 | 1745 | return false; |
6797c324 | 1746 | |
1ee306e1 LP |
1747 | return r; |
1748 | } | |
1749 | ||
c3350683 LP |
1750 | r = sd_bus_message_read(reply, "s", &state); |
1751 | if (r < 0) | |
1ee306e1 | 1752 | return -EINVAL; |
1ee306e1 | 1753 | |
9b420b3c | 1754 | return !STR_IN_SET(state, "inactive", "failed"); |
1ee306e1 | 1755 | } |
bd16acf3 | 1756 | |
c3350683 | 1757 | int manager_job_is_active(Manager *manager, const char *path) { |
4afd3348 LP |
1758 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
1759 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; | |
c3350683 | 1760 | int r; |
bd16acf3 | 1761 | |
c3350683 LP |
1762 | assert(manager); |
1763 | assert(path); | |
bd16acf3 | 1764 | |
c3350683 LP |
1765 | r = sd_bus_get_property( |
1766 | manager->bus, | |
1767 | "org.freedesktop.systemd1", | |
1768 | path, | |
1769 | "org.freedesktop.systemd1.Job", | |
1770 | "State", | |
1771 | &error, | |
1772 | &reply, | |
1773 | "s"); | |
1774 | if (r < 0) { | |
1775 | if (sd_bus_error_has_name(&error, SD_BUS_ERROR_NO_REPLY) || | |
1776 | sd_bus_error_has_name(&error, SD_BUS_ERROR_DISCONNECTED)) | |
1777 | return true; | |
bd16acf3 | 1778 | |
c3350683 LP |
1779 | if (sd_bus_error_has_name(&error, SD_BUS_ERROR_UNKNOWN_OBJECT)) |
1780 | return false; | |
bd16acf3 | 1781 | |
bd16acf3 | 1782 | return r; |
c3350683 | 1783 | } |
bd16acf3 | 1784 | |
c3350683 LP |
1785 | /* We don't actually care about the state really. The fact |
1786 | * that we could read the job state is enough for us */ | |
bd16acf3 | 1787 | |
c3350683 | 1788 | return true; |
bd16acf3 | 1789 | } |
ab49725f KS |
1790 | |
1791 | int manager_get_machine_by_pid(Manager *m, pid_t pid, Machine **machine) { | |
ab49725f KS |
1792 | Machine *mm; |
1793 | int r; | |
1794 | ||
1795 | assert(m); | |
1796 | assert(pid >= 1); | |
1797 | assert(machine); | |
1798 | ||
4a0b58c4 | 1799 | mm = hashmap_get(m->machine_leaders, PID_TO_PTR(pid)); |
077c8c36 LP |
1800 | if (!mm) { |
1801 | _cleanup_free_ char *unit = NULL; | |
ab49725f | 1802 | |
077c8c36 LP |
1803 | r = cg_pid_get_unit(pid, &unit); |
1804 | if (r >= 0) | |
1805 | mm = hashmap_get(m->machine_units, unit); | |
1806 | } | |
ab49725f KS |
1807 | if (!mm) |
1808 | return 0; | |
1809 | ||
1810 | *machine = mm; | |
1811 | return 1; | |
1812 | } | |
1813 | ||
1814 | int manager_add_machine(Manager *m, const char *name, Machine **_machine) { | |
1815 | Machine *machine; | |
1816 | ||
1817 | assert(m); | |
1818 | assert(name); | |
1819 | ||
1820 | machine = hashmap_get(m->machines, name); | |
1821 | if (!machine) { | |
fbe55073 | 1822 | machine = machine_new(m, _MACHINE_CLASS_INVALID, name); |
ab49725f KS |
1823 | if (!machine) |
1824 | return -ENOMEM; | |
1825 | } | |
1826 | ||
1827 | if (_machine) | |
1828 | *_machine = machine; | |
1829 | ||
1830 | return 0; | |
1831 | } | |
9153b02b LP |
1832 | |
1833 | int bus_reply_pair_array(sd_bus_message *m, char **l) { | |
1834 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; | |
1835 | char **k, **v; | |
1836 | int r; | |
1837 | ||
1838 | r = sd_bus_message_new_method_return(m, &reply); | |
1839 | if (r < 0) | |
1840 | return r; | |
1841 | ||
1842 | r = sd_bus_message_open_container(reply, 'a', "{ss}"); | |
1843 | if (r < 0) | |
1844 | return r; | |
1845 | ||
1846 | STRV_FOREACH_PAIR(k, v, l) { | |
1847 | r = sd_bus_message_append(reply, "{ss}", *k, *v); | |
1848 | if (r < 0) | |
1849 | return r; | |
1850 | } | |
1851 | ||
1852 | r = sd_bus_message_close_container(reply); | |
1853 | if (r < 0) | |
1854 | return r; | |
1855 | ||
1856 | return sd_bus_send(NULL, reply, NULL); | |
1857 | ||
1858 | } |