]>
Commit | Line | Data |
---|---|---|
db9ecf05 | 1 | /* SPDX-License-Identifier: LGPL-2.1-or-later */ |
61d0578b | 2 | |
ca78ad1d ZJS |
3 | #include <fcntl.h> |
4 | #include <sys/stat.h> | |
5 | #include <sys/types.h> | |
6 | #include <unistd.h> | |
7 | ||
61d0578b LP |
8 | #include "alloc-util.h" |
9 | #include "bus-common-errors.h" | |
40af3d02 | 10 | #include "bus-get-properties.h" |
61d0578b | 11 | #include "bus-label.h" |
3b91bae3 | 12 | #include "bus-object.h" |
269e4d2d | 13 | #include "bus-polkit.h" |
61d0578b | 14 | #include "bus-util.h" |
57f1b61b | 15 | #include "discover-image.h" |
61d0578b LP |
16 | #include "fd-util.h" |
17 | #include "fileio.h" | |
18 | #include "io-util.h" | |
204f52e3 | 19 | #include "missing_capability.h" |
6ef06723 | 20 | #include "os-util.h" |
61d0578b LP |
21 | #include "portable.h" |
22 | #include "portabled-bus.h" | |
23 | #include "portabled-image-bus.h" | |
24 | #include "portabled-image.h" | |
25 | #include "portabled.h" | |
26 | #include "process-util.h" | |
27 | #include "strv.h" | |
28 | #include "user-util.h" | |
29 | ||
30 | static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_type, image_type, ImageType); | |
31 | ||
32 | int bus_image_common_get_os_release( | |
33 | Manager *m, | |
34 | sd_bus_message *message, | |
35 | const char *name_or_path, | |
36 | Image *image, | |
37 | sd_bus_error *error) { | |
38 | ||
39 | int r; | |
40 | ||
41 | assert(name_or_path || image); | |
42 | assert(message); | |
43 | ||
44 | if (!m) { | |
45 | assert(image); | |
46 | m = image->userdata; | |
47 | } | |
48 | ||
49 | r = bus_image_acquire(m, | |
50 | message, | |
51 | name_or_path, | |
52 | image, | |
53 | BUS_IMAGE_AUTHENTICATE_BY_PATH, | |
54 | "org.freedesktop.portable1.inspect-images", | |
55 | &image, | |
56 | error); | |
57 | if (r < 0) | |
58 | return r; | |
59 | if (r == 0) /* Will call us back */ | |
60 | return 1; | |
61 | ||
62 | if (!image->metadata_valid) { | |
84be0c71 | 63 | r = image_read_metadata(image, &image_policy_service); |
61d0578b LP |
64 | if (r < 0) |
65 | return sd_bus_error_set_errnof(error, r, "Failed to read image metadata: %m"); | |
66 | } | |
67 | ||
68 | return bus_reply_pair_array(message, image->os_release); | |
69 | } | |
70 | ||
71 | static int bus_image_method_get_os_release(sd_bus_message *message, void *userdata, sd_bus_error *error) { | |
72 | return bus_image_common_get_os_release(NULL, message, NULL, userdata, error); | |
73 | } | |
74 | ||
75 | static int append_fd(sd_bus_message *m, PortableMetadata *d) { | |
76 | _cleanup_fclose_ FILE *f = NULL; | |
77 | _cleanup_free_ char *buf = NULL; | |
907952bb | 78 | size_t n = 0; |
61d0578b LP |
79 | int r; |
80 | ||
81 | assert(m); | |
61d0578b | 82 | |
907952bb LB |
83 | if (d) { |
84 | assert(d->fd >= 0); | |
61d0578b | 85 | |
fe008489 YW |
86 | r = fdopen_independent(d->fd, "r", &f); |
87 | if (r < 0) | |
88 | return r; | |
907952bb LB |
89 | |
90 | r = read_full_stream(f, &buf, &n); | |
91 | if (r < 0) | |
92 | return r; | |
93 | } | |
61d0578b LP |
94 | |
95 | return sd_bus_message_append_array(m, 'y', buf, n); | |
96 | } | |
97 | ||
98 | int bus_image_common_get_metadata( | |
99 | Manager *m, | |
100 | sd_bus_message *message, | |
101 | const char *name_or_path, | |
102 | Image *image, | |
103 | sd_bus_error *error) { | |
104 | ||
e3f7ed94 | 105 | _cleanup_ordered_hashmap_free_ OrderedHashmap *extension_releases = NULL; |
61d0578b | 106 | _cleanup_(portable_metadata_unrefp) PortableMetadata *os_release = NULL; |
907952bb | 107 | _cleanup_strv_free_ char **matches = NULL, **extension_images = NULL; |
67818055 | 108 | _cleanup_hashmap_free_ Hashmap *unit_files = NULL; |
61d0578b LP |
109 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; |
110 | _cleanup_free_ PortableMetadata **sorted = NULL; | |
06768b90 | 111 | PortableFlags flags = 0; |
61d0578b LP |
112 | int r; |
113 | ||
114 | assert(name_or_path || image); | |
115 | assert(message); | |
116 | ||
117 | if (!m) { | |
118 | assert(image); | |
119 | m = image->userdata; | |
120 | } | |
121 | ||
087a799f ZJS |
122 | bool have_exti = sd_bus_message_is_method_call(message, NULL, "GetImageMetadataWithExtensions") || |
123 | sd_bus_message_is_method_call(message, NULL, "GetMetadataWithExtensions"); | |
124 | ||
125 | if (have_exti) { | |
907952bb LB |
126 | r = sd_bus_message_read_strv(message, &extension_images); |
127 | if (r < 0) | |
128 | return r; | |
129 | } | |
130 | ||
61d0578b LP |
131 | r = sd_bus_message_read_strv(message, &matches); |
132 | if (r < 0) | |
133 | return r; | |
134 | ||
087a799f | 135 | if (have_exti) { |
e3f7ed94 LB |
136 | uint64_t input_flags = 0; |
137 | ||
907952bb LB |
138 | r = sd_bus_message_read(message, "t", &input_flags); |
139 | if (r < 0) | |
140 | return r; | |
e3f7ed94 LB |
141 | |
142 | if ((input_flags & ~_PORTABLE_MASK_PUBLIC) != 0) | |
907952bb LB |
143 | return sd_bus_reply_method_errorf(message, SD_BUS_ERROR_INVALID_ARGS, |
144 | "Invalid 'flags' parameter '%" PRIu64 "'", | |
145 | input_flags); | |
06768b90 | 146 | flags |= input_flags; |
907952bb LB |
147 | } |
148 | ||
61d0578b LP |
149 | r = bus_image_acquire(m, |
150 | message, | |
151 | name_or_path, | |
152 | image, | |
153 | BUS_IMAGE_AUTHENTICATE_BY_PATH, | |
154 | "org.freedesktop.portable1.inspect-images", | |
155 | &image, | |
156 | error); | |
157 | if (r < 0) | |
158 | return r; | |
159 | if (r == 0) /* Will call us back */ | |
160 | return 1; | |
161 | ||
162 | r = portable_extract( | |
163 | image->path, | |
164 | matches, | |
907952bb | 165 | extension_images, |
84be0c71 | 166 | /* image_policy= */ NULL, |
06768b90 | 167 | flags, |
61d0578b | 168 | &os_release, |
e3f7ed94 | 169 | &extension_releases, |
61d0578b | 170 | &unit_files, |
8a129c80 | 171 | NULL, |
61d0578b LP |
172 | error); |
173 | if (r < 0) | |
174 | return r; | |
175 | ||
176 | r = portable_metadata_hashmap_to_sorted_array(unit_files, &sorted); | |
177 | if (r < 0) | |
178 | return r; | |
179 | ||
180 | r = sd_bus_message_new_method_return(message, &reply); | |
181 | if (r < 0) | |
182 | return r; | |
183 | ||
184 | r = sd_bus_message_append(reply, "s", image->path); | |
185 | if (r < 0) | |
186 | return r; | |
187 | ||
188 | r = append_fd(reply, os_release); | |
189 | if (r < 0) | |
190 | return r; | |
191 | ||
e3f7ed94 LB |
192 | /* If it was requested, also send back the extension path and the content |
193 | * of each extension-release file. Behind a flag, as it's an incompatible | |
194 | * change. */ | |
087a799f | 195 | if (have_exti) { |
e3f7ed94 LB |
196 | PortableMetadata *extension_release; |
197 | ||
087a799f ZJS |
198 | r = sd_bus_message_open_container(reply, 'a', "{say}"); |
199 | if (r < 0) | |
200 | return r; | |
201 | ||
e3f7ed94 LB |
202 | ORDERED_HASHMAP_FOREACH(extension_release, extension_releases) { |
203 | ||
204 | r = sd_bus_message_open_container(reply, 'e', "say"); | |
205 | if (r < 0) | |
206 | return r; | |
207 | ||
208 | r = sd_bus_message_append(reply, "s", extension_release->image_path); | |
209 | if (r < 0) | |
210 | return r; | |
211 | ||
212 | r = append_fd(reply, extension_release); | |
213 | if (r < 0) | |
214 | return r; | |
215 | ||
216 | r = sd_bus_message_close_container(reply); | |
217 | if (r < 0) | |
218 | return r; | |
219 | } | |
087a799f ZJS |
220 | |
221 | r = sd_bus_message_close_container(reply); | |
222 | if (r < 0) | |
223 | return r; | |
e3f7ed94 LB |
224 | } |
225 | ||
087a799f ZJS |
226 | r = sd_bus_message_open_container(reply, 'a', "{say}"); |
227 | if (r < 0) | |
228 | return r; | |
229 | ||
90e3f358 | 230 | for (size_t i = 0; i < hashmap_size(unit_files); i++) { |
61d0578b | 231 | |
61d0578b LP |
232 | r = sd_bus_message_open_container(reply, 'e', "say"); |
233 | if (r < 0) | |
234 | return r; | |
235 | ||
236 | r = sd_bus_message_append(reply, "s", sorted[i]->name); | |
237 | if (r < 0) | |
238 | return r; | |
239 | ||
240 | r = append_fd(reply, sorted[i]); | |
241 | if (r < 0) | |
242 | return r; | |
243 | ||
244 | r = sd_bus_message_close_container(reply); | |
245 | if (r < 0) | |
246 | return r; | |
247 | } | |
248 | ||
249 | r = sd_bus_message_close_container(reply); | |
250 | if (r < 0) | |
251 | return r; | |
252 | ||
253 | return sd_bus_send(NULL, reply, NULL); | |
254 | } | |
255 | ||
256 | static int bus_image_method_get_metadata(sd_bus_message *message, void *userdata, sd_bus_error *error) { | |
257 | return bus_image_common_get_metadata(NULL, message, NULL, userdata, error); | |
258 | } | |
259 | ||
260 | static int bus_image_method_get_state( | |
261 | sd_bus_message *message, | |
262 | void *userdata, | |
263 | sd_bus_error *error) { | |
264 | ||
0017415c | 265 | _cleanup_strv_free_ char **extension_images = NULL; |
99534007 | 266 | Image *image = ASSERT_PTR(userdata); |
61d0578b LP |
267 | PortableState state; |
268 | int r; | |
269 | ||
270 | assert(message); | |
61d0578b | 271 | |
0017415c LB |
272 | if (sd_bus_message_is_method_call(message, NULL, "GetStateWithExtensions")) { |
273 | uint64_t input_flags = 0; | |
274 | ||
275 | r = sd_bus_message_read_strv(message, &extension_images); | |
276 | if (r < 0) | |
277 | return r; | |
278 | ||
279 | r = sd_bus_message_read(message, "t", &input_flags); | |
280 | if (r < 0) | |
281 | return r; | |
282 | ||
283 | /* No flags are supported by this method for now. */ | |
284 | if (input_flags != 0) | |
285 | return sd_bus_reply_method_errorf(message, SD_BUS_ERROR_INVALID_ARGS, | |
286 | "Invalid 'flags' parameter '%" PRIu64 "'", | |
287 | input_flags); | |
288 | } | |
289 | ||
61d0578b LP |
290 | r = portable_get_state( |
291 | sd_bus_message_get_bus(message), | |
292 | image->path, | |
0017415c | 293 | extension_images, |
61d0578b LP |
294 | 0, |
295 | &state, | |
296 | error); | |
297 | if (r < 0) | |
298 | return r; | |
299 | ||
300 | return sd_bus_reply_method_return(message, "s", portable_state_to_string(state)); | |
301 | } | |
302 | ||
303 | int bus_image_common_attach( | |
304 | Manager *m, | |
305 | sd_bus_message *message, | |
306 | const char *name_or_path, | |
307 | Image *image, | |
308 | sd_bus_error *error) { | |
309 | ||
907952bb | 310 | _cleanup_strv_free_ char **matches = NULL, **extension_images = NULL; |
61d0578b LP |
311 | PortableChange *changes = NULL; |
312 | PortableFlags flags = 0; | |
c5a935b0 | 313 | const char *profile, *copy_mode; |
61d0578b | 314 | size_t n_changes = 0; |
907952bb | 315 | int r; |
61d0578b LP |
316 | |
317 | assert(message); | |
318 | assert(name_or_path || image); | |
319 | ||
2b4b01b0 DT |
320 | CLEANUP_ARRAY(changes, n_changes, portable_changes_free); |
321 | ||
61d0578b LP |
322 | if (!m) { |
323 | assert(image); | |
324 | m = image->userdata; | |
325 | } | |
326 | ||
907952bb LB |
327 | if (sd_bus_message_is_method_call(message, NULL, "AttachImageWithExtensions") || |
328 | sd_bus_message_is_method_call(message, NULL, "AttachWithExtensions")) { | |
329 | r = sd_bus_message_read_strv(message, &extension_images); | |
330 | if (r < 0) | |
331 | return r; | |
332 | } | |
333 | ||
61d0578b LP |
334 | r = sd_bus_message_read_strv(message, &matches); |
335 | if (r < 0) | |
336 | return r; | |
337 | ||
907952bb | 338 | r = sd_bus_message_read(message, "s", &profile); |
61d0578b LP |
339 | if (r < 0) |
340 | return r; | |
341 | ||
907952bb LB |
342 | if (sd_bus_message_is_method_call(message, NULL, "AttachImageWithExtensions") || |
343 | sd_bus_message_is_method_call(message, NULL, "AttachWithExtensions")) { | |
344 | uint64_t input_flags = 0; | |
345 | ||
346 | r = sd_bus_message_read(message, "st", ©_mode, &input_flags); | |
347 | if (r < 0) | |
348 | return r; | |
349 | if ((input_flags & ~_PORTABLE_MASK_PUBLIC) != 0) | |
350 | return sd_bus_reply_method_errorf(message, SD_BUS_ERROR_INVALID_ARGS, | |
351 | "Invalid 'flags' parameter '%" PRIu64 "'", | |
352 | input_flags); | |
353 | flags |= input_flags; | |
354 | } else { | |
355 | int runtime; | |
356 | ||
357 | r = sd_bus_message_read(message, "bs", &runtime, ©_mode); | |
358 | if (r < 0) | |
359 | return r; | |
360 | ||
361 | if (runtime) | |
362 | flags |= PORTABLE_RUNTIME; | |
363 | } | |
364 | ||
61d0578b LP |
365 | if (streq(copy_mode, "symlink")) |
366 | flags |= PORTABLE_PREFER_SYMLINK; | |
367 | else if (streq(copy_mode, "copy")) | |
368 | flags |= PORTABLE_PREFER_COPY; | |
369 | else if (!isempty(copy_mode)) | |
370 | return sd_bus_reply_method_errorf(message, SD_BUS_ERROR_INVALID_ARGS, "Unknown copy mode '%s'", copy_mode); | |
371 | ||
61d0578b LP |
372 | r = bus_image_acquire(m, |
373 | message, | |
374 | name_or_path, | |
375 | image, | |
376 | BUS_IMAGE_AUTHENTICATE_ALL, | |
377 | "org.freedesktop.portable1.attach-images", | |
378 | &image, | |
379 | error); | |
380 | if (r < 0) | |
381 | return r; | |
382 | if (r == 0) /* Will call us back */ | |
383 | return 1; | |
384 | ||
385 | r = portable_attach( | |
386 | sd_bus_message_get_bus(message), | |
387 | image->path, | |
388 | matches, | |
389 | profile, | |
907952bb | 390 | extension_images, |
84be0c71 | 391 | /* image_policy= */ NULL, |
61d0578b LP |
392 | flags, |
393 | &changes, | |
394 | &n_changes, | |
395 | error); | |
396 | if (r < 0) | |
2b4b01b0 | 397 | return r; |
61d0578b | 398 | |
2b4b01b0 | 399 | return reply_portable_changes(message, changes, n_changes); |
61d0578b LP |
400 | } |
401 | ||
402 | static int bus_image_method_attach(sd_bus_message *message, void *userdata, sd_bus_error *error) { | |
403 | return bus_image_common_attach(NULL, message, NULL, userdata, error); | |
404 | } | |
405 | ||
406 | static int bus_image_method_detach( | |
407 | sd_bus_message *message, | |
408 | void *userdata, | |
409 | sd_bus_error *error) { | |
410 | ||
907952bb | 411 | _cleanup_strv_free_ char **extension_images = NULL; |
61d0578b | 412 | PortableChange *changes = NULL; |
99534007 DT |
413 | Image *image = ASSERT_PTR(userdata); |
414 | Manager *m = ASSERT_PTR(image->userdata); | |
907952bb | 415 | PortableFlags flags = 0; |
61d0578b | 416 | size_t n_changes = 0; |
907952bb | 417 | int r; |
61d0578b LP |
418 | |
419 | assert(message); | |
61d0578b | 420 | |
a5290eff DT |
421 | CLEANUP_ARRAY(changes, n_changes, portable_changes_free); |
422 | ||
907952bb LB |
423 | if (sd_bus_message_is_method_call(message, NULL, "DetachWithExtensions")) { |
424 | r = sd_bus_message_read_strv(message, &extension_images); | |
425 | if (r < 0) | |
426 | return r; | |
427 | } | |
428 | ||
429 | if (sd_bus_message_is_method_call(message, NULL, "DetachWithExtensions")) { | |
430 | uint64_t input_flags = 0; | |
431 | ||
432 | r = sd_bus_message_read(message, "t", &input_flags); | |
433 | if (r < 0) | |
434 | return r; | |
435 | ||
436 | if ((input_flags & ~_PORTABLE_MASK_PUBLIC) != 0) | |
437 | return sd_bus_reply_method_errorf(message, SD_BUS_ERROR_INVALID_ARGS, | |
438 | "Invalid 'flags' parameter '%" PRIu64 "'", | |
439 | input_flags); | |
440 | flags |= input_flags; | |
441 | } else { | |
442 | int runtime; | |
443 | ||
444 | r = sd_bus_message_read(message, "b", &runtime); | |
445 | if (r < 0) | |
446 | return r; | |
447 | ||
448 | if (runtime) | |
449 | flags |= PORTABLE_RUNTIME; | |
450 | } | |
61d0578b LP |
451 | |
452 | r = bus_verify_polkit_async( | |
453 | message, | |
454 | CAP_SYS_ADMIN, | |
455 | "org.freedesktop.portable1.attach-images", | |
456 | NULL, | |
457 | false, | |
458 | UID_INVALID, | |
459 | &m->polkit_registry, | |
460 | error); | |
461 | if (r < 0) | |
462 | return r; | |
463 | if (r == 0) | |
464 | return 1; /* Will call us back */ | |
465 | ||
466 | r = portable_detach( | |
467 | sd_bus_message_get_bus(message), | |
468 | image->path, | |
907952bb LB |
469 | extension_images, |
470 | flags, | |
61d0578b LP |
471 | &changes, |
472 | &n_changes, | |
473 | error); | |
474 | if (r < 0) | |
a5290eff | 475 | return r; |
61d0578b | 476 | |
a5290eff | 477 | return reply_portable_changes(message, changes, n_changes); |
61d0578b LP |
478 | } |
479 | ||
480 | int bus_image_common_remove( | |
481 | Manager *m, | |
482 | sd_bus_message *message, | |
483 | const char *name_or_path, | |
484 | Image *image, | |
485 | sd_bus_error *error) { | |
486 | ||
19ee48a6 | 487 | _cleanup_close_pair_ int errno_pipe_fd[2] = PIPE_EBADF; |
61d0578b LP |
488 | _cleanup_(sigkill_waitp) pid_t child = 0; |
489 | PortableState state; | |
490 | int r; | |
491 | ||
492 | assert(message); | |
493 | assert(name_or_path || image); | |
494 | ||
495 | if (!m) { | |
496 | assert(image); | |
497 | m = image->userdata; | |
498 | } | |
499 | ||
500 | if (m->n_operations >= OPERATIONS_MAX) | |
1b09b81c | 501 | return sd_bus_error_set(error, SD_BUS_ERROR_LIMITS_EXCEEDED, "Too many ongoing operations."); |
61d0578b LP |
502 | |
503 | r = bus_image_acquire(m, | |
504 | message, | |
505 | name_or_path, | |
506 | image, | |
507 | BUS_IMAGE_AUTHENTICATE_ALL, | |
508 | "org.freedesktop.portable1.manage-images", | |
509 | &image, | |
510 | error); | |
511 | if (r < 0) | |
512 | return r; | |
513 | if (r == 0) | |
514 | return 1; /* Will call us back */ | |
515 | ||
516 | r = portable_get_state( | |
517 | sd_bus_message_get_bus(message), | |
518 | image->path, | |
0017415c | 519 | NULL, |
61d0578b LP |
520 | 0, |
521 | &state, | |
522 | error); | |
523 | if (r < 0) | |
524 | return r; | |
525 | ||
526 | if (state != PORTABLE_DETACHED) | |
527 | return sd_bus_error_set_errnof(error, EBUSY, "Image '%s' is not detached, refusing.", image->path); | |
528 | ||
529 | if (pipe2(errno_pipe_fd, O_CLOEXEC|O_NONBLOCK) < 0) | |
530 | return sd_bus_error_set_errnof(error, errno, "Failed to create pipe: %m"); | |
531 | ||
532 | r = safe_fork("(sd-imgrm)", FORK_RESET_SIGNALS, &child); | |
533 | if (r < 0) | |
534 | return sd_bus_error_set_errnof(error, r, "Failed to fork(): %m"); | |
535 | if (r == 0) { | |
536 | errno_pipe_fd[0] = safe_close(errno_pipe_fd[0]); | |
537 | ||
538 | r = image_remove(image); | |
539 | if (r < 0) { | |
540 | (void) write(errno_pipe_fd[1], &r, sizeof(r)); | |
541 | _exit(EXIT_FAILURE); | |
542 | } | |
543 | ||
544 | _exit(EXIT_SUCCESS); | |
545 | } | |
546 | ||
547 | errno_pipe_fd[1] = safe_close(errno_pipe_fd[1]); | |
548 | ||
549 | r = operation_new(m, child, message, errno_pipe_fd[0], NULL); | |
550 | if (r < 0) | |
551 | return r; | |
552 | ||
553 | child = 0; | |
254d1313 | 554 | errno_pipe_fd[0] = -EBADF; |
61d0578b LP |
555 | |
556 | return 1; | |
557 | } | |
558 | ||
559 | static int bus_image_method_remove(sd_bus_message *message, void *userdata, sd_bus_error *error) { | |
560 | return bus_image_common_remove(NULL, message, NULL, userdata, error); | |
561 | } | |
562 | ||
e26fe5f9 LB |
563 | /* Given two PortableChange arrays, return a new array that has all elements of the first that are |
564 | * not also present in the second, comparing the basename of the path values. */ | |
565 | static int normalize_portable_changes( | |
566 | const PortableChange *changes_attached, | |
567 | size_t n_changes_attached, | |
568 | const PortableChange *changes_detached, | |
569 | size_t n_changes_detached, | |
570 | PortableChange **ret_changes, | |
571 | size_t *ret_n_changes) { | |
572 | ||
573 | PortableChange *changes = NULL; | |
574 | size_t n_changes = 0; | |
e26fe5f9 LB |
575 | |
576 | assert(ret_n_changes); | |
577 | assert(ret_changes); | |
578 | ||
579 | if (n_changes_detached == 0) | |
580 | return 0; /* Nothing to do */ | |
581 | ||
582 | changes = new0(PortableChange, n_changes_attached + n_changes_detached); | |
583 | if (!changes) | |
584 | return -ENOMEM; | |
585 | ||
8c2d0d3a LP |
586 | CLEANUP_ARRAY(changes, n_changes, portable_changes_free); |
587 | ||
e26fe5f9 LB |
588 | /* Corner case: only detached, nothing attached */ |
589 | if (n_changes_attached == 0) { | |
590 | memcpy(changes, changes_detached, sizeof(PortableChange) * n_changes_detached); | |
591 | *ret_changes = TAKE_PTR(changes); | |
592 | *ret_n_changes = n_changes_detached; | |
e26fe5f9 LB |
593 | return 0; |
594 | } | |
595 | ||
596 | for (size_t i = 0; i < n_changes_detached; ++i) { | |
597 | bool found = false; | |
598 | ||
599 | for (size_t j = 0; j < n_changes_attached; ++j) | |
600 | if (streq(basename(changes_detached[i].path), basename(changes_attached[j].path))) { | |
601 | found = true; | |
602 | break; | |
603 | } | |
604 | ||
605 | if (!found) { | |
606 | _cleanup_free_ char *path = NULL, *source = NULL; | |
607 | ||
608 | path = strdup(changes_detached[i].path); | |
8c2d0d3a LP |
609 | if (!path) |
610 | return -ENOMEM; | |
e26fe5f9 LB |
611 | |
612 | if (changes_detached[i].source) { | |
613 | source = strdup(changes_detached[i].source); | |
8c2d0d3a LP |
614 | if (!source) |
615 | return -ENOMEM; | |
e26fe5f9 LB |
616 | } |
617 | ||
618 | changes[n_changes++] = (PortableChange) { | |
ba5b6c59 | 619 | .type_or_errno = changes_detached[i].type_or_errno, |
e26fe5f9 LB |
620 | .path = TAKE_PTR(path), |
621 | .source = TAKE_PTR(source), | |
622 | }; | |
623 | } | |
624 | } | |
625 | ||
626 | *ret_n_changes = n_changes; | |
627 | *ret_changes = TAKE_PTR(changes); | |
628 | ||
629 | return 0; | |
e26fe5f9 LB |
630 | } |
631 | ||
632 | int bus_image_common_reattach( | |
633 | Manager *m, | |
634 | sd_bus_message *message, | |
635 | const char *name_or_path, | |
636 | Image *image, | |
637 | sd_bus_error *error) { | |
638 | ||
639 | PortableChange *changes_detached = NULL, *changes_attached = NULL, *changes_gone = NULL; | |
640 | size_t n_changes_detached = 0, n_changes_attached = 0, n_changes_gone = 0; | |
907952bb | 641 | _cleanup_strv_free_ char **matches = NULL, **extension_images = NULL; |
e26fe5f9 LB |
642 | PortableFlags flags = PORTABLE_REATTACH; |
643 | const char *profile, *copy_mode; | |
907952bb | 644 | int r; |
e26fe5f9 LB |
645 | |
646 | assert(message); | |
647 | assert(name_or_path || image); | |
648 | ||
f86a4129 DT |
649 | CLEANUP_ARRAY(changes_detached, n_changes_detached, portable_changes_free); |
650 | CLEANUP_ARRAY(changes_attached, n_changes_attached, portable_changes_free); | |
651 | CLEANUP_ARRAY(changes_gone, n_changes_gone, portable_changes_free); | |
652 | ||
e26fe5f9 LB |
653 | if (!m) { |
654 | assert(image); | |
655 | m = image->userdata; | |
656 | } | |
657 | ||
907952bb LB |
658 | if (sd_bus_message_is_method_call(message, NULL, "ReattachImageWithExtensions") || |
659 | sd_bus_message_is_method_call(message, NULL, "ReattachWithExtensions")) { | |
660 | r = sd_bus_message_read_strv(message, &extension_images); | |
661 | if (r < 0) | |
662 | return r; | |
663 | } | |
664 | ||
e26fe5f9 LB |
665 | r = sd_bus_message_read_strv(message, &matches); |
666 | if (r < 0) | |
667 | return r; | |
668 | ||
907952bb | 669 | r = sd_bus_message_read(message, "s", &profile); |
e26fe5f9 LB |
670 | if (r < 0) |
671 | return r; | |
672 | ||
907952bb LB |
673 | if (sd_bus_message_is_method_call(message, NULL, "ReattachImageWithExtensions") || |
674 | sd_bus_message_is_method_call(message, NULL, "ReattachWithExtensions")) { | |
675 | uint64_t input_flags = 0; | |
676 | ||
677 | r = sd_bus_message_read(message, "st", ©_mode, &input_flags); | |
678 | if (r < 0) | |
679 | return r; | |
680 | ||
681 | if ((input_flags & ~_PORTABLE_MASK_PUBLIC) != 0) | |
682 | return sd_bus_reply_method_errorf(message, SD_BUS_ERROR_INVALID_ARGS, | |
683 | "Invalid 'flags' parameter '%" PRIu64 "'", | |
684 | input_flags); | |
685 | flags |= input_flags; | |
686 | } else { | |
687 | int runtime; | |
688 | ||
689 | r = sd_bus_message_read(message, "bs", &runtime, ©_mode); | |
690 | if (r < 0) | |
691 | return r; | |
692 | ||
693 | if (runtime) | |
694 | flags |= PORTABLE_RUNTIME; | |
695 | } | |
696 | ||
e26fe5f9 LB |
697 | if (streq(copy_mode, "symlink")) |
698 | flags |= PORTABLE_PREFER_SYMLINK; | |
699 | else if (streq(copy_mode, "copy")) | |
700 | flags |= PORTABLE_PREFER_COPY; | |
701 | else if (!isempty(copy_mode)) | |
702 | return sd_bus_reply_method_errorf(message, SD_BUS_ERROR_INVALID_ARGS, "Unknown copy mode '%s'", copy_mode); | |
703 | ||
e26fe5f9 LB |
704 | r = bus_image_acquire(m, |
705 | message, | |
706 | name_or_path, | |
707 | image, | |
708 | BUS_IMAGE_AUTHENTICATE_ALL, | |
709 | "org.freedesktop.portable1.attach-images", | |
710 | &image, | |
711 | error); | |
712 | if (r < 0) | |
713 | return r; | |
714 | if (r == 0) /* Will call us back */ | |
715 | return 1; | |
716 | ||
717 | r = portable_detach( | |
718 | sd_bus_message_get_bus(message), | |
719 | image->path, | |
907952bb | 720 | extension_images, |
e26fe5f9 LB |
721 | flags, |
722 | &changes_detached, | |
723 | &n_changes_detached, | |
724 | error); | |
725 | if (r < 0) | |
f86a4129 | 726 | return r; |
e26fe5f9 LB |
727 | |
728 | r = portable_attach( | |
729 | sd_bus_message_get_bus(message), | |
730 | image->path, | |
731 | matches, | |
732 | profile, | |
907952bb | 733 | extension_images, |
84be0c71 | 734 | /* image_policy= */ NULL, |
e26fe5f9 LB |
735 | flags, |
736 | &changes_attached, | |
737 | &n_changes_attached, | |
738 | error); | |
739 | if (r < 0) | |
f86a4129 | 740 | return r; |
e26fe5f9 LB |
741 | |
742 | /* We want to return the list of units really removed by the detach, | |
743 | * and not added again by the attach */ | |
744 | r = normalize_portable_changes(changes_attached, n_changes_attached, | |
745 | changes_detached, n_changes_detached, | |
746 | &changes_gone, &n_changes_gone); | |
747 | if (r < 0) | |
f86a4129 | 748 | return r; |
e26fe5f9 LB |
749 | |
750 | /* First, return the units that are gone (so that the caller can stop them) | |
751 | * Then, return the units that are changed/added (so that the caller can | |
752 | * start/restart/enable them) */ | |
f86a4129 DT |
753 | return reply_portable_changes_pair(message, |
754 | changes_gone, n_changes_gone, | |
755 | changes_attached, n_changes_attached); | |
e26fe5f9 LB |
756 | } |
757 | ||
758 | static int bus_image_method_reattach(sd_bus_message *message, void *userdata, sd_bus_error *error) { | |
759 | return bus_image_common_reattach(NULL, message, NULL, userdata, error); | |
760 | } | |
761 | ||
61d0578b LP |
762 | int bus_image_common_mark_read_only( |
763 | Manager *m, | |
764 | sd_bus_message *message, | |
765 | const char *name_or_path, | |
766 | Image *image, | |
767 | sd_bus_error *error) { | |
768 | ||
769 | int r, read_only; | |
770 | ||
771 | assert(message); | |
772 | assert(name_or_path || image); | |
773 | ||
774 | if (!m) { | |
775 | assert(image); | |
776 | m = image->userdata; | |
777 | } | |
778 | ||
779 | r = sd_bus_message_read(message, "b", &read_only); | |
780 | if (r < 0) | |
781 | return r; | |
782 | ||
783 | r = bus_image_acquire(m, | |
784 | message, | |
785 | name_or_path, | |
786 | image, | |
787 | BUS_IMAGE_AUTHENTICATE_ALL, | |
788 | "org.freedesktop.portable1.manage-images", | |
789 | &image, | |
790 | error); | |
791 | if (r < 0) | |
792 | return r; | |
793 | if (r == 0) | |
794 | return 1; /* Will call us back */ | |
795 | ||
796 | r = image_read_only(image, read_only); | |
797 | if (r < 0) | |
798 | return r; | |
799 | ||
800 | return sd_bus_reply_method_return(message, NULL); | |
801 | } | |
802 | ||
803 | static int bus_image_method_mark_read_only(sd_bus_message *message, void *userdata, sd_bus_error *error) { | |
804 | return bus_image_common_mark_read_only(NULL, message, NULL, userdata, error); | |
805 | } | |
806 | ||
807 | int bus_image_common_set_limit( | |
808 | Manager *m, | |
809 | sd_bus_message *message, | |
810 | const char *name_or_path, | |
811 | Image *image, | |
812 | sd_bus_error *error) { | |
813 | ||
814 | uint64_t limit; | |
815 | int r; | |
816 | ||
817 | assert(message); | |
818 | assert(name_or_path || image); | |
819 | ||
820 | if (!m) { | |
821 | assert(image); | |
822 | m = image->userdata; | |
823 | } | |
824 | ||
825 | r = sd_bus_message_read(message, "t", &limit); | |
826 | if (r < 0) | |
827 | return r; | |
828 | if (!FILE_SIZE_VALID_OR_INFINITY(limit)) | |
1b09b81c | 829 | return sd_bus_error_set(error, SD_BUS_ERROR_INVALID_ARGS, "New limit out of range"); |
61d0578b LP |
830 | |
831 | r = bus_image_acquire(m, | |
832 | message, | |
833 | name_or_path, | |
834 | image, | |
835 | BUS_IMAGE_AUTHENTICATE_ALL, | |
836 | "org.freedesktop.portable1.manage-images", | |
837 | &image, | |
838 | error); | |
839 | if (r < 0) | |
840 | return r; | |
841 | if (r == 0) | |
842 | return 1; /* Will call us back */ | |
843 | ||
844 | r = image_set_limit(image, limit); | |
845 | if (r < 0) | |
846 | return r; | |
847 | ||
848 | return sd_bus_reply_method_return(message, NULL); | |
849 | } | |
850 | ||
851 | static int bus_image_method_set_limit(sd_bus_message *message, void *userdata, sd_bus_error *error) { | |
852 | return bus_image_common_set_limit(NULL, message, NULL, userdata, error); | |
853 | } | |
854 | ||
855 | const sd_bus_vtable image_vtable[] = { | |
856 | SD_BUS_VTABLE_START(0), | |
857 | SD_BUS_PROPERTY("Name", "s", NULL, offsetof(Image, name), 0), | |
858 | SD_BUS_PROPERTY("Path", "s", NULL, offsetof(Image, path), 0), | |
859 | SD_BUS_PROPERTY("Type", "s", property_get_type, offsetof(Image, type), 0), | |
860 | SD_BUS_PROPERTY("ReadOnly", "b", bus_property_get_bool, offsetof(Image, read_only), 0), | |
861 | SD_BUS_PROPERTY("CreationTimestamp", "t", NULL, offsetof(Image, crtime), 0), | |
862 | SD_BUS_PROPERTY("ModificationTimestamp", "t", NULL, offsetof(Image, mtime), 0), | |
863 | SD_BUS_PROPERTY("Usage", "t", NULL, offsetof(Image, usage), 0), | |
864 | SD_BUS_PROPERTY("Limit", "t", NULL, offsetof(Image, limit), 0), | |
865 | SD_BUS_PROPERTY("UsageExclusive", "t", NULL, offsetof(Image, usage_exclusive), 0), | |
866 | SD_BUS_PROPERTY("LimitExclusive", "t", NULL, offsetof(Image, limit_exclusive), 0), | |
598f938e LB |
867 | SD_BUS_METHOD_WITH_ARGS("GetOSRelease", |
868 | SD_BUS_NO_ARGS, | |
869 | SD_BUS_RESULT("a{ss}", os_release), | |
870 | bus_image_method_get_os_release, | |
871 | SD_BUS_VTABLE_UNPRIVILEGED), | |
872 | SD_BUS_METHOD_WITH_ARGS("GetMetadata", | |
873 | SD_BUS_ARGS("as", matches), | |
874 | SD_BUS_RESULT("s", image, | |
875 | "ay", os_release, | |
876 | "a{say}", units), | |
877 | bus_image_method_get_metadata, | |
878 | SD_BUS_VTABLE_UNPRIVILEGED), | |
907952bb LB |
879 | SD_BUS_METHOD_WITH_ARGS("GetMetadataWithExtensions", |
880 | SD_BUS_ARGS("as", extensions, | |
881 | "as", matches, | |
882 | "t", flags), | |
883 | SD_BUS_RESULT("s", image, | |
884 | "ay", os_release, | |
087a799f | 885 | "a{say}", extensions, |
907952bb LB |
886 | "a{say}", units), |
887 | bus_image_method_get_metadata, | |
888 | SD_BUS_VTABLE_UNPRIVILEGED), | |
598f938e LB |
889 | SD_BUS_METHOD_WITH_ARGS("GetState", |
890 | SD_BUS_NO_ARGS, | |
891 | SD_BUS_RESULT("s", state), | |
892 | bus_image_method_get_state, | |
893 | SD_BUS_VTABLE_UNPRIVILEGED), | |
0017415c LB |
894 | SD_BUS_METHOD_WITH_ARGS("GetStateWithExtensions", |
895 | SD_BUS_ARGS("as", extensions, | |
896 | "t", flags), | |
897 | SD_BUS_RESULT("s", state), | |
898 | bus_image_method_get_state, | |
899 | SD_BUS_VTABLE_UNPRIVILEGED), | |
598f938e LB |
900 | SD_BUS_METHOD_WITH_ARGS("Attach", |
901 | SD_BUS_ARGS("as", matches, | |
902 | "s", profile, | |
903 | "b", runtime, | |
904 | "s", copy_mode), | |
905 | SD_BUS_RESULT("a(sss)", changes), | |
906 | bus_image_method_attach, | |
907 | SD_BUS_VTABLE_UNPRIVILEGED), | |
907952bb LB |
908 | SD_BUS_METHOD_WITH_ARGS("AttachWithExtensions", |
909 | SD_BUS_ARGS("as", extensions, | |
910 | "as", matches, | |
911 | "s", profile, | |
912 | "s", copy_mode, | |
913 | "t", flags), | |
914 | SD_BUS_RESULT("a(sss)", changes), | |
915 | bus_image_method_attach, | |
916 | SD_BUS_VTABLE_UNPRIVILEGED), | |
598f938e LB |
917 | SD_BUS_METHOD_WITH_ARGS("Detach", |
918 | SD_BUS_ARGS("b", runtime), | |
919 | SD_BUS_RESULT("a(sss)", changes), | |
920 | bus_image_method_detach, | |
921 | SD_BUS_VTABLE_UNPRIVILEGED), | |
907952bb LB |
922 | SD_BUS_METHOD_WITH_ARGS("DetachWithExtensions", |
923 | SD_BUS_ARGS("as", extensions, | |
924 | "t", flags), | |
925 | SD_BUS_RESULT("a(sss)", changes), | |
926 | bus_image_method_detach, | |
927 | SD_BUS_VTABLE_UNPRIVILEGED), | |
598f938e LB |
928 | SD_BUS_METHOD_WITH_ARGS("Reattach", |
929 | SD_BUS_ARGS("as", matches, | |
930 | "s", profile, | |
931 | "b", runtime, | |
932 | "s", copy_mode), | |
933 | SD_BUS_RESULT("a(sss)", changes_removed, | |
934 | "a(sss)", changes_updated), | |
935 | bus_image_method_reattach, | |
936 | SD_BUS_VTABLE_UNPRIVILEGED), | |
907952bb LB |
937 | SD_BUS_METHOD_WITH_ARGS("ReattacheWithExtensions", |
938 | SD_BUS_ARGS("as", extensions, | |
939 | "as", matches, | |
940 | "s", profile, | |
941 | "s", copy_mode, | |
942 | "t", flags), | |
943 | SD_BUS_RESULT("a(sss)", changes_removed, | |
944 | "a(sss)", changes_updated), | |
945 | bus_image_method_reattach, | |
946 | SD_BUS_VTABLE_UNPRIVILEGED), | |
598f938e LB |
947 | SD_BUS_METHOD_WITH_ARGS("Remove", |
948 | SD_BUS_NO_ARGS, | |
949 | SD_BUS_NO_RESULT, | |
950 | bus_image_method_remove, | |
951 | SD_BUS_VTABLE_UNPRIVILEGED), | |
952 | SD_BUS_METHOD_WITH_ARGS("MarkReadOnly", | |
953 | SD_BUS_ARGS("b", read_only), | |
954 | SD_BUS_NO_RESULT, | |
955 | bus_image_method_mark_read_only, | |
956 | SD_BUS_VTABLE_UNPRIVILEGED), | |
957 | SD_BUS_METHOD_WITH_ARGS("SetLimit", | |
958 | SD_BUS_ARGS("t", limit), | |
959 | SD_BUS_NO_RESULT, | |
960 | bus_image_method_set_limit, | |
961 | SD_BUS_VTABLE_UNPRIVILEGED), | |
61d0578b LP |
962 | SD_BUS_VTABLE_END |
963 | }; | |
964 | ||
965 | int bus_image_path(Image *image, char **ret) { | |
966 | assert(image); | |
967 | assert(ret); | |
968 | ||
969 | if (!image->discoverable) | |
970 | return -EINVAL; | |
971 | ||
972 | return sd_bus_path_encode("/org/freedesktop/portable1/image", image->name, ret); | |
973 | } | |
974 | ||
975 | int bus_image_acquire( | |
976 | Manager *m, | |
977 | sd_bus_message *message, | |
978 | const char *name_or_path, | |
979 | Image *image, | |
980 | ImageAcquireMode mode, | |
981 | const char *polkit_action, | |
982 | Image **ret, | |
983 | sd_bus_error *error) { | |
984 | ||
985 | _cleanup_(image_unrefp) Image *loaded = NULL; | |
986 | Image *cached; | |
987 | int r; | |
988 | ||
989 | assert(m); | |
990 | assert(message); | |
991 | assert(name_or_path || image); | |
992 | assert(mode >= 0); | |
993 | assert(mode < _BUS_IMAGE_ACQUIRE_MODE_MAX); | |
994 | assert(polkit_action || mode == BUS_IMAGE_REFUSE_BY_PATH); | |
995 | assert(ret); | |
996 | ||
997 | /* Acquires an 'Image' object if not acquired yet, and enforces necessary authentication while doing so. */ | |
998 | ||
999 | if (mode == BUS_IMAGE_AUTHENTICATE_ALL) { | |
1000 | r = bus_verify_polkit_async( | |
1001 | message, | |
1002 | CAP_SYS_ADMIN, | |
1003 | polkit_action, | |
1004 | NULL, | |
1005 | false, | |
1006 | UID_INVALID, | |
1007 | &m->polkit_registry, | |
1008 | error); | |
1009 | if (r < 0) | |
1010 | return r; | |
1011 | if (r == 0) { /* Will call us back */ | |
1012 | *ret = NULL; | |
1013 | return 0; | |
1014 | } | |
1015 | } | |
1016 | ||
1017 | /* Already passed in? */ | |
1018 | if (image) { | |
1019 | *ret = image; | |
1020 | return 1; | |
1021 | } | |
1022 | ||
1023 | /* Let's see if this image is already cached? */ | |
1024 | cached = manager_image_cache_get(m, name_or_path); | |
1025 | if (cached) { | |
1026 | *ret = cached; | |
1027 | return 1; | |
1028 | } | |
1029 | ||
1030 | if (image_name_is_valid(name_or_path)) { | |
1031 | ||
1032 | /* If it's a short name, let's search for it */ | |
d577d4a4 | 1033 | r = image_find(IMAGE_PORTABLE, name_or_path, NULL, &loaded); |
61d0578b | 1034 | if (r == -ENOENT) |
5943d85f ZJS |
1035 | return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_PORTABLE_IMAGE, |
1036 | "No image '%s' found.", name_or_path); | |
61d0578b LP |
1037 | |
1038 | /* other errors are handled below… */ | |
1039 | } else { | |
1040 | /* Don't accept path if this is always forbidden */ | |
1041 | if (mode == BUS_IMAGE_REFUSE_BY_PATH) | |
5943d85f ZJS |
1042 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, |
1043 | "Expected image name, not path in place of '%s'.", name_or_path); | |
61d0578b LP |
1044 | |
1045 | if (!path_is_absolute(name_or_path)) | |
5943d85f ZJS |
1046 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, |
1047 | "Image name '%s' is not valid or not a valid path.", name_or_path); | |
61d0578b LP |
1048 | |
1049 | if (!path_is_normalized(name_or_path)) | |
5943d85f ZJS |
1050 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, |
1051 | "Image path '%s' is not normalized.", name_or_path); | |
61d0578b LP |
1052 | |
1053 | if (mode == BUS_IMAGE_AUTHENTICATE_BY_PATH) { | |
1054 | r = bus_verify_polkit_async( | |
1055 | message, | |
1056 | CAP_SYS_ADMIN, | |
1057 | polkit_action, | |
1058 | NULL, | |
1059 | false, | |
1060 | UID_INVALID, | |
1061 | &m->polkit_registry, | |
1062 | error); | |
1063 | if (r < 0) | |
1064 | return r; | |
1065 | if (r == 0) { /* Will call us back */ | |
1066 | *ret = NULL; | |
1067 | return 0; | |
1068 | } | |
1069 | } | |
1070 | ||
1071 | r = image_from_path(name_or_path, &loaded); | |
1072 | } | |
787dfb82 | 1073 | if (r == -EMEDIUMTYPE) { |
5943d85f ZJS |
1074 | sd_bus_error_setf(error, BUS_ERROR_BAD_PORTABLE_IMAGE_TYPE, |
1075 | "Type of image '%s' not recognized; supported image types are directories/btrfs subvolumes, block devices, and raw disk image files with suffix '.raw'.", | |
1076 | name_or_path); | |
787dfb82 LP |
1077 | return r; |
1078 | } | |
61d0578b LP |
1079 | if (r < 0) |
1080 | return r; | |
1081 | ||
1082 | /* Add what we just loaded to the cache. This has as side-effect that the object stays in memory until the | |
1083 | * cache is purged again, i.e. at least for the current event loop iteration, which is all we need, and which | |
1084 | * means we don't actually need to ref the return object. */ | |
1085 | r = manager_image_cache_add(m, loaded); | |
1086 | if (r < 0) | |
1087 | return r; | |
1088 | ||
1089 | *ret = loaded; | |
1090 | return 1; | |
1091 | } | |
1092 | ||
1093 | int bus_image_object_find( | |
1094 | sd_bus *bus, | |
1095 | const char *path, | |
1096 | const char *interface, | |
1097 | void *userdata, | |
1098 | void **found, | |
1099 | sd_bus_error *error) { | |
1100 | ||
1101 | _cleanup_free_ char *e = NULL; | |
1102 | Manager *m = userdata; | |
1103 | Image *image = NULL; | |
1104 | int r; | |
1105 | ||
1106 | assert(bus); | |
1107 | assert(path); | |
1108 | assert(interface); | |
1109 | assert(found); | |
1110 | ||
1111 | r = sd_bus_path_decode(path, "/org/freedesktop/portable1/image", &e); | |
1112 | if (r < 0) | |
1113 | return 0; | |
1114 | if (r == 0) | |
1115 | goto not_found; | |
4313e2b6 ZJS |
1116 | if (isempty(e)) |
1117 | /* The path is "/org/freedesktop/portable1/image" itself */ | |
1118 | goto not_found; | |
61d0578b LP |
1119 | |
1120 | r = bus_image_acquire(m, sd_bus_get_current_message(bus), e, NULL, BUS_IMAGE_REFUSE_BY_PATH, NULL, &image, error); | |
1121 | if (r == -ENOENT) | |
1122 | goto not_found; | |
1123 | if (r < 0) | |
1124 | return r; | |
1125 | ||
1126 | *found = image; | |
1127 | return 1; | |
1128 | ||
1129 | not_found: | |
1130 | *found = NULL; | |
1131 | return 0; | |
1132 | } | |
1133 | ||
1134 | int bus_image_node_enumerator(sd_bus *bus, const char *path, void *userdata, char ***nodes, sd_bus_error *error) { | |
b07ec5a1 | 1135 | _cleanup_hashmap_free_ Hashmap *images = NULL; |
61d0578b | 1136 | _cleanup_strv_free_ char **l = NULL; |
61d0578b | 1137 | Manager *m = userdata; |
319a4f4b | 1138 | size_t n = 0; |
61d0578b | 1139 | Image *image; |
61d0578b LP |
1140 | int r; |
1141 | ||
1142 | assert(bus); | |
1143 | assert(path); | |
1144 | assert(nodes); | |
1145 | ||
b07ec5a1 | 1146 | images = hashmap_new(&image_hash_ops); |
61d0578b LP |
1147 | if (!images) |
1148 | return -ENOMEM; | |
1149 | ||
1150 | r = manager_image_cache_discover(m, images, error); | |
1151 | if (r < 0) | |
1152 | return r; | |
1153 | ||
90e74a66 | 1154 | HASHMAP_FOREACH(image, images) { |
61d0578b LP |
1155 | char *p; |
1156 | ||
1157 | r = bus_image_path(image, &p); | |
1158 | if (r < 0) | |
1159 | return r; | |
1160 | ||
319a4f4b | 1161 | if (!GREEDY_REALLOC(l, n+2)) { |
61d0578b LP |
1162 | free(p); |
1163 | return -ENOMEM; | |
1164 | } | |
1165 | ||
1166 | l[n++] = p; | |
1167 | l[n] = NULL; | |
1168 | } | |
1169 | ||
1170 | *nodes = TAKE_PTR(l); | |
1171 | ||
1172 | return 1; | |
1173 | } | |
3b91bae3 LB |
1174 | |
1175 | const BusObjectImplementation image_object = { | |
1176 | "/org/freedesktop/portable1/image", | |
1177 | "org.freedesktop.portable1.Image", | |
1178 | .fallback_vtables = BUS_FALLBACK_VTABLES({image_vtable, bus_image_object_find}), | |
1179 | .node_enumerator = bus_image_node_enumerator, | |
1180 | }; |