Program.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. using MediaBrowser.Common.Implementations.IO;
  2. using MediaBrowser.Common.Implementations.Logging;
  3. using MediaBrowser.Model.Logging;
  4. using MediaBrowser.Server.Implementations;
  5. using MediaBrowser.Server.Mono.Native;
  6. using MediaBrowser.Server.Startup.Common;
  7. using Microsoft.Win32;
  8. using System;
  9. using System.Diagnostics;
  10. using System.IO;
  11. using System.Linq;
  12. using System.Net;
  13. using System.Net.Security;
  14. using System.Reflection;
  15. using System.Security.Cryptography.X509Certificates;
  16. using System.Threading.Tasks;
  17. namespace MediaBrowser.Server.Mono
  18. {
  19. public class MainClass
  20. {
  21. private static ApplicationHost _appHost;
  22. private static ILogger _logger;
  23. public static void Main(string[] args)
  24. {
  25. var applicationPath = Assembly.GetEntryAssembly().Location;
  26. var options = new StartupOptions();
  27. // Allow this to be specified on the command line.
  28. var customProgramDataPath = options.GetOption("-programdata");
  29. var appPaths = CreateApplicationPaths(applicationPath, customProgramDataPath);
  30. var logManager = new NlogManager(appPaths.LogDirectoryPath, "server");
  31. logManager.ReloadLogger(LogSeverity.Info);
  32. logManager.AddConsoleOutput();
  33. var logger = _logger = logManager.GetLogger("Main");
  34. ApplicationHost.LogEnvironmentInfo(logger, appPaths, true);
  35. AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
  36. try
  37. {
  38. RunApplication(appPaths, logManager, options);
  39. }
  40. finally
  41. {
  42. logger.Info("Shutting down");
  43. _appHost.Dispose();
  44. }
  45. }
  46. private static ServerApplicationPaths CreateApplicationPaths(string applicationPath, string programDataPath)
  47. {
  48. if (string.IsNullOrEmpty(programDataPath))
  49. {
  50. programDataPath = ApplicationPathHelper.GetProgramDataPath(applicationPath);
  51. }
  52. return new ServerApplicationPaths(programDataPath, applicationPath, Path.GetDirectoryName(applicationPath));
  53. }
  54. private static readonly TaskCompletionSource<bool> ApplicationTaskCompletionSource = new TaskCompletionSource<bool>();
  55. private static void RunApplication(ServerApplicationPaths appPaths, ILogManager logManager, StartupOptions options)
  56. {
  57. SystemEvents.SessionEnding += SystemEvents_SessionEnding;
  58. // Allow all https requests
  59. ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(delegate { return true; });
  60. var fileSystem = new CommonFileSystem(logManager.GetLogger("FileSystem"), false, true);
  61. var nativeApp = new NativeApp();
  62. _appHost = new ApplicationHost(appPaths, logManager, options, fileSystem, "MBServer.Mono", nativeApp);
  63. if (options.ContainsOption("-v"))
  64. {
  65. Console.WriteLine(_appHost.ApplicationVersion.ToString());
  66. return;
  67. }
  68. Console.WriteLine("appHost.Init");
  69. var initProgress = new Progress<double>();
  70. var task = _appHost.Init(initProgress);
  71. Task.WaitAll(task);
  72. Console.WriteLine("Running startup tasks");
  73. task = _appHost.RunStartupTasks();
  74. Task.WaitAll(task);
  75. task = ApplicationTaskCompletionSource.Task;
  76. Task.WaitAll(task);
  77. }
  78. /// <summary>
  79. /// Handles the SessionEnding event of the SystemEvents control.
  80. /// </summary>
  81. /// <param name="sender">The source of the event.</param>
  82. /// <param name="e">The <see cref="SessionEndingEventArgs"/> instance containing the event data.</param>
  83. static void SystemEvents_SessionEnding(object sender, SessionEndingEventArgs e)
  84. {
  85. if (e.Reason == SessionEndReasons.SystemShutdown)
  86. {
  87. Shutdown();
  88. }
  89. }
  90. /// <summary>
  91. /// Handles the UnhandledException event of the CurrentDomain control.
  92. /// </summary>
  93. /// <param name="sender">The source of the event.</param>
  94. /// <param name="e">The <see cref="UnhandledExceptionEventArgs"/> instance containing the event data.</param>
  95. static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
  96. {
  97. var exception = (Exception)e.ExceptionObject;
  98. new UnhandledExceptionWriter(_appHost.ServerConfigurationManager.ApplicationPaths, _logger, _appHost.LogManager).Log(exception);
  99. if (!Debugger.IsAttached)
  100. {
  101. Environment.Exit(System.Runtime.InteropServices.Marshal.GetHRForException(exception));
  102. }
  103. }
  104. public static void Shutdown()
  105. {
  106. ApplicationTaskCompletionSource.SetResult(true);
  107. }
  108. public static void Restart(StartupOptions startupOptions)
  109. {
  110. _logger.Info("Disposing app host");
  111. _appHost.Dispose();
  112. _logger.Info("Starting new instance");
  113. string module = startupOptions.GetOption("-restartpath");
  114. string commandLineArgsString = startupOptions.GetOption("-restartargs") ?? string.Empty;
  115. if (string.IsNullOrWhiteSpace(module))
  116. {
  117. module = Environment.GetCommandLineArgs().First();
  118. }
  119. if (!startupOptions.ContainsOption("-restartargs"))
  120. {
  121. var args = Environment.GetCommandLineArgs()
  122. .Skip(1)
  123. .Select(NormalizeCommandLineArgument);
  124. commandLineArgsString = string.Join(" ", args.ToArray());
  125. }
  126. _logger.Info("Executable: {0}", module);
  127. _logger.Info("Arguments: {0}", commandLineArgsString);
  128. Process.Start(module, commandLineArgsString);
  129. _logger.Info("Calling Environment.Exit");
  130. Environment.Exit(0);
  131. }
  132. private static string NormalizeCommandLineArgument(string arg)
  133. {
  134. if (arg.IndexOf(" ", StringComparison.OrdinalIgnoreCase) == -1)
  135. {
  136. return arg;
  137. }
  138. return "\"" + arg + "\"";
  139. }
  140. }
  141. class NoCheckCertificatePolicy : ICertificatePolicy
  142. {
  143. public bool CheckValidationResult(ServicePoint srvPoint, X509Certificate certificate, WebRequest request, int certificateProblem)
  144. {
  145. return true;
  146. }
  147. }
  148. }