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