home *** CD-ROM | disk | FTP | other *** search
/ Network Support Encyclopedia 96-1 / novell-nsepro-1996-1-cd2.iso / download / netware / xfsh1.exe / FSMON.C next >
Text File  |  1995-08-28  |  8KB  |  305 lines

  1. /**************************************************************************
  2. **   File: fsmon.c
  3. **
  4. **   Description:
  5. **
  6. **   Example program for using a File-System Monitor Hook.
  7. **
  8. **   This program demonstrates how to use File-System Monitoring Services
  9. **   to monitor files that are closed on the File Server.
  10. **
  11. **
  12. **   DISCLAIMER  
  13. **  
  14. **   Novell, Inc. makes no representations or warranties with respect to
  15. **   any NetWare software, and specifically disclaims any express or
  16. **   implied warranties of merchantability, title, or fitness for a
  17. **   particular purpose.  
  18. **
  19. **   Distribution of any NetWare software is forbidden without the
  20. **   express written consent of Novell, Inc.  Further, Novell reserves
  21. **   the right to discontinue distribution of any NetWare software.
  22. **   
  23. **   Novell is not responsible for lost profits or revenue, loss of use
  24. **   of the software, loss of data, costs of re-creating lost data, the
  25. **   cost of any substitute equipment or program, or claims by any party
  26. **   other than you.  Novell strongly recommends a backup be made before
  27. **   any software is installed.   Technical support for this software
  28. **   may be provided at the discretion of Novell.
  29. **
  30. **      QMK386 options used:
  31. **
  32. **      None
  33. **
  34. **   Programmers:
  35. **
  36. **   Ini   Who                  Firm
  37. **   ---------------------------------------------------------------------
  38. **   KDB   Kevin Burnett        Novell Developer Support
  39. **   ABJ    Adam Jerome                Novell Developer Support
  40. **
  41. **
  42. **   History:
  43. **
  44. **   When        Who      What
  45. **   ---------------------------------------------------------------------
  46. **   8-24-1995   KDB      First code.
  47. **
  48. */
  49.  
  50.  
  51. /**************************************************************************
  52. ** Compiler setup.
  53. */
  54.  
  55.     /*----------------------------------------------------------------------
  56.     ** ANSI
  57.     */
  58.     #include <conio.h>    /*    SetAutoScreenDestructionMode(),
  59.                                     SetCtrlCharCheckMode() */
  60.     #include <process.h>    /* ThreadSwitchWithDelay(), GetThreadID(),
  61.                                     SetThreadGroupID(), GetThreadGroupID(),
  62.                                     ResumeThread(), SuspendThread() */
  63.     #include <stdio.h>    /* printf() */
  64.     #include <signal.h>    /* signal() */
  65.     #include <errno.h>    /* Symbol 'ESUCCESS'*/
  66.  
  67.     /*----------------------------------------------------------------------
  68.     ** NetWare
  69.     */
  70.     #include <fshooks.h>        /* NWAddFSMonitorHook(), NWRemoveFSMonitorHook() */
  71.     #include <fileengd.h>    /* FEMapConnsHandleToVolAndDir(),
  72.                                         FEMapVolumeAndDirectoryToPath(),
  73.                                         FEMapVolumeNumberToName() */
  74.  
  75. /**************************************************************************
  76. ** Global variables
  77. */
  78.     int    NLM_exiting = 0;
  79.     int   NLM_threadCnt=0;
  80.     int   NLM_mainThreadGroupID;
  81.     int    NLM_ccode;
  82.     int    NLM_mainThreadID;
  83.  
  84. /**************************************************************************
  85. ** Get and print path of closed file procedure.
  86. */
  87. int NLM_CloseFileEventHandler(void *v)
  88.       {                         
  89.     typedef struct
  90.        {
  91.         LONG    connection;
  92.         long    task;
  93.         long    fileHandle;
  94.        } CloseFileCallBackStruct;
  95.  
  96.     CloseFileCallBackStruct *c=(CloseFileCallBackStruct *)v;
  97.  
  98.     int    ccode,
  99.             volumeNumber;
  100.  
  101.     LONG    directoryNumber;
  102.  
  103.     char    nwPath[256],
  104.             volName[48];
  105.  
  106.     LONG    nwPathCnt;
  107.  
  108.     LONG  nCnt;
  109.     char  *cp;
  110.  
  111.     int oldThreadGroupID;
  112.  
  113.     ++NLM_threadCnt;
  114.  
  115.     /*----------------------------------------------------------------------
  116.     ** Save and then re-set the OS Thread ID to CLIB's Thread ID. 
  117.     */
  118.     oldThreadGroupID=SetThreadGroupID(NLM_mainThreadGroupID);
  119.  
  120.     /*----------------------------------------------------------------------
  121.     ** Get volume number and directory number relative to given connections
  122.     ** file handle.  This call defaults to OS context file handles, although
  123.     ** CLIB file hamdles may be used.
  124.     */
  125.     ccode = FEMapConnsHandleToVolAndDir(
  126.         /* I- conneciton Number */    c->connection,
  127.         /* I- handle                 */    (int)c->fileHandle,
  128.         /* -O volume Number        */    &volumeNumber,
  129.         /* -O directory Number    */    &directoryNumber
  130.         );
  131.  
  132.     if(ccode != ESUCCESS)
  133.        {
  134.         printf("ERROR: FEMapConsHandleToVolAndDir() reports %d\n", ccode);
  135.         NLM_exiting = (-1);
  136.      goto END_ERR;
  137.        }
  138.  
  139.     /*----------------------------------------------------------------------
  140.     ** Map the volume number and directory number to a NetWare-style path.
  141.     ** (These are length proceeded paths).
  142.     */
  143.     ccode = FEMapVolumeAndDirectoryToPath(
  144.         /* I- volume Number      */    volumeNumber,
  145.         /* I- directory Number */    directoryNumber,
  146.         /* -O path String       */    &nwPath,
  147.         /* -O path Count           */    &nwPathCnt
  148.         );
  149.  
  150.     if(ccode != ESUCCESS)
  151.        {
  152.         printf("ERROR: FEMapVolumeAndDirectoryToPath() reports %d\n", ccode);
  153.         NLM_exiting = (-1);
  154.         goto END_ERR;
  155.        }
  156.  
  157.     /*----------------------------------------------------------------------
  158.     ** Map the volume number to a volume name.
  159.     */
  160.     ccode = FEMapVolumeNumberToName(
  161.         /* I- volume Number       */    volumeNumber,
  162.         /* -O volume Name       */    &volName
  163.         );
  164.  
  165.     if(ccode != ESUCCESS)
  166.        {
  167.         printf("ERROR: FEMapVolumeNumberToName() reports %d\n", ccode);
  168.         NLM_exiting = (-1);
  169.         goto END_ERR;
  170.        }
  171.  
  172.     /*----------------------------------------------------------------------
  173.     ** Print out the volume name and path/file name, taking into consideration
  174.     ** the Netware lenght proceeded format.
  175.     */
  176.     printf("%.*s", volName[0], &volName[1]);
  177.     cp=nwPath;
  178.     for(nCnt=0; nCnt < nwPathCnt; ++nCnt)
  179.         {
  180.         if(nCnt == 0)
  181.             printf(":");
  182.         else
  183.             printf("\\");
  184.  
  185.         printf("%.*s", cp[0], &cp[1]);
  186.  
  187.         cp += cp[0] +1;
  188.         }
  189.     printf("\n");
  190.     
  191.  
  192. END_ERR:
  193.  
  194.     /*----------------------------------------------------------------------
  195.     ** Restore the OS's Thread ID. 
  196.     */
  197.     SetThreadGroupID(oldThreadGroupID);
  198.  
  199.   --NLM_threadCnt;
  200.  
  201.     return(ESUCCESS);
  202.   }
  203.  
  204. /**************************************************************************
  205. ** NLM Signal Handler.
  206. */
  207. void NLM_SignalHandler(int sig)
  208.   {
  209.   switch(sig)
  210.         {
  211.         case SIGTERM:
  212.             NLM_exiting = (-1);
  213.             ResumeThread(NLM_mainThreadID);
  214.             while(NLM_threadCnt != 0)
  215.                ThreadSwitchWithDelay();
  216.  
  217.             break;
  218.  
  219.         default:
  220.            break;
  221.         }
  222.  
  223.   
  224.   return;
  225.   }
  226.  
  227. /**************************************************************************
  228. ** main procedure
  229. */
  230. void main(void)
  231.     {
  232.     int    ccode;
  233.     LONG    CBHandle=0;
  234.  
  235.     ++NLM_threadCnt;
  236.  
  237.     /*----------------------------------------------------------------------
  238.     ** No final "Press a key" message when unloading.
  239.     */
  240.     SetAutoScreenDestructionMode(TRUE);
  241.  
  242.     /*----------------------------------------------------------------------
  243.     ** Disable CTRL-S and CTRL-C.
  244.     */
  245.     SetCtrlCharCheckMode(FALSE);
  246.  
  247.     /*----------------------------------------------------------------------
  248.     ** Get the current thread group ID.
  249.     */
  250.     NLM_mainThreadGroupID=GetThreadGroupID();
  251.     NLM_mainThreadID=GetThreadID();
  252.  
  253.     /*----------------------------------------------------------------------
  254.     ** Register SIGTERM & SIGINT handler.
  255.     */
  256.     signal(SIGTERM, NLM_SignalHandler);
  257.  
  258.     /*----------------------------------------------------------------------
  259.     ** Indicate we waiting for a file to close on the server
  260.     */
  261.     printf("Closed Files...\n");
  262.  
  263.     /*----------------------------------------------------------------------
  264.     ** Hook OS file system close file routine.
  265.     */
  266.     ccode = NWAddFSMonitorHook(
  267.         /* I- callBack Number     */    FSHOOK_PRE_CLOSEFILE,
  268.         /* I- callBack Function */    NLM_CloseFileEventHandler,
  269.         /* -O callBack Handle   */    &CBHandle
  270.         );
  271.  
  272.     if(ccode != 0)
  273.         {
  274.         printf("ERROR: NWAddFSMonitorHook() reports %d\n", ccode);
  275.        CBHandle = 0;
  276.        goto END_ERR;
  277.         }
  278.  
  279.     /*----------------------------------------------------------------------
  280.     ** Put Thread to sleep waiting for NLM to unload.
  281.     */
  282.     SuspendThread(NLM_mainThreadID);
  283.  
  284. END_ERR:
  285.  
  286.  
  287.     NLM_exiting = (-1);
  288.  
  289.     /*----------------------------------------------------------------------
  290.     ** Remove the OS file system hook.
  291.     */
  292.     if(CBHandle != 0)
  293.         NWRemoveFSMonitorHook(FSHOOK_PRE_CLOSEFILE, CBHandle);
  294.  
  295.     /*----------------------------------------------------------------------
  296.     ** Wait for FE handler to finish if needed.
  297.     */
  298.     while(NLM_threadCnt > 1)
  299.        ThreadSwitchWithDelay();
  300.  
  301.     --NLM_threadCnt;
  302.  
  303.     return;
  304.     }
  305.