[CSRSRV] Only when CSRSRV is compiled in debugging mode, should we display debugging...
[reactos.git] / subsystems / win32 / csrsrv / procsup.c
index 451f269..30cde8a 100644 (file)
@@ -57,7 +57,7 @@ CsrSetToNormalPriority(VOID)
     NtSetInformationProcess(NtCurrentProcess(),
                             ProcessBasePriority,
                             &BasePriority,
-                            sizeof(KPRIORITY));
+                            sizeof(BasePriority));
 }
 
 /*++
@@ -79,7 +79,7 @@ VOID
 NTAPI
 CsrSetToShutdownPriority(VOID)
 {
-    KPRIORITY SetBasePriority = (8 + 1) + 6;
+    KPRIORITY BasePriority = (8 + 1) + 6;
     BOOLEAN Old;
 
     /* Get the shutdown privilege */
@@ -91,114 +91,11 @@ CsrSetToShutdownPriority(VOID)
         /* Set the Priority */
         NtSetInformationProcess(NtCurrentProcess(),
                                 ProcessBasePriority,
-                                &SetBasePriority,
-                                sizeof(KPRIORITY));
+                                &BasePriority,
+                                sizeof(BasePriority));
     }
 }
 
-/*++
- * @name FindProcessForShutdown
- *
- * The FindProcessForShutdown routine returns a CSR Process which is ready
- * to be shutdown, and sets the appropriate shutdown flags for it.
- *
- * @param CallerLuid
- *        Pointer to the LUID of the CSR Process calling this routine.
- *
- * @return Pointer to a CSR Process which is ready to be shutdown.
- *
- * @remarks None.
- *
- *--*/
-PCSR_PROCESS
-NTAPI
-FindProcessForShutdown(IN PLUID CallerLuid)
-{
-    PCSR_PROCESS CsrProcess, ReturnCsrProcess = NULL;
-    // PCSR_THREAD CsrThread;
-    NTSTATUS Status;
-    ULONG Level = 0;
-    LUID ProcessLuid;
-    LUID SystemLuid = SYSTEM_LUID;
-    // BOOLEAN IsSystemLuid = FALSE, IsOurLuid = FALSE;
-    PLIST_ENTRY NextEntry;
-
-    /* Set the List Pointers */
-    NextEntry = CsrRootProcess->ListLink.Flink;
-    while (NextEntry != &CsrRootProcess->ListLink)
-    {
-        /* Get the process */
-        CsrProcess = CONTAINING_RECORD(NextEntry, CSR_PROCESS, ListLink);
-
-        /* Move to the next entry */
-        NextEntry = NextEntry->Flink;
-
-        /* Skip this process if it's already been processed */
-        if (CsrProcess->Flags & CsrProcessSkipShutdown) continue;
-
-        /* Get the LUID of this Process */
-        Status = CsrGetProcessLuid(CsrProcess->ProcessHandle, &ProcessLuid);
-
-        /* Check if we didn't get access to the LUID */
-        if (Status == STATUS_ACCESS_DENIED)
-        {
-            /* FIXME: Check if we have any threads */
-/*
-            /\* Check if we have any threads *\/
-            if (CsrProcess->ThreadCount)
-            {
-                /\* Impersonate one of the threads and retry *\/
-                CsrThread = CONTAINING_RECORD(CsrProcess->ThreadList.Flink,
-                                              CSR_THREAD,
-                                              Link);
-                CsrImpersonateClient(CsrThread);
-                Status = CsrGetProcessLuid(NULL, &ProcessLuid);
-                CsrRevertToSelf();
-            }
-*/
-        }
-
-        if (!NT_SUCCESS(Status))
-        {
-            /* We didn't have access, so skip it */
-            CsrProcess->Flags |= CsrProcessSkipShutdown;
-            continue;
-        }
-
-        /* Check if this is the System LUID */
-        if ((/*IsSystemLuid =*/ RtlEqualLuid(&ProcessLuid, &SystemLuid)))
-        {
-            /* Mark this process */
-            CsrProcess->ShutdownFlags |= CsrShutdownSystem;
-        }
-        else if (!(/*IsOurLuid =*/ RtlEqualLuid(&ProcessLuid, CallerLuid)))
-        {
-            /* Our LUID doesn't match with the caller's */
-            CsrProcess->ShutdownFlags |= CsrShutdownOther;
-        }
-
-        /* Check if we're past the previous level */
-        // FIXME: if ((CsrProcess->ShutdownLevel > Level) || !(ReturnCsrProcess))
-        if (CsrProcess->ShutdownLevel > Level /* || !ReturnCsrProcess */)
-        {
-            /* Update the level */
-            Level = CsrProcess->ShutdownLevel;
-
-            /* Set the final process */
-            ReturnCsrProcess = CsrProcess;
-        }
-    }
-
-    /* Check if we found a process */
-    if (ReturnCsrProcess)
-    {
-        /* Skip this one next time */
-        ReturnCsrProcess->Flags |= CsrProcessSkipShutdown;
-    }
-
-    return ReturnCsrProcess;
-}
-
 /*++
  * @name CsrProcessRefcountZero
  *
@@ -591,11 +488,11 @@ CsrCreateProcess(IN HANDLE hProcess,
         }
     }
 
-    /* Set the Exception port for us */
+    /* Set the Exception Port for us */
     Status = NtSetInformationProcess(hProcess,
                                      ProcessExceptionPort,
                                      &CsrApiPort,
-                                     sizeof(HANDLE));
+                                     sizeof(CsrApiPort));
     if (!NT_SUCCESS(Status))
     {
         /* Failed */
@@ -652,7 +549,7 @@ CsrCreateProcess(IN HANDLE hProcess,
         Status = NtSetInformationProcess(hProcess,
                                          ProcessDebugPort,
                                          &CsrApiPort,
-                                         sizeof(HANDLE));
+                                         sizeof(CsrApiPort));
         ASSERT(NT_SUCCESS(Status));
         if (!NT_SUCCESS(Status))
         {
@@ -666,7 +563,7 @@ CsrCreateProcess(IN HANDLE hProcess,
     /* Get the Thread Create Time */
     Status = NtQueryInformationThread(hThread,
                                       ThreadTimes,
-                                      (PVOID)&KernelTimes,
+                                      &KernelTimes,
                                       sizeof(KernelTimes),
                                       NULL);
     if (!NT_SUCCESS(Status))
@@ -1048,8 +945,10 @@ CsrImpersonateClient(IN PCSR_THREAD CsrThread)
     if (!NT_SUCCESS(Status))
     {
         /* Failure */
+#ifdef CSR_DBG
         DPRINT1("CSRSS: Can't impersonate client thread - Status = %lx\n", Status);
         // if (Status != STATUS_BAD_IMPERSONATION_LEVEL) DbgBreakPoint();
+#endif
         return FALSE;
     }
 
@@ -1175,7 +1074,7 @@ CsrRevertToSelf(VOID)
     Status = NtSetInformationThread(NtCurrentThread(),
                                     ThreadImpersonationToken,
                                     &ImpersonationToken,
-                                    sizeof(HANDLE));
+                                    sizeof(ImpersonationToken));
 
     /* Return TRUE or FALSE */
     return NT_SUCCESS(Status);
@@ -1200,16 +1099,16 @@ VOID
 NTAPI
 CsrSetBackgroundPriority(IN PCSR_PROCESS CsrProcess)
 {
-    PROCESS_PRIORITY_CLASS PriorityClass;
+    PROCESS_FOREGROUND_BACKGROUND ProcessPriority;
 
     /* Set the Foreground bit off */
-    PriorityClass.Foreground = FALSE;
+    ProcessPriority.Foreground = FALSE;
 
-    /* Set the new Priority */
+    /* Set the new priority */
     NtSetInformationProcess(CsrProcess->ProcessHandle,
-                            ProcessPriorityClass,
-                            &PriorityClass,
-                            sizeof(PriorityClass));
+                            ProcessForegroundInformation,
+                            &ProcessPriority,
+                            sizeof(ProcessPriority));
 }
 
 /*++
@@ -1231,16 +1130,120 @@ VOID
 NTAPI
 CsrSetForegroundPriority(IN PCSR_PROCESS CsrProcess)
 {
-    PROCESS_PRIORITY_CLASS PriorityClass;
+    PROCESS_FOREGROUND_BACKGROUND ProcessPriority;
 
     /* Set the Foreground bit on */
-    PriorityClass.Foreground = TRUE;
+    ProcessPriority.Foreground = TRUE;
 
-    /* Set the new Priority */
+    /* Set the new priority */
     NtSetInformationProcess(CsrProcess->ProcessHandle,
-                            ProcessPriorityClass,
-                            &PriorityClass,
-                            sizeof(PriorityClass));
+                            ProcessForegroundInformation,
+                            &ProcessPriority,
+                            sizeof(ProcessPriority));
+}
+
+/*++
+ * @name FindProcessForShutdown
+ *
+ * The FindProcessForShutdown routine returns a CSR Process which is ready
+ * to be shutdown, and sets the appropriate shutdown flags for it.
+ *
+ * @param CallerLuid
+ *        Pointer to the LUID of the CSR Process calling this routine.
+ *
+ * @return Pointer to a CSR Process which is ready to be shutdown.
+ *
+ * @remarks None.
+ *
+ *--*/
+PCSR_PROCESS
+NTAPI
+FindProcessForShutdown(IN PLUID CallerLuid)
+{
+    PCSR_PROCESS CsrProcess, ReturnCsrProcess = NULL;
+    PCSR_THREAD CsrThread;
+    NTSTATUS Status;
+    ULONG Level = 0;
+    LUID ProcessLuid;
+    LUID SystemLuid = SYSTEM_LUID;
+    PLIST_ENTRY NextEntry;
+
+    /* Set the List Pointers */
+    NextEntry = CsrRootProcess->ListLink.Flink;
+    while (NextEntry != &CsrRootProcess->ListLink)
+    {
+        /* Get the process */
+        CsrProcess = CONTAINING_RECORD(NextEntry, CSR_PROCESS, ListLink);
+
+        /* Move to the next entry */
+        NextEntry = NextEntry->Flink;
+
+        /* Skip this process if it's already been processed */
+        if (CsrProcess->Flags & CsrProcessSkipShutdown) continue;
+
+        /* Get the LUID of this process */
+        Status = CsrGetProcessLuid(CsrProcess->ProcessHandle, &ProcessLuid);
+
+        /* Check if we didn't get access to the LUID */
+        if (Status == STATUS_ACCESS_DENIED)
+        {
+            /* Check if we have any threads */
+            if (CsrProcess->ThreadCount)
+            {
+                /* Impersonate one of the threads and retry */
+                CsrThread = CONTAINING_RECORD(CsrProcess->ThreadList.Flink,
+                                              CSR_THREAD,
+                                              Link);
+                if (CsrImpersonateClient(CsrThread))
+                {
+                    Status = CsrGetProcessLuid(NULL, &ProcessLuid);
+                    CsrRevertToSelf();
+                }
+                else
+                {
+                    Status = STATUS_BAD_IMPERSONATION_LEVEL;
+                }
+            }
+        }
+
+        if (!NT_SUCCESS(Status))
+        {
+            /* We didn't have access, so skip it */
+            CsrProcess->Flags |= CsrProcessSkipShutdown;
+            continue;
+        }
+
+        /* Check if this is the System LUID */
+        if (RtlEqualLuid(&ProcessLuid, &SystemLuid))
+        {
+            /* Mark this process */
+            CsrProcess->ShutdownFlags |= CsrShutdownSystem;
+        }
+        else if (!RtlEqualLuid(&ProcessLuid, CallerLuid))
+        {
+            /* Our LUID doesn't match with the caller's */
+            CsrProcess->ShutdownFlags |= CsrShutdownOther;
+        }
+
+        /* Check if we're past the previous level */
+        if ((CsrProcess->ShutdownLevel > Level) || !ReturnCsrProcess)
+        {
+            /* Update the level */
+            Level = CsrProcess->ShutdownLevel;
+
+            /* Set the final process */
+            ReturnCsrProcess = CsrProcess;
+        }
+    }
+
+    /* Check if we found a process */
+    if (ReturnCsrProcess)
+    {
+        /* Skip this one next time */
+        ReturnCsrProcess->Flags |= CsrProcessSkipShutdown;
+    }
+
+    return ReturnCsrProcess;
 }
 
 /*++
@@ -1273,7 +1276,7 @@ CsrShutdownProcesses(IN PLUID CallerLuid,
     BOOLEAN FirstTry;
     ULONG i;
     PCSR_SERVER_DLL ServerDll;
-    ULONG Result = 0; /* Intentionally invalid enumeratee to silence compiler warning */
+    ULONG Result = 0;
 
     /* Acquire process lock */
     CsrAcquireProcessLock();
