结构体 struct module 分析
来源:互联网 发布:奥尔巴赫知乎 编辑:程序博客网 时间:2024/05/17 00:55
原文: http://hi.baidu.com/woaimuxiaoyizhong/blog/item/909cbb7b847913e10ad187f5.html
结构体structmodule在内核中代表一个内核模块,通过insmod(实际执行init_module系统调用)把自己编写的内核模块插入内核时,模块便与一个struct module结构体相关联,并成为内核的一部分。下面是结构体struct module的完整定义,接下来会逐个解释:
struct module { enum module_state state; struct list_head list; char name[MODULE_NAME_LEN]; struct module_kobject mkobj; struct module_param_attrs *param_attrs; const char *version; const char *srcversion; const struct kernel_symbol *syms; unsigned int num_syms; const unsigned long *crcs; const struct kernel_symbol *gpl_syms; unsigned int num_gpl_syms; const unsigned long *gpl_crcs; unsigned int num_exentries; const struct exception_table_entry *extable; int (*init)(void); void *module_init; void *module_core; unsigned long init_size, core_size; unsigned long init_text_size, core_text_size; struct mod_arch_specific arch; int unsafe; int license_gplok;#ifdef CONFIG_MODULE_UNLOAD struct module_ref ref[NR_CPUS]; struct list_head modules_which_use_me; struct task_struct *waiter; void (*exit)(void);#endif#ifdef CONFIG_KALLSYMS Elf_Sym *symtab; unsigned long num_symtab; char *strtab; struct module_sect_attrs *sect_attrs;#endif void *percpu; char *args; };
我们插入一个内核模块,一般会使用工具insmod,该工具实际上调用了系统调用init_module,在该系统调用函数中,首先调用load_module,把用户空间传入的整个内核模块文件创建成一个内核模块,返回一个structmodule结构体。内核中便以这个结构体代表这个内核模块。
state是模块当前的状态。它是一个枚举型变量,可取的值为:MODULE_STATE_LIVE,MODULE_STATE_COMING,MODULE_STATE_GOING。分别表示模块当前正常使用中(存活状态),模块当前正在被加载,模块当前正在被卸载。load_module函数中完成模块的部分创建工作后,把状态置为MODULE_STATE_COMING,sys_init_module函数中完成模块的全部初始化工作后(包括把模块加入全局的模块列表,调用模块本身的初始化函数),把模块状态置为MODULE_STATE_LIVE,最后,使用rmmod工具卸载模块时,会调用系统调用delete_module,会把模块的状态置为MODULE_STATE_GOING。这是模块内部维护的一个状态。
list是作为一个列表的成员,所有的内核模块都被维护在一个全局链表中,链表头是一个全局变量struct module *modules。任何一个新创建的模块,都会被加入到这个链表的头部,通过modules->next即可引用到。
name是模块的名字,一般会拿模块文件的文件名作为模块名。它是这个模块的一个标识。
另外,还要介绍一下宏THIS_MODULE,它的定义如下是#define THIS_MODULE(&__this_module),__this_module是一个structmodule变量,代表当前模块,跟current有几分相似。可以通过THIS_MODULE宏来引用模块的structmodule结构,试试下面的模块:
#include <linux/module.h> MODULE_LICENSE("Dual BSD/GPL"); static int hello_init(void) { unsigned int cpu = get_cpu(); struct module *mod; printk(KERN_ALERT "this module: %p==%p\n", &__this_module, THIS_MODULE ); printk(KERN_ALERT "module state: %d\n", THIS_MODULE->state ); printk(KERN_ALERT "module name: %s\n", THIS_MODULE->name ); list_for_each_entry(mod, *(&THIS_MODULE->list.prev), list ) printk(KERN_ALERT "module name: %s\n", mod->name ); return 0; } static void hello_exit(void) { printk(KERN_ALERT "module state: %d\n", THIS_MODULE->state ); } module_init(hello_init); module_exit(hello_exit);
- 结构体 struct module 分析
- 结构体struct module
- 结构体struct module
- 结构体struct module(1)
- struct module结构体 和 THIS_MODULE详解
- struct module结构体 和 THIS_MODULE详解
- linux内核中结构体struct module
- struct module结构体 和 THIS_MODULE详解
- struct object 结构体分析
- struct--tcphdr 结构分析
- struct sk_buff与struct socket及struct sock 结构体分析
- C# 深入分析 结构struct
- struct module
- gcc 中结构体(struct)内存对齐问题分析
- #pragma预处理分析 和 struct 结构体大小的计算
- linux下的task struct结构体分析
- 结构体(struct) 用法
- 什么结构体 struct
- 文件系统Minifilter驱动
- 9.9随笔
- 重新认识字符串和字符型数据
- GWT-Ext开发之HelloWorld
- 英语面试技巧以及准备工作
- 结构体 struct module 分析
- IT人员迅速提升自我效率的十大方法
- Fedora 9 安装vmware tools无kernel-devel的问题
- 向sql server中插入中文字段出现问号??
- win7重定向函数引发的问题
- MOF编译器无法连接VMI服务器。原因可能是语义错误的解决方案
- CentOS 6 无法上网问题
- 持久化的一些相关概念
- 修复exe文件关联