实验终于成功了,copy_to_user() && copy_from_user()

来源:互联网 发布:asp网页程序员招聘 编辑:程序博客网 时间:2024/05/16 09:55

//纠结了两天后,关于这两个函数的实验终于有点成就感了,网上关于这两个函数的用法说法不一,有人说使用前需要检查buf是否

//可用。急于实验的我参考了多种方案,结果还是没有成功。最后觉得还是很需要了解这两个函数的原型,以及file_operations的定

//义。通过查看源文件了解到这两个函数已经检查了buf是否可用,以及进一步了解了file_operations结构,实验发现原来

//copy_to_user(),与copy_from_user()的使用可以这么简洁!以下的注释是为了测试buf、buf_dev是否为整形变量,实验表明,

//那是可以的。

//随便提一下函数的定义在asm/uaccess.h,而file_operations则定义在linux/fs.h中

 

//实验平台:fedore 10

//日期2011_1_1

//Seven.Bao
//first_chrdev_m.c

#define  __NO_VERSION__
#define  __KENREL__
#define  DEVICE_NAME "first_chrdev_m"
#include  <linux/module.h>
#include  <linux/version.h>
#include <linux/types.h>
#include <linux/fs.h>
#include <linux/mm.h>
#include <linux/errno.h>
#include <asm/segment.h>
#include <asm/uaccess.h>
#include <linux/init.h>

MODULE_LICENSE("GPL");
MODULE_AUTHOR("Seven");

unsigned int test_major = 0;
static char buf_dev[100] = "kernel_data_2011_1_1";
//static int chrdev_var = 2011;

static ssize_t write_test(struct file *file,const char *buf,size_t count,loff_t* f_pos){
        //copy_from_user(&chrdev_var,buf,sizeof(int));
        copy_from_user(buf_dev,buf,count);
        return 0;
}

static ssize_t read_test(struct file *file,char *buf,size_t count,loff_t* f_pos){
        //copy_to_user(buf,&chrdev_var,sizeof(int));
        copy_to_user(buf,buf_dev,count);
        return count;
}

static int open_test(struct inode *inode,struct file *file ) {
        return 0;
}

static void release_test(struct inode *inode,struct file *file ) {
        return 0;
}

static int ioctl_test(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg){
        return 0;
}

struct file_operations test_fops = {
        .read    = read_test,
        .write   = write_test,
        .open    = open_test,
        .release = release_test,
        .ioctl   = ioctl_test,
};

static int __init first_chrdev_init(void){
        int result;
        result = register_chrdev(0,DEVICE_NAME,&test_fops);

        if (result < 0){
                printk("<0>""chrdev_init: can't get major number\n");
                return result;
        }

        if (test_major == 0){
                test_major = result;
        }

        printk("<0>""install module success!\n");

        return 0;
}

static int __exit first_chrdev_exit(void){
        printk("<0>""remove module success!!\n");
        unregister_chrdev(test_major,DEVICE_NAME);
        return 0;
}

module_init(first_chrdev_init);
module_exit(first_chrdev_exit);

 

//first_chrdev_t.c

#define DEVICE_NAME "/dev/first_chrdev_m"
#include<stdio.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>

int main(int argc,char *argv[]){

        int fd,num = 0;

        char buf_write[100] = "user_write";
        char buf_read[100]  = "user_read";

        fd=open(DEVICE_NAME,O_RDWR);
        if(fd == -1){
                perror("open");
                return -1;
        }

        //printf("please input to num,num = ");
        //scanf("%d",&num);

        //write(fd,&num,sizeof(int));
        //read(fd,&num,sizeof(int));

        //printf("second read ,num = %d\n",(int)num);

        printf("please input to buf_write,write = ");
        scanf("%s",buf_write);

        write(fd,buf_write,20);
        read(fd,buf_read,20);

        printf("after write,buf_read = %s\n",buf_read);

        close(fd);
        return 0;
}