Sending signal from kernel to user space
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
Comments
-
mohwastz almost 2 years
How to get signal from kernel space to user space?
-
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 about 13 yearssir tell me what is the header file?
-
mohwastz about 13 yearsheader file for kernel API function kill_proc_info(int sig, struct siginfo *info, pid_t pid)
-
mohwastz about 13 yearswhile executing my program I got following error
-
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 over 11 yearsThis may help people.ee.ethz.ch/~arkeller/linux/multi/…
-
dexterous about 10 yearsCan you please provide the complete example?
-
dexterous about 10 yearsAlso, how will I know the pid of the user application?
-
Abhishek Sagar about 8 yearsuser 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 over 7 years@dexterous_stranger I did this but using
kill_proc_info()
instead ofsend_sig_info()
. You can use thefor_each_process()
macro to find the process by name and get it's PID.