2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS CSR SubSystem
4 * FILE: subsystems/win32/csrss/csrsrv/procsup.c
5 * PURPOSE: CSR Server DLL Process Management
6 * PROGRAMMERS: ReactOS Portable Systems Group
7 * Alex Ionescu (alex@relsoft.net)
10 /* INCLUDES *******************************************************************/
17 /* GLOBALS ********************************************************************/
19 RTL_CRITICAL_SECTION CsrProcessLock
;
20 PCSR_PROCESS CsrRootProcess
= NULL
;
21 SECURITY_QUALITY_OF_SERVICE CsrSecurityQos
=
23 sizeof(SECURITY_QUALITY_OF_SERVICE
),
24 SecurityImpersonation
,
25 SECURITY_STATIC_TRACKING
,
28 ULONG CsrProcessSequenceCount
= 5;
29 extern ULONG CsrTotalPerProcessDataLength
; // remove 'extern' if not needed.
32 /* PRIVATE FUNCTIONS **********************************************************/
35 * @name CsrpSetToNormalPriority
37 * The CsrpSetToNormalPriority routine sets the current NT Process'
38 * priority to the normal priority for CSR Processes.
44 * @remarks The "Normal" Priority corresponds to the Normal Forground
45 * Priority (9) plus a boost of 4.
50 CsrSetToNormalPriority(VOID
) // CsrpSetToNormalPriority
52 KPRIORITY BasePriority
= (8 + 1) + 4;
54 /* Set the Priority */
55 NtSetInformationProcess(NtCurrentProcess(),
62 * @name CsrpSetToShutdownPriority
64 * The CsrpSetToShutdownPriority routine sets the current NT Process'
65 * priority to the boosted priority for CSR Processes doing shutdown.
66 * Additonally, it acquires the Shutdown Privilege required for shutdown.
72 * @remarks The "Shutdown" Priority corresponds to the Normal Forground
73 * Priority (9) plus a boost of 6.
78 CsrSetToShutdownPriority(VOID
) // CsrpSetToShutdownPriority
80 KPRIORITY SetBasePriority
= (8 + 1) + 6;
83 /* Get the shutdown privilege */
84 if (NT_SUCCESS(RtlAdjustPrivilege(SE_SHUTDOWN_PRIVILEGE
,
89 /* Set the Priority */
90 NtSetInformationProcess(NtCurrentProcess(),
98 * @name FindProcessForShutdown
100 * The FindProcessForShutdown routine returns a CSR Process which is ready
101 * to be shutdown, and sets the appropriate shutdown flags for it.
104 * Pointer to the LUID of the CSR Process calling this routine.
106 * @return Pointer to a CSR Process which is ready to be shutdown.
113 FindProcessForShutdown(IN PLUID CallerLuid
)
115 PCSR_PROCESS CsrProcess
, ReturnCsrProcess
= NULL
;
116 // PCSR_THREAD CsrThread;
120 LUID SystemLuid
= SYSTEM_LUID
;
121 // BOOLEAN IsSystemLuid = FALSE, IsOurLuid = FALSE;
122 PLIST_ENTRY NextEntry
;
124 /* Set the List Pointers */
125 NextEntry
= CsrRootProcess
->ListLink
.Flink
;
126 while (NextEntry
!= &CsrRootProcess
->ListLink
)
128 /* Get the process */
129 CsrProcess
= CONTAINING_RECORD(NextEntry
, CSR_PROCESS
, ListLink
);
131 /* Move to the next entry */
132 NextEntry
= NextEntry
->Flink
;
134 /* Skip this process if it's already been processed */
135 if (CsrProcess
->Flags
& CsrProcessSkipShutdown
) continue;
137 /* Get the LUID of this Process */
138 Status
= CsrGetProcessLuid(CsrProcess
->ProcessHandle
, &ProcessLuid
);
140 /* Check if we didn't get access to the LUID */
141 if (Status
== STATUS_ACCESS_DENIED
)
143 /* FIXME: Check if we have any threads */
145 /\* Check if we have any threads *\/
146 if (CsrProcess->ThreadCount)
148 /\* Impersonate one of the threads and retry *\/
149 CsrThread = CONTAINING_RECORD(CsrProcess->ThreadList.Flink,
152 CsrImpersonateClient(CsrThread);
153 Status = CsrGetProcessLuid(NULL, &ProcessLuid);
159 if (!NT_SUCCESS(Status
))
161 /* We didn't have access, so skip it */
162 CsrProcess
->Flags
|= CsrProcessSkipShutdown
;
166 /* Check if this is the System LUID */
167 if ((/*IsSystemLuid =*/ RtlEqualLuid(&ProcessLuid
, &SystemLuid
)))
169 /* Mark this process */
170 CsrProcess
->ShutdownFlags
|= CsrShutdownSystem
;
172 else if (!(/*IsOurLuid =*/ RtlEqualLuid(&ProcessLuid
, CallerLuid
)))
174 /* Our LUID doesn't match with the caller's */
175 CsrProcess
->ShutdownFlags
|= CsrShutdownOther
;
178 /* Check if we're past the previous level */
179 // FIXME: if ((CsrProcess->ShutdownLevel > Level) || !(ReturnCsrProcess))
180 if (CsrProcess
->ShutdownLevel
> Level
/* || !ReturnCsrProcess */)
182 /* Update the level */
183 Level
= CsrProcess
->ShutdownLevel
;
185 /* Set the final process */
186 ReturnCsrProcess
= CsrProcess
;
190 /* Check if we found a process */
191 if (ReturnCsrProcess
)
193 /* Skip this one next time */
194 ReturnCsrProcess
->Flags
|= CsrProcessSkipShutdown
;
197 return ReturnCsrProcess
;
201 * @name CsrProcessRefcountZero
203 * The CsrProcessRefcountZero routine is executed when a CSR Process has lost
204 * all its active references. It removes and de-allocates the CSR Process.
207 * Pointer to the CSR Process that is to be deleted.
211 * @remarks Do not call this routine. It is reserved for the internal
212 * thread management routines when a CSR Process has lost all
215 * This routine is called with the Process Lock held.
220 CsrProcessRefcountZero(IN PCSR_PROCESS CsrProcess
)
222 ASSERT(ProcessStructureListLocked());
224 /* Remove the Process from the list */
225 CsrRemoveProcess(CsrProcess
);
227 /* Check if there's a session */
228 if (CsrProcess
->NtSession
)
230 /* Dereference the Session */
231 CsrDereferenceNtSession(CsrProcess
->NtSession
, 0);
234 /* Close the Client Port if there is one */
235 if (CsrProcess
->ClientPort
) NtClose(CsrProcess
->ClientPort
);
237 /* Close the process handle */
238 NtClose(CsrProcess
->ProcessHandle
);
240 /* Free the Proces Object */
241 CsrDeallocateProcess(CsrProcess
);
245 * @name CsrLockedDereferenceProcess
247 * The CsrLockedDereferenceProcess dereferences a CSR Process while the
248 * Process Lock is already being held.
251 * Pointer to the CSR Process to be dereferenced.
255 * @remarks This routine will return with the Process Lock held.
260 CsrLockedDereferenceProcess(PCSR_PROCESS CsrProcess
)
264 /* Decrease reference count */
265 LockCount
= --CsrProcess
->ReferenceCount
;
266 ASSERT(LockCount
>= 0);
269 /* Call the generic cleanup code */
270 DPRINT1("Should kill process: %p\n", CsrProcess
);
271 CsrProcessRefcountZero(CsrProcess
);
272 CsrAcquireProcessLock();
277 * @name CsrAllocateProcess
280 * The CsrAllocateProcess routine allocates a new CSR Process object.
282 * @return Pointer to the newly allocated CSR Process.
289 CsrAllocateProcess(VOID
)
291 PCSR_PROCESS CsrProcess
;
294 /* Calculate the amount of memory this should take */
295 TotalSize
= sizeof(CSR_PROCESS
) +
296 (CSR_SERVER_DLL_MAX
* sizeof(PVOID
)) +
297 CsrTotalPerProcessDataLength
;
299 /* Allocate a Process */
300 CsrProcess
= RtlAllocateHeap(CsrHeap
, HEAP_ZERO_MEMORY
, TotalSize
);
301 if (!CsrProcess
) return NULL
;
303 /* Handle the Sequence Number and protect against overflow */
304 CsrProcess
->SequenceNumber
= CsrProcessSequenceCount
++;
305 if (CsrProcessSequenceCount
< 5) CsrProcessSequenceCount
= 5;
307 /* Increase the reference count */
308 CsrProcess
->ReferenceCount
++;
310 /* Initialize the Thread List */
311 InitializeListHead(&CsrProcess
->ThreadList
);
313 /* Return the Process */
318 * @name CsrLockedReferenceProcess
320 * The CsrLockedReferenceProcess references a CSR Process while the
321 * Process Lock is already being held.
324 * Pointer to the CSR Process to be referenced.
328 * @remarks This routine will return with the Process Lock held.
333 CsrLockedReferenceProcess(IN PCSR_PROCESS CsrProcess
)
335 /* Increment the reference count */
336 ++CsrProcess
->ReferenceCount
;
340 * @name CsrInitializeProcessStructure
343 * The CsrInitializeProcessStructure routine sets up support for CSR Processes
348 * @return STATUS_SUCCESS in case of success, STATUS_UNSUCCESSFUL
356 CsrInitializeProcessStructure(VOID
)
361 /* Initialize the Lock */
362 Status
= RtlInitializeCriticalSection(&CsrProcessLock
);
363 if (!NT_SUCCESS(Status
)) return Status
;
365 /* Set up the Root Process */
366 CsrRootProcess
= CsrAllocateProcess();
367 if (!CsrRootProcess
) return STATUS_NO_MEMORY
;
369 /* Set up the minimal information for it */
370 InitializeListHead(&CsrRootProcess
->ListLink
);
371 CsrRootProcess
->ProcessHandle
= (HANDLE
)-1;
372 CsrRootProcess
->ClientId
= NtCurrentTeb()->ClientId
;
374 /* Initialize the Thread Hash List */
375 for (i
= 0; i
< 256; i
++) InitializeListHead(&CsrThreadHashTable
[i
]);
377 /* Initialize the Wait Lock */
378 return RtlInitializeCriticalSection(&CsrWaitListsLock
);
382 * @name CsrDeallocateProcess
384 * The CsrDeallocateProcess frees the memory associated with a CSR Process.
387 * Pointer to the CSR Process to be freed.
391 * @remarks Do not call this routine. It is reserved for the internal
392 * thread management routines when a CSR Process has been cleanly
393 * dereferenced and killed.
398 CsrDeallocateProcess(IN PCSR_PROCESS CsrProcess
)
400 /* Free the process object from the heap */
401 RtlFreeHeap(CsrHeap
, 0, CsrProcess
);
405 * @name CsrRemoveProcess
407 * The CsrRemoveProcess function undoes a CsrInsertProcess operation and
408 * removes the CSR Process from the Process List and notifies Server DLLs
412 * Pointer to the CSR Process to remove.
421 CsrRemoveProcess(IN PCSR_PROCESS CsrProcess
)
423 PCSR_SERVER_DLL ServerDll
;
425 ASSERT(ProcessStructureListLocked());
427 /* Remove us from the Process List */
428 RemoveEntryList(&CsrProcess
->ListLink
);
430 /* Release the lock */
431 CsrReleaseProcessLock();
433 /* Loop every Server DLL */
434 for (i
= 0; i
< CSR_SERVER_DLL_MAX
; i
++)
436 /* Get the Server DLL */
437 ServerDll
= CsrLoadedServerDll
[i
];
439 /* Check if it's valid and if it has a Disconnect Callback */
440 if ((ServerDll
) && (ServerDll
->DisconnectCallback
))
443 ServerDll
->DisconnectCallback(CsrProcess
);
449 * @name CsrInsertProcess
451 * The CsrInsertProcess routine inserts a CSR Process into the Process List
452 * and notifies Server DLLs of the creation of a new CSR Process.
455 * Optional pointer to the CSR Process creating this CSR Process.
457 * @param CurrentProcess
458 * Optional pointer to the current CSR Process.
461 * Pointer to the CSR Process which is to be inserted.
470 CsrInsertProcess(IN PCSR_PROCESS Parent OPTIONAL
,
471 IN PCSR_PROCESS CurrentProcess OPTIONAL
,
472 IN PCSR_PROCESS CsrProcess
)
474 PCSR_SERVER_DLL ServerDll
;
476 ASSERT(ProcessStructureListLocked());
479 CsrProcess
->Parent
= Parent
;
481 /* Insert it into the Root List */
482 InsertTailList(&CsrRootProcess
->ListLink
, &CsrProcess
->ListLink
);
484 /* Notify the Server DLLs */
485 for (i
= 0; i
< CSR_SERVER_DLL_MAX
; i
++)
487 /* Get the current Server DLL */
488 ServerDll
= CsrLoadedServerDll
[i
];
490 /* Make sure it's valid and that it has callback */
491 if ((ServerDll
) && (ServerDll
->NewProcessCallback
))
493 ServerDll
->NewProcessCallback(CurrentProcess
, CsrProcess
);
499 /* PUBLIC FUNCTIONS ***********************************************************/
502 * @name CsrGetProcessLuid
505 * Do nothing for 500ms.
508 * Optional handle to the process whose LUID should be returned.
511 * Pointer to a LUID Pointer which will receive the CSR Process' LUID
513 * @return STATUS_SUCCESS in case of success, STATUS_UNSUCCESSFUL
516 * @remarks If hProcess is not supplied, then the current thread's token will
517 * be used. If that too is missing, then the current process' token
523 CsrGetProcessLuid(HANDLE hProcess OPTIONAL
,
526 HANDLE hToken
= NULL
;
529 PTOKEN_STATISTICS TokenStats
;
531 /* Check if we have a handle to a CSR Process */
534 /* We don't, so try opening the Thread's Token */
535 Status
= NtOpenThreadToken(NtCurrentThread(),
540 /* Check for success */
541 if (!NT_SUCCESS(Status
))
543 /* If we got some other failure, then return and quit */
544 if (Status
!= STATUS_NO_TOKEN
) return Status
;
546 /* We don't have a Thread Token, use a Process Token */
547 hProcess
= NtCurrentProcess();
552 /* Check if we have a token by now */
555 /* No token yet, so open the Process Token */
556 Status
= NtOpenProcessToken(hProcess
,
559 if (!NT_SUCCESS(Status
))
561 /* Still no token, return the error */
566 /* Now get the size we'll need for the Token Information */
567 Status
= NtQueryInformationToken(hToken
,
573 /* Allocate memory for the Token Info */
574 if (!(TokenStats
= RtlAllocateHeap(CsrHeap
, 0, Length
)))
576 /* Fail and close the token */
578 return STATUS_NO_MEMORY
;
581 /* Now query the information */
582 Status
= NtQueryInformationToken(hToken
,
588 /* Close the handle */
591 /* Check for success */
592 if (NT_SUCCESS(Status
))
594 /* Return the LUID */
595 *Luid
= TokenStats
->AuthenticationId
;
598 /* Free the query information */
599 RtlFreeHeap(CsrHeap
, 0, TokenStats
);
601 /* Return the Status */
606 * @name CsrImpersonateClient
609 * The CsrImpersonateClient will impersonate the given CSR Thread.
612 * Pointer to the CSR Thread to impersonate.
614 * @return TRUE if impersionation suceeded, false otherwise.
616 * @remarks Impersonation can be recursive.
621 CsrImpersonateClient(IN PCSR_THREAD CsrThread
)
624 PCSR_THREAD CurrentThread
= CsrGetClientThread();
626 /* Use the current thread if none given */
627 if (!CsrThread
) CsrThread
= CurrentThread
;
629 /* Still no thread, something is wrong */
637 Status
= NtImpersonateThread(NtCurrentThread(),
638 CsrThread
->ThreadHandle
,
641 if (!NT_SUCCESS(Status
))
645 DPRINT1("CSRSS: Can't impersonate client thread - Status = %lx\n", Status);
646 if (Status != STATUS_BAD_IMPERSONATION_LEVEL) DbgBreakPoint();
651 /* Increase the impersonation count for the current thread */
652 if (CurrentThread
) ++CurrentThread
->ImpersonationCount
;
659 * @name CsrRevertToSelf
662 * The CsrRevertToSelf routine will attempt to remove an active impersonation.
666 * @return TRUE if the reversion was succesful, false otherwise.
668 * @remarks Impersonation can be recursive; as such, the impersonation token
669 * will only be deleted once the CSR Thread's impersonaton count
675 CsrRevertToSelf(VOID
)
678 PCSR_THREAD CurrentThread
= CsrGetClientThread();
679 HANDLE ImpersonationToken
= NULL
;
681 /* Check if we have a Current Thread */
684 /* Make sure impersonation is on */
685 if (!CurrentThread
->ImpersonationCount
)
687 // DPRINT1("CSRSS: CsrRevertToSelf called while not impersonating\n");
691 else if (--CurrentThread
->ImpersonationCount
> 0)
693 /* Success; impersonation count decreased but still not zero */
698 /* Impersonation has been totally removed, revert to ourselves */
699 Status
= NtSetInformationThread(NtCurrentThread(),
700 ThreadImpersonationToken
,
704 /* Return TRUE or FALSE */
705 return NT_SUCCESS(Status
);
709 * @name CsrDereferenceProcess
712 * The CsrDereferenceProcess routine removes a reference from a CSR Process.
715 * Pointer to the CSR Process to dereference.
719 * @remarks If the reference count has reached zero (ie: the CSR Process has
720 * no more active references), it will be deleted.
725 CsrDereferenceProcess(IN PCSR_PROCESS CsrProcess
)
729 /* Acquire process lock */
730 CsrAcquireProcessLock();
732 /* Decrease reference count */
733 LockCount
= --CsrProcess
->ReferenceCount
;
734 ASSERT(LockCount
>= 0);
737 /* Call the generic cleanup code */
738 CsrProcessRefcountZero(CsrProcess
);
742 /* Just release the lock */
743 CsrReleaseProcessLock();
748 * @name CsrDestroyProcess
751 * The CsrDestroyProcess routine destroys the CSR Process corresponding to
755 * Pointer to the Client ID Structure corresponding to the CSR
756 * Process which is about to be destroyed.
761 * @return STATUS_SUCCESS in case of success, STATUS_THREAD_IS_TERMINATING
762 * if the CSR Process is already terminating.
769 CsrDestroyProcess(IN PCLIENT_ID Cid
,
770 IN NTSTATUS ExitStatus
)
772 PCSR_THREAD CsrThread
;
773 PCSR_PROCESS CsrProcess
;
774 CLIENT_ID ClientId
= *Cid
;
775 PLIST_ENTRY NextEntry
;
778 CsrAcquireProcessLock();
780 /* Find the thread */
781 CsrThread
= CsrLocateThreadByClientId(&CsrProcess
, &ClientId
);
783 /* Make sure we got one back, and that it's not already gone */
784 if (!(CsrThread
) || (CsrProcess
->Flags
& CsrProcessTerminating
))
786 /* Release the lock and return failure */
787 CsrReleaseProcessLock();
788 return STATUS_THREAD_IS_TERMINATING
;
791 /* Set the terminated flag */
792 CsrProcess
->Flags
|= CsrProcessTerminating
;
794 /* Get the List Pointers */
795 NextEntry
= CsrProcess
->ThreadList
.Flink
;
796 while (NextEntry
!= &CsrProcess
->ThreadList
)
798 /* Get the current thread entry */
799 CsrThread
= CONTAINING_RECORD(NextEntry
, CSR_THREAD
, Link
);
801 /* Make sure the thread isn't already dead */
802 if (CsrThread
->Flags
& CsrThreadTerminated
)
804 NextEntry
= NextEntry
->Flink
;
808 /* Set the Terminated flag */
809 CsrThread
->Flags
|= CsrThreadTerminated
;
811 /* Acquire the Wait Lock */
812 CsrAcquireWaitLock();
814 /* Do we have an active wait block? */
815 if (CsrThread
->WaitBlock
)
817 /* Notify waiters of termination */
818 CsrNotifyWaitBlock(CsrThread
->WaitBlock
,
822 CsrProcessTerminating
,
826 /* Release the Wait Lock */
827 CsrReleaseWaitLock();
829 /* Dereference the thread */
830 CsrLockedDereferenceThread(CsrThread
);
831 NextEntry
= CsrProcess
->ThreadList
.Flink
;
834 /* Release the Process Lock and return success */
835 CsrReleaseProcessLock();
836 return STATUS_SUCCESS
;
840 * @name CsrCreateProcess
843 * Do nothing for 500ms.
845 * @param ArgumentCount
846 * Description of the parameter. Wrapped to more lines on ~70th
850 * Description of the parameter. Wrapped to more lines on ~70th
853 * @return STATUS_SUCCESS in case of success, STATUS_UNSUCCESSFUL
861 CsrCreateProcess(IN HANDLE hProcess
,
863 IN PCLIENT_ID ClientId
,
864 IN PCSR_NT_SESSION NtSession
,
866 IN PCLIENT_ID DebugCid
)
868 PCSR_THREAD CurrentThread
= CsrGetClientThread();
869 CLIENT_ID CurrentCid
;
870 PCSR_PROCESS CurrentProcess
;
873 PCSR_PROCESS CsrProcess
;
875 PCSR_THREAD CsrThread
;
876 KERNEL_USER_TIMES KernelTimes
;
878 /* Get the current CID and lock Processes */
879 CurrentCid
= CurrentThread
->ClientId
;
880 CsrAcquireProcessLock();
882 /* Get the current CSR Thread */
883 CurrentThread
= CsrLocateThreadByClientId(&CurrentProcess
, &CurrentCid
);
886 /* We've failed to locate the thread */
887 CsrReleaseProcessLock();
888 return STATUS_THREAD_IS_TERMINATING
;
891 /* Allocate a new Process Object */
892 CsrProcess
= CsrAllocateProcess();
895 /* Couldn't allocate Process */
896 CsrReleaseProcessLock();
897 return STATUS_NO_MEMORY
;
900 /* Inherit the Process Data */
901 CurrentProcess
= CurrentThread
->Process
;
902 ProcessData
= &CurrentProcess
->ServerData
[CSR_SERVER_DLL_MAX
];
903 for (i
= 0; i
< CSR_SERVER_DLL_MAX
; i
++)
905 /* Check if the DLL is Loaded and has Per Process Data */
906 if ((CsrLoadedServerDll
[i
]) && (CsrLoadedServerDll
[i
]->SizeOfProcessData
))
908 /* Set the pointer */
909 CsrProcess
->ServerData
[i
] = ProcessData
;
912 RtlMoveMemory(ProcessData
,
913 CurrentProcess
->ServerData
[i
],
914 CsrLoadedServerDll
[i
]->SizeOfProcessData
);
916 /* Update next data pointer */
917 ProcessData
= (PVOID
)((ULONG_PTR
)ProcessData
+
918 CsrLoadedServerDll
[i
]->SizeOfProcessData
);
922 /* No data for this Server */
923 CsrProcess
->ServerData
[i
] = NULL
;
927 /* Set the Exception port for us */
928 Status
= NtSetInformationProcess(hProcess
,
929 ProcessExceptionPort
,
932 if (!NT_SUCCESS(Status
))
935 CsrDeallocateProcess(CsrProcess
);
936 CsrReleaseProcessLock();
937 return STATUS_NO_MEMORY
;
940 /* Check if CreateProcess got CREATE_NEW_PROCESS_GROUP */
941 if (!(Flags
& CsrProcessCreateNewGroup
))
943 /* Create new data */
944 CsrProcess
->ProcessGroupId
= HandleToUlong(ClientId
->UniqueProcess
);
945 CsrProcess
->ProcessGroupSequence
= CsrProcess
->SequenceNumber
;
949 /* Copy it from the current process */
950 CsrProcess
->ProcessGroupId
= CurrentProcess
->ProcessGroupId
;
951 CsrProcess
->ProcessGroupSequence
= CurrentProcess
->ProcessGroupSequence
;
954 /* Check if this is a console process */
955 if (Flags
& CsrProcessIsConsoleApp
) CsrProcess
->Flags
|= CsrProcessIsConsoleApp
;
957 /* Mask out non-debug flags */
958 Flags
&= ~(CsrProcessIsConsoleApp
| CsrProcessCreateNewGroup
| CsrProcessPriorityFlags
);
960 /* Check if every process will be debugged */
961 if (!(Flags
) && (CurrentProcess
->DebugFlags
& CsrDebugProcessChildren
))
963 /* Pass it on to the current process */
964 CsrProcess
->DebugFlags
= CsrDebugProcessChildren
;
965 CsrProcess
->DebugCid
= CurrentProcess
->DebugCid
;
968 /* Check if Debugging was used on this process */
969 if ((Flags
& (CsrDebugOnlyThisProcess
| CsrDebugProcessChildren
)) && (DebugCid
))
971 /* Save the debug flag used */
972 CsrProcess
->DebugFlags
= Flags
;
975 CsrProcess
->DebugCid
= *DebugCid
;
978 /* Check if Debugging is enabled */
979 if (CsrProcess
->DebugFlags
)
981 /* Set the Debug Port for us */
982 Status
= NtSetInformationProcess(hProcess
,
986 ASSERT(NT_SUCCESS(Status
));
987 if (!NT_SUCCESS(Status
))
990 CsrDeallocateProcess(CsrProcess
);
991 CsrReleaseProcessLock();
992 return STATUS_NO_MEMORY
;
996 /* Get the Thread Create Time */
997 Status
= NtQueryInformationThread(hThread
,
1000 sizeof(KernelTimes
),
1002 if (!NT_SUCCESS(Status
))
1005 CsrDeallocateProcess(CsrProcess
);
1006 CsrReleaseProcessLock();
1007 return STATUS_NO_MEMORY
;
1010 /* Allocate a CSR Thread Structure */
1011 CsrThread
= CsrAllocateThread(CsrProcess
);
1015 CsrDeallocateProcess(CsrProcess
);
1016 CsrReleaseProcessLock();
1017 return STATUS_NO_MEMORY
;
1020 /* Save the data we have */
1021 CsrThread
->CreateTime
= KernelTimes
.CreateTime
;
1022 CsrThread
->ClientId
= *ClientId
;
1023 CsrThread
->ThreadHandle
= hThread
;
1024 ProtectHandle(hThread
);
1025 CsrThread
->Flags
= 0;
1027 /* Insert the Thread into the Process */
1028 CsrInsertThread(CsrProcess
, CsrThread
);
1030 /* Reference the session */
1031 CsrReferenceNtSession(NtSession
);
1032 CsrProcess
->NtSession
= NtSession
;
1034 /* Setup Process Data */
1035 CsrProcess
->ClientId
= *ClientId
;
1036 CsrProcess
->ProcessHandle
= hProcess
;
1037 CsrProcess
->ShutdownLevel
= 0x280;
1039 /* Set the Priority to Background */
1040 CsrSetBackgroundPriority(CsrProcess
);
1042 /* Insert the Process */
1043 CsrInsertProcess(NULL
, CurrentProcess
, CsrProcess
);
1045 /* Release lock and return */
1046 CsrReleaseProcessLock();
1051 * @name CsrUnlockProcess
1054 * The CsrUnlockProcess undoes a previous CsrLockProcessByClientId operation.
1057 * Pointer to a previously locked CSR Process.
1059 * @return STATUS_SUCCESS.
1061 * @remarks This routine must be called with the Process Lock held.
1066 CsrUnlockProcess(IN PCSR_PROCESS CsrProcess
)
1068 /* Dereference the process */
1069 CsrLockedDereferenceProcess(CsrProcess
);
1071 /* Release the lock and return */
1072 CsrReleaseProcessLock();
1073 return STATUS_SUCCESS
;
1077 * @name CsrSetBackgroundPriority
1080 * The CsrSetBackgroundPriority routine sets the priority for the given CSR
1081 * Process as a Background priority.
1084 * Pointer to the CSR Process whose priority will be modified.
1093 CsrSetBackgroundPriority(IN PCSR_PROCESS CsrProcess
)
1095 PROCESS_PRIORITY_CLASS PriorityClass
;
1097 /* Set the Foreground bit off */
1098 PriorityClass
.Foreground
= FALSE
;
1100 /* Set the new Priority */
1101 NtSetInformationProcess(CsrProcess
->ProcessHandle
,
1102 ProcessPriorityClass
,
1104 sizeof(PriorityClass
));
1108 * @name CsrLockProcessByClientId
1111 * The CsrLockProcessByClientId routine locks the CSR Process corresponding
1112 * to the given Process ID and optionally returns it.
1115 * Process ID corresponding to the CSR Process which will be locked.
1118 * Optional pointer to a CSR Process pointer which will hold the
1119 * CSR Process corresponding to the given Process ID.
1121 * @return STATUS_SUCCESS in case of success, STATUS_UNSUCCESSFUL
1124 * @remarks Locking a CSR Process is defined as acquiring an extra
1125 * reference to it and returning with the Process Lock held.
1130 CsrLockProcessByClientId(IN HANDLE Pid
,
1131 OUT PCSR_PROCESS
*CsrProcess
)
1133 PLIST_ENTRY NextEntry
;
1134 PCSR_PROCESS CurrentProcess
= NULL
;
1137 /* Acquire the lock */
1138 CsrAcquireProcessLock();
1140 /* Assume failure */
1141 ASSERT(CsrProcess
!= NULL
);
1144 /* Setup the List Pointers */
1145 NextEntry
= &CsrRootProcess
->ListLink
;
1148 /* Get the Process */
1149 CurrentProcess
= CONTAINING_RECORD(NextEntry
, CSR_PROCESS
, ListLink
);
1151 /* Check for PID Match */
1152 if (CurrentProcess
->ClientId
.UniqueProcess
== Pid
)
1154 Status
= STATUS_SUCCESS
;
1159 NextEntry
= NextEntry
->Flink
;
1160 } while (NextEntry
!= &CsrRootProcess
->ListLink
);
1162 /* Check if we didn't find it in the list */
1163 if (!NT_SUCCESS(Status
))
1165 /* Nothing found, release the lock */
1166 CsrReleaseProcessLock();
1170 /* Lock the found process and return it */
1171 CsrLockedReferenceProcess(CurrentProcess
);
1172 *CsrProcess
= CurrentProcess
;
1175 /* Return the result */
1180 * @name CsrShutdownProcesses
1183 * The CsrShutdownProcesses routine shuts down every CSR Process possible
1184 * and calls each Server DLL's shutdown notification.
1187 * Pointer to the LUID of the CSR Process that is ordering the
1191 * Flags to send to the shutdown notification routine.
1193 * @return STATUS_SUCCESS in case of success, STATUS_UNSUCCESSFUL
1201 CsrShutdownProcesses(IN PLUID CallerLuid
,
1204 PLIST_ENTRY NextEntry
;
1205 PCSR_PROCESS CsrProcess
;
1209 PCSR_SERVER_DLL ServerDll
;
1210 ULONG Result
= 0; /* Intentionally invalid enumeratee to silence compiler warning */
1212 /* Acquire process lock */
1213 CsrAcquireProcessLock();
1215 /* Add shutdown flag */
1216 CsrRootProcess
->ShutdownFlags
|= CsrShutdownSystem
;
1218 /* Get the list pointers */
1219 NextEntry
= CsrRootProcess
->ListLink
.Flink
;
1220 while (NextEntry
!= &CsrRootProcess
->ListLink
)
1222 /* Get the Process */
1223 CsrProcess
= CONTAINING_RECORD(NextEntry
, CSR_PROCESS
, ListLink
);
1225 /* Remove the skip flag, set shutdown flags to 0*/
1226 CsrProcess
->Flags
&= ~CsrProcessSkipShutdown
;
1227 CsrProcess
->ShutdownFlags
= 0;
1229 /* Move to the next */
1230 NextEntry
= NextEntry
->Flink
;
1233 /* Set shudown Priority */
1234 CsrpSetToShutdownPriority();
1239 /* Find the next process to shutdown */
1240 CsrProcess
= FindProcessForShutdown(CallerLuid
);
1241 if (!CsrProcess
) break;
1243 /* Increase reference to process */
1244 CsrProcess
->ReferenceCount
++;
1249 /* Loop all the servers */
1250 for (i
= 0; i
< CSR_SERVER_DLL_MAX
; i
++)
1252 /* Get the current server */
1253 ServerDll
= CsrLoadedServerDll
[i
];
1254 if ((ServerDll
) && (ServerDll
->ShutdownProcessCallback
))
1256 /* Release the lock, make the callback, and acquire it back */
1257 CsrReleaseProcessLock();
1258 Result
= ServerDll
->ShutdownProcessCallback(CsrProcess
,
1261 CsrAcquireProcessLock();
1263 /* Check the result */
1264 if (Result
== CsrShutdownCsrProcess
)
1266 /* The callback unlocked the process */
1269 else if (Result
== CsrShutdownCancelled
)
1271 /* Check if this was a forced shutdown */
1272 if (Flags
& EWX_FORCE
)
1274 DPRINT1("Process %x cancelled forced shutdown (Dll = %d)\n",
1275 CsrProcess
->ClientId
.UniqueProcess
, i
);
1279 /* Shutdown was cancelled, unlock and exit */
1280 CsrReleaseProcessLock();
1281 Status
= STATUS_CANCELLED
;
1287 /* No matches during the first try, so loop again */
1288 if ((FirstTry
) && (Result
== CsrShutdownNonCsrProcess
))
1294 /* Second try, break out */
1298 /* We've reached the final loop here, so dereference */
1299 if (i
== CSR_SERVER_DLL_MAX
) CsrLockedDereferenceProcess(CsrProcess
);
1303 CsrReleaseProcessLock();
1304 Status
= STATUS_SUCCESS
;
1307 /* Return to normal priority */
1308 CsrpSetToNormalPriority();
1312 /* FIXME: Temporary hack. This is really "CsrShutdownProcess", mostly. Used by win32csr */
1315 CsrEnumProcesses(IN CSRSS_ENUM_PROCESS_PROC EnumProc
,
1318 PVOID
* RealContext
= (PVOID
*)Context
;
1319 PLUID CallerLuid
= RealContext
[0];
1320 PCSR_PROCESS CsrProcess
= NULL
;
1321 NTSTATUS Status
= STATUS_UNSUCCESSFUL
;
1323 PLIST_ENTRY NextEntry
;
1326 /* Acquire process lock */
1327 CsrAcquireProcessLock();
1329 /* Get the list pointers */
1330 NextEntry
= CsrRootProcess
->ListLink
.Flink
;
1331 while (NextEntry
!= &CsrRootProcess
->ListLink
)
1333 /* Get the Process */
1334 CsrProcess
= CONTAINING_RECORD(NextEntry
, CSR_PROCESS
, ListLink
);
1336 /* Remove the skip flag, set shutdown flags to 0*/
1337 CsrProcess
->Flags
&= ~CsrProcessSkipShutdown
;
1338 CsrProcess
->ShutdownFlags
= 0;
1340 /* Move to the next */
1341 NextEntry
= NextEntry
->Flink
;
1344 /* Set shudown Priority */
1345 CsrSetToShutdownPriority();
1347 /* Loop all processes */
1348 //DPRINT1("Enumerating for LUID: %lx %lx\n", CallerLuid->HighPart, CallerLuid->LowPart);
1353 /* Find the next process to shutdown */
1355 if (!(CsrProcess
= FindProcessForShutdown(CallerLuid
)))
1358 CsrReleaseProcessLock();
1359 Status
= STATUS_SUCCESS
;
1364 /* Release the lock, make the callback, and acquire it back */
1365 //DPRINT1("Found process: %lx\n", CsrProcess->ClientId.UniqueProcess);
1366 CsrReleaseProcessLock();
1367 Result
= (ULONG
)EnumProc(CsrProcess
, (PVOID
)((ULONG_PTR
)Context
| FirstTry
));
1368 CsrAcquireProcessLock();
1370 /* Check the result */
1371 //DPRINT1("Result: %d\n", Result);
1372 if (Result
== CsrShutdownCsrProcess
)
1374 /* The callback unlocked the process */
1377 else if (Result
== CsrShutdownNonCsrProcess
)
1379 /* A non-CSR process, the callback didn't touch it */
1382 else if (Result
== CsrShutdownCancelled
)
1384 /* Shutdown was cancelled, unlock and exit */
1385 CsrReleaseProcessLock();
1386 Status
= STATUS_CANCELLED
;
1390 /* No matches during the first try, so loop again */
1391 if (FirstTry
&& Result
== CsrShutdownNonCsrProcess
)
1399 /* Return to normal priority */
1400 CsrSetToNormalPriority();
1405 * @name CsrDebugProcess
1408 * The CsrDebugProcess routine is deprecated in NT 5.1 and higher. It is
1409 * exported only for compatibility with older CSR Server DLLs.
1414 * @return Deprecated
1416 * @remarks Deprecated.
1421 CsrDebugProcess(IN PCSR_PROCESS CsrProcess
)
1423 /* CSR does not handle debugging anymore */
1424 DPRINT("CSRSRV: %s(%08lx) called\n", __FUNCTION__
, CsrProcess
);
1425 return STATUS_UNSUCCESSFUL
;
1429 * @name CsrDebugProcessStop
1432 * The CsrDebugProcessStop routine is deprecated in NT 5.1 and higher. It is
1433 * exported only for compatibility with older CSR Server DLLs.
1438 * @return Deprecated
1440 * @remarks Deprecated.
1445 CsrDebugProcessStop(IN PCSR_PROCESS CsrProcess
)
1447 /* CSR does not handle debugging anymore */
1448 DPRINT("CSRSRV: %s(%08lx) called\n", __FUNCTION__
, CsrProcess
);
1449 return STATUS_UNSUCCESSFUL
;
1453 * @name CsrSetForegroundPriority
1456 * The CsrSetForegroundPriority routine sets the priority for the given CSR
1457 * Process as a Foreground priority.
1460 * Pointer to the CSR Process whose priority will be modified.
1469 CsrSetForegroundPriority(IN PCSR_PROCESS CsrProcess
)
1471 PROCESS_PRIORITY_CLASS PriorityClass
;
1473 /* Set the Foreground bit on */
1474 PriorityClass
.Foreground
= TRUE
;
1476 /* Set the new Priority */
1477 NtSetInformationProcess(CsrProcess
->ProcessHandle
,
1478 ProcessPriorityClass
,
1480 sizeof(PriorityClass
));