Sending signal from kernel to user space

12,081

Solution 1

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.

Solution 2

There is something called a NetLink interface which provides a set of API's for communication between a kernel process and a user process.It is similar to the socket interface and the communication is asynchronous and hence is preferred to IOCTL.

Overview here: http://www.linuxjournal.com/article/7356

Solution 3

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

Share:
12,081
mohwastz
Author by

mohwastz

I'm student of device driver.

Updated on June 20, 2022

Comments

  • mohwastz
    mohwastz almost 2 years

    How to get signal from kernel space to user space?

  • mohwastz
    mohwastz about 13 years
    #include<linux/module.h> #include<linux/kernel.h> #include <linux/signal.h> int init_module() { // int i,res=0; // char msg1[80]; struct siginfo info; printk(KERN_ALERT"\n HELLO WORLD!!"); info.si_signo = 14; // GIVE YOU SIGNAL NUMBER HERE!!!!! info.si_errno = 0; info.si_code = SI_USER; info.si_pid = current->tgid; info.si_uid = current->uid; kill_proc_info(14, &info, 553); return 0; }void cleanup_module(){printk(KERN_ALERT"\n Good bye world!!");}MODULE_LICENSE("GPL");
  • mohwastz
    mohwastz about 13 years
    sir tell me what is the header file?
  • mohwastz
    mohwastz about 13 years
    header file for kernel API function kill_proc_info(int sig, struct siginfo *info, pid_t pid)
  • mohwastz
    mohwastz about 13 years
    while executing my program I got following error
  • mohwastz
    mohwastz about 13 years
    /home/wasimtabrez/Work/int/signal2.c: In function ‘init_module’: /home/wasimtabrez/Work/int/signal2.c:24: error: dereferencing pointer to incomplete type /home/wasimtabrez/Work/int/signal2.c:25: error: dereferencing pointer to incomplete type /home/wasimtabrez/Work/int/signal2.c:27: error: implicit declaration of function ‘kill_proc_info’ make[2]: *** [/home/wasimtabrez/Work/int/signal2.o] Error 1 make[1]: *** [_module_/home/wasimtabrez/Work/int] Error 2 make[1]: Leaving directory `/usr/src/linux-headers-2.6.35-28-generic' make: *** [all] Error 2
  • duslabo
    duslabo over 11 years
  • dexterous
    dexterous about 10 years
    Can you please provide the complete example?
  • dexterous
    dexterous about 10 years
    Also, how will I know the pid of the user application?
  • Abhishek Sagar
    Abhishek Sagar about 8 years
    user space application can register with the module at the time of initialisation using netlink sockets and , in register message they can specify their PID.
  • Chef Pharaoh
    Chef Pharaoh over 7 years
    @dexterous_stranger I did this but using kill_proc_info() instead of send_sig_info(). You can use the for_each_process() macro to find the process by name and get it's PID.