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.

404 lines
9.1 KiB

  1. //---------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1995
  5. //
  6. // File: servhlp.hxx
  7. //
  8. // Contents: helper functions for winnt service object
  9. //
  10. //
  11. // History: 12/11/95 ramv (Ram Viswanathan) Created.
  12. //
  13. //--------------------------------------------------------------------------
  14. #include "winnt.hxx"
  15. #pragma hdrstop
  16. //
  17. // mapping WinNT Status Codes to ADs Status Codes and vice versa
  18. //
  19. typedef struct _ServiceStatusList {
  20. DWORD dwWinNTServiceStatus;
  21. DWORD dwADsServiceStatus;
  22. } SERVICE_STATUS_LIST, *PSERVICE_STATUS_LIST;
  23. SERVICE_STATUS_LIST ServiceStatusList[] =
  24. {
  25. {SERVICE_STOPPED, ADS_SERVICE_STOPPED },
  26. {SERVICE_START_PENDING, ADS_SERVICE_START_PENDING },
  27. {SERVICE_STOP_PENDING, ADS_SERVICE_STOP_PENDING },
  28. {SERVICE_RUNNING, ADS_SERVICE_RUNNING },
  29. {SERVICE_CONTINUE_PENDING, ADS_SERVICE_CONTINUE_PENDING },
  30. {SERVICE_PAUSE_PENDING, ADS_SERVICE_PAUSE_PENDING },
  31. {SERVICE_PAUSED, ADS_SERVICE_PAUSED },
  32. };
  33. typedef struct _ServiceTypeList {
  34. DWORD dwWinNTServiceType;
  35. DWORD dwADsServiceType;
  36. } SERVICETYPELIST, *PSERVICETYPELIST;
  37. typedef struct _StartTypeList {
  38. DWORD dwWinNTStartType;
  39. DWORD dwADsStartType;
  40. } STARTTYPELIST, *PSTARTTYPELIST;
  41. typedef struct _ErrorList {
  42. DWORD dwWinNTErrorControl;
  43. DWORD dwADsErrorControl;
  44. } ERRORLIST, *PERRORLIST;
  45. SERVICETYPELIST ServiceTypeList[] =
  46. {
  47. { SERVICE_WIN32_OWN_PROCESS,ADS_SERVICE_OWN_PROCESS },
  48. { SERVICE_WIN32_SHARE_PROCESS,ADS_SERVICE_SHARE_PROCESS },
  49. { SERVICE_KERNEL_DRIVER,ADS_SERVICE_KERNEL_DRIVER},
  50. { SERVICE_FILE_SYSTEM_DRIVER,ADS_SERVICE_FILE_SYSTEM_DRIVER}
  51. };
  52. STARTTYPELIST StartTypeList[] =
  53. {
  54. {SERVICE_BOOT_START,ADS_SERVICE_BOOT_START },
  55. {SERVICE_SYSTEM_START, ADS_SERVICE_SYSTEM_START},
  56. {SERVICE_AUTO_START,ADS_SERVICE_AUTO_START },
  57. {SERVICE_DEMAND_START, ADS_SERVICE_DEMAND_START},
  58. {SERVICE_DISABLED, ADS_SERVICE_DISABLED}
  59. };
  60. ERRORLIST ErrorList[] =
  61. {
  62. {SERVICE_ERROR_IGNORE,ADS_SERVICE_ERROR_IGNORE },
  63. {SERVICE_ERROR_NORMAL,ADS_SERVICE_ERROR_NORMAL },
  64. {SERVICE_ERROR_SEVERE,ADS_SERVICE_ERROR_SEVERE },
  65. {SERVICE_ERROR_CRITICAL,ADS_SERVICE_ERROR_CRITICAL}
  66. };
  67. BOOL ServiceStatusWinNTToADs( DWORD dwWinNTStatus,
  68. DWORD *pdwADsStatus)
  69. {
  70. BOOL found = FALSE;
  71. int i;
  72. for (i=0;i<7;i++){
  73. if(dwWinNTStatus == ServiceStatusList[i].dwWinNTServiceStatus){
  74. *pdwADsStatus = ServiceStatusList[i].dwADsServiceStatus;
  75. found = TRUE;
  76. break;
  77. }
  78. }
  79. return (found);
  80. }
  81. BOOL ServiceStatusADsToWinNT( DWORD dwADsStatus,
  82. DWORD *pdwWinNTStatus)
  83. {
  84. BOOL found = FALSE;
  85. int i;
  86. for (i=0;i<7;i++){
  87. if(dwADsStatus == ServiceStatusList[i].dwADsServiceStatus){
  88. *pdwWinNTStatus = ServiceStatusList[i].dwWinNTServiceStatus;
  89. found = TRUE;
  90. break;
  91. }
  92. }
  93. return (found);
  94. }
  95. HRESULT
  96. WinNTEnumServices( LPTSTR szComputerName,
  97. LPDWORD pdwServiceObjectReturned,
  98. LPBYTE *ppMem
  99. )
  100. {
  101. SC_HANDLE schSCManager = NULL;
  102. LPBYTE pMem = NULL;
  103. DWORD dwBytesNeeded =0;
  104. DWORD dwBufLen = 0;
  105. DWORD dwResumeHandle = 0;
  106. BOOL fStatus;
  107. DWORD dwLastError = NULL;
  108. HRESULT hr = S_OK;
  109. ADsAssert(pdwServiceObjectReturned);
  110. ADsAssert(ppMem);
  111. schSCManager = OpenSCManager(szComputerName,
  112. NULL,
  113. SC_MANAGER_ENUMERATE_SERVICE
  114. );
  115. if(schSCManager == NULL){
  116. hr =HRESULT_FROM_WIN32(GetLastError());
  117. goto cleanup;
  118. }
  119. //
  120. // enumerate all win32 services
  121. //
  122. fStatus = EnumServicesStatus(schSCManager,
  123. SERVICE_WIN32,
  124. SERVICE_ACTIVE|
  125. SERVICE_INACTIVE,
  126. (LPENUM_SERVICE_STATUS)pMem,
  127. dwBufLen,
  128. &dwBytesNeeded,
  129. pdwServiceObjectReturned,
  130. &dwResumeHandle
  131. );
  132. if(dwBytesNeeded==0){
  133. hr = S_FALSE;
  134. goto cleanup;
  135. }
  136. if (!fStatus) {
  137. dwLastError = GetLastError();
  138. switch (dwLastError) {
  139. case ERROR_INSUFFICIENT_BUFFER:
  140. case ERROR_MORE_DATA :
  141. pMem = (LPBYTE)AllocADsMem(dwBytesNeeded);
  142. dwBufLen = dwBytesNeeded;
  143. if (!pMem) {
  144. hr = E_OUTOFMEMORY;
  145. break;
  146. }
  147. fStatus = EnumServicesStatus(schSCManager,
  148. SERVICE_WIN32,
  149. SERVICE_ACTIVE|
  150. SERVICE_INACTIVE,
  151. (LPENUM_SERVICE_STATUS)pMem,
  152. dwBufLen,
  153. &dwBytesNeeded,
  154. pdwServiceObjectReturned,
  155. &dwResumeHandle
  156. );
  157. if (!fStatus) {
  158. hr = HRESULT_FROM_WIN32(GetLastError());
  159. break;
  160. }
  161. break;
  162. default:
  163. hr = HRESULT_FROM_WIN32(GetLastError());
  164. break;
  165. }
  166. BAIL_IF_ERROR(hr);
  167. }
  168. *ppMem = pMem;
  169. cleanup:
  170. if(FAILED(hr)){
  171. if(pMem){
  172. FreeADsMem(pMem);
  173. }
  174. }
  175. //
  176. // Close the handle to service control manager
  177. //
  178. if (schSCManager) {
  179. CloseServiceHandle(schSCManager);
  180. }
  181. RRETURN(hr);
  182. }
  183. BOOL ServiceTypeWinNTToADs(DWORD dwServiceType,
  184. DWORD *pdwADsServiceType )
  185. {
  186. BOOL found = FALSE;
  187. int i;
  188. for (i=0; i<4; i++){
  189. if(dwServiceType == ServiceTypeList[i].dwWinNTServiceType){
  190. *pdwADsServiceType = ServiceTypeList[i].dwADsServiceType;
  191. found = TRUE;
  192. break;
  193. }
  194. break;
  195. }
  196. return(found);
  197. }
  198. BOOL StartTypeWinNTToADs(DWORD dwStartType,
  199. DWORD *pdwADsStartType )
  200. {
  201. BOOL found = FALSE;
  202. int i;
  203. for (i=0; i<5; i++){
  204. if(dwStartType == StartTypeList[i].dwWinNTStartType){
  205. *pdwADsStartType = StartTypeList[i].dwADsStartType;
  206. found = TRUE;
  207. break;
  208. }
  209. }
  210. return(found);
  211. }
  212. BOOL ErrorControlWinNTToADs(DWORD dwErrorControl,
  213. DWORD *pdwADsErrorControl)
  214. {
  215. BOOL found = FALSE;
  216. int i;
  217. for (i=0; i<4; i++){
  218. if(dwErrorControl == ErrorList[i].dwWinNTErrorControl){
  219. *pdwADsErrorControl = ErrorList[i].dwADsErrorControl;
  220. found = TRUE;
  221. break;
  222. }
  223. }
  224. return(found);
  225. }
  226. BOOL ServiceTypeADsToWinNT(DWORD dwADsServiceType,
  227. DWORD *pdwServiceType)
  228. {
  229. BOOL found = FALSE;
  230. int i;
  231. for (i=0; i<4; i++){
  232. if(dwADsServiceType == ServiceTypeList[i].dwADsServiceType){
  233. *pdwServiceType = ServiceTypeList[i].dwWinNTServiceType;
  234. found = TRUE;
  235. break;
  236. }
  237. break;
  238. }
  239. return(found);
  240. }
  241. BOOL StartTypeADsToWinNT(DWORD dwADsStartType,
  242. DWORD *pdwStartType)
  243. {
  244. BOOL found = FALSE;
  245. int i;
  246. for (i=0; i<5; i++){
  247. if(dwADsStartType == StartTypeList[i].dwADsStartType){
  248. *pdwStartType = StartTypeList[i].dwWinNTStartType;
  249. found = TRUE;
  250. break;
  251. }
  252. }
  253. return(found);
  254. }
  255. BOOL ErrorControlADsToWinNT(DWORD dwADsErrorControl,
  256. DWORD *pdwErrorControl )
  257. {
  258. BOOL found = FALSE;
  259. int i;
  260. for (i=0; i<4; i++){
  261. if(dwADsErrorControl == ErrorList[i].dwADsErrorControl){
  262. *pdwErrorControl = ErrorList[i].dwWinNTErrorControl;
  263. found = TRUE;
  264. break;
  265. }
  266. }
  267. return(found);
  268. }
  269. HRESULT WinNTDeleteService(POBJECTINFO pObjectInfo)
  270. {
  271. SC_HANDLE schService = NULL;
  272. SC_HANDLE schSCManager = NULL;
  273. HRESULT hr = S_OK;
  274. BOOL fRetval = FALSE;
  275. schSCManager = OpenSCManager(pObjectInfo->ComponentArray[1],
  276. NULL,
  277. SC_MANAGER_ALL_ACCESS
  278. );
  279. if(schSCManager == NULL){
  280. hr = HRESULT_FROM_WIN32(GetLastError());
  281. goto cleanup;
  282. }
  283. schService = OpenService(schSCManager,
  284. pObjectInfo->ComponentArray[2],
  285. DELETE );
  286. if(schService == NULL){
  287. hr = HRESULT_FROM_WIN32(GetLastError());
  288. goto cleanup;
  289. }
  290. fRetval = DeleteService(schService);
  291. if(!fRetval){
  292. hr = HRESULT_FROM_WIN32(GetLastError());
  293. goto cleanup;
  294. }
  295. cleanup:
  296. if(schSCManager){
  297. fRetval = CloseServiceHandle(schSCManager);
  298. if(!fRetval && SUCCEEDED(hr)){
  299. RRETURN(HRESULT_FROM_WIN32(GetLastError()));
  300. }
  301. }
  302. if(schService){
  303. fRetval = CloseServiceHandle(schService);
  304. if(!fRetval && SUCCEEDED(hr)){
  305. RRETURN(HRESULT_FROM_WIN32(GetLastError()));
  306. }
  307. }
  308. RRETURN(hr);
  309. }