1 // -*- C++ -*-
2 //==============================================================================================
3 //
4 //	This file is part of LiDIA --- a library for computational number theory
5 //
6 //	Copyright (c) 1994--2001 the LiDIA Group.  All rights reserved.
7 //
8 //	See http://www.informatik.tu-darmstadt.de/TI/LiDIA/
9 //
10 //----------------------------------------------------------------------------------------------
11 //
12 //	$Id$
13 //
14 //	Author	: Markus Maurer (MM)
15 //	Changes	: See CVS log
16 //
17 //==============================================================================================
18 
19 
20 #ifndef LIDIA_LIDIA_SIGNAL_H_GUARD_
21 #define LIDIA_LIDIA_SIGNAL_H_GUARD_
22 
23 
24 
25 #ifndef LIDIA_LIDIA_H_GUARD_
26 # include	"LiDIA/LiDIA.h"
27 #endif
28 #include	<signal.h>
29 
30 
31 
32 #ifdef LIDIA_NAMESPACE
33 namespace LiDIA {
34 # define IN_NAMESPACE_LIDIA
35 #endif
36 
37 
38 
39 // defines and typedefs
40 // for signal handling
41 
42 #ifndef SIGHUP
43 #define LIDIA_SIGHUP  1       // hangup
44 #else
45 #define LIDIA_SIGHUP SIGHUP
46 #endif
47 
48 #ifndef SIGINT
49 #define LIDIA_SIGINT  2       // interrupt (rubout)
50 #else
51 #define LIDIA_SIGINT SIGINT
52 #endif
53 
54 #ifndef SIGQUIT
55 #define LIDIA_SIGQUIT 3       // quit (ASCII FS)
56 #else
57 #define LIDIA_SIGQUIT SIGQUIT
58 #endif
59 
60 #ifndef SIGILL
61 #define LIDIA_SIGILL  4       // illegal instruction (not reset when caught)
62 #else
63 #define LIDIA_SIGILL SIGILL
64 #endif
65 
66 #ifndef SIGTRAP
67 #define LIDIA_SIGTRAP 5       // trace trap (not reset when caught)
68 #else
69 #define LIDIA_SIGTRAP SIGTRAP
70 #endif
71 
72 #ifndef SIGIOT
73 #define LIDIA_SIGIOT  6       // IOT instruction
74 #else
75 #define LIDIA_SIGIOT SIGIOT
76 #endif
77 
78 #ifndef SIGABRT
79 #define LIDIA_SIGABRT 6       // used by abort, replace SIGIOT in the future
80 #else
81 #define LIDIA_SIGABRT SIGABRT
82 #endif
83 
84 #ifndef SIGEMT
85 #define LIDIA_SIGEMT  7       // EMT instruction
86 #else
87 #define LIDIA_SIGEMT SIGEMT
88 #endif
89 
90 #ifndef SIGFPE
91 #define LIDIA_SIGFPE  8       // floating point exception
92 #else
93 #define LIDIA_SIGFPE SIGFPE
94 #endif
95 
96 #ifndef SIGKILL
97 #define LIDIA_SIGKILL 9       // kill (cannot be caught or ignored)
98 #else
99 #define LIDIA_SIGKILL SIGKILL
100 #endif
101 
102 #ifndef SIGBUS
103 #define LIDIA_SIGBUS  10      // bus error
104 #else
105 #define LIDIA_SIGBUS  SIGBUS
106 #endif
107 
108 #ifndef SIGSEGV
109 #define LIDIA_SIGSEGV 11      // segmentation violation
110 #else
111 #define LIDIA_SIGSEGV SIGSEGV
112 #endif
113 
114 #ifndef SIGSYS
115 #define LIDIA_SIGSYS  12      // bad argument to system call
116 #else
117 #define LIDIA_SIGSYS SIGSYS
118 #endif
119 
120 #ifndef SIGPIPE
121 #define LIDIA_SIGPIPE 13      // write on a pipe with no one to read it
122 #else
123 #define LIDIA_SIGPIPE SIGPIPE
124 #endif
125 
126 #ifndef SIGALRM
127 #define LIDIA_SIGALRM 14      // alarm clock
128 #else
129 #define LIDIA_SIGALRM SIGALRM
130 #endif
131 
132 #ifndef SIGTERM
133 #define LIDIA_SIGTERM 15      // software termination signal from kill
134 #else
135 #define LIDIA_SIGTERM SIGTERM
136 #endif
137 
138 #ifndef SIGUSR1
139 #define LIDIA_SIGUSR1 16      // user defined signal 1
140 #else
141 #define LIDIA_SIGUSR1 SIGUSR1
142 #endif
143 
144 #ifndef SIGUSR2
145 #define LIDIA_SIGUSR2 17      // user defined signal 2
146 #else
147 #define LIDIA_SIGUSR2 SIGUSR2
148 #endif
149 
150 #ifndef SIGCLD
151 #define LIDIA_SIGCLD  18      // child status change
152 #else
153 #define LIDIA_SIGCLD  SIGCLD
154 #endif
155 
156 #ifndef SIGCHLD
157 #define LIDIA_SIGCHLD 18      // child status change alias (POSIX)
158 #else
159 #define LIDIA_SIGCHLD SIGCHLD
160 #endif
161 
162 #ifndef SIGPWR
163 #define LIDIA_SIGPWR  19      // power-fail restart
164 #else
165 #define LIDIA_SIGPWR  SIGPWR
166 #endif
167 
168 #ifndef SIGWINCH
169 #define LIDIA_SIGWINCH 20     // window size change
170 #else
171 #define LIDIA_SIGWINCH SIGWINCH
172 #endif
173 
174 #ifndef SIGURG
175 #define LIDIA_SIGURG  21      // urgent socket condition
176 #else
177 #define LIDIA_SIGURG SIGURG
178 #endif
179 
180 #ifndef SIGPOLL
181 #define LIDIA_SIGPOLL 22      // pollable event occured
182 #else
183 #define LIDIA_SIGPOLL SIGPOLL
184 #endif
185 
186 #ifndef SIGIO
187 #define LIDIA_SIGIO   LIDIA_SIGPOLL // socket I/O possible (SIGPOLL alias)
188 #endif
189 
190 #ifndef SIGSTOP
191 #define LIDIA_SIGSTOP 23      // stop (cannot be caught or ignored)
192 #else
193 #define LIDIA_SIGSTOP SIGSTOP
194 #endif
195 
196 #ifndef SIGTSTP
197 #define LIDIA_SIGTSTP 24      // user stop requested from tty
198 #else
199 #define LIDIA_SIGTSTP SIGTSTP
200 #endif
201 
202 #ifndef SIGCONT
203 #define LIDIA_SIGCONT 25      // stopped process has been continued
204 #else
205 #define LIDIA_SIGCONT SIGCONT
206 #endif
207 
208 #ifndef SIGTTIN
209 #define LIDIA_SIGTTIN 26      // background tty read attempted
210 #else
211 #define LIDIA_SIGTTIN SIGTTIN
212 #endif
213 
214 #ifndef SIGTTOU
215 #define LIDIA_SIGTTOU 27      // background tty write attempted
216 #else
217 #define LIDIA_SIGTTOU SIGTTOU
218 #endif
219 
220 #ifndef SIGVTALRM
221 #define LIDIA_SIGVTALRM 28    // virtual timer expired
222 #else
223 #define LIDIA_SIGVTALRM SIGVTALRM
224 #endif
225 
226 #ifndef SIGPROF
227 #define LIDIA_SIGPROF 29      // profiling timer expired
228 #else
229 #define LIDIA_SIGPROF SIGPROF
230 #endif
231 
232 #ifndef SIGXCPU
233 #define LIDIA_SIGXCPU 30      // exceeded cpu limit
234 #else
235 #define LIDIA_SIGXCPU SIGXCPU
236 #endif
237 
238 #ifndef SIGXFSZ
239 #define LIDIA_SIGXFSZ 31      // exceeded file size limit
240 #else
241 #define LIDIA_SIGXFSZ SIGXFSZ
242 #endif
243 
244 #ifndef SIGWAITING
245 #define LIDIA_SIGWAITING 32   // process's lwps are blocked
246 #else
247 #define LIDIA_SIGWAITING SIGWAITING
248 #endif
249 
250 
251 // we allow only POSIX
252 #define LIDIA_SIGNAL_FUNCTION(f) extern "C" void f(int)
253 extern "C" { // signal handlers are C functions
254     typedef void (*lidia_signal_handler_t)(int);
255 }
256 
257 
258 class lidia_signal {
259 private:
260 
261 	int signal_num; // the signal, i.e. SIGINT
262 	// avoid name collision with function signal
263 
264 public:
265 
266 	typedef struct {
267 		int sig;
268 		int offset, width;
269 		int sp; // stack pointer
270 		int ss; // stack size
271 		lidia_signal_handler_t* stack;
272 	} signal_stack;
273 
274 private:
275 
276 	static signal_stack signals[];
277 	static const int    default_stack_size;
278 
279 
280 	static void allocate (lidia_signal_handler_t * &s,
281 			      int old_size,
282 			      int new_size);
283 
284 	static void print_stack_index (int index);
285 
286 	static void install_handler (int sig,
287 				     lidia_signal_handler_t handler);
288 	static void uninstall_handler (int sig);
289 
290 
291 public:
292 
293 	lidia_signal(int sig, lidia_signal_handler_t handler);
294 	~lidia_signal();
295 
296 	static void print_stack (int sig);
297 
298 };
299 
300 
301 
302 #ifdef LIDIA_NAMESPACE
303 }	// end of namespace LiDIA
304 # undef IN_NAMESPACE_LIDIA
305 #endif
306 
307 
308 
309 #endif	// LIDIA_LIDIA_SIGNAL_H_GUARD_
310