2 * PROJECT: ReactOS Kernel
3 * LICENSE: GPL - See COPYING in the top level directory
4 * FILE: ntoskrnl/ob/obname.c
5 * PURPOSE: Manages all functions related to the Object Manager name-
6 * space, such as finding objects or querying their names.
7 * PROGRAMMERS: Alex Ionescu (alex.ionescu@reactos.org)
9 * Thomas Weidenmueller (w3seek@reactos.org)
12 /* INCLUDES ******************************************************************/
18 BOOLEAN ObpCaseInsensitive
= TRUE
;
19 POBJECT_DIRECTORY ObpRootDirectoryObject
;
20 POBJECT_DIRECTORY ObpTypeDirectoryObject
;
22 /* DOS Device Prefix \??\ and \?? */
23 ALIGNEDNAME ObpDosDevicesShortNamePrefix
= {{L
'\\',L
'?',L
'?',L
'\\'}};
24 ALIGNEDNAME ObpDosDevicesShortNameRoot
= {{L
'\\',L
'?',L
'?',L
'\0'}};
25 UNICODE_STRING ObpDosDevicesShortName
=
27 sizeof(ObpDosDevicesShortNamePrefix
),
28 sizeof(ObpDosDevicesShortNamePrefix
),
29 (PWSTR
)&ObpDosDevicesShortNamePrefix
32 /* PRIVATE FUNCTIONS *********************************************************/
36 ObpCreateDosDevicesDirectory(VOID
)
38 OBJECT_ATTRIBUTES ObjectAttributes
;
39 UNICODE_STRING Name
, LinkName
;
40 HANDLE Handle
, SymHandle
;
43 /* Create the '\??' directory */
44 RtlInitUnicodeString(&Name
, L
"\\??");
45 InitializeObjectAttributes(&ObjectAttributes
,
50 Status
= NtCreateDirectoryObject(&Handle
,
53 if (!NT_SUCCESS(Status
)) return FALSE
;
55 /* Initialize the GLOBALROOT path */
56 RtlInitUnicodeString(&LinkName
, L
"GLOBALROOT");
57 RtlInitUnicodeString(&Name
, L
"");
58 InitializeObjectAttributes(&ObjectAttributes
,
63 Status
= NtCreateSymbolicLinkObject(&SymHandle
,
64 SYMBOLIC_LINK_ALL_ACCESS
,
67 if (NT_SUCCESS(Status
)) NtClose(SymHandle
);
69 /* Link \??\Global to \?? */
70 RtlInitUnicodeString(&LinkName
, L
"Global");
71 RtlInitUnicodeString(&Name
, L
"\\??");
72 InitializeObjectAttributes(&ObjectAttributes
,
77 Status
= NtCreateSymbolicLinkObject(&SymHandle
,
78 SYMBOLIC_LINK_ALL_ACCESS
,
81 if (NT_SUCCESS(Status
)) NtClose(SymHandle
);
83 /* Close the directory handle */
85 if (!NT_SUCCESS(Status
)) return Status
;
87 /* Create link from '\DosDevices' to '\??' directory */
88 RtlCreateUnicodeString(&LinkName
, L
"\\DosDevices");
89 InitializeObjectAttributes(&ObjectAttributes
,
94 Status
= NtCreateSymbolicLinkObject(&SymHandle
,
95 SYMBOLIC_LINK_ALL_ACCESS
,
98 if (NT_SUCCESS(Status
)) NtClose(SymHandle
);
100 /* FIXME: Hack Hack! */
101 ObSystemDeviceMap
= ExAllocatePoolWithTag(NonPagedPool
,
102 sizeof(*ObSystemDeviceMap
),
103 TAG('O', 'b', 'D', 'm'));
104 RtlZeroMemory(ObSystemDeviceMap
, sizeof(*ObSystemDeviceMap
));
112 ObDereferenceDeviceMap(IN PEPROCESS Process
)
115 PDEVICE_MAP DeviceMap
= Process
->DeviceMap
;
117 /* FIXME: We don't use Process Devicemaps yet */
120 /* FIXME: Acquire the DeviceMap Spinlock */
121 // KeAcquireSpinLock(DeviceMap->Lock, &OldIrql);
123 /* Delete the device map link and dereference it */
124 Process
->DeviceMap
= NULL
;
125 if (--DeviceMap
->ReferenceCount
)
127 /* Nobody is referencing it anymore, unlink the DOS directory */
128 DeviceMap
->DosDevicesDirectory
->DeviceMap
= NULL
;
130 /* FIXME: Release the DeviceMap Spinlock */
131 // KeReleasepinLock(DeviceMap->Lock, OldIrql);
133 /* Dereference the DOS Devices Directory and free the Device Map */
134 ObDereferenceObject(DeviceMap
->DosDevicesDirectory
);
135 ExFreePool(DeviceMap
);
139 /* FIXME: Release the DeviceMap Spinlock */
140 // KeReleasepinLock(DeviceMap->Lock, OldIrql);
147 ObInheritDeviceMap(IN PEPROCESS Parent
,
148 IN PEPROCESS Process
)
150 /* FIXME: Devicemap Support */
154 * @name ObpDeleteNameCheck
156 * The ObpDeleteNameCheck routine checks if a named object should be
157 * removed from the object directory namespace.
160 * Pointer to the object to check for possible removal.
164 * @remarks An object is removed if the following 4 criteria are met:
165 * 1) The object has 0 handles open
166 * 2) The object is in the directory namespace and has a name
167 * 3) The object is not permanent
172 ObpDeleteNameCheck(IN PVOID Object
)
174 POBJECT_HEADER ObjectHeader
;
175 OBP_LOOKUP_CONTEXT Context
;
176 POBJECT_HEADER_NAME_INFO ObjectNameInfo
;
177 POBJECT_TYPE ObjectType
;
178 PVOID Directory
= NULL
;
180 /* Get object structures */
181 ObjectHeader
= OBJECT_TO_OBJECT_HEADER(Object
);
182 ObjectNameInfo
= ObpReferenceNameInfo(ObjectHeader
);
183 ObjectType
= ObjectHeader
->Type
;
186 * Check if the handle count is 0, if the object is named,
187 * and if the object isn't a permanent object.
189 if (!(ObjectHeader
->HandleCount
) &&
191 (ObjectNameInfo
->Name
.Length
) &&
192 (ObjectNameInfo
->Directory
) &&
193 !(ObjectHeader
->Flags
& OB_FLAG_PERMANENT
))
195 /* Setup a lookup context */
196 ObpInitializeLookupContext(&Context
);
198 /* Lock the directory */
199 ObpAcquireDirectoryLockExclusive(ObjectNameInfo
->Directory
, &Context
);
202 Object
= ObpLookupEntryDirectory(ObjectNameInfo
->Directory
,
203 &ObjectNameInfo
->Name
,
209 /* Lock the object */
210 ObpAcquireObjectLock(ObjectHeader
);
212 /* Make sure we can still delete the object */
213 if (!(ObjectHeader
->HandleCount
) &&
214 !(ObjectHeader
->Flags
& OB_FLAG_PERMANENT
))
216 /* First delete it from the directory */
217 ObpDeleteEntryDirectory(&Context
);
219 /* Check if this is a symbolic link */
220 if (ObjectType
== ObSymbolicLinkType
)
222 /* Remove internal name */
223 ObpDeleteSymbolicLinkName(Object
);
226 /* Check if the magic protection flag is set */
227 ObjectNameInfo
= OBJECT_HEADER_TO_NAME_INFO(ObjectHeader
);
228 if ((ObjectNameInfo
) &&
229 (ObjectNameInfo
->QueryReferences
& 0x40000000))
231 /* Remove protection flag */
232 InterlockedExchangeAdd((PLONG
)&ObjectNameInfo
->QueryReferences
,
236 /* Get the directory */
237 Directory
= ObjectNameInfo
->Directory
;
240 /* Release the lock */
241 ObpReleaseObjectLock(ObjectHeader
);
244 /* Cleanup after lookup */
245 ObpReleaseLookupContext(&Context
);
247 /* Remove another query reference since we added one on top */
248 ObpDereferenceNameInfo(ObjectNameInfo
);
250 /* Check if we were inserted in a directory */
253 /* We were, so first remove the extra reference we had added */
254 ObpDereferenceNameInfo(ObjectNameInfo
);
256 /* Now dereference the object as well */
257 ObDereferenceObject(Object
);
262 /* Remove the reference we added */
263 ObpDereferenceNameInfo(ObjectNameInfo
);
269 ObpLookupObjectName(IN HANDLE RootHandle OPTIONAL
,
270 IN PUNICODE_STRING ObjectName
,
272 IN POBJECT_TYPE ObjectType
,
273 IN KPROCESSOR_MODE AccessMode
,
274 IN OUT PVOID ParseContext
,
275 IN PSECURITY_QUALITY_OF_SERVICE SecurityQos OPTIONAL
,
276 IN PVOID InsertObject OPTIONAL
,
277 IN OUT PACCESS_STATE AccessState
,
278 OUT POBP_LOOKUP_CONTEXT LookupContext
,
279 OUT PVOID
*FoundObject
)
282 POBJECT_HEADER ObjectHeader
;
283 UNICODE_STRING ComponentName
, RemainingName
;
284 BOOLEAN Reparse
= FALSE
, SymLink
= FALSE
;
285 POBJECT_DIRECTORY Directory
= NULL
, ParentDirectory
= NULL
, RootDirectory
;
286 POBJECT_DIRECTORY ReferencedDirectory
= NULL
, ReferencedParentDirectory
= NULL
;
288 OB_PARSE_METHOD ParseRoutine
;
290 KPROCESSOR_MODE AccessCheckMode
;
292 POBJECT_HEADER_NAME_INFO ObjectNameInfo
;
293 ULONG MaxReparse
= 30;
295 OBTRACE(OB_NAMESPACE_DEBUG
,
296 "%s - Finding Object: %wZ. Expecting: %p\n",
301 /* Initialize starting state */
302 ObpInitializeLookupContext(LookupContext
);
304 Status
= STATUS_SUCCESS
;
307 /* Check if case-insensitivity is checked */
308 if (ObpCaseInsensitive
)
310 /* Check if the object type requests this */
311 if (!(ObjectType
) || (ObjectType
->TypeInfo
.CaseInsensitive
))
313 /* Add the flag to disable case sensitivity */
314 Attributes
|= OBJ_CASE_INSENSITIVE
;
318 /* Check if this is a access checks are being forced */
319 AccessCheckMode
= (Attributes
& OBJ_FORCE_ACCESS_CHECK
) ?
320 UserMode
: AccessMode
;
322 /* Check if we got a Root Directory */
325 /* We did. Reference it */
326 Status
= ObReferenceObjectByHandle(RootHandle
,
330 (PVOID
*)&RootDirectory
,
332 if (!NT_SUCCESS(Status
)) return Status
;
335 ObjectHeader
= OBJECT_TO_OBJECT_HEADER(RootDirectory
);
337 /* The name cannot start with a separator, unless this is a file */
338 if ((ObjectName
->Buffer
) &&
339 (ObjectName
->Buffer
[0] == OBJ_NAME_PATH_SEPARATOR
) &&
340 (ObjectHeader
->Type
!= IoFileObjectType
))
342 /* The syntax is bad, so fail this request */
343 ObDereferenceObject(RootDirectory
);
344 return STATUS_OBJECT_PATH_SYNTAX_BAD
;
347 /* Don't parse a Directory */
348 if (ObjectHeader
->Type
!= ObDirectoryType
)
350 /* Make sure the Object Type has a parse routine */
351 ParseRoutine
= ObjectHeader
->Type
->TypeInfo
.ParseProcedure
;
354 /* We can't parse a name if we don't have a parse routine */
355 ObDereferenceObject(RootDirectory
);
356 return STATUS_INVALID_HANDLE
;
359 /* Set default parse count */
365 /* Start with the full name */
366 RemainingName
= *ObjectName
;
368 /* Call the Parse Procedure */
369 ObpCalloutStart(&CalloutIrql
);
370 Status
= ParseRoutine(RootDirectory
,
380 ObpCalloutEnd(CalloutIrql
, "Parse", ObjectHeader
->Type
, Object
);
382 /* Check for success or failure, so not reparse */
383 if ((Status
!= STATUS_REPARSE
) &&
384 (Status
!= STATUS_REPARSE_OBJECT
))
386 /* Check for failure */
387 if (!NT_SUCCESS(Status
))
389 /* Parse routine might not have cleared this, do it */
394 /* Modify status to reflect failure inside Ob */
395 Status
= STATUS_OBJECT_NAME_NOT_FOUND
;
398 /* We're done, return the status and object */
399 *FoundObject
= Object
;
400 ObDereferenceObject(RootDirectory
);
403 else if ((!ObjectName
->Length
) ||
404 (!ObjectName
->Buffer
) ||
405 (ObjectName
->Buffer
[0] == OBJ_NAME_PATH_SEPARATOR
))
407 /* Reparsed to the root directory, so start over */
408 ObDereferenceObject(RootDirectory
);
409 RootDirectory
= ObpRootDirectoryObject
;
411 /* Don't use this anymore, since we're starting at root */
415 else if (--MaxReparse
)
417 /* Try reparsing again */
422 /* Reparsed too many times */
423 ObDereferenceObject(RootDirectory
);
425 /* Return the object and normalized status */
426 *FoundObject
= Object
;
427 if (!Object
) Status
= STATUS_OBJECT_NAME_NOT_FOUND
;
432 else if (!(ObjectName
->Length
) || !(ObjectName
->Buffer
))
434 /* Just return the Root Directory if we didn't get a name*/
435 Status
= ObReferenceObjectByPointer(RootDirectory
,
439 if (NT_SUCCESS(Status
)) Object
= RootDirectory
;
441 /* Remove the first reference we added and return the object */
442 ObDereferenceObject(RootDirectory
);
443 *FoundObject
= Object
;
449 /* We did not get a Root Directory, so use the root */
450 RootDirectory
= ObpRootDirectoryObject
;
452 /* It must start with a path separator */
453 if (!(ObjectName
->Length
) ||
454 !(ObjectName
->Buffer
) ||
455 (ObjectName
->Buffer
[0] != OBJ_NAME_PATH_SEPARATOR
))
457 /* This name is invalid, so fail */
458 return STATUS_OBJECT_PATH_SYNTAX_BAD
;
461 /* Check if the name is only the path separator */
462 if (ObjectName
->Length
== sizeof(OBJ_NAME_PATH_SEPARATOR
))
464 /* So the caller only wants the root directory; do we have one? */
467 /* This must be the first time we're creating it... right? */
470 /* Yes, so return it to ObInsert so that it can create it */
471 Status
= ObReferenceObjectByPointer(InsertObject
,
475 if (NT_SUCCESS(Status
)) *FoundObject
= InsertObject
;
480 /* This should never really happen */
482 return STATUS_INVALID_PARAMETER
;
487 /* We do have the root directory, so just return it */
488 Status
= ObReferenceObjectByPointer(RootDirectory
,
492 if (NT_SUCCESS(Status
)) *FoundObject
= RootDirectory
;
499 /* FIXME: Check if we have a device map */
501 /* Check if this is a possible DOS name */
502 if (!((ULONG_PTR
)(ObjectName
->Buffer
) & 7))
505 * This could be one. Does it match the prefix?
506 * Note that as an optimization, the match is done as 64-bit
507 * compare since the prefix is "\??\" which is exactly 8 bytes.
509 * In the second branch, we test for "\??" which is also valid.
510 * This time, we use a 32-bit compare followed by a Unicode
511 * character compare (16-bit), since the sum is 6 bytes.
513 if ((ObjectName
->Length
>= ObpDosDevicesShortName
.Length
) &&
514 (*(PULONGLONG
)(ObjectName
->Buffer
) ==
515 ObpDosDevicesShortNamePrefix
.Alignment
.QuadPart
))
519 else if ((ObjectName
->Length
== ObpDosDevicesShortName
.Length
-
521 (*(PULONG
)(ObjectName
->Buffer
) ==
522 ObpDosDevicesShortNameRoot
.Alignment
.LowPart
) &&
523 (*((PWCHAR
)(ObjectName
->Buffer
) + 2) ==
524 (WCHAR
)(ObpDosDevicesShortNameRoot
.Alignment
.HighPart
)))
532 /* Check if we were reparsing a symbolic link */
544 RemainingName
= *ObjectName
;
546 /* Disable reparsing again */
549 /* Start parse loop */
555 /* Check if the name starts with a path separator */
556 if ((RemainingName
.Length
) &&
557 (RemainingName
.Buffer
[0] == OBJ_NAME_PATH_SEPARATOR
))
559 /* Skip the path separator */
560 RemainingName
.Buffer
++;
561 RemainingName
.Length
-= sizeof(OBJ_NAME_PATH_SEPARATOR
);
564 /* Find the next Part Name */
565 ComponentName
= RemainingName
;
566 while (RemainingName
.Length
)
568 /* Break if we found the \ ending */
569 if (RemainingName
.Buffer
[0] == OBJ_NAME_PATH_SEPARATOR
) break;
572 RemainingName
.Buffer
++;
573 RemainingName
.Length
-= sizeof(OBJ_NAME_PATH_SEPARATOR
);
576 /* Get its size and make sure it's valid */
577 ComponentName
.Length
-= RemainingName
.Length
;
578 if (!ComponentName
.Length
)
580 /* Invalid size, fail */
581 Status
= STATUS_OBJECT_NAME_INVALID
;
585 /* Check if we're in the root */
586 if (!Directory
) Directory
= RootDirectory
;
588 /* Check if this is a user-mode call that needs to traverse */
589 if ((AccessCheckMode
!= KernelMode
) &&
590 !(AccessState
->Flags
& TOKEN_HAS_TRAVERSE_PRIVILEGE
))
592 /* We shouldn't have referenced a directory yet */
593 ASSERT(ReferencedDirectory
== NULL
);
595 /* Reference the directory */
596 ObReferenceObject(Directory
);
597 ReferencedDirectory
= Directory
;
599 /* Check if we have a parent directory */
602 /* Check for traverse access */
603 if (!ObpCheckTraverseAccess(ParentDirectory
,
610 /* We don't have it, fail */
616 /* Check if we don't have a remaining name yet */
617 if (!RemainingName
.Length
)
619 /* Check if we don't have a referenced directory yet */
620 if (!ReferencedDirectory
)
623 ObReferenceObject(Directory
);
624 ReferencedDirectory
= Directory
;
627 /* Check if we are inserting an object */
630 /* Lock the directory */
631 ObpAcquireDirectoryLockExclusive(Directory
, LookupContext
);
636 Object
= ObpLookupEntryDirectory(Directory
,
639 InsertObject
? FALSE
: TRUE
,
643 /* We didn't find it... do we still have a path? */
644 if (RemainingName
.Length
)
646 /* Then tell the caller the path wasn't found */
647 Status
= STATUS_OBJECT_PATH_NOT_FOUND
;
650 else if (!InsertObject
)
652 /* Otherwise, we have a path, but the name isn't valid */
653 Status
= STATUS_OBJECT_NAME_NOT_FOUND
;
657 /* Check create access for the object */
658 if (!ObCheckCreateObjectAccess(Directory
,
659 ObjectType
== ObDirectoryType
?
660 DIRECTORY_CREATE_SUBDIRECTORY
:
661 DIRECTORY_CREATE_OBJECT
,
668 /* We don't have create access, fail */
672 /* Get the object header */
673 ObjectHeader
= OBJECT_TO_OBJECT_HEADER(InsertObject
);
675 /* FIXME: Check if this is a Section Object or Sym Link */
676 /* FIXME: If it is, then check if this isn't session 0 */
677 /* FIXME: If it isn't, check for SeCreateGlobalPrivilege */
678 /* FIXME: If privilege isn't there, check for unsecure name */
679 /* FIXME: If it isn't a known unsecure name, then fail */
681 /* Create Object Name */
682 NewName
= ExAllocatePoolWithTag(PagedPool
,
683 ComponentName
.Length
,
686 !(ObpInsertEntryDirectory(Directory
,
690 /* Either couldn't allocate the name, or insert failed */
691 if (NewName
) ExFreePool(NewName
);
693 /* Fail due to memory reasons */
694 Status
= STATUS_INSUFFICIENT_RESOURCES
;
698 /* Reference newly to be inserted object */
699 ObReferenceObject(InsertObject
);
701 /* Get the name information */
702 ObjectNameInfo
= OBJECT_HEADER_TO_NAME_INFO(ObjectHeader
);
704 /* Reference the directory */
705 ObReferenceObject(Directory
);
708 RtlCopyMemory(NewName
,
709 ComponentName
.Buffer
,
710 ComponentName
.Length
);
712 /* Check if we had an old name */
713 if (ObjectNameInfo
->Name
.Buffer
)
716 ExFreePoolWithTag(ObjectNameInfo
->Name
.Buffer
, OB_NAME_TAG
);
720 ObjectNameInfo
->Name
.Buffer
= NewName
;
721 ObjectNameInfo
->Name
.Length
= ComponentName
.Length
;
722 ObjectNameInfo
->Name
.MaximumLength
= ComponentName
.Length
;
724 /* Return Status and the Expected Object */
725 Status
= STATUS_SUCCESS
;
726 Object
= InsertObject
;
728 /* Get out of here */
733 /* We found it, so now get its header */
734 ObjectHeader
= OBJECT_TO_OBJECT_HEADER(Object
);
737 * Check for a parse Procedure, but don't bother to parse for an insert
738 * unless it's a Symbolic Link, in which case we MUST parse
740 ParseRoutine
= ObjectHeader
->Type
->TypeInfo
.ParseProcedure
;
741 if ((ParseRoutine
) &&
742 (!(InsertObject
) || (ParseRoutine
== ObpParseSymbolicLink
)))
744 /* Use the Root Directory next time */
747 /* Increment the pointer count */
748 InterlockedExchangeAdd(&ObjectHeader
->PointerCount
, 1);
750 /* Cleanup from the first lookup */
751 ObpReleaseLookupContext(LookupContext
);
753 /* Check if we have a referenced directory */
754 if (ReferencedDirectory
)
756 /* We do, dereference it */
757 ObDereferenceObject(ReferencedDirectory
);
758 ReferencedDirectory
= NULL
;
761 /* Check if we have a referenced parent directory */
762 if (ReferencedParentDirectory
)
764 /* We do, dereference it */
765 ObDereferenceObject(ReferencedParentDirectory
);
766 ReferencedParentDirectory
= NULL
;
769 /* Call the Parse Procedure */
770 ObpCalloutStart(&CalloutIrql
);
771 Status
= ParseRoutine(Object
,
781 ObpCalloutEnd(CalloutIrql
, "Parse", ObjectHeader
->Type
, Object
);
783 /* Remove our extra reference */
784 ObDereferenceObject(&ObjectHeader
->Body
);
786 /* Check if we have to reparse */
787 if ((Status
== STATUS_REPARSE
) ||
788 (Status
== STATUS_REPARSE_OBJECT
))
793 /* Start over from root if we got sent back there */
794 if ((Status
== STATUS_REPARSE_OBJECT
) ||
795 (ObjectName
->Buffer
[0] == OBJ_NAME_PATH_SEPARATOR
))
797 /* Check if we got a root directory */
800 /* Stop using it, because we have a new directory now */
801 ObDereferenceObject(RootDirectory
);
806 ParentDirectory
= NULL
;
807 RootDirectory
= ObpRootDirectoryObject
;
809 /* Check for reparse status */
810 if (Status
== STATUS_REPARSE_OBJECT
)
812 /* Don't reparse again */
815 /* Did we actually get an object to which to reparse? */
818 /* We didn't, so set a failure status */
819 Status
= STATUS_OBJECT_NAME_NOT_FOUND
;
823 /* We did, so we're free to parse the new object */
829 /* This is a symbolic link */
834 else if (RootDirectory
== ObpRootDirectoryObject
)
836 /* We got STATUS_REPARSE but are at the Root Directory */
838 Status
= STATUS_OBJECT_NAME_NOT_FOUND
;
842 else if (!NT_SUCCESS(Status
))
849 /* We didn't reparse but we didn't find the Object Either */
850 Status
= STATUS_OBJECT_NAME_NOT_FOUND
;
853 /* Break out of the loop */
858 /* No parse routine...do we still have a remaining name? */
859 if (!RemainingName
.Length
)
861 /* Are we creating an object? */
864 /* Check if this is a user-mode call that needs to traverse */
865 if ((AccessCheckMode
!= KernelMode
) &&
866 !(AccessState
->Flags
& TOKEN_HAS_TRAVERSE_PRIVILEGE
))
868 /* Check if we can get it */
869 if (!ObpCheckTraverseAccess(Directory
,
876 /* We don't have access, fail */
882 /* Reference the Object */
883 Status
= ObReferenceObjectByPointer(Object
,
887 if (!NT_SUCCESS(Status
)) Object
= NULL
;
890 /* And get out of the reparse loop */
895 /* We still have a name; check if this is a directory object */
896 if (ObjectHeader
->Type
== ObDirectoryType
)
898 /* Check if we have a referenced parent directory */
899 if (ReferencedParentDirectory
)
902 ObDereferenceObject(ReferencedParentDirectory
);
905 /* Restart the lookup from this directory */
906 ReferencedParentDirectory
= ReferencedDirectory
;
907 ParentDirectory
= Directory
;
909 ReferencedDirectory
= NULL
;
913 /* We still have a name, but no parse routine for it */
914 Status
= STATUS_OBJECT_TYPE_MISMATCH
;
923 /* Check if we failed */
924 if (!NT_SUCCESS(Status
))
926 /* Cleanup after lookup */
927 ObpReleaseLookupContext(LookupContext
);
930 /* Check if we have a device map and dereference it if so */
931 //if (DeviceMap) ObfDereferenceDeviceMap(DeviceMap);
933 /* Check if we have a referenced directory and dereference it if so */
934 if (ReferencedDirectory
) ObDereferenceObject(ReferencedDirectory
);
936 /* Check if we have a referenced parent directory */
937 if (ReferencedParentDirectory
)
939 /* We do, dereference it */
940 ObDereferenceObject(ReferencedParentDirectory
);
943 /* Set the found object and check if we got one */
944 *FoundObject
= Object
;
947 /* Nothing was found. Did we reparse or get success? */
948 if ((Status
== STATUS_REPARSE
) || (NT_SUCCESS(Status
)))
950 /* Set correct failure */
951 Status
= STATUS_OBJECT_NAME_NOT_FOUND
;
955 /* Check if we had a root directory */
956 if (RootHandle
) ObDereferenceObject(RootDirectory
);
958 /* Return status to caller */
959 OBTRACE(OB_NAMESPACE_DEBUG
,
960 "%s - Found Object: %p. Expected: %p\n",
967 /* PUBLIC FUNCTIONS *********************************************************/
971 ObQueryNameString(IN PVOID Object
,
972 OUT POBJECT_NAME_INFORMATION ObjectNameInfo
,
974 OUT PULONG ReturnLength
)
976 POBJECT_HEADER_NAME_INFO LocalInfo
;
977 POBJECT_HEADER ObjectHeader
;
978 POBJECT_DIRECTORY ParentDirectory
;
981 BOOLEAN ObjectIsNamed
;
983 /* Get the Kernel Meta-Structures */
984 ObjectHeader
= OBJECT_TO_OBJECT_HEADER(Object
);
985 LocalInfo
= OBJECT_HEADER_TO_NAME_INFO(ObjectHeader
);
987 /* Check if a Query Name Procedure is available */
988 if (ObjectHeader
->Type
->TypeInfo
.QueryNameProcedure
)
990 /* Call the procedure */
991 ObjectIsNamed
= ((LocalInfo
) && (LocalInfo
->Name
.Length
> 0));
992 return ObjectHeader
->Type
->TypeInfo
.QueryNameProcedure(Object
,
1000 /* Check if the object doesn't even have a name */
1001 if (!(LocalInfo
) || !(LocalInfo
->Name
.Buffer
))
1003 /* We're returning the name structure */
1004 *ReturnLength
= sizeof(OBJECT_NAME_INFORMATION
);
1006 /* Check if we were given enough space */
1007 if (*ReturnLength
> Length
) return STATUS_INFO_LENGTH_MISMATCH
;
1009 /* Return an empty buffer */
1010 RtlInitEmptyUnicodeString(&ObjectNameInfo
->Name
, NULL
, 0);
1011 return STATUS_SUCCESS
;
1015 * Find the size needed for the name. We won't do
1016 * this during the Name Creation loop because we want
1017 * to let the caller know that the buffer isn't big
1018 * enough right at the beginning, not work our way through
1019 * and find out at the end
1021 if (Object
== ObpRootDirectoryObject
)
1023 /* Size of the '\' string */
1024 NameSize
= sizeof(OBJ_NAME_PATH_SEPARATOR
);
1028 /* Get the Object Directory and add name of Object */
1029 ParentDirectory
= LocalInfo
->Directory
;
1030 NameSize
= sizeof(OBJ_NAME_PATH_SEPARATOR
) + LocalInfo
->Name
.Length
;
1032 /* Loop inside the directory to get the top-most one (meaning root) */
1033 while ((ParentDirectory
!= ObpRootDirectoryObject
) && (ParentDirectory
))
1035 /* Get the Name Information */
1036 LocalInfo
= OBJECT_HEADER_TO_NAME_INFO(
1037 OBJECT_TO_OBJECT_HEADER(ParentDirectory
));
1039 /* Add the size of the Directory Name */
1040 if (LocalInfo
&& LocalInfo
->Directory
)
1042 /* Size of the '\' string + Directory Name */
1043 NameSize
+= sizeof(OBJ_NAME_PATH_SEPARATOR
) +
1044 LocalInfo
->Name
.Length
;
1046 /* Move to next parent Directory */
1047 ParentDirectory
= LocalInfo
->Directory
;
1051 /* Directory with no name. We append "...\" */
1052 NameSize
+= sizeof(L
"...") + sizeof(OBJ_NAME_PATH_SEPARATOR
);
1058 /* Finally, add the name of the structure and the null char */
1059 *ReturnLength
= NameSize
+
1060 sizeof(OBJECT_NAME_INFORMATION
) +
1061 sizeof(UNICODE_NULL
);
1063 /* Check if we were given enough space */
1064 if (*ReturnLength
> Length
) return STATUS_INFO_LENGTH_MISMATCH
;
1067 * Now we will actually create the name. We work backwards because
1068 * it's easier to start off from the Name we have and walk up the
1069 * parent directories. We use the same logic as Name Length calculation.
1071 LocalInfo
= OBJECT_HEADER_TO_NAME_INFO(ObjectHeader
);
1072 ObjectName
= (PWCH
)((ULONG_PTR
)ObjectNameInfo
+ *ReturnLength
);
1073 *--ObjectName
= UNICODE_NULL
;
1075 /* Check if the object is actually the Root directory */
1076 if (Object
== ObpRootDirectoryObject
)
1078 /* This is already the Root Directory, return "\\" */
1079 *--ObjectName
= OBJ_NAME_PATH_SEPARATOR
;
1080 ObjectNameInfo
->Name
.Length
= (USHORT
)NameSize
;
1081 ObjectNameInfo
->Name
.MaximumLength
= (USHORT
)(NameSize
+
1082 sizeof(UNICODE_NULL
));
1083 ObjectNameInfo
->Name
.Buffer
= ObjectName
;
1084 return STATUS_SUCCESS
;
1088 /* Start by adding the Object's Name */
1089 ObjectName
= (PWCH
)((ULONG_PTR
)ObjectName
-
1090 LocalInfo
->Name
.Length
);
1091 RtlCopyMemory(ObjectName
,
1092 LocalInfo
->Name
.Buffer
,
1093 LocalInfo
->Name
.Length
);
1095 /* Now parse the Parent directories until we reach the top */
1096 ParentDirectory
= LocalInfo
->Directory
;
1097 while ((ParentDirectory
!= ObpRootDirectoryObject
) && (ParentDirectory
))
1099 /* Get the name information */
1100 LocalInfo
= OBJECT_HEADER_TO_NAME_INFO(
1101 OBJECT_TO_OBJECT_HEADER(ParentDirectory
));
1104 *(--ObjectName
) = OBJ_NAME_PATH_SEPARATOR
;
1106 /* Add the Parent Directory's Name */
1107 if (LocalInfo
&& LocalInfo
->Name
.Buffer
)
1110 ObjectName
= (PWCH
)((ULONG_PTR
)ObjectName
-
1111 LocalInfo
->Name
.Length
);
1112 RtlCopyMemory(ObjectName
,
1113 LocalInfo
->Name
.Buffer
,
1114 LocalInfo
->Name
.Length
);
1116 /* Move to next parent */
1117 ParentDirectory
= LocalInfo
->Directory
;
1121 /* Directory without a name, we add "..." */
1122 ObjectName
-= sizeof(L
"...");
1123 ObjectName
= L
"...";
1128 /* Add Root Directory Name */
1129 *(--ObjectName
) = OBJ_NAME_PATH_SEPARATOR
;
1130 ObjectNameInfo
->Name
.Length
= (USHORT
)NameSize
;
1131 ObjectNameInfo
->Name
.MaximumLength
= (USHORT
)(NameSize
+
1132 sizeof(UNICODE_NULL
));
1133 ObjectNameInfo
->Name
.Buffer
= ObjectName
;
1136 /* Return success */
1137 return STATUS_SUCCESS
;
1142 ObQueryDeviceMapInformation(IN PEPROCESS Process
,
1143 IN PPROCESS_DEVICEMAP_INFORMATION DeviceMapInfo
)
1148 * FIXME: This is an ugly hack for now, to always return the System Device Map
1149 * instead of returning the Process Device Map. Not important yet since we don't use it
1152 /* FIXME: Acquire the DeviceMap Spinlock */
1153 // KeAcquireSpinLock(DeviceMap->Lock, &OldIrql);
1156 DeviceMapInfo
->Query
.DriveMap
= ObSystemDeviceMap
->DriveMap
;
1157 RtlCopyMemory(DeviceMapInfo
->Query
.DriveType
,
1158 ObSystemDeviceMap
->DriveType
,
1159 sizeof(ObSystemDeviceMap
->DriveType
));
1161 /* FIXME: Release the DeviceMap Spinlock */
1162 // KeReleasepinLock(DeviceMap->Lock, OldIrql);