Proper exit handling
Kemp at DOCKMASTER.NCSC.MIL
Kemp at DOCKMASTER.NCSC.MIL
Mon Mar 27 23:59:44 AEST 1989
Does anyone know of a bulletproof method of installing an exit
handling routine that is guaranteed to be called when a process
terminates?
I have a C program running under SunOS 4.0.1 which sets the terminal
input mode to the equivalent of cbreak and noecho. When the user types
any key, the terminal input mode is restored to its normal state and the
process exits. (The program plays files through a D/A converter, but
that is not particularly relevant to this discussion.)
I start by trapping a few terminal related signals:
signal(SIGHUP, onterm);
signal(SIGINT, onterm);
signal(SIGTERM, onterm);
signal(SIGQUIT, SIG_IGN);
signal(SIGTSTP, SIG_IGN);
The handler just sets a global flag and returns; the main program
checks the flag and takes appropriate action:
void onterm(sig)
int sig;
{
done = 1;
if (sig == SIGINT) done++;
}
This works fine under normal circumstances, but if something unusual
happens which kills the process (bus error, arithmetic exception, or any
of a million other things), the terminal will be left in hosed mode. I
could trap the 20 signals that cause a process to die, but that is a
gross and ugly hack, and besides what about SIGSTOP and SIGKILL, which
cannot be caught?
What is needed is an exit handler that will always be called when the
process dies, whatever the reason. If anyone knows The Right Way to do
this, please post it. I'm sure everyone here is interested in writing
quality code that works first time, every time!
Thanks,
Dave Kemp (Kemp at dockmaster.arpa)
p.s. Can anyone tell me where in TFM to find documentation on SIGTERM
(the software termination signal)? It would be nice if this were sent
by exit() TO the terminating process, but I assume that it is just
another of the signals that causes the process to terminate. SIGNAL(3)
doesn't say anything about it except to list its value.
More information about the Comp.unix.questions
mailing list