@@ -1296,7 +1299,7 @@ CsrShutdownProcesses(IN PLUID CallerLuid,
         CsrProcess->ShutdownFlags = 0;
     }
 
-    /* Set shudown Priority */
+    /* Set shutdown Priority */
     CsrSetToShutdownPriority();
 
     /* Start looping */
@@ -1336,6 +1339,7 @@ CsrShutdownProcesses(IN PLUID CallerLuid,
                     }
                     else if (Result == CsrShutdownCancelled)
                     {
+#ifdef CSR_DBG
                         /* Check if this was a forced shutdown */
                         if (Flags & EWX_FORCE)
                         {
@@ -1343,6 +1347,7 @@ CsrShutdownProcesses(IN PLUID CallerLuid,
                                      CsrProcess->ClientId.UniqueProcess, i);
                             DbgBreakPoint();
                         }
+#endif
 
                         /* Shutdown was cancelled, unlock and exit */
                         CsrReleaseProcessLock();
@@ -1353,7 +1358,7 @@ CsrShutdownProcesses(IN PLUID CallerLuid,
             }
 
             /* No matches during the first try, so loop again */
-            if ((FirstTry) && (Result == CsrShutdownNonCsrProcess))
+            if (FirstTry && (Result == CsrShutdownNonCsrProcess))
             {
                 FirstTry = FALSE;
                 continue;
@@ -1364,7 +1369,8 @@ CsrShutdownProcesses(IN PLUID CallerLuid,
         }
 
         /* We've reached the final loop here, so dereference */
-        if (i == CSR_SERVER_DLL_MAX) CsrLockedDereferenceProcess(CsrProcess);
+        if (i == CSR_SERVER_DLL_MAX)
+            CsrLockedDereferenceProcess(CsrProcess);
     }
 
     /* Success path */
@@ -1378,100 +1384,6 @@ Quickie:
     return Status;
 }
 
