]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/portable/portable.c
util-lib: split out all temporary file related calls into tmpfiles-util.c
[thirdparty/systemd.git] / src / portable / portable.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2
3 #include <stdio_ext.h>
4
5 #include "bus-common-errors.h"
6 #include "bus-error.h"
7 #include "conf-files.h"
8 #include "copy.h"
9 #include "def.h"
10 #include "dirent-util.h"
11 #include "dissect-image.h"
12 #include "fd-util.h"
13 #include "fileio.h"
14 #include "fs-util.h"
15 #include "io-util.h"
16 #include "locale-util.h"
17 #include "loop-util.h"
18 #include "machine-image.h"
19 #include "mkdir.h"
20 #include "os-util.h"
21 #include "path-lookup.h"
22 #include "portable.h"
23 #include "process-util.h"
24 #include "set.h"
25 #include "signal-util.h"
26 #include "socket-util.h"
27 #include "string-table.h"
28 #include "strv.h"
29 #include "tmpfile-util.h"
30 #include "user-util.h"
31
32 static const char profile_dirs[] = CONF_PATHS_NULSTR("systemd/portable/profile");
33
34 /* Markers used in the first line of our 20-portable.conf unit file drop-in to determine, that a) the unit file was
35 * dropped there by the portable service logic and b) for which image it was dropped there. */
36 #define PORTABLE_DROPIN_MARKER_BEGIN "# Drop-in created for image '"
37 #define PORTABLE_DROPIN_MARKER_END "', do not edit."
38
39 static bool prefix_match(const char *unit, const char *prefix) {
40 const char *p;
41
42 p = startswith(unit, prefix);
43 if (!p)
44 return false;
45
46 /* Only respect prefixes followed by dash or dot or when there's a complete match */
47 return IN_SET(*p, '-', '.', '@', 0);
48 }
49
50 static bool unit_match(const char *unit, char **matches) {
51 const char *dot;
52 char **i;
53
54 dot = strrchr(unit, '.');
55 if (!dot)
56 return false;
57
58 if (!STR_IN_SET(dot, ".service", ".socket", ".target", ".timer", ".path"))
59 return false;
60
61 /* Empty match expression means: everything */
62 if (strv_isempty(matches))
63 return true;
64
65 /* Otherwise, at least one needs to match */
66 STRV_FOREACH(i, matches)
67 if (prefix_match(unit, *i))
68 return true;
69
70 return false;
71 }
72
73 static PortableMetadata *portable_metadata_new(const char *name, int fd) {
74 PortableMetadata *m;
75
76 m = malloc0(offsetof(PortableMetadata, name) + strlen(name) + 1);
77 if (!m)
78 return NULL;
79
80 strcpy(m->name, name);
81 m->fd = fd;
82
83 return m;
84 }
85
86 PortableMetadata *portable_metadata_unref(PortableMetadata *i) {
87 if (!i)
88 return NULL;
89
90 safe_close(i->fd);
91 free(i->source);
92
93 return mfree(i);
94 }
95
96 Hashmap *portable_metadata_hashmap_unref(Hashmap *h) {
97
98 for (;;) {
99 PortableMetadata *i;
100
101 i = hashmap_steal_first(h);
102 if (!i)
103 break;
104
105 portable_metadata_unref(i);
106 }
107
108 return hashmap_free(h);
109 }
110
111 static int compare_metadata(PortableMetadata *const *x, PortableMetadata *const *y) {
112 return strcmp((*x)->name, (*y)->name);
113 }
114
115 int portable_metadata_hashmap_to_sorted_array(Hashmap *unit_files, PortableMetadata ***ret) {
116
117 _cleanup_free_ PortableMetadata **sorted = NULL;
118 Iterator iterator;
119 PortableMetadata *item;
120 size_t k = 0;
121
122 sorted = new(PortableMetadata*, hashmap_size(unit_files));
123 if (!sorted)
124 return -ENOMEM;
125
126 HASHMAP_FOREACH(item, unit_files, iterator)
127 sorted[k++] = item;
128
129 assert(k == hashmap_size(unit_files));
130
131 typesafe_qsort(sorted, k, compare_metadata);
132
133 *ret = TAKE_PTR(sorted);
134 return 0;
135 }
136
137 static int send_item(
138 int socket_fd,
139 const char *name,
140 int fd) {
141
142 union {
143 struct cmsghdr cmsghdr;
144 uint8_t buf[CMSG_SPACE(sizeof(int))];
145 } control = {};
146 struct iovec iovec;
147 struct msghdr mh = {
148 .msg_control = &control,
149 .msg_controllen = sizeof(control),
150 .msg_iov = &iovec,
151 .msg_iovlen = 1,
152 };
153 struct cmsghdr *cmsg;
154 _cleanup_close_ int data_fd = -1;
155
156 assert(socket_fd >= 0);
157 assert(name);
158 assert(fd >= 0);
159
160 data_fd = fd_duplicate_data_fd(fd);
161 if (data_fd < 0)
162 return data_fd;
163
164 cmsg = CMSG_FIRSTHDR(&mh);
165 cmsg->cmsg_level = SOL_SOCKET;
166 cmsg->cmsg_type = SCM_RIGHTS;
167 cmsg->cmsg_len = CMSG_LEN(sizeof(int));
168 memcpy(CMSG_DATA(cmsg), &data_fd, sizeof(int));
169
170 mh.msg_controllen = CMSG_SPACE(sizeof(int));
171 iovec = IOVEC_MAKE_STRING(name);
172
173 if (sendmsg(socket_fd, &mh, MSG_NOSIGNAL) < 0)
174 return -errno;
175
176 return 0;
177 }
178
179 static int recv_item(
180 int socket_fd,
181 char **ret_name,
182 int *ret_fd) {
183
184 union {
185 struct cmsghdr cmsghdr;
186 uint8_t buf[CMSG_SPACE(sizeof(int))];
187 } control = {};
188 char buffer[PATH_MAX+2];
189 struct iovec iov = IOVEC_INIT(buffer, sizeof(buffer)-1);
190 struct msghdr mh = {
191 .msg_control = &control,
192 .msg_controllen = sizeof(control),
193 .msg_iov = &iov,
194 .msg_iovlen = 1,
195 };
196 struct cmsghdr *cmsg;
197 _cleanup_close_ int found_fd = -1;
198 char *copy;
199 ssize_t n;
200
201 assert(socket_fd >= 0);
202 assert(ret_name);
203 assert(ret_fd);
204
205 n = recvmsg(socket_fd, &mh, MSG_CMSG_CLOEXEC);
206 if (n < 0)
207 return -errno;
208
209 CMSG_FOREACH(cmsg, &mh) {
210 if (cmsg->cmsg_level == SOL_SOCKET &&
211 cmsg->cmsg_type == SCM_RIGHTS) {
212
213 if (cmsg->cmsg_len == CMSG_LEN(sizeof(int))) {
214 assert(found_fd < 0);
215 found_fd = *(int*) CMSG_DATA(cmsg);
216 break;
217 }
218
219 cmsg_close_all(&mh);
220 return -EIO;
221 }
222 }
223
224 buffer[n] = 0;
225
226 copy = strdup(buffer);
227 if (!copy)
228 return -ENOMEM;
229
230 *ret_name = copy;
231 *ret_fd = TAKE_FD(found_fd);
232
233 return 0;
234 }
235
236 static int extract_now(
237 const char *where,
238 char **matches,
239 int socket_fd,
240 PortableMetadata **ret_os_release,
241 Hashmap **ret_unit_files) {
242
243 _cleanup_(portable_metadata_hashmap_unrefp) Hashmap *unit_files = NULL;
244 _cleanup_(portable_metadata_unrefp) PortableMetadata *os_release = NULL;
245 _cleanup_(lookup_paths_free) LookupPaths paths = {};
246 _cleanup_close_ int os_release_fd = -1;
247 _cleanup_free_ char *os_release_path = NULL;
248 char **i;
249 int r;
250
251 /* Extracts the metadata from a directory tree 'where'. Extracts two kinds of information: the /etc/os-release
252 * data, and all unit files matching the specified expression. Note that this function is called in two very
253 * different but also similar contexts. When the tool gets invoked on a directory tree, we'll process it
254 * directly, and in-process, and thus can return the requested data directly, via 'ret_os_release' and
255 * 'ret_unit_files'. However, if the tool is invoked on a raw disk image — which needs to be mounted first — we
256 * are invoked in a child process with private mounts and then need to send the collected data to our
257 * parent. To handle both cases in one call this function also gets a 'socket_fd' parameter, which when >= 0 is
258 * used to send the data to the parent. */
259
260 assert(where);
261
262 /* First, find /etc/os-release and send it upstream (or just save it). */
263 r = open_os_release(where, &os_release_path, &os_release_fd);
264 if (r < 0)
265 log_debug_errno(r, "Couldn't acquire os-release file, ignoring: %m");
266 else {
267 if (socket_fd >= 0) {
268 r = send_item(socket_fd, "/etc/os-release", os_release_fd);
269 if (r < 0)
270 return log_debug_errno(r, "Failed to send os-release file: %m");
271 }
272
273 if (ret_os_release) {
274 os_release = portable_metadata_new("/etc/os-release", os_release_fd);
275 if (!os_release)
276 return -ENOMEM;
277
278 os_release_fd = -1;
279 os_release->source = TAKE_PTR(os_release_path);
280 }
281 }
282
283 /* Then, send unit file data to the parent (or/and add it to the hashmap). For that we use our usual unit
284 * discovery logic. Note that we force looking inside of /lib/systemd/system/ for units too, as we mightbe
285 * compiled for a split-usr system but the image might be a legacy-usr one. */
286 r = lookup_paths_init(&paths, UNIT_FILE_SYSTEM, LOOKUP_PATHS_SPLIT_USR, where);
287 if (r < 0)
288 return log_debug_errno(r, "Failed to acquire lookup paths: %m");
289
290 unit_files = hashmap_new(&string_hash_ops);
291 if (!unit_files)
292 return -ENOMEM;
293
294 STRV_FOREACH(i, paths.search_path) {
295 _cleanup_free_ char *resolved = NULL;
296 _cleanup_closedir_ DIR *d = NULL;
297 struct dirent *de;
298
299 r = chase_symlinks_and_opendir(*i, where, 0, &resolved, &d);
300 if (r < 0) {
301 log_debug_errno(r, "Failed to open unit path '%s', ignoring: %m", *i);
302 continue;
303 }
304
305 FOREACH_DIRENT(de, d, return log_debug_errno(errno, "Failed to read directory: %m")) {
306 _cleanup_(portable_metadata_unrefp) PortableMetadata *m = NULL;
307 _cleanup_close_ int fd = -1;
308
309 if (!unit_name_is_valid(de->d_name, UNIT_NAME_ANY))
310 continue;
311
312 if (!unit_match(de->d_name, matches))
313 continue;
314
315 /* Filter out duplicates */
316 if (hashmap_get(unit_files, de->d_name))
317 continue;
318
319 dirent_ensure_type(d, de);
320 if (!IN_SET(de->d_type, DT_LNK, DT_REG))
321 continue;
322
323 fd = openat(dirfd(d), de->d_name, O_CLOEXEC|O_RDONLY);
324 if (fd < 0) {
325 log_debug_errno(errno, "Failed to open unit file '%s', ignoring: %m", de->d_name);
326 continue;
327 }
328
329 if (socket_fd >= 0) {
330 r = send_item(socket_fd, de->d_name, fd);
331 if (r < 0)
332 return log_debug_errno(r, "Failed to send unit metadata to parent: %m");
333 }
334
335 m = portable_metadata_new(de->d_name, fd);
336 if (!m)
337 return -ENOMEM;
338 fd = -1;
339
340 m->source = strjoin(resolved, "/", de->d_name);
341 if (!m->source)
342 return -ENOMEM;
343
344 r = hashmap_put(unit_files, m->name, m);
345 if (r < 0)
346 return log_debug_errno(r, "Failed to add unit to hashmap: %m");
347 m = NULL;
348 }
349 }
350
351 if (ret_os_release)
352 *ret_os_release = TAKE_PTR(os_release);
353 if (ret_unit_files)
354 *ret_unit_files = TAKE_PTR(unit_files);
355
356 return 0;
357 }
358
359 static int portable_extract_by_path(
360 const char *path,
361 char **matches,
362 PortableMetadata **ret_os_release,
363 Hashmap **ret_unit_files,
364 sd_bus_error *error) {
365
366 _cleanup_(portable_metadata_hashmap_unrefp) Hashmap *unit_files = NULL;
367 _cleanup_(portable_metadata_unrefp) PortableMetadata* os_release = NULL;
368 _cleanup_(loop_device_unrefp) LoopDevice *d = NULL;
369 int r;
370
371 assert(path);
372
373 r = loop_device_make_by_path(path, O_RDONLY, &d);
374 if (r == -EISDIR) {
375 /* We can't turn this into a loop-back block device, and this returns EISDIR? Then this is a directory
376 * tree and not a raw device. It's easy then. */
377
378 r = extract_now(path, matches, -1, &os_release, &unit_files);
379 if (r < 0)
380 return r;
381
382 } else if (r < 0)
383 return log_debug_errno(r, "Failed to set up loopback device: %m");
384 else {
385 _cleanup_(dissected_image_unrefp) DissectedImage *m = NULL;
386 _cleanup_(rmdir_and_freep) char *tmpdir = NULL;
387 _cleanup_(close_pairp) int seq[2] = { -1, -1 };
388 _cleanup_(sigkill_waitp) pid_t child = 0;
389
390 /* We now have a loopback block device, let's fork off a child in its own mount namespace, mount it
391 * there, and extract the metadata we need. The metadata is sent from the child back to us. */
392
393 BLOCK_SIGNALS(SIGCHLD);
394
395 r = mkdtemp_malloc("/tmp/inspect-XXXXXX", &tmpdir);
396 if (r < 0)
397 return log_debug_errno(r, "Failed to create temporary directory: %m");
398
399 r = dissect_image(d->fd, NULL, 0, DISSECT_IMAGE_READ_ONLY|DISSECT_IMAGE_REQUIRE_ROOT|DISSECT_IMAGE_DISCARD_ON_LOOP, &m);
400 if (r == -ENOPKG)
401 sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Couldn't identify a suitable partition table or file system in '%s'.", path);
402 else if (r == -EADDRNOTAVAIL)
403 sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "No root partition for specified root hash found in '%s'.", path);
404 else if (r == -ENOTUNIQ)
405 sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Multiple suitable root partitions found in image '%s'.", path);
406 else if (r == -ENXIO)
407 sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "No suitable root partition found in image '%s'.", path);
408 else if (r == -EPROTONOSUPPORT)
409 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);
410 if (r < 0)
411 return r;
412
413 if (socketpair(AF_UNIX, SOCK_SEQPACKET|SOCK_CLOEXEC, 0, seq) < 0)
414 return log_debug_errno(errno, "Failed to allocated SOCK_SEQPACKET socket: %m");
415
416 r = safe_fork("(sd-dissect)", FORK_RESET_SIGNALS|FORK_DEATHSIG|FORK_NEW_MOUNTNS|FORK_MOUNTNS_SLAVE|FORK_LOG, &child);
417 if (r < 0)
418 return r;
419 if (r == 0) {
420 seq[0] = safe_close(seq[0]);
421
422 r = dissected_image_mount(m, tmpdir, UID_INVALID, DISSECT_IMAGE_READ_ONLY|DISSECT_IMAGE_VALIDATE_OS);
423 if (r < 0) {
424 log_debug_errno(r, "Failed to mount dissected image: %m");
425 goto child_finish;
426 }
427
428 r = extract_now(tmpdir, matches, seq[1], NULL, NULL);
429
430 child_finish:
431 _exit(r < 0 ? EXIT_FAILURE : EXIT_SUCCESS);
432 }
433
434 seq[1] = safe_close(seq[1]);
435
436 unit_files = hashmap_new(&string_hash_ops);
437 if (!unit_files)
438 return -ENOMEM;
439
440 for (;;) {
441 _cleanup_(portable_metadata_unrefp) PortableMetadata *add = NULL;
442 _cleanup_free_ char *name = NULL;
443 _cleanup_close_ int fd = -1;
444
445 r = recv_item(seq[0], &name, &fd);
446 if (r < 0)
447 return log_debug_errno(r, "Failed to receive item: %m");
448
449 /* We can't really distuingish 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(name) && fd < 0)
452 break;
453
454 if (isempty(name) || fd < 0) {
455 log_debug("Invalid item sent from child.");
456 return -EINVAL;
457 }
458
459 add = portable_metadata_new(name, fd);
460 if (!add)
461 return -ENOMEM;
462 fd = -1;
463
464 /* Note that we do not initialize 'add->source' here, as the source path is not usable here as
465 * it refers to a path only valid in the short-living namespaced child process we forked
466 * here. */
467
468 if (PORTABLE_METADATA_IS_UNIT(add)) {
469 r = hashmap_put(unit_files, add->name, add);
470 if (r < 0)
471 return log_debug_errno(r, "Failed to add item to unit file list: %m");
472
473 add = NULL;
474
475 } else if (PORTABLE_METADATA_IS_OS_RELEASE(add)) {
476
477 assert(!os_release);
478 os_release = TAKE_PTR(add);
479 } else
480 assert_not_reached("Unexpected metadata item from child.");
481 }
482
483 r = wait_for_terminate_and_check("(sd-dissect)", child, 0);
484 if (r < 0)
485 return r;
486 child = 0;
487 }
488
489 if (!os_release)
490 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Image '%s' lacks os-release data, refusing.", path);
491
492 if (hashmap_isempty(unit_files))
493 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Couldn't find any matching unit files in image '%s', refusing.", path);
494
495 if (ret_unit_files)
496 *ret_unit_files = TAKE_PTR(unit_files);
497
498 if (ret_os_release)
499 *ret_os_release = TAKE_PTR(os_release);
500
501 return 0;
502 }
503
504 int portable_extract(
505 const char *name_or_path,
506 char **matches,
507 PortableMetadata **ret_os_release,
508 Hashmap **ret_unit_files,
509 sd_bus_error *error) {
510
511 _cleanup_(image_unrefp) Image *image = NULL;
512 int r;
513
514 assert(name_or_path);
515
516 r = image_find_harder(IMAGE_PORTABLE, name_or_path, &image);
517 if (r < 0)
518 return r;
519
520 return portable_extract_by_path(image->path, matches, ret_os_release, ret_unit_files, error);
521 }
522
523 static int unit_file_is_active(
524 sd_bus *bus,
525 const char *name,
526 sd_bus_error *error) {
527
528 static const char *const active_states[] = {
529 "activating",
530 "active",
531 "reloading",
532 "deactivating",
533 NULL,
534 };
535 int r;
536
537 if (!bus)
538 return false;
539
540 /* If we are looking at a plain or instance things are easy, we can just query the state */
541 if (unit_name_is_valid(name, UNIT_NAME_PLAIN|UNIT_NAME_INSTANCE)) {
542 _cleanup_free_ char *path = NULL, *buf = NULL;
543
544 path = unit_dbus_path_from_name(name);
545 if (!path)
546 return -ENOMEM;
547
548 r = sd_bus_get_property_string(
549 bus,
550 "org.freedesktop.systemd1",
551 path,
552 "org.freedesktop.systemd1.Unit",
553 "ActiveState",
554 error,
555 &buf);
556 if (r < 0)
557 return log_debug_errno(r, "Failed to retrieve unit state: %s", bus_error_message(error, r));
558
559 return strv_contains((char**) active_states, buf);
560 }
561
562 /* Otherwise we need to enumerate. But let's build the most restricted query we can */
563 if (unit_name_is_valid(name, UNIT_NAME_TEMPLATE)) {
564 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL, *reply = NULL;
565 const char *at, *prefix, *joined;
566
567 r = sd_bus_message_new_method_call(
568 bus,
569 &m,
570 "org.freedesktop.systemd1",
571 "/org/freedesktop/systemd1",
572 "org.freedesktop.systemd1.Manager",
573 "ListUnitsByPatterns");
574 if (r < 0)
575 return r;
576
577 r = sd_bus_message_append_strv(m, (char**) active_states);
578 if (r < 0)
579 return r;
580
581 at = strchr(name, '@');
582 assert(at);
583
584 prefix = strndupa(name, at + 1 - name);
585 joined = strjoina(prefix, "*", at + 1);
586
587 r = sd_bus_message_append_strv(m, STRV_MAKE(joined));
588 if (r < 0)
589 return r;
590
591 r = sd_bus_call(bus, m, 0, error, &reply);
592 if (r < 0)
593 return log_debug_errno(r, "Failed to list units: %s", bus_error_message(error, r));
594
595 r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_ARRAY, "(ssssssouso)");
596 if (r < 0)
597 return r;
598
599 r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_STRUCT, "ssssssouso");
600 if (r < 0)
601 return r;
602
603 return r > 0;
604 }
605
606 return -EINVAL;
607 }
608
609 static int portable_changes_add(
610 PortableChange **changes,
611 size_t *n_changes,
612 PortableChangeType type,
613 const char *path,
614 const char *source) {
615
616 _cleanup_free_ char *p = NULL, *s = NULL;
617 PortableChange *c;
618
619 assert(path);
620 assert(!changes == !n_changes);
621
622 if (!changes)
623 return 0;
624
625 c = reallocarray(*changes, *n_changes + 1, sizeof(PortableChange));
626 if (!c)
627 return -ENOMEM;
628 *changes = c;
629
630 p = strdup(path);
631 if (!p)
632 return -ENOMEM;
633
634 path_simplify(p, false);
635
636 if (source) {
637 s = strdup(source);
638 if (!s)
639 return -ENOMEM;
640
641 path_simplify(s, false);
642 }
643
644 c[(*n_changes)++] = (PortableChange) {
645 .type = type,
646 .path = TAKE_PTR(p),
647 .source = TAKE_PTR(s),
648 };
649
650 return 0;
651 }
652
653 static int portable_changes_add_with_prefix(
654 PortableChange **changes,
655 size_t *n_changes,
656 PortableChangeType type,
657 const char *prefix,
658 const char *path,
659 const char *source) {
660
661 assert(path);
662 assert(!changes == !n_changes);
663
664 if (!changes)
665 return 0;
666
667 if (prefix) {
668 path = strjoina(prefix, "/", path);
669
670 if (source)
671 source = strjoina(prefix, "/", source);
672 }
673
674 return portable_changes_add(changes, n_changes, type, path, source);
675 }
676
677 void portable_changes_free(PortableChange *changes, size_t n_changes) {
678 size_t i;
679
680 assert(changes || n_changes == 0);
681
682 for (i = 0; i < n_changes; i++) {
683 free(changes[i].path);
684 free(changes[i].source);
685 }
686
687 free(changes);
688 }
689
690 static int install_chroot_dropin(
691 const char *image_path,
692 ImageType type,
693 const PortableMetadata *m,
694 const char *dropin_dir,
695 char **ret_dropin,
696 PortableChange **changes,
697 size_t *n_changes) {
698
699 _cleanup_free_ char *text = NULL, *dropin = NULL;
700 int r;
701
702 assert(image_path);
703 assert(m);
704 assert(dropin_dir);
705
706 dropin = strjoin(dropin_dir, "/20-portable.conf");
707 if (!dropin)
708 return -ENOMEM;
709
710 text = strjoin(PORTABLE_DROPIN_MARKER_BEGIN, image_path, PORTABLE_DROPIN_MARKER_END "\n");
711 if (!text)
712 return -ENOMEM;
713
714 if (endswith(m->name, ".service"))
715 if (!strextend(&text,
716 "\n"
717 "[Service]\n",
718 IN_SET(type, IMAGE_DIRECTORY, IMAGE_SUBVOLUME) ? "RootDirectory=" : "RootImage=", image_path, "\n"
719 "Environment=PORTABLE=", basename(image_path), "\n"
720 "LogExtraFields=PORTABLE=", basename(image_path), "\n",
721 NULL))
722
723 return -ENOMEM;
724
725 r = write_string_file(dropin, text, WRITE_STRING_FILE_CREATE|WRITE_STRING_FILE_ATOMIC);
726 if (r < 0)
727 return log_debug_errno(r, "Failed to write '%s': %m", dropin);
728
729 (void) portable_changes_add(changes, n_changes, PORTABLE_WRITE, dropin, NULL);
730
731 if (ret_dropin)
732 *ret_dropin = TAKE_PTR(dropin);
733
734 return 0;
735 }
736
737 static int find_profile(const char *name, const char *unit, char **ret) {
738 const char *p, *dot;
739
740 assert(name);
741 assert(ret);
742
743 assert_se(dot = strrchr(unit, '.'));
744
745 NULSTR_FOREACH(p, profile_dirs) {
746 _cleanup_free_ char *joined;
747
748 joined = strjoin(p, "/", name, "/", dot + 1, ".conf");
749 if (!joined)
750 return -ENOMEM;
751
752 if (laccess(joined, F_OK) >= 0) {
753 *ret = TAKE_PTR(joined);
754 return 0;
755 }
756
757 if (errno != ENOENT)
758 return -errno;
759 }
760
761 return -ENOENT;
762 }
763
764 static int install_profile_dropin(
765 const char *image_path,
766 const PortableMetadata *m,
767 const char *dropin_dir,
768 const char *profile,
769 PortableFlags flags,
770 char **ret_dropin,
771 PortableChange **changes,
772 size_t *n_changes) {
773
774 _cleanup_free_ char *dropin = NULL, *from = NULL;
775 int r;
776
777 assert(image_path);
778 assert(m);
779 assert(dropin_dir);
780
781 if (!profile)
782 return 0;
783
784 r = find_profile(profile, m->name, &from);
785 if (r < 0) {
786 if (r != -ENOENT)
787 return log_debug_errno(errno, "Profile '%s' is not accessible: %m", profile);
788
789 log_debug_errno(errno, "Skipping link to profile '%s', as it does not exist: %m", profile);
790 return 0;
791 }
792
793 dropin = strjoin(dropin_dir, "/10-profile.conf");
794 if (!dropin)
795 return -ENOMEM;
796
797 if (flags & PORTABLE_PREFER_COPY) {
798
799 r = copy_file_atomic(from, dropin, 0644, 0, COPY_REFLINK);
800 if (r < 0)
801 return log_debug_errno(r, "Failed to copy %s %s %s: %m", from, special_glyph(ARROW), dropin);
802
803 (void) portable_changes_add(changes, n_changes, PORTABLE_COPY, dropin, from);
804
805 } else {
806
807 if (symlink(from, dropin) < 0)
808 return log_debug_errno(errno, "Failed to link %s %s %s: %m", from, special_glyph(ARROW), dropin);
809
810 (void) portable_changes_add(changes, n_changes, PORTABLE_SYMLINK, dropin, from);
811 }
812
813 if (ret_dropin)
814 *ret_dropin = TAKE_PTR(dropin);
815
816 return 0;
817 }
818
819 static const char *attached_path(const LookupPaths *paths, PortableFlags flags) {
820 const char *where;
821
822 assert(paths);
823
824 if (flags & PORTABLE_RUNTIME)
825 where = paths->runtime_attached;
826 else
827 where = paths->persistent_attached;
828
829 assert(where);
830 return where;
831 }
832
833 static int attach_unit_file(
834 const LookupPaths *paths,
835 const char *image_path,
836 ImageType type,
837 const PortableMetadata *m,
838 const char *profile,
839 PortableFlags flags,
840 PortableChange **changes,
841 size_t *n_changes) {
842
843 _cleanup_(unlink_and_freep) char *chroot_dropin = NULL, *profile_dropin = NULL;
844 _cleanup_(rmdir_and_freep) char *dropin_dir = NULL;
845 const char *where, *path;
846 int r;
847
848 assert(paths);
849 assert(image_path);
850 assert(m);
851 assert(PORTABLE_METADATA_IS_UNIT(m));
852
853 where = attached_path(paths, flags);
854
855 (void) mkdir_parents(where, 0755);
856 if (mkdir(where, 0755) < 0) {
857 if (errno != EEXIST)
858 return -errno;
859 } else
860 (void) portable_changes_add(changes, n_changes, PORTABLE_MKDIR, where, NULL);
861
862 path = strjoina(where, "/", m->name);
863 dropin_dir = strjoin(path, ".d");
864 if (!dropin_dir)
865 return -ENOMEM;
866
867 if (mkdir(dropin_dir, 0755) < 0) {
868 if (errno != EEXIST)
869 return -errno;
870 } else
871 (void) portable_changes_add(changes, n_changes, PORTABLE_MKDIR, dropin_dir, NULL);
872
873 /* We install the drop-ins first, and the actual unit file last to achieve somewhat atomic behaviour if PID 1
874 * is reloaded while we are creating things here: as long as only the drop-ins exist the unit doesn't exist at
875 * all for PID 1. */
876
877 r = install_chroot_dropin(image_path, type, m, dropin_dir, &chroot_dropin, changes, n_changes);
878 if (r < 0)
879 return r;
880
881 r = install_profile_dropin(image_path, m, dropin_dir, profile, flags, &profile_dropin, changes, n_changes);
882 if (r < 0)
883 return r;
884
885 if ((flags & PORTABLE_PREFER_SYMLINK) && m->source) {
886
887 if (symlink(m->source, path) < 0)
888 return log_debug_errno(errno, "Failed to symlink unit file '%s': %m", path);
889
890 (void) portable_changes_add(changes, n_changes, PORTABLE_SYMLINK, path, m->source);
891
892 } else {
893 _cleanup_(unlink_and_freep) char *tmp = NULL;
894 _cleanup_close_ int fd = -1;
895
896 fd = open_tmpfile_linkable(where, O_WRONLY|O_CLOEXEC, &tmp);
897 if (fd < 0)
898 return log_debug_errno(fd, "Failed to create unit file '%s': %m", path);
899
900 r = copy_bytes(m->fd, fd, UINT64_MAX, COPY_REFLINK);
901 if (r < 0)
902 return log_debug_errno(r, "Failed to copy unit file '%s': %m", path);
903
904 if (fchmod(fd, 0644) < 0)
905 return log_debug_errno(errno, "Failed to change unit file access mode for '%s': %m", path);
906
907 r = link_tmpfile(fd, tmp, path);
908 if (r < 0)
909 return log_debug_errno(r, "Failed to install unit file '%s': %m", path);
910
911 tmp = mfree(tmp);
912
913 (void) portable_changes_add(changes, n_changes, PORTABLE_COPY, path, m->source);
914 }
915
916 /* All is established now, now let's disable any rollbacks */
917 chroot_dropin = mfree(chroot_dropin);
918 profile_dropin = mfree(profile_dropin);
919 dropin_dir = mfree(dropin_dir);
920
921 return 0;
922 }
923
924 static int image_symlink(
925 const char *image_path,
926 PortableFlags flags,
927 char **ret) {
928
929 const char *fn, *where;
930 char *joined = NULL;
931
932 assert(image_path);
933 assert(ret);
934
935 fn = last_path_component(image_path);
936
937 if (flags & PORTABLE_RUNTIME)
938 where = "/run/portables/";
939 else
940 where = "/etc/portables/";
941
942 joined = strjoin(where, fn);
943 if (!joined)
944 return -ENOMEM;
945
946 *ret = joined;
947 return 0;
948 }
949
950 static int install_image_symlink(
951 const char *image_path,
952 PortableFlags flags,
953 PortableChange **changes,
954 size_t *n_changes) {
955
956 _cleanup_free_ char *sl = NULL;
957 int r;
958
959 assert(image_path);
960
961 /* If the image is outside of the image search also link it into it, so that it can be found with short image
962 * names and is listed among the images. */
963
964 if (image_in_search_path(IMAGE_PORTABLE, image_path))
965 return 0;
966
967 r = image_symlink(image_path, flags, &sl);
968 if (r < 0)
969 return log_debug_errno(r, "Failed to generate image symlink path: %m");
970
971 (void) mkdir_parents(sl, 0755);
972
973 if (symlink(image_path, sl) < 0)
974 return log_debug_errno(errno, "Failed to link %s %s %s: %m", image_path, special_glyph(ARROW), sl);
975
976 (void) portable_changes_add(changes, n_changes, PORTABLE_SYMLINK, sl, image_path);
977 return 0;
978 }
979
980 int portable_attach(
981 sd_bus *bus,
982 const char *name_or_path,
983 char **matches,
984 const char *profile,
985 PortableFlags flags,
986 PortableChange **changes,
987 size_t *n_changes,
988 sd_bus_error *error) {
989
990 _cleanup_(portable_metadata_hashmap_unrefp) Hashmap *unit_files = NULL;
991 _cleanup_(lookup_paths_free) LookupPaths paths = {};
992 _cleanup_(image_unrefp) Image *image = NULL;
993 PortableMetadata *item;
994 Iterator iterator;
995 int r;
996
997 assert(name_or_path);
998
999 r = image_find_harder(IMAGE_PORTABLE, name_or_path, &image);
1000 if (r < 0)
1001 return r;
1002
1003 r = portable_extract_by_path(image->path, matches, NULL, &unit_files, error);
1004 if (r < 0)
1005 return r;
1006
1007 r = lookup_paths_init(&paths, UNIT_FILE_SYSTEM, LOOKUP_PATHS_SPLIT_USR, NULL);
1008 if (r < 0)
1009 return r;
1010
1011 HASHMAP_FOREACH(item, unit_files, iterator) {
1012 r = unit_file_exists(UNIT_FILE_SYSTEM, &paths, item->name);
1013 if (r < 0)
1014 return sd_bus_error_set_errnof(error, r, "Failed to determine whether unit '%s' exists on the host: %m", item->name);
1015 if (r > 0)
1016 return sd_bus_error_setf(error, BUS_ERROR_UNIT_EXISTS, "Unit file '%s' exists on the host already, refusing.", item->name);
1017
1018 r = unit_file_is_active(bus, item->name, error);
1019 if (r < 0)
1020 return r;
1021 if (r > 0)
1022 return sd_bus_error_setf(error, BUS_ERROR_UNIT_EXISTS, "Unit file '%s' is active already, refusing.", item->name);
1023 }
1024
1025 HASHMAP_FOREACH(item, unit_files, iterator) {
1026 r = attach_unit_file(&paths, image->path, image->type, item, profile, flags, changes, n_changes);
1027 if (r < 0)
1028 return r;
1029 }
1030
1031 /* We don't care too much for the image symlink, it's just a convenience thing, it's not necessary for proper
1032 * operation otherwise. */
1033 (void) install_image_symlink(image->path, flags, changes, n_changes);
1034
1035 return 0;
1036 }
1037
1038 static bool marker_matches_image(const char *marker, const char *name_or_path) {
1039 const char *a;
1040
1041 assert(marker);
1042 assert(name_or_path);
1043
1044 a = last_path_component(marker);
1045
1046 if (image_name_is_valid(name_or_path)) {
1047 const char *e;
1048
1049 /* We shall match against an image name. In that case let's compare the last component, and optionally
1050 * allow either a suffix of ".raw" or a series of "/". */
1051
1052 e = startswith(a, name_or_path);
1053 if (!e)
1054 return false;
1055
1056 return
1057 e[strspn(e, "/")] == 0 ||
1058 streq(e, ".raw");
1059 } else {
1060 const char *b;
1061 size_t l;
1062
1063 /* We shall match against a path. Let's ignore any prefix here though, as often there are many ways to
1064 * reach the same file. However, in this mode, let's validate any file suffix. */
1065
1066 l = strcspn(a, "/");
1067 b = last_path_component(name_or_path);
1068
1069 if (strcspn(b, "/") != l)
1070 return false;
1071
1072 return memcmp(a, b, l) == 0;
1073 }
1074 }
1075
1076 static int test_chroot_dropin(
1077 DIR *d,
1078 const char *where,
1079 const char *fname,
1080 const char *name_or_path,
1081 char **ret_marker) {
1082
1083 _cleanup_free_ char *line = NULL, *marker = NULL;
1084 _cleanup_fclose_ FILE *f = NULL;
1085 _cleanup_close_ int fd = -1;
1086 const char *p, *e, *k;
1087 int r;
1088
1089 assert(d);
1090 assert(where);
1091 assert(fname);
1092
1093 /* We recognize unis created from portable images via the drop-in we created for them */
1094
1095 p = strjoina(fname, ".d/20-portable.conf");
1096 fd = openat(dirfd(d), p, O_RDONLY|O_CLOEXEC);
1097 if (fd < 0) {
1098 if (errno == ENOENT)
1099 return 0;
1100
1101 return log_debug_errno(errno, "Failed to open %s/%s: %m", where, p);
1102 }
1103
1104 f = fdopen(fd, "re");
1105 if (!f)
1106 return log_debug_errno(errno, "Failed to convert file handle: %m");
1107 fd = -1;
1108
1109 (void) __fsetlocking(f, FSETLOCKING_BYCALLER);
1110
1111 r = read_line(f, LONG_LINE_MAX, &line);
1112 if (r < 0)
1113 return log_debug_errno(r, "Failed to read from %s/%s: %m", where, p);
1114
1115 e = startswith(line, PORTABLE_DROPIN_MARKER_BEGIN);
1116 if (!e)
1117 return 0;
1118
1119 k = endswith(e, PORTABLE_DROPIN_MARKER_END);
1120 if (!k)
1121 return 0;
1122
1123 marker = strndup(e, k - e);
1124 if (!marker)
1125 return -ENOMEM;
1126
1127 if (!name_or_path)
1128 r = true;
1129 else
1130 r = marker_matches_image(marker, name_or_path);
1131
1132 if (ret_marker)
1133 *ret_marker = TAKE_PTR(marker);
1134
1135 return r;
1136 }
1137
1138 int portable_detach(
1139 sd_bus *bus,
1140 const char *name_or_path,
1141 PortableFlags flags,
1142 PortableChange **changes,
1143 size_t *n_changes,
1144 sd_bus_error *error) {
1145
1146 _cleanup_(lookup_paths_free) LookupPaths paths = {};
1147 _cleanup_set_free_free_ Set *unit_files = NULL, *markers = NULL;
1148 _cleanup_closedir_ DIR *d = NULL;
1149 const char *where, *item;
1150 Iterator iterator;
1151 struct dirent *de;
1152 int ret = 0;
1153 int r;
1154
1155 assert(name_or_path);
1156
1157 r = lookup_paths_init(&paths, UNIT_FILE_SYSTEM, LOOKUP_PATHS_SPLIT_USR, NULL);
1158 if (r < 0)
1159 return r;
1160
1161 where = attached_path(&paths, flags);
1162
1163 d = opendir(where);
1164 if (!d) {
1165 if (errno == ENOENT)
1166 goto not_found;
1167
1168 return log_debug_errno(errno, "Failed to open '%s' directory: %m", where);
1169 }
1170
1171 unit_files = set_new(&string_hash_ops);
1172 if (!unit_files)
1173 return -ENOMEM;
1174
1175 markers = set_new(&path_hash_ops);
1176 if (!markers)
1177 return -ENOMEM;
1178
1179 FOREACH_DIRENT(de, d, return log_debug_errno(errno, "Failed to enumerate '%s' directory: %m", where)) {
1180 _cleanup_free_ char *marker = NULL;
1181 UnitFileState state;
1182
1183 if (!unit_name_is_valid(de->d_name, UNIT_NAME_ANY))
1184 continue;
1185
1186 /* Filter out duplicates */
1187 if (set_get(unit_files, de->d_name))
1188 continue;
1189
1190 dirent_ensure_type(d, de);
1191 if (!IN_SET(de->d_type, DT_LNK, DT_REG))
1192 continue;
1193
1194 r = test_chroot_dropin(d, where, de->d_name, name_or_path, &marker);
1195 if (r < 0)
1196 return r;
1197 if (r == 0)
1198 continue;
1199
1200 r = unit_file_lookup_state(UNIT_FILE_SYSTEM, &paths, de->d_name, &state);
1201 if (r < 0)
1202 return log_debug_errno(r, "Failed to determine unit file state of '%s': %m", de->d_name);
1203 if (!IN_SET(state, UNIT_FILE_STATIC, UNIT_FILE_DISABLED, UNIT_FILE_LINKED, UNIT_FILE_RUNTIME))
1204 return sd_bus_error_setf(error, BUS_ERROR_UNIT_EXISTS, "Unit file '%s' is in state '%s', can't detach.", de->d_name, unit_file_state_to_string(state));
1205
1206 r = unit_file_is_active(bus, de->d_name, error);
1207 if (r < 0)
1208 return r;
1209 if (r > 0)
1210 return sd_bus_error_setf(error, BUS_ERROR_UNIT_EXISTS, "Unit file '%s' is active, can't detach.", de->d_name);
1211
1212 r = set_put_strdup(unit_files, de->d_name);
1213 if (r < 0)
1214 return log_debug_errno(r, "Failed to add unit name '%s' to set: %m", de->d_name);
1215
1216 if (path_is_absolute(marker) &&
1217 !image_in_search_path(IMAGE_PORTABLE, marker)) {
1218
1219 r = set_ensure_allocated(&markers, &path_hash_ops);
1220 if (r < 0)
1221 return r;
1222
1223 r = set_put(markers, marker);
1224 if (r >= 0)
1225 marker = NULL;
1226 else if (r != -EEXIST)
1227 return r;
1228 }
1229 }
1230
1231 if (set_isempty(unit_files))
1232 goto not_found;
1233
1234 SET_FOREACH(item, unit_files, iterator) {
1235 _cleanup_free_ char *md = NULL;
1236 const char *suffix;
1237
1238 if (unlinkat(dirfd(d), item, 0) < 0) {
1239 log_debug_errno(errno, "Can't remove unit file %s/%s: %m", where, item);
1240
1241 if (errno != ENOENT && ret >= 0)
1242 ret = -errno;
1243 } else
1244 portable_changes_add_with_prefix(changes, n_changes, PORTABLE_UNLINK, where, item, NULL);
1245
1246 FOREACH_STRING(suffix, ".d/10-profile.conf", ".d/20-portable.conf") {
1247 _cleanup_free_ char *dropin = NULL;
1248
1249 dropin = strjoin(item, suffix);
1250 if (!dropin)
1251 return -ENOMEM;
1252
1253 if (unlinkat(dirfd(d), dropin, 0) < 0) {
1254 log_debug_errno(errno, "Can't remove drop-in %s/%s: %m", where, dropin);
1255
1256 if (errno != ENOENT && ret >= 0)
1257 ret = -errno;
1258 } else
1259 portable_changes_add_with_prefix(changes, n_changes, PORTABLE_UNLINK, where, dropin, NULL);
1260 }
1261
1262 md = strjoin(item, ".d");
1263 if (!md)
1264 return -ENOMEM;
1265
1266 if (unlinkat(dirfd(d), md, AT_REMOVEDIR) < 0) {
1267 log_debug_errno(errno, "Can't remove drop-in directory %s/%s: %m", where, md);
1268
1269 if (errno != ENOENT && ret >= 0)
1270 ret = -errno;
1271 } else
1272 portable_changes_add_with_prefix(changes, n_changes, PORTABLE_UNLINK, where, md, NULL);
1273 }
1274
1275 /* Now, also drop any image symlink, for images outside of the sarch path */
1276 SET_FOREACH(item, markers, iterator) {
1277 _cleanup_free_ char *sl = NULL;
1278 struct stat st;
1279
1280 r = image_symlink(item, flags, &sl);
1281 if (r < 0) {
1282 log_debug_errno(r, "Failed to determine image symlink for '%s', ignoring: %m", item);
1283 continue;
1284 }
1285
1286 if (lstat(sl, &st) < 0) {
1287 log_debug_errno(errno, "Failed to stat '%s', ignoring: %m", sl);
1288 continue;
1289 }
1290
1291 if (!S_ISLNK(st.st_mode)) {
1292 log_debug("Image '%s' is not a symlink, ignoring.", sl);
1293 continue;
1294 }
1295
1296 if (unlink(sl) < 0) {
1297 log_debug_errno(errno, "Can't remove image symlink '%s': %m", sl);
1298
1299 if (errno != ENOENT && ret >= 0)
1300 ret = -errno;
1301 } else
1302 portable_changes_add(changes, n_changes, PORTABLE_UNLINK, sl, NULL);
1303 }
1304
1305 /* Try to remove the unit file directory, if we can */
1306 if (rmdir(where) >= 0)
1307 portable_changes_add(changes, n_changes, PORTABLE_UNLINK, where, NULL);
1308
1309 return ret;
1310
1311 not_found:
1312 log_debug("No unit files associated with '%s' found. Image not attached?", name_or_path);
1313 return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_UNIT, "No unit files associated with '%s' found. Image not attached?", name_or_path);
1314 }
1315
1316 static int portable_get_state_internal(
1317 sd_bus *bus,
1318 const char *name_or_path,
1319 PortableFlags flags,
1320 PortableState *ret,
1321 sd_bus_error *error) {
1322
1323 _cleanup_(lookup_paths_free) LookupPaths paths = {};
1324 bool found_enabled = false, found_running = false;
1325 _cleanup_set_free_free_ Set *unit_files = NULL;
1326 _cleanup_closedir_ DIR *d = NULL;
1327 const char *where;
1328 struct dirent *de;
1329 int r;
1330
1331 assert(name_or_path);
1332 assert(ret);
1333
1334 r = lookup_paths_init(&paths, UNIT_FILE_SYSTEM, LOOKUP_PATHS_SPLIT_USR, NULL);
1335 if (r < 0)
1336 return r;
1337
1338 where = attached_path(&paths, flags);
1339
1340 d = opendir(where);
1341 if (!d) {
1342 if (errno == ENOENT) {
1343 /* If the 'attached' directory doesn't exist at all, then we know for sure this image isn't attached. */
1344 *ret = PORTABLE_DETACHED;
1345 return 0;
1346 }
1347
1348 return log_debug_errno(errno, "Failed to open '%s' directory: %m", where);
1349 }
1350
1351 unit_files = set_new(&string_hash_ops);
1352 if (!unit_files)
1353 return -ENOMEM;
1354
1355 FOREACH_DIRENT(de, d, return log_debug_errno(errno, "Failed to enumerate '%s' directory: %m", where)) {
1356 UnitFileState state;
1357
1358 if (!unit_name_is_valid(de->d_name, UNIT_NAME_ANY))
1359 continue;
1360
1361 /* Filter out duplicates */
1362 if (set_get(unit_files, de->d_name))
1363 continue;
1364
1365 dirent_ensure_type(d, de);
1366 if (!IN_SET(de->d_type, DT_LNK, DT_REG))
1367 continue;
1368
1369 r = test_chroot_dropin(d, where, de->d_name, name_or_path, NULL);
1370 if (r < 0)
1371 return r;
1372 if (r == 0)
1373 continue;
1374
1375 r = unit_file_lookup_state(UNIT_FILE_SYSTEM, &paths, de->d_name, &state);
1376 if (r < 0)
1377 return log_debug_errno(r, "Failed to determine unit file state of '%s': %m", de->d_name);
1378 if (!IN_SET(state, UNIT_FILE_STATIC, UNIT_FILE_DISABLED, UNIT_FILE_LINKED, UNIT_FILE_LINKED_RUNTIME))
1379 found_enabled = true;
1380
1381 r = unit_file_is_active(bus, de->d_name, error);
1382 if (r < 0)
1383 return r;
1384 if (r > 0)
1385 found_running = true;
1386
1387 r = set_put_strdup(unit_files, de->d_name);
1388 if (r < 0)
1389 return log_debug_errno(r, "Failed to add unit name '%s' to set: %m", de->d_name);
1390 }
1391
1392 *ret = found_running ? (!set_isempty(unit_files) && (flags & PORTABLE_RUNTIME) ? PORTABLE_RUNNING_RUNTIME : PORTABLE_RUNNING) :
1393 found_enabled ? (flags & PORTABLE_RUNTIME ? PORTABLE_ENABLED_RUNTIME : PORTABLE_ENABLED) :
1394 !set_isempty(unit_files) ? (flags & PORTABLE_RUNTIME ? PORTABLE_ATTACHED_RUNTIME : PORTABLE_ATTACHED) : PORTABLE_DETACHED;
1395
1396 return 0;
1397 }
1398
1399 int portable_get_state(
1400 sd_bus *bus,
1401 const char *name_or_path,
1402 PortableFlags flags,
1403 PortableState *ret,
1404 sd_bus_error *error) {
1405
1406 PortableState state;
1407 int r;
1408
1409 assert(name_or_path);
1410 assert(ret);
1411
1412 /* We look for matching units twice: once in the regular directories, and once in the runtime directories — but
1413 * the latter only if we didn't find anything in the former. */
1414
1415 r = portable_get_state_internal(bus, name_or_path, flags & ~PORTABLE_RUNTIME, &state, error);
1416 if (r < 0)
1417 return r;
1418
1419 if (state == PORTABLE_DETACHED) {
1420 r = portable_get_state_internal(bus, name_or_path, flags | PORTABLE_RUNTIME, &state, error);
1421 if (r < 0)
1422 return r;
1423 }
1424
1425 *ret = state;
1426 return 0;
1427 }
1428
1429 int portable_get_profiles(char ***ret) {
1430 assert(ret);
1431
1432 return conf_files_list_nulstr(ret, NULL, NULL, CONF_FILES_DIRECTORY|CONF_FILES_BASENAME|CONF_FILES_FILTER_MASKED, profile_dirs);
1433 }
1434
1435 static const char* const portable_change_type_table[_PORTABLE_CHANGE_TYPE_MAX] = {
1436 [PORTABLE_COPY] = "copy",
1437 [PORTABLE_MKDIR] = "mkdir",
1438 [PORTABLE_SYMLINK] = "symlink",
1439 [PORTABLE_UNLINK] = "unlink",
1440 [PORTABLE_WRITE] = "write",
1441 };
1442
1443 DEFINE_STRING_TABLE_LOOKUP(portable_change_type, PortableChangeType);
1444
1445 static const char* const portable_state_table[_PORTABLE_STATE_MAX] = {
1446 [PORTABLE_DETACHED] = "detached",
1447 [PORTABLE_ATTACHED] = "attached",
1448 [PORTABLE_ATTACHED_RUNTIME] = "attached-runtime",
1449 [PORTABLE_ENABLED] = "enabled",
1450 [PORTABLE_ENABLED_RUNTIME] = "enabled-runtime",
1451 [PORTABLE_RUNNING] = "running",
1452 [PORTABLE_RUNNING_RUNTIME] = "running-runtime",
1453 };
1454
1455 DEFINE_STRING_TABLE_LOOKUP(portable_state, PortableState);