1
我是Linux内核编程的小老虎,我想我能找到答案(因为它看起来很简单),但还没有运气。我需要制作一个打印内核版本号的Linux内核模块。加载时需要实现一个显示此类消息的模块:如何在内核模块中打印linux内核版本号
“Hello Master。您当前正在使用Linux(版本)”,其中(版本)是内核版本号。
我该怎么做?我尝试使用uname(http://man7.org/linux/man-pages/man2/uname.2.html),但是当我包含sys/utsname.h时,使用我的makefile编译时出现致命错误
“Can not open include file:'sys/utsname.h':No such file or directory ”。
这里是我的模块
#undef __KERNEL__
#define __KERNEL__
#undef __MODULE__
#define __MODULE__
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <sys/utsname.h>
struct utsname unameData;
static int __init config_init(void)
{
uname(&unameData);
printk(KERN_INFO "Version number is %s\n", unameData.version);
return 0;
}
static void __exit config_exit(void)
{
printk(KERN_INFO "config_exit executed with success\n");
return;
}
module_init(config_init);
module_exit(config_exit);
MODULE_LICENSE("GPL");
的Makefile
obj-m := config.o
KDIR := /lib/modules/$(shell uname -r)/build
PWD := $(shell pwd)
all:
$(MAKE) -C $(KDIR) M=$(PWD) modules
clean:
$(MAKE) -C $(KDIR) M=$(PWD) clean
完美!谢谢。我不知道“linux/utsname.h”....我试图使用“sys/utsname.h”,但是当我切换它工作正常。 – 2014-12-07 17:22:54