Source code of Windows XP (NT5)
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.

169 lines
4.1 KiB

  1. /*++
  2. Copyright (c) 1992 Microsoft Corporation
  3. Module Name:
  4. LockTest.c
  5. Abstract:
  6. This file contains tests of the Service Controller's lock APIs:
  7. RLockServiceDatabase
  8. RQueryServiceLockStatusW
  9. RUnlockServiceDatabase
  10. Author:
  11. John Rogers (JohnRo) 15-Apr-1992
  12. Environment:
  13. User Mode - Win32
  14. Revision History:
  15. 15-Apr-1992 JohnRo
  16. Created.
  17. 21-Apr-1992 JohnRo
  18. Split-out lock test functions.
  19. --*/
  20. //
  21. // INCLUDES
  22. //
  23. #define UNICODE
  24. #include <windows.h>
  25. #include <assert.h> // assert().
  26. #include <debugfmt.h> // FORMAT_ equates.
  27. #include <winsvc.h>
  28. #include <scdebug.h> // STATIC.
  29. #include <sctest.h> // TRACE_MSG macros, UNEXPECTED_MSG(), etc.
  30. #include <stdio.h> // printf()
  31. #define BUF_SIZE 1024 // arbitrary
  32. STATIC VOID
  33. DumpLockStatus(
  34. IN LPQUERY_SERVICE_LOCK_STATUS LockStatus
  35. )
  36. {
  37. (VOID) printf( "Lock status:\n" );
  38. (VOID) printf( " Locked: " FORMAT_LPSTR "\n",
  39. (LockStatus->fIsLocked) ? "yes" : "no" );
  40. (VOID) printf( " LockOwner: " FORMAT_LPWSTR "\n",
  41. (LockStatus->lpLockOwner != NULL) ? LockStatus->lpLockOwner : L"NONE" );
  42. (VOID) printf( " lock duration: " FORMAT_DWORD "\n",
  43. LockStatus->dwLockDuration );
  44. } // DumpLockStatus
  45. STATIC VOID
  46. GetAndDisplayLockStatus(
  47. IN LPSTR Comment,
  48. IN SC_HANDLE hScManager
  49. )
  50. {
  51. BYTE buffer[BUF_SIZE];
  52. LPQUERY_SERVICE_LOCK_STATUS lockStatus = (LPVOID) &buffer[0];
  53. DWORD sizeNeed;
  54. TRACE_MSG1( "calling QueryServiceLockStatus " FORMAT_LPSTR "...\n",
  55. Comment );
  56. if ( !QueryServiceLockStatus(hScManager, lockStatus, BUF_SIZE, &sizeNeed)) {
  57. UNEXPECTED_MSG( "from QueryServiceLockStatus (default)",
  58. GetLastError() );
  59. assert( FALSE );
  60. }
  61. DumpLockStatus( lockStatus );
  62. }
  63. VOID
  64. TestLocks(
  65. VOID
  66. )
  67. {
  68. SC_HANDLE hScManager = NULL;
  69. SC_LOCK lock;
  70. ///////////////////////////////////////////////////////////////////
  71. SetLastError( 149 );
  72. (VOID) printf( "Force last error is " FORMAT_DWORD "\n", GetLastError() );
  73. TRACE_MSG1( "handle (before anything) is " FORMAT_HEX_DWORD ".\n",
  74. (DWORD) hScManager );
  75. ///////////////////////////////////////////////////////////////////
  76. TRACE_MSG0( "calling OpenSCManagerW (default)...\n" );
  77. hScManager = OpenSCManager(
  78. NULL, // local machine.
  79. NULL, // no database name.
  80. GENERIC_ALL ); // desired access.
  81. TRACE_MSG1( "back from OpenSCManagerW, handle is " FORMAT_HEX_DWORD ".\n",
  82. (DWORD) hScManager );
  83. if (hScManager == NULL) {
  84. UNEXPECTED_MSG( "from OpenSCManagerW (default)", GetLastError() );
  85. goto Cleanup;
  86. }
  87. ///////////////////////////////////////////////////////////////////
  88. GetAndDisplayLockStatus( "default, empty", hScManager );
  89. ///////////////////////////////////////////////////////////////////
  90. TRACE_MSG0( "calling LockServiceDatabase (default)...\n" );
  91. lock = LockServiceDatabase( hScManager );
  92. TRACE_MSG1( "Got back lock " FORMAT_HEX_DWORD ".\n", (DWORD) lock );
  93. if (lock == NULL) {
  94. UNEXPECTED_MSG( "from LockServiceDatabase (default)", GetLastError() );
  95. goto Cleanup;
  96. }
  97. ///////////////////////////////////////////////////////////////////
  98. GetAndDisplayLockStatus( "default, not empty", hScManager );
  99. ///////////////////////////////////////////////////////////////////
  100. TRACE_MSG0( "calling UnlockServiceDatabase (default)...\n" );
  101. if ( !UnlockServiceDatabase( lock ) ) {
  102. UNEXPECTED_MSG( "from UnlockServiceDatabase (default)",
  103. GetLastError() );
  104. goto Cleanup;
  105. }
  106. ///////////////////////////////////////////////////////////////////
  107. GetAndDisplayLockStatus( "default, empty again", hScManager );
  108. ///////////////////////////////////////////////////////////////////
  109. Cleanup:
  110. if (hScManager != NULL) {
  111. TRACE_MSG0( "calling CloseServiceHandle...\n" );
  112. if ( !CloseServiceHandle( hScManager ) ) {
  113. UNEXPECTED_MSG( "from CloseServiceHandle", GetLastError() );
  114. }
  115. }
  116. } // TestLocks