Reading Files From The Linux Kernel Space (Module/Driver) (Fedora 14)

Want to support HowtoForge? Become a subscriber!
Submitted by Pavel Abrosimov (Contact Author) (Forums) on Mon, 2011-01-03 16:52. :: Fedora | Kernel | C/C++

Reading Files From The Linux Kernel Space (Module/Driver) (Fedora 14)

It seems like operations that communicate with userspace are really discouraged. And I am not arguing with that. Although it doesn't mean that they cannot be accomplished. A driver loads configuration that could be stored only on HDD. When MS Windows XP boots up it remembers that I have turned Bluetooth off so it is not powered on, unlike even the latest Linux (Fedora 14 at the moment of writing) – such functionality in many drivers was not implemented. When developer uses procfs or character devices there should be another software or script in boot sequence that is actually sending information from a file to particular device.

After some time looking over the internet I have managed to write such module that reads a file. And here it goes:

#include <linux/module.h>  // Needed by all modules
#include <linux/kernel.h>  // Needed for KERN_INFO
#include <linux/fs.h>      // Needed by filp
#include <asm/uaccess.h>   // Needed by segment descriptors

int init_module(void)
    // Create variables
    struct file *f;
    char buf[128];
    mm_segment_t fs;
    int i;
    // Init the buffer with 0
        buf[i] = 0;
    // To see in /var/log/messages that the module is operating
    printk(KERN_INFO "My module is loaded\n");
    // I am using Fedora and for the test I have chosen following file
    // Obviously it is much smaller than the 128 bytes, but hell with it =)
    f = filp_open("/etc/fedora-release", O_RDONLY, 0);
    if(f == NULL)
        printk(KERN_ALERT "filp_open error!!.\n");
        // Get current segment descriptor
        fs = get_fs();
        // Set segment descriptor associated to kernel space
        // Read the file
        f->f_op->read(f, buf, 128, &f->f_pos);
        // Restore segment descriptor
        // See what we read from file
        printk(KERN_INFO "buf:%s\n",buf);
    return 0;

void cleanup_module(void)
    printk(KERN_INFO "My module is unloaded\n");

That is all that is needed to read the /etc/fedora-release file. Just in case, the following needs to be in Makefile if the source is saved as module.c:

obj-m += module.o


    make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules


    make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean

Issuing following:

$ make

$ su

# insmod module.ko

Then in the end of /var/log/messages I see following:

Dec 11 13:06:11 thinkpad kernel: [ 2410.431974] My module is loaded

Dec 11 13:06:11 thinkpad kernel: [ 2410.431993] buf:Fedora release 14 (Laughlin)

Dec 11 13:06:11 thinkpad kernel: [ 2410.431995]

The additional new line could be just from the file. But hey, that is exactly what is in that file!!!

It does looks like write functions are also there so if anyone feels like writing something from kernel space to a file – functionality is about there.

Please do not use the comment function to ask for help! If you need help, please use our forum.
Comments will be published after administrator approval.
Submitted by Marvin (not registered) on Sat, 2014-12-13 05:10.
thanks , it worked perfectly to me.
Submitted by veena hosur (not registered) on Mon, 2013-03-04 11:12.
hi...i want to read file in kernel mode.i am using f->f_op->read (above code)to read the file.but its reading till size of buffer but i want to read till end of file.please tell me to read till end of file
Submitted by Lawrence D'Oliveiro (not registered) on Sat, 2011-01-08 10:04.

The kernel should be small and support a basic core of general functionality, not be large and weighed down with features.

 A corollary of this is that the kernel should not implement policy, but only mechanisms to allow userland to implement policy.

An example of mechanism is userland being able to enable or disable the kernel Bluetooth driver. An example of policy is that the state at shutdown should be saved and restored on boot.