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 DPRINT("RtlCreateUserProcess: %wZ\n", ImageFileName
);
200 /* Map and Load the File */
201 Status
= RtlpMapFile(ImageFileName
,
204 if(!NT_SUCCESS(Status
))
206 DPRINT1("Could not map process image\n");
210 /* Clean out the CurDir Handle if we won't use it */
211 if (!InheritHandles
) ProcessParameters
->CurrentDirectory
.Handle
= NULL
;
213 /* Use us as parent if none other specified */
214 if (!ParentProcess
) ParentProcess
= NtCurrentProcess();
216 /* Initialize the Object Attributes */
217 InitializeObjectAttributes(&ObjectAttributes
,
221 ProcessSecurityDescriptor
);
224 * If FLG_ENABLE_CSRDEBUG is used, then CSRSS is created under the
227 if ((RtlGetNtGlobalFlags() & FLG_ENABLE_CSRDEBUG
) &&
228 (wcsstr(ImageFileName
->Buffer
, L
"csrss")))
230 UNICODE_STRING DebugString
= RTL_CONSTANT_STRING(L
"\\WindowsSS");
231 InitializeObjectAttributes(&ObjectAttributes
,
235 ProcessSecurityDescriptor
);
238 /* Create Kernel Process Object */
239 Status
= ZwCreateProcess(&ProcessInfo
->ProcessHandle
,
247 if (!NT_SUCCESS(Status
))
249 DPRINT1("Could not create Kernel Process Object\n");
254 /* Get some information on the image */
255 Status
= ZwQuerySection(hSection
,
256 SectionImageInformation
,
257 &ProcessInfo
->ImageInformation
,
258 sizeof(SECTION_IMAGE_INFORMATION
),
260 if (!NT_SUCCESS(Status
))
262 DPRINT1("Could not query Section Info\n");
263 ZwClose(ProcessInfo
->ProcessHandle
);
268 /* Get some information about the process */
269 ZwQueryInformationProcess(ProcessInfo
->ProcessHandle
,
270 ProcessBasicInformation
,
272 sizeof(ProcessBasicInfo
),
274 if (!NT_SUCCESS(Status
))
276 DPRINT1("Could not query Process Info\n");
277 ZwClose(ProcessInfo
->ProcessHandle
);
282 /* Create Process Environment */
283 RtlpInitEnvironment(ProcessInfo
->ProcessHandle
,
284 ProcessBasicInfo
.PebBaseAddress
,
287 /* Create the first Thread */
288 Status
= RtlCreateUserThread(ProcessInfo
->ProcessHandle
,
289 ThreadSecurityDescriptor
,
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
))
300 DPRINT1("Could not Create Thread\n");
301 ZwClose(ProcessInfo
->ProcessHandle
);
302 ZwClose(hSection
); /* Don't try to optimize this on top! */
306 /* Close the Section Handle and return */
308 return STATUS_SUCCESS
;
316 RtlEncodePointer(IN PVOID Pointer
)
321 Status
= ZwQueryInformationProcess(NtCurrentProcess(),
327 if(!NT_SUCCESS(Status
))
329 DPRINT1("Failed to receive the process cookie! Status: 0x%lx\n", Status
);
333 return (PVOID
)((ULONG_PTR
)Pointer
^ Cookie
);
342 RtlSetProcessIsCritical(
344 OUT PBOOLEAN OldValue OPTIONAL
,
345 IN BOOLEAN IsWinlogon
)