home *** CD-ROM | disk | FTP | other *** search
/ High Voltage Shareware / high1.zip / high1 / DIR24 / EDMI5.ZIP / IFSR0.ZIP / R0COMM.C < prev    next >
C/C++ Source or Header  |  1993-10-04  |  9KB  |  283 lines

  1. /******************************************************************************
  2. *******************************************************************************
  3. **
  4. ** EXR0R3 - A ring 0/ring 3 IFS skeleton
  5. ** Copyright (C) 1993 by Andre Asselin
  6. **
  7. ** R0COMM.C - Ring 0 side of communications
  8. **
  9. ** History:
  10. ** 5/25/93 - created
  11. **
  12. *******************************************************************************
  13. ******************************************************************************/
  14.  
  15. #include "r0inc.h"
  16.  
  17.  
  18. // Local prototypes
  19. static void DetachCP(void);
  20.  
  21.  
  22. /******************************************************************************
  23. **
  24. ** FS_INIT - Initialize the IFS
  25. **
  26. ** Parameters
  27. ** ---------
  28. ** char far *szParm                    pointer to command line parameters
  29. ** unsigned long pDevHlp               pointer to DevHlp entry point
  30. ** unsigned long far *pMiniFSD         pointer to data passed between the
  31. **                                     mini-FSD and the IFS
  32. **
  33. ******************************************************************************/
  34.  
  35.  
  36. #pragma argsused
  37. short int far pascal _export _loadds FS_INIT(char far *szParm,
  38.                                              void (*pDevHlp)(void),
  39.                                              unsigned long far *pMiniFSD)
  40. {
  41.    SEL GDTSels[2];
  42.    int rc;
  43.    static char signon[]="EXR0R3.IFS - A ring 0/ring 3 IFS skeleton\r\n"
  44.                         "Copyright (C) 1993 by Andre Asselin\r\n";
  45.  
  46.    // Put up banner
  47.    DosPutMessage(1, sizeof(signon)-1, signon);
  48.  
  49.    // Run C runtime startup code
  50.    Startup();
  51.  
  52.    // If there were any command line parameters, save them
  53.    if (szParm != NULL) {
  54.       strncpy(CmdLineSave, szParm, sizeof(CmdLineSave));
  55.    }
  56.  
  57.    // Save the DevHelp entry point
  58.    DevHelp = pDevHlp;
  59.  
  60.    // Allocate virtual memory for the two lock handles
  61.    rc = VMAlloc(VMAPA_NOPHYSADDR, 2 * sizeof(HLOCK),
  62.                 VMAF_SWAPPABLE | VMAF_GLOBALSPACE, &Lock1);
  63.    if (rc != NO_ERROR)
  64.       return rc;
  65.    Lock2 = Lock1 + sizeof(HLOCK);
  66.  
  67.    // Allocate 2 GDT selectors to communicate with the control program
  68.    rc = AllocGDTSelector(2, GDTSels);
  69.    if (rc != NO_ERROR)
  70.       return rc;
  71.  
  72.    // Set the pointers to the control program communication buffer
  73.    CPData.OpData = (OPDATA *)MK_FP(GDTSels[0], 0);
  74.    CPData.Buf = (void *)MK_FP(GDTSels[1], 0);
  75.  
  76.    // Say the control program has never been attached
  77.    CPAttached = -1;
  78.  
  79.    return NO_ERROR;
  80. }
  81.  
  82.  
  83. /******************************************************************************
  84. **
  85. ** FS_SHUTDOWN - Prepare for a system shutdown
  86. **
  87. ** Parameters
  88. ** ---------
  89. ** unsigned short usType               flavor of call
  90. **   values:
  91. **     SD_BEGIN                  begining the shutdown process
  92. **     SD_COMPLETE               finishing the shutdown process
  93. ** unsigned long ulReserved            reserved
  94. **
  95. ******************************************************************************/
  96.  
  97. #pragma argsused
  98. short int far pascal _export _loadds FS_SHUTDOWN(unsigned short usType,
  99.                                                  unsigned long ulReserved)
  100. {
  101.    return NO_ERROR;
  102. }
  103.  
  104.  
  105. /******************************************************************************
  106. **
  107. ** FS_FSCTL - Extended IFS control
  108. **
  109. ** Parameters
  110. ** ---------
  111. ** union argdat far *pArgdat
  112. ** unsigned short iArgType             argument type
  113. **   values:
  114. **     FSCTL_ARG_FILEINSTANCE    filehandle directed
  115. **     FSCTL_ARG_CURDIR          pathname directed
  116. **     FSCTL_ARG_NULL            FSD directed
  117. ** unsigned short func                 function code
  118. **   values:
  119. **     FSCTL_FUNC_NONE           NULL function
  120. **     FSCTL_FUNC_NEW_INFO       return error code information
  121. **     FSCTL_FUNC_EASIZE         return max EA size and max EA list size
  122. ** char far *pParm                     UNVERIFIED pointer to parameter area
  123. ** unsigned short lenParm              size of area pointed to by pParm
  124. ** unsigned short far *plenParmOut     length of parameters passed in pParm
  125. ** char far *pData                     UNVERIFIED pointer to information area
  126. ** unsigned short lenData              size of area pointed to by pData
  127. ** unsigned short far *plenDataOut     length of parameters passed in pData
  128. **
  129. ******************************************************************************/
  130.  
  131. #pragma argsused
  132. short int far pascal _export _loadds FS_FSCTL(union argdat far *pArgdat,
  133.                                               unsigned short iArgType,
  134.                                               unsigned short func,
  135.                                               char far *pParm,
  136.                                               unsigned short lenParm,
  137.                                               unsigned short far *plenParmOut,
  138.                                               char far *pData,
  139.                                               unsigned short lenData,
  140.                                               unsigned short far *plenDataOut)
  141. {
  142.    int rc;
  143.    SEL *LInfoSeg;
  144.  
  145.    switch (func) {
  146.    case FSCTL_FUNC_NONE:
  147.         return NO_ERROR;
  148.  
  149.    case FSCTL_FUNC_NEW_INFO:
  150.    case FSCTL_FUNC_EASIZE:
  151.         return ERROR_NOT_SUPPORTED;
  152.  
  153.    case FSCTL_FUNC_INIT:
  154.       // pParm points to an InitStruc
  155.  
  156.       // If control program has never been attached, ignore these checks
  157.       if (CPAttached != -1) {
  158.          // Make sure the control program isn't already attached
  159.          if (CPAttached)
  160.             return (ERROR_INVALID_PARAMETER);
  161.  
  162.          // Make sure that it is OK to attach at this time
  163.          rc = FSH_SEMWAIT(&CPData.BufLock, 0);
  164.          if (rc != NO_ERROR)
  165.             return (ERROR_INVALID_PARAMETER);
  166.       }
  167.  
  168.       // Verify size of and addressability of the pParm area
  169.       if (lenParm != sizeof(INITSTRUC))
  170.          return ERROR_INVALID_PARAMETER;
  171.       rc = FSH_PROBEBUF(PB_OPREAD, pParm, lenParm);
  172.       if (rc != NO_ERROR)
  173.          return rc;
  174.  
  175.       // Lock down the operation buffer
  176.       rc = VMLock(((INITSTRUC *)pParm)->OpData, sizeof(OPDATA), VMLPL_NOPAGELIST,
  177.                   Lock1, VMLAF_WRITE | VMLAF_LONG, NULL);
  178.       if (rc != NO_ERROR)
  179.          return rc;
  180.  
  181.       // Map the GDT selector we got at init time to the memory we just locked
  182.       rc = LinToGDTSelector(FP_SEG(CPData.OpData), 
  183.                             ((INITSTRUC *)pParm)->OpData, sizeof(OPDATA));
  184.       if (rc != NO_ERROR)
  185.          return rc;
  186.  
  187.       // Lock down the data buffer
  188.       rc = VMLock(((INITSTRUC *)pParm)->Buf, 65536, VMLPL_NOPAGELIST,
  189.                   Lock2, VMLAF_WRITE | VMLAF_LONG, NULL);
  190.       if (rc != NO_ERROR)
  191.          return rc;
  192.  
  193.       // Map the GDT selector we got at init time to the memory we just locked
  194.       rc = LinToGDTSelector(FP_SEG(CPData.Buf), ((INITSTRUC *)pParm)->Buf,
  195.                             65536);
  196.       if (rc != NO_ERROR)
  197.          return rc;
  198.  
  199.       // The operation data area is now OK to use
  200.       rc = FSH_SEMCLEAR(&CPData.BufLock);
  201.       if (rc != NO_ERROR)
  202.          return rc;
  203.  
  204.       // Get PID of the control program
  205.       rc = GetDOSVar(DHGETDOSV_LOCINFOSEG, (unsigned long *)&LInfoSeg);
  206.       if (rc != NO_ERROR)
  207.          return rc;
  208.  
  209.       CPPID = ((LINFOSEG *)MK_FP(LInfoSeg, 0))->pidCurrent;
  210.  
  211.       // The control program is now attached
  212.       CPAttached = 1;
  213.  
  214.       /* FALLTHRU */
  215.  
  216.    case FSCTL_FUNC_NEXT:
  217.       if (!CPAttached)
  218.          return ERROR_BAD_COMMAND;
  219.  
  220.       // Set the semaphore that indicates the control program is ready for
  221.       // another operation
  222.       rc = FSH_SEMSET(&CPData.CmdReady);
  223.       if (rc == ERROR_INTERRUPT) {
  224.          DetachCP();
  225.          return rc;
  226.       }
  227.  
  228.       // Clear the semaphore that indicates the control program just returned
  229.       rc = FSH_SEMCLEAR(&CPData.CmdComplete);
  230.       if (rc == ERROR_INTERRUPT) {
  231.          DetachCP();
  232.          return rc;
  233.       }
  234.  
  235.       // Wait for the semaphore that indicates another operation is ready
  236.       rc = FSH_SEMWAIT(&CPData.CmdReady, -1);
  237.       if (rc == ERROR_INTERRUPT)
  238.          DetachCP();
  239.  
  240.       return rc;
  241.  
  242.    default:
  243.       return ERROR_NOT_SUPPORTED;
  244.    }
  245. }
  246.  
  247.  
  248.  
  249. /******************************************************************************
  250. **
  251. ** FS_EXIT - Notify FSD that a process is ending
  252. **
  253. ** Parameters
  254. ** ---------
  255. ** unsigned short uid                   user ID of process
  256. ** unsigned short pid                   process ID of process
  257. ** unsigned short pdb                   DOS mode process ID of process
  258. **
  259. ******************************************************************************/
  260.  
  261. #pragma argsused
  262. void far pascal _export _loadds FS_EXIT(unsigned short uid, unsigned short pid,
  263.                                         unsigned short pdb)
  264. {
  265.    // If the control program is exiting, detach ourselves from it
  266.    if (CPAttached == 1 && pid == CPPID)
  267.       DetachCP();
  268. }
  269.  
  270.  
  271. /******************************************************************************
  272. **
  273. ** DetachCP - Detach the control program from the IFS
  274. **
  275. ******************************************************************************/
  276.  
  277. static void DetachCP(void) {
  278.    CPAttached = 0;
  279.    VMUnlock(Lock1);
  280.    VMUnlock(Lock2);
  281.    FSH_SEMCLEAR(&CPData.CmdComplete);
  282. }
  283.