当前位置: > Linux编程 >

Linux内核读写文件

时间:2014-12-04 19:49来源:linux.it.net.cn 作者:IT
1. 序曲
在用户态,读写文件可以通过read和write这两个系统调用来完成(C库函数实际上是对系统调用的封装)。 但是,在内核态没有这样的系统调用,我们又该如何读写文件呢?
阅读linux内核源码,可以知道陷入内核执行的是实际执行的是sys_read和sys_write这两个函数,但是这两个函数没有使用EXPORT_SYMBOL导出,也就是说其他模块不能使用。
在fs/open.c中系统调用具体实现如下(内核版本2.6.34.1):
SYSCALL_DEFINE3(open, const char __user *, filename, int, flags, int, mode)
{
long ret;
       if (force_o_largefile())
              flags |= O_LARGEFILE;
 
       ret = do_sys_open(AT_FDCWD, filename, flags, mode);
       /* avoid REGPARM breakage on x86: */
       asmlinkage_protect(3, ret, filename, flags, mode);
       return ret;
}
跟踪do_sys_open()函数,就会发现它主要使用了do_filp_open()函数该函数在fs/namei.c中,而在该文件中,filp_open函数也是调用了do_filp_open函数,并且接口和sys_open函数极为相似,调用参数也和sys_open一样,并且使用EXPORT_SYMBOL导出了,所以我们猜想该函数可以打开文件,功能和open一样。
使用同样的方法,找出了一组在内核操作文件的函数,如下:
功能 函数原型
打开文件 struct file *filp_open(const char *filename, int flags, int mode)
读文件 ssize_t vfs_read(struct file *file, char __user *buf, size_t count, loff_t *pos)
写文件 ssize_t vfs_write(struct file *file, const char __user *buf, size_t count, loff_t *pos)
关闭文件 int filp_close(struct file *filp, fl_owner_t id)
 
2. 内核空间与用户空间
在vfs_read和vfs_write函数中,其参数buf指向的用户空间的内存地址,如果我们直接使用内核空间的指针,则会返回-EFALUT。这是因为使用的缓冲区超过了用户空间的地址范围。一般系统调用会要求你使用的缓冲区不能在内核区。这个可以用set_fs()、get_fs()来解决。
在include/asm/uaccess.h中,有如下定义: 
#define MAKE_MM_SEG(s) ((mm_segment_t) { (s) }) 
#define KERNEL_DS MAKE_MM_SEG(0xFFFFFFFF) 
#define USER_DS MAKE_MM_SEG(PAGE_OFFSET) 
#define get_ds() (KERNEL_DS) 
#define get_fs() (current->addr_limit) 
#define set_fs(x) (current->addr_limit = (x)) 
 
如果使用,如下:
mm_segment_t fs = get_fs(); 
set_fs(KERNEL_FS); 
//vfs_write(); 
vfs_read(); 
set_fs(fs);
 
详尽解释:系统调用本来是提供给用户空间的程序访问的,所以,对传递给它的参数(比如上面的buf),它默认会认为来自用户空间,在read或write()函数中,为了保护内核空间,一般会用get_fs()得到的值来和USER_DS进行比较,从而防止用户空间程序“蓄意”破坏内核空间;而现在要在内核空间使用系统调用,此时传递给read或write()的参数地址就是内核空间的地址了,在USER_DS之上(USER_DS ~ KERNEL_DS),如果不做任何其它处理,在write()函数中,会认为该地址超过了USER_DS范围,所以会认为是用户空间的“蓄意破坏”,从而不允许进一步的执行;为了解决这个问题; set_fs(KERNEL_DS);将其能访问的空间限制扩大到KERNEL_DS,这样就可以在内核顺利使用系统调用了! 
 
 
3. 完整的例子
#include <linux/kernel.h> 
#include <linux/module.h> 
#include <linux/init.h> 
#include <linux/fs.h> 
#include <linux/string.h> 
#include <linux/mm.h> oracle 
#include <linux/syscalls.h> ssh 
#include <asm/unistd.h> linux 
#include <asm/uaccess.h> 
#define MY_FILE "/root/tmp.txt" 
char buf[128]; 
struct file *file = NULL; 
 
static int __init init(void) 

  mm_segment_t old_fs; 
  printk("Hello, I'm the module that intends to write messages to file/n"); 
  if(file == NULL) 
      file = filp_open(MY_FILE, O_RDWR | O_APPEND | O_CREAT, 0644); 
  if (IS_ERR(file)) { 
  printk("error occured while opening file %s./n", MY_FILE); 
  return 0; 
  } 
  sprintf(buf,"%s", "The Messages."); 
  old_fs = get_fs(); 
  set_fs(KERNEL_DS); 
  file->f_op->write(file, (char *)buf, sizeof(buf), &file->f_pos); 
  set_fs(old_fs); 
  return 0; 
 }
 static void __exit exit(void) 
 { 
  if(file != NULL) 
  filp_close(file, NULL); oracle 
 }
 module_init(init); 
 module_exit(exit); 
MODULE_LICENSE("GPL"); 
 
注意:
1. 一个是要记得编译的时候加上-D__KERNEL_SYSCALLS__ 
 
(责任编辑:IT)
------分隔线----------------------------