3 * reactos/subsys/csrss/api/wapi.c
5 * CSRSS port message processing
7 * ReactOS Operating System
11 /* INCLUDES ******************************************************************/
18 /* GLOBALS *******************************************************************/
20 HANDLE CsrssApiHeap
= (HANDLE
) 0;
22 static unsigned ApiDefinitionsCount
= 0;
23 static PCSRSS_API_DEFINITION ApiDefinitions
= NULL
;
25 /* FUNCTIONS *****************************************************************/
28 CsrApiRegisterDefinitions(PCSRSS_API_DEFINITION NewDefinitions
)
31 PCSRSS_API_DEFINITION Scan
;
32 PCSRSS_API_DEFINITION New
;
34 DPRINT("CSR: %s called", __FUNCTION__
);
37 for (Scan
= NewDefinitions
; 0 != Scan
->Handler
; Scan
++)
42 New
= RtlAllocateHeap(CsrssApiHeap
, 0,
43 (ApiDefinitionsCount
+ NewCount
)
44 * sizeof(CSRSS_API_DEFINITION
));
47 DPRINT1("Unable to allocate memory\n");
48 return STATUS_NO_MEMORY
;
50 if (0 != ApiDefinitionsCount
)
52 RtlCopyMemory(New
, ApiDefinitions
,
53 ApiDefinitionsCount
* sizeof(CSRSS_API_DEFINITION
));
54 RtlFreeHeap(CsrssApiHeap
, 0, ApiDefinitions
);
56 RtlCopyMemory(New
+ ApiDefinitionsCount
, NewDefinitions
,
57 NewCount
* sizeof(CSRSS_API_DEFINITION
));
59 ApiDefinitionsCount
+= NewCount
;
61 return STATUS_SUCCESS
;
66 CsrApiCallHandler(PCSRSS_PROCESS_DATA ProcessData
,
67 PCSR_API_MESSAGE Request
)
73 DPRINT("CSR: Calling handler for type: %x.\n", Request
->Type
);
74 Type
= Request
->Type
& 0xFFFF; /* FIXME: USE MACRO */
75 DPRINT("CSR: API Number: %x ServerID: %x\n",Type
, Request
->Type
>> 16);
77 /* FIXME: Extract DefIndex instead of looping */
78 for (DefIndex
= 0; ! Found
&& DefIndex
< ApiDefinitionsCount
; DefIndex
++)
80 if (ApiDefinitions
[DefIndex
].Type
== Type
)
82 if (Request
->Header
.u1
.s1
.DataLength
< ApiDefinitions
[DefIndex
].MinRequestSize
)
84 DPRINT1("Request type %d min request size %d actual %d\n",
85 Type
, ApiDefinitions
[DefIndex
].MinRequestSize
,
86 Request
->Header
.u1
.s1
.DataLength
);
87 Request
->Status
= STATUS_INVALID_PARAMETER
;
91 (ApiDefinitions
[DefIndex
].Handler
)(ProcessData
, Request
);
98 DPRINT1("CSR: Unknown request type 0x%x\n", Request
->Type
);
99 Request
->Header
.u1
.s1
.TotalLength
= sizeof(CSR_API_MESSAGE
);
100 Request
->Header
.u1
.s1
.DataLength
= sizeof(CSR_API_MESSAGE
) - sizeof(PORT_MESSAGE
);
101 Request
->Status
= STATUS_INVALID_SYSTEM_SERVICE
;
108 ClientConnectionThread(HANDLE ServerPort
)
111 CSR_API_MESSAGE Request
;
112 PCSR_API_MESSAGE Reply
;
113 PCSRSS_PROCESS_DATA ProcessData
;
115 DPRINT("CSR: %s called", __FUNCTION__
);
117 /* Reply must be NULL at the first call to NtReplyWaitReceivePort */
120 /* Loop and reply/wait for a new message */
123 /* Send the reply and wait for a new request */
124 Status
= NtReplyWaitReceivePort(ServerPort
,
128 if (!NT_SUCCESS(Status
))
130 DPRINT1("CSR: NtReplyWaitReceivePort failed\n");
134 /* If the connection was closed, handle that */
135 if (Request
.Header
.u2
.s2
.Type
== LPC_PORT_CLOSED
)
137 CsrFreeProcessData( Request
.Header
.ClientId
.UniqueProcess
);
141 DPRINT("CSR: Got CSR API: %x [Message Origin: %x]\n",
143 Request
.Header
.ClientId
.UniqueProcess
);
145 /* Get the Process Data */
146 ProcessData
= CsrGetProcessData(Request
.Header
.ClientId
.UniqueProcess
);
147 if (ProcessData
== NULL
)
149 DPRINT1("CSR: Message %d: Unable to find data for process 0x%x\n",
150 Request
.Header
.u2
.s2
.Type
,
151 Request
.Header
.ClientId
.UniqueProcess
);
155 /* Call the Handler */
156 CsrApiCallHandler(ProcessData
, &Request
);
158 /* Send back the reply */
162 /* Close the port and exit the thread */
164 RtlExitUserThread(STATUS_SUCCESS
);
167 /**********************************************************************
169 * ServerApiPortThread/1
172 * Handle connection requests from clients to the port
173 * "\Windows\ApiPort".
176 ServerApiPortThread (PVOID PortHandle
)
178 NTSTATUS Status
= STATUS_SUCCESS
;
179 PORT_MESSAGE Request
;
180 HANDLE hApiListenPort
= * (PHANDLE
) PortHandle
;
181 HANDLE ServerPort
= (HANDLE
) 0;
182 HANDLE ServerThread
= (HANDLE
) 0;
183 PCSRSS_PROCESS_DATA ProcessData
= NULL
;
185 CsrInitProcessData();
187 DPRINT("CSR: %s called", __FUNCTION__
);
191 REMOTE_PORT_VIEW LpcRead
;
194 Status
= NtListenPort (hApiListenPort
, &Request
);
195 if (!NT_SUCCESS(Status
))
197 DPRINT1("CSR: NtListenPort() failed\n");
200 Status
= NtAcceptConnectPort(& ServerPort
,
206 if (!NT_SUCCESS(Status
))
208 DPRINT1("CSR: NtAcceptConnectPort() failed\n");
212 ProcessData
= CsrCreateProcessData(Request
.ClientId
.UniqueProcess
);
213 if (ProcessData
== NULL
)
215 DPRINT1("Unable to allocate or find data for process 0x%x\n",
216 Request
.ClientId
.UniqueProcess
);
217 Status
= STATUS_UNSUCCESSFUL
;
222 ProcessData
->CsrSectionViewBase
= LpcRead
.ViewBase
;
223 ProcessData
->CsrSectionViewSize
= LpcRead
.ViewSize
;
225 Status
= NtCompleteConnectPort(ServerPort
);
226 if (!NT_SUCCESS(Status
))
228 DPRINT1("CSR: NtCompleteConnectPort() failed\n");
232 Status
= RtlCreateUserThread(NtCurrentProcess(),
238 (PTHREAD_START_ROUTINE
)ClientConnectionThread
,
242 if (!NT_SUCCESS(Status
))
244 DPRINT1("CSR: Unable to create server thread\n");
247 NtClose(ServerThread
);
254 NtTerminateThread(NtCurrentThread(), Status
);
258 /**********************************************************************
260 * ServerSbApiPortThread/1
263 * Handle connection requests from SM to the port
264 * "\Windows\SbApiPort". We will accept only one
265 * connection request (from the SM).
268 ServerSbApiPortThread (PVOID PortHandle
)
270 HANDLE hSbApiPortListen
= * (PHANDLE
) PortHandle
;
271 HANDLE hConnectedPort
= (HANDLE
) 0;
272 PORT_MESSAGE Request
;
273 PVOID Context
= NULL
;
274 NTSTATUS Status
= STATUS_SUCCESS
;
276 DPRINT("CSR: %s called\n", __FUNCTION__
);
278 RtlZeroMemory(&Request
, sizeof(PORT_MESSAGE
));
279 Status
= NtListenPort (hSbApiPortListen
, & Request
);
280 if (!NT_SUCCESS(Status
))
282 DPRINT1("CSR: %s: NtListenPort(SB) failed (Status=0x%08lx)\n",
283 __FUNCTION__
, Status
);
286 Status
= NtAcceptConnectPort (& hConnectedPort
,
292 if(!NT_SUCCESS(Status
))
294 DPRINT1("CSR: %s: NtAcceptConnectPort() failed (Status=0x%08lx)\n",
295 __FUNCTION__
, Status
);
298 Status
= NtCompleteConnectPort (hConnectedPort
);
299 if(!NT_SUCCESS(Status
))
301 DPRINT1("CSR: %s: NtCompleteConnectPort() failed (Status=0x%08lx)\n",
302 __FUNCTION__
, Status
);
305 PPORT_MESSAGE Reply
= NULL
;
307 * Tell the init thread the SM gave the
308 * green light for boostrapping.
310 Status
= NtSetEvent (hBootstrapOk
, NULL
);
311 if(!NT_SUCCESS(Status
))
313 DPRINT1("CSR: %s: NtSetEvent failed (Status=0x%08lx)\n",
314 __FUNCTION__
, Status
);
316 /* Wait for messages from the SM */
320 Status
= NtReplyWaitReceivePort(hConnectedPort
,
324 if(!NT_SUCCESS(Status
))
326 DPRINT1("CSR: %s: NtReplyWaitReceivePort failed (Status=0x%08lx)\n",
327 __FUNCTION__
, Status
);
330 switch (Request
.u2
.s2
.Type
)//fix .h PORT_MESSAGE_TYPE(Request))
334 DPRINT1("CSR: %s received message (type=%d)\n",
335 __FUNCTION__
, Request
.u2
.s2
.Type
);
342 DPRINT1("CSR: %s: terminating!\n", __FUNCTION__
);
343 if(hConnectedPort
) NtClose (hConnectedPort
);
344 NtClose (hSbApiPortListen
);
345 NtTerminateThread (NtCurrentThread(), Status
);