Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2929194
  • 博文数量: 401
  • 博客积分: 12926
  • 博客等级: 上将
  • 技术积分: 4588
  • 用 户 组: 普通用户
  • 注册时间: 2009-02-22 14:51
文章分类

全部博文(401)

文章存档

2015年(16)

2014年(4)

2013年(12)

2012年(82)

2011年(98)

2010年(112)

2009年(77)

分类: LINUX

2010-10-19 12:11:26

#include
#include
#include
#include
#include
#include  
#include
#include
#include
#include
#include
#include
MODULE_LICENSE("GPL");

/**
 ** \fn openFile
 ** \brief Open a file
 ** \param   pathname  the path to the file to be opened
 *\param   flags     permision flags to open the file with
 *      * \param   mode      mode flags to open the file with
 *       * \param   uid       owner user id
 *        * \param   gid       owner group id
 *         * \return  file_ptr         pointer to the opened file
 *          *           NULL - err_code  error opening as indicated by the error code
 *           */
static struct file *openFile (const char *filename, int32_t flags, int32_t mode, uid_t uid, gid_t gid)
{
    struct file *file;
    do {
        struct dentry *saved_root = current->fs->pwd.dentry;
        struct vfsmount *saved_mnt = current->fs->pwd.mnt;

        current->fs->pwd.dentry = init_task.fs->pwd.dentry;
        current->fs->pwd.mnt = init_task.fs->pwd.mnt;

        if (NULL == (file = filp_open (filename, flags, mode)))
            printk(KERN_ERR "kermit: can't open file %s!\n", filename);

        current->fs->pwd.mnt = saved_mnt;
        current->fs->pwd.dentry = saved_root;
    } while (0);
    return file;
}

/** 
 *  * \fn closeFile
 *   * \brief Close a file
 *    * \param   file  pointer to the file to be closed
 *     * \return  0     success
 *      *           != 0  error closing the file as indicated by the error code
 *       */
static int32_t closeFile (struct file *file)
{
    return filp_close (file, current->files);
}

/** 
 *  * \fn readFile
 *   * \brief Read from a file
 *    * \param   file  pointer to the file to be read from
 *     * \param   addr  buffer to hold the read data
 *      * \param   len   number of bytes to read
 *       * \param   pos   starting position
 *        * \return  number of bytes read
 *         *
 *          * \warning file should be a valid file pointer
 *           *           and the corresponding file should
 *            *           be opened, i.e. the file should
 *             *           located be on the linked node
 *              */
static int32_t readFile(struct file *file, char *addr, size_t len, loff_t *pos)
{
    int32_t ret;
    do {
        mm_segment_t old_fs;

        old_fs = get_fs();
        set_fs(KERNEL_DS); /* Enable to read in kernel memory */
        ret = vfs_read(file, addr, len, pos);
        set_fs(old_fs); /* Disable to read in kernel memory */
    } while (0);
    return ret;
}


static int __init hello_init (void)
{
    char cbuf[128] = {0};
    printk("Hello module init\n");

    struct file *lut_fp = openFile("/tmp/1.txt", 0, 0, 0, 0);
    if (NULL == lut_fp ) {
        return 0;
    }
    lut_fp->f_pos = 0;

    readFile(lut_fp, cbuf, sizeof(cbuf), &lut_fp->f_pos);

    printk("%s \n", cbuf);
    closeFile (lut_fp);


    return 0;
}

static void __exit hello_exit (void)
{
    printk("Hello module exit\n");
}


module_init(hello_init);
module_exit(hello_exit);


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