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