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.

444 lines
9.4 KiB

  1. // Copyright (c) 2002 Microsoft Corporation
  2. //
  3. // File: IndexingInstallationUnit.cpp
  4. //
  5. // Synopsis: Defines a IndexingInstallationUnit
  6. // This object has the knowledge for installing the
  7. // indexing service
  8. //
  9. // History: 03/20/2002 JeffJon Created
  10. #include "pch.h"
  11. #include "resource.h"
  12. #include "IndexingInstallationUnit.h"
  13. #include "InstallationUnitProvider.h"
  14. #include <ciodm.h>
  15. IndexingInstallationUnit::IndexingInstallationUnit() :
  16. InstallationUnit(
  17. 0,
  18. 0,
  19. 0,
  20. 0,
  21. 0,
  22. 0,
  23. 0,
  24. 0,
  25. 0,
  26. 0,
  27. String(),
  28. String(),
  29. String(),
  30. INDEXING_SERVICE)
  31. {
  32. LOG_CTOR(IndexingInstallationUnit);
  33. }
  34. IndexingInstallationUnit::~IndexingInstallationUnit()
  35. {
  36. LOG_DTOR(IndexingInstallationUnit);
  37. }
  38. InstallationReturnType
  39. IndexingInstallationUnit::InstallService(HANDLE /*logfileHandle*/, HWND /*hwnd*/)
  40. {
  41. LOG_FUNCTION(IndexingInstallationUnit::InstallService);
  42. InstallationReturnType result = INSTALL_SUCCESS;
  43. String unattendFileText;
  44. String infFileText;
  45. unattendFileText += L"[Components]\n";
  46. unattendFileText += L"indexsrv_system=ON\n";
  47. bool ocmResult = InstallServiceWithOcManager(infFileText, unattendFileText);
  48. if (ocmResult &&
  49. IsServiceOn())
  50. {
  51. LOG(L"Indexing service installed successfully");
  52. }
  53. else
  54. {
  55. LOG(L"Failed to install the indexing service");
  56. result = INSTALL_FAILURE;
  57. }
  58. LOG_INSTALL_RETURN(result);
  59. return result;
  60. }
  61. UnInstallReturnType
  62. IndexingInstallationUnit::UnInstallService(HANDLE /*logfileHandle*/, HWND /*hwnd*/)
  63. {
  64. LOG_FUNCTION(IndexingInstallationUnit::UnInstallService);
  65. UnInstallReturnType result = UNINSTALL_SUCCESS;
  66. LOG_UNINSTALL_RETURN(result);
  67. return result;
  68. }
  69. bool
  70. IndexingInstallationUnit::IsServiceInstalled()
  71. {
  72. LOG_FUNCTION(IndexingInstallationUnit::IsServiceInstalled);
  73. bool result = false;
  74. // If we can instantiate the indexing server COM object
  75. // then the indexing service is installed
  76. do
  77. {
  78. CLSID clsid;
  79. HRESULT hr = CLSIDFromProgID( L"Microsoft.ISAdm", &clsid );
  80. if (FAILED(hr))
  81. {
  82. LOG(String::format(
  83. L"Failed to get the CLSID from ProgID: hr = 0x%x",
  84. hr));
  85. break;
  86. }
  87. SmartInterface<IAdminIndexServer> adminIndexServer;
  88. hr = adminIndexServer.AcquireViaCreateInstance(
  89. clsid,
  90. 0,
  91. CLSCTX_INPROC_SERVER);
  92. if (SUCCEEDED(hr))
  93. {
  94. result = true;
  95. }
  96. else
  97. {
  98. LOG(String::format(
  99. L"Failed to CreateInstance the indexing COM object: hr = 0x%1!x!",
  100. hr));
  101. }
  102. } while (false);
  103. LOG_BOOL(result);
  104. return result;
  105. }
  106. HRESULT
  107. IndexingInstallationUnit::StartService(HANDLE logfileHandle)
  108. {
  109. LOG_FUNCTION(IndexingInstallationUnit::StartService);
  110. HRESULT hr = S_OK;
  111. do
  112. {
  113. if (!IsServiceInstalled())
  114. {
  115. InstallationReturnType installResult =
  116. InstallService(logfileHandle, 0);
  117. if (installResult != INSTALL_SUCCESS)
  118. {
  119. LOG(L"Failed to install the indexing service");
  120. hr = Win32ToHresult(ERROR_SERVICE_DOES_NOT_EXIST);
  121. break;
  122. }
  123. }
  124. // Since the Indexing service is no longer SERVICE_DEMAND_START we
  125. // we have to change the service config from SERVICE_DISABLED to
  126. // SERVICE_AUTO_START.
  127. hr = ChangeServiceConfigToAutoStart();
  128. if (FAILED(hr))
  129. {
  130. LOG(String::format(
  131. L"Failed to enable the indexing service: hr = 0x%1!x!",
  132. hr));
  133. break;
  134. }
  135. hr = ModifyIndexingService(true);
  136. } while (false);
  137. LOG_HRESULT(hr);
  138. return hr;
  139. }
  140. HRESULT
  141. IndexingInstallationUnit::StopService()
  142. {
  143. LOG_FUNCTION(IndexingInstallationUnit::StopService);
  144. HRESULT hr = S_OK;
  145. do
  146. {
  147. hr = ModifyIndexingService(false);
  148. if (FAILED(hr))
  149. {
  150. LOG(String::format(
  151. L"Failed to stop the indexing service: hr = 0x%1!x!",
  152. hr));
  153. break;
  154. }
  155. // Set the indexing service to disabled so that it doesn't
  156. // start after a reboot
  157. hr = ChangeServiceConfigToDisabled();
  158. if (FAILED(hr))
  159. {
  160. LOG(String::format(
  161. L"Failed to disable the indexing service: hr = 0x%1!x!",
  162. hr));
  163. break;
  164. }
  165. } while (false);
  166. LOG_HRESULT(hr);
  167. return hr;
  168. }
  169. bool
  170. IndexingInstallationUnit::IsServiceOn()
  171. {
  172. LOG_FUNCTION(IndexingInstallationUnit::IsServiceOn);
  173. bool result = false;
  174. do
  175. {
  176. CLSID clsid;
  177. HRESULT hr = CLSIDFromProgID( L"Microsoft.ISAdm", &clsid );
  178. if (FAILED(hr))
  179. {
  180. LOG(String::format(
  181. L"Failed to get the CLSID from ProgID: hr = 0x%x",
  182. hr));
  183. break;
  184. }
  185. SmartInterface<IAdminIndexServer> adminIndexServer;
  186. hr = adminIndexServer.AcquireViaCreateInstance(
  187. clsid,
  188. 0,
  189. CLSCTX_INPROC_SERVER);
  190. if (FAILED(hr))
  191. {
  192. LOG(String::format(
  193. L"Failed to CoCreateInstance of IAdminIndexServer: hr = 0x%x",
  194. hr));
  195. break;
  196. }
  197. VARIANT_BOOL var;
  198. hr = adminIndexServer->IsRunning(&var);
  199. if (FAILED(hr))
  200. {
  201. LOG(String::format(
  202. L"Failed to get running state: hr = 0x%x",
  203. hr));
  204. break;
  205. }
  206. LOG(String::format(
  207. L"var = 0x%1!x!",
  208. var));
  209. result = var ? true : false;
  210. } while (false);
  211. LOG_BOOL(result);
  212. return result;
  213. }
  214. HRESULT
  215. IndexingInstallationUnit::ModifyIndexingService(bool turnOn)
  216. {
  217. LOG_FUNCTION2(
  218. IndexingInstallationUnit::ModifyIndexingService,
  219. turnOn ? L"true" : L"false");
  220. HRESULT hr = S_OK;
  221. do
  222. {
  223. CLSID clsid;
  224. hr = CLSIDFromProgID( L"Microsoft.ISAdm", &clsid );
  225. if (FAILED(hr))
  226. {
  227. LOG(String::format(
  228. L"Failed to get the CLSID from ProgID: hr = 0x%x",
  229. hr));
  230. break;
  231. }
  232. SmartInterface<IAdminIndexServer> adminIndexServer;
  233. hr = adminIndexServer.AcquireViaCreateInstance(
  234. clsid,
  235. 0,
  236. CLSCTX_INPROC_SERVER);
  237. if (FAILED(hr))
  238. {
  239. LOG(String::format(
  240. L"Failed to CoCreateInstance of IAdminIndexServer: hr = 0x%x",
  241. hr));
  242. break;
  243. }
  244. if (turnOn)
  245. {
  246. hr = adminIndexServer->Start();
  247. }
  248. else
  249. {
  250. hr = adminIndexServer->Stop();
  251. }
  252. if (FAILED(hr))
  253. {
  254. LOG(String::format(
  255. L"Failed to start or stop indexing service: hr = 0x%x",
  256. hr));
  257. break;
  258. }
  259. } while (false);
  260. LOG(String::format(L"hr = %1!x!", hr));
  261. return hr;
  262. }
  263. HRESULT
  264. IndexingInstallationUnit::ChangeServiceConfigToAutoStart()
  265. {
  266. LOG_FUNCTION(IndexingInstallationUnit::ChangeServiceConfigToAutoStart);
  267. HRESULT hr = ChangeServiceStartType(SERVICE_AUTO_START);
  268. LOG_HRESULT(hr);
  269. return hr;
  270. }
  271. HRESULT
  272. IndexingInstallationUnit::ChangeServiceConfigToDisabled()
  273. {
  274. LOG_FUNCTION(IndexingInstallationUnit::ChangeServiceConfigToDisabled);
  275. HRESULT hr = ChangeServiceStartType(SERVICE_DISABLED);
  276. LOG_HRESULT(hr);
  277. return hr;
  278. }
  279. HRESULT
  280. IndexingInstallationUnit::ChangeServiceStartType(DWORD startType)
  281. {
  282. LOG_FUNCTION(IndexingInstallationUnit::ChangeServiceStartType);
  283. HRESULT hr = S_OK;
  284. SC_HANDLE handle = 0;
  285. SC_HANDLE serviceHandle = 0;
  286. do
  287. {
  288. // Open the service controller
  289. hr =
  290. Win::OpenSCManager(
  291. L"",
  292. GENERIC_READ | GENERIC_WRITE,
  293. handle);
  294. if (FAILED(hr))
  295. {
  296. LOG(String::format(
  297. L"Failed to open service controller: hr = 0x%1!x!",
  298. hr));
  299. break;
  300. }
  301. // The indexing service name
  302. static const String serviceName(L"cisvc");
  303. // Open the service
  304. hr =
  305. Win::OpenService(
  306. handle,
  307. serviceName,
  308. SERVICE_CHANGE_CONFIG,
  309. serviceHandle);
  310. if (FAILED(hr))
  311. {
  312. LOG(String::format(
  313. L"Failed to open service %1: hr = 0x%2!x!",
  314. serviceName.c_str(),
  315. hr));
  316. break;
  317. }
  318. hr =
  319. Win::ChangeServiceConfig(
  320. serviceHandle,
  321. SERVICE_NO_CHANGE,
  322. startType,
  323. SERVICE_NO_CHANGE,
  324. L"",
  325. L"",
  326. 0,
  327. L"",
  328. L"",
  329. EncryptedString(),
  330. L"");
  331. if (FAILED(hr))
  332. {
  333. LOG(String::format(
  334. L"Failed to change service config: hr = 0x%1!x!",
  335. hr));
  336. break;
  337. }
  338. } while(false);
  339. // Close the service handles if necessary
  340. if (serviceHandle)
  341. {
  342. Win::CloseServiceHandle(serviceHandle);
  343. }
  344. if (handle)
  345. {
  346. Win::CloseServiceHandle(handle);
  347. }
  348. LOG_HRESULT(hr);
  349. return hr;
  350. }