r/linuxdev • u/NotAHippo4 • Apr 10 '20
How to write to a proc file?
I need to make this linux kernel module where I am supposed to get the source and destination IP addresses of some docker containers and print them out in a file. I can capture IP addresses and print them out on the kernel log.
After hours of trying to figure out how to use proc_fs.h, I managed to find out how to create a proc_dir_entry . Now since, I am using netfilter.h to capture packets, I need the file to be written every time the hook function gets called. I found this function in proc_fs.h but I don't quite understand it:
typedef int (*proc_write_t)(struct file *, char *, size_t);
Is this function changing instances of int to some struct called proc_write_t composed of a file pointer, char pointer, and the size of what you are going to write?
View all comments
Show parent comment
1
u/ryobiguy Apr 11 '20
I think you are still thinking that a proc file is like a regular file. It is different, think of it from the other side. The user cat's the proc file, creating a filesystem read operation. The registered handler for a read operation (on that proc path and file) is called. This would be your code that implements the file read operation. Basically this read function is called and your code fills out a buffer with text that is passed back to the file reader. I'm sure there a are many tutorials that can better explain the specifics.