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)
11 /* INCLUDES ****************************************************************/
18 /* INTERNAL FUNCTIONS *******************************************************/
22 RtlpMapFile(PUNICODE_STRING ImageFileName
,
26 OBJECT_ATTRIBUTES ObjectAttributes
;
29 IO_STATUS_BLOCK IoStatusBlock
;
31 /* Open the Image File */
32 InitializeObjectAttributes(&ObjectAttributes
,
34 Attributes
& (OBJ_CASE_INSENSITIVE
| OBJ_INHERIT
),
37 Status
= ZwOpenFile(&hFile
,
38 SYNCHRONIZE
| FILE_EXECUTE
| FILE_READ_DATA
,
41 FILE_SHARE_DELETE
| FILE_SHARE_READ
,
42 FILE_SYNCHRONOUS_IO_NONALERT
| FILE_NON_DIRECTORY_FILE
);
43 if (!NT_SUCCESS(Status
))
45 DPRINT1("Failed to read image file from disk, Status = 0x%08X\n", Status
);
49 /* Now create a section for this image */
50 Status
= ZwCreateSection(Section
,
57 if (!NT_SUCCESS(Status
))
59 DPRINT1("Failed to create section for image file, Status = 0x%08X\n", Status
);
66 /* FUNCTIONS ****************************************************************/
70 RtlpInitEnvironment(HANDLE ProcessHandle
,
72 PRTL_USER_PROCESS_PARAMETERS ProcessParameters
)
75 PVOID BaseAddress
= NULL
;
78 PWCHAR Environment
= 0;
79 DPRINT("RtlpInitEnvironment (hProcess: %p, Peb: %p Params: %p)\n",
80 ProcessHandle
, Peb
, ProcessParameters
);
82 /* Give the caller 1MB if he requested it */
83 if (ProcessParameters
->Flags
& RTL_USER_PROCESS_PARAMETERS_RESERVE_1MB
)
85 /* Give 1MB starting at 0x4 */
86 BaseAddress
= (PVOID
)4;
87 EnviroSize
= (1024 * 1024) - 256;
88 Status
= ZwAllocateVirtualMemory(ProcessHandle
,
94 if (!NT_SUCCESS(Status
))
96 DPRINT1("Failed to reserve 1MB of space \n");
101 /* Find the end of the Enviroment Block */
102 if ((Environment
= (PWCHAR
)ProcessParameters
->Environment
))
104 while (*Environment
++) while (*Environment
++);
106 /* Calculate the size of the block */
107 EnviroSize
= (ULONG
)((ULONG_PTR
)Environment
-
108 (ULONG_PTR
)ProcessParameters
->Environment
);
110 /* Allocate and Initialize new Environment Block */
112 Status
= ZwAllocateVirtualMemory(ProcessHandle
,
116 MEM_RESERVE
| MEM_COMMIT
,
118 if (!NT_SUCCESS(Status
))
120 DPRINT1("Failed to allocate Environment Block\n");
124 /* Write the Environment Block */
125 ZwWriteVirtualMemory(ProcessHandle
,
127 ProcessParameters
->Environment
,
132 ProcessParameters
->Environment
= BaseAddress
;
135 /* Now allocate space for the Parameter Block */
137 Size
= ProcessParameters
->MaximumLength
;
138 Status
= ZwAllocateVirtualMemory(ProcessHandle
,
144 if (!NT_SUCCESS(Status
))
146 DPRINT1("Failed to allocate Parameter Block\n");
150 /* Write the Parameter Block */
151 ZwWriteVirtualMemory(ProcessHandle
,
154 ProcessParameters
->Length
,
157 /* Write pointer to Parameter Block */
158 ZwWriteVirtualMemory(ProcessHandle
,
159 &Peb
->ProcessParameters
,
165 return STATUS_SUCCESS
;
171 * Creates a process and its initial thread.
174 * - The first thread is created suspended, so it needs a manual resume!!!
175 * - If ParentProcess is NULL, current process is used
176 * - ProcessParameters must be normalized
177 * - Attributes are object attribute flags used when opening the ImageFileName.
178 * Valid flags are OBJ_INHERIT and OBJ_CASE_INSENSITIVE.
184 RtlCreateUserProcess(IN PUNICODE_STRING ImageFileName
,
186 IN OUT PRTL_USER_PROCESS_PARAMETERS ProcessParameters
,
187 IN PSECURITY_DESCRIPTOR ProcessSecurityDescriptor OPTIONAL
,
188 IN PSECURITY_DESCRIPTOR ThreadSecurityDescriptor OPTIONAL
,
189 IN HANDLE ParentProcess OPTIONAL
,
190 IN BOOLEAN InheritHandles
,
191 IN HANDLE DebugPort OPTIONAL
,
192 IN HANDLE ExceptionPort OPTIONAL
,
193 OUT PRTL_USER_PROCESS_INFORMATION ProcessInfo
)
197 PROCESS_BASIC_INFORMATION ProcessBasicInfo
;
198 OBJECT_ATTRIBUTES ObjectAttributes
;
199 UNICODE_STRING DebugString
= RTL_CONSTANT_STRING(L
"\\WindowsSS");
200 DPRINT("RtlCreateUserProcess: %wZ\n", ImageFileName
);
202 /* Map and Load the File */
203 Status
= RtlpMapFile(ImageFileName
,
206 if (!NT_SUCCESS(Status
))
208 DPRINT1("Could not map process image\n");
212 /* Clean out the CurDir Handle if we won't use it */
213 if (!InheritHandles
) ProcessParameters
->CurrentDirectory
.Handle
= NULL
;
215 /* Use us as parent if none other specified */
216 if (!ParentProcess
) ParentProcess
= NtCurrentProcess();
218 /* Initialize the Object Attributes */
219 InitializeObjectAttributes(&ObjectAttributes
,
223 ProcessSecurityDescriptor
);
226 * If FLG_ENABLE_CSRDEBUG is used, then CSRSS is created under the
229 if ((RtlGetNtGlobalFlags() & FLG_ENABLE_CSRDEBUG
) &&
230 (wcsstr(ImageFileName
->Buffer
, L
"csrss")))
232 ObjectAttributes
.ObjectName
= &DebugString
;
235 /* Create Kernel Process Object */
236 Status
= ZwCreateProcess(&ProcessInfo
->ProcessHandle
,
244 if (!NT_SUCCESS(Status
))
246 DPRINT1("Could not create Kernel Process Object\n");
251 /* Get some information on the image */
252 Status
= ZwQuerySection(hSection
,
253 SectionImageInformation
,
254 &ProcessInfo
->ImageInformation
,
255 sizeof(SECTION_IMAGE_INFORMATION
),
257 if (!NT_SUCCESS(Status
))
259 DPRINT1("Could not query Section Info\n");
260 ZwClose(ProcessInfo
->ProcessHandle
);
265 /* Get some information about the process */
266 Status
= ZwQueryInformationProcess(ProcessInfo
->ProcessHandle
,
267 ProcessBasicInformation
,
269 sizeof(ProcessBasicInfo
),
271 if (!NT_SUCCESS(Status
))
273 DPRINT1("Could not query Process Info\n");
274 ZwClose(ProcessInfo
->ProcessHandle
);
279 /* Create Process Environment */
280 RtlpInitEnvironment(ProcessInfo
->ProcessHandle
,
281 ProcessBasicInfo
.PebBaseAddress
,
284 /* Create the first Thread */
285 Status
= RtlCreateUserThread(ProcessInfo
->ProcessHandle
,
286 ThreadSecurityDescriptor
,
288 ProcessInfo
->ImageInformation
.ZeroBits
,
289 ProcessInfo
->ImageInformation
.MaximumStackSize
,
290 ProcessInfo
->ImageInformation
.CommittedStackSize
,
291 ProcessInfo
->ImageInformation
.TransferAddress
,
292 ProcessBasicInfo
.PebBaseAddress
,
293 &ProcessInfo
->ThreadHandle
,
294 &ProcessInfo
->ClientId
);
295 if (!NT_SUCCESS(Status
))
297 DPRINT1("Could not Create Thread\n");
298 ZwClose(ProcessInfo
->ProcessHandle
);
299 ZwClose(hSection
); /* Don't try to optimize this on top! */
303 /* Close the Section Handle and return */
305 return STATUS_SUCCESS
;
313 RtlEncodePointer(IN PVOID Pointer
)
318 Status
= ZwQueryInformationProcess(NtCurrentProcess(),
323 if(!NT_SUCCESS(Status
))
325 DPRINT1("Failed to receive the process cookie! Status: 0x%lx\n", Status
);
329 return (PVOID
)((ULONG_PTR
)Pointer
^ Cookie
);
337 RtlDecodePointer(IN PVOID Pointer
)
339 return RtlEncodePointer(Pointer
);
347 RtlEncodeSystemPointer(IN PVOID Pointer
)
349 return (PVOID
)((ULONG_PTR
)Pointer
^ SharedUserData
->Cookie
);
356 * Implementation based on the documentation from:
357 * http://www.geoffchappell.com/studies/windows/win32/ntdll/api/rtl/peb/setprocessiscritical.htm
361 RtlSetProcessIsCritical(IN BOOLEAN NewValue
,
362 OUT PBOOLEAN OldValue OPTIONAL
,
363 IN BOOLEAN NeedBreaks
)
365 ULONG BreakOnTermination
;
367 /* Initialize to FALSE */
368 if (OldValue
) *OldValue
= FALSE
;
370 /* Fail, if the critical breaks flag is required but is not set */
372 !(NtCurrentPeb()->NtGlobalFlag
& FLG_ENABLE_SYSTEM_CRIT_BREAKS
))
374 return STATUS_UNSUCCESSFUL
;
377 /* Check if the caller wants the old value */
380 /* Query and return the old break on termination flag for the process */
381 ZwQueryInformationProcess(NtCurrentProcess(),
382 ProcessBreakOnTermination
,
386 *OldValue
= (BOOLEAN
)BreakOnTermination
;
389 /* Set the break on termination flag for the process */
390 BreakOnTermination
= NewValue
;
391 return ZwSetInformationProcess(NtCurrentProcess(),
392 ProcessBreakOnTermination
,
399 RtlGetCurrentProcessorNumber(VOID
)
401 /* Forward to kernel */
402 return NtGetCurrentProcessorNumber();