1
|
1 // sighandlers.cc -*- C++ -*- |
|
2 /* |
|
3 |
1009
|
4 Copyright (C) 1992, 1993, 1994, 1995 John W. Eaton |
1
|
5 |
|
6 This file is part of Octave. |
|
7 |
|
8 Octave is free software; you can redistribute it and/or modify it |
|
9 under the terms of the GNU General Public License as published by the |
|
10 Free Software Foundation; either version 2, or (at your option) any |
|
11 later version. |
|
12 |
|
13 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
16 for more details. |
|
17 |
|
18 You should have received a copy of the GNU General Public License |
|
19 along with Octave; see the file COPYING. If not, write to the Free |
1315
|
20 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
1
|
21 |
|
22 */ |
|
23 |
240
|
24 #ifdef HAVE_CONFIG_H |
1230
|
25 #include <config.h> |
1
|
26 #endif |
|
27 |
1343
|
28 #include <csignal> |
1344
|
29 #include <new> |
|
30 |
1350
|
31 #include <iostream.h> |
|
32 |
|
33 #ifdef HAVE_UNISTD_H |
834
|
34 #include <sys/types.h> |
|
35 #include <unistd.h> |
|
36 #endif |
1343
|
37 |
1352
|
38 #include "error.h" |
1373
|
39 #include "load-save.h" |
1670
|
40 #include "toplev.h" |
1
|
41 #include "sighandlers.h" |
1352
|
42 #include "syswait.h" |
1
|
43 #include "utils.h" |
|
44 |
|
45 // Nonzero means we have already printed a message for this series of |
|
46 // SIGPIPES. We assume that the writer will eventually give up. |
|
47 int pipe_handler_error_count = 0; |
|
48 |
|
49 // Nonzero means we can be interrupted. |
|
50 int can_interrupt = 0; |
|
51 |
|
52 static void |
|
53 my_friendly_exit (const char *sig_name, int sig_number) |
|
54 { |
886
|
55 error ("%s -- stopping myself...", sig_name); |
1373
|
56 |
|
57 save_user_variables (); |
|
58 |
1
|
59 clean_up_and_exit (sig_number); |
|
60 } |
|
61 |
635
|
62 // I know, not really a signal handler. |
|
63 |
|
64 static void |
|
65 octave_new_handler (void) |
|
66 { |
1394
|
67 error ("memory exhausted -- trying to return to prompt"); |
|
68 |
|
69 if (can_interrupt) |
|
70 { |
|
71 jump_to_top_level (); |
|
72 panic_impossible (); |
|
73 } |
|
74 else |
1395
|
75 my_friendly_exit ("operator new", 1); |
635
|
76 } |
|
77 |
1446
|
78 sig_handler * |
|
79 octave_set_signal_handler (int sig, sig_handler *handler) |
|
80 { |
|
81 #if defined (HAVE_POSIX_SIGNALS) |
|
82 struct sigaction act, oact; |
|
83 act.sa_handler = handler; |
|
84 act.sa_flags = 0; |
|
85 sigemptyset (&act.sa_mask); |
|
86 sigemptyset (&oact.sa_mask); |
|
87 sigaction (sig, &act, &oact); |
|
88 return oact.sa_handler; |
|
89 #else |
|
90 return signal (sig, handler); |
|
91 #endif |
|
92 } |
|
93 |
1
|
94 static RETSIGTYPE |
1446
|
95 generic_sig_handler (int sig) |
1
|
96 { |
1446
|
97 my_friendly_exit (sys_siglist[sig], sig); |
1
|
98 |
834
|
99 #if RETSIGTYPE == void |
|
100 return; |
|
101 #else |
|
102 return 0; |
|
103 #endif |
1
|
104 } |
|
105 |
1230
|
106 // Handle SIGCHLD. Should use waitpid and ignore stopped jobs. |
|
107 // Needs to restore state of plotter such that it will be restarted |
|
108 // again when needed. Needs to close file descriptors corresponding |
|
109 // to processes started with execute(). |
|
110 |
|
111 #if 0 |
|
112 static RETSIGTYPE |
1446
|
113 sigchld_handler (int sig) |
1230
|
114 { |
|
115 int status; |
|
116 pid_t pid = wait (&status); |
|
117 |
|
118 if (pid < 0) |
|
119 cerr << "wait error\n"; |
|
120 else |
|
121 { |
|
122 cerr << "sigchld caught, PID = " << pid << "; status: "; |
|
123 |
|
124 int lo_byte = (status & 0xff); |
|
125 int hi_byte = ((status >> 8) & 0xff); |
|
126 if (lo_byte == 0177) |
|
127 { |
|
128 cerr << "stopped with signal = " << hi_byte << "\n"; |
|
129 } |
|
130 else if (lo_byte) |
|
131 { |
|
132 int sig_num = (lo_byte & 0x7f); |
|
133 cerr << "stopped with signal = " << sig_num << "\n"; |
|
134 if (lo_byte & 0200) |
|
135 cerr << "child dumped core\n"; |
|
136 } |
|
137 else |
|
138 { |
|
139 cerr << "exited with status = " << hi_byte << "\n"; |
|
140 } |
|
141 } |
|
142 |
1446
|
143 octave_set_signal_handler (SIGCHLD, sigchld_handler); |
1230
|
144 } |
|
145 #endif |
|
146 |
1373
|
147 #if defined (__alpha__) |
|
148 static RETSIGTYPE |
1488
|
149 sigfpe_handler (int /* sig */) |
1373
|
150 { |
|
151 // Can this ever cause trouble on systems that don't forget signal |
|
152 // handlers when they are invoked? |
|
153 |
1446
|
154 octave_set_signal_handler (SIGFPE, sigfpe_handler); |
1373
|
155 |
1394
|
156 error ("floating point exception -- trying to return to prompt"); |
1373
|
157 |
|
158 if (can_interrupt) |
|
159 { |
|
160 jump_to_top_level (); |
|
161 panic_impossible (); |
|
162 } |
|
163 |
|
164 #if RETSIGTYPE == void |
|
165 return; |
|
166 #else |
|
167 return 0; |
|
168 #endif |
|
169 } |
|
170 #endif |
|
171 |
635
|
172 // Handle SIGINT by restarting the parser (see octave.cc). |
|
173 |
1
|
174 static RETSIGTYPE |
1488
|
175 sigint_handler (int /* sig */) |
1
|
176 { |
1358
|
177 // Can this ever cause trouble on systems that don't forget signal |
|
178 // handlers when they are invoked? |
834
|
179 |
1446
|
180 octave_set_signal_handler (SIGINT, sigint_handler); |
834
|
181 |
1
|
182 if (can_interrupt) |
|
183 { |
|
184 jump_to_top_level (); |
|
185 panic_impossible (); |
|
186 } |
|
187 |
|
188 #if RETSIGTYPE == void |
|
189 return; |
|
190 #else |
|
191 return 0; |
|
192 #endif |
|
193 } |
|
194 |
|
195 static RETSIGTYPE |
1488
|
196 sigpipe_handler (int /* sig */) |
1
|
197 { |
1358
|
198 // Can this ever cause trouble on systems that don't forget signal |
|
199 // handlers when they are invoked? |
834
|
200 |
1446
|
201 octave_set_signal_handler (SIGPIPE, sigpipe_handler); |
834
|
202 |
1
|
203 if (pipe_handler_error_count++ == 0) |
529
|
204 message (0, "broken pipe"); |
1
|
205 |
1358
|
206 // Don't loop forever on account of this. |
|
207 |
1
|
208 if (pipe_handler_error_count > 100) |
|
209 jump_to_top_level (); |
|
210 |
|
211 #if RETSIGTYPE == void |
|
212 return; |
|
213 #else |
|
214 return 0; |
|
215 #endif |
|
216 } |
|
217 |
1651
|
218 void |
|
219 catch_interrupts (void) |
|
220 { |
|
221 octave_set_signal_handler (SIGINT, sigint_handler); |
|
222 } |
|
223 |
635
|
224 // Install all the handlers for the signals we might care about. |
|
225 |
1
|
226 void |
|
227 install_signal_handlers (void) |
|
228 { |
635
|
229 set_new_handler (octave_new_handler); |
|
230 |
1
|
231 #ifdef SIGABRT |
1446
|
232 octave_set_signal_handler (SIGABRT, generic_sig_handler); |
1
|
233 #endif |
|
234 |
|
235 #ifdef SIGALRM |
1446
|
236 octave_set_signal_handler (SIGALRM, generic_sig_handler); |
1
|
237 #endif |
|
238 |
|
239 #ifdef SIGBUS |
1446
|
240 octave_set_signal_handler (SIGBUS, generic_sig_handler); |
1
|
241 #endif |
|
242 |
1230
|
243 #if 0 |
|
244 #ifdef SIGCHLD |
1446
|
245 octave_set_signal_handler (SIGCHLD, sigchld_handler); |
1230
|
246 #endif |
|
247 #endif |
|
248 |
1
|
249 #ifdef SIGEMT |
1446
|
250 octave_set_signal_handler (SIGEMT, generic_sig_handler); |
1
|
251 #endif |
|
252 |
|
253 #ifdef SIGFPE |
1373
|
254 #if defined (__alpha__) |
1446
|
255 octave_set_signal_handler (SIGFPE, sigfpe_handler); |
1373
|
256 #else |
1446
|
257 octave_set_signal_handler (SIGFPE, generic_sig_handler); |
1
|
258 #endif |
1373
|
259 #endif |
1
|
260 |
|
261 #ifdef SIGHUP |
1446
|
262 octave_set_signal_handler (SIGHUP, generic_sig_handler); |
1
|
263 #endif |
|
264 |
|
265 #ifdef SIGILL |
1446
|
266 octave_set_signal_handler (SIGILL, generic_sig_handler); |
1
|
267 #endif |
|
268 |
|
269 #ifdef SIGINT |
1446
|
270 octave_set_signal_handler (SIGINT, sigint_handler); |
1
|
271 #endif |
|
272 |
|
273 #ifdef SIGIOT |
1446
|
274 octave_set_signal_handler (SIGIOT, generic_sig_handler); |
1
|
275 #endif |
|
276 |
|
277 #ifdef SIGLOST |
1446
|
278 octave_set_signal_handler (SIGLOST, generic_sig_handler); |
1
|
279 #endif |
|
280 |
|
281 #ifdef SIGPIPE |
1446
|
282 octave_set_signal_handler (SIGPIPE, sigpipe_handler); |
1
|
283 #endif |
|
284 |
|
285 #ifdef SIGPOLL |
1446
|
286 octave_set_signal_handler (SIGPOLL, SIG_IGN); |
1
|
287 #endif |
|
288 |
|
289 #ifdef SIGPROF |
1446
|
290 octave_set_signal_handler (SIGPROF, generic_sig_handler); |
1
|
291 #endif |
|
292 |
|
293 #ifdef SIGQUIT |
1446
|
294 octave_set_signal_handler (SIGQUIT, generic_sig_handler); |
1
|
295 #endif |
|
296 |
|
297 #ifdef SIGSEGV |
1446
|
298 octave_set_signal_handler (SIGSEGV, generic_sig_handler); |
1
|
299 #endif |
|
300 |
|
301 #ifdef SIGSYS |
1446
|
302 octave_set_signal_handler (SIGSYS, generic_sig_handler); |
1
|
303 #endif |
|
304 |
|
305 #ifdef SIGTERM |
1446
|
306 octave_set_signal_handler (SIGTERM, generic_sig_handler); |
1
|
307 #endif |
|
308 |
|
309 #ifdef SIGTRAP |
1446
|
310 octave_set_signal_handler (SIGTRAP, generic_sig_handler); |
1
|
311 #endif |
|
312 |
|
313 #ifdef SIGUSR1 |
1446
|
314 octave_set_signal_handler (SIGUSR1, generic_sig_handler); |
1
|
315 #endif |
|
316 |
|
317 #ifdef SIGUSR2 |
1446
|
318 octave_set_signal_handler (SIGUSR2, generic_sig_handler); |
1
|
319 #endif |
|
320 |
|
321 #ifdef SIGVTALRM |
1446
|
322 octave_set_signal_handler (SIGVTALRM, generic_sig_handler); |
1
|
323 #endif |
|
324 |
895
|
325 #ifdef SIGIO |
1446
|
326 octave_set_signal_handler (SIGIO, SIG_IGN); |
895
|
327 #endif |
|
328 |
1
|
329 #ifdef SIGXCPU |
1446
|
330 octave_set_signal_handler (SIGXCPU, generic_sig_handler); |
1
|
331 #endif |
|
332 |
|
333 #ifdef SIGXFSZ |
1446
|
334 octave_set_signal_handler (SIGXFSZ, generic_sig_handler); |
1
|
335 #endif |
|
336 } |
|
337 |
886
|
338 #ifndef HAVE_SYS_SIGLIST |
839
|
339 char *sys_siglist[NSIG + 1] = |
|
340 { |
|
341 #ifdef AIX |
|
342 /* AIX has changed the signals a bit */ |
|
343 "bogus signal", /* 0 */ |
|
344 "hangup", /* 1 SIGHUP */ |
|
345 "interrupt", /* 2 SIGINT */ |
|
346 "quit", /* 3 SIGQUIT */ |
|
347 "illegal instruction", /* 4 SIGILL */ |
|
348 "trace trap", /* 5 SIGTRAP */ |
|
349 "IOT instruction", /* 6 SIGIOT */ |
|
350 "crash likely", /* 7 SIGDANGER */ |
|
351 "floating point exception", /* 8 SIGFPE */ |
|
352 "kill", /* 9 SIGKILL */ |
|
353 "bus error", /* 10 SIGBUS */ |
|
354 "segmentation violation", /* 11 SIGSEGV */ |
|
355 "bad argument to system call", /* 12 SIGSYS */ |
|
356 "write on a pipe with no one to read it", /* 13 SIGPIPE */ |
|
357 "alarm clock", /* 14 SIGALRM */ |
|
358 "software termination signum", /* 15 SIGTERM */ |
|
359 "user defined signal 1", /* 16 SIGUSR1 */ |
|
360 "user defined signal 2", /* 17 SIGUSR2 */ |
|
361 "death of a child", /* 18 SIGCLD */ |
|
362 "power-fail restart", /* 19 SIGPWR */ |
|
363 "bogus signal", /* 20 */ |
|
364 "bogus signal", /* 21 */ |
|
365 "bogus signal", /* 22 */ |
|
366 "bogus signal", /* 23 */ |
|
367 "bogus signal", /* 24 */ |
|
368 "LAN I/O interrupt", /* 25 SIGAIO */ |
|
369 "PTY I/O interrupt", /* 26 SIGPTY */ |
|
370 "I/O intervention required", /* 27 SIGIOINT */ |
|
371 "HFT grant", /* 28 SIGGRANT */ |
|
372 "HFT retract", /* 29 SIGRETRACT */ |
|
373 "HFT sound done", /* 30 SIGSOUND */ |
|
374 "HFT input ready", /* 31 SIGMSG */ |
|
375 #else /* not AIX */ |
|
376 "bogus signal", /* 0 */ |
|
377 "hangup", /* 1 SIGHUP */ |
|
378 "interrupt", /* 2 SIGINT */ |
|
379 "quit", /* 3 SIGQUIT */ |
|
380 "illegal instruction", /* 4 SIGILL */ |
|
381 "trace trap", /* 5 SIGTRAP */ |
|
382 "IOT instruction", /* 6 SIGIOT */ |
|
383 "EMT instruction", /* 7 SIGEMT */ |
|
384 "floating point exception", /* 8 SIGFPE */ |
|
385 "kill", /* 9 SIGKILL */ |
|
386 "bus error", /* 10 SIGBUS */ |
|
387 "segmentation violation", /* 11 SIGSEGV */ |
|
388 "bad argument to system call", /* 12 SIGSYS */ |
|
389 "write on a pipe with no one to read it", /* 13 SIGPIPE */ |
|
390 "alarm clock", /* 14 SIGALRM */ |
|
391 "software termination signum", /* 15 SIGTERM */ |
|
392 "user defined signal 1", /* 16 SIGUSR1 */ |
|
393 "user defined signal 2", /* 17 SIGUSR2 */ |
|
394 "death of a child", /* 18 SIGCLD */ |
|
395 "power-fail restart", /* 19 SIGPWR */ |
|
396 #ifdef sun |
|
397 "window size change", /* 20 SIGWINCH */ |
|
398 "urgent socket condition", /* 21 SIGURG */ |
|
399 "pollable event occured", /* 22 SIGPOLL */ |
|
400 "stop (cannot be caught or ignored)", /* 23 SIGSTOP */ |
|
401 "user stop requested from tty", /* 24 SIGTSTP */ |
|
402 "stopped process has been continued", /* 25 SIGCONT */ |
|
403 "background tty read attempted", /* 26 SIGTTIN */ |
|
404 "background tty write attempted", /* 27 SIGTTOU */ |
|
405 "virtual timer expired", /* 28 SIGVTALRM */ |
|
406 "profiling timer expired", /* 29 SIGPROF */ |
|
407 "exceeded cpu limit", /* 30 SIGXCPU */ |
|
408 "exceeded file size limit", /* 31 SIGXFSZ */ |
|
409 "process's lwps are blocked", /* 32 SIGWAITING */ |
|
410 "special signal used by thread library", /* 33 SIGLWP */ |
|
411 #ifdef SIGFREEZE |
|
412 "Special Signal Used By CPR", /* 34 SIGFREEZE */ |
|
413 #endif |
|
414 #ifdef SIGTHAW |
|
415 "Special Signal Used By CPR", /* 35 SIGTHAW */ |
|
416 #endif |
|
417 #endif /* sun */ |
|
418 #endif /* not AIX */ |
|
419 0 |
|
420 }; |
|
421 #endif |
|
422 |
1
|
423 /* |
|
424 ;;; Local Variables: *** |
|
425 ;;; mode: C++ *** |
|
426 ;;; page-delimiter: "^/\\*" *** |
|
427 ;;; End: *** |
|
428 */ |