Pass pointer to CSRSS process on first create request.
[reactos.git] / reactos / subsys / csrss / init.c
1 /* $Id: init.c,v 1.13 2002/06/14 14:23:14 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 /*
22 * Server's named ports.
23 */
24 static HANDLE ApiPortHandle;
25
26
27 HANDLE CsrInitEvent = INVALID_HANDLE_VALUE;
28 HANDLE CsrHeap = INVALID_HANDLE_VALUE;
29
30 HANDLE CsrObjectDirectory = INVALID_HANDLE_VALUE;
31 HANDLE CsrApiPort = INVALID_HANDLE_VALUE;
32 HANDLE CsrSbApiPort = INVALID_HANDLE_VALUE;
33
34 UNICODE_STRING CsrDirectoryName;
35
36 extern HANDLE CsrssApiHeap;
37
38 ULONG
39 InitializeVideoAddressSpace(VOID);
40
41 static NTSTATUS
42 CsrParseCommandLine (
43 ULONG ArgumentCount,
44 PWSTR *ArgumentArray
45 )
46 {
47 NTSTATUS Status;
48 OBJECT_ATTRIBUTES Attributes;
49 ANSI_STRING AnsiString;
50
51 ULONG i;
52
53 /* DbgPrint ("Arguments: %ld\n", ArgumentCount);
54 for (i = 0; i < ArgumentCount; i++)
55 {
56 DbgPrint ("Argument %ld: %S\n", i, ArgumentArray[i]);
57 }*/
58
59
60 /* create object directory ('\Windows') */
61 RtlCreateUnicodeString (&CsrDirectoryName,
62 L"\\Windows");
63
64 InitializeObjectAttributes (&Attributes,
65 &CsrDirectoryName,
66 0,
67 NULL,
68 NULL);
69
70 Status = NtCreateDirectoryObject(&CsrObjectDirectory,
71 0xF000F,
72 &Attributes);
73
74 return Status;
75 }
76
77
78 static VOID
79 CsrInitVideo(VOID)
80 {
81 OBJECT_ATTRIBUTES ObjectAttributes;
82 UNICODE_STRING DeviceName;
83 IO_STATUS_BLOCK Iosb;
84 HANDLE VideoHandle;
85 NTSTATUS Status;
86
87 RtlInitUnicodeString(&DeviceName, L"\\??\\DISPLAY1");
88 InitializeObjectAttributes(&ObjectAttributes,
89 &DeviceName,
90 0,
91 NULL,
92 NULL);
93 Status = NtOpenFile(&VideoHandle,
94 FILE_ALL_ACCESS,
95 &ObjectAttributes,
96 &Iosb,
97 0,
98 0);
99 if (NT_SUCCESS(Status))
100 {
101 NtClose(VideoHandle);
102 }
103 }
104
105
106 /**********************************************************************
107 * NAME
108 * CsrServerInitialization
109 *
110 * DESCRIPTION
111 * Create a directory object (\windows) and two named LPC ports:
112 *
113 * 1. \windows\ApiPort
114 * 2. \windows\SbApiPort
115 *
116 * RETURN VALUE
117 * TRUE: Initialization OK; otherwise FALSE.
118 */
119 BOOL
120 STDCALL
121 CsrServerInitialization (
122 ULONG ArgumentCount,
123 PWSTR *ArgumentArray
124 )
125 {
126 NTSTATUS Status;
127 OBJECT_ATTRIBUTES ObAttributes;
128 UNICODE_STRING PortName;
129 OBJECT_ATTRIBUTES RefreshEventAttr;
130 UNICODE_STRING RefreshEventName;
131 HANDLE RefreshEventHandle;
132
133 Status = CsrParseCommandLine (ArgumentCount, ArgumentArray);
134 if (!NT_SUCCESS(Status))
135 {
136 PrintString("CSR: Unable to parse the command line (Status: %x)\n", Status);
137 return(FALSE);
138 }
139
140 CsrInitVideo();
141
142 /* NEW NAMED PORT: \ApiPort */
143 RtlInitUnicodeString(&PortName, L"\\Windows\\ApiPort");
144 InitializeObjectAttributes(&ObAttributes,
145 &PortName,
146 0,
147 NULL,
148 NULL);
149
150 Status = NtCreatePort(&ApiPortHandle,
151 &ObAttributes,
152 260,
153 328,
154 0);
155 if (!NT_SUCCESS(Status))
156 {
157 PrintString("CSR: Unable to create \\ApiPort (Status %x)\n", Status);
158 return(FALSE);
159 }
160 CsrssApiHeap = RtlCreateHeap(HEAP_GROWABLE,
161 NULL,
162 65536,
163 65536,
164 NULL,
165 NULL);
166 if (CsrssApiHeap == NULL)
167 {
168 PrintString("CSR: Failed to create private heap, aborting\n");
169 return FALSE;
170 }
171
172 CsrInitConsoleSupport();
173 Status = RtlCreateUserThread(NtCurrentProcess(),
174 NULL,
175 FALSE,
176 0,
177 NULL,
178 NULL,
179 (PTHREAD_START_ROUTINE)Thread_Api,
180 ApiPortHandle,
181 NULL,
182 NULL);
183 if (!NT_SUCCESS(Status))
184 {
185 PrintString("CSR: Unable to create server thread\n");
186 NtClose(ApiPortHandle);
187 return FALSE;
188 }
189 RtlInitUnicodeString( &RefreshEventName, L"\\TextConsoleRefreshEvent" );
190 InitializeObjectAttributes( &RefreshEventAttr, &RefreshEventName, 0, NULL, NULL );
191 Status = NtCreateEvent( &RefreshEventHandle, STANDARD_RIGHTS_ALL, &RefreshEventAttr, FALSE, FALSE );
192 if( !NT_SUCCESS( Status ) )
193 {
194 PrintString( "CSR: Unable to create refresh event!\n" );
195 return FALSE;
196 }
197 Status = RtlCreateUserThread( NtCurrentProcess(), NULL, FALSE, 0, NULL, NULL, (PTHREAD_START_ROUTINE)Console_Api, (PVOID) RefreshEventHandle, NULL, NULL );
198 if( !NT_SUCCESS( Status ) )
199 {
200 PrintString( "CSR: Unable to create console thread\n" );
201 return FALSE;
202 }
203
204 InitializeVideoAddressSpace();
205
206 return TRUE;
207 }
208
209 /* EOF */