Linux Device Driver Tutorial Part 25 – Sending Signal from Linux Device Driver to User Space

This is the Series on Linux Device Driver. The aim of this series is to provide easy and practical examples that anyone can understand. This is the Linux Device Driver Tutorial Part 25 – Sending Signal from Linux Device Driver to User Space.

Prerequisites

In the example section, we explained signals using interrupt program. So I would recommend you to explore interrupts using below links before start this.

  1. Interrupts Concepts
  2. Interrupts Examples Program
  3. IOCTL Tutorial

Signals

Introduction

Generally A signal is a action which is intended to send a particular message. It can be sound, gesture, event etc. Below are the normal signals which we are using day to day life.

  • When we take money in ATM, we will get a message
  • Calling someone by making sound or gesture
  • Microwave oven making sound when it finishes its job
  • etc.

What about Linux? Signals are a way of sending simple messages which is used to notify a process or thread of a particular event. In Linux, there are many process will be running at a time. We can send signal from one process to another process. Signals are one of the oldest inter-process communication methods. This signals are asynchronous. Like User space signals, can we send signal to user space from kernel space? Yes why not. We will see the complete Signals in upcoming tutorials. In this tutorial we will learn how to send signal from Linux Device Driver to User Space.

Sending Signal from Linux Device Driver to User Space

Using the following steps easily we can send the signals.

  1. Decide the signal that you want to send.
  2. Register the user space application with driver.
  3. Once something happened (in our example we used interrupts) send signals to user space.
  4. Unregister the user space application when you done.

Decide the signal that you want to send

First select the signal number which you want to send. In our case we are going to send signal 44.

Example:

Register the user space application with driver

Before sending signal, your device driver should know to whom it needs to send the signal. For that we need to register the process to driver. So we need to send the PID to driver first. Then that driver will use the PID and sends the signal. You can register the application PID in any ways like IOCTL, Open/read/write call. In our example we are going to register using IOCTL.

Example:

Send signals to user space

After registering the application to driver, then driver can able to send the signal when it requires. In our example, we will send the signal when we get the interrupt.

Example:

Unregister the user space application

When you done with your task, you can unregister your application. Here we are unregistering when that application closes the driver.

Example:

Device Driver Source Code

The complete device driver code is given below. In this source code, When we read the /dev/etx_device interrupt will hit (To understand interrupts in Linux go to this tutorial). Whenever interrupt hits, I’m sending signal to user space application who registered already. Since it is a tutorial post, I’m not going to do any job in interrupt handler except sending signal.

driver.c

Makefile:

Application Source Code

This application register with the driver using IOCTL. Once it registered, it will be waiting for the signal from the driver. If we want to close this application we need to press CTRL+C. Because we it will run infinitely. We have installed CTRL+C signal handler.

test_app.c

Building Driver and Application

  • Build the driver by using Makefile (sudo make)
  • Use below line in terminal to compile the user space application.

gcc -o test_app test_app.c

Execution (Output)

As of now, we have driver.ko and test_app. Now we will see the output.

  • Load the driver using sudo insmod driver.ko
  • Run the application (sudo ./test_app)

*********************************
*******WWW.EmbeTronicX.com*******
*********************************
Installed signal handler for SIGETX = 44

Opening Driver
Registering application …Done!!!
Waiting for signal…

  • This application will be waiting for signal
  • To send the signal from driver to app, we need to trigger the interrupt by reading the driver (sudo cat /dev/etx_device).
  • Now see the Dmesg (dmesg)

Major = 246 Minor = 0
Device Driver Insert…Done!!!
Device File Opened…!!!
REG_CURRENT_TASK
Device File Opened…!!!
Read Function
Shared IRQ: Interrupt Occurred
Sending signal to app
Device File Closed…!!!

  • As per the print, driver has send the signal. Now check the app.

Received signal from kernel : Value = 1

  • So application also got the signal.
  • Close the application by pressing CTRL+C
  • Unload the module using sudo rmmod driver
%d bloggers like this: