|
我做了一个非常简单的linux驱动程序
但在测试时发现过不了verify_area函数,每次总是有错
轻高手指点一下,源码如下:
驱动的源码:
#define __NO_VERSION__
#include <linux/module.h>
#include <linux/version.h>
char kernel_version [] = UTS_RELEASE;
#include <linux/types.h>
#include <linux/fs.h>
#include <linux/mm.h>
#include <linux/errno.h>
#include <asm/segment.h>
#include <asm/uaccess.h>
unsigned int test_major = 0;
static int read_test(struct inode *node,struct file *file,char *buf,int count)
{
int left;
unsigned char temp;
if (verify_area(VERIFY_WRITE,buf,count) == -EFAULT )
{
printk(KERN_INFO "buffer error \n");
return -EFAULT;
}
temp = 1;
for(left = count ; left > 0 ; left--)
{
__put_user(temp,buf);
buf++;
}
printk(KERN_INFO "read test \n");
return count;
}
static int write_test(struct inode *inode,struct file *file,const char *buf,int count)
{
printk(KERN_INFO "write test \n");
return count;
}
static int open_test(struct inode *inode,struct file *file )
{
MOD_INC_USE_COUNT;
return 0;
}
static void release_test(struct inode *inode,struct file *file )
{
MOD_DEC_USE_COUNT;
}
struct file_operations test_fops = {
NULL,
NULL,
read_test,
write_test,
NULL, /* test_readdir */
NULL,
NULL, /* test_ioctl */
NULL, /* test_mmap */
open_test,
NULL,
release_test,
NULL, /* test_fsync */
NULL, /* test_fasync */
NULL,
NULL,
NULL,
NULL,
NULL,
/* nothing more, fill with NULLs */
};
int init_module(void)
{
int result;
result = register_chrdev(0, "test", &test_fops);
if (result < 0) {
printk(KERN_INFO "test: can't get major number\n");
return result;
}
if (test_major == 0) test_major = result; /* dynamic */
return 0;
}
void cleanup_module(void)
{
unregister_chrdev(test_major, "test");
}
测试程序的源码:
//#include <stdio.h>
#include <linux/types.h>
#include <linux/stat.h>
#include <linux/fcntl.h>
main()
{
int testdev;
int i;
char buff[10];
memset(buff,0,10);
testdev = open("/dev/test",O_RDWR);
if(testdev == -1)
{
printf("Can not open file \n");
exit(0);
}
read(testdev,buff,5);
for(i=0;i<5;i++)
{
printf("%d\n",buff);
}
close(testdev);
} |
|