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.

176 lines
4.0 KiB

  1. /*++
  2. Copyright (C) Microsoft Corporation, 1997 - 1999
  3. Module Name:
  4. StartSvc
  5. Abstract:
  6. This module provides a Starter service for Calais.
  7. Author:
  8. Doug Barlow (dbarlow) 2/10/1997
  9. Environment:
  10. Win32, C++
  11. Notes:
  12. --*/
  13. #define __SUBROUTINE__
  14. #ifndef WIN32_LEAN_AND_MEAN
  15. #define WIN32_LEAN_AND_MEAN
  16. #endif
  17. #include <windows.h>
  18. #include <stdio.h>
  19. #include <tchar.h>
  20. #include <winscard.h>
  21. #include <CalMsgs.h>
  22. #include <CalaisLb.h>
  23. /*++
  24. StartCalaisService:
  25. This function starts the Calais service.
  26. Arguments:
  27. None
  28. Return Value:
  29. a DWORD status code. ERROR_SUCCESS implies success.
  30. Throws:
  31. None.
  32. Author:
  33. Doug Barlow (dbarlow) 2/10/1997
  34. --*/
  35. #undef __SUBROUTINE__
  36. #define __SUBROUTINE__ DBGT("StartCalaisService")
  37. DWORD
  38. StartCalaisService(
  39. void)
  40. {
  41. // return ERROR_SERVICE_DISABLED;
  42. SC_HANDLE schService = NULL;
  43. SC_HANDLE schSCManager = NULL;
  44. DWORD dwReturn = ERROR_SUCCESS;
  45. DWORD dwGiveUpCount;
  46. DWORD dwSts;
  47. BOOL fSts;
  48. try
  49. {
  50. SERVICE_STATUS ssStatus; // current status of the service
  51. schSCManager = OpenSCManager(
  52. NULL, // machine (NULL == local)
  53. NULL, // database (NULL == default)
  54. GENERIC_READ); // access required
  55. dwSts = NULL == schSCManager ? GetLastError() : ERROR_SUCCESS;
  56. if (NULL == schSCManager)
  57. throw dwSts;
  58. schService = OpenService(
  59. schSCManager,
  60. CalaisString(CALSTR_PRIMARYSERVICE),
  61. SERVICE_QUERY_STATUS | SERVICE_START);
  62. dwSts = NULL == schService ? GetLastError() : ERROR_SUCCESS;
  63. if (NULL == schService)
  64. throw dwSts;
  65. // try to start the service
  66. fSts = StartService(schService, 0, NULL);
  67. dwSts = !fSts ? GetLastError() : ERROR_SUCCESS;
  68. if (!fSts)
  69. {
  70. dwSts = GetLastError();
  71. switch (dwSts)
  72. {
  73. case ERROR_SERVICE_ALREADY_RUNNING:
  74. break;
  75. default:
  76. throw dwSts;
  77. }
  78. }
  79. dwGiveUpCount = 60;
  80. Sleep(1000);
  81. for (;;)
  82. {
  83. fSts = QueryServiceStatus(schService, &ssStatus);
  84. dwSts = !fSts ? GetLastError() : ERROR_SUCCESS;
  85. if (!fSts)
  86. break;
  87. if (ssStatus.dwCurrentState == SERVICE_START_PENDING)
  88. {
  89. if (0 < --dwGiveUpCount)
  90. Sleep(1000);
  91. else
  92. throw (DWORD)SCARD_E_NO_SERVICE;
  93. }
  94. else
  95. break;
  96. }
  97. if (ssStatus.dwCurrentState != SERVICE_RUNNING)
  98. throw GetLastError();
  99. fSts = CloseServiceHandle(schService);
  100. dwSts = !fSts ? GetLastError() : ERROR_SUCCESS;
  101. schService = NULL;
  102. fSts = CloseServiceHandle(schSCManager);
  103. dwSts = !fSts ? GetLastError() : ERROR_SUCCESS;
  104. schSCManager = NULL;
  105. }
  106. catch (DWORD dwErr)
  107. {
  108. if (NULL != schService)
  109. {
  110. fSts = CloseServiceHandle(schService);
  111. dwSts = !fSts ? GetLastError() : ERROR_SUCCESS;
  112. }
  113. if (NULL != schSCManager)
  114. {
  115. fSts = CloseServiceHandle(schSCManager);
  116. dwSts = !fSts ? GetLastError() : ERROR_SUCCESS;
  117. }
  118. dwReturn = dwErr;
  119. }
  120. catch (...)
  121. {
  122. if (NULL != schService)
  123. {
  124. fSts = CloseServiceHandle(schService);
  125. dwSts = !fSts ? GetLastError() : ERROR_SUCCESS;
  126. }
  127. if (NULL != schSCManager)
  128. {
  129. fSts = CloseServiceHandle(schSCManager);
  130. dwSts = !fSts ? GetLastError() : ERROR_SUCCESS;
  131. }
  132. dwReturn = ERROR_INVALID_PARAMETER;
  133. }
  134. return dwReturn;
  135. }