How do character device or character special files work?
Clash Royale CLAN TAG#URR8PPP
up vote
19
down vote
favorite
I am trying to understand character special files. From wikipedia, I understand that these files
"provide an interface" for devices that transmit data one character at a time. My understanding is that the system somehow calls the character device instead of calling the device driver directly. But how does the file provide this interface? Is it an executable that translates the system call? Can someone explain what's up.
files drivers
add a comment |Â
up vote
19
down vote
favorite
I am trying to understand character special files. From wikipedia, I understand that these files
"provide an interface" for devices that transmit data one character at a time. My understanding is that the system somehow calls the character device instead of calling the device driver directly. But how does the file provide this interface? Is it an executable that translates the system call? Can someone explain what's up.
files drivers
add a comment |Â
up vote
19
down vote
favorite
up vote
19
down vote
favorite
I am trying to understand character special files. From wikipedia, I understand that these files
"provide an interface" for devices that transmit data one character at a time. My understanding is that the system somehow calls the character device instead of calling the device driver directly. But how does the file provide this interface? Is it an executable that translates the system call? Can someone explain what's up.
files drivers
I am trying to understand character special files. From wikipedia, I understand that these files
"provide an interface" for devices that transmit data one character at a time. My understanding is that the system somehow calls the character device instead of calling the device driver directly. But how does the file provide this interface? Is it an executable that translates the system call? Can someone explain what's up.
files drivers
edited Mar 2 at 7:31
Ciro Santilli æ°çÂÂæ¹é ä¸Âå¿ å ÂÃ¥ÂÂäºÂ件 æ³Âè½®åÂÂ
4,14613936
4,14613936
asked May 3 '12 at 14:53
bernie2436
1,920153755
1,920153755
add a comment |Â
add a comment |Â
5 Answers
5
active
oldest
votes
up vote
17
down vote
accepted
They are actually just that - interfaces. Encoded by a "major" and "minor" number they provide a hook to the kernel.
They come in two flavors (well, three, but named pipes are out of the scope of this explanation for now): Character Devices and Block Devices.
Block Devices tend to be storage devices, capable of buffering output and storing data for later retrieval.
Character Devices are things like audio or graphics cards, or input devices like keyboard and mouse.
In each case, when the kernel loads the correct driver (either at boot time, or via programs like udev) it scans the various buses to see if any devices handled by that driver are actually present on the system. If so, it sets up a device that 'listens' on the appropriate major/minor number.
(For instance, the Digital Signal Processor of the first audio card found by your system gets the major/minor number pair of 14/3; the second gets 14,35, etc.)
It's up to udev to create an entry in /dev
named dsp
as a character device marked major 14 minor 3.
(In significantly older or minimum-footprint versions of Linux, /dev/
may not be dynamically loaded but just contain all possible device files statically.)
Then, when a userspace program tries to access a file that's marked as a 'character special file' with the appropriate major/minor number (for instance, your audio player trying to send digital audio to /dev/dsp
), the kernel knows that this data needs to be transmitted via the driver that major/minor number is attached to; presumably said driver knows what to do with it in turn.
1
1. So major/minor numbers are analogous to ports?
â bernie2436
May 3 '12 at 15:38
2. So when programs access any file, the kernel reads these special interfaces to learn if the program should get interrupts from a particular device? Ex: if a program opens a word file, it reads the character device special file to know the program should respond to keyboard input?
â bernie2436
May 3 '12 at 15:45
1) Somewhat. It's a poor man's analogy but it'll do.
â Shadur
May 3 '12 at 17:21
2
2) You're missing about three or four layers of abstraction there. The program you open a text file with neither knows nor cares what the keyboard device is. Communication with the underlying hardware happens either via the terminal emulator (if you're in console mode) or via the X event layer (if you're in graphics mode), either of which will listen to the keyboard and other drives and decides what, if anything, to pass on to the program. I'm summarizing a fairly complex multilayer system here; you might do well to read up on the X Window System in general.
â Shadur
May 3 '12 at 17:24
1
Note also that, on some flavors of UN*X, there are character special files for storage devices; a read or a write to the special file turns into a read or write to a sequence of blocks on the device. (In recent versions of FreeBSD, those are the only special files for storage devices; there are no block special files.)
â user44841
Oct 22 '14 at 0:34
add a comment |Â
up vote
9
down vote
Every file, device or otherwise, supports 6 basic operations within the VFS:
- Open
- Close
- Read
- Write
- Seek
- Tell
Additionally, device files support I/O Control, which allows other miscellaneous operations not covered by the first 6.
In the case of a character special, seek and tell are not implemented since they support a streaming interface. That is, reading or writing directly such as is done with redirection in the shell:
echo 'foo' > /dev/some/char
sed ... < /dev/some/char
add a comment |Â
up vote
4
down vote
Minimal runnable file_operations
example
Once you see a minimal example, it all becomes obvious.
The key ideas are:
file_operations
contains the callbacks for each file related syscallmknod path c major minor
creates a character device which uses thosefile_operations
- for character devices that dynamically allocate device numbers (the norm to avoid conflicts), find the number with
cat /proc/devices
character_device.ko
kernel module:
#include <asm/uaccess.h> /* copy_from_user, copy_to_user */
#include <linux/errno.h> /* EFAULT */
#include <linux/fs.h> /* register_chrdev, unregister_chrdev */
#include <linux/jiffies.h>
#include <linux/module.h>
#include <linux/printk.h> /* printk */
#include <uapi/linux/stat.h> /* S_IRUSR */
#define NAME "lkmc_character_device"
MODULE_LICENSE("GPL");
static int major;
static ssize_t read(struct file *filp, char __user *buf, size_t len, loff_t *off)
size_t ret;
char kbuf = 'a', 'b', 'c', 'd';
ret = 0;
if (*off == 0)
if (copy_to_user(buf, kbuf, sizeof(kbuf)))
ret = -EFAULT;
else
ret = sizeof(kbuf);
*off = 1;
return ret;
static const struct file_operations fops =
.owner = THIS_MODULE,
.read = read,
;
static int myinit(void)
major = register_chrdev(0, NAME, &fops);
return 0;
static void myexit(void)
unregister_chrdev(major, NAME);
module_init(myinit)
module_exit(myexit)
Userland test program:
insmod /character_device.ko
dev="lkmc_character_device"
major="$(grep "$dev" /proc/devices | cut -d ' ' -f 1)"
mknod "/dev/$dev" c "$major" 0
cat /dev/lkmc_character_device
# => abcd
rm /dev/lkmc_character_device
rmmod character_device
GitHub QEMU + Buildroot upstream with boilerplate to actually run it:
- https://github.com/cirosantilli/linux-kernel-module-cheat/blob/6788a577c394a2fc512d8f3df0806d84dc09f355/kernel_module/character_device.c
- https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/rootfs_overlay/character_device.sh
More complex examples:
read
,write
,lseek
with a fixed size internal buffer and on debugfs instead of a character device: https://github.com/cirosantilli/linux-kernel-module-cheat/blob/6788a577c394a2fc512d8f3df0806d84dc09f355/kernel_module/fops.cpoll
: https://github.com/cirosantilli/linux-kernel-module-cheat/blob/6788a577c394a2fc512d8f3df0806d84dc09f355/kernel_module/poll.cioctl
: https://github.com/cirosantilli/linux-kernel-module-cheat/blob/6788a577c394a2fc512d8f3df0806d84dc09f355/kernel_module/poll.canon_inode_getfd
associates afile_operations
to a file descriptor without any filesystem file: https://stackoverflow.com/questions/4508998/what-is-anonymous-inode/44388030#44388030
add a comment |Â
up vote
3
down vote
"Character at a time" is a misnomer (as is the idea that character devices necessarily do not support seek and tell). In fact, "block at a time" (i.e. strictly record-oriented, such as a tape drive*) devices must be character devices. So is the idea that a character device must necessarily be unseekable - character device drivers define a full file_operations
structure which is free to define llseek or not according to whether the device supports the operation. The character devices most people think of as examples are null, urandom, TTY devices, sound card, mouse, etc... which are all unseekable because of the specifics of what those devices are, but /dev/vcs, /dev/fb0, and /dev/kmem are also character devices and they are all seekable.
As I mentioned, a character device driver defines a file_operations structure that has function pointers for all the operations someone might want to call on a file - seek, read, write, ioctl, etc - and these are each called once when the corresponding system call is executed with this device file open. And read and write can therefore do whatever it wants with its arguments - it can refuse to accept a write that's too large or only write what fits; it can read only the data corresponding to one record rather than the whole requested number of bytes.
So, what is a block device, then? Basically, block devices are disk drives. No other kind of device (except for virtual disk drives, like ramdisk and loopback) is a block device. They are integrated into the I/O request system, the filesystem layer, the buffer/cache system, and the virtual memory system in a way that character devices are not, even when you're accessing e.g. /dev/sda from a user process. Even the "raw devices" that page mentions as an exception are character devices.
*Some UNIX systems implemented what is now called "fixed-block mode" - which lets the kernel group and split I/O requests to fit the configured block boundaries in more or less the same manner as is done for disk drives - as a block device. A character device is needed for "variable-block mode", which preserves block boundaries from the user program as a single write(2) call writes one block and a single read(2) call returns one block. Since mode switching is implemented now as an ioctl rather than a separate device file, a character device is used. Variable-record tape drives are mostly "non-seekable" because seeking involves counting a number of records rather than a number of bytes, and the native seek operation is implemented as an ioctl.
add a comment |Â
up vote
1
down vote
Character devices can be created by kernel modules (or the kernel itself). When a device is created, the creator provides pointers to functions that implement handle standard calls like open, read, etc. The Linux kernel then associates those functions with the character device, so for example when a user-mode application calls the read() function on a character device file, it will result in a syscall and then the kernel will route this call to a read function specified when creating the driver.
There's a step-by-step tutorial on creating a character device here, you can create a sample project and the step through it using a debugger to understand how the device object is created and when the handlers are invoked.
add a comment |Â
5 Answers
5
active
oldest
votes
5 Answers
5
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
17
down vote
accepted
They are actually just that - interfaces. Encoded by a "major" and "minor" number they provide a hook to the kernel.
They come in two flavors (well, three, but named pipes are out of the scope of this explanation for now): Character Devices and Block Devices.
Block Devices tend to be storage devices, capable of buffering output and storing data for later retrieval.
Character Devices are things like audio or graphics cards, or input devices like keyboard and mouse.
In each case, when the kernel loads the correct driver (either at boot time, or via programs like udev) it scans the various buses to see if any devices handled by that driver are actually present on the system. If so, it sets up a device that 'listens' on the appropriate major/minor number.
(For instance, the Digital Signal Processor of the first audio card found by your system gets the major/minor number pair of 14/3; the second gets 14,35, etc.)
It's up to udev to create an entry in /dev
named dsp
as a character device marked major 14 minor 3.
(In significantly older or minimum-footprint versions of Linux, /dev/
may not be dynamically loaded but just contain all possible device files statically.)
Then, when a userspace program tries to access a file that's marked as a 'character special file' with the appropriate major/minor number (for instance, your audio player trying to send digital audio to /dev/dsp
), the kernel knows that this data needs to be transmitted via the driver that major/minor number is attached to; presumably said driver knows what to do with it in turn.
1
1. So major/minor numbers are analogous to ports?
â bernie2436
May 3 '12 at 15:38
2. So when programs access any file, the kernel reads these special interfaces to learn if the program should get interrupts from a particular device? Ex: if a program opens a word file, it reads the character device special file to know the program should respond to keyboard input?
â bernie2436
May 3 '12 at 15:45
1) Somewhat. It's a poor man's analogy but it'll do.
â Shadur
May 3 '12 at 17:21
2
2) You're missing about three or four layers of abstraction there. The program you open a text file with neither knows nor cares what the keyboard device is. Communication with the underlying hardware happens either via the terminal emulator (if you're in console mode) or via the X event layer (if you're in graphics mode), either of which will listen to the keyboard and other drives and decides what, if anything, to pass on to the program. I'm summarizing a fairly complex multilayer system here; you might do well to read up on the X Window System in general.
â Shadur
May 3 '12 at 17:24
1
Note also that, on some flavors of UN*X, there are character special files for storage devices; a read or a write to the special file turns into a read or write to a sequence of blocks on the device. (In recent versions of FreeBSD, those are the only special files for storage devices; there are no block special files.)
â user44841
Oct 22 '14 at 0:34
add a comment |Â
up vote
17
down vote
accepted
They are actually just that - interfaces. Encoded by a "major" and "minor" number they provide a hook to the kernel.
They come in two flavors (well, three, but named pipes are out of the scope of this explanation for now): Character Devices and Block Devices.
Block Devices tend to be storage devices, capable of buffering output and storing data for later retrieval.
Character Devices are things like audio or graphics cards, or input devices like keyboard and mouse.
In each case, when the kernel loads the correct driver (either at boot time, or via programs like udev) it scans the various buses to see if any devices handled by that driver are actually present on the system. If so, it sets up a device that 'listens' on the appropriate major/minor number.
(For instance, the Digital Signal Processor of the first audio card found by your system gets the major/minor number pair of 14/3; the second gets 14,35, etc.)
It's up to udev to create an entry in /dev
named dsp
as a character device marked major 14 minor 3.
(In significantly older or minimum-footprint versions of Linux, /dev/
may not be dynamically loaded but just contain all possible device files statically.)
Then, when a userspace program tries to access a file that's marked as a 'character special file' with the appropriate major/minor number (for instance, your audio player trying to send digital audio to /dev/dsp
), the kernel knows that this data needs to be transmitted via the driver that major/minor number is attached to; presumably said driver knows what to do with it in turn.
1
1. So major/minor numbers are analogous to ports?
â bernie2436
May 3 '12 at 15:38
2. So when programs access any file, the kernel reads these special interfaces to learn if the program should get interrupts from a particular device? Ex: if a program opens a word file, it reads the character device special file to know the program should respond to keyboard input?
â bernie2436
May 3 '12 at 15:45
1) Somewhat. It's a poor man's analogy but it'll do.
â Shadur
May 3 '12 at 17:21
2
2) You're missing about three or four layers of abstraction there. The program you open a text file with neither knows nor cares what the keyboard device is. Communication with the underlying hardware happens either via the terminal emulator (if you're in console mode) or via the X event layer (if you're in graphics mode), either of which will listen to the keyboard and other drives and decides what, if anything, to pass on to the program. I'm summarizing a fairly complex multilayer system here; you might do well to read up on the X Window System in general.
â Shadur
May 3 '12 at 17:24
1
Note also that, on some flavors of UN*X, there are character special files for storage devices; a read or a write to the special file turns into a read or write to a sequence of blocks on the device. (In recent versions of FreeBSD, those are the only special files for storage devices; there are no block special files.)
â user44841
Oct 22 '14 at 0:34
add a comment |Â
up vote
17
down vote
accepted
up vote
17
down vote
accepted
They are actually just that - interfaces. Encoded by a "major" and "minor" number they provide a hook to the kernel.
They come in two flavors (well, three, but named pipes are out of the scope of this explanation for now): Character Devices and Block Devices.
Block Devices tend to be storage devices, capable of buffering output and storing data for later retrieval.
Character Devices are things like audio or graphics cards, or input devices like keyboard and mouse.
In each case, when the kernel loads the correct driver (either at boot time, or via programs like udev) it scans the various buses to see if any devices handled by that driver are actually present on the system. If so, it sets up a device that 'listens' on the appropriate major/minor number.
(For instance, the Digital Signal Processor of the first audio card found by your system gets the major/minor number pair of 14/3; the second gets 14,35, etc.)
It's up to udev to create an entry in /dev
named dsp
as a character device marked major 14 minor 3.
(In significantly older or minimum-footprint versions of Linux, /dev/
may not be dynamically loaded but just contain all possible device files statically.)
Then, when a userspace program tries to access a file that's marked as a 'character special file' with the appropriate major/minor number (for instance, your audio player trying to send digital audio to /dev/dsp
), the kernel knows that this data needs to be transmitted via the driver that major/minor number is attached to; presumably said driver knows what to do with it in turn.
They are actually just that - interfaces. Encoded by a "major" and "minor" number they provide a hook to the kernel.
They come in two flavors (well, three, but named pipes are out of the scope of this explanation for now): Character Devices and Block Devices.
Block Devices tend to be storage devices, capable of buffering output and storing data for later retrieval.
Character Devices are things like audio or graphics cards, or input devices like keyboard and mouse.
In each case, when the kernel loads the correct driver (either at boot time, or via programs like udev) it scans the various buses to see if any devices handled by that driver are actually present on the system. If so, it sets up a device that 'listens' on the appropriate major/minor number.
(For instance, the Digital Signal Processor of the first audio card found by your system gets the major/minor number pair of 14/3; the second gets 14,35, etc.)
It's up to udev to create an entry in /dev
named dsp
as a character device marked major 14 minor 3.
(In significantly older or minimum-footprint versions of Linux, /dev/
may not be dynamically loaded but just contain all possible device files statically.)
Then, when a userspace program tries to access a file that's marked as a 'character special file' with the appropriate major/minor number (for instance, your audio player trying to send digital audio to /dev/dsp
), the kernel knows that this data needs to be transmitted via the driver that major/minor number is attached to; presumably said driver knows what to do with it in turn.
edited Nov 23 '16 at 20:20
Eddie
1033
1033
answered May 3 '12 at 15:09
Shadur
18.5k64154
18.5k64154
1
1. So major/minor numbers are analogous to ports?
â bernie2436
May 3 '12 at 15:38
2. So when programs access any file, the kernel reads these special interfaces to learn if the program should get interrupts from a particular device? Ex: if a program opens a word file, it reads the character device special file to know the program should respond to keyboard input?
â bernie2436
May 3 '12 at 15:45
1) Somewhat. It's a poor man's analogy but it'll do.
â Shadur
May 3 '12 at 17:21
2
2) You're missing about three or four layers of abstraction there. The program you open a text file with neither knows nor cares what the keyboard device is. Communication with the underlying hardware happens either via the terminal emulator (if you're in console mode) or via the X event layer (if you're in graphics mode), either of which will listen to the keyboard and other drives and decides what, if anything, to pass on to the program. I'm summarizing a fairly complex multilayer system here; you might do well to read up on the X Window System in general.
â Shadur
May 3 '12 at 17:24
1
Note also that, on some flavors of UN*X, there are character special files for storage devices; a read or a write to the special file turns into a read or write to a sequence of blocks on the device. (In recent versions of FreeBSD, those are the only special files for storage devices; there are no block special files.)
â user44841
Oct 22 '14 at 0:34
add a comment |Â
1
1. So major/minor numbers are analogous to ports?
â bernie2436
May 3 '12 at 15:38
2. So when programs access any file, the kernel reads these special interfaces to learn if the program should get interrupts from a particular device? Ex: if a program opens a word file, it reads the character device special file to know the program should respond to keyboard input?
â bernie2436
May 3 '12 at 15:45
1) Somewhat. It's a poor man's analogy but it'll do.
â Shadur
May 3 '12 at 17:21
2
2) You're missing about three or four layers of abstraction there. The program you open a text file with neither knows nor cares what the keyboard device is. Communication with the underlying hardware happens either via the terminal emulator (if you're in console mode) or via the X event layer (if you're in graphics mode), either of which will listen to the keyboard and other drives and decides what, if anything, to pass on to the program. I'm summarizing a fairly complex multilayer system here; you might do well to read up on the X Window System in general.
â Shadur
May 3 '12 at 17:24
1
Note also that, on some flavors of UN*X, there are character special files for storage devices; a read or a write to the special file turns into a read or write to a sequence of blocks on the device. (In recent versions of FreeBSD, those are the only special files for storage devices; there are no block special files.)
â user44841
Oct 22 '14 at 0:34
1
1
1. So major/minor numbers are analogous to ports?
â bernie2436
May 3 '12 at 15:38
1. So major/minor numbers are analogous to ports?
â bernie2436
May 3 '12 at 15:38
2. So when programs access any file, the kernel reads these special interfaces to learn if the program should get interrupts from a particular device? Ex: if a program opens a word file, it reads the character device special file to know the program should respond to keyboard input?
â bernie2436
May 3 '12 at 15:45
2. So when programs access any file, the kernel reads these special interfaces to learn if the program should get interrupts from a particular device? Ex: if a program opens a word file, it reads the character device special file to know the program should respond to keyboard input?
â bernie2436
May 3 '12 at 15:45
1) Somewhat. It's a poor man's analogy but it'll do.
â Shadur
May 3 '12 at 17:21
1) Somewhat. It's a poor man's analogy but it'll do.
â Shadur
May 3 '12 at 17:21
2
2
2) You're missing about three or four layers of abstraction there. The program you open a text file with neither knows nor cares what the keyboard device is. Communication with the underlying hardware happens either via the terminal emulator (if you're in console mode) or via the X event layer (if you're in graphics mode), either of which will listen to the keyboard and other drives and decides what, if anything, to pass on to the program. I'm summarizing a fairly complex multilayer system here; you might do well to read up on the X Window System in general.
â Shadur
May 3 '12 at 17:24
2) You're missing about three or four layers of abstraction there. The program you open a text file with neither knows nor cares what the keyboard device is. Communication with the underlying hardware happens either via the terminal emulator (if you're in console mode) or via the X event layer (if you're in graphics mode), either of which will listen to the keyboard and other drives and decides what, if anything, to pass on to the program. I'm summarizing a fairly complex multilayer system here; you might do well to read up on the X Window System in general.
â Shadur
May 3 '12 at 17:24
1
1
Note also that, on some flavors of UN*X, there are character special files for storage devices; a read or a write to the special file turns into a read or write to a sequence of blocks on the device. (In recent versions of FreeBSD, those are the only special files for storage devices; there are no block special files.)
â user44841
Oct 22 '14 at 0:34
Note also that, on some flavors of UN*X, there are character special files for storage devices; a read or a write to the special file turns into a read or write to a sequence of blocks on the device. (In recent versions of FreeBSD, those are the only special files for storage devices; there are no block special files.)
â user44841
Oct 22 '14 at 0:34
add a comment |Â
up vote
9
down vote
Every file, device or otherwise, supports 6 basic operations within the VFS:
- Open
- Close
- Read
- Write
- Seek
- Tell
Additionally, device files support I/O Control, which allows other miscellaneous operations not covered by the first 6.
In the case of a character special, seek and tell are not implemented since they support a streaming interface. That is, reading or writing directly such as is done with redirection in the shell:
echo 'foo' > /dev/some/char
sed ... < /dev/some/char
add a comment |Â
up vote
9
down vote
Every file, device or otherwise, supports 6 basic operations within the VFS:
- Open
- Close
- Read
- Write
- Seek
- Tell
Additionally, device files support I/O Control, which allows other miscellaneous operations not covered by the first 6.
In the case of a character special, seek and tell are not implemented since they support a streaming interface. That is, reading or writing directly such as is done with redirection in the shell:
echo 'foo' > /dev/some/char
sed ... < /dev/some/char
add a comment |Â
up vote
9
down vote
up vote
9
down vote
Every file, device or otherwise, supports 6 basic operations within the VFS:
- Open
- Close
- Read
- Write
- Seek
- Tell
Additionally, device files support I/O Control, which allows other miscellaneous operations not covered by the first 6.
In the case of a character special, seek and tell are not implemented since they support a streaming interface. That is, reading or writing directly such as is done with redirection in the shell:
echo 'foo' > /dev/some/char
sed ... < /dev/some/char
Every file, device or otherwise, supports 6 basic operations within the VFS:
- Open
- Close
- Read
- Write
- Seek
- Tell
Additionally, device files support I/O Control, which allows other miscellaneous operations not covered by the first 6.
In the case of a character special, seek and tell are not implemented since they support a streaming interface. That is, reading or writing directly such as is done with redirection in the shell:
echo 'foo' > /dev/some/char
sed ... < /dev/some/char
answered May 3 '12 at 15:27
Ignacio Vazquez-Abrams
32.1k66780
32.1k66780
add a comment |Â
add a comment |Â
up vote
4
down vote
Minimal runnable file_operations
example
Once you see a minimal example, it all becomes obvious.
The key ideas are:
file_operations
contains the callbacks for each file related syscallmknod path c major minor
creates a character device which uses thosefile_operations
- for character devices that dynamically allocate device numbers (the norm to avoid conflicts), find the number with
cat /proc/devices
character_device.ko
kernel module:
#include <asm/uaccess.h> /* copy_from_user, copy_to_user */
#include <linux/errno.h> /* EFAULT */
#include <linux/fs.h> /* register_chrdev, unregister_chrdev */
#include <linux/jiffies.h>
#include <linux/module.h>
#include <linux/printk.h> /* printk */
#include <uapi/linux/stat.h> /* S_IRUSR */
#define NAME "lkmc_character_device"
MODULE_LICENSE("GPL");
static int major;
static ssize_t read(struct file *filp, char __user *buf, size_t len, loff_t *off)
size_t ret;
char kbuf = 'a', 'b', 'c', 'd';
ret = 0;
if (*off == 0)
if (copy_to_user(buf, kbuf, sizeof(kbuf)))
ret = -EFAULT;
else
ret = sizeof(kbuf);
*off = 1;
return ret;
static const struct file_operations fops =
.owner = THIS_MODULE,
.read = read,
;
static int myinit(void)
major = register_chrdev(0, NAME, &fops);
return 0;
static void myexit(void)
unregister_chrdev(major, NAME);
module_init(myinit)
module_exit(myexit)
Userland test program:
insmod /character_device.ko
dev="lkmc_character_device"
major="$(grep "$dev" /proc/devices | cut -d ' ' -f 1)"
mknod "/dev/$dev" c "$major" 0
cat /dev/lkmc_character_device
# => abcd
rm /dev/lkmc_character_device
rmmod character_device
GitHub QEMU + Buildroot upstream with boilerplate to actually run it:
- https://github.com/cirosantilli/linux-kernel-module-cheat/blob/6788a577c394a2fc512d8f3df0806d84dc09f355/kernel_module/character_device.c
- https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/rootfs_overlay/character_device.sh
More complex examples:
read
,write
,lseek
with a fixed size internal buffer and on debugfs instead of a character device: https://github.com/cirosantilli/linux-kernel-module-cheat/blob/6788a577c394a2fc512d8f3df0806d84dc09f355/kernel_module/fops.cpoll
: https://github.com/cirosantilli/linux-kernel-module-cheat/blob/6788a577c394a2fc512d8f3df0806d84dc09f355/kernel_module/poll.cioctl
: https://github.com/cirosantilli/linux-kernel-module-cheat/blob/6788a577c394a2fc512d8f3df0806d84dc09f355/kernel_module/poll.canon_inode_getfd
associates afile_operations
to a file descriptor without any filesystem file: https://stackoverflow.com/questions/4508998/what-is-anonymous-inode/44388030#44388030
add a comment |Â
up vote
4
down vote
Minimal runnable file_operations
example
Once you see a minimal example, it all becomes obvious.
The key ideas are:
file_operations
contains the callbacks for each file related syscallmknod path c major minor
creates a character device which uses thosefile_operations
- for character devices that dynamically allocate device numbers (the norm to avoid conflicts), find the number with
cat /proc/devices
character_device.ko
kernel module:
#include <asm/uaccess.h> /* copy_from_user, copy_to_user */
#include <linux/errno.h> /* EFAULT */
#include <linux/fs.h> /* register_chrdev, unregister_chrdev */
#include <linux/jiffies.h>
#include <linux/module.h>
#include <linux/printk.h> /* printk */
#include <uapi/linux/stat.h> /* S_IRUSR */
#define NAME "lkmc_character_device"
MODULE_LICENSE("GPL");
static int major;
static ssize_t read(struct file *filp, char __user *buf, size_t len, loff_t *off)
size_t ret;
char kbuf = 'a', 'b', 'c', 'd';
ret = 0;
if (*off == 0)
if (copy_to_user(buf, kbuf, sizeof(kbuf)))
ret = -EFAULT;
else
ret = sizeof(kbuf);
*off = 1;
return ret;
static const struct file_operations fops =
.owner = THIS_MODULE,
.read = read,
;
static int myinit(void)
major = register_chrdev(0, NAME, &fops);
return 0;
static void myexit(void)
unregister_chrdev(major, NAME);
module_init(myinit)
module_exit(myexit)
Userland test program:
insmod /character_device.ko
dev="lkmc_character_device"
major="$(grep "$dev" /proc/devices | cut -d ' ' -f 1)"
mknod "/dev/$dev" c "$major" 0
cat /dev/lkmc_character_device
# => abcd
rm /dev/lkmc_character_device
rmmod character_device
GitHub QEMU + Buildroot upstream with boilerplate to actually run it:
- https://github.com/cirosantilli/linux-kernel-module-cheat/blob/6788a577c394a2fc512d8f3df0806d84dc09f355/kernel_module/character_device.c
- https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/rootfs_overlay/character_device.sh
More complex examples:
read
,write
,lseek
with a fixed size internal buffer and on debugfs instead of a character device: https://github.com/cirosantilli/linux-kernel-module-cheat/blob/6788a577c394a2fc512d8f3df0806d84dc09f355/kernel_module/fops.cpoll
: https://github.com/cirosantilli/linux-kernel-module-cheat/blob/6788a577c394a2fc512d8f3df0806d84dc09f355/kernel_module/poll.cioctl
: https://github.com/cirosantilli/linux-kernel-module-cheat/blob/6788a577c394a2fc512d8f3df0806d84dc09f355/kernel_module/poll.canon_inode_getfd
associates afile_operations
to a file descriptor without any filesystem file: https://stackoverflow.com/questions/4508998/what-is-anonymous-inode/44388030#44388030
add a comment |Â
up vote
4
down vote
up vote
4
down vote
Minimal runnable file_operations
example
Once you see a minimal example, it all becomes obvious.
The key ideas are:
file_operations
contains the callbacks for each file related syscallmknod path c major minor
creates a character device which uses thosefile_operations
- for character devices that dynamically allocate device numbers (the norm to avoid conflicts), find the number with
cat /proc/devices
character_device.ko
kernel module:
#include <asm/uaccess.h> /* copy_from_user, copy_to_user */
#include <linux/errno.h> /* EFAULT */
#include <linux/fs.h> /* register_chrdev, unregister_chrdev */
#include <linux/jiffies.h>
#include <linux/module.h>
#include <linux/printk.h> /* printk */
#include <uapi/linux/stat.h> /* S_IRUSR */
#define NAME "lkmc_character_device"
MODULE_LICENSE("GPL");
static int major;
static ssize_t read(struct file *filp, char __user *buf, size_t len, loff_t *off)
size_t ret;
char kbuf = 'a', 'b', 'c', 'd';
ret = 0;
if (*off == 0)
if (copy_to_user(buf, kbuf, sizeof(kbuf)))
ret = -EFAULT;
else
ret = sizeof(kbuf);
*off = 1;
return ret;
static const struct file_operations fops =
.owner = THIS_MODULE,
.read = read,
;
static int myinit(void)
major = register_chrdev(0, NAME, &fops);
return 0;
static void myexit(void)
unregister_chrdev(major, NAME);
module_init(myinit)
module_exit(myexit)
Userland test program:
insmod /character_device.ko
dev="lkmc_character_device"
major="$(grep "$dev" /proc/devices | cut -d ' ' -f 1)"
mknod "/dev/$dev" c "$major" 0
cat /dev/lkmc_character_device
# => abcd
rm /dev/lkmc_character_device
rmmod character_device
GitHub QEMU + Buildroot upstream with boilerplate to actually run it:
- https://github.com/cirosantilli/linux-kernel-module-cheat/blob/6788a577c394a2fc512d8f3df0806d84dc09f355/kernel_module/character_device.c
- https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/rootfs_overlay/character_device.sh
More complex examples:
read
,write
,lseek
with a fixed size internal buffer and on debugfs instead of a character device: https://github.com/cirosantilli/linux-kernel-module-cheat/blob/6788a577c394a2fc512d8f3df0806d84dc09f355/kernel_module/fops.cpoll
: https://github.com/cirosantilli/linux-kernel-module-cheat/blob/6788a577c394a2fc512d8f3df0806d84dc09f355/kernel_module/poll.cioctl
: https://github.com/cirosantilli/linux-kernel-module-cheat/blob/6788a577c394a2fc512d8f3df0806d84dc09f355/kernel_module/poll.canon_inode_getfd
associates afile_operations
to a file descriptor without any filesystem file: https://stackoverflow.com/questions/4508998/what-is-anonymous-inode/44388030#44388030
Minimal runnable file_operations
example
Once you see a minimal example, it all becomes obvious.
The key ideas are:
file_operations
contains the callbacks for each file related syscallmknod path c major minor
creates a character device which uses thosefile_operations
- for character devices that dynamically allocate device numbers (the norm to avoid conflicts), find the number with
cat /proc/devices
character_device.ko
kernel module:
#include <asm/uaccess.h> /* copy_from_user, copy_to_user */
#include <linux/errno.h> /* EFAULT */
#include <linux/fs.h> /* register_chrdev, unregister_chrdev */
#include <linux/jiffies.h>
#include <linux/module.h>
#include <linux/printk.h> /* printk */
#include <uapi/linux/stat.h> /* S_IRUSR */
#define NAME "lkmc_character_device"
MODULE_LICENSE("GPL");
static int major;
static ssize_t read(struct file *filp, char __user *buf, size_t len, loff_t *off)
size_t ret;
char kbuf = 'a', 'b', 'c', 'd';
ret = 0;
if (*off == 0)
if (copy_to_user(buf, kbuf, sizeof(kbuf)))
ret = -EFAULT;
else
ret = sizeof(kbuf);
*off = 1;
return ret;
static const struct file_operations fops =
.owner = THIS_MODULE,
.read = read,
;
static int myinit(void)
major = register_chrdev(0, NAME, &fops);
return 0;
static void myexit(void)
unregister_chrdev(major, NAME);
module_init(myinit)
module_exit(myexit)
Userland test program:
insmod /character_device.ko
dev="lkmc_character_device"
major="$(grep "$dev" /proc/devices | cut -d ' ' -f 1)"
mknod "/dev/$dev" c "$major" 0
cat /dev/lkmc_character_device
# => abcd
rm /dev/lkmc_character_device
rmmod character_device
GitHub QEMU + Buildroot upstream with boilerplate to actually run it:
- https://github.com/cirosantilli/linux-kernel-module-cheat/blob/6788a577c394a2fc512d8f3df0806d84dc09f355/kernel_module/character_device.c
- https://github.com/cirosantilli/linux-kernel-module-cheat/blob/master/rootfs_overlay/character_device.sh
More complex examples:
read
,write
,lseek
with a fixed size internal buffer and on debugfs instead of a character device: https://github.com/cirosantilli/linux-kernel-module-cheat/blob/6788a577c394a2fc512d8f3df0806d84dc09f355/kernel_module/fops.cpoll
: https://github.com/cirosantilli/linux-kernel-module-cheat/blob/6788a577c394a2fc512d8f3df0806d84dc09f355/kernel_module/poll.cioctl
: https://github.com/cirosantilli/linux-kernel-module-cheat/blob/6788a577c394a2fc512d8f3df0806d84dc09f355/kernel_module/poll.canon_inode_getfd
associates afile_operations
to a file descriptor without any filesystem file: https://stackoverflow.com/questions/4508998/what-is-anonymous-inode/44388030#44388030
edited Jun 18 '17 at 8:38
answered Jun 18 '17 at 8:30
Ciro Santilli æ°çÂÂæ¹é ä¸Âå¿ å ÂÃ¥ÂÂäºÂ件 æ³Âè½®åÂÂ
4,14613936
4,14613936
add a comment |Â
add a comment |Â
up vote
3
down vote
"Character at a time" is a misnomer (as is the idea that character devices necessarily do not support seek and tell). In fact, "block at a time" (i.e. strictly record-oriented, such as a tape drive*) devices must be character devices. So is the idea that a character device must necessarily be unseekable - character device drivers define a full file_operations
structure which is free to define llseek or not according to whether the device supports the operation. The character devices most people think of as examples are null, urandom, TTY devices, sound card, mouse, etc... which are all unseekable because of the specifics of what those devices are, but /dev/vcs, /dev/fb0, and /dev/kmem are also character devices and they are all seekable.
As I mentioned, a character device driver defines a file_operations structure that has function pointers for all the operations someone might want to call on a file - seek, read, write, ioctl, etc - and these are each called once when the corresponding system call is executed with this device file open. And read and write can therefore do whatever it wants with its arguments - it can refuse to accept a write that's too large or only write what fits; it can read only the data corresponding to one record rather than the whole requested number of bytes.
So, what is a block device, then? Basically, block devices are disk drives. No other kind of device (except for virtual disk drives, like ramdisk and loopback) is a block device. They are integrated into the I/O request system, the filesystem layer, the buffer/cache system, and the virtual memory system in a way that character devices are not, even when you're accessing e.g. /dev/sda from a user process. Even the "raw devices" that page mentions as an exception are character devices.
*Some UNIX systems implemented what is now called "fixed-block mode" - which lets the kernel group and split I/O requests to fit the configured block boundaries in more or less the same manner as is done for disk drives - as a block device. A character device is needed for "variable-block mode", which preserves block boundaries from the user program as a single write(2) call writes one block and a single read(2) call returns one block. Since mode switching is implemented now as an ioctl rather than a separate device file, a character device is used. Variable-record tape drives are mostly "non-seekable" because seeking involves counting a number of records rather than a number of bytes, and the native seek operation is implemented as an ioctl.
add a comment |Â
up vote
3
down vote
"Character at a time" is a misnomer (as is the idea that character devices necessarily do not support seek and tell). In fact, "block at a time" (i.e. strictly record-oriented, such as a tape drive*) devices must be character devices. So is the idea that a character device must necessarily be unseekable - character device drivers define a full file_operations
structure which is free to define llseek or not according to whether the device supports the operation. The character devices most people think of as examples are null, urandom, TTY devices, sound card, mouse, etc... which are all unseekable because of the specifics of what those devices are, but /dev/vcs, /dev/fb0, and /dev/kmem are also character devices and they are all seekable.
As I mentioned, a character device driver defines a file_operations structure that has function pointers for all the operations someone might want to call on a file - seek, read, write, ioctl, etc - and these are each called once when the corresponding system call is executed with this device file open. And read and write can therefore do whatever it wants with its arguments - it can refuse to accept a write that's too large or only write what fits; it can read only the data corresponding to one record rather than the whole requested number of bytes.
So, what is a block device, then? Basically, block devices are disk drives. No other kind of device (except for virtual disk drives, like ramdisk and loopback) is a block device. They are integrated into the I/O request system, the filesystem layer, the buffer/cache system, and the virtual memory system in a way that character devices are not, even when you're accessing e.g. /dev/sda from a user process. Even the "raw devices" that page mentions as an exception are character devices.
*Some UNIX systems implemented what is now called "fixed-block mode" - which lets the kernel group and split I/O requests to fit the configured block boundaries in more or less the same manner as is done for disk drives - as a block device. A character device is needed for "variable-block mode", which preserves block boundaries from the user program as a single write(2) call writes one block and a single read(2) call returns one block. Since mode switching is implemented now as an ioctl rather than a separate device file, a character device is used. Variable-record tape drives are mostly "non-seekable" because seeking involves counting a number of records rather than a number of bytes, and the native seek operation is implemented as an ioctl.
add a comment |Â
up vote
3
down vote
up vote
3
down vote
"Character at a time" is a misnomer (as is the idea that character devices necessarily do not support seek and tell). In fact, "block at a time" (i.e. strictly record-oriented, such as a tape drive*) devices must be character devices. So is the idea that a character device must necessarily be unseekable - character device drivers define a full file_operations
structure which is free to define llseek or not according to whether the device supports the operation. The character devices most people think of as examples are null, urandom, TTY devices, sound card, mouse, etc... which are all unseekable because of the specifics of what those devices are, but /dev/vcs, /dev/fb0, and /dev/kmem are also character devices and they are all seekable.
As I mentioned, a character device driver defines a file_operations structure that has function pointers for all the operations someone might want to call on a file - seek, read, write, ioctl, etc - and these are each called once when the corresponding system call is executed with this device file open. And read and write can therefore do whatever it wants with its arguments - it can refuse to accept a write that's too large or only write what fits; it can read only the data corresponding to one record rather than the whole requested number of bytes.
So, what is a block device, then? Basically, block devices are disk drives. No other kind of device (except for virtual disk drives, like ramdisk and loopback) is a block device. They are integrated into the I/O request system, the filesystem layer, the buffer/cache system, and the virtual memory system in a way that character devices are not, even when you're accessing e.g. /dev/sda from a user process. Even the "raw devices" that page mentions as an exception are character devices.
*Some UNIX systems implemented what is now called "fixed-block mode" - which lets the kernel group and split I/O requests to fit the configured block boundaries in more or less the same manner as is done for disk drives - as a block device. A character device is needed for "variable-block mode", which preserves block boundaries from the user program as a single write(2) call writes one block and a single read(2) call returns one block. Since mode switching is implemented now as an ioctl rather than a separate device file, a character device is used. Variable-record tape drives are mostly "non-seekable" because seeking involves counting a number of records rather than a number of bytes, and the native seek operation is implemented as an ioctl.
"Character at a time" is a misnomer (as is the idea that character devices necessarily do not support seek and tell). In fact, "block at a time" (i.e. strictly record-oriented, such as a tape drive*) devices must be character devices. So is the idea that a character device must necessarily be unseekable - character device drivers define a full file_operations
structure which is free to define llseek or not according to whether the device supports the operation. The character devices most people think of as examples are null, urandom, TTY devices, sound card, mouse, etc... which are all unseekable because of the specifics of what those devices are, but /dev/vcs, /dev/fb0, and /dev/kmem are also character devices and they are all seekable.
As I mentioned, a character device driver defines a file_operations structure that has function pointers for all the operations someone might want to call on a file - seek, read, write, ioctl, etc - and these are each called once when the corresponding system call is executed with this device file open. And read and write can therefore do whatever it wants with its arguments - it can refuse to accept a write that's too large or only write what fits; it can read only the data corresponding to one record rather than the whole requested number of bytes.
So, what is a block device, then? Basically, block devices are disk drives. No other kind of device (except for virtual disk drives, like ramdisk and loopback) is a block device. They are integrated into the I/O request system, the filesystem layer, the buffer/cache system, and the virtual memory system in a way that character devices are not, even when you're accessing e.g. /dev/sda from a user process. Even the "raw devices" that page mentions as an exception are character devices.
*Some UNIX systems implemented what is now called "fixed-block mode" - which lets the kernel group and split I/O requests to fit the configured block boundaries in more or less the same manner as is done for disk drives - as a block device. A character device is needed for "variable-block mode", which preserves block boundaries from the user program as a single write(2) call writes one block and a single read(2) call returns one block. Since mode switching is implemented now as an ioctl rather than a separate device file, a character device is used. Variable-record tape drives are mostly "non-seekable" because seeking involves counting a number of records rather than a number of bytes, and the native seek operation is implemented as an ioctl.
answered Mar 30 '17 at 16:28
Random832
8,29012235
8,29012235
add a comment |Â
add a comment |Â
up vote
1
down vote
Character devices can be created by kernel modules (or the kernel itself). When a device is created, the creator provides pointers to functions that implement handle standard calls like open, read, etc. The Linux kernel then associates those functions with the character device, so for example when a user-mode application calls the read() function on a character device file, it will result in a syscall and then the kernel will route this call to a read function specified when creating the driver.
There's a step-by-step tutorial on creating a character device here, you can create a sample project and the step through it using a debugger to understand how the device object is created and when the handlers are invoked.
add a comment |Â
up vote
1
down vote
Character devices can be created by kernel modules (or the kernel itself). When a device is created, the creator provides pointers to functions that implement handle standard calls like open, read, etc. The Linux kernel then associates those functions with the character device, so for example when a user-mode application calls the read() function on a character device file, it will result in a syscall and then the kernel will route this call to a read function specified when creating the driver.
There's a step-by-step tutorial on creating a character device here, you can create a sample project and the step through it using a debugger to understand how the device object is created and when the handlers are invoked.
add a comment |Â
up vote
1
down vote
up vote
1
down vote
Character devices can be created by kernel modules (or the kernel itself). When a device is created, the creator provides pointers to functions that implement handle standard calls like open, read, etc. The Linux kernel then associates those functions with the character device, so for example when a user-mode application calls the read() function on a character device file, it will result in a syscall and then the kernel will route this call to a read function specified when creating the driver.
There's a step-by-step tutorial on creating a character device here, you can create a sample project and the step through it using a debugger to understand how the device object is created and when the handlers are invoked.
Character devices can be created by kernel modules (or the kernel itself). When a device is created, the creator provides pointers to functions that implement handle standard calls like open, read, etc. The Linux kernel then associates those functions with the character device, so for example when a user-mode application calls the read() function on a character device file, it will result in a syscall and then the kernel will route this call to a read function specified when creating the driver.
There's a step-by-step tutorial on creating a character device here, you can create a sample project and the step through it using a debugger to understand how the device object is created and when the handlers are invoked.
answered Feb 25 '14 at 2:06
bazis
111
111
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f37829%2fhow-do-character-device-or-character-special-files-work%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password