1
|
1 /* |
|
2 |
1884
|
3 Copyright (C) 1996 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> |
1343
|
28 #include <csignal> |
2536
|
29 |
1344
|
30 #include <new> |
|
31 |
1350
|
32 #include <iostream.h> |
|
33 |
|
34 #ifdef HAVE_UNISTD_H |
2442
|
35 #ifdef HAVE_SYS_TYPES_H |
834
|
36 #include <sys/types.h> |
2442
|
37 #endif |
834
|
38 #include <unistd.h> |
|
39 #endif |
1343
|
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 |
|
53 // Nonzero means we can be interrupted. |
|
54 int can_interrupt = 0; |
|
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 |
2016
|
74 void |
|
75 octave_save_signal_mask (void) |
|
76 { |
|
77 #if defined (HAVE_POSIX_SIGNALS) |
|
78 sigprocmask (0, 0, &octave_signal_mask); |
|
79 #endif |
|
80 } |
|
81 |
|
82 void |
|
83 octave_restore_signal_mask (void) |
|
84 { |
|
85 #if defined (HAVE_POSIX_SIGNALS) |
|
86 sigprocmask (SIG_SETMASK, &octave_signal_mask, 0); |
|
87 #endif |
|
88 } |
|
89 |
1
|
90 static void |
|
91 my_friendly_exit (const char *sig_name, int sig_number) |
|
92 { |
2536
|
93 static bool been_there_done_that = false; |
|
94 |
|
95 if (been_there_done_that) |
|
96 { |
|
97 #ifdef SIGABRT |
|
98 octave_set_signal_handler (SIGABRT, SIG_DFL); |
|
99 #endif |
|
100 |
|
101 error ("attempted clean up seems to have failed -- aborting..."); |
1373
|
102 |
2536
|
103 abort (); |
|
104 } |
|
105 else |
|
106 { |
|
107 been_there_done_that = true; |
1373
|
108 |
2536
|
109 error ("%s -- stopping myself...", sig_name); |
|
110 |
|
111 save_user_variables (); |
|
112 |
|
113 clean_up_and_exit (sig_number); |
|
114 } |
1
|
115 } |
|
116 |
635
|
117 // I know, not really a signal handler. |
|
118 |
|
119 static void |
|
120 octave_new_handler (void) |
|
121 { |
1394
|
122 error ("memory exhausted -- trying to return to prompt"); |
|
123 |
|
124 if (can_interrupt) |
|
125 { |
|
126 jump_to_top_level (); |
|
127 panic_impossible (); |
|
128 } |
|
129 else |
1395
|
130 my_friendly_exit ("operator new", 1); |
635
|
131 } |
|
132 |
1446
|
133 sig_handler * |
|
134 octave_set_signal_handler (int sig, sig_handler *handler) |
|
135 { |
|
136 #if defined (HAVE_POSIX_SIGNALS) |
|
137 struct sigaction act, oact; |
|
138 act.sa_handler = handler; |
|
139 act.sa_flags = 0; |
|
140 sigemptyset (&act.sa_mask); |
|
141 sigemptyset (&oact.sa_mask); |
|
142 sigaction (sig, &act, &oact); |
|
143 return oact.sa_handler; |
|
144 #else |
|
145 return signal (sig, handler); |
|
146 #endif |
|
147 } |
|
148 |
1
|
149 static RETSIGTYPE |
1446
|
150 generic_sig_handler (int sig) |
1
|
151 { |
1446
|
152 my_friendly_exit (sys_siglist[sig], sig); |
1
|
153 |
2469
|
154 SIGHANDLER_RETURN (0); |
1
|
155 } |
|
156 |
2091
|
157 // Handle SIGCHLD. |
1230
|
158 |
|
159 static RETSIGTYPE |
2092
|
160 sigchld_handler (int /* sig */) |
1230
|
161 { |
2209
|
162 int n = octave_child_list::length (); |
|
163 |
|
164 for (int i = 0; i < n; i++) |
1230
|
165 { |
2209
|
166 octave_child& elt = octave_child_list::elem (i); |
|
167 |
|
168 pid_t pid = elt.pid; |
2197
|
169 |
|
170 if (pid > 0) |
1230
|
171 { |
2209
|
172 int status; |
|
173 |
2210
|
174 if (waitpid (pid, &status, WNOHANG) > 0) |
2091
|
175 { |
2209
|
176 elt.pid = -1; |
2197
|
177 |
2209
|
178 octave_child::dead_child_handler f = elt.handler; |
2197
|
179 |
2209
|
180 if (f) |
2475
|
181 f (pid, status); |
2209
|
182 |
|
183 break; |
2091
|
184 } |
1230
|
185 } |
|
186 } |
2469
|
187 |
2475
|
188 #ifdef MUST_REINSTALL_SIGHANDLERS |
|
189 octave_set_signal_handler (SIGCHLD, sigchld_handler); |
|
190 #endif |
|
191 |
2469
|
192 SIGHANDLER_RETURN (0); |
1230
|
193 } |
|
194 |
1373
|
195 #if defined (__alpha__) |
|
196 static RETSIGTYPE |
1488
|
197 sigfpe_handler (int /* sig */) |
1373
|
198 { |
2469
|
199 #ifdef MUST_REINSTALL_SIGHANDLERS |
1446
|
200 octave_set_signal_handler (SIGFPE, sigfpe_handler); |
2469
|
201 #endif |
1373
|
202 |
1394
|
203 error ("floating point exception -- trying to return to prompt"); |
1373
|
204 |
|
205 if (can_interrupt) |
|
206 { |
|
207 jump_to_top_level (); |
|
208 panic_impossible (); |
|
209 } |
|
210 |
2469
|
211 SIGHANDLER_RETURN (0); |
1373
|
212 } |
|
213 #endif |
|
214 |
635
|
215 // Handle SIGINT by restarting the parser (see octave.cc). |
|
216 |
1
|
217 static RETSIGTYPE |
1488
|
218 sigint_handler (int /* sig */) |
1
|
219 { |
2469
|
220 #ifdef MUST_REINSTALL_SIGHANDLERS |
1446
|
221 octave_set_signal_handler (SIGINT, sigint_handler); |
2469
|
222 #endif |
834
|
223 |
1
|
224 if (can_interrupt) |
|
225 { |
|
226 jump_to_top_level (); |
|
227 panic_impossible (); |
|
228 } |
|
229 |
2469
|
230 SIGHANDLER_RETURN (0); |
1
|
231 } |
|
232 |
|
233 static RETSIGTYPE |
1488
|
234 sigpipe_handler (int /* sig */) |
1
|
235 { |
2469
|
236 #ifdef MUST_REINSTALL_SIGHANDLERS |
1446
|
237 octave_set_signal_handler (SIGPIPE, sigpipe_handler); |
2469
|
238 #endif |
834
|
239 |
1
|
240 if (pipe_handler_error_count++ == 0) |
2206
|
241 warning ("broken pipe"); |
1
|
242 |
1358
|
243 // Don't loop forever on account of this. |
|
244 |
1
|
245 if (pipe_handler_error_count > 100) |
|
246 jump_to_top_level (); |
|
247 |
2469
|
248 SIGHANDLER_RETURN (0); |
1
|
249 } |
|
250 |
1651
|
251 void |
|
252 catch_interrupts (void) |
|
253 { |
|
254 octave_set_signal_handler (SIGINT, sigint_handler); |
|
255 } |
|
256 |
635
|
257 // Install all the handlers for the signals we might care about. |
|
258 |
1
|
259 void |
|
260 install_signal_handlers (void) |
|
261 { |
635
|
262 set_new_handler (octave_new_handler); |
|
263 |
1
|
264 #ifdef SIGABRT |
1446
|
265 octave_set_signal_handler (SIGABRT, generic_sig_handler); |
1
|
266 #endif |
|
267 |
|
268 #ifdef SIGALRM |
1446
|
269 octave_set_signal_handler (SIGALRM, generic_sig_handler); |
1
|
270 #endif |
|
271 |
|
272 #ifdef SIGBUS |
1446
|
273 octave_set_signal_handler (SIGBUS, generic_sig_handler); |
1
|
274 #endif |
|
275 |
1230
|
276 #ifdef SIGCHLD |
1446
|
277 octave_set_signal_handler (SIGCHLD, sigchld_handler); |
1230
|
278 #endif |
|
279 |
1
|
280 #ifdef SIGEMT |
1446
|
281 octave_set_signal_handler (SIGEMT, generic_sig_handler); |
1
|
282 #endif |
|
283 |
|
284 #ifdef SIGFPE |
1373
|
285 #if defined (__alpha__) |
1446
|
286 octave_set_signal_handler (SIGFPE, sigfpe_handler); |
1373
|
287 #else |
1446
|
288 octave_set_signal_handler (SIGFPE, generic_sig_handler); |
1
|
289 #endif |
1373
|
290 #endif |
1
|
291 |
|
292 #ifdef SIGHUP |
1446
|
293 octave_set_signal_handler (SIGHUP, generic_sig_handler); |
1
|
294 #endif |
|
295 |
|
296 #ifdef SIGILL |
1446
|
297 octave_set_signal_handler (SIGILL, generic_sig_handler); |
1
|
298 #endif |
|
299 |
|
300 #ifdef SIGINT |
1446
|
301 octave_set_signal_handler (SIGINT, sigint_handler); |
1
|
302 #endif |
|
303 |
|
304 #ifdef SIGIOT |
1446
|
305 octave_set_signal_handler (SIGIOT, generic_sig_handler); |
1
|
306 #endif |
|
307 |
|
308 #ifdef SIGLOST |
1446
|
309 octave_set_signal_handler (SIGLOST, generic_sig_handler); |
1
|
310 #endif |
|
311 |
|
312 #ifdef SIGPIPE |
1446
|
313 octave_set_signal_handler (SIGPIPE, sigpipe_handler); |
1
|
314 #endif |
|
315 |
|
316 #ifdef SIGPOLL |
1446
|
317 octave_set_signal_handler (SIGPOLL, SIG_IGN); |
1
|
318 #endif |
|
319 |
|
320 #ifdef SIGPROF |
1446
|
321 octave_set_signal_handler (SIGPROF, generic_sig_handler); |
1
|
322 #endif |
|
323 |
|
324 #ifdef SIGQUIT |
1446
|
325 octave_set_signal_handler (SIGQUIT, generic_sig_handler); |
1
|
326 #endif |
|
327 |
|
328 #ifdef SIGSEGV |
1446
|
329 octave_set_signal_handler (SIGSEGV, generic_sig_handler); |
1
|
330 #endif |
|
331 |
|
332 #ifdef SIGSYS |
1446
|
333 octave_set_signal_handler (SIGSYS, generic_sig_handler); |
1
|
334 #endif |
|
335 |
|
336 #ifdef SIGTERM |
1446
|
337 octave_set_signal_handler (SIGTERM, generic_sig_handler); |
1
|
338 #endif |
|
339 |
|
340 #ifdef SIGTRAP |
1446
|
341 octave_set_signal_handler (SIGTRAP, generic_sig_handler); |
1
|
342 #endif |
|
343 |
|
344 #ifdef SIGUSR1 |
1446
|
345 octave_set_signal_handler (SIGUSR1, generic_sig_handler); |
1
|
346 #endif |
|
347 |
|
348 #ifdef SIGUSR2 |
1446
|
349 octave_set_signal_handler (SIGUSR2, generic_sig_handler); |
1
|
350 #endif |
|
351 |
|
352 #ifdef SIGVTALRM |
1446
|
353 octave_set_signal_handler (SIGVTALRM, generic_sig_handler); |
1
|
354 #endif |
|
355 |
895
|
356 #ifdef SIGIO |
1446
|
357 octave_set_signal_handler (SIGIO, SIG_IGN); |
895
|
358 #endif |
|
359 |
1
|
360 #ifdef SIGXCPU |
1446
|
361 octave_set_signal_handler (SIGXCPU, generic_sig_handler); |
1
|
362 #endif |
|
363 |
|
364 #ifdef SIGXFSZ |
1446
|
365 octave_set_signal_handler (SIGXFSZ, generic_sig_handler); |
1
|
366 #endif |
|
367 } |
|
368 |
886
|
369 #ifndef HAVE_SYS_SIGLIST |
839
|
370 char *sys_siglist[NSIG + 1] = |
|
371 { |
|
372 #ifdef AIX |
|
373 /* AIX has changed the signals a bit */ |
|
374 "bogus signal", /* 0 */ |
|
375 "hangup", /* 1 SIGHUP */ |
|
376 "interrupt", /* 2 SIGINT */ |
|
377 "quit", /* 3 SIGQUIT */ |
|
378 "illegal instruction", /* 4 SIGILL */ |
|
379 "trace trap", /* 5 SIGTRAP */ |
|
380 "IOT instruction", /* 6 SIGIOT */ |
|
381 "crash likely", /* 7 SIGDANGER */ |
|
382 "floating point exception", /* 8 SIGFPE */ |
|
383 "kill", /* 9 SIGKILL */ |
|
384 "bus error", /* 10 SIGBUS */ |
|
385 "segmentation violation", /* 11 SIGSEGV */ |
|
386 "bad argument to system call", /* 12 SIGSYS */ |
|
387 "write on a pipe with no one to read it", /* 13 SIGPIPE */ |
|
388 "alarm clock", /* 14 SIGALRM */ |
|
389 "software termination signum", /* 15 SIGTERM */ |
|
390 "user defined signal 1", /* 16 SIGUSR1 */ |
|
391 "user defined signal 2", /* 17 SIGUSR2 */ |
|
392 "death of a child", /* 18 SIGCLD */ |
|
393 "power-fail restart", /* 19 SIGPWR */ |
|
394 "bogus signal", /* 20 */ |
|
395 "bogus signal", /* 21 */ |
|
396 "bogus signal", /* 22 */ |
|
397 "bogus signal", /* 23 */ |
|
398 "bogus signal", /* 24 */ |
|
399 "LAN I/O interrupt", /* 25 SIGAIO */ |
|
400 "PTY I/O interrupt", /* 26 SIGPTY */ |
|
401 "I/O intervention required", /* 27 SIGIOINT */ |
|
402 "HFT grant", /* 28 SIGGRANT */ |
|
403 "HFT retract", /* 29 SIGRETRACT */ |
|
404 "HFT sound done", /* 30 SIGSOUND */ |
|
405 "HFT input ready", /* 31 SIGMSG */ |
|
406 #else /* not AIX */ |
2512
|
407 #ifdef __EMX__ |
|
408 "bogus signal #0", /* 0 */ |
|
409 "hangup", /* 1 SIGHUP */ |
|
410 "interrupt (Ctrl-C)", /* 2 SIGINT (Ctrl-C) */ |
|
411 "quit", /* 3 SIGQUIT */ |
|
412 "illegal instruction", /* 4 SIGILL */ |
|
413 "single step", /* 5 SIGTRAP */ |
|
414 "abort", /* 6 SIGABRT */ |
|
415 "EMT instruction", /* 7 SIGEMT */ |
|
416 "floating point exception", /* 8 SIGFPE */ |
|
417 "kill", /* 9 SIGKILL */ |
|
418 "bus error", /* 10 SIGBUS */ |
|
419 "segmentation violation", /* 11 SIGSEGV */ |
|
420 "bad argument to system call", /* 12 SIGSYS */ |
|
421 "broken pipe", /* 13 SIGPIPE */ |
|
422 "alarm clock", /* 14 SIGALRM */ |
|
423 "software termination signum", /* 15 SIGTERM */ |
|
424 "user defined signal 1", /* 16 SIGUSR1 */ |
|
425 "user defined signal 2", /* 17 SIGUSR2 */ |
|
426 "death of a child", /* 18 SIGCHLD */ |
|
427 "bogus signal #20", /* 19 */ |
|
428 "bogus signal #21", /* 20 */ |
|
429 "break (Ctrl-Break)", /* 21 SIGBREAK */ |
|
430 #else /* not __EMX__ */ |
839
|
431 "bogus signal", /* 0 */ |
|
432 "hangup", /* 1 SIGHUP */ |
|
433 "interrupt", /* 2 SIGINT */ |
|
434 "quit", /* 3 SIGQUIT */ |
|
435 "illegal instruction", /* 4 SIGILL */ |
|
436 "trace trap", /* 5 SIGTRAP */ |
|
437 "IOT instruction", /* 6 SIGIOT */ |
|
438 "EMT instruction", /* 7 SIGEMT */ |
|
439 "floating point exception", /* 8 SIGFPE */ |
|
440 "kill", /* 9 SIGKILL */ |
|
441 "bus error", /* 10 SIGBUS */ |
|
442 "segmentation violation", /* 11 SIGSEGV */ |
|
443 "bad argument to system call", /* 12 SIGSYS */ |
|
444 "write on a pipe with no one to read it", /* 13 SIGPIPE */ |
|
445 "alarm clock", /* 14 SIGALRM */ |
|
446 "software termination signum", /* 15 SIGTERM */ |
|
447 "user defined signal 1", /* 16 SIGUSR1 */ |
|
448 "user defined signal 2", /* 17 SIGUSR2 */ |
|
449 "death of a child", /* 18 SIGCLD */ |
|
450 "power-fail restart", /* 19 SIGPWR */ |
|
451 #ifdef sun |
|
452 "window size change", /* 20 SIGWINCH */ |
|
453 "urgent socket condition", /* 21 SIGURG */ |
|
454 "pollable event occured", /* 22 SIGPOLL */ |
|
455 "stop (cannot be caught or ignored)", /* 23 SIGSTOP */ |
|
456 "user stop requested from tty", /* 24 SIGTSTP */ |
|
457 "stopped process has been continued", /* 25 SIGCONT */ |
|
458 "background tty read attempted", /* 26 SIGTTIN */ |
|
459 "background tty write attempted", /* 27 SIGTTOU */ |
|
460 "virtual timer expired", /* 28 SIGVTALRM */ |
|
461 "profiling timer expired", /* 29 SIGPROF */ |
|
462 "exceeded cpu limit", /* 30 SIGXCPU */ |
|
463 "exceeded file size limit", /* 31 SIGXFSZ */ |
|
464 "process's lwps are blocked", /* 32 SIGWAITING */ |
|
465 "special signal used by thread library", /* 33 SIGLWP */ |
|
466 #ifdef SIGFREEZE |
|
467 "Special Signal Used By CPR", /* 34 SIGFREEZE */ |
|
468 #endif |
|
469 #ifdef SIGTHAW |
|
470 "Special Signal Used By CPR", /* 35 SIGTHAW */ |
|
471 #endif |
|
472 #endif /* sun */ |
2512
|
473 #endif /* __EMX__ */ |
|
474 #endif /* AIX */ |
839
|
475 0 |
|
476 }; |
|
477 #endif |
|
478 |
2209
|
479 octave_child_list *octave_child_list::instance = 0; |
|
480 |
|
481 void |
|
482 octave_child_list::do_insert (pid_t pid, octave_child::dead_child_handler f) |
|
483 { |
|
484 // Insert item in first open slot, increasing size of list if |
|
485 // necessary. |
|
486 |
|
487 bool enlarge = true; |
|
488 |
|
489 for (int i = 0; i < curr_len; i++) |
|
490 { |
2305
|
491 octave_child& tmp = list (i); |
2209
|
492 |
|
493 if (tmp.pid < 0) |
|
494 { |
2305
|
495 list (i) = octave_child (pid, f); |
2209
|
496 enlarge = false; |
|
497 break; |
|
498 } |
|
499 } |
|
500 |
|
501 if (enlarge) |
|
502 { |
|
503 int total_len = list.length (); |
|
504 |
|
505 if (curr_len == total_len) |
|
506 { |
|
507 if (total_len == 0) |
|
508 list.resize (16); |
|
509 else |
|
510 list.resize (total_len * 2); |
|
511 } |
|
512 |
2305
|
513 list (curr_len) = octave_child (pid, f); |
2209
|
514 curr_len++; |
|
515 } |
|
516 } |
|
517 |
|
518 void |
|
519 octave_child_list::insert (pid_t pid, octave_child::dead_child_handler f) |
|
520 { |
|
521 if (! instance) |
|
522 instance = new octave_child_list (); |
|
523 |
|
524 if (instance) |
|
525 instance->do_insert (pid, f); |
|
526 else |
|
527 panic_impossible (); |
|
528 } |
|
529 |
2210
|
530 void |
|
531 octave_child_list::do_remove (pid_t pid) |
|
532 { |
|
533 // Mark the record for PID invalid. |
|
534 |
|
535 for (int i = 0; i < curr_len; i++) |
|
536 { |
2305
|
537 octave_child& tmp = list (i); |
2210
|
538 |
|
539 if (tmp.pid == pid) |
|
540 { |
|
541 tmp.pid = -1; |
|
542 break; |
|
543 } |
|
544 } |
|
545 } |
|
546 |
|
547 void |
|
548 octave_child_list::remove (pid_t pid) |
|
549 { |
|
550 if (! instance) |
|
551 instance = new octave_child_list (); |
|
552 |
|
553 if (instance) |
|
554 instance->do_remove (pid); |
|
555 else |
|
556 panic_impossible (); |
|
557 } |
|
558 |
1
|
559 /* |
|
560 ;;; Local Variables: *** |
|
561 ;;; mode: C++ *** |
|
562 ;;; End: *** |
|
563 */ |