32435fc4f19f51ffae3b17c9df77a2b56220f152
[reactos.git] / reactos / subsys / csrss / init.c
1 /* $Id: init.c,v 1.2 1999/07/17 23:10:30 ea Exp $
2 *
3 * reactos/subsys/csrss/init.c
4 *
5 * Initialize the CSRSS subsystem server process.
6 *
7 * ReactOS Operating System
8 *
9 */
10 #define PROTO_LPC
11 #include <ddk/ntddk.h>
12 #include "csrss.h"
13
14
15 void Thread_Api(void*);
16 void Thread_SbApi(void*);
17
18 /*
19 * Server's named ports.
20 */
21 struct _SERVER_PORTS
22 Server =
23 {
24 {INVALID_HANDLE_VALUE,Thread_Api},
25 {INVALID_HANDLE_VALUE,Thread_SbApi}
26 };
27 /**********************************************************************
28 * NAME
29 * Thread_Api
30 *
31 * DESCRIPTION
32 * Handle connection requests from clients to the port
33 * "\Windows\ApiPort".
34 */
35 static
36 void
37 Thread_Api(void * pPort)
38 {
39 NTSTATUS Status;
40 HANDLE Port;
41 HANDLE ConnectedPort;
42
43 Port = * (HANDLE*) pPort;
44
45 /*
46 * Make the CSR API port listen.
47 */
48 Status = NtListenPort(
49 Port,
50 CSRSS_API_PORT_QUEUE_SIZE
51 );
52 if (!NT_SUCCESS(Status))
53 {
54 /*
55 * FIXME: notify SM we could not
56 * make the port listen.
57 */
58 return;
59 }
60 /*
61 * Wait for a client to connect
62 */
63 while (TRUE)
64 {
65 /*
66 * Wait for a connection request;
67 * the new connected port's handle
68 * is stored in ConnectedPort.
69 */
70 Status = NtAcceptConnectPort(
71 Port,
72 & ConnectedPort
73 );
74 if (NT_SUCCESS(Status))
75 {
76 if (NT_SUCCESS(NtCompleteConnectPort(ConnectedPort)))
77 {
78 /* dispatch call */
79 continue;
80 }
81 /* error */
82 }
83 }
84 }
85
86
87 /**********************************************************************
88 * NAME
89 * Thread_SbApi
90 *
91 * DESCRIPTION
92 * Handle connection requests from clients to the port
93 * "\Windows\SbApiPort".
94 */
95 static
96 void
97 Thread_SbApi(void * pPort)
98 {
99 NTSTATUS Status;
100 HANDLE Port;
101 HANDLE ConnectedPort;
102
103 Port = * (HANDLE*) pPort;
104
105 Status = NtListenPort(
106 Port,
107 CSRSS_SBAPI_PORT_QUEUE_SIZE
108 );
109 if (!NT_SUCCESS(Status))
110 {
111 return;
112 }
113 /*
114 * Wait for a client to connect
115 */
116 while (TRUE)
117 {
118 Status = NtAcceptConnectPort(
119 Port,
120 & ConnectedPort
121 );
122 if (NT_SUCCESS(Status))
123 {
124 if (NT_SUCCESS(NtCompleteConnectPort(ConnectedPort)))
125 {
126 /* dispatch call */
127 continue;
128 }
129 /* error: Port.CompleteConnect failed */
130 continue;
131 }
132 /* error: Port.AcceptConnect failed */
133 }
134 }
135 #endif
136
137
138 /**********************************************************************
139 * NAME
140 * InitializeServer
141 *
142 * DESCRIPTION
143 * Create a directory object (\windows) and two named LPC ports:
144 *
145 * 1. \windows\ApiPort
146 * 2. \windows\SbApiPort
147 *
148 * RETURN VALUE
149 * TRUE: Initialization OK; otherwise FALSE.
150 */
151 BOOL
152 InitializeServer(void)
153 {
154 NTSTATUS Status;
155 OBJECT_ATTRIBUTES ObAttributes;
156
157
158 /* NEW NAMED PORT: \ApiPort */
159 Status = NtCreatePort(
160 & Server.Api.Port, /* New port's handle */
161 & ObAttributes, /* Port object's attributes */
162 ...
163 );
164 if (!NT_SUCCESS(Status))
165 {
166 return FALSE;
167 }
168 Status = NtCreateThread(
169 & Server.Api.Thread,
170 0, /* desired access */
171 & ObAttributes, /* object attributes */
172 NtCurrentProcess(), /* process' handle */
173 0, /* client id */
174 Thread_ApiPort,
175 (void*) & Server.Api.Port
176 );
177 if (!NT_SUCCESS(Status))
178 {
179 NtClose(Server.Api.Port);
180 return FALSE;
181 }
182 /* NEW NAMED PORT: \SbApiPort */
183 Status = NtCreatePort(
184 & Server.SbApi.Port,
185 & ObAttributes,
186 ...
187 );
188 if (!NT_SUCCESS(Status))
189 {
190 NtClose(Server.Api.Port);
191 return FALSE;
192 }
193 Status = NtCreateThread(
194 & Server.SbApi.Thread,
195 Thread_SbApi,
196 (void*) & Server.SbApi.Port
197 );
198 if (!NT_SUCCESS(Status))
199 {
200 NtClose(Server.Api.Port);
201 NtClose(Server.SbApi.Port);
202 return FALSE;
203 }
204 return TRUE;
205 }
206
207
208 /* EOF */