Learning Kernel Programming Day2:
Have already explained how to insert/remove a module to/from kernel at runtime in the kernel programming #1 post.
Please go through it before reading this post #2.
We can put our own names for initialization function and cleanup functions using two macros. They are,
1. module_init
2. module_exit
These two macros allows module owners to put their own init() and cleanup() names in the module. These macros are defined in linux/init.h.
Example: module_2.c
1 #include<linux/module.h> /* Needed by all modules *MUST* */
2 #include<linux/init.h> /* Needed for module_init and module_exit macros */
3 int __init module_2_init(void) {
4 printk(KERN_ALERT "2nd module program: Module Inserted\n");
5 return 0;
6 }
7
8 void module_2_cleanup() {
9 printk(KERN_ALERT "2nd module program: Module Removed\n");
10 }
11
12 module_init(module_2_init);
13 module_exit(module_2_cleanup);
Note: Please define the init() and cleanup() functions before using these macros only, otherwise you end up with compilation errors.
Next steps:
Build module:
Insert module:
Check kernel log:
Remove module: The details for these steps are already provided in Kernel Programming #1 post. Please refer it.
Observation:
The return value of init function is either 0 or -E (error number). Change the value of the return with 1 and -1 values and check the output.
If the return value 1 is used then we see the below message after inserting module using insmod.
[1306301.170326] 2nd module program: Module Inserted
[1306301.170331] sys_init_module: module_2'->init suspiciously returned 1, it should follow 0/-E convention
[1306301.170333] sys_init_module: loading module anyway...
[1306301.170337] Pid: 23621, comm: insmod Tainted: P 2.6.32-28-generic #55-Ubuntu
[1306301.170339] Call Trace:
[1306301.170349] [<ffffffff810a0e61>] sys_init_module+0x1e1/0x260
[1306301.170356] [<ffffffff810121b2>] system_call_fastpath+0x16/0x1b
If the return value -1 is (any error code) used then the result will be
insmod: error inserting 'rtc.ko': -1 Operation not permitted
i.e. our module is not inserted/loaded into kernel because of return value -1.
Have already explained how to insert/remove a module to/from kernel at runtime in the kernel programming #1 post.
Please go through it before reading this post #2.
We can put our own names for initialization function and cleanup functions using two macros. They are,
1. module_init
2. module_exit
These two macros allows module owners to put their own init() and cleanup() names in the module. These macros are defined in linux/init.h.
Example: module_2.c
1 #include<linux/module.h> /* Needed by all modules *MUST* */
2 #include<linux/init.h> /* Needed for module_init and module_exit macros */
3 int __init module_2_init(void) {
4 printk(KERN_ALERT "2nd module program: Module Inserted\n");
5 return 0;
6 }
7
8 void module_2_cleanup() {
9 printk(KERN_ALERT "2nd module program: Module Removed\n");
10 }
11
12 module_init(module_2_init);
13 module_exit(module_2_cleanup);
Note: Please define the init() and cleanup() functions before using these macros only, otherwise you end up with compilation errors.
Next steps:
Build module:
Insert module:
Check kernel log:
Remove module: The details for these steps are already provided in Kernel Programming #1 post. Please refer it.
Observation:
The return value of init function is either 0 or -E (error number). Change the value of the return with 1 and -1 values and check the output.
If the return value 1 is used then we see the below message after inserting module using insmod.
[1306301.170326] 2nd module program: Module Inserted
[1306301.170331] sys_init_module: module_2'->init suspiciously returned 1, it should follow 0/-E convention
[1306301.170333] sys_init_module: loading module anyway...
[1306301.170337] Pid: 23621, comm: insmod Tainted: P 2.6.32-28-generic #55-Ubuntu
[1306301.170339] Call Trace:
[1306301.170349] [<ffffffff810a0e61>] sys_init_module+0x1e1/0x260
[1306301.170356] [<ffffffff810121b2>] system_call_fastpath+0x16/0x1b
If the return value -1 is (any error code) used then the result will be
insmod: error inserting 'rtc.ko': -1 Operation not permitted
i.e. our module is not inserted/loaded into kernel because of return value -1.
No comments:
Post a Comment