-/* HACK: Temporary hack. This is really "CsrShutdownProcesses", mostly. Used by winsrv */
-#if 0
-NTSTATUS
-WINAPI
-CsrEnumProcesses(IN CSRSS_ENUM_PROCESS_PROC EnumProc,
-                 IN PVOID Context)
-{
-    PVOID* RealContext = (PVOID*)Context;
-    PLUID CallerLuid = RealContext[0];
-    PCSR_PROCESS CsrProcess = NULL;
-    NTSTATUS Status = STATUS_UNSUCCESSFUL;
-    BOOLEAN FirstTry;
-    PLIST_ENTRY NextEntry;
-    ULONG Result = 0;
-
-    /* Acquire process lock */
-    CsrAcquireProcessLock();
-
-    /* Get the list pointers */
-    NextEntry = CsrRootProcess->ListLink.Flink;
-    while (NextEntry != &CsrRootProcess->ListLink)
-    {
-        /* Get the Process */
-        CsrProcess = CONTAINING_RECORD(NextEntry, CSR_PROCESS, ListLink);
-
-        /* Move to the next entry */
-        NextEntry = NextEntry->Flink;
-
-        /* Remove the skip flag, set shutdown flags to 0 */
-        CsrProcess->Flags &= ~CsrProcessSkipShutdown;
-        CsrProcess->ShutdownFlags = 0;
-    }
-
-    /* Set shudown Priority */
-    CsrSetToShutdownPriority();
-
-    /* Loop all processes */
-    //DPRINT1("Enumerating for LUID: %lx %lx\n", CallerLuid->HighPart, CallerLuid->LowPart);
-
-    /* Start looping */
-    while (TRUE)
-    {
-        /* Find the next process to shutdown */
-        FirstTry = TRUE;
-        if (!(CsrProcess = FindProcessForShutdown(CallerLuid)))
-        {
-            /* Done, quit */
-            CsrReleaseProcessLock();
-            Status = STATUS_SUCCESS;
-            goto Quickie;
-        }
-
-LoopAgain:
-        /* Release the lock, make the callback, and acquire it back */
-        //DPRINT1("Found process: %lx\n", CsrProcess->ClientId.UniqueProcess);
-        CsrReleaseProcessLock();
-        Result = (ULONG)EnumProc(CsrProcess, (PVOID)((ULONG_PTR)Context | FirstTry));
-        CsrAcquireProcessLock();
-
-        /* Check the result */
-        //DPRINT1("Result: %d\n", Result);
-        if (Result == CsrShutdownCsrProcess)
-        {
-            /* The callback unlocked the process */
-            break;
-        }
-        else if (Result == CsrShutdownNonCsrProcess)
-        {
-            /* A non-CSR process, the callback didn't touch it */
-            //continue;
-        }
-        else if (Result == CsrShutdownCancelled)
-        {
-            /* Shutdown was cancelled, unlock and exit */
-            CsrReleaseProcessLock();
-            Status = STATUS_CANCELLED;
-            goto Quickie;
-        }
-
-        /* No matches during the first try, so loop again */
-        if (FirstTry && Result == CsrShutdownNonCsrProcess)
-        {
-            FirstTry = FALSE;
-            goto LoopAgain;
-        }
-    }
-
-Quickie:
-    /* Return to normal priority */
-    CsrSetToNormalPriority();
-    return Status;
-}
-#endif
-
 /*++
  * @name CsrUnlockProcess
  * @implemented NT4