Saturday, April 13, 2013

Device Node

Device nodes are special files to allow interaction with device driver.  Kernel will hand over the I/O calls (e.g. read) to the driver instead of to file.  The driver handles the request and returns result to the caller.  This abstraction allows user to use familiar I/O call to interact with drivers.

Each device node is assigned a major number and a minor number.  The major and minor numbers identify the device driver loaded in memory.  If the numbers cannot be matched, system returns ENODEV as the device cannot be found.

Special device nodes are:
/dev/null (1,3) - read returns EOF, writes are discarded
/dev/zero (1,5) - read returns \0, writes are discarded
/dev/full (1,7) - read returns \0, write returns ENOSPC indicating the device is full
/dev/random (1,8) - random number generator.  An entropy pool is generated by hashing noise collected from driver and other sources.  Read returns from entropy pool.  The result is suitable for seeding process like keygen as it is cryptographicall strong.  Kernel monitors the amount of entropy in the pool.  If it reaches zero, read will be blocked.  This scenario could happen in diskless station which have little or no I/O activities.
/dev/urandom (1,9) - a lower grade version of /dev/random.  Read will be successful even if the entropy pool is depleted.

Normal I/O call cannot represent all functions of device e.g. set baud rate.  ioctl (I/O control) is used for such out of band communication with the device.

int ioctl (int fd, int request, ...)

The request is a code known to kernel representing the command to the driver.

No comments: