Author: tkreuzer
Date: Mon Nov 11 18:52:59 2013
New Revision: 60947

URL: http://svn.reactos.org/svn/reactos?rev=60947&view=rev
Log:
[NTOSKRNL]
Fix PsSetProcessWin32Process and PsSetThreadWin32Thread

Modified:
    trunk/reactos/include/ndk/psfuncs.h
    trunk/reactos/ntoskrnl/ntoskrnl.spec
    trunk/reactos/ntoskrnl/ps/process.c
    trunk/reactos/ntoskrnl/ps/thread.c
    trunk/reactos/win32ss/user/ntuser/main.c

Modified: trunk/reactos/include/ndk/psfuncs.h
URL: 
http://svn.reactos.org/svn/reactos/trunk/reactos/include/ndk/psfuncs.h?rev=60947&r1=60946&r2=60947&view=diff
==============================================================================
--- trunk/reactos/include/ndk/psfuncs.h [iso-8859-1] (original)
+++ trunk/reactos/include/ndk/psfuncs.h [iso-8859-1] Mon Nov 11 18:52:59 2013
@@ -56,19 +56,21 @@
 );
 
 NTKERNELAPI
-VOID
+NTSTATUS
 NTAPI
 PsSetProcessWin32Process(
     _Inout_ PEPROCESS Process,
-    _In_ PVOID Win32Process
-);
-
-NTKERNELAPI
-VOID
+    _In_opt_ PVOID Win32Process,
+    _In_opt_ PVOID OldWin32Process
+);
+
+NTKERNELAPI
+PVOID
 NTAPI
 PsSetThreadWin32Thread(
     _Inout_ PETHREAD Thread,
-    PVOID Win32Thread
+    _In_ PVOID Win32Thread,
+    _In_ PVOID OldWin32Thread
 );
 
 NTKERNELAPI

Modified: trunk/reactos/ntoskrnl/ntoskrnl.spec
URL: 
http://svn.reactos.org/svn/reactos/trunk/reactos/ntoskrnl/ntoskrnl.spec?rev=60947&r1=60946&r2=60947&view=diff
==============================================================================
--- trunk/reactos/ntoskrnl/ntoskrnl.spec        [iso-8859-1] (original)
+++ trunk/reactos/ntoskrnl/ntoskrnl.spec        [iso-8859-1] Mon Nov 11 
18:52:59 2013
@@ -1031,10 +1031,10 @@
 @ stdcall PsSetProcessPriorityByClass(ptr ptr)
 @ stdcall PsSetProcessPriorityClass(ptr long)
 @ stdcall PsSetProcessSecurityPort(ptr ptr)
-@ stdcall PsSetProcessWin32Process(ptr ptr)
+@ stdcall PsSetProcessWin32Process(ptr ptr ptr)
 @ stdcall PsSetProcessWindowStation(ptr ptr)
 @ stdcall PsSetThreadHardErrorsAreDisabled(ptr long)
-@ stdcall PsSetThreadWin32Thread(ptr ptr)
+@ stdcall PsSetThreadWin32Thread(ptr ptr ptr)
 @ stdcall PsTerminateSystemThread(long)
 @ extern PsThreadType _PsThreadType
 ;PsWrapApcWow64Thread

Modified: trunk/reactos/ntoskrnl/ps/process.c
URL: 
http://svn.reactos.org/svn/reactos/trunk/reactos/ntoskrnl/ps/process.c?rev=60947&r1=60946&r2=60947&view=diff
==============================================================================
--- trunk/reactos/ntoskrnl/ps/process.c [iso-8859-1] (original)
+++ trunk/reactos/ntoskrnl/ps/process.c [iso-8859-1] Mon Nov 11 18:52:59 2013
@@ -1240,12 +1240,58 @@
 /*
  * @implemented
  */
-VOID
-NTAPI
-PsSetProcessWin32Process(PEPROCESS Process,
-                         PVOID Win32Process)
-{
-    Process->Win32Process = Win32Process;
+NTSTATUS 
+NTAPI 
+PsSetProcessWin32Process(
+    _Inout_ PEPROCESS Process, 
+    _In_opt_ PVOID Win32Process, 
+    _In_opt_ PVOID OldWin32Process)
+{
+    NTSTATUS Status;
+
+    /* Assume success */
+    Status = STATUS_SUCCESS;
+
+    /* Lock the process */
+    KeEnterCriticalRegion();
+    ExAcquirePushLockExclusive(&Process->ProcessLock);
+
+    /* Check if we set a new win32 process */
+    if (Win32Process != NULL)
+    {
+        /* Check if the process is in the right state */
+        if (((Process->Flags & PSF_PROCESS_DELETE_BIT) == 0) &&
+            (Process->Win32Process == NULL))
+        {
+            /* Set the new win32 process */
+            Process->Win32Process = Win32Process;
+        }
+        else
+        {
+            /* Otherwise fail */
+            Status = STATUS_PROCESS_IS_TERMINATING;
+        }
+    }
+    else
+    {
+        /* Reset the win32 process, did the caller specify the correct old 
value? */
+        if (Process->Win32Process == OldWin32Process)
+        {
+            /* Yes, so reset the win32 process to NULL */
+            Process->Win32Process = 0;
+        }
+        else
+        {
+            /* Otherwise fail */
+            Status = STATUS_UNSUCCESSFUL;
+        }
+    }
+
+    /* Unlock the process */
+    ExReleasePushLockExclusive(&Process->ProcessLock);
+    KeLeaveCriticalRegion();
+
+    return Status;
 }
 
 /*

Modified: trunk/reactos/ntoskrnl/ps/thread.c
URL: 
http://svn.reactos.org/svn/reactos/trunk/reactos/ntoskrnl/ps/thread.c?rev=60947&r1=60946&r2=60947&view=diff
==============================================================================
--- trunk/reactos/ntoskrnl/ps/thread.c  [iso-8859-1] (original)
+++ trunk/reactos/ntoskrnl/ps/thread.c  [iso-8859-1] Mon Nov 11 18:52:59 2013
@@ -904,12 +904,27 @@
 /*
  * @implemented
  */
-VOID
-NTAPI
-PsSetThreadWin32Thread(IN PETHREAD Thread,
-                       IN PVOID Win32Thread)
-{
-    Thread->Tcb.Win32Thread = Win32Thread;
+PVOID
+NTAPI
+PsSetThreadWin32Thread(
+    _Inout_ PETHREAD Thread,
+    _In_ PVOID Win32Thread,
+    _In_ PVOID OldWin32Thread)
+{
+    /* Are we setting the win32 process? */
+    if (Win32Thread != NULL)
+    {
+        /* Just exchange it */
+        return InterlockedExchangePointer(&Thread->Tcb.Win32Thread,
+                                          Win32Thread);
+    }
+    else
+    {
+        /* We are resetting, only exchange when the old win32 thread matches */
+        return InterlockedCompareExchangePointer(&Thread->Tcb.Win32Thread,
+                                                 Win32Thread,
+                                                 OldWin32Thread);
+    }
 }
 
 NTSTATUS

Modified: trunk/reactos/win32ss/user/ntuser/main.c
URL: 
http://svn.reactos.org/svn/reactos/trunk/reactos/win32ss/user/ntuser/main.c?rev=60947&r1=60946&r2=60947&view=diff
==============================================================================
--- trunk/reactos/win32ss/user/ntuser/main.c    [iso-8859-1] (original)
+++ trunk/reactos/win32ss/user/ntuser/main.c    [iso-8859-1] Mon Nov 11 
18:52:59 2013
@@ -85,7 +85,7 @@
 
         RtlZeroMemory(ppiCurrent, sizeof(PROCESSINFO));
 
-        PsSetProcessWin32Process(Process, ppiCurrent);
+        PsSetProcessWin32Process(Process, ppiCurrent, NULL);
 
 #if DBG
         DbgInitDebugChannels();
@@ -237,7 +237,7 @@
 #endif
 
         /* Free the PROCESSINFO */
-        PsSetProcessWin32Process(Process, NULL);
+        PsSetProcessWin32Process(Process, NULL, ppiCurrent);
         ExFreePoolWithTag(ppiCurrent, USERTAG_PROCESSINFO);
     }
 
@@ -280,7 +280,7 @@
 
     /* Initialize the THREADINFO */
 
-    PsSetThreadWin32Thread(Thread, ptiCurrent);
+    PsSetThreadWin32Thread(Thread, ptiCurrent, NULL);
     IntReferenceThreadInfo(ptiCurrent);
     ptiCurrent->pEThread = Thread;
     ptiCurrent->ppi = PsGetCurrentProcessWin32Process();
@@ -463,7 +463,7 @@
 
        IntSetThreadDesktop(NULL, TRUE);
 
-       PsSetThreadWin32Thread(pti->pEThread, NULL);
+       PsSetThreadWin32Thread(pti->pEThread, NULL, pti);
        ExFreePoolWithTag(pti, USERTAG_THREADINFO);
     }
 }


Reply via email to