f1dfd19a0c985f3ef47fb7db70021089e7858efd
[reactos.git] / reactos / subsys / csrss / api / wapi.c
1 /* $Id: wapi.c,v 1.3 2000/02/27 02:12:07 ekohl Exp $
2 *
3 * reactos/subsys/csrss/init.c
4 *
5 * Initialize the CSRSS subsystem server process.
6 *
7 * ReactOS Operating System
8 *
9 */
10
11 /* INCLUDES ******************************************************************/
12
13 #include <ddk/ntddk.h>
14 #include <ntdll/rtl.h>
15 #include <csrss/csrss.h>
16
17 #include "api.h"
18
19 /* GLOBALS *******************************************************************/
20
21 HANDLE CsrssApiHeap;
22
23 /* FUNCTIONS *****************************************************************/
24
25 static void Thread_Api2(HANDLE ServerPort)
26 {
27 NTSTATUS Status;
28 PLPCMESSAGE LpcReply;
29 LPCMESSAGE LpcRequest;
30 PCSRSS_API_REQUEST Request;
31 CSRSS_API_REPLY Reply;
32 PCSRSS_PROCESS_DATA ProcessData;
33
34 LpcReply = NULL;
35
36 for (;;)
37 {
38 Status = NtReplyWaitReceivePort(ServerPort,
39 0,
40 LpcReply,
41 &LpcRequest);
42 if (!NT_SUCCESS(Status))
43 {
44 DisplayString(L"NtReplyWaitReceivePort failed\n");
45 }
46
47 Request = (PCSRSS_API_REQUEST)LpcRequest.MessageData;
48
49 ProcessData = CsrGetProcessData(LpcRequest.ClientProcessId);
50
51 DisplayString(L"Received request\n");
52
53 switch (Request->Type)
54 {
55 case CSRSS_CREATE_PROCESS:
56 Reply.Status = CsrCreateProcess(ProcessData,
57 Request);
58 break;
59
60 case CSRSS_TERMINATE_PROCESS:
61 Reply.Status = CsrTerminateProcess(ProcessData,
62 Request);
63 break;
64
65 case CSRSS_WRITE_CONSOLE:
66 Reply.Status = CsrWriteConsole(ProcessData,
67 Request,
68 &Reply.Count);
69 break;
70
71 case CSRSS_READ_CONSOLE:
72 Reply.Status = CsrReadConsole(ProcessData,
73 Request,
74 &Reply.Count);
75 break;
76
77 case CSRSS_ALLOC_CONSOLE:
78 Reply.Status = CsrAllocConsole(ProcessData,
79 Request,
80 &Reply.Handle);
81 break;
82
83 case CSRSS_FREE_CONSOLE:
84 Reply.Status = CsrFreeConsole(ProcessData,
85 Request);
86 break;
87
88 case CSRSS_CONNECT_PROCESS:
89 Reply.Status = CsrConnectProcess(ProcessData,
90 Request);
91
92 default:
93 Reply.Status = STATUS_NOT_IMPLEMENTED;
94 }
95
96 LpcReply = &LpcRequest;
97 RtlCopyMemory(LpcReply->MessageData, &Reply, sizeof(Reply));
98 }
99 }
100
101 /**********************************************************************
102 * NAME
103 * Thread_Api
104 *
105 * DESCRIPTION
106 * Handle connection requests from clients to the port
107 * "\Windows\ApiPort".
108 */
109 void Thread_Api(PVOID PortHandle)
110 {
111 NTSTATUS Status;
112 LPCMESSAGE Request;
113 HANDLE ServerPort;
114
115 CsrssApiHeap = RtlCreateHeap(HEAP_GROWABLE,
116 NULL,
117 65536,
118 65536,
119 NULL,
120 NULL);
121 if (CsrssApiHeap == NULL)
122 {
123 PrintString("Failed to create private heap, aborting\n");
124 return;
125 }
126
127 CsrInitProcessData();
128
129 for (;;)
130 {
131 Status = NtListenPort(PortHandle, &Request);
132 if (!NT_SUCCESS(Status))
133 {
134 DisplayString(L"NtListenPort() failed\n");
135 NtTerminateThread(NtCurrentThread(), Status);
136 }
137
138 Status = NtAcceptConnectPort(&ServerPort,
139 PortHandle,
140 NULL,
141 1,
142 0,
143 NULL);
144 if (!NT_SUCCESS(Status))
145 {
146 DisplayString(L"NtAcceptConnectPort() failed\n");
147 NtTerminateThread(NtCurrentThread(), Status);
148 }
149
150 Status = NtCompleteConnectPort(ServerPort);
151 if (!NT_SUCCESS(Status))
152 {
153 DisplayString(L"NtCompleteConnectPort() failed\n");
154 NtTerminateThread(NtCurrentThread(), Status);
155 }
156
157 Status = RtlCreateUserThread(NtCurrentProcess(),
158 NULL,
159 FALSE,
160 0,
161 NULL,
162 NULL,
163 (PTHREAD_START_ROUTINE)Thread_Api2,
164 ServerPort,
165 NULL,
166 NULL);
167 if (!NT_SUCCESS(Status))
168 {
169 DisplayString(L"Unable to create server thread\n");
170 NtClose(ServerPort);
171 NtTerminateThread(NtCurrentThread(), Status);
172 }
173 }
174 }
175