bootstrap
[reactos.git] / ntoskrnl / ob / handle.c
1 /*
2  *  ReactOS kernel
3  *  Copyright (C) 1998, 1999, 2000, 2001 ReactOS Team
4  *
5  *  This program is free software; you can redistribute it and/or modify
6  *  it under the terms of the GNU General Public License as published by
7  *  the Free Software Foundation; either version 2 of the License, or
8  *  (at your option) any later version.
9  *
10  *  This program is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  *  GNU General Public License for more details.
14  *
15  *  You should have received a copy of the GNU General Public License
16  *  along with this program; if not, write to the Free Software
17  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */
19 /* $Id$
20  *
21  * COPYRIGHT:          See COPYING in the top level directory
22  * PROJECT:            ReactOS kernel
23  * FILE:               ntoskrnl/ob/handle.c
24  * PURPOSE:            Managing handles
25  * PROGRAMMER:         David Welch (welch@cwcom.net)
26  * REVISION HISTORY:
27  *                 17/06/98: Created
28  */
29
30 /* INCLUDES ****************************************************************/
31
32 #include <ddk/ntddk.h>
33 #include <internal/ob.h>
34 #include <internal/ps.h>
35 #include <internal/pool.h>
36 #include <internal/safe.h>
37
38 #define NDEBUG
39 #include <internal/debug.h>
40
41 /* TYPES *******************************************************************/
42
43 /*
44  * PURPOSE: Defines a handle
45  */
46 typedef struct
47 {
48    PVOID ObjectBody;
49    ACCESS_MASK GrantedAccess;
50    BOOLEAN Inherit;
51 } HANDLE_REP, *PHANDLE_REP;
52
53 #define HANDLE_BLOCK_ENTRIES ((PAGE_SIZE-sizeof(LIST_ENTRY))/sizeof(HANDLE_REP))
54
55
56 /*
57  * PURPOSE: Defines a page's worth of handles
58  */
59 typedef struct
60 {
61    LIST_ENTRY entry;
62    HANDLE_REP handles[HANDLE_BLOCK_ENTRIES];
63 } HANDLE_BLOCK, *PHANDLE_BLOCK;
64
65
66 /* GLOBALS *******************************************************************/
67
68 #define TAG_HANDLE_TABLE    TAG('H', 'T', 'B', 'L')
69
70 /* FUNCTIONS ***************************************************************/
71
72
73 static PHANDLE_REP ObpGetObjectByHandle(PHANDLE_TABLE HandleTable, HANDLE h)
74 /*
75  * FUNCTION: Get the data structure for a handle
76  * ARGUMENTS:
77  *         Process = Process to get the handle for
78  *         h = Handle
79  * ARGUMENTS: A pointer to the information about the handle on success,
80  *            NULL on failure
81  */
82 {
83    PLIST_ENTRY current;
84    unsigned int handle = (((unsigned int)h) >> 2) - 1;
85    unsigned int count=handle/HANDLE_BLOCK_ENTRIES;
86    HANDLE_BLOCK* blk = NULL;
87    unsigned int i;
88    
89    DPRINT("ObpGetObjectByHandle(HandleTable %x, h %x)\n",HandleTable,h);
90    
91    current = HandleTable->ListHead.Flink;
92    DPRINT("current %x\n",current);
93    
94    for (i=0;i<count;i++)
95      {
96         current = current->Flink;
97         if (current == (&(HandleTable->ListHead)))
98           {
99              return(NULL);
100           }
101      }
102    
103    blk = CONTAINING_RECORD(current,HANDLE_BLOCK,entry);
104    DPRINT("object: %p\n",&(blk->handles[handle%HANDLE_BLOCK_ENTRIES]));
105    return(&(blk->handles[handle%HANDLE_BLOCK_ENTRIES]));
106 }
107
108 #ifndef LIBCAPTIVE
109
110 NTSTATUS
111 ObDuplicateObject(PEPROCESS SourceProcess,
112                   PEPROCESS TargetProcess,
113                   HANDLE SourceHandle,
114                   PHANDLE TargetHandle,
115                   ACCESS_MASK DesiredAccess,
116                   BOOLEAN InheritHandle,
117                   ULONG Options)
118 {
119   KIRQL oldIrql;
120   PHANDLE_REP SourceHandleRep;
121   PVOID ObjectBody;
122
123   KeAcquireSpinLock(&SourceProcess->HandleTable.ListLock, &oldIrql);
124   SourceHandleRep = ObpGetObjectByHandle(&SourceProcess->HandleTable,
125                                          SourceHandle);
126   if (SourceHandleRep == NULL)
127     {
128       KeReleaseSpinLock(&SourceProcess->HandleTable.ListLock, oldIrql);
129       return(STATUS_INVALID_HANDLE);
130     }
131   ObjectBody = SourceHandleRep->ObjectBody;
132   ObReferenceObjectByPointer(ObjectBody,
133                              0,
134                              NULL,
135                              UserMode);
136   
137   if (Options & DUPLICATE_SAME_ACCESS)
138     {
139       DesiredAccess = SourceHandleRep->GrantedAccess;
140     }
141   
142   KeReleaseSpinLock(&SourceProcess->HandleTable.ListLock, oldIrql);
143   ObCreateHandle(TargetProcess,
144                  ObjectBody,
145                  DesiredAccess,
146                  InheritHandle,
147                  TargetHandle);
148   
149   if (Options & DUPLICATE_CLOSE_SOURCE)
150     {
151       ZwClose(SourceHandle);
152     }
153   
154   ObDereferenceObject(ObjectBody);
155   return(STATUS_SUCCESS);
156 }
157
158 NTSTATUS STDCALL 
159 NtDuplicateObject (IN   HANDLE          SourceProcessHandle,
160                    IN   HANDLE          SourceHandle,
161                    IN   HANDLE          TargetProcessHandle,
162                    OUT  PHANDLE         UnsafeTargetHandle,
163                    IN   ACCESS_MASK     DesiredAccess,
164                    IN   BOOLEAN         InheritHandle,
165                    ULONG                Options)
166 /*
167  * FUNCTION: Copies a handle from one process space to another
168  * ARGUMENTS:
169  *         SourceProcessHandle = The source process owning the handle. The 
170  *                               source process should have opened
171  *                               the SourceHandle with PROCESS_DUP_HANDLE 
172  *                               access.
173  *         SourceHandle = The handle to the object.
174  *         TargetProcessHandle = The destination process owning the handle 
175  *         TargetHandle (OUT) = Caller should supply storage for the 
176  *                              duplicated handle. 
177  *         DesiredAccess = The desired access to the handle.
178  *         InheritHandle = Indicates wheter the new handle will be inheritable
179  *                         or not.
180  *         Options = Specifies special actions upon duplicating the handle. 
181  *                   Can be one of the values DUPLICATE_CLOSE_SOURCE | 
182  *                   DUPLICATE_SAME_ACCESS. DUPLICATE_CLOSE_SOURCE specifies 
183  *                   that the source handle should be closed after duplicating. 
184  *                   DUPLICATE_SAME_ACCESS specifies to ignore the 
185  *                   DesiredAccess paramter and just grant the same access to 
186  *                   the new handle.
187  * RETURNS: Status
188  * REMARKS: This function maps to the win32 DuplicateHandle.
189  */
190 {
191    PEPROCESS SourceProcess;
192    PEPROCESS TargetProcess;
193    PHANDLE_REP SourceHandleRep;
194    KIRQL oldIrql;
195    PVOID ObjectBody;
196    HANDLE TargetHandle;
197    NTSTATUS Status;
198    
199    ASSERT_IRQL(PASSIVE_LEVEL);
200    
201    Status = ObReferenceObjectByHandle(SourceProcessHandle,
202                                       PROCESS_DUP_HANDLE,
203                                       NULL,
204                                       UserMode,
205                                       (PVOID*)&SourceProcess,
206                                       NULL);
207    if (!NT_SUCCESS(Status))
208      {
209        return(Status);
210      }
211    Status = ObReferenceObjectByHandle(TargetProcessHandle,
212                                       PROCESS_DUP_HANDLE,
213                                       NULL,
214                                       UserMode,
215                                       (PVOID*)&TargetProcess,
216                                       NULL);
217    if (!NT_SUCCESS(Status))
218      {
219        ObDereferenceObject(SourceProcess);
220        return(Status);
221      }
222    KeAcquireSpinLock(&SourceProcess->HandleTable.ListLock, &oldIrql);
223    SourceHandleRep = ObpGetObjectByHandle(&SourceProcess->HandleTable,
224                                           SourceHandle);
225    if (SourceHandleRep == NULL)
226      {
227         KeReleaseSpinLock(&SourceProcess->HandleTable.ListLock, oldIrql);
228         ObDereferenceObject(SourceProcess);
229         ObDereferenceObject(TargetProcess);
230         return(STATUS_INVALID_HANDLE);
231      }
232    ObjectBody = SourceHandleRep->ObjectBody;
233    ObReferenceObjectByPointer(ObjectBody,
234                               0,
235                               NULL,
236                               UserMode);
237    
238    if (Options & DUPLICATE_SAME_ACCESS)
239      {
240         DesiredAccess = SourceHandleRep->GrantedAccess;
241      }
242    
243    KeReleaseSpinLock(&SourceProcess->HandleTable.ListLock, oldIrql);
244    if (!SourceHandleRep->Inherit)
245      {
246        ObDereferenceObject(TargetProcess);
247        ObDereferenceObject(SourceProcess);
248        ObDereferenceObject(ObjectBody);
249        return STATUS_INVALID_HANDLE;
250      }
251    ObCreateHandle(TargetProcess,
252                   ObjectBody,
253                   DesiredAccess,
254                   InheritHandle,
255                   &TargetHandle);
256    
257    if (Options & DUPLICATE_CLOSE_SOURCE)
258      {
259         ZwClose(SourceHandle);
260      }
261    
262    ObDereferenceObject(TargetProcess);
263    ObDereferenceObject(SourceProcess);
264    ObDereferenceObject(ObjectBody);
265    
266    Status = MmCopyToCaller(UnsafeTargetHandle, &TargetHandle, sizeof(HANDLE));
267    if (!NT_SUCCESS(Status))
268      {
269        return(Status);
270      }
271
272    return(STATUS_SUCCESS);
273 }
274
275 VOID ObCloseAllHandles(PEPROCESS Process)
276 {
277    KIRQL oldIrql;
278    PHANDLE_TABLE HandleTable;
279    PLIST_ENTRY current_entry;
280    PHANDLE_BLOCK current;
281    ULONG i;
282    PVOID ObjectBody;
283    
284    DPRINT("ObCloseAllHandles(Process %x)\n", Process);
285    
286    HandleTable = &Process->HandleTable;
287    
288    KeAcquireSpinLock(&HandleTable->ListLock, &oldIrql);
289    
290    current_entry = HandleTable->ListHead.Flink;
291    
292    while (current_entry != &HandleTable->ListHead)
293      {
294         current = CONTAINING_RECORD(current_entry, HANDLE_BLOCK, entry);
295         
296         for (i = 0; i < HANDLE_BLOCK_ENTRIES; i++)
297           {
298              ObjectBody = current->handles[i].ObjectBody;
299              
300              if (ObjectBody != NULL)
301                {
302                   POBJECT_HEADER Header = BODY_TO_HEADER(ObjectBody);
303                   
304                   if (Header->ObjectType == PsProcessType ||
305                       Header->ObjectType == PsThreadType)
306                     {
307                        DPRINT("Deleting handle to %x\n", ObjectBody);
308                     }
309                   
310                   ObReferenceObjectByPointer(ObjectBody,
311                                              0,
312                                              NULL,
313                                              UserMode);
314                   InterlockedDecrement(&Header->HandleCount);
315                   current->handles[i].ObjectBody = NULL;
316                   
317                   KeReleaseSpinLock(&HandleTable->ListLock, oldIrql);
318                   KeDetachProcess();
319                   
320                   if ((Header->ObjectType != NULL) &&
321                       (Header->ObjectType->Close != NULL))
322                     {
323                        Header->ObjectType->Close(ObjectBody, 
324                                                  Header->HandleCount);
325                     }
326                   
327                   ObDereferenceObject(ObjectBody);
328                   KeAttachProcess(Process);
329                   KeAcquireSpinLock(&HandleTable->ListLock, &oldIrql);
330                   current_entry = &HandleTable->ListHead;
331                   break;
332                }
333           }
334         
335         current_entry = current_entry->Flink;
336      }
337    KeReleaseSpinLock(&HandleTable->ListLock, oldIrql);
338    DPRINT("ObCloseAllHandles() finished\n");
339    DPRINT("Type %x\n", BODY_TO_HEADER(Process)->ObjectType);
340 }
341
342 VOID ObDeleteHandleTable(PEPROCESS Process)
343 /*
344  * FUNCTION: Deletes the handle table associated with a process
345  */
346 {
347    PLIST_ENTRY current = NULL;
348    PHANDLE_TABLE HandleTable = NULL;
349    
350    ObCloseAllHandles(Process);
351    
352    HandleTable = &Process->HandleTable;
353    current = RemoveHeadList(&HandleTable->ListHead);
354    
355    while (current != &HandleTable->ListHead)
356      {
357         HANDLE_BLOCK* HandleBlock = CONTAINING_RECORD(current,
358                                                       HANDLE_BLOCK,
359                                                       entry);
360         DPRINT("Freeing %x\n", HandleBlock);
361         ExFreePool(HandleBlock);
362         
363         current = RemoveHeadList(&HandleTable->ListHead);
364      }
365 }
366
367 #endif /* LIBCAPTIVE */
368
369 VOID ObCreateHandleTable(PEPROCESS Parent,
370                          BOOLEAN Inherit,
371                          PEPROCESS Process)
372 /*
373  * FUNCTION: Creates a handle table for a process
374  * ARGUMENTS:
375  *       Parent = Parent process (or NULL if this is the first process)
376  *       Inherit = True if the process should inherit its parent's handles
377  *       Process = Process whose handle table is to be created
378  */
379 {
380    PHANDLE_TABLE ParentHandleTable, HandleTable;
381    KIRQL oldIrql;
382    PLIST_ENTRY parent_current;
383    ULONG i;
384    PHANDLE_BLOCK current_block, new_block;   
385
386    DPRINT("ObCreateHandleTable(Parent %x, Inherit %d, Process %x)\n",
387           Parent,Inherit,Process);
388    
389    InitializeListHead(&(Process->HandleTable.ListHead));
390    KeInitializeSpinLock(&(Process->HandleTable.ListLock));
391    
392    if (Parent != NULL)
393      {
394         ParentHandleTable = &Parent->HandleTable;
395         HandleTable = &Process->HandleTable;
396
397         KeAcquireSpinLock(&Parent->HandleTable.ListLock, &oldIrql);
398         KeAcquireSpinLockAtDpcLevel(&Process->HandleTable.ListLock);
399         
400         parent_current = ParentHandleTable->ListHead.Flink;
401         
402         while (parent_current != &ParentHandleTable->ListHead)
403           {
404              current_block = CONTAINING_RECORD(parent_current,
405                                                HANDLE_BLOCK,
406                                                entry);
407              new_block = ExAllocatePoolWithTag(NonPagedPool,
408                                                sizeof(HANDLE_BLOCK),
409                                                TAG_HANDLE_TABLE);
410              if (new_block == NULL)
411              {
412                 break;
413              }
414              RtlZeroMemory(new_block, sizeof(HANDLE_BLOCK));
415
416              for (i=0; i<HANDLE_BLOCK_ENTRIES; i++)
417              {
418                 if (current_block->handles[i].ObjectBody)
419                 {
420                    if (current_block->handles[i].Inherit && Inherit)
421                    {
422                       new_block->handles[i].ObjectBody = 
423                         current_block->handles[i].ObjectBody;
424                       new_block->handles[i].GrantedAccess = 
425                         current_block->handles[i].GrantedAccess;
426                       new_block->handles[i].Inherit = TRUE;
427                       InterlockedIncrement(&(BODY_TO_HEADER(current_block->handles[i].ObjectBody)->HandleCount));
428                    }
429                 }
430              }
431              InsertTailList(&Process->HandleTable.ListHead, &new_block->entry);
432              parent_current = parent_current->Flink;
433           }
434         KeReleaseSpinLockFromDpcLevel(&Process->HandleTable.ListLock);
435         KeReleaseSpinLock(&Parent->HandleTable.ListLock, oldIrql);
436      }
437 }
438
439
440 PVOID ObDeleteHandle(PEPROCESS Process, HANDLE Handle)
441 {
442    PHANDLE_REP Rep;
443    PVOID ObjectBody;
444    KIRQL oldIrql;
445    PHANDLE_TABLE HandleTable;
446    POBJECT_HEADER Header;
447    
448    DPRINT("ObDeleteHandle(Handle %x)\n",Handle);
449    
450    HandleTable = &Process->HandleTable;
451    
452    KeAcquireSpinLock(&HandleTable->ListLock, &oldIrql);
453    
454    Rep = ObpGetObjectByHandle(HandleTable, Handle);
455    if (Rep == NULL)
456      {
457         KeReleaseSpinLock(&HandleTable->ListLock, oldIrql);     
458         return(NULL);
459      }
460    
461    ObjectBody = Rep->ObjectBody;
462    DPRINT("ObjectBody %x\n", ObjectBody);
463    if (ObjectBody != NULL)
464      {
465         Header = BODY_TO_HEADER(ObjectBody);
466         InterlockedDecrement(&(BODY_TO_HEADER(ObjectBody)->HandleCount));
467         ObReferenceObjectByPointer(ObjectBody,
468                                    0,
469                                    NULL,
470                                    UserMode);
471         Rep->ObjectBody = NULL;
472    
473         KeReleaseSpinLock(&HandleTable->ListLock, oldIrql);
474    
475         if ((Header->ObjectType != NULL) &&
476             (Header->ObjectType->Close != NULL))
477           {
478              Header->ObjectType->Close(ObjectBody, Header->HandleCount);
479           }
480      }
481    else
482      {
483         KeReleaseSpinLock(&HandleTable->ListLock, oldIrql);
484      }
485    
486    DPRINT("Finished ObDeleteHandle()\n");
487    return(ObjectBody);
488 }
489
490
491 NTSTATUS ObCreateHandle(PEPROCESS Process,
492                         PVOID ObjectBody,
493                         ACCESS_MASK GrantedAccess,
494                         BOOLEAN Inherit,
495                         PHANDLE HandleReturn)
496 /*
497  * FUNCTION: Add a handle referencing an object
498  * ARGUMENTS:
499  *         obj = Object body that the handle should refer to
500  * RETURNS: The created handle
501  * NOTE: The handle is valid only in the context of the current process
502  */
503 {
504    LIST_ENTRY* current;
505    unsigned int handle=1;
506    unsigned int i;
507    HANDLE_BLOCK* new_blk = NULL;
508    PHANDLE_TABLE HandleTable;
509    KIRQL oldlvl;
510
511    DPRINT("ObCreateHandle(Process %x, obj %x)\n",Process,ObjectBody);
512
513    assert(Process);
514
515    if (ObjectBody != NULL)
516      {
517         InterlockedIncrement(&(BODY_TO_HEADER(ObjectBody)->HandleCount));
518      }
519    HandleTable = &Process->HandleTable;
520    KeAcquireSpinLock(&HandleTable->ListLock, &oldlvl);
521    current = HandleTable->ListHead.Flink;
522    /*
523     * Scan through the currently allocated handle blocks looking for a free
524     * slot
525     */
526    while (current != (&HandleTable->ListHead))
527      {
528         HANDLE_BLOCK* blk = CONTAINING_RECORD(current,HANDLE_BLOCK,entry);
529
530         DPRINT("Current %x\n",current);
531
532         for (i=0;i<HANDLE_BLOCK_ENTRIES;i++)
533           {
534              DPRINT("Considering slot %d containing %x\n",i,blk->handles[i]);
535              if (blk->handles[i].ObjectBody==NULL)
536                {
537                   blk->handles[i].ObjectBody = ObjectBody;
538                   blk->handles[i].GrantedAccess = GrantedAccess;
539                   blk->handles[i].Inherit = Inherit;
540                   KeReleaseSpinLock(&HandleTable->ListLock, oldlvl);
541                   *HandleReturn = (HANDLE)((handle + i) << 2);
542                   return(STATUS_SUCCESS);
543                }
544           }
545         
546         handle = handle + HANDLE_BLOCK_ENTRIES;
547         current = current->Flink;
548      }
549
550    /*
551     * Add a new handle block to the end of the list
552     */
553    new_blk = 
554      (HANDLE_BLOCK *)ExAllocatePoolWithTag(NonPagedPool,sizeof(HANDLE_BLOCK),
555                                            TAG_HANDLE_TABLE);
556    if (!new_blk)
557     {
558       *HandleReturn = (PHANDLE)NULL;
559       return(STATUS_INSUFFICIENT_RESOURCES);
560     }
561    RtlZeroMemory(new_blk,sizeof(HANDLE_BLOCK));
562    InsertTailList(&(Process->HandleTable.ListHead),
563                   &new_blk->entry);
564    new_blk->handles[0].ObjectBody = ObjectBody;
565    new_blk->handles[0].GrantedAccess = GrantedAccess;
566    new_blk->handles[0].Inherit = Inherit;
567    KeReleaseSpinLock(&HandleTable->ListLock, oldlvl);
568    *HandleReturn = (HANDLE)(handle << 2);
569    return(STATUS_SUCCESS);
570 }
571
572
573 NTSTATUS STDCALL
574 ObReferenceObjectByHandle(HANDLE Handle,
575                           ACCESS_MASK DesiredAccess,
576                           POBJECT_TYPE ObjectType,
577                           KPROCESSOR_MODE AccessMode,
578                           PVOID* Object,
579                           POBJECT_HANDLE_INFORMATION HandleInformationPtr)
580 /*
581  * FUNCTION: Increments the reference count for an object and returns a 
582  * pointer to its body
583  * ARGUMENTS:
584  *         Handle = Handle for the object
585  *         DesiredAccess = Desired access to the object
586  *         ObjectType
587  *         AccessMode 
588  *         Object (OUT) = Points to the object body on return
589  *         HandleInformation (OUT) = Contains information about the handle 
590  *                                   on return
591  * RETURNS: Status
592  */
593 {
594    PHANDLE_REP HandleRep;
595    POBJECT_HEADER ObjectHeader;
596    KIRQL oldIrql;
597    PVOID ObjectBody;
598    ACCESS_MASK GrantedAccess;
599    
600    ASSERT_IRQL(PASSIVE_LEVEL);
601    
602    DPRINT("ObReferenceObjectByHandle(Handle %x, DesiredAccess %x, "
603            "ObjectType %x, AccessMode %d, Object %x)\n",Handle,DesiredAccess,
604            ObjectType,AccessMode,Object);
605
606    
607    /*
608     * Handle special handle names
609     */
610    if (Handle == NtCurrentProcess() && 
611        (ObjectType == PsProcessType || ObjectType == NULL))
612      {
613         DPRINT("Reference from %x\n", ((PULONG)&Handle)[-1]);
614         
615         ObReferenceObjectByPointer(PsGetCurrentProcess(),
616                                    PROCESS_ALL_ACCESS,
617                                    PsProcessType,
618                                    UserMode);
619         *Object = PsGetCurrentProcess();
620         DPRINT("Referencing current process %x\n", PsGetCurrentProcess());
621         return(STATUS_SUCCESS);
622      }
623    else if (Handle == NtCurrentProcess())
624      {
625         CHECKPOINT;
626         return(STATUS_OBJECT_TYPE_MISMATCH);
627      }
628    if (Handle == NtCurrentThread() && 
629        (ObjectType == PsThreadType || ObjectType == NULL))
630      {
631         ObReferenceObjectByPointer(PsGetCurrentThread(),
632                                    THREAD_ALL_ACCESS,
633                                    PsThreadType,
634                                    UserMode);
635         *Object = PsGetCurrentThread();
636         CHECKPOINT;
637         return(STATUS_SUCCESS);
638      }
639    else if (Handle == NtCurrentThread())
640      {
641         CHECKPOINT;
642         return(STATUS_OBJECT_TYPE_MISMATCH);
643      }
644    
645    KeAcquireSpinLock(&PsGetCurrentProcess()->HandleTable.ListLock,
646                      &oldIrql);
647    HandleRep = ObpGetObjectByHandle(&PsGetCurrentProcess()->HandleTable,
648                                     Handle);
649    if (HandleRep == NULL || HandleRep->ObjectBody == NULL)
650      {
651         KeReleaseSpinLock(&PsGetCurrentProcess()->HandleTable.ListLock,
652                           oldIrql);
653         return(STATUS_INVALID_HANDLE);
654      }
655    ObjectBody = HandleRep->ObjectBody;
656    DPRINT("ObjectBody %p\n",ObjectBody);
657    ObjectHeader = BODY_TO_HEADER(ObjectBody);
658    DPRINT("ObjectHeader->RefCount %lu\n",ObjectHeader->RefCount);
659    ObReferenceObjectByPointer(ObjectBody,
660                               0,
661                               NULL,
662                               UserMode);
663    GrantedAccess = HandleRep->GrantedAccess;
664    KeReleaseSpinLock(&PsGetCurrentProcess()->HandleTable.ListLock,
665                      oldIrql);
666    
667    ObjectHeader = BODY_TO_HEADER(ObjectBody);
668    DPRINT("ObjectHeader->RefCount %lu\n",ObjectHeader->RefCount);
669
670    if (ObjectType != NULL && ObjectType != ObjectHeader->ObjectType)
671      {
672         CHECKPOINT;
673         return(STATUS_OBJECT_TYPE_MISMATCH);
674      }
675    
676    if (ObjectHeader->ObjectType == PsProcessType)
677      {
678         DPRINT("Reference from %x\n", ((PULONG)&Handle)[-1]);
679      }
680    
681    if (AccessMode == UserMode)
682      {
683         RtlMapGenericMask(&DesiredAccess, ObjectHeader->ObjectType->Mapping);
684
685         if (!(GrantedAccess & DesiredAccess) &&
686             !((~GrantedAccess) & DesiredAccess))
687           {
688              CHECKPOINT;
689              return(STATUS_ACCESS_DENIED);
690           }
691      }
692    
693    *Object = ObjectBody;
694    
695    CHECKPOINT;
696    return(STATUS_SUCCESS);
697 }
698
699 /**********************************************************************
700  * NAME                                                 EXPORTED
701  *      NtClose
702  *      
703  * DESCRIPTION
704  *      Closes a handle reference to an object.
705  *      
706  * ARGUMENTS
707  *      Handle
708  *              Handle to close.
709  *              
710  * RETURN VALUE
711  *      Status.
712  */
713 NTSTATUS STDCALL NtClose(HANDLE Handle)
714 {
715    PVOID                ObjectBody;
716    POBJECT_HEADER       Header;
717    
718    assert_irql(PASSIVE_LEVEL);
719    
720    DPRINT("NtClose(Handle %x)\n",Handle);
721    
722    ObjectBody = ObDeleteHandle(PsGetCurrentProcess(), Handle);
723    if (ObjectBody == NULL)
724      {
725         return(STATUS_INVALID_HANDLE);
726      }
727    
728    Header = BODY_TO_HEADER(ObjectBody);
729    
730    DPRINT("Dereferencing %x\n", ObjectBody);
731    ObDereferenceObject(ObjectBody);
732    
733    return(STATUS_SUCCESS);
734 }
735
736 #ifndef LIBCAPTIVE
737
738 NTSTATUS STDCALL
739 ObInsertObject(PVOID Object,
740                PACCESS_STATE PassedAccessState,
741                ACCESS_MASK DesiredAccess,
742                ULONG AdditionalReferences,
743                PVOID* ReferencedObject,
744                PHANDLE Handle)
745 {
746   return(ObCreateHandle(PsGetCurrentProcess(),
747                         Object,
748                         DesiredAccess,
749                         FALSE,
750                         Handle));
751 }
752
753 #endif /* LIBCAPTIVE */
754
755 /* EOF */