1e1ff028b72bb6504e4e94fe7ca0c7581c7a033d
[reactos.git] / reactos / ntoskrnl / ob / object.c
1 /* $Id: object.c,v 1.80 2004/07/19 12:48:59 ekohl Exp $
2 *
3 * COPYRIGHT: See COPYING in the top level directory
4 * PROJECT: ReactOS kernel
5 * FILE: ntoskrnl/ob/object.c
6 * PURPOSE: Implements generic object managment functions
7 * PROGRAMMERS David Welch (welch@cwcom.net), Skywing (skywing@valhallalegends.com)
8 * UPDATE HISTORY:
9 * 10/06/98: Created
10 * 09/13/03: Fixed various ObXxx routines to not call retention
11 * checks directly at a raised IRQL.
12 */
13
14 /* INCLUDES *****************************************************************/
15
16 #define NTOS_MODE_KERNEL
17 #include <ntos.h>
18 #include <roscfg.h>
19 #include <internal/ob.h>
20 #include <internal/ps.h>
21 #include <internal/id.h>
22 #include <internal/ke.h>
23
24 #define NDEBUG
25 #include <internal/debug.h>
26
27
28 typedef struct _RETENTION_CHECK_PARAMS
29 {
30 WORK_QUEUE_ITEM WorkItem;
31 POBJECT_HEADER ObjectHeader;
32 } RETENTION_CHECK_PARAMS, *PRETENTION_CHECK_PARAMS;
33
34
35 /* FUNCTIONS ************************************************************/
36
37 PVOID HEADER_TO_BODY(POBJECT_HEADER obj)
38 {
39 return(((char*)obj)+sizeof(OBJECT_HEADER)-sizeof(COMMON_BODY_HEADER));
40 }
41
42
43 POBJECT_HEADER BODY_TO_HEADER(PVOID body)
44 {
45 PCOMMON_BODY_HEADER chdr = (PCOMMON_BODY_HEADER)body;
46 return(CONTAINING_RECORD((&(chdr->Type)),OBJECT_HEADER,Type));
47 }
48
49
50 /**********************************************************************
51 * NAME PRIVATE
52 * ObFindObject@16
53 *
54 * DESCRIPTION
55 *
56 * ARGUMENTS
57 * ObjectAttributes
58 *
59 * ReturnedObject
60 *
61 * RemainigPath
62 * Pointer to a unicode string that will contain the
63 * remaining path if the function returns successfully.
64 * The caller must free the buffer after use by calling
65 * RtlFreeUnicodeString ().
66 *
67 * ObjectType
68 * Optional pointer to an object type. This is used to
69 * descide if a symbolic link object will be parsed or not.
70 *
71 * RETURN VALUE
72 */
73 NTSTATUS
74 ObFindObject(POBJECT_ATTRIBUTES ObjectAttributes,
75 PVOID* ReturnedObject,
76 PUNICODE_STRING RemainingPath,
77 POBJECT_TYPE ObjectType)
78 {
79 PVOID NextObject;
80 PVOID CurrentObject;
81 PVOID RootObject;
82 POBJECT_HEADER CurrentHeader;
83 NTSTATUS Status;
84 PWSTR current;
85 UNICODE_STRING PathString;
86 ULONG Attributes;
87 PUNICODE_STRING ObjectName;
88
89 DPRINT("ObFindObject(ObjectAttributes %x, ReturnedObject %x, "
90 "RemainingPath %x)\n",ObjectAttributes,ReturnedObject,RemainingPath);
91 DPRINT("ObjectAttributes->ObjectName %wZ\n",
92 ObjectAttributes->ObjectName);
93
94 RtlInitUnicodeString (RemainingPath, NULL);
95
96 if (ObjectAttributes->RootDirectory == NULL)
97 {
98 ObReferenceObjectByPointer(NameSpaceRoot,
99 DIRECTORY_TRAVERSE,
100 NULL,
101 UserMode);
102 CurrentObject = NameSpaceRoot;
103 }
104 else
105 {
106 Status = ObReferenceObjectByHandle(ObjectAttributes->RootDirectory,
107 DIRECTORY_TRAVERSE,
108 NULL,
109 UserMode,
110 &CurrentObject,
111 NULL);
112 if (!NT_SUCCESS(Status))
113 {
114 return Status;
115 }
116 }
117
118 ObjectName = ObjectAttributes->ObjectName;
119 if (ObjectName->Length == 0 ||
120 ObjectName->Buffer[0] == UNICODE_NULL)
121 {
122 *ReturnedObject = CurrentObject;
123 return STATUS_SUCCESS;
124 }
125
126 if (ObjectAttributes->RootDirectory == NULL &&
127 ObjectName->Buffer[0] != L'\\')
128 {
129 ObDereferenceObject (CurrentObject);
130 return STATUS_UNSUCCESSFUL;
131 }
132
133 /* Create a zero-terminated copy of the object name */
134 PathString.Length = ObjectName->Length;
135 PathString.MaximumLength = ObjectName->Length + sizeof(WCHAR);
136 PathString.Buffer = ExAllocatePool (NonPagedPool,
137 PathString.MaximumLength);
138 if (PathString.Buffer == NULL)
139 {
140 ObDereferenceObject (CurrentObject);
141 return STATUS_INSUFFICIENT_RESOURCES;
142 }
143
144 RtlCopyMemory (PathString.Buffer,
145 ObjectName->Buffer,
146 ObjectName->Length);
147 PathString.Buffer[PathString.Length / sizeof(WCHAR)] = UNICODE_NULL;
148
149 current = PathString.Buffer;
150
151 RootObject = CurrentObject;
152 Attributes = ObjectAttributes->Attributes;
153 if (ObjectType == ObSymbolicLinkType)
154 Attributes |= OBJ_OPENLINK;
155
156 while (TRUE)
157 {
158 DPRINT("current %S\n",current);
159 CurrentHeader = BODY_TO_HEADER(CurrentObject);
160
161 DPRINT("Current ObjectType %wZ\n",
162 &CurrentHeader->ObjectType->TypeName);
163
164 if (CurrentHeader->ObjectType->Parse == NULL)
165 {
166 DPRINT("Current object can't parse\n");
167 break;
168 }
169 Status = CurrentHeader->ObjectType->Parse(CurrentObject,
170 &NextObject,
171 &PathString,
172 &current,
173 Attributes);
174 if (Status == STATUS_REPARSE)
175 {
176 /* reparse the object path */
177 NextObject = NameSpaceRoot;
178 current = PathString.Buffer;
179
180 ObReferenceObjectByPointer(NextObject,
181 DIRECTORY_TRAVERSE,
182 NULL,
183 UserMode);
184 }
185
186 if (NextObject == NULL)
187 {
188 break;
189 }
190 ObDereferenceObject(CurrentObject);
191 CurrentObject = NextObject;
192 }
193
194 if (current)
195 RtlCreateUnicodeString (RemainingPath, current);
196 RtlFreeUnicodeString (&PathString);
197 *ReturnedObject = CurrentObject;
198
199 return STATUS_SUCCESS;
200 }
201
202
203 /**********************************************************************
204 * NAME EXPORTED
205 * ObQueryNameString@16
206 *
207 * DESCRIPTION
208 *
209 * ARGUMENTS
210 *
211 * RETURN VALUE
212 *
213 * @implemented
214 */
215 NTSTATUS STDCALL
216 ObQueryNameString (IN PVOID Object,
217 OUT POBJECT_NAME_INFORMATION ObjectNameInfo,
218 IN ULONG Length,
219 OUT PULONG ReturnLength)
220 {
221 POBJECT_NAME_INFORMATION LocalInfo;
222 POBJECT_HEADER ObjectHeader;
223 ULONG LocalReturnLength;
224 NTSTATUS Status;
225
226 *ReturnLength = 0;
227
228 if (Length < sizeof(OBJECT_NAME_INFORMATION) + sizeof(WCHAR))
229 return STATUS_INVALID_BUFFER_SIZE;
230
231 ObjectNameInfo->Name.MaximumLength = (USHORT)(Length - sizeof(OBJECT_NAME_INFORMATION));
232 ObjectNameInfo->Name.Length = 0;
233 ObjectNameInfo->Name.Buffer =
234 (PWCHAR)((ULONG_PTR)ObjectNameInfo + sizeof(OBJECT_NAME_INFORMATION));
235 ObjectNameInfo->Name.Buffer[0] = 0;
236
237 ObjectHeader = BODY_TO_HEADER(Object);
238
239 if (ObjectHeader->ObjectType != NULL &&
240 ObjectHeader->ObjectType->QueryName != NULL)
241 {
242 DPRINT ("Calling %x\n", ObjectHeader->ObjectType->QueryName);
243 Status = ObjectHeader->ObjectType->QueryName (Object,
244 ObjectNameInfo,
245 Length,
246 ReturnLength);
247 }
248 else if (ObjectHeader->Name.Length > 0 && ObjectHeader->Name.Buffer != NULL)
249 {
250 DPRINT ("Object does not have a 'QueryName' function\n");
251
252 if (ObjectHeader->Parent == NameSpaceRoot)
253 {
254 DPRINT ("Reached the root directory\n");
255 ObjectNameInfo->Name.Length = 0;
256 ObjectNameInfo->Name.Buffer[0] = 0;
257 Status = STATUS_SUCCESS;
258 }
259 else if (ObjectHeader->Parent != NULL)
260 {
261 LocalInfo = ExAllocatePool (NonPagedPool,
262 sizeof(OBJECT_NAME_INFORMATION) +
263 MAX_PATH * sizeof(WCHAR));
264 if (LocalInfo == NULL)
265 return STATUS_INSUFFICIENT_RESOURCES;
266
267 Status = ObQueryNameString (ObjectHeader->Parent,
268 LocalInfo,
269 MAX_PATH * sizeof(WCHAR),
270 &LocalReturnLength);
271 if (!NT_SUCCESS (Status))
272 {
273 ExFreePool (LocalInfo);
274 return Status;
275 }
276
277 Status = RtlAppendUnicodeStringToString (&ObjectNameInfo->Name,
278 &LocalInfo->Name);
279
280 ExFreePool (LocalInfo);
281
282 if (!NT_SUCCESS (Status))
283 return Status;
284 }
285
286 DPRINT ("Object path %wZ\n", &ObjectHeader->Name);
287 Status = RtlAppendUnicodeToString (&ObjectNameInfo->Name,
288 L"\\");
289 if (!NT_SUCCESS (Status))
290 return Status;
291
292 Status = RtlAppendUnicodeStringToString (&ObjectNameInfo->Name,
293 &ObjectHeader->Name);
294 }
295 else
296 {
297 DPRINT ("Object is unnamed\n");
298
299 ObjectNameInfo->Name.MaximumLength = 0;
300 ObjectNameInfo->Name.Length = 0;
301 ObjectNameInfo->Name.Buffer = NULL;
302
303 Status = STATUS_SUCCESS;
304 }
305
306 if (NT_SUCCESS (Status))
307 {
308 ObjectNameInfo->Name.MaximumLength =
309 (ObjectNameInfo->Name.Length) ? ObjectNameInfo->Name.Length + sizeof(WCHAR) : 0;
310 *ReturnLength =
311 sizeof(OBJECT_NAME_INFORMATION) + ObjectNameInfo->Name.MaximumLength;
312 DPRINT ("Returned object path: %wZ\n", &ObjectNameInfo->Name);
313 }
314
315 return Status;
316 }
317
318
319 /**********************************************************************
320 * NAME EXPORTED
321 * ObCreateObject@36
322 *
323 * DESCRIPTION
324 *
325 * ARGUMENTS
326 *
327 * RETURN VALUE
328 * Status
329 *
330 * @implemented
331 */
332 NTSTATUS STDCALL
333 ObCreateObject (IN KPROCESSOR_MODE ObjectAttributesAccessMode OPTIONAL,
334 IN POBJECT_TYPE Type,
335 IN POBJECT_ATTRIBUTES ObjectAttributes OPTIONAL,
336 IN KPROCESSOR_MODE AccessMode,
337 IN OUT PVOID ParseContext OPTIONAL,
338 IN ULONG ObjectSize,
339 IN ULONG PagedPoolCharge OPTIONAL,
340 IN ULONG NonPagedPoolCharge OPTIONAL,
341 OUT PVOID *Object)
342 {
343 PVOID Parent = NULL;
344 UNICODE_STRING RemainingPath;
345 POBJECT_HEADER Header;
346 POBJECT_HEADER ParentHeader = NULL;
347 NTSTATUS Status;
348 BOOLEAN ObjectAttached = FALSE;
349 PWCHAR NamePtr;
350 PSECURITY_DESCRIPTOR NewSecurityDescriptor = NULL;
351 SECURITY_SUBJECT_CONTEXT SubjectContext;
352
353 assert_irql(APC_LEVEL);
354
355 DPRINT("ObCreateObject(Type %p ObjectAttributes %p, Object %p)\n",
356 Type, ObjectAttributes, Object);
357
358 if (Type == NULL)
359 {
360 DPRINT1("Invalid object type!\n");
361 return STATUS_INVALID_PARAMETER;
362 }
363
364 if (ObjectAttributes != NULL &&
365 ObjectAttributes->ObjectName != NULL &&
366 ObjectAttributes->ObjectName->Buffer != NULL)
367 {
368 Status = ObFindObject(ObjectAttributes,
369 &Parent,
370 &RemainingPath,
371 NULL);
372 if (!NT_SUCCESS(Status))
373 {
374 DPRINT("ObFindObject() failed! (Status 0x%x)\n", Status);
375 return(Status);
376 }
377 }
378 else
379 {
380 RtlInitUnicodeString(&RemainingPath, NULL);
381 }
382
383 Header = (POBJECT_HEADER)ExAllocatePoolWithTag(NonPagedPool,
384 OBJECT_ALLOC_SIZE(ObjectSize),
385 Type->Tag);
386 if (Header == NULL)
387 return STATUS_INSUFFICIENT_RESOURCES;
388 RtlZeroMemory(Header, OBJECT_ALLOC_SIZE(ObjectSize));
389
390 /* Initialize the object header */
391 Header->HandleCount = 0;
392 Header->RefCount = 1;
393 Header->ObjectType = Type;
394 if (ObjectAttributes != NULL &&
395 ObjectAttributes->Attributes & OBJ_PERMANENT)
396 {
397 Header->Permanent = TRUE;
398 }
399 else
400 {
401 Header->Permanent = FALSE;
402 }
403
404 if (ObjectAttributes != NULL &&
405 ObjectAttributes->Attributes & OBJ_INHERIT)
406 {
407 Header->Inherit = TRUE;
408 }
409 else
410 {
411 Header->Inherit = FALSE;
412 }
413
414 RtlInitUnicodeString(&(Header->Name),NULL);
415
416 if (Parent != NULL)
417 {
418 ParentHeader = BODY_TO_HEADER(Parent);
419 }
420
421 if (ParentHeader != NULL &&
422 ParentHeader->ObjectType == ObDirectoryType &&
423 RemainingPath.Buffer != NULL)
424 {
425 NamePtr = RemainingPath.Buffer;
426 if (*NamePtr == L'\\')
427 NamePtr++;
428
429 ObpAddEntryDirectory(Parent,
430 Header,
431 NamePtr);
432
433 ObjectAttached = TRUE;
434 }
435
436 if (Header->ObjectType->Create != NULL)
437 {
438 DPRINT("Calling %x\n", Header->ObjectType->Create);
439 Status = Header->ObjectType->Create(HEADER_TO_BODY(Header),
440 Parent,
441 RemainingPath.Buffer,
442 ObjectAttributes);
443 if (!NT_SUCCESS(Status))
444 {
445 if (ObjectAttached == TRUE)
446 {
447 ObpRemoveEntryDirectory(Header);
448 }
449 if (Parent)
450 {
451 ObDereferenceObject(Parent);
452 }
453 RtlFreeUnicodeString(&Header->Name);
454 RtlFreeUnicodeString(&RemainingPath);
455 ExFreePool(Header);
456 return(Status);
457 }
458 }
459 RtlFreeUnicodeString(&RemainingPath);
460
461 SeCaptureSubjectContext(&SubjectContext);
462
463 /* Build the new security descriptor */
464 Status = SeAssignSecurity((ParentHeader != NULL) ? ParentHeader->SecurityDescriptor : NULL,
465 (ObjectAttributes != NULL) ? ObjectAttributes->SecurityDescriptor : NULL,
466 &NewSecurityDescriptor,
467 (Header->ObjectType == ObDirectoryType),
468 &SubjectContext,
469 Header->ObjectType->Mapping,
470 PagedPool);
471 if (NT_SUCCESS(Status))
472 {
473 DPRINT("NewSecurityDescriptor %p\n", NewSecurityDescriptor);
474
475 if (Header->ObjectType->Security != NULL)
476 {
477 /* FIXME: Call the security method */
478 Status = STATUS_SUCCESS;
479 }
480 else
481 {
482 /* Assign the security descriptor to the object header */
483 Status = ObpAddSecurityDescriptor(NewSecurityDescriptor,
484 &Header->SecurityDescriptor);
485 DPRINT("Object security descriptor %p\n", Header->SecurityDescriptor);
486 }
487
488 /* Release the new security descriptor */
489 SeDeassignSecurity(&NewSecurityDescriptor);
490 }
491
492 SeReleaseSubjectContext(&SubjectContext);
493
494 if (Object != NULL)
495 {
496 *Object = HEADER_TO_BODY(Header);
497 }
498
499 return(STATUS_SUCCESS);
500 }
501
502
503 /*
504 * FUNCTION: Increments the pointer reference count for a given object
505 * ARGUMENTS:
506 * ObjectBody = Object's body
507 * DesiredAccess = Desired access to the object
508 * ObjectType = Points to the object type structure
509 * AccessMode = Type of access check to perform
510 * RETURNS: Status
511 *
512 * @implemented
513 */
514 NTSTATUS STDCALL
515 ObReferenceObjectByPointer(IN PVOID Object,
516 IN ACCESS_MASK DesiredAccess,
517 IN POBJECT_TYPE ObjectType,
518 IN KPROCESSOR_MODE AccessMode)
519 {
520 POBJECT_HEADER Header;
521
522 DPRINT("ObReferenceObjectByPointer(Object %x, ObjectType %x)\n",
523 Object,ObjectType);
524
525 Header = BODY_TO_HEADER(Object);
526
527 if (ObjectType != NULL && Header->ObjectType != ObjectType)
528 {
529 DPRINT("Failed %x (type was %x %S) should be %x %S\n",
530 Header,
531 Header->ObjectType,
532 Header->ObjectType->TypeName.Buffer,
533 ObjectType,
534 ObjectType->TypeName.Buffer);
535 return(STATUS_UNSUCCESSFUL);
536 }
537 if (Header->ObjectType == PsProcessType)
538 {
539 DPRINT("Ref p 0x%x refcount %d type %x ",
540 Object, Header->RefCount, PsProcessType);
541 DPRINT("eip %x\n", ((PULONG)&Object)[-1]);
542 }
543 if (Header->ObjectType == PsThreadType)
544 {
545 DPRINT("Deref t 0x%x with refcount %d type %x ",
546 Object, Header->RefCount, PsThreadType);
547 DPRINT("eip %x\n", ((PULONG)&Object)[-1]);
548 }
549
550 if (Header->CloseInProcess)
551 {
552 if (Header->ObjectType == PsProcessType)
553 {
554 return STATUS_PROCESS_IS_TERMINATING;
555 }
556 if (Header->ObjectType == PsThreadType)
557 {
558 return STATUS_THREAD_IS_TERMINATING;
559 }
560 return(STATUS_UNSUCCESSFUL);
561 }
562
563 InterlockedIncrement(&Header->RefCount);
564
565 return(STATUS_SUCCESS);
566 }
567
568
569 /*
570 * @implemented
571 */
572 NTSTATUS STDCALL
573 ObOpenObjectByPointer(IN POBJECT Object,
574 IN ULONG HandleAttributes,
575 IN PACCESS_STATE PassedAccessState,
576 IN ACCESS_MASK DesiredAccess,
577 IN POBJECT_TYPE ObjectType,
578 IN KPROCESSOR_MODE AccessMode,
579 OUT PHANDLE Handle)
580 {
581 NTSTATUS Status;
582
583 DPRINT("ObOpenObjectByPointer()\n");
584
585 Status = ObReferenceObjectByPointer(Object,
586 0,
587 ObjectType,
588 AccessMode);
589 if (!NT_SUCCESS(Status))
590 {
591 return Status;
592 }
593
594 Status = ObCreateHandle(PsGetCurrentProcess(),
595 Object,
596 DesiredAccess,
597 (BOOLEAN)(HandleAttributes & OBJ_INHERIT),
598 Handle);
599
600 ObDereferenceObject(Object);
601
602 return STATUS_SUCCESS;
603 }
604
605
606 static NTSTATUS
607 ObpDeleteObject(POBJECT_HEADER Header)
608 {
609 DPRINT("ObPerformRetentionChecks(Header %p)\n", Header);
610 if (KeGetCurrentIrql() != PASSIVE_LEVEL)
611 {
612 DPRINT("ObpPerformRetentionChecks called at an unsupported IRQL. Use ObpPerformRetentionChecksDpcLevel instead.\n");
613 KEBUGCHECK(0);
614 }
615
616 if (Header->SecurityDescriptor != NULL)
617 {
618 ObpRemoveSecurityDescriptor(Header->SecurityDescriptor);
619 }
620
621 if (Header->ObjectType != NULL &&
622 Header->ObjectType->Delete != NULL)
623 {
624 Header->ObjectType->Delete(HEADER_TO_BODY(Header));
625 }
626
627 if (Header->Name.Buffer != NULL)
628 {
629 ObpRemoveEntryDirectory(Header);
630 RtlFreeUnicodeString(&Header->Name);
631 }
632
633 DPRINT("ObPerformRetentionChecks() = Freeing object\n");
634 ExFreePool(Header);
635
636 return(STATUS_SUCCESS);
637 }
638
639
640 VOID STDCALL
641 ObpDeleteObjectWorkRoutine (IN PVOID Parameter)
642 {
643 PRETENTION_CHECK_PARAMS Params = (PRETENTION_CHECK_PARAMS)Parameter;
644 /* ULONG Tag; */ /* See below */
645
646 assert(Params);
647 assert(KeGetCurrentIrql() == PASSIVE_LEVEL); /* We need PAGED_CODE somewhere... */
648
649 /* Turn this on when we have ExFreePoolWithTag
650 Tag = Params->ObjectHeader->ObjectType->Tag; */
651 ObpDeleteObject(Params->ObjectHeader);
652 ExFreePool(Params);
653 /* ExFreePoolWithTag(Params, Tag); */
654 }
655
656
657 STATIC NTSTATUS
658 ObpDeleteObjectDpcLevel(IN POBJECT_HEADER ObjectHeader,
659 IN LONG OldRefCount)
660 {
661 if (ObjectHeader->RefCount < 0)
662 {
663 CPRINT("Object %p/%p has invalid reference count (%d)\n",
664 ObjectHeader, HEADER_TO_BODY(ObjectHeader),
665 ObjectHeader->RefCount);
666 KEBUGCHECK(0);
667 }
668
669 if (ObjectHeader->HandleCount < 0)
670 {
671 CPRINT("Object %p/%p has invalid handle count (%d)\n",
672 ObjectHeader, HEADER_TO_BODY(ObjectHeader),
673 ObjectHeader->HandleCount);
674 KEBUGCHECK(0);
675 }
676
677 if (ObjectHeader->CloseInProcess)
678 {
679 KEBUGCHECK(0);
680 return STATUS_UNSUCCESSFUL;
681 }
682 ObjectHeader->CloseInProcess = TRUE;
683
684 switch (KeGetCurrentIrql ())
685 {
686 case PASSIVE_LEVEL:
687 return ObpDeleteObject (ObjectHeader);
688
689 case APC_LEVEL:
690 case DISPATCH_LEVEL:
691 {
692 PRETENTION_CHECK_PARAMS Params;
693
694 /*
695 We use must succeed pool here because if the allocation fails
696 then we leak memory.
697 */
698 Params = (PRETENTION_CHECK_PARAMS)
699 ExAllocatePoolWithTag(NonPagedPoolMustSucceed,
700 sizeof(RETENTION_CHECK_PARAMS),
701 ObjectHeader->ObjectType->Tag);
702 Params->ObjectHeader = ObjectHeader;
703 ExInitializeWorkItem(&Params->WorkItem,
704 ObpDeleteObjectWorkRoutine,
705 (PVOID)Params);
706 ExQueueWorkItem(&Params->WorkItem,
707 CriticalWorkQueue);
708 }
709 return STATUS_PENDING;
710
711 default:
712 DPRINT("ObpPerformRetentionChecksDpcLevel called at unsupported "
713 "IRQL %u!\n", KeGetCurrentIrql());
714 KEBUGCHECK(0);
715 return STATUS_UNSUCCESSFUL;
716 }
717
718 return STATUS_SUCCESS;
719 }
720
721
722 /**********************************************************************
723 * NAME EXPORTED
724 * ObfReferenceObject@4
725 *
726 * DESCRIPTION
727 * Increments a given object's reference count and performs
728 * retention checks.
729 *
730 * ARGUMENTS
731 * ObjectBody = Body of the object.
732 *
733 * RETURN VALUE
734 * None.
735 *
736 * @implemented
737 */
738 VOID FASTCALL
739 ObfReferenceObject(IN PVOID Object)
740 {
741 POBJECT_HEADER Header;
742
743 assert(Object);
744
745 Header = BODY_TO_HEADER(Object);
746
747 /* No one should be referencing an object once we are deleting it. */
748 if (Header->CloseInProcess)
749 {
750 KEBUGCHECK(0);
751 }
752
753 (VOID)InterlockedIncrement(&Header->RefCount);
754 }
755
756
757 /**********************************************************************
758 * NAME EXPORTED
759 * ObfDereferenceObject@4
760 *
761 * DESCRIPTION
762 * Decrements a given object's reference count and performs
763 * retention checks.
764 *
765 * ARGUMENTS
766 * ObjectBody = Body of the object.
767 *
768 * RETURN VALUE
769 * None.
770 *
771 * @implemented
772 */
773 VOID FASTCALL
774 ObfDereferenceObject(IN PVOID Object)
775 {
776 POBJECT_HEADER Header;
777 LONG NewRefCount;
778 BOOL Permanent;
779 ULONG HandleCount;
780
781 assert(Object);
782
783 /* Extract the object header. */
784 Header = BODY_TO_HEADER(Object);
785 Permanent = Header->Permanent;
786 HandleCount = Header->HandleCount;
787
788 /*
789 Drop our reference and get the new count so we can tell if this was the
790 last reference.
791 */
792 NewRefCount = InterlockedDecrement(&Header->RefCount);
793 assert(NewRefCount >= 0);
794
795 /* Check whether the object can now be deleted. */
796 if (NewRefCount == 0 &&
797 HandleCount == 0 &&
798 !Permanent)
799 {
800 ObpDeleteObjectDpcLevel(Header, NewRefCount);
801 }
802 }
803
804
805 /**********************************************************************
806 * NAME EXPORTED
807 * ObGetObjectPointerCount@4
808 *
809 * DESCRIPTION
810 * Retrieves the pointer(reference) count of the given object.
811 *
812 * ARGUMENTS
813 * ObjectBody = Body of the object.
814 *
815 * RETURN VALUE
816 * Reference count.
817 *
818 * @implemented
819 */
820 ULONG STDCALL
821 ObGetObjectPointerCount(PVOID Object)
822 {
823 POBJECT_HEADER Header;
824
825 assert(Object);
826 Header = BODY_TO_HEADER(Object);
827
828 return(Header->RefCount);
829 }
830
831
832 /**********************************************************************
833 * NAME INTERNAL
834 * ObGetObjectHandleCount@4
835 *
836 * DESCRIPTION
837 * Retrieves the handle count of the given object.
838 *
839 * ARGUMENTS
840 * ObjectBody = Body of the object.
841 *
842 * RETURN VALUE
843 * Reference count.
844 */
845 ULONG
846 ObGetObjectHandleCount(PVOID Object)
847 {
848 POBJECT_HEADER Header;
849
850 assert(Object);
851 Header = BODY_TO_HEADER(Object);
852
853 return(Header->HandleCount);
854 }
855
856
857 /**********************************************************************
858 * NAME EXPORTED
859 * ObDereferenceObject@4
860 *
861 * DESCRIPTION
862 * Decrements a given object's reference count and performs
863 * retention checks.
864 *
865 * ARGUMENTS
866 * ObjectBody = Body of the object.
867 *
868 * RETURN VALUE
869 * None.
870 *
871 * @implemented
872 */
873
874 #ifdef ObDereferenceObject
875 #undef ObDereferenceObject
876 #endif
877
878 VOID STDCALL
879 ObDereferenceObject(IN PVOID Object)
880 {
881 ObfDereferenceObject(Object);
882 }
883
884 /* EOF */