如何从linux内核模块代码(内核模式)中获取有关哪个版本的内核正在运行的运行时信息?在运行时从linux内核模块获取内核版本
6
A
回答
16
按照惯例,Linux内核模块加载机制不允许加载未针对正在运行的内核编译的模块,因此您所指的“正在运行的内核”很可能已在内核模块编译时已知。
为了检索版本字符串常量,旧版本要求您包含<linux/version.h>
,其他<linux/utsrelease.h>
和更新的<generated/utsrelease.h>
。如果您真的想在运行时获得更多信息,那么函数linux/utsname.h
是最标准的运行时接口。
执行虚拟/proc/version
procfs节点使用utsname()->release
。
如果你想调节基于在编译时内核版本的代码,你可以使用一个预处理器块,如:
#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,16)
...
#else
...
#endif
它可以让你比较兑主要/次要版本。
3
您一次只能安全地为任何一个内核版本构建模块。这意味着在运行时从模块询问是多余的。
通过查看最近的内核中的UTS_RELEASE
的值,您可以在构建时找到此值,其中包括<generated/utsrelease.h>
以及其他这样做的方式。
0
为什么我不能为任何版本构建内核模块?
由于内核模块API的设计不稳定,如内核树中所述:Documentation/stable_api_nonsense.txt
。摘要如下:
Executive Summary
-----------------
You think you want a stable kernel interface, but you really do not, and
you don't even know it. What you want is a stable running driver, and
you get that only if your driver is in the main kernel tree. You also
get lots of other good benefits if your driver is in the main kernel
tree, all of which has made Linux into such a strong, stable, and mature
operating system which is the reason you are using it in the first
place.
参见:How to build a Linux kernel module so that it is compatible with all kernel releases?
如何做到这一点在编译时被要求在:Is there a macro definition to check the Linux kernel version?
相关问题
- 1. 如何在内核模块中打印linux内核版本号
- 2. 运行Linux内核模块(Hello World)
- 3. 启动时内核模块在Linux内核中的状态
- 4. 无法在Linux内核版本4.2.3上从内核模块打开/读取文本文件
- 5. 剖析Linux内核模块
- 6. Linux内核模块调试
- 7. Linux内核模块编程
- 8. 关于linux内核模块
- 9. Linux内核模块编译
- 10. 安装Linux内核模块
- 11. Linux内核模块编译
- 12. Linux内核模块ABI(x86)
- 13. Linux内核版本号?
- 14. Linux内核版本编号
- 15. CentOS的Linux内核版本
- 16. Linux内核模块:在运行时动态加载代码
- 17. 在编写Linux内核模块时获取用户进程pid
- 18. 使用C读取linux内核版本?
- 19. 如何在安装新内核时自动执行linux内核模块编译?
- 20. 从Linux内核模块写入debugfs
- 21. 如何从Linux内核克隆模块?
- 22. 内核模块版本魔术错误
- 23. 如何从Linux内核模块获取使用次数?
- 24. 内核模块
- 25. 如何在Linux内核模块
- 26. 如何在Linux内核模块中获取CPU信息?
- 27. Linux内核函数和内核模块开发
- 28. Linux /内核:如何选择Videobuf2框架作为内核模块?
- 29. Linux内核模块:延迟workqueue使内核崩溃
- 30. 使用gcc与内核头文件编译Linux内核模块
中的utsname()函数的伎俩。谢谢。 – Bogi