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.

202 lines
4.0 KiB

  1. /*++
  2. Copyright (c) 1989 Microsoft Corporation
  3. Module Name:
  4. smsesnid.c
  5. Abstract:
  6. Session Manager Session ID Management
  7. Author:
  8. Mark Lucovsky (markl) 04-Oct-1989
  9. Revision History:
  10. --*/
  11. #include "smsrvp.h"
  12. #include <string.h>
  13. ULONG
  14. SmpAllocateSessionId(
  15. IN PSMPKNOWNSUBSYS OwningSubsystem,
  16. IN PSMPKNOWNSUBSYS CreatorSubsystem OPTIONAL
  17. )
  18. /*++
  19. Routine Description:
  20. This function allocates a session ID.
  21. Arguments:
  22. OwningSubsystem - Supplies the address of the subsystem that should
  23. become the owner of this session.
  24. CreatorSubsystem - An optional parameter that supplies
  25. the address of the subsystem requesting the creation of this
  26. session. This subsystem is notified when the session completes.
  27. Return Value:
  28. This function returns the session ID for this session.
  29. --*/
  30. {
  31. ULONG SessionId;
  32. PLIST_ENTRY SessionIdListInsertPoint;
  33. PSMPSESSION Session;
  34. RtlEnterCriticalSection(&SmpSessionListLock);
  35. //
  36. // SessionIds are allocated by incrementing a 32 bit counter.
  37. // If the counter wraps, then session IDs are allocated by
  38. // scanning the sorted list of current session IDs for a hole.
  39. //
  40. SessionId = SmpNextSessionId++;
  41. SessionIdListInsertPoint = SmpSessionListHead.Blink;
  42. if ( !SmpNextSessionIdScanMode ) {
  43. if ( SmpNextSessionId == 0 ) {
  44. //
  45. // We have used up 32 bits worth of session IDs so
  46. // enable scan mode session ID allocation.
  47. //
  48. SmpNextSessionIdScanMode = TRUE;
  49. }
  50. } else {
  51. //
  52. // Compute a session ID by scanning the sorted session ID list
  53. // until a hole is found. When an ID is found, then save it,
  54. // and recalculate the insert point.
  55. //
  56. #if DBG
  57. DbgPrint("SMSS: SessionId's Wrapped\n");
  58. DbgBreakPoint();
  59. #endif
  60. }
  61. Session = RtlAllocateHeap(SmpHeap, MAKE_TAG( SM_TAG ), sizeof(SMPSESSION));
  62. if (Session) {
  63. Session->SessionId = SessionId;
  64. Session->OwningSubsystem = OwningSubsystem;
  65. Session->CreatorSubsystem = CreatorSubsystem;
  66. InsertTailList(SessionIdListInsertPoint,&Session->SortedSessionIdListLinks);
  67. } else {
  68. DbgPrint("SMSS: Unable to keep track of session ID -- no memory available\n");
  69. }
  70. RtlLeaveCriticalSection(&SmpSessionListLock);
  71. return SessionId;
  72. }
  73. PSMPSESSION
  74. SmpSessionIdToSession(
  75. IN ULONG SessionId
  76. )
  77. /*++
  78. Routine Description:
  79. This function locates the session structure for the specified
  80. session ID.
  81. It is assumed that the caller holds the session list lock.
  82. Arguments:
  83. SessionId - Supplies the session ID to locate the structure for.
  84. Return Value:
  85. NULL - No session matches the specified session.
  86. NON-NULL - Returns a pointer to the session structure associated with
  87. the specified session ID.
  88. --*/
  89. {
  90. PLIST_ENTRY Next;
  91. PSMPSESSION Session;
  92. Next = SmpSessionListHead.Flink;
  93. while ( Next != &SmpSessionListHead ) {
  94. Session = CONTAINING_RECORD(Next, SMPSESSION, SortedSessionIdListLinks );
  95. if ( Session->SessionId == SessionId ) {
  96. return Session;
  97. }
  98. Next = Session->SortedSessionIdListLinks.Flink;
  99. }
  100. return NULL;
  101. }
  102. VOID
  103. SmpDeleteSession(
  104. IN ULONG SessionId
  105. )
  106. /*++
  107. Routine Description:
  108. This function locates and deletes a session ID.
  109. Arguments:
  110. SessionId - Supplies the session ID to delete.
  111. Return Value:
  112. None.
  113. --*/
  114. {
  115. PSMPSESSION Session;
  116. RtlEnterCriticalSection(&SmpSessionListLock);
  117. Session = SmpSessionIdToSession(SessionId);
  118. if ( Session ) {
  119. RemoveEntryList(&Session->SortedSessionIdListLinks);
  120. RtlLeaveCriticalSection(&SmpSessionListLock);
  121. RtlFreeHeap(SmpHeap,0,Session);
  122. } else {
  123. RtlLeaveCriticalSection(&SmpSessionListLock);
  124. }
  125. return;
  126. }