如何禁用 Linux 内核模块对键盘和鼠标的访问?
我正在尝试编写一个内核模块,该模块会在一天中的某些时间之间禁用输入。我找到了如何获取时间(如何获取 Linux 内核空间中的当前时间(一天中的时间))以及如何调度函数。 我似乎无法弄清楚如何禁用输入。我认为内核必须有某个地方可以做到这一点,但在阅读了 API 后我仍然没有进一步的进展。我想我应该直接访问驱动程序并将其关闭或其他什么,但这似乎有点不通用。 这可能吗?
感谢您抽出时间。
I'm trying to write a kernel module which disables input between certain times of day. I found out how to get the time (How to get current hour (time of day) in linux kernel space) and how to schedule a function.
I can't seem to work out how to disable the inputs though. I'm thinking there has to be some place the kernel to do this but having read the API I'm still no further forward. I suppose I should just access the drivers directly and shut them off or something, but that seems a little non-generic.
Is this even possible?
Thanks for your time.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不确定,但如果您在用户空间中,切换到未使用的虚拟控制台,然后将键盘置于原始模式应该就足够了。这将阻止通常切换回另一个虚拟控制台的组合键。这不会禁用鼠标,但如果不是当前的 VT,X 应该忽略鼠标(只需确保 gpm 没有运行)。
如果启用了 magic-sysrq 组合键,您还必须禁用它,因为有一个 sysrq 键可以使键盘退出原始模式,否则可以解决此问题。
编辑:如果您处于正常的任务上下文中,应该可以从内核空间完成所有这些操作。我希望内核线程就可以。
您可以从内核打开文件和设备,但不建议这样做。具有包含 /dev 的命名空间的任务(我不确定内核任务是否包含)。您可以调用 filp_open (我认为)并获取一个文件 *,然后您可以在其 file_operations (f_op) 上调用适当的方法。这应该包括执行上述操作所需的 ioctl。
可能有一种方法可以直接打开设备,而不是通过 filp_open。
简而言之,应该是可以的。在内核空间中这是一件非常危险的事情。
I'm not sure, but if you're in userspace, it should be sufficient to switch to an unused virtual console, then put the keyboard into raw mode. This will block the key combinations which would normally switch back into another virtual console. That won't disable the mouse, but X should ignore the mouse if it is not the current VT (just make sure gpm isn't running).
You also have to disable the magic-sysrq key combination if that is enabled, as there is a sysrq key to take the keyboard out of raw mode, which would otherwise get around this.
EDIT: it should be possible to do all of this from kernel space, provided you're in a normal task context. A kernel thread would do, I expect.
You can open files and devices from the kernel, it's just not recommended. A task which has a namespace containing /dev (I'm not sure whether kernel tasks do). You can call filp_open (I think) and get a file *, which you can then call the appropriate methods on its file_operations (f_op). This should include the ioctls required to do the above.
There might be a way of opening a device directly, not via the filp_open.
In short, it should be possible. It's a very dodgy thing to do from kernel space.
我能想到的一种方法是禁用关键字和鼠标的 IRQ。
disable_irq 和 enable_irq 可以用来做到这一点。
这是 x86 的 IRQ 编号。如果需要,您可能必须查阅其他表来获取其他平台(如 SPARC、IA-64)的 IRQ。
One way I can think of is disabling the IRQs for keyword and mouse.
disable_irq and enable_irq can be used to do that.
Here is IRQ numbers for x86. May be you have to consult other table to get IRQ for other platforms like SPARC, IA-64 if you need.