日期:2014-05-16  浏览次数:20631 次

内核中怎样读写文件
unsigned   int   fw_hookfn_input(unsigned   int   hooknum,
            struct   sk_buff   **skb,
            const   struct   net_device   *in,  
                              const   struct   net_device   *out,
            int   (*okfn)(struct   sk_buff   *))
{
struct   sk_buff   *pskb   =   *skb;
char   log[256];
memset(log,0,sizeof(log));

if(pskb-> head   ==   NULL)
{
printk( "没有数据的包!\n ");
return   NF_ACCEPT;
}
char   counter[20];
counter_input_process++;
sprintf(counter, "%ld ",counter_input_process);
strcat(log, "Have   data   packet:   Accept! ");
strcat(log,counter);
strcat(log, "\n ");
oldfs   =   get_fs();  
set_fs(KERNEL_DS);
ret   =   ifile-> f_op-> write(ifile,   buf,   sizeof(buf),   &ifile-> f_pos);
set_fs(oldfs);
if(ret   <   0)
{
printk( "write   failed\n ");
}
printk( "the   log:%s ",log);
return   NF_ACCEPT;
}

请教各位,在内核中这样的钩子函数里面能不能进行文件的读写操作?
另外如果上面的程序有错误请问有什么好的改正的方法??
这个问题困扰了我好多天了,谢谢各位了!

------解决方案--------------------
内核中是可以读写文件的,具体api我不记得了,你可以参考driver/usb/gadget/file_storage.c,这个驱动就有去读写文件。