第 10 章。替换 Printks

替换printk

第 1 章中名为使用 X的章节中,我说过 X 和内核模块编程不兼容。 这对于开发内核模块来说是正确的,但在实际使用中,您希望能够将消息发送到加载模块的命令来自的任何 tty[1]

完成此操作的方法是使用current,一个指向当前运行任务的指针,以获取当前任务的 tty 结构。 然后,我们查看该 tty 结构内部,找到一个指向字符串写入函数的指针,我们使用该指针将字符串写入 tty。

示例 10-1. print_string.c

/*  print_string.c - Send output to the tty you're running on, regardless of whether 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/sched.h>    // For current
#include <linux/tty.h>      // For the tty declarations
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Peter Jay Salzman");


void print_string(char *str)
{
   struct tty_struct *my_tty;
   my_tty = current->tty;           // The tty for the current task

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

      /* my_tty->driver is a struct which holds the tty's functions, one of which (write)
       * is used to write strings to the tty.  It can be used to take a string either
       * from the user's memory segment or the kernel's memory segment.
       *
       * The function's 1st parameter is the tty to write to, because the same function
       * would normally be used for all tty's of a certain type.  The 2nd parameter
       * controls whether the function receives a string from kernel memory (false, 0) or
       * from user memory (true, non zero).  The 3rd parameter is a pointer to a string.
       * The 4th parameter is the length of the string.
       */
      (*(my_tty->driver).write)(
         my_tty,                 // The tty itself
         0,                      // We don't take the string from user space
         str,                    // String
         strlen(str));           // Length

      /* ttys were originally hardware devices, which (usually) strictly followed the
       * ASCII standard.  In ASCII, to move to a new line you need two characters, a
       * carriage return and a line feed.  On Unix, the ASCII line feed is used for both
       * purposes - so we can't just use \n, because it wouldn't have a carriage return
       * and the next line will start at the column right after the line feed. 
       *
       * BTW, this is why text files are different between Unix and MS Windows.  In CP/M
       * and its derivatives, like MS-DOS and MS Windows, the ASCII standard was strictly
       * adhered to, and therefore a newline requirs both a LF and a CR.
       */
      (*(my_tty->driver).write)(my_tty, 0, "\015\012", 2);
   }
}


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


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


module_init(print_string_init);
module_exit(print_string_exit);

注释

[1]

Teletype,最初是用于与 Unix 系统通信的键盘打印机组合,如今是 Unix 程序使用的文本流的抽象,无论是物理终端、X 显示器上的 xterm、与 telnet 等一起使用的网络连接,等等。