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 >
Wrap
Text File
|
1995-08-28
|
8KB
|
305 lines
/**************************************************************************
** File: fsmon.c
**
** Description:
**
** Example program for using a File-System Monitor Hook.
**
** This program demonstrates how to use File-System Monitoring Services
** to monitor files that are closed on the File Server.
**
**
** DISCLAIMER
**
** Novell, Inc. makes no representations or warranties with respect to
** any NetWare software, and specifically disclaims any express or
** implied warranties of merchantability, title, or fitness for a
** particular purpose.
**
** Distribution of any NetWare software is forbidden without the
** express written consent of Novell, Inc. Further, Novell reserves
** the right to discontinue distribution of any NetWare software.
**
** Novell is not responsible for lost profits or revenue, loss of use
** of the software, loss of data, costs of re-creating lost data, the
** cost of any substitute equipment or program, or claims by any party
** other than you. Novell strongly recommends a backup be made before
** any software is installed. Technical support for this software
** may be provided at the discretion of Novell.
**
** QMK386 options used:
**
** None
**
** Programmers:
**
** Ini Who Firm
** ---------------------------------------------------------------------
** KDB Kevin Burnett Novell Developer Support
** ABJ Adam Jerome Novell Developer Support
**
**
** History:
**
** When Who What
** ---------------------------------------------------------------------
** 8-24-1995 KDB First code.
**
*/
/**************************************************************************
** Compiler setup.
*/
/*----------------------------------------------------------------------
** ANSI
*/
#include <conio.h> /* SetAutoScreenDestructionMode(),
SetCtrlCharCheckMode() */
#include <process.h> /* ThreadSwitchWithDelay(), GetThreadID(),
SetThreadGroupID(), GetThreadGroupID(),
ResumeThread(), SuspendThread() */
#include <stdio.h> /* printf() */
#include <signal.h> /* signal() */
#include <errno.h> /* Symbol 'ESUCCESS'*/
/*----------------------------------------------------------------------
** NetWare
*/
#include <fshooks.h> /* NWAddFSMonitorHook(), NWRemoveFSMonitorHook() */
#include <fileengd.h> /* FEMapConnsHandleToVolAndDir(),
FEMapVolumeAndDirectoryToPath(),
FEMapVolumeNumberToName() */
/**************************************************************************
** Global variables
*/
int NLM_exiting = 0;
int NLM_threadCnt=0;
int NLM_mainThreadGroupID;
int NLM_ccode;
int NLM_mainThreadID;
/**************************************************************************
** Get and print path of closed file procedure.
*/
int NLM_CloseFileEventHandler(void *v)
{
typedef struct
{
LONG connection;
long task;
long fileHandle;
} CloseFileCallBackStruct;
CloseFileCallBackStruct *c=(CloseFileCallBackStruct *)v;
int ccode,
volumeNumber;
LONG directoryNumber;
char nwPath[256],
volName[48];
LONG nwPathCnt;
LONG nCnt;
char *cp;
int oldThreadGroupID;
++NLM_threadCnt;
/*----------------------------------------------------------------------
** Save and then re-set the OS Thread ID to CLIB's Thread ID.
*/
oldThreadGroupID=SetThreadGroupID(NLM_mainThreadGroupID);
/*----------------------------------------------------------------------
** Get volume number and directory number relative to given connections
** file handle. This call defaults to OS context file handles, although
** CLIB file hamdles may be used.
*/
ccode = FEMapConnsHandleToVolAndDir(
/* I- conneciton Number */ c->connection,
/* I- handle */ (int)c->fileHandle,
/* -O volume Number */ &volumeNumber,
/* -O directory Number */ &directoryNumber
);
if(ccode != ESUCCESS)
{
printf("ERROR: FEMapConsHandleToVolAndDir() reports %d\n", ccode);
NLM_exiting = (-1);
goto END_ERR;
}
/*----------------------------------------------------------------------
** Map the volume number and directory number to a NetWare-style path.
** (These are length proceeded paths).
*/
ccode = FEMapVolumeAndDirectoryToPath(
/* I- volume Number */ volumeNumber,
/* I- directory Number */ directoryNumber,
/* -O path String */ &nwPath,
/* -O path Count */ &nwPathCnt
);
if(ccode != ESUCCESS)
{
printf("ERROR: FEMapVolumeAndDirectoryToPath() reports %d\n", ccode);
NLM_exiting = (-1);
goto END_ERR;
}
/*----------------------------------------------------------------------
** Map the volume number to a volume name.
*/
ccode = FEMapVolumeNumberToName(
/* I- volume Number */ volumeNumber,
/* -O volume Name */ &volName
);
if(ccode != ESUCCESS)
{
printf("ERROR: FEMapVolumeNumberToName() reports %d\n", ccode);
NLM_exiting = (-1);
goto END_ERR;
}
/*----------------------------------------------------------------------
** Print out the volume name and path/file name, taking into consideration
** the Netware lenght proceeded format.
*/
printf("%.*s", volName[0], &volName[1]);
cp=nwPath;
for(nCnt=0; nCnt < nwPathCnt; ++nCnt)
{
if(nCnt == 0)
printf(":");
else
printf("\\");
printf("%.*s", cp[0], &cp[1]);
cp += cp[0] +1;
}
printf("\n");
END_ERR:
/*----------------------------------------------------------------------
** Restore the OS's Thread ID.
*/
SetThreadGroupID(oldThreadGroupID);
--NLM_threadCnt;
return(ESUCCESS);
}
/**************************************************************************
** NLM Signal Handler.
*/
void NLM_SignalHandler(int sig)
{
switch(sig)
{
case SIGTERM:
NLM_exiting = (-1);
ResumeThread(NLM_mainThreadID);
while(NLM_threadCnt != 0)
ThreadSwitchWithDelay();
break;
default:
break;
}
return;
}
/**************************************************************************
** main procedure
*/
void main(void)
{
int ccode;
LONG CBHandle=0;
++NLM_threadCnt;
/*----------------------------------------------------------------------
** No final "Press a key" message when unloading.
*/
SetAutoScreenDestructionMode(TRUE);
/*----------------------------------------------------------------------
** Disable CTRL-S and CTRL-C.
*/
SetCtrlCharCheckMode(FALSE);
/*----------------------------------------------------------------------
** Get the current thread group ID.
*/
NLM_mainThreadGroupID=GetThreadGroupID();
NLM_mainThreadID=GetThreadID();
/*----------------------------------------------------------------------
** Register SIGTERM & SIGINT handler.
*/
signal(SIGTERM, NLM_SignalHandler);
/*----------------------------------------------------------------------
** Indicate we waiting for a file to close on the server
*/
printf("Closed Files...\n");
/*----------------------------------------------------------------------
** Hook OS file system close file routine.
*/
ccode = NWAddFSMonitorHook(
/* I- callBack Number */ FSHOOK_PRE_CLOSEFILE,
/* I- callBack Function */ NLM_CloseFileEventHandler,
/* -O callBack Handle */ &CBHandle
);
if(ccode != 0)
{
printf("ERROR: NWAddFSMonitorHook() reports %d\n", ccode);
CBHandle = 0;
goto END_ERR;
}
/*----------------------------------------------------------------------
** Put Thread to sleep waiting for NLM to unload.
*/
SuspendThread(NLM_mainThreadID);
END_ERR:
NLM_exiting = (-1);
/*----------------------------------------------------------------------
** Remove the OS file system hook.
*/
if(CBHandle != 0)
NWRemoveFSMonitorHook(FSHOOK_PRE_CLOSEFILE, CBHandle);
/*----------------------------------------------------------------------
** Wait for FE handler to finish if needed.
*/
while(NLM_threadCnt > 1)
ThreadSwitchWithDelay();
--NLM_threadCnt;
return;
}