AppDomainManager.cs 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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 OpenSimulator 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. using log4net;
  37. namespace OpenSim.Region.ScriptEngine.DotNetEngine
  38. {
  39. public class AppDomainManager
  40. {
  41. //
  42. // This class does AppDomain handling and loading/unloading of
  43. // scripts in it. It is instanced in "ScriptEngine" and controlled
  44. // from "ScriptManager"
  45. //
  46. // 1. Create a new AppDomain if old one is full (or doesn't exist)
  47. // 2. Load scripts into AppDomain
  48. // 3. Unload scripts from AppDomain (stopping them and marking
  49. // them as inactive)
  50. // 4. Unload AppDomain completely when all scripts in it has stopped
  51. //
  52. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  53. private int maxScriptsPerAppDomain = 1;
  54. // Internal list of all AppDomains
  55. private List<AppDomainStructure> appDomains =
  56. new List<AppDomainStructure>();
  57. // Structure to keep track of data around AppDomain
  58. private class AppDomainStructure
  59. {
  60. // The AppDomain itself
  61. public AppDomain CurrentAppDomain;
  62. // Number of scripts loaded into AppDomain
  63. public int ScriptsLoaded;
  64. // Number of dead scripts
  65. public int ScriptsWaitingUnload;
  66. }
  67. // Current AppDomain
  68. private AppDomainStructure currentAD;
  69. private object getLock = new object(); // Mutex
  70. private object freeLock = new object(); // Mutex
  71. private ScriptEngine m_scriptEngine;
  72. //public AppDomainManager(ScriptEngine scriptEngine)
  73. public AppDomainManager(ScriptEngine scriptEngine)
  74. {
  75. m_scriptEngine = scriptEngine;
  76. ReadConfig();
  77. }
  78. public void ReadConfig()
  79. {
  80. maxScriptsPerAppDomain = m_scriptEngine.ScriptConfigSource.GetInt(
  81. "ScriptsPerAppDomain", 1);
  82. }
  83. // Find a free AppDomain, creating one if necessary
  84. private AppDomainStructure GetFreeAppDomain()
  85. {
  86. lock (getLock)
  87. {
  88. // Current full?
  89. if (currentAD != null &&
  90. currentAD.ScriptsLoaded >= maxScriptsPerAppDomain)
  91. {
  92. // Add it to AppDomains list and empty current
  93. appDomains.Add(currentAD);
  94. currentAD = null;
  95. }
  96. // No current
  97. if (currentAD == null)
  98. {
  99. // Create a new current AppDomain
  100. currentAD = new AppDomainStructure();
  101. currentAD.CurrentAppDomain = PrepareNewAppDomain();
  102. }
  103. return currentAD;
  104. }
  105. }
  106. private int AppDomainNameCount;
  107. // Create and prepare a new AppDomain for scripts
  108. private AppDomain PrepareNewAppDomain()
  109. {
  110. // Create and prepare a new AppDomain
  111. AppDomainNameCount++;
  112. // TODO: Currently security match current appdomain
  113. // Construct and initialize settings for a second AppDomain.
  114. AppDomainSetup ads = new AppDomainSetup();
  115. ads.ApplicationBase = AppDomain.CurrentDomain.BaseDirectory;
  116. ads.DisallowBindingRedirects = true;
  117. ads.DisallowCodeDownload = true;
  118. ads.LoaderOptimization = LoaderOptimization.MultiDomainHost;
  119. ads.ShadowCopyFiles = "false"; // Disable shadowing
  120. ads.ConfigurationFile =
  121. AppDomain.CurrentDomain.SetupInformation.ConfigurationFile;
  122. AppDomain AD = AppDomain.CreateDomain("ScriptAppDomain_" +
  123. AppDomainNameCount, null, ads);
  124. m_log.Info("[" + m_scriptEngine.ScriptEngineName +
  125. "]: AppDomain Loading: " +
  126. AssemblyName.GetAssemblyName(
  127. "OpenSim.Region.ScriptEngine.Shared.dll").ToString());
  128. AD.Load(AssemblyName.GetAssemblyName(
  129. "OpenSim.Region.ScriptEngine.Shared.dll"));
  130. // Return the new AppDomain
  131. return AD;
  132. }
  133. // Unload appdomains that are full and have only dead scripts
  134. private void UnloadAppDomains()
  135. {
  136. lock (freeLock)
  137. {
  138. // Go through all
  139. foreach (AppDomainStructure ads in new ArrayList(appDomains))
  140. {
  141. // Don't process current AppDomain
  142. if (ads.CurrentAppDomain != currentAD.CurrentAppDomain)
  143. {
  144. // Not current AppDomain
  145. // Is number of unloaded bigger or equal to number of loaded?
  146. if (ads.ScriptsLoaded <= ads.ScriptsWaitingUnload)
  147. {
  148. // Remove from internal list
  149. appDomains.Remove(ads);
  150. // Unload
  151. AppDomain.Unload(ads.CurrentAppDomain);
  152. }
  153. }
  154. }
  155. }
  156. }
  157. public IScript LoadScript(string FileName, out AppDomain ad)
  158. {
  159. // Find next available AppDomain to put it in
  160. AppDomainStructure FreeAppDomain = GetFreeAppDomain();
  161. IScript mbrt = (IScript)
  162. FreeAppDomain.CurrentAppDomain.CreateInstanceFromAndUnwrap(
  163. FileName, "SecondLife.Script");
  164. FreeAppDomain.ScriptsLoaded++;
  165. ad = FreeAppDomain.CurrentAppDomain;
  166. return mbrt;
  167. }
  168. // Increase "dead script" counter for an AppDomain
  169. public void StopScript(AppDomain ad)
  170. {
  171. lock (freeLock)
  172. {
  173. // Check if it is current AppDomain
  174. if (currentAD.CurrentAppDomain == ad)
  175. {
  176. // Yes - increase
  177. currentAD.ScriptsWaitingUnload++;
  178. return;
  179. }
  180. // Lopp through all AppDomains
  181. foreach (AppDomainStructure ads in new ArrayList(appDomains))
  182. {
  183. if (ads.CurrentAppDomain == ad)
  184. {
  185. // Found it
  186. ads.ScriptsWaitingUnload++;
  187. break;
  188. }
  189. }
  190. }
  191. UnloadAppDomains(); // Outsite lock, has its own GetLock
  192. }
  193. // If set to true then threads and stuff should try
  194. // to make a graceful exit
  195. public bool PleaseShutdown
  196. {
  197. get { return _PleaseShutdown; }
  198. set { _PleaseShutdown = value; }
  199. }
  200. private bool _PleaseShutdown = false;
  201. }
  202. }