2013-07-29 59 views
0

我有一個像結構:如何將節點添加到內核鏈表

struct test 
{ 
    int a; 
    struct* page p; 
    struct list_head l; 
}; 

我已經檢查include/linux/list.h,但我不知道我應該如何添加到我的名單。我做的是:

test* node_1 = (struct test*)malloc(sizeof(test)); 
node_1->a = 10; 
node_1->p = alloc_page(); 
LIST_HEAD(node_1->l); 
// for the rest nodes 
test* node_2 = (struct test*)malloc(sizeof(test)); 
node_2->a = 20; 
node_2->p = alloc_page(); 
//can I use list_add or should I write a custom add? 
list_add(node_2,node_1); //??? 

對於加入list_add第二個參數應該是node_1每個節點?

回答

1

@Sweet,我開發了一個使用內核暴露列表的小內核模塊,如果您有任何疑問或反饋,請通過並回復給我。

void todo_add_entry(struct todo_struct *new) 
{ 
#ifdef QUEUE 
    list_add_tail(&new->list, &first.list); 
#else 
    list_add(&new->list, &first.list); 
#endif  
} 

void todo_del_entry(struct todo_struct *new) 
{ 
    struct todo_struct *str, *temp; 

    list_for_each_entry_safe(str, temp, &first.list, list) 
    { 
     if((str->priority == new->priority) && (str->name == new->name)) 
     { 
      list_del(&str->list); 
#ifdef DEBUG    
      dmsg("Same:%d \t %d\n", str->priority, new->priority);   
#endif   

      kfree(str); 
     } 
    }  
} 

#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,36) 
static long 
list_minor_ioctl(struct file *filp, U32 cmd, unsigned long arg) 
#else 
static int 
list_minor_ioctl(struct inode *inode, struct file *filp, U32 cmd, 
       unsigned long arg) 
#endif 
{ 
    S32 res = 0; 
    S32 err = 0; 
    struct todo_struct *new; 
    struct test *a; 

    if(_IOC_TYPE(cmd) != IOCTL_LIST_MAGIC) 
     return -ENOTTY; 

    if(_IOC_NR(cmd) > IOCTL_MAX_CMDS) 
     return -ENOTTY; 

    if(_IOC_DIR(cmd) & _IOC_READ) 
     err = !access_ok(VERIFY_WRITE, (void __user*) arg, _IOC_SIZE(cmd));    

    if(_IOC_DIR(cmd) & _IOC_WRITE) 
     err = !access_ok(VERIFY_READ, (void __user*) arg, _IOC_SIZE(cmd));    

    if(err) 
     return -EFAULT; 

    new = kmalloc(sizeof(*new), GFP_KERNEL);  

    a = (struct test *) arg; 

    switch(cmd) 
    { 
    case IOCTL_LIST_ADD: 

     new->priority = a->priority; 
     new->name  = a->name; 

     INIT_LIST_HEAD(&new->list); 

     todo_add_entry(new); 

     break; 

    case IOCTL_LIST_DEL: 
     new->priority = a->priority; 
     new->name  = a->name; 

#ifdef DEBUG   
     dmsg("prio:%d \t name:%s \n", new->priority, new->name); 
#endif  

     todo_del_entry(new);  
     break; 

    default: /* Default case --> Ignore, will never get executed */ 
     break; 
    } 

    return res; 
} 
+0

謝謝,但我沒有訪問上述鏈接。 – Sara

+0

即使在博客中粘貼的來源看起來很奇怪。等一下我只會在這裏發佈它:-)。 –

+1

如果使用list_add_tail它遵循QUEUE實現,但如果使用list_add,則遵循STACK實現。 –