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.

142 lines
3.9 KiB

  1. #include <stdafx.h>
  2. #include "Errors.h"
  3. #include "SNMPCtrl.h"
  4. #include "EventCmd.h"
  5. CSNMPController gSNMPController;
  6. CSNMPController::CSNMPController()
  7. {
  8. m_hSNMPService = NULL;
  9. m_hServiceController = NULL;
  10. }
  11. CSNMPController::~CSNMPController()
  12. {
  13. if (m_hSNMPService != NULL)
  14. CloseServiceHandle(m_hSNMPService);
  15. if (m_hServiceController != NULL)
  16. CloseServiceHandle(m_hServiceController);
  17. }
  18. DWORD CSNMPController::LoadSvcHandle()
  19. {
  20. if (m_hSNMPService == NULL)
  21. {
  22. if (m_hServiceController == NULL)
  23. {
  24. m_hServiceController = OpenSCManager(
  25. gCommandLine.m_szSystem,
  26. "ServicesActive",
  27. GENERIC_EXECUTE);
  28. if (m_hServiceController == NULL)
  29. return _E(GetLastError(), IDS_ERR25);
  30. }
  31. m_hSNMPService = OpenService(
  32. m_hServiceController,
  33. "SNMP",
  34. SERVICE_CONTROL_INTERROGATE | SERVICE_START | SERVICE_STOP);
  35. if (m_hSNMPService == NULL)
  36. return _E(GetLastError(), IDS_ERR26);
  37. }
  38. return ERROR_SUCCESS;
  39. }
  40. BOOL CSNMPController::IsSNMPRunning()
  41. {
  42. SERVICE_STATUS snmpStatus;
  43. if (LoadSvcHandle() != ERROR_SUCCESS)
  44. return FALSE;
  45. if (!QueryServiceStatus(m_hSNMPService, &snmpStatus))
  46. return _E(GetLastError(), IDS_ERR27);
  47. _W(WARN_TRACK, IDS_TRCK_WRN49, snmpStatus.dwCurrentState);
  48. return snmpStatus.dwCurrentState == SERVICE_RUNNING;
  49. }
  50. DWORD CSNMPController::StartSNMP()
  51. {
  52. DWORD retCode;
  53. SERVICE_STATUS svcStatus;
  54. DWORD dwRetries;
  55. if ((retCode = LoadSvcHandle()) != ERROR_SUCCESS)
  56. return retCode;
  57. if (!StartService(m_hSNMPService, 0, NULL))
  58. return _E(GetLastError(), IDS_ERR28);
  59. for (dwRetries = 10; dwRetries > 0; dwRetries--)
  60. {
  61. printf("."); fflush(stdout);
  62. if (!QueryServiceStatus(m_hSNMPService, &svcStatus))
  63. return _E(GetLastError(), IDS_ERR29);
  64. if (svcStatus.dwCurrentState == SERVICE_RUNNING)
  65. break;
  66. if (svcStatus.dwCurrentState == SERVICE_START_PENDING)
  67. {
  68. if (svcStatus.dwWaitHint < 200)
  69. svcStatus.dwWaitHint = 200;
  70. if (svcStatus.dwWaitHint > 1000)
  71. svcStatus.dwWaitHint = 1000;
  72. Sleep(svcStatus.dwWaitHint);
  73. }
  74. else
  75. return _E(ERROR_INVALID_STATE, IDS_ERR06, svcStatus.dwWaitHint);
  76. }
  77. printf("\n");
  78. return retCode;
  79. }
  80. DWORD CSNMPController::StopSNMP()
  81. {
  82. DWORD retCode;
  83. SERVICE_STATUS svcStatus;
  84. DWORD dwRetries;
  85. if ((retCode = LoadSvcHandle()) != ERROR_SUCCESS)
  86. return retCode;
  87. if (!ControlService(m_hSNMPService, SERVICE_CONTROL_STOP, &svcStatus))
  88. {
  89. retCode = GetLastError();
  90. if (retCode == ERROR_SERVICE_NOT_ACTIVE)
  91. {
  92. _W(WARN_TRACK, IDS_TRCK_WRN50);
  93. return ERROR_SUCCESS;
  94. }
  95. return _E(GetLastError(), IDS_ERR30);
  96. }
  97. for (dwRetries = 10; dwRetries > 0; dwRetries--)
  98. {
  99. printf("."); fflush(stdout);
  100. if (!QueryServiceStatus(m_hSNMPService, &svcStatus))
  101. return _E(GetLastError(), IDS_ERR31);
  102. if (svcStatus.dwCurrentState == SERVICE_STOPPED)
  103. break;
  104. if (svcStatus.dwCurrentState == SERVICE_STOP_PENDING)
  105. {
  106. if (svcStatus.dwWaitHint < 200)
  107. svcStatus.dwWaitHint = 200;
  108. if (svcStatus.dwWaitHint > 1000)
  109. svcStatus.dwWaitHint = 1000;
  110. Sleep(svcStatus.dwWaitHint);
  111. }
  112. else
  113. return _E(ERROR_INVALID_STATE, IDS_ERR06, svcStatus.dwWaitHint);
  114. }
  115. printf("\n");
  116. if (dwRetries == 0)
  117. return _E(ERROR_TIMEOUT, IDS_ERR07);
  118. return retCode;
  119. }