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