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