In this post, I will cover basics of procfs, overview of standard procfs files and and steps to create custom proc entries.
procfs is a RAM based file system mounted at /proc. procfs is a communication mechanism between linux user space & kernel space. From user space, procfs files can be read to view information in kernel's internal data structures & written into to modify information in kernel's internal data structures. By using a RAM based file system, the well established file system interface is leveraged rather than create yet another specific interface.
Standard procfs files
- Process specific entries: Linux kernel uses procfs to export (and also to receive user inputs) info about it's processes. Each process has a file named /proc/<pid>. Here is the list of files/sub-directories in process with pid 1:
root@babu-VirtualBox:/proc/1# ls
attr cpuset limits net sched syscall
autogroup cwd loginuid ns schedstat task
auxv environ map_files oom_adj sessionid timers
cgroup exe maps oom_score smaps wchan
clear_refs fd mem oom_score_adj stack
cmdline fdinfo mountinfo pagemap stat
comm io mounts personality statm
coredump_filter latency mountstats root status
root@babu-VirtualBox:/proc/1#
I will not delve into the details of each file above. In some of my earlier posts, I displayed contents of some of the above files such as /proc/<pid>/smaps etc.
- Kernel Data: Apart from process specific directories, /proc contains various other files offering insight into kernel's data structures:
root@babu-VirtualBox:/proc# ls | grep -v '^[0-9]' | column -xacpi asound buddyinfo
bus cgroups cmdline
consoles cpuinfo crypto
devices diskstats dma
dri driver execdomains
fb filesystems fs
interrupts iomem ioports
irq kallsyms kcore
key-users kmsg kpagecount
kpageflags latency_stats loadavg
locks lttng mdstat
meminfo misc modules
mounts mtrr net
pagetypeinfo partitions sched_debug
schedstat scsi self
slabinfo softirqs stat
swaps sys sysrq-trigger
sysvipc timer_list timer_stats
tty uptime version
version_signature vmallocinfo vmstat
zoneinfo
root@babu-VirtualBox:/proc#
I will not delve into the details of each file above. In some of my earlier posts, I displayed contents of some of the above files such as /proc/meminfo, /proc/slabinfo, /proc/buddyinfo etc.
- Networking Info: /proc/net - TBD
- IDE devices info : /proc/ide - TBD
- SCSI devices info: /proc/scsi - TBD
- Parallel port info: /proc/parport - TBD
- tty info: /proc/tty - TBD
- File System Info: /proc/fs - TBD
- Console Info: /proc/consoles - TBD
Steps to create Custom procfs files
Kernel modules too can leverage procfs to display the information about their data structures as well as to get configuration info inputs from user.
procfs APIs have two versions - legacy version & latest version. The legacy version is deprecated in kernel versions >= 3.10. I will cover details of latest version in this post. The latest version is useful for kernel versions >= 3.13 ....
- How to create a new proc directory?
- How to create a new proc file?
- Reading APIs: Creating file alone is not sufficient. We need to write callback handlers that actually read/write into the module's data structures. Here is the code from the example code that displays it's data structures through procfs (reading from kernel module). This code uses seq_file interface. I will cover details of this interface in another blog post:
- Writing APIs: Here is some sample code that helps provide inputs to the kernel module through procfs (writing to kernel module). This code uses seq_file interface. I will cover details of this interface in another blog post.
- How to remove a proc file?
- How to remove a proc directory?
Example kernel module illustrating procfs file creation:
https://github.com/babuneelam/procfsv2_seq
procfs read/write outputs for the example kernel module:
root@babu-VirtualBox:~/examples/proc/procv2_seq# ls
err modules.order proc_test.c proc_test.mod.c proc_test.mod.o
Makefile Module.symvers proc_test.ko proc_test.mod.gcno proc_test.o
root@babu-VirtualBox:~/examples/proc/procv2_seq#
root@babu-VirtualBox:~/examples/proc/procv2_seq# ls /proc | grep test
root@babu-VirtualBox:~/examples/proc/procv2_seq# insmod proc_test.ko
root@babu-VirtualBox:~/examples/proc/procv2_seq# ls /proc | grep test
test
root@babu-VirtualBox:~/examples/proc/procv2_seq# cd /proc/test
root@babu-VirtualBox:/proc/test# ls
config
root@babu-VirtualBox:/proc/test# cat config
Config 1 Info:
----------------------------
0 0 0 0
Format:whether data sould be stored in a hash tableMax Hash table sizewhether to log statisticsMax number of statistics to be stored
Config 2 Info:----------------------------
20 21 22 23 24 25root@babu-VirtualBox:/proc/test#root@babu-VirtualBox:/proc/test# echo "1 0 1 0" > configroot@babu-VirtualBox:/proc/test# cat config
Config 1 Info:
----------------------------
1 0 1 0
Format:whether data sould be stored in a hash tableMax Hash table sizewhether to log statisticsMax number of statistics to be stored
Config 2 Info:
----------------------------
20 21 22 23 24 25root@babu-VirtualBox:/proc/test#
Hope this helps.
References:
https://www.kernel.org/doc/Documentation/filesystems/proc.txt
http://man7.org/linux/man-pages/man5/proc.5.html
No comments:
Post a Comment