• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

systemd / systemd / 19315930715

12 Nov 2025 11:39PM UTC coverage: 72.251% (-0.2%) from 72.412%
19315930715

push

github

bluca
mkosi: update debian commit reference to efdd7a637

* efdd7a6377 Install new file for upstream build
* 9ebdc6099e d/rules: enable 10-systemd-logind-root-ignore-inhibitors.rules.example on Ubuntu
* 1255cc7663 initramfs-tools: only skip chzdev rules if zdev_early=0
* 4675b281ee d/t/boot-and-services: skip apparmor test on armhf
* 214d6e37b2 d/t/boot-and-services: run transient unit to check syslog messages
* f4e196aa26 d/t/boot-and-services: tweak test_rsyslog regex
* dbd366a43e Install new files for upstream build
* bb7f8ef532 Install new files for upstream build
* efa7cee8a7 Install new file for upstream build
* 95aa1d1685 Install new file for upstream build
* b770f0f01b kernel-install: skip 55-initrd.install when an initrd generator is configured
* af8d1e3134 Update changelog for 258.1-2 release
* 2d0e73cd14 d/libnss-systemd.postinst: Ensure module is enabled for all four databases

306471 of 424176 relevant lines covered (72.25%)

1239443.53 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

89.03
/src/basic/locale-util.c
1
/* SPDX-License-Identifier: LGPL-2.1-or-later */
2

3
#include <fcntl.h>
4
#include <langinfo.h>
5
#include <stdlib.h>
6
#include <sys/mman.h>
7
#include <sys/stat.h>
8
#include <unistd.h>
9

10
#include "dirent-util.h"
11
#include "env-util.h"
12
#include "fd-util.h"
13
#include "fileio.h"
14
#include "locale-util.h"
15
#include "log.h"
16
#include "path-util.h"
17
#include "process-util.h"
18
#include "set.h"
19
#include "string-table.h"
20
#include "string-util.h"
21
#include "strv.h"
22
#include "utf8.h"
23

24
static char* normalize_locale(const char *name) {
24✔
25
        const char *e;
24✔
26

27
        /* Locale names are weird: glibc has some magic rules when looking for the charset name on disk: it
28
         * lowercases everything, and removes most special chars. This means the official .UTF-8 suffix
29
         * becomes .utf8 when looking things up on disk. When enumerating locales, let's do the reverse
30
         * operation, and go back to ".UTF-8" which appears to be the more commonly accepted name. We only do
31
         * that for UTF-8 however, since it's kinda the only charset that matters. */
32

33
        e = endswith(name, ".utf8");
24✔
34
        if (e) {
24✔
35
                _cleanup_free_ char *prefix = NULL;
19✔
36

37
                prefix = strndup(name, e - name);
19✔
38
                if (!prefix)
19✔
39
                        return NULL;
40

41
                return strjoin(prefix, ".UTF-8");
19✔
42
        }
43

44
        e = strstr(name, ".utf8@");
5✔
45
        if (e) {
5✔
46
                _cleanup_free_ char *prefix = NULL;
×
47

48
                prefix = strndup(name, e - name);
×
49
                if (!prefix)
×
50
                        return NULL;
51

52
                return strjoin(prefix, ".UTF-8@", e + 6);
×
53
        }
54

55
        return strdup(name);
5✔
56
}
57

