1 /* $Id: init.c,v 1.31 2002/03/18 16:16:47 ekohl Exp $
3 * init.c - Session Manager initialization
5 * ReactOS Operating System
7 * --------------------------------------------------------------------
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.
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 * General Public License for more details.
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,
24 * --------------------------------------------------------------------
26 * 19990530 (Emanuele Aliberti)
27 * Compiled successfully with egcs 1.1.2
30 #include <ntdll/rtl.h>
32 #include <napi/shared_data.h>
38 /* TYPES ********************************************************************/
41 * NOTE: This is only used until the dos device links are
42 * read from the registry!!
48 } LINKDATA
, *PLINKDATA
;
50 /* GLOBAL VARIABLES *********************************************************/
52 HANDLE SmApiPort
= INVALID_HANDLE_VALUE
;
53 HANDLE DbgSsApiPort
= INVALID_HANDLE_VALUE
;
54 HANDLE DbgUiApiPort
= INVALID_HANDLE_VALUE
;
56 PWSTR SmSystemEnvironment
= NULL
;
59 /* FUNCTIONS ****************************************************************/
62 SmCreatePagingFiles (VOID
)
64 UNICODE_STRING FileName
;
65 LARGE_INTEGER InitialSize
;
66 LARGE_INTEGER MaximumSize
;
69 /* FIXME: Read file names from registry */
71 RtlInitUnicodeString (&FileName
,
72 L
"\\SystemRoot\\pagefile.sys");
74 InitialSize
.QuadPart
= 50 * 4096;
75 MaximumSize
.QuadPart
= 80 * 4096;
77 Status
= NtCreatePagingFile(&FileName
,
81 if (!NT_SUCCESS(Status
))
83 PrintString("SM: Failed to create paging file (Status was 0x%.8X)\n", Status
);
89 SmInitDosDevices(VOID
)
91 OBJECT_ATTRIBUTES ObjectAttributes
;
92 UNICODE_STRING DeviceName
;
93 UNICODE_STRING LinkName
;
97 IO_STATUS_BLOCK StatusBlock
;
100 WCHAR LinkBuffer
[80];
103 LINKDATA LinkData
[] =
104 {{L
"\\Device\\NamedPipe", L
"PIPE"},
105 {L
"\\Device\\Null", L
"NUL"},
106 {L
"\\Device\\Mup", L
"UNC"},
107 {L
"\\Device\\MailSlot", L
"MAILSLOT"},
108 {L
"\\DosDevices\\COM1", L
"AUX"},
109 {L
"\\DosDevices\\LPT1", L
"PRN"},
112 /* FIXME: Read the list of symbolic links from the registry!! */
114 LinkPtr
= &LinkData
[0];
115 while (LinkPtr
->DeviceName
!= NULL
)
117 swprintf(LinkBuffer
, L
"\\??\\%s",
119 RtlInitUnicodeString(&LinkName
,
121 RtlInitUnicodeString(&DeviceName
,
122 LinkPtr
->DeviceName
);
125 /* check if target device exists (can be opened) */
126 InitializeObjectAttributes(&ObjectAttributes
,
132 Status
= NtOpenFile(&DeviceHandle
,
137 FILE_SYNCHRONOUS_IO_NONALERT
);
138 if (NT_SUCCESS(Status
))
140 NtClose(DeviceHandle
);
142 /* create symbolic link */
143 InitializeObjectAttributes(&ObjectAttributes
,
149 Status
= NtCreateSymbolicLinkObject(&LinkHandle
,
150 SYMBOLIC_LINK_ALL_ACCESS
,
153 if (!NT_SUCCESS(Status
))
155 PrintString("SM: NtCreateSymbolicLink( %wZ --> %wZ ) failed!\n",
169 SmSetEnvironmentVariables (VOID
)
171 UNICODE_STRING EnvVariable
;
172 UNICODE_STRING EnvValue
;
173 UNICODE_STRING EnvExpandedValue
;
174 ULONG ExpandedLength
;
175 WCHAR ExpandBuffer
[512];
176 WCHAR ValueBuffer
[MAX_PATH
];
177 PKUSER_SHARED_DATA SharedUserData
=
178 (PKUSER_SHARED_DATA
)USER_SHARED_DATA_BASE
;
181 * The following environment variables are read from the registry.
182 * Because the registry does not work yet, the environment variables
183 * are set one by one, using information from the shared user page.
185 * Variables (example):
186 * SystemRoot = C:\reactos
190 * Path = %SystemRoot%\system32;%SystemRoot%
191 * windir = %SystemRoot%
194 /* copy system root into value buffer */
195 wcscpy (ValueBuffer
, SharedUserData
->NtSystemRoot
);
197 /* set "SystemRoot = C:\reactos" */
198 RtlInitUnicodeString (&EnvVariable
,
200 RtlInitUnicodeString (&EnvValue
,
202 RtlSetEnvironmentVariable (&SmSystemEnvironment
,
206 /* cut off trailing path */
209 /* Set "SystemDrive = C:" */
210 RtlInitUnicodeString (&EnvVariable
,
212 RtlSetEnvironmentVariable (&SmSystemEnvironment
,
217 /* Set "OS = ReactOS" */
218 RtlInitUnicodeString (&EnvVariable
,
220 RtlInitUnicodeString (&EnvValue
,
222 RtlSetEnvironmentVariable (&SmSystemEnvironment
,
227 /* Set "Path = %SystemRoot%\system32;%SystemRoot%" */
228 RtlInitUnicodeString (&EnvVariable
,
230 RtlInitUnicodeString (&EnvValue
,
231 L
"%SystemRoot%\\system32;%SystemRoot%");
232 EnvExpandedValue
.Length
= 0;
233 EnvExpandedValue
.MaximumLength
= 512 * sizeof(WCHAR
);
234 EnvExpandedValue
.Buffer
= ExpandBuffer
;
236 RtlExpandEnvironmentStrings_U (SmSystemEnvironment
,
240 RtlSetEnvironmentVariable (&SmSystemEnvironment
,
244 /* Set "windir = %SystemRoot%" */
245 RtlInitUnicodeString (&EnvVariable
,
247 RtlInitUnicodeString (&EnvValue
,
249 EnvExpandedValue
.Length
= 0;
250 EnvExpandedValue
.MaximumLength
= 512 * sizeof(WCHAR
);
251 EnvExpandedValue
.Buffer
= ExpandBuffer
;
253 RtlExpandEnvironmentStrings_U (SmSystemEnvironment
,
257 RtlSetEnvironmentVariable (&SmSystemEnvironment
,
263 BOOL
InitSessionManager (HANDLE Children
[])
266 UNICODE_STRING UnicodeString
;
267 OBJECT_ATTRIBUTES ObjectAttributes
;
268 UNICODE_STRING CmdLineW
;
269 PRTL_USER_PROCESS_PARAMETERS ProcessParameters
;
270 RTL_PROCESS_INFO ProcessInfo
;
271 HANDLE CsrssInitEvent
;
272 HANDLE WindowsDirectory
;
273 WCHAR UnicodeBuffer
[MAX_PATH
];
274 PKUSER_SHARED_DATA SharedUserData
=
275 (PKUSER_SHARED_DATA
)USER_SHARED_DATA_BASE
;
278 * FIXME: The '\Windows' directory is created by csrss.exe but
279 * win32k.sys needs it at intialization and it is loaded
284 * Create the '\Windows' directory
286 RtlInitUnicodeString(&UnicodeString
,
289 InitializeObjectAttributes(&ObjectAttributes
,
295 Status
= ZwCreateDirectoryObject(&WindowsDirectory
,
298 if (!NT_SUCCESS(Status
))
300 DisplayString(L
"SM: Could not create \\Windows directory!\n");
304 /* Create the "\SmApiPort" object (LPC) */
305 RtlInitUnicodeString(&UnicodeString
,
307 InitializeObjectAttributes(&ObjectAttributes
,
313 Status
= NtCreatePort(&SmApiPort
,
318 if (!NT_SUCCESS(Status
))
324 DisplayString (L
"SM: \\SmApiPort created...\n");
327 /* Create two threads for "\SmApiPort" */
328 RtlCreateUserThread(NtCurrentProcess(),
334 (PTHREAD_START_ROUTINE
)SmApiThread
,
339 RtlCreateUserThread (NtCurrentProcess (),
345 (PTHREAD_START_ROUTINE
)SmApiThread
,
350 /* Create the system environment */
351 Status
= RtlCreateEnvironment(FALSE
,
352 &SmSystemEnvironment
);
353 if (!NT_SUCCESS(Status
))
358 DisplayString (L
"SM: System Environment created\n");
361 /* Define symbolic links to kernel devices (MS-DOS names) */
364 /* FIXME: Run all programs in the boot execution list */
367 /* FIXME: Process the file rename list */
368 // SmProcessFileRenameList();
370 /* FIXME: Load the well known DLLs */
373 /* Create paging files */
374 SmCreatePagingFiles();
376 /* Load remaining registry hives */
377 NtInitializeRegistry(FALSE
);
379 /* Set environment variables from registry */
380 SmSetEnvironmentVariables();
382 /* Load the kernel mode driver win32k.sys */
383 RtlInitUnicodeString(&CmdLineW
,
384 L
"\\SystemRoot\\system32\\drivers\\win32k.sys");
385 Status
= NtLoadDriver(&CmdLineW
);
387 if (!NT_SUCCESS(Status
))
394 RtlInitUnicodeString(&UnicodeString
,
396 InitializeObjectAttributes(&ObjectAttributes
,
401 Status
= NtCreateEvent(&CsrssInitEvent
,
406 if (!NT_SUCCESS(Status
))
408 DbgPrint("Failed to create csrss notification event\n");
412 * Start the Win32 subsystem (csrss.exe)
415 /* initialize executable path */
416 wcscpy(UnicodeBuffer
, L
"\\??\\");
417 wcscat(UnicodeBuffer
, SharedUserData
->NtSystemRoot
);
418 wcscat(UnicodeBuffer
, L
"\\system32\\csrss.exe");
419 RtlInitUnicodeString(&UnicodeString
,
422 RtlCreateProcessParameters(&ProcessParameters
,
433 Status
= RtlCreateUserProcess(&UnicodeString
,
434 OBJ_CASE_INSENSITIVE
,
444 RtlDestroyProcessParameters (ProcessParameters
);
446 if (!NT_SUCCESS(Status
))
448 DisplayString(L
"SM: Loading csrss.exe failed!\n");
452 NtWaitForSingleObject(CsrssInitEvent
,
456 Children
[CHILD_CSRSS
] = ProcessInfo
.ProcessHandle
;
459 * Start the logon process (winlogon.exe)
462 /* initialize executable path */
463 wcscpy(UnicodeBuffer
, L
"\\??\\");
464 wcscat(UnicodeBuffer
, SharedUserData
->NtSystemRoot
);
465 wcscat(UnicodeBuffer
, L
"\\system32\\winlogon.exe");
466 RtlInitUnicodeString(&UnicodeString
,
469 RtlCreateProcessParameters(&ProcessParameters
,
480 Status
= RtlCreateUserProcess(&UnicodeString
,
481 OBJ_CASE_INSENSITIVE
,
491 RtlDestroyProcessParameters(ProcessParameters
);
493 if (!NT_SUCCESS(Status
))
495 DisplayString(L
"SM: Loading winlogon.exe failed!\n");
496 NtTerminateProcess(Children
[CHILD_CSRSS
],
500 Children
[CHILD_WINLOGON
] = ProcessInfo
.ProcessHandle
;
502 /* Create the \DbgSsApiPort object (LPC) */
503 RtlInitUnicodeString(&UnicodeString
,
505 InitializeObjectAttributes(&ObjectAttributes
,
511 Status
= NtCreatePort(&DbgSsApiPort
,
517 if (!NT_SUCCESS(Status
))
522 DisplayString(L
"SM: DbgSsApiPort created...\n");
525 /* Create the \DbgUiApiPort object (LPC) */
526 RtlInitUnicodeString(&UnicodeString
,
528 InitializeObjectAttributes(&ObjectAttributes
,
534 Status
= NtCreatePort(&DbgUiApiPort
,
539 if (!NT_SUCCESS(Status
))
544 DisplayString (L
"SM: DbgUiApiPort created...\n");