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.

156 lines
6.8 KiB

  1. // --------------------------------------------------------------------------
  2. // Module Name: Service.h
  3. //
  4. // Copyright (c) 2000, Microsoft Corporation
  5. //
  6. // This file contains a class that implements generic portions of a Win32
  7. // serivce.
  8. //
  9. // History: 2000-11-29 vtan created
  10. // --------------------------------------------------------------------------
  11. #ifndef _Service_
  12. #define _Service_
  13. #include "APIConnection.h"
  14. // --------------------------------------------------------------------------
  15. // CService
  16. //
  17. // Purpose: Base class implementation of a service for the service control
  18. // manager.
  19. //
  20. // History: 2000-11-29 vtan created
  21. // --------------------------------------------------------------------------
  22. class CService : public CCountedObject
  23. {
  24. private:
  25. friend class CServiceWorkItem;
  26. CService (void);
  27. protected:
  28. CService (CAPIConnection *pAPIConnection, CServerAPI *pServerAPI, const TCHAR *pszServiceName);
  29. virtual ~CService (void);
  30. public:
  31. static BOOL IsValid(CService* pService);
  32. void Start (void);
  33. static NTSTATUS Install (const TCHAR *pszName,
  34. const TCHAR *pszImage,
  35. const TCHAR *pszGroup,
  36. const TCHAR *pszAccount,
  37. const TCHAR *pszDllName,
  38. const TCHAR *pszDependencies,
  39. const TCHAR *pszSvchostGroup,
  40. const TCHAR *pszServiceMainName,
  41. DWORD dwStartType,
  42. HINSTANCE hInstance,
  43. UINT uiDisplayNameID,
  44. UINT uiDescriptionID,
  45. SERVICE_FAILURE_ACTIONS *psfa = NULL);
  46. static NTSTATUS Remove (const TCHAR *pszName);
  47. protected:
  48. virtual NTSTATUS SignalStartStop (BOOL fStart /* FALSE == stop/shutdown */);
  49. virtual DWORD HandlerEx (DWORD dwControl);
  50. private:
  51. static DWORD WINAPI CB_HandlerEx (DWORD dwControl, DWORD dwEventType, LPVOID lpEventData, LPVOID lpContext);
  52. static NTSTATUS AddService (const TCHAR *pszName,
  53. const TCHAR *pszImage,
  54. const TCHAR *pszGroup,
  55. const TCHAR *pszAccount,
  56. const TCHAR *pszDependencies,
  57. DWORD dwStartType,
  58. HINSTANCE hInstance,
  59. UINT uiDisplayNameID,
  60. SERVICE_FAILURE_ACTIONS *psfa = NULL);
  61. static NTSTATUS AddServiceDescription (const TCHAR *pszName, HINSTANCE hInstance, UINT uiDescriptionID);
  62. static NTSTATUS AddServiceParameters (const TCHAR *pszName, const TCHAR *pszDllName, const TCHAR *pszServiceMainName);
  63. static NTSTATUS AddServiceToGroup (const TCHAR *pszName, const TCHAR *pszSvchostGroup);
  64. static bool StringInMulitpleStringList (const TCHAR *pszStringList, const TCHAR *pszString);
  65. static void StringInsertInMultipleStringList (TCHAR *pszStringList, const TCHAR *pszString, DWORD dwStringListSize);
  66. protected:
  67. #define CSVC_TAG "CSVC"
  68. #define DEAD_CSVC_TAG "DEAD"
  69. #define CB_CSVC_TAG 4
  70. CHAR _szTag[CB_CSVC_TAG];
  71. SERVICE_STATUS_HANDLE _hService;
  72. SERVICE_STATUS _serviceStatus;
  73. const TCHAR* _pszServiceName;
  74. CAPIConnection* _pAPIConnection;
  75. CAPIDispatchSync* _pAPIDispatchSync;
  76. CServerAPI* _pServerAPI;
  77. };
  78. // --------------------------------------------------------------------------
  79. // CServiceWorkItem
  80. //
  81. // Purpose: Work item class to stop the server using the CServerAPI class.
  82. //
  83. // History: 2000-11-29 vtan created
  84. // --------------------------------------------------------------------------
  85. class CServiceWorkItem : public CWorkItem
  86. {
  87. private:
  88. CServiceWorkItem (void);
  89. public:
  90. CServiceWorkItem (CServerAPI *pServerAPI);
  91. virtual ~CServiceWorkItem (void);
  92. protected:
  93. virtual void Entry (void);
  94. private:
  95. CServerAPI* _pServerAPI;
  96. };
  97. // --------------------------------------------------------------------------
  98. // Debug wrap of SetServiceStatus
  99. // --------------------------------------------------------------------------
  100. // uncomment to activate:
  101. // #define DEBUG_SERVICE_STATUS
  102. #ifdef DEBUG_SERVICE_STATUS
  103. BOOL _DebugSetServiceStatus(
  104. SERVICE_STATUS_HANDLE h, LPSERVICE_STATUS pStatus, CService* pService )
  105. {
  106. char* pszStatus = NULL;
  107. switch(pStatus->dwCurrentState)
  108. {
  109. #define ASSIGN_STATUS_STRING(scs) case scs: pszStatus = #scs; break
  110. ASSIGN_STATUS_STRING(SERVICE_CONTINUE_PENDING);
  111. ASSIGN_STATUS_STRING(SERVICE_PAUSE_PENDING);
  112. ASSIGN_STATUS_STRING(SERVICE_PAUSED);
  113. ASSIGN_STATUS_STRING(SERVICE_RUNNING);
  114. ASSIGN_STATUS_STRING(SERVICE_START_PENDING);
  115. ASSIGN_STATUS_STRING(SERVICE_STOP_PENDING);
  116. ASSIGN_STATUS_STRING(SERVICE_STOPPED);
  117. }
  118. CHAR szMsg[512];
  119. StringCchPrintfA(szMsg, ARRAYSIZE(szMsg),
  120. "\nCService (%08lx) assigning dwCurrentState: %s, dwWin32ExitCode: %08lX\n",
  121. pService, pszStatus, pStatus->dwWin32ExitCode);
  122. DISPLAYMSG(szMsg);
  123. return SetServiceStatus(h, pStatus);
  124. }
  125. # define _SetServiceStatus(h, pStatus, pService) _DebugSetServiceStatus(h, pStatus, pService)
  126. #else // DEBUG_SERVICE_STATUS
  127. # define _SetServiceStatus(h, pStatus, pService) SetServiceStatus(h, pStatus)
  128. #endif // DEBUG_SERVICE_STATUS
  129. #endif /* _Service_ */