home *** CD-ROM | disk | FTP | other *** search
/ Columbia Kermit / kermit.zip / k95source / cknsig.c < prev    next >
C/C++ Source or Header  |  2020-01-01  |  9KB  |  287 lines

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