| Massimo Cafaro 2005-09-20, 7:02 pm |
| Dear all,
I am developing a multi-threaded daemon application and I have a
question about signal handling.
My strategy for signal handling is the following one:
1) block all of the signals using pthread_sigmask()
2) use sigaction() to ignore SIGHUP, SIGINT, SIGQUIT, SIGPIPE
3) use sigaction() to install a signal handler for SIGCHLD (the daemon
forks new processes)
4) use sigaction() to install a signal handler for SIGTERM, SIGBUS,
SIGFPE, SIGILL, SIGSEGV, SIGSYS, SIGXCPU, SIGXFSZ
5) unblock all of the signals using pthread_sigmask()
The question is, given that in the signal handler installed for
SIGTERM, SIGBUS, SIGFPE, SIGILL, SIGSEGV, SIGSYS, SIGXCPU, SIGXFSZ I
can only restrict myself to async signal safe calls, how can I safely
perform clenup before calling _exit() ?
I need to do many things, including freeing some malloced memory, but I
can't since there are just a few async signal safe functions I can
safely take advantage of inside the signal handler.
The possibility of just setting a flag of type volatile sig_atomic_t is
ruled out by the fact that, with the exception of SIGTERM, these
signals require the signal handler to _exit().
Also, I believe that siglongjmp() is not a viable solution either,
again the handler should only _exit().
The question is, it seems to me that the only remaining solution is the
use a dedicated thread for cleanup, to be used as follows.
The thread starts and blocks in a call to sem_wait(). It then blocks
indefinitely, until in the signal handler sem_post() is called (this is
async signal safe). The thread then proceeds with the application
cleanup code, while the signal handler is blocked in a call to recv(),
recfrom() or read() from a previously created pipe. When the thread is
done, it calls send(), sendto() or write() and immediately calls
pthread_exit(). The signal handler then proceeds to _exit().
Another solution for the thread to signal that it is done, could be the
use of mkdir() in the thread before exiting and a test using chdir()
in the signal handler, followed by a call to rmdir() before _exit().
All of these functions are async signal safe.
Is this correct? Is there a better way?
Thanks in advance, and best regards.
Massimo
--
****************************************
****************************************
***********************
_Massimo
Cafaro, Ph.D.__ __ __ __ __ __ __ _Center for Advanced Computational
Technologies (CACT)
_Assistant Professor __ __ __ __ __ __ __ __ __ _National
Nanotechnology Laboratory (NNL) of INFM
_University of Lecce, Italy_ _ __ __ __ __ __ _SPACI Consortium_
_Faculty of Engineering_ __ __ __ _ __ __ __ _Voice_ +39 0832 297371_ _
_Via per Monteroni __ _ __ __ __ __ __ __ __ __ _Fax_ __ _+39 0832 297279_
_73100 Lecce, Italy _ __ __ __ __ __ __ __ __ __ _Web _ _
http://sara.unile.it/~cafaro __ __ __ __ _
_E-mail massimo.cafaro@unile.it_ __ _cafaro@cacr.caltech.edu
****************************************
****************************************
***********************
|