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.

257 lines
5.9 KiB

  1. //+--------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1996-1999
  5. //
  6. // File: tlsbkup.cpp
  7. //
  8. // Contents:
  9. // Backup/restore of database
  10. //
  11. // History:
  12. // 5/28/99 Created RobLeit
  13. //---------------------------------------------------------------------------
  14. #include "pch.cpp"
  15. #include "globals.h"
  16. #include "init.h"
  17. #define STRSAFE_NO_DEPRECATE
  18. #include <strsafe.h>
  19. extern "C" VOID ServiceStop();
  20. static BOOL g_fDoingBackupRestore = FALSE;
  21. static CCriticalSection g_csBackupRestore;
  22. ////////////////////////////////////////////////////////////////////////////
  23. extern "C" HRESULT WINAPI
  24. ExportTlsDatabase(
  25. )
  26. /*++
  27. --*/
  28. {
  29. RPC_STATUS rpcStatus;
  30. HRESULT hr = S_OK;
  31. TCHAR szExportedDb[MAX_PATH+1];
  32. TCHAR *pszExportedDbEnd;
  33. size_t cbRemaining;
  34. if (g_fDoingBackupRestore)
  35. {
  36. return HRESULT_FROM_WIN32(ERROR_BUSY);
  37. }
  38. g_csBackupRestore.Lock();
  39. if (g_fDoingBackupRestore)
  40. {
  41. g_csBackupRestore.UnLock();
  42. return HRESULT_FROM_WIN32(ERROR_BUSY);
  43. }
  44. // ignore all call if service is shutting down
  45. if( IsServiceShuttingdown() == TRUE )
  46. {
  47. g_csBackupRestore.UnLock();
  48. return HRESULT_FROM_WIN32(ERROR_BUSY);
  49. }
  50. g_fDoingBackupRestore = TRUE;
  51. // Tell RPC threads to stop handling clients
  52. ServiceSignalShutdown();
  53. // Stop listening to other RPC interfaces
  54. (VOID)RpcServerUnregisterIf(TermServLicensing_v1_0_s_ifspec,
  55. NULL, // UUID
  56. TRUE); // Wait for calls to complete
  57. (VOID)RpcServerUnregisterIf(HydraLicenseService_v1_0_s_ifspec,
  58. NULL, // UUID
  59. TRUE); // Wait for calls to complete
  60. // Release handles to database
  61. TLSPrepareForBackupRestore();
  62. hr = StringCbCopyEx(szExportedDb,sizeof(szExportedDb),g_szDatabaseDir,&pszExportedDbEnd, &cbRemaining,0);
  63. if (FAILED(hr))
  64. {
  65. goto cleanup;
  66. }
  67. hr = StringCbCopyEx(pszExportedDbEnd,cbRemaining,TLSBACKUP_EXPORT_DIR,&pszExportedDbEnd, &cbRemaining,0);
  68. if (FAILED(hr))
  69. {
  70. goto cleanup;
  71. }
  72. CreateDirectoryEx(g_szDatabaseDir,
  73. szExportedDb,
  74. NULL); // Ignore errors, they'll show up in CopyFile
  75. hr = StringCbCopyEx(pszExportedDbEnd,cbRemaining,_TEXT("\\"),&pszExportedDbEnd, &cbRemaining,0);
  76. if (FAILED(hr))
  77. {
  78. goto cleanup;
  79. }
  80. hr = StringCbCopyEx(pszExportedDbEnd,cbRemaining,g_szDatabaseFname,NULL,NULL,0);
  81. if (FAILED(hr))
  82. {
  83. goto cleanup;
  84. }
  85. // Copy database file
  86. if (!CopyFile(g_szDatabaseFile,szExportedDb,FALSE))
  87. {
  88. hr = HRESULT_FROM_WIN32(GetLastError());
  89. goto cleanup;
  90. }
  91. cleanup:
  92. // Restart RPC and work manager
  93. ServiceResetShutdownEvent();
  94. // Restart after backup
  95. hr = TLSRestartAfterBackupRestore(TRUE);
  96. if( ERROR_SUCCESS != hr )
  97. {
  98. // force a shutdown...
  99. ServiceSignalShutdown();
  100. ServiceStop();
  101. }
  102. else
  103. {
  104. // Begin listening again
  105. hr = RpcServerRegisterIf(TermServLicensing_v1_0_s_ifspec,
  106. NULL,
  107. NULL);
  108. if(SUCCEEDED(hr))
  109. {
  110. hr = RpcServerRegisterIf(HydraLicenseService_v1_0_s_ifspec,
  111. NULL,
  112. NULL);
  113. }
  114. if(FAILED(hr))
  115. {
  116. // force a shutdown...
  117. ServiceSignalShutdown();
  118. ServiceStop();
  119. }
  120. }
  121. g_fDoingBackupRestore = FALSE;
  122. g_csBackupRestore.UnLock();
  123. return hr;
  124. }
  125. ////////////////////////////////////////////////////////////////////////////
  126. extern "C" HRESULT WINAPI
  127. ImportTlsDatabase(
  128. )
  129. /*++
  130. --*/
  131. {
  132. HRESULT hr = S_OK;
  133. if (g_fDoingBackupRestore)
  134. {
  135. return HRESULT_FROM_WIN32(ERROR_BUSY);
  136. }
  137. g_csBackupRestore.Lock();
  138. if (g_fDoingBackupRestore)
  139. {
  140. g_csBackupRestore.UnLock();
  141. return HRESULT_FROM_WIN32(ERROR_BUSY);
  142. }
  143. // ignore all call if service is shutting down
  144. if( IsServiceShuttingdown() == TRUE )
  145. {
  146. g_csBackupRestore.UnLock();
  147. return HRESULT_FROM_WIN32(ERROR_BUSY);
  148. }
  149. g_fDoingBackupRestore = TRUE;
  150. // Tell RPC threads to stop handling clients
  151. ServiceSignalShutdown();
  152. // Stop listening to other RPC interfaces
  153. (VOID)RpcServerUnregisterIf(TermServLicensing_v1_0_s_ifspec,
  154. NULL, // UUID
  155. TRUE); // Wait for calls to complete
  156. (VOID)RpcServerUnregisterIf(HydraLicenseService_v1_0_s_ifspec,
  157. NULL, // UUID
  158. TRUE); // Wait for calls to complete
  159. TLSPrepareForBackupRestore();
  160. // Restart RPC
  161. ServiceResetShutdownEvent();
  162. // not restart after backup
  163. hr = TLSRestartAfterBackupRestore(FALSE);
  164. if( ERROR_SUCCESS != hr )
  165. {
  166. // force a shutdown...
  167. ServiceSignalShutdown();
  168. ServiceStop();
  169. }
  170. else
  171. {
  172. // Begin listening again
  173. hr = RpcServerRegisterIf(TermServLicensing_v1_0_s_ifspec,
  174. NULL,
  175. NULL);
  176. if(SUCCEEDED(hr))
  177. {
  178. hr = RpcServerRegisterIf(HydraLicenseService_v1_0_s_ifspec,
  179. NULL,
  180. NULL);
  181. }
  182. if(FAILED(hr))
  183. {
  184. // force a shutdown...
  185. ServiceSignalShutdown();
  186. ServiceStop();
  187. }
  188. }
  189. g_fDoingBackupRestore = FALSE;
  190. g_csBackupRestore.UnLock();
  191. return hr;
  192. }