1 /* SPDX-License-Identifier: LGPL-2.1+ */
9 #include "env-file-label.h"
13 #include "fileio-label.h"
16 #include "keymap-util.h"
17 #include "locale-util.h"
20 #include "nulstr-util.h"
21 #include "string-util.h"
23 #include "tmpfile-util.h"
25 static bool startswith_comma(const char *s
, const char *prefix
) {
26 s
= startswith(s
, prefix
);
30 return IN_SET(*s
, ',', '\0');
33 static const char* strnulldash(const char *s
) {
34 return isempty(s
) || streq(s
, "-") ? NULL
: s
;
37 static const char* systemd_kbd_model_map(void) {
40 s
= getenv("SYSTEMD_KBD_MODEL_MAP");
44 return SYSTEMD_KBD_MODEL_MAP
;
47 static const char* systemd_language_fallback_map(void) {
50 s
= getenv("SYSTEMD_LANGUAGE_FALLBACK_MAP");
54 return SYSTEMD_LANGUAGE_FALLBACK_MAP
;
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
);
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
);
69 static void context_free_locale(Context
*c
) {
72 for (p
= 0; p
< _VARIABLE_LC_MAX
; p
++)
73 c
->locale
[p
] = mfree(c
->locale
[p
]);
76 void context_clear(Context
*c
) {
77 context_free_locale(c
);
79 context_free_vconsole(c
);
81 sd_bus_message_unref(c
->locale_cache
);
82 sd_bus_message_unref(c
->x11_cache
);
83 sd_bus_message_unref(c
->vc_cache
);
85 bus_verify_polkit_async_registry_free(c
->polkit_registry
);
88 void locale_simplify(char *locale
[_VARIABLE_LC_MAX
]) {
91 for (p
= VARIABLE_LANG
+1; p
< _VARIABLE_LC_MAX
; p
++)
92 if (isempty(locale
[p
]) || streq_ptr(locale
[VARIABLE_LANG
], locale
[p
]))
93 locale
[p
] = mfree(locale
[p
]);
96 int locale_read_data(Context
*c
, sd_bus_message
*m
) {
100 /* Do not try to re-read the file within single bus operation. */
102 if (m
== c
->locale_cache
)
105 sd_bus_message_unref(c
->locale_cache
);
106 c
->locale_cache
= sd_bus_message_ref(m
);
109 r
= stat("/etc/locale.conf", &st
);
110 if (r
< 0 && errno
!= ENOENT
)
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
)
122 context_free_locale(c
);
124 r
= parse_env_file(NULL
, "/etc/locale.conf",
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
],
138 "LC_IDENTIFICATION", &c
->locale
[VARIABLE_LC_IDENTIFICATION
]);
144 c
->locale_mtime
= USEC_INFINITY
;
145 context_free_locale(c
);
147 /* Fill in what we got passed from systemd. */
148 for (p
= 0; p
< _VARIABLE_LC_MAX
; p
++) {
151 name
= locale_variable_to_string(p
);
154 r
= free_and_strdup(&c
->locale
[p
], empty_to_null(getenv(name
)));
160 locale_simplify(c
->locale
);
164 int vconsole_read_data(Context
*c
, sd_bus_message
*m
) {
169 /* Do not try to re-read the file within single bus operation. */
171 if (m
== c
->vc_cache
)
174 sd_bus_message_unref(c
->vc_cache
);
175 c
->vc_cache
= sd_bus_message_ref(m
);
178 if (stat("/etc/vconsole.conf", &st
) < 0) {
182 c
->vc_mtime
= USEC_INFINITY
;
183 context_free_vconsole(c
);
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
)
193 context_free_vconsole(c
);
195 r
= parse_env_file(NULL
, "/etc/vconsole.conf",
196 "KEYMAP", &c
->vc_keymap
,
197 "KEYMAP_TOGGLE", &c
->vc_keymap_toggle
);
204 int x11_read_data(Context
*c
, sd_bus_message
*m
) {
205 _cleanup_fclose_
FILE *f
= NULL
;
206 bool in_section
= false;
211 /* Do not try to re-read the file within single bus operation. */
213 if (m
== c
->x11_cache
)
216 sd_bus_message_unref(c
->x11_cache
);
217 c
->x11_cache
= sd_bus_message_ref(m
);
220 if (stat("/etc/X11/xorg.conf.d/00-keyboard.conf", &st
) < 0) {
224 c
->x11_mtime
= USEC_INFINITY
;
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
)
237 f
= fopen("/etc/X11/xorg.conf.d/00-keyboard.conf", "re");
242 _cleanup_free_
char *line
= NULL
;
245 r
= read_line(f
, LONG_LINE_MAX
, &line
);
252 if (IN_SET(l
[0], 0, '#'))
255 if (in_section
&& first_word(l
, "Option")) {
256 _cleanup_strv_free_
char **a
= NULL
;
258 r
= strv_split_extract(&a
, l
, WHITESPACE
, EXTRACT_QUOTES
);
262 if (strv_length(a
) == 3) {
265 if (streq(a
[1], "XkbLayout"))
267 else if (streq(a
[1], "XkbModel"))
269 else if (streq(a
[1], "XkbVariant"))
271 else if (streq(a
[1], "XkbOptions"))
275 free_and_replace(*p
, a
[2]);
279 } else if (!in_section
&& first_word(l
, "Section")) {
280 _cleanup_strv_free_
char **a
= NULL
;
282 r
= strv_split_extract(&a
, l
, WHITESPACE
, EXTRACT_QUOTES
);
286 if (strv_length(a
) == 2 && streq(a
[1], "InputClass"))
289 } else if (in_section
&& first_word(l
, "EndSection"))
296 int locale_write_data(Context
*c
, char ***settings
) {
297 _cleanup_strv_free_
char **l
= NULL
;
301 /* Set values will be returned as strv in *settings on success. */
303 for (p
= 0; p
< _VARIABLE_LC_MAX
; p
++) {
304 _cleanup_free_
char *t
= NULL
;
308 name
= locale_variable_to_string(p
);
311 if (isempty(c
->locale
[p
]))
314 if (asprintf(&t
, "%s=%s", name
, c
->locale
[p
]) < 0)
317 u
= strv_env_set(l
, t
);
321 strv_free_and_replace(l
, u
);
324 if (strv_isempty(l
)) {
325 if (unlink("/etc/locale.conf") < 0)
326 return errno
== ENOENT
? 0 : -errno
;
328 c
->locale_mtime
= USEC_INFINITY
;
332 r
= write_env_file_label("/etc/locale.conf", l
);
336 *settings
= TAKE_PTR(l
);
338 if (stat("/etc/locale.conf", &st
) >= 0)
339 c
->locale_mtime
= timespec_load(&st
.st_mtim
);
344 int vconsole_write_data(Context
*c
) {
345 _cleanup_strv_free_
char **l
= NULL
;
349 r
= load_env_file(NULL
, "/etc/vconsole.conf", &l
);
350 if (r
< 0 && r
!= -ENOENT
)
353 if (isempty(c
->vc_keymap
))
354 l
= strv_env_unset(l
, "KEYMAP");
356 _cleanup_free_
char *s
= NULL
;
359 s
= strappend("KEYMAP=", c
->vc_keymap
);
363 u
= strv_env_set(l
, s
);
367 strv_free_and_replace(l
, u
);
370 if (isempty(c
->vc_keymap_toggle
))
371 l
= strv_env_unset(l
, "KEYMAP_TOGGLE");
373 _cleanup_free_
char *s
= NULL
;
376 s
= strappend("KEYMAP_TOGGLE=", c
->vc_keymap_toggle
);
380 u
= strv_env_set(l
, s
);
384 strv_free_and_replace(l
, u
);
387 if (strv_isempty(l
)) {
388 if (unlink("/etc/vconsole.conf") < 0)
389 return errno
== ENOENT
? 0 : -errno
;
391 c
->vc_mtime
= USEC_INFINITY
;
395 r
= write_env_file_label("/etc/vconsole.conf", l
);
399 if (stat("/etc/vconsole.conf", &st
) >= 0)
400 c
->vc_mtime
= timespec_load(&st
.st_mtim
);
405 int x11_write_data(Context
*c
) {
406 _cleanup_fclose_
FILE *f
= NULL
;
407 _cleanup_free_
char *temp_path
= NULL
;
411 if (isempty(c
->x11_layout
) &&
412 isempty(c
->x11_model
) &&
413 isempty(c
->x11_variant
) &&
414 isempty(c
->x11_options
)) {
416 if (unlink("/etc/X11/xorg.conf.d/00-keyboard.conf") < 0)
417 return errno
== ENOENT
? 0 : -errno
;
419 c
->vc_mtime
= USEC_INFINITY
;
423 mkdir_p_label("/etc/X11/xorg.conf.d", 0755);
425 r
= fopen_temporary("/etc/X11/xorg.conf.d/00-keyboard.conf", &f
, &temp_path
);
429 (void) __fsetlocking(f
, FSETLOCKING_BYCALLER
);
430 (void) fchmod(fileno(f
), 0644);
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
);
439 if (!isempty(c
->x11_layout
))
440 fprintf(f
, " Option \"XkbLayout\" \"%s\"\n", c
->x11_layout
);
442 if (!isempty(c
->x11_model
))
443 fprintf(f
, " Option \"XkbModel\" \"%s\"\n", c
->x11_model
);
445 if (!isempty(c
->x11_variant
))
446 fprintf(f
, " Option \"XkbVariant\" \"%s\"\n", c
->x11_variant
);
448 if (!isempty(c
->x11_options
))
449 fprintf(f
, " Option \"XkbOptions\" \"%s\"\n", c
->x11_options
);
451 fputs("EndSection\n", f
);
453 r
= fflush_sync_and_check(f
);
457 if (rename(temp_path
, "/etc/X11/xorg.conf.d/00-keyboard.conf") < 0) {
462 if (stat("/etc/X11/xorg.conf.d/00-keyboard.conf", &st
) >= 0)
463 c
->x11_mtime
= timespec_load(&st
.st_mtim
);
469 (void) unlink(temp_path
);
474 static int read_next_mapping(const char* filename
,
475 unsigned min_fields
, unsigned max_fields
,
476 FILE *f
, unsigned *n
, char ***a
) {
482 _cleanup_free_
char *line
= NULL
;
487 r
= read_line(f
, LONG_LINE_MAX
, &line
);
496 if (IN_SET(l
[0], 0, '#'))
499 r
= strv_split_extract(&b
, l
, WHITESPACE
, EXTRACT_QUOTES
);
503 length
= strv_length(b
);
504 if (length
< min_fields
|| length
> max_fields
) {
505 log_error("Invalid line %s:%u, ignoring.", filename
, *n
);
518 int vconsole_convert_to_x11(Context
*c
) {
522 map
= systemd_kbd_model_map();
524 if (isempty(c
->vc_keymap
)) {
526 !isempty(c
->x11_layout
) ||
527 !isempty(c
->x11_model
) ||
528 !isempty(c
->x11_variant
) ||
529 !isempty(c
->x11_options
);
533 _cleanup_fclose_
FILE *f
= NULL
;
536 f
= fopen(map
, "re");
541 _cleanup_strv_free_
char **a
= NULL
;
544 r
= read_next_mapping(map
, 5, UINT_MAX
, f
, &n
, &a
);
550 if (!streq(c
->vc_keymap
, a
[0]))
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]))) {
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)
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
));
577 else if (modified
< 0)
578 log_notice("X11 keyboard layout was not modified: no conversion found for \"%s\".",
581 log_debug("X11 keyboard layout did not need to be modified.");
586 int find_converted_keymap(const char *x11_layout
, const char *x11_variant
, char **new_keymap
) {
588 _cleanup_free_
char *n
;
591 n
= strjoin(x11_layout
, "-", x11_variant
);
593 n
= strdup(x11_layout
);
597 NULSTR_FOREACH(dir
, KBD_KEYMAP_DIRS
) {
598 _cleanup_free_
char *p
= NULL
, *pz
= NULL
;
601 p
= strjoin(dir
, "xkb/", n
, ".map");
602 pz
= strjoin(dir
, "xkb/", n
, ".map.gz");
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
);
611 *new_keymap
= TAKE_PTR(n
);
619 int find_legacy_keymap(Context
*c
, char **ret
) {
621 _cleanup_fclose_
FILE *f
= NULL
;
622 _cleanup_free_
char *new_keymap
= NULL
;
624 unsigned best_matching
= 0;
627 assert(!isempty(c
->x11_layout
));
629 map
= systemd_kbd_model_map();
631 f
= fopen(map
, "re");
636 _cleanup_strv_free_
char **a
= NULL
;
637 unsigned matching
= 0;
639 r
= read_next_mapping(map
, 5, UINT_MAX
, f
, &n
, &a
);
645 /* Determine how well matching this entry is */
646 if (streq(c
->x11_layout
, a
[1]))
647 /* If we got an exact match, this is best */
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]))
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
))
667 if (isempty(c
->x11_model
) || streq_ptr(c
->x11_model
, a
[2])) {
670 if (streq_ptr(c
->x11_variant
, a
[3])) {
673 if (streq_ptr(c
->x11_options
, a
[4]))
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",
684 if (matching
> best_matching
) {
685 best_matching
= matching
;
687 r
= free_and_strdup(&new_keymap
, a
[0]);
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.
699 char *l
, *v
= NULL
, *converted
;
701 l
= strndupa(c
->x11_layout
, strcspn(c
->x11_layout
, ","));
703 v
= strndupa(c
->x11_variant
, strcspn(c
->x11_variant
, ","));
704 r
= find_converted_keymap(l
, v
, &converted
);
708 free_and_replace(new_keymap
, converted
);
711 *ret
= TAKE_PTR(new_keymap
);
715 int find_language_fallback(const char *lang
, char **language
) {
717 _cleanup_fclose_
FILE *f
= NULL
;
723 map
= systemd_language_fallback_map();
725 f
= fopen(map
, "re");
730 _cleanup_strv_free_
char **a
= NULL
;
733 r
= read_next_mapping(map
, 2, 2, f
, &n
, &a
);
737 if (streq(lang
, a
[0])) {
738 assert(strv_length(a
) == 2);
739 *language
= TAKE_PTR(a
[1]);
744 assert_not_reached("should not be here");
747 int x11_convert_to_vconsole(Context
*c
) {
748 bool modified
= false;
750 if (isempty(c
->x11_layout
)) {
752 !isempty(c
->vc_keymap
) ||
753 !isempty(c
->vc_keymap_toggle
);
755 context_free_vconsole(c
);
757 _cleanup_free_
char *new_keymap
= NULL
;
760 r
= find_converted_keymap(c
->x11_layout
, c
->x11_variant
, &new_keymap
);
764 r
= find_legacy_keymap(c
, &new_keymap
);
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\".",
775 if (!streq_ptr(c
->vc_keymap
, new_keymap
)) {
776 free_and_replace(c
->vc_keymap
, new_keymap
);
777 c
->vc_keymap_toggle
= mfree(c
->vc_keymap_toggle
);
783 log_info("Changing virtual console keymap to '%s' toggle '%s'",
784 strempty(c
->vc_keymap
), strempty(c
->vc_keymap_toggle
));
786 log_debug("Virtual console keymap was not modified.");