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.

422 lines
12 KiB

  1. /*---------------------------------------------------------------------------
  2. File: TDCTInsall.cpp
  3. Comments: Utility class used by the dispatcher to install the DCT agent service.
  4. The TDCTInstall class encapsulates the service control management required
  5. to remotely install the agent service, configure it, and start it.
  6. (c) Copyright 1999, Mission Critical Software, Inc., All Rights Reserved
  7. Proprietary and confidential to Mission Critical Software, Inc.
  8. REVISION LOG ENTRY
  9. Revision By: Christy Boles
  10. Revised on 02/18/99 11:33:17
  11. ---------------------------------------------------------------------------
  12. */
  13. #include "StdAfx.h"
  14. #include "TInst.h"
  15. #include "ErrDct.hpp"
  16. extern TErrorDct err;
  17. //-----------------------------------------------------------------------------
  18. // Open service control manager
  19. //-----------------------------------------------------------------------------
  20. DWORD // ret-OS return code
  21. TDCTInstall::ScmOpen(BOOL bSilent)
  22. {
  23. DWORD rcOs=0; // OS return code
  24. if ( DebugLogging() )
  25. {
  26. err.DbgMsgWrite(
  27. ErrI,
  28. L"%ls install on %ls Start - Open SCM",
  29. m_sDisplayName,m_sComputer );
  30. }
  31. m_hScm = OpenSCManager(m_sComputer,NULL, SC_MANAGER_ALL_ACCESS );
  32. if ( DebugLogging() )
  33. {
  34. err.DbgMsgWrite(
  35. ErrI,
  36. L"%ls install on %ls End - Open SCM",
  37. m_sDisplayName,m_sComputer );
  38. }
  39. if ( !m_hScm )
  40. {
  41. rcOs = GetLastError();
  42. if ( ! bSilent )
  43. err.SysMsgWrite(
  44. ErrW,
  45. rcOs,
  46. DCT_MSG_SCM_OPEN_FAILED_SD,
  47. m_sComputer,
  48. rcOs );
  49. }
  50. return rcOs;
  51. }
  52. //-----------------------------------------------------------------------------
  53. // Close service control manager
  54. //-----------------------------------------------------------------------------
  55. void
  56. TDCTInstall::ScmClose()
  57. {
  58. if ( m_hScm )
  59. {
  60. CloseServiceHandle( m_hScm );
  61. m_hScm = NULL;
  62. }
  63. }
  64. //-----------------------------------------------------------------------------
  65. // Create and start the service
  66. //-----------------------------------------------------------------------------
  67. DWORD // ret-OS return code
  68. TDCTInstall::ServiceStart()
  69. {
  70. DWORD rcOs=0; // OS return code
  71. WCHAR sFile[LEN_Path];
  72. SC_HANDLE hSvc; // Service handle
  73. BOOL bRc; // boolean return code
  74. MCSASSERT(*m_sExeName);
  75. MCSASSERT(*m_sDisplayName);
  76. MCSASSERT(*m_sServiceName);
  77. swprintf(sFile,L"%s",m_sExeName);
  78. if ( DebugLogging() )
  79. {
  80. err.DbgMsgWrite(
  81. ErrI,
  82. L"%ls install on %ls Start - Open %ls service",
  83. m_sDisplayName,
  84. m_sComputer,
  85. m_sServiceName
  86. );
  87. }
  88. hSvc = OpenService( m_hScm, m_sServiceName, SERVICE_ALL_ACCESS );
  89. if ( DebugLogging() )
  90. {
  91. err.DbgMsgWrite(
  92. ErrI,
  93. L"%ls install on %ls End - Open %ls service",
  94. m_sDisplayName, m_sComputer, m_sServiceName );
  95. }
  96. if ( !hSvc )
  97. {
  98. rcOs = GetLastError();
  99. switch ( rcOs )
  100. {
  101. case ERROR_SERVICE_DOES_NOT_EXIST:
  102. break; // no message for this case
  103. default:
  104. err.SysMsgWrite(
  105. ErrW,
  106. rcOs,
  107. DCT_MSG_OPEN_SERVICE_FAILED_SSD,
  108. m_sComputer,
  109. m_sServiceName,
  110. rcOs );
  111. break;
  112. }
  113. rcOs = 0;
  114. if ( DebugLogging() )
  115. {
  116. err.DbgMsgWrite(
  117. ErrI,
  118. L"%ls install on %ls Start - Create %ls service",
  119. m_sDisplayName, m_sComputer, m_sServiceName );
  120. }
  121. hSvc = CreateService( m_hScm, // SCM database handle
  122. m_sServiceName, // Name of service
  123. m_sDisplayName, // Display name
  124. SERVICE_ALL_ACCESS, // Type of access to service
  125. SERVICE_WIN32_OWN_PROCESS, // Type of service
  126. m_StartType, // When to start service
  127. SERVICE_ERROR_NORMAL, // Severity if service fails to start
  128. sFile, // Name of binary file
  129. NULL, // Name of load ordering group
  130. NULL, // Variable to get tag identifier
  131. // m_sDependencies, // Array of dependency names
  132. NULL,
  133. *m_sServiceAccount ? m_sServiceAccount : NULL, // Account name of service
  134. *m_sServiceAccountPassword ? m_sServiceAccountPassword : NULL); // Password for service account
  135. if ( DebugLogging() )
  136. {
  137. err.DbgMsgWrite(
  138. ErrI,
  139. L"%ls install on %ls End - Create %ls service",
  140. m_sDisplayName,m_sComputer,m_sServiceName );
  141. }
  142. if ( !hSvc )
  143. {
  144. rcOs = GetLastError();
  145. err.SysMsgWrite(
  146. ErrW,
  147. rcOs,
  148. DCT_MSG_CREATE_SERVICE_FAILED_SSSSD,
  149. m_sServiceName,
  150. m_sDisplayName,
  151. sFile,
  152. m_sDependencies,
  153. rcOs );
  154. }
  155. }
  156. else
  157. {
  158. if ( DebugLogging() )
  159. {
  160. err.DbgMsgWrite(
  161. ErrI,
  162. L"%ls install on %ls Start - Configure %ls service",
  163. m_sDisplayName, m_sComputer, m_sServiceName );
  164. }
  165. bRc = ChangeServiceConfig(
  166. hSvc, // service handle
  167. SERVICE_WIN32_OWN_PROCESS, // Type of service
  168. m_StartType, // When to start service
  169. SERVICE_ERROR_NORMAL, // Severity if service fails to start
  170. sFile, // Name of binary file
  171. NULL, // Name of load ordering group
  172. NULL, // Variable to get tag identifier
  173. m_sDependencies, // Array of dependency names
  174. *m_sServiceAccount ? m_sServiceAccount : NULL, // Account name of service
  175. *m_sServiceAccountPassword ? m_sServiceAccountPassword : NULL, // Password for service account
  176. m_sDisplayName ); // Display name
  177. if ( DebugLogging() )
  178. {
  179. err.DbgMsgWrite(
  180. ErrI,
  181. L"%ls install on %ls End - Configure %ls service",
  182. m_sDisplayName,m_sComputer, m_sServiceName );
  183. }
  184. if ( !bRc )
  185. {
  186. rcOs = GetLastError();
  187. err.SysMsgWrite(
  188. ErrW,
  189. rcOs,
  190. DCT_MSG_CHANGE_SERVICE_CONFIG_FAILED_SSSSD,
  191. m_sServiceName,
  192. m_sDisplayName,
  193. sFile,
  194. m_sDependencies,
  195. rcOs );
  196. }
  197. }
  198. if ( hSvc )
  199. {
  200. if ( DebugLogging() )
  201. {
  202. err.DbgMsgWrite(
  203. ErrI,
  204. L"%ls install on %ls Start - Start %ls service",
  205. m_sDisplayName, m_sComputer, m_sServiceName );
  206. }
  207. int nCnt = 0;
  208. do
  209. {
  210. bRc = StartService( hSvc, 0, NULL );
  211. if ( !bRc )
  212. {
  213. Sleep(5000);
  214. nCnt++;
  215. err.DbgMsgWrite(0, L"Start service failed.");
  216. }
  217. } while ( !bRc && nCnt < 5 );
  218. if ( DebugLogging() )
  219. {
  220. err.DbgMsgWrite(
  221. ErrI,
  222. L"%ls install on %ls End - Start %ls service",
  223. m_sDisplayName, m_sComputer, m_sServiceName );
  224. }
  225. if ( !bRc )
  226. {
  227. rcOs = GetLastError();
  228. err.SysMsgWrite(
  229. ErrW,
  230. rcOs,
  231. DCT_MSG_START_SERVICE_FAILED_SD,
  232. m_sServiceName,
  233. rcOs );
  234. }
  235. else
  236. {
  237. Sleep( 2000 ); // give the service two seconds to get going
  238. }
  239. CloseServiceHandle( hSvc );
  240. }
  241. return rcOs;
  242. }
  243. //-----------------------------------------------------------------------------
  244. // Stop the service if it is running
  245. //-----------------------------------------------------------------------------
  246. void
  247. TDCTInstall::ServiceStop()
  248. {
  249. DWORD rcOs=0; // OS return code
  250. SC_HANDLE hSvc; // Service handle
  251. SERVICE_STATUS SvcStat; // Service status
  252. DWORD i;
  253. BOOL bRc;
  254. if ( DebugLogging() )
  255. {
  256. err.DbgMsgWrite(
  257. ErrI,
  258. L"%ls install on %ls Start - Open %ls service",
  259. m_sDisplayName, m_sComputer,m_sServiceName );
  260. }
  261. hSvc = OpenService(
  262. m_hScm,
  263. m_sServiceName,
  264. SERVICE_STOP | SERVICE_INTERROGATE );
  265. if ( DebugLogging() )
  266. {
  267. err.DbgMsgWrite(
  268. ErrI,
  269. L"%ls install on %ls End - Open %ls service",
  270. m_sDisplayName, m_sComputer, m_sServiceName );
  271. }
  272. if ( !hSvc )
  273. {
  274. rcOs = GetLastError();
  275. switch ( rcOs )
  276. {
  277. case ERROR_SERVICE_DOES_NOT_EXIST:
  278. break; // no message for this case
  279. default:
  280. err.SysMsgWrite(
  281. ErrW,
  282. rcOs,
  283. DCT_MSG_OPEN_SERVICE_FAILED_SSD,
  284. m_sComputer,
  285. m_sServiceName,
  286. rcOs );
  287. break;
  288. }
  289. }
  290. else
  291. {
  292. if ( DebugLogging() )
  293. {
  294. err.DbgMsgWrite(
  295. ErrI,
  296. L"%ls install on %ls Start - Interrogate %ls service",
  297. m_sDisplayName,m_sComputer,m_sServiceName );
  298. }
  299. bRc = ControlService( hSvc, SERVICE_CONTROL_INTERROGATE, &SvcStat );
  300. if ( DebugLogging() )
  301. {
  302. err.DbgMsgWrite(
  303. ErrI,
  304. L"%ls install on %ls End - Interrogate %ls service",
  305. m_sDisplayName,m_sComputer,m_sServiceName );
  306. }
  307. if ( bRc )
  308. {
  309. if ( SvcStat.dwCurrentState != SERVICE_STOPPED )
  310. { // Service is running
  311. if ( DebugLogging() )
  312. {
  313. err.DbgMsgWrite(
  314. ErrI,
  315. L"%ls install on %ls Start - Stop %ls service",
  316. m_sDisplayName,m_sComputer,m_sServiceName);
  317. }
  318. bRc = ControlService( hSvc, SERVICE_CONTROL_STOP, &SvcStat );
  319. if ( DebugLogging() )
  320. {
  321. err.DbgMsgWrite(
  322. ErrI,
  323. L"%ls on %ls End - Stop %ls service",
  324. m_sDisplayName,m_sComputer,m_sServiceName);
  325. }
  326. if ( bRc )
  327. { // Service accepted the stop request
  328. for ( i = 0; i < 10; i++ ) // 30 seconds total
  329. {
  330. Sleep( 3000 ); // three seconds
  331. if ( DebugLogging() )
  332. {
  333. err.DbgMsgWrite(
  334. ErrI,
  335. L"%ls install on %ls Start - Interrogate %ls service",
  336. m_sDisplayName,m_sComputer,m_sServiceName);
  337. }
  338. bRc = ControlService(
  339. hSvc,
  340. SERVICE_CONTROL_INTERROGATE,
  341. &SvcStat );
  342. if ( DebugLogging() )
  343. {
  344. err.DbgMsgWrite(
  345. ErrI,
  346. L"%ls install on %ls End - Interrogate %ls service",
  347. m_sDisplayName,m_sComputer,m_sServiceName);
  348. }
  349. if ( !bRc )
  350. break;
  351. if ( SvcStat.dwCurrentState == SERVICE_STOPPED )
  352. break;
  353. }
  354. if ( SvcStat.dwCurrentState != SERVICE_STOPPED )
  355. {
  356. rcOs = GetLastError();
  357. switch ( rcOs )
  358. {
  359. case 0:
  360. case ERROR_SERVICE_NOT_ACTIVE: // Service is not running
  361. break;
  362. default:
  363. err.SysMsgWrite(
  364. ErrW,
  365. rcOs,
  366. DCT_MSG_SERVICE_STOP_FAILED_SSD,
  367. m_sComputer,
  368. m_sServiceName,
  369. rcOs );
  370. break;
  371. }
  372. }
  373. }
  374. }
  375. }
  376. else
  377. {
  378. rcOs = GetLastError();
  379. rcOs = 0;
  380. }
  381. CloseServiceHandle( hSvc );
  382. }
  383. }