10.4. The /proc Filesystem
Unix systems have come a long way with respect to providing uniform interfaces to different parts of the system; as you learned in Chapter 4, hardware is represented in Linux in the form of a special type of file in the /dev directory. We'll have a lot more to say about this directory in "Device Files," later in this chapter. There is, however, a special filesystem called the /proc filesystem that goes even one step further: it unifies files and processes.
From the user's or the system administrator's point of view, the /proc filesystem looks just like any other filesystem; you can navigate around it with the cd command, list directory contents with the ls command, and view file contents with the cat command. However, none of these files and directories occupies any space on your hard disk. The kernel traps accesses to the /proc filesystem and generates directory and file contents on the fly. In other words, whenever you list a directory or view file contents in the /proc filesystem, the kernel dynamically generates the contents you want to see.
To make this less abstract, let's see some examples. The following example displays the list of files in the top-level directory of the /proc filesystem:
tigger # ls /proc . 3759 5538 5679 5750 6137 9 filesystems net .. 3798 5539 5681 5751 6186 966 fs partitions 1 3858 5540 5683 5754 6497 acpi ide scsi 10 3868 5541 5686 5757 6498 asound interrupts self 11 3892 5542 5688 5759 6511 bluetooth iomem slabinfo 1138 3898 5556 5689 5761 6582 buddyinfo ioports splash 14 4 5572 5692 5800 6720 bus irq stat 15 4356 5574 5693 5803 6740 cmdline kallsyms swaps 1584 4357 5579 5698 5826 6741 config.gz kcore sys 1585 4368 5580 5701 5827 6817 cpufreq kmsg sysrq-trigger 1586 4715 5592 5705 5829 6818 cpuinfo loadavg sysvipc 16 4905 5593 5706 5941 6819 crypto locks tty 17 5 5619 5707 6 6886 devices mdstat uptime 18 5103 5658 5713 6063 689 diskstats meminfo version 19 5193 5661 5715 6086 6892 dma misc vmstat 2 5219 5663 5717 6107 6894 dri mm 2466 5222 5666 5740 6115 6912 driver modules 2958 5228 5673 5741 6118 7 execdomains mounts 3 5537 5677 5748 6130 8 fb mtrr
The numbers will be different on your system, but the general organization will be the same. All those numbers are directories that represent each of the processes running on your system. For example, let's look at the information about the process with the ID 3759:
tigger # ls /proc/3759 . auxv delay fd mem oom_score statm wchan .. cmdline environ mapped_base mounts root status attr cwd exe maps oom_adj stat task
(The output can be slightly different if you are using a different version of the Linux kernel.) You see a number of files that each contain information about this process. For example, the cmdline file shows the command line with which this process was started. status gives information about the internal state of the process, and cwd links to the current working directory of this process.
Probably you'll find the hardware information even more interesting than the process information. All the information that the kernel has gathered about your hardware is collected in the /proc filesystem, even though it can be difficult to find the information you are looking for.
Let's start by checking your machine's memory. This is represented by the file /proc/meminfo:
owl # cat /proc/meminfo MemTotal: 1034304 kB MemFree: 382396 kB Buffers: 51352 kB Cached: 312648 kB SwapCached: 0 kB Active: 448816 kB Inactive: 141100 kB HighTotal: 131008 kB HighFree: 252 kB LowTotal: 903296 kB LowFree: 382144 kB SwapTotal: 1172724 kB SwapFree: 1172724 kB Dirty: 164 kB Writeback: 0 kB Mapped: 294868 kB Slab: 38788 kB Committed_AS: 339916 kB PageTables: 2124 kB VmallocTotal: 114680 kB VmallocUsed: 78848 kB VmallocChunk: 35392 kB HugePages_Total: 0 HugePages_Free: 0 Hugepagesize: 4096 kB
If you then try the command free , you can see that you get exactly the same information, only in a different format. free does nothing more than read /proc/meminfo and rearrange the output a bit.
Most tools on your system that report information about your hardware do it this way. The /proc filesystem is a portable and easy way to get at this information. The information is especially useful if you want to add new hardware to your system. For example, most hardware boards need a few I/O addresses to communicate with the CPU and the operating system. If you configured two boards to use the same I/O addresses, disaster is about to happen. You can avoid this by checking which I/O addresses the kernel has already detected as being in use:
tigger # more /proc/ioports 0000-001f : dma1 0020-0021 : pic1 0040-005f : timer 0060-006f : keyboard 0070-0077 : rtc 0080-008f : dma page reg 00a0-00a1 : pic2 00c0-00df : dma2 00f0-00ff : fpu 0170-0177 : ide1 01f0-01f7 : ide0 02f8-02ff : serial 0376-0376 : ide1 0378-037a : parport0 03c0-03df : vesafb 03f6-03f6 : ide0 03f8-03ff : serial 0cf8-0cff : PCI conf1 c000-cfff : PCI Bus #02 c000-c0ff : 0000:02:04.0 c000-c00f : advansys c400-c43f : 0000:02:09.0 c400-c43f : e100 d000-d00f : 0000:00:07.1 d000-d007 : ide0 d008-d00f : ide1 d400-d4ff : 0000:00:07.5 d400-d4ff : AMD AMD768 - AC'97 d800-d83f : 0000:00:07.5 d800-d83f : AMD AMD768 - Controller dc00-dcff : 0000:00:09.0 e000-e003 : 0000:00:00.0
Now you can look for I/O addresses that are free. Of course, the kernel can show I/O addresses only for boards that it has detected and recognized, but in a correctly configured system, this should be the case for all boards.
You can use the /proc filesystem for the other information you might need when configuring new hardware as well: /proc/interrupts lists the occupied interrupt lines (IRQs) and /proc/dma lists the DMA channels in use.