]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/vconsole/vconsole-setup.c
vconsole: Add generic is_*() functions
[thirdparty/systemd.git] / src / vconsole / vconsole-setup.c
CommitLineData
97c4a07d
LP
1/***
2 This file is part of systemd.
3
4 Copyright 2010 Kay Sievers
5
6 systemd is free software; you can redistribute it and/or modify it
5430f7f2
LP
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
97c4a07d
LP
9 (at your option) any later version.
10
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
5430f7f2 14 Lesser General Public License for more details.
97c4a07d 15
5430f7f2 16 You should have received a copy of the GNU Lesser General Public License
97c4a07d
LP
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
18***/
19
97c4a07d 20#include <errno.h>
97c4a07d 21#include <fcntl.h>
97c4a07d 22#include <limits.h>
97c4a07d 23#include <linux/kd.h>
07630cea 24#include <linux/tiocl.h>
dd04aac9 25#include <linux/vt.h>
07630cea
LP
26#include <stdbool.h>
27#include <stdio.h>
28#include <stdlib.h>
29#include <sys/ioctl.h>
042d7f50 30#include <termios.h>
07630cea 31#include <unistd.h>
97c4a07d 32
b5efdb8a 33#include "alloc-util.h"
3ffd4af2 34#include "fd-util.h"
a5c32cff 35#include "fileio.h"
c004493c 36#include "io-util.h"
8752c575 37#include "locale-util.h"
07630cea 38#include "log.h"
0b452006 39#include "process-util.h"
ce30c8dc 40#include "signal-util.h"
d054f0a4 41#include "stdio-util.h"
07630cea
LP
42#include "string-util.h"
43#include "terminal-util.h"
44#include "util.h"
45#include "virt.h"
97c4a07d 46
653ab83b 47static bool is_vconsole(int fd) {
97c4a07d
LP
48 unsigned char data[1];
49
50 data[0] = TIOCL_GETFGCONSOLE;
51 return ioctl(fd, TIOCLINUX, data) >= 0;
97c4a07d
LP
52}
53
03044059
MS
54static bool is_allocated(unsigned int idx) {
55 char vcname[strlen("/dev/vcs") + DECIMAL_STR_MAX(int)];
56
57 xsprintf(vcname, "/dev/vcs%i", idx);
58 return access(vcname, F_OK) == 0;
59}
60
61static bool is_allocated_byfd(int fd) {
62 struct vt_stat vcs = {};
63
64 if (ioctl(fd, VT_GETSTATE, &vcs) < 0) {
65 log_warning_errno(errno, "VT_GETSTATE failed: %m");
66 return false;
67 }
68 return is_allocated(vcs.v_active);
69}
70
71static bool is_settable(int fd) {
72 int r, curr_mode;
73
74 r = ioctl(fd, KDGKBMODE, &curr_mode);
75 /*
76 * Make sure we only adjust consoles in K_XLATE or K_UNICODE mode.
77 * Oterwise we would (likely) interfere with X11's processing of the
78 * key events.
79 *
80 * http://lists.freedesktop.org/archives/systemd-devel/2013-February/008573.html
81 */
82 return r == 0 && IN_SET(curr_mode, K_XLATE, K_UNICODE);
83}
84
042d7f50
MS
85static int toggle_utf8(int fd, bool utf8) {
86 int r;
87 struct termios tc = {};
97c4a07d 88
042d7f50 89 r = ioctl(fd, KDSKBMODE, utf8 ? K_UNICODE : K_XLATE);
97c4a07d 90 if (r < 0)
042d7f50 91 return log_warning_errno(errno, "Failed to %s UTF-8 kbdmode: %m", utf8 ? "enable" : "disable");
97c4a07d 92
042d7f50
MS
93 r = loop_write(fd, utf8 ? "\033%G" : "\033%@", 3, false);
94 if (r < 0)
95 return log_warning_errno(r, "Failed to %s UTF-8 term processing: %m", utf8 ? "enable" : "disable");
96
97 r = tcgetattr(fd, &tc);
98 if (r >= 0) {
99 if (utf8)
100 tc.c_iflag |= IUTF8;
101 else
102 tc.c_iflag &= ~IUTF8;
103 r = tcsetattr(fd, TCSANOW, &tc);
a25d4d0e 104 }
042d7f50
MS
105 if (r < 0)
106 return log_warning_errno(errno, "Failed to %s iutf8 flag: %m", utf8 ? "enable" : "disable");
d305a67b 107
042d7f50
MS
108 return 0;
109}
d305a67b 110
042d7f50
MS
111static int toggle_utf8_sysfs(bool utf8) {
112 int r;
d305a67b 113
042d7f50 114 r = write_string_file("/sys/module/vt/parameters/default_utf8", one_zero(utf8), 0);
d305a67b 115 if (r < 0)
042d7f50 116 log_warning_errno(r, "Failed to %s sysfs UTF-8 flag: %m", utf8 ? "enable" : "disable");
d305a67b
TG
117 return r;
118}
119
aecb6fcb 120static int keyboard_load_and_wait(const char *vc, const char *map, const char *map_toggle, bool utf8) {
5b396b06 121 const char *args[8];
aecb6fcb 122 int i = 0, r;
97c4a07d
LP
123 pid_t pid;
124
8931278c
LDM
125 /* An empty map means kernel map */
126 if (isempty(map))
aecb6fcb 127 return 1;
944d4c91 128
9841e8e3 129 args[i++] = KBD_LOADKEYS;
97c4a07d
LP
130 args[i++] = "-q";
131 args[i++] = "-C";
132 args[i++] = vc;
133 if (utf8)
134 args[i++] = "-u";
135 args[i++] = map;
5b396b06
AB
136 if (map_toggle)
137 args[i++] = map_toggle;
97c4a07d
LP
138 args[i++] = NULL;
139
741f8cf6 140 pid = fork();
aecb6fcb
LP
141 if (pid < 0)
142 return log_error_errno(errno, "Failed to fork: %m");
143 else if (pid == 0) {
ce30c8dc
LP
144
145 (void) reset_all_signal_handlers();
146 (void) reset_signal_mask();
147
97c4a07d
LP
148 execv(args[0], (char **) args);
149 _exit(EXIT_FAILURE);
150 }
151
aecb6fcb
LP
152 r = wait_for_terminate_and_warn(KBD_LOADKEYS, pid, true);
153 if (r < 0)
154 return r;
155
156 return r == 0;
97c4a07d
LP
157}
158
aecb6fcb 159static int font_load_and_wait(const char *vc, const char *font, const char *map, const char *unimap) {
97c4a07d 160 const char *args[9];
aecb6fcb 161 int i = 0, r;
97c4a07d
LP
162 pid_t pid;
163
8931278c
LDM
164 /* An empty font means kernel font */
165 if (isempty(font))
aecb6fcb 166 return 1;
944d4c91 167
9841e8e3 168 args[i++] = KBD_SETFONT;
97c4a07d
LP
169 args[i++] = "-C";
170 args[i++] = vc;
171 args[i++] = font;
dd36de4d 172 if (map) {
97c4a07d
LP
173 args[i++] = "-m";
174 args[i++] = map;
175 }
dd36de4d 176 if (unimap) {
97c4a07d
LP
177 args[i++] = "-u";
178 args[i++] = unimap;
179 }
180 args[i++] = NULL;
181
741f8cf6 182 pid = fork();
aecb6fcb
LP
183 if (pid < 0)
184 return log_error_errno(errno, "Failed to fork: %m");
185 else if (pid == 0) {
ce30c8dc
LP
186
187 (void) reset_all_signal_handlers();
188 (void) reset_signal_mask();
189
97c4a07d
LP
190 execv(args[0], (char **) args);
191 _exit(EXIT_FAILURE);
192 }
193
aecb6fcb
LP
194 r = wait_for_terminate_and_warn(KBD_SETFONT, pid, true);
195 if (r < 0)
196 return r;
197
198 return r == 0;
97c4a07d
LP
199}
200
d3b37e84
KS
201/*
202 * A newly allocated VT uses the font from the active VT. Here
203 * we update all possibly already allocated VTs with the configured
204 * font. It also allows to restart systemd-vconsole-setup.service,
205 * to apply a new font to all VTs.
206 */
207static void font_copy_to_all_vcs(int fd) {
b92bea5d 208 struct vt_stat vcs = {};
ff452e76 209 struct unimapdesc unimapd;
6a08e1b0 210 _cleanup_free_ struct unipair* unipairs = NULL;
b92bea5d 211 int i, r;
dd04aac9 212
6a08e1b0 213 unipairs = new(struct unipair, USHRT_MAX);
e2c9192a
LP
214 if (!unipairs) {
215 log_oom();
6a08e1b0
KR
216 return;
217 }
218
d3b37e84 219 /* get active, and 16 bit mask of used VT numbers */
d3b37e84 220 r = ioctl(fd, VT_GETSTATE, &vcs);
ab51b943
LP
221 if (r < 0) {
222 log_debug_errno(errno, "VT_GETSTATE failed, ignoring: %m");
dd04aac9 223 return;
ab51b943 224 }
dd04aac9 225
9fa71843 226 for (i = 1; i <= 63; i++) {
ab51b943 227 char vcname[strlen("/dev/vcs") + DECIMAL_STR_MAX(int)];
7fd1b19b 228 _cleanup_close_ int vcfd = -1;
b92bea5d 229 struct console_font_op cfo = {};
dd04aac9 230
d3b37e84 231 if (i == vcs.v_active)
dd04aac9
KS
232 continue;
233
10ffbc99 234 /* skip non-allocated ttys */
d054f0a4 235 xsprintf(vcname, "/dev/vcs%i", i);
10ffbc99 236 if (access(vcname, F_OK) < 0)
dd04aac9
KS
237 continue;
238
d054f0a4 239 xsprintf(vcname, "/dev/tty%i", i);
d3b37e84
KS
240 vcfd = open_terminal(vcname, O_RDWR|O_CLOEXEC);
241 if (vcfd < 0)
dd04aac9
KS
242 continue;
243
d3b37e84 244 /* copy font from active VT, where the font was uploaded to */
dd04aac9 245 cfo.op = KD_FONT_OP_COPY;
d3b37e84 246 cfo.height = vcs.v_active-1; /* tty1 == index 0 */
791a4fd8 247 (void) ioctl(vcfd, KDFONTOP, &cfo);
ff452e76 248
ff452e76
CS
249 /* copy unicode translation table */
250 /* unimapd is a ushort count and a pointer to an
251 array of struct unipair { ushort, ushort } */
252 unimapd.entries = unipairs;
253 unimapd.entry_ct = USHRT_MAX;
254 if (ioctl(fd, GIO_UNIMAP, &unimapd) >= 0) {
255 struct unimapinit adv = { 0, 0, 0 };
256
791a4fd8
TA
257 (void) ioctl(vcfd, PIO_UNIMAPCLR, &adv);
258 (void) ioctl(vcfd, PIO_UNIMAP, &unimapd);
ff452e76 259 }
dd04aac9
KS
260 }
261}
262
97c4a07d
LP
263int main(int argc, char **argv) {
264 const char *vc;
abee28c5
ZJS
265 _cleanup_free_ char
266 *vc_keymap = NULL, *vc_keymap_toggle = NULL,
267 *vc_font = NULL, *vc_font_map = NULL, *vc_font_unimap = NULL;
268 _cleanup_close_ int fd = -1;
ab51b943 269 bool utf8, font_copy = false, font_ok, keyboard_ok;
dd04aac9 270 int r = EXIT_FAILURE;
97c4a07d 271
944d4c91 272 log_set_target(LOG_TARGET_AUTO);
97c4a07d
LP
273 log_parse_environment();
274 log_open();
275
4c12626c
LP
276 umask(0022);
277
97c4a07d
LP
278 if (argv[1])
279 vc = argv[1];
dd04aac9 280 else {
d3b37e84
KS
281 vc = "/dev/tty0";
282 font_copy = true;
dd04aac9 283 }
97c4a07d 284
741f8cf6
LP
285 fd = open_terminal(vc, O_RDWR|O_CLOEXEC);
286 if (fd < 0) {
709f6e46 287 log_error_errno(fd, "Failed to open %s: %m", vc);
abee28c5 288 return EXIT_FAILURE;
97c4a07d
LP
289 }
290
653ab83b 291 if (!is_vconsole(fd)) {
97c4a07d 292 log_error("Device %s is not a virtual console.", vc);
abee28c5 293 return EXIT_FAILURE;
97c4a07d
LP
294 }
295
03044059
MS
296 if (!is_allocated_byfd(fd)) {
297 log_error("Virtual console %s is not allocated.", vc);
298 return EXIT_FAILURE;
299 }
300
301 if (!is_settable(fd)) {
302 log_error("Virtual console %s is not in K_XLATE or K_UNICODE.", vc);
303 return EXIT_FAILURE;
304 }
305
653ab83b 306 utf8 = is_locale_utf8();
97c4a07d 307
2034ec42
MS
308 r = parse_env_file("/etc/vconsole.conf", NEWLINE,
309 "KEYMAP", &vc_keymap,
310 "KEYMAP_TOGGLE", &vc_keymap_toggle,
311 "FONT", &vc_font,
312 "FONT_MAP", &vc_font_map,
313 "FONT_UNIMAP", &vc_font_unimap,
314 NULL);
315
316 if (r < 0 && r != -ENOENT)
da927ba9 317 log_warning_errno(r, "Failed to read /etc/vconsole.conf: %m");
2034ec42
MS
318
319 /* Let the kernel command line override /etc/vconsole.conf */
75f86906 320 if (detect_container() <= 0) {
741f8cf6
LP
321 r = parse_env_file("/proc/cmdline", WHITESPACE,
322 "vconsole.keymap", &vc_keymap,
323 "vconsole.keymap.toggle", &vc_keymap_toggle,
324 "vconsole.font", &vc_font,
325 "vconsole.font.map", &vc_font_map,
326 "vconsole.font.unimap", &vc_font_unimap,
327 NULL);
328
329 if (r < 0 && r != -ENOENT)
da927ba9 330 log_warning_errno(r, "Failed to read /proc/cmdline: %m");
741f8cf6 331 }
1ebdf5b6 332
042d7f50
MS
333 toggle_utf8_sysfs(utf8);
334 toggle_utf8(fd, utf8);
653ab83b 335
aecb6fcb
LP
336 font_ok = font_load_and_wait(vc, vc_font, vc_font_map, vc_font_unimap) > 0;
337 keyboard_ok = keyboard_load_and_wait(vc, vc_keymap, vc_keymap_toggle, utf8) > 0;
97c4a07d 338
8931278c
LDM
339 /* Only copy the font when we executed setfont successfully */
340 if (font_copy && font_ok)
ab51b943 341 (void) font_copy_to_all_vcs(fd);
97c4a07d 342
8931278c 343 return font_ok && keyboard_ok ? EXIT_SUCCESS : EXIT_FAILURE;
97c4a07d 344}