1 /* $Id: session.c,v 1.4 2003/12/21 20:11:46 ea Exp $
3 * PROJECT : ReactOS / POSIX+ Environment Subsystem Server
4 * FILE : reactos/subsys/psx/server/ob/session.c
5 * DESCRIPTION: terminal
7 * AUTHOR : Emanuele Aliberti <eal@users.sf.net>
9 * --------------------------------------------------------------------
11 * This software is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License as
13 * published by the Free Software Foundation; either version 2 of the
14 * License, or (at your option) any later version.
16 * This software is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this software; see the file COPYING. If not, write
23 * to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge,
26 * --------------------------------------------------------------------
30 #define LOCK_ALL_SESSIONS RtlEnterCriticalSection(& Sessions.Lock)
31 #define UNLOCK_ALL_SESSIONS RtlLeaveCriticalSection(& Sessions.Lock)
32 #define LOCK_THIS_SESSION RtlEnterCriticalSection(& Session->Lock)
33 #define UNLOCK_THIS_SESSION RtlLeaveCriticalSection(& Session->Lock)
36 /* A double-linked list for the PSX_SESSION instances */
43 CRITICAL_SECTION Lock
;
47 /**** FUNCTIONS ******************************************************/
49 /**********************************************************************
50 * PsxInitializeSessions/0
53 * Initialize the PSX session manager.
60 PsxInitializeSessions (VOID
)
62 debug_print (L
"PSXSS: ->%s", __FUNCTION__
);
63 /* Initalize the attributes */
64 Sessions
.NextFreeId
= 0;
67 RtlInitializeCriticalSection (& Sessions
.Lock
);
68 return STATUS_SUCCESS
;
70 /**********************************************************************
71 * PsxCreateSessionObjects/1 PRIVATE
74 PRIVATE NTSTATUS STDCALL
75 PsxCreateSessionObjects (
76 IN PLPC_MAX_MESSAGE pRequest
,
77 IN OUT PPSX_SESSION pSession
82 WCHAR NameBuffer
[NAME_BUFFER_SIZE
];
84 SECURITY_QUALITY_OF_SERVICE Sqos
;
85 PSX_CONNECT_PORT_DATA ConnectData
;
86 ULONG ConnectDataSize
= sizeof ConnectData
;
91 PSX_NS_SESSION_PORT_TEMPLATE
,
92 PSX_NS_SUBSYSTEM_DIRECTORY_NAME
,
93 PSX_NS_SESSION_DIRECTORY_NAME
,
94 pRequest
->Header
.ClientId
.UniqueProcess
96 debug_print (L
"PSXSS: %s: %s", __FUNCTION__
, NameBuffer
);
97 RtlInitUnicodeString (& Name
, NameBuffer
);
98 InitializeObjectAttributes (& Oa
, & Name
, 0, NULL
, NULL
);
99 RtlZeroMemory (& Sqos
, sizeof Sqos
);
100 ConnectData
.ConnectionType
= PSX_CONNECTION_TYPE_SERVER
;
101 ConnectData
.Version
= PSX_LPC_PROTOCOL_VERSION
;
102 ConnectData
.PortIdentifier
= 0;
103 Status
= NtConnectPort (
104 & pSession
->TerminalChannel
.hPort
,
110 (PVOID
) & ConnectData
,
113 if (!NT_SUCCESS(Status
))
115 debug_print (L
"PSXSS: %s: NtConnectPort failed with %08x\n", __FUNCTION__
, Status
);
122 PSX_NS_SESSION_DATA_TEMPLATE
,
123 PSX_NS_SUBSYSTEM_DIRECTORY_NAME
,
124 PSX_NS_SESSION_DIRECTORY_NAME
,
125 pRequest
->Header
.ClientId
.UniqueProcess
127 debug_print (L
"PSXSS: : %s", __FUNCTION__
, NameBuffer
);
128 RtlInitUnicodeString (& Name
, NameBuffer
);
129 InitializeObjectAttributes (& Oa
, & Name
, 0, 0, 0);
130 Status
= NtOpenSection (
131 & pSession
->TerminalChannel
.Section
.Handle
,
132 SECTION_ALL_ACCESS
, /* DesiredAccess */
135 if (!NT_SUCCESS(Status
))
137 NtClose (pSession
->TerminalChannel
.hPort
);
138 debug_print (L
"PSXSS: %s: NtOpenSection failed with %08x\n", __FUNCTION__
, Status
);
141 pSession
->TerminalChannel
.Section
.BaseAddress
= NULL
;
142 pSession
->TerminalChannel
.Section
.ViewSize
= PSX_TERMINAL_SECTION_SIZE
;
143 Status
= NtMapViewOfSection (
144 pSession
->TerminalChannel
.Section
.Handle
,
146 & pSession
->TerminalChannel
.Section
.BaseAddress
,
149 0, /* SectionOffset */
150 & pSession
->TerminalChannel
.Section
.ViewSize
,
152 0, /* AllocationType */
153 PAGE_READWRITE
/* Protect 4 */
155 if (!NT_SUCCESS(Status
))
157 NtClose (pSession
->TerminalChannel
.hPort
);
158 NtClose (pSession
->TerminalChannel
.Section
.Handle
);
159 debug_print (L
"PSXSS: %s: NtMapViewOfSection failed with %08x\n", __FUNCTION__
, Status
);
164 /**********************************************************************
168 * Create a new PSX_SESSION object and insert it in the
169 * PSX sessions table.
174 * A status value on error; otherwise STATUS_SUCCESS.
178 IN PLPC_MAX_MESSAGE pRequest
,
179 IN HANDLE hConnectedPort
,
180 IN ULONG ulPortIdentifier
183 PPSX_SESSION Session
= NULL
;
185 debug_print (L
"PSXSS: ->%s", __FUNCTION__
);
186 /* Create the PSX_SESSION object */
187 Session
= RtlAllocateHeap (Server
.Heap
, 0, sizeof (PSX_SESSION
));
190 debug_print (L
"PSXSS: %s: failed to create a new session object", __FUNCTION__
);
191 return STATUS_MEMORY_NOT_ALLOCATED
;
193 RtlZeroMemory (Session
, sizeof (PSX_SESSION
));
194 /* Initialiaze the new PSX_SESSION object */
195 Session
->SessionChannel
.hPort
= hConnectedPort
;
196 Session
->SessionChannel
.ulPortIdentifier
= ulPortIdentifier
;
198 Session
->Id
= Sessions
.NextFreeId
++;
200 Session
->Status
= SESSION_STATUS_INITIALIZATION
;
210 if (INVALID_HANDLE_VALUE
== Session
->Heap
)
212 RtlFreeHeap (Server
.Heap
, 0, Session
);
213 debug_print (L
"PSX: %s: failed to create a new heap for session # %d", __FUNCTION__
, Session
->Id
);
214 return STATUS_MEMORY_NOT_ALLOCATED
;
216 RtlInitializeCriticalSection (& Session
->Lock
);
217 /* TODO: open the terminal's shared section */
218 /* TODO: connect to the terminal's port */
219 /* Inset the new PSX_SESSION object in the sessions table */
221 if (NULL
== Sessions
.List
)
223 Sessions
.List
= Session
;
224 Session
->Previous
= Session
;
226 Session
->Next
= Sessions
.List
; /* Last one points to the top one */
227 Session
->Previous
= Sessions
.List
->Previous
;
228 Sessions
.List
->Previous
= Session
; /* Top one now points to the new one (tail) */
232 debug_print (L
"%s: session # %d created", __FUNCTION__
, Session
->Id
);
233 Session
->Status
= SESSION_STATUS_READY
;
234 return STATUS_SUCCESS
;
236 /**********************************************************************
237 * PsxTerminateSession/1
240 * Remove a PSX_SESSION object from the PSX sessions table and
246 * A status value on error; otherwise STATUS_SUCCESS.
249 PsxTerminateSession (
250 IN PPSX_SESSION Session
254 PPSX_SESSION Previous
= NULL
;
255 PPSX_SESSION Next
= NULL
;
257 /* Release any resource managed by the session */
258 RtlDestroyHeap (Session
->Heap
);
259 /* Remove the session object from the sessions table */
262 Previous
= Session
->Previous
;
263 Next
= Session
->Next
;
264 /* TODO: handle the case of no session left */
265 Next
->Previous
= Previous
;
266 Previous
->Next
= Next
;
269 /* Delete the old PSX_SESSION object */
270 RtlFreeHeap (Server
.Heap
, 0, Session
);
272 debug_print(L
"PSX: session # %d deleted", Id
);
273 return STATUS_SUCCESS
;
277 PsxWriteTerminalSession (
278 IN PPSX_SESSION Session
,
281 IN OUT PULONG Written
285 /* TODO: lock this session's section for writing */
286 /* TODO: copy the data in this session's section */
287 /* TODO: request a WRITE operation to the session terminal */
288 /* TODO: unlock this session's section */
293 PsxReadTerminalSession (
294 IN PPSX_SESSION Session
,
301 /* TODO: lock this session's section for reading */
302 /* TODO: request a READ operation to the session terminal */
303 /* TODO: copy the data from this session's section */
304 /* TODO: unlock this session's section */