https://github.com/bbzaffari/messaging-kernel-module
This project implements a Messaging character device driver between user-space processes, using circular queues managed in kernel space. Includes a C client for interaction and testing. Built for OS class, exploring driver design, memory safety, and concurrent access handling.
https://github.com/bbzaffari/messaging-kernel-module
buildroot c kconfig kernel kernel-driver kernel-module linux-kernel qemu rootfs
Last synced: 6 months ago
JSON representation
This project implements a Messaging character device driver between user-space processes, using circular queues managed in kernel space. Includes a C client for interaction and testing. Built for OS class, exploring driver design, memory safety, and concurrent access handling.
- Host: GitHub
- URL: https://github.com/bbzaffari/messaging-kernel-module
- Owner: bbzaffari
- License: gpl-2.0
- Created: 2025-05-17T13:36:37.000Z (8 months ago)
- Default Branch: main
- Last Pushed: 2025-07-29T21:07:30.000Z (6 months ago)
- Last Synced: 2025-07-29T23:49:51.062Z (6 months ago)
- Topics: buildroot, c, kconfig, kernel, kernel-driver, kernel-module, linux-kernel, qemu, rootfs
- Language: C
- Homepage:
- Size: 114 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Messaging Kernel Module
This project implements a character device driver that enables inter-process messaging via the Linux kernel. Using circular queues managed in kernel space.
It was developed as part of the Operating Systems course (Prof. Sérgio Johann Filho) and cross-compiled with **Buildroot** for use on an embedded Linux environment.
## Overview
The module exposes a character device `/dev/mq` that allows user-space applications to:
* **Register** and **unregister** a process under a name.
* **Send messages** to specific registered processes.
* **Broadcast messages** to all registered processes.
* **Read messages** from the process's own queue.
A simple C client (`client.c`) is provided to interact with the device and test its behavior.
## How It Works
### Data Structures
In kernel space, the module maintains:
* A **control block list**, which holds:
* The process **PID**.
* An assigned **name** (up to 8 chars).
* A **message queue** (circular buffer).
* A spinlock for concurrent access.
Each message queue holds up to `QUEUE_LEN` messages, storing:
* The message **data** (dynamically allocated).
* The **sender's name**.
* The message **size**.
### Main Operations
* `/reg ` — Registers the calling process's PID and name.
* `/unr` — Unregisters the calling PID and cleans up its message queue.
* `/msg ` — Sends a message to the named destination, if registered.
* `/all ` — Sends the same message to all other registered processes.
* `/read` — Reads (and removes) the next message in the calling process's queue.
### Parameters
The module can be configured at load time with:
* `MAX_DEVICES`: maximum number of registered processes.
* `QUEUE_LEN`: number of messages per process queue.
* `CMD_BUF_SIZE`: maximum command size.
Example:
```bash
modprobe mq_driver MAX_DEVICES=8 QUEUE_LEN=8 CMD_BUF_SIZE=256
```
## Notes on Implementation
### Use of PID
**Disclaimer:** In a more formal or production-grade Linux driver, one would typically associate user-space interactions via file descriptors, file pointers (`struct file`), or inode references to track context.
In this project, for simplicity and as an educational abstraction, the process PID is used as the unique identifier. This avoids managing file structures and focuses on learning kernel data structures and synchronization.
This choice makes it easier to reason about process-specific state but limits interactions to one registration per PID and assumes stable PID handling.
### Memory Management
* Memory is dynamically allocated using `kmalloc`/`kfree`.
* Message queues are properly cleaned up on process unregistration.
* Circular buffer logic ensures that if a queue overflows, the oldest message is overwritten.
* Spinlocks protect both the control block list and per-process queues against race conditions.
### Build and Deployment
The system was cross-compiled using **Buildroot** to generate a minimal Linux environment for testing on embedded hardware.
Scripts are included to:
* Build the kernel module.
* Install and load it (`insmod`/`rmmod`).
* Compile the client application.
## Repository Structure
```
.
├── client.c # User-space C client for testing
├── mq_driver.c # Kernel module source
├── Makefile # Build rules
├── README.md # This documentation
└── tp2.pdf # Project description and assignment details
```
## How to build it with buildroot
...
## How to run it with QEMU
...
## Final Remarks
This project is an educational exercise aimed at deepening understanding of:
* Kernel-space vs user-space boundaries.
* Character device drivers.
* Process communication.
* Memory and concurrency management at kernel level.