我正在开发一个Linux内核模块,我想调用函数get_futex_key
.我怎么能这样做呢?最终目标是使用内核函数返回包含给定Futex的线程列表.
我按照这个tutorial创建了一个"Hello World"模块.它使用printk
函数.我理解使用printk
是可能的,因为它是内核可见的符号之一.
# cat /proc/kallsyms | grep printk
...
ffffffff9ebb9c32 T printk
...
功能get_futex_key
在/proc/kallsyms
中也可见.它是在futex.c
,而不是futex.h
中定义的.例如,请看这个source.
# cat /proc/kallsyms | grep "get_futex_key"
ffffffff9e3223c0 t get_futex_key_refs.isra.11
ffffffff9e322a30 t get_futex_key
假设0x20566a0
是用strace
得到的uaddr
.
# strace -p 117589
strace: Process 117589 attached
futex(0x20566a0, FUTEX_WAIT_BITSET_PRIVATE|FUTEX_CLOCK_REALTIME, 0, NULL, 0xffffffff^Cstrace: Process 117589 detached
我以fut.c
的身份try 了以下代码.
#include <linux/module.h> // included for all kernel modules
#include <linux/kernel.h> // included for KERN_INFO
#include <linux/init.h> // included for __init and __exit macros
#include <linux/futex.h>
#include <kernel/futex.c>
MODULE_LICENSE("GPL");
MODULE_AUTHOR("FutexTestAuthor");
MODULE_DESCRIPTION("Futex Examiner");
static int __init hello_init(void)
{
union futex_key key1 = FUTEX_KEY_INIT;
printk(KERN_INFO "Get futex key\n");
//futex_key key1 = FUTEX_KEY_INIT;
get_futex_key(0x20566a0, 0, &key1, VERIFY_READ);
printk(KERN_INFO "key1 = %p\n", &key1);
return 0; // Non-zero return means that the module couldn't be loaded.
}
static void __exit hello_cleanup(void)
{
printk(KERN_INFO "Cleaning up futex module.\n");
}
module_init(hello_init);
module_exit(hello_cleanup);
The 100
obj-m += fut.o
all:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
clean:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean
Output of 100
# make
make -C /lib/modules/4.15.0-204-generic/build M=/home/myusername/git/LinuxExamples/kernel/fut modules
make[1]: Entering directory '/usr/src/linux-headers-4.15.0-204-generic'
CC [M] /home/myusername/git/LinuxExamples/kernel/fut/fut.o
/home/myusername/git/LinuxExamples/kernel/fut/fut.c:5:10: fatal error: kernel/futex.c: No such file or directory
#include <kernel/futex.c>
^~~~~~~~~~~~~~~~
compilation terminated.
scripts/Makefile.build:340: recipe for target '/home/myusername/git/LinuxExamples/kernel/fut/fut.o' failed
make[2]: *** [/home/myusername/git/LinuxExamples/kernel/fut/fut.o] Error 1
Makefile:1596: recipe for target '_module_/home/myusername/git/LinuxExamples/kernel/fut' failed
make[1]: *** [_module_/home/myusername/git/LinuxExamples/kernel/fut] Error 2
make[1]: Leaving directory '/usr/src/linux-headers-4.15.0-204-generic'
Makefile:4: recipe for target 'all' failed
make: *** [all] Error 2