As far as I understand, a SEGV
signal is sent by the OS to inform a process that it has attempted an illegal memory access.
But I noticed that it is also possible to send a SEGV
signal to a process from another process (e.g. kill -s SEGV pid
).
Therefore I was wondering, what is the use of being able to send a SEGV
signal to another process?
Futhermore, is there anyway for a process to know whether the SEGV
signal was sent by the OS or by another process?
Best Answer
I suppose someone could send it manually for testing. But even if no use for it was thought of originally, we never know if someone might come up with a use, and then arbitrary restrictions would just get in the way.
Yes, as mentioned in Catch which user sends kill signal, signal handlers set with
sigaction()
with theSA_SIGINFO
flag can read a number of details from thesiginfo_t
structure. Those contain the reason the signal was sent (si_code
), and if it applies, the sending user and PID (si_uid
,si_pid
).See the man page: https://man7.org/linux/man-pages/man2/sigaction.2.html
Here's a simple program to test that, it sets up a handler for SIGSEGV and sends it to itself, first with
kill()
and then by triggering an invalid memory access:The output on my system is:
(Note that some fields that don't apply to the case in question contain garbage.)