Talk to the device
This runs in your browser. It follows the same rules as kernel/chardev.c: the same limits, the same error codes and the same log messages.
What the mutex prevents
Three threads call write() at the same moment. Step through it with the lock, then without it, and watch the first 48 bytes of the buffer.
Interface
System calls
| Call | When | Returns |
|---|---|---|
write | 1–256 bytes, room in buffer | n |
write | 0 bytes | EINVAL |
write | more than 256 bytes | EMSGSIZE |
write | buffer full | ENOSPC |
read | message queued, buffer big enough | length |
read | queue empty | 0 (EOF) |
read | buffer smaller than message (message stays queued) | EMSGSIZE |
read / write | bad user pointer (queue unchanged) | EFAULT |
ioctl commands
| Command | Encoding | Value |
|---|---|---|
CHARDEV_IOC_GET_COUNT | _IOR('k', 1, __u32) | 0x80046b01 |
CHARDEV_IOC_CLEAR | _IO('k', 2) | 0x00006b02 |
| anything else | ENOTTY |
The value packs direction (read = 2, top bits), argument size (4), the magic byte 'k' (0x6b) and the command number.
Where the code runs
What happens at insmod
kmallocthe 4096-byte bufferalloc_chrdev_regionpicks a free major numbercdev_addconnects that number to the file operationsclass_create+device_create; udev creates/dev/chardevproc_createadds/proc/chardev_stats
rmmod undoes these in reverse. If a step fails during load, only the steps before it are undone.
What the test suite checks
user/chardev_test.c prints PASS or FAIL for each of these, then scripts/run_tests.sh runs it again under valgrind and prints the kernel log.
cat stops cleanly.close() return value is checked.# build, load, test, valgrind, dmesg, unload
sudo apt install build-essential linux-headers-$(uname -r) valgrind
sudo scripts/run_tests.sh