ReactOS 0.4.16-dev-197-g92996da
handle.c File Reference
#include "consrv.h"
#include <win/console.h>
#include <debug.h>
Include dependency graph for handle.c:

Go to the source code of this file.

Classes

struct  _CONSOLE_IO_HANDLE
 

Macros

#define NDEBUG
 
#define IO_HANDLES_INCREMENT   2 * 3
 

Typedefs

typedef struct _CONSOLE_IO_HANDLE CONSOLE_IO_HANDLE
 
typedef struct _CONSOLE_IO_HANDLEPCONSOLE_IO_HANDLE
 

Functions

static LONG AdjustHandleCounts (IN PCONSOLE_IO_HANDLE Handle, IN LONG Change)
 
static VOID ConSrvCloseHandle (IN PCONSOLE_IO_HANDLE Handle)
 
NTSTATUS ConSrvInheritHandlesTable (IN PCONSOLE_PROCESS_DATA SourceProcessData, IN PCONSOLE_PROCESS_DATA TargetProcessData)
 
VOID ConSrvFreeHandlesTable (IN PCONSOLE_PROCESS_DATA ProcessData)
 
VOID ConSrvInitObject (IN OUT PCONSOLE_IO_OBJECT Object, IN CONSOLE_IO_OBJECT_TYPE Type, IN PCONSOLE Console)
 
NTSTATUS ConSrvInsertObject (IN PCONSOLE_PROCESS_DATA ProcessData, OUT PHANDLE Handle, IN PCONSOLE_IO_OBJECT Object, IN ULONG Access, IN BOOLEAN Inheritable, IN ULONG ShareMode)
 
NTSTATUS ConSrvRemoveObject (IN PCONSOLE_PROCESS_DATA ProcessData, IN HANDLE Handle)
 
NTSTATUS ConSrvGetObject (IN PCONSOLE_PROCESS_DATA ProcessData, IN HANDLE Handle, OUT PCONSOLE_IO_OBJECT *Object, OUT PVOID *Entry OPTIONAL, IN ULONG Access, IN BOOLEAN LockConsole, IN CONSOLE_IO_OBJECT_TYPE Type)
 
VOID ConSrvReleaseObject (IN PCONSOLE_IO_OBJECT Object, IN BOOLEAN IsConsoleLocked)
 
 CON_API (SrvOpenConsole, CONSOLE_OPENCONSOLE, OpenConsoleRequest)
 
 CON_API (SrvDuplicateHandle, CONSOLE_DUPLICATEHANDLE, DuplicateHandleRequest)
 
 CON_API (SrvGetHandleInformation, CONSOLE_GETHANDLEINFO, GetHandleInfoRequest)
 
 CON_API (SrvSetHandleInformation, CONSOLE_SETHANDLEINFO, SetHandleInfoRequest)
 
 CON_API (SrvCloseHandle, CONSOLE_CLOSEHANDLE, CloseHandleRequest)
 
 CON_API (SrvVerifyConsoleIoHandle, CONSOLE_VERIFYHANDLE, VerifyHandleRequest)
 

Macro Definition Documentation

◆ IO_HANDLES_INCREMENT

#define IO_HANDLES_INCREMENT   2 * 3

◆ NDEBUG

#define NDEBUG

Definition at line 17 of file handle.c.

Typedef Documentation

◆ CONSOLE_IO_HANDLE

◆ PCONSOLE_IO_HANDLE

Function Documentation

◆ AdjustHandleCounts()

static LONG AdjustHandleCounts ( IN PCONSOLE_IO_HANDLE  Handle,
IN LONG  Change 
)
static

Definition at line 35 of file handle.c.

