gpt4 book ai didi

linux - 如何从内核模块写入 TTY?

转载 作者:IT王子 更新时间:2023-10-29 00:49:42 27 4
gpt4 key购买 nike

第一次发帖给 SO,所以我会尽量把问题弄对。

我正在制作一个简单的 Linux 内核模块,目的是将数据从加载内核模块的位置回显到 TTY shell。我遇到的问题是内核“Ooops”-ing 出现以下消息(被“watch 'dmesg | tail -50'”捕获)。内核模块的名称是 Seraphim:

[  184.222748] SELinux: initialized (dev proc, type proc), uses genfs_contexts
[ 1877.456607] seraphim: module verification failed: signature and/or required key missing - tainting kernel
[ 1877.457321] ------------------
[ 1877.457324] Seraphim started.
[ 1877.457348] BUG: unable to handle kernel NULL pointer dereference at 0000000000000218
[ 1877.457411] IP: [<ffffffffa0012030>] seraphim_entry+0x30/0x1000 [seraphim]
[ 1877.457462] PGD 115a2e067 PUD 10aca8067 PMD 0
[ 1877.457498] Oops: 0000 [#1] SMP
[ 1877.457524] Modules linked in: seraphim(OF+) rfcomm bnep nf_conntrack_netbios_ns nf_conn track_broadcast ipt_MASQUERADE ip6t_REJECT xt_conntrack ebtable_nat ebtable_broute bridge stp llce btable_filter ebtables ip6table_nat nf_conntrack_ipv6 nf_defrag_ipv6 nf_nat_ipv6 ip6table_ma etc.

向TTY终端写入数据的代码如下:

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/sched.h>
#include <linux/tty.h>

static void printString(char *string) {

struct tty_struct *tty;

tty = current->signal->tty;

if(tty != NULL) {

(tty->driver->ops->write) (tty, string, strlen(string));
}

else
printk("tty equals to zero");
}

我做错了什么?

我正在按照 http://www.tldp.org/LDP/lkmpg/2.6/lkmpg.pdf 上的教程进行操作,但它已经过时(我使用的内核是 Fedora 19 上的 3.11.10-200),所以我不得不翻阅 3.11.10-200 源文件以找到足够的结构。

最佳答案

使用 tty = get_current_tty(); 代替 tty = current->signal->tty;

就是这样

您需要在访问之前锁定 tty,get_current_tty 在内部执行此操作

注意:get_current_ttyEXPORT_SYMBOL_GPL 下,因此您的模块或代码

关于linux - 如何从内核模块写入 TTY?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20719713/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com