6.4 Hardware Devices

Some common block devices are listed in Table 6.1. Device numbers for similar devices follow the obvious pattern (for instance, the second partition on the first SCSI drive is /dev/sda2). It's occasionally useful to know which devices these device names correspond to when examining mounted file systems in /proc/mounts (see Section 7.5, "Drives, Mounts, and File 7.5," in Chapter 7, for more about this).

Table 6.1. Partial Listing of Common Block Devices

Device

Name

Major

Minor

First floppy drive

/dev/fd0

2

0

Second floppy drive

/dev/fd1

2

1

Primary IDE controller, master device

/dev/hda

3

0

Primary IDE controller, master device, first partition

/dev/hda1

3

1

Primary IDE controller, secondary device

/dev/hdb

3

64

Primary IDE controller, secondary device, first partition

/dev/hdb1

3

65

Secondary IDE controller, master device

/dev/hdc

22

0

Secondary IDE controller, secondary device

/dev/hdd

22

64

First SCSI drive

/dev/sda

8

0

First SCSI drive, first partition

/dev/sda1

8

1

Second SCSI disk

/dev/sdb

8

16

Second SCSI disk, first partition

/dev/sdb1

8

17

First SCSI CD-ROM drive

/dev/scd0

11

0

Second SCSI CD-ROM drive

/dev/scd1

11

1

Table 6.2 lists some common character devices.

Table 6.2. Some Common Character Devices

Device

Name

Major

Minor

Parallel port 0

/dev/lp0 or /dev/par0

6

0

Parallel port 1

/dev/lp1 or /dev/par1

6

1

First serial port

/dev/ttyS0

4

64

Second serial port

/dev/ttyS1

4

65

IDE tape drive

/dev/ht0

37

0

First SCSI tape drive

/dev/st0

9

0

Second SCSI tape drive

/dev/st1

9

1

System console

/dev/console

5

1

First virtual terminal

/dev/tty1

4

1

Second virtual terminal

/dev/tty2

4

2

Process's current terminal device

/dev/tty

5

0

Sound card

/dev/audio

14

4

You can access certain hardware components through more than one character device; often, the different character devices provide different semantics. For example, when you use the IDE tape device /dev/ht0, Linux automatically rewinds the tape in the drive when you close the file descriptor. You can use the device /dev/nht0 to access the same tape drive, except that Linux will not automatically rewind the tape when you close the file descriptor. You sometimes might see programs using /dev/cua0 and similar devices; these are older interfaces to serial ports such as /dev/ttyS0.

Occasionally, you'll want to write data directly to character devices—for example:

·         A terminal program might access a modem directly through a serial port device. Data written to or read from the devices is transmitted via the modem to a remote computer.

·         A tape backup program might write data directly to a tape device. The backup program could implement its own compression and error-checking format.

·         A program can write directly to the first virtual terminal [3] writing data to /dev/tty1.

[3] On most GNU/Linux systems , you can switch to the first virtual terminal by pressing Ctrl+Alt+F1. Use Ctrl+Alt+F2 for the second virtual terminal, and so on.

Terminal windows running in a graphical environment, or remote login terminal sessions, are not associated with virtual terminals; instead, they're associated with pseudo-terminals. See Section 6.6, "PTYs," for information about these.

·         Sometimes a program needs to access the terminal device with which it is associated.

For example, your program may need to prompt the user for a password. For security reasons, you might want to ignore redirection of standard input and output and always read the password from the terminal, no matter how the user invokes the command. One way to do this is to open /dev/tty, which always corresponds to the terminal device associated with the process that opens it. Write the prompt message to that device, and read the password from it. By ignoring standard input and output, this prevents the user from feeding your program a password from a file using shell syntax such as this:

 
% secure_program < my-password.txt 

If you need to authenticate users in your program, you should learn about GNU/Linux's PAM facility. See Section 10.5, "Authenticating Users," in Chapter 10, "Security," for more information.

·         A program can play sounds through the system's sound card by sending audio data to /dev/audio. Note that the audio data must be in Sun audio format (usually associated with the .au extension).

For example, many GNU/Linux distributions come with the classic sound file /usr/share/sndconfig/sample.au. If your system includes this file, try playing it by invoking the following:

 
% cat /usr/share/sndconfig/sample.au > /dev/audio 

If you're planning on using sound in your program, though, you should investigate the various sound libraries and services available for GNU/Linux . The Gnome windowing environment uses the Enlightenment Sound Daemon (EsounD), at http://www.tux.org/~ricdude/EsounD.html. KDE uses aRts, at http://space.twc.de/~stefan/kde/arts-mcop-doc/. If you use one of these sound systems instead of writing directly to /dev/audio, your program will cooperate better with other programs that use the computer's sound card.