37{
39
40 DPRINT("AdjustHandleCounts(0x%p, %d), Object = 0x%p\n",
41 Handle, Change, Object);
42 DPRINT("\tAdjustHandleCounts(0x%p, %d), Object = 0x%p, Object->ReferenceCount = %d, Object->Type = %lu\n",
43 Handle, Change, Object, Object->ReferenceCount, Object->Type);
44
45 if (Handle->Access & GENERIC_READ) Object->AccessRead += Change;
46 if (Handle->Access & GENERIC_WRITE) Object->AccessWrite += Change;
47 if (!(Handle->ShareMode & FILE_SHARE_READ)) Object->ExclusiveRead += Change;
48 if (!(Handle->ShareMode & FILE_SHARE_WRITE)) Object->ExclusiveWrite += Change;
49
50 Object->ReferenceCount += Change;
51
52 return Object->ReferenceCount;
53}
#define GENERIC_READ
Definition: compat.h:135
#define FILE_SHARE_READ
Definition: compat.h:136
ULONG Handle
Definition: gdb_input.c:15
#define FILE_SHARE_WRITE
Definition: nt_native.h:681
#define GENERIC_WRITE
Definition: nt_native.h:90
#define DPRINT
Definition: sndvol32.h:73
_Must_inspect_result_ _In_ WDFCOLLECTION _In_ WDFOBJECT Object

Referenced by ConSrvCloseHandle(), ConSrvInheritHandlesTable(), and ConSrvInsertObject().

◆ CON_API() [1/6]

CON_API ( SrvCloseHandle  ,
CONSOLE_CLOSEHANDLE  ,
CloseHandleRequest   
)

Definition at line 587 of file handle.c.

589{
590 return ConSrvRemoveObject(ProcessData, CloseHandleRequest->Handle);
591}
NTSTATUS ConSrvRemoveObject(IN PCONSOLE_PROCESS_DATA ProcessData, IN HANDLE Handle)
Definition: handle.c:292

◆ CON_API() [2/6]

CON_API ( SrvDuplicateHandle  ,
CONSOLE_DUPLICATEHANDLE  ,
DuplicateHandleRequest   
)

!IsConsoleHandle(SourceHandle) ||

Definition at line 446 of file handle.c.

