aboutsummaryrefslogtreecommitdiff
path: root/src/lib9/notify.c
blob: 7e3c04f72b58b2c87ca33d0cf58861a8f383d14c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include <signal.h>

#include <u.h>
#define NOPLAN9DEFINES
#include <libc.h>
#include "9proc.h"

extern char *_p9sigstr(int, char*);

static int sigs[] = {
	SIGHUP,
	SIGINT,
	SIGQUIT,
	SIGILL,
	SIGTRAP,
	SIGABRT,
#ifdef SIGEMT
	SIGEMT,
#endif
	SIGFPE,
	SIGBUS,
	SIGSEGV,
	SIGSYS,
	SIGPIPE,
	SIGALRM,
	SIGTERM,
	SIGTSTP,
	SIGTTIN,
	SIGTTOU,
	SIGXCPU,
	SIGXFSZ,
	SIGVTALRM,
	SIGUSR1,
	SIGUSR2,
#ifdef SIGINFO
	SIGINFO,
#endif
};

static void (*notifyf)(void*, char*);

static void
notifysigf(int sig)
{
	int v;
	char tmp[64];
	Uproc *up;

	up = _p9uproc();
	v = p9setjmp(up->notejb);
	if(v == 0 && notifyf)
		(*notifyf)(nil, _p9sigstr(sig, tmp));
	else if(v == 2){
if(0)print("HANDLED %d\n", sig);
		return;
	}
if(0)print("DEFAULT %d\n", sig);
	signal(sig, SIG_DFL);
	kill(getpid(), sig);
}
	
int
notify(void (*f)(void*, char*))
{
	int i;
	void (*sf)(int);

	if(f == nil)
		sf = SIG_DFL;
	else{
		notifyf = f;
		sf = notifysigf;
	}
	for(i=0; i<nelem(sigs); i++)
		signal(sigs[i], sf);
	return 0;
}

int
noted(int v)
{
	Uproc *up;

	up = _p9uproc();
	p9longjmp(up->notejb, v==NCONT ? 2 : 1);
	abort();
	return 0;
}