In article <bn**********@hydrogen.cis.okstate.edu>,
Martin McCormick <ma****@okstate.edu> wrote:
A C program contains several signal statements to remove a
lock file if the program gets killed:
/*Set up interrupt handler to catch ctrl-C so that lock file can be removed.*/
signal(SIGINT,crash);
signal(SIGBUS,crash);
signal(SIGSEGV,crash);
This part works. Those signals cause the "crash.c" module to
run and get rid of the lock. Is there a standard way to also print
the error the user of the program would have seen so that whoever
runs the program knows that something bad happened? With the
signal handler, the program silently finishes which might make
someone think it was successful when in reality, it just did nothing
and abended. Thank you.
Be aware (I have to say this or people will jump on me and start
stuffing socks down my throat) that it's impossible (or at least
Extremely Difficult) to do this without invoking undefined behavior,
because the set of functions you're allowed to call from a signal handler
is extremely limited[1]. There's a good chance, however, that your
implementation defines the behavior of doing something like this to be
"what you would expect", and you're almost certainly already relying on
this to be able to clean up the lock before the program terminates.
For more details on what your implementation (as opposed to the language)
allows you to do in a signal handler, a newsgroup for your platform may
be more useful.
The signal handler gets the signal as its parameter, so a crash() that
looks something like this will probably come close to doing what you want:
--------
void crash(int sig)
{
do_cleanup();
#if USE_ALTERNATIVE_1
/*Alternative 1: Now that we've done the cleanup, let the default
signal handler dump core or whatever
*/
if(signal(sig,SIG_DFL)==SIG_ERR);
{
fputs("This is odd, can't revert to default signal handler\n",stderr);
abort();
}
raise(sig);
/*We shouldn't get here, but to be sure:*/
return;
#else
/*Alternative 2: After doing cleanup, inform user that we terminated
abnormally, but bypass the default signal handler
*/
fprintf(stderr,"Terminating abnormally (signal %d received)\n",sig);
exit();
#endif
}
--------
The boring standardese details:
Alternative #2 isn't allowed for asynchronous signals (which the ones
you're handling probably are), because you're not allowed to call most
standard library functions[1]. You're probably not doing anything worse
than you're already doing in do_cleanup(), though.
Alternative #1 is slightly more problematic, since you're not allowed
to call raise() in a signal handler invoked by calling raise() either.
In practice, though, it (like alternative #2) isn't likely to be any
worse than do_cleanup() is already doing.
dave
[1] To be precise, signal() with a first argument equal to the signal
that resulted in the handler being called or abort() when handling
an asynchronous signal, (rather more generously) anything but raise()
when handling a signal raised by raise().
--
Dave Vandervies
dj******@csclub.uwaterloo.ca
If he'd got it wrong (and it happens to us all), several people would
have jumped on him and started stuffing socks down his throat.
--Richard Heathfield in comp.lang.c