3 * reactos/subsys/csrss/api/wapi.c
5 * CSRSS port message processing
7 * ReactOS Operating System
11 /* INCLUDES ******************************************************************/
20 /* GLOBALS *******************************************************************/
22 extern HANDLE hApiPort
;
24 HANDLE CsrssApiHeap
= (HANDLE
) 0;
26 static unsigned ApiDefinitionsCount
= 0;
27 static PCSRSS_API_DEFINITION ApiDefinitions
= NULL
;
29 /* FUNCTIONS *****************************************************************/
32 CsrApiRegisterDefinitions(PCSRSS_API_DEFINITION NewDefinitions
)
35 PCSRSS_API_DEFINITION Scan
;
36 PCSRSS_API_DEFINITION New
;
38 DPRINT("CSR: %s called\n", __FUNCTION__
);
41 for (Scan
= NewDefinitions
; 0 != Scan
->Handler
; Scan
++)
46 New
= RtlAllocateHeap(CsrssApiHeap
, 0,
47 (ApiDefinitionsCount
+ NewCount
)
48 * sizeof(CSRSS_API_DEFINITION
));
51 DPRINT1("Unable to allocate memory\n");
52 return STATUS_NO_MEMORY
;
54 if (0 != ApiDefinitionsCount
)
56 RtlCopyMemory(New
, ApiDefinitions
,
57 ApiDefinitionsCount
* sizeof(CSRSS_API_DEFINITION
));
58 RtlFreeHeap(CsrssApiHeap
, 0, ApiDefinitions
);
60 RtlCopyMemory(New
+ ApiDefinitionsCount
, NewDefinitions
,
61 NewCount
* sizeof(CSRSS_API_DEFINITION
));
63 ApiDefinitionsCount
+= NewCount
;
65 return STATUS_SUCCESS
;
70 CsrApiCallHandler(PCSRSS_PROCESS_DATA ProcessData
,
71 PCSR_API_MESSAGE Request
)
76 DPRINT("CSR: Calling handler for type: %x.\n", Request
->Type
);
77 Type
= Request
->Type
& 0xFFFF; /* FIXME: USE MACRO */
78 DPRINT("CSR: API Number: %x ServerID: %x\n",Type
, Request
->Type
>> 16);
80 /* FIXME: Extract DefIndex instead of looping */
81 for (DefIndex
= 0; DefIndex
< ApiDefinitionsCount
; DefIndex
++)
83 if (ApiDefinitions
[DefIndex
].Type
== Type
)
85 if (Request
->Header
.u1
.s1
.DataLength
< ApiDefinitions
[DefIndex
].MinRequestSize
)
87 DPRINT1("Request type %d min request size %d actual %d\n",
88 Type
, ApiDefinitions
[DefIndex
].MinRequestSize
,
89 Request
->Header
.u1
.s1
.DataLength
);
90 Request
->Status
= STATUS_INVALID_PARAMETER
;
94 Request
->Status
= (ApiDefinitions
[DefIndex
].Handler
)(ProcessData
, Request
);
99 DPRINT1("CSR: Unknown request type 0x%x\n", Request
->Type
);
100 Request
->Header
.u1
.s1
.TotalLength
= sizeof(CSR_API_MESSAGE
);
101 Request
->Header
.u1
.s1
.DataLength
= sizeof(CSR_API_MESSAGE
) - sizeof(PORT_MESSAGE
);
102 Request
->Status
= STATUS_INVALID_SYSTEM_SERVICE
;
106 CallHardError(IN PCSRSS_PROCESS_DATA ProcessData
,
107 IN PHARDERROR_MSG HardErrorMessage
);
112 CsrHandleHardError(IN PCSRSS_PROCESS_DATA ProcessData
,
113 IN OUT PHARDERROR_MSG Message
)
115 DPRINT1("CSR: received hard error %lx\n", Message
->Status
);
117 /* Call the hard error handler in win32csr */
118 (VOID
)CallHardError(ProcessData
, Message
);
122 CsrpHandleConnectionRequest (PPORT_MESSAGE Request
,
123 IN HANDLE hApiListenPort
)
126 HANDLE ServerPort
= NULL
, ServerThread
= NULL
;
127 PCSRSS_PROCESS_DATA ProcessData
= NULL
;
128 REMOTE_PORT_VIEW LpcRead
;
129 LpcRead
.Length
= sizeof(LpcRead
);
132 DPRINT("CSR: %s: Handling: %p\n", __FUNCTION__
, Request
);
134 Status
= NtAcceptConnectPort(&ServerPort
,
140 if (!NT_SUCCESS(Status
))
142 DPRINT1("CSR: NtAcceptConnectPort() failed\n");
146 ProcessData
= CsrGetProcessData(Request
->ClientId
.UniqueProcess
);
147 if (ProcessData
== NULL
)
149 ProcessData
= CsrCreateProcessData(Request
->ClientId
.UniqueProcess
);
150 if (ProcessData
== NULL
)
152 DPRINT1("Unable to allocate or find data for process 0x%x\n",
153 Request
->ClientId
.UniqueProcess
);
154 Status
= STATUS_UNSUCCESSFUL
;
159 ProcessData
->CsrSectionViewBase
= LpcRead
.ViewBase
;
160 ProcessData
->CsrSectionViewSize
= LpcRead
.ViewSize
;
161 ProcessData
->ServerCommunicationPort
= ServerPort
;
163 Status
= NtCompleteConnectPort(ServerPort
);
164 if (!NT_SUCCESS(Status
))
166 DPRINT1("CSR: NtCompleteConnectPort() failed\n");
170 Status
= RtlCreateUserThread(NtCurrentProcess(),
176 (PTHREAD_START_ROUTINE
)ClientConnectionThread
,
180 if (!NT_SUCCESS(Status
))
182 DPRINT1("CSR: Unable to create server thread\n");
186 NtClose(ServerThread
);
188 Status
= STATUS_SUCCESS
;
189 DPRINT("CSR: %s done\n", __FUNCTION__
);
195 ClientConnectionThread(HANDLE ServerPort
)
198 BYTE RawRequest
[LPC_MAX_DATA_LENGTH
];
199 PCSR_API_MESSAGE Request
= (PCSR_API_MESSAGE
)RawRequest
;
200 PCSR_API_MESSAGE Reply
;
201 PCSRSS_PROCESS_DATA ProcessData
;
203 DPRINT("CSR: %s called\n", __FUNCTION__
);
205 /* Reply must be NULL at the first call to NtReplyWaitReceivePort */
208 /* Loop and reply/wait for a new message */
211 /* Send the reply and wait for a new request */
212 Status
= NtReplyWaitReceivePort(hApiPort
,
216 /* Client died, continue */
217 if (Status
== STATUS_INVALID_CID
)
223 if (!NT_SUCCESS(Status
))
225 DPRINT1("NtReplyWaitReceivePort failed: %lx\n", Status
);
229 /* If the connection was closed, handle that */
230 if (Request
->Header
.u2
.s2
.Type
== LPC_PORT_CLOSED
)
232 DPRINT("Port died, oh well\n");
233 CsrFreeProcessData( Request
->Header
.ClientId
.UniqueProcess
);
237 if (Request
->Header
.u2
.s2
.Type
== LPC_CONNECTION_REQUEST
)
239 CsrpHandleConnectionRequest((PPORT_MESSAGE
)Request
, ServerPort
);
244 if (Request
->Header
.u2
.s2
.Type
== LPC_CLIENT_DIED
)
246 DPRINT("Client died, oh well\n");
251 if ((Request
->Header
.u2
.s2
.Type
!= LPC_ERROR_EVENT
) &&
252 (Request
->Header
.u2
.s2
.Type
!= LPC_REQUEST
))
254 DPRINT1("CSR: received message %d\n", Request
->Header
.u2
.s2
.Type
);
259 DPRINT("CSR: Got CSR API: %x [Message Origin: %x]\n",
261 Request
->Header
.ClientId
.UniqueThread
);
263 /* Get the Process Data */
264 ProcessData
= CsrGetProcessData(Request
->Header
.ClientId
.UniqueProcess
);
265 if (ProcessData
== NULL
)
267 DPRINT1("Message %d: Unable to find data for process 0x%x\n",
268 Request
->Header
.u2
.s2
.Type
,
269 Request
->Header
.ClientId
.UniqueProcess
);
272 if (ProcessData
->Terminated
)
274 DPRINT1("Message %d: process %p already terminated\n",
275 Request
->Type
, Request
->Header
.ClientId
.UniqueProcess
);
279 /* Check if we got a hard error */
280 if (Request
->Header
.u2
.s2
.Type
== LPC_ERROR_EVENT
)
282 /* Call the Handler */
283 CsrHandleHardError(ProcessData
, (PHARDERROR_MSG
)Request
);
287 /* Call the Handler */
288 CsrApiCallHandler(ProcessData
, Request
);
291 /* Send back the reply */
295 /* Close the port and exit the thread */
296 // NtClose(ServerPort);
298 DPRINT("CSR: %s done\n", __FUNCTION__
);
299 RtlExitUserThread(STATUS_SUCCESS
);
302 /**********************************************************************
304 * ServerSbApiPortThread/1
307 * Handle connection requests from SM to the port
308 * "\Windows\SbApiPort". We will accept only one
309 * connection request (from the SM).
312 ServerSbApiPortThread (HANDLE hSbApiPortListen
)
314 HANDLE hConnectedPort
= (HANDLE
) 0;
315 PORT_MESSAGE Request
;
316 PVOID Context
= NULL
;
317 NTSTATUS Status
= STATUS_SUCCESS
;
318 PPORT_MESSAGE Reply
= NULL
;
320 DPRINT("CSR: %s called\n", __FUNCTION__
);
322 RtlZeroMemory(&Request
, sizeof(PORT_MESSAGE
));
323 Status
= NtListenPort (hSbApiPortListen
, & Request
);
325 if (!NT_SUCCESS(Status
))
327 DPRINT1("CSR: %s: NtListenPort(SB) failed (Status=0x%08lx)\n",
328 __FUNCTION__
, Status
);
331 Status
= NtAcceptConnectPort(&hConnectedPort
,
337 if(!NT_SUCCESS(Status
))
339 DPRINT1("CSR: %s: NtAcceptConnectPort() failed (Status=0x%08lx)\n",
340 __FUNCTION__
, Status
);
343 Status
= NtCompleteConnectPort (hConnectedPort
);
344 if(!NT_SUCCESS(Status
))
346 DPRINT1("CSR: %s: NtCompleteConnectPort() failed (Status=0x%08lx)\n",
347 __FUNCTION__
, Status
);
351 * Tell the init thread the SM gave the
352 * green light for boostrapping.
354 Status
= NtSetEvent (hBootstrapOk
, NULL
);
355 if(!NT_SUCCESS(Status
))
357 DPRINT1("CSR: %s: NtSetEvent failed (Status=0x%08lx)\n",
358 __FUNCTION__
, Status
);
360 /* Wait for messages from the SM */
364 Status
= NtReplyWaitReceivePort(hConnectedPort
,
368 if(!NT_SUCCESS(Status
))
370 DPRINT1("CSR: %s: NtReplyWaitReceivePort failed (Status=0x%08lx)\n",
371 __FUNCTION__
, Status
);
375 switch (Request
.u2
.s2
.Type
) //fix .h PORT_MESSAGE_TYPE(Request))
379 DPRINT1("CSR: %s received message (type=%d)\n",
380 __FUNCTION__
, Request
.u2
.s2
.Type
);
388 DPRINT("CSR: %s: terminating!\n", __FUNCTION__
);
389 if(hConnectedPort
) NtClose (hConnectedPort
);
390 NtClose (hSbApiPortListen
);
391 NtTerminateThread (NtCurrentThread(), Status
);