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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5634466/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 03:37:41  来源:igfitidea点击:

Sending signal from kernel to user space

linuxlinux-device-driverembedded-linux

提问by mohwastz

How to get signal from kernel space to user space?

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

回答by gby

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

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

NOTEThis 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

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

回答by liv2hak

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.

有一种叫做 NetLink 接口的东西,它为内核进程和用户进程之间的通信提供了一组 API。它类似于套接字接口,通信是异步的,因此优先于 IOCTL。

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

概述在这里:http: //www.linuxjournal.com/article/7356

回答by Raulp

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_funcfunction :

定义signal_handler_func函数:

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.

t 是用户应用程序的 PID。