All of lore.kernel.org
 help / color / mirror / Atom feed
From: dexter.haslem@gmail.com (Dexter Haslem)
To: kernelnewbies@lists.kernelnewbies.org
Subject: copy_to_user
Date: Wed, 22 Dec 2010 18:18:27 -0700	[thread overview]
Message-ID: <4D12A363.60404@gmail.com> (raw)
In-Reply-To: <792436.73318.qm@web94712.mail.in2.yahoo.com>

On 12/22/2010 5:59 PM, Hemanth Kumar wrote:
> Hi All,
>
>             I have small problem with copy_to_user in read function,below is my code,when I try to read from userspace I get segmentation fault,
> Can any please point me where I went wrong,
>
>
> #include<linux/kernel.h>
> #include<linux/module.h>
> #include<linux/init.h>
> #include<linux/types.h>
> #include<linux/proc_fs.h>
> #include<linux/fs.h>
> #include<linux/kdev_t.h>
> #include<linux/jiffies.h>
> #include<linux/cdev.h>
> #include<asm/uaccess.h>
> #include<linux/mutex.h>
>
> struct mutex timer;
> static struct cdev my_cdev;
> dev_t devn;
> int maj = 300;
> int min = 0;
> int count = 1;
> char modname[] = "mytimer";
> short x[10] = {1,2,3,4,5,6,7,8,9,10};
>
>
>
>
> ssize_t my_read(struct file *file,char *buf,size_t count,loff_t *pos){
>       unsigned long res;
>       void *k = (void *)&x;
>            void *l = (void *)&x+1;
>            void *j = (void *)&x+2;
>
>                       mutex_lock(&timer);
>                               res =    copy_to_user(buf,k,sizeof(short));
>                               res =    copy_to_user(buf,l,sizeof(short));
>                               res =    copy_to_user(buf,j,sizeof(short));
>
>                         /*    res =  copy_to_user(buf,&x+4,sizeof(short));
>                               res =    copy_to_user(buf,&x+5,sizeof(short));
>                               res =    copy_to_user(buf,&x+6,sizeof(short));
>                               res =    copy_to_user(buf,&x+7,sizeof(short));
>                               res =    copy_to_user(buf,&x+8,sizeof(short));
>                               res =    copy_to_user(buf,&x+9,sizeof(short));
>                          */
>                   mutex_unlock(&timer);
>
>     return 20;
>
> }
>
>
> static struct file_operations my_fops = {
>                   .owner = THIS_MODULE,
>                   .read = my_read,
>
> };
>
>
> static int __init my_init(void){
>          int ret;
>     devn = MKDEV(maj,min);
>
>       ret = register_chrdev_region(devn,count,modname);
>
>        cdev_init(&my_cdev,&my_fops);
>        cdev_add(&my_cdev,devn,count);
>
>        printk("<1>  Register timer maj = %d\n",maj);
>
>
>
>
>   return 0;
> }
>
>
>
> static void __exit my_exit(void){
>
>        cdev_del(&my_cdev);
>         unregister_chrdev_region(devn,count);
>          printk("<1>  Bye Bye \n");
>
> }
>
>
> module_init(my_init);
> module_exit(my_exit);
> MODULE_LICENSE("Dual BSD/GPL");
>
>
>
>
> my userspace App:
>
> #include<stdio.h>
> #include<fcntl.h>
> #include<stdlib.h>
> #include<unistd.h>
>
> int main()
> {
>           int nbytes ;
>           char n[20];
>           short a = *((short *)&n[0]);
>           short b = *((short *)&n[2]);
>           short c = *((short *)&n[4]);
>
>          int     fd = open( "/dev/mytimer", O_RDONLY );
>          if ( fd<  0 ) { perror( "/dev/mytimer" ); exit(1); }
>
> while ( 1 )
>                  {
>
>                   nbytes = read( fd, n, 40 );
>                  if ( nbytes<  0 ) break;
>
>                  printf( "\r a = %d \n ", a);
>                  printf("\r b = %d \n",b);
>                  printf("\r c = %d \n",c);
>
>                  sleep(1);
>                  fflush( stdout );
>                  }
> return 0;
> }
>
>
>
>
> Best regards,
>
>
>
>
>
> _______________________________________________
> Kernelnewbies mailing list
> Kernelnewbies at kernelnewbies.org
> http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies
Hi,

At first glance, you have char n[20], but read 40 bytes in the read 
call. char is only 1 byte on x86 I believe so that might be your problem.

-- 
-Dexter Haslem

  reply	other threads:[~2010-12-23  1:18 UTC|newest]

Thread overview: 24+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-12-23  0:59 copy_to_user Hemanth Kumar
2010-12-23  1:18 ` Dexter Haslem [this message]
2010-12-23  2:15   ` copy_to_user Hemanth Kumar
2010-12-23  4:35   ` copy_to_user Srinivas G.
2010-12-23  5:18     ` copy_to_user Hemanth Kumar
2010-12-23  5:37     ` copy_to_user mukti jain
2010-12-23 12:21       ` copy_to_user Hemanth Kumar
2010-12-24  6:07       ` copy_to_user Hemanth Kumar
2010-12-24  7:59         ` copy_to_user Nilesh Tayade
2010-12-24  9:00           ` copy_to_user Dave Hylands
2010-12-24  9:20             ` copy_to_user Nilesh Tayade
2010-12-24 10:00               ` copy_to_user Hemanth Kumar
2010-12-24  9:26           ` copy_to_user Hemanth Kumar
     [not found] <589679.60204.qm@web94707.mail.in2.yahoo.com>
2010-12-24 18:31 ` copy_to_user Mulyadi Santosa
2010-12-26  1:33   ` copy_to_user Hemanth Kumar
  -- strict thread matches above, loose matches on Subject: below --
2001-11-25 21:58 copy to user Marco C. Mason
2001-11-20 20:54 Luis Miguel Correia Henriques
2001-11-20 21:28 ` John Alvord
2001-11-20 21:44 ` Andreas Dilger
2001-11-21 11:02   ` Luís Henriques
2001-11-20 22:02 ` n0ano
2001-11-20 22:05 ` Chris Wright
2001-11-21  8:43 ` Mathijs Mohlmann
2001-11-21 10:12   ` Jan Hudec

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=4D12A363.60404@gmail.com \
    --to=dexter.haslem@gmail.com \
    --cc=kernelnewbies@lists.kernelnewbies.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.