b2b885e6d125aa378e66dd19165dbe31a7fa00c2
[reactos.git] / reactos / subsys / csrss / csrss.c
1 /* $Id$
2 *
3 * csrss.c - Client/Server Runtime subsystem
4 *
5 * ReactOS Operating System
6 *
7 * --------------------------------------------------------------------
8 *
9 * This software is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License as
11 * published by the Free Software Foundation; either version 2 of the
12 * License, or (at your option) any later version.
13 *
14 * This software is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Library General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this software; see the file COPYING.LIB. If not, write
21 * to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge,
22 * MA 02139, USA.
23 *
24 * --------------------------------------------------------------------
25 *
26 * 19990417 (Emanuele Aliberti)
27 * Do nothing native application skeleton
28 * 19990528 (Emanuele Aliberti)
29 * Compiled successfully with egcs 1.1.2
30 * 19990605 (Emanuele Aliberti)
31 * First standalone run under ReactOS (it
32 * actually does nothing but running).
33 */
34 #include <ddk/ntddk.h>
35 #include <ntdll/rtl.h>
36 #include <csrss/csrss.h>
37 #include <rosrtl/string.h>
38
39 #include "api.h"
40
41 #define NDEBUG
42 #include <debug.h>
43
44 #define CSRP_MAX_ARGUMENT_COUNT 512
45
46 typedef struct _COMMAND_LINE_ARGUMENT
47 {
48 ULONG Count;
49 UNICODE_STRING Buffer;
50 PWSTR * Vector;
51
52 } COMMAND_LINE_ARGUMENT, *PCOMMAND_LINE_ARGUMENT;
53
54 /**********************************************************************
55 * NAME PRIVATE
56 * CsrpParseCommandLine/3
57 */
58 static NTSTATUS STDCALL
59 CsrpParseCommandLine (HANDLE ProcessHeap,
60 PRTL_USER_PROCESS_PARAMETERS RtlProcessParameters,
61 PCOMMAND_LINE_ARGUMENT Argument)
62 {
63 INT i = 0;
64 INT afterlastspace = 0;
65
66
67 DPRINT("CSR: %s called\n", __FUNCTION__);
68
69 RtlZeroMemory (Argument, sizeof (COMMAND_LINE_ARGUMENT));
70
71 Argument->Vector = (PWSTR *) RtlAllocateHeap (ProcessHeap,
72 0,
73 (CSRP_MAX_ARGUMENT_COUNT * sizeof Argument->Vector[0]));
74 if(NULL == Argument->Vector)
75 {
76 DPRINT("CSR: %s: no memory for Argument->Vector\n", __FUNCTION__);
77 return STATUS_NO_MEMORY;
78 }
79
80 Argument->Buffer.Length =
81 Argument->Buffer.MaximumLength =
82 RtlProcessParameters->CommandLine.Length
83 + sizeof Argument->Buffer.Buffer [0]; /* zero terminated */
84 Argument->Buffer.Buffer =
85 (PWSTR) RtlAllocateHeap (ProcessHeap,
86 0,
87 Argument->Buffer.MaximumLength);
88 if(NULL == Argument->Buffer.Buffer)
89 {
90 DPRINT("CSR: %s: no memory for Argument->Buffer.Buffer\n", __FUNCTION__);
91 return STATUS_NO_MEMORY;
92 }
93
94 RtlCopyMemory (Argument->Buffer.Buffer,
95 RtlProcessParameters->CommandLine.Buffer,
96 RtlProcessParameters->CommandLine.Length);
97
98 while (Argument->Buffer.Buffer [i])
99 {
100 if (Argument->Buffer.Buffer[i] == L' ')
101 {
102 Argument->Count ++;
103 Argument->Buffer.Buffer [i] = L'\0';
104 Argument->Vector [Argument->Count - 1] = & (Argument->Buffer.Buffer [afterlastspace]);
105 i++;
106 while (Argument->Buffer.Buffer [i] == L' ')
107 {
108 i++;
109 }
110 afterlastspace = i;
111 }
112 else
113 {
114 i++;
115 }
116 }
117
118 if (Argument->Buffer.Buffer [afterlastspace] != L'\0')
119 {
120 Argument->Count ++;
121 Argument->Buffer.Buffer [i] = L'\0';
122 Argument->Vector [Argument->Count - 1] = & (Argument->Buffer.Buffer [afterlastspace]);
123 }
124
125 return STATUS_SUCCESS;
126 }
127
128 /**********************************************************************
129 * NAME PRIVATE
130 * CsrpFreeCommandLine/2
131 */
132
133 static VOID STDCALL
134 CsrpFreeCommandLine (HANDLE ProcessHeap,
135 PCOMMAND_LINE_ARGUMENT Argument)
136 {
137 DPRINT("CSR: %s called\n", __FUNCTION__);
138
139 RtlFreeHeap (ProcessHeap,
140 0,
141 Argument->Vector);
142 RtlFreeHeap (ProcessHeap,
143 0,
144 Argument->Buffer.Buffer);
145 }
146
147
148 /**********************************************************************
149 * NAME PRIVATE
150 * CsrpOpenSmInitDoneEvent/0
151 */
152 static NTSTATUS STDCALL
153 CsrpOpenSmInitDoneEvent (PHANDLE CsrssInitEvent)
154 {
155 OBJECT_ATTRIBUTES ObjectAttributes;
156 UNICODE_STRING EventName;
157
158 DPRINT("CSR: %s called\n", __FUNCTION__);
159
160 RtlInitUnicodeString(& EventName,
161 L"\\CsrssInitDone");
162 InitializeObjectAttributes (& ObjectAttributes,
163 & EventName,
164 EVENT_ALL_ACCESS,
165 0,
166 NULL);
167 return NtOpenEvent (CsrssInitEvent,
168 EVENT_ALL_ACCESS,
169 & ObjectAttributes);
170 }
171
172 /* Native process' entry point */
173
174 VOID STDCALL NtProcessStartup(PPEB Peb)
175 {
176 PRTL_USER_PROCESS_PARAMETERS RtlProcessParameters = NULL;
177 COMMAND_LINE_ARGUMENT CmdLineArg = {0};
178 HANDLE CsrssInitEvent = (HANDLE) 0;
179 NTSTATUS Status = STATUS_SUCCESS;
180
181 DPRINT("CSR: %s\n", __FUNCTION__);
182
183 RtlProcessParameters = RtlNormalizeProcessParams (Peb->ProcessParameters);
184
185 /*==================================================================
186 * Parse the command line.
187 *================================================================*/
188 Status = CsrpParseCommandLine (Peb->ProcessHeap,
189 RtlProcessParameters,
190 & CmdLineArg);
191 if(STATUS_SUCCESS != Status)
192 {
193 DbgPrint("CSR: CsrpParseCommandLine failed (Status=0x%08lx)\n",
194 Status);
195 }
196 /*
197 * Open the SM notification event to notify we are OK after
198 * subsystem server initialization.
199 */
200 Status = CsrpOpenSmInitDoneEvent(& CsrssInitEvent);
201 if (!NT_SUCCESS(Status))
202 {
203 DbgPrint("CSR: CsrpOpenSmInitDoneEvent failed (Status=0x%08lx)\n",
204 Status);
205 }
206 /*==================================================================
207 * Initialize the Win32 environment subsystem server.
208 *================================================================*/
209 if (CsrServerInitialization (CmdLineArg.Count, CmdLineArg.Vector) == TRUE)
210 {
211 /*=============================================================
212 * Tell SM we are up and safe. If we fail to notify SM, it will
213 * die and the kernel will bugcheck with
214 * SESSION5_INITIALIZATION_FAILED.
215 * TODO: this won't be necessary, because CSR will call SM
216 * API SM_SESSION_COMPLETE.
217 *===========================================================*/
218 NtSetEvent (CsrssInitEvent, NULL);
219
220 CsrpFreeCommandLine (Peb->ProcessHeap, & CmdLineArg);
221 /*
222 * Terminate the current thread only.
223 */
224 NtTerminateThread (NtCurrentThread(), 0);
225 }
226 else
227 {
228 DisplayString (L"CSR: CsrServerInitialization failed.\n");
229
230 CsrpFreeCommandLine (Peb->ProcessHeap, & CmdLineArg);
231 /*
232 * Tell the SM we failed.
233 */
234 NtTerminateProcess (NtCurrentProcess(), 0);
235 }
236 }
237
238 /* EOF */