Reputation: 1
How does one find reliably whether a process received a signal due to its own misbehavior or was sent the same by another process? Basically, how does one determine whether si_pid field is valid or not.
Upvotes: 0
Views: 672
Reputation: 249394
If si_pid
in the siginfo_t
structure matches getpid()
then the process signaled itself. Otherwise, another process did. Since process IDs are unique at any point in time, a PID you have now could not possibly have sent you the signal at a time when it had your PID (because then it would have signaled itself and not you).
Edit:
As you have discovered, the si_pid
field is not always set; sometimes it contains garbage values. The first thing to check is that you have passed SA_SIGINFO
in the sa_flags
field of your struct sigaction
when registering your handler. Without this, your handler may not receive a siginfo_t
at all.
Once that's done, there are rules for when si_pid
is set, described here: https://www.mkssoftware.com/docs/man5/siginfo_t.5.asp#Signal_Codes
In brief: si_pid
should be set if si_code
is one of:
SI_USER
- includes calls to kill()
SI_QUEUE
SI_TIMER
SI_ASYNCIO
SI_MESGQ
It is also set whenever si_signo
is SIGCHLD
.
Upvotes: 2