Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

92 lines
2.2 KiB

  1. /*++
  2. Copyright (c) 2000 Microsoft Corporation
  3. Module Name:
  4. CorrectOpenFileExclusive.cpp
  5. Abstract:
  6. On Win9x, opening a file for exclusive access will fail if the file is
  7. already opened. WinNT will allow the exclusive open to succeed.
  8. This shim will force CreateFile to fail exclusive open if the file is
  9. already opened.
  10. History:
  11. 11/10/2000 robkenny created
  12. --*/
  13. #include "precomp.h"
  14. IMPLEMENT_SHIM_BEGIN(CorrectOpenFileExclusive)
  15. #include "ShimHookMacro.h"
  16. APIHOOK_ENUM_BEGIN
  17. APIHOOK_ENUM_ENTRY(OpenFile )
  18. APIHOOK_ENUM_END
  19. HFILE
  20. APIHOOK(OpenFile)(
  21. LPCSTR lpFileName, // file name
  22. LPOFSTRUCT lpReOpenBuff, // file information
  23. UINT uStyle // action and attributes
  24. )
  25. {
  26. if (uStyle & OF_SHARE_EXCLUSIVE)
  27. {
  28. // We need to check to see if the file is already open.
  29. // We can do a fairly good job by attempting to open it
  30. // with read, write and execute access, which will only succeed
  31. // if all other handles to the object are have shared the file for RWE.
  32. DWORD CreateDisposition = OPEN_EXISTING;
  33. if (uStyle & OF_CREATE )
  34. {
  35. CreateDisposition = CREATE_ALWAYS;
  36. }
  37. HANDLE hFile = CreateFileA(
  38. lpFileName,
  39. GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE,
  40. 0, // No sharing allowed
  41. NULL,
  42. CreateDisposition,
  43. 0,
  44. NULL
  45. );
  46. if (hFile == INVALID_HANDLE_VALUE)
  47. {
  48. LOGN( eDbgLevelError, "Force CreateFile exclusive open to fail since file %s is already opened.", lpFileName);
  49. lpReOpenBuff->nErrCode = (WORD) GetLastError();
  50. return (HFILE)HandleToUlong(INVALID_HANDLE_VALUE);
  51. }
  52. else
  53. {
  54. CloseHandle(hFile);
  55. }
  56. }
  57. HFILE returnValue = ORIGINAL_API(OpenFile)(
  58. lpFileName, lpReOpenBuff, uStyle);
  59. return (HFILE)HandleToUlong(returnValue);
  60. }
  61. /*++
  62. Register hooked functions
  63. --*/
  64. HOOK_BEGIN
  65. APIHOOK_ENTRY(KERNEL32.DLL, OpenFile)
  66. HOOK_END
  67. IMPLEMENT_SHIM_END