views:

3034

answers:

5

I'm writing a Linux kernel module that needs to open and read files. What's the best way to accomplish that?

+3  A: 

assuming you can get pointers to the relavent function pointers to the open/read/close system calls, you can do something like this:

mm_segment_t fs = get_fs();
set_fs(KERNEL_DS);

fd = (*syscall_open)(file, flags, mode);
if(fd != -1) {
    (*syscall_read)(fd, buf, size);
    (*syscall_close)(fd);
}
set_fs(fs);

you will need to create the "syscall_*" function pointers I have shown though. I am sure there is a better way, but I believe that this would work.

Evan Teran
A: 

You can also find some informations about sys_call_open in this Linux Kernel Module Programing Guide.

VonC
Yeah, I've taken a look at that. Incidentally, I _am_ patching the open(2) system call, and I actually need to read a file _inside_ my patched version.
mipadi
+10  A: 

Can I ask why are you trying to open a file?

I like to follow Linux development (out of curiosity, I'm not a kernel developer, I do Java), and I've seen discussion of this question before. I was able to find a LKML message about this, basically mentioning it's usually a bad idea. I'm almost positive that LWN covered it in the last year, but I'm having trouble finding the article.

If this is a private module (like for some custom hardware and the module won't be distributed) then you can do this, but I'm under the impression that if you are going to submit your code to the mainline then it may not be accepted.

Evan Teran mentioned sysfs, which seems like a good idea to me. If you really need to do harder custom stuff you could always make new ioctrls.

EDIT:

OK, I found the article I was looking for, it's from Linux Journal. It explains why doing this kind of stuff is generally a bad idea, then goes on to tell you exactly how to do it anyway.

MBCook
It's a custom module, it won't be distributed. I need to open a file that contains a mapping of inodes => checksums.
mipadi
@mipadi: I found the article I was thinking of, it explains exactly how to open and read a file from the kernel (after the "don't do this" part). I've edited my post to reflect that and contain the link. Good luck.
MBCook
A: 

Generally speaking, if you need to read/write files from a kernel module, you're doing something wrong architecturally.

There exist mechanisms (netlink for example - or just register a character device) to allow a kernel module to talk to a userspace helper process. That userspace helper process can do whatever it wants.

You could also implement a system call (or such like) to take a file descriptor opened in userspace and read/write it from the kernel.

This would probably be neater than trying to open files in kernel space.

There are some other things which already open files from kernel space, you could look at them (the loop driver springs to mind?).

MarkR
I've actually altered the mechanism to read from a file in /proc, so I probably won't need to read a file directly.
mipadi
+1  A: 

/proc filesystem is also good for private use, and it's easy.
http://www.linuxtopia.org/online_books/Linux_Kernel_Module_Programming_Guide/x773.html

Denes Tarjan