-1
私は練習プロジェクトに取り組んでいて、バンプを打っています。私はいくつかの誕生日のレコードを作成し、それらの値をカーネルログバッファに出力する単純なカーネルモジュールを作成しようとしています。私は文字列値をレコード内のcharポインタに割り当てる方法を理解するのが難しいです。これは私のコードです:カーネルモジュールを書くとき、どのように文字ポインタに文字列値を割り当てますか?
#include <linux/string.h>
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/list.h>
#include <linux/slab.h>
struct birthday{
int day;
int month;
int year;
char *name;
struct list_head list;
};
static LIST_HEAD(birthday_list);
/*this fn is called when the module is loaded*/
int simple_init(void){
struct birthday *ptr;
struct birthday *person;
person = kmalloc(sizeof(*person), GFP_KERNEL);
person->day=22;
person->month=7;
person->year=1990;
strcpy(person->name, "Jane");
struct birthday *person2;
person2 = kmalloc(sizeof(*person2), GFP_KERNEL);
person2->day=4;
person2->month=3;
person2->year=1990;
strcpy(person->name, "Michael");
struct birthday *person3;
person3 = kmalloc(sizeof(*person3), GFP_KERNEL);
person3->day=4;
person3->month=3;
person3->year=1990;
strcpy(person->name, "Tom");
struct birthday *person4;
person4 = kmalloc(sizeof(*person4), GFP_KERNEL);
person4->day=4;
person4->month=4;
person4->year=1990;
strcpy(person->name, "Lacey");
struct birthday *person5;
person5 = kmalloc(sizeof(*person5), GFP_KERNEL);
person5->day=4;
person5->month=5;
person5->year=1990;
strcpy(person->name, "Ruth");
INIT_LIST_HEAD(&person->list);
list_add_tail(&person->list, &birthday_list);
list_add_tail(&person2->list, &birthday_list);
list_add_tail(&person3->list, &birthday_list);
list_add_tail(&person4->list, &birthday_list);
list_add_tail(&person5->list, &birthday_list);
printk(KERN_INFO "Module initialized");
list_for_each_entry(ptr,&birthday_list, list){
printk(KERN_INFO "Name: %s, Birthday: %d/%d/%d\n",ptr->name,ptr->month,ptr->day,ptr->year);
}
return 0;
}
/*this fn is called when the module is removed*/
void simple_exit(void){
struct birthday *ptr, *next;
list_for_each_entry_safe(ptr, next, &birthday_list, list){
list_del(&ptr->list);
kfree(ptr);
}
printk(KERN_INFO "Removing module\n");
}
/*macros for registering module entrance and exit points*/
module_init(simple_init);
module_exit(simple_exit);
このコードは、文字列属性とそれに関連するすべての命令を取り除くとコンパイルされて動作します。つまり、コードはコンパイルされますが、実行するとシステムがフリーズします。ありがとうございました!
まず、単純なユーザ空間プログラムの中でCの基礎を学ぶべきでしょう。つまり、初期化されていないポインタの逆参照によって未定義の動作が発生しています。 – EOF