difference between procfile_read() & procfile_write() - Linux

This is a discussion on difference between procfile_read() & procfile_write() - Linux ; Hi ,gurus I have a small question about the /proc filesystem.Hopefully you can help me with it .Thanks in advance for your kindly help. In one of the book Linux Kernel Module Programming Guide's example code, when describing read&write a ...

+ Reply to Thread
Results 1 to 4 of 4

Thread: difference between procfile_read() & procfile_write()

  1. difference between procfile_read() & procfile_write()

    Hi ,gurus
    I have a small question about the /proc filesystem.Hopefully you can
    help me
    with it .Thanks in advance for your kindly help.

    In one of the book Linux Kernel Module Programming Guide's example
    code,
    when describing read&write a /proc file, the author
    mentioned "As the buffer (in read or write function) is in kernel
    space, for write function you need to import data because it comes
    from user space, but not for the read function because data is already
    in kernel space"

    in a sample programm which implement a simple /proc file which can be
    read and write.
    the procfile_read() use memcpy(buffer, procfs_buffer,
    procfs_buffer_size);on the other hand the procfile_write() use
    copy_from_user(procfs_buffer, buffer, procfs_buffer_size) to do the
    actual data transferring job.

    In my opinion I think the "buffer" are in the user_space and
    "procfs_buffer",which defined in the module is in the kernel_space.
    when we try to read from the proc file we are actually getting data
    from procfs_buffer,which is in the kernel_space.why don't it use
    copy_to_user(buffer,procfs_buffer,procfs_buffer_si ze) to put the data
    from kernel_space to user_space.

    Below is the sample programm for reference
    thanks
    scott

    /**
    * procfs2.c - create a "file" in /proc
    *
    */

    #include /* Specifically, a module */
    #include /* We're doing kernel work */
    #include /* Necessary because we use the proc fs */
    #include /* for copy_from_user */

    #define PROCFS_MAX_SIZE 1024
    #define PROCFS_NAME "buffer1k"

    /**
    * This structure hold information about the /proc file
    *
    */
    static struct proc_dir_entry *Our_Proc_File;

    /**
    * The buffer used to store character for this module
    *
    */
    static char procfs_buffer[PROCFS_MAX_SIZE];

    /**
    * The size of the buffer
    *
    */
    static unsigned long procfs_buffer_size = 0;

    /**
    * This function is called then the /proc file is read
    *
    */
    int
    procfile_read(char *buffer,
    char **buffer_location,
    off_t offset, int buffer_length, int *eof, void *data)
    {
    int ret;

    printk(KERN_INFO "procfile_read (/proc/%s) called\n", PROCFS_NAME);

    if (offset > 0) {
    /* we have finished to read, return 0 */
    ret = 0;
    } else {
    /* fill the buffer, return the buffer size */
    memcpy(buffer, procfs_buffer, procfs_buffer_size);
    ret = procfs_buffer_size;
    }

    return ret;
    }

    /**
    * This function is called with the /proc file is written
    *
    */
    int procfile_write(struct file *file, const char *buffer, unsigned
    long count,
    void *data)
    {
    /* get buffer size */
    procfs_buffer_size = count;
    if (procfs_buffer_size > PROCFS_MAX_SIZE ) {
    procfs_buffer_size = PROCFS_MAX_SIZE;
    }

    /* write data to the buffer */
    if ( copy_from_user(procfs_buffer, buffer, procfs_buffer_size) ) {
    return -EFAULT;
    }

    return procfs_buffer_size;
    }

    /**
    *This function is called when the module is loaded
    *
    */
    int init_module()
    {
    /* create the /proc file */
    Our_Proc_File = create_proc_entry(PROCFS_NAME, 0644, NULL);

    if (Our_Proc_File == NULL) {
    remove_proc_entry(PROCFS_NAME, &proc_root);
    printk(KERN_ALERT "Error: Could not initialize /proc/%s\n",
    PROCFS_NAME);
    return -ENOMEM;
    }

    Our_Proc_File->read_proc = procfile_read;
    Our_Proc_File->write_proc = procfile_write;
    Our_Proc_File->owner = THIS_MODULE;
    Our_Proc_File->mode = S_IFREG | S_IRUGO;
    Our_Proc_File->uid = 0;
    Our_Proc_File->gid = 0;
    Our_Proc_File->size = 37;

    printk(KERN_INFO "/proc/%s created\n", PROCFS_NAME);
    return 0; /* everything is ok */
    }

    /**
    *This function is called when the module is unloaded
    *
    */
    void cleanup_module()
    {
    remove_proc_entry(PROCFS_NAME, &proc_root);
    printk(KERN_INFO "/proc/%s removed\n", PROCFS_NAME);
    }

  2. Re: difference between procfile_read() & procfile_write()

    On Nov 16, 6:15 am, scottshi wrote:
    > Hi ,gurus



    There are no gurus here, it isn't even an advocacy group, what it is -
    is the Erik Funkenbusch fan club, where ther 'advocates' ignore
    legitimate topics by genuine posters and have loads of time in
    humoring the worst scum on Usenet.

    > I have a small question about the /proc filesystem ..


    dunno, try linux.kernel ..

  3. Re: difference between procfile_read() & procfile_write()

    After takin' a swig o' grog, scottshi belched out this bit o' wisdom:

    > Hi ,gurus
    > I have a small question about the /proc filesystem.Hopefully you can
    > help me
    > with it .Thanks in advance for your kindly help.


    Sounds like the Nigerian scam applied to homework!


  4. Re: difference between procfile_read() & procfile_write()

    After takin' a swig o' grog, Doug Mentohl belched out this bit o' wisdom:

    > On Nov 16, 6:15 am, scottshi wrote:
    >> Hi ,gurus

    >
    >
    > There are no gurus here...


    Sure there are.

    I just don't happen to know a lot about the quirks of proc files.



    > is the Erik Funkenbusch fan club, where ther 'advocates' ignore
    > legitimate topics by genuine posters and have loads of time in
    > humoring the worst scum on Usenet.


    Why don't you just light up a menthol and relax?

    The nicotine will calm you in nothing flat.

    >> I have a small question about the /proc filesystem ..

    >
    > dunno, try linux.kernel ..


    He should try alt.do-my-homework.linux.

    --
    Tux rox!

+ Reply to Thread