问题描述
我需要从内核向用户空间函数发送一个字符串,而无需特别从用户空间中请求它,而是通过内核中的某个事件在用户空间中触发某种函数或应用程序.到目前为止,我已经尝试过一个从用户空间中的init开始,然后进入睡眠状态并不断阅读有关netlink的Ioctl,但是找不到适合它的有效示例.任何建议或示例将非常有必要.
I need to send a string from kernel to a user space function without asking for it in particular from the user space, sort of triggering a function or application in the user space via some event in kernel.So far I have tried an Ioctl that starts on an init in user space and then sleeps and kept reading about netlink but couldn't find a good working example for it.Any suggestions or examples will be much obliged.
推荐答案
这是我的流程的工作方式,我也会对任何改进建议感兴趣:
Here's how my process works, I would be interested in any suggestions for improvements as well:
- 启动内核模块
-
启动用户空间应用程序,该应用程序将自定义命令发送到内核模块,以注册用于内核模块信号的用户空间PID.就我而言,这是通过写入/dev/mymodule来实现的.内核模块注册PID:
- Start the kernel module
Start user space application, which sends a custom command to the kernel module to register the user space PID for kernel module signals. In my case this was via a write to /dev/mymodule. The kernel module registers the PID:
...
printk("registering a new process id to receive signals: %d\n", current->pid);
signal_pid = current->pid;
...
用户空间应用程序还向内核注册了用于某些类型信号的处理程序.
The user space application also registers a handler with the kernel for certain types of signals.
void local_sig_handler(int signum) {
printf("received a signal from my module\n");
fflush(stdout); }
...
signal(SIGIO, local_sig_handler);
内核模块生成信号
Kernel module generates a signal
...
struct siginfo info;
struct task_struct *t;
info.si_signo=SIGIO;
info.si_int=1;
info.si_code = SI_QUEUE;
printk("<1>IRQ received: %d\n", irq);
printk("<1>searching for task id: %d\n", signal_pid);
t= pid_task(find_vpid(signal_pid),PIDTYPE_PID);//user_pid has been fetched successfully
if(t == NULL){
printk("<1>no such pid, cannot send signal\n");
} else {
printk("<1>found the task, sending signal\n");
send_sig_info(SIGIO, &info, t);
}
内核将信号中继到应用程序的处理程序
Kernel relays the signal to the application's handler
这篇关于用内核触发用户空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!