Special files can be found on most Unix-based operating systems. They are commonly used as a virtual interface between a device driver and some userspace application. There are a variety of special files on a Linux system. A few of the common files include a character device or a block device. A character device or character special file allow transmission of one character or byte at a time between device node and some userspace application. These are used with mice, keyboards as a stream communication mechanism. Another type of special file is for a block device where data can be transferred in blocks and is used for a higher data rate with devices like a CD-ROM drive or your hard disk drive. There are also special files for pseudo devices, like /dev/random ( I wrote about random number generation in an earlier post, found here ). Now that you have a basic idea of what a special file could be used for, how do you make them?
Create a Special File Using ‘mknod’
Well you guessed it, the whole title of the article highlights what tool you may use to create a special file. This can be done from a utility and also using a C function called mknod.
You can create a device entry in the file system using the mknod command or the mknod system call (in C). If you create a device entry it doesn’t necessarily mean that the device driver or hardware device is present or available, the device entry is only a hole for communicating with the driver. You have to be root to create a block or character devices when using the mknod command or the mknod system call.
The mknod command takes four (4) arguments. The first argument is the path in which the entry will appear in your filesystem (/dev/random for example). The second argument allows you to specify b for a block device or c for a character device. The third and fourth arguments allow you to specify the major and minor device numbers. The major and minor numbers are meant to relate to a specific hardware device on your system.
As an example:
# mknod ./dev/random b 12 5
This is stating create the /dev/random file descriptor, as a block device with major device number of 12 and a minor device number of 5.
As I mentioned earlier, this can also be achieved within a C process (running as root) to create a special file. Its parameters are mostly similar:
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
int mknod(const char *pathname, mode_t mode, dev_t dev);
The pathname is the /dev/random mentioned above.
Taken from the mknod manual page:
The mode argument specifies both the permissions to use and the type of node to be created. It should be a combination (using bitwise OR) of one of the file types listed below and the permissions for the new node. The permissions are modified by the process’s umask in the usual way: the permissions of the created node are (mode & ~umask). The file type must be one of S_IFREG, S_IFCHR, S_IFBLK, S_IFIFO or S_IFSOCK to specify a regular file (which will be created empty), character special file, block special file, FIFO (named pipe), or Unix domain socket, respectively. (Zero file type is equivalent to type S_IFREG.) If the file type is S_IFCHR or S_IFBLK then dev specifies the major and minor numbers of the newly created device special file (makedev(3) may be useful to build the value for dev); otherwise it is ignored.
There is also a great article here for more information about mknod.
There you have it, use mknod in Bash or C!