Make signal handlers async-signal-safe
authorSteven Luo <steven+maemo@steven676.net>
Sun, 28 Nov 2010 12:11:09 +0000 (04:11 -0800)
committerSteven Luo <steven+maemo@steven676.net>
Sun, 28 Nov 2010 12:11:09 +0000 (04:11 -0800)
commit3cc94f16ef41a2784c4185d52536f0d2e7c6610b
tree7ac219100206bd8b040932a0e45d4af1ff45bf74
parent7fff15c78fc51caa851559f569501e949241cb62
Make signal handlers async-signal-safe

Signal handlers can be invoked at any time during program execution,
including when global data is in an inconsistent state.  Therefore,
signal handlers must not modify global data without the volatile
sig_atomic_t qualifier and must not call functions outside the
async-signal-safe set; invoking a signal handler which breaks these
rules during the execution of an async-signal-unsafe function results in
undefined behavior.

All of our signal handlers currently violate these rules.

The SIGCHLD handler is easy to fix -- remove the unsafe log_msg() call,
which has outlived its usefulness anyway.

We fix the SIGHUP handler by moving the work of rereading the config
file out of signal handler context and into the main loop.  In the new
design, the process opens a pipe, the read end of which is polled in the
GLib event loop.  The signal handler writes the signal number to the
write end of the pipe, which causes the event loop to dispatch a request
to reread the config file.  It's possible to handle other signals using
the same infrastructure by teaching the dispatcher other signals.
main.c