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>
44 static bool is_vconsole(int fd) {
45 unsigned char data[1];
47 data[0] = TIOCL_GETFGCONSOLE;
48 return ioctl(fd, TIOCLINUX, data) >= 0;
51 static int disable_utf8(int fd) {
54 if (ioctl(fd, KDSKBMODE, K_XLATE) < 0)
57 if (loop_write(fd, "\033%@", 3, false) < 0)
60 k = write_string_file("/sys/module/vt/parameters/default_utf8", "0");
65 log_warning("Failed to disable UTF-8: %s", strerror(-r));
70 static int enable_utf8(int fd) {
74 if (ioctl(fd, KDGKBMODE, ¤t) < 0 || current == K_XLATE) {
76 * Change the current keyboard to unicode, unless it
77 * is currently in raw or off mode anyway. We
78 * shouldn't interfere with X11's processing of the
81 * http://lists.freedesktop.org/archives/systemd-devel/2013-February/008573.html
85 if (ioctl(fd, KDSKBMODE, K_UNICODE) < 0)
89 if (loop_write(fd, "\033%G", 3, false) < 0)
92 k = write_string_file("/sys/module/vt/parameters/default_utf8", "1");
97 log_warning("Failed to enable UTF-8: %s", strerror(-r));
102 static int keymap_load(const char *vc, const char *map, const char *map_toggle, bool utf8, pid_t *_pid) {
108 /* An empty map means kernel map */
113 args[i++] = KBD_LOADKEYS;
121 args[i++] = map_toggle;
126 log_error("Failed to fork: %m");
128 } else if (pid == 0) {
129 execv(args[0], (char **) args);
137 static int font_load(const char *vc, const char *font, const char *map, const char *unimap, pid_t *_pid) {
143 /* An empty font means kernel font */
148 args[i++] = KBD_SETFONT;
164 log_error("Failed to fork: %m");
166 } else if (pid == 0) {
167 execv(args[0], (char **) args);
176 * A newly allocated VT uses the font from the active VT. Here
177 * we update all possibly already allocated VTs with the configured
178 * font. It also allows to restart systemd-vconsole-setup.service,
179 * to apply a new font to all VTs.
181 static void font_copy_to_all_vcs(int fd) {
186 /* get active, and 16 bit mask of used VT numbers */
188 r = ioctl(fd, VT_GETSTATE, &vcs);
192 for (i = 1; i <= 15; i++) {
194 struct console_font_op cfo;
195 int _cleanup_close_ vcfd = -1;
197 if (i == vcs.v_active)
200 /* skip non-allocated ttys */
201 snprintf(vcname, sizeof(vcname), "/dev/vcs%i", i);
202 if (access(vcname, F_OK) < 0)
205 snprintf(vcname, sizeof(vcname), "/dev/tty%i", i);
206 vcfd = open_terminal(vcname, O_RDWR|O_CLOEXEC);
210 /* copy font from active VT, where the font was uploaded to */
212 cfo.op = KD_FONT_OP_COPY;
213 cfo.height = vcs.v_active-1; /* tty1 == index 0 */
214 ioctl(vcfd, KDFONTOP, &cfo);
218 int main(int argc, char **argv) {
220 char *vc_keymap = NULL;
221 char *vc_keymap_toggle = NULL;
222 char *vc_font = NULL;
223 char *vc_font_map = NULL;
224 char *vc_font_unimap = NULL;
227 pid_t font_pid = 0, keymap_pid = 0;
228 bool font_copy = false;
229 int r = EXIT_FAILURE;
231 log_set_target(LOG_TARGET_AUTO);
232 log_parse_environment();
244 fd = open_terminal(vc, O_RDWR|O_CLOEXEC);
246 log_error("Failed to open %s: %m", vc);
250 if (!is_vconsole(fd)) {
251 log_error("Device %s is not a virtual console.", vc);
255 utf8 = is_locale_utf8();
259 if (detect_container(NULL) <= 0) {
260 r = parse_env_file("/proc/cmdline", WHITESPACE,
261 "vconsole.keymap", &vc_keymap,
262 "vconsole.keymap.toggle", &vc_keymap_toggle,
263 "vconsole.font", &vc_font,
264 "vconsole.font.map", &vc_font_map,
265 "vconsole.font.unimap", &vc_font_unimap,
268 if (r < 0 && r != -ENOENT)
269 log_warning("Failed to read /proc/cmdline: %s", strerror(-r));
272 /* Hmm, nothing set on the kernel cmd line? Then let's
273 * try /etc/vconsole.conf */
275 r = parse_env_file("/etc/vconsole.conf", NEWLINE,
276 "KEYMAP", &vc_keymap,
277 "KEYMAP_TOGGLE", &vc_keymap_toggle,
279 "FONT_MAP", &vc_font_map,
280 "FONT_UNIMAP", &vc_font_unimap,
283 if (r < 0 && r != -ENOENT)
284 log_warning("Failed to read /etc/vconsole.conf: %s", strerror(-r));
293 if (keymap_load(vc, vc_keymap, vc_keymap_toggle, utf8, &keymap_pid) >= 0 &&
294 font_load(vc, vc_font, vc_font_map, vc_font_unimap, &font_pid) >= 0)
299 wait_for_terminate_and_warn(KBD_LOADKEYS, keymap_pid);
302 wait_for_terminate_and_warn(KBD_SETFONT, font_pid);
304 font_copy_to_all_vcs(fd);
310 free(vc_font_unimap);
313 close_nointr_nofail(fd);