* Significant improve boot speed and debug log neatness:
[reactos.git] / reactos / lib / rtl / process.c
1 /*
2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS system libraries
4 * FILE: lib/rtl/process.c
5 * PURPOSE: Process functions
6 * PROGRAMMER: Alex Ionescu (alex@relsoft.net)
7 * Ariadne (ariadne@xs4all.nl)
8 */
9
10 /* INCLUDES ****************************************************************/
11
12 #include <rtl.h>
13
14 #define NDEBUG
15 #include <debug.h>
16
17 /* INTERNAL FUNCTIONS *******************************************************/
18
19 NTSTATUS
20 NTAPI
21 RtlpMapFile(PUNICODE_STRING ImageFileName,
22 ULONG Attributes,
23 PHANDLE Section)
24 {
25 OBJECT_ATTRIBUTES ObjectAttributes;
26 NTSTATUS Status;
27 HANDLE hFile = NULL;
28 IO_STATUS_BLOCK IoStatusBlock;
29
30 /* Open the Image File */
31 InitializeObjectAttributes(&ObjectAttributes,
32 ImageFileName,
33 Attributes & (OBJ_CASE_INSENSITIVE | OBJ_INHERIT),
34 NULL,
35 NULL);
36 Status = ZwOpenFile(&hFile,
37 SYNCHRONIZE | FILE_EXECUTE | FILE_READ_DATA,
38 &ObjectAttributes,
39 &IoStatusBlock,
40 FILE_SHARE_DELETE | FILE_SHARE_READ,
41 FILE_SYNCHRONOUS_IO_NONALERT | FILE_NON_DIRECTORY_FILE);
42 if (!NT_SUCCESS(Status))
43 {
44 DPRINT1("Failed to read image file from disk\n");
45 return(Status);
46 }
47
48 /* Now create a section for this image */
49 Status = ZwCreateSection(Section,
50 SECTION_ALL_ACCESS,
51 NULL,
52 NULL,
53 PAGE_EXECUTE,
54 SEC_IMAGE,
55 hFile);
56 if (!NT_SUCCESS(Status))
57 {
58 DPRINT1("Failed to create section for image file\n");
59 }
60
61 ZwClose(hFile);
62 return Status;
63 }
64
65 /* FUNCTIONS ****************************************************************/
66
67 NTSTATUS
68 NTAPI
69 RtlpInitEnvironment(HANDLE ProcessHandle,
70 PPEB Peb,
71 PRTL_USER_PROCESS_PARAMETERS ProcessParameters)
72 {
73 NTSTATUS Status;
74 PVOID BaseAddress = NULL;
75 ULONG EnviroSize;
76 ULONG Size;
77 PWCHAR Environment = 0;
78 DPRINT("RtlpInitEnvironment (hProcess: %p, Peb: %p Params: %p)\n",
79 ProcessHandle, Peb, ProcessParameters);
80
81 /* Give the caller 1MB if he requested it */
82 if (ProcessParameters->Flags & RTL_USER_PROCESS_PARAMETERS_RESERVE_1MB)
83 {
84 /* Give 1MB starting at 0x4 */
85 BaseAddress = (PVOID)4;
86 EnviroSize = 1024 * 1024;
87 Status = ZwAllocateVirtualMemory(ProcessHandle,
88 &BaseAddress,
89 0,
90 &EnviroSize,
91 MEM_RESERVE,
92 PAGE_READWRITE);
93 if (!NT_SUCCESS(Status))
94 {
95 DPRINT1("Failed to reserve 1MB of space \n");
96 return(Status);
97 }
98 }
99
100 /* Find the end of the Enviroment Block */
101 if ((Environment = (PWCHAR)ProcessParameters->Environment))
102 {
103 while (*Environment++) while (*Environment++);
104
105 /* Calculate the size of the block */
106 EnviroSize = (ULONG)((ULONG_PTR)Environment -
107 (ULONG_PTR)ProcessParameters->Environment);
108
109 /* Allocate and Initialize new Environment Block */
110 Size = EnviroSize;
111 Status = ZwAllocateVirtualMemory(ProcessHandle,
112 &BaseAddress,
113 0,
114 &Size,
115 MEM_RESERVE | MEM_COMMIT,
116 PAGE_READWRITE);
117 if (!NT_SUCCESS(Status))
118 {
119 DPRINT1("Failed to allocate Environment Block\n");
120 return(Status);
121 }
122
123 /* Write the Environment Block */
124 ZwWriteVirtualMemory(ProcessHandle,
125 BaseAddress,
126 ProcessParameters->Environment,
127 EnviroSize,
128 NULL);
129
130 /* Save pointer */
131 ProcessParameters->Environment = BaseAddress;
132 }
133
134 /* Now allocate space for the Parameter Block */
135 BaseAddress = NULL;
136 Size = ProcessParameters->MaximumLength;
137 Status = ZwAllocateVirtualMemory(ProcessHandle,
138 &BaseAddress,
139 0,
140 &Size,
141 MEM_COMMIT,
142 PAGE_READWRITE);
143 if (!NT_SUCCESS(Status))
144 {
145 DPRINT1("Failed to allocate Parameter Block\n");
146 return(Status);
147 }
148
149 /* Write the Parameter Block */
150 ZwWriteVirtualMemory(ProcessHandle,
151 BaseAddress,
152 ProcessParameters,
153 ProcessParameters->Length,
154 NULL);
155
156 /* Write pointer to Parameter Block */
157 ZwWriteVirtualMemory(ProcessHandle,
158 &Peb->ProcessParameters,
159 &BaseAddress,
160 sizeof(BaseAddress),
161 NULL);
162
163 /* Return */
164 return STATUS_SUCCESS;
165 }
166
167 /*
168 * @implemented
169 *
170 * Creates a process and its initial thread.
171 *
172 * NOTES:
173 * - The first thread is created suspended, so it needs a manual resume!!!
174 * - If ParentProcess is NULL, current process is used
175 * - ProcessParameters must be normalized
176 * - Attributes are object attribute flags used when opening the ImageFileName.
177 * Valid flags are OBJ_INHERIT and OBJ_CASE_INSENSITIVE.
178 *
179 * -Gunnar
180 */
181 NTSTATUS
182 NTAPI
183 RtlCreateUserProcess(IN PUNICODE_STRING ImageFileName,
184 IN ULONG Attributes,
185 IN OUT PRTL_USER_PROCESS_PARAMETERS ProcessParameters,
186 IN PSECURITY_DESCRIPTOR ProcessSecurityDescriptor OPTIONAL,
187 IN PSECURITY_DESCRIPTOR ThreadSecurityDescriptor OPTIONAL,
188 IN HANDLE ParentProcess OPTIONAL,
189 IN BOOLEAN InheritHandles,
190 IN HANDLE DebugPort OPTIONAL,
191 IN HANDLE ExceptionPort OPTIONAL,
192 OUT PRTL_USER_PROCESS_INFORMATION ProcessInfo)
193 {
194 NTSTATUS Status;
195 HANDLE hSection;
196 PROCESS_BASIC_INFORMATION ProcessBasicInfo;
197 OBJECT_ATTRIBUTES ObjectAttributes;
198 DPRINT("RtlCreateUserProcess: %wZ\n", ImageFileName);
199
200 /* Map and Load the File */
201 Status = RtlpMapFile(ImageFileName,
202 Attributes,
203 &hSection);
204 if(!NT_SUCCESS(Status))
205 {
206 DPRINT1("Could not map process image\n");
207 return Status;
208 }
209
210 /* Clean out the CurDir Handle if we won't use it */
211 if (!InheritHandles) ProcessParameters->CurrentDirectory.Handle = NULL;
212
213 /* Use us as parent if none other specified */
214 if (!ParentProcess) ParentProcess = NtCurrentProcess();
215
216 /* Initialize the Object Attributes */
217 InitializeObjectAttributes(&ObjectAttributes,
218 NULL,
219 0,
220 NULL,
221 ProcessSecurityDescriptor);
222
223 /*
224 * If FLG_ENABLE_CSRDEBUG is used, then CSRSS is created under the
225 * watch of WindowsSS
226 */
227 if ((RtlGetNtGlobalFlags() & FLG_ENABLE_CSRDEBUG) &&
228 (wcsstr(ImageFileName->Buffer, L"csrss")))
229 {
230 UNICODE_STRING DebugString = RTL_CONSTANT_STRING(L"\\WindowsSS");
231 InitializeObjectAttributes(&ObjectAttributes,
232 &DebugString,
233 0,
234 NULL,
235 ProcessSecurityDescriptor);
236 }
237
238 /* Create Kernel Process Object */
239 Status = ZwCreateProcess(&ProcessInfo->ProcessHandle,
240 PROCESS_ALL_ACCESS,
241 &ObjectAttributes,
242 ParentProcess,
243 InheritHandles,
244 hSection,
245 DebugPort,
246 ExceptionPort);
247 if (!NT_SUCCESS(Status))
248 {
249 DPRINT1("Could not create Kernel Process Object\n");
250 ZwClose(hSection);
251 return(Status);
252 }
253
254 /* Get some information on the image */
255 Status = ZwQuerySection(hSection,
256 SectionImageInformation,
257 &ProcessInfo->ImageInformation,
258 sizeof(SECTION_IMAGE_INFORMATION),
259 NULL);
260 if (!NT_SUCCESS(Status))
261 {
262 DPRINT1("Could not query Section Info\n");
263 ZwClose(ProcessInfo->ProcessHandle);
264 ZwClose(hSection);
265 return(Status);
266 }
267
268 /* Get some information about the process */
269 ZwQueryInformationProcess(ProcessInfo->ProcessHandle,
270 ProcessBasicInformation,
271 &ProcessBasicInfo,
272 sizeof(ProcessBasicInfo),
273 NULL);
274 if (!NT_SUCCESS(Status))
275 {
276 DPRINT1("Could not query Process Info\n");
277 ZwClose(ProcessInfo->ProcessHandle);
278 ZwClose(hSection);
279 return(Status);
280 }
281
282 /* Create Process Environment */
283 RtlpInitEnvironment(ProcessInfo->ProcessHandle,
284 ProcessBasicInfo.PebBaseAddress,
285 ProcessParameters);
286
287 /* Create the first Thread */
288 Status = RtlCreateUserThread(ProcessInfo->ProcessHandle,
289 ThreadSecurityDescriptor,
290 TRUE,
291 ProcessInfo->ImageInformation.ZeroBits,
292 ProcessInfo->ImageInformation.MaximumStackSize,
293 ProcessInfo->ImageInformation.CommittedStackSize,
294 ProcessInfo->ImageInformation.TransferAddress,
295 ProcessBasicInfo.PebBaseAddress,
296 &ProcessInfo->ThreadHandle,
297 &ProcessInfo->ClientId);
298 if (!NT_SUCCESS(Status))
299 {
300 DPRINT1("Could not Create Thread\n");
301 ZwClose(ProcessInfo->ProcessHandle);
302 ZwClose(hSection); /* Don't try to optimize this on top! */
303 return Status;
304 }
305
306 /* Close the Section Handle and return */
307 ZwClose(hSection);
308 return STATUS_SUCCESS;
309 }
310
311 /*
312 * @implemented
313 */
314 PVOID
315 NTAPI
316 RtlEncodePointer(IN PVOID Pointer)
317 {
318 ULONG Cookie;
319 NTSTATUS Status;
320
321 Status = ZwQueryInformationProcess(NtCurrentProcess(),
322 ProcessCookie,
323 &Cookie,
324 sizeof(Cookie),
325 NULL);
326
327 if(!NT_SUCCESS(Status))
328 {
329 DPRINT1("Failed to receive the process cookie! Status: 0x%lx\n", Status);
330 return Pointer;
331 }
332
333 return (PVOID)((ULONG_PTR)Pointer ^ Cookie);
334 }
335
336 /*
337 * @unimplemented
338 */
339 NTSYSAPI
340 VOID
341 NTAPI
342 RtlSetProcessIsCritical(
343 IN BOOLEAN NewValue,
344 OUT PBOOLEAN OldValue OPTIONAL,
345 IN BOOLEAN IsWinlogon)
346 {
347 //TODO
348 }
349
350 /* EOF */