Find the answer to your Linux question:
Results 1 to 5 of 5
How to read and write a file in a linux module? struct file *f=filp_open() f->f_op->read() f->f_op->write() ??...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Jul 2003
    Location
    china
    Posts
    8

    How to read and write a file in a module?


    How to read and write a file in a linux module?

    struct file *f=filp_open()

    f->f_op->read() f->f_op->write() ??

  2. #2
    Linux Guru
    Join Date
    Oct 2001
    Location
    Täby, Sweden
    Posts
    7,578
    Why are you so obsessed with file access? You shouldn't use that at all, really, unless it's really necessary. What is it that you're trying to do?

  3. #3
    Just Joined!
    Join Date
    Jul 2003
    Location
    china
    Posts
    8
    I'm a student and I will do nothing.
    I only want to know whether I could access a file in a module.

  4. #4
    Linux Guru
    Join Date
    Oct 2001
    Location
    Täby, Sweden
    Posts
    7,578
    I would recommend against it if possible. Generally, except in the filesystem code, you shouldn't access any files from the kernel. But if you really do want to, then yes, I'm pretty sure that it is possible.
    filp_open is probably the easiest way to go, but you can't use filp_close to close it again, since that requires that it's called from a process. I'm not exactly sure how to do it, but I believe calling fput on the file simply does the trick. You should make sure to call f->f_op->flush(f) first, if it has a f->f_op->flush.

  5. #5
    Just Joined!
    Join Date
    Nov 2003
    Posts
    7

    Re: How to read and write a file in a module?

    Quote Originally Posted by pinper
    How to read and write a file in a linux module?

    struct file *f=filp_open()

    f->f_op->read() f->f_op->write() ??
    A good point to start with is reading kernel/acct.c

    The main steps are:
    set_fs (for kernel not to get confused with kernel addresses instead of userspace __user addresses)

    filp_open (or any of namei family)
    f->f_op->write
    fput

    set_fs (to return current process' addrlimit).

    P.S. Never do that from bottom half

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •