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