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 |
1315
|
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
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" |
|
40 |
1352
|
41 #include "error.h" |
1373
|
42 #include "load-save.h" |
2091
|
43 #include "pager.h" |
1
|
44 #include "sighandlers.h" |
1352
|
45 #include "syswait.h" |
2091
|
46 #include "toplev.h" |
1
|
47 #include "utils.h" |
|
48 |
|
49 // Nonzero means we have already printed a message for this series of |
|
50 // SIGPIPES. We assume that the writer will eventually give up. |
|
51 int pipe_handler_error_count = 0; |
|
52 |
3018
|
53 // TRUE means we can be interrupted. |
|
54 bool can_interrupt = false; |
1
|
55 |
2016
|
56 // Allow us to save the signal mask and then restore it to the most |
|
57 // recently saved value. This is necessary when using the POSIX |
|
58 // signal handling interface on some systems calling longjmp out of |
|
59 // the signal handler to get to the top level on an interrupt doesn't |
|
60 // restore the original signal mask. Alternatively, we could use |
|
61 // sigsetjmp/siglongjmp, but saving and restoring the signal mask |
|
62 // ourselves works ok and seems simpler just now. |
|
63 |
|
64 #if defined (HAVE_POSIX_SIGNALS) |
|
65 static sigset_t octave_signal_mask; |
|
66 #endif |
|
67 |
2469
|
68 #if RETSIGTYPE == void |
|
69 #define SIGHANDLER_RETURN(status) return |
|
70 #else |
|
71 #define SIGHANDLER_RETURN(status) return status |
|
72 #endif |
|
73 |
2554
|
74 #if defined (MUST_REINSTALL_SIGHANDLERS) |
|
75 #define MAYBE_REINSTALL_SIGHANDLER(sig, handler) \ |
|
76 octave_set_signal_handler (sig, handler) |
2931
|
77 #define REINSTALL_USES_SIG 1 |
2554
|
78 #else |
|
79 #define MAYBE_REINSTALL_SIGHANDLER(sig, handler) \ |
|
80 do { } while (0) |
|
81 #endif |
|
82 |
|
83 #if defined (__EMX__) |
2626
|
84 #define MAYBE_ACK_SIGNAL(sig) \ |
2554
|
85 octave_set_signal_handler (sig, SIG_ACK) |
2931
|
86 #define ACK_USES_SIG 1 |
2554
|
87 #else |
2626
|
88 #define MAYBE_ACK_SIGNAL(sig) \ |
2554
|
89 do { } while (0) |
|
90 #endif |
|
91 |
3162
|
92 #if defined (SIGABRT) |
|
93 #define OCTAVE_MEMORY_EXHAUSTED_ERROR SIGABRT |
|
94 #else |
|
95 #define OCTAVE_MEMORY_EXHAUSTED_ERROR (-1) |
|
96 #endif |
|
97 |
2016
|
98 void |
|
99 octave_save_signal_mask (void) |
|
100 { |
|
101 #if defined (HAVE_POSIX_SIGNALS) |
|
102 sigprocmask (0, 0, &octave_signal_mask); |
|
103 #endif |
|
104 } |
|
105 |
|
106 void |
|
107 octave_restore_signal_mask (void) |
|
108 { |
|
109 #if defined (HAVE_POSIX_SIGNALS) |
|
110 sigprocmask (SIG_SETMASK, &octave_signal_mask, 0); |
|
111 #endif |
|
112 } |
|
113 |
1
|
114 static void |
|
115 my_friendly_exit (const char *sig_name, int sig_number) |
|
116 { |
2536
|
117 static bool been_there_done_that = false; |
|
118 |
|
119 if (been_there_done_that) |
|
120 { |
2554
|
121 #if defined (SIGABRT) |
2536
|
122 octave_set_signal_handler (SIGABRT, SIG_DFL); |
|
123 #endif |
|
124 |
3644
|
125 std::cerr << "panic: attempted clean up apparently failed -- aborting...\n"; |
2536
|
126 abort (); |
|
127 } |
|
128 else |
|
129 { |
|
130 been_there_done_that = true; |
1373
|
131 |
3644
|
132 std::cerr << "panic: " << sig_name << " -- stopping myself...\n"; |
2536
|
133 |
|
134 save_user_variables (); |
|
135 |
3162
|
136 if (sig_number < 0) |
|
137 exit (1); |
|
138 else |
|
139 { |
|
140 octave_set_signal_handler (sig_number, SIG_DFL); |
|
141 |
|
142 kill (getpid (), sig_number); |
|
143 } |
2536
|
144 } |
1
|
145 } |
|
146 |
635
|
147 // I know, not really a signal handler. |
|
148 |
|
149 static void |
|
150 octave_new_handler (void) |
|
151 { |
3531
|
152 std::cerr << "error: memory exhausted -- trying to return to prompt\n"; |
1394
|
153 |
|
154 if (can_interrupt) |
|
155 { |
|
156 jump_to_top_level (); |
|
157 panic_impossible (); |
|
158 } |
|
159 else |
3162
|
160 my_friendly_exit ("operator new", OCTAVE_MEMORY_EXHAUSTED_ERROR); |
635
|
161 } |
|
162 |
1446
|
163 sig_handler * |
|
164 octave_set_signal_handler (int sig, sig_handler *handler) |
|
165 { |
|
166 #if defined (HAVE_POSIX_SIGNALS) |
|
167 struct sigaction act, oact; |
|
168 act.sa_handler = handler; |
|
169 act.sa_flags = 0; |
|
170 sigemptyset (&act.sa_mask); |
|
171 sigemptyset (&oact.sa_mask); |
|
172 sigaction (sig, &act, &oact); |
|
173 return oact.sa_handler; |
|
174 #else |
|
175 return signal (sig, handler); |
|
176 #endif |
|
177 } |
|
178 |
1
|
179 static RETSIGTYPE |
1446
|
180 generic_sig_handler (int sig) |
1
|
181 { |
1446
|
182 my_friendly_exit (sys_siglist[sig], sig); |
1
|
183 |
2469
|
184 SIGHANDLER_RETURN (0); |
1
|
185 } |
|
186 |
2091
|
187 // Handle SIGCHLD. |
1230
|
188 |
|
189 static RETSIGTYPE |
2092
|
190 sigchld_handler (int /* sig */) |
1230
|
191 { |
2705
|
192 volatile octave_interrupt_handler saved_interrupt_handler |
2554
|
193 = octave_ignore_interrupts (); |
|
194 |
2626
|
195 // I wonder if this is really right, or if SIGCHLD should just be |
|
196 // blocked on OS/2 systems the same as for systems with POSIX signal |
|
197 // functions. |
|
198 |
|
199 #if defined (__EMX__) |
2554
|
200 volatile sig_handler *saved_sigchld_handler |
2626
|
201 = octave_set_signal_handler (SIGCHLD, SIG_IGN); |
2554
|
202 #endif |
|
203 |
2626
|
204 sigset_t set, oset; |
|
205 |
|
206 BLOCK_CHILD (set, oset); |
|
207 |
2209
|
208 int n = octave_child_list::length (); |
|
209 |
3252
|
210 for (int i = 0; i < n; i++) |
2554
|
211 { |
3252
|
212 octave_child& elt = octave_child_list::elem (i); |
|
213 |
|
214 pid_t pid = elt.pid; |
2209
|
215 |
3252
|
216 if (pid > 0) |
|
217 { |
|
218 int status; |
2209
|
219 |
3252
|
220 if (waitpid (pid, &status, WNOHANG) > 0) |
2091
|
221 { |
3252
|
222 elt.pid = -1; |
2554
|
223 |
3252
|
224 octave_child::dead_child_handler f = elt.handler; |
2197
|
225 |
3252
|
226 if (f) |
|
227 f (pid, status); |
2197
|
228 |
3252
|
229 break; |
2091
|
230 } |
1230
|
231 } |
|
232 } |
2469
|
233 |
2600
|
234 octave_set_interrupt_handler (saved_interrupt_handler); |
2554
|
235 |
2626
|
236 UNBLOCK_CHILD (oset); |
|
237 |
|
238 #ifdef __EMX__ |
2554
|
239 octave_set_signal_handler (SIGCHLD, saved_sigchld_handler); |
2475
|
240 #endif |
|
241 |
2626
|
242 MAYBE_ACK_SIGNAL (SIGCHLD); |
2554
|
243 |
|
244 MAYBE_REINSTALL_SIGHANDLER (SIGCHLD, sigchld_handler); |
|
245 |
2469
|
246 SIGHANDLER_RETURN (0); |
1230
|
247 } |
|
248 |
1373
|
249 #if defined (__alpha__) |
|
250 static RETSIGTYPE |
1488
|
251 sigfpe_handler (int /* sig */) |
1373
|
252 { |
2626
|
253 MAYBE_ACK_SIGNAL (SIGFPE); |
2554
|
254 |
|
255 MAYBE_REINSTALL_SIGHANDLER (SIGFPE, sigfpe_handler); |
1373
|
256 |
3531
|
257 std::cerr << "error: floating point exception -- trying to return to prompt\n"; |
1373
|
258 |
|
259 if (can_interrupt) |
|
260 { |
|
261 jump_to_top_level (); |
|
262 panic_impossible (); |
|
263 } |
|
264 |
2469
|
265 SIGHANDLER_RETURN (0); |
1373
|
266 } |
|
267 #endif |
|
268 |
3343
|
269 #if 0 |
3281
|
270 #if defined (SIGWINCH) |
|
271 static RETSIGTYPE |
|
272 sigwinch_handler (int /* sig */) |
|
273 { |
|
274 MAYBE_ACK_SIGNAL (SIGWINCH); |
|
275 |
|
276 MAYBE_REINSTALL_SIGHANDLER (SIGWINCH, sigwinch_handler); |
|
277 |
|
278 command_editor::resize_terminal (); |
|
279 |
|
280 SIGHANDLER_RETURN (0); |
|
281 } |
|
282 #endif |
3343
|
283 #endif |
3281
|
284 |
635
|
285 // Handle SIGINT by restarting the parser (see octave.cc). |
2554
|
286 // |
|
287 // This also has to work for SIGBREAK (on systems that have it), so we |
|
288 // use the value of sig, instead of just assuming that it is called |
|
289 // for SIGINT only. |
635
|
290 |
1
|
291 static RETSIGTYPE |
2931
|
292 #if defined (ACK_USES_SIG) || defined (REINSTALL_USES_SIG) |
2554
|
293 sigint_handler (int sig) |
2931
|
294 #else |
|
295 sigint_handler (int) |
|
296 #endif |
1
|
297 { |
2626
|
298 MAYBE_ACK_SIGNAL (sig); |
2554
|
299 |
|
300 MAYBE_REINSTALL_SIGHANDLER (sig, sigint_handler); |
834
|
301 |
1
|
302 if (can_interrupt) |
|
303 { |
|
304 jump_to_top_level (); |
|
305 panic_impossible (); |
|
306 } |
|
307 |
2469
|
308 SIGHANDLER_RETURN (0); |
1
|
309 } |
|
310 |
|
311 static RETSIGTYPE |
1488
|
312 sigpipe_handler (int /* sig */) |
1
|
313 { |
2626
|
314 MAYBE_ACK_SIGNAL (SIGPIPE); |
2554
|
315 |
|
316 MAYBE_REINSTALL_SIGHANDLER (SIGPIPE, sigpipe_handler); |
834
|
317 |
1
|
318 if (pipe_handler_error_count++ == 0) |
3531
|
319 std::cerr << "warning: broken pipe\n"; |
1
|
320 |
1358
|
321 // Don't loop forever on account of this. |
|
322 |
1
|
323 if (pipe_handler_error_count > 100) |
|
324 jump_to_top_level (); |
|
325 |
2469
|
326 SIGHANDLER_RETURN (0); |
1
|
327 } |
|
328 |
2705
|
329 octave_interrupt_handler |
2554
|
330 octave_catch_interrupts (void) |
|
331 { |
2705
|
332 octave_interrupt_handler retval; |
2554
|
333 |
|
334 #ifdef SIGINT |
2705
|
335 retval.int_handler = octave_set_signal_handler (SIGINT, sigint_handler); |
2554
|
336 #endif |
|
337 |
|
338 #ifdef SIGBREAK |
2705
|
339 retval.brk_handler = octave_set_signal_handler (SIGBREAK, sigint_handler); |
2554
|
340 #endif |
2705
|
341 |
|
342 return retval; |
2554
|
343 } |
|
344 |
2705
|
345 octave_interrupt_handler |
|
346 octave_ignore_interrupts (void) |
2554
|
347 { |
2705
|
348 octave_interrupt_handler retval; |
|
349 |
2554
|
350 #ifdef SIGINT |
2705
|
351 retval.int_handler = octave_set_signal_handler (SIGINT, SIG_IGN); |
2554
|
352 #endif |
|
353 |
|
354 #ifdef SIGBREAK |
2705
|
355 retval.brk_handler = octave_set_signal_handler (SIGBREAK, SIG_IGN); |
2554
|
356 #endif |
2705
|
357 |
|
358 return retval; |
|
359 } |
|
360 |
|
361 octave_interrupt_handler |
|
362 octave_set_interrupt_handler (const volatile octave_interrupt_handler& h) |
|
363 { |
|
364 octave_interrupt_handler retval; |
|
365 |
|
366 #ifdef SIGINT |
|
367 retval.int_handler = octave_set_signal_handler (SIGINT, h.int_handler); |
|
368 #endif |
|
369 |
|
370 #ifdef SIGBREAK |
|
371 retval.brk_handler = octave_set_signal_handler (SIGBREAK, h.brk_handler); |
|
372 #endif |
|
373 |
|
374 return retval; |
1651
|
375 } |
|
376 |
635
|
377 // Install all the handlers for the signals we might care about. |
|
378 |
1
|
379 void |
|
380 install_signal_handlers (void) |
|
381 { |
3503
|
382 std::set_new_handler (octave_new_handler); |
635
|
383 |
2554
|
384 octave_catch_interrupts (); |
|
385 |
1
|
386 #ifdef SIGABRT |
1446
|
387 octave_set_signal_handler (SIGABRT, generic_sig_handler); |
1
|
388 #endif |
|
389 |
|
390 #ifdef SIGALRM |
1446
|
391 octave_set_signal_handler (SIGALRM, generic_sig_handler); |
1
|
392 #endif |
|
393 |
|
394 #ifdef SIGBUS |
1446
|
395 octave_set_signal_handler (SIGBUS, generic_sig_handler); |
1
|
396 #endif |
|
397 |
1230
|
398 #ifdef SIGCHLD |
1446
|
399 octave_set_signal_handler (SIGCHLD, sigchld_handler); |
1230
|
400 #endif |
|
401 |
3174
|
402 // SIGCLD |
|
403 // SIGCONT |
|
404 |
1
|
405 #ifdef SIGEMT |
1446
|
406 octave_set_signal_handler (SIGEMT, generic_sig_handler); |
1
|
407 #endif |
|
408 |
|
409 #ifdef SIGFPE |
1373
|
410 #if defined (__alpha__) |
1446
|
411 octave_set_signal_handler (SIGFPE, sigfpe_handler); |
1373
|
412 #else |
1446
|
413 octave_set_signal_handler (SIGFPE, generic_sig_handler); |
1
|
414 #endif |
1373
|
415 #endif |
1
|
416 |
|
417 #ifdef SIGHUP |
1446
|
418 octave_set_signal_handler (SIGHUP, generic_sig_handler); |
1
|
419 #endif |
|
420 |
|
421 #ifdef SIGILL |
1446
|
422 octave_set_signal_handler (SIGILL, generic_sig_handler); |
1
|
423 #endif |
|
424 |
3174
|
425 // SIGINFO |
|
426 // SIGINT |
|
427 |
1
|
428 #ifdef SIGIOT |
1446
|
429 octave_set_signal_handler (SIGIOT, generic_sig_handler); |
1
|
430 #endif |
|
431 |
|
432 #ifdef SIGLOST |
1446
|
433 octave_set_signal_handler (SIGLOST, generic_sig_handler); |
1
|
434 #endif |
|
435 |
|
436 #ifdef SIGPIPE |
1446
|
437 octave_set_signal_handler (SIGPIPE, sigpipe_handler); |
1
|
438 #endif |
|
439 |
|
440 #ifdef SIGPOLL |
1446
|
441 octave_set_signal_handler (SIGPOLL, SIG_IGN); |
1
|
442 #endif |
|
443 |
|
444 #ifdef SIGPROF |
1446
|
445 octave_set_signal_handler (SIGPROF, generic_sig_handler); |
1
|
446 #endif |
|
447 |
3174
|
448 // SIGPWR |
|
449 |
1
|
450 #ifdef SIGQUIT |
1446
|
451 octave_set_signal_handler (SIGQUIT, generic_sig_handler); |
1
|
452 #endif |
|
453 |
|
454 #ifdef SIGSEGV |
1446
|
455 octave_set_signal_handler (SIGSEGV, generic_sig_handler); |
1
|
456 #endif |
|
457 |
3174
|
458 // SIGSTOP |
|
459 |
1
|
460 #ifdef SIGSYS |
1446
|
461 octave_set_signal_handler (SIGSYS, generic_sig_handler); |
1
|
462 #endif |
|
463 |
|
464 #ifdef SIGTERM |
1446
|
465 octave_set_signal_handler (SIGTERM, generic_sig_handler); |
1
|
466 #endif |
|
467 |
|
468 #ifdef SIGTRAP |
1446
|
469 octave_set_signal_handler (SIGTRAP, generic_sig_handler); |
1
|
470 #endif |
|
471 |
3174
|
472 // SIGTSTP |
|
473 // SIGTTIN |
|
474 // SIGTTOU |
|
475 // SIGURG |
|
476 |
1
|
477 #ifdef SIGUSR1 |
1446
|
478 octave_set_signal_handler (SIGUSR1, generic_sig_handler); |
1
|
479 #endif |
|
480 |
|
481 #ifdef SIGUSR2 |
1446
|
482 octave_set_signal_handler (SIGUSR2, generic_sig_handler); |
1
|
483 #endif |
|
484 |
|
485 #ifdef SIGVTALRM |
1446
|
486 octave_set_signal_handler (SIGVTALRM, generic_sig_handler); |
1
|
487 #endif |
|
488 |
895
|
489 #ifdef SIGIO |
1446
|
490 octave_set_signal_handler (SIGIO, SIG_IGN); |
895
|
491 #endif |
|
492 |
3343
|
493 #if 0 |
3281
|
494 #ifdef SIGWINCH |
|
495 octave_set_signal_handler (SIGWINCH, sigwinch_handler); |
|
496 #endif |
3343
|
497 #endif |
3174
|
498 |
1
|
499 #ifdef SIGXCPU |
1446
|
500 octave_set_signal_handler (SIGXCPU, generic_sig_handler); |
1
|
501 #endif |
|
502 |
|
503 #ifdef SIGXFSZ |
1446
|
504 octave_set_signal_handler (SIGXFSZ, generic_sig_handler); |
1
|
505 #endif |
|
506 } |
|
507 |
2209
|
508 octave_child_list *octave_child_list::instance = 0; |
|
509 |
2926
|
510 bool |
|
511 octave_child_list::instance_ok (void) |
|
512 { |
|
513 bool retval = true; |
|
514 |
|
515 if (! instance) |
|
516 instance = new octave_child_list (); |
|
517 |
|
518 if (! instance) |
|
519 { |
|
520 ::error ("unable to create child list object!"); |
|
521 |
|
522 retval = false; |
|
523 } |
|
524 |
|
525 return retval; |
|
526 } |
|
527 |
|
528 void |
|
529 octave_child_list::insert (pid_t pid, octave_child::dead_child_handler f) |
|
530 { |
|
531 if (instance_ok ()) |
|
532 instance->do_insert (pid, f); |
|
533 } |
|
534 |
|
535 void |
|
536 octave_child_list::remove (pid_t pid) |
|
537 { |
|
538 if (instance_ok ()) |
|
539 instance->do_remove (pid); |
|
540 } |
|
541 |
|
542 int |
|
543 octave_child_list::length (void) |
|
544 { |
|
545 return (instance_ok ()) ? instance->do_length () : 0; |
|
546 } |
|
547 |
|
548 octave_child& |
|
549 octave_child_list::elem (int i) |
|
550 { |
|
551 static octave_child foo; |
|
552 |
|
553 return (instance_ok ()) ? instance->do_elem (i) : foo; |
|
554 } |
|
555 |
2209
|
556 void |
|
557 octave_child_list::do_insert (pid_t pid, octave_child::dead_child_handler f) |
|
558 { |
|
559 // Insert item in first open slot, increasing size of list if |
|
560 // necessary. |
|
561 |
|
562 bool enlarge = true; |
|
563 |
|
564 for (int i = 0; i < curr_len; i++) |
|
565 { |
2305
|
566 octave_child& tmp = list (i); |
2209
|
567 |
|
568 if (tmp.pid < 0) |
|
569 { |
2305
|
570 list (i) = octave_child (pid, f); |
2209
|
571 enlarge = false; |
|
572 break; |
|
573 } |
|
574 } |
|
575 |
|
576 if (enlarge) |
|
577 { |
|
578 int total_len = list.length (); |
|
579 |
|
580 if (curr_len == total_len) |
|
581 { |
|
582 if (total_len == 0) |
|
583 list.resize (16); |
|
584 else |
|
585 list.resize (total_len * 2); |
|
586 } |
|
587 |
2305
|
588 list (curr_len) = octave_child (pid, f); |
2209
|
589 curr_len++; |
|
590 } |
|
591 } |
|
592 |
|
593 void |
2210
|
594 octave_child_list::do_remove (pid_t pid) |
|
595 { |
|
596 // Mark the record for PID invalid. |
|
597 |
|
598 for (int i = 0; i < curr_len; i++) |
|
599 { |
2305
|
600 octave_child& tmp = list (i); |
2210
|
601 |
|
602 if (tmp.pid == pid) |
|
603 { |
|
604 tmp.pid = -1; |
|
605 break; |
|
606 } |
|
607 } |
|
608 } |
|
609 |
2926
|
610 int |
|
611 octave_child_list::do_length (void) const |
|
612 { |
|
613 return curr_len; |
|
614 } |
|
615 |
|
616 octave_child& |
|
617 octave_child_list::do_elem (int i) |
2210
|
618 { |
2926
|
619 static octave_child foo; |
|
620 |
|
621 int n = do_length (); |
2210
|
622 |
2926
|
623 if (i >= 0 && i < n) |
|
624 return list (i); |
2210
|
625 else |
2926
|
626 return foo; |
2210
|
627 } |
|
628 |
1
|
629 /* |
|
630 ;;; Local Variables: *** |
|
631 ;;; mode: C++ *** |
|
632 ;;; End: *** |
|
633 */ |