代码:
#ifndef __KERNEL__
#define __KERNEL__
#endif
#ifndef MODULE
#define MODULE
#endif
#include<linux/config.h>
#include<linux/module.h>
#include<linux/kernel.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>
char kernel_version[]=UTS_RELEASE;
struct file_operations
{
int (*seek) (struct inode *,struct file * ,off_t,int);
int (*read) (struct inode *,struct file * ,char ,int);
int (*write) (struct inode *,struct file * ,off_t,int);
int (*readdir) (struct inode *,struct file *,struct dirent * ,int);
int (*select) (struct inode *,struct file *,int ,select_table *);
int (*ioctl) (struct inode *,struct file *,unsigned int ,unsigned long);
int (*mmap) (struct inode *,struct file * ,struct file *,struct vm_area_struct * );
int (*open) (struct inode *,struct file *);
int (*release) (struct inode * ,struct file *);
int (*fsync ) (struct inode * ,struct file *);
int (*fasync) (struct inode *,struct file *,int);
int (*check_media_change) (struct inode * ,struct file *);
int (*revalidate) (dev_t dev);
};
unsigned int test_major=0;
static int read_test(struct inode *node, struct file * file,char *buf,int count)
{
int left;
if(verify_area(VERIFY_WRITE,buf,count)==-EFAULT)
return -EFAULT;
for(left=count;left>0;left--)
{
_put_user(1,buf,1);
buf++;
}
return count;
};
static int write_test(struct inode* inode,struct file * file,const char * buf ,int count)
{
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,
read_test,
write_test,
NULL, /*test_readdir */
NULL,
NULL, /*test_ioctl */
NULL, /*test_mmap */
open_test,
release_test,
NULL, /*test_fsync */
NULL, /* test_fasync */
/*nothing more ,fill with NULLs */
};
/* 初始化设备驱动程序 */
int init_module(void)
{
int result ;
result=register_chrdev(0,"test",&test_fops);
if(result<0)
{
printk("<0> 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");
}
编译命令:
gcc -c -O2 -DMODULE -D__KERNEL__ -I/usr/src/linux-2.4.20-8/include test2.c
提示如下:
test2.c:19: redefinition of `struct file_operations'
test2.c:23: warning: `struct dirent' declared inside parameter list
test2.c:23: warning: its scope is only this definition or declaration, which is probably not what you want
test2.c:24: parse error before "select_table"
test2.c: In function `read_test':
test2.c:38: `VERIFY_WRITE' undeclared (first use in this function)
test2.c:38: (Each undeclared identifier is reported only once
test2.c:38: for each function it appears in.)
test2.c: At top level:
test2.c:65: warning: initialization from incompatible pointer type
test2.c:66: warning: initialization from incompatible pointer type
test2.c:72: warning: initialization from incompatible pointer type
我用的是RedHat 9.0 linux-2.4.20-8的内核
希望高手指点 全天等候! 删去
struct file_operations
{
int (*seek) (struct inode *,struct file * ,off_t,int);
int (*read) (struct inode *,struct file * ,char ,int);
int (*write) (struct inode *,struct file * ,off_t,int);
int (*readdir) (struct inode *,struct file *,struct dirent * ,int);
int (*select) (struct inode *,struct file *,int ,select_table *);
int (*ioctl) (struct inode *,struct file *,unsigned int ,unsigned long);
int (*mmap) (struct inode *,struct file * ,struct file *,struct vm_area_struct * );
int (*open) (struct inode *,struct file *);
int (*release) (struct inode * ,struct file *);
int (*fsync ) (struct inode * ,struct file *);
int (*fasync) (struct inode *,struct file *,int);
int (*check_media_change) (struct inode * ,struct file *);
int (*revalidate) (dev_t dev);
};
页:
1
[2]