summaryrefslogtreecommitdiff
path: root/src/shared/locale-setup.c
blob: 8943a42c9d3bb39188b49b48930c1174b70a0189 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
/* SPDX-License-Identifier: LGPL-2.1-or-later */

#include <errno.h>
#include <sys/stat.h>

#include "env-file-label.h"
#include "env-file.h"
#include "env-util.h"
#include "errno-util.h"
#include "fd-util.h"
#include "locale-setup.h"
#include "proc-cmdline.h"
#include "stat-util.h"
#include "strv.h"

void locale_context_clear(LocaleContext *c) {
        assert(c);

        c->st = (struct stat) {};

        for (LocaleVariable i = 0; i < _VARIABLE_LC_MAX; i++)
                c->locale[i] = mfree(c->locale[i]);
}

static int locale_context_load_proc(LocaleContext *c, LocaleLoadFlag flag) {
        int r;

        assert(c);

        if (!FLAGS_SET(flag, LOCALE_LOAD_PROC_CMDLINE))
                return 0;

        locale_context_clear(c);

        r = proc_cmdline_get_key_many(PROC_CMDLINE_STRIP_RD_PREFIX,
                                      "locale.LANG",              &c->locale[VARIABLE_LANG],
                                      "locale.LANGUAGE",          &c->locale[VARIABLE_LANGUAGE],
                                      "locale.LC_CTYPE",          &c->locale[VARIABLE_LC_CTYPE],
                                      "locale.LC_NUMERIC",        &c->locale[VARIABLE_LC_NUMERIC],
                                      "locale.LC_TIME",           &c->locale[VARIABLE_LC_TIME],
                                      "locale.LC_COLLATE",        &c->locale[VARIABLE_LC_COLLATE],
                                      "locale.LC_MONETARY",       &c->locale[VARIABLE_LC_MONETARY],
                                      "locale.LC_MESSAGES",       &c->locale[VARIABLE_LC_MESSAGES],
                                      "locale.LC_PAPER",          &c->locale[VARIABLE_LC_PAPER],
                                      "locale.LC_NAME",           &c->locale[VARIABLE_LC_NAME],
                                      "locale.LC_ADDRESS",        &c->locale[VARIABLE_LC_ADDRESS],
                                      "locale.LC_TELEPHONE",      &c->locale[VARIABLE_LC_TELEPHONE],
                                      "locale.LC_MEASUREMENT",    &c->locale[VARIABLE_LC_MEASUREMENT],
                                      "locale.LC_IDENTIFICATION", &c->locale[VARIABLE_LC_IDENTIFICATION]);
        if (r == -ENOENT)
                return 0;
        if (r < 0)
                return log_debug_errno(r, "Failed to read /proc/cmdline: %m");
        return r;
}

static int locale_context_load_conf(LocaleContext *c, LocaleLoadFlag flag) {
        _cleanup_close_ int fd = -EBADF;
        struct stat st;
        int r;

        assert(c);

        if (!FLAGS_SET(flag, LOCALE_LOAD_LOCALE_CONF))
                return 0;

        fd = RET_NERRNO(open("/etc/locale.conf", O_CLOEXEC | O_PATH));
        if (fd == -ENOENT)
                return 0;
        if (fd < 0)
                return log_debug_errno(errno, "Failed to open /etc/locale.conf: %m");

        if (fstat(fd, &st) < 0)
                return log_debug_errno(errno, "Failed to stat /etc/locale.conf: %m");

        /* If the file is not changed, then we do not need to re-read the file. */
        if (stat_inode_unmodified(&c->st, &st))
                return 0;

        c->st = st;
        locale_context_clear(c);

        r = parse_env_file_fd(fd, "/etc/locale.conf",
                              "LANG",              &c->locale[VARIABLE_LANG],
                              "LANGUAGE",          &c->locale[VARIABLE_LANGUAGE],
                              "LC_CTYPE",          &c->locale[VARIABLE_LC_CTYPE],
                              "LC_NUMERIC",        &c->locale[VARIABLE_LC_NUMERIC],
                              "LC_TIME",           &c->locale[VARIABLE_LC_TIME],
                              "LC_COLLATE",        &c->locale[VARIABLE_LC_COLLATE],
                              "LC_MONETARY",       &c->locale[VARIABLE_LC_MONETARY],
                              "LC_MESSAGES",       &c->locale[VARIABLE_LC_MESSAGES],
                              "LC_PAPER",          &c->locale[VARIABLE_LC_PAPER],
                              "LC_NAME",           &c->locale[VARIABLE_LC_NAME],
                              "LC_ADDRESS",        &c->locale[VARIABLE_LC_ADDRESS],
                              "LC_TELEPHONE",      &c->locale[VARIABLE_LC_TELEPHONE],
                              "LC_MEASUREMENT",    &c->locale[VARIABLE_LC_MEASUREMENT],
                              "LC_IDENTIFICATION", &c->locale[VARIABLE_LC_IDENTIFICATION]);
        if (r < 0)
                return log_debug_errno(r, "Failed to read /etc/locale.conf: %m");

        return 1; /* loaded */
}

