-->

Previous | Table of Contents | Next

Page 225

The Controlling Terminal Device—/dev/tty

Most processes have a controlling terminal, particularly if they were started interactively by a user. The controlling terminal, which I'll refer to as simply /dev/tty, is used for initiating a conversation directly with the user (for example, to ask them something). An example is the crypt command:


$ fmt diary.txt | crypt | mail -s Diary confidant@linux.org

Enter key:

$

Here, the crypt command has opened /dev/tty in order to obtain a password. It was not able to use its own standard output to issue the prompt and its standard input to read the password because these are being used for the data to be encrypted.

NOTE
Of course, it's unusual to send e-mail encrypted with crypt. A better choice is probably PGP. PGP is available in RPM format from ftp://ftp.replay.com//pub/linux/redhat.

More useful examples of this are commands that need to ask the operator something even if their input and output are redirected. A case in point is the cpio command, which prompts the operator for the name of a new tape device when it runs out of space. See the section "/dev/null and Friends" later in this chapter for another example.

Nonserial Mice

Many computers have bus or PS/2 mice instead of serial mice. This has the advantage of keeping both of the two standard serial ports free, but the disadvantage of using up another IRQ. These devices are used by gpm and the X Window System, but most other programs don't interact with them directly. Setting up your system with these mice is quite easy; the Red Hat Linux installation process pretty much takes care of it for you. Should you have problems with your mouse, though, you should read the manual page for gpm and the Linux BusMouse-HOWTO.

Audio Devices

There are several audio-related device nodes on Linux systems, and they include the following:

/dev/sndstat Indicates the status of the sound driver
/dev/audio* Sun-compatible audio output device
/dev/dsp* Sound sampling device
/dev/mixer For control of the mixer hardware on the sound card
/dev/music A high-level sequencer interface

Page 226

/dev/sequencer* A low-level sequencer interface
/dev/midi* Direct MIDI port access

Setting up the sound driver under Linux can sometimes be quite difficult, but the Linux Sound-HOWTO provides useful advice.

Random Number Devices

Many aspects of computing require the generation of apparently random sequences. Examples include games, numerical computations, and various computer security related applications. Numerical computing with random numbers requires that the sequence of random numbers be repeatable but also that the sequence "looks" random. Games require apparently random numbers too, but the quality of the random numbers used is not quite as critical as for numerical computation programs. The system libraries produce repeatable sequences of "pseudo- random" numbers that satisfy these requirements well.

On the other hand, there are many aspects of computer security in which it is advantageous to generate numbers that really are random. Because you can assume that an attacker has access to the same sorts of random number generators that you do, using these is usually not very safe—an attacker can use these generators to figure out what random number you'll come out with next. Sequences that are genuinely random must in the end be produced from the real world, and not from the internals of some computer program. For this reason, the Linux kernel keeps a supply of random numbers internally. These numbers are derived from very precise timings of the intervals between "random" external events—for example, the user's keypresses on the keyboard, mouse events, and even some interrupts (such as from the floppy disk drive and some network cards). These "real" random numbers are used in security-critical contexts—for example, the choosing of TCP sequence numbers.

NOTE
The Linux kernel uses these methods to produce TCP sequence numbers that are more difficult to guess than those of any other implementation at the time of writing. This improves the security of TCP connections against "hijacking."

The two random number devices differ in what happens when the rate of reading exceeds the rate at which random data is collected inside the kernel. The /dev/random device makes the calling program wait until some more randomness arrives, and the /dev/urandom device falls back on the difficult-to-guess MD5 hash to produce a stream of random data. When more random information arrives later, this is added to the randomness of /dev/urandom. To summarize, /dev/random doesn't sacrifice quality in favor of speed, but /dev/urandom does.

Page 227

/dev/null and Friends

In the following, the special devices /dev/full and /dev/null first simulate a tape-full condition and then discard the output:


$ echo diary.txt | cpio -o >/dev/full

Found end of tape.  To continue, type device/file name when ready.

/dev/null

52 blocks

In the real world, when the tape on /dev/st0 became full, you would probably just have changed the tape in the drive and typed /dev/st0 a second time. However, /dev/full is occasionally useful for testing purposes, and /dev/null is used all the time for discarding unwanted output. The device /dev/zero produces a stream of zero bytes when read. (/dev/null, on the other hand, produces no output at all.)

Memory Devices

The memory devices have the same major device number as /dev/null and /dev/full, but are used very differently. They are as follows:

/dev/mem Provides access to physical memory
/dev/kmem Provides access to the kernel's virtual memory
/dev/port Provides access to I/O ports

These devices are not frequently used in many programs; the X Window System's X server uses memory mapping on /dev/mem to access the video memory, and many programs use /dev/port to access I/O ports on those architectures that have a separate I/O space. (Many modern processors do not.)

Virtual Console Screen Devices

The virtual console screen devices exist to provide screen capture capabilities for virtual consoles (VCs). They are not readable by ordinary users; hence, other users cannot eavesdrop on your session.

There are two sets of device nodes for this purpose:


$ ls -l /dev/vcs[012] /dev/vcsa[012]

crw--w----   1 root     tty        7,   0 Sep 27  1995 /dev/vcs0

crw--w----   1 root     tty        7,   1 Sep 27  1995 /dev/vcs1

crw--w----   1 root     tty        7,   2 Sep 27  1995 /dev/vcs2

crw--w----   1 root     tty        7, 128 Sep 27  1995 /dev/vcsa0

crw--w----   1 root     tty        7, 129 Sep 27  1995 /dev/vcsa1

crw--w----   1 root     tty        7, 130 Sep 27  1995 /dev/vcsa2

Each set is numbered from 0 to 63, corresponding to the numbering system for the /dev/tty* console devices. The device /dev/vcs0, like the device dev/tty0, always refers to the currently selected VC.

Previous | Table of Contents | Next