一個(gè)簡(jiǎn)單的內(nèi)核模塊來(lái)讀取 timespec 數(shù)據(jù)結(jié)構(gòu)的數(shù)據(jù)吏颖。
"read_kernel_time.c":
#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/timekeeping.h>
struct timespec ts;
static int __init init_k_time(void)
{
printk(KERN_INFO "Get kernel time:\n");
ts = current_kernel_time();
printk(KERN_INFO "ts->tv_sec: %ld\t ts->tv_nsec: %ld\n", ts.tv_sec, ts.tv_nsec);
return 0;
}
static void __exit exit_k_time(void)
{
printk(KERN_INFO "Exit\n");
}
module_init(init_k_time);
module_exit(exit_k_time);
MODULE_LICENSE("GPL");
"Makefile":
obj-m := read_kernel_time.o
all:
make -C /lib/modules/`uname -r`/build M=$(PWD) modules
clean:
make -C /lib/modules/`uname -r`/build M=$(PWD) clean
依賴的編譯包:gcc, make, kernel-devel.
特殊情況球拦,在fedora下餐蔬,默認(rèn)系統(tǒng)里沒(méi)有kernel-devel包庆揩,如果此時(shí)安裝的話,版本會(huì)比當(dāng)前的kernel版本高态兴,此時(shí)編譯時(shí)會(huì)出現(xiàn)類似這樣的錯(cuò)誤:
make[1]: *** /lib/modules/4.14.13-200.fc26.x86_64/build: No such file or directory. Stop.
make: *** [Makefile:4: all] Error 2
錯(cuò)誤就是當(dāng)前kernel版本是"4.14.13-200.fc26",但是直接用"dnf install kernel-devel"安裝的版本是"4.14.14-200.fc26"。這種情況下可以用下面的命令來(lái)安裝:
sudo dnf install "kernel-devel-uname-r == $(uname -r)"
Reference:
Hello, World (part 1): The Simplest Module
yum-installs-kernel-devel-different-from-my-kernel-version