• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 217
  • Last Modified:

How to access data in an existing file form a kernel module

Hi, I'm a newbie in programming and especially in Linux. I'm trying to write a LKM and to get it to do what I want, I need information from files in /proc. To be more precise, I need info from the 'status' file in the /proc/pid where pid is the PID of the process I need the info for. In other words, the file I need info from is constantly different.

I checked the Linux Kernel Module Programming Guide already but that has examples of using a static file in /proc that you created in the module.

Does anybody know how this can best be done, and have some (simple) examples? Like I said, I'm fairly new to C and kernel programming so the simpler the example, the better :-)

If it is not possible, some examples on how to input/output data between a user and kernelspace program through /proc in Linux 2.4 are also very appreciated.

I'm using RedHat 7.3 with a 2.4.20 kernel by the way.
1 Solution
First of all, you're way off base with the proc stuff in the Linux Kernel Module Programming Guide.  That's about how to program the other side of the file -- so someone else can read it like you want to read /proc/PID.

Reading a /proc file is the same as reading any other file.  And how to read a file from inside the kernel is about the most frequently asked question about kernel programming.  I have an example of how to do that at ftp://giraffe-data.com/pub/filewrite.c.  Actually, that's for writing to a file, but reading is analogous.
No comment has been added lately, so it's time to clean up this TA.
I will leave the following recommendation for this question in the Cleanup topic area:

Accept: bryanh {http:#8031139}

Please leave any comments here within the next seven days.

EE Cleanup Volunteer

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now