]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/portable/portable.c
update TODO
[thirdparty/systemd.git] / src / portable / portable.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <linux/loop.h>
4
5 #include "bus-common-errors.h"
6 #include "bus-error.h"
7 #include "chase.h"
8 #include "conf-files.h"
9 #include "copy.h"
10 #include "data-fd-util.h"
11 #include "constants.h"
12 #include "dirent-util.h"
13 #include "discover-image.h"
14 #include "dissect-image.h"
15 #include "env-file.h"
16 #include "env-util.h"
17 #include "errno-list.h"
18 #include "escape.h"
19 #include "extension-util.h"
20 #include "fd-util.h"
21 #include "fileio.h"
22 #include "fs-util.h"
23 #include "install.h"
24 #include "io-util.h"
25 #include "locale-util.h"
26 #include "loop-util.h"
27 #include "mkdir.h"
28 #include "nulstr-util.h"
29 #include "os-util.h"
30 #include "path-lookup.h"
31 #include "portable.h"
32 #include "process-util.h"
33 #include "selinux-util.h"
34 #include "set.h"
35 #include "signal-util.h"
36 #include "socket-util.h"
37 #include "sort-util.h"
38 #include "string-table.h"
39 #include "strv.h"
40 #include "tmpfile-util.h"
41 #include "user-util.h"
42
43 /* Markers used in the first line of our 20-portable.conf unit file drop-in to determine, that a) the unit file was
44 * dropped there by the portable service logic and b) for which image it was dropped there. */
45 #define PORTABLE_DROPIN_MARKER_BEGIN "# Drop-in created for image '"
46 #define PORTABLE_DROPIN_MARKER_END "', do not edit."
47
48 static bool prefix_match(const char *unit, const char *prefix) {
49 const char *p;
50
51 p = startswith(unit, prefix);
52 if (!p)
53 return false;
54
55 /* Only respect prefixes followed by dash or dot or when there's a complete match */
56 return IN_SET(*p, '-', '.', '@', 0);
57 }
58
59 static bool unit_match(const char *unit, char **matches) {
60 const char *dot;
61
62 dot = strrchr(unit, '.');
63 if (!dot)
64 return false;
65
66 if (!STR_IN_SET(dot, ".service", ".socket", ".target", ".timer", ".path"))
67 return false;
68
69 /* Empty match expression means: everything */
70 if (strv_isempty(matches))
71 return true;
72
73 /* Otherwise, at least one needs to match */
74 STRV_FOREACH(i, matches)
75 if (prefix_match(unit, *i))
76 return true;
77
78 return false;
79 }
80
81 static PortableMetadata *portable_metadata_new(const char *name, const char *path, const char *selinux_label, int fd) {
82 PortableMetadata *m;
83
84 m = malloc0(offsetof(PortableMetadata, name) + strlen(name) + 1);
85 if (!m)
86 return NULL;
87
88 /* In case of a layered attach, we want to remember which image the unit came from */
89 if (path) {
90 m->image_path = strdup(path);
91 if (!m->image_path)
92 return mfree(m);
93 }
94
95 /* The metadata file might have SELinux labels, we need to carry them and reapply them */
96 if (!isempty(selinux_label)) {
97 m->selinux_label = strdup(selinux_label);
98 if (!m->selinux_label) {
99 free(m->image_path);
100 return mfree(m);
101 }
102 }
103
104 strcpy(m->name, name);
105 m->fd = fd;
106
107 return TAKE_PTR(m);
108 }
109
110 PortableMetadata *portable_metadata_unref(PortableMetadata *i) {
111 if (!i)
112 return NULL;
113
114 safe_close(i->fd);
115 free(i->source);
116 free(i->image_path);
117 free(i->selinux_label);
118
119 return mfree(i);
120 }
121
122 static int compare_metadata(PortableMetadata *const *x, PortableMetadata *const *y) {
123 return strcmp((*x)->name, (*y)->name);
124 }
125
126 int portable_metadata_hashmap_to_sorted_array(Hashmap *unit_files, PortableMetadata ***ret) {
127
128 _cleanup_free_ PortableMetadata **sorted = NULL;
129 PortableMetadata *item;
130 size_t k = 0;
131
132 sorted = new(PortableMetadata*, hashmap_size(unit_files));
133 if (!sorted)
134 return -ENOMEM;
135
136 HASHMAP_FOREACH(item, unit_files)
137 sorted[k++] = item;
138
139 assert(k == hashmap_size(unit_files));
140
141 typesafe_qsort(sorted, k, compare_metadata);
142
143 *ret = TAKE_PTR(sorted);
144 return 0;
145 }
146
147 static int send_one_fd_iov_with_data_fd(
148 int socket_fd,
149 const struct iovec *iov,
150 size_t iovlen,
151 int fd) {
152
153 _cleanup_close_ int data_fd = -EBADF;
154
155 assert(iov || iovlen == 0);
156 assert(socket_fd >= 0);
157 assert(fd >= 0);
158
159 data_fd = copy_data_fd(fd);
160 if (data_fd < 0)
161 return data_fd;
162
163 return send_one_fd_iov(socket_fd, data_fd, iov, iovlen, 0);
164 }
165
166 DEFINE_PRIVATE_HASH_OPS_WITH_VALUE_DESTRUCTOR(portable_metadata_hash_ops, char, string_hash_func, string_compare_func,
167 PortableMetadata, portable_metadata_unref);
168
169 static int extract_now(
170 const char *where,
171 char **matches,
172 const char *image_name,
173 bool path_is_extension,
174 bool relax_extension_release_check,
175 int socket_fd,
176 PortableMetadata **ret_os_release,
177 Hashmap **ret_unit_files) {
178
179 _cleanup_hashmap_free_ Hashmap *unit_files = NULL;
180 _cleanup_(portable_metadata_unrefp) PortableMetadata *os_release = NULL;
181 _cleanup_(lookup_paths_free) LookupPaths paths = {};
182 _cleanup_close_ int os_release_fd = -EBADF;
183 _cleanup_free_ char *os_release_path = NULL;
184 const char *os_release_id;
185 int r;
186
187 /* Extracts the metadata from a directory tree 'where'. Extracts two kinds of information: the /etc/os-release
188 * data, and all unit files matching the specified expression. Note that this function is called in two very
189 * different but also similar contexts. When the tool gets invoked on a directory tree, we'll process it
190 * directly, and in-process, and thus can return the requested data directly, via 'ret_os_release' and
191 * 'ret_unit_files'. However, if the tool is invoked on a raw disk image — which needs to be mounted first — we
192 * are invoked in a child process with private mounts and then need to send the collected data to our
193 * parent. To handle both cases in one call this function also gets a 'socket_fd' parameter, which when >= 0 is
194 * used to send the data to the parent. */
195
196 assert(where);
197
198 /* First, find os-release/extension-release and send it upstream (or just save it). */
199 if (path_is_extension) {
200 os_release_id = strjoina("/usr/lib/extension-release.d/extension-release.", image_name);
201 r = open_extension_release(where, image_name, relax_extension_release_check, &os_release_path, &os_release_fd);
202 } else {
203 os_release_id = "/etc/os-release";
204 r = open_os_release(where, &os_release_path, &os_release_fd);
205 }
206 if (r < 0)
207 log_debug_errno(r,
208 "Couldn't acquire %s file, ignoring: %m",
209 path_is_extension ? "extension-release " : "os-release");
210 else {
211 if (socket_fd >= 0) {
212 struct iovec iov[] = {
213 IOVEC_MAKE_STRING(os_release_id),
214 IOVEC_MAKE((char *)"\0", sizeof(char)),
215 };
216
217 r = send_one_fd_iov_with_data_fd(socket_fd, iov, ELEMENTSOF(iov), os_release_fd);
218 if (r < 0)
219 return log_debug_errno(r, "Failed to send os-release file: %m");
220 }
221
222 if (ret_os_release) {
223 os_release = portable_metadata_new(os_release_id, NULL, NULL, os_release_fd);
224 if (!os_release)
225 return -ENOMEM;
226
227 os_release_fd = -EBADF;
228 os_release->source = TAKE_PTR(os_release_path);
229 }
230 }
231
232 /* Then, send unit file data to the parent (or/and add it to the hashmap). For that we use our usual unit
233 * discovery logic. Note that we force looking inside of /lib/systemd/system/ for units too, as we mightbe
234 * compiled for a split-usr system but the image might be a legacy-usr one. */
235 r = lookup_paths_init(&paths, RUNTIME_SCOPE_SYSTEM, LOOKUP_PATHS_SPLIT_USR, where);
236 if (r < 0)
237 return log_debug_errno(r, "Failed to acquire lookup paths: %m");
238
239 unit_files = hashmap_new(&portable_metadata_hash_ops);
240 if (!unit_files)
241 return -ENOMEM;
242
243 STRV_FOREACH(i, paths.search_path) {
244 _cleanup_free_ char *resolved = NULL;
245 _cleanup_closedir_ DIR *d = NULL;
246
247 r = chase_and_opendir(*i, where, 0, &resolved, &d);
248 if (r < 0) {
249 log_debug_errno(r, "Failed to open unit path '%s', ignoring: %m", *i);
250 continue;
251 }
252
253 FOREACH_DIRENT(de, d, return log_debug_errno(errno, "Failed to read directory: %m")) {
254 _cleanup_(portable_metadata_unrefp) PortableMetadata *m = NULL;
255 _cleanup_(mac_selinux_freep) char *con = NULL;
256 _cleanup_close_ int fd = -EBADF;
257
258 if (!unit_name_is_valid(de->d_name, UNIT_NAME_ANY))
259 continue;
260
261 if (!unit_match(de->d_name, matches))
262 continue;
263
264 /* Filter out duplicates */
265 if (hashmap_get(unit_files, de->d_name))
266 continue;
267
268 if (!IN_SET(de->d_type, DT_LNK, DT_REG))
269 continue;
270
271 fd = openat(dirfd(d), de->d_name, O_CLOEXEC|O_RDONLY);
272 if (fd < 0) {
273 log_debug_errno(errno, "Failed to open unit file '%s', ignoring: %m", de->d_name);
274 continue;
275 }
276
277 #if HAVE_SELINUX
278 /* The units will be copied on the host's filesystem, so if they had a SELinux label
279 * we have to preserve it. Copy it out so that it can be applied later. */
280
281 r = fgetfilecon_raw(fd, &con);
282 if (r < 0 && !ERRNO_IS_XATTR_ABSENT(errno))
283 log_debug_errno(errno, "Failed to get SELinux file context from '%s', ignoring: %m", de->d_name);
284 #endif
285
286 if (socket_fd >= 0) {
287 struct iovec iov[] = {
288 IOVEC_MAKE_STRING(de->d_name),
289 IOVEC_MAKE((char *)"\0", sizeof(char)),
290 IOVEC_MAKE_STRING(strempty(con)),
291 };
292
293 r = send_one_fd_iov_with_data_fd(socket_fd, iov, ELEMENTSOF(iov), fd);
294 if (r < 0)
295 return log_debug_errno(r, "Failed to send unit metadata to parent: %m");
296 }
297
298 m = portable_metadata_new(de->d_name, where, con, fd);
299 if (!m)
300 return -ENOMEM;
301 fd = -EBADF;
302
303 m->source = path_join(resolved, de->d_name);
304 if (!m->source)
305 return -ENOMEM;
306
307 r = hashmap_put(unit_files, m->name, m);
308 if (r < 0)
309 return log_debug_errno(r, "Failed to add unit to hashmap: %m");
310 m = NULL;
311 }
312 }
313
314 if (ret_os_release)
315 *ret_os_release = TAKE_PTR(os_release);
316 if (ret_unit_files)
317 *ret_unit_files = TAKE_PTR(unit_files);
318
319 return 0;
320 }
321
322 static int portable_extract_by_path(
323 const char *path,
324 bool path_is_extension,
325 bool relax_extension_release_check,
326 char **matches,
327 const ImagePolicy *image_policy,
328 PortableMetadata **ret_os_release,
329 Hashmap **ret_unit_files,
330 sd_bus_error *error) {
331
332 _cleanup_hashmap_free_ Hashmap *unit_files = NULL;
333 _cleanup_(portable_metadata_unrefp) PortableMetadata* os_release = NULL;
334 _cleanup_(loop_device_unrefp) LoopDevice *d = NULL;
335 int r;
336
337 assert(path);
338
339 r = loop_device_make_by_path(path, O_RDONLY, /* sector_size= */ UINT32_MAX, LO_FLAGS_PARTSCAN, LOCK_SH, &d);
340 if (r == -EISDIR) {
341 _cleanup_free_ char *image_name = NULL;
342
343 /* We can't turn this into a loop-back block device, and this returns EISDIR? Then this is a directory
344 * tree and not a raw device. It's easy then. */
345
346 r = path_extract_filename(path, &image_name);
347 if (r < 0)
348 return log_error_errno(r, "Failed to extract image name from path '%s': %m", path);
349
350 r = extract_now(path, matches, image_name, path_is_extension, /* relax_extension_release_check= */ false, -1, &os_release, &unit_files);
351 if (r < 0)
352 return r;
353
354 } else if (r < 0)
355 return log_debug_errno(r, "Failed to set up loopback device for %s: %m", path);
356 else {
357 _cleanup_(dissected_image_unrefp) DissectedImage *m = NULL;
358 _cleanup_(rmdir_and_freep) char *tmpdir = NULL;
359 _cleanup_(close_pairp) int seq[2] = PIPE_EBADF;
360 _cleanup_(sigkill_waitp) pid_t child = 0;
361
362 /* We now have a loopback block device, let's fork off a child in its own mount namespace, mount it
363 * there, and extract the metadata we need. The metadata is sent from the child back to us. */
364
365 BLOCK_SIGNALS(SIGCHLD);
366
367 r = mkdtemp_malloc("/tmp/inspect-XXXXXX", &tmpdir);
368 if (r < 0)
369 return log_debug_errno(r, "Failed to create temporary directory: %m");
370
371 r = dissect_loop_device(
372 d,
373 /* verity= */ NULL,
374 /* mount_options= */ NULL,
375 image_policy,
376 DISSECT_IMAGE_READ_ONLY |
377 DISSECT_IMAGE_GENERIC_ROOT |
378 DISSECT_IMAGE_REQUIRE_ROOT |
379 DISSECT_IMAGE_DISCARD_ON_LOOP |
380 DISSECT_IMAGE_RELAX_VAR_CHECK |
381 DISSECT_IMAGE_USR_NO_ROOT |
382 DISSECT_IMAGE_ADD_PARTITION_DEVICES |
383 DISSECT_IMAGE_PIN_PARTITION_DEVICES,
384 &m);
385 if (r == -ENOPKG)
386 sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Couldn't identify a suitable partition table or file system in '%s'.", path);
387 else if (r == -EADDRNOTAVAIL)
388 sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "No root partition for specified root hash found in '%s'.", path);
389 else if (r == -ENOTUNIQ)
390 sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Multiple suitable root partitions found in image '%s'.", path);
391 else if (r == -ENXIO)
392 sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "No suitable root partition found in image '%s'.", path);
393 else if (r == -EPROTONOSUPPORT)
394 sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Device '%s' is loopback block device with partition scanning turned off, please turn it on.", path);
395 if (r < 0)
396 return r;
397
398 if (socketpair(AF_UNIX, SOCK_SEQPACKET|SOCK_CLOEXEC, 0, seq) < 0)
399 return log_debug_errno(errno, "Failed to allocated SOCK_SEQPACKET socket: %m");
400
401 r = safe_fork("(sd-dissect)", FORK_RESET_SIGNALS|FORK_DEATHSIG|FORK_NEW_MOUNTNS|FORK_MOUNTNS_SLAVE|FORK_LOG, &child);
402 if (r < 0)
403 return r;
404 if (r == 0) {
405 DissectImageFlags flags = DISSECT_IMAGE_READ_ONLY;
406
407 seq[0] = safe_close(seq[0]);
408
409 if (path_is_extension)
410 flags |= DISSECT_IMAGE_VALIDATE_OS_EXT | (relax_extension_release_check ? DISSECT_IMAGE_RELAX_SYSEXT_CHECK : 0);
411 else
412 flags |= DISSECT_IMAGE_VALIDATE_OS;
413
414 r = dissected_image_mount(m, tmpdir, UID_INVALID, UID_INVALID, flags);
415 if (r < 0) {
416 log_debug_errno(r, "Failed to mount dissected image: %m");
417 goto child_finish;
418 }
419
420 r = extract_now(tmpdir, matches, m->image_name, path_is_extension, relax_extension_release_check, seq[1], NULL, NULL);
421
422 child_finish:
423 _exit(r < 0 ? EXIT_FAILURE : EXIT_SUCCESS);
424 }
425
426 seq[1] = safe_close(seq[1]);
427
428 unit_files = hashmap_new(&portable_metadata_hash_ops);
429 if (!unit_files)
430 return -ENOMEM;
431
432 for (;;) {
433 _cleanup_(portable_metadata_unrefp) PortableMetadata *add = NULL;
434 _cleanup_close_ int fd = -EBADF;
435 /* We use NAME_MAX space for the SELinux label here. The kernel currently enforces no limit, but
436 * according to suggestions from the SELinux people this will change and it will probably be
437 * identical to NAME_MAX. For now we use that, but this should be updated one day when the final
438 * limit is known. */
439 char iov_buffer[PATH_MAX + NAME_MAX + 2];
440 struct iovec iov = IOVEC_MAKE(iov_buffer, sizeof(iov_buffer));
441
442 ssize_t n = receive_one_fd_iov(seq[0], &iov, 1, 0, &fd);
443 if (n == -EIO)
444 break;
445 if (n < 0)
446 return log_debug_errno(n, "Failed to receive item: %m");
447 iov_buffer[n] = 0;
448
449 /* We can't really distinguish a zero-length datagram without any fds from EOF (both are signalled the
450 * same way by recvmsg()). Hence, accept either as end notification. */
451 if (isempty(iov_buffer) && fd < 0)
452 break;
453
454 if (isempty(iov_buffer) || fd < 0)
455 return log_debug_errno(SYNTHETIC_ERRNO(EINVAL),
456 "Invalid item sent from child.");
457
458 /* Given recvmsg cannot be used with multiple io vectors if you don't know the size in advance,
459 * use a marker to separate the name and the optional SELinux context. */
460 char *selinux_label = memchr(iov_buffer, 0, n);
461 assert(selinux_label);
462 selinux_label++;
463
464 add = portable_metadata_new(iov_buffer, path, selinux_label, fd);
465 if (!add)
466 return -ENOMEM;
467 fd = -EBADF;
468
469 /* Note that we do not initialize 'add->source' here, as the source path is not usable here as
470 * it refers to a path only valid in the short-living namespaced child process we forked
471 * here. */
472
473 if (PORTABLE_METADATA_IS_UNIT(add)) {
474 r = hashmap_put(unit_files, add->name, add);
475 if (r < 0)
476 return log_debug_errno(r, "Failed to add item to unit file list: %m");
477
478 add = NULL;
479
480 } else if (PORTABLE_METADATA_IS_OS_RELEASE(add) || PORTABLE_METADATA_IS_EXTENSION_RELEASE(add)) {
481
482 assert(!os_release);
483 os_release = TAKE_PTR(add);
484 } else
485 assert_not_reached();
486 }
487
488 r = wait_for_terminate_and_check("(sd-dissect)", child, 0);
489 if (r < 0)
490 return r;
491 child = 0;
492 }
493
494 if (!os_release)
495 return sd_bus_error_setf(error,
496 SD_BUS_ERROR_INVALID_ARGS,
497 "Image '%s' lacks %s data, refusing.",
498 path,
499 path_is_extension ? "extension-release" : "os-release");
500
501 if (ret_unit_files)
502 *ret_unit_files = TAKE_PTR(unit_files);
503
504 if (ret_os_release)
505 *ret_os_release = TAKE_PTR(os_release);
506
507 return 0;
508 }
509
510 static int extract_image_and_extensions(
511 const char *name_or_path,
512 char **matches,
513 char **extension_image_paths,
514 bool validate_sysext,
515 bool relax_extension_release_check,
516 const ImagePolicy *image_policy,
517 Image **ret_image,
518 OrderedHashmap **ret_extension_images,
519 OrderedHashmap **ret_extension_releases,
520 PortableMetadata **ret_os_release,
521 Hashmap **ret_unit_files,
522 char ***ret_valid_prefixes,
523 sd_bus_error *error) {
524
525 _cleanup_free_ char *id = NULL, *version_id = NULL, *sysext_level = NULL;
526 _cleanup_(portable_metadata_unrefp) PortableMetadata *os_release = NULL;
527 _cleanup_ordered_hashmap_free_ OrderedHashmap *extension_images = NULL, *extension_releases = NULL;
528 _cleanup_hashmap_free_ Hashmap *unit_files = NULL;
529 _cleanup_strv_free_ char **valid_prefixes = NULL;
530 _cleanup_(image_unrefp) Image *image = NULL;
531 Image *ext;
532 int r;
533
534 assert(name_or_path);
535
536 r = image_find_harder(IMAGE_PORTABLE, name_or_path, NULL, &image);
537 if (r < 0)
538 return r;
539
540 if (!strv_isempty(extension_image_paths)) {
541 extension_images = ordered_hashmap_new(&image_hash_ops);
542 if (!extension_images)
543 return -ENOMEM;
544
545 if (ret_extension_releases) {
546 extension_releases = ordered_hashmap_new(&portable_metadata_hash_ops);
547 if (!extension_releases)
548 return -ENOMEM;
549 }
550
551 STRV_FOREACH(p, extension_image_paths) {
552 _cleanup_(image_unrefp) Image *new = NULL;
553
554 r = image_find_harder(IMAGE_PORTABLE, *p, NULL, &new);
555 if (r < 0)
556 return r;
557
558 r = ordered_hashmap_put(extension_images, new->name, new);
559 if (r < 0)
560 return r;
561 TAKE_PTR(new);
562 }
563 }
564
565 r = portable_extract_by_path(
566 image->path,
567 /* path_is_extension= */ false,
568 /* relax_extension_release_check= */ false,
569 matches,
570 image_policy,
571 &os_release,
572 &unit_files,
573 error);
574 if (r < 0)
575 return r;
576
577 /* If we are layering extension images on top of a runtime image, check that the os-release and
578 * extension-release metadata match, otherwise reject it immediately as invalid, or it will fail when
579 * the units are started. Also, collect valid portable prefixes if caller requested that. */
580 if (validate_sysext || ret_valid_prefixes) {
581 _cleanup_free_ char *prefixes = NULL;
582
583 r = parse_env_file_fd(os_release->fd, os_release->name,
584 "ID", &id,
585 "VERSION_ID", &version_id,
586 "SYSEXT_LEVEL", &sysext_level,
587 "PORTABLE_PREFIXES", &prefixes);
588 if (r < 0)
589 return r;
590 if (isempty(id))
591 return sd_bus_error_set_errnof(error, SYNTHETIC_ERRNO(ESTALE), "Image %s os-release metadata lacks the ID field", name_or_path);
592
593 if (prefixes) {
594 valid_prefixes = strv_split(prefixes, WHITESPACE);
595 if (!valid_prefixes)
596 return -ENOMEM;
597 }
598 }
599
600 ORDERED_HASHMAP_FOREACH(ext, extension_images) {
601 _cleanup_(portable_metadata_unrefp) PortableMetadata *extension_release_meta = NULL;
602 _cleanup_hashmap_free_ Hashmap *extra_unit_files = NULL;
603 _cleanup_strv_free_ char **extension_release = NULL;
604 const char *e;
605
606 r = portable_extract_by_path(
607 ext->path,
608 /* path_is_extension= */ true,
609 relax_extension_release_check,
610 matches,
611 image_policy,
612 &extension_release_meta,
613 &extra_unit_files,
614 error);
615 if (r < 0)
616 return r;
617
618 r = hashmap_move(unit_files, extra_unit_files);
619 if (r < 0)
620 return r;
621
622 if (!validate_sysext && !ret_valid_prefixes && !ret_extension_releases)
623 continue;
624
625 r = load_env_file_pairs_fd(extension_release_meta->fd, extension_release_meta->name, &extension_release);
626 if (r < 0)
627 return r;
628
629 if (validate_sysext) {
630 r = extension_release_validate(ext->path, id, version_id, sysext_level, "portable", extension_release);
631 if (r == 0)
632 return sd_bus_error_set_errnof(error, SYNTHETIC_ERRNO(ESTALE), "Image %s extension-release metadata does not match the root's", ext->path);
633 if (r < 0)
634 return sd_bus_error_set_errnof(error, r, "Failed to compare image %s extension-release metadata with the root's os-release: %m", ext->path);
635 }
636
637 e = strv_env_pairs_get(extension_release, "PORTABLE_PREFIXES");
638 if (e) {
639 _cleanup_strv_free_ char **l = NULL;
640
641 l = strv_split(e, WHITESPACE);
642 if (!l)
643 return -ENOMEM;
644
645 r = strv_extend_strv(&valid_prefixes, l, true);
646 if (r < 0)
647 return r;
648 }
649
650 if (ret_extension_releases) {
651 r = ordered_hashmap_put(extension_releases, ext->name, extension_release_meta);
652 if (r < 0)
653 return r;
654 TAKE_PTR(extension_release_meta);
655 }
656 }
657
658 strv_sort(valid_prefixes);
659
660 if (ret_image)
661 *ret_image = TAKE_PTR(image);
662 if (ret_extension_images)
663 *ret_extension_images = TAKE_PTR(extension_images);
664 if (ret_extension_releases)
665 *ret_extension_releases = TAKE_PTR(extension_releases);
666 if (ret_os_release)
667 *ret_os_release = TAKE_PTR(os_release);
668 if (ret_unit_files)
669 *ret_unit_files = TAKE_PTR(unit_files);
670 if (ret_valid_prefixes)
671 *ret_valid_prefixes = TAKE_PTR(valid_prefixes);
672
673 return 0;
674 }
675
676 int portable_extract(
677 const char *name_or_path,
678 char **matches,
679 char **extension_image_paths,
680 const ImagePolicy *image_policy,
681 PortableFlags flags,
682 PortableMetadata **ret_os_release,
683 OrderedHashmap **ret_extension_releases,
684 Hashmap **ret_unit_files,
685 char ***ret_valid_prefixes,
686 sd_bus_error *error) {
687
688 _cleanup_(portable_metadata_unrefp) PortableMetadata *os_release = NULL;
689 _cleanup_ordered_hashmap_free_ OrderedHashmap *extension_images = NULL, *extension_releases = NULL;
690 _cleanup_hashmap_free_ Hashmap *unit_files = NULL;
691 _cleanup_(strv_freep) char **valid_prefixes = NULL;
692 _cleanup_(image_unrefp) Image *image = NULL;
693 int r;
694
695 assert(name_or_path);
696
697 r = extract_image_and_extensions(
698 name_or_path,
699 matches,
700 extension_image_paths,
701 /* validate_sysext= */ false,
702 /* relax_extension_release_check= */ FLAGS_SET(flags, PORTABLE_FORCE_SYSEXT),
703 image_policy,
704 &image,
705 &extension_images,
706 &extension_releases,
707 &os_release,
708 &unit_files,
709 ret_valid_prefixes ? &valid_prefixes : NULL,
710 error);
711 if (r < 0)
712 return r;
713
714 if (hashmap_isempty(unit_files)) {
715 _cleanup_free_ char *extensions = strv_join(extension_image_paths, ", ");
716 if (!extensions)
717 return -ENOMEM;
718
719 return sd_bus_error_setf(error,
720 SD_BUS_ERROR_INVALID_ARGS,
721 "Couldn't find any matching unit files in image '%s%s%s', refusing.",
722 image->path,
723 isempty(extensions) ? "" : "' or any of its extensions '",
724 isempty(extensions) ? "" : extensions);
725 }
726
727 if (ret_os_release)
728 *ret_os_release = TAKE_PTR(os_release);
729 if (ret_extension_releases)
730 *ret_extension_releases = TAKE_PTR(extension_releases);
731 if (ret_unit_files)
732 *ret_unit_files = TAKE_PTR(unit_files);
733 if (ret_valid_prefixes)
734 *ret_valid_prefixes = TAKE_PTR(valid_prefixes);
735
736 return 0;
737 }
738
739 static int unit_file_is_active(
740 sd_bus *bus,
741 const char *name,
742 sd_bus_error *error) {
743
744 static const char *const active_states[] = {
745 "activating",
746 "active",
747 "reloading",
748 "deactivating",
749 NULL,
750 };
751 int r;
752
753 if (!bus)
754 return false;
755
756 /* If we are looking at a plain or instance things are easy, we can just query the state */
757 if (unit_name_is_valid(name, UNIT_NAME_PLAIN|UNIT_NAME_INSTANCE)) {
758 _cleanup_free_ char *path = NULL, *buf = NULL;
759
760 path = unit_dbus_path_from_name(name);
761 if (!path)
762 return -ENOMEM;
763
764 r = sd_bus_get_property_string(
765 bus,
766 "org.freedesktop.systemd1",
767 path,
768 "org.freedesktop.systemd1.Unit",
769 "ActiveState",
770 error,
771 &buf);
772 if (r < 0)
773 return log_debug_errno(r, "Failed to retrieve unit state: %s", bus_error_message(error, r));
774
775 return strv_contains((char**) active_states, buf);
776 }
777
778 /* Otherwise we need to enumerate. But let's build the most restricted query we can */
779 if (unit_name_is_valid(name, UNIT_NAME_TEMPLATE)) {
780 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL, *reply = NULL;
781 const char *at, *prefix, *joined;
782
783 r = sd_bus_message_new_method_call(
784 bus,
785 &m,
786 "org.freedesktop.systemd1",
787 "/org/freedesktop/systemd1",
788 "org.freedesktop.systemd1.Manager",
789 "ListUnitsByPatterns");
790 if (r < 0)
791 return r;
792
793 r = sd_bus_message_append_strv(m, (char**) active_states);
794 if (r < 0)
795 return r;
796
797 at = strchr(name, '@');
798 assert(at);
799
800 prefix = strndupa_safe(name, at + 1 - name);
801 joined = strjoina(prefix, "*", at + 1);
802
803 r = sd_bus_message_append_strv(m, STRV_MAKE(joined));
804 if (r < 0)
805 return r;
806
807 r = sd_bus_call(bus, m, 0, error, &reply);
808 if (r < 0)
809 return log_debug_errno(r, "Failed to list units: %s", bus_error_message(error, r));
810
811 r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_ARRAY, "(ssssssouso)");
812 if (r < 0)
813 return r;
814
815 r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_STRUCT, "ssssssouso");
816 if (r < 0)
817 return r;
818
819 return r > 0;
820 }
821
822 return -EINVAL;
823 }
824
825 static int portable_changes_add(
826 PortableChange **changes,
827 size_t *n_changes,
828 int type_or_errno, /* PORTABLE_COPY, PORTABLE_SYMLINK, … if positive, or errno if negative */
829 const char *path,
830 const char *source) {
831
832 _cleanup_free_ char *p = NULL, *s = NULL;
833 PortableChange *c;
834
835 assert(path);
836 assert(!changes == !n_changes);
837
838 if (type_or_errno >= 0)
839 assert(type_or_errno < _PORTABLE_CHANGE_TYPE_MAX);
840 else
841 assert(type_or_errno >= -ERRNO_MAX);
842
843 if (!changes)
844 return 0;
845
846 c = reallocarray(*changes, *n_changes + 1, sizeof(PortableChange));
847 if (!c)
848 return -ENOMEM;
849 *changes = c;
850
851 p = strdup(path);
852 if (!p)
853 return -ENOMEM;
854
855 path_simplify(p);
856
857 if (source) {
858 s = strdup(source);
859 if (!s)
860 return -ENOMEM;
861
862 path_simplify(s);
863 }
864
865 c[(*n_changes)++] = (PortableChange) {
866 .type_or_errno = type_or_errno,
867 .path = TAKE_PTR(p),
868 .source = TAKE_PTR(s),
869 };
870
871 return 0;
872 }
873
874 static int portable_changes_add_with_prefix(
875 PortableChange **changes,
876 size_t *n_changes,
877 int type_or_errno,
878 const char *prefix,
879 const char *path,
880 const char *source) {
881
882 _cleanup_free_ char *path_buf = NULL, *source_buf = NULL;
883
884 assert(path);
885 assert(!changes == !n_changes);
886
887 if (!changes)
888 return 0;
889
890 if (prefix) {
891 path_buf = path_join(prefix, path);
892 if (!path_buf)
893 return -ENOMEM;
894
895 path = path_buf;
896
897 if (source) {
898 source_buf = path_join(prefix, source);
899 if (!source_buf)
900 return -ENOMEM;
901
902 source = source_buf;
903 }
904 }
905
906 return portable_changes_add(changes, n_changes, type_or_errno, path, source);
907 }
908
909 void portable_changes_free(PortableChange *changes, size_t n_changes) {
910 size_t i;
911
912 assert(changes || n_changes == 0);
913
914 for (i = 0; i < n_changes; i++) {
915 free(changes[i].path);
916 free(changes[i].source);
917 }
918
919 free(changes);
920 }
921
922 static const char *root_setting_from_image(ImageType type) {
923 return IN_SET(type, IMAGE_DIRECTORY, IMAGE_SUBVOLUME) ? "RootDirectory=" : "RootImage=";
924 }
925
926 static const char *extension_setting_from_image(ImageType type) {
927 return IN_SET(type, IMAGE_DIRECTORY, IMAGE_SUBVOLUME) ? "ExtensionDirectories=" : "ExtensionImages=";
928 }
929
930 static int make_marker_text(const char *image_path, OrderedHashmap *extension_images, char **ret_text) {
931 _cleanup_free_ char *text = NULL, *escaped_image_path = NULL;
932 Image *ext;
933
934 assert(image_path);
935 assert(ret_text);
936
937 escaped_image_path = xescape(image_path, ":");
938 if (!escaped_image_path)
939 return -ENOMEM;
940
941 /* If the image is layered, include all layers in the marker as a colon-separated
942 * list of paths, so that we can do exact matches on removal. */
943 text = strjoin(PORTABLE_DROPIN_MARKER_BEGIN, escaped_image_path);
944 if (!text)
945 return -ENOMEM;
946
947 ORDERED_HASHMAP_FOREACH(ext, extension_images) {
948 _cleanup_free_ char *escaped = NULL;
949
950 escaped = xescape(ext->path, ":");
951 if (!escaped)
952 return -ENOMEM;
953
954 if (!strextend(&text, ":", escaped))
955 return -ENOMEM;
956 }
957
958 if (!strextend(&text, PORTABLE_DROPIN_MARKER_END "\n"))
959 return -ENOMEM;
960
961 *ret_text = TAKE_PTR(text);
962 return 0;
963 }
964
965 static int append_release_log_fields(
966 char **text,
967 const PortableMetadata *release,
968 ImageClass type,
969 const char *field_name) {
970
971 static const char *const field_versions[_IMAGE_CLASS_MAX][4]= {
972 [IMAGE_PORTABLE] = { "IMAGE_VERSION", "VERSION_ID", "BUILD_ID", NULL },
973 [IMAGE_EXTENSION] = { "SYSEXT_IMAGE_VERSION", "SYSEXT_VERSION_ID", "SYSEXT_BUILD_ID", NULL },
974 };
975 static const char *const field_ids[_IMAGE_CLASS_MAX][3]= {
976 [IMAGE_PORTABLE] = { "IMAGE_ID", "ID", NULL },
977 [IMAGE_EXTENSION] = { "SYSEXT_IMAGE_ID", "SYSEXT_ID", NULL },
978 };
979 _cleanup_strv_free_ char **fields = NULL;
980 const char *id = NULL, *version = NULL;
981 int r;
982
983 assert(IN_SET(type, IMAGE_PORTABLE, IMAGE_EXTENSION));
984 assert(!strv_isempty((char *const *)field_ids[type]));
985 assert(!strv_isempty((char *const *)field_versions[type]));
986 assert(field_name);
987 assert(text);
988
989 if (!release)
990 return 0; /* Nothing to do. */
991
992 r = load_env_file_pairs_fd(release->fd, release->name, &fields);
993 if (r < 0)
994 return log_debug_errno(r, "Failed to parse '%s': %m", release->name);
995
996 /* Find an ID first, in order of preference from more specific to less specific: IMAGE_ID -> ID */
997 id = strv_find_first_field((char *const *)field_ids[type], fields);
998
999 /* Then the version, same logic, prefer the more specific one */
1000 version = strv_find_first_field((char *const *)field_versions[type], fields);
1001
1002 /* If there's no valid version to be found, simply omit it. */
1003 if (!id && !version)
1004 return 0;
1005
1006 if (!strextend(text,
1007 "LogExtraFields=",
1008 field_name,
1009 "=",
1010 strempty(id),
1011 id && version ? "_" : "",
1012 strempty(version),
1013 "\n"))
1014 return -ENOMEM;
1015
1016 return 0;
1017 }
1018
1019 static int install_chroot_dropin(
1020 const char *image_path,
1021 ImageType type,
1022 OrderedHashmap *extension_images,
1023 OrderedHashmap *extension_releases,
1024 const PortableMetadata *m,
1025 const PortableMetadata *os_release,
1026 const char *dropin_dir,
1027 PortableFlags flags,
1028 char **ret_dropin,
1029 PortableChange **changes,
1030 size_t *n_changes) {
1031
1032 _cleanup_free_ char *text = NULL, *dropin = NULL;
1033 int r;
1034
1035 assert(image_path);
1036 assert(m);
1037 assert(dropin_dir);
1038
1039 dropin = path_join(dropin_dir, "20-portable.conf");
1040 if (!dropin)
1041 return -ENOMEM;
1042
1043 r = make_marker_text(image_path, extension_images, &text);
1044 if (r < 0)
1045 return log_debug_errno(r, "Failed to generate marker string for portable drop-in: %m");
1046
1047 if (endswith(m->name, ".service")) {
1048 const char *os_release_source, *root_type;
1049 _cleanup_free_ char *base_name = NULL;
1050 Image *ext;
1051
1052 root_type = root_setting_from_image(type);
1053
1054 if (access("/etc/os-release", F_OK) < 0) {
1055 if (errno != ENOENT)
1056 return log_debug_errno(errno, "Failed to check if /etc/os-release exists: %m");
1057
1058 os_release_source = "/usr/lib/os-release";
1059 } else
1060 os_release_source = "/etc/os-release";
1061
1062 r = path_extract_filename(m->image_path ?: image_path, &base_name);
1063 if (r < 0)
1064 return log_debug_errno(r, "Failed to extract basename from '%s': %m", m->image_path ?: image_path);
1065
1066 if (!strextend(&text,
1067 "\n"
1068 "[Service]\n",
1069 root_type, image_path, "\n"
1070 "Environment=PORTABLE=", base_name, "\n"
1071 "BindReadOnlyPaths=", os_release_source, ":/run/host/os-release\n"
1072 "LogExtraFields=PORTABLE=", base_name, "\n"))
1073 return -ENOMEM;
1074
1075 /* If we have a single image then PORTABLE= will point to it, so we add
1076 * PORTABLE_NAME_AND_VERSION= with the os-release fields and we are done. But if we have
1077 * extensions, PORTABLE= will point to the image where the current unit was found in. So we
1078 * also list PORTABLE_ROOT= and PORTABLE_ROOT_NAME_AND_VERSION= for the base image, and
1079 * PORTABLE_EXTENSION= and PORTABLE_EXTENSION_NAME_AND_VERSION= for each extension, so that
1080 * all needed metadata is available. */
1081 if (ordered_hashmap_isempty(extension_images))
1082 r = append_release_log_fields(&text, os_release, IMAGE_PORTABLE, "PORTABLE_NAME_AND_VERSION");
1083 else {
1084 _cleanup_free_ char *root_base_name = NULL;
1085
1086 r = path_extract_filename(image_path, &root_base_name);
1087 if (r < 0)
1088 return log_debug_errno(r, "Failed to extract basename from '%s': %m", image_path);
1089
1090 if (!strextend(&text,
1091 "Environment=PORTABLE_ROOT=", root_base_name, "\n",
1092 "LogExtraFields=PORTABLE_ROOT=", root_base_name, "\n"))
1093 return -ENOMEM;
1094
1095 r = append_release_log_fields(&text, os_release, IMAGE_PORTABLE, "PORTABLE_ROOT_NAME_AND_VERSION");
1096 }
1097 if (r < 0)
1098 return r;
1099
1100 if (m->image_path && !path_equal(m->image_path, image_path))
1101 ORDERED_HASHMAP_FOREACH(ext, extension_images) {
1102 _cleanup_free_ char *extension_base_name = NULL;
1103
1104 r = path_extract_filename(ext->path, &extension_base_name);
1105 if (r < 0)
1106 return log_debug_errno(r, "Failed to extract basename from '%s': %m", ext->path);
1107
1108 if (!strextend(&text,
1109 "\n",
1110 extension_setting_from_image(ext->type),
1111 ext->path,
1112 /* With --force tell PID1 to avoid enforcing that the image <name> and
1113 * extension-release.<name> have to match. */
1114 !IN_SET(type, IMAGE_DIRECTORY, IMAGE_SUBVOLUME) &&
1115 FLAGS_SET(flags, PORTABLE_FORCE_SYSEXT) ?
1116 ":x-systemd.relax-extension-release-check\n" :
1117 "\n",
1118 /* In PORTABLE= we list the 'main' image name for this unit
1119 * (the image where the unit was extracted from), but we are
1120 * stacking multiple images, so list those too. */
1121 "LogExtraFields=PORTABLE_EXTENSION=", extension_base_name, "\n"))
1122 return -ENOMEM;
1123
1124 /* Look for image/version identifiers in the extension release files. We
1125 * look for all possible IDs, but typically only 1 or 2 will be set, so
1126 * the number of fields added shouldn't be too large. We prefix the DDI
1127 * name to the value, so that we can add the same field multiple times and
1128 * still be able to identify what applies to what. */
1129 r = append_release_log_fields(&text,
1130 ordered_hashmap_get(extension_releases, ext->name),
1131 IMAGE_EXTENSION,
1132 "PORTABLE_EXTENSION_NAME_AND_VERSION");
1133 if (r < 0)
1134 return r;
1135 }
1136 }
1137
1138 r = write_string_file(dropin, text, WRITE_STRING_FILE_CREATE|WRITE_STRING_FILE_ATOMIC);
1139 if (r < 0)
1140 return log_debug_errno(r, "Failed to write '%s': %m", dropin);
1141
1142 (void) portable_changes_add(changes, n_changes, PORTABLE_WRITE, dropin, NULL);
1143
1144 if (ret_dropin)
1145 *ret_dropin = TAKE_PTR(dropin);
1146
1147 return 0;
1148 }
1149
1150 static int install_profile_dropin(
1151 const char *image_path,
1152 const PortableMetadata *m,
1153 const char *dropin_dir,
1154 const char *profile,
1155 PortableFlags flags,
1156 char **ret_dropin,
1157 PortableChange **changes,
1158 size_t *n_changes) {
1159
1160 _cleanup_free_ char *dropin = NULL, *from = NULL;
1161 int r;
1162
1163 assert(image_path);
1164 assert(m);
1165 assert(dropin_dir);
1166
1167 if (!profile)
1168 return 0;
1169
1170 r = find_portable_profile(profile, m->name, &from);
1171 if (r < 0) {
1172 if (r != -ENOENT)
1173 return log_debug_errno(errno, "Profile '%s' is not accessible: %m", profile);
1174
1175 log_debug_errno(errno, "Skipping link to profile '%s', as it does not exist: %m", profile);
1176 return 0;
1177 }
1178
1179 dropin = path_join(dropin_dir, "10-profile.conf");
1180 if (!dropin)
1181 return -ENOMEM;
1182
1183 if (flags & PORTABLE_PREFER_COPY) {
1184
1185 r = copy_file_atomic(from, dropin, 0644, COPY_REFLINK);
1186 if (r < 0)
1187 return log_debug_errno(r, "Failed to copy %s %s %s: %m", from, special_glyph(SPECIAL_GLYPH_ARROW_RIGHT), dropin);
1188
1189 (void) portable_changes_add(changes, n_changes, PORTABLE_COPY, dropin, from);
1190
1191 } else {
1192
1193 if (symlink(from, dropin) < 0)
1194 return log_debug_errno(errno, "Failed to link %s %s %s: %m", from, special_glyph(SPECIAL_GLYPH_ARROW_RIGHT), dropin);
1195
1196 (void) portable_changes_add(changes, n_changes, PORTABLE_SYMLINK, dropin, from);
1197 }
1198
1199 if (ret_dropin)
1200 *ret_dropin = TAKE_PTR(dropin);
1201
1202 return 0;
1203 }
1204
1205 static const char *attached_path(const LookupPaths *paths, PortableFlags flags) {
1206 const char *where;
1207
1208 assert(paths);
1209
1210 if (flags & PORTABLE_RUNTIME)
1211 where = paths->runtime_attached;
1212 else
1213 where = paths->persistent_attached;
1214
1215 assert(where);
1216 return where;
1217 }
1218
1219 static int attach_unit_file(
1220 const LookupPaths *paths,
1221 const char *image_path,
1222 ImageType type,
1223 OrderedHashmap *extension_images,
1224 OrderedHashmap *extension_releases,
1225 const PortableMetadata *m,
1226 const PortableMetadata *os_release,
1227 const char *profile,
1228 PortableFlags flags,
1229 PortableChange **changes,
1230 size_t *n_changes) {
1231
1232 _cleanup_(unlink_and_freep) char *chroot_dropin = NULL, *profile_dropin = NULL;
1233 _cleanup_(rmdir_and_freep) char *dropin_dir = NULL;
1234 _cleanup_free_ char *path = NULL;
1235 const char *where;
1236 int r;
1237
1238 assert(paths);
1239 assert(image_path);
1240 assert(m);
1241 assert(PORTABLE_METADATA_IS_UNIT(m));
1242
1243 where = attached_path(paths, flags);
1244
1245 (void) mkdir_parents(where, 0755);
1246 if (mkdir(where, 0755) < 0) {
1247 if (errno != EEXIST)
1248 return log_debug_errno(errno, "Failed to create attach directory %s: %m", where);
1249 } else
1250 (void) portable_changes_add(changes, n_changes, PORTABLE_MKDIR, where, NULL);
1251
1252 path = path_join(where, m->name);
1253 if (!path)
1254 return -ENOMEM;
1255
1256 dropin_dir = strjoin(path, ".d");
1257 if (!dropin_dir)
1258 return -ENOMEM;
1259
1260 if (mkdir(dropin_dir, 0755) < 0) {
1261 if (errno != EEXIST)
1262 return log_debug_errno(errno, "Failed to create drop-in directory %s: %m", dropin_dir);
1263 } else
1264 (void) portable_changes_add(changes, n_changes, PORTABLE_MKDIR, dropin_dir, NULL);
1265
1266 /* We install the drop-ins first, and the actual unit file last to achieve somewhat atomic behaviour if PID 1
1267 * is reloaded while we are creating things here: as long as only the drop-ins exist the unit doesn't exist at
1268 * all for PID 1. */
1269
1270 r = install_chroot_dropin(image_path, type, extension_images, extension_releases, m, os_release, dropin_dir, flags, &chroot_dropin, changes, n_changes);
1271 if (r < 0)
1272 return r;
1273
1274 r = install_profile_dropin(image_path, m, dropin_dir, profile, flags, &profile_dropin, changes, n_changes);
1275 if (r < 0)
1276 return r;
1277
1278 if ((flags & PORTABLE_PREFER_SYMLINK) && m->source) {
1279
1280 if (symlink(m->source, path) < 0)
1281 return log_debug_errno(errno, "Failed to symlink unit file '%s': %m", path);
1282
1283 (void) portable_changes_add(changes, n_changes, PORTABLE_SYMLINK, path, m->source);
1284
1285 } else {
1286 _cleanup_(unlink_and_freep) char *tmp = NULL;
1287 _cleanup_close_ int fd = -EBADF;
1288
1289 (void) mac_selinux_create_file_prepare_label(path, m->selinux_label);
1290
1291 fd = open_tmpfile_linkable(path, O_WRONLY|O_CLOEXEC, &tmp);
1292 mac_selinux_create_file_clear(); /* Clear immediately in case of errors */
1293 if (fd < 0)
1294 return log_debug_errno(fd, "Failed to create unit file '%s': %m", path);
1295
1296 r = copy_bytes(m->fd, fd, UINT64_MAX, COPY_REFLINK);
1297 if (r < 0)
1298 return log_debug_errno(r, "Failed to copy unit file '%s': %m", path);
1299
1300 if (fchmod(fd, 0644) < 0)
1301 return log_debug_errno(errno, "Failed to change unit file access mode for '%s': %m", path);
1302
1303 r = link_tmpfile(fd, tmp, path, /* replace= */ false);
1304 if (r < 0)
1305 return log_debug_errno(r, "Failed to install unit file '%s': %m", path);
1306
1307 tmp = mfree(tmp);
1308
1309 (void) portable_changes_add(changes, n_changes, PORTABLE_COPY, path, m->source);
1310 }
1311
1312 /* All is established now, now let's disable any rollbacks */
1313 chroot_dropin = mfree(chroot_dropin);
1314 profile_dropin = mfree(profile_dropin);
1315 dropin_dir = mfree(dropin_dir);
1316
1317 return 0;
1318 }
1319
1320 static int image_symlink(
1321 const char *image_path,
1322 PortableFlags flags,
1323 char **ret) {
1324
1325 const char *fn, *where;
1326 char *joined = NULL;
1327
1328 assert(image_path);
1329 assert(ret);
1330
1331 fn = last_path_component(image_path);
1332
1333 if (flags & PORTABLE_RUNTIME)
1334 where = "/run/portables/";
1335 else
1336 where = "/etc/portables/";
1337
1338 joined = strjoin(where, fn);
1339 if (!joined)
1340 return -ENOMEM;
1341
1342 *ret = joined;
1343 return 0;
1344 }
1345
1346 static int install_image_symlink(
1347 const char *image_path,
1348 PortableFlags flags,
1349 PortableChange **changes,
1350 size_t *n_changes) {
1351
1352 _cleanup_free_ char *sl = NULL;
1353 int r;
1354
1355 assert(image_path);
1356
1357 /* If the image is outside of the image search also link it into it, so that it can be found with short image
1358 * names and is listed among the images. */
1359
1360 if (image_in_search_path(IMAGE_PORTABLE, NULL, image_path))
1361 return 0;
1362
1363 r = image_symlink(image_path, flags, &sl);
1364 if (r < 0)
1365 return log_debug_errno(r, "Failed to generate image symlink path: %m");
1366
1367 (void) mkdir_parents(sl, 0755);
1368
1369 if (symlink(image_path, sl) < 0)
1370 return log_debug_errno(errno, "Failed to link %s %s %s: %m", image_path, special_glyph(SPECIAL_GLYPH_ARROW_RIGHT), sl);
1371
1372 (void) portable_changes_add(changes, n_changes, PORTABLE_SYMLINK, sl, image_path);
1373 return 0;
1374 }
1375
1376 static int install_image_and_extensions_symlinks(
1377 const Image *image,
1378 OrderedHashmap *extension_images,
1379 PortableFlags flags,
1380 PortableChange **changes,
1381 size_t *n_changes) {
1382
1383 Image *ext;
1384 int r;
1385
1386 assert(image);
1387
1388 ORDERED_HASHMAP_FOREACH(ext, extension_images) {
1389 r = install_image_symlink(ext->path, flags, changes, n_changes);
1390 if (r < 0)
1391 return r;
1392 }
1393
1394 r = install_image_symlink(image->path, flags, changes, n_changes);
1395 if (r < 0)
1396 return r;
1397
1398 return 0;
1399 }
1400
1401 static bool prefix_matches_compatible(char **matches, char **valid_prefixes) {
1402 /* Checks if all 'matches' are included in the list of 'valid_prefixes' */
1403
1404 STRV_FOREACH(m, matches)
1405 if (!strv_contains(valid_prefixes, *m))
1406 return false;
1407
1408 return true;
1409 }
1410
1411 int portable_attach(
1412 sd_bus *bus,
1413 const char *name_or_path,
1414 char **matches,
1415 const char *profile,
1416 char **extension_image_paths,
1417 const ImagePolicy *image_policy,
1418 PortableFlags flags,
1419 PortableChange **changes,
1420 size_t *n_changes,
1421 sd_bus_error *error) {
1422
1423 _cleanup_ordered_hashmap_free_ OrderedHashmap *extension_images = NULL, *extension_releases = NULL;
1424 _cleanup_(portable_metadata_unrefp) PortableMetadata *os_release = NULL;
1425 _cleanup_hashmap_free_ Hashmap *unit_files = NULL;
1426 _cleanup_(lookup_paths_free) LookupPaths paths = {};
1427 _cleanup_strv_free_ char **valid_prefixes = NULL;
1428 _cleanup_(image_unrefp) Image *image = NULL;
1429 PortableMetadata *item;
1430 int r;
1431
1432 r = extract_image_and_extensions(
1433 name_or_path,
1434 matches,
1435 extension_image_paths,
1436 /* validate_sysext= */ true,
1437 /* relax_extension_release_check= */ FLAGS_SET(flags, PORTABLE_FORCE_SYSEXT),
1438 image_policy,
1439 &image,
1440 &extension_images,
1441 &extension_releases,
1442 &os_release,
1443 &unit_files,
1444 &valid_prefixes,
1445 error);
1446 if (r < 0)
1447 return r;
1448
1449 if (valid_prefixes && !prefix_matches_compatible(matches, valid_prefixes)) {
1450 _cleanup_free_ char *matches_joined = NULL, *extensions_joined = NULL, *valid_prefixes_joined = NULL;
1451
1452 matches_joined = strv_join(matches, "', '");
1453 if (!matches_joined)
1454 return -ENOMEM;
1455
1456 extensions_joined = strv_join(extension_image_paths, ", ");
1457 if (!extensions_joined)
1458 return -ENOMEM;
1459
1460 valid_prefixes_joined = strv_join(valid_prefixes, ", ");
1461 if (!valid_prefixes_joined)
1462 return -ENOMEM;
1463
1464 return sd_bus_error_setf(
1465 error,
1466 SD_BUS_ERROR_INVALID_ARGS,
1467 "Selected matches '%s' are not compatible with portable service image '%s%s%s', refusing. (Acceptable prefix matches are: %s)",
1468 matches_joined,
1469 image->path,
1470 isempty(extensions_joined) ? "" : "' or any of its extensions '",
1471 strempty(extensions_joined),
1472 valid_prefixes_joined);
1473 }
1474
1475 if (hashmap_isempty(unit_files)) {
1476 _cleanup_free_ char *extensions_joined = strv_join(extension_image_paths, ", ");
1477 if (!extensions_joined)
1478 return -ENOMEM;
1479
1480 return sd_bus_error_setf(
1481 error,
1482 SD_BUS_ERROR_INVALID_ARGS,
1483 "Couldn't find any matching unit files in image '%s%s%s', refusing.",
1484 image->path,
1485 isempty(extensions_joined) ? "" : "' or any of its extensions '",
1486 strempty(extensions_joined));
1487 }
1488
1489 r = lookup_paths_init(&paths, RUNTIME_SCOPE_SYSTEM, LOOKUP_PATHS_SPLIT_USR, NULL);
1490 if (r < 0)
1491 return r;
1492
1493 if (!FLAGS_SET(flags, PORTABLE_REATTACH) && !FLAGS_SET(flags, PORTABLE_FORCE_ATTACH))
1494 HASHMAP_FOREACH(item, unit_files) {
1495 r = unit_file_exists(RUNTIME_SCOPE_SYSTEM, &paths, item->name);
1496 if (r < 0)
1497 return sd_bus_error_set_errnof(error, r, "Failed to determine whether unit '%s' exists on the host: %m", item->name);
1498 if (r > 0)
1499 return sd_bus_error_setf(error, BUS_ERROR_UNIT_EXISTS, "Unit file '%s' exists on the host already, refusing.", item->name);
1500
1501 r = unit_file_is_active(bus, item->name, error);
1502 if (r < 0)
1503 return r;
1504 if (r > 0)
1505 return sd_bus_error_setf(error, BUS_ERROR_UNIT_EXISTS, "Unit file '%s' is active already, refusing.", item->name);
1506 }
1507
1508 HASHMAP_FOREACH(item, unit_files) {
1509 r = attach_unit_file(&paths, image->path, image->type, extension_images, extension_releases,
1510 item, os_release, profile, flags, changes, n_changes);
1511 if (r < 0)
1512 return sd_bus_error_set_errnof(error, r, "Failed to attach unit '%s': %m", item->name);
1513 }
1514
1515 /* We don't care too much for the image symlink, it's just a convenience thing, it's not necessary for proper
1516 * operation otherwise. */
1517 (void) install_image_and_extensions_symlinks(image, extension_images, flags, changes, n_changes);
1518
1519 return 0;
1520 }
1521
1522 static bool marker_matches_images(const char *marker, const char *name_or_path, char **extension_image_paths) {
1523 _cleanup_strv_free_ char **root_and_extensions = NULL;
1524 const char *a;
1525 int r;
1526
1527 assert(marker);
1528 assert(name_or_path);
1529
1530 /* If extensions were used when attaching, the marker will be a colon-separated
1531 * list of images/paths. We enforce strict 1:1 matching, so that we are sure
1532 * we are detaching exactly what was attached.
1533 * For each image, starting with the root, we look for a token in the marker,
1534 * and return a negative answer on any non-matching combination. */
1535
1536 root_and_extensions = strv_new(name_or_path);
1537 if (!root_and_extensions)
1538 return -ENOMEM;
1539
1540 r = strv_extend_strv(&root_and_extensions, extension_image_paths, false);
1541 if (r < 0)
1542 return r;
1543
1544 STRV_FOREACH(image_name_or_path, root_and_extensions) {
1545 _cleanup_free_ char *image = NULL;
1546
1547 r = extract_first_word(&marker, &image, ":", EXTRACT_UNQUOTE|EXTRACT_RETAIN_ESCAPE);
1548 if (r < 0)
1549 return log_debug_errno(r, "Failed to parse marker: %s", marker);
1550 if (r == 0)
1551 return false;
1552
1553 a = last_path_component(image);
1554
1555 if (image_name_is_valid(*image_name_or_path)) {
1556 const char *e, *underscore;
1557
1558 /* We shall match against an image name. In that case let's compare the last component, and optionally
1559 * allow either a suffix of ".raw" or a series of "/".
1560 * But allow matching on a different version of the same image, when a "_" is used as a separator. */
1561 underscore = strchr(*image_name_or_path, '_');
1562 if (underscore) {
1563 if (strneq(a, *image_name_or_path, underscore - *image_name_or_path))
1564 continue;
1565 return false;
1566 }
1567
1568 e = startswith(a, *image_name_or_path);
1569 if (!e)
1570 return false;
1571
1572 if(!(e[strspn(e, "/")] == 0 || streq(e, ".raw")))
1573 return false;
1574 } else {
1575 const char *b, *underscore;
1576 size_t l;
1577
1578 /* We shall match against a path. Let's ignore any prefix here though, as often there are many ways to
1579 * reach the same file. However, in this mode, let's validate any file suffix.
1580 * But also ensure that we don't fail if both components don't have a '/' at all
1581 * (strcspn returns the full length of the string in that case, which might not
1582 * match as the versions might differ). */
1583
1584 l = strcspn(a, "/");
1585 b = last_path_component(*image_name_or_path);
1586
1587 if ((a[l] != '/') != !strchr(b, '/')) /* One is a directory, the other is not */
1588 return false;
1589
1590 if (a[l] != 0 && strcspn(b, "/") != l)
1591 return false;
1592
1593 underscore = strchr(b, '_');
1594 if (underscore)
1595 l = underscore - b;
1596 else { /* Either component could be versioned */
1597 underscore = strchr(a, '_');
1598 if (underscore)
1599 l = underscore - a;
1600 }
1601
1602 if (!strneq(a, b, l))
1603 return false;
1604 }
1605 }
1606
1607 return true;
1608 }
1609
1610 static int test_chroot_dropin(
1611 DIR *d,
1612 const char *where,
1613 const char *fname,
1614 const char *name_or_path,
1615 char **extension_image_paths,
1616 char **ret_marker) {
1617
1618 _cleanup_free_ char *line = NULL, *marker = NULL;
1619 _cleanup_fclose_ FILE *f = NULL;
1620 _cleanup_close_ int fd = -EBADF;
1621 const char *p, *e, *k;
1622 int r;
1623
1624 assert(d);
1625 assert(where);
1626 assert(fname);
1627
1628 /* We recognize unis created from portable images via the drop-in we created for them */
1629
1630 p = strjoina(fname, ".d/20-portable.conf");
1631 fd = openat(dirfd(d), p, O_RDONLY|O_CLOEXEC);
1632 if (fd < 0) {
1633 if (errno == ENOENT)
1634 return 0;
1635
1636 return log_debug_errno(errno, "Failed to open %s/%s: %m", where, p);
1637 }
1638
1639 r = take_fdopen_unlocked(&fd, "r", &f);
1640 if (r < 0)
1641 return log_debug_errno(r, "Failed to convert file handle: %m");
1642
1643 r = read_line(f, LONG_LINE_MAX, &line);
1644 if (r < 0)
1645 return log_debug_errno(r, "Failed to read from %s/%s: %m", where, p);
1646
1647 e = startswith(line, PORTABLE_DROPIN_MARKER_BEGIN);
1648 if (!e)
1649 return 0;
1650
1651 k = endswith(e, PORTABLE_DROPIN_MARKER_END);
1652 if (!k)
1653 return 0;
1654
1655 marker = strndup(e, k - e);
1656 if (!marker)
1657 return -ENOMEM;
1658
1659 if (!name_or_path)
1660 r = true;
1661 else
1662 r = marker_matches_images(marker, name_or_path, extension_image_paths);
1663
1664 if (ret_marker)
1665 *ret_marker = TAKE_PTR(marker);
1666
1667 return r;
1668 }
1669
1670 int portable_detach(
1671 sd_bus *bus,
1672 const char *name_or_path,
1673 char **extension_image_paths,
1674 PortableFlags flags,
1675 PortableChange **changes,
1676 size_t *n_changes,
1677 sd_bus_error *error) {
1678
1679 _cleanup_(lookup_paths_free) LookupPaths paths = {};
1680 _cleanup_set_free_ Set *unit_files = NULL, *markers = NULL;
1681 _cleanup_free_ char *extensions = NULL;
1682 _cleanup_closedir_ DIR *d = NULL;
1683 const char *where, *item;
1684 int ret = 0;
1685 int r;
1686
1687 assert(name_or_path);
1688
1689 r = lookup_paths_init(&paths, RUNTIME_SCOPE_SYSTEM, LOOKUP_PATHS_SPLIT_USR, NULL);
1690 if (r < 0)
1691 return r;
1692
1693 where = attached_path(&paths, flags);
1694
1695 d = opendir(where);
1696 if (!d) {
1697 if (errno == ENOENT)
1698 goto not_found;
1699
1700 return log_debug_errno(errno, "Failed to open '%s' directory: %m", where);
1701 }
1702
1703 FOREACH_DIRENT(de, d, return log_debug_errno(errno, "Failed to enumerate '%s' directory: %m", where)) {
1704 _cleanup_free_ char *marker = NULL, *unit_name = NULL;
1705 const char *dot;
1706
1707 /* When a portable service is enabled with "portablectl --copy=symlink --enable --now attach",
1708 * and is disabled with "portablectl --enable --now detach", which calls DisableUnitFilesWithFlags
1709 * DBus method, the main unit file is removed, but its drop-ins are not. Hence, here we need
1710 * to list both main unit files and drop-in directories (without the main unit files). */
1711
1712 dot = endswith(de->d_name, ".d");
1713 if (dot)
1714 unit_name = strndup(de->d_name, dot - de->d_name);
1715 else
1716 unit_name = strdup(de->d_name);
1717 if (!unit_name)
1718 return -ENOMEM;
1719
1720 if (!unit_name_is_valid(unit_name, UNIT_NAME_ANY))
1721 continue;
1722
1723 /* Filter out duplicates */
1724 if (set_contains(unit_files, unit_name))
1725 continue;
1726
1727 if (dot ? !IN_SET(de->d_type, DT_LNK, DT_DIR) : !IN_SET(de->d_type, DT_LNK, DT_REG))
1728 continue;
1729
1730 r = test_chroot_dropin(d, where, unit_name, name_or_path, extension_image_paths, &marker);
1731 if (r < 0)
1732 return r;
1733 if (r == 0)
1734 continue;
1735
1736 if (!FLAGS_SET(flags, PORTABLE_REATTACH) && !FLAGS_SET(flags, PORTABLE_FORCE_ATTACH)) {
1737 r = unit_file_is_active(bus, unit_name, error);
1738 if (r < 0)
1739 return r;
1740 if (r > 0)
1741 return sd_bus_error_setf(error, BUS_ERROR_UNIT_EXISTS, "Unit file '%s' is active, can't detach.", unit_name);
1742 }
1743
1744 r = set_ensure_consume(&unit_files, &string_hash_ops_free, TAKE_PTR(unit_name));
1745 if (r < 0)
1746 return log_oom_debug();
1747
1748 for (const char *p = marker;;) {
1749 _cleanup_free_ char *image = NULL;
1750
1751 r = extract_first_word(&p, &image, ":", EXTRACT_UNESCAPE_SEPARATORS|EXTRACT_RETAIN_ESCAPE);
1752 if (r < 0)
1753 return log_debug_errno(r, "Failed to parse marker: %s", p);
1754 if (r == 0)
1755 break;
1756
1757 if (path_is_absolute(image) && !image_in_search_path(IMAGE_PORTABLE, NULL, image)) {
1758 r = set_ensure_consume(&markers, &path_hash_ops_free, TAKE_PTR(image));
1759 if (r < 0)
1760 return r;
1761 }
1762 }
1763 }
1764
1765 if (set_isempty(unit_files))
1766 goto not_found;
1767
1768 SET_FOREACH(item, unit_files) {
1769 _cleanup_free_ char *md = NULL;
1770
1771 if (unlinkat(dirfd(d), item, 0) < 0) {
1772 log_debug_errno(errno, "Can't remove unit file %s/%s: %m", where, item);
1773
1774 if (errno != ENOENT && ret >= 0)
1775 ret = -errno;
1776 } else
1777 portable_changes_add_with_prefix(changes, n_changes, PORTABLE_UNLINK, where, item, NULL);
1778
1779 FOREACH_STRING(suffix, ".d/10-profile.conf", ".d/20-portable.conf") {
1780 _cleanup_free_ char *dropin = NULL;
1781
1782 dropin = strjoin(item, suffix);
1783 if (!dropin)
1784 return -ENOMEM;
1785
1786 if (unlinkat(dirfd(d), dropin, 0) < 0) {
1787 log_debug_errno(errno, "Can't remove drop-in %s/%s: %m", where, dropin);
1788
1789 if (errno != ENOENT && ret >= 0)
1790 ret = -errno;
1791 } else
1792 portable_changes_add_with_prefix(changes, n_changes, PORTABLE_UNLINK, where, dropin, NULL);
1793 }
1794
1795 md = strjoin(item, ".d");
1796 if (!md)
1797 return -ENOMEM;
1798
1799 if (unlinkat(dirfd(d), md, AT_REMOVEDIR) < 0) {
1800 log_debug_errno(errno, "Can't remove drop-in directory %s/%s: %m", where, md);
1801
1802 if (errno != ENOENT && ret >= 0)
1803 ret = -errno;
1804 } else
1805 portable_changes_add_with_prefix(changes, n_changes, PORTABLE_UNLINK, where, md, NULL);
1806 }
1807
1808 /* Now, also drop any image symlink, for images outside of the sarch path */
1809 SET_FOREACH(item, markers) {
1810 _cleanup_free_ char *sl = NULL;
1811 struct stat st;
1812
1813 r = image_symlink(item, flags, &sl);
1814 if (r < 0) {
1815 log_debug_errno(r, "Failed to determine image symlink for '%s', ignoring: %m", item);
1816 continue;
1817 }
1818
1819 if (lstat(sl, &st) < 0) {
1820 log_debug_errno(errno, "Failed to stat '%s', ignoring: %m", sl);
1821 continue;
1822 }
1823
1824 if (!S_ISLNK(st.st_mode)) {
1825 log_debug("Image '%s' is not a symlink, ignoring.", sl);
1826 continue;
1827 }
1828
1829 if (unlink(sl) < 0) {
1830 log_debug_errno(errno, "Can't remove image symlink '%s': %m", sl);
1831
1832 if (errno != ENOENT && ret >= 0)
1833 ret = -errno;
1834 } else
1835 portable_changes_add(changes, n_changes, PORTABLE_UNLINK, sl, NULL);
1836 }
1837
1838 /* Try to remove the unit file directory, if we can */
1839 if (rmdir(where) >= 0)
1840 portable_changes_add(changes, n_changes, PORTABLE_UNLINK, where, NULL);
1841
1842 return ret;
1843
1844 not_found:
1845 extensions = strv_join(extension_image_paths, ", ");
1846 if (!extensions)
1847 return -ENOMEM;
1848
1849 r = sd_bus_error_setf(error,
1850 BUS_ERROR_NO_SUCH_UNIT,
1851 "No unit files associated with '%s%s%s' found attached to the system. Image not attached?",
1852 name_or_path,
1853 isempty(extensions) ? "" : "' or any of its extensions '",
1854 isempty(extensions) ? "" : extensions);
1855 return log_debug_errno(r, "%s", error->message);
1856 }
1857
1858 static int portable_get_state_internal(
1859 sd_bus *bus,
1860 const char *name_or_path,
1861 char **extension_image_paths,
1862 PortableFlags flags,
1863 PortableState *ret,
1864 sd_bus_error *error) {
1865
1866 _cleanup_(lookup_paths_free) LookupPaths paths = {};
1867 bool found_enabled = false, found_running = false;
1868 _cleanup_set_free_ Set *unit_files = NULL;
1869 _cleanup_closedir_ DIR *d = NULL;
1870 const char *where;
1871 int r;
1872
1873 assert(name_or_path);
1874 assert(ret);
1875
1876 r = lookup_paths_init(&paths, RUNTIME_SCOPE_SYSTEM, LOOKUP_PATHS_SPLIT_USR, NULL);
1877 if (r < 0)
1878 return r;
1879
1880 where = attached_path(&paths, flags);
1881
1882 d = opendir(where);
1883 if (!d) {
1884 if (errno == ENOENT) {
1885 /* If the 'attached' directory doesn't exist at all, then we know for sure this image isn't attached. */
1886 *ret = PORTABLE_DETACHED;
1887 return 0;
1888 }
1889
1890 return log_debug_errno(errno, "Failed to open '%s' directory: %m", where);
1891 }
1892
1893 FOREACH_DIRENT(de, d, return log_debug_errno(errno, "Failed to enumerate '%s' directory: %m", where)) {
1894 UnitFileState state;
1895
1896 if (!unit_name_is_valid(de->d_name, UNIT_NAME_ANY))
1897 continue;
1898
1899 /* Filter out duplicates */
1900 if (set_contains(unit_files, de->d_name))
1901 continue;
1902
1903 if (!IN_SET(de->d_type, DT_LNK, DT_REG))
1904 continue;
1905
1906 r = test_chroot_dropin(d, where, de->d_name, name_or_path, extension_image_paths, NULL);
1907 if (r < 0)
1908 return r;
1909 if (r == 0)
1910 continue;
1911
1912 r = unit_file_lookup_state(RUNTIME_SCOPE_SYSTEM, &paths, de->d_name, &state);
1913 if (r < 0)
1914 return log_debug_errno(r, "Failed to determine unit file state of '%s': %m", de->d_name);
1915 if (!IN_SET(state, UNIT_FILE_STATIC, UNIT_FILE_DISABLED, UNIT_FILE_LINKED, UNIT_FILE_LINKED_RUNTIME))
1916 found_enabled = true;
1917
1918 r = unit_file_is_active(bus, de->d_name, error);
1919 if (r < 0)
1920 return r;
1921 if (r > 0)
1922 found_running = true;
1923
1924 r = set_put_strdup(&unit_files, de->d_name);
1925 if (r < 0)
1926 return log_debug_errno(r, "Failed to add unit name '%s' to set: %m", de->d_name);
1927 }
1928
1929 *ret = found_running ? (!set_isempty(unit_files) && (flags & PORTABLE_RUNTIME) ? PORTABLE_RUNNING_RUNTIME : PORTABLE_RUNNING) :
1930 found_enabled ? (flags & PORTABLE_RUNTIME ? PORTABLE_ENABLED_RUNTIME : PORTABLE_ENABLED) :
1931 !set_isempty(unit_files) ? (flags & PORTABLE_RUNTIME ? PORTABLE_ATTACHED_RUNTIME : PORTABLE_ATTACHED) : PORTABLE_DETACHED;
1932
1933 return 0;
1934 }
1935
1936 int portable_get_state(
1937 sd_bus *bus,
1938 const char *name_or_path,
1939 char **extension_image_paths,
1940 PortableFlags flags,
1941 PortableState *ret,
1942 sd_bus_error *error) {
1943
1944 PortableState state;
1945 int r;
1946
1947 assert(name_or_path);
1948 assert(ret);
1949
1950 /* We look for matching units twice: once in the regular directories, and once in the runtime directories — but
1951 * the latter only if we didn't find anything in the former. */
1952
1953 r = portable_get_state_internal(bus, name_or_path, extension_image_paths, flags & ~PORTABLE_RUNTIME, &state, error);
1954 if (r < 0)
1955 return r;
1956
1957 if (state == PORTABLE_DETACHED) {
1958 r = portable_get_state_internal(bus, name_or_path, extension_image_paths, flags | PORTABLE_RUNTIME, &state, error);
1959 if (r < 0)
1960 return r;
1961 }
1962
1963 *ret = state;
1964 return 0;
1965 }
1966
1967 int portable_get_profiles(char ***ret) {
1968 assert(ret);
1969
1970 return conf_files_list_nulstr(ret, NULL, NULL, CONF_FILES_DIRECTORY|CONF_FILES_BASENAME|CONF_FILES_FILTER_MASKED, PORTABLE_PROFILE_DIRS);
1971 }
1972
1973 static const char* const portable_change_type_table[_PORTABLE_CHANGE_TYPE_MAX] = {
1974 [PORTABLE_COPY] = "copy",
1975 [PORTABLE_MKDIR] = "mkdir",
1976 [PORTABLE_SYMLINK] = "symlink",
1977 [PORTABLE_UNLINK] = "unlink",
1978 [PORTABLE_WRITE] = "write",
1979 };
1980
1981 DEFINE_STRING_TABLE_LOOKUP(portable_change_type, int);
1982
1983 static const char* const portable_state_table[_PORTABLE_STATE_MAX] = {
1984 [PORTABLE_DETACHED] = "detached",
1985 [PORTABLE_ATTACHED] = "attached",
1986 [PORTABLE_ATTACHED_RUNTIME] = "attached-runtime",
1987 [PORTABLE_ENABLED] = "enabled",
1988 [PORTABLE_ENABLED_RUNTIME] = "enabled-runtime",
1989 [PORTABLE_RUNNING] = "running",
1990 [PORTABLE_RUNNING_RUNTIME] = "running-runtime",
1991 };
1992
1993 DEFINE_STRING_TABLE_LOOKUP(portable_state, PortableState);