| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <stdlib.h> |
| 4 |
|
| 5 |
#include "alloc-util.h" |
| 6 |
#include "ansi-color.h" |
| 7 |
#include "chase.h" |
| 8 |
#include "dirent-util.h" |
| 9 |
#include "env-file.h" |
| 10 |
#include "errno-util.h" |
| 11 |
#include "escape.h" |
| 12 |
#include "fd-util.h" |
| 13 |
#include "fs-util.h" |
| 14 |
#include "glyph-util.h" |
| 15 |
#include "log.h" |
| 16 |
#include "os-util.h" |
| 17 |
#include "path-util.h" |
| 18 |
#include "stat-util.h" |
| 19 |
#include "string-table.h" |
| 20 |
#include "string-util.h" |
| 21 |
#include "strv.h" |
| 22 |
#include "time-util.h" |
| 23 |
#include "utf8.h" |
| 24 |
#include "xattr-util.h" |
| 25 |
|
| 26 |
static const char* const image_class_table[_IMAGE_CLASS_MAX] = { |
| 27 |
[IMAGE_MACHINE] = "machine", |
| 28 |
[IMAGE_PORTABLE] = "portable", |
| 29 |
[IMAGE_SYSEXT] = "sysext", |
| 30 |
[IMAGE_CONFEXT] = "confext", |
| 31 |
}; |
| 32 |
|
| 33 |
DEFINE_STRING_TABLE_LOOKUP(image_class, ImageClass); |
| 34 |
|
| 35 |
/* Helper struct for naming simplicity and reusability */ |
| 36 |
static const struct { |
| 37 |
const char *release_file_directory; |
| 38 |
const char *release_file_path_prefix; |
| 39 |
} image_class_release_info[_IMAGE_CLASS_MAX] = { |
| 40 |
[IMAGE_SYSEXT] = { |
| 41 |
.release_file_directory = "/usr/lib/extension-release.d/", |
| 42 |
.release_file_path_prefix = "/usr/lib/extension-release.d/extension-release.", |
| 43 |
}, |
| 44 |
[IMAGE_CONFEXT] = { |
| 45 |
.release_file_directory = "/etc/extension-release.d/", |
| 46 |
.release_file_path_prefix = "/etc/extension-release.d/extension-release.", |
| 47 |
} |
| 48 |
}; |
| 49 |
|
| 50 |
bool image_name_is_valid(const char *s) { |
| 51 |
if (!filename_is_valid(s)) |
| 52 |
return false; |
| 53 |
|
| 54 |
if (string_has_cc(s, NULL)) |
| 55 |
return false; |
| 56 |
|
| 57 |
if (!utf8_is_valid(s)) |
| 58 |
return false; |
| 59 |
|
| 60 |
/* Temporary files for atomically creating new files */ |
| 61 |
if (startswith(s, ".#")) |
| 62 |
return false; |
| 63 |
|
| 64 |
return true; |
| 65 |
} |
| 66 |
|
| 67 |
int path_extract_image_name(const char *path, char **ret) { |
| 68 |
_cleanup_free_ char *fn = NULL; |
| 69 |
int r; |
| 70 |
|
| 71 |
assert(path); |
| 72 |
assert(ret); |
| 73 |
|
| 74 |
/* Extract last component from path, without any "/" suffixes. */ |
| 75 |
r = path_extract_filename(path, &fn); |
| 76 |
if (r < 0) |
| 77 |
return r; |
| 78 |
if (r != O_DIRECTORY) { |
| 79 |
/* Chop off any image suffixes we recognize (unless we already know this must refer to some dir) */ |
| 80 |
char *m = ENDSWITH_SET(fn, ".sysext.raw", ".confext.raw", ".raw"); |
| 81 |
if (m) |
| 82 |
*m = 0; |
| 83 |
} |
| 84 |
|
| 85 |
/* Truncate the version/counting suffixes */ |
| 86 |
fn[strcspn(fn, "_+")] = 0; |
| 87 |
|
| 88 |
if (!image_name_is_valid(fn)) |
| 89 |
return -EINVAL; |
| 90 |
|
| 91 |
*ret = TAKE_PTR(fn); |
| 92 |
return 0; |
| 93 |
} |
| 94 |
|
| 95 |
int path_is_extension_tree(ImageClass image_class, const char *path, const char *extension, bool relax_extension_release_check) { |
| 96 |
int r; |
| 97 |
|
| 98 |
assert(path); |
| 99 |
|
| 100 |
/* Does the path exist at all? If not, generate an error immediately. This is useful so that a missing root dir |
| 101 |
* always results in -ENOENT, and we can properly distinguish the case where the whole root doesn't exist from |
| 102 |
* the case where just the os-release file is missing. */ |
| 103 |
r = access_nofollow(path, F_OK); |
| 104 |
if (r < 0) |
| 105 |
return r; |
| 106 |
|
| 107 |
/* We use /usr/lib/extension-release.d/extension-release[.NAME] as flag for something being a system extension, |
| 108 |
* /etc/extension-release.d/extension-release[.NAME] as flag for something being a system configuration, and finally, |
| 109 |
* and {/etc|/usr/lib}/os-release as a flag for something being an OS (when not an extension). */ |
| 110 |
r = open_extension_release(path, image_class, extension, relax_extension_release_check, NULL, NULL); |
| 111 |
if (r == -ENOENT) /* We got nothing */ |
| 112 |
return 0; |
| 113 |
if (r < 0) |
| 114 |
return r; |
| 115 |
|
| 116 |
return 1; |
| 117 |
} |
| 118 |
|
| 119 |
int fd_is_os_tree(int fd) { |
| 120 |
int r; |
| 121 |
|
| 122 |
assert(fd >= 0); |
| 123 |
|
| 124 |
r = open_extension_release_at( |
| 125 |
fd, |
| 126 |
IMAGE_MACHINE, |
| 127 |
/* extension= */ NULL, |
| 128 |
/* relax_extension_release_check= */ false, |
| 129 |
/* ret_path= */ NULL, |
| 130 |
/* ret_fd= */ NULL); |
| 131 |
if (r == -ENOENT) |
| 132 |
return false; |
| 133 |
if (r < 0) |
| 134 |
return r; |
| 135 |
|
| 136 |
return true; |
| 137 |
} |
| 138 |
|
| 139 |
static int extension_release_strict_xattr_value(int extension_release_fd, const char *extension_release_dir_path, const char *filename) { |
| 140 |
int r; |
| 141 |
|
| 142 |
assert(extension_release_fd >= 0); |
| 143 |
assert(extension_release_dir_path); |
| 144 |
assert(filename); |
| 145 |
|
| 146 |
/* No xattr or cannot parse it? Then skip this. */ |
| 147 |
r = getxattr_at_bool(extension_release_fd, /* path= */ NULL, "user.extension-release.strict", /* at_flags= */ 0); |
| 148 |
if (ERRNO_IS_NEG_XATTR_ABSENT(r)) |
| 149 |
return log_debug_errno(r, "%s/%s does not have user.extension-release.strict xattr, ignoring.", |
| 150 |
extension_release_dir_path, filename); |
| 151 |
if (r < 0) |
| 152 |
return log_debug_errno(r, "%s/%s: Failed to read 'user.extension-release.strict' extended attribute from file, ignoring: %m", |
| 153 |
extension_release_dir_path, filename); |
| 154 |
|
| 155 |
/* Explicitly set to request strict matching? Skip it. */ |
| 156 |
if (r > 0) { |
| 157 |
log_debug("%s/%s: 'user.extension-release.strict' attribute is true, ignoring file.", |
| 158 |
extension_release_dir_path, filename); |
| 159 |
return true; |
| 160 |
} |
| 161 |
|
| 162 |
log_debug("%s/%s: 'user.extension-release.strict' attribute is false%s", |
| 163 |
extension_release_dir_path, filename, |
| 164 |
glyph(GLYPH_ELLIPSIS)); |
| 165 |
|
| 166 |
return false; |
| 167 |
} |
| 168 |
|
| 169 |
int open_os_release_at(int rfd, char **ret_path, int *ret_fd) { |
| 170 |
const char *e; |
| 171 |
int r; |
| 172 |
|
| 173 |
assert(wildcard_fd_is_valid(rfd)); |
| 174 |
|
| 175 |
e = secure_getenv("SYSTEMD_OS_RELEASE"); |
| 176 |
if (e) |
| 177 |
return chaseat(rfd, rfd, e, /* flags= */ 0, ret_path, ret_fd); |
| 178 |
|
| 179 |
FOREACH_STRING(path, "/etc/os-release", "/usr/lib/os-release") { |
| 180 |
r = chaseat(rfd, rfd, path, /* flags= */ 0, ret_path, ret_fd); |
| 181 |
if (r != -ENOENT) |
| 182 |
return r; |
| 183 |
} |
| 184 |
|
| 185 |
return -ENOENT; |
| 186 |
} |
| 187 |
|
| 188 |
int open_os_release(const char *root, char **ret_path, int *ret_fd) { |
| 189 |
_cleanup_close_ int rfd = XAT_FDROOT, fd = -EBADF; |
| 190 |
_cleanup_free_ char *p = NULL; |
| 191 |
int r; |
| 192 |
|
| 193 |
if (!empty_or_root(root)) { |
| 194 |
rfd = open(root, O_CLOEXEC | O_DIRECTORY | O_PATH); |
| 195 |
if (rfd < 0) |
| 196 |
return -errno; |
| 197 |
} |
| 198 |
|
| 199 |
r = open_os_release_at(rfd, ret_path ? &p : NULL, ret_fd ? &fd : NULL); |
| 200 |
if (r < 0) |
| 201 |
return r; |
| 202 |
|
| 203 |
if (ret_path) { |
| 204 |
r = chaseat_prefix_root(p, root, ret_path); |
| 205 |
if (r < 0) |
| 206 |
return r; |
| 207 |
} |
| 208 |
|
| 209 |
if (ret_fd) |
| 210 |
*ret_fd = TAKE_FD(fd); |
| 211 |
|
| 212 |
return 0; |
| 213 |
} |
| 214 |
|
| 215 |
int open_extension_release_at( |
| 216 |
int rfd, |
| 217 |
ImageClass image_class, |
| 218 |
const char *extension, |
| 219 |
bool relax_extension_release_check, |
| 220 |
char **ret_path, |
| 221 |
int *ret_fd) { |
| 222 |
|
| 223 |
_cleanup_free_ char *dir_path = NULL, *path_found = NULL; |
| 224 |
_cleanup_close_ int fd_found = -EBADF; |
| 225 |
_cleanup_closedir_ DIR *dir = NULL; |
| 226 |
bool found = false; |
| 227 |
const char *p; |
| 228 |
int r; |
| 229 |
|
| 230 |
assert(wildcard_fd_is_valid(rfd)); |
| 231 |
assert(!extension || (image_class >= 0 && image_class < _IMAGE_CLASS_MAX)); |
| 232 |
|
| 233 |
if (!extension) |
| 234 |
return open_os_release_at(rfd, ret_path, ret_fd); |
| 235 |
|
| 236 |
if (!IN_SET(image_class, IMAGE_SYSEXT, IMAGE_CONFEXT)) |
| 237 |
return -EINVAL; |
| 238 |
|
| 239 |
if (!image_name_is_valid(extension)) |
| 240 |
return log_debug_errno(SYNTHETIC_ERRNO(EINVAL), "The extension name %s is invalid.", extension); |
| 241 |
|
| 242 |
p = strjoina(image_class_release_info[image_class].release_file_path_prefix, extension); |
| 243 |
r = chaseat(rfd, rfd, p, /* flags= */ 0, ret_path, ret_fd); |
| 244 |
log_full_errno_zerook(LOG_DEBUG, MIN(r, 0), "Checking for %s: %m", p); |
| 245 |
if (r != -ENOENT) |
| 246 |
return r; |
| 247 |
|
| 248 |
/* Cannot find the expected extension-release file? The image filename might have been mangled on |
| 249 |
* deployment, so fallback to checking for any file in the extension-release.d directory, and return |
| 250 |
* the first one with a user.extension-release xattr instead. The user.extension-release.strict |
| 251 |
* xattr is checked to ensure the author of the image considers it OK if names do not match. */ |
| 252 |
|
| 253 |
p = image_class_release_info[image_class].release_file_directory; |
| 254 |
r = chase_and_opendirat(rfd, rfd, p, /* chase_flags= */ 0, &dir_path, &dir); |
| 255 |
if (r < 0) |
| 256 |
return log_debug_errno(r, "Cannot open %s, ignoring: %m", p); |
| 257 |
|
| 258 |
FOREACH_DIRENT(de, dir, return -errno) { |
| 259 |
_cleanup_close_ int fd = -EBADF; |
| 260 |
const char *image_name; |
| 261 |
|
| 262 |
if (!IN_SET(de->d_type, DT_REG, DT_UNKNOWN)) |
| 263 |
continue; |
| 264 |
|
| 265 |
image_name = startswith(de->d_name, "extension-release."); |
| 266 |
if (!image_name) |
| 267 |
continue; |
| 268 |
|
| 269 |
if (!image_name_is_valid(image_name)) { |
| 270 |
log_debug("%s/%s is not a valid release file name, ignoring.", dir_path, de->d_name); |
| 271 |
continue; |
| 272 |
} |
| 273 |
|
| 274 |
/* We already chased the directory, and checked that this is a real file, so we shouldn't |
| 275 |
* fail to open it. */ |
| 276 |
fd = openat(dirfd(dir), de->d_name, O_PATH|O_CLOEXEC|O_NOFOLLOW); |
| 277 |
if (fd < 0) |
| 278 |
return log_debug_errno(errno, "Failed to open release file %s/%s: %m", dir_path, de->d_name); |
| 279 |
|
| 280 |
/* Really ensure it is a regular file after we open it. */ |
| 281 |
r = fd_verify_regular(fd); |
| 282 |
if (r < 0) { |
| 283 |
log_debug_errno(r, "%s/%s is not a regular file, ignoring: %m", dir_path, de->d_name); |
| 284 |
continue; |
| 285 |
} |
| 286 |
|
| 287 |
if (!relax_extension_release_check) { |
| 288 |
_cleanup_free_ char *base_extension = NULL; |
| 289 |
|
| 290 |
r = path_extract_image_name(extension, &base_extension); |
| 291 |
if (r < 0) { |
| 292 |
log_debug_errno(r, "Failed to extract image name from %s, ignoring: %m", extension); |
| 293 |
continue; |
| 294 |
} |
| 295 |
|
| 296 |
if (!streq(image_name, base_extension) && |
| 297 |
extension_release_strict_xattr_value(fd, dir_path, image_name) != 0) |
| 298 |
continue; |
| 299 |
} |
| 300 |
|
| 301 |
/* We already found what we were looking for, but there's another candidate? We treat this as |
| 302 |
* an error, as we want to enforce that there are no ambiguities in case we are in the |
| 303 |
* fallback path. */ |
| 304 |
if (found) |
| 305 |
return -ENOTUNIQ; |
| 306 |
|
| 307 |
found = true; |
| 308 |
|
| 309 |
if (ret_fd) |
| 310 |
fd_found = TAKE_FD(fd); |
| 311 |
|
| 312 |
if (ret_path) { |
| 313 |
path_found = path_join(dir_path, de->d_name); |
| 314 |
if (!path_found) |
| 315 |
return -ENOMEM; |
| 316 |
} |
| 317 |
} |
| 318 |
if (!found) |
| 319 |
return -ENOENT; |
| 320 |
|
| 321 |
if (ret_fd) |
| 322 |
*ret_fd = TAKE_FD(fd_found); |
| 323 |
if (ret_path) |
| 324 |
*ret_path = TAKE_PTR(path_found); |
| 325 |
|
| 326 |
return 0; |
| 327 |
} |
| 328 |
|
| 329 |
int open_extension_release( |
| 330 |
const char *root, |
| 331 |
ImageClass image_class, |
| 332 |
const char *extension, |
| 333 |
bool relax_extension_release_check, |
| 334 |
char **ret_path, |
| 335 |
int *ret_fd) { |
| 336 |
|
| 337 |
_cleanup_close_ int rfd = XAT_FDROOT, fd = -EBADF; |
| 338 |
_cleanup_free_ char *p = NULL; |
| 339 |
int r; |
| 340 |
|
| 341 |
if (!empty_or_root(root)) { |
| 342 |
rfd = open(root, O_CLOEXEC | O_DIRECTORY | O_PATH); |
| 343 |
if (rfd < 0) |
| 344 |
return -errno; |
| 345 |
} |
| 346 |
|
| 347 |
r = open_extension_release_at(rfd, image_class, extension, relax_extension_release_check, |
| 348 |
ret_path ? &p : NULL, ret_fd ? &fd : NULL); |
| 349 |
if (r < 0) |
| 350 |
return r; |
| 351 |
|
| 352 |
if (ret_path) { |
| 353 |
r = chaseat_prefix_root(p, root, ret_path); |
| 354 |
if (r < 0) |
| 355 |
return r; |
| 356 |
} |
| 357 |
|
| 358 |
if (ret_fd) |
| 359 |
*ret_fd = TAKE_FD(fd); |
| 360 |
|
| 361 |
return 0; |
| 362 |
} |
| 363 |
|
| 364 |
static int parse_extension_release_atv( |
| 365 |
int rfd, |
| 366 |
ImageClass image_class, |
| 367 |
const char *extension, |
| 368 |
bool relax_extension_release_check, |
| 369 |
va_list ap) { |
| 370 |
|
| 371 |
_cleanup_close_ int fd = -EBADF; |
| 372 |
_cleanup_free_ char *p = NULL; |
| 373 |
int r; |
| 374 |
|
| 375 |
assert(wildcard_fd_is_valid(rfd)); |
| 376 |
|
| 377 |
r = open_extension_release_at(rfd, image_class, extension, relax_extension_release_check, &p, &fd); |
| 378 |
if (r < 0) |
| 379 |
return r; |
| 380 |
|
| 381 |
return parse_env_file_fdv(fd, p, ap); |
| 382 |
} |
| 383 |
|
| 384 |
int parse_extension_release_at_sentinel( |
| 385 |
int rfd, |
| 386 |
ImageClass image_class, |
| 387 |
bool relax_extension_release_check, |
| 388 |
const char *extension, |
| 389 |
...) { |
| 390 |
|
| 391 |
va_list ap; |
| 392 |
int r; |
| 393 |
|
| 394 |
assert(wildcard_fd_is_valid(rfd)); |
| 395 |
|
| 396 |
va_start(ap, extension); |
| 397 |
r = parse_extension_release_atv(rfd, image_class, extension, relax_extension_release_check, ap); |
| 398 |
va_end(ap); |
| 399 |
return r; |
| 400 |
} |
| 401 |
|
| 402 |
int parse_extension_release_sentinel( |
| 403 |
const char *root, |
| 404 |
ImageClass image_class, |
| 405 |
bool relax_extension_release_check, |
| 406 |
const char *extension, |
| 407 |
...) { |
| 408 |
|
| 409 |
_cleanup_close_ int rfd = XAT_FDROOT; |
| 410 |
va_list ap; |
| 411 |
int r; |
| 412 |
|
| 413 |
if (!empty_or_root(root)) { |
| 414 |
rfd = open(root, O_CLOEXEC | O_DIRECTORY | O_PATH); |
| 415 |
if (rfd < 0) |
| 416 |
return -errno; |
| 417 |
} |
| 418 |
|
| 419 |
va_start(ap, extension); |
| 420 |
r = parse_extension_release_atv(rfd, image_class, extension, relax_extension_release_check, ap); |
| 421 |
va_end(ap); |
| 422 |
|
| 423 |
return r; |
| 424 |
} |
| 425 |
|
| 426 |
int load_os_release_pairs_with_prefix(const char *root, const char *prefix, char ***ret) { |
| 427 |
_cleanup_strv_free_ char **os_release_pairs = NULL, **os_release_pairs_prefixed = NULL; |
| 428 |
int r; |
| 429 |
|
| 430 |
r = load_os_release_pairs(root, &os_release_pairs); |
| 431 |
if (r < 0) |
| 432 |
return r; |
| 433 |
|
| 434 |
STRV_FOREACH_PAIR(p, q, os_release_pairs) { |
| 435 |
char *line; |
| 436 |
|
| 437 |
/* We strictly return only the four main ID fields and ignore the rest */ |
| 438 |
if (!STR_IN_SET(*p, "ID", "VERSION_ID", "BUILD_ID", "VARIANT_ID")) |
| 439 |
continue; |
| 440 |
|
| 441 |
ascii_strlower(*p); |
| 442 |
line = strjoin(prefix, *p, "=", *q); |
| 443 |
if (!line) |
| 444 |
return -ENOMEM; |
| 445 |
r = strv_consume(&os_release_pairs_prefixed, line); |
| 446 |
if (r < 0) |
| 447 |
return r; |
| 448 |
} |
| 449 |
|
| 450 |
*ret = TAKE_PTR(os_release_pairs_prefixed); |
| 451 |
|
| 452 |
return 0; |
| 453 |
} |
| 454 |
|
| 455 |
int load_extension_release_pairs(const char *root, ImageClass image_class, const char *extension, bool relax_extension_release_check, char ***ret) { |
| 456 |
_cleanup_close_ int fd = -EBADF; |
| 457 |
_cleanup_free_ char *p = NULL; |
| 458 |
int r; |
| 459 |
|
| 460 |
r = open_extension_release(root, image_class, extension, relax_extension_release_check, &p, &fd); |
| 461 |
if (r < 0) |
| 462 |
return r; |
| 463 |
|
| 464 |
return load_env_file_pairs_fd(fd, p, ret); |
| 465 |
} |
| 466 |
|
| 467 |
int os_release_support_ended(const char *support_end, bool quiet, usec_t *ret_eol) { |
| 468 |
_cleanup_free_ char *_support_end_alloc = NULL; |
| 469 |
int r; |
| 470 |
|
| 471 |
if (!support_end) { |
| 472 |
/* If the caller has the variably handy, they can pass it in. If not, we'll read it |
| 473 |
* ourselves. */ |
| 474 |
|
| 475 |
r = parse_os_release(NULL, |
| 476 |
"SUPPORT_END", &_support_end_alloc); |
| 477 |
if (r < 0 && r != -ENOENT) |
| 478 |
return log_full_errno(quiet ? LOG_DEBUG : LOG_WARNING, r, |
| 479 |
"Failed to read os-release file, ignoring: %m"); |
| 480 |
|
| 481 |
support_end = _support_end_alloc; |
| 482 |
} |
| 483 |
|
| 484 |
if (isempty(support_end)) { /* An empty string is a explicit way to say "no EOL exists" */ |
| 485 |
if (ret_eol) |
| 486 |
*ret_eol = USEC_INFINITY; |
| 487 |
|
| 488 |
return false; /* no end date defined */ |
| 489 |
} |
| 490 |
|
| 491 |
struct tm tm = {}; |
| 492 |
const char *k = strptime(support_end, "%Y-%m-%d", &tm); |
| 493 |
if (!k || *k) |
| 494 |
return log_full_errno(quiet ? LOG_DEBUG : LOG_WARNING, SYNTHETIC_ERRNO(EINVAL), |
| 495 |
"Failed to parse SUPPORT_END= from os-release file, ignoring: %s", support_end); |
| 496 |
|
| 497 |
usec_t eol; |
| 498 |
r = mktime_or_timegm_usec(&tm, /* utc= */ true, &eol); |
| 499 |
if (r < 0) |
| 500 |
return log_full_errno(quiet ? LOG_DEBUG : LOG_WARNING, r, |
| 501 |
"Failed to convert SUPPORT_END= time from os-release file, ignoring: %m"); |
| 502 |
|
| 503 |
if (ret_eol) |
| 504 |
*ret_eol = eol; |
| 505 |
|
| 506 |
return now(CLOCK_REALTIME) > eol; |
| 507 |
} |
| 508 |
|
| 509 |
const char* os_release_pretty_name(const char *pretty_name, const char *name) { |
| 510 |
/* Distills a "pretty" name to show from os-release data. First argument is supposed to be the |
| 511 |
* PRETTY_NAME= field, the second one the NAME= field. This function is trivial, of course, and |
| 512 |
* exists mostly to ensure we use the same logic wherever possible. */ |
| 513 |
|
| 514 |
return empty_to_null(pretty_name) ?: |
| 515 |
empty_to_null(name) ?: "Linux"; |
| 516 |
} |
| 517 |
|
| 518 |
char *unescape_fancy_name(char **fancy_name) { |
| 519 |
assert(fancy_name); |
| 520 |
|
| 521 |
/* Checks if the fancy name is valid, unescapes if it is, nullifies it if not */ |
| 522 |
|
| 523 |
_cleanup_free_ char *unescaped_fancy_name = NULL; |
| 524 |
|
| 525 |
if (isempty(*fancy_name)) |
| 526 |
goto clear; |
| 527 |
|
| 528 |
/* We undo one level of C escapes on this */ |
| 529 |
ssize_t n = cunescape(*fancy_name, /* flags= */ 0, &unescaped_fancy_name); |
| 530 |
if (n < 0) { |
| 531 |
log_debug_errno((int) n, "Failed to unescape FANCY_NAME= string, suppressing: %m"); |
| 532 |
goto clear; |
| 533 |
} |
| 534 |
|
| 535 |
if (!utf8_is_valid(unescaped_fancy_name)) { |
| 536 |
log_debug("Unescaped FANCY_NAME= string is not valid UTF-8, suppressing."); |
| 537 |
goto clear; |
| 538 |
} |
| 539 |
|
| 540 |
free_and_replace(*fancy_name, unescaped_fancy_name); |
| 541 |
return *fancy_name; |
| 542 |
|
| 543 |
clear: |
| 544 |
*fancy_name = mfree(*fancy_name); |
| 545 |
return NULL; |
| 546 |
} |
| 547 |
|
| 548 |
bool use_fancy_name(const char *fancy_name) { |
| 549 |
|
| 550 |
/* Decides whether to show the specified fancy name */ |
| 551 |
|
| 552 |
if (isempty(fancy_name)) |
| 553 |
return false; |
| 554 |
|
| 555 |
if (!colors_enabled()) |
| 556 |
return false; |
| 557 |
|
| 558 |
return emoji_enabled() || ascii_is_valid(fancy_name); |
| 559 |
} |