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