I'm newie programming Linux modules: I'm making a proc to add and show values of a list. (and later remove)
This is the code:
(without the includes here)
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);
If if use "echo add X > /proc/queue" (X is an int) I can add values to the list, but when I try to show the list I enter in an infinite loop and its not the list_for_each inside the read function, because all the function is in loop:
[ 7382.404493] --End of list----Listing inserted numbers--
[ 7382.404497] 1 2
[ 7382.404498] 3 4
[ 7382.404499] --End of list----Listing inserted numbers--
[ 7382.404503] 1 2
[ 7382.404504] 3 4
[ 7382.404505] --End of list----Listing inserted numbers--
[ 7382.404509] 1 2
[ 7382.404510] 3 4
[ 7382.404511] --End of list--[ 7382.404493] --End of list----Listing inserted numbers--
[ 7382.404497] 1 2
[ 7382.404498] 3 4
[ 7382.404499] --End of list----Listing inserted numbers--
[ 7382.404503] 1 2
[ 7382.404504] 3 4
[ 7382.404505] --End of list----Listing inserted numbers--
[ 7382.404509] 1 2
[ 7382.404510] 3 4
[ 7382.404511] --End of list--
I'm adding nice the nodes? Why I enter in an infinite loop when reading?
Thanks everyone who reads until here :)