Search code examples
cpointerssigaction

Why is context in sigaction handler a void pointer?


In sigaction(2) man page:

The siginfo_t argument to a SA_SIGINFO handler
   When the SA_SIGINFO flag is specified in act.sa_flags, the signal
   handler address is passed via the act.sa_sigaction field.  This han‐
   dler takes three arguments, as follows:

       void
       handler(int sig, siginfo_t *info, void *ucontext)
       {
           ...
       }

Why is ucontext a void * when the man page states that it is a ucontext_t *?

ucontext
          This is a pointer to a ucontext_t structure, cast to void *.
          The structure pointed to by this field contains signal context
          information that was saved on the user-space stack by the ker‐
          nel; for details, see sigreturn(2).  Further information about
          the ucontext_t structure can be found in getcontext(3).  Com‐
          monly, the handler function doesn't make any use of the third
          argument.

Solution

  • POSIX actually requires this to be a void *, the third argument to sigaction is:

    void(*) (int, siginfo_t *, void *)
    

    Additionally, since a void * can be freely cast to and from any other sort of data pointer, there's little reason not to use the general case in a situation where you may want to seamlessly add different types in future.