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