linux 模块的使用

1.  如一段模块的代码:hellomod.c(点击)

     
     
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
static int __init lkp_init ( void )
{
     printk ( "<1> Hello, world! from the kernel space... \n " );
     return 0 ;
}
static void __exit lkp_exit ( void )
{
     printk ( "<1> Goodbye, world! leaving kernel space... \n " );
}
module_init ( lkp_init );
module_exit ( lkp_exit );
MODULE_LICENSE ( "GPL" );

1.1   module_init (lkp_init)  这一段宏展开 :

          #define module_init(x)    __initcall(x);

          #define __initcall(fn) device_initcall(fn)

          #define device_initcall(fn)        __define_initcall("6",fn,6)

          #define __define_initcall(level,fn,id) \
                        static initcall_t __initcall_##fn##id __used \
                        __attribute__((__section__(".initcall" level ".init"))) = fn

           所以这段宏是:

                   static   initcall_t   __initcall_##fn##6    __used    __attribute__((__section__(".initcall"    “6”    ".init"))) = lkp_init

                   其中     initcall_t 是函数指针原型: typedef int (*initcall_t)(void); 

                   __initcall_##fn##id    就是函数指针的名称,它其实是一个变量名称                  

                   __attribute__((__section__(".initcall" level ".init")))   则表示把对象放在一个这个由括号中的名称所指代的section中。

   所以这一段宏的意思是  申请一个变量名为 __initcall_lkp_init6 类型为 typedef int (*initcall_t)(void) 的函数指针,并赋值为 lkp_init。

1.2   module_exit (lkp_exit) 这一段宏展开 :

           #define module_exit(x)    __exitcall(x);

           #define __exitcall(fn) \
                         static exitcall_t __exitcall_##fn __exit_call = fn

      和上一个宏一样,定义一个函数变量并赋值。


1.3  MODULE_LICENSE ("GPL") 这一段宏展开:

           #define MODULE_LICENSE(_license)                 MODULE_INFO(license, _license)

           #define MODULE_INFO(tag, info)                           __MODULE_INFO(tag, tag, info)


          #define ___module_cat(a,b)                                    __mod_ ## a ## b

          #define __module_cat(a,b)                                      ___module_cat(a,b)

          #define __stringify_1(x)                                             #x
          #define __stringify(x)                                                 __stringify_1(x)

          #define __MODULE_INFO(tag, name, info)                    \
                        static const char __module_cat(name,__LINE__)[]                \
                                            __attribute_used__                            \
                                            __attribute__((section(".modinfo"),unused)) = __stringify(tag) "=" info

          展开后形式如下:

                    static const char
                                  __mod_license24[] __attribute__ ((__used__))
                                  __attribute__((section(".modinfo"),unused)) = "license" "=" "GPL";

                    (static const char   str[] = "license" "=" "GPL";   上面的型如这个,定义一个字符数组然后给字符数组赋值)


****  另外注意上边:

                  #define ___module_cat(a,b)  __mod_ ## a ## b

                  这一句中的 ## 的用法,(可以自己尝试着是一下)!!!


1.4  module_param(name, type, perm) 解析

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值