[CSRSRV]
[reactos.git] / subsystems / win32 / csrsrv / procsup.c
index 08c806a..fe96ddb 100644 (file)
@@ -268,8 +268,8 @@ CsrLockedDereferenceProcess(PCSR_PROCESS CsrProcess)
     {
         /* Call the generic cleanup code */
         DPRINT1("Should kill process: %p\n", CsrProcess);
-        CsrProcessRefcountZero(CsrProcess);
         CsrAcquireProcessLock();
+        CsrProcessRefcountZero(CsrProcess);
     }
 }
 
@@ -305,7 +305,7 @@ CsrAllocateProcess(VOID)
     if (CsrProcessSequenceCount < 5) CsrProcessSequenceCount = 5;
 
     /* Increase the reference count */
-    CsrProcess->ReferenceCount++;
+    CsrLockedReferenceProcess(CsrProcess);
 
     /* Initialize the Thread List */
     InitializeListHead(&CsrProcess->ThreadList);
@@ -451,11 +451,8 @@ CsrRemoveProcess(IN PCSR_PROCESS CsrProcess)
  * The CsrInsertProcess routine inserts a CSR Process into the Process List
  * and notifies Server DLLs of the creation of a new CSR Process.
  *
- * @param Parent
- *        Optional pointer to the CSR Process creating this CSR Process.
- *
- * @param CurrentProcess
- *        Optional pointer to the current CSR Process.
+ * @param ParentProcess
+ *        Optional pointer to the Parent Process creating this CSR Process.
  *
  * @param CsrProcess
  *        Pointer to the CSR Process which is to be inserted.
@@ -467,17 +464,13 @@ CsrRemoveProcess(IN PCSR_PROCESS CsrProcess)
  *--*/
 VOID
 NTAPI
-CsrInsertProcess(IN PCSR_PROCESS Parent OPTIONAL,   // ParentProcess
-                 IN PCSR_PROCESS CurrentProcess OPTIONAL,   // CallingProcess
-                 IN PCSR_PROCESS CsrProcess)    // Process
+CsrInsertProcess(IN PCSR_PROCESS ParentProcess OPTIONAL,
+                 IN PCSR_PROCESS CsrProcess)
 {
     PCSR_SERVER_DLL ServerDll;
     ULONG i;
     ASSERT(ProcessStructureListLocked());
 
-    /* Set the parent */
-    CsrProcess->Parent = Parent;
-
     /* Insert it into the Root List */
     InsertTailList(&CsrRootProcess->ListLink, &CsrProcess->ListLink);
 
@@ -490,7 +483,7 @@ CsrInsertProcess(IN PCSR_PROCESS Parent OPTIONAL,   // ParentProcess
         /* Make sure it's valid and that it has callback */
         if (ServerDll && ServerDll->NewProcessCallback)
         {
-            ServerDll->NewProcessCallback(CurrentProcess, CsrProcess);
+            ServerDll->NewProcessCallback(ParentProcess, CsrProcess);
         }
     }
 }
@@ -706,7 +699,7 @@ CsrCreateProcess(IN HANDLE hProcess,
     CsrSetBackgroundPriority(CsrProcess);
 
     /* Insert the Process */
-    CsrInsertProcess(NULL, CurrentProcess, CsrProcess);
+    CsrInsertProcess(CurrentProcess, CsrProcess);
 
     /* Release lock and return */
     CsrReleaseProcessLock();
@@ -854,10 +847,13 @@ CsrDestroyProcess(IN PCLIENT_ID Cid,
         /* Get the current thread entry */
         CsrThread = CONTAINING_RECORD(NextEntry, CSR_THREAD, Link);
 
+        /* Move to the next entry */
+        NextEntry = NextEntry->Flink;
+
         /* Make sure the thread isn't already dead */
         if (CsrThread->Flags & CsrThreadTerminated)
         {
-            NextEntry = NextEntry->Flink;
+            /* Go the the next thread */
             continue;
         }
 
@@ -884,7 +880,6 @@ CsrDestroyProcess(IN PCLIENT_ID Cid,
 
         /* Dereference the thread */
         CsrLockedDereferenceThread(CsrThread);
-        NextEntry = CsrProcess->ThreadList.Flink;
     }
 
     /* Release the Process Lock and return success */
@@ -1005,7 +1000,7 @@ CsrGetProcessLuid(IN HANDLE hProcess OPTIONAL,
  * @param CsrThread
  *        Pointer to the CSR Thread to impersonate.
  *
- * @return TRUE if impersionation suceeded, false otherwise.
+ * @return TRUE if impersonation succeeded, false otherwise.
  *
  * @remarks Impersonation can be recursive.
  *
@@ -1077,7 +1072,7 @@ CsrLockProcessByClientId(IN HANDLE Pid,
 {
     PLIST_ENTRY NextEntry;
     PCSR_PROCESS CurrentProcess = NULL;
-    NTSTATUS Status;
+    NTSTATUS Status = STATUS_UNSUCCESSFUL;
 
     /* Acquire the lock */
     CsrAcquireProcessLock();
@@ -1100,7 +1095,7 @@ CsrLockProcessByClientId(IN HANDLE Pid,
             break;
         }
 
-        /* Next entry */
+        /* Move to the next entry */
         NextEntry = NextEntry->Flink;
     } while (NextEntry != &CsrRootProcess->ListLink);
 
@@ -1116,7 +1111,7 @@ CsrLockProcessByClientId(IN HANDLE Pid,
         CsrLockedReferenceProcess(CurrentProcess);
         *CsrProcess = CurrentProcess;
     }
-    
+
     /* Return the result */
     return Status;
 }
@@ -1279,12 +1274,12 @@ CsrShutdownProcesses(IN PLUID CallerLuid,
         /* Get the Process */
         CsrProcess = CONTAINING_RECORD(NextEntry, CSR_PROCESS, ListLink);
 
-        /* Remove the skip flag, set shutdown flags to 0*/
+        /* Move to the next entry */
+        NextEntry = NextEntry->Flink;
+
+        /* Remove the skip flag, set shutdown flags to 0 */
         CsrProcess->Flags &= ~CsrProcessSkipShutdown;
         CsrProcess->ShutdownFlags = 0;
-
-        /* Move to the next */
-        NextEntry = NextEntry->Flink;
     }
 
     /* Set shudown Priority */
@@ -1298,7 +1293,7 @@ CsrShutdownProcesses(IN PLUID CallerLuid,
         if (!CsrProcess) break;
 
         /* Increase reference to process */
-        CsrProcess->ReferenceCount++;
+        CsrLockedReferenceProcess(CsrProcess);
 
         FirstTry = TRUE;
         while (TRUE)
@@ -1394,12 +1389,12 @@ CsrEnumProcesses(IN CSRSS_ENUM_PROCESS_PROC EnumProc,
         /* Get the Process */
         CsrProcess = CONTAINING_RECORD(NextEntry, CSR_PROCESS, ListLink);
 
-        /* Remove the skip flag, set shutdown flags to 0*/
+        /* Move to the next entry */
+        NextEntry = NextEntry->Flink;
+
+        /* Remove the skip flag, set shutdown flags to 0 */
         CsrProcess->Flags &= ~CsrProcessSkipShutdown;
         CsrProcess->ShutdownFlags = 0;
-
-        /* Move to the next */
-        NextEntry = NextEntry->Flink;
     }
     
     /* Set shudown Priority */