Module dependencies

This commit is contained in:
Ciro Santilli
2017-06-06 10:13:09 +01:00
parent 9337ea98d8
commit 5c4ae86b89
4 changed files with 110 additions and 0 deletions

View File

@@ -330,3 +330,6 @@ But TODO I don't think you can see where you are in the kernel source code and l
1. [timer](kernel_module/timer.c) 1. [timer](kernel_module/timer.c)
1. [work_from_work](kernel_module/work_from_work.c) 1. [work_from_work](kernel_module/work_from_work.c)
1. [irq](irq.c) 1. [irq](irq.c)
1. Module dependencies
1. [dep.c](kernel_module/dep.c)
1. [dep2.c](kernel_module/dep2.c)

View File

@@ -1,3 +1,4 @@
CONFIG_DEPMOD=y
CONFIG_MODINFO=y CONFIG_MODINFO=y
CONFIG_NC=y CONFIG_NC=y
CONFIG_NC_EXTRA=y CONFIG_NC_EXTRA=y

73
kernel_module/dep.c Normal file
View File

@@ -0,0 +1,73 @@
/*
Exports the lkmc_dep which dep2.ko uses.
insmod /dep.ko
# dmesg => 0
# dmesg => 0
# dmesg => ...
insmod /dep2.ko
# dmesg => 1
# dmesg => 2
# dmesg => ...
rmmod dep
# Fails because dep2 uses it.
rmmod dep2
# Dmesg stops incrementing.
rmmod dep
sys visibility:
dmesg -n 1
insmod /dep.ko
insmod /dep2.ko
ls -l /sys/module/dep/holders
# => ../../dep2
cat refcnt
# => 1
depmod:
grep dep "/lib/module/"*"/depmod"
# extra/dep2.ko: extra/dep.ko
# extra/dep.ko:
modprobe dep
# lsmod
# Both dep and dep2 were loaded.
TODO: at what point does buildroot / busybox generate that file?
*/
#include <linux/delay.h> /* usleep_range */
#include <linux/kernel.h>
#include <linux/kthread.h>
#include <linux/module.h>
MODULE_LICENSE("GPL");
int lkmc_dep = 0;
EXPORT_SYMBOL(lkmc_dep);
static struct task_struct *kthread;
static int work_func(void *data)
{
while (!kthread_should_stop()) {
printk(KERN_INFO "%d\n", lkmc_dep);
usleep_range(1000000, 1000001);
}
return 0;
}
static int myinit(void)
{
kthread = kthread_create(work_func, NULL, "mykthread");
wake_up_process(kthread);
return 0;
}
static void myexit(void)
{
kthread_stop(kthread);
}
module_init(myinit)
module_exit(myexit)

33
kernel_module/dep2.c Normal file
View File

@@ -0,0 +1,33 @@
#include <linux/delay.h> /* usleep_range */
#include <linux/kernel.h>
#include <linux/kthread.h>
#include <linux/module.h>
MODULE_LICENSE("GPL");
extern int lkmc_dep;
static struct task_struct *kthread;
static int work_func(void *data)
{
while (!kthread_should_stop()) {
usleep_range(1000000, 1000001);
lkmc_dep++;
}
return 0;
}
static int myinit(void)
{
kthread = kthread_create(work_func, NULL, "mykthread");
wake_up_process(kthread);
return 0;
}
static void myexit(void)
{
kthread_stop(kthread);
}
module_init(myinit)
module_exit(myexit)