1 /* $Id: namespc.c,v 1.29 2002/03/01 00:47:40 ekohl Exp $
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
7 * PROGRAMMER: David Welch (welch@mcmail.com)
12 /* INCLUDES ***************************************************************/
15 #include <ddk/ntddk.h>
16 #include <internal/ob.h>
17 #include <internal/io.h>
18 #include <internal/pool.h>
21 #include <internal/debug.h>
23 /* GLOBALS ****************************************************************/
25 POBJECT_TYPE ObDirectoryType
= NULL
;
26 POBJECT_TYPE ObTypeObjectType
= NULL
;
28 PDIRECTORY_OBJECT NameSpaceRoot
= NULL
;
30 static GENERIC_MAPPING ObpDirectoryMapping
= {
31 STANDARD_RIGHTS_READ
|DIRECTORY_QUERY
|DIRECTORY_TRAVERSE
,
32 STANDARD_RIGHTS_WRITE
|DIRECTORY_CREATE_OBJECT
|DIRECTORY_CREATE_SUBDIRECTORY
,
33 STANDARD_RIGHTS_EXECUTE
|DIRECTORY_QUERY
|DIRECTORY_TRAVERSE
,
34 DIRECTORY_ALL_ACCESS
};
36 static GENERIC_MAPPING ObpTypeMapping
= {
38 STANDARD_RIGHTS_WRITE
,
39 STANDARD_RIGHTS_EXECUTE
,
42 /* FUNCTIONS **************************************************************/
45 ObReferenceObjectByName(PUNICODE_STRING ObjectPath
,
47 PACCESS_STATE PassedAccessState
,
48 ACCESS_MASK DesiredAccess
,
49 POBJECT_TYPE ObjectType
,
50 KPROCESSOR_MODE AccessMode
,
55 UNICODE_STRING RemainingPath
;
56 OBJECT_ATTRIBUTES ObjectAttributes
;
59 InitializeObjectAttributes(&ObjectAttributes
,
64 Status
= ObFindObject(&ObjectAttributes
,
68 if (!NT_SUCCESS(Status
))
73 DPRINT("RemainingPath.Buffer '%S' Object %p\n", RemainingPath
.Buffer
, Object
);
75 if (RemainingPath
.Buffer
!= NULL
|| Object
== NULL
)
78 DPRINT("Object %p\n", Object
);
80 RtlFreeUnicodeString (&RemainingPath
);
81 return(STATUS_UNSUCCESSFUL
);
84 RtlFreeUnicodeString (&RemainingPath
);
85 return(STATUS_SUCCESS
);
89 /**********************************************************************
94 * Obtain a handle to an existing object.
116 ObOpenObjectByName(IN POBJECT_ATTRIBUTES ObjectAttributes
,
117 IN POBJECT_TYPE ObjectType
,
118 IN OUT PVOID ParseContext
,
119 IN KPROCESSOR_MODE AccessMode
,
120 IN ACCESS_MASK DesiredAccess
,
121 IN PACCESS_STATE PassedAccessState
,
124 UNICODE_STRING RemainingPath
;
128 DPRINT("ObOpenObjectByName()\n");
130 Status
= ObFindObject(ObjectAttributes
,
134 if (!NT_SUCCESS(Status
))
139 if (RemainingPath
.Buffer
!= NULL
||
142 RtlFreeUnicodeString(&RemainingPath
);
143 return STATUS_UNSUCCESSFUL
;
146 Status
= ObCreateHandle(PsGetCurrentProcess(),
152 ObDereferenceObject(Object
);
153 RtlFreeUnicodeString(&RemainingPath
);
160 ObpAddEntryDirectory(PDIRECTORY_OBJECT Parent
,
161 POBJECT_HEADER Header
,
164 * FUNCTION: Add an entry to a namespace directory
166 * Parent = directory to add in
167 * Header = Header of the object to add the entry for
168 * Name = Name to give the entry
173 RtlCreateUnicodeString(&Header
->Name
, Name
);
174 Header
->Parent
= Parent
;
176 KeAcquireSpinLock(&Parent
->Lock
, &oldlvl
);
177 InsertTailList(&Parent
->head
, &Header
->Entry
);
178 KeReleaseSpinLock(&Parent
->Lock
, oldlvl
);
182 ObpRemoveEntryDirectory(POBJECT_HEADER Header
)
184 * FUNCTION: Remove an entry from a namespace directory
186 * Header = Header of the object to remove
191 DPRINT("ObpRemoveEntryDirectory(Header %x)\n",Header
);
193 KeAcquireSpinLock(&(Header
->Parent
->Lock
),&oldlvl
);
194 RemoveEntryList(&(Header
->Entry
));
195 KeReleaseSpinLock(&(Header
->Parent
->Lock
),oldlvl
);
199 ObpFindEntryDirectory(PDIRECTORY_OBJECT DirectoryObject
,
203 PLIST_ENTRY current
= DirectoryObject
->head
.Flink
;
204 POBJECT_HEADER current_obj
;
206 DPRINT("ObFindEntryDirectory(dir %x, name %S)\n",DirectoryObject
, Name
);
210 return(DirectoryObject
);
212 if (Name
[0]=='.' && Name
[1]==0)
214 return(DirectoryObject
);
216 if (Name
[0]=='.' && Name
[1]=='.' && Name
[2]==0)
218 return(BODY_TO_HEADER(DirectoryObject
)->Parent
);
220 while (current
!=(&(DirectoryObject
->head
)))
222 current_obj
= CONTAINING_RECORD(current
,OBJECT_HEADER
,Entry
);
223 DPRINT("Scanning %S %S\n",current_obj
->Name
.Buffer
, Name
);
224 if (Attributes
& OBJ_CASE_INSENSITIVE
)
226 if (_wcsicmp(current_obj
->Name
.Buffer
, Name
)==0)
228 DPRINT("Found it %x\n",HEADER_TO_BODY(current_obj
));
229 return(HEADER_TO_BODY(current_obj
));
234 if ( wcscmp(current_obj
->Name
.Buffer
, Name
)==0)
236 DPRINT("Found it %x\n",HEADER_TO_BODY(current_obj
));
237 return(HEADER_TO_BODY(current_obj
));
240 current
= current
->Flink
;
242 DPRINT("%s() = NULL\n",__FUNCTION__
);
247 ObpParseDirectory(PVOID Object
,
249 PUNICODE_STRING FullPath
,
251 POBJECT_TYPE ObjectType
,
257 DPRINT("ObpParseDirectory(Object %x, Path %x, *Path %S)\n",
264 return STATUS_UNSUCCESSFUL
;
267 end
= wcschr((*Path
)+1, '\\');
273 FoundObject
= ObpFindEntryDirectory(Object
, (*Path
)+1, Attributes
);
275 if (FoundObject
== NULL
)
281 return STATUS_UNSUCCESSFUL
;
284 ObReferenceObjectByPointer(FoundObject
,
285 STANDARD_RIGHTS_REQUIRED
,
299 *NextObject
= FoundObject
;
301 return STATUS_SUCCESS
;
305 ObpCreateDirectory(PVOID ObjectBody
,
308 POBJECT_ATTRIBUTES ObjectAttributes
)
310 PDIRECTORY_OBJECT DirectoryObject
= (PDIRECTORY_OBJECT
)ObjectBody
;
312 DPRINT("ObpCreateDirectory(ObjectBody %x, Parent %x, RemainingPath %S)\n",
313 ObjectBody
, Parent
, RemainingPath
);
315 if (RemainingPath
!= NULL
&& wcschr(RemainingPath
+1, '\\') != NULL
)
317 return(STATUS_UNSUCCESSFUL
);
320 InitializeListHead(&DirectoryObject
->head
);
321 KeInitializeSpinLock(&DirectoryObject
->Lock
);
323 return(STATUS_SUCCESS
);
330 * FUNCTION: Initialize the object manager namespace
333 OBJECT_ATTRIBUTES ObjectAttributes
;
336 /* create 'directory' object type */
337 ObDirectoryType
= ExAllocatePool(NonPagedPool
,sizeof(OBJECT_TYPE
));
339 ObDirectoryType
->Tag
= TAG('D', 'I', 'R', 'T');
340 ObDirectoryType
->TotalObjects
= 0;
341 ObDirectoryType
->TotalHandles
= 0;
342 ObDirectoryType
->MaxObjects
= ULONG_MAX
;
343 ObDirectoryType
->MaxHandles
= ULONG_MAX
;
344 ObDirectoryType
->PagedPoolCharge
= 0;
345 ObDirectoryType
->NonpagedPoolCharge
= sizeof(DIRECTORY_OBJECT
);
346 ObDirectoryType
->Mapping
= &ObpDirectoryMapping
;
347 ObDirectoryType
->Dump
= NULL
;
348 ObDirectoryType
->Open
= NULL
;
349 ObDirectoryType
->Close
= NULL
;
350 ObDirectoryType
->Delete
= NULL
;
351 ObDirectoryType
->Parse
= ObpParseDirectory
;
352 ObDirectoryType
->Security
= NULL
;
353 ObDirectoryType
->QueryName
= NULL
;
354 ObDirectoryType
->OkayToClose
= NULL
;
355 ObDirectoryType
->Create
= ObpCreateDirectory
;
356 ObDirectoryType
->DuplicationNotify
= NULL
;
358 RtlInitUnicodeString(&ObDirectoryType
->TypeName
,
361 /* create 'type' object type*/
362 ObTypeObjectType
= ExAllocatePool(NonPagedPool
,sizeof(OBJECT_TYPE
));
364 ObTypeObjectType
->Tag
= TAG('T', 'y', 'p', 'T');
365 ObTypeObjectType
->TotalObjects
= 0;
366 ObTypeObjectType
->TotalHandles
= 0;
367 ObTypeObjectType
->MaxObjects
= ULONG_MAX
;
368 ObTypeObjectType
->MaxHandles
= ULONG_MAX
;
369 ObTypeObjectType
->PagedPoolCharge
= 0;
370 ObTypeObjectType
->NonpagedPoolCharge
= sizeof(TYPE_OBJECT
);
371 ObTypeObjectType
->Mapping
= &ObpTypeMapping
;
372 ObTypeObjectType
->Dump
= NULL
;
373 ObTypeObjectType
->Open
= NULL
;
374 ObTypeObjectType
->Close
= NULL
;
375 ObTypeObjectType
->Delete
= NULL
;
376 ObTypeObjectType
->Parse
= NULL
;
377 ObTypeObjectType
->Security
= NULL
;
378 ObTypeObjectType
->QueryName
= NULL
;
379 ObTypeObjectType
->OkayToClose
= NULL
;
380 ObTypeObjectType
->Create
= NULL
;
381 ObTypeObjectType
->DuplicationNotify
= NULL
;
383 RtlInitUnicodeString(&ObTypeObjectType
->TypeName
,
386 /* create root directory */
388 STANDARD_RIGHTS_REQUIRED
,
391 (PVOID
*)&NameSpaceRoot
);
393 /* create '\ObjectTypes' directory */
394 RtlInitUnicodeString(&Name
,
396 InitializeObjectAttributes(&ObjectAttributes
,
402 STANDARD_RIGHTS_REQUIRED
,
407 ObpCreateTypeObject(ObDirectoryType
);
408 ObpCreateTypeObject(ObTypeObjectType
);
413 ObpCreateTypeObject(POBJECT_TYPE ObjectType
)
415 OBJECT_ATTRIBUTES ObjectAttributes
;
416 WCHAR NameString
[80];
417 PTYPE_OBJECT TypeObject
= NULL
;
422 RtlInitUnicodeString(&Name
,
424 InitializeObjectAttributes(&ObjectAttributes
,
430 Status
= NtOpenDirectoryObject(&TypesDir
,
431 STANDARD_RIGHTS_REQUIRED
,
433 if (!NT_SUCCESS(Status
))
436 DPRINT("ObjectType: %wZ\n", &ObjectType
->TypeName
);
437 wcscpy(NameString
, L
"\\");
438 wcscat(NameString
, ObjectType
->TypeName
.Buffer
);
439 RtlInitUnicodeString(&Name
,
442 InitializeObjectAttributes(&ObjectAttributes
,
447 Status
= ObCreateObject(NULL
,
448 STANDARD_RIGHTS_REQUIRED
,
451 (PVOID
*)&TypeObject
);
452 if (NT_SUCCESS(Status
))
454 TypeObject
->ObjectType
= ObjectType
;
459 return(STATUS_SUCCESS
);