繁体   English   中英

如何从内核模块写入TTY?

[英]How can I write to TTY from a kernel module?

首先发布到SO,所以我会尝试将问题做对。

我正在制作一个简单的Linux内核模块,目的是将数据回送到加载内核模块的TTY shell。 我遇到的问题是内核“Ooops” - 带有以下消息(用“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下,因此是您的模块或代码

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM