ARTICLE

Understanding /proc: Part 2
Contributed by Andy Kissner in Misc on 2006-04-27 13:46:14
Page 2 of 1 (which might have something to do with why the kernel developers decided to name it 'proc'). But /proc is also handy when it comes to learning about the devices on your system. While it is currently being deprecated in favor of the /sys filesystem for device information, /proc can still provide some valuable insight as to what you have attached to (or inside of) your computer.

What is a device?

Before we continue, I think a little clarification is in order. Just what do I mean by a device?

Some would say that a device is anything with a driver. While this would include the expected physical devices like a USB controller, hard drive, or a network card, there are some objects with drivers that aren't physical devices like the null device (/dev/null) or the loopback device ('lo'). Others would argue that a device is only a physical device that you can take, pick apart, or basically just a tangible object.

For the purposes of this article, I choose to go with the latter definition, and not include the so-called "virtual" devices. These will be covered in the fourth and final installment of this series, covering miscellaneous things that one can do with /proc, as well as a few practical 'real-world' scenarios.

Viewing driver information

One of the easiest things you can do with /proc is viewing information about the drivers currently in use by the kernel. But first, a little background information:

Everything that the system interacts with is represented in the kernel by a device driver. A driver is the mechanism used by the mouse to tell the system "Hey, look at me! I'm at coordinate 121, 548!) or allows the computer to let the printer know that it is time to print something. Drivers can be implemented in two different ways: as a built-in piece of static kernel code, or as a dynamic piece of code known as a module. A built-in driver is, as the name suggests, compiled in statically to the kernel, and is always ready to be called upon. A kernel module, however, is a separate piece of code which can be loaded and unloaded from the kernel on the fly. While you must load a module to be able to use it, the advantage is that it doesn't take up memory when you don't need it.

That being said, we are ready to probe /proc for information about the drivers loaded by the kernel (either statically at compile-time or dynamically by the user).

Every driver loaded has an entry in the file /proc/devices, which as the name suggests, has some valuable information about devices attached to the system. Each device (driver, to be technically correct) is represented in the kernel by two numbers: a major number and a minor number. Each driver has a distinct major number, and each instance of said driver has a minor number. They are generally presented in the form X:Y, where X is the major and Y is the minor number. The /proc/devices file lists the mapping between major number and actual device.

For example:

Character devices:

1 mem
2 pty
3 ttyp
4 /dev/vc/0
4 tty
4 ttyS
5 /dev/tty
5 /dev/console
5 /dev/ptmx
6 lp
7 vcs
10 misc
13 input
14 sound
21 sg
86 ch
116 alsa
128 ptm
136 pts
171 ieee1394
180 usb
189 usb_device
226 drm

Block devices:

3 ide0
8 sd
65 sd
66 sd
67 sd
68 sd
69 sd
70 sd
71 sd
128 sd
129 sd
130 sd
131 sd
132 sd
133 sd
134 sd
135 sd

That is the contents of my /proc/devices file. One thing of note is that even if there are several instances of the same driver (i.e. major number 4 in the example above), /proc/devices does not differentiate between minor numbers of the same major. If you want to get the minor number for a character or block device, you can use 'ls -l':

brw-rw---- 1 root disk 8, 5 Apr 19 09:47 /dev/sda5

In the above listing, the major number for the driver used for the device /dev/sda5 is 8, and the minor number is 5 (which makes sense, sense it is the fifth partition!).

For more information about the modules currently loaded, you can either run 'lsmod' or look at /proc/modules directly. It really doesn't matter which one you do, since lsmod gets its information from the modules file.

Bus Information

As you may or may not already know, a bus is a subsystem within a computer that allows for the transfer of data between the processor and other devices attached to the system. Two of the most common buses in use today are USB (Universal Serial Bus) and PCI (Peripheral Component Interconnect), and all modern computer systems will have devices that utilize at least one of those two bus standards.

The /proc filesystem contains some valuable information about devices using both. Most of the USB and PCI specific information is kept in /proc/bus/usb and /proc/bus/pci, respectively. Inside each of these directories will be two things: further subdirectories with names like 00, 01, and 03; and a file named 'devices'. The numbered subdirectories contain some raw information about each USB hub / PCI slot in particular, however most people will only use the information in /proc/bus/{pci,usb}/devices. However, you still won't normally access this data raw; almost all Linux systems ship with the 'lspci' tool, which is an invaluable diagnostic, telling you which PCI devices you have connected, where. Many systems these days also include the 'lsusb' tool, or make it available separately, which does a similar task for USB devices.

Network Information

/proc also provides tools to view statistics and settings for your networking hardware. Much like the information available inside of the /proc/bus/ directories, most network information is obtained via a separate wrapper tool, such as 'ifconfig'.

The raw file used by ifconfig is /proc/net/dev, but there is no pretty formatting or spacing like with ifconfig's output. There is a lot more information available in /proc/net/, however they don't pertain to devices.

Networking will be covered in greater detail in the next installment of Understanding /proc, appropriately entitled 'Networking with /proc'.


 
Discussion(s)
Great Tutorial
Written by Bobby Lee on 2006-04-28 12:33:49
Great article on the proc file system. Within this short tutorial, you have really enlighten me and catapulted me to the next level of understanding Linux. Prior to this, I had been rotely performing tasks and never really knowing what was gooing on.

I look forward to the next tutorial.
Discuss! Reply!

thanks
Written by Andy Kissner on 2006-05-05 16:42:55
Thanks!
Did you see the first one?
Discuss! Reply!

Bulgarian translation
Written by saejoh on 2006-05-23 13:59:51
Hello Mr Kissner,
I found this article very useful for me and therefore useful for the Linux-community in Bulgaria. So I desided to translate it into Bulgarian. Everyone interested can reach it here.
There is also a translation of the first part of this great article.
Looking forward to the next parts.
Discuss! Reply!

Excellent
Written by Chandra Sekhar . R on 2006-08-02 20:15:26
Very much useful !!!!!!!!
Thanx
Discuss! Reply!