service.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  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. #ifdef HAVE_CONFIG_H
  39. # include <config.h>
  40. #endif
  41. #include <windows.h>
  42. #include "nt/ntos.h"
  43. #define SERVRET_ERROR 0
  44. #define SERVRET_INSTALLED 1
  45. #define SERVRET_STARTING 2
  46. #define SERVRET_STARTED 3
  47. #define SERVRET_STOPPING 4
  48. #define SERVRET_REMOVED 5
  49. DWORD NS_WINAPI
  50. SERVICE_GetNTServiceStatus(LPCTSTR szServiceName, LPDWORD lpLastError )
  51. {
  52. SERVICE_STATUS ServiceStatus;
  53. SC_HANDLE schService = NULL;
  54. SC_HANDLE schSCManager = NULL;
  55. DWORD lastError = 0;
  56. int ret = 0;
  57. //ereport(LOG_INFORM, "open SC Manager");
  58. if ((schSCManager = OpenSCManager(
  59. NULL, // machine (NULL == local)
  60. NULL, // database (NULL == default)
  61. SC_MANAGER_ALL_ACCESS // access required
  62. )) == NULL ) {
  63. lastError = GetLastError();
  64. ret = SERVRET_ERROR;
  65. goto finish;
  66. }
  67. schService = OpenService(schSCManager, szServiceName, SERVICE_ALL_ACCESS);
  68. if (schService == NULL ) {
  69. lastError = GetLastError();
  70. if (lastError == ERROR_SERVICE_DOES_NOT_EXIST) {
  71. lastError = 0;
  72. ret = SERVRET_REMOVED;
  73. } else
  74. ret = SERVRET_ERROR;
  75. goto finish;
  76. }
  77. ret = ControlService(schService, SERVICE_CONTROL_INTERROGATE, &ServiceStatus);
  78. if ( !ret ) {
  79. lastError = GetLastError();
  80. if ( lastError == ERROR_SERVICE_NOT_ACTIVE ) {
  81. lastError = 0;
  82. ret = SERVRET_INSTALLED;
  83. } else
  84. ret = SERVRET_ERROR;
  85. goto finish;
  86. }
  87. switch ( ServiceStatus.dwCurrentState ) {
  88. case SERVICE_STOPPED: ret = SERVRET_INSTALLED; break;
  89. case SERVICE_START_PENDING: ret = SERVRET_STARTING; break;
  90. case SERVICE_STOP_PENDING: ret = SERVRET_STOPPING; break;
  91. case SERVICE_RUNNING: ret = SERVRET_STARTED; break;
  92. case SERVICE_CONTINUE_PENDING: ret = SERVRET_STARTED; break;
  93. case SERVICE_PAUSE_PENDING: ret = SERVRET_STARTED; break;
  94. case SERVICE_PAUSED: ret = SERVRET_STARTED; break;
  95. default: ret = SERVRET_ERROR; break;
  96. }
  97. finish:
  98. if ( schService)
  99. CloseServiceHandle(schService);
  100. if ( schSCManager)
  101. CloseServiceHandle(schSCManager);
  102. if ( lpLastError )
  103. *lpLastError = lastError;
  104. return ret;
  105. }
  106. DWORD NS_WINAPI
  107. SERVICE_InstallNTService(LPCTSTR szServiceName, LPCTSTR szServiceDisplayName, LPCTSTR szServiceExe )
  108. {
  109. LPCTSTR lpszBinaryPathName = szServiceExe;
  110. SC_HANDLE schService = NULL;
  111. SC_HANDLE schSCManager = NULL;
  112. int lastError = 0;
  113. int ret = 0;
  114. //ereport(LOG_INFORM, "open SC Manager");
  115. if ((schSCManager = OpenSCManager(
  116. NULL, // machine (NULL == local)
  117. NULL, // database (NULL == default)
  118. SC_MANAGER_ALL_ACCESS // access required
  119. )) == NULL ) {
  120. lastError = GetLastError();
  121. goto finish;
  122. }
  123. /* check if service already exists */
  124. schService = OpenService( schSCManager,
  125. szServiceName,
  126. SERVICE_ALL_ACCESS
  127. );
  128. if (schService) {
  129. lastError = ERROR_SERVICE_EXISTS;
  130. goto finish;
  131. }
  132. schService = CreateService(
  133. schSCManager, // SCManager database
  134. szServiceName, // name of service
  135. szServiceDisplayName, // name to display
  136. SERVICE_ALL_ACCESS, // desired access
  137. SERVICE_WIN32_OWN_PROCESS |
  138. SERVICE_INTERACTIVE_PROCESS, // service type
  139. SERVICE_AUTO_START, //SERVICE_DEMAND_START, // start type
  140. SERVICE_ERROR_NORMAL, // error control type
  141. lpszBinaryPathName, // service's binary
  142. NULL, // no load ordering group
  143. NULL, // no tag identifier
  144. NULL, // no dependencies
  145. NULL, // LocalSystem account
  146. NULL); // no password
  147. if (schService == NULL) {
  148. lastError = GetLastError();
  149. }
  150. // successfully installed service
  151. finish:
  152. if ( schService)
  153. CloseServiceHandle(schService);
  154. if ( schSCManager)
  155. CloseServiceHandle(schSCManager);
  156. return lastError;
  157. }
  158. DWORD NS_WINAPI
  159. SERVICE_RemoveNTService(LPCTSTR szServiceName)
  160. {
  161. SC_HANDLE schService = NULL;
  162. SC_HANDLE schSCManager = NULL;
  163. int lastError = 0;
  164. int ret = 0;
  165. //ereport(LOG_INFORM, "open SC Manager");
  166. if ((schSCManager = OpenSCManager(
  167. NULL, // machine (NULL == local)
  168. NULL, // database (NULL == default)
  169. SC_MANAGER_ALL_ACCESS // access required
  170. )) == NULL ) {
  171. lastError = GetLastError();
  172. goto finish;
  173. }
  174. schService = OpenService(schSCManager, szServiceName, SERVICE_ALL_ACCESS);
  175. if (schService == NULL ) {
  176. lastError = GetLastError();
  177. goto finish;
  178. }
  179. ret = DeleteService(schService);
  180. if ( !ret) {
  181. lastError = GetLastError();
  182. goto finish;
  183. }
  184. // successfully removed service
  185. finish:
  186. if ( schService)
  187. CloseServiceHandle(schService);
  188. if ( schSCManager)
  189. CloseServiceHandle(schSCManager);
  190. return lastError;
  191. }
  192. DWORD NS_WINAPI
  193. SERVICE_StartNTService(LPCTSTR szServiceName)
  194. {
  195. SC_HANDLE schService = NULL;
  196. SC_HANDLE schSCManager = NULL;
  197. int lastError = 0;
  198. int ret = 0;
  199. //ereport(LOG_INFORM, "open SC Manager");
  200. if ((schSCManager = OpenSCManager(
  201. NULL, // machine (NULL == local)
  202. NULL, // database (NULL == default)
  203. SC_MANAGER_ALL_ACCESS // access required
  204. )) == NULL ) {
  205. lastError = GetLastError();
  206. goto finish;
  207. }
  208. schService = OpenService(schSCManager, szServiceName, SERVICE_ALL_ACCESS);
  209. if (schService == NULL ) {
  210. lastError = GetLastError();
  211. goto finish;
  212. }
  213. ret = StartService(schService, 0, NULL);
  214. if ( !ret ) {
  215. lastError = GetLastError();
  216. goto finish;
  217. }
  218. // successfully started service
  219. finish:
  220. if ( schService)
  221. CloseServiceHandle(schService);
  222. if ( schSCManager)
  223. CloseServiceHandle(schSCManager);
  224. return lastError;
  225. }
  226. DWORD NS_WINAPI
  227. SERVICE_StartNTServiceAndWait(LPCTSTR szServiceName, LPDWORD lpdwLastError)
  228. {
  229. DWORD dwLastError;
  230. DWORD dwStatus;
  231. int i;
  232. /* check if service is running */
  233. dwStatus = SERVICE_GetNTServiceStatus( szServiceName, &dwLastError );
  234. if ( dwStatus == SERVRET_STARTED )
  235. return TRUE;
  236. dwLastError = SERVICE_StartNTService( szServiceName );
  237. if ( dwLastError != 0 ) {
  238. goto errorExit;
  239. }
  240. for ( i=0; i<5; i++ ) {
  241. // make sure the service got installed
  242. dwStatus = SERVICE_GetNTServiceStatus( szServiceName, &dwLastError );
  243. if ( dwStatus == SERVRET_ERROR) {
  244. if ( dwLastError != ERROR_SERVICE_CANNOT_ACCEPT_CTRL )
  245. goto errorExit;
  246. } else if ( dwStatus == SERVRET_STARTED )
  247. return TRUE;
  248. Sleep ( 1000 );
  249. }
  250. dwLastError = 0;
  251. errorExit:
  252. if ( lpdwLastError )
  253. *lpdwLastError = dwLastError;
  254. return FALSE;
  255. }
  256. DWORD NS_WINAPI
  257. SERVICE_StopNTService(LPCTSTR szServiceName)
  258. {
  259. SC_HANDLE schService = NULL;
  260. SC_HANDLE schSCManager = NULL;
  261. int lastError = 0;
  262. int ret = 0;
  263. SERVICE_STATUS ServiceStatus;
  264. //ereport(LOG_INFORM, "open SC Manager");
  265. if ((schSCManager = OpenSCManager(
  266. NULL, // machine (NULL == local)
  267. NULL, // database (NULL == default)
  268. SC_MANAGER_ALL_ACCESS // access required
  269. )) == NULL ) {
  270. lastError = GetLastError();
  271. goto finish;
  272. }
  273. schService = OpenService(schSCManager, szServiceName, SERVICE_ALL_ACCESS);
  274. if (schService == NULL ) {
  275. lastError = GetLastError();
  276. goto finish;
  277. }
  278. ret = ControlService(schService, SERVICE_CONTROL_STOP, &ServiceStatus);
  279. if ( !ret ) {
  280. lastError = GetLastError();
  281. goto finish;
  282. }
  283. // server is stopping
  284. finish:
  285. if ( schService)
  286. CloseServiceHandle(schService);
  287. if ( schSCManager)
  288. CloseServiceHandle(schSCManager);
  289. return lastError;
  290. }
  291. DWORD NS_WINAPI
  292. SERVICE_StopNTServiceAndWait(LPCTSTR szServiceName, LPDWORD lpdwLastError)
  293. {
  294. DWORD dwLastError;
  295. DWORD dwStatus;
  296. int i;
  297. /* check if service is running */
  298. dwStatus = SERVICE_GetNTServiceStatus( szServiceName, &dwLastError );
  299. if ( dwStatus != SERVRET_STARTED )
  300. return TRUE;
  301. for ( i=0; i<30; i++ ) {
  302. dwLastError = SERVICE_StopNTService( szServiceName );
  303. Sleep ( 1000 );
  304. // make sure the service is stoppped and just installed
  305. dwStatus = SERVICE_GetNTServiceStatus( szServiceName, &dwLastError );
  306. Sleep ( 1000 );
  307. if ( dwStatus == SERVRET_INSTALLED ) {
  308. Sleep ( 1000 );
  309. return TRUE;
  310. }
  311. }
  312. if ( lpdwLastError )
  313. *lpdwLastError = dwLastError;
  314. return FALSE;
  315. }
  316. DWORD NS_WINAPI
  317. SERVICE_ReinstallNTService(LPCTSTR szServiceName, LPCTSTR szServiceDisplayName, LPCTSTR szServiceExe )
  318. {
  319. DWORD dwLastError;
  320. DWORD dwStatus;
  321. int i;
  322. for ( i=0; i< 5; i++ ) {
  323. /* if service is running, stop it */
  324. dwStatus = SERVICE_GetNTServiceStatus( szServiceName, &dwLastError );
  325. if ( dwStatus == SERVRET_STARTED )
  326. SERVICE_StopNTServiceAndWait( szServiceName, &dwLastError );
  327. /* if service is installed, remove it */
  328. dwStatus = SERVICE_GetNTServiceStatus( szServiceName, &dwLastError );
  329. if ( dwStatus == SERVRET_INSTALLED )
  330. SERVICE_RemoveNTService( szServiceName );
  331. /* try and install the service again */
  332. dwStatus = SERVICE_GetNTServiceStatus( szServiceName, &dwLastError );
  333. if ( dwStatus == SERVRET_REMOVED )
  334. SERVICE_InstallNTService( szServiceName, szServiceDisplayName, szServiceExe );
  335. /* try and start the service again */
  336. dwStatus = SERVICE_GetNTServiceStatus( szServiceName, &dwLastError );
  337. if ( dwStatus == SERVRET_INSTALLED ) {
  338. return NO_ERROR;
  339. }
  340. }
  341. /* if no error reported, force an error */
  342. if ( dwLastError == NO_ERROR )
  343. dwLastError = (DWORD)-1;
  344. return dwLastError;
  345. }