1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2010 Kay Sievers
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
34 #include <sys/ioctl.h>
36 #include <linux/tiocl.h>
43 static bool is_console(int fd) {
44 unsigned char data[1];
46 data[0] = TIOCL_GETFGCONSOLE;
47 return ioctl(fd, TIOCLINUX, data) >= 0;
50 static bool is_locale_utf8(void) {
53 if (!setlocale(LC_ALL, ""))
56 set = nl_langinfo(CODESET);
60 return streq(set, "UTF-8");
63 static int disable_utf8(int fd) {
66 if (ioctl(fd, KDSKBMODE, K_XLATE) < 0)
69 if (loop_write(fd, "\033%@", 3, false) < 0)
72 if ((k = write_one_line_file("/sys/module/vt/parameters/default_utf8", "0")) < 0)
76 log_warning("Failed to disable UTF-8: %s", strerror(errno));
81 static int load_keymap(const char *vc, const char *map, bool utf8, pid_t *_pid) {
86 args[i++] = KBD_LOADKEYS;
95 if ((pid = fork()) < 0) {
96 log_error("Failed to fork: %m");
98 } else if (pid == 0) {
99 execv(args[0], (char **) args);
107 static int load_font(const char *vc, const char *font, const char *map, const char *unimap, pid_t *_pid) {
112 args[i++] = KBD_SETFONT;
126 if ((pid = fork()) < 0) {
127 log_error("Failed to fork: %m");
129 } else if (pid == 0) {
130 execv(args[0], (char **) args);
138 int main(int argc, char **argv) {
140 char *vc_keymap = NULL;
141 char *vc_font = NULL;
142 char *vc_font_map = NULL;
143 char *vc_font_unimap = NULL;
145 char *vc_unicode = NULL;
149 int r = EXIT_FAILURE;
150 pid_t font_pid = 0, keymap_pid = 0;
152 log_set_target(LOG_TARGET_SYSLOG_OR_KMSG);
153 log_parse_environment();
161 if ((fd = open(vc, O_RDWR|O_CLOEXEC)) < 0) {
162 log_error("Failed to open %s: %m", vc);
166 if (!is_console(fd)) {
167 log_error("Device %s is not a virtual console.", vc);
171 if (!(utf8 = is_locale_utf8()))
175 if ((r = parse_env_file("/etc/sysconfig/i18n", NEWLINE,
177 "SYSFONTACM", &vc_font_map,
178 "UNIMAP", &vc_font_unimap,
182 log_warning("Failed to read /etc/sysconfig/i18n: %s", strerror(-r));
185 if ((r = parse_env_file("/etc/sysconfig/keyboard", NEWLINE,
186 "KEYTABLE", &vc_keymap,
187 "KEYMAP", &vc_keymap,
191 log_warning("Failed to read /etc/sysconfig/i18n: %s", strerror(-r));
194 if (access("/etc/sysconfig/console/default.kmap", F_OK) >= 0) {
197 if (!(t = strdup("/etc/sysconfig/console/default.kmap"))) {
198 log_error("Out of memory.");
205 #elif defined(TARGET_ARCH)
206 if ((r = parse_env_file("/etc/rc.conf", NEWLINE,
207 "KEYMAP", &vc_keymap,
208 "CONSOLEFONT", &vc_font,
209 "CONSOLEMAP", &vc_font_map,
213 log_warning("Failed to read /etc/rc.conf: %s", strerror(-r));
215 #elif defined(TARGET_GENTOO)
216 if ((r = parse_env_file("/etc/rc.conf", NEWLINE,
217 "unicode", &vc_unicode,
220 log_warning("Failed to read /etc/rc.conf: %s", strerror(-r));
223 int rc_unicode = parse_boolean(vc_unicode);
225 log_error("Unknown value for /etc/rc.conf unicode=%s", vc_unicode);
227 if (rc_unicode && !utf8)
228 log_warning("/etc/rc.conf wants unicode, but current locale is not UTF-8 capable!");
229 else if (!rc_unicode && utf8) {
230 log_debug("/etc/rc.conf does not want unicode, leave it on in kernel but does not apply to vconsole.");
236 /* /etc/conf.d/consolefont comments and gentoo documentation
237 * mention uppercase, but the actual contents are lowercase.
238 * the existing /etc/init.d/consolefont tries both
240 if ((r = parse_env_file("/etc/conf.d/consolefont", NEWLINE,
241 "CONSOLEFONT", &vc_font,
242 "consolefont", &vc_font,
243 "consoletranslation", &vc_font_map,
244 "CONSOLETRANSLATION", &vc_font_map,
245 "unicodemap", &vc_font_unimap,
246 "UNICODEMAP", &vc_font_unimap,
249 log_warning("Failed to read /etc/conf.d/consolefont: %s", strerror(-r));
252 if ((r = parse_env_file("/etc/conf.d/keymaps", NEWLINE,
253 "keymap", &vc_keymap,
254 "KEYMAP", &vc_keymap,
257 log_warning("Failed to read /etc/conf.d/keymaps: %s", strerror(-r));
261 /* Override distribution-specific options with the
262 * distribution-independent configuration */
263 if ((r = parse_env_file("/etc/vconsole", NEWLINE,
264 "KEYMAP", &vc_keymap,
266 "FONT_MAP", &vc_font_map,
267 "FONT_UNIMAP", &vc_font_unimap,
271 log_warning("Failed to read /etc/vconsole: %s", strerror(-r));
274 if ((r = parse_env_file("/proc/cmdline", WHITESPACE,
277 "KEYTABLE", &vc_keymap,
279 "vconsole.keymap", &vc_keymap,
280 "vconsole.font", &vc_font,
281 "vconsole.font.map", &vc_font_map,
282 "vconsole.font.unimap", &vc_font_unimap,
286 log_warning("Failed to read /proc/cmdline: %s", strerror(-r));
290 vc_keymap = strdup("us");
292 vc_font = strdup(DEFAULT_FONT);
294 if (!vc_keymap || !vc_font) {
295 log_error("Failed to allocate strings.");
299 if (load_keymap(vc, vc_keymap, utf8, &keymap_pid) >= 0 &&
300 load_font(vc, vc_font, vc_font_map, vc_font_unimap, &font_pid) >= 0)
305 wait_for_terminate_and_warn(KBD_LOADKEYS, keymap_pid);
308 wait_for_terminate_and_warn(KBD_SETFONT, font_pid);
313 free(vc_font_unimap);
316 close_nointr_nofail(fd);