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.

331 lines
10 KiB

  1. //////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright (c) 2001 Microsoft Corporation
  4. //
  5. // Module Name:
  6. // tdilib.cpp
  7. //
  8. // Abstract:
  9. // tdilib initialization and shutdown functions
  10. //
  11. //////////////////////////////////////////////////////////////////////////
  12. #include "stdafx.h"
  13. #include "winsvc.h"
  14. //
  15. // library global variables are defined here
  16. //
  17. HANDLE hTdiSampleDriver; // handle used to call driver
  18. CRITICAL_SECTION LibCriticalSection; // serialize DeviceIoControl calls...
  19. // ----------------------------------------------
  20. //
  21. // Function: TdiLibInit
  22. //
  23. // Arguments: none
  24. //
  25. // Returns: TRUE -- everything initialized ok
  26. // FALSE -- initialization error (usually, unable to attach driver)
  27. //
  28. // Descript: This function is called by the by the exe to
  29. // initialize communication with the driver. It loads the
  30. // driver, and establishes communication with it
  31. //
  32. // ----------------------------------------------
  33. BOOLEAN
  34. TdiLibInit(VOID)
  35. {
  36. //
  37. // the handle to tdisample.sys should always be null on entry
  38. // Complain loadly if it is not.
  39. //
  40. if (hTdiSampleDriver != NULL)
  41. {
  42. OutputDebugString(TEXT("hTdiSampleDriver non-null on entry!\n"));
  43. return FALSE;
  44. }
  45. //
  46. // find out what operating system we are using.
  47. //
  48. OSVERSIONINFO OsVersionInfo;
  49. OsVersionInfo.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
  50. if (GetVersionEx(&OsVersionInfo))
  51. {
  52. if (OsVersionInfo.dwPlatformId == VER_PLATFORM_WIN32_NT)
  53. {
  54. if (OsVersionInfo.dwMajorVersion < 5)
  55. {
  56. OutputDebugString(TEXT("Not supported for versions prior to Windows 2000\n"));
  57. return FALSE;
  58. }
  59. }
  60. else
  61. {
  62. OutputDebugString(TEXT("Unrecognized OS version\n"));
  63. return FALSE;
  64. }
  65. }
  66. else
  67. {
  68. OutputDebugString(TEXT("Cannot get OS version -- aborting\n"));
  69. return FALSE;
  70. }
  71. //
  72. // assume tdisample.sys driver is loaded -- try to attach it
  73. // normally driver will be loaded
  74. //
  75. hTdiSampleDriver = CreateFile(TEXT("\\\\.\\TDISAMPLE"),
  76. GENERIC_READ | GENERIC_WRITE,
  77. FILE_SHARE_READ | FILE_SHARE_WRITE,
  78. NULL, // lpSecurityAttirbutes
  79. OPEN_EXISTING,
  80. FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED,
  81. NULL); // lpTemplateFile
  82. //
  83. // If the driver is not loaded, then we have to try and load it...
  84. //
  85. if (hTdiSampleDriver == INVALID_HANDLE_VALUE)
  86. {
  87. OutputDebugString(TEXT("Tdisample.sys not loaded. Attempting to load\n"));
  88. SC_HANDLE hSCMan = NULL;
  89. SC_HANDLE hDriver = NULL;
  90. if ((hSCMan = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS)) == NULL)
  91. {
  92. OutputDebugString(TEXT("Failed to Open ServiceManager\n"));
  93. return FALSE;
  94. }
  95. //
  96. // open service
  97. //
  98. if((hDriver = OpenService(hSCMan, TEXT("tdisample"), SERVICE_ALL_ACCESS)) == NULL)
  99. {
  100. //
  101. // service doesn't exist -- try to create it
  102. //
  103. OutputDebugString(TEXT("Service does not exist -- try to create it"));
  104. hDriver = CreateService(hSCMan,
  105. TEXT("tdisample"),
  106. TEXT("tdisample"),
  107. SERVICE_ALL_ACCESS,
  108. SERVICE_KERNEL_DRIVER,
  109. SERVICE_DEMAND_START,
  110. SERVICE_ERROR_NORMAL,
  111. TEXT("\\SystemRoot\\system32\\drivers\\tdisample.sys"),
  112. NULL, NULL, NULL, NULL, NULL);
  113. }
  114. if (hDriver != NULL)
  115. {
  116. SERVICE_STATUS ServiceStatus;
  117. if(QueryServiceStatus(hDriver, &ServiceStatus))
  118. {
  119. if(ServiceStatus.dwServiceType != SERVICE_KERNEL_DRIVER)
  120. {
  121. CloseServiceHandle(hDriver);
  122. CloseServiceHandle(hSCMan);
  123. return FALSE;
  124. }
  125. switch(ServiceStatus.dwCurrentState)
  126. {
  127. //
  128. // this is the one we expect! try and start it
  129. //
  130. case SERVICE_STOPPED:
  131. {
  132. int i;
  133. if(!StartService(hDriver, 0, NULL))
  134. {
  135. CloseServiceHandle(hDriver);
  136. CloseServiceHandle(hSCMan);
  137. return FALSE;
  138. }
  139. //
  140. // we need to make sure tdisample.sys is actually running
  141. //
  142. for(i=0; i < 30; i++)
  143. {
  144. Sleep(500);
  145. if(QueryServiceStatus(hDriver, &ServiceStatus))
  146. {
  147. if(ServiceStatus.dwCurrentState == SERVICE_RUNNING)
  148. {
  149. break;
  150. }
  151. }
  152. }
  153. if (ServiceStatus.dwCurrentState != SERVICE_RUNNING)
  154. {
  155. OutputDebugString(TEXT("Failed to start tdisample service\n"));
  156. CloseServiceHandle(hDriver);
  157. CloseServiceHandle(hSCMan);
  158. return FALSE;
  159. }
  160. break;
  161. }
  162. //
  163. // we don't expect this, but (technically) it is not an error
  164. // If it happens, just assume that the load succeeded
  165. //
  166. case SERVICE_RUNNING:
  167. OutputDebugString(TEXT("ServiceStatus thinks driver is running\n"));
  168. break;
  169. //
  170. // anything else is an error
  171. //
  172. default:
  173. OutputDebugString(TEXT("ServiceStatusError\n"));
  174. CloseServiceHandle(hDriver);
  175. CloseServiceHandle(hSCMan);
  176. return FALSE;
  177. }
  178. }
  179. else
  180. {
  181. OutputDebugString(TEXT("QueryServiceStatus failed\n"));
  182. CloseServiceHandle(hDriver);
  183. CloseServiceHandle(hSCMan);
  184. return FALSE;
  185. }
  186. }
  187. else
  188. {
  189. OutputDebugString(TEXT("Tdisample service does not exist!\n"));
  190. CloseServiceHandle(hSCMan);
  191. return FALSE;
  192. }
  193. //
  194. // if get to here, system thinks it is loaded. So try again
  195. //
  196. hTdiSampleDriver = CreateFile(TEXT("\\\\.\\TDISAMPLE"),
  197. GENERIC_READ | GENERIC_WRITE,
  198. FILE_SHARE_READ | FILE_SHARE_WRITE,
  199. NULL, // lpSecurityAttirbutes
  200. OPEN_EXISTING,
  201. FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED,
  202. NULL); // lpTemplateFile
  203. //
  204. // if fails again, give up
  205. //
  206. if (hTdiSampleDriver == INVALID_HANDLE_VALUE)
  207. {
  208. OutputDebugString(TEXT("Unable to load Tdisample.sys\n"));
  209. hTdiSampleDriver = NULL;
  210. return FALSE;
  211. }
  212. }
  213. //
  214. // if get to here, tdisample.sys is loaded, and hTdiSampleDriver is valid
  215. //
  216. try
  217. {
  218. InitializeCriticalSection(&LibCriticalSection);
  219. }
  220. catch(...)
  221. {
  222. CloseHandle(hTdiSampleDriver);
  223. hTdiSampleDriver = NULL;
  224. return FALSE;
  225. }
  226. //
  227. // make sure the dll and driver are the same version (ie, are compatible)
  228. //
  229. {
  230. NTSTATUS lStatus; // status of command
  231. ULONG ulVersion = 0; // default value (error)
  232. RECEIVE_BUFFER ReceiveBuffer; // return info from command
  233. SEND_BUFFER SendBuffer; // arguments for command
  234. //
  235. // call driver to execute command
  236. //
  237. lStatus = TdiLibDeviceIO(ulVERSION_CHECK,
  238. &SendBuffer,
  239. &ReceiveBuffer);
  240. if (lStatus == STATUS_SUCCESS)
  241. {
  242. ulVersion = ReceiveBuffer.RESULTS.ulReturnValue;
  243. }
  244. //
  245. // check results..
  246. //
  247. if (ulVersion == TDI_SAMPLE_VERSION_ID)
  248. {
  249. return TRUE; // only successful completion!!!
  250. }
  251. else
  252. {
  253. OutputDebugString(TEXT("Incompatible driver version.\n"));
  254. }
  255. //
  256. // if get to here, had an error and need to clean up
  257. //
  258. DeleteCriticalSection(&LibCriticalSection);
  259. CloseHandle(hTdiSampleDriver);
  260. hTdiSampleDriver = NULL;
  261. }
  262. return FALSE;
  263. }
  264. // ----------------------------------------------
  265. //
  266. // Function: TdiLibClose
  267. //
  268. // Arguments: none
  269. //
  270. // Returns: none
  271. //
  272. // Descript: This function is called by the by the dll or exe to
  273. // shut down communication with the driver.
  274. //
  275. // ----------------------------------------------
  276. VOID
  277. TdiLibClose(VOID)
  278. {
  279. //
  280. // need to check for NULL since function will be called
  281. // if the open above failed..
  282. //
  283. if (hTdiSampleDriver != NULL)
  284. {
  285. DeleteCriticalSection(&LibCriticalSection);
  286. //
  287. // close the connection to tdisample.sys
  288. //
  289. if (!CloseHandle(hTdiSampleDriver))
  290. {
  291. OutputDebugString(TEXT("\n TdiLibClose: closehandle failed\n"));
  292. }
  293. hTdiSampleDriver = NULL;
  294. }
  295. }
  296. //////////////////////////////////////////////////////////////////////////
  297. // end of file tdilib.cpp
  298. //////////////////////////////////////////////////////////////////////////