Chinaunix首页 | 论坛 | 博客
  • 博客访问: 3472950
  • 博文数量: 1805
  • 博客积分: 135
  • 博客等级: 入伍新兵
  • 技术积分: 3345
  • 用 户 组: 普通用户
  • 注册时间: 2010-03-19 20:01
文章分类

全部博文(1805)

文章存档

2017年(19)

2016年(80)

2015年(341)

2014年(438)

2013年(349)

2012年(332)

2011年(248)

分类: LINUX

2015-03-15 15:54:41

原文地址:linux 内核态写文件 作者:gaocheng

#include
#include
#include
#include
#include
#include
#include


 struct file *filp;




static int __init my_fsop_init(void)
{
    char *p;
    int i;
    loff_t pos = 0;
    mm_segment_t old_fs;
    char *name = "/var/kerfile";
   
   filp = filp_open(name , O_CREAT | 2 | O_NOFOLLOW | O_LARGEFILE  , 0777);
  

   p = kmalloc(100,GFP_KERNEL);
  
   if( NULL == p)
   {
      return -1;   
   }
  
   for(i=0;i<100;i++)
   {
       *(p+i) = '0'+i;
   }
 
  //we usr kernel mem for user
 
  old_fs = get_fs();
  set_fs(KERNEL_DS);
   
  vfs_write(filp, p, 100, &pos);
 
  set_fs(old_fs);

  filp_close(filp, current->files);

  return 0;
  
}




static void __exit my_fsop_exit(void)
{
   
}


module_init(my_fsop_init);
module_exit(my_fsop_exit);



MODULE_LICENSE("GPL");
MODULE_AUTHOR("RAISECOM");



 
阅读(542) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~