2017-11-18 17:09:20 +01:00
|
|
|
|
/* SPDX-License-Identifier: LGPL-2.1+ */
|
2014-07-07 11:49:48 +02:00
|
|
|
|
|
2015-11-30 21:43:37 +01:00
|
|
|
|
#include <dirent.h>
|
|
|
|
|
#include <errno.h>
|
|
|
|
|
#include <fcntl.h>
|
2017-11-10 10:31:44 +01:00
|
|
|
|
#include <ftw.h>
|
2015-10-26 23:01:30 +01:00
|
|
|
|
#include <langinfo.h>
|
2015-11-30 21:43:37 +01:00
|
|
|
|
#include <libintl.h>
|
2015-10-26 23:01:30 +01:00
|
|
|
|
#include <locale.h>
|
2015-11-30 21:43:37 +01:00
|
|
|
|
#include <stddef.h>
|
|
|
|
|
#include <stdint.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <string.h>
|
2014-07-07 11:49:48 +02:00
|
|
|
|
#include <sys/mman.h>
|
2015-11-30 21:43:37 +01:00
|
|
|
|
#include <sys/stat.h>
|
2014-07-07 11:49:48 +02:00
|
|
|
|
|
2017-11-10 10:31:44 +01:00
|
|
|
|
#include "def.h"
|
2015-10-26 20:07:55 +01:00
|
|
|
|
#include "dirent-util.h"
|
2018-11-06 17:59:58 +01:00
|
|
|
|
#include "env-util.h"
|
2015-10-25 13:14:12 +01:00
|
|
|
|
#include "fd-util.h"
|
2015-12-01 23:22:03 +01:00
|
|
|
|
#include "hashmap.h"
|
2015-10-25 13:14:12 +01:00
|
|
|
|
#include "locale-util.h"
|
2015-10-26 18:59:36 +01:00
|
|
|
|
#include "path-util.h"
|
2014-07-07 11:49:48 +02:00
|
|
|
|
#include "set.h"
|
2015-10-26 22:31:05 +01:00
|
|
|
|
#include "string-table.h"
|
2015-10-24 22:58:24 +02:00
|
|
|
|
#include "string-util.h"
|
2014-07-07 11:49:48 +02:00
|
|
|
|
#include "strv.h"
|
2015-10-24 22:58:24 +02:00
|
|
|
|
#include "utf8.h"
|
2014-07-07 11:49:48 +02:00
|
|
|
|
|
|
|
|
|
static int add_locales_from_archive(Set *locales) {
|
|
|
|
|
/* Stolen from glibc... */
|
|
|
|
|
|
|
|
|
|
struct locarhead {
|
|
|
|
|
uint32_t magic;
|
|
|
|
|
/* Serial number. */
|
|
|
|
|
uint32_t serial;
|
|
|
|
|
/* Name hash table. */
|
|
|
|
|
uint32_t namehash_offset;
|
|
|
|
|
uint32_t namehash_used;
|
|
|
|
|
uint32_t namehash_size;
|
|
|
|
|
/* String table. */
|
|
|
|
|
uint32_t string_offset;
|
|
|
|
|
uint32_t string_used;
|
|
|
|
|
uint32_t string_size;
|
|
|
|
|
/* Table with locale records. */
|
|
|
|
|
uint32_t locrectab_offset;
|
|
|
|
|
uint32_t locrectab_used;
|
|
|
|
|
uint32_t locrectab_size;
|
|
|
|
|
/* MD5 sum hash table. */
|
|
|
|
|
uint32_t sumhash_offset;
|
|
|
|
|
uint32_t sumhash_used;
|
|
|
|
|
uint32_t sumhash_size;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct namehashent {
|
|
|
|
|
/* Hash value of the name. */
|
|
|
|
|
uint32_t hashval;
|
|
|
|
|
/* Offset of the name in the string table. */
|
|
|
|
|
uint32_t name_offset;
|
|
|
|
|
/* Offset of the locale record. */
|
|
|
|
|
uint32_t locrec_offset;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const struct locarhead *h;
|
|
|
|
|
const struct namehashent *e;
|
|
|
|
|
const void *p = MAP_FAILED;
|
|
|
|
|
_cleanup_close_ int fd = -1;
|
|
|
|
|
size_t sz = 0;
|
|
|
|
|
struct stat st;
|
tree-wide: be more careful with the type of array sizes
Previously we were a bit sloppy with the index and size types of arrays,
we'd regularly use unsigned. While I don't think this ever resulted in
real issues I think we should be more careful there and follow a
stricter regime: unless there's a strong reason not to use size_t for
array sizes and indexes, size_t it should be. Any allocations we do
ultimately will use size_t anyway, and converting forth and back between
unsigned and size_t will always be a source of problems.
Note that on 32bit machines "unsigned" and "size_t" are equivalent, and
on 64bit machines our arrays shouldn't grow that large anyway, and if
they do we have a problem, however that kind of overly large allocation
we have protections for usually, but for overflows we do not have that
so much, hence let's add it.
So yeah, it's a story of the current code being already "good enough",
but I think some extra type hygiene is better.
This patch tries to be comprehensive, but it probably isn't and I missed
a few cases. But I guess we can cover that later as we notice it. Among
smaller fixes, this changes:
1. strv_length()' return type becomes size_t
2. the unit file changes array size becomes size_t
3. DNS answer and query array sizes become size_t
Fixes: https://bugs.freedesktop.org/show_bug.cgi?id=76745
2018-04-27 14:09:31 +02:00
|
|
|
|
size_t i;
|
2014-07-07 11:49:48 +02:00
|
|
|
|
int r;
|
|
|
|
|
|
|
|
|
|
fd = open("/usr/lib/locale/locale-archive", O_RDONLY|O_NOCTTY|O_CLOEXEC);
|
|
|
|
|
if (fd < 0)
|
|
|
|
|
return errno == ENOENT ? 0 : -errno;
|
|
|
|
|
|
|
|
|
|
if (fstat(fd, &st) < 0)
|
|
|
|
|
return -errno;
|
|
|
|
|
|
|
|
|
|
if (!S_ISREG(st.st_mode))
|
|
|
|
|
return -EBADMSG;
|
|
|
|
|
|
|
|
|
|
if (st.st_size < (off_t) sizeof(struct locarhead))
|
|
|
|
|
return -EBADMSG;
|
|
|
|
|
|
|
|
|
|
p = mmap(NULL, st.st_size, PROT_READ, MAP_SHARED, fd, 0);
|
|
|
|
|
if (p == MAP_FAILED)
|
|
|
|
|
return -errno;
|
|
|
|
|
|
|
|
|
|
h = (const struct locarhead *) p;
|
|
|
|
|
if (h->magic != 0xde020109 ||
|
|
|
|
|
h->namehash_offset + h->namehash_size > st.st_size ||
|
|
|
|
|
h->string_offset + h->string_size > st.st_size ||
|
|
|
|
|
h->locrectab_offset + h->locrectab_size > st.st_size ||
|
|
|
|
|
h->sumhash_offset + h->sumhash_size > st.st_size) {
|
|
|
|
|
r = -EBADMSG;
|
|
|
|
|
goto finish;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
e = (const struct namehashent*) ((const uint8_t*) p + h->namehash_offset);
|
|
|
|
|
for (i = 0; i < h->namehash_size; i++) {
|
|
|
|
|
char *z;
|
|
|
|
|
|
|
|
|
|
if (e[i].locrec_offset == 0)
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
if (!utf8_is_valid((char*) p + e[i].name_offset))
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
z = strdup((char*) p + e[i].name_offset);
|
|
|
|
|
if (!z) {
|
|
|
|
|
r = -ENOMEM;
|
|
|
|
|
goto finish;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
r = set_consume(locales, z);
|
|
|
|
|
if (r < 0)
|
|
|
|
|
goto finish;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
r = 0;
|
|
|
|
|
|
|
|
|
|
finish:
|
|
|
|
|
if (p != MAP_FAILED)
|
|
|
|
|
munmap((void*) p, sz);
|
|
|
|
|
|
|
|
|
|
return r;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int add_locales_from_libdir (Set *locales) {
|
|
|
|
|
_cleanup_closedir_ DIR *dir = NULL;
|
|
|
|
|
struct dirent *entry;
|
|
|
|
|
int r;
|
|
|
|
|
|
|
|
|
|
dir = opendir("/usr/lib/locale");
|
|
|
|
|
if (!dir)
|
|
|
|
|
return errno == ENOENT ? 0 : -errno;
|
|
|
|
|
|
|
|
|
|
FOREACH_DIRENT(entry, dir, return -errno) {
|
|
|
|
|
char *z;
|
|
|
|
|
|
2016-04-04 13:07:56 +02:00
|
|
|
|
dirent_ensure_type(dir, entry);
|
|
|
|
|
|
2014-07-07 11:49:48 +02:00
|
|
|
|
if (entry->d_type != DT_DIR)
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
z = strdup(entry->d_name);
|
|
|
|
|
if (!z)
|
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
|
|
r = set_consume(locales, z);
|
|
|
|
|
if (r < 0 && r != -EEXIST)
|
|
|
|
|
return r;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int get_locales(char ***ret) {
|
|
|
|
|
_cleanup_set_free_ Set *locales = NULL;
|
|
|
|
|
_cleanup_strv_free_ char **l = NULL;
|
|
|
|
|
int r;
|
|
|
|
|
|
2014-08-13 01:00:18 +02:00
|
|
|
|
locales = set_new(&string_hash_ops);
|
2014-07-07 11:49:48 +02:00
|
|
|
|
if (!locales)
|
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
|
|
r = add_locales_from_archive(locales);
|
|
|
|
|
if (r < 0 && r != -ENOENT)
|
|
|
|
|
return r;
|
|
|
|
|
|
|
|
|
|
r = add_locales_from_libdir(locales);
|
|
|
|
|
if (r < 0)
|
|
|
|
|
return r;
|
|
|
|
|
|
|
|
|
|
l = set_get_strv(locales);
|
|
|
|
|
if (!l)
|
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
|
|
strv_sort(l);
|
|
|
|
|
|
2018-04-05 07:26:26 +02:00
|
|
|
|
*ret = TAKE_PTR(l);
|
2014-07-07 11:49:48 +02:00
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool locale_is_valid(const char *name) {
|
|
|
|
|
|
|
|
|
|
if (isempty(name))
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
if (strlen(name) >= 128)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
if (!utf8_is_valid(name))
|
|
|
|
|
return false;
|
|
|
|
|
|
2014-12-12 02:32:33 +01:00
|
|
|
|
if (!filename_is_valid(name))
|
2014-07-07 11:49:48 +02:00
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
if (!string_is_safe(name))
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2014-09-24 13:17:43 +02:00
|
|
|
|
|
2015-10-26 23:01:30 +01:00
|
|
|
|
void init_gettext(void) {
|
|
|
|
|
setlocale(LC_ALL, "");
|
|
|
|
|
textdomain(GETTEXT_PACKAGE);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool is_locale_utf8(void) {
|
|
|
|
|
const char *set;
|
|
|
|
|
static int cached_answer = -1;
|
|
|
|
|
|
|
|
|
|
/* Note that we default to 'true' here, since today UTF8 is
|
|
|
|
|
* pretty much supported everywhere. */
|
|
|
|
|
|
|
|
|
|
if (cached_answer >= 0)
|
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
|
|
if (!setlocale(LC_ALL, "")) {
|
|
|
|
|
cached_answer = true;
|
|
|
|
|
goto out;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
set = nl_langinfo(CODESET);
|
|
|
|
|
if (!set) {
|
|
|
|
|
cached_answer = true;
|
|
|
|
|
goto out;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (streq(set, "UTF-8")) {
|
|
|
|
|
cached_answer = true;
|
|
|
|
|
goto out;
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-27 02:22:40 +02:00
|
|
|
|
/* For LC_CTYPE=="C" return true, because CTYPE is effectively
|
2015-10-26 23:01:30 +01:00
|
|
|
|
* unset and everything can do to UTF-8 nowadays. */
|
|
|
|
|
set = setlocale(LC_CTYPE, NULL);
|
|
|
|
|
if (!set) {
|
|
|
|
|
cached_answer = true;
|
|
|
|
|
goto out;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Check result, but ignore the result if C was set
|
|
|
|
|
* explicitly. */
|
|
|
|
|
cached_answer =
|
|
|
|
|
STR_IN_SET(set, "C", "POSIX") &&
|
|
|
|
|
!getenv("LC_ALL") &&
|
|
|
|
|
!getenv("LC_CTYPE") &&
|
|
|
|
|
!getenv("LANG");
|
|
|
|
|
|
|
|
|
|
out:
|
|
|
|
|
return (bool) cached_answer;
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-06 17:59:58 +01:00
|
|
|
|
static bool emoji_enabled(void) {
|
|
|
|
|
static int cached_emoji_enabled = -1;
|
|
|
|
|
|
|
|
|
|
if (cached_emoji_enabled < 0) {
|
|
|
|
|
int val;
|
|
|
|
|
|
|
|
|
|
val = getenv_bool("SYSTEMD_EMOJI");
|
|
|
|
|
if (val < 0)
|
|
|
|
|
cached_emoji_enabled =
|
|
|
|
|
is_locale_utf8() &&
|
|
|
|
|
!STRPTR_IN_SET(getenv("TERM"), "dumb", "linux");
|
|
|
|
|
else
|
|
|
|
|
cached_emoji_enabled = val;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return cached_emoji_enabled;
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-07 23:30:18 +02:00
|
|
|
|
const char *special_glyph(SpecialGlyph code) {
|
|
|
|
|
|
2018-05-03 16:15:04 +02:00
|
|
|
|
/* A list of a number of interesting unicode glyphs we can use to decorate our output. It's probably wise to be
|
|
|
|
|
* conservative here, and primarily stick to the glyphs defined in the eurlatgr font, so that display still
|
|
|
|
|
* works reasonably well on the Linux console. For details see:
|
|
|
|
|
*
|
|
|
|
|
* http://git.altlinux.org/people/legion/packages/kbd.git?p=kbd.git;a=blob;f=data/consolefonts/README.eurlatgr
|
|
|
|
|
*/
|
|
|
|
|
|
2016-05-09 17:24:08 +02:00
|
|
|
|
static const char* const draw_table[2][_SPECIAL_GLYPH_MAX] = {
|
2016-05-07 23:30:18 +02:00
|
|
|
|
/* ASCII fallback */
|
|
|
|
|
[false] = {
|
2018-12-13 22:11:32 +01:00
|
|
|
|
[SPECIAL_GLYPH_TREE_VERTICAL] = "| ",
|
|
|
|
|
[SPECIAL_GLYPH_TREE_BRANCH] = "|-",
|
|
|
|
|
[SPECIAL_GLYPH_TREE_RIGHT] = "`-",
|
|
|
|
|
[SPECIAL_GLYPH_TREE_SPACE] = " ",
|
|
|
|
|
[SPECIAL_GLYPH_TRIANGULAR_BULLET] = ">",
|
|
|
|
|
[SPECIAL_GLYPH_BLACK_CIRCLE] = "*",
|
|
|
|
|
[SPECIAL_GLYPH_BULLET] = "*",
|
|
|
|
|
[SPECIAL_GLYPH_ARROW] = "->",
|
|
|
|
|
[SPECIAL_GLYPH_MDASH] = "-",
|
|
|
|
|
[SPECIAL_GLYPH_ELLIPSIS] = "...",
|
|
|
|
|
[SPECIAL_GLYPH_MU] = "u",
|
|
|
|
|
[SPECIAL_GLYPH_CHECK_MARK] = "+",
|
|
|
|
|
[SPECIAL_GLYPH_CROSS_MARK] = "-",
|
|
|
|
|
[SPECIAL_GLYPH_ECSTATIC_SMILEY] = ":-]",
|
|
|
|
|
[SPECIAL_GLYPH_HAPPY_SMILEY] = ":-}",
|
|
|
|
|
[SPECIAL_GLYPH_SLIGHTLY_HAPPY_SMILEY] = ":-)",
|
|
|
|
|
[SPECIAL_GLYPH_NEUTRAL_SMILEY] = ":-|",
|
|
|
|
|
[SPECIAL_GLYPH_SLIGHTLY_UNHAPPY_SMILEY] = ":-(",
|
|
|
|
|
[SPECIAL_GLYPH_UNHAPPY_SMILEY] = ":-{️",
|
|
|
|
|
[SPECIAL_GLYPH_DEPRESSED_SMILEY] = ":-[",
|
2015-10-26 23:01:30 +01:00
|
|
|
|
},
|
|
|
|
|
|
2016-05-07 23:30:18 +02:00
|
|
|
|
/* UTF-8 */
|
2018-04-11 19:49:50 +02:00
|
|
|
|
[true] = {
|
2018-12-13 22:11:32 +01:00
|
|
|
|
[SPECIAL_GLYPH_TREE_VERTICAL] = "\342\224\202 ", /* │ */
|
|
|
|
|
[SPECIAL_GLYPH_TREE_BRANCH] = "\342\224\234\342\224\200", /* ├─ */
|
|
|
|
|
[SPECIAL_GLYPH_TREE_RIGHT] = "\342\224\224\342\224\200", /* └─ */
|
|
|
|
|
[SPECIAL_GLYPH_TREE_SPACE] = " ", /* */
|
|
|
|
|
[SPECIAL_GLYPH_TRIANGULAR_BULLET] = "\342\200\243", /* ‣ */
|
|
|
|
|
[SPECIAL_GLYPH_BLACK_CIRCLE] = "\342\227\217", /* ● */
|
|
|
|
|
[SPECIAL_GLYPH_BULLET] = "\342\200\242", /* • */
|
|
|
|
|
[SPECIAL_GLYPH_ARROW] = "\342\206\222", /* → */
|
|
|
|
|
[SPECIAL_GLYPH_MDASH] = "\342\200\223", /* – */
|
|
|
|
|
[SPECIAL_GLYPH_ELLIPSIS] = "\342\200\246", /* … */
|
|
|
|
|
[SPECIAL_GLYPH_MU] = "\316\274", /* μ */
|
|
|
|
|
[SPECIAL_GLYPH_CHECK_MARK] = "\342\234\223", /* ✓ */
|
|
|
|
|
[SPECIAL_GLYPH_CROSS_MARK] = "\342\234\227", /* ✗ */
|
|
|
|
|
[SPECIAL_GLYPH_ECSTATIC_SMILEY] = "\360\237\230\207", /* 😇 */
|
|
|
|
|
[SPECIAL_GLYPH_HAPPY_SMILEY] = "\360\237\230\200", /* 😀 */
|
|
|
|
|
[SPECIAL_GLYPH_SLIGHTLY_HAPPY_SMILEY] = "\360\237\231\202", /* 🙂 */
|
|
|
|
|
[SPECIAL_GLYPH_NEUTRAL_SMILEY] = "\360\237\230\220", /* 😐 */
|
|
|
|
|
[SPECIAL_GLYPH_SLIGHTLY_UNHAPPY_SMILEY] = "\360\237\231\201", /* 🙁 */
|
|
|
|
|
[SPECIAL_GLYPH_UNHAPPY_SMILEY] = "\360\237\230\250", /* 😨️️ */
|
|
|
|
|
[SPECIAL_GLYPH_DEPRESSED_SMILEY] = "\360\237\244\242", /* 🤢 */
|
2016-05-07 23:30:18 +02:00
|
|
|
|
},
|
2015-10-26 23:01:30 +01:00
|
|
|
|
};
|
|
|
|
|
|
2018-11-06 17:59:58 +01:00
|
|
|
|
assert(code < _SPECIAL_GLYPH_MAX);
|
|
|
|
|
|
|
|
|
|
return draw_table[code >= _SPECIAL_GLYPH_FIRST_SMILEY ? emoji_enabled() : is_locale_utf8()][code];
|
2015-10-26 23:01:30 +01:00
|
|
|
|
}
|
|
|
|
|
|
2018-12-04 09:29:54 +01:00
|
|
|
|
void locale_variables_free(char *l[_VARIABLE_LC_MAX]) {
|
2018-11-12 16:50:58 +01:00
|
|
|
|
LocaleVariable i;
|
|
|
|
|
|
|
|
|
|
if (!l)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
for (i = 0; i < _VARIABLE_LC_MAX; i++)
|
|
|
|
|
l[i] = mfree(l[i]);
|
|
|
|
|
}
|
|
|
|
|
|
2014-09-24 13:17:43 +02:00
|
|
|
|
static const char * const locale_variable_table[_VARIABLE_LC_MAX] = {
|
|
|
|
|
[VARIABLE_LANG] = "LANG",
|
|
|
|
|
[VARIABLE_LANGUAGE] = "LANGUAGE",
|
|
|
|
|
[VARIABLE_LC_CTYPE] = "LC_CTYPE",
|
|
|
|
|
[VARIABLE_LC_NUMERIC] = "LC_NUMERIC",
|
|
|
|
|
[VARIABLE_LC_TIME] = "LC_TIME",
|
|
|
|
|
[VARIABLE_LC_COLLATE] = "LC_COLLATE",
|
|
|
|
|
[VARIABLE_LC_MONETARY] = "LC_MONETARY",
|
|
|
|
|
[VARIABLE_LC_MESSAGES] = "LC_MESSAGES",
|
|
|
|
|
[VARIABLE_LC_PAPER] = "LC_PAPER",
|
|
|
|
|
[VARIABLE_LC_NAME] = "LC_NAME",
|
|
|
|
|
[VARIABLE_LC_ADDRESS] = "LC_ADDRESS",
|
|
|
|
|
[VARIABLE_LC_TELEPHONE] = "LC_TELEPHONE",
|
|
|
|
|
[VARIABLE_LC_MEASUREMENT] = "LC_MEASUREMENT",
|
|
|
|
|
[VARIABLE_LC_IDENTIFICATION] = "LC_IDENTIFICATION"
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
DEFINE_STRING_TABLE_LOOKUP(locale_variable, LocaleVariable);
|