2

我对从我的内核模块的 C 程序中显示内核版本有疑问。所以插入后,当我通过 dmesg 显示日志消息时,我能够看到我的内核版本。

所以我的简单 C 代码如下,任何人都可以告诉我如何在插入后显示内核版本,如果我想在程序中插入“谁”也一样。所以在这里你给我解决方案如何编程或我需要包含哪个结构,以便我能够在插入模块后显示主机名和内核版本。

程序:

#include<linux/init.h>      //for init modules
#include<linux/module.h>    //for kernel modules
#include<linux/kernel.h>    //for kernel function

MODULE_LICENSE("GPL");      //For giving licence to module
MODULE_AUTHOR("RAVI BHUVA");    //For authorization of module

static int __init init_hello(void)  //for initialation of module this function is used
{
  printk(KERN_INFO "Hello Master. \nYou are currently using linux ");
  return(0);
}

static void __exit exit_hello(void) //for exiting from module this function is used
{
  printk(KERN_INFO "Good Bye\n");
}

module_init(init_hello);        //for initialation of module
module_exit(exit_hello);        //for exiting from module
4

3 回答 3

3

您可以使用 UTS_RELEASE 变量打印 linux 的版本。只是打印它。和广告头文件#include

于 2012-12-16T06:09:11.877 回答
2

按宏用法。

#include<linux/init.h>      //for init modules
#include<linux/module.h>    //for kernel modules
#include<linux/kernel.h>    //for kernel function
#include<generated/utsrelease.h>//For UTS_RELEASE MACRO

MODULE_LICENSE("GPL");      //For giving licence to module
MODULE_AUTHOR("RAVI BHUVA");    //For authorization of module

static int __init init_hello(void)  //for initialation of module this function is used
{
printk(KERN_INFO "Hello Master. \nYou are currently using linux %s\n",UTS_RELEASE);//By using macro here i print version of kernel.
return(0);
}

static void __exit exit_hello(void) //for exiting from module this function is used
{
printk(KERN_INFO "Good Bye\n");
}

module_init(init_hello);        //for initialation of module
module_exit(exit_hello);        //for exiting from module

通过这种方式,您可以显示内核版本。

于 2012-12-16T06:45:49.320 回答
0

上述解决方案将打印您的模块编译时使用的内核版本。因此,如果您希望模块打印正在运行的内核版本,这将起作用:

#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/utsname.h>

static int __init hello_init(void)
{
        pr_alert("You are currently using Linux %s\n", utsname()->release);
        return 0;
}

static void __exit hello_exit(void)
{
        pr_alert("Bye");
}

module_init(hello_init);
module_exit(hello_exit);
MODULE_LICENSE("GPL");

如果您查看 proc 文件系统,则在 path 中有一个文件,/proc/version它会吐出一个Linux version 3.2.0-56-generic (buildd@batsu) (gcc version 4.6.3 (Ubuntu/Linaro 4.6.3-1ubuntu5) ) #86-Ubuntu SMP Wed Oct 23 17:31:43 UTC 2013描述正在运行的内核版本的字符串。在进一步检查/proc/version.c实际在 proc fs 中实现版本文件的内核源代码时,您将看到负责输出字符串的这段代码:

static int version_proc_show(struct seq_file *m, void *v)
{
        seq_printf(m, linux_proc_banner,
                utsname()->sysname,
                utsname()->release,
                utsname()->version);
        return 0;
}
于 2014-01-26T03:03:48.603 回答