进一步阅读

进一步阅读

使用Alt+Fn您可以在大多数 Linux 发行版中的虚拟控制台之间切换。什么应用程序处理控制台的切换以及如何处理?我想它必须在所有其他进程之前读取键盘输入。或者它是由设备驱动程序或另一个内核模块处理的?

答案1

Linux 内核中内置了一个终端仿真器程序。它并不表现为具有打开文件句柄的正在运行的进程。它位于帧缓冲区和输入事件子系统之上,它使用内部内核接口来访问。它向应用程序模式系统呈现为一系列内核虚拟终端设备/dev/tty1等,下面有一个伪文件/sys,显示活动的 KVT 编号,以及一系列 CGA 风格的视频缓冲设备,/dev/vcsa1等等。

通常,是内核终端模拟器识别⎇ Alt+键和弦。这一切完全在内核模式代码内完成。 (您可以使用内核构建选项构建没有此代码的内核。)FNCONFIG_VT

但是,应用程序软件可以禁用此功能。例如,Xorg 服务器就是这样做的。当它在屏幕上处于活动状态时,它会暂时关闭或断开大部分内核终端仿真器的连接,识别它自己的键和弦 ( ⎈ Control+ ⎇ Alt+ ),并使用系统调用在程序控制下切换活动 KVT。实际上,Xorg 服务器使用 KVT 切换作为协商对帧缓冲区和与内核内置终端仿真器共享的 HID 的独占访问的手段。FNioctl()

进一步阅读

答案2

在基于 systemd 的发行版(例如 Enterprise Linux 7 和 8)中,有 systemd-getty-generator。您可以在 Lennart Poettering 博客上阅读有关此解决方案的更多信息:http://0pointer.de/blog/projects/serial-console.html以及在免费桌面上https://www.freedesktop.org/software/systemd/man/systemd-getty-generator.html

简单的测试。控制台在ctrl+ alt+上启动F2

[root@SpaceStation ~]# systemctl list-units  | grep getty
  [email protected]                                                                       loaded active running   Getty on tty2
  system-getty.slice                                                                       loaded active active    system-getty.slice
  getty.target                                                                             loaded active active    Login Prompts

进入第三个控制台(ctrl++ alt)后F3

[root@SpaceStation ~]# systemctl list-units  | grep getty
  [email protected]                                                                       loaded active running   Getty on tty2
  [email protected]                                                                       loaded active running   Getty on tty3
  system-getty.slice                                                                       loaded active active    system-getty.slice
  getty.target    

生成的服务文件如下所示:

cat /usr/lib/systemd/system/[email protected]
#  This file is part of systemd.
#
#  systemd is free software; you can redistribute it and/or modify it
#  under the terms of the GNU Lesser General Public License as published by
#  the Free Software Foundation; either version 2.1 of the License, or
#  (at your option) any later version.

[Unit]
Description=Getty on %I
Documentation=man:agetty(8) man:systemd-getty-generator(8)
Documentation=http://0pointer.de/blog/projects/serial-console.html
After=systemd-user-sessions.service plymouth-quit-wait.service getty-pre.target
After=rc-local.service

# If additional gettys are spawned during boot then we should make
# sure that this is synchronized before getty.target, even though
# getty.target didn't actually pull it in.
Before=getty.target
IgnoreOnIsolate=yes

# On systems without virtual consoles, don't start any getty. Note
# that serial gettys are covered by [email protected], not this
# unit.
ConditionPathExists=/dev/tty0

[Service]
# the VT is cleared by TTYVTDisallocate
ExecStart=-/sbin/agetty --noclear %I $TERM
Type=idle
Restart=always
RestartSec=0
UtmpIdentifier=%I
TTYPath=/dev/%I
TTYReset=yes
TTYVHangup=yes
TTYVTDisallocate=yes
KillMode=process
IgnoreSIGPIPE=no
SendSIGHUP=yes

# Unset locale for the console getty since the console has problems
# displaying some internationalized messages.
Environment=LANG= LANGUAGE= LC_CTYPE= LC_NUMERIC= LC_TIME= LC_COLLATE= LC_MONETARY= LC_MESSAGES= LC_PAPER= LC_NAME= LC_ADDRESS= LC_TELEPHONE= LC_MEASUREMENT= LC_IDENTIFICATION=

[Install]
WantedBy=getty.target
DefaultInstance=tty

最重要的一行是ExecStart=-/sbin/agetty --noclear %I $TERM负责启动终端。

相关内容