58
#ifdef __GLIBC__
59
static int add_locales_from_archive(Set *locales) {
10✔
60
        /* Stolen from glibc... */
61

62
        struct locarhead {
10✔
63
                uint32_t magic;
64
                /* Serial number.  */
65
                uint32_t serial;
66
                /* Name hash table.  */
67
                uint32_t namehash_offset;
68
                uint32_t namehash_used;
69
                uint32_t namehash_size;
70
                /* String table.  */
71
                uint32_t string_offset;
72
                uint32_t string_used;
73
                uint32_t string_size;
74
                /* Table with locale records.  */
75
                uint32_t locrectab_offset;
76
                uint32_t locrectab_used;
77
                uint32_t locrectab_size;
78
                /* MD5 sum hash table.  */
79
                uint32_t sumhash_offset;
80
                uint32_t sumhash_used;
81
                uint32_t sumhash_size;
82
        };
83

84
        struct namehashent {
10✔
85
                /* Hash value of the name.  */
86
                uint32_t hashval;
87
                /* Offset of the name in the string table.  */
88
                uint32_t name_offset;
89
                /* Offset of the locale record.  */
90
                uint32_t locrec_offset;
91
        };
92

93
        int r;
10✔
94

95
        assert(locales);
10✔
96

97
        _cleanup_close_ int fd = open("/usr/lib/locale/locale-archive", O_RDONLY|O_NOCTTY|O_CLOEXEC);
20✔
98
        if (fd < 0)
10✔
99
                return errno == ENOENT ? 0 : -errno;
3✔
100

101
        struct stat st;
7✔
102
        if (fstat(fd, &st) < 0)
7✔
103
                return -errno;
×
104

105
        if (!S_ISREG(st.st_mode))
7✔
106
                return -EBADMSG;
107

108
        if (st.st_size < (off_t) sizeof(struct locarhead))
7✔
109
                return -EBADMSG;
110

111
        if (file_offset_beyond_memory_size(st.st_size))
7✔
112
                return -EFBIG;
113

114
        void *p = mmap(NULL, st.st_size, PROT_READ, MAP_SHARED, fd, 0);
7✔
115
        if (p == MAP_FAILED)
7✔
116
                return -errno;
×
117

118
        const struct namehashent *e;
7✔
119
        const struct locarhead *h = p;
7✔
120
        if (h->magic != 0xde020109 ||
7✔
121
            h->namehash_offset + h->namehash_size > st.st_size ||
7✔
122
            h->string_offset + h->string_size > st.st_size ||
7✔
123
            h->locrectab_offset + h->locrectab_size > st.st_size ||
7✔
124
            h->sumhash_offset + h->sumhash_size > st.st_size) {
7✔
125
                r = -EBADMSG;
×
126
                goto finish;
×
127
        }
128

129
        e = (const struct namehashent*) ((const uint8_t*) p + h->namehash_offset);
7✔
130
        for (size_t i = 0; i < h->namehash_size; i++) {
6,356✔
131
                char *z;
6,349✔
132

133
                if (e[i].locrec_offset == 0)
6,349✔
134
                        continue;
6,340✔
135

136
                if (!utf8_is_valid((char*) p + e[i].name_offset))
9✔
137
                        continue;
×
138

139
                z = normalize_locale((char*) p + e[i].name_offset);
9✔
140
                if (!z) {
9✔
141
                        r = -ENOMEM;
×
142
                        goto finish;
×
143
                }
144

145
                r = set_consume(locales, z);
9✔
146
                if (r < 0)
9✔
147
                        goto finish;
×
148
        }
149

150
        r = 0;
151

152
finish:
7✔
153
        if (p != MAP_FAILED)
7✔
154
                munmap((void*) p, st.st_size);
7✔
155

156
        return r;
7✔
157
}
158

159
static int add_locales_from_libdir(Set *locales) {
10✔
160
        _cleanup_closedir_ DIR *dir = NULL;
10✔
161
        int r;
10✔
162

163
        assert(locales);
10✔
164

165
        dir = opendir("/usr/lib/locale");
10✔
166
        if (!dir)
10✔
167
                return errno == ENOENT ? 0 : -errno;
×
168

169
        FOREACH_DIRENT(de, dir, return -errno) {
52✔
170
                char *z;
22✔
171

172
                if (de->d_type != DT_DIR)
22✔
173
                        continue;
7✔
174

175
                z = normalize_locale(de->d_name);
15✔
176
                if (!z)
15✔
177
                        return -ENOMEM;
178

179
                r = set_consume(locales, z);
15✔
180
                if (r < 0)
15✔
181
                        return r;
182
        }
183

184
        return 0;
185
}
186

187
#else
188

189
static int add_locales_for_musl(Set *locales) {
190
        int r;
191

192
        assert(locales);
193

194
        _cleanup_closedir_ DIR *dir = opendir("/usr/share/i18n/locales/musl/");
195
        if (!dir)
196
                return errno == ENOENT ? 0 : -errno;
197

198
        FOREACH_DIRENT(de, dir, return -errno) {
199
                if (de->d_type != DT_REG)
200
                        continue;
201

202
                char *z = normalize_locale(de->d_name);
203
                if (!z)
204
                        return -ENOMEM;
205

206
                r = set_consume(locales, z);
207
                if (r < 0)
208
                        return r;
209
        }
210

211
        return 0;
212
}
213
#endif
214

