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
)
77 DPRINT("CSR: Calling handler for type: %x.\n", Request
->Type
);
78 Type
= Request
->Type
& 0xFFFF; /* FIXME: USE MACRO */
79 DPRINT("CSR: API Number: %x ServerID: %x\n",Type
, Request
->Type
>> 16);
81 /* FIXME: Extract DefIndex instead of looping */
82 for (DefIndex
= 0; ! Found
&& DefIndex
< ApiDefinitionsCount
; DefIndex
++)
84 if (ApiDefinitions
[DefIndex
].Type
== Type
)
86 if (Request
->Header
.u1
.s1
.DataLength
< ApiDefinitions
[DefIndex
].MinRequestSize
)
88 DPRINT1("Request type %d min request size %d actual %d\n",
89 Type
, ApiDefinitions
[DefIndex
].MinRequestSize
,
90 Request
->Header
.u1
.s1
.DataLength
);
91 Request
->Status
= STATUS_INVALID_PARAMETER
;
95 (ApiDefinitions
[DefIndex
].Handler
)(ProcessData
, Request
);
102 DPRINT1("CSR: Unknown request type 0x%x\n", Request
->Type
);
103 Request
->Header
.u1
.s1
.TotalLength
= sizeof(CSR_API_MESSAGE
);
104 Request
->Header
.u1
.s1
.DataLength
= sizeof(CSR_API_MESSAGE
) - sizeof(PORT_MESSAGE
);
105 Request
->Status
= STATUS_INVALID_SYSTEM_SERVICE
;
109 BOOL FASTCALL
CallHardError(void);
114 CsrHandleHardError(IN PCSRSS_PROCESS_DATA ProcessData
,
115 IN OUT PHARDERROR_MSG Message
)
117 DPRINT1("CSR: received hard error %lx\n", Message
->Status
);
119 /* Call the hard error handler in win32csr */
124 CsrpHandleConnectionRequest (PPORT_MESSAGE Request
,
125 IN HANDLE hApiListenPort
)
128 HANDLE ServerPort
= (HANDLE
) 0;
129 PCSRSS_PROCESS_DATA ProcessData
= NULL
;
130 REMOTE_PORT_VIEW LpcRead
;
131 LpcRead
.Length
= sizeof(LpcRead
);
134 DPRINT("CSR: %s: Handling: %p\n", __FUNCTION__
, Request
);
136 Status
= NtAcceptConnectPort(&ServerPort
,
142 if (!NT_SUCCESS(Status
))
144 DPRINT1("CSR: NtAcceptConnectPort() failed\n");
148 ProcessData
= CsrGetProcessData(Request
->ClientId
.UniqueProcess
);
149 if (ProcessData
== NULL
)
151 ProcessData
= CsrCreateProcessData(Request
->ClientId
.UniqueProcess
);
152 if (ProcessData
== NULL
)
154 DPRINT1("Unable to allocate or find data for process 0x%x\n",
155 Request
->ClientId
.UniqueProcess
);
156 Status
= STATUS_UNSUCCESSFUL
;
161 ProcessData
->CsrSectionViewBase
= LpcRead
.ViewBase
;
162 ProcessData
->CsrSectionViewSize
= LpcRead
.ViewSize
;
163 ProcessData
->ServerCommunicationPort
= ServerPort
;
165 Status
= NtCompleteConnectPort(ServerPort
);
166 if (!NT_SUCCESS(Status
))
168 DPRINT1("CSR: NtCompleteConnectPort() failed\n");
172 HANDLE ServerThread
= (HANDLE
) 0;
173 Status
= RtlCreateUserThread(NtCurrentProcess(),
179 (PTHREAD_START_ROUTINE
)ClientConnectionThread
,
183 if (!NT_SUCCESS(Status
))
185 DPRINT1("CSR: Unable to create server thread\n");
189 NtClose(ServerThread
);
191 Status
= STATUS_SUCCESS
;
192 DPRINT("CSR: %s done\n", __FUNCTION__
);
198 ClientConnectionThread(HANDLE ServerPort
)
201 BYTE RawRequest
[LPC_MAX_DATA_LENGTH
];
202 PCSR_API_MESSAGE Request
= (PCSR_API_MESSAGE
)RawRequest
;
203 PCSR_API_MESSAGE Reply
;
204 PCSRSS_PROCESS_DATA ProcessData
;
206 DPRINT("CSR: %s called\n", __FUNCTION__
);
208 /* Reply must be NULL at the first call to NtReplyWaitReceivePort */
211 /* Loop and reply/wait for a new message */
214 /* Send the reply and wait for a new request */
215 Status
= NtReplyWaitReceivePort(hApiPort
,
219 /* Client died, continue */
220 if (Status
== STATUS_INVALID_CID
)
226 if (!NT_SUCCESS(Status
))
228 DPRINT1("NtReplyWaitReceivePort failed: %lx\n", Status
);
232 /* If the connection was closed, handle that */
233 if (Request
->Header
.u2
.s2
.Type
== LPC_PORT_CLOSED
)
235 DPRINT("Port died, oh well\n");
236 CsrFreeProcessData( Request
->Header
.ClientId
.UniqueProcess
);
240 if (Request
->Header
.u2
.s2
.Type
== LPC_CONNECTION_REQUEST
)
242 CsrpHandleConnectionRequest((PPORT_MESSAGE
)Request
, ServerPort
);
247 if (Request
->Header
.u2
.s2
.Type
== LPC_CLIENT_DIED
)
249 DPRINT("Client died, oh well\n");
254 if ((Request
->Header
.u2
.s2
.Type
!= LPC_ERROR_EVENT
) &&
255 (Request
->Header
.u2
.s2
.Type
!= LPC_REQUEST
))
257 DPRINT1("CSR: received message %d\n", Request
->Header
.u2
.s2
.Type
);
262 DPRINT("CSR: Got CSR API: %x [Message Origin: %x]\n",
264 Request
->Header
.ClientId
.UniqueThread
);
266 /* Get the Process Data */
267 ProcessData
= CsrGetProcessData(Request
->Header
.ClientId
.UniqueProcess
);
268 if (ProcessData
== NULL
)
270 DPRINT1("Message %d: Unable to find data for process 0x%x\n",
271 Request
->Header
.u2
.s2
.Type
,
272 Request
->Header
.ClientId
.UniqueProcess
);
275 if (ProcessData
->Terminated
)
277 DPRINT1("Message %d: process %d already terminated\n",
278 Request
->Type
, (ULONG
)Request
->Header
.ClientId
.UniqueProcess
);
282 /* Check if we got a hard error */
283 if (Request
->Header
.u2
.s2
.Type
== LPC_ERROR_EVENT
)
285 /* Call the Handler */
286 CsrHandleHardError(ProcessData
, (PHARDERROR_MSG
)Request
);
290 /* Call the Handler */
291 CsrApiCallHandler(ProcessData
, Request
);
294 /* Send back the reply */
298 /* Close the port and exit the thread */
299 // NtClose(ServerPort);
301 DPRINT("CSR: %s done\n", __FUNCTION__
);
302 RtlExitUserThread(STATUS_SUCCESS
);
305 /**********************************************************************
307 * ServerApiPortThread/1
310 * Handle connection requests from clients to the port
311 * "\Windows\ApiPort".
315 ServerApiPortThread (HANDLE hApiListenPort
)
317 NTSTATUS Status
= STATUS_SUCCESS
;
318 BYTE RawRequest
[sizeof(PORT_MESSAGE
) + sizeof(CSR_CONNECTION_INFO
)];
319 PPORT_MESSAGE Request
= (PPORT_MESSAGE
)RawRequest
;
321 DPRINT("CSR: %s called", __FUNCTION__
);
325 REMOTE_PORT_VIEW LpcRead
;
326 LpcRead
.Length
= sizeof(LpcRead
);
328 Status
= NtListenPort (hApiListenPort
, Request
);
329 if (!NT_SUCCESS(Status
))
331 DPRINT1("CSR: NtListenPort() failed, status=%x\n", Status
);
335 Status
= CsrpHandleConnectionRequest(Request
, hApiListenPort
);
336 if(!NT_SUCCESS(Status
))
338 DPRINT1("CSR: %s: SmpHandleConnectionRequest failed (Status=0x%08lx)\n",
339 __FUNCTION__
, Status
);
344 NtClose(hApiListenPort
);
345 NtTerminateThread(NtCurrentThread(), Status
);
350 /**********************************************************************
352 * ServerSbApiPortThread/1
355 * Handle connection requests from SM to the port
356 * "\Windows\SbApiPort". We will accept only one
357 * connection request (from the SM).
360 ServerSbApiPortThread (HANDLE hSbApiPortListen
)
362 HANDLE hConnectedPort
= (HANDLE
) 0;
363 PORT_MESSAGE Request
;
364 PVOID Context
= NULL
;
365 NTSTATUS Status
= STATUS_SUCCESS
;
366 PPORT_MESSAGE Reply
= NULL
;
368 DPRINT("CSR: %s called\n", __FUNCTION__
);
370 RtlZeroMemory(&Request
, sizeof(PORT_MESSAGE
));
371 Status
= NtListenPort (hSbApiPortListen
, & Request
);
373 if (!NT_SUCCESS(Status
))
375 DPRINT1("CSR: %s: NtListenPort(SB) failed (Status=0x%08lx)\n",
376 __FUNCTION__
, Status
);
379 Status
= NtAcceptConnectPort(&hConnectedPort
,
385 if(!NT_SUCCESS(Status
))
387 DPRINT1("CSR: %s: NtAcceptConnectPort() failed (Status=0x%08lx)\n",
388 __FUNCTION__
, Status
);
391 Status
= NtCompleteConnectPort (hConnectedPort
);
392 if(!NT_SUCCESS(Status
))
394 DPRINT1("CSR: %s: NtCompleteConnectPort() failed (Status=0x%08lx)\n",
395 __FUNCTION__
, Status
);
399 * Tell the init thread the SM gave the
400 * green light for boostrapping.
402 Status
= NtSetEvent (hBootstrapOk
, NULL
);
403 if(!NT_SUCCESS(Status
))
405 DPRINT1("CSR: %s: NtSetEvent failed (Status=0x%08lx)\n",
406 __FUNCTION__
, Status
);
408 /* Wait for messages from the SM */
412 Status
= NtReplyWaitReceivePort(hConnectedPort
,
416 if(!NT_SUCCESS(Status
))
418 DPRINT1("CSR: %s: NtReplyWaitReceivePort failed (Status=0x%08lx)\n",
419 __FUNCTION__
, Status
);
423 switch (Request
.u2
.s2
.Type
) //fix .h PORT_MESSAGE_TYPE(Request))
427 DPRINT1("CSR: %s received message (type=%d)\n",
428 __FUNCTION__
, Request
.u2
.s2
.Type
);
436 DPRINT("CSR: %s: terminating!\n", __FUNCTION__
);
437 if(hConnectedPort
) NtClose (hConnectedPort
);
438 NtClose (hSbApiPortListen
);
439 NtTerminateThread (NtCurrentThread(), Status
);