Source code of Windows XP (NT5)
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.

148 lines
3.1 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. Set6to4ServiceStatus(
  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. Cleanup6to4();
  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 = Start6to4();
  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. Set6to4ServiceStatus(SERVICE_STOP_PENDING, dwErr);
  53. Stop6to4();
  54. }
  55. TraceLeave("OnStop");
  56. LEAVE_API();
  57. }
  58. ////////////////////////////////////////////////////////////////
  59. // ServiceMain - main entry point called by svchost or by the
  60. // standalone main.
  61. //
  62. #define SERVICE_CONTROL_DDNS_REGISTER 128
  63. VOID WINAPI
  64. ServiceHandler(
  65. IN DWORD dwCode)
  66. {
  67. switch (dwCode) {
  68. case SERVICE_CONTROL_STOP:
  69. OnStop(NO_ERROR);
  70. break;
  71. case SERVICE_CONTROL_PARAMCHANGE:
  72. OnConfigChange();
  73. break;
  74. case SERVICE_CONTROL_DDNS_REGISTER:
  75. OnIpv6AddressChange(NULL, TRUE);
  76. break;
  77. }
  78. }
  79. VOID WINAPI
  80. ServiceMain(
  81. IN ULONG argc,
  82. IN LPWSTR *argv)
  83. {
  84. DWORD dwErr = NO_ERROR;
  85. #ifndef STANDALONE
  86. g_hServiceStatusHandle = RegisterServiceCtrlHandler(SERVICE_NAME,
  87. ServiceHandler);
  88. // RegisterServiceCtrlHandler returns NULL on failure
  89. if (g_hServiceStatusHandle == NULL) {
  90. dwErr = GetLastError();
  91. goto Error;
  92. }
  93. #endif
  94. ZeroMemory(&g_ServiceStatus, sizeof(g_ServiceStatus));
  95. g_ServiceStatus.dwServiceType =
  96. SERVICE_WIN32_SHARE_PROCESS | SERVICE_INTERACTIVE_PROCESS;
  97. g_ServiceStatus.dwControlsAccepted =
  98. SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_PARAMCHANGE;
  99. Set6to4ServiceStatus(SERVICE_START_PENDING, NO_ERROR);
  100. // Do startup processing
  101. dwErr = OnStartup();
  102. if (dwErr) {
  103. goto Error;
  104. }
  105. #ifndef STANDALONE
  106. Set6to4ServiceStatus(SERVICE_RUNNING, NO_ERROR);
  107. // Wait until shutdown time
  108. return;
  109. #endif
  110. Error:
  111. #ifndef STANDALONE
  112. OnStop(dwErr);
  113. #endif
  114. return;
  115. }