1
|
1 // sighandlers.cc -*- C++ -*- |
|
2 /* |
|
3 |
1009
|
4 Copyright (C) 1992, 1993, 1994, 1995 John W. Eaton |
1
|
5 |
|
6 This file is part of Octave. |
|
7 |
|
8 Octave is free software; you can redistribute it and/or modify it |
|
9 under the terms of the GNU General Public License as published by the |
|
10 Free Software Foundation; either version 2, or (at your option) any |
|
11 later version. |
|
12 |
|
13 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
16 for more details. |
|
17 |
|
18 You should have received a copy of the GNU General Public License |
|
19 along with Octave; see the file COPYING. If not, write to the Free |
1315
|
20 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
1
|
21 |
|
22 */ |
|
23 |
240
|
24 #ifdef HAVE_CONFIG_H |
1230
|
25 #include <config.h> |
1
|
26 #endif |
|
27 |
1343
|
28 #include <csignal> |
1344
|
29 #include <new> |
|
30 |
1350
|
31 #include <iostream.h> |
|
32 |
|
33 #ifdef HAVE_UNISTD_H |
834
|
34 #include <sys/types.h> |
|
35 #include <unistd.h> |
|
36 #endif |
1343
|
37 |
1352
|
38 #include "error.h" |
1373
|
39 #include "load-save.h" |
1352
|
40 #include "octave.h" |
1
|
41 #include "sighandlers.h" |
1352
|
42 #include "syswait.h" |
1
|
43 #include "utils.h" |
|
44 |
|
45 // Nonzero means we have already printed a message for this series of |
|
46 // SIGPIPES. We assume that the writer will eventually give up. |
|
47 int pipe_handler_error_count = 0; |
|
48 |
|
49 // Nonzero means we can be interrupted. |
|
50 int can_interrupt = 0; |
|
51 |
|
52 static void |
|
53 my_friendly_exit (const char *sig_name, int sig_number) |
|
54 { |
886
|
55 error ("%s -- stopping myself...", sig_name); |
1373
|
56 |
|
57 save_user_variables (); |
|
58 |
1
|
59 clean_up_and_exit (sig_number); |
|
60 } |
|
61 |
635
|
62 // I know, not really a signal handler. |
|
63 |
|
64 static void |
|
65 octave_new_handler (void) |
|
66 { |
1394
|
67 error ("memory exhausted -- trying to return to prompt"); |
|
68 |
|
69 if (can_interrupt) |
|
70 { |
|
71 jump_to_top_level (); |
|
72 panic_impossible (); |
|
73 } |
|
74 else |
1395
|
75 my_friendly_exit ("operator new", 1); |
635
|
76 } |
|
77 |
1
|
78 static RETSIGTYPE |
834
|
79 generic_sig_handler (int i) |
1
|
80 { |
834
|
81 my_friendly_exit (sys_siglist[i], i); |
1
|
82 |
834
|
83 #if RETSIGTYPE == void |
|
84 return; |
|
85 #else |
|
86 return 0; |
|
87 #endif |
1
|
88 } |
|
89 |
1230
|
90 // Handle SIGCHLD. Should use waitpid and ignore stopped jobs. |
|
91 // Needs to restore state of plotter such that it will be restarted |
|
92 // again when needed. Needs to close file descriptors corresponding |
|
93 // to processes started with execute(). |
|
94 |
|
95 #if 0 |
|
96 static RETSIGTYPE |
|
97 sigchld_handler (int i) |
|
98 { |
|
99 int status; |
|
100 pid_t pid = wait (&status); |
|
101 |
|
102 if (pid < 0) |
|
103 cerr << "wait error\n"; |
|
104 else |
|
105 { |
|
106 cerr << "sigchld caught, PID = " << pid << "; status: "; |
|
107 |
|
108 int lo_byte = (status & 0xff); |
|
109 int hi_byte = ((status >> 8) & 0xff); |
|
110 if (lo_byte == 0177) |
|
111 { |
|
112 cerr << "stopped with signal = " << hi_byte << "\n"; |
|
113 } |
|
114 else if (lo_byte) |
|
115 { |
|
116 int sig_num = (lo_byte & 0x7f); |
|
117 cerr << "stopped with signal = " << sig_num << "\n"; |
|
118 if (lo_byte & 0200) |
|
119 cerr << "child dumped core\n"; |
|
120 } |
|
121 else |
|
122 { |
|
123 cerr << "exited with status = " << hi_byte << "\n"; |
|
124 } |
|
125 } |
|
126 |
|
127 signal (SIGCHLD, sigchld_handler); |
|
128 } |
|
129 #endif |
|
130 |
1373
|
131 #if defined (__alpha__) |
|
132 static RETSIGTYPE |
|
133 sigfpe_handler (int i) |
|
134 { |
|
135 // Can this ever cause trouble on systems that don't forget signal |
|
136 // handlers when they are invoked? |
|
137 |
|
138 signal (SIGFPE, sigfpe_handler); |
|
139 |
1394
|
140 error ("floating point exception -- trying to return to prompt"); |
1373
|
141 |
|
142 if (can_interrupt) |
|
143 { |
|
144 jump_to_top_level (); |
|
145 panic_impossible (); |
|
146 } |
|
147 |
|
148 #if RETSIGTYPE == void |
|
149 return; |
|
150 #else |
|
151 return 0; |
|
152 #endif |
|
153 } |
|
154 #endif |
|
155 |
635
|
156 // Handle SIGINT by restarting the parser (see octave.cc). |
|
157 |
834
|
158 // XXX FIXME XXX -- it would probably be good to try to use POSIX |
|
159 // signal interface if it is available. |
|
160 |
1
|
161 static RETSIGTYPE |
|
162 sigint_handler (int i) |
|
163 { |
1358
|
164 // Can this ever cause trouble on systems that don't forget signal |
|
165 // handlers when they are invoked? |
834
|
166 |
|
167 signal (SIGINT, sigint_handler); |
|
168 |
1
|
169 if (can_interrupt) |
|
170 { |
|
171 jump_to_top_level (); |
|
172 panic_impossible (); |
|
173 } |
|
174 |
|
175 #if RETSIGTYPE == void |
|
176 return; |
|
177 #else |
|
178 return 0; |
|
179 #endif |
|
180 } |
|
181 |
|
182 static RETSIGTYPE |
|
183 sigpipe_handler (int i) |
|
184 { |
1358
|
185 // Can this ever cause trouble on systems that don't forget signal |
|
186 // handlers when they are invoked? |
834
|
187 |
|
188 signal (SIGPIPE, sigpipe_handler); |
|
189 |
1
|
190 if (pipe_handler_error_count++ == 0) |
529
|
191 message (0, "broken pipe"); |
1
|
192 |
1358
|
193 // Don't loop forever on account of this. |
|
194 |
1
|
195 if (pipe_handler_error_count > 100) |
|
196 jump_to_top_level (); |
|
197 |
|
198 #if RETSIGTYPE == void |
|
199 return; |
|
200 #else |
|
201 return 0; |
|
202 #endif |
|
203 } |
|
204 |
635
|
205 // Install all the handlers for the signals we might care about. |
|
206 |
1
|
207 void |
|
208 install_signal_handlers (void) |
|
209 { |
635
|
210 set_new_handler (octave_new_handler); |
|
211 |
1
|
212 #ifdef SIGABRT |
834
|
213 signal (SIGABRT, generic_sig_handler); |
1
|
214 #endif |
|
215 |
|
216 #ifdef SIGALRM |
834
|
217 signal (SIGALRM, generic_sig_handler); |
1
|
218 #endif |
|
219 |
|
220 #ifdef SIGBUS |
834
|
221 signal (SIGBUS, generic_sig_handler); |
1
|
222 #endif |
|
223 |
1230
|
224 #if 0 |
|
225 #ifdef SIGCHLD |
|
226 signal (SIGCHLD, sigchld_handler); |
|
227 #endif |
|
228 #endif |
|
229 |
1
|
230 #ifdef SIGEMT |
834
|
231 signal (SIGEMT, generic_sig_handler); |
1
|
232 #endif |
|
233 |
|
234 #ifdef SIGFPE |
1373
|
235 #if defined (__alpha__) |
|
236 signal (SIGFPE, sigfpe_handler); |
|
237 #else |
834
|
238 signal (SIGFPE, generic_sig_handler); |
1
|
239 #endif |
1373
|
240 #endif |
1
|
241 |
|
242 #ifdef SIGHUP |
834
|
243 signal (SIGHUP, generic_sig_handler); |
1
|
244 #endif |
|
245 |
|
246 #ifdef SIGILL |
834
|
247 signal (SIGILL, generic_sig_handler); |
1
|
248 #endif |
|
249 |
|
250 #ifdef SIGINT |
|
251 signal (SIGINT, sigint_handler); |
|
252 #endif |
|
253 |
|
254 #ifdef SIGIOT |
834
|
255 signal (SIGIOT, generic_sig_handler); |
1
|
256 #endif |
|
257 |
|
258 #ifdef SIGLOST |
834
|
259 signal (SIGLOST, generic_sig_handler); |
1
|
260 #endif |
|
261 |
|
262 #ifdef SIGPIPE |
|
263 signal (SIGPIPE, sigpipe_handler); |
|
264 #endif |
|
265 |
|
266 #ifdef SIGPOLL |
895
|
267 signal (SIGPOLL, SIG_IGN); |
1
|
268 #endif |
|
269 |
|
270 #ifdef SIGPROF |
834
|
271 signal (SIGPROF, generic_sig_handler); |
1
|
272 #endif |
|
273 |
|
274 #ifdef SIGQUIT |
834
|
275 signal (SIGQUIT, generic_sig_handler); |
1
|
276 #endif |
|
277 |
|
278 #ifdef SIGSEGV |
834
|
279 signal (SIGSEGV, generic_sig_handler); |
1
|
280 #endif |
|
281 |
|
282 #ifdef SIGSYS |
834
|
283 signal (SIGSYS, generic_sig_handler); |
1
|
284 #endif |
|
285 |
|
286 #ifdef SIGTERM |
834
|
287 signal (SIGTERM, generic_sig_handler); |
1
|
288 #endif |
|
289 |
|
290 #ifdef SIGTRAP |
834
|
291 signal (SIGTRAP, generic_sig_handler); |
1
|
292 #endif |
|
293 |
|
294 #ifdef SIGUSR1 |
834
|
295 signal (SIGUSR1, generic_sig_handler); |
1
|
296 #endif |
|
297 |
|
298 #ifdef SIGUSR2 |
834
|
299 signal (SIGUSR2, generic_sig_handler); |
1
|
300 #endif |
|
301 |
|
302 #ifdef SIGVTALRM |
834
|
303 signal (SIGVTALRM, generic_sig_handler); |
1
|
304 #endif |
|
305 |
895
|
306 #ifdef SIGIO |
|
307 signal (SIGIO, SIG_IGN); |
|
308 #endif |
|
309 |
1
|
310 #ifdef SIGXCPU |
834
|
311 signal (SIGXCPU, generic_sig_handler); |
1
|
312 #endif |
|
313 |
|
314 #ifdef SIGXFSZ |
834
|
315 signal (SIGXFSZ, generic_sig_handler); |
1
|
316 #endif |
|
317 } |
|
318 |
886
|
319 #ifndef HAVE_SYS_SIGLIST |
839
|
320 char *sys_siglist[NSIG + 1] = |
|
321 { |
|
322 #ifdef AIX |
|
323 /* AIX has changed the signals a bit */ |
|
324 "bogus signal", /* 0 */ |
|
325 "hangup", /* 1 SIGHUP */ |
|
326 "interrupt", /* 2 SIGINT */ |
|
327 "quit", /* 3 SIGQUIT */ |
|
328 "illegal instruction", /* 4 SIGILL */ |
|
329 "trace trap", /* 5 SIGTRAP */ |
|
330 "IOT instruction", /* 6 SIGIOT */ |
|
331 "crash likely", /* 7 SIGDANGER */ |
|
332 "floating point exception", /* 8 SIGFPE */ |
|
333 "kill", /* 9 SIGKILL */ |
|
334 "bus error", /* 10 SIGBUS */ |
|
335 "segmentation violation", /* 11 SIGSEGV */ |
|
336 "bad argument to system call", /* 12 SIGSYS */ |
|
337 "write on a pipe with no one to read it", /* 13 SIGPIPE */ |
|
338 "alarm clock", /* 14 SIGALRM */ |
|
339 "software termination signum", /* 15 SIGTERM */ |
|
340 "user defined signal 1", /* 16 SIGUSR1 */ |
|
341 "user defined signal 2", /* 17 SIGUSR2 */ |
|
342 "death of a child", /* 18 SIGCLD */ |
|
343 "power-fail restart", /* 19 SIGPWR */ |
|
344 "bogus signal", /* 20 */ |
|
345 "bogus signal", /* 21 */ |
|
346 "bogus signal", /* 22 */ |
|
347 "bogus signal", /* 23 */ |
|
348 "bogus signal", /* 24 */ |
|
349 "LAN I/O interrupt", /* 25 SIGAIO */ |
|
350 "PTY I/O interrupt", /* 26 SIGPTY */ |
|
351 "I/O intervention required", /* 27 SIGIOINT */ |
|
352 "HFT grant", /* 28 SIGGRANT */ |
|
353 "HFT retract", /* 29 SIGRETRACT */ |
|
354 "HFT sound done", /* 30 SIGSOUND */ |
|
355 "HFT input ready", /* 31 SIGMSG */ |
|
356 #else /* not AIX */ |
|
357 "bogus signal", /* 0 */ |
|
358 "hangup", /* 1 SIGHUP */ |
|
359 "interrupt", /* 2 SIGINT */ |
|
360 "quit", /* 3 SIGQUIT */ |
|
361 "illegal instruction", /* 4 SIGILL */ |
|
362 "trace trap", /* 5 SIGTRAP */ |
|
363 "IOT instruction", /* 6 SIGIOT */ |
|
364 "EMT instruction", /* 7 SIGEMT */ |
|
365 "floating point exception", /* 8 SIGFPE */ |
|
366 "kill", /* 9 SIGKILL */ |
|
367 "bus error", /* 10 SIGBUS */ |
|
368 "segmentation violation", /* 11 SIGSEGV */ |
|
369 "bad argument to system call", /* 12 SIGSYS */ |
|
370 "write on a pipe with no one to read it", /* 13 SIGPIPE */ |
|
371 "alarm clock", /* 14 SIGALRM */ |
|
372 "software termination signum", /* 15 SIGTERM */ |
|
373 "user defined signal 1", /* 16 SIGUSR1 */ |
|
374 "user defined signal 2", /* 17 SIGUSR2 */ |
|
375 "death of a child", /* 18 SIGCLD */ |
|
376 "power-fail restart", /* 19 SIGPWR */ |
|
377 #ifdef sun |
|
378 "window size change", /* 20 SIGWINCH */ |
|
379 "urgent socket condition", /* 21 SIGURG */ |
|
380 "pollable event occured", /* 22 SIGPOLL */ |
|
381 "stop (cannot be caught or ignored)", /* 23 SIGSTOP */ |
|
382 "user stop requested from tty", /* 24 SIGTSTP */ |
|
383 "stopped process has been continued", /* 25 SIGCONT */ |
|
384 "background tty read attempted", /* 26 SIGTTIN */ |
|
385 "background tty write attempted", /* 27 SIGTTOU */ |
|
386 "virtual timer expired", /* 28 SIGVTALRM */ |
|
387 "profiling timer expired", /* 29 SIGPROF */ |
|
388 "exceeded cpu limit", /* 30 SIGXCPU */ |
|
389 "exceeded file size limit", /* 31 SIGXFSZ */ |
|
390 "process's lwps are blocked", /* 32 SIGWAITING */ |
|
391 "special signal used by thread library", /* 33 SIGLWP */ |
|
392 #ifdef SIGFREEZE |
|
393 "Special Signal Used By CPR", /* 34 SIGFREEZE */ |
|
394 #endif |
|
395 #ifdef SIGTHAW |
|
396 "Special Signal Used By CPR", /* 35 SIGTHAW */ |
|
397 #endif |
|
398 #endif /* sun */ |
|
399 #endif /* not AIX */ |
|
400 0 |
|
401 }; |
|
402 #endif |
|
403 |
1
|
404 /* |
|
405 ;;; Local Variables: *** |
|
406 ;;; mode: C++ *** |
|
407 ;;; page-delimiter: "^/\\*" *** |
|
408 ;;; End: *** |
|
409 */ |