1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 Copyright 2013 Kay Sievers
14 #include "fileio-label.h"
16 #include "keymap-util.h"
17 #include "locale-util.h"
20 #include "string-util.h"
23 static bool startswith_comma(const char *s
, const char *prefix
) {
24 s
= startswith(s
, prefix
);
28 return IN_SET(*s
, ',', '\0');
31 static const char* strnulldash(const char *s
) {
32 return isempty(s
) || streq(s
, "-") ? NULL
: s
;
35 static const char* systemd_kbd_model_map(void) {
38 s
= getenv("SYSTEMD_KBD_MODEL_MAP");
42 return SYSTEMD_KBD_MODEL_MAP
;
45 static const char* systemd_language_fallback_map(void) {
48 s
= getenv("SYSTEMD_LANGUAGE_FALLBACK_MAP");
52 return SYSTEMD_LANGUAGE_FALLBACK_MAP
;
55 static void context_free_x11(Context
*c
) {
56 c
->x11_layout
= mfree(c
->x11_layout
);
57 c
->x11_options
= mfree(c
->x11_options
);
58 c
->x11_model
= mfree(c
->x11_model
);
59 c
->x11_variant
= mfree(c
->x11_variant
);
62 static void context_free_vconsole(Context
*c
) {
63 c
->vc_keymap
= mfree(c
->vc_keymap
);
64 c
->vc_keymap_toggle
= mfree(c
->vc_keymap_toggle
);
67 static void context_free_locale(Context
*c
) {
70 for (p
= 0; p
< _VARIABLE_LC_MAX
; p
++)
71 c
->locale
[p
] = mfree(c
->locale
[p
]);
74 void context_free(Context
*c
) {
75 context_free_locale(c
);
77 context_free_vconsole(c
);
80 void locale_simplify(char *locale
[_VARIABLE_LC_MAX
]) {
83 for (p
= VARIABLE_LANG
+1; p
< _VARIABLE_LC_MAX
; p
++)
84 if (isempty(locale
[p
]) || streq_ptr(locale
[VARIABLE_LANG
], locale
[p
]))
85 locale
[p
] = mfree(locale
[p
]);
88 int locale_read_data(Context
*c
, sd_bus_message
*m
) {
92 /* Do not try to re-read the file within single bus operation. */
93 if (m
&& m
== c
->locale_cache
)
96 /* To suppress multiple call of stat(), store the message to cache here. */
99 r
= stat("/etc/locale.conf", &st
);
100 if (r
< 0 && errno
!= ENOENT
)
106 /* If mtime is not changed, then we do not need to re-read the file. */
107 t
= timespec_load(&st
.st_mtim
);
108 if (c
->locale_mtime
!= USEC_INFINITY
&& t
== c
->locale_mtime
)
112 context_free_locale(c
);
114 r
= parse_env_file(NULL
, "/etc/locale.conf", NEWLINE
,
115 "LANG", &c
->locale
[VARIABLE_LANG
],
116 "LANGUAGE", &c
->locale
[VARIABLE_LANGUAGE
],
117 "LC_CTYPE", &c
->locale
[VARIABLE_LC_CTYPE
],
118 "LC_NUMERIC", &c
->locale
[VARIABLE_LC_NUMERIC
],
119 "LC_TIME", &c
->locale
[VARIABLE_LC_TIME
],
120 "LC_COLLATE", &c
->locale
[VARIABLE_LC_COLLATE
],
121 "LC_MONETARY", &c
->locale
[VARIABLE_LC_MONETARY
],
122 "LC_MESSAGES", &c
->locale
[VARIABLE_LC_MESSAGES
],
123 "LC_PAPER", &c
->locale
[VARIABLE_LC_PAPER
],
124 "LC_NAME", &c
->locale
[VARIABLE_LC_NAME
],
125 "LC_ADDRESS", &c
->locale
[VARIABLE_LC_ADDRESS
],
126 "LC_TELEPHONE", &c
->locale
[VARIABLE_LC_TELEPHONE
],
127 "LC_MEASUREMENT", &c
->locale
[VARIABLE_LC_MEASUREMENT
],
128 "LC_IDENTIFICATION", &c
->locale
[VARIABLE_LC_IDENTIFICATION
],
135 c
->locale_mtime
= USEC_INFINITY
;
136 context_free_locale(c
);
138 /* Fill in what we got passed from systemd. */
139 for (p
= 0; p
< _VARIABLE_LC_MAX
; p
++) {
142 name
= locale_variable_to_string(p
);
145 r
= free_and_strdup(&c
->locale
[p
], empty_to_null(getenv(name
)));
151 locale_simplify(c
->locale
);
155 int vconsole_read_data(Context
*c
, sd_bus_message
*m
) {
160 /* Do not try to re-read the file within single bus operation. */
161 if (m
&& m
== c
->vc_cache
)
164 /* To suppress multiple call of stat(), store the message to cache here. */
167 if (stat("/etc/vconsole.conf", &st
) < 0) {
171 c
->vc_mtime
= USEC_INFINITY
;
172 context_free_vconsole(c
);
176 /* If mtime is not changed, then we do not need to re-read */
177 t
= timespec_load(&st
.st_mtim
);
178 if (c
->vc_mtime
!= USEC_INFINITY
&& t
== c
->vc_mtime
)
182 context_free_vconsole(c
);
184 r
= parse_env_file(NULL
, "/etc/vconsole.conf", NEWLINE
,
185 "KEYMAP", &c
->vc_keymap
,
186 "KEYMAP_TOGGLE", &c
->vc_keymap_toggle
,
194 int x11_read_data(Context
*c
, sd_bus_message
*m
) {
195 _cleanup_fclose_
FILE *f
= NULL
;
196 bool in_section
= false;
202 /* Do not try to re-read the file within single bus operation. */
203 if (m
&& m
== c
->x11_cache
)
206 /* To suppress multiple call of stat(), store the message to cache here. */
209 if (stat("/etc/X11/xorg.conf.d/00-keyboard.conf", &st
) < 0) {
213 c
->x11_mtime
= USEC_INFINITY
;
218 /* If mtime is not changed, then we do not need to re-read */
219 t
= timespec_load(&st
.st_mtim
);
220 if (c
->x11_mtime
!= USEC_INFINITY
&& t
== c
->x11_mtime
)
226 f
= fopen("/etc/X11/xorg.conf.d/00-keyboard.conf", "re");
230 while (fgets(line
, sizeof(line
), f
)) {
236 if (IN_SET(l
[0], 0, '#'))
239 if (in_section
&& first_word(l
, "Option")) {
240 _cleanup_strv_free_
char **a
= NULL
;
242 r
= strv_split_extract(&a
, l
, WHITESPACE
, EXTRACT_QUOTES
);
246 if (strv_length(a
) == 3) {
249 if (streq(a
[1], "XkbLayout"))
251 else if (streq(a
[1], "XkbModel"))
253 else if (streq(a
[1], "XkbVariant"))
255 else if (streq(a
[1], "XkbOptions"))
259 free_and_replace(*p
, a
[2]);
263 } else if (!in_section
&& first_word(l
, "Section")) {
264 _cleanup_strv_free_
char **a
= NULL
;
266 r
= strv_split_extract(&a
, l
, WHITESPACE
, EXTRACT_QUOTES
);
270 if (strv_length(a
) == 2 && streq(a
[1], "InputClass"))
273 } else if (in_section
&& first_word(l
, "EndSection"))
280 int locale_write_data(Context
*c
, char ***settings
) {
281 _cleanup_strv_free_
char **l
= NULL
;
285 /* Set values will be returned as strv in *settings on success. */
287 for (p
= 0; p
< _VARIABLE_LC_MAX
; p
++) {
288 _cleanup_free_
char *t
= NULL
;
292 name
= locale_variable_to_string(p
);
295 if (isempty(c
->locale
[p
]))
298 if (asprintf(&t
, "%s=%s", name
, c
->locale
[p
]) < 0)
301 u
= strv_env_set(l
, t
);
305 strv_free_and_replace(l
, u
);
308 if (strv_isempty(l
)) {
309 if (unlink("/etc/locale.conf") < 0)
310 return errno
== ENOENT
? 0 : -errno
;
312 c
->locale_mtime
= USEC_INFINITY
;
316 r
= write_env_file_label("/etc/locale.conf", l
);
320 *settings
= TAKE_PTR(l
);
322 if (stat("/etc/locale.conf", &st
) >= 0)
323 c
->locale_mtime
= timespec_load(&st
.st_mtim
);
328 int vconsole_write_data(Context
*c
) {
329 _cleanup_strv_free_
char **l
= NULL
;
333 r
= load_env_file(NULL
, "/etc/vconsole.conf", NULL
, &l
);
334 if (r
< 0 && r
!= -ENOENT
)
337 if (isempty(c
->vc_keymap
))
338 l
= strv_env_unset(l
, "KEYMAP");
340 _cleanup_free_
char *s
= NULL
;
343 s
= strappend("KEYMAP=", c
->vc_keymap
);
347 u
= strv_env_set(l
, s
);
351 strv_free_and_replace(l
, u
);
354 if (isempty(c
->vc_keymap_toggle
))
355 l
= strv_env_unset(l
, "KEYMAP_TOGGLE");
357 _cleanup_free_
char *s
= NULL
;
360 s
= strappend("KEYMAP_TOGGLE=", c
->vc_keymap_toggle
);
364 u
= strv_env_set(l
, s
);
368 strv_free_and_replace(l
, u
);
371 if (strv_isempty(l
)) {
372 if (unlink("/etc/vconsole.conf") < 0)
373 return errno
== ENOENT
? 0 : -errno
;
375 c
->vc_mtime
= USEC_INFINITY
;
379 r
= write_env_file_label("/etc/vconsole.conf", l
);
383 if (stat("/etc/vconsole.conf", &st
) >= 0)
384 c
->vc_mtime
= timespec_load(&st
.st_mtim
);
389 int x11_write_data(Context
*c
) {
390 _cleanup_fclose_
FILE *f
= NULL
;
391 _cleanup_free_
char *temp_path
= NULL
;
395 if (isempty(c
->x11_layout
) &&
396 isempty(c
->x11_model
) &&
397 isempty(c
->x11_variant
) &&
398 isempty(c
->x11_options
)) {
400 if (unlink("/etc/X11/xorg.conf.d/00-keyboard.conf") < 0)
401 return errno
== ENOENT
? 0 : -errno
;
403 c
->vc_mtime
= USEC_INFINITY
;
407 mkdir_p_label("/etc/X11/xorg.conf.d", 0755);
409 r
= fopen_temporary("/etc/X11/xorg.conf.d/00-keyboard.conf", &f
, &temp_path
);
413 (void) __fsetlocking(f
, FSETLOCKING_BYCALLER
);
414 (void) fchmod(fileno(f
), 0644);
416 fputs("# Written by systemd-localed(8), read by systemd-localed and Xorg. It's\n"
417 "# probably wise not to edit this file manually. Use localectl(1) to\n"
418 "# instruct systemd-localed to update it.\n"
419 "Section \"InputClass\"\n"
420 " Identifier \"system-keyboard\"\n"
421 " MatchIsKeyboard \"on\"\n", f
);
423 if (!isempty(c
->x11_layout
))
424 fprintf(f
, " Option \"XkbLayout\" \"%s\"\n", c
->x11_layout
);
426 if (!isempty(c
->x11_model
))
427 fprintf(f
, " Option \"XkbModel\" \"%s\"\n", c
->x11_model
);
429 if (!isempty(c
->x11_variant
))
430 fprintf(f
, " Option \"XkbVariant\" \"%s\"\n", c
->x11_variant
);
432 if (!isempty(c
->x11_options
))
433 fprintf(f
, " Option \"XkbOptions\" \"%s\"\n", c
->x11_options
);
435 fputs("EndSection\n", f
);
437 r
= fflush_sync_and_check(f
);
441 if (rename(temp_path
, "/etc/X11/xorg.conf.d/00-keyboard.conf") < 0) {
446 if (stat("/etc/X11/xorg.conf.d/00-keyboard.conf", &st
) >= 0)
447 c
->x11_mtime
= timespec_load(&st
.st_mtim
);
453 (void) unlink(temp_path
);
458 static int read_next_mapping(const char* filename
,
459 unsigned min_fields
, unsigned max_fields
,
460 FILE *f
, unsigned *n
, char ***a
) {
472 if (!fgets(line
, sizeof(line
), f
)) {
475 return errno
> 0 ? -errno
: -EIO
;
483 if (IN_SET(l
[0], 0, '#'))
486 r
= strv_split_extract(&b
, l
, WHITESPACE
, EXTRACT_QUOTES
);
490 length
= strv_length(b
);
491 if (length
< min_fields
|| length
> max_fields
) {
492 log_error("Invalid line %s:%u, ignoring.", filename
, *n
);
503 int vconsole_convert_to_x11(Context
*c
) {
507 map
= systemd_kbd_model_map();
509 if (isempty(c
->vc_keymap
)) {
511 !isempty(c
->x11_layout
) ||
512 !isempty(c
->x11_model
) ||
513 !isempty(c
->x11_variant
) ||
514 !isempty(c
->x11_options
);
518 _cleanup_fclose_
FILE *f
= NULL
;
521 f
= fopen(map
, "re");
526 _cleanup_strv_free_
char **a
= NULL
;
529 r
= read_next_mapping(map
, 5, UINT_MAX
, f
, &n
, &a
);
535 if (!streq(c
->vc_keymap
, a
[0]))
538 if (!streq_ptr(c
->x11_layout
, strnulldash(a
[1])) ||
539 !streq_ptr(c
->x11_model
, strnulldash(a
[2])) ||
540 !streq_ptr(c
->x11_variant
, strnulldash(a
[3])) ||
541 !streq_ptr(c
->x11_options
, strnulldash(a
[4]))) {
543 if (free_and_strdup(&c
->x11_layout
, strnulldash(a
[1])) < 0 ||
544 free_and_strdup(&c
->x11_model
, strnulldash(a
[2])) < 0 ||
545 free_and_strdup(&c
->x11_variant
, strnulldash(a
[3])) < 0 ||
546 free_and_strdup(&c
->x11_options
, strnulldash(a
[4])) < 0)
557 log_info("Changing X11 keyboard layout to '%s' model '%s' variant '%s' options '%s'",
558 strempty(c
->x11_layout
),
559 strempty(c
->x11_model
),
560 strempty(c
->x11_variant
),
561 strempty(c
->x11_options
));
562 else if (modified
< 0)
563 log_notice("X11 keyboard layout was not modified: no conversion found for \"%s\".",
566 log_debug("X11 keyboard layout did not need to be modified.");
571 int find_converted_keymap(const char *x11_layout
, const char *x11_variant
, char **new_keymap
) {
573 _cleanup_free_
char *n
;
576 n
= strjoin(x11_layout
, "-", x11_variant
);
578 n
= strdup(x11_layout
);
582 NULSTR_FOREACH(dir
, KBD_KEYMAP_DIRS
) {
583 _cleanup_free_
char *p
= NULL
, *pz
= NULL
;
586 p
= strjoin(dir
, "xkb/", n
, ".map");
587 pz
= strjoin(dir
, "xkb/", n
, ".map.gz");
591 uncompressed
= access(p
, F_OK
) == 0;
592 if (uncompressed
|| access(pz
, F_OK
) == 0) {
593 log_debug("Found converted keymap %s at %s",
594 n
, uncompressed
? p
: pz
);
596 *new_keymap
= TAKE_PTR(n
);
604 int find_legacy_keymap(Context
*c
, char **ret
) {
606 _cleanup_fclose_
FILE *f
= NULL
;
607 _cleanup_free_
char *new_keymap
= NULL
;
609 unsigned best_matching
= 0;
612 assert(!isempty(c
->x11_layout
));
614 map
= systemd_kbd_model_map();
616 f
= fopen(map
, "re");
621 _cleanup_strv_free_
char **a
= NULL
;
622 unsigned matching
= 0;
624 r
= read_next_mapping(map
, 5, UINT_MAX
, f
, &n
, &a
);
630 /* Determine how well matching this entry is */
631 if (streq(c
->x11_layout
, a
[1]))
632 /* If we got an exact match, this is best */
635 /* We have multiple X layouts, look for an
636 * entry that matches our key with everything
637 * but the first layout stripped off. */
638 if (startswith_comma(c
->x11_layout
, a
[1]))
643 /* If that didn't work, strip off the
644 * other layouts from the entry, too */
645 x
= strndupa(a
[1], strcspn(a
[1], ","));
646 if (startswith_comma(c
->x11_layout
, x
))
652 if (isempty(c
->x11_model
) || streq_ptr(c
->x11_model
, a
[2])) {
655 if (streq_ptr(c
->x11_variant
, a
[3])) {
658 if (streq_ptr(c
->x11_options
, a
[4]))
664 /* The best matching entry so far, then let's save that */
665 if (matching
>= MAX(best_matching
, 1u)) {
666 log_debug("Found legacy keymap %s with score %u",
669 if (matching
> best_matching
) {
670 best_matching
= matching
;
672 r
= free_and_strdup(&new_keymap
, a
[0]);
679 if (best_matching
< 10 && c
->x11_layout
) {
680 /* The best match is only the first part of the X11
681 * keymap. Check if we have a converted map which
682 * matches just the first layout.
684 char *l
, *v
= NULL
, *converted
;
686 l
= strndupa(c
->x11_layout
, strcspn(c
->x11_layout
, ","));
688 v
= strndupa(c
->x11_variant
, strcspn(c
->x11_variant
, ","));
689 r
= find_converted_keymap(l
, v
, &converted
);
693 free_and_replace(new_keymap
, converted
);
696 *ret
= TAKE_PTR(new_keymap
);
700 int find_language_fallback(const char *lang
, char **language
) {
702 _cleanup_fclose_
FILE *f
= NULL
;
708 map
= systemd_language_fallback_map();
710 f
= fopen(map
, "re");
715 _cleanup_strv_free_
char **a
= NULL
;
718 r
= read_next_mapping(map
, 2, 2, f
, &n
, &a
);
722 if (streq(lang
, a
[0])) {
723 assert(strv_length(a
) == 2);
724 *language
= TAKE_PTR(a
[1]);
729 assert_not_reached("should not be here");
732 int x11_convert_to_vconsole(Context
*c
) {
733 bool modified
= false;
735 if (isempty(c
->x11_layout
)) {
737 !isempty(c
->vc_keymap
) ||
738 !isempty(c
->vc_keymap_toggle
);
740 context_free_vconsole(c
);
742 _cleanup_free_
char *new_keymap
= NULL
;
745 r
= find_converted_keymap(c
->x11_layout
, c
->x11_variant
, &new_keymap
);
749 r
= find_legacy_keymap(c
, &new_keymap
);
754 /* We search for layout-variant match first, but then we also look
755 * for anything which matches just the layout. So it's accurate to say
756 * that we couldn't find anything which matches the layout. */
757 log_notice("No conversion to virtual console map found for \"%s\".",
760 if (!streq_ptr(c
->vc_keymap
, new_keymap
)) {
761 free_and_replace(c
->vc_keymap
, new_keymap
);
762 c
->vc_keymap_toggle
= mfree(c
->vc_keymap_toggle
);
768 log_info("Changing virtual console keymap to '%s' toggle '%s'",
769 strempty(c
->vc_keymap
), strempty(c
->vc_keymap_toggle
));
771 log_debug("Virtual console keymap was not modified.");