1 /* SPDX-License-Identifier: LGPL-2.1+ */
12 #include "fileio-label.h"
14 #include "keymap-util.h"
15 #include "locale-util.h"
18 #include "string-util.h"
20 #include "tmpfile-util.h"
22 static bool startswith_comma(const char *s
, const char *prefix
) {
23 s
= startswith(s
, prefix
);
27 return IN_SET(*s
, ',', '\0');
30 static const char* strnulldash(const char *s
) {
31 return isempty(s
) || streq(s
, "-") ? NULL
: s
;
34 static const char* systemd_kbd_model_map(void) {
37 s
= getenv("SYSTEMD_KBD_MODEL_MAP");
41 return SYSTEMD_KBD_MODEL_MAP
;
44 static const char* systemd_language_fallback_map(void) {
47 s
= getenv("SYSTEMD_LANGUAGE_FALLBACK_MAP");
51 return SYSTEMD_LANGUAGE_FALLBACK_MAP
;
54 static void context_free_x11(Context
*c
) {
55 c
->x11_layout
= mfree(c
->x11_layout
);
56 c
->x11_options
= mfree(c
->x11_options
);
57 c
->x11_model
= mfree(c
->x11_model
);
58 c
->x11_variant
= mfree(c
->x11_variant
);
61 static void context_free_vconsole(Context
*c
) {
62 c
->vc_keymap
= mfree(c
->vc_keymap
);
63 c
->vc_keymap_toggle
= mfree(c
->vc_keymap_toggle
);
66 static void context_free_locale(Context
*c
) {
69 for (p
= 0; p
< _VARIABLE_LC_MAX
; p
++)
70 c
->locale
[p
] = mfree(c
->locale
[p
]);
73 void context_clear(Context
*c
) {
74 context_free_locale(c
);
76 context_free_vconsole(c
);
78 sd_bus_message_unref(c
->locale_cache
);
79 sd_bus_message_unref(c
->x11_cache
);
80 sd_bus_message_unref(c
->vc_cache
);
82 bus_verify_polkit_async_registry_free(c
->polkit_registry
);
85 void locale_simplify(char *locale
[_VARIABLE_LC_MAX
]) {
88 for (p
= VARIABLE_LANG
+1; p
< _VARIABLE_LC_MAX
; p
++)
89 if (isempty(locale
[p
]) || streq_ptr(locale
[VARIABLE_LANG
], locale
[p
]))
90 locale
[p
] = mfree(locale
[p
]);
93 int locale_read_data(Context
*c
, sd_bus_message
*m
) {
97 /* Do not try to re-read the file within single bus operation. */
99 if (m
== c
->locale_cache
)
102 sd_bus_message_unref(c
->locale_cache
);
103 c
->locale_cache
= sd_bus_message_ref(m
);
106 r
= stat("/etc/locale.conf", &st
);
107 if (r
< 0 && errno
!= ENOENT
)
113 /* If mtime is not changed, then we do not need to re-read the file. */
114 t
= timespec_load(&st
.st_mtim
);
115 if (c
->locale_mtime
!= USEC_INFINITY
&& t
== c
->locale_mtime
)
119 context_free_locale(c
);
121 r
= parse_env_file(NULL
, "/etc/locale.conf",
122 "LANG", &c
->locale
[VARIABLE_LANG
],
123 "LANGUAGE", &c
->locale
[VARIABLE_LANGUAGE
],
124 "LC_CTYPE", &c
->locale
[VARIABLE_LC_CTYPE
],
125 "LC_NUMERIC", &c
->locale
[VARIABLE_LC_NUMERIC
],
126 "LC_TIME", &c
->locale
[VARIABLE_LC_TIME
],
127 "LC_COLLATE", &c
->locale
[VARIABLE_LC_COLLATE
],
128 "LC_MONETARY", &c
->locale
[VARIABLE_LC_MONETARY
],
129 "LC_MESSAGES", &c
->locale
[VARIABLE_LC_MESSAGES
],
130 "LC_PAPER", &c
->locale
[VARIABLE_LC_PAPER
],
131 "LC_NAME", &c
->locale
[VARIABLE_LC_NAME
],
132 "LC_ADDRESS", &c
->locale
[VARIABLE_LC_ADDRESS
],
133 "LC_TELEPHONE", &c
->locale
[VARIABLE_LC_TELEPHONE
],
134 "LC_MEASUREMENT", &c
->locale
[VARIABLE_LC_MEASUREMENT
],
135 "LC_IDENTIFICATION", &c
->locale
[VARIABLE_LC_IDENTIFICATION
]);
141 c
->locale_mtime
= USEC_INFINITY
;
142 context_free_locale(c
);
144 /* Fill in what we got passed from systemd. */
145 for (p
= 0; p
< _VARIABLE_LC_MAX
; p
++) {
148 name
= locale_variable_to_string(p
);
151 r
= free_and_strdup(&c
->locale
[p
], empty_to_null(getenv(name
)));
157 locale_simplify(c
->locale
);
161 int vconsole_read_data(Context
*c
, sd_bus_message
*m
) {
166 /* Do not try to re-read the file within single bus operation. */
168 if (m
== c
->vc_cache
)
171 sd_bus_message_unref(c
->vc_cache
);
172 c
->vc_cache
= sd_bus_message_ref(m
);
175 if (stat("/etc/vconsole.conf", &st
) < 0) {
179 c
->vc_mtime
= USEC_INFINITY
;
180 context_free_vconsole(c
);
184 /* If mtime is not changed, then we do not need to re-read */
185 t
= timespec_load(&st
.st_mtim
);
186 if (c
->vc_mtime
!= USEC_INFINITY
&& t
== c
->vc_mtime
)
190 context_free_vconsole(c
);
192 r
= parse_env_file(NULL
, "/etc/vconsole.conf",
193 "KEYMAP", &c
->vc_keymap
,
194 "KEYMAP_TOGGLE", &c
->vc_keymap_toggle
);
201 int x11_read_data(Context
*c
, sd_bus_message
*m
) {
202 _cleanup_fclose_
FILE *f
= NULL
;
203 bool in_section
= false;
208 /* Do not try to re-read the file within single bus operation. */
210 if (m
== c
->x11_cache
)
213 sd_bus_message_unref(c
->x11_cache
);
214 c
->x11_cache
= sd_bus_message_ref(m
);
217 if (stat("/etc/X11/xorg.conf.d/00-keyboard.conf", &st
) < 0) {
221 c
->x11_mtime
= USEC_INFINITY
;
226 /* If mtime is not changed, then we do not need to re-read */
227 t
= timespec_load(&st
.st_mtim
);
228 if (c
->x11_mtime
!= USEC_INFINITY
&& t
== c
->x11_mtime
)
234 f
= fopen("/etc/X11/xorg.conf.d/00-keyboard.conf", "re");
239 _cleanup_free_
char *line
= NULL
;
242 r
= read_line(f
, LONG_LINE_MAX
, &line
);
249 if (IN_SET(l
[0], 0, '#'))
252 if (in_section
&& first_word(l
, "Option")) {
253 _cleanup_strv_free_
char **a
= NULL
;
255 r
= strv_split_extract(&a
, l
, WHITESPACE
, EXTRACT_QUOTES
);
259 if (strv_length(a
) == 3) {
262 if (streq(a
[1], "XkbLayout"))
264 else if (streq(a
[1], "XkbModel"))
266 else if (streq(a
[1], "XkbVariant"))
268 else if (streq(a
[1], "XkbOptions"))
272 free_and_replace(*p
, a
[2]);
276 } else if (!in_section
&& first_word(l
, "Section")) {
277 _cleanup_strv_free_
char **a
= NULL
;
279 r
= strv_split_extract(&a
, l
, WHITESPACE
, EXTRACT_QUOTES
);
283 if (strv_length(a
) == 2 && streq(a
[1], "InputClass"))
286 } else if (in_section
&& first_word(l
, "EndSection"))
293 int locale_write_data(Context
*c
, char ***settings
) {
294 _cleanup_strv_free_
char **l
= NULL
;
298 /* Set values will be returned as strv in *settings on success. */
300 for (p
= 0; p
< _VARIABLE_LC_MAX
; p
++) {
301 _cleanup_free_
char *t
= NULL
;
305 name
= locale_variable_to_string(p
);
308 if (isempty(c
->locale
[p
]))
311 if (asprintf(&t
, "%s=%s", name
, c
->locale
[p
]) < 0)
314 u
= strv_env_set(l
, t
);
318 strv_free_and_replace(l
, u
);
321 if (strv_isempty(l
)) {
322 if (unlink("/etc/locale.conf") < 0)
323 return errno
== ENOENT
? 0 : -errno
;
325 c
->locale_mtime
= USEC_INFINITY
;
329 r
= write_env_file_label("/etc/locale.conf", l
);
333 *settings
= TAKE_PTR(l
);
335 if (stat("/etc/locale.conf", &st
) >= 0)
336 c
->locale_mtime
= timespec_load(&st
.st_mtim
);
341 int vconsole_write_data(Context
*c
) {
342 _cleanup_strv_free_
char **l
= NULL
;
346 r
= load_env_file(NULL
, "/etc/vconsole.conf", &l
);
347 if (r
< 0 && r
!= -ENOENT
)
350 if (isempty(c
->vc_keymap
))
351 l
= strv_env_unset(l
, "KEYMAP");
353 _cleanup_free_
char *s
= NULL
;
356 s
= strappend("KEYMAP=", c
->vc_keymap
);
360 u
= strv_env_set(l
, s
);
364 strv_free_and_replace(l
, u
);
367 if (isempty(c
->vc_keymap_toggle
))
368 l
= strv_env_unset(l
, "KEYMAP_TOGGLE");
370 _cleanup_free_
char *s
= NULL
;
373 s
= strappend("KEYMAP_TOGGLE=", c
->vc_keymap_toggle
);
377 u
= strv_env_set(l
, s
);
381 strv_free_and_replace(l
, u
);
384 if (strv_isempty(l
)) {
385 if (unlink("/etc/vconsole.conf") < 0)
386 return errno
== ENOENT
? 0 : -errno
;
388 c
->vc_mtime
= USEC_INFINITY
;
392 r
= write_env_file_label("/etc/vconsole.conf", l
);
396 if (stat("/etc/vconsole.conf", &st
) >= 0)
397 c
->vc_mtime
= timespec_load(&st
.st_mtim
);
402 int x11_write_data(Context
*c
) {
403 _cleanup_fclose_
FILE *f
= NULL
;
404 _cleanup_free_
char *temp_path
= NULL
;
408 if (isempty(c
->x11_layout
) &&
409 isempty(c
->x11_model
) &&
410 isempty(c
->x11_variant
) &&
411 isempty(c
->x11_options
)) {
413 if (unlink("/etc/X11/xorg.conf.d/00-keyboard.conf") < 0)
414 return errno
== ENOENT
? 0 : -errno
;
416 c
->vc_mtime
= USEC_INFINITY
;
420 mkdir_p_label("/etc/X11/xorg.conf.d", 0755);
422 r
= fopen_temporary("/etc/X11/xorg.conf.d/00-keyboard.conf", &f
, &temp_path
);
426 (void) __fsetlocking(f
, FSETLOCKING_BYCALLER
);
427 (void) fchmod(fileno(f
), 0644);
429 fputs("# Written by systemd-localed(8), read by systemd-localed and Xorg. It's\n"
430 "# probably wise not to edit this file manually. Use localectl(1) to\n"
431 "# instruct systemd-localed to update it.\n"
432 "Section \"InputClass\"\n"
433 " Identifier \"system-keyboard\"\n"
434 " MatchIsKeyboard \"on\"\n", f
);
436 if (!isempty(c
->x11_layout
))
437 fprintf(f
, " Option \"XkbLayout\" \"%s\"\n", c
->x11_layout
);
439 if (!isempty(c
->x11_model
))
440 fprintf(f
, " Option \"XkbModel\" \"%s\"\n", c
->x11_model
);
442 if (!isempty(c
->x11_variant
))
443 fprintf(f
, " Option \"XkbVariant\" \"%s\"\n", c
->x11_variant
);
445 if (!isempty(c
->x11_options
))
446 fprintf(f
, " Option \"XkbOptions\" \"%s\"\n", c
->x11_options
);
448 fputs("EndSection\n", f
);
450 r
= fflush_sync_and_check(f
);
454 if (rename(temp_path
, "/etc/X11/xorg.conf.d/00-keyboard.conf") < 0) {
459 if (stat("/etc/X11/xorg.conf.d/00-keyboard.conf", &st
) >= 0)
460 c
->x11_mtime
= timespec_load(&st
.st_mtim
);
466 (void) unlink(temp_path
);
471 static int read_next_mapping(const char* filename
,
472 unsigned min_fields
, unsigned max_fields
,
473 FILE *f
, unsigned *n
, char ***a
) {
479 _cleanup_free_
char *line
= NULL
;
484 r
= read_line(f
, LONG_LINE_MAX
, &line
);
493 if (IN_SET(l
[0], 0, '#'))
496 r
= strv_split_extract(&b
, l
, WHITESPACE
, EXTRACT_QUOTES
);
500 length
= strv_length(b
);
501 if (length
< min_fields
|| length
> max_fields
) {
502 log_error("Invalid line %s:%u, ignoring.", filename
, *n
);
515 int vconsole_convert_to_x11(Context
*c
) {
519 map
= systemd_kbd_model_map();
521 if (isempty(c
->vc_keymap
)) {
523 !isempty(c
->x11_layout
) ||
524 !isempty(c
->x11_model
) ||
525 !isempty(c
->x11_variant
) ||
526 !isempty(c
->x11_options
);
530 _cleanup_fclose_
FILE *f
= NULL
;
533 f
= fopen(map
, "re");
538 _cleanup_strv_free_
char **a
= NULL
;
541 r
= read_next_mapping(map
, 5, UINT_MAX
, f
, &n
, &a
);
547 if (!streq(c
->vc_keymap
, a
[0]))
550 if (!streq_ptr(c
->x11_layout
, strnulldash(a
[1])) ||
551 !streq_ptr(c
->x11_model
, strnulldash(a
[2])) ||
552 !streq_ptr(c
->x11_variant
, strnulldash(a
[3])) ||
553 !streq_ptr(c
->x11_options
, strnulldash(a
[4]))) {
555 if (free_and_strdup(&c
->x11_layout
, strnulldash(a
[1])) < 0 ||
556 free_and_strdup(&c
->x11_model
, strnulldash(a
[2])) < 0 ||
557 free_and_strdup(&c
->x11_variant
, strnulldash(a
[3])) < 0 ||
558 free_and_strdup(&c
->x11_options
, strnulldash(a
[4])) < 0)
569 log_info("Changing X11 keyboard layout to '%s' model '%s' variant '%s' options '%s'",
570 strempty(c
->x11_layout
),
571 strempty(c
->x11_model
),
572 strempty(c
->x11_variant
),
573 strempty(c
->x11_options
));
574 else if (modified
< 0)
575 log_notice("X11 keyboard layout was not modified: no conversion found for \"%s\".",
578 log_debug("X11 keyboard layout did not need to be modified.");
583 int find_converted_keymap(const char *x11_layout
, const char *x11_variant
, char **new_keymap
) {
585 _cleanup_free_
char *n
;
588 n
= strjoin(x11_layout
, "-", x11_variant
);
590 n
= strdup(x11_layout
);
594 NULSTR_FOREACH(dir
, KBD_KEYMAP_DIRS
) {
595 _cleanup_free_
char *p
= NULL
, *pz
= NULL
;
598 p
= strjoin(dir
, "xkb/", n
, ".map");
599 pz
= strjoin(dir
, "xkb/", n
, ".map.gz");
603 uncompressed
= access(p
, F_OK
) == 0;
604 if (uncompressed
|| access(pz
, F_OK
) == 0) {
605 log_debug("Found converted keymap %s at %s",
606 n
, uncompressed
? p
: pz
);
608 *new_keymap
= TAKE_PTR(n
);
616 int find_legacy_keymap(Context
*c
, char **ret
) {
618 _cleanup_fclose_
FILE *f
= NULL
;
619 _cleanup_free_
char *new_keymap
= NULL
;
621 unsigned best_matching
= 0;
624 assert(!isempty(c
->x11_layout
));
626 map
= systemd_kbd_model_map();
628 f
= fopen(map
, "re");
633 _cleanup_strv_free_
char **a
= NULL
;
634 unsigned matching
= 0;
636 r
= read_next_mapping(map
, 5, UINT_MAX
, f
, &n
, &a
);
642 /* Determine how well matching this entry is */
643 if (streq(c
->x11_layout
, a
[1]))
644 /* If we got an exact match, this is best */
647 /* We have multiple X layouts, look for an
648 * entry that matches our key with everything
649 * but the first layout stripped off. */
650 if (startswith_comma(c
->x11_layout
, a
[1]))
655 /* If that didn't work, strip off the
656 * other layouts from the entry, too */
657 x
= strndupa(a
[1], strcspn(a
[1], ","));
658 if (startswith_comma(c
->x11_layout
, x
))
664 if (isempty(c
->x11_model
) || streq_ptr(c
->x11_model
, a
[2])) {
667 if (streq_ptr(c
->x11_variant
, a
[3])) {
670 if (streq_ptr(c
->x11_options
, a
[4]))
676 /* The best matching entry so far, then let's save that */
677 if (matching
>= MAX(best_matching
, 1u)) {
678 log_debug("Found legacy keymap %s with score %u",
681 if (matching
> best_matching
) {
682 best_matching
= matching
;
684 r
= free_and_strdup(&new_keymap
, a
[0]);
691 if (best_matching
< 10 && c
->x11_layout
) {
692 /* The best match is only the first part of the X11
693 * keymap. Check if we have a converted map which
694 * matches just the first layout.
696 char *l
, *v
= NULL
, *converted
;
698 l
= strndupa(c
->x11_layout
, strcspn(c
->x11_layout
, ","));
700 v
= strndupa(c
->x11_variant
, strcspn(c
->x11_variant
, ","));
701 r
= find_converted_keymap(l
, v
, &converted
);
705 free_and_replace(new_keymap
, converted
);
708 *ret
= TAKE_PTR(new_keymap
);
712 int find_language_fallback(const char *lang
, char **language
) {
714 _cleanup_fclose_
FILE *f
= NULL
;
720 map
= systemd_language_fallback_map();
722 f
= fopen(map
, "re");
727 _cleanup_strv_free_
char **a
= NULL
;
730 r
= read_next_mapping(map
, 2, 2, f
, &n
, &a
);
734 if (streq(lang
, a
[0])) {
735 assert(strv_length(a
) == 2);
736 *language
= TAKE_PTR(a
[1]);
741 assert_not_reached("should not be here");
744 int x11_convert_to_vconsole(Context
*c
) {
745 bool modified
= false;
747 if (isempty(c
->x11_layout
)) {
749 !isempty(c
->vc_keymap
) ||
750 !isempty(c
->vc_keymap_toggle
);
752 context_free_vconsole(c
);
754 _cleanup_free_
char *new_keymap
= NULL
;
757 r
= find_converted_keymap(c
->x11_layout
, c
->x11_variant
, &new_keymap
);
761 r
= find_legacy_keymap(c
, &new_keymap
);
766 /* We search for layout-variant match first, but then we also look
767 * for anything which matches just the layout. So it's accurate to say
768 * that we couldn't find anything which matches the layout. */
769 log_notice("No conversion to virtual console map found for \"%s\".",
772 if (!streq_ptr(c
->vc_keymap
, new_keymap
)) {
773 free_and_replace(c
->vc_keymap
, new_keymap
);
774 c
->vc_keymap_toggle
= mfree(c
->vc_keymap_toggle
);
780 log_info("Changing virtual console keymap to '%s' toggle '%s'",
781 strempty(c
->vc_keymap
), strempty(c
->vc_keymap_toggle
));
783 log_debug("Virtual console keymap was not modified.");