A fully functional Linux kernel character device driver simulation with circular buffering, sequence tracking, and user-space interaction.
sudo apt update
sudo apt install build-essential linux-headers-$(uname -r) python3# Build kernel module
make
# Load module
sudo insmod src/simulated_chardev.ko
# Check major number
dmesg | tail
# Create device node (replace 250 with actual major)
sudo mknod /dev/simchardev c 250 0
sudo chmod 666 /dev/simchardevpython3 user/test_client.pyflowchart TD
A[User Space] <--> B[/dev/simchardev]
B <--> C[Kernel Space Driver]
subgraph C [Driver Components]
D[Circular Buffer]
E[Mutex Lock]
F[Sequence Counter]
G[Message Storage]
end
subgraph A [User Operations]
H[write: Store data]
I[read: Retrieve data]
J[ioctl: Control/Status]
K[open/close: Access]
end
H --> D
I --> D
J --> F
K --> E
- Dynamic major number allocation
- Circular buffer with configurable size (128 entries)
- Thread-safe operations with mutex locks
- Message sequencing with auto-increment
- CRC validation for data integrity
- IOCTL commands:
RESET_BUFFER,GET_STATUS - Standard file operations:
open,read,write,release
- Python test client with interactive menu
- Comprehensive error handling
- Support for all driver operations
- Human-readable output formatting
# Standard build
make
# Clean build artifacts
make clean# Load module
sudo insmod src/simulated_chardev.ko
# Verify loading (check major number)
dmesg | tail -5
# List loaded modules
lsmod | grep simulated
# Remove module
sudo rmmod simulated_chardev
# Clean up device file
sudo rm /dev/simchardev# Write to device
echo "Test message" | sudo tee /dev/simchardev
# Read from device
sudo cat /dev/simchardev
# Direct IOCTL calls
python3 user/test_client.pyThe test client provides an interactive interface:
$ python3 user/test_client.py
=================================
Simulated Char Device Test Client
=================================
1. Write message
2. Read messages
3. Reset buffer (IOCTL)
4. Get driver status (IOCTL)
5. Run full test sequence
6. Exit
Choose option: # Write multiple messages
> 1
Enter message: Hello Driver!
Message written successfully (Seq: 1)
> 1
Enter message: Second message
Message written successfully (Seq: 2)
# Read back messages
> 2
--- Messages in buffer ---
Seq 1: Hello Driver! (CRC: 0x...)
Seq 2: Second message (CRC: 0x...)
# Check driver status
> 4
Driver Status:
- Total messages: 2
- Buffer capacity: 128
- Buffer usage: 2/128
- Last sequence: 2| Command | Code | Description |
|---|---|---|
RESET_BUFFER |
0x4001 | Clear all messages, reset sequence |
GET_STATUS |
0x4002 | Get buffer usage and statistics |
# Reset buffer
python3 -c "
import fcntl
fd = open('/dev/simchardev', 'r')
fcntl.ioctl(fd, 0x4001)
print('Buffer reset')
"
# Get status
python3 -c "
import fcntl, struct
fd = open('/dev/simchardev', 'r')
status = bytearray(16)
fcntl.ioctl(fd, 0x4002, status)
total, capacity, used, seq = struct.unpack('IIII', status)
print(f'Status: {used}/{capacity} messages, seq:{seq}')
"-
Module fails to load:
# Check kernel headers uname -r sudo apt install linux-headers-$(uname -r) # Check make output make clean && make
-
Permission denied on /dev/simchardev:
sudo chmod 666 /dev/simchardev # OR add your user to appropriate group sudo usermod -aG dialout $USER
-
Device busy when removing:
# Check what's using it sudo lsof /dev/simchardev # Force close if needed sudo fuser -k /dev/simchardev sudo rmmod simulated_chardev
Check kernel logs for driver messages:
# Tail kernel messages
dmesg -w
# Filter driver messages
dmesg | grep simulatedThis project demonstrates:
- Linux character device driver architecture
- User-kernel space communication
- Circular buffer implementation
- Concurrency control with mutexes
- IOCTL interface design
- Kernel module development workflow
# Complete workflow
make
sudo insmod src/simulated_chardev.ko
MAJOR=$(dmesg | grep "major" | tail -1 | awk '{print $NF}')
sudo mknod /dev/simchardev c $MAJOR 0
sudo chmod 666 /dev/simchardev
python3 user/test_client.py
sudo rmmod simulated_chardev
sudo rm /dev/simchardev