Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

172 lines
3.8 KiB

  1. /*++
  2. Copyright (c) 1992 Microsoft Corporation
  3. Module Name:
  4. handledb.c
  5. Abstract:
  6. This module contains the code to maintain an open handle data base for the
  7. INSTALER program. This is used to track path names associated with open
  8. handles so we can construct full paths from relative opens.
  9. Author:
  10. Steve Wood (stevewo) 11-Aug-1994
  11. Revision History:
  12. --*/
  13. #include "instaler.h"
  14. char *HandleTypes[] = {
  15. "File/Key",
  16. "File",
  17. "Key"
  18. };
  19. POPENHANDLE_INFO
  20. FindOpenHandle(
  21. PPROCESS_INFO Process,
  22. HANDLE Handle,
  23. ULONG Type
  24. )
  25. {
  26. PLIST_ENTRY Next, Head;
  27. POPENHANDLE_INFO p;
  28. Head = &Process->OpenHandleListHead;
  29. Next = Head->Flink;
  30. while (Next != Head) {
  31. p = CONTAINING_RECORD( Next, OPENHANDLE_INFO, Entry );
  32. if (p->Handle == Handle && (Type == (ULONG)-1 || p->Type == Type)) {
  33. return p;
  34. }
  35. Next = Next->Flink;
  36. }
  37. return NULL;
  38. }
  39. BOOLEAN
  40. AddOpenHandle(
  41. PPROCESS_INFO Process,
  42. HANDLE Handle,
  43. ULONG Type,
  44. PWSTR Name,
  45. BOOLEAN InheritHandle
  46. )
  47. {
  48. POPENHANDLE_INFO p;
  49. if (Type == (ULONG)-1) {
  50. return FALSE;
  51. }
  52. if (FindOpenHandle( Process, Handle, Type ) != NULL) {
  53. return FALSE;
  54. }
  55. p = AllocMem( sizeof( *p ) );
  56. if (p == NULL) {
  57. return FALSE;
  58. }
  59. p->Handle = Handle;
  60. p->Type = (USHORT)Type;
  61. p->Inherit = InheritHandle;
  62. p->Name = Name;
  63. p->LengthOfName = (USHORT)(wcslen( Name ) * sizeof( WCHAR ));
  64. if (Type == HANDLE_TYPE_FILE && p->LengthOfName == (3 * sizeof( WCHAR ))) {
  65. p->RootDirectory = TRUE;
  66. }
  67. InsertHeadList( &Process->OpenHandleListHead, &p->Entry );
  68. return TRUE;
  69. }
  70. BOOLEAN
  71. DeleteOpenHandle(
  72. PPROCESS_INFO Process,
  73. HANDLE Handle,
  74. ULONG Type
  75. )
  76. {
  77. POPENHANDLE_INFO p;
  78. p = FindOpenHandle( Process, Handle, Type );
  79. if (p == NULL) {
  80. //
  81. // We will see lots of close calls for handles we dont care
  82. // about. Ignore them quietly.
  83. //
  84. return FALSE;
  85. }
  86. RemoveEntryList( &p->Entry );
  87. FreeMem( &p->QueryName );
  88. FreeMem( &p );
  89. return TRUE;
  90. }
  91. static BOOLEAN IgnoredFirstProcess = FALSE;
  92. VOID
  93. InheritHandles(
  94. PPROCESS_INFO Process
  95. )
  96. {
  97. PPROCESS_INFO ParentProcess;
  98. PLIST_ENTRY Prev, Head;
  99. POPENHANDLE_INFO p;
  100. if (Process->ProcessInformation.InheritedFromUniqueProcessId == 0) {
  101. return;
  102. }
  103. ParentProcess = FindProcessById( Process->ProcessInformation.InheritedFromUniqueProcessId );
  104. if (ParentProcess == NULL) {
  105. if (!IgnoredFirstProcess) {
  106. IgnoredFirstProcess = TRUE;
  107. }
  108. else {
  109. DbgEvent( INTERNALERROR, ("Unable to find parent process (%x)\n", Process->ProcessInformation.InheritedFromUniqueProcessId ) );
  110. }
  111. return;
  112. }
  113. Head = &ParentProcess->OpenHandleListHead;
  114. Prev = Head->Blink;
  115. while (Prev != Head) {
  116. p = CONTAINING_RECORD( Prev, OPENHANDLE_INFO, Entry );
  117. if (p->Inherit) {
  118. if (!AddOpenHandle( Process,
  119. p->Handle,
  120. p->Type,
  121. p->Name,
  122. TRUE
  123. )
  124. ) {
  125. DbgEvent( INTERNALERROR, ("Unable to inherit %s handle (%x) for '%ws' from process (%x)\n",
  126. HandleTypes[ 1+p->Type ],
  127. p->Handle,
  128. p->Name,
  129. Process->ProcessInformation.InheritedFromUniqueProcessId
  130. )
  131. );
  132. }
  133. }
  134. Prev = Prev->Blink;
  135. }
  136. return;
  137. }