2020-11-09 05:23:58 +01:00
|
|
|
/* SPDX-License-Identifier: LGPL-2.1-or-later */
|
2010-09-16 01:57:42 +02:00
|
|
|
|
|
|
|
#include <errno.h>
|
2015-10-24 22:58:24 +02:00
|
|
|
#include <stdlib.h>
|
2010-09-16 01:57:42 +02:00
|
|
|
|
2018-11-30 22:08:41 +01:00
|
|
|
#include "env-file.h"
|
2013-10-01 00:08:30 +02:00
|
|
|
#include "env-util.h"
|
2015-11-16 22:09:36 +01:00
|
|
|
#include "locale-setup.h"
|
2014-09-24 13:17:43 +02:00
|
|
|
#include "locale-util.h"
|
2018-11-12 12:53:28 +01:00
|
|
|
#include "proc-cmdline.h"
|
2015-10-24 22:58:24 +02:00
|
|
|
#include "string-util.h"
|
|
|
|
#include "strv.h"
|
|
|
|
#include "util.h"
|
|
|
|
#include "virt.h"
|
2010-09-16 01:57:42 +02:00
|
|
|
|
2013-07-26 05:22:22 +02:00
|
|
|
int locale_setup(char ***environment) {
|
2018-11-12 16:50:58 +01:00
|
|
|
_cleanup_(locale_variables_freep) char *variables[_VARIABLE_LC_MAX] = {};
|
2018-11-12 12:50:06 +01:00
|
|
|
_cleanup_strv_free_ char **add = NULL;
|
2018-11-12 12:51:23 +01:00
|
|
|
LocaleVariable i;
|
2018-11-12 12:53:28 +01:00
|
|
|
int r;
|
2010-09-16 01:57:42 +02:00
|
|
|
|
2018-11-12 12:53:28 +01:00
|
|
|
r = proc_cmdline_get_key_many(PROC_CMDLINE_STRIP_RD_PREFIX,
|
|
|
|
"locale.LANG", &variables[VARIABLE_LANG],
|
|
|
|
"locale.LANGUAGE", &variables[VARIABLE_LANGUAGE],
|
|
|
|
"locale.LC_CTYPE", &variables[VARIABLE_LC_CTYPE],
|
|
|
|
"locale.LC_NUMERIC", &variables[VARIABLE_LC_NUMERIC],
|
|
|
|
"locale.LC_TIME", &variables[VARIABLE_LC_TIME],
|
|
|
|
"locale.LC_COLLATE", &variables[VARIABLE_LC_COLLATE],
|
|
|
|
"locale.LC_MONETARY", &variables[VARIABLE_LC_MONETARY],
|
|
|
|
"locale.LC_MESSAGES", &variables[VARIABLE_LC_MESSAGES],
|
|
|
|
"locale.LC_PAPER", &variables[VARIABLE_LC_PAPER],
|
|
|
|
"locale.LC_NAME", &variables[VARIABLE_LC_NAME],
|
|
|
|
"locale.LC_ADDRESS", &variables[VARIABLE_LC_ADDRESS],
|
|
|
|
"locale.LC_TELEPHONE", &variables[VARIABLE_LC_TELEPHONE],
|
|
|
|
"locale.LC_MEASUREMENT", &variables[VARIABLE_LC_MEASUREMENT],
|
|
|
|
"locale.LC_IDENTIFICATION", &variables[VARIABLE_LC_IDENTIFICATION]);
|
|
|
|
if (r < 0 && r != -ENOENT)
|
|
|
|
log_warning_errno(r, "Failed to read /proc/cmdline: %m");
|
2012-11-22 01:00:26 +01:00
|
|
|
|
2018-11-12 12:53:28 +01:00
|
|
|
/* Hmm, nothing set on the kernel cmd line? Then let's try /etc/locale.conf */
|
2012-11-22 01:00:26 +01:00
|
|
|
if (r <= 0) {
|
2018-11-12 14:04:47 +01:00
|
|
|
r = parse_env_file(NULL, "/etc/locale.conf",
|
2012-11-22 01:00:26 +01:00
|
|
|
"LANG", &variables[VARIABLE_LANG],
|
|
|
|
"LANGUAGE", &variables[VARIABLE_LANGUAGE],
|
|
|
|
"LC_CTYPE", &variables[VARIABLE_LC_CTYPE],
|
|
|
|
"LC_NUMERIC", &variables[VARIABLE_LC_NUMERIC],
|
|
|
|
"LC_TIME", &variables[VARIABLE_LC_TIME],
|
|
|
|
"LC_COLLATE", &variables[VARIABLE_LC_COLLATE],
|
|
|
|
"LC_MONETARY", &variables[VARIABLE_LC_MONETARY],
|
|
|
|
"LC_MESSAGES", &variables[VARIABLE_LC_MESSAGES],
|
|
|
|
"LC_PAPER", &variables[VARIABLE_LC_PAPER],
|
|
|
|
"LC_NAME", &variables[VARIABLE_LC_NAME],
|
|
|
|
"LC_ADDRESS", &variables[VARIABLE_LC_ADDRESS],
|
|
|
|
"LC_TELEPHONE", &variables[VARIABLE_LC_TELEPHONE],
|
|
|
|
"LC_MEASUREMENT", &variables[VARIABLE_LC_MEASUREMENT],
|
2018-11-12 14:18:03 +01:00
|
|
|
"LC_IDENTIFICATION", &variables[VARIABLE_LC_IDENTIFICATION]);
|
2012-11-22 01:00:26 +01:00
|
|
|
if (r < 0 && r != -ENOENT)
|
2014-11-28 13:19:16 +01:00
|
|
|
log_warning_errno(r, "Failed to read /etc/locale.conf: %m");
|
2010-09-21 01:51:23 +02:00
|
|
|
}
|
|
|
|
|
2014-09-24 13:17:43 +02:00
|
|
|
for (i = 0; i < _VARIABLE_LC_MAX; i++) {
|
2013-10-01 00:08:30 +02:00
|
|
|
char *s;
|
|
|
|
|
2013-07-26 05:22:22 +02:00
|
|
|
if (!variables[i])
|
|
|
|
continue;
|
|
|
|
|
2016-10-23 17:43:27 +02:00
|
|
|
s = strjoin(locale_variable_to_string(i), "=", variables[i]);
|
2018-11-12 16:50:58 +01:00
|
|
|
if (!s)
|
|
|
|
return -ENOMEM;
|
2013-10-01 00:08:30 +02:00
|
|
|
|
2018-11-12 16:50:58 +01:00
|
|
|
if (strv_consume(&add, s) < 0)
|
|
|
|
return -ENOMEM;
|
2013-10-01 00:08:30 +02:00
|
|
|
}
|
|
|
|
|
2018-11-12 13:12:39 +01:00
|
|
|
if (strv_isempty(add)) {
|
2018-12-28 13:38:36 +01:00
|
|
|
/* If no locale is configured then default to compile-time default. */
|
2013-10-01 00:08:30 +02:00
|
|
|
|
2018-12-28 13:38:36 +01:00
|
|
|
add = strv_new("LANG=" SYSTEMD_DEFAULT_LOCALE);
|
2018-11-12 16:50:58 +01:00
|
|
|
if (!add)
|
|
|
|
return -ENOMEM;
|
2018-11-12 13:12:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (strv_isempty(*environment))
|
|
|
|
strv_free_and_replace(*environment, add);
|
|
|
|
else {
|
|
|
|
char **merged;
|
|
|
|
|
|
|
|
merged = strv_env_merge(2, *environment, add);
|
2018-11-12 16:50:58 +01:00
|
|
|
if (!merged)
|
|
|
|
return -ENOMEM;
|
2013-07-26 05:22:22 +02:00
|
|
|
|
2018-11-12 13:12:39 +01:00
|
|
|
strv_free_and_replace(*environment, merged);
|
2010-09-16 01:57:42 +02:00
|
|
|
}
|
2010-09-21 01:40:34 +02:00
|
|
|
|
2018-11-12 16:50:58 +01:00
|
|
|
return 0;
|
2010-09-16 01:57:42 +02:00
|
|
|
}
|