一、linux内核态 send_sig_info/kill_fasync 向用户态发送信号,可以带参数。kill_fasync 仅发送信号不能发送参数,需要用户端使用 read读取数据。
二、用户态C语言代码
#include <stdio.h>
#include <signal.h>
#define SIG_ETX SIGRTMIN
void handle_sig_action(int sig,siginfo_t *info,void *ctx)
{
printf("\r\n sig info recv: signo = %d, info->si_signo = %d,info->si_int = %d \r\n",
sig ,info->si_signo, info->si_int);
}
int main(int argc, char **argv)
{
struct sigaction new_sa;
new_sa.sa_sigaction = handle_sig_action;
new_sa.sa_flags = SA_RESTART | SA_SIGINFO;
new_sa.sa_restorer = NULL;
sigemptyset(&new_sa.sa_mask);
sigaction(SIG_ETX, &new_sa, NULL);
printf("sigaction demotest start signal =%d \n",SIG_ETX );
while (1)
{
sleep(1);
}
printf("sigaction demo exit!\r\n" );
}
三、kernel 驱动模块代码,使用for_each_process 遍历所有进程。
#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/signal.h>
#include <linux/sched.h>
#define SIG_ETX 34//( SIGRTMIN + 1 )
static int mysig_init(void)
{
siginfo_t info;
struct task_struct *p;
info.si_signo = SIG_ETX;
info.si_code = -1;//__SI_RT;
info.si_int = 0x8;
//read_lock(&tasklist_lock);
p = &init_task;
for_each_process(p)
{
printk("p->pid=%d ,p->comm=%s,SIG_ETX=%d\r\n", p->pid,p->comm,SIG_ETX);
if ( strcmp(p->comm, "demotest") == 0 )
{
//read_unlock(&tasklist_lock);
printk("read to send signal to user space.\r\n");
goto find_ps;
}
}
//read_unlock(&tasklist_lock);
printk("can't find the process demotest, you should run it first!\r\n");
return -1;
find_ps:
printk("send signal to user space.");
send_sig_info(SIG_ETX, &info, p);
return 0;
}
static void mysig_exit(void)
{
printk("my signal test exit");
}
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Terry");
module_init(mysig_init);
module_exit(mysig_exit);
四、运行效果,上面是用户态,下面的内核态。
五、用linux kill -l 命令发从信号
六、参考文章
Linux 内核态 与 用户态传递 实时信号_send_sig_info_雷电羊的博客-CSDN博客
linux内核态 send_sig_info/kill_fasync 向用户态发送数据_青松0527的博客-CSDN博客
WDS1期第12课 字符设备驱动 7 async异步通知用户空间 驱动代码的kill_fasync函数通知用户的signal函数 fcntl_巴德鸟的博客-CSDN博客
https://www.cnblogs.com/Wayne123/archive/2023/03/16/17221123.html
Linux信号_linux 发送信号命令_Ssorrymaker的博客-CSDN博客
Linux驱动实践:中断处理函数如何【发送信号】给应用层?- 驱动与应用层数据交互(二)_send_sig_info_生活需要深度的博客-CSDN博客