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

Use of .sa_sigaction and .sa_handler in same project(man page clarification)

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

On some architectures a union is involved: do not assign to both sa_handler and sa_sigaction(sigaction man page).

有两种阅读方式:

  1. 这是否意味着,按照我的理解,您不能在同一个 struct sigaction.
  2. 中设置 .sa_sigaction.sa_handler
  3. 或者在整个项目中只允许使用 .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.对的时候,我混了就无效了

哪个是真的? 注意:在我的架构中,它被定义为一个 union

说的就是同一个结构。它解释了原因:它们可能是 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);
};

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