2012-12-15 2 views
2

J'ai une question concernant l'affichage de la version du noyau de mon programme C du module noyau. donc après l'insertion je suis capable de voir ma version de noyau quand j'affiche des messages de log par dmesg. Donc mon code C simple est comme ci-dessous et s'il vous plaît quelqu'un peut-il me dire comment je peux afficher la version du noyau après l'insertion et même si je veux insérer "who" dans le programme alors. alors ici vous me donnez une solution comment programmer ou quelle structure je dois inclure afin que je puisse afficher la version du nom d'hôte et du noyau après l'insertion du module.Comment afficher la version du noyau lorsque j'insère un module dans un fichier c uniquement?

Programme:

#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 

Répondre

3

vous pouvez imprimer la version de linux en utilisant la variable UTS_RELEASE. il suffit de l'imprimer. et le fichier d'en-tête d'annonce #include

2

Par utilisation de macros.

#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 

De cette façon, vous pouvez afficher la version du noyau.

0

La solution ci-dessus affichera la version du noyau de votre module. Compilé avec. Donc, si vous voulez le module pour imprimer la version du noyau est en cours d'exécution sur ce fonctionnera:

#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"); 

Si vous regardez le système de fichiers proc il y a un fichier au chemin /proc/version qui recrache une chaîne Linux version 3.2.0-56-generic ([email protected]) (gcc version 4.6.3 (Ubuntu/Linaro 4.6.3-1ubuntu5)) #86-Ubuntu SMP Wed Oct 23 17:31:43 UTC 2013 décrivant la version du noyau en cours d'exécution. En examinant de plus près /proc/version.c le code source du noyau qui implémente réellement le fichier de version dans le proc fs, vous verrez ce morceau de code qui est responsable de la chaîne de sortie:

Questions connexes