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