Search code examples
androidlinux-kernelkernelkernel-moduleprocfile

Access data from proc file within kernel module


I need to access some proc files in a module on Android kernel. Basically i need the info shown in cat command, like cat /proc/uptime. However i need to do it programmatically.

I tried work with proc_fs functions, but it was just a little fuzzy for me, usually the examples are to create a proc file then read it and that is it. I need to actually use the data from proc files.

I also tred the good fopen, but it does not seems to work on modules.

How can i do that? i'm really newbie on this. I'm working on goldfish Android kernel.

Thanks.


Solution

  • Procfs is an in-memory file system. It is an interface for the userspace to fetch info and put (config) info into the kernel data structures. In other words, procfs enables userspace to interact and look into the kernel data structures the way they exist during runtime.

    Hence, any file inside /proc is not meant to be read from inside a kernel or a kernel module. And why would one want to do that? In a monolithic kernel like Linux, you can access the data structures of one subsystem in the kernel through another directly or through a pre-defined function.

    The following function call might help:

    struct timespec uptime;
    
    do_posix_clock_monotonic_gettime(&uptime);
    

    You can refer to the /proc/uptime implementation at the link below, it is essentially a seq_file.

    http://lxr.free-electrons.com/source/fs/proc/uptime.c