215
int get_locales(char ***ret) {
10✔
216
        _cleanup_set_free_ Set *locales = NULL;
10✔
217
        int r;
10✔
218

219
        locales = set_new(&string_hash_ops_free);
10✔
220
        if (!locales)
10✔
221
                return -ENOMEM;
222

223
#ifdef __GLIBC__
224
        r = add_locales_from_archive(locales);
10✔
225
        if (r < 0 && r != -ENOENT)
10✔
226
                return r;
227

228
        r = add_locales_from_libdir(locales);
10✔
229
        if (r < 0)
10✔
230
                return r;
231
#else
232
        r = add_locales_for_musl(locales);
233
        if (r < 0)
234
                return r;
235
#endif
236

237
        char *locale;
10✔
238
        SET_FOREACH(locale, locales) {
44✔
239
                r = locale_is_installed(locale);
24✔
240
                if (r < 0)
24✔
241
                        return r;
×
242
                if (r == 0)
24✔
243
                        free(set_remove(locales, locale));
5✔
244
        }
245

246
        _cleanup_strv_free_ char **l = set_to_strv(&locales);
20✔
247
        if (!l)
10✔
248
                return -ENOMEM;
249

250
        r = getenv_bool("SYSTEMD_LIST_NON_UTF8_LOCALES");
10✔
251
        if (r <= 0) {
10✔
252
                if (!IN_SET(r, -ENXIO, 0))
10✔
253
                        log_debug_errno(r, "Failed to parse $SYSTEMD_LIST_NON_UTF8_LOCALES as boolean, ignoring: %m");
×
254

255
                /* Filter out non-UTF-8 locales, because it's 2019, by default */
256
                char **b = l;
10✔
257
                STRV_FOREACH(a, l)
29✔
258
                        if (endswith(*a, "UTF-8") || strstr(*a, ".UTF-8@"))
19✔
259
                                *(b++) = *a;
19✔
260
                        else
261
                                free(*a);
×
262

263
                *b = NULL;
10✔
264
        }
265

266
        strv_sort(l);
10✔
267

268
        *ret = TAKE_PTR(l);
10✔
269

270
        return 0;
10✔
271
}
272

273
bool locale_is_valid(const char *name) {
81✔
274

275
        if (isempty(name))
81✔
276
                return false;
277

278
        if (strlen(name) >= 128)
79✔
279
                return false;
280

281
        if (!utf8_is_valid(name))
79✔
282
                return false;
283

284
        if (!filename_is_valid(name))
79✔
285
                return false;
286

287
        /* Locales look like: ll_CC.ENC@variant, where ll and CC are alphabetic, ENC is alphanumeric with
288
         * dashes, and variant seems to be alphabetic.
289
         * See: https://www.gnu.org/software/gettext/manual/html_node/Locale-Names.html */
290
        if (!in_charset(name, ALPHANUMERICAL "_.-@"))
77✔
291
                return false;
2✔
292

293
        return true;
294
}
295

296
int locale_is_installed(const char *name) {
47✔
297
        if (!locale_is_valid(name))
47✔
298
                return false;
47✔
299

300
        if (STR_IN_SET(name, "C", "POSIX")) /* These ones are always OK */
44✔
301
                return true;
6✔
302

303
#ifdef __GLIBC__
304
        _cleanup_(freelocalep) locale_t loc = newlocale(LC_ALL_MASK, name, (locale_t) 0);
38✔
305
        if (loc == (locale_t) 0)
38✔
306
                return errno == ENOMEM ? -ENOMEM : false;
12✔
307

308
        return true;
309
#else
310
        /* musl also has C.UTF-8 as builtin */
311
        if (streq(name, "C.UTF-8"))
312
                return true;
313

314
        /* musl's newlocale() always succeeds and provides a fake locale object even when the locale does
315
         * not exist. Hence, we need to explicitly check if the locale file exists. */
316
        _cleanup_free_ char *p = path_join("/usr/share/i18n/locales/musl/", name);
317
        if (!p)
318
                return -ENOMEM;
319

320
        return access(p, F_OK) >= 0;
321
#endif
322
}
323

