替换 printk 函数

在实际应用中我们希望把消息送到任何一个 tty。这在内核模块被释放时确认错误是很重要的,因为它将会在所有内核模块中使用。

这样做的方法是使用一个指向当前运行任务的指针,来得到当前任务的 tty 结构。然后,我们到 tty 结构里寻找一个指向写串函数的指针,我们用这个函数把一个串写进 tty。

/* printstr.c - send textual output to the tty you're 
 * running on, regardless of whether it's passed 
 * through X11, telnet, etc. */

/* Copyright (C) 1998 by Ori Pomerantz */

/* Standard in kernel modules */
#include <linux/module.h>   /* Specifically, a module */


/* Necessary here */
#include <linux/sched.h>    /* For current */
#include <linux/tty.h>      /* For the tty declarations */

/* Print the string to the appropriate tty, the one 
 * the current task uses */
void print_string(char *str)
{
  struct tty_struct *my_tty;

  /* The tty for the current task */
  my_tty = current->signal->tty;

  /* If my_tty is NULL, it means that the current task 
   * has no tty you can print to (this is possible, for 
   * example, if it's a daemon). In this case, 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 first parameter is the tty to 
     * write to, because the  same function would 
     * normally be used for all tty's of a certain type.
     * The second parameter is a pointer to a string, 
     * and the third parameter is the length of 
     * the string.
     */
    ((*(my_tty->driver)).ops->write)(
        my_tty, /* The tty itself */
	str, /* String */
	strlen(str));  /* Length */

    /* ttys were originally hardware devices, which 
     * (usually) adhered strictly to the ASCII standard. 
     * According to ASCII, to move to a new line you 
     * need two characters, a carriage return and a 
     * line feed. In Unix, on the other hand, 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 the reason why the text file 
     * format is different between Unix and Windows. 
     * In CP/M and its derivatives, such as MS-DOS and 
     * Windows, the ASCII standard was strictly 
     * adhered to, and therefore a new line requires 
     * both a line feed and a carriage return. 
     */
    ((*(my_tty->driver)).ops->write)(
      my_tty,  
      "\015\012",
      2);
  }
}


/* Module initialization and cleanup ****************** */

/* Initialize the module - register the proc file */
int printk_init_module()
{
  print_string("Module Inserted");

  return 0;
}


/* Cleanup - unregister our file from /proc */
void printk_cleanup_module()
{
	print_string("Module Removed");
}  


MODULE_AUTHOR("modified by gudujian");
MODULE_LICENSE("Dual BSD/GPL");

module_init(printk_init_module);
module_exit(printk_cleanup_module);

加载,卸载模块是显示如下:

050


###根据linux 内核编程改编。




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值