Question

Dupe on stackoverflow by one of my friends having the same problem:

I copied the code from this tutorial: http://www.thegeekstuff.com/2012/04/create-proc-files/

And made the module which created my procfile.

I was happy to find that I gained a procfile I could echo "text" > proc/name_of_procfile

I can also read with cat!!

I've been fooling with it and now have a sizable number of values in my procfile - whenever I cat it goes through these values sequentially:

Q1.I wonder - how do I delete values from the procfile?

ALSO, later, I am going to try to get the behavior that whenever a file reads my procfile, it changes the value of whatever it just read in the proc_file.

Q2. I know this would be implemented in my "int read_info" method, but any pointers on how to do this?

Was it helpful?

Solution

Q1.

You can't delete these values in a normal sense of a delete operation on a file system. You may echo something into procfile if it is writable. Then it depends on a module's behavior how this 'incoming' data is treated. Will it be stored or discarded or in any way accounted somehow internally - the module decides. To be more precise, the logic that you put into module defines the treatment. The module from this example has very simple logic: it memorizes a string in its internal buffer when the kernel calls its write_info function - actually it does concatenate a new data with the current content. Then it can return the content of this buffer when kernel calls its read_info function. So if you do:

echo "some text">/proc/yourprocfile
echo "another text">/proc/yourprocfile

Then cat /proc/yourprocfile would print

some text
another text

on the screen. To summarize: the complete behavior of these files is module-defined.

Q2.

You could do this nearly like you do this in the usual C program. There are two major differences:

  • the API accessible in the kernel mode is different. Some functions have the same or very similar names and functionality as their analogues in standard C library has. The other functions have different names but still analogous to some user-space functions. And then, some specific functions that are available only in kernel space.

  • everything you do in kernel space should be several times checked by you and debugged on the sheet of paper as any bug in your module may cause kernel core dump at a minimum (kernel just stops and then you have to press reset button to reboot your system) and may cause data corruption of your file systems at worst.

So, I would recommend you to set up the VirtualBox package; to create with this beautiful tool a virtual machine; to install there the minimal linux version and to make a snapshot of it's virtual disk after installation. So if something goes wrong you always may revert any damage. You need not to install all your build tools and kernel source there - you will be only loading and testing/debugging your modules/applications on this virtual machine.

But anyway, the area that you are going to touch, or, to be more correct, already touched, is very interesting one.

Good luck

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top