2023-04-19 linux内核态 send_sig_info 向用户态发送信号参数,可以带参数。用户态使用sigaction(SIG_ETX, &new_sa, NULL);处理介绍数据函数

一、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博客

linux内核模块之简单进程遍历_遍历进程时需要get_task_struct_nai_dev的博客-CSDN博客

从Linux内核模块向应用发信号_send_sig_info_yuwen_dai的博客-CSDN博客

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值