@@ -3610,6 +3610,84 @@ Bibliography:
3610
3610
* http://www.makelinux.net/ldd3/chp-14-sect-1
3611
3611
* https://www.win.tue.nl/~aeb/linux/lk/lk-13.html
3612
3612
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
+
3613
3691
=== Pseudo files
3614
3692
3615
3693
==== File operations
@@ -3815,83 +3893,6 @@ Bibliography:
3815
3893
* https://github.com/simonjhall/dma
3816
3894
* https://github.com/ikwzm/udmabuf
3817
3895
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
-
3895
3896
==== Anonymous inode
3896
3897
3897
3898
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