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