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)
10 /* INCLUDES ****************************************************************/
17 /* INTERNAL FUNCTIONS *******************************************************/
21 RtlpMapFile(PUNICODE_STRING ImageFileName
,
25 OBJECT_ATTRIBUTES ObjectAttributes
;
28 IO_STATUS_BLOCK IoStatusBlock
;
30 /* Open the Image File */
31 InitializeObjectAttributes(&ObjectAttributes
,
33 Attributes
& (OBJ_CASE_INSENSITIVE
| OBJ_INHERIT
),
36 Status
= ZwOpenFile(&hFile
,
37 SYNCHRONIZE
| FILE_EXECUTE
| FILE_READ_DATA
,
40 FILE_SHARE_DELETE
| FILE_SHARE_READ
,
41 FILE_SYNCHRONOUS_IO_NONALERT
| FILE_NON_DIRECTORY_FILE
);
42 if (!NT_SUCCESS(Status
))
44 DPRINT1("Failed to read image file from disk\n");
48 /* Now create a section for this image */
49 Status
= ZwCreateSection(Section
,
56 if (!NT_SUCCESS(Status
))
58 DPRINT1("Failed to create section for image file\n");
65 /* FUNCTIONS ****************************************************************/
69 RtlpInitEnvironment(HANDLE ProcessHandle
,
71 PRTL_USER_PROCESS_PARAMETERS ProcessParameters
)
74 PVOID BaseAddress
= NULL
;
77 PWCHAR Environment
= 0;
78 DPRINT("RtlpInitEnvironment (hProcess: %p, Peb: %p Params: %p)\n",
79 ProcessHandle
, Peb
, ProcessParameters
);
81 /* Give the caller 1MB if he requested it */
82 if (ProcessParameters
->Flags
& RTL_USER_PROCESS_PARAMETERS_RESERVE_1MB
)
84 /* Give 1MB starting at 0x4 */
85 BaseAddress
= (PVOID
)4;
86 EnviroSize
= 1024 * 1024;
87 Status
= ZwAllocateVirtualMemory(ProcessHandle
,
93 if (!NT_SUCCESS(Status
))
95 DPRINT1("Failed to reserve 1MB of space \n");
100 /* Find the end of the Enviroment Block */
101 if ((Environment
= (PWCHAR
)ProcessParameters
->Environment
))
103 while (*Environment
++) while (*Environment
++);
105 /* Calculate the size of the block */
106 EnviroSize
= (ULONG
)((ULONG_PTR
)Environment
-
107 (ULONG_PTR
)ProcessParameters
->Environment
);
109 /* Allocate and Initialize new Environment Block */
111 Status
= ZwAllocateVirtualMemory(ProcessHandle
,
115 MEM_RESERVE
| MEM_COMMIT
,
117 if (!NT_SUCCESS(Status
))
119 DPRINT1("Failed to allocate Environment Block\n");
123 /* Write the Environment Block */
124 ZwWriteVirtualMemory(ProcessHandle
,
126 ProcessParameters
->Environment
,
131 ProcessParameters
->Environment
= BaseAddress
;
134 /* Now allocate space for the Parameter Block */
136 Size
= ProcessParameters
->MaximumLength
;
137 Status
= ZwAllocateVirtualMemory(ProcessHandle
,
143 if (!NT_SUCCESS(Status
))
145 DPRINT1("Failed to allocate Parameter Block\n");
149 /* Write the Parameter Block */
150 ZwWriteVirtualMemory(ProcessHandle
,
153 ProcessParameters
->Length
,
156 /* Write pointer to Parameter Block */
157 ZwWriteVirtualMemory(ProcessHandle
,
158 &Peb
->ProcessParameters
,
164 return STATUS_SUCCESS
;
170 * Creates a process and its initial thread.
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.
183 RtlCreateUserProcess(IN PUNICODE_STRING ImageFileName
,
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
)
196 PROCESS_BASIC_INFORMATION ProcessBasicInfo
;
197 OBJECT_ATTRIBUTES ObjectAttributes
;
198 UNICODE_STRING DebugString
= RTL_CONSTANT_STRING(L
"\\WindowsSS");
199 DPRINT("RtlCreateUserProcess: %wZ\n", ImageFileName
);
201 /* Map and Load the File */
202 Status
= RtlpMapFile(ImageFileName
,
205 if(!NT_SUCCESS(Status
))
207 DPRINT1("Could not map process image\n");
211 /* Clean out the CurDir Handle if we won't use it */
212 if (!InheritHandles
) ProcessParameters
->CurrentDirectory
.Handle
= NULL
;
214 /* Use us as parent if none other specified */
215 if (!ParentProcess
) ParentProcess
= NtCurrentProcess();
217 /* Initialize the Object Attributes */
218 InitializeObjectAttributes(&ObjectAttributes
,
222 ProcessSecurityDescriptor
);
225 * If FLG_ENABLE_CSRDEBUG is used, then CSRSS is created under the
228 if ((RtlGetNtGlobalFlags() & FLG_ENABLE_CSRDEBUG
) &&
229 (wcsstr(ImageFileName
->Buffer
, L
"csrss")))
231 ObjectAttributes
.ObjectName
= &DebugString
;
234 /* Create Kernel Process Object */
235 Status
= ZwCreateProcess(&ProcessInfo
->ProcessHandle
,
243 if (!NT_SUCCESS(Status
))
245 DPRINT1("Could not create Kernel Process Object\n");
250 /* Get some information on the image */
251 Status
= ZwQuerySection(hSection
,
252 SectionImageInformation
,
253 &ProcessInfo
->ImageInformation
,
254 sizeof(SECTION_IMAGE_INFORMATION
),
256 if (!NT_SUCCESS(Status
))
258 DPRINT1("Could not query Section Info\n");
259 ZwClose(ProcessInfo
->ProcessHandle
);
264 /* Get some information about the process */
265 ZwQueryInformationProcess(ProcessInfo
->ProcessHandle
,
266 ProcessBasicInformation
,
268 sizeof(ProcessBasicInfo
),
270 if (!NT_SUCCESS(Status
))
272 DPRINT1("Could not query Process Info\n");
273 ZwClose(ProcessInfo
->ProcessHandle
);
278 /* Create Process Environment */
279 RtlpInitEnvironment(ProcessInfo
->ProcessHandle
,
280 ProcessBasicInfo
.PebBaseAddress
,
283 /* Create the first Thread */
284 Status
= RtlCreateUserThread(ProcessInfo
->ProcessHandle
,
285 ThreadSecurityDescriptor
,
287 ProcessInfo
->ImageInformation
.ZeroBits
,
288 ProcessInfo
->ImageInformation
.MaximumStackSize
,
289 ProcessInfo
->ImageInformation
.CommittedStackSize
,
290 ProcessInfo
->ImageInformation
.TransferAddress
,
291 ProcessBasicInfo
.PebBaseAddress
,
292 &ProcessInfo
->ThreadHandle
,
293 &ProcessInfo
->ClientId
);
294 if (!NT_SUCCESS(Status
))
296 DPRINT1("Could not Create Thread\n");
297 ZwClose(ProcessInfo
->ProcessHandle
);
298 ZwClose(hSection
); /* Don't try to optimize this on top! */
302 /* Close the Section Handle and return */
304 return STATUS_SUCCESS
;
312 RtlEncodePointer(IN PVOID Pointer
)
317 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 RtlEncodeSystemPointer(IN PVOID Pointer
)
349 RtlSetProcessIsCritical(
351 OUT PBOOLEAN OldValue OPTIONAL
,
352 IN BOOLEAN IsWinlogon
)
359 RtlGetCurrentProcessorNumber(VOID
)
361 /* Forward to kernel */
362 return NtGetCurrentProcessorNumber();