Chinaunix首页 | 论坛 | 博客
  • 博客访问: 464339
  • 博文数量: 89
  • 博客积分: 1126
  • 博客等级: 少尉
  • 技术积分: 1432
  • 用 户 组: 普通用户
  • 注册时间: 2011-04-11 23:37
文章分类

全部博文(89)

文章存档

2016年(6)

2015年(2)

2014年(1)

2013年(3)

2012年(23)

2011年(54)

分类:

2011-06-26 21:33:56

原文地址:带参数到内核模块 作者:hunaiquan

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

阅读(608) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~