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.

130 lines
2.9 KiB

  1. #include "precomp.h"
  2. #pragma hdrstop
  3. #define SERVICE_NAME TEXT("6to4")
  4. HANDLE g_hServiceStatusHandle = INVALID_HANDLE_VALUE;
  5. SERVICE_STATUS g_ServiceStatus;
  6. VOID
  7. SetHelperServiceStatus(
  8. IN DWORD dwState,
  9. IN DWORD dwErr)
  10. {
  11. BOOL bOk;
  12. Trace1(FSM, _T("Setting state to %d"), dwState);
  13. g_ServiceStatus.dwCurrentState = dwState;
  14. g_ServiceStatus.dwCheckPoint = 0;
  15. g_ServiceStatus.dwWin32ExitCode= dwErr;
  16. #ifndef STANDALONE
  17. bOk = SetServiceStatus(g_hServiceStatusHandle, &g_ServiceStatus);
  18. if (!bOk) {
  19. Trace0(ERR, _T("SetServiceStatus returned failure"));
  20. }
  21. #endif
  22. if (dwState == SERVICE_STOPPED) {
  23. CleanupHelperService();
  24. // Uninitialize tracing and error logging.
  25. UNINITIALIZE_TRACING_LOGGING();
  26. }
  27. }
  28. DWORD
  29. OnStartup()
  30. {
  31. DWORD dwErr;
  32. ENTER_API();
  33. // Initialize tracing and error logging. Continue irrespective of
  34. // success or failure. NOTE: TracePrintf and ReportEvent both have
  35. // built in checks for validity of TRACEID and LOGHANDLE.
  36. INITIALIZE_TRACING_LOGGING();
  37. TraceEnter("OnStartup");
  38. dwErr = StartHelperService();
  39. TraceLeave("OnStartup");
  40. LEAVE_API();
  41. return dwErr;
  42. }
  43. VOID
  44. OnStop(
  45. IN DWORD dwErr)
  46. {
  47. ENTER_API();
  48. TraceEnter("OnStop");
  49. // Make sure we don't try to stop twice.
  50. if ((g_ServiceStatus.dwCurrentState != SERVICE_STOP_PENDING) &&
  51. (g_ServiceStatus.dwCurrentState != SERVICE_STOPPED)) {
  52. StopHelperService(dwErr);
  53. }
  54. TraceLeave("OnStop");
  55. LEAVE_API();
  56. }
  57. ////////////////////////////////////////////////////////////////
  58. // ServiceMain - main entry point called by svchost or by the
  59. // standalone main.
  60. //
  61. #define SERVICE_CONTROL_DDNS_REGISTER 128
  62. VOID WINAPI
  63. ServiceHandler(
  64. IN DWORD dwCode)
  65. {
  66. switch (dwCode) {
  67. case SERVICE_CONTROL_STOP:
  68. OnStop(NO_ERROR);
  69. break;
  70. case SERVICE_CONTROL_PARAMCHANGE:
  71. OnConfigChange();
  72. break;
  73. case SERVICE_CONTROL_DDNS_REGISTER:
  74. OnIpv6AddressChange(NULL, TRUE);
  75. break;
  76. }
  77. }
  78. VOID WINAPI
  79. ServiceMain(
  80. IN ULONG argc,
  81. IN LPWSTR *argv)
  82. {
  83. DWORD dwErr;
  84. #ifndef STANDALONE
  85. g_hServiceStatusHandle = RegisterServiceCtrlHandler(SERVICE_NAME,
  86. ServiceHandler);
  87. // RegisterServiceCtrlHandler returns NULL on failure
  88. if (g_hServiceStatusHandle == NULL) {
  89. return;
  90. }
  91. #endif
  92. ZeroMemory(&g_ServiceStatus, sizeof(g_ServiceStatus));
  93. g_ServiceStatus.dwServiceType = SERVICE_WIN32_SHARE_PROCESS;
  94. g_ServiceStatus.dwControlsAccepted =
  95. SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_PARAMCHANGE;
  96. // Do startup processing
  97. dwErr = OnStartup();
  98. #ifndef STANDALONE
  99. if (dwErr != NO_ERROR) {
  100. OnStop(dwErr);
  101. }
  102. #endif
  103. }