LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 02-24-2014, 09:06 AM   #1
rahil khera
LQ Newbie
 
Registered: Sep 2009
Posts: 14

Rep: Reputation: 0
copy_from_user getting call infinite time in kernel level programming


I am trying following program.



Code:
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/proc_fs.h>
#include<linux/sched.h>
#include <asm/uaccess.h>
#include <linux/slab.h>

int len,temp;
struct rw_semaphore sem;
char *msg;
size_t buffer_size= 0;
int read_proc(struct file *filp,char *buf,size_t count,loff_t *offp ) 
{

down_read(&sem);
//copy_to_user(buffer1,"In reader", 1024);
if(count > 1024)
{
	buffer_size = 1024;
}
else
{
	buffer_size = count;
}
copy_to_user(buf,msg,buffer_size);
up_read(&sem);   
return 0;
}

int write_proc(struct file *filp,const char *buf,size_t count,loff_t *offp)
{
down_write(&sem);
/*if(count > 1024)
{
	buffer_size = 1024;
}
else
{
	buffer_size = count;
}*/

//copy_to_user(buffer2,"In writer", 1024);
copy_from_user(msg,buf,buffer_size);
up_write(&sem);
return 0;
}

struct file_operations proc_fops = 
{
read: read_proc,
write: write_proc
};

void create_new_proc_entry() 
{
proc_create("hello1",0,NULL,&proc_fops);
msg=kmalloc(GFP_KERNEL,1024*sizeof(char));
//*msg = "PICT";
}

int proc_init (void) 
{
 create_new_proc_entry();
 init_rwsem(&sem);
 *msg = "PICT";
printk(KERN_INFO "msg :- %s \n", *msg);
 
 return 0;
}

void proc_cleanup(void) 
{
 remove_proc_entry("hello1",NULL);
}

MODULE_LICENSE("GPL"); 
module_init(proc_init);
module_exit(proc_cleanup);

After successfull make, when i try running cat /proc/hello1 command, it does not give any output and when i try running echo "hi" >> /proc/hello1 command, it goes into infinite loop and system gets dump.

Please tell me my mistake or how I should modify it to make it work.?
 
  


Reply

Tags
kernel module, reader, semaphore, writer



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
copy_from_user() function fails inside a kernel thread. rajneesh.gaur Linux - Newbie 1 02-18-2012 04:58 PM
linux kernel level socket programming yethish Programming 1 07-05-2011 03:05 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 08:56 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration