[FORMATTING]
[reactos.git] / reactos / lib / rtl / process.c
index ec70933..50e36eb 100644 (file)
@@ -1,17 +1,16 @@
-/* $Id$
- *
- * COPYRIGHT:       See COPYING in the top level directory
- * PROJECT:         ReactOS system libraries
- * FILE:            lib/ntdll/rtl/process.c
- * PURPOSE:         Process functions
- * PROGRAMMER:      Ariadne ( ariadne@xs4all.nl)
- * UPDATE HISTORY:
- *                  Created 01/11/98
+/*
+ * COPYRIGHT:         See COPYING in the top level directory
+ * PROJECT:           ReactOS system libraries
+ * FILE:              lib/rtl/process.c
+ * PURPOSE:           Process functions
+ * PROGRAMMER:        Alex Ionescu (alex@relsoft.net)
+ *                    Ariadne (ariadne@xs4all.nl)
+ *                    Eric Kohl
  */
 
 /* INCLUDES ****************************************************************/
 
-#include "rtl.h"
+#include <rtl.h>
 
 #define NDEBUG
 #include <debug.h>
@@ -19,7 +18,7 @@
 /* INTERNAL FUNCTIONS *******************************************************/
 
 NTSTATUS
-STDCALL
+NTAPI
 RtlpMapFile(PUNICODE_STRING ImageFileName,
             ULONG Attributes,
             PHANDLE Section)
@@ -44,10 +43,10 @@ RtlpMapFile(PUNICODE_STRING ImageFileName,
     if (!NT_SUCCESS(Status))
     {
         DPRINT1("Failed to read image file from disk\n");
-        return(Status);
+        return Status;
     }
-    
-    /* Now create a section for this image */    
+
+    /* Now create a section for this image */
     Status = ZwCreateSection(Section,
                              SECTION_ALL_ACCESS,
                              NULL,
@@ -67,22 +66,21 @@ RtlpMapFile(PUNICODE_STRING ImageFileName,
 /* FUNCTIONS ****************************************************************/
 
 NTSTATUS
-STDCALL
+NTAPI
 RtlpInitEnvironment(HANDLE ProcessHandle,
                     PPEB Peb,
                     PRTL_USER_PROCESS_PARAMETERS ProcessParameters)
 {
     NTSTATUS Status;
     PVOID BaseAddress = NULL;
-    ULONG EnviroSize;
-    ULONG Size;
+    SIZE_T EnviroSize;
+    SIZE_T Size;
     PWCHAR Environment = 0;
-    
-    DPRINT("RtlpInitEnvironment (hProcess: %lx, Peb: %p Params: %p)\n",
+    DPRINT("RtlpInitEnvironment (hProcess: %p, Peb: %p Params: %p)\n",
             ProcessHandle, Peb, ProcessParameters);
-            
+
     /* Give the caller 1MB if he requested it */
-    if (ProcessParameters->Flags & PPF_RESERVE_1MB)
+    if (ProcessParameters->Flags & RTL_USER_PROCESS_PARAMETERS_RESERVE_1MB)
     {
         /* Give 1MB starting at 0x4 */
         BaseAddress = (PVOID)4;
@@ -96,10 +94,10 @@ RtlpInitEnvironment(HANDLE ProcessHandle,
         if (!NT_SUCCESS(Status))
         {
             DPRINT1("Failed to reserve 1MB of space \n");
-            return(Status);
+            return Status;
         }
     }
-    
+
     /* Find the end of the Enviroment Block */
     if ((Environment = (PWCHAR)ProcessParameters->Environment))
     {
@@ -108,7 +106,6 @@ RtlpInitEnvironment(HANDLE ProcessHandle,
         /* Calculate the size of the block */
         EnviroSize = (ULONG)((ULONG_PTR)Environment -
                              (ULONG_PTR)ProcessParameters->Environment);
-        DPRINT("EnvironmentSize %ld\n", EnviroSize);
 
         /* Allocate and Initialize new Environment Block */
         Size = EnviroSize;
@@ -121,26 +118,23 @@ RtlpInitEnvironment(HANDLE ProcessHandle,
         if (!NT_SUCCESS(Status))
         {
             DPRINT1("Failed to allocate Environment Block\n");
-            return(Status);
+            return Status;
         }
-        
+
         /* Write the Environment Block */
         ZwWriteVirtualMemory(ProcessHandle,
                              BaseAddress,
                              ProcessParameters->Environment,
                              EnviroSize,
                              NULL);
-                             
+
         /* Save pointer */
         ProcessParameters->Environment = BaseAddress;
     }
-    
-    DPRINT("EnvironmentPointer %p\n", BaseAddress);
-    DPRINT("Ppb->MaximumLength %x\n", ProcessParameters->MaximumLength);
 
     /* Now allocate space for the Parameter Block */
     BaseAddress = NULL;
-    Size = ProcessParameters->MaximumLength;    
+    Size = ProcessParameters->MaximumLength;
     Status = ZwAllocateVirtualMemory(ProcessHandle,
                                      &BaseAddress,
                                      0,
@@ -150,16 +144,16 @@ RtlpInitEnvironment(HANDLE ProcessHandle,
     if (!NT_SUCCESS(Status))
     {
         DPRINT1("Failed to allocate Parameter Block\n");
-        return(Status);
+        return Status;
     }
-    
+
     /* Write the Parameter Block */
     ZwWriteVirtualMemory(ProcessHandle,
                          BaseAddress,
                          ProcessParameters,
                          ProcessParameters->Length,
                          NULL);
-  
+
     /* Write pointer to Parameter Block */
     ZwWriteVirtualMemory(ProcessHandle,
                          &Peb->ProcessParameters,
@@ -186,7 +180,7 @@ RtlpInitEnvironment(HANDLE ProcessHandle,
  * -Gunnar
  */
 NTSTATUS
-STDCALL
+NTAPI
 RtlCreateUserProcess(IN PUNICODE_STRING ImageFileName,
                      IN ULONG Attributes,
                      IN OUT PRTL_USER_PROCESS_PARAMETERS ProcessParameters,
@@ -202,7 +196,7 @@ RtlCreateUserProcess(IN PUNICODE_STRING ImageFileName,
     HANDLE hSection;
     PROCESS_BASIC_INFORMATION ProcessBasicInfo;
     OBJECT_ATTRIBUTES ObjectAttributes;
-
+    UNICODE_STRING DebugString = RTL_CONSTANT_STRING(L"\\WindowsSS");
     DPRINT("RtlCreateUserProcess: %wZ\n", ImageFileName);
 
     /* Map and Load the File */
@@ -214,17 +208,17 @@ RtlCreateUserProcess(IN PUNICODE_STRING ImageFileName,
         DPRINT1("Could not map process image\n");
         return Status;
     }
-    
+
     /* Clean out the CurDir Handle if we won't use it */
     if (!InheritHandles) ProcessParameters->CurrentDirectory.Handle = NULL;
-    
+
     /* Use us as parent if none other specified */
     if (!ParentProcess) ParentProcess = NtCurrentProcess();
-    
+
     /* Initialize the Object Attributes */
-    InitializeObjectAttributes(&ObjectAttributes, 
-                               NULL, 
-                               0, 
+    InitializeObjectAttributes(&ObjectAttributes,
+                               NULL,
+                               0,
                                NULL,
                                ProcessSecurityDescriptor);
 
@@ -235,15 +229,9 @@ RtlCreateUserProcess(IN PUNICODE_STRING ImageFileName,
     if ((RtlGetNtGlobalFlags() & FLG_ENABLE_CSRDEBUG) &&
         (wcsstr(ImageFileName->Buffer, L"csrss")))
     {
-        UNICODE_STRING DebugString = RTL_CONSTANT_STRING(L"\\WindowsSS");
-        InitializeObjectAttributes(&ObjectAttributes, 
-                                   &DebugString, 
-                                   0, 
-                                   NULL,
-                                   ProcessSecurityDescriptor);
+        ObjectAttributes.ObjectName = &DebugString;
     }
-    
-                               
+
     /* Create Kernel Process Object */
     Status = ZwCreateProcess(&ProcessInfo->ProcessHandle,
                              PROCESS_ALL_ACCESS,
@@ -257,9 +245,9 @@ RtlCreateUserProcess(IN PUNICODE_STRING ImageFileName,
     {
         DPRINT1("Could not create Kernel Process Object\n");
         ZwClose(hSection);
-        return(Status);
+        return Status;
     }
-    
+
     /* Get some information on the image */
     Status = ZwQuerySection(hSection,
                             SectionImageInformation,
@@ -271,7 +259,7 @@ RtlCreateUserProcess(IN PUNICODE_STRING ImageFileName,
         DPRINT1("Could not query Section Info\n");
         ZwClose(ProcessInfo->ProcessHandle);
         ZwClose(hSection);
-        return(Status);
+        return Status;
     }
 
     /* Get some information about the process */
@@ -285,8 +273,8 @@ RtlCreateUserProcess(IN PUNICODE_STRING ImageFileName,
         DPRINT1("Could not query Process Info\n");
         ZwClose(ProcessInfo->ProcessHandle);
         ZwClose(hSection);
-        return(Status);
-    }  
+        return Status;
+    }
 
     /* Create Process Environment */
     RtlpInitEnvironment(ProcessInfo->ProcessHandle,
@@ -311,10 +299,103 @@ RtlCreateUserProcess(IN PUNICODE_STRING ImageFileName,
         ZwClose(hSection); /* Don't try to optimize this on top! */
         return Status;
     }
-    
+
     /* Close the Section Handle and return */
     ZwClose(hSection);
     return STATUS_SUCCESS;
 }
 
-/* EOF */
+/*
+ * @implemented
+ */
+PVOID
+NTAPI
+RtlEncodePointer(IN PVOID Pointer)
+{
+    ULONG Cookie;
+    NTSTATUS Status;
+
+    Status = ZwQueryInformationProcess(NtCurrentProcess(),
+                                       ProcessCookie,
+                                       &Cookie,
+                                       sizeof(Cookie),
+                                       NULL);
+    if(!NT_SUCCESS(Status))
+    {
+        DPRINT1("Failed to receive the process cookie! Status: 0x%lx\n", Status);
+        return Pointer;
+    }
+
+    return (PVOID)((ULONG_PTR)Pointer ^ Cookie);
+}
+
+/*
+ * @implemented
+ */
+PVOID
+NTAPI
+RtlDecodePointer(IN PVOID Pointer)
+{
+    return RtlEncodePointer(Pointer);
+}
+
+/*
+ * @unimplemented
+ */
+PVOID
+NTAPI
+RtlEncodeSystemPointer(IN PVOID Pointer)
+{
+    UNIMPLEMENTED;
+    return NULL;
+}
+
+/*
+ * @implemented
+ *
+ * NOTES:
+ *   Implementation based on the documentation from:
+ *   http://www.geoffchappell.com/studies/windows/win32/ntdll/api/rtl/peb/setprocessiscritical.htm
+ */
+NTSTATUS
+NTAPI
+RtlSetProcessIsCritical(IN BOOLEAN NewValue,
+                        OUT PBOOLEAN OldValue OPTIONAL,
+                        IN BOOLEAN NeedBreaks)
+{
+    ULONG BreakOnTermination = FALSE;
+
+    if (OldValue)
+        *OldValue = FALSE;
+
+    /* Fail, if the critical breaks flag is required but is not set */
+    if (NeedBreaks == TRUE &&
+        !(NtCurrentPeb()->NtGlobalFlag & FLG_ENABLE_SYSTEM_CRIT_BREAKS))
+        return STATUS_UNSUCCESSFUL;
+
+    if (OldValue)
+    {
+        /* Query and return the old break on termination flag for the process */
+        ZwQueryInformationProcess(NtCurrentProcess(),
+                                  ProcessBreakOnTermination,
+                                  &BreakOnTermination,
+                                  sizeof(ULONG),
+                                  NULL);
+        *OldValue = (BOOLEAN)BreakOnTermination;
+    }
+
+    /* Set the break on termination flag for the process */
+    BreakOnTermination = NewValue;
+    return ZwSetInformationProcess(NtCurrentProcess(),
+                                   ProcessBreakOnTermination,
+                                   &BreakOnTermination,
+                                   sizeof(ULONG));
+}
+
+ULONG
+NTAPI
+RtlGetCurrentProcessorNumber(VOID)
+{
+    /* Forward to kernel */
+    return NtGetCurrentProcessorNumber();
+}