1 /* SPDX-License-Identifier: LGPL-2.1+ */
11 #include "env-file-label.h"
15 #include "fileio-label.h"
18 #include "keymap-util.h"
19 #include "locale-util.h"
22 #include "nulstr-util.h"
23 #include "string-util.h"
25 #include "tmpfile-util.h"
27 static bool startswith_comma(const char *s
, const char *prefix
) {
28 s
= startswith(s
, prefix
);
32 return IN_SET(*s
, ',', '\0');
35 static const char* strnulldash(const char *s
) {
36 return isempty(s
) || streq(s
, "-") ? NULL
: s
;
39 static const char* systemd_kbd_model_map(void) {
42 s
= getenv("SYSTEMD_KBD_MODEL_MAP");
46 return SYSTEMD_KBD_MODEL_MAP
;
49 static const char* systemd_language_fallback_map(void) {
52 s
= getenv("SYSTEMD_LANGUAGE_FALLBACK_MAP");
56 return SYSTEMD_LANGUAGE_FALLBACK_MAP
;
59 static void context_free_x11(Context
*c
) {
60 c
->x11_layout
= mfree(c
->x11_layout
);
61 c
->x11_options
= mfree(c
->x11_options
);
62 c
->x11_model
= mfree(c
->x11_model
);
63 c
->x11_variant
= mfree(c
->x11_variant
);
66 static void context_free_vconsole(Context
*c
) {
67 c
->vc_keymap
= mfree(c
->vc_keymap
);
68 c
->vc_keymap_toggle
= mfree(c
->vc_keymap_toggle
);
71 static void context_free_locale(Context
*c
) {
74 for (p
= 0; p
< _VARIABLE_LC_MAX
; p
++)
75 c
->locale
[p
] = mfree(c
->locale
[p
]);
78 void context_clear(Context
*c
) {
79 context_free_locale(c
);
81 context_free_vconsole(c
);
83 sd_bus_message_unref(c
->locale_cache
);
84 sd_bus_message_unref(c
->x11_cache
);
85 sd_bus_message_unref(c
->vc_cache
);
87 bus_verify_polkit_async_registry_free(c
->polkit_registry
);
90 void locale_simplify(char *locale
[_VARIABLE_LC_MAX
]) {
93 for (p
= VARIABLE_LANG
+1; p
< _VARIABLE_LC_MAX
; p
++)
94 if (isempty(locale
[p
]) || streq_ptr(locale
[VARIABLE_LANG
], locale
[p
]))
95 locale
[p
] = mfree(locale
[p
]);
98 int locale_read_data(Context
*c
, sd_bus_message
*m
) {
102 /* Do not try to re-read the file within single bus operation. */
104 if (m
== c
->locale_cache
)
107 sd_bus_message_unref(c
->locale_cache
);
108 c
->locale_cache
= sd_bus_message_ref(m
);
111 r
= stat("/etc/locale.conf", &st
);
112 if (r
< 0 && errno
!= ENOENT
)
118 /* If mtime is not changed, then we do not need to re-read the file. */
119 t
= timespec_load(&st
.st_mtim
);
120 if (c
->locale_mtime
!= USEC_INFINITY
&& t
== c
->locale_mtime
)
124 context_free_locale(c
);
126 r
= parse_env_file(NULL
, "/etc/locale.conf",
127 "LANG", &c
->locale
[VARIABLE_LANG
],
128 "LANGUAGE", &c
->locale
[VARIABLE_LANGUAGE
],
129 "LC_CTYPE", &c
->locale
[VARIABLE_LC_CTYPE
],
130 "LC_NUMERIC", &c
->locale
[VARIABLE_LC_NUMERIC
],
131 "LC_TIME", &c
->locale
[VARIABLE_LC_TIME
],
132 "LC_COLLATE", &c
->locale
[VARIABLE_LC_COLLATE
],
133 "LC_MONETARY", &c
->locale
[VARIABLE_LC_MONETARY
],
134 "LC_MESSAGES", &c
->locale
[VARIABLE_LC_MESSAGES
],
135 "LC_PAPER", &c
->locale
[VARIABLE_LC_PAPER
],
136 "LC_NAME", &c
->locale
[VARIABLE_LC_NAME
],
137 "LC_ADDRESS", &c
->locale
[VARIABLE_LC_ADDRESS
],
138 "LC_TELEPHONE", &c
->locale
[VARIABLE_LC_TELEPHONE
],
139 "LC_MEASUREMENT", &c
->locale
[VARIABLE_LC_MEASUREMENT
],
140 "LC_IDENTIFICATION", &c
->locale
[VARIABLE_LC_IDENTIFICATION
]);
146 c
->locale_mtime
= USEC_INFINITY
;
147 context_free_locale(c
);
149 /* Fill in what we got passed from systemd. */
150 for (p
= 0; p
< _VARIABLE_LC_MAX
; p
++) {
153 name
= locale_variable_to_string(p
);
156 r
= free_and_strdup(&c
->locale
[p
], empty_to_null(getenv(name
)));
162 locale_simplify(c
->locale
);
166 int vconsole_read_data(Context
*c
, sd_bus_message
*m
) {
171 /* Do not try to re-read the file within single bus operation. */
173 if (m
== c
->vc_cache
)
176 sd_bus_message_unref(c
->vc_cache
);
177 c
->vc_cache
= sd_bus_message_ref(m
);
180 if (stat("/etc/vconsole.conf", &st
) < 0) {
184 c
->vc_mtime
= USEC_INFINITY
;
185 context_free_vconsole(c
);
189 /* If mtime is not changed, then we do not need to re-read */
190 t
= timespec_load(&st
.st_mtim
);
191 if (c
->vc_mtime
!= USEC_INFINITY
&& t
== c
->vc_mtime
)
195 context_free_vconsole(c
);
197 r
= parse_env_file(NULL
, "/etc/vconsole.conf",
198 "KEYMAP", &c
->vc_keymap
,
199 "KEYMAP_TOGGLE", &c
->vc_keymap_toggle
);
206 int x11_read_data(Context
*c
, sd_bus_message
*m
) {
207 _cleanup_fclose_
FILE *f
= NULL
;
208 bool in_section
= false;
213 /* Do not try to re-read the file within single bus operation. */
215 if (m
== c
->x11_cache
)
218 sd_bus_message_unref(c
->x11_cache
);
219 c
->x11_cache
= sd_bus_message_ref(m
);
222 if (stat("/etc/X11/xorg.conf.d/00-keyboard.conf", &st
) < 0) {
226 c
->x11_mtime
= USEC_INFINITY
;
231 /* If mtime is not changed, then we do not need to re-read */
232 t
= timespec_load(&st
.st_mtim
);
233 if (c
->x11_mtime
!= USEC_INFINITY
&& t
== c
->x11_mtime
)
239 f
= fopen("/etc/X11/xorg.conf.d/00-keyboard.conf", "re");
244 _cleanup_free_
char *line
= NULL
;
247 r
= read_line(f
, LONG_LINE_MAX
, &line
);
254 if (IN_SET(l
[0], 0, '#'))
257 if (in_section
&& first_word(l
, "Option")) {
258 _cleanup_strv_free_
char **a
= NULL
;
260 r
= strv_split_extract(&a
, l
, WHITESPACE
, EXTRACT_QUOTES
);
264 if (strv_length(a
) == 3) {
267 if (streq(a
[1], "XkbLayout"))
269 else if (streq(a
[1], "XkbModel"))
271 else if (streq(a
[1], "XkbVariant"))
273 else if (streq(a
[1], "XkbOptions"))
277 free_and_replace(*p
, a
[2]);
281 } else if (!in_section
&& first_word(l
, "Section")) {
282 _cleanup_strv_free_
char **a
= NULL
;
284 r
= strv_split_extract(&a
, l
, WHITESPACE
, EXTRACT_QUOTES
);
288 if (strv_length(a
) == 2 && streq(a
[1], "InputClass"))
291 } else if (in_section
&& first_word(l
, "EndSection"))
298 int locale_write_data(Context
*c
, char ***settings
) {
299 _cleanup_strv_free_
char **l
= NULL
;
303 /* Set values will be returned as strv in *settings on success. */
305 for (p
= 0; p
< _VARIABLE_LC_MAX
; p
++) {
306 _cleanup_free_
char *t
= NULL
;
310 name
= locale_variable_to_string(p
);
313 if (isempty(c
->locale
[p
]))
316 if (asprintf(&t
, "%s=%s", name
, c
->locale
[p
]) < 0)
319 u
= strv_env_set(l
, t
);
323 strv_free_and_replace(l
, u
);
326 if (strv_isempty(l
)) {
327 if (unlink("/etc/locale.conf") < 0)
328 return errno
== ENOENT
? 0 : -errno
;
330 c
->locale_mtime
= USEC_INFINITY
;
334 r
= write_env_file_label("/etc/locale.conf", l
);
338 *settings
= TAKE_PTR(l
);
340 if (stat("/etc/locale.conf", &st
) >= 0)
341 c
->locale_mtime
= timespec_load(&st
.st_mtim
);
346 int vconsole_write_data(Context
*c
) {
347 _cleanup_strv_free_
char **l
= NULL
;
351 r
= load_env_file(NULL
, "/etc/vconsole.conf", &l
);
352 if (r
< 0 && r
!= -ENOENT
)
355 if (isempty(c
->vc_keymap
))
356 l
= strv_env_unset(l
, "KEYMAP");
358 _cleanup_free_
char *s
= NULL
;
361 s
= strappend("KEYMAP=", c
->vc_keymap
);
365 u
= strv_env_set(l
, s
);
369 strv_free_and_replace(l
, u
);
372 if (isempty(c
->vc_keymap_toggle
))
373 l
= strv_env_unset(l
, "KEYMAP_TOGGLE");
375 _cleanup_free_
char *s
= NULL
;
378 s
= strappend("KEYMAP_TOGGLE=", c
->vc_keymap_toggle
);
382 u
= strv_env_set(l
, s
);
386 strv_free_and_replace(l
, u
);
389 if (strv_isempty(l
)) {
390 if (unlink("/etc/vconsole.conf") < 0)
391 return errno
== ENOENT
? 0 : -errno
;
393 c
->vc_mtime
= USEC_INFINITY
;
397 r
= write_env_file_label("/etc/vconsole.conf", l
);
401 if (stat("/etc/vconsole.conf", &st
) >= 0)
402 c
->vc_mtime
= timespec_load(&st
.st_mtim
);
407 int x11_write_data(Context
*c
) {
408 _cleanup_fclose_
FILE *f
= NULL
;
409 _cleanup_free_
char *temp_path
= NULL
;
413 if (isempty(c
->x11_layout
) &&
414 isempty(c
->x11_model
) &&
415 isempty(c
->x11_variant
) &&
416 isempty(c
->x11_options
)) {
418 if (unlink("/etc/X11/xorg.conf.d/00-keyboard.conf") < 0)
419 return errno
== ENOENT
? 0 : -errno
;
421 c
->vc_mtime
= USEC_INFINITY
;
425 mkdir_p_label("/etc/X11/xorg.conf.d", 0755);
427 r
= fopen_temporary("/etc/X11/xorg.conf.d/00-keyboard.conf", &f
, &temp_path
);
431 (void) __fsetlocking(f
, FSETLOCKING_BYCALLER
);
432 (void) fchmod(fileno(f
), 0644);
434 fputs("# Written by systemd-localed(8), read by systemd-localed and Xorg. It's\n"
435 "# probably wise not to edit this file manually. Use localectl(1) to\n"
436 "# instruct systemd-localed to update it.\n"
437 "Section \"InputClass\"\n"
438 " Identifier \"system-keyboard\"\n"
439 " MatchIsKeyboard \"on\"\n", f
);
441 if (!isempty(c
->x11_layout
))
442 fprintf(f
, " Option \"XkbLayout\" \"%s\"\n", c
->x11_layout
);
444 if (!isempty(c
->x11_model
))
445 fprintf(f
, " Option \"XkbModel\" \"%s\"\n", c
->x11_model
);
447 if (!isempty(c
->x11_variant
))
448 fprintf(f
, " Option \"XkbVariant\" \"%s\"\n", c
->x11_variant
);
450 if (!isempty(c
->x11_options
))
451 fprintf(f
, " Option \"XkbOptions\" \"%s\"\n", c
->x11_options
);
453 fputs("EndSection\n", f
);
455 r
= fflush_sync_and_check(f
);
459 if (rename(temp_path
, "/etc/X11/xorg.conf.d/00-keyboard.conf") < 0) {
464 if (stat("/etc/X11/xorg.conf.d/00-keyboard.conf", &st
) >= 0)
465 c
->x11_mtime
= timespec_load(&st
.st_mtim
);
471 (void) unlink(temp_path
);
476 static int read_next_mapping(const char* filename
,
477 unsigned min_fields
, unsigned max_fields
,
478 FILE *f
, unsigned *n
, char ***a
) {
484 _cleanup_free_
char *line
= NULL
;
489 r
= read_line(f
, LONG_LINE_MAX
, &line
);
498 if (IN_SET(l
[0], 0, '#'))
501 r
= strv_split_extract(&b
, l
, WHITESPACE
, EXTRACT_QUOTES
);
505 length
= strv_length(b
);
506 if (length
< min_fields
|| length
> max_fields
) {
507 log_error("Invalid line %s:%u, ignoring.", filename
, *n
);
520 int vconsole_convert_to_x11(Context
*c
) {
524 map
= systemd_kbd_model_map();
526 if (isempty(c
->vc_keymap
)) {
528 !isempty(c
->x11_layout
) ||
529 !isempty(c
->x11_model
) ||
530 !isempty(c
->x11_variant
) ||
531 !isempty(c
->x11_options
);
535 _cleanup_fclose_
FILE *f
= NULL
;
538 f
= fopen(map
, "re");
543 _cleanup_strv_free_
char **a
= NULL
;
546 r
= read_next_mapping(map
, 5, UINT_MAX
, f
, &n
, &a
);
552 if (!streq(c
->vc_keymap
, a
[0]))
555 if (!streq_ptr(c
->x11_layout
, strnulldash(a
[1])) ||
556 !streq_ptr(c
->x11_model
, strnulldash(a
[2])) ||
557 !streq_ptr(c
->x11_variant
, strnulldash(a
[3])) ||
558 !streq_ptr(c
->x11_options
, strnulldash(a
[4]))) {
560 if (free_and_strdup(&c
->x11_layout
, strnulldash(a
[1])) < 0 ||
561 free_and_strdup(&c
->x11_model
, strnulldash(a
[2])) < 0 ||
562 free_and_strdup(&c
->x11_variant
, strnulldash(a
[3])) < 0 ||
563 free_and_strdup(&c
->x11_options
, strnulldash(a
[4])) < 0)
574 log_info("Changing X11 keyboard layout to '%s' model '%s' variant '%s' options '%s'",
575 strempty(c
->x11_layout
),
576 strempty(c
->x11_model
),
577 strempty(c
->x11_variant
),
578 strempty(c
->x11_options
));
579 else if (modified
< 0)
580 log_notice("X11 keyboard layout was not modified: no conversion found for \"%s\".",
583 log_debug("X11 keyboard layout did not need to be modified.");
588 int find_converted_keymap(const char *x11_layout
, const char *x11_variant
, char **new_keymap
) {
590 _cleanup_free_
char *n
;
593 n
= strjoin(x11_layout
, "-", x11_variant
);
595 n
= strdup(x11_layout
);
599 NULSTR_FOREACH(dir
, KBD_KEYMAP_DIRS
) {
600 _cleanup_free_
char *p
= NULL
, *pz
= NULL
;
603 p
= strjoin(dir
, "xkb/", n
, ".map");
604 pz
= strjoin(dir
, "xkb/", n
, ".map.gz");
608 uncompressed
= access(p
, F_OK
) == 0;
609 if (uncompressed
|| access(pz
, F_OK
) == 0) {
610 log_debug("Found converted keymap %s at %s",
611 n
, uncompressed
? p
: pz
);
613 *new_keymap
= TAKE_PTR(n
);
621 int find_legacy_keymap(Context
*c
, char **ret
) {
623 _cleanup_fclose_
FILE *f
= NULL
;
624 _cleanup_free_
char *new_keymap
= NULL
;
626 unsigned best_matching
= 0;
629 assert(!isempty(c
->x11_layout
));
631 map
= systemd_kbd_model_map();
633 f
= fopen(map
, "re");
638 _cleanup_strv_free_
char **a
= NULL
;
639 unsigned matching
= 0;
641 r
= read_next_mapping(map
, 5, UINT_MAX
, f
, &n
, &a
);
647 /* Determine how well matching this entry is */
648 if (streq(c
->x11_layout
, a
[1]))
649 /* If we got an exact match, this is best */
652 /* We have multiple X layouts, look for an
653 * entry that matches our key with everything
654 * but the first layout stripped off. */
655 if (startswith_comma(c
->x11_layout
, a
[1]))
660 /* If that didn't work, strip off the
661 * other layouts from the entry, too */
662 x
= strndupa(a
[1], strcspn(a
[1], ","));
663 if (startswith_comma(c
->x11_layout
, x
))
669 if (isempty(c
->x11_model
) || streq_ptr(c
->x11_model
, a
[2])) {
672 if (streq_ptr(c
->x11_variant
, a
[3])) {
675 if (streq_ptr(c
->x11_options
, a
[4]))
681 /* The best matching entry so far, then let's save that */
682 if (matching
>= MAX(best_matching
, 1u)) {
683 log_debug("Found legacy keymap %s with score %u",
686 if (matching
> best_matching
) {
687 best_matching
= matching
;
689 r
= free_and_strdup(&new_keymap
, a
[0]);
696 if (best_matching
< 10 && c
->x11_layout
) {
697 /* The best match is only the first part of the X11
698 * keymap. Check if we have a converted map which
699 * matches just the first layout.
701 char *l
, *v
= NULL
, *converted
;
703 l
= strndupa(c
->x11_layout
, strcspn(c
->x11_layout
, ","));
705 v
= strndupa(c
->x11_variant
, strcspn(c
->x11_variant
, ","));
706 r
= find_converted_keymap(l
, v
, &converted
);
710 free_and_replace(new_keymap
, converted
);
713 *ret
= TAKE_PTR(new_keymap
);
717 int find_language_fallback(const char *lang
, char **language
) {
719 _cleanup_fclose_
FILE *f
= NULL
;
725 map
= systemd_language_fallback_map();
727 f
= fopen(map
, "re");
732 _cleanup_strv_free_
char **a
= NULL
;
735 r
= read_next_mapping(map
, 2, 2, f
, &n
, &a
);
739 if (streq(lang
, a
[0])) {
740 assert(strv_length(a
) == 2);
741 *language
= TAKE_PTR(a
[1]);
746 assert_not_reached("should not be here");
749 int x11_convert_to_vconsole(Context
*c
) {
750 bool modified
= false;
752 if (isempty(c
->x11_layout
)) {
754 !isempty(c
->vc_keymap
) ||
755 !isempty(c
->vc_keymap_toggle
);
757 context_free_vconsole(c
);
759 _cleanup_free_
char *new_keymap
= NULL
;
762 r
= find_converted_keymap(c
->x11_layout
, c
->x11_variant
, &new_keymap
);
766 r
= find_legacy_keymap(c
, &new_keymap
);
771 /* We search for layout-variant match first, but then we also look
772 * for anything which matches just the layout. So it's accurate to say
773 * that we couldn't find anything which matches the layout. */
774 log_notice("No conversion to virtual console map found for \"%s\".",
777 if (!streq_ptr(c
->vc_keymap
, new_keymap
)) {
778 free_and_replace(c
->vc_keymap
, new_keymap
);
779 c
->vc_keymap_toggle
= mfree(c
->vc_keymap_toggle
);
785 log_info("Changing virtual console keymap to '%s' toggle '%s'",
786 strempty(c
->vc_keymap
), strempty(c
->vc_keymap_toggle
));
788 log_debug("Virtual console keymap was not modified.");