diff --git a/src/main.c b/src/main.c index 4841b8b..d55d4d2 100644 --- a/src/main.c +++ b/src/main.c @@ -26,6 +26,9 @@ #include #include #include +#include +#include +#include int Pid; /* for maildir and imap */ char Hostname[256]; /* for maildir */ @@ -79,6 +82,34 @@ PACKAGE " " VERSION " - mailbox synchronizer\n" exit( code ); } +static void +crashHandler( int n ) +{ + int dpid; + char pbuf[10], pabuf[20]; + + close( 0 ); + open( "/dev/tty", O_RDWR ); + dup2( 0, 1 ); + dup2( 0, 2 ); + fprintf( stderr, "*** " EXE " caught signal %d. Starting debugger ...\n", n ); + switch ((dpid = fork ())) { + case -1: + perror( "fork()" ); + break; + case 0: + sprintf( pbuf, "%d", Pid ); + sprintf( pabuf, "/proc/%d/exe", Pid ); + execlp( "gdb", "gdb", pabuf, pbuf, (char *)0 ); + perror( "execlp()" ); + _exit( 1 ); + default: + waitpid( dpid, 0, 0 ); + break; + } + exit( 3 ); +} + static int matches( const char *t, const char *p ) { @@ -385,6 +416,12 @@ main( int argc, char **argv ) } } + if (DFlags & DEBUG) { + signal( SIGSEGV, crashHandler ); + signal( SIGBUS, crashHandler ); + signal( SIGILL, crashHandler ); + } + if (merge_ops( cops, ops )) return 1;