The Mutex Lock component
#define _WIN32_WINNT 0x0500
#include <windows.h>
#include <sddl.h>
#include <tchar.h>
#include <stdio.h>
/* All Variables are here! */
FILE *mutexFile; // Read from file
FILE *logFile; // Log File SynchroLock.log
FILE *heartbeat; // File to output process heartbeat
CHAR stringMutex[256]; // !Prone to Overflow!
INT countMutex; // Number of Mutexes
HANDLE hMutex[1024]; // Handle of Mutexes
CHAR *removeNewLine; // Removes NewLine Char at end of gets()
INT createPass;
SYSTEMTIME HeartBeatTime; // For LocalTime information for logging heartbeat.
BOOL LogExists=FALSE;
SECURITY_ATTRIBUTES MutexAttributes; // Global var used by RegisterMyMutex functn
// Function RegisterMyMutex registers each Mutex as a globally accessible unit per computer
int RegisterMyMutex(CHAR *currentMutex[])
{
HANDLE registerMutex;
registerMutex = CreateMutex(&MutexAttributes, FALSE, currentMutex);
if (GetLastError()==ERROR_ALREADY_EXISTS) return -1;
if (registerMutex == NULL)
return 0;
return 1;
}
// End Of Function RegisterMyMutex
// Entry Point
void main()
{
const TCHAR * szSD = _T("D:") // Discretionary
// _T("(A;OICI;GRGWGX;;;AU)"); // Allow RWX to Authenticated Users
_T("(A;OICI;GA;;;AU)"); // Allow Generic Access to all authenticated users
// Initialise Mutex Security Attributes to run as Service
ZeroMemory( &MutexAttributes, sizeof(MutexAttributes) );
MutexAttributes.nLength = sizeof( MutexAttributes ); // Set SecurityAttrib structures
MutexAttributes.bInheritHandle = FALSE;
if (!ConvertStringSecurityDescriptorToSecurityDescriptor(szSD, SDDL_REVISION_1, &MutexAttributes.lpSecurityDescriptor, NULL))
printf("Failed to ConvertStringSecurityDescriptorToSecurityDescriptor\n");
//SetSecurityDescriptorDacl( &MutexAttributes, TRUE, pDacl, FALSE );
// SecurityDescriptor is now prepared
countMutex = 0; // Initialise counter mutex loaded
mutexFile = fopen("c:\\SynchroLock\\Mutex.list","r");
logFile = fopen("c:\\SynchroLock\\SynchroLock.log","a+");
if (mutexFile==NULL)
{
MessageBox(0,"Cannot Open Mutex File","Cannot Read", MB_OK);
ExitProcess(0);
}
if (logFile==NULL){
MessageBox(0,"No results will be written.","Error: Cannot open LogFile", MB_OK); // TO DO - FILE already created and running, QUIT!
fclose(mutexFile);
ExitProcess(0);
}
else LogExists=TRUE;
// Load All Mutexes in Mutex.List file
// Maximum Mutex length = 256 chars
GetLocalTime(&HeartBeatTime);
fprintf(logFile, "\n+Log file appended on %02d/%02d/%d %02d:%02d:%02d by MutexLock.\n",
HeartBeatTime.wDay,
HeartBeatTime.wMonth,
HeartBeatTime.wYear,
HeartBeatTime.wHour,
HeartBeatTime.wMinute,
HeartBeatTime.wSecond);
while(fgets(stringMutex, 256, mutexFile)!=NULL)
{
if ((removeNewLine=strchr(stringMutex,'\n'))!=NULL)
*removeNewLine='\0'; // Remove NewLine char from eos, replace with NULL
countMutex++;
// printf("%d %s",countMutex, stringMutex); // debug
// MessageBox(0,stringMutex,"Registering", MB_OK);
createPass = RegisterMyMutex(stringMutex);
if(createPass==0)
fprintf(logFile, "WARNING! %s failed to create. %d\n", stringMutex, GetLastError());
else if (createPass==-1)
fprintf(logFile, "ATTENTION! prohibited application %s may already be in use!\n" , stringMutex);
} // End While Loop
fclose(mutexFile);
fclose(logFile);
// Create a hearbeat and circulate
while (LogExists){
heartbeat = fopen("\\SynchroLock\\Heartbeat.log","a+");
GetLocalTime(&HeartBeatTime);
fprintf(heartbeat, "MutexLock %02d/%02d/%d %02d:%02d:%02d \n",
HeartBeatTime.wDay,
HeartBeatTime.wMonth,
HeartBeatTime.wYear,
HeartBeatTime.wHour,
HeartBeatTime.wMinute,
HeartBeatTime.wSecond);
fclose(heartbeat);
Sleep(360000); // sleep 60 seconds
}
}
No comments:
Post a Comment