Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1527821
  • 博文数量: 399
  • 博客积分: 8508
  • 博客等级: 中将
  • 技术积分: 5302
  • 用 户 组: 普通用户
  • 注册时间: 2009-10-14 09:28
个人简介

能力强的人善于解决问题,有智慧的人善于绕过问题。 区别很微妙,小心谨慎做后者。

文章分类

全部博文(399)

文章存档

2018年(3)

2017年(1)

2016年(1)

2015年(69)

2013年(14)

2012年(17)

2011年(12)

2010年(189)

2009年(93)

分类: LINUX

2010-01-18 23:54:13

#include
#include
#include

#include

#include
#include
#include /* get_fs(),set_fs(),get_ds() */

#define FILE_DIR "/root/test.txt"

MODULE_LICENSE("GPL");
MODULE_AUTHOR("kenthy@163.com");

char *buff = "module read/write test";
char tmp[100];

static struct file *filp = NULL;

static int __init wr_test_init(void)
{
    mm_segment_t old_fs;
    ssize_t ret;
   
    filp = filp_open(FILE_DIR, O_RDWR | O_CREAT, 0644);
   
    //    if(!filp)

    if(IS_ERR(filp))
        printk("open error...\n");
   
    old_fs = get_fs();
    set_fs(get_ds());
   
    filp->f_op->write(filp, buff, strlen(buff), &filp->f_pos);
   
    filp->f_op->llseek(filp,0,0);
    ret = filp->f_op->read(filp, tmp, strlen(buff), &filp->f_pos);
   
    set_fs(old_fs);
   
    if(ret > 0)
        printk("%s\n",tmp);
    else if(ret == 0)
        printk("read nothing.............\n");
    else
        {
            printk("read error\n");
            return -1;
        }

    return 0;
}

static void __exit wr_test_exit(void)
{
    if(filp)
        filp_close(filp,NULL);
}

module_init(wr_test_init);
module_exit(wr_test_exit);
阅读(652) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~