Note that there are some explanatory texts on larger screens.

plurals
  1. POCode for writing and reading on a device file from a kernel module?
    primarykey
    data
    text
    <p>I have tried the following code many times .</p> <pre><code>#include&lt;linux/module.h&gt; #include&lt;linux/kernel.h&gt; #include&lt;linux/fs.h&gt; #include&lt;linux/cdev.h&gt; #include&lt;asm/uaccess.h&gt; #include&lt;linux/semaphore.h&gt; MODULE_LICENSE("DUAL BSD/GPL"); static int dev_open(struct inode *,struct file *); static int dev_release(struct inode *,struct file *); ssize_t dev_read(struct file *,char *, size_t ,loff_t *); ssize_t dev_write(struct file *,const char *,size_t ,loff_t *); static int major; int dev_major = 0; int dev_minor = 0; struct cdev *cdev; struct device { char array[100]; struct semaphore sem; }chr_arr; struct file_operations dev_ops = { .owner = THIS_MODULE, .read = dev_read, .write = dev_write, .open = dev_open, .release = dev_release }; ssize_t dev_read(struct file *filp,char *buf,size_t count,loff_t *offset) { int i; i=copy_to_user(buf,chr_arr.array,count); printk(KERN_ALERT"buff:%s",buf); return i; } ssize_t dev_write(struct file *filp,const char *buf,size_t count,loff_t *offset) { //printk(KERN_ALERT"\nsorry,byebye"); int j; //msg_ptr = kmalloc(count,GFP_KERNEL); //for(j=0;j&lt;count;j++) if(count&gt;100) return -1; j = copy_from_user(chr_arr.array,buf,count); //printk(KERN_ALERT"msg_ptr:%s",msg_ptr); return j; } static int dev_open(struct inode *inode,struct file *filp) { filp-&gt;private_data = inode-&gt;i_cdev; if(down_interruptible(&amp;chr_arr.sem)) { printk(KERN_INFO " could not hold semaphore"); return -1; } //printk(KERN_ALERT"ah ha the device is open !now we can go further"); return 0; } static int dev_release(struct inode *inode,struct file *filp) { up(&amp;chr_arr.sem); //module_put(THIS_MODULE); return 0; } static int init_device(void) { int result; dev_t dev_no,dev; result = alloc_chrdev_region(&amp;dev_no,0,1,"chr_dev"); if(result &lt; 0) { printk("sorry no major number left"); return result; } major = MAJOR(dev_no); dev = MKDEV(major,0); cdev = cdev_alloc(); cdev-&gt;ops = &amp;dev_ops; sema_init(&amp;chr_arr.sem,1); printk("the major number allocated is %d\n",major); result = cdev_add(cdev,dev,1); if(result &lt; 0 ) { printk(KERN_INFO "Unable to allocate cdev"); return result; } return 0; } static void clean_device(void) { cdev_del(cdev); unregister_chrdev_region(major,1); } module_init(init_device); module_exit(clean_device); </code></pre> <p>but its giving me the following warning.</p> <pre><code>CC [M] /home/karan/practice/scrw/scrw1.o In file included from /usr/src/linux-2.6.34.10-0.6/arch/x86/include/asm/uaccess.h:571:0, from /home/karan/practice/scrw/scrw1.c:4: In function ‘copy_from_user’,inlined from ‘write’ at /home/karan/practice/scrw/scrw1.c:43:6: /usr/src/linux-2.6.34.10-0.6/arch/x86/include/asm/uaccess_32.h:212:26: warning: call to ‘copy_from_user_overflow’ declared with attribute warning: copy_from_user() buffer size is not provably correct Building modules, stage 2. MODPOST 1 modules CC /home/karan/practice/scrw/scrw1.mod.o LD [M] /home/karan/practice/scrw/scrw1.ko </code></pre> <p>and then when I try to write echo hi > /dev/my_dev the screen freezes after 30 secs or so.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload