iT邦幫忙

DAY 27
0

Linux kernel模組的開發系列 第 27

輸出訊息到當下 tty 的範例

tty (Teletype) 指的是跟系統溝通的輸出入介面,它可以是連接主機的 terminal (螢幕 + 鍵盤)、X-Window,或是網路連結的 ssh/telnet 程式。如果我們想要讓訊息輸出到當前 module 被呼叫的 tty 上時,必須藉由 current 指標去獲得目前 tty 的資訊。current 是一個指標,他隨時指向當下執行系統呼叫的程式。

之前介紹的 The Linux Kernel Module Programming Guide 網站有提供一個範例 (http://www.tldp.org/LDP/lkmpg/2.6/html/x1161.html#FTN.AEN1169),但是其內容有誤,錯誤訊息:

error: ‘struct tty_driver’ has no member named ‘write’

其原因是 tty_driver 結構沒有 write 成員,他反倒是定義在 tty_operations 中,所以要透過 tty_driver 中的 ops 去指向 tty_operations 中的 write。

print_string.c 範例如下,載入及卸載 module 時,皆會輸出 hello 訊息:

/*
 *  print_string.c - Send output to the tty we're running on, regardless if it's
 *  through X11, telnet, etc.  We do this by printing the string to the tty
 *  associated with the current task.
 */
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/sched.h>    /* For current */
#include <linux/tty.h>        /* For the tty declarations */
#include <linux/version.h>    /* For LINUX_VERSION_CODE */

MODULE_LICENSE("GPL");
MODULE_AUTHOR("Peter Jay Salzman");

static void print_string(char *str)
{
    struct tty_struct *my_tty;

/*
 * tty struct went into signal struct in 2.6.6
 */
#if ( LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,5) )
/*
 * The tty for the current task
 */
    my_tty = current->tty;
#else
/*
 * The tty for the current task, for 2.6.6+ kernels
 */
    my_tty = current->signal->tty;
#endif

/*
 * If my_tty is NULL, the current task has no tty you can print to
 * (ie, if it's a daemon).  If so, there's nothing we can do.
 */
    if (my_tty != NULL) {

        //((my_tty->driver)->write) (my_tty,    /* The tty itself */
        ((my_tty->driver)->ops->write) (my_tty,    /* The tty itself */
#if ( LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,9) )        
                       0,    /* Don't take the string
                           from user space        */
#endif
                       str,    /* String                 */
                       strlen(str));    /* Length */


#if ( LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,9) )        
        //((my_tty->driver)->write) (my_tty, 0, "\015\012", 2);
        ((my_tty->driver)->ops->write) (my_tty, 0, "\015\012", 2);
#else
        //((my_tty->driver)->write) (my_tty, "\015\012", 2);
        ((my_tty->driver)->ops->write) (my_tty, "\015\012", 2);
#endif
    }
}

static int __init print_string_init(void)
{
    print_string("The module has been inserted.  Hello world!");
    return 0;
}

static void __exit print_string_exit(void)
{
    print_string("The module has been removed.  Farewell world!");
}

module_init(print_string_init);
module_exit(print_string_exit);

上一篇
Blocking 與 Non-blocking I/O
下一篇
工作排程
系列文
Linux kernel模組的開發30

尚未有邦友留言

立即登入留言