Application.cs 18 KB

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