Application.cs 17 KB

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