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.

392 lines
11 KiB

  1. //////////////////////////////////////////////////////////////////////////////
  2. //
  3. // File Name: fxocSvc.cpp
  4. //
  5. // Abstract: This provides the Service routines used in the FaxOCM
  6. // code base.
  7. //
  8. // Environment: Windows XP / User Mode
  9. //
  10. // Copyright (c) 2000 Microsoft Corporation
  11. //
  12. // Revision History:
  13. //
  14. // Date: Developer: Comments:
  15. // ----- ---------- ---------
  16. // 21-Mar-2000 Oren Rosenbloom (orenr) Created
  17. //////////////////////////////////////////////////////////////////////////////
  18. #include "faxocm.h"
  19. #pragma hdrstop
  20. #include <Accctrl.h>
  21. #include <Aclapi.h>
  22. /////////////////////// Static Function Prototypes /////////////////////////
  23. ///////////////////////////////
  24. // fxocSvc_Init
  25. //
  26. // Initialize the fax service
  27. // setup subsystem
  28. //
  29. // Params:
  30. // - void.
  31. // Returns:
  32. // - NO_ERROR on success.
  33. // - error code otherwise.
  34. //
  35. DWORD fxocSvc_Init(void)
  36. {
  37. DWORD dwRes = NO_ERROR;
  38. DBG_ENTER(_T("Init Service Module"),dwRes);
  39. return dwRes;
  40. }
  41. ///////////////////////////////
  42. // fxocSvc_Term
  43. //
  44. // Terminate the fax service
  45. // setup subsystem
  46. //
  47. // Params:
  48. // - void.
  49. // Returns:
  50. // - NO_ERROR on success.
  51. // - error code otherwise.
  52. //
  53. DWORD fxocSvc_Term(void)
  54. {
  55. DWORD dwRes = NO_ERROR;
  56. DBG_ENTER(_T("Term Service Module"),dwRes);
  57. return dwRes;
  58. }
  59. ///////////////////////////////
  60. // fxocSvc_Install
  61. //
  62. // Create/delete the fax service as
  63. // specified in the INF file
  64. //
  65. // Params:
  66. // - pszSubcomponentId.
  67. // - pszInstallSection - section in INF we are installing from
  68. // Returns:
  69. // - NO_ERROR on success.
  70. // - error code otherwise.
  71. //
  72. DWORD fxocSvc_Install(const TCHAR *pszSubcomponentId,
  73. const TCHAR *pszInstallSection)
  74. {
  75. DWORD dwReturn = NO_ERROR;
  76. BOOL bSuccess = FALSE;
  77. HINF hInf = faxocm_GetComponentInf();
  78. OCMANAGER_ROUTINES *pHelpers = faxocm_GetComponentHelperRoutines();
  79. DBG_ENTER( _T("fxocSvc_Install"),
  80. dwReturn,
  81. _T("%s - %s"),
  82. pszSubcomponentId,
  83. pszInstallSection);
  84. if ((hInf == NULL) ||
  85. (pszInstallSection == NULL) ||
  86. (pHelpers == NULL))
  87. {
  88. return ERROR_INVALID_PARAMETER;
  89. }
  90. // attempt to install any Services specified in the Fax
  91. // install section in the INF file.
  92. bSuccess = ::SetupInstallServicesFromInfSection(hInf,
  93. pszInstallSection,
  94. 0);
  95. if (bSuccess)
  96. {
  97. SC_ACTION Actions[] =
  98. {
  99. {SC_ACTION_RESTART, 1000 * 60}, // Restart the service 1 minute after 1st failure
  100. {SC_ACTION_RESTART, 1000 * 60}, // Restart the service 1 minute after 2nd failure
  101. {SC_ACTION_NONE, 0} // Do nothing for 3rd, 4th, ... failures
  102. };
  103. SERVICE_FAILURE_ACTIONS sfa =
  104. {
  105. 60 * 5, // After 5 minutes, reset failures count
  106. NULL, // Reboot message is unchanged
  107. NULL, // No change in the run command
  108. ARR_SIZE (Actions), // Number of actions
  109. Actions // Actions array
  110. };
  111. VERBOSE(DBG_MSG,
  112. _T("Successfully installed fax service from ")
  113. _T("section '%s'"),
  114. pszInstallSection);
  115. dwReturn = SetServiceFailureActions (NULL, FAX_SERVICE_NAME, &sfa);
  116. if (NO_ERROR == dwReturn)
  117. {
  118. VERBOSE(DBG_MSG,_T("Successfully set fax service failure actions..."));
  119. //
  120. // if this install is being done through the control panel via the
  121. // Add/Remove Windows Components dialog (i.e. NOT a clean/upgrade install
  122. // of the OS), then start the service if a reboot is not required.
  123. // If this is not a stand alone install, then the machine will be rebooted
  124. // anyway so the fax service will auto-start.
  125. //
  126. if (fxState_IsStandAlone())
  127. {
  128. dwReturn = fxocSvc_StartFaxService();
  129. if (dwReturn == NO_ERROR)
  130. {
  131. VERBOSE(DBG_MSG,_T("Successfully started fax service..."));
  132. }
  133. else
  134. {
  135. VERBOSE(SETUP_ERR,
  136. _T("Failed to start fax service, rc = 0x%lx"),
  137. dwReturn);
  138. }
  139. }
  140. }
  141. else
  142. {
  143. VERBOSE(SETUP_ERR,
  144. _T("Failed to set fax service failure actions, rc = 0x%lx"),
  145. dwReturn);
  146. }
  147. }
  148. else
  149. {
  150. dwReturn = ::GetLastError();
  151. VERBOSE(SETUP_ERR,
  152. _T("Failed to install the services section in ")
  153. _T("section '%s', rc = 0x%lx"),
  154. pszInstallSection,
  155. dwReturn);
  156. }
  157. return dwReturn;
  158. }
  159. ///////////////////////////////
  160. // fxocSvc_Uninstall
  161. //
  162. // Delete the fax service as
  163. // specified in the INF file
  164. //
  165. // Params:
  166. // - pszSubcomponentId.
  167. // - pszInstallSection - section in INF we are installing from
  168. // Returns:
  169. // - NO_ERROR on success.
  170. // - error code otherwise.
  171. //
  172. DWORD fxocSvc_Uninstall(const TCHAR *pszSubcomponentId,
  173. const TCHAR *pszUninstallSection)
  174. {
  175. DWORD dwReturn = NO_ERROR;
  176. HINF hInf = faxocm_GetComponentInf();
  177. BOOL bSuccess = FALSE;
  178. DBG_ENTER( _T("fxocSvc_Uninstall"),
  179. dwReturn,
  180. _T("%s - %s"),
  181. pszSubcomponentId,
  182. pszUninstallSection);
  183. if ((hInf == NULL) ||
  184. (pszUninstallSection == NULL))
  185. {
  186. return ERROR_INVALID_PARAMETER;
  187. }
  188. if (dwReturn == NO_ERROR)
  189. {
  190. bSuccess = StopService(NULL, FAX_SERVICE_NAME, TRUE);
  191. if (!bSuccess)
  192. {
  193. dwReturn = GetLastError();
  194. VERBOSE(SETUP_ERR,
  195. _T("Uninstall failed to stop fax service, ec = 0x%lx, ")
  196. _T("attempting to uninstall fax service anyway"),
  197. dwReturn);
  198. }
  199. bSuccess = ::SetupInstallServicesFromInfSection(hInf,
  200. pszUninstallSection,
  201. 0);
  202. if (bSuccess)
  203. {
  204. VERBOSE(DBG_MSG,
  205. _T("Successfully uninstalled service ")
  206. _T("from section '%s'"),
  207. pszUninstallSection);
  208. }
  209. else
  210. {
  211. dwReturn = ::GetLastError();
  212. VERBOSE(SETUP_ERR,
  213. _T("Failed to uninstall ")
  214. _T("service, SubcomponentId = '%s', ")
  215. _T("uninstall Section = '%s', rc = 0x%lx"),
  216. pszSubcomponentId,
  217. pszUninstallSection,
  218. dwReturn);
  219. }
  220. }
  221. return dwReturn;
  222. }
  223. ///////////////////////////////
  224. // fxocSvc_StartFaxService
  225. //
  226. // Start the fax service
  227. // specified in the given
  228. // INF file's section.
  229. //
  230. // Params:
  231. // None
  232. //
  233. // Returns:
  234. // - NO_ERROR on success
  235. // - error code otherwise.
  236. //
  237. DWORD fxocSvc_StartFaxService()
  238. {
  239. DWORD dwReturn = NO_ERROR;
  240. DBG_ENTER( _T("fxocSvc_StartFaxService"),
  241. dwReturn);
  242. if (!EnsureFaxServiceIsStarted (NULL))
  243. {
  244. dwReturn = GetLastError ();
  245. }
  246. return dwReturn;
  247. }
  248. ///////////////////////////////
  249. // fxocSvc_StartService
  250. //
  251. // Start the specified service
  252. //
  253. // Params:
  254. // - pszServiceName
  255. // Returns:
  256. // - NO_ERROR on success
  257. // - error code otherwise.
  258. //
  259. DWORD fxocSvc_StartService(const TCHAR *pszServiceName)
  260. {
  261. BOOL bSuccess = FALSE;
  262. DWORD dwReturn = NO_ERROR;
  263. SC_HANDLE hSvcMgr = NULL;
  264. SC_HANDLE hService = NULL;
  265. DBG_ENTER( _T("fxocSvc_StartService"),
  266. dwReturn,
  267. _T("%s"),
  268. pszServiceName);
  269. // open the service manager
  270. hSvcMgr = ::OpenSCManager(NULL,
  271. NULL,
  272. SC_MANAGER_ALL_ACCESS);
  273. if (hSvcMgr == NULL)
  274. {
  275. dwReturn = ::GetLastError();
  276. VERBOSE(SETUP_ERR,
  277. _T("Failed to open the service manager, rc = 0x%lx"),
  278. dwReturn);
  279. }
  280. if (dwReturn == NO_ERROR)
  281. {
  282. hService = ::OpenService(hSvcMgr,
  283. pszServiceName,
  284. SERVICE_ALL_ACCESS);
  285. if (hService == NULL)
  286. {
  287. dwReturn = ::GetLastError();
  288. VERBOSE(SETUP_ERR,
  289. _T("fxocSvc_StartService, Failed to open service ")
  290. _T("'%s', rc = 0x%lx"),
  291. pszServiceName,
  292. dwReturn);
  293. }
  294. }
  295. // Start the fax service.
  296. if (dwReturn == NO_ERROR)
  297. {
  298. bSuccess = StartService(hService, 0, NULL);
  299. if (!bSuccess)
  300. {
  301. dwReturn = ::GetLastError();
  302. if (dwReturn == ERROR_SERVICE_ALREADY_RUNNING)
  303. {
  304. dwReturn = NO_ERROR;
  305. }
  306. else
  307. {
  308. VERBOSE(SETUP_ERR,
  309. _T("Failed to start service '%s', ")
  310. _T("rc = 0x%lx"),
  311. pszServiceName,
  312. dwReturn);
  313. }
  314. }
  315. }
  316. if (dwReturn == NO_ERROR)
  317. {
  318. SERVICE_STATUS Status;
  319. int i = 0;
  320. do
  321. {
  322. QueryServiceStatus(hService, &Status);
  323. i++;
  324. if (Status.dwCurrentState != SERVICE_RUNNING)
  325. {
  326. Sleep(1000);
  327. }
  328. } while ((i < 60) && (Status.dwCurrentState != SERVICE_RUNNING));
  329. if (Status.dwCurrentState != SERVICE_RUNNING)
  330. {
  331. VERBOSE(SETUP_ERR,
  332. _T("Failed to start '%s' service"),
  333. pszServiceName);
  334. }
  335. }
  336. if (hService)
  337. {
  338. CloseServiceHandle(hService);
  339. }
  340. if (hSvcMgr)
  341. {
  342. CloseServiceHandle(hSvcMgr);
  343. }
  344. return dwReturn;
  345. }