2

カーネル モジュールの C プログラムからカーネル バージョンを表示する方法について質問があります。そのため、挿入後、dmesg でログ メッセージを表示すると、カーネルのバージョンを確認できます。

したがって、私の単純な C コードは以下のとおりです。挿入後にカーネル バージョンを表示する方法と、プログラムに「who」を挿入したい場合も同じ方法を教えてください。ここでは、モジュールの挿入後にホスト名とカーネルバージョンを表示できるように、プログラム方法または含める必要がある構造の解決策を教えてください。

プログラム:

#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 ファイル システムを見ると、実行中のカーネルのバージョンを説明/proc/versionする文字列を出力するファイルが path にあります。実際にバージョン ファイルを proc fs に実装しているカーネルのソース コードを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

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 に答える