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