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 |
834
|
31 #include <sys/types.h> |
|
32 #ifdef HAVE_UNISTD_H |
|
33 #include <unistd.h> |
|
34 #endif |
1230
|
35 #include <sys/wait.h> |
1343
|
36 |
1230
|
37 #include <iostream.h> |
1
|
38 |
|
39 #include "sighandlers.h" |
|
40 #include "octave.h" |
|
41 #include "error.h" |
|
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 { |
834
|
128 // Can this ever cause trouble on systems that don't forget signal |
|
129 // handlers when they are invoked? |
|
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 { |
834
|
149 // Can this ever cause trouble on systems that don't forget signal |
|
150 // handlers when they are invoked? |
|
151 |
|
152 signal (SIGPIPE, sigpipe_handler); |
|
153 |
1
|
154 if (pipe_handler_error_count++ == 0) |
529
|
155 message (0, "broken pipe"); |
1
|
156 |
|
157 // Don\'t loop forever on account of this. |
|
158 if (pipe_handler_error_count > 100) |
|
159 jump_to_top_level (); |
|
160 |
|
161 #if RETSIGTYPE == void |
|
162 return; |
|
163 #else |
|
164 return 0; |
|
165 #endif |
|
166 } |
|
167 |
635
|
168 // Install all the handlers for the signals we might care about. |
|
169 |
1
|
170 void |
|
171 install_signal_handlers (void) |
|
172 { |
635
|
173 set_new_handler (octave_new_handler); |
|
174 |
1
|
175 #ifdef SIGABRT |
834
|
176 signal (SIGABRT, generic_sig_handler); |
1
|
177 #endif |
|
178 |
|
179 #ifdef SIGALRM |
834
|
180 signal (SIGALRM, generic_sig_handler); |
1
|
181 #endif |
|
182 |
|
183 #ifdef SIGBUS |
834
|
184 signal (SIGBUS, generic_sig_handler); |
1
|
185 #endif |
|
186 |
1230
|
187 #if 0 |
|
188 #ifdef SIGCHLD |
|
189 signal (SIGCHLD, sigchld_handler); |
|
190 #endif |
|
191 #endif |
|
192 |
1
|
193 #ifdef SIGEMT |
834
|
194 signal (SIGEMT, generic_sig_handler); |
1
|
195 #endif |
|
196 |
|
197 #ifdef SIGFPE |
834
|
198 signal (SIGFPE, generic_sig_handler); |
1
|
199 #endif |
|
200 |
|
201 #ifdef SIGHUP |
834
|
202 signal (SIGHUP, generic_sig_handler); |
1
|
203 #endif |
|
204 |
|
205 #ifdef SIGILL |
834
|
206 signal (SIGILL, generic_sig_handler); |
1
|
207 #endif |
|
208 |
|
209 #ifdef SIGINT |
|
210 signal (SIGINT, sigint_handler); |
|
211 #endif |
|
212 |
|
213 #ifdef SIGIOT |
834
|
214 signal (SIGIOT, generic_sig_handler); |
1
|
215 #endif |
|
216 |
|
217 #ifdef SIGLOST |
834
|
218 signal (SIGLOST, generic_sig_handler); |
1
|
219 #endif |
|
220 |
|
221 #ifdef SIGPIPE |
|
222 signal (SIGPIPE, sigpipe_handler); |
|
223 #endif |
|
224 |
|
225 #ifdef SIGPOLL |
895
|
226 signal (SIGPOLL, SIG_IGN); |
1
|
227 #endif |
|
228 |
|
229 #ifdef SIGPROF |
834
|
230 signal (SIGPROF, generic_sig_handler); |
1
|
231 #endif |
|
232 |
|
233 #ifdef SIGQUIT |
834
|
234 signal (SIGQUIT, generic_sig_handler); |
1
|
235 #endif |
|
236 |
|
237 #ifdef SIGSEGV |
834
|
238 signal (SIGSEGV, generic_sig_handler); |
1
|
239 #endif |
|
240 |
|
241 #ifdef SIGSYS |
834
|
242 signal (SIGSYS, generic_sig_handler); |
1
|
243 #endif |
|
244 |
|
245 #ifdef SIGTERM |
834
|
246 signal (SIGTERM, generic_sig_handler); |
1
|
247 #endif |
|
248 |
|
249 #ifdef SIGTRAP |
834
|
250 signal (SIGTRAP, generic_sig_handler); |
1
|
251 #endif |
|
252 |
|
253 #ifdef SIGUSR1 |
834
|
254 signal (SIGUSR1, generic_sig_handler); |
1
|
255 #endif |
|
256 |
|
257 #ifdef SIGUSR2 |
834
|
258 signal (SIGUSR2, generic_sig_handler); |
1
|
259 #endif |
|
260 |
|
261 #ifdef SIGVTALRM |
834
|
262 signal (SIGVTALRM, generic_sig_handler); |
1
|
263 #endif |
|
264 |
895
|
265 #ifdef SIGIO |
|
266 signal (SIGIO, SIG_IGN); |
|
267 #endif |
|
268 |
1
|
269 #ifdef SIGXCPU |
834
|
270 signal (SIGXCPU, generic_sig_handler); |
1
|
271 #endif |
|
272 |
|
273 #ifdef SIGXFSZ |
834
|
274 signal (SIGXFSZ, generic_sig_handler); |
1
|
275 #endif |
|
276 } |
|
277 |
886
|
278 #ifndef HAVE_SYS_SIGLIST |
839
|
279 char *sys_siglist[NSIG + 1] = |
|
280 { |
|
281 #ifdef AIX |
|
282 /* AIX has changed the signals a bit */ |
|
283 "bogus signal", /* 0 */ |
|
284 "hangup", /* 1 SIGHUP */ |
|
285 "interrupt", /* 2 SIGINT */ |
|
286 "quit", /* 3 SIGQUIT */ |
|
287 "illegal instruction", /* 4 SIGILL */ |
|
288 "trace trap", /* 5 SIGTRAP */ |
|
289 "IOT instruction", /* 6 SIGIOT */ |
|
290 "crash likely", /* 7 SIGDANGER */ |
|
291 "floating point exception", /* 8 SIGFPE */ |
|
292 "kill", /* 9 SIGKILL */ |
|
293 "bus error", /* 10 SIGBUS */ |
|
294 "segmentation violation", /* 11 SIGSEGV */ |
|
295 "bad argument to system call", /* 12 SIGSYS */ |
|
296 "write on a pipe with no one to read it", /* 13 SIGPIPE */ |
|
297 "alarm clock", /* 14 SIGALRM */ |
|
298 "software termination signum", /* 15 SIGTERM */ |
|
299 "user defined signal 1", /* 16 SIGUSR1 */ |
|
300 "user defined signal 2", /* 17 SIGUSR2 */ |
|
301 "death of a child", /* 18 SIGCLD */ |
|
302 "power-fail restart", /* 19 SIGPWR */ |
|
303 "bogus signal", /* 20 */ |
|
304 "bogus signal", /* 21 */ |
|
305 "bogus signal", /* 22 */ |
|
306 "bogus signal", /* 23 */ |
|
307 "bogus signal", /* 24 */ |
|
308 "LAN I/O interrupt", /* 25 SIGAIO */ |
|
309 "PTY I/O interrupt", /* 26 SIGPTY */ |
|
310 "I/O intervention required", /* 27 SIGIOINT */ |
|
311 "HFT grant", /* 28 SIGGRANT */ |
|
312 "HFT retract", /* 29 SIGRETRACT */ |
|
313 "HFT sound done", /* 30 SIGSOUND */ |
|
314 "HFT input ready", /* 31 SIGMSG */ |
|
315 #else /* not AIX */ |
|
316 "bogus signal", /* 0 */ |
|
317 "hangup", /* 1 SIGHUP */ |
|
318 "interrupt", /* 2 SIGINT */ |
|
319 "quit", /* 3 SIGQUIT */ |
|
320 "illegal instruction", /* 4 SIGILL */ |
|
321 "trace trap", /* 5 SIGTRAP */ |
|
322 "IOT instruction", /* 6 SIGIOT */ |
|
323 "EMT instruction", /* 7 SIGEMT */ |
|
324 "floating point exception", /* 8 SIGFPE */ |
|
325 "kill", /* 9 SIGKILL */ |
|
326 "bus error", /* 10 SIGBUS */ |
|
327 "segmentation violation", /* 11 SIGSEGV */ |
|
328 "bad argument to system call", /* 12 SIGSYS */ |
|
329 "write on a pipe with no one to read it", /* 13 SIGPIPE */ |
|
330 "alarm clock", /* 14 SIGALRM */ |
|
331 "software termination signum", /* 15 SIGTERM */ |
|
332 "user defined signal 1", /* 16 SIGUSR1 */ |
|
333 "user defined signal 2", /* 17 SIGUSR2 */ |
|
334 "death of a child", /* 18 SIGCLD */ |
|
335 "power-fail restart", /* 19 SIGPWR */ |
|
336 #ifdef sun |
|
337 "window size change", /* 20 SIGWINCH */ |
|
338 "urgent socket condition", /* 21 SIGURG */ |
|
339 "pollable event occured", /* 22 SIGPOLL */ |
|
340 "stop (cannot be caught or ignored)", /* 23 SIGSTOP */ |
|
341 "user stop requested from tty", /* 24 SIGTSTP */ |
|
342 "stopped process has been continued", /* 25 SIGCONT */ |
|
343 "background tty read attempted", /* 26 SIGTTIN */ |
|
344 "background tty write attempted", /* 27 SIGTTOU */ |
|
345 "virtual timer expired", /* 28 SIGVTALRM */ |
|
346 "profiling timer expired", /* 29 SIGPROF */ |
|
347 "exceeded cpu limit", /* 30 SIGXCPU */ |
|
348 "exceeded file size limit", /* 31 SIGXFSZ */ |
|
349 "process's lwps are blocked", /* 32 SIGWAITING */ |
|
350 "special signal used by thread library", /* 33 SIGLWP */ |
|
351 #ifdef SIGFREEZE |
|
352 "Special Signal Used By CPR", /* 34 SIGFREEZE */ |
|
353 #endif |
|
354 #ifdef SIGTHAW |
|
355 "Special Signal Used By CPR", /* 35 SIGTHAW */ |
|
356 #endif |
|
357 #endif /* sun */ |
|
358 #endif /* not AIX */ |
|
359 0 |
|
360 }; |
|
361 #endif |
|
362 |
1
|
363 /* |
|
364 ;;; Local Variables: *** |
|
365 ;;; mode: C++ *** |
|
366 ;;; page-delimiter: "^/\\*" *** |
|
367 ;;; End: *** |
|
368 */ |