Hello,
I'd like to know whether it is possible to let the parent know who kills its child process. The case is likely as below:
if there are four processes, we call them A, B, C and D. B is the child of A, and can be killed by both C and D. if B is killed, then A will receive SIGCHLD from B. But how to let A know who kills B? C or D ?
Thanks in advance !
currently I can find two ways to do that, but not so neatly:
1) let C or D send a signal to inform A before it kills B;
2) let C or D write a flag to a file before it kills B, A checks this flag in this file when handle SIGCHLD from B.
other cleaner solutions ?
Thanks.
have D send B a SIGUSR1
have C send B a SIGUSR2
B traps both SIGUSR1, and SIGUSR2. Then exits optionally with the value of the signal
A calls waitpid() on B, then calls WTERMSIG or WEXITSTATUS if B exited with the SIG value. This requires C code.
Thanks for your reasonable advice ...
I probably did not state clearly about the case ... well, for the example ABCD case, it's simple because there are only two processes that could kill the child B.
What I really want to know is that, if it is illegal for any process except C to kill process B, A have to check that who kills B when receives SIGCHLD from B (what if we kill B manually by "kill -9" command ? perhaps A can distinguish them by pid of the killer ?).
kill -9 cannot be caught by a process, it just exits. It cannot know who killed it.
And, as long as "whoever" B,C,D,...Z has the privilege to send a signal to B (it can send a signal to itself you know) kill -9 will always clobber B. So there is no "illegal" anything about sending signals.
Rather than go on about this, what are you trying to do? It sounds like you got bogged down trying to solve something else, rather than signals.
yes, there is really a pratical problem I want to fix...I'm sorry for my poor English that I did not express myself clearly with the word "illegal"...
what I meant is that in our system or according to our design requirements, there is a process(just like B in this example), its parent is A, and A will handle SIGCHLD from B. But we can only allow one process(C) to kill B, any other process killing B will be considered as an error case in A.
so we have to distinguish who kills this process B.
Actually, B here is ntpd service, we want to start it as A's child, let ntpd run in foreground mode. But if we re-configure ntpd, ntpd has to be restarted to reload the new configuration. So we allow C who has the privilege to re-configure ntpd to restart ntpd, but not other processes.
currently, I'd like to let C set a flag in /var/run/ntpd.pid file after re-configuration and before restart ntpd.
So I come here for help to see whether there is more smart ideas..
Thank you.
A cannot tell who sent the signal to B. B cannot tell who sent the signal to it.
The only thing you can do is to to write something to a file before you kill the process.
Or, if you can make it work, create a completely distinct user for ntpd, one that has privilege to run ntpd. Then nobody but that user (or root) can kill off the ntpd daemon.
Do you have random processes killing the ntpd daemon?
Just in case that ntpd exits for some unexpected reason or error operations.