static int locale_context_load_env(LocaleContext *c, LocaleLoadFlag flag) {
        int r;

        assert(c);

        if (!FLAGS_SET(flag, LOCALE_LOAD_ENVIRONMENT))
                return 0;

        locale_context_clear(c);

        /* Fill in what we got passed from systemd. */
        for (LocaleVariable p = 0; p < _VARIABLE_LC_MAX; p++) {
                const char *name = ASSERT_PTR(locale_variable_to_string(p));

                r = free_and_strdup(&c->locale[p], empty_to_null(getenv(name)));
                if (r < 0)
                        return log_oom_debug();
        }

        return 1; /* loaded */
}

int locale_context_load(LocaleContext *c, LocaleLoadFlag flag) {
        int r;

        assert(c);

        r = locale_context_load_proc(c, flag);
        if (r > 0)
                goto finalize;

        r = locale_context_load_conf(c, flag);
        if (r != 0)
                goto finalize;

        r = locale_context_load_env(c, flag);

finalize:
        if (r <= 0) {
                /* Nothing loaded, or error. */
                locale_context_clear(c);
                return r;
        }

        if (FLAGS_SET(flag, LOCALE_LOAD_SIMPLIFY))
                locale_variables_simplify(c->locale);

        return 0;
}

int locale_context_build_env(const LocaleContext *c, char ***ret_set, char ***ret_unset) {
        _cleanup_strv_free_ char **set = NULL, **unset = NULL;
        int r;

        assert(c);

        if (!ret_set && !ret_unset)
                return 0;

        for (LocaleVariable p = 0; p < _VARIABLE_LC_MAX; p++) {
                const char *name = ASSERT_PTR(locale_variable_to_string(p));

                if (isempty(c->locale[p])) {
                        if (!ret_unset)
                                continue;
                        r = strv_extend(&unset, name);
                } else {
                        if (!ret_set)
                                continue;
                        r = strv_env_assign(&set, name, c->locale[p]);
                }
                if (r < 0)
                        return r;
        }

        if (ret_set)
                *ret_set = TAKE_PTR(set);
        if (ret_unset)
                *ret_unset = TAKE_PTR(unset);
        return 0;
}

int locale_context_save(LocaleContext *c, char ***ret_set, char ***ret_unset) {
        _cleanup_strv_free_ char **set = NULL, **unset = NULL;
        int r;

        assert(c);

        /* Set values will be returned as strv in *ret on success. */

        r = locale_context_build_env(c, &set, ret_unset ? &unset : NULL);
        if (r < 0)
                return r;

        if (strv_isempty(set)) {
                if (unlink("/etc/locale.conf") < 0)
                        return errno == ENOENT ? 0 : -errno;

                c->st = (struct stat) {};

                if (ret_set)
                        *ret_set = NULL;
                if (ret_unset)
                        *ret_unset = NULL;
                return 0;
        }

        r = write_env_file_label("/etc/locale.conf", set);
        if (r < 0)
                return r;

        if (stat("/etc/locale.conf", &c->st) < 0)
                return -errno;

        if (ret_set)
                *ret_set = TAKE_PTR(set);
        if (ret_unset)
                *ret_unset = TAKE_PTR(unset);
        return 0;
}

int locale_context_merge(const LocaleContext *c, char *l[_VARIABLE_LC_MAX]) {
        assert(c);
        assert(l);

        for (LocaleVariable p = 0; p < _VARIABLE_LC_MAX; p++)
                if (!isempty(c->locale[p]) && isempty(l[p])) {
                        l[p] = strdup(c->locale[p]);
                        if (!l[p])
                                return -ENOMEM;
                }

        return 0;
}

void locale_context_take(LocaleContext *c, char *l[_VARIABLE_LC_MAX]) {
        assert(c);
        assert(l);

        for (LocaleVariable p = 0; p < _VARIABLE_LC_MAX; p++)
                free_and_replace(c->locale[p], l[p]);
}

bool locale_context_equal(const LocaleContext *c, char *l[_VARIABLE_LC_MAX]) {
        assert(c);
        assert(l);

        for (LocaleVariable p = 0; p < _VARIABLE_LC_MAX; p++)
                if (!streq_ptr(c->locale[p], l[p]))
                        return false;

        return true;
}

int locale_setup(char ***environment) {
        _cleanup_(locale_context_clear) LocaleContext c = {};
        _cleanup_strv_free_ char **add = NULL;
        int r;

        assert(environment);

        r = locale_context_load(&c, LOCALE_LOAD_PROC_CMDLINE | LOCALE_LOAD_LOCALE_CONF);
        if (r < 0)
                return r;

        r = locale_context_build_env(&c, &add, NULL);
        if (r < 0)
                return r;

        if (strv_isempty(add)) {
                /* If no locale is configured then default to compile-time default. */

                add = strv_new("LANG=" SYSTEMD_DEFAULT_LOCALE);
                if (!add)
                        return -ENOMEM;
        }

        if (strv_isempty(*environment))
                strv_free_and_replace(*environment, add);
        else {
                char **merged;

                merged = strv_env_merge(*environment, add);
                if (!merged)
                        return -ENOMEM;

                strv_free_and_replace(*environment, merged);
        }

        return 0;
}