Skip to content

Commit 59960b8

Browse files
committed
Move characte devices next to pseudo filesystems
1 parent e05eb91 commit 59960b8

File tree

1 file changed

+78
-77
lines changed

1 file changed

+78
-77
lines changed

README.adoc

Lines changed: 78 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -3610,6 +3610,84 @@ Bibliography:
36103610
* http://www.makelinux.net/ldd3/chp-14-sect-1
36113611
* https://www.win.tue.nl/~aeb/linux/lk/lk-13.html
36123612

3613+
==== Character devices
3614+
3615+
Character devices can have arbitrary <<file-operations>> associated to them:
3616+
3617+
....
3618+
/character_device.sh
3619+
echo $?
3620+
....
3621+
3622+
Outcome: the test passes:
3623+
3624+
....
3625+
0
3626+
....
3627+
3628+
Sources:
3629+
3630+
* link:rootfs_overlay/character_device.sh[]
3631+
* link:rootfs_overlay/mknoddev.sh[]
3632+
* link:kernel_module/character_device.c[]
3633+
3634+
Unlike <<procfs>> entires, character device files are created with userland `mknod` or `mknodat` syscalls:
3635+
3636+
....
3637+
mknod </dev/path_to_dev> c <major> <minor>
3638+
....
3639+
3640+
Intuitively, for physical devices like keyboards, the major number maps to which driver, and the minor number maps to which device it is.
3641+
3642+
A single driver can drive multiple compatible devices.
3643+
3644+
The major and minor numbers can be observed with:
3645+
3646+
....
3647+
ls -l /dev/urandom
3648+
....
3649+
3650+
Output:
3651+
3652+
....
3653+
crw-rw-rw- 1 root root 1, 9 Jun 29 05:45 /dev/urandom
3654+
....
3655+
3656+
which means:
3657+
3658+
* `c` (first letter): this is a character device. Would be `b` for a block device.
3659+
* `1, 9`: the major number is `1`, and the minor `9`
3660+
3661+
To avoid device number conflicts when registering the driver we:
3662+
3663+
* ask the kernel to allocate a free major number for us with: `register_chrdev(0`
3664+
* find ouf which number was assigned by grepping `/proc/devices` for the kernel module name
3665+
3666+
Bibliography: https://unix.stackexchange.com/questions/37829/understanding-character-device-or-character-special-files/371758#371758
3667+
3668+
===== Automatically create character device file on insmod
3669+
3670+
And also destroy it on `rmmod`:
3671+
3672+
....
3673+
/character_device_create.sh
3674+
echo $?
3675+
....
3676+
3677+
Outcome: the test passes:
3678+
3679+
....
3680+
0
3681+
....
3682+
3683+
Sources:
3684+
3685+
* link:kernel_module/character_device_create.c[]
3686+
* link:rootfs_overlay/character_device_create.sh[]
3687+
3688+
Bibliography: https://stackoverflow.com/questions/5970595/how-to-create-a-device-node-from-the-init-module-code-of-a-linux-kernel-module/45531867#45531867
3689+
3690+
36133691
=== Pseudo files
36143692

36153693
==== File operations
@@ -3815,83 +3893,6 @@ Bibliography:
38153893
* https://github.com/simonjhall/dma
38163894
* https://github.com/ikwzm/udmabuf
38173895

3818-
==== Character devices
3819-
3820-
Character devices can have arbitrary <<file-operations>> associated to them:
3821-
3822-
....
3823-
/character_device.sh
3824-
echo $?
3825-
....
3826-
3827-
Outcome: the test passes:
3828-
3829-
....
3830-
0
3831-
....
3832-
3833-
Sources:
3834-
3835-
* link:rootfs_overlay/character_device.sh[]
3836-
* link:rootfs_overlay/mknoddev.sh[]
3837-
* link:kernel_module/character_device.c[]
3838-
3839-
Unlike <<procfs>> entires, character device files are created with userland `mknod` or `mknodat` syscalls:
3840-
3841-
....
3842-
mknod </dev/path_to_dev> c <major> <minor>
3843-
....
3844-
3845-
Intuitively, for physical devices like keyboards, the major number maps to which driver, and the minor number maps to which device it is.
3846-
3847-
A single driver can drive multiple compatible devices.
3848-
3849-
The major and minor numbers can be observed with:
3850-
3851-
....
3852-
ls -l /dev/urandom
3853-
....
3854-
3855-
Output:
3856-
3857-
....
3858-
crw-rw-rw- 1 root root 1, 9 Jun 29 05:45 /dev/urandom
3859-
....
3860-
3861-
which means:
3862-
3863-
* `c` (first letter): this is a character device. Would be `b` for a block device.
3864-
* `1, 9`: the major number is `1`, and the minor `9`
3865-
3866-
To avoid device number conflicts when registering the driver we:
3867-
3868-
* ask the kernel to allocate a free major number for us with: `register_chrdev(0`
3869-
* find ouf which number was assigned by grepping `/proc/devices` for the kernel module name
3870-
3871-
Bibliography: https://unix.stackexchange.com/questions/37829/understanding-character-device-or-character-special-files/371758#371758
3872-
3873-
===== Automatically create character device file on insmod
3874-
3875-
And also destroy it on `rmmod`:
3876-
3877-
....
3878-
/character_device_create.sh
3879-
echo $?
3880-
....
3881-
3882-
Outcome: the test passes:
3883-
3884-
....
3885-
0
3886-
....
3887-
3888-
Sources:
3889-
3890-
* link:kernel_module/character_device_create.c[]
3891-
* link:rootfs_overlay/character_device_create.sh[]
3892-
3893-
Bibliography: https://stackoverflow.com/questions/5970595/how-to-create-a-device-node-from-the-init-module-code-of-a-linux-kernel-module/45531867#45531867
3894-
38953896
==== Anonymous inode
38963897

38973898
Anonymous inodes allow getting multiple file descriptors from a single filesystem entry, which reduces namespace pollution compared to creating multiple device files:

0 commit comments

Comments
 (0)