summaryrefslogtreecommitdiffstats
path: root/private/sm/server/smsesnid.c
blob: 24fe463b39324ee2eb6d3a770c8d0fff4c00f578 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
/*++

Copyright (c) 1989  Microsoft Corporation

Module Name:

    smsesnid.c

Abstract:

    Session Manager Session ID Management

Author:

    Mark Lucovsky (markl) 04-Oct-1989

Revision History:

--*/

#include "smsrvp.h"
#include <string.h>


ULONG
SmpAllocateSessionId(
    IN PSMPKNOWNSUBSYS OwningSubsystem,
    IN PSMPKNOWNSUBSYS CreatorSubsystem OPTIONAL
    )

/*++

Routine Description:

    This function allocates a session id.

Arguments:

    OwningSubsystem - Supplies the address of the subsystem that should
        become the owner of this session.


    CreatorSubsystem - An optional parameter that if supplied supplies
        the address of the subsystem requesting the creation of this
        session.  This subsystem is notified when the session completes.

Return Value:

    This function returns the session id for this session.

--*/

{

    ULONG SessionId;
    PLIST_ENTRY SessionIdListInsertPoint;
    PSMPSESSION Session;

    RtlEnterCriticalSection(&SmpSessionListLock);

    //
    // SessionId's are allocated by incrementing a 32 bit counter.
    // If the counter wraps, then session id's are allocated by
    // scaning the sorted list of current session id's for a hole.
    //

    SessionId = SmpNextSessionId++;
    SessionIdListInsertPoint = SmpSessionListHead.Blink;

    if ( !SmpNextSessionIdScanMode ) {

        if ( SmpNextSessionId == 0 ) {

            //
            // We have used up 32 bits worth of session id's so
            // enable scan mode session id allocation.
            //

            SmpNextSessionIdScanMode = TRUE;
        }

    } else {

        //
        // Compute a session id by scanning the sorted session id list
        // until a  whole is found. When an id is found, then save it,
        // and re-calculate the inster point.
        //

        DbgPrint("SMSS: SessionId's Wraped\n");
        DbgBreakPoint();

    }

    Session = RtlAllocateHeap(SmpHeap, MAKE_TAG( SM_TAG ), sizeof(SMPSESSION));

    Session->SessionId = SessionId;
    Session->OwningSubsystem = OwningSubsystem;
    Session->CreatorSubsystem = CreatorSubsystem;

    InsertTailList(SessionIdListInsertPoint,&Session->SortedSessionIdListLinks);

    RtlLeaveCriticalSection(&SmpSessionListLock);

    return SessionId;
}


PSMPSESSION
SmpSessionIdToSession(
    IN ULONG SessionId
    )

/*++

Routine Description:

    This function locates the session structure for the specified
    session id.

    It is assumed that the caller holds the session list lock.

Arguments:

    SessionId - Supplies the session id whose session structure
        located

Return Value:

    NULL - No session matches the specified session

    NON-NULL - Returns a pointer to the session structure associated with
        the specified session id.

--*/

{

    PLIST_ENTRY Next;
    PSMPSESSION Session;

    Next = SmpSessionListHead.Flink;
    while ( Next != &SmpSessionListHead ) {
        Session = CONTAINING_RECORD(Next, SMPSESSION, SortedSessionIdListLinks );

        if ( Session->SessionId == SessionId ) {
            return Session;
        }
        Next = Session->SortedSessionIdListLinks.Flink;
    }

    return NULL;
}


VOID
SmpDeleteSession(
    IN ULONG SessionId,
    IN BOOLEAN SendSessionComplete,
    IN NTSTATUS SessionStatus
    )

/*++

Routine Description:

    This function locates and deletes a session id.  If the
    SendSessionComplete flag is true, then it also sends a session
    complete message to the creator subsystem.

Arguments:

    SessionId - Supplies the session id to delete.

    SendSessionComplete - Specifies whether a session complete message
        is to be sent to the creator subsystem (if one exists).

    SessionStatus - Supplies the session completion status


Return Value:


--*/

{

    PSMPSESSION Session;
    PSMPKNOWNSUBSYS CreatorSubsystem;

    RtlEnterCriticalSection(&SmpSessionListLock);

    Session = SmpSessionIdToSession(SessionId);

    if ( Session ) {

        RemoveEntryList(&Session->SortedSessionIdListLinks);


        RtlLeaveCriticalSection(&SmpSessionListLock);

        CreatorSubsystem = Session->CreatorSubsystem;

        RtlFreeHeap(SmpHeap,0,Session);

        //
        // If there is a creator subsystem, and if
        // told to send a session complete message, then do it.
        //

        if ( CreatorSubsystem && SendSessionComplete ) {

            //
            // Foreign Session Complete
            //
        }

    } else {

        RtlLeaveCriticalSection(&SmpSessionListLock);
    }

    return;


    //
    // Make the compiler happy
    //

    SessionStatus;
}