1999-01-16 21:03:00 +00:00
|
|
|
/*
|
|
|
|
* COPYRIGHT: See COPYING in the top level directory
|
|
|
|
* PROJECT: ReactOS kernel
|
|
|
|
* FILE: ntoskrnl/ps/kill.c
|
|
|
|
* PURPOSE: Terminating a thread
|
1999-03-25 00:37:06 +00:00
|
|
|
* PROGRAMMER: David Welch (welch@cwcom.net)
|
1999-01-16 21:03:00 +00:00
|
|
|
* UPDATE HISTORY:
|
|
|
|
* Created 22/05/98
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* INCLUDES *****************************************************************/
|
|
|
|
|
|
|
|
#include <ddk/ntddk.h>
|
|
|
|
#include <internal/ps.h>
|
1999-03-25 00:37:06 +00:00
|
|
|
#include <internal/ke.h>
|
1999-04-01 12:39:43 +00:00
|
|
|
#include <internal/mm.h>
|
|
|
|
#include <internal/ob.h>
|
1999-01-16 21:03:00 +00:00
|
|
|
|
1999-12-13 22:04:41 +00:00
|
|
|
#define NDEBUG
|
1999-01-16 21:03:00 +00:00
|
|
|
#include <internal/debug.h>
|
|
|
|
|
1999-04-10 12:08:24 +00:00
|
|
|
/* GLOBALS *******************************************************************/
|
1999-01-16 21:03:00 +00:00
|
|
|
|
|
|
|
extern ULONG PiNrThreads;
|
1999-11-02 08:55:45 +00:00
|
|
|
extern ULONG PiNrRunnableThreads;
|
|
|
|
extern KSPIN_LOCK PiThreadListLock;
|
1999-12-18 17:48:23 +00:00
|
|
|
extern LIST_ENTRY PiThreadListHead;
|
1999-01-16 21:03:00 +00:00
|
|
|
|
|
|
|
/* FUNCTIONS *****************************************************************/
|
|
|
|
|
1999-12-18 17:48:23 +00:00
|
|
|
VOID PiTerminateProcessThreads(PEPROCESS Process, NTSTATUS ExitStatus)
|
|
|
|
{
|
|
|
|
KIRQL oldlvl;
|
|
|
|
PLIST_ENTRY current_entry;
|
|
|
|
PETHREAD current;
|
|
|
|
|
|
|
|
KeAcquireSpinLock(&PiThreadListLock, &oldlvl);
|
|
|
|
|
|
|
|
current_entry = PiThreadListHead.Flink;
|
|
|
|
while (current_entry != &PiThreadListHead)
|
|
|
|
{
|
|
|
|
current = CONTAINING_RECORD(current_entry,ETHREAD,Tcb.QueueListEntry);
|
|
|
|
if (current->ThreadsProcess == Process &&
|
|
|
|
current != PsGetCurrentThread())
|
|
|
|
{
|
|
|
|
KeReleaseSpinLock(&PiThreadListLock, oldlvl);
|
|
|
|
PsTerminateOtherThread(current, ExitStatus);
|
|
|
|
KeAcquireSpinLock(&PiThreadListLock, &oldlvl);
|
|
|
|
current_entry = PiThreadListHead.Flink;
|
|
|
|
}
|
|
|
|
current_entry = current_entry->Flink;
|
|
|
|
}
|
|
|
|
|
|
|
|
KeReleaseSpinLock(&PiThreadListLock, oldlvl);
|
|
|
|
}
|
|
|
|
|
|
|
|
VOID PsReapThreads(VOID)
|
|
|
|
{
|
|
|
|
PLIST_ENTRY current_entry;
|
|
|
|
PETHREAD current;
|
|
|
|
KIRQL oldIrql;
|
|
|
|
|
|
|
|
// DPRINT1("PsReapThreads()\n");
|
|
|
|
|
|
|
|
KeAcquireSpinLock(&PiThreadListLock, &oldIrql);
|
|
|
|
|
|
|
|
current_entry = PiThreadListHead.Flink;
|
|
|
|
|
|
|
|
while (current_entry != &PiThreadListHead)
|
|
|
|
{
|
|
|
|
current = CONTAINING_RECORD(current_entry, ETHREAD,
|
|
|
|
Tcb.ThreadListEntry);
|
|
|
|
|
|
|
|
current_entry = current_entry->Flink;
|
|
|
|
|
|
|
|
if (current->Tcb.State == THREAD_STATE_TERMINATED_1)
|
|
|
|
{
|
|
|
|
PEPROCESS Process = current->ThreadsProcess;
|
|
|
|
NTSTATUS Status = current->ExitStatus;
|
|
|
|
|
|
|
|
ObReferenceObjectByPointer(Process,
|
|
|
|
0,
|
|
|
|
PsProcessType,
|
|
|
|
KernelMode );
|
|
|
|
DPRINT("Reaping thread %x\n", current);
|
|
|
|
current->Tcb.State = THREAD_STATE_TERMINATED_2;
|
|
|
|
RemoveEntryList(¤t->Tcb.ProcessThreadListEntry);
|
|
|
|
KeReleaseSpinLock(&PiThreadListLock, oldIrql);
|
|
|
|
ObDereferenceObject(current);
|
|
|
|
KeAcquireSpinLock(&PiThreadListLock, &oldIrql);
|
|
|
|
if(IsListEmpty( &Process->Pcb.ThreadListHead))
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* TODO: Optimize this so it doesnt jerk the IRQL around so
|
|
|
|
* much :)
|
|
|
|
*/
|
|
|
|
DPRINT("Last thread terminated, terminating process\n");
|
|
|
|
KeReleaseSpinLock(&PiThreadListLock, oldIrql);
|
|
|
|
PiTerminateProcess(Process, Status);
|
|
|
|
KeAcquireSpinLock(&PiThreadListLock, &oldIrql);
|
|
|
|
}
|
|
|
|
ObDereferenceObject(Process);
|
|
|
|
current_entry = PiThreadListHead.Flink;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
KeReleaseSpinLock(&PiThreadListLock, oldIrql);
|
|
|
|
}
|
|
|
|
|
1999-01-16 21:03:00 +00:00
|
|
|
VOID PsTerminateCurrentThread(NTSTATUS ExitStatus)
|
1999-02-06 18:34:14 +00:00
|
|
|
/*
|
|
|
|
* FUNCTION: Terminates the current thread
|
|
|
|
*/
|
1999-01-16 21:03:00 +00:00
|
|
|
{
|
1999-12-13 22:04:41 +00:00
|
|
|
KIRQL oldIrql;
|
1999-01-16 21:03:00 +00:00
|
|
|
PETHREAD CurrentThread;
|
|
|
|
|
|
|
|
CurrentThread = PsGetCurrentThread();
|
|
|
|
|
|
|
|
DPRINT("terminating %x\n",CurrentThread);
|
1999-12-13 22:04:41 +00:00
|
|
|
KeAcquireSpinLock(&PiThreadListLock, &oldIrql);
|
|
|
|
|
|
|
|
CurrentThread->ExitStatus = ExitStatus;
|
1999-11-12 12:01:17 +00:00
|
|
|
|
|
|
|
DPRINT("ObGetReferenceCount(CurrentThread) %d\n",
|
|
|
|
ObGetReferenceCount(CurrentThread));
|
1999-12-02 20:53:55 +00:00
|
|
|
DPRINT("ObGetHandleCount(CurrentThread) %x\n",
|
|
|
|
ObGetHandleCount(CurrentThread));
|
1999-11-12 12:01:17 +00:00
|
|
|
|
1999-10-07 23:46:27 +00:00
|
|
|
CurrentThread->Tcb.DispatcherHeader.SignalState = TRUE;
|
1999-09-06 21:32:57 +00:00
|
|
|
KeDispatcherObjectWake(&CurrentThread->Tcb.DispatcherHeader);
|
1999-11-12 12:01:17 +00:00
|
|
|
|
1999-12-13 22:04:41 +00:00
|
|
|
PsDispatchThreadNoLock(THREAD_STATE_TERMINATED_1);
|
1999-11-12 12:01:17 +00:00
|
|
|
KeBugCheck(0);
|
1999-01-16 21:03:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
VOID PsTerminateOtherThread(PETHREAD Thread, NTSTATUS ExitStatus)
|
1999-02-06 18:34:14 +00:00
|
|
|
/*
|
|
|
|
* FUNCTION: Terminate a thread when calling from that thread's context
|
|
|
|
*/
|
1999-01-16 21:03:00 +00:00
|
|
|
{
|
1999-11-02 08:55:45 +00:00
|
|
|
KIRQL oldIrql;
|
1999-06-09 15:50:16 +00:00
|
|
|
|
1999-11-02 08:55:45 +00:00
|
|
|
KeAcquireSpinLock(&PiThreadListLock, &oldIrql);
|
|
|
|
if (Thread->Tcb.State == THREAD_STATE_RUNNABLE)
|
|
|
|
{
|
|
|
|
RemoveEntryList(&Thread->Tcb.QueueListEntry);
|
|
|
|
}
|
1999-12-02 20:53:55 +00:00
|
|
|
Thread->Tcb.State = THREAD_STATE_TERMINATED_2;
|
1999-10-07 23:46:27 +00:00
|
|
|
Thread->Tcb.DispatcherHeader.SignalState = TRUE;
|
1999-09-06 21:32:57 +00:00
|
|
|
KeDispatcherObjectWake(&Thread->Tcb.DispatcherHeader);
|
1999-11-02 08:55:45 +00:00
|
|
|
KeReleaseSpinLock(&PiThreadListLock, oldIrql);
|
1999-12-18 17:48:23 +00:00
|
|
|
ObDereferenceObject(Thread);
|
1999-01-16 21:03:00 +00:00
|
|
|
}
|
|
|
|
|
1999-11-24 11:51:55 +00:00
|
|
|
NTSTATUS STDCALL PiTerminateProcess(PEPROCESS Process,
|
|
|
|
NTSTATUS ExitStatus)
|
|
|
|
{
|
|
|
|
KIRQL oldlvl;
|
|
|
|
|
|
|
|
DPRINT("PsTerminateProcess(Process %x, ExitStatus %x)\n",
|
|
|
|
Process, ExitStatus);
|
|
|
|
|
|
|
|
PiTerminateProcessThreads(Process, ExitStatus);
|
|
|
|
KeRaiseIrql(DISPATCH_LEVEL, &oldlvl);
|
|
|
|
Process->Pcb.ProcessState = PROCESS_STATE_TERMINATED;
|
|
|
|
Process->Pcb.DispatcherHeader.SignalState = TRUE;
|
|
|
|
KeDispatcherObjectWake(&Process->Pcb.DispatcherHeader);
|
|
|
|
KeLowerIrql(oldlvl);
|
|
|
|
return(STATUS_SUCCESS);
|
|
|
|
}
|
1999-01-16 21:03:00 +00:00
|
|
|
|
1999-11-02 08:55:45 +00:00
|
|
|
NTSTATUS STDCALL NtTerminateProcess(IN HANDLE ProcessHandle,
|
|
|
|
IN NTSTATUS ExitStatus)
|
1999-01-16 21:03:00 +00:00
|
|
|
{
|
|
|
|
NTSTATUS Status;
|
|
|
|
PEPROCESS Process;
|
1999-04-01 12:39:43 +00:00
|
|
|
|
1999-06-18 22:11:21 +00:00
|
|
|
DPRINT("NtTerminateProcess(ProcessHandle %x, ExitStatus %x)\n",
|
1999-04-01 12:39:43 +00:00
|
|
|
ProcessHandle, ExitStatus);
|
|
|
|
|
1999-01-16 21:03:00 +00:00
|
|
|
Status = ObReferenceObjectByHandle(ProcessHandle,
|
|
|
|
PROCESS_TERMINATE,
|
|
|
|
PsProcessType,
|
|
|
|
UserMode,
|
|
|
|
(PVOID*)&Process,
|
|
|
|
NULL);
|
|
|
|
if (Status != STATUS_SUCCESS)
|
|
|
|
{
|
|
|
|
return(Status);
|
|
|
|
}
|
1999-04-01 12:39:43 +00:00
|
|
|
|
1999-11-24 11:51:55 +00:00
|
|
|
PiTerminateProcess(Process, ExitStatus);
|
1999-01-16 21:03:00 +00:00
|
|
|
if (PsGetCurrentThread()->ThreadsProcess == Process)
|
|
|
|
{
|
1999-04-01 12:39:43 +00:00
|
|
|
ObDereferenceObject(Process);
|
1999-01-16 21:03:00 +00:00
|
|
|
PsTerminateCurrentThread(ExitStatus);
|
|
|
|
}
|
1999-04-01 12:39:43 +00:00
|
|
|
ObDereferenceObject(Process);
|
1999-01-16 21:03:00 +00:00
|
|
|
return(STATUS_SUCCESS);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-11-12 12:01:17 +00:00
|
|
|
NTSTATUS STDCALL NtTerminateThread(IN HANDLE ThreadHandle,
|
|
|
|
IN NTSTATUS ExitStatus)
|
1999-01-16 21:03:00 +00:00
|
|
|
{
|
|
|
|
PETHREAD Thread;
|
|
|
|
NTSTATUS Status;
|
|
|
|
|
|
|
|
Status = ObReferenceObjectByHandle(ThreadHandle,
|
|
|
|
THREAD_TERMINATE,
|
|
|
|
PsThreadType,
|
|
|
|
UserMode,
|
|
|
|
(PVOID*)&Thread,
|
|
|
|
NULL);
|
|
|
|
if (Status != STATUS_SUCCESS)
|
|
|
|
{
|
|
|
|
return(Status);
|
|
|
|
}
|
1999-12-02 20:53:55 +00:00
|
|
|
|
|
|
|
ObDereferenceObject(Thread);
|
|
|
|
|
1999-01-16 21:03:00 +00:00
|
|
|
if (Thread == PsGetCurrentThread())
|
|
|
|
{
|
|
|
|
PsTerminateCurrentThread(ExitStatus);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
PsTerminateOtherThread(Thread, ExitStatus);
|
|
|
|
}
|
1999-02-06 18:34:14 +00:00
|
|
|
return(STATUS_SUCCESS);
|
1999-01-16 21:03:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
NTSTATUS PsTerminateSystemThread(NTSTATUS ExitStatus)
|
|
|
|
/*
|
|
|
|
* FUNCTION: Terminates the current thread
|
|
|
|
* ARGUMENTS:
|
|
|
|
* ExitStatus = Status to pass to the creater
|
|
|
|
* RETURNS: Doesn't
|
|
|
|
*/
|
|
|
|
{
|
|
|
|
PsTerminateCurrentThread(ExitStatus);
|
1999-02-06 18:34:14 +00:00
|
|
|
return(STATUS_SUCCESS);
|
1999-01-16 21:03:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-11-12 12:01:17 +00:00
|
|
|
NTSTATUS STDCALL NtRegisterThreadTerminatePort(HANDLE TerminationPort)
|
1999-01-16 21:03:00 +00:00
|
|
|
{
|
1999-11-12 12:01:17 +00:00
|
|
|
UNIMPLEMENTED;
|
1999-01-16 21:03:00 +00:00
|
|
|
}
|