Application.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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.IO;
  29. using System.Reflection;
  30. using log4net;
  31. using log4net.Config;
  32. using Nini.Config;
  33. using OpenSim.Framework;
  34. using OpenSim.Framework.Console;
  35. namespace OpenSim
  36. {
  37. /// <summary>
  38. /// Starting class for the OpenSimulator Region
  39. /// </summary>
  40. public class Application
  41. {
  42. /// <summary>
  43. /// Text Console Logger
  44. /// </summary>
  45. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  46. /// <summary>
  47. /// Path to the main ini Configuration file
  48. /// </summary>
  49. public static string iniFilePath = "";
  50. /// <summary>
  51. /// Save Crashes in the bin/crashes folder. Configurable with m_crashDir
  52. /// </summary>
  53. public static bool m_saveCrashDumps = false;
  54. /// <summary>
  55. /// Directory to save crash reports to. Relative to bin/
  56. /// </summary>
  57. public static string m_crashDir = "crashes";
  58. /// <summary>
  59. /// Instance of the OpenSim class. This could be OpenSim or OpenSimBackground depending on the configuration
  60. /// </summary>
  61. protected static OpenSimBase m_sim = null;
  62. //could move our main function into OpenSimMain and kill this class
  63. public static void Main(string[] args)
  64. {
  65. // First line, hook the appdomain to the crash reporter
  66. AppDomain.CurrentDomain.UnhandledException +=
  67. new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
  68. // Add the arguments supplied when running the application to the configuration
  69. ArgvConfigSource configSource = new ArgvConfigSource(args);
  70. // Configure Log4Net
  71. configSource.AddSwitch("Startup", "logconfig");
  72. string logConfigFile = configSource.Configs["Startup"].GetString("logconfig", String.Empty);
  73. if (logConfigFile != String.Empty)
  74. {
  75. XmlConfigurator.Configure(new System.IO.FileInfo(logConfigFile));
  76. m_log.InfoFormat("[OPENSIM MAIN]: configured log4net using \"{0}\" as configuration file",
  77. logConfigFile);
  78. }
  79. else
  80. {
  81. XmlConfigurator.Configure();
  82. m_log.Info("[OPENSIM MAIN]: configured log4net using default OpenSim.exe.config");
  83. }
  84. m_log.InfoFormat(
  85. "[OPENSIM MAIN]: System Locale is {0}", System.Threading.Thread.CurrentThread.CurrentCulture);
  86. string monoThreadsPerCpu = System.Environment.GetEnvironmentVariable("MONO_THREADS_PER_CPU");
  87. m_log.InfoFormat(
  88. "[OPENSIM MAIN]: Environment variable MONO_THREADS_PER_CPU is {0}", monoThreadsPerCpu ?? "unset");
  89. // Increase the number of IOCP threads available. Mono defaults to a tragically low number
  90. int workerThreads, iocpThreads;
  91. System.Threading.ThreadPool.GetMaxThreads(out workerThreads, out iocpThreads);
  92. m_log.InfoFormat("[OPENSIM MAIN]: Runtime gave us {0} worker threads and {1} IOCP threads", workerThreads, iocpThreads);
  93. if (workerThreads < 500 || iocpThreads < 1000)
  94. {
  95. workerThreads = 500;
  96. iocpThreads = 1000;
  97. m_log.Info("[OPENSIM MAIN]: Bumping up to 500 worker threads and 1000 IOCP threads");
  98. System.Threading.ThreadPool.SetMaxThreads(workerThreads, iocpThreads);
  99. }
  100. // Check if the system is compatible with OpenSimulator.
  101. // Ensures that the minimum system requirements are met
  102. string supported = String.Empty;
  103. if (Util.IsEnvironmentSupported(ref supported))
  104. {
  105. m_log.Info("Environment is compatible.\n");
  106. }
  107. else
  108. {
  109. m_log.Warn("Environment is unsupported (" + supported + ")\n");
  110. }
  111. // Configure nIni aliases and localles
  112. Culture.SetCurrentCulture();
  113. // Validate that the user has the most basic configuration done
  114. // If not, offer to do the most basic configuration for them warning them along the way of the importance of
  115. // reading these files.
  116. /*
  117. m_log.Info("Checking for reguired configuration...\n");
  118. bool OpenSim_Ini = (File.Exists(Path.Combine(Util.configDir(), "OpenSim.ini")))
  119. || (File.Exists(Path.Combine(Util.configDir(), "opensim.ini")))
  120. || (File.Exists(Path.Combine(Util.configDir(), "openSim.ini")))
  121. || (File.Exists(Path.Combine(Util.configDir(), "Opensim.ini")));
  122. bool StanaloneCommon_ProperCased = File.Exists(Path.Combine(Path.Combine(Util.configDir(), "config-include"), "StandaloneCommon.ini"));
  123. bool StanaloneCommon_lowercased = File.Exists(Path.Combine(Path.Combine(Util.configDir(), "config-include"), "standalonecommon.ini"));
  124. bool GridCommon_ProperCased = File.Exists(Path.Combine(Path.Combine(Util.configDir(), "config-include"), "GridCommon.ini"));
  125. bool GridCommon_lowerCased = File.Exists(Path.Combine(Path.Combine(Util.configDir(), "config-include"), "gridcommon.ini"));
  126. if ((OpenSim_Ini)
  127. && (
  128. (StanaloneCommon_ProperCased
  129. || StanaloneCommon_lowercased
  130. || GridCommon_ProperCased
  131. || GridCommon_lowerCased
  132. )))
  133. {
  134. m_log.Info("Required Configuration Files Found\n");
  135. }
  136. else
  137. {
  138. MainConsole.Instance = new LocalConsole("Region");
  139. string resp = MainConsole.Instance.CmdPrompt(
  140. "\n\n*************Required Configuration files not found.*************\n\n OpenSimulator will not run without these files.\n\nRemember, these file names are Case Sensitive in Linux and Proper Cased.\n1. ./OpenSim.ini\nand\n2. ./config-include/StandaloneCommon.ini \nor\n3. ./config-include/GridCommon.ini\n\nAlso, you will want to examine these files in great detail because only the basic system will load by default. OpenSimulator can do a LOT more if you spend a little time going through these files.\n\n" + ": " + "Do you want to copy the most basic Defaults from standalone?",
  141. "yes");
  142. if (resp == "yes")
  143. {
  144. if (!(OpenSim_Ini))
  145. {
  146. try
  147. {
  148. File.Copy(Path.Combine(Util.configDir(), "OpenSim.ini.example"),
  149. Path.Combine(Util.configDir(), "OpenSim.ini"));
  150. } catch (UnauthorizedAccessException)
  151. {
  152. MainConsole.Instance.Output("Unable to Copy OpenSim.ini.example to OpenSim.ini, Make sure OpenSim has have the required permissions\n");
  153. } catch (ArgumentException)
  154. {
  155. MainConsole.Instance.Output("Unable to Copy OpenSim.ini.example to OpenSim.ini, The current directory is invalid.\n");
  156. } catch (System.IO.PathTooLongException)
  157. {
  158. MainConsole.Instance.Output("Unable to Copy OpenSim.ini.example to OpenSim.ini, the Path to these files is too long.\n");
  159. } catch (System.IO.DirectoryNotFoundException)
  160. {
  161. MainConsole.Instance.Output("Unable to Copy OpenSim.ini.example to OpenSim.ini, the current directory is reporting as not found.\n");
  162. } catch (System.IO.FileNotFoundException)
  163. {
  164. MainConsole.Instance.Output("Unable to Copy OpenSim.ini.example to OpenSim.ini, the example is not found, please make sure that the example files exist.\n");
  165. } catch (System.IO.IOException)
  166. {
  167. // Destination file exists already or a hard drive failure... .. so we can just drop this one
  168. //MainConsole.Instance.Output("Unable to Copy OpenSim.ini.example to OpenSim.ini, the example is not found, please make sure that the example files exist.\n");
  169. } catch (System.NotSupportedException)
  170. {
  171. MainConsole.Instance.Output("Unable to Copy OpenSim.ini.example to OpenSim.ini, The current directory is invalid.\n");
  172. }
  173. }
  174. if (!(StanaloneCommon_ProperCased || StanaloneCommon_lowercased))
  175. {
  176. try
  177. {
  178. File.Copy(Path.Combine(Path.Combine(Util.configDir(), "config-include"), "StandaloneCommon.ini.example"),
  179. Path.Combine(Path.Combine(Util.configDir(), "config-include"), "StandaloneCommon.ini"));
  180. }
  181. catch (UnauthorizedAccessException)
  182. {
  183. MainConsole.Instance.Output("Unable to Copy StandaloneCommon.ini.example to StandaloneCommon.ini, Make sure OpenSim has the required permissions\n");
  184. }
  185. catch (ArgumentException)
  186. {
  187. MainConsole.Instance.Output("Unable to Copy StandaloneCommon.ini.example to StandaloneCommon.ini, The current directory is invalid.\n");
  188. }
  189. catch (System.IO.PathTooLongException)
  190. {
  191. MainConsole.Instance.Output("Unable to Copy StandaloneCommon.ini.example to StandaloneCommon.ini, the Path to these files is too long.\n");
  192. }
  193. catch (System.IO.DirectoryNotFoundException)
  194. {
  195. MainConsole.Instance.Output("Unable to Copy StandaloneCommon.ini.example to StandaloneCommon.ini, the current directory is reporting as not found.\n");
  196. }
  197. catch (System.IO.FileNotFoundException)
  198. {
  199. MainConsole.Instance.Output("Unable to Copy StandaloneCommon.ini.example to StandaloneCommon.ini, the example is not found, please make sure that the example files exist.\n");
  200. }
  201. catch (System.IO.IOException)
  202. {
  203. // Destination file exists already or a hard drive failure... .. so we can just drop this one
  204. //MainConsole.Instance.Output("Unable to Copy OpenSim.ini.example to OpenSim.ini, the example is not found, please make sure that the example files exist.\n");
  205. }
  206. catch (System.NotSupportedException)
  207. {
  208. MainConsole.Instance.Output("Unable to Copy StandaloneCommon.ini.example to StandaloneCommon.ini, The current directory is invalid.\n");
  209. }
  210. }
  211. }
  212. MainConsole.Instance = null;
  213. }
  214. */
  215. configSource.Alias.AddAlias("On", true);
  216. configSource.Alias.AddAlias("Off", false);
  217. configSource.Alias.AddAlias("True", true);
  218. configSource.Alias.AddAlias("False", false);
  219. configSource.Alias.AddAlias("Yes", true);
  220. configSource.Alias.AddAlias("No", false);
  221. configSource.AddSwitch("Startup", "background");
  222. configSource.AddSwitch("Startup", "inifile");
  223. configSource.AddSwitch("Startup", "inimaster");
  224. configSource.AddSwitch("Startup", "inidirectory");
  225. configSource.AddSwitch("Startup", "physics");
  226. configSource.AddSwitch("Startup", "gui");
  227. configSource.AddSwitch("Startup", "console");
  228. configSource.AddSwitch("Startup", "save_crashes");
  229. configSource.AddSwitch("Startup", "crash_dir");
  230. configSource.AddConfig("StandAlone");
  231. configSource.AddConfig("Network");
  232. // Check if we're running in the background or not
  233. bool background = configSource.Configs["Startup"].GetBoolean("background", false);
  234. // Check if we're saving crashes
  235. m_saveCrashDumps = configSource.Configs["Startup"].GetBoolean("save_crashes", false);
  236. // load Crash directory config
  237. m_crashDir = configSource.Configs["Startup"].GetString("crash_dir", m_crashDir);
  238. if (background)
  239. {
  240. m_sim = new OpenSimBackground(configSource);
  241. m_sim.Startup();
  242. }
  243. else
  244. {
  245. m_sim = new OpenSim(configSource);
  246. m_sim.Startup();
  247. while (true)
  248. {
  249. try
  250. {
  251. // Block thread here for input
  252. MainConsole.Instance.Prompt();
  253. }
  254. catch (Exception e)
  255. {
  256. m_log.ErrorFormat("Command error: {0}", e);
  257. }
  258. }
  259. }
  260. }
  261. private static bool _IsHandlingException = false; // Make sure we don't go recursive on ourself
  262. /// <summary>
  263. /// Global exception handler -- all unhandlet exceptions end up here :)
  264. /// </summary>
  265. /// <param name="sender"></param>
  266. /// <param name="e"></param>
  267. private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
  268. {
  269. if (_IsHandlingException)
  270. {
  271. return;
  272. }
  273. _IsHandlingException = true;
  274. // TODO: Add config option to allow users to turn off error reporting
  275. // TODO: Post error report (disabled for now)
  276. string msg = String.Empty;
  277. msg += "\r\n";
  278. msg += "APPLICATION EXCEPTION DETECTED: " + e.ToString() + "\r\n";
  279. msg += "\r\n";
  280. msg += "Exception: " + e.ExceptionObject.ToString() + "\r\n";
  281. Exception ex = (Exception) e.ExceptionObject;
  282. if (ex.InnerException != null)
  283. {
  284. msg += "InnerException: " + ex.InnerException.ToString() + "\r\n";
  285. }
  286. msg += "\r\n";
  287. msg += "Application is terminating: " + e.IsTerminating.ToString() + "\r\n";
  288. m_log.ErrorFormat("[APPLICATION]: {0}", msg);
  289. if (m_saveCrashDumps)
  290. {
  291. // Log exception to disk
  292. try
  293. {
  294. if (!Directory.Exists(m_crashDir))
  295. {
  296. Directory.CreateDirectory(m_crashDir);
  297. }
  298. string log = Util.GetUniqueFilename(ex.GetType() + ".txt");
  299. using (StreamWriter m_crashLog = new StreamWriter(Path.Combine(m_crashDir, log)))
  300. {
  301. m_crashLog.WriteLine(msg);
  302. }
  303. File.Copy("OpenSim.ini", Path.Combine(m_crashDir, log + "_OpenSim.ini"), true);
  304. }
  305. catch (Exception e2)
  306. {
  307. m_log.ErrorFormat("[CRASH LOGGER CRASHED]: {0}", e2);
  308. }
  309. }
  310. _IsHandlingException = false;
  311. }
  312. }
  313. }