内核Linux模块在读取列表时触发循环循环

时间:2017-06-02 16:25:27

标签: linux list linux-kernel kernel-module proc

我正在编程Linux模块:我正在制作一个proc来添加和显示列表的值。 (后来删除)

这是代码:

(此处不包括)

isTapToFocusEnabled

如果使用" echo add X>的/ proc /队列" (X是一个int)我可以在列表中添加值,但是当我尝试显示列表时,我输入了一个无限循环,而不是read函数中的list_for_each,因为所有函数都在循环中:

int ret,temp,temp2;

struct k_list {
    struct list_head links;
    int data;
};

struct list_head my_list;

int pop_queue(struct file *filp,char *buf,size_t count,loff_t *offp) 
{
    char kbuf[128];
    struct list_head *iter;
    struct k_list *objPtr;

    printk(KERN_INFO "--Listing inserted numbers--");

    int contador = 0;
    list_for_each(iter,&my_list) {
        objPtr = list_entry(iter, struct k_list, links);
        printk(KERN_INFO "%d ", objPtr->data);            
        contador++;
    }
    printk(KERN_INFO "--End of list--");

    return count;
}

int push_queue(struct file *filp,const char *buf,size_t count,loff_t *offp)
{
    char kbuf[128];
    int w;
    temp=copy_from_user(kbuf,buf,count);
    if(sscanf(kbuf,"add %i",&w)==1){
        printk(KERN_INFO "\n add = %i \n" ,w);
        struct k_list * newNode = (struct  k_list *)vmalloc(sizeof(struct k_list));
        if (newNode == NULL){
            printk(KERN_INFO "Something went wrong");
        }
        newNode->data = w;
        //INIT_LIST_HEAD(&newNode->links);
        list_add_tail(&newNode->links,&my_list);
    }
    if(sscanf(kbuf,"remove %i",&w)==1) {
        printk(KERN_INFO "\n remove = %i \n" ,w);
    }

    return count;
}

struct file_operations proc_fops = {
    .read = pop_queue, 
    .write = push_queue,
};

int queue_init (void) {
    INIT_LIST_HEAD(&my_list);
    proc_create("queue",0666,NULL,&proc_fops);
    return 0;
}

void queue_cleanup(void) {
    remove_proc_entry("queue",NULL);
}

MODULE_LICENSE("GPL"); 
module_init(queue_init);
module_exit(queue_cleanup);

我添加好节点?为什么我在阅读时进入无限循环?

感谢所有读到这里的人:)

0 个答案:

没有答案