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