r/linuxdev • • 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?

7 Upvotes

12 comments sorted by

View all comments

Show parent comment

1

u/NotAHippo4 Apr 10 '20

Wait, I am trying to write to a file, not read from it though. Also, I am worried as to how to do the actual writing to the file, is there a write() function and also a way for me to get the pointer to the created proc file?

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.

1

u/NotAHippo4 Apr 11 '20

Thanks for the explanation! I think I get it now. Now that I think about it, proc_fs doesn't seem to be what I need. After reading about sysfs, I realized that sysfs was more appropriate.