精华区 [关闭][返回]

当前位置:网易精华区>>讨论区精华>>电脑技术>>● Linux>>Linux之开发篇>>内核技术>>Linux内核模块编程指南(十)

主题:Linux内核模块编程指南(十)
发信人: arira(大天使)
整理人: qiaoqian(2001-12-31 00:01:56), 站内信件
主题:《Linux内核模块编程指南》(十) 
发信人: kevintz()
整理人: kevintz(2000-06-24 00:42:52), 站内信件  
《Linux内核模块编程指南》 
《Linux Kernel Module Programming Guide》 
作者:Ori Pomerantz 中译者:谭志([email protected]
   
译者注: 
1、LKMPG是一本免费的书,英文版的发行和修改遵从GPL version 2的许可。为了
节省时间,我只翻译了其中的大部分的大意,或者说这只是我学习中的一些中文
笔记吧,不能算是严格上的翻译,但我认为这已经足够了。本文也允许免费发布
,但发布前请和我联系,但不要把本文用于商业目的。鉴于本人的水平,文章中
难免有错误,请大家不吝指正。 
2、本文中的例子在Linux(kernel version 2.2.10)上调试通过。你用的Linux必
须支持内核模块的加载,如果不支持,请在编译内核时选上内核模块的支持或升
级你的内核到一个支持内核模块的版本。 
       

       

                        第十章  替换printk's 


    在刚开始的时候,我提到过X和内核模块编程不要混用的问题。这在内核模块
开发时是正确的。但实际使用上你想能够发信息到运行调用这个模块的程序所在
的终端上。这在内核模块释放以后能够标识错误时就显得很重要了。因为内核模
块被很多进程调用,不同的进程有不同的终端。 
     
    实现方法是使用current(一个指向当前运行任务的指针)来取得任务的tty结
构体,然后从tty结构体里找到写字符串到tty的函数指针。 


/* printk.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 */ 


/* The necessary header files */ 

/* Standard in kernel modules */ 
#include <linux/kernel.h>   /* We're doing kernel work */ 
#include <linux/module.h>   /* Specifically, a module */ 

/* Deal with CONFIG_MODVERSIONS */ 
#if CONFIG_MODVERSIONS==1 
#define MODVERSIONS 
#include <linux/modversions.h> 
#endif         

/* 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->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 controls whether the  
     * function receives a string from kernel memory  
     * (false, 0) or from user memory (true, non zero).  
     * The third parameter is a pointer to a string,  
     * and the fourth 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) 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).write)( 
      my_tty,   
      0, 
      "\015\012", 
      2); 
  } 



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


/* Initialize the module - register the proc file */ 
int init_module() 

  print_string("Module Inserted"); 

  return 0; 



/* Cleanup - unregister our file from /proc */ 
void cleanup_module() 

  print_string("Module Removed"); 
}   


kevintz注: 
有了这个print_string函数调试就方便多了,但它没有printk可变参数的功能,
这是它最大的缺点。下面是我写的一个可接收可变参数的版本。大家可以试试。


static char buff[1024]; /*buffer print_string to write*/ 
spinlock_t console_lock; 
void print_string(const char *fmt, ...) 

  va_list args; 
  int i; 
  struct tty_struct *my_tty; 
  long flags; 

  spin_lock_irqsave(&console_lock, flags); 
  /* The tty for the current task */ 
  va_start(args, fmt); 
  i=vsprintf(buff, fmt, args); 
  buff[i]=0; 
  va_end(args); 
  my_tty = current->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).write)( 
        my_tty, /* The tty itself */ 
        0, /* We don't take the string from user space */ 
        buff, /* String */ 
        strlen(buff));  /* Length */ 

    (*(my_tty->driver).write)( 
      my_tty, 
      0, 
      "\015\012", 
      2); 
  } 
  spin_unlock_irqrestore(&console_lock, flags); 





--
那一刹那,我开始用心去看这个世界,所有的事物真的可以看得前 
所未有的那么清楚……  

 
 

[关闭][返回]