1 | /* C K U S I G -- Kermit signal handling for Unix and OS/2 systems */ |
---|
2 | |
---|
3 | /* |
---|
4 | Author: Jeffrey Altman (jaltman@columbia.edu), |
---|
5 | Columbia University Academic Information Systems, New York City. |
---|
6 | |
---|
7 | Copyright (C) 1985, 1996, Trustees of Columbia University in the City of New |
---|
8 | York. The C-Kermit software may not be, in whole or in part, licensed or |
---|
9 | sold for profit as a software product itself, nor may it be included in or |
---|
10 | distributed with commercial products or otherwise distributed by commercial |
---|
11 | concerns to their clients or customers without written permission of the |
---|
12 | Office of Kermit Development and Distribution, Columbia University. This |
---|
13 | copyright notice must not be removed, altered, or obscured. |
---|
14 | */ |
---|
15 | #include "ckcsym.h" |
---|
16 | #include "ckcasc.h" /* ASCII character symbols */ |
---|
17 | #include "ckcdeb.h" /* Debug & other symbols */ |
---|
18 | #include "ckcker.h" /* Kermit symbols */ |
---|
19 | #include "ckcnet.h" /* Network symbols */ |
---|
20 | #ifndef NOSPL |
---|
21 | #include "ckuusr.h" |
---|
22 | #endif /* NOSPL */ |
---|
23 | |
---|
24 | #include <signal.h> |
---|
25 | #ifdef NT |
---|
26 | #include <setjmpex.h> |
---|
27 | #include <excpt.h> |
---|
28 | #else /* NT */ |
---|
29 | #include <setjmp.h> |
---|
30 | #endif /* NT */ |
---|
31 | #include "ckcsig.h" |
---|
32 | |
---|
33 | #ifdef NOCCTRAP |
---|
34 | extern ckjmpbuf cmjbuf; |
---|
35 | #endif /* NOCCTRAP */ |
---|
36 | |
---|
37 | #ifdef MAC |
---|
38 | #define signal msignal |
---|
39 | #define SIGTYP long |
---|
40 | #define alarm malarm |
---|
41 | #define SIG_IGN 0 |
---|
42 | #define SIGALRM 1 |
---|
43 | #define SIGINT 2 |
---|
44 | SIGTYP (*msignal(int type, SIGTYP (*func)(int)))(int); |
---|
45 | #endif /* MAC */ |
---|
46 | |
---|
47 | #ifdef AMIGA |
---|
48 | #define signal asignal |
---|
49 | #define alarm aarlarm |
---|
50 | #define SIGALRM (_NUMSIG+1) |
---|
51 | #define SIGTYP void |
---|
52 | SIGTYP (*asignal(int type, SIGTYP (*func)(int)))(int); |
---|
53 | unsigned aalarm(unsigned); |
---|
54 | #endif /* AMIGA */ |
---|
55 | |
---|
56 | #ifdef NTASM |
---|
57 | DWORD |
---|
58 | ckgetIP(void) |
---|
59 | { |
---|
60 | __asm |
---|
61 | { |
---|
62 | mov eax, dword ptr [esp+0x10] |
---|
63 | jmp ckgetIP + 0x18 |
---|
64 | } |
---|
65 | return 1; |
---|
66 | |
---|
67 | } |
---|
68 | #endif /* NTASM */ |
---|
69 | |
---|
70 | #ifdef NT |
---|
71 | DWORD |
---|
72 | exception_filter( void ) |
---|
73 | { |
---|
74 | GetExceptionInformation ; |
---|
75 | return( EXCEPTION_EXECUTE_HANDLER ) ; |
---|
76 | } |
---|
77 | void |
---|
78 | crash( void ) |
---|
79 | { |
---|
80 | int x = 0, y = 0 ; |
---|
81 | x / y ; |
---|
82 | } |
---|
83 | #endif /* NT */ |
---|
84 | |
---|
85 | #ifndef NOCCTRAP |
---|
86 | int |
---|
87 | #ifdef CK_ANSIC |
---|
88 | cc_execute( ckjptr(sj_buf), ck_sigfunc dofunc, ck_sigfunc failfunc ) |
---|
89 | #else |
---|
90 | cc_execute( sj_buf, dofunc, failfunc) |
---|
91 | ckjptr(sj_buf); |
---|
92 | ck_sigfunc dofunc; |
---|
93 | ck_sigfunc failfunc; |
---|
94 | #endif /* CK_ANSIC */ |
---|
95 | /* cc_execute */ { |
---|
96 | int rc = 0 ; |
---|
97 | #ifdef NTASM |
---|
98 | DWORD Eip, Esp ; |
---|
99 | isinterrupted = 0; |
---|
100 | sj_buf->retcode = 0 ; |
---|
101 | sj_buf->Id = GetCurrentThreadId() ; |
---|
102 | memset( &sj_buf->context, 0, sizeof(CONTEXT) ); |
---|
103 | sj_buf->context.ContextFlags = CONTEXT_FULL ; |
---|
104 | #ifndef COMMENT |
---|
105 | GetThreadContext(GetCurrentThread(), &(sj_buf->context) ) ; |
---|
106 | __asm |
---|
107 | { |
---|
108 | mov ecx,dword ptr [sj_buf] |
---|
109 | mov dword ptr [ecx+0xc4],esp |
---|
110 | } |
---|
111 | sj_buf->context.EFlags = 530 ; |
---|
112 | sj_buf->context.Eip = ckgetIP()+0x0C ; |
---|
113 | #else /* COMMENT */ |
---|
114 | __asm |
---|
115 | { |
---|
116 | mov eax, dword ptr [sj_buf] |
---|
117 | push eax |
---|
118 | mov eax, 0xfffffffe |
---|
119 | push eax |
---|
120 | mov eax, 0x00000039 |
---|
121 | mov edx,esp |
---|
122 | int 0x2e |
---|
123 | pop eax |
---|
124 | pop eax |
---|
125 | } |
---|
126 | #endif /* COMMENT */ |
---|
127 | #endif /* NTASM */ |
---|
128 | if ( |
---|
129 | #ifdef NTASM |
---|
130 | isinterrupted |
---|
131 | #else |
---|
132 | cksetjmp(ckjdref(sj_buf)) |
---|
133 | #endif /* NTASM */ |
---|
134 | ) { |
---|
135 | #ifdef NTASM |
---|
136 | __asm |
---|
137 | { |
---|
138 | mov esp, ESPToRestore |
---|
139 | } |
---|
140 | isinterrupted = 0 ; |
---|
141 | #endif /* NTASM */ |
---|
142 | (*failfunc)(NULL) ; |
---|
143 | #ifdef NTASM |
---|
144 | rc = sj_buf->retcode ; |
---|
145 | #else /* NTASM */ |
---|
146 | rc = -1 ; |
---|
147 | #endif /* NTASM */ |
---|
148 | } else { |
---|
149 | #ifdef NT |
---|
150 | __try { |
---|
151 | (*dofunc)(NULL) ; |
---|
152 | } |
---|
153 | __except( exception_filter() ) |
---|
154 | { |
---|
155 | debug(F100,"cc_execute __except","",0); |
---|
156 | debug(F111,"exception_filter","_exception_code", GetExceptionCode() ) ; |
---|
157 | longjmp(ckjdref(sj_buf),SIGINT) ; |
---|
158 | } |
---|
159 | #else /* NT */ |
---|
160 | (*dofunc)(NULL) ; |
---|
161 | #endif /* NT */ |
---|
162 | } |
---|
163 | return rc ; |
---|
164 | } |
---|
165 | #endif /* NOCCTRAP */ |
---|
166 | |
---|
167 | int |
---|
168 | #ifdef CK_ANSIC /* ANSIC C declaration... */ |
---|
169 | alrm_execute(ckjptr(sj_buf), |
---|
170 | int timo, |
---|
171 | ck_sighand handler, |
---|
172 | ck_sigfunc dofunc, |
---|
173 | ck_sigfunc failfunc |
---|
174 | ) |
---|
175 | |
---|
176 | #else /* Not ANSIC C ... */ |
---|
177 | |
---|
178 | alrm_execute(sj_buf, |
---|
179 | timo, |
---|
180 | handler, |
---|
181 | dofunc, |
---|
182 | failfunc |
---|
183 | ) |
---|
184 | ckjptr(sj_buf); |
---|
185 | int timo; |
---|
186 | ck_sighand handler; |
---|
187 | ck_sigfunc dofunc; |
---|
188 | ck_sigfunc failfunc; |
---|
189 | #endif /* CK_ANSIC */ |
---|
190 | |
---|
191 | /* alrm_execute */ { |
---|
192 | |
---|
193 | int rc = 0; |
---|
194 | int savalrm = 0; |
---|
195 | _PROTOTYP(SIGTYP (*savhandler), (int)); |
---|
196 | |
---|
197 | savalrm = alarm(timo); |
---|
198 | savhandler = signal(SIGALRM, handler); |
---|
199 | |
---|
200 | #ifdef NTASM |
---|
201 | sj_buf->retcode = 0 ; |
---|
202 | sj_buf->Id = GetCurrentThreadId(); |
---|
203 | memset(&sj_buf->context, 0, sizeof(CONTEXT)); |
---|
204 | sj_buf->context.ContextFlags = CONTEXT_FULL; |
---|
205 | #ifndef COMMENT |
---|
206 | GetThreadContext(GetCurrentThread(), &(sj_buf->context)); |
---|
207 | #else |
---|
208 | __asm |
---|
209 | { |
---|
210 | mov eax, dword ptr [sj_buf] |
---|
211 | push eax |
---|
212 | mov eax, 0xfffffffe |
---|
213 | push eax |
---|
214 | mov eax, 0x00000039 |
---|
215 | mov edx,esp |
---|
216 | int 0x2e |
---|
217 | pop eax |
---|
218 | pop eax |
---|
219 | } |
---|
220 | #endif |
---|
221 | isinterrupted = 0; |
---|
222 | #endif /* NTASM */ |
---|
223 | if ( |
---|
224 | #ifdef NTASM |
---|
225 | sj_buf->retcode |
---|
226 | #else |
---|
227 | cksetjmp(ckjdref(sj_buf)) |
---|
228 | #endif /* NTASM */ |
---|
229 | ) { |
---|
230 | (*failfunc)(NULL) ; |
---|
231 | rc = -1 ; |
---|
232 | } else { |
---|
233 | #ifdef NT |
---|
234 | __try { |
---|
235 | (*dofunc)(NULL) ; |
---|
236 | } |
---|
237 | __except( exception_filter() ) |
---|
238 | { |
---|
239 | debug(F100,"alrm_execute __except","",0); |
---|
240 | debug(F111,"exception_filter","_exception_code", GetExceptionCode() ) ; |
---|
241 | longjmp(ckjdref(sj_buf),SIGINT) ; |
---|
242 | } |
---|
243 | #else /* NT */ |
---|
244 | (*dofunc)(NULL) ; |
---|
245 | #endif /* NT */ |
---|
246 | } |
---|
247 | alarm(savalrm) ; |
---|
248 | if ( savhandler ) |
---|
249 | signal( SIGALRM, savhandler ) ; |
---|
250 | return rc ; |
---|
251 | } |
---|
252 | |
---|
253 | int |
---|
254 | #ifdef CK_ANSIC /* ANSIC C declaration... */ |
---|
255 | cc_alrm_execute(ckjptr(sj_buf), |
---|
256 | int timo, |
---|
257 | ck_sighand handler, |
---|
258 | ck_sigfunc dofunc, |
---|
259 | ck_sigfunc failfunc |
---|
260 | ) |
---|
261 | |
---|
262 | #else /* Not ANSIC C ... */ |
---|
263 | |
---|
264 | cc_alrm_execute(sj_buf, |
---|
265 | timo, |
---|
266 | handler, |
---|
267 | dofunc, |
---|
268 | failfunc |
---|
269 | ) |
---|
270 | ckjptr(sj_buf); |
---|
271 | int timo; |
---|
272 | ck_sighand handler; |
---|
273 | ck_sigfunc dofunc; |
---|
274 | ck_sigfunc failfunc; |
---|
275 | #endif /* CK_ANSIC */ |
---|
276 | |
---|
277 | /* cc_alrm_execute */ { |
---|
278 | |
---|
279 | int rc = 0; |
---|
280 | int savalrm = 0; |
---|
281 | _PROTOTYP(SIGTYP (*savhandler), (int)); |
---|
282 | savalrm = alarm(timo); |
---|
283 | savhandler = signal( SIGALRM, handler ); |
---|
284 | |
---|
285 | #ifdef NTASM |
---|
286 | sj_buf->retcode = 0 ; |
---|
287 | sj_buf->Id = GetCurrentThreadId() ; |
---|
288 | memset( &sj_buf->context, 0, sizeof(CONTEXT) ); |
---|
289 | sj_buf->context.ContextFlags = CONTEXT_FULL ; |
---|
290 | #ifndef COMMENT |
---|
291 | GetThreadContext( GetCurrentThread(), &(sj_buf->context) ) ; |
---|
292 | #else |
---|
293 | __asm |
---|
294 | { |
---|
295 | mov eax, dword ptr [sj_buf] |
---|
296 | push eax |
---|
297 | mov eax, 0xfffffffe |
---|
298 | push eax |
---|
299 | mov eax, 0x00000039 |
---|
300 | mov edx,esp |
---|
301 | int 0x2e |
---|
302 | pop eax |
---|
303 | pop eax |
---|
304 | } |
---|
305 | #endif |
---|
306 | isinterrupted = 0; |
---|
307 | #endif /* NTASM */ |
---|
308 | if ( |
---|
309 | #ifdef NTASM |
---|
310 | sj_buf->retcode |
---|
311 | #else |
---|
312 | cksetjmp(ckjdref(sj_buf)) |
---|
313 | #endif /* NTASM */ |
---|
314 | ) { |
---|
315 | (*failfunc)(NULL) ; |
---|
316 | rc = -1 ; |
---|
317 | } else { |
---|
318 | #ifdef NT |
---|
319 | __try { |
---|
320 | (*dofunc)(NULL) ; |
---|
321 | } |
---|
322 | __except( exception_filter() ) |
---|
323 | { |
---|
324 | debug(F100,"cc_alrm_execute __except","",0); |
---|
325 | debug(F111,"exception_filter","_exception_code", GetExceptionCode() ) ; |
---|
326 | longjmp(ckjdref(sj_buf),SIGINT) ; |
---|
327 | } |
---|
328 | #else /* NT */ |
---|
329 | (*dofunc)(NULL) ; |
---|
330 | #endif /* NT */ |
---|
331 | } |
---|
332 | alarm(savalrm) ; |
---|
333 | if ( savhandler ) |
---|
334 | signal( SIGALRM, savhandler ) ; |
---|
335 | return rc ; |
---|
336 | } |
---|
337 | |
---|