Background
I am writing a shared library in C, dynamically linked with LD_PRELOAD
, that is meant to intercept and override network calls from the application preloading it, such as socket()
, connect()
, recv()
, send()
, etc.
In the init of the library I launch a thread with pthread_create()
. This thread polls some kernel memory, which is mapped to user space.
The library is meant to be generic enough to deal with - but not limited to - networking benchmark applications such as netperf, iperf, sockperf.
My issue
Everything works fine, life is sweet in most of the cases except one. Deamonized applications. For instance, if I launch netserver (the server side of the netperf benchmarking application) as a deamon, i.e. without the -D
parameter, one of the first things the application does is call fork()
. On fork, the parent is closed using exit(EXIT_SUCCESS)
and the child listens for connections. The fact that the parent exits kills my polling thread.
So what I'd like to achieve is have the child spawn a new polling thread if the parent has gone. I can intercept and override the fork()
call but fundamentally, how can I have the child know whether the parent has gone or not? I can't take any assumption because the library has to be generic.
Thanks.
You can poll periodically getppid() function. As soon as it starts to return '1' (the id of init process) - your parent is dead.
Update
excerpt from 'man pthread_create':
The new thread terminates in one of the following ways: ...
- Any of the threads in the process calls exit(3), or the main thread performs a return from main(). This causes the termination of all threads in the process.
So, if your thread is created by the netserver process that calls exit - yes this thread will be terminated
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With