从 boost::siginfo 处理程序中的 siginfo 获取 sig_int 值

Obtaining the sig_int value from siginfo in boost::siginfo handler

我有一个自定义信号,它通过以下方式从内核模块发送给用户 space。请注意,我在附加到信号的 si_int 结构参数中包含一个整数 (id),并且我已经知道我定位的用户 space 应用程序的 pid。

struct siginfo info;
memset(&info, 0, sizeof(struct siginfo));
info.si_signo = MY_CUSTOM_SIGNAL;
info.si_code = SI_QUEUE;        // Fake coming from user-space
info.si_int = id;               // Include some variable id
send_sig_info(MY_CUSTOM_SIGNAL, &info, pid);

在我的 user-space 应用程序中,我通过以下方式监听信号:

boost::asio::io_service io;
boost::asio::signal_set signals(io, MY_CUSTOM_SIGNAL);
signals.async_wait(handler);
io.run();

... 并在它发生时调用此处理程序...

void handler(const boost::system::error_code& error, int signal_number)
{
    // Do something with the signal
}

不幸的是,siginfo 没有传递到处理程序中,所以我无法确定内核模块分配的 id 变量的值。有什么方法可以确定这个值吗?

简而言之,没有。

sigaction(), Boost.Asio registers its internal signal handler as sa_handler and does not set the SA_SIGINFO flag. Hence, Boost.Asio's signal handler never receives the siginfo_t associated with the signal, and cannot propagate it to the user's SignalHandler when completing the async_wait() operation. Here is the relevant excerpt from the implementation存在的情况下:

using namespace std; // For memset.
struct sigaction sa;
memset(&sa, 0, sizeof(sa));
sa.sa_handler = boost_asio_signal_handler;
sigfillset(&sa.sa_mask);
if (::sigaction(signal_number, &sa, 0) == -1)

考虑到您最近关于管道上 and Boost.Asio only receiving and propagating signum, it may be worth considering implementing your own signal handler using the self-pipe trick. However, where as Boost.Asio only writes and reads signum 的问题,可以写入和读取其他信息,例如 siginfo.si_int