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