From 59960b808cdf4d468112b8e116b5c2fa94627c92 Mon Sep 17 00:00:00 2001 From: Ciro Santilli Date: Fri, 6 Jul 2018 09:01:49 +0100 Subject: [PATCH] Move characte devices next to pseudo filesystems --- README.adoc | 155 ++++++++++++++++++++++++++-------------------------- 1 file changed, 78 insertions(+), 77 deletions(-) diff --git a/README.adoc b/README.adoc index 8bd5274..16a059a 100644 --- a/README.adoc +++ b/README.adoc @@ -3610,6 +3610,84 @@ Bibliography: * http://www.makelinux.net/ldd3/chp-14-sect-1 * https://www.win.tue.nl/~aeb/linux/lk/lk-13.html +==== Character devices + +Character devices can have arbitrary <> associated to them: + +.... +/character_device.sh +echo $? +.... + +Outcome: the test passes: + +.... +0 +.... + +Sources: + +* link:rootfs_overlay/character_device.sh[] +* link:rootfs_overlay/mknoddev.sh[] +* link:kernel_module/character_device.c[] + +Unlike <> entires, character device files are created with userland `mknod` or `mknodat` syscalls: + +.... +mknod c +.... + +Intuitively, for physical devices like keyboards, the major number maps to which driver, and the minor number maps to which device it is. + +A single driver can drive multiple compatible devices. + +The major and minor numbers can be observed with: + +.... +ls -l /dev/urandom +.... + +Output: + +.... +crw-rw-rw- 1 root root 1, 9 Jun 29 05:45 /dev/urandom +.... + +which means: + +* `c` (first letter): this is a character device. Would be `b` for a block device. +* `1, 9`: the major number is `1`, and the minor `9` + +To avoid device number conflicts when registering the driver we: + +* ask the kernel to allocate a free major number for us with: `register_chrdev(0` +* find ouf which number was assigned by grepping `/proc/devices` for the kernel module name + +Bibliography: https://unix.stackexchange.com/questions/37829/understanding-character-device-or-character-special-files/371758#371758 + +===== Automatically create character device file on insmod + +And also destroy it on `rmmod`: + +.... +/character_device_create.sh +echo $? +.... + +Outcome: the test passes: + +.... +0 +.... + +Sources: + +* link:kernel_module/character_device_create.c[] +* link:rootfs_overlay/character_device_create.sh[] + +Bibliography: https://stackoverflow.com/questions/5970595/how-to-create-a-device-node-from-the-init-module-code-of-a-linux-kernel-module/45531867#45531867 + + === Pseudo files ==== File operations @@ -3815,83 +3893,6 @@ Bibliography: * https://github.com/simonjhall/dma * https://github.com/ikwzm/udmabuf -==== Character devices - -Character devices can have arbitrary <> associated to them: - -.... -/character_device.sh -echo $? -.... - -Outcome: the test passes: - -.... -0 -.... - -Sources: - -* link:rootfs_overlay/character_device.sh[] -* link:rootfs_overlay/mknoddev.sh[] -* link:kernel_module/character_device.c[] - -Unlike <> entires, character device files are created with userland `mknod` or `mknodat` syscalls: - -.... -mknod c -.... - -Intuitively, for physical devices like keyboards, the major number maps to which driver, and the minor number maps to which device it is. - -A single driver can drive multiple compatible devices. - -The major and minor numbers can be observed with: - -.... -ls -l /dev/urandom -.... - -Output: - -.... -crw-rw-rw- 1 root root 1, 9 Jun 29 05:45 /dev/urandom -.... - -which means: - -* `c` (first letter): this is a character device. Would be `b` for a block device. -* `1, 9`: the major number is `1`, and the minor `9` - -To avoid device number conflicts when registering the driver we: - -* ask the kernel to allocate a free major number for us with: `register_chrdev(0` -* find ouf which number was assigned by grepping `/proc/devices` for the kernel module name - -Bibliography: https://unix.stackexchange.com/questions/37829/understanding-character-device-or-character-special-files/371758#371758 - -===== Automatically create character device file on insmod - -And also destroy it on `rmmod`: - -.... -/character_device_create.sh -echo $? -.... - -Outcome: the test passes: - -.... -0 -.... - -Sources: - -* link:kernel_module/character_device_create.c[] -* link:rootfs_overlay/character_device_create.sh[] - -Bibliography: https://stackoverflow.com/questions/5970595/how-to-create-a-device-node-from-the-init-module-code-of-a-linux-kernel-module/45531867#45531867 - ==== Anonymous inode Anonymous inodes allow getting multiple file descriptors from a single filesystem entry, which reduces namespace pollution compared to creating multiple device files: