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.

174 lines
4.1 KiB

  1. /*++
  2. Copyright (c) 1985 - 1999, Microsoft Corporation
  3. Module Name:
  4. share.c
  5. Abstract:
  6. Implements sharing for input and output handles
  7. Author:
  8. Therese Stowell (thereses) 11-Nov-1990
  9. Revision History:
  10. --*/
  11. #include "precomp.h"
  12. #pragma hdrstop
  13. NTSTATUS
  14. ConsoleAddShare(
  15. IN ACCESS_MASK DesiredAccess,
  16. IN ULONG DesiredShareAccess,
  17. IN OUT PCONSOLE_SHARE_ACCESS ShareAccess,
  18. IN OUT PHANDLE_DATA HandleData
  19. )
  20. {
  21. ULONG Ocount;
  22. ULONG ReadAccess;
  23. ULONG WriteAccess;
  24. ULONG SharedRead;
  25. ULONG SharedWrite;
  26. //
  27. // Set the access type in the file object for the current accessor.
  28. //
  29. ReadAccess = (DesiredAccess & GENERIC_READ) != 0;
  30. WriteAccess = (DesiredAccess & GENERIC_WRITE) != 0;
  31. SharedRead = (DesiredShareAccess & FILE_SHARE_READ) != 0;
  32. SharedWrite = (DesiredShareAccess & FILE_SHARE_WRITE) != 0;
  33. //
  34. // Now check to see whether or not the desired accesses are compatible
  35. // with the way that the file is currently open.
  36. //
  37. Ocount = ShareAccess->OpenCount;
  38. if ( (ReadAccess && (ShareAccess->SharedRead < Ocount))
  39. ||
  40. (WriteAccess && (ShareAccess->SharedWrite < Ocount))
  41. ||
  42. ((ShareAccess->Readers != 0) && !SharedRead)
  43. ||
  44. ((ShareAccess->Writers != 0) && !SharedWrite)
  45. ) {
  46. //
  47. // The check failed. Simply return to the caller indicating that the
  48. // current open cannot access the file.
  49. //
  50. return STATUS_SHARING_VIOLATION;
  51. } else {
  52. //
  53. // The check was successful. Update the counter information in the
  54. // shared access structure for this open request if the caller
  55. // specified that it should be updated.
  56. //
  57. ShareAccess->OpenCount++;
  58. ShareAccess->Readers += ReadAccess;
  59. ShareAccess->Writers += WriteAccess;
  60. ShareAccess->SharedRead += SharedRead;
  61. ShareAccess->SharedWrite += SharedWrite;
  62. HandleData->Access = DesiredAccess;
  63. HandleData->ShareAccess = DesiredShareAccess;
  64. return STATUS_SUCCESS;
  65. }
  66. }
  67. NTSTATUS
  68. ConsoleDupShare(
  69. IN ACCESS_MASK DesiredAccess,
  70. IN ULONG DesiredShareAccess,
  71. IN OUT PCONSOLE_SHARE_ACCESS ShareAccess,
  72. IN OUT PHANDLE_DATA TargetHandleData
  73. )
  74. {
  75. ULONG ReadAccess;
  76. ULONG WriteAccess;
  77. ULONG SharedRead;
  78. ULONG SharedWrite;
  79. //
  80. // Set the access type in the file object for the current accessor.
  81. //
  82. ReadAccess = (DesiredAccess & GENERIC_READ) != 0;
  83. WriteAccess = (DesiredAccess & GENERIC_WRITE) != 0;
  84. SharedRead = (DesiredShareAccess & FILE_SHARE_READ) != 0;
  85. SharedWrite = (DesiredShareAccess & FILE_SHARE_WRITE) != 0;
  86. if (ShareAccess->OpenCount == 0) {
  87. ASSERT (FALSE);
  88. return STATUS_SHARING_VIOLATION;
  89. }
  90. ShareAccess->OpenCount++;
  91. ShareAccess->Readers += ReadAccess;
  92. ShareAccess->Writers += WriteAccess;
  93. ShareAccess->SharedRead += SharedRead;
  94. ShareAccess->SharedWrite += SharedWrite;
  95. TargetHandleData->Access = DesiredAccess;
  96. TargetHandleData->ShareAccess = DesiredShareAccess;
  97. return STATUS_SUCCESS;
  98. }
  99. NTSTATUS
  100. ConsoleRemoveShare(
  101. IN ULONG DesiredAccess,
  102. IN ULONG DesiredShareAccess,
  103. IN OUT PCONSOLE_SHARE_ACCESS ShareAccess
  104. )
  105. {
  106. ULONG ReadAccess;
  107. ULONG WriteAccess;
  108. ULONG SharedRead;
  109. ULONG SharedWrite;
  110. //
  111. // Set the access type in the file object for the current accessor.
  112. //
  113. ReadAccess = (DesiredAccess & GENERIC_READ) != 0;
  114. WriteAccess = (DesiredAccess & GENERIC_WRITE) != 0;
  115. SharedRead = (DesiredShareAccess & FILE_SHARE_READ) != 0;
  116. SharedWrite = (DesiredShareAccess & FILE_SHARE_WRITE) != 0;
  117. if (ShareAccess->OpenCount == 0) {
  118. ASSERT (FALSE);
  119. return STATUS_UNSUCCESSFUL;
  120. }
  121. ShareAccess->OpenCount--;
  122. ShareAccess->Readers -= ReadAccess;
  123. ShareAccess->Writers -= WriteAccess;
  124. ShareAccess->SharedRead -= SharedRead;
  125. ShareAccess->SharedWrite -= SharedWrite;
  126. return STATUS_SUCCESS;
  127. }