带参数到内核模块

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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值