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.

175 lines
4.6 KiB

  1. /*++
  2. Copyright (c) 1991-1993 Microsoft Corporation
  3. Module Name:
  4. Started.c
  5. Abstract:
  6. NetpIsServiceStarted() routine.
  7. Author:
  8. Rita Wong (ritaw) 10-May-1991
  9. Environment:
  10. User Mode - Win32
  11. Revision History:
  12. 10-May-1991 RitaW
  13. Created routine for wksta APIs.
  14. 24-Jul-1991 JohnRo
  15. Provide NetpIsServiceStarted() in NetLib for use by <netrpc.h> macros.
  16. Fixed bug (was using wrong field). Also, this code should use
  17. NetApiBufferFree() instead of LocalFree().
  18. 16-Sep-1991 JohnRo
  19. Made changes suggested by PC-LINT.
  20. 18-Feb-1992 RitaW
  21. Converted to use the Win32 service control APIs.
  22. 06-Mar-1992 JohnRo
  23. Fixed bug checking current state values.
  24. 02-Nov-1992 JohnRo
  25. Added NetpIsRemoteServiceStarted().
  26. 30-Jun-1993 JohnRo
  27. Use NetServiceGetInfo instead of NetServiceControl (much faster).
  28. Use NetpKdPrint() where possible.
  29. Use PREFIX_ and FORMAT_ equates.
  30. Made changes suggested by PC-LINT 5.0
  31. --*/
  32. // These must be included first:
  33. #include <nt.h> // (Only needed by NT version of netlib.h)
  34. #include <ntrtl.h>
  35. #include <nturtl.h>
  36. #include <windef.h> // IN, BOOL, LPTSTR, etc.
  37. #include <winbase.h>
  38. #include <winsvc.h> // Win32 service control APIs
  39. #include <lmcons.h> // NET_API_STATUS (needed by netlib.h et al).
  40. // These may be included in any order:
  41. #include <lmapibuf.h> // NetApiBufferFree().
  42. #include <lmerr.h> // NERR_Success.
  43. #include <lmsvc.h> // LPSERVICE_INFO_2, etc.
  44. #include <netlib.h> // My prototypes.
  45. #include <netdebug.h> // NetpKdPrint(), NetpAssert(), FORMAT_ equates.
  46. #include <prefix.h> // PREFIX_ equates.
  47. #include <tstr.h> // TCHAR_EOS.
  48. BOOL
  49. NetpIsServiceStarted(
  50. IN LPWSTR ServiceName
  51. )
  52. /*++
  53. Routine Description:
  54. This routine queries the Service Controller to find out if the
  55. specified service has been started.
  56. Arguments:
  57. ServiceName - Supplies the name of the service.
  58. Return Value:
  59. Returns TRUE if the specified service has been started; otherwise
  60. returns FALSE. This routine returns FALSE if it got an error
  61. from calling the Service Controller.
  62. --*/
  63. {
  64. NET_API_STATUS ApiStatus;
  65. SC_HANDLE hScManager;
  66. SC_HANDLE hService;
  67. SERVICE_STATUS ServiceStatus;
  68. if ((hScManager = OpenSCManager(
  69. NULL,
  70. NULL,
  71. SC_MANAGER_CONNECT
  72. )) == (SC_HANDLE) NULL) {
  73. #if DBG
  74. ApiStatus = (NET_API_STATUS) GetLastError();
  75. KdPrintEx((DPFLTR_NETAPI_ID,
  76. DPFLTR_WARNING_LEVEL,
  77. PREFIX_NETLIB
  78. "NetpIsServiceStarted: OpenSCManager failed: "
  79. FORMAT_API_STATUS
  80. "\n",
  81. ApiStatus));
  82. #endif
  83. return FALSE;
  84. }
  85. if ((hService = OpenService(
  86. hScManager,
  87. ServiceName,
  88. SERVICE_QUERY_STATUS
  89. )) == (SC_HANDLE) NULL) {
  90. #if DBG
  91. ApiStatus = (NET_API_STATUS) GetLastError();
  92. KdPrintEx((DPFLTR_NETAPI_ID,
  93. DPFLTR_WARNING_LEVEL,
  94. PREFIX_NETLIB
  95. "NetpIsServiceStarted: OpenService failed: "
  96. FORMAT_API_STATUS
  97. "\n",
  98. ApiStatus));
  99. #endif
  100. (void) CloseServiceHandle(hScManager);
  101. return FALSE;
  102. }
  103. if (! QueryServiceStatus(
  104. hService,
  105. &ServiceStatus
  106. )) {
  107. #if DBG
  108. ApiStatus = GetLastError();
  109. KdPrintEx((DPFLTR_NETAPI_ID,
  110. DPFLTR_WARNING_LEVEL,
  111. PREFIX_NETLIB
  112. "NetpIsServiceStarted: QueryServiceStatus failed: "
  113. FORMAT_API_STATUS
  114. "\n",
  115. ApiStatus));
  116. #endif
  117. (void) CloseServiceHandle(hScManager);
  118. (void) CloseServiceHandle(hService);
  119. return FALSE;
  120. }
  121. (void) CloseServiceHandle(hScManager);
  122. (void) CloseServiceHandle(hService);
  123. if ( (ServiceStatus.dwCurrentState == SERVICE_RUNNING) ||
  124. (ServiceStatus.dwCurrentState == SERVICE_CONTINUE_PENDING) ||
  125. (ServiceStatus.dwCurrentState == SERVICE_PAUSE_PENDING) ||
  126. (ServiceStatus.dwCurrentState == SERVICE_PAUSED) ) {
  127. return TRUE;
  128. }
  129. return FALSE;
  130. } // NetpIsServiceStarted