service.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. /** BEGIN COPYRIGHT BLOCK
  2. * This Program is free software; you can redistribute it and/or modify it under
  3. * the terms of the GNU General Public License as published by the Free Software
  4. * Foundation; version 2 of the License.
  5. *
  6. * This Program is distributed in the hope that it will be useful, but WITHOUT
  7. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  8. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  9. *
  10. * You should have received a copy of the GNU General Public License along with
  11. * this Program; if not, write to the Free Software Foundation, Inc., 59 Temple
  12. * Place, Suite 330, Boston, MA 02111-1307 USA.
  13. *
  14. * In addition, as a special exception, Red Hat, Inc. gives You the additional
  15. * right to link the code of this Program with code not covered under the GNU
  16. * General Public License ("Non-GPL Code") and to distribute linked combinations
  17. * including the two, subject to the limitations in this paragraph. Non-GPL Code
  18. * permitted under this exception must only link to the code of this Program
  19. * through those well defined interfaces identified in the file named EXCEPTION
  20. * found in the source code files (the "Approved Interfaces"). The files of
  21. * Non-GPL Code may instantiate templates or use macros or inline functions from
  22. * the Approved Interfaces without causing the resulting work to be covered by
  23. * the GNU General Public License. Only Red Hat, Inc. may make changes or
  24. * additions to the list of Approved Interfaces. You must obey the GNU General
  25. * Public License in all respects for all of the Program code and other code used
  26. * in conjunction with the Program except the Non-GPL Code covered by this
  27. * exception. If you modify this file, you may extend this exception to your
  28. * version of the file, but you are not obligated to do so. If you do not wish to
  29. * provide this exception without modification, you must delete this exception
  30. * statement from your version and license this file solely under the GPL without
  31. * exception.
  32. *
  33. *
  34. * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
  35. * Copyright (C) 2005 Red Hat, Inc.
  36. * All rights reserved.
  37. * END COPYRIGHT BLOCK **/
  38. #include <windows.h>
  39. #include "nt/ntos.h"
  40. #define SERVRET_ERROR 0
  41. #define SERVRET_INSTALLED 1
  42. #define SERVRET_STARTING 2
  43. #define SERVRET_STARTED 3
  44. #define SERVRET_STOPPING 4
  45. #define SERVRET_REMOVED 5
  46. DWORD NS_WINAPI
  47. SERVICE_GetNTServiceStatus(LPCTSTR szServiceName, LPDWORD lpLastError )
  48. {
  49. SERVICE_STATUS ServiceStatus;
  50. SC_HANDLE schService = NULL;
  51. SC_HANDLE schSCManager = NULL;
  52. DWORD lastError = 0;
  53. int ret = 0;
  54. //ereport(LOG_INFORM, "open SC Manager");
  55. if ((schSCManager = OpenSCManager(
  56. NULL, // machine (NULL == local)
  57. NULL, // database (NULL == default)
  58. SC_MANAGER_ALL_ACCESS // access required
  59. )) == NULL ) {
  60. lastError = GetLastError();
  61. ret = SERVRET_ERROR;
  62. goto finish;
  63. }
  64. schService = OpenService(schSCManager, szServiceName, SERVICE_ALL_ACCESS);
  65. if (schService == NULL ) {
  66. lastError = GetLastError();
  67. if (lastError == ERROR_SERVICE_DOES_NOT_EXIST) {
  68. lastError = 0;
  69. ret = SERVRET_REMOVED;
  70. } else
  71. ret = SERVRET_ERROR;
  72. goto finish;
  73. }
  74. ret = ControlService(schService, SERVICE_CONTROL_INTERROGATE, &ServiceStatus);
  75. if ( !ret ) {
  76. lastError = GetLastError();
  77. if ( lastError == ERROR_SERVICE_NOT_ACTIVE ) {
  78. lastError = 0;
  79. ret = SERVRET_INSTALLED;
  80. } else
  81. ret = SERVRET_ERROR;
  82. goto finish;
  83. }
  84. switch ( ServiceStatus.dwCurrentState ) {
  85. case SERVICE_STOPPED: ret = SERVRET_INSTALLED; break;
  86. case SERVICE_START_PENDING: ret = SERVRET_STARTING; break;
  87. case SERVICE_STOP_PENDING: ret = SERVRET_STOPPING; break;
  88. case SERVICE_RUNNING: ret = SERVRET_STARTED; break;
  89. case SERVICE_CONTINUE_PENDING: ret = SERVRET_STARTED; break;
  90. case SERVICE_PAUSE_PENDING: ret = SERVRET_STARTED; break;
  91. case SERVICE_PAUSED: ret = SERVRET_STARTED; break;
  92. default: ret = SERVRET_ERROR; break;
  93. }
  94. finish:
  95. if ( schService)
  96. CloseServiceHandle(schService);
  97. if ( schSCManager)
  98. CloseServiceHandle(schSCManager);
  99. if ( lpLastError )
  100. *lpLastError = lastError;
  101. return ret;
  102. }
  103. DWORD NS_WINAPI
  104. SERVICE_InstallNTService(LPCTSTR szServiceName, LPCTSTR szServiceDisplayName, LPCTSTR szServiceExe )
  105. {
  106. LPCTSTR lpszBinaryPathName = szServiceExe;
  107. SC_HANDLE schService = NULL;
  108. SC_HANDLE schSCManager = NULL;
  109. int lastError = 0;
  110. int ret = 0;
  111. //ereport(LOG_INFORM, "open SC Manager");
  112. if ((schSCManager = OpenSCManager(
  113. NULL, // machine (NULL == local)
  114. NULL, // database (NULL == default)
  115. SC_MANAGER_ALL_ACCESS // access required
  116. )) == NULL ) {
  117. lastError = GetLastError();
  118. goto finish;
  119. }
  120. /* check if service already exists */
  121. schService = OpenService( schSCManager,
  122. szServiceName,
  123. SERVICE_ALL_ACCESS
  124. );
  125. if (schService) {
  126. lastError = ERROR_SERVICE_EXISTS;
  127. goto finish;
  128. }
  129. schService = CreateService(
  130. schSCManager, // SCManager database
  131. szServiceName, // name of service
  132. szServiceDisplayName, // name to display
  133. SERVICE_ALL_ACCESS, // desired access
  134. SERVICE_WIN32_OWN_PROCESS |
  135. SERVICE_INTERACTIVE_PROCESS, // service type
  136. SERVICE_AUTO_START, //SERVICE_DEMAND_START, // start type
  137. SERVICE_ERROR_NORMAL, // error control type
  138. lpszBinaryPathName, // service's binary
  139. NULL, // no load ordering group
  140. NULL, // no tag identifier
  141. NULL, // no dependencies
  142. NULL, // LocalSystem account
  143. NULL); // no password
  144. if (schService == NULL) {
  145. lastError = GetLastError();
  146. }
  147. // successfully installed service
  148. finish:
  149. if ( schService)
  150. CloseServiceHandle(schService);
  151. if ( schSCManager)
  152. CloseServiceHandle(schSCManager);
  153. return lastError;
  154. }
  155. DWORD NS_WINAPI
  156. SERVICE_RemoveNTService(LPCTSTR szServiceName)
  157. {
  158. SC_HANDLE schService = NULL;
  159. SC_HANDLE schSCManager = NULL;
  160. int lastError = 0;
  161. int ret = 0;
  162. //ereport(LOG_INFORM, "open SC Manager");
  163. if ((schSCManager = OpenSCManager(
  164. NULL, // machine (NULL == local)
  165. NULL, // database (NULL == default)
  166. SC_MANAGER_ALL_ACCESS // access required
  167. )) == NULL ) {
  168. lastError = GetLastError();
  169. goto finish;
  170. }
  171. schService = OpenService(schSCManager, szServiceName, SERVICE_ALL_ACCESS);
  172. if (schService == NULL ) {
  173. lastError = GetLastError();
  174. goto finish;
  175. }
  176. ret = DeleteService(schService);
  177. if ( !ret) {
  178. lastError = GetLastError();
  179. goto finish;
  180. }
  181. // successfully removed service
  182. finish:
  183. if ( schService)
  184. CloseServiceHandle(schService);
  185. if ( schSCManager)
  186. CloseServiceHandle(schSCManager);
  187. return lastError;
  188. }
  189. DWORD NS_WINAPI
  190. SERVICE_StartNTService(LPCTSTR szServiceName)
  191. {
  192. SC_HANDLE schService = NULL;
  193. SC_HANDLE schSCManager = NULL;
  194. int lastError = 0;
  195. int ret = 0;
  196. //ereport(LOG_INFORM, "open SC Manager");
  197. if ((schSCManager = OpenSCManager(
  198. NULL, // machine (NULL == local)
  199. NULL, // database (NULL == default)
  200. SC_MANAGER_ALL_ACCESS // access required
  201. )) == NULL ) {
  202. lastError = GetLastError();
  203. goto finish;
  204. }
  205. schService = OpenService(schSCManager, szServiceName, SERVICE_ALL_ACCESS);
  206. if (schService == NULL ) {
  207. lastError = GetLastError();
  208. goto finish;
  209. }
  210. ret = StartService(schService, 0, NULL);
  211. if ( !ret ) {
  212. lastError = GetLastError();
  213. goto finish;
  214. }
  215. // successfully started service
  216. finish:
  217. if ( schService)
  218. CloseServiceHandle(schService);
  219. if ( schSCManager)
  220. CloseServiceHandle(schSCManager);
  221. return lastError;
  222. }
  223. DWORD NS_WINAPI
  224. SERVICE_StartNTServiceAndWait(LPCTSTR szServiceName, LPDWORD lpdwLastError)
  225. {
  226. DWORD dwLastError;
  227. DWORD dwStatus;
  228. int i;
  229. /* check if service is running */
  230. dwStatus = SERVICE_GetNTServiceStatus( szServiceName, &dwLastError );
  231. if ( dwStatus == SERVRET_STARTED )
  232. return TRUE;
  233. dwLastError = SERVICE_StartNTService( szServiceName );
  234. if ( dwLastError != 0 ) {
  235. goto errorExit;
  236. }
  237. for ( i=0; i<5; i++ ) {
  238. // make sure the service got installed
  239. dwStatus = SERVICE_GetNTServiceStatus( szServiceName, &dwLastError );
  240. if ( dwStatus == SERVRET_ERROR) {
  241. if ( dwLastError != ERROR_SERVICE_CANNOT_ACCEPT_CTRL )
  242. goto errorExit;
  243. } else if ( dwStatus == SERVRET_STARTED )
  244. return TRUE;
  245. Sleep ( 1000 );
  246. }
  247. dwLastError = 0;
  248. errorExit:
  249. if ( lpdwLastError )
  250. *lpdwLastError = dwLastError;
  251. return FALSE;
  252. }
  253. DWORD NS_WINAPI
  254. SERVICE_StopNTService(LPCTSTR szServiceName)
  255. {
  256. SC_HANDLE schService = NULL;
  257. SC_HANDLE schSCManager = NULL;
  258. int lastError = 0;
  259. int ret = 0;
  260. SERVICE_STATUS ServiceStatus;
  261. //ereport(LOG_INFORM, "open SC Manager");
  262. if ((schSCManager = OpenSCManager(
  263. NULL, // machine (NULL == local)
  264. NULL, // database (NULL == default)
  265. SC_MANAGER_ALL_ACCESS // access required
  266. )) == NULL ) {
  267. lastError = GetLastError();
  268. goto finish;
  269. }
  270. schService = OpenService(schSCManager, szServiceName, SERVICE_ALL_ACCESS);
  271. if (schService == NULL ) {
  272. lastError = GetLastError();
  273. goto finish;
  274. }
  275. ret = ControlService(schService, SERVICE_CONTROL_STOP, &ServiceStatus);
  276. if ( !ret ) {
  277. lastError = GetLastError();
  278. goto finish;
  279. }
  280. // server is stopping
  281. finish:
  282. if ( schService)
  283. CloseServiceHandle(schService);
  284. if ( schSCManager)
  285. CloseServiceHandle(schSCManager);
  286. return lastError;
  287. }
  288. DWORD NS_WINAPI
  289. SERVICE_StopNTServiceAndWait(LPCTSTR szServiceName, LPDWORD lpdwLastError)
  290. {
  291. DWORD dwLastError;
  292. DWORD dwStatus;
  293. int i;
  294. /* check if service is running */
  295. dwStatus = SERVICE_GetNTServiceStatus( szServiceName, &dwLastError );
  296. if ( dwStatus != SERVRET_STARTED )
  297. return TRUE;
  298. for ( i=0; i<30; i++ ) {
  299. dwLastError = SERVICE_StopNTService( szServiceName );
  300. Sleep ( 1000 );
  301. // make sure the service is stoppped and just installed
  302. dwStatus = SERVICE_GetNTServiceStatus( szServiceName, &dwLastError );
  303. Sleep ( 1000 );
  304. if ( dwStatus == SERVRET_INSTALLED ) {
  305. Sleep ( 1000 );
  306. return TRUE;
  307. }
  308. }
  309. if ( lpdwLastError )
  310. *lpdwLastError = dwLastError;
  311. return FALSE;
  312. }
  313. DWORD NS_WINAPI
  314. SERVICE_ReinstallNTService(LPCTSTR szServiceName, LPCTSTR szServiceDisplayName, LPCTSTR szServiceExe )
  315. {
  316. DWORD dwLastError;
  317. DWORD dwStatus;
  318. int i;
  319. for ( i=0; i< 5; i++ ) {
  320. /* if service is running, stop it */
  321. dwStatus = SERVICE_GetNTServiceStatus( szServiceName, &dwLastError );
  322. if ( dwStatus == SERVRET_STARTED )
  323. SERVICE_StopNTServiceAndWait( szServiceName, &dwLastError );
  324. /* if service is installed, remove it */
  325. dwStatus = SERVICE_GetNTServiceStatus( szServiceName, &dwLastError );
  326. if ( dwStatus == SERVRET_INSTALLED )
  327. SERVICE_RemoveNTService( szServiceName );
  328. /* try and install the service again */
  329. dwStatus = SERVICE_GetNTServiceStatus( szServiceName, &dwLastError );
  330. if ( dwStatus == SERVRET_REMOVED )
  331. SERVICE_InstallNTService( szServiceName, szServiceDisplayName, szServiceExe );
  332. /* try and start the service again */
  333. dwStatus = SERVICE_GetNTServiceStatus( szServiceName, &dwLastError );
  334. if ( dwStatus == SERVRET_INSTALLED ) {
  335. return NO_ERROR;
  336. }
  337. }
  338. /* if no error reported, force an error */
  339. if ( dwLastError == NO_ERROR )
  340. dwLastError = (DWORD)-1;
  341. return dwLastError;
  342. }