Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sending signal from kernel to user space [closed]

How to get signal from kernel space to user space?

like image 655
mohwastz Avatar asked Apr 12 '11 11:04

mohwastz


People also ask

Can we send a signal to userspace from kernel space?

These signals are asynchronous. Like User space signals, can we send a signal to userspace from kernel space? Yes, why not. We will see the complete Signals in upcoming tutorials. In this tutorial, we will learn how to send a signal from Linux Device Driver to User Space. Using the following steps easily we can send the signals.

How to listen to a signal in kernel module?

To achieve this, add below code in kernel driver: In user space application, set its pid to kernel module, then listen on the signal.

How to send signals to userspace using a driver?

Using the following steps easily we can send the signals. Decide the signal that you want to send. Register the user space application with the driver. Once something happened (in our example we used interrupts) send signals to userspace. Unregister the user space application when you have done with it.

How to send signals to userspace using interrupts?

Once something happened (in our example we used interrupts) send signals to userspace. Unregister the user space application when you have done with it. First, select the signal number which you want to send. In our case, we are going to send signal 44. Before sending the signal, your device driver should know to whom it needs to send the signal.


2 Answers

To get the signal from kernel to user space use the following code in your user space and kernel space code as below :

user space application :

signal(SIGIO, &signal_handler_func); 
fcntl(fd, F_SETOWN, getpid());
oflags = fcntl(fd, F_GETFL);
fcntl(fd, F_SETFL, oflags | FASYNC);

define signal_handler_func function :

void signal_handler_func (int sig)
{

//handle the action corresponding to the signal here
}

kernel Space Module :

int ret = 0;
struct siginfo info;
memset(&info, 0, sizeof(struct siginfo));
info.si_signo = SIG_TEST;
info.si_code = SI_QUEUE;
info.si_int = 1234;  

send_sig_info(SIG_TEST, &info, t);//send signal to user land 

t is the PID of the user application.

like image 51
Raulp Avatar answered Nov 30 '22 23:11

Raulp


Use the kernel API function kill_proc_info(int sig, struct siginfo *info, pid_t pid)

NOTE This is actually a bad answer. The functions does send a signal to user space but the right way to do this, as the asker intended is to use the fasync character device method as documented here: http://www.xml.com/ldd/chapter/book/ch05.html#t4

like image 33
gby Avatar answered Nov 30 '22 23:11

gby