Re: character device driver reading only last character of buffer

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 




On Thu, Sep 23, 2010 at 11:47 PM, Carlo Caione <carlo.caione@xxxxxxxxx> wrote:



Have you ever used malloc() in user space?
Man, I don't want to be rude, but what you need is to code in user space
before putting your hands in kernel space.

Since you mentioned so I am posting a program this uses malloc.You can see it,I have myself checked and it is working.
You do show  the attitude but it would be good if you show some attitude on the code I posted.


#include <stdio.h>
#include <stdlib.h>
struct node {
    struct node *next, *prev;
    int data;
} *start, *prv;
void add_element(int e);
void read_list ();
static int j = 0;
int main()
{
    int i, element, choice;
    choice = 1;
    struct node *temp;
    start = NULL;
    while (choice == 1) {
        printf("What do you want 0 exit 1 to add\n");
        scanf("%d", &choice);
        if (choice == 0)
            break;
        printf("Enter data to be entered\n");
        scanf("%d", &element);
        add_element(element);
    }

               read_list();
}

void add_element(int e)
{
//      printf("\n printing from function %d\n", e);
    struct node *temp;
    if (j == 0) {
        start = (struct node *)malloc(sizeof(struct node));
       
        start->data = "">        //printf("the data entered is %d \n", start->data);
        prv=start;//is necessary since in otherwise case prv is pointing to some garbage value
       
    }

          if(j!=0){
        temp = (struct node *)malloc(sizeof(struct node));
        temp->data = "">        prv->next = temp;
              temp->next=NULL;
        prv=temp;//since prv is  global pointer if I do not do this step then next time when new memory is allocated prv is still pointing to start or previous pointer what ever the value was so new temp declaration will not have much effect
         }
   
    j++;

   
}

void read_list()
{
    struct node *temp;
    temp = start;
    while (temp) {
        printf(" %d --> ", temp->data);
        temp = temp->next;
    }
}



On Thu, Sep 23, 2010 at 11:58 PM, Manish Katiyar <mkatiyar@xxxxxxxxx> wrote:
Did you really want to allocate 1 byte ?

>   if (!memory_buffer) {
>     result = -ENOMEM;
>     goto fail;
>   }
>   memset(memory_buffer, 0, 10);

>And then write 10 bytes on it without expecting to cause corruptions ?
I had made that change that you said but it had not worked.

On Fri, Sep 24, 2010 at 12:19 AM, Greg Freemyer <greg.freemyer@xxxxxxxxx> wrote:
>I'm putting you in my autodelete list, so I won't be responding to you again.
You are free to put where ever you want but it would be good if you show your attitude on code not on me.
This is a newbie list and no one can stop us from asking if we are in problem there are much better people here than you.


On Fri, Sep 24, 2010 at 12:34 AM, Bond <jamesbond.2k.g@xxxxxxxxx> wrote:
I changed that part
/* Necessary includes for device drivers */
#include <linux/init.h>
//#include <linux/config.h>
#include <linux/module.h>
#include <linux/kernel.h> /* printk() */
#include <linux/slab.h> /* kmalloc() */
#include <linux/fs.h> /* everything... */
#include <linux/errno.h> /* error codes */
#include <linux/types.h> /* size_t */
#include <linux/proc_fs.h>
#include <linux/fcntl.h> /* O_ACCMODE */
#include <asm/system.h> /* cli(), *_flags */
#include <asm/uaccess.h> /* copy_from/to_user */

MODULE_LICENSE("Dual BSD/GPL");

/* Declaration of memory.c functions */
int memory_open(struct inode *inode, struct file *filp);
int memory_release(struct inode *inode, struct file *filp);
ssize_t memory_read(struct file *filp, char *buf, size_t count, loff_t *f_pos);
ssize_t memory_write(struct file *filp, char *buf, size_t count, loff_t *f_pos);
void memory_exit(void);
int memory_init(void);

/* Structure that declares the usual file */
/* access functions */
struct file_operations memory_fops = {
  read: memory_read,
  write: memory_write,
  open: memory_open,
  release: memory_release
};
/* Declaration of the init and exit functions */
module_init(memory_init);
module_exit(memory_exit);

/* Global variables of the driver */
/* Major number */
int memory_major = 60;
/* Buffer to store data */
char *memory_buffer;

int memory_init(void) {
  int result;

  /* Registering device */
  result = register_chrdev(memory_major, "bond", &memory_fops);
  if (result < 0) {
    printk(KERN_ALERT  "memory: cannot obtain major number %d\n", memory_major);
    return result;
  }

  /* Allocating memory for the buffer */
  memory_buffer = kmalloc(10, GFP_KERNEL);
  if (!memory_buffer) {
    result = -ENOMEM;
    goto fail;
  }
  memset(memory_buffer, 0, 10);

  printk(KERN_ALERT "Inserting bond module\n");
  return 0;

  fail:
    memory_exit();
    return result;
}


void memory_exit(void) {
  /* Freeing the major number */
  unregister_chrdev(memory_major, "bond");

  /* Freeing buffer memory */
  if (memory_buffer) {
    kfree(memory_buffer);
  }

  printk( KERN_ALERT "Removing bond module\n");

}


int memory_open(struct inode *inode, struct file *filp) {

  /* Success */
  return 0;
}


int memory_release(struct inode *inode, struct file *filp) {
 
  /* Success */
  return 0;
}


ssize_t memory_read(struct file *filp, char *buf,
                    size_t count, loff_t *f_pos) {
 
  /* Transfering data to user space */
  copy_to_user(buf,memory_buffer,count<10 ? count:10);


  /* Changing reading position as best suits */
/*  if (*f_pos == 0) {
    *f_pos+=1;
    return 1;
  } else {
    return 0;
  }*/

}

ssize_t memory_write( struct file *filp, char *buf,
                      size_t count, loff_t *f_pos) {

  char *tmp;


  copy_from_user(memory_buffer,tmp,count<10 ? count : 10);
  return 1;
}


The above also did not worked when I had posted this in the first post I have changed kmalloc and copy_from_user, copy_to_user
as some of you above have suggested to have a buffer size type of thing but it also did not worked.


[Index of Archives]     [Newbies FAQ]     [Linux Kernel Mentors]     [Linux Kernel Development]     [IETF Annouce]     [Git]     [Networking]     [Security]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux SCSI]     [Linux ACPI]
  Powered by Linux