在运行时从linux内核模块获取内核版本 [英] Getting kernel version from linux kernel module at runtime

查看:266
本文介绍了在运行时从linux内核模块获取内核版本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从linux内核模块代码(内核模式)内部获取有关正在运行哪个版本的内核的运行时信息?

how can I obtain runtime information about which version of kernel is running from inside linux kernel module code (kernel mode)?

推荐答案

按照惯例,Linux内核模块加载机制不允许加载未针对正在运行的内核编译的模块,因此您所指的正在运行的内核"很有可能是在内核模块编译时就知道的.

By convention, Linux kernel module loading mechanism doesn't allow loading modules that were not compiled against the running kernel, so the "running kernel" you are referring to is most likely is already known at kernel module compilation time.

要检索版本字符串常量,较旧的版本要求您包括<linux/version.h>,其他<linux/utsrelease.h>和较新的<generated/utsrelease.h>.如果您真的想在运行时获取更多信息,那么linux/utsname.h中的utsname()函数是最标准的运行时界面.

For retrieving the version string constant, older versions require you to include <linux/version.h>, others <linux/utsrelease.h>, and newer ones <generated/utsrelease.h>. If you really want to get more information at run-time, then utsname() function from linux/utsname.h is the most standard run-time interface.

虚拟/proc/version procfs节点的实现使用utsname()->release.

The implementation of the virtual /proc/version procfs node uses utsname()->release.

如果要在编译时根据内核版本来调节代码,则可以使用预处理器块,例如:

If you want to condition the code based on kernel version in compile time, you can use a preprocessor block such as:

#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,16)
...
#else
...
#endif

它允许您与主要/次要版本进行比较.

It allows you to compare against major/minor versions.

这篇关于在运行时从linux内核模块获取内核版本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