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.

137 lines
3.9 KiB

  1. //____________________________________________________________________________
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1995 - 1996.
  5. //
  6. // File: schdsrvc.cxx
  7. //
  8. // Contents:
  9. //
  10. // Notes: Hack around the service not being started when creating tasks
  11. //
  12. // Functions: StartScheduler
  13. //
  14. // History: 2/19/1997 SusiA Cut from MSDN
  15. //
  16. // Notes: This function works for either Win9x or Windows NT.
  17. // If the service is running but paused, does nothing.//
  18. //____________________________________________________________________________
  19. #include "precomp.h"
  20. #undef TRACE
  21. #define TRACE(x) //OutputDebugString(x)
  22. #define MAX_SERVICE_WAIT_TIME 90000 // a minute and a half
  23. #define SCHED_CLASS TEXT("SAGEWINDOWCLASS")
  24. #define SCHED_TITLE TEXT("SYSTEM AGENT COM WINDOW")
  25. #define SCHED_SERVICE_APP_NAME TEXT("mstask.exe")
  26. #define SCHED_SERVICE_NAME TEXT("Schedule")
  27. DWORD StartScheduler()
  28. {
  29. DWORD dwTimeOut;
  30. DWORD dwError;
  31. SC_HANDLE hSC = NULL;
  32. SC_HANDLE hSchSvc = NULL;
  33. hSC = OpenSCManager(NULL, NULL, SC_MANAGER_CONNECT);
  34. if (hSC == NULL)
  35. {
  36. return GetLastError();
  37. }
  38. hSchSvc = OpenService(hSC,
  39. SCHED_SERVICE_NAME,
  40. SERVICE_START | SERVICE_QUERY_STATUS);
  41. CloseServiceHandle(hSC);
  42. if (hSchSvc == NULL)
  43. {
  44. return GetLastError();
  45. }
  46. SERVICE_STATUS SvcStatus;
  47. if (QueryServiceStatus(hSchSvc, &SvcStatus) == FALSE)
  48. {
  49. CloseServiceHandle(hSchSvc);
  50. return GetLastError();
  51. }
  52. if (SvcStatus.dwCurrentState == SERVICE_RUNNING)
  53. {
  54. // The service is already running.
  55. CloseServiceHandle(hSchSvc);
  56. return ERROR_SUCCESS;
  57. }
  58. if (StartService(hSchSvc, 0, NULL) == FALSE)
  59. {
  60. CloseServiceHandle(hSchSvc);
  61. return GetLastError();
  62. }
  63. dwTimeOut = GetTickCount() + MAX_SERVICE_WAIT_TIME;
  64. BOOL bContinue = TRUE;
  65. dwError = ERROR_SERVICE_NEVER_STARTED;
  66. while (bContinue)
  67. {
  68. if (QueryServiceStatus(hSchSvc, &SvcStatus) == FALSE)
  69. {
  70. dwError = GetLastError();
  71. break;
  72. }
  73. switch (SvcStatus.dwCurrentState)
  74. {
  75. // This is good!
  76. case SERVICE_RUNNING:
  77. dwError = ERROR_SUCCESS;
  78. // Fall through
  79. // These are bad
  80. case SERVICE_STOPPED:
  81. case SERVICE_STOP_PENDING:
  82. case SERVICE_PAUSE_PENDING:
  83. case SERVICE_PAUSED:
  84. bContinue = FALSE;
  85. break;
  86. default:
  87. if (GetTickCount() < dwTimeOut)
  88. {
  89. // How long to sleep? According to the SDK use a tenth of the wait hint
  90. // and floor/ceil it between 1 and 10 seconds.
  91. DWORD dwSleep = SvcStatus.dwWaitHint / 10;
  92. if (dwSleep < 1000)
  93. {
  94. dwSleep = 1000;
  95. }
  96. else if (dwSleep > 10000)
  97. {
  98. dwSleep = 10000;
  99. }
  100. TRACE("########## Waiting for Task Scheduler service to be started...\n");
  101. Sleep(dwSleep);
  102. }
  103. else
  104. {
  105. TRACE("########## Starting Task Scheduler service timed out...\n");
  106. bContinue = FALSE;
  107. }
  108. break;
  109. }
  110. }
  111. CloseServiceHandle(hSchSvc);
  112. TRACE("########## Stop waiting for Task Scheduler service to start...\n");
  113. return dwError;
  114. }