ReactOS  0.4.14-dev-49-gfb4591c
psnotify.c
Go to the documentation of this file.
1 /*
2  * PROJECT: ReactOS Kernel
3  * LICENSE: GPL - See COPYING in the top level directory
4  * FILE: ntoskrnl/ps/psnotify.c
5  * PURPOSE: Process Manager: Callbacks to Registered Clients (Drivers)
6  * PROGRAMMERS: Alex Ionescu (alex.ionescu@reactos.org)
7  * Thomas Weidenmueller (w3seek@reactos.org)
8  */
9 
10 /* INCLUDES ******************************************************************/
11 
12 #include <ntoskrnl.h>
13 #define NDEBUG
14 #include <debug.h>
15 
16 /* GLOBALS *******************************************************************/
17 
25 
26 /* PUBLIC FUNCTIONS **********************************************************/
27 
28 /*
29  * @implemented
30  */
32 NTAPI
35 {
36  ULONG i;
38  PAGED_CODE();
39 
40  /* Check if we're removing */
41  if (Remove)
42  {
43  /* Loop all the routines */
44  for (i = 0; i < PSP_MAX_CREATE_PROCESS_NOTIFY; i++)
45  {
46  /* Reference the callback block */
48  if (!CallBack) continue;
49 
50  /* Check it this is a matching block */
51  if (ExGetCallBackBlockRoutine(CallBack) == (PVOID)NotifyRoutine)
52  {
53  /* Try removing it if it matches */
55  NULL,
56  CallBack))
57  {
58  /* Decrement the number of routines */
60 
61  /* Dereference the block */
63  CallBack);
64 
65  /* Wait for active callbacks */
66  ExWaitForCallBacks(CallBack);
67 
68  /* Free the callback and exit */
69  ExFreeCallBack(CallBack);
70  return STATUS_SUCCESS;
71  }
72 
73  /* Dereference the block */
75  CallBack);
76  }
77  }
78 
79  /* We didn't find any matching block */
81  }
82  else
83  {
84  /* Allocate a callback */
85  CallBack = ExAllocateCallBack((PVOID)NotifyRoutine, NULL);
86  if (!CallBack) return STATUS_INSUFFICIENT_RESOURCES;
87 
88  /* Loop all callbacks */
89  for (i = 0; i < PSP_MAX_CREATE_PROCESS_NOTIFY; i++)
90  {
91  /* Add this routine if it's an empty slot */
93  CallBack,
94  NULL))
95  {
96  /* Found and inserted into an empty slot, return */
98  return STATUS_SUCCESS;
99  }
100  }
101 
102  /* We didn't find a free slot, free the callback and fail */
103  ExFreeCallBack(CallBack);
105  }
106 }
107 
108 /*
109  * @implemented
110  */
111 ULONG
112 NTAPI
113 PsSetLegoNotifyRoutine(PVOID LegoNotifyRoutine)
114 {
115  /* Set the System-Wide Lego Routine */
116  PspLegoNotifyRoutine = LegoNotifyRoutine;
117 
118  /* Return the location to the Lego Data */
119  return FIELD_OFFSET(KTHREAD, LegoData);
120 }
121 
122 /*
123  * @implemented
124  */
125 NTSTATUS
126 NTAPI
128 {
129  ULONG i;
131  PAGED_CODE();
132 
133  /* Loop all callbacks */
134  for (i = 0; i < PSP_MAX_LOAD_IMAGE_NOTIFY; i++)
135  {
136  /* Reference this slot */
138  if (CallBack)
139  {
140  /* Check for a match */
141  if (ExGetCallBackBlockRoutine(CallBack) == (PVOID)NotifyRoutine)
142  {
143  /* Try removing it if it matches */
145  NULL,
146  CallBack))
147  {
148  /* We removed it, now dereference the block */
151  CallBack);
152 
153  /* Wait for active callbacks */
154  ExWaitForCallBacks(CallBack);
155 
156  /* Free the callback and return */
157  ExFreeCallBack(CallBack);
158  return STATUS_SUCCESS;
159  }
160  }
161 
162  /* Dereference the callback */
164  }
165  }
166 
167  /* Nothing found to remove */
169 }
170 
171 /*
172  * @implemented
173  */
174 NTSTATUS
175 NTAPI
177 {
178  ULONG i;
180  PAGED_CODE();
181 
182  /* Allocate a callback */
183  CallBack = ExAllocateCallBack((PVOID)NotifyRoutine, NULL);
184  if (!CallBack) return STATUS_INSUFFICIENT_RESOURCES;
185 
186  /* Loop callbacks */
187  for (i = 0; i < PSP_MAX_LOAD_IMAGE_NOTIFY; i++)
188  {
189  /* Add this entry if the slot is empty */
191  CallBack,
192  NULL))
193  {
194  /* Return success */
197  return STATUS_SUCCESS;
198  }
199  }
200 
201  /* No free space found, fail */
202  ExFreeCallBack(CallBack);
204 }
205 
206 /*
207  * @implemented
208  */
209 NTSTATUS
210 NTAPI
212 {
213  ULONG i;
215  PAGED_CODE();
216 
217  /* Loop all callbacks */
218  for (i = 0; i < PSP_MAX_CREATE_THREAD_NOTIFY; i++)
219  {
220  /* Reference this slot */
222  if (CallBack)
223  {
224  /* Check for a match */
225  if (ExGetCallBackBlockRoutine(CallBack) == (PVOID)NotifyRoutine)
226  {
227  /* Try removing it if it matches */
229  NULL,
230  CallBack))
231  {
232  /* We removed it, now dereference the block */
235  CallBack);
236 
237  /* Wait for active callbacks */
238  ExWaitForCallBacks(CallBack);
239 
240  /* Free the callback and return */
241  ExFreeCallBack(CallBack);
242  return STATUS_SUCCESS;
243  }
244  }
245 
246  /* Dereference the callback */
248  }
249  }
250 
251  /* Nothing found to remove */
253 }
254 
255 /*
256  * @implemented
257  */
258 NTSTATUS
259 NTAPI
261 {
262  ULONG i;
264  PAGED_CODE();
265 
266  /* Allocate a callback */
267  CallBack = ExAllocateCallBack((PVOID)NotifyRoutine, NULL);
268  if (!CallBack) return STATUS_INSUFFICIENT_RESOURCES;
269 
270  /* Loop callbacks */
271  for (i = 0; i < PSP_MAX_CREATE_THREAD_NOTIFY; i++)
272  {
273  /* Add this entry if the slot is empty */
275  CallBack,
276  NULL))
277  {
278  /* Return success */
280  return STATUS_SUCCESS;
281  }
282  }
283 
284  /* No free space found, fail */
285  ExFreeCallBack(CallBack);
287 }
288 
289 /* EOF */
BOOLEAN PsImageNotifyEnabled
Definition: psnotify.c:18
PEX_CALLBACK_ROUTINE_BLOCK NTAPI ExAllocateCallBack(IN PEX_CALLBACK_FUNCTION Function, IN PVOID Context)
Definition: callback.c:54
#define PSP_MAX_CREATE_PROCESS_NOTIFY
Definition: ps.h:66
#define IN
Definition: typedefs.h:38
#define TRUE
Definition: types.h:120
#define STATUS_INSUFFICIENT_RESOURCES
Definition: udferr_usr.h:158
VOID(NTAPI * PCREATE_THREAD_NOTIFY_ROUTINE)(_In_ HANDLE ProcessId, _In_ HANDLE ThreadId, _In_ BOOLEAN Create)
Definition: pstypes.h:189
#define STATUS_INVALID_PARAMETER
Definition: udferr_usr.h:135
LONG NTSTATUS
Definition: precomp.h:26
NTSTATUS NTAPI PsRemoveLoadImageNotifyRoutine(IN PLOAD_IMAGE_NOTIFY_ROUTINE NotifyRoutine)
Definition: psnotify.c:127
#define PAGED_CODE()
Definition: video.h:57
EX_CALLBACK PspProcessNotifyRoutine[PSP_MAX_CREATE_PROCESS_NOTIFY]
Definition: psnotify.c:22
EX_CALLBACK PspThreadNotifyRoutine[PSP_MAX_CREATE_THREAD_NOTIFY]
Definition: psnotify.c:21
ULONG PspThreadNotifyRoutineCount
Definition: psnotify.c:19
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint i
Definition: glfuncs.h:248
PEX_CALLBACK_ROUTINE_BLOCK NTAPI ExReferenceCallBackBlock(IN OUT PEX_CALLBACK CallBack)
Definition: callback.c:122
NTSTATUS(* NTAPI)(IN PFILE_FULL_EA_INFORMATION EaBuffer, IN ULONG EaLength, OUT PULONG ErrorOffset)
Definition: IoEaTest.cpp:117
VOID(NTAPI * PLOAD_IMAGE_NOTIFY_ROUTINE)(_In_ PUNICODE_STRING FullImageName, _In_ HANDLE ProcessId, _In_ PIMAGE_INFO ImageInfo)
Definition: pstypes.h:220
unsigned char BOOLEAN
VOID(NTAPI * PLEGO_NOTIFY_ROUTINE)(_In_ PKTHREAD Thread)
Definition: pstypes.h:601
smooth NULL
Definition: ftsmooth.c:416
ULONG NTAPI PsSetLegoNotifyRoutine(PVOID LegoNotifyRoutine)
Definition: psnotify.c:113
BOOLEAN NTAPI ExCompareExchangeCallBack(IN OUT PEX_CALLBACK CallBack, IN PEX_CALLBACK_ROUTINE_BLOCK NewBlock, IN PEX_CALLBACK_ROUTINE_BLOCK OldBlock)
Definition: callback.c:170
_In_ BOOLEAN Remove
Definition: psfuncs.h:110
#define PSP_MAX_CREATE_THREAD_NOTIFY
Definition: ps.h:64
ULONG PspProcessNotifyRoutineCount
Definition: psnotify.c:19
#define PSP_MAX_LOAD_IMAGE_NOTIFY
Definition: ps.h:65
PLEGO_NOTIFY_ROUTINE PspLegoNotifyRoutine
Definition: psnotify.c:24
NTSTATUS NTAPI PsSetLoadImageNotifyRoutine(IN PLOAD_IMAGE_NOTIFY_ROUTINE NotifyRoutine)
Definition: psnotify.c:176
ULONG PspLoadImageNotifyRoutineCount
Definition: psnotify.c:20
VOID(NTAPI * PCREATE_PROCESS_NOTIFY_ROUTINE)(_In_ HANDLE ParentId, _In_ HANDLE ProcessId, _In_ BOOLEAN Create)
Definition: pstypes.h:160
NTSTATUS NTAPI PsSetCreateThreadNotifyRoutine(IN PCREATE_THREAD_NOTIFY_ROUTINE NotifyRoutine)
Definition: psnotify.c:260
#define InterlockedDecrement
Definition: armddk.h:52
EX_CALLBACK PspLoadImageNotifyRoutine[PSP_MAX_LOAD_IMAGE_NOTIFY]
Definition: psnotify.c:23
NTSTATUS NTAPI PsRemoveCreateThreadNotifyRoutine(IN PCREATE_THREAD_NOTIFY_ROUTINE NotifyRoutine)
Definition: psnotify.c:211
PEX_CALLBACK_FUNCTION NTAPI ExGetCallBackBlockRoutine(IN PEX_CALLBACK_ROUTINE_BLOCK CallbackBlock)
Definition: callback.c:93
VOID NTAPI ExDereferenceCallBackBlock(IN OUT PEX_CALLBACK CallBack, IN PEX_CALLBACK_ROUTINE_BLOCK CallbackBlock)
Definition: callback.c:109
#define InterlockedIncrement
Definition: armddk.h:53
#define FIELD_OFFSET(t, f)
Definition: typedefs.h:254
VOID NTAPI ExWaitForCallBacks(IN PEX_CALLBACK_ROUTINE_BLOCK CallbackBlock)
Definition: callback.c:85
#define STATUS_PROCEDURE_NOT_FOUND
Definition: ntstatus.h:344
VOID NTAPI ExFreeCallBack(IN PEX_CALLBACK_ROUTINE_BLOCK CallbackBlock)
Definition: callback.c:77
unsigned int ULONG
Definition: retypes.h:1
return STATUS_SUCCESS
Definition: btrfs.c:2966
signed int * PLONG
Definition: retypes.h:5
NTSTATUS NTAPI PsSetCreateProcessNotifyRoutine(IN PCREATE_PROCESS_NOTIFY_ROUTINE NotifyRoutine, IN BOOLEAN Remove)
Definition: psnotify.c:33