Skip to content

HattoriMan/LinuxDeviceDriver

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Linux Character Device Driver - OS Assignment

Overview

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

Features

  • 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

Requirements

Install required packages:

sudo apt-get install build-essential linux-headers-$(uname -r)

Build Instructions

make
  • builds the driver.ko module

Check Kernel Version

uname -r

Example:

6.8.0-106-generic → use 6,8

Load Module

sudo insmod driver.ko kernel_version=6,8 time=30

Parameters:

  • kernel_version → kernel major and minor version
  • time → timeout in seconds

Check Kernel Logs

dmesg | tail -10

You should see:

  • Major number
  • Minor number
  • Timer value

Create Device File

sudo mknod /dev/my_device c 240 0
sudo chmod 666 /dev/my_device

Explanation:

  • mknod creates the device file using major and minor numbers
  • chmod 666 allows all users to read and write

Execution (Correct Order)

cat /dev/my_device
whoami > /dev/my_device

Explanation:

  • cat triggers the read() function
  • whoami > triggers the write() function

Important:

-If write is executed before read, it will block -It will resume only after read or timeout


Remove Module

sudo rmmod driver
dmesg | tail -10

Expected Results

SUCCESS

  • Successfully completed the actions within time. Username:

FAILURE

  • FAILURE: Conditions not met

Test Cases

  1. Success Case
cat /dev/my_device
whoami > /dev/my_device
  • Read → Write within time → SUCCESS
  1. Wrong Order
whoami > /dev/my_device
cat /dev/my_device
  • Write blocks → FAIL
  1. Timeout Case
cat /dev/my_device
sleep 35
whoami > /dev/my_device
  • Write after timeout → FAIL
  1. Only Read
cat /dev/my_device
  • No write → FAIL
  1. Only Write
whoami > /dev/my_device
  • Blocks until timeout → FAIL
  1. Version Mismatch
sudo insmod driver.ko kernel_version=5,4 time=30
  • Module not inserted

Technical Details

Waitqueue

  • Used to enforce ordering:
    • wait_event_interruptible() blocks write
    • wake_up() resumes execution after read or timeout

Timer

  • Implemented using timer_list
  • Uses jiffies to track timeout
  • On expiry:
    • Sets time_up = 1
    • Wakes blocked processes

Memory Transfer

  • copy_to_user() → kernel → user
  • copy_from_user() → user → kernel

Debugging

dmesg -w              # Live logs
dmesg -c              # Clear logs
lsmod | grep driver   # Check module loaded

Notes

  • Device file is created manually using mknod
  • Write operation blocks until read or timeout
  • Ensure correct kernel version is passed during insertion

Author

Manav Sharma

About

This is a working implementation of a linux device driver. It involves a timer and a waitqueue. It is supposed to complete a read, write operation in sequence with a blocking call implemented via the waitqueue in the specified time(in seconds) provided during insertion of the module.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors