1 | /* C K C S I G . H */ |
---|
2 | |
---|
3 | /* Definitions and prototypes for signal handling */ |
---|
4 | |
---|
5 | /* |
---|
6 | Author: Jeffrey Altman (jaltman@columbia.edu), |
---|
7 | Columbia University Academic Information Systems, New York City. |
---|
8 | |
---|
9 | Copyright (C) 1985, 1996, Trustees of Columbia University in the City of New |
---|
10 | York. The C-Kermit software may not be, in whole or in part, licensed or |
---|
11 | sold for profit as a software product itself, nor may it be included in or |
---|
12 | distributed with commercial products or otherwise distributed by commercial |
---|
13 | concerns to their clients or customers without written permission of the |
---|
14 | Office of Kermit Development and Distribution, Columbia University. This |
---|
15 | copyright notice must not be removed, altered, or obscured. |
---|
16 | */ |
---|
17 | #ifdef OS2 |
---|
18 | #ifndef NT |
---|
19 | #ifndef __HEV__ /* INCL_SEMAPHORE may also define HEV */ |
---|
20 | #define __HEV__ |
---|
21 | typedef ULONG HEV; /* hev */ |
---|
22 | typedef HEV *PHEV; |
---|
23 | #endif /* __HEV__ */ |
---|
24 | #endif /* NT */ |
---|
25 | struct _threadinfo { |
---|
26 | int inuse; |
---|
27 | int child; |
---|
28 | int sibling; |
---|
29 | #ifdef NT |
---|
30 | HANDLE id; |
---|
31 | HANDLE handle; |
---|
32 | HANDLE parent; |
---|
33 | HANDLE CompletionSem ; |
---|
34 | HANDLE DieSem ; |
---|
35 | #else /* NT */ |
---|
36 | TID id; |
---|
37 | TID parent; |
---|
38 | HEV CompletionSem; |
---|
39 | HEV DieSem; |
---|
40 | #endif /* NT */ |
---|
41 | }; |
---|
42 | #endif /* OS2 */ |
---|
43 | |
---|
44 | #ifdef CK_ANSIC |
---|
45 | typedef SIGTYP (*ck_sigfunc)(void *); |
---|
46 | typedef SIGTYP (*ck_sighand)(int); |
---|
47 | #else |
---|
48 | typedef SIGTYP (*ck_sigfunc)(); |
---|
49 | typedef SIGTYP (*ck_sighand)(); |
---|
50 | #endif /* CK_ANSIC */ |
---|
51 | |
---|
52 | /* Macros for POSIX vs old-style signal handling. */ |
---|
53 | |
---|
54 | #ifdef CK_POSIX_SIG |
---|
55 | typedef sigjmp_buf ckjmpbuf; |
---|
56 | #else |
---|
57 | #ifdef NT |
---|
58 | #include <windows.h> |
---|
59 | #ifdef NTASM |
---|
60 | typedef struct { |
---|
61 | CONTEXT context; |
---|
62 | DWORD retcode; |
---|
63 | } ckjmpbuf; |
---|
64 | #else /* NTASM */ |
---|
65 | typedef jmp_buf ckjmpbuf; |
---|
66 | #endif /* NTASM */ |
---|
67 | #else |
---|
68 | typedef jmp_buf ckjmpbuf; |
---|
69 | #endif |
---|
70 | #endif /* CK_POSIX_SIG */ |
---|
71 | /* |
---|
72 | Suppose you want to pass the address of a jmp_buf bar to a function foo. |
---|
73 | Since jmp_buf is normally defined (typedef'd) as an array, you would do |
---|
74 | it like this: foo(bar), where foo = foo(jmp_buf bar). But suppose a |
---|
75 | jmp_buf is (say) a struct rather than an array. Then you must do |
---|
76 | foo(&bar) where foo is foo(jmp_buf * bar). This is controlled here in |
---|
77 | the traditional fashion, by ifdefs. By default, we assume that jmp_buf |
---|
78 | is an array. Define the symbol JBNOTARRAY if jmp_buf is not an array. |
---|
79 | */ |
---|
80 | #ifndef JBNOTARRAY |
---|
81 | #ifdef NT |
---|
82 | #define JBNOTARRAY |
---|
83 | #endif /* NT */ |
---|
84 | #endif /* JBNOTARRAY */ |
---|
85 | |
---|
86 | #ifdef JBNOTARRAY |
---|
87 | typedef ckjmpbuf * ckjptr; |
---|
88 | #define ckjaddr(x) & x |
---|
89 | #define ckjdref(x) * x |
---|
90 | #ifdef CK_POSIX_SIG |
---|
91 | #define cksetjmp(x) sigsetjmp(x,1) |
---|
92 | #define cklongjmp(x,y) siglongjmp(x,y) |
---|
93 | #else |
---|
94 | #ifdef NT |
---|
95 | __inline int |
---|
96 | ck_ih(void) { |
---|
97 | extern int TlsIndex; |
---|
98 | #ifdef NTSIG |
---|
99 | struct _threadinfo * threadinfo; |
---|
100 | threadinfo = (struct _threadinfo *) TlsGetValue(TlsIndex); |
---|
101 | if (threadinfo) { |
---|
102 | if (WaitAndResetSem(threadinfo->DieSem,0)) { |
---|
103 | ckThreadDie(threadinfo); |
---|
104 | return 1; /* This should never execute */ |
---|
105 | } |
---|
106 | } |
---|
107 | #ifdef COMMENT |
---|
108 | else debug( F100, "ck_ih() threadinfo is NULL","",0); |
---|
109 | #endif /* COMMENT */ |
---|
110 | #endif /* NTSIG */ |
---|
111 | return 0; |
---|
112 | } |
---|
113 | #ifdef NTSIG |
---|
114 | #define cksetjmp(x) setjmp(x) |
---|
115 | #define cklongjmp(x,y) longjmp(x,y) |
---|
116 | #else /* NTSIG */ |
---|
117 | #ifdef NTASM |
---|
118 | __inline DWORD |
---|
119 | cksetjmp( ckjptr jmp ) { |
---|
120 | extern int isinterrupted; |
---|
121 | jmp->retcode = 0; |
---|
122 | memset( &jmp->context, 0, sizeof(CONTEXT) ); |
---|
123 | jmp->context.ContextFlags = CONTEXT_FULL ; |
---|
124 | if ( !GetThreadContext( GetCurrentThread(), &jmp->context ) ) |
---|
125 | debug( F101, "cksetjmp GetThreadContext failed","",GetLastError()); |
---|
126 | debug(F101,"cksetjmp returns","",jmp->retcode); |
---|
127 | isinterrupted = 0; |
---|
128 | return (jmp->retcode); |
---|
129 | } |
---|
130 | |
---|
131 | __inline void |
---|
132 | cklongjmp( ckjptr jmp, int retval ) { |
---|
133 | extern HANDLE tidCommand; |
---|
134 | extern int ttyfd, mdmtyp ; |
---|
135 | extern DWORD CommandID; |
---|
136 | extern int isinterrupted; |
---|
137 | |
---|
138 | connoi(); |
---|
139 | isinterrupted = 1; |
---|
140 | jmp->retcode = ( retval ? retval : 1 ); |
---|
141 | debug(F101,"about to SetThreadContext for thread","", CommandID); |
---|
142 | debug(F101,"from Thread","",GetCurrentThreadId()); |
---|
143 | if ( mdmtyp >= 0 ) { |
---|
144 | PurgeComm( (HANDLE) ttyfd, PURGE_TXABORT | PURGE_RXABORT ); |
---|
145 | } |
---|
146 | if (SetThreadContext( tidCommand, &jmp->context )) |
---|
147 | debug(F100,"cklongjmp SetThreadContext success","",0); |
---|
148 | else |
---|
149 | debug(F101,"cklongjmp SetThreadContext failed","",GetLastError()); |
---|
150 | msleep(50); |
---|
151 | cmini(1); /* Reset command parser */ |
---|
152 | putkey(13); /* Stuff a carriage return */ |
---|
153 | /* PostEventAvailSem(); */ |
---|
154 | } |
---|
155 | #else /* NTASM */ |
---|
156 | void crash( void ) ; |
---|
157 | #define cksetjmp(x) setjmp(x) |
---|
158 | __inline void |
---|
159 | cklongjmp( ckjptr jmp, int retval ) { |
---|
160 | extern HANDLE tidCommand; |
---|
161 | extern int ttyfd, mdmtyp; |
---|
162 | extern DWORD CommandID; |
---|
163 | CONTEXT context; |
---|
164 | |
---|
165 | if ( mdmtyp >= 0 ) { |
---|
166 | PurgeComm( (HANDLE) ttyfd, PURGE_TXABORT | PURGE_RXABORT ) ; |
---|
167 | } |
---|
168 | memset( &context, 0, sizeof(CONTEXT) ); |
---|
169 | context.ContextFlags = CONTEXT_FULL; |
---|
170 | if ( !GetThreadContext( tidCommand, &context ) ) |
---|
171 | debug( F101, "cklongjmp GetThreadContext failed","",GetLastError()); |
---|
172 | |
---|
173 | /* Invalidate the instruction pointer */ |
---|
174 | context.Eip = (unsigned long) crash; |
---|
175 | |
---|
176 | debug(F101,"about to SetThreadContext for thread","", CommandID); |
---|
177 | debug(F101,"from Thread","",GetCurrentThreadId()); |
---|
178 | if (SetThreadContext( tidCommand, &context )) |
---|
179 | debug(F100,"cklongjmp SetThreadContext success","",0); |
---|
180 | else |
---|
181 | debug(F101,"cklongjmp SetThreadContext failed","",GetLastError()); |
---|
182 | } |
---|
183 | #endif /* NTASM */ |
---|
184 | #endif /* NTSIG */ |
---|
185 | #else /* NT */ |
---|
186 | #define cksetjmp(x) setjmp(x) |
---|
187 | #endif /* NT */ |
---|
188 | #endif /* CK_POSIX_SIG */ |
---|
189 | #else /* jmp_buf is an array */ |
---|
190 | typedef ckjmpbuf ckjptr; |
---|
191 | #define ckjaddr(x) x |
---|
192 | #define ckjdref(x) x |
---|
193 | #ifdef CK_POSIX_SIG |
---|
194 | #define cksetjmp(x) sigsetjmp(x,1) |
---|
195 | #define cklongjmp(x,y) siglongjmp(x,y) |
---|
196 | #else |
---|
197 | #define cksetjmp(x) setjmp(x) |
---|
198 | #define cklongjmp(x,y) longjmp(x,y) |
---|
199 | #endif /* CK_POSIX_SIG */ |
---|
200 | #endif /* JBNOTARRAY */ |
---|
201 | |
---|
202 | _PROTOTYP( int cc_execute, (ckjptr, ck_sigfunc, ck_sigfunc) ); |
---|
203 | _PROTOTYP( int alrm_execute, |
---|
204 | (ckjptr, |
---|
205 | int /* timo */, |
---|
206 | ck_sighand /* handler */, |
---|
207 | ck_sigfunc, ck_sigfunc) ); |
---|
208 | _PROTOTYP( int cc_alrm_execute, |
---|
209 | (ckjptr, |
---|
210 | int /* timo */, |
---|
211 | ck_sighand /* handler */, |
---|
212 | ck_sigfunc, |
---|
213 | ck_sigfunc) ); |
---|
214 | |
---|
215 | /* End of ckusig.h */ |
---|
216 | |
---|