Win32 utility to bootstrap the PSX subsystem (it is an optional subsystem, therefore...
[reactos.git] / posix / server / port / session.c
1 /* $Id: session.c,v 1.4 2003/12/21 20:11:46 ea Exp $
2 *
3 * PROJECT : ReactOS / POSIX+ Environment Subsystem Server
4 * FILE : reactos/subsys/psx/server/port/session.c
5 * DESCRIPTION: \POSIX+\SessionPort LPC port logic.
6 * DATE : 2002-04-04
7 * AUTHOR : Emanuele Aliberti <eal@users.sf.net>
8 *
9 * --------------------------------------------------------------------
10 *
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.
15 *
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.
20 *
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,
24 * MA 02139, USA.
25 *
26 * --------------------------------------------------------------------
27 */
28 #include <psxss.h>
29 #include <psx/lpcproto.h>
30 #include "utils.h"
31
32 /**********************************************************************
33 * ProcessConnectionRequest/ PRIVATE
34 */
35 PRIVATE NTSTATUS STDCALL
36 ProcessConnectionRequest (PLPC_MAX_MESSAGE pRequest)
37 {
38 PPSX_CONNECT_PORT_DATA pConnectData = (PPSX_CONNECT_PORT_DATA) & pRequest->Data;
39 NTSTATUS Status;
40 HANDLE hConnectedPort;
41 ULONG ulPortIdentifier;
42
43 debug_print (L"PSXSS: ->%s", TEXT(__FUNCTION__));
44
45 /* Check if the caller is a terminal */
46 Status = PsxCheckConnectionRequest (
47 pConnectData,
48 PSX_CONNECTION_TYPE_TERMINAL
49 );
50 if (!NT_SUCCESS(Status))
51 {
52 Status = NtAcceptConnectPort (
53 & hConnectedPort,
54 NULL,
55 & pRequest->Header,
56 FALSE, /* reject connection request */
57 NULL,
58 NULL
59 );
60 if (!NT_SUCCESS(Status))
61 {
62 debug_print(
63 L"PSXSS: %s: NtAcceptConnectPort failed with status=%08x",
64 TEXT(__FUNCTION__),
65 Status
66 );
67 }
68 return STATUS_UNSUCCESSFUL;
69 }
70 /* OK, accept the connection */
71 Status = NtAcceptConnectPort (
72 & hConnectedPort,
73 & ulPortIdentifier,
74 & pRequest->Header,
75 TRUE, /* accept connection request */
76 NULL,
77 NULL
78 );
79 if (!NT_SUCCESS(Status))
80 {
81 debug_print(L"PSXSS: %s: NtAcceptConnectPort failed with status=%08x", TEXT(__FUNCTION__), Status);
82 return Status;
83 }
84 /* OK, now create a new PSX_SESSION object */
85 Status = PsxCreateSession (
86 pRequest,
87 hConnectedPort,
88 ulPortIdentifier
89 );
90 if (!NT_SUCCESS(Status))
91 {
92 debug_print(L"PSXSS: %s: PsxCreateSession failed with status=%08x", TEXT(__FUNCTION__), Status);
93 return Status;
94 }
95 Status = NtCompleteConnectPort (hConnectedPort);
96 if (!NT_SUCCESS(Status))
97 {
98 debug_print(L"PSXSS: %s: NtCompleteConnectPort failed with status=%08x", TEXT(__FUNCTION__), Status);
99 return Status;
100 }
101 debug_print (L"PSXSS: <-%s", TEXT(__FUNCTION__));
102 return STATUS_SUCCESS;
103 }
104 /**********************************************************************
105 * ProcessRequest/ PRIVATE
106 */
107 PRIVATE NTSTATUS STDCALL
108 ProcessRequest (PPSX_MAX_MESSAGE pRequest)
109 {
110 debug_print (L"PSXSS: ->%s", TEXT(__FUNCTION__));
111 /* TODO: Read data from the section */
112 /* TODO: send data to the process */
113 debug_print (L"PSXSS: <-%s", TEXT(__FUNCTION__));
114 return STATUS_NOT_IMPLEMENTED;
115 }
116 /**********************************************************************
117 * SessionPortListener/1
118 *
119 * DESCRIPTION
120 * Listen on port \POSIX+\SessionPort and create new sessions
121 * when a new terminal emulator calls.
122 *
123 * ARGUMENTS
124 * \POSIX+\SessionPort handle.
125 *
126 * RETURN VALUE
127 * None.
128 */
129 VOID STDCALL
130 SessionPortListener (PVOID pArg)
131 {
132 ULONG ulIndex = (ULONG) pArg;
133 NTSTATUS Status;
134 LPC_TYPE RequestType;
135 ULONG PortIdentifier;
136 PSX_MAX_MESSAGE Request;
137 PPSX_MAX_MESSAGE Reply = NULL;
138 BOOL NullReply = FALSE;
139
140 debug_print (L"PSXSS: ->%s pArg=%d", TEXT(__FUNCTION__), ulIndex);
141
142 while (TRUE)
143 {
144 Reply = NULL;
145 NullReply = FALSE;
146 while (!NullReply)
147 {
148 Status = NtReplyWaitReceivePort (
149 Server.Port[ulIndex].hObject,
150 0,
151 (PLPC_MESSAGE) Reply,
152 (PLPC_MESSAGE) & Request
153 );
154 if (!NT_SUCCESS(Status))
155 {
156 break;
157 }
158 RequestType = PORT_MESSAGE_TYPE(Request);
159 switch (RequestType)
160 {
161 case LPC_CONNECTION_REQUEST:
162 ProcessConnectionRequest ((PLPC_MAX_MESSAGE) & Request);
163 NullReply = TRUE;
164 continue;
165 case LPC_CLIENT_DIED:
166 case LPC_PORT_CLOSED:
167 case LPC_DEBUG_EVENT:
168 case LPC_ERROR_EVENT:
169 case LPC_EXCEPTION:
170 NullReply = TRUE;
171 continue;
172 default:
173 if (RequestType != LPC_REQUEST)
174 {
175 NullReply = TRUE;
176 continue;
177 }
178 }
179 Reply = & Request;
180 Reply->PsxHeader.Status = ProcessRequest (& Request);
181 NullReply = FALSE;
182 }
183 if ((STATUS_INVALID_HANDLE == Status) ||
184 (STATUS_OBJECT_TYPE_MISMATCH == Status))
185 {
186 break;
187 }
188 }
189 #ifdef __PSXSS_ON_W32__
190 TerminateThread(GetCurrentThread(),Status);
191 #else
192 NtTerminateThread(NtCurrentThread(),Status);
193 #endif
194 }
195 /* EOF */