| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <stdlib.h> |
| 4 |
#include <sys/stat.h> |
| 5 |
#include <unistd.h> |
| 6 |
|
| 7 |
#include "alloc-util.h" |
| 8 |
#include "efivars.h" |
| 9 |
#include "env-file.h" |
| 10 |
#include "env-util.h" |
| 11 |
#include "errno-util.h" |
| 12 |
#include "fd-util.h" |
| 13 |
#include "iovec-util.h" |
| 14 |
#include "locale-setup.h" |
| 15 |
#include "log.h" |
| 16 |
#include "proc-cmdline.h" |
| 17 |
#include "stat-util.h" |
| 18 |
#include "string-util.h" |
| 19 |
#include "strv.h" |
| 20 |
|
| 21 |
void locale_context_clear(LocaleContext *c) { |
| 22 |
assert(c); |
| 23 |
|
| 24 |
c->st = (struct stat) {}; |
| 25 |
|
| 26 |
for (LocaleVariable i = 0; i < _VARIABLE_LC_MAX; i++) |
| 27 |
c->locale[i] = mfree(c->locale[i]); |
| 28 |
} |
| 29 |
|
| 30 |
static int locale_context_load_proc(LocaleContext *c, LocaleLoadFlag flag) { |
| 31 |
int r; |
| 32 |
|
| 33 |
assert(c); |
| 34 |
|
| 35 |
if (!FLAGS_SET(flag, LOCALE_LOAD_PROC_CMDLINE)) |
| 36 |
return 0; |
| 37 |
|
| 38 |
locale_context_clear(c); |
| 39 |
|
| 40 |
r = proc_cmdline_get_key_many(PROC_CMDLINE_STRIP_RD_PREFIX, |
| 41 |
"locale.LANG", &c->locale[VARIABLE_LANG], |
| 42 |
"locale.LANGUAGE", &c->locale[VARIABLE_LANGUAGE], |
| 43 |
"locale.LC_CTYPE", &c->locale[VARIABLE_LC_CTYPE], |
| 44 |
"locale.LC_NUMERIC", &c->locale[VARIABLE_LC_NUMERIC], |
| 45 |
"locale.LC_TIME", &c->locale[VARIABLE_LC_TIME], |
| 46 |
"locale.LC_COLLATE", &c->locale[VARIABLE_LC_COLLATE], |
| 47 |
"locale.LC_MONETARY", &c->locale[VARIABLE_LC_MONETARY], |
| 48 |
"locale.LC_MESSAGES", &c->locale[VARIABLE_LC_MESSAGES], |
| 49 |
"locale.LC_PAPER", &c->locale[VARIABLE_LC_PAPER], |
| 50 |
"locale.LC_NAME", &c->locale[VARIABLE_LC_NAME], |
| 51 |
"locale.LC_ADDRESS", &c->locale[VARIABLE_LC_ADDRESS], |
| 52 |
"locale.LC_TELEPHONE", &c->locale[VARIABLE_LC_TELEPHONE], |
| 53 |
"locale.LC_MEASUREMENT", &c->locale[VARIABLE_LC_MEASUREMENT], |
| 54 |
"locale.LC_IDENTIFICATION", &c->locale[VARIABLE_LC_IDENTIFICATION]); |
| 55 |
if (r == -ENOENT) |
| 56 |
return 0; |
| 57 |
if (r < 0) |
| 58 |
return log_debug_errno(r, "Failed to read /proc/cmdline: %m"); |
| 59 |
return r; |
| 60 |
} |
| 61 |
|
| 62 |
static int locale_context_load_conf(LocaleContext *c, LocaleLoadFlag flag) { |
| 63 |
_cleanup_close_ int fd = -EBADF; |
| 64 |
struct stat st; |
| 65 |
int r; |
| 66 |
|
| 67 |
assert(c); |
| 68 |
|
| 69 |
if (!FLAGS_SET(flag, LOCALE_LOAD_LOCALE_CONF)) |
| 70 |
return 0; |
| 71 |
|
| 72 |
fd = RET_NERRNO(open(etc_locale_conf(), O_CLOEXEC | O_PATH)); |
| 73 |
if (fd == -ENOENT) |
| 74 |
return 0; |
| 75 |
if (fd < 0) |
| 76 |
return log_debug_errno(errno, "Failed to open %s: %m", "/etc/locale.conf"); |
| 77 |
|
| 78 |
if (fstat(fd, &st) < 0) |
| 79 |
return log_debug_errno(errno, "Failed to stat /etc/locale.conf: %m"); |
| 80 |
|
| 81 |
/* If the file is not changed, then we do not need to re-read the file. */ |
| 82 |
if (stat_inode_unmodified(&c->st, &st)) |
| 83 |
return 1; /* (already) loaded */ |
| 84 |
|
| 85 |
c->st = st; |
| 86 |
locale_context_clear(c); |
| 87 |
|
| 88 |
r = parse_env_file_fd(fd, etc_locale_conf(), |
| 89 |
"LANG", &c->locale[VARIABLE_LANG], |
| 90 |
"LANGUAGE", &c->locale[VARIABLE_LANGUAGE], |
| 91 |
"LC_CTYPE", &c->locale[VARIABLE_LC_CTYPE], |
| 92 |
"LC_NUMERIC", &c->locale[VARIABLE_LC_NUMERIC], |
| 93 |
"LC_TIME", &c->locale[VARIABLE_LC_TIME], |
| 94 |
"LC_COLLATE", &c->locale[VARIABLE_LC_COLLATE], |
| 95 |
"LC_MONETARY", &c->locale[VARIABLE_LC_MONETARY], |
| 96 |
"LC_MESSAGES", &c->locale[VARIABLE_LC_MESSAGES], |
| 97 |
"LC_PAPER", &c->locale[VARIABLE_LC_PAPER], |
| 98 |
"LC_NAME", &c->locale[VARIABLE_LC_NAME], |
| 99 |
"LC_ADDRESS", &c->locale[VARIABLE_LC_ADDRESS], |
| 100 |
"LC_TELEPHONE", &c->locale[VARIABLE_LC_TELEPHONE], |
| 101 |
"LC_MEASUREMENT", &c->locale[VARIABLE_LC_MEASUREMENT], |
| 102 |
"LC_IDENTIFICATION", &c->locale[VARIABLE_LC_IDENTIFICATION]); |
| 103 |
if (r < 0) |
| 104 |
return log_debug_errno(r, "Failed to read /etc/locale.conf: %m"); |
| 105 |
|
| 106 |
return 1; /* loaded */ |
| 107 |
} |
| 108 |
|
| 109 |
static int locale_context_load_env(LocaleContext *c, LocaleLoadFlag flag) { |
| 110 |
int r; |
| 111 |
|
| 112 |
assert(c); |
| 113 |
|
| 114 |
if (!FLAGS_SET(flag, LOCALE_LOAD_ENVIRONMENT)) |
| 115 |
return 0; |
| 116 |
|
| 117 |
locale_context_clear(c); |
| 118 |
|
| 119 |
/* Fill in what we got passed from systemd. */ |
| 120 |
for (LocaleVariable p = 0; p < _VARIABLE_LC_MAX; p++) { |
| 121 |
const char *name = ASSERT_PTR(locale_variable_to_string(p)); |
| 122 |
|
| 123 |
r = free_and_strdup(&c->locale[p], empty_to_null(getenv(name))); |
| 124 |
if (r < 0) |
| 125 |
return log_oom_debug(); |
| 126 |
} |
| 127 |
|
| 128 |
return 1; /* loaded */ |
| 129 |
} |
| 130 |
|
| 131 |
int locale_context_load(LocaleContext *c, LocaleLoadFlag flag) { |
| 132 |
int r; |
| 133 |
|
| 134 |
assert(c); |
| 135 |
|
| 136 |
r = locale_context_load_proc(c, flag); |
| 137 |
if (r > 0) |
| 138 |
goto finalize; |
| 139 |
|
| 140 |
r = locale_context_load_conf(c, flag); |
| 141 |
if (r != 0) |
| 142 |
goto finalize; |
| 143 |
|
| 144 |
r = locale_context_load_env(c, flag); |
| 145 |
|
| 146 |
finalize: |
| 147 |
if (r <= 0) { |
| 148 |
/* Nothing loaded, or error. */ |
| 149 |
locale_context_clear(c); |
| 150 |
return r; |
| 151 |
} |
| 152 |
|
| 153 |
if (FLAGS_SET(flag, LOCALE_LOAD_SIMPLIFY)) |
| 154 |
locale_variables_simplify(c->locale); |
| 155 |
|
| 156 |
return 0; |
| 157 |
} |
| 158 |
|
| 159 |
int locale_context_build_env(const LocaleContext *c, char ***ret_set, char ***ret_unset) { |
| 160 |
_cleanup_strv_free_ char **set = NULL, **unset = NULL; |
| 161 |
int r; |
| 162 |
|
| 163 |
assert(c); |
| 164 |
|
| 165 |
if (!ret_set && !ret_unset) |
| 166 |
return 0; |
| 167 |
|
| 168 |
for (LocaleVariable p = 0; p < _VARIABLE_LC_MAX; p++) { |
| 169 |
const char *name = ASSERT_PTR(locale_variable_to_string(p)); |
| 170 |
|
| 171 |
if (isempty(c->locale[p])) { |
| 172 |
if (!ret_unset) |
| 173 |
continue; |
| 174 |
r = strv_extend(&unset, name); |
| 175 |
} else { |
| 176 |
if (!ret_set) |
| 177 |
continue; |
| 178 |
r = strv_env_assign(&set, name, c->locale[p]); |
| 179 |
} |
| 180 |
if (r < 0) |
| 181 |
return r; |
| 182 |
} |
| 183 |
|
| 184 |
if (ret_set) |
| 185 |
*ret_set = TAKE_PTR(set); |
| 186 |
if (ret_unset) |
| 187 |
*ret_unset = TAKE_PTR(unset); |
| 188 |
return 0; |
| 189 |
} |
| 190 |
|
| 191 |
int locale_context_save(LocaleContext *c, char ***ret_set, char ***ret_unset) { |
| 192 |
_cleanup_strv_free_ char **set = NULL, **unset = NULL; |
| 193 |
int r; |
| 194 |
|
| 195 |
assert(c); |
| 196 |
|
| 197 |
/* Set values will be returned as strv in *ret on success. */ |
| 198 |
|
| 199 |
r = locale_context_build_env(c, &set, ret_unset ? &unset : NULL); |
| 200 |
if (r < 0) |
| 201 |
return r; |
| 202 |
|
| 203 |
if (strv_isempty(set)) { |
| 204 |
if (unlink(etc_locale_conf()) < 0) |
| 205 |
return errno == ENOENT ? 0 : -errno; |
| 206 |
|
| 207 |
c->st = (struct stat) {}; |
| 208 |
|
| 209 |
if (ret_set) |
| 210 |
*ret_set = NULL; |
| 211 |
if (ret_unset) |
| 212 |
*ret_unset = NULL; |
| 213 |
return 0; |
| 214 |
} |
| 215 |
|
| 216 |
r = write_env_file( |
| 217 |
AT_FDCWD, |
| 218 |
etc_locale_conf(), |
| 219 |
/* headers= */ NULL, |
| 220 |
set, |
| 221 |
WRITE_ENV_FILE_LABEL); |
| 222 |
if (r < 0) |
| 223 |
return r; |
| 224 |
|
| 225 |
if (stat(etc_locale_conf(), &c->st) < 0) |
| 226 |
return -errno; |
| 227 |
|
| 228 |
if (ret_set) |
| 229 |
*ret_set = TAKE_PTR(set); |
| 230 |
if (ret_unset) |
| 231 |
*ret_unset = TAKE_PTR(unset); |
| 232 |
return 0; |
| 233 |
} |
| 234 |
|
| 235 |
int locale_context_merge(const LocaleContext *c, char *l[_VARIABLE_LC_MAX]) { |
| 236 |
assert(c); |
| 237 |
assert(l); |
| 238 |
|
| 239 |
for (LocaleVariable p = 0; p < _VARIABLE_LC_MAX; p++) |
| 240 |
if (!isempty(c->locale[p]) && isempty(l[p])) { |
| 241 |
l[p] = strdup(c->locale[p]); |
| 242 |
if (!l[p]) |
| 243 |
return -ENOMEM; |
| 244 |
} |
| 245 |
|
| 246 |
return 0; |
| 247 |
} |
| 248 |
|
| 249 |
void locale_context_take(LocaleContext *c, char *l[_VARIABLE_LC_MAX]) { |
| 250 |
assert(c); |
| 251 |
assert(l); |
| 252 |
|
| 253 |
for (LocaleVariable p = 0; p < _VARIABLE_LC_MAX; p++) |
| 254 |
free_and_replace(c->locale[p], l[p]); |
| 255 |
} |
| 256 |
|
| 257 |
bool locale_context_equal(const LocaleContext *c, char *l[_VARIABLE_LC_MAX]) { |
| 258 |
assert(c); |
| 259 |
assert(l); |
| 260 |
|
| 261 |
for (LocaleVariable p = 0; p < _VARIABLE_LC_MAX; p++) |
| 262 |
if (!streq_ptr(c->locale[p], l[p])) |
| 263 |
return false; |
| 264 |
|
| 265 |
return true; |
| 266 |
} |
| 267 |
|
| 268 |
int locale_setup(char ***environment) { |
| 269 |
_cleanup_(locale_context_clear) LocaleContext c = {}; |
| 270 |
_cleanup_strv_free_ char **add = NULL; |
| 271 |
int r; |
| 272 |
|
| 273 |
assert(environment); |
| 274 |
|
| 275 |
r = locale_context_load(&c, LOCALE_LOAD_PROC_CMDLINE | LOCALE_LOAD_LOCALE_CONF); |
| 276 |
if (r < 0) |
| 277 |
return r; |
| 278 |
|
| 279 |
r = locale_context_build_env(&c, &add, NULL); |
| 280 |
if (r < 0) |
| 281 |
return r; |
| 282 |
|
| 283 |
if (strv_isempty(add)) { |
| 284 |
/* If no locale is configured then default to compile-time default. */ |
| 285 |
|
| 286 |
add = strv_new("LANG=" SYSTEMD_DEFAULT_LOCALE); |
| 287 |
if (!add) |
| 288 |
return -ENOMEM; |
| 289 |
} |
| 290 |
|
| 291 |
if (strv_isempty(*environment)) |
| 292 |
strv_free_and_replace(*environment, add); |
| 293 |
else { |
| 294 |
char **merged; |
| 295 |
|
| 296 |
merged = strv_env_merge(*environment, add); |
| 297 |
if (!merged) |
| 298 |
return -ENOMEM; |
| 299 |
|
| 300 |
strv_free_and_replace(*environment, merged); |
| 301 |
} |
| 302 |
|
| 303 |
return 0; |
| 304 |
} |
| 305 |
|
| 306 |
const char* etc_locale_conf(void) { |
| 307 |
static const char *cached = NULL; |
| 308 |
|
| 309 |
if (!cached) |
| 310 |
cached = secure_getenv("SYSTEMD_ETC_LOCALE_CONF") ?: "/etc/locale.conf"; |
| 311 |
|
| 312 |
return cached; |
| 313 |
} |
| 314 |
|
| 315 |
const char* etc_vconsole_conf(void) { |
| 316 |
static const char *cached = NULL; |
| 317 |
|
| 318 |
if (!cached) |
| 319 |
cached = secure_getenv("SYSTEMD_ETC_VCONSOLE_CONF") ?: "/etc/vconsole.conf"; |
| 320 |
|
| 321 |
return cached; |
| 322 |
} |
| 323 |
|
| 324 |
int locale_lang_from_efi(char **ret, LocaleLangFromEfiFlags flags) { |
| 325 |
int r; |
| 326 |
|
| 327 |
assert(ret); |
| 328 |
|
| 329 |
if (!is_efi_boot()) { |
| 330 |
*ret = NULL; |
| 331 |
return 0; |
| 332 |
} |
| 333 |
|
| 334 |
/* NB: unlike most other UEFI variables, PlatformLang is actually in 7bit ASCII! Hence we are not |
| 335 |
* using efi_get_variable_string() here */ |
| 336 |
_cleanup_(iovec_done) struct iovec iov = {}; |
| 337 |
r = efi_get_variable(EFI_GLOBAL_VARIABLE_STR("PlatformLang"), /* ret_attribute= */ NULL, &iov.iov_base, &iov.iov_len); |
| 338 |
if (r == -ENOENT) { |
| 339 |
*ret = NULL; |
| 340 |
return 0; |
| 341 |
} |
| 342 |
if (r < 0) |
| 343 |
return log_debug_errno(r, "Failed to read PlatformLang EFI variable: %m"); |
| 344 |
|
| 345 |
_cleanup_free_ char *tag = NULL; |
| 346 |
r = make_cstring(iov.iov_base, iov.iov_len, MAKE_CSTRING_ALLOW_TRAILING_NUL, &tag); |
| 347 |
if (r < 0) |
| 348 |
return log_debug_errno(r, "Failed to convert PlatformLang EFI variable to C string: %m"); |
| 349 |
|
| 350 |
/* Convert the UEFI BCP47 language tag into a glibc tag. We'll not bother with the complexity of the |
| 351 |
* whole spec, but just convert "xx-XX" into "xx_XX", with some flexibility on the case |
| 352 |
* sensitivity. This is a best-effort thing anyway. */ |
| 353 |
|
| 354 |
if (strlen(tag) != 5 || |
| 355 |
!strchr(LETTERS, tag[0]) || |
| 356 |
!strchr(LETTERS, tag[1]) || |
| 357 |
tag[2] != '-' || |
| 358 |
!strchr(LETTERS, tag[3]) || |
| 359 |
!strchr(LETTERS, tag[4])) { |
| 360 |
log_debug("PlatformLang variable does not have the form 'xx-XX', ignoring: %s", tag); |
| 361 |
*ret = NULL; |
| 362 |
return 0; |
| 363 |
} |
| 364 |
|
| 365 |
tag[0] = ascii_tolower(tag[0]); |
| 366 |
tag[1] = ascii_tolower(tag[1]); |
| 367 |
tag[2] = '_'; |
| 368 |
tag[3] = ascii_toupper(tag[3]); |
| 369 |
tag[4] = ascii_toupper(tag[4]); |
| 370 |
|
| 371 |
/* Let's optionally suppress en_US locale, since that's almost certainly just the built-in default |
| 372 |
* locale of the firmware. Since we typically prefer C.UTF-8 over en_US.UTF-8 as default, let's hence |
| 373 |
* suppress it. */ |
| 374 |
if (FLAGS_SET(flags, LOCALE_SUPPRESS_EN_US) && streq(tag, "en_US")) { |
| 375 |
log_debug("Firmware language is en_US, suppressing because likely just the firmware default."); |
| 376 |
*ret = NULL; |
| 377 |
return 0; |
| 378 |
} |
| 379 |
|
| 380 |
if (!strextend(&tag, ".UTF-8")) |
| 381 |
return -ENOMEM; |
| 382 |
|
| 383 |
if (FLAGS_SET(flags, LOCALE_REQUIRE_INSTALLED)) { |
| 384 |
r = locale_is_installed(tag); |
| 385 |
if (r < 0) |
| 386 |
return r; |
| 387 |
if (r == 0) { |
| 388 |
log_debug("Determined locale '%s' from PlatformLang, but it isn't installed, ignoring.", tag); |
| 389 |
*ret = NULL; |
| 390 |
return 0; |
| 391 |
} |
| 392 |
} |
| 393 |
|
| 394 |
*ret = TAKE_PTR(tag); |
| 395 |
return 1; |
| 396 |
} |