448{
450 HANDLE SourceHandle = DuplicateHandleRequest->SourceHandle;
454
455 DuplicateHandleRequest->TargetHandle = INVALID_HANDLE_VALUE;
456
457 RtlEnterCriticalSection(&ProcessData->HandleTableLock);
458
459 // ASSERT( (ProcessData->HandleTable == NULL && ProcessData->HandleTableSize == 0) ||
460 // (ProcessData->HandleTable != NULL && ProcessData->HandleTableSize != 0) );
461
462 if (
463 Index >= ProcessData->HandleTableSize ||
464 (Entry = &ProcessData->HandleTable[Index])->Object == NULL)
465 {
466 DPRINT1("Couldn't duplicate invalid handle 0x%p\n", SourceHandle);
468 goto Quit;
469 }
470
471 if (DuplicateHandleRequest->Options & DUPLICATE_SAME_ACCESS)
472 {
473 DesiredAccess = Entry->Access;
474 }
475 else
476 {
477 DesiredAccess = DuplicateHandleRequest->DesiredAccess;
478 /* Make sure the source handle has all the desired flags */
479 if ((Entry->Access & DesiredAccess) == 0)
480 {
481 DPRINT1("Handle 0x%p only has access %X; requested %X\n",
484 goto Quit;
485 }
486 }
487
488 /* Insert the new handle inside the process handles table */
489 Status = ConSrvInsertObject(ProcessData,
490 &DuplicateHandleRequest->TargetHandle,
491 Entry->Object,
493 DuplicateHandleRequest->InheritHandle,
494 Entry->ShareMode);
495 if (NT_SUCCESS(Status) &&
496 (DuplicateHandleRequest->Options & DUPLICATE_CLOSE_SOURCE))
497 {
498 /* Close the original handle if needed */
500 }
501
502Quit:
503 RtlLeaveCriticalSection(&ProcessData->HandleTableLock);
504
505 return Status;
506}
LONG NTSTATUS
Definition: precomp.h:26
#define DPRINT1
Definition: precomp.h:8
#define HandleToULong(h)
Definition: basetsd.h:95
#define STATUS_INVALID_HANDLE
Definition: d3dkmdt.h:40
#define NULL
Definition: types.h:112
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:33
#define INVALID_HANDLE_VALUE
Definition: compat.h:731
unsigned long DWORD
Definition: ntddk_ex.h:95
Status
Definition: gdiplustypes.h:25
_In_ HANDLE SourceHandle
Definition: obfuncs.h:429
NTSYSAPI NTSTATUS NTAPI RtlEnterCriticalSection(_In_ PRTL_CRITICAL_SECTION CriticalSection)
NTSYSAPI NTSTATUS NTAPI RtlLeaveCriticalSection(_In_ PRTL_CRITICAL_SECTION CriticalSection)
base of all file and directory entries
Definition: entries.h:83
uint32_t ULONG
Definition: typedefs.h:59
#define STATUS_INVALID_PARAMETER
Definition: udferr_usr.h:135
_In_ WDFCOLLECTION _In_ ULONG Index
_Must_inspect_result_ _In_ WDFDEVICE _In_ ULONG _In_ ACCESS_MASK DesiredAccess
Definition: wdfdevice.h:2658
NTSTATUS ConSrvInsertObject(IN PCONSOLE_PROCESS_DATA ProcessData, OUT PHANDLE Handle, IN PCONSOLE_IO_OBJECT Object, IN ULONG Access, IN BOOLEAN Inheritable, IN ULONG ShareMode)
Definition: handle.c:227
static VOID ConSrvCloseHandle(IN PCONSOLE_IO_HANDLE Handle)
Definition: handle.c:56
#define DUPLICATE_SAME_ACCESS
#define DUPLICATE_CLOSE_SOURCE

◆ CON_API() [3/6]

CON_API ( SrvGetHandleInformation  ,
CONSOLE_GETHANDLEINFO  ,
GetHandleInfoRequest   
)

Definition at line 509 of file handle.c.

511{
513 HANDLE Handle = GetHandleInfoRequest->Handle;
516
517 RtlEnterCriticalSection(&ProcessData->HandleTableLock);
518
519 ASSERT(ProcessData->HandleTable);
520 // ASSERT( (ProcessData->HandleTable == NULL && ProcessData->HandleTableSize == 0) ||
521 // (ProcessData->HandleTable != NULL && ProcessData->HandleTableSize != 0) );
522
523 if (!IsConsoleHandle(Handle) ||
524 Index >= ProcessData->HandleTableSize ||
525 (Entry = &ProcessData->HandleTable[Index])->Object == NULL)
526 {
528 goto Quit;
529 }
530
531 /*
532 * Retrieve the handle information flags. The console server
533 * doesn't support HANDLE_FLAG_PROTECT_FROM_CLOSE.
534 */
535 GetHandleInfoRequest->Flags = 0;
536 if (Entry->Inheritable) GetHandleInfoRequest->Flags |= HANDLE_FLAG_INHERIT;
537
539
540Quit:
541 RtlLeaveCriticalSection(&ProcessData->HandleTableLock);
542
543 return Status;
544}
#define ASSERT(a)
Definition: mode.c:44
#define IsConsoleHandle(h)
Definition: console.h:14
#define STATUS_SUCCESS
Definition: shellext.h:65
#define HANDLE_FLAG_INHERIT
Definition: winbase.h:289

◆ CON_API() [4/6]

CON_API ( SrvOpenConsole  ,
CONSOLE_OPENCONSOLE  ,
OpenConsoleRequest   
)

Definition at line 393 of file handle.c.

395{
396 /*
397 * This API opens a handle to either the input buffer or to
398 * a screen-buffer of the console of the current process.
399 */
400
402 DWORD DesiredAccess = OpenConsoleRequest->DesiredAccess;
403 DWORD ShareMode = OpenConsoleRequest->ShareMode;
405
406 OpenConsoleRequest->Handle = INVALID_HANDLE_VALUE;
407
408 RtlEnterCriticalSection(&ProcessData->HandleTableLock);
409
410 /*
411 * Open a handle to either the active screen buffer or the input buffer.
412 */
413 if (OpenConsoleRequest->HandleType == HANDLE_OUTPUT)
414 {
415 Object = &Console->ActiveBuffer->Header;
416 }
417 else // HANDLE_INPUT
418 {
419 Object = &Console->InputBuffer.Header;
420 }
421
422 if (((DesiredAccess & GENERIC_READ) && Object->ExclusiveRead != 0) ||
423 ((DesiredAccess & GENERIC_WRITE) && Object->ExclusiveWrite != 0) ||
424 (!(ShareMode & FILE_SHARE_READ) && Object->AccessRead != 0) ||
425 (!(ShareMode & FILE_SHARE_WRITE) && Object->AccessWrite != 0))
426 {
427 DPRINT1("Sharing violation\n");
429 }
430 else
431 {
432 Status = ConSrvInsertObject(ProcessData,
433 &OpenConsoleRequest->Handle,
434 Object,
436 OpenConsoleRequest->InheritHandle,
437 ShareMode);
438 }
439
440 RtlLeaveCriticalSection(&ProcessData->HandleTableLock);
441
442 return Status;
443}
CConsole Console
@ HANDLE_OUTPUT
Definition: conmsg.h:668
#define STATUS_SHARING_VIOLATION
Definition: udferr_usr.h:154

◆ CON_API() [5/6]

CON_API ( SrvSetHandleInformation  ,
CONSOLE_SETHANDLEINFO  ,
SetHandleInfoRequest   
)

Definition at line 547 of file handle.c.

549{
551 HANDLE Handle = SetHandleInfoRequest->Handle;
554
555 RtlEnterCriticalSection(&ProcessData->HandleTableLock);
556
557 ASSERT(ProcessData->HandleTable);
558 // ASSERT( (ProcessData->HandleTable == NULL && ProcessData->HandleTableSize == 0) ||
559 // (ProcessData->HandleTable != NULL && ProcessData->HandleTableSize != 0) );
560
561 if (!IsConsoleHandle(Handle) ||
562 Index >= ProcessData->HandleTableSize ||
563 (Entry = &ProcessData->HandleTable[Index])->Object == NULL)
564 {
566 goto Quit;
567 }
568
569 /*
570 * Modify the handle information flags. The console server
571 * doesn't support HANDLE_FLAG_PROTECT_FROM_CLOSE.
572 */
573 if (SetHandleInfoRequest->Mask & HANDLE_FLAG_INHERIT)
574 {
575 Entry->Inheritable = ((SetHandleInfoRequest->Flags & HANDLE_FLAG_INHERIT) != 0);
576 }
577
579
580Quit:
581 RtlLeaveCriticalSection(&ProcessData->HandleTableLock);
582
583 return Status;
584}

◆ CON_API() [6/6]

CON_API ( SrvVerifyConsoleIoHandle  ,
CONSOLE_VERIFYHANDLE  ,
VerifyHandleRequest   
)

Definition at line 594 of file handle.c.

596{
597 HANDLE IoHandle = VerifyHandleRequest->Handle;
598 ULONG Index = HandleToULong(IoHandle) >> 2;
599
600 VerifyHandleRequest->IsValid = FALSE;
601
602 RtlEnterCriticalSection(&ProcessData->HandleTableLock);
603
604 // ASSERT( (ProcessData->HandleTable == NULL && ProcessData->HandleTableSize == 0) ||
605 // (ProcessData->HandleTable != NULL && ProcessData->HandleTableSize != 0) );
606
607 if (!IsConsoleHandle(IoHandle) ||
608 Index >= ProcessData->HandleTableSize ||
609 ProcessData->HandleTable[Index].Object == NULL)
610 {
611 DPRINT("SrvVerifyConsoleIoHandle failed\n");
612 }
613 else
614 {
615 VerifyHandleRequest->IsValid = TRUE;
616 }
617
618 RtlLeaveCriticalSection(&ProcessData->HandleTableLock);
619
620 return STATUS_SUCCESS;
621}
#define TRUE
Definition: types.h:120
#define FALSE
Definition: types.h:117

◆ ConSrvCloseHandle()

static VOID ConSrvCloseHandle ( IN PCONSOLE_IO_HANDLE  Handle)
static

Definition at line 56 of file handle.c.

57{
59 if (Object != NULL)
60 {
61 /*
62 * If this is a input handle, notify and dereference
63 * all the waits related to this handle.
64 */
65 if (Object->Type == INPUT_BUFFER)
66 {
67 // PCONSOLE_INPUT_BUFFER InputBuffer = (PCONSOLE_INPUT_BUFFER)Object;
69
70 /*
71 * Wake up all the writing waiters related to this handle for this
72 * input buffer, if any, then dereference them and purge them all
73 * from the list.
74 * To select them amongst all the waiters for this input buffer,
75 * pass the handle pointer to the waiters, then they will check
76 * whether or not they are related to this handle and if so, they
77 * return.
78 */
79 CsrNotifyWait(&Console->ReadWaitQueue,
80 TRUE,
81 NULL,
82 (PVOID)Handle);
83 if (!IsListEmpty(&Console->ReadWaitQueue))
84 {
85 CsrDereferenceWait(&Console->ReadWaitQueue);
86 }
87 }
88
89 /* If the last handle to a screen buffer is closed, delete it... */
90 if (AdjustHandleCounts(Handle, -1) == 0)
91 {
92 if (Object->Type == TEXTMODE_BUFFER || Object->Type == GRAPHICS_BUFFER)
93 {
95 /* ...unless it's the only buffer left. Windows allows deletion
96 * even of the last buffer, but having to deal with a lack of
97 * any active buffer might be error-prone. */
98 if (Buffer->ListEntry.Flink != Buffer->ListEntry.Blink)
100 }
101 else if (Object->Type == INPUT_BUFFER)
102 {
103 DPRINT("Closing the input buffer\n");
104 }
105 else
106 {
107 DPRINT1("Invalid object type %d\n", Object->Type);
108 }
109 }
110
111 /* Invalidate (zero-out) this handle entry */
112 // Handle->Object = NULL;
113 // RtlZeroMemory(Handle, sizeof(*Handle));
114 }
115 RtlZeroMemory(Handle, sizeof(*Handle)); // Be sure the whole entry is invalidated.
116}
Definition: bufpool.h:45
VOID NTAPI ConDrvDeleteScreenBuffer(PCONSOLE_SCREEN_BUFFER Buffer)
Definition: conoutput.c:130
struct _CONSRV_CONSOLE * PCONSRV_CONSOLE
VOID NTAPI CsrDereferenceWait(IN PLIST_ENTRY WaitList)
Definition: wait.c:266
BOOLEAN NTAPI CsrNotifyWait(IN PLIST_ENTRY WaitList, IN BOOLEAN NotifyAll, IN PVOID WaitArgument1, IN PVOID WaitArgument2)
Definition: wait.c:388
#define IsListEmpty(ListHead)
Definition: env_spec_w32.h:954
#define TEXTMODE_BUFFER
Definition: pccons.c:21
#define RtlZeroMemory(Destination, Length)
Definition: typedefs.h:262
static LONG AdjustHandleCounts(IN PCONSOLE_IO_HANDLE Handle, IN LONG Change)
Definition: handle.c:35
@ INPUT_BUFFER
Definition: conio.h:28
@ GRAPHICS_BUFFER
Definition: conio.h:26
struct _CONSOLE_SCREEN_BUFFER * PCONSOLE_SCREEN_BUFFER

Referenced by CON_API(), ConSrvFreeHandlesTable(), and ConSrvRemoveObject().

◆ ConSrvFreeHandlesTable()

VOID ConSrvFreeHandlesTable ( IN PCONSOLE_PROCESS_DATA  ProcessData)

Definition at line 172 of file handle.c.

173{
174 RtlEnterCriticalSection(&ProcessData->HandleTableLock);
175
176 if (ProcessData->HandleTable != NULL)
177 {
178 ULONG i;
179
180 /*
181 * ProcessData->ConsoleHandle is NULL (and the assertion fails) when
182 * ConSrvFreeHandlesTable is called in ConSrvConnect during the
183 * allocation of a new console.
184 */
185 // ASSERT(ProcessData->ConsoleHandle);
186 if (ProcessData->ConsoleHandle != NULL)
187 {
188 /* Close all the console handles */
189 for (i = 0; i < ProcessData->HandleTableSize; i++)
190 {
191 ConSrvCloseHandle(&ProcessData->HandleTable[i]);
192 }
193 }
194 /* Free the handles table memory */
195 ConsoleFreeHeap(ProcessData->HandleTable);
196 ProcessData->HandleTable = NULL;
197 }
198
199 ProcessData->HandleTableSize = 0;
200
201 RtlLeaveCriticalSection(&ProcessData->HandleTableLock);
202}
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint i
Definition: glfuncs.h:248
#define ConsoleFreeHeap(HeapBase)
Definition: heap.h:15

Referenced by ConSrvAllocateConsole(), ConSrvInheritConsole(), ConSrvInitProcessHandles(), and ConSrvRemoveConsole().

◆ ConSrvGetObject()

NTSTATUS ConSrvGetObject ( IN PCONSOLE_PROCESS_DATA  ProcessData,
IN HANDLE  Handle,
OUT PCONSOLE_IO_OBJECT Object,
OUT PVOID *Entry  OPTIONAL,
IN ULONG  Access,
IN BOOLEAN  LockConsole,
IN CONSOLE_IO_OBJECT_TYPE  Type 
)

Definition at line 318 of file handle.c.

325{
326 // NTSTATUS Status;
328 PCONSOLE_IO_HANDLE HandleEntry = NULL;
329 PCONSOLE_IO_OBJECT ObjectEntry = NULL;
330 // PCONSRV_CONSOLE ObjectConsole;
331
332 ASSERT(Object);
333 if (Entry) *Entry = NULL;
334
335 DPRINT("ConSrvGetObject -- Object: 0x%x, Handle: 0x%x\n", Object, Handle);
336
337 RtlEnterCriticalSection(&ProcessData->HandleTableLock);
338
339 if ( IsConsoleHandle(Handle) &&
340 Index < ProcessData->HandleTableSize )
341 {
342 HandleEntry = &ProcessData->HandleTable[Index];
343 ObjectEntry = HandleEntry->Object;
344 }
345
346 if ( HandleEntry == NULL ||
347 ObjectEntry == NULL ||
348 (HandleEntry->Access & Access) == 0 ||
349 /*(Type != 0 && ObjectEntry->Type != Type)*/
350 (Type != 0 && (ObjectEntry->Type & Type) == 0) )
351 {
352 DPRINT("ConSrvGetObject -- Invalid handle 0x%x of type %lu with access %lu ; retrieved object 0x%x (handle 0x%x) of type %lu with access %lu\n",
353 Handle, Type, Access, ObjectEntry, HandleEntry, (ObjectEntry ? ObjectEntry->Type : 0), (HandleEntry ? HandleEntry->Access : 0));
354
355 RtlLeaveCriticalSection(&ProcessData->HandleTableLock);
357 }
358
359 RtlLeaveCriticalSection(&ProcessData->HandleTableLock);
360
361 // Status = ConSrvGetConsole(ProcessData, &ObjectConsole, LockConsole);
362 // if (NT_SUCCESS(Status))
363 if (ConDrvValidateConsoleUnsafe(ObjectEntry->Console, CONSOLE_RUNNING, LockConsole))
364 {
365 _InterlockedIncrement(&ObjectEntry->Console->ReferenceCount);
366
367 /* Return the objects to the caller */
368 *Object = ObjectEntry;
369 if (Entry) *Entry = HandleEntry;
370
371 // RtlLeaveCriticalSection(&ProcessData->HandleTableLock);
372 return STATUS_SUCCESS;
373 }
374 else
375 {
376 // RtlLeaveCriticalSection(&ProcessData->HandleTableLock);
378 }
379}
Type
Definition: Type.h:7
long __cdecl _InterlockedIncrement(_Interlocked_operand_ long volatile *_Addend)
PCONSOLE_IO_OBJECT Object
Definition: handle.c:25
struct _CONSOLE * Console
Definition: conio.h:36
CONSOLE_IO_OBJECT_TYPE Type
Definition: conio.h:34
BOOLEAN NTAPI ConDrvValidateConsoleUnsafe(IN PCONSOLE Console, IN CONSOLE_STATE ExpectedState, IN BOOLEAN LockConsole)
Definition: console.c:36
@ CONSOLE_RUNNING
Definition: conio.h:283

Referenced by CON_API().

◆ ConSrvInheritHandlesTable()

NTSTATUS ConSrvInheritHandlesTable ( IN PCONSOLE_PROCESS_DATA  SourceProcessData,
IN PCONSOLE_PROCESS_DATA  TargetProcessData 
)

Definition at line 120 of file handle.c.

122{
124 ULONG i, j;
125
126 RtlEnterCriticalSection(&SourceProcessData->HandleTableLock);
127
128 /* Inherit a handles table only if there is no already */
129 if (TargetProcessData->HandleTable != NULL /* || TargetProcessData->HandleTableSize != 0 */)
130 {
132 goto Quit;
133 }
134
135 /* Allocate a new handle table for the child process */
136 TargetProcessData->HandleTable = ConsoleAllocHeap(HEAP_ZERO_MEMORY,
137 SourceProcessData->HandleTableSize
138 * sizeof(CONSOLE_IO_HANDLE));
139 if (TargetProcessData->HandleTable == NULL)
140 {
142 goto Quit;
143 }
144
145 TargetProcessData->HandleTableSize = SourceProcessData->HandleTableSize;
146
147 /*
148 * Parse the parent process' handles table and, for each handle,
149 * do a copy of it and reference it, if the handle is inheritable.
150 */
151 for (i = 0, j = 0; i < SourceProcessData->HandleTableSize; i++)
152 {
153 if (SourceProcessData->HandleTable[i].Object != NULL &&
154 SourceProcessData->HandleTable[i].Inheritable)
155 {
156 /*
157 * Copy the handle data and increment the reference count of the
158 * pointed object (via the call to ConSrvCreateHandleEntry == AdjustHandleCounts).
159 */
160 TargetProcessData->HandleTable[j] = SourceProcessData->HandleTable[i];
161 AdjustHandleCounts(&TargetProcessData->HandleTable[j], +1);
162 ++j;
163 }
164 }
165
166Quit:
167 RtlLeaveCriticalSection(&SourceProcessData->HandleTableLock);
168 return Status;
169}
#define STATUS_NO_MEMORY
Definition: d3dkmdt.h:51
#define HEAP_ZERO_MEMORY
Definition: compat.h:134
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint GLint GLint j
Definition: glfuncs.h:250
#define STATUS_UNSUCCESSFUL
Definition: udferr_usr.h:132
#define ConsoleAllocHeap(Flags, Size)
Definition: heap.h:14

Referenced by ConSrvNewProcess().

◆ ConSrvInitObject()

VOID ConSrvInitObject ( IN OUT PCONSOLE_IO_OBJECT  Object,
IN CONSOLE_IO_OBJECT_TYPE  Type,
IN PCONSOLE  Console 
)

Definition at line 211 of file handle.c.

214{
215 ASSERT(Object);
216 // if (!Object) return;
217
218 Object->Type = Type;
219 Object->Console = Console;
220 Object->ReferenceCount = 0;
221
222 Object->AccessRead = Object->AccessWrite = 0;
223 Object->ExclusiveRead = Object->ExclusiveWrite = 0;
224}

Referenced by ConDrvInitInputBuffer(), and CONSOLE_SCREEN_BUFFER_Initialize().

◆ ConSrvInsertObject()

NTSTATUS ConSrvInsertObject ( IN PCONSOLE_PROCESS_DATA  ProcessData,
OUT PHANDLE  Handle,
IN PCONSOLE_IO_OBJECT  Object,
IN ULONG  Access,
IN BOOLEAN  Inheritable,
IN ULONG  ShareMode 
)

Definition at line 227 of file handle.c.

233{
234#define IO_HANDLES_INCREMENT 2 * 3
235
236 ULONG i = 0;
237 PCONSOLE_IO_HANDLE Block;
238
239 // NOTE: Commented out because calling code always lock HandleTableLock before.
240 // RtlEnterCriticalSection(&ProcessData->HandleTableLock);
241
242 ASSERT( (ProcessData->HandleTable == NULL && ProcessData->HandleTableSize == 0) ||
243 (ProcessData->HandleTable != NULL && ProcessData->HandleTableSize != 0) );
244
245 if (ProcessData->HandleTable)
246 {
247 for (i = 0; i < ProcessData->HandleTableSize; i++)
248 {
249 if (ProcessData->HandleTable[i].Object == NULL)
250 break;
251 }
252 }
253
254 if (i >= ProcessData->HandleTableSize)
255 {
256 /* Allocate a new handles table */
258 (ProcessData->HandleTableSize +
260 if (Block == NULL)
261 {
262 // RtlLeaveCriticalSection(&ProcessData->HandleTableLock);
263 return STATUS_UNSUCCESSFUL;
264 }
265
266 /* If we previously had a handles table, free it and use the new one */
267 if (ProcessData->HandleTable)
268 {
269 /* Copy the handles from the old table to the new one */
270 RtlCopyMemory(Block,
271 ProcessData->HandleTable,
272 ProcessData->HandleTableSize * sizeof(CONSOLE_IO_HANDLE));
273 ConsoleFreeHeap(ProcessData->HandleTable);
274 }
275 ProcessData->HandleTable = Block;
276 ProcessData->HandleTableSize += IO_HANDLES_INCREMENT;
277 }
278
279 ProcessData->HandleTable[i].Object = Object;
280 ProcessData->HandleTable[i].Access = Access;
281 ProcessData->HandleTable[i].Inheritable = Inheritable;
282 ProcessData->HandleTable[i].ShareMode = ShareMode;
283 AdjustHandleCounts(&ProcessData->HandleTable[i], +1);
284 *Handle = ULongToHandle((i << 2) | 0x3);
285
286 // RtlLeaveCriticalSection(&ProcessData->HandleTableLock);
287
288 return STATUS_SUCCESS;
289}
#define ULongToHandle(h)
Definition: basetsd.h:81
#define RtlCopyMemory(Destination, Source, Length)
Definition: typedefs.h:263
#define IO_HANDLES_INCREMENT

Referenced by CON_API(), and ConSrvInitProcessHandles().

◆ ConSrvReleaseObject()

VOID ConSrvReleaseObject ( IN PCONSOLE_IO_OBJECT  Object,
IN BOOLEAN  IsConsoleLocked 
)

Definition at line 382 of file handle.c.

384{
385 PCONSRV_CONSOLE ObjectConsole = (PCONSRV_CONSOLE)Object->Console;
386 ConSrvReleaseConsole(ObjectConsole, IsConsoleLocked);
387}
VOID ConSrvReleaseConsole(IN PCONSRV_CONSOLE Console, IN BOOLEAN IsConsoleLocked)
Definition: console.c:316

Referenced by CON_API().

◆ ConSrvRemoveObject()

NTSTATUS ConSrvRemoveObject ( IN PCONSOLE_PROCESS_DATA  ProcessData,
IN HANDLE  Handle 
)

Definition at line 292 of file handle.c.

294{
296
297 RtlEnterCriticalSection(&ProcessData->HandleTableLock);
298
299 ASSERT(ProcessData->HandleTable);
300 // ASSERT( (ProcessData->HandleTable == NULL && ProcessData->HandleTableSize == 0) ||
301 // (ProcessData->HandleTable != NULL && ProcessData->HandleTableSize != 0) );
302
303 if (Index >= ProcessData->HandleTableSize ||
304 ProcessData->HandleTable[Index].Object == NULL)
305 {
306 RtlLeaveCriticalSection(&ProcessData->HandleTableLock);
308 }
309
310 ASSERT(ProcessData->ConsoleHandle);
311 ConSrvCloseHandle(&ProcessData->HandleTable[Index]);
312
313 RtlLeaveCriticalSection(&ProcessData->HandleTableLock);
314 return STATUS_SUCCESS;
315}

Referenced by CON_API().