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.

163 lines
3.6 KiB

  1. /*++
  2. Copyright (C) Microsoft Corporation, 1991 - 1999
  3. Module Name:
  4. startsvc.c
  5. Abstract:
  6. This routine implements on-demand starting of the RpcSs service.
  7. Author:
  8. Bharat Shah (barats) 4-5-92
  9. Revision History:
  10. --*/
  11. #include <sysinc.h>
  12. #include <rpc.h>
  13. #include <winsvc.h>
  14. #include <startsvc.h>
  15. #define SUCCESS 0
  16. const RPC_CHAR *pRPCEPMPR = RPC_CONST_STRING("RPCSS");
  17. RPC_STATUS RPC_ENTRY
  18. StartServiceIfNecessary(
  19. void
  20. )
  21. /*++
  22. Routine Description:
  23. If the rpcss service has not yet been started, then we attempt to
  24. start it.
  25. Returns:
  26. RPC_S_OK - The rpcss service is running.
  27. Service controller errors.
  28. --*/
  29. {
  30. SC_HANDLE hServiceController = NULL;
  31. SC_HANDLE hService = NULL;
  32. SERVICE_STATUS ServiceStatus;
  33. DWORD status;
  34. DWORD Counter = 0L;
  35. BOOL FirstTime = TRUE;
  36. unsigned long ArgC = 0;
  37. RPC_CHAR PAPI * ArgV[1] = { NULL };
  38. //
  39. // Get a handle to the service controller.
  40. //
  41. hServiceController = OpenSCManager(
  42. NULL,
  43. NULL,
  44. GENERIC_READ);
  45. if (hServiceController == NULL)
  46. {
  47. status = GetLastError();
  48. return(status);
  49. }
  50. //
  51. // Get a handle to the service
  52. //
  53. hService = OpenService(
  54. hServiceController,
  55. pRPCEPMPR,
  56. GENERIC_READ|SERVICE_START);
  57. if (hService == NULL)
  58. {
  59. status = GetLastError();
  60. goto CleanExit;
  61. }
  62. //
  63. // Call StartService
  64. //
  65. /*
  66. if (!StartService(hService,ArgC,ArgV))
  67. {
  68. status = GetLastError();
  69. if (status == ERROR_SERVICE_ALREADY_RUNNING)
  70. status = RPC_S_OK;
  71. goto CleanExit;
  72. }
  73. */
  74. do
  75. {
  76. if (!QueryServiceStatus(hService,&ServiceStatus))
  77. {
  78. status = GetLastError();
  79. goto CleanExit;
  80. }
  81. switch(ServiceStatus.dwCurrentState)
  82. {
  83. case SERVICE_RUNNING:
  84. status = SUCCESS;
  85. goto CleanExit;
  86. break;
  87. case SERVICE_STOP_PENDING:
  88. case SERVICE_START_PENDING:
  89. if (!FirstTime && (Counter == ServiceStatus.dwCheckPoint))
  90. {
  91. status = ERROR_SERVICE_REQUEST_TIMEOUT;
  92. goto CleanExit;
  93. }
  94. else
  95. {
  96. FirstTime = FALSE;
  97. Counter = ServiceStatus.dwCheckPoint;
  98. Sleep(ServiceStatus.dwWaitHint);
  99. }
  100. break;
  101. case SERVICE_STOPPED:
  102. if (!StartService(hService, ArgC, ArgV))
  103. {
  104. status = GetLastError();
  105. if (status == ERROR_SERVICE_ALREADY_RUNNING)
  106. status = RPC_S_OK;
  107. goto CleanExit;
  108. }
  109. Sleep(500);
  110. break;
  111. default:
  112. status = GetLastError();
  113. goto CleanExit;
  114. break;
  115. }
  116. }
  117. while (TRUE);
  118. CleanExit:
  119. if(hServiceController != NULL) {
  120. (VOID) CloseServiceHandle(hServiceController);
  121. }
  122. if(hService != NULL) {
  123. (VOID) CloseServiceHandle(hService);
  124. }
  125. return(status);
  126. }