AppDomainManager.cs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. /*
  2. * Copyright (c) Contributors, http://opensimulator.org/
  3. * See CONTRIBUTORS.TXT for a full list of copyright holders.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of the OpenSim Project nor the
  13. * names of its contributors may be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
  17. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
  20. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. using System;
  28. using System.Collections;
  29. using System.Collections.Generic;
  30. using System.Reflection;
  31. using System.Security;
  32. using System.Security.Policy;
  33. using System.Security.Permissions;
  34. using OpenSim.Region.ScriptEngine.Interfaces;
  35. using OpenSim.Region.ScriptEngine.Shared.ScriptBase;
  36. namespace OpenSim.Region.ScriptEngine.DotNetEngine
  37. {
  38. public class AppDomainManager
  39. {
  40. //
  41. // This class does AppDomain handling and loading/unloading of
  42. // scripts in it. It is instanced in "ScriptEngine" and controlled
  43. // from "ScriptManager"
  44. //
  45. // 1. Create a new AppDomain if old one is full (or doesn't exist)
  46. // 2. Load scripts into AppDomain
  47. // 3. Unload scripts from AppDomain (stopping them and marking
  48. // them as inactive)
  49. // 4. Unload AppDomain completely when all scripts in it has stopped
  50. //
  51. private int maxScriptsPerAppDomain = 1;
  52. // Internal list of all AppDomains
  53. private List<AppDomainStructure> appDomains =
  54. new List<AppDomainStructure>();
  55. // Structure to keep track of data around AppDomain
  56. private class AppDomainStructure
  57. {
  58. // The AppDomain itself
  59. public AppDomain CurrentAppDomain;
  60. // Number of scripts loaded into AppDomain
  61. public int ScriptsLoaded;
  62. // Number of dead scripts
  63. public int ScriptsWaitingUnload;
  64. }
  65. // Current AppDomain
  66. private AppDomainStructure currentAD;
  67. private object getLock = new object(); // Mutex
  68. private object freeLock = new object(); // Mutex
  69. private ScriptEngine m_scriptEngine;
  70. //public AppDomainManager(ScriptEngine scriptEngine)
  71. public AppDomainManager(ScriptEngine scriptEngine)
  72. {
  73. m_scriptEngine = scriptEngine;
  74. ReadConfig();
  75. }
  76. public void ReadConfig()
  77. {
  78. maxScriptsPerAppDomain = m_scriptEngine.ScriptConfigSource.GetInt(
  79. "ScriptsPerAppDomain", 1);
  80. }
  81. // Find a free AppDomain, creating one if necessary
  82. private AppDomainStructure GetFreeAppDomain()
  83. {
  84. lock (getLock)
  85. {
  86. // Current full?
  87. if (currentAD != null &&
  88. currentAD.ScriptsLoaded >= maxScriptsPerAppDomain)
  89. {
  90. // Add it to AppDomains list and empty current
  91. appDomains.Add(currentAD);
  92. currentAD = null;
  93. }
  94. // No current
  95. if (currentAD == null)
  96. {
  97. // Create a new current AppDomain
  98. currentAD = new AppDomainStructure();
  99. currentAD.CurrentAppDomain = PrepareNewAppDomain();
  100. }
  101. return currentAD;
  102. }
  103. }
  104. private int AppDomainNameCount;
  105. // Create and prepare a new AppDomain for scripts
  106. private AppDomain PrepareNewAppDomain()
  107. {
  108. // Create and prepare a new AppDomain
  109. AppDomainNameCount++;
  110. // TODO: Currently security match current appdomain
  111. // Construct and initialize settings for a second AppDomain.
  112. AppDomainSetup ads = new AppDomainSetup();
  113. ads.ApplicationBase = AppDomain.CurrentDomain.BaseDirectory;
  114. ads.DisallowBindingRedirects = true;
  115. ads.DisallowCodeDownload = true;
  116. ads.LoaderOptimization = LoaderOptimization.MultiDomainHost;
  117. ads.ShadowCopyFiles = "false"; // Disable shadowing
  118. ads.ConfigurationFile =
  119. AppDomain.CurrentDomain.SetupInformation.ConfigurationFile;
  120. AppDomain AD = AppDomain.CreateDomain("ScriptAppDomain_" +
  121. AppDomainNameCount, null, ads);
  122. m_scriptEngine.Log.Info("[" + m_scriptEngine.ScriptEngineName +
  123. "]: AppDomain Loading: " +
  124. AssemblyName.GetAssemblyName(
  125. "OpenSim.Region.ScriptEngine.Shared.dll").ToString());
  126. AD.Load(AssemblyName.GetAssemblyName(
  127. "OpenSim.Region.ScriptEngine.Shared.dll"));
  128. // Return the new AppDomain
  129. return AD;
  130. }
  131. // Unload appdomains that are full and have only dead scripts
  132. private void UnloadAppDomains()
  133. {
  134. lock (freeLock)
  135. {
  136. // Go through all
  137. foreach (AppDomainStructure ads in new ArrayList(appDomains))
  138. {
  139. // Don't process current AppDomain
  140. if (ads.CurrentAppDomain != currentAD.CurrentAppDomain)
  141. {
  142. // Not current AppDomain
  143. // Is number of unloaded bigger or equal to number of loaded?
  144. if (ads.ScriptsLoaded <= ads.ScriptsWaitingUnload)
  145. {
  146. // Remove from internal list
  147. appDomains.Remove(ads);
  148. // Unload
  149. AppDomain.Unload(ads.CurrentAppDomain);
  150. }
  151. }
  152. }
  153. }
  154. }
  155. public IScript LoadScript(string FileName, out AppDomain ad)
  156. {
  157. // Find next available AppDomain to put it in
  158. AppDomainStructure FreeAppDomain = GetFreeAppDomain();
  159. IScript mbrt = (IScript)
  160. FreeAppDomain.CurrentAppDomain.CreateInstanceFromAndUnwrap(
  161. FileName, "SecondLife.Script");
  162. FreeAppDomain.ScriptsLoaded++;
  163. ad = FreeAppDomain.CurrentAppDomain;
  164. return mbrt;
  165. }
  166. // Increase "dead script" counter for an AppDomain
  167. public void StopScript(AppDomain ad)
  168. {
  169. lock (freeLock)
  170. {
  171. // Check if it is current AppDomain
  172. if (currentAD.CurrentAppDomain == ad)
  173. {
  174. // Yes - increase
  175. currentAD.ScriptsWaitingUnload++;
  176. return;
  177. }
  178. // Lopp through all AppDomains
  179. foreach (AppDomainStructure ads in new ArrayList(appDomains))
  180. {
  181. if (ads.CurrentAppDomain == ad)
  182. {
  183. // Found it
  184. ads.ScriptsWaitingUnload++;
  185. break;
  186. }
  187. }
  188. }
  189. UnloadAppDomains(); // Outsite lock, has its own GetLock
  190. }
  191. // If set to true then threads and stuff should try
  192. // to make a graceful exit
  193. public bool PleaseShutdown
  194. {
  195. get { return _PleaseShutdown; }
  196. set { _PleaseShutdown = value; }
  197. }
  198. private bool _PleaseShutdown = false;
  199. }
  200. }