Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

File I/O in a Linux kernel module

Tags:

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

like image 505
mipadi Avatar asked Nov 08 '08 23:11

mipadi


People also ask

How do I write a kernel module to a file?

Instead, functions exclusively for kernel's file access are provided: # Read the file from the kernel space. ssize_t kernel_read(struct file *file, void *buf, size_t count, loff_t *pos); # Write the file from the kernel space.

How do I open a kernel file?

If you cannot open your KERNEL file correctly, try to right-click or long-press the file. Then click "Open with" and choose an application. You can also display a KERNEL file directly in the browser: Just drag the file onto this browser window and drop it.

What is kernel file system in Linux?

The Linux kernel implements the concept of Virtual File System (VFS, originally Virtual Filesystem Switch), so that it is (to a large degree) possible to separate actual "low-level" filesystem code from the rest of the kernel. The API of a filesystem is described below.


1 Answers

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.

like image 125
MBCook Avatar answered Sep 19 '22 06:09

MBCook