-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcknsig.c
286 lines (259 loc) · 8.85 KB
/
cknsig.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
/* C K N S I G -- Kermit signal handling for Win32 systems */
/*
Author: Jeffrey E Altman ([email protected]),
Secure Endpoints Inc., New York City.
Copyright (C) 1985, 2004, Trustees of Columbia University in the City of New
York. All rights reserved.
*/
#include "ckcsym.h"
#include "ckcasc.h" /* ASCII character symbols */
#include "ckcdeb.h" /* Debug & other symbols */
#include "ckcker.h" /* Kermit symbols */
#include "ckcnet.h" /* Network symbols */
#ifndef NOSPL
#include "ckuusr.h"
#endif /* NOSPL */
#ifdef NT
#include <windows.h>
#else /* NT */
#define INCL_WIN
#define INCL_VIO
#define INCL_ERRORS
#define INCL_DOSPROCESS
#define INCL_DOSSEMAPHORES
#define INCL_DOSDEVIOCTL
#define INCL_WINCLIPBOARD
#define INCL_DOSDATETIME
#include <os2.h>
#undef COMMENT /* COMMENT is defined in os2.h */
#endif /* NT */
#include "ckothr.h"
#include "ckosyn.h"
#include <signal.h>
#ifndef NOCCTRAP
#include <setjmp.h>
#endif /* NOCCTRAP */
#include "ckcsig.h" /* requires ckocon.h to be earlier */
#define THRDSTKSIZ (2*131072)
extern int network, ttyfd ;
extern int TlsIndex ;
int
cc_execute( ckjptr(sj_buf), ck_sigfunc dofunc, ck_sigfunc failfunc ) {
int rc = 0 ;
TID tidThread ;
ULONG semid ;
ULONG ccindex ;
#ifdef NT
HANDLE hevThread ;
#else /* NT */
HEV hevThread ;
#endif /* NT */
struct _threadinfo * threadinfo = (struct _threadinfo *) TlsGetValue(TlsIndex) ;
debug(F100,"cc_execute enter","",0);
/* create an event semaphore for new thread */
#ifdef NT
hevThread = CreateEvent( NULL, TRUE, FALSE, NULL ) ;
#else
DosCreateEventSem( NULL, &hevThread, DC_SEM_SHARED, FALSE ) ;
#endif /* NT */
CreateCtrlCSem( FALSE, &ccindex ) ;
CreateCtrlCMuxWait( ccindex, hevThread ) ;
/* Begin new thread with dofunc */
tidThread = ckThreadBegin(dofunc,THRDSTKSIZ,(void *)&hevThread,TRUE,
threadinfo ? threadinfo->id : NULL );
/* Wait for the event semaphore or Ctrl-C */
/* semaphore to be set */
/* when Ctrl-C semaphore is set execute failfunc */
/* and return -1 */
/* else if event semaphore is set return 0 */
semid = WaitCtrlCMuxWait( ccindex, SEM_INDEFINITE_WAIT ) ;
if ( semid == 1 ) { /* Ctrl-C */
debug(F100,"cc_execute ctrl-c","",0);
ResetCtrlCSem(ccindex) ;
if ( !network ) {
debug(F100,"cc_execute about to PurgeComm","",0);
if (PurgeComm( (HANDLE) ttyfd, PURGE_TXABORT | PURGE_RXABORT )) {
debug( F100, "cc_execute PurgeComm successful","",0);
} else {
debug( F101, "cc_execute PurgeComm failed","",GetLastError() );
}
msleep(500);
}
ckThreadKill( tidThread ) ;
rc = -1 ;
(*failfunc)(0) ;
} else if ( semid == 2 ) { /* Thread completed successfully */
debug(F100,"cc_execute thread completed","",0);
ResetSem( hevThread ) ;
}
CloseCtrlCMuxWait(ccindex) ;
CloseCtrlCSem( ccindex ) ;
#ifdef NT
CloseHandle( hevThread ) ;
#ifdef NTSIG
ck_ih();
#endif /* NTSIG */
#else /* NT */
DosCloseEventSem( hevThread );
#endif /* NT */
return rc ;
}
int
alrm_execute(ckjptr(sj_buf),
int timo,
ck_sighand handler,
ck_sigfunc dofunc,
ck_sigfunc failfunc
)
/* alrm_execute */ {
int rc = 0;
TID tidThread ;
ULONG semid ;
ULONG alrmindex ;
#ifdef NT
HANDLE hevThread ;
#else /* NT */
HEV hevThread ;
#endif /* NT */
int savalrm = 0;
_PROTOTYP( SIGTYP (*savhandler), (int) );
struct _threadinfo * threadinfo = (struct _threadinfo *) TlsGetValue(TlsIndex) ;
debug(F100,"alrm_execute enter","",0);
savalrm = alarm(timo);
savhandler = signal( SIGALRM, handler );
/* create an event semaphore for new thread */
#ifdef NT
hevThread = CreateEvent( NULL, TRUE, FALSE, NULL ) ;
#else
DosCreateEventSem( NULL, &hevThread, DC_SEM_SHARED, FALSE ) ;
#endif /* NT */
CreateAlarmSigSem( FALSE, &alrmindex ) ;
CreateAlarmSigMuxWait( alrmindex, hevThread ) ;
/* begin new thread with dofunc */
tidThread = ckThreadBegin(dofunc,THRDSTKSIZ,(void *)&hevThread,TRUE,
threadinfo ? threadinfo->id : NULL );
/* wait for the event semaphore or Ctrl-C */
/* semaphore to be set */
/* when Ctrl-C semaphore is set execute failfunc */
/* and return -1 */
/* else if event semaphore is set return 0 */
semid = WaitAlarmSigMuxWait( alrmindex, SEM_INDEFINITE_WAIT ) ;
if ( semid == 1 ) { /* Alarm */
debug(F100,"alrm_execute timeout","",0);
(*handler)(SIGALRM) ;
ResetAlarmSigSem(alrmindex) ;
if ( !network ) {
debug(F100,"alrm_execute about to PurgeComm","",0);
if (PurgeComm( (HANDLE) ttyfd, PURGE_TXABORT | PURGE_RXABORT )) {
debug( F100, "alrm_execute PurgeComm successful","",0);
} else {
debug(F101,"alrm_execute PurgeComm failed","",GetLastError() );
}
msleep(500);
}
ckThreadKill( tidThread ) ;
rc = -1 ;
(*failfunc)(0) ;
} else if ( semid == 2 ) { /* Thread completed successfully */
debug(F100,"alrm_execute thread completed","",0);
ResetSem( hevThread ) ;
}
CloseAlarmSigMuxWait(alrmindex) ;
CloseAlarmSigSem( alrmindex ) ;
#ifdef NT
CloseHandle( hevThread ) ;
#else /* NT */
DosCloseEventSem( hevThread );
#endif /* NT */
alarm(savalrm) ;
if ( savhandler )
signal( SIGALRM, savhandler ) ;
#ifdef NTSIG
ck_ih();
#endif /* NTSIG */
return rc ;
}
int
cc_alrm_execute(ckjptr(sj_buf),
int timo,
ck_sighand handler,
ck_sigfunc dofunc,
ck_sigfunc failfunc
)
/* cc_alrm_execute */ {
int rc = 0;
TID tidThread ;
ULONG semid ;
ULONG alrmindex, ccindex ;
#ifdef NT
HANDLE hevThread ;
#else /* NT */
HEV hevThread ;
#endif /* NT */
struct _threadinfo * threadinfo = (struct _threadinfo *) TlsGetValue(TlsIndex) ;
int savalrm = 0;
_PROTOTYP( SIGTYP (*savhandler), (int) );
debug(F100,"cc_alrm_execute enter","",0);
savalrm = alarm(timo);
savhandler = signal( SIGALRM, handler );
/* create an event semaphore for new thread */
#ifdef NT
hevThread = CreateEvent( NULL, TRUE, FALSE, NULL ) ;
#else
DosCreateEventSem( NULL, &hevThread, DC_SEM_SHARED, FALSE ) ;
#endif /* NT */
CreateCtrlCSem( FALSE, &ccindex ) ;
CreateAlarmSigSem( FALSE, &alrmindex ) ;
CreateCtrlCAlarmSigMuxWait( ccindex, alrmindex, hevThread ) ;
/* begin new thread with dofunc */
tidThread = ckThreadBegin(dofunc,THRDSTKSIZ,(void *)&hevThread,TRUE,
threadinfo ? threadinfo->id : NULL );
/* wait for the event semaphore or Ctrl-C */
/* semaphore to be set */
/* when Ctrl-C semaphore is set execute failfunc */
/* and return -1 */
/* else if event semaphore is set return 0 */
semid = WaitCtrlCAlarmSigMuxWait(ccindex,alrmindex,SEM_INDEFINITE_WAIT);
if ( semid == 1 ) { /* CtrlC */
debug(F100,"cc_alrm_execute ctrl-c","",0);
ResetCtrlCSem(ccindex) ;
ckThreadKill( tidThread ) ;
rc = -1 ;
(*failfunc)(0) ;
} else if ( semid == 2 ) { /* Alarm */
debug(F100,"cc_alrm_execute timeout","",0);
(*handler)(SIGALRM) ;
ResetAlarmSigSem(alrmindex) ;
if ( !network ) {
debug(F100,"cc_alrm_execute about to PurgeComm","",0);
if (PurgeComm( (HANDLE) ttyfd, PURGE_TXABORT | PURGE_RXABORT )) {
debug( F100, "cc_alrm_execute PurgeComm successful","",0);
} else {
debug(F101,"cc_alrm_execute PurgeComm failed","",
GetLastError() );
}
msleep(500);
}
ckThreadKill( tidThread ) ;
rc = -1 ;
(*failfunc)(0) ;
} else if ( semid == 3 ) { /* Thread completed successfully */
debug(F100,"cc_alrm_execute thread completed","",0);
ResetSem( hevThread ) ;
}
CloseCtrlCAlarmSigMuxWait(alrmindex) ;
CloseCtrlCSem( ccindex ) ;
CloseAlarmSigSem( alrmindex ) ;
#ifdef NT
CloseHandle( hevThread ) ;
#else /* NT */
DosCloseEventSem( hevThread );
#endif /* NT */
alarm(savalrm) ;
if ( savhandler )
signal( SIGALRM, savhandler ) ;
#ifdef NTSIG
ck_ih();
#endif /* NTSIG */
return rc ;
}