8d3b679fe7dedd7dc277cc3de2dfd11a5aa5882e
[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 NTSTATUS
109 ObDuplicateObject(PEPROCESS SourceProcess,
110                   PEPROCESS TargetProcess,
111                   HANDLE SourceHandle,
112                   PHANDLE TargetHandle,
113                   ACCESS_MASK DesiredAccess,
114                   BOOLEAN InheritHandle,
115                   ULONG Options)
116 {
117   KIRQL oldIrql;
118   PHANDLE_REP SourceHandleRep;
119   PVOID ObjectBody;
120
121   KeAcquireSpinLock(&SourceProcess->HandleTable.ListLock, &oldIrql);
122   SourceHandleRep = ObpGetObjectByHandle(&SourceProcess->HandleTable,
123                                          SourceHandle);
124   if (SourceHandleRep == NULL)
125     {
126       KeReleaseSpinLock(&SourceProcess->HandleTable.ListLock, oldIrql);
127       return(STATUS_INVALID_HANDLE);
128     }
129   ObjectBody = SourceHandleRep->ObjectBody;
130   ObReferenceObjectByPointer(ObjectBody,
131                              0,
132                              NULL,
133                              UserMode);
134   
135   if (Options & DUPLICATE_SAME_ACCESS)
136     {
137       DesiredAccess = SourceHandleRep->GrantedAccess;
138     }
139   
140   KeReleaseSpinLock(&SourceProcess->HandleTable.ListLock, oldIrql);
141   ObCreateHandle(TargetProcess,
142                  ObjectBody,
143                  DesiredAccess,
144                  InheritHandle,
145                  TargetHandle);
146   
147   if (Options & DUPLICATE_CLOSE_SOURCE)
148     {
149       ZwClose(SourceHandle);
150     }
151   
152   ObDereferenceObject(ObjectBody);
153   return(STATUS_SUCCESS);
154 }
155
156 NTSTATUS STDCALL 
157 NtDuplicateObject (IN   HANDLE          SourceProcessHandle,
158                    IN   HANDLE          SourceHandle,
159                    IN   HANDLE          TargetProcessHandle,
160                    OUT  PHANDLE         UnsafeTargetHandle,
161                    IN   ACCESS_MASK     DesiredAccess,
162                    IN   BOOLEAN         InheritHandle,
163                    ULONG                Options)
164 /*
165  * FUNCTION: Copies a handle from one process space to another
166  * ARGUMENTS:
167  *         SourceProcessHandle = The source process owning the handle. The 
168  *                               source process should have opened
169  *                               the SourceHandle with PROCESS_DUP_HANDLE 
170  *                               access.
171  *         SourceHandle = The handle to the object.
172  *         TargetProcessHandle = The destination process owning the handle 
173  *         TargetHandle (OUT) = Caller should supply storage for the 
174  *                              duplicated handle. 
175  *         DesiredAccess = The desired access to the handle.
176  *         InheritHandle = Indicates wheter the new handle will be inheritable
177  *                         or not.
178  *         Options = Specifies special actions upon duplicating the handle. 
179  *                   Can be one of the values DUPLICATE_CLOSE_SOURCE | 
180  *                   DUPLICATE_SAME_ACCESS. DUPLICATE_CLOSE_SOURCE specifies 
181  *                   that the source handle should be closed after duplicating. 
182  *                   DUPLICATE_SAME_ACCESS specifies to ignore the 
183  *                   DesiredAccess paramter and just grant the same access to 
184  *                   the new handle.
185  * RETURNS: Status
186  * REMARKS: This function maps to the win32 DuplicateHandle.
187  */
188 {
189    PEPROCESS SourceProcess;
190    PEPROCESS TargetProcess;
191    PHANDLE_REP SourceHandleRep;
192    KIRQL oldIrql;
193    PVOID ObjectBody;
194    HANDLE TargetHandle;
195    NTSTATUS Status;
196    
197    ASSERT_IRQL(PASSIVE_LEVEL);
198    
199    Status = ObReferenceObjectByHandle(SourceProcessHandle,
200                                       PROCESS_DUP_HANDLE,
201                                       NULL,
202                                       UserMode,
203                                       (PVOID*)&SourceProcess,
204                                       NULL);
205    if (!NT_SUCCESS(Status))
206      {
207        return(Status);
208      }
209    Status = ObReferenceObjectByHandle(TargetProcessHandle,
210                                       PROCESS_DUP_HANDLE,
211                                       NULL,
212                                       UserMode,
213                                       (PVOID*)&TargetProcess,
214                                       NULL);
215    if (!NT_SUCCESS(Status))
216      {
217        ObDereferenceObject(SourceProcess);
218        return(Status);
219      }
220    KeAcquireSpinLock(&SourceProcess->HandleTable.ListLock, &oldIrql);
221    SourceHandleRep = ObpGetObjectByHandle(&SourceProcess->HandleTable,
222                                           SourceHandle);
223    if (SourceHandleRep == NULL)
224      {
225         KeReleaseSpinLock(&SourceProcess->HandleTable.ListLock, oldIrql);
226         ObDereferenceObject(SourceProcess);
227         ObDereferenceObject(TargetProcess);
228         return(STATUS_INVALID_HANDLE);
229      }
230    ObjectBody = SourceHandleRep->ObjectBody;
231    ObReferenceObjectByPointer(ObjectBody,
232                               0,
233                               NULL,
234                               UserMode);
235    
236    if (Options & DUPLICATE_SAME_ACCESS)
237      {
238         DesiredAccess = SourceHandleRep->GrantedAccess;
239      }
240    
241    KeReleaseSpinLock(&SourceProcess->HandleTable.ListLock, oldIrql);
242    if (!SourceHandleRep->Inherit)
243      {
244        ObDereferenceObject(TargetProcess);
245        ObDereferenceObject(SourceProcess);
246        ObDereferenceObject(ObjectBody);
247        return STATUS_INVALID_HANDLE;
248      }
249    ObCreateHandle(TargetProcess,
250                   ObjectBody,
251                   DesiredAccess,
252                   InheritHandle,
253                   &TargetHandle);
254    
255    if (Options & DUPLICATE_CLOSE_SOURCE)
256      {
257         ZwClose(SourceHandle);
258      }
259    
260    ObDereferenceObject(TargetProcess);
261    ObDereferenceObject(SourceProcess);
262    ObDereferenceObject(ObjectBody);
263    
264    Status = MmCopyToCaller(UnsafeTargetHandle, &TargetHandle, sizeof(HANDLE));
265    if (!NT_SUCCESS(Status))
266      {
267        return(Status);
268      }
269
270    return(STATUS_SUCCESS);
271 }
272
273 VOID ObCloseAllHandles(PEPROCESS Process)
274 {
275    KIRQL oldIrql;
276    PHANDLE_TABLE HandleTable;
277    PLIST_ENTRY current_entry;
278    PHANDLE_BLOCK current;
279    ULONG i;
280    PVOID ObjectBody;
281    
282    DPRINT("ObCloseAllHandles(Process %x)\n", Process);
283    
284    HandleTable = &Process->HandleTable;
285    
286    KeAcquireSpinLock(&HandleTable->ListLock, &oldIrql);
287    
288    current_entry = HandleTable->ListHead.Flink;
289    
290    while (current_entry != &HandleTable->ListHead)
291      {
292         current = CONTAINING_RECORD(current_entry, HANDLE_BLOCK, entry);
293         
294         for (i = 0; i < HANDLE_BLOCK_ENTRIES; i++)
295           {
296              ObjectBody = current->handles[i].ObjectBody;
297              
298              if (ObjectBody != NULL)
299                {
300                   POBJECT_HEADER Header = BODY_TO_HEADER(ObjectBody);
301                   
302                   if (Header->ObjectType == PsProcessType ||
303                       Header->ObjectType == PsThreadType)
304                     {
305                        DPRINT("Deleting handle to %x\n", ObjectBody);
306                     }
307                   
308                   ObReferenceObjectByPointer(ObjectBody,
309                                              0,
310                                              NULL,
311                                              UserMode);
312                   InterlockedDecrement(&Header->HandleCount);
313                   current->handles[i].ObjectBody = NULL;
314                   
315                   KeReleaseSpinLock(&HandleTable->ListLock, oldIrql);
316                   KeDetachProcess();
317                   
318                   if ((Header->ObjectType != NULL) &&
319                       (Header->ObjectType->Close != NULL))
320                     {
321                        Header->ObjectType->Close(ObjectBody, 
322                                                  Header->HandleCount);
323                     }
324                   
325                   ObDereferenceObject(ObjectBody);
326                   KeAttachProcess(Process);
327                   KeAcquireSpinLock(&HandleTable->ListLock, &oldIrql);
328                   current_entry = &HandleTable->ListHead;
329                   break;
330                }
331           }
332         
333         current_entry = current_entry->Flink;
334      }
335    KeReleaseSpinLock(&HandleTable->ListLock, oldIrql);
336    DPRINT("ObCloseAllHandles() finished\n");
337    DPRINT("Type %x\n", BODY_TO_HEADER(Process)->ObjectType);
338 }
339
340 VOID ObDeleteHandleTable(PEPROCESS Process)
341 /*
342  * FUNCTION: Deletes the handle table associated with a process
343  */
344 {
345    PLIST_ENTRY current = NULL;
346    PHANDLE_TABLE HandleTable = NULL;
347    
348    ObCloseAllHandles(Process);
349    
350    HandleTable = &Process->HandleTable;
351    current = RemoveHeadList(&HandleTable->ListHead);
352    
353    while (current != &HandleTable->ListHead)
354      {
355         HANDLE_BLOCK* HandleBlock = CONTAINING_RECORD(current,
356                                                       HANDLE_BLOCK,
357                                                       entry);
358         DPRINT("Freeing %x\n", HandleBlock);
359         ExFreePool(HandleBlock);
360         
361         current = RemoveHeadList(&HandleTable->ListHead);
362      }
363 }
364
365
366 VOID ObCreateHandleTable(PEPROCESS Parent,
367                          BOOLEAN Inherit,
368                          PEPROCESS Process)
369 /*
370  * FUNCTION: Creates a handle table for a process
371  * ARGUMENTS:
372  *       Parent = Parent process (or NULL if this is the first process)
373  *       Inherit = True if the process should inherit its parent's handles
374  *       Process = Process whose handle table is to be created
375  */
376 {
377    PHANDLE_TABLE ParentHandleTable, HandleTable;
378    KIRQL oldIrql;
379    PLIST_ENTRY parent_current;
380    ULONG i;
381    PHANDLE_BLOCK current_block, new_block;   
382
383    DPRINT("ObCreateHandleTable(Parent %x, Inherit %d, Process %x)\n",
384           Parent,Inherit,Process);
385    
386    InitializeListHead(&(Process->HandleTable.ListHead));
387    KeInitializeSpinLock(&(Process->HandleTable.ListLock));
388    
389    if (Parent != NULL)
390      {
391         ParentHandleTable = &Parent->HandleTable;
392         HandleTable = &Process->HandleTable;
393
394         KeAcquireSpinLock(&Parent->HandleTable.ListLock, &oldIrql);
395         KeAcquireSpinLockAtDpcLevel(&Process->HandleTable.ListLock);
396         
397         parent_current = ParentHandleTable->ListHead.Flink;
398         
399         while (parent_current != &ParentHandleTable->ListHead)
400           {
401              current_block = CONTAINING_RECORD(parent_current,
402                                                HANDLE_BLOCK,
403                                                entry);
404              new_block = ExAllocatePoolWithTag(NonPagedPool,
405                                                sizeof(HANDLE_BLOCK),
406                                                TAG_HANDLE_TABLE);
407              if (new_block == NULL)
408              {
409                 break;
410              }
411              RtlZeroMemory(new_block, sizeof(HANDLE_BLOCK));
412
413              for (i=0; i<HANDLE_BLOCK_ENTRIES; i++)
414              {
415                 if (current_block->handles[i].ObjectBody)
416                 {
417                    if (current_block->handles[i].Inherit && Inherit)
418                    {
419                       new_block->handles[i].ObjectBody = 
420                         current_block->handles[i].ObjectBody;
421                       new_block->handles[i].GrantedAccess = 
422                         current_block->handles[i].GrantedAccess;
423                       new_block->handles[i].Inherit = TRUE;
424                       InterlockedIncrement(&(BODY_TO_HEADER(current_block->handles[i].ObjectBody)->HandleCount));
425                    }
426                 }
427              }
428              InsertTailList(&Process->HandleTable.ListHead, &new_block->entry);
429              parent_current = parent_current->Flink;
430           }
431         KeReleaseSpinLockFromDpcLevel(&Process->HandleTable.ListLock);
432         KeReleaseSpinLock(&Parent->HandleTable.ListLock, oldIrql);
433      }
434 }
435
436
437 PVOID ObDeleteHandle(PEPROCESS Process, HANDLE Handle)
438 {
439    PHANDLE_REP Rep;
440    PVOID ObjectBody;
441    KIRQL oldIrql;
442    PHANDLE_TABLE HandleTable;
443    POBJECT_HEADER Header;
444    
445    DPRINT("ObDeleteHandle(Handle %x)\n",Handle);
446    
447    HandleTable = &Process->HandleTable;
448    
449    KeAcquireSpinLock(&HandleTable->ListLock, &oldIrql);
450    
451    Rep = ObpGetObjectByHandle(HandleTable, Handle);
452    if (Rep == NULL)
453      {
454         KeReleaseSpinLock(&HandleTable->ListLock, oldIrql);     
455         return(NULL);
456      }
457    
458    ObjectBody = Rep->ObjectBody;
459    DPRINT("ObjectBody %x\n", ObjectBody);
460    if (ObjectBody != NULL)
461      {
462         Header = BODY_TO_HEADER(ObjectBody);
463         InterlockedDecrement(&(BODY_TO_HEADER(ObjectBody)->HandleCount));
464         ObReferenceObjectByPointer(ObjectBody,
465                                    0,
466                                    NULL,
467                                    UserMode);
468         Rep->ObjectBody = NULL;
469    
470         KeReleaseSpinLock(&HandleTable->ListLock, oldIrql);
471    
472         if ((Header->ObjectType != NULL) &&
473             (Header->ObjectType->Close != NULL))
474           {
475              Header->ObjectType->Close(ObjectBody, Header->HandleCount);
476           }
477      }
478    else
479      {
480         KeReleaseSpinLock(&HandleTable->ListLock, oldIrql);
481      }
482    
483    DPRINT("Finished ObDeleteHandle()\n");
484    return(ObjectBody);
485 }
486
487
488 NTSTATUS ObCreateHandle(PEPROCESS Process,
489                         PVOID ObjectBody,
490                         ACCESS_MASK GrantedAccess,
491                         BOOLEAN Inherit,
492                         PHANDLE HandleReturn)
493 /*
494  * FUNCTION: Add a handle referencing an object
495  * ARGUMENTS:
496  *         obj = Object body that the handle should refer to
497  * RETURNS: The created handle
498  * NOTE: The handle is valid only in the context of the current process
499  */
500 {
501    LIST_ENTRY* current;
502    unsigned int handle=1;
503    unsigned int i;
504    HANDLE_BLOCK* new_blk = NULL;
505    PHANDLE_TABLE HandleTable;
506    KIRQL oldlvl;
507
508    DPRINT("ObCreateHandle(Process %x, obj %x)\n",Process,ObjectBody);
509
510    assert(Process);
511
512    if (ObjectBody != NULL)
513      {
514         InterlockedIncrement(&(BODY_TO_HEADER(ObjectBody)->HandleCount));
515      }
516    HandleTable = &Process->HandleTable;
517    KeAcquireSpinLock(&HandleTable->ListLock, &oldlvl);
518    current = HandleTable->ListHead.Flink;
519    /*
520     * Scan through the currently allocated handle blocks looking for a free
521     * slot
522     */
523    while (current != (&HandleTable->ListHead))
524      {
525         HANDLE_BLOCK* blk = CONTAINING_RECORD(current,HANDLE_BLOCK,entry);
526
527         DPRINT("Current %x\n",current);
528
529         for (i=0;i<HANDLE_BLOCK_ENTRIES;i++)
530           {
531              DPRINT("Considering slot %d containing %x\n",i,blk->handles[i]);
532              if (blk->handles[i].ObjectBody==NULL)
533                {
534                   blk->handles[i].ObjectBody = ObjectBody;
535                   blk->handles[i].GrantedAccess = GrantedAccess;
536                   blk->handles[i].Inherit = Inherit;
537                   KeReleaseSpinLock(&HandleTable->ListLock, oldlvl);
538                   *HandleReturn = (HANDLE)((handle + i) << 2);
539                   return(STATUS_SUCCESS);
540                }
541           }
542         
543         handle = handle + HANDLE_BLOCK_ENTRIES;
544         current = current->Flink;
545      }
546
547    /*
548     * Add a new handle block to the end of the list
549     */
550    new_blk = 
551      (HANDLE_BLOCK *)ExAllocatePoolWithTag(NonPagedPool,sizeof(HANDLE_BLOCK),
552                                            TAG_HANDLE_TABLE);
553    if (!new_blk)
554     {
555       *HandleReturn = (PHANDLE)NULL;
556       return(STATUS_INSUFFICIENT_RESOURCES);
557     }
558    RtlZeroMemory(new_blk,sizeof(HANDLE_BLOCK));
559    InsertTailList(&(Process->HandleTable.ListHead),
560                   &new_blk->entry);
561    new_blk->handles[0].ObjectBody = ObjectBody;
562    new_blk->handles[0].GrantedAccess = GrantedAccess;
563    new_blk->handles[0].Inherit = Inherit;
564    KeReleaseSpinLock(&HandleTable->ListLock, oldlvl);
565    *HandleReturn = (HANDLE)(handle << 2);
566    return(STATUS_SUCCESS);
567 }
568
569
570 NTSTATUS STDCALL
571 ObReferenceObjectByHandle(HANDLE Handle,
572                           ACCESS_MASK DesiredAccess,
573                           POBJECT_TYPE ObjectType,
574                           KPROCESSOR_MODE AccessMode,
575                           PVOID* Object,
576                           POBJECT_HANDLE_INFORMATION HandleInformationPtr)
577 /*
578  * FUNCTION: Increments the reference count for an object and returns a 
579  * pointer to its body
580  * ARGUMENTS:
581  *         Handle = Handle for the object
582  *         DesiredAccess = Desired access to the object
583  *         ObjectType
584  *         AccessMode 
585  *         Object (OUT) = Points to the object body on return
586  *         HandleInformation (OUT) = Contains information about the handle 
587  *                                   on return
588  * RETURNS: Status
589  */
590 {
591    PHANDLE_REP HandleRep;
592    POBJECT_HEADER ObjectHeader;
593    KIRQL oldIrql;
594    PVOID ObjectBody;
595    ACCESS_MASK GrantedAccess;
596    
597    ASSERT_IRQL(PASSIVE_LEVEL);
598    
599    DPRINT("ObReferenceObjectByHandle(Handle %x, DesiredAccess %x, "
600            "ObjectType %x, AccessMode %d, Object %x)\n",Handle,DesiredAccess,
601            ObjectType,AccessMode,Object);
602
603    
604    /*
605     * Handle special handle names
606     */
607    if (Handle == NtCurrentProcess() && 
608        (ObjectType == PsProcessType || ObjectType == NULL))
609      {
610         DPRINT("Reference from %x\n", ((PULONG)&Handle)[-1]);
611         
612         ObReferenceObjectByPointer(PsGetCurrentProcess(),
613                                    PROCESS_ALL_ACCESS,
614                                    PsProcessType,
615                                    UserMode);
616         *Object = PsGetCurrentProcess();
617         DPRINT("Referencing current process %x\n", PsGetCurrentProcess());
618         return(STATUS_SUCCESS);
619      }
620    else if (Handle == NtCurrentProcess())
621      {
622         CHECKPOINT;
623         return(STATUS_OBJECT_TYPE_MISMATCH);
624      }
625    if (Handle == NtCurrentThread() && 
626        (ObjectType == PsThreadType || ObjectType == NULL))
627      {
628         ObReferenceObjectByPointer(PsGetCurrentThread(),
629                                    THREAD_ALL_ACCESS,
630                                    PsThreadType,
631                                    UserMode);
632         *Object = PsGetCurrentThread();
633         CHECKPOINT;
634         return(STATUS_SUCCESS);
635      }
636    else if (Handle == NtCurrentThread())
637      {
638         CHECKPOINT;
639         return(STATUS_OBJECT_TYPE_MISMATCH);
640      }
641    
642    KeAcquireSpinLock(&PsGetCurrentProcess()->HandleTable.ListLock,
643                      &oldIrql);
644    HandleRep = ObpGetObjectByHandle(&PsGetCurrentProcess()->HandleTable,
645                                     Handle);
646    if (HandleRep == NULL || HandleRep->ObjectBody == NULL)
647      {
648         KeReleaseSpinLock(&PsGetCurrentProcess()->HandleTable.ListLock,
649                           oldIrql);
650         return(STATUS_INVALID_HANDLE);
651      }
652    ObjectBody = HandleRep->ObjectBody;
653    DPRINT("ObjectBody %p\n",ObjectBody);
654    ObjectHeader = BODY_TO_HEADER(ObjectBody);
655    DPRINT("ObjectHeader->RefCount %lu\n",ObjectHeader->RefCount);
656    ObReferenceObjectByPointer(ObjectBody,
657                               0,
658                               NULL,
659                               UserMode);
660    GrantedAccess = HandleRep->GrantedAccess;
661    KeReleaseSpinLock(&PsGetCurrentProcess()->HandleTable.ListLock,
662                      oldIrql);
663    
664    ObjectHeader = BODY_TO_HEADER(ObjectBody);
665    DPRINT("ObjectHeader->RefCount %lu\n",ObjectHeader->RefCount);
666
667    if (ObjectType != NULL && ObjectType != ObjectHeader->ObjectType)
668      {
669         CHECKPOINT;
670         return(STATUS_OBJECT_TYPE_MISMATCH);
671      }
672    
673    if (ObjectHeader->ObjectType == PsProcessType)
674      {
675         DPRINT("Reference from %x\n", ((PULONG)&Handle)[-1]);
676      }
677    
678    if (AccessMode == UserMode)
679      {
680         RtlMapGenericMask(&DesiredAccess, ObjectHeader->ObjectType->Mapping);
681
682         if (!(GrantedAccess & DesiredAccess) &&
683             !((~GrantedAccess) & DesiredAccess))
684           {
685              CHECKPOINT;
686              return(STATUS_ACCESS_DENIED);
687           }
688      }
689    
690    *Object = ObjectBody;
691    
692    CHECKPOINT;
693    return(STATUS_SUCCESS);
694 }
695
696
697 /**********************************************************************
698  * NAME                                                 EXPORTED
699  *      NtClose
700  *      
701  * DESCRIPTION
702  *      Closes a handle reference to an object.
703  *      
704  * ARGUMENTS
705  *      Handle
706  *              Handle to close.
707  *              
708  * RETURN VALUE
709  *      Status.
710  */
711 NTSTATUS STDCALL NtClose(HANDLE Handle)
712 {
713    PVOID                ObjectBody;
714    POBJECT_HEADER       Header;
715    
716    assert_irql(PASSIVE_LEVEL);
717    
718    DPRINT("NtClose(Handle %x)\n",Handle);
719    
720    ObjectBody = ObDeleteHandle(PsGetCurrentProcess(), Handle);
721    if (ObjectBody == NULL)
722      {
723         return(STATUS_INVALID_HANDLE);
724      }
725    
726    Header = BODY_TO_HEADER(ObjectBody);
727    
728    DPRINT("Dereferencing %x\n", ObjectBody);
729    ObDereferenceObject(ObjectBody);
730    
731    return(STATUS_SUCCESS);
732 }
733
734 NTSTATUS STDCALL
735 ObInsertObject(PVOID Object,
736                PACCESS_STATE PassedAccessState,
737                ACCESS_MASK DesiredAccess,
738                ULONG AdditionalReferences,
739                PVOID* ReferencedObject,
740                PHANDLE Handle)
741 {
742   return(ObCreateHandle(PsGetCurrentProcess(),
743                         Object,
744                         DesiredAccess,
745                         FALSE,
746                         Handle));
747 }
748
749 /* EOF */