I have two modules. I want the modules to be interdependent while doing insmod or rmmod. Currently, my module2 is dependent on module1. If I insert module1 then module2, it works fine. On the other hand, the reverse of it doesn't work. This is logical in explanation. However, I want a neat code to avoid such dependencies. If I do insmod of mod2 then mod1 should automatically be insmod, Or some other decent way to tackle this issue. Here are my two modules.
static int multiplyMod1(int a, int b);
/** Once the symbol is exported, check in /proc/kallsyms **/
/** Call multiplyMod1 from other module **/
/** Ensure that this symbol doesn't exist somewhere **/
EXPORT_SYMBOL(multiplyMod1);
static int multiplyMod1(int a, int b)
{
return a*b;
}
static int mod1_init(void)
{
printk(KERN_ALERT "Hello mod1..Init\n");
return 0;
}
static void mod1_exit(void)
{
printk(KERN_ALERT "Goodbye..mod1\n");
}
module_init(mod1_init);
module_exit(mod1_exit);
The makefile -
obj-m += mod1.o
all:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules modules_install
clean:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean
Here is my second module. This is dependent on the module 1.
/** Ensure that the mod1 is insmod so that multiplyMod1 symbol is available **/
extern int multiplyMod1(int,int);
static int mod2_init(void)
{
printk(KERN_ALERT "Hello mod2..Init\r\n");
printk(KERN_ALERT "The Multiplication result from Mod2 is..%d\r\n",multiplyMod1(49,7));
return 0;
}
static void mod2_exit(void)
{
printk(KERN_ALERT "Goodbye..mod2\r\n");
}
module_init(mod2_init);
module_exit(mod2_exit);
You can use request_module API in module
mod2
in which you want to use exported symbol, this loads required module on demand if not loaded previously.In module
mod2
,Don't forget to increment module reference count using try_module_get()