Program.cs 6.7 KB

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