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

Ric*_*ard 5 signals linux-kernel

我的内核模块代码需要发送信号[ def。]用户土地计划,以将其执行转移到注册的信号处理程序。

我知道如何在两个用户登陆进程之间发送信号,但是我找不到有关该任务的任何在线示例。

具体来说,我的预期任务可能需要如下所示的接口(一旦error!= 1,int a=10不应执行代码行):

void __init m_start(){
    ...
    if(error){
        send_signal_to_userland_process(SIGILL)
    }
    int a = 10;
    ...
}

module_init(m_start())
Run Code Online (Sandbox Code Playgroud)

omo*_*tto 7

我过去使用的一个示例是从内核空间中的硬件中断向用户空间发送信号。如下所示:

内核空间

#include <asm/siginfo.h>    //siginfo
#include <linux/rcupdate.h> //rcu_read_lock
#include <linux/sched.h>    //find_task_by_pid_type

static int pid; // Stores application PID in user space

#define SIG_TEST 44
Run Code Online (Sandbox Code Playgroud)

需要一些“包含”和定义。基本上,您需要用户空间中应用程序的PID。

struct siginfo info;
struct task_struct *t;

memset(&info, 0, sizeof(struct siginfo));
info.si_signo = SIG_TEST;
// This is bit of a trickery: SI_QUEUE is normally used by sigqueue from user space,    and kernel space should use SI_KERNEL. 
// But if SI_KERNEL is used the real_time data  is not delivered to the user space signal handler function. */
info.si_code = SI_QUEUE;
// real time signals may have 32 bits of data.
info.si_int = 1234; // Any value you want to send
rcu_read_lock();
// find the task with that pid
t = pid_task(find_pid_ns(pid, &init_pid_ns), PIDTYPE_PID);
if (t != NULL) {
    rcu_read_unlock();      
    if (send_sig_info(SIG_TEST, &info, t) < 0) // send signal
        printk("send_sig_info error\n");
} else {
     printk("pid_task error\n");
     rcu_read_unlock();
    //return -ENODEV;
}
Run Code Online (Sandbox Code Playgroud)

前面的代码准备信号结构并发送它。请记住,您需要应用程序的PID。在我的情况下,来自用户空间的应用程序通过ioctl驱动程序过程发送其PID:

static long dev_ioctl(struct file *file, unsigned int cmd, unsigned long arg) {
    ioctl_arg_t args;
    switch (cmd) {
        case IOCTL_SET_VARIABLES:
              if (copy_from_user(&args, (ioctl_arg_t *)arg, sizeof(ioctl_arg_t))) return -EACCES;
              pid = args.pid;
        break;
Run Code Online (Sandbox Code Playgroud)

用户空间

定义并实现回调函数:

#define SIG_TEST 44

void signalFunction(int n, siginfo_t *info, void *unused) {
    printf("received value %d\n", info->si_int);
}
Run Code Online (Sandbox Code Playgroud)

在主要过程中:

int  fd = open("/dev/YourModule", O_RDWR); 
if (fd < 0) return -1;

args.pid       = getpid();
ioctl(fd, IOCTL_SET_VARIABLES, &args); // send the our PID as argument

struct sigaction sig;

sig.sa_sigaction = signalFunction; // Callback function
sig.sa_flags = SA_SIGINFO;
sigaction(SIG_TEST, &sig, NULL);
Run Code Online (Sandbox Code Playgroud)

我希望它会有所帮助,尽管答案有点长,但是很容易理解。


Tsy*_*rev 5

例如,您可以使用kill_pid(在 中声明<linux/sched.h>)将信号发送到指定的进程。为了形成参数给它,见实施sys_kill(定义为SYSCALL_DEFINE2(kill)kernel/signal.c)。

请注意,从内核向当前进程发送信号几乎没有用:内核代码应该在用户空间程序看到信号触发之前返回。


Bas*_*tch 0

你的界面违背了 Linux 的精神。不要这样做......系统调用(特别是与您的驱动程序相关的系统调用)应该只会失败errno(请参阅syscalls(2) ...);考虑使用eventfd(2)netlink(7)进行此类异步内核 <-> 用户态通信(并期望用户代码能够轮询(2)它们)。

内核模块可能无法加载。我不熟悉细节(从未编写过任何内核模块),但这个 hello2.c示例表明模块 init 函数可以在失败时返回非零错误代码。

人们确实期望信号(这是一个困难且痛苦的概念)的行为如signal(7)中记录的那样,而您想要做的事情并不符合该图片。因此,一个行为良好的内核模块永远不应该向进程异步发送任何信号

如果你的内核模块表现不佳,你的用户会很生气并且不会使用它。

如果您想分叉您的实验内核(例如用于研究目的),请不要期望它会被大量使用;只有这样,您才能真正像您打算做的那样打破信号行为,并且您可以编写不适合内核模块图片的内容(例如添加新的系统调用)。另请参阅kernelnewbies