.sa_sigaction和.sa_handler在同一项目中的使用(说明手册页)

时间:2019-04-11 19:20:25

标签: linux signals man sigaction

在查找手册页进行签名时,我偶然发现了Linux手册页中一个令人困惑的注释:

  

在某些体系结构上,涉及一个联合:不要同时分配给两个   sa_handler和sa_sigaction(sigaction man page)。

有两种可能的阅读方式:

  1. 据我所知,这是否意味着不允许您在同一.sa_sigaction中设置.sa_handlerstruct sigaction
  2. 还是只允许在整个项目中使用.sa_sigaction.sa_handler
struct sigaction sigactionArray[SIGNAL_SIGACTION_ARRAY_SIZE] = {
   {.sa_handler = SIG_DFL, .sa_flags = SA_NODEFER},
   {.sa_handler = SIG_DFL, .sa_flags = SA_NODEFER},
   {.sa_handler = SIG_DFL, .sa_flags = SA_NODEFER},
   {.sa_sigaction = HandleBacktraceSignals, .sa_flags = SA_SIGINFO | SA_RESTART | SA_ONSTACK},
   {.sa_sigaction = HandleBacktraceSignals, .sa_flags = SA_SIGINFO | SA_RESTART | SA_ONSTACK},
   {.sa_sigaction = HandleBacktraceSignals, .sa_flags = SA_SIGINFO | SA_RESTART | SA_ONSTACK},
   {.sa_sigaction = HandleBacktraceSignals, .sa_flags = SA_SIGINFO | SA_RESTART | SA_ONSTACK},
   {.sa_sigaction = HandleBacktraceSignals, .sa_flags = SA_SIGINFO | SA_RESTART | SA_ONSTACK},
   {.sa_handler = SIG_IGN, .sa_flags = SA_NODEFER},
   {.sa_handler = SIG_IGN, .sa_flags = SA_NODEFER},
   {.sa_sigaction = HandleBacktraceSignals, .sa_flags = SA_SIGINFO | SA_RESTART | SA_ONSTACK},
   {.sa_handler = SIG_IGN, .sa_flags = SA_NODEFER},
   {.sa_sigaction = HandleBacktraceSignals, .sa_flags = SA_SIGINFO | SA_RESTART | SA_ONSTACK},
   {.sa_handler = SIG_DFL, .sa_flags = SA_NODEFER},
   {.sa_handler = SIG_DFL, .sa_flags = SA_NODEFER},
   {.sa_handler = SIG_IGN, .sa_flags = SA_NODEFER},
   {.sa_handler = SIG_IGN, .sa_flags = SA_NODEFER},
   {.sa_handler = SIG_DFL, .sa_flags = SA_NODEFER},
   {.sa_handler = SIG_IGN, .sa_flags = SA_NODEFER},
   {.sa_handler = SIG_DFL, .sa_flags = SA_NODEFER},
   {.sa_handler = SIG_DFL, .sa_flags = SA_NODEFER},
   {.sa_handler = SIG_DFL, .sa_flags = SA_NODEFER},
   {.sa_handler = SIG_DFL, .sa_flags = SA_NODEFER},
   {.sa_sigaction = HandleBacktraceSignals, .sa_flags = SA_SIGINFO | SA_RESTART | SA_ONSTACK},
   {.sa_sigaction = HandleBacktraceSignals, .sa_flags = SA_SIGINFO | SA_RESTART | SA_ONSTACK},
   {.sa_handler = SIG_DFL, .sa_flags = SA_NODEFER},
   {.sa_handler = SIG_DFL, .sa_flags = SA_NODEFER},
   {.sa_handler = SIG_IGN, .sa_flags = SA_NODEFER},
   {.sa_handler = SIG_DFL, .sa_flags = SA_NODEFER},
   {.sa_handler = SIG_DFL, .sa_flags = SA_NODEFER},
   {.sa_sigaction = HandleBacktraceSignals, .sa_flags = SA_SIGINFO | SA_RESTART | SA_ONSTACK}
};

根据解释1.上面的代码片段是可以的,但是当定义2.正确时,由于我混合使用,它将是无效的。

哪个是真的? 注意:在我的架构上,它定义为联合体

1 个答案:

答案 0 :(得分:1)

它只是在谈论相同的结构。它解释了原因:它们可能是union的一部分,因此它们在结构中使用相同的内存。例如。实际上可能是这样的:

struct sigaction {
    union {
        void     (*sa_handler)(int);
        void     (*sa_sigaction)(int, siginfo_t *, void *);
    },
    sigset_t   sa_mask;
    int        sa_flags;
    void     (*sa_restorer)(void);
};

因此,写入其中一个会覆盖另一个。