324
static bool is_locale_utf8_impl(void) {
10,531✔
325
        const char *set;
10,531✔
326
        int r;
10,531✔
327

328
        /* Note that we default to 'true' here, since today UTF8 is pretty much supported everywhere. */
329

330
        r = secure_getenv_bool("SYSTEMD_UTF8");
10,531✔
331
        if (r >= 0)
10,531✔
332
                return r;
120✔
333
        if (r != -ENXIO)
10,411✔
334
                log_debug_errno(r, "Failed to parse $SYSTEMD_UTF8, ignoring: %m");
×
335

336
        /* This function may be called from libsystemd, and setlocale() is not thread safe. Assuming yes. */
337
        if (!is_main_thread())
10,411✔
338
                return true;
339

340
        if (!setlocale(LC_ALL, ""))
10,389✔
341
                return true;
342

343
        set = nl_langinfo(CODESET);
10,389✔
344
        if (!set || streq(set, "UTF-8"))
10,389✔
345
                return true;
346

347
        set = setlocale(LC_CTYPE, NULL);
2,871✔
348
        if (!set)
2,871✔
349
                return true;
350

351
        /* Unless LC_CTYPE is explicitly overridden, return true. Because here CTYPE is effectively unset
352
         * and everything can do to UTF-8 nowadays. */
353
        return STR_IN_SET(set, "C", "POSIX") &&
5,742✔
354
                !getenv("LC_ALL") &&
5,742✔
355
                !getenv("LC_CTYPE") &&
8,613✔
356
                !getenv("LANG");
2,871✔
357
}
358

359
bool is_locale_utf8(void) {
960,907✔
360
        static int cached = -1;
960,907✔
361

362
        if (cached < 0)
960,907✔
363
                cached = is_locale_utf8_impl();
10,531✔
364

365
        return cached;
960,907✔
366
}
367

368
void locale_variables_free(char *l[_VARIABLE_LC_MAX]) {
9✔
369
        free_many_charp(l, _VARIABLE_LC_MAX);
9✔
370
}
9✔
371

372
void locale_variables_simplify(char *l[_VARIABLE_LC_MAX]) {
286✔
373
        assert(l);
286✔
374

375
        for (LocaleVariable p = 0; p < _VARIABLE_LC_MAX; p++) {
4,290✔
376
                if (p == VARIABLE_LANG)
4,004✔
377
                        continue;
286✔
378
                if (isempty(l[p]) || streq_ptr(l[VARIABLE_LANG], l[p]))
3,736✔
379
                        l[p] = mfree(l[p]);
3,702✔
380
        }
381
}
286✔
382

383
static const char * const locale_variable_table[_VARIABLE_LC_MAX] = {
384
        [VARIABLE_LANG]              = "LANG",
385
        [VARIABLE_LANGUAGE]          = "LANGUAGE",
386
        [VARIABLE_LC_CTYPE]          = "LC_CTYPE",
387
        [VARIABLE_LC_NUMERIC]        = "LC_NUMERIC",
388
        [VARIABLE_LC_TIME]           = "LC_TIME",
389
        [VARIABLE_LC_COLLATE]        = "LC_COLLATE",
390
        [VARIABLE_LC_MONETARY]       = "LC_MONETARY",
391
        [VARIABLE_LC_MESSAGES]       = "LC_MESSAGES",
392
        [VARIABLE_LC_PAPER]          = "LC_PAPER",
393
        [VARIABLE_LC_NAME]           = "LC_NAME",
394
        [VARIABLE_LC_ADDRESS]        = "LC_ADDRESS",
395
        [VARIABLE_LC_TELEPHONE]      = "LC_TELEPHONE",
396
        [VARIABLE_LC_MEASUREMENT]    = "LC_MEASUREMENT",
397
        [VARIABLE_LC_IDENTIFICATION] = "LC_IDENTIFICATION"
398
};
399

400
DEFINE_STRING_TABLE_LOOKUP(locale_variable, LocaleVariable);
15,980✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc