3 * COPYRIGHT: See COPYING in the top level directory
4 * PROJECT: ReactOS kernel
5 * FILE: ntoskrnl/ob/namespc.c
6 * PURPOSE: Manages the system namespace
8 * PROGRAMMERS: David Welch (welch@mcmail.com)
11 /* INCLUDES ***************************************************************/
15 #include <internal/debug.h>
17 #if defined (ALLOC_PRAGMA)
18 #pragma alloc_text(INIT, ObInit)
22 extern ULONG NtGlobalFlag
;
24 /* GLOBALS ****************************************************************/
26 POBJECT_TYPE ObDirectoryType
= NULL
;
27 POBJECT_TYPE ObTypeObjectType
= NULL
;
29 PDIRECTORY_OBJECT NameSpaceRoot
= NULL
;
30 PDIRECTORY_OBJECT ObpTypeDirectoryObject
= NULL
;
31 /* FIXME: Move this somewhere else once devicemap support is in */
32 PDEVICE_MAP ObSystemDeviceMap
= NULL
;
33 KEVENT ObpDefaultObject
;
35 static GENERIC_MAPPING ObpDirectoryMapping
= {
36 STANDARD_RIGHTS_READ
|DIRECTORY_QUERY
|DIRECTORY_TRAVERSE
,
37 STANDARD_RIGHTS_WRITE
|DIRECTORY_CREATE_OBJECT
|DIRECTORY_CREATE_SUBDIRECTORY
,
38 STANDARD_RIGHTS_EXECUTE
|DIRECTORY_QUERY
|DIRECTORY_TRAVERSE
,
39 DIRECTORY_ALL_ACCESS
};
41 static GENERIC_MAPPING ObpTypeMapping
= {
43 STANDARD_RIGHTS_WRITE
,
44 STANDARD_RIGHTS_EXECUTE
,
49 ObpAllocateObject(POBJECT_CREATE_INFORMATION ObjectCreateInfo
,
50 PUNICODE_STRING ObjectName
,
51 POBJECT_TYPE ObjectType
,
53 POBJECT_HEADER
*ObjectHeader
);
55 /* FUNCTIONS **************************************************************/
61 ObReferenceObjectByName(PUNICODE_STRING ObjectPath
,
63 PACCESS_STATE PassedAccessState
,
64 ACCESS_MASK DesiredAccess
,
65 POBJECT_TYPE ObjectType
,
66 KPROCESSOR_MODE AccessMode
,
71 UNICODE_STRING RemainingPath
;
72 UNICODE_STRING ObjectName
;
73 OBJECT_CREATE_INFORMATION ObjectCreateInfo
;
78 /* Capture the name */
79 DPRINT("Capturing Name\n");
80 Status
= ObpCaptureObjectName(&ObjectName
, ObjectPath
, AccessMode
);
81 if (!NT_SUCCESS(Status
))
83 DPRINT("ObpCaptureObjectName() failed (Status %lx)\n", Status
);
88 * Create a fake ObjectCreateInfo structure. Note that my upcoming
89 * ObFindObject refactoring will remove the need for this hack.
91 ObjectCreateInfo
.RootDirectory
= NULL
;
92 ObjectCreateInfo
.Attributes
= Attributes
;
94 Status
= ObFindObject(&ObjectCreateInfo
,
100 if (ObjectName
.Buffer
) ExFreePool(ObjectName
.Buffer
);
102 if (!NT_SUCCESS(Status
))
106 DPRINT("RemainingPath.Buffer '%S' Object %p\n", RemainingPath
.Buffer
, Object
);
108 if (RemainingPath
.Buffer
!= NULL
|| Object
== NULL
)
110 DPRINT("Object %p\n", Object
);
112 RtlFreeUnicodeString (&RemainingPath
);
113 return(STATUS_OBJECT_NAME_NOT_FOUND
);
116 RtlFreeUnicodeString (&RemainingPath
);
117 return(STATUS_SUCCESS
);
121 /**********************************************************************
126 * Obtain a handle to an existing object.
150 ObOpenObjectByName(IN POBJECT_ATTRIBUTES ObjectAttributes
,
151 IN POBJECT_TYPE ObjectType
,
152 IN OUT PVOID ParseContext
,
153 IN KPROCESSOR_MODE AccessMode
,
154 IN ACCESS_MASK DesiredAccess
,
155 IN PACCESS_STATE PassedAccessState
,
158 UNICODE_STRING RemainingPath
;
160 UNICODE_STRING ObjectName
;
161 OBJECT_CREATE_INFORMATION ObjectCreateInfo
;
166 DPRINT("ObOpenObjectByName(...)\n");
168 /* Capture all the info */
169 DPRINT("Capturing Create Info\n");
170 Status
= ObpCaptureObjectAttributes(ObjectAttributes
,
175 if (!NT_SUCCESS(Status
))
177 DPRINT("ObpCaptureObjectAttributes() failed (Status %lx)\n", Status
);
181 Status
= ObFindObject(&ObjectCreateInfo
,
186 if (ObjectName
.Buffer
) ExFreePool(ObjectName
.Buffer
);
187 if (!NT_SUCCESS(Status
))
189 DPRINT("ObFindObject() failed (Status %lx)\n", Status
);
193 DPRINT("OBject: %p, Remaining Path: %wZ\n", Object
, &RemainingPath
);
196 Status
= STATUS_UNSUCCESSFUL
;
199 if (RemainingPath
.Buffer
!= NULL
)
201 if (wcschr(RemainingPath
.Buffer
+ 1, L
'\\') == NULL
)
202 Status
= STATUS_OBJECT_NAME_NOT_FOUND
;
204 Status
=STATUS_OBJECT_PATH_NOT_FOUND
;
208 Status
= ObpCreateHandle(Object
,
210 ObjectCreateInfo
.Attributes
,
216 ObDereferenceObject(Object
);
218 RtlFreeUnicodeString(&RemainingPath
);
219 ObpReleaseCapturedAttributes(&ObjectCreateInfo
);
226 ObQueryDeviceMapInformation(PEPROCESS Process
,
227 PPROCESS_DEVICEMAP_INFORMATION DeviceMapInfo
)
232 * FIXME: This is an ugly hack for now, to always return the System Device Map
233 * instead of returning the Process Device Map. Not important yet since we don't use it
236 /* FIXME: Acquire the DeviceMap Spinlock */
237 // KeAcquireSpinLock(DeviceMap->Lock, &OldIrql);
240 DeviceMapInfo
->Query
.DriveMap
= ObSystemDeviceMap
->DriveMap
;
241 RtlMoveMemory(DeviceMapInfo
->Query
.DriveType
, ObSystemDeviceMap
->DriveType
, sizeof(ObSystemDeviceMap
->DriveType
));
243 /* FIXME: Release the DeviceMap Spinlock */
244 // KeReleasepinLock(DeviceMap->Lock, OldIrql);
249 ObpAddEntryDirectory(PDIRECTORY_OBJECT Parent
,
250 POBJECT_HEADER Header
,
253 * FUNCTION: Add an entry to a namespace directory
255 * Parent = directory to add in
256 * Header = Header of the object to add the entry for
257 * Name = Name to give the entry
262 ASSERT(HEADER_TO_OBJECT_NAME(Header
));
263 HEADER_TO_OBJECT_NAME(Header
)->Directory
= Parent
;
265 KeAcquireSpinLock(&Parent
->Lock
, &oldlvl
);
266 InsertTailList(&Parent
->head
, &Header
->Entry
);
267 KeReleaseSpinLock(&Parent
->Lock
, oldlvl
);
273 ObpRemoveEntryDirectory(POBJECT_HEADER Header
)
275 * FUNCTION: Remove an entry from a namespace directory
277 * Header = Header of the object to remove
282 DPRINT("ObpRemoveEntryDirectory(Header %x)\n",Header
);
284 KeAcquireSpinLock(&(HEADER_TO_OBJECT_NAME(Header
)->Directory
->Lock
),&oldlvl
);
285 if (Header
->Entry
.Flink
&& Header
->Entry
.Blink
)
287 RemoveEntryList(&(Header
->Entry
));
288 Header
->Entry
.Flink
= Header
->Entry
.Blink
= NULL
;
290 KeReleaseSpinLock(&(HEADER_TO_OBJECT_NAME(Header
)->Directory
->Lock
),oldlvl
);
295 ObpCreateDirectory(OB_OPEN_REASON Reason
,
299 ACCESS_MASK GrantedAccess
)
301 PDIRECTORY_OBJECT Directory
= ObjectBody
;
303 if (Reason
== ObCreateHandle
)
305 InitializeListHead(&Directory
->head
);
306 KeInitializeSpinLock(&Directory
->Lock
);
309 return STATUS_SUCCESS
;
313 ObpFindEntryDirectory(PDIRECTORY_OBJECT DirectoryObject
,
317 PLIST_ENTRY current
= DirectoryObject
->head
.Flink
;
318 POBJECT_HEADER current_obj
;
320 DPRINT("ObFindEntryDirectory(dir %x, name %S)\n",DirectoryObject
, Name
);
324 return(DirectoryObject
);
326 if (Name
[0]=='.' && Name
[1]==0)
328 return(DirectoryObject
);
330 if (Name
[0]=='.' && Name
[1]=='.' && Name
[2]==0)
332 return(HEADER_TO_OBJECT_NAME(BODY_TO_HEADER(DirectoryObject
))->Directory
);
334 while (current
!=(&(DirectoryObject
->head
)))
336 current_obj
= CONTAINING_RECORD(current
,OBJECT_HEADER
,Entry
);
337 DPRINT(" Scanning: %S for: %S\n",HEADER_TO_OBJECT_NAME(current_obj
)->Name
.Buffer
, Name
);
338 if (Attributes
& OBJ_CASE_INSENSITIVE
)
340 if (_wcsicmp(HEADER_TO_OBJECT_NAME(current_obj
)->Name
.Buffer
, Name
)==0)
342 DPRINT("Found it %x\n",¤t_obj
->Body
);
343 return(¤t_obj
->Body
);
348 if ( wcscmp(HEADER_TO_OBJECT_NAME(current_obj
)->Name
.Buffer
, Name
)==0)
350 DPRINT("Found it %x\n",¤t_obj
->Body
);
351 return(¤t_obj
->Body
);
354 current
= current
->Flink
;
356 DPRINT(" Not Found: %s() = NULL\n",__FUNCTION__
);
362 ObpParseDirectory(PVOID Object
,
364 PUNICODE_STRING FullPath
,
373 DPRINT("ObpParseDirectory(Object %x, Path %x, *Path %S)\n",
380 return STATUS_UNSUCCESSFUL
;
387 End
= wcschr(Start
, L
'\\');
393 KeAcquireSpinLock(&(((PDIRECTORY_OBJECT
)Object
)->Lock
), &oldlvl
);
394 FoundObject
= ObpFindEntryDirectory(Object
, Start
, Attributes
);
395 if (FoundObject
== NULL
)
397 KeReleaseSpinLock(&(((PDIRECTORY_OBJECT
)Object
)->Lock
), oldlvl
);
402 return STATUS_UNSUCCESSFUL
;
405 ObReferenceObjectByPointer(FoundObject
,
406 STANDARD_RIGHTS_REQUIRED
,
409 KeReleaseSpinLock(&(((PDIRECTORY_OBJECT
)Object
)->Lock
), oldlvl
);
420 *NextObject
= FoundObject
;
422 return STATUS_SUCCESS
;
429 OBJECT_ATTRIBUTES ObjectAttributes
;
431 SECURITY_DESCRIPTOR SecurityDescriptor
;
432 OBJECT_TYPE_INITIALIZER ObjectTypeInitializer
;
434 /* Initialize the security descriptor cache */
437 /* Initialize the Default Event */
438 KeInitializeEvent(&ObpDefaultObject
, NotificationEvent
, TRUE
);
440 /* Create the Type Type */
441 DPRINT("Creating Type Type\n");
442 RtlZeroMemory(&ObjectTypeInitializer
, sizeof(ObjectTypeInitializer
));
443 RtlInitUnicodeString(&Name
, L
"Type");
444 ObjectTypeInitializer
.Length
= sizeof(ObjectTypeInitializer
);
445 ObjectTypeInitializer
.ValidAccessMask
= OBJECT_TYPE_ALL_ACCESS
;
446 ObjectTypeInitializer
.UseDefaultObject
= TRUE
;
447 ObjectTypeInitializer
.MaintainTypeList
= TRUE
;
448 ObjectTypeInitializer
.PoolType
= NonPagedPool
;
449 ObjectTypeInitializer
.GenericMapping
= ObpTypeMapping
;
450 ObjectTypeInitializer
.DefaultNonPagedPoolCharge
= sizeof(OBJECT_TYPE
);
451 ObpCreateTypeObject(&ObjectTypeInitializer
, &Name
, &ObTypeObjectType
);
453 /* Create the Directory Type */
454 DPRINT("Creating Directory Type\n");
455 RtlZeroMemory(&ObjectTypeInitializer
, sizeof(ObjectTypeInitializer
));
456 RtlInitUnicodeString(&Name
, L
"Directory");
457 ObjectTypeInitializer
.Length
= sizeof(ObjectTypeInitializer
);
458 ObjectTypeInitializer
.ValidAccessMask
= DIRECTORY_ALL_ACCESS
;
459 ObjectTypeInitializer
.UseDefaultObject
= FALSE
;
460 ObjectTypeInitializer
.OpenProcedure
= ObpCreateDirectory
;
461 ObjectTypeInitializer
.ParseProcedure
= ObpParseDirectory
;
462 ObjectTypeInitializer
.MaintainTypeList
= FALSE
;
463 ObjectTypeInitializer
.GenericMapping
= ObpDirectoryMapping
;
464 ObjectTypeInitializer
.DefaultNonPagedPoolCharge
= sizeof(DIRECTORY_OBJECT
);
465 ObpCreateTypeObject(&ObjectTypeInitializer
, &Name
, &ObDirectoryType
);
467 /* Create security descriptor */
468 RtlCreateSecurityDescriptor(&SecurityDescriptor
,
469 SECURITY_DESCRIPTOR_REVISION1
);
470 RtlSetOwnerSecurityDescriptor(&SecurityDescriptor
,
473 RtlSetGroupSecurityDescriptor(&SecurityDescriptor
,
476 RtlSetDaclSecurityDescriptor(&SecurityDescriptor
,
481 /* Create root directory */
482 DPRINT("Creating Root Directory\n");
483 InitializeObjectAttributes(&ObjectAttributes
,
487 &SecurityDescriptor
);
488 ObCreateObject(KernelMode
,
493 sizeof(DIRECTORY_OBJECT
),
496 (PVOID
*)&NameSpaceRoot
);
497 ObInsertObject((PVOID
)NameSpaceRoot
,
499 DIRECTORY_ALL_ACCESS
,
504 /* Create '\ObjectTypes' directory */
505 RtlInitUnicodeString(&Name
, L
"\\ObjectTypes");
506 InitializeObjectAttributes(&ObjectAttributes
,
510 &SecurityDescriptor
);
511 ObCreateObject(KernelMode
,
516 sizeof(DIRECTORY_OBJECT
),
519 (PVOID
*)&ObpTypeDirectoryObject
);
520 ObInsertObject((PVOID
)ObpTypeDirectoryObject
,
522 DIRECTORY_ALL_ACCESS
,
527 /* Insert the two objects we already created but couldn't add */
528 /* NOTE: Uses TypeList & Creator Info in OB 2.0 */
529 ObpAddEntryDirectory(ObpTypeDirectoryObject
, BODY_TO_HEADER(ObTypeObjectType
), NULL
);
530 ObpAddEntryDirectory(ObpTypeDirectoryObject
, BODY_TO_HEADER(ObDirectoryType
), NULL
);
532 /* Create 'symbolic link' object type */
533 ObInitSymbolicLinkImplementation();
535 /* FIXME: Hack Hack! */
536 ObSystemDeviceMap
= ExAllocatePoolWithTag(NonPagedPool
, sizeof(*ObSystemDeviceMap
), TAG('O', 'b', 'D', 'm'));
537 RtlZeroMemory(ObSystemDeviceMap
, sizeof(*ObSystemDeviceMap
));
542 ObpCreateTypeObject(POBJECT_TYPE_INITIALIZER ObjectTypeInitializer
,
543 PUNICODE_STRING TypeName
,
544 POBJECT_TYPE
*ObjectType
)
546 POBJECT_HEADER Header
;
547 POBJECT_TYPE LocalObjectType
;
551 DPRINT("ObpCreateTypeObject(ObjectType: %wZ)\n", TypeName
);
553 /* Allocate the Object */
554 Status
= ObpAllocateObject(NULL
,
557 OBJECT_ALLOC_SIZE(sizeof(OBJECT_TYPE
)),
559 if (!NT_SUCCESS(Status
))
561 DPRINT1("ObpAllocateObject failed!\n");
565 LocalObjectType
= (POBJECT_TYPE
)&Header
->Body
;
566 DPRINT("Local ObjectType: %p Header: %p \n", LocalObjectType
, Header
);
568 /* Check if this is the first Object Type */
569 if (!ObTypeObjectType
)
571 ObTypeObjectType
= LocalObjectType
;
572 Header
->Type
= ObTypeObjectType
;
573 LocalObjectType
->Key
= TAG('O', 'b', 'j', 'T');
578 Tag
[0] = TypeName
->Buffer
[0];
579 Tag
[1] = TypeName
->Buffer
[1];
580 Tag
[2] = TypeName
->Buffer
[2];
581 Tag
[3] = TypeName
->Buffer
[3];
584 DPRINT("Convert: %s \n", Tag
);
585 LocalObjectType
->Key
= *(PULONG
)Tag
;
589 LocalObjectType
->TypeInfo
= *ObjectTypeInitializer
;
590 LocalObjectType
->Name
= *TypeName
;
591 LocalObjectType
->TypeInfo
.PoolType
= ObjectTypeInitializer
->PoolType
;
593 /* These two flags need to be manually set up */
594 Header
->Flags
|= OB_FLAG_KERNEL_MODE
| OB_FLAG_PERMANENT
;
596 /* Check if we have to maintain a type list */
597 if (NtGlobalFlag
& FLG_MAINTAIN_OBJECT_TYPELIST
)
600 LocalObjectType
->TypeInfo
.MaintainTypeList
= TRUE
;
603 /* Calculate how much space our header'll take up */
604 HeaderSize
= sizeof(OBJECT_HEADER
) + sizeof(OBJECT_HEADER_NAME_INFO
) +
605 (ObjectTypeInitializer
->MaintainHandleCount
?
606 sizeof(OBJECT_HEADER_HANDLE_INFO
) : 0);
608 /* Update the Pool Charges */
609 if (ObjectTypeInitializer
->PoolType
== NonPagedPool
)
611 LocalObjectType
->TypeInfo
.DefaultNonPagedPoolCharge
+= HeaderSize
;
615 LocalObjectType
->TypeInfo
.DefaultPagedPoolCharge
+= HeaderSize
;
618 /* All objects types need a security procedure */
619 if (!ObjectTypeInitializer
->SecurityProcedure
)
621 LocalObjectType
->TypeInfo
.SecurityProcedure
= SeDefaultObjectMethod
;
624 /* Select the Wait Object */
625 if (LocalObjectType
->TypeInfo
.UseDefaultObject
)
627 /* Add the SYNCHRONIZE access mask since it's waitable */
628 LocalObjectType
->TypeInfo
.ValidAccessMask
|= SYNCHRONIZE
;
630 /* Use the "Default Object", a simple event */
631 LocalObjectType
->DefaultObject
= &ObpDefaultObject
;
633 /* Special system objects get an optimized hack so they can be waited on */
634 else if (TypeName
->Length
== 8 && !wcscmp(TypeName
->Buffer
, L
"File"))
636 LocalObjectType
->DefaultObject
= (PVOID
)FIELD_OFFSET(FILE_OBJECT
, Event
);
638 /* FIXME: When LPC stops sucking, add a hack for Waitable Ports */
641 /* No default Object */
642 LocalObjectType
->DefaultObject
= NULL
;
645 /* Initialize Object Type components */
646 ExInitializeResourceLite(&LocalObjectType
->Mutex
);
647 InitializeListHead(&LocalObjectType
->TypeList
);
649 /* Insert it into the Object Directory */
650 if (ObpTypeDirectoryObject
)
652 ObpAddEntryDirectory(ObpTypeDirectoryObject
, Header
, TypeName
->Buffer
);
653 ObReferenceObject(ObpTypeDirectoryObject
);
656 *ObjectType
= LocalObjectType
;