1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
13 #include "constants.h"
14 #include "dirent-util.h"
19 #include "locale-util.h"
20 #include "missing_syscall.h"
21 #include "path-util.h"
23 #include "string-table.h"
24 #include "string-util.h"
28 static char *normalize_locale(const char *name
) {
31 /* Locale names are weird: glibc has some magic rules when looking for the charset name on disk: it
32 * lowercases everything, and removes most special chars. This means the official .UTF-8 suffix
33 * becomes .utf8 when looking things up on disk. When enumerating locales, let's do the reverse
34 * operation, and go back to ".UTF-8" which appears to be the more commonly accepted name. We only do
35 * that for UTF-8 however, since it's kinda the only charset that matters. */
37 e
= endswith(name
, ".utf8");
39 _cleanup_free_
char *prefix
= NULL
;
41 prefix
= strndup(name
, e
- name
);
45 return strjoin(prefix
, ".UTF-8");
48 e
= strstr(name
, ".utf8@");
50 _cleanup_free_
char *prefix
= NULL
;
52 prefix
= strndup(name
, e
- name
);
56 return strjoin(prefix
, ".UTF-8@", e
+ 6);
62 static int add_locales_from_archive(Set
*locales
) {
63 /* Stolen from glibc... */
69 /* Name hash table. */
70 uint32_t namehash_offset
;
71 uint32_t namehash_used
;
72 uint32_t namehash_size
;
74 uint32_t string_offset
;
77 /* Table with locale records. */
78 uint32_t locrectab_offset
;
79 uint32_t locrectab_used
;
80 uint32_t locrectab_size
;
81 /* MD5 sum hash table. */
82 uint32_t sumhash_offset
;
83 uint32_t sumhash_used
;
84 uint32_t sumhash_size
;
88 /* Hash value of the name. */
90 /* Offset of the name in the string table. */
92 /* Offset of the locale record. */
93 uint32_t locrec_offset
;
96 const struct locarhead
*h
;
97 const struct namehashent
*e
;
98 const void *p
= MAP_FAILED
;
99 _cleanup_close_
int fd
= -EBADF
;
104 fd
= open("/usr/lib/locale/locale-archive", O_RDONLY
|O_NOCTTY
|O_CLOEXEC
);
106 return errno
== ENOENT
? 0 : -errno
;
108 if (fstat(fd
, &st
) < 0)
111 if (!S_ISREG(st
.st_mode
))
114 if (st
.st_size
< (off_t
) sizeof(struct locarhead
))
117 if (file_offset_beyond_memory_size(st
.st_size
))
120 p
= mmap(NULL
, st
.st_size
, PROT_READ
, MAP_SHARED
, fd
, 0);
124 h
= (const struct locarhead
*) p
;
125 if (h
->magic
!= 0xde020109 ||
126 h
->namehash_offset
+ h
->namehash_size
> st
.st_size
||
127 h
->string_offset
+ h
->string_size
> st
.st_size
||
128 h
->locrectab_offset
+ h
->locrectab_size
> st
.st_size
||
129 h
->sumhash_offset
+ h
->sumhash_size
> st
.st_size
) {
134 e
= (const struct namehashent
*) ((const uint8_t*) p
+ h
->namehash_offset
);
135 for (size_t i
= 0; i
< h
->namehash_size
; i
++) {
138 if (e
[i
].locrec_offset
== 0)
141 if (!utf8_is_valid((char*) p
+ e
[i
].name_offset
))
144 z
= normalize_locale((char*) p
+ e
[i
].name_offset
);
150 r
= set_consume(locales
, z
);
159 munmap((void*) p
, sz
);
164 static int add_locales_from_libdir(Set
*locales
) {
165 _cleanup_closedir_
DIR *dir
= NULL
;
168 dir
= opendir("/usr/lib/locale");
170 return errno
== ENOENT
? 0 : -errno
;
172 FOREACH_DIRENT(de
, dir
, return -errno
) {
175 if (de
->d_type
!= DT_DIR
)
178 z
= normalize_locale(de
->d_name
);
182 r
= set_consume(locales
, z
);
183 if (r
< 0 && r
!= -EEXIST
)
190 int get_locales(char ***ret
) {
191 _cleanup_set_free_free_ Set
*locales
= NULL
;
192 _cleanup_strv_free_
char **l
= NULL
;
195 locales
= set_new(&string_hash_ops
);
199 r
= add_locales_from_archive(locales
);
200 if (r
< 0 && r
!= -ENOENT
)
203 r
= add_locales_from_libdir(locales
);
208 SET_FOREACH(locale
, locales
) {
209 r
= locale_is_installed(locale
);
213 free(set_remove(locales
, locale
));
216 l
= set_get_strv(locales
);
220 /* Now, all elements are owned by strv 'l'. Hence, do not call set_free_free(). */
221 locales
= set_free(locales
);
223 r
= getenv_bool("SYSTEMD_LIST_NON_UTF8_LOCALES");
224 if (r
== -ENXIO
|| r
== 0) {
227 /* Filter out non-UTF-8 locales, because it's 2019, by default */
228 for (a
= b
= l
; *a
; a
++) {
230 if (endswith(*a
, "UTF-8") ||
231 strstr(*a
, ".UTF-8@"))
240 log_debug_errno(r
, "Failed to parse $SYSTEMD_LIST_NON_UTF8_LOCALES as boolean");
249 bool locale_is_valid(const char *name
) {
254 if (strlen(name
) >= 128)
257 if (!utf8_is_valid(name
))
260 if (!filename_is_valid(name
))
263 if (!string_is_safe(name
))
269 int locale_is_installed(const char *name
) {
270 if (!locale_is_valid(name
))
273 if (STR_IN_SET(name
, "C", "POSIX")) /* These ones are always OK */
276 _cleanup_(freelocalep
) locale_t loc
=
277 newlocale(LC_ALL_MASK
, name
, 0);
278 if (loc
== (locale_t
) 0)
279 return errno
== ENOMEM
? -ENOMEM
: false;
284 bool is_locale_utf8(void) {
285 static int cached_answer
= -1;
289 /* Note that we default to 'true' here, since today UTF8 is
290 * pretty much supported everywhere. */
292 if (cached_answer
>= 0)
295 r
= getenv_bool_secure("SYSTEMD_UTF8");
299 } else if (r
!= -ENXIO
)
300 log_debug_errno(r
, "Failed to parse $SYSTEMD_UTF8, ignoring: %m");
302 /* This function may be called from libsystemd, and setlocale() is not thread safe. Assuming yes. */
303 if (gettid() != raw_getpid()) {
304 cached_answer
= true;
308 if (!setlocale(LC_ALL
, "")) {
309 cached_answer
= true;
313 set
= nl_langinfo(CODESET
);
315 cached_answer
= true;
319 if (streq(set
, "UTF-8")) {
320 cached_answer
= true;
324 /* For LC_CTYPE=="C" return true, because CTYPE is effectively
325 * unset and everything can do to UTF-8 nowadays. */
326 set
= setlocale(LC_CTYPE
, NULL
);
328 cached_answer
= true;
332 /* Check result, but ignore the result if C was set
335 STR_IN_SET(set
, "C", "POSIX") &&
337 !getenv("LC_CTYPE") &&
341 return (bool) cached_answer
;
344 void locale_variables_free(char *l
[_VARIABLE_LC_MAX
]) {
345 free_many_charp(l
, _VARIABLE_LC_MAX
);
348 void locale_variables_simplify(char *l
[_VARIABLE_LC_MAX
]) {
351 for (LocaleVariable p
= 0; p
< _VARIABLE_LC_MAX
; p
++) {
352 if (p
== VARIABLE_LANG
)
354 if (isempty(l
[p
]) || streq_ptr(l
[VARIABLE_LANG
], l
[p
]))
359 static const char * const locale_variable_table
[_VARIABLE_LC_MAX
] = {
360 [VARIABLE_LANG
] = "LANG",
361 [VARIABLE_LANGUAGE
] = "LANGUAGE",
362 [VARIABLE_LC_CTYPE
] = "LC_CTYPE",
363 [VARIABLE_LC_NUMERIC
] = "LC_NUMERIC",
364 [VARIABLE_LC_TIME
] = "LC_TIME",
365 [VARIABLE_LC_COLLATE
] = "LC_COLLATE",
366 [VARIABLE_LC_MONETARY
] = "LC_MONETARY",
367 [VARIABLE_LC_MESSAGES
] = "LC_MESSAGES",
368 [VARIABLE_LC_PAPER
] = "LC_PAPER",
369 [VARIABLE_LC_NAME
] = "LC_NAME",
370 [VARIABLE_LC_ADDRESS
] = "LC_ADDRESS",
371 [VARIABLE_LC_TELEPHONE
] = "LC_TELEPHONE",
372 [VARIABLE_LC_MEASUREMENT
] = "LC_MEASUREMENT",
373 [VARIABLE_LC_IDENTIFICATION
] = "LC_IDENTIFICATION"
376 DEFINE_STRING_TABLE_LOOKUP(locale_variable
, LocaleVariable
);