ack/plat/em/libsys/signal.c

102 lines
2.4 KiB
C
Raw Permalink Normal View History

1994-06-24 14:02:31 +00:00
/* $Id$ */
1985-01-01 22:27:02 +00:00
#include <signal.h>
2022-08-01 20:08:23 +00:00
extern void _setsig(int (*)(int));
1985-01-01 22:27:02 +00:00
2022-08-01 20:08:23 +00:00
static sighandler_t vector[16] = {
1985-01-01 22:27:02 +00:00
SIG_DFL, SIG_DFL, SIG_DFL, SIG_DFL, SIG_DFL, SIG_DFL, SIG_DFL, SIG_DFL,
SIG_DFL, SIG_DFL, SIG_DFL, SIG_DFL, SIG_DFL, SIG_DFL, SIG_DFL, SIG_DFL
} ;
2022-08-01 20:08:23 +00:00
static const char mapvec[] = {
1985-01-01 22:27:02 +00:00
0, /* EARRAY */
0, /* ERANGE */
0, /* ESET */
0, /* EIOVFL */
SIGFPE, /* EFOVFL */
SIGFPE, /* EFUNDFL */
0, /* EIDIVZ */
SIGFPE, /* EFDIVZ */
0, /* EIUND, already ignored */
SIGFPE, /* EFUND */
0, /* ECONV */
0, /* 11 */
0, /* 12 */
0, /* 13 */
0, /* 14 */
0, /* 15 */
SIGSEGV, /* ESTACK */
SIGSEGV, /* EHEAP */
0, /* EILLINS */
0, /* EODDZ */
0, /* ECASE */
SIGSEGV, /* EBADMEM */
SIGBUS, /* EBADPTR */
0, /* EBADPC */
0, /* EBADLAE */
SIGSYS, /* EBADMON */
0, /* EBADLIN */
0, /* EBADGTO */
} ;
#define VECBASE 128
static firsttime = 1 ;
2022-08-01 20:08:23 +00:00
extern int sigtrp(int mapval, int sig) ;
static int catchtrp(int trapno) ;
static int procesig(int signo) ;
1985-01-01 22:27:02 +00:00
2022-08-01 20:08:23 +00:00
sighandler_t signal(int sig, sighandler_t func) {
1985-01-01 22:27:02 +00:00
register index, i ;
2022-08-01 20:08:23 +00:00
sighandler_t prev ;
1985-01-01 22:27:02 +00:00
index= sig-1 ;
if ( index<0 || index>=(sizeof vector/sizeof vector[0]) ) {
2022-08-01 20:08:23 +00:00
return (sighandler_t) -1 ;
1985-01-01 22:27:02 +00:00
}
if ( firsttime ) {
firsttime= 0 ;
_setsig(catchtrp) ;
}
prev= vector[index] ;
if ( prev!=func ) {
register int mapval ;
vector[index]= func ;
if ( func==SIG_IGN ) {
mapval= -3;
} else if ( func==SIG_DFL ) {
mapval= -2;
} else {
mapval=VECBASE+sig;
}
2022-08-01 20:08:23 +00:00
if ( sigtrp(mapval,sig)== -1 ) return (sighandler_t) -1;
1985-01-01 22:27:02 +00:00
}
return prev ;
}
2022-08-01 20:08:23 +00:00
static int catchtrp(int trapno) {
1985-01-01 22:27:02 +00:00
if ( trapno>VECBASE &&
trapno<=VECBASE + (sizeof vector/sizeof vector[0]) ) {
return procesig(trapno-VECBASE) ;
}
if ( trapno>=0 && trapno< (sizeof mapvec/sizeof mapvec[0]) &&
mapvec[trapno] ) {
return procesig(mapvec[trapno]) ;
}
return 0 ; /* Failed to handle the trap */
}
2022-08-01 20:08:23 +00:00
static int procesig(int sig) {
1985-01-01 22:27:02 +00:00
register index ;
2022-08-01 20:08:23 +00:00
sighandler_t trf ;
1985-01-01 22:27:02 +00:00
index= sig-1 ;
trf= vector[index] ;
if ( trf==SIG_IGN ) return 1 ;
if ( sig!=SIGILL && sig!=SIGTRAP ) vector[index]= SIG_IGN ;
if ( trf==SIG_DFL ) return 0 ;
(*trf)(sig) ;
return 1 ;
}