Leaked source code of windows server 2003
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.

282 lines
8.2 KiB

  1. /*==========================================================================
  2. *
  3. * Copyright (C) 1995 Microsoft Corporation. All Rights Reserved.
  4. *
  5. * File: dllinit.c
  6. * Content: DDRAW.DLL initialization
  7. *
  8. ***************************************************************************/
  9. /*
  10. * unfortunately we have to break our pre-compiled headers to get our
  11. * GUIDS defined...
  12. */
  13. #define INITGUID
  14. #include "ddrawpr.h"
  15. #include <initguid.h>
  16. #ifdef WINNT
  17. #undef IUnknown
  18. #include <objbase.h>
  19. #include "aclapi.h"
  20. #endif
  21. HANDLE hWindowListMutex; //=(HANDLE)0;
  22. #define WINDOWLISTMUTEXNAME "DDrawWindowListMutex"
  23. #define INITCSWINDLIST() \
  24. hWindowListMutex = CreateMutex(NULL,FALSE,WINDOWLISTMUTEXNAME);
  25. #define FINIWINDLIST() CloseHandle(hWindowListMutex);
  26. HINSTANCE g_hModule=0;
  27. /*
  28. * Winnt specific global statics
  29. */
  30. BYTE szDeviceWndClass[] = "DirectDrawDeviceWnd";
  31. /*
  32. * This mutex is owned by the exclusive mode owner
  33. */
  34. HANDLE hExclusiveModeMutex=0;
  35. HANDLE hCheckExclusiveModeMutex=0;
  36. #define EXCLUSIVE_MODE_MUTEX_NAME "__DDrawExclMode__"
  37. #define CHECK_EXCLUSIVE_MODE_MUTEX_NAME "__DDrawCheckExclMode__"
  38. //#endif
  39. /*
  40. * Win95 specific global statics
  41. */
  42. #ifdef WIN95
  43. LPVOID lpWin16Lock;
  44. static CRITICAL_SECTION csInit = {0};
  45. CRITICAL_SECTION csWindowList;
  46. CRITICAL_SECTION csDriverObjectList;
  47. #endif
  48. extern BOOL APIENTRY D3DDllMain(HMODULE hModule,
  49. DWORD dwReason,
  50. LPVOID lpvReserved);
  51. extern void CPixel__Cleanup();
  52. #undef DPF_MODNAME
  53. #define DPF_MODNAME "DllMain"
  54. /*
  55. * DllMain
  56. */
  57. BOOL WINAPI
  58. DllMain(HINSTANCE hmod, DWORD dwReason, LPVOID lpvReserved)
  59. {
  60. DWORD pid;
  61. BOOL didhelp;
  62. pid = GetCurrentProcessId();
  63. switch(dwReason)
  64. {
  65. case DLL_PROCESS_ATTACH:
  66. DisableThreadLibraryCalls(hmod);
  67. DPFINIT();
  68. // Create the DirectDraw csect
  69. DPF(4, "====> ENTER: DLLMAIN(%08lx): Process Attach: %08lx, tid=%08lx", DllMain,
  70. pid, GetCurrentThreadId());
  71. /*
  72. * This must be the first time.
  73. */
  74. INITCSWINDLIST();
  75. g_hModule = hmod;
  76. //Let's grant the world MUTEX_ALL_ACCESS.... (bugs 210604, 30170, 194290, 194355)
  77. {
  78. #ifdef WINNT
  79. SECURITY_ATTRIBUTES sa;
  80. SID_IDENTIFIER_AUTHORITY sia = SECURITY_WORLD_SID_AUTHORITY;
  81. PSID adminSid = 0;
  82. ULONG cbAcl;
  83. PACL acl=0;
  84. PSECURITY_DESCRIPTOR pSD;
  85. BYTE buffer[SECURITY_DESCRIPTOR_MIN_LENGTH];
  86. BOOL bSecurityGooSucceeded = FALSE;
  87. //Granny's old fashioned LocalAlloc:
  88. BYTE Buffer1[256];
  89. BYTE Buffer2[16];
  90. // Create the SID for world
  91. cbAcl = GetSidLengthRequired(1);
  92. if (cbAcl < sizeof(Buffer2))
  93. {
  94. adminSid = (PSID) Buffer2;
  95. InitializeSid(
  96. adminSid,
  97. &sia,
  98. 1
  99. );
  100. *GetSidSubAuthority(adminSid, 0) = SECURITY_WORLD_RID;
  101. // Create an ACL giving World all access.
  102. cbAcl = sizeof(ACL) +
  103. (sizeof(ACCESS_ALLOWED_ACE) - sizeof(DWORD)) +
  104. GetLengthSid(adminSid);
  105. if (cbAcl < sizeof(Buffer1))
  106. {
  107. acl = (PACL)&Buffer1;
  108. if (InitializeAcl(
  109. acl,
  110. cbAcl,
  111. ACL_REVISION
  112. ))
  113. {
  114. if (AddAccessAllowedAce(
  115. acl,
  116. ACL_REVISION,
  117. SYNCHRONIZE|MUTANT_QUERY_STATE|DELETE|READ_CONTROL, //|WRITE_OWNER|WRITE_DAC,
  118. adminSid
  119. ))
  120. {
  121. // Create a security descriptor with the above ACL.
  122. pSD = (PSECURITY_DESCRIPTOR)buffer;
  123. if (InitializeSecurityDescriptor(pSD, SECURITY_DESCRIPTOR_REVISION))
  124. {
  125. if (SetSecurityDescriptorDacl(pSD, TRUE, acl, FALSE))
  126. {
  127. // Fill in the SECURITY_ATTRIBUTES struct.
  128. sa.nLength = sizeof(sa);
  129. sa.lpSecurityDescriptor = pSD;
  130. sa.bInheritHandle = TRUE;
  131. bSecurityGooSucceeded = TRUE;
  132. }
  133. }
  134. }
  135. }
  136. }
  137. }
  138. #endif
  139. DDASSERT(0 == hExclusiveModeMutex);
  140. hExclusiveModeMutex = CreateMutex(
  141. #ifdef WINNT
  142. bSecurityGooSucceeded ? &sa :
  143. #endif
  144. NULL, //use default access if security goo failed.
  145. FALSE,
  146. EXCLUSIVE_MODE_MUTEX_NAME );
  147. #ifdef WINNT
  148. if (0 == hExclusiveModeMutex)
  149. {
  150. hExclusiveModeMutex = OpenMutex(
  151. SYNCHRONIZE|DELETE, // access flag
  152. FALSE, // inherit flag
  153. EXCLUSIVE_MODE_MUTEX_NAME // pointer to mutex-object name
  154. );
  155. }
  156. #endif
  157. if (hExclusiveModeMutex == 0)
  158. {
  159. DPF_ERR("Could not create exclusive mode mutex. exiting");
  160. return FALSE;
  161. }
  162. DDASSERT(0 == hCheckExclusiveModeMutex);
  163. hCheckExclusiveModeMutex = CreateMutex(
  164. #ifdef WINNT
  165. bSecurityGooSucceeded ? &sa :
  166. #endif
  167. NULL, //use default access if security goo failed.
  168. FALSE,
  169. CHECK_EXCLUSIVE_MODE_MUTEX_NAME );
  170. #ifdef WINNT
  171. if (0 == hCheckExclusiveModeMutex)
  172. {
  173. hCheckExclusiveModeMutex = OpenMutex(
  174. SYNCHRONIZE|DELETE, // access flag
  175. FALSE, // inherit flag
  176. CHECK_EXCLUSIVE_MODE_MUTEX_NAME // pointer to mutex-object name
  177. );
  178. }
  179. #endif
  180. if (hCheckExclusiveModeMutex == 0)
  181. {
  182. DPF_ERR("Could not create exclusive mode check mutex. exiting");
  183. CloseHandle(hExclusiveModeMutex);
  184. return FALSE;
  185. }
  186. }
  187. if (!MemInit())
  188. {
  189. DPF(0,"LEAVING, COULD NOT MemInit");
  190. CloseHandle(hExclusiveModeMutex);
  191. CloseHandle(hCheckExclusiveModeMutex);
  192. return FALSE;
  193. }
  194. // Do whatever it takes for D3D (mostly PSGP stuff)
  195. D3DDllMain(g_hModule, dwReason, lpvReserved);
  196. DPF(4, "====> EXIT: DLLMAIN(%08lx): Process Attach: %08lx", DllMain,
  197. pid);
  198. break;
  199. case DLL_PROCESS_DETACH:
  200. DPF(4, "====> ENTER: DLLMAIN(%08lx): Process Detach %08lx, tid=%08lx",
  201. DllMain, pid, GetCurrentThreadId());
  202. // Cleanup registry in CPixel
  203. CPixel__Cleanup();
  204. /*
  205. * disconnect from thunk, even if other cleanup code commented out...
  206. */
  207. MemFini();
  208. DDASSERT(0 != hExclusiveModeMutex);
  209. CloseHandle(hCheckExclusiveModeMutex);
  210. CloseHandle(hExclusiveModeMutex);
  211. FINIWINDLIST();
  212. // Do whatever it takes for D3D (mostly PSGP stuff)
  213. D3DDllMain(g_hModule, dwReason, lpvReserved);
  214. DPF(4, "====> EXIT: DLLMAIN(%08lx): Process Detach %08lx",
  215. DllMain, pid);
  216. break;
  217. /*
  218. * we don't ever want to see thread attach/detach
  219. */
  220. #ifdef DEBUG
  221. case DLL_THREAD_ATTACH:
  222. DPF(4, "THREAD_ATTACH");
  223. break;
  224. case DLL_THREAD_DETACH:
  225. DPF(4,"THREAD_DETACH");
  226. break;
  227. #endif
  228. default:
  229. break;
  230. }
  231. return TRUE;
  232. } /* DllMain */