siginfo_t


The sigaction system call can establish a handler that receives more information about a signal than just the signal's number, such as how it was generated and who sent it. In particular, when installing the handler, we must set SA_SIGINFO in the struct sigaction's sa_flags field, and set the sa_sigaction function pointer with our handler (rather than the sa_handler). The handler now receives a siginfo_t argument, that contains many additional fields describing the signal.

The following program establishes a handler for SIGINT and then enters an infinite loop. To demonstrate that the kernel does not queue traditional signals, we use sigprocmask to block SIGINT at the start of the loop, and unblock it at the end: no matter how many SIGINTs we send the process while it is executing the loop body (the sleep), the kernel will only deliver one of these signals at the top of the loop.

Our handler uses the siginfo_t argument to print additional information about the signal, such as the si_code which indicates who sent the signal. In the event that a user process (such as the kill utility) sent us the signal, handler also prints the UID and PID of that process.

sa_siginfo.c