一般有两种方式:
-
- 系统调用
-
- 用flip->open()等函数。
- 利用系统调用:
- sys_open,sys_write,sys_read等。
其实分析过sys_open可以知道,最后调用的也是filp->open。
sys_open ==> do_sys_open ==> filp->open
在linuxsir上的一个帖子,上面一个版主说:sys_open和进程紧密相关,往往不在内核中使用。
而其实sys_open最后也是调用了filp->open。
其实好像Linux2.6.20后面就不推荐使用sys_open,那我们这里就就后者进行详细的介绍
- flip->open 等函数:
在模块中,用户空间的open,read,write,llseek等函数都是不可以使用的。应该使用其在内核中对应的函数。可以使用filp->open配合struct file里的read/write来进行对文件的读写操作。
直接上干货(内容自己悟!):
例1 :
filp->f_op->read
filp_open
filp->f_op->write
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <asm/uaccess.h>
#include <linux/mm.h>
MODULE_AUTHOR("Kenthy@163.com.");
MODULE_DESCRIPTION("Kernel study and test.");
void fileread(const char * filename)
{
struct file *filp;
struct inode *inode;
mm_segment_t fs;
off_t fsize;
char *buf;
unsigned long magic;
printk("<1>start....\n");
filp=filp_open(filename,O_RDONLY,0);
inode=filp->f_dentry->d_inode;
magic=inode->i_sb->s_magic;
printk("<1>file system magic:%li \n",magic);
printk("<1>supe