如何从内核模块向所有正在运行的进程广播自定义Linux信号 [英] How to broadcast a custom Linux signal from a kernel module to all running processes

查看:289
本文介绍了如何从内核模块向所有正在运行的进程广播自定义Linux信号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了一个内核模块,该模块向用户空间提供了一些有关硬件中断的信息.当前,用户空间应用程序使用IOCTL将其PID x发送到内核模块.然后,内核模块使用此PID查找任务并发送信号:

I have written a kernel module that supplies some information about a hardware interrupt to user-space. Currently, the user-space application uses IOCTL to send its PID x to the kernel module. The kernel module then uses this PID to find the task and send the signal:

#define CUSTOM_SIGNAL 44
struct siginfo info;
memset(&info, 0, sizeof(struct siginfo));
info.si_signo = CUSTOM_SIGNAL;
info.si_code = SI_QUEUE;
info.si_int = 0;
struct task_struct *t = pid_task(find_pid_ns(x, &init_pid_ns), PIDTYPE_PID);
send_sig_info(CUSTOM_SIGNAL, &info, t); 

这真的很好.但是,我发现为单个信号维护动态的PID接收器列表非常棘手.出于这个原因,我想将信号广播为所有正在运行的进程的默认值(因此它们无需注册即可得到通知,它只是发生了).

This works really well. However, I find it rather tricky to maintain a dynamic list of PID receivers for a single signal. For this reason I would like to broadcast the signal be default to all running processes (so they need not register to be notified -- it just happens).

我可以想到的一个模仿此行为的示例是系统关闭信号.是否可以简单地广播我的CUSTOM_SIGNAL,或者我是否需要遍历所有PID,按上述方式一对一发送.还是有代表广播的特殊任务?

One example I can think of that mimics this behaviour is the system shutdown signal. Is it possible to simply broadcast my CUSTOM_SIGNAL, or do I need to iterate over all PIDs, sending one-by-one as above. Or is there a special task representing broadcast?

推荐答案

这是完全错误的.

我已经编写了一个内核模块,其中提供了有关 硬件中断到用户空间.当前,用户空间 应用程序使用IOCTL将其PID x发送到内核模块.这 内核模块然后使用此PID查找任务并发送信号

I have written a kernel module that supplies some information about a hardware interrupt to user-space. Currently, the user-space application uses IOCTL to send its PID x to the kernel module. The kernel module then uses this PID to find the task and send the signal

首先,您可以通过 current 访问调用线程任务结构.像这样获得一个PID然后再找到它是没有任何意义的.

First off, you have access to calling thread task structure by the means of current. Obtaining a PID like this and then finding it makes no sense whatsoever.

您的代码示例还建议您不要锁定RCU,如果您启用了调试,这将是您会发现的错误.

Also your code sample suggests you don't lock RCU, which is a bug you would learn about if you had debug enabled.

这真的很好.不过,我觉得维持 单个信号的PID接收器的动态列表.因此,我 希望将信号广播为所有正在运行的进程的默认设置 (因此,他们无需注册即可收到通知-只是发生这种情况.)

This works really well. However, I find it rather tricky to maintain a dynamic list of PID receivers for a single signal. For this reason I would like to broadcast the signal be default to all running processes (so they need not register to be notified -- it just happens).

这在原理上同样是错误的.您会引起虚假的唤醒,这可能会带来很多奇怪的乐趣(并非所有事物都像人们认为的那样原子性").

This is similarly wrong on its principle. You will cause spurious wake ups and that has potential for a lot of weird-ass fun (not everything is as 'atomic' as people think).

相反,创建一个设备驱动程序,可以在该设备驱动程序中获取文件描述符.感兴趣的线程将轮询描述符,这就是他们将如何了解事件的信息.

Instead create a device driver to which a file descriptor can be obtained. Interested threads will poll the descriptor and that's how they will learn about the event.

这篇关于如何从内核模块向所有正在运行的进程广播自定义Linux信号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