Re: Catching signal errors
Re: Catching signal errors
- Subject: Re: Catching signal errors
- From: Kevin Harris <email@hidden>
- Date: Mon, 07 Feb 2005 11:51:12 -0700
Rick Steele wrote:
Forgive the ignorance but,
if one has a function like this within an application:
void SumFunction(){
Ptr pPtr = -1L; //create something that will obviously
crash if deferenced
char pSomethingElse;
pSomethingElse = (*pPtr); // Causing the error
}
how would some one catch the signal that would inevitable occur so the
app might be able to close down gracefully?
Try 0 (or NULL) -- it should cause a SIGSEGV or SIGBUS (depending on the
platform), whereas -1L could (potentially) be a valid chunk of memory.
How about this snippet:
--------------------------------
#include <signal.h>
#include <stdlib.h>
#include <unistd.h>
typedef void (*full_sighandler_t)(int,siginfo_t*,void*);
void installSignalHandler(int sig, full_sighandler_t handler)
{
struct sigaction temp;
sigaction(sig, 0, &temp);
// Set the SA_SIGINFO flag so that sa_sigaction may be safely used.
temp.sa_flags = SA_SIGINFO;
temp.sa_sigaction = handler;
sigemptyset(&temp.sa_mask);
sigaction(sig, &temp, NULL);
}
void fatalSigHandler(int sig, siginfo_t* info, void* context)
{
// We are very limited in what we are allowed to do in a signal
handler.
// Memory should not be allocated, and most libc functions should
not be used.
static char sig_message[] = "Aborting: Fatal signal occurred!\n";
write(1, sig_message, sizeof(sig_message));
exit(1);
}
int main(int argc, char** argv)
{
installSignalHandler(SIGSEGV, &fatalSigHandler);
installSignalHandler(SIGBUS, &fatalSigHandler);
int* foo = NULL;
*foo = 0;
exit(0);
}
--------------------------------
You should read the man pages for signal and sigaction.
-Kevin-
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Darwin-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden