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" |
2091
|
39 #include "pager.h" |
1
|
40 #include "sighandlers.h" |
1352
|
41 #include "syswait.h" |
2091
|
42 #include "toplev.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 |
2016
|
52 // Allow us to save the signal mask and then restore it to the most |
|
53 // recently saved value. This is necessary when using the POSIX |
|
54 // signal handling interface on some systems calling longjmp out of |
|
55 // the signal handler to get to the top level on an interrupt doesn't |
|
56 // restore the original signal mask. Alternatively, we could use |
|
57 // sigsetjmp/siglongjmp, but saving and restoring the signal mask |
|
58 // ourselves works ok and seems simpler just now. |
|
59 |
|
60 #if defined (HAVE_POSIX_SIGNALS) |
|
61 static sigset_t octave_signal_mask; |
|
62 #endif |
|
63 |
|
64 void |
|
65 octave_save_signal_mask (void) |
|
66 { |
|
67 #if defined (HAVE_POSIX_SIGNALS) |
|
68 sigprocmask (0, 0, &octave_signal_mask); |
|
69 #endif |
|
70 } |
|
71 |
|
72 void |
|
73 octave_restore_signal_mask (void) |
|
74 { |
|
75 #if defined (HAVE_POSIX_SIGNALS) |
|
76 sigprocmask (SIG_SETMASK, &octave_signal_mask, 0); |
|
77 #endif |
|
78 } |
|
79 |
1
|
80 static void |
|
81 my_friendly_exit (const char *sig_name, int sig_number) |
|
82 { |
886
|
83 error ("%s -- stopping myself...", sig_name); |
1373
|
84 |
|
85 save_user_variables (); |
|
86 |
1
|
87 clean_up_and_exit (sig_number); |
|
88 } |
|
89 |
635
|
90 // I know, not really a signal handler. |
|
91 |
|
92 static void |
|
93 octave_new_handler (void) |
|
94 { |
1394
|
95 error ("memory exhausted -- trying to return to prompt"); |
|
96 |
|
97 if (can_interrupt) |
|
98 { |
|
99 jump_to_top_level (); |
|
100 panic_impossible (); |
|
101 } |
|
102 else |
1395
|
103 my_friendly_exit ("operator new", 1); |
635
|
104 } |
|
105 |
1446
|
106 sig_handler * |
|
107 octave_set_signal_handler (int sig, sig_handler *handler) |
|
108 { |
|
109 #if defined (HAVE_POSIX_SIGNALS) |
|
110 struct sigaction act, oact; |
|
111 act.sa_handler = handler; |
|
112 act.sa_flags = 0; |
|
113 sigemptyset (&act.sa_mask); |
|
114 sigemptyset (&oact.sa_mask); |
|
115 sigaction (sig, &act, &oact); |
|
116 return oact.sa_handler; |
|
117 #else |
|
118 return signal (sig, handler); |
|
119 #endif |
|
120 } |
|
121 |
1
|
122 static RETSIGTYPE |
1446
|
123 generic_sig_handler (int sig) |
1
|
124 { |
1446
|
125 my_friendly_exit (sys_siglist[sig], sig); |
1
|
126 |
834
|
127 #if RETSIGTYPE == void |
|
128 return; |
|
129 #else |
|
130 return 0; |
|
131 #endif |
1
|
132 } |
|
133 |
2091
|
134 // Handle SIGCHLD. |
1230
|
135 |
2091
|
136 // XXX FIXME XXX -- this should probably be implemented by having a |
|
137 // global list of pids to check and a corresponding list of functions |
|
138 // to call if a pid is recognized. That way, we just have to register |
|
139 // functions elsewhere and this function doesn't have to change. |
|
140 |
1230
|
141 static RETSIGTYPE |
2092
|
142 sigchld_handler (int /* sig */) |
1230
|
143 { |
2092
|
144 // Can this ever cause trouble on systems that don't forget signal |
|
145 // handlers when they are invoked? |
|
146 |
|
147 octave_set_signal_handler (SIGCHLD, sigchld_handler); |
|
148 |
2197
|
149 if (octave_pager_pid > 0) |
1230
|
150 { |
2197
|
151 int status; |
|
152 pid_t pid = waitpid (octave_pager_pid, &status, 0); |
|
153 |
|
154 if (pid > 0) |
1230
|
155 { |
2197
|
156 if (WIFEXITED (status) || WIFSIGNALLED (status)) |
2091
|
157 { |
2197
|
158 octave_pager_pid = -1; |
|
159 |
|
160 // XXX FIXME XXX -- I'm not sure that this is the right |
|
161 // thing to do here, but it seems to work. |
|
162 |
|
163 // Don't call error() here because that tries to flush |
|
164 // pending output, which isn't going to do anything |
|
165 // anyway. |
|
166 |
|
167 cerr << "error: connection to external pager lost --\n"; |
|
168 cerr << "error: pending computations and output have been discarded\n"; |
2091
|
169 } |
1230
|
170 } |
|
171 } |
|
172 } |
|
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 #ifdef SIGCHLD |
1446
|
271 octave_set_signal_handler (SIGCHLD, sigchld_handler); |
1230
|
272 #endif |
|
273 |
1
|
274 #ifdef SIGEMT |
1446
|
275 octave_set_signal_handler (SIGEMT, generic_sig_handler); |
1
|
276 #endif |
|
277 |
|
278 #ifdef SIGFPE |
1373
|
279 #if defined (__alpha__) |
1446
|
280 octave_set_signal_handler (SIGFPE, sigfpe_handler); |
1373
|
281 #else |
1446
|
282 octave_set_signal_handler (SIGFPE, generic_sig_handler); |
1
|
283 #endif |
1373
|
284 #endif |
1
|
285 |
|
286 #ifdef SIGHUP |
1446
|
287 octave_set_signal_handler (SIGHUP, generic_sig_handler); |
1
|
288 #endif |
|
289 |
|
290 #ifdef SIGILL |
1446
|
291 octave_set_signal_handler (SIGILL, generic_sig_handler); |
1
|
292 #endif |
|
293 |
|
294 #ifdef SIGINT |
1446
|
295 octave_set_signal_handler (SIGINT, sigint_handler); |
1
|
296 #endif |
|
297 |
|
298 #ifdef SIGIOT |
1446
|
299 octave_set_signal_handler (SIGIOT, generic_sig_handler); |
1
|
300 #endif |
|
301 |
|
302 #ifdef SIGLOST |
1446
|
303 octave_set_signal_handler (SIGLOST, generic_sig_handler); |
1
|
304 #endif |
|
305 |
|
306 #ifdef SIGPIPE |
1446
|
307 octave_set_signal_handler (SIGPIPE, sigpipe_handler); |
1
|
308 #endif |
|
309 |
|
310 #ifdef SIGPOLL |
1446
|
311 octave_set_signal_handler (SIGPOLL, SIG_IGN); |
1
|
312 #endif |
|
313 |
|
314 #ifdef SIGPROF |
1446
|
315 octave_set_signal_handler (SIGPROF, generic_sig_handler); |
1
|
316 #endif |
|
317 |
|
318 #ifdef SIGQUIT |
1446
|
319 octave_set_signal_handler (SIGQUIT, generic_sig_handler); |
1
|
320 #endif |
|
321 |
|
322 #ifdef SIGSEGV |
1446
|
323 octave_set_signal_handler (SIGSEGV, generic_sig_handler); |
1
|
324 #endif |
|
325 |
|
326 #ifdef SIGSYS |
1446
|
327 octave_set_signal_handler (SIGSYS, generic_sig_handler); |
1
|
328 #endif |
|
329 |
|
330 #ifdef SIGTERM |
1446
|
331 octave_set_signal_handler (SIGTERM, generic_sig_handler); |
1
|
332 #endif |
|
333 |
|
334 #ifdef SIGTRAP |
1446
|
335 octave_set_signal_handler (SIGTRAP, generic_sig_handler); |
1
|
336 #endif |
|
337 |
|
338 #ifdef SIGUSR1 |
1446
|
339 octave_set_signal_handler (SIGUSR1, generic_sig_handler); |
1
|
340 #endif |
|
341 |
|
342 #ifdef SIGUSR2 |
1446
|
343 octave_set_signal_handler (SIGUSR2, generic_sig_handler); |
1
|
344 #endif |
|
345 |
|
346 #ifdef SIGVTALRM |
1446
|
347 octave_set_signal_handler (SIGVTALRM, generic_sig_handler); |
1
|
348 #endif |
|
349 |
895
|
350 #ifdef SIGIO |
1446
|
351 octave_set_signal_handler (SIGIO, SIG_IGN); |
895
|
352 #endif |
|
353 |
1
|
354 #ifdef SIGXCPU |
1446
|
355 octave_set_signal_handler (SIGXCPU, generic_sig_handler); |
1
|
356 #endif |
|
357 |
|
358 #ifdef SIGXFSZ |
1446
|
359 octave_set_signal_handler (SIGXFSZ, generic_sig_handler); |
1
|
360 #endif |
|
361 } |
|
362 |
886
|
363 #ifndef HAVE_SYS_SIGLIST |
839
|
364 char *sys_siglist[NSIG + 1] = |
|
365 { |
|
366 #ifdef AIX |
|
367 /* AIX has changed the signals a bit */ |
|
368 "bogus signal", /* 0 */ |
|
369 "hangup", /* 1 SIGHUP */ |
|
370 "interrupt", /* 2 SIGINT */ |
|
371 "quit", /* 3 SIGQUIT */ |
|
372 "illegal instruction", /* 4 SIGILL */ |
|
373 "trace trap", /* 5 SIGTRAP */ |
|
374 "IOT instruction", /* 6 SIGIOT */ |
|
375 "crash likely", /* 7 SIGDANGER */ |
|
376 "floating point exception", /* 8 SIGFPE */ |
|
377 "kill", /* 9 SIGKILL */ |
|
378 "bus error", /* 10 SIGBUS */ |
|
379 "segmentation violation", /* 11 SIGSEGV */ |
|
380 "bad argument to system call", /* 12 SIGSYS */ |
|
381 "write on a pipe with no one to read it", /* 13 SIGPIPE */ |
|
382 "alarm clock", /* 14 SIGALRM */ |
|
383 "software termination signum", /* 15 SIGTERM */ |
|
384 "user defined signal 1", /* 16 SIGUSR1 */ |
|
385 "user defined signal 2", /* 17 SIGUSR2 */ |
|
386 "death of a child", /* 18 SIGCLD */ |
|
387 "power-fail restart", /* 19 SIGPWR */ |
|
388 "bogus signal", /* 20 */ |
|
389 "bogus signal", /* 21 */ |
|
390 "bogus signal", /* 22 */ |
|
391 "bogus signal", /* 23 */ |
|
392 "bogus signal", /* 24 */ |
|
393 "LAN I/O interrupt", /* 25 SIGAIO */ |
|
394 "PTY I/O interrupt", /* 26 SIGPTY */ |
|
395 "I/O intervention required", /* 27 SIGIOINT */ |
|
396 "HFT grant", /* 28 SIGGRANT */ |
|
397 "HFT retract", /* 29 SIGRETRACT */ |
|
398 "HFT sound done", /* 30 SIGSOUND */ |
|
399 "HFT input ready", /* 31 SIGMSG */ |
|
400 #else /* not AIX */ |
|
401 "bogus signal", /* 0 */ |
|
402 "hangup", /* 1 SIGHUP */ |
|
403 "interrupt", /* 2 SIGINT */ |
|
404 "quit", /* 3 SIGQUIT */ |
|
405 "illegal instruction", /* 4 SIGILL */ |
|
406 "trace trap", /* 5 SIGTRAP */ |
|
407 "IOT instruction", /* 6 SIGIOT */ |
|
408 "EMT instruction", /* 7 SIGEMT */ |
|
409 "floating point exception", /* 8 SIGFPE */ |
|
410 "kill", /* 9 SIGKILL */ |
|
411 "bus error", /* 10 SIGBUS */ |
|
412 "segmentation violation", /* 11 SIGSEGV */ |
|
413 "bad argument to system call", /* 12 SIGSYS */ |
|
414 "write on a pipe with no one to read it", /* 13 SIGPIPE */ |
|
415 "alarm clock", /* 14 SIGALRM */ |
|
416 "software termination signum", /* 15 SIGTERM */ |
|
417 "user defined signal 1", /* 16 SIGUSR1 */ |
|
418 "user defined signal 2", /* 17 SIGUSR2 */ |
|
419 "death of a child", /* 18 SIGCLD */ |
|
420 "power-fail restart", /* 19 SIGPWR */ |
|
421 #ifdef sun |
|
422 "window size change", /* 20 SIGWINCH */ |
|
423 "urgent socket condition", /* 21 SIGURG */ |
|
424 "pollable event occured", /* 22 SIGPOLL */ |
|
425 "stop (cannot be caught or ignored)", /* 23 SIGSTOP */ |
|
426 "user stop requested from tty", /* 24 SIGTSTP */ |
|
427 "stopped process has been continued", /* 25 SIGCONT */ |
|
428 "background tty read attempted", /* 26 SIGTTIN */ |
|
429 "background tty write attempted", /* 27 SIGTTOU */ |
|
430 "virtual timer expired", /* 28 SIGVTALRM */ |
|
431 "profiling timer expired", /* 29 SIGPROF */ |
|
432 "exceeded cpu limit", /* 30 SIGXCPU */ |
|
433 "exceeded file size limit", /* 31 SIGXFSZ */ |
|
434 "process's lwps are blocked", /* 32 SIGWAITING */ |
|
435 "special signal used by thread library", /* 33 SIGLWP */ |
|
436 #ifdef SIGFREEZE |
|
437 "Special Signal Used By CPR", /* 34 SIGFREEZE */ |
|
438 #endif |
|
439 #ifdef SIGTHAW |
|
440 "Special Signal Used By CPR", /* 35 SIGTHAW */ |
|
441 #endif |
|
442 #endif /* sun */ |
|
443 #endif /* not AIX */ |
|
444 0 |
|
445 }; |
|
446 #endif |
|
447 |
1
|
448 /* |
|
449 ;;; Local Variables: *** |
|
450 ;;; mode: C++ *** |
|
451 ;;; End: *** |
|
452 */ |