| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <linux/loop.h> |
| 4 |
#include <sched.h> |
| 5 |
#include <unistd.h> |
| 6 |
|
| 7 |
#include "sd-bus.h" |
| 8 |
#include "sd-messages.h" |
| 9 |
#include "sd-varlink.h" |
| 10 |
|
| 11 |
#include "bus-common-errors.h" |
| 12 |
#include "bus-error.h" |
| 13 |
#include "bus-locator.h" |
| 14 |
#include "chase.h" |
| 15 |
#include "conf-files.h" |
| 16 |
#include "copy.h" |
| 17 |
#include "cryptsetup-util.h" |
| 18 |
#include "data-fd-util.h" |
| 19 |
#include "dirent-util.h" |
| 20 |
#include "discover-image.h" |
| 21 |
#include "dissect-image.h" |
| 22 |
#include "env-file.h" |
| 23 |
#include "env-util.h" |
| 24 |
#include "errno-util.h" |
| 25 |
#include "escape.h" |
| 26 |
#include "extension-util.h" |
| 27 |
#include "extract-word.h" |
| 28 |
#include "fd-util.h" |
| 29 |
#include "fileio.h" |
| 30 |
#include "fs-util.h" |
| 31 |
#include "glyph-util.h" |
| 32 |
#include "image-policy.h" |
| 33 |
#include "install.h" |
| 34 |
#include "iovec-util.h" |
| 35 |
#include "libmount-util.h" |
| 36 |
#include "log-context.h" |
| 37 |
#include "log.h" |
| 38 |
#include "loop-util.h" |
| 39 |
#include "mkdir.h" |
| 40 |
#include "namespace-util.h" |
| 41 |
#include "nsresource.h" |
| 42 |
#include "os-util.h" |
| 43 |
#include "path-lookup.h" |
| 44 |
#include "pidref.h" |
| 45 |
#include "portable.h" |
| 46 |
#include "portable-util.h" |
| 47 |
#include "process-util.h" |
| 48 |
#include "rm-rf.h" |
| 49 |
#include "selinux-util.h" |
| 50 |
#include "set.h" |
| 51 |
#include "socket-util.h" |
| 52 |
#include "sort-util.h" |
| 53 |
#include "string-table.h" |
| 54 |
#include "strv.h" |
| 55 |
#include "tmpfile-util.h" |
| 56 |
#include "uid-classification.h" |
| 57 |
#include "unit-name.h" |
| 58 |
#include "vpick.h" |
| 59 |
|
| 60 |
/* Markers used in the first line of our 20-portable.conf unit file drop-in to determine, that a) the unit file was |
| 61 |
* dropped there by the portable service logic and b) for which image it was dropped there. */ |
| 62 |
#define PORTABLE_DROPIN_MARKER_BEGIN "# Drop-in created for image '" |
| 63 |
#define PORTABLE_DROPIN_MARKER_END "', do not edit." |
| 64 |
|
| 65 |
static bool prefix_match(const char *unit, const char *prefix) { |
| 66 |
const char *p; |
| 67 |
|
| 68 |
p = startswith(unit, prefix); |
| 69 |
if (!p) |
| 70 |
return false; |
| 71 |
|
| 72 |
/* Only respect prefixes followed by dash or dot or when there's a complete match */ |
| 73 |
return IN_SET(*p, '-', '.', '@', 0); |
| 74 |
} |
| 75 |
|
| 76 |
static bool unit_match(const char *unit, char **matches) { |
| 77 |
const char *dot; |
| 78 |
|
| 79 |
dot = strrchr(unit, '.'); |
| 80 |
if (!dot) |
| 81 |
return false; |
| 82 |
|
| 83 |
if (!STR_IN_SET(dot, ".service", ".socket", ".target", ".timer", ".path")) |
| 84 |
return false; |
| 85 |
|
| 86 |
/* Empty match expression means: everything */ |
| 87 |
if (strv_isempty(matches)) |
| 88 |
return true; |
| 89 |
|
| 90 |
/* Otherwise, at least one needs to match */ |
| 91 |
STRV_FOREACH(i, matches) |
| 92 |
if (prefix_match(unit, *i)) |
| 93 |
return true; |
| 94 |
|
| 95 |
return false; |
| 96 |
} |
| 97 |
|
| 98 |
static PortableMetadata *portable_metadata_new(const char *name, const char *path, const char *selinux_label, int fd) { |
| 99 |
PortableMetadata *m; |
| 100 |
|
| 101 |
m = malloc0(offsetof(PortableMetadata, name) + strlen(name) + 1); |
| 102 |
if (!m) |
| 103 |
return NULL; |
| 104 |
|
| 105 |
/* In case of a layered attach, we want to remember which image the unit came from */ |
| 106 |
if (path) { |
| 107 |
m->image_path = strdup(path); |
| 108 |
if (!m->image_path) |
| 109 |
return mfree(m); |
| 110 |
} |
| 111 |
|
| 112 |
/* The metadata file might have SELinux labels, we need to carry them and reapply them */ |
| 113 |
if (!isempty(selinux_label)) { |
| 114 |
m->selinux_label = strdup(selinux_label); |
| 115 |
if (!m->selinux_label) { |
| 116 |
free(m->image_path); |
| 117 |
return mfree(m); |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
strcpy(m->name, name); |
| 122 |
m->fd = fd; |
| 123 |
|
| 124 |
return TAKE_PTR(m); |
| 125 |
} |
| 126 |
|
| 127 |
PortableMetadata *portable_metadata_unref(PortableMetadata *i) { |
| 128 |
if (!i) |
| 129 |
return NULL; |
| 130 |
|
| 131 |
safe_close(i->fd); |
| 132 |
free(i->source); |
| 133 |
free(i->image_path); |
| 134 |
free(i->selinux_label); |
| 135 |
|
| 136 |
return mfree(i); |
| 137 |
} |
| 138 |
|
| 139 |
static int compare_metadata(PortableMetadata *const *x, PortableMetadata *const *y) { |
| 140 |
assert(x); |
| 141 |
assert(y); |
| 142 |
|
| 143 |
return strcmp((*x)->name, (*y)->name); |
| 144 |
} |
| 145 |
|
| 146 |
int portable_metadata_hashmap_to_sorted_array(Hashmap *unit_files, PortableMetadata ***ret) { |
| 147 |
|
| 148 |
_cleanup_free_ PortableMetadata **sorted = NULL; |
| 149 |
PortableMetadata *item; |
| 150 |
size_t k = 0; |
| 151 |
|
| 152 |
assert(ret); |
| 153 |
|
| 154 |
sorted = new(PortableMetadata*, hashmap_size(unit_files)); |
| 155 |
if (!sorted) |
| 156 |
return -ENOMEM; |
| 157 |
|
| 158 |
HASHMAP_FOREACH(item, unit_files) |
| 159 |
sorted[k++] = item; |
| 160 |
|
| 161 |
assert(k == hashmap_size(unit_files)); |
| 162 |
|
| 163 |
typesafe_qsort(sorted, k, compare_metadata); |
| 164 |
|
| 165 |
*ret = TAKE_PTR(sorted); |
| 166 |
return 0; |
| 167 |
} |
| 168 |
|
| 169 |
static int send_one_fd_iov_with_data_fd( |
| 170 |
int socket_fd, |
| 171 |
const struct iovec *iov, |
| 172 |
size_t iovlen, |
| 173 |
int fd) { |
| 174 |
|
| 175 |
_cleanup_close_ int data_fd = -EBADF; |
| 176 |
|
| 177 |
assert(iov || iovlen == 0); |
| 178 |
assert(socket_fd >= 0); |
| 179 |
assert(fd >= 0); |
| 180 |
|
| 181 |
data_fd = copy_data_fd(fd); |
| 182 |
if (data_fd < 0) |
| 183 |
return data_fd; |
| 184 |
|
| 185 |
return send_one_fd_iov(socket_fd, data_fd, iov, iovlen, 0); |
| 186 |
} |
| 187 |
|
| 188 |
DEFINE_PRIVATE_HASH_OPS_WITH_VALUE_DESTRUCTOR(portable_metadata_hash_ops, char, string_hash_func, string_compare_func, |
| 189 |
PortableMetadata, portable_metadata_unref); |
| 190 |
|
| 191 |
static int receive_portable_metadata( |
| 192 |
int socket_fd, |
| 193 |
const char *path, |
| 194 |
PortableMetadata **ret_os_release, |
| 195 |
Hashmap **ret_unit_files) { |
| 196 |
|
| 197 |
_cleanup_(portable_metadata_unrefp) PortableMetadata* os_release = NULL; |
| 198 |
_cleanup_(hashmap_freep) Hashmap *unit_files = NULL; |
| 199 |
int r; |
| 200 |
|
| 201 |
assert(socket_fd >= 0); |
| 202 |
assert(path); |
| 203 |
assert(ret_os_release); |
| 204 |
assert(ret_unit_files); |
| 205 |
|
| 206 |
unit_files = hashmap_new(&portable_metadata_hash_ops); |
| 207 |
if (!unit_files) |
| 208 |
return -ENOMEM; |
| 209 |
|
| 210 |
for (;;) { |
| 211 |
_cleanup_(portable_metadata_unrefp) PortableMetadata *add = NULL; |
| 212 |
_cleanup_close_ int fd = -EBADF; |
| 213 |
/* We use NAME_MAX space for the SELinux label here. The kernel currently enforces no limit, |
| 214 |
* but according to suggestions from the SELinux people this will change and it will probably |
| 215 |
* be identical to NAME_MAX. For now we use that, but this should be updated one day when the |
| 216 |
* final limit is known. */ |
| 217 |
char iov_buffer[PATH_MAX + NAME_MAX + 2]; |
| 218 |
struct iovec iov = IOVEC_MAKE(iov_buffer, sizeof(iov_buffer)); |
| 219 |
|
| 220 |
ssize_t n = receive_one_fd_iov(socket_fd, &iov, 1, 0, &fd); |
| 221 |
if (n == -EIO) |
| 222 |
break; |
| 223 |
if (n < 0) |
| 224 |
return log_debug_errno(n, "Failed to receive item: %m"); |
| 225 |
iov_buffer[n] = 0; |
| 226 |
|
| 227 |
/* We can't really distinguish a zero-length datagram without any fds from EOF (both are |
| 228 |
* signalled the same way by recvmsg()). Hence, accept either as end notification. */ |
| 229 |
if (isempty(iov_buffer) && fd < 0) |
| 230 |
break; |
| 231 |
|
| 232 |
if (isempty(iov_buffer) || fd < 0) |
| 233 |
return log_debug_errno(SYNTHETIC_ERRNO(EINVAL), |
| 234 |
"Invalid item sent from child."); |
| 235 |
|
| 236 |
/* Given recvmsg cannot be used with multiple io vectors if you don't know the size in |
| 237 |
* advance, use a marker to separate the name and the optional SELinux context. */ |
| 238 |
char *selinux_label = memchr(iov_buffer, 0, n); |
| 239 |
assert(selinux_label); |
| 240 |
selinux_label++; |
| 241 |
|
| 242 |
add = portable_metadata_new(iov_buffer, path, selinux_label, fd); |
| 243 |
if (!add) |
| 244 |
return -ENOMEM; |
| 245 |
fd = -EBADF; |
| 246 |
|
| 247 |
/* Note that we do not initialize 'add->source' here, as the source path is not usable here |
| 248 |
* as it refers to a path only valid in the short-living namespaced child process we forked |
| 249 |
* here. */ |
| 250 |
|
| 251 |
if (PORTABLE_METADATA_IS_UNIT(add)) { |
| 252 |
r = hashmap_put(unit_files, add->name, add); |
| 253 |
if (r < 0) |
| 254 |
return log_debug_errno(r, "Failed to add item to unit file list: %m"); |
| 255 |
|
| 256 |
add = NULL; |
| 257 |
|
| 258 |
} else if (PORTABLE_METADATA_IS_OS_RELEASE(add) || PORTABLE_METADATA_IS_EXTENSION_RELEASE(add)) { |
| 259 |
|
| 260 |
assert(!os_release); |
| 261 |
os_release = TAKE_PTR(add); |
| 262 |
} else |
| 263 |
assert_not_reached(); |
| 264 |
} |
| 265 |
|
| 266 |
*ret_os_release = TAKE_PTR(os_release); |
| 267 |
*ret_unit_files = TAKE_PTR(unit_files); |
| 268 |
return 0; |
| 269 |
} |
| 270 |
|
| 271 |
static int extract_now( |
| 272 |
RuntimeScope scope, |
| 273 |
int rfd, |
| 274 |
const char *image_path, |
| 275 |
char **matches, |
| 276 |
const char *image_name, |
| 277 |
bool path_is_extension, |
| 278 |
bool relax_extension_release_check, |
| 279 |
int socket_fd, |
| 280 |
PortableMetadata **ret_os_release, |
| 281 |
Hashmap **ret_unit_files) { |
| 282 |
|
| 283 |
_cleanup_hashmap_free_ Hashmap *unit_files = NULL; |
| 284 |
_cleanup_(portable_metadata_unrefp) PortableMetadata *os_release = NULL; |
| 285 |
_cleanup_(lookup_paths_done) LookupPaths paths = {}; |
| 286 |
_cleanup_close_ int os_release_fd = -EBADF; |
| 287 |
_cleanup_free_ char *os_release_path = NULL; |
| 288 |
const char *os_release_id; |
| 289 |
int r; |
| 290 |
|
| 291 |
/* Extracts the metadata from a directory tree 'dir_fd'. Extracts two kinds of information: the /etc/os-release |
| 292 |
* data, and all unit files matching the specified expression. Note that this function is called in two very |
| 293 |
* different but also similar contexts. When the tool gets invoked on a directory tree, we'll process it |
| 294 |
* directly, and in-process, and thus can return the requested data directly, via 'ret_os_release' and |
| 295 |
* 'ret_unit_files'. However, if the tool is invoked on a raw disk image — which needs to be mounted first — we |
| 296 |
* are invoked in a child process with private mounts and then need to send the collected data to our |
| 297 |
* parent. To handle both cases in one call this function also gets a 'socket_fd' parameter, which when >= 0 is |
| 298 |
* used to send the data to the parent. */ |
| 299 |
|
| 300 |
assert(scope < _RUNTIME_SCOPE_MAX); |
| 301 |
assert(rfd >= 0); |
| 302 |
|
| 303 |
/* First, find os-release/extension-release and send it upstream (or just save it). */ |
| 304 |
if (path_is_extension) { |
| 305 |
ImageClass class = IMAGE_SYSEXT; |
| 306 |
|
| 307 |
r = open_extension_release_at(rfd, IMAGE_SYSEXT, image_name, relax_extension_release_check, &os_release_path, &os_release_fd); |
| 308 |
if (r == -ENOENT) { |
| 309 |
r = open_extension_release_at(rfd, IMAGE_CONFEXT, image_name, relax_extension_release_check, &os_release_path, &os_release_fd); |
| 310 |
if (r >= 0) |
| 311 |
class = IMAGE_CONFEXT; |
| 312 |
} |
| 313 |
if (r < 0) |
| 314 |
return log_error_errno(r, "Failed to open extension release from '%s': %m", image_name); |
| 315 |
|
| 316 |
os_release_id = strjoina((class == IMAGE_SYSEXT) ? "/usr/lib" : "/etc", "/extension-release.d/extension-release.", image_name); |
| 317 |
} else { |
| 318 |
os_release_id = "/etc/os-release"; |
| 319 |
r = open_os_release_at(rfd, &os_release_path, &os_release_fd); |
| 320 |
} |
| 321 |
if (r < 0) |
| 322 |
log_debug_errno(r, |
| 323 |
"Couldn't acquire %s file, ignoring: %m", |
| 324 |
path_is_extension ? "extension-release " : "os-release"); |
| 325 |
else { |
| 326 |
if (socket_fd >= 0) { |
| 327 |
struct iovec iov[] = { |
| 328 |
IOVEC_MAKE_STRING(os_release_id), |
| 329 |
IOVEC_MAKE((char *)"\0", sizeof(char)), |
| 330 |
}; |
| 331 |
|
| 332 |
r = send_one_fd_iov_with_data_fd(socket_fd, iov, ELEMENTSOF(iov), os_release_fd); |
| 333 |
if (r < 0) |
| 334 |
return log_debug_errno(r, "Failed to send os-release file: %m"); |
| 335 |
} |
| 336 |
|
| 337 |
if (ret_os_release) { |
| 338 |
os_release = portable_metadata_new(os_release_id, NULL, NULL, os_release_fd); |
| 339 |
if (!os_release) |
| 340 |
return -ENOMEM; |
| 341 |
|
| 342 |
os_release_fd = -EBADF; |
| 343 |
os_release->source = TAKE_PTR(os_release_path); |
| 344 |
} |
| 345 |
} |
| 346 |
|
| 347 |
/* Then, send unit file data to the parent (or/and add it to the hashmap). For that we use our usual |
| 348 |
* unit discovery logic. If we're running in a user session, we look for units in |
| 349 |
* /usr/lib/systemd/user/ and corresponding directories. */ |
| 350 |
r = lookup_paths_init( |
| 351 |
&paths, |
| 352 |
scope == RUNTIME_SCOPE_USER ? RUNTIME_SCOPE_GLOBAL : RUNTIME_SCOPE_SYSTEM, |
| 353 |
LOOKUP_PATHS_SPLIT_USR, |
| 354 |
/* root_dir= */ NULL); |
| 355 |
if (r < 0) |
| 356 |
return log_debug_errno(r, "Failed to acquire lookup paths: %m"); |
| 357 |
|
| 358 |
unit_files = hashmap_new(&portable_metadata_hash_ops); |
| 359 |
if (!unit_files) |
| 360 |
return -ENOMEM; |
| 361 |
|
| 362 |
STRV_FOREACH(i, paths.search_path) { |
| 363 |
_cleanup_free_ char *relative = NULL, *resolved = NULL; |
| 364 |
_cleanup_closedir_ DIR *d = NULL; |
| 365 |
|
| 366 |
r = chase_and_opendirat(rfd, rfd, *i, /* chase_flags= */ 0, &relative, &d); |
| 367 |
if (r < 0) { |
| 368 |
log_debug_errno(r, "Failed to open unit path '%s', ignoring: %m", *i); |
| 369 |
continue; |
| 370 |
} |
| 371 |
|
| 372 |
r = chaseat_prefix_root(relative, image_path, &resolved); |
| 373 |
if (r < 0) |
| 374 |
return r; |
| 375 |
|
| 376 |
FOREACH_DIRENT(de, d, return log_debug_errno(errno, "Failed to read directory: %m")) { |
| 377 |
_cleanup_(portable_metadata_unrefp) PortableMetadata *m = NULL; |
| 378 |
_cleanup_freecon_ char *con = NULL; |
| 379 |
_cleanup_close_ int fd = -EBADF; |
| 380 |
struct stat st; |
| 381 |
|
| 382 |
if (!unit_name_is_valid(de->d_name, UNIT_NAME_ANY)) |
| 383 |
continue; |
| 384 |
|
| 385 |
if (!unit_match(de->d_name, matches)) |
| 386 |
continue; |
| 387 |
|
| 388 |
/* Filter out duplicates */ |
| 389 |
if (hashmap_contains(unit_files, de->d_name)) |
| 390 |
continue; |
| 391 |
|
| 392 |
if (!IN_SET(de->d_type, DT_LNK, DT_REG)) |
| 393 |
continue; |
| 394 |
|
| 395 |
fd = openat(dirfd(d), de->d_name, O_CLOEXEC|O_RDONLY); |
| 396 |
if (fd < 0) { |
| 397 |
log_debug_errno(errno, "Failed to open unit file '%s', ignoring: %m", de->d_name); |
| 398 |
continue; |
| 399 |
} |
| 400 |
|
| 401 |
/* Reject empty files, just in case */ |
| 402 |
if (fstat(fd, &st) < 0) { |
| 403 |
log_debug_errno(errno, "Failed to stat unit file '%s', ignoring: %m", de->d_name); |
| 404 |
continue; |
| 405 |
} |
| 406 |
|
| 407 |
if (st.st_size <= 0) { |
| 408 |
log_debug("Unit file '%s' is empty, ignoring.", de->d_name); |
| 409 |
continue; |
| 410 |
} |
| 411 |
|
| 412 |
#if HAVE_SELINUX |
| 413 |
/* The units will be copied on the host's filesystem, so if they had a SELinux label |
| 414 |
* we have to preserve it. Copy it out so that it can be applied later. */ |
| 415 |
if (mac_selinux_use()) { |
| 416 |
r = sym_fgetfilecon_raw(fd, &con); |
| 417 |
if (r < 0 && !ERRNO_IS_XATTR_ABSENT(errno)) |
| 418 |
log_debug_errno(errno, "Failed to get SELinux file context from '%s', ignoring: %m", de->d_name); |
| 419 |
} |
| 420 |
#endif |
| 421 |
|
| 422 |
if (socket_fd >= 0) { |
| 423 |
struct iovec iov[] = { |
| 424 |
IOVEC_MAKE_STRING(de->d_name), |
| 425 |
IOVEC_MAKE((char *)"\0", sizeof(char)), |
| 426 |
IOVEC_MAKE_STRING(strempty(con)), |
| 427 |
}; |
| 428 |
|
| 429 |
r = send_one_fd_iov_with_data_fd(socket_fd, iov, ELEMENTSOF(iov), fd); |
| 430 |
if (r < 0) |
| 431 |
return log_debug_errno(r, "Failed to send unit metadata to parent: %m"); |
| 432 |
} |
| 433 |
|
| 434 |
m = portable_metadata_new(de->d_name, image_path, con, fd); |
| 435 |
if (!m) |
| 436 |
return -ENOMEM; |
| 437 |
fd = -EBADF; |
| 438 |
|
| 439 |
m->source = path_join(resolved, de->d_name); |
| 440 |
if (!m->source) |
| 441 |
return -ENOMEM; |
| 442 |
|
| 443 |
r = hashmap_put(unit_files, m->name, m); |
| 444 |
if (r < 0) |
| 445 |
return log_debug_errno(r, "Failed to add unit to hashmap: %m"); |
| 446 |
m = NULL; |
| 447 |
} |
| 448 |
} |
| 449 |
|
| 450 |
if (ret_os_release) |
| 451 |
*ret_os_release = TAKE_PTR(os_release); |
| 452 |
if (ret_unit_files) |
| 453 |
*ret_unit_files = TAKE_PTR(unit_files); |
| 454 |
|
| 455 |
return 0; |
| 456 |
} |
| 457 |
|
| 458 |
static int portable_extract_by_path( |
| 459 |
RuntimeScope scope, |
| 460 |
const char *path, |
| 461 |
bool path_is_extension, |
| 462 |
bool relax_extension_release_check, |
| 463 |
char **matches, |
| 464 |
const ImagePolicy *image_policy, |
| 465 |
PortableMetadata **ret_os_release, |
| 466 |
Hashmap **ret_unit_files, |
| 467 |
ImagePolicy **ret_pinned_image_policy, |
| 468 |
sd_bus_error *error) { |
| 469 |
|
| 470 |
_cleanup_hashmap_free_ Hashmap *unit_files = NULL; |
| 471 |
_cleanup_(portable_metadata_unrefp) PortableMetadata* os_release = NULL; |
| 472 |
_cleanup_(image_policy_freep) ImagePolicy *pinned_image_policy = NULL; |
| 473 |
int r; |
| 474 |
|
| 475 |
assert(path); |
| 476 |
|
| 477 |
_cleanup_close_ int rfd = open(path, O_PATH|O_CLOEXEC); |
| 478 |
if (rfd < 0) |
| 479 |
return log_debug_errno(errno, "Failed to open '%s': %m", path); |
| 480 |
|
| 481 |
struct stat st; |
| 482 |
if (fstat(rfd, &st) < 0) |
| 483 |
return log_debug_errno(errno, "Failed to stat '%s': %m", path); |
| 484 |
|
| 485 |
if (S_ISDIR(st.st_mode)) { |
| 486 |
_cleanup_free_ char *image_name = NULL; |
| 487 |
r = path_extract_filename(path, &image_name); |
| 488 |
if (r < 0) |
| 489 |
return log_debug_errno(r, "Failed to extract image name from path '%s': %m", path); |
| 490 |
|
| 491 |
if (scope == RUNTIME_SCOPE_USER && uid_is_foreign(st.st_uid)) { |
| 492 |
_cleanup_close_ int userns_fd = nsresource_allocate_userns( |
| 493 |
/* vl= */ NULL, |
| 494 |
/* name= */ NULL, |
| 495 |
NSRESOURCE_UIDS_64K); |
| 496 |
if (userns_fd < 0) |
| 497 |
return log_debug_errno(userns_fd, "Failed to allocate user namespace: %m"); |
| 498 |
|
| 499 |
_cleanup_close_ int mfd = -EBADF; |
| 500 |
r = mountfsd_mount_directory_fd( |
| 501 |
/* vl= */ NULL, |
| 502 |
rfd, |
| 503 |
userns_fd, |
| 504 |
DISSECT_IMAGE_FOREIGN_UID, |
| 505 |
&mfd); |
| 506 |
if (r < 0) |
| 507 |
return r; |
| 508 |
|
| 509 |
_cleanup_close_pair_ int seq[2] = EBADF_PAIR; |
| 510 |
if (socketpair(AF_UNIX, SOCK_SEQPACKET|SOCK_CLOEXEC, 0, seq) < 0) |
| 511 |
return log_debug_errno(errno, "Failed to allocated SOCK_SEQPACKET socket: %m"); |
| 512 |
|
| 513 |
_cleanup_close_pair_ int errno_pipe_fd[2] = EBADF_PAIR; |
| 514 |
if (pipe2(errno_pipe_fd, O_CLOEXEC|O_NONBLOCK) < 0) |
| 515 |
return log_debug_errno(errno, "Failed to create pipe: %m"); |
| 516 |
|
| 517 |
_cleanup_(pidref_done_sigkill_wait) PidRef child = PIDREF_NULL; |
| 518 |
r = pidref_safe_fork("(sd-extract)", |
| 519 |
FORK_RESET_SIGNALS|FORK_DEATHSIG_SIGKILL|FORK_REOPEN_LOG, |
| 520 |
&child); |
| 521 |
if (r < 0) |
| 522 |
return r; |
| 523 |
if (r == 0) { |
| 524 |
seq[0] = safe_close(seq[0]); |
| 525 |
errno_pipe_fd[0] = safe_close(errno_pipe_fd[0]); |
| 526 |
|
| 527 |
if (setns(userns_fd, CLONE_NEWUSER) < 0) { |
| 528 |
r = log_debug_errno(errno, "Failed to join userns: %m"); |
| 529 |
report_errno_and_exit(errno_pipe_fd[1], r); |
| 530 |
} |
| 531 |
|
| 532 |
r = extract_now(scope, |
| 533 |
mfd, |
| 534 |
path, |
| 535 |
matches, |
| 536 |
image_name, |
| 537 |
path_is_extension, |
| 538 |
/* relax_extension_release_check= */ false, |
| 539 |
seq[1], |
| 540 |
/* ret_os_release= */ NULL, |
| 541 |
/* ret_unit_files= */ NULL); |
| 542 |
report_errno_and_exit(errno_pipe_fd[1], r); |
| 543 |
} |
| 544 |
|
| 545 |
seq[1] = safe_close(seq[1]); |
| 546 |
errno_pipe_fd[1] = safe_close(errno_pipe_fd[1]); |
| 547 |
|
| 548 |
r = receive_portable_metadata(seq[0], path, &os_release, &unit_files); |
| 549 |
if (r < 0) |
| 550 |
return r; |
| 551 |
|
| 552 |
r = pidref_wait_for_terminate_and_check("(sd-extract)", &child, 0); |
| 553 |
if (r < 0) |
| 554 |
return r; |
| 555 |
if (r != EXIT_SUCCESS) { |
| 556 |
if (read(errno_pipe_fd[0], &r, sizeof(r)) == sizeof(r)) |
| 557 |
return log_debug_errno(r, "Failed to extract portable metadata from '%s': %m", path); |
| 558 |
|
| 559 |
return log_debug_errno(SYNTHETIC_ERRNO(EPROTO), "Child failed."); |
| 560 |
} |
| 561 |
} else { |
| 562 |
r = extract_now(scope, |
| 563 |
rfd, |
| 564 |
path, |
| 565 |
matches, |
| 566 |
image_name, |
| 567 |
path_is_extension, |
| 568 |
/* relax_extension_release_check= */ false, |
| 569 |
/* socket_fd= */ -EBADF, |
| 570 |
&os_release, |
| 571 |
&unit_files); |
| 572 |
if (r < 0) |
| 573 |
return r; |
| 574 |
} |
| 575 |
} else { |
| 576 |
_cleanup_(dissected_image_unrefp) DissectedImage *m = NULL; |
| 577 |
_cleanup_(rmdir_and_freep) char *tmpdir = NULL; |
| 578 |
_cleanup_close_pair_ int seq[2] = EBADF_PAIR, errno_pipe_fd[2] = EBADF_PAIR; |
| 579 |
_cleanup_(pidref_done_sigkill_wait) PidRef child = PIDREF_NULL; |
| 580 |
_cleanup_close_ int userns_fd = -EBADF; |
| 581 |
DissectImageFlags flags = |
| 582 |
DISSECT_IMAGE_READ_ONLY | |
| 583 |
DISSECT_IMAGE_GENERIC_ROOT | |
| 584 |
DISSECT_IMAGE_REQUIRE_ROOT | |
| 585 |
DISSECT_IMAGE_DISCARD_ON_LOOP | |
| 586 |
DISSECT_IMAGE_RELAX_VAR_CHECK | |
| 587 |
DISSECT_IMAGE_USR_NO_ROOT | |
| 588 |
DISSECT_IMAGE_ADD_PARTITION_DEVICES | |
| 589 |
DISSECT_IMAGE_PIN_PARTITION_DEVICES | |
| 590 |
DISSECT_IMAGE_ALLOW_USERSPACE_VERITY; |
| 591 |
|
| 592 |
if (path_is_extension) |
| 593 |
flags |= DISSECT_IMAGE_VALIDATE_OS_EXT | (relax_extension_release_check ? DISSECT_IMAGE_RELAX_EXTENSION_CHECK : 0); |
| 594 |
else |
| 595 |
flags |= DISSECT_IMAGE_VALIDATE_OS; |
| 596 |
|
| 597 |
_cleanup_(verity_settings_done) VeritySettings verity = VERITY_SETTINGS_DEFAULT; |
| 598 |
r = verity_settings_load( |
| 599 |
&verity, |
| 600 |
path, |
| 601 |
/* root_hash_path= */ NULL, |
| 602 |
/* root_hash_sig_path= */ NULL); |
| 603 |
if (r < 0) |
| 604 |
return log_debug_errno(r, "Failed to read verity artifacts for %s: %m", path); |
| 605 |
|
| 606 |
if (verity.data_path) |
| 607 |
flags |= DISSECT_IMAGE_NO_PARTITION_TABLE; |
| 608 |
|
| 609 |
/* We now have a loopback block device, let's fork off a child in its own mount namespace, mount it |
| 610 |
* there, and extract the metadata we need. The metadata is sent from the child back to us. */ |
| 611 |
|
| 612 |
/* Load some libraries before we fork workers off that want to use them */ |
| 613 |
(void) DLOPEN_CRYPTSETUP(LOG_DEBUG, SD_ELF_NOTE_DLOPEN_PRIORITY_RECOMMENDED); |
| 614 |
(void) DLOPEN_LIBMOUNT(LOG_DEBUG, SD_ELF_NOTE_DLOPEN_PRIORITY_REQUIRED); |
| 615 |
|
| 616 |
r = mkdtemp_malloc("/tmp/inspect-XXXXXX", &tmpdir); |
| 617 |
if (r < 0) |
| 618 |
return log_debug_errno(r, "Failed to create temporary directory: %m"); |
| 619 |
|
| 620 |
if (scope == RUNTIME_SCOPE_USER) { |
| 621 |
userns_fd = nsresource_allocate_userns( |
| 622 |
/* vl= */ NULL, |
| 623 |
/* name= */ NULL, |
| 624 |
NSRESOURCE_UIDS_64K); |
| 625 |
if (userns_fd < 0) |
| 626 |
return log_debug_errno(userns_fd, "Failed to allocate user namespace: %m"); |
| 627 |
|
| 628 |
r = mountfsd_mount_image_fd( |
| 629 |
/* vl= */ NULL, |
| 630 |
rfd, |
| 631 |
userns_fd, |
| 632 |
/* options= */ NULL, |
| 633 |
image_policy, |
| 634 |
&verity, |
| 635 |
flags, |
| 636 |
&m); |
| 637 |
if (r < 0) |
| 638 |
return r; |
| 639 |
} else { |
| 640 |
_cleanup_(loop_device_unrefp) LoopDevice *d = NULL; |
| 641 |
|
| 642 |
r = loop_device_make_by_path_at( |
| 643 |
rfd, |
| 644 |
/* path= */ NULL, |
| 645 |
O_RDONLY, |
| 646 |
/* sector_size= */ UINT32_MAX, |
| 647 |
LO_FLAGS_PARTSCAN, |
| 648 |
LOCK_SH, |
| 649 |
&d); |
| 650 |
if (r < 0) |
| 651 |
return log_debug_errno(r, "Failed to set up loopback device for %s: %m", path); |
| 652 |
|
| 653 |
r = dissect_loop_device( |
| 654 |
d, |
| 655 |
&verity, |
| 656 |
/* mount_options= */ NULL, |
| 657 |
image_policy, |
| 658 |
/* image_filter= */ NULL, |
| 659 |
flags, |
| 660 |
&m); |
| 661 |
if (r == -ENOPKG) |
| 662 |
sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Couldn't identify a suitable partition table or file system in '%s'.", path); |
| 663 |
else if (r == -EADDRNOTAVAIL) |
| 664 |
sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "No root partition for specified root hash found in '%s'.", path); |
| 665 |
else if (r == -ENOTUNIQ) |
| 666 |
sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Multiple suitable root partitions found in image '%s'.", path); |
| 667 |
else if (r == -ENXIO) |
| 668 |
sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "No suitable root partition found in image '%s'.", path); |
| 669 |
else if (r == -EPROTONOSUPPORT) |
| 670 |
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); |
| 671 |
if (r < 0) |
| 672 |
return r; |
| 673 |
|
| 674 |
r = dissected_image_load_verity_sig_partition(m, d->fd, &verity); |
| 675 |
if (r < 0) |
| 676 |
return log_debug_errno(r, "Failed to load verity sig partition for '%s': %m", path); |
| 677 |
|
| 678 |
r = dissected_image_guess_verity_roothash(m, &verity); |
| 679 |
if (r < 0) |
| 680 |
return log_debug_errno(r, "Failed to guess verity roothash for '%s': %m", path); |
| 681 |
} |
| 682 |
|
| 683 |
if (!m->image_name) { |
| 684 |
r = dissected_image_name_from_path(path, &m->image_name); |
| 685 |
if (r < 0) |
| 686 |
return r; |
| 687 |
} |
| 688 |
|
| 689 |
if (ret_pinned_image_policy) { |
| 690 |
pinned_image_policy = image_policy_new_from_dissected(m, &verity); |
| 691 |
if (!pinned_image_policy) |
| 692 |
return -ENOMEM; |
| 693 |
} |
| 694 |
|
| 695 |
if (socketpair(AF_UNIX, SOCK_SEQPACKET|SOCK_CLOEXEC, 0, seq) < 0) |
| 696 |
return log_debug_errno(errno, "Failed to allocated SOCK_SEQPACKET socket: %m"); |
| 697 |
|
| 698 |
if (pipe2(errno_pipe_fd, O_CLOEXEC|O_NONBLOCK) < 0) |
| 699 |
return log_debug_errno(errno, "Failed to create pipe: %m"); |
| 700 |
|
| 701 |
r = pidref_safe_fork( |
| 702 |
"(sd-dissect)", |
| 703 |
FORK_RESET_SIGNALS|FORK_DEATHSIG_SIGKILL|(scope == RUNTIME_SCOPE_SYSTEM ? FORK_NEW_MOUNTNS|FORK_MOUNTNS_SLAVE : 0), |
| 704 |
&child); |
| 705 |
if (r < 0) |
| 706 |
return r; |
| 707 |
if (r == 0) { |
| 708 |
seq[0] = safe_close(seq[0]); |
| 709 |
errno_pipe_fd[0] = safe_close(errno_pipe_fd[0]); |
| 710 |
|
| 711 |
if (scope == RUNTIME_SCOPE_USER) { |
| 712 |
r = detach_mount_namespace_userns(userns_fd); |
| 713 |
if (r < 0) { |
| 714 |
log_debug_errno(r, "Failed to detach mount namespace: %m"); |
| 715 |
report_errno_and_exit(errno_pipe_fd[1], r); |
| 716 |
} |
| 717 |
} |
| 718 |
|
| 719 |
r = dissected_image_mount( |
| 720 |
m, |
| 721 |
tmpdir, |
| 722 |
/* uid_shift= */ UID_INVALID, |
| 723 |
/* uid_range= */ UID_INVALID, |
| 724 |
userns_fd, |
| 725 |
flags); |
| 726 |
if (r < 0) { |
| 727 |
log_debug_errno(r, "Failed to mount dissected image '%s': %m", path); |
| 728 |
report_errno_and_exit(errno_pipe_fd[1], r); |
| 729 |
} |
| 730 |
|
| 731 |
_cleanup_close_ int mfd = open(tmpdir, O_DIRECTORY|O_CLOEXEC); |
| 732 |
if (mfd < 0) { |
| 733 |
r = log_debug_errno(errno, "Failed to open '%s': %m", tmpdir); |
| 734 |
report_errno_and_exit(errno_pipe_fd[1], r); |
| 735 |
} |
| 736 |
|
| 737 |
r = extract_now(scope, |
| 738 |
mfd, |
| 739 |
path, |
| 740 |
matches, |
| 741 |
m->image_name, |
| 742 |
path_is_extension, |
| 743 |
relax_extension_release_check, |
| 744 |
seq[1], |
| 745 |
/* ret_os_release= */ NULL, |
| 746 |
/* ret_unit_files= */ NULL); |
| 747 |
report_errno_and_exit(errno_pipe_fd[1], r); |
| 748 |
} |
| 749 |
|
| 750 |
seq[1] = safe_close(seq[1]); |
| 751 |
errno_pipe_fd[1] = safe_close(errno_pipe_fd[1]); |
| 752 |
|
| 753 |
r = receive_portable_metadata(seq[0], path, &os_release, &unit_files); |
| 754 |
if (r < 0) |
| 755 |
return r; |
| 756 |
|
| 757 |
r = pidref_wait_for_terminate_and_check("(sd-dissect)", &child, 0); |
| 758 |
if (r < 0) |
| 759 |
return r; |
| 760 |
|
| 761 |
pidref_done(&child); |
| 762 |
|
| 763 |
if (r != EXIT_SUCCESS) { |
| 764 |
if (read(errno_pipe_fd[0], &r, sizeof(r)) == sizeof(r)) |
| 765 |
return log_debug_errno(r, "Failed to extract portable metadata from '%s': %m", path); |
| 766 |
|
| 767 |
return log_debug_errno(SYNTHETIC_ERRNO(EPROTO), "Child failed."); |
| 768 |
} |
| 769 |
} |
| 770 |
|
| 771 |
if (!os_release) |
| 772 |
return sd_bus_error_setf(error, |
| 773 |
SD_BUS_ERROR_INVALID_ARGS, |
| 774 |
"Image '%s' lacks %s data, refusing.", |
| 775 |
path, |
| 776 |
path_is_extension ? "extension-release" : "os-release"); |
| 777 |
|
| 778 |
if (ret_unit_files) |
| 779 |
*ret_unit_files = TAKE_PTR(unit_files); |
| 780 |
|
| 781 |
if (ret_os_release) |
| 782 |
*ret_os_release = TAKE_PTR(os_release); |
| 783 |
|
| 784 |
if (ret_pinned_image_policy) |
| 785 |
*ret_pinned_image_policy = TAKE_PTR(pinned_image_policy); |
| 786 |
|
| 787 |
return 0; |
| 788 |
} |
| 789 |
|
| 790 |
static int extract_image_and_extensions( |
| 791 |
RuntimeScope scope, |
| 792 |
const char *name_or_path, |
| 793 |
char **matches, |
| 794 |
char **extension_image_paths, |
| 795 |
bool validate_extension, |
| 796 |
bool relax_extension_release_check, |
| 797 |
const ImagePolicy *image_policy, |
| 798 |
Image **ret_image, |
| 799 |
OrderedHashmap **ret_extension_images, |
| 800 |
OrderedHashmap **ret_extension_releases, |
| 801 |
PortableMetadata **ret_os_release, |
| 802 |
Hashmap **ret_unit_files, |
| 803 |
char ***ret_valid_prefixes, |
| 804 |
ImagePolicy **ret_pinned_root_image_policy, |
| 805 |
ImagePolicy **ret_pinned_ext_image_policy, |
| 806 |
sd_bus_error *error) { |
| 807 |
|
| 808 |
_cleanup_free_ char *id = NULL, *id_like = NULL, *version_id = NULL, *sysext_level = NULL, *confext_level = NULL; |
| 809 |
_cleanup_(image_policy_freep) ImagePolicy *pinned_root_image_policy = NULL, *pinned_ext_image_policy = NULL; |
| 810 |
_cleanup_(portable_metadata_unrefp) PortableMetadata *os_release = NULL; |
| 811 |
_cleanup_ordered_hashmap_free_ OrderedHashmap *extension_images = NULL, *extension_releases = NULL; |
| 812 |
_cleanup_(pick_result_done) PickResult result = PICK_RESULT_NULL; |
| 813 |
_cleanup_hashmap_free_ Hashmap *unit_files = NULL; |
| 814 |
_cleanup_strv_free_ char **valid_prefixes = NULL; |
| 815 |
_cleanup_(image_unrefp) Image *image = NULL; |
| 816 |
Image *ext; |
| 817 |
int r; |
| 818 |
|
| 819 |
assert(name_or_path); |
| 820 |
|
| 821 |
/* If we get a path, then check if it can be resolved with vpick. We need this as we might just |
| 822 |
* get a simple image name, which would make vpick error out. */ |
| 823 |
if (path_is_absolute(name_or_path)) { |
| 824 |
r = path_pick(/* toplevel_path= */ NULL, |
| 825 |
/* toplevel_fd= */ AT_FDCWD, |
| 826 |
name_or_path, |
| 827 |
pick_filter_image_any, |
| 828 |
ELEMENTSOF(pick_filter_image_any), |
| 829 |
PICK_ARCHITECTURE|PICK_TRIES|PICK_RESOLVE, |
| 830 |
&result); |
| 831 |
if (r < 0) |
| 832 |
return r; |
| 833 |
if (!result.path) |
| 834 |
return log_debug_errno( |
| 835 |
SYNTHETIC_ERRNO(ENOENT), |
| 836 |
"No matching entry in .v/ directory %s found.", |
| 837 |
name_or_path); |
| 838 |
|
| 839 |
name_or_path = result.path; |
| 840 |
} |
| 841 |
|
| 842 |
r = image_find_harder(scope, IMAGE_PORTABLE, name_or_path, /* root= */ NULL, &image); |
| 843 |
if (r < 0) |
| 844 |
return r; |
| 845 |
|
| 846 |
if (!strv_isempty(extension_image_paths)) { |
| 847 |
extension_images = ordered_hashmap_new(&image_hash_ops); |
| 848 |
if (!extension_images) |
| 849 |
return -ENOMEM; |
| 850 |
|
| 851 |
if (ret_extension_releases) { |
| 852 |
extension_releases = ordered_hashmap_new(&portable_metadata_hash_ops); |
| 853 |
if (!extension_releases) |
| 854 |
return -ENOMEM; |
| 855 |
} |
| 856 |
|
| 857 |
STRV_FOREACH(p, extension_image_paths) { |
| 858 |
_cleanup_(pick_result_done) PickResult ext_result = PICK_RESULT_NULL; |
| 859 |
_cleanup_(image_unrefp) Image *new = NULL; |
| 860 |
const char *path = *p; |
| 861 |
|
| 862 |
if (path_is_absolute(*p)) { |
| 863 |
r = path_pick(/* toplevel_path= */ NULL, |
| 864 |
/* toplevel_fd= */ AT_FDCWD, |
| 865 |
*p, |
| 866 |
pick_filter_image_any, |
| 867 |
ELEMENTSOF(pick_filter_image_any), |
| 868 |
PICK_ARCHITECTURE|PICK_TRIES|PICK_RESOLVE, |
| 869 |
&ext_result); |
| 870 |
if (r < 0) |
| 871 |
return r; |
| 872 |
if (!ext_result.path) |
| 873 |
return log_debug_errno( |
| 874 |
SYNTHETIC_ERRNO(ENOENT), |
| 875 |
"No matching entry in .v/ directory %s found.", |
| 876 |
*p); |
| 877 |
|
| 878 |
path = ext_result.path; |
| 879 |
} |
| 880 |
|
| 881 |
r = image_find_harder(scope, IMAGE_PORTABLE, path, NULL, &new); |
| 882 |
if (r < 0) |
| 883 |
return r; |
| 884 |
|
| 885 |
r = ordered_hashmap_put(extension_images, new->name, new); |
| 886 |
if (r < 0) |
| 887 |
return r; |
| 888 |
TAKE_PTR(new); |
| 889 |
} |
| 890 |
} |
| 891 |
|
| 892 |
r = portable_extract_by_path( |
| 893 |
scope, |
| 894 |
image->path, |
| 895 |
/* path_is_extension= */ false, |
| 896 |
/* relax_extension_release_check= */ false, |
| 897 |
matches, |
| 898 |
image_policy, |
| 899 |
&os_release, |
| 900 |
&unit_files, |
| 901 |
&pinned_root_image_policy, |
| 902 |
error); |
| 903 |
if (r < 0) |
| 904 |
return r; |
| 905 |
|
| 906 |
/* If we are layering extension images on top of a runtime image, check that the os-release and |
| 907 |
* extension-release metadata match, otherwise reject it immediately as invalid, or it will fail when |
| 908 |
* the units are started. Also, collect valid portable prefixes if caller requested that. */ |
| 909 |
if (validate_extension || ret_valid_prefixes) { |
| 910 |
_cleanup_free_ char *prefixes = NULL, *portable_scope_str = NULL; |
| 911 |
|
| 912 |
r = parse_env_file_fd( |
| 913 |
os_release->fd, os_release->name, |
| 914 |
"ID", &id, |
| 915 |
"ID_LIKE", &id_like, |
| 916 |
"VERSION_ID", &version_id, |
| 917 |
"SYSEXT_LEVEL", &sysext_level, |
| 918 |
"CONFEXT_LEVEL", &confext_level, |
| 919 |
"PORTABLE_PREFIXES", &prefixes, |
| 920 |
"PORTABLE_SCOPE", &portable_scope_str); |
| 921 |
if (r < 0) |
| 922 |
return r; |
| 923 |
if (isempty(id)) |
| 924 |
return sd_bus_error_set_errnof(error, ESTALE, "Image %s os-release metadata lacks the ID field", name_or_path); |
| 925 |
|
| 926 |
if (prefixes) { |
| 927 |
valid_prefixes = strv_split(prefixes, WHITESPACE); |
| 928 |
if (!valid_prefixes) |
| 929 |
return -ENOMEM; |
| 930 |
} |
| 931 |
|
| 932 |
RuntimeScope portable_scope = RUNTIME_SCOPE_SYSTEM; |
| 933 |
if (portable_scope_str) { |
| 934 |
if (streq(portable_scope_str, "any")) |
| 935 |
portable_scope = _RUNTIME_SCOPE_INVALID; |
| 936 |
else { |
| 937 |
portable_scope = runtime_scope_from_string(portable_scope_str); |
| 938 |
if (portable_scope < 0) |
| 939 |
return sd_bus_error_setf( |
| 940 |
error, |
| 941 |
SD_BUS_ERROR_INVALID_ARGS, |
| 942 |
"Invalid PORTABLE_SCOPE value '%s' in image %s.", |
| 943 |
portable_scope_str, |
| 944 |
name_or_path); |
| 945 |
} |
| 946 |
} |
| 947 |
|
| 948 |
if (portable_scope != _RUNTIME_SCOPE_INVALID && portable_scope != scope) |
| 949 |
return sd_bus_error_setf( |
| 950 |
error, |
| 951 |
SD_BUS_ERROR_INVALID_ARGS, |
| 952 |
"Image %s portable scope '%s' incompatible with portabled runtime scope '%s'.", |
| 953 |
name_or_path, |
| 954 |
runtime_scope_to_string(portable_scope), |
| 955 |
runtime_scope_to_string(scope)); |
| 956 |
} |
| 957 |
|
| 958 |
ORDERED_HASHMAP_FOREACH(ext, extension_images) { |
| 959 |
_cleanup_(portable_metadata_unrefp) PortableMetadata *extension_release_meta = NULL; |
| 960 |
_cleanup_(image_policy_freep) ImagePolicy *policy = NULL; |
| 961 |
_cleanup_hashmap_free_ Hashmap *extra_unit_files = NULL; |
| 962 |
_cleanup_strv_free_ char **extension_release = NULL; |
| 963 |
const char *e; |
| 964 |
|
| 965 |
r = portable_extract_by_path( |
| 966 |
scope, |
| 967 |
ext->path, |
| 968 |
/* path_is_extension= */ true, |
| 969 |
relax_extension_release_check, |
| 970 |
matches, |
| 971 |
image_policy, |
| 972 |
&extension_release_meta, |
| 973 |
&extra_unit_files, |
| 974 |
&policy, |
| 975 |
error); |
| 976 |
if (r < 0) |
| 977 |
return r; |
| 978 |
|
| 979 |
r = hashmap_move(unit_files, extra_unit_files); |
| 980 |
if (r < 0) |
| 981 |
return r; |
| 982 |
|
| 983 |
if (!pinned_ext_image_policy && policy) |
| 984 |
pinned_ext_image_policy = TAKE_PTR(policy); |
| 985 |
else if (policy) { |
| 986 |
_cleanup_(image_policy_freep) ImagePolicy *intersected_policy = NULL; |
| 987 |
|
| 988 |
/* There is a single policy for all extension images, so we need a union */ |
| 989 |
r = image_policy_union(pinned_ext_image_policy, policy, &intersected_policy); |
| 990 |
if (r < 0) |
| 991 |
return log_debug_errno(r, "Failed to merge extension image policies: %m"); |
| 992 |
|
| 993 |
free_and_replace(pinned_ext_image_policy, intersected_policy); |
| 994 |
} |
| 995 |
|
| 996 |
if (!validate_extension && !ret_valid_prefixes && !ret_extension_releases) |
| 997 |
continue; |
| 998 |
|
| 999 |
r = load_env_file_pairs_fd(extension_release_meta->fd, extension_release_meta->name, &extension_release); |
| 1000 |
if (r < 0) |
| 1001 |
return r; |
| 1002 |
|
| 1003 |
if (validate_extension) { |
| 1004 |
r = extension_release_validate(ext->path, id, id_like, version_id, sysext_level, "portable", extension_release, IMAGE_SYSEXT); |
| 1005 |
if (r < 0) |
| 1006 |
r = extension_release_validate(ext->path, id, id_like, version_id, confext_level, "portable", extension_release, IMAGE_CONFEXT); |
| 1007 |
|
| 1008 |
if (r == 0) |
| 1009 |
return sd_bus_error_set_errnof(error, ESTALE, "Image %s extension-release metadata does not match the root's", ext->path); |
| 1010 |
if (r < 0) |
| 1011 |
return sd_bus_error_set_errnof(error, r, "Failed to compare image %s extension-release metadata with the root's os-release: %m", ext->path); |
| 1012 |
} |
| 1013 |
|
| 1014 |
e = strv_env_pairs_get(extension_release, "PORTABLE_PREFIXES"); |
| 1015 |
if (e) { |
| 1016 |
r = strv_split_and_extend(&valid_prefixes, e, WHITESPACE, /* filter_duplicates= */ true); |
| 1017 |
if (r < 0) |
| 1018 |
return r; |
| 1019 |
} |
| 1020 |
|
| 1021 |
if (ret_extension_releases) { |
| 1022 |
r = ordered_hashmap_put(extension_releases, ext->name, extension_release_meta); |
| 1023 |
if (r < 0) |
| 1024 |
return r; |
| 1025 |
TAKE_PTR(extension_release_meta); |
| 1026 |
} |
| 1027 |
} |
| 1028 |
|
| 1029 |
strv_sort(valid_prefixes); |
| 1030 |
|
| 1031 |
if (ret_image) |
| 1032 |
*ret_image = TAKE_PTR(image); |
| 1033 |
if (ret_extension_images) |
| 1034 |
*ret_extension_images = TAKE_PTR(extension_images); |
| 1035 |
if (ret_extension_releases) |
| 1036 |
*ret_extension_releases = TAKE_PTR(extension_releases); |
| 1037 |
if (ret_os_release) |
| 1038 |
*ret_os_release = TAKE_PTR(os_release); |
| 1039 |
if (ret_unit_files) |
| 1040 |
*ret_unit_files = TAKE_PTR(unit_files); |
| 1041 |
if (ret_valid_prefixes) |
| 1042 |
*ret_valid_prefixes = TAKE_PTR(valid_prefixes); |
| 1043 |
if (ret_pinned_root_image_policy) |
| 1044 |
*ret_pinned_root_image_policy = TAKE_PTR(pinned_root_image_policy); |
| 1045 |
if (ret_pinned_ext_image_policy) |
| 1046 |
*ret_pinned_ext_image_policy = TAKE_PTR(pinned_ext_image_policy); |
| 1047 |
|
| 1048 |
return 0; |
| 1049 |
} |
| 1050 |
|
| 1051 |
int portable_extract( |
| 1052 |
RuntimeScope scope, |
| 1053 |
const char *name_or_path, |
| 1054 |
char **matches, |
| 1055 |
char **extension_image_paths, |
| 1056 |
const ImagePolicy *image_policy, |
| 1057 |
PortableFlags flags, |
| 1058 |
PortableMetadata **ret_os_release, |
| 1059 |
OrderedHashmap **ret_extension_releases, |
| 1060 |
Hashmap **ret_unit_files, |
| 1061 |
char ***ret_valid_prefixes, |
| 1062 |
sd_bus_error *error) { |
| 1063 |
|
| 1064 |
_cleanup_(portable_metadata_unrefp) PortableMetadata *os_release = NULL; |
| 1065 |
_cleanup_ordered_hashmap_free_ OrderedHashmap *extension_images = NULL, *extension_releases = NULL; |
| 1066 |
_cleanup_hashmap_free_ Hashmap *unit_files = NULL; |
| 1067 |
_cleanup_strv_free_ char **valid_prefixes = NULL; |
| 1068 |
_cleanup_(image_unrefp) Image *image = NULL; |
| 1069 |
int r; |
| 1070 |
|
| 1071 |
assert(name_or_path); |
| 1072 |
|
| 1073 |
r = extract_image_and_extensions( |
| 1074 |
scope, |
| 1075 |
name_or_path, |
| 1076 |
matches, |
| 1077 |
extension_image_paths, |
| 1078 |
/* validate_extension= */ false, |
| 1079 |
/* relax_extension_release_check= */ FLAGS_SET(flags, PORTABLE_FORCE_EXTENSION), |
| 1080 |
image_policy, |
| 1081 |
&image, |
| 1082 |
&extension_images, |
| 1083 |
&extension_releases, |
| 1084 |
&os_release, |
| 1085 |
&unit_files, |
| 1086 |
ret_valid_prefixes ? &valid_prefixes : NULL, |
| 1087 |
/* pinned_root_image_policy= */ NULL, |
| 1088 |
/* pinned_ext_image_policy= */ NULL, |
| 1089 |
error); |
| 1090 |
if (r < 0) |
| 1091 |
return r; |
| 1092 |
|
| 1093 |
if (hashmap_isempty(unit_files)) { |
| 1094 |
_cleanup_free_ char *extensions = strv_join(extension_image_paths, ", "); |
| 1095 |
if (!extensions) |
| 1096 |
return -ENOMEM; |
| 1097 |
|
| 1098 |
return sd_bus_error_setf(error, |
| 1099 |
SD_BUS_ERROR_INVALID_ARGS, |
| 1100 |
"Couldn't find any matching unit files in image '%s%s%s', refusing.", |
| 1101 |
image->path, |
| 1102 |
isempty(extensions) ? "" : "' or any of its extensions '", |
| 1103 |
isempty(extensions) ? "" : extensions); |
| 1104 |
} |
| 1105 |
|
| 1106 |
if (ret_os_release) |
| 1107 |
*ret_os_release = TAKE_PTR(os_release); |
| 1108 |
if (ret_extension_releases) |
| 1109 |
*ret_extension_releases = TAKE_PTR(extension_releases); |
| 1110 |
if (ret_unit_files) |
| 1111 |
*ret_unit_files = TAKE_PTR(unit_files); |
| 1112 |
if (ret_valid_prefixes) |
| 1113 |
*ret_valid_prefixes = TAKE_PTR(valid_prefixes); |
| 1114 |
|
| 1115 |
return 0; |
| 1116 |
} |
| 1117 |
|
| 1118 |
static int unit_file_is_active( |
| 1119 |
sd_bus *bus, |
| 1120 |
const char *name, |
| 1121 |
sd_bus_error *error) { |
| 1122 |
|
| 1123 |
static const char *const active_states[] = { |
| 1124 |
"activating", |
| 1125 |
"active", |
| 1126 |
"reloading", |
| 1127 |
"deactivating", |
| 1128 |
NULL, |
| 1129 |
}; |
| 1130 |
int r; |
| 1131 |
|
| 1132 |
if (!bus) |
| 1133 |
return false; |
| 1134 |
|
| 1135 |
/* If we are looking at a plain or instance things are easy, we can just query the state */ |
| 1136 |
if (unit_name_is_valid(name, UNIT_NAME_PLAIN|UNIT_NAME_INSTANCE)) { |
| 1137 |
_cleanup_free_ char *path = NULL, *buf = NULL; |
| 1138 |
|
| 1139 |
path = unit_dbus_path_from_name(name); |
| 1140 |
if (!path) |
| 1141 |
return -ENOMEM; |
| 1142 |
|
| 1143 |
r = sd_bus_get_property_string( |
| 1144 |
bus, |
| 1145 |
"org.freedesktop.systemd1", |
| 1146 |
path, |
| 1147 |
"org.freedesktop.systemd1.Unit", |
| 1148 |
"ActiveState", |
| 1149 |
error, |
| 1150 |
&buf); |
| 1151 |
if (r < 0) |
| 1152 |
return log_debug_errno(r, "Failed to retrieve unit state: %s", bus_error_message(error, r)); |
| 1153 |
|
| 1154 |
return strv_contains((char**) active_states, buf); |
| 1155 |
} |
| 1156 |
|
| 1157 |
/* Otherwise we need to enumerate. But let's build the most restricted query we can */ |
| 1158 |
if (unit_name_is_valid(name, UNIT_NAME_TEMPLATE)) { |
| 1159 |
_cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL, *reply = NULL; |
| 1160 |
const char *at, *prefix, *joined; |
| 1161 |
|
| 1162 |
r = bus_message_new_method_call(bus, &m, bus_systemd_mgr, "ListUnitsByPatterns"); |
| 1163 |
if (r < 0) |
| 1164 |
return r; |
| 1165 |
|
| 1166 |
r = sd_bus_message_append_strv(m, (char**) active_states); |
| 1167 |
if (r < 0) |
| 1168 |
return r; |
| 1169 |
|
| 1170 |
at = strchr(name, '@'); |
| 1171 |
assert(at); |
| 1172 |
|
| 1173 |
prefix = strndupa_safe(name, at + 1 - name); |
| 1174 |
joined = strjoina(prefix, "*", at + 1); |
| 1175 |
|
| 1176 |
r = sd_bus_message_append_strv(m, STRV_MAKE(joined)); |
| 1177 |
if (r < 0) |
| 1178 |
return r; |
| 1179 |
|
| 1180 |
r = sd_bus_call(bus, m, 0, error, &reply); |
| 1181 |
if (r < 0) |
| 1182 |
return log_debug_errno(r, "Failed to list units: %s", bus_error_message(error, r)); |
| 1183 |
|
| 1184 |
r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_ARRAY, "(ssssssouso)"); |
| 1185 |
if (r < 0) |
| 1186 |
return r; |
| 1187 |
|
| 1188 |
r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_STRUCT, "ssssssouso"); |
| 1189 |
if (r < 0) |
| 1190 |
return r; |
| 1191 |
|
| 1192 |
return r > 0; |
| 1193 |
} |
| 1194 |
|
| 1195 |
return -EINVAL; |
| 1196 |
} |
| 1197 |
|
| 1198 |
static int portable_changes_add( |
| 1199 |
PortableChange **changes, |
| 1200 |
size_t *n_changes, |
| 1201 |
int type_or_errno, /* PORTABLE_COPY, PORTABLE_SYMLINK, … if positive, or errno if negative */ |
| 1202 |
const char *path, |
| 1203 |
const char *source) { |
| 1204 |
|
| 1205 |
_cleanup_free_ char *p = NULL, *s = NULL; |
| 1206 |
int r; |
| 1207 |
|
| 1208 |
assert(path); |
| 1209 |
assert(!changes == !n_changes); |
| 1210 |
|
| 1211 |
if (type_or_errno >= 0) |
| 1212 |
assert(type_or_errno < _PORTABLE_CHANGE_TYPE_MAX); |
| 1213 |
else |
| 1214 |
assert(type_or_errno >= -ERRNO_MAX); |
| 1215 |
|
| 1216 |
if (!changes) |
| 1217 |
return 0; |
| 1218 |
|
| 1219 |
if (!GREEDY_REALLOC(*changes, *n_changes + 1)) |
| 1220 |
return -ENOMEM; |
| 1221 |
|
| 1222 |
r = path_simplify_alloc(path, &p); |
| 1223 |
if (r < 0) |
| 1224 |
return r; |
| 1225 |
|
| 1226 |
r = path_simplify_alloc(source, &s); |
| 1227 |
if (r < 0) |
| 1228 |
return r; |
| 1229 |
|
| 1230 |
(*changes)[(*n_changes)++] = (PortableChange) { |
| 1231 |
.type_or_errno = type_or_errno, |
| 1232 |
.path = TAKE_PTR(p), |
| 1233 |
.source = TAKE_PTR(s), |
| 1234 |
}; |
| 1235 |
|
| 1236 |
return 0; |
| 1237 |
} |
| 1238 |
|
| 1239 |
static int portable_changes_add_with_prefix( |
| 1240 |
PortableChange **changes, |
| 1241 |
size_t *n_changes, |
| 1242 |
int type_or_errno, |
| 1243 |
const char *prefix, |
| 1244 |
const char *path, |
| 1245 |
const char *source) { |
| 1246 |
|
| 1247 |
_cleanup_free_ char *path_buf = NULL, *source_buf = NULL; |
| 1248 |
|
| 1249 |
assert(path); |
| 1250 |
assert(!changes == !n_changes); |
| 1251 |
|
| 1252 |
if (!changes) |
| 1253 |
return 0; |
| 1254 |
|
| 1255 |
if (prefix) { |
| 1256 |
path_buf = path_join(prefix, path); |
| 1257 |
if (!path_buf) |
| 1258 |
return -ENOMEM; |
| 1259 |
|
| 1260 |
path = path_buf; |
| 1261 |
|
| 1262 |
if (source) { |
| 1263 |
source_buf = path_join(prefix, source); |
| 1264 |
if (!source_buf) |
| 1265 |
return -ENOMEM; |
| 1266 |
|
| 1267 |
source = source_buf; |
| 1268 |
} |
| 1269 |
} |
| 1270 |
|
| 1271 |
return portable_changes_add(changes, n_changes, type_or_errno, path, source); |
| 1272 |
} |
| 1273 |
|
| 1274 |
static void portable_change_done(PortableChange *change) { |
| 1275 |
assert(change); |
| 1276 |
change->path = mfree(change->path); |
| 1277 |
change->source = mfree(change->source); |
| 1278 |
} |
| 1279 |
|
| 1280 |
DEFINE_ARRAY_FREE_FUNC(portable_changes_free, PortableChange, portable_change_done); |
| 1281 |
|
| 1282 |
static const char *root_setting_from_image(ImageType type) { |
| 1283 |
switch (type) { |
| 1284 |
case IMAGE_DIRECTORY: |
| 1285 |
case IMAGE_SUBVOLUME: |
| 1286 |
return "RootDirectory="; |
| 1287 |
|
| 1288 |
case IMAGE_RAW: |
| 1289 |
case IMAGE_BLOCK: |
| 1290 |
return "RootImage="; |
| 1291 |
|
| 1292 |
case IMAGE_MSTACK: |
| 1293 |
return "RootMStack="; |
| 1294 |
|
| 1295 |
default: |
| 1296 |
return NULL; |
| 1297 |
} |
| 1298 |
} |
| 1299 |
|
| 1300 |
static const char *extension_setting_from_image(ImageType type) { |
| 1301 |
switch (type) { |
| 1302 |
case IMAGE_DIRECTORY: |
| 1303 |
case IMAGE_SUBVOLUME: |
| 1304 |
return "ExtensionDirectories="; |
| 1305 |
|
| 1306 |
case IMAGE_RAW: |
| 1307 |
case IMAGE_BLOCK: |
| 1308 |
return "ExtensionImages="; |
| 1309 |
|
| 1310 |
default: |
| 1311 |
return NULL; |
| 1312 |
} |
| 1313 |
} |
| 1314 |
|
| 1315 |
static int make_marker_text(const char *image_path, OrderedHashmap *extension_images, char **ret_text) { |
| 1316 |
_cleanup_free_ char *text = NULL, *escaped_image_path = NULL; |
| 1317 |
Image *ext; |
| 1318 |
|
| 1319 |
assert(image_path); |
| 1320 |
assert(ret_text); |
| 1321 |
|
| 1322 |
escaped_image_path = xescape(image_path, ":"); |
| 1323 |
if (!escaped_image_path) |
| 1324 |
return -ENOMEM; |
| 1325 |
|
| 1326 |
/* If the image is layered, include all layers in the marker as a colon-separated |
| 1327 |
* list of paths, so that we can do exact matches on removal. */ |
| 1328 |
text = strjoin(PORTABLE_DROPIN_MARKER_BEGIN, escaped_image_path); |
| 1329 |
if (!text) |
| 1330 |
return -ENOMEM; |
| 1331 |
|
| 1332 |
ORDERED_HASHMAP_FOREACH(ext, extension_images) { |
| 1333 |
_cleanup_free_ char *escaped = NULL; |
| 1334 |
|
| 1335 |
escaped = xescape(ext->path, ":"); |
| 1336 |
if (!escaped) |
| 1337 |
return -ENOMEM; |
| 1338 |
|
| 1339 |
if (!strextend(&text, ":", escaped)) |
| 1340 |
return -ENOMEM; |
| 1341 |
} |
| 1342 |
|
| 1343 |
if (!strextend(&text, PORTABLE_DROPIN_MARKER_END "\n")) |
| 1344 |
return -ENOMEM; |
| 1345 |
|
| 1346 |
*ret_text = TAKE_PTR(text); |
| 1347 |
return 0; |
| 1348 |
} |
| 1349 |
|
| 1350 |
static int append_release_log_fields( |
| 1351 |
char **text, |
| 1352 |
const PortableMetadata *release, |
| 1353 |
ImageClass type, |
| 1354 |
const char *field_name) { |
| 1355 |
|
| 1356 |
static const char *const field_versions[_IMAGE_CLASS_MAX][4]= { |
| 1357 |
[IMAGE_PORTABLE] = { "IMAGE_VERSION", "VERSION_ID", "BUILD_ID", NULL }, |
| 1358 |
[IMAGE_SYSEXT] = { "SYSEXT_IMAGE_VERSION", "SYSEXT_VERSION_ID", "SYSEXT_BUILD_ID", NULL }, |
| 1359 |
[IMAGE_CONFEXT] = { "CONFEXT_IMAGE_VERSION", "CONFEXT_VERSION_ID", "CONFEXT_BUILD_ID", NULL }, |
| 1360 |
}; |
| 1361 |
static const char *const field_ids[_IMAGE_CLASS_MAX][3]= { |
| 1362 |
[IMAGE_PORTABLE] = { "IMAGE_ID", "ID", NULL }, |
| 1363 |
[IMAGE_SYSEXT] = { "SYSEXT_IMAGE_ID", "SYSEXT_ID", NULL }, |
| 1364 |
[IMAGE_CONFEXT] = { "CONFEXT_IMAGE_ID", "CONFEXT_ID", NULL }, |
| 1365 |
}; |
| 1366 |
_cleanup_strv_free_ char **fields = NULL; |
| 1367 |
const char *id = NULL, *version = NULL; |
| 1368 |
int r; |
| 1369 |
|
| 1370 |
assert(IN_SET(type, IMAGE_PORTABLE, IMAGE_SYSEXT, IMAGE_CONFEXT)); |
| 1371 |
assert(!strv_isempty((char *const *)field_ids[type])); |
| 1372 |
assert(!strv_isempty((char *const *)field_versions[type])); |
| 1373 |
assert(field_name); |
| 1374 |
assert(text); |
| 1375 |
|
| 1376 |
if (!release) |
| 1377 |
return 0; /* Nothing to do. */ |
| 1378 |
|
| 1379 |
r = load_env_file_pairs_fd(release->fd, release->name, &fields); |
| 1380 |
if (r < 0) |
| 1381 |
return log_debug_errno(r, "Failed to parse '%s': %m", release->name); |
| 1382 |
|
| 1383 |
/* Find an ID first, in order of preference from more specific to less specific: IMAGE_ID -> ID */ |
| 1384 |
id = strv_find_first_field((char *const *)field_ids[type], fields); |
| 1385 |
if (id && string_has_cc(id, /* ok= */ NULL)) { |
| 1386 |
log_debug("os-release file '%s' contains control characters in the ID field, skipping.", |
| 1387 |
release->name); |
| 1388 |
id = NULL; |
| 1389 |
} |
| 1390 |
|
| 1391 |
/* Then the version, same logic, prefer the more specific one */ |
| 1392 |
version = strv_find_first_field((char *const *)field_versions[type], fields); |
| 1393 |
if (version && string_has_cc(version, /* ok= */ NULL)) { |
| 1394 |
log_debug("os-release file '%s' contains control characters in the version field, skipping.", |
| 1395 |
release->name); |
| 1396 |
version = NULL; |
| 1397 |
} |
| 1398 |
|
| 1399 |
/* If there's no valid version to be found, simply omit it. */ |
| 1400 |
if (!id && !version) |
| 1401 |
return 0; |
| 1402 |
|
| 1403 |
if (!strextend(text, |
| 1404 |
"LogExtraFields=", |
| 1405 |
field_name, |
| 1406 |
"=", |
| 1407 |
strempty(id), |
| 1408 |
id && version ? "_" : "", |
| 1409 |
strempty(version), |
| 1410 |
"\n")) |
| 1411 |
return -ENOMEM; |
| 1412 |
|
| 1413 |
return 0; |
| 1414 |
} |
| 1415 |
|
| 1416 |
static int install_chroot_dropin( |
| 1417 |
const char *image_path, |
| 1418 |
ImageType type, |
| 1419 |
OrderedHashmap *extension_images, |
| 1420 |
OrderedHashmap *extension_releases, |
| 1421 |
const ImagePolicy *pinned_root_image_policy, |
| 1422 |
const ImagePolicy *pinned_ext_image_policy, |
| 1423 |
const PortableMetadata *m, |
| 1424 |
const PortableMetadata *os_release, |
| 1425 |
const char *dropin_dir, |
| 1426 |
PortableFlags flags, |
| 1427 |
char **ret_dropin, |
| 1428 |
PortableChange **changes, |
| 1429 |
size_t *n_changes) { |
| 1430 |
|
| 1431 |
_cleanup_free_ char *text = NULL, *dropin = NULL; |
| 1432 |
int r; |
| 1433 |
|
| 1434 |
assert(image_path); |
| 1435 |
assert(m); |
| 1436 |
assert(dropin_dir); |
| 1437 |
|
| 1438 |
dropin = path_join(dropin_dir, "20-portable.conf"); |
| 1439 |
if (!dropin) |
| 1440 |
return -ENOMEM; |
| 1441 |
|
| 1442 |
r = make_marker_text(image_path, extension_images, &text); |
| 1443 |
if (r < 0) |
| 1444 |
return log_debug_errno(r, "Failed to generate marker string for portable drop-in: %m"); |
| 1445 |
|
| 1446 |
if (endswith(m->name, ".service")) { |
| 1447 |
const char *root_type; |
| 1448 |
_cleanup_free_ char *base_name = NULL; |
| 1449 |
Image *ext; |
| 1450 |
|
| 1451 |
root_type = root_setting_from_image(type); |
| 1452 |
if (!root_type) |
| 1453 |
return log_debug_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "Image type '%s' not supported as portable service.", image_type_to_string(type)); |
| 1454 |
|
| 1455 |
r = path_extract_filename(m->image_path ?: image_path, &base_name); |
| 1456 |
if (r < 0) |
| 1457 |
return log_debug_errno(r, "Failed to extract basename from '%s': %m", m->image_path ?: image_path); |
| 1458 |
|
| 1459 |
if (!strextend(&text, |
| 1460 |
"\n" |
| 1461 |
"[Service]\n", |
| 1462 |
root_type, image_path, "\n" |
| 1463 |
"Environment=PORTABLE=", base_name, "\n" |
| 1464 |
"LogExtraFields=PORTABLE=", base_name, "\n")) |
| 1465 |
return -ENOMEM; |
| 1466 |
|
| 1467 |
if (pinned_root_image_policy) { |
| 1468 |
_cleanup_free_ char *policy_str = NULL; |
| 1469 |
|
| 1470 |
r = image_policy_to_string(pinned_root_image_policy, /* simplify= */ true, &policy_str); |
| 1471 |
if (r < 0) |
| 1472 |
return log_debug_errno(r, "Failed to serialize pinned image policy: %m"); |
| 1473 |
|
| 1474 |
if (!strextend(&text, |
| 1475 |
"RootImagePolicy=", policy_str, "\n")) |
| 1476 |
return -ENOMEM; |
| 1477 |
} |
| 1478 |
|
| 1479 |
/* If we have a single image then PORTABLE= will point to it, so we add |
| 1480 |
* PORTABLE_NAME_AND_VERSION= with the os-release fields and we are done. But if we have |
| 1481 |
* extensions, PORTABLE= will point to the image where the current unit was found in. So we |
| 1482 |
* also list PORTABLE_ROOT= and PORTABLE_ROOT_NAME_AND_VERSION= for the base image, and |
| 1483 |
* PORTABLE_EXTENSION= and PORTABLE_EXTENSION_NAME_AND_VERSION= for each extension, so that |
| 1484 |
* all needed metadata is available. */ |
| 1485 |
if (ordered_hashmap_isempty(extension_images)) |
| 1486 |
r = append_release_log_fields(&text, os_release, IMAGE_PORTABLE, "PORTABLE_NAME_AND_VERSION"); |
| 1487 |
else { |
| 1488 |
_cleanup_free_ char *root_base_name = NULL; |
| 1489 |
|
| 1490 |
r = path_extract_filename(image_path, &root_base_name); |
| 1491 |
if (r < 0) |
| 1492 |
return log_debug_errno(r, "Failed to extract basename from '%s': %m", image_path); |
| 1493 |
|
| 1494 |
if (!strextend(&text, |
| 1495 |
"Environment=PORTABLE_ROOT=", root_base_name, "\n", |
| 1496 |
"LogExtraFields=PORTABLE_ROOT=", root_base_name, "\n")) |
| 1497 |
return -ENOMEM; |
| 1498 |
|
| 1499 |
r = append_release_log_fields(&text, os_release, IMAGE_PORTABLE, "PORTABLE_ROOT_NAME_AND_VERSION"); |
| 1500 |
} |
| 1501 |
if (r < 0) |
| 1502 |
return r; |
| 1503 |
|
| 1504 |
ORDERED_HASHMAP_FOREACH(ext, extension_images) { |
| 1505 |
|
| 1506 |
const char *extension_setting = extension_setting_from_image(ext->type); |
| 1507 |
if (!extension_setting) |
| 1508 |
return log_debug_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "Image type '%s' not supported for extensions: %m", image_type_to_string(ext->type)); |
| 1509 |
|
| 1510 |
_cleanup_free_ char *extension_base_name = NULL; |
| 1511 |
r = path_extract_filename(ext->path, &extension_base_name); |
| 1512 |
if (r < 0) |
| 1513 |
return log_debug_errno(r, "Failed to extract basename from '%s': %m", ext->path); |
| 1514 |
|
| 1515 |
if (!strextend(&text, |
| 1516 |
"\n", |
| 1517 |
extension_setting, |
| 1518 |
ext->path, |
| 1519 |
/* With --force tell PID1 to avoid enforcing that the image <name> and |
| 1520 |
* extension-release.<name> have to match. */ |
| 1521 |
!IN_SET(ext->type, IMAGE_DIRECTORY, IMAGE_SUBVOLUME) && |
| 1522 |
FLAGS_SET(flags, PORTABLE_FORCE_EXTENSION) ? |
| 1523 |
":x-systemd.relax-extension-release-check\n" : |
| 1524 |
"\n", |
| 1525 |
/* In PORTABLE= we list the 'main' image name for this unit |
| 1526 |
* (the image where the unit was extracted from), but we are |
| 1527 |
* stacking multiple images, so list those too. */ |
| 1528 |
"LogExtraFields=PORTABLE_EXTENSION=", extension_base_name, "\n")) |
| 1529 |
return -ENOMEM; |
| 1530 |
|
| 1531 |
if (pinned_ext_image_policy && !IN_SET(ext->type, IMAGE_DIRECTORY, IMAGE_SUBVOLUME)) { |
| 1532 |
_cleanup_free_ char *policy_str = NULL; |
| 1533 |
|
| 1534 |
r = image_policy_to_string(pinned_ext_image_policy, /* simplify= */ true, &policy_str); |
| 1535 |
if (r < 0) |
| 1536 |
return log_debug_errno(r, "Failed to serialize pinned image policy: %m"); |
| 1537 |
|
| 1538 |
if (!strextend(&text, |
| 1539 |
"ExtensionImagePolicy=", policy_str, "\n")) |
| 1540 |
return -ENOMEM; |
| 1541 |
} |
| 1542 |
|
| 1543 |
/* Look for image/version identifiers in the extension release files. We |
| 1544 |
* look for all possible IDs, but typically only 1 or 2 will be set, so |
| 1545 |
* the number of fields added shouldn't be too large. We prefix the DDI |
| 1546 |
* name to the value, so that we can add the same field multiple times and |
| 1547 |
* still be able to identify what applies to what. */ |
| 1548 |
r = append_release_log_fields(&text, |
| 1549 |
ordered_hashmap_get(extension_releases, ext->name), |
| 1550 |
IMAGE_SYSEXT, |
| 1551 |
"PORTABLE_EXTENSION_NAME_AND_VERSION"); |
| 1552 |
if (r < 0) |
| 1553 |
return r; |
| 1554 |
|
| 1555 |
r = append_release_log_fields(&text, |
| 1556 |
ordered_hashmap_get(extension_releases, ext->name), |
| 1557 |
IMAGE_CONFEXT, |
| 1558 |
"PORTABLE_EXTENSION_NAME_AND_VERSION"); |
| 1559 |
if (r < 0) |
| 1560 |
return r; |
| 1561 |
} |
| 1562 |
} |
| 1563 |
|
| 1564 |
r = write_string_file(dropin, text, WRITE_STRING_FILE_CREATE|WRITE_STRING_FILE_ATOMIC|WRITE_STRING_FILE_SYNC); |
| 1565 |
if (r < 0) |
| 1566 |
return log_debug_errno(r, "Failed to write '%s': %m", dropin); |
| 1567 |
|
| 1568 |
(void) portable_changes_add(changes, n_changes, PORTABLE_WRITE, dropin, NULL); |
| 1569 |
|
| 1570 |
if (ret_dropin) |
| 1571 |
*ret_dropin = TAKE_PTR(dropin); |
| 1572 |
|
| 1573 |
return 0; |
| 1574 |
} |
| 1575 |
|
| 1576 |
static int install_profile_dropin( |
| 1577 |
RuntimeScope scope, |
| 1578 |
const char *image_path, |
| 1579 |
const PortableMetadata *m, |
| 1580 |
const char *dropin_dir, |
| 1581 |
const char *profile, |
| 1582 |
PortableFlags flags, |
| 1583 |
char **ret_dropin, |
| 1584 |
PortableChange **changes, |
| 1585 |
size_t *n_changes) { |
| 1586 |
|
| 1587 |
_cleanup_free_ char *dropin = NULL, *from = NULL; |
| 1588 |
int r; |
| 1589 |
|
| 1590 |
assert(image_path); |
| 1591 |
assert(m); |
| 1592 |
assert(dropin_dir); |
| 1593 |
|
| 1594 |
if (!profile) |
| 1595 |
return 0; |
| 1596 |
|
| 1597 |
r = find_portable_profile(scope, profile, m->name, &from); |
| 1598 |
if (r < 0) { |
| 1599 |
if (r != -ENOENT) |
| 1600 |
return log_debug_errno(errno, "Profile '%s' is not accessible: %m", profile); |
| 1601 |
|
| 1602 |
log_debug_errno(errno, "Skipping link to profile '%s', as it does not exist: %m", profile); |
| 1603 |
return 0; |
| 1604 |
} |
| 1605 |
|
| 1606 |
dropin = path_join(dropin_dir, "10-profile.conf"); |
| 1607 |
if (!dropin) |
| 1608 |
return -ENOMEM; |
| 1609 |
|
| 1610 |
if (flags & PORTABLE_PREFER_COPY) { |
| 1611 |
CopyFlags copy_flags = COPY_REFLINK|COPY_FSYNC; |
| 1612 |
|
| 1613 |
if (flags & PORTABLE_FORCE_ATTACH) |
| 1614 |
copy_flags |= COPY_REPLACE; |
| 1615 |
|
| 1616 |
r = copy_file_atomic(from, dropin, 0644, copy_flags); |
| 1617 |
if (r < 0) |
| 1618 |
return log_debug_errno(r, "Failed to copy %s %s %s: %m", from, glyph(GLYPH_ARROW_RIGHT), dropin); |
| 1619 |
|
| 1620 |
(void) portable_changes_add(changes, n_changes, PORTABLE_COPY, dropin, from); |
| 1621 |
|
| 1622 |
} else { |
| 1623 |
|
| 1624 |
if (flags & PORTABLE_FORCE_ATTACH) |
| 1625 |
r = symlink_atomic(from, dropin); |
| 1626 |
else |
| 1627 |
r = RET_NERRNO(symlink(from, dropin)); |
| 1628 |
if (r < 0) |
| 1629 |
return log_debug_errno(r, "Failed to link %s %s %s: %m", from, glyph(GLYPH_ARROW_RIGHT), dropin); |
| 1630 |
|
| 1631 |
(void) portable_changes_add(changes, n_changes, PORTABLE_SYMLINK, dropin, from); |
| 1632 |
} |
| 1633 |
|
| 1634 |
if (ret_dropin) |
| 1635 |
*ret_dropin = TAKE_PTR(dropin); |
| 1636 |
|
| 1637 |
return 0; |
| 1638 |
} |
| 1639 |
|
| 1640 |
static const char *attached_path(const LookupPaths *paths, PortableFlags flags) { |
| 1641 |
const char *where; |
| 1642 |
|
| 1643 |
assert(paths); |
| 1644 |
|
| 1645 |
if (flags & PORTABLE_RUNTIME) |
| 1646 |
where = paths->runtime_attached; |
| 1647 |
else |
| 1648 |
where = paths->persistent_attached; |
| 1649 |
|
| 1650 |
assert(where); |
| 1651 |
return where; |
| 1652 |
} |
| 1653 |
|
| 1654 |
static int attach_unit_file( |
| 1655 |
RuntimeScope scope, |
| 1656 |
const LookupPaths *paths, |
| 1657 |
const char *image_path, |
| 1658 |
ImageType type, |
| 1659 |
OrderedHashmap *extension_images, |
| 1660 |
OrderedHashmap *extension_releases, |
| 1661 |
const ImagePolicy *pinned_root_image_policy, |
| 1662 |
const ImagePolicy *pinned_ext_image_policy, |
| 1663 |
const PortableMetadata *m, |
| 1664 |
const PortableMetadata *os_release, |
| 1665 |
const char *profile, |
| 1666 |
PortableFlags flags, |
| 1667 |
PortableChange **changes, |
| 1668 |
size_t *n_changes) { |
| 1669 |
|
| 1670 |
_cleanup_(unlink_and_freep) char *chroot_dropin = NULL, *profile_dropin = NULL; |
| 1671 |
_cleanup_(rmdir_and_freep) char *dropin_dir = NULL; |
| 1672 |
_cleanup_free_ char *path = NULL; |
| 1673 |
const char *where; |
| 1674 |
int r; |
| 1675 |
|
| 1676 |
assert(paths); |
| 1677 |
assert(image_path); |
| 1678 |
assert(m); |
| 1679 |
assert(PORTABLE_METADATA_IS_UNIT(m)); |
| 1680 |
|
| 1681 |
where = attached_path(paths, flags); |
| 1682 |
|
| 1683 |
(void) mkdir_parents(where, 0755); |
| 1684 |
if (mkdir(where, 0755) < 0) { |
| 1685 |
if (errno != EEXIST) |
| 1686 |
return log_debug_errno(errno, "Failed to create attach directory %s: %m", where); |
| 1687 |
} else |
| 1688 |
(void) portable_changes_add(changes, n_changes, PORTABLE_MKDIR, where, NULL); |
| 1689 |
|
| 1690 |
path = path_join(where, m->name); |
| 1691 |
if (!path) |
| 1692 |
return -ENOMEM; |
| 1693 |
|
| 1694 |
dropin_dir = strjoin(path, ".d"); |
| 1695 |
if (!dropin_dir) |
| 1696 |
return -ENOMEM; |
| 1697 |
|
| 1698 |
if (mkdir(dropin_dir, 0755) < 0) { |
| 1699 |
if (errno != EEXIST) |
| 1700 |
return log_debug_errno(errno, "Failed to create drop-in directory %s: %m", dropin_dir); |
| 1701 |
} else |
| 1702 |
(void) portable_changes_add(changes, n_changes, PORTABLE_MKDIR, dropin_dir, NULL); |
| 1703 |
|
| 1704 |
/* We install the drop-ins first, and the actual unit file last to achieve somewhat atomic behaviour if PID 1 |
| 1705 |
* is reloaded while we are creating things here: as long as only the drop-ins exist the unit doesn't exist at |
| 1706 |
* all for PID 1. */ |
| 1707 |
|
| 1708 |
r = install_chroot_dropin( |
| 1709 |
image_path, |
| 1710 |
type, |
| 1711 |
extension_images, |
| 1712 |
extension_releases, |
| 1713 |
pinned_root_image_policy, |
| 1714 |
pinned_ext_image_policy, |
| 1715 |
m, |
| 1716 |
os_release, |
| 1717 |
dropin_dir, |
| 1718 |
flags, |
| 1719 |
&chroot_dropin, |
| 1720 |
changes, |
| 1721 |
n_changes); |
| 1722 |
if (r < 0) |
| 1723 |
return r; |
| 1724 |
|
| 1725 |
r = install_profile_dropin(scope, image_path, m, dropin_dir, profile, flags, &profile_dropin, changes, n_changes); |
| 1726 |
if (r < 0) |
| 1727 |
return r; |
| 1728 |
|
| 1729 |
if ((flags & PORTABLE_PREFER_SYMLINK) && m->source) { |
| 1730 |
|
| 1731 |
if (flags & PORTABLE_FORCE_ATTACH) |
| 1732 |
r = symlink_atomic(m->source, path); |
| 1733 |
else |
| 1734 |
r = RET_NERRNO(symlink(m->source, path)); |
| 1735 |
if (r < 0) |
| 1736 |
return log_debug_errno(r, "Failed to symlink unit file '%s': %m", path); |
| 1737 |
|
| 1738 |
(void) portable_changes_add(changes, n_changes, PORTABLE_SYMLINK, path, m->source); |
| 1739 |
|
| 1740 |
} else { |
| 1741 |
LinkTmpfileFlags link_flags = LINK_TMPFILE_SYNC; |
| 1742 |
_cleanup_(unlink_and_freep) char *tmp = NULL; |
| 1743 |
_cleanup_close_ int fd = -EBADF; |
| 1744 |
|
| 1745 |
if (flags & PORTABLE_FORCE_ATTACH) |
| 1746 |
link_flags |= LINK_TMPFILE_REPLACE; |
| 1747 |
|
| 1748 |
(void) mac_selinux_create_file_prepare_label(path, m->selinux_label); |
| 1749 |
|
| 1750 |
fd = open_tmpfile_linkable(path, O_WRONLY|O_CLOEXEC, &tmp); |
| 1751 |
mac_selinux_create_file_clear(); /* Clear immediately in case of errors */ |
| 1752 |
if (fd < 0) |
| 1753 |
return log_debug_errno(fd, "Failed to create unit file '%s': %m", path); |
| 1754 |
|
| 1755 |
r = copy_bytes(m->fd, fd, UINT64_MAX, COPY_REFLINK); |
| 1756 |
if (r < 0) |
| 1757 |
return log_debug_errno(r, "Failed to copy unit file '%s': %m", path); |
| 1758 |
|
| 1759 |
if (fchmod(fd, 0644) < 0) |
| 1760 |
return log_debug_errno(errno, "Failed to change unit file access mode for '%s': %m", path); |
| 1761 |
|
| 1762 |
r = link_tmpfile(fd, tmp, path, link_flags); |
| 1763 |
if (r < 0) |
| 1764 |
return log_debug_errno(r, "Failed to install unit file '%s': %m", path); |
| 1765 |
|
| 1766 |
tmp = mfree(tmp); |
| 1767 |
|
| 1768 |
(void) portable_changes_add(changes, n_changes, PORTABLE_COPY, path, m->source); |
| 1769 |
} |
| 1770 |
|
| 1771 |
/* All is established now, now let's disable any rollbacks */ |
| 1772 |
chroot_dropin = mfree(chroot_dropin); |
| 1773 |
profile_dropin = mfree(profile_dropin); |
| 1774 |
dropin_dir = mfree(dropin_dir); |
| 1775 |
|
| 1776 |
return 0; |
| 1777 |
} |
| 1778 |
|
| 1779 |
static int image_target_path(RuntimeScope scope, const char *image_path, PortableFlags flags, char **ret) { |
| 1780 |
_cleanup_free_ char *where = NULL; |
| 1781 |
const char *fn; |
| 1782 |
char *joined = NULL; |
| 1783 |
int r; |
| 1784 |
|
| 1785 |
assert(image_path); |
| 1786 |
assert(ret); |
| 1787 |
|
| 1788 |
fn = last_path_component(image_path); |
| 1789 |
|
| 1790 |
if (flags & PORTABLE_RUNTIME) |
| 1791 |
r = runtime_directory_generic(scope, "portables", &where); |
| 1792 |
else |
| 1793 |
r = config_directory_generic(scope, "portables", &where); |
| 1794 |
if (r < 0) |
| 1795 |
return r; |
| 1796 |
|
| 1797 |
joined = path_join(where, fn); |
| 1798 |
if (!joined) |
| 1799 |
return -ENOMEM; |
| 1800 |
|
| 1801 |
*ret = joined; |
| 1802 |
return 0; |
| 1803 |
} |
| 1804 |
|
| 1805 |
static int install_image( |
| 1806 |
RuntimeScope scope, |
| 1807 |
const char *image_path, |
| 1808 |
PortableFlags flags, |
| 1809 |
PortableChange **changes, |
| 1810 |
size_t *n_changes) { |
| 1811 |
|
| 1812 |
_cleanup_free_ char *target = NULL; |
| 1813 |
int r; |
| 1814 |
|
| 1815 |
assert(scope < _RUNTIME_SCOPE_MAX); |
| 1816 |
assert(image_path); |
| 1817 |
|
| 1818 |
/* If the image is outside of the image search also link it into it, so that it can be found with |
| 1819 |
* short image names and is listed among the images. If we are operating in mixed mode, the image is |
| 1820 |
* copied instead. */ |
| 1821 |
|
| 1822 |
if (image_in_search_path(scope, IMAGE_PORTABLE, NULL, image_path)) |
| 1823 |
return 0; |
| 1824 |
|
| 1825 |
r = image_target_path(scope, image_path, flags, &target); |
| 1826 |
if (r < 0) |
| 1827 |
return log_debug_errno(r, "Failed to generate image symlink path: %m"); |
| 1828 |
|
| 1829 |
(void) mkdir_parents(target, 0755); |
| 1830 |
|
| 1831 |
if (flags & PORTABLE_MIXED_COPY_LINK) { |
| 1832 |
if (scope == RUNTIME_SCOPE_USER) { |
| 1833 |
_cleanup_close_ int userns_fd = nsresource_allocate_userns( |
| 1834 |
/* vl= */ NULL, |
| 1835 |
/* name= */ NULL, |
| 1836 |
NSRESOURCE_UIDS_64K); |
| 1837 |
if (userns_fd < 0) |
| 1838 |
return log_debug_errno(userns_fd, "Failed to allocate user namespace: %m"); |
| 1839 |
|
| 1840 |
_cleanup_close_ int fd = open(image_path, O_DIRECTORY|O_CLOEXEC); |
| 1841 |
if (fd < 0) |
| 1842 |
return log_debug_errno(errno, "Failed to open '%s': %m", image_path); |
| 1843 |
|
| 1844 |
struct stat st; |
| 1845 |
if (fstat(fd, &st) < 0) |
| 1846 |
return log_debug_errno(errno, "Failed to stat '%s': %m", image_path); |
| 1847 |
|
| 1848 |
_cleanup_(sd_varlink_unrefp) sd_varlink *mountfsd_link = NULL; |
| 1849 |
r = mountfsd_connect(&mountfsd_link); |
| 1850 |
if (r < 0) |
| 1851 |
return r; |
| 1852 |
|
| 1853 |
_cleanup_close_ int tree_fd = -EBADF; |
| 1854 |
if (uid_is_foreign(st.st_uid)) { |
| 1855 |
r = mountfsd_mount_directory_fd( |
| 1856 |
mountfsd_link, |
| 1857 |
fd, |
| 1858 |
userns_fd, |
| 1859 |
DISSECT_IMAGE_FOREIGN_UID, |
| 1860 |
&tree_fd); |
| 1861 |
if (r < 0) |
| 1862 |
return r; |
| 1863 |
} else |
| 1864 |
tree_fd = TAKE_FD(fd); |
| 1865 |
|
| 1866 |
_cleanup_close_ int directory_fd = -EBADF; |
| 1867 |
r = mountfsd_make_directory(mountfsd_link, target, MODE_INVALID, /* flags= */ 0, &directory_fd); |
| 1868 |
if (r < 0) |
| 1869 |
return r; |
| 1870 |
|
| 1871 |
_cleanup_close_ int copy_fd = -EBADF; |
| 1872 |
r = mountfsd_mount_directory_fd(mountfsd_link, directory_fd, userns_fd, DISSECT_IMAGE_FOREIGN_UID, ©_fd); |
| 1873 |
if (r < 0) |
| 1874 |
return r; |
| 1875 |
|
| 1876 |
r = copy_tree_at_foreign(tree_fd, copy_fd, userns_fd); |
| 1877 |
if (r < 0) |
| 1878 |
return r; |
| 1879 |
} else { |
| 1880 |
r = copy_tree(image_path, |
| 1881 |
target, |
| 1882 |
UID_INVALID, |
| 1883 |
GID_INVALID, |
| 1884 |
COPY_REFLINK | COPY_FSYNC | COPY_FSYNC_FULL | COPY_SYNCFS, |
| 1885 |
/* denylist= */ NULL, |
| 1886 |
/* subvolumes= */ NULL); |
| 1887 |
if (r < 0) |
| 1888 |
return log_debug_errno( |
| 1889 |
r, |
| 1890 |
"Failed to copy %s %s %s: %m", |
| 1891 |
image_path, |
| 1892 |
glyph(GLYPH_ARROW_RIGHT), |
| 1893 |
target); |
| 1894 |
} |
| 1895 |
} else { |
| 1896 |
if (symlink(image_path, target) < 0) |
| 1897 |
return log_debug_errno( |
| 1898 |
errno, |
| 1899 |
"Failed to link %s %s %s: %m", |
| 1900 |
image_path, |
| 1901 |
glyph(GLYPH_ARROW_RIGHT), |
| 1902 |
target); |
| 1903 |
} |
| 1904 |
|
| 1905 |
(void) portable_changes_add( |
| 1906 |
changes, |
| 1907 |
n_changes, |
| 1908 |
(flags & PORTABLE_MIXED_COPY_LINK) ? PORTABLE_COPY : PORTABLE_SYMLINK, |
| 1909 |
target, |
| 1910 |
image_path); |
| 1911 |
return 0; |
| 1912 |
} |
| 1913 |
|
| 1914 |
static int install_image_and_extensions( |
| 1915 |
RuntimeScope scope, |
| 1916 |
const Image *image, |
| 1917 |
OrderedHashmap *extension_images, |
| 1918 |
PortableFlags flags, |
| 1919 |
PortableChange **changes, |
| 1920 |
size_t *n_changes) { |
| 1921 |
|
| 1922 |
Image *ext; |
| 1923 |
int r; |
| 1924 |
|
| 1925 |
assert(image); |
| 1926 |
|
| 1927 |
ORDERED_HASHMAP_FOREACH(ext, extension_images) { |
| 1928 |
r = install_image(scope, ext->path, flags, changes, n_changes); |
| 1929 |
if (r < 0) |
| 1930 |
return r; |
| 1931 |
} |
| 1932 |
|
| 1933 |
r = install_image(scope, image->path, flags, changes, n_changes); |
| 1934 |
if (r < 0) |
| 1935 |
return r; |
| 1936 |
|
| 1937 |
return 0; |
| 1938 |
} |
| 1939 |
|
| 1940 |
static bool prefix_matches_compatible(char **matches, char **valid_prefixes) { |
| 1941 |
/* Checks if all 'matches' are included in the list of 'valid_prefixes' */ |
| 1942 |
|
| 1943 |
STRV_FOREACH(m, matches) |
| 1944 |
if (!strv_contains(valid_prefixes, *m)) |
| 1945 |
return false; |
| 1946 |
|
| 1947 |
return true; |
| 1948 |
} |
| 1949 |
|
| 1950 |
static void log_portable_verb( |
| 1951 |
const char *verb, |
| 1952 |
const char *message_id, |
| 1953 |
const char *image_path, |
| 1954 |
const char *profile, |
| 1955 |
OrderedHashmap *extension_images, |
| 1956 |
char **extension_image_paths, |
| 1957 |
PortableFlags flags) { |
| 1958 |
|
| 1959 |
_cleanup_free_ char *root_base_name = NULL, *extensions_joined = NULL; |
| 1960 |
_cleanup_strv_free_ char **extension_base_names = NULL; |
| 1961 |
Image *ext; |
| 1962 |
int r; |
| 1963 |
|
| 1964 |
assert(verb); |
| 1965 |
assert(message_id); |
| 1966 |
assert(image_path); |
| 1967 |
assert(!extension_images || !extension_image_paths); |
| 1968 |
|
| 1969 |
/* Use the same structured metadata as it is attached to units via LogExtraFields=. The main image |
| 1970 |
* is logged as PORTABLE_ROOT= and extensions, if any, as individual PORTABLE_EXTENSION= fields. */ |
| 1971 |
|
| 1972 |
r = path_extract_filename(image_path, &root_base_name); |
| 1973 |
if (r < 0) |
| 1974 |
log_debug_errno(r, "Failed to extract basename from '%s', ignoring: %m", image_path); |
| 1975 |
|
| 1976 |
ORDERED_HASHMAP_FOREACH(ext, extension_images) { |
| 1977 |
_cleanup_free_ char *extension_base_name = NULL; |
| 1978 |
|
| 1979 |
r = path_extract_filename(ext->path, &extension_base_name); |
| 1980 |
if (r < 0) { |
| 1981 |
log_debug_errno(r, "Failed to extract basename from '%s', ignoring: %m", ext->path); |
| 1982 |
continue; |
| 1983 |
} |
| 1984 |
|
| 1985 |
r = strv_extendf(&extension_base_names, "PORTABLE_EXTENSION=%s", extension_base_name); |
| 1986 |
if (r < 0) |
| 1987 |
log_oom_debug(); |
| 1988 |
|
| 1989 |
if (!strextend_with_separator(&extensions_joined, ", ", ext->path)) |
| 1990 |
log_oom_debug(); |
| 1991 |
} |
| 1992 |
|
| 1993 |
STRV_FOREACH(e, extension_image_paths) { |
| 1994 |
_cleanup_free_ char *extension_base_name = NULL; |
| 1995 |
|
| 1996 |
r = path_extract_filename(*e, &extension_base_name); |
| 1997 |
if (r < 0) { |
| 1998 |
log_debug_errno(r, "Failed to extract basename from '%s', ignoring: %m", *e); |
| 1999 |
continue; |
| 2000 |
} |
| 2001 |
|
| 2002 |
r = strv_extendf(&extension_base_names, "PORTABLE_EXTENSION=%s", extension_base_name); |
| 2003 |
if (r < 0) |
| 2004 |
log_oom_debug(); |
| 2005 |
|
| 2006 |
if (!strextend_with_separator(&extensions_joined, ", ", *e)) |
| 2007 |
log_oom_debug(); |
| 2008 |
} |
| 2009 |
|
| 2010 |
LOG_CONTEXT_PUSH_STRV(extension_base_names); |
| 2011 |
|
| 2012 |
log_struct(LOG_INFO, |
| 2013 |
LOG_MESSAGE("Successfully %s%s '%s%s%s%s%s'", |
| 2014 |
verb, |
| 2015 |
FLAGS_SET(flags, PORTABLE_RUNTIME) ? " ephemeral" : "", |
| 2016 |
image_path, |
| 2017 |
isempty(extensions_joined) ? "" : "' and its extension(s) '", |
| 2018 |
strempty(extensions_joined), |
| 2019 |
isempty(profile) ? "" : "' using profile '", |
| 2020 |
strempty(profile)), |
| 2021 |
message_id, |
| 2022 |
LOG_ITEM("PORTABLE_ROOT=%s", strna(root_base_name))); |
| 2023 |
} |
| 2024 |
|
| 2025 |
int portable_attach( |
| 2026 |
RuntimeScope scope, |
| 2027 |
sd_bus *bus, |
| 2028 |
const char *name_or_path, |
| 2029 |
char **matches, |
| 2030 |
const char *profile, |
| 2031 |
char **extension_image_paths, |
| 2032 |
const ImagePolicy *image_policy, |
| 2033 |
PortableFlags flags, |
| 2034 |
PortableChange **changes, |
| 2035 |
size_t *n_changes, |
| 2036 |
sd_bus_error *error) { |
| 2037 |
|
| 2038 |
_cleanup_(image_policy_freep) ImagePolicy *pinned_root_image_policy = NULL, *pinned_ext_image_policy = NULL; |
| 2039 |
_cleanup_ordered_hashmap_free_ OrderedHashmap *extension_images = NULL, *extension_releases = NULL; |
| 2040 |
_cleanup_(portable_metadata_unrefp) PortableMetadata *os_release = NULL; |
| 2041 |
_cleanup_hashmap_free_ Hashmap *unit_files = NULL; |
| 2042 |
_cleanup_(lookup_paths_done) LookupPaths paths = {}; |
| 2043 |
_cleanup_strv_free_ char **valid_prefixes = NULL; |
| 2044 |
_cleanup_(image_unrefp) Image *image = NULL; |
| 2045 |
PortableMetadata *item; |
| 2046 |
int r; |
| 2047 |
|
| 2048 |
assert(scope < _RUNTIME_SCOPE_MAX); |
| 2049 |
|
| 2050 |
r = extract_image_and_extensions( |
| 2051 |
scope, |
| 2052 |
name_or_path, |
| 2053 |
matches, |
| 2054 |
extension_image_paths, |
| 2055 |
/* validate_extension= */ true, |
| 2056 |
/* relax_extension_release_check= */ FLAGS_SET(flags, PORTABLE_FORCE_EXTENSION), |
| 2057 |
image_policy, |
| 2058 |
&image, |
| 2059 |
&extension_images, |
| 2060 |
&extension_releases, |
| 2061 |
&os_release, |
| 2062 |
&unit_files, |
| 2063 |
&valid_prefixes, |
| 2064 |
&pinned_root_image_policy, |
| 2065 |
&pinned_ext_image_policy, |
| 2066 |
error); |
| 2067 |
if (r < 0) |
| 2068 |
return r; |
| 2069 |
|
| 2070 |
if (valid_prefixes && !prefix_matches_compatible(matches, valid_prefixes)) { |
| 2071 |
_cleanup_free_ char *matches_joined = NULL, *extensions_joined = NULL, *valid_prefixes_joined = NULL; |
| 2072 |
|
| 2073 |
matches_joined = strv_join(matches, "', '"); |
| 2074 |
if (!matches_joined) |
| 2075 |
return -ENOMEM; |
| 2076 |
|
| 2077 |
extensions_joined = strv_join(extension_image_paths, ", "); |
| 2078 |
if (!extensions_joined) |
| 2079 |
return -ENOMEM; |
| 2080 |
|
| 2081 |
valid_prefixes_joined = strv_join(valid_prefixes, ", "); |
| 2082 |
if (!valid_prefixes_joined) |
| 2083 |
return -ENOMEM; |
| 2084 |
|
| 2085 |
return sd_bus_error_setf( |
| 2086 |
error, |
| 2087 |
SD_BUS_ERROR_INVALID_ARGS, |
| 2088 |
"Selected matches '%s' are not compatible with portable service image '%s%s%s', refusing. (Acceptable prefix matches are: %s)", |
| 2089 |
matches_joined, |
| 2090 |
image->path, |
| 2091 |
isempty(extensions_joined) ? "" : "' or any of its extensions '", |
| 2092 |
strempty(extensions_joined), |
| 2093 |
valid_prefixes_joined); |
| 2094 |
} |
| 2095 |
|
| 2096 |
if (hashmap_isempty(unit_files)) { |
| 2097 |
_cleanup_free_ char *extensions_joined = strv_join(extension_image_paths, ", "); |
| 2098 |
if (!extensions_joined) |
| 2099 |
return -ENOMEM; |
| 2100 |
|
| 2101 |
return sd_bus_error_setf( |
| 2102 |
error, |
| 2103 |
SD_BUS_ERROR_INVALID_ARGS, |
| 2104 |
"Couldn't find any matching unit files in image '%s%s%s', refusing.", |
| 2105 |
image->path, |
| 2106 |
isempty(extensions_joined) ? "" : "' or any of its extensions '", |
| 2107 |
strempty(extensions_joined)); |
| 2108 |
} |
| 2109 |
|
| 2110 |
r = lookup_paths_init(&paths, scope, /* flags= */ 0, NULL); |
| 2111 |
if (r < 0) |
| 2112 |
return r; |
| 2113 |
|
| 2114 |
if (!FLAGS_SET(flags, PORTABLE_REATTACH) && !FLAGS_SET(flags, PORTABLE_FORCE_ATTACH)) |
| 2115 |
HASHMAP_FOREACH(item, unit_files) { |
| 2116 |
r = unit_file_exists_full(scope, &paths, SEARCH_IGNORE_TEMPLATE, item->name, /* ret_path= */ NULL); |
| 2117 |
if (r < 0) |
| 2118 |
return sd_bus_error_set_errnof(error, r, "Failed to determine whether unit '%s' exists on the host: %m", item->name); |
| 2119 |
if (r > 0) |
| 2120 |
return sd_bus_error_setf(error, BUS_ERROR_UNIT_EXISTS, "Unit file '%s' exists on the host already, refusing.", item->name); |
| 2121 |
|
| 2122 |
r = unit_file_is_active(bus, item->name, error); |
| 2123 |
if (r < 0) |
| 2124 |
return r; |
| 2125 |
if (r > 0) |
| 2126 |
return sd_bus_error_setf(error, BUS_ERROR_UNIT_EXISTS, "Unit file '%s' is active already, refusing.", item->name); |
| 2127 |
} |
| 2128 |
|
| 2129 |
HASHMAP_FOREACH(item, unit_files) { |
| 2130 |
r = attach_unit_file( |
| 2131 |
scope, |
| 2132 |
&paths, |
| 2133 |
image->path, |
| 2134 |
image->type, |
| 2135 |
extension_images, |
| 2136 |
extension_releases, |
| 2137 |
pinned_root_image_policy, |
| 2138 |
pinned_ext_image_policy, |
| 2139 |
item, |
| 2140 |
os_release, |
| 2141 |
profile, |
| 2142 |
flags, |
| 2143 |
changes, |
| 2144 |
n_changes); |
| 2145 |
if (r < 0) |
| 2146 |
return sd_bus_error_set_errnof(error, r, "Failed to attach unit '%s': %m", item->name); |
| 2147 |
} |
| 2148 |
|
| 2149 |
/* We don't care too much for the image symlink/copy, it's just a convenience thing, it's not necessary for |
| 2150 |
* proper operation otherwise. */ |
| 2151 |
(void) install_image_and_extensions(scope, image, extension_images, flags, changes, n_changes); |
| 2152 |
|
| 2153 |
log_portable_verb( |
| 2154 |
"attached", |
| 2155 |
"MESSAGE_ID=" SD_MESSAGE_PORTABLE_ATTACHED_STR, |
| 2156 |
image->path, |
| 2157 |
profile, |
| 2158 |
extension_images, |
| 2159 |
/* extension_image_paths= */ NULL, |
| 2160 |
flags); |
| 2161 |
|
| 2162 |
return 0; |
| 2163 |
} |
| 2164 |
|
| 2165 |
static bool marker_matches_images(const char *marker, const char *name_or_path, char **extension_image_paths, bool match_all) { |
| 2166 |
_cleanup_strv_free_ char **root_and_extensions = NULL; |
| 2167 |
int r; |
| 2168 |
|
| 2169 |
assert(marker); |
| 2170 |
assert(name_or_path); |
| 2171 |
|
| 2172 |
/* If extensions were used when attaching, the marker will be a colon-separated |
| 2173 |
* list of images/paths. We enforce strict 1:1 matching, so that we are sure |
| 2174 |
* we are detaching exactly what was attached. |
| 2175 |
* For each image, starting with the root, we look for a token in the marker, |
| 2176 |
* and return a negative answer on any non-matching combination. |
| 2177 |
* If a partial match is allowed, then return immediately once it is found, otherwise |
| 2178 |
* ensure that everything matches. */ |
| 2179 |
|
| 2180 |
root_and_extensions = strv_new(name_or_path); |
| 2181 |
if (!root_and_extensions) |
| 2182 |
return -ENOMEM; |
| 2183 |
|
| 2184 |
r = strv_extend_strv(&root_and_extensions, extension_image_paths, false); |
| 2185 |
if (r < 0) |
| 2186 |
return r; |
| 2187 |
|
| 2188 |
/* Ensure the number of images passed matches the number of images listed in the marker */ |
| 2189 |
while (!isempty(marker)) |
| 2190 |
STRV_FOREACH(image_name_or_path, root_and_extensions) { |
| 2191 |
_cleanup_free_ char *image = NULL, *base_image = NULL, *base_image_name_or_path = NULL; |
| 2192 |
_cleanup_(pick_result_done) PickResult result = PICK_RESULT_NULL; |
| 2193 |
|
| 2194 |
r = extract_first_word(&marker, &image, ":", EXTRACT_UNQUOTE|EXTRACT_RETAIN_ESCAPE); |
| 2195 |
if (r < 0) |
| 2196 |
return log_debug_errno(r, "Failed to parse marker: %s", marker); |
| 2197 |
if (r == 0) |
| 2198 |
return false; |
| 2199 |
|
| 2200 |
r = path_extract_image_name(image, &base_image); |
| 2201 |
if (r < 0) |
| 2202 |
return log_debug_errno(r, "Failed to extract image name from %s, ignoring: %m", image); |
| 2203 |
|
| 2204 |
r = path_pick(/* toplevel_path= */ NULL, |
| 2205 |
/* toplevel_fd= */ AT_FDCWD, |
| 2206 |
*image_name_or_path, |
| 2207 |
pick_filter_image_any, |
| 2208 |
ELEMENTSOF(pick_filter_image_any), |
| 2209 |
PICK_ARCHITECTURE|PICK_TRIES|PICK_RESOLVE, |
| 2210 |
&result); |
| 2211 |
if (r < 0) |
| 2212 |
return r; |
| 2213 |
if (!result.path) |
| 2214 |
return log_debug_errno( |
| 2215 |
SYNTHETIC_ERRNO(ENOENT), |
| 2216 |
"No matching entry in .v/ directory %s found.", |
| 2217 |
*image_name_or_path); |
| 2218 |
|
| 2219 |
r = path_extract_image_name(result.path, &base_image_name_or_path); |
| 2220 |
if (r < 0) |
| 2221 |
return log_debug_errno(r, "Failed to extract image name from %s, ignoring: %m", result.path); |
| 2222 |
|
| 2223 |
if (!streq(base_image, base_image_name_or_path)) { |
| 2224 |
if (match_all) |
| 2225 |
return false; |
| 2226 |
} else if (!match_all) |
| 2227 |
return true; |
| 2228 |
} |
| 2229 |
|
| 2230 |
return match_all; |
| 2231 |
} |
| 2232 |
|
| 2233 |
static int test_chroot_dropin( |
| 2234 |
DIR *d, |
| 2235 |
const char *where, |
| 2236 |
const char *fname, |
| 2237 |
const char *name_or_path, |
| 2238 |
char **extension_image_paths, |
| 2239 |
char **ret_marker) { |
| 2240 |
|
| 2241 |
_cleanup_free_ char *line = NULL, *marker = NULL; |
| 2242 |
_cleanup_fclose_ FILE *f = NULL; |
| 2243 |
_cleanup_close_ int fd = -EBADF; |
| 2244 |
const char *p, *e, *k; |
| 2245 |
int r; |
| 2246 |
|
| 2247 |
assert(d); |
| 2248 |
assert(where); |
| 2249 |
assert(fname); |
| 2250 |
|
| 2251 |
/* We recognize unis created from portable images via the drop-in we created for them */ |
| 2252 |
|
| 2253 |
p = strjoina(fname, ".d/20-portable.conf"); |
| 2254 |
fd = openat(dirfd(d), p, O_RDONLY|O_CLOEXEC); |
| 2255 |
if (fd < 0) { |
| 2256 |
if (errno == ENOENT) |
| 2257 |
return 0; |
| 2258 |
|
| 2259 |
return log_debug_errno(errno, "Failed to open %s/%s: %m", where, p); |
| 2260 |
} |
| 2261 |
|
| 2262 |
r = take_fdopen_unlocked(&fd, "r", &f); |
| 2263 |
if (r < 0) |
| 2264 |
return log_debug_errno(r, "Failed to convert file handle: %m"); |
| 2265 |
|
| 2266 |
r = read_line(f, LONG_LINE_MAX, &line); |
| 2267 |
if (r < 0) |
| 2268 |
return log_debug_errno(r, "Failed to read from %s/%s: %m", where, p); |
| 2269 |
|
| 2270 |
e = startswith(line, PORTABLE_DROPIN_MARKER_BEGIN); |
| 2271 |
if (!e) |
| 2272 |
return 0; |
| 2273 |
|
| 2274 |
k = endswith(e, PORTABLE_DROPIN_MARKER_END); |
| 2275 |
if (!k) |
| 2276 |
return 0; |
| 2277 |
|
| 2278 |
marker = strndup(e, k - e); |
| 2279 |
if (!marker) |
| 2280 |
return -ENOMEM; |
| 2281 |
|
| 2282 |
if (!name_or_path) |
| 2283 |
r = true; |
| 2284 |
else |
| 2285 |
/* When detaching we want to match exactly on all images, but when inspecting we only need |
| 2286 |
* to get the state of one component */ |
| 2287 |
r = marker_matches_images(marker, name_or_path, extension_image_paths, ret_marker != NULL); |
| 2288 |
|
| 2289 |
if (ret_marker) |
| 2290 |
*ret_marker = TAKE_PTR(marker); |
| 2291 |
|
| 2292 |
return r; |
| 2293 |
} |
| 2294 |
|
| 2295 |
int portable_detach( |
| 2296 |
RuntimeScope scope, |
| 2297 |
sd_bus *bus, |
| 2298 |
const char *name_or_path, |
| 2299 |
char **extension_image_paths, |
| 2300 |
PortableFlags flags, |
| 2301 |
PortableChange **changes, |
| 2302 |
size_t *n_changes, |
| 2303 |
sd_bus_error *error) { |
| 2304 |
|
| 2305 |
_cleanup_(lookup_paths_done) LookupPaths paths = {}; |
| 2306 |
_cleanup_set_free_ Set *unit_files = NULL, *markers = NULL; |
| 2307 |
_cleanup_free_ char *extensions = NULL; |
| 2308 |
_cleanup_closedir_ DIR *d = NULL; |
| 2309 |
const char *where, *item; |
| 2310 |
int r, ret = 0; |
| 2311 |
|
| 2312 |
assert(scope < _RUNTIME_SCOPE_MAX); |
| 2313 |
assert(name_or_path); |
| 2314 |
|
| 2315 |
r = lookup_paths_init(&paths, scope, /* flags= */ 0, NULL); |
| 2316 |
if (r < 0) |
| 2317 |
return r; |
| 2318 |
|
| 2319 |
where = attached_path(&paths, flags); |
| 2320 |
|
| 2321 |
d = opendir(where); |
| 2322 |
if (!d) { |
| 2323 |
if (errno == ENOENT) |
| 2324 |
goto not_found; |
| 2325 |
|
| 2326 |
return log_debug_errno(errno, "Failed to open '%s' directory: %m", where); |
| 2327 |
} |
| 2328 |
|
| 2329 |
FOREACH_DIRENT(de, d, return log_debug_errno(errno, "Failed to enumerate '%s' directory: %m", where)) { |
| 2330 |
_cleanup_free_ char *marker = NULL, *unit_name = NULL; |
| 2331 |
const char *dot; |
| 2332 |
|
| 2333 |
/* When a portable service is enabled with "portablectl --copy=symlink --enable --now attach", |
| 2334 |
* and is disabled with "portablectl --enable --now detach", which calls DisableUnitFilesWithFlags |
| 2335 |
* DBus method, the main unit file is removed, but its drop-ins are not. Hence, here we need |
| 2336 |
* to list both main unit files and drop-in directories (without the main unit files). */ |
| 2337 |
|
| 2338 |
dot = endswith(de->d_name, ".d"); |
| 2339 |
if (dot) |
| 2340 |
unit_name = strndup(de->d_name, dot - de->d_name); |
| 2341 |
else |
| 2342 |
unit_name = strdup(de->d_name); |
| 2343 |
if (!unit_name) |
| 2344 |
return -ENOMEM; |
| 2345 |
|
| 2346 |
if (!unit_name_is_valid(unit_name, UNIT_NAME_ANY)) |
| 2347 |
continue; |
| 2348 |
|
| 2349 |
/* Filter out duplicates */ |
| 2350 |
if (set_contains(unit_files, unit_name)) |
| 2351 |
continue; |
| 2352 |
|
| 2353 |
if (dot ? !IN_SET(de->d_type, DT_LNK, DT_DIR) : !IN_SET(de->d_type, DT_LNK, DT_REG)) |
| 2354 |
continue; |
| 2355 |
|
| 2356 |
r = test_chroot_dropin(d, where, unit_name, name_or_path, extension_image_paths, &marker); |
| 2357 |
if (r < 0) |
| 2358 |
return r; |
| 2359 |
if (r == 0) |
| 2360 |
continue; |
| 2361 |
|
| 2362 |
if (!FLAGS_SET(flags, PORTABLE_REATTACH) && !FLAGS_SET(flags, PORTABLE_FORCE_ATTACH)) { |
| 2363 |
r = unit_file_is_active(bus, unit_name, error); |
| 2364 |
if (r < 0) |
| 2365 |
return r; |
| 2366 |
if (r > 0) |
| 2367 |
return sd_bus_error_setf(error, BUS_ERROR_UNIT_EXISTS, "Unit file '%s' is active, can't detach.", unit_name); |
| 2368 |
} |
| 2369 |
|
| 2370 |
r = set_ensure_consume(&unit_files, &string_hash_ops_free, TAKE_PTR(unit_name)); |
| 2371 |
if (r < 0) |
| 2372 |
return log_oom_debug(); |
| 2373 |
|
| 2374 |
for (const char *p = marker;;) { |
| 2375 |
_cleanup_free_ char *image = NULL; |
| 2376 |
|
| 2377 |
r = extract_first_word(&p, &image, ":", EXTRACT_UNESCAPE_SEPARATORS|EXTRACT_RETAIN_ESCAPE); |
| 2378 |
if (r < 0) |
| 2379 |
return log_debug_errno(r, "Failed to parse marker: %s", p); |
| 2380 |
if (r == 0) |
| 2381 |
break; |
| 2382 |
|
| 2383 |
if (path_is_absolute(image) && !image_in_search_path(scope, IMAGE_PORTABLE, NULL, image)) { |
| 2384 |
r = set_ensure_consume(&markers, &path_hash_ops_free, TAKE_PTR(image)); |
| 2385 |
if (r < 0) |
| 2386 |
return r; |
| 2387 |
} |
| 2388 |
} |
| 2389 |
} |
| 2390 |
|
| 2391 |
if (set_isempty(unit_files)) |
| 2392 |
goto not_found; |
| 2393 |
|
| 2394 |
SET_FOREACH(item, unit_files) { |
| 2395 |
_cleanup_free_ char *md = NULL; |
| 2396 |
|
| 2397 |
if (unlinkat(dirfd(d), item, 0) < 0) { |
| 2398 |
log_debug_errno(errno, "Can't remove unit file %s/%s: %m", where, item); |
| 2399 |
|
| 2400 |
if (errno != ENOENT && ret >= 0) |
| 2401 |
ret = -errno; |
| 2402 |
} else |
| 2403 |
portable_changes_add_with_prefix(changes, n_changes, PORTABLE_UNLINK, where, item, NULL); |
| 2404 |
|
| 2405 |
FOREACH_STRING(suffix, ".d/10-profile.conf", ".d/20-portable.conf") { |
| 2406 |
_cleanup_free_ char *dropin = NULL; |
| 2407 |
|
| 2408 |
dropin = strjoin(item, suffix); |
| 2409 |
if (!dropin) |
| 2410 |
return -ENOMEM; |
| 2411 |
|
| 2412 |
if (unlinkat(dirfd(d), dropin, 0) < 0) { |
| 2413 |
log_debug_errno(errno, "Can't remove drop-in %s/%s: %m", where, dropin); |
| 2414 |
|
| 2415 |
if (errno != ENOENT && ret >= 0) |
| 2416 |
ret = -errno; |
| 2417 |
} else |
| 2418 |
portable_changes_add_with_prefix(changes, n_changes, PORTABLE_UNLINK, where, dropin, NULL); |
| 2419 |
} |
| 2420 |
|
| 2421 |
md = strjoin(item, ".d"); |
| 2422 |
if (!md) |
| 2423 |
return -ENOMEM; |
| 2424 |
|
| 2425 |
if (unlinkat(dirfd(d), md, AT_REMOVEDIR) < 0) { |
| 2426 |
log_debug_errno(errno, "Can't remove drop-in directory %s/%s: %m", where, md); |
| 2427 |
|
| 2428 |
if (errno != ENOENT && ret >= 0) |
| 2429 |
ret = -errno; |
| 2430 |
} else |
| 2431 |
portable_changes_add_with_prefix(changes, n_changes, PORTABLE_UNLINK, where, md, NULL); |
| 2432 |
} |
| 2433 |
|
| 2434 |
/* Now, also drop any image symlink or copy, for images outside of the search path */ |
| 2435 |
SET_FOREACH(item, markers) { |
| 2436 |
_cleanup_free_ char *target = NULL; |
| 2437 |
|
| 2438 |
r = image_target_path(scope, item, flags, &target); |
| 2439 |
if (r < 0) { |
| 2440 |
log_debug_errno(r, "Failed to determine image path for '%s', ignoring: %m", item); |
| 2441 |
continue; |
| 2442 |
} |
| 2443 |
|
| 2444 |
r = rm_rf(target, REMOVE_ROOT | REMOVE_PHYSICAL | REMOVE_MISSING_OK | REMOVE_SYNCFS); |
| 2445 |
if (r < 0) { |
| 2446 |
log_debug_errno(r, "Can't remove image '%s': %m", target); |
| 2447 |
|
| 2448 |
if (r != -ENOENT) |
| 2449 |
RET_GATHER(ret, r); |
| 2450 |
} else |
| 2451 |
portable_changes_add(changes, n_changes, PORTABLE_UNLINK, target, NULL); |
| 2452 |
} |
| 2453 |
|
| 2454 |
/* Try to remove the unit file directory, if we can */ |
| 2455 |
if (rmdir(where) >= 0) |
| 2456 |
portable_changes_add(changes, n_changes, PORTABLE_UNLINK, where, NULL); |
| 2457 |
|
| 2458 |
log_portable_verb( |
| 2459 |
"detached", |
| 2460 |
"MESSAGE_ID=" SD_MESSAGE_PORTABLE_DETACHED_STR, |
| 2461 |
name_or_path, |
| 2462 |
/* profile= */ NULL, |
| 2463 |
/* extension_images= */ NULL, |
| 2464 |
extension_image_paths, |
| 2465 |
flags); |
| 2466 |
|
| 2467 |
return ret; |
| 2468 |
|
| 2469 |
not_found: |
| 2470 |
extensions = strv_join(extension_image_paths, ", "); |
| 2471 |
if (!extensions) |
| 2472 |
return -ENOMEM; |
| 2473 |
|
| 2474 |
r = sd_bus_error_setf(error, |
| 2475 |
BUS_ERROR_NO_SUCH_UNIT, |
| 2476 |
"No unit files associated with '%s%s%s' found attached to the system. Image not attached?", |
| 2477 |
name_or_path, |
| 2478 |
isempty(extensions) ? "" : "' or any of its extensions '", |
| 2479 |
isempty(extensions) ? "" : extensions); |
| 2480 |
return log_debug_errno(r, "%s", error->message); |
| 2481 |
} |
| 2482 |
|
| 2483 |
static int portable_get_state_internal( |
| 2484 |
RuntimeScope scope, |
| 2485 |
sd_bus *bus, |
| 2486 |
const char *name_or_path, |
| 2487 |
char **extension_image_paths, |
| 2488 |
PortableFlags flags, |
| 2489 |
PortableState *ret, |
| 2490 |
sd_bus_error *error) { |
| 2491 |
|
| 2492 |
_cleanup_(lookup_paths_done) LookupPaths paths = {}; |
| 2493 |
bool found_enabled = false, found_running = false; |
| 2494 |
_cleanup_set_free_ Set *unit_files = NULL; |
| 2495 |
_cleanup_closedir_ DIR *d = NULL; |
| 2496 |
const char *where; |
| 2497 |
int r; |
| 2498 |
|
| 2499 |
assert(scope < _RUNTIME_SCOPE_MAX); |
| 2500 |
assert(name_or_path); |
| 2501 |
assert(ret); |
| 2502 |
|
| 2503 |
r = lookup_paths_init(&paths, scope, /* flags= */ 0, NULL); |
| 2504 |
if (r < 0) |
| 2505 |
return r; |
| 2506 |
|
| 2507 |
where = attached_path(&paths, flags); |
| 2508 |
|
| 2509 |
d = opendir(where); |
| 2510 |
if (!d) { |
| 2511 |
if (errno == ENOENT) { |
| 2512 |
/* If the 'attached' directory doesn't exist at all, then we know for sure this image isn't attached. */ |
| 2513 |
*ret = PORTABLE_DETACHED; |
| 2514 |
return 0; |
| 2515 |
} |
| 2516 |
|
| 2517 |
return log_debug_errno(errno, "Failed to open '%s' directory: %m", where); |
| 2518 |
} |
| 2519 |
|
| 2520 |
FOREACH_DIRENT(de, d, return log_debug_errno(errno, "Failed to enumerate '%s' directory: %m", where)) { |
| 2521 |
UnitFileState state; |
| 2522 |
|
| 2523 |
if (!unit_name_is_valid(de->d_name, UNIT_NAME_ANY)) |
| 2524 |
continue; |
| 2525 |
|
| 2526 |
/* Filter out duplicates */ |
| 2527 |
if (set_contains(unit_files, de->d_name)) |
| 2528 |
continue; |
| 2529 |
|
| 2530 |
if (!IN_SET(de->d_type, DT_LNK, DT_REG)) |
| 2531 |
continue; |
| 2532 |
|
| 2533 |
r = test_chroot_dropin(d, where, de->d_name, name_or_path, extension_image_paths, NULL); |
| 2534 |
if (r < 0) |
| 2535 |
return r; |
| 2536 |
if (r == 0) |
| 2537 |
continue; |
| 2538 |
|
| 2539 |
r = unit_file_lookup_state(scope, &paths, de->d_name, &state); |
| 2540 |
if (r < 0) |
| 2541 |
return log_debug_errno(r, "Failed to determine unit file state of '%s': %m", de->d_name); |
| 2542 |
if (!IN_SET(state, UNIT_FILE_STATIC, UNIT_FILE_DISABLED, UNIT_FILE_LINKED, UNIT_FILE_LINKED_RUNTIME)) |
| 2543 |
found_enabled = true; |
| 2544 |
|
| 2545 |
r = unit_file_is_active(bus, de->d_name, error); |
| 2546 |
if (r < 0) |
| 2547 |
return r; |
| 2548 |
if (r > 0) |
| 2549 |
found_running = true; |
| 2550 |
|
| 2551 |
r = set_put_strdup(&unit_files, de->d_name); |
| 2552 |
if (r < 0) |
| 2553 |
return log_debug_errno(r, "Failed to add unit name '%s' to set: %m", de->d_name); |
| 2554 |
} |
| 2555 |
|
| 2556 |
*ret = found_running ? (!set_isempty(unit_files) && (flags & PORTABLE_RUNTIME) ? PORTABLE_RUNNING_RUNTIME : PORTABLE_RUNNING) : |
| 2557 |
found_enabled ? (flags & PORTABLE_RUNTIME ? PORTABLE_ENABLED_RUNTIME : PORTABLE_ENABLED) : |
| 2558 |
!set_isempty(unit_files) ? (flags & PORTABLE_RUNTIME ? PORTABLE_ATTACHED_RUNTIME : PORTABLE_ATTACHED) : PORTABLE_DETACHED; |
| 2559 |
|
| 2560 |
return 0; |
| 2561 |
} |
| 2562 |
|
| 2563 |
int portable_get_state( |
| 2564 |
RuntimeScope scope, |
| 2565 |
sd_bus *bus, |
| 2566 |
const char *name_or_path, |
| 2567 |
char **extension_image_paths, |
| 2568 |
PortableFlags flags, |
| 2569 |
PortableState *ret, |
| 2570 |
sd_bus_error *error) { |
| 2571 |
|
| 2572 |
PortableState state; |
| 2573 |
int r; |
| 2574 |
|
| 2575 |
assert(name_or_path); |
| 2576 |
assert(ret); |
| 2577 |
|
| 2578 |
/* We look for matching units twice: once in the regular directories, and once in the runtime directories — but |
| 2579 |
* the latter only if we didn't find anything in the former. */ |
| 2580 |
|
| 2581 |
r = portable_get_state_internal( |
| 2582 |
scope, |
| 2583 |
bus, |
| 2584 |
name_or_path, |
| 2585 |
extension_image_paths, |
| 2586 |
flags & ~PORTABLE_RUNTIME, |
| 2587 |
&state, |
| 2588 |
error); |
| 2589 |
if (r < 0) |
| 2590 |
return r; |
| 2591 |
|
| 2592 |
if (state == PORTABLE_DETACHED) { |
| 2593 |
r = portable_get_state_internal(scope, bus, name_or_path, extension_image_paths, flags | PORTABLE_RUNTIME, &state, error); |
| 2594 |
if (r < 0) |
| 2595 |
return r; |
| 2596 |
} |
| 2597 |
|
| 2598 |
*ret = state; |
| 2599 |
return 0; |
| 2600 |
} |
| 2601 |
|
| 2602 |
int portable_get_profiles(RuntimeScope scope, char ***ret) { |
| 2603 |
_cleanup_strv_free_ char **dirs = NULL; |
| 2604 |
int r; |
| 2605 |
|
| 2606 |
assert(ret); |
| 2607 |
|
| 2608 |
r = portable_profile_dirs(scope, &dirs); |
| 2609 |
if (r < 0) |
| 2610 |
return r; |
| 2611 |
|
| 2612 |
return conf_files_list_strv(ret, NULL, NULL, CONF_FILES_DIRECTORY|CONF_FILES_BASENAME|CONF_FILES_FILTER_MASKED, (const char* const*) dirs); |
| 2613 |
} |
| 2614 |
|
| 2615 |
static const char* const portable_change_type_table[_PORTABLE_CHANGE_TYPE_MAX] = { |
| 2616 |
[PORTABLE_COPY] = "copy", |
| 2617 |
[PORTABLE_MKDIR] = "mkdir", |
| 2618 |
[PORTABLE_SYMLINK] = "symlink", |
| 2619 |
[PORTABLE_UNLINK] = "unlink", |
| 2620 |
[PORTABLE_WRITE] = "write", |
| 2621 |
}; |
| 2622 |
|
| 2623 |
DEFINE_STRING_TABLE_LOOKUP(portable_change_type, int); |
| 2624 |
|
| 2625 |
static const char* const portable_state_table[_PORTABLE_STATE_MAX] = { |
| 2626 |
[PORTABLE_DETACHED] = "detached", |
| 2627 |
[PORTABLE_ATTACHED] = "attached", |
| 2628 |
[PORTABLE_ATTACHED_RUNTIME] = "attached-runtime", |
| 2629 |
[PORTABLE_ENABLED] = "enabled", |
| 2630 |
[PORTABLE_ENABLED_RUNTIME] = "enabled-runtime", |
| 2631 |
[PORTABLE_RUNNING] = "running", |
| 2632 |
[PORTABLE_RUNNING_RUNTIME] = "running-runtime", |
| 2633 |
}; |
| 2634 |
|
| 2635 |
DEFINE_STRING_TABLE_LOOKUP(portable_state, PortableState); |