int RunTest(char* Helper, HANDLE TheFile, HANDLE WaitFile) { STARTUPINFO si; PROCESS_INFORMATION pi; DWORD ChildRetCode = 0; DWORD ParentRetCode = 0; DWORD BytesRead; char DataBuffer[BUF_SIZE]; ZeroMemory( &si, sizeof(si) ); si.cb = sizeof(si); ZeroMemory( &pi, sizeof(pi) ); /* Load up the helper Process, and then Wait until it signals that it is finished locking. */ if(!CreateProcess( NULL,Helper,NULL,NULL,FALSE,0,NULL,NULL,&si,&pi)) { Fail("ERROR: CreateProcess failed to load executable '%s'.",Helper); } SignalAndBusyWait(WaitFile); /* Now the child proccess has locked another section of the file, from bytes 11 through 20. Let's check that the parent lock is still ignored by the parent proccess and that the child's lock is respected. */ if(ReadFile(TheFile, DataBuffer, 10, &BytesRead, NULL) == 0) { Trace("ERROR: ReadFile failed when attempting to read a section of " "the file which was locked by the current process. It should " "have been able to read this. GetLastError() returned %d.", GetLastError()); ParentRetCode = 1; } SetFilePointer(TheFile, 11, 0, FILE_BEGIN); if(ReadFile(TheFile, DataBuffer, 10, &BytesRead, NULL) != 0) { Trace("ERROR: ReadFile returned success when it should " "have failed. Attempted to read 10 bytes of the file which " "were locked by the child."); ParentRetCode = 1; } /* We're finished testing. Let the child proccess know so it can clean up, and the parent will wait until it is done. */ SignalFinish(WaitFile); WaitForSingleObject(pi.hProcess,INFINITE); /* Get the return value from the helper process */ if (GetExitCodeProcess(pi.hProcess, &ChildRetCode) == 0) { Fail("ERROR: GetExitCodeProccess failed when attempting to retrieve " "the exit code of the child process."); } if(CloseHandle( pi.hProcess ) == 0) { Fail("ERROR: CloseHandle failed to close the process."); } if(CloseHandle( pi.hThread ) == 0) { Fail("ERROR: CloseHandle failed to close the thread."); } return (ChildRetCode || ParentRetCode); }
int __cdecl main(int argc, char *argv[]) { HANDLE TheFile, WaitFile; int result = 0; char DataBuffer[BUF_SIZE]; DWORD BytesRead; if(0 != (PAL_Initialize(argc, argv))) { return FAIL; } /* Open the same file that the parent has opened and locked */ TheFile = CreateFile(FILENAME, GENERIC_READ|GENERIC_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); if (TheFile == INVALID_HANDLE_VALUE) { Trace("ERROR: Could not open file '%s' with CreateFile.\n",FILENAME); result = 1; } /* Open up the WaitFile that we're using for IPC */ WaitFile = CreateFile(WAITFILENAME, GENERIC_READ|GENERIC_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); if (WaitFile == INVALID_HANDLE_VALUE) { Trace("ERROR: Could not open file '%s' with CreateFile. " "GetLastError() returned %d.\n",WAITFILENAME,GetLastError()); result = 1; } /* Check to ensure the parent lock is respected */ if(ReadFile(TheFile, DataBuffer, 10, &BytesRead, NULL) != 0) { Trace("ERROR: ReadFile returned success when it should " "have failed. Attempted to read the first 10 bytes " "of a file which was locked by the parent process.\n"); result = 1; } // Sleep for a bit to give the parent a chance to block before we do. Sleep(1000); /* Switch back to the parent, so it can unlock the file */ SignalAndBusyWait(WaitFile); if(ReadFile(TheFile, DataBuffer, 10, &BytesRead, NULL) == 0) { Trace("ERROR: ReadFile was unable to read from the file after it " "had been unlocked. Attempted to read 10 bytes and ReadFile " "returned 0. GetLastError() returned %d.\n",GetLastError()); result = 1; } PAL_TerminateEx(result); return result; }
int RunTest(char* Helper, HANDLE TheFile, HANDLE WaitFile) { STARTUPINFO si; PROCESS_INFORMATION pi; DWORD ChildRetCode = 0; DWORD ParentRetCode = 0; ZeroMemory( &si, sizeof(si) ); si.cb = sizeof(si); ZeroMemory( &pi, sizeof(pi) ); /* Load up the helper Process, and then Wait until it signals that it is finished locking. */ if(!CreateProcess( NULL, Helper, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi)) { Fail("ERROR: CreateProcess failed to load executable '%s'.\n",Helper); } SignalAndBusyWait(WaitFile); /* When the child proccess is finished setting its lock and testing the parent lock, then the parent can test the child's lock. */ if(UnlockFile(TheFile, 10, 0, 10, 0) != 0) { Trace("ERROR: The parent proccess called Unlock on the child " "proccesses lock, and the function returned non-zero, when " "it should have failed.\n"); ParentRetCode = 1; } /* Switch back to the child so that it can unlock its portion and cleanup. */ SignalFinish(WaitFile); WaitForSingleObject(pi.hProcess,INFINITE); /* Get the return value from the helper process */ if (GetExitCodeProcess(pi.hProcess, &ChildRetCode) == 0) { Fail("ERROR: GetExitCodeProccess failed when attempting to retrieve " "the exit code of the child process.\n"); } if(CloseHandle( pi.hProcess ) == 0) { Fail("ERROR: CloseHandle failed to close the process.\n"); } if(CloseHandle( pi.hThread ) == 0) { Fail("ERROR: CloseHandle failed to close the thread.\n"); } return (ChildRetCode || ParentRetCode); }
int __cdecl main(int argc, char *argv[]) { HANDLE TheFile, WaitFile; int result = 0; if(0 != (PAL_Initialize(argc, argv))) { return FAIL; } /* Open the same file that the parent has opened and locked */ TheFile = CreateFile(FILENAME, GENERIC_READ|GENERIC_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); if (TheFile == INVALID_HANDLE_VALUE) { Trace("ERROR: Could not open file '%s' with CreateFile.\n",FILENAME); result = 1; } /* Open up the WaitFile that we're using for IPC */ WaitFile = CreateFile(WAITFILENAME, GENERIC_READ|GENERIC_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); if (WaitFile == INVALID_HANDLE_VALUE) { Trace("ERROR: Could not open file '%s' with CreateFile. " "GetLastError() returned %d.\n",WAITFILENAME,GetLastError()); result = 1; } /* Lock a section of the file different from which was locked in the parent proccess */ if(LockFile(TheFile, 10, 0, 10, 0) == 0) { Trace("ERROR: The LockFile call within the child failed to lock " "the file. GetLastError() returned %d.\n",GetLastError()); result = 1; } /* Attempt to unlock the portion of the file which was locked within the parent process. */ if(UnlockFile(TheFile, 0, 0, 10, 0) != 0) { Trace("ERROR: The UnlockFile call within the child succeeded in " "calling UnlockFile on the portion of the file which was " "locked by the parent.\n"); result = 1; } // Sleep for a bit to give the parent a chance to block before we do. Sleep(1000); /* Switch back to the parent, so it can check the child lock */ SignalAndBusyWait(WaitFile); /* Finally, clean up the lock which was done within this proccess and exit. */ if(UnlockFile(TheFile, 10, 0, 10, 0) == 0) { Trace("ERROR: The UnlockFile call within the child failed to unlock " "the portion of the file which was locked by the child. " "GetLastError() returned %d.\n", GetLastError()); result = 1; } PAL_TerminateEx(result); return result; }
int __cdecl main(int argc, char *argv[]) { HANDLE TheFile, WaitFile; int result = 0; char DataBuffer[BUF_SIZE]; DWORD BytesRead; if(0 != (PAL_Initialize(argc, argv))) { return FAIL; } /* Open the same file that the parent has opened and locked */ TheFile = CreateFile(FILENAME, GENERIC_READ|GENERIC_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); if (TheFile == INVALID_HANDLE_VALUE) { Trace("ERROR: Could not open file '%s' with CreateFile.",FILENAME); result = 1; } /* Open up the WaitFile that we're using for IPC */ WaitFile = CreateFile(WAITFILENAME, GENERIC_READ|GENERIC_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); if (WaitFile == INVALID_HANDLE_VALUE) { Trace("ERROR: Could not open file '%s' with CreateFile. " "GetLastError() returned %d.",WAITFILENAME,GetLastError()); result = 1; } /* Lock the same file that the parent process locked, but the child locks bytes 11 through 20 */ if(LockFile(TheFile, 11, 0, 10, 0) == 0) { Trace("ERROR: LockFile failed in the child proccess. " "GetLastError returns %d.", GetLastError()); result = 1; } /* Check to ensure the parent lock is respected */ if(ReadFile(TheFile, DataBuffer, 10, &BytesRead, NULL) != 0) { Trace("ERROR: ReadFile returned success when it should " "have failed. Attempted to read the first 10 bytes " "of a file which was locked by the parent process."); result = 1; } /* Check to ensure the lock put on by this proccess doesn't restrict access */ if(SetFilePointer(TheFile, 11, 0, FILE_BEGIN) == INVALID_SET_FILE_POINTER) { Trace("ERROR: SetFilePointer was unable to move the file pointer to " "the 11th byte in the file, within the child proccess. " "GetLastError() returned %d.",GetLastError()); result = 1; } if(ReadFile(TheFile, DataBuffer, 10, &BytesRead, NULL) == 0) { Trace("ERROR: ReadFile failed when attempting to read a section of " "the file which was locked by the current process. It should " "have been able to read this. GetLastError() returned %d.", GetLastError()); result = 1; } // Sleep for a bit to give the parent a chance to block before we do. Sleep(1000); /* Switch back to the parent, so it can check the child's locks */ SignalAndBusyWait(WaitFile); if(UnlockFile(TheFile, 11, 0, 10, 0) == 0) { Fail("ERROR: Failed to Unlock bytes 11-20 in the file. " "GetLastError returned %d.",GetLastError()); } PAL_Terminate(); return result; }