This project implements a Linux kernel character device driver that enforces ordered execution of operations within a time limit.
The driver ensures that:
- The kernel version matches before loading
- A READ operation is performed before WRITE
- Both operations occur within a specified time
- The result is validated when the module is removed
- Kernel version validation using
LINUX_VERSION_CODE - Dynamic character device allocation
- Waitqueue-based synchronization (read before write)
- Timer-based timeout enforcement
- Safe user-kernel communication using copy APIs
- Success/failure validation during module removal
Install required packages:
sudo apt-get install build-essential linux-headers-$(uname -r)make
- builds the
driver.komodule
uname -r
6.8.0-106-generic → use 6,8
sudo insmod driver.ko kernel_version=6,8 time=30
kernel_version→ kernel major and minor versiontime→ timeout in seconds
dmesg | tail -10
You should see:
- Major number
- Minor number
- Timer value
sudo mknod /dev/my_device c 240 0
sudo chmod 666 /dev/my_device
mknodcreates the device file using major and minor numberschmod 666allows all users to read and write
cat /dev/my_device
whoami > /dev/my_device
cattriggers the read() functionwhoami >triggers the write() function
-If write is executed before read, it will block -It will resume only after read or timeout
sudo rmmod driver
dmesg | tail -10
- Successfully completed the actions within time. Username:
- FAILURE: Conditions not met
- Success Case
cat /dev/my_device
whoami > /dev/my_device
- Read → Write within time → SUCCESS
- Wrong Order
whoami > /dev/my_device
cat /dev/my_device
- Write blocks → FAIL
- Timeout Case
cat /dev/my_device
sleep 35
whoami > /dev/my_device
- Write after timeout → FAIL
- Only Read
cat /dev/my_device
- No write → FAIL
- Only Write
whoami > /dev/my_device
- Blocks until timeout → FAIL
- Version Mismatch
sudo insmod driver.ko kernel_version=5,4 time=30
- Module not inserted
- Used to enforce ordering:
wait_event_interruptible()blocks writewake_up()resumes execution after read or timeout
- Implemented using
timer_list - Uses
jiffiesto track timeout - On expiry:
- Sets
time_up = 1 - Wakes blocked processes
- Sets
copy_to_user()→ kernel → usercopy_from_user()→ user → kernel
dmesg -w # Live logs
dmesg -c # Clear logs
lsmod | grep driver # Check module loaded
- Device file is created manually using
mknod - Write operation blocks until read or timeout
- Ensure correct kernel version is passed during insertion
Manav Sharma