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" |
4153
|
41 #include "quit.h" |
3281
|
42 |
4185
|
43 #include "defun.h" |
1352
|
44 #include "error.h" |
1373
|
45 #include "load-save.h" |
4294
|
46 #include "oct-map.h" |
2091
|
47 #include "pager.h" |
4185
|
48 #include "pt-bp.h" |
1
|
49 #include "sighandlers.h" |
4787
|
50 #include "sysdep.h" |
1352
|
51 #include "syswait.h" |
2091
|
52 #include "toplev.h" |
1
|
53 #include "utils.h" |
|
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. |
|
68 static bool Vsighup_dumps_octave_core; |
|
69 |
|
70 // Similar to Vsighup_dumps_octave_core, but for SIGTERM signal. |
|
71 static bool Vsigterm_dumps_octave_core; |
|
72 |
2469
|
73 #if RETSIGTYPE == void |
|
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 |
|
100 // Called from OCTAVE_QUIT to actually do something about the signals |
|
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 * |
|
187 octave_set_signal_handler (int sig, sig_handler *handler) |
|
188 { |
|
189 #if defined (HAVE_POSIX_SIGNALS) |
|
190 struct sigaction act, oact; |
5144
|
191 |
1446
|
192 act.sa_handler = handler; |
|
193 act.sa_flags = 0; |
5144
|
194 |
|
195 if (sig == SIGALRM) |
|
196 { |
|
197 #if defined (SA_INTERRUPT) |
|
198 act.sa_flags |= SA_INTERRUPT; |
|
199 #endif |
|
200 } |
|
201 else |
|
202 { |
|
203 #if defined (SA_RESTART) |
|
204 act.sa_flags |= SA_RESTART; |
|
205 #endif |
|
206 } |
|
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 { |
1446
|
222 my_friendly_exit (sys_siglist[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 |
5451
|
357 static |
|
358 void 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 { |
|
369 octave_debug_on_interrupt_state = true; |
|
370 |
5451
|
371 return; |
4185
|
372 } |
|
373 else |
|
374 // Clear the flag and do normal interrupt stuff. |
|
375 octave_debug_on_interrupt_state = false; |
|
376 } |
|
377 |
4153
|
378 if (octave_interrupt_immediately) |
|
379 octave_jump_to_enclosing_context (); |
4429
|
380 else |
4787
|
381 { |
4793
|
382 // If we are already cleaning up from a previous interrupt, |
|
383 // take note of the fact that another interrupt signal has |
|
384 // arrived. |
|
385 |
|
386 if (octave_interrupt_state < 0) |
|
387 octave_interrupt_state = 0; |
|
388 |
5142
|
389 octave_signal_caught = 1; |
4787
|
390 octave_interrupt_state++; |
|
391 |
4791
|
392 if (interactive && octave_interrupt_state == 2) |
|
393 std::cerr << "Press Control-C again to abort." << std::endl; |
4787
|
394 |
4791
|
395 if (octave_interrupt_state >= 3) |
5451
|
396 my_friendly_exit (sig_name, sig_number, true); |
4787
|
397 } |
1
|
398 } |
|
399 |
5451
|
400 } |
|
401 |
|
402 static RETSIGTYPE |
|
403 sigint_handler (int sig) |
|
404 { |
|
405 MAYBE_ACK_SIGNAL (sig); |
|
406 |
|
407 MAYBE_REINSTALL_SIGHANDLER (sig, sigint_handler); |
|
408 |
|
409 #ifdef USE_W32_SIGINT |
|
410 if (w32_in_main_thread ()) |
|
411 user_abort (sys_siglist[sig], sig); |
|
412 else |
|
413 w32_raise (sig); |
|
414 #else |
|
415 user_abort (sys_siglist[sig], sig); |
|
416 #endif |
|
417 |
2469
|
418 SIGHANDLER_RETURN (0); |
1
|
419 } |
|
420 |
4064
|
421 #ifdef SIGPIPE |
1
|
422 static RETSIGTYPE |
1488
|
423 sigpipe_handler (int /* sig */) |
1
|
424 { |
2626
|
425 MAYBE_ACK_SIGNAL (SIGPIPE); |
2554
|
426 |
|
427 MAYBE_REINSTALL_SIGHANDLER (SIGPIPE, sigpipe_handler); |
834
|
428 |
5142
|
429 octave_signal_caught = 1; |
|
430 |
|
431 octave_signals_caught[SIGPIPE] = true; |
1
|
432 |
1358
|
433 // Don't loop forever on account of this. |
|
434 |
5142
|
435 if (pipe_handler_error_count++ > 100 && octave_interrupt_state >= 0) |
4787
|
436 octave_interrupt_state++; |
1
|
437 |
2469
|
438 SIGHANDLER_RETURN (0); |
1
|
439 } |
4064
|
440 #endif /* defined(SIGPIPE) */ |
1
|
441 |
5451
|
442 #ifdef USE_W32_SIGINT |
|
443 static BOOL CALLBACK |
|
444 w32_sigint_handler (DWORD sig) |
|
445 { |
|
446 const char *sig_name; |
|
447 |
|
448 switch(sig) |
|
449 { |
|
450 case CTRL_BREAK_EVENT: |
|
451 sig_name = "Ctrl-Break"; |
|
452 break; |
|
453 case CTRL_C_EVENT: |
|
454 sig_name = "Ctrl-C"; |
|
455 break; |
|
456 case CTRL_CLOSE_EVENT: |
|
457 sig_name = "close console"; |
|
458 break; |
|
459 case CTRL_LOGOFF_EVENT: |
|
460 sig_name = "logoff"; |
|
461 break; |
|
462 case CTRL_SHUTDOWN_EVENT: |
|
463 sig_name = "shutdown"; |
|
464 break; |
|
465 default: |
|
466 sig_name = "unknown console event"; |
|
467 break; |
|
468 } |
|
469 |
|
470 switch(sig) |
|
471 { |
|
472 case CTRL_BREAK_EVENT: |
|
473 case CTRL_C_EVENT: |
|
474 w32_raise (SIGINT); |
|
475 break; |
|
476 |
|
477 case CTRL_CLOSE_EVENT: |
|
478 case CTRL_LOGOFF_EVENT: |
|
479 case CTRL_SHUTDOWN_EVENT: |
|
480 default: |
|
481 // We should do the following: |
|
482 // clean_up_and_exit (0); |
|
483 // We can't because we aren't running in the normal Octave thread. |
|
484 user_abort(sig_name, sig); |
|
485 break; |
|
486 } |
|
487 |
|
488 // Return TRUE if the event was handled, or FALSE if another handler |
|
489 // should be called. |
|
490 // XXX FIXME XXX check that windows terminates the thread. |
|
491 return TRUE; |
|
492 } |
|
493 #endif /* w32_sigint_handler */ |
|
494 |
|
495 |
2705
|
496 octave_interrupt_handler |
2554
|
497 octave_catch_interrupts (void) |
|
498 { |
2705
|
499 octave_interrupt_handler retval; |
2554
|
500 |
|
501 #ifdef SIGINT |
2705
|
502 retval.int_handler = octave_set_signal_handler (SIGINT, sigint_handler); |
2554
|
503 #endif |
|
504 |
|
505 #ifdef SIGBREAK |
2705
|
506 retval.brk_handler = octave_set_signal_handler (SIGBREAK, sigint_handler); |
2554
|
507 #endif |
2705
|
508 |
5451
|
509 #ifdef USE_W32_SIGINT |
|
510 |
|
511 // Intercept windows console control events. |
|
512 // Note that the windows console signal handlers chain, so if |
|
513 // install_signal_handlers is called more than once in the same program, |
|
514 // then first call the following to avoid duplicates: |
|
515 // |
|
516 // SetConsoleCtrlHandler (w32_sigint_handler, FALSE); |
|
517 |
|
518 if (! SetConsoleCtrlHandler (w32_sigint_handler, TRUE)) |
|
519 error ("SetConsoleCtrlHandler failed with %ld\n", GetLastError ()); |
|
520 |
|
521 w32_set_quiet_shutdown (); |
|
522 |
|
523 #endif |
|
524 |
2705
|
525 return retval; |
2554
|
526 } |
|
527 |
2705
|
528 octave_interrupt_handler |
|
529 octave_ignore_interrupts (void) |
2554
|
530 { |
2705
|
531 octave_interrupt_handler retval; |
|
532 |
2554
|
533 #ifdef SIGINT |
2705
|
534 retval.int_handler = octave_set_signal_handler (SIGINT, SIG_IGN); |
2554
|
535 #endif |
|
536 |
|
537 #ifdef SIGBREAK |
2705
|
538 retval.brk_handler = octave_set_signal_handler (SIGBREAK, SIG_IGN); |
2554
|
539 #endif |
2705
|
540 |
|
541 return retval; |
|
542 } |
|
543 |
|
544 octave_interrupt_handler |
|
545 octave_set_interrupt_handler (const volatile octave_interrupt_handler& h) |
|
546 { |
|
547 octave_interrupt_handler retval; |
|
548 |
|
549 #ifdef SIGINT |
|
550 retval.int_handler = octave_set_signal_handler (SIGINT, h.int_handler); |
|
551 #endif |
|
552 |
|
553 #ifdef SIGBREAK |
|
554 retval.brk_handler = octave_set_signal_handler (SIGBREAK, h.brk_handler); |
|
555 #endif |
|
556 |
|
557 return retval; |
1651
|
558 } |
|
559 |
635
|
560 // Install all the handlers for the signals we might care about. |
|
561 |
1
|
562 void |
|
563 install_signal_handlers (void) |
|
564 { |
5142
|
565 for (int i = 0; i < NSIG; i++) |
|
566 octave_signals_caught[i] = false; |
|
567 |
2554
|
568 octave_catch_interrupts (); |
|
569 |
1
|
570 #ifdef SIGABRT |
1446
|
571 octave_set_signal_handler (SIGABRT, generic_sig_handler); |
1
|
572 #endif |
|
573 |
|
574 #ifdef SIGALRM |
1446
|
575 octave_set_signal_handler (SIGALRM, generic_sig_handler); |
1
|
576 #endif |
|
577 |
|
578 #ifdef SIGBUS |
1446
|
579 octave_set_signal_handler (SIGBUS, generic_sig_handler); |
1
|
580 #endif |
|
581 |
1230
|
582 #ifdef SIGCHLD |
1446
|
583 octave_set_signal_handler (SIGCHLD, sigchld_handler); |
1230
|
584 #endif |
|
585 |
3174
|
586 // SIGCLD |
|
587 // SIGCONT |
|
588 |
1
|
589 #ifdef SIGEMT |
1446
|
590 octave_set_signal_handler (SIGEMT, generic_sig_handler); |
1
|
591 #endif |
|
592 |
|
593 #ifdef SIGFPE |
1373
|
594 #if defined (__alpha__) |
1446
|
595 octave_set_signal_handler (SIGFPE, sigfpe_handler); |
1373
|
596 #else |
1446
|
597 octave_set_signal_handler (SIGFPE, generic_sig_handler); |
1
|
598 #endif |
1373
|
599 #endif |
1
|
600 |
|
601 #ifdef SIGHUP |
4449
|
602 octave_set_signal_handler (SIGHUP, sig_hup_or_term_handler); |
1
|
603 #endif |
|
604 |
|
605 #ifdef SIGILL |
1446
|
606 octave_set_signal_handler (SIGILL, generic_sig_handler); |
1
|
607 #endif |
|
608 |
3174
|
609 // SIGINFO |
|
610 // SIGINT |
|
611 |
1
|
612 #ifdef SIGIOT |
1446
|
613 octave_set_signal_handler (SIGIOT, generic_sig_handler); |
1
|
614 #endif |
|
615 |
|
616 #ifdef SIGLOST |
1446
|
617 octave_set_signal_handler (SIGLOST, generic_sig_handler); |
1
|
618 #endif |
|
619 |
|
620 #ifdef SIGPIPE |
1446
|
621 octave_set_signal_handler (SIGPIPE, sigpipe_handler); |
1
|
622 #endif |
|
623 |
|
624 #ifdef SIGPOLL |
1446
|
625 octave_set_signal_handler (SIGPOLL, SIG_IGN); |
1
|
626 #endif |
|
627 |
|
628 #ifdef SIGPROF |
1446
|
629 octave_set_signal_handler (SIGPROF, generic_sig_handler); |
1
|
630 #endif |
|
631 |
3174
|
632 // SIGPWR |
|
633 |
1
|
634 #ifdef SIGQUIT |
1446
|
635 octave_set_signal_handler (SIGQUIT, generic_sig_handler); |
1
|
636 #endif |
|
637 |
|
638 #ifdef SIGSEGV |
1446
|
639 octave_set_signal_handler (SIGSEGV, generic_sig_handler); |
1
|
640 #endif |
|
641 |
3174
|
642 // SIGSTOP |
|
643 |
1
|
644 #ifdef SIGSYS |
1446
|
645 octave_set_signal_handler (SIGSYS, generic_sig_handler); |
1
|
646 #endif |
|
647 |
|
648 #ifdef SIGTERM |
4449
|
649 octave_set_signal_handler (SIGTERM, sig_hup_or_term_handler); |
1
|
650 #endif |
|
651 |
|
652 #ifdef SIGTRAP |
1446
|
653 octave_set_signal_handler (SIGTRAP, generic_sig_handler); |
1
|
654 #endif |
|
655 |
3174
|
656 // SIGTSTP |
|
657 // SIGTTIN |
|
658 // SIGTTOU |
|
659 // SIGURG |
|
660 |
1
|
661 #ifdef SIGUSR1 |
1446
|
662 octave_set_signal_handler (SIGUSR1, generic_sig_handler); |
1
|
663 #endif |
|
664 |
|
665 #ifdef SIGUSR2 |
1446
|
666 octave_set_signal_handler (SIGUSR2, generic_sig_handler); |
1
|
667 #endif |
|
668 |
|
669 #ifdef SIGVTALRM |
1446
|
670 octave_set_signal_handler (SIGVTALRM, generic_sig_handler); |
1
|
671 #endif |
|
672 |
895
|
673 #ifdef SIGIO |
1446
|
674 octave_set_signal_handler (SIGIO, SIG_IGN); |
895
|
675 #endif |
|
676 |
3343
|
677 #if 0 |
3281
|
678 #ifdef SIGWINCH |
|
679 octave_set_signal_handler (SIGWINCH, sigwinch_handler); |
|
680 #endif |
3343
|
681 #endif |
3174
|
682 |
1
|
683 #ifdef SIGXCPU |
1446
|
684 octave_set_signal_handler (SIGXCPU, generic_sig_handler); |
1
|
685 #endif |
|
686 |
|
687 #ifdef SIGXFSZ |
1446
|
688 octave_set_signal_handler (SIGXFSZ, generic_sig_handler); |
1
|
689 #endif |
5451
|
690 |
1
|
691 } |
|
692 |
4294
|
693 static Octave_map |
|
694 make_sig_struct (void) |
|
695 { |
|
696 Octave_map m; |
|
697 |
|
698 #ifdef SIGABRT |
4675
|
699 m.assign ("ABRT", SIGABRT); |
4294
|
700 #endif |
|
701 |
|
702 #ifdef SIGALRM |
4675
|
703 m.assign ("ALRM", SIGALRM); |
4294
|
704 #endif |
|
705 |
|
706 #ifdef SIGBUS |
4675
|
707 m.assign ("BUS", SIGBUS); |
4294
|
708 #endif |
|
709 |
|
710 #ifdef SIGCHLD |
4675
|
711 m.assign ("CHLD", SIGCHLD); |
4294
|
712 #endif |
|
713 |
|
714 #ifdef SIGCLD |
4675
|
715 m.assign ("CLD", SIGCLD); |
4294
|
716 #endif |
|
717 |
|
718 #ifdef SIGCONT |
4675
|
719 m.assign ("CONT", SIGCONT); |
4294
|
720 #endif |
|
721 |
|
722 #ifdef SIGEMT |
4675
|
723 m.assign ("EMT", SIGEMT); |
4294
|
724 #endif |
|
725 |
|
726 #ifdef SIGFPE |
4675
|
727 m.assign ("FPE", SIGFPE); |
4294
|
728 #endif |
|
729 |
|
730 #ifdef SIGHUP |
4675
|
731 m.assign ("HUP", SIGHUP); |
4294
|
732 #endif |
|
733 |
|
734 #ifdef SIGILL |
4675
|
735 m.assign ("ILL", SIGILL); |
4294
|
736 #endif |
|
737 |
|
738 #ifdef SIGINFO |
4675
|
739 m.assign ("INFO", SIGINFO); |
4294
|
740 #endif |
|
741 |
|
742 #ifdef SIGINT |
4675
|
743 m.assign ("INT", SIGINT); |
4294
|
744 #endif |
|
745 |
|
746 #ifdef SIGIOT |
4675
|
747 m.assign ("IOT", SIGIOT); |
4294
|
748 #endif |
|
749 |
|
750 #ifdef SIGLOST |
4675
|
751 m.assign ("LOST", SIGLOST); |
4294
|
752 #endif |
|
753 |
|
754 #ifdef SIGPIPE |
4675
|
755 m.assign ("PIPE", SIGPIPE); |
4294
|
756 #endif |
|
757 |
|
758 #ifdef SIGPOLL |
4675
|
759 m.assign ("POLL", SIGPOLL); |
4294
|
760 #endif |
|
761 |
|
762 #ifdef SIGPROF |
4675
|
763 m.assign ("PROF", SIGPROF); |
4294
|
764 #endif |
|
765 |
|
766 #ifdef SIGPWR |
4675
|
767 m.assign ("PWR", SIGPWR); |
4294
|
768 #endif |
|
769 |
|
770 #ifdef SIGQUIT |
4675
|
771 m.assign ("QUIT", SIGQUIT); |
4294
|
772 #endif |
|
773 |
|
774 #ifdef SIGSEGV |
4675
|
775 m.assign ("SEGV", SIGSEGV); |
4294
|
776 #endif |
|
777 |
|
778 #ifdef SIGSTOP |
4675
|
779 m.assign ("STOP", SIGSTOP); |
4294
|
780 #endif |
|
781 |
|
782 #ifdef SIGSYS |
4675
|
783 m.assign ("SYS", SIGSYS); |
4294
|
784 #endif |
|
785 |
|
786 #ifdef SIGTERM |
4675
|
787 m.assign ("TERM", SIGTERM); |
4294
|
788 #endif |
|
789 |
|
790 #ifdef SIGTRAP |
4675
|
791 m.assign ("TRAP", SIGTRAP); |
4294
|
792 #endif |
|
793 |
|
794 #ifdef SIGTSTP |
4675
|
795 m.assign ("TSTP", SIGTSTP); |
4294
|
796 #endif |
|
797 |
|
798 #ifdef SIGTTIN |
4675
|
799 m.assign ("TTIN", SIGTTIN); |
4294
|
800 #endif |
|
801 |
|
802 #ifdef SIGTTOU |
4675
|
803 m.assign ("TTOU", SIGTTOU); |
4294
|
804 #endif |
|
805 |
|
806 #ifdef SIGURG |
4675
|
807 m.assign ("URG", SIGURG); |
4294
|
808 #endif |
|
809 |
|
810 #ifdef SIGUSR1 |
4675
|
811 m.assign ("USR1", SIGUSR1); |
4294
|
812 #endif |
|
813 |
|
814 #ifdef SIGUSR2 |
4675
|
815 m.assign ("USR2", SIGUSR2); |
4294
|
816 #endif |
|
817 |
|
818 #ifdef SIGVTALRM |
4675
|
819 m.assign ("VTALRM", SIGVTALRM); |
4294
|
820 #endif |
|
821 |
|
822 #ifdef SIGIO |
4675
|
823 m.assign ("IO", SIGIO); |
4294
|
824 #endif |
|
825 |
|
826 #ifdef SIGWINCH |
4675
|
827 m.assign ("WINCH", SIGWINCH); |
4294
|
828 #endif |
|
829 |
|
830 #ifdef SIGXCPU |
4675
|
831 m.assign ("XCPU", SIGXCPU); |
4294
|
832 #endif |
|
833 |
|
834 #ifdef SIGXFSZ |
4675
|
835 m.assign ("XFSZ", SIGXFSZ); |
4294
|
836 #endif |
|
837 |
|
838 return m; |
|
839 } |
|
840 |
5142
|
841 octave_child_list::octave_child_list_rep *octave_child_list::instance = 0; |
5128
|
842 |
2926
|
843 bool |
|
844 octave_child_list::instance_ok (void) |
|
845 { |
|
846 bool retval = true; |
|
847 |
|
848 if (! instance) |
5142
|
849 instance = new octave_child_list_rep (); |
2926
|
850 |
|
851 if (! instance) |
|
852 { |
|
853 ::error ("unable to create child list object!"); |
|
854 |
|
855 retval = false; |
|
856 } |
|
857 |
|
858 return retval; |
|
859 } |
|
860 |
|
861 void |
5142
|
862 octave_child_list::insert (pid_t pid, octave_child::child_event_handler f) |
|
863 { |
|
864 if (instance_ok ()) |
|
865 instance->insert (pid, f); |
|
866 } |
|
867 |
|
868 void |
|
869 octave_child_list::reap (void) |
2926
|
870 { |
|
871 if (instance_ok ()) |
5142
|
872 instance->reap (); |
|
873 } |
|
874 |
|
875 bool |
|
876 octave_child_list::wait (void) |
|
877 { |
|
878 return (instance_ok ()) ? instance->wait () : false; |
2926
|
879 } |
|
880 |
5142
|
881 class pid_equal |
|
882 { |
|
883 public: |
|
884 |
|
885 pid_equal (pid_t v) : val (v) { } |
|
886 |
|
887 bool operator () (const octave_child& oc) const { return oc.pid == val; } |
|
888 |
|
889 private: |
|
890 |
|
891 pid_t val; |
|
892 }; |
|
893 |
2926
|
894 void |
|
895 octave_child_list::remove (pid_t pid) |
|
896 { |
|
897 if (instance_ok ()) |
5142
|
898 instance->remove_if (pid_equal (pid)); |
2926
|
899 } |
|
900 |
5142
|
901 #define OCL_REP octave_child_list::octave_child_list_rep |
2926
|
902 |
5142
|
903 void |
|
904 OCL_REP::insert (pid_t pid, octave_child::child_event_handler f) |
2926
|
905 { |
5142
|
906 append (octave_child (pid, f)); |
2926
|
907 } |
|
908 |
2209
|
909 void |
5142
|
910 OCL_REP::reap (void) |
2209
|
911 { |
5142
|
912 // Mark the record for PID invalid. |
2209
|
913 |
5142
|
914 for (iterator p = begin (); p != end (); p++) |
|
915 { |
|
916 // The call to the octave_child::child_event_handler might |
|
917 // invalidate the iterator (for example, by calling |
|
918 // octave_child_list::remove), so we increment the iterator |
|
919 // here. |
2209
|
920 |
5142
|
921 octave_child& oc = *p; |
2209
|
922 |
5142
|
923 if (oc.have_status) |
2209
|
924 { |
5142
|
925 oc.have_status = 0; |
|
926 |
|
927 octave_child::child_event_handler f = oc.handler; |
|
928 |
|
929 if (f && f (oc.pid, oc.status)) |
|
930 oc.pid = -1; |
2209
|
931 } |
|
932 } |
|
933 |
5142
|
934 remove_if (pid_equal (-1)); |
2209
|
935 } |
|
936 |
5142
|
937 // Wait on our children and record any changes in their status. |
|
938 |
|
939 bool |
|
940 OCL_REP::wait (void) |
2210
|
941 { |
5142
|
942 bool retval = false; |
2210
|
943 |
5142
|
944 for (iterator p = begin (); p != end (); p++) |
2210
|
945 { |
5142
|
946 octave_child& oc = *p; |
|
947 |
|
948 pid_t pid = oc.pid; |
|
949 |
|
950 if (pid > 0) |
|
951 { |
|
952 int status; |
2210
|
953 |
5142
|
954 if (waitpid (pid, &status, WNOHANG) > 0) |
|
955 { |
|
956 oc.have_status = 1; |
|
957 |
|
958 oc.status = status; |
|
959 |
|
960 retval = true; |
|
961 |
|
962 break; |
|
963 } |
2210
|
964 } |
|
965 } |
|
966 |
5142
|
967 return retval; |
2210
|
968 } |
|
969 |
4185
|
970 static int |
|
971 debug_on_interrupt (void) |
|
972 { |
|
973 Vdebug_on_interrupt = check_preference ("debug_on_interrupt"); |
|
974 |
|
975 return 0; |
|
976 } |
|
977 |
4449
|
978 static int |
|
979 sighup_dumps_octave_core (void) |
|
980 { |
|
981 Vsighup_dumps_octave_core = check_preference ("sighup_dumps_octave_core"); |
|
982 |
|
983 return 0; |
|
984 } |
|
985 |
|
986 static int |
|
987 sigterm_dumps_octave_core (void) |
|
988 { |
|
989 Vsigterm_dumps_octave_core = check_preference ("sigterm_dumps_octave_core"); |
|
990 |
|
991 return 0; |
|
992 } |
|
993 |
4185
|
994 void |
|
995 symbols_of_sighandlers (void) |
|
996 { |
4233
|
997 DEFVAR (debug_on_interrupt, false, debug_on_interrupt, |
4185
|
998 "-*- texinfo -*-\n\ |
|
999 @defvr {Built-in Variable} debug_on_interrupt\n\ |
|
1000 If @code{debug_on_interrupt} is nonzero, Octave will try to enter\n\ |
|
1001 debugging mode when it receives an interrupt signal (typically\n\ |
|
1002 generated with @kbd{C-c}). If a second interrupt signal is received\n\ |
|
1003 before reaching the debugging mode, a normal interrupt will occur.\n\ |
|
1004 The default value is 0.\n\ |
|
1005 @end defvr"); |
4294
|
1006 |
4449
|
1007 DEFVAR (sighup_dumps_octave_core, true, sighup_dumps_octave_core, |
|
1008 "-*- texinfo -*-\n\ |
|
1009 @defvr {Built-in Variable} sighup_dumps_octave_core\n\ |
|
1010 If this variable is set to a nonzero value and\n\ |
|
1011 @code{crash_dumps_octave_core} is also nonzero, Octave tries to save all\n\ |
|
1012 current variables the the file \"octave-core\" if it receives a\n\ |
|
1013 hangup signal. The default value is 1.\n\ |
|
1014 @end defvr"); |
|
1015 |
|
1016 DEFVAR (sigterm_dumps_octave_core, true, sigterm_dumps_octave_core, |
|
1017 "-*- texinfo -*-\n\ |
|
1018 @defvr {Built-in Variable} sigterm_dumps_octave_core\n\ |
|
1019 If this variable is set to a nonzero value and\n\ |
|
1020 @code{crash_dumps_octave_core} is also nonzero, Octave tries to save all\n\ |
|
1021 current variables the the file \"octave-core\" if it receives a\n\ |
|
1022 terminate signal. The default value is 1.\n\ |
|
1023 @end defvr"); |
|
1024 |
4294
|
1025 DEFCONST (SIG, make_sig_struct (), |
|
1026 "-*- texinfo -*-\n\ |
5333
|
1027 @defvr {Built-in Constant} SIG\n\ |
4294
|
1028 Structure of Unix signal names and their defined values.\n\ |
|
1029 @end defvr"); |
4185
|
1030 } |
|
1031 |
1
|
1032 /* |
|
1033 ;;; Local Variables: *** |
|
1034 ;;; mode: C++ *** |
|
1035 ;;; End: *** |
|
1036 */ |