从内核向用户空间发送信号

moh*_*stz 5 linux linux-device-driver embedded-linux

如何从内核空间获取信号到用户空间?

Rau*_*ulp 6

要从内核到用户空间获取信号,请在用户空间和内核空间代码中使用以下代码,如下所示:

用户空间应用:

signal(SIGIO, &signal_handler_func); 
fcntl(fd, F_SETOWN, getpid());
oflags = fcntl(fd, F_GETFL);
fcntl(fd, F_SETFL, oflags | FASYNC);
Run Code Online (Sandbox Code Playgroud)

定义signal_handler_func函数:

void signal_handler_func (int sig)
{

//handle the action corresponding to the signal here
}
Run Code Online (Sandbox Code Playgroud)

内核空间模块:

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 
Run Code Online (Sandbox Code Playgroud)

t是用户应用程序的PID.


gby*_*gby 1

使用内核API函数kill_proc_info(int sig, struct siginfo *info, pid_t pid)

注意这实际上是一个糟糕的答案。这些函数确实向用户空间发送信号,但正确的方法是这样做,因为提问者的目的是使用 fasync 字符设备方法,如下所述:http://www.xml.com/ldd/chapter/book/ch05 .html#t4