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.

175 lines
3.2 KiB

  1. /*++
  2. Copyright (c) 2002 Microsoft Corporation
  3. Module Name :
  4. service.cpp
  5. Abstract:
  6. Functions to control the SCM
  7. Author:
  8. Christopher Achille (cachille)
  9. Project:
  10. URLScan Update
  11. Revision History:
  12. March 2002: Created
  13. --*/
  14. #include "stdafx.h"
  15. #include "windows.h"
  16. #include "service.h"
  17. // StopService
  18. //
  19. // Stop a service, given a handle to it
  20. //
  21. BOOL
  22. StopService( SC_HANDLE hService )
  23. {
  24. SERVICE_STATUS svcStatus;
  25. DWORD dwTimeWaited = 0;
  26. if ( !ControlService( hService, SERVICE_CONTROL_STOP, &svcStatus ))
  27. {
  28. // Could not send stop message
  29. return FALSE;
  30. }
  31. while ( QueryServiceStatus( hService, & svcStatus ) &&
  32. ( svcStatus.dwCurrentState == SERVICE_STOP_PENDING ) &&
  33. ( dwTimeWaited < SERVICE_MAXWAIT )
  34. )
  35. {
  36. Sleep( SERVICE_INTERVALCHECK );
  37. dwTimeWaited += SERVICE_INTERVALCHECK;
  38. }
  39. return svcStatus.dwCurrentState == SERVICE_STOPPED;
  40. }
  41. // Stop the webService
  42. //
  43. // Stops the web service, and also tells us if it was running
  44. //
  45. // Paramters:
  46. // bWasRunning - TRUE == web service was running before it was stopped
  47. // FALSE == it was not running, so it did not need to be stopped
  48. //
  49. // Return Values:
  50. // TRUE - Successfully stopped
  51. // FALSE - Could not be stopped
  52. //
  53. BOOL
  54. StopWebSerivce( BOOL *bWasRunning )
  55. {
  56. SC_HANDLE hManager = NULL;
  57. SC_HANDLE hService = NULL;
  58. SERVICE_STATUS svcStatus;
  59. BOOL bRunning = FALSE;
  60. BOOL bRet = TRUE;
  61. // ASSERT( bWasRunning );
  62. hManager = OpenSCManager( NULL, NULL, GENERIC_ALL );
  63. if ( hManager == NULL )
  64. {
  65. // Could not open SCM
  66. return FALSE;
  67. }
  68. hService = ::OpenService( hManager, SERVICE_NAME_WEB, GENERIC_ALL );
  69. if ( hService == NULL )
  70. {
  71. // could not open Service
  72. CloseServiceHandle( hManager );
  73. return FALSE;
  74. }
  75. if ( QueryServiceStatus( hService, &svcStatus ))
  76. {
  77. bRunning = svcStatus.dwCurrentState != SERVICE_STOPPED;
  78. if ( bRunning )
  79. {
  80. // If it is running, try to stop it
  81. bRet = StopService( hService );
  82. }
  83. }
  84. else
  85. {
  86. bRet = FALSE;
  87. }
  88. if ( bRet )
  89. {
  90. *bWasRunning = bRunning;
  91. }
  92. if ( hManager )
  93. {
  94. CloseServiceHandle( hManager );
  95. }
  96. if ( hService )
  97. {
  98. CloseServiceHandle( hService );
  99. }
  100. return bRet;
  101. }
  102. // StartWebService
  103. //
  104. // Start the WebSerivce
  105. //
  106. // Return
  107. // TRUE - Successfuly started
  108. // FALSE - Unsuccessful, not started
  109. BOOL
  110. StartWebSerivce()
  111. {
  112. SC_HANDLE hManager = NULL;
  113. SC_HANDLE hService = NULL;
  114. BOOL bRet = FALSE;
  115. hManager = OpenSCManager( NULL, NULL, GENERIC_ALL );
  116. if ( hManager == NULL )
  117. {
  118. // Could not open SCM
  119. return FALSE;
  120. }
  121. hService = ::OpenService( hManager, SERVICE_NAME_WEB, GENERIC_ALL );
  122. if ( hService )
  123. {
  124. if ( StartService( hService, 0, NULL ) )
  125. {
  126. bRet = TRUE;
  127. }
  128. }
  129. if ( hManager )
  130. {
  131. CloseServiceHandle( hManager );
  132. }
  133. if ( hService )
  134. {
  135. CloseServiceHandle( hService );
  136. }
  137. return bRet;
  138. }