Mercurial > octave
annotate src/sighandlers.cc @ 10182:0522a65bcd56
assume unistd.h and sys/types.h exist
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Thu, 21 Jan 2010 15:41:19 -0500 |
parents | cd96d29c5efa |
children | d4f813c3f5ed |
rev | line source |
---|---|
1 | 1 /* |
2 | |
7017 | 3 Copyright (C) 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2002, |
8920 | 4 2003, 2004, 2005, 2006, 2007, 2008, 2009 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 | |
7016 | 10 Free Software Foundation; either version 3 of the License, or (at your |
11 option) any later version. | |
1 | 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 | |
7016 | 19 along with Octave; see the file COPYING. If not, see |
20 <http://www.gnu.org/licenses/>. | |
1 | 21 |
22 */ | |
23 | |
240 | 24 #ifdef HAVE_CONFIG_H |
1230 | 25 #include <config.h> |
1 | 26 #endif |
27 | |
2536 | 28 #include <cstdlib> |
29 | |
3503 | 30 #include <iostream> |
1344 | 31 #include <new> |
32 | |
834 | 33 #include <sys/types.h> |
34 #include <unistd.h> | |
1343 | 35 |
3281 | 36 #include "cmd-edit.h" |
5453 | 37 #include "oct-syscalls.h" |
4153 | 38 #include "quit.h" |
3281 | 39 |
8658
73c4516fae10
New evaluator and debugger derived from tree-walker class
John W. Eaton <jwe@octave.org>
parents:
7481
diff
changeset
|
40 #include "debug.h" |
4185 | 41 #include "defun.h" |
1352 | 42 #include "error.h" |
1373 | 43 #include "load-save.h" |
4294 | 44 #include "oct-map.h" |
2091 | 45 #include "pager.h" |
4185 | 46 #include "pt-bp.h" |
8658
73c4516fae10
New evaluator and debugger derived from tree-walker class
John W. Eaton <jwe@octave.org>
parents:
7481
diff
changeset
|
47 #include "pt-eval.h" |
1 | 48 #include "sighandlers.h" |
4787 | 49 #include "sysdep.h" |
1352 | 50 #include "syswait.h" |
2091 | 51 #include "toplev.h" |
1 | 52 #include "utils.h" |
5794 | 53 #include "variables.h" |
1 | 54 |
55 // Nonzero means we have already printed a message for this series of | |
56 // SIGPIPES. We assume that the writer will eventually give up. | |
57 int pipe_handler_error_count = 0; | |
58 | |
3018 | 59 // TRUE means we can be interrupted. |
60 bool can_interrupt = false; | |
1 | 61 |
4185 | 62 // TRUE means we should try to enter the debugger on SIGINT. |
63 static bool Vdebug_on_interrupt = false; | |
64 | |
4449 | 65 // Allow users to avoid writing octave-core for SIGHUP (sent by |
66 // closing gnome-terminal, for example). Note that this variable has | |
67 // no effect if Vcrash_dumps_octave_core is FALSE. | |
5794 | 68 static bool Vsighup_dumps_octave_core = true; |
4449 | 69 |
70 // Similar to Vsighup_dumps_octave_core, but for SIGTERM signal. | |
5794 | 71 static bool Vsigterm_dumps_octave_core = true; |
4449 | 72 |
6098 | 73 #if defined (RETSIGTYPE_IS_VOID) |
2469 | 74 #define SIGHANDLER_RETURN(status) return |
75 #else | |
76 #define SIGHANDLER_RETURN(status) return status | |
77 #endif | |
78 | |
2554 | 79 #if defined (MUST_REINSTALL_SIGHANDLERS) |
80 #define MAYBE_REINSTALL_SIGHANDLER(sig, handler) \ | |
81 octave_set_signal_handler (sig, handler) | |
2931 | 82 #define REINSTALL_USES_SIG 1 |
2554 | 83 #else |
84 #define MAYBE_REINSTALL_SIGHANDLER(sig, handler) \ | |
85 do { } while (0) | |
86 #endif | |
87 | |
88 #if defined (__EMX__) | |
2626 | 89 #define MAYBE_ACK_SIGNAL(sig) \ |
2554 | 90 octave_set_signal_handler (sig, SIG_ACK) |
2931 | 91 #define ACK_USES_SIG 1 |
2554 | 92 #else |
2626 | 93 #define MAYBE_ACK_SIGNAL(sig) \ |
2554 | 94 do { } while (0) |
95 #endif | |
96 | |
5142 | 97 // List of signals we have caught since last call to octave_signal_handler. |
98 static bool octave_signals_caught[NSIG]; | |
99 | |
10142
829e69ec3110
make OCTAVE_QUIT a function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
100 // Called from octave_quit () to actually do something about the signals |
5142 | 101 // we have caught. |
102 | |
103 void | |
104 octave_signal_handler (void) | |
105 { | |
106 // The list of signals is relatively short, so we will just go | |
107 // linearly through the list. | |
108 | |
109 for (int i = 0; i < NSIG; i++) | |
110 { | |
111 if (octave_signals_caught[i]) | |
112 { | |
113 octave_signals_caught[i] = false; | |
114 | |
115 switch (i) | |
116 { | |
5451 | 117 #ifdef SIGCHLD |
5142 | 118 case SIGCHLD: |
119 octave_child_list::reap (); | |
120 break; | |
5451 | 121 #endif |
5142 | 122 |
123 case SIGFPE: | |
124 std::cerr << "warning: floating point exception -- trying to return to prompt" << std::endl; | |
125 break; | |
126 | |
5451 | 127 #ifdef SIGPIPE |
5142 | 128 case SIGPIPE: |
129 std::cerr << "warning: broken pipe -- some output may be lost" << std::endl; | |
130 break; | |
5451 | 131 #endif |
5142 | 132 } |
133 } | |
134 } | |
135 } | |
136 | |
1 | 137 static void |
4787 | 138 my_friendly_exit (const char *sig_name, int sig_number, |
139 bool save_vars = true) | |
1 | 140 { |
2536 | 141 static bool been_there_done_that = false; |
142 | |
143 if (been_there_done_that) | |
144 { | |
2554 | 145 #if defined (SIGABRT) |
2536 | 146 octave_set_signal_handler (SIGABRT, SIG_DFL); |
147 #endif | |
148 | |
3644 | 149 std::cerr << "panic: attempted clean up apparently failed -- aborting...\n"; |
5451 | 150 |
151 MINGW_SIGNAL_CLEANUP (); | |
152 | |
2536 | 153 abort (); |
154 } | |
155 else | |
156 { | |
157 been_there_done_that = true; | |
1373 | 158 |
3644 | 159 std::cerr << "panic: " << sig_name << " -- stopping myself...\n"; |
2536 | 160 |
4787 | 161 if (save_vars) |
4791 | 162 dump_octave_core (); |
2536 | 163 |
3162 | 164 if (sig_number < 0) |
5451 | 165 { |
166 MINGW_SIGNAL_CLEANUP (); | |
167 | |
168 exit (1); | |
169 } | |
3162 | 170 else |
171 { | |
172 octave_set_signal_handler (sig_number, SIG_DFL); | |
173 | |
4092 | 174 #if defined (HAVE_RAISE) |
175 raise (sig_number); | |
176 #elif defined (HAVE_KILL) | |
3162 | 177 kill (getpid (), sig_number); |
4092 | 178 #else |
179 exit (1); | |
180 #endif | |
3162 | 181 } |
4092 | 182 |
2536 | 183 } |
1 | 184 } |
185 | |
1446 | 186 sig_handler * |
5770 | 187 octave_set_signal_handler (int sig, sig_handler *handler, |
188 bool restart_syscalls) | |
1446 | 189 { |
190 #if defined (HAVE_POSIX_SIGNALS) | |
191 struct sigaction act, oact; | |
5144 | 192 |
1446 | 193 act.sa_handler = handler; |
194 act.sa_flags = 0; | |
5144 | 195 |
196 if (sig == SIGALRM) | |
197 { | |
198 #if defined (SA_INTERRUPT) | |
199 act.sa_flags |= SA_INTERRUPT; | |
200 #endif | |
201 } | |
202 #if defined (SA_RESTART) | |
5775 | 203 // FIXME -- Do we also need to explicitly disable SA_RESTART? |
5770 | 204 else if (restart_syscalls) |
205 act.sa_flags |= SA_RESTART; | |
5144 | 206 #endif |
207 | |
1446 | 208 sigemptyset (&act.sa_mask); |
209 sigemptyset (&oact.sa_mask); | |
5144 | 210 |
1446 | 211 sigaction (sig, &act, &oact); |
5144 | 212 |
1446 | 213 return oact.sa_handler; |
214 #else | |
215 return signal (sig, handler); | |
216 #endif | |
217 } | |
218 | |
1 | 219 static RETSIGTYPE |
1446 | 220 generic_sig_handler (int sig) |
1 | 221 { |
5844 | 222 my_friendly_exit (strsignal (sig), sig); |
1 | 223 |
2469 | 224 SIGHANDLER_RETURN (0); |
1 | 225 } |
226 | |
2091 | 227 // Handle SIGCHLD. |
1230 | 228 |
4064 | 229 #ifdef SIGCHLD |
1230 | 230 static RETSIGTYPE |
2092 | 231 sigchld_handler (int /* sig */) |
1230 | 232 { |
2705 | 233 volatile octave_interrupt_handler saved_interrupt_handler |
2554 | 234 = octave_ignore_interrupts (); |
235 | |
2626 | 236 // I wonder if this is really right, or if SIGCHLD should just be |
237 // blocked on OS/2 systems the same as for systems with POSIX signal | |
238 // functions. | |
239 | |
240 #if defined (__EMX__) | |
2554 | 241 volatile sig_handler *saved_sigchld_handler |
2626 | 242 = octave_set_signal_handler (SIGCHLD, SIG_IGN); |
2554 | 243 #endif |
244 | |
2626 | 245 sigset_t set, oset; |
246 | |
247 BLOCK_CHILD (set, oset); | |
248 | |
5142 | 249 if (octave_child_list::wait ()) |
2554 | 250 { |
5142 | 251 // The status of some child changed. |
2209 | 252 |
5142 | 253 octave_signal_caught = 1; |
2197 | 254 |
5142 | 255 octave_signals_caught[SIGCHLD] = true; |
1230 | 256 } |
2469 | 257 |
2600 | 258 octave_set_interrupt_handler (saved_interrupt_handler); |
2554 | 259 |
2626 | 260 UNBLOCK_CHILD (oset); |
261 | |
262 #ifdef __EMX__ | |
2554 | 263 octave_set_signal_handler (SIGCHLD, saved_sigchld_handler); |
2475 | 264 #endif |
265 | |
2626 | 266 MAYBE_ACK_SIGNAL (SIGCHLD); |
2554 | 267 |
268 MAYBE_REINSTALL_SIGHANDLER (SIGCHLD, sigchld_handler); | |
269 | |
2469 | 270 SIGHANDLER_RETURN (0); |
1230 | 271 } |
4064 | 272 #endif /* defined(SIGCHLD) */ |
1230 | 273 |
4064 | 274 #ifdef SIGFPE |
1373 | 275 #if defined (__alpha__) |
276 static RETSIGTYPE | |
1488 | 277 sigfpe_handler (int /* sig */) |
1373 | 278 { |
2626 | 279 MAYBE_ACK_SIGNAL (SIGFPE); |
2554 | 280 |
281 MAYBE_REINSTALL_SIGHANDLER (SIGFPE, sigfpe_handler); | |
1373 | 282 |
5142 | 283 if (can_interrupt && octave_interrupt_state >= 0) |
284 { | |
285 octave_signal_caught = 1; | |
1373 | 286 |
5142 | 287 octave_signals_caught[SIGFPE] = true; |
4182 | 288 |
5142 | 289 octave_interrupt_state++; |
290 } | |
1373 | 291 |
2469 | 292 SIGHANDLER_RETURN (0); |
1373 | 293 } |
4064 | 294 #endif /* defined(__alpha__) */ |
295 #endif /* defined(SIGFPE) */ | |
1373 | 296 |
4449 | 297 #if defined (SIGHUP) || defined (SIGTERM) |
298 static RETSIGTYPE | |
299 sig_hup_or_term_handler (int sig) | |
300 { | |
301 MAYBE_ACK_SIGNAL (sig); | |
302 | |
303 MAYBE_REINSTALL_SIGHANDLER (sig, sig_hup_or_term_handler); | |
304 | |
305 switch (sig) | |
306 { | |
307 #if defined (SIGHUP) | |
308 case SIGHUP: | |
309 { | |
310 if (Vsighup_dumps_octave_core) | |
4791 | 311 dump_octave_core (); |
4449 | 312 } |
313 break; | |
314 #endif | |
315 | |
316 #if defined (SIGTERM) | |
317 case SIGTERM: | |
318 { | |
319 if (Vsigterm_dumps_octave_core) | |
4791 | 320 dump_octave_core (); |
4449 | 321 } |
322 break; | |
323 #endif | |
324 | |
325 default: | |
326 break; | |
327 } | |
328 | |
329 clean_up_and_exit (0); | |
330 | |
331 SIGHANDLER_RETURN (0); | |
332 } | |
333 #endif | |
334 | |
3343 | 335 #if 0 |
3281 | 336 #if defined (SIGWINCH) |
337 static RETSIGTYPE | |
338 sigwinch_handler (int /* sig */) | |
339 { | |
340 MAYBE_ACK_SIGNAL (SIGWINCH); | |
341 | |
342 MAYBE_REINSTALL_SIGHANDLER (SIGWINCH, sigwinch_handler); | |
343 | |
344 command_editor::resize_terminal (); | |
345 | |
346 SIGHANDLER_RETURN (0); | |
347 } | |
348 #endif | |
3343 | 349 #endif |
3281 | 350 |
635 | 351 // Handle SIGINT by restarting the parser (see octave.cc). |
2554 | 352 // |
353 // This also has to work for SIGBREAK (on systems that have it), so we | |
354 // use the value of sig, instead of just assuming that it is called | |
355 // for SIGINT only. | |
635 | 356 |
8658
73c4516fae10
New evaluator and debugger derived from tree-walker class
John W. Eaton <jwe@octave.org>
parents:
7481
diff
changeset
|
357 static void |
73c4516fae10
New evaluator and debugger derived from tree-walker class
John W. Eaton <jwe@octave.org>
parents:
7481
diff
changeset
|
358 user_abort (const char *sig_name, int sig_number) |
1 | 359 { |
4172 | 360 if (! octave_initialized) |
361 exit (1); | |
362 | |
1 | 363 if (can_interrupt) |
364 { | |
4185 | 365 if (Vdebug_on_interrupt) |
366 { | |
367 if (! octave_debug_on_interrupt_state) | |
368 { | |
8658
73c4516fae10
New evaluator and debugger derived from tree-walker class
John W. Eaton <jwe@octave.org>
parents:
7481
diff
changeset
|
369 tree_evaluator::debug_mode = true; |
4185 | 370 octave_debug_on_interrupt_state = true; |
371 | |
5451 | 372 return; |
4185 | 373 } |
374 else | |
8658
73c4516fae10
New evaluator and debugger derived from tree-walker class
John W. Eaton <jwe@octave.org>
parents:
7481
diff
changeset
|
375 { |
73c4516fae10
New evaluator and debugger derived from tree-walker class
John W. Eaton <jwe@octave.org>
parents:
7481
diff
changeset
|
376 // Clear the flag and do normal interrupt stuff. |
73c4516fae10
New evaluator and debugger derived from tree-walker class
John W. Eaton <jwe@octave.org>
parents:
7481
diff
changeset
|
377 |
73c4516fae10
New evaluator and debugger derived from tree-walker class
John W. Eaton <jwe@octave.org>
parents:
7481
diff
changeset
|
378 tree_evaluator::debug_mode = bp_table::have_breakpoints (); |
73c4516fae10
New evaluator and debugger derived from tree-walker class
John W. Eaton <jwe@octave.org>
parents:
7481
diff
changeset
|
379 octave_debug_on_interrupt_state = false; |
73c4516fae10
New evaluator and debugger derived from tree-walker class
John W. Eaton <jwe@octave.org>
parents:
7481
diff
changeset
|
380 } |
4185 | 381 } |
382 | |
4153 | 383 if (octave_interrupt_immediately) |
7481
78f3811155f7
use exceptions in liboctave error handler
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
384 { |
78f3811155f7
use exceptions in liboctave error handler
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
385 if (octave_interrupt_state == 0) |
78f3811155f7
use exceptions in liboctave error handler
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
386 octave_interrupt_state = 1; |
78f3811155f7
use exceptions in liboctave error handler
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
387 |
78f3811155f7
use exceptions in liboctave error handler
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
388 octave_jump_to_enclosing_context (); |
78f3811155f7
use exceptions in liboctave error handler
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
389 } |
4429 | 390 else |
4787 | 391 { |
4793 | 392 // If we are already cleaning up from a previous interrupt, |
393 // take note of the fact that another interrupt signal has | |
394 // arrived. | |
395 | |
396 if (octave_interrupt_state < 0) | |
397 octave_interrupt_state = 0; | |
398 | |
5142 | 399 octave_signal_caught = 1; |
4787 | 400 octave_interrupt_state++; |
401 | |
4791 | 402 if (interactive && octave_interrupt_state == 2) |
403 std::cerr << "Press Control-C again to abort." << std::endl; | |
4787 | 404 |
4791 | 405 if (octave_interrupt_state >= 3) |
5451 | 406 my_friendly_exit (sig_name, sig_number, true); |
4787 | 407 } |
1 | 408 } |
409 | |
5451 | 410 } |
411 | |
412 static RETSIGTYPE | |
413 sigint_handler (int sig) | |
414 { | |
415 MAYBE_ACK_SIGNAL (sig); | |
416 | |
417 MAYBE_REINSTALL_SIGHANDLER (sig, sigint_handler); | |
418 | |
419 #ifdef USE_W32_SIGINT | |
420 if (w32_in_main_thread ()) | |
5844 | 421 user_abort (strsignal (sig), sig); |
5451 | 422 else |
423 w32_raise (sig); | |
424 #else | |
5844 | 425 user_abort (strsignal (sig), sig); |
5451 | 426 #endif |
427 | |
2469 | 428 SIGHANDLER_RETURN (0); |
1 | 429 } |
430 | |
4064 | 431 #ifdef SIGPIPE |
1 | 432 static RETSIGTYPE |
1488 | 433 sigpipe_handler (int /* sig */) |
1 | 434 { |
2626 | 435 MAYBE_ACK_SIGNAL (SIGPIPE); |
2554 | 436 |
437 MAYBE_REINSTALL_SIGHANDLER (SIGPIPE, sigpipe_handler); | |
834 | 438 |
5142 | 439 octave_signal_caught = 1; |
440 | |
441 octave_signals_caught[SIGPIPE] = true; | |
1 | 442 |
1358 | 443 // Don't loop forever on account of this. |
444 | |
5142 | 445 if (pipe_handler_error_count++ > 100 && octave_interrupt_state >= 0) |
4787 | 446 octave_interrupt_state++; |
1 | 447 |
2469 | 448 SIGHANDLER_RETURN (0); |
1 | 449 } |
4064 | 450 #endif /* defined(SIGPIPE) */ |
1 | 451 |
5451 | 452 #ifdef USE_W32_SIGINT |
453 static BOOL CALLBACK | |
454 w32_sigint_handler (DWORD sig) | |
455 { | |
456 const char *sig_name; | |
457 | |
458 switch(sig) | |
459 { | |
460 case CTRL_BREAK_EVENT: | |
461 sig_name = "Ctrl-Break"; | |
462 break; | |
463 case CTRL_C_EVENT: | |
464 sig_name = "Ctrl-C"; | |
465 break; | |
466 case CTRL_CLOSE_EVENT: | |
467 sig_name = "close console"; | |
468 break; | |
469 case CTRL_LOGOFF_EVENT: | |
470 sig_name = "logoff"; | |
471 break; | |
472 case CTRL_SHUTDOWN_EVENT: | |
473 sig_name = "shutdown"; | |
474 break; | |
475 default: | |
476 sig_name = "unknown console event"; | |
477 break; | |
478 } | |
479 | |
480 switch(sig) | |
481 { | |
482 case CTRL_BREAK_EVENT: | |
483 case CTRL_C_EVENT: | |
484 w32_raise (SIGINT); | |
485 break; | |
486 | |
487 case CTRL_CLOSE_EVENT: | |
488 case CTRL_LOGOFF_EVENT: | |
489 case CTRL_SHUTDOWN_EVENT: | |
490 default: | |
491 // We should do the following: | |
492 // clean_up_and_exit (0); | |
493 // We can't because we aren't running in the normal Octave thread. | |
494 user_abort(sig_name, sig); | |
495 break; | |
496 } | |
497 | |
498 // Return TRUE if the event was handled, or FALSE if another handler | |
499 // should be called. | |
5775 | 500 // FIXME check that windows terminates the thread. |
5451 | 501 return TRUE; |
502 } | |
503 #endif /* w32_sigint_handler */ | |
504 | |
505 | |
2705 | 506 octave_interrupt_handler |
2554 | 507 octave_catch_interrupts (void) |
508 { | |
2705 | 509 octave_interrupt_handler retval; |
2554 | 510 |
511 #ifdef SIGINT | |
2705 | 512 retval.int_handler = octave_set_signal_handler (SIGINT, sigint_handler); |
2554 | 513 #endif |
514 | |
515 #ifdef SIGBREAK | |
2705 | 516 retval.brk_handler = octave_set_signal_handler (SIGBREAK, sigint_handler); |
2554 | 517 #endif |
2705 | 518 |
5451 | 519 #ifdef USE_W32_SIGINT |
520 | |
521 // Intercept windows console control events. | |
522 // Note that the windows console signal handlers chain, so if | |
523 // install_signal_handlers is called more than once in the same program, | |
524 // then first call the following to avoid duplicates: | |
525 // | |
526 // SetConsoleCtrlHandler (w32_sigint_handler, FALSE); | |
527 | |
528 if (! SetConsoleCtrlHandler (w32_sigint_handler, TRUE)) | |
529 error ("SetConsoleCtrlHandler failed with %ld\n", GetLastError ()); | |
530 | |
531 w32_set_quiet_shutdown (); | |
532 | |
533 #endif | |
534 | |
2705 | 535 return retval; |
2554 | 536 } |
537 | |
2705 | 538 octave_interrupt_handler |
539 octave_ignore_interrupts (void) | |
2554 | 540 { |
2705 | 541 octave_interrupt_handler retval; |
542 | |
2554 | 543 #ifdef SIGINT |
2705 | 544 retval.int_handler = octave_set_signal_handler (SIGINT, SIG_IGN); |
2554 | 545 #endif |
546 | |
547 #ifdef SIGBREAK | |
2705 | 548 retval.brk_handler = octave_set_signal_handler (SIGBREAK, SIG_IGN); |
2554 | 549 #endif |
2705 | 550 |
551 return retval; | |
552 } | |
553 | |
554 octave_interrupt_handler | |
5770 | 555 octave_set_interrupt_handler (const volatile octave_interrupt_handler& h, |
556 bool restart_syscalls) | |
2705 | 557 { |
558 octave_interrupt_handler retval; | |
559 | |
560 #ifdef SIGINT | |
5770 | 561 retval.int_handler = octave_set_signal_handler (SIGINT, h.int_handler, |
562 restart_syscalls); | |
2705 | 563 #endif |
564 | |
565 #ifdef SIGBREAK | |
5770 | 566 retval.brk_handler = octave_set_signal_handler (SIGBREAK, h.brk_handler, |
567 restart_syscalls); | |
2705 | 568 #endif |
569 | |
570 return retval; | |
1651 | 571 } |
572 | |
635 | 573 // Install all the handlers for the signals we might care about. |
574 | |
1 | 575 void |
576 install_signal_handlers (void) | |
577 { | |
5142 | 578 for (int i = 0; i < NSIG; i++) |
579 octave_signals_caught[i] = false; | |
580 | |
2554 | 581 octave_catch_interrupts (); |
582 | |
1 | 583 #ifdef SIGABRT |
1446 | 584 octave_set_signal_handler (SIGABRT, generic_sig_handler); |
1 | 585 #endif |
586 | |
587 #ifdef SIGALRM | |
1446 | 588 octave_set_signal_handler (SIGALRM, generic_sig_handler); |
1 | 589 #endif |
590 | |
591 #ifdef SIGBUS | |
1446 | 592 octave_set_signal_handler (SIGBUS, generic_sig_handler); |
1 | 593 #endif |
594 | |
1230 | 595 #ifdef SIGCHLD |
1446 | 596 octave_set_signal_handler (SIGCHLD, sigchld_handler); |
1230 | 597 #endif |
598 | |
3174 | 599 // SIGCLD |
600 // SIGCONT | |
601 | |
1 | 602 #ifdef SIGEMT |
1446 | 603 octave_set_signal_handler (SIGEMT, generic_sig_handler); |
1 | 604 #endif |
605 | |
606 #ifdef SIGFPE | |
1373 | 607 #if defined (__alpha__) |
1446 | 608 octave_set_signal_handler (SIGFPE, sigfpe_handler); |
1373 | 609 #else |
1446 | 610 octave_set_signal_handler (SIGFPE, generic_sig_handler); |
1 | 611 #endif |
1373 | 612 #endif |
1 | 613 |
614 #ifdef SIGHUP | |
4449 | 615 octave_set_signal_handler (SIGHUP, sig_hup_or_term_handler); |
1 | 616 #endif |
617 | |
618 #ifdef SIGILL | |
1446 | 619 octave_set_signal_handler (SIGILL, generic_sig_handler); |
1 | 620 #endif |
621 | |
3174 | 622 // SIGINFO |
623 // SIGINT | |
624 | |
1 | 625 #ifdef SIGIOT |
1446 | 626 octave_set_signal_handler (SIGIOT, generic_sig_handler); |
1 | 627 #endif |
628 | |
629 #ifdef SIGLOST | |
1446 | 630 octave_set_signal_handler (SIGLOST, generic_sig_handler); |
1 | 631 #endif |
632 | |
633 #ifdef SIGPIPE | |
1446 | 634 octave_set_signal_handler (SIGPIPE, sigpipe_handler); |
1 | 635 #endif |
636 | |
637 #ifdef SIGPOLL | |
1446 | 638 octave_set_signal_handler (SIGPOLL, SIG_IGN); |
1 | 639 #endif |
640 | |
5866 | 641 // SIGPROF |
3174 | 642 // SIGPWR |
643 | |
1 | 644 #ifdef SIGQUIT |
1446 | 645 octave_set_signal_handler (SIGQUIT, generic_sig_handler); |
1 | 646 #endif |
647 | |
648 #ifdef SIGSEGV | |
1446 | 649 octave_set_signal_handler (SIGSEGV, generic_sig_handler); |
1 | 650 #endif |
651 | |
3174 | 652 // SIGSTOP |
653 | |
1 | 654 #ifdef SIGSYS |
1446 | 655 octave_set_signal_handler (SIGSYS, generic_sig_handler); |
1 | 656 #endif |
657 | |
658 #ifdef SIGTERM | |
4449 | 659 octave_set_signal_handler (SIGTERM, sig_hup_or_term_handler); |
1 | 660 #endif |
661 | |
662 #ifdef SIGTRAP | |
1446 | 663 octave_set_signal_handler (SIGTRAP, generic_sig_handler); |
1 | 664 #endif |
665 | |
3174 | 666 // SIGTSTP |
667 // SIGTTIN | |
668 // SIGTTOU | |
669 // SIGURG | |
670 | |
1 | 671 #ifdef SIGUSR1 |
1446 | 672 octave_set_signal_handler (SIGUSR1, generic_sig_handler); |
1 | 673 #endif |
674 | |
675 #ifdef SIGUSR2 | |
1446 | 676 octave_set_signal_handler (SIGUSR2, generic_sig_handler); |
1 | 677 #endif |
678 | |
679 #ifdef SIGVTALRM | |
1446 | 680 octave_set_signal_handler (SIGVTALRM, generic_sig_handler); |
1 | 681 #endif |
682 | |
895 | 683 #ifdef SIGIO |
1446 | 684 octave_set_signal_handler (SIGIO, SIG_IGN); |
895 | 685 #endif |
686 | |
3343 | 687 #if 0 |
3281 | 688 #ifdef SIGWINCH |
689 octave_set_signal_handler (SIGWINCH, sigwinch_handler); | |
690 #endif | |
3343 | 691 #endif |
3174 | 692 |
1 | 693 #ifdef SIGXCPU |
1446 | 694 octave_set_signal_handler (SIGXCPU, generic_sig_handler); |
1 | 695 #endif |
696 | |
697 #ifdef SIGXFSZ | |
1446 | 698 octave_set_signal_handler (SIGXFSZ, generic_sig_handler); |
1 | 699 #endif |
5451 | 700 |
1 | 701 } |
702 | |
4294 | 703 static Octave_map |
704 make_sig_struct (void) | |
705 { | |
706 Octave_map m; | |
707 | |
708 #ifdef SIGABRT | |
4675 | 709 m.assign ("ABRT", SIGABRT); |
4294 | 710 #endif |
711 | |
712 #ifdef SIGALRM | |
4675 | 713 m.assign ("ALRM", SIGALRM); |
4294 | 714 #endif |
715 | |
716 #ifdef SIGBUS | |
4675 | 717 m.assign ("BUS", SIGBUS); |
4294 | 718 #endif |
719 | |
720 #ifdef SIGCHLD | |
4675 | 721 m.assign ("CHLD", SIGCHLD); |
4294 | 722 #endif |
723 | |
724 #ifdef SIGCLD | |
4675 | 725 m.assign ("CLD", SIGCLD); |
4294 | 726 #endif |
727 | |
728 #ifdef SIGCONT | |
4675 | 729 m.assign ("CONT", SIGCONT); |
4294 | 730 #endif |
731 | |
732 #ifdef SIGEMT | |
4675 | 733 m.assign ("EMT", SIGEMT); |
4294 | 734 #endif |
735 | |
736 #ifdef SIGFPE | |
4675 | 737 m.assign ("FPE", SIGFPE); |
4294 | 738 #endif |
739 | |
740 #ifdef SIGHUP | |
4675 | 741 m.assign ("HUP", SIGHUP); |
4294 | 742 #endif |
743 | |
744 #ifdef SIGILL | |
4675 | 745 m.assign ("ILL", SIGILL); |
4294 | 746 #endif |
747 | |
748 #ifdef SIGINFO | |
4675 | 749 m.assign ("INFO", SIGINFO); |
4294 | 750 #endif |
751 | |
752 #ifdef SIGINT | |
4675 | 753 m.assign ("INT", SIGINT); |
4294 | 754 #endif |
755 | |
756 #ifdef SIGIOT | |
4675 | 757 m.assign ("IOT", SIGIOT); |
4294 | 758 #endif |
759 | |
760 #ifdef SIGLOST | |
4675 | 761 m.assign ("LOST", SIGLOST); |
4294 | 762 #endif |
763 | |
764 #ifdef SIGPIPE | |
4675 | 765 m.assign ("PIPE", SIGPIPE); |
4294 | 766 #endif |
767 | |
768 #ifdef SIGPOLL | |
4675 | 769 m.assign ("POLL", SIGPOLL); |
4294 | 770 #endif |
771 | |
772 #ifdef SIGPROF | |
4675 | 773 m.assign ("PROF", SIGPROF); |
4294 | 774 #endif |
775 | |
776 #ifdef SIGPWR | |
4675 | 777 m.assign ("PWR", SIGPWR); |
4294 | 778 #endif |
779 | |
780 #ifdef SIGQUIT | |
4675 | 781 m.assign ("QUIT", SIGQUIT); |
4294 | 782 #endif |
783 | |
784 #ifdef SIGSEGV | |
4675 | 785 m.assign ("SEGV", SIGSEGV); |
4294 | 786 #endif |
787 | |
788 #ifdef SIGSTOP | |
4675 | 789 m.assign ("STOP", SIGSTOP); |
4294 | 790 #endif |
791 | |
792 #ifdef SIGSYS | |
4675 | 793 m.assign ("SYS", SIGSYS); |
4294 | 794 #endif |
795 | |
796 #ifdef SIGTERM | |
4675 | 797 m.assign ("TERM", SIGTERM); |
4294 | 798 #endif |
799 | |
800 #ifdef SIGTRAP | |
4675 | 801 m.assign ("TRAP", SIGTRAP); |
4294 | 802 #endif |
803 | |
804 #ifdef SIGTSTP | |
4675 | 805 m.assign ("TSTP", SIGTSTP); |
4294 | 806 #endif |
807 | |
808 #ifdef SIGTTIN | |
4675 | 809 m.assign ("TTIN", SIGTTIN); |
4294 | 810 #endif |
811 | |
812 #ifdef SIGTTOU | |
4675 | 813 m.assign ("TTOU", SIGTTOU); |
4294 | 814 #endif |
815 | |
816 #ifdef SIGURG | |
4675 | 817 m.assign ("URG", SIGURG); |
4294 | 818 #endif |
819 | |
820 #ifdef SIGUSR1 | |
4675 | 821 m.assign ("USR1", SIGUSR1); |
4294 | 822 #endif |
823 | |
824 #ifdef SIGUSR2 | |
4675 | 825 m.assign ("USR2", SIGUSR2); |
4294 | 826 #endif |
827 | |
828 #ifdef SIGVTALRM | |
4675 | 829 m.assign ("VTALRM", SIGVTALRM); |
4294 | 830 #endif |
831 | |
832 #ifdef SIGIO | |
4675 | 833 m.assign ("IO", SIGIO); |
4294 | 834 #endif |
835 | |
836 #ifdef SIGWINCH | |
4675 | 837 m.assign ("WINCH", SIGWINCH); |
4294 | 838 #endif |
839 | |
840 #ifdef SIGXCPU | |
4675 | 841 m.assign ("XCPU", SIGXCPU); |
4294 | 842 #endif |
843 | |
844 #ifdef SIGXFSZ | |
4675 | 845 m.assign ("XFSZ", SIGXFSZ); |
4294 | 846 #endif |
847 | |
848 return m; | |
849 } | |
850 | |
5142 | 851 octave_child_list::octave_child_list_rep *octave_child_list::instance = 0; |
5128 | 852 |
2926 | 853 bool |
854 octave_child_list::instance_ok (void) | |
855 { | |
856 bool retval = true; | |
857 | |
858 if (! instance) | |
5142 | 859 instance = new octave_child_list_rep (); |
2926 | 860 |
861 if (! instance) | |
862 { | |
863 ::error ("unable to create child list object!"); | |
864 | |
865 retval = false; | |
866 } | |
867 | |
868 return retval; | |
869 } | |
870 | |
871 void | |
5142 | 872 octave_child_list::insert (pid_t pid, octave_child::child_event_handler f) |
873 { | |
874 if (instance_ok ()) | |
875 instance->insert (pid, f); | |
876 } | |
877 | |
878 void | |
879 octave_child_list::reap (void) | |
2926 | 880 { |
881 if (instance_ok ()) | |
5142 | 882 instance->reap (); |
883 } | |
884 | |
885 bool | |
886 octave_child_list::wait (void) | |
887 { | |
888 return (instance_ok ()) ? instance->wait () : false; | |
2926 | 889 } |
890 | |
5142 | 891 class pid_equal |
892 { | |
893 public: | |
894 | |
895 pid_equal (pid_t v) : val (v) { } | |
896 | |
897 bool operator () (const octave_child& oc) const { return oc.pid == val; } | |
898 | |
899 private: | |
900 | |
901 pid_t val; | |
902 }; | |
903 | |
2926 | 904 void |
905 octave_child_list::remove (pid_t pid) | |
906 { | |
907 if (instance_ok ()) | |
5142 | 908 instance->remove_if (pid_equal (pid)); |
2926 | 909 } |
910 | |
5142 | 911 #define OCL_REP octave_child_list::octave_child_list_rep |
2926 | 912 |
5142 | 913 void |
914 OCL_REP::insert (pid_t pid, octave_child::child_event_handler f) | |
2926 | 915 { |
5142 | 916 append (octave_child (pid, f)); |
2926 | 917 } |
918 | |
2209 | 919 void |
5142 | 920 OCL_REP::reap (void) |
2209 | 921 { |
5142 | 922 // Mark the record for PID invalid. |
2209 | 923 |
5142 | 924 for (iterator p = begin (); p != end (); p++) |
925 { | |
926 // The call to the octave_child::child_event_handler might | |
927 // invalidate the iterator (for example, by calling | |
928 // octave_child_list::remove), so we increment the iterator | |
929 // here. | |
2209 | 930 |
5142 | 931 octave_child& oc = *p; |
2209 | 932 |
5142 | 933 if (oc.have_status) |
2209 | 934 { |
5142 | 935 oc.have_status = 0; |
936 | |
937 octave_child::child_event_handler f = oc.handler; | |
938 | |
939 if (f && f (oc.pid, oc.status)) | |
940 oc.pid = -1; | |
2209 | 941 } |
942 } | |
943 | |
5142 | 944 remove_if (pid_equal (-1)); |
2209 | 945 } |
946 | |
5142 | 947 // Wait on our children and record any changes in their status. |
948 | |
949 bool | |
950 OCL_REP::wait (void) | |
2210 | 951 { |
5142 | 952 bool retval = false; |
2210 | 953 |
5142 | 954 for (iterator p = begin (); p != end (); p++) |
2210 | 955 { |
5142 | 956 octave_child& oc = *p; |
957 | |
958 pid_t pid = oc.pid; | |
959 | |
960 if (pid > 0) | |
961 { | |
962 int status; | |
2210 | 963 |
5453 | 964 if (octave_syscalls::waitpid (pid, &status, WNOHANG) > 0) |
5142 | 965 { |
966 oc.have_status = 1; | |
967 | |
968 oc.status = status; | |
969 | |
970 retval = true; | |
971 | |
972 break; | |
973 } | |
2210 | 974 } |
975 } | |
976 | |
5142 | 977 return retval; |
2210 | 978 } |
979 | |
5780 | 980 DEFUN (SIG, args, , |
981 "-*- texinfo -*-\n\ | |
982 @deftypefn {Built-in Function} {} SIG ()\n\ | |
983 Return a structure containing Unix signal names and their defined values.\n\ | |
984 @end deftypefn") | |
985 { | |
986 octave_value retval; | |
987 | |
988 if (args.length () == 0) | |
989 { | |
990 static Octave_map m = make_sig_struct (); | |
991 | |
992 retval = m; | |
993 } | |
994 else | |
5823 | 995 print_usage (); |
5780 | 996 |
997 return retval; | |
998 } | |
999 | |
5794 | 1000 DEFUN (debug_on_interrupt, args, nargout, |
1001 "-*- texinfo -*-\n\ | |
1002 @deftypefn {Built-in Function} {@var{val} =} debug_on_interrupt ()\n\ | |
1003 @deftypefnx {Built-in Function} {@var{old_val} =} debug_on_interrupt (@var{new_val})\n\ | |
1004 Query or set the internal variable that controls whether Octave will try\n\ | |
1005 to enter debugging mode when it receives an interrupt signal (typically\n\ | |
1006 generated with @kbd{C-c}). If a second interrupt signal is received\n\ | |
1007 before reaching the debugging mode, a normal interrupt will occur.\n\ | |
1008 @end deftypefn") | |
4449 | 1009 { |
5794 | 1010 return SET_INTERNAL_VARIABLE (debug_on_interrupt); |
4449 | 1011 } |
1012 | |
5794 | 1013 DEFUN (sighup_dumps_octave_core, args, nargout, |
1014 "-*- texinfo -*-\n\ | |
1015 @deftypefn {Built-in Function} {@var{val} =} sighup_dumps_octave_core ()\n\ | |
1016 @deftypefnx {Built-in Function} {@var{old_val} =} sighup_dumps_octave_core (@var{new_val})\n\ | |
1017 Query or set the internal variable that controls whether Octave tries\n\ | |
6653 | 1018 to save all current variables to the file \"octave-core\" if it receives\n\ |
5794 | 1019 a hangup signal.\n\ |
1020 @end deftypefn") | |
4185 | 1021 { |
5794 | 1022 return SET_INTERNAL_VARIABLE (sighup_dumps_octave_core); |
1023 } | |
4294 | 1024 |
5794 | 1025 DEFUN (sigterm_dumps_octave_core, args, nargout, |
1026 "-*- texinfo -*-\n\ | |
1027 @deftypefn {Built-in Function} {@var{val} =} sigterm_dumps_octave_core ()\n\ | |
1028 @deftypefnx {Built-in Function} {@var{old_val} =} sigterm_dumps_octave_core (@var{new_val})\n\ | |
1029 Query or set the internal variable that controls whether Octave tries\n\ | |
6653 | 1030 to save all current variables to the file \"octave-core\" if it receives\n\ |
5794 | 1031 a terminate signal.\n\ |
1032 @end deftypefn") | |
1033 { | |
1034 return SET_INTERNAL_VARIABLE (sigterm_dumps_octave_core); | |
4185 | 1035 } |