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