Chinaunix首页 | 论坛 | 博客
  • 博客访问: 217685
  • 博文数量: 27
  • 博客积分: 2010
  • 博客等级: 大尉
  • 技术积分: 360
  • 用 户 组: 普通用户
  • 注册时间: 2008-07-14 17:06
文章分类

全部博文(27)

文章存档

2011年(1)

2009年(5)

2008年(21)

我的朋友

分类: LINUX

2009-03-29 23:28:04

模块编写<三>
4.1模块功能介绍
    本模块在模块二的基础上增加了在proc文件中输出xtime值的功能。例如:
lzel@lzel-laptop:~/works/driver/clockmodule/3$ ls -l /proc/xtime
-r--r--r-- 1 root root 0 2009-03-30 06:02 /proc/xtime
lzel@lzel-laptop:~/works/driver/clockmodule/3$ cat /proc/xtime
tv_sec: 1238364172
tv_nsec: 506717194
lzel@lzel-laptop:~/works/driver/clockmodule/3$
4.2模块源代码:
1.文件结构:
lzel@lzel-laptop:~/works/driver/clockmodule/3$ ls
clockmodules.c  Makefile
lzel@lzel-laptop:~/works/driver/clockmodule/3$
2.clockmodules.c源代码:
lzel@lzel-laptop:~/works/driver/clockmodule/3$ cat 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");
    struct timespec time; /* struct timespec 时间类型结构体*/
    int len;

    time = current_kernel_time(); /* 从内核中获取xtime的值*/
    len = sprintf(page,"tv_sec: %ld\ntv_nsec: %ld\n",time.tv_sec, time.tv_nsec); /* 输出到/proc/xtime文件中*/
    return len;
}

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/3$
3.Makefile文件:
lzel@lzel-laptop:~/works/driver/clockmodule/3$ 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/3$
4.3模块编译
1.编译
lzel@lzel-laptop:~/works/driver/clockmodule/3$ 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/3/clockmodules.o
  Building modules, stage 2.
  MODPOST 1 modules
  CC      /home/lzel/works/driver/clockmodule/3/clockmodules.mod.o
  LD [M]  /home/lzel/works/driver/clockmodule/3/clockmodules.ko
make[1]: Leaving directory `/usr/src/linux-headers-2.6.27-11-generic'
lzel@lzel-laptop:~/works/driver/clockmodule/3$ ls
clockmodules.c   clockmodules.mod.c  clockmodules.o  Module.markers  Module.symvers
clockmodules.ko  clockmodules.mod.o  Makefile        modules.order
lzel@lzel-laptop:~/works/driver/clockmodule/3$

注:红色的.ko文件为要加载的目标文件(红色为故意着色)。
2.查看模块信息:
lzel@lzel-laptop:~/works/driver/clockmodule/3$ modinfo clockmodules.ko
filename:       clockmodules.ko
author:         lzel
license:        Dual BSD/GPL
srcversion:     840ADB385F481B598F526D6
depends:       
vermagic:       2.6.27-11-generic SMP mod_unload modversions 586
lzel@lzel-laptop:~/works/driver/clockmodule/3$
4.4模块运行:
4.4.1加载模块
1.加载:
lzel@lzel-laptop:~/works/driver/clockmodule/3$ sudo insmod clockmodules.ko
[sudo] password for lzel:
lzel@lzel-laptop:~/works/driver/clockmodule/3$
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/3$ dmesg | tail -1
[ 5935.836375] create xtime in /proc success
lzel@lzel-laptop:~/works/driver/clockmodule/3$
4.确认proc文件是否创建
lzel@lzel-laptop:~/works/driver/clockmodule/3$ ls -l /proc | grep xtime
-r--r--r--  1 root       root               0 2009-03-30 06:15 xtime
lzel@lzel-laptop:~/works/driver/clockmodule/3$ 
5.读写测试
lzel@lzel-laptop:~/works/driver/clockmodule/3$ cat /proc/xtime //读取了xtime的值
tv_sec: 1238364928
tv_nsec: 742716743
lzel@lzel-laptop:~/works/driver/clockmodule/3$ 
4.4.2卸载模块:
1.卸载:
lzel@lzel-laptop:~/works/driver/clockmodule/3$ sudo rmmod clockmodules
2.确认卸载:
lzel@lzel-laptop:~/works/driver/clockmodule/3$ 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/3$ dmesg | tail -2
[ 5935.836375] create xtime in /proc success
[ 6890.845837] remove the file xtime in /proc  //卸载成功,并删除/proc/xtime文件
lzel@lzel-laptop:~/works/driver/clockmodule/3$
lzel@lzel-laptop:~/works/driver/clockmodule/3$ ls -l /proc/xtime
ls: cannot access /proc/xtime: No such file or directory
lzel@lzel-laptop:~/works/driver/clockmodule/3$
4.5总结:
    实现把xtime打印到/proc/xtime文件中,需要涉及一个内核函数,该函数可以通过[]看到:
struct timespec current_kernel_time(void)
{
    struct timespec now;
    unsigned long seq;

    do {
        seq = read_seqbegin(&xtime_lock);

        now = xtime_cache;
    } while (read_seqretry(&xtime_lock, seq));

    return now;
}
EXPORT_SYMBOL(current_kernel_time);
    刚开始我试图通过直接打印xtime的值来实现,但是没有成功。因为,xtime是临界变量,没有被内核导出,不能被模块随意访问,故此不可行。
 
阅读(2536) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~