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