在sigaction中恢复原始信号

发布于 2024-12-04 18:15:30 字数 933 浏览 1 评论 0原文

我定义了 sigaction 并且工作正常。但是我想在我的操作完成后恢复原始信号。这是我的 sigaction:

static void signal_handler(int signal, siginfo_t *info, void *reserved)
{
    //Some logging statements
    //How do I restore the original signal here??
}

信号处理程序是从 JNI_Onload 设置的:

extern "C" jint JNI_OnLoad(JavaVM* vm, void* /*reserved*/)
{
    struct sigaction handler, action_old;
    memset(&handler, 0, sizeof(handler));
    handler.sa_sigaction = signal_handler;
    handler.sa_flags = SA_SIGINFO;
    sigaction(SIGILL, &handler, &action_old);
    sigaction(SIGABRT, &handler, &action_old);
    sigaction(SIGBUS, &handler, &action_old);
    sigaction(SIGFPE, &handler, &action_old);
    sigaction(SIGSEGV, &handler, &action_old);
    sigaction(SIGSTKFLT, &handler, &action_old);

    //Can I restore prior signal here???

    return JNI_VERSION_1_6;
}

I have sigaction defined and it works fine. However I want to restore the original signal after my action is completed. This is my sigaction:

static void signal_handler(int signal, siginfo_t *info, void *reserved)
{
    //Some logging statements
    //How do I restore the original signal here??
}

The signal handler is set from JNI_Onload:

extern "C" jint JNI_OnLoad(JavaVM* vm, void* /*reserved*/)
{
    struct sigaction handler, action_old;
    memset(&handler, 0, sizeof(handler));
    handler.sa_sigaction = signal_handler;
    handler.sa_flags = SA_SIGINFO;
    sigaction(SIGILL, &handler, &action_old);
    sigaction(SIGABRT, &handler, &action_old);
    sigaction(SIGBUS, &handler, &action_old);
    sigaction(SIGFPE, &handler, &action_old);
    sigaction(SIGSEGV, &handler, &action_old);
    sigaction(SIGSTKFLT, &handler, &action_old);

    //Can I restore prior signal here???

    return JNI_VERSION_1_6;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

野稚 2024-12-11 18:15:30

将旧操作保存在全局(或文件范围)变量(或按信号 ID 索引的数组)中,并从信号处理程序内部调用 sigaction 来恢复以前的行为。 sigaction 保证异步信号安全。

另请参阅:http://pubs.opengroup.org/onlinepubs/9699919799/函数/V2_chap02.html#tag_15_04_03

Save the old actions in global (or file-scope) variables (or an array indexed by signal id) and call sigaction from inside your signal handler to restore the previous behavior. sigaction is guaranteed to be async-signal safe.

See also: http://pubs.opengroup.org/onlinepubs/9699919799/functions/V2_chap02.html#tag_15_04_03

离鸿 2024-12-11 18:15:30

http://www.gnu.org/s/hello /manual/libc/Basic-Signal-Handling.html - 说:

信号函数返回先前对指定符号有效的操作。您可以保存该值并稍后通过再次调用信号来恢复它。

http://www.gnu.org/s/hello/manual/libc/Basic-Signal-Handling.html - says:

The signal function returns the action that was previously in effect for the specified signum. You can save this value and restore it later by calling signal again.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文