]>
Commit | Line | Data |
---|---|---|
db9ecf05 | 1 | /* SPDX-License-Identifier: LGPL-2.1-or-later */ |
4897d1dc ZJS |
2 | |
3 | #include <errno.h> | |
ca78ad1d ZJS |
4 | #include <sys/stat.h> |
5 | #include <sys/types.h> | |
4897d1dc ZJS |
6 | #include <unistd.h> |
7 | ||
269e4d2d | 8 | #include "bus-polkit.h" |
8f20232f | 9 | #include "copy.h" |
686d13b9 | 10 | #include "env-file-label.h" |
f05e1d0d | 11 | #include "env-file.h" |
4897d1dc ZJS |
12 | #include "env-util.h" |
13 | #include "fd-util.h" | |
14 | #include "fileio-label.h" | |
15 | #include "fileio.h" | |
8f20232f | 16 | #include "fs-util.h" |
f05e1d0d | 17 | #include "kbd-util.h" |
4897d1dc ZJS |
18 | #include "keymap-util.h" |
19 | #include "locale-util.h" | |
20 | #include "macro.h" | |
35cd0ba5 | 21 | #include "mkdir-label.h" |
d8b4d14d | 22 | #include "nulstr-util.h" |
8f20232f | 23 | #include "process-util.h" |
4897d1dc ZJS |
24 | #include "string-util.h" |
25 | #include "strv.h" | |
e4de7287 | 26 | #include "tmpfile-util.h" |
4897d1dc ZJS |
27 | |
28 | static bool startswith_comma(const char *s, const char *prefix) { | |
5ad327dd ZJS |
29 | s = startswith(s, prefix); |
30 | if (!s) | |
31 | return false; | |
4897d1dc | 32 | |
4c701096 | 33 | return IN_SET(*s, ',', '\0'); |
4897d1dc ZJS |
34 | } |
35 | ||
cabffaf8 ZJS |
36 | static const char* systemd_kbd_model_map(void) { |
37 | const char* s; | |
38 | ||
39 | s = getenv("SYSTEMD_KBD_MODEL_MAP"); | |
40 | if (s) | |
41 | return s; | |
42 | ||
43 | return SYSTEMD_KBD_MODEL_MAP; | |
44 | } | |
45 | ||
46 | static const char* systemd_language_fallback_map(void) { | |
47 | const char* s; | |
48 | ||
49 | s = getenv("SYSTEMD_LANGUAGE_FALLBACK_MAP"); | |
50 | if (s) | |
51 | return s; | |
52 | ||
53 | return SYSTEMD_LANGUAGE_FALLBACK_MAP; | |
54 | } | |
55 | ||
4897d1dc ZJS |
56 | static void context_free_x11(Context *c) { |
57 | c->x11_layout = mfree(c->x11_layout); | |
58 | c->x11_options = mfree(c->x11_options); | |
59 | c->x11_model = mfree(c->x11_model); | |
60 | c->x11_variant = mfree(c->x11_variant); | |
61 | } | |
62 | ||
63 | static void context_free_vconsole(Context *c) { | |
64 | c->vc_keymap = mfree(c->vc_keymap); | |
65 | c->vc_keymap_toggle = mfree(c->vc_keymap_toggle); | |
66 | } | |
67 | ||
68 | static void context_free_locale(Context *c) { | |
ab4ab13c | 69 | for (LocaleVariable p = 0; p < _VARIABLE_LC_MAX; p++) |
4897d1dc ZJS |
70 | c->locale[p] = mfree(c->locale[p]); |
71 | } | |
72 | ||
6804d7a8 | 73 | void context_clear(Context *c) { |
4897d1dc ZJS |
74 | context_free_locale(c); |
75 | context_free_x11(c); | |
76 | context_free_vconsole(c); | |
65d34266 YW |
77 | |
78 | sd_bus_message_unref(c->locale_cache); | |
79 | sd_bus_message_unref(c->x11_cache); | |
80 | sd_bus_message_unref(c->vc_cache); | |
af7865c1 YW |
81 | |
82 | bus_verify_polkit_async_registry_free(c->polkit_registry); | |
4897d1dc ZJS |
83 | }; |
84 | ||
df4fd2c7 | 85 | void locale_simplify(char *locale[_VARIABLE_LC_MAX]) { |
ab4ab13c | 86 | for (LocaleVariable p = VARIABLE_LANG+1; p < _VARIABLE_LC_MAX; p++) |
df4fd2c7 YW |
87 | if (isempty(locale[p]) || streq_ptr(locale[VARIABLE_LANG], locale[p])) |
88 | locale[p] = mfree(locale[p]); | |
4897d1dc ZJS |
89 | } |
90 | ||
df4fd2c7 YW |
91 | int locale_read_data(Context *c, sd_bus_message *m) { |
92 | struct stat st; | |
4897d1dc ZJS |
93 | int r; |
94 | ||
df4fd2c7 | 95 | /* Do not try to re-read the file within single bus operation. */ |
65d34266 YW |
96 | if (m) { |
97 | if (m == c->locale_cache) | |
98 | return 0; | |
4897d1dc | 99 | |
65d34266 YW |
100 | sd_bus_message_unref(c->locale_cache); |
101 | c->locale_cache = sd_bus_message_ref(m); | |
102 | } | |
df4fd2c7 YW |
103 | |
104 | r = stat("/etc/locale.conf", &st); | |
105 | if (r < 0 && errno != ENOENT) | |
106 | return -errno; | |
107 | ||
108 | if (r >= 0) { | |
109 | usec_t t; | |
110 | ||
111 | /* If mtime is not changed, then we do not need to re-read the file. */ | |
112 | t = timespec_load(&st.st_mtim); | |
113 | if (c->locale_mtime != USEC_INFINITY && t == c->locale_mtime) | |
114 | return 0; | |
4897d1dc | 115 | |
df4fd2c7 YW |
116 | c->locale_mtime = t; |
117 | context_free_locale(c); | |
118 | ||
aa8fbc74 | 119 | r = parse_env_file(NULL, "/etc/locale.conf", |
df4fd2c7 YW |
120 | "LANG", &c->locale[VARIABLE_LANG], |
121 | "LANGUAGE", &c->locale[VARIABLE_LANGUAGE], | |
122 | "LC_CTYPE", &c->locale[VARIABLE_LC_CTYPE], | |
123 | "LC_NUMERIC", &c->locale[VARIABLE_LC_NUMERIC], | |
124 | "LC_TIME", &c->locale[VARIABLE_LC_TIME], | |
125 | "LC_COLLATE", &c->locale[VARIABLE_LC_COLLATE], | |
126 | "LC_MONETARY", &c->locale[VARIABLE_LC_MONETARY], | |
127 | "LC_MESSAGES", &c->locale[VARIABLE_LC_MESSAGES], | |
128 | "LC_PAPER", &c->locale[VARIABLE_LC_PAPER], | |
129 | "LC_NAME", &c->locale[VARIABLE_LC_NAME], | |
130 | "LC_ADDRESS", &c->locale[VARIABLE_LC_ADDRESS], | |
131 | "LC_TELEPHONE", &c->locale[VARIABLE_LC_TELEPHONE], | |
132 | "LC_MEASUREMENT", &c->locale[VARIABLE_LC_MEASUREMENT], | |
13df9c39 | 133 | "LC_IDENTIFICATION", &c->locale[VARIABLE_LC_IDENTIFICATION]); |
df4fd2c7 YW |
134 | if (r < 0) |
135 | return r; | |
136 | } else { | |
df4fd2c7 YW |
137 | c->locale_mtime = USEC_INFINITY; |
138 | context_free_locale(c); | |
139 | ||
4897d1dc | 140 | /* Fill in what we got passed from systemd. */ |
ab4ab13c | 141 | for (LocaleVariable p = 0; p < _VARIABLE_LC_MAX; p++) { |
4897d1dc ZJS |
142 | const char *name; |
143 | ||
144 | name = locale_variable_to_string(p); | |
145 | assert(name); | |
146 | ||
147 | r = free_and_strdup(&c->locale[p], empty_to_null(getenv(name))); | |
148 | if (r < 0) | |
149 | return r; | |
150 | } | |
4897d1dc ZJS |
151 | } |
152 | ||
df4fd2c7 YW |
153 | locale_simplify(c->locale); |
154 | return 0; | |
4897d1dc ZJS |
155 | } |
156 | ||
df4fd2c7 YW |
157 | int vconsole_read_data(Context *c, sd_bus_message *m) { |
158 | struct stat st; | |
159 | usec_t t; | |
4897d1dc | 160 | |
df4fd2c7 | 161 | /* Do not try to re-read the file within single bus operation. */ |
65d34266 YW |
162 | if (m) { |
163 | if (m == c->vc_cache) | |
164 | return 0; | |
df4fd2c7 | 165 | |
65d34266 YW |
166 | sd_bus_message_unref(c->vc_cache); |
167 | c->vc_cache = sd_bus_message_ref(m); | |
168 | } | |
df4fd2c7 YW |
169 | |
170 | if (stat("/etc/vconsole.conf", &st) < 0) { | |
171 | if (errno != ENOENT) | |
172 | return -errno; | |
173 | ||
174 | c->vc_mtime = USEC_INFINITY; | |
175 | context_free_vconsole(c); | |
176 | return 0; | |
177 | } | |
178 | ||
179 | /* If mtime is not changed, then we do not need to re-read */ | |
180 | t = timespec_load(&st.st_mtim); | |
181 | if (c->vc_mtime != USEC_INFINITY && t == c->vc_mtime) | |
182 | return 0; | |
183 | ||
184 | c->vc_mtime = t; | |
4897d1dc ZJS |
185 | context_free_vconsole(c); |
186 | ||
40f35786 ZJS |
187 | return parse_env_file(NULL, "/etc/vconsole.conf", |
188 | "KEYMAP", &c->vc_keymap, | |
189 | "KEYMAP_TOGGLE", &c->vc_keymap_toggle); | |
4897d1dc ZJS |
190 | } |
191 | ||
df4fd2c7 YW |
192 | int x11_read_data(Context *c, sd_bus_message *m) { |
193 | _cleanup_fclose_ FILE *f = NULL; | |
4897d1dc | 194 | bool in_section = false; |
df4fd2c7 YW |
195 | struct stat st; |
196 | usec_t t; | |
4897d1dc ZJS |
197 | int r; |
198 | ||
df4fd2c7 | 199 | /* Do not try to re-read the file within single bus operation. */ |
65d34266 YW |
200 | if (m) { |
201 | if (m == c->x11_cache) | |
202 | return 0; | |
df4fd2c7 | 203 | |
65d34266 YW |
204 | sd_bus_message_unref(c->x11_cache); |
205 | c->x11_cache = sd_bus_message_ref(m); | |
206 | } | |
df4fd2c7 YW |
207 | |
208 | if (stat("/etc/X11/xorg.conf.d/00-keyboard.conf", &st) < 0) { | |
209 | if (errno != ENOENT) | |
210 | return -errno; | |
211 | ||
212 | c->x11_mtime = USEC_INFINITY; | |
213 | context_free_x11(c); | |
214 | return 0; | |
215 | } | |
216 | ||
217 | /* If mtime is not changed, then we do not need to re-read */ | |
218 | t = timespec_load(&st.st_mtim); | |
219 | if (c->x11_mtime != USEC_INFINITY && t == c->x11_mtime) | |
220 | return 0; | |
221 | ||
222 | c->x11_mtime = t; | |
4897d1dc ZJS |
223 | context_free_x11(c); |
224 | ||
225 | f = fopen("/etc/X11/xorg.conf.d/00-keyboard.conf", "re"); | |
226 | if (!f) | |
df4fd2c7 | 227 | return -errno; |
4897d1dc | 228 | |
1d47b569 LP |
229 | for (;;) { |
230 | _cleanup_free_ char *line = NULL; | |
4897d1dc ZJS |
231 | char *l; |
232 | ||
1d47b569 LP |
233 | r = read_line(f, LONG_LINE_MAX, &line); |
234 | if (r < 0) | |
235 | return r; | |
236 | if (r == 0) | |
237 | break; | |
4897d1dc | 238 | |
1d47b569 | 239 | l = strstrip(line); |
4c701096 | 240 | if (IN_SET(l[0], 0, '#')) |
4897d1dc ZJS |
241 | continue; |
242 | ||
243 | if (in_section && first_word(l, "Option")) { | |
244 | _cleanup_strv_free_ char **a = NULL; | |
245 | ||
90e30d76 | 246 | r = strv_split_full(&a, l, WHITESPACE, EXTRACT_UNQUOTE); |
4897d1dc ZJS |
247 | if (r < 0) |
248 | return r; | |
249 | ||
250 | if (strv_length(a) == 3) { | |
251 | char **p = NULL; | |
252 | ||
253 | if (streq(a[1], "XkbLayout")) | |
254 | p = &c->x11_layout; | |
255 | else if (streq(a[1], "XkbModel")) | |
256 | p = &c->x11_model; | |
257 | else if (streq(a[1], "XkbVariant")) | |
258 | p = &c->x11_variant; | |
259 | else if (streq(a[1], "XkbOptions")) | |
260 | p = &c->x11_options; | |
261 | ||
38cd55b0 | 262 | if (p) |
f9ecfd3b | 263 | free_and_replace(*p, a[2]); |
4897d1dc ZJS |
264 | } |
265 | ||
266 | } else if (!in_section && first_word(l, "Section")) { | |
267 | _cleanup_strv_free_ char **a = NULL; | |
268 | ||
90e30d76 | 269 | r = strv_split_full(&a, l, WHITESPACE, EXTRACT_UNQUOTE); |
4897d1dc ZJS |
270 | if (r < 0) |
271 | return -ENOMEM; | |
272 | ||
273 | if (strv_length(a) == 2 && streq(a[1], "InputClass")) | |
274 | in_section = true; | |
275 | ||
276 | } else if (in_section && first_word(l, "EndSection")) | |
277 | in_section = false; | |
278 | } | |
279 | ||
280 | return 0; | |
281 | } | |
282 | ||
4897d1dc | 283 | int locale_write_data(Context *c, char ***settings) { |
4897d1dc | 284 | _cleanup_strv_free_ char **l = NULL; |
df4fd2c7 | 285 | struct stat st; |
ab4ab13c | 286 | int r; |
4897d1dc ZJS |
287 | |
288 | /* Set values will be returned as strv in *settings on success. */ | |
289 | ||
f08231fe ZJS |
290 | for (LocaleVariable p = 0; p < _VARIABLE_LC_MAX; p++) |
291 | if (!isempty(c->locale[p])) { | |
292 | r = strv_env_assign(&l, locale_variable_to_string(p), c->locale[p]); | |
293 | if (r < 0) | |
294 | return r; | |
295 | } | |
4897d1dc ZJS |
296 | |
297 | if (strv_isempty(l)) { | |
298 | if (unlink("/etc/locale.conf") < 0) | |
299 | return errno == ENOENT ? 0 : -errno; | |
300 | ||
df4fd2c7 | 301 | c->locale_mtime = USEC_INFINITY; |
4897d1dc ZJS |
302 | return 0; |
303 | } | |
304 | ||
305 | r = write_env_file_label("/etc/locale.conf", l); | |
306 | if (r < 0) | |
307 | return r; | |
308 | ||
ae2a15bc | 309 | *settings = TAKE_PTR(l); |
df4fd2c7 YW |
310 | |
311 | if (stat("/etc/locale.conf", &st) >= 0) | |
312 | c->locale_mtime = timespec_load(&st.st_mtim); | |
313 | ||
4897d1dc ZJS |
314 | return 0; |
315 | } | |
316 | ||
317 | int vconsole_write_data(Context *c) { | |
4897d1dc | 318 | _cleanup_strv_free_ char **l = NULL; |
df4fd2c7 YW |
319 | struct stat st; |
320 | int r; | |
4897d1dc | 321 | |
aa8fbc74 | 322 | r = load_env_file(NULL, "/etc/vconsole.conf", &l); |
4897d1dc ZJS |
323 | if (r < 0 && r != -ENOENT) |
324 | return r; | |
325 | ||
f08231fe ZJS |
326 | r = strv_env_assign(&l, "KEYMAP", empty_to_null(c->vc_keymap)); |
327 | if (r < 0) | |
328 | return r; | |
4897d1dc | 329 | |
f08231fe ZJS |
330 | r = strv_env_assign(&l, "KEYMAP_TOGGLE", empty_to_null(c->vc_keymap_toggle)); |
331 | if (r < 0) | |
332 | return r; | |
4897d1dc ZJS |
333 | |
334 | if (strv_isempty(l)) { | |
335 | if (unlink("/etc/vconsole.conf") < 0) | |
336 | return errno == ENOENT ? 0 : -errno; | |
337 | ||
df4fd2c7 | 338 | c->vc_mtime = USEC_INFINITY; |
4897d1dc ZJS |
339 | return 0; |
340 | } | |
341 | ||
df4fd2c7 YW |
342 | r = write_env_file_label("/etc/vconsole.conf", l); |
343 | if (r < 0) | |
344 | return r; | |
345 | ||
346 | if (stat("/etc/vconsole.conf", &st) >= 0) | |
347 | c->vc_mtime = timespec_load(&st.st_mtim); | |
348 | ||
349 | return 0; | |
4897d1dc ZJS |
350 | } |
351 | ||
352 | int x11_write_data(Context *c) { | |
353 | _cleanup_fclose_ FILE *f = NULL; | |
354 | _cleanup_free_ char *temp_path = NULL; | |
df4fd2c7 | 355 | struct stat st; |
4897d1dc ZJS |
356 | int r; |
357 | ||
358 | if (isempty(c->x11_layout) && | |
359 | isempty(c->x11_model) && | |
360 | isempty(c->x11_variant) && | |
361 | isempty(c->x11_options)) { | |
362 | ||
363 | if (unlink("/etc/X11/xorg.conf.d/00-keyboard.conf") < 0) | |
364 | return errno == ENOENT ? 0 : -errno; | |
365 | ||
df4fd2c7 | 366 | c->vc_mtime = USEC_INFINITY; |
4897d1dc ZJS |
367 | return 0; |
368 | } | |
369 | ||
6e5dcce4 | 370 | (void) mkdir_p_label("/etc/X11/xorg.conf.d", 0755); |
4897d1dc ZJS |
371 | r = fopen_temporary("/etc/X11/xorg.conf.d/00-keyboard.conf", &f, &temp_path); |
372 | if (r < 0) | |
373 | return r; | |
374 | ||
0d536673 | 375 | (void) fchmod(fileno(f), 0644); |
4897d1dc | 376 | |
0d536673 LP |
377 | fputs("# Written by systemd-localed(8), read by systemd-localed and Xorg. It's\n" |
378 | "# probably wise not to edit this file manually. Use localectl(1) to\n" | |
379 | "# instruct systemd-localed to update it.\n" | |
380 | "Section \"InputClass\"\n" | |
381 | " Identifier \"system-keyboard\"\n" | |
382 | " MatchIsKeyboard \"on\"\n", f); | |
4897d1dc ZJS |
383 | |
384 | if (!isempty(c->x11_layout)) | |
385 | fprintf(f, " Option \"XkbLayout\" \"%s\"\n", c->x11_layout); | |
386 | ||
387 | if (!isempty(c->x11_model)) | |
388 | fprintf(f, " Option \"XkbModel\" \"%s\"\n", c->x11_model); | |
389 | ||
390 | if (!isempty(c->x11_variant)) | |
391 | fprintf(f, " Option \"XkbVariant\" \"%s\"\n", c->x11_variant); | |
392 | ||
393 | if (!isempty(c->x11_options)) | |
394 | fprintf(f, " Option \"XkbOptions\" \"%s\"\n", c->x11_options); | |
395 | ||
0d536673 | 396 | fputs("EndSection\n", f); |
4897d1dc | 397 | |
0675e94a | 398 | r = fflush_sync_and_check(f); |
4897d1dc ZJS |
399 | if (r < 0) |
400 | goto fail; | |
401 | ||
402 | if (rename(temp_path, "/etc/X11/xorg.conf.d/00-keyboard.conf") < 0) { | |
403 | r = -errno; | |
404 | goto fail; | |
405 | } | |
406 | ||
df4fd2c7 YW |
407 | if (stat("/etc/X11/xorg.conf.d/00-keyboard.conf", &st) >= 0) |
408 | c->x11_mtime = timespec_load(&st.st_mtim); | |
409 | ||
4897d1dc ZJS |
410 | return 0; |
411 | ||
412 | fail: | |
4897d1dc ZJS |
413 | if (temp_path) |
414 | (void) unlink(temp_path); | |
415 | ||
416 | return r; | |
417 | } | |
418 | ||
419 | static int read_next_mapping(const char* filename, | |
420 | unsigned min_fields, unsigned max_fields, | |
421 | FILE *f, unsigned *n, char ***a) { | |
422 | assert(f); | |
423 | assert(n); | |
424 | assert(a); | |
425 | ||
426 | for (;;) { | |
1d47b569 LP |
427 | _cleanup_free_ char *line = NULL; |
428 | size_t length; | |
4897d1dc ZJS |
429 | char *l, **b; |
430 | int r; | |
4897d1dc | 431 | |
1d47b569 LP |
432 | r = read_line(f, LONG_LINE_MAX, &line); |
433 | if (r < 0) | |
434 | return r; | |
435 | if (r == 0) | |
436 | break; | |
4897d1dc ZJS |
437 | |
438 | (*n)++; | |
439 | ||
440 | l = strstrip(line); | |
4c701096 | 441 | if (IN_SET(l[0], 0, '#')) |
4897d1dc ZJS |
442 | continue; |
443 | ||
90e30d76 | 444 | r = strv_split_full(&b, l, WHITESPACE, EXTRACT_UNQUOTE); |
4897d1dc ZJS |
445 | if (r < 0) |
446 | return r; | |
447 | ||
448 | length = strv_length(b); | |
449 | if (length < min_fields || length > max_fields) { | |
450 | log_error("Invalid line %s:%u, ignoring.", filename, *n); | |
451 | strv_free(b); | |
452 | continue; | |
453 | ||
454 | } | |
455 | ||
456 | *a = b; | |
457 | return 1; | |
458 | } | |
1d47b569 LP |
459 | |
460 | return 0; | |
4897d1dc ZJS |
461 | } |
462 | ||
463 | int vconsole_convert_to_x11(Context *c) { | |
cabffaf8 | 464 | const char *map; |
6f3287b3 | 465 | int modified = -1; |
4897d1dc | 466 | |
cabffaf8 ZJS |
467 | map = systemd_kbd_model_map(); |
468 | ||
4897d1dc | 469 | if (isempty(c->vc_keymap)) { |
4897d1dc ZJS |
470 | modified = |
471 | !isempty(c->x11_layout) || | |
472 | !isempty(c->x11_model) || | |
473 | !isempty(c->x11_variant) || | |
474 | !isempty(c->x11_options); | |
475 | ||
476 | context_free_x11(c); | |
477 | } else { | |
478 | _cleanup_fclose_ FILE *f = NULL; | |
479 | unsigned n = 0; | |
480 | ||
cabffaf8 | 481 | f = fopen(map, "re"); |
4897d1dc ZJS |
482 | if (!f) |
483 | return -errno; | |
484 | ||
485 | for (;;) { | |
486 | _cleanup_strv_free_ char **a = NULL; | |
487 | int r; | |
488 | ||
cabffaf8 | 489 | r = read_next_mapping(map, 5, UINT_MAX, f, &n, &a); |
4897d1dc ZJS |
490 | if (r < 0) |
491 | return r; | |
492 | if (r == 0) | |
493 | break; | |
494 | ||
495 | if (!streq(c->vc_keymap, a[0])) | |
496 | continue; | |
497 | ||
dc90e0fa LP |
498 | if (!streq_ptr(c->x11_layout, empty_or_dash_to_null(a[1])) || |
499 | !streq_ptr(c->x11_model, empty_or_dash_to_null(a[2])) || | |
500 | !streq_ptr(c->x11_variant, empty_or_dash_to_null(a[3])) || | |
501 | !streq_ptr(c->x11_options, empty_or_dash_to_null(a[4]))) { | |
4897d1dc | 502 | |
dc90e0fa LP |
503 | if (free_and_strdup(&c->x11_layout, empty_or_dash_to_null(a[1])) < 0 || |
504 | free_and_strdup(&c->x11_model, empty_or_dash_to_null(a[2])) < 0 || | |
505 | free_and_strdup(&c->x11_variant, empty_or_dash_to_null(a[3])) < 0 || | |
506 | free_and_strdup(&c->x11_options, empty_or_dash_to_null(a[4])) < 0) | |
4897d1dc ZJS |
507 | return -ENOMEM; |
508 | ||
509 | modified = true; | |
510 | } | |
511 | ||
512 | break; | |
513 | } | |
514 | } | |
515 | ||
6f3287b3 | 516 | if (modified > 0) |
4897d1dc ZJS |
517 | log_info("Changing X11 keyboard layout to '%s' model '%s' variant '%s' options '%s'", |
518 | strempty(c->x11_layout), | |
519 | strempty(c->x11_model), | |
520 | strempty(c->x11_variant), | |
521 | strempty(c->x11_options)); | |
6f3287b3 ZJS |
522 | else if (modified < 0) |
523 | log_notice("X11 keyboard layout was not modified: no conversion found for \"%s\".", | |
524 | c->vc_keymap); | |
4897d1dc | 525 | else |
6f3287b3 | 526 | log_debug("X11 keyboard layout did not need to be modified."); |
4897d1dc | 527 | |
6f3287b3 | 528 | return modified > 0; |
4897d1dc ZJS |
529 | } |
530 | ||
531 | int find_converted_keymap(const char *x11_layout, const char *x11_variant, char **new_keymap) { | |
532 | const char *dir; | |
c2b2df60 | 533 | _cleanup_free_ char *n = NULL; |
4897d1dc ZJS |
534 | |
535 | if (x11_variant) | |
605405c6 | 536 | n = strjoin(x11_layout, "-", x11_variant); |
4897d1dc ZJS |
537 | else |
538 | n = strdup(x11_layout); | |
539 | if (!n) | |
540 | return -ENOMEM; | |
541 | ||
542 | NULSTR_FOREACH(dir, KBD_KEYMAP_DIRS) { | |
543 | _cleanup_free_ char *p = NULL, *pz = NULL; | |
544 | bool uncompressed; | |
545 | ||
605405c6 ZJS |
546 | p = strjoin(dir, "xkb/", n, ".map"); |
547 | pz = strjoin(dir, "xkb/", n, ".map.gz"); | |
4897d1dc ZJS |
548 | if (!p || !pz) |
549 | return -ENOMEM; | |
550 | ||
551 | uncompressed = access(p, F_OK) == 0; | |
552 | if (uncompressed || access(pz, F_OK) == 0) { | |
553 | log_debug("Found converted keymap %s at %s", | |
554 | n, uncompressed ? p : pz); | |
555 | ||
ae2a15bc | 556 | *new_keymap = TAKE_PTR(n); |
4897d1dc ZJS |
557 | return 1; |
558 | } | |
559 | } | |
560 | ||
561 | return 0; | |
562 | } | |
563 | ||
6a6e9c03 | 564 | int find_legacy_keymap(Context *c, char **ret) { |
cabffaf8 ZJS |
565 | const char *map; |
566 | _cleanup_fclose_ FILE *f = NULL; | |
6a6e9c03 | 567 | _cleanup_free_ char *new_keymap = NULL; |
4897d1dc ZJS |
568 | unsigned n = 0; |
569 | unsigned best_matching = 0; | |
570 | int r; | |
571 | ||
5ad327dd ZJS |
572 | assert(!isempty(c->x11_layout)); |
573 | ||
cabffaf8 ZJS |
574 | map = systemd_kbd_model_map(); |
575 | ||
576 | f = fopen(map, "re"); | |
4897d1dc ZJS |
577 | if (!f) |
578 | return -errno; | |
579 | ||
580 | for (;;) { | |
581 | _cleanup_strv_free_ char **a = NULL; | |
582 | unsigned matching = 0; | |
583 | ||
cabffaf8 | 584 | r = read_next_mapping(map, 5, UINT_MAX, f, &n, &a); |
4897d1dc ZJS |
585 | if (r < 0) |
586 | return r; | |
587 | if (r == 0) | |
588 | break; | |
589 | ||
590 | /* Determine how well matching this entry is */ | |
5ad327dd | 591 | if (streq(c->x11_layout, a[1])) |
4897d1dc ZJS |
592 | /* If we got an exact match, this is best */ |
593 | matching = 10; | |
594 | else { | |
595 | /* We have multiple X layouts, look for an | |
596 | * entry that matches our key with everything | |
597 | * but the first layout stripped off. */ | |
598 | if (startswith_comma(c->x11_layout, a[1])) | |
599 | matching = 5; | |
600 | else { | |
6d946490 | 601 | _cleanup_free_ char *x = NULL; |
4897d1dc ZJS |
602 | |
603 | /* If that didn't work, strip off the | |
604 | * other layouts from the entry, too */ | |
6d946490 | 605 | x = strndup(a[1], strcspn(a[1], ",")); |
4897d1dc ZJS |
606 | if (startswith_comma(c->x11_layout, x)) |
607 | matching = 1; | |
608 | } | |
609 | } | |
610 | ||
611 | if (matching > 0) { | |
612 | if (isempty(c->x11_model) || streq_ptr(c->x11_model, a[2])) { | |
613 | matching++; | |
614 | ||
615 | if (streq_ptr(c->x11_variant, a[3])) { | |
616 | matching++; | |
617 | ||
618 | if (streq_ptr(c->x11_options, a[4])) | |
619 | matching++; | |
620 | } | |
621 | } | |
622 | } | |
623 | ||
624 | /* The best matching entry so far, then let's save that */ | |
625 | if (matching >= MAX(best_matching, 1u)) { | |
626 | log_debug("Found legacy keymap %s with score %u", | |
627 | a[0], matching); | |
628 | ||
629 | if (matching > best_matching) { | |
630 | best_matching = matching; | |
631 | ||
6a6e9c03 | 632 | r = free_and_strdup(&new_keymap, a[0]); |
4897d1dc ZJS |
633 | if (r < 0) |
634 | return r; | |
635 | } | |
636 | } | |
637 | } | |
638 | ||
639 | if (best_matching < 10 && c->x11_layout) { | |
640 | /* The best match is only the first part of the X11 | |
641 | * keymap. Check if we have a converted map which | |
642 | * matches just the first layout. | |
643 | */ | |
644 | char *l, *v = NULL, *converted; | |
645 | ||
2f82562b | 646 | l = strndupa_safe(c->x11_layout, strcspn(c->x11_layout, ",")); |
4897d1dc | 647 | if (c->x11_variant) |
2f82562b LP |
648 | v = strndupa_safe(c->x11_variant, |
649 | strcspn(c->x11_variant, ",")); | |
4897d1dc ZJS |
650 | r = find_converted_keymap(l, v, &converted); |
651 | if (r < 0) | |
652 | return r; | |
6a6e9c03 ZJS |
653 | if (r > 0) |
654 | free_and_replace(new_keymap, converted); | |
4897d1dc ZJS |
655 | } |
656 | ||
6a6e9c03 ZJS |
657 | *ret = TAKE_PTR(new_keymap); |
658 | return (bool) *ret; | |
4897d1dc ZJS |
659 | } |
660 | ||
661 | int find_language_fallback(const char *lang, char **language) { | |
cabffaf8 | 662 | const char *map; |
4897d1dc ZJS |
663 | _cleanup_fclose_ FILE *f = NULL; |
664 | unsigned n = 0; | |
665 | ||
aa63b56f | 666 | assert(lang); |
4897d1dc ZJS |
667 | assert(language); |
668 | ||
cabffaf8 ZJS |
669 | map = systemd_language_fallback_map(); |
670 | ||
671 | f = fopen(map, "re"); | |
4897d1dc ZJS |
672 | if (!f) |
673 | return -errno; | |
674 | ||
675 | for (;;) { | |
676 | _cleanup_strv_free_ char **a = NULL; | |
677 | int r; | |
678 | ||
cabffaf8 | 679 | r = read_next_mapping(map, 2, 2, f, &n, &a); |
4897d1dc ZJS |
680 | if (r <= 0) |
681 | return r; | |
682 | ||
683 | if (streq(lang, a[0])) { | |
684 | assert(strv_length(a) == 2); | |
1cc6c93a | 685 | *language = TAKE_PTR(a[1]); |
4897d1dc ZJS |
686 | return 1; |
687 | } | |
688 | } | |
689 | ||
04499a70 | 690 | assert_not_reached(); |
4897d1dc ZJS |
691 | } |
692 | ||
693 | int x11_convert_to_vconsole(Context *c) { | |
694 | bool modified = false; | |
695 | ||
696 | if (isempty(c->x11_layout)) { | |
4897d1dc ZJS |
697 | modified = |
698 | !isempty(c->vc_keymap) || | |
699 | !isempty(c->vc_keymap_toggle); | |
700 | ||
aa63b56f | 701 | context_free_vconsole(c); |
4897d1dc | 702 | } else { |
6a837b03 | 703 | _cleanup_free_ char *new_keymap = NULL; |
4897d1dc ZJS |
704 | int r; |
705 | ||
706 | r = find_converted_keymap(c->x11_layout, c->x11_variant, &new_keymap); | |
707 | if (r < 0) | |
708 | return r; | |
709 | else if (r == 0) { | |
710 | r = find_legacy_keymap(c, &new_keymap); | |
711 | if (r < 0) | |
712 | return r; | |
713 | } | |
5ad327dd ZJS |
714 | if (r == 0) |
715 | /* We search for layout-variant match first, but then we also look | |
716 | * for anything which matches just the layout. So it's accurate to say | |
717 | * that we couldn't find anything which matches the layout. */ | |
718 | log_notice("No conversion to virtual console map found for \"%s\".", | |
719 | c->x11_layout); | |
4897d1dc ZJS |
720 | |
721 | if (!streq_ptr(c->vc_keymap, new_keymap)) { | |
6a837b03 | 722 | free_and_replace(c->vc_keymap, new_keymap); |
4897d1dc ZJS |
723 | c->vc_keymap_toggle = mfree(c->vc_keymap_toggle); |
724 | modified = true; | |
6a837b03 | 725 | } |
4897d1dc ZJS |
726 | } |
727 | ||
728 | if (modified) | |
729 | log_info("Changing virtual console keymap to '%s' toggle '%s'", | |
730 | strempty(c->vc_keymap), strempty(c->vc_keymap_toggle)); | |
731 | else | |
732 | log_debug("Virtual console keymap was not modified."); | |
733 | ||
734 | return modified; | |
735 | } | |
8f20232f MK |
736 | |
737 | bool locale_gen_check_available(void) { | |
738 | #if HAVE_LOCALEGEN | |
739 | if (access(LOCALEGEN_PATH, X_OK) < 0) { | |
740 | if (errno != ENOENT) | |
741 | log_warning_errno(errno, "Unable to determine whether " LOCALEGEN_PATH " exists and is executable, assuming it is not: %m"); | |
742 | return false; | |
743 | } | |
744 | if (access("/etc/locale.gen", F_OK) < 0) { | |
745 | if (errno != ENOENT) | |
746 | log_warning_errno(errno, "Unable to determine whether /etc/locale.gen exists, assuming it does not: %m"); | |
747 | return false; | |
748 | } | |
749 | return true; | |
750 | #else | |
751 | return false; | |
752 | #endif | |
753 | } | |
754 | ||
755 | #if HAVE_LOCALEGEN | |
756 | static bool locale_encoding_is_utf8_or_unspecified(const char *locale) { | |
757 | const char *c = strchr(locale, '.'); | |
758 | return !c || strcaseeq(c, ".UTF-8") || strcasestr(locale, ".UTF-8@"); | |
759 | } | |
760 | ||
761 | static int locale_gen_locale_supported(const char *locale_entry) { | |
762 | /* Returns an error valus <= 0 if the locale-gen entry is invalid or unsupported, | |
763 | * 1 in case the locale entry is valid, and -EOPNOTSUPP specifically in case | |
764 | * the distributor has not provided us with a SUPPORTED file to check | |
765 | * locale for validity. */ | |
766 | ||
767 | _cleanup_fclose_ FILE *f = NULL; | |
768 | int r; | |
769 | ||
770 | assert(locale_entry); | |
771 | ||
772 | /* Locale templates without country code are never supported */ | |
773 | if (!strstr(locale_entry, "_")) | |
774 | return -EINVAL; | |
775 | ||
776 | f = fopen("/usr/share/i18n/SUPPORTED", "re"); | |
777 | if (!f) { | |
778 | if (errno == ENOENT) | |
779 | return log_debug_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), | |
780 | "Unable to check validity of locale entry %s: /usr/share/i18n/SUPPORTED does not exist", | |
781 | locale_entry); | |
782 | return -errno; | |
783 | } | |
784 | ||
785 | for (;;) { | |
786 | _cleanup_free_ char *line = NULL; | |
787 | ||
788 | r = read_line(f, LONG_LINE_MAX, &line); | |
789 | if (r < 0) | |
790 | return log_debug_errno(r, "Failed to read /usr/share/i18n/SUPPORTED: %m"); | |
791 | if (r == 0) | |
792 | return 0; | |
793 | ||
794 | line = strstrip(line); | |
795 | if (strcaseeq_ptr(line, locale_entry)) | |
796 | return 1; | |
797 | } | |
798 | } | |
799 | #endif | |
800 | ||
801 | int locale_gen_enable_locale(const char *locale) { | |
802 | #if HAVE_LOCALEGEN | |
803 | _cleanup_fclose_ FILE *fr = NULL, *fw = NULL; | |
804 | _cleanup_(unlink_and_freep) char *temp_path = NULL; | |
805 | _cleanup_free_ char *locale_entry = NULL; | |
806 | bool locale_enabled = false, first_line = false; | |
807 | bool write_new = false; | |
808 | int r; | |
809 | ||
810 | if (isempty(locale)) | |
811 | return 0; | |
812 | ||
813 | if (locale_encoding_is_utf8_or_unspecified(locale)) { | |
814 | locale_entry = strjoin(locale, " UTF-8"); | |
815 | if (!locale_entry) | |
816 | return -ENOMEM; | |
817 | } else | |
818 | return -ENOEXEC; /* We do not process non-UTF-8 locale */ | |
819 | ||
820 | r = locale_gen_locale_supported(locale_entry); | |
821 | if (r == 0) | |
822 | return -EINVAL; | |
823 | if (r < 0 && r != -EOPNOTSUPP) | |
824 | return r; | |
825 | ||
826 | fr = fopen("/etc/locale.gen", "re"); | |
827 | if (!fr) { | |
828 | if (errno != ENOENT) | |
829 | return -errno; | |
830 | write_new = true; | |
831 | } | |
832 | ||
833 | r = fopen_temporary("/etc/locale.gen", &fw, &temp_path); | |
834 | if (r < 0) | |
835 | return r; | |
836 | ||
837 | if (write_new) | |
838 | (void) fchmod(fileno(fw), 0644); | |
839 | else { | |
840 | /* apply mode & xattrs of the original file to new file */ | |
841 | r = copy_access(fileno(fr), fileno(fw)); | |
842 | if (r < 0) | |
843 | return r; | |
23e026de | 844 | r = copy_xattr(fileno(fr), fileno(fw), COPY_ALL_XATTRS); |
8f20232f MK |
845 | if (r < 0) |
846 | return r; | |
847 | } | |
848 | ||
849 | if (!write_new) { | |
850 | /* The config file ends with a line break, which we do not want to include before potentially appending a new locale | |
851 | * instead of uncommenting an existing line. By prepending linebreaks, we can avoid buffering this file but can still write | |
852 | * a nice config file without empty lines */ | |
853 | first_line = true; | |
854 | for (;;) { | |
855 | _cleanup_free_ char *line = NULL; | |
856 | char *line_locale; | |
857 | ||
858 | r = read_line(fr, LONG_LINE_MAX, &line); | |
859 | if (r < 0) | |
860 | return r; | |
861 | if (r == 0) | |
862 | break; | |
863 | ||
864 | if (locale_enabled) { | |
865 | /* Just complete writing the file if the new locale was already enabled */ | |
866 | if (!first_line) | |
867 | fputc('\n', fw); | |
868 | fputs(line, fw); | |
869 | first_line = false; | |
870 | continue; | |
871 | } | |
872 | ||
873 | line = strstrip(line); | |
874 | if (isempty(line)) { | |
875 | fputc('\n', fw); | |
876 | first_line = false; | |
877 | continue; | |
878 | } | |
879 | ||
880 | line_locale = line; | |
881 | if (line_locale[0] == '#') | |
882 | line_locale = strstrip(line_locale + 1); | |
883 | else if (strcaseeq_ptr(line_locale, locale_entry)) | |
884 | return 0; /* the file already had our locale activated, so skip updating it */ | |
885 | ||
886 | if (strcaseeq_ptr(line_locale, locale_entry)) { | |
887 | /* Uncomment existing line for new locale */ | |
888 | if (!first_line) | |
889 | fputc('\n', fw); | |
890 | fputs(locale_entry, fw); | |
891 | locale_enabled = true; | |
892 | first_line = false; | |
893 | continue; | |
894 | } | |
895 | ||
896 | /* The line was not for the locale we want to enable, just copy it */ | |
897 | if (!first_line) | |
898 | fputc('\n', fw); | |
899 | fputs(line, fw); | |
900 | first_line = false; | |
901 | } | |
902 | } | |
903 | ||
904 | /* Add locale to enable to the end of the file if it was not found as commented line */ | |
905 | if (!locale_enabled) { | |
906 | if (!write_new) | |
907 | fputc('\n', fw); | |
908 | fputs(locale_entry, fw); | |
909 | } | |
910 | fputc('\n', fw); | |
911 | ||
912 | r = fflush_sync_and_check(fw); | |
913 | if (r < 0) | |
914 | return r; | |
915 | ||
916 | if (rename(temp_path, "/etc/locale.gen") < 0) | |
917 | return -errno; | |
918 | temp_path = mfree(temp_path); | |
919 | ||
920 | return 0; | |
921 | #else | |
922 | return -EOPNOTSUPP; | |
923 | #endif | |
924 | } | |
925 | ||
926 | int locale_gen_run(void) { | |
927 | #if HAVE_LOCALEGEN | |
928 | pid_t pid; | |
929 | int r; | |
930 | ||
931 | r = safe_fork("(sd-localegen)", FORK_RESET_SIGNALS|FORK_RLIMIT_NOFILE_SAFE|FORK_CLOSE_ALL_FDS|FORK_LOG|FORK_WAIT, &pid); | |
932 | if (r < 0) | |
933 | return r; | |
934 | if (r == 0) { | |
935 | execl(LOCALEGEN_PATH, LOCALEGEN_PATH, NULL); | |
936 | _exit(EXIT_FAILURE); | |
937 | } | |
938 | ||
939 | return 0; | |
940 | #else | |
941 | return -EOPNOTSUPP; | |
942 | #endif | |
943 | } |