hrushikeshj / lkm-reducer

A char device which sums up the numbers written to it

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Reducer: Simple Loadable Linux Kernel Module

Reducer is an LKM that registers a char device and sums up all the numbers written to it. This is a very simple module, created to learn about char devices and Kernal Modules.

Usage

Once the module is loaded, a character device file named /dev/reducer has be created(see Installation steps)

Write numbers

echo 15 > /dev/reducer
echo 6 > /dev/reducer

Reading the Sum

cat /dev/reducer
# 21

Multiple numbers can also be written together

echo "4 82 2" > /dev/reducer
echo "5" > /dev/reducer
echo "8" > /dev/reducer

cat /dev/reducer
101

Using Python/any other language

f=open('/dev/reducer', 'w') # open in write mode
f.write('10 5')
f.close()

f=open('/dev/reducer', 'r') # open in read mode
sum_r = f.read()
print(sum_r) # o/p: 15

f.close()

Installation

  1. Clone the Repository:
git clone https://github.com/hrushikeshj/lkm-reducer.git
cd lkm-reducer
  1. Build the Module: It's recommended to have your self-compiled kernel.
make
  1. Load the Kernel Module
sudo insmod reducer.ko
  1. Create device file

Get the device number through kernel log(sudo dmesg) or /proc/devices. The char device need not only be in /dev, but by convention, it is placed there.

dev_no=$(cat /proc/devices | grep reducer | awk '{print $1;}')
# dev_no eg, 238
sudo mknod /dev/reducer c $dev_no 0

Note:

Steps 3 and 4, i.e, to load the module and create the device file can be easily done using the install_lmk.sh script.

chmod +x install_lmk.sh
sudo ./install_lmk.sh ins reducer.ko reducer

To Delete

sudo ./install_lmk.sh del reducer.ko reducer

Reference

About

A char device which sums up the numbers written to it


Languages

Language:C 90.7%Language:Shell 6.6%Language:Makefile 2.8%