From 059a7a0f64bd554f5cd390dd46a53576db61e920 Mon Sep 17 00:00:00 2001 From: Eric Kohl Date: Thu, 3 Jan 2002 18:02:34 +0000 Subject: [PATCH] Implemented process and thread notification routines. svn path=/trunk/; revision=2479 --- reactos/include/ddk/psfuncs.h | 9 ++++- reactos/include/ddk/pstypes.h | 8 ++++ reactos/ntoskrnl/ntoskrnl.def | 6 +-- reactos/ntoskrnl/ntoskrnl.edf | 6 +-- reactos/ntoskrnl/ps/create.c | 73 +++++++++++++++++++++++++++-------- reactos/ntoskrnl/ps/process.c | 58 ++++++++++++++++++++++------ 6 files changed, 125 insertions(+), 35 deletions(-) diff --git a/reactos/include/ddk/psfuncs.h b/reactos/include/ddk/psfuncs.h index 8dca7ccb5b7..d4a804995c8 100644 --- a/reactos/include/ddk/psfuncs.h +++ b/reactos/include/ddk/psfuncs.h @@ -1,4 +1,4 @@ -/* $Id: psfuncs.h,v 1.17 2002/01/03 14:01:16 ekohl Exp $ +/* $Id: psfuncs.h,v 1.18 2002/01/03 17:58:44 ekohl Exp $ */ #ifndef _INCLUDE_DDK_PSFUNCS_H #define _INCLUDE_DDK_PSFUNCS_H @@ -87,6 +87,13 @@ NTSTATUS STDCALL PsLookupThreadByThreadId(IN PVOID ThreadId, OUT struct _ETHREAD **Thread); // OUT PETHREAD *Thread); +NTSTATUS STDCALL +PsSetCreateProcessNotifyRoutine(IN PCREATE_PROCESS_NOTIFY_ROUTINE NotifyRoutine, + IN BOOLEAN Remove); + +NTSTATUS STDCALL +PsSetCreateThreadNotifyRoutine(IN PCREATE_THREAD_NOTIFY_ROUTINE NotifyRoutine); + #endif /* EOF */ diff --git a/reactos/include/ddk/pstypes.h b/reactos/include/ddk/pstypes.h index 695f00eb7c8..9aae408fee5 100644 --- a/reactos/include/ddk/pstypes.h +++ b/reactos/include/ddk/pstypes.h @@ -20,6 +20,14 @@ struct _KTHREAD; typedef NTSTATUS STDCALL (*PKSTART_ROUTINE)(PVOID StartContext); +typedef VOID STDCALL (*PCREATE_PROCESS_NOTIFY_ROUTINE)(HANDLE ParentId, + HANDLE ProcessId, + BOOLEAN Create); + +typedef VOID STDCALL (*PCREATE_THREAD_NOTIFY_ROUTINE)(HANDLE ProcessId, + HANDLE ThreadId, + BOOLEAN Create); + typedef struct _STACK_INFORMATION { PVOID BaseAddress; diff --git a/reactos/ntoskrnl/ntoskrnl.def b/reactos/ntoskrnl/ntoskrnl.def index aff07dba16a..38e7dea38b6 100644 --- a/reactos/ntoskrnl/ntoskrnl.def +++ b/reactos/ntoskrnl/ntoskrnl.def @@ -1,4 +1,4 @@ -; $Id: ntoskrnl.def,v 1.121 2002/01/03 14:02:04 ekohl Exp $ +; $Id: ntoskrnl.def,v 1.122 2002/01/03 18:02:34 ekohl Exp $ ; ; reactos/ntoskrnl/ntoskrnl.def ; @@ -609,8 +609,8 @@ PsReferenceImpersonationToken@16 PsReferencePrimaryToken@4 ;PsReturnPoolQuota@12 PsRevertToSelf@0 -;PsSetCreateProcessNotifyRoutine@8 -;PsSetCreateThreadNotifyRoutine@4 +PsSetCreateProcessNotifyRoutine@8 +PsSetCreateThreadNotifyRoutine@4 ;PsSetLegoNotifyRoutine@4 ;PsSetProcessPriorityByClass@8 PsTerminateSystemThread@4 diff --git a/reactos/ntoskrnl/ntoskrnl.edf b/reactos/ntoskrnl/ntoskrnl.edf index 0949157a870..6d23e6262d9 100644 --- a/reactos/ntoskrnl/ntoskrnl.edf +++ b/reactos/ntoskrnl/ntoskrnl.edf @@ -1,4 +1,4 @@ -; $Id: ntoskrnl.edf,v 1.107 2002/01/03 14:02:04 ekohl Exp $ +; $Id: ntoskrnl.edf,v 1.108 2002/01/03 18:02:34 ekohl Exp $ ; ; reactos/ntoskrnl/ntoskrnl.def ; @@ -609,8 +609,8 @@ PsReferenceImpersonationToken=PsReferenceImpersonationToken@16 PsReferencePrimaryToken=PsReferencePrimaryToken@4 ;PsReturnPoolQuota PsRevertToSelf=PsRevertToSelf@0 -;PsSetCreateProcessNotifyRoutine -;PsSetCreateThreadNotifyRoutine +PsSetCreateProcessNotifyRoutine=PsSetCreateProcessNotifyRoutine@8 +PsSetCreateThreadNotifyRoutine=PsSetCreateThreadNotifyRoutine@4 ;PsSetLegoNotifyRoutine ;PsSetProcessPriorityByClass PsTerminateSystemThread=PsTerminateSystemThread@4 diff --git a/reactos/ntoskrnl/ps/create.c b/reactos/ntoskrnl/ps/create.c index 9ca613785ec..082075d44d5 100644 --- a/reactos/ntoskrnl/ps/create.c +++ b/reactos/ntoskrnl/ps/create.c @@ -1,4 +1,4 @@ -/* $Id: create.c,v 1.41 2001/11/21 18:44:25 ekohl Exp $ +/* $Id: create.c,v 1.42 2002/01/03 17:59:09 ekohl Exp $ * * COPYRIGHT: See COPYING in the top level directory * PROJECT: ReactOS kernel @@ -40,6 +40,12 @@ extern ULONG PiNrThreads; extern LIST_ENTRY PiThreadListHead; +#define MAX_THREAD_NOTIFY_ROUTINE_COUNT 8 + +static ULONG PiThreadNotifyRoutineCount = 0; +static PCREATE_THREAD_NOTIFY_ROUTINE +PiThreadNotifyRoutine[MAX_THREAD_NOTIFY_ROUTINE_COUNT]; + /* FUNCTIONS ***************************************************************/ NTSTATUS STDCALL @@ -299,20 +305,32 @@ PsBeginThread(PKSTART_ROUTINE StartRoutine, PVOID StartContext) VOID STDCALL PiDeleteThread(PVOID ObjectBody) { - KIRQL oldIrql; - - DPRINT("PiDeleteThread(ObjectBody %x)\n",ObjectBody); - - KeAcquireSpinLock(&PiThreadListLock, &oldIrql); - DPRINT("Process %x(%d)\n", ((PETHREAD)ObjectBody)->ThreadsProcess, - ObGetReferenceCount(((PETHREAD)ObjectBody)->ThreadsProcess)); - ObDereferenceObject(((PETHREAD)ObjectBody)->ThreadsProcess); - ((PETHREAD)ObjectBody)->ThreadsProcess = NULL; - PiNrThreads--; - RemoveEntryList(&((PETHREAD)ObjectBody)->Tcb.ThreadListEntry); - HalReleaseTask((PETHREAD)ObjectBody); - KeReleaseSpinLock(&PiThreadListLock, oldIrql); - DPRINT("PiDeleteThread() finished\n"); + KIRQL oldIrql; + PETHREAD Thread; + ULONG i; + + DPRINT("PiDeleteThread(ObjectBody %x)\n",ObjectBody); + + KeAcquireSpinLock(&PiThreadListLock, &oldIrql); + Thread = (PETHREAD)ObjectBody; + + for (i = 0; i < PiThreadNotifyRoutineCount; i++) + { + PiThreadNotifyRoutine[i](Thread->Cid.UniqueProcess, + Thread->Cid.UniqueThread, + FALSE); + } + + DPRINT("Process %x(%d)\n", + Thread->ThreadsProcess, + ObGetReferenceCount(Thread->ThreadsProcess)); + ObDereferenceObject(Thread->ThreadsProcess); + Thread->ThreadsProcess = NULL; + PiNrThreads--; + RemoveEntryList(&Thread->Tcb.ThreadListEntry); + HalReleaseTask(Thread); + KeReleaseSpinLock(&PiThreadListLock, oldIrql); + DPRINT("PiDeleteThread() finished\n"); } VOID STDCALL @@ -338,6 +356,7 @@ PsInitializeThread(HANDLE ProcessHandle, NTSTATUS Status; KIRQL oldIrql; PEPROCESS Process; + ULONG i; /* * Reference process @@ -408,8 +427,15 @@ PsInitializeThread(HANDLE ProcessHandle, Thread->Tcb.BasePriority = Thread->ThreadsProcess->Pcb.BasePriority; Thread->Tcb.Priority = Thread->Tcb.BasePriority; - - return(STATUS_SUCCESS); + + for (i = 0; i < PiThreadNotifyRoutineCount; i++) + { + PiThreadNotifyRoutine[i](Thread->Cid.UniqueProcess, + Thread->Cid.UniqueThread, + TRUE); + } + + return(STATUS_SUCCESS); } @@ -656,4 +682,17 @@ PsCreateSystemThread(PHANDLE ThreadHandle, return(STATUS_SUCCESS); } + +NTSTATUS STDCALL +PsSetCreateThreadNotifyRoutine(IN PCREATE_THREAD_NOTIFY_ROUTINE NotifyRoutine) +{ + if (PiThreadNotifyRoutineCount >= MAX_THREAD_NOTIFY_ROUTINE_COUNT) + return(STATUS_INSUFFICIENT_RESOURCES); + + PiThreadNotifyRoutine[PiThreadNotifyRoutineCount] = NotifyRoutine; + PiThreadNotifyRoutineCount++; + + return(STATUS_SUCCESS); +} + /* EOF */ diff --git a/reactos/ntoskrnl/ps/process.c b/reactos/ntoskrnl/ps/process.c index 0dd2a45bad3..3cb635a979d 100644 --- a/reactos/ntoskrnl/ps/process.c +++ b/reactos/ntoskrnl/ps/process.c @@ -1,4 +1,4 @@ -/* $Id: process.c,v 1.74 2002/01/03 14:03:05 ekohl Exp $ +/* $Id: process.c,v 1.75 2002/01/03 17:59:09 ekohl Exp $ * * COPYRIGHT: See COPYING in the top level directory * PROJECT: ReactOS kernel @@ -46,6 +46,12 @@ static GENERIC_MAPPING PiProcessMapping = {PROCESS_READ, PROCESS_EXECUTE, PROCESS_ALL_ACCESS}; +#define MAX_PROCESS_NOTIFY_ROUTINE_COUNT 8 + +static ULONG PiProcessNotifyRoutineCount = 0; +static PCREATE_PROCESS_NOTIFY_ROUTINE +PiProcessNotifyRoutine[MAX_PROCESS_NOTIFY_ROUTINE_COUNT]; + /* FUNCTIONS *****************************************************************/ @@ -300,20 +306,29 @@ PiFreeSymbols(PPEB Peb) VOID STDCALL PiDeleteProcess(PVOID ObjectBody) { - KIRQL oldIrql; - - DPRINT("PiDeleteProcess(ObjectBody %x)\n",ObjectBody); - - KeAcquireSpinLock(&PsProcessListLock, &oldIrql); - RemoveEntryList(&((PEPROCESS)ObjectBody)->ProcessListEntry); + KIRQL oldIrql; + PEPROCESS Process; + ULONG i; + + DPRINT("PiDeleteProcess(ObjectBody %x)\n",ObjectBody); + + Process = (PEPROCESS)Process; + KeAcquireSpinLock(&PsProcessListLock, &oldIrql); + for (i = 0; i < PiProcessNotifyRoutineCount; i++) + { + PiProcessNotifyRoutine[i](Process->InheritedFromUniqueProcessId, + (HANDLE)Process->UniqueProcessId, + FALSE); + } + RemoveEntryList(&Process->ProcessListEntry); KeReleaseSpinLock(&PsProcessListLock, oldIrql); #ifdef KDBG - PiFreeSymbols(((PEPROCESS)ObjectBody)->Peb); + PiFreeSymbols(Process->Peb); #endif /* KDBG */ - (VOID)MmReleaseMmInfo((PEPROCESS)ObjectBody); - ObDeleteHandleTable((PEPROCESS)ObjectBody); + (VOID)MmReleaseMmInfo(Process); + ObDeleteHandleTable(Process); } @@ -393,7 +408,7 @@ PsGetCurrentProcess(VOID) PEPROCESS STDCALL IoGetCurrentProcess(VOID) { - return(PsGetCurrentProcess()); + return(PsGetCurrentProcess()); } NTSTATUS STDCALL @@ -454,6 +469,7 @@ NtCreateProcess(OUT PHANDLE ProcessHandle, PEPORT ExceptionPort; PVOID BaseAddress; PMEMORY_AREA MemoryArea; + ULONG i; DPRINT("NtCreateProcess(ObjectAttributes %x)\n",ObjectAttributes); @@ -500,6 +516,12 @@ NtCreateProcess(OUT PHANDLE ProcessHandle, MmCopyMmInfo(ParentProcess, Process); KeAcquireSpinLock(&PsProcessListLock, &oldIrql); + for (i = 0; i < PiProcessNotifyRoutineCount; i++) + { + PiProcessNotifyRoutine[i](Process->InheritedFromUniqueProcessId, + (HANDLE)Process->UniqueProcessId, + TRUE); + } InsertHeadList(&PsProcessListHead, &Process->ProcessListEntry); InitializeListHead(&Process->ThreadListHead); KeReleaseSpinLock(&PsProcessListLock, oldIrql); @@ -1183,4 +1205,18 @@ PsLookupProcessByProcessId(IN PVOID ProcessId, return(STATUS_INVALID_PARAMETER); } + +NTSTATUS STDCALL +PsSetCreateProcessNotifyRoutine(IN PCREATE_PROCESS_NOTIFY_ROUTINE NotifyRoutine, + IN BOOLEAN Remove) +{ + if (PiProcessNotifyRoutineCount >= MAX_PROCESS_NOTIFY_ROUTINE_COUNT) + return(STATUS_INSUFFICIENT_RESOURCES); + + PiProcessNotifyRoutine[PiProcessNotifyRoutineCount] = NotifyRoutine; + PiProcessNotifyRoutineCount++; + + return(STATUS_SUCCESS); +} + /* EOF */