Coder Social home page Coder Social logo

linux-kernel-module's Introduction

linux-kernel-module

GuardRails badge

Simple examples of Linux Kernel Modules, written as a learning exercise.

Sample 1: Greeter

This sample simply takes a name as a parameter, and writes a greeting to the kernel log (/var/log/kern.log). cd into greeter/ before running any of the commands before.

Building the Module

make

The module is compiled to greeter.ko.

Installing / Uninstalling

# Install:
sudo insmod greeter.ko

# Install with parameters:
sudo insmod greeter.ko name=Frodo

# Uninstall
sudo rmmod greeter

Sample 2: Babel

babel is a character driver which adds a device called /dev/babel. This is a device you can 'talk' to. It'll babble back in gibberish.

Install with:

cd babel
make
sudo insmod babel.ko

Then run the test client, which'll let you chat with the /dev/babel device:

sudo ./babel-client

Uninstall with:

sudo rmmod babel

Make the /dev/babel device read/write accessible without super user priviledges by adding a rule. First get the Kernal and Subsystem name:

udevadm info -a -p /sys/class/babel/babel

# Will show something like:
#   KERNEL=="babel"
#   SUBSYSTEM=="babel"

Now create a low-priority rule to enable user access:

echo 'KERNEL=="babel", SUBSYSTEM=="babel", MODE="0666"' >> /etc/udev/rules.d/99-babel.rules

Reload the driver and it will be accessible without superuser rights.

Tips

Show loaded modules with:

lsmod

Show menuconfig with:

sudo make menuconfig

Show the kernel log with:

dmesg

Show info about the module:

modinfo greeter.ko

Note: if you get the following error:

insmod: ERROR: could not insert module greeter.ko: Invalid parameters

Then make sure you are not trying to install the module from a shared location (such as a shared folder on a virtual machine). Copy it to the home directory and install from there instead.

Trace module calls with:

sudo apt-get install strace
sudo strace ./babel/babel_client

strace will show low-level system calls in realtime as the program makes them.

Reading

The bulk of this code came from:

With the inspiration from the "Introduction to Linux Kernels" webinar from the Linux Foundation.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.