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 |
7016
|
9 Free Software Foundation; either version 3 of the License, or (at your |
|
10 option) any later version. |
1
|
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 |
7016
|
18 along with Octave; see the file COPYING. If not, see |
|
19 <http://www.gnu.org/licenses/>. |
1
|
20 |
|
21 */ |
|
22 |
240
|
23 #ifdef HAVE_CONFIG_H |
1230
|
24 #include <config.h> |
1
|
25 #endif |
|
26 |
2536
|
27 #include <cstdlib> |
|
28 |
3503
|
29 #include <iostream> |
1344
|
30 #include <new> |
|
31 |
1350
|
32 #ifdef HAVE_UNISTD_H |
2442
|
33 #ifdef HAVE_SYS_TYPES_H |
834
|
34 #include <sys/types.h> |
2442
|
35 #endif |
834
|
36 #include <unistd.h> |
|
37 #endif |
1343
|
38 |
3281
|
39 #include "cmd-edit.h" |
5453
|
40 #include "oct-syscalls.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" |
5794
|
54 #include "variables.h" |
1
|
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. |
5794
|
69 static bool Vsighup_dumps_octave_core = true; |
4449
|
70 |
|
71 // Similar to Vsighup_dumps_octave_core, but for SIGTERM signal. |
5794
|
72 static bool Vsigterm_dumps_octave_core = true; |
4449
|
73 |
6098
|
74 #if defined (RETSIGTYPE_IS_VOID) |
2469
|
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 { |
5844
|
223 my_friendly_exit (strsignal (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 ()) |
5844
|
412 user_abort (strsignal (sig), sig); |
5451
|
413 else |
|
414 w32_raise (sig); |
|
415 #else |
5844
|
416 user_abort (strsignal (sig), sig); |
5451
|
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 |
5866
|
632 // SIGPROF |
3174
|
633 // SIGPWR |
|
634 |
1
|
635 #ifdef SIGQUIT |
1446
|
636 octave_set_signal_handler (SIGQUIT, generic_sig_handler); |
1
|
637 #endif |
|
638 |
|
639 #ifdef SIGSEGV |
1446
|
640 octave_set_signal_handler (SIGSEGV, generic_sig_handler); |
1
|
641 #endif |
|
642 |
3174
|
643 // SIGSTOP |
|
644 |
1
|
645 #ifdef SIGSYS |
1446
|
646 octave_set_signal_handler (SIGSYS, generic_sig_handler); |
1
|
647 #endif |
|
648 |
|
649 #ifdef SIGTERM |
4449
|
650 octave_set_signal_handler (SIGTERM, sig_hup_or_term_handler); |
1
|
651 #endif |
|
652 |
|
653 #ifdef SIGTRAP |
1446
|
654 octave_set_signal_handler (SIGTRAP, generic_sig_handler); |
1
|
655 #endif |
|
656 |
3174
|
657 // SIGTSTP |
|
658 // SIGTTIN |
|
659 // SIGTTOU |
|
660 // SIGURG |
|
661 |
1
|
662 #ifdef SIGUSR1 |
1446
|
663 octave_set_signal_handler (SIGUSR1, generic_sig_handler); |
1
|
664 #endif |
|
665 |
|
666 #ifdef SIGUSR2 |
1446
|
667 octave_set_signal_handler (SIGUSR2, generic_sig_handler); |
1
|
668 #endif |
|
669 |
|
670 #ifdef SIGVTALRM |
1446
|
671 octave_set_signal_handler (SIGVTALRM, generic_sig_handler); |
1
|
672 #endif |
|
673 |
895
|
674 #ifdef SIGIO |
1446
|
675 octave_set_signal_handler (SIGIO, SIG_IGN); |
895
|
676 #endif |
|
677 |
3343
|
678 #if 0 |
3281
|
679 #ifdef SIGWINCH |
|
680 octave_set_signal_handler (SIGWINCH, sigwinch_handler); |
|
681 #endif |
3343
|
682 #endif |
3174
|
683 |
1
|
684 #ifdef SIGXCPU |
1446
|
685 octave_set_signal_handler (SIGXCPU, generic_sig_handler); |
1
|
686 #endif |
|
687 |
|
688 #ifdef SIGXFSZ |
1446
|
689 octave_set_signal_handler (SIGXFSZ, generic_sig_handler); |
1
|
690 #endif |
5451
|
691 |
1
|
692 } |
|
693 |
4294
|
694 static Octave_map |
|
695 make_sig_struct (void) |
|
696 { |
|
697 Octave_map m; |
|
698 |
|
699 #ifdef SIGABRT |
4675
|
700 m.assign ("ABRT", SIGABRT); |
4294
|
701 #endif |
|
702 |
|
703 #ifdef SIGALRM |
4675
|
704 m.assign ("ALRM", SIGALRM); |
4294
|
705 #endif |
|
706 |
|
707 #ifdef SIGBUS |
4675
|
708 m.assign ("BUS", SIGBUS); |
4294
|
709 #endif |
|
710 |
|
711 #ifdef SIGCHLD |
4675
|
712 m.assign ("CHLD", SIGCHLD); |
4294
|
713 #endif |
|
714 |
|
715 #ifdef SIGCLD |
4675
|
716 m.assign ("CLD", SIGCLD); |
4294
|
717 #endif |
|
718 |
|
719 #ifdef SIGCONT |
4675
|
720 m.assign ("CONT", SIGCONT); |
4294
|
721 #endif |
|
722 |
|
723 #ifdef SIGEMT |
4675
|
724 m.assign ("EMT", SIGEMT); |
4294
|
725 #endif |
|
726 |
|
727 #ifdef SIGFPE |
4675
|
728 m.assign ("FPE", SIGFPE); |
4294
|
729 #endif |
|
730 |
|
731 #ifdef SIGHUP |
4675
|
732 m.assign ("HUP", SIGHUP); |
4294
|
733 #endif |
|
734 |
|
735 #ifdef SIGILL |
4675
|
736 m.assign ("ILL", SIGILL); |
4294
|
737 #endif |
|
738 |
|
739 #ifdef SIGINFO |
4675
|
740 m.assign ("INFO", SIGINFO); |
4294
|
741 #endif |
|
742 |
|
743 #ifdef SIGINT |
4675
|
744 m.assign ("INT", SIGINT); |
4294
|
745 #endif |
|
746 |
|
747 #ifdef SIGIOT |
4675
|
748 m.assign ("IOT", SIGIOT); |
4294
|
749 #endif |
|
750 |
|
751 #ifdef SIGLOST |
4675
|
752 m.assign ("LOST", SIGLOST); |
4294
|
753 #endif |
|
754 |
|
755 #ifdef SIGPIPE |
4675
|
756 m.assign ("PIPE", SIGPIPE); |
4294
|
757 #endif |
|
758 |
|
759 #ifdef SIGPOLL |
4675
|
760 m.assign ("POLL", SIGPOLL); |
4294
|
761 #endif |
|
762 |
|
763 #ifdef SIGPROF |
4675
|
764 m.assign ("PROF", SIGPROF); |
4294
|
765 #endif |
|
766 |
|
767 #ifdef SIGPWR |
4675
|
768 m.assign ("PWR", SIGPWR); |
4294
|
769 #endif |
|
770 |
|
771 #ifdef SIGQUIT |
4675
|
772 m.assign ("QUIT", SIGQUIT); |
4294
|
773 #endif |
|
774 |
|
775 #ifdef SIGSEGV |
4675
|
776 m.assign ("SEGV", SIGSEGV); |
4294
|
777 #endif |
|
778 |
|
779 #ifdef SIGSTOP |
4675
|
780 m.assign ("STOP", SIGSTOP); |
4294
|
781 #endif |
|
782 |
|
783 #ifdef SIGSYS |
4675
|
784 m.assign ("SYS", SIGSYS); |
4294
|
785 #endif |
|
786 |
|
787 #ifdef SIGTERM |
4675
|
788 m.assign ("TERM", SIGTERM); |
4294
|
789 #endif |
|
790 |
|
791 #ifdef SIGTRAP |
4675
|
792 m.assign ("TRAP", SIGTRAP); |
4294
|
793 #endif |
|
794 |
|
795 #ifdef SIGTSTP |
4675
|
796 m.assign ("TSTP", SIGTSTP); |
4294
|
797 #endif |
|
798 |
|
799 #ifdef SIGTTIN |
4675
|
800 m.assign ("TTIN", SIGTTIN); |
4294
|
801 #endif |
|
802 |
|
803 #ifdef SIGTTOU |
4675
|
804 m.assign ("TTOU", SIGTTOU); |
4294
|
805 #endif |
|
806 |
|
807 #ifdef SIGURG |
4675
|
808 m.assign ("URG", SIGURG); |
4294
|
809 #endif |
|
810 |
|
811 #ifdef SIGUSR1 |
4675
|
812 m.assign ("USR1", SIGUSR1); |
4294
|
813 #endif |
|
814 |
|
815 #ifdef SIGUSR2 |
4675
|
816 m.assign ("USR2", SIGUSR2); |
4294
|
817 #endif |
|
818 |
|
819 #ifdef SIGVTALRM |
4675
|
820 m.assign ("VTALRM", SIGVTALRM); |
4294
|
821 #endif |
|
822 |
|
823 #ifdef SIGIO |
4675
|
824 m.assign ("IO", SIGIO); |
4294
|
825 #endif |
|
826 |
|
827 #ifdef SIGWINCH |
4675
|
828 m.assign ("WINCH", SIGWINCH); |
4294
|
829 #endif |
|
830 |
|
831 #ifdef SIGXCPU |
4675
|
832 m.assign ("XCPU", SIGXCPU); |
4294
|
833 #endif |
|
834 |
|
835 #ifdef SIGXFSZ |
4675
|
836 m.assign ("XFSZ", SIGXFSZ); |
4294
|
837 #endif |
|
838 |
|
839 return m; |
|
840 } |
|
841 |
5142
|
842 octave_child_list::octave_child_list_rep *octave_child_list::instance = 0; |
5128
|
843 |
2926
|
844 bool |
|
845 octave_child_list::instance_ok (void) |
|
846 { |
|
847 bool retval = true; |
|
848 |
|
849 if (! instance) |
5142
|
850 instance = new octave_child_list_rep (); |
2926
|
851 |
|
852 if (! instance) |
|
853 { |
|
854 ::error ("unable to create child list object!"); |
|
855 |
|
856 retval = false; |
|
857 } |
|
858 |
|
859 return retval; |
|
860 } |
|
861 |
|
862 void |
5142
|
863 octave_child_list::insert (pid_t pid, octave_child::child_event_handler f) |
|
864 { |
|
865 if (instance_ok ()) |
|
866 instance->insert (pid, f); |
|
867 } |
|
868 |
|
869 void |
|
870 octave_child_list::reap (void) |
2926
|
871 { |
|
872 if (instance_ok ()) |
5142
|
873 instance->reap (); |
|
874 } |
|
875 |
|
876 bool |
|
877 octave_child_list::wait (void) |
|
878 { |
|
879 return (instance_ok ()) ? instance->wait () : false; |
2926
|
880 } |
|
881 |
5142
|
882 class pid_equal |
|
883 { |
|
884 public: |
|
885 |
|
886 pid_equal (pid_t v) : val (v) { } |
|
887 |
|
888 bool operator () (const octave_child& oc) const { return oc.pid == val; } |
|
889 |
|
890 private: |
|
891 |
|
892 pid_t val; |
|
893 }; |
|
894 |
2926
|
895 void |
|
896 octave_child_list::remove (pid_t pid) |
|
897 { |
|
898 if (instance_ok ()) |
5142
|
899 instance->remove_if (pid_equal (pid)); |
2926
|
900 } |
|
901 |
5142
|
902 #define OCL_REP octave_child_list::octave_child_list_rep |
2926
|
903 |
5142
|
904 void |
|
905 OCL_REP::insert (pid_t pid, octave_child::child_event_handler f) |
2926
|
906 { |
5142
|
907 append (octave_child (pid, f)); |
2926
|
908 } |
|
909 |
2209
|
910 void |
5142
|
911 OCL_REP::reap (void) |
2209
|
912 { |
5142
|
913 // Mark the record for PID invalid. |
2209
|
914 |
5142
|
915 for (iterator p = begin (); p != end (); p++) |
|
916 { |
|
917 // The call to the octave_child::child_event_handler might |
|
918 // invalidate the iterator (for example, by calling |
|
919 // octave_child_list::remove), so we increment the iterator |
|
920 // here. |
2209
|
921 |
5142
|
922 octave_child& oc = *p; |
2209
|
923 |
5142
|
924 if (oc.have_status) |
2209
|
925 { |
5142
|
926 oc.have_status = 0; |
|
927 |
|
928 octave_child::child_event_handler f = oc.handler; |
|
929 |
|
930 if (f && f (oc.pid, oc.status)) |
|
931 oc.pid = -1; |
2209
|
932 } |
|
933 } |
|
934 |
5142
|
935 remove_if (pid_equal (-1)); |
2209
|
936 } |
|
937 |
5142
|
938 // Wait on our children and record any changes in their status. |
|
939 |
|
940 bool |
|
941 OCL_REP::wait (void) |
2210
|
942 { |
5142
|
943 bool retval = false; |
2210
|
944 |
5142
|
945 for (iterator p = begin (); p != end (); p++) |
2210
|
946 { |
5142
|
947 octave_child& oc = *p; |
|
948 |
|
949 pid_t pid = oc.pid; |
|
950 |
|
951 if (pid > 0) |
|
952 { |
|
953 int status; |
2210
|
954 |
5453
|
955 if (octave_syscalls::waitpid (pid, &status, WNOHANG) > 0) |
5142
|
956 { |
|
957 oc.have_status = 1; |
|
958 |
|
959 oc.status = status; |
|
960 |
|
961 retval = true; |
|
962 |
|
963 break; |
|
964 } |
2210
|
965 } |
|
966 } |
|
967 |
5142
|
968 return retval; |
2210
|
969 } |
|
970 |
5780
|
971 DEFUN (SIG, args, , |
|
972 "-*- texinfo -*-\n\ |
|
973 @deftypefn {Built-in Function} {} SIG ()\n\ |
|
974 Return a structure containing Unix signal names and their defined values.\n\ |
|
975 @end deftypefn") |
|
976 { |
|
977 octave_value retval; |
|
978 |
|
979 if (args.length () == 0) |
|
980 { |
|
981 static Octave_map m = make_sig_struct (); |
|
982 |
|
983 retval = m; |
|
984 } |
|
985 else |
5823
|
986 print_usage (); |
5780
|
987 |
|
988 return retval; |
|
989 } |
|
990 |
5794
|
991 DEFUN (debug_on_interrupt, args, nargout, |
|
992 "-*- texinfo -*-\n\ |
|
993 @deftypefn {Built-in Function} {@var{val} =} debug_on_interrupt ()\n\ |
|
994 @deftypefnx {Built-in Function} {@var{old_val} =} debug_on_interrupt (@var{new_val})\n\ |
|
995 Query or set the internal variable that controls whether Octave will try\n\ |
|
996 to enter debugging mode when it receives an interrupt signal (typically\n\ |
|
997 generated with @kbd{C-c}). If a second interrupt signal is received\n\ |
|
998 before reaching the debugging mode, a normal interrupt will occur.\n\ |
|
999 @end deftypefn") |
4449
|
1000 { |
5794
|
1001 return SET_INTERNAL_VARIABLE (debug_on_interrupt); |
4449
|
1002 } |
|
1003 |
5794
|
1004 DEFUN (sighup_dumps_octave_core, args, nargout, |
|
1005 "-*- texinfo -*-\n\ |
|
1006 @deftypefn {Built-in Function} {@var{val} =} sighup_dumps_octave_core ()\n\ |
|
1007 @deftypefnx {Built-in Function} {@var{old_val} =} sighup_dumps_octave_core (@var{new_val})\n\ |
|
1008 Query or set the internal variable that controls whether Octave tries\n\ |
6653
|
1009 to save all current variables to the file \"octave-core\" if it receives\n\ |
5794
|
1010 a hangup signal.\n\ |
|
1011 @end deftypefn") |
4185
|
1012 { |
5794
|
1013 return SET_INTERNAL_VARIABLE (sighup_dumps_octave_core); |
|
1014 } |
4294
|
1015 |
5794
|
1016 DEFUN (sigterm_dumps_octave_core, args, nargout, |
|
1017 "-*- texinfo -*-\n\ |
|
1018 @deftypefn {Built-in Function} {@var{val} =} sigterm_dumps_octave_core ()\n\ |
|
1019 @deftypefnx {Built-in Function} {@var{old_val} =} sigterm_dumps_octave_core (@var{new_val})\n\ |
|
1020 Query or set the internal variable that controls whether Octave tries\n\ |
6653
|
1021 to save all current variables to the file \"octave-core\" if it receives\n\ |
5794
|
1022 a terminate signal.\n\ |
|
1023 @end deftypefn") |
|
1024 { |
|
1025 return SET_INTERNAL_VARIABLE (sigterm_dumps_octave_core); |
4185
|
1026 } |
|
1027 |
1
|
1028 /* |
|
1029 ;;; Local Variables: *** |
|
1030 ;;; mode: C++ *** |
|
1031 ;;; End: *** |
|
1032 */ |