2 * PROJECT: ReactOS Kernel
3 * LICENSE: GPL - See COPYING in the top level directory
4 * FILE: ntoskrnl/ps/kill.c
5 * PURPOSE: Process Manager: Process and Thread Termination
6 * PROGRAMMERS: Alex Ionescu (alex.ionescu@reactos.org)
7 * Filip Navara (xnavara@reactos.org)
8 * Thomas Weidenmueller (w3seek@reactos.org
11 /* INCLUDES *****************************************************************/
17 /* GLOBALS *******************************************************************/
19 LIST_ENTRY PspReaperListHead
= { NULL
, NULL
};
20 WORK_QUEUE_ITEM PspReaperWorkItem
;
21 LARGE_INTEGER ShortTime
= {{-10 * 100 * 1000, -1}};
23 /* PRIVATE FUNCTIONS *********************************************************/
27 PspCatchCriticalBreak(IN PCHAR Message
,
28 IN PVOID ProcessOrThread
,
32 BOOLEAN Handled
= FALSE
;
35 /* Check if a debugger is enabled */
36 if (KdDebuggerEnabled
)
38 /* Print out the message */
39 DbgPrint(Message
, ProcessOrThread
, ImageName
);
42 /* If a debugger isn't present, don't prompt */
43 if (KdDebuggerNotPresent
) break;
45 /* A debuger is active, prompt for action */
46 DbgPrompt("Break, or Ignore (bi)?", Action
, sizeof(Action
));
68 /* Did we ultimately handle this? */
71 /* We didn't, bugcheck */
72 KeBugCheckEx(CRITICAL_OBJECT_TERMINATION
,
73 ((PKPROCESS
)ProcessOrThread
)->Header
.Type
,
74 (ULONG_PTR
)ProcessOrThread
,
82 PspTerminateProcess(IN PEPROCESS Process
,
83 IN NTSTATUS ExitStatus
)
86 NTSTATUS Status
= STATUS_NOTHING_TO_TERMINATE
;
88 PSTRACE(PS_KILL_DEBUG
,
89 "Process: %p ExitStatus: %p\n", Process
, ExitStatus
);
92 /* Check if this is a Critical Process */
93 if (Process
->BreakOnTermination
)
95 /* Break to debugger */
96 PspCatchCriticalBreak("Terminating critical process 0x%p (%s)\n",
98 Process
->ImageFileName
);
101 /* Set the delete flag */
102 InterlockedOr((PLONG
)&Process
->Flags
, PSF_PROCESS_DELETE_BIT
);
104 /* Get the first thread */
105 Thread
= PsGetNextProcessThread(Process
, NULL
);
109 PspTerminateThreadByPointer(Thread
, ExitStatus
, FALSE
);
110 Thread
= PsGetNextProcessThread(Process
, Thread
);
112 /* We had at least one thread, so termination is OK */
113 Status
= STATUS_SUCCESS
;
116 /* Check if there was nothing to terminate or if we have a debug port */
117 if ((Status
== STATUS_NOTHING_TO_TERMINATE
) || (Process
->DebugPort
))
119 /* Clear the handle table anyway */
120 ObClearProcessHandleTable(Process
);
129 PsTerminateProcess(IN PEPROCESS Process
,
130 IN NTSTATUS ExitStatus
)
132 /* Call the internal API */
133 return PspTerminateProcess(Process
, ExitStatus
);
138 PspShutdownProcessManager(VOID
)
140 PEPROCESS Process
= NULL
;
142 /* Loop every process */
143 Process
= PsGetNextProcess(Process
);
146 /* Make sure this isn't the idle or initial process */
147 if ((Process
!= PsInitialSystemProcess
) && (Process
!= PsIdleProcess
))
150 PspTerminateProcess(Process
, STATUS_SYSTEM_SHUTDOWN
);
153 /* Get the next process */
154 Process
= PsGetNextProcess(Process
);
160 PspExitApcRundown(IN PKAPC Apc
)
170 PspReapRoutine(IN PVOID Context
)
172 PSINGLE_LIST_ENTRY NextEntry
;
174 PSTRACE(PS_KILL_DEBUG
, "Context: %p\n", Context
);
176 /* Start main loop */
179 /* Write magic value and return the next entry to process */
180 NextEntry
= InterlockedExchangePointer(&PspReaperListHead
.Flink
,
182 ASSERT((NextEntry
!= NULL
) && (NextEntry
!= (PVOID
)1));
184 /* Start inner loop */
187 /* Get the first Thread Entry */
188 Thread
= CONTAINING_RECORD(NextEntry
, ETHREAD
, ReaperLink
);
190 /* Delete this entry's kernel stack */
191 MmDeleteKernelStack((PVOID
)Thread
->Tcb
.StackBase
,
192 Thread
->Tcb
.LargeStack
);
193 Thread
->Tcb
.InitialStack
= NULL
;
195 /* Move to the next entry */
196 NextEntry
= NextEntry
->Next
;
198 /* Dereference this thread */
199 ObDereferenceObject(Thread
);
200 } while ((NextEntry
!= NULL
) && (NextEntry
!= (PVOID
)1));
202 /* Remove magic value, keep looping if it got changed */
203 } while (InterlockedCompareExchangePointer(&PspReaperListHead
.Flink
,
210 PspDeleteProcess(IN PVOID ObjectBody
)
212 PEPROCESS Process
= (PEPROCESS
)ObjectBody
;
215 PSTRACE(PS_KILL_DEBUG
, "ObjectBody: %p\n", ObjectBody
);
218 /* Check if it has an Active Process Link */
219 if (Process
->ActiveProcessLinks
.Flink
)
221 /* Remove it from the Active List */
222 KeAcquireGuardedMutex(&PspActiveProcessMutex
);
223 RemoveEntryList(&Process
->ActiveProcessLinks
);
224 KeReleaseGuardedMutex(&PspActiveProcessMutex
);
227 /* Check for Auditing information */
228 if (Process
->SeAuditProcessCreationInfo
.ImageFileName
)
231 ExFreePool(Process
->SeAuditProcessCreationInfo
.ImageFileName
);
232 Process
->SeAuditProcessCreationInfo
.ImageFileName
= NULL
;
235 /* Check if we have a job */
238 /* Remove the process from the job */
239 PspRemoveProcessFromJob(Process
, Process
->Job
);
242 ObDereferenceObject(Process
->Job
);
246 /* Increase the stack count */
247 Process
->Pcb
.StackCount
++;
249 /* Check if we have a debug port */
250 if (Process
->DebugPort
)
252 /* Deference the Debug Port */
253 ObDereferenceObject(Process
->DebugPort
);
254 Process
->DebugPort
= NULL
;
257 /* Check if we have an exception port */
258 if (Process
->ExceptionPort
)
260 /* Deference the Exception Port */
261 ObDereferenceObject(Process
->ExceptionPort
);
262 Process
->ExceptionPort
= NULL
;
265 /* Check if we have a section object */
266 if (Process
->SectionObject
)
268 /* Deference the Section Object */
269 ObDereferenceObject(Process
->SectionObject
);
270 Process
->SectionObject
= NULL
;
274 /* Clean Ldt and Vdm objects */
275 PspDeleteLdt(Process
);
276 PspDeleteVdmObjects(Process
);
279 /* Delete the Object Table */
280 if (Process
->ObjectTable
)
282 /* Attach to the process */
283 KeStackAttachProcess(&Process
->Pcb
, &ApcState
);
285 /* Kill the Object Info */
286 ObKillProcess(Process
);
289 KeUnstackDetachProcess(&ApcState
);
292 /* Check if we have an address space, and clean it */
293 if (Process
->HasAddressSpace
)
295 /* Attach to the process */
296 KeStackAttachProcess(&Process
->Pcb
, &ApcState
);
298 /* Clean the Address Space */
299 PspExitProcess(FALSE
, Process
);
302 KeUnstackDetachProcess(&ApcState
);
304 /* Completely delete the Address Space */
305 MmDeleteProcessAddressSpace(Process
);
308 /* See if we have a PID */
309 if (Process
->UniqueProcessId
)
312 if (!(ExDestroyHandle(PspCidTable
, Process
->UniqueProcessId
, NULL
)))
314 /* Something wrong happened, bugcheck */
315 KeBugCheck(CID_HANDLE_DELETION
);
319 /* Cleanup security information */
320 PspDeleteProcessSecurity(Process
);
322 /* Check if we have kept information on the Working Set */
323 if (Process
->WorkingSetWatch
)
326 ExFreePool(Process
->WorkingSetWatch
);
328 /* And return the quota it was taking up */
329 PsReturnProcessNonPagedPoolQuota(Process
, 0x2000);
332 /* Dereference the Device Map */
333 ObDereferenceDeviceMap(Process
);
335 /* Destroy the Quota Block */
336 PspDestroyQuotaBlock(Process
);
341 PspDeleteThread(IN PVOID ObjectBody
)
343 PETHREAD Thread
= (PETHREAD
)ObjectBody
;
344 PEPROCESS Process
= Thread
->ThreadsProcess
;
346 PSTRACE(PS_KILL_DEBUG
, "ObjectBody: %p\n", ObjectBody
);
348 ASSERT(Thread
->Tcb
.Win32Thread
== NULL
);
350 /* Check if we have a stack */
351 if (Thread
->Tcb
.InitialStack
)
354 MmDeleteKernelStack((PVOID
)Thread
->Tcb
.StackBase
,
355 Thread
->Tcb
.LargeStack
);
358 /* Check if we have a CID Handle */
359 if (Thread
->Cid
.UniqueThread
)
361 /* Delete the CID Handle */
362 if (!(ExDestroyHandle(PspCidTable
, Thread
->Cid
.UniqueThread
, NULL
)))
364 /* Something wrong happened, bugcheck */
365 KeBugCheck(CID_HANDLE_DELETION
);
369 /* Cleanup impersionation information */
370 PspDeleteThreadSecurity(Thread
);
372 /* Make sure the thread was inserted, before continuing */
373 if (!Process
) return;
375 /* Check if the thread list is valid */
376 if (Thread
->ThreadListEntry
.Flink
)
378 /* Lock the thread's process */
379 KeEnterCriticalRegion();
380 ExAcquirePushLockExclusive(&Process
->ProcessLock
);
382 /* Remove us from the list */
383 RemoveEntryList(&Thread
->ThreadListEntry
);
385 /* Release the lock */
386 ExReleasePushLockExclusive(&Process
->ProcessLock
);
387 KeLeaveCriticalRegion();
390 /* Dereference the Process */
391 ObDereferenceObject(Process
);
395 * FUNCTION: Terminates the current thread
396 * See "Windows Internals" - Chapter 13, Page 50-53
400 PspExitThread(IN NTSTATUS ExitStatus
)
402 CLIENT_DIED_MSG TerminationMsg
;
405 PEPROCESS CurrentProcess
;
406 PETHREAD Thread
, OtherThread
, PreviousThread
= NULL
;
407 PVOID DeallocationStack
;
409 BOOLEAN Last
= FALSE
;
410 PTERMINATION_PORT TerminationPort
, NextPort
;
411 PLIST_ENTRY FirstEntry
, CurrentEntry
;
415 PSTRACE(PS_KILL_DEBUG
, "ExitStatus: %p\n", ExitStatus
);
417 /* Get the Current Thread and Process */
418 Thread
= PsGetCurrentThread();
419 CurrentProcess
= Thread
->ThreadsProcess
;
420 ASSERT((Thread
) == PsGetCurrentThread());
422 /* Can't terminate a thread if it attached another process */
423 if (KeIsAttachedProcess())
426 KeBugCheckEx(INVALID_PROCESS_ATTACH_ATTEMPT
,
427 (ULONG_PTR
)CurrentProcess
,
428 (ULONG_PTR
)Thread
->Tcb
.ApcState
.Process
,
429 (ULONG_PTR
)Thread
->Tcb
.ApcStateIndex
,
433 /* Lower to Passive Level */
434 KeLowerIrql(PASSIVE_LEVEL
);
436 /* Can't be a worker thread */
437 if (Thread
->ActiveExWorker
)
440 KeBugCheckEx(ACTIVE_EX_WORKER_THREAD_TERMINATION
,
447 /* Can't have pending APCs */
448 if (Thread
->Tcb
.CombinedApcDisable
!= 0)
451 KeBugCheckEx(KERNEL_APC_PENDING_DURING_EXIT
,
453 Thread
->Tcb
.CombinedApcDisable
,
458 /* Lock the thread */
459 ExWaitForRundownProtectionRelease(&Thread
->RundownProtect
);
461 /* Cleanup the power state */
462 PopCleanupPowerState((PPOWER_STATE
)&Thread
->Tcb
.PowerState
);
464 /* Call the WMI Callback for Threads */
465 //WmiTraceThread(Thread, NULL, FALSE);
467 /* Run Thread Notify Routines before we desintegrate the thread */
468 PspRunCreateThreadNotifyRoutines(Thread
, FALSE
);
470 /* Lock the Process before we modify its thread entries */
471 KeEnterCriticalRegion();
472 ExAcquirePushLockExclusive(&CurrentProcess
->ProcessLock
);
474 /* Decrease the active thread count, and check if it's 0 */
475 if (!(--CurrentProcess
->ActiveThreads
))
477 /* Set the delete flag */
478 InterlockedOr((PLONG
)&CurrentProcess
->Flags
, PSF_PROCESS_DELETE_BIT
);
480 /* Remember we are last */
483 /* Check if this termination is due to the thread dying */
484 if (ExitStatus
== STATUS_THREAD_IS_TERMINATING
)
486 /* Check if the last thread was pending */
487 if (CurrentProcess
->ExitStatus
== STATUS_PENDING
)
489 /* Use the last exit status */
490 CurrentProcess
->ExitStatus
= CurrentProcess
->
491 LastThreadExitStatus
;
496 /* Just a normal exit, write the code */
497 CurrentProcess
->ExitStatus
= ExitStatus
;
500 /* Loop all the current threads */
501 FirstEntry
= &CurrentProcess
->ThreadListHead
;
502 CurrentEntry
= FirstEntry
->Flink
;
503 while (FirstEntry
!= CurrentEntry
)
505 /* Get the thread on the list */
506 OtherThread
= CONTAINING_RECORD(CurrentEntry
,
510 /* Check if it's a thread that's still alive */
511 if ((OtherThread
!= Thread
) &&
512 !(KeReadStateThread(&OtherThread
->Tcb
)) &&
513 (ObReferenceObjectSafe(OtherThread
)))
515 /* It's a live thread and we referenced it, unlock process */
516 ExReleasePushLockExclusive(&CurrentProcess
->ProcessLock
);
517 KeLeaveCriticalRegion();
519 /* Wait on the thread */
520 KeWaitForSingleObject(OtherThread
,
526 /* Check if we had a previous thread to dereference */
527 if (PreviousThread
) ObDereferenceObject(PreviousThread
);
529 /* Remember the thread and re-lock the process */
530 PreviousThread
= OtherThread
;
531 KeEnterCriticalRegion();
532 ExAcquirePushLockExclusive(&CurrentProcess
->ProcessLock
);
535 /* Go to the next thread */
536 CurrentEntry
= CurrentEntry
->Flink
;
539 else if (ExitStatus
!= STATUS_THREAD_IS_TERMINATING
)
541 /* Write down the exit status of the last thread to get killed */
542 CurrentProcess
->LastThreadExitStatus
= ExitStatus
;
545 /* Unlock the Process */
546 ExReleasePushLockExclusive(&CurrentProcess
->ProcessLock
);
547 KeLeaveCriticalRegion();
549 /* Check if we had a previous thread to dereference */
550 if (PreviousThread
) ObDereferenceObject(PreviousThread
);
552 /* Check if the process has a debug port and if this is a user thread */
553 if ((CurrentProcess
->DebugPort
) && !(Thread
->SystemThread
))
555 /* Notify the Debug API. */
556 Last
? DbgkExitProcess(CurrentProcess
->ExitStatus
) :
557 DbgkExitThread(ExitStatus
);
560 /* Check if this is a Critical Thread */
561 if ((KdDebuggerEnabled
) && (Thread
->BreakOnTermination
))
563 /* Break to debugger */
564 PspCatchCriticalBreak("Critical thread 0x%p (in %s) exited\n",
566 CurrentProcess
->ImageFileName
);
569 /* Check if it's the last thread and this is a Critical Process */
570 if ((Last
) && (CurrentProcess
->BreakOnTermination
))
572 /* Check if a debugger is here to handle this */
573 if (KdDebuggerEnabled
)
575 /* Break to debugger */
576 PspCatchCriticalBreak("Critical process 0x%p (in %s) exited\n",
578 CurrentProcess
->ImageFileName
);
582 /* Bugcheck, we can't allow this */
583 KeBugCheckEx(CRITICAL_PROCESS_DIED
,
584 (ULONG_PTR
)CurrentProcess
,
592 ASSERT(Thread
->Tcb
.CombinedApcDisable
== 0);
594 /* Process the Termination Ports */
595 TerminationPort
= Thread
->TerminationPort
;
598 /* Setup the message header */
599 TerminationMsg
.h
.u2
.ZeroInit
= 0;
600 TerminationMsg
.h
.u2
.s2
.Type
= LPC_CLIENT_DIED
;
601 TerminationMsg
.h
.u1
.s1
.TotalLength
= sizeof(TerminationMsg
);
602 TerminationMsg
.h
.u1
.s1
.DataLength
= sizeof(TerminationMsg
) -
603 sizeof(PORT_MESSAGE
);
608 /* Save the Create Time */
609 TerminationMsg
.CreateTime
= Thread
->CreateTime
;
611 /* Loop trying to send message */
614 /* Send the LPC Message */
615 Status
= LpcRequestPort(TerminationPort
->Port
,
617 if ((Status
== STATUS_NO_MEMORY
) ||
618 (Status
== STATUS_INSUFFICIENT_RESOURCES
))
620 /* Wait a bit and try again */
621 KeDelayExecutionThread(KernelMode
, FALSE
, &ShortTime
);
627 /* Dereference this LPC Port */
628 ObDereferenceObject(TerminationPort
->Port
);
630 /* Move to the next one */
631 NextPort
= TerminationPort
->Next
;
633 /* Free the Termination Port Object */
634 ExFreePool(TerminationPort
);
636 /* Keep looping as long as there is a port */
637 TerminationPort
= NextPort
;
638 } while (TerminationPort
);
640 else if (((ExitStatus
== STATUS_THREAD_IS_TERMINATING
) &&
641 (Thread
->DeadThread
)) ||
642 !(Thread
->DeadThread
))
645 * This case is special and deserves some extra comments. What
646 * basically happens here is that this thread doesn't have a termination
647 * port, which means that it died before being fully created. Since we
648 * still have to notify an LPC Server, we'll use the exception port,
649 * which we know exists. However, we need to know how far the thread
650 * actually got created. We have three possibilites:
652 * - NtCreateThread returned an error really early: DeadThread is set.
653 * - NtCreateThread managed to create the thread: DeadThread is off.
654 * - NtCreateThread was creating the thread (with Deadthread set,
655 * but the thread got killed prematurely: STATUS_THREAD_IS_TERMINATING
658 * For the 2 & 3rd scenarios, the thread has been created far enough to
659 * warrant notification to the LPC Server.
662 /* Setup the message header */
663 TerminationMsg
.h
.u2
.s2
.Type
= LPC_CLIENT_DIED
;
664 TerminationMsg
.h
.u1
.s1
.TotalLength
= sizeof(TerminationMsg
);
665 TerminationMsg
.h
.u1
.s1
.DataLength
= sizeof(TerminationMsg
) -
666 sizeof(PORT_MESSAGE
);
668 /* Make sure the process has an exception port */
669 if (CurrentProcess
->ExceptionPort
)
671 /* Save the Create Time */
672 TerminationMsg
.CreateTime
= Thread
->CreateTime
;
674 /* Loop trying to send message */
677 /* Send the LPC Message */
678 Status
= LpcRequestPort(CurrentProcess
->ExceptionPort
,
680 if ((Status
== STATUS_NO_MEMORY
) ||
681 (Status
== STATUS_INSUFFICIENT_RESOURCES
))
683 /* Wait a bit and try again */
684 KeDelayExecutionThread(KernelMode
, FALSE
, &ShortTime
);
692 /* Rundown Win32 Thread if there is one */
693 if (Thread
->Tcb
.Win32Thread
) PspW32ThreadCallout(Thread
,
694 PsW32ThreadCalloutExit
);
696 /* If we are the last thread and have a W32 Process */
697 if ((Last
) && (CurrentProcess
->Win32Process
))
699 /* Run it down too */
700 PspW32ProcessCallout(CurrentProcess
, FALSE
);
703 /* Make sure Stack Swap is enabled */
704 if (!Thread
->Tcb
.EnableStackSwap
)
706 /* Stack swap really shouldn't be disabled during exit! */
707 KeBugCheckEx(KERNEL_STACK_LOCKED_AT_EXIT
, 0, 0, 0, 0);
710 /* Cancel I/O for the thread. */
711 IoCancelThreadIo(Thread
);
716 /* FIXME: Rundown Registry Notifications (NtChangeNotify)
717 CmNotifyRunDown(Thread); */
719 /* Rundown Mutexes */
722 /* Check if we have a TEB */
723 Teb
= Thread
->Tcb
.Teb
;
726 /* Check if the thread is still alive */
727 if (!Thread
->DeadThread
)
729 /* Check if we need to free its stack */
730 if (Teb
->FreeStackOnTermination
)
732 /* Set the TEB's Deallocation Stack as the Base Address */
734 DeallocationStack
= Teb
->DeallocationStack
;
736 /* Free the Thread's Stack */
737 ZwFreeVirtualMemory(NtCurrentProcess(),
743 /* Free the debug handle */
744 if (Teb
->DbgSsReserved
[1]) ObCloseHandle(Teb
->DbgSsReserved
[1],
748 /* Decommit the TEB */
749 MmDeleteTeb(CurrentProcess
, Teb
);
750 Thread
->Tcb
.Teb
= NULL
;
754 LpcExitThread(Thread
);
756 /* Save the exit status and exit time */
757 Thread
->ExitStatus
= ExitStatus
;
758 KeQuerySystemTime(&Thread
->ExitTime
);
761 ASSERT(Thread
->Tcb
.CombinedApcDisable
== 0);
763 /* Check if this is the final thread or not */
766 /* Set the process exit time */
767 CurrentProcess
->ExitTime
= Thread
->ExitTime
;
769 /* Exit the process */
770 PspExitProcess(TRUE
, CurrentProcess
);
772 /* Get the process token and check if we need to audit */
773 PrimaryToken
= PsReferencePrimaryToken(CurrentProcess
);
774 if (SeDetailedAuditingWithToken(PrimaryToken
))
777 SeAuditProcessExit(CurrentProcess
);
780 /* Dereference the process token */
781 ObFastDereferenceObject(&CurrentProcess
->Token
, PrimaryToken
);
783 /* Check if this is a VDM Process and rundown the VDM DPCs if so */
784 if (CurrentProcess
->VdmObjects
) { /* VdmRundownDpcs(CurrentProcess); */ }
786 /* Kill the process in the Object Manager */
787 ObKillProcess(CurrentProcess
);
789 /* Check if we have a section object */
790 if (CurrentProcess
->SectionObject
)
792 /* Dereference and clear the Section Object */
793 ObDereferenceObject(CurrentProcess
->SectionObject
);
794 CurrentProcess
->SectionObject
= NULL
;
797 /* Check if the process is part of a job */
798 if (CurrentProcess
->Job
)
800 /* Remove the process from the job */
801 PspExitProcessFromJob(CurrentProcess
->Job
, CurrentProcess
);
806 KeEnterCriticalRegion();
808 /* Disable APC queueing, force a resumption */
809 Thread
->Tcb
.ApcQueueable
= FALSE
;
810 KeForceResumeThread(&Thread
->Tcb
);
813 KeLeaveCriticalRegion();
815 /* Flush the User APCs */
816 FirstEntry
= KeFlushQueueApc(&Thread
->Tcb
, UserMode
);
819 /* Start with the first entry */
820 CurrentEntry
= FirstEntry
;
824 Apc
= CONTAINING_RECORD(CurrentEntry
, KAPC
, ApcListEntry
);
826 /* Move to the next one */
827 CurrentEntry
= CurrentEntry
->Flink
;
829 /* Rundown the APC or de-allocate it */
830 if (Apc
->RundownRoutine
)
832 /* Call its own routine */
833 Apc
->RundownRoutine(Apc
);
837 /* Do it ourselves */
841 while (CurrentEntry
!= FirstEntry
);
844 /* Clean address space if this was the last thread */
845 if (Last
) MmCleanProcessAddressSpace(CurrentProcess
);
847 /* Call the Lego routine */
848 if (Thread
->Tcb
.LegoData
) PspRunLegoRoutine(&Thread
->Tcb
);
850 /* Flush the APC queue, which should be empty */
851 FirstEntry
= KeFlushQueueApc(&Thread
->Tcb
, KernelMode
);
852 if ((FirstEntry
) || (Thread
->Tcb
.CombinedApcDisable
!= 0))
855 KeBugCheckEx(KERNEL_APC_PENDING_DURING_EXIT
,
856 (ULONG_PTR
)FirstEntry
,
857 Thread
->Tcb
.CombinedApcDisable
,
862 /* Signal the process if this was the last thread */
863 if (Last
) KeSetProcess(&CurrentProcess
->Pcb
, 0, FALSE
);
865 /* Terminate the Thread from the Scheduler */
866 KeTerminateThread(0);
871 PsExitSpecialApc(IN PKAPC Apc
,
872 IN OUT PKNORMAL_ROUTINE
* NormalRoutine
,
873 IN OUT PVOID
* NormalContext
,
874 IN OUT PVOID
* SystemArgument1
,
875 IN OUT PVOID
* SystemArgument2
)
879 PSTRACE(PS_KILL_DEBUG
,
880 "Apc: %p SystemArgument2: %p \n", Apc
, SystemArgument2
);
882 /* Don't do anything unless we are in User-Mode */
883 if (Apc
->SystemArgument2
)
886 Status
= (NTSTATUS
)Apc
->NormalContext
;
887 PspExitApcRundown(Apc
);
889 /* Terminate the Thread */
890 PspExitThread(Status
);
896 PspExitNormalApc(IN PVOID NormalContext
,
897 IN PVOID SystemArgument1
,
898 IN PVOID SystemArgument2
)
900 PKAPC Apc
= (PKAPC
)SystemArgument1
;
901 PETHREAD Thread
= PsGetCurrentThread();
903 PSTRACE(PS_KILL_DEBUG
, "SystemArgument2: %p \n", SystemArgument2
);
905 /* This should never happen */
906 ASSERT(!(((ULONG_PTR
)SystemArgument2
) & 1));
908 /* If we're here, this is not a System Thread, so kill it from User-Mode */
911 OriginalApcEnvironment
,
918 /* Now insert the APC with the User-Mode Flag */
919 if (!(KeInsertQueueApc(Apc
,
921 (PVOID
)((ULONG_PTR
)SystemArgument2
| 1),
924 /* Failed to insert, free the APC */
925 PspExitApcRundown(Apc
);
928 /* Set the APC Pending flag */
929 Thread
->Tcb
.ApcState
.UserApcPending
= TRUE
;
933 * See "Windows Internals" - Chapter 13, Page 49
937 PspTerminateThreadByPointer(IN PETHREAD Thread
,
938 IN NTSTATUS ExitStatus
,
942 NTSTATUS Status
= STATUS_SUCCESS
;
945 PSTRACE(PS_KILL_DEBUG
, "Thread: %p ExitStatus: %p\n", Thread
, ExitStatus
);
948 /* Check if this is a Critical Thread, and Bugcheck */
949 if (Thread
->BreakOnTermination
)
951 /* Break to debugger */
952 PspCatchCriticalBreak("Terminating critical thread 0x%p (%s)\n",
954 Thread
->ThreadsProcess
->ImageFileName
);
957 /* Check if we are already inside the thread */
958 if ((bSelf
) || (PsGetCurrentThread() == Thread
))
960 /* This should only happen at passive */
961 ASSERT_IRQL_EQUAL(PASSIVE_LEVEL
);
963 /* Mark it as terminated */
964 PspSetCrossThreadFlag(Thread
, CT_TERMINATED_BIT
);
966 /* Directly terminate the thread */
967 PspExitThread(ExitStatus
);
970 /* This shouldn't be a system thread */
971 if (Thread
->SystemThread
) return STATUS_ACCESS_DENIED
;
973 /* Allocate the APC */
974 Apc
= ExAllocatePoolWithTag(NonPagedPool
, sizeof(KAPC
), TAG_TERMINATE_APC
);
975 if (!Apc
) return STATUS_INSUFFICIENT_RESOURCES
;
977 /* Set the Terminated Flag */
978 Flags
= Thread
->CrossThreadFlags
| CT_TERMINATED_BIT
;
980 /* Set it, and check if it was already set while we were running */
981 if (!(InterlockedExchange((PLONG
)&Thread
->CrossThreadFlags
, Flags
) &
984 /* Initialize a Kernel Mode APC to Kill the Thread */
987 OriginalApcEnvironment
,
994 /* Insert it into the APC Queue */
995 if (!KeInsertQueueApc(Apc
, Apc
, NULL
, 2))
997 /* The APC was already in the queue, fail */
998 Status
= STATUS_UNSUCCESSFUL
;
1002 /* Forcefully resume the thread and return */
1003 KeForceResumeThread(&Thread
->Tcb
);
1008 /* We failed, free the APC */
1017 PspExitProcess(IN BOOLEAN LastThread
,
1018 IN PEPROCESS Process
)
1022 PSTRACE(PS_KILL_DEBUG
,
1023 "LastThread: %p Process: %p\n", LastThread
, Process
);
1024 PSREFTRACE(Process
);
1026 /* Set Process Exit flag */
1027 InterlockedOr((PLONG
)&Process
->Flags
, PSF_PROCESS_EXITING_BIT
);
1029 /* Check if we are the last thread */
1032 /* Notify the WMI Process Callback */
1033 //WmiTraceProcess(Process, FALSE);
1035 /* Run the Notification Routines */
1036 PspRunCreateProcessNotifyRoutines(Process
, FALSE
);
1039 /* Cleanup the power state */
1040 PopCleanupPowerState((PPOWER_STATE
)&Process
->Pcb
.PowerState
);
1042 /* Clear the security port */
1043 if (!Process
->SecurityPort
)
1045 /* So we don't double-dereference */
1046 Process
->SecurityPort
= (PVOID
)1;
1048 else if (Process
->SecurityPort
!= (PVOID
)1)
1050 /* Dereference it */
1051 ObDereferenceObject(Process
->SecurityPort
);
1052 Process
->SecurityPort
= (PVOID
)1;
1055 /* Check if we are the last thread */
1058 /* Check if we have to set the Timer Resolution */
1059 if (Process
->SetTimerResolution
)
1061 /* Set it to default */
1062 ZwSetTimerResolution(KeMaximumIncrement
, 0, &Actual
);
1065 /* Check if we are part of a Job that has a completion port */
1066 if ((Process
->Job
) && (Process
->Job
->CompletionPort
))
1068 /* FIXME: Check job status code and do I/O completion if needed */
1071 /* FIXME: Notify the Prefetcher */
1075 /* Clear process' address space here */
1076 MmCleanProcessAddressSpace(Process
);
1080 /* PUBLIC FUNCTIONS **********************************************************/
1087 PsTerminateSystemThread(IN NTSTATUS ExitStatus
)
1089 PETHREAD Thread
= PsGetCurrentThread();
1091 /* Make sure this is a system thread */
1092 if (!Thread
->SystemThread
) return STATUS_INVALID_PARAMETER
;
1094 /* Terminate it for real */
1095 return PspTerminateThreadByPointer(Thread
, ExitStatus
, TRUE
);
1103 NtTerminateProcess(IN HANDLE ProcessHandle OPTIONAL
,
1104 IN NTSTATUS ExitStatus
)
1107 PEPROCESS Process
, CurrentProcess
= PsGetCurrentProcess();
1108 PETHREAD Thread
, CurrentThread
= PsGetCurrentThread();
1109 BOOLEAN KillByHandle
;
1111 PSTRACE(PS_KILL_DEBUG
,
1112 "ProcessHandle: %p ExitStatus: %p\n", ProcessHandle
, ExitStatus
);
1114 /* Were we passed a process handle? */
1117 /* Yes we were, use it */
1118 KillByHandle
= TRUE
;
1122 /* We weren't... we assume this is suicide */
1123 KillByHandle
= FALSE
;
1124 ProcessHandle
= NtCurrentProcess();
1127 /* Get the Process Object */
1128 Status
= ObReferenceObjectByHandle(ProcessHandle
,
1131 KeGetPreviousMode(),
1134 if (!NT_SUCCESS(Status
)) return(Status
);
1136 /* Check if this is a Critical Process, and Bugcheck */
1137 if (Process
->BreakOnTermination
)
1139 /* Break to debugger */
1140 PspCatchCriticalBreak("Terminating critical process 0x%p (%s)\n",
1142 Process
->ImageFileName
);
1145 /* Lock the Process */
1146 if (!ExAcquireRundownProtection(&Process
->RundownProtect
))
1148 /* Failed to lock, fail */
1149 ObDereferenceObject (Process
);
1150 return STATUS_PROCESS_IS_TERMINATING
;
1153 /* Set the delete flag, unless the process is comitting suicide */
1154 if (KillByHandle
) PspSetProcessFlag(Process
, PSF_PROCESS_DELETE_BIT
);
1156 /* Get the first thread */
1157 Status
= STATUS_NOTHING_TO_TERMINATE
;
1158 Thread
= PsGetNextProcessThread(Process
, NULL
);
1161 /* We know we have at least a thread */
1162 Status
= STATUS_SUCCESS
;
1164 /* Loop and kill the others */
1167 /* Ensure it's not ours*/
1168 if (Thread
!= CurrentThread
)
1171 PspTerminateThreadByPointer(Thread
, ExitStatus
, FALSE
);
1174 /* Move to the next thread */
1175 Thread
= PsGetNextProcessThread(Process
, Thread
);
1179 /* Unlock the process */
1180 ExReleaseRundownProtection(&Process
->RundownProtect
);
1182 /* Check if we are killing ourselves */
1183 if (Process
== CurrentProcess
)
1185 /* Also make sure the caller gave us our handle */
1188 /* Dereference the process */
1189 ObDereferenceObject(Process
);
1191 /* Terminate ourselves */
1192 PspTerminateThreadByPointer(CurrentThread
, ExitStatus
, TRUE
);
1195 else if (ExitStatus
== DBG_TERMINATE_PROCESS
)
1197 /* Disable debugging on this process */
1198 DbgkClearProcessDebugObject(Process
, NULL
);
1201 /* Check if there was nothing to terminate, or if we have a Debug Port */
1202 if ((Status
== STATUS_NOTHING_TO_TERMINATE
) ||
1203 ((Process
->DebugPort
) && (KillByHandle
)))
1205 /* Clear the handle table */
1206 ObClearProcessHandleTable(Process
);
1208 /* Return status now */
1209 Status
= STATUS_SUCCESS
;
1212 /* Decrease the reference count we added */
1213 ObDereferenceObject(Process
);
1221 NtTerminateThread(IN HANDLE ThreadHandle
,
1222 IN NTSTATUS ExitStatus
)
1225 PETHREAD CurrentThread
= PsGetCurrentThread();
1228 PSTRACE(PS_KILL_DEBUG
,
1229 "ThreadHandle: %p ExitStatus: %p\n", ThreadHandle
, ExitStatus
);
1231 /* Handle the special NULL case */
1234 /* Check if we're the only thread left */
1235 if (PsGetCurrentProcess()->ActiveThreads
== 1)
1237 /* This is invalid */
1238 return STATUS_CANT_TERMINATE_SELF
;
1241 /* Terminate us directly */
1244 else if (ThreadHandle
== NtCurrentThread())
1247 /* Terminate this thread */
1248 return PspTerminateThreadByPointer(CurrentThread
,
1253 /* We are terminating another thread, get the Thread Object */
1254 Status
= ObReferenceObjectByHandle(ThreadHandle
,
1257 KeGetPreviousMode(),
1260 if (!NT_SUCCESS(Status
)) return Status
;
1262 /* Check to see if we're running in the same thread */
1263 if (Thread
!= CurrentThread
)
1266 Status
= PspTerminateThreadByPointer(Thread
, ExitStatus
, FALSE
);
1268 /* Dereference the Thread and return */
1269 ObDereferenceObject(Thread
);
1273 /* Dereference the thread and terminate ourselves */
1274 ObDereferenceObject(Thread
);
1284 NtRegisterThreadTerminatePort(IN HANDLE PortHandle
)
1287 PTERMINATION_PORT TerminationPort
;
1288 PVOID TerminationLpcPort
;
1291 PSTRACE(PS_KILL_DEBUG
, "PortHandle: %p\n", PortHandle
);
1294 Status
= ObReferenceObjectByHandle(PortHandle
,
1297 KeGetPreviousMode(),
1298 &TerminationLpcPort
,
1300 if (!NT_SUCCESS(Status
)) return(Status
);
1302 /* Allocate the Port and make sure it suceeded */
1303 TerminationPort
= ExAllocatePoolWithTag(NonPagedPool
,
1304 sizeof(TERMINATION_PORT
),
1308 /* Associate the Port */
1309 Thread
= PsGetCurrentThread();
1310 TerminationPort
->Port
= TerminationLpcPort
;
1311 TerminationPort
->Next
= Thread
->TerminationPort
;
1312 Thread
->TerminationPort
= TerminationPort
;
1314 /* Return success */
1315 return STATUS_SUCCESS
;
1318 /* Dereference and Fail */
1319 ObDereferenceObject(TerminationPort
);
1320 return STATUS_INSUFFICIENT_RESOURCES
;