Friday, 20 July 2018

Procfs

On the root, there is a folder titled “proc”. This folder is not really on /dev/sda1 or where ever you think the folder resides. This folder is a mount point for the procfs (Process Filesystem) which is a filesystem in memory.


It can act as a bridge connecting the user space and the kernel space. User space program can use proc files to read the information exported by kernel. Every entry in the proc file system provides some information from the kernel.

The entry “meminfo”  gives the details of the memory being used in the system.
To read the data in this entry just run
cat /proc/meminfo
Similarly the “modules” entry gives details of all the modules that are currently a part of the kernel.
cat /proc/modules
It gives similar information as lsmod. Like this more proc entries are there.
  • /proc/devices — registered character and block major numbers
  • /proc/iomem — on-system physical RAM and bus device addresses
  • /proc/ioports — on-system I/O port addresses (especially for x86 systems)
  • /proc/interrupts — registered interrupt request numbers
  • /proc/softirqs — registered soft IRQs
  • /proc/swaps — currently active swaps
  • /proc/kallsyms — running kernel symbols, including from loaded modules
  • /proc/partitions — currently connected block devices and their partitions
  • /proc/filesystems — currently active filesystem drivers
  • /proc/cpuinfo — information about the CPU(s) on the system
Most proc files are read-only and only expose kernel information to user space programs.
proc files can also be used to control and modify kernel behavior on the fly. The proc files needs to be writtable in this case.
For example, to enable IP forwarding of iptable, one can use the command below,
echo 1 > /proc/sys/net/ipv4/ip_forward

Creating Procfs Entry


The creation of proc entries has undergone a considerable change in kernel version 3.10 and above. In this post we will see one of the methods we can use in linux kernel version 3.10 and above let us see how we can create proc entries in version 3.10 and above.


The function is defined in proc_fs.h.
Where,
<name> : The name of the proc entry
<mode> : The access mode for proc entry
<parent> : The name of the parent directory under /proc. If NULL is passed as parent, the /proc directory will be set as parent.
<proc_fops> : The structure in which the file operations for the proc entry will be created.


MakeFile



Building and Testing Driver

  • Build the driver by using Makefile (sudo make)
  • Load the driver using sudo insmod driver.ko
  • Check our procfs entry using ls in procfs directory
linux@embetronicx-VirtualBox:ls /proc/
    filesystems      iomem      kallsyms      modules    partitions
  • Now our procfs entry is there under /proc directory.
  • Now you can read procfs variable using cat.
linux@embetronicx-VirtualBox:  cat /proc/etx_proc
try_proc_array
  • We initialized the etx_array with “try_proc_array”. That’s why we got “try_proc_array”.
  • Now do proc write using echo command and check using cat.
linux@embetronicx-VirtualBox: echo "device driver proc" > /proc/etx_proc
linux@embetronicx-VirtualBox:  cat /proc/etx_proc
device driver proc


No comments:

Post a Comment