Application.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  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.DebugFormat(
  85. "[OPENSIM MAIN]: System Locale is {0}", System.Threading.Thread.CurrentThread.CurrentCulture);
  86. // Increase the number of IOCP threads available. Mono defaults to a tragically low number
  87. int workerThreads, iocpThreads;
  88. System.Threading.ThreadPool.GetMaxThreads(out workerThreads, out iocpThreads);
  89. m_log.InfoFormat("[OPENSIM MAIN]: Runtime gave us {0} worker threads and {1} IOCP threads", workerThreads, iocpThreads);
  90. if (workerThreads < 500 || iocpThreads < 1000)
  91. {
  92. workerThreads = 500;
  93. iocpThreads = 1000;
  94. m_log.Info("[OPENSIM MAIN]: Bumping up to 500 worker threads and 1000 IOCP threads");
  95. System.Threading.ThreadPool.SetMaxThreads(workerThreads, iocpThreads);
  96. }
  97. // Check if the system is compatible with OpenSimulator.
  98. // Ensures that the minimum system requirements are met
  99. m_log.Info("Performing compatibility checks... \n");
  100. string supported = String.Empty;
  101. if (Util.IsEnvironmentSupported(ref supported))
  102. {
  103. m_log.Info("Environment is compatible.\n");
  104. }
  105. else
  106. {
  107. m_log.Warn("Environment is unsupported (" + supported + ")\n");
  108. }
  109. // Configure nIni aliases and localles
  110. Culture.SetCurrentCulture();
  111. // Validate that the user has the most basic configuration done
  112. // If not, offer to do the most basic configuration for them warning them along the way of the importance of
  113. // reading these files.
  114. /*
  115. m_log.Info("Checking for reguired configuration...\n");
  116. bool OpenSim_Ini = (File.Exists(Path.Combine(Util.configDir(), "OpenSim.ini")))
  117. || (File.Exists(Path.Combine(Util.configDir(), "opensim.ini")))
  118. || (File.Exists(Path.Combine(Util.configDir(), "openSim.ini")))
  119. || (File.Exists(Path.Combine(Util.configDir(), "Opensim.ini")));
  120. bool StanaloneCommon_ProperCased = File.Exists(Path.Combine(Path.Combine(Util.configDir(), "config-include"), "StandaloneCommon.ini"));
  121. bool StanaloneCommon_lowercased = File.Exists(Path.Combine(Path.Combine(Util.configDir(), "config-include"), "standalonecommon.ini"));
  122. bool GridCommon_ProperCased = File.Exists(Path.Combine(Path.Combine(Util.configDir(), "config-include"), "GridCommon.ini"));
  123. bool GridCommon_lowerCased = File.Exists(Path.Combine(Path.Combine(Util.configDir(), "config-include"), "gridcommon.ini"));
  124. if ((OpenSim_Ini)
  125. && (
  126. (StanaloneCommon_ProperCased
  127. || StanaloneCommon_lowercased
  128. || GridCommon_ProperCased
  129. || GridCommon_lowerCased
  130. )))
  131. {
  132. m_log.Info("Required Configuration Files Found\n");
  133. }
  134. else
  135. {
  136. MainConsole.Instance = new LocalConsole("Region");
  137. string resp = MainConsole.Instance.CmdPrompt(
  138. "\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?",
  139. "yes");
  140. if (resp == "yes")
  141. {
  142. if (!(OpenSim_Ini))
  143. {
  144. try
  145. {
  146. File.Copy(Path.Combine(Util.configDir(), "OpenSim.ini.example"),
  147. Path.Combine(Util.configDir(), "OpenSim.ini"));
  148. } catch (UnauthorizedAccessException)
  149. {
  150. MainConsole.Instance.Output("Unable to Copy OpenSim.ini.example to OpenSim.ini, Make sure OpenSim has have the required permissions\n");
  151. } catch (ArgumentException)
  152. {
  153. MainConsole.Instance.Output("Unable to Copy OpenSim.ini.example to OpenSim.ini, The current directory is invalid.\n");
  154. } catch (System.IO.PathTooLongException)
  155. {
  156. MainConsole.Instance.Output("Unable to Copy OpenSim.ini.example to OpenSim.ini, the Path to these files is too long.\n");
  157. } catch (System.IO.DirectoryNotFoundException)
  158. {
  159. MainConsole.Instance.Output("Unable to Copy OpenSim.ini.example to OpenSim.ini, the current directory is reporting as not found.\n");
  160. } catch (System.IO.FileNotFoundException)
  161. {
  162. 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");
  163. } catch (System.IO.IOException)
  164. {
  165. // Destination file exists already or a hard drive failure... .. so we can just drop this one
  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.NotSupportedException)
  168. {
  169. MainConsole.Instance.Output("Unable to Copy OpenSim.ini.example to OpenSim.ini, The current directory is invalid.\n");
  170. }
  171. }
  172. if (!(StanaloneCommon_ProperCased || StanaloneCommon_lowercased))
  173. {
  174. try
  175. {
  176. File.Copy(Path.Combine(Path.Combine(Util.configDir(), "config-include"), "StandaloneCommon.ini.example"),
  177. Path.Combine(Path.Combine(Util.configDir(), "config-include"), "StandaloneCommon.ini"));
  178. }
  179. catch (UnauthorizedAccessException)
  180. {
  181. MainConsole.Instance.Output("Unable to Copy StandaloneCommon.ini.example to StandaloneCommon.ini, Make sure OpenSim has the required permissions\n");
  182. }
  183. catch (ArgumentException)
  184. {
  185. MainConsole.Instance.Output("Unable to Copy StandaloneCommon.ini.example to StandaloneCommon.ini, The current directory is invalid.\n");
  186. }
  187. catch (System.IO.PathTooLongException)
  188. {
  189. MainConsole.Instance.Output("Unable to Copy StandaloneCommon.ini.example to StandaloneCommon.ini, the Path to these files is too long.\n");
  190. }
  191. catch (System.IO.DirectoryNotFoundException)
  192. {
  193. MainConsole.Instance.Output("Unable to Copy StandaloneCommon.ini.example to StandaloneCommon.ini, the current directory is reporting as not found.\n");
  194. }
  195. catch (System.IO.FileNotFoundException)
  196. {
  197. 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");
  198. }
  199. catch (System.IO.IOException)
  200. {
  201. // Destination file exists already or a hard drive failure... .. so we can just drop this one
  202. //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");
  203. }
  204. catch (System.NotSupportedException)
  205. {
  206. MainConsole.Instance.Output("Unable to Copy StandaloneCommon.ini.example to StandaloneCommon.ini, The current directory is invalid.\n");
  207. }
  208. }
  209. }
  210. MainConsole.Instance = null;
  211. }
  212. */
  213. configSource.Alias.AddAlias("On", true);
  214. configSource.Alias.AddAlias("Off", false);
  215. configSource.Alias.AddAlias("True", true);
  216. configSource.Alias.AddAlias("False", false);
  217. configSource.Alias.AddAlias("Yes", true);
  218. configSource.Alias.AddAlias("No", false);
  219. configSource.AddSwitch("Startup", "background");
  220. configSource.AddSwitch("Startup", "inifile");
  221. configSource.AddSwitch("Startup", "inimaster");
  222. configSource.AddSwitch("Startup", "inidirectory");
  223. configSource.AddSwitch("Startup", "physics");
  224. configSource.AddSwitch("Startup", "gui");
  225. configSource.AddSwitch("Startup", "console");
  226. configSource.AddSwitch("Startup", "save_crashes");
  227. configSource.AddSwitch("Startup", "crash_dir");
  228. configSource.AddConfig("StandAlone");
  229. configSource.AddConfig("Network");
  230. // Check if we're running in the background or not
  231. bool background = configSource.Configs["Startup"].GetBoolean("background", false);
  232. // Check if we're saving crashes
  233. m_saveCrashDumps = configSource.Configs["Startup"].GetBoolean("save_crashes", false);
  234. // load Crash directory config
  235. m_crashDir = configSource.Configs["Startup"].GetString("crash_dir", m_crashDir);
  236. if (background)
  237. {
  238. m_sim = new OpenSimBackground(configSource);
  239. m_sim.Startup();
  240. }
  241. else
  242. {
  243. m_sim = new OpenSim(configSource);
  244. m_sim.Startup();
  245. while (true)
  246. {
  247. try
  248. {
  249. // Block thread here for input
  250. MainConsole.Instance.Prompt();
  251. }
  252. catch (Exception e)
  253. {
  254. m_log.ErrorFormat("Command error: {0}", e);
  255. }
  256. }
  257. }
  258. }
  259. private static bool _IsHandlingException = false; // Make sure we don't go recursive on ourself
  260. /// <summary>
  261. /// Global exception handler -- all unhandlet exceptions end up here :)
  262. /// </summary>
  263. /// <param name="sender"></param>
  264. /// <param name="e"></param>
  265. private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
  266. {
  267. if (_IsHandlingException)
  268. {
  269. return;
  270. }
  271. _IsHandlingException = true;
  272. // TODO: Add config option to allow users to turn off error reporting
  273. // TODO: Post error report (disabled for now)
  274. string msg = String.Empty;
  275. msg += "\r\n";
  276. msg += "APPLICATION EXCEPTION DETECTED: " + e.ToString() + "\r\n";
  277. msg += "\r\n";
  278. msg += "Exception: " + e.ExceptionObject.ToString() + "\r\n";
  279. Exception ex = (Exception) e.ExceptionObject;
  280. if (ex.InnerException != null)
  281. {
  282. msg += "InnerException: " + ex.InnerException.ToString() + "\r\n";
  283. }
  284. msg += "\r\n";
  285. msg += "Application is terminating: " + e.IsTerminating.ToString() + "\r\n";
  286. m_log.ErrorFormat("[APPLICATION]: {0}", msg);
  287. if (m_saveCrashDumps)
  288. {
  289. // Log exception to disk
  290. try
  291. {
  292. if (!Directory.Exists(m_crashDir))
  293. {
  294. Directory.CreateDirectory(m_crashDir);
  295. }
  296. string log = Util.GetUniqueFilename(ex.GetType() + ".txt");
  297. using (StreamWriter m_crashLog = new StreamWriter(Path.Combine(m_crashDir, log)))
  298. {
  299. m_crashLog.WriteLine(msg);
  300. }
  301. File.Copy("OpenSim.ini", Path.Combine(m_crashDir, log + "_OpenSim.ini"), true);
  302. }
  303. catch (Exception e2)
  304. {
  305. m_log.ErrorFormat("[CRASH LOGGER CRASHED]: {0}", e2);
  306. }
  307. }
  308. _IsHandlingException = false;
  309. }
  310. }
  311. }