关闭

带参数到内核模块

87人阅读 评论(0) 收藏 举报
分类:
hellop.c 文件内容如下:
  1. /*
  2.  * $Id: hellop.c,v 1.4 2004/09/26 07:02:43 gregkh Exp $
  3.  */
  4. #include <linux/init.h>
  5. #include <linux/module.h>
  6. #include <linux/moduleparam.h>

  7. MODULE_LICENSE("Dual BSD/GPL");

  8. /*
  9.  * These lines, although not shown in the book,
  10.  * are needed to make hello.c run properly even when
  11.  * your kernel has version support enabled
  12.  */


  13. /*
  14.  * A couple of parameters that can be passed in: how many times we say
  15.  * hello, and to whom.
  16.  */
  17. static char *whom = "world";
  18. static int howmany = 1;
  19. module_param(howmany, int, S_IRUGO);
  20. module_param(whom, charp, S_IRUGO);

  21. static int hello_init(void)
  22. {
  23.         int i;
  24.         for (i = 0; i < howmany; i++)
  25.                 printk(KERN_ALERT "(%d) Hello, %s\n", i, whom);
  26.         return 0;
  27. }
  28. static void hello_exit(void)
  29. {
  30.         printk(KERN_ALERT "Goodbye, cruel world\n");
  31. }

  32. module_init(hello_init);
  33. module_exit(hello_exit);
Makefile 文件内容如下:
  1. # To build modules outside of the kernel tree, we run "make"
  2. # in the kernel source tree; the Makefile these then includes this
  3. # Makefile once again.
  4. # This conditional selects whether we are being included from the
  5. # kernel Makefile or not.
  6. ifeq ($(KERNELRELEASE),)

  7. # Assume the source tree is where the running kernel was built
  8. # You should set KERNELDIR in the environment if it's elsewhere
  9. KERNELDIR ?= /lib/modules/$(shell uname -r)/build
  10. # The current directory is passed to sub-makes as argument
  11. PWD := $(shell pwd)

  12. modules:
  13. $(MAKE) -C $(KERNELDIR) M=$(PWD) modules

  14. modules_install:
  15. $(MAKE) -C $(KERNELDIR) M=$(PWD) modules_install

  16. clean:
  17. rm -rf *.o *~ core .depend .*.cmd *.ko *.mod.c .tmp_versions

  18. .PHONY: modules modules_install clean

  19. else
  20. # called from kernel build system: just declare what our modules are
  21. obj-m := hellop.o
  22. endif
$ make  编译
$ sudo insmod hellop.ko howmany=10 whom="Tom"  加载模块

在/var/log/syslog中打印信息如下:
  1. Jun 21 16:12:33 eric-Inspiron-530 kernel: [23155.364464] (0) Hello, Tom
  2. Jun 21 16:12:33 eric-Inspiron-530 kernel: [23155.364468] (1) Hello, Tom
  3. Jun 21 16:12:33 eric-Inspiron-530 kernel: [23155.364470] (2) Hello, Tom
  4. Jun 21 16:12:33 eric-Inspiron-530 kernel: [23155.364471] (3) Hello, Tom
  5. Jun 21 16:12:33 eric-Inspiron-530 kernel: [23155.364473] (4) Hello, Tom
  6. Jun 21 16:12:33 eric-Inspiron-530 kernel: [23155.364474] (5) Hello, Tom
  7. Jun 21 16:12:33 eric-Inspiron-530 kernel: [23155.364476] (6) Hello, Tom
  8. Jun 21 16:12:33 eric-Inspiron-530 kernel: [23155.364477] (7) Hello, Tom
  9. Jun 21 16:12:33 eric-Inspiron-530 kernel: [23155.364479] (8) Hello, Tom
  10. Jun 21 16:12:33 eric-Inspiron-530 kernel: [23155.364480] (9) Hello, Tom

<script>window._bd_share_config={"common":{"bdSnsKey":{},"bdText":"","bdMini":"2","bdMiniList":false,"bdPic":"","bdStyle":"0","bdSize":"16"},"share":{}};with(document)0[(getElementsByTagName('head')[0]||body).appendChild(createElement('script')).src='http://bdimg.share.baidu.com/static/api/js/share.js?v=89860593.js?cdnversion='+~(-new Date()/36e5)];</script>
阅读(287) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~
评论热议
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:32847次
    • 积分:401
    • 等级:
    • 排名:千里之外
    • 原创:90篇
    • 转载:10篇
    • 译文:0篇
    • 评论:0条
    文章分类