如何从内核模块写入TTY? [英] How can I write to TTY from a kernel module?

查看:199
本文介绍了如何从内核模块写入TTY?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

第一篇SO文章,所以我会尽力解决这个问题.

First post to SO, so I'll try to make the question right.

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

I'm making a simple Linux kernel module with the goal of echoing data back to the TTY shell from where the kernel module was loaded. The problem I having is the kernel "Ooops"-ing with the following message (caught with " watch 'dmesg | tail -50' "). The kernel module's name is 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终端的代码如下:

The code used for writing data to the TTY terminal follows:

#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源文件以找到适当的结构.

I was following the tutorial at http://www.tldp.org/LDP/lkmpg/2.6/lkmpg.pdf but it was out of date (the kernel I am using is 3.11.10-200 on Fedora 19), so I had to rummage through 3.11.10-200 source files to find the adequate structures.

推荐答案

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

就这样

您需要在访问tty之前先对其进行锁定,然后get_current_tty在内部对其进行操作

you need to lock the tty before accessing it and get_current_tty does it internally

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

NOTE: get_current_tty is under EXPORT_SYMBOL_GPL, hence your module or code

这篇关于如何从内核模块写入TTY?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