2020-11-07使用内核链表
2020-11-07 本文已影响0人
sxs7
[root@localhost tst]# dmesg
[ 668.111872] --------------------------------
[ 668.111875] sxs4 val4 1
[ 668.111876] sxs3 val3 1
[ 668.111876] sxs2 val2 1
[ 668.111877] sxs1 val1 1
[ 668.111877] sxs0 val0 1
[ 668.111878] --------------------------------
[ 668.111879] sxs3 val3 1
[ 668.111879] sxs2 val2 1
[ 668.111880] sxs1 val1 1
[ 668.111880] sxs0 val0 1
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/slab.h>
#include <linux/list.h>
typedef struct _krc_pcr_node
{
char * path;
char * value;
long long cnt;
struct list_head list;
} node_info;
struct list_head node_list;
int mylist_init(void)
{
int i = 0;
node_info *node, *cur_node;
struct list_head *pos, *another;
INIT_LIST_HEAD(&node_list);
for (i = 0; i < 5; i++) {
node = vmalloc(sizeof(node_info));
memset(node, 0, sizeof(node_info));
node->path = vmalloc(8);
memset(node->path, 0, 8);
sprintf(node->path, "sxs%d", i);
node->value = vmalloc(8);
memset(node->value, 0, 8);
sprintf(node->value, "val%d", i);
node->cnt++;
list_add( &(node->list), &node_list);
}
printk("--------------------------------\n");
list_for_each_safe(pos, another, &node_list) {
cur_node = list_entry(pos, node_info, list);
printk("%s %s %lld\n", cur_node->path, cur_node->value, cur_node->cnt);
if (!strcmp(cur_node->path, "sxs4")) {
list_del(&(cur_node->list));
}
}
printk("--------------------------------\n");
list_for_each(pos, &node_list) {
cur_node = list_entry(pos, node_info, list);
printk("%s %s %lld\n", cur_node->path, cur_node->value, cur_node->cnt);
}
return 0;
}
void mylist_exit(void)
{
return;
}
module_init(mylist_init);
module_exit(mylist_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("SXS");
MODULE_DESCRIPTION("List Module");
MODULE_ALIAS("List module");
obj-m+=link.o
all:
make -C /lib/modules/$(shell uname -r)/build/ M=$(PWD) modules
clean:
make -C /lib/modules/$(shell uname -r)/build/ M=$(PWD) clean