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.

244 lines
6.3 KiB

  1. //+--------------------------------------------------------------------------
  2. //
  3. // Copyright (c) 1997-1999 Microsoft Corporation
  4. //
  5. // File: services.cpp
  6. //
  7. // Contents:
  8. //
  9. // History:
  10. //
  11. //---------------------------------------------------------------------------
  12. #include "stdafx.h"
  13. #include "logfile.h"
  14. /*
  15. * Constants.
  16. */
  17. const UINT SECTION_SIZE = 256;
  18. const TCHAR SERVICE_DEL_KEY[] = _T("DelService");
  19. const UINT SERVICE_DEL_NAME = 1;
  20. const TCHAR SERVICE_START_KEY[] = _T("StartService");
  21. const UINT SERVICE_START_NAME = 1;
  22. /*
  23. * Helper Functions.
  24. */
  25. VOID
  26. ProcessDelService(
  27. IN SC_HANDLE schSCManager,
  28. IN LPCTSTR pszServiceName
  29. )
  30. {
  31. SC_HANDLE schService;
  32. SERVICE_STATUS ssStatus;
  33. schService = OpenService(schSCManager, pszServiceName, SERVICE_ALL_ACCESS);
  34. if (!schService) {
  35. LOGMESSAGE(_T("ProcessDelService: Can't open service %s"),
  36. pszServiceName);
  37. return;
  38. }
  39. if (ControlService(schService, SERVICE_CONTROL_STOP, &ssStatus)) {
  40. Sleep(1000);
  41. while(QueryServiceStatus(schService, &ssStatus)) {
  42. if (ssStatus.dwCurrentState == SERVICE_STOP_PENDING) {
  43. Sleep(1000);
  44. } else {
  45. break;
  46. }
  47. }
  48. if (ssStatus.dwCurrentState != SERVICE_STOPPED) {
  49. LOGMESSAGE(_T("ProcessDelService: Couldn't stop service %s"),
  50. pszServiceName);
  51. }
  52. } else {
  53. LOGMESSAGE(_T("ProcessDelService: Couldn't control service %s"),
  54. pszServiceName);
  55. }
  56. if (DeleteService(schService)) {
  57. LOGMESSAGE(_T("ProcessDelService: %s deleted"), pszServiceName);
  58. } else {
  59. LOGMESSAGE(_T("ProcessDelService: Couldn't delete service %s"),
  60. pszServiceName);
  61. }
  62. CloseServiceHandle(schService);
  63. }
  64. VOID
  65. ProcessStartService(
  66. IN SC_HANDLE schSCManager,
  67. IN LPCTSTR pszServiceName
  68. )
  69. {
  70. SC_HANDLE schService;
  71. SERVICE_STATUS ssStatus;
  72. schService = OpenService(schSCManager, pszServiceName, SERVICE_ALL_ACCESS);
  73. if (!schService) {
  74. LOGMESSAGE(_T("ProcessStartService: Can't open service %s"),
  75. pszServiceName);
  76. return;
  77. }
  78. if (StartService(schService, 0, NULL)) {
  79. Sleep(1000);
  80. while(QueryServiceStatus(schService, &ssStatus)) {
  81. if (ssStatus.dwCurrentState == SERVICE_START_PENDING) {
  82. Sleep(1000);
  83. } else {
  84. break;
  85. }
  86. }
  87. if (ssStatus.dwCurrentState == SERVICE_RUNNING) {
  88. LOGMESSAGE(_T("ProcessStartService: %s started"),
  89. pszServiceName);
  90. } else {
  91. LOGMESSAGE(_T("ProcessStartService: Couldn't start service %s"),
  92. pszServiceName);
  93. }
  94. } else {
  95. LOGMESSAGE(_T("ProcessStartService: Couldn't control service %s"),
  96. pszServiceName);
  97. }
  98. CloseServiceHandle(schService);
  99. }
  100. /*
  101. * ServiceDeleteFromInfSection()
  102. *
  103. * Handles service deletion from inf sections in the form of:
  104. *
  105. * [SectionName]
  106. * DelService = Service1
  107. * DelService = Service2
  108. *
  109. * where Service1 and Service2 are service names.
  110. */
  111. DWORD
  112. ServiceDeleteFromInfSection(
  113. IN HINF hInf,
  114. IN LPCTSTR pszSection
  115. )
  116. {
  117. BOOL fErr;
  118. BOOL fFound;
  119. INFCONTEXT infContext;
  120. SC_HANDLE schSCManager;
  121. schSCManager = OpenSCManager(
  122. NULL,
  123. NULL,
  124. SC_MANAGER_ALL_ACCESS
  125. );
  126. if (!schSCManager) {
  127. LOGMESSAGE(_T("ServiceDeleteFromInfSection: Failed to open SC Manager"));
  128. return(GetLastError());
  129. }
  130. fFound = SetupFindFirstLine(
  131. hInf,
  132. pszSection,
  133. SERVICE_DEL_KEY,
  134. &infContext
  135. );
  136. while (fFound) {
  137. TCHAR pszServiceName[SECTION_SIZE];
  138. fErr = SetupGetStringField(
  139. &infContext,
  140. SERVICE_DEL_NAME,
  141. pszServiceName,
  142. SECTION_SIZE,
  143. NULL
  144. );
  145. if (fErr) {
  146. ProcessDelService(schSCManager, pszServiceName);
  147. } else {
  148. LOGMESSAGE(_T("ServiceDeleteFromInfSection: Could not get service section."));
  149. }
  150. fFound = SetupFindNextMatchLine(
  151. &infContext,
  152. SERVICE_DEL_KEY,
  153. &infContext
  154. );
  155. }
  156. CloseServiceHandle(schSCManager);
  157. return(ERROR_SUCCESS);
  158. }
  159. /*
  160. * ServiceStartFromInfSection()
  161. *
  162. * Starts a service that has been installed by setupapi.
  163. *
  164. */
  165. DWORD
  166. ServiceStartFromInfSection(
  167. IN HINF hInf,
  168. IN LPCTSTR pszSection
  169. )
  170. {
  171. BOOL fErr;
  172. BOOL fFound;
  173. INFCONTEXT infContext;
  174. SC_HANDLE schSCManager;
  175. schSCManager = OpenSCManager(
  176. NULL,
  177. NULL,
  178. SC_MANAGER_ALL_ACCESS
  179. );
  180. if (!schSCManager) {
  181. LOGMESSAGE(_T("ServiceStartFromInfSection: Failed to open SC Manager"));
  182. return(GetLastError());
  183. }
  184. fFound = SetupFindFirstLine(
  185. hInf,
  186. pszSection,
  187. SERVICE_START_KEY,
  188. &infContext
  189. );
  190. while (fFound) {
  191. TCHAR pszServiceName[SECTION_SIZE];
  192. fErr = SetupGetStringField(
  193. &infContext,
  194. SERVICE_START_NAME,
  195. pszServiceName,
  196. SECTION_SIZE,
  197. NULL
  198. );
  199. if (fErr) {
  200. ProcessStartService(schSCManager, pszServiceName);
  201. } else {
  202. LOGMESSAGE(_T("ServiceStartFromInfSection: Could not get service section."));
  203. }
  204. fFound = SetupFindNextMatchLine(
  205. &infContext,
  206. SERVICE_START_KEY,
  207. &infContext
  208. );
  209. }
  210. CloseServiceHandle(schSCManager);
  211. return(ERROR_SUCCESS);
  212. }