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.

172 lines
3.7 KiB

  1. #ifndef _SCMMANAGER_HXX_
  2. #define _SCMMANAGER_HXX_
  3. #include <winsvc.h>
  4. //
  5. // WaitHint for SERVICE_START_PENDING
  6. //
  7. const DWORD HTTPFILTER_SERVICE_STARTUP_WAIT_HINT = 180 * 1000; // 3 minutes
  8. //
  9. // WaitHint for SERVICE_STOP_PENDING
  10. //
  11. const DWORD HTTPFILTER_SERVICE_STOP_WAIT_HINT = 180 * 1000; // 3 minutes
  12. class SCM_MANAGER
  13. {
  14. public:
  15. SCM_MANAGER(
  16. const WCHAR * pszServiceName
  17. )
  18. {
  19. _hService = NULL;
  20. _hServiceStopEvent = NULL;
  21. _hServicePauseEvent = NULL;
  22. _hServiceContinueEvent = NULL;
  23. _dwStartupWaitHint = HTTPFILTER_SERVICE_STARTUP_WAIT_HINT;
  24. wcsncpy( _szServiceName,
  25. pszServiceName,
  26. sizeof( _szServiceName ) / sizeof( _szServiceName[0] ) );
  27. _szServiceName[ ( sizeof( _szServiceName ) / sizeof( _szServiceName[0] ) ) - 1 ] = L'\0';
  28. ZeroMemory( &_serviceStatus, sizeof( _serviceStatus ) );
  29. _fInitcsSCMLock = FALSE;
  30. }
  31. virtual
  32. ~SCM_MANAGER();
  33. HRESULT
  34. RunService(
  35. VOID
  36. );
  37. static
  38. VOID
  39. ReportFatalServiceStartupError(
  40. WCHAR * pszServiceName,
  41. DWORD dwError
  42. );
  43. private:
  44. HRESULT
  45. Initialize(
  46. VOID
  47. );
  48. VOID
  49. Terminate(
  50. VOID
  51. );
  52. HRESULT
  53. IndicatePending(
  54. DWORD dwPendingState
  55. );
  56. HRESULT
  57. IndicateComplete(
  58. DWORD dwState,
  59. HRESULT hrErrorToReport = S_OK
  60. );
  61. static
  62. DWORD
  63. WINAPI
  64. GlobalServiceControlHandler(
  65. DWORD dwControl,
  66. DWORD /*dwEventType*/,
  67. LPVOID /*pEventData*/,
  68. LPVOID pServiceContext
  69. );
  70. VOID
  71. UpdateServiceStatus(
  72. BOOL fUpdateCheckpoint = FALSE
  73. );
  74. BOOL
  75. QueryIsInitialized(
  76. VOID
  77. )
  78. {
  79. return _hService != NULL;
  80. }
  81. protected:
  82. //
  83. // child class should implement
  84. // it's specific Start/Stop sequence
  85. // by implementing OnServiceStart() and OnServiceStop()
  86. //
  87. virtual
  88. HRESULT
  89. OnServiceStart(
  90. VOID
  91. ) = NULL;
  92. virtual
  93. HRESULT
  94. OnServiceStop(
  95. VOID
  96. ) = NULL;
  97. DWORD
  98. ControlHandler(
  99. DWORD dwControlCode
  100. );
  101. private:
  102. //
  103. // Not implemented methods
  104. // Declarations present to prevent compiler
  105. // to generate default ones.
  106. //
  107. SCM_MANAGER();
  108. SCM_MANAGER( const SCM_MANAGER& );
  109. SCM_MANAGER& operator=( const SCM_MANAGER& );
  110. //
  111. // _hService, _serviceStatus and _strServiceName
  112. // are used for SCM communication
  113. //
  114. SERVICE_STATUS_HANDLE _hService;
  115. SERVICE_STATUS _serviceStatus;
  116. //
  117. // 256 is the max supported size of the service name
  118. //
  119. WCHAR _szServiceName[ 256 ];
  120. //
  121. // Event is signaled when the service should end
  122. //
  123. HANDLE _hServiceStopEvent;
  124. //
  125. // Event is signaled when the service should pause
  126. //
  127. HANDLE _hServicePauseEvent;
  128. //
  129. // Event is signaled when the service should continue
  130. //
  131. HANDLE _hServiceContinueEvent;
  132. //
  133. // Critical section to synchronize using of _serviceStatus
  134. // _hService with SetServiceStatus() call.
  135. //
  136. CRITICAL_SECTION _csSCMLock;
  137. BOOL _fInitcsSCMLock;
  138. //
  139. // Service start up and stop wait hint
  140. //
  141. DWORD _dwStartupWaitHint;
  142. DWORD _dwStopWaitHint;
  143. };
  144. #endif