在查找手册页进行签名时,我偶然发现了Linux手册页中一个令人困惑的注释:
在某些体系结构上,涉及一个联合:不要同时分配给两个 sa_handler和sa_sigaction(sigaction man page)。
有两种可能的阅读方式:
.sa_sigaction
中设置.sa_handler
和struct sigaction
。.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.正确时,由于我混合使用,它将是无效的。
哪个是真的? 注意:在我的架构上,它定义为联合体
答案 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);
};
因此,写入其中一个会覆盖另一个。