ckcsig.h 6.0 KB

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