我正在从事一个练习项目并且遇到了麻烦。我试图编写一个简单的内核模块,创建几个生日记录并将其值打印到内核日志缓冲区。我很难搞清楚如何将字符串值分配给记录中的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);
当我摆脱字符串属性以及与之关联的所有指令时,代码会编译并运行。原样,代码编译但当我运行它时系统冻结。谢谢大家!
答案 0 :(得分:0)
我找到了解决方案。在进行赋值之前,我忘了将内核空间分配给我的字符串。这是更正的代码,现在可以使用。
#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,*person2,*person3,*person4,*person5;
person = kmalloc(sizeof(*person), GFP_KERNEL);
person->day=22;
person->month=7;
person->year=1990;
person->name=kmalloc(sizeof("Jane")+1, GFP_KERNEL);
strcpy(person->name, "Jane");
person2 = kmalloc(sizeof(*person2), GFP_KERNEL);
person2->day=4;
person2->month=3;
person2->year=1990;
person2->name=kmalloc(sizeof("Michael")+1, GFP_KERNEL);
strcpy(person2->name, "Michael");
person3 = kmalloc(sizeof(*person3), GFP_KERNEL);
person3->day=4;
person3->month=3;
person3->year=1990;
person3->name=kmalloc(sizeof("Lacey")+1, GFP_KERNEL);
strcpy(person3->name, "Lacey");
person4 = kmalloc(sizeof(*person4), GFP_KERNEL);
person4->day=4;
person4->month=4;
person4->year=1990;
person4->name=kmalloc(sizeof("Tom")+1, GFP_KERNEL);
strcpy(person4->name, "Tom");
person5 = kmalloc(sizeof(*person5), GFP_KERNEL);
person5->day=4;
person5->month=5;
person5->year=1990;
person5->name=kmalloc(sizeof("Stuart")+1, GFP_KERNEL);
strcpy(person5->name, "Stuart");
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);
谢谢!