This article, which is part of the series on Linux device drivers, demonstrates the creation and usage of files under the /proc virtual filesystem.
After many months, Shweta and Pugs got together for some peaceful technical romancing. All through, they had been using all kinds of kernel windows, especially through the /proc virtual filesystem (using cat), to help them decode various details of Linux device drivers. Here’s a nonexhaustive summary listing: /proc/modules—dynamically loaded modules /proc/devices—registered character and block major numbers /proc/iomem—onsystem physical RAM and bus device addresses /proc/ioports—onsystem I/O port addresses (especially for x86 systems) /proc/interrupts—registered interrupt request numbers /proc/softirqs—registered soft IRQS /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/swaps—currently active swaps /proc/cpuinfo—information about the CPU(S) on the system /proc/meminfo—information about the memory on the system, viz., RAM, swap, ... simple—just use the right set of APIS, and there you go.” “For you, everything is simple,” Shweta grumbled. “No yaar, this is seriously simple,” smiled Pugs. “Just watch me creating one for you,” he added.
And in a jiffy, Pugs created the proc_window.c file below (download it from http://www.linuxforu.com/article_source_ code/mar12/device_driver.zip): #include <linux/module.h> #include <linux/kernel.h> #include <linux/proc_fs.h> #include <linux/jiffies.h> static struct proc_dir_entry *parent, *file, *link; static int state = 0; int time_read(char *page, char **start, off_t off, int count, int *eof, void *data) { int len, val; unsigned long act_jiffies; len = sprintf(page, “state = %d\n”, state); act_jiffies = jiffies INITIAL_JIFFIES; val = jiffies_to_msecs(act_jiffies); switch (state) { case 0:
len += sprintf(page + len, “time = %ld jiffies\n”, act_jiffies);
break; case 1:
len += sprintf(page + len, “time = %d msecs\n”, val);