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 Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
32 #include <sys/ioctl.h>
34 #include <linux/tiocl.h>
42 static bool is_vconsole(int fd) {
43 unsigned char data[1];
45 data[0] = TIOCL_GETFGCONSOLE;
46 return ioctl(fd, TIOCLINUX, data) >= 0;
49 static int disable_utf8(int fd) {
52 if (ioctl(fd, KDSKBMODE, K_XLATE) < 0)
55 if (loop_write(fd, "\033%@", 3, false) < 0)
58 k = write_one_line_file("/sys/module/vt/parameters/default_utf8", "0");
63 log_warning("Failed to disable UTF-8: %s", strerror(-r));
68 static int enable_utf8(int fd) {
71 if (ioctl(fd, KDSKBMODE, K_UNICODE) < 0)
74 if (loop_write(fd, "\033%G", 3, false) < 0)
77 k = write_one_line_file("/sys/module/vt/parameters/default_utf8", "1");
82 log_warning("Failed to enable UTF-8: %s", strerror(-r));
87 static int load_keymap(const char *vc, const char *map, const char *map_toggle, bool utf8, pid_t *_pid) {
93 /* An empty map means kernel map */
98 args[i++] = KBD_LOADKEYS;
106 args[i++] = map_toggle;
111 log_error("Failed to fork: %m");
113 } else if (pid == 0) {
114 execv(args[0], (char **) args);
122 static int load_font(const char *vc, const char *font, const char *map, const char *unimap, pid_t *_pid) {
128 /* An empty font means kernel font */
133 args[i++] = KBD_SETFONT;
149 log_error("Failed to fork: %m");
151 } else if (pid == 0) {
152 execv(args[0], (char **) args);
160 int main(int argc, char **argv) {
162 char *vc_keymap = NULL;
163 char *vc_keymap_toggle = NULL;
164 char *vc_font = NULL;
165 char *vc_font_map = NULL;
166 char *vc_font_unimap = NULL;
169 int r = EXIT_FAILURE;
170 pid_t font_pid = 0, keymap_pid = 0;
172 log_set_target(LOG_TARGET_AUTO);
173 log_parse_environment();
183 fd = open_terminal(vc, O_RDWR|O_CLOEXEC);
185 log_error("Failed to open %s: %m", vc);
189 if (!is_vconsole(fd)) {
190 log_error("Device %s is not a virtual console.", vc);
194 utf8 = is_locale_utf8();
198 if (detect_container(NULL) <= 0) {
199 r = parse_env_file("/proc/cmdline", WHITESPACE,
200 "vconsole.keymap", &vc_keymap,
201 "vconsole.keymap.toggle", &vc_keymap_toggle,
202 "vconsole.font", &vc_font,
203 "vconsole.font.map", &vc_font_map,
204 "vconsole.font.unimap", &vc_font_unimap,
207 if (r < 0 && r != -ENOENT)
208 log_warning("Failed to read /proc/cmdline: %s", strerror(-r));
211 /* Hmm, nothing set on the kernel cmd line? Then let's
212 * try /etc/vconsole.conf */
214 r = parse_env_file("/etc/vconsole.conf", NEWLINE,
215 "KEYMAP", &vc_keymap,
216 "KEYMAP_TOGGLE", &vc_keymap_toggle,
218 "FONT_MAP", &vc_font_map,
219 "FONT_UNIMAP", &vc_font_unimap,
222 if (r < 0 && r != -ENOENT)
223 log_warning("Failed to read /etc/vconsole.conf: %s", strerror(-r));
237 if (load_keymap(vc, vc_keymap, vc_keymap_toggle, utf8, &keymap_pid) >= 0 &&
238 load_font(vc, vc_font, vc_font_map, vc_font_unimap, &font_pid) >= 0)
243 wait_for_terminate_and_warn(KBD_LOADKEYS, keymap_pid);
246 wait_for_terminate_and_warn(KBD_SETFONT, font_pid);
251 free(vc_font_unimap);
254 close_nointr_nofail(fd);