模块编写<二>
3.1模块功能介绍
本模块在模块一的基础上增加了创建proc文件的功能。本模块在/proc目录下新建一个具有只读类型的文件,名为 xtime,内容为空。例如:
lzel@lzel-laptop:~$ ls -l /proc/|grep xtime
-r--r--r-- 1 root root 0 2009-03-30 05:12 xtime
lzel@lzel-laptop:~$ cat /proc/xtime
lzel@lzel-laptop:~$
3.2模块源代码:
1.文件结构:
lzel@lzel-laptop:~/works/driver/clockmodule/2$ ls
clockmodules.c Makefile
lzel@lzel-laptop:~/works/driver/clockmodule/2$
2.clockmodules.c源代码:
#include
#include
#include
#include
#define MODULE_NAME "xtime" /* /proc下的文件名*/
static struct proc_dir_entry *xtime_file; /* 创建proc文件结构的指针*/
static int proc_read_xtime(char *page, char **stat, off_t off, int count, int *eof, void *data) /* 读proc文件时执行的函数*/
{
printk(KERN_INFO"I want to read the xtime.\n");
return 0;
}
static int __init clockmodules_init(void)
{
int ret = 0;
xtime_file = create_proc_read_entry("xtime", 0444, NULL, proc_read_xtime, NULL); /* 创建一个只读属性的文件->0444*/
if (xtime_file == NULL) { /* 创建失败处理*/
ret = -ENOMEM;
printk(KERN_INFO"create xtime in /proc error\n");
goto error;
}
xtime_file->owner = THIS_MODULE; /* 指明文件属者*/
//printk(KERN_INFO"hello,\n xtime is: \n");
printk(KERN_INFO"create xtime in /proc success\n"); /* 创建成功,输出日志*/
return 0;
error:
remove_proc_entry("xtime", NULL); /* 避免无效指针*/
return ret;
}
static void __exit clockmodules_exit(void)
{
//printk(KERN_INFO"goodbye xtime\n");
remove_proc_entry("xtime", NULL); /* 卸载模块时,须同时删除相关的proc文件*/
printk(KERN_INFO"remove the file xtime in /proc\n");
}
module_init(clockmodules_init);
module_exit(clockmodules_exit);
MODULE_LICENSE("Dual BSD/GPL");
MODULE_AUTHOR("lzel");
lzel@lzel-laptop:~/works/driver/clockmodule/2$
3.Makefile文件:
lzel@lzel-laptop:~/works/driver/clockmodule/2$ cat Makefile
#
# Copyleft (C) 2009 lizeliang
#
# /2/Makefile
#
# DATE: 3.20.2009
#
# REV:1.0.A
#
# PLATFROM: Linux/Unix
#
# REV LIST:
# DATE: 3.22.2009
# BY: lizeliang
# MODIFICATION: standard more
#
# target
TARGET= clockmodules
# variable
KERNEL= `uname -r`
KDIR= /usr/src/linux-headers-$(KERNEL)
PWD= `pwd`
obj-m:= $(TARGET).o
# make
default:
make -C $(KDIR) M=$(PWD) modules
# clean
clean:
make -C $(KDIR) M=$(PWD) clean
#End of Makefile
lzel@lzel-laptop:~/works/driver/clockmodule/2$
3.3模块编译
1.编译
lzel@lzel-laptop:~/works/driver/clockmodule/2$ make
make -C /usr/src/linux-headers-`uname -r` M=`pwd` modules
make[1]: Entering directory `/usr/src/linux-headers-2.6.27-11-generic'
CC [M] /home/lzel/works/driver/clockmodule/1/clockmodules.o
Building modules, stage 2.
MODPOST 1 modules
CC /home/lzel/works/driver/clockmodule/1/clockmodules.mod.o
LD [M] /home/lzel/works/driver/clockmodule/1/clockmodules.ko
make[1]: Leaving directory `/usr/src/linux-headers-2.6.27-11-generic'
lzel@lzel-laptop:~/works/driver/clockmodule/2$ ls
clockmodules.c clockmodules.mod.c clockmodules.o Module.markers Module.symvers
clockmodules.ko clockmodules.mod.o Makefile modules.order xtime_one.doc
lzel@lzel-laptop:~/works/driver/clockmodule/2$
注:红色的.ko文件为要加载的目标文件(红色为故意着色)。
2.查看模块信息:
lzel@lzel-laptop:~/works/driver/clockmodule/2$ modinfo clockmodules.ko
filename: clockmodules.ko
author: lzel
license: Dual BSD/GPL
srcversion: 3744E16989B7B9A84858130
depends:
vermagic: 2.6.27-11-generic SMP mod_unload modversions 586
lzel@lzel-laptop:~/works/driver/clockmodule/2$
3.4模块运行:
3.4.1加载模块
1.加载:
lzel@lzel-laptop:~/works/driver/clockmodule/2$ sudo insmod clockmodules.ko
[sudo] password for lzel:
lzel@lzel-laptop:~/works/driver/clockmodule/2$
2.确认加载:
lzel@lzel-laptop:~/works/driver/clockmodule/2$ lsmod | head -n 10
Module Size Used by
clockmodules 9732 0
af_packet 25728 0
i915 38528 2
drm 86056 3 i915
binfmt_misc 16904 1
sco 18308 2
bridge 56980 0
stp 10628 1 bridge
bnep 20480 2
lzel@lzel-laptop:~/works/driver/clockmodule/2$
3.查看日志信息
lzel@lzel-laptop:~/works/driver/clockmodule/2$ dmesg | tail -1
[ 4135.047677] create xtime in /proc success
lzel@lzel-laptop:~/works/driver/clockmodule/2$
4.确认proc文件是否创建
lzel@lzel-laptop:~/works/driver/clockmodule/2$ ls -l /proc/| grep xtime
-r--r--r-- 1 root root 0 2009-03-30 05:35 xtime
lzel@lzel-laptop:~/works/driver/clockmodule/2$
5.读写测试
lzel@lzel-laptop:~/works/driver/clockmodule/2$ cat /proc/xtime //可读但是没有内容
lzel@lzel-laptop:~/works/driver/clockmodule/2$ dmesg | tail -n 3
[ 4122.425377] remove the file xtime in /proc
[ 4135.047677] create xtime in /proc success
[ 4361.060595] I want to read the xtime. //每读一次,读函数就会打印这样一句
lzel@lzel-laptop:~/works/driver/clockmodule/2$ echo 1> /proc/xtime
bash: /proc/xtime: Permission denied //不可写
lzel@lzel-laptop:~/works/driver/clockmodule/2$
3.4.2卸载模块:
1.卸载:
lzel@lzel-laptop:~/works/driver/clockmodule/2$ sudo rmmod clockmodules
2.确认卸载:
lzel@lzel-laptop:~/works/driver/clockmodule/2$ lsmod | head -n 10
Module Size Used by
af_packet 25728 0
i915 38528 2
drm 86056 3 i915
binfmt_misc 16904 1
sco 18308 2
bridge 56980 0
stp 10628 1 bridge
bnep 20480 2
rfcomm 44432 0
lzel@lzel-laptop:~/works/driver/clockmodule/2$
3.查看日志信息:
lzel@lzel-laptop:~/works/driver/clockmodule/2$ dmesg | tail -n 5
[ 4695.348503] create xtime in /proc success
[ 4701.731963] I want to read the xtime. //多次读取
[ 4702.466817] I want to read the xtime.
[ 4703.096173] I want to read the xtime.
[ 4716.949715] remove the file xtime in /proc //卸载成功,并删除/proc/xtime文件
lzel@lzel-laptop:~/works/driver/clockmodule/2$
lzel@lzel-laptop:~/works/driver/clockmodule/2$ ls -l /proc/xtime
ls: cannot access /proc/xtime: No such file or directory
lzel@lzel-laptop:~/works/driver/clockmodule/2$
3.5总结:
本模块仅仅是在前一个模块的基础上增加了一个创建proc文件的功能,主要使用到创建只读proc文件函数create_proc_read_entry,该函数接收五个参数来创建文件,分别为文件名、文件属性、文件所在目录(/proc下则为NULL)、读文件时执行的函数及其参数。
读proc文件时要执行的函数详细如下:
int read_func (char *buffer,char **stat,off_t off,int count,int *peof,void *data);
@buffer :把要返回给用户的信息写在buffer里,最大不超过PAGE_SIZE(一般4K)
@stat :一般不使用
@off :buffer的偏移量s
@count :用户要取的字节数@peof :读到文件尾时,把peof指向的位置置1
@data :被多个proc文件定义为读时,通过data传递参数
阅读(952) | 评论(0) | 转发(0) |