【问题标题】:How to display kernel version when i insert any module in c file only?仅在 c 文件中插入任何模块时如何显示内核版本?
【发布时间】:2012-12-15 15:24:21
【问题描述】:

我有关于从我的内核模块的 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

【问题讨论】:

    标签: linux module kernel version


    【解决方案1】:

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

    【讨论】:

      【解决方案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
      

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

      【讨论】:

        【解决方案3】:

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

        #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 文件系统,在路径 /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;
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-12-31
          • 2018-06-18
          • 2020-09-12
          • 2019-01-19
          • 2013-01-05
          • 2014-09-03
          相关资源
          最近更新 更多