]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
4897d1dc ZJS |
2 | |
3 | #include <errno.h> | |
0d536673 | 4 | #include <stdio_ext.h> |
4897d1dc ZJS |
5 | #include <string.h> |
6 | #include <unistd.h> | |
7 | ||
af7865c1 | 8 | #include "bus-util.h" |
686d13b9 | 9 | #include "env-file-label.h" |
f05e1d0d | 10 | #include "env-file.h" |
4897d1dc ZJS |
11 | #include "env-util.h" |
12 | #include "fd-util.h" | |
13 | #include "fileio-label.h" | |
14 | #include "fileio.h" | |
f05e1d0d | 15 | #include "kbd-util.h" |
4897d1dc ZJS |
16 | #include "keymap-util.h" |
17 | #include "locale-util.h" | |
18 | #include "macro.h" | |
19 | #include "mkdir.h" | |
d8b4d14d | 20 | #include "nulstr-util.h" |
4897d1dc ZJS |
21 | #include "string-util.h" |
22 | #include "strv.h" | |
e4de7287 | 23 | #include "tmpfile-util.h" |
4897d1dc ZJS |
24 | |
25 | static bool startswith_comma(const char *s, const char *prefix) { | |
5ad327dd ZJS |
26 | s = startswith(s, prefix); |
27 | if (!s) | |
28 | return false; | |
4897d1dc | 29 | |
4c701096 | 30 | return IN_SET(*s, ',', '\0'); |
4897d1dc ZJS |
31 | } |
32 | ||
33 | static const char* strnulldash(const char *s) { | |
34 | return isempty(s) || streq(s, "-") ? NULL : s; | |
35 | } | |
36 | ||
cabffaf8 ZJS |
37 | static const char* systemd_kbd_model_map(void) { |
38 | const char* s; | |
39 | ||
40 | s = getenv("SYSTEMD_KBD_MODEL_MAP"); | |
41 | if (s) | |
42 | return s; | |
43 | ||
44 | return SYSTEMD_KBD_MODEL_MAP; | |
45 | } | |
46 | ||
47 | static const char* systemd_language_fallback_map(void) { | |
48 | const char* s; | |
49 | ||
50 | s = getenv("SYSTEMD_LANGUAGE_FALLBACK_MAP"); | |
51 | if (s) | |
52 | return s; | |
53 | ||
54 | return SYSTEMD_LANGUAGE_FALLBACK_MAP; | |
55 | } | |
56 | ||
4897d1dc ZJS |
57 | static void context_free_x11(Context *c) { |
58 | c->x11_layout = mfree(c->x11_layout); | |
59 | c->x11_options = mfree(c->x11_options); | |
60 | c->x11_model = mfree(c->x11_model); | |
61 | c->x11_variant = mfree(c->x11_variant); | |
62 | } | |
63 | ||
64 | static void context_free_vconsole(Context *c) { | |
65 | c->vc_keymap = mfree(c->vc_keymap); | |
66 | c->vc_keymap_toggle = mfree(c->vc_keymap_toggle); | |
67 | } | |
68 | ||
69 | static void context_free_locale(Context *c) { | |
70 | int p; | |
71 | ||
72 | for (p = 0; p < _VARIABLE_LC_MAX; p++) | |
73 | c->locale[p] = mfree(c->locale[p]); | |
74 | } | |
75 | ||
6804d7a8 | 76 | void context_clear(Context *c) { |
4897d1dc ZJS |
77 | context_free_locale(c); |
78 | context_free_x11(c); | |
79 | context_free_vconsole(c); | |
65d34266 YW |
80 | |
81 | sd_bus_message_unref(c->locale_cache); | |
82 | sd_bus_message_unref(c->x11_cache); | |
83 | sd_bus_message_unref(c->vc_cache); | |
af7865c1 YW |
84 | |
85 | bus_verify_polkit_async_registry_free(c->polkit_registry); | |
4897d1dc ZJS |
86 | }; |
87 | ||
df4fd2c7 | 88 | void locale_simplify(char *locale[_VARIABLE_LC_MAX]) { |
4897d1dc ZJS |
89 | int p; |
90 | ||
91 | for (p = VARIABLE_LANG+1; p < _VARIABLE_LC_MAX; p++) | |
df4fd2c7 YW |
92 | if (isempty(locale[p]) || streq_ptr(locale[VARIABLE_LANG], locale[p])) |
93 | locale[p] = mfree(locale[p]); | |
4897d1dc ZJS |
94 | } |
95 | ||
df4fd2c7 YW |
96 | int locale_read_data(Context *c, sd_bus_message *m) { |
97 | struct stat st; | |
4897d1dc ZJS |
98 | int r; |
99 | ||
df4fd2c7 | 100 | /* Do not try to re-read the file within single bus operation. */ |
65d34266 YW |
101 | if (m) { |
102 | if (m == c->locale_cache) | |
103 | return 0; | |
4897d1dc | 104 | |
65d34266 YW |
105 | sd_bus_message_unref(c->locale_cache); |
106 | c->locale_cache = sd_bus_message_ref(m); | |
107 | } | |
df4fd2c7 YW |
108 | |
109 | r = stat("/etc/locale.conf", &st); | |
110 | if (r < 0 && errno != ENOENT) | |
111 | return -errno; | |
112 | ||
113 | if (r >= 0) { | |
114 | usec_t t; | |
115 | ||
116 | /* If mtime is not changed, then we do not need to re-read the file. */ | |
117 | t = timespec_load(&st.st_mtim); | |
118 | if (c->locale_mtime != USEC_INFINITY && t == c->locale_mtime) | |
119 | return 0; | |
4897d1dc | 120 | |
df4fd2c7 YW |
121 | c->locale_mtime = t; |
122 | context_free_locale(c); | |
123 | ||
aa8fbc74 | 124 | r = parse_env_file(NULL, "/etc/locale.conf", |
df4fd2c7 YW |
125 | "LANG", &c->locale[VARIABLE_LANG], |
126 | "LANGUAGE", &c->locale[VARIABLE_LANGUAGE], | |
127 | "LC_CTYPE", &c->locale[VARIABLE_LC_CTYPE], | |
128 | "LC_NUMERIC", &c->locale[VARIABLE_LC_NUMERIC], | |
129 | "LC_TIME", &c->locale[VARIABLE_LC_TIME], | |
130 | "LC_COLLATE", &c->locale[VARIABLE_LC_COLLATE], | |
131 | "LC_MONETARY", &c->locale[VARIABLE_LC_MONETARY], | |
132 | "LC_MESSAGES", &c->locale[VARIABLE_LC_MESSAGES], | |
133 | "LC_PAPER", &c->locale[VARIABLE_LC_PAPER], | |
134 | "LC_NAME", &c->locale[VARIABLE_LC_NAME], | |
135 | "LC_ADDRESS", &c->locale[VARIABLE_LC_ADDRESS], | |
136 | "LC_TELEPHONE", &c->locale[VARIABLE_LC_TELEPHONE], | |
137 | "LC_MEASUREMENT", &c->locale[VARIABLE_LC_MEASUREMENT], | |
13df9c39 | 138 | "LC_IDENTIFICATION", &c->locale[VARIABLE_LC_IDENTIFICATION]); |
df4fd2c7 YW |
139 | if (r < 0) |
140 | return r; | |
141 | } else { | |
4897d1dc ZJS |
142 | int p; |
143 | ||
df4fd2c7 YW |
144 | c->locale_mtime = USEC_INFINITY; |
145 | context_free_locale(c); | |
146 | ||
4897d1dc ZJS |
147 | /* Fill in what we got passed from systemd. */ |
148 | for (p = 0; p < _VARIABLE_LC_MAX; p++) { | |
149 | const char *name; | |
150 | ||
151 | name = locale_variable_to_string(p); | |
152 | assert(name); | |
153 | ||
154 | r = free_and_strdup(&c->locale[p], empty_to_null(getenv(name))); | |
155 | if (r < 0) | |
156 | return r; | |
157 | } | |
4897d1dc ZJS |
158 | } |
159 | ||
df4fd2c7 YW |
160 | locale_simplify(c->locale); |
161 | return 0; | |
4897d1dc ZJS |
162 | } |
163 | ||
df4fd2c7 YW |
164 | int vconsole_read_data(Context *c, sd_bus_message *m) { |
165 | struct stat st; | |
166 | usec_t t; | |
4897d1dc ZJS |
167 | int r; |
168 | ||
df4fd2c7 | 169 | /* Do not try to re-read the file within single bus operation. */ |
65d34266 YW |
170 | if (m) { |
171 | if (m == c->vc_cache) | |
172 | return 0; | |
df4fd2c7 | 173 | |
65d34266 YW |
174 | sd_bus_message_unref(c->vc_cache); |
175 | c->vc_cache = sd_bus_message_ref(m); | |
176 | } | |
df4fd2c7 YW |
177 | |
178 | if (stat("/etc/vconsole.conf", &st) < 0) { | |
179 | if (errno != ENOENT) | |
180 | return -errno; | |
181 | ||
182 | c->vc_mtime = USEC_INFINITY; | |
183 | context_free_vconsole(c); | |
184 | return 0; | |
185 | } | |
186 | ||
187 | /* If mtime is not changed, then we do not need to re-read */ | |
188 | t = timespec_load(&st.st_mtim); | |
189 | if (c->vc_mtime != USEC_INFINITY && t == c->vc_mtime) | |
190 | return 0; | |
191 | ||
192 | c->vc_mtime = t; | |
4897d1dc ZJS |
193 | context_free_vconsole(c); |
194 | ||
aa8fbc74 | 195 | r = parse_env_file(NULL, "/etc/vconsole.conf", |
4897d1dc | 196 | "KEYMAP", &c->vc_keymap, |
13df9c39 | 197 | "KEYMAP_TOGGLE", &c->vc_keymap_toggle); |
df4fd2c7 | 198 | if (r < 0) |
4897d1dc ZJS |
199 | return r; |
200 | ||
201 | return 0; | |
202 | } | |
203 | ||
df4fd2c7 YW |
204 | int x11_read_data(Context *c, sd_bus_message *m) { |
205 | _cleanup_fclose_ FILE *f = NULL; | |
4897d1dc | 206 | bool in_section = false; |
df4fd2c7 YW |
207 | struct stat st; |
208 | usec_t t; | |
4897d1dc ZJS |
209 | int r; |
210 | ||
df4fd2c7 | 211 | /* Do not try to re-read the file within single bus operation. */ |
65d34266 YW |
212 | if (m) { |
213 | if (m == c->x11_cache) | |
214 | return 0; | |
df4fd2c7 | 215 | |
65d34266 YW |
216 | sd_bus_message_unref(c->x11_cache); |
217 | c->x11_cache = sd_bus_message_ref(m); | |
218 | } | |
df4fd2c7 YW |
219 | |
220 | if (stat("/etc/X11/xorg.conf.d/00-keyboard.conf", &st) < 0) { | |
221 | if (errno != ENOENT) | |
222 | return -errno; | |
223 | ||
224 | c->x11_mtime = USEC_INFINITY; | |
225 | context_free_x11(c); | |
226 | return 0; | |
227 | } | |
228 | ||
229 | /* If mtime is not changed, then we do not need to re-read */ | |
230 | t = timespec_load(&st.st_mtim); | |
231 | if (c->x11_mtime != USEC_INFINITY && t == c->x11_mtime) | |
232 | return 0; | |
233 | ||
234 | c->x11_mtime = t; | |
4897d1dc ZJS |
235 | context_free_x11(c); |
236 | ||
237 | f = fopen("/etc/X11/xorg.conf.d/00-keyboard.conf", "re"); | |
238 | if (!f) | |
df4fd2c7 | 239 | return -errno; |
4897d1dc | 240 | |
1d47b569 LP |
241 | for (;;) { |
242 | _cleanup_free_ char *line = NULL; | |
4897d1dc ZJS |
243 | char *l; |
244 | ||
1d47b569 LP |
245 | r = read_line(f, LONG_LINE_MAX, &line); |
246 | if (r < 0) | |
247 | return r; | |
248 | if (r == 0) | |
249 | break; | |
4897d1dc | 250 | |
1d47b569 | 251 | l = strstrip(line); |
4c701096 | 252 | if (IN_SET(l[0], 0, '#')) |
4897d1dc ZJS |
253 | continue; |
254 | ||
255 | if (in_section && first_word(l, "Option")) { | |
256 | _cleanup_strv_free_ char **a = NULL; | |
257 | ||
258 | r = strv_split_extract(&a, l, WHITESPACE, EXTRACT_QUOTES); | |
259 | if (r < 0) | |
260 | return r; | |
261 | ||
262 | if (strv_length(a) == 3) { | |
263 | char **p = NULL; | |
264 | ||
265 | if (streq(a[1], "XkbLayout")) | |
266 | p = &c->x11_layout; | |
267 | else if (streq(a[1], "XkbModel")) | |
268 | p = &c->x11_model; | |
269 | else if (streq(a[1], "XkbVariant")) | |
270 | p = &c->x11_variant; | |
271 | else if (streq(a[1], "XkbOptions")) | |
272 | p = &c->x11_options; | |
273 | ||
274 | if (p) { | |
f9ecfd3b | 275 | free_and_replace(*p, a[2]); |
4897d1dc ZJS |
276 | } |
277 | } | |
278 | ||
279 | } else if (!in_section && first_word(l, "Section")) { | |
280 | _cleanup_strv_free_ char **a = NULL; | |
281 | ||
282 | r = strv_split_extract(&a, l, WHITESPACE, EXTRACT_QUOTES); | |
283 | if (r < 0) | |
284 | return -ENOMEM; | |
285 | ||
286 | if (strv_length(a) == 2 && streq(a[1], "InputClass")) | |
287 | in_section = true; | |
288 | ||
289 | } else if (in_section && first_word(l, "EndSection")) | |
290 | in_section = false; | |
291 | } | |
292 | ||
293 | return 0; | |
294 | } | |
295 | ||
4897d1dc | 296 | int locale_write_data(Context *c, char ***settings) { |
4897d1dc | 297 | _cleanup_strv_free_ char **l = NULL; |
df4fd2c7 YW |
298 | struct stat st; |
299 | int r, p; | |
4897d1dc ZJS |
300 | |
301 | /* Set values will be returned as strv in *settings on success. */ | |
302 | ||
4897d1dc ZJS |
303 | for (p = 0; p < _VARIABLE_LC_MAX; p++) { |
304 | _cleanup_free_ char *t = NULL; | |
305 | char **u; | |
306 | const char *name; | |
307 | ||
308 | name = locale_variable_to_string(p); | |
309 | assert(name); | |
310 | ||
df4fd2c7 | 311 | if (isempty(c->locale[p])) |
4897d1dc | 312 | continue; |
4897d1dc ZJS |
313 | |
314 | if (asprintf(&t, "%s=%s", name, c->locale[p]) < 0) | |
315 | return -ENOMEM; | |
316 | ||
317 | u = strv_env_set(l, t); | |
318 | if (!u) | |
319 | return -ENOMEM; | |
320 | ||
130d3d22 | 321 | strv_free_and_replace(l, u); |
4897d1dc ZJS |
322 | } |
323 | ||
324 | if (strv_isempty(l)) { | |
325 | if (unlink("/etc/locale.conf") < 0) | |
326 | return errno == ENOENT ? 0 : -errno; | |
327 | ||
df4fd2c7 | 328 | c->locale_mtime = USEC_INFINITY; |
4897d1dc ZJS |
329 | return 0; |
330 | } | |
331 | ||
332 | r = write_env_file_label("/etc/locale.conf", l); | |
333 | if (r < 0) | |
334 | return r; | |
335 | ||
ae2a15bc | 336 | *settings = TAKE_PTR(l); |
df4fd2c7 YW |
337 | |
338 | if (stat("/etc/locale.conf", &st) >= 0) | |
339 | c->locale_mtime = timespec_load(&st.st_mtim); | |
340 | ||
4897d1dc ZJS |
341 | return 0; |
342 | } | |
343 | ||
344 | int vconsole_write_data(Context *c) { | |
4897d1dc | 345 | _cleanup_strv_free_ char **l = NULL; |
df4fd2c7 YW |
346 | struct stat st; |
347 | int r; | |
4897d1dc | 348 | |
aa8fbc74 | 349 | r = load_env_file(NULL, "/etc/vconsole.conf", &l); |
4897d1dc ZJS |
350 | if (r < 0 && r != -ENOENT) |
351 | return r; | |
352 | ||
353 | if (isempty(c->vc_keymap)) | |
354 | l = strv_env_unset(l, "KEYMAP"); | |
355 | else { | |
356 | _cleanup_free_ char *s = NULL; | |
357 | char **u; | |
358 | ||
359 | s = strappend("KEYMAP=", c->vc_keymap); | |
360 | if (!s) | |
361 | return -ENOMEM; | |
362 | ||
363 | u = strv_env_set(l, s); | |
364 | if (!u) | |
365 | return -ENOMEM; | |
366 | ||
130d3d22 | 367 | strv_free_and_replace(l, u); |
4897d1dc ZJS |
368 | } |
369 | ||
370 | if (isempty(c->vc_keymap_toggle)) | |
371 | l = strv_env_unset(l, "KEYMAP_TOGGLE"); | |
372 | else { | |
373 | _cleanup_free_ char *s = NULL; | |
374 | char **u; | |
375 | ||
376 | s = strappend("KEYMAP_TOGGLE=", c->vc_keymap_toggle); | |
377 | if (!s) | |
378 | return -ENOMEM; | |
379 | ||
380 | u = strv_env_set(l, s); | |
381 | if (!u) | |
382 | return -ENOMEM; | |
383 | ||
130d3d22 | 384 | strv_free_and_replace(l, u); |
4897d1dc ZJS |
385 | } |
386 | ||
387 | if (strv_isempty(l)) { | |
388 | if (unlink("/etc/vconsole.conf") < 0) | |
389 | return errno == ENOENT ? 0 : -errno; | |
390 | ||
df4fd2c7 | 391 | c->vc_mtime = USEC_INFINITY; |
4897d1dc ZJS |
392 | return 0; |
393 | } | |
394 | ||
df4fd2c7 YW |
395 | r = write_env_file_label("/etc/vconsole.conf", l); |
396 | if (r < 0) | |
397 | return r; | |
398 | ||
399 | if (stat("/etc/vconsole.conf", &st) >= 0) | |
400 | c->vc_mtime = timespec_load(&st.st_mtim); | |
401 | ||
402 | return 0; | |
4897d1dc ZJS |
403 | } |
404 | ||
405 | int x11_write_data(Context *c) { | |
406 | _cleanup_fclose_ FILE *f = NULL; | |
407 | _cleanup_free_ char *temp_path = NULL; | |
df4fd2c7 | 408 | struct stat st; |
4897d1dc ZJS |
409 | int r; |
410 | ||
411 | if (isempty(c->x11_layout) && | |
412 | isempty(c->x11_model) && | |
413 | isempty(c->x11_variant) && | |
414 | isempty(c->x11_options)) { | |
415 | ||
416 | if (unlink("/etc/X11/xorg.conf.d/00-keyboard.conf") < 0) | |
417 | return errno == ENOENT ? 0 : -errno; | |
418 | ||
df4fd2c7 | 419 | c->vc_mtime = USEC_INFINITY; |
4897d1dc ZJS |
420 | return 0; |
421 | } | |
422 | ||
423 | mkdir_p_label("/etc/X11/xorg.conf.d", 0755); | |
424 | ||
425 | r = fopen_temporary("/etc/X11/xorg.conf.d/00-keyboard.conf", &f, &temp_path); | |
426 | if (r < 0) | |
427 | return r; | |
428 | ||
0d536673 LP |
429 | (void) __fsetlocking(f, FSETLOCKING_BYCALLER); |
430 | (void) fchmod(fileno(f), 0644); | |
4897d1dc | 431 | |
0d536673 LP |
432 | fputs("# Written by systemd-localed(8), read by systemd-localed and Xorg. It's\n" |
433 | "# probably wise not to edit this file manually. Use localectl(1) to\n" | |
434 | "# instruct systemd-localed to update it.\n" | |
435 | "Section \"InputClass\"\n" | |
436 | " Identifier \"system-keyboard\"\n" | |
437 | " MatchIsKeyboard \"on\"\n", f); | |
4897d1dc ZJS |
438 | |
439 | if (!isempty(c->x11_layout)) | |
440 | fprintf(f, " Option \"XkbLayout\" \"%s\"\n", c->x11_layout); | |
441 | ||
442 | if (!isempty(c->x11_model)) | |
443 | fprintf(f, " Option \"XkbModel\" \"%s\"\n", c->x11_model); | |
444 | ||
445 | if (!isempty(c->x11_variant)) | |
446 | fprintf(f, " Option \"XkbVariant\" \"%s\"\n", c->x11_variant); | |
447 | ||
448 | if (!isempty(c->x11_options)) | |
449 | fprintf(f, " Option \"XkbOptions\" \"%s\"\n", c->x11_options); | |
450 | ||
0d536673 | 451 | fputs("EndSection\n", f); |
4897d1dc | 452 | |
0675e94a | 453 | r = fflush_sync_and_check(f); |
4897d1dc ZJS |
454 | if (r < 0) |
455 | goto fail; | |
456 | ||
457 | if (rename(temp_path, "/etc/X11/xorg.conf.d/00-keyboard.conf") < 0) { | |
458 | r = -errno; | |
459 | goto fail; | |
460 | } | |
461 | ||
df4fd2c7 YW |
462 | if (stat("/etc/X11/xorg.conf.d/00-keyboard.conf", &st) >= 0) |
463 | c->x11_mtime = timespec_load(&st.st_mtim); | |
464 | ||
4897d1dc ZJS |
465 | return 0; |
466 | ||
467 | fail: | |
4897d1dc ZJS |
468 | if (temp_path) |
469 | (void) unlink(temp_path); | |
470 | ||
471 | return r; | |
472 | } | |
473 | ||
474 | static int read_next_mapping(const char* filename, | |
475 | unsigned min_fields, unsigned max_fields, | |
476 | FILE *f, unsigned *n, char ***a) { | |
477 | assert(f); | |
478 | assert(n); | |
479 | assert(a); | |
480 | ||
481 | for (;;) { | |
1d47b569 LP |
482 | _cleanup_free_ char *line = NULL; |
483 | size_t length; | |
4897d1dc ZJS |
484 | char *l, **b; |
485 | int r; | |
4897d1dc | 486 | |
1d47b569 LP |
487 | r = read_line(f, LONG_LINE_MAX, &line); |
488 | if (r < 0) | |
489 | return r; | |
490 | if (r == 0) | |
491 | break; | |
4897d1dc ZJS |
492 | |
493 | (*n)++; | |
494 | ||
495 | l = strstrip(line); | |
4c701096 | 496 | if (IN_SET(l[0], 0, '#')) |
4897d1dc ZJS |
497 | continue; |
498 | ||
499 | r = strv_split_extract(&b, l, WHITESPACE, EXTRACT_QUOTES); | |
500 | if (r < 0) | |
501 | return r; | |
502 | ||
503 | length = strv_length(b); | |
504 | if (length < min_fields || length > max_fields) { | |
505 | log_error("Invalid line %s:%u, ignoring.", filename, *n); | |
506 | strv_free(b); | |
507 | continue; | |
508 | ||
509 | } | |
510 | ||
511 | *a = b; | |
512 | return 1; | |
513 | } | |
1d47b569 LP |
514 | |
515 | return 0; | |
4897d1dc ZJS |
516 | } |
517 | ||
518 | int vconsole_convert_to_x11(Context *c) { | |
cabffaf8 | 519 | const char *map; |
6f3287b3 | 520 | int modified = -1; |
4897d1dc | 521 | |
cabffaf8 ZJS |
522 | map = systemd_kbd_model_map(); |
523 | ||
4897d1dc | 524 | if (isempty(c->vc_keymap)) { |
4897d1dc ZJS |
525 | modified = |
526 | !isempty(c->x11_layout) || | |
527 | !isempty(c->x11_model) || | |
528 | !isempty(c->x11_variant) || | |
529 | !isempty(c->x11_options); | |
530 | ||
531 | context_free_x11(c); | |
532 | } else { | |
533 | _cleanup_fclose_ FILE *f = NULL; | |
534 | unsigned n = 0; | |
535 | ||
cabffaf8 | 536 | f = fopen(map, "re"); |
4897d1dc ZJS |
537 | if (!f) |
538 | return -errno; | |
539 | ||
540 | for (;;) { | |
541 | _cleanup_strv_free_ char **a = NULL; | |
542 | int r; | |
543 | ||
cabffaf8 | 544 | r = read_next_mapping(map, 5, UINT_MAX, f, &n, &a); |
4897d1dc ZJS |
545 | if (r < 0) |
546 | return r; | |
547 | if (r == 0) | |
548 | break; | |
549 | ||
550 | if (!streq(c->vc_keymap, a[0])) | |
551 | continue; | |
552 | ||
553 | if (!streq_ptr(c->x11_layout, strnulldash(a[1])) || | |
554 | !streq_ptr(c->x11_model, strnulldash(a[2])) || | |
555 | !streq_ptr(c->x11_variant, strnulldash(a[3])) || | |
556 | !streq_ptr(c->x11_options, strnulldash(a[4]))) { | |
557 | ||
558 | if (free_and_strdup(&c->x11_layout, strnulldash(a[1])) < 0 || | |
559 | free_and_strdup(&c->x11_model, strnulldash(a[2])) < 0 || | |
560 | free_and_strdup(&c->x11_variant, strnulldash(a[3])) < 0 || | |
561 | free_and_strdup(&c->x11_options, strnulldash(a[4])) < 0) | |
562 | return -ENOMEM; | |
563 | ||
564 | modified = true; | |
565 | } | |
566 | ||
567 | break; | |
568 | } | |
569 | } | |
570 | ||
6f3287b3 | 571 | if (modified > 0) |
4897d1dc ZJS |
572 | log_info("Changing X11 keyboard layout to '%s' model '%s' variant '%s' options '%s'", |
573 | strempty(c->x11_layout), | |
574 | strempty(c->x11_model), | |
575 | strempty(c->x11_variant), | |
576 | strempty(c->x11_options)); | |
6f3287b3 ZJS |
577 | else if (modified < 0) |
578 | log_notice("X11 keyboard layout was not modified: no conversion found for \"%s\".", | |
579 | c->vc_keymap); | |
4897d1dc | 580 | else |
6f3287b3 | 581 | log_debug("X11 keyboard layout did not need to be modified."); |
4897d1dc | 582 | |
6f3287b3 | 583 | return modified > 0; |
4897d1dc ZJS |
584 | } |
585 | ||
586 | int find_converted_keymap(const char *x11_layout, const char *x11_variant, char **new_keymap) { | |
587 | const char *dir; | |
588 | _cleanup_free_ char *n; | |
589 | ||
590 | if (x11_variant) | |
605405c6 | 591 | n = strjoin(x11_layout, "-", x11_variant); |
4897d1dc ZJS |
592 | else |
593 | n = strdup(x11_layout); | |
594 | if (!n) | |
595 | return -ENOMEM; | |
596 | ||
597 | NULSTR_FOREACH(dir, KBD_KEYMAP_DIRS) { | |
598 | _cleanup_free_ char *p = NULL, *pz = NULL; | |
599 | bool uncompressed; | |
600 | ||
605405c6 ZJS |
601 | p = strjoin(dir, "xkb/", n, ".map"); |
602 | pz = strjoin(dir, "xkb/", n, ".map.gz"); | |
4897d1dc ZJS |
603 | if (!p || !pz) |
604 | return -ENOMEM; | |
605 | ||
606 | uncompressed = access(p, F_OK) == 0; | |
607 | if (uncompressed || access(pz, F_OK) == 0) { | |
608 | log_debug("Found converted keymap %s at %s", | |
609 | n, uncompressed ? p : pz); | |
610 | ||
ae2a15bc | 611 | *new_keymap = TAKE_PTR(n); |
4897d1dc ZJS |
612 | return 1; |
613 | } | |
614 | } | |
615 | ||
616 | return 0; | |
617 | } | |
618 | ||
6a6e9c03 | 619 | int find_legacy_keymap(Context *c, char **ret) { |
cabffaf8 ZJS |
620 | const char *map; |
621 | _cleanup_fclose_ FILE *f = NULL; | |
6a6e9c03 | 622 | _cleanup_free_ char *new_keymap = NULL; |
4897d1dc ZJS |
623 | unsigned n = 0; |
624 | unsigned best_matching = 0; | |
625 | int r; | |
626 | ||
5ad327dd ZJS |
627 | assert(!isempty(c->x11_layout)); |
628 | ||
cabffaf8 ZJS |
629 | map = systemd_kbd_model_map(); |
630 | ||
631 | f = fopen(map, "re"); | |
4897d1dc ZJS |
632 | if (!f) |
633 | return -errno; | |
634 | ||
635 | for (;;) { | |
636 | _cleanup_strv_free_ char **a = NULL; | |
637 | unsigned matching = 0; | |
638 | ||
cabffaf8 | 639 | r = read_next_mapping(map, 5, UINT_MAX, f, &n, &a); |
4897d1dc ZJS |
640 | if (r < 0) |
641 | return r; | |
642 | if (r == 0) | |
643 | break; | |
644 | ||
645 | /* Determine how well matching this entry is */ | |
5ad327dd | 646 | if (streq(c->x11_layout, a[1])) |
4897d1dc ZJS |
647 | /* If we got an exact match, this is best */ |
648 | matching = 10; | |
649 | else { | |
650 | /* We have multiple X layouts, look for an | |
651 | * entry that matches our key with everything | |
652 | * but the first layout stripped off. */ | |
653 | if (startswith_comma(c->x11_layout, a[1])) | |
654 | matching = 5; | |
655 | else { | |
656 | char *x; | |
657 | ||
658 | /* If that didn't work, strip off the | |
659 | * other layouts from the entry, too */ | |
660 | x = strndupa(a[1], strcspn(a[1], ",")); | |
661 | if (startswith_comma(c->x11_layout, x)) | |
662 | matching = 1; | |
663 | } | |
664 | } | |
665 | ||
666 | if (matching > 0) { | |
667 | if (isempty(c->x11_model) || streq_ptr(c->x11_model, a[2])) { | |
668 | matching++; | |
669 | ||
670 | if (streq_ptr(c->x11_variant, a[3])) { | |
671 | matching++; | |
672 | ||
673 | if (streq_ptr(c->x11_options, a[4])) | |
674 | matching++; | |
675 | } | |
676 | } | |
677 | } | |
678 | ||
679 | /* The best matching entry so far, then let's save that */ | |
680 | if (matching >= MAX(best_matching, 1u)) { | |
681 | log_debug("Found legacy keymap %s with score %u", | |
682 | a[0], matching); | |
683 | ||
684 | if (matching > best_matching) { | |
685 | best_matching = matching; | |
686 | ||
6a6e9c03 | 687 | r = free_and_strdup(&new_keymap, a[0]); |
4897d1dc ZJS |
688 | if (r < 0) |
689 | return r; | |
690 | } | |
691 | } | |
692 | } | |
693 | ||
694 | if (best_matching < 10 && c->x11_layout) { | |
695 | /* The best match is only the first part of the X11 | |
696 | * keymap. Check if we have a converted map which | |
697 | * matches just the first layout. | |
698 | */ | |
699 | char *l, *v = NULL, *converted; | |
700 | ||
701 | l = strndupa(c->x11_layout, strcspn(c->x11_layout, ",")); | |
702 | if (c->x11_variant) | |
703 | v = strndupa(c->x11_variant, strcspn(c->x11_variant, ",")); | |
704 | r = find_converted_keymap(l, v, &converted); | |
705 | if (r < 0) | |
706 | return r; | |
6a6e9c03 ZJS |
707 | if (r > 0) |
708 | free_and_replace(new_keymap, converted); | |
4897d1dc ZJS |
709 | } |
710 | ||
6a6e9c03 ZJS |
711 | *ret = TAKE_PTR(new_keymap); |
712 | return (bool) *ret; | |
4897d1dc ZJS |
713 | } |
714 | ||
715 | int find_language_fallback(const char *lang, char **language) { | |
cabffaf8 | 716 | const char *map; |
4897d1dc ZJS |
717 | _cleanup_fclose_ FILE *f = NULL; |
718 | unsigned n = 0; | |
719 | ||
aa63b56f | 720 | assert(lang); |
4897d1dc ZJS |
721 | assert(language); |
722 | ||
cabffaf8 ZJS |
723 | map = systemd_language_fallback_map(); |
724 | ||
725 | f = fopen(map, "re"); | |
4897d1dc ZJS |
726 | if (!f) |
727 | return -errno; | |
728 | ||
729 | for (;;) { | |
730 | _cleanup_strv_free_ char **a = NULL; | |
731 | int r; | |
732 | ||
cabffaf8 | 733 | r = read_next_mapping(map, 2, 2, f, &n, &a); |
4897d1dc ZJS |
734 | if (r <= 0) |
735 | return r; | |
736 | ||
737 | if (streq(lang, a[0])) { | |
738 | assert(strv_length(a) == 2); | |
1cc6c93a | 739 | *language = TAKE_PTR(a[1]); |
4897d1dc ZJS |
740 | return 1; |
741 | } | |
742 | } | |
743 | ||
744 | assert_not_reached("should not be here"); | |
745 | } | |
746 | ||
747 | int x11_convert_to_vconsole(Context *c) { | |
748 | bool modified = false; | |
749 | ||
750 | if (isempty(c->x11_layout)) { | |
4897d1dc ZJS |
751 | modified = |
752 | !isempty(c->vc_keymap) || | |
753 | !isempty(c->vc_keymap_toggle); | |
754 | ||
aa63b56f | 755 | context_free_vconsole(c); |
4897d1dc | 756 | } else { |
6a837b03 | 757 | _cleanup_free_ char *new_keymap = NULL; |
4897d1dc ZJS |
758 | int r; |
759 | ||
760 | r = find_converted_keymap(c->x11_layout, c->x11_variant, &new_keymap); | |
761 | if (r < 0) | |
762 | return r; | |
763 | else if (r == 0) { | |
764 | r = find_legacy_keymap(c, &new_keymap); | |
765 | if (r < 0) | |
766 | return r; | |
767 | } | |
5ad327dd ZJS |
768 | if (r == 0) |
769 | /* We search for layout-variant match first, but then we also look | |
770 | * for anything which matches just the layout. So it's accurate to say | |
771 | * that we couldn't find anything which matches the layout. */ | |
772 | log_notice("No conversion to virtual console map found for \"%s\".", | |
773 | c->x11_layout); | |
4897d1dc ZJS |
774 | |
775 | if (!streq_ptr(c->vc_keymap, new_keymap)) { | |
6a837b03 | 776 | free_and_replace(c->vc_keymap, new_keymap); |
4897d1dc ZJS |
777 | c->vc_keymap_toggle = mfree(c->vc_keymap_toggle); |
778 | modified = true; | |
6a837b03 | 779 | } |
4897d1dc ZJS |
780 | } |
781 | ||
782 | if (modified) | |
783 | log_info("Changing virtual console keymap to '%s' toggle '%s'", | |
784 | strempty(c->vc_keymap), strempty(c->vc_keymap_toggle)); | |
785 | else | |
786 | log_debug("Virtual console keymap was not modified."); | |
787 | ||
788 | return modified; | |
